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/tcp.h> 20 #include <linux/filter.h> 21 #include <linux/perf_event.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 <fcntl.h> 29 #include <pthread.h> 30 #include <linux/bpf.h> 31 #include <linux/err.h> 32 #include <bpf/bpf.h> 33 #include <bpf/libbpf.h> 34 35 #include "test_iptunnel_common.h" 36 #include "bpf_util.h" 37 #include "bpf_endian.h" 38 #include "trace_helpers.h" 39 #include "flow_dissector_load.h" 40 41 extern int error_cnt, pass_cnt; 42 extern bool jit_enabled; 43 extern bool verifier_stats; 44 45 #define MAGIC_BYTES 123 46 47 /* ipv4 test vector */ 48 struct ipv4_packet { 49 struct ethhdr eth; 50 struct iphdr iph; 51 struct tcphdr tcp; 52 } __packed; 53 extern struct ipv4_packet pkt_v4; 54 55 /* ipv6 test vector */ 56 struct ipv6_packet { 57 struct ethhdr eth; 58 struct ipv6hdr iph; 59 struct tcphdr tcp; 60 } __packed; 61 extern struct ipv6_packet pkt_v6; 62 63 #define _CHECK(condition, tag, duration, format...) ({ \ 64 int __ret = !!(condition); \ 65 if (__ret) { \ 66 error_cnt++; \ 67 printf("%s:FAIL:%s ", __func__, tag); \ 68 printf(format); \ 69 } else { \ 70 pass_cnt++; \ 71 printf("%s:PASS:%s %d nsec\n", __func__, tag, duration);\ 72 } \ 73 __ret; \ 74 }) 75 76 #define CHECK(condition, tag, format...) \ 77 _CHECK(condition, tag, duration, format) 78 #define CHECK_ATTR(condition, tag, format...) \ 79 _CHECK(condition, tag, tattr.duration, format) 80 81 #define MAGIC_VAL 0x1234 82 #define NUM_ITER 100000 83 #define VIP_NUM 5 84 85 static inline __u64 ptr_to_u64(const void *ptr) 86 { 87 return (__u64) (unsigned long) ptr; 88 } 89 90 int bpf_find_map(const char *test, struct bpf_object *obj, const char *name); 91 int compare_map_keys(int map1_fd, int map2_fd); 92 int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len); 93 int extract_build_id(char *build_id, size_t size); 94 void *spin_lock_thread(void *arg); 95 96 #ifdef __x86_64__ 97 #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep" 98 #elif defined(__s390x__) 99 #define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep" 100 #else 101 #define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep" 102 #endif 103