bpf_helpers.h 12.7 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 */
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#ifndef __BPF_HELPERS_H
#define __BPF_HELPERS_H

/* helper macro to place programs, maps, license in
 * different sections in elf_bpf file. Section names
 * are interpreted by elf_bpf loader
 */
#define SEC(NAME) __attribute__((section(NAME), used))

/* helper functions called from eBPF programs written in C */
static void *(*bpf_map_lookup_elem)(void *map, void *key) =
	(void *) BPF_FUNC_map_lookup_elem;
static int (*bpf_map_update_elem)(void *map, void *key, void *value,
				  unsigned long long flags) =
	(void *) BPF_FUNC_map_update_elem;
static int (*bpf_map_delete_elem)(void *map, void *key) =
	(void *) BPF_FUNC_map_delete_elem;
19 20 21 22 23 24
static int (*bpf_probe_read)(void *dst, int size, void *unsafe_ptr) =
	(void *) BPF_FUNC_probe_read;
static unsigned long long (*bpf_ktime_get_ns)(void) =
	(void *) BPF_FUNC_ktime_get_ns;
static int (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) =
	(void *) BPF_FUNC_trace_printk;
25 26
static void (*bpf_tail_call)(void *ctx, void *map, int index) =
	(void *) BPF_FUNC_tail_call;
27 28
static unsigned long long (*bpf_get_smp_processor_id)(void) =
	(void *) BPF_FUNC_get_smp_processor_id;
29 30 31 32 33 34
static unsigned long long (*bpf_get_current_pid_tgid)(void) =
	(void *) BPF_FUNC_get_current_pid_tgid;
static unsigned long long (*bpf_get_current_uid_gid)(void) =
	(void *) BPF_FUNC_get_current_uid_gid;
static int (*bpf_get_current_comm)(void *buf, int buf_size) =
	(void *) BPF_FUNC_get_current_comm;
35 36
static unsigned long long (*bpf_perf_event_read)(void *map,
						 unsigned long long flags) =
37
	(void *) BPF_FUNC_perf_event_read;
38 39 40 41
static int (*bpf_clone_redirect)(void *ctx, int ifindex, int flags) =
	(void *) BPF_FUNC_clone_redirect;
static int (*bpf_redirect)(int ifindex, int flags) =
	(void *) BPF_FUNC_redirect;
42 43
static int (*bpf_redirect_map)(void *map, int key, int flags) =
	(void *) BPF_FUNC_redirect_map;
44 45 46
static int (*bpf_perf_event_output)(void *ctx, void *map,
				    unsigned long long flags, void *data,
				    int size) =
47
	(void *) BPF_FUNC_perf_event_output;
48 49
static int (*bpf_get_stackid)(void *ctx, void *map, int flags) =
	(void *) BPF_FUNC_get_stackid;
50 51
static int (*bpf_probe_write_user)(void *dst, void *src, int size) =
	(void *) BPF_FUNC_probe_write_user;
52 53
static int (*bpf_current_task_under_cgroup)(void *map, int index) =
	(void *) BPF_FUNC_current_task_under_cgroup;
54 55 56 57 58 59 60 61
static int (*bpf_skb_get_tunnel_key)(void *ctx, void *key, int size, int flags) =
	(void *) BPF_FUNC_skb_get_tunnel_key;
static int (*bpf_skb_set_tunnel_key)(void *ctx, void *key, int size, int flags) =
	(void *) BPF_FUNC_skb_set_tunnel_key;
static int (*bpf_skb_get_tunnel_opt)(void *ctx, void *md, int size) =
	(void *) BPF_FUNC_skb_get_tunnel_opt;
static int (*bpf_skb_set_tunnel_opt)(void *ctx, void *md, int size) =
	(void *) BPF_FUNC_skb_set_tunnel_opt;
62 63
static unsigned long long (*bpf_get_prandom_u32)(void) =
	(void *) BPF_FUNC_get_prandom_u32;
