0001
0002
0003
0004 #define _GNU_SOURCE
0005 #include "test_progs.h"
0006 #include "testing_helpers.h"
0007 #include "cgroup_helpers.h"
0008 #include <argp.h>
0009 #include <pthread.h>
0010 #include <sched.h>
0011 #include <signal.h>
0012 #include <string.h>
0013 #include <execinfo.h> /* backtrace */
0014 #include <linux/membarrier.h>
0015 #include <sys/sysinfo.h> /* get_nprocs */
0016 #include <netinet/in.h>
0017 #include <sys/select.h>
0018 #include <sys/socket.h>
0019 #include <sys/un.h>
0020
0021 static bool verbose(void)
0022 {
0023 return env.verbosity > VERBOSE_NONE;
0024 }
0025
0026 static void stdio_hijack_init(char **log_buf, size_t *log_cnt)
0027 {
0028 #ifdef __GLIBC__
0029 if (verbose() && env.worker_id == -1) {
0030
0031 return;
0032 }
0033
0034 fflush(stdout);
0035 fflush(stderr);
0036
0037 stdout = open_memstream(log_buf, log_cnt);
0038 if (!stdout) {
0039 stdout = env.stdout;
0040 perror("open_memstream");
0041 return;
0042 }
0043
0044 if (env.subtest_state)
0045 env.subtest_state->stdout = stdout;
0046 else
0047 env.test_state->stdout = stdout;
0048
0049 stderr = stdout;
0050 #endif
0051 }
0052
0053 static void stdio_hijack(char **log_buf, size_t *log_cnt)
0054 {
0055 #ifdef __GLIBC__
0056 if (verbose() && env.worker_id == -1) {
0057
0058 return;
0059 }
0060
0061 env.stdout = stdout;
0062 env.stderr = stderr;
0063
0064 stdio_hijack_init(log_buf, log_cnt);
0065 #endif
0066 }
0067
0068 static void stdio_restore_cleanup(void)
0069 {
0070 #ifdef __GLIBC__
0071 if (verbose() && env.worker_id == -1) {
0072
0073 return;
0074 }
0075
0076 fflush(stdout);
0077
0078 if (env.subtest_state) {
0079 fclose(env.subtest_state->stdout);
0080 env.subtest_state->stdout = NULL;
0081 stdout = env.test_state->stdout;
0082 stderr = env.test_state->stdout;
0083 } else {
0084 fclose(env.test_state->stdout);
0085 env.test_state->stdout = NULL;
0086 }
0087 #endif
0088 }
0089
0090 static void stdio_restore(void)
0091 {
0092 #ifdef __GLIBC__
0093 if (verbose() && env.worker_id == -1) {
0094
0095 return;
0096 }
0097
0098 if (stdout == env.stdout)
0099 return;
0100
0101 stdio_restore_cleanup();
0102
0103 stdout = env.stdout;
0104 stderr = env.stderr;
0105 #endif
0106 }
0107
0108
0109 static bool glob_match(const char *str, const char *pat)
0110 {
0111 while (*str && *pat && *pat != '*') {
0112 if (*str != *pat)
0113 return false;
0114 str++;
0115 pat++;
0116 }
0117
0118 if (*pat == '*') {
0119 while (*pat == '*')
0120 pat++;
0121 if (!*pat)
0122 return true;
0123 while (*str)
0124 if (glob_match(str++, pat))
0125 return true;
0126 }
0127 return !*str && !*pat;
0128 }
0129
0130 #define EXIT_NO_TEST 2
0131 #define EXIT_ERR_SETUP_INFRA 3
0132
0133
0134 struct test_env env = {};
0135
0136 struct prog_test_def {
0137 const char *test_name;
0138 int test_num;
0139 void (*run_test)(void);
0140 void (*run_serial_test)(void);
0141 bool should_run;
0142 bool need_cgroup_cleanup;
0143 };
0144
0145
0146
0147
0148
0149 int usleep(useconds_t usec)
0150 {
0151 struct timespec ts = {
0152 .tv_sec = usec / 1000000,
0153 .tv_nsec = (usec % 1000000) * 1000,
0154 };
0155
0156 return syscall(__NR_nanosleep, &ts, NULL);
0157 }
0158
0159 static bool should_run(struct test_selector *sel, int num, const char *name)
0160 {
0161 int i;
0162
0163 for (i = 0; i < sel->blacklist.cnt; i++) {
0164 if (glob_match(name, sel->blacklist.tests[i].name) &&
0165 !sel->blacklist.tests[i].subtest_cnt)
0166 return false;
0167 }
0168
0169 for (i = 0; i < sel->whitelist.cnt; i++) {
0170 if (glob_match(name, sel->whitelist.tests[i].name))
0171 return true;
0172 }
0173
0174 if (!sel->whitelist.cnt && !sel->num_set)
0175 return true;
0176
0177 return num < sel->num_set_len && sel->num_set[num];
0178 }
0179
0180 static bool should_run_subtest(struct test_selector *sel,
0181 struct test_selector *subtest_sel,
0182 int subtest_num,
0183 const char *test_name,
0184 const char *subtest_name)
0185 {
0186 int i, j;
0187
0188 for (i = 0; i < sel->blacklist.cnt; i++) {
0189 if (glob_match(test_name, sel->blacklist.tests[i].name)) {
0190 if (!sel->blacklist.tests[i].subtest_cnt)
0191 return false;
0192
0193 for (j = 0; j < sel->blacklist.tests[i].subtest_cnt; j++) {
0194 if (glob_match(subtest_name,
0195 sel->blacklist.tests[i].subtests[j]))
0196 return false;
0197 }
0198 }
0199 }
0200
0201 for (i = 0; i < sel->whitelist.cnt; i++) {
0202 if (glob_match(test_name, sel->whitelist.tests[i].name)) {
0203 if (!sel->whitelist.tests[i].subtest_cnt)
0204 return true;
0205
0206 for (j = 0; j < sel->whitelist.tests[i].subtest_cnt; j++) {
0207 if (glob_match(subtest_name,
0208 sel->whitelist.tests[i].subtests[j]))
0209 return true;
0210 }
0211 }
0212 }
0213
0214 if (!sel->whitelist.cnt && !subtest_sel->num_set)
0215 return true;
0216
0217 return subtest_num < subtest_sel->num_set_len && subtest_sel->num_set[subtest_num];
0218 }
0219
0220 static char *test_result(bool failed, bool skipped)
0221 {
0222 return failed ? "FAIL" : (skipped ? "SKIP" : "OK");
0223 }
0224
0225 static void print_test_log(char *log_buf, size_t log_cnt)
0226 {
0227 log_buf[log_cnt] = '\0';
0228 fprintf(env.stdout, "%s", log_buf);
0229 if (log_buf[log_cnt - 1] != '\n')
0230 fprintf(env.stdout, "\n");
0231 }
0232
0233 #define TEST_NUM_WIDTH 7
0234
0235 static void print_test_name(int test_num, const char *test_name, char *result)
0236 {
0237 fprintf(env.stdout, "#%-*d %s", TEST_NUM_WIDTH, test_num, test_name);
0238
0239 if (result)
0240 fprintf(env.stdout, ":%s", result);
0241
0242 fprintf(env.stdout, "\n");
0243 }
0244
0245 static void print_subtest_name(int test_num, int subtest_num,
0246 const char *test_name, char *subtest_name,
0247 char *result)
0248 {
0249 char test_num_str[TEST_NUM_WIDTH + 1];
0250
0251 snprintf(test_num_str, sizeof(test_num_str), "%d/%d", test_num, subtest_num);
0252
0253 fprintf(env.stdout, "#%-*s %s/%s",
0254 TEST_NUM_WIDTH, test_num_str,
0255 test_name, subtest_name);
0256
0257 if (result)
0258 fprintf(env.stdout, ":%s", result);
0259
0260 fprintf(env.stdout, "\n");
0261 }
0262
0263 static void dump_test_log(const struct prog_test_def *test,
0264 const struct test_state *test_state,
0265 bool skip_ok_subtests,
0266 bool par_exec_result)
0267 {
0268 bool test_failed = test_state->error_cnt > 0;
0269 bool force_log = test_state->force_log;
0270 bool print_test = verbose() || force_log || test_failed;
0271 int i;
0272 struct subtest_state *subtest_state;
0273 bool subtest_failed;
0274 bool subtest_filtered;
0275 bool print_subtest;
0276
0277
0278 if (env.worker_id != -1)
0279 return;
0280
0281
0282
0283
0284 if (verbose() && !par_exec_result)
0285 return;
0286
0287 if (test_state->log_cnt && print_test)
0288 print_test_log(test_state->log_buf, test_state->log_cnt);
0289
0290 for (i = 0; i < test_state->subtest_num; i++) {
0291 subtest_state = &test_state->subtest_states[i];
0292 subtest_failed = subtest_state->error_cnt;
0293 subtest_filtered = subtest_state->filtered;
0294 print_subtest = verbose() || force_log || subtest_failed;
0295
0296 if ((skip_ok_subtests && !subtest_failed) || subtest_filtered)
0297 continue;
0298
0299 if (subtest_state->log_cnt && print_subtest) {
0300 print_test_log(subtest_state->log_buf,
0301 subtest_state->log_cnt);
0302 }
0303
0304 print_subtest_name(test->test_num, i + 1,
0305 test->test_name, subtest_state->name,
0306 test_result(subtest_state->error_cnt,
0307 subtest_state->skipped));
0308 }
0309
0310 print_test_name(test->test_num, test->test_name,
0311 test_result(test_failed, test_state->skip_cnt));
0312 }
0313
0314 static void stdio_restore(void);
0315
0316
0317
0318
0319 static void reset_affinity(void)
0320 {
0321 cpu_set_t cpuset;
0322 int i, err;
0323
0324 CPU_ZERO(&cpuset);
0325 for (i = 0; i < env.nr_cpus; i++)
0326 CPU_SET(i, &cpuset);
0327
0328 err = sched_setaffinity(0, sizeof(cpuset), &cpuset);
0329 if (err < 0) {
0330 stdio_restore();
0331 fprintf(stderr, "Failed to reset process affinity: %d!\n", err);
0332 exit(EXIT_ERR_SETUP_INFRA);
0333 }
0334 err = pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);
0335 if (err < 0) {
0336 stdio_restore();
0337 fprintf(stderr, "Failed to reset thread affinity: %d!\n", err);
0338 exit(EXIT_ERR_SETUP_INFRA);
0339 }
0340 }
0341
0342 static void save_netns(void)
0343 {
0344 env.saved_netns_fd = open("/proc/self/ns/net", O_RDONLY);
0345 if (env.saved_netns_fd == -1) {
0346 perror("open(/proc/self/ns/net)");
0347 exit(EXIT_ERR_SETUP_INFRA);
0348 }
0349 }
0350
0351 static void restore_netns(void)
0352 {
0353 if (setns(env.saved_netns_fd, CLONE_NEWNET) == -1) {
0354 stdio_restore();
0355 perror("setns(CLONE_NEWNS)");
0356 exit(EXIT_ERR_SETUP_INFRA);
0357 }
0358 }
0359
0360 void test__end_subtest(void)
0361 {
0362 struct prog_test_def *test = env.test;
0363 struct test_state *test_state = env.test_state;
0364 struct subtest_state *subtest_state = env.subtest_state;
0365
0366 if (subtest_state->error_cnt) {
0367 test_state->error_cnt++;
0368 } else {
0369 if (!subtest_state->skipped)
0370 test_state->sub_succ_cnt++;
0371 else
0372 test_state->skip_cnt++;
0373 }
0374
0375 if (verbose() && !env.workers)
0376 print_subtest_name(test->test_num, test_state->subtest_num,
0377 test->test_name, subtest_state->name,
0378 test_result(subtest_state->error_cnt,
0379 subtest_state->skipped));
0380
0381 stdio_restore_cleanup();
0382 env.subtest_state = NULL;
0383 }
0384
0385 bool test__start_subtest(const char *subtest_name)
0386 {
0387 struct prog_test_def *test = env.test;
0388 struct test_state *state = env.test_state;
0389 struct subtest_state *subtest_state;
0390 size_t sub_state_size = sizeof(*subtest_state);
0391
0392 if (env.subtest_state)
0393 test__end_subtest();
0394
0395 state->subtest_num++;
0396 state->subtest_states =
0397 realloc(state->subtest_states,
0398 state->subtest_num * sub_state_size);
0399 if (!state->subtest_states) {
0400 fprintf(stderr, "Not enough memory to allocate subtest result\n");
0401 return false;
0402 }
0403
0404 subtest_state = &state->subtest_states[state->subtest_num - 1];
0405
0406 memset(subtest_state, 0, sub_state_size);
0407
0408 if (!subtest_name || !subtest_name[0]) {
0409 fprintf(env.stderr,
0410 "Subtest #%d didn't provide sub-test name!\n",
0411 state->subtest_num);
0412 return false;
0413 }
0414
0415 subtest_state->name = strdup(subtest_name);
0416 if (!subtest_state->name) {
0417 fprintf(env.stderr,
0418 "Subtest #%d: failed to copy subtest name!\n",
0419 state->subtest_num);
0420 return false;
0421 }
0422
0423 if (!should_run_subtest(&env.test_selector,
0424 &env.subtest_selector,
0425 state->subtest_num,
0426 test->test_name,
0427 subtest_name)) {
0428 subtest_state->filtered = true;
0429 return false;
0430 }
0431
0432 env.subtest_state = subtest_state;
0433 stdio_hijack_init(&subtest_state->log_buf, &subtest_state->log_cnt);
0434
0435 return true;
0436 }
0437
0438 void test__force_log(void)
0439 {
0440 env.test_state->force_log = true;
0441 }
0442
0443 void test__skip(void)
0444 {
0445 if (env.subtest_state)
0446 env.subtest_state->skipped = true;
0447 else
0448 env.test_state->skip_cnt++;
0449 }
0450
0451 void test__fail(void)
0452 {
0453 if (env.subtest_state)
0454 env.subtest_state->error_cnt++;
0455 else
0456 env.test_state->error_cnt++;
0457 }
0458
0459 int test__join_cgroup(const char *path)
0460 {
0461 int fd;
0462
0463 if (!env.test->need_cgroup_cleanup) {
0464 if (setup_cgroup_environment()) {
0465 fprintf(stderr,
0466 "#%d %s: Failed to setup cgroup environment\n",
0467 env.test->test_num, env.test->test_name);
0468 return -1;
0469 }
0470
0471 env.test->need_cgroup_cleanup = true;
0472 }
0473
0474 fd = create_and_get_cgroup(path);
0475 if (fd < 0) {
0476 fprintf(stderr,
0477 "#%d %s: Failed to create cgroup '%s' (errno=%d)\n",
0478 env.test->test_num, env.test->test_name, path, errno);
0479 return fd;
0480 }
0481
0482 if (join_cgroup(path)) {
0483 fprintf(stderr,
0484 "#%d %s: Failed to join cgroup '%s' (errno=%d)\n",
0485 env.test->test_num, env.test->test_name, path, errno);
0486 return -1;
0487 }
0488
0489 return fd;
0490 }
0491
0492 int bpf_find_map(const char *test, struct bpf_object *obj, const char *name)
0493 {
0494 struct bpf_map *map;
0495
0496 map = bpf_object__find_map_by_name(obj, name);
0497 if (!map) {
0498 fprintf(stdout, "%s:FAIL:map '%s' not found\n", test, name);
0499 test__fail();
0500 return -1;
0501 }
0502 return bpf_map__fd(map);
0503 }
0504
0505 static bool is_jit_enabled(void)
0506 {
0507 const char *jit_sysctl = "/proc/sys/net/core/bpf_jit_enable";
0508 bool enabled = false;
0509 int sysctl_fd;
0510
0511 sysctl_fd = open(jit_sysctl, 0, O_RDONLY);
0512 if (sysctl_fd != -1) {
0513 char tmpc;
0514
0515 if (read(sysctl_fd, &tmpc, sizeof(tmpc)) == 1)
0516 enabled = (tmpc != '0');
0517 close(sysctl_fd);
0518 }
0519
0520 return enabled;
0521 }
0522
0523 int compare_map_keys(int map1_fd, int map2_fd)
0524 {
0525 __u32 key, next_key;
0526 char val_buf[PERF_MAX_STACK_DEPTH *
0527 sizeof(struct bpf_stack_build_id)];
0528 int err;
0529
0530 err = bpf_map_get_next_key(map1_fd, NULL, &key);
0531 if (err)
0532 return err;
0533 err = bpf_map_lookup_elem(map2_fd, &key, val_buf);
0534 if (err)
0535 return err;
0536
0537 while (bpf_map_get_next_key(map1_fd, &key, &next_key) == 0) {
0538 err = bpf_map_lookup_elem(map2_fd, &next_key, val_buf);
0539 if (err)
0540 return err;
0541
0542 key = next_key;
0543 }
0544 if (errno != ENOENT)
0545 return -1;
0546
0547 return 0;
0548 }
0549
0550 int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len)
0551 {
0552 __u32 key, next_key, *cur_key_p, *next_key_p;
0553 char *val_buf1, *val_buf2;
0554 int i, err = 0;
0555
0556 val_buf1 = malloc(stack_trace_len);
0557 val_buf2 = malloc(stack_trace_len);
0558 cur_key_p = NULL;
0559 next_key_p = &key;
0560 while (bpf_map_get_next_key(smap_fd, cur_key_p, next_key_p) == 0) {
0561 err = bpf_map_lookup_elem(smap_fd, next_key_p, val_buf1);
0562 if (err)
0563 goto out;
0564 err = bpf_map_lookup_elem(amap_fd, next_key_p, val_buf2);
0565 if (err)
0566 goto out;
0567 for (i = 0; i < stack_trace_len; i++) {
0568 if (val_buf1[i] != val_buf2[i]) {
0569 err = -1;
0570 goto out;
0571 }
0572 }
0573 key = *next_key_p;
0574 cur_key_p = &key;
0575 next_key_p = &next_key;
0576 }
0577 if (errno != ENOENT)
0578 err = -1;
0579
0580 out:
0581 free(val_buf1);
0582 free(val_buf2);
0583 return err;
0584 }
0585
0586 int extract_build_id(char *build_id, size_t size)
0587 {
0588 FILE *fp;
0589 char *line = NULL;
0590 size_t len = 0;
0591
0592 fp = popen("readelf -n ./urandom_read | grep 'Build ID'", "r");
0593 if (fp == NULL)
0594 return -1;
0595
0596 if (getline(&line, &len, fp) == -1)
0597 goto err;
0598 pclose(fp);
0599
0600 if (len > size)
0601 len = size;
0602 memcpy(build_id, line, len);
0603 build_id[len] = '\0';
0604 free(line);
0605 return 0;
0606 err:
0607 pclose(fp);
0608 return -1;
0609 }
0610
0611 static int finit_module(int fd, const char *param_values, int flags)
0612 {
0613 return syscall(__NR_finit_module, fd, param_values, flags);
0614 }
0615
0616 static int delete_module(const char *name, int flags)
0617 {
0618 return syscall(__NR_delete_module, name, flags);
0619 }
0620
0621
0622
0623
0624 int kern_sync_rcu(void)
0625 {
0626 return syscall(__NR_membarrier, MEMBARRIER_CMD_SHARED, 0, 0);
0627 }
0628
0629 static void unload_bpf_testmod(void)
0630 {
0631 if (kern_sync_rcu())
0632 fprintf(env.stderr, "Failed to trigger kernel-side RCU sync!\n");
0633 if (delete_module("bpf_testmod", 0)) {
0634 if (errno == ENOENT) {
0635 if (verbose())
0636 fprintf(stdout, "bpf_testmod.ko is already unloaded.\n");
0637 return;
0638 }
0639 fprintf(env.stderr, "Failed to unload bpf_testmod.ko from kernel: %d\n", -errno);
0640 return;
0641 }
0642 if (verbose())
0643 fprintf(stdout, "Successfully unloaded bpf_testmod.ko.\n");
0644 }
0645
0646 static int load_bpf_testmod(void)
0647 {
0648 int fd;
0649
0650
0651 unload_bpf_testmod();
0652
0653 if (verbose())
0654 fprintf(stdout, "Loading bpf_testmod.ko...\n");
0655
0656 fd = open("bpf_testmod.ko", O_RDONLY);
0657 if (fd < 0) {
0658 fprintf(env.stderr, "Can't find bpf_testmod.ko kernel module: %d\n", -errno);
0659 return -ENOENT;
0660 }
0661 if (finit_module(fd, "", 0)) {
0662 fprintf(env.stderr, "Failed to load bpf_testmod.ko into the kernel: %d\n", -errno);
0663 close(fd);
0664 return -EINVAL;
0665 }
0666 close(fd);
0667
0668 if (verbose())
0669 fprintf(stdout, "Successfully loaded bpf_testmod.ko.\n");
0670 return 0;
0671 }
0672
0673
0674 #define DEFINE_TEST(name) \
0675 extern void test_##name(void) __weak; \
0676 extern void serial_test_##name(void) __weak;
0677 #include <prog_tests/tests.h>
0678 #undef DEFINE_TEST
0679
0680 static struct prog_test_def prog_test_defs[] = {
0681 #define DEFINE_TEST(name) { \
0682 .test_name = #name, \
0683 .run_test = &test_##name, \
0684 .run_serial_test = &serial_test_##name, \
0685 },
0686 #include <prog_tests/tests.h>
0687 #undef DEFINE_TEST
0688 };
0689
0690 static const int prog_test_cnt = ARRAY_SIZE(prog_test_defs);
0691
0692 static struct test_state test_states[ARRAY_SIZE(prog_test_defs)];
0693
0694 const char *argp_program_version = "test_progs 0.1";
0695 const char *argp_program_bug_address = "<bpf@vger.kernel.org>";
0696 static const char argp_program_doc[] = "BPF selftests test runner";
0697
0698 enum ARG_KEYS {
0699 ARG_TEST_NUM = 'n',
0700 ARG_TEST_NAME = 't',
0701 ARG_TEST_NAME_BLACKLIST = 'b',
0702 ARG_VERIFIER_STATS = 's',
0703 ARG_VERBOSE = 'v',
0704 ARG_GET_TEST_CNT = 'c',
0705 ARG_LIST_TEST_NAMES = 'l',
0706 ARG_TEST_NAME_GLOB_ALLOWLIST = 'a',
0707 ARG_TEST_NAME_GLOB_DENYLIST = 'd',
0708 ARG_NUM_WORKERS = 'j',
0709 ARG_DEBUG = -1,
0710 };
0711
0712 static const struct argp_option opts[] = {
0713 { "num", ARG_TEST_NUM, "NUM", 0,
0714 "Run test number NUM only " },
0715 { "name", ARG_TEST_NAME, "NAMES", 0,
0716 "Run tests with names containing any string from NAMES list" },
0717 { "name-blacklist", ARG_TEST_NAME_BLACKLIST, "NAMES", 0,
0718 "Don't run tests with names containing any string from NAMES list" },
0719 { "verifier-stats", ARG_VERIFIER_STATS, NULL, 0,
0720 "Output verifier statistics", },
0721 { "verbose", ARG_VERBOSE, "LEVEL", OPTION_ARG_OPTIONAL,
0722 "Verbose output (use -vv or -vvv for progressively verbose output)" },
0723 { "count", ARG_GET_TEST_CNT, NULL, 0,
0724 "Get number of selected top-level tests " },
0725 { "list", ARG_LIST_TEST_NAMES, NULL, 0,
0726 "List test names that would run (without running them) " },
0727 { "allow", ARG_TEST_NAME_GLOB_ALLOWLIST, "NAMES", 0,
0728 "Run tests with name matching the pattern (supports '*' wildcard)." },
0729 { "deny", ARG_TEST_NAME_GLOB_DENYLIST, "NAMES", 0,
0730 "Don't run tests with name matching the pattern (supports '*' wildcard)." },
0731 { "workers", ARG_NUM_WORKERS, "WORKERS", OPTION_ARG_OPTIONAL,
0732 "Number of workers to run in parallel, default to number of cpus." },
0733 { "debug", ARG_DEBUG, NULL, 0,
0734 "print extra debug information for test_progs." },
0735 {},
0736 };
0737
0738 static int libbpf_print_fn(enum libbpf_print_level level,
0739 const char *format, va_list args)
0740 {
0741 if (env.verbosity < VERBOSE_VERY && level == LIBBPF_DEBUG)
0742 return 0;
0743 vfprintf(stdout, format, args);
0744 return 0;
0745 }
0746
0747 static void free_test_filter_set(const struct test_filter_set *set)
0748 {
0749 int i, j;
0750
0751 if (!set)
0752 return;
0753
0754 for (i = 0; i < set->cnt; i++) {
0755 free((void *)set->tests[i].name);
0756 for (j = 0; j < set->tests[i].subtest_cnt; j++)
0757 free((void *)set->tests[i].subtests[j]);
0758
0759 free((void *)set->tests[i].subtests);
0760 }
0761
0762 free((void *)set->tests);
0763 }
0764
0765 static void free_test_selector(struct test_selector *test_selector)
0766 {
0767 free_test_filter_set(&test_selector->blacklist);
0768 free_test_filter_set(&test_selector->whitelist);
0769 free(test_selector->num_set);
0770 }
0771
0772 extern int extra_prog_load_log_flags;
0773
0774 static error_t parse_arg(int key, char *arg, struct argp_state *state)
0775 {
0776 struct test_env *env = state->input;
0777
0778 switch (key) {
0779 case ARG_TEST_NUM: {
0780 char *subtest_str = strchr(arg, '/');
0781
0782 if (subtest_str) {
0783 *subtest_str = '\0';
0784 if (parse_num_list(subtest_str + 1,
0785 &env->subtest_selector.num_set,
0786 &env->subtest_selector.num_set_len)) {
0787 fprintf(stderr,
0788 "Failed to parse subtest numbers.\n");
0789 return -EINVAL;
0790 }
0791 }
0792 if (parse_num_list(arg, &env->test_selector.num_set,
0793 &env->test_selector.num_set_len)) {
0794 fprintf(stderr, "Failed to parse test numbers.\n");
0795 return -EINVAL;
0796 }
0797 break;
0798 }
0799 case ARG_TEST_NAME_GLOB_ALLOWLIST:
0800 case ARG_TEST_NAME: {
0801 if (parse_test_list(arg,
0802 &env->test_selector.whitelist,
0803 key == ARG_TEST_NAME_GLOB_ALLOWLIST))
0804 return -ENOMEM;
0805 break;
0806 }
0807 case ARG_TEST_NAME_GLOB_DENYLIST:
0808 case ARG_TEST_NAME_BLACKLIST: {
0809 if (parse_test_list(arg,
0810 &env->test_selector.blacklist,
0811 key == ARG_TEST_NAME_GLOB_DENYLIST))
0812 return -ENOMEM;
0813 break;
0814 }
0815 case ARG_VERIFIER_STATS:
0816 env->verifier_stats = true;
0817 break;
0818 case ARG_VERBOSE:
0819 env->verbosity = VERBOSE_NORMAL;
0820 if (arg) {
0821 if (strcmp(arg, "v") == 0) {
0822 env->verbosity = VERBOSE_VERY;
0823 extra_prog_load_log_flags = 1;
0824 } else if (strcmp(arg, "vv") == 0) {
0825 env->verbosity = VERBOSE_SUPER;
0826 extra_prog_load_log_flags = 2;
0827 } else {
0828 fprintf(stderr,
0829 "Unrecognized verbosity setting ('%s'), only -v and -vv are supported\n",
0830 arg);
0831 return -EINVAL;
0832 }
0833 }
0834
0835 if (verbose()) {
0836 if (setenv("SELFTESTS_VERBOSE", "1", 1) == -1) {
0837 fprintf(stderr,
0838 "Unable to setenv SELFTESTS_VERBOSE=1 (errno=%d)",
0839 errno);
0840 return -EINVAL;
0841 }
0842 }
0843
0844 break;
0845 case ARG_GET_TEST_CNT:
0846 env->get_test_cnt = true;
0847 break;
0848 case ARG_LIST_TEST_NAMES:
0849 env->list_test_names = true;
0850 break;
0851 case ARG_NUM_WORKERS:
0852 if (arg) {
0853 env->workers = atoi(arg);
0854 if (!env->workers) {
0855 fprintf(stderr, "Invalid number of worker: %s.", arg);
0856 return -EINVAL;
0857 }
0858 } else {
0859 env->workers = get_nprocs();
0860 }
0861 break;
0862 case ARG_DEBUG:
0863 env->debug = true;
0864 break;
0865 case ARGP_KEY_ARG:
0866 argp_usage(state);
0867 break;
0868 case ARGP_KEY_END:
0869 break;
0870 default:
0871 return ARGP_ERR_UNKNOWN;
0872 }
0873 return 0;
0874 }
0875
0876
0877
0878
0879
0880
0881
0882
0883 int cd_flavor_subdir(const char *exec_name)
0884 {
0885
0886
0887
0888
0889
0890 const char *flavor = strrchr(exec_name, '/');
0891
0892 if (!flavor)
0893 flavor = exec_name;
0894 else
0895 flavor++;
0896
0897 flavor = strrchr(flavor, '-');
0898 if (!flavor)
0899 return 0;
0900 flavor++;
0901 if (verbose())
0902 fprintf(stdout, "Switching to flavor '%s' subdirectory...\n", flavor);
0903
0904 return chdir(flavor);
0905 }
0906
0907 int trigger_module_test_read(int read_sz)
0908 {
0909 int fd, err;
0910
0911 fd = open(BPF_TESTMOD_TEST_FILE, O_RDONLY);
0912 err = -errno;
0913 if (!ASSERT_GE(fd, 0, "testmod_file_open"))
0914 return err;
0915
0916 read(fd, NULL, read_sz);
0917 close(fd);
0918
0919 return 0;
0920 }
0921
0922 int trigger_module_test_write(int write_sz)
0923 {
0924 int fd, err;
0925 char *buf = malloc(write_sz);
0926
0927 if (!buf)
0928 return -ENOMEM;
0929
0930 memset(buf, 'a', write_sz);
0931 buf[write_sz-1] = '\0';
0932
0933 fd = open(BPF_TESTMOD_TEST_FILE, O_WRONLY);
0934 err = -errno;
0935 if (!ASSERT_GE(fd, 0, "testmod_file_open")) {
0936 free(buf);
0937 return err;
0938 }
0939
0940 write(fd, buf, write_sz);
0941 close(fd);
0942 free(buf);
0943 return 0;
0944 }
0945
0946 #define MAX_BACKTRACE_SZ 128
0947 void crash_handler(int signum)
0948 {
0949 void *bt[MAX_BACKTRACE_SZ];
0950 size_t sz;
0951
0952 sz = backtrace(bt, ARRAY_SIZE(bt));
0953
0954 if (env.test) {
0955 env.test_state->error_cnt++;
0956 dump_test_log(env.test, env.test_state, true, false);
0957 }
0958 if (env.stdout)
0959 stdio_restore();
0960 if (env.worker_id != -1)
0961 fprintf(stderr, "[%d]: ", env.worker_id);
0962 fprintf(stderr, "Caught signal #%d!\nStack trace:\n", signum);
0963 backtrace_symbols_fd(bt, sz, STDERR_FILENO);
0964 }
0965
0966 static void sigint_handler(int signum)
0967 {
0968 int i;
0969
0970 for (i = 0; i < env.workers; i++)
0971 if (env.worker_socks[i] > 0)
0972 close(env.worker_socks[i]);
0973 }
0974
0975 static int current_test_idx;
0976 static pthread_mutex_t current_test_lock;
0977 static pthread_mutex_t stdout_output_lock;
0978
0979 static inline const char *str_msg(const struct msg *msg, char *buf)
0980 {
0981 switch (msg->type) {
0982 case MSG_DO_TEST:
0983 sprintf(buf, "MSG_DO_TEST %d", msg->do_test.num);
0984 break;
0985 case MSG_TEST_DONE:
0986 sprintf(buf, "MSG_TEST_DONE %d (log: %d)",
0987 msg->test_done.num,
0988 msg->test_done.have_log);
0989 break;
0990 case MSG_SUBTEST_DONE:
0991 sprintf(buf, "MSG_SUBTEST_DONE %d (log: %d)",
0992 msg->subtest_done.num,
0993 msg->subtest_done.have_log);
0994 break;
0995 case MSG_TEST_LOG:
0996 sprintf(buf, "MSG_TEST_LOG (cnt: %ld, last: %d)",
0997 strlen(msg->test_log.log_buf),
0998 msg->test_log.is_last);
0999 break;
1000 case MSG_EXIT:
1001 sprintf(buf, "MSG_EXIT");
1002 break;
1003 default:
1004 sprintf(buf, "UNKNOWN");
1005 break;
1006 }
1007
1008 return buf;
1009 }
1010
1011 static int send_message(int sock, const struct msg *msg)
1012 {
1013 char buf[256];
1014
1015 if (env.debug)
1016 fprintf(stderr, "Sending msg: %s\n", str_msg(msg, buf));
1017 return send(sock, msg, sizeof(*msg), 0);
1018 }
1019
1020 static int recv_message(int sock, struct msg *msg)
1021 {
1022 int ret;
1023 char buf[256];
1024
1025 memset(msg, 0, sizeof(*msg));
1026 ret = recv(sock, msg, sizeof(*msg), 0);
1027 if (ret >= 0) {
1028 if (env.debug)
1029 fprintf(stderr, "Received msg: %s\n", str_msg(msg, buf));
1030 }
1031 return ret;
1032 }
1033
1034 static void run_one_test(int test_num)
1035 {
1036 struct prog_test_def *test = &prog_test_defs[test_num];
1037 struct test_state *state = &test_states[test_num];
1038
1039 env.test = test;
1040 env.test_state = state;
1041
1042 stdio_hijack(&state->log_buf, &state->log_cnt);
1043
1044 if (test->run_test)
1045 test->run_test();
1046 else if (test->run_serial_test)
1047 test->run_serial_test();
1048
1049
1050 if (env.subtest_state)
1051 test__end_subtest();
1052
1053 state->tested = true;
1054
1055 if (verbose() && env.worker_id == -1)
1056 print_test_name(test_num + 1, test->test_name,
1057 test_result(state->error_cnt, state->skip_cnt));
1058
1059 reset_affinity();
1060 restore_netns();
1061 if (test->need_cgroup_cleanup)
1062 cleanup_cgroup_environment();
1063
1064 stdio_restore();
1065
1066 dump_test_log(test, state, false, false);
1067 }
1068
1069 struct dispatch_data {
1070 int worker_id;
1071 int sock_fd;
1072 };
1073
1074 static int read_prog_test_msg(int sock_fd, struct msg *msg, enum msg_type type)
1075 {
1076 if (recv_message(sock_fd, msg) < 0)
1077 return 1;
1078
1079 if (msg->type != type) {
1080 printf("%s: unexpected message type %d. expected %d\n", __func__, msg->type, type);
1081 return 1;
1082 }
1083
1084 return 0;
1085 }
1086
1087 static int dispatch_thread_read_log(int sock_fd, char **log_buf, size_t *log_cnt)
1088 {
1089 FILE *log_fp = NULL;
1090 int result = 0;
1091
1092 log_fp = open_memstream(log_buf, log_cnt);
1093 if (!log_fp)
1094 return 1;
1095
1096 while (true) {
1097 struct msg msg;
1098
1099 if (read_prog_test_msg(sock_fd, &msg, MSG_TEST_LOG)) {
1100 result = 1;
1101 goto out;
1102 }
1103
1104 fprintf(log_fp, "%s", msg.test_log.log_buf);
1105 if (msg.test_log.is_last)
1106 break;
1107 }
1108
1109 out:
1110 fclose(log_fp);
1111 log_fp = NULL;
1112 return result;
1113 }
1114
1115 static int dispatch_thread_send_subtests(int sock_fd, struct test_state *state)
1116 {
1117 struct msg msg;
1118 struct subtest_state *subtest_state;
1119 int subtest_num = state->subtest_num;
1120
1121 state->subtest_states = malloc(subtest_num * sizeof(*subtest_state));
1122
1123 for (int i = 0; i < subtest_num; i++) {
1124 subtest_state = &state->subtest_states[i];
1125
1126 memset(subtest_state, 0, sizeof(*subtest_state));
1127
1128 if (read_prog_test_msg(sock_fd, &msg, MSG_SUBTEST_DONE))
1129 return 1;
1130
1131 subtest_state->name = strdup(msg.subtest_done.name);
1132 subtest_state->error_cnt = msg.subtest_done.error_cnt;
1133 subtest_state->skipped = msg.subtest_done.skipped;
1134 subtest_state->filtered = msg.subtest_done.filtered;
1135
1136
1137 if (msg.subtest_done.have_log)
1138 if (dispatch_thread_read_log(sock_fd,
1139 &subtest_state->log_buf,
1140 &subtest_state->log_cnt))
1141 return 1;
1142 }
1143
1144 return 0;
1145 }
1146
1147 static void *dispatch_thread(void *ctx)
1148 {
1149 struct dispatch_data *data = ctx;
1150 int sock_fd;
1151
1152 sock_fd = data->sock_fd;
1153
1154 while (true) {
1155 int test_to_run = -1;
1156 struct prog_test_def *test;
1157 struct test_state *state;
1158
1159
1160 {
1161 pthread_mutex_lock(¤t_test_lock);
1162
1163 if (current_test_idx >= prog_test_cnt) {
1164 pthread_mutex_unlock(¤t_test_lock);
1165 goto done;
1166 }
1167
1168 test = &prog_test_defs[current_test_idx];
1169 test_to_run = current_test_idx;
1170 current_test_idx++;
1171
1172 pthread_mutex_unlock(¤t_test_lock);
1173 }
1174
1175 if (!test->should_run || test->run_serial_test)
1176 continue;
1177
1178
1179 {
1180 struct msg msg_do_test;
1181
1182 memset(&msg_do_test, 0, sizeof(msg_do_test));
1183 msg_do_test.type = MSG_DO_TEST;
1184 msg_do_test.do_test.num = test_to_run;
1185 if (send_message(sock_fd, &msg_do_test) < 0) {
1186 perror("Fail to send command");
1187 goto done;
1188 }
1189 env.worker_current_test[data->worker_id] = test_to_run;
1190 }
1191
1192
1193 do {
1194 struct msg msg;
1195
1196 if (read_prog_test_msg(sock_fd, &msg, MSG_TEST_DONE))
1197 goto error;
1198 if (test_to_run != msg.test_done.num)
1199 goto error;
1200
1201 state = &test_states[test_to_run];
1202 state->tested = true;
1203 state->error_cnt = msg.test_done.error_cnt;
1204 state->skip_cnt = msg.test_done.skip_cnt;
1205 state->sub_succ_cnt = msg.test_done.sub_succ_cnt;
1206 state->subtest_num = msg.test_done.subtest_num;
1207
1208
1209 if (msg.test_done.have_log) {
1210 if (dispatch_thread_read_log(sock_fd,
1211 &state->log_buf,
1212 &state->log_cnt))
1213 goto error;
1214 }
1215
1216
1217 if (!state->subtest_num)
1218 break;
1219
1220 if (dispatch_thread_send_subtests(sock_fd, state))
1221 goto error;
1222 } while (false);
1223
1224 pthread_mutex_lock(&stdout_output_lock);
1225 dump_test_log(test, state, false, true);
1226 pthread_mutex_unlock(&stdout_output_lock);
1227 }
1228 error:
1229 if (env.debug)
1230 fprintf(stderr, "[%d]: Protocol/IO error: %s.\n", data->worker_id, strerror(errno));
1231
1232 done:
1233 {
1234 struct msg msg_exit;
1235
1236 msg_exit.type = MSG_EXIT;
1237 if (send_message(sock_fd, &msg_exit) < 0) {
1238 if (env.debug)
1239 fprintf(stderr, "[%d]: send_message msg_exit: %s.\n",
1240 data->worker_id, strerror(errno));
1241 }
1242 }
1243 return NULL;
1244 }
1245
1246 static void calculate_summary_and_print_errors(struct test_env *env)
1247 {
1248 int i;
1249 int succ_cnt = 0, fail_cnt = 0, sub_succ_cnt = 0, skip_cnt = 0;
1250
1251 for (i = 0; i < prog_test_cnt; i++) {
1252 struct test_state *state = &test_states[i];
1253
1254 if (!state->tested)
1255 continue;
1256
1257 sub_succ_cnt += state->sub_succ_cnt;
1258 skip_cnt += state->skip_cnt;
1259
1260 if (state->error_cnt)
1261 fail_cnt++;
1262 else
1263 succ_cnt++;
1264 }
1265
1266
1267
1268
1269
1270
1271 if (!verbose() && fail_cnt) {
1272 printf("\nAll error logs:\n");
1273
1274
1275 for (i = 0; i < prog_test_cnt; i++) {
1276 struct prog_test_def *test = &prog_test_defs[i];
1277 struct test_state *state = &test_states[i];
1278
1279 if (!state->tested || !state->error_cnt)
1280 continue;
1281
1282 dump_test_log(test, state, true, true);
1283 }
1284 }
1285
1286 printf("Summary: %d/%d PASSED, %d SKIPPED, %d FAILED\n",
1287 succ_cnt, sub_succ_cnt, skip_cnt, fail_cnt);
1288
1289 env->succ_cnt = succ_cnt;
1290 env->sub_succ_cnt = sub_succ_cnt;
1291 env->fail_cnt = fail_cnt;
1292 env->skip_cnt = skip_cnt;
1293 }
1294
1295 static void server_main(void)
1296 {
1297 pthread_t *dispatcher_threads;
1298 struct dispatch_data *data;
1299 struct sigaction sigact_int = {
1300 .sa_handler = sigint_handler,
1301 .sa_flags = SA_RESETHAND,
1302 };
1303 int i;
1304
1305 sigaction(SIGINT, &sigact_int, NULL);
1306
1307 dispatcher_threads = calloc(sizeof(pthread_t), env.workers);
1308 data = calloc(sizeof(struct dispatch_data), env.workers);
1309
1310 env.worker_current_test = calloc(sizeof(int), env.workers);
1311 for (i = 0; i < env.workers; i++) {
1312 int rc;
1313
1314 data[i].worker_id = i;
1315 data[i].sock_fd = env.worker_socks[i];
1316 rc = pthread_create(&dispatcher_threads[i], NULL, dispatch_thread, &data[i]);
1317 if (rc < 0) {
1318 perror("Failed to launch dispatcher thread");
1319 exit(EXIT_ERR_SETUP_INFRA);
1320 }
1321 }
1322
1323
1324 for (i = 0; i < env.workers; i++) {
1325 while (true) {
1326 int ret = pthread_tryjoin_np(dispatcher_threads[i], NULL);
1327
1328 if (!ret) {
1329 break;
1330 } else if (ret == EBUSY) {
1331 if (env.debug)
1332 fprintf(stderr, "Still waiting for thread %d (test %d).\n",
1333 i, env.worker_current_test[i] + 1);
1334 usleep(1000 * 1000);
1335 continue;
1336 } else {
1337 fprintf(stderr, "Unexpected error joining dispatcher thread: %d", ret);
1338 break;
1339 }
1340 }
1341 }
1342 free(dispatcher_threads);
1343 free(env.worker_current_test);
1344 free(data);
1345
1346
1347 save_netns();
1348
1349 for (int i = 0; i < prog_test_cnt; i++) {
1350 struct prog_test_def *test = &prog_test_defs[i];
1351
1352 if (!test->should_run || !test->run_serial_test)
1353 continue;
1354
1355 run_one_test(i);
1356 }
1357
1358
1359 fflush(stderr);
1360 fflush(stdout);
1361
1362 calculate_summary_and_print_errors(&env);
1363
1364
1365 for (i = 0; i < env.workers; i++) {
1366 int wstatus, pid;
1367
1368 pid = waitpid(env.worker_pids[i], &wstatus, 0);
1369 if (pid != env.worker_pids[i])
1370 perror("Unable to reap worker");
1371 }
1372 }
1373
1374 static void worker_main_send_log(int sock, char *log_buf, size_t log_cnt)
1375 {
1376 char *src;
1377 size_t slen;
1378
1379 src = log_buf;
1380 slen = log_cnt;
1381 while (slen) {
1382 struct msg msg_log;
1383 char *dest;
1384 size_t len;
1385
1386 memset(&msg_log, 0, sizeof(msg_log));
1387 msg_log.type = MSG_TEST_LOG;
1388 dest = msg_log.test_log.log_buf;
1389 len = slen >= MAX_LOG_TRUNK_SIZE ? MAX_LOG_TRUNK_SIZE : slen;
1390 memcpy(dest, src, len);
1391
1392 src += len;
1393 slen -= len;
1394 if (!slen)
1395 msg_log.test_log.is_last = true;
1396
1397 assert(send_message(sock, &msg_log) >= 0);
1398 }
1399 }
1400
1401 static void free_subtest_state(struct subtest_state *state)
1402 {
1403 if (state->log_buf) {
1404 free(state->log_buf);
1405 state->log_buf = NULL;
1406 state->log_cnt = 0;
1407 }
1408 free(state->name);
1409 state->name = NULL;
1410 }
1411
1412 static int worker_main_send_subtests(int sock, struct test_state *state)
1413 {
1414 int i, result = 0;
1415 struct msg msg;
1416 struct subtest_state *subtest_state;
1417
1418 memset(&msg, 0, sizeof(msg));
1419 msg.type = MSG_SUBTEST_DONE;
1420
1421 for (i = 0; i < state->subtest_num; i++) {
1422 subtest_state = &state->subtest_states[i];
1423
1424 msg.subtest_done.num = i;
1425
1426 strncpy(msg.subtest_done.name, subtest_state->name, MAX_SUBTEST_NAME);
1427
1428 msg.subtest_done.error_cnt = subtest_state->error_cnt;
1429 msg.subtest_done.skipped = subtest_state->skipped;
1430 msg.subtest_done.filtered = subtest_state->filtered;
1431 msg.subtest_done.have_log = false;
1432
1433 if (verbose() || state->force_log || subtest_state->error_cnt) {
1434 if (subtest_state->log_cnt)
1435 msg.subtest_done.have_log = true;
1436 }
1437
1438 if (send_message(sock, &msg) < 0) {
1439 perror("Fail to send message done");
1440 result = 1;
1441 goto out;
1442 }
1443
1444
1445 if (msg.subtest_done.have_log)
1446 worker_main_send_log(sock, subtest_state->log_buf, subtest_state->log_cnt);
1447
1448 free_subtest_state(subtest_state);
1449 free(subtest_state->name);
1450 }
1451
1452 out:
1453 for (; i < state->subtest_num; i++)
1454 free_subtest_state(&state->subtest_states[i]);
1455 free(state->subtest_states);
1456 return result;
1457 }
1458
1459 static int worker_main(int sock)
1460 {
1461 save_netns();
1462
1463 while (true) {
1464
1465 struct msg msg;
1466
1467 if (recv_message(sock, &msg) < 0)
1468 goto out;
1469
1470 switch (msg.type) {
1471 case MSG_EXIT:
1472 if (env.debug)
1473 fprintf(stderr, "[%d]: worker exit.\n",
1474 env.worker_id);
1475 goto out;
1476 case MSG_DO_TEST: {
1477 int test_to_run = msg.do_test.num;
1478 struct prog_test_def *test = &prog_test_defs[test_to_run];
1479 struct test_state *state = &test_states[test_to_run];
1480 struct msg msg;
1481
1482 if (env.debug)
1483 fprintf(stderr, "[%d]: #%d:%s running.\n",
1484 env.worker_id,
1485 test_to_run + 1,
1486 test->test_name);
1487
1488 run_one_test(test_to_run);
1489
1490 memset(&msg, 0, sizeof(msg));
1491 msg.type = MSG_TEST_DONE;
1492 msg.test_done.num = test_to_run;
1493 msg.test_done.error_cnt = state->error_cnt;
1494 msg.test_done.skip_cnt = state->skip_cnt;
1495 msg.test_done.sub_succ_cnt = state->sub_succ_cnt;
1496 msg.test_done.subtest_num = state->subtest_num;
1497 msg.test_done.have_log = false;
1498
1499 if (verbose() || state->force_log || state->error_cnt) {
1500 if (state->log_cnt)
1501 msg.test_done.have_log = true;
1502 }
1503 if (send_message(sock, &msg) < 0) {
1504 perror("Fail to send message done");
1505 goto out;
1506 }
1507
1508
1509 if (msg.test_done.have_log)
1510 worker_main_send_log(sock, state->log_buf, state->log_cnt);
1511
1512 if (state->log_buf) {
1513 free(state->log_buf);
1514 state->log_buf = NULL;
1515 state->log_cnt = 0;
1516 }
1517
1518 if (state->subtest_num)
1519 if (worker_main_send_subtests(sock, state))
1520 goto out;
1521
1522 if (env.debug)
1523 fprintf(stderr, "[%d]: #%d:%s done.\n",
1524 env.worker_id,
1525 test_to_run + 1,
1526 test->test_name);
1527 break;
1528 }
1529 default:
1530 if (env.debug)
1531 fprintf(stderr, "[%d]: unknown message.\n", env.worker_id);
1532 return -1;
1533 }
1534 }
1535 out:
1536 return 0;
1537 }
1538
1539 static void free_test_states(void)
1540 {
1541 int i, j;
1542
1543 for (i = 0; i < ARRAY_SIZE(prog_test_defs); i++) {
1544 struct test_state *test_state = &test_states[i];
1545
1546 for (j = 0; j < test_state->subtest_num; j++)
1547 free_subtest_state(&test_state->subtest_states[j]);
1548
1549 free(test_state->subtest_states);
1550 free(test_state->log_buf);
1551 test_state->subtest_states = NULL;
1552 test_state->log_buf = NULL;
1553 }
1554 }
1555
1556 int main(int argc, char **argv)
1557 {
1558 static const struct argp argp = {
1559 .options = opts,
1560 .parser = parse_arg,
1561 .doc = argp_program_doc,
1562 };
1563 struct sigaction sigact = {
1564 .sa_handler = crash_handler,
1565 .sa_flags = SA_RESETHAND,
1566 };
1567 int err, i;
1568
1569 sigaction(SIGSEGV, &sigact, NULL);
1570
1571 err = argp_parse(&argp, argc, argv, 0, NULL, &env);
1572 if (err)
1573 return err;
1574
1575 err = cd_flavor_subdir(argv[0]);
1576 if (err)
1577 return err;
1578
1579
1580 libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
1581 libbpf_set_print(libbpf_print_fn);
1582
1583 srand(time(NULL));
1584
1585 env.jit_enabled = is_jit_enabled();
1586 env.nr_cpus = libbpf_num_possible_cpus();
1587 if (env.nr_cpus < 0) {
1588 fprintf(stderr, "Failed to get number of CPUs: %d!\n",
1589 env.nr_cpus);
1590 return -1;
1591 }
1592
1593 env.stdout = stdout;
1594 env.stderr = stderr;
1595
1596 env.has_testmod = true;
1597 if (!env.list_test_names && load_bpf_testmod()) {
1598 fprintf(env.stderr, "WARNING! Selftests relying on bpf_testmod.ko will be skipped.\n");
1599 env.has_testmod = false;
1600 }
1601
1602
1603 for (i = 0; i < prog_test_cnt; i++) {
1604 struct prog_test_def *test = &prog_test_defs[i];
1605
1606 test->test_num = i + 1;
1607 test->should_run = should_run(&env.test_selector,
1608 test->test_num, test->test_name);
1609
1610 if ((test->run_test == NULL && test->run_serial_test == NULL) ||
1611 (test->run_test != NULL && test->run_serial_test != NULL)) {
1612 fprintf(stderr, "Test %d:%s must have either test_%s() or serial_test_%sl() defined.\n",
1613 test->test_num, test->test_name, test->test_name, test->test_name);
1614 exit(EXIT_ERR_SETUP_INFRA);
1615 }
1616 }
1617
1618
1619 if (env.get_test_cnt || env.list_test_names)
1620 env.workers = 0;
1621
1622
1623 env.worker_id = -1;
1624 if (env.workers) {
1625 env.worker_pids = calloc(sizeof(__pid_t), env.workers);
1626 env.worker_socks = calloc(sizeof(int), env.workers);
1627 if (env.debug)
1628 fprintf(stdout, "Launching %d workers.\n", env.workers);
1629 for (i = 0; i < env.workers; i++) {
1630 int sv[2];
1631 pid_t pid;
1632
1633 if (socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sv) < 0) {
1634 perror("Fail to create worker socket");
1635 return -1;
1636 }
1637 pid = fork();
1638 if (pid < 0) {
1639 perror("Failed to fork worker");
1640 return -1;
1641 } else if (pid != 0) {
1642 close(sv[1]);
1643 env.worker_pids[i] = pid;
1644 env.worker_socks[i] = sv[0];
1645 } else {
1646 close(sv[0]);
1647 env.worker_id = i;
1648 return worker_main(sv[1]);
1649 }
1650 }
1651
1652 if (env.worker_id == -1) {
1653 server_main();
1654 goto out;
1655 }
1656 }
1657
1658
1659
1660
1661 save_netns();
1662
1663 for (i = 0; i < prog_test_cnt; i++) {
1664 struct prog_test_def *test = &prog_test_defs[i];
1665
1666 if (!test->should_run)
1667 continue;
1668
1669 if (env.get_test_cnt) {
1670 env.succ_cnt++;
1671 continue;
1672 }
1673
1674 if (env.list_test_names) {
1675 fprintf(env.stdout, "%s\n", test->test_name);
1676 env.succ_cnt++;
1677 continue;
1678 }
1679
1680 run_one_test(i);
1681 }
1682
1683 if (env.get_test_cnt) {
1684 printf("%d\n", env.succ_cnt);
1685 goto out;
1686 }
1687
1688 if (env.list_test_names)
1689 goto out;
1690
1691 calculate_summary_and_print_errors(&env);
1692
1693 close(env.saved_netns_fd);
1694 out:
1695 if (!env.list_test_names && env.has_testmod)
1696 unload_bpf_testmod();
1697
1698 free_test_selector(&env.test_selector);
1699 free_test_selector(&env.subtest_selector);
1700 free_test_states();
1701
1702 if (env.succ_cnt + env.fail_cnt + env.skip_cnt == 0)
1703 return EXIT_NO_TEST;
1704
1705 return env.fail_cnt ? EXIT_FAILURE : EXIT_SUCCESS;
1706 }