diff options
Diffstat (limited to 'tools')
-rw-r--r-- | tools/testing/selftests/bpf/Makefile | 1 | ||||
-rw-r--r-- | tools/testing/selftests/bpf/bpf_endian.h | 14 | ||||
-rw-r--r-- | tools/testing/selftests/bpf/bpf_helpers.h | 198 |
3 files changed, 212 insertions, 1 deletions
diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index 2ca51a8a588c..153c3a181a4c 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile | |||
@@ -37,6 +37,5 @@ CLANG ?= clang | |||
37 | 37 | ||
38 | %.o: %.c | 38 | %.o: %.c |
39 | $(CLANG) -I. -I./include/uapi -I../../../include/uapi \ | 39 | $(CLANG) -I. -I./include/uapi -I../../../include/uapi \ |
40 | -I../../../../samples/bpf/ \ | ||
41 | -Wno-compare-distinct-pointer-types \ | 40 | -Wno-compare-distinct-pointer-types \ |
42 | -O2 -target bpf -c $< -o $@ | 41 | -O2 -target bpf -c $< -o $@ |
diff --git a/tools/testing/selftests/bpf/bpf_endian.h b/tools/testing/selftests/bpf/bpf_endian.h index 487cbfb89beb..74af266aa512 100644 --- a/tools/testing/selftests/bpf/bpf_endian.h +++ b/tools/testing/selftests/bpf/bpf_endian.h | |||
@@ -23,11 +23,19 @@ | |||
23 | # define __bpf_htons(x) __builtin_bswap16(x) | 23 | # define __bpf_htons(x) __builtin_bswap16(x) |
24 | # define __bpf_constant_ntohs(x) ___constant_swab16(x) | 24 | # define __bpf_constant_ntohs(x) ___constant_swab16(x) |
25 | # define __bpf_constant_htons(x) ___constant_swab16(x) | 25 | # define __bpf_constant_htons(x) ___constant_swab16(x) |
26 | # define __bpf_ntohl(x) __builtin_bswap32(x) | ||
27 | # define __bpf_htonl(x) __builtin_bswap32(x) | ||
28 | # define __bpf_constant_ntohl(x) ___constant_swab32(x) | ||
29 | # define __bpf_constant_htonl(x) ___constant_swab32(x) | ||
26 | #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ | 30 | #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
27 | # define __bpf_ntohs(x) (x) | 31 | # define __bpf_ntohs(x) (x) |
28 | # define __bpf_htons(x) (x) | 32 | # define __bpf_htons(x) (x) |
29 | # define __bpf_constant_ntohs(x) (x) | 33 | # define __bpf_constant_ntohs(x) (x) |
30 | # define __bpf_constant_htons(x) (x) | 34 | # define __bpf_constant_htons(x) (x) |
35 | # define __bpf_ntohl(x) (x) | ||
36 | # define __bpf_htonl(x) (x) | ||
37 | # define __bpf_constant_ntohl(x) (x) | ||
38 | # define __bpf_constant_htonl(x) (x) | ||
31 | #else | 39 | #else |
32 | # error "Fix your compiler's __BYTE_ORDER__?!" | 40 | # error "Fix your compiler's __BYTE_ORDER__?!" |
33 | #endif | 41 | #endif |
@@ -38,5 +46,11 @@ | |||
38 | #define bpf_ntohs(x) \ | 46 | #define bpf_ntohs(x) \ |
39 | (__builtin_constant_p(x) ? \ | 47 | (__builtin_constant_p(x) ? \ |
40 | __bpf_constant_ntohs(x) : __bpf_ntohs(x)) | 48 | __bpf_constant_ntohs(x) : __bpf_ntohs(x)) |
49 | #define bpf_htonl(x) \ | ||
50 | (__builtin_constant_p(x) ? \ | ||
51 | __bpf_constant_htonl(x) : __bpf_htonl(x)) | ||
52 | #define bpf_ntohl(x) \ | ||
53 | (__builtin_constant_p(x) ? \ | ||
54 | __bpf_constant_ntohl(x) : __bpf_ntohl(x)) | ||
41 | 55 | ||
42 | #endif /* __BPF_ENDIAN__ */ | 56 | #endif /* __BPF_ENDIAN__ */ |
diff --git a/tools/testing/selftests/bpf/bpf_helpers.h b/tools/testing/selftests/bpf/bpf_helpers.h new file mode 100644 index 000000000000..d50ac342dc92 --- /dev/null +++ b/tools/testing/selftests/bpf/bpf_helpers.h | |||
@@ -0,0 +1,198 @@ | |||
1 | #ifndef __BPF_HELPERS_H | ||
2 | #define __BPF_HELPERS_H | ||
3 | |||
4 | /* helper macro to place programs, maps, license in | ||
5 | * different sections in elf_bpf file. Section names | ||
6 | * are interpreted by elf_bpf loader | ||
7 | */ | ||
8 | #define SEC(NAME) __attribute__((section(NAME), used)) | ||
9 | |||
10 | /* helper functions called from eBPF programs written in C */ | ||
11 | static void *(*bpf_map_lookup_elem)(void *map, void *key) = | ||
12 | (void *) BPF_FUNC_map_lookup_elem; | ||
13 | static int (*bpf_map_update_elem)(void *map, void *key, void *value, | ||
14 | unsigned long long flags) = | ||
15 | (void *) BPF_FUNC_map_update_elem; | ||
16 | static int (*bpf_map_delete_elem)(void *map, void *key) = | ||
17 | (void *) BPF_FUNC_map_delete_elem; | ||
18 | static int (*bpf_probe_read)(void *dst, int size, void *unsafe_ptr) = | ||
19 | (void *) BPF_FUNC_probe_read; | ||
20 | static unsigned long long (*bpf_ktime_get_ns)(void) = | ||
21 | (void *) BPF_FUNC_ktime_get_ns; | ||
22 | static int (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) = | ||
23 | (void *) BPF_FUNC_trace_printk; | ||
24 | static void (*bpf_tail_call)(void *ctx, void *map, int index) = | ||
25 | (void *) BPF_FUNC_tail_call; | ||
26 | static unsigned long long (*bpf_get_smp_processor_id)(void) = | ||
27 | (void *) BPF_FUNC_get_smp_processor_id; | ||
28 | static unsigned long long (*bpf_get_current_pid_tgid)(void) = | ||
29 | (void *) BPF_FUNC_get_current_pid_tgid; | ||
30 | static unsigned long long (*bpf_get_current_uid_gid)(void) = | ||
31 | (void *) BPF_FUNC_get_current_uid_gid; | ||
32 | static int (*bpf_get_current_comm)(void *buf, int buf_size) = | ||
33 | (void *) BPF_FUNC_get_current_comm; | ||
34 | static unsigned long long (*bpf_perf_event_read)(void *map, | ||
35 | unsigned long long flags) = | ||
36 | (void *) BPF_FUNC_perf_event_read; | ||
37 | static int (*bpf_clone_redirect)(void *ctx, int ifindex, int flags) = | ||
38 | (void *) BPF_FUNC_clone_redirect; | ||
39 | static int (*bpf_redirect)(int ifindex, int flags) = | ||
40 | (void *) BPF_FUNC_redirect; | ||
41 | static int (*bpf_perf_event_output)(void *ctx, void *map, | ||
42 | unsigned long long flags, void *data, | ||
43 | int size) = | ||
44 | (void *) BPF_FUNC_perf_event_output; | ||
45 | static int (*bpf_get_stackid)(void *ctx, void *map, int flags) = | ||
46 | (void *) BPF_FUNC_get_stackid; | ||
47 | static int (*bpf_probe_write_user)(void *dst, void *src, int size) = | ||
48 | (void *) BPF_FUNC_probe_write_user; | ||
49 | static int (*bpf_current_task_under_cgroup)(void *map, int index) = | ||
50 | (void *) BPF_FUNC_current_task_under_cgroup; | ||
51 | static int (*bpf_skb_get_tunnel_key)(void *ctx, void *key, int size, int flags) = | ||
52 | (void *) BPF_FUNC_skb_get_tunnel_key; | ||
53 | static int (*bpf_skb_set_tunnel_key)(void *ctx, void *key, int size, int flags) = | ||
54 | (void *) BPF_FUNC_skb_set_tunnel_key; | ||
55 | static int (*bpf_skb_get_tunnel_opt)(void *ctx, void *md, int size) = | ||
56 | (void *) BPF_FUNC_skb_get_tunnel_opt; | ||
57 | static int (*bpf_skb_set_tunnel_opt)(void *ctx, void *md, int size) = | ||
58 | (void *) BPF_FUNC_skb_set_tunnel_opt; | ||
59 | static unsigned long long (*bpf_get_prandom_u32)(void) = | ||
60 | (void *) BPF_FUNC_get_prandom_u32; | ||
61 | static int (*bpf_xdp_adjust_head)(void *ctx, int offset) = | ||
62 | (void *) BPF_FUNC_xdp_adjust_head; | ||
63 | static int (*bpf_setsockopt)(void *ctx, int level, int optname, void *optval, | ||
64 | int optlen) = | ||
65 | (void *) BPF_FUNC_setsockopt; | ||
66 | |||
67 | /* llvm builtin functions that eBPF C program may use to | ||
68 | * emit BPF_LD_ABS and BPF_LD_IND instructions | ||
69 | */ | ||
70 | struct sk_buff; | ||
71 | unsigned long long load_byte(void *skb, | ||
72 | unsigned long long off) asm("llvm.bpf.load.byte"); | ||
73 | unsigned long long load_half(void *skb, | ||
74 | unsigned long long off) asm("llvm.bpf.load.half"); | ||
75 | unsigned long long load_word(void *skb, | ||
76 | unsigned long long off) asm("llvm.bpf.load.word"); | ||
77 | |||
78 | /* a helper structure used by eBPF C program | ||
79 | * to describe map attributes to elf_bpf loader | ||
80 | */ | ||
81 | struct bpf_map_def { | ||
82 | unsigned int type; | ||
83 | unsigned int key_size; | ||
84 | unsigned int value_size; | ||
85 | unsigned int max_entries; | ||
86 | unsigned int map_flags; | ||
87 | unsigned int inner_map_idx; | ||
88 | }; | ||
89 | |||
90 | static int (*bpf_skb_load_bytes)(void *ctx, int off, void *to, int len) = | ||
91 | (void *) BPF_FUNC_skb_load_bytes; | ||
92 | static int (*bpf_skb_store_bytes)(void *ctx, int off, void *from, int len, int flags) = | ||
93 | (void *) BPF_FUNC_skb_store_bytes; | ||
94 | static int (*bpf_l3_csum_replace)(void *ctx, int off, int from, int to, int flags) = | ||
95 | (void *) BPF_FUNC_l3_csum_replace; | ||
96 | static int (*bpf_l4_csum_replace)(void *ctx, int off, int from, int to, int flags) = | ||
97 | (void *) BPF_FUNC_l4_csum_replace; | ||
98 | static int (*bpf_skb_under_cgroup)(void *ctx, void *map, int index) = | ||
99 | (void *) BPF_FUNC_skb_under_cgroup; | ||
100 | static int (*bpf_skb_change_head)(void *, int len, int flags) = | ||
101 | (void *) BPF_FUNC_skb_change_head; | ||
102 | |||
103 | #if defined(__x86_64__) | ||
104 | |||
105 | #define PT_REGS_PARM1(x) ((x)->di) | ||
106 | #define PT_REGS_PARM2(x) ((x)->si) | ||
107 | #define PT_REGS_PARM3(x) ((x)->dx) | ||
108 | #define PT_REGS_PARM4(x) ((x)->cx) | ||
109 | #define PT_REGS_PARM5(x) ((x)->r8) | ||
110 | #define PT_REGS_RET(x) ((x)->sp) | ||
111 | #define PT_REGS_FP(x) ((x)->bp) | ||
112 | #define PT_REGS_RC(x) ((x)->ax) | ||
113 | #define PT_REGS_SP(x) ((x)->sp) | ||
114 | #define PT_REGS_IP(x) ((x)->ip) | ||
115 | |||
116 | #elif defined(__s390x__) | ||
117 | |||
118 | #define PT_REGS_PARM1(x) ((x)->gprs[2]) | ||
119 | #define PT_REGS_PARM2(x) ((x)->gprs[3]) | ||
120 | #define PT_REGS_PARM3(x) ((x)->gprs[4]) | ||
121 | #define PT_REGS_PARM4(x) ((x)->gprs[5]) | ||
122 | #define PT_REGS_PARM5(x) ((x)->gprs[6]) | ||
123 | #define PT_REGS_RET(x) ((x)->gprs[14]) | ||
124 | #define PT_REGS_FP(x) ((x)->gprs[11]) /* Works only with CONFIG_FRAME_POINTER */ | ||
125 | #define PT_REGS_RC(x) ((x)->gprs[2]) | ||
126 | #define PT_REGS_SP(x) ((x)->gprs[15]) | ||
127 | #define PT_REGS_IP(x) ((x)->psw.addr) | ||
128 | |||
129 | #elif defined(__aarch64__) | ||
130 | |||
131 | #define PT_REGS_PARM1(x) ((x)->regs[0]) | ||
132 | #define PT_REGS_PARM2(x) ((x)->regs[1]) | ||
133 | #define PT_REGS_PARM3(x) ((x)->regs[2]) | ||
134 | #define PT_REGS_PARM4(x) ((x)->regs[3]) | ||
135 | #define PT_REGS_PARM5(x) ((x)->regs[4]) | ||
136 | #define PT_REGS_RET(x) ((x)->regs[30]) | ||
137 | #define PT_REGS_FP(x) ((x)->regs[29]) /* Works only with CONFIG_FRAME_POINTER */ | ||
138 | #define PT_REGS_RC(x) ((x)->regs[0]) | ||
139 | #define PT_REGS_SP(x) ((x)->sp) | ||
140 | #define PT_REGS_IP(x) ((x)->pc) | ||
141 | |||
142 | #elif defined(__mips__) | ||
143 | |||
144 | #define PT_REGS_PARM1(x) ((x)->regs[4]) | ||
145 | #define PT_REGS_PARM2(x) ((x)->regs[5]) | ||
146 | #define PT_REGS_PARM3(x) ((x)->regs[6]) | ||
147 | #define PT_REGS_PARM4(x) ((x)->regs[7]) | ||
148 | #define PT_REGS_PARM5(x) ((x)->regs[8]) | ||
149 | #define PT_REGS_RET(x) ((x)->regs[31]) | ||
150 | #define PT_REGS_FP(x) ((x)->regs[30]) /* Works only with CONFIG_FRAME_POINTER */ | ||
151 | #define PT_REGS_RC(x) ((x)->regs[1]) | ||
152 | #define PT_REGS_SP(x) ((x)->regs[29]) | ||
153 | #define PT_REGS_IP(x) ((x)->cp0_epc) | ||
154 | |||
155 | #elif defined(__powerpc__) | ||
156 | |||
157 | #define PT_REGS_PARM1(x) ((x)->gpr[3]) | ||
158 | #define PT_REGS_PARM2(x) ((x)->gpr[4]) | ||
159 | #define PT_REGS_PARM3(x) ((x)->gpr[5]) | ||
160 | #define PT_REGS_PARM4(x) ((x)->gpr[6]) | ||
161 | #define PT_REGS_PARM5(x) ((x)->gpr[7]) | ||
162 | #define PT_REGS_RC(x) ((x)->gpr[3]) | ||
163 | #define PT_REGS_SP(x) ((x)->sp) | ||
164 | #define PT_REGS_IP(x) ((x)->nip) | ||
165 | |||
166 | #elif defined(__sparc__) | ||
167 | |||
168 | #define PT_REGS_PARM1(x) ((x)->u_regs[UREG_I0]) | ||
169 | #define PT_REGS_PARM2(x) ((x)->u_regs[UREG_I1]) | ||
170 | #define PT_REGS_PARM3(x) ((x)->u_regs[UREG_I2]) | ||
171 | #define PT_REGS_PARM4(x) ((x)->u_regs[UREG_I3]) | ||
172 | #define PT_REGS_PARM5(x) ((x)->u_regs[UREG_I4]) | ||
173 | #define PT_REGS_RET(x) ((x)->u_regs[UREG_I7]) | ||
174 | #define PT_REGS_RC(x) ((x)->u_regs[UREG_I0]) | ||
175 | #define PT_REGS_SP(x) ((x)->u_regs[UREG_FP]) | ||
176 | #if defined(__arch64__) | ||
177 | #define PT_REGS_IP(x) ((x)->tpc) | ||
178 | #else | ||
179 | #define PT_REGS_IP(x) ((x)->pc) | ||
180 | #endif | ||
181 | |||
182 | #endif | ||
183 | |||
184 | #ifdef __powerpc__ | ||
185 | #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = (ctx)->link; }) | ||
186 | #define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP | ||
187 | #elif defined(__sparc__) | ||
188 | #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = PT_REGS_RET(ctx); }) | ||
189 | #define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP | ||
190 | #else | ||
191 | #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ \ | ||
192 | bpf_probe_read(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); }) | ||
193 | #define BPF_KRETPROBE_READ_RET_IP(ip, ctx) ({ \ | ||
194 | bpf_probe_read(&(ip), sizeof(ip), \ | ||
195 | (void *)(PT_REGS_FP(ctx) + sizeof(ip))); }) | ||
196 | #endif | ||
197 | |||
198 | #endif | ||