1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ 2 #ifndef __BPF_HELPERS__ 3 #define __BPF_HELPERS__ 4 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 */ 11 #include "bpf_helper_defs.h" 12 13 #define __uint(name, val) int (*name)[val] 14 #define __type(name, val) typeof(val) *name 15 #define __array(name, val) typeof(val) *name[] 16 17 /* Helper macro to print out debug messages */ 18 #define bpf_printk(fmt, ...) \ 19 ({ \ 20 char ____fmt[] = fmt; \ 21 bpf_trace_printk(____fmt, sizeof(____fmt), \ 22 ##__VA_ARGS__); \ 23 }) 24 25 /* 26 * Helper macro to place programs, maps, license in 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 32 #ifndef __always_inline 33 #define __always_inline __attribute__((always_inline)) 34 #endif 35 #ifndef __weak 36 #define __weak __attribute__((weak)) 37 #endif 38 39 /* 40 * Helper macro to manipulate data structures 41 */ 42 #ifndef offsetof 43 #define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER) 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 /* 54 * Helper structure used by eBPF C program 55 * to describe BPF map attributes to libbpf loader 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; 62 unsigned int map_flags; 63 }; 64 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 71 enum libbpf_tristate { 72 TRI_NO = 0, 73 TRI_YES = 1, 74 TRI_MODULE = 2, 75 }; 76 77 #define __kconfig __attribute__((section(".kconfig"))) 78 79 #endif 80