xref: /openbmc/linux/tools/lib/bpf/bpf_helpers.h (revision a5961bed)
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 #if __GNUC__ && !__clang__
26 
27 /*
28  * Pragma macros are broken on GCC
29  * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55578
30  * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90400
31  */
32 #define SEC(name) __attribute__((section(name), used))
33 
34 #else
35 
36 #define SEC(name) \
37 	_Pragma("GCC diagnostic push")					    \
38 	_Pragma("GCC diagnostic ignored \"-Wignored-attributes\"")	    \
39 	__attribute__((section(name), used))				    \
40 	_Pragma("GCC diagnostic pop")					    \
41 
42 #endif
43 
44 /* Avoid 'linux/stddef.h' definition of '__always_inline'. */
45 #undef __always_inline
46 #define __always_inline inline __attribute__((always_inline))
47 
48 #ifndef __noinline
49 #define __noinline __attribute__((noinline))
50 #endif
51 #ifndef __weak
52 #define __weak __attribute__((weak))
53 #endif
54 
55 /*
56  * Use __hidden attribute to mark a non-static BPF subprogram effectively
57  * static for BPF verifier's verification algorithm purposes, allowing more
58  * extensive and permissive BPF verification process, taking into account
59  * subprogram's caller context.
60  */
61 #define __hidden __attribute__((visibility("hidden")))
62 
63 /* When utilizing vmlinux.h with BPF CO-RE, user BPF programs can't include
64  * any system-level headers (such as stddef.h, linux/version.h, etc), and
65  * commonly-used macros like NULL and KERNEL_VERSION aren't available through
66  * vmlinux.h. This just adds unnecessary hurdles and forces users to re-define
67  * them on their own. So as a convenience, provide such definitions here.
68  */
69 #ifndef NULL
70 #define NULL ((void *)0)
71 #endif
72 
73 #ifndef KERNEL_VERSION
74 #define KERNEL_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + ((c) > 255 ? 255 : (c)))
75 #endif
76 
77 /*
78  * Helper macros to manipulate data structures
79  */
80 #ifndef offsetof
81 #define offsetof(TYPE, MEMBER)	((unsigned long)&((TYPE *)0)->MEMBER)
82 #endif
83 #ifndef container_of
84 #define container_of(ptr, type, member)				\
85 	({							\
86 		void *__mptr = (void *)(ptr);			\
87 		((type *)(__mptr - offsetof(type, member)));	\
88 	})
89 #endif
90 
91 /*
92  * Compiler (optimization) barrier.
93  */
94 #ifndef barrier
95 #define barrier() asm volatile("" ::: "memory")
96 #endif
97 
98 /* Variable-specific compiler (optimization) barrier. It's a no-op which makes
99  * compiler believe that there is some black box modification of a given
100  * variable and thus prevents compiler from making extra assumption about its
101  * value and potential simplifications and optimizations on this variable.
102  *
103  * E.g., compiler might often delay or even omit 32-bit to 64-bit casting of
104  * a variable, making some code patterns unverifiable. Putting barrier_var()
105  * in place will ensure that cast is performed before the barrier_var()
106  * invocation, because compiler has to pessimistically assume that embedded
107  * asm section might perform some extra operations on that variable.
108  *
109  * This is a variable-specific variant of more global barrier().
110  */
111 #ifndef barrier_var
112 #define barrier_var(var) asm volatile("" : "+r"(var))
113 #endif
114 
115 /*
116  * Helper macro to throw a compilation error if __bpf_unreachable() gets
117  * built into the resulting code. This works given BPF back end does not
118  * implement __builtin_trap(). This is useful to assert that certain paths
119  * of the program code are never used and hence eliminated by the compiler.
120  *
121  * For example, consider a switch statement that covers known cases used by
122  * the program. __bpf_unreachable() can then reside in the default case. If
123  * the program gets extended such that a case is not covered in the switch
124  * statement, then it will throw a build error due to the default case not
125  * being compiled out.
126  */
127 #ifndef __bpf_unreachable
128 # define __bpf_unreachable()	__builtin_trap()
129 #endif
130 
131 /*
132  * Helper function to perform a tail call with a constant/immediate map slot.
133  */
134 #if __clang_major__ >= 8 && defined(__bpf__)
135 static __always_inline void
136 bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
137 {
138 	if (!__builtin_constant_p(slot))
139 		__bpf_unreachable();
140 
141 	/*
142 	 * Provide a hard guarantee that LLVM won't optimize setting r2 (map
143 	 * pointer) and r3 (constant map index) from _different paths_ ending
144 	 * up at the _same_ call insn as otherwise we won't be able to use the
145 	 * jmpq/nopl retpoline-free patching by the x86-64 JIT in the kernel
146 	 * given they mismatch. See also d2e4c1e6c294 ("bpf: Constant map key
147 	 * tracking for prog array pokes") for details on verifier tracking.
148 	 *
149 	 * Note on clobber list: we need to stay in-line with BPF calling
150 	 * convention, so even if we don't end up using r0, r4, r5, we need
151 	 * to mark them as clobber so that LLVM doesn't end up using them
152 	 * before / after the call.
153 	 */
154 	asm volatile("r1 = %[ctx]\n\t"
155 		     "r2 = %[map]\n\t"
156 		     "r3 = %[slot]\n\t"
157 		     "call 12"
158 		     :: [ctx]"r"(ctx), [map]"r"(map), [slot]"i"(slot)
159 		     : "r0", "r1", "r2", "r3", "r4", "r5");
160 }
161 #endif
162 
163 enum libbpf_pin_type {
164 	LIBBPF_PIN_NONE,
165 	/* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
166 	LIBBPF_PIN_BY_NAME,
167 };
168 
169 enum libbpf_tristate {
170 	TRI_NO = 0,
171 	TRI_YES = 1,
172 	TRI_MODULE = 2,
173 };
174 
175 #define __kconfig __attribute__((section(".kconfig")))
176 #define __ksym __attribute__((section(".ksyms")))
177 #define __kptr_untrusted __attribute__((btf_type_tag("kptr_untrusted")))
178 #define __kptr __attribute__((btf_type_tag("kptr")))
179 
180 #define bpf_ksym_exists(sym) ({									\
181 	_Static_assert(!__builtin_constant_p(!!sym), #sym " should be marked as __weak");	\
182 	!!sym;											\
183 })
184 
185 #ifndef ___bpf_concat
186 #define ___bpf_concat(a, b) a ## b
187 #endif
188 #ifndef ___bpf_apply
189 #define ___bpf_apply(fn, n) ___bpf_concat(fn, n)
190 #endif
191 #ifndef ___bpf_nth
192 #define ___bpf_nth(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c, N, ...) N
193 #endif
194 #ifndef ___bpf_narg
195 #define ___bpf_narg(...) \
196 	___bpf_nth(_, ##__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
197 #endif
198 
199 #define ___bpf_fill0(arr, p, x) do {} while (0)
200 #define ___bpf_fill1(arr, p, x) arr[p] = x
201 #define ___bpf_fill2(arr, p, x, args...) arr[p] = x; ___bpf_fill1(arr, p + 1, args)
202 #define ___bpf_fill3(arr, p, x, args...) arr[p] = x; ___bpf_fill2(arr, p + 1, args)
203 #define ___bpf_fill4(arr, p, x, args...) arr[p] = x; ___bpf_fill3(arr, p + 1, args)
204 #define ___bpf_fill5(arr, p, x, args...) arr[p] = x; ___bpf_fill4(arr, p + 1, args)
205 #define ___bpf_fill6(arr, p, x, args...) arr[p] = x; ___bpf_fill5(arr, p + 1, args)
206 #define ___bpf_fill7(arr, p, x, args...) arr[p] = x; ___bpf_fill6(arr, p + 1, args)
207 #define ___bpf_fill8(arr, p, x, args...) arr[p] = x; ___bpf_fill7(arr, p + 1, args)
208 #define ___bpf_fill9(arr, p, x, args...) arr[p] = x; ___bpf_fill8(arr, p + 1, args)
209 #define ___bpf_fill10(arr, p, x, args...) arr[p] = x; ___bpf_fill9(arr, p + 1, args)
210 #define ___bpf_fill11(arr, p, x, args...) arr[p] = x; ___bpf_fill10(arr, p + 1, args)
211 #define ___bpf_fill12(arr, p, x, args...) arr[p] = x; ___bpf_fill11(arr, p + 1, args)
212 #define ___bpf_fill(arr, args...) \
213 	___bpf_apply(___bpf_fill, ___bpf_narg(args))(arr, 0, args)
214 
215 /*
216  * BPF_SEQ_PRINTF to wrap bpf_seq_printf to-be-printed values
217  * in a structure.
218  */
219 #define BPF_SEQ_PRINTF(seq, fmt, args...)			\
220 ({								\
221 	static const char ___fmt[] = fmt;			\
222 	unsigned long long ___param[___bpf_narg(args)];		\
223 								\
224 	_Pragma("GCC diagnostic push")				\
225 	_Pragma("GCC diagnostic ignored \"-Wint-conversion\"")	\
226 	___bpf_fill(___param, args);				\
227 	_Pragma("GCC diagnostic pop")				\
228 								\
229 	bpf_seq_printf(seq, ___fmt, sizeof(___fmt),		\
230 		       ___param, sizeof(___param));		\
231 })
232 
233 /*
234  * BPF_SNPRINTF wraps the bpf_snprintf helper with variadic arguments instead of
235  * an array of u64.
236  */
237 #define BPF_SNPRINTF(out, out_size, fmt, args...)		\
238 ({								\
239 	static const char ___fmt[] = fmt;			\
240 	unsigned long long ___param[___bpf_narg(args)];		\
241 								\
242 	_Pragma("GCC diagnostic push")				\
243 	_Pragma("GCC diagnostic ignored \"-Wint-conversion\"")	\
244 	___bpf_fill(___param, args);				\
245 	_Pragma("GCC diagnostic pop")				\
246 								\
247 	bpf_snprintf(out, out_size, ___fmt,			\
248 		     ___param, sizeof(___param));		\
249 })
250 
251 #ifdef BPF_NO_GLOBAL_DATA
252 #define BPF_PRINTK_FMT_MOD
253 #else
254 #define BPF_PRINTK_FMT_MOD static const
255 #endif
256 
257 #define __bpf_printk(fmt, ...)				\
258 ({							\
259 	BPF_PRINTK_FMT_MOD char ____fmt[] = fmt;	\
260 	bpf_trace_printk(____fmt, sizeof(____fmt),	\
261 			 ##__VA_ARGS__);		\
262 })
263 
264 /*
265  * __bpf_vprintk wraps the bpf_trace_vprintk helper with variadic arguments
266  * instead of an array of u64.
267  */
268 #define __bpf_vprintk(fmt, args...)				\
269 ({								\
270 	static const char ___fmt[] = fmt;			\
271 	unsigned long long ___param[___bpf_narg(args)];		\
272 								\
273 	_Pragma("GCC diagnostic push")				\
274 	_Pragma("GCC diagnostic ignored \"-Wint-conversion\"")	\
275 	___bpf_fill(___param, args);				\
276 	_Pragma("GCC diagnostic pop")				\
277 								\
278 	bpf_trace_vprintk(___fmt, sizeof(___fmt),		\
279 			  ___param, sizeof(___param));		\
280 })
281 
282 /* Use __bpf_printk when bpf_printk call has 3 or fewer fmt args
283  * Otherwise use __bpf_vprintk
284  */
285 #define ___bpf_pick_printk(...) \
286 	___bpf_nth(_, ##__VA_ARGS__, __bpf_vprintk, __bpf_vprintk, __bpf_vprintk,	\
287 		   __bpf_vprintk, __bpf_vprintk, __bpf_vprintk, __bpf_vprintk,		\
288 		   __bpf_vprintk, __bpf_vprintk, __bpf_printk /*3*/, __bpf_printk /*2*/,\
289 		   __bpf_printk /*1*/, __bpf_printk /*0*/)
290 
291 /* Helper macro to print out debug messages */
292 #define bpf_printk(fmt, args...) ___bpf_pick_printk(args)(fmt, ##args)
293 
294 struct bpf_iter_num;
295 
296 extern int bpf_iter_num_new(struct bpf_iter_num *it, int start, int end) __weak __ksym;
297 extern int *bpf_iter_num_next(struct bpf_iter_num *it) __weak __ksym;
298 extern void bpf_iter_num_destroy(struct bpf_iter_num *it) __weak __ksym;
299 
300 #ifndef bpf_for_each
301 /* bpf_for_each(iter_type, cur_elem, args...) provides generic construct for
302  * using BPF open-coded iterators without having to write mundane explicit
303  * low-level loop logic. Instead, it provides for()-like generic construct
304  * that can be used pretty naturally. E.g., for some hypothetical cgroup
305  * iterator, you'd write:
306  *
307  * struct cgroup *cg, *parent_cg = <...>;
308  *
309  * bpf_for_each(cgroup, cg, parent_cg, CG_ITER_CHILDREN) {
310  *     bpf_printk("Child cgroup id = %d", cg->cgroup_id);
311  *     if (cg->cgroup_id == 123)
312  *         break;
313  * }
314  *
315  * I.e., it looks almost like high-level for each loop in other languages,
316  * supports continue/break, and is verifiable by BPF verifier.
317  *
318  * For iterating integers, the difference betwen bpf_for_each(num, i, N, M)
319  * and bpf_for(i, N, M) is in that bpf_for() provides additional proof to
320  * verifier that i is in [N, M) range, and in bpf_for_each() case i is `int
321  * *`, not just `int`. So for integers bpf_for() is more convenient.
322  *
323  * Note: this macro relies on C99 feature of allowing to declare variables
324  * inside for() loop, bound to for() loop lifetime. It also utilizes GCC
325  * extension: __attribute__((cleanup(<func>))), supported by both GCC and
326  * Clang.
327  */
328 #define bpf_for_each(type, cur, args...) for (							\
329 	/* initialize and define destructor */							\
330 	struct bpf_iter_##type ___it __attribute__((aligned(8), /* enforce, just in case */,	\
331 						    cleanup(bpf_iter_##type##_destroy))),	\
332 	/* ___p pointer is just to call bpf_iter_##type##_new() *once* to init ___it */		\
333 			       *___p __attribute__((unused)) = (				\
334 					bpf_iter_##type##_new(&___it, ##args),			\
335 	/* this is a workaround for Clang bug: it currently doesn't emit BTF */			\
336 	/* for bpf_iter_##type##_destroy() when used from cleanup() attribute */		\
337 					(void)bpf_iter_##type##_destroy, (void *)0);		\
338 	/* iteration and termination check */							\
339 	(((cur) = bpf_iter_##type##_next(&___it)));						\
340 )
341 #endif /* bpf_for_each */
342 
343 #ifndef bpf_for
344 /* bpf_for(i, start, end) implements a for()-like looping construct that sets
345  * provided integer variable *i* to values starting from *start* through,
346  * but not including, *end*. It also proves to BPF verifier that *i* belongs
347  * to range [start, end), so this can be used for accessing arrays without
348  * extra checks.
349  *
350  * Note: *start* and *end* are assumed to be expressions with no side effects
351  * and whose values do not change throughout bpf_for() loop execution. They do
352  * not have to be statically known or constant, though.
353  *
354  * Note: similarly to bpf_for_each(), it relies on C99 feature of declaring for()
355  * loop bound variables and cleanup attribute, supported by GCC and Clang.
356  */
357 #define bpf_for(i, start, end) for (								\
358 	/* initialize and define destructor */							\
359 	struct bpf_iter_num ___it __attribute__((aligned(8), /* enforce, just in case */	\
360 						 cleanup(bpf_iter_num_destroy))),		\
361 	/* ___p pointer is necessary to call bpf_iter_num_new() *once* to init ___it */		\
362 			    *___p __attribute__((unused)) = (					\
363 				bpf_iter_num_new(&___it, (start), (end)),			\
364 	/* this is a workaround for Clang bug: it currently doesn't emit BTF */			\
365 	/* for bpf_iter_num_destroy() when used from cleanup() attribute */			\
366 				(void)bpf_iter_num_destroy, (void *)0);				\
367 	({											\
368 		/* iteration step */								\
369 		int *___t = bpf_iter_num_next(&___it);						\
370 		/* termination and bounds check */						\
371 		(___t && ((i) = *___t, (i) >= (start) && (i) < (end)));				\
372 	});											\
373 )
374 #endif /* bpf_for */
375 
376 #ifndef bpf_repeat
377 /* bpf_repeat(N) performs N iterations without exposing iteration number
378  *
379  * Note: similarly to bpf_for_each(), it relies on C99 feature of declaring for()
380  * loop bound variables and cleanup attribute, supported by GCC and Clang.
381  */
382 #define bpf_repeat(N) for (									\
383 	/* initialize and define destructor */							\
384 	struct bpf_iter_num ___it __attribute__((aligned(8), /* enforce, just in case */	\
385 						 cleanup(bpf_iter_num_destroy))),		\
386 	/* ___p pointer is necessary to call bpf_iter_num_new() *once* to init ___it */		\
387 			    *___p __attribute__((unused)) = (					\
388 				bpf_iter_num_new(&___it, 0, (N)),				\
389 	/* this is a workaround for Clang bug: it currently doesn't emit BTF */			\
390 	/* for bpf_iter_num_destroy() when used from cleanup() attribute */			\
391 				(void)bpf_iter_num_destroy, (void *)0);				\
392 	bpf_iter_num_next(&___it);								\
393 	/* nothing here  */									\
394 )
395 #endif /* bpf_repeat */
396 
397 #endif
398