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