xref: /linux/tools/testing/selftests/bpf/test_progs.h (revision 9406b485dea5e25bed7c81cd822747d494cc8bde)
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/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 str_set {
50 	const char **strs;
51 	int cnt;
52 };
53 
54 struct test_selector {
55 	struct str_set whitelist;
56 	struct str_set blacklist;
57 	bool *num_set;
58 	int num_set_len;
59 };
60 
61 struct test_env {
62 	struct test_selector test_selector;
63 	struct test_selector subtest_selector;
64 	bool verifier_stats;
65 	enum verbosity verbosity;
66 
67 	bool jit_enabled;
68 
69 	struct prog_test_def *test;
70 	FILE *stdout;
71 	FILE *stderr;
72 	char *log_buf;
73 	size_t log_cnt;
74 	int nr_cpus;
75 
76 	int succ_cnt; /* successful tests */
77 	int sub_succ_cnt; /* successful sub-tests */
78 	int fail_cnt; /* total failed tests + sub-tests */
79 	int skip_cnt; /* skipped tests */
80 };
81 
82 extern struct test_env env;
83 
84 extern void test__force_log();
85 extern bool test__start_subtest(const char *name);
86 extern void test__skip(void);
87 extern void test__fail(void);
88 extern int test__join_cgroup(const char *path);
89 
90 #define PRINT_FAIL(format...)                                                  \
91 	({                                                                     \
92 		test__fail();                                                  \
93 		fprintf(stdout, "%s:FAIL:%d ", __func__, __LINE__);            \
94 		fprintf(stdout, ##format);                                     \
95 	})
96 
97 #define _CHECK(condition, tag, duration, format...) ({			\
98 	int __ret = !!(condition);					\
99 	int __save_errno = errno;					\
100 	if (__ret) {							\
101 		test__fail();						\
102 		fprintf(stdout, "%s:FAIL:%s ", __func__, tag);		\
103 		fprintf(stdout, ##format);				\
104 	} else {							\
105 		fprintf(stdout, "%s:PASS:%s %d nsec\n",			\
106 		       __func__, tag, duration);			\
107 	}								\
108 	errno = __save_errno;						\
109 	__ret;								\
110 })
111 
112 #define CHECK_FAIL(condition) ({					\
113 	int __ret = !!(condition);					\
114 	int __save_errno = errno;					\
115 	if (__ret) {							\
116 		test__fail();						\
117 		fprintf(stdout, "%s:FAIL:%d\n", __func__, __LINE__);	\
118 	}								\
119 	errno = __save_errno;						\
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 static inline __u64 ptr_to_u64(const void *ptr)
129 {
130 	return (__u64) (unsigned long) ptr;
131 }
132 
133 int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);
134 int compare_map_keys(int map1_fd, int map2_fd);
135 int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
136 int extract_build_id(char *build_id, size_t size);
137 
138 #ifdef __x86_64__
139 #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
140 #elif defined(__s390x__)
141 #define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep"
142 #else
143 #define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
144 #endif
145