1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2017 Facebook
5 #include "test_progs.h"
6 #include "testing_helpers.h"
7 #include "cgroup_helpers.h"
13 #include <sys/sysinfo.h> /* get_nprocs */
14 #include <netinet/in.h>
15 #include <sys/select.h>
16 #include <sys/socket.h>
20 #include "json_writer.h"
22 #include "network_helpers.h"
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.
29 #include <execinfo.h> /* backtrace */
31 __weak int backtrace(void **buffer, int size)
36 __weak void backtrace_symbols_fd(void *const *buffer, int size, int fd)
38 dprintf(fd, "<backtrace not supported>\n");
42 int env_verbosity = 0;
44 static bool verbose(void)
46 return env.verbosity > VERBOSE_NONE;
49 static void stdio_hijack_init(char **log_buf, size_t *log_cnt)
52 if (verbose() && env.worker_id == -1) {
53 /* nothing to do, output to stdout by default */
60 stdout = open_memstream(log_buf, log_cnt);
62 stdout = env.stdout_saved;
63 perror("open_memstream");
67 if (env.subtest_state)
68 env.subtest_state->stdout_saved = stdout;
70 env.test_state->stdout_saved = stdout;
76 static void stdio_hijack(char **log_buf, size_t *log_cnt)
79 if (verbose() && env.worker_id == -1) {
80 /* nothing to do, output to stdout by default */
84 env.stdout_saved = stdout;
85 env.stderr_saved = stderr;
87 stdio_hijack_init(log_buf, log_cnt);
91 static void stdio_restore_cleanup(void)
94 if (verbose() && env.worker_id == -1) {
95 /* nothing to do, output to stdout by default */
101 if (env.subtest_state) {
102 fclose(env.subtest_state->stdout_saved);
103 env.subtest_state->stdout_saved = NULL;
104 stdout = env.test_state->stdout_saved;
105 stderr = env.test_state->stdout_saved;
107 fclose(env.test_state->stdout_saved);
108 env.test_state->stdout_saved = NULL;
113 static void stdio_restore(void)
116 if (verbose() && env.worker_id == -1) {
117 /* nothing to do, output to stdout by default */
121 if (stdout == env.stdout_saved)
124 stdio_restore_cleanup();
126 stdout = env.stdout_saved;
127 stderr = env.stderr_saved;
131 /* Adapted from perf/util/string.c */
132 static bool glob_match(const char *str, const char *pat)
134 while (*str && *pat && *pat != '*') {
140 /* Check wild card */
144 if (!*pat) /* Tail wild card matches all */
147 if (glob_match(str++, pat))
150 return !*str && !*pat;
153 #define EXIT_NO_TEST 2
154 #define EXIT_ERR_SETUP_INFRA 3
156 /* defined in test_progs.h */
157 struct test_env env = {};
159 struct prog_test_def {
160 const char *test_name;
162 void (*run_test)(void);
163 void (*run_serial_test)(void);
165 bool need_cgroup_cleanup;
169 /* Override C runtime library's usleep() implementation to ensure nanosleep()
170 * is always called. Usleep is frequently used in selftests as a way to
171 * trigger kprobe and tracepoints.
173 int usleep(useconds_t usec)
175 struct timespec ts = {
176 .tv_sec = usec / 1000000,
177 .tv_nsec = (usec % 1000000) * 1000,
180 return syscall(__NR_nanosleep, &ts, NULL);
183 /* Watchdog timer is started by watchdog_start() and stopped by watchdog_stop().
184 * If timer is active for longer than env.secs_till_notify,
185 * it prints the name of the current test to the stderr.
186 * If timer is active for longer than env.secs_till_kill,
187 * it kills the thread executing the test by sending a SIGSEGV signal to it.
189 static void watchdog_timer_func(union sigval sigval)
191 struct itimerspec timeout = {};
195 if (env.subtest_state)
196 snprintf(test_name, sizeof(test_name), "%s/%s",
197 env.test->test_name, env.subtest_state->name);
199 snprintf(test_name, sizeof(test_name), "%s",
200 env.test->test_name);
202 switch (env.watchdog_state) {
204 fprintf(env.stderr_saved, "WATCHDOG: test case %s executes for %d seconds...\n",
205 test_name, env.secs_till_notify);
206 timeout.it_value.tv_sec = env.secs_till_kill - env.secs_till_notify;
207 env.watchdog_state = WD_KILL;
208 err = timer_settime(env.watchdog, 0, &timeout, NULL);
210 fprintf(env.stderr_saved, "Failed to arm watchdog timer\n");
213 fprintf(env.stderr_saved,
214 "WATCHDOG: test case %s executes for %d seconds, terminating with SIGSEGV\n",
215 test_name, env.secs_till_kill);
216 pthread_kill(env.main_thread, SIGSEGV);
221 static void watchdog_start(void)
223 struct itimerspec timeout = {};
226 if (env.secs_till_kill == 0)
228 if (env.secs_till_notify > 0) {
229 env.watchdog_state = WD_NOTIFY;
230 timeout.it_value.tv_sec = env.secs_till_notify;
232 env.watchdog_state = WD_KILL;
233 timeout.it_value.tv_sec = env.secs_till_kill;
235 err = timer_settime(env.watchdog, 0, &timeout, NULL);
237 fprintf(env.stderr_saved, "Failed to start watchdog timer\n");
240 static void watchdog_stop(void)
242 struct itimerspec timeout = {};
245 env.watchdog_state = WD_NOTIFY;
246 err = timer_settime(env.watchdog, 0, &timeout, NULL);
248 fprintf(env.stderr_saved, "Failed to stop watchdog timer\n");
251 static void watchdog_init(void)
253 struct sigevent watchdog_sev = {
254 .sigev_notify = SIGEV_THREAD,
255 .sigev_notify_function = watchdog_timer_func,
259 env.main_thread = pthread_self();
260 err = timer_create(CLOCK_MONOTONIC, &watchdog_sev, &env.watchdog);
262 fprintf(stderr, "Failed to initialize watchdog timer\n");
265 static bool should_run(struct test_selector *sel, int num, const char *name)
269 for (i = 0; i < sel->blacklist.cnt; i++) {
270 if (glob_match(name, sel->blacklist.tests[i].name) &&
271 !sel->blacklist.tests[i].subtest_cnt)
275 for (i = 0; i < sel->whitelist.cnt; i++) {
276 if (glob_match(name, sel->whitelist.tests[i].name))
280 if (!sel->whitelist.cnt && !sel->num_set)
283 return num < sel->num_set_len && sel->num_set[num];
286 static bool match_subtest(struct test_filter_set *filter,
287 const char *test_name,
288 const char *subtest_name)
292 for (i = 0; i < filter->cnt; i++) {
293 if (glob_match(test_name, filter->tests[i].name)) {
294 if (!filter->tests[i].subtest_cnt)
297 for (j = 0; j < filter->tests[i].subtest_cnt; j++) {
298 if (glob_match(subtest_name,
299 filter->tests[i].subtests[j]))
308 static bool should_run_subtest(struct test_selector *sel,
309 struct test_selector *subtest_sel,
311 const char *test_name,
312 const char *subtest_name)
314 if (match_subtest(&sel->blacklist, test_name, subtest_name))
317 if (match_subtest(&sel->whitelist, test_name, subtest_name))
320 if (!sel->whitelist.cnt && !subtest_sel->num_set)
323 return subtest_num < subtest_sel->num_set_len && subtest_sel->num_set[subtest_num];
326 static bool should_tmon(struct test_selector *sel, const char *name)
330 for (i = 0; i < sel->whitelist.cnt; i++) {
331 if (glob_match(name, sel->whitelist.tests[i].name) &&
332 !sel->whitelist.tests[i].subtest_cnt)
339 static char *test_result(bool failed, bool skipped)
341 return failed ? "FAIL" : (skipped ? "SKIP" : "OK");
344 #define TEST_NUM_WIDTH 7
346 static void print_test_result(const struct prog_test_def *test, const struct test_state *test_state)
348 int skipped_cnt = test_state->skip_cnt;
349 int subtests_cnt = test_state->subtest_num;
351 fprintf(env.stdout_saved, "#%-*d %s:", TEST_NUM_WIDTH, test->test_num, test->test_name);
352 if (test_state->error_cnt)
353 fprintf(env.stdout_saved, "FAIL");
354 else if (!skipped_cnt)
355 fprintf(env.stdout_saved, "OK");
356 else if (skipped_cnt == subtests_cnt || !subtests_cnt)
357 fprintf(env.stdout_saved, "SKIP");
359 fprintf(env.stdout_saved, "OK (SKIP: %d/%d)", skipped_cnt, subtests_cnt);
361 fprintf(env.stdout_saved, "\n");
364 static void print_test_log(char *log_buf, size_t log_cnt)
366 log_buf[log_cnt] = '\0';
367 fprintf(env.stdout_saved, "%s", log_buf);
368 if (log_buf[log_cnt - 1] != '\n')
369 fprintf(env.stdout_saved, "\n");
372 static void print_subtest_name(int test_num, int subtest_num,
373 const char *test_name, char *subtest_name,
376 char test_num_str[32];
378 snprintf(test_num_str, sizeof(test_num_str), "%d/%d", test_num, subtest_num);
380 fprintf(env.stdout_saved, "#%-*s %s/%s",
381 TEST_NUM_WIDTH, test_num_str,
382 test_name, subtest_name);
385 fprintf(env.stdout_saved, ":%s", result);
387 fprintf(env.stdout_saved, "\n");
390 static void jsonw_write_log_message(json_writer_t *w, char *log_buf, size_t log_cnt)
392 /* open_memstream (from stdio_hijack_init) ensures that log_bug is terminated by a
393 * null byte. Yet in parallel mode, log_buf will be NULL if there is no message.
396 jsonw_string_field(w, "message", log_buf);
398 jsonw_string_field(w, "message", "");
402 static void dump_test_log(const struct prog_test_def *test,
403 const struct test_state *test_state,
404 bool skip_ok_subtests,
405 bool par_exec_result,
408 bool test_failed = test_state->error_cnt > 0;
409 bool force_log = test_state->force_log;
410 bool print_test = verbose() || force_log || test_failed;
412 struct subtest_state *subtest_state;
414 bool subtest_filtered;
417 /* we do not print anything in the worker thread */
418 if (env.worker_id != -1)
421 /* there is nothing to print when verbose log is used and execution
422 * is not in parallel mode
424 if (verbose() && !par_exec_result)
427 if (test_state->log_cnt && print_test)
428 print_test_log(test_state->log_buf, test_state->log_cnt);
430 if (w && print_test) {
431 jsonw_start_object(w);
432 jsonw_string_field(w, "name", test->test_name);
433 jsonw_uint_field(w, "number", test->test_num);
434 jsonw_write_log_message(w, test_state->log_buf, test_state->log_cnt);
435 jsonw_bool_field(w, "failed", test_failed);
436 jsonw_name(w, "subtests");
437 jsonw_start_array(w);
440 for (i = 0; i < test_state->subtest_num; i++) {
441 subtest_state = &test_state->subtest_states[i];
442 subtest_failed = subtest_state->error_cnt;
443 subtest_filtered = subtest_state->filtered;
444 print_subtest = verbose() || force_log || subtest_failed;
446 if ((skip_ok_subtests && !subtest_failed) || subtest_filtered)
449 if (subtest_state->log_cnt && print_subtest) {
450 print_test_log(subtest_state->log_buf,
451 subtest_state->log_cnt);
454 print_subtest_name(test->test_num, i + 1,
455 test->test_name, subtest_state->name,
456 test_result(subtest_state->error_cnt,
457 subtest_state->skipped));
459 if (w && print_subtest) {
460 jsonw_start_object(w);
461 jsonw_string_field(w, "name", subtest_state->name);
462 jsonw_uint_field(w, "number", i+1);
463 jsonw_write_log_message(w, subtest_state->log_buf, subtest_state->log_cnt);
464 jsonw_bool_field(w, "failed", subtest_failed);
469 if (w && print_test) {
474 print_test_result(test, test_state);
477 static void stdio_restore(void);
479 /* A bunch of tests set custom affinity per-thread and/or per-process. Reset
480 * it after each test/sub-test.
482 static void reset_affinity(void)
488 for (i = 0; i < env.nr_cpus; i++)
491 err = sched_setaffinity(0, sizeof(cpuset), &cpuset);
494 fprintf(stderr, "Failed to reset process affinity: %d!\n", err);
495 exit(EXIT_ERR_SETUP_INFRA);
497 err = pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);
500 fprintf(stderr, "Failed to reset thread affinity: %d!\n", err);
501 exit(EXIT_ERR_SETUP_INFRA);
505 static void save_netns(void)
507 env.saved_netns_fd = open("/proc/self/ns/net", O_RDONLY);
508 if (env.saved_netns_fd == -1) {
509 perror("open(/proc/self/ns/net)");
510 exit(EXIT_ERR_SETUP_INFRA);
514 static void restore_netns(void)
516 if (setns(env.saved_netns_fd, CLONE_NEWNET) == -1) {
518 perror("setns(CLONE_NEWNS)");
519 exit(EXIT_ERR_SETUP_INFRA);
523 void test__end_subtest(void)
525 struct prog_test_def *test = env.test;
526 struct test_state *test_state = env.test_state;
527 struct subtest_state *subtest_state = env.subtest_state;
529 if (subtest_state->error_cnt) {
530 test_state->error_cnt++;
532 if (!subtest_state->skipped)
533 test_state->sub_succ_cnt++;
535 test_state->skip_cnt++;
538 if (verbose() && !env.workers)
539 print_subtest_name(test->test_num, test_state->subtest_num,
540 test->test_name, subtest_state->name,
541 test_result(subtest_state->error_cnt,
542 subtest_state->skipped));
544 stdio_restore_cleanup();
545 env.subtest_state = NULL;
548 bool test__start_subtest(const char *subtest_name)
550 struct prog_test_def *test = env.test;
551 struct test_state *state = env.test_state;
552 struct subtest_state *subtest_state;
553 size_t sub_state_size = sizeof(*subtest_state);
555 if (env.subtest_state)
558 state->subtest_num++;
559 state->subtest_states =
560 realloc(state->subtest_states,
561 state->subtest_num * sub_state_size);
562 if (!state->subtest_states) {
563 fprintf(stderr, "Not enough memory to allocate subtest result\n");
567 subtest_state = &state->subtest_states[state->subtest_num - 1];
569 memset(subtest_state, 0, sub_state_size);
571 if (!subtest_name || !subtest_name[0]) {
572 fprintf(env.stderr_saved,
573 "Subtest #%d didn't provide sub-test name!\n",
578 subtest_state->name = strdup(subtest_name);
579 if (!subtest_state->name) {
580 fprintf(env.stderr_saved,
581 "Subtest #%d: failed to copy subtest name!\n",
586 if (!should_run_subtest(&env.test_selector,
587 &env.subtest_selector,
591 subtest_state->filtered = true;
595 subtest_state->should_tmon = match_subtest(&env.tmon_selector.whitelist,
599 env.subtest_state = subtest_state;
600 stdio_hijack_init(&subtest_state->log_buf, &subtest_state->log_cnt);
606 void test__force_log(void)
608 env.test_state->force_log = true;
611 void test__skip(void)
613 if (env.subtest_state)
614 env.subtest_state->skipped = true;
616 env.test_state->skip_cnt++;
619 void test__fail(void)
621 if (env.subtest_state)
622 env.subtest_state->error_cnt++;
624 env.test_state->error_cnt++;
627 int test__join_cgroup(const char *path)
631 if (!env.test->need_cgroup_cleanup) {
632 if (setup_cgroup_environment()) {
634 "#%d %s: Failed to setup cgroup environment\n",
635 env.test->test_num, env.test->test_name);
639 env.test->need_cgroup_cleanup = true;
642 fd = create_and_get_cgroup(path);
645 "#%d %s: Failed to create cgroup '%s' (errno=%d)\n",
646 env.test->test_num, env.test->test_name, path, errno);
650 if (join_cgroup(path)) {
652 "#%d %s: Failed to join cgroup '%s' (errno=%d)\n",
653 env.test->test_num, env.test->test_name, path, errno);
660 int bpf_find_map(const char *test, struct bpf_object *obj, const char *name)
664 map = bpf_object__find_map_by_name(obj, name);
666 fprintf(stdout, "%s:FAIL:map '%s' not found\n", test, name);
670 return bpf_map__fd(map);
673 int compare_map_keys(int map1_fd, int map2_fd)
676 char val_buf[PERF_MAX_STACK_DEPTH *
677 sizeof(struct bpf_stack_build_id)];
680 err = bpf_map_get_next_key(map1_fd, NULL, &key);
683 err = bpf_map_lookup_elem(map2_fd, &key, val_buf);
687 while (bpf_map_get_next_key(map1_fd, &key, &next_key) == 0) {
688 err = bpf_map_lookup_elem(map2_fd, &next_key, val_buf);
700 int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len)
702 __u32 key, next_key, *cur_key_p, *next_key_p;
703 char *val_buf1, *val_buf2;
706 val_buf1 = malloc(stack_trace_len);
707 val_buf2 = malloc(stack_trace_len);
710 while (bpf_map_get_next_key(smap_fd, cur_key_p, next_key_p) == 0) {
711 err = bpf_map_lookup_elem(smap_fd, next_key_p, val_buf1);
714 err = bpf_map_lookup_elem(amap_fd, next_key_p, val_buf2);
717 for (i = 0; i < stack_trace_len; i++) {
718 if (val_buf1[i] != val_buf2[i]) {
725 next_key_p = &next_key;
738 struct tmonitor_ctx *tmon;
739 struct nstoken *nstoken;
742 /* Create a new network namespace with the given name.
744 * Create a new network namespace and set the network namespace of the
745 * current process to the new network namespace if the argument "open" is
746 * true. This function should be paired with netns_free() to release the
747 * resource and delete the network namespace.
749 * It also implements the functionality of the option "-m" by starting
750 * traffic monitor on the background to capture the packets in this network
751 * namespace if the current test or subtest matching the pattern.
753 * nsname: the name of the network namespace to create.
754 * open: open the network namespace if true.
756 * Return: the network namespace object on success, NULL on failure.
758 struct netns_obj *netns_new(const char *nsname, bool open)
760 struct netns_obj *netns_obj = malloc(sizeof(*netns_obj));
761 const char *test_name, *subtest_name;
766 memset(netns_obj, 0, sizeof(*netns_obj));
768 netns_obj->nsname = strdup(nsname);
769 if (!netns_obj->nsname)
772 /* Create the network namespace */
773 r = make_netns(nsname);
777 /* Start traffic monitor */
778 if (env.test->should_tmon ||
779 (env.subtest_state && env.subtest_state->should_tmon)) {
780 test_name = env.test->test_name;
781 subtest_name = env.subtest_state ? env.subtest_state->name : NULL;
782 netns_obj->tmon = traffic_monitor_start(nsname, test_name, subtest_name);
783 if (!netns_obj->tmon) {
784 fprintf(stderr, "Failed to start traffic monitor for %s\n", nsname);
788 netns_obj->tmon = NULL;
792 netns_obj->nstoken = open_netns(nsname);
793 if (!netns_obj->nstoken)
799 traffic_monitor_stop(netns_obj->tmon);
800 remove_netns(nsname);
801 free(netns_obj->nsname);
806 /* Delete the network namespace.
808 * This function should be paired with netns_new() to delete the namespace
809 * created by netns_new().
811 void netns_free(struct netns_obj *netns_obj)
815 traffic_monitor_stop(netns_obj->tmon);
816 close_netns(netns_obj->nstoken);
817 remove_netns(netns_obj->nsname);
818 free(netns_obj->nsname);
822 /* extern declarations for test funcs */
823 #define DEFINE_TEST(name) \
824 extern void test_##name(void) __weak; \
825 extern void serial_test_##name(void) __weak;
826 #include <prog_tests/tests.h>
829 static struct prog_test_def prog_test_defs[] = {
830 #define DEFINE_TEST(name) { \
831 .test_name = #name, \
832 .run_test = &test_##name, \
833 .run_serial_test = &serial_test_##name, \
835 #include <prog_tests/tests.h>
839 static const int prog_test_cnt = ARRAY_SIZE(prog_test_defs);
841 static struct test_state test_states[ARRAY_SIZE(prog_test_defs)];
843 const char *argp_program_version = "test_progs 0.1";
845 static const char argp_program_doc[] =
846 "BPF selftests test runner\v"
847 "Options accepting the NAMES parameter take either a comma-separated list\n"
848 "of test names, or a filename prefixed with @. The file contains one name\n"
849 "(or wildcard pattern) per line, and comments beginning with # are ignored.\n"
851 "These options can be passed repeatedly to read multiple files.\n";
856 ARG_TEST_NAME_BLACKLIST = 'b',
857 ARG_VERIFIER_STATS = 's',
859 ARG_GET_TEST_CNT = 'c',
860 ARG_LIST_TEST_NAMES = 'l',
861 ARG_TEST_NAME_GLOB_ALLOWLIST = 'a',
862 ARG_TEST_NAME_GLOB_DENYLIST = 'd',
863 ARG_NUM_WORKERS = 'j',
865 ARG_JSON_SUMMARY = 'J',
866 ARG_TRAFFIC_MONITOR = 'm',
867 ARG_WATCHDOG_TIMEOUT = 'w',
870 static const struct argp_option opts[] = {
871 { "num", ARG_TEST_NUM, "NUM", 0,
872 "Run test number NUM only " },
873 { "name", ARG_TEST_NAME, "NAMES", 0,
874 "Run tests with names containing any string from NAMES list" },
875 { "name-blacklist", ARG_TEST_NAME_BLACKLIST, "NAMES", 0,
876 "Don't run tests with names containing any string from NAMES list" },
877 { "verifier-stats", ARG_VERIFIER_STATS, NULL, 0,
878 "Output verifier statistics", },
879 { "verbose", ARG_VERBOSE, "LEVEL", OPTION_ARG_OPTIONAL,
880 "Verbose output (use -vv or -vvv for progressively verbose output)" },
881 { "count", ARG_GET_TEST_CNT, NULL, 0,
882 "Get number of selected top-level tests " },
883 { "list", ARG_LIST_TEST_NAMES, NULL, 0,
884 "List test names that would run (without running them) " },
885 { "allow", ARG_TEST_NAME_GLOB_ALLOWLIST, "NAMES", 0,
886 "Run tests with name matching the pattern (supports '*' wildcard)." },
887 { "deny", ARG_TEST_NAME_GLOB_DENYLIST, "NAMES", 0,
888 "Don't run tests with name matching the pattern (supports '*' wildcard)." },
889 { "workers", ARG_NUM_WORKERS, "WORKERS", OPTION_ARG_OPTIONAL,
890 "Number of workers to run in parallel, default to number of cpus." },
891 { "debug", ARG_DEBUG, NULL, 0,
892 "print extra debug information for test_progs." },
893 { "json-summary", ARG_JSON_SUMMARY, "FILE", 0, "Write report in json format to this file."},
894 #ifdef TRAFFIC_MONITOR
895 { "traffic-monitor", ARG_TRAFFIC_MONITOR, "NAMES", 0,
896 "Monitor network traffic of tests with name matching the pattern (supports '*' wildcard)." },
898 { "watchdog-timeout", ARG_WATCHDOG_TIMEOUT, "SECONDS", 0,
899 "Kill the process if tests are not making progress for specified number of seconds." },
903 static FILE *libbpf_capture_stream;
908 } libbpf_output_capture;
910 /* Creates a global memstream capturing INFO and WARN level output
911 * passed to libbpf_print_fn.
912 * Returns 0 on success, negative value on failure.
913 * On failure the description is printed using PRINT_FAIL and
914 * current test case is marked as fail.
916 int start_libbpf_log_capture(void)
918 if (libbpf_capture_stream) {
919 PRINT_FAIL("%s: libbpf_capture_stream != NULL\n", __func__);
923 libbpf_capture_stream = open_memstream(&libbpf_output_capture.buf,
924 &libbpf_output_capture.buf_sz);
925 if (!libbpf_capture_stream) {
926 PRINT_FAIL("%s: open_memstream failed errno=%d\n", __func__, errno);
933 /* Destroys global memstream created by start_libbpf_log_capture().
934 * Returns a pointer to captured data which has to be freed.
935 * Returned buffer is null terminated.
937 char *stop_libbpf_log_capture(void)
941 if (!libbpf_capture_stream)
944 fputc(0, libbpf_capture_stream);
945 fclose(libbpf_capture_stream);
946 libbpf_capture_stream = NULL;
947 /* get 'buf' after fclose(), see open_memstream() documentation */
948 buf = libbpf_output_capture.buf;
949 memset(&libbpf_output_capture, 0, sizeof(libbpf_output_capture));
953 static int libbpf_print_fn(enum libbpf_print_level level,
954 const char *format, va_list args)
956 if (libbpf_capture_stream && level != LIBBPF_DEBUG) {
959 va_copy(args2, args);
960 vfprintf(libbpf_capture_stream, format, args2);
964 if (env.verbosity < VERBOSE_VERY && level == LIBBPF_DEBUG)
967 vfprintf(stdout, format, args);
971 static void free_test_filter_set(const struct test_filter_set *set)
978 for (i = 0; i < set->cnt; i++) {
979 free((void *)set->tests[i].name);
980 for (j = 0; j < set->tests[i].subtest_cnt; j++)
981 free((void *)set->tests[i].subtests[j]);
983 free((void *)set->tests[i].subtests);
986 free((void *)set->tests);
989 static void free_test_selector(struct test_selector *test_selector)
991 free_test_filter_set(&test_selector->blacklist);
992 free_test_filter_set(&test_selector->whitelist);
993 free(test_selector->num_set);
996 extern int extra_prog_load_log_flags;
998 static error_t parse_arg(int key, char *arg, struct argp_state *state)
1000 struct test_env *env = state->input;
1004 case ARG_TEST_NUM: {
1005 char *subtest_str = strchr(arg, '/');
1008 *subtest_str = '\0';
1009 if (parse_num_list(subtest_str + 1,
1010 &env->subtest_selector.num_set,
1011 &env->subtest_selector.num_set_len)) {
1013 "Failed to parse subtest numbers.\n");
1017 if (parse_num_list(arg, &env->test_selector.num_set,
1018 &env->test_selector.num_set_len)) {
1019 fprintf(stderr, "Failed to parse test numbers.\n");
1024 case ARG_TEST_NAME_GLOB_ALLOWLIST:
1025 case ARG_TEST_NAME: {
1027 err = parse_test_list_file(arg + 1,
1028 &env->test_selector.whitelist,
1029 key == ARG_TEST_NAME_GLOB_ALLOWLIST);
1031 err = parse_test_list(arg,
1032 &env->test_selector.whitelist,
1033 key == ARG_TEST_NAME_GLOB_ALLOWLIST);
1037 case ARG_TEST_NAME_GLOB_DENYLIST:
1038 case ARG_TEST_NAME_BLACKLIST: {
1040 err = parse_test_list_file(arg + 1,
1041 &env->test_selector.blacklist,
1042 key == ARG_TEST_NAME_GLOB_DENYLIST);
1044 err = parse_test_list(arg,
1045 &env->test_selector.blacklist,
1046 key == ARG_TEST_NAME_GLOB_DENYLIST);
1050 case ARG_VERIFIER_STATS:
1051 env->verifier_stats = true;
1054 env->verbosity = VERBOSE_NORMAL;
1056 if (strcmp(arg, "v") == 0) {
1057 env->verbosity = VERBOSE_VERY;
1058 extra_prog_load_log_flags = 1;
1059 } else if (strcmp(arg, "vv") == 0) {
1060 env->verbosity = VERBOSE_SUPER;
1061 extra_prog_load_log_flags = 2;
1064 "Unrecognized verbosity setting ('%s'), only -v and -vv are supported\n",
1069 env_verbosity = env->verbosity;
1072 if (setenv("SELFTESTS_VERBOSE", "1", 1) == -1) {
1074 "Unable to setenv SELFTESTS_VERBOSE=1 (errno=%d)",
1081 case ARG_GET_TEST_CNT:
1082 env->get_test_cnt = true;
1084 case ARG_LIST_TEST_NAMES:
1085 env->list_test_names = true;
1087 case ARG_NUM_WORKERS:
1089 env->workers = atoi(arg);
1090 if (!env->workers) {
1091 fprintf(stderr, "Invalid number of worker: %s.", arg);
1095 env->workers = get_nprocs();
1101 case ARG_JSON_SUMMARY:
1102 env->json = fopen(arg, "w");
1103 if (env->json == NULL) {
1104 perror("Failed to open json summary file");
1113 #ifdef TRAFFIC_MONITOR
1114 case ARG_TRAFFIC_MONITOR:
1116 err = parse_test_list_file(arg + 1,
1117 &env->tmon_selector.whitelist,
1120 err = parse_test_list(arg,
1121 &env->tmon_selector.whitelist,
1125 case ARG_WATCHDOG_TIMEOUT:
1126 env->secs_till_kill = atoi(arg);
1127 if (env->secs_till_kill < 0) {
1128 fprintf(stderr, "Invalid watchdog timeout: %s.\n", arg);
1131 if (env->secs_till_kill < env->secs_till_notify) {
1132 env->secs_till_notify = 0;
1136 return ARGP_ERR_UNKNOWN;
1142 * Determine if test_progs is running as a "flavored" test runner and switch
1143 * into corresponding sub-directory to load correct BPF objects.
1145 * This is done by looking at executable name. If it contains "-flavor"
1146 * suffix, then we are running as a flavored test runner.
1148 int cd_flavor_subdir(const char *exec_name)
1150 /* General form of argv[0] passed here is:
1151 * some/path/to/test_progs[-flavor], where -flavor part is optional.
1152 * First cut out "test_progs[-flavor]" part, then extract "flavor"
1153 * part, if it's there.
1155 const char *flavor = strrchr(exec_name, '/');
1162 flavor = strrchr(flavor, '-');
1167 fprintf(stdout, "Switching to flavor '%s' subdirectory...\n", flavor);
1169 return chdir(flavor);
1172 int trigger_module_test_read(int read_sz)
1176 fd = open(BPF_TESTMOD_TEST_FILE, O_RDONLY);
1178 if (!ASSERT_GE(fd, 0, "testmod_file_open"))
1181 read(fd, NULL, read_sz);
1187 int trigger_module_test_write(int write_sz)
1190 char *buf = malloc(write_sz);
1195 memset(buf, 'a', write_sz);
1196 buf[write_sz-1] = '\0';
1198 fd = open(BPF_TESTMOD_TEST_FILE, O_WRONLY);
1200 if (!ASSERT_GE(fd, 0, "testmod_file_open")) {
1205 write(fd, buf, write_sz);
1211 int write_sysctl(const char *sysctl, const char *value)
1215 fd = open(sysctl, O_WRONLY);
1216 if (!ASSERT_NEQ(fd, -1, "open sysctl"))
1219 len = strlen(value);
1220 err = write(fd, value, len);
1222 if (!ASSERT_EQ(err, len, "write sysctl"))
1228 int get_bpf_max_tramp_links_from(struct btf *btf)
1230 const struct btf_enum *e;
1231 const struct btf_type *t;
1236 for (i = 1, type_cnt = btf__type_cnt(btf); i < type_cnt; i++) {
1237 t = btf__type_by_id(btf, i);
1238 if (!t || !btf_is_enum(t) || t->name_off)
1241 for (j = 0, vlen = btf_vlen(t); j < vlen; j++, e++) {
1242 name = btf__str_by_offset(btf, e->name_off);
1243 if (name && !strcmp(name, "BPF_MAX_TRAMP_LINKS"))
1251 int get_bpf_max_tramp_links(void)
1253 struct btf *vmlinux_btf;
1256 vmlinux_btf = btf__load_vmlinux_btf();
1257 if (!ASSERT_OK_PTR(vmlinux_btf, "vmlinux btf"))
1259 ret = get_bpf_max_tramp_links_from(vmlinux_btf);
1260 btf__free(vmlinux_btf);
1265 #define MAX_BACKTRACE_SZ 128
1266 void crash_handler(int signum)
1268 void *bt[MAX_BACKTRACE_SZ];
1271 sz = backtrace(bt, ARRAY_SIZE(bt));
1273 if (env.stdout_saved)
1276 env.test_state->error_cnt++;
1277 dump_test_log(env.test, env.test_state, true, false, NULL);
1279 if (env.worker_id != -1)
1280 fprintf(stderr, "[%d]: ", env.worker_id);
1281 fprintf(stderr, "Caught signal #%d!\nStack trace:\n", signum);
1282 backtrace_symbols_fd(bt, sz, STDERR_FILENO);
1285 void hexdump(const char *prefix, const void *buf, size_t len)
1287 for (int i = 0; i < len; i++) {
1290 fprintf(stdout, "\n");
1291 fprintf(stdout, "%s", prefix);
1293 if (i && !(i % 8) && (i % 16))
1294 fprintf(stdout, "\t");
1295 fprintf(stdout, "%02X ", ((uint8_t *)(buf))[i]);
1297 fprintf(stdout, "\n");
1300 static void sigint_handler(int signum)
1304 for (i = 0; i < env.workers; i++)
1305 if (env.worker_socks[i] > 0)
1306 close(env.worker_socks[i]);
1309 static int current_test_idx;
1310 static pthread_mutex_t current_test_lock;
1311 static pthread_mutex_t stdout_output_lock;
1313 static inline const char *str_msg(const struct msg *msg, char *buf)
1315 switch (msg->type) {
1317 sprintf(buf, "MSG_DO_TEST %d", msg->do_test.num);
1320 sprintf(buf, "MSG_TEST_DONE %d (log: %d)",
1322 msg->test_done.have_log);
1324 case MSG_SUBTEST_DONE:
1325 sprintf(buf, "MSG_SUBTEST_DONE %d (log: %d)",
1326 msg->subtest_done.num,
1327 msg->subtest_done.have_log);
1330 sprintf(buf, "MSG_TEST_LOG (cnt: %zu, last: %d)",
1331 strlen(msg->test_log.log_buf),
1332 msg->test_log.is_last);
1335 sprintf(buf, "MSG_EXIT");
1338 sprintf(buf, "UNKNOWN");
1345 static int send_message(int sock, const struct msg *msg)
1350 fprintf(stderr, "Sending msg: %s\n", str_msg(msg, buf));
1351 return send(sock, msg, sizeof(*msg), 0);
1354 static int recv_message(int sock, struct msg *msg)
1359 memset(msg, 0, sizeof(*msg));
1360 ret = recv(sock, msg, sizeof(*msg), 0);
1363 fprintf(stderr, "Received msg: %s\n", str_msg(msg, buf));
1368 static void run_one_test(int test_num)
1370 struct prog_test_def *test = &prog_test_defs[test_num];
1371 struct test_state *state = &test_states[test_num];
1374 env.test_state = state;
1376 stdio_hijack(&state->log_buf, &state->log_cnt);
1381 else if (test->run_serial_test)
1382 test->run_serial_test();
1385 /* ensure last sub-test is finalized properly */
1386 if (env.subtest_state)
1387 test__end_subtest();
1389 state->tested = true;
1391 if (verbose() && env.worker_id == -1)
1392 print_test_result(test, state);
1396 if (test->need_cgroup_cleanup)
1397 cleanup_cgroup_environment();
1400 free(stop_libbpf_log_capture());
1402 dump_test_log(test, state, false, false, NULL);
1405 struct dispatch_data {
1410 static int read_prog_test_msg(int sock_fd, struct msg *msg, enum msg_type type)
1412 if (recv_message(sock_fd, msg) < 0)
1415 if (msg->type != type) {
1416 printf("%s: unexpected message type %d. expected %d\n", __func__, msg->type, type);
1423 static int dispatch_thread_read_log(int sock_fd, char **log_buf, size_t *log_cnt)
1425 FILE *log_fp = NULL;
1428 log_fp = open_memstream(log_buf, log_cnt);
1435 if (read_prog_test_msg(sock_fd, &msg, MSG_TEST_LOG)) {
1440 fprintf(log_fp, "%s", msg.test_log.log_buf);
1441 if (msg.test_log.is_last)
1451 static int dispatch_thread_send_subtests(int sock_fd, struct test_state *state)
1454 struct subtest_state *subtest_state;
1455 int subtest_num = state->subtest_num;
1457 state->subtest_states = malloc(subtest_num * sizeof(*subtest_state));
1459 for (int i = 0; i < subtest_num; i++) {
1460 subtest_state = &state->subtest_states[i];
1462 memset(subtest_state, 0, sizeof(*subtest_state));
1464 if (read_prog_test_msg(sock_fd, &msg, MSG_SUBTEST_DONE))
1467 subtest_state->name = strdup(msg.subtest_done.name);
1468 subtest_state->error_cnt = msg.subtest_done.error_cnt;
1469 subtest_state->skipped = msg.subtest_done.skipped;
1470 subtest_state->filtered = msg.subtest_done.filtered;
1472 /* collect all logs */
1473 if (msg.subtest_done.have_log)
1474 if (dispatch_thread_read_log(sock_fd,
1475 &subtest_state->log_buf,
1476 &subtest_state->log_cnt))
1483 static void *dispatch_thread(void *ctx)
1485 struct dispatch_data *data = ctx;
1488 sock_fd = data->sock_fd;
1491 int test_to_run = -1;
1492 struct prog_test_def *test;
1493 struct test_state *state;
1497 pthread_mutex_lock(¤t_test_lock);
1499 if (current_test_idx >= prog_test_cnt) {
1500 pthread_mutex_unlock(¤t_test_lock);
1504 test = &prog_test_defs[current_test_idx];
1505 test_to_run = current_test_idx;
1508 pthread_mutex_unlock(¤t_test_lock);
1511 if (!test->should_run || test->run_serial_test)
1514 /* run test through worker */
1516 struct msg msg_do_test;
1518 memset(&msg_do_test, 0, sizeof(msg_do_test));
1519 msg_do_test.type = MSG_DO_TEST;
1520 msg_do_test.do_test.num = test_to_run;
1521 if (send_message(sock_fd, &msg_do_test) < 0) {
1522 perror("Fail to send command");
1525 env.worker_current_test[data->worker_id] = test_to_run;
1528 /* wait for test done */
1532 if (read_prog_test_msg(sock_fd, &msg, MSG_TEST_DONE))
1534 if (test_to_run != msg.test_done.num)
1537 state = &test_states[test_to_run];
1538 state->tested = true;
1539 state->error_cnt = msg.test_done.error_cnt;
1540 state->skip_cnt = msg.test_done.skip_cnt;
1541 state->sub_succ_cnt = msg.test_done.sub_succ_cnt;
1542 state->subtest_num = msg.test_done.subtest_num;
1544 /* collect all logs */
1545 if (msg.test_done.have_log) {
1546 if (dispatch_thread_read_log(sock_fd,
1552 /* collect all subtests and subtest logs */
1553 if (!state->subtest_num)
1556 if (dispatch_thread_send_subtests(sock_fd, state))
1560 pthread_mutex_lock(&stdout_output_lock);
1561 dump_test_log(test, state, false, true, NULL);
1562 pthread_mutex_unlock(&stdout_output_lock);
1563 } /* while (true) */
1566 fprintf(stderr, "[%d]: Protocol/IO error: %s.\n", data->worker_id, strerror(errno));
1570 struct msg msg_exit;
1572 msg_exit.type = MSG_EXIT;
1573 if (send_message(sock_fd, &msg_exit) < 0) {
1575 fprintf(stderr, "[%d]: send_message msg_exit: %s.\n",
1576 data->worker_id, strerror(errno));
1582 static void calculate_summary_and_print_errors(struct test_env *env)
1585 int succ_cnt = 0, fail_cnt = 0, sub_succ_cnt = 0, skip_cnt = 0;
1586 json_writer_t *w = NULL;
1588 for (i = 0; i < prog_test_cnt; i++) {
1589 struct test_state *state = &test_states[i];
1594 sub_succ_cnt += state->sub_succ_cnt;
1595 skip_cnt += state->skip_cnt;
1597 if (state->error_cnt)
1604 w = jsonw_new(env->json);
1606 fprintf(env->stderr_saved, "Failed to create new JSON stream.");
1610 jsonw_start_object(w);
1611 jsonw_uint_field(w, "success", succ_cnt);
1612 jsonw_uint_field(w, "success_subtest", sub_succ_cnt);
1613 jsonw_uint_field(w, "skipped", skip_cnt);
1614 jsonw_uint_field(w, "failed", fail_cnt);
1615 jsonw_name(w, "results");
1616 jsonw_start_array(w);
1620 * We only print error logs summary when there are failed tests and
1621 * verbose mode is not enabled. Otherwise, results may be inconsistent.
1624 if (!verbose() && fail_cnt) {
1625 printf("\nAll error logs:\n");
1627 /* print error logs again */
1628 for (i = 0; i < prog_test_cnt; i++) {
1629 struct prog_test_def *test = &prog_test_defs[i];
1630 struct test_state *state = &test_states[i];
1632 if (!state->tested || !state->error_cnt)
1635 dump_test_log(test, state, true, true, w);
1641 jsonw_end_object(w);
1648 printf("Summary: %d/%d PASSED, %d SKIPPED, %d FAILED\n",
1649 succ_cnt, sub_succ_cnt, skip_cnt, fail_cnt);
1651 env->succ_cnt = succ_cnt;
1652 env->sub_succ_cnt = sub_succ_cnt;
1653 env->fail_cnt = fail_cnt;
1654 env->skip_cnt = skip_cnt;
1657 static void server_main(void)
1659 pthread_t *dispatcher_threads;
1660 struct dispatch_data *data;
1661 struct sigaction sigact_int = {
1662 .sa_handler = sigint_handler,
1663 .sa_flags = SA_RESETHAND,
1667 sigaction(SIGINT, &sigact_int, NULL);
1669 dispatcher_threads = calloc(sizeof(pthread_t), env.workers);
1670 data = calloc(sizeof(struct dispatch_data), env.workers);
1672 env.worker_current_test = calloc(sizeof(int), env.workers);
1673 for (i = 0; i < env.workers; i++) {
1676 data[i].worker_id = i;
1677 data[i].sock_fd = env.worker_socks[i];
1678 rc = pthread_create(&dispatcher_threads[i], NULL, dispatch_thread, &data[i]);
1680 perror("Failed to launch dispatcher thread");
1681 exit(EXIT_ERR_SETUP_INFRA);
1685 /* wait for all dispatcher to finish */
1686 for (i = 0; i < env.workers; i++) {
1688 int ret = pthread_tryjoin_np(dispatcher_threads[i], NULL);
1692 } else if (ret == EBUSY) {
1694 fprintf(stderr, "Still waiting for thread %d (test %d).\n",
1695 i, env.worker_current_test[i] + 1);
1696 usleep(1000 * 1000);
1699 fprintf(stderr, "Unexpected error joining dispatcher thread: %d", ret);
1704 free(dispatcher_threads);
1705 free(env.worker_current_test);
1708 /* run serial tests */
1711 for (int i = 0; i < prog_test_cnt; i++) {
1712 struct prog_test_def *test = &prog_test_defs[i];
1714 if (!test->should_run || !test->run_serial_test)
1720 /* generate summary */
1724 calculate_summary_and_print_errors(&env);
1726 /* reap all workers */
1727 for (i = 0; i < env.workers; i++) {
1730 pid = waitpid(env.worker_pids[i], &wstatus, 0);
1731 if (pid != env.worker_pids[i])
1732 perror("Unable to reap worker");
1736 static void worker_main_send_log(int sock, char *log_buf, size_t log_cnt)
1748 memset(&msg_log, 0, sizeof(msg_log));
1749 msg_log.type = MSG_TEST_LOG;
1750 dest = msg_log.test_log.log_buf;
1751 len = slen >= MAX_LOG_TRUNK_SIZE ? MAX_LOG_TRUNK_SIZE : slen;
1752 memcpy(dest, src, len);
1757 msg_log.test_log.is_last = true;
1759 assert(send_message(sock, &msg_log) >= 0);
1763 static void free_subtest_state(struct subtest_state *state)
1765 if (state->log_buf) {
1766 free(state->log_buf);
1767 state->log_buf = NULL;
1774 static int worker_main_send_subtests(int sock, struct test_state *state)
1778 struct subtest_state *subtest_state;
1780 memset(&msg, 0, sizeof(msg));
1781 msg.type = MSG_SUBTEST_DONE;
1783 for (i = 0; i < state->subtest_num; i++) {
1784 subtest_state = &state->subtest_states[i];
1786 msg.subtest_done.num = i;
1788 strncpy(msg.subtest_done.name, subtest_state->name, MAX_SUBTEST_NAME);
1790 msg.subtest_done.error_cnt = subtest_state->error_cnt;
1791 msg.subtest_done.skipped = subtest_state->skipped;
1792 msg.subtest_done.filtered = subtest_state->filtered;
1793 msg.subtest_done.have_log = false;
1795 if (verbose() || state->force_log || subtest_state->error_cnt) {
1796 if (subtest_state->log_cnt)
1797 msg.subtest_done.have_log = true;
1800 if (send_message(sock, &msg) < 0) {
1801 perror("Fail to send message done");
1807 if (msg.subtest_done.have_log)
1808 worker_main_send_log(sock, subtest_state->log_buf, subtest_state->log_cnt);
1810 free_subtest_state(subtest_state);
1811 free(subtest_state->name);
1815 for (; i < state->subtest_num; i++)
1816 free_subtest_state(&state->subtest_states[i]);
1817 free(state->subtest_states);
1821 static int worker_main(int sock)
1827 /* receive command */
1830 if (recv_message(sock, &msg) < 0)
1836 fprintf(stderr, "[%d]: worker exit.\n",
1840 int test_to_run = msg.do_test.num;
1841 struct prog_test_def *test = &prog_test_defs[test_to_run];
1842 struct test_state *state = &test_states[test_to_run];
1846 fprintf(stderr, "[%d]: #%d:%s running.\n",
1851 run_one_test(test_to_run);
1853 memset(&msg, 0, sizeof(msg));
1854 msg.type = MSG_TEST_DONE;
1855 msg.test_done.num = test_to_run;
1856 msg.test_done.error_cnt = state->error_cnt;
1857 msg.test_done.skip_cnt = state->skip_cnt;
1858 msg.test_done.sub_succ_cnt = state->sub_succ_cnt;
1859 msg.test_done.subtest_num = state->subtest_num;
1860 msg.test_done.have_log = false;
1862 if (verbose() || state->force_log || state->error_cnt) {
1864 msg.test_done.have_log = true;
1866 if (send_message(sock, &msg) < 0) {
1867 perror("Fail to send message done");
1872 if (msg.test_done.have_log)
1873 worker_main_send_log(sock, state->log_buf, state->log_cnt);
1875 if (state->log_buf) {
1876 free(state->log_buf);
1877 state->log_buf = NULL;
1881 if (state->subtest_num)
1882 if (worker_main_send_subtests(sock, state))
1886 fprintf(stderr, "[%d]: #%d:%s done.\n",
1891 } /* case MSG_DO_TEST */
1894 fprintf(stderr, "[%d]: unknown message.\n", env.worker_id);
1902 static void free_test_states(void)
1906 for (i = 0; i < ARRAY_SIZE(prog_test_defs); i++) {
1907 struct test_state *test_state = &test_states[i];
1909 for (j = 0; j < test_state->subtest_num; j++)
1910 free_subtest_state(&test_state->subtest_states[j]);
1912 free(test_state->subtest_states);
1913 free(test_state->log_buf);
1914 test_state->subtest_states = NULL;
1915 test_state->log_buf = NULL;
1919 int main(int argc, char **argv)
1921 static const struct argp argp = {
1923 .parser = parse_arg,
1924 .doc = argp_program_doc,
1926 struct sigaction sigact = {
1927 .sa_handler = crash_handler,
1928 .sa_flags = SA_RESETHAND,
1932 sigaction(SIGSEGV, &sigact, NULL);
1934 env.secs_till_notify = 10;
1935 env.secs_till_kill = 120;
1936 err = argp_parse(&argp, argc, argv, 0, NULL, &env);
1940 err = cd_flavor_subdir(argv[0]);
1946 /* Use libbpf 1.0 API mode */
1947 libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
1948 libbpf_set_print(libbpf_print_fn);
1952 env.jit_enabled = is_jit_enabled();
1953 env.nr_cpus = libbpf_num_possible_cpus();
1954 if (env.nr_cpus < 0) {
1955 fprintf(stderr, "Failed to get number of CPUs: %d!\n",
1960 env.stdout_saved = stdout;
1961 env.stderr_saved = stderr;
1963 env.has_testmod = true;
1964 if (!env.list_test_names) {
1965 /* ensure previous instance of the module is unloaded */
1966 unload_bpf_testmod(verbose());
1968 if (load_bpf_testmod(verbose())) {
1969 fprintf(env.stderr_saved, "WARNING! Selftests relying on bpf_testmod.ko will be skipped.\n");
1970 env.has_testmod = false;
1974 /* initializing tests */
1975 for (i = 0; i < prog_test_cnt; i++) {
1976 struct prog_test_def *test = &prog_test_defs[i];
1978 test->test_num = i + 1;
1979 test->should_run = should_run(&env.test_selector,
1980 test->test_num, test->test_name);
1982 if ((test->run_test == NULL && test->run_serial_test == NULL) ||
1983 (test->run_test != NULL && test->run_serial_test != NULL)) {
1984 fprintf(stderr, "Test %d:%s must have either test_%s() or serial_test_%sl() defined.\n",
1985 test->test_num, test->test_name, test->test_name, test->test_name);
1986 exit(EXIT_ERR_SETUP_INFRA);
1988 if (test->should_run)
1989 test->should_tmon = should_tmon(&env.tmon_selector, test->test_name);
1992 /* ignore workers if we are just listing */
1993 if (env.get_test_cnt || env.list_test_names)
1996 /* launch workers if requested */
1997 env.worker_id = -1; /* main process */
1999 env.worker_pids = calloc(sizeof(pid_t), env.workers);
2000 env.worker_socks = calloc(sizeof(int), env.workers);
2002 fprintf(stdout, "Launching %d workers.\n", env.workers);
2003 for (i = 0; i < env.workers; i++) {
2007 if (socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sv) < 0) {
2008 perror("Fail to create worker socket");
2013 perror("Failed to fork worker");
2015 } else if (pid != 0) { /* main process */
2017 env.worker_pids[i] = pid;
2018 env.worker_socks[i] = sv[0];
2019 } else { /* inside each worker process */
2022 return worker_main(sv[1]);
2026 if (env.worker_id == -1) {
2032 /* The rest of the main process */
2034 /* on single mode */
2037 for (i = 0; i < prog_test_cnt; i++) {
2038 struct prog_test_def *test = &prog_test_defs[i];
2040 if (!test->should_run)
2043 if (env.get_test_cnt) {
2048 if (env.list_test_names) {
2049 fprintf(env.stdout_saved, "%s\n", test->test_name);
2057 if (env.get_test_cnt) {
2058 printf("%d\n", env.succ_cnt);
2062 if (env.list_test_names)
2065 calculate_summary_and_print_errors(&env);
2067 close(env.saved_netns_fd);
2069 if (!env.list_test_names && env.has_testmod)
2070 unload_bpf_testmod(verbose());
2072 free_test_selector(&env.test_selector);
2073 free_test_selector(&env.subtest_selector);
2074 free_test_selector(&env.tmon_selector);
2077 if (env.succ_cnt + env.fail_cnt + env.skip_cnt == 0)
2078 return EXIT_NO_TEST;
2080 return env.fail_cnt ? EXIT_FAILURE : EXIT_SUCCESS;