1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2019 Richard Palethorpe <rpalethorpe@suse.com>
4 *
5 * Essential Extended Berkeley Packet Filter (eBPF) headers
6 *
7 * Mostly copied/adapted from linux/bpf.h and libbpf so that we can perform
8 * some eBPF testing without any external dependencies.
9 */
10
11 #ifndef LAPI_BPF_H__
12 #define LAPI_BPF_H__
13
14 #include <stdint.h>
15
16 #include "lapi/syscalls.h"
17
18 /* Start copy from linux/bpf_(common).h */
19 #define BPF_CLASS(code) ((code) & 0x07)
20 #define BPF_LD 0x00
21 #define BPF_LDX 0x01
22 #define BPF_ST 0x02
23 #define BPF_STX 0x03
24 #define BPF_ALU 0x04
25 #define BPF_JMP 0x05
26
27 #define BPF_JNE 0x50 /* jump != */
28
29 #define BPF_SIZE(code) ((code) & 0x18)
30 #define BPF_B 0x10 /* 8-bit */
31 #define BPF_W 0x00 /* 32-bit */
32 #define BPF_DW 0x18 /* double word (64-bit) */
33
34 #define BPF_MODE(code) ((code) & 0xe0)
35 #define BPF_IMM 0x00
36 #define BPF_MEM 0x60
37
38 #define BPF_OP(code) ((code) & 0xf0)
39 #define BPF_ADD 0x00
40 #define BPF_SUB 0x10
41 #define BPF_DIV 0x30
42 #define BPF_LSH 0x60
43 #define BPF_RSH 0x70
44 #define BPF_MOD 0x90
45
46 #define BPF_JEQ 0x10
47
48 #define BPF_SRC(code) ((code) & 0x08)
49 #define BPF_K 0x00
50 #define BPF_X 0x08
51
52 #define BPF_ALU64 0x07 /* alu mode in double word width */
53 #define BPF_MOV 0xb0 /* mov reg to reg */
54 #define BPF_CALL 0x80 /* function call */
55 #define BPF_EXIT 0x90 /* function return */
56
57 /* Register numbers */
58 enum {
59 BPF_REG_0 = 0,
60 BPF_REG_1,
61 BPF_REG_2,
62 BPF_REG_3,
63 BPF_REG_4,
64 BPF_REG_5,
65 BPF_REG_6,
66 BPF_REG_7,
67 BPF_REG_8,
68 BPF_REG_9,
69 BPF_REG_10,
70 MAX_BPF_REG,
71 };
72
73 struct bpf_insn {
74 uint8_t code; /* opcode */
75 uint8_t dst_reg:4; /* dest register */
76 uint8_t src_reg:4; /* source register */
77 int16_t off; /* signed offset */
78 int32_t imm; /* signed immediate constant */
79 };
80
81 enum bpf_cmd {
82 BPF_MAP_CREATE,
83 BPF_MAP_LOOKUP_ELEM,
84 BPF_MAP_UPDATE_ELEM,
85 BPF_MAP_DELETE_ELEM,
86 BPF_MAP_GET_NEXT_KEY,
87 BPF_PROG_LOAD,
88 BPF_OBJ_PIN,
89 BPF_OBJ_GET,
90 BPF_PROG_ATTACH,
91 BPF_PROG_DETACH,
92 BPF_PROG_TEST_RUN,
93 BPF_PROG_GET_NEXT_ID,
94 BPF_MAP_GET_NEXT_ID,
95 BPF_PROG_GET_FD_BY_ID,
96 BPF_MAP_GET_FD_BY_ID,
97 BPF_OBJ_GET_INFO_BY_FD,
98 BPF_PROG_QUERY,
99 BPF_RAW_TRACEPOINT_OPEN,
100 BPF_BTF_LOAD,
101 BPF_BTF_GET_FD_BY_ID,
102 BPF_TASK_FD_QUERY,
103 BPF_MAP_LOOKUP_AND_DELETE_ELEM,
104 BPF_MAP_FREEZE,
105 };
106
107 enum bpf_map_type {
108 BPF_MAP_TYPE_UNSPEC,
109 BPF_MAP_TYPE_HASH,
110 BPF_MAP_TYPE_ARRAY,
111 BPF_MAP_TYPE_PROG_ARRAY,
112 BPF_MAP_TYPE_PERF_EVENT_ARRAY,
113 BPF_MAP_TYPE_PERCPU_HASH,
114 BPF_MAP_TYPE_PERCPU_ARRAY,
115 BPF_MAP_TYPE_STACK_TRACE,
116 BPF_MAP_TYPE_CGROUP_ARRAY,
117 BPF_MAP_TYPE_LRU_HASH,
118 BPF_MAP_TYPE_LRU_PERCPU_HASH,
119 BPF_MAP_TYPE_LPM_TRIE,
120 BPF_MAP_TYPE_ARRAY_OF_MAPS,
121 BPF_MAP_TYPE_HASH_OF_MAPS,
122 BPF_MAP_TYPE_DEVMAP,
123 BPF_MAP_TYPE_SOCKMAP,
124 BPF_MAP_TYPE_CPUMAP,
125 BPF_MAP_TYPE_XSKMAP,
126 BPF_MAP_TYPE_SOCKHASH,
127 BPF_MAP_TYPE_CGROUP_STORAGE,
128 BPF_MAP_TYPE_REUSEPORT_SOCKARRAY,
129 BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE,
130 BPF_MAP_TYPE_QUEUE,
131 BPF_MAP_TYPE_STACK,
132 BPF_MAP_TYPE_SK_STORAGE,
133 };
134
135 enum bpf_prog_type {
136 BPF_PROG_TYPE_UNSPEC,
137 BPF_PROG_TYPE_SOCKET_FILTER,
138 BPF_PROG_TYPE_KPROBE,
139 BPF_PROG_TYPE_SCHED_CLS,
140 BPF_PROG_TYPE_SCHED_ACT,
141 BPF_PROG_TYPE_TRACEPOINT,
142 BPF_PROG_TYPE_XDP,
143 BPF_PROG_TYPE_PERF_EVENT,
144 BPF_PROG_TYPE_CGROUP_SKB,
145 BPF_PROG_TYPE_CGROUP_SOCK,
146 BPF_PROG_TYPE_LWT_IN,
147 BPF_PROG_TYPE_LWT_OUT,
148 BPF_PROG_TYPE_LWT_XMIT,
149 BPF_PROG_TYPE_SOCK_OPS,
150 BPF_PROG_TYPE_SK_SKB,
151 BPF_PROG_TYPE_CGROUP_DEVICE,
152 BPF_PROG_TYPE_SK_MSG,
153 BPF_PROG_TYPE_RAW_TRACEPOINT,
154 BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
155 BPF_PROG_TYPE_LWT_SEG6LOCAL,
156 BPF_PROG_TYPE_LIRC_MODE2,
157 BPF_PROG_TYPE_SK_REUSEPORT,
158 BPF_PROG_TYPE_FLOW_DISSECTOR,
159 BPF_PROG_TYPE_CGROUP_SYSCTL,
160 BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE,
161 BPF_PROG_TYPE_CGROUP_SOCKOPT,
162 };
163
164 #define BPF_PSEUDO_MAP_FD 1
165
166 #define BPF_OBJ_NAME_LEN 16U
167
168 #define BPF_ANY 0 /* create new element or update existing */
169 #define BPF_NOEXIST 1 /* create new element if it didn't exist */
170 #define BPF_EXIST 2 /* update existing element */
171 #define BPF_F_LOCK 4 /* spin_lock-ed map_lookup/map_update */
172
173 #define aligned_uint64_t uint64_t __attribute__((aligned(8)))
174
175 union bpf_attr {
176 struct { /* anonymous struct used by BPF_MAP_CREATE command */
177 uint32_t map_type; /* one of enum bpf_map_type */
178 uint32_t key_size; /* size of key in bytes */
179 uint32_t value_size; /* size of value in bytes */
180 uint32_t max_entries; /* max number of entries in a map */
181 uint32_t map_flags; /* BPF_MAP_CREATE related
182 * flags defined above.
183 */
184 uint32_t inner_map_fd; /* fd pointing to the inner map */
185 uint32_t numa_node; /* numa node (effective only if
186 * BPF_F_NUMA_NODE is set).
187 */
188 char map_name[BPF_OBJ_NAME_LEN];
189 uint32_t map_ifindex; /* ifindex of netdev to create on */
190 uint32_t btf_fd; /* fd pointing to a BTF type data */
191 uint32_t btf_key_type_id; /* BTF type_id of the key */
192 uint32_t btf_value_type_id; /* BTF type_id of the value */
193 };
194
195 struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
196 uint32_t map_fd;
197 aligned_uint64_t key;
198 union {
199 aligned_uint64_t value;
200 aligned_uint64_t next_key;
201 };
202 uint64_t flags;
203 };
204
205 struct { /* anonymous struct used by BPF_PROG_LOAD command */
206 uint32_t prog_type; /* one of enum bpf_prog_type */
207 uint32_t insn_cnt;
208 aligned_uint64_t insns;
209 aligned_uint64_t license;
210 uint32_t log_level; /* verbosity level of verifier */
211 uint32_t log_size; /* size of user buffer */
212 aligned_uint64_t log_buf; /* user supplied buffer */
213 uint32_t kern_version; /* not used */
214 uint32_t prog_flags;
215 char prog_name[BPF_OBJ_NAME_LEN];
216 uint32_t prog_ifindex; /* ifindex of netdev to prep for */
217 /* For some prog types expected attach type must be known at
218 * load time to verify attach type specific parts of prog
219 * (context accesses, allowed helpers, etc).
220 */
221 uint32_t expected_attach_type;
222 uint32_t prog_btf_fd; /* fd pointing to BTF type data */
223 uint32_t func_info_rec_size; /* userspace bpf_func_info size */
224 aligned_uint64_t func_info; /* func info */
225 uint32_t func_info_cnt; /* number of bpf_func_info records */
226 uint32_t line_info_rec_size; /* userspace bpf_line_info size */
227 aligned_uint64_t line_info; /* line info */
228 uint32_t line_info_cnt; /* number of bpf_line_info records */
229 };
230
231 struct { /* anonymous struct used by BPF_OBJ_* commands */
232 aligned_uint64_t pathname;
233 uint32_t bpf_fd;
234 uint32_t file_flags;
235 };
236
237 struct { /* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */
238 uint32_t target_fd; /* container object to attach to */
239 uint32_t attach_bpf_fd; /* eBPF program to attach */
240 uint32_t attach_type;
241 uint32_t attach_flags;
242 };
243
244 struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */
245 uint32_t prog_fd;
246 uint32_t retval;
247 uint32_t data_size_in; /* input: len of data_in */
248 uint32_t data_size_out; /* input/output: len of data_out
249 * returns ENOSPC if data_out
250 * is too small.
251 */
252 aligned_uint64_t data_in;
253 aligned_uint64_t data_out;
254 uint32_t repeat;
255 uint32_t duration;
256 uint32_t ctx_size_in; /* input: len of ctx_in */
257 uint32_t ctx_size_out; /* input/output: len of ctx_out
258 * returns ENOSPC if ctx_out
259 * is too small.
260 */
261 aligned_uint64_t ctx_in;
262 aligned_uint64_t ctx_out;
263 } test;
264
265 struct { /* anonymous struct used by BPF_*_GET_*_ID */
266 union {
267 uint32_t start_id;
268 uint32_t prog_id;
269 uint32_t map_id;
270 uint32_t btf_id;
271 };
272 uint32_t next_id;
273 uint32_t open_flags;
274 };
275
276 struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */
277 uint32_t bpf_fd;
278 uint32_t info_len;
279 aligned_uint64_t info;
280 } info;
281
282 struct { /* anonymous struct used by BPF_PROG_QUERY command */
283 uint32_t target_fd; /* container object to query */
284 uint32_t attach_type;
285 uint32_t query_flags;
286 uint32_t attach_flags;
287 aligned_uint64_t prog_ids;
288 uint32_t prog_cnt;
289 } query;
290
291 struct {
292 uint64_t name;
293 uint32_t prog_fd;
294 } raw_tracepoint;
295
296 struct { /* anonymous struct for BPF_BTF_LOAD */
297 aligned_uint64_t btf;
298 aligned_uint64_t btf_log_buf;
299 uint32_t btf_size;
300 uint32_t btf_log_size;
301 uint32_t btf_log_level;
302 };
303
304 struct {
305 uint32_t pid; /* input: pid */
306 uint32_t fd; /* input: fd */
307 uint32_t flags; /* input: flags */
308 uint32_t buf_len; /* input/output: buf len */
309 aligned_uint64_t buf; /* input/output:
310 * tp_name for tracepoint
311 * symbol for kprobe
312 * filename for uprobe
313 */
314 uint32_t prog_id; /* output: prod_id */
315 uint32_t fd_type; /* output: BPF_FD_TYPE_* */
316 uint64_t probe_offset; /* output: probe_offset */
317 uint64_t probe_addr; /* output: probe_addr */
318 } task_fd_query;
319 } __attribute__((aligned(8)));
320
321 #define __BPF_FUNC_MAPPER(FN) \
322 FN(unspec), \
323 FN(map_lookup_elem), \
324 FN(map_update_elem), \
325 FN(map_delete_elem), \
326 FN(probe_read), \
327 FN(ktime_get_ns), \
328 FN(trace_printk), \
329 FN(get_prandom_u32), \
330 FN(get_smp_processor_id), \
331 FN(skb_store_bytes), \
332 FN(l3_csum_replace), \
333 FN(l4_csum_replace), \
334 FN(tail_call), \
335 FN(clone_redirect), \
336 FN(get_current_pid_tgid), \
337 FN(get_current_uid_gid), \
338 FN(get_current_comm), \
339 FN(get_cgroup_classid), \
340 FN(skb_vlan_push), \
341 FN(skb_vlan_pop), \
342 FN(skb_get_tunnel_key), \
343 FN(skb_set_tunnel_key), \
344 FN(perf_event_read), \
345 FN(redirect), \
346 FN(get_route_realm), \
347 FN(perf_event_output), \
348 FN(skb_load_bytes), \
349 FN(get_stackid), \
350 FN(csum_diff), \
351 FN(skb_get_tunnel_opt), \
352 FN(skb_set_tunnel_opt), \
353 FN(skb_change_proto), \
354 FN(skb_change_type), \
355 FN(skb_under_cgroup), \
356 FN(get_hash_recalc), \
357 FN(get_current_task), \
358 FN(probe_write_user), \
359 FN(current_task_under_cgroup), \
360 FN(skb_change_tail), \
361 FN(skb_pull_data), \
362 FN(csum_update), \
363 FN(set_hash_invalid), \
364 FN(get_numa_node_id), \
365 FN(skb_change_head), \
366 FN(xdp_adjust_head), \
367 FN(probe_read_str), \
368 FN(get_socket_cookie), \
369 FN(get_socket_uid), \
370 FN(set_hash), \
371 FN(setsockopt), \
372 FN(skb_adjust_room), \
373 FN(redirect_map), \
374 FN(sk_redirect_map), \
375 FN(sock_map_update), \
376 FN(xdp_adjust_meta), \
377 FN(perf_event_read_value), \
378 FN(perf_prog_read_value), \
379 FN(getsockopt), \
380 FN(override_return), \
381 FN(sock_ops_cb_flags_set), \
382 FN(msg_redirect_map), \
383 FN(msg_apply_bytes), \
384 FN(msg_cork_bytes), \
385 FN(msg_pull_data), \
386 FN(bind), \
387 FN(xdp_adjust_tail), \
388 FN(skb_get_xfrm_state), \
389 FN(get_stack), \
390 FN(skb_load_bytes_relative), \
391 FN(fib_lookup), \
392 FN(sock_hash_update), \
393 FN(msg_redirect_hash), \
394 FN(sk_redirect_hash), \
395 FN(lwt_push_encap), \
396 FN(lwt_seg6_store_bytes), \
397 FN(lwt_seg6_adjust_srh), \
398 FN(lwt_seg6_action), \
399 FN(rc_repeat), \
400 FN(rc_keydown), \
401 FN(skb_cgroup_id), \
402 FN(get_current_cgroup_id), \
403 FN(get_local_storage), \
404 FN(sk_select_reuseport), \
405 FN(skb_ancestor_cgroup_id), \
406 FN(sk_lookup_tcp), \
407 FN(sk_lookup_udp), \
408 FN(sk_release), \
409 FN(map_push_elem), \
410 FN(map_pop_elem), \
411 FN(map_peek_elem), \
412 FN(msg_push_data), \
413 FN(msg_pop_data), \
414 FN(rc_pointer_rel), \
415 FN(spin_lock), \
416 FN(spin_unlock), \
417 FN(sk_fullsock), \
418 FN(tcp_sock), \
419 FN(skb_ecn_set_ce), \
420 FN(get_listener_sock), \
421 FN(skc_lookup_tcp), \
422 FN(tcp_check_syncookie), \
423 FN(sysctl_get_name), \
424 FN(sysctl_get_current_value), \
425 FN(sysctl_get_new_value), \
426 FN(sysctl_set_new_value), \
427 FN(strtol), \
428 FN(strtoul), \
429 FN(sk_storage_get), \
430 FN(sk_storage_delete), \
431 FN(send_signal),
432
433 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
434 * function eBPF program intends to call
435 */
436 #define __BPF_ENUM_FN(x) BPF_FUNC_ ## x
437 enum bpf_func_id {
438 __BPF_FUNC_MAPPER(__BPF_ENUM_FN)
439 __BPF_FUNC_MAX_ID,
440 };
441 #undef __BPF_ENUM_FN
442
443 /* End copy from linux/bpf.h */
444
445 /* Start copy from tools/include/filter.h */
446
447 #define BPF_ALU64_REG(OP, DST, SRC) \
448 ((struct bpf_insn) { \
449 .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \
450 .dst_reg = DST, \
451 .src_reg = SRC, \
452 .off = 0, \
453 .imm = 0 })
454
455 #define BPF_ALU32_REG(OP, DST, SRC) \
456 ((struct bpf_insn) { \
457 .code = BPF_ALU | BPF_OP(OP) | BPF_X, \
458 .dst_reg = DST, \
459 .src_reg = SRC, \
460 .off = 0, \
461 .imm = 0 })
462
463 #define BPF_ALU64_IMM(OP, DST, IMM) \
464 ((struct bpf_insn) { \
465 .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
466 .dst_reg = DST, \
467 .src_reg = 0, \
468 .off = 0, \
469 .imm = IMM })
470
471 #define BPF_ALU32_IMM(OP, DST, IMM) \
472 ((struct bpf_insn) { \
473 .code = BPF_ALU | BPF_OP(OP) | BPF_K, \
474 .dst_reg = DST, \
475 .src_reg = 0, \
476 .off = 0, \
477 .imm = IMM })
478
479 #define BPF_MOV64_REG(DST, SRC) \
480 ((struct bpf_insn) { \
481 .code = BPF_ALU64 | BPF_MOV | BPF_X, \
482 .dst_reg = DST, \
483 .src_reg = SRC, \
484 .off = 0, \
485 .imm = 0 })
486
487 #define BPF_MOV32_REG(DST, SRC) \
488 ((struct bpf_insn) { \
489 .code = BPF_ALU | BPF_MOV | BPF_X, \
490 .dst_reg = DST, \
491 .src_reg = SRC, \
492 .off = 0, \
493 .imm = 0 })
494
495 #define BPF_LD_IMM64(DST, IMM) \
496 BPF_LD_IMM64_RAW(DST, 0, IMM)
497
498 #define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
499 ((struct bpf_insn) { \
500 .code = BPF_LD | BPF_DW | BPF_IMM, \
501 .dst_reg = DST, \
502 .src_reg = SRC, \
503 .off = 0, \
504 .imm = (uint32_t) (IMM) }), \
505 ((struct bpf_insn) { \
506 .code = 0, /* zero is reserved opcode */ \
507 .dst_reg = 0, \
508 .src_reg = 0, \
509 .off = 0, \
510 .imm = ((uint64_t) (IMM)) >> 32 })
511
512 /* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */
513 #define BPF_LD_MAP_FD(DST, MAP_FD) \
514 BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD)
515
516 #define BPF_ST_MEM(SIZE, DST, OFF, IMM) \
517 ((struct bpf_insn) { \
518 .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \
519 .dst_reg = DST, \
520 .src_reg = 0, \
521 .off = OFF, \
522 .imm = IMM })
523
524 #define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \
525 ((struct bpf_insn) { \
526 .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \
527 .dst_reg = DST, \
528 .src_reg = SRC, \
529 .off = OFF, \
530 .imm = 0 })
531
532 #define BPF_STX_MEM(SIZE, DST, SRC, OFF) \
533 ((struct bpf_insn) { \
534 .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \
535 .dst_reg = DST, \
536 .src_reg = SRC, \
537 .off = OFF, \
538 .imm = 0 })
539
540 #define BPF_JMP_IMM(OP, DST, IMM, OFF) \
541 ((struct bpf_insn) { \
542 .code = BPF_JMP | BPF_OP(OP) | BPF_K, \
543 .dst_reg = DST, \
544 .src_reg = 0, \
545 .off = OFF, \
546 .imm = IMM })
547
548 #define BPF_MOV64_IMM(DST, IMM) \
549 ((struct bpf_insn) { \
550 .code = BPF_ALU64 | BPF_MOV | BPF_K, \
551 .dst_reg = DST, \
552 .src_reg = 0, \
553 .off = 0, \
554 .imm = IMM })
555
556 #define BPF_MOV32_IMM(DST, IMM) \
557 ((struct bpf_insn) { \
558 .code = BPF_ALU | BPF_MOV | BPF_K, \
559 .dst_reg = DST, \
560 .src_reg = 0, \
561 .off = 0, \
562 .imm = IMM })
563
564 #define BPF_EMIT_CALL(FUNC) \
565 ((struct bpf_insn) { \
566 .code = BPF_JMP | BPF_CALL, \
567 .dst_reg = 0, \
568 .src_reg = 0, \
569 .off = 0, \
570 .imm = ((FUNC) - BPF_FUNC_unspec) })
571
572 #define BPF_EXIT_INSN() \
573 ((struct bpf_insn) { \
574 .code = BPF_JMP | BPF_EXIT, \
575 .dst_reg = 0, \
576 .src_reg = 0, \
577 .off = 0, \
578 .imm = 0 })
579
580 /* End copy from tools/include/filter.h */
581
582 /* Start copy from tools/lib/bpf */
ptr_to_u64(const void * ptr)583 static inline uint64_t ptr_to_u64(const void *ptr)
584 {
585 return (uint64_t) (unsigned long) ptr;
586 }
587
bpf(enum bpf_cmd cmd,union bpf_attr * attr,unsigned int size)588 static inline int bpf(enum bpf_cmd cmd, union bpf_attr *attr, unsigned int size)
589 {
590 return tst_syscall(__NR_bpf, cmd, attr, size);
591 }
592 /* End copy from tools/lib/bpf */
593
594 #endif /* LAPI_BPF_H__ */
595