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