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