1*4882a593Smuzhiyun /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2*4882a593Smuzhiyun #ifndef __BPF_HELPERS__
3*4882a593Smuzhiyun #define __BPF_HELPERS__
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun /*
6*4882a593Smuzhiyun * Note that bpf programs need to include either
7*4882a593Smuzhiyun * vmlinux.h (auto-generated from BTF) or linux/types.h
8*4882a593Smuzhiyun * in advance since bpf_helper_defs.h uses such types
9*4882a593Smuzhiyun * as __u64.
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun #include "bpf_helper_defs.h"
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #define __uint(name, val) int (*name)[val]
14*4882a593Smuzhiyun #define __type(name, val) typeof(val) *name
15*4882a593Smuzhiyun #define __array(name, val) typeof(val) *name[]
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun /* Helper macro to print out debug messages */
18*4882a593Smuzhiyun #define bpf_printk(fmt, ...) \
19*4882a593Smuzhiyun ({ \
20*4882a593Smuzhiyun char ____fmt[] = fmt; \
21*4882a593Smuzhiyun bpf_trace_printk(____fmt, sizeof(____fmt), \
22*4882a593Smuzhiyun ##__VA_ARGS__); \
23*4882a593Smuzhiyun })
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun /*
26*4882a593Smuzhiyun * Helper macro to place programs, maps, license in
27*4882a593Smuzhiyun * different sections in elf_bpf file. Section names
28*4882a593Smuzhiyun * are interpreted by elf_bpf loader
29*4882a593Smuzhiyun */
30*4882a593Smuzhiyun #define SEC(NAME) __attribute__((section(NAME), used))
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #ifndef __always_inline
33*4882a593Smuzhiyun #define __always_inline __attribute__((always_inline))
34*4882a593Smuzhiyun #endif
35*4882a593Smuzhiyun #ifndef __noinline
36*4882a593Smuzhiyun #define __noinline __attribute__((noinline))
37*4882a593Smuzhiyun #endif
38*4882a593Smuzhiyun #ifndef __weak
39*4882a593Smuzhiyun #define __weak __attribute__((weak))
40*4882a593Smuzhiyun #endif
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun /*
43*4882a593Smuzhiyun * Helper macro to manipulate data structures
44*4882a593Smuzhiyun */
45*4882a593Smuzhiyun #ifndef offsetof
46*4882a593Smuzhiyun #define offsetof(TYPE, MEMBER) ((unsigned long)&((TYPE *)0)->MEMBER)
47*4882a593Smuzhiyun #endif
48*4882a593Smuzhiyun #ifndef container_of
49*4882a593Smuzhiyun #define container_of(ptr, type, member) \
50*4882a593Smuzhiyun ({ \
51*4882a593Smuzhiyun void *__mptr = (void *)(ptr); \
52*4882a593Smuzhiyun ((type *)(__mptr - offsetof(type, member))); \
53*4882a593Smuzhiyun })
54*4882a593Smuzhiyun #endif
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun /*
57*4882a593Smuzhiyun * Helper macro to throw a compilation error if __bpf_unreachable() gets
58*4882a593Smuzhiyun * built into the resulting code. This works given BPF back end does not
59*4882a593Smuzhiyun * implement __builtin_trap(). This is useful to assert that certain paths
60*4882a593Smuzhiyun * of the program code are never used and hence eliminated by the compiler.
61*4882a593Smuzhiyun *
62*4882a593Smuzhiyun * For example, consider a switch statement that covers known cases used by
63*4882a593Smuzhiyun * the program. __bpf_unreachable() can then reside in the default case. If
64*4882a593Smuzhiyun * the program gets extended such that a case is not covered in the switch
65*4882a593Smuzhiyun * statement, then it will throw a build error due to the default case not
66*4882a593Smuzhiyun * being compiled out.
67*4882a593Smuzhiyun */
68*4882a593Smuzhiyun #ifndef __bpf_unreachable
69*4882a593Smuzhiyun # define __bpf_unreachable() __builtin_trap()
70*4882a593Smuzhiyun #endif
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun /*
73*4882a593Smuzhiyun * Helper function to perform a tail call with a constant/immediate map slot.
74*4882a593Smuzhiyun */
75*4882a593Smuzhiyun #if __clang_major__ >= 8 && defined(__bpf__)
76*4882a593Smuzhiyun static __always_inline void
bpf_tail_call_static(void * ctx,const void * map,const __u32 slot)77*4882a593Smuzhiyun bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun if (!__builtin_constant_p(slot))
80*4882a593Smuzhiyun __bpf_unreachable();
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun /*
83*4882a593Smuzhiyun * Provide a hard guarantee that LLVM won't optimize setting r2 (map
84*4882a593Smuzhiyun * pointer) and r3 (constant map index) from _different paths_ ending
85*4882a593Smuzhiyun * up at the _same_ call insn as otherwise we won't be able to use the
86*4882a593Smuzhiyun * jmpq/nopl retpoline-free patching by the x86-64 JIT in the kernel
87*4882a593Smuzhiyun * given they mismatch. See also d2e4c1e6c294 ("bpf: Constant map key
88*4882a593Smuzhiyun * tracking for prog array pokes") for details on verifier tracking.
89*4882a593Smuzhiyun *
90*4882a593Smuzhiyun * Note on clobber list: we need to stay in-line with BPF calling
91*4882a593Smuzhiyun * convention, so even if we don't end up using r0, r4, r5, we need
92*4882a593Smuzhiyun * to mark them as clobber so that LLVM doesn't end up using them
93*4882a593Smuzhiyun * before / after the call.
94*4882a593Smuzhiyun */
95*4882a593Smuzhiyun asm volatile("r1 = %[ctx]\n\t"
96*4882a593Smuzhiyun "r2 = %[map]\n\t"
97*4882a593Smuzhiyun "r3 = %[slot]\n\t"
98*4882a593Smuzhiyun "call 12"
99*4882a593Smuzhiyun :: [ctx]"r"(ctx), [map]"r"(map), [slot]"i"(slot)
100*4882a593Smuzhiyun : "r0", "r1", "r2", "r3", "r4", "r5");
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun #endif
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun /*
105*4882a593Smuzhiyun * Helper structure used by eBPF C program
106*4882a593Smuzhiyun * to describe BPF map attributes to libbpf loader
107*4882a593Smuzhiyun */
108*4882a593Smuzhiyun struct bpf_map_def {
109*4882a593Smuzhiyun unsigned int type;
110*4882a593Smuzhiyun unsigned int key_size;
111*4882a593Smuzhiyun unsigned int value_size;
112*4882a593Smuzhiyun unsigned int max_entries;
113*4882a593Smuzhiyun unsigned int map_flags;
114*4882a593Smuzhiyun };
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun enum libbpf_pin_type {
117*4882a593Smuzhiyun LIBBPF_PIN_NONE,
118*4882a593Smuzhiyun /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
119*4882a593Smuzhiyun LIBBPF_PIN_BY_NAME,
120*4882a593Smuzhiyun };
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun enum libbpf_tristate {
123*4882a593Smuzhiyun TRI_NO = 0,
124*4882a593Smuzhiyun TRI_YES = 1,
125*4882a593Smuzhiyun TRI_MODULE = 2,
126*4882a593Smuzhiyun };
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun #define __kconfig __attribute__((section(".kconfig")))
129*4882a593Smuzhiyun #define __ksym __attribute__((section(".ksyms")))
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun #endif
132