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> 199a365e67SStanislav Fomichev #include <netinet/tcp.h> 203f306588SStanislav Fomichev #include <linux/filter.h> 213f306588SStanislav Fomichev #include <linux/perf_event.h> 229a365e67SStanislav Fomichev #include <linux/socket.h> 233f306588SStanislav Fomichev #include <linux/unistd.h> 243f306588SStanislav Fomichev 253f306588SStanislav Fomichev #include <sys/ioctl.h> 263f306588SStanislav Fomichev #include <sys/wait.h> 273f306588SStanislav Fomichev #include <sys/types.h> 283f306588SStanislav Fomichev #include <sys/time.h> 293f306588SStanislav Fomichev #include <fcntl.h> 303f306588SStanislav Fomichev #include <pthread.h> 313f306588SStanislav Fomichev #include <linux/bpf.h> 323f306588SStanislav Fomichev #include <linux/err.h> 333f306588SStanislav Fomichev #include <bpf/bpf.h> 343f306588SStanislav Fomichev #include <bpf/libbpf.h> 353f306588SStanislav Fomichev 363f306588SStanislav Fomichev #include "test_iptunnel_common.h" 373f306588SStanislav Fomichev #include "bpf_util.h" 383e689141SToke Høiland-Jørgensen #include <bpf/bpf_endian.h> 393f306588SStanislav Fomichev #include "trace_helpers.h" 40cd49291cSAndrii Nakryiko #include "testing_helpers.h" 413f306588SStanislav Fomichev #include "flow_dissector_load.h" 423f306588SStanislav Fomichev 43a8fdaad5SAndrii Nakryiko enum verbosity { 44a8fdaad5SAndrii Nakryiko VERBOSE_NONE, 45a8fdaad5SAndrii Nakryiko VERBOSE_NORMAL, 46a8fdaad5SAndrii Nakryiko VERBOSE_VERY, 47a8fdaad5SAndrii Nakryiko VERBOSE_SUPER, 48a8fdaad5SAndrii Nakryiko }; 49a8fdaad5SAndrii Nakryiko 50b65053cdSAndrii Nakryiko struct str_set { 51b65053cdSAndrii Nakryiko const char **strs; 52b65053cdSAndrii Nakryiko int cnt; 53b65053cdSAndrii Nakryiko }; 54b65053cdSAndrii Nakryiko 553a516a0aSAndrii Nakryiko struct test_selector { 56b65053cdSAndrii Nakryiko struct str_set whitelist; 57b65053cdSAndrii Nakryiko struct str_set blacklist; 583a516a0aSAndrii Nakryiko bool *num_set; 593a516a0aSAndrii Nakryiko int num_set_len; 603a516a0aSAndrii Nakryiko }; 613a516a0aSAndrii Nakryiko 620ff97e56SAndrii Nakryiko struct test_env { 633a516a0aSAndrii Nakryiko struct test_selector test_selector; 643a516a0aSAndrii Nakryiko struct test_selector subtest_selector; 650ff97e56SAndrii Nakryiko bool verifier_stats; 66a8fdaad5SAndrii Nakryiko enum verbosity verbosity; 670ff97e56SAndrii Nakryiko 680ff97e56SAndrii Nakryiko bool jit_enabled; 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 13322ba3635SAndrii Nakryiko #define ASSERT_EQ(actual, expected, name) ({ \ 13422ba3635SAndrii Nakryiko static int duration = 0; \ 13522ba3635SAndrii Nakryiko typeof(actual) ___act = (actual); \ 13622ba3635SAndrii Nakryiko typeof(expected) ___exp = (expected); \ 13722ba3635SAndrii Nakryiko bool ___ok = ___act == ___exp; \ 13822ba3635SAndrii Nakryiko CHECK(!___ok, (name), \ 13922ba3635SAndrii Nakryiko "unexpected %s: actual %lld != expected %lld\n", \ 14022ba3635SAndrii Nakryiko (name), (long long)(___act), (long long)(___exp)); \ 14122ba3635SAndrii Nakryiko ___ok; \ 14222ba3635SAndrii Nakryiko }) 14322ba3635SAndrii Nakryiko 14422ba3635SAndrii Nakryiko #define ASSERT_STREQ(actual, expected, name) ({ \ 14522ba3635SAndrii Nakryiko static int duration = 0; \ 14622ba3635SAndrii Nakryiko const char *___act = actual; \ 14722ba3635SAndrii Nakryiko const char *___exp = expected; \ 14822ba3635SAndrii Nakryiko bool ___ok = strcmp(___act, ___exp) == 0; \ 14922ba3635SAndrii Nakryiko CHECK(!___ok, (name), \ 15022ba3635SAndrii Nakryiko "unexpected %s: actual '%s' != expected '%s'\n", \ 15122ba3635SAndrii Nakryiko (name), ___act, ___exp); \ 15222ba3635SAndrii Nakryiko ___ok; \ 15322ba3635SAndrii Nakryiko }) 15422ba3635SAndrii Nakryiko 15522ba3635SAndrii Nakryiko #define ASSERT_OK(res, name) ({ \ 15622ba3635SAndrii Nakryiko static int duration = 0; \ 15722ba3635SAndrii Nakryiko long long ___res = (res); \ 15822ba3635SAndrii Nakryiko bool ___ok = ___res == 0; \ 15922ba3635SAndrii Nakryiko CHECK(!___ok, (name), "unexpected error: %lld\n", ___res); \ 16022ba3635SAndrii Nakryiko ___ok; \ 16122ba3635SAndrii Nakryiko }) 16222ba3635SAndrii Nakryiko 16322ba3635SAndrii Nakryiko #define ASSERT_ERR(res, name) ({ \ 16422ba3635SAndrii Nakryiko static int duration = 0; \ 16522ba3635SAndrii Nakryiko long long ___res = (res); \ 16622ba3635SAndrii Nakryiko bool ___ok = ___res < 0; \ 16722ba3635SAndrii Nakryiko CHECK(!___ok, (name), "unexpected success: %lld\n", ___res); \ 16822ba3635SAndrii Nakryiko ___ok; \ 16922ba3635SAndrii Nakryiko }) 17022ba3635SAndrii Nakryiko 17122ba3635SAndrii Nakryiko #define ASSERT_NULL(ptr, name) ({ \ 17222ba3635SAndrii Nakryiko static int duration = 0; \ 17322ba3635SAndrii Nakryiko const void *___res = (ptr); \ 17422ba3635SAndrii Nakryiko bool ___ok = !___res; \ 17522ba3635SAndrii Nakryiko CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res); \ 17622ba3635SAndrii Nakryiko ___ok; \ 17722ba3635SAndrii Nakryiko }) 17822ba3635SAndrii Nakryiko 17922ba3635SAndrii Nakryiko #define ASSERT_OK_PTR(ptr, name) ({ \ 18022ba3635SAndrii Nakryiko static int duration = 0; \ 18122ba3635SAndrii Nakryiko const void *___res = (ptr); \ 18222ba3635SAndrii Nakryiko bool ___ok = !IS_ERR_OR_NULL(___res); \ 18322ba3635SAndrii Nakryiko CHECK(!___ok, (name), \ 18422ba3635SAndrii Nakryiko "unexpected error: %ld\n", PTR_ERR(___res)); \ 18522ba3635SAndrii Nakryiko ___ok; \ 18622ba3635SAndrii Nakryiko }) 18722ba3635SAndrii Nakryiko 18822ba3635SAndrii Nakryiko #define ASSERT_ERR_PTR(ptr, name) ({ \ 18922ba3635SAndrii Nakryiko static int duration = 0; \ 19022ba3635SAndrii Nakryiko const void *___res = (ptr); \ 19122ba3635SAndrii Nakryiko bool ___ok = IS_ERR(___res) \ 19222ba3635SAndrii Nakryiko CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res); \ 19322ba3635SAndrii Nakryiko ___ok; \ 19422ba3635SAndrii Nakryiko }) 19522ba3635SAndrii Nakryiko 196886225bbSStanislav Fomichev static inline __u64 ptr_to_u64(const void *ptr) 197886225bbSStanislav Fomichev { 198886225bbSStanislav Fomichev return (__u64) (unsigned long) ptr; 199886225bbSStanislav Fomichev } 200886225bbSStanislav Fomichev 2019028bbccSAndrii Nakryiko static inline void *u64_to_ptr(__u64 ptr) 2029028bbccSAndrii Nakryiko { 2039028bbccSAndrii Nakryiko return (void *) (unsigned long) ptr; 2049028bbccSAndrii Nakryiko } 2059028bbccSAndrii Nakryiko 2063f306588SStanislav Fomichev int bpf_find_map(const char *test, struct bpf_object *obj, const char *name); 207615741d8SStanislav Fomichev int compare_map_keys(int map1_fd, int map2_fd); 208615741d8SStanislav Fomichev int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len); 209615741d8SStanislav Fomichev int extract_build_id(char *build_id, size_t size); 2101cb59a60SIlya Leoshkevich 2111cb59a60SIlya Leoshkevich #ifdef __x86_64__ 2121cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep" 2131cb59a60SIlya Leoshkevich #elif defined(__s390x__) 2141cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep" 2151cb59a60SIlya Leoshkevich #else 2161cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep" 2171cb59a60SIlya Leoshkevich #endif 218