1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 #ifndef __BPF_HELPERS__
3 #define __BPF_HELPERS__
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
11 #include "bpf_helper_defs.h"
13 #define __uint(name, val) int (*name)[val]
14 #define __type(name, val) typeof(val) *name
15 #define __array(name, val) typeof(val) *name[]
17 /* Helper macro to print out debug messages */
18 #define bpf_printk(fmt, ...) \
20 char ____fmt[] = fmt; \
21 bpf_trace_printk(____fmt, sizeof(____fmt), \
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
30 #define SEC(NAME) __attribute__((section(NAME), used))
32 #ifndef __always_inline
33 #define __always_inline __attribute__((always_inline))
36 #define __noinline __attribute__((noinline))
39 #define __weak __attribute__((weak))
43 * Helper macro to manipulate data structures
46 /* offsetof() definition that uses __builtin_offset() might not preserve field
47 * offset CO-RE relocation properly, so force-redefine offsetof() using
48 * old-school approach which works with CO-RE correctly
51 #define offsetof(type, member) ((unsigned long)&((type *)0)->member)
53 /* redefined container_of() to ensure we use the above offsetof() macro */
55 #define container_of(ptr, type, member) \
57 void *__mptr = (void *)(ptr); \
58 ((type *)(__mptr - offsetof(type, member))); \
62 * Helper macro to throw a compilation error if __bpf_unreachable() gets
63 * built into the resulting code. This works given BPF back end does not
64 * implement __builtin_trap(). This is useful to assert that certain paths
65 * of the program code are never used and hence eliminated by the compiler.
67 * For example, consider a switch statement that covers known cases used by
68 * the program. __bpf_unreachable() can then reside in the default case. If
69 * the program gets extended such that a case is not covered in the switch
70 * statement, then it will throw a build error due to the default case not
73 #ifndef __bpf_unreachable
74 # define __bpf_unreachable() __builtin_trap()
78 * Helper function to perform a tail call with a constant/immediate map slot.
80 #if __clang_major__ >= 8 && defined(__bpf__)
81 static __always_inline void
82 bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
84 if (!__builtin_constant_p(slot))
88 * Provide a hard guarantee that LLVM won't optimize setting r2 (map
89 * pointer) and r3 (constant map index) from _different paths_ ending
90 * up at the _same_ call insn as otherwise we won't be able to use the
91 * jmpq/nopl retpoline-free patching by the x86-64 JIT in the kernel
92 * given they mismatch. See also d2e4c1e6c294 ("bpf: Constant map key
93 * tracking for prog array pokes") for details on verifier tracking.
95 * Note on clobber list: we need to stay in-line with BPF calling
96 * convention, so even if we don't end up using r0, r4, r5, we need
97 * to mark them as clobber so that LLVM doesn't end up using them
98 * before / after the call.
100 asm volatile("r1 = %[ctx]\n\t"
104 :: [ctx]"r"(ctx), [map]"r"(map), [slot]"i"(slot)
105 : "r0", "r1", "r2", "r3", "r4", "r5");
110 * Helper structure used by eBPF C program
111 * to describe BPF map attributes to libbpf loader
115 unsigned int key_size;
116 unsigned int value_size;
117 unsigned int max_entries;
118 unsigned int map_flags;
121 enum libbpf_pin_type {
123 /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
127 enum libbpf_tristate {
133 #define __kconfig __attribute__((section(".kconfig")))
134 #define __ksym __attribute__((section(".ksyms")))