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