mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 10:40:15 +02:00 
			
		
		
		
	samples/bpf: add option for native and skb mode for redirect apps
When testing with a driver that has both native and generic redirect support: $ sudo ./samples/bpf/xdp_redirect -N 5 6 input: 5 output: 6 ifindex 6: 4961879 pkt/s ifindex 6: 6391319 pkt/s ifindex 6: 6419468 pkt/s $ sudo ./samples/bpf/xdp_redirect -S 5 6 input: 5 output: 6 ifindex 6: 1845435 pkt/s ifindex 6: 3882850 pkt/s ifindex 6: 3893974 pkt/s $ sudo ./samples/bpf/xdp_redirect_map -N 5 6 input: 5 output: 6 map[0] (vports) = 4, map[1] (map) = 5, map[2] (count) = 0 ifindex 6: 2207374 pkt/s ifindex 6: 6212869 pkt/s ifindex 6: 6286515 pkt/s $ sudo ./samples/bpf/xdp_redirect_map -S 5 6 input: 5 output: 6 map[0] (vports) = 4, map[1] (map) = 5, map[2] (count) = 0 ifindex 6: 5052528 pkt/s ifindex 6: 5736631 pkt/s ifindex 6: 5739962 pkt/s Signed-off-by: Andy Gospodarek <andy@greyhouse.net> Acked-by: John Fastabend <john.fastabend@gmail.com> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
		
							parent
							
								
									7924a42133
								
							
						
					
					
						commit
						24251c2647
					
				
					 2 changed files with 80 additions and 16 deletions
				
			
		| 
						 | 
					@ -10,6 +10,7 @@
 | 
				
			||||||
 * General Public License for more details.
 | 
					 * General Public License for more details.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
#include <linux/bpf.h>
 | 
					#include <linux/bpf.h>
 | 
				
			||||||
 | 
					#include <linux/if_link.h>
 | 
				
			||||||
#include <assert.h>
 | 
					#include <assert.h>
 | 
				
			||||||
#include <errno.h>
 | 
					#include <errno.h>
 | 
				
			||||||
#include <signal.h>
 | 
					#include <signal.h>
 | 
				
			||||||
| 
						 | 
					@ -17,6 +18,7 @@
 | 
				
			||||||
#include <stdlib.h>
 | 
					#include <stdlib.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <unistd.h>
 | 
					#include <unistd.h>
 | 
				
			||||||
 | 
					#include <libgen.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "bpf_load.h"
 | 
					#include "bpf_load.h"
 | 
				
			||||||
#include "bpf_util.h"
 | 
					#include "bpf_util.h"
 | 
				
			||||||
| 
						 | 
					@ -25,9 +27,11 @@
 | 
				
			||||||
static int ifindex_in;
 | 
					static int ifindex_in;
 | 
				
			||||||
