/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */1#ifndef __BPF_HELPERS__2#define __BPF_HELPERS__34/*5* Note that bpf programs need to include either6* vmlinux.h (auto-generated from BTF) or linux/types.h7* in advance since bpf_helper_defs.h uses such types8* as __u64.9*/10#include "bpf_helper_defs.h"1112#define __uint(name, val) int (*name)[val]13#define __type(name, val) typeof(val) *name14#define __array(name, val) typeof(val) *name[]15#define __ulong(name, val) enum { ___bpf_concat(__unique_value, __COUNTER__) = val } name1617#ifndef likely18#define likely(x) (__builtin_expect(!!(x), 1))19#endif2021#ifndef unlikely22#define unlikely(x) (__builtin_expect(!!(x), 0))23#endif2425/*26* Helper macro to place programs, maps, license in27* different sections in elf_bpf file. Section names28* are interpreted by libbpf depending on the context (BPF programs, BPF maps,29* extern variables, etc).30* To allow use of SEC() with externs (e.g., for extern .maps declarations),31* make sure __attribute__((unused)) doesn't trigger compilation warning.32*/33#if __GNUC__ && !__clang__3435/*36* Pragma macros are broken on GCC37* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=5557838* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=9040039*/40#define SEC(name) __attribute__((section(name), used))4142#else4344#define SEC(name) \45_Pragma("GCC diagnostic push") \46_Pragma("GCC diagnostic ignored \"-Wignored-attributes\"") \47__attribute__((section(name), used)) \48_Pragma("GCC diagnostic pop") \4950#endif5152/* Avoid 'linux/stddef.h' definition of '__always_inline'. */53#undef __always_inline54#define __always_inline inline __attribute__((always_inline))5556#ifndef __noinline57#define __noinline __attribute__((noinline))58#endif59#ifndef __weak60#define __weak __attribute__((weak))61#endif6263/*64* Use __hidden attribute to mark a non-static BPF subprogram effectively65* static for BPF verifier's verification algorithm purposes, allowing more66* extensive and permissive BPF verification process, taking into account67* subprogram's caller context.68*/69#define __hidden __attribute__((visibility("hidden")))7071/* When utilizing vmlinux.h with BPF CO-RE, user BPF programs can't include72* any system-level headers (such as stddef.h, linux/version.h, etc), and73* commonly-used macros like NULL and KERNEL_VERSION aren't available through74* vmlinux.h. This just adds unnecessary hurdles and forces users to re-define75* them on their own. So as a convenience, provide such definitions here.76*/77#ifndef NULL78#define NULL ((void *)0)79#endif8081#ifndef KERNEL_VERSION82#define KERNEL_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + ((c) > 255 ? 255 : (c)))83#endif8485/*86* Helper macros to manipulate data structures87*/8889/* offsetof() definition that uses __builtin_offset() might not preserve field90* offset CO-RE relocation properly, so force-redefine offsetof() using91* old-school approach which works with CO-RE correctly92*/93#undef offsetof94#define offsetof(type, member) ((unsigned long)&((type *)0)->member)9596/* redefined container_of() to ensure we use the above offsetof() macro */97#undef container_of98#define container_of(ptr, type, member) \99({ \100void *__mptr = (void *)(ptr); \101((type *)(__mptr - offsetof(type, member))); \102})103104/*105* Compiler (optimization) barrier.106*/107#ifndef barrier108#define barrier() asm volatile("" ::: "memory")109#endif110111/* Variable-specific compiler (optimization) barrier. It's a no-op which makes112* compiler believe that there is some black box modification of a given113* variable and thus prevents compiler from making extra assumption about its114* value and potential simplifications and optimizations on this variable.115*116* E.g., compiler might often delay or even omit 32-bit to 64-bit casting of117* a variable, making some code patterns unverifiable. Putting barrier_var()118* in place will ensure that cast is performed before the barrier_var()119* invocation, because compiler has to pessimistically assume that embedded120* asm section might perform some extra operations on that variable.121*122* This is a variable-specific variant of more global barrier().123*/124#ifndef barrier_var125#define barrier_var(var) asm volatile("" : "+r"(var))126#endif127128/*129* Helper macro to throw a compilation error if __bpf_unreachable() gets130* built into the resulting code. This works given BPF back end does not131* implement __builtin_trap(). This is useful to assert that certain paths132* of the program code are never used and hence eliminated by the compiler.133*134* For example, consider a switch statement that covers known cases used by135* the program. __bpf_unreachable() can then reside in the default case. If136* the program gets extended such that a case is not covered in the switch137* statement, then it will throw a build error due to the default case not138* being compiled out.139*/140#ifndef __bpf_unreachable141# define __bpf_unreachable() __builtin_trap()142#endif143144/*145* Helper function to perform a tail call with a constant/immediate map slot.146*/147#if (defined(__clang__) && __clang_major__ >= 8) || (!defined(__clang__) && __GNUC__ > 12)148#if defined(__bpf__)149static __always_inline void150bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)151{152if (!__builtin_constant_p(slot))153__bpf_unreachable();154155/*156* Provide a hard guarantee that LLVM won't optimize setting r2 (map157* pointer) and r3 (constant map index) from _different paths_ ending158* up at the _same_ call insn as otherwise we won't be able to use the159* jmpq/nopl retpoline-free patching by the x86-64 JIT in the kernel160* given they mismatch. See also d2e4c1e6c294 ("bpf: Constant map key161* tracking for prog array pokes") for details on verifier tracking.162*163* Note on clobber list: we need to stay in-line with BPF calling164* convention, so even if we don't end up using r0, r4, r5, we need165* to mark them as clobber so that LLVM doesn't end up using them166* before / after the call.167*/168asm volatile("r1 = %[ctx]\n\t"169"r2 = %[map]\n\t"170"r3 = %[slot]\n\t"171"call 12"172:: [ctx]"r"(ctx), [map]"r"(map), [slot]"i"(slot)173: "r0", "r1", "r2", "r3", "r4", "r5");174}175#endif176#endif177178enum libbpf_pin_type {179LIBBPF_PIN_NONE,180/* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */181LIBBPF_PIN_BY_NAME,182};183184enum libbpf_tristate {185TRI_NO = 0,186TRI_YES = 1,187TRI_MODULE = 2,188};189190#define __kconfig __attribute__((section(".kconfig")))191#define __ksym __attribute__((section(".ksyms")))192#define __kptr_untrusted __attribute__((btf_type_tag("kptr_untrusted")))193#define __kptr __attribute__((btf_type_tag("kptr")))194#define __percpu_kptr __attribute__((btf_type_tag("percpu_kptr")))195#define __uptr __attribute__((btf_type_tag("uptr")))196197#if defined (__clang__)198#define bpf_ksym_exists(sym) ({ \199_Static_assert(!__builtin_constant_p(!!sym), \200#sym " should be marked as __weak"); \201!!sym; \202})203#elif __GNUC__ > 8204#define bpf_ksym_exists(sym) ({ \205_Static_assert(__builtin_has_attribute (*sym, __weak__), \206#sym " should be marked as __weak"); \207!!sym; \208})209#else210#define bpf_ksym_exists(sym) !!sym211#endif212213#define __arg_ctx __attribute__((btf_decl_tag("arg:ctx")))214#define __arg_nonnull __attribute((btf_decl_tag("arg:nonnull")))215#define __arg_nullable __attribute((btf_decl_tag("arg:nullable")))216#define __arg_trusted __attribute((btf_decl_tag("arg:trusted")))217#define __arg_untrusted __attribute((btf_decl_tag("arg:untrusted")))218#define __arg_arena __attribute((btf_decl_tag("arg:arena")))219220#ifndef ___bpf_concat221#define ___bpf_concat(a, b) a ## b222#endif223#ifndef ___bpf_apply224#define ___bpf_apply(fn, n) ___bpf_concat(fn, n)225#endif226#ifndef ___bpf_nth227#define ___bpf_nth(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c, N, ...) N228#endif229#ifndef ___bpf_narg230#define ___bpf_narg(...) \231___bpf_nth(_, ##__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)232#endif233234#define ___bpf_fill0(arr, p, x) do {} while (0)235#define ___bpf_fill1(arr, p, x) arr[p] = x236#define ___bpf_fill2(arr, p, x, args...) arr[p] = x; ___bpf_fill1(arr, p + 1, args)237#define ___bpf_fill3(arr, p, x, args...) arr[p] = x; ___bpf_fill2(arr, p + 1, args)238#define ___bpf_fill4(arr, p, x, args...) arr[p] = x; ___bpf_fill3(arr, p + 1, args)239#define ___bpf_fill5(arr, p, x, args...) arr[p] = x; ___bpf_fill4(arr, p + 1, args)240#define ___bpf_fill6(arr, p, x, args...) arr[p] = x; ___bpf_fill5(arr, p + 1, args)241#define ___bpf_fill7(arr, p, x, args...) arr[p] = x; ___bpf_fill6(arr, p + 1, args)242#define ___bpf_fill8(arr, p, x, args...) arr[p] = x; ___bpf_fill7(arr, p + 1, args)243#define ___bpf_fill9(arr, p, x, args...) arr[p] = x; ___bpf_fill8(arr, p + 1, args)244#define ___bpf_fill10(arr, p, x, args...) arr[p] = x; ___bpf_fill9(arr, p + 1, args)245#define ___bpf_fill11(arr, p, x, args...) arr[p] = x; ___bpf_fill10(arr, p + 1, args)246#define ___bpf_fill12(arr, p, x, args...) arr[p] = x; ___bpf_fill11(arr, p + 1, args)247#define ___bpf_fill(arr, args...) \248___bpf_apply(___bpf_fill, ___bpf_narg(args))(arr, 0, args)249250/*251* BPF_SEQ_PRINTF to wrap bpf_seq_printf to-be-printed values252* in a structure.253*/254#define BPF_SEQ_PRINTF(seq, fmt, args...) \255({ \256static const char ___fmt[] = fmt; \257unsigned long long ___param[___bpf_narg(args)]; \258\259_Pragma("GCC diagnostic push") \260_Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \261___bpf_fill(___param, args); \262_Pragma("GCC diagnostic pop") \263\264bpf_seq_printf(seq, ___fmt, sizeof(___fmt), \265___param, sizeof(___param)); \266})267268/*269* BPF_SNPRINTF wraps the bpf_snprintf helper with variadic arguments instead of270* an array of u64.271*/272#define BPF_SNPRINTF(out, out_size, fmt, args...) \273({ \274static const char ___fmt[] = fmt; \275unsigned long long ___param[___bpf_narg(args)]; \276\277_Pragma("GCC diagnostic push") \278_Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \279___bpf_fill(___param, args); \280_Pragma("GCC diagnostic pop") \281\282bpf_snprintf(out, out_size, ___fmt, \283___param, sizeof(___param)); \284})285286#ifdef BPF_NO_GLOBAL_DATA287#define BPF_PRINTK_FMT_MOD288#else289#define BPF_PRINTK_FMT_MOD static const290#endif291292#define __bpf_printk(fmt, ...) \293({ \294BPF_PRINTK_FMT_MOD char ____fmt[] = fmt; \295bpf_trace_printk(____fmt, sizeof(____fmt), \296##__VA_ARGS__); \297})298299/*300* __bpf_vprintk wraps the bpf_trace_vprintk helper with variadic arguments301* instead of an array of u64.302*/303#define __bpf_vprintk(fmt, args...) \304({ \305static const char ___fmt[] = fmt; \306unsigned long long ___param[___bpf_narg(args)]; \307\308_Pragma("GCC diagnostic push") \309_Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \310___bpf_fill(___param, args); \311_Pragma("GCC diagnostic pop") \312\313bpf_trace_vprintk(___fmt, sizeof(___fmt), \314___param, sizeof(___param)); \315})316317extern int bpf_stream_vprintk(int stream_id, const char *fmt__str, const void *args,318__u32 len__sz, void *aux__prog) __weak __ksym;319320#define bpf_stream_printk(stream_id, fmt, args...) \321({ \322static const char ___fmt[] = fmt; \323unsigned long long ___param[___bpf_narg(args)]; \324\325_Pragma("GCC diagnostic push") \326_Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \327___bpf_fill(___param, args); \328_Pragma("GCC diagnostic pop") \329\330bpf_stream_vprintk(stream_id, ___fmt, ___param, sizeof(___param), NULL);\331})332333/* Use __bpf_printk when bpf_printk call has 3 or fewer fmt args334* Otherwise use __bpf_vprintk335*/336#define ___bpf_pick_printk(...) \337___bpf_nth(_, ##__VA_ARGS__, __bpf_vprintk, __bpf_vprintk, __bpf_vprintk, \338__bpf_vprintk, __bpf_vprintk, __bpf_vprintk, __bpf_vprintk, \339__bpf_vprintk, __bpf_vprintk, __bpf_printk /*3*/, __bpf_printk /*2*/,\340__bpf_printk /*1*/, __bpf_printk /*0*/)341342/* Helper macro to print out debug messages */343#define bpf_printk(fmt, args...) ___bpf_pick_printk(args)(fmt, ##args)344345struct bpf_iter_num;346347extern int bpf_iter_num_new(struct bpf_iter_num *it, int start, int end) __weak __ksym;348extern int *bpf_iter_num_next(struct bpf_iter_num *it) __weak __ksym;349extern void bpf_iter_num_destroy(struct bpf_iter_num *it) __weak __ksym;350351#ifndef bpf_for_each352/* bpf_for_each(iter_type, cur_elem, args...) provides generic construct for353* using BPF open-coded iterators without having to write mundane explicit354* low-level loop logic. Instead, it provides for()-like generic construct355* that can be used pretty naturally. E.g., for some hypothetical cgroup356* iterator, you'd write:357*358* struct cgroup *cg, *parent_cg = <...>;359*360* bpf_for_each(cgroup, cg, parent_cg, CG_ITER_CHILDREN) {361* bpf_printk("Child cgroup id = %d", cg->cgroup_id);362* if (cg->cgroup_id == 123)363* break;364* }365*366* I.e., it looks almost like high-level for each loop in other languages,367* supports continue/break, and is verifiable by BPF verifier.368*369* For iterating integers, the difference between bpf_for_each(num, i, N, M)370* and bpf_for(i, N, M) is in that bpf_for() provides additional proof to371* verifier that i is in [N, M) range, and in bpf_for_each() case i is `int372* *`, not just `int`. So for integers bpf_for() is more convenient.373*374* Note: this macro relies on C99 feature of allowing to declare variables375* inside for() loop, bound to for() loop lifetime. It also utilizes GCC376* extension: __attribute__((cleanup(<func>))), supported by both GCC and377* Clang.378*/379#define bpf_for_each(type, cur, args...) for ( \380/* initialize and define destructor */ \381struct bpf_iter_##type ___it __attribute__((aligned(8), /* enforce, just in case */, \382cleanup(bpf_iter_##type##_destroy))), \383/* ___p pointer is just to call bpf_iter_##type##_new() *once* to init ___it */ \384*___p __attribute__((unused)) = ( \385bpf_iter_##type##_new(&___it, ##args), \386/* this is a workaround for Clang bug: it currently doesn't emit BTF */ \387/* for bpf_iter_##type##_destroy() when used from cleanup() attribute */ \388(void)bpf_iter_##type##_destroy, (void *)0); \389/* iteration and termination check */ \390(((cur) = bpf_iter_##type##_next(&___it))); \391)392#endif /* bpf_for_each */393394#ifndef bpf_for395/* bpf_for(i, start, end) implements a for()-like looping construct that sets396* provided integer variable *i* to values starting from *start* through,397* but not including, *end*. It also proves to BPF verifier that *i* belongs398* to range [start, end), so this can be used for accessing arrays without399* extra checks.400*401* Note: *start* and *end* are assumed to be expressions with no side effects402* and whose values do not change throughout bpf_for() loop execution. They do403* not have to be statically known or constant, though.404*405* Note: similarly to bpf_for_each(), it relies on C99 feature of declaring for()406* loop bound variables and cleanup attribute, supported by GCC and Clang.407*/408#define bpf_for(i, start, end) for ( \409/* initialize and define destructor */ \410struct bpf_iter_num ___it __attribute__((aligned(8), /* enforce, just in case */ \411cleanup(bpf_iter_num_destroy))), \412/* ___p pointer is necessary to call bpf_iter_num_new() *once* to init ___it */ \413*___p __attribute__((unused)) = ( \414bpf_iter_num_new(&___it, (start), (end)), \415/* this is a workaround for Clang bug: it currently doesn't emit BTF */ \416/* for bpf_iter_num_destroy() when used from cleanup() attribute */ \417(void)bpf_iter_num_destroy, (void *)0); \418({ \419/* iteration step */ \420int *___t = bpf_iter_num_next(&___it); \421/* termination and bounds check */ \422(___t && ((i) = *___t, (i) >= (start) && (i) < (end))); \423}); \424)425#endif /* bpf_for */426427#ifndef bpf_repeat428/* bpf_repeat(N) performs N iterations without exposing iteration number429*430* Note: similarly to bpf_for_each(), it relies on C99 feature of declaring for()431* loop bound variables and cleanup attribute, supported by GCC and Clang.432*/433#define bpf_repeat(N) for ( \434/* initialize and define destructor */ \435struct bpf_iter_num ___it __attribute__((aligned(8), /* enforce, just in case */ \436cleanup(bpf_iter_num_destroy))), \437/* ___p pointer is necessary to call bpf_iter_num_new() *once* to init ___it */ \438*___p __attribute__((unused)) = ( \439bpf_iter_num_new(&___it, 0, (N)), \440/* this is a workaround for Clang bug: it currently doesn't emit BTF */ \441/* for bpf_iter_num_destroy() when used from cleanup() attribute */ \442(void)bpf_iter_num_destroy, (void *)0); \443bpf_iter_num_next(&___it); \444/* nothing here */ \445)446#endif /* bpf_repeat */447448#endif449450451