64 65
static int (*bpf_xdp_adjust_head)(void *ctx, int offset) =
	(void *) BPF_FUNC_xdp_adjust_head;
66 67
static int (*bpf_xdp_adjust_meta)(void *ctx, int offset) =
	(void *) BPF_FUNC_xdp_adjust_meta;
68 69
static int (*bpf_get_socket_cookie)(void *ctx) =
	(void *) BPF_FUNC_get_socket_cookie;
70 71 72
static int (*bpf_setsockopt)(void *ctx, int level, int optname, void *optval,
			     int optlen) =
	(void *) BPF_FUNC_setsockopt;
73 74 75
static int (*bpf_getsockopt)(void *ctx, int level, int optname, void *optval,
			     int optlen) =
	(void *) BPF_FUNC_getsockopt;
L
Lawrence Brakmo 已提交
76 77
static int (*bpf_sock_ops_cb_flags_set)(void *ctx, int flags) =
	(void *) BPF_FUNC_sock_ops_cb_flags_set;
78
static int (*bpf_sk_redirect_map)(void *ctx, void *map, int key, int flags) =
J
John Fastabend 已提交
79
	(void *) BPF_FUNC_sk_redirect_map;
80 81
static int (*bpf_sk_redirect_hash)(void *ctx, void *map, void *key, int flags) =
	(void *) BPF_FUNC_sk_redirect_hash;
J
John Fastabend 已提交
82
static int (*bpf_sock_map_update)(void *map, void *key, void *value,
83
				  unsigned long long flags) =
J
John Fastabend 已提交
84
	(void *) BPF_FUNC_sock_map_update;
85 86 87
static int (*bpf_sock_hash_update)(void *map, void *key, void *value,
				   unsigned long long flags) =
	(void *) BPF_FUNC_sock_hash_update;
88 89 90
static int (*bpf_perf_event_read_value)(void *map, unsigned long long flags,
					void *buf, unsigned int buf_size) =
	(void *) BPF_FUNC_perf_event_read_value;
91 92 93
static int (*bpf_perf_prog_read_value)(void *ctx, void *buf,
				       unsigned int buf_size) =
	(void *) BPF_FUNC_perf_prog_read_value;
94 95
static int (*bpf_override_return)(void *ctx, unsigned long rc) =
	(void *) BPF_FUNC_override_return;
96 97
static int (*bpf_msg_redirect_map)(void *ctx, void *map, int key, int flags) =
	(void *) BPF_FUNC_msg_redirect_map;
98 99 100
static int (*bpf_msg_redirect_hash)(void *ctx,
				    void *map, void *key, int flags) =
	(void *) BPF_FUNC_msg_redirect_hash;
101 102
static int (*bpf_msg_apply_bytes)(void *ctx, int len) =
	(void *) BPF_FUNC_msg_apply_bytes;
103 104
static int (*bpf_msg_cork_bytes)(void *ctx, int len) =
	(void *) BPF_FUNC_msg_cork_bytes;
105 106
static int (*bpf_msg_pull_data)(void *ctx, int start, int end, int flags) =
	(void *) BPF_FUNC_msg_pull_data;
107 108
static int (*bpf_bind)(void *ctx, void *addr, int addr_len) =
	(void *) BPF_FUNC_bind;
109 110
static int (*bpf_xdp_adjust_tail)(void *ctx, int offset) =
	(void *) BPF_FUNC_xdp_adjust_tail;
111 112 113
static int (*bpf_skb_get_xfrm_state)(void *ctx, int index, void *state,
				     int size, int flags) =
	(void *) BPF_FUNC_skb_get_xfrm_state;
114 115
static int (*bpf_sk_select_reuseport)(void *ctx, void *map, void *key, __u32 flags) =
	(void *) BPF_FUNC_sk_select_reuseport;
