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