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