1 /* Copyright (c) 2016 Facebook
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 #include <linux/bpf.h>
8 #include <linux/if_link.h>
9 #include <assert.h>
10 #include <errno.h>
11 #include <signal.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/resource.h>
16 #include <arpa/inet.h>
17 #include <netinet/ether.h>
18 #include <unistd.h>
19 #include <time.h>
20 #include "bpf/libbpf.h"
21 #include <bpf/bpf.h>
22 #include "bpf_util.h"
23 #include "xdp_tx_iptunnel_common.h"
24 
25 #define STATS_INTERVAL_S 2U
26 
27 static int ifindex = -1;
28 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
29 static int rxcnt_map_fd;
30 static __u32 prog_id;
31 
32 static void int_exit(int sig)
33 {
34 	__u32 curr_prog_id = 0;
35 
36 	if (ifindex > -1) {
37 		if (bpf_get_link_xdp_id(ifindex, &curr_prog_id, xdp_flags)) {
38 			printf("bpf_get_link_xdp_id failed\n");
39 			exit(1);
40 		}
41 		if (prog_id == curr_prog_id)
42 			bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
43 		else if (!curr_prog_id)
44 			printf("couldn't find a prog id on a given iface\n");
45 		else
46 			printf("program on interface changed, not removing\n");
47 	}
48 	exit(0);
49 }
50 
51 /* simple per-protocol drop counter
52  */
53 static void poll_stats(unsigned int kill_after_s)
54 {
55 	const unsigned int nr_protos = 256;
56 	unsigned int nr_cpus = bpf_num_possible_cpus();
57 	time_t started_at = time(NULL);
58 	__u64 values[nr_cpus], prev[nr_protos][nr_cpus];
59 	__u32 proto;
60 	int i;
61 
62 	memset(prev, 0, sizeof(prev));
63 
64 	while (!kill_after_s || time(NULL) - started_at <= kill_after_s) {
65 		sleep(STATS_INTERVAL_S);
66 
67 		for (proto = 0; proto < nr_protos; proto++) {
68 			__u64 sum = 0;
69 
70 			assert(bpf_map_lookup_elem(rxcnt_map_fd, &proto,
71 						   values) == 0);
72 			for (i = 0; i < nr_cpus; i++)
73 				sum += (values[i] - prev[proto][i]);
74 
75 			if (sum)
76 				printf("proto %u: sum:%10llu pkts, rate:%10llu pkts/s\n",
77 				       proto, sum, sum / STATS_INTERVAL_S);
78 			memcpy(prev[proto], values, sizeof(values));
79 		}
80 	}
81 }
82 
83 static void usage(const char *cmd)
84 {
85 	printf("Start a XDP prog which encapsulates incoming packets\n"
86 	       "in an IPv4/v6 header and XDP_TX it out.  The dst <VIP:PORT>\n"
87 	       "is used to select packets to encapsulate\n\n");
88 	printf("Usage: %s [...]\n", cmd);
89 	printf("    -i <ifindex> Interface Index\n");
90 	printf("    -a <vip-service-address> IPv4 or IPv6\n");
91 	printf("    -p <vip-service-port> A port range (e.g. 433-444) is also allowed\n");
92 	printf("    -s <source-ip> Used in the IPTunnel header\n");
93 	printf("    -d <dest-ip> Used in the IPTunnel header\n");
94 	printf("    -m <dest-MAC> Used in sending the IP Tunneled pkt\n");
95 	printf("    -T <stop-after-X-seconds> Default: 0 (forever)\n");
96 	printf("    -P <IP-Protocol> Default is TCP\n");
97 	printf("    -S use skb-mode\n");
98 	printf("    -N enforce native mode\n");
99 	printf("    -F Force loading the XDP prog\n");
100 	printf("    -h Display this help\n");
101 }
102 
103 static int parse_ipstr(const char *ipstr, unsigned int *addr)
104 {
105 	if (inet_pton(AF_INET6, ipstr, addr) == 1) {
106 		return AF_INET6;
107 	} else if (inet_pton(AF_INET, ipstr, addr) == 1) {
108 		addr[1] = addr[2] = addr[3] = 0;
109 		return AF_INET;
110 	}
111 
112 	fprintf(stderr, "%s is an invalid IP\n", ipstr);
113 	return AF_UNSPEC;
114 }
115 
116 static int parse_ports(const char *port_str, int *min_port, int *max_port)
117 {
118 	char *end;
119 	long tmp_min_port;
120 	long tmp_max_port;
121 
122 	tmp_min_port = strtol(optarg, &end, 10);
123 	if (tmp_min_port < 1 || tmp_min_port > 65535) {
124 		fprintf(stderr, "Invalid port(s):%s\n", optarg);
125 		return 1;
126 	}
127 
128 	if (*end == '-') {
129 		end++;
130 		tmp_max_port = strtol(end, NULL, 10);
131 		if (tmp_max_port < 1 || tmp_max_port > 65535) {
132 			fprintf(stderr, "Invalid port(s):%s\n", optarg);
133 			return 1;
134 		}
135 	} else {
136 		tmp_max_port = tmp_min_port;
137 	}
138 
139 	if (tmp_min_port > tmp_max_port) {
140 		fprintf(stderr, "Invalid port(s):%s\n", optarg);
141 		return 1;
142 	}
143 
144 	if (tmp_max_port - tmp_min_port + 1 > MAX_IPTNL_ENTRIES) {
145 		fprintf(stderr, "Port range (%s) is larger than %u\n",
146 			port_str, MAX_IPTNL_ENTRIES);
147 		return 1;
148 	}
149 	*min_port = tmp_min_port;
150 	*max_port = tmp_max_port;
151 
152 	return 0;
153 }
154 
155 int main(int argc, char **argv)
156 {
157 	struct bpf_prog_load_attr prog_load_attr = {
158 		.prog_type	= BPF_PROG_TYPE_XDP,
159 	};
160 	struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
161 	int min_port = 0, max_port = 0, vip2tnl_map_fd;
162 	const char *optstr = "i:a:p:s:d:m:T:P:FSNh";
163 	unsigned char opt_flags[256] = {};
164 	struct bpf_prog_info info = {};
165 	__u32 info_len = sizeof(info);
166 	unsigned int kill_after_s = 0;
167 	struct iptnl_info tnl = {};
168 	struct bpf_object *obj;
169 	struct vip vip = {};
170 	char filename[256];
171 	int opt, prog_fd;
172 	int i, err;
173 
174 	tnl.family = AF_UNSPEC;
175 	vip.protocol = IPPROTO_TCP;
176 
177 	for (i = 0; i < strlen(optstr); i++)
178 		if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z')
179 			opt_flags[(unsigned char)optstr[i]] = 1;
180 
181 	while ((opt = getopt(argc, argv, optstr)) != -1) {
182 		unsigned short family;
183 		unsigned int *v6;
184 
185 		switch (opt) {
186 		case 'i':
187 			ifindex = atoi(optarg);
188 			break;
189 		case 'a':
190 			vip.family = parse_ipstr(optarg, vip.daddr.v6);
191 			if (vip.family == AF_UNSPEC)
192 				return 1;
193 			break;
194 		case 'p':
195 			if (parse_ports(optarg, &min_port, &max_port))
196 				return 1;
197 			break;
198 		case 'P':
199 			vip.protocol = atoi(optarg);
200 			break;
201 		case 's':
202 		case 'd':
203 			if (opt == 's')
204 				v6 = tnl.saddr.v6;
205 			else
206 				v6 = tnl.daddr.v6;
207 
208 			family = parse_ipstr(optarg, v6);
209 			if (family == AF_UNSPEC)
210 				return 1;
211 			if (tnl.family == AF_UNSPEC) {
212 				tnl.family = family;
213 			} else if (tnl.family != family) {
214 				fprintf(stderr,
215 					"The IP version of the src and dst addresses used in the IP encapsulation does not match\n");
216 				return 1;
217 			}
218 			break;
219 		case 'm':
220 			if (!ether_aton_r(optarg,
221 					  (struct ether_addr *)tnl.dmac)) {
222 				fprintf(stderr, "Invalid mac address:%s\n",
223 					optarg);
224 				return 1;
225 			}
226 			break;
227 		case 'T':
228 			kill_after_s = atoi(optarg);
229 			break;
230 		case 'S':
231 			xdp_flags |= XDP_FLAGS_SKB_MODE;
232 			break;
233 		case 'N':
234 			xdp_flags |= XDP_FLAGS_DRV_MODE;
235 			break;
236 		case 'F':
237 			xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
238 			break;
239 		default:
240 			usage(argv[0]);
241 			return 1;
242 		}
243 		opt_flags[opt] = 0;
244 	}
245 
246 	for (i = 0; i < strlen(optstr); i++) {
247 		if (opt_flags[(unsigned int)optstr[i]]) {
248 			fprintf(stderr, "Missing argument -%c\n", optstr[i]);
249 			usage(argv[0]);
250 			return 1;
251 		}
252 	}
253 
254 	if (setrlimit(RLIMIT_MEMLOCK, &r)) {
255 		perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)");
256 		return 1;
257 	}
258 
259 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
260 	prog_load_attr.file = filename;
261 
262 	if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
263 		return 1;
264 
265 	if (!prog_fd) {
266 		printf("load_bpf_file: %s\n", strerror(errno));
267 		return 1;
268 	}
269 
270 	rxcnt_map_fd = bpf_object__find_map_fd_by_name(obj, "rxcnt");
271 	vip2tnl_map_fd = bpf_object__find_map_fd_by_name(obj, "vip2tnl");
272 	if (vip2tnl_map_fd < 0 || rxcnt_map_fd < 0) {
273 		printf("bpf_object__find_map_fd_by_name failed\n");
274 		return 1;
275 	}
276 
277 	signal(SIGINT, int_exit);
278 	signal(SIGTERM, int_exit);
279 
280 	while (min_port <= max_port) {
281 		vip.dport = htons(min_port++);
282 		if (bpf_map_update_elem(vip2tnl_map_fd, &vip, &tnl,
283 					BPF_NOEXIST)) {
284 			perror("bpf_map_update_elem(&vip2tnl)");
285 			return 1;
286 		}
287 	}
288 
289 	if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
290 		printf("link set xdp fd failed\n");
291 		return 1;
292 	}
293 
294 	err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
295 	if (err) {
296 		printf("can't get prog info - %s\n", strerror(errno));
297 		return err;
298 	}
299 	prog_id = info.id;
300 
301 	poll_stats(kill_after_s);
302 
303 	bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
304 
305 	return 0;
306 }
307