• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2016 John Fastabend <john.r.fastabend@intel.com>
2  *
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of version 2 of the GNU General Public
5  * License as published by the Free Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful, but
8  * WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * General Public License for more details.
11  */
12 #include <linux/bpf.h>
13 #include <linux/if_link.h>
14 #include <assert.h>
15 #include <errno.h>
16 #include <signal.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <stdbool.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <libgen.h>
23 
24 #include "bpf_load.h"
25 #include "bpf_util.h"
26 #include "libbpf.h"
27 
28 static int ifindex_in;
29 static int ifindex_out;
30 static bool ifindex_out_xdp_dummy_attached = true;
31 
32 static __u32 xdp_flags;
33 
int_exit(int sig)34 static void int_exit(int sig)
35 {
36 	set_link_xdp_fd(ifindex_in, -1, xdp_flags);
37 	if (ifindex_out_xdp_dummy_attached)
38 		set_link_xdp_fd(ifindex_out, -1, xdp_flags);
39 	exit(0);
40 }
41 
poll_stats(int interval,int ifindex)42 static void poll_stats(int interval, int ifindex)
43 {
44 	unsigned int nr_cpus = bpf_num_possible_cpus();
45 	__u64 values[nr_cpus], prev[nr_cpus];
46 
47 	memset(prev, 0, sizeof(prev));
48 
49 	while (1) {
50 		__u64 sum = 0;
51 		__u32 key = 0;
52 		int i;
53 
54 		sleep(interval);
55 		assert(bpf_map_lookup_elem(map_fd[1], &key, values) == 0);
56 		for (i = 0; i < nr_cpus; i++)
57 			sum += (values[i] - prev[i]);
58 		if (sum)
59 			printf("ifindex %i: %10llu pkt/s\n",
60 			       ifindex, sum / interval);
61 		memcpy(prev, values, sizeof(values));
62 	}
63 }
64 
usage(const char * prog)65 static void usage(const char *prog)
66 {
67 	fprintf(stderr,
68 		"usage: %s [OPTS] IFINDEX_IN IFINDEX_OUT\n\n"
69 		"OPTS:\n"
70 		"    -S    use skb-mode\n"
71 		"    -N    enforce native mode\n",
72 		prog);
73 }
74 
75 
main(int argc,char ** argv)76 int main(int argc, char **argv)
77 {
78 	const char *optstr = "SN";
79 	char filename[256];
80 	int ret, opt, key = 0;
81 
82 	while ((opt = getopt(argc, argv, optstr)) != -1) {
83 		switch (opt) {
84 		case 'S':
85 			xdp_flags |= XDP_FLAGS_SKB_MODE;
86 			break;
87 		case 'N':
88 			xdp_flags |= XDP_FLAGS_DRV_MODE;
89 			break;
90 		default:
91 			usage(basename(argv[0]));
92 			return 1;
93 		}
94 	}
95 
96 	if (optind == argc) {
97 		printf("usage: %s IFINDEX_IN IFINDEX_OUT\n", argv[0]);
98 		return 1;
99 	}
100 
101 	ifindex_in = strtoul(argv[optind], NULL, 0);
102 	ifindex_out = strtoul(argv[optind + 1], NULL, 0);
103 	printf("input: %d output: %d\n", ifindex_in, ifindex_out);
104 
105 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
106 
107 	if (load_bpf_file(filename)) {
108 		printf("%s", bpf_log_buf);
109 		return 1;
110 	}
111 
112 	if (!prog_fd[0]) {
113 		printf("load_bpf_file: %s\n", strerror(errno));
114 		return 1;
115 	}
116 
117 	if (set_link_xdp_fd(ifindex_in, prog_fd[0], xdp_flags) < 0) {
118 		printf("ERROR: link set xdp fd failed on %d\n", ifindex_in);
119 		return 1;
120 	}
121 
122 	/* Loading dummy XDP prog on out-device */
123 	if (set_link_xdp_fd(ifindex_out, prog_fd[1],
124 			    (xdp_flags | XDP_FLAGS_UPDATE_IF_NOEXIST)) < 0) {
125 		printf("WARN: link set xdp fd failed on %d\n", ifindex_out);
126 		ifindex_out_xdp_dummy_attached = false;
127 	}
128 
129 	signal(SIGINT, int_exit);
130 	signal(SIGTERM, int_exit);
131 
132 	/* bpf redirect port */
133 	ret = bpf_map_update_elem(map_fd[0], &key, &ifindex_out, 0);
134 	if (ret) {
135 		perror("bpf_update_elem");
136 		goto out;
137 	}
138 
139 	poll_stats(2, ifindex_out);
140 
141 out:
142 	return 0;
143 }
144