xref: /openbmc/linux/tools/testing/selftests/bpf/test_progs.h (revision d38835b75f67df16cef65c14aa64796a1832e6b4)
13f306588SStanislav Fomichev /* SPDX-License-Identifier: GPL-2.0 */
23f306588SStanislav Fomichev #include <stdio.h>
33f306588SStanislav Fomichev #include <unistd.h>
43f306588SStanislav Fomichev #include <errno.h>
53f306588SStanislav Fomichev #include <string.h>
63f306588SStanislav Fomichev #include <assert.h>
73f306588SStanislav Fomichev #include <stdlib.h>
83f306588SStanislav Fomichev #include <stdarg.h>
93f306588SStanislav Fomichev #include <time.h>
103f306588SStanislav Fomichev #include <signal.h>
113f306588SStanislav Fomichev 
123f306588SStanislav Fomichev #include <linux/types.h>
133f306588SStanislav Fomichev typedef __u16 __sum16;
143f306588SStanislav Fomichev #include <arpa/inet.h>
153f306588SStanislav Fomichev #include <linux/if_ether.h>
163f306588SStanislav Fomichev #include <linux/if_packet.h>
173f306588SStanislav Fomichev #include <linux/ip.h>
183f306588SStanislav Fomichev #include <linux/ipv6.h>
193f306588SStanislav Fomichev #include <linux/tcp.h>
203f306588SStanislav Fomichev #include <linux/filter.h>
213f306588SStanislav Fomichev #include <linux/perf_event.h>
223f306588SStanislav Fomichev #include <linux/unistd.h>
233f306588SStanislav Fomichev 
243f306588SStanislav Fomichev #include <sys/ioctl.h>
253f306588SStanislav Fomichev #include <sys/wait.h>
263f306588SStanislav Fomichev #include <sys/types.h>
273f306588SStanislav Fomichev #include <sys/time.h>
283f306588SStanislav Fomichev #include <fcntl.h>
293f306588SStanislav Fomichev #include <pthread.h>
303f306588SStanislav Fomichev #include <linux/bpf.h>
313f306588SStanislav Fomichev #include <linux/err.h>
323f306588SStanislav Fomichev #include <bpf/bpf.h>
333f306588SStanislav Fomichev #include <bpf/libbpf.h>
343f306588SStanislav Fomichev 
353f306588SStanislav Fomichev #include "test_iptunnel_common.h"
363f306588SStanislav Fomichev #include "bpf_util.h"
373f306588SStanislav Fomichev #include "bpf_endian.h"
383f306588SStanislav Fomichev #include "trace_helpers.h"
393f306588SStanislav Fomichev #include "flow_dissector_load.h"
403f306588SStanislav Fomichev 
413a516a0aSAndrii Nakryiko struct test_selector {
423a516a0aSAndrii Nakryiko 	const char *name;
433a516a0aSAndrii Nakryiko 	bool *num_set;
443a516a0aSAndrii Nakryiko 	int num_set_len;
453a516a0aSAndrii Nakryiko };
463a516a0aSAndrii Nakryiko 
470ff97e56SAndrii Nakryiko struct test_env {
483a516a0aSAndrii Nakryiko 	struct test_selector test_selector;
493a516a0aSAndrii Nakryiko 	struct test_selector subtest_selector;
500ff97e56SAndrii Nakryiko 	bool verifier_stats;
510ff97e56SAndrii Nakryiko 	bool verbose;
520ff97e56SAndrii Nakryiko 	bool very_verbose;
530ff97e56SAndrii Nakryiko 
540ff97e56SAndrii Nakryiko 	bool jit_enabled;
550ff97e56SAndrii Nakryiko 
560ff97e56SAndrii Nakryiko 	struct prog_test_def *test;
57946152b3SStanislav Fomichev 	FILE *stdout;
58946152b3SStanislav Fomichev 	FILE *stderr;
590ff97e56SAndrii Nakryiko 	char *log_buf;
600ff97e56SAndrii Nakryiko 	size_t log_cnt;
610ff97e56SAndrii Nakryiko 
623a516a0aSAndrii Nakryiko 	int succ_cnt; /* successful tests */
633a516a0aSAndrii Nakryiko 	int sub_succ_cnt; /* successful sub-tests */
643a516a0aSAndrii Nakryiko 	int fail_cnt; /* total failed tests + sub-tests */
65cd9c21d7SStanislav Fomichev 	int skip_cnt; /* skipped tests */
660ff97e56SAndrii Nakryiko };
670ff97e56SAndrii Nakryiko 
680ff97e56SAndrii Nakryiko extern struct test_env env;
690ff97e56SAndrii Nakryiko 
700ff97e56SAndrii Nakryiko extern void test__force_log();
713a516a0aSAndrii Nakryiko extern bool test__start_subtest(const char *name);
72cd9c21d7SStanislav Fomichev extern void test__skip(void);
73*d38835b7SStanislav Fomichev extern void test__fail(void);
743f306588SStanislav Fomichev 
753f306588SStanislav Fomichev #define MAGIC_BYTES 123
763f306588SStanislav Fomichev 
773f306588SStanislav Fomichev /* ipv4 test vector */
783f306588SStanislav Fomichev struct ipv4_packet {
793f306588SStanislav Fomichev 	struct ethhdr eth;
803f306588SStanislav Fomichev 	struct iphdr iph;
813f306588SStanislav Fomichev 	struct tcphdr tcp;
823f306588SStanislav Fomichev } __packed;
833f306588SStanislav Fomichev extern struct ipv4_packet pkt_v4;
843f306588SStanislav Fomichev 
853f306588SStanislav Fomichev /* ipv6 test vector */
863f306588SStanislav Fomichev struct ipv6_packet {
873f306588SStanislav Fomichev 	struct ethhdr eth;
883f306588SStanislav Fomichev 	struct ipv6hdr iph;
893f306588SStanislav Fomichev 	struct tcphdr tcp;
903f306588SStanislav Fomichev } __packed;
913f306588SStanislav Fomichev extern struct ipv6_packet pkt_v6;
923f306588SStanislav Fomichev 
933f306588SStanislav Fomichev #define _CHECK(condition, tag, duration, format...) ({			\
943f306588SStanislav Fomichev 	int __ret = !!(condition);					\
953f306588SStanislav Fomichev 	if (__ret) {							\
96*d38835b7SStanislav Fomichev 		test__fail();						\
9766bd2ec1SStanislav Fomichev 		printf("%s:FAIL:%s ", __func__, tag);			\
9866bd2ec1SStanislav Fomichev 		printf(format);						\
993f306588SStanislav Fomichev 	} else {							\
10066bd2ec1SStanislav Fomichev 		printf("%s:PASS:%s %d nsec\n",				\
1010ff97e56SAndrii Nakryiko 		       __func__, tag, duration);			\
1023f306588SStanislav Fomichev 	}								\
1033f306588SStanislav Fomichev 	__ret;								\
1043f306588SStanislav Fomichev })
1053f306588SStanislav Fomichev 
106*d38835b7SStanislav Fomichev #define CHECK_FAIL(condition) ({					\
107*d38835b7SStanislav Fomichev 	int __ret = !!(condition);					\
108*d38835b7SStanislav Fomichev 	if (__ret) {							\
109*d38835b7SStanislav Fomichev 		test__fail();						\
110*d38835b7SStanislav Fomichev 		printf("%s:FAIL:%d ", __func__, __LINE__);		\
111*d38835b7SStanislav Fomichev 	}								\
112*d38835b7SStanislav Fomichev 	__ret;								\
113*d38835b7SStanislav Fomichev })
114*d38835b7SStanislav Fomichev 
1153f306588SStanislav Fomichev #define CHECK(condition, tag, format...) \
1163f306588SStanislav Fomichev 	_CHECK(condition, tag, duration, format)
1173f306588SStanislav Fomichev #define CHECK_ATTR(condition, tag, format...) \
1183f306588SStanislav Fomichev 	_CHECK(condition, tag, tattr.duration, format)
1193f306588SStanislav Fomichev 
120eaf90740SStanislav Fomichev #define MAGIC_VAL 0x1234
121eaf90740SStanislav Fomichev #define NUM_ITER 100000
122eaf90740SStanislav Fomichev #define VIP_NUM 5
123eaf90740SStanislav Fomichev 
124886225bbSStanislav Fomichev static inline __u64 ptr_to_u64(const void *ptr)
125886225bbSStanislav Fomichev {
126886225bbSStanislav Fomichev 	return (__u64) (unsigned long) ptr;
127886225bbSStanislav Fomichev }
128886225bbSStanislav Fomichev 
1293f306588SStanislav Fomichev int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);
130615741d8SStanislav Fomichev int compare_map_keys(int map1_fd, int map2_fd);
131615741d8SStanislav Fomichev int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
132615741d8SStanislav Fomichev int extract_build_id(char *build_id, size_t size);
133271a6337SStanislav Fomichev void *spin_lock_thread(void *arg);
1341cb59a60SIlya Leoshkevich 
1351cb59a60SIlya Leoshkevich #ifdef __x86_64__
1361cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
1371cb59a60SIlya Leoshkevich #elif defined(__s390x__)
1381cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep"
1391cb59a60SIlya Leoshkevich #else
1401cb59a60SIlya Leoshkevich #define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
1411cb59a60SIlya Leoshkevich #endif
142