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