blob: e25dbf6038cf6d9ea8afb0d2db2fd7acd1b46d0f [file] [log] [blame]
Alexei Starovoitov249b8122014-12-01 15:06:37 -08001#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 */
11static void *(*bpf_map_lookup_elem)(void *map, void *key) =
12 (void *) BPF_FUNC_map_lookup_elem;
13static int (*bpf_map_update_elem)(void *map, void *key, void *value,
14 unsigned long long flags) =
15 (void *) BPF_FUNC_map_update_elem;
16static int (*bpf_map_delete_elem)(void *map, void *key) =
17 (void *) BPF_FUNC_map_delete_elem;
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070018static int (*bpf_probe_read)(void *dst, int size, void *unsafe_ptr) =
19 (void *) BPF_FUNC_probe_read;
20static unsigned long long (*bpf_ktime_get_ns)(void) =
21 (void *) BPF_FUNC_ktime_get_ns;
22static int (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) =
23 (void *) BPF_FUNC_trace_printk;
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070024static void (*bpf_tail_call)(void *ctx, void *map, int index) =
25 (void *) BPF_FUNC_tail_call;
Alexei Starovoitov530b2c82015-05-19 16:59:06 -070026static unsigned long long (*bpf_get_smp_processor_id)(void) =
27 (void *) BPF_FUNC_get_smp_processor_id;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -070028static unsigned long long (*bpf_get_current_pid_tgid)(void) =
29 (void *) BPF_FUNC_get_current_pid_tgid;
30static unsigned long long (*bpf_get_current_uid_gid)(void) =
31 (void *) BPF_FUNC_get_current_uid_gid;
32static int (*bpf_get_current_comm)(void *buf, int buf_size) =
33 (void *) BPF_FUNC_get_current_comm;
Teng Qin41e9a802017-06-02 21:03:53 -070034static unsigned long long (*bpf_perf_event_read)(void *map,
35 unsigned long long flags) =
Kaixu Xia47efb302015-08-06 07:02:36 +000036 (void *) BPF_FUNC_perf_event_read;
Alexei Starovoitov27b29f62015-09-15 23:05:43 -070037static int (*bpf_clone_redirect)(void *ctx, int ifindex, int flags) =
38 (void *) BPF_FUNC_clone_redirect;
39static int (*bpf_redirect)(int ifindex, int flags) =
40 (void *) BPF_FUNC_redirect;
John Fastabend9d6e0052017-07-17 09:30:25 -070041static int (*bpf_redirect_map)(void *map, int key, int flags) =
42 (void *) BPF_FUNC_redirect_map;
Adam Barth05b8ad22016-08-10 09:45:39 -070043static int (*bpf_perf_event_output)(void *ctx, void *map,
44 unsigned long long flags, void *data,
45 int size) =
Alexei Starovoitov39111692015-10-20 20:02:35 -070046 (void *) BPF_FUNC_perf_event_output;
Alexei Starovoitova6ffe7b2016-02-17 19:58:59 -080047static int (*bpf_get_stackid)(void *ctx, void *map, int flags) =
48 (void *) BPF_FUNC_get_stackid;
Sargun Dhillon96ae5222016-07-25 05:54:46 -070049static int (*bpf_probe_write_user)(void *dst, void *src, int size) =
50 (void *) BPF_FUNC_probe_write_user;
Sargun Dhillon9e6e60e2016-08-12 08:57:04 -070051static int (*bpf_current_task_under_cgroup)(void *map, int index) =
52 (void *) BPF_FUNC_current_task_under_cgroup;
William Tu6afb1e22016-08-19 11:55:44 -070053static int (*bpf_skb_get_tunnel_key)(void *ctx, void *key, int size, int flags) =
54 (void *) BPF_FUNC_skb_get_tunnel_key;
55static int (*bpf_skb_set_tunnel_key)(void *ctx, void *key, int size, int flags) =
56 (void *) BPF_FUNC_skb_set_tunnel_key;
57static int (*bpf_skb_get_tunnel_opt)(void *ctx, void *md, int size) =
58 (void *) BPF_FUNC_skb_get_tunnel_opt;
59static int (*bpf_skb_set_tunnel_opt)(void *ctx, void *md, int size) =
60 (void *) BPF_FUNC_skb_set_tunnel_opt;
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -070061static unsigned long long (*bpf_get_prandom_u32)(void) =
62 (void *) BPF_FUNC_get_prandom_u32;
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -080063static int (*bpf_xdp_adjust_head)(void *ctx, int offset) =
64 (void *) BPF_FUNC_xdp_adjust_head;
Daniel Borkmann22c88522017-09-25 02:25:53 +020065static int (*bpf_xdp_adjust_meta)(void *ctx, int offset) =
66 (void *) BPF_FUNC_xdp_adjust_meta;
Lawrence Brakmo8c4b4c72017-06-30 20:02:46 -070067static int (*bpf_setsockopt)(void *ctx, int level, int optname, void *optval,
68 int optlen) =
69 (void *) BPF_FUNC_setsockopt;
John Fastabend69e8cc12017-08-15 22:33:32 -070070static int (*bpf_sk_redirect_map)(void *map, int key, int flags) =
71 (void *) BPF_FUNC_sk_redirect_map;
72static int (*bpf_sock_map_update)(void *map, void *key, void *value,
John Fastabend464bc0f2017-08-28 07:10:04 -070073 unsigned long long flags) =
John Fastabend69e8cc12017-08-15 22:33:32 -070074 (void *) BPF_FUNC_sock_map_update;
Yonghong Song020a32d2017-10-05 09:19:21 -070075static int (*bpf_perf_event_read_value)(void *map, unsigned long long flags,
76 void *buf, unsigned int buf_size) =
77 (void *) BPF_FUNC_perf_event_read_value;
Yonghong Song81b9cf82017-10-05 09:19:23 -070078static int (*bpf_perf_prog_read_value)(void *ctx, void *buf,
79 unsigned int buf_size) =
80 (void *) BPF_FUNC_perf_prog_read_value;
John Fastabend69e8cc12017-08-15 22:33:32 -070081
Alexei Starovoitov249b8122014-12-01 15:06:37 -080082
83/* llvm builtin functions that eBPF C program may use to
84 * emit BPF_LD_ABS and BPF_LD_IND instructions
85 */
86struct sk_buff;
87unsigned long long load_byte(void *skb,
88 unsigned long long off) asm("llvm.bpf.load.byte");
89unsigned long long load_half(void *skb,
90 unsigned long long off) asm("llvm.bpf.load.half");
91unsigned long long load_word(void *skb,
92 unsigned long long off) asm("llvm.bpf.load.word");
93
94/* a helper structure used by eBPF C program
95 * to describe map attributes to elf_bpf loader
96 */
97struct bpf_map_def {
98 unsigned int type;
99 unsigned int key_size;
100 unsigned int value_size;
101 unsigned int max_entries;
Alexei Starovoitov89b97602016-03-07 21:57:20 -0800102 unsigned int map_flags;
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -0700103 unsigned int inner_map_idx;
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700104 unsigned int numa_node;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800105};
106
Thomas Graff74599f2016-11-30 17:10:11 +0100107static int (*bpf_skb_load_bytes)(void *ctx, int off, void *to, int len) =
108 (void *) BPF_FUNC_skb_load_bytes;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -0700109static int (*bpf_skb_store_bytes)(void *ctx, int off, void *from, int len, int flags) =
110 (void *) BPF_FUNC_skb_store_bytes;
111static int (*bpf_l3_csum_replace)(void *ctx, int off, int from, int to, int flags) =
112 (void *) BPF_FUNC_l3_csum_replace;
113static int (*bpf_l4_csum_replace)(void *ctx, int off, int from, int to, int flags) =
114 (void *) BPF_FUNC_l4_csum_replace;
Daniel Borkmann747ea552016-08-12 22:17:17 +0200115static int (*bpf_skb_under_cgroup)(void *ctx, void *map, int index) =
116 (void *) BPF_FUNC_skb_under_cgroup;
Thomas Graff74599f2016-11-30 17:10:11 +0100117static int (*bpf_skb_change_head)(void *, int len, int flags) =
118 (void *) BPF_FUNC_skb_change_head;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -0700119
Joel Fernandesb655fc12017-09-20 09:11:58 -0700120/* Scan the ARCH passed in from ARCH env variable (see Makefile) */
121#if defined(__TARGET_ARCH_x86)
122 #define bpf_target_x86
123 #define bpf_target_defined
124#elif defined(__TARGET_ARCH_s930x)
125 #define bpf_target_s930x
126 #define bpf_target_defined
127#elif defined(__TARGET_ARCH_arm64)
128 #define bpf_target_arm64
129 #define bpf_target_defined
130#elif defined(__TARGET_ARCH_mips)
131 #define bpf_target_mips
132 #define bpf_target_defined
133#elif defined(__TARGET_ARCH_powerpc)
134 #define bpf_target_powerpc
135 #define bpf_target_defined
136#elif defined(__TARGET_ARCH_sparc)
137 #define bpf_target_sparc
138 #define bpf_target_defined
139#else
140 #undef bpf_target_defined
141#endif
142
143/* Fall back to what the compiler says */
144#ifndef bpf_target_defined
Michael Holzheud9125572015-07-06 16:20:07 +0200145#if defined(__x86_64__)
Joel Fernandesb655fc12017-09-20 09:11:58 -0700146 #define bpf_target_x86
147#elif defined(__s390x__)
148 #define bpf_target_s930x
149#elif defined(__aarch64__)
150 #define bpf_target_arm64
151#elif defined(__mips__)
152 #define bpf_target_mips
153#elif defined(__powerpc__)
154 #define bpf_target_powerpc
155#elif defined(__sparc__)
156 #define bpf_target_sparc
157#endif
158#endif
159
160#if defined(bpf_target_x86)
Michael Holzheud9125572015-07-06 16:20:07 +0200161
162#define PT_REGS_PARM1(x) ((x)->di)
163#define PT_REGS_PARM2(x) ((x)->si)
164#define PT_REGS_PARM3(x) ((x)->dx)
165#define PT_REGS_PARM4(x) ((x)->cx)
166#define PT_REGS_PARM5(x) ((x)->r8)
167#define PT_REGS_RET(x) ((x)->sp)
168#define PT_REGS_FP(x) ((x)->bp)
169#define PT_REGS_RC(x) ((x)->ax)
170#define PT_REGS_SP(x) ((x)->sp)
Naveen N. Rao138d6152016-04-04 22:31:34 +0530171#define PT_REGS_IP(x) ((x)->ip)
Michael Holzheud9125572015-07-06 16:20:07 +0200172
Joel Fernandesb655fc12017-09-20 09:11:58 -0700173#elif defined(bpf_target_s390x)
Michael Holzheud9125572015-07-06 16:20:07 +0200174
175#define PT_REGS_PARM1(x) ((x)->gprs[2])
176#define PT_REGS_PARM2(x) ((x)->gprs[3])
177#define PT_REGS_PARM3(x) ((x)->gprs[4])
178#define PT_REGS_PARM4(x) ((x)->gprs[5])
179#define PT_REGS_PARM5(x) ((x)->gprs[6])
180#define PT_REGS_RET(x) ((x)->gprs[14])
181#define PT_REGS_FP(x) ((x)->gprs[11]) /* Works only with CONFIG_FRAME_POINTER */
182#define PT_REGS_RC(x) ((x)->gprs[2])
183#define PT_REGS_SP(x) ((x)->gprs[15])
Michael Holzheu2dbb4c02016-11-28 13:48:30 +0100184#define PT_REGS_IP(x) ((x)->psw.addr)
Michael Holzheud9125572015-07-06 16:20:07 +0200185
Joel Fernandesb655fc12017-09-20 09:11:58 -0700186#elif defined(bpf_target_arm64)
Yang Shi85ff8a432015-10-26 17:02:19 -0700187
188#define PT_REGS_PARM1(x) ((x)->regs[0])
189#define PT_REGS_PARM2(x) ((x)->regs[1])
190#define PT_REGS_PARM3(x) ((x)->regs[2])
191#define PT_REGS_PARM4(x) ((x)->regs[3])
192#define PT_REGS_PARM5(x) ((x)->regs[4])
193#define PT_REGS_RET(x) ((x)->regs[30])
194#define PT_REGS_FP(x) ((x)->regs[29]) /* Works only with CONFIG_FRAME_POINTER */
195#define PT_REGS_RC(x) ((x)->regs[0])
196#define PT_REGS_SP(x) ((x)->sp)
Naveen N. Rao138d6152016-04-04 22:31:34 +0530197#define PT_REGS_IP(x) ((x)->pc)
198
Joel Fernandesb655fc12017-09-20 09:11:58 -0700199#elif defined(bpf_target_mips)
David Daneyc1932cd2017-06-13 16:49:37 -0700200
201#define PT_REGS_PARM1(x) ((x)->regs[4])
202#define PT_REGS_PARM2(x) ((x)->regs[5])
203#define PT_REGS_PARM3(x) ((x)->regs[6])
204#define PT_REGS_PARM4(x) ((x)->regs[7])
205#define PT_REGS_PARM5(x) ((x)->regs[8])
206#define PT_REGS_RET(x) ((x)->regs[31])
207#define PT_REGS_FP(x) ((x)->regs[30]) /* Works only with CONFIG_FRAME_POINTER */
208#define PT_REGS_RC(x) ((x)->regs[1])
209#define PT_REGS_SP(x) ((x)->regs[29])
210#define PT_REGS_IP(x) ((x)->cp0_epc)
211
Joel Fernandesb655fc12017-09-20 09:11:58 -0700212#elif defined(bpf_target_powerpc)
Naveen N. Rao138d6152016-04-04 22:31:34 +0530213
214#define PT_REGS_PARM1(x) ((x)->gpr[3])
215#define PT_REGS_PARM2(x) ((x)->gpr[4])
216#define PT_REGS_PARM3(x) ((x)->gpr[5])
217#define PT_REGS_PARM4(x) ((x)->gpr[6])
218#define PT_REGS_PARM5(x) ((x)->gpr[7])
219#define PT_REGS_RC(x) ((x)->gpr[3])
220#define PT_REGS_SP(x) ((x)->sp)
221#define PT_REGS_IP(x) ((x)->nip)
Yang Shi85ff8a432015-10-26 17:02:19 -0700222
Joel Fernandesb655fc12017-09-20 09:11:58 -0700223#elif defined(bpf_target_sparc)
David S. Millerb0c47802017-04-22 12:31:05 -0700224
225#define PT_REGS_PARM1(x) ((x)->u_regs[UREG_I0])
226#define PT_REGS_PARM2(x) ((x)->u_regs[UREG_I1])
227#define PT_REGS_PARM3(x) ((x)->u_regs[UREG_I2])
228#define PT_REGS_PARM4(x) ((x)->u_regs[UREG_I3])
229#define PT_REGS_PARM5(x) ((x)->u_regs[UREG_I4])
230#define PT_REGS_RET(x) ((x)->u_regs[UREG_I7])
231#define PT_REGS_RC(x) ((x)->u_regs[UREG_I0])
232#define PT_REGS_SP(x) ((x)->u_regs[UREG_FP])
Joel Fernandesb655fc12017-09-20 09:11:58 -0700233
234/* Should this also be a bpf_target check for the sparc case? */
David S. Millerb0c47802017-04-22 12:31:05 -0700235#if defined(__arch64__)
236#define PT_REGS_IP(x) ((x)->tpc)
237#else
238#define PT_REGS_IP(x) ((x)->pc)
239#endif
240
Michael Holzheud9125572015-07-06 16:20:07 +0200241#endif
Naveen N. Rao138d6152016-04-04 22:31:34 +0530242
Joel Fernandesb655fc12017-09-20 09:11:58 -0700243#ifdef bpf_target_powerpc
Naveen N. Rao138d6152016-04-04 22:31:34 +0530244#define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = (ctx)->link; })
245#define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP
Joel Fernandesb655fc12017-09-20 09:11:58 -0700246#elif bpf_target_sparc
David S. Millerb0c47802017-04-22 12:31:05 -0700247#define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = PT_REGS_RET(ctx); })
248#define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP
Naveen N. Rao138d6152016-04-04 22:31:34 +0530249#else
250#define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ \
251 bpf_probe_read(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); })
252#define BPF_KRETPROBE_READ_RET_IP(ip, ctx) ({ \
253 bpf_probe_read(&(ip), sizeof(ip), \
254 (void *)(PT_REGS_FP(ctx) + sizeof(ip))); })
255#endif
256
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800257#endif