xref: /linux/tools/testing/selftests/bpf/test_progs.h (revision 5a8cd539ac19f7a68e68e1d25ef9ca2ff55b8500)
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_MEMEQ(actual, expected, len, name) ({			\
353 	static int duration = 0;					\
354 	const void *__act = actual;					\
355 	const void *__exp = expected;					\
356 	int __len = len;						\
357 	bool ___ok = memcmp(__act, __exp, __len) == 0;			\
358 	CHECK(!___ok, (name), "unexpected memory mismatch\n");		\
359 	fprintf(stdout, "actual:\n");					\
360 	hexdump("\t", __act, __len);					\
361 	fprintf(stdout, "expected:\n");					\
362 	hexdump("\t", __exp, __len);					\
363 	___ok;								\
364 })
365 
366 #define ASSERT_OK(res, name) ({						\
367 	static int duration = 0;					\
368 	long long ___res = (res);					\
369 	bool ___ok = ___res == 0;					\
370 	CHECK(!___ok, (name), "unexpected error: %lld (errno %d)\n",	\
371 	      ___res, errno);						\
372 	___ok;								\
373 })
374 
375 #define ASSERT_ERR(res, name) ({					\
376 	static int duration = 0;					\
377 	long long ___res = (res);					\
378 	bool ___ok = ___res < 0;					\
379 	CHECK(!___ok, (name), "unexpected success: %lld\n", ___res);	\
380 	___ok;								\
381 })
382 
383 #define ASSERT_NULL(ptr, name) ({					\
384 	static int duration = 0;					\
385 	const void *___res = (ptr);					\
386 	bool ___ok = !___res;						\
387 	CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res);	\
388 	___ok;								\
389 })
390 
391 #define ASSERT_OK_PTR(ptr, name) ({					\
392 	static int duration = 0;					\
393 	const void *___res = (ptr);					\
394 	int ___err = libbpf_get_error(___res);				\
395 	bool ___ok = ___err == 0;					\
396 	CHECK(!___ok, (name), "unexpected error: %d\n", ___err);	\
397 	___ok;								\
398 })
399 
400 #define ASSERT_ERR_PTR(ptr, name) ({					\
401 	static int duration = 0;					\
402 	const void *___res = (ptr);					\
403 	int ___err = libbpf_get_error(___res);				\
404 	bool ___ok = ___err != 0;					\
405 	CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res);	\
406 	___ok;								\
407 })
408 
409 #define ASSERT_OK_FD(fd, name) ({					\
410 	static int duration = 0;					\
411 	int ___fd = (fd);						\
412 	bool ___ok = ___fd >= 0;					\
413 	CHECK(!___ok, (name), "unexpected fd: %d (errno %d)\n",		\
414 	      ___fd, errno);						\
415 	___ok;								\
416 })
417 
418 #define ASSERT_ERR_FD(fd, name) ({					\
419 	static int duration = 0;					\
420 	int ___fd = (fd);						\
421 	bool ___ok = ___fd < 0;						\
422 	CHECK(!___ok, (name), "unexpected fd: %d\n", ___fd);		\
423 	___ok;								\
424 })
425 
426 #define SYS(goto_label, fmt, ...)					\
427 	({								\
428 		char cmd[1024];						\
429 		snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__);		\
430 		if (!ASSERT_OK(system(cmd), cmd))			\
431 			goto goto_label;				\
432 	})
433 
434 #define SYS_FAIL(goto_label, fmt, ...)					\
435 	({								\
436 		char cmd[1024];						\
437 		snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__);		\
438 		if (!ASSERT_NEQ(0, system(cmd), cmd))			\
439 			goto goto_label;				\
440 	})
441 
442 #define ALL_TO_DEV_NULL " >/dev/null 2>&1"
443 
444 #define SYS_NOFAIL(fmt, ...)						\
445 	({								\
446 		char cmd[1024];						\
447 		int n;							\
448 		n = snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__);	\
449 		if (n < sizeof(cmd) && sizeof(cmd) - n >= sizeof(ALL_TO_DEV_NULL)) \
450 			strcat(cmd, ALL_TO_DEV_NULL);			\
451 		system(cmd);						\
452 	})
453 
454 int start_libbpf_log_capture(void);
455 char *stop_libbpf_log_capture(void);
456 
ptr_to_u64(const void * ptr)457 static inline __u64 ptr_to_u64(const void *ptr)
458 {
459 	return (__u64) (unsigned long) ptr;
460 }
461 
u64_to_ptr(__u64 ptr)462 static inline void *u64_to_ptr(__u64 ptr)
463 {
464 	return (void *) (unsigned long) ptr;
465 }
466 
id_from_prog_fd(int fd)467 static inline __u32 id_from_prog_fd(int fd)
468 {
469 	struct bpf_prog_info prog_info = {};
470 	__u32 prog_info_len = sizeof(prog_info);
471 	int err;
472 
473 	err = bpf_obj_get_info_by_fd(fd, &prog_info, &prog_info_len);
474 	if (!ASSERT_OK(err, "id_from_prog_fd"))
475 		return 0;
476 
477 	ASSERT_NEQ(prog_info.id, 0, "prog_info.id");
478 	return prog_info.id;
479 }
480 
id_from_link_fd(int fd)481 static inline __u32 id_from_link_fd(int fd)
482 {
483 	struct bpf_link_info link_info = {};
484 	__u32 link_info_len = sizeof(link_info);
485 	int err;
486 
487 	err = bpf_link_get_info_by_fd(fd, &link_info, &link_info_len);
488 	if (!ASSERT_OK(err, "id_from_link_fd"))
489 		return 0;
490 
491 	ASSERT_NEQ(link_info.id, 0, "link_info.id");
492 	return link_info.id;
493 }
494 
495 int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);
496 int compare_map_keys(int map1_fd, int map2_fd);
497 int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
498 int trigger_module_test_read(int read_sz);
499 int trigger_module_test_write(int write_sz);
500 int write_sysctl(const char *sysctl, const char *value);
501 int get_bpf_max_tramp_links_from(struct btf *btf);
502 int get_bpf_max_tramp_links(void);
503 
504 struct netns_obj;
505 struct netns_obj *netns_new(const char *name, bool open);
506 void netns_free(struct netns_obj *netns);
507 
508 #ifdef __x86_64__
509 #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
510 #elif defined(__s390x__)
511 #define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep"
512 #elif defined(__aarch64__)
513 #define SYS_NANOSLEEP_KPROBE_NAME "__arm64_sys_nanosleep"
514 #elif defined(__riscv)
515 #define SYS_NANOSLEEP_KPROBE_NAME "__riscv_sys_nanosleep"
516 #else
517 #define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
518 #endif
519 
520 #define BPF_TESTMOD_TEST_FILE "/sys/kernel/bpf_testmod"
521 
522 typedef int (*pre_execution_cb)(struct bpf_object *obj);
523 
524 struct test_loader {
525 	char *log_buf;
526 	size_t log_buf_sz;
527 	pre_execution_cb pre_execution_cb;
528 
529 	struct bpf_object *obj;
530 };
531 
test_loader__set_pre_execution_cb(struct test_loader * tester,pre_execution_cb cb)532 static inline void test_loader__set_pre_execution_cb(struct test_loader *tester,
533 						     pre_execution_cb cb)
534 {
535 	tester->pre_execution_cb = cb;
536 }
537 
538 typedef const void *(*skel_elf_bytes_fn)(size_t *sz);
539 
540 extern void test_loader__run_subtests(struct test_loader *tester,
541 				      const char *skel_name,
542 				      skel_elf_bytes_fn elf_bytes_factory);
543 
544 extern void test_loader_fini(struct test_loader *tester);
545 
546 #define RUN_TESTS(skel) ({						       \
547 	struct test_loader tester = {};					       \
548 									       \
549 	test_loader__run_subtests(&tester, #skel, skel##__elf_bytes);	       \
550 	test_loader_fini(&tester);					       \
551 })
552 
553 struct expect_msg {
554 	const char *substr; /* substring match */
555 	regex_t regex;
556 	bool is_regex;
557 	bool on_next_line;
558 	bool negative;
559 };
560 
561 struct expected_msgs {
562 	struct expect_msg *patterns;
563 	size_t cnt;
564 };
565 
566 void validate_msgs(const char *log_buf, struct expected_msgs *msgs,
567 		   void (*emit_fn)(const char *buf, bool force));
568 void free_msgs(struct expected_msgs *msgs);
569 void verify_test_stderr(struct bpf_object *obj, struct bpf_program *prog);
570 
571 #endif /* __TEST_PROGS_H */
572