xref: /linux/tools/testing/selftests/bpf/test_progs.h (revision 1375dc4a4579d5e767dd8c2d2abcd929ff59d0a7)
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 struct prog_test_def;
42 
43 struct test_selector {
44 	const char *name;
45 	bool *num_set;
46 	int num_set_len;
47 };
48 
49 struct test_env {
50 	struct test_selector test_selector;
51 	struct test_selector subtest_selector;
52 	bool verifier_stats;
53 	bool verbose;
54 	bool very_verbose;
55 
56 	bool jit_enabled;
57 
58 	struct prog_test_def *test;
59 	char *log_buf;
60 	size_t log_cnt;
61 	size_t log_cap;
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 };
67 
68 extern int error_cnt;
69 extern int pass_cnt;
70 extern struct test_env env;
71 
72 extern void test__printf(const char *fmt, ...);
73 extern void test__vprintf(const char *fmt, va_list args);
74 extern void test__force_log();
75 extern bool test__start_subtest(const char *name);
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 		error_cnt++;						\
99 		test__printf("%s:FAIL:%s ", __func__, tag);		\
100 		test__printf(format);					\
101 	} else {							\
102 		pass_cnt++;						\
103 		test__printf("%s:PASS:%s %d nsec\n",			\
104 			      __func__, tag, duration);			\
105 	}								\
106 	__ret;								\
107 })
108 
109 #define CHECK(condition, tag, format...) \
110 	_CHECK(condition, tag, duration, format)
111 #define CHECK_ATTR(condition, tag, format...) \
112 	_CHECK(condition, tag, tattr.duration, format)
113 
114 #define MAGIC_VAL 0x1234
115 #define NUM_ITER 100000
116 #define VIP_NUM 5
117 
118 static inline __u64 ptr_to_u64(const void *ptr)
119 {
120 	return (__u64) (unsigned long) ptr;
121 }
122 
123 int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);
124 int compare_map_keys(int map1_fd, int map2_fd);
125 int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
126 int extract_build_id(char *build_id, size_t size);
127 void *spin_lock_thread(void *arg);
128 
129 #ifdef __x86_64__
130 #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
131 #elif defined(__s390x__)
132 #define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep"
133 #else
134 #define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
135 #endif
136