1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2017 Facebook
3  */
4 #include "test_progs.h"
5 #include "cgroup_helpers.h"
6 #include "bpf_rlimit.h"
7 #include <argp.h>
8 #include <string.h>
9 
10 /* defined in test_progs.h */
11 struct test_env env;
12 
13 struct prog_test_def {
14 	const char *test_name;
15 	int test_num;
16 	void (*run_test)(void);
17 	bool force_log;
18 	int error_cnt;
19 	int skip_cnt;
20 	bool tested;
21 	bool need_cgroup_cleanup;
22 
23 	const char *subtest_name;
24 	int subtest_num;
25 
26 	/* store counts before subtest started */
27 	int old_error_cnt;
28 };
29 
30 static bool should_run(struct test_selector *sel, int num, const char *name)
31 {
32 	if (sel->name && sel->name[0] && !strstr(name, sel->name))
33 		return false;
34 
35 	if (!sel->num_set)
36 		return true;
37 
38 	return num < sel->num_set_len && sel->num_set[num];
39 }
40 
41 static void dump_test_log(const struct prog_test_def *test, bool failed)
42 {
43 	if (stdout == env.stdout)
44 		return;
45 
46 	fflush(stdout); /* exports env.log_buf & env.log_cnt */
47 
48 	if (env.verbose || test->force_log || failed) {
49 		if (env.log_cnt) {
50 			env.log_buf[env.log_cnt] = '\0';
51 			fprintf(env.stdout, "%s", env.log_buf);
52 			if (env.log_buf[env.log_cnt - 1] != '\n')
53 				fprintf(env.stdout, "\n");
54 		}
55 	}
56 
57 	fseeko(stdout, 0, SEEK_SET); /* rewind */
58 }
59 
60 static void skip_account(void)
61 {
62 	if (env.test->skip_cnt) {
63 		env.skip_cnt++;
64 		env.test->skip_cnt = 0;
65 	}
66 }
67 
68 void test__end_subtest()
69 {
70 	struct prog_test_def *test = env.test;
71 	int sub_error_cnt = test->error_cnt - test->old_error_cnt;
72 
73 	if (sub_error_cnt)
74 		env.fail_cnt++;
75 	else
76 		env.sub_succ_cnt++;
77 	skip_account();
78 
79 	dump_test_log(test, sub_error_cnt);
80 
81 	fprintf(env.stdout, "#%d/%d %s:%s\n",
82 	       test->test_num, test->subtest_num,
83 	       test->subtest_name, sub_error_cnt ? "FAIL" : "OK");
84 }
85 
86 bool test__start_subtest(const char *name)
87 {
88 	struct prog_test_def *test = env.test;
89 
90 	if (test->subtest_name) {
91 		test__end_subtest();
92 		test->subtest_name = NULL;
93 	}
94 
95 	test->subtest_num++;
96 
97 	if (!name || !name[0]) {
98 		fprintf(env.stderr,
99 			"Subtest #%d didn't provide sub-test name!\n",
100 			test->subtest_num);
101 		return false;
102 	}
103 
104 	if (!should_run(&env.subtest_selector, test->subtest_num, name))
105 		return false;
106 
107 	test->subtest_name = name;
108 	env.test->old_error_cnt = env.test->error_cnt;
109 
110 	return true;
111 }
112 
113 void test__force_log() {
114 	env.test->force_log = true;
115 }
116 
117 void test__skip(void)
118 {
119 	env.test->skip_cnt++;
120 }
121 
122 void test__fail(void)
123 {
124 	env.test->error_cnt++;
125 }
126 
127 int test__join_cgroup(const char *path)
128 {
129 	int fd;
130 
131 	if (!env.test->need_cgroup_cleanup) {
132 		if (setup_cgroup_environment()) {
133 			fprintf(stderr,
134 				"#%d %s: Failed to setup cgroup environment\n",
135 				env.test->test_num, env.test->test_name);
136 			return -1;
137 		}
138 
139 		env.test->need_cgroup_cleanup = true;
140 	}
141 
142 	fd = create_and_get_cgroup(path);
143 	if (fd < 0) {
144 		fprintf(stderr,
145 			"#%d %s: Failed to create cgroup '%s' (errno=%d)\n",
146 			env.test->test_num, env.test->test_name, path, errno);
147 		return fd;
148 	}
149 
150 	if (join_cgroup(path)) {
151 		fprintf(stderr,
152 			"#%d %s: Failed to join cgroup '%s' (errno=%d)\n",
153 			env.test->test_num, env.test->test_name, path, errno);
154 		return -1;
155 	}
156 
157 	return fd;
158 }
159 
160 struct ipv4_packet pkt_v4 = {
161 	.eth.h_proto = __bpf_constant_htons(ETH_P_IP),
162 	.iph.ihl = 5,
163 	.iph.protocol = IPPROTO_TCP,
164 	.iph.tot_len = __bpf_constant_htons(MAGIC_BYTES),
165 	.tcp.urg_ptr = 123,
166 	.tcp.doff = 5,
167 };
168 
169 struct ipv6_packet pkt_v6 = {
170 	.eth.h_proto = __bpf_constant_htons(ETH_P_IPV6),
171 	.iph.nexthdr = IPPROTO_TCP,
172 	.iph.payload_len = __bpf_constant_htons(MAGIC_BYTES),
173 	.tcp.urg_ptr = 123,
174 	.tcp.doff = 5,
175 };
176 
177 int bpf_find_map(const char *test, struct bpf_object *obj, const char *name)
178 {
179 	struct bpf_map *map;
180 
181 	map = bpf_object__find_map_by_name(obj, name);
182 	if (!map) {
183 		printf("%s:FAIL:map '%s' not found\n", test, name);
184 		test__fail();
185 		return -1;
186 	}
187 	return bpf_map__fd(map);
188 }
189 
190 static bool is_jit_enabled(void)
191 {
192 	const char *jit_sysctl = "/proc/sys/net/core/bpf_jit_enable";
193 	bool enabled = false;
194 	int sysctl_fd;
195 
196 	sysctl_fd = open(jit_sysctl, 0, O_RDONLY);
197 	if (sysctl_fd != -1) {
198 		char tmpc;
199 
200 		if (read(sysctl_fd, &tmpc, sizeof(tmpc)) == 1)
201 			enabled = (tmpc != '0');
202 		close(sysctl_fd);
203 	}
204 
205 	return enabled;
206 }
207 
208 int compare_map_keys(int map1_fd, int map2_fd)
209 {
210 	__u32 key, next_key;
211 	char val_buf[PERF_MAX_STACK_DEPTH *
212 		     sizeof(struct bpf_stack_build_id)];
213 	int err;
214 
215 	err = bpf_map_get_next_key(map1_fd, NULL, &key);
216 	if (err)
217 		return err;
218 	err = bpf_map_lookup_elem(map2_fd, &key, val_buf);
219 	if (err)
220 		return err;
221 
222 	while (bpf_map_get_next_key(map1_fd, &key, &next_key) == 0) {
223 		err = bpf_map_lookup_elem(map2_fd, &next_key, val_buf);
224 		if (err)
225 			return err;
226 
227 		key = next_key;
228 	}
229 	if (errno != ENOENT)
230 		return -1;
231 
232 	return 0;
233 }
234 
235 int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len)
236 {
237 	__u32 key, next_key, *cur_key_p, *next_key_p;
238 	char *val_buf1, *val_buf2;
239 	int i, err = 0;
240 
241 	val_buf1 = malloc(stack_trace_len);
242 	val_buf2 = malloc(stack_trace_len);
243 	cur_key_p = NULL;
244 	next_key_p = &key;
245 	while (bpf_map_get_next_key(smap_fd, cur_key_p, next_key_p) == 0) {
246 		err = bpf_map_lookup_elem(smap_fd, next_key_p, val_buf1);
247 		if (err)
248 			goto out;
249 		err = bpf_map_lookup_elem(amap_fd, next_key_p, val_buf2);
250 		if (err)
251 			goto out;
252 		for (i = 0; i < stack_trace_len; i++) {
253 			if (val_buf1[i] != val_buf2[i]) {
254 				err = -1;
255 				goto out;
256 			}
257 		}
258 		key = *next_key_p;
259 		cur_key_p = &key;
260 		next_key_p = &next_key;
261 	}
262 	if (errno != ENOENT)
263 		err = -1;
264 
265 out:
266 	free(val_buf1);
267 	free(val_buf2);
268 	return err;
269 }
270 
271 int extract_build_id(char *build_id, size_t size)
272 {
273 	FILE *fp;
274 	char *line = NULL;
275 	size_t len = 0;
276 
277 	fp = popen("readelf -n ./urandom_read | grep 'Build ID'", "r");
278 	if (fp == NULL)
279 		return -1;
280 
281 	if (getline(&line, &len, fp) == -1)
282 		goto err;
283 	fclose(fp);
284 
285 	if (len > size)
286 		len = size;
287 	memcpy(build_id, line, len);
288 	build_id[len] = '\0';
289 	return 0;
290 err:
291 	fclose(fp);
292 	return -1;
293 }
294 
295 void *spin_lock_thread(void *arg)
296 {
297 	__u32 duration, retval;
298 	int err, prog_fd = *(u32 *) arg;
299 
300 	err = bpf_prog_test_run(prog_fd, 10000, &pkt_v4, sizeof(pkt_v4),
301 				NULL, NULL, &retval, &duration);
302 	CHECK(err || retval, "",
303 	      "err %d errno %d retval %d duration %d\n",
304 	      err, errno, retval, duration);
305 	pthread_exit(arg);
306 }
307 
308 /* extern declarations for test funcs */
309 #define DEFINE_TEST(name) extern void test_##name();
310 #include <prog_tests/tests.h>
311 #undef DEFINE_TEST
312 
313 static struct prog_test_def prog_test_defs[] = {
314 #define DEFINE_TEST(name) {		\
315 	.test_name = #name,		\
316 	.run_test = &test_##name,	\
317 },
318 #include <prog_tests/tests.h>
319 #undef DEFINE_TEST
320 };
321 const int prog_test_cnt = ARRAY_SIZE(prog_test_defs);
322 
323 const char *argp_program_version = "test_progs 0.1";
324 const char *argp_program_bug_address = "<bpf@vger.kernel.org>";
325 const char argp_program_doc[] = "BPF selftests test runner";
326 
327 enum ARG_KEYS {
328 	ARG_TEST_NUM = 'n',
329 	ARG_TEST_NAME = 't',
330 	ARG_VERIFIER_STATS = 's',
331 	ARG_VERBOSE = 'v',
332 };
333 
334 static const struct argp_option opts[] = {
335 	{ "num", ARG_TEST_NUM, "NUM", 0,
336 	  "Run test number NUM only " },
337 	{ "name", ARG_TEST_NAME, "NAME", 0,
338 	  "Run tests with names containing NAME" },
339 	{ "verifier-stats", ARG_VERIFIER_STATS, NULL, 0,
340 	  "Output verifier statistics", },
341 	{ "verbose", ARG_VERBOSE, "LEVEL", OPTION_ARG_OPTIONAL,
342 	  "Verbose output (use -vv for extra verbose output)" },
343 	{},
344 };
345 
346 static int libbpf_print_fn(enum libbpf_print_level level,
347 			   const char *format, va_list args)
348 {
349 	if (!env.very_verbose && level == LIBBPF_DEBUG)
350 		return 0;
351 	vprintf(format, args);
352 	return 0;
353 }
354 
355 int parse_num_list(const char *s, struct test_selector *sel)
356 {
357 	int i, set_len = 0, num, start = 0, end = -1;
358 	bool *set = NULL, *tmp, parsing_end = false;
359 	char *next;
360 
361 	while (s[0]) {
362 		errno = 0;
363 		num = strtol(s, &next, 10);
364 		if (errno)
365 			return -errno;
366 
367 		if (parsing_end)
368 			end = num;
369 		else
370 			start = num;
371 
372 		if (!parsing_end && *next == '-') {
373 			s = next + 1;
374 			parsing_end = true;
375 			continue;
376 		} else if (*next == ',') {
377 			parsing_end = false;
378 			s = next + 1;
379 			end = num;
380 		} else if (*next == '\0') {
381 			parsing_end = false;
382 			s = next;
383 			end = num;
384 		} else {
385 			return -EINVAL;
386 		}
387 
388 		if (start > end)
389 			return -EINVAL;
390 
391 		if (end + 1 > set_len) {
392 			set_len = end + 1;
393 			tmp = realloc(set, set_len);
394 			if (!tmp) {
395 				free(set);
396 				return -ENOMEM;
397 			}
398 			set = tmp;
399 		}
400 		for (i = start; i <= end; i++) {
401 			set[i] = true;
402 		}
403 
404 	}
405 
406 	if (!set)
407 		return -EINVAL;
408 
409 	sel->num_set = set;
410 	sel->num_set_len = set_len;
411 
412 	return 0;
413 }
414 
415 static error_t parse_arg(int key, char *arg, struct argp_state *state)
416 {
417 	struct test_env *env = state->input;
418 
419 	switch (key) {
420 	case ARG_TEST_NUM: {
421 		char *subtest_str = strchr(arg, '/');
422 
423 		if (subtest_str) {
424 			*subtest_str = '\0';
425 			if (parse_num_list(subtest_str + 1,
426 					   &env->subtest_selector)) {
427 				fprintf(stderr,
428 					"Failed to parse subtest numbers.\n");
429 				return -EINVAL;
430 			}
431 		}
432 		if (parse_num_list(arg, &env->test_selector)) {
433 			fprintf(stderr, "Failed to parse test numbers.\n");
434 			return -EINVAL;
435 		}
436 		break;
437 	}
438 	case ARG_TEST_NAME: {
439 		char *subtest_str = strchr(arg, '/');
440 
441 		if (subtest_str) {
442 			*subtest_str = '\0';
443 			env->subtest_selector.name = strdup(subtest_str + 1);
444 			if (!env->subtest_selector.name)
445 				return -ENOMEM;
446 		}
447 		env->test_selector.name = strdup(arg);
448 		if (!env->test_selector.name)
449 			return -ENOMEM;
450 		break;
451 	}
452 	case ARG_VERIFIER_STATS:
453 		env->verifier_stats = true;
454 		break;
455 	case ARG_VERBOSE:
456 		if (arg) {
457 			if (strcmp(arg, "v") == 0) {
458 				env->very_verbose = true;
459 			} else {
460 				fprintf(stderr,
461 					"Unrecognized verbosity setting ('%s'), only -v and -vv are supported\n",
462 					arg);
463 				return -EINVAL;
464 			}
465 		}
466 		env->verbose = true;
467 		break;
468 	case ARGP_KEY_ARG:
469 		argp_usage(state);
470 		break;
471 	case ARGP_KEY_END:
472 		break;
473 	default:
474 		return ARGP_ERR_UNKNOWN;
475 	}
476 	return 0;
477 }
478 
479 static void stdio_hijack(void)
480 {
481 #ifdef __GLIBC__
482 	env.stdout = stdout;
483 	env.stderr = stderr;
484 
485 	if (env.verbose) {
486 		/* nothing to do, output to stdout by default */
487 		return;
488 	}
489 
490 	/* stdout and stderr -> buffer */
491 	fflush(stdout);
492 
493 	stdout = open_memstream(&env.log_buf, &env.log_cnt);
494 	if (!stdout) {
495 		stdout = env.stdout;
496 		perror("open_memstream");
497 		return;
498 	}
499 
500 	stderr = stdout;
501 #endif
502 }
503 
504 static void stdio_restore(void)
505 {
506 #ifdef __GLIBC__
507 	if (stdout == env.stdout)
508 		return;
509 
510 	fclose(stdout);
511 	free(env.log_buf);
512 
513 	env.log_buf = NULL;
514 	env.log_cnt = 0;
515 
516 	stdout = env.stdout;
517 	stderr = env.stderr;
518 #endif
519 }
520 
521 int main(int argc, char **argv)
522 {
523 	static const struct argp argp = {
524 		.options = opts,
525 		.parser = parse_arg,
526 		.doc = argp_program_doc,
527 	};
528 	int err, i;
529 
530 	err = argp_parse(&argp, argc, argv, 0, NULL, &env);
531 	if (err)
532 		return err;
533 
534 	libbpf_set_print(libbpf_print_fn);
535 
536 	srand(time(NULL));
537 
538 	env.jit_enabled = is_jit_enabled();
539 
540 	stdio_hijack();
541 	for (i = 0; i < prog_test_cnt; i++) {
542 		struct prog_test_def *test = &prog_test_defs[i];
543 
544 		env.test = test;
545 		test->test_num = i + 1;
546 
547 		if (!should_run(&env.test_selector,
548 				test->test_num, test->test_name))
549 			continue;
550 
551 		test->run_test();
552 		/* ensure last sub-test is finalized properly */
553 		if (test->subtest_name)
554 			test__end_subtest();
555 
556 		test->tested = true;
557 		if (test->error_cnt)
558 			env.fail_cnt++;
559 		else
560 			env.succ_cnt++;
561 		skip_account();
562 
563 		dump_test_log(test, test->error_cnt);
564 
565 		fprintf(env.stdout, "#%d %s:%s\n",
566 			test->test_num, test->test_name,
567 			test->error_cnt ? "FAIL" : "OK");
568 
569 		if (test->need_cgroup_cleanup)
570 			cleanup_cgroup_environment();
571 	}
572 	stdio_restore();
573 	printf("Summary: %d/%d PASSED, %d SKIPPED, %d FAILED\n",
574 	       env.succ_cnt, env.sub_succ_cnt, env.skip_cnt, env.fail_cnt);
575 
576 	free(env.test_selector.num_set);
577 	free(env.subtest_selector.num_set);
578 
579 	return env.fail_cnt ? EXIT_FAILURE : EXIT_SUCCESS;
580 }
581