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