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 <netinet/tcp.h>
20 #include <linux/filter.h>
21 #include <linux/perf_event.h>
22 #include <linux/socket.h>
23 #include <linux/unistd.h>
24 
25 #include <sys/ioctl.h>
26 #include <sys/wait.h>
27 #include <sys/types.h>
28 #include <sys/time.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_endian.h"
39 #include "trace_helpers.h"
40 #include "flow_dissector_load.h"
41 
42 enum verbosity {
43 	VERBOSE_NONE,
44 	VERBOSE_NORMAL,
45 	VERBOSE_VERY,
46 	VERBOSE_SUPER,
47 };
48 
49 struct test_selector {
50 	const char *name;
51 	bool *num_set;
52 	int num_set_len;
53 };
54 
55 struct test_env {
56 	struct test_selector test_selector;
57 	struct test_selector subtest_selector;
58 	bool verifier_stats;
59 	enum verbosity verbosity;
60 
61 	bool jit_enabled;
62 
63 	struct prog_test_def *test;
64 	FILE *stdout;
65 	FILE *stderr;
66 	char *log_buf;
67 	size_t log_cnt;
68 
69 	int succ_cnt; /* successful tests */
70 	int sub_succ_cnt; /* successful sub-tests */
71 	int fail_cnt; /* total failed tests + sub-tests */
72 	int skip_cnt; /* skipped tests */
73 };
74 
75 extern struct test_env env;
76 
77 extern void test__force_log();
78 extern bool test__start_subtest(const char *name);
79 extern void test__skip(void);
80 extern void test__fail(void);
81 extern int test__join_cgroup(const char *path);
82 
83 #define MAGIC_BYTES 123
84 
85 /* ipv4 test vector */
86 struct ipv4_packet {
87 	struct ethhdr eth;
88 	struct iphdr iph;
89 	struct tcphdr tcp;
90 } __packed;
91 extern struct ipv4_packet pkt_v4;
92 
93 /* ipv6 test vector */
94 struct ipv6_packet {
95 	struct ethhdr eth;
96 	struct ipv6hdr iph;
97 	struct tcphdr tcp;
98 } __packed;
99 extern struct ipv6_packet pkt_v6;
100 
101 #define _CHECK(condition, tag, duration, format...) ({			\
102 	int __ret = !!(condition);					\
103 	if (__ret) {							\
104 		test__fail();						\
105 		printf("%s:FAIL:%s ", __func__, tag);			\
106 		printf(format);						\
107 	} else {							\
108 		printf("%s:PASS:%s %d nsec\n",				\
109 		       __func__, tag, duration);			\
110 	}								\
111 	__ret;								\
112 })
113 
114 #define CHECK_FAIL(condition) ({					\
115 	int __ret = !!(condition);					\
116 	if (__ret) {							\
117 		test__fail();						\
118 		printf("%s:FAIL:%d\n", __func__, __LINE__);		\
119 	}								\
120 	__ret;								\
121 })
122 
123 #define CHECK(condition, tag, format...) \
124 	_CHECK(condition, tag, duration, format)
125 #define CHECK_ATTR(condition, tag, format...) \
126 	_CHECK(condition, tag, tattr.duration, format)
127 
128 #define MAGIC_VAL 0x1234
129 #define NUM_ITER 100000
130 #define VIP_NUM 5
131 
132 static inline __u64 ptr_to_u64(const void *ptr)
133 {
134 	return (__u64) (unsigned long) ptr;
135 }
136 
137 int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);
138 int compare_map_keys(int map1_fd, int map2_fd);
139 int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
140 int extract_build_id(char *build_id, size_t size);
141 void *spin_lock_thread(void *arg);
142 
143 #ifdef __x86_64__
144 #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
145 #elif defined(__s390x__)
146 #define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep"
147 #else
148 #define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
149 #endif
150