xref: /linux/tools/testing/selftests/bpf/test_progs.h (revision 1c36a1a41a47c6ebd8d64bd76a67e74d7a39ee19)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __TEST_PROGS_H
3 #define __TEST_PROGS_H
4 
5 #include <stdio.h>
6 #include <unistd.h>
7 #include <errno.h>
8 #include <string.h>
9 #include <assert.h>
10 #include <regex.h>
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <time.h>
14 #include <signal.h>
15 
16 #include <linux/types.h>
17 typedef __u16 __sum16;
18 #include <arpa/inet.h>
19 #include <linux/if_ether.h>
20 #include <linux/if_packet.h>
21 #include <linux/ip.h>
22 #include <linux/ipv6.h>
23 #include <linux/filter.h>
24 #include <linux/perf_event.h>
25 #include <linux/socket.h>
26 #include <linux/unistd.h>
27 
28 #include <sys/ioctl.h>
29 #include <sys/wait.h>
30 #include <sys/types.h>
31 #include <sys/time.h>
32 #include <sys/param.h>
33 #include <fcntl.h>
34 #include <pthread.h>
35 #include <linux/bpf.h>
36 #include <linux/err.h>
37 #include <bpf/bpf.h>
38 #include <bpf/libbpf.h>
39 
40 #include "test_iptunnel_common.h"
41 #include "bpf_util.h"
42 #include <bpf/bpf_endian.h>
43 #include "trace_helpers.h"
44 #include "testing_helpers.h"
45 
46 enum verbosity {
47 	VERBOSE_NONE,
48 	VERBOSE_NORMAL,
49 	VERBOSE_VERY,
50 	VERBOSE_SUPER,
51 };
52 
53 struct test_filter {
54 	char *name;
55 	char **subtests;
56 	int subtest_cnt;
57 };
58 
59 struct test_filter_set {
60 	struct test_filter *tests;
61 	int cnt;
62 };
63 
64 struct test_selector {
65 	struct test_filter_set whitelist;
66 	struct test_filter_set blacklist;
67 	bool *num_set;
68 	int num_set_len;
69 };
70 
71 struct subtest_state {
72 	char *name;
73 	size_t log_cnt;
74 	char *log_buf;
75 	int error_cnt;
76 	bool skipped;
77 	bool filtered;
78 	bool should_tmon;
79 
80 	FILE *stdout_saved;
81 };
82 
83 struct test_state {
84 	bool tested;
85 	bool force_log;
86 
87 	int error_cnt;
88 	int skip_cnt;
89 	int sub_succ_cnt;
90 
91 	struct subtest_state *subtest_states;
92 	int subtest_num;
93 
94 	size_t log_cnt;
95 	char *log_buf;
96 
97 	FILE *stdout_saved;
98 };
99 
100 extern int env_verbosity;
101 
102 struct test_env {
103 	struct test_selector test_selector;
104 	struct test_selector subtest_selector;
105 	struct test_selector tmon_selector;
106 	bool verifier_stats;
107 	bool debug;
108 	bool error_summary;
109 	enum verbosity verbosity;
110 
111 	bool jit_enabled;
112 	bool has_testmod;
113 	bool get_test_cnt;
114 	bool list_test_names;
115 
116 	struct prog_test_def *test; /* current running test */
117 	struct test_state *test_state; /* current running test state */
118 	struct subtest_state *subtest_state; /* current running subtest state */
119 
120 	FILE *stdout_saved;
121 	FILE *stderr_saved;
122 	int nr_cpus;
123 	FILE *json;
124 
125 	int succ_cnt; /* successful tests */
126 	int sub_succ_cnt; /* successful sub-tests */
127 	int fail_cnt; /* failed tests */
128 	int skip_cnt; /* skipped tests */
129 	int not_built_cnt; /* tests not built */
130 
131 	int saved_netns_fd;
132 	int workers; /* number of worker process */
133 	int worker_id; /* id number of current worker, main process is -1 */
134 	pid_t *worker_pids; /* array of worker pids */
135 	int *worker_socks; /* array of worker socks */
136 	int *worker_current_test; /* array of current running test for each worker */
137 
138 	pthread_t main_thread;
139 	int secs_till_notify;
140 	int secs_till_kill;
141 	timer_t watchdog; /* watch for stalled tests/subtests */
142 	enum { WD_NOTIFY, WD_KILL } watchdog_state;
143 };
144 
145 #define MAX_LOG_TRUNK_SIZE 8192
146 #define MAX_SUBTEST_NAME 1024
147 enum msg_type {
148 	MSG_DO_TEST = 0,
149 	MSG_TEST_DONE = 1,
150 	MSG_TEST_LOG = 2,
151 	MSG_SUBTEST_DONE = 3,
152 	MSG_EXIT = 255,
153 };
154 struct msg {
155 	enum msg_type type;
156 	union {
157 		struct {
158 			int num;
159 		} do_test;
160 		struct {
161 			int num;
162 			int sub_succ_cnt;
163 			int error_cnt;
164 			int skip_cnt;
165 			bool have_log;
166 			int subtest_num;
167 		} test_done;
168 		struct {
169 			char log_buf[MAX_LOG_TRUNK_SIZE + 1];
170 			bool is_last;
171 		} test_log;
172 		struct {
173 			int num;
174 			char name[MAX_SUBTEST_NAME + 1];
175 			int error_cnt;
176 			bool skipped;
177 			bool filtered;
178 			bool have_log;
179 		} subtest_done;
180 	};
181 };
182 
183 extern struct test_env env;
184 
185 void test__force_log(void);
186 bool test__start_subtest_with_desc(const char *name, const char *description);
187 bool test__start_subtest(const char *name);
188 void test__end_subtest(void);
189 void test__skip(void);
190 void test__fail(void);
191 int test__join_cgroup(const char *path);
192 void hexdump(const char *prefix, const void *buf, size_t len);
193 
194 #define PRINT_FAIL(format...)                                                  \
195 	({                                                                     \
196 		test__fail();                                                  \
197 		fprintf(stdout, "%s:FAIL:%d ", __func__, __LINE__);            \
198 		fprintf(stdout, ##format);                                     \
199 	})
200 
201 #define _CHECK(condition, tag, duration, format...) ({			\
202 	int __ret = !!(condition);					\
203 	int __save_errno = errno;					\
204 	if (__ret) {							\
205 		test__fail();						\
206 		fprintf(stdout, "%s:FAIL:%s ", __func__, tag);		\
207 		fprintf(stdout, ##format);				\
208 	} else {							\
209 		fprintf(stdout, "%s:PASS:%s %d nsec\n",			\
210 		       __func__, tag, duration);			\
211 	}								\
212 	errno = __save_errno;						\
213 	__ret;								\
214 })
215 
216 #define CHECK_FAIL(condition) ({					\
217 	int __ret = !!(condition);					\
218 	int __save_errno = errno;					\
219 	if (__ret) {							\
220 		test__fail();						\
221 		fprintf(stdout, "%s:FAIL:%d\n", __func__, __LINE__);	\
222 	}								\
223 	errno = __save_errno;						\
224 	__ret;								\
225 })
226 
227 #define CHECK(condition, tag, format...) \
228 	_CHECK(condition, tag, duration, format)
229 #define CHECK_ATTR(condition, tag, format...) \
230 	_CHECK(condition, tag, tattr.duration, format)
231 
232 #define ASSERT_FAIL(fmt, args...) ({					\
233 	static int duration = 0;					\
234 	CHECK(false, "", fmt"\n", ##args);				\
235 	false;								\
236 })
237 
238 #define ASSERT_TRUE(actual, name) ({					\
239 	static int duration = 0;					\
240 	bool ___ok = (actual);						\
241 	CHECK(!___ok, (name), "unexpected %s: got FALSE\n", (name));	\
242 	___ok;								\
243 })
244 
245 #define ASSERT_FALSE(actual, name) ({					\
246 	static int duration = 0;					\
247 	bool ___ok = !(actual);						\
248 	CHECK(!___ok, (name), "unexpected %s: got TRUE\n", (name));	\
249 	___ok;								\
250 })
251 
252 #define ASSERT_EQ(actual, expected, name) ({				\
253 	static int duration = 0;					\
254 	typeof(actual) ___act = (actual);				\
255 	typeof(expected) ___exp = (expected);				\
256 	bool ___ok = ___act == ___exp;					\
257 	CHECK(!___ok, (name),						\
258 	      "unexpected %s: actual %lld != expected %lld\n",		\
259 	      (name), (long long)(___act), (long long)(___exp));	\
260 	___ok;								\
261 })
262 
263 #define ASSERT_NEQ(actual, expected, name) ({				\
264 	static int duration = 0;					\
265 	typeof(actual) ___act = (actual);				\
266 	typeof(expected) ___exp = (expected);				\
267 	bool ___ok = ___act != ___exp;					\
268 	CHECK(!___ok, (name),						\
269 	      "unexpected %s: actual %lld == expected %lld\n",		\
270 	      (name), (long long)(___act), (long long)(___exp));	\
271 	___ok;								\
272 })
273 
274 #define ASSERT_LT(actual, expected, name) ({				\
275 	static int duration = 0;					\
276 	typeof(actual) ___act = (actual);				\
277 	typeof(expected) ___exp = (expected);				\
278 	bool ___ok = ___act < ___exp;					\
279 	CHECK(!___ok, (name),						\
280 	      "unexpected %s: actual %lld >= expected %lld\n",		\
281 	      (name), (long long)(___act), (long long)(___exp));	\
282 	___ok;								\
283 })
284 
285 #define ASSERT_LE(actual, expected, name) ({				\
286 	static int duration = 0;					\
287 	typeof(actual) ___act = (actual);				\
288 	typeof(expected) ___exp = (expected);				\
289 	bool ___ok = ___act <= ___exp;					\
290 	CHECK(!___ok, (name),						\
291 	      "unexpected %s: actual %lld > expected %lld\n",		\
292 	      (name), (long long)(___act), (long long)(___exp));	\
293 	___ok;								\
294 })
295 
296 #define ASSERT_GT(actual, expected, name) ({				\
297 	static int duration = 0;					\
298 	typeof(actual) ___act = (actual);				\
299 	typeof(expected) ___exp = (expected);				\
300 	bool ___ok = ___act > ___exp;					\
301 	CHECK(!___ok, (name),						\
302 	      "unexpected %s: actual %lld <= expected %lld\n",		\
303 	      (name), (long long)(___act), (long long)(___exp));	\
304 	___ok;								\
305 })
306 
307 #define ASSERT_GE(actual, expected, name) ({				\
308 	static int duration = 0;					\
309 	typeof(actual) ___act = (actual);				\
310 	typeof(expected) ___exp = (expected);				\
311 	bool ___ok = ___act >= ___exp;					\
312 	CHECK(!___ok, (name),						\
313 	      "unexpected %s: actual %lld < expected %lld\n",		\
314 	      (name), (long long)(___act), (long long)(___exp));	\
315 	___ok;								\
316 })
317 
318 #define ASSERT_STREQ(actual, expected, name) ({				\
319 	static int duration = 0;					\
320 	const char *___act = actual;					\
321 	const char *___exp = expected;					\
322 	bool ___ok = strcmp(___act, ___exp) == 0;			\
323 	CHECK(!___ok, (name),						\
324 	      "unexpected %s: actual '%s' != expected '%s'\n",		\
325 	      (name), ___act, ___exp);					\
326 	___ok;								\
327 })
328 
329 #define ASSERT_STRNEQ(actual, expected, len, name) ({			\
330 	static int duration = 0;					\
331 	const char *___act = actual;					\
332 	const char *___exp = expected;					\
333 	int ___len = len;						\
334 	bool ___ok = strncmp(___act, ___exp, ___len) == 0;		\
335 	CHECK(!___ok, (name),						\
336 	      "unexpected %s: actual '%.*s' != expected '%.*s'\n",	\
337 	      (name), ___len, ___act, ___len, ___exp);			\
338 	___ok;								\
339 })
340 
341 #define ASSERT_HAS_SUBSTR(str, substr, name) ({				\
342 	static int duration = 0;					\
343 	const char *___str = str;					\
344 	const char *___substr = substr;					\
345 	bool ___ok = strstr(___str, ___substr) != NULL;			\
346 	CHECK(!___ok, (name),						\
347 	      "unexpected %s: '%s' is not a substring of '%s'\n",	\
348 	      (name), ___substr, ___str);				\
349 	___ok;								\
350 })
351 
352 #define ASSERT_TEXT_EQ(actual, expected, name) \
353 	ASSERT_TRUE(compare_text_to_expected(actual, expected) == 0, name)
354 
355 #define ASSERT_MEMEQ(actual, expected, len, name) ({			\
356 	static int duration = 0;					\
357 	const void *__act = actual;					\
358 	const void *__exp = expected;					\
359 	int __len = len;						\
360 	bool ___ok = memcmp(__act, __exp, __len) == 0;			\
361 	CHECK(!___ok, (name), "unexpected memory mismatch\n");		\
362 	fprintf(stdout, "actual:\n");					\
363 	hexdump("\t", __act, __len);					\
364 	fprintf(stdout, "expected:\n");					\
365 	hexdump("\t", __exp, __len);					\
366 	___ok;								\
367 })
368 
369 #define ASSERT_OK(res, name) ({						\
370 	static int duration = 0;					\
371 	long long ___res = (res);					\
372 	bool ___ok = ___res == 0;					\
373 	CHECK(!___ok, (name), "unexpected error: %lld (errno %d)\n",	\
374 	      ___res, errno);						\
375 	___ok;								\
376 })
377 
378 #define ASSERT_ERR(res, name) ({					\
379 	static int duration = 0;					\
380 	long long ___res = (res);					\
381 	bool ___ok = ___res < 0;					\
382 	CHECK(!___ok, (name), "unexpected success: %lld\n", ___res);	\
383 	___ok;								\
384 })
385 
386 #define ASSERT_NULL(ptr, name) ({					\
387 	static int duration = 0;					\
388 	const void *___res = (ptr);					\
389 	bool ___ok = !___res;						\
390 	CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res);	\
391 	___ok;								\
392 })
393 
394 #define ASSERT_OK_PTR(ptr, name) ({					\
395 	static int duration = 0;					\
396 	const void *___res = (ptr);					\
397 	int ___err = libbpf_get_error(___res);				\
398 	bool ___ok = ___err == 0;					\
399 	CHECK(!___ok, (name), "unexpected error: %d\n", ___err);	\
400 	___ok;								\
401 })
402 
403 #define ASSERT_ERR_PTR(ptr, name) ({					\
404 	static int duration = 0;					\
405 	const void *___res = (ptr);					\
406 	int ___err = libbpf_get_error(___res);				\
407 	bool ___ok = ___err != 0;					\
408 	CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res);	\
409 	___ok;								\
410 })
411 
412 #define ASSERT_OK_FD(fd, name) ({					\
413 	static int duration = 0;					\
414 	int ___fd = (fd);						\
415 	bool ___ok = ___fd >= 0;					\
416 	CHECK(!___ok, (name), "unexpected fd: %d (errno %d)\n",		\
417 	      ___fd, errno);						\
418 	___ok;								\
419 })
420 
421 #define ASSERT_ERR_FD(fd, name) ({					\
422 	static int duration = 0;					\
423 	int ___fd = (fd);						\
424 	bool ___ok = ___fd < 0;						\
425 	CHECK(!___ok, (name), "unexpected fd: %d\n", ___fd);		\
426 	___ok;								\
427 })
428 
429 #define SYS(goto_label, fmt, ...)					\
430 	({								\
431 		char cmd[1024];						\
432 		snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__);		\
433 		if (!ASSERT_OK(system(cmd), cmd))			\
434 			goto goto_label;				\
435 	})
436 
437 #define SYS_FAIL(goto_label, fmt, ...)					\
438 	({								\
439 		char cmd[1024];						\
440 		snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__);		\
441 		if (!ASSERT_NEQ(0, system(cmd), cmd))			\
442 			goto goto_label;				\
443 	})
444 
445 #define ALL_TO_DEV_NULL " >/dev/null 2>&1"
446 
447 #define SYS_NOFAIL(fmt, ...)						\
448 	({								\
449 		char cmd[1024];						\
450 		int n;							\
451 		n = snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__);	\
452 		if (n < sizeof(cmd) && sizeof(cmd) - n >= sizeof(ALL_TO_DEV_NULL)) \
453 			strcat(cmd, ALL_TO_DEV_NULL);			\
454 		system(cmd);						\
455 	})
456 
457 int start_libbpf_log_capture(void);
458 char *stop_libbpf_log_capture(void);
459 
460 static inline __u64 ptr_to_u64(const void *ptr)
461 {
462 	return (__u64) (unsigned long) ptr;
463 }
464 
465 static inline void *u64_to_ptr(__u64 ptr)
466 {
467 	return (void *) (unsigned long) ptr;
468 }
469 
470 static inline __u32 id_from_prog_fd(int fd)
471 {
472 	struct bpf_prog_info prog_info = {};
473 	__u32 prog_info_len = sizeof(prog_info);
474 	int err;
475 
476 	err = bpf_obj_get_info_by_fd(fd, &prog_info, &prog_info_len);
477 	if (!ASSERT_OK(err, "id_from_prog_fd"))
478 		return 0;
479 
480 	ASSERT_NEQ(prog_info.id, 0, "prog_info.id");
481 	return prog_info.id;
482 }
483 
484 static inline __u32 id_from_link_fd(int fd)
485 {
486 	struct bpf_link_info link_info = {};
487 	__u32 link_info_len = sizeof(link_info);
488 	int err;
489 
490 	err = bpf_link_get_info_by_fd(fd, &link_info, &link_info_len);
491 	if (!ASSERT_OK(err, "id_from_link_fd"))
492 		return 0;
493 
494 	ASSERT_NEQ(link_info.id, 0, "link_info.id");
495 	return link_info.id;
496 }
497 
498 int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);
499 int compare_map_keys(int map1_fd, int map2_fd);
500 int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
501 int trigger_module_test_read(int read_sz);
502 int trigger_module_test_write(int write_sz);
503 int write_sysctl(const char *sysctl, const char *value);
504 int get_bpf_max_tramp_links_from(struct btf *btf);
505 int get_bpf_max_tramp_links(void);
506 
507 struct netns_obj;
508 struct netns_obj *netns_new(const char *name, bool open);
509 void netns_free(struct netns_obj *netns);
510 
511 #ifdef __x86_64__
512 #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
513 #elif defined(__s390x__)
514 #define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep"
515 #elif defined(__aarch64__)
516 #define SYS_NANOSLEEP_KPROBE_NAME "__arm64_sys_nanosleep"
517 #elif defined(__riscv)
518 #define SYS_NANOSLEEP_KPROBE_NAME "__riscv_sys_nanosleep"
519 #else
520 #define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
521 #endif
522 
523 #define BPF_TESTMOD_TEST_FILE "/sys/kernel/bpf_testmod"
524 
525 typedef int (*pre_execution_cb)(struct bpf_object *obj);
526 
527 struct test_loader {
528 	char *log_buf;
529 	size_t log_buf_sz;
530 	pre_execution_cb pre_execution_cb;
531 
532 	struct bpf_object *obj;
533 };
534 
535 static inline void test_loader__set_pre_execution_cb(struct test_loader *tester,
536 						     pre_execution_cb cb)
537 {
538 	tester->pre_execution_cb = cb;
539 }
540 
541 typedef const void *(*skel_elf_bytes_fn)(size_t *sz);
542 
543 extern void test_loader__run_subtests(struct test_loader *tester,
544 				      const char *skel_name,
545 				      skel_elf_bytes_fn elf_bytes_factory);
546 
547 extern void test_loader_fini(struct test_loader *tester);
548 
549 #define RUN_TESTS(skel) ({						       \
550 	struct test_loader tester = {};					       \
551 									       \
552 	test_loader__run_subtests(&tester, #skel, skel##__elf_bytes);	       \
553 	test_loader_fini(&tester);					       \
554 })
555 
556 struct expect_msg {
557 	const char *substr; /* substring match */
558 	regex_t regex;
559 	bool is_regex;
560 	bool on_next_line;
561 	bool negative;
562 };
563 
564 struct expected_msgs {
565 	struct expect_msg *patterns;
566 	size_t cnt;
567 };
568 
569 void validate_msgs(const char *log_buf, struct expected_msgs *msgs,
570 		   void (*emit_fn)(const char *buf, bool force));
571 void free_msgs(struct expected_msgs *msgs);
572 void verify_test_stderr(struct bpf_object *obj, struct bpf_program *prog);
573 
574 #endif /* __TEST_PROGS_H */
575