1e01a75c1SAndrii Nakryiko /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2e01a75c1SAndrii Nakryiko #ifndef __BPF_HELPERS__
3e01a75c1SAndrii Nakryiko #define __BPF_HELPERS__
4e01a75c1SAndrii Nakryiko
5ae460c02SYoshiki Komachi /*
6ae460c02SYoshiki Komachi * Note that bpf programs need to include either
7ae460c02SYoshiki Komachi * vmlinux.h (auto-generated from BTF) or linux/types.h
8ae460c02SYoshiki Komachi * in advance since bpf_helper_defs.h uses such types
9ae460c02SYoshiki Komachi * as __u64.
10ae460c02SYoshiki Komachi */
11e01a75c1SAndrii Nakryiko #include "bpf_helper_defs.h"
12e01a75c1SAndrii Nakryiko
13e01a75c1SAndrii Nakryiko #define __uint(name, val) int (*name)[val]
14e01a75c1SAndrii Nakryiko #define __type(name, val) typeof(val) *name
15646f02ffSAndrii Nakryiko #define __array(name, val) typeof(val) *name[]
16e01a75c1SAndrii Nakryiko
177db3822aSAndrii Nakryiko /*
187db3822aSAndrii Nakryiko * Helper macro to place programs, maps, license in
19e01a75c1SAndrii Nakryiko * different sections in elf_bpf file. Section names
200fec7a3cSAndrii Nakryiko * are interpreted by libbpf depending on the context (BPF programs, BPF maps,
210fec7a3cSAndrii Nakryiko * extern variables, etc).
220fec7a3cSAndrii Nakryiko * To allow use of SEC() with externs (e.g., for extern .maps declarations),
230fec7a3cSAndrii Nakryiko * make sure __attribute__((unused)) doesn't trigger compilation warning.
24e01a75c1SAndrii Nakryiko */
2518410251SJames Hilliard #if __GNUC__ && !__clang__
2618410251SJames Hilliard
2718410251SJames Hilliard /*
2818410251SJames Hilliard * Pragma macros are broken on GCC
2918410251SJames Hilliard * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55578
3018410251SJames Hilliard * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90400
3118410251SJames Hilliard */
3218410251SJames Hilliard #define SEC(name) __attribute__((section(name), used))
3318410251SJames Hilliard
3418410251SJames Hilliard #else
3518410251SJames Hilliard
360fec7a3cSAndrii Nakryiko #define SEC(name) \
370fec7a3cSAndrii Nakryiko _Pragma("GCC diagnostic push") \
380fec7a3cSAndrii Nakryiko _Pragma("GCC diagnostic ignored \"-Wignored-attributes\"") \
390fec7a3cSAndrii Nakryiko __attribute__((section(name), used)) \
400fec7a3cSAndrii Nakryiko _Pragma("GCC diagnostic pop") \
41e01a75c1SAndrii Nakryiko
4218410251SJames Hilliard #endif
4318410251SJames Hilliard
440205e9deSPedro Tammela /* Avoid 'linux/stddef.h' definition of '__always_inline'. */
450205e9deSPedro Tammela #undef __always_inline
46ce5a518eSIan Rogers #define __always_inline inline __attribute__((always_inline))
470205e9deSPedro Tammela
48819c23afSAndrii Nakryiko #ifndef __noinline
49819c23afSAndrii Nakryiko #define __noinline __attribute__((noinline))
50819c23afSAndrii Nakryiko #endif
51166750bcSAndrii Nakryiko #ifndef __weak
52166750bcSAndrii Nakryiko #define __weak __attribute__((weak))
53166750bcSAndrii Nakryiko #endif
547db3822aSAndrii Nakryiko
55aea28a60SAndrii Nakryiko /*
56aea28a60SAndrii Nakryiko * Use __hidden attribute to mark a non-static BPF subprogram effectively
57aea28a60SAndrii Nakryiko * static for BPF verifier's verification algorithm purposes, allowing more
58aea28a60SAndrii Nakryiko * extensive and permissive BPF verification process, taking into account
59aea28a60SAndrii Nakryiko * subprogram's caller context.
60aea28a60SAndrii Nakryiko */
61aea28a60SAndrii Nakryiko #define __hidden __attribute__((visibility("hidden")))
62aea28a60SAndrii Nakryiko
639ae2c26eSAndrii Nakryiko /* When utilizing vmlinux.h with BPF CO-RE, user BPF programs can't include
649ae2c26eSAndrii Nakryiko * any system-level headers (such as stddef.h, linux/version.h, etc), and
659ae2c26eSAndrii Nakryiko * commonly-used macros like NULL and KERNEL_VERSION aren't available through
669ae2c26eSAndrii Nakryiko * vmlinux.h. This just adds unnecessary hurdles and forces users to re-define
679ae2c26eSAndrii Nakryiko * them on their own. So as a convenience, provide such definitions here.
689ae2c26eSAndrii Nakryiko */
699ae2c26eSAndrii Nakryiko #ifndef NULL
709ae2c26eSAndrii Nakryiko #define NULL ((void *)0)
719ae2c26eSAndrii Nakryiko #endif
729ae2c26eSAndrii Nakryiko
739ae2c26eSAndrii Nakryiko #ifndef KERNEL_VERSION
741e1032b0SHengqi Chen #define KERNEL_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + ((c) > 255 ? 255 : (c)))
759ae2c26eSAndrii Nakryiko #endif
769ae2c26eSAndrii Nakryiko
777db3822aSAndrii Nakryiko /*
789ae2c26eSAndrii Nakryiko * Helper macros to manipulate data structures
795fbc2208SYonghong Song */
805fbc2208SYonghong Song
8163fe3fd3SYonghong Song /* offsetof() definition that uses __builtin_offset() might not preserve field
825fbc2208SYonghong Song * offset CO-RE relocation properly, so force-redefine offsetof() using
835fbc2208SYonghong Song * old-school approach which works with CO-RE correctly
845fbc2208SYonghong Song */
855fbc2208SYonghong Song #undef offsetof
865fbc2208SYonghong Song #define offsetof(type, member) ((unsigned long)&((type *)0)->member)
875fbc2208SYonghong Song
885fbc2208SYonghong Song /* redefined container_of() to ensure we use the above offsetof() macro */
895fbc2208SYonghong Song #undef container_of
905fbc2208SYonghong Song #define container_of(ptr, type, member) \
915fbc2208SYonghong Song ({ \
92f760d053SAndrii Nakryiko void *__mptr = (void *)(ptr); \
93f760d053SAndrii Nakryiko ((type *)(__mptr - offsetof(type, member))); \
94f760d053SAndrii Nakryiko })
95f760d053SAndrii Nakryiko
96f760d053SAndrii Nakryiko /*
97f760d053SAndrii Nakryiko * Compiler (optimization) barrier.
98f760d053SAndrii Nakryiko */
99f760d053SAndrii Nakryiko #ifndef barrier
100f760d053SAndrii Nakryiko #define barrier() asm volatile("" ::: "memory")
101f760d053SAndrii Nakryiko #endif
102f760d053SAndrii Nakryiko
103f760d053SAndrii Nakryiko /* Variable-specific compiler (optimization) barrier. It's a no-op which makes
104f760d053SAndrii Nakryiko * compiler believe that there is some black box modification of a given
105f760d053SAndrii Nakryiko * variable and thus prevents compiler from making extra assumption about its
106f760d053SAndrii Nakryiko * value and potential simplifications and optimizations on this variable.
107f760d053SAndrii Nakryiko *
108f760d053SAndrii Nakryiko * E.g., compiler might often delay or even omit 32-bit to 64-bit casting of
109f760d053SAndrii Nakryiko * a variable, making some code patterns unverifiable. Putting barrier_var()
110f760d053SAndrii Nakryiko * in place will ensure that cast is performed before the barrier_var()
111f760d053SAndrii Nakryiko * invocation, because compiler has to pessimistically assume that embedded
112e85465e4SIlya Leoshkevich * asm section might perform some extra operations on that variable.
113f760d053SAndrii Nakryiko *
114f760d053SAndrii Nakryiko * This is a variable-specific variant of more global barrier().
115f760d053SAndrii Nakryiko */
1160e9f6841SDaniel Borkmann #ifndef barrier_var
1170e9f6841SDaniel Borkmann #define barrier_var(var) asm volatile("" : "+r"(var))
1180e9f6841SDaniel Borkmann #endif
1190e9f6841SDaniel Borkmann
1200e9f6841SDaniel Borkmann /*
1210e9f6841SDaniel Borkmann * Helper macro to throw a compilation error if __bpf_unreachable() gets
1220e9f6841SDaniel Borkmann * built into the resulting code. This works given BPF back end does not
1230e9f6841SDaniel Borkmann * implement __builtin_trap(). This is useful to assert that certain paths
1240e9f6841SDaniel Borkmann * of the program code are never used and hence eliminated by the compiler.
1250e9f6841SDaniel Borkmann *
1260e9f6841SDaniel Borkmann * For example, consider a switch statement that covers known cases used by
1270e9f6841SDaniel Borkmann * the program. __bpf_unreachable() can then reside in the default case. If
1280e9f6841SDaniel Borkmann * the program gets extended such that a case is not covered in the switch
1290e9f6841SDaniel Borkmann * statement, then it will throw a build error due to the default case not
1300e9f6841SDaniel Borkmann * being compiled out.
1310e9f6841SDaniel Borkmann */
1320e9f6841SDaniel Borkmann #ifndef __bpf_unreachable
1330e9f6841SDaniel Borkmann # define __bpf_unreachable() __builtin_trap()
134665f5d35SDaniel Borkmann #endif
1350e9f6841SDaniel Borkmann
1360e9f6841SDaniel Borkmann /*
1370e9f6841SDaniel Borkmann * Helper function to perform a tail call with a constant/immediate map slot.
1380e9f6841SDaniel Borkmann */
1390e9f6841SDaniel Borkmann #if __clang_major__ >= 8 && defined(__bpf__)
1400e9f6841SDaniel Borkmann static __always_inline void
bpf_tail_call_static(void * ctx,const void * map,const __u32 slot)1410e9f6841SDaniel Borkmann bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
142665f5d35SDaniel Borkmann {
143665f5d35SDaniel Borkmann if (!__builtin_constant_p(slot))
1440e9f6841SDaniel Borkmann __bpf_unreachable();
1450e9f6841SDaniel Borkmann
1460e9f6841SDaniel Borkmann /*
1470e9f6841SDaniel Borkmann * Provide a hard guarantee that LLVM won't optimize setting r2 (map
1480e9f6841SDaniel Borkmann * pointer) and r3 (constant map index) from _different paths_ ending
1490e9f6841SDaniel Borkmann * up at the _same_ call insn as otherwise we won't be able to use the
1500e9f6841SDaniel Borkmann * jmpq/nopl retpoline-free patching by the x86-64 JIT in the kernel
151665f5d35SDaniel Borkmann * given they mismatch. See also d2e4c1e6c294 ("bpf: Constant map key
152665f5d35SDaniel Borkmann * tracking for prog array pokes") for details on verifier tracking.
1530e9f6841SDaniel Borkmann *
154665f5d35SDaniel Borkmann * Note on clobber list: we need to stay in-line with BPF calling
1550e9f6841SDaniel Borkmann * convention, so even if we don't end up using r0, r4, r5, we need
1560e9f6841SDaniel Borkmann * to mark them as clobber so that LLVM doesn't end up using them
1570e9f6841SDaniel Borkmann * before / after the call.
1580e9f6841SDaniel Borkmann */
1590e9f6841SDaniel Borkmann asm volatile("r1 = %[ctx]\n\t"
1600e9f6841SDaniel Borkmann "r2 = %[map]\n\t"
1613652c9a1SDaniel Borkmann "r3 = %[slot]\n\t"
1620e9f6841SDaniel Borkmann "call 12"
16357a00f41SToke Høiland-Jørgensen :: [ctx]"r"(ctx), [map]"r"(map), [slot]"i"(slot)
16457a00f41SToke Høiland-Jørgensen : "r0", "r1", "r2", "r3", "r4", "r5");
16557a00f41SToke Høiland-Jørgensen }
16657a00f41SToke Høiland-Jørgensen #endif
16757a00f41SToke Høiland-Jørgensen
16857a00f41SToke Høiland-Jørgensen enum libbpf_pin_type {
169166750bcSAndrii Nakryiko LIBBPF_PIN_NONE,
170166750bcSAndrii Nakryiko /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
171166750bcSAndrii Nakryiko LIBBPF_PIN_BY_NAME,
172166750bcSAndrii Nakryiko };
173166750bcSAndrii Nakryiko
174166750bcSAndrii Nakryiko enum libbpf_tristate {
17581bfdd08SAndrii Nakryiko TRI_NO = 0,
1761c0c7074SAndrii Nakryiko TRI_YES = 1,
17703b77e17SAlexei Starovoitov TRI_MODULE = 2,
178ef89654fSKumar Kartikeya Dwivedi };
17981bfdd08SAndrii Nakryiko
1805cbd3fe3SAlexei Starovoitov #define __kconfig __attribute__((section(".kconfig")))
1815cbd3fe3SAlexei Starovoitov #define __ksym __attribute__((section(".ksyms")))
1825cbd3fe3SAlexei Starovoitov #define __kptr_untrusted __attribute__((btf_type_tag("kptr_untrusted")))
1835cbd3fe3SAlexei Starovoitov #define __kptr __attribute__((btf_type_tag("kptr")))
1845cbd3fe3SAlexei Starovoitov
185d6a6a555SFlorent Revest #define bpf_ksym_exists(sym) ({ \
186d6a6a555SFlorent Revest _Static_assert(!__builtin_constant_p(!!sym), #sym " should be marked as __weak"); \
187d6a6a555SFlorent Revest !!sym; \
188d6a6a555SFlorent Revest })
189d6a6a555SFlorent Revest
190d6a6a555SFlorent Revest #ifndef ___bpf_concat
191d6a6a555SFlorent Revest #define ___bpf_concat(a, b) a ## b
192d6a6a555SFlorent Revest #endif
193d6a6a555SFlorent Revest #ifndef ___bpf_apply
194d6a6a555SFlorent Revest #define ___bpf_apply(fn, n) ___bpf_concat(fn, n)
195d6a6a555SFlorent Revest #endif
196d6a6a555SFlorent Revest #ifndef ___bpf_nth
197d6a6a555SFlorent Revest #define ___bpf_nth(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c, N, ...) N
198d6a6a555SFlorent Revest #endif
199d6a6a555SFlorent Revest #ifndef ___bpf_narg
200d6a6a555SFlorent Revest #define ___bpf_narg(...) \
201d6a6a555SFlorent Revest ___bpf_nth(_, ##__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
202d6a6a555SFlorent Revest #endif
203d6a6a555SFlorent Revest
204d6a6a555SFlorent Revest #define ___bpf_fill0(arr, p, x) do {} while (0)
205d6a6a555SFlorent Revest #define ___bpf_fill1(arr, p, x) arr[p] = x
206d6a6a555SFlorent Revest #define ___bpf_fill2(arr, p, x, args...) arr[p] = x; ___bpf_fill1(arr, p + 1, args)
207d6a6a555SFlorent Revest #define ___bpf_fill3(arr, p, x, args...) arr[p] = x; ___bpf_fill2(arr, p + 1, args)
208d6a6a555SFlorent Revest #define ___bpf_fill4(arr, p, x, args...) arr[p] = x; ___bpf_fill3(arr, p + 1, args)
209d6a6a555SFlorent Revest #define ___bpf_fill5(arr, p, x, args...) arr[p] = x; ___bpf_fill4(arr, p + 1, args)
210d6a6a555SFlorent Revest #define ___bpf_fill6(arr, p, x, args...) arr[p] = x; ___bpf_fill5(arr, p + 1, args)
211d6a6a555SFlorent Revest #define ___bpf_fill7(arr, p, x, args...) arr[p] = x; ___bpf_fill6(arr, p + 1, args)
212d6a6a555SFlorent Revest #define ___bpf_fill8(arr, p, x, args...) arr[p] = x; ___bpf_fill7(arr, p + 1, args)
213d6a6a555SFlorent Revest #define ___bpf_fill9(arr, p, x, args...) arr[p] = x; ___bpf_fill8(arr, p + 1, args)
214d6a6a555SFlorent Revest #define ___bpf_fill10(arr, p, x, args...) arr[p] = x; ___bpf_fill9(arr, p + 1, args)
215d6a6a555SFlorent Revest #define ___bpf_fill11(arr, p, x, args...) arr[p] = x; ___bpf_fill10(arr, p + 1, args)
216d6a6a555SFlorent Revest #define ___bpf_fill12(arr, p, x, args...) arr[p] = x; ___bpf_fill11(arr, p + 1, args)
217d6a6a555SFlorent Revest #define ___bpf_fill(arr, args...) \
218d6a6a555SFlorent Revest ___bpf_apply(___bpf_fill, ___bpf_narg(args))(arr, 0, args)
219d6a6a555SFlorent Revest
220d6a6a555SFlorent Revest /*
221d6a6a555SFlorent Revest * BPF_SEQ_PRINTF to wrap bpf_seq_printf to-be-printed values
222d6a6a555SFlorent Revest * in a structure.
223d6a6a555SFlorent Revest */
224d6a6a555SFlorent Revest #define BPF_SEQ_PRINTF(seq, fmt, args...) \
225d6a6a555SFlorent Revest ({ \
226d6a6a555SFlorent Revest static const char ___fmt[] = fmt; \
227d6a6a555SFlorent Revest unsigned long long ___param[___bpf_narg(args)]; \
228d6a6a555SFlorent Revest \
229d6a6a555SFlorent Revest _Pragma("GCC diagnostic push") \
230d6a6a555SFlorent Revest _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
231d6a6a555SFlorent Revest ___bpf_fill(___param, args); \
232d6a6a555SFlorent Revest _Pragma("GCC diagnostic pop") \
233d6a6a555SFlorent Revest \
234d6a6a555SFlorent Revest bpf_seq_printf(seq, ___fmt, sizeof(___fmt), \
235d6a6a555SFlorent Revest ___param, sizeof(___param)); \
236d6a6a555SFlorent Revest })
237d6a6a555SFlorent Revest
238d6a6a555SFlorent Revest /*
239d6a6a555SFlorent Revest * BPF_SNPRINTF wraps the bpf_snprintf helper with variadic arguments instead of
240d6a6a555SFlorent Revest * an array of u64.
241d6a6a555SFlorent Revest */
242d6a6a555SFlorent Revest #define BPF_SNPRINTF(out, out_size, fmt, args...) \
243d6a6a555SFlorent Revest ({ \
244d6a6a555SFlorent Revest static const char ___fmt[] = fmt; \
245d6a6a555SFlorent Revest unsigned long long ___param[___bpf_narg(args)]; \
246d6a6a555SFlorent Revest \
247d6a6a555SFlorent Revest _Pragma("GCC diagnostic push") \
248d6a6a555SFlorent Revest _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
249d6a6a555SFlorent Revest ___bpf_fill(___param, args); \
250d6a6a555SFlorent Revest _Pragma("GCC diagnostic pop") \
2516c66b0e7SDave Marchevsky \
2526c66b0e7SDave Marchevsky bpf_snprintf(out, out_size, ___fmt, \
2536c66b0e7SDave Marchevsky ___param, sizeof(___param)); \
2546c66b0e7SDave Marchevsky })
2556c66b0e7SDave Marchevsky
2566c66b0e7SDave Marchevsky #ifdef BPF_NO_GLOBAL_DATA
257c2758baaSDave Marchevsky #define BPF_PRINTK_FMT_MOD
258c2758baaSDave Marchevsky #else
2596c66b0e7SDave Marchevsky #define BPF_PRINTK_FMT_MOD static const
260c2758baaSDave Marchevsky #endif
261c2758baaSDave Marchevsky
262c2758baaSDave Marchevsky #define __bpf_printk(fmt, ...) \
263c2758baaSDave Marchevsky ({ \
264c2758baaSDave Marchevsky BPF_PRINTK_FMT_MOD char ____fmt[] = fmt; \
265c2758baaSDave Marchevsky bpf_trace_printk(____fmt, sizeof(____fmt), \
266c2758baaSDave Marchevsky ##__VA_ARGS__); \
267c2758baaSDave Marchevsky })
268c2758baaSDave Marchevsky
269c2758baaSDave Marchevsky /*
270c2758baaSDave Marchevsky * __bpf_vprintk wraps the bpf_trace_vprintk helper with variadic arguments
271c2758baaSDave Marchevsky * instead of an array of u64.
272c2758baaSDave Marchevsky */
273c2758baaSDave Marchevsky #define __bpf_vprintk(fmt, args...) \
274c2758baaSDave Marchevsky ({ \
275c2758baaSDave Marchevsky static const char ___fmt[] = fmt; \
276c2758baaSDave Marchevsky unsigned long long ___param[___bpf_narg(args)]; \
277c2758baaSDave Marchevsky \
278c2758baaSDave Marchevsky _Pragma("GCC diagnostic push") \
279c2758baaSDave Marchevsky _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
280c2758baaSDave Marchevsky ___bpf_fill(___param, args); \
281c2758baaSDave Marchevsky _Pragma("GCC diagnostic pop") \
282c2758baaSDave Marchevsky \
283c2758baaSDave Marchevsky bpf_trace_vprintk(___fmt, sizeof(___fmt), \
284c2758baaSDave Marchevsky ___param, sizeof(___param)); \
285c2758baaSDave Marchevsky })
286c2758baaSDave Marchevsky
287c2758baaSDave Marchevsky /* Use __bpf_printk when bpf_printk call has 3 or fewer fmt args
288c2758baaSDave Marchevsky * Otherwise use __bpf_vprintk
289c2758baaSDave Marchevsky */
290c2758baaSDave Marchevsky #define ___bpf_pick_printk(...) \
291c2758baaSDave Marchevsky ___bpf_nth(_, ##__VA_ARGS__, __bpf_vprintk, __bpf_vprintk, __bpf_vprintk, \
292c2758baaSDave Marchevsky __bpf_vprintk, __bpf_vprintk, __bpf_vprintk, __bpf_vprintk, \
293c2758baaSDave Marchevsky __bpf_vprintk, __bpf_vprintk, __bpf_printk /*3*/, __bpf_printk /*2*/,\
294c5e64741SAndrii Nakryiko __bpf_printk /*1*/, __bpf_printk /*0*/)
295c5e64741SAndrii Nakryiko
296*94dccba7SAndrii Nakryiko /* Helper macro to print out debug messages */
297*94dccba7SAndrii Nakryiko #define bpf_printk(fmt, args...) ___bpf_pick_printk(args)(fmt, ##args)
298*94dccba7SAndrii Nakryiko
299c5e64741SAndrii Nakryiko struct bpf_iter_num;
300c5e64741SAndrii Nakryiko
301c5e64741SAndrii Nakryiko extern int bpf_iter_num_new(struct bpf_iter_num *it, int start, int end) __weak __ksym;
302c5e64741SAndrii Nakryiko extern int *bpf_iter_num_next(struct bpf_iter_num *it) __weak __ksym;
303c5e64741SAndrii Nakryiko extern void bpf_iter_num_destroy(struct bpf_iter_num *it) __weak __ksym;
304c5e64741SAndrii Nakryiko
305c5e64741SAndrii Nakryiko #ifndef bpf_for_each
306c5e64741SAndrii Nakryiko /* bpf_for_each(iter_type, cur_elem, args...) provides generic construct for
307c5e64741SAndrii Nakryiko * using BPF open-coded iterators without having to write mundane explicit
308c5e64741SAndrii Nakryiko * low-level loop logic. Instead, it provides for()-like generic construct
309c5e64741SAndrii Nakryiko * that can be used pretty naturally. E.g., for some hypothetical cgroup
310c5e64741SAndrii Nakryiko * iterator, you'd write:
311c5e64741SAndrii Nakryiko *
312c5e64741SAndrii Nakryiko * struct cgroup *cg, *parent_cg = <...>;
313c5e64741SAndrii Nakryiko *
314c5e64741SAndrii Nakryiko * bpf_for_each(cgroup, cg, parent_cg, CG_ITER_CHILDREN) {
315c5e64741SAndrii Nakryiko * bpf_printk("Child cgroup id = %d", cg->cgroup_id);
316c5e64741SAndrii Nakryiko * if (cg->cgroup_id == 123)
317c5e64741SAndrii Nakryiko * break;
318c5e64741SAndrii Nakryiko * }
319c5e64741SAndrii Nakryiko *
320c5e64741SAndrii Nakryiko * I.e., it looks almost like high-level for each loop in other languages,
321c5e64741SAndrii Nakryiko * supports continue/break, and is verifiable by BPF verifier.
322c5e64741SAndrii Nakryiko *
323c5e64741SAndrii Nakryiko * For iterating integers, the difference betwen bpf_for_each(num, i, N, M)
324c5e64741SAndrii Nakryiko * and bpf_for(i, N, M) is in that bpf_for() provides additional proof to
325c5e64741SAndrii Nakryiko * verifier that i is in [N, M) range, and in bpf_for_each() case i is `int
326c5e64741SAndrii Nakryiko * *`, not just `int`. So for integers bpf_for() is more convenient.
327c5e64741SAndrii Nakryiko *
328c5e64741SAndrii Nakryiko * Note: this macro relies on C99 feature of allowing to declare variables
329c5e64741SAndrii Nakryiko * inside for() loop, bound to for() loop lifetime. It also utilizes GCC
330c5e64741SAndrii Nakryiko * extension: __attribute__((cleanup(<func>))), supported by both GCC and
331c5e64741SAndrii Nakryiko * Clang.
332c5e64741SAndrii Nakryiko */
333c5e64741SAndrii Nakryiko #define bpf_for_each(type, cur, args...) for ( \
334c5e64741SAndrii Nakryiko /* initialize and define destructor */ \
335c5e64741SAndrii Nakryiko struct bpf_iter_##type ___it __attribute__((aligned(8), /* enforce, just in case */, \
336c5e64741SAndrii Nakryiko cleanup(bpf_iter_##type##_destroy))), \
337c5e64741SAndrii Nakryiko /* ___p pointer is just to call bpf_iter_##type##_new() *once* to init ___it */ \
338c5e64741SAndrii Nakryiko *___p __attribute__((unused)) = ( \
339c5e64741SAndrii Nakryiko bpf_iter_##type##_new(&___it, ##args), \
340c5e64741SAndrii Nakryiko /* this is a workaround for Clang bug: it currently doesn't emit BTF */ \
341c5e64741SAndrii Nakryiko /* for bpf_iter_##type##_destroy() when used from cleanup() attribute */ \
342c5e64741SAndrii Nakryiko (void)bpf_iter_##type##_destroy, (void *)0); \
343c5e64741SAndrii Nakryiko /* iteration and termination check */ \
344c5e64741SAndrii Nakryiko (((cur) = bpf_iter_##type##_next(&___it))); \
345c5e64741SAndrii Nakryiko )
346c5e64741SAndrii Nakryiko #endif /* bpf_for_each */
347c5e64741SAndrii Nakryiko
348c5e64741SAndrii Nakryiko #ifndef bpf_for
349c5e64741SAndrii Nakryiko /* bpf_for(i, start, end) implements a for()-like looping construct that sets
350c5e64741SAndrii Nakryiko * provided integer variable *i* to values starting from *start* through,
351c5e64741SAndrii Nakryiko * but not including, *end*. It also proves to BPF verifier that *i* belongs
352c5e64741SAndrii Nakryiko * to range [start, end), so this can be used for accessing arrays without
353c5e64741SAndrii Nakryiko * extra checks.
354c5e64741SAndrii Nakryiko *
355c5e64741SAndrii Nakryiko * Note: *start* and *end* are assumed to be expressions with no side effects
356c5e64741SAndrii Nakryiko * and whose values do not change throughout bpf_for() loop execution. They do
357c5e64741SAndrii Nakryiko * not have to be statically known or constant, though.
358c5e64741SAndrii Nakryiko *
359c5e64741SAndrii Nakryiko * Note: similarly to bpf_for_each(), it relies on C99 feature of declaring for()
360c5e64741SAndrii Nakryiko * loop bound variables and cleanup attribute, supported by GCC and Clang.
361c5e64741SAndrii Nakryiko */
362c5e64741SAndrii Nakryiko #define bpf_for(i, start, end) for ( \
363c5e64741SAndrii Nakryiko /* initialize and define destructor */ \
364c5e64741SAndrii Nakryiko struct bpf_iter_num ___it __attribute__((aligned(8), /* enforce, just in case */ \
365c5e64741SAndrii Nakryiko cleanup(bpf_iter_num_destroy))), \
366c5e64741SAndrii Nakryiko /* ___p pointer is necessary to call bpf_iter_num_new() *once* to init ___it */ \
367c5e64741SAndrii Nakryiko *___p __attribute__((unused)) = ( \
368c5e64741SAndrii Nakryiko bpf_iter_num_new(&___it, (start), (end)), \
369c5e64741SAndrii Nakryiko /* this is a workaround for Clang bug: it currently doesn't emit BTF */ \
370c5e64741SAndrii Nakryiko /* for bpf_iter_num_destroy() when used from cleanup() attribute */ \
371c5e64741SAndrii Nakryiko (void)bpf_iter_num_destroy, (void *)0); \
372c5e64741SAndrii Nakryiko ({ \
373c5e64741SAndrii Nakryiko /* iteration step */ \
374c5e64741SAndrii Nakryiko int *___t = bpf_iter_num_next(&___it); \
375c5e64741SAndrii Nakryiko /* termination and bounds check */ \
376c5e64741SAndrii Nakryiko (___t && ((i) = *___t, (i) >= (start) && (i) < (end))); \
377c5e64741SAndrii Nakryiko }); \
378c5e64741SAndrii Nakryiko )
379c5e64741SAndrii Nakryiko #endif /* bpf_for */
380c5e64741SAndrii Nakryiko
381c5e64741SAndrii Nakryiko #ifndef bpf_repeat
382c5e64741SAndrii Nakryiko /* bpf_repeat(N) performs N iterations without exposing iteration number
383c5e64741SAndrii Nakryiko *
384c5e64741SAndrii Nakryiko * Note: similarly to bpf_for_each(), it relies on C99 feature of declaring for()
385c5e64741SAndrii Nakryiko * loop bound variables and cleanup attribute, supported by GCC and Clang.
386c5e64741SAndrii Nakryiko */
387c5e64741SAndrii Nakryiko #define bpf_repeat(N) for ( \
388c5e64741SAndrii Nakryiko /* initialize and define destructor */ \
389c5e64741SAndrii Nakryiko struct bpf_iter_num ___it __attribute__((aligned(8), /* enforce, just in case */ \
390c5e64741SAndrii Nakryiko cleanup(bpf_iter_num_destroy))), \
391c5e64741SAndrii Nakryiko /* ___p pointer is necessary to call bpf_iter_num_new() *once* to init ___it */ \
392c5e64741SAndrii Nakryiko *___p __attribute__((unused)) = ( \
393c5e64741SAndrii Nakryiko bpf_iter_num_new(&___it, 0, (N)), \
394c5e64741SAndrii Nakryiko /* this is a workaround for Clang bug: it currently doesn't emit BTF */ \
395c5e64741SAndrii Nakryiko /* for bpf_iter_num_destroy() when used from cleanup() attribute */ \
396c5e64741SAndrii Nakryiko (void)bpf_iter_num_destroy, (void *)0); \
397e01a75c1SAndrii Nakryiko bpf_iter_num_next(&___it); \
398 /* nothing here */ \
399 )
400 #endif /* bpf_repeat */
401
402 #endif
403