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