static int ifindex_out;
 | 
					static int ifindex_out;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static __u32 xdp_flags;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void int_exit(int sig)
 | 
					static void int_exit(int sig)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	set_link_xdp_fd(ifindex_in, -1, 0);
 | 
						set_link_xdp_fd(ifindex_in, -1, xdp_flags);
 | 
				
			||||||
	exit(0);
 | 
						exit(0);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -56,20 +60,47 @@ static void poll_stats(int interval, int ifindex)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int main(int ac, char **argv)
 | 
					static void usage(const char *prog)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
						fprintf(stderr,
 | 
				
			||||||
 | 
							"usage: %s [OPTS] IFINDEX_IN IFINDEX_OUT\n\n"
 | 
				
			||||||
 | 
							"OPTS:\n"
 | 
				
			||||||
 | 
							"    -S    use skb-mode\n"
 | 
				
			||||||
 | 
							"    -N    enforce native mode\n",
 | 
				
			||||||
 | 
							prog);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int main(int argc, char **argv)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						const char *optstr = "SN";
 | 
				
			||||||
	char filename[256];
 | 
						char filename[256];
 | 
				
			||||||
	int ret, key = 0;
 | 
						int ret, opt, key = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 | 
						while ((opt = getopt(argc, argv, optstr)) != -1) {
 | 
				
			||||||
 | 
							switch (opt) {
 | 
				
			||||||
 | 
							case 'S':
 | 
				
			||||||
 | 
								xdp_flags |= XDP_FLAGS_SKB_MODE;
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
							case 'N':
 | 
				
			||||||
 | 
								xdp_flags |= XDP_FLAGS_DRV_MODE;
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
							default:
 | 
				
			||||||
 | 
								usage(basename(argv[0]));
 | 
				
			||||||
 | 
								return 1;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (ac != 3) {
 | 
						if (optind == argc) {
 | 
				
			||||||
		printf("usage: %s IFINDEX_IN IFINDEX_OUT\n", argv[0]);
 | 
							printf("usage: %s IFINDEX_IN IFINDEX_OUT\n", argv[0]);
 | 
				
			||||||
		return 1;
 | 
							return 1;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ifindex_in = strtoul(argv[1], NULL, 0);
 | 
						ifindex_in = strtoul(argv[optind], NULL, 0);
 | 
				
			||||||
	ifindex_out = strtoul(argv[2], NULL, 0);
 | 
						ifindex_out = strtoul(argv[optind + 1], NULL, 0);
 | 
				
			||||||
 | 
						printf("input: %d output: %d\n", ifindex_in, ifindex_out);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (load_bpf_file(filename)) {
 | 
						if (load_bpf_file(filename)) {
 | 
				
			||||||
		printf("%s", bpf_log_buf);
 | 
							printf("%s", bpf_log_buf);
 | 
				
			||||||
| 
						 | 
					@ -82,8 +113,9 @@ int main(int ac, char **argv)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	signal(SIGINT, int_exit);
 | 
						signal(SIGINT, int_exit);
 | 
				
			||||||
 | 
						signal(SIGTERM, int_exit);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (set_link_xdp_fd(ifindex_in, prog_fd[0], 0) < 0) {
 | 
						if (set_link_xdp_fd(ifindex_in, prog_fd[0], xdp_flags) < 0) {
 | 
				
			||||||
		printf("link set xdp fd failed\n");
 | 
							printf("link set xdp fd failed\n");
 | 
				
			||||||
		return 1;
 | 
							return 1;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -10,6 +10,7 @@
 | 
				
			||||||
 * General Public License for more details.
 | 
					 * General Public License for more details.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
#include <linux/bpf.h>
 | 
					#include <linux/bpf.h>
 | 
				
			||||||
 | 
					#include <linux/if_link.h>
 | 
				
			||||||
#include <assert.h>
 | 
					#include <assert.h>
 | 
				
			||||||
#include <errno.h>
 | 
					#include <errno.h>
 | 
				
			||||||
#include <signal.h>
 | 
					#include <signal.h>
 | 
				
			||||||
| 
						 | 
					@ -17,6 +18,7 @@
 | 
				
			||||||
#include <stdlib.h>
 | 
					#include <stdlib.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <unistd.h>
 | 
					#include <unistd.h>
 | 
				
			||||||
 | 
					#include <libgen.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "bpf_load.h"
 | 
					#include "bpf_load.h"
 | 
				
			||||||
#include "bpf_util.h"
 | 
					#include "bpf_util.h"
 | 
				
			||||||
| 
						 | 
					@ -25,9 +27,11 @@
 | 
				
			||||||
static int ifindex_in;
 | 
					static int ifindex_in;
 | 
				
			||||||
static int ifindex_out;
 | 
					static int ifindex_out;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static __u32 xdp_flags;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void int_exit(int sig)
 | 
					static void int_exit(int sig)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	set_link_xdp_fd(ifindex_in, -1, 0);
 | 
						set_link_xdp_fd(ifindex_in, -1, xdp_flags);
 | 
				
			||||||
	exit(0);
 | 
						exit(0);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -56,20 +60,47 @@ static void poll_stats(int interval, int ifindex)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int main(int ac, char **argv)
 | 
					static void usage(const char *prog)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
						fprintf(stderr,
 | 
				
			||||||
 | 
							"usage: %s [OPTS] IFINDEX_IN IFINDEX_OUT\n\n"
 | 
				
			||||||
 | 
							"OPTS:\n"
 | 
				
			||||||
 | 
							"    -S    use skb-mode\n"
 | 
				
			||||||
 | 
							"    -N    enforce native mode\n",
 | 
				
			||||||
 | 
							prog);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int main(int argc, char **argv)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						const char *optstr = "SN";
 | 
				
			||||||
	char filename[256];
 | 
						char filename[256];
 | 
				
			||||||
	int ret, key = 0;
 | 
						int ret, opt, key = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 | 
						while ((opt = getopt(argc, argv, optstr)) != -1) {
 | 
				
			||||||
 | 
							switch (opt) {
 | 
				
			||||||
 | 
							case 'S':
 | 
				
			||||||
 | 
								xdp_flags |= XDP_FLAGS_SKB_MODE;
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
							case 'N':
 | 
				
			||||||
 | 
								xdp_flags |= XDP_FLAGS_DRV_MODE;
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
							default:
 | 
				
			||||||
 | 
								usage(basename(argv[0]));
 | 
				
			||||||
 | 
								return 1;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (ac != 3) {
 | 
						if (optind == argc) {
 | 
				
			||||||
		printf("usage: %s IFINDEX_IN IFINDEX_OUT\n", argv[0]);
 | 
							printf("usage: %s IFINDEX_IN IFINDEX_OUT\n", argv[0]);
 | 
				
			||||||
		return 1;
 | 
							return 1;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ifindex_in = strtoul(argv[1], NULL, 0);
 | 
						ifindex_in = strtoul(argv[optind], NULL, 0);
 | 
				
			||||||
	ifindex_out = strtoul(argv[2], NULL, 0);
 | 
						ifindex_out = strtoul(argv[optind + 1], NULL, 0);
 | 
				
			||||||
 | 
						printf("input: %d output: %d\n", ifindex_in, ifindex_out);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (load_bpf_file(filename)) {
 | 
						if (load_bpf_file(filename)) {
 | 
				
			||||||
		printf("%s", bpf_log_buf);
 | 
							printf("%s", bpf_log_buf);
 | 
				
			||||||
| 
						 | 
					@ -82,8 +113,9 @@ int main(int ac, char **argv)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	signal(SIGINT, int_exit);
 | 
						signal(SIGINT, int_exit);
 | 
				
			||||||
 | 
						signal(SIGTERM, int_exit);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (set_link_xdp_fd(ifindex_in, prog_fd[0], 0) < 0) {
 | 
						if (set_link_xdp_fd(ifindex_in, prog_fd[0], xdp_flags) < 0) {
 | 
				
			||||||
		printf("link set xdp fd failed\n");
 | 
							printf("link set xdp fd failed\n");
 | 
				
			||||||
		return 1;
 | 
							return 1;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue