1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #include <stdio.h> 3 #include <unistd.h> 4 #include <errno.h> 5 #include <string.h> 6 #include <assert.h> 7 #include <stdlib.h> 8 #include <stdarg.h> 9 #include <time.h> 10 #include <signal.h> 11 12 #include <linux/types.h> 13 typedef __u16 __sum16; 14 #include <arpa/inet.h> 15 #include <linux/if_ether.h> 16 #include <linux/if_packet.h> 17 #include <linux/ip.h> 18 #include <linux/ipv6.h> 19 #include <linux/filter.h> 20 #include <linux/perf_event.h> 21 #include <linux/socket.h> 22 #include <linux/unistd.h> 23 24 #include <sys/ioctl.h> 25 #include <sys/wait.h> 26 #include <sys/types.h> 27 #include <sys/time.h> 28 #include <sys/param.h> 29 #include <fcntl.h> 30 #include <pthread.h> 31 #include <linux/bpf.h> 32 #include <linux/err.h> 33 #include <bpf/bpf.h> 34 #include <bpf/libbpf.h> 35 36 #include "test_iptunnel_common.h" 37 #include "bpf_util.h" 38 #include <bpf/bpf_endian.h> 39 #include "trace_helpers.h" 40 #include "testing_helpers.h" 41 42 enum verbosity { 43 VERBOSE_NONE, 44 VERBOSE_NORMAL, 45 VERBOSE_VERY, 46 VERBOSE_SUPER, 47 }; 48 49 struct test_filter { 50 char *name; 51 char **subtests; 52 int subtest_cnt; 53 }; 54 55 struct test_filter_set { 56 struct test_filter *tests; 57 int cnt; 58 }; 59 60 struct test_selector { 61 struct test_filter_set whitelist; 62 struct test_filter_set blacklist; 63 bool *num_set; 64 int num_set_len; 65 }; 66 67 struct test_state { 68 bool tested; 69 bool force_log; 70 71 int error_cnt; 72 int skip_cnt; 73 int subtest_skip_cnt; 74 int sub_succ_cnt; 75 76 char *subtest_name; 77 int subtest_num; 78 79 /* store counts before subtest started */ 80 int old_error_cnt; 81 82 size_t log_cnt; 83 char *log_buf; 84 }; 85 86 struct test_env { 87 struct test_selector test_selector; 88 struct test_selector subtest_selector; 89 bool verifier_stats; 90 bool debug; 91 enum verbosity verbosity; 92 93 bool jit_enabled; 94 bool has_testmod; 95 bool get_test_cnt; 96 bool list_test_names; 97 98 struct prog_test_def *test; /* current running test */ 99 struct test_state *test_state; /* current running test result */ 100 101 FILE *stdout; 102 FILE *stderr; 103 int nr_cpus; 104 105 int succ_cnt; /* successful tests */ 106 int sub_succ_cnt; /* successful sub-tests */ 107 int fail_cnt; /* total failed tests + sub-tests */ 108 int skip_cnt; /* skipped tests */ 109 110 int saved_netns_fd; 111 int workers; /* number of worker process */ 112 int worker_id; /* id number of current worker, main process is -1 */ 113 pid_t *worker_pids; /* array of worker pids */ 114 int *worker_socks; /* array of worker socks */ 115 int *worker_current_test; /* array of current running test for each worker */ 116 }; 117 118 #define MAX_LOG_TRUNK_SIZE 8192 119 enum msg_type { 120 MSG_DO_TEST = 0, 121 MSG_TEST_DONE = 1, 122 MSG_TEST_LOG = 2, 123 MSG_EXIT = 255, 124 }; 125 struct msg { 126 enum msg_type type; 127 union { 128 struct { 129 int test_num; 130 } do_test; 131 struct { 132 int test_num; 133 int sub_succ_cnt; 134 int error_cnt; 135 int skip_cnt; 136 bool have_log; 137 } test_done; 138 struct { 139 char log_buf[MAX_LOG_TRUNK_SIZE + 1]; 140 bool is_last; 141 } test_log; 142 }; 143 }; 144 145 extern struct test_env env; 146 147 void test__force_log(void); 148 bool test__start_subtest(const char *name); 149 void test__end_subtest(void); 150 void test__skip(void); 151 void test__fail(void); 152 int test__join_cgroup(const char *path); 153 154 #define PRINT_FAIL(format...) \ 155 ({ \ 156 test__fail(); \ 157 fprintf(stdout, "%s:FAIL:%d ", __func__, __LINE__); \ 158 fprintf(stdout, ##format); \ 159 }) 160 161 #define _CHECK(condition, tag, duration, format...) ({ \ 162 int __ret = !!(condition); \ 163 int __save_errno = errno; \ 164 if (__ret) { \ 165 test__fail(); \ 166 fprintf(stdout, "%s:FAIL:%s ", __func__, tag); \ 167 fprintf(stdout, ##format); \ 168 } else { \ 169 fprintf(stdout, "%s:PASS:%s %d nsec\n", \ 170 __func__, tag, duration); \ 171 } \ 172 errno = __save_errno; \ 173 __ret; \ 174 }) 175 176 #define CHECK_FAIL(condition) ({ \ 177 int __ret = !!(condition); \ 178 int __save_errno = errno; \ 179 if (__ret) { \ 180 test__fail(); \ 181 fprintf(stdout, "%s:FAIL:%d\n", __func__, __LINE__); \ 182 } \ 183 errno = __save_errno; \ 184 __ret; \ 185 }) 186 187 #define CHECK(condition, tag, format...) \ 188 _CHECK(condition, tag, duration, format) 189 #define CHECK_ATTR(condition, tag, format...) \ 190 _CHECK(condition, tag, tattr.duration, format) 191 192 #define ASSERT_TRUE(actual, name) ({ \ 193 static int duration = 0; \ 194 bool ___ok = (actual); \ 195 CHECK(!___ok, (name), "unexpected %s: got FALSE\n", (name)); \ 196 ___ok; \ 197 }) 198 199 #define ASSERT_FALSE(actual, name) ({ \ 200 static int duration = 0; \ 201 bool ___ok = !(actual); \ 202 CHECK(!___ok, (name), "unexpected %s: got TRUE\n", (name)); \ 203 ___ok; \ 204 }) 205 206 #define ASSERT_EQ(actual, expected, name) ({ \ 207 static int duration = 0; \ 208 typeof(actual) ___act = (actual); \ 209 typeof(expected) ___exp = (expected); \ 210 bool ___ok = ___act == ___exp; \ 211 CHECK(!___ok, (name), \ 212 "unexpected %s: actual %lld != expected %lld\n", \ 213 (name), (long long)(___act), (long long)(___exp)); \ 214 ___ok; \ 215 }) 216 217 #define ASSERT_NEQ(actual, expected, name) ({ \ 218 static int duration = 0; \ 219 typeof(actual) ___act = (actual); \ 220 typeof(expected) ___exp = (expected); \ 221 bool ___ok = ___act != ___exp; \ 222 CHECK(!___ok, (name), \ 223 "unexpected %s: actual %lld == expected %lld\n", \ 224 (name), (long long)(___act), (long long)(___exp)); \ 225 ___ok; \ 226 }) 227 228 #define ASSERT_LT(actual, expected, name) ({ \ 229 static int duration = 0; \ 230 typeof(actual) ___act = (actual); \ 231 typeof(expected) ___exp = (expected); \ 232 bool ___ok = ___act < ___exp; \ 233 CHECK(!___ok, (name), \ 234 "unexpected %s: actual %lld >= expected %lld\n", \ 235 (name), (long long)(___act), (long long)(___exp)); \ 236 ___ok; \ 237 }) 238 239 #define ASSERT_LE(actual, expected, name) ({ \ 240 static int duration = 0; \ 241 typeof(actual) ___act = (actual); \ 242 typeof(expected) ___exp = (expected); \ 243 bool ___ok = ___act <= ___exp; \ 244 CHECK(!___ok, (name), \ 245 "unexpected %s: actual %lld > expected %lld\n", \ 246 (name), (long long)(___act), (long long)(___exp)); \ 247 ___ok; \ 248 }) 249 250 #define ASSERT_GT(actual, expected, name) ({ \ 251 static int duration = 0; \ 252 typeof(actual) ___act = (actual); \ 253 typeof(expected) ___exp = (expected); \ 254 bool ___ok = ___act > ___exp; \ 255 CHECK(!___ok, (name), \ 256 "unexpected %s: actual %lld <= expected %lld\n", \ 257 (name), (long long)(___act), (long long)(___exp)); \ 258 ___ok; \ 259 }) 260 261 #define ASSERT_GE(actual, expected, name) ({ \ 262 static int duration = 0; \ 263 typeof(actual) ___act = (actual); \ 264 typeof(expected) ___exp = (expected); \ 265 bool ___ok = ___act >= ___exp; \ 266 CHECK(!___ok, (name), \ 267 "unexpected %s: actual %lld < expected %lld\n", \ 268 (name), (long long)(___act), (long long)(___exp)); \ 269 ___ok; \ 270 }) 271 272 #define ASSERT_STREQ(actual, expected, name) ({ \ 273 static int duration = 0; \ 274 const char *___act = actual; \ 275 const char *___exp = expected; \ 276 bool ___ok = strcmp(___act, ___exp) == 0; \ 277 CHECK(!___ok, (name), \ 278 "unexpected %s: actual '%s' != expected '%s'\n", \ 279 (name), ___act, ___exp); \ 280 ___ok; \ 281 }) 282 283 #define ASSERT_STRNEQ(actual, expected, len, name) ({ \ 284 static int duration = 0; \ 285 const char *___act = actual; \ 286 const char *___exp = expected; \ 287 int ___len = len; \ 288 bool ___ok = strncmp(___act, ___exp, ___len) == 0; \ 289 CHECK(!___ok, (name), \ 290 "unexpected %s: actual '%.*s' != expected '%.*s'\n", \ 291 (name), ___len, ___act, ___len, ___exp); \ 292 ___ok; \ 293 }) 294 295 #define ASSERT_HAS_SUBSTR(str, substr, name) ({ \ 296 static int duration = 0; \ 297 const char *___str = str; \ 298 const char *___substr = substr; \ 299 bool ___ok = strstr(___str, ___substr) != NULL; \ 300 CHECK(!___ok, (name), \ 301 "unexpected %s: '%s' is not a substring of '%s'\n", \ 302 (name), ___substr, ___str); \ 303 ___ok; \ 304 }) 305 306 #define ASSERT_OK(res, name) ({ \ 307 static int duration = 0; \ 308 long long ___res = (res); \ 309 bool ___ok = ___res == 0; \ 310 CHECK(!___ok, (name), "unexpected error: %lld (errno %d)\n", \ 311 ___res, errno); \ 312 ___ok; \ 313 }) 314 315 #define ASSERT_ERR(res, name) ({ \ 316 static int duration = 0; \ 317 long long ___res = (res); \ 318 bool ___ok = ___res < 0; \ 319 CHECK(!___ok, (name), "unexpected success: %lld\n", ___res); \ 320 ___ok; \ 321 }) 322 323 #define ASSERT_NULL(ptr, name) ({ \ 324 static int duration = 0; \ 325 const void *___res = (ptr); \ 326 bool ___ok = !___res; \ 327 CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res); \ 328 ___ok; \ 329 }) 330 331 #define ASSERT_OK_PTR(ptr, name) ({ \ 332 static int duration = 0; \ 333 const void *___res = (ptr); \ 334 int ___err = libbpf_get_error(___res); \ 335 bool ___ok = ___err == 0; \ 336 CHECK(!___ok, (name), "unexpected error: %d\n", ___err); \ 337 ___ok; \ 338 }) 339 340 #define ASSERT_ERR_PTR(ptr, name) ({ \ 341 static int duration = 0; \ 342 const void *___res = (ptr); \ 343 int ___err = libbpf_get_error(___res); \ 344 bool ___ok = ___err != 0; \ 345 CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res); \ 346 ___ok; \ 347 }) 348 349 static inline __u64 ptr_to_u64(const void *ptr) 350 { 351 return (__u64) (unsigned long) ptr; 352 } 353 354 static inline void *u64_to_ptr(__u64 ptr) 355 { 356 return (void *) (unsigned long) ptr; 357 } 358 359 int bpf_find_map(const char *test, struct bpf_object *obj, const char *name); 360 int compare_map_keys(int map1_fd, int map2_fd); 361 int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len); 362 int extract_build_id(char *build_id, size_t size); 363 int kern_sync_rcu(void); 364 int trigger_module_test_read(int read_sz); 365 int trigger_module_test_write(int write_sz); 366 367 #ifdef __x86_64__ 368 #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep" 369 #elif defined(__s390x__) 370 #define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep" 371 #elif defined(__aarch64__) 372 #define SYS_NANOSLEEP_KPROBE_NAME "__arm64_sys_nanosleep" 373 #else 374 #define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep" 375 #endif 376 377 #define BPF_TESTMOD_TEST_FILE "/sys/kernel/bpf_testmod" 378