1 /* Copyright (c) 2017 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 <stdio.h>
8 #include <unistd.h>
9 #include <errno.h>
10 #include <string.h>
11 #include <assert.h>
12 #include <stdlib.h>
13 
14 #include <linux/types.h>
15 typedef __u16 __sum16;
16 #include <arpa/inet.h>
17 #include <linux/if_ether.h>
18 #include <linux/if_packet.h>
19 #include <linux/ip.h>
20 #include <linux/ipv6.h>
21 #include <linux/tcp.h>
22 
23 #include <sys/wait.h>
24 #include <sys/resource.h>
25 #include <sys/types.h>
26 #include <fcntl.h>
27 
28 #include <linux/bpf.h>
29 #include <linux/err.h>
30 #include <bpf/bpf.h>
31 #include <bpf/libbpf.h>
32 #include "test_iptunnel_common.h"
33 #include "bpf_util.h"
34 #include "bpf_endian.h"
35 
36 static int error_cnt, pass_cnt;
37 
38 #define MAGIC_BYTES 123
39 
40 /* ipv4 test vector */
41 static struct {
42 	struct ethhdr eth;
43 	struct iphdr iph;
44 	struct tcphdr tcp;
45 } __packed pkt_v4 = {
46 	.eth.h_proto = bpf_htons(ETH_P_IP),
47 	.iph.ihl = 5,
48 	.iph.protocol = 6,
49 	.iph.tot_len = bpf_htons(MAGIC_BYTES),
50 	.tcp.urg_ptr = 123,
51 };
52 
53 /* ipv6 test vector */
54 static struct {
55 	struct ethhdr eth;
56 	struct ipv6hdr iph;
57 	struct tcphdr tcp;
58 } __packed pkt_v6 = {
59 	.eth.h_proto = bpf_htons(ETH_P_IPV6),
60 	.iph.nexthdr = 6,
61 	.iph.payload_len = bpf_htons(MAGIC_BYTES),
62 	.tcp.urg_ptr = 123,
63 };
64 
65 #define CHECK(condition, tag, format...) ({				\
66 	int __ret = !!(condition);					\
67 	if (__ret) {							\
68 		error_cnt++;						\
69 		printf("%s:FAIL:%s ", __func__, tag);			\
70 		printf(format);						\
71 	} else {							\
72 		pass_cnt++;						\
73 		printf("%s:PASS:%s %d nsec\n", __func__, tag, duration);\
74 	}								\
75 	__ret;								\
76 })
77 
78 static int bpf_prog_load(const char *file, enum bpf_prog_type type,
79 			 struct bpf_object **pobj, int *prog_fd)
80 {
81 	struct bpf_program *prog;
82 	struct bpf_object *obj;
83 	int err;
84 
85 	obj = bpf_object__open(file);
86 	if (IS_ERR(obj)) {
87 		error_cnt++;
88 		return -ENOENT;
89 	}
90 
91 	prog = bpf_program__next(NULL, obj);
92 	if (!prog) {
93 		bpf_object__close(obj);
94 		error_cnt++;
95 		return -ENOENT;
96 	}
97 
98 	bpf_program__set_type(prog, type);
99 	err = bpf_object__load(obj);
100 	if (err) {
101 		bpf_object__close(obj);
102 		error_cnt++;
103 		return -EINVAL;
104 	}
105 
106 	*pobj = obj;
107 	*prog_fd = bpf_program__fd(prog);
108 	return 0;
109 }
110 
111 static int bpf_find_map(const char *test, struct bpf_object *obj,
112 			const char *name)
113 {
114 	struct bpf_map *map;
115 
116 	map = bpf_object__find_map_by_name(obj, name);
117 	if (!map) {
118 		printf("%s:FAIL:map '%s' not found\n", test, name);
119 		error_cnt++;
120 		return -1;
121 	}
122 	return bpf_map__fd(map);
123 }
124 
125 static void test_pkt_access(void)
126 {
127 	const char *file = "./test_pkt_access.o";
128 	struct bpf_object *obj;
129 	__u32 duration, retval;
130 	int err, prog_fd;
131 
132 	err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd);
133 	if (err)
134 		return;
135 
136 	err = bpf_prog_test_run(prog_fd, 100000, &pkt_v4, sizeof(pkt_v4),
137 				NULL, NULL, &retval, &duration);
138 	CHECK(err || errno || retval, "ipv4",
139 	      "err %d errno %d retval %d duration %d\n",
140 	      err, errno, retval, duration);
141 
142 	err = bpf_prog_test_run(prog_fd, 100000, &pkt_v6, sizeof(pkt_v6),
143 				NULL, NULL, &retval, &duration);
144 	CHECK(err || errno || retval, "ipv6",
145 	      "err %d errno %d retval %d duration %d\n",
146 	      err, errno, retval, duration);
147 	bpf_object__close(obj);
148 }
149 
150 static void test_xdp(void)
151 {
152 	struct vip key4 = {.protocol = 6, .family = AF_INET};
153 	struct vip key6 = {.protocol = 6, .family = AF_INET6};
154 	struct iptnl_info value4 = {.family = AF_INET};
155 	struct iptnl_info value6 = {.family = AF_INET6};
156 	const char *file = "./test_xdp.o";
157 	struct bpf_object *obj;
158 	char buf[128];
159 	struct ipv6hdr *iph6 = (void *)buf + sizeof(struct ethhdr);
160 	struct iphdr *iph = (void *)buf + sizeof(struct ethhdr);
161 	__u32 duration, retval, size;
162 	int err, prog_fd, map_fd;
163 
164 	err = bpf_prog_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
165 	if (err)
166 		return;
167 
168 	map_fd = bpf_find_map(__func__, obj, "vip2tnl");
169 	if (map_fd < 0)
170 		goto out;
171 	bpf_map_update_elem(map_fd, &key4, &value4, 0);
172 	bpf_map_update_elem(map_fd, &key6, &value6, 0);
173 
174 	err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4),
175 				buf, &size, &retval, &duration);
176 
177 	CHECK(err || errno || retval != XDP_TX || size != 74 ||
178 	      iph->protocol != IPPROTO_IPIP, "ipv4",
179 	      "err %d errno %d retval %d size %d\n",
180 	      err, errno, retval, size);
181 
182 	err = bpf_prog_test_run(prog_fd, 1, &pkt_v6, sizeof(pkt_v6),
183 				buf, &size, &retval, &duration);
184 	CHECK(err || errno || retval != XDP_TX || size != 114 ||
185 	      iph6->nexthdr != IPPROTO_IPV6, "ipv6",
186 	      "err %d errno %d retval %d size %d\n",
187 	      err, errno, retval, size);
188 out:
189 	bpf_object__close(obj);
190 }
191 
192 #define MAGIC_VAL 0x1234
193 #define NUM_ITER 100000
194 #define VIP_NUM 5
195 
196 static void test_l4lb(void)
197 {
198 	unsigned int nr_cpus = bpf_num_possible_cpus();
199 	const char *file = "./test_l4lb.o";
200 	struct vip key = {.protocol = 6};
201 	struct vip_meta {
202 		__u32 flags;
203 		__u32 vip_num;
204 	} value = {.vip_num = VIP_NUM};
205 	__u32 stats_key = VIP_NUM;
206 	struct vip_stats {
207 		__u64 bytes;
208 		__u64 pkts;
209 	} stats[nr_cpus];
210 	struct real_definition {
211 		union {
212 			__be32 dst;
213 			__be32 dstv6[4];
214 		};
215 		__u8 flags;
216 	} real_def = {.dst = MAGIC_VAL};
217 	__u32 ch_key = 11, real_num = 3;
218 	__u32 duration, retval, size;
219 	int err, i, prog_fd, map_fd;
220 	__u64 bytes = 0, pkts = 0;
221 	struct bpf_object *obj;
222 	char buf[128];
223 	u32 *magic = (u32 *)buf;
224 
225 	err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd);
226 	if (err)
227 		return;
228 
229 	map_fd = bpf_find_map(__func__, obj, "vip_map");
230 	if (map_fd < 0)
231 		goto out;
232 	bpf_map_update_elem(map_fd, &key, &value, 0);
233 
234 	map_fd = bpf_find_map(__func__, obj, "ch_rings");
235 	if (map_fd < 0)
236 		goto out;
237 	bpf_map_update_elem(map_fd, &ch_key, &real_num, 0);
238 
239 	map_fd = bpf_find_map(__func__, obj, "reals");
240 	if (map_fd < 0)
241 		goto out;
242 	bpf_map_update_elem(map_fd, &real_num, &real_def, 0);
243 
244 	err = bpf_prog_test_run(prog_fd, NUM_ITER, &pkt_v4, sizeof(pkt_v4),
245 				buf, &size, &retval, &duration);
246 	CHECK(err || errno || retval != 7/*TC_ACT_REDIRECT*/ || size != 54 ||
247 	      *magic != MAGIC_VAL, "ipv4",
248 	      "err %d errno %d retval %d size %d magic %x\n",
249 	      err, errno, retval, size, *magic);
250 
251 	err = bpf_prog_test_run(prog_fd, NUM_ITER, &pkt_v6, sizeof(pkt_v6),
252 				buf, &size, &retval, &duration);
253 	CHECK(err || errno || retval != 7/*TC_ACT_REDIRECT*/ || size != 74 ||
254 	      *magic != MAGIC_VAL, "ipv6",
255 	      "err %d errno %d retval %d size %d magic %x\n",
256 	      err, errno, retval, size, *magic);
257 
258 	map_fd = bpf_find_map(__func__, obj, "stats");
259 	if (map_fd < 0)
260 		goto out;
261 	bpf_map_lookup_elem(map_fd, &stats_key, stats);
262 	for (i = 0; i < nr_cpus; i++) {
263 		bytes += stats[i].bytes;
264 		pkts += stats[i].pkts;
265 	}
266 	if (bytes != MAGIC_BYTES * NUM_ITER * 2 || pkts != NUM_ITER * 2) {
267 		error_cnt++;
268 		printf("test_l4lb:FAIL:stats %lld %lld\n", bytes, pkts);
269 	}
270 out:
271 	bpf_object__close(obj);
272 }
273 
274 static void test_tcp_estats(void)
275 {
276 	const char *file = "./test_tcp_estats.o";
277 	int err, prog_fd;
278 	struct bpf_object *obj;
279 	__u32 duration = 0;
280 
281 	err = bpf_prog_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd);
282 	CHECK(err, "", "err %d errno %d\n", err, errno);
283 	if (err)
284 		return;
285 
286 	bpf_object__close(obj);
287 }
288 
289 static inline __u64 ptr_to_u64(const void *ptr)
290 {
291 	return (__u64) (unsigned long) ptr;
292 }
293 
294 static void test_bpf_obj_id(void)
295 {
296 	const __u64 array_magic_value = 0xfaceb00c;
297 	const __u32 array_key = 0;
298 	const int nr_iters = 2;
299 	const char *file = "./test_obj_id.o";
300 	const char *jit_sysctl = "/proc/sys/net/core/bpf_jit_enable";
301 
302 	struct bpf_object *objs[nr_iters];
303 	int prog_fds[nr_iters], map_fds[nr_iters];
304 	/* +1 to test for the info_len returned by kernel */
305 	struct bpf_prog_info prog_infos[nr_iters + 1];
306 	struct bpf_map_info map_infos[nr_iters + 1];
307 	char jited_insns[128], xlated_insns[128];
308 	__u32 i, next_id, info_len, nr_id_found, duration = 0;
309 	int sysctl_fd, jit_enabled = 0, err = 0;
310 	__u64 array_value;
311 
312 	sysctl_fd = open(jit_sysctl, 0, O_RDONLY);
313 	if (sysctl_fd != -1) {
314 		char tmpc;
315 
316 		if (read(sysctl_fd, &tmpc, sizeof(tmpc)) == 1)
317 			jit_enabled = (tmpc != '0');
318 		close(sysctl_fd);
319 	}
320 
321 	err = bpf_prog_get_fd_by_id(0);
322 	CHECK(err >= 0 || errno != ENOENT,
323 	      "get-fd-by-notexist-prog-id", "err %d errno %d\n", err, errno);
324 
325 	err = bpf_map_get_fd_by_id(0);
326 	CHECK(err >= 0 || errno != ENOENT,
327 	      "get-fd-by-notexist-map-id", "err %d errno %d\n", err, errno);
328 
329 	for (i = 0; i < nr_iters; i++)
330 		objs[i] = NULL;
331 
332 	/* Check bpf_obj_get_info_by_fd() */
333 	for (i = 0; i < nr_iters; i++) {
334 		err = bpf_prog_load(file, BPF_PROG_TYPE_SOCKET_FILTER,
335 				    &objs[i], &prog_fds[i]);
336 		/* test_obj_id.o is a dumb prog. It should never fail
337 		 * to load.
338 		 */
339 		assert(!err);
340 
341 		/* Check getting prog info */
342 		info_len = sizeof(struct bpf_prog_info) * 2;
343 		prog_infos[i].jited_prog_insns = ptr_to_u64(jited_insns);
344 		prog_infos[i].jited_prog_len = sizeof(jited_insns);
345 		prog_infos[i].xlated_prog_insns = ptr_to_u64(xlated_insns);
346 		prog_infos[i].xlated_prog_len = sizeof(xlated_insns);
347 		err = bpf_obj_get_info_by_fd(prog_fds[i], &prog_infos[i],
348 					     &info_len);
349 		if (CHECK(err ||
350 			  prog_infos[i].type != BPF_PROG_TYPE_SOCKET_FILTER ||
351 			  info_len != sizeof(struct bpf_prog_info) ||
352 			  (jit_enabled && !prog_infos[i].jited_prog_len) ||
353 			  !prog_infos[i].xlated_prog_len,
354 			  "get-prog-info(fd)",
355 			  "err %d errno %d i %d type %d(%d) info_len %u(%lu) jit_enabled %d jited_prog_len %u xlated_prog_len %u\n",
356 			  err, errno, i,
357 			  prog_infos[i].type, BPF_PROG_TYPE_SOCKET_FILTER,
358 			  info_len, sizeof(struct bpf_prog_info),
359 			  jit_enabled,
360 			  prog_infos[i].jited_prog_len,
361 			  prog_infos[i].xlated_prog_len))
362 			goto done;
363 
364 		map_fds[i] = bpf_find_map(__func__, objs[i], "test_map_id");
365 		assert(map_fds[i] >= 0);
366 		err = bpf_map_update_elem(map_fds[i], &array_key,
367 					  &array_magic_value, 0);
368 		assert(!err);
369 
370 		/* Check getting map info */
371 		info_len = sizeof(struct bpf_map_info) * 2;
372 		err = bpf_obj_get_info_by_fd(map_fds[i], &map_infos[i],
373 					     &info_len);
374 		if (CHECK(err ||
375 			  map_infos[i].type != BPF_MAP_TYPE_ARRAY ||
376 			  map_infos[i].key_size != sizeof(__u32) ||
377 			  map_infos[i].value_size != sizeof(__u64) ||
378 			  map_infos[i].max_entries != 1 ||
379 			  map_infos[i].map_flags != 0 ||
380 			  info_len != sizeof(struct bpf_map_info),
381 			  "get-map-info(fd)",
382 			  "err %d errno %d type %d(%d) info_len %u(%lu) key_size %u value_size %u max_entries %u map_flags %X\n",
383 			  err, errno,
384 			  map_infos[i].type, BPF_MAP_TYPE_ARRAY,
385 			  info_len, sizeof(struct bpf_map_info),
386 			  map_infos[i].key_size,
387 			  map_infos[i].value_size,
388 			  map_infos[i].max_entries,
389 			  map_infos[i].map_flags))
390 			goto done;
391 	}
392 
393 	/* Check bpf_prog_get_next_id() */
394 	nr_id_found = 0;
395 	next_id = 0;
396 	while (!bpf_prog_get_next_id(next_id, &next_id)) {
397 		struct bpf_prog_info prog_info;
398 		int prog_fd;
399 
400 		info_len = sizeof(prog_info);
401 
402 		prog_fd = bpf_prog_get_fd_by_id(next_id);
403 		if (prog_fd < 0 && errno == ENOENT)
404 			/* The bpf_prog is in the dead row */
405 			continue;
406 		if (CHECK(prog_fd < 0, "get-prog-fd(next_id)",
407 			  "prog_fd %d next_id %d errno %d\n",
408 			  prog_fd, next_id, errno))
409 			break;
410 
411 		for (i = 0; i < nr_iters; i++)
412 			if (prog_infos[i].id == next_id)
413 				break;
414 
415 		if (i == nr_iters)
416 			continue;
417 
418 		nr_id_found++;
419 
420 		err = bpf_obj_get_info_by_fd(prog_fd, &prog_info, &info_len);
421 		CHECK(err || info_len != sizeof(struct bpf_prog_info) ||
422 		      memcmp(&prog_info, &prog_infos[i], info_len),
423 		      "get-prog-info(next_id->fd)",
424 		      "err %d errno %d info_len %u(%lu) memcmp %d\n",
425 		      err, errno, info_len, sizeof(struct bpf_prog_info),
426 		      memcmp(&prog_info, &prog_infos[i], info_len));
427 
428 		close(prog_fd);
429 	}
430 	CHECK(nr_id_found != nr_iters,
431 	      "check total prog id found by get_next_id",
432 	      "nr_id_found %u(%u)\n",
433 	      nr_id_found, nr_iters);
434 
435 	/* Check bpf_map_get_next_id() */
436 	nr_id_found = 0;
437 	next_id = 0;
438 	while (!bpf_map_get_next_id(next_id, &next_id)) {
439 		struct bpf_map_info map_info;
440 		int map_fd;
441 
442 		info_len = sizeof(map_info);
443 
444 		map_fd = bpf_map_get_fd_by_id(next_id);
445 		if (map_fd < 0 && errno == ENOENT)
446 			/* The bpf_map is in the dead row */
447 			continue;
448 		if (CHECK(map_fd < 0, "get-map-fd(next_id)",
449 			  "map_fd %d next_id %u errno %d\n",
450 			  map_fd, next_id, errno))
451 			break;
452 
453 		for (i = 0; i < nr_iters; i++)
454 			if (map_infos[i].id == next_id)
455 				break;
456 
457 		if (i == nr_iters)
458 			continue;
459 
460 		nr_id_found++;
461 
462 		err = bpf_map_lookup_elem(map_fd, &array_key, &array_value);
463 		assert(!err);
464 
465 		err = bpf_obj_get_info_by_fd(map_fd, &map_info, &info_len);
466 		CHECK(err || info_len != sizeof(struct bpf_map_info) ||
467 		      memcmp(&map_info, &map_infos[i], info_len) ||
468 		      array_value != array_magic_value,
469 		      "check get-map-info(next_id->fd)",
470 		      "err %d errno %d info_len %u(%lu) memcmp %d array_value %llu(%llu)\n",
471 		      err, errno, info_len, sizeof(struct bpf_map_info),
472 		      memcmp(&map_info, &map_infos[i], info_len),
473 		      array_value, array_magic_value);
474 
475 		close(map_fd);
476 	}
477 	CHECK(nr_id_found != nr_iters,
478 	      "check total map id found by get_next_id",
479 	      "nr_id_found %u(%u)\n",
480 	      nr_id_found, nr_iters);
481 
482 done:
483 	for (i = 0; i < nr_iters; i++)
484 		bpf_object__close(objs[i]);
485 }
486 
487 static void test_pkt_md_access(void)
488 {
489 	const char *file = "./test_pkt_md_access.o";
490 	struct bpf_object *obj;
491 	__u32 duration, retval;
492 	int err, prog_fd;
493 
494 	err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd);
495 	if (err)
496 		return;
497 
498 	err = bpf_prog_test_run(prog_fd, 10, &pkt_v4, sizeof(pkt_v4),
499 				NULL, NULL, &retval, &duration);
500 	CHECK(err || retval, "",
501 	      "err %d errno %d retval %d duration %d\n",
502 	      err, errno, retval, duration);
503 
504 	bpf_object__close(obj);
505 }
506 
507 int main(void)
508 {
509 	struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
510 
511 	setrlimit(RLIMIT_MEMLOCK, &rinf);
512 
513 	test_pkt_access();
514 	test_xdp();
515 	test_l4lb();
516 	test_tcp_estats();
517 	test_bpf_obj_id();
518 	test_pkt_md_access();
519 
520 	printf("Summary: %d PASSED, %d FAILED\n", pass_cnt, error_cnt);
521 	return error_cnt ? EXIT_FAILURE : EXIT_SUCCESS;
522 }
523