116 117
static int (*bpf_get_stack)(void *ctx, void *buf, int size, int flags) =
	(void *) BPF_FUNC_get_stack;
118 119 120
static int (*bpf_fib_lookup)(void *ctx, struct bpf_fib_lookup *params,
			     int plen, __u32 flags) =
	(void *) BPF_FUNC_fib_lookup;
121 122 123 124 125 126 127 128 129 130 131 132
static int (*bpf_lwt_push_encap)(void *ctx, unsigned int type, void *hdr,
				 unsigned int len) =
	(void *) BPF_FUNC_lwt_push_encap;
static int (*bpf_lwt_seg6_store_bytes)(void *ctx, unsigned int offset,
				       void *from, unsigned int len) =
	(void *) BPF_FUNC_lwt_seg6_store_bytes;
static int (*bpf_lwt_seg6_action)(void *ctx, unsigned int action, void *param,
				  unsigned int param_len) =
	(void *) BPF_FUNC_lwt_seg6_action;
static int (*bpf_lwt_seg6_adjust_srh)(void *ctx, unsigned int offset,
				      unsigned int len) =
	(void *) BPF_FUNC_lwt_seg6_adjust_srh;
133 134 135 136 137
static int (*bpf_rc_repeat)(void *ctx) =
	(void *) BPF_FUNC_rc_repeat;
static int (*bpf_rc_keydown)(void *ctx, unsigned int protocol,
			     unsigned long long scancode, unsigned int toggle) =
	(void *) BPF_FUNC_rc_keydown;
138 139
static unsigned long long (*bpf_get_current_cgroup_id)(void) =
	(void *) BPF_FUNC_get_current_cgroup_id;
140 141
static void *(*bpf_get_local_storage)(void *map, unsigned long long flags) =
	(void *) BPF_FUNC_get_local_storage;
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

/* llvm builtin functions that eBPF C program may use to
 * emit BPF_LD_ABS and BPF_LD_IND instructions
 */
struct sk_buff;
unsigned long long load_byte(void *skb,
			     unsigned long long off) asm("llvm.bpf.load.byte");
unsigned long long load_half(void *skb,
			     unsigned long long off) asm("llvm.bpf.load.half");
unsigned long long load_word(void *skb,
			     unsigned long long off) asm("llvm.bpf.load.word");

/* a helper structure used by eBPF C program
 * to describe map attributes to elf_bpf loader
 */
struct bpf_map_def {
	unsigned int type;
	unsigned int key_size;
	unsigned int value_size;
	unsigned int max_entries;
162
	unsigned int map_flags;
M
Martin KaFai Lau 已提交
163
	unsigned int inner_map_idx;
164
	unsigned int numa_node;
165 166
};

