Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / tools / testing / selftests / bpf / bpf_helpers.h
blobdde2c11d7771078071ccca4c1e9f2a85014e0443
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __BPF_HELPERS_H
3 #define __BPF_HELPERS_H
5 /* helper macro to place programs, maps, license in
6 * different sections in elf_bpf file. Section names
7 * are interpreted by elf_bpf loader
8 */
9 #define SEC(NAME) __attribute__((section(NAME), used))
11 /* helper functions called from eBPF programs written in C */
12 static void *(*bpf_map_lookup_elem)(void *map, void *key) =
13 (void *) BPF_FUNC_map_lookup_elem;
14 static int (*bpf_map_update_elem)(void *map, void *key, void *value,
15 unsigned long long flags) =
16 (void *) BPF_FUNC_map_update_elem;
17 static int (*bpf_map_delete_elem)(void *map, void *key) =
18 (void *) BPF_FUNC_map_delete_elem;
19 static int (*bpf_probe_read)(void *dst, int size, void *unsafe_ptr) =
20 (void *) BPF_FUNC_probe_read;
21 static unsigned long long (*bpf_ktime_get_ns)(void) =
22 (void *) BPF_FUNC_ktime_get_ns;
23 static int (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) =
24 (void *) BPF_FUNC_trace_printk;
25 static void (*bpf_tail_call)(void *ctx, void *map, int index) =
26 (void *) BPF_FUNC_tail_call;
27 static unsigned long long (*bpf_get_smp_processor_id)(void) =
28 (void *) BPF_FUNC_get_smp_processor_id;
29 static unsigned long long (*bpf_get_current_pid_tgid)(void) =
30 (void *) BPF_FUNC_get_current_pid_tgid;
31 static unsigned long long (*bpf_get_current_uid_gid)(void) =
32 (void *) BPF_FUNC_get_current_uid_gid;
33 static int (*bpf_get_current_comm)(void *buf, int buf_size) =
34 (void *) BPF_FUNC_get_current_comm;
35 static unsigned long long (*bpf_perf_event_read)(void *map,
36 unsigned long long flags) =
37 (void *) BPF_FUNC_perf_event_read;
38 static int (*bpf_clone_redirect)(void *ctx, int ifindex, int flags) =
39 (void *) BPF_FUNC_clone_redirect;
40 static int (*bpf_redirect)(int ifindex, int flags) =
41 (void *) BPF_FUNC_redirect;
42 static int (*bpf_redirect_map)(void *map, int key, int flags) =
43 (void *) BPF_FUNC_redirect_map;
44 static int (*bpf_perf_event_output)(void *ctx, void *map,
45 unsigned long long flags, void *data,
46 int size) =
47 (void *) BPF_FUNC_perf_event_output;
48 static int (*bpf_get_stackid)(void *ctx, void *map, int flags) =
49 (void *) BPF_FUNC_get_stackid;
50 static int (*bpf_probe_write_user)(void *dst, void *src, int size) =
51 (void *) BPF_FUNC_probe_write_user;
52 static int (*bpf_current_task_under_cgroup)(void *map, int index) =
53 (void *) BPF_FUNC_current_task_under_cgroup;
54 static int (*bpf_skb_get_tunnel_key)(void *ctx, void *key, int size, int flags) =
55 (void *) BPF_FUNC_skb_get_tunnel_key;
56 static int (*bpf_skb_set_tunnel_key)(void *ctx, void *key, int size, int flags) =
57 (void *) BPF_FUNC_skb_set_tunnel_key;
58 static int (*bpf_skb_get_tunnel_opt)(void *ctx, void *md, int size) =
59 (void *) BPF_FUNC_skb_get_tunnel_opt;
60 static int (*bpf_skb_set_tunnel_opt)(void *ctx, void *md, int size) =
61 (void *) BPF_FUNC_skb_set_tunnel_opt;
62 static unsigned long long (*bpf_get_prandom_u32)(void) =
63 (void *) BPF_FUNC_get_prandom_u32;
64 static int (*bpf_xdp_adjust_head)(void *ctx, int offset) =
65 (void *) BPF_FUNC_xdp_adjust_head;
66 static int (*bpf_xdp_adjust_meta)(void *ctx, int offset) =
67 (void *) BPF_FUNC_xdp_adjust_meta;
68 static int (*bpf_setsockopt)(void *ctx, int level, int optname, void *optval,
69 int optlen) =
70 (void *) BPF_FUNC_setsockopt;
71 static int (*bpf_getsockopt)(void *ctx, int level, int optname, void *optval,
72 int optlen) =
73 (void *) BPF_FUNC_getsockopt;
74 static int (*bpf_sock_ops_cb_flags_set)(void *ctx, int flags) =
75 (void *) BPF_FUNC_sock_ops_cb_flags_set;
76 static int (*bpf_sk_redirect_map)(void *ctx, void *map, int key, int flags) =
77 (void *) BPF_FUNC_sk_redirect_map;
78 static int (*bpf_sock_map_update)(void *map, void *key, void *value,
79 unsigned long long flags) =
80 (void *) BPF_FUNC_sock_map_update;
81 static int (*bpf_perf_event_read_value)(void *map, unsigned long long flags,
82 void *buf, unsigned int buf_size) =
83 (void *) BPF_FUNC_perf_event_read_value;
84 static int (*bpf_perf_prog_read_value)(void *ctx, void *buf,
85 unsigned int buf_size) =
86 (void *) BPF_FUNC_perf_prog_read_value;
87 static int (*bpf_override_return)(void *ctx, unsigned long rc) =
88 (void *) BPF_FUNC_override_return;
90 /* llvm builtin functions that eBPF C program may use to
91 * emit BPF_LD_ABS and BPF_LD_IND instructions
93 struct sk_buff;
94 unsigned long long load_byte(void *skb,
95 unsigned long long off) asm("llvm.bpf.load.byte");
96 unsigned long long load_half(void *skb,
97 unsigned long long off) asm("llvm.bpf.load.half");
98 unsigned long long load_word(void *skb,
99 unsigned long long off) asm("llvm.bpf.load.word");
101 /* a helper structure used by eBPF C program
102 * to describe map attributes to elf_bpf loader
104 struct bpf_map_def {
105 unsigned int type;
106 unsigned int key_size;
107 unsigned int value_size;
108 unsigned int max_entries;
109 unsigned int map_flags;
110 unsigned int inner_map_idx;
111 unsigned int numa_node;
114 static int (*bpf_skb_load_bytes)(void *ctx, int off, void *to, int len) =
115 (void *) BPF_FUNC_skb_load_bytes;
116 static int (*bpf_skb_store_bytes)(void *ctx, int off, void *from, int len, int flags) =
117 (void *) BPF_FUNC_skb_store_bytes;
118 static int (*bpf_l3_csum_replace)(void *ctx, int off, int from, int to, int flags) =
119 (void *) BPF_FUNC_l3_csum_replace;
120 static int (*bpf_l4_csum_replace)(void *ctx, int off, int from, int to, int flags) =
121 (void *) BPF_FUNC_l4_csum_replace;
122 static int (*bpf_skb_under_cgroup)(void *ctx, void *map, int index) =
123 (void *) BPF_FUNC_skb_under_cgroup;
124 static int (*bpf_skb_change_head)(void *, int len, int flags) =
125 (void *) BPF_FUNC_skb_change_head;
127 /* Scan the ARCH passed in from ARCH env variable (see Makefile) */
128 #if defined(__TARGET_ARCH_x86)
129 #define bpf_target_x86
130 #define bpf_target_defined
131 #elif defined(__TARGET_ARCH_s930x)
132 #define bpf_target_s930x
133 #define bpf_target_defined
134 #elif defined(__TARGET_ARCH_arm64)
135 #define bpf_target_arm64
136 #define bpf_target_defined
137 #elif defined(__TARGET_ARCH_mips)
138 #define bpf_target_mips
139 #define bpf_target_defined
140 #elif defined(__TARGET_ARCH_powerpc)
141 #define bpf_target_powerpc
142 #define bpf_target_defined
143 #elif defined(__TARGET_ARCH_sparc)
144 #define bpf_target_sparc
145 #define bpf_target_defined
146 #else
147 #undef bpf_target_defined
148 #endif
150 /* Fall back to what the compiler says */
151 #ifndef bpf_target_defined
152 #if defined(__x86_64__)
153 #define bpf_target_x86
154 #elif defined(__s390x__)
155 #define bpf_target_s930x
156 #elif defined(__aarch64__)
157 #define bpf_target_arm64
158 #elif defined(__mips__)
159 #define bpf_target_mips
160 #elif defined(__powerpc__)
161 #define bpf_target_powerpc
162 #elif defined(__sparc__)
163 #define bpf_target_sparc
164 #endif
165 #endif
167 #if defined(bpf_target_x86)
169 #define PT_REGS_PARM1(x) ((x)->di)
170 #define PT_REGS_PARM2(x) ((x)->si)
171 #define PT_REGS_PARM3(x) ((x)->dx)
172 #define PT_REGS_PARM4(x) ((x)->cx)
173 #define PT_REGS_PARM5(x) ((x)->r8)
174 #define PT_REGS_RET(x) ((x)->sp)
175 #define PT_REGS_FP(x) ((x)->bp)
176 #define PT_REGS_RC(x) ((x)->ax)
177 #define PT_REGS_SP(x) ((x)->sp)
178 #define PT_REGS_IP(x) ((x)->ip)
180 #elif defined(bpf_target_s390x)
182 #define PT_REGS_PARM1(x) ((x)->gprs[2])
183 #define PT_REGS_PARM2(x) ((x)->gprs[3])
184 #define PT_REGS_PARM3(x) ((x)->gprs[4])
185 #define PT_REGS_PARM4(x) ((x)->gprs[5])
186 #define PT_REGS_PARM5(x) ((x)->gprs[6])
187 #define PT_REGS_RET(x) ((x)->gprs[14])
188 #define PT_REGS_FP(x) ((x)->gprs[11]) /* Works only with CONFIG_FRAME_POINTER */
189 #define PT_REGS_RC(x) ((x)->gprs[2])
190 #define PT_REGS_SP(x) ((x)->gprs[15])
191 #define PT_REGS_IP(x) ((x)->psw.addr)
193 #elif defined(bpf_target_arm64)
195 #define PT_REGS_PARM1(x) ((x)->regs[0])
196 #define PT_REGS_PARM2(x) ((x)->regs[1])
197 #define PT_REGS_PARM3(x) ((x)->regs[2])
198 #define PT_REGS_PARM4(x) ((x)->regs[3])
199 #define PT_REGS_PARM5(x) ((x)->regs[4])
200 #define PT_REGS_RET(x) ((x)->regs[30])
201 #define PT_REGS_FP(x) ((x)->regs[29]) /* Works only with CONFIG_FRAME_POINTER */
202 #define PT_REGS_RC(x) ((x)->regs[0])
203 #define PT_REGS_SP(x) ((x)->sp)
204 #define PT_REGS_IP(x) ((x)->pc)
206 #elif defined(bpf_target_mips)
208 #define PT_REGS_PARM1(x) ((x)->regs[4])
209 #define PT_REGS_PARM2(x) ((x)->regs[5])
210 #define PT_REGS_PARM3(x) ((x)->regs[6])
211 #define PT_REGS_PARM4(x) ((x)->regs[7])
212 #define PT_REGS_PARM5(x) ((x)->regs[8])
213 #define PT_REGS_RET(x) ((x)->regs[31])
214 #define PT_REGS_FP(x) ((x)->regs[30]) /* Works only with CONFIG_FRAME_POINTER */
215 #define PT_REGS_RC(x) ((x)->regs[1])
216 #define PT_REGS_SP(x) ((x)->regs[29])
217 #define PT_REGS_IP(x) ((x)->cp0_epc)
219 #elif defined(bpf_target_powerpc)
221 #define PT_REGS_PARM1(x) ((x)->gpr[3])
222 #define PT_REGS_PARM2(x) ((x)->gpr[4])
223 #define PT_REGS_PARM3(x) ((x)->gpr[5])
224 #define PT_REGS_PARM4(x) ((x)->gpr[6])
225 #define PT_REGS_PARM5(x) ((x)->gpr[7])
226 #define PT_REGS_RC(x) ((x)->gpr[3])
227 #define PT_REGS_SP(x) ((x)->sp)
228 #define PT_REGS_IP(x) ((x)->nip)
230 #elif defined(bpf_target_sparc)
232 #define PT_REGS_PARM1(x) ((x)->u_regs[UREG_I0])
233 #define PT_REGS_PARM2(x) ((x)->u_regs[UREG_I1])
234 #define PT_REGS_PARM3(x) ((x)->u_regs[UREG_I2])
235 #define PT_REGS_PARM4(x) ((x)->u_regs[UREG_I3])
236 #define PT_REGS_PARM5(x) ((x)->u_regs[UREG_I4])
237 #define PT_REGS_RET(x) ((x)->u_regs[UREG_I7])
238 #define PT_REGS_RC(x) ((x)->u_regs[UREG_I0])
239 #define PT_REGS_SP(x) ((x)->u_regs[UREG_FP])
241 /* Should this also be a bpf_target check for the sparc case? */
242 #if defined(__arch64__)
243 #define PT_REGS_IP(x) ((x)->tpc)
244 #else
245 #define PT_REGS_IP(x) ((x)->pc)
246 #endif
248 #endif
250 #ifdef bpf_target_powerpc
251 #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = (ctx)->link; })
252 #define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP
253 #elif bpf_target_sparc
254 #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = PT_REGS_RET(ctx); })
255 #define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP
256 #else
257 #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ \
258 bpf_probe_read(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); })
259 #define BPF_KRETPROBE_READ_RET_IP(ip, ctx) ({ \
260 bpf_probe_read(&(ip), sizeof(ip), \
261 (void *)(PT_REGS_FP(ctx) + sizeof(ip))); })
262 #endif
264 #endif