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