13f306588SStanislav Fomichev /* SPDX-License-Identifier: GPL-2.0 */ 23f306588SStanislav Fomichev #include <stdio.h> 33f306588SStanislav Fomichev #include <unistd.h> 43f306588SStanislav Fomichev #include <errno.h> 53f306588SStanislav Fomichev #include <string.h> 63f306588SStanislav Fomichev #include <assert.h> 73f306588SStanislav Fomichev #include <stdlib.h> 83f306588SStanislav Fomichev #include <stdarg.h> 93f306588SStanislav Fomichev #include <time.h> 103f306588SStanislav Fomichev #include <signal.h> 113f306588SStanislav Fomichev 123f306588SStanislav Fomichev #include <linux/types.h> 133f306588SStanislav Fomichev typedef __u16 __sum16; 143f306588SStanislav Fomichev #include <arpa/inet.h> 153f306588SStanislav Fomichev #include <linux/if_ether.h> 163f306588SStanislav Fomichev #include <linux/if_packet.h> 173f306588SStanislav Fomichev #include <linux/ip.h> 183f306588SStanislav Fomichev #include <linux/ipv6.h> 193f306588SStanislav Fomichev #include <linux/filter.h> 203f306588SStanislav Fomichev #include <linux/perf_event.h> 219a365e67SStanislav Fomichev #include <linux/socket.h> 223f306588SStanislav Fomichev #include <linux/unistd.h> 233f306588SStanislav Fomichev 243f306588SStanislav Fomichev #include <sys/ioctl.h> 253f306588SStanislav Fomichev #include <sys/wait.h> 263f306588SStanislav Fomichev #include <sys/types.h> 273f306588SStanislav Fomichev #include <sys/time.h> 283f306588SStanislav Fomichev #include <fcntl.h> 293f306588SStanislav Fomichev #include <pthread.h> 303f306588SStanislav Fomichev #include <linux/bpf.h> 313f306588SStanislav Fomichev #include <linux/err.h> 323f306588SStanislav Fomichev #include <bpf/bpf.h> 333f306588SStanislav Fomichev #include <bpf/libbpf.h> 343f306588SStanislav Fomichev 353f306588SStanislav Fomichev #include "test_iptunnel_common.h" 363f306588SStanislav Fomichev #include "bpf_util.h" 373e689141SToke Høiland-Jørgensen #include <bpf/bpf_endian.h> 383f306588SStanislav Fomichev #include "trace_helpers.h" 39cd49291cSAndrii Nakryiko #include "testing_helpers.h" 403f306588SStanislav Fomichev #include "flow_dissector_load.h" 413f306588SStanislav Fomichev 42a8fdaad5SAndrii Nakryiko enum verbosity { 43a8fdaad5SAndrii Nakryiko VERBOSE_NONE, 44a8fdaad5SAndrii Nakryiko VERBOSE_NORMAL, 45a8fdaad5SAndrii Nakryiko VERBOSE_VERY, 46a8fdaad5SAndrii Nakryiko VERBOSE_SUPER, 47a8fdaad5SAndrii Nakryiko }; 48a8fdaad5SAndrii Nakryiko 49b65053cdSAndrii Nakryiko struct str_set { 50b65053cdSAndrii Nakryiko const char **strs; 51b65053cdSAndrii Nakryiko int cnt; 52b65053cdSAndrii Nakryiko }; 53b65053cdSAndrii Nakryiko 543a516a0aSAndrii Nakryiko struct test_selector { 55b65053cdSAndrii Nakryiko struct str_set whitelist; 56b65053cdSAndrii Nakryiko struct str_set blacklist; 573a516a0aSAndrii Nakryiko bool *num_set; 583a516a0aSAndrii Nakryiko int num_set_len; 593a516a0aSAndrii Nakryiko }; 603a516a0aSAndrii Nakryiko 610ff97e56SAndrii Nakryiko struct test_env { 623a516a0aSAndrii Nakryiko struct test_selector test_selector; 633a516a0aSAndrii Nakryiko struct test_selector subtest_selector; 640ff97e56SAndrii Nakryiko bool verifier_stats; 65a8fdaad5SAndrii Nakryiko enum verbosity verbosity; 660ff97e56SAndrii Nakryiko 670ff97e56SAndrii Nakryiko bool jit_enabled; 689f7fa225SAndrii Nakryiko bool has_testmod; 69643e7233SJesper Dangaard Brouer bool get_test_cnt; 70c1f1f365SJesper Dangaard Brouer bool list_test_names; 710ff97e56SAndrii Nakryiko 720ff97e56SAndrii Nakryiko struct prog_test_def *test; 73946152b3SStanislav Fomichev FILE *stdout; 74946152b3SStanislav Fomichev FILE *stderr; 750ff97e56SAndrii Nakryiko char *log_buf; 760ff97e56SAndrii Nakryiko size_t log_cnt; 77fd27b183SAndrii Nakryiko int nr_cpus; 780ff97e56SAndrii Nakryiko 793a516a0aSAndrii Nakryiko int succ_cnt; /* successful tests */ 803a516a0aSAndrii Nakryiko int sub_succ_cnt; /* successful sub-tests */ 813a516a0aSAndrii Nakryiko int fail_cnt; /* total failed tests + sub-tests */ 82cd9c21d7SStanislav Fomichev int skip_cnt; /* skipped tests */ 83811d7e37SMartin KaFai Lau 84811d7e37SMartin KaFai Lau int saved_netns_fd; 850ff97e56SAndrii Nakryiko }; 860ff97e56SAndrii Nakryiko 870ff97e56SAndrii Nakryiko extern struct test_env env; 880ff97e56SAndrii Nakryiko 890ff97e56SAndrii Nakryiko extern void test__force_log(); 903a516a0aSAndrii Nakryiko extern bool test__start_subtest(const char *name); 91cd9c21d7SStanislav Fomichev extern void test__skip(void); 92d38835b7SStanislav Fomichev extern void test__fail(void); 9388dadc63SStanislav Fomichev extern int test__join_cgroup(const char *path); 943f306588SStanislav Fomichev 9523458901SLorenz Bauer #define PRINT_FAIL(format...) \ 9623458901SLorenz Bauer ({ \ 9723458901SLorenz Bauer test__fail(); \ 9823458901SLorenz Bauer fprintf(stdout, "%s:FAIL:%d ", __func__, __LINE__); \ 9923458901SLorenz Bauer fprintf(stdout, ##format); \ 10023458901SLorenz Bauer }) 10123458901SLorenz Bauer 1023f306588SStanislav Fomichev #define _CHECK(condition, tag, duration, format...) ({ \ 1033f306588SStanislav Fomichev int __ret = !!(condition); \ 104478bee0dSAndrey Ignatov int __save_errno = errno; \ 1053f306588SStanislav Fomichev if (__ret) { \ 106d38835b7SStanislav Fomichev test__fail(); \ 1073e2671fbSAndrii Nakryiko fprintf(stdout, "%s:FAIL:%s ", __func__, tag); \ 1083e2671fbSAndrii Nakryiko fprintf(stdout, ##format); \ 1093f306588SStanislav Fomichev } else { \ 1103e2671fbSAndrii Nakryiko fprintf(stdout, "%s:PASS:%s %d nsec\n", \ 1110ff97e56SAndrii Nakryiko __func__, tag, duration); \ 1123f306588SStanislav Fomichev } \ 113478bee0dSAndrey Ignatov errno = __save_errno; \ 1143f306588SStanislav Fomichev __ret; \ 1153f306588SStanislav Fomichev }) 1163f306588SStanislav Fomichev 117d38835b7SStanislav Fomichev #define CHECK_FAIL(condition) ({ \ 118d38835b7SStanislav Fomichev int __ret = !!(condition); \ 119478bee0dSAndrey Ignatov int __save_errno = errno; \ 120d38835b7SStanislav Fomichev if (__ret) { \ 121d38835b7SStanislav Fomichev test__fail(); \ 1223e2671fbSAndrii Nakryiko fprintf(stdout, "%s:FAIL:%d\n", __func__, __LINE__); \ 123d38835b7SStanislav Fomichev } \ 124478bee0dSAndrey Ignatov errno = __save_errno; \ 125d38835b7SStanislav Fomichev __ret; \ 126d38835b7SStanislav Fomichev }) 127d38835b7SStanislav Fomichev 1283f306588SStanislav Fomichev #define CHECK(condition, tag, format...) \ 1293f306588SStanislav Fomichev _CHECK(condition, tag, duration, format) 1303f306588SStanislav Fomichev #define CHECK_ATTR(condition, tag, format...) \ 1313f306588SStanislav Fomichev _CHECK(condition, tag, tattr.duration, format) 1323f306588SStanislav Fomichev 1337a2fa70aSAndrii Nakryiko #define ASSERT_TRUE(actual, name) ({ \ 1347a2fa70aSAndrii Nakryiko static int duration = 0; \ 1357a2fa70aSAndrii Nakryiko bool ___ok = (actual); \ 1367a2fa70aSAndrii Nakryiko CHECK(!___ok, (name), "unexpected %s: got FALSE\n", (name)); \ 1377a2fa70aSAndrii Nakryiko ___ok; \ 1387a2fa70aSAndrii Nakryiko }) 1397a2fa70aSAndrii Nakryiko 1407a2fa70aSAndrii Nakryiko #define ASSERT_FALSE(actual, name) ({ \ 1417a2fa70aSAndrii Nakryiko static int duration = 0; \ 1427a2fa70aSAndrii Nakryiko bool ___ok = !(actual); \ 1437a2fa70aSAndrii Nakryiko CHECK(!___ok, (name), "unexpected %s: got TRUE\n", (name)); \ 1447a2fa70aSAndrii Nakryiko ___ok; \ 1457a2fa70aSAndrii Nakryiko }) 1467a2fa70aSAndrii Nakryiko 14722ba3635SAndrii Nakryiko #define ASSERT_EQ(actual, expected, name) ({ \ 14822ba3635SAndrii Nakryiko static int duration = 0; \ 14922ba3635SAndrii Nakryiko typeof(actual) ___act = (actual); \ 15022ba3635SAndrii Nakryiko typeof(expected) ___exp = (expected); \ 15122ba3635SAndrii Nakryiko bool ___ok = ___act == ___exp; \ 15222ba3635SAndrii Nakryiko CHECK(!___ok, (name), \ 15322ba3635SAndrii Nakryiko "unexpected %s: actual %lld != expected %lld\n", \ 15422ba3635SAndrii Nakryiko (name), (long long)(___act), (long long)(___exp)); \ 15522ba3635SAndrii Nakryiko ___ok; \ 15622ba3635SAndrii Nakryiko }) 15722ba3635SAndrii Nakryiko 158197389daSAndrii Nakryiko #define ASSERT_NEQ(actual, expected, name) ({ \ 159197389daSAndrii Nakryiko static int duration = 0; \ 160197389daSAndrii Nakryiko typeof(actual) ___act = (actual); \ 161197389daSAndrii Nakryiko typeof(expected) ___exp = (expected); \ 162197389daSAndrii Nakryiko bool ___ok = ___act != ___exp; \ 163197389daSAndrii Nakryiko CHECK(!___ok, (name), \ 164197389daSAndrii Nakryiko "unexpected %s: actual %lld == expected %lld\n", \ 165197389daSAndrii Nakryiko (name), (long long)(___act), (long long)(___exp)); \ 166197389daSAndrii Nakryiko ___ok; \ 167197389daSAndrii Nakryiko }) 168197389daSAndrii Nakryiko 1699de7f0fdSYonghong Song #define ASSERT_LT(actual, expected, name) ({ \ 1709de7f0fdSYonghong Song static int duration = 0; \ 1719de7f0fdSYonghong Song typeof(actual) ___act = (actual); \ 1729de7f0fdSYonghong Song typeof(expected) ___exp = (expected); \ 1739de7f0fdSYonghong Song bool ___ok = ___act < ___exp; \ 1749de7f0fdSYonghong Song CHECK(!___ok, (name), \ 1759de7f0fdSYonghong Song "unexpected %s: actual %lld >= expected %lld\n", \ 1769de7f0fdSYonghong Song (name), (long long)(___act), (long long)(___exp)); \ 1779de7f0fdSYonghong Song ___ok; \ 1789de7f0fdSYonghong Song }) 1799de7f0fdSYonghong Song 1807a2fa70aSAndrii Nakryiko #define ASSERT_LE(actual, expected, name) ({ \ 1817a2fa70aSAndrii Nakryiko static int duration = 0; \ 1827a2fa70aSAndrii Nakryiko typeof(actual) ___act = (actual); \ 1837a2fa70aSAndrii Nakryiko typeof(expected) ___exp = (expected); \ 1847a2fa70aSAndrii Nakryiko bool ___ok = ___act <= ___exp; \ 1857a2fa70aSAndrii Nakryiko CHECK(!___ok, (name), \ 1867a2fa70aSAndrii Nakryiko "unexpected %s: actual %lld > expected %lld\n", \ 1877a2fa70aSAndrii Nakryiko (name), (long long)(___act), (long long)(___exp)); \ 1887a2fa70aSAndrii Nakryiko ___ok; \ 1897a2fa70aSAndrii Nakryiko }) 1907a2fa70aSAndrii Nakryiko 1917a2fa70aSAndrii Nakryiko #define ASSERT_GT(actual, expected, name) ({ \ 1927a2fa70aSAndrii Nakryiko static int duration = 0; \ 1937a2fa70aSAndrii Nakryiko typeof(actual) ___act = (actual); \ 1947a2fa70aSAndrii Nakryiko typeof(expected) ___exp = (expected); \ 1957a2fa70aSAndrii Nakryiko bool ___ok = ___act > ___exp; \ 1967a2fa70aSAndrii Nakryiko CHECK(!___ok, (name), \ 1977a2fa70aSAndrii Nakryiko "unexpected %s: actual %lld <= expected %lld\n", \ 1987a2fa70aSAndrii Nakryiko (name), (long long)(___act), (long long)(___exp)); \ 1997a2fa70aSAndrii Nakryiko ___ok; \ 2007a2fa70aSAndrii Nakryiko }) 2017a2fa70aSAndrii Nakryiko 2027a2fa70aSAndrii Nakryiko #define ASSERT_GE(actual, expected, name) ({ \ 2037a2fa70aSAndrii Nakryiko static int duration = 0; \ 2047a2fa70aSAndrii Nakryiko typeof(actual) ___act = (actual); \ 2057a2fa70aSAndrii Nakryiko typeof(expected) ___exp = (expected); \ 2067a2fa70aSAndrii Nakryiko bool ___ok = ___act >= ___exp; \ 2077a2fa70aSAndrii Nakryiko CHECK(!___ok, (name), \ 2087a2fa70aSAndrii Nakryiko "unexpected %s: actual %lld < expected %lld\n", \ 2097a2fa70aSAndrii Nakryiko (name), (long long)(___act), (long long)(___exp)); \ 2107a2fa70aSAndrii Nakryiko ___ok; \ 2117a2fa70aSAndrii Nakryiko }) 2127a2fa70aSAndrii Nakryiko 21322ba3635SAndrii Nakryiko #define ASSERT_STREQ(actual, expected, name) ({ \ 21422ba3635SAndrii Nakryiko static int duration = 0; \ 21522ba3635SAndrii Nakryiko const char *___act = actual; \ 21622ba3635SAndrii Nakryiko const char *___exp = expected; \ 21722ba3635SAndrii Nakryiko bool ___ok = strcmp(___act, ___exp) == 0; \ 21822ba3635SAndrii Nakryiko CHECK(!___ok, (name), \ 21922ba3635SAndrii Nakryiko "unexpected %s: actual '%s' != expected '%s'\n", \ 22022ba3635SAndrii Nakryiko (name), ___act, ___exp); \ 22122ba3635SAndrii Nakryiko ___ok; \ 22222ba3635SAndrii Nakryiko }) 22322ba3635SAndrii Nakryiko 224*17283337SAlan Maguire #define ASSERT_STRNEQ(actual, expected, len, name) ({ \ 225*17283337SAlan Maguire static int duration = 0; \ 226*17283337SAlan Maguire const char *___act = actual; \ 227*17283337SAlan Maguire const char *___exp = expected; \ 228*17283337SAlan Maguire int ___len = len; \ 229*17283337SAlan Maguire bool ___ok = strncmp(___act, ___exp, ___len) == 0; \ 230*17283337SAlan Maguire CHECK(!___ok, (name), \ 231*17283337SAlan Maguire "unexpected %s: actual '%.*s' != expected '%.*s'\n", \ 232*17283337SAlan Maguire (name), ___len, ___act, ___len, ___exp); \ 233*17283337SAlan Maguire ___ok; \ 234*17283337SAlan Maguire }) 235*17283337SAlan Maguire 23622ba3635SAndrii Nakryiko #define ASSERT_OK(res, name) ({ \ 23722ba3635SAndrii Nakryiko static int duration = 0; \ 23822ba3635SAndrii Nakryiko long long ___res = (res); \ 23922ba3635SAndrii Nakryiko bool ___ok = ___res == 0; \ 2407a2fa70aSAndrii Nakryiko CHECK(!___ok, (name), "unexpected error: %lld (errno %d)\n", \ 2417a2fa70aSAndrii Nakryiko ___res, errno); \ 24222ba3635SAndrii Nakryiko ___ok; \ 24322ba3635SAndrii Nakryiko }) 24422ba3635SAndrii Nakryiko 24522ba3635SAndrii Nakryiko #define ASSERT_ERR(res, name) ({ \ 24622ba3635SAndrii Nakryiko static int duration = 0; \ 24722ba3635SAndrii Nakryiko long long ___res = (res); \ 24822ba3635SAndrii Nakryiko bool ___ok = ___res < 0; \ 24922ba3635SAndrii Nakryiko CHECK(!___ok, (name), "unexpected success: %lld\n", ___res); \ 25022ba3635SAndrii Nakryiko ___ok; \ 25122ba3635SAndrii Nakryiko }) 25222ba3635SAndrii Nakryiko 25322ba3635SAndrii Nakryiko #define ASSERT_NULL(ptr, name) ({ \ 25422ba3635SAndrii Nakryiko static int duration = 0; \ 25522ba3635SAndrii Nakryiko const void *___res = (ptr); \ 25622ba3635SAndrii Nakryiko bool ___ok = !___res; \ 25722ba3635SAndrii Nakryiko CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res); \ 25822ba3635SAndrii Nakryiko ___ok; \ 25922ba3635SAndrii Nakryiko }) 26022ba3635SAndrii Nakryiko 26122ba3635SAndrii Nakryiko #define ASSERT_OK_PTR(ptr, name) ({ \ 26222ba3635SAndrii Nakryiko static int duration = 0; \ 26322ba3635SAndrii Nakryiko const void *___res = (ptr); \ 264bad2e478SAndrii Nakryiko int ___err = libbpf_get_error(___res); \ 265bad2e478SAndrii Nakryiko bool ___ok = ___err == 0; \ 266bad2e478SAndrii Nakryiko CHECK(!___ok, (name), "unexpected error: %d\n", ___err); \ 26722ba3635SAndrii Nakryiko ___ok; \ 26822ba3635SAndrii Nakryiko }) 26922ba3635SAndrii Nakryiko 27022ba3635SAndrii Nakryiko #define ASSERT_ERR_PTR(ptr, name) ({ \ 27122ba3635SAndrii Nakryiko static int duration = 0; \ 27222ba3635SAndrii Nakryiko const void *___res = (ptr); \ 273bad2e478SAndrii Nakryiko int ___err = libbpf_get_error(___res); \ 274bad2e478SAndrii Nakryiko bool ___ok = ___err != 0; \ 27522ba3635SAndrii Nakryiko CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res); \ 27622ba3635SAndrii Nakryiko ___ok; \ 27722ba3635SAndrii Nakryiko }) 27822ba3635SAndrii Nakryiko 279886225bbSStanislav Fomichev static inline __u64 ptr_to_u64(const void *ptr) 280886225bbSStanislav Fomichev { 281886225bbSStanislav Fomichev return (__u64) (unsigned long) ptr; 282886225bbSStanislav Fomichev } 283886225bbSStanislav Fomichev 2849028bbccSAndrii Nakryiko static inline void *u64_to_ptr(__u64 ptr) 2859028bbccSAndrii Nakryiko { 2869028bbccSAndrii Nakryiko return (void *) (unsigned long) ptr; 2879028bbccSAndrii Nakryiko } 2889028bbccSAndrii Nakryiko 2893f306588SStanislav Fomichev int bpf_find_map(const char *test, struct bpf_object *obj, const char *name); 290615741d8SStanislav Fomichev int compare_map_keys(int map1_fd, int map2_fd); 291615741d8SStanislav Fomichev int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len); 292615741d8SStanislav Fomichev int extract_build_id(char *build_id, size_t size); 293635599baSAndrii Nakryiko int kern_sync_rcu(void); 2941cb59a60SIlya Leoshkevich 2951cb59a60SIlya Leoshkevich #ifdef __x86_64__ 2961cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep" 2971cb59a60SIlya Leoshkevich #elif defined(__s390x__) 2981cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep" 2991cb59a60SIlya Leoshkevich #else 3001cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep" 3011cb59a60SIlya Leoshkevich #endif 302