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 /* 18 * Helper macro to place programs, maps, license in 19 * different sections in elf_bpf file. Section names 20 * are interpreted by libbpf depending on the context (BPF programs, BPF maps, 21 * extern variables, etc). 22 * To allow use of SEC() with externs (e.g., for extern .maps declarations), 23 * make sure __attribute__((unused)) doesn't trigger compilation warning. 24 */ 25 #define SEC(name) \ 26 _Pragma("GCC diagnostic push") \ 27 _Pragma("GCC diagnostic ignored \"-Wignored-attributes\"") \ 28 __attribute__((section(name), used)) \ 29 _Pragma("GCC diagnostic pop") \ 30 31 /* Avoid 'linux/stddef.h' definition of '__always_inline'. */ 32 #undef __always_inline 33 #define __always_inline inline __attribute__((always_inline)) 34 35 #ifndef __noinline 36 #define __noinline __attribute__((noinline)) 37 #endif 38 #ifndef __weak 39 #define __weak __attribute__((weak)) 40 #endif 41 42 /* 43 * Use __hidden attribute to mark a non-static BPF subprogram effectively 44 * static for BPF verifier's verification algorithm purposes, allowing more 45 * extensive and permissive BPF verification process, taking into account 46 * subprogram's caller context. 47 */ 48 #define __hidden __attribute__((visibility("hidden"))) 49 50 /* When utilizing vmlinux.h with BPF CO-RE, user BPF programs can't include 51 * any system-level headers (such as stddef.h, linux/version.h, etc), and 52 * commonly-used macros like NULL and KERNEL_VERSION aren't available through 53 * vmlinux.h. This just adds unnecessary hurdles and forces users to re-define 54 * them on their own. So as a convenience, provide such definitions here. 55 */ 56 #ifndef NULL 57 #define NULL ((void *)0) 58 #endif 59 60 #ifndef KERNEL_VERSION 61 #define KERNEL_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + ((c) > 255 ? 255 : (c))) 62 #endif 63 64 /* 65 * Helper macros to manipulate data structures 66 */ 67 #ifndef offsetof 68 #define offsetof(TYPE, MEMBER) ((unsigned long)&((TYPE *)0)->MEMBER) 69 #endif 70 #ifndef container_of 71 #define container_of(ptr, type, member) \ 72 ({ \ 73 void *__mptr = (void *)(ptr); \ 74 ((type *)(__mptr - offsetof(type, member))); \ 75 }) 76 #endif 77 78 /* 79 * Helper macro to throw a compilation error if __bpf_unreachable() gets 80 * built into the resulting code. This works given BPF back end does not 81 * implement __builtin_trap(). This is useful to assert that certain paths 82 * of the program code are never used and hence eliminated by the compiler. 83 * 84 * For example, consider a switch statement that covers known cases used by 85 * the program. __bpf_unreachable() can then reside in the default case. If 86 * the program gets extended such that a case is not covered in the switch 87 * statement, then it will throw a build error due to the default case not 88 * being compiled out. 89 */ 90 #ifndef __bpf_unreachable 91 # define __bpf_unreachable() __builtin_trap() 92 #endif 93 94 /* 95 * Helper function to perform a tail call with a constant/immediate map slot. 96 */ 97 #if __clang_major__ >= 8 && defined(__bpf__) 98 static __always_inline void 99 bpf_tail_call_static(void *ctx, const void *map, const __u32 slot) 100 { 101 if (!__builtin_constant_p(slot)) 102 __bpf_unreachable(); 103 104 /* 105 * Provide a hard guarantee that LLVM won't optimize setting r2 (map 106 * pointer) and r3 (constant map index) from _different paths_ ending 107 * up at the _same_ call insn as otherwise we won't be able to use the 108 * jmpq/nopl retpoline-free patching by the x86-64 JIT in the kernel 109 * given they mismatch. See also d2e4c1e6c294 ("bpf: Constant map key 110 * tracking for prog array pokes") for details on verifier tracking. 111 * 112 * Note on clobber list: we need to stay in-line with BPF calling 113 * convention, so even if we don't end up using r0, r4, r5, we need 114 * to mark them as clobber so that LLVM doesn't end up using them 115 * before / after the call. 116 */ 117 asm volatile("r1 = %[ctx]\n\t" 118 "r2 = %[map]\n\t" 119 "r3 = %[slot]\n\t" 120 "call 12" 121 :: [ctx]"r"(ctx), [map]"r"(map), [slot]"i"(slot) 122 : "r0", "r1", "r2", "r3", "r4", "r5"); 123 } 124 #endif 125 126 /* 127 * Helper structure used by eBPF C program 128 * to describe BPF map attributes to libbpf loader 129 */ 130 struct bpf_map_def { 131 unsigned int type; 132 unsigned int key_size; 133 unsigned int value_size; 134 unsigned int max_entries; 135 unsigned int map_flags; 136 } __attribute__((deprecated("use BTF-defined maps in .maps section"))); 137 138 enum libbpf_pin_type { 139 LIBBPF_PIN_NONE, 140 /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */ 141 LIBBPF_PIN_BY_NAME, 142 }; 143 144 enum libbpf_tristate { 145 TRI_NO = 0, 146 TRI_YES = 1, 147 TRI_MODULE = 2, 148 }; 149 150 #define __kconfig __attribute__((section(".kconfig"))) 151 #define __ksym __attribute__((section(".ksyms"))) 152 #if __has_attribute(btf_type_tag) 153 #define __kptr __attribute__((btf_type_tag("kptr"))) 154 #define __kptr_ref __attribute__((btf_type_tag("kptr_ref"))) 155 #else 156 #define __kptr 157 #define __kptr_ref 158 #endif 159 160 #ifndef ___bpf_concat 161 #define ___bpf_concat(a, b) a ## b 162 #endif 163 #ifndef ___bpf_apply 164 #define ___bpf_apply(fn, n) ___bpf_concat(fn, n) 165 #endif 166 #ifndef ___bpf_nth 167 #define ___bpf_nth(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c, N, ...) N 168 #endif 169 #ifndef ___bpf_narg 170 #define ___bpf_narg(...) \ 171 ___bpf_nth(_, ##__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) 172 #endif 173 174 #define ___bpf_fill0(arr, p, x) do {} while (0) 175 #define ___bpf_fill1(arr, p, x) arr[p] = x 176 #define ___bpf_fill2(arr, p, x, args...) arr[p] = x; ___bpf_fill1(arr, p + 1, args) 177 #define ___bpf_fill3(arr, p, x, args...) arr[p] = x; ___bpf_fill2(arr, p + 1, args) 178 #define ___bpf_fill4(arr, p, x, args...) arr[p] = x; ___bpf_fill3(arr, p + 1, args) 179 #define ___bpf_fill5(arr, p, x, args...) arr[p] = x; ___bpf_fill4(arr, p + 1, args) 180 #define ___bpf_fill6(arr, p, x, args...) arr[p] = x; ___bpf_fill5(arr, p + 1, args) 181 #define ___bpf_fill7(arr, p, x, args...) arr[p] = x; ___bpf_fill6(arr, p + 1, args) 182 #define ___bpf_fill8(arr, p, x, args...) arr[p] = x; ___bpf_fill7(arr, p + 1, args) 183 #define ___bpf_fill9(arr, p, x, args...) arr[p] = x; ___bpf_fill8(arr, p + 1, args) 184 #define ___bpf_fill10(arr, p, x, args...) arr[p] = x; ___bpf_fill9(arr, p + 1, args) 185 #define ___bpf_fill11(arr, p, x, args...) arr[p] = x; ___bpf_fill10(arr, p + 1, args) 186 #define ___bpf_fill12(arr, p, x, args...) arr[p] = x; ___bpf_fill11(arr, p + 1, args) 187 #define ___bpf_fill(arr, args...) \ 188 ___bpf_apply(___bpf_fill, ___bpf_narg(args))(arr, 0, args) 189 190 /* 191 * BPF_SEQ_PRINTF to wrap bpf_seq_printf to-be-printed values 192 * in a structure. 193 */ 194 #define BPF_SEQ_PRINTF(seq, fmt, args...) \ 195 ({ \ 196 static const char ___fmt[] = fmt; \ 197 unsigned long long ___param[___bpf_narg(args)]; \ 198 \ 199 _Pragma("GCC diagnostic push") \ 200 _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ 201 ___bpf_fill(___param, args); \ 202 _Pragma("GCC diagnostic pop") \ 203 \ 204 bpf_seq_printf(seq, ___fmt, sizeof(___fmt), \ 205 ___param, sizeof(___param)); \ 206 }) 207 208 /* 209 * BPF_SNPRINTF wraps the bpf_snprintf helper with variadic arguments instead of 210 * an array of u64. 211 */ 212 #define BPF_SNPRINTF(out, out_size, fmt, args...) \ 213 ({ \ 214 static const char ___fmt[] = fmt; \ 215 unsigned long long ___param[___bpf_narg(args)]; \ 216 \ 217 _Pragma("GCC diagnostic push") \ 218 _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ 219 ___bpf_fill(___param, args); \ 220 _Pragma("GCC diagnostic pop") \ 221 \ 222 bpf_snprintf(out, out_size, ___fmt, \ 223 ___param, sizeof(___param)); \ 224 }) 225 226 #ifdef BPF_NO_GLOBAL_DATA 227 #define BPF_PRINTK_FMT_MOD 228 #else 229 #define BPF_PRINTK_FMT_MOD static const 230 #endif 231 232 #define __bpf_printk(fmt, ...) \ 233 ({ \ 234 BPF_PRINTK_FMT_MOD char ____fmt[] = fmt; \ 235 bpf_trace_printk(____fmt, sizeof(____fmt), \ 236 ##__VA_ARGS__); \ 237 }) 238 239 /* 240 * __bpf_vprintk wraps the bpf_trace_vprintk helper with variadic arguments 241 * instead of an array of u64. 242 */ 243 #define __bpf_vprintk(fmt, args...) \ 244 ({ \ 245 static const char ___fmt[] = fmt; \ 246 unsigned long long ___param[___bpf_narg(args)]; \ 247 \ 248 _Pragma("GCC diagnostic push") \ 249 _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ 250 ___bpf_fill(___param, args); \ 251 _Pragma("GCC diagnostic pop") \ 252 \ 253 bpf_trace_vprintk(___fmt, sizeof(___fmt), \ 254 ___param, sizeof(___param)); \ 255 }) 256 257 /* Use __bpf_printk when bpf_printk call has 3 or fewer fmt args 258 * Otherwise use __bpf_vprintk 259 */ 260 #define ___bpf_pick_printk(...) \ 261 ___bpf_nth(_, ##__VA_ARGS__, __bpf_vprintk, __bpf_vprintk, __bpf_vprintk, \ 262 __bpf_vprintk, __bpf_vprintk, __bpf_vprintk, __bpf_vprintk, \ 263 __bpf_vprintk, __bpf_vprintk, __bpf_printk /*3*/, __bpf_printk /*2*/,\ 264 __bpf_printk /*1*/, __bpf_printk /*0*/) 265 266 /* Helper macro to print out debug messages */ 267 #define bpf_printk(fmt, args...) ___bpf_pick_printk(args)(fmt, ##args) 268 269 #endif 270