xref: /linux/tools/testing/selftests/bpf/test_progs.c (revision 9ec7ba20c97d92fa59b351832aeeb934b3b16177)
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 <sys/sysinfo.h> /* get_nprocs */
14 #include <netinet/in.h>
15 #include <sys/select.h>
16 #include <sys/socket.h>
17 #include <linux/keyctl.h>
18 #include <sys/un.h>
19 #include <bpf/btf.h>
20 #include <time.h>
21 #include "json_writer.h"
22 
23 #include "network_helpers.h"
24 #include "verification_cert.h"
25 
26 /* backtrace() and backtrace_symbols_fd() are glibc specific,
27  * use header file when glibc is available and provide stub
28  * implementations when another libc implementation is used.
29  */
30 #ifdef __GLIBC__
31 #include <execinfo.h> /* backtrace */
32 #else
33 __weak int backtrace(void **buffer, int size)
34 {
35 	return 0;
36 }
37 
38 __weak void backtrace_symbols_fd(void *const *buffer, int size, int fd)
39 {
40 	dprintf(fd, "<backtrace not supported>\n");
41 }
42 #endif /*__GLIBC__ */
43 
44 int env_verbosity = 0;
45 
46 static bool verbose(void)
47 {
48 	return env.verbosity > VERBOSE_NONE;
49 }
50 
51 static void stdio_hijack_init(char **log_buf, size_t *log_cnt)
52 {
53 #ifdef __GLIBC__
54 	if (verbose() && env.worker_id == -1) {
55 		/* nothing to do, output to stdout by default */
56 		return;
57 	}
58 
59 	fflush(stdout);
60 	fflush(stderr);
61 
62 	stdout = open_memstream(log_buf, log_cnt);
63 	if (!stdout) {
64 		stdout = env.stdout_saved;
65 		perror("open_memstream");
66 		return;
67 	}
68 
69 	if (env.subtest_state)
70 		env.subtest_state->stdout_saved = stdout;
71 	else
72 		env.test_state->stdout_saved = stdout;
73 
74 	stderr = stdout;
75 #endif
76 }
77 
78 static void stdio_hijack(char **log_buf, size_t *log_cnt)
79 {
80 #ifdef __GLIBC__
81 	if (verbose() && env.worker_id == -1) {
82 		/* nothing to do, output to stdout by default */
83 		return;
84 	}
85 
86 	env.stdout_saved = stdout;
87 	env.stderr_saved = stderr;
88 
89 	stdio_hijack_init(log_buf, log_cnt);
90 #endif
91 }
92 
93 static pthread_mutex_t stdout_lock = PTHREAD_MUTEX_INITIALIZER;
94 
95 static void stdio_restore(void)
96 {
97 #ifdef __GLIBC__
98 	if (verbose() && env.worker_id == -1) {
99 		/* nothing to do, output to stdout by default */
100 		return;
101 	}
102 
103 	fflush(stdout);
104 
105 	pthread_mutex_lock(&stdout_lock);
106 
107 	if (env.subtest_state) {
108 		if (env.subtest_state->stdout_saved)
109 			fclose(env.subtest_state->stdout_saved);
110 		env.subtest_state->stdout_saved = NULL;
111 		stdout = env.test_state->stdout_saved;
112 		stderr = env.test_state->stdout_saved;
113 	} else {
114 		if (env.test_state->stdout_saved)
115 			fclose(env.test_state->stdout_saved);
116 		env.test_state->stdout_saved = NULL;
117 		stdout = env.stdout_saved;
118 		stderr = env.stderr_saved;
119 	}
120 
121 	pthread_mutex_unlock(&stdout_lock);
122 #endif
123 }
124 
125 static int traffic_monitor_print_fn(const char *format, va_list args)
126 {
127 	pthread_mutex_lock(&stdout_lock);
128 	vfprintf(stdout, format, args);
129 	pthread_mutex_unlock(&stdout_lock);
130 
131 	return 0;
132 }
133 
134 /* Adapted from perf/util/string.c */
135 static bool glob_match(const char *str, const char *pat)
136 {
137 	while (*str && *pat && *pat != '*') {
138 		if (*str != *pat)
139 			return false;
140 		str++;
141 		pat++;
142 	}
143 	/* Check wild card */
144 	if (*pat == '*') {
145 		while (*pat == '*')
146 			pat++;
147 		if (!*pat) /* Tail wild card matches all */
148 			return true;
149 		while (*str)
150 			if (glob_match(str++, pat))
151 				return true;
152 	}
153 	return !*str && !*pat;
154 }
155 
156 #define EXIT_NO_TEST		2
157 #define EXIT_ERR_SETUP_INFRA	3
158 
159 /* defined in test_progs.h */
160 struct test_env env = {};
161 
162 struct prog_test_def {
163 	const char *test_name;
164 	int test_num;
165 	void (*run_test)(void);
166 	void (*run_serial_test)(void);
167 	bool should_run;
168 	bool not_built;
169 	bool selected;
170 	bool need_cgroup_cleanup;
171 	bool should_tmon;
172 };
173 
174 /* Override C runtime library's usleep() implementation to ensure nanosleep()
175  * is always called. Usleep is frequently used in selftests as a way to
176  * trigger kprobe and tracepoints.
177  */
178 int usleep(useconds_t usec)
179 {
180 	struct timespec ts = {
181 		.tv_sec = usec / 1000000,
182 		.tv_nsec = (usec % 1000000) * 1000,
183 	};
184 
185 	return syscall(__NR_nanosleep, &ts, NULL);
186 }
187 
188 /* Watchdog timer is started by watchdog_start() and stopped by watchdog_stop().
189  * If timer is active for longer than env.secs_till_notify,
190  * it prints the name of the current test to the stderr.
191  * If timer is active for longer than env.secs_till_kill,
192  * it kills the thread executing the test by sending a SIGSEGV signal to it.
193  */
194 static void watchdog_timer_func(union sigval sigval)
195 {
196 	struct itimerspec timeout = {};
197 	char test_name[256];
198 	int err;
199 
200 	if (env.subtest_state)
201 		snprintf(test_name, sizeof(test_name), "%s/%s",
202 			 env.test->test_name, env.subtest_state->name);
203 	else
204 		snprintf(test_name, sizeof(test_name), "%s",
205 			 env.test->test_name);
206 
207 	switch (env.watchdog_state) {
208 	case WD_NOTIFY:
209 		fprintf(env.stderr_saved, "WATCHDOG: test case %s executes for %d seconds...\n",
210 			test_name, env.secs_till_notify);
211 		timeout.it_value.tv_sec = env.secs_till_kill - env.secs_till_notify;
212 		env.watchdog_state = WD_KILL;
213 		err = timer_settime(env.watchdog, 0, &timeout, NULL);
214 		if (err)
215 			fprintf(env.stderr_saved, "Failed to arm watchdog timer\n");
216 		break;
217 	case WD_KILL:
218 		fprintf(env.stderr_saved,
219 			"WATCHDOG: test case %s executes for %d seconds, terminating with SIGSEGV\n",
220 			test_name, env.secs_till_kill);
221 		pthread_kill(env.main_thread, SIGSEGV);
222 		break;
223 	}
224 }
225 
226 static void watchdog_start(void)
227 {
228 	struct itimerspec timeout = {};
229 	int err;
230 
231 	if (env.secs_till_kill == 0)
232 		return;
233 	if (env.secs_till_notify > 0) {
234 		env.watchdog_state = WD_NOTIFY;
235 		timeout.it_value.tv_sec = env.secs_till_notify;
236 	} else {
237 		env.watchdog_state = WD_KILL;
238 		timeout.it_value.tv_sec = env.secs_till_kill;
239 	}
240 	err = timer_settime(env.watchdog, 0, &timeout, NULL);
241 	if (err)
242 		fprintf(env.stderr_saved, "Failed to start watchdog timer\n");
243 }
244 
245 static void watchdog_stop(void)
246 {
247 	struct itimerspec timeout = {};
248 	int err;
249 
250 	env.watchdog_state = WD_NOTIFY;
251 	err = timer_settime(env.watchdog, 0, &timeout, NULL);
252 	if (err)
253 		fprintf(env.stderr_saved, "Failed to stop watchdog timer\n");
254 }
255 
256 static void watchdog_init(void)
257 {
258 	struct sigevent watchdog_sev = {
259 		.sigev_notify = SIGEV_THREAD,
260 		.sigev_notify_function = watchdog_timer_func,
261 	};
262 	int err;
263 
264 	env.main_thread = pthread_self();
265 	err = timer_create(CLOCK_MONOTONIC, &watchdog_sev, &env.watchdog);
266 	if (err)
267 		fprintf(stderr, "Failed to initialize watchdog timer\n");
268 }
269 
270 static bool should_run(struct test_selector *sel, int num, const char *name)
271 {
272 	int i;
273 
274 	for (i = 0; i < sel->blacklist.cnt; i++) {
275 		if (glob_match(name, sel->blacklist.tests[i].name) &&
276 		    !sel->blacklist.tests[i].subtest_cnt)
277 			return false;
278 	}
279 
280 	for (i = 0; i < sel->whitelist.cnt; i++) {
281 		if (glob_match(name, sel->whitelist.tests[i].name))
282 			return true;
283 	}
284 
285 	if (!sel->whitelist.cnt && !sel->num_set)
286 		return true;
287 
288 	return num < sel->num_set_len && sel->num_set[num];
289 }
290 
291 static bool match_subtest(struct test_filter_set *filter,
292 			  const char *test_name,
293 			  const char *subtest_name)
294 {
295 	int i, j;
296 
297 	for (i = 0; i < filter->cnt; i++) {
298 		if (glob_match(test_name, filter->tests[i].name)) {
299 			if (!filter->tests[i].subtest_cnt)
300 				return true;
301 
302 			for (j = 0; j < filter->tests[i].subtest_cnt; j++) {
303 				if (glob_match(subtest_name,
304 					       filter->tests[i].subtests[j]))
305 					return true;
306 			}
307 		}
308 	}
309 
310 	return false;
311 }
312 
313 static bool match_subtest_desc(struct test_filter_set *filter,
314 			       const char *test_name,
315 			       const char *subtest_name,
316 			       const char *subtest_desc)
317 {
318 	if (match_subtest(filter, test_name, subtest_name))
319 		return true;
320 
321 	if (!subtest_desc || !subtest_desc[0] ||
322 	    strcmp(subtest_name, subtest_desc) == 0)
323 		return false;
324 
325 	return match_subtest(filter, test_name, subtest_desc);
326 }
327 
328 static bool should_run_subtest(struct test_selector *sel,
329 			       struct test_selector *subtest_sel,
330 			       int subtest_num,
331 			       const char *test_name,
332 			       const char *subtest_name,
333 			       const char *subtest_desc)
334 {
335 	if (match_subtest_desc(&sel->blacklist, test_name,
336 			       subtest_name, subtest_desc))
337 		return false;
338 
339 	if (match_subtest_desc(&sel->whitelist, test_name,
340 			       subtest_name, subtest_desc))
341 		return true;
342 
343 	if (!sel->whitelist.cnt && !subtest_sel->num_set)
344 		return true;
345 
346 	return subtest_num < subtest_sel->num_set_len && subtest_sel->num_set[subtest_num];
347 }
348 
349 static bool should_tmon(struct test_selector *sel, const char *name)
350 {
351 	int i;
352 
353 	for (i = 0; i < sel->whitelist.cnt; i++) {
354 		if (glob_match(name, sel->whitelist.tests[i].name) &&
355 		    !sel->whitelist.tests[i].subtest_cnt)
356 			return true;
357 	}
358 
359 	return false;
360 }
361 
362 static char *test_result(bool failed, bool skipped)
363 {
364 	return failed ? "FAIL" : (skipped ? "SKIP" : "OK");
365 }
366 
367 #define TEST_NUM_WIDTH 7
368 
369 static void print_test_result(const struct prog_test_def *test, const struct test_state *test_state)
370 {
371 	int skipped_cnt = test_state->skip_cnt;
372 	int subtests_cnt = test_state->subtest_num;
373 
374 	fprintf(env.stdout_saved, "#%-*d %s:", TEST_NUM_WIDTH, test->test_num, test->test_name);
375 	if (test_state->error_cnt)
376 		fprintf(env.stdout_saved, "FAIL");
377 	else if (test->not_built)
378 		fprintf(env.stdout_saved, "SKIP (not built)");
379 	else if (!skipped_cnt)
380 		fprintf(env.stdout_saved, "OK");
381 	else if (skipped_cnt == subtests_cnt || !subtests_cnt)
382 		fprintf(env.stdout_saved, "SKIP");
383 	else
384 		fprintf(env.stdout_saved, "OK (SKIP: %d/%d)", skipped_cnt, subtests_cnt);
385 
386 	fprintf(env.stdout_saved, "\n");
387 }
388 
389 static void print_test_log(char *log_buf, size_t log_cnt)
390 {
391 	log_buf[log_cnt] = '\0';
392 	fprintf(env.stdout_saved, "%s", log_buf);
393 	if (log_buf[log_cnt - 1] != '\n')
394 		fprintf(env.stdout_saved, "\n");
395 }
396 
397 static void print_subtest_name(int test_num, int subtest_num,
398 			       const char *test_name, char *subtest_name,
399 			       char *result)
400 {
401 	char test_num_str[32];
402 
403 	snprintf(test_num_str, sizeof(test_num_str), "%d/%d", test_num, subtest_num);
404 
405 	fprintf(env.stdout_saved, "#%-*s %s/%s",
406 		TEST_NUM_WIDTH, test_num_str,
407 		test_name, subtest_name);
408 
409 	if (result)
410 		fprintf(env.stdout_saved, ":%s", result);
411 
412 	fprintf(env.stdout_saved, "\n");
413 }
414 
415 static void jsonw_write_log_message(json_writer_t *w, char *log_buf, size_t log_cnt)
416 {
417 	/* open_memstream (from stdio_hijack_init) ensures that log_bug is terminated by a
418 	 * null byte. Yet in parallel mode, log_buf will be NULL if there is no message.
419 	 */
420 	if (log_cnt) {
421 		jsonw_string_field(w, "message", log_buf);
422 	} else {
423 		jsonw_string_field(w, "message", "");
424 	}
425 }
426 
427 static void dump_test_log(const struct prog_test_def *test,
428 			  const struct test_state *test_state,
429 			  bool skip_ok_subtests,
430 			  bool par_exec_result,
431 			  json_writer_t *w)
432 {
433 	bool test_failed = test_state->error_cnt > 0;
434 	bool force_log = test_state->force_log;
435 	bool print_test = verbose() || force_log || test_failed;
436 	int i;
437 	struct subtest_state *subtest_state;
438 	bool subtest_failed;
439 	bool subtest_filtered;
440 	bool print_subtest;
441 
442 	/* we do not print anything in the worker thread */
443 	if (env.worker_id != -1)
444 		return;
445 
446 	/* there is nothing to print when verbose log is used and execution
447 	 * is not in parallel mode
448 	 */
449 	if (verbose() && !par_exec_result)
450 		return;
451 
452 	if (test_state->log_cnt && print_test)
453 		print_test_log(test_state->log_buf, test_state->log_cnt);
454 
455 	if (w && print_test) {
456 		jsonw_start_object(w);
457 		jsonw_string_field(w, "name", test->test_name);
458 		jsonw_uint_field(w, "number", test->test_num);
459 		jsonw_write_log_message(w, test_state->log_buf, test_state->log_cnt);
460 		jsonw_bool_field(w, "failed", test_failed);
461 		jsonw_name(w, "subtests");
462 		jsonw_start_array(w);
463 	}
464 
465 	for (i = 0; i < test_state->subtest_num; i++) {
466 		subtest_state = &test_state->subtest_states[i];
467 		subtest_failed = subtest_state->error_cnt;
468 		subtest_filtered = subtest_state->filtered;
469 		print_subtest = verbose() || force_log || subtest_failed;
470 
471 		if ((skip_ok_subtests && !subtest_failed) || subtest_filtered)
472 			continue;
473 
474 		if (subtest_state->log_cnt && print_subtest) {
475 			print_test_log(subtest_state->log_buf,
476 				       subtest_state->log_cnt);
477 		}
478 
479 		print_subtest_name(test->test_num, i + 1,
480 				   test->test_name, subtest_state->name,
481 				   test_result(subtest_state->error_cnt,
482 					       subtest_state->skipped));
483 
484 		if (w && print_subtest) {
485 			jsonw_start_object(w);
486 			jsonw_string_field(w, "name", subtest_state->name);
487 			jsonw_uint_field(w, "number", i+1);
488 			jsonw_write_log_message(w, subtest_state->log_buf, subtest_state->log_cnt);
489 			jsonw_bool_field(w, "failed", subtest_failed);
490 			jsonw_end_object(w);
491 		}
492 	}
493 
494 	if (w && print_test) {
495 		jsonw_end_array(w);
496 		jsonw_end_object(w);
497 	}
498 
499 	print_test_result(test, test_state);
500 }
501 
502 /* A bunch of tests set custom affinity per-thread and/or per-process. Reset
503  * it after each test/sub-test.
504  */
505 static void reset_affinity(void)
506 {
507 	cpu_set_t cpuset;
508 	int i, err;
509 
510 	CPU_ZERO(&cpuset);
511 	for (i = 0; i < env.nr_cpus; i++)
512 		CPU_SET(i, &cpuset);
513 
514 	err = sched_setaffinity(0, sizeof(cpuset), &cpuset);
515 	if (err < 0) {
516 		fprintf(stderr, "Failed to reset process affinity: %d!\n", err);
517 		exit(EXIT_ERR_SETUP_INFRA);
518 	}
519 	err = pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);
520 	if (err < 0) {
521 		fprintf(stderr, "Failed to reset thread affinity: %d!\n", err);
522 		exit(EXIT_ERR_SETUP_INFRA);
523 	}
524 }
525 
526 static void save_netns(void)
527 {
528 	env.saved_netns_fd = open("/proc/self/ns/net", O_RDONLY);
529 	if (env.saved_netns_fd == -1) {
530 		perror("open(/proc/self/ns/net)");
531 		exit(EXIT_ERR_SETUP_INFRA);
532 	}
533 }
534 
535 static void restore_netns(void)
536 {
537 	if (setns(env.saved_netns_fd, CLONE_NEWNET) == -1) {
538 		perror("setns(CLONE_NEWNS)");
539 		exit(EXIT_ERR_SETUP_INFRA);
540 	}
541 }
542 
543 void test__end_subtest(void)
544 {
545 	struct prog_test_def *test = env.test;
546 	struct test_state *test_state = env.test_state;
547 	struct subtest_state *subtest_state = env.subtest_state;
548 
549 	if (subtest_state->error_cnt) {
550 		test_state->error_cnt++;
551 	} else {
552 		if (!subtest_state->skipped)
553 			test_state->sub_succ_cnt++;
554 		else
555 			test_state->skip_cnt++;
556 	}
557 
558 	if (verbose() && !env.workers)
559 		print_subtest_name(test->test_num, test_state->subtest_num,
560 				   test->test_name, subtest_state->name,
561 				   test_result(subtest_state->error_cnt,
562 					       subtest_state->skipped));
563 
564 	stdio_restore();
565 
566 	env.subtest_state = NULL;
567 }
568 
569 bool test__start_subtest_with_desc(const char *subtest_name, const char *subtest_desc)
570 {
571 	struct prog_test_def *test = env.test;
572 	struct test_state *state = env.test_state;
573 	struct subtest_state *subtest_state;
574 	const char *subtest_display_name;
575 	size_t sub_state_size = sizeof(*subtest_state);
576 	void *tmp;
577 
578 	if (env.subtest_state)
579 		test__end_subtest();
580 
581 	state->subtest_num++;
582 	tmp = realloc(state->subtest_states, state->subtest_num * sub_state_size);
583 	if (!tmp) {
584 		state->subtest_num--;
585 		fprintf(stderr, "Not enough memory to allocate subtest result\n");
586 		return false;
587 	}
588 	state->subtest_states = tmp;
589 
590 	subtest_state = &state->subtest_states[state->subtest_num - 1];
591 
592 	memset(subtest_state, 0, sub_state_size);
593 
594 	if (!subtest_name || !subtest_name[0]) {
595 		fprintf(env.stderr_saved,
596 			"Subtest #%d didn't provide sub-test name!\n",
597 			state->subtest_num);
598 		return false;
599 	}
600 
601 	subtest_display_name = subtest_desc ? subtest_desc : subtest_name;
602 
603 	subtest_state->name = strdup(subtest_display_name);
604 	if (!subtest_state->name) {
605 		fprintf(env.stderr_saved,
606 			"Subtest #%d: failed to copy subtest name!\n",
607 			state->subtest_num);
608 		return false;
609 	}
610 
611 	if (!should_run_subtest(&env.test_selector,
612 				&env.subtest_selector,
613 				state->subtest_num,
614 				test->test_name,
615 				subtest_name,
616 				subtest_desc)) {
617 		subtest_state->filtered = true;
618 		return false;
619 	}
620 
621 	subtest_state->should_tmon = match_subtest_desc(&env.tmon_selector.whitelist,
622 							test->test_name, subtest_name,
623 							subtest_desc);
624 
625 	env.subtest_state = subtest_state;
626 	stdio_hijack_init(&subtest_state->log_buf, &subtest_state->log_cnt);
627 	watchdog_start();
628 
629 	return true;
630 }
631 
632 bool test__start_subtest(const char *subtest_name)
633 {
634 	return test__start_subtest_with_desc(subtest_name, NULL);
635 }
636 
637 void test__force_log(void)
638 {
639 	env.test_state->force_log = true;
640 }
641 
642 void test__skip(void)
643 {
644 	if (env.subtest_state)
645 		env.subtest_state->skipped = true;
646 	else
647 		env.test_state->skip_cnt++;
648 }
649 
650 void test__fail(void)
651 {
652 	if (env.subtest_state)
653 		env.subtest_state->error_cnt++;
654 	else
655 		env.test_state->error_cnt++;
656 }
657 
658 int test__join_cgroup(const char *path)
659 {
660 	int fd;
661 
662 	if (!env.test->need_cgroup_cleanup) {
663 		if (setup_cgroup_environment()) {
664 			fprintf(stderr,
665 				"#%d %s: Failed to setup cgroup environment\n",
666 				env.test->test_num, env.test->test_name);
667 			return -1;
668 		}
669 
670 		env.test->need_cgroup_cleanup = true;
671 	}
672 
673 	fd = create_and_get_cgroup(path);
674 	if (fd < 0) {
675 		fprintf(stderr,
676 			"#%d %s: Failed to create cgroup '%s' (errno=%d)\n",
677 			env.test->test_num, env.test->test_name, path, errno);
678 		return fd;
679 	}
680 
681 	if (join_cgroup(path)) {
682 		fprintf(stderr,
683 			"#%d %s: Failed to join cgroup '%s' (errno=%d)\n",
684 			env.test->test_num, env.test->test_name, path, errno);
685 		return -1;
686 	}
687 
688 	return fd;
689 }
690 
691 int bpf_find_map(const char *test, struct bpf_object *obj, const char *name)
692 {
693 	struct bpf_map *map;
694 
695 	map = bpf_object__find_map_by_name(obj, name);
696 	if (!map) {
697 		fprintf(stdout, "%s:FAIL:map '%s' not found\n", test, name);
698 		test__fail();
699 		return -1;
700 	}
701 	return bpf_map__fd(map);
702 }
703 
704 int compare_map_keys(int map1_fd, int map2_fd)
705 {
706 	__u32 key, next_key;
707 	char val_buf[PERF_MAX_STACK_DEPTH *
708 		     sizeof(struct bpf_stack_build_id)];
709 	int err;
710 
711 	err = bpf_map_get_next_key(map1_fd, NULL, &key);
712 	if (err)
713 		return err;
714 	err = bpf_map_lookup_elem(map2_fd, &key, val_buf);
715 	if (err)
716 		return err;
717 
718 	while (bpf_map_get_next_key(map1_fd, &key, &next_key) == 0) {
719 		err = bpf_map_lookup_elem(map2_fd, &next_key, val_buf);
720 		if (err)
721 			return err;
722 
723 		key = next_key;
724 	}
725 	if (errno != ENOENT)
726 		return -1;
727 
728 	return 0;
729 }
730 
731 int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len)
732 {
733 	__u32 key, next_key, *cur_key_p, *next_key_p;
734 	char *val_buf1 = NULL, *val_buf2 = NULL;
735 	int i, err = -ENOMEM;
736 
737 	val_buf1 = malloc(stack_trace_len);
738 	val_buf2 = malloc(stack_trace_len);
739 	if (!val_buf1 || !val_buf2)
740 		goto out;
741 	err = 0;
742 	cur_key_p = NULL;
743 	next_key_p = &key;
744 	while (bpf_map_get_next_key(smap_fd, cur_key_p, next_key_p) == 0) {
745 		err = bpf_map_lookup_elem(smap_fd, next_key_p, val_buf1);
746 		if (err)
747 			goto out;
748 		err = bpf_map_lookup_elem(amap_fd, next_key_p, val_buf2);
749 		if (err)
750 			goto out;
751 		for (i = 0; i < stack_trace_len; i++) {
752 			if (val_buf1[i] != val_buf2[i]) {
753 				err = -1;
754 				goto out;
755 			}
756 		}
757 		key = *next_key_p;
758 		cur_key_p = &key;
759 		next_key_p = &next_key;
760 	}
761 	if (errno != ENOENT)
762 		err = -1;
763 
764 out:
765 	free(val_buf1);
766 	free(val_buf2);
767 	return err;
768 }
769 
770 struct netns_obj {
771 	char *nsname;
772 	struct tmonitor_ctx *tmon;
773 	struct nstoken *nstoken;
774 };
775 
776 /* Create a new network namespace with the given name.
777  *
778  * Create a new network namespace and set the network namespace of the
779  * current process to the new network namespace if the argument "open" is
780  * true. This function should be paired with netns_free() to release the
781  * resource and delete the network namespace.
782  *
783  * It also implements the functionality of the option "-m" by starting
784  * traffic monitor on the background to capture the packets in this network
785  * namespace if the current test or subtest matching the pattern.
786  *
787  * nsname: the name of the network namespace to create.
788  * open: open the network namespace if true.
789  *
790  * Return: the network namespace object on success, NULL on failure.
791  */
792 struct netns_obj *netns_new(const char *nsname, bool open)
793 {
794 	struct netns_obj *netns_obj = malloc(sizeof(*netns_obj));
795 	const char *test_name, *subtest_name;
796 	int r;
797 
798 	if (!netns_obj)
799 		return NULL;
800 	memset(netns_obj, 0, sizeof(*netns_obj));
801 
802 	netns_obj->nsname = strdup(nsname);
803 	if (!netns_obj->nsname)
804 		goto fail;
805 
806 	/* Create the network namespace */
807 	r = make_netns(nsname);
808 	if (r)
809 		goto fail;
810 
811 	/* Start traffic monitor */
812 	if (env.test->should_tmon ||
813 	    (env.subtest_state && env.subtest_state->should_tmon)) {
814 		test_name = env.test->test_name;
815 		subtest_name = env.subtest_state ? env.subtest_state->name : NULL;
816 		netns_obj->tmon = traffic_monitor_start(nsname, test_name, subtest_name);
817 		if (!netns_obj->tmon) {
818 			fprintf(stderr, "Failed to start traffic monitor for %s\n", nsname);
819 			goto fail;
820 		}
821 	} else {
822 		netns_obj->tmon = NULL;
823 	}
824 
825 	if (open) {
826 		netns_obj->nstoken = open_netns(nsname);
827 		if (!netns_obj->nstoken)
828 			goto fail;
829 	}
830 
831 	return netns_obj;
832 fail:
833 	traffic_monitor_stop(netns_obj->tmon);
834 	remove_netns(nsname);
835 	free(netns_obj->nsname);
836 	free(netns_obj);
837 	return NULL;
838 }
839 
840 /* Delete the network namespace.
841  *
842  * This function should be paired with netns_new() to delete the namespace
843  * created by netns_new().
844  */
845 void netns_free(struct netns_obj *netns_obj)
846 {
847 	if (!netns_obj)
848 		return;
849 	traffic_monitor_stop(netns_obj->tmon);
850 	close_netns(netns_obj->nstoken);
851 	remove_netns(netns_obj->nsname);
852 	free(netns_obj->nsname);
853 	free(netns_obj);
854 }
855 
856 /* extern declarations for test funcs */
857 #define DEFINE_TEST(name)				\
858 	extern void test_##name(void) __weak;		\
859 	extern void serial_test_##name(void) __weak;
860 #include <prog_tests/tests.h>
861 #undef DEFINE_TEST
862 
863 static struct prog_test_def prog_test_defs[] = {
864 #define DEFINE_TEST(name) {			\
865 	.test_name = #name,			\
866 	.run_test = &test_##name,		\
867 	.run_serial_test = &serial_test_##name,	\
868 },
869 #include <prog_tests/tests.h>
870 #undef DEFINE_TEST
871 };
872 
873 static const int prog_test_cnt = ARRAY_SIZE(prog_test_defs);
874 
875 static struct test_state test_states[ARRAY_SIZE(prog_test_defs)];
876 
877 const char *argp_program_version = "test_progs 0.1";
878 const char *argp_program_bug_address = "<bpf@vger.kernel.org>";
879 static const char argp_program_doc[] =
880 "BPF selftests test runner\v"
881 "Options accepting the NAMES parameter take either a comma-separated list\n"
882 "of test names, or a filename prefixed with @. The file contains one name\n"
883 "(or wildcard pattern) per line, and comments beginning with # are ignored.\n"
884 "\n"
885 "These options can be passed repeatedly to read multiple files.\n";
886 
887 enum ARG_KEYS {
888 	ARG_TEST_NUM = 'n',
889 	ARG_TEST_NAME = 't',
890 	ARG_TEST_NAME_BLACKLIST = 'b',
891 	ARG_VERIFIER_STATS = 's',
892 	ARG_VERBOSE = 'v',
893 	ARG_GET_TEST_CNT = 'c',
894 	ARG_LIST_TEST_NAMES = 'l',
895 	ARG_TEST_NAME_GLOB_ALLOWLIST = 'a',
896 	ARG_TEST_NAME_GLOB_DENYLIST = 'd',
897 	ARG_NUM_WORKERS = 'j',
898 	ARG_DEBUG = -1,
899 	ARG_JSON_SUMMARY = 'J',
900 	ARG_TRAFFIC_MONITOR = 'm',
901 	ARG_WATCHDOG_TIMEOUT = 'w',
902 };
903 
904 static const struct argp_option opts[] = {
905 	{ "num", ARG_TEST_NUM, "NUM", 0,
906 	  "Run test number NUM only " },
907 	{ "name", ARG_TEST_NAME, "NAMES", 0,
908 	  "Run tests with names containing any string from NAMES list" },
909 	{ "name-blacklist", ARG_TEST_NAME_BLACKLIST, "NAMES", 0,
910 	  "Don't run tests with names containing any string from NAMES list" },
911 	{ "verifier-stats", ARG_VERIFIER_STATS, NULL, 0,
912 	  "Output verifier statistics", },
913 	{ "verbose", ARG_VERBOSE, "LEVEL", OPTION_ARG_OPTIONAL,
914 	  "Verbose output (use -vv or -vvv for progressively verbose output)" },
915 	{ "count", ARG_GET_TEST_CNT, NULL, 0,
916 	  "Get number of selected top-level tests " },
917 	{ "list", ARG_LIST_TEST_NAMES, NULL, 0,
918 	  "List test names that would run (without running them) " },
919 	{ "allow", ARG_TEST_NAME_GLOB_ALLOWLIST, "NAMES", 0,
920 	  "Run tests with name matching the pattern (supports '*' wildcard)." },
921 	{ "deny", ARG_TEST_NAME_GLOB_DENYLIST, "NAMES", 0,
922 	  "Don't run tests with name matching the pattern (supports '*' wildcard)." },
923 	{ "workers", ARG_NUM_WORKERS, "WORKERS", OPTION_ARG_OPTIONAL,
924 	  "Number of workers to run in parallel, default to number of cpus." },
925 	{ "debug", ARG_DEBUG, NULL, 0,
926 	  "print extra debug information for test_progs." },
927 	{ "json-summary", ARG_JSON_SUMMARY, "FILE", 0, "Write report in json format to this file."},
928 #ifdef TRAFFIC_MONITOR
929 	{ "traffic-monitor", ARG_TRAFFIC_MONITOR, "NAMES", 0,
930 	  "Monitor network traffic of tests with name matching the pattern (supports '*' wildcard)." },
931 #endif
932 	{ "watchdog-timeout", ARG_WATCHDOG_TIMEOUT, "SECONDS", 0,
933 	  "Kill the process if tests are not making progress for specified number of seconds." },
934 	{},
935 };
936 
937 static FILE *libbpf_capture_stream;
938 
939 static struct {
940 	char *buf;
941 	size_t buf_sz;
942 } libbpf_output_capture;
943 
944 /* Creates a global memstream capturing INFO and WARN level output
945  * passed to libbpf_print_fn.
946  * Returns 0 on success, negative value on failure.
947  * On failure the description is printed using PRINT_FAIL and
948  * current test case is marked as fail.
949  */
950 int start_libbpf_log_capture(void)
951 {
952 	if (libbpf_capture_stream) {
953 		PRINT_FAIL("%s: libbpf_capture_stream != NULL\n", __func__);
954 		return -EINVAL;
955 	}
956 
957 	libbpf_capture_stream = open_memstream(&libbpf_output_capture.buf,
958 					       &libbpf_output_capture.buf_sz);
959 	if (!libbpf_capture_stream) {
960 		PRINT_FAIL("%s: open_memstream failed errno=%d\n", __func__, errno);
961 		return -EINVAL;
962 	}
963 
964 	return 0;
965 }
966 
967 /* Destroys global memstream created by start_libbpf_log_capture().
968  * Returns a pointer to captured data which has to be freed.
969  * Returned buffer is null terminated.
970  */
971 char *stop_libbpf_log_capture(void)
972 {
973 	char *buf;
974 
975 	if (!libbpf_capture_stream)
976 		return NULL;
977 
978 	fputc(0, libbpf_capture_stream);
979 	fclose(libbpf_capture_stream);
980 	libbpf_capture_stream = NULL;
981 	/* get 'buf' after fclose(), see open_memstream() documentation */
982 	buf = libbpf_output_capture.buf;
983 	memset(&libbpf_output_capture, 0, sizeof(libbpf_output_capture));
984 	return buf;
985 }
986 
987 static int libbpf_print_fn(enum libbpf_print_level level,
988 			   const char *format, va_list args)
989 {
990 	if (libbpf_capture_stream && level != LIBBPF_DEBUG) {
991 		va_list args2;
992 
993 		va_copy(args2, args);
994 		vfprintf(libbpf_capture_stream, format, args2);
995 		va_end(args2);
996 	}
997 
998 	if (env.verbosity < VERBOSE_VERY && level == LIBBPF_DEBUG)
999 		return 0;
1000 
1001 	vfprintf(stdout, format, args);
1002 	return 0;
1003 }
1004 
1005 static void free_test_filter_set(const struct test_filter_set *set)
1006 {
1007 	int i, j;
1008 
1009 	if (!set)
1010 		return;
1011 
1012 	for (i = 0; i < set->cnt; i++) {
1013 		free((void *)set->tests[i].name);
1014 		for (j = 0; j < set->tests[i].subtest_cnt; j++)
1015 			free((void *)set->tests[i].subtests[j]);
1016 
1017 		free((void *)set->tests[i].subtests);
1018 	}
1019 
1020 	free((void *)set->tests);
1021 }
1022 
1023 static void free_test_selector(struct test_selector *test_selector)
1024 {
1025 	free_test_filter_set(&test_selector->blacklist);
1026 	free_test_filter_set(&test_selector->whitelist);
1027 	free(test_selector->num_set);
1028 }
1029 
1030 extern int extra_prog_load_log_flags;
1031 
1032 static error_t parse_arg(int key, char *arg, struct argp_state *state)
1033 {
1034 	struct test_env *env = state->input;
1035 	int err = 0;
1036 
1037 	switch (key) {
1038 	case ARG_TEST_NUM: {
1039 		char *subtest_str = strchr(arg, '/');
1040 
1041 		if (subtest_str) {
1042 			*subtest_str = '\0';
1043 			if (parse_num_list(subtest_str + 1,
1044 					   &env->subtest_selector.num_set,
1045 					   &env->subtest_selector.num_set_len)) {
1046 				fprintf(stderr,
1047 					"Failed to parse subtest numbers.\n");
1048 				return -EINVAL;
1049 			}
1050 		}
1051 		if (parse_num_list(arg, &env->test_selector.num_set,
1052 				   &env->test_selector.num_set_len)) {
1053 			fprintf(stderr, "Failed to parse test numbers.\n");
1054 			return -EINVAL;
1055 		}
1056 		break;
1057 	}
1058 	case ARG_TEST_NAME_GLOB_ALLOWLIST:
1059 	case ARG_TEST_NAME: {
1060 		if (arg[0] == '@')
1061 			err = parse_test_list_file(arg + 1,
1062 						   &env->test_selector.whitelist,
1063 						   key == ARG_TEST_NAME_GLOB_ALLOWLIST);
1064 		else
1065 			err = parse_test_list(arg,
1066 					      &env->test_selector.whitelist,
1067 					      key == ARG_TEST_NAME_GLOB_ALLOWLIST);
1068 
1069 		break;
1070 	}
1071 	case ARG_TEST_NAME_GLOB_DENYLIST:
1072 	case ARG_TEST_NAME_BLACKLIST: {
1073 		if (arg[0] == '@')
1074 			err = parse_test_list_file(arg + 1,
1075 						   &env->test_selector.blacklist,
1076 						   key == ARG_TEST_NAME_GLOB_DENYLIST);
1077 		else
1078 			err = parse_test_list(arg,
1079 					      &env->test_selector.blacklist,
1080 					      key == ARG_TEST_NAME_GLOB_DENYLIST);
1081 
1082 		break;
1083 	}
1084 	case ARG_VERIFIER_STATS:
1085 		env->verifier_stats = true;
1086 		break;
1087 	case ARG_VERBOSE:
1088 		env->verbosity = VERBOSE_NORMAL;
1089 		if (arg) {
1090 			if (strcmp(arg, "v") == 0) {
1091 				env->verbosity = VERBOSE_VERY;
1092 				extra_prog_load_log_flags = 1;
1093 			} else if (strcmp(arg, "vv") == 0) {
1094 				env->verbosity = VERBOSE_SUPER;
1095 				extra_prog_load_log_flags = 2;
1096 			} else {
1097 				fprintf(stderr,
1098 					"Unrecognized verbosity setting ('%s'), only -v and -vv are supported\n",
1099 					arg);
1100 				return -EINVAL;
1101 			}
1102 		}
1103 		env_verbosity = env->verbosity;
1104 
1105 		if (verbose()) {
1106 			if (setenv("SELFTESTS_VERBOSE", "1", 1) == -1) {
1107 				fprintf(stderr,
1108 					"Unable to setenv SELFTESTS_VERBOSE=1 (errno=%d)",
1109 					errno);
1110 				return -EINVAL;
1111 			}
1112 		}
1113 
1114 		break;
1115 	case ARG_GET_TEST_CNT:
1116 		env->get_test_cnt = true;
1117 		break;
1118 	case ARG_LIST_TEST_NAMES:
1119 		env->list_test_names = true;
1120 		break;
1121 	case ARG_NUM_WORKERS:
1122 		if (arg) {
1123 			env->workers = atoi(arg);
1124 			if (!env->workers) {
1125 				fprintf(stderr, "Invalid number of worker: %s.", arg);
1126 				return -EINVAL;
1127 			}
1128 		} else {
1129 			env->workers = get_nprocs();
1130 		}
1131 		break;
1132 	case ARG_DEBUG:
1133 		env->debug = true;
1134 		break;
1135 	case ARG_JSON_SUMMARY:
1136 		env->json = fopen(arg, "w");
1137 		if (env->json == NULL) {
1138 			perror("Failed to open json summary file");
1139 			return -errno;
1140 		}
1141 		break;
1142 	case ARGP_KEY_ARG:
1143 		argp_usage(state);
1144 		break;
1145 	case ARGP_KEY_END:
1146 		break;
1147 #ifdef TRAFFIC_MONITOR
1148 	case ARG_TRAFFIC_MONITOR:
1149 		if (arg[0] == '@')
1150 			err = parse_test_list_file(arg + 1,
1151 						   &env->tmon_selector.whitelist,
1152 						   true);
1153 		else
1154 			err = parse_test_list(arg,
1155 					      &env->tmon_selector.whitelist,
1156 					      true);
1157 		break;
1158 #endif
1159 	case ARG_WATCHDOG_TIMEOUT:
1160 		env->secs_till_kill = atoi(arg);
1161 		if (env->secs_till_kill < 0) {
1162 			fprintf(stderr, "Invalid watchdog timeout: %s.\n", arg);
1163 			return -EINVAL;
1164 		}
1165 		if (env->secs_till_kill < env->secs_till_notify) {
1166 			env->secs_till_notify = 0;
1167 		}
1168 		break;
1169 	default:
1170 		return ARGP_ERR_UNKNOWN;
1171 	}
1172 	return err;
1173 }
1174 
1175 /*
1176  * Determine if test_progs is running as a "flavored" test runner and switch
1177  * into corresponding sub-directory to load correct BPF objects.
1178  *
1179  * This is done by looking at executable name. If it contains "-flavor"
1180  * suffix, then we are running as a flavored test runner.
1181  */
1182 int cd_flavor_subdir(const char *exec_name)
1183 {
1184 	/* General form of argv[0] passed here is:
1185 	 * some/path/to/test_progs[-flavor], where -flavor part is optional.
1186 	 * First cut out "test_progs[-flavor]" part, then extract "flavor"
1187 	 * part, if it's there.
1188 	 */
1189 	const char *flavor = strrchr(exec_name, '/');
1190 
1191 	if (!flavor)
1192 		flavor = exec_name;
1193 	else
1194 		flavor++;
1195 
1196 	flavor = strrchr(flavor, '-');
1197 	if (!flavor)
1198 		return 0;
1199 	flavor++;
1200 	if (verbose())
1201 		fprintf(stdout,	"Switching to flavor '%s' subdirectory...\n", flavor);
1202 
1203 	return chdir(flavor);
1204 }
1205 
1206 int trigger_module_test_read(int read_sz)
1207 {
1208 	int fd, err;
1209 
1210 	fd = open(BPF_TESTMOD_TEST_FILE, O_RDONLY);
1211 	err = -errno;
1212 	if (!ASSERT_GE(fd, 0, "testmod_file_open"))
1213 		return err;
1214 
1215 	read(fd, NULL, read_sz);
1216 	close(fd);
1217 
1218 	return 0;
1219 }
1220 
1221 int trigger_module_test_write(int write_sz)
1222 {
1223 	int fd, err;
1224 	char *buf = malloc(write_sz);
1225 
1226 	if (!buf)
1227 		return -ENOMEM;
1228 
1229 	memset(buf, 'a', write_sz);
1230 	buf[write_sz-1] = '\0';
1231 
1232 	fd = open(BPF_TESTMOD_TEST_FILE, O_WRONLY);
1233 	err = -errno;
1234 	if (!ASSERT_GE(fd, 0, "testmod_file_open")) {
1235 		free(buf);
1236 		return err;
1237 	}
1238 
1239 	write(fd, buf, write_sz);
1240 	close(fd);
1241 	free(buf);
1242 	return 0;
1243 }
1244 
1245 int write_sysctl(const char *sysctl, const char *value)
1246 {
1247 	int fd, err, len;
1248 
1249 	fd = open(sysctl, O_WRONLY);
1250 	if (!ASSERT_NEQ(fd, -1, "open sysctl"))
1251 		return -1;
1252 
1253 	len = strlen(value);
1254 	err = write(fd, value, len);
1255 	close(fd);
1256 	if (!ASSERT_EQ(err, len, "write sysctl"))
1257 		return -1;
1258 
1259 	return 0;
1260 }
1261 
1262 int get_bpf_max_tramp_links_from(struct btf *btf)
1263 {
1264 	const struct btf_enum *e;
1265 	const struct btf_type *t;
1266 	__u32 i, type_cnt;
1267 	const char *name;
1268 	__u32 j, vlen;
1269 
1270 	for (i = 1, type_cnt = btf__type_cnt(btf); i < type_cnt; i++) {
1271 		t = btf__type_by_id(btf, i);
1272 		if (!t || !btf_is_enum(t) || t->name_off)
1273 			continue;
1274 		e = btf_enum(t);
1275 		for (j = 0, vlen = btf_vlen(t); j < vlen; j++, e++) {
1276 			name = btf__str_by_offset(btf, e->name_off);
1277 			if (name && !strcmp(name, "BPF_MAX_TRAMP_LINKS"))
1278 				return e->val;
1279 		}
1280 	}
1281 
1282 	return -1;
1283 }
1284 
1285 int get_bpf_max_tramp_links(void)
1286 {
1287 	struct btf *vmlinux_btf;
1288 	int ret;
1289 
1290 	vmlinux_btf = btf__load_vmlinux_btf();
1291 	if (!ASSERT_OK_PTR(vmlinux_btf, "vmlinux btf"))
1292 		return -1;
1293 	ret = get_bpf_max_tramp_links_from(vmlinux_btf);
1294 	btf__free(vmlinux_btf);
1295 
1296 	return ret;
1297 }
1298 
1299 static void dump_crash_log(void)
1300 {
1301 	fflush(stdout);
1302 	stdout = env.stdout_saved;
1303 	stderr = env.stderr_saved;
1304 
1305 	if (env.test) {
1306 		env.test_state->error_cnt++;
1307 		dump_test_log(env.test, env.test_state, true, false, NULL);
1308 	}
1309 }
1310 
1311 #define MAX_BACKTRACE_SZ 128
1312 
1313 void crash_handler(int signum)
1314 {
1315 	void *bt[MAX_BACKTRACE_SZ];
1316 	size_t sz;
1317 
1318 	sz = backtrace(bt, ARRAY_SIZE(bt));
1319 
1320 	dump_crash_log();
1321 
1322 	if (env.worker_id != -1)
1323 		fprintf(stderr, "[%d]: ", env.worker_id);
1324 	fprintf(stderr, "Caught signal #%d!\nStack trace:\n", signum);
1325 	backtrace_symbols_fd(bt, sz, STDERR_FILENO);
1326 }
1327 
1328 #ifdef __SANITIZE_ADDRESS__
1329 void __asan_on_error(void)
1330 {
1331 	dump_crash_log();
1332 }
1333 #endif
1334 
1335 void hexdump(const char *prefix, const void *buf, size_t len)
1336 {
1337 	for (int i = 0; i < len; i++) {
1338 		if (!(i % 16)) {
1339 			if (i)
1340 				fprintf(stdout, "\n");
1341 			fprintf(stdout, "%s", prefix);
1342 		}
1343 		if (i && !(i % 8) && (i % 16))
1344 			fprintf(stdout, "\t");
1345 		fprintf(stdout, "%02X ", ((uint8_t *)(buf))[i]);
1346 	}
1347 	fprintf(stdout, "\n");
1348 }
1349 
1350 static void sigint_handler(int signum)
1351 {
1352 	int i;
1353 
1354 	for (i = 0; i < env.workers; i++)
1355 		if (env.worker_socks[i] > 0)
1356 			close(env.worker_socks[i]);
1357 }
1358 
1359 static int current_test_idx;
1360 static pthread_mutex_t current_test_lock;
1361 static pthread_mutex_t stdout_output_lock;
1362 
1363 static inline const char *str_msg(const struct msg *msg, char *buf)
1364 {
1365 	switch (msg->type) {
1366 	case MSG_DO_TEST:
1367 		sprintf(buf, "MSG_DO_TEST %d", msg->do_test.num);
1368 		break;
1369 	case MSG_TEST_DONE:
1370 		sprintf(buf, "MSG_TEST_DONE %d (log: %d)",
1371 			msg->test_done.num,
1372 			msg->test_done.have_log);
1373 		break;
1374 	case MSG_SUBTEST_DONE:
1375 		sprintf(buf, "MSG_SUBTEST_DONE %d (log: %d)",
1376 			msg->subtest_done.num,
1377 			msg->subtest_done.have_log);
1378 		break;
1379 	case MSG_TEST_LOG:
1380 		sprintf(buf, "MSG_TEST_LOG (cnt: %zu, last: %d)",
1381 			strlen(msg->test_log.log_buf),
1382 			msg->test_log.is_last);
1383 		break;
1384 	case MSG_EXIT:
1385 		sprintf(buf, "MSG_EXIT");
1386 		break;
1387 	default:
1388 		sprintf(buf, "UNKNOWN");
1389 		break;
1390 	}
1391 
1392 	return buf;
1393 }
1394 
1395 static int send_message(int sock, const struct msg *msg)
1396 {
1397 	char buf[256];
1398 
1399 	if (env.debug)
1400 		fprintf(stderr, "Sending msg: %s\n", str_msg(msg, buf));
1401 	return send(sock, msg, sizeof(*msg), 0);
1402 }
1403 
1404 static int recv_message(int sock, struct msg *msg)
1405 {
1406 	int ret;
1407 	char buf[256];
1408 
1409 	memset(msg, 0, sizeof(*msg));
1410 	ret = recv(sock, msg, sizeof(*msg), 0);
1411 	if (ret >= 0) {
1412 		if (env.debug)
1413 			fprintf(stderr, "Received msg: %s\n", str_msg(msg, buf));
1414 	}
1415 	return ret;
1416 }
1417 
1418 static bool ns_is_needed(const char *test_name)
1419 {
1420 	if (strlen(test_name) < 3)
1421 		return false;
1422 
1423 	return !strncmp(test_name, "ns_", 3);
1424 }
1425 
1426 static void run_one_test(int test_num)
1427 {
1428 	struct prog_test_def *test = &prog_test_defs[test_num];
1429 	struct test_state *state = &test_states[test_num];
1430 	struct netns_obj *ns = NULL;
1431 
1432 	env.test = test;
1433 	env.test_state = state;
1434 
1435 	stdio_hijack(&state->log_buf, &state->log_cnt);
1436 
1437 	watchdog_start();
1438 	if (ns_is_needed(test->test_name))
1439 		ns = netns_new(test->test_name, true);
1440 	if (test->run_test)
1441 		test->run_test();
1442 	else if (test->run_serial_test)
1443 		test->run_serial_test();
1444 	netns_free(ns);
1445 	watchdog_stop();
1446 
1447 	/* ensure last sub-test is finalized properly */
1448 	if (env.subtest_state)
1449 		test__end_subtest();
1450 
1451 	state->tested = true;
1452 
1453 	stdio_restore();
1454 
1455 	if (verbose() && env.worker_id == -1)
1456 		print_test_result(test, state);
1457 
1458 	reset_affinity();
1459 	restore_netns();
1460 	if (test->need_cgroup_cleanup)
1461 		cleanup_cgroup_environment();
1462 
1463 	free(stop_libbpf_log_capture());
1464 
1465 	dump_test_log(test, state, false, false, NULL);
1466 }
1467 
1468 struct dispatch_data {
1469 	int worker_id;
1470 	int sock_fd;
1471 };
1472 
1473 static int read_prog_test_msg(int sock_fd, struct msg *msg, enum msg_type type)
1474 {
1475 	if (recv_message(sock_fd, msg) < 0)
1476 		return 1;
1477 
1478 	if (msg->type != type) {
1479 		printf("%s: unexpected message type %d. expected %d\n", __func__, msg->type, type);
1480 		return 1;
1481 	}
1482 
1483 	return 0;
1484 }
1485 
1486 static int dispatch_thread_read_log(int sock_fd, char **log_buf, size_t *log_cnt)
1487 {
1488 	FILE *log_fp = NULL;
1489 	int result = 0;
1490 
1491 	log_fp = open_memstream(log_buf, log_cnt);
1492 	if (!log_fp)
1493 		return 1;
1494 
1495 	while (true) {
1496 		struct msg msg;
1497 
1498 		if (read_prog_test_msg(sock_fd, &msg, MSG_TEST_LOG)) {
1499 			result = 1;
1500 			goto out;
1501 		}
1502 
1503 		fprintf(log_fp, "%s", msg.test_log.log_buf);
1504 		if (msg.test_log.is_last)
1505 			break;
1506 	}
1507 
1508 out:
1509 	fclose(log_fp);
1510 	log_fp = NULL;
1511 	return result;
1512 }
1513 
1514 static int dispatch_thread_send_subtests(int sock_fd, struct test_state *state)
1515 {
1516 	struct msg msg;
1517 	struct subtest_state *subtest_state;
1518 	int subtest_num = state->subtest_num;
1519 
1520 	state->subtest_states = calloc(subtest_num, sizeof(*subtest_state));
1521 	if (!state->subtest_states) {
1522 		state->subtest_num = 0;
1523 		return -ENOMEM;
1524 	}
1525 
1526 	for (int i = 0; i < subtest_num; i++) {
1527 		subtest_state = &state->subtest_states[i];
1528 
1529 		if (read_prog_test_msg(sock_fd, &msg, MSG_SUBTEST_DONE))
1530 			return 1;
1531 
1532 		subtest_state->name = strdup(msg.subtest_done.name);
1533 		subtest_state->error_cnt = msg.subtest_done.error_cnt;
1534 		subtest_state->skipped = msg.subtest_done.skipped;
1535 		subtest_state->filtered = msg.subtest_done.filtered;
1536 
1537 		/* collect all logs */
1538 		if (msg.subtest_done.have_log)
1539 			if (dispatch_thread_read_log(sock_fd,
1540 						     &subtest_state->log_buf,
1541 						     &subtest_state->log_cnt))
1542 				return 1;
1543 	}
1544 
1545 	return 0;
1546 }
1547 
1548 static void *dispatch_thread(void *ctx)
1549 {
1550 	struct dispatch_data *data = ctx;
1551 	int sock_fd;
1552 
1553 	sock_fd = data->sock_fd;
1554 
1555 	while (true) {
1556 		int test_to_run = -1;
1557 		struct prog_test_def *test;
1558 		struct test_state *state;
1559 
1560 		/* grab a test */
1561 		{
1562 			pthread_mutex_lock(&current_test_lock);
1563 
1564 			if (current_test_idx >= prog_test_cnt) {
1565 				pthread_mutex_unlock(&current_test_lock);
1566 				goto done;
1567 			}
1568 
1569 			test = &prog_test_defs[current_test_idx];
1570 			test_to_run = current_test_idx;
1571 			current_test_idx++;
1572 
1573 			pthread_mutex_unlock(&current_test_lock);
1574 		}
1575 
1576 		if (!test->should_run || test->run_serial_test)
1577 			continue;
1578 
1579 		/* run test through worker */
1580 		{
1581 			struct msg msg_do_test;
1582 
1583 			memset(&msg_do_test, 0, sizeof(msg_do_test));
1584 			msg_do_test.type = MSG_DO_TEST;
1585 			msg_do_test.do_test.num = test_to_run;
1586 			if (send_message(sock_fd, &msg_do_test) < 0) {
1587 				perror("Fail to send command");
1588 				goto done;
1589 			}
1590 			env.worker_current_test[data->worker_id] = test_to_run;
1591 		}
1592 
1593 		/* wait for test done */
1594 		do {
1595 			struct msg msg;
1596 
1597 			if (read_prog_test_msg(sock_fd, &msg, MSG_TEST_DONE))
1598 				goto error;
1599 			if (test_to_run != msg.test_done.num)
1600 				goto error;
1601 
1602 			state = &test_states[test_to_run];
1603 			state->tested = true;
1604 			state->error_cnt = msg.test_done.error_cnt;
1605 			state->skip_cnt = msg.test_done.skip_cnt;
1606 			state->sub_succ_cnt = msg.test_done.sub_succ_cnt;
1607 			state->subtest_num = msg.test_done.subtest_num;
1608 
1609 			/* collect all logs */
1610 			if (msg.test_done.have_log) {
1611 				if (dispatch_thread_read_log(sock_fd,
1612 							     &state->log_buf,
1613 							     &state->log_cnt))
1614 					goto error;
1615 			}
1616 
1617 			/* collect all subtests and subtest logs */
1618 			if (!state->subtest_num)
1619 				break;
1620 
1621 			if (dispatch_thread_send_subtests(sock_fd, state))
1622 				goto error;
1623 		} while (false);
1624 
1625 		pthread_mutex_lock(&stdout_output_lock);
1626 		dump_test_log(test, state, false, true, NULL);
1627 		pthread_mutex_unlock(&stdout_output_lock);
1628 	} /* while (true) */
1629 error:
1630 	if (env.debug)
1631 		fprintf(stderr, "[%d]: Protocol/IO error: %s.\n", data->worker_id, strerror(errno));
1632 
1633 done:
1634 	{
1635 		struct msg msg_exit;
1636 
1637 		msg_exit.type = MSG_EXIT;
1638 		if (send_message(sock_fd, &msg_exit) < 0) {
1639 			if (env.debug)
1640 				fprintf(stderr, "[%d]: send_message msg_exit: %s.\n",
1641 					data->worker_id, strerror(errno));
1642 		}
1643 	}
1644 	return NULL;
1645 }
1646 
1647 static void calculate_summary_and_print_errors(struct test_env *env)
1648 {
1649 	int i;
1650 	int succ_cnt = 0, fail_cnt = 0, sub_succ_cnt = 0, skip_cnt = 0;
1651 	json_writer_t *w = NULL;
1652 
1653 	for (i = 0; i < prog_test_cnt; i++) {
1654 		struct prog_test_def *test = &prog_test_defs[i];
1655 		struct test_state *state = &test_states[i];
1656 
1657 		if (!state->tested)
1658 			continue;
1659 
1660 		sub_succ_cnt += state->sub_succ_cnt;
1661 		skip_cnt += state->skip_cnt;
1662 
1663 		if (state->error_cnt)
1664 			fail_cnt++;
1665 		else if (!test->not_built)
1666 			succ_cnt++;
1667 	}
1668 
1669 	if (env->json) {
1670 		w = jsonw_new(env->json);
1671 		if (!w)
1672 			fprintf(env->stderr_saved, "Failed to create new JSON stream.");
1673 	}
1674 
1675 	if (w) {
1676 		jsonw_start_object(w);
1677 		jsonw_uint_field(w, "success", succ_cnt);
1678 		jsonw_uint_field(w, "success_subtest", sub_succ_cnt);
1679 		jsonw_uint_field(w, "skipped", skip_cnt);
1680 		jsonw_uint_field(w, "failed", fail_cnt);
1681 		jsonw_name(w, "results");
1682 		jsonw_start_array(w);
1683 	}
1684 
1685 	/*
1686 	 * We only print error logs summary when there are failed tests and
1687 	 * verbose mode is not enabled. Otherwise, results may be inconsistent.
1688 	 *
1689 	 */
1690 	if (!verbose() && fail_cnt) {
1691 		printf("\nAll error logs:\n");
1692 
1693 		/* print error logs again */
1694 		for (i = 0; i < prog_test_cnt; i++) {
1695 			struct prog_test_def *test = &prog_test_defs[i];
1696 			struct test_state *state = &test_states[i];
1697 
1698 			if (!state->tested || !state->error_cnt)
1699 				continue;
1700 
1701 			dump_test_log(test, state, true, true, w);
1702 		}
1703 	}
1704 
1705 	if (w) {
1706 		jsonw_end_array(w);
1707 		jsonw_end_object(w);
1708 		jsonw_destroy(&w);
1709 	}
1710 
1711 	if (env->json)
1712 		fclose(env->json);
1713 
1714 	if (env->not_built_cnt)
1715 		printf("Summary: %d/%d PASSED, %d SKIPPED (%d not built), %d FAILED\n",
1716 		       succ_cnt, sub_succ_cnt, skip_cnt, env->not_built_cnt,
1717 		       fail_cnt);
1718 	else
1719 		printf("Summary: %d/%d PASSED, %d SKIPPED, %d FAILED\n",
1720 		       succ_cnt, sub_succ_cnt, skip_cnt, fail_cnt);
1721 
1722 	env->succ_cnt = succ_cnt;
1723 	env->sub_succ_cnt = sub_succ_cnt;
1724 	env->fail_cnt = fail_cnt;
1725 	env->skip_cnt = skip_cnt;
1726 }
1727 
1728 static void server_main(void)
1729 {
1730 	pthread_t *dispatcher_threads;
1731 	struct dispatch_data *data;
1732 	struct sigaction sigact_int = {
1733 		.sa_handler = sigint_handler,
1734 		.sa_flags = SA_RESETHAND,
1735 	};
1736 	int i;
1737 
1738 	sigaction(SIGINT, &sigact_int, NULL);
1739 
1740 	dispatcher_threads = calloc(sizeof(pthread_t), env.workers);
1741 	data = calloc(sizeof(struct dispatch_data), env.workers);
1742 
1743 	env.worker_current_test = calloc(sizeof(int), env.workers);
1744 	for (i = 0; i < env.workers; i++) {
1745 		int rc;
1746 
1747 		data[i].worker_id = i;
1748 		data[i].sock_fd = env.worker_socks[i];
1749 		rc = pthread_create(&dispatcher_threads[i], NULL, dispatch_thread, &data[i]);
1750 		if (rc) {
1751 			perror("Failed to launch dispatcher thread");
1752 			exit(EXIT_ERR_SETUP_INFRA);
1753 		}
1754 	}
1755 
1756 	/* wait for all dispatcher to finish */
1757 	for (i = 0; i < env.workers; i++) {
1758 		while (true) {
1759 			int ret = pthread_tryjoin_np(dispatcher_threads[i], NULL);
1760 
1761 			if (!ret) {
1762 				break;
1763 			} else if (ret == EBUSY) {
1764 				if (env.debug)
1765 					fprintf(stderr, "Still waiting for thread %d (test %d).\n",
1766 						i,  env.worker_current_test[i] + 1);
1767 				usleep(1000 * 1000);
1768 				continue;
1769 			} else {
1770 				fprintf(stderr, "Unexpected error joining dispatcher thread: %d", ret);
1771 				break;
1772 			}
1773 		}
1774 	}
1775 	free(dispatcher_threads);
1776 	free(env.worker_current_test);
1777 	free(data);
1778 
1779 	/* run serial tests */
1780 	save_netns();
1781 
1782 	for (int i = 0; i < prog_test_cnt; i++) {
1783 		struct prog_test_def *test = &prog_test_defs[i];
1784 
1785 		if (!test->should_run || !test->run_serial_test)
1786 			continue;
1787 
1788 		run_one_test(i);
1789 	}
1790 
1791 	/* mark not-built tests as skipped */
1792 	for (int i = 0; i < prog_test_cnt; i++) {
1793 		struct prog_test_def *test = &prog_test_defs[i];
1794 		struct test_state *state = &test_states[i];
1795 
1796 		if (test->not_built && test->selected) {
1797 			state->tested = true;
1798 			state->skip_cnt = 1;
1799 			env.not_built_cnt++;
1800 			print_test_result(test, state);
1801 		}
1802 	}
1803 
1804 	/* generate summary */
1805 	fflush(stderr);
1806 	fflush(stdout);
1807 
1808 	calculate_summary_and_print_errors(&env);
1809 
1810 	/* reap all workers */
1811 	for (i = 0; i < env.workers; i++) {
1812 		int wstatus, pid;
1813 
1814 		pid = waitpid(env.worker_pids[i], &wstatus, 0);
1815 		if (pid != env.worker_pids[i])
1816 			perror("Unable to reap worker");
1817 	}
1818 }
1819 
1820 static void worker_main_send_log(int sock, char *log_buf, size_t log_cnt)
1821 {
1822 	char *src;
1823 	size_t slen;
1824 
1825 	src = log_buf;
1826 	slen = log_cnt;
1827 	while (slen) {
1828 		struct msg msg_log;
1829 		char *dest;
1830 		size_t len;
1831 
1832 		memset(&msg_log, 0, sizeof(msg_log));
1833 		msg_log.type = MSG_TEST_LOG;
1834 		dest = msg_log.test_log.log_buf;
1835 		len = slen >= MAX_LOG_TRUNK_SIZE ? MAX_LOG_TRUNK_SIZE : slen;
1836 		memcpy(dest, src, len);
1837 
1838 		src += len;
1839 		slen -= len;
1840 		if (!slen)
1841 			msg_log.test_log.is_last = true;
1842 
1843 		assert(send_message(sock, &msg_log) >= 0);
1844 	}
1845 }
1846 
1847 static void free_subtest_state(struct subtest_state *state)
1848 {
1849 	if (state->log_buf) {
1850 		free(state->log_buf);
1851 		state->log_buf = NULL;
1852 		state->log_cnt = 0;
1853 	}
1854 	free(state->name);
1855 	state->name = NULL;
1856 }
1857 
1858 static int worker_main_send_subtests(int sock, struct test_state *state)
1859 {
1860 	int i, result = 0;
1861 	struct msg msg;
1862 	struct subtest_state *subtest_state;
1863 
1864 	memset(&msg, 0, sizeof(msg));
1865 	msg.type = MSG_SUBTEST_DONE;
1866 
1867 	for (i = 0; i < state->subtest_num; i++) {
1868 		subtest_state = &state->subtest_states[i];
1869 
1870 		msg.subtest_done.num = i;
1871 
1872 		strscpy(msg.subtest_done.name, subtest_state->name, MAX_SUBTEST_NAME);
1873 
1874 		msg.subtest_done.error_cnt = subtest_state->error_cnt;
1875 		msg.subtest_done.skipped = subtest_state->skipped;
1876 		msg.subtest_done.filtered = subtest_state->filtered;
1877 		msg.subtest_done.have_log = false;
1878 
1879 		if (verbose() || state->force_log || subtest_state->error_cnt) {
1880 			if (subtest_state->log_cnt)
1881 				msg.subtest_done.have_log = true;
1882 		}
1883 
1884 		if (send_message(sock, &msg) < 0) {
1885 			perror("Fail to send message done");
1886 			result = 1;
1887 			goto out;
1888 		}
1889 
1890 		/* send logs */
1891 		if (msg.subtest_done.have_log)
1892 			worker_main_send_log(sock, subtest_state->log_buf, subtest_state->log_cnt);
1893 
1894 		free_subtest_state(subtest_state);
1895 	}
1896 
1897 out:
1898 	for (; i < state->subtest_num; i++)
1899 		free_subtest_state(&state->subtest_states[i]);
1900 	free(state->subtest_states);
1901 	return result;
1902 }
1903 
1904 static int worker_main(int sock)
1905 {
1906 	save_netns();
1907 	watchdog_init();
1908 
1909 	while (true) {
1910 		/* receive command */
1911 		struct msg msg;
1912 
1913 		if (recv_message(sock, &msg) < 0)
1914 			goto out;
1915 
1916 		switch (msg.type) {
1917 		case MSG_EXIT:
1918 			if (env.debug)
1919 				fprintf(stderr, "[%d]: worker exit.\n",
1920 					env.worker_id);
1921 			goto out;
1922 		case MSG_DO_TEST: {
1923 			int test_to_run = msg.do_test.num;
1924 			struct prog_test_def *test = &prog_test_defs[test_to_run];
1925 			struct test_state *state = &test_states[test_to_run];
1926 			struct msg msg;
1927 
1928 			if (env.debug)
1929 				fprintf(stderr, "[%d]: #%d:%s running.\n",
1930 					env.worker_id,
1931 					test_to_run + 1,
1932 					test->test_name);
1933 
1934 			run_one_test(test_to_run);
1935 
1936 			memset(&msg, 0, sizeof(msg));
1937 			msg.type = MSG_TEST_DONE;
1938 			msg.test_done.num = test_to_run;
1939 			msg.test_done.error_cnt = state->error_cnt;
1940 			msg.test_done.skip_cnt = state->skip_cnt;
1941 			msg.test_done.sub_succ_cnt = state->sub_succ_cnt;
1942 			msg.test_done.subtest_num = state->subtest_num;
1943 			msg.test_done.have_log = false;
1944 
1945 			if (verbose() || state->force_log || state->error_cnt) {
1946 				if (state->log_cnt)
1947 					msg.test_done.have_log = true;
1948 			}
1949 			if (send_message(sock, &msg) < 0) {
1950 				perror("Fail to send message done");
1951 				goto out;
1952 			}
1953 
1954 			/* send logs */
1955 			if (msg.test_done.have_log)
1956 				worker_main_send_log(sock, state->log_buf, state->log_cnt);
1957 
1958 			if (state->log_buf) {
1959 				free(state->log_buf);
1960 				state->log_buf = NULL;
1961 				state->log_cnt = 0;
1962 			}
1963 
1964 			if (state->subtest_num)
1965 				if (worker_main_send_subtests(sock, state))
1966 					goto out;
1967 
1968 			if (env.debug)
1969 				fprintf(stderr, "[%d]: #%d:%s done.\n",
1970 					env.worker_id,
1971 					test_to_run + 1,
1972 					test->test_name);
1973 			break;
1974 		} /* case MSG_DO_TEST */
1975 		default:
1976 			if (env.debug)
1977 				fprintf(stderr, "[%d]: unknown message.\n",  env.worker_id);
1978 			return -1;
1979 		}
1980 	}
1981 out:
1982 	return 0;
1983 }
1984 
1985 static void free_test_states(void)
1986 {
1987 	int i, j;
1988 
1989 	for (i = 0; i < ARRAY_SIZE(prog_test_defs); i++) {
1990 		struct test_state *test_state = &test_states[i];
1991 
1992 		for (j = 0; j < test_state->subtest_num; j++)
1993 			free_subtest_state(&test_state->subtest_states[j]);
1994 
1995 		free(test_state->subtest_states);
1996 		free(test_state->log_buf);
1997 		test_state->subtest_states = NULL;
1998 		test_state->log_buf = NULL;
1999 	}
2000 }
2001 
2002 static __u32 register_session_key(const char *key_data, size_t key_data_size)
2003 {
2004 	return syscall(__NR_add_key, "asymmetric", "libbpf_session_key",
2005 			(const void *)key_data, key_data_size,
2006 			KEY_SPEC_SESSION_KEYRING);
2007 }
2008 
2009 int main(int argc, char **argv)
2010 {
2011 	static const struct argp argp = {
2012 		.options = opts,
2013 		.parser = parse_arg,
2014 		.doc = argp_program_doc,
2015 	};
2016 	int err, i;
2017 
2018 #ifndef __SANITIZE_ADDRESS__
2019 	struct sigaction sigact = {
2020 		.sa_handler = crash_handler,
2021 		.sa_flags = SA_RESETHAND,
2022 	};
2023 	sigaction(SIGSEGV, &sigact, NULL);
2024 #endif
2025 
2026 	env.stdout_saved = stdout;
2027 	env.stderr_saved = stderr;
2028 
2029 	env.secs_till_notify = 10;
2030 	env.secs_till_kill = 120;
2031 	err = argp_parse(&argp, argc, argv, 0, NULL, &env);
2032 	if (err)
2033 		return err;
2034 
2035 	err = cd_flavor_subdir(argv[0]);
2036 	if (err)
2037 		return err;
2038 
2039 	watchdog_init();
2040 
2041 	/* Use libbpf 1.0 API mode */
2042 	libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
2043 	libbpf_set_print(libbpf_print_fn);
2044 	err = register_session_key((const char *)test_progs_verification_cert,
2045 				   test_progs_verification_cert_len);
2046 	if (err < 0)
2047 		return err;
2048 
2049 	traffic_monitor_set_print(traffic_monitor_print_fn);
2050 
2051 	srand(time(NULL));
2052 
2053 	env.jit_enabled = is_jit_enabled();
2054 	env.nr_cpus = libbpf_num_possible_cpus();
2055 	if (env.nr_cpus < 0) {
2056 		fprintf(stderr, "Failed to get number of CPUs: %d!\n",
2057 			env.nr_cpus);
2058 		return -1;
2059 	}
2060 
2061 	env.has_testmod = true;
2062 	if (!env.list_test_names) {
2063 		/* ensure previous instance of the module is unloaded */
2064 		unload_bpf_testmod(verbose());
2065 
2066 		if (load_bpf_testmod(verbose())) {
2067 			fprintf(env.stderr_saved, "WARNING! Selftests relying on bpf_testmod.ko will be skipped.\n");
2068 			env.has_testmod = false;
2069 		}
2070 	}
2071 
2072 	/* initializing tests */
2073 	for (i = 0; i < prog_test_cnt; i++) {
2074 		struct prog_test_def *test = &prog_test_defs[i];
2075 
2076 		test->test_num = i + 1;
2077 		test->selected = should_run(&env.test_selector,
2078 					    test->test_num, test->test_name);
2079 		test->should_run = test->selected;
2080 
2081 		if (test->run_test && test->run_serial_test) {
2082 			fprintf(stderr, "Test %d:%s must have either test_%s() or serial_test_%sl() defined.\n",
2083 				test->test_num, test->test_name, test->test_name, test->test_name);
2084 			exit(EXIT_ERR_SETUP_INFRA);
2085 		}
2086 		if (!test->run_test && !test->run_serial_test) {
2087 			test->not_built = true;
2088 			test->should_run = false;
2089 			continue;
2090 		}
2091 		if (test->should_run)
2092 			test->should_tmon = should_tmon(&env.tmon_selector, test->test_name);
2093 	}
2094 
2095 	/* ignore workers if we are just listing */
2096 	if (env.get_test_cnt || env.list_test_names)
2097 		env.workers = 0;
2098 
2099 	/* launch workers if requested */
2100 	env.worker_id = -1; /* main process */
2101 	if (env.workers) {
2102 		env.worker_pids = calloc(sizeof(pid_t), env.workers);
2103 		env.worker_socks = calloc(sizeof(int), env.workers);
2104 		if (env.debug)
2105 			fprintf(stdout, "Launching %d workers.\n", env.workers);
2106 		for (i = 0; i < env.workers; i++) {
2107 			int sv[2];
2108 			pid_t pid;
2109 
2110 			if (socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sv) < 0) {
2111 				perror("Fail to create worker socket");
2112 				return -1;
2113 			}
2114 			pid = fork();
2115 			if (pid < 0) {
2116 				perror("Failed to fork worker");
2117 				return -1;
2118 			} else if (pid != 0) { /* main process */
2119 				close(sv[1]);
2120 				env.worker_pids[i] = pid;
2121 				env.worker_socks[i] = sv[0];
2122 			} else { /* inside each worker process */
2123 				close(sv[0]);
2124 				env.worker_id = i;
2125 				return worker_main(sv[1]);
2126 			}
2127 		}
2128 
2129 		if (env.worker_id == -1) {
2130 			server_main();
2131 			goto out;
2132 		}
2133 	}
2134 
2135 	/* The rest of the main process */
2136 
2137 	/* on single mode */
2138 	save_netns();
2139 
2140 	for (i = 0; i < prog_test_cnt; i++) {
2141 		struct prog_test_def *test = &prog_test_defs[i];
2142 		struct test_state *state = &test_states[i];
2143 
2144 		if (!test->should_run) {
2145 			if (test->not_built && test->selected &&
2146 			    !env.get_test_cnt && !env.list_test_names) {
2147 				state->tested = true;
2148 				state->skip_cnt = 1;
2149 				env.not_built_cnt++;
2150 				print_test_result(test, state);
2151 			}
2152 			continue;
2153 		}
2154 
2155 		if (env.get_test_cnt) {
2156 			env.succ_cnt++;
2157 			continue;
2158 		}
2159 
2160 		if (env.list_test_names) {
2161 			fprintf(env.stdout_saved, "%s\n", test->test_name);
2162 			env.succ_cnt++;
2163 			continue;
2164 		}
2165 
2166 		run_one_test(i);
2167 	}
2168 
2169 	if (env.get_test_cnt) {
2170 		printf("%d\n", env.succ_cnt);
2171 		goto out;
2172 	}
2173 
2174 	if (env.list_test_names)
2175 		goto out;
2176 
2177 	calculate_summary_and_print_errors(&env);
2178 
2179 	close(env.saved_netns_fd);
2180 out:
2181 	if (!env.list_test_names && env.has_testmod)
2182 		unload_bpf_testmod(verbose());
2183 
2184 	free_test_selector(&env.test_selector);
2185 	free_test_selector(&env.subtest_selector);
2186 	free_test_selector(&env.tmon_selector);
2187 	free_test_states();
2188 
2189 	if (env.succ_cnt + env.fail_cnt + env.skip_cnt == 0)
2190 		return EXIT_NO_TEST;
2191 
2192 	return env.fail_cnt ? EXIT_FAILURE : EXIT_SUCCESS;
2193 }
2194