bpf_helpers.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
  2. #ifndef __BPF_HELPERS__
  3. #define __BPF_HELPERS__
  4. /*
  5. * Note that bpf programs need to include either
  6. * vmlinux.h (auto-generated from BTF) or linux/types.h
  7. * in advance since bpf_helper_defs.h uses such types
  8. * as __u64.
  9. */
  10. #include "bpf_helper_defs.h"
  11. #define __uint(name, val) int (*name)[val]
  12. #define __type(name, val) typeof(val) *name
  13. #define __array(name, val) typeof(val) *name[]
  14. /*
  15. * Helper macro to place programs, maps, license in
  16. * different sections in elf_bpf file. Section names
  17. * are interpreted by libbpf depending on the context (BPF programs, BPF maps,
  18. * extern variables, etc).
  19. * To allow use of SEC() with externs (e.g., for extern .maps declarations),
  20. * make sure __attribute__((unused)) doesn't trigger compilation warning.
  21. */
  22. #if __GNUC__ && !__clang__
  23. /*
  24. * Pragma macros are broken on GCC
  25. * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55578
  26. * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90400
  27. */
  28. #define SEC(name) __attribute__((section(name), used))
  29. #else
  30. #define SEC(name) \
  31. _Pragma("GCC diagnostic push") \
  32. _Pragma("GCC diagnostic ignored \"-Wignored-attributes\"") \
  33. __attribute__((section(name), used)) \
  34. _Pragma("GCC diagnostic pop") \
  35. #endif
  36. /* Avoid 'linux/stddef.h' definition of '__always_inline'. */
  37. #undef __always_inline
  38. #define __always_inline inline __attribute__((always_inline))
  39. #ifndef __noinline
  40. #define __noinline __attribute__((noinline))
  41. #endif
  42. #ifndef __weak
  43. #define __weak __attribute__((weak))
  44. #endif
  45. /*
  46. * Use __hidden attribute to mark a non-static BPF subprogram effectively
  47. * static for BPF verifier's verification algorithm purposes, allowing more
  48. * extensive and permissive BPF verification process, taking into account
  49. * subprogram's caller context.
  50. */
  51. #define __hidden __attribute__((visibility("hidden")))
  52. /* When utilizing vmlinux.h with BPF CO-RE, user BPF programs can't include
  53. * any system-level headers (such as stddef.h, linux/version.h, etc), and
  54. * commonly-used macros like NULL and KERNEL_VERSION aren't available through
  55. * vmlinux.h. This just adds unnecessary hurdles and forces users to re-define
  56. * them on their own. So as a convenience, provide such definitions here.
  57. */
  58. #ifndef NULL
  59. #define NULL ((void *)0)
  60. #endif
  61. #ifndef KERNEL_VERSION
  62. #define KERNEL_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + ((c) > 255 ? 255 : (c)))
  63. #endif
  64. /*
  65. * Helper macros to manipulate data structures
  66. */
  67. /* offsetof() definition that uses __builtin_offset() might not preserve field
  68. * offset CO-RE relocation properly, so force-redefine offsetof() using
  69. * old-school approach which works with CO-RE correctly
  70. */
  71. #undef offsetof
  72. #define offsetof(type, member) ((unsigned long)&((type *)0)->member)
  73. /* redefined container_of() to ensure we use the above offsetof() macro */
  74. #undef container_of
  75. #define container_of(ptr, type, member) \
  76. ({ \
  77. void *__mptr = (void *)(ptr); \
  78. ((type *)(__mptr - offsetof(type, member))); \
  79. })
  80. /*
  81. * Compiler (optimization) barrier.
  82. */
  83. #ifndef barrier
  84. #define barrier() asm volatile("" ::: "memory")
  85. #endif
  86. /* Variable-specific compiler (optimization) barrier. It's a no-op which makes
  87. * compiler believe that there is some black box modification of a given
  88. * variable and thus prevents compiler from making extra assumption about its
  89. * value and potential simplifications and optimizations on this variable.
  90. *
  91. * E.g., compiler might often delay or even omit 32-bit to 64-bit casting of
  92. * a variable, making some code patterns unverifiable. Putting barrier_var()
  93. * in place will ensure that cast is performed before the barrier_var()
  94. * invocation, because compiler has to pessimistically assume that embedded
  95. * asm section might perform some extra operations on that variable.
  96. *
  97. * This is a variable-specific variant of more global barrier().
  98. */
  99. #ifndef barrier_var
  100. #define barrier_var(var) asm volatile("" : "=r"(var) : "0"(var))
  101. #endif
  102. /*
  103. * Helper macro to throw a compilation error if __bpf_unreachable() gets
  104. * built into the resulting code. This works given BPF back end does not
  105. * implement __builtin_trap(). This is useful to assert that certain paths
  106. * of the program code are never used and hence eliminated by the compiler.
  107. *
  108. * For example, consider a switch statement that covers known cases used by
  109. * the program. __bpf_unreachable() can then reside in the default case. If
  110. * the program gets extended such that a case is not covered in the switch
  111. * statement, then it will throw a build error due to the default case not
  112. * being compiled out.
  113. */
  114. #ifndef __bpf_unreachable
  115. # define __bpf_unreachable() __builtin_trap()
  116. #endif
  117. /*
  118. * Helper function to perform a tail call with a constant/immediate map slot.
  119. */
  120. #if __clang_major__ >= 8 && defined(__bpf__)
  121. static __always_inline void
  122. bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
  123. {
  124. if (!__builtin_constant_p(slot))
  125. __bpf_unreachable();
  126. /*
  127. * Provide a hard guarantee that LLVM won't optimize setting r2 (map
  128. * pointer) and r3 (constant map index) from _different paths_ ending
  129. * up at the _same_ call insn as otherwise we won't be able to use the
  130. * jmpq/nopl retpoline-free patching by the x86-64 JIT in the kernel
  131. * given they mismatch. See also d2e4c1e6c294 ("bpf: Constant map key
  132. * tracking for prog array pokes") for details on verifier tracking.
  133. *
  134. * Note on clobber list: we need to stay in-line with BPF calling
  135. * convention, so even if we don't end up using r0, r4, r5, we need
  136. * to mark them as clobber so that LLVM doesn't end up using them
  137. * before / after the call.
  138. */
  139. asm volatile("r1 = %[ctx]\n\t"
  140. "r2 = %[map]\n\t"
  141. "r3 = %[slot]\n\t"
  142. "call 12"
  143. :: [ctx]"r"(ctx), [map]"r"(map), [slot]"i"(slot)
  144. : "r0", "r1", "r2", "r3", "r4", "r5");
  145. }
  146. #endif
  147. enum libbpf_pin_type {
  148. LIBBPF_PIN_NONE,
  149. /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
  150. LIBBPF_PIN_BY_NAME,
  151. };
  152. enum libbpf_tristate {
  153. TRI_NO = 0,
  154. TRI_YES = 1,
  155. TRI_MODULE = 2,
  156. };
  157. #define __kconfig __attribute__((section(".kconfig")))
  158. #define __ksym __attribute__((section(".ksyms")))
  159. #define __kptr __attribute__((btf_type_tag("kptr")))
  160. #define __kptr_ref __attribute__((btf_type_tag("kptr_ref")))
  161. #ifndef ___bpf_concat
  162. #define ___bpf_concat(a, b) a ## b
  163. #endif
  164. #ifndef ___bpf_apply
  165. #define ___bpf_apply(fn, n) ___bpf_concat(fn, n)
  166. #endif
  167. #ifndef ___bpf_nth
  168. #define ___bpf_nth(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c, N, ...) N
  169. #endif
  170. #ifndef ___bpf_narg
  171. #define ___bpf_narg(...) \
  172. ___bpf_nth(_, ##__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
  173. #endif
  174. #define ___bpf_fill0(arr, p, x) do {} while (0)
  175. #define ___bpf_fill1(arr, p, x) arr[p] = x
  176. #define ___bpf_fill2(arr, p, x, args...) arr[p] = x; ___bpf_fill1(arr, p + 1, args)
  177. #define ___bpf_fill3(arr, p, x, args...) arr[p] = x; ___bpf_fill2(arr, p + 1, args)
  178. #define ___bpf_fill4(arr, p, x, args...) arr[p] = x; ___bpf_fill3(arr, p + 1, args)
  179. #define ___bpf_fill5(arr, p, x, args...) arr[p] = x; ___bpf_fill4(arr, p + 1, args)
  180. #define ___bpf_fill6(arr, p, x, args...) arr[p] = x; ___bpf_fill5(arr, p + 1, args)
  181. #define ___bpf_fill7(arr, p, x, args...) arr[p] = x; ___bpf_fill6(arr, p + 1, args)
  182. #define ___bpf_fill8(arr, p, x, args...) arr[p] = x; ___bpf_fill7(arr, p + 1, args)
  183. #define ___bpf_fill9(arr, p, x, args...) arr[p] = x; ___bpf_fill8(arr, p + 1, args)
  184. #define ___bpf_fill10(arr, p, x, args...) arr[p] = x; ___bpf_fill9(arr, p + 1, args)
  185. #define ___bpf_fill11(arr, p, x, args...) arr[p] = x; ___bpf_fill10(arr, p + 1, args)
  186. #define ___bpf_fill12(arr, p, x, args...) arr[p] = x; ___bpf_fill11(arr, p + 1, args)
  187. #define ___bpf_fill(arr, args...) \
  188. ___bpf_apply(___bpf_fill, ___bpf_narg(args))(arr, 0, args)
  189. /*
  190. * BPF_SEQ_PRINTF to wrap bpf_seq_printf to-be-printed values
  191. * in a structure.
  192. */
  193. #define BPF_SEQ_PRINTF(seq, fmt, args...) \
  194. ({ \
  195. static const char ___fmt[] = fmt; \
  196. unsigned long long ___param[___bpf_narg(args)]; \
  197. \
  198. _Pragma("GCC diagnostic push") \
  199. _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
  200. ___bpf_fill(___param, args); \
  201. _Pragma("GCC diagnostic pop") \
  202. \
  203. bpf_seq_printf(seq, ___fmt, sizeof(___fmt), \
  204. ___param, sizeof(___param)); \
  205. })
  206. /*
  207. * BPF_SNPRINTF wraps the bpf_snprintf helper with variadic arguments instead of
  208. * an array of u64.
  209. */
  210. #define BPF_SNPRINTF(out, out_size, fmt, args...) \
  211. ({ \
  212. static const char ___fmt[] = fmt; \
  213. unsigned long long ___param[___bpf_narg(args)]; \
  214. \
  215. _Pragma("GCC diagnostic push") \
  216. _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
  217. ___bpf_fill(___param, args); \
  218. _Pragma("GCC diagnostic pop") \
  219. \
  220. bpf_snprintf(out, out_size, ___fmt, \
  221. ___param, sizeof(___param)); \
  222. })
  223. #ifdef BPF_NO_GLOBAL_DATA
  224. #define BPF_PRINTK_FMT_MOD
  225. #else
  226. #define BPF_PRINTK_FMT_MOD static const
  227. #endif
  228. #define __bpf_printk(fmt, ...) \
  229. ({ \
  230. BPF_PRINTK_FMT_MOD char ____fmt[] = fmt; \
  231. bpf_trace_printk(____fmt, sizeof(____fmt), \
  232. ##__VA_ARGS__); \
  233. })
  234. /*
  235. * __bpf_vprintk wraps the bpf_trace_vprintk helper with variadic arguments
  236. * instead of an array of u64.
  237. */
  238. #define __bpf_vprintk(fmt, args...) \
  239. ({ \
  240. static const char ___fmt[] = fmt; \
  241. unsigned long long ___param[___bpf_narg(args)]; \
  242. \
  243. _Pragma("GCC diagnostic push") \
  244. _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
  245. ___bpf_fill(___param, args); \
  246. _Pragma("GCC diagnostic pop") \
  247. \
  248. bpf_trace_vprintk(___fmt, sizeof(___fmt), \
  249. ___param, sizeof(___param)); \
  250. })
  251. /* Use __bpf_printk when bpf_printk call has 3 or fewer fmt args
  252. * Otherwise use __bpf_vprintk
  253. */
  254. #define ___bpf_pick_printk(...) \
  255. ___bpf_nth(_, ##__VA_ARGS__, __bpf_vprintk, __bpf_vprintk, __bpf_vprintk, \
  256. __bpf_vprintk, __bpf_vprintk, __bpf_vprintk, __bpf_vprintk, \
  257. __bpf_vprintk, __bpf_vprintk, __bpf_printk /*3*/, __bpf_printk /*2*/,\
  258. __bpf_printk /*1*/, __bpf_printk /*0*/)
  259. /* Helper macro to print out debug messages */
  260. #define bpf_printk(fmt, args...) ___bpf_pick_printk(args)(fmt, ##args)
  261. #endif