blob: 265f8e0e8ada8aa44587903b88cab2119bed4375 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Alexei Starovoitov249b8122014-12-01 15:06:37 -08002#ifndef __BPF_HELPERS_H
3#define __BPF_HELPERS_H
4
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))
10
11/* helper functions called from eBPF programs written in C */
12static void *(*bpf_map_lookup_elem)(void *map, void *key) =
13 (void *) BPF_FUNC_map_lookup_elem;
14static int (*bpf_map_update_elem)(void *map, void *key, void *value,
15 unsigned long long flags) =
16 (void *) BPF_FUNC_map_update_elem;
17static int (*bpf_map_delete_elem)(void *map, void *key) =
18 (void *) BPF_FUNC_map_delete_elem;
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070019static int (*bpf_probe_read)(void *dst, int size, void *unsafe_ptr) =
20 (void *) BPF_FUNC_probe_read;
21static unsigned long long (*bpf_ktime_get_ns)(void) =
22 (void *) BPF_FUNC_ktime_get_ns;
23static int (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) =
24 (void *) BPF_FUNC_trace_printk;
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070025static void (*bpf_tail_call)(void *ctx, void *map, int index) =
26 (void *) BPF_FUNC_tail_call;
Alexei Starovoitov530b2c82015-05-19 16:59:06 -070027static unsigned long long (*bpf_get_smp_processor_id)(void) =
28 (void *) BPF_FUNC_get_smp_processor_id;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -070029static unsigned long long (*bpf_get_current_pid_tgid)(void) =
30 (void *) BPF_FUNC_get_current_pid_tgid;
31static unsigned long long (*bpf_get_current_uid_gid)(void) =
32 (void *) BPF_FUNC_get_current_uid_gid;
33static int (*bpf_get_current_comm)(void *buf, int buf_size) =
34 (void *) BPF_FUNC_get_current_comm;
Teng Qin41e9a802017-06-02 21:03:53 -070035static unsigned long long (*bpf_perf_event_read)(void *map,
36 unsigned long long flags) =
Kaixu Xia47efb302015-08-06 07:02:36 +000037 (void *) BPF_FUNC_perf_event_read;
Alexei Starovoitov27b29f62015-09-15 23:05:43 -070038static int (*bpf_clone_redirect)(void *ctx, int ifindex, int flags) =
39 (void *) BPF_FUNC_clone_redirect;
40static int (*bpf_redirect)(int ifindex, int flags) =
41 (void *) BPF_FUNC_redirect;
John Fastabend9d6e0052017-07-17 09:30:25 -070042static int (*bpf_redirect_map)(void *map, int key, int flags) =
43 (void *) BPF_FUNC_redirect_map;
Adam Barth05b8ad22016-08-10 09:45:39 -070044static int (*bpf_perf_event_output)(void *ctx, void *map,
45 unsigned long long flags, void *data,
46 int size) =
Alexei Starovoitov39111692015-10-20 20:02:35 -070047 (void *) BPF_FUNC_perf_event_output;
Alexei Starovoitova6ffe7b2016-02-17 19:58:59 -080048static int (*bpf_get_stackid)(void *ctx, void *map, int flags) =
49 (void *) BPF_FUNC_get_stackid;
Sargun Dhillon96ae5222016-07-25 05:54:46 -070050static int (*bpf_probe_write_user)(void *dst, void *src, int size) =
51 (void *) BPF_FUNC_probe_write_user;
Sargun Dhillon9e6e60e2016-08-12 08:57:04 -070052static int (*bpf_current_task_under_cgroup)(void *map, int index) =
53 (void *) BPF_FUNC_current_task_under_cgroup;
William Tu6afb1e22016-08-19 11:55:44 -070054static int (*bpf_skb_get_tunnel_key)(void *ctx, void *key, int size, int flags) =
55 (void *) BPF_FUNC_skb_get_tunnel_key;
56static int (*bpf_skb_set_tunnel_key)(void *ctx, void *key, int size, int flags) =
57 (void *) BPF_FUNC_skb_set_tunnel_key;
58static int (*bpf_skb_get_tunnel_opt)(void *ctx, void *md, int size) =
59 (void *) BPF_FUNC_skb_get_tunnel_opt;
60static int (*bpf_skb_set_tunnel_opt)(void *ctx, void *md, int size) =
61 (void *) BPF_FUNC_skb_set_tunnel_opt;
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -070062static unsigned long long (*bpf_get_prandom_u32)(void) =
63 (void *) BPF_FUNC_get_prandom_u32;
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -080064static int (*bpf_xdp_adjust_head)(void *ctx, int offset) =
65 (void *) BPF_FUNC_xdp_adjust_head;
Daniel Borkmann22c88522017-09-25 02:25:53 +020066static int (*bpf_xdp_adjust_meta)(void *ctx, int offset) =
67 (void *) BPF_FUNC_xdp_adjust_meta;
Lawrence Brakmo8c4b4c72017-06-30 20:02:46 -070068static int (*bpf_setsockopt)(void *ctx, int level, int optname, void *optval,
69 int optlen) =
70 (void *) BPF_FUNC_setsockopt;
Lawrence Brakmocd86d1f2017-10-20 11:05:40 -070071static int (*bpf_getsockopt)(void *ctx, int level, int optname, void *optval,
72 int optlen) =
73 (void *) BPF_FUNC_getsockopt;
Lawrence Brakmod6d4f602018-01-25 16:14:16 -080074static int (*bpf_sock_ops_cb_flags_set)(void *ctx, int flags) =
75 (void *) BPF_FUNC_sock_ops_cb_flags_set;
John Fastabend34f795022017-10-18 07:10:36 -070076static int (*bpf_sk_redirect_map)(void *ctx, void *map, int key, int flags) =
John Fastabend69e8cc12017-08-15 22:33:32 -070077 (void *) BPF_FUNC_sk_redirect_map;
78static int (*bpf_sock_map_update)(void *map, void *key, void *value,
John Fastabend464bc0f2017-08-28 07:10:04 -070079 unsigned long long flags) =
John Fastabend69e8cc12017-08-15 22:33:32 -070080 (void *) BPF_FUNC_sock_map_update;
Yonghong Song020a32d2017-10-05 09:19:21 -070081static 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;
Yonghong Song81b9cf82017-10-05 09:19:23 -070084static int (*bpf_perf_prog_read_value)(void *ctx, void *buf,
85 unsigned int buf_size) =
86 (void *) BPF_FUNC_perf_prog_read_value;
Josef Bacik965de872017-12-11 11:36:49 -050087static int (*bpf_override_return)(void *ctx, unsigned long rc) =
88 (void *) BPF_FUNC_override_return;
John Fastabend4c4c3c22018-03-18 12:57:41 -070089static int (*bpf_msg_redirect_map)(void *ctx, void *map, int key, int flags) =
90 (void *) BPF_FUNC_msg_redirect_map;
John Fastabend1c16c312018-03-18 12:57:56 -070091static int (*bpf_msg_apply_bytes)(void *ctx, int len) =
92 (void *) BPF_FUNC_msg_apply_bytes;
John Fastabend468b3fd2018-03-18 12:58:02 -070093static int (*bpf_msg_cork_bytes)(void *ctx, int len) =
94 (void *) BPF_FUNC_msg_cork_bytes;
John Fastabend0dcbbf62018-03-18 12:58:12 -070095static int (*bpf_msg_pull_data)(void *ctx, int start, int end, int flags) =
96 (void *) BPF_FUNC_msg_pull_data;
Andrey Ignatov622adaf2018-03-30 15:08:06 -070097static int (*bpf_bind)(void *ctx, void *addr, int addr_len) =
98 (void *) BPF_FUNC_bind;
Nikita V. Shirokov0367d0a2018-04-17 21:42:22 -070099static int (*bpf_xdp_adjust_tail)(void *ctx, int offset) =
100 (void *) BPF_FUNC_xdp_adjust_tail;
Eyal Birger29a36f92018-04-24 17:50:30 +0300101static int (*bpf_skb_get_xfrm_state)(void *ctx, int index, void *state,
102 int size, int flags) =
103 (void *) BPF_FUNC_skb_get_xfrm_state;
Yonghong Songde2ff052018-04-28 22:28:12 -0700104static int (*bpf_get_stack)(void *ctx, void *buf, int size, int flags) =
105 (void *) BPF_FUNC_get_stack;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800106
107/* llvm builtin functions that eBPF C program may use to
108 * emit BPF_LD_ABS and BPF_LD_IND instructions
109 */
110struct sk_buff;
111unsigned long long load_byte(void *skb,
112 unsigned long long off) asm("llvm.bpf.load.byte");
113unsigned long long load_half(void *skb,
114 unsigned long long off) asm("llvm.bpf.load.half");
115unsigned long long load_word(void *skb,
116 unsigned long long off) asm("llvm.bpf.load.word");
117
118/* a helper structure used by eBPF C program
119 * to describe map attributes to elf_bpf loader
120 */
121struct bpf_map_def {
122 unsigned int type;
123 unsigned int key_size;
124 unsigned int value_size;
125 unsigned int max_entries;
Alexei Starovoitov89b97602016-03-07 21:57:20 -0800126 unsigned int map_flags;
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -0700127 unsigned int inner_map_idx;
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700128 unsigned int numa_node;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800129};
130
Thomas Graff74599f2016-11-30 17:10:11 +0100131static int (*bpf_skb_load_bytes)(void *ctx, int off, void *to, int len) =
132 (void *) BPF_FUNC_skb_load_bytes;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -0700133static int (*bpf_skb_store_bytes)(void *ctx, int off, void *from, int len, int flags) =
134 (void *) BPF_FUNC_skb_store_bytes;
135static int (*bpf_l3_csum_replace)(void *ctx, int off, int from, int to, int flags) =
136 (void *) BPF_FUNC_l3_csum_replace;
137static int (*bpf_l4_csum_replace)(void *ctx, int off, int from, int to, int flags) =
138 (void *) BPF_FUNC_l4_csum_replace;
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -0700139static int (*bpf_csum_diff)(void *from, int from_size, void *to, int to_size, int seed) =
140 (void *) BPF_FUNC_csum_diff;
Daniel Borkmann747ea552016-08-12 22:17:17 +0200141static int (*bpf_skb_under_cgroup)(void *ctx, void *map, int index) =
142 (void *) BPF_FUNC_skb_under_cgroup;
Thomas Graff74599f2016-11-30 17:10:11 +0100143static int (*bpf_skb_change_head)(void *, int len, int flags) =
144 (void *) BPF_FUNC_skb_change_head;
John Fastabend82a86162018-03-18 12:57:31 -0700145static int (*bpf_skb_pull_data)(void *, int len) =
146 (void *) BPF_FUNC_skb_pull_data;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -0700147
Joel Fernandesb655fc12017-09-20 09:11:58 -0700148/* Scan the ARCH passed in from ARCH env variable (see Makefile) */
149#if defined(__TARGET_ARCH_x86)
150 #define bpf_target_x86
151 #define bpf_target_defined
152#elif defined(__TARGET_ARCH_s930x)
153 #define bpf_target_s930x
154 #define bpf_target_defined
155#elif defined(__TARGET_ARCH_arm64)
156 #define bpf_target_arm64
157 #define bpf_target_defined
158#elif defined(__TARGET_ARCH_mips)
159 #define bpf_target_mips
160 #define bpf_target_defined
161#elif defined(__TARGET_ARCH_powerpc)
162 #define bpf_target_powerpc
163 #define bpf_target_defined
164#elif defined(__TARGET_ARCH_sparc)
165 #define bpf_target_sparc
166 #define bpf_target_defined
167#else
168 #undef bpf_target_defined
169#endif
170
171/* Fall back to what the compiler says */
172#ifndef bpf_target_defined
Michael Holzheud9125572015-07-06 16:20:07 +0200173#if defined(__x86_64__)
Joel Fernandesb655fc12017-09-20 09:11:58 -0700174 #define bpf_target_x86
175#elif defined(__s390x__)
176 #define bpf_target_s930x
177#elif defined(__aarch64__)
178 #define bpf_target_arm64
179#elif defined(__mips__)
180 #define bpf_target_mips
181#elif defined(__powerpc__)
182 #define bpf_target_powerpc
183#elif defined(__sparc__)
184 #define bpf_target_sparc
185#endif
186#endif
187
188#if defined(bpf_target_x86)
Michael Holzheud9125572015-07-06 16:20:07 +0200189
190#define PT_REGS_PARM1(x) ((x)->di)
191#define PT_REGS_PARM2(x) ((x)->si)
192#define PT_REGS_PARM3(x) ((x)->dx)
193#define PT_REGS_PARM4(x) ((x)->cx)
194#define PT_REGS_PARM5(x) ((x)->r8)
195#define PT_REGS_RET(x) ((x)->sp)
196#define PT_REGS_FP(x) ((x)->bp)
197#define PT_REGS_RC(x) ((x)->ax)
198#define PT_REGS_SP(x) ((x)->sp)
Naveen N. Rao138d6152016-04-04 22:31:34 +0530199#define PT_REGS_IP(x) ((x)->ip)
Michael Holzheud9125572015-07-06 16:20:07 +0200200
Joel Fernandesb655fc12017-09-20 09:11:58 -0700201#elif defined(bpf_target_s390x)
Michael Holzheud9125572015-07-06 16:20:07 +0200202
203#define PT_REGS_PARM1(x) ((x)->gprs[2])
204#define PT_REGS_PARM2(x) ((x)->gprs[3])
205#define PT_REGS_PARM3(x) ((x)->gprs[4])
206#define PT_REGS_PARM4(x) ((x)->gprs[5])
207#define PT_REGS_PARM5(x) ((x)->gprs[6])
208#define PT_REGS_RET(x) ((x)->gprs[14])
209#define PT_REGS_FP(x) ((x)->gprs[11]) /* Works only with CONFIG_FRAME_POINTER */
210#define PT_REGS_RC(x) ((x)->gprs[2])
211#define PT_REGS_SP(x) ((x)->gprs[15])
Michael Holzheu2dbb4c02016-11-28 13:48:30 +0100212#define PT_REGS_IP(x) ((x)->psw.addr)
Michael Holzheud9125572015-07-06 16:20:07 +0200213
Joel Fernandesb655fc12017-09-20 09:11:58 -0700214#elif defined(bpf_target_arm64)
Yang Shi85ff8a432015-10-26 17:02:19 -0700215
216#define PT_REGS_PARM1(x) ((x)->regs[0])
217#define PT_REGS_PARM2(x) ((x)->regs[1])
218#define PT_REGS_PARM3(x) ((x)->regs[2])
219#define PT_REGS_PARM4(x) ((x)->regs[3])
220#define PT_REGS_PARM5(x) ((x)->regs[4])
221#define PT_REGS_RET(x) ((x)->regs[30])
222#define PT_REGS_FP(x) ((x)->regs[29]) /* Works only with CONFIG_FRAME_POINTER */
223#define PT_REGS_RC(x) ((x)->regs[0])
224#define PT_REGS_SP(x) ((x)->sp)
Naveen N. Rao138d6152016-04-04 22:31:34 +0530225#define PT_REGS_IP(x) ((x)->pc)
226
Joel Fernandesb655fc12017-09-20 09:11:58 -0700227#elif defined(bpf_target_mips)
David Daneyc1932cd2017-06-13 16:49:37 -0700228
229#define PT_REGS_PARM1(x) ((x)->regs[4])
230#define PT_REGS_PARM2(x) ((x)->regs[5])
231#define PT_REGS_PARM3(x) ((x)->regs[6])
232#define PT_REGS_PARM4(x) ((x)->regs[7])
233#define PT_REGS_PARM5(x) ((x)->regs[8])
234#define PT_REGS_RET(x) ((x)->regs[31])
235#define PT_REGS_FP(x) ((x)->regs[30]) /* Works only with CONFIG_FRAME_POINTER */
236#define PT_REGS_RC(x) ((x)->regs[1])
237#define PT_REGS_SP(x) ((x)->regs[29])
238#define PT_REGS_IP(x) ((x)->cp0_epc)
239
Joel Fernandesb655fc12017-09-20 09:11:58 -0700240#elif defined(bpf_target_powerpc)
Naveen N. Rao138d6152016-04-04 22:31:34 +0530241
242#define PT_REGS_PARM1(x) ((x)->gpr[3])
243#define PT_REGS_PARM2(x) ((x)->gpr[4])
244#define PT_REGS_PARM3(x) ((x)->gpr[5])
245#define PT_REGS_PARM4(x) ((x)->gpr[6])
246#define PT_REGS_PARM5(x) ((x)->gpr[7])
247#define PT_REGS_RC(x) ((x)->gpr[3])
248#define PT_REGS_SP(x) ((x)->sp)
249#define PT_REGS_IP(x) ((x)->nip)
Yang Shi85ff8a432015-10-26 17:02:19 -0700250
Joel Fernandesb655fc12017-09-20 09:11:58 -0700251#elif defined(bpf_target_sparc)
David S. Millerb0c47802017-04-22 12:31:05 -0700252
253#define PT_REGS_PARM1(x) ((x)->u_regs[UREG_I0])
254#define PT_REGS_PARM2(x) ((x)->u_regs[UREG_I1])
255#define PT_REGS_PARM3(x) ((x)->u_regs[UREG_I2])
256#define PT_REGS_PARM4(x) ((x)->u_regs[UREG_I3])
257#define PT_REGS_PARM5(x) ((x)->u_regs[UREG_I4])
258#define PT_REGS_RET(x) ((x)->u_regs[UREG_I7])
259#define PT_REGS_RC(x) ((x)->u_regs[UREG_I0])
260#define PT_REGS_SP(x) ((x)->u_regs[UREG_FP])
Joel Fernandesb655fc12017-09-20 09:11:58 -0700261
262/* Should this also be a bpf_target check for the sparc case? */
David S. Millerb0c47802017-04-22 12:31:05 -0700263#if defined(__arch64__)
264#define PT_REGS_IP(x) ((x)->tpc)
265#else
266#define PT_REGS_IP(x) ((x)->pc)
267#endif
268
Michael Holzheud9125572015-07-06 16:20:07 +0200269#endif
Naveen N. Rao138d6152016-04-04 22:31:34 +0530270
Joel Fernandesb655fc12017-09-20 09:11:58 -0700271#ifdef bpf_target_powerpc
Naveen N. Rao138d6152016-04-04 22:31:34 +0530272#define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = (ctx)->link; })
273#define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP
Joel Fernandesb655fc12017-09-20 09:11:58 -0700274#elif bpf_target_sparc
David S. Millerb0c47802017-04-22 12:31:05 -0700275#define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = PT_REGS_RET(ctx); })
276#define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP
Naveen N. Rao138d6152016-04-04 22:31:34 +0530277#else
278#define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ \
279 bpf_probe_read(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); })
280#define BPF_KRETPROBE_READ_RET_IP(ip, ctx) ({ \
281 bpf_probe_read(&(ip), sizeof(ip), \
282 (void *)(PT_REGS_FP(ctx) + sizeof(ip))); })
283#endif
284
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800285#endif