xref: /linux/tools/testing/selftests/bpf/test_progs.c (revision 662f11d55ffd02933e1bd275d732b97eddccf870)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2017 Facebook
3  */
4 #define _GNU_SOURCE
5 #include "test_progs.h"
6 #include "cgroup_helpers.h"
7 #include "bpf_rlimit.h"
8 #include <argp.h>
9 #include <pthread.h>
10 #include <sched.h>
11 #include <signal.h>
12 #include <string.h>
13 #include <execinfo.h> /* backtrace */
14 #include <linux/membarrier.h>
15 #include <sys/sysinfo.h> /* get_nprocs */
16 #include <netinet/in.h>
17 #include <sys/select.h>
18 #include <sys/socket.h>
19 #include <sys/un.h>
20 
21 /* Adapted from perf/util/string.c */
22 static bool glob_match(const char *str, const char *pat)
23 {
24 	while (*str && *pat && *pat != '*') {
25 		if (*str != *pat)
26 			return false;
27 		str++;
28 		pat++;
29 	}
30 	/* Check wild card */
31 	if (*pat == '*') {
32 		while (*pat == '*')
33 			pat++;
34 		if (!*pat) /* Tail wild card matches all */
35 			return true;
36 		while (*str)
37 			if (glob_match(str++, pat))
38 				return true;
39 	}
40 	return !*str && !*pat;
41 }
42 
43 #define EXIT_NO_TEST		2
44 #define EXIT_ERR_SETUP_INFRA	3
45 
46 /* defined in test_progs.h */
47 struct test_env env = {};
48 
49 struct prog_test_def {
50 	const char *test_name;
51 	int test_num;
52 	void (*run_test)(void);
53 	void (*run_serial_test)(void);
54 	bool force_log;
55 	int error_cnt;
56 	int skip_cnt;
57 	int sub_succ_cnt;
58 	bool should_run;
59 	bool tested;
60 	bool need_cgroup_cleanup;
61 
62 	char *subtest_name;
63 	int subtest_num;
64 
65 	/* store counts before subtest started */
66 	int old_error_cnt;
67 };
68 
69 /* Override C runtime library's usleep() implementation to ensure nanosleep()
70  * is always called. Usleep is frequently used in selftests as a way to
71  * trigger kprobe and tracepoints.
72  */
73 int usleep(useconds_t usec)
74 {
75 	struct timespec ts = {
76 		.tv_sec = usec / 1000000,
77 		.tv_nsec = (usec % 1000000) * 1000,
78 	};
79 
80 	return syscall(__NR_nanosleep, &ts, NULL);
81 }
82 
83 static bool should_run(struct test_selector *sel, int num, const char *name)
84 {
85 	int i;
86 
87 	for (i = 0; i < sel->blacklist.cnt; i++) {
88 		if (glob_match(name, sel->blacklist.strs[i]))
89 			return false;
90 	}
91 
92 	for (i = 0; i < sel->whitelist.cnt; i++) {
93 		if (glob_match(name, sel->whitelist.strs[i]))
94 			return true;
95 	}
96 
97 	if (!sel->whitelist.cnt && !sel->num_set)
98 		return true;
99 
100 	return num < sel->num_set_len && sel->num_set[num];
101 }
102 
103 static void dump_test_log(const struct prog_test_def *test, bool failed)
104 {
105 	if (stdout == env.stdout)
106 		return;
107 
108 	/* worker always holds log */
109 	if (env.worker_id != -1)
110 		return;
111 
112 	fflush(stdout); /* exports env.log_buf & env.log_cnt */
113 
114 	if (env.verbosity > VERBOSE_NONE || test->force_log || failed) {
115 		if (env.log_cnt) {
116 			env.log_buf[env.log_cnt] = '\0';
117 			fprintf(env.stdout, "%s", env.log_buf);
118 			if (env.log_buf[env.log_cnt - 1] != '\n')
119 				fprintf(env.stdout, "\n");
120 		}
121 	}
122 }
123 
124 static void skip_account(void)
125 {
126 	if (env.test->skip_cnt) {
127 		env.skip_cnt++;
128 		env.test->skip_cnt = 0;
129 	}
130 }
131 
132 static void stdio_restore(void);
133 
134 /* A bunch of tests set custom affinity per-thread and/or per-process. Reset
135  * it after each test/sub-test.
136  */
137 static void reset_affinity(void)
138 {
139 
140 	cpu_set_t cpuset;
141 	int i, err;
142 
143 	CPU_ZERO(&cpuset);
144 	for (i = 0; i < env.nr_cpus; i++)
145 		CPU_SET(i, &cpuset);
146 
147 	err = sched_setaffinity(0, sizeof(cpuset), &cpuset);
148 	if (err < 0) {
149 		stdio_restore();
150 		fprintf(stderr, "Failed to reset process affinity: %d!\n", err);
151 		exit(EXIT_ERR_SETUP_INFRA);
152 	}
153 	err = pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);
154 	if (err < 0) {
155 		stdio_restore();
156 		fprintf(stderr, "Failed to reset thread affinity: %d!\n", err);
157 		exit(EXIT_ERR_SETUP_INFRA);
158 	}
159 }
160 
161 static void save_netns(void)
162 {
163 	env.saved_netns_fd = open("/proc/self/ns/net", O_RDONLY);
164 	if (env.saved_netns_fd == -1) {
165 		perror("open(/proc/self/ns/net)");
166 		exit(EXIT_ERR_SETUP_INFRA);
167 	}
168 }
169 
170 static void restore_netns(void)
171 {
172 	if (setns(env.saved_netns_fd, CLONE_NEWNET) == -1) {
173 		stdio_restore();
174 		perror("setns(CLONE_NEWNS)");
175 		exit(EXIT_ERR_SETUP_INFRA);
176 	}
177 }
178 
179 void test__end_subtest(void)
180 {
181 	struct prog_test_def *test = env.test;
182 	int sub_error_cnt = test->error_cnt - test->old_error_cnt;
183 
184 	dump_test_log(test, sub_error_cnt);
185 
186 	fprintf(stdout, "#%d/%d %s/%s:%s\n",
187 	       test->test_num, test->subtest_num, test->test_name, test->subtest_name,
188 	       sub_error_cnt ? "FAIL" : (test->skip_cnt ? "SKIP" : "OK"));
189 
190 	if (sub_error_cnt)
191 		test->error_cnt++;
192 	else if (test->skip_cnt == 0)
193 		test->sub_succ_cnt++;
194 	skip_account();
195 
196 	free(test->subtest_name);
197 	test->subtest_name = NULL;
198 }
199 
200 bool test__start_subtest(const char *name)
201 {
202 	struct prog_test_def *test = env.test;
203 
204 	if (test->subtest_name)
205 		test__end_subtest();
206 
207 	test->subtest_num++;
208 
209 	if (!name || !name[0]) {
210 		fprintf(env.stderr,
211 			"Subtest #%d didn't provide sub-test name!\n",
212 			test->subtest_num);
213 		return false;
214 	}
215 
216 	if (!should_run(&env.subtest_selector, test->subtest_num, name))
217 		return false;
218 
219 	test->subtest_name = strdup(name);
220 	if (!test->subtest_name) {
221 		fprintf(env.stderr,
222 			"Subtest #%d: failed to copy subtest name!\n",
223 			test->subtest_num);
224 		return false;
225 	}
226 	env.test->old_error_cnt = env.test->error_cnt;
227 
228 	return true;
229 }
230 
231 void test__force_log(void)
232 {
233 	env.test->force_log = true;
234 }
235 
236 void test__skip(void)
237 {
238 	env.test->skip_cnt++;
239 }
240 
241 void test__fail(void)
242 {
243 	env.test->error_cnt++;
244 }
245 
246 int test__join_cgroup(const char *path)
247 {
248 	int fd;
249 
250 	if (!env.test->need_cgroup_cleanup) {
251 		if (setup_cgroup_environment()) {
252 			fprintf(stderr,
253 				"#%d %s: Failed to setup cgroup environment\n",
254 				env.test->test_num, env.test->test_name);
255 			return -1;
256 		}
257 
258 		env.test->need_cgroup_cleanup = true;
259 	}
260 
261 	fd = create_and_get_cgroup(path);
262 	if (fd < 0) {
263 		fprintf(stderr,
264 			"#%d %s: Failed to create cgroup '%s' (errno=%d)\n",
265 			env.test->test_num, env.test->test_name, path, errno);
266 		return fd;
267 	}
268 
269 	if (join_cgroup(path)) {
270 		fprintf(stderr,
271 			"#%d %s: Failed to join cgroup '%s' (errno=%d)\n",
272 			env.test->test_num, env.test->test_name, path, errno);
273 		return -1;
274 	}
275 
276 	return fd;
277 }
278 
279 int bpf_find_map(const char *test, struct bpf_object *obj, const char *name)
280 {
281 	struct bpf_map *map;
282 
283 	map = bpf_object__find_map_by_name(obj, name);
284 	if (!map) {
285 		fprintf(stdout, "%s:FAIL:map '%s' not found\n", test, name);
286 		test__fail();
287 		return -1;
288 	}
289 	return bpf_map__fd(map);
290 }
291 
292 static bool is_jit_enabled(void)
293 {
294 	const char *jit_sysctl = "/proc/sys/net/core/bpf_jit_enable";
295 	bool enabled = false;
296 	int sysctl_fd;
297 
298 	sysctl_fd = open(jit_sysctl, 0, O_RDONLY);
299 	if (sysctl_fd != -1) {
300 		char tmpc;
301 
302 		if (read(sysctl_fd, &tmpc, sizeof(tmpc)) == 1)
303 			enabled = (tmpc != '0');
304 		close(sysctl_fd);
305 	}
306 
307 	return enabled;
308 }
309 
310 int compare_map_keys(int map1_fd, int map2_fd)
311 {
312 	__u32 key, next_key;
313 	char val_buf[PERF_MAX_STACK_DEPTH *
314 		     sizeof(struct bpf_stack_build_id)];
315 	int err;
316 
317 	err = bpf_map_get_next_key(map1_fd, NULL, &key);
318 	if (err)
319 		return err;
320 	err = bpf_map_lookup_elem(map2_fd, &key, val_buf);
321 	if (err)
322 		return err;
323 
324 	while (bpf_map_get_next_key(map1_fd, &key, &next_key) == 0) {
325 		err = bpf_map_lookup_elem(map2_fd, &next_key, val_buf);
326 		if (err)
327 			return err;
328 
329 		key = next_key;
330 	}
331 	if (errno != ENOENT)
332 		return -1;
333 
334 	return 0;
335 }
336 
337 int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len)
338 {
339 	__u32 key, next_key, *cur_key_p, *next_key_p;
340 	char *val_buf1, *val_buf2;
341 	int i, err = 0;
342 
343 	val_buf1 = malloc(stack_trace_len);
344 	val_buf2 = malloc(stack_trace_len);
345 	cur_key_p = NULL;
346 	next_key_p = &key;
347 	while (bpf_map_get_next_key(smap_fd, cur_key_p, next_key_p) == 0) {
348 		err = bpf_map_lookup_elem(smap_fd, next_key_p, val_buf1);
349 		if (err)
350 			goto out;
351 		err = bpf_map_lookup_elem(amap_fd, next_key_p, val_buf2);
352 		if (err)
353 			goto out;
354 		for (i = 0; i < stack_trace_len; i++) {
355 			if (val_buf1[i] != val_buf2[i]) {
356 				err = -1;
357 				goto out;
358 			}
359 		}
360 		key = *next_key_p;
361 		cur_key_p = &key;
362 		next_key_p = &next_key;
363 	}
364 	if (errno != ENOENT)
365 		err = -1;
366 
367 out:
368 	free(val_buf1);
369 	free(val_buf2);
370 	return err;
371 }
372 
373 int extract_build_id(char *build_id, size_t size)
374 {
375 	FILE *fp;
376 	char *line = NULL;
377 	size_t len = 0;
378 
379 	fp = popen("readelf -n ./urandom_read | grep 'Build ID'", "r");
380 	if (fp == NULL)
381 		return -1;
382 
383 	if (getline(&line, &len, fp) == -1)
384 		goto err;
385 	pclose(fp);
386 
387 	if (len > size)
388 		len = size;
389 	memcpy(build_id, line, len);
390 	build_id[len] = '\0';
391 	free(line);
392 	return 0;
393 err:
394 	pclose(fp);
395 	return -1;
396 }
397 
398 static int finit_module(int fd, const char *param_values, int flags)
399 {
400 	return syscall(__NR_finit_module, fd, param_values, flags);
401 }
402 
403 static int delete_module(const char *name, int flags)
404 {
405 	return syscall(__NR_delete_module, name, flags);
406 }
407 
408 /*
409  * Trigger synchronize_rcu() in kernel.
410  */
411 int kern_sync_rcu(void)
412 {
413 	return syscall(__NR_membarrier, MEMBARRIER_CMD_SHARED, 0, 0);
414 }
415 
416 static void unload_bpf_testmod(void)
417 {
418 	if (kern_sync_rcu())
419 		fprintf(env.stderr, "Failed to trigger kernel-side RCU sync!\n");
420 	if (delete_module("bpf_testmod", 0)) {
421 		if (errno == ENOENT) {
422 			if (env.verbosity > VERBOSE_NONE)
423 				fprintf(stdout, "bpf_testmod.ko is already unloaded.\n");
424 			return;
425 		}
426 		fprintf(env.stderr, "Failed to unload bpf_testmod.ko from kernel: %d\n", -errno);
427 		return;
428 	}
429 	if (env.verbosity > VERBOSE_NONE)
430 		fprintf(stdout, "Successfully unloaded bpf_testmod.ko.\n");
431 }
432 
433 static int load_bpf_testmod(void)
434 {
435 	int fd;
436 
437 	/* ensure previous instance of the module is unloaded */
438 	unload_bpf_testmod();
439 
440 	if (env.verbosity > VERBOSE_NONE)
441 		fprintf(stdout, "Loading bpf_testmod.ko...\n");
442 
443 	fd = open("bpf_testmod.ko", O_RDONLY);
444 	if (fd < 0) {
445 		fprintf(env.stderr, "Can't find bpf_testmod.ko kernel module: %d\n", -errno);
446 		return -ENOENT;
447 	}
448 	if (finit_module(fd, "", 0)) {
449 		fprintf(env.stderr, "Failed to load bpf_testmod.ko into the kernel: %d\n", -errno);
450 		close(fd);
451 		return -EINVAL;
452 	}
453 	close(fd);
454 
455 	if (env.verbosity > VERBOSE_NONE)
456 		fprintf(stdout, "Successfully loaded bpf_testmod.ko.\n");
457 	return 0;
458 }
459 
460 /* extern declarations for test funcs */
461 #define DEFINE_TEST(name)				\
462 	extern void test_##name(void) __weak;		\
463 	extern void serial_test_##name(void) __weak;
464 #include <prog_tests/tests.h>
465 #undef DEFINE_TEST
466 
467 static struct prog_test_def prog_test_defs[] = {
468 #define DEFINE_TEST(name) {			\
469 	.test_name = #name,			\
470 	.run_test = &test_##name,		\
471 	.run_serial_test = &serial_test_##name,	\
472 },
473 #include <prog_tests/tests.h>
474 #undef DEFINE_TEST
475 };
476 const int prog_test_cnt = ARRAY_SIZE(prog_test_defs);
477 
478 const char *argp_program_version = "test_progs 0.1";
479 const char *argp_program_bug_address = "<bpf@vger.kernel.org>";
480 const char argp_program_doc[] = "BPF selftests test runner";
481 
482 enum ARG_KEYS {
483 	ARG_TEST_NUM = 'n',
484 	ARG_TEST_NAME = 't',
485 	ARG_TEST_NAME_BLACKLIST = 'b',
486 	ARG_VERIFIER_STATS = 's',
487 	ARG_VERBOSE = 'v',
488 	ARG_GET_TEST_CNT = 'c',
489 	ARG_LIST_TEST_NAMES = 'l',
490 	ARG_TEST_NAME_GLOB_ALLOWLIST = 'a',
491 	ARG_TEST_NAME_GLOB_DENYLIST = 'd',
492 	ARG_NUM_WORKERS = 'j',
493 	ARG_DEBUG = -1,
494 };
495 
496 static const struct argp_option opts[] = {
497 	{ "num", ARG_TEST_NUM, "NUM", 0,
498 	  "Run test number NUM only " },
499 	{ "name", ARG_TEST_NAME, "NAMES", 0,
500 	  "Run tests with names containing any string from NAMES list" },
501 	{ "name-blacklist", ARG_TEST_NAME_BLACKLIST, "NAMES", 0,
502 	  "Don't run tests with names containing any string from NAMES list" },
503 	{ "verifier-stats", ARG_VERIFIER_STATS, NULL, 0,
504 	  "Output verifier statistics", },
505 	{ "verbose", ARG_VERBOSE, "LEVEL", OPTION_ARG_OPTIONAL,
506 	  "Verbose output (use -vv or -vvv for progressively verbose output)" },
507 	{ "count", ARG_GET_TEST_CNT, NULL, 0,
508 	  "Get number of selected top-level tests " },
509 	{ "list", ARG_LIST_TEST_NAMES, NULL, 0,
510 	  "List test names that would run (without running them) " },
511 	{ "allow", ARG_TEST_NAME_GLOB_ALLOWLIST, "NAMES", 0,
512 	  "Run tests with name matching the pattern (supports '*' wildcard)." },
513 	{ "deny", ARG_TEST_NAME_GLOB_DENYLIST, "NAMES", 0,
514 	  "Don't run tests with name matching the pattern (supports '*' wildcard)." },
515 	{ "workers", ARG_NUM_WORKERS, "WORKERS", OPTION_ARG_OPTIONAL,
516 	  "Number of workers to run in parallel, default to number of cpus." },
517 	{ "debug", ARG_DEBUG, NULL, 0,
518 	  "print extra debug information for test_progs." },
519 	{},
520 };
521 
522 static int libbpf_print_fn(enum libbpf_print_level level,
523 			   const char *format, va_list args)
524 {
525 	if (env.verbosity < VERBOSE_VERY && level == LIBBPF_DEBUG)
526 		return 0;
527 	vfprintf(stdout, format, args);
528 	return 0;
529 }
530 
531 static void free_str_set(const struct str_set *set)
532 {
533 	int i;
534 
535 	if (!set)
536 		return;
537 
538 	for (i = 0; i < set->cnt; i++)
539 		free((void *)set->strs[i]);
540 	free(set->strs);
541 }
542 
543 static int parse_str_list(const char *s, struct str_set *set, bool is_glob_pattern)
544 {
545 	char *input, *state = NULL, *next, **tmp, **strs = NULL;
546 	int i, cnt = 0;
547 
548 	input = strdup(s);
549 	if (!input)
550 		return -ENOMEM;
551 
552 	while ((next = strtok_r(state ? NULL : input, ",", &state))) {
553 		tmp = realloc(strs, sizeof(*strs) * (cnt + 1));
554 		if (!tmp)
555 			goto err;
556 		strs = tmp;
557 
558 		if (is_glob_pattern) {
559 			strs[cnt] = strdup(next);
560 			if (!strs[cnt])
561 				goto err;
562 		} else {
563 			strs[cnt] = malloc(strlen(next) + 2 + 1);
564 			if (!strs[cnt])
565 				goto err;
566 			sprintf(strs[cnt], "*%s*", next);
567 		}
568 
569 		cnt++;
570 	}
571 
572 	tmp = realloc(set->strs, sizeof(*strs) * (cnt + set->cnt));
573 	if (!tmp)
574 		goto err;
575 	memcpy(tmp + set->cnt, strs, sizeof(*strs) * cnt);
576 	set->strs = (const char **)tmp;
577 	set->cnt += cnt;
578 
579 	free(input);
580 	free(strs);
581 	return 0;
582 err:
583 	for (i = 0; i < cnt; i++)
584 		free(strs[i]);
585 	free(strs);
586 	free(input);
587 	return -ENOMEM;
588 }
589 
590 extern int extra_prog_load_log_flags;
591 
592 static error_t parse_arg(int key, char *arg, struct argp_state *state)
593 {
594 	struct test_env *env = state->input;
595 
596 	switch (key) {
597 	case ARG_TEST_NUM: {
598 		char *subtest_str = strchr(arg, '/');
599 
600 		if (subtest_str) {
601 			*subtest_str = '\0';
602 			if (parse_num_list(subtest_str + 1,
603 					   &env->subtest_selector.num_set,
604 					   &env->subtest_selector.num_set_len)) {
605 				fprintf(stderr,
606 					"Failed to parse subtest numbers.\n");
607 				return -EINVAL;
608 			}
609 		}
610 		if (parse_num_list(arg, &env->test_selector.num_set,
611 				   &env->test_selector.num_set_len)) {
612 			fprintf(stderr, "Failed to parse test numbers.\n");
613 			return -EINVAL;
614 		}
615 		break;
616 	}
617 	case ARG_TEST_NAME_GLOB_ALLOWLIST:
618 	case ARG_TEST_NAME: {
619 		char *subtest_str = strchr(arg, '/');
620 
621 		if (subtest_str) {
622 			*subtest_str = '\0';
623 			if (parse_str_list(subtest_str + 1,
624 					   &env->subtest_selector.whitelist,
625 					   key == ARG_TEST_NAME_GLOB_ALLOWLIST))
626 				return -ENOMEM;
627 		}
628 		if (parse_str_list(arg, &env->test_selector.whitelist,
629 				   key == ARG_TEST_NAME_GLOB_ALLOWLIST))
630 			return -ENOMEM;
631 		break;
632 	}
633 	case ARG_TEST_NAME_GLOB_DENYLIST:
634 	case ARG_TEST_NAME_BLACKLIST: {
635 		char *subtest_str = strchr(arg, '/');
636 
637 		if (subtest_str) {
638 			*subtest_str = '\0';
639 			if (parse_str_list(subtest_str + 1,
640 					   &env->subtest_selector.blacklist,
641 					   key == ARG_TEST_NAME_GLOB_DENYLIST))
642 				return -ENOMEM;
643 		}
644 		if (parse_str_list(arg, &env->test_selector.blacklist,
645 				   key == ARG_TEST_NAME_GLOB_DENYLIST))
646 			return -ENOMEM;
647 		break;
648 	}
649 	case ARG_VERIFIER_STATS:
650 		env->verifier_stats = true;
651 		break;
652 	case ARG_VERBOSE:
653 		env->verbosity = VERBOSE_NORMAL;
654 		if (arg) {
655 			if (strcmp(arg, "v") == 0) {
656 				env->verbosity = VERBOSE_VERY;
657 				extra_prog_load_log_flags = 1;
658 			} else if (strcmp(arg, "vv") == 0) {
659 				env->verbosity = VERBOSE_SUPER;
660 				extra_prog_load_log_flags = 2;
661 			} else {
662 				fprintf(stderr,
663 					"Unrecognized verbosity setting ('%s'), only -v and -vv are supported\n",
664 					arg);
665 				return -EINVAL;
666 			}
667 		}
668 
669 		if (env->verbosity > VERBOSE_NONE) {
670 			if (setenv("SELFTESTS_VERBOSE", "1", 1) == -1) {
671 				fprintf(stderr,
672 					"Unable to setenv SELFTESTS_VERBOSE=1 (errno=%d)",
673 					errno);
674 				return -EINVAL;
675 			}
676 		}
677 
678 		break;
679 	case ARG_GET_TEST_CNT:
680 		env->get_test_cnt = true;
681 		break;
682 	case ARG_LIST_TEST_NAMES:
683 		env->list_test_names = true;
684 		break;
685 	case ARG_NUM_WORKERS:
686 		if (arg) {
687 			env->workers = atoi(arg);
688 			if (!env->workers) {
689 				fprintf(stderr, "Invalid number of worker: %s.", arg);
690 				return -EINVAL;
691 			}
692 		} else {
693 			env->workers = get_nprocs();
694 		}
695 		break;
696 	case ARG_DEBUG:
697 		env->debug = true;
698 		break;
699 	case ARGP_KEY_ARG:
700 		argp_usage(state);
701 		break;
702 	case ARGP_KEY_END:
703 		break;
704 	default:
705 		return ARGP_ERR_UNKNOWN;
706 	}
707 	return 0;
708 }
709 
710 static void stdio_hijack(void)
711 {
712 #ifdef __GLIBC__
713 	env.stdout = stdout;
714 	env.stderr = stderr;
715 
716 	if (env.verbosity > VERBOSE_NONE && env.worker_id == -1) {
717 		/* nothing to do, output to stdout by default */
718 		return;
719 	}
720 
721 	/* stdout and stderr -> buffer */
722 	fflush(stdout);
723 
724 	stdout = open_memstream(&env.log_buf, &env.log_cnt);
725 	if (!stdout) {
726 		stdout = env.stdout;
727 		perror("open_memstream");
728 		return;
729 	}
730 
731 	stderr = stdout;
732 #endif
733 }
734 
735 static void stdio_restore(void)
736 {
737 #ifdef __GLIBC__
738 	if (stdout == env.stdout)
739 		return;
740 
741 	fclose(stdout);
742 
743 	stdout = env.stdout;
744 	stderr = env.stderr;
745 #endif
746 }
747 
748 /*
749  * Determine if test_progs is running as a "flavored" test runner and switch
750  * into corresponding sub-directory to load correct BPF objects.
751  *
752  * This is done by looking at executable name. If it contains "-flavor"
753  * suffix, then we are running as a flavored test runner.
754  */
755 int cd_flavor_subdir(const char *exec_name)
756 {
757 	/* General form of argv[0] passed here is:
758 	 * some/path/to/test_progs[-flavor], where -flavor part is optional.
759 	 * First cut out "test_progs[-flavor]" part, then extract "flavor"
760 	 * part, if it's there.
761 	 */
762 	const char *flavor = strrchr(exec_name, '/');
763 
764 	if (!flavor)
765 		return 0;
766 	flavor++;
767 	flavor = strrchr(flavor, '-');
768 	if (!flavor)
769 		return 0;
770 	flavor++;
771 	if (env.verbosity > VERBOSE_NONE)
772 		fprintf(stdout,	"Switching to flavor '%s' subdirectory...\n", flavor);
773 
774 	return chdir(flavor);
775 }
776 
777 int trigger_module_test_read(int read_sz)
778 {
779 	int fd, err;
780 
781 	fd = open(BPF_TESTMOD_TEST_FILE, O_RDONLY);
782 	err = -errno;
783 	if (!ASSERT_GE(fd, 0, "testmod_file_open"))
784 		return err;
785 
786 	read(fd, NULL, read_sz);
787 	close(fd);
788 
789 	return 0;
790 }
791 
792 int trigger_module_test_write(int write_sz)
793 {
794 	int fd, err;
795 	char *buf = malloc(write_sz);
796 
797 	if (!buf)
798 		return -ENOMEM;
799 
800 	memset(buf, 'a', write_sz);
801 	buf[write_sz-1] = '\0';
802 
803 	fd = open(BPF_TESTMOD_TEST_FILE, O_WRONLY);
804 	err = -errno;
805 	if (!ASSERT_GE(fd, 0, "testmod_file_open")) {
806 		free(buf);
807 		return err;
808 	}
809 
810 	write(fd, buf, write_sz);
811 	close(fd);
812 	free(buf);
813 	return 0;
814 }
815 
816 #define MAX_BACKTRACE_SZ 128
817 void crash_handler(int signum)
818 {
819 	void *bt[MAX_BACKTRACE_SZ];
820 	size_t sz;
821 
822 	sz = backtrace(bt, ARRAY_SIZE(bt));
823 
824 	if (env.test)
825 		dump_test_log(env.test, true);
826 	if (env.stdout)
827 		stdio_restore();
828 	if (env.worker_id != -1)
829 		fprintf(stderr, "[%d]: ", env.worker_id);
830 	fprintf(stderr, "Caught signal #%d!\nStack trace:\n", signum);
831 	backtrace_symbols_fd(bt, sz, STDERR_FILENO);
832 }
833 
834 static void sigint_handler(int signum)
835 {
836 	int i;
837 
838 	for (i = 0; i < env.workers; i++)
839 		if (env.worker_socks[i] > 0)
840 			close(env.worker_socks[i]);
841 }
842 
843 static int current_test_idx;
844 static pthread_mutex_t current_test_lock;
845 static pthread_mutex_t stdout_output_lock;
846 
847 struct test_result {
848 	int error_cnt;
849 	int skip_cnt;
850 	int sub_succ_cnt;
851 
852 	size_t log_cnt;
853 	char *log_buf;
854 };
855 
856 static struct test_result test_results[ARRAY_SIZE(prog_test_defs)];
857 
858 static inline const char *str_msg(const struct msg *msg, char *buf)
859 {
860 	switch (msg->type) {
861 	case MSG_DO_TEST:
862 		sprintf(buf, "MSG_DO_TEST %d", msg->do_test.test_num);
863 		break;
864 	case MSG_TEST_DONE:
865 		sprintf(buf, "MSG_TEST_DONE %d (log: %d)",
866 			msg->test_done.test_num,
867 			msg->test_done.have_log);
868 		break;
869 	case MSG_TEST_LOG:
870 		sprintf(buf, "MSG_TEST_LOG (cnt: %ld, last: %d)",
871 			strlen(msg->test_log.log_buf),
872 			msg->test_log.is_last);
873 		break;
874 	case MSG_EXIT:
875 		sprintf(buf, "MSG_EXIT");
876 		break;
877 	default:
878 		sprintf(buf, "UNKNOWN");
879 		break;
880 	}
881 
882 	return buf;
883 }
884 
885 static int send_message(int sock, const struct msg *msg)
886 {
887 	char buf[256];
888 
889 	if (env.debug)
890 		fprintf(stderr, "Sending msg: %s\n", str_msg(msg, buf));
891 	return send(sock, msg, sizeof(*msg), 0);
892 }
893 
894 static int recv_message(int sock, struct msg *msg)
895 {
896 	int ret;
897 	char buf[256];
898 
899 	memset(msg, 0, sizeof(*msg));
900 	ret = recv(sock, msg, sizeof(*msg), 0);
901 	if (ret >= 0) {
902 		if (env.debug)
903 			fprintf(stderr, "Received msg: %s\n", str_msg(msg, buf));
904 	}
905 	return ret;
906 }
907 
908 static void run_one_test(int test_num)
909 {
910 	struct prog_test_def *test = &prog_test_defs[test_num];
911 
912 	env.test = test;
913 
914 	if (test->run_test)
915 		test->run_test();
916 	else if (test->run_serial_test)
917 		test->run_serial_test();
918 
919 	/* ensure last sub-test is finalized properly */
920 	if (test->subtest_name)
921 		test__end_subtest();
922 
923 	test->tested = true;
924 
925 	dump_test_log(test, test->error_cnt);
926 
927 	reset_affinity();
928 	restore_netns();
929 	if (test->need_cgroup_cleanup)
930 		cleanup_cgroup_environment();
931 }
932 
933 struct dispatch_data {
934 	int worker_id;
935 	int sock_fd;
936 };
937 
938 static void *dispatch_thread(void *ctx)
939 {
940 	struct dispatch_data *data = ctx;
941 	int sock_fd;
942 	FILE *log_fd = NULL;
943 
944 	sock_fd = data->sock_fd;
945 
946 	while (true) {
947 		int test_to_run = -1;
948 		struct prog_test_def *test;
949 		struct test_result *result;
950 
951 		/* grab a test */
952 		{
953 			pthread_mutex_lock(&current_test_lock);
954 
955 			if (current_test_idx >= prog_test_cnt) {
956 				pthread_mutex_unlock(&current_test_lock);
957 				goto done;
958 			}
959 
960 			test = &prog_test_defs[current_test_idx];
961 			test_to_run = current_test_idx;
962 			current_test_idx++;
963 
964 			pthread_mutex_unlock(&current_test_lock);
965 		}
966 
967 		if (!test->should_run || test->run_serial_test)
968 			continue;
969 
970 		/* run test through worker */
971 		{
972 			struct msg msg_do_test;
973 
974 			msg_do_test.type = MSG_DO_TEST;
975 			msg_do_test.do_test.test_num = test_to_run;
976 			if (send_message(sock_fd, &msg_do_test) < 0) {
977 				perror("Fail to send command");
978 				goto done;
979 			}
980 			env.worker_current_test[data->worker_id] = test_to_run;
981 		}
982 
983 		/* wait for test done */
984 		{
985 			int err;
986 			struct msg msg_test_done;
987 
988 			err = recv_message(sock_fd, &msg_test_done);
989 			if (err < 0)
990 				goto error;
991 			if (msg_test_done.type != MSG_TEST_DONE)
992 				goto error;
993 			if (test_to_run != msg_test_done.test_done.test_num)
994 				goto error;
995 
996 			test->tested = true;
997 			result = &test_results[test_to_run];
998 
999 			result->error_cnt = msg_test_done.test_done.error_cnt;
1000 			result->skip_cnt = msg_test_done.test_done.skip_cnt;
1001 			result->sub_succ_cnt = msg_test_done.test_done.sub_succ_cnt;
1002 
1003 			/* collect all logs */
1004 			if (msg_test_done.test_done.have_log) {
1005 				log_fd = open_memstream(&result->log_buf, &result->log_cnt);
1006 				if (!log_fd)
1007 					goto error;
1008 
1009 				while (true) {
1010 					struct msg msg_log;
1011 
1012 					if (recv_message(sock_fd, &msg_log) < 0)
1013 						goto error;
1014 					if (msg_log.type != MSG_TEST_LOG)
1015 						goto error;
1016 
1017 					fprintf(log_fd, "%s", msg_log.test_log.log_buf);
1018 					if (msg_log.test_log.is_last)
1019 						break;
1020 				}
1021 				fclose(log_fd);
1022 				log_fd = NULL;
1023 			}
1024 			/* output log */
1025 			{
1026 				pthread_mutex_lock(&stdout_output_lock);
1027 
1028 				if (result->log_cnt) {
1029 					result->log_buf[result->log_cnt] = '\0';
1030 					fprintf(stdout, "%s", result->log_buf);
1031 					if (result->log_buf[result->log_cnt - 1] != '\n')
1032 						fprintf(stdout, "\n");
1033 				}
1034 
1035 				fprintf(stdout, "#%d %s:%s\n",
1036 					test->test_num, test->test_name,
1037 					result->error_cnt ? "FAIL" : (result->skip_cnt ? "SKIP" : "OK"));
1038 
1039 				pthread_mutex_unlock(&stdout_output_lock);
1040 			}
1041 
1042 		} /* wait for test done */
1043 	} /* while (true) */
1044 error:
1045 	if (env.debug)
1046 		fprintf(stderr, "[%d]: Protocol/IO error: %s.\n", data->worker_id, strerror(errno));
1047 
1048 	if (log_fd)
1049 		fclose(log_fd);
1050 done:
1051 	{
1052 		struct msg msg_exit;
1053 
1054 		msg_exit.type = MSG_EXIT;
1055 		if (send_message(sock_fd, &msg_exit) < 0) {
1056 			if (env.debug)
1057 				fprintf(stderr, "[%d]: send_message msg_exit: %s.\n",
1058 					data->worker_id, strerror(errno));
1059 		}
1060 	}
1061 	return NULL;
1062 }
1063 
1064 static void print_all_error_logs(void)
1065 {
1066 	int i;
1067 
1068 	if (env.fail_cnt)
1069 		fprintf(stdout, "\nAll error logs:\n");
1070 
1071 	/* print error logs again */
1072 	for (i = 0; i < prog_test_cnt; i++) {
1073 		struct prog_test_def *test;
1074 		struct test_result *result;
1075 
1076 		test = &prog_test_defs[i];
1077 		result = &test_results[i];
1078 
1079 		if (!test->tested || !result->error_cnt)
1080 			continue;
1081 
1082 		fprintf(stdout, "\n#%d %s:%s\n",
1083 			test->test_num, test->test_name,
1084 			result->error_cnt ? "FAIL" : (result->skip_cnt ? "SKIP" : "OK"));
1085 
1086 		if (result->log_cnt) {
1087 			result->log_buf[result->log_cnt] = '\0';
1088 			fprintf(stdout, "%s", result->log_buf);
1089 			if (result->log_buf[result->log_cnt - 1] != '\n')
1090 				fprintf(stdout, "\n");
1091 		}
1092 	}
1093 }
1094 
1095 static int server_main(void)
1096 {
1097 	pthread_t *dispatcher_threads;
1098 	struct dispatch_data *data;
1099 	struct sigaction sigact_int = {
1100 		.sa_handler = sigint_handler,
1101 		.sa_flags = SA_RESETHAND,
1102 	};
1103 	int i;
1104 
1105 	sigaction(SIGINT, &sigact_int, NULL);
1106 
1107 	dispatcher_threads = calloc(sizeof(pthread_t), env.workers);
1108 	data = calloc(sizeof(struct dispatch_data), env.workers);
1109 
1110 	env.worker_current_test = calloc(sizeof(int), env.workers);
1111 	for (i = 0; i < env.workers; i++) {
1112 		int rc;
1113 
1114 		data[i].worker_id = i;
1115 		data[i].sock_fd = env.worker_socks[i];
1116 		rc = pthread_create(&dispatcher_threads[i], NULL, dispatch_thread, &data[i]);
1117 		if (rc < 0) {
1118 			perror("Failed to launch dispatcher thread");
1119 			exit(EXIT_ERR_SETUP_INFRA);
1120 		}
1121 	}
1122 
1123 	/* wait for all dispatcher to finish */
1124 	for (i = 0; i < env.workers; i++) {
1125 		while (true) {
1126 			int ret = pthread_tryjoin_np(dispatcher_threads[i], NULL);
1127 
1128 			if (!ret) {
1129 				break;
1130 			} else if (ret == EBUSY) {
1131 				if (env.debug)
1132 					fprintf(stderr, "Still waiting for thread %d (test %d).\n",
1133 						i,  env.worker_current_test[i] + 1);
1134 				usleep(1000 * 1000);
1135 				continue;
1136 			} else {
1137 				fprintf(stderr, "Unexpected error joining dispatcher thread: %d", ret);
1138 				break;
1139 			}
1140 		}
1141 	}
1142 	free(dispatcher_threads);
1143 	free(env.worker_current_test);
1144 	free(data);
1145 
1146 	/* run serial tests */
1147 	save_netns();
1148 
1149 	for (int i = 0; i < prog_test_cnt; i++) {
1150 		struct prog_test_def *test = &prog_test_defs[i];
1151 		struct test_result *result = &test_results[i];
1152 
1153 		if (!test->should_run || !test->run_serial_test)
1154 			continue;
1155 
1156 		stdio_hijack();
1157 
1158 		run_one_test(i);
1159 
1160 		stdio_restore();
1161 		if (env.log_buf) {
1162 			result->log_cnt = env.log_cnt;
1163 			result->log_buf = strdup(env.log_buf);
1164 
1165 			free(env.log_buf);
1166 			env.log_buf = NULL;
1167 			env.log_cnt = 0;
1168 		}
1169 		restore_netns();
1170 
1171 		fprintf(stdout, "#%d %s:%s\n",
1172 			test->test_num, test->test_name,
1173 			test->error_cnt ? "FAIL" : (test->skip_cnt ? "SKIP" : "OK"));
1174 
1175 		result->error_cnt = test->error_cnt;
1176 		result->skip_cnt = test->skip_cnt;
1177 		result->sub_succ_cnt = test->sub_succ_cnt;
1178 	}
1179 
1180 	/* generate summary */
1181 	fflush(stderr);
1182 	fflush(stdout);
1183 
1184 	for (i = 0; i < prog_test_cnt; i++) {
1185 		struct prog_test_def *current_test;
1186 		struct test_result *result;
1187 
1188 		current_test = &prog_test_defs[i];
1189 		result = &test_results[i];
1190 
1191 		if (!current_test->tested)
1192 			continue;
1193 
1194 		env.succ_cnt += result->error_cnt ? 0 : 1;
1195 		env.skip_cnt += result->skip_cnt;
1196 		if (result->error_cnt)
1197 			env.fail_cnt++;
1198 		env.sub_succ_cnt += result->sub_succ_cnt;
1199 	}
1200 
1201 	fprintf(stdout, "Summary: %d/%d PASSED, %d SKIPPED, %d FAILED\n",
1202 		env.succ_cnt, env.sub_succ_cnt, env.skip_cnt, env.fail_cnt);
1203 
1204 	print_all_error_logs();
1205 
1206 	/* reap all workers */
1207 	for (i = 0; i < env.workers; i++) {
1208 		int wstatus, pid;
1209 
1210 		pid = waitpid(env.worker_pids[i], &wstatus, 0);
1211 		if (pid != env.worker_pids[i])
1212 			perror("Unable to reap worker");
1213 	}
1214 
1215 	return 0;
1216 }
1217 
1218 static int worker_main(int sock)
1219 {
1220 	save_netns();
1221 
1222 	while (true) {
1223 		/* receive command */
1224 		struct msg msg;
1225 
1226 		if (recv_message(sock, &msg) < 0)
1227 			goto out;
1228 
1229 		switch (msg.type) {
1230 		case MSG_EXIT:
1231 			if (env.debug)
1232 				fprintf(stderr, "[%d]: worker exit.\n",
1233 					env.worker_id);
1234 			goto out;
1235 		case MSG_DO_TEST: {
1236 			int test_to_run;
1237 			struct prog_test_def *test;
1238 			struct msg msg_done;
1239 
1240 			test_to_run = msg.do_test.test_num;
1241 			test = &prog_test_defs[test_to_run];
1242 
1243 			if (env.debug)
1244 				fprintf(stderr, "[%d]: #%d:%s running.\n",
1245 					env.worker_id,
1246 					test_to_run + 1,
1247 					test->test_name);
1248 
1249 			stdio_hijack();
1250 
1251 			run_one_test(test_to_run);
1252 
1253 			stdio_restore();
1254 
1255 			memset(&msg_done, 0, sizeof(msg_done));
1256 			msg_done.type = MSG_TEST_DONE;
1257 			msg_done.test_done.test_num = test_to_run;
1258 			msg_done.test_done.error_cnt = test->error_cnt;
1259 			msg_done.test_done.skip_cnt = test->skip_cnt;
1260 			msg_done.test_done.sub_succ_cnt = test->sub_succ_cnt;
1261 			msg_done.test_done.have_log = false;
1262 
1263 			if (env.verbosity > VERBOSE_NONE || test->force_log || test->error_cnt) {
1264 				if (env.log_cnt)
1265 					msg_done.test_done.have_log = true;
1266 			}
1267 			if (send_message(sock, &msg_done) < 0) {
1268 				perror("Fail to send message done");
1269 				goto out;
1270 			}
1271 
1272 			/* send logs */
1273 			if (msg_done.test_done.have_log) {
1274 				char *src;
1275 				size_t slen;
1276 
1277 				src = env.log_buf;
1278 				slen = env.log_cnt;
1279 				while (slen) {
1280 					struct msg msg_log;
1281 					char *dest;
1282 					size_t len;
1283 
1284 					memset(&msg_log, 0, sizeof(msg_log));
1285 					msg_log.type = MSG_TEST_LOG;
1286 					dest = msg_log.test_log.log_buf;
1287 					len = slen >= MAX_LOG_TRUNK_SIZE ? MAX_LOG_TRUNK_SIZE : slen;
1288 					memcpy(dest, src, len);
1289 
1290 					src += len;
1291 					slen -= len;
1292 					if (!slen)
1293 						msg_log.test_log.is_last = true;
1294 
1295 					assert(send_message(sock, &msg_log) >= 0);
1296 				}
1297 			}
1298 			if (env.log_buf) {
1299 				free(env.log_buf);
1300 				env.log_buf = NULL;
1301 				env.log_cnt = 0;
1302 			}
1303 			if (env.debug)
1304 				fprintf(stderr, "[%d]: #%d:%s done.\n",
1305 					env.worker_id,
1306 					test_to_run + 1,
1307 					test->test_name);
1308 			break;
1309 		} /* case MSG_DO_TEST */
1310 		default:
1311 			if (env.debug)
1312 				fprintf(stderr, "[%d]: unknown message.\n",  env.worker_id);
1313 			return -1;
1314 		}
1315 	}
1316 out:
1317 	return 0;
1318 }
1319 
1320 int main(int argc, char **argv)
1321 {
1322 	static const struct argp argp = {
1323 		.options = opts,
1324 		.parser = parse_arg,
1325 		.doc = argp_program_doc,
1326 	};
1327 	struct sigaction sigact = {
1328 		.sa_handler = crash_handler,
1329 		.sa_flags = SA_RESETHAND,
1330 		};
1331 	int err, i;
1332 
1333 	sigaction(SIGSEGV, &sigact, NULL);
1334 
1335 	err = argp_parse(&argp, argc, argv, 0, NULL, &env);
1336 	if (err)
1337 		return err;
1338 
1339 	err = cd_flavor_subdir(argv[0]);
1340 	if (err)
1341 		return err;
1342 
1343 	/* Use libbpf 1.0 API mode */
1344 	libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
1345 
1346 	libbpf_set_print(libbpf_print_fn);
1347 
1348 	srand(time(NULL));
1349 
1350 	env.jit_enabled = is_jit_enabled();
1351 	env.nr_cpus = libbpf_num_possible_cpus();
1352 	if (env.nr_cpus < 0) {
1353 		fprintf(stderr, "Failed to get number of CPUs: %d!\n",
1354 			env.nr_cpus);
1355 		return -1;
1356 	}
1357 
1358 	env.stdout = stdout;
1359 	env.stderr = stderr;
1360 
1361 	env.has_testmod = true;
1362 	if (!env.list_test_names && load_bpf_testmod()) {
1363 		fprintf(env.stderr, "WARNING! Selftests relying on bpf_testmod.ko will be skipped.\n");
1364 		env.has_testmod = false;
1365 	}
1366 
1367 	/* initializing tests */
1368 	for (i = 0; i < prog_test_cnt; i++) {
1369 		struct prog_test_def *test = &prog_test_defs[i];
1370 
1371 		test->test_num = i + 1;
1372 		if (should_run(&env.test_selector,
1373 				test->test_num, test->test_name))
1374 			test->should_run = true;
1375 		else
1376 			test->should_run = false;
1377 
1378 		if ((test->run_test == NULL && test->run_serial_test == NULL) ||
1379 		    (test->run_test != NULL && test->run_serial_test != NULL)) {
1380 			fprintf(stderr, "Test %d:%s must have either test_%s() or serial_test_%sl() defined.\n",
1381 				test->test_num, test->test_name, test->test_name, test->test_name);
1382 			exit(EXIT_ERR_SETUP_INFRA);
1383 		}
1384 	}
1385 
1386 	/* ignore workers if we are just listing */
1387 	if (env.get_test_cnt || env.list_test_names)
1388 		env.workers = 0;
1389 
1390 	/* launch workers if requested */
1391 	env.worker_id = -1; /* main process */
1392 	if (env.workers) {
1393 		env.worker_pids = calloc(sizeof(__pid_t), env.workers);
1394 		env.worker_socks = calloc(sizeof(int), env.workers);
1395 		if (env.debug)
1396 			fprintf(stdout, "Launching %d workers.\n", env.workers);
1397 		for (i = 0; i < env.workers; i++) {
1398 			int sv[2];
1399 			pid_t pid;
1400 
1401 			if (socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sv) < 0) {
1402 				perror("Fail to create worker socket");
1403 				return -1;
1404 			}
1405 			pid = fork();
1406 			if (pid < 0) {
1407 				perror("Failed to fork worker");
1408 				return -1;
1409 			} else if (pid != 0) { /* main process */
1410 				close(sv[1]);
1411 				env.worker_pids[i] = pid;
1412 				env.worker_socks[i] = sv[0];
1413 			} else { /* inside each worker process */
1414 				close(sv[0]);
1415 				env.worker_id = i;
1416 				return worker_main(sv[1]);
1417 			}
1418 		}
1419 
1420 		if (env.worker_id == -1) {
1421 			server_main();
1422 			goto out;
1423 		}
1424 	}
1425 
1426 	/* The rest of the main process */
1427 
1428 	/* on single mode */
1429 	save_netns();
1430 
1431 	for (i = 0; i < prog_test_cnt; i++) {
1432 		struct prog_test_def *test = &prog_test_defs[i];
1433 		struct test_result *result;
1434 
1435 		if (!test->should_run)
1436 			continue;
1437 
1438 		if (env.get_test_cnt) {
1439 			env.succ_cnt++;
1440 			continue;
1441 		}
1442 
1443 		if (env.list_test_names) {
1444 			fprintf(env.stdout, "%s\n", test->test_name);
1445 			env.succ_cnt++;
1446 			continue;
1447 		}
1448 
1449 		stdio_hijack();
1450 
1451 		run_one_test(i);
1452 
1453 		stdio_restore();
1454 
1455 		fprintf(env.stdout, "#%d %s:%s\n",
1456 			test->test_num, test->test_name,
1457 			test->error_cnt ? "FAIL" : (test->skip_cnt ? "SKIP" : "OK"));
1458 
1459 		result = &test_results[i];
1460 		result->error_cnt = test->error_cnt;
1461 		if (env.log_buf) {
1462 			result->log_buf = strdup(env.log_buf);
1463 			result->log_cnt = env.log_cnt;
1464 
1465 			free(env.log_buf);
1466 			env.log_buf = NULL;
1467 			env.log_cnt = 0;
1468 		}
1469 
1470 		if (test->error_cnt)
1471 			env.fail_cnt++;
1472 		else
1473 			env.succ_cnt++;
1474 
1475 		skip_account();
1476 		env.sub_succ_cnt += test->sub_succ_cnt;
1477 	}
1478 
1479 	if (env.get_test_cnt) {
1480 		printf("%d\n", env.succ_cnt);
1481 		goto out;
1482 	}
1483 
1484 	if (env.list_test_names)
1485 		goto out;
1486 
1487 	fprintf(stdout, "Summary: %d/%d PASSED, %d SKIPPED, %d FAILED\n",
1488 		env.succ_cnt, env.sub_succ_cnt, env.skip_cnt, env.fail_cnt);
1489 
1490 	print_all_error_logs();
1491 
1492 	close(env.saved_netns_fd);
1493 out:
1494 	if (!env.list_test_names && env.has_testmod)
1495 		unload_bpf_testmod();
1496 	free_str_set(&env.test_selector.blacklist);
1497 	free_str_set(&env.test_selector.whitelist);
1498 	free(env.test_selector.num_set);
1499 	free_str_set(&env.subtest_selector.blacklist);
1500 	free_str_set(&env.subtest_selector.whitelist);
1501 	free(env.subtest_selector.num_set);
1502 
1503 	if (env.succ_cnt + env.fail_cnt + env.skip_cnt == 0)
1504 		return EXIT_NO_TEST;
1505 
1506 	return env.fail_cnt ? EXIT_FAILURE : EXIT_SUCCESS;
1507 }
1508