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 		bzero(&prog_infos[i], info_len);
344 		prog_infos[i].jited_prog_insns = ptr_to_u64(jited_insns);
345 		prog_infos[i].jited_prog_len = sizeof(jited_insns);
346 		prog_infos[i].xlated_prog_insns = ptr_to_u64(xlated_insns);
347 		prog_infos[i].xlated_prog_len = sizeof(xlated_insns);
348 		err = bpf_obj_get_info_by_fd(prog_fds[i], &prog_infos[i],
349 					     &info_len);
350 		if (CHECK(err ||
351 			  prog_infos[i].type != BPF_PROG_TYPE_SOCKET_FILTER ||
352 			  info_len != sizeof(struct bpf_prog_info) ||
353 			  (jit_enabled && !prog_infos[i].jited_prog_len) ||
354 			  !prog_infos[i].xlated_prog_len,
355 			  "get-prog-info(fd)",
356 			  "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",
357 			  err, errno, i,
358 			  prog_infos[i].type, BPF_PROG_TYPE_SOCKET_FILTER,
359 			  info_len, sizeof(struct bpf_prog_info),
360 			  jit_enabled,
361 			  prog_infos[i].jited_prog_len,
362 			  prog_infos[i].xlated_prog_len))
363 			goto done;
364 
365 		map_fds[i] = bpf_find_map(__func__, objs[i], "test_map_id");
366 		assert(map_fds[i] >= 0);
367 		err = bpf_map_update_elem(map_fds[i], &array_key,
368 					  &array_magic_value, 0);
369 		assert(!err);
370 
371 		/* Check getting map info */
372 		info_len = sizeof(struct bpf_map_info) * 2;
373 		bzero(&map_infos[i], info_len);
374 		err = bpf_obj_get_info_by_fd(map_fds[i], &map_infos[i],
375 					     &info_len);
376 		if (CHECK(err ||
377 			  map_infos[i].type != BPF_MAP_TYPE_ARRAY ||
378 			  map_infos[i].key_size != sizeof(__u32) ||
379 			  map_infos[i].value_size != sizeof(__u64) ||
380 			  map_infos[i].max_entries != 1 ||
381 			  map_infos[i].map_flags != 0 ||
382 			  info_len != sizeof(struct bpf_map_info),
383 			  "get-map-info(fd)",
384 			  "err %d errno %d type %d(%d) info_len %u(%lu) key_size %u value_size %u max_entries %u map_flags %X\n",
385 			  err, errno,
386 			  map_infos[i].type, BPF_MAP_TYPE_ARRAY,
387 			  info_len, sizeof(struct bpf_map_info),
388 			  map_infos[i].key_size,
389 			  map_infos[i].value_size,
390 			  map_infos[i].max_entries,
391 			  map_infos[i].map_flags))
392 			goto done;
393 	}
394 
395 	/* Check bpf_prog_get_next_id() */
396 	nr_id_found = 0;
397 	next_id = 0;
398 	while (!bpf_prog_get_next_id(next_id, &next_id)) {
399 		struct bpf_prog_info prog_info = {};
400 		int prog_fd;
401 
402 		info_len = sizeof(prog_info);
403 
404 		prog_fd = bpf_prog_get_fd_by_id(next_id);
405 		if (prog_fd < 0 && errno == ENOENT)
406 			/* The bpf_prog is in the dead row */
407 			continue;
408 		if (CHECK(prog_fd < 0, "get-prog-fd(next_id)",
409 			  "prog_fd %d next_id %d errno %d\n",
410 			  prog_fd, next_id, errno))
411 			break;
412 
413 		for (i = 0; i < nr_iters; i++)
414 			if (prog_infos[i].id == next_id)
415 				break;
416 
417 		if (i == nr_iters)
418 			continue;
419 
420 		nr_id_found++;
421 
422 		err = bpf_obj_get_info_by_fd(prog_fd, &prog_info, &info_len);
423 		prog_infos[i].jited_prog_insns = 0;
424 		prog_infos[i].xlated_prog_insns = 0;
425 		CHECK(err || info_len != sizeof(struct bpf_prog_info) ||
426 		      memcmp(&prog_info, &prog_infos[i], info_len),
427 		      "get-prog-info(next_id->fd)",
428 		      "err %d errno %d info_len %u(%lu) memcmp %d\n",
429 		      err, errno, info_len, sizeof(struct bpf_prog_info),
430 		      memcmp(&prog_info, &prog_infos[i], info_len));
431 
432 		close(prog_fd);
433 	}
434 	CHECK(nr_id_found != nr_iters,
435 	      "check total prog id found by get_next_id",
436 	      "nr_id_found %u(%u)\n",
437 	      nr_id_found, nr_iters);
438 
439 	/* Check bpf_map_get_next_id() */
440 	nr_id_found = 0;
441 	next_id = 0;
442 	while (!bpf_map_get_next_id(next_id, &next_id)) {
443 		struct bpf_map_info map_info = {};
444 		int map_fd;
445 
446 		info_len = sizeof(map_info);
447 
448 		map_fd = bpf_map_get_fd_by_id(next_id);
449 		if (map_fd < 0 && errno == ENOENT)
450 			/* The bpf_map is in the dead row */
451 			continue;
452 		if (CHECK(map_fd < 0, "get-map-fd(next_id)",
453 			  "map_fd %d next_id %u errno %d\n",
454 			  map_fd, next_id, errno))
455 			break;
456 
457 		for (i = 0; i < nr_iters; i++)
458 			if (map_infos[i].id == next_id)
459 				break;
460 
461 		if (i == nr_iters)
462 			continue;
463 
464 		nr_id_found++;
465 
466 		err = bpf_map_lookup_elem(map_fd, &array_key, &array_value);
467 		assert(!err);
468 
469 		err = bpf_obj_get_info_by_fd(map_fd, &map_info, &info_len);
470 		CHECK(err || info_len != sizeof(struct bpf_map_info) ||
471 		      memcmp(&map_info, &map_infos[i], info_len) ||
472 		      array_value != array_magic_value,
473 		      "check get-map-info(next_id->fd)",
474 		      "err %d errno %d info_len %u(%lu) memcmp %d array_value %llu(%llu)\n",
475 		      err, errno, info_len, sizeof(struct bpf_map_info),
476 		      memcmp(&map_info, &map_infos[i], info_len),
477 		      array_value, array_magic_value);
478 
479 		close(map_fd);
480 	}
481 	CHECK(nr_id_found != nr_iters,
482 	      "check total map id found by get_next_id",
483 	      "nr_id_found %u(%u)\n",
484 	      nr_id_found, nr_iters);
485 
486 done:
487 	for (i = 0; i < nr_iters; i++)
488 		bpf_object__close(objs[i]);
489 }
490 
491 static void test_pkt_md_access(void)
492 {
493 	const char *file = "./test_pkt_md_access.o";
494 	struct bpf_object *obj;
495 	__u32 duration, retval;
496 	int err, prog_fd;
497 
498 	err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd);
499 	if (err)
500 		return;
501 
502 	err = bpf_prog_test_run(prog_fd, 10, &pkt_v4, sizeof(pkt_v4),
503 				NULL, NULL, &retval, &duration);
504 	CHECK(err || retval, "",
505 	      "err %d errno %d retval %d duration %d\n",
506 	      err, errno, retval, duration);
507 
508 	bpf_object__close(obj);
509 }
510 
511 int main(void)
512 {
513 	struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
514 
515 	setrlimit(RLIMIT_MEMLOCK, &rinf);
516 
517 	test_pkt_access();
518 	test_xdp();
519 	test_l4lb();
520 	test_tcp_estats();
521 	test_bpf_obj_id();
522 	test_pkt_md_access();
523 
524 	printf("Summary: %d PASSED, %d FAILED\n", pass_cnt, error_cnt);
525 	return error_cnt ? EXIT_FAILURE : EXIT_SUCCESS;
526 }
527