167 168 169 170 171 172 173 174 175
#define BPF_ANNOTATE_KV_PAIR(name, type_key, type_val)		\
	struct ____btf_map_##name {				\
		type_key key;					\
		type_val value;					\
	};							\
	struct ____btf_map_##name				\
	__attribute__ ((section(".maps." #name), used))		\
		____btf_map_##name = { }

176 177
static int (*bpf_skb_load_bytes)(void *ctx, int off, void *to, int len) =
	(void *) BPF_FUNC_skb_load_bytes;
178 179
static int (*bpf_skb_load_bytes_relative)(void *ctx, int off, void *to, int len, __u32 start_header) =
	(void *) BPF_FUNC_skb_load_bytes_relative;
180 181 182 183 184 185
static int (*bpf_skb_store_bytes)(void *ctx, int off, void *from, int len, int flags) =
	(void *) BPF_FUNC_skb_store_bytes;
static int (*bpf_l3_csum_replace)(void *ctx, int off, int from, int to, int flags) =
	(void *) BPF_FUNC_l3_csum_replace;
static int (*bpf_l4_csum_replace)(void *ctx, int off, int from, int to, int flags) =
	(void *) BPF_FUNC_l4_csum_replace;
186 187
static int (*bpf_csum_diff)(void *from, int from_size, void *to, int to_size, int seed) =
	(void *) BPF_FUNC_csum_diff;
188 189
static int (*bpf_skb_under_cgroup)(void *ctx, void *map, int index) =
	(void *) BPF_FUNC_skb_under_cgroup;
190 191
static int (*bpf_skb_change_head)(void *, int len, int flags) =
	(void *) BPF_FUNC_skb_change_head;
192 193
static int (*bpf_skb_pull_data)(void *, int len) =
	(void *) BPF_FUNC_skb_pull_data;
194

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
/* Scan the ARCH passed in from ARCH env variable (see Makefile) */
#if defined(__TARGET_ARCH_x86)
	#define bpf_target_x86
	#define bpf_target_defined
#elif defined(__TARGET_ARCH_s930x)
	#define bpf_target_s930x
	#define bpf_target_defined
#elif defined(__TARGET_ARCH_arm64)
	#define bpf_target_arm64
	#define bpf_target_defined
#elif defined(__TARGET_ARCH_mips)
	#define bpf_target_mips
	#define bpf_target_defined
#elif defined(__TARGET_ARCH_powerpc)
	#define bpf_target_powerpc
	#define bpf_target_defined
#elif defined(__TARGET_ARCH_sparc)
	#define bpf_target_sparc
	#define bpf_target_defined
#else
	#undef bpf_target_defined
#endif

/* Fall back to what the compiler says */
#ifndef bpf_target_defined
220
#if defined(__x86_64__)
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
	#define bpf_target_x86
#elif defined(__s390x__)
	#define bpf_target_s930x
#elif defined(__aarch64__)
	#define bpf_target_arm64
#elif defined(__mips__)
	#define bpf_target_mips
#elif defined(__powerpc__)
	#define bpf_target_powerpc
#elif defined(__sparc__)
	#define bpf_target_sparc
#endif
#endif

#if defined(bpf_target_x86)
236 237 238 239 240 241 242 243 244 245

#define PT_REGS_PARM1(x) ((x)->di)
#define PT_REGS_PARM2(x) ((x)->si)
#define PT_REGS_PARM3(x) ((x)->dx)
#define PT_REGS_PARM4(x) ((x)->cx)
#define PT_REGS_PARM5(x) ((x)->r8)
#define PT_REGS_RET(x) ((x)->sp)
#define PT_REGS_FP(x) ((x)->bp)
#define PT_REGS_RC(x) ((x)->ax)
#define PT_REGS_SP(x) ((x)->sp)
246
#define PT_REGS_IP(x) ((x)->ip)
247

248
#elif defined(bpf_target_s390x)
249 250 251 252 253 254 255 256 257 258

#define PT_REGS_PARM1(x) ((x)->gprs[2])
#define PT_REGS_PARM2(x) ((x)->gprs[3])
#define PT_REGS_PARM3(x) ((x)->gprs[4])
#define PT_REGS_PARM4(x) ((x)->gprs[5])
#define PT_REGS_PARM5(x) ((x)->gprs[6])
#define PT_REGS_RET(x) ((x)->gprs[14])
#define PT_REGS_FP(x) ((x)->gprs[11]) /* Works only with CONFIG_FRAME_POINTER */
#define PT_REGS_RC(x) ((x)->gprs[2])
#define PT_REGS_SP(x) ((x)->gprs[15])
259
#define PT_REGS_IP(x) ((x)->psw.addr)
260

261
#elif defined(bpf_target_arm64)
262 263 264 265 266 267 268 269 270 271

#define PT_REGS_PARM1(x) ((x)->regs[0])
#define PT_REGS_PARM2(x) ((x)->regs[1])
#define PT_REGS_PARM3(x) ((x)->regs[2])
#define PT_REGS_PARM4(x) ((x)->regs[3])
#define PT_REGS_PARM5(x) ((x)->regs[4])
#define PT_REGS_RET(x) ((x)->regs[30])
#define PT_REGS_FP(x) ((x)->regs[29]) /* Works only with CONFIG_FRAME_POINTER */
#define PT_REGS_RC(x) ((x)->regs[0])
#define PT_REGS_SP(x) ((x)->sp)
272 273
#define PT_REGS_IP(x) ((x)->pc)

274
#elif defined(bpf_target_mips)
275 276 277 278 279 280 281 282 283 284 285 286

#define PT_REGS_PARM1(x) ((x)->regs[4])
#define PT_REGS_PARM2(x) ((x)->regs[5])
#define PT_REGS_PARM3(x) ((x)->regs[6])
#define PT_REGS_PARM4(x) ((x)->regs[7])
#define PT_REGS_PARM5(x) ((x)->regs[8])
#define PT_REGS_RET(x) ((x)->regs[31])
#define PT_REGS_FP(x) ((x)->regs[30]) /* Works only with CONFIG_FRAME_POINTER */
#define PT_REGS_RC(x) ((x)->regs[1])
#define PT_REGS_SP(x) ((x)->regs[29])
#define PT_REGS_IP(x) ((x)->cp0_epc)

287
#elif defined(bpf_target_powerpc)
288 289 290 291 292 293 294 295 296

#define PT_REGS_PARM1(x) ((x)->gpr[3])
#define PT_REGS_PARM2(x) ((x)->gpr[4])
#define PT_REGS_PARM3(x) ((x)->gpr[5])
#define PT_REGS_PARM4(x) ((x)->gpr[6])
#define PT_REGS_PARM5(x) ((x)->gpr[7])
#define PT_REGS_RC(x) ((x)->gpr[3])
#define PT_REGS_SP(x) ((x)->sp)
#define PT_REGS_IP(x) ((x)->nip)
297

298
#elif defined(bpf_target_sparc)
299 300 301 302 303 304 305 306 307

#define PT_REGS_PARM1(x) ((x)->u_regs[UREG_I0])
#define PT_REGS_PARM2(x) ((x)->u_regs[UREG_I1])
#define PT_REGS_PARM3(x) ((x)->u_regs[UREG_I2])
#define PT_REGS_PARM4(x) ((x)->u_regs[UREG_I3])
#define PT_REGS_PARM5(x) ((x)->u_regs[UREG_I4])
#define PT_REGS_RET(x) ((x)->u_regs[UREG_I7])
#define PT_REGS_RC(x) ((x)->u_regs[UREG_I0])
#define PT_REGS_SP(x) ((x)->u_regs[UREG_FP])
308 309

/* Should this also be a bpf_target check for the sparc case? */
310 311 312 313 314 315
#if defined(__arch64__)
#define PT_REGS_IP(x) ((x)->tpc)
#else
#define PT_REGS_IP(x) ((x)->pc)
#endif

316
#endif
317

318
#ifdef bpf_target_powerpc
319 320
#define BPF_KPROBE_READ_RET_IP(ip, ctx)		({ (ip) = (ctx)->link; })
#define BPF_KRETPROBE_READ_RET_IP		BPF_KPROBE_READ_RET_IP
321
#elif bpf_target_sparc
322 323
#define BPF_KPROBE_READ_RET_IP(ip, ctx)		({ (ip) = PT_REGS_RET(ctx); })
#define BPF_KRETPROBE_READ_RET_IP		BPF_KPROBE_READ_RET_IP
324 325 326 327 328 329 330 331
#else
#define BPF_KPROBE_READ_RET_IP(ip, ctx)		({				\
		bpf_probe_read(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); })
#define BPF_KRETPROBE_READ_RET_IP(ip, ctx)	({				\
		bpf_probe_read(&(ip), sizeof(ip),				\
				(void *)(PT_REGS_FP(ctx) + sizeof(ip))); })
#endif

332
#endif