Andrii Nakryiko | 929ffa6 | 2019-08-15 22:45:43 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ |
Ilya Leoshkevich | 4ce150b | 2019-09-12 18:05:43 +0200 | [diff] [blame] | 2 | #ifndef __BPF_HELPERS__ |
| 3 | #define __BPF_HELPERS__ |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 4 | |
Yoshiki Komachi | ae460c0 | 2020-04-21 09:05:27 +0900 | [diff] [blame] | 5 | /* |
| 6 | * Note that bpf programs need to include either |
| 7 | * vmlinux.h (auto-generated from BTF) or linux/types.h |
| 8 | * in advance since bpf_helper_defs.h uses such types |
| 9 | * as __u64. |
| 10 | */ |
Andrii Nakryiko | 24f2576 | 2019-10-06 20:07:38 -0700 | [diff] [blame] | 11 | #include "bpf_helper_defs.h" |
| 12 | |
Andrii Nakryiko | 00acd00 | 2019-07-05 08:50:10 -0700 | [diff] [blame] | 13 | #define __uint(name, val) int (*name)[val] |
Andrii Nakryiko | a53ba15 | 2019-10-03 21:02:11 -0700 | [diff] [blame] | 14 | #define __type(name, val) typeof(val) *name |
Andrii Nakryiko | 646f02f | 2020-04-28 17:27:39 -0700 | [diff] [blame] | 15 | #define __array(name, val) typeof(val) *name[] |
Andrii Nakryiko | 00acd00 | 2019-07-05 08:50:10 -0700 | [diff] [blame] | 16 | |
Andrii Nakryiko | 7db3822 | 2019-10-08 10:59:41 -0700 | [diff] [blame] | 17 | /* Helper macro to print out debug messages */ |
Michal Rostecki | 37739d1 | 2019-05-23 14:53:54 +0200 | [diff] [blame] | 18 | #define bpf_printk(fmt, ...) \ |
| 19 | ({ \ |
| 20 | char ____fmt[] = fmt; \ |
| 21 | bpf_trace_printk(____fmt, sizeof(____fmt), \ |
| 22 | ##__VA_ARGS__); \ |
| 23 | }) |
| 24 | |
Andrii Nakryiko | 7db3822 | 2019-10-08 10:59:41 -0700 | [diff] [blame] | 25 | /* |
| 26 | * Helper macro to place programs, maps, license in |
Ilya Leoshkevich | 4ce150b | 2019-09-12 18:05:43 +0200 | [diff] [blame] | 27 | * different sections in elf_bpf file. Section names |
| 28 | * are interpreted by elf_bpf loader |
| 29 | */ |
| 30 | #define SEC(NAME) __attribute__((section(NAME), used)) |
| 31 | |
Andrii Nakryiko | 7db3822 | 2019-10-08 10:59:41 -0700 | [diff] [blame] | 32 | #ifndef __always_inline |
| 33 | #define __always_inline __attribute__((always_inline)) |
| 34 | #endif |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 35 | #ifndef __weak |
| 36 | #define __weak __attribute__((weak)) |
| 37 | #endif |
Andrii Nakryiko | 7db3822 | 2019-10-08 10:59:41 -0700 | [diff] [blame] | 38 | |
| 39 | /* |
Yonghong Song | 5fbc220 | 2020-05-09 10:59:19 -0700 | [diff] [blame] | 40 | * Helper macro to manipulate data structures |
| 41 | */ |
| 42 | #ifndef offsetof |
Yonghong Song | 63fe3fd | 2020-08-10 20:08:52 -0700 | [diff] [blame] | 43 | #define offsetof(TYPE, MEMBER) ((unsigned long)&((TYPE *)0)->MEMBER) |
Yonghong Song | 5fbc220 | 2020-05-09 10:59:19 -0700 | [diff] [blame] | 44 | #endif |
| 45 | #ifndef container_of |
| 46 | #define container_of(ptr, type, member) \ |
| 47 | ({ \ |
| 48 | void *__mptr = (void *)(ptr); \ |
| 49 | ((type *)(__mptr - offsetof(type, member))); \ |
| 50 | }) |
| 51 | #endif |
| 52 | |
| 53 | /* |
Andrii Nakryiko | 7db3822 | 2019-10-08 10:59:41 -0700 | [diff] [blame] | 54 | * Helper structure used by eBPF C program |
Andrii Nakryiko | 36b5d47 | 2019-10-08 10:59:37 -0700 | [diff] [blame] | 55 | * to describe BPF map attributes to libbpf loader |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 56 | */ |
| 57 | struct bpf_map_def { |
| 58 | unsigned int type; |
| 59 | unsigned int key_size; |
| 60 | unsigned int value_size; |
| 61 | unsigned int max_entries; |
Alexei Starovoitov | 89b9760 | 2016-03-07 21:57:20 -0800 | [diff] [blame] | 62 | unsigned int map_flags; |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 63 | }; |
| 64 | |
Toke Høiland-Jørgensen | 57a00f4 | 2019-11-02 12:09:41 +0100 | [diff] [blame] | 65 | enum libbpf_pin_type { |
| 66 | LIBBPF_PIN_NONE, |
| 67 | /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */ |
| 68 | LIBBPF_PIN_BY_NAME, |
| 69 | }; |
| 70 | |
Andrii Nakryiko | 166750b | 2019-12-13 17:47:08 -0800 | [diff] [blame] | 71 | enum libbpf_tristate { |
| 72 | TRI_NO = 0, |
| 73 | TRI_YES = 1, |
| 74 | TRI_MODULE = 2, |
| 75 | }; |
| 76 | |
Andrii Nakryiko | 81bfdd0 | 2019-12-18 16:28:34 -0800 | [diff] [blame] | 77 | #define __kconfig __attribute__((section(".kconfig"))) |
Andrii Nakryiko | 1c0c707 | 2020-06-19 16:16:56 -0700 | [diff] [blame] | 78 | #define __ksym __attribute__((section(".ksyms"))) |
Andrii Nakryiko | 81bfdd0 | 2019-12-18 16:28:34 -0800 | [diff] [blame] | 79 | |
Alexei Starovoitov | 249b812 | 2014-12-01 15:06:37 -0800 | [diff] [blame] | 80 | #endif |