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