1 /* SPDX-License-Identifier: GPL-2.0 */
15 #include <linux/types.h>
16 typedef __u16 __sum16;
17 #include <arpa/inet.h>
18 #include <linux/if_ether.h>
19 #include <linux/if_packet.h>
21 #include <linux/ipv6.h>
22 #include <linux/filter.h>
23 #include <linux/perf_event.h>
24 #include <linux/socket.h>
25 #include <linux/unistd.h>
27 #include <sys/ioctl.h>
29 #include <sys/types.h>
31 #include <sys/param.h>
34 #include <linux/bpf.h>
35 #include <linux/err.h>
37 #include <bpf/libbpf.h>
39 #include "test_iptunnel_common.h"
41 #include <bpf/bpf_endian.h>
42 #include "trace_helpers.h"
43 #include "testing_helpers.h"
58 struct test_filter_set {
59 struct test_filter *tests;
63 struct test_selector {
64 struct test_filter_set whitelist;
65 struct test_filter_set blacklist;
70 struct subtest_state {
90 struct subtest_state *subtest_states;
99 extern int env_verbosity;
102 struct test_selector test_selector;
103 struct test_selector subtest_selector;
104 struct test_selector tmon_selector;
107 enum verbosity verbosity;
112 bool list_test_names;
114 struct prog_test_def *test; /* current running test */
115 struct test_state *test_state; /* current running test state */
116 struct subtest_state *subtest_state; /* current running subtest state */
123 int succ_cnt; /* successful tests */
124 int sub_succ_cnt; /* successful sub-tests */
125 int fail_cnt; /* total failed tests + sub-tests */
126 int skip_cnt; /* skipped tests */
129 int workers; /* number of worker process */
130 int worker_id; /* id number of current worker, main process is -1 */
131 pid_t *worker_pids; /* array of worker pids */
132 int *worker_socks; /* array of worker socks */
133 int *worker_current_test; /* array of current running test for each worker */
135 pthread_t main_thread;
136 int secs_till_notify;
138 timer_t watchdog; /* watch for stalled tests/subtests */
139 enum { WD_NOTIFY, WD_KILL } watchdog_state;
142 #define MAX_LOG_TRUNK_SIZE 8192
143 #define MAX_SUBTEST_NAME 1024
148 MSG_SUBTEST_DONE = 3,
166 char log_buf[MAX_LOG_TRUNK_SIZE + 1];
171 char name[MAX_SUBTEST_NAME + 1];
180 extern struct test_env env;
182 void test__force_log(void);
183 bool test__start_subtest(const char *name);
184 void test__end_subtest(void);
185 void test__skip(void);
186 void test__fail(void);
187 int test__join_cgroup(const char *path);
188 void hexdump(const char *prefix, const void *buf, size_t len);
190 #define PRINT_FAIL(format...) \
193 fprintf(stdout, "%s:FAIL:%d ", __func__, __LINE__); \
194 fprintf(stdout, ##format); \
197 #define _CHECK(condition, tag, duration, format...) ({ \
198 int __ret = !!(condition); \
199 int __save_errno = errno; \
202 fprintf(stdout, "%s:FAIL:%s ", __func__, tag); \
203 fprintf(stdout, ##format); \
205 fprintf(stdout, "%s:PASS:%s %d nsec\n", \
206 __func__, tag, duration); \
208 errno = __save_errno; \
212 #define CHECK_FAIL(condition) ({ \
213 int __ret = !!(condition); \
214 int __save_errno = errno; \
217 fprintf(stdout, "%s:FAIL:%d\n", __func__, __LINE__); \
219 errno = __save_errno; \
223 #define CHECK(condition, tag, format...) \
224 _CHECK(condition, tag, duration, format)
225 #define CHECK_ATTR(condition, tag, format...) \
226 _CHECK(condition, tag, tattr.duration, format)
228 #define ASSERT_FAIL(fmt, args...) ({ \
229 static int duration = 0; \
230 CHECK(false, "", fmt"\n", ##args); \
234 #define ASSERT_TRUE(actual, name) ({ \
235 static int duration = 0; \
236 bool ___ok = (actual); \
237 CHECK(!___ok, (name), "unexpected %s: got FALSE\n", (name)); \
241 #define ASSERT_FALSE(actual, name) ({ \
242 static int duration = 0; \
243 bool ___ok = !(actual); \
244 CHECK(!___ok, (name), "unexpected %s: got TRUE\n", (name)); \
248 #define ASSERT_EQ(actual, expected, name) ({ \
249 static int duration = 0; \
250 typeof(actual) ___act = (actual); \
251 typeof(expected) ___exp = (expected); \
252 bool ___ok = ___act == ___exp; \
253 CHECK(!___ok, (name), \
254 "unexpected %s: actual %lld != expected %lld\n", \
255 (name), (long long)(___act), (long long)(___exp)); \
259 #define ASSERT_NEQ(actual, expected, name) ({ \
260 static int duration = 0; \
261 typeof(actual) ___act = (actual); \
262 typeof(expected) ___exp = (expected); \
263 bool ___ok = ___act != ___exp; \
264 CHECK(!___ok, (name), \
265 "unexpected %s: actual %lld == expected %lld\n", \
266 (name), (long long)(___act), (long long)(___exp)); \
270 #define ASSERT_LT(actual, expected, name) ({ \
271 static int duration = 0; \
272 typeof(actual) ___act = (actual); \
273 typeof(expected) ___exp = (expected); \
274 bool ___ok = ___act < ___exp; \
275 CHECK(!___ok, (name), \
276 "unexpected %s: actual %lld >= expected %lld\n", \
277 (name), (long long)(___act), (long long)(___exp)); \
281 #define ASSERT_LE(actual, expected, name) ({ \
282 static int duration = 0; \
283 typeof(actual) ___act = (actual); \
284 typeof(expected) ___exp = (expected); \
285 bool ___ok = ___act <= ___exp; \
286 CHECK(!___ok, (name), \
287 "unexpected %s: actual %lld > expected %lld\n", \
288 (name), (long long)(___act), (long long)(___exp)); \
292 #define ASSERT_GT(actual, expected, name) ({ \
293 static int duration = 0; \
294 typeof(actual) ___act = (actual); \
295 typeof(expected) ___exp = (expected); \
296 bool ___ok = ___act > ___exp; \
297 CHECK(!___ok, (name), \
298 "unexpected %s: actual %lld <= expected %lld\n", \
299 (name), (long long)(___act), (long long)(___exp)); \
303 #define ASSERT_GE(actual, expected, name) ({ \
304 static int duration = 0; \
305 typeof(actual) ___act = (actual); \
306 typeof(expected) ___exp = (expected); \
307 bool ___ok = ___act >= ___exp; \
308 CHECK(!___ok, (name), \
309 "unexpected %s: actual %lld < expected %lld\n", \
310 (name), (long long)(___act), (long long)(___exp)); \
314 #define ASSERT_STREQ(actual, expected, name) ({ \
315 static int duration = 0; \
316 const char *___act = actual; \
317 const char *___exp = expected; \
318 bool ___ok = strcmp(___act, ___exp) == 0; \
319 CHECK(!___ok, (name), \
320 "unexpected %s: actual '%s' != expected '%s'\n", \
321 (name), ___act, ___exp); \
325 #define ASSERT_STRNEQ(actual, expected, len, name) ({ \
326 static int duration = 0; \
327 const char *___act = actual; \
328 const char *___exp = expected; \
330 bool ___ok = strncmp(___act, ___exp, ___len) == 0; \
331 CHECK(!___ok, (name), \
332 "unexpected %s: actual '%.*s' != expected '%.*s'\n", \
333 (name), ___len, ___act, ___len, ___exp); \
337 #define ASSERT_HAS_SUBSTR(str, substr, name) ({ \
338 static int duration = 0; \
339 const char *___str = str; \
340 const char *___substr = substr; \
341 bool ___ok = strstr(___str, ___substr) != NULL; \
342 CHECK(!___ok, (name), \
343 "unexpected %s: '%s' is not a substring of '%s'\n", \
344 (name), ___substr, ___str); \
348 #define ASSERT_MEMEQ(actual, expected, len, name) ({ \
349 static int duration = 0; \
350 const void *__act = actual; \
351 const void *__exp = expected; \
353 bool ___ok = memcmp(__act, __exp, __len) == 0; \
354 CHECK(!___ok, (name), "unexpected memory mismatch\n"); \
355 fprintf(stdout, "actual:\n"); \
356 hexdump("\t", __act, __len); \
357 fprintf(stdout, "expected:\n"); \
358 hexdump("\t", __exp, __len); \
362 #define ASSERT_OK(res, name) ({ \
363 static int duration = 0; \
364 long long ___res = (res); \
365 bool ___ok = ___res == 0; \
366 CHECK(!___ok, (name), "unexpected error: %lld (errno %d)\n", \
371 #define ASSERT_ERR(res, name) ({ \
372 static int duration = 0; \
373 long long ___res = (res); \
374 bool ___ok = ___res < 0; \
375 CHECK(!___ok, (name), "unexpected success: %lld\n", ___res); \
379 #define ASSERT_NULL(ptr, name) ({ \
380 static int duration = 0; \
381 const void *___res = (ptr); \
382 bool ___ok = !___res; \
383 CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res); \
387 #define ASSERT_OK_PTR(ptr, name) ({ \
388 static int duration = 0; \
389 const void *___res = (ptr); \
390 int ___err = libbpf_get_error(___res); \
391 bool ___ok = ___err == 0; \
392 CHECK(!___ok, (name), "unexpected error: %d\n", ___err); \
396 #define ASSERT_ERR_PTR(ptr, name) ({ \
397 static int duration = 0; \
398 const void *___res = (ptr); \
399 int ___err = libbpf_get_error(___res); \
400 bool ___ok = ___err != 0; \
401 CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res); \
405 #define ASSERT_OK_FD(fd, name) ({ \
406 static int duration = 0; \
408 bool ___ok = ___fd >= 0; \
409 CHECK(!___ok, (name), "unexpected fd: %d (errno %d)\n", \
414 #define ASSERT_ERR_FD(fd, name) ({ \
415 static int duration = 0; \
417 bool ___ok = ___fd < 0; \
418 CHECK(!___ok, (name), "unexpected fd: %d\n", ___fd); \
422 #define SYS(goto_label, fmt, ...) \
425 snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__); \
426 if (!ASSERT_OK(system(cmd), cmd)) \
430 #define ALL_TO_DEV_NULL " >/dev/null 2>&1"
432 #define SYS_NOFAIL(fmt, ...) \
436 n = snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__); \
437 if (n < sizeof(cmd) && sizeof(cmd) - n >= sizeof(ALL_TO_DEV_NULL)) \
438 strcat(cmd, ALL_TO_DEV_NULL); \
442 int start_libbpf_log_capture(void);
443 char *stop_libbpf_log_capture(void);
445 static inline __u64 ptr_to_u64(const void *ptr)
447 return (__u64) (unsigned long) ptr;
450 static inline void *u64_to_ptr(__u64 ptr)
452 return (void *) (unsigned long) ptr;
455 int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);
456 int compare_map_keys(int map1_fd, int map2_fd);
457 int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
458 int trigger_module_test_read(int read_sz);
459 int trigger_module_test_write(int write_sz);
460 int write_sysctl(const char *sysctl, const char *value);
461 int get_bpf_max_tramp_links_from(struct btf *btf);
462 int get_bpf_max_tramp_links(void);
465 struct netns_obj *netns_new(const char *name, bool open);
466 void netns_free(struct netns_obj *netns);
469 #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
470 #elif defined(__s390x__)
471 #define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep"
472 #elif defined(__aarch64__)
473 #define SYS_NANOSLEEP_KPROBE_NAME "__arm64_sys_nanosleep"
474 #elif defined(__riscv)
475 #define SYS_NANOSLEEP_KPROBE_NAME "__riscv_sys_nanosleep"
477 #define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
480 #define BPF_TESTMOD_TEST_FILE "/sys/kernel/bpf_testmod"
482 typedef int (*pre_execution_cb)(struct bpf_object *obj);
487 pre_execution_cb pre_execution_cb;
489 struct bpf_object *obj;
492 static inline void test_loader__set_pre_execution_cb(struct test_loader *tester,
495 tester->pre_execution_cb = cb;
498 typedef const void *(*skel_elf_bytes_fn)(size_t *sz);
500 extern void test_loader__run_subtests(struct test_loader *tester,
501 const char *skel_name,
502 skel_elf_bytes_fn elf_bytes_factory);
504 extern void test_loader_fini(struct test_loader *tester);
506 #define RUN_TESTS(skel) ({ \
507 struct test_loader tester = {}; \
509 test_loader__run_subtests(&tester, #skel, skel##__elf_bytes); \
510 test_loader_fini(&tester); \
513 #endif /* __TEST_PROGS_H */