xref: /openbmc/linux/tools/lib/bpf/bpf_helpers.h (revision de8c12110a130337c8e7e7b8250de0580e644dee)
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 /* Avoid 'linux/stddef.h' definition of '__always_inline'. */
33 #undef __always_inline
34 #define __always_inline inline __attribute__((always_inline))
35 
36 #ifndef __noinline
37 #define __noinline __attribute__((noinline))
38 #endif
39 #ifndef __weak
40 #define __weak __attribute__((weak))
41 #endif
42 
43 /* When utilizing vmlinux.h with BPF CO-RE, user BPF programs can't include
44  * any system-level headers (such as stddef.h, linux/version.h, etc), and
45  * commonly-used macros like NULL and KERNEL_VERSION aren't available through
46  * vmlinux.h. This just adds unnecessary hurdles and forces users to re-define
47  * them on their own. So as a convenience, provide such definitions here.
48  */
49 #ifndef NULL
50 #define NULL ((void *)0)
51 #endif
52 
53 #ifndef KERNEL_VERSION
54 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + ((c) > 255 ? 255 : (c))
55 #endif
56 
57 /*
58  * Helper macros to manipulate data structures
59  */
60 #ifndef offsetof
61 #define offsetof(TYPE, MEMBER)	((unsigned long)&((TYPE *)0)->MEMBER)
62 #endif
63 #ifndef container_of
64 #define container_of(ptr, type, member)				\
65 	({							\
66 		void *__mptr = (void *)(ptr);			\
67 		((type *)(__mptr - offsetof(type, member)));	\
68 	})
69 #endif
70 
71 /*
72  * Helper macro to throw a compilation error if __bpf_unreachable() gets
73  * built into the resulting code. This works given BPF back end does not
74  * implement __builtin_trap(). This is useful to assert that certain paths
75  * of the program code are never used and hence eliminated by the compiler.
76  *
77  * For example, consider a switch statement that covers known cases used by
78  * the program. __bpf_unreachable() can then reside in the default case. If
79  * the program gets extended such that a case is not covered in the switch
80  * statement, then it will throw a build error due to the default case not
81  * being compiled out.
82  */
83 #ifndef __bpf_unreachable
84 # define __bpf_unreachable()	__builtin_trap()
85 #endif
86 
87 /*
88  * Helper function to perform a tail call with a constant/immediate map slot.
89  */
90 #if __clang_major__ >= 8 && defined(__bpf__)
91 static __always_inline void
92 bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
93 {
94 	if (!__builtin_constant_p(slot))
95 		__bpf_unreachable();
96 
97 	/*
98 	 * Provide a hard guarantee that LLVM won't optimize setting r2 (map
99 	 * pointer) and r3 (constant map index) from _different paths_ ending
100 	 * up at the _same_ call insn as otherwise we won't be able to use the
101 	 * jmpq/nopl retpoline-free patching by the x86-64 JIT in the kernel
102 	 * given they mismatch. See also d2e4c1e6c294 ("bpf: Constant map key
103 	 * tracking for prog array pokes") for details on verifier tracking.
104 	 *
105 	 * Note on clobber list: we need to stay in-line with BPF calling
106 	 * convention, so even if we don't end up using r0, r4, r5, we need
107 	 * to mark them as clobber so that LLVM doesn't end up using them
108 	 * before / after the call.
109 	 */
110 	asm volatile("r1 = %[ctx]\n\t"
111 		     "r2 = %[map]\n\t"
112 		     "r3 = %[slot]\n\t"
113 		     "call 12"
114 		     :: [ctx]"r"(ctx), [map]"r"(map), [slot]"i"(slot)
115 		     : "r0", "r1", "r2", "r3", "r4", "r5");
116 }
117 #endif
118 
119 /*
120  * Helper structure used by eBPF C program
121  * to describe BPF map attributes to libbpf loader
122  */
123 struct bpf_map_def {
124 	unsigned int type;
125 	unsigned int key_size;
126 	unsigned int value_size;
127 	unsigned int max_entries;
128 	unsigned int map_flags;
129 };
130 
131 enum libbpf_pin_type {
132 	LIBBPF_PIN_NONE,
133 	/* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
134 	LIBBPF_PIN_BY_NAME,
135 };
136 
137 enum libbpf_tristate {
138 	TRI_NO = 0,
139 	TRI_YES = 1,
140 	TRI_MODULE = 2,
141 };
142 
143 #define __kconfig __attribute__((section(".kconfig")))
144 #define __ksym __attribute__((section(".ksyms")))
145 
146 #endif
147