bpf_helpers.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. #define SEC(name) \
  23. _Pragma("GCC diagnostic push") \
  24. _Pragma("GCC diagnostic ignored \"-Wignored-attributes\"") \
  25. __attribute__((section(name), used)) \
  26. _Pragma("GCC diagnostic pop") \
  27. /* Avoid 'linux/stddef.h' definition of '__always_inline'. */
  28. #undef __always_inline
  29. #define __always_inline inline __attribute__((always_inline))
  30. #ifndef __noinline
  31. #define __noinline __attribute__((noinline))
  32. #endif
  33. #ifndef __weak
  34. #define __weak __attribute__((weak))
  35. #endif
  36. /*
  37. * Use __hidden attribute to mark a non-static BPF subprogram effectively
  38. * static for BPF verifier's verification algorithm purposes, allowing more
  39. * extensive and permissive BPF verification process, taking into account
  40. * subprogram's caller context.
  41. */
  42. #define __hidden __attribute__((visibility("hidden")))
  43. /* When utilizing vmlinux.h with BPF CO-RE, user BPF programs can't include
  44. * any system-level headers (such as stddef.h, linux/version.h, etc), and
  45. * commonly-used macros like NULL and KERNEL_VERSION aren't available through
  46. * vmlinux.h. This just adds unnecessary hurdles and forces users to re-define
  47. * them on their own. So as a convenience, provide such definitions here.
  48. */
  49. #ifndef NULL
  50. #define NULL ((void *)0)
  51. #endif
  52. #ifndef KERNEL_VERSION
  53. #define KERNEL_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + ((c) > 255 ? 255 : (c)))
  54. #endif
  55. /*
  56. * Helper macros to manipulate data structures
  57. */
  58. #ifndef offsetof
  59. #define offsetof(TYPE, MEMBER) ((unsigned long)&((TYPE *)0)->MEMBER)
  60. #endif
  61. #ifndef container_of
  62. #define container_of(ptr, type, member) \
  63. ({ \
  64. void *__mptr = (void *)(ptr); \
  65. ((type *)(__mptr - offsetof(type, member))); \
  66. })
  67. #endif
  68. /*
  69. * Helper macro to throw a compilation error if __bpf_unreachable() gets
  70. * built into the resulting code. This works given BPF back end does not
  71. * implement __builtin_trap(). This is useful to assert that certain paths
  72. * of the program code are never used and hence eliminated by the compiler.
  73. *
  74. * For example, consider a switch statement that covers known cases used by
  75. * the program. __bpf_unreachable() can then reside in the default case. If
  76. * the program gets extended such that a case is not covered in the switch
  77. * statement, then it will throw a build error due to the default case not
  78. * being compiled out.
  79. */
  80. #ifndef __bpf_unreachable
  81. # define __bpf_unreachable() __builtin_trap()
  82. #endif
  83. /*
  84. * Helper function to perform a tail call with a constant/immediate map slot.
  85. */
  86. #if __clang_major__ >= 8 && defined(__bpf__)
  87. static __always_inline void
  88. bpf_tail_call_static(void *ctx, const void *map, const __u32 slot)
  89. {
  90. if (!__builtin_constant_p(slot))
  91. __bpf_unreachable();
  92. /*
  93. * Provide a hard guarantee that LLVM won't optimize setting r2 (map
  94. * pointer) and r3 (constant map index) from _different paths_ ending
  95. * up at the _same_ call insn as otherwise we won't be able to use the
  96. * jmpq/nopl retpoline-free patching by the x86-64 JIT in the kernel
  97. * given they mismatch. See also d2e4c1e6c294 ("bpf: Constant map key
  98. * tracking for prog array pokes") for details on verifier tracking.
  99. *
  100. * Note on clobber list: we need to stay in-line with BPF calling
  101. * convention, so even if we don't end up using r0, r4, r5, we need
  102. * to mark them as clobber so that LLVM doesn't end up using them
  103. * before / after the call.
  104. */
  105. asm volatile("r1 = %[ctx]\n\t"
  106. "r2 = %[map]\n\t"
  107. "r3 = %[slot]\n\t"
  108. "call 12"
  109. :: [ctx]"r"(ctx), [map]"r"(map), [slot]"i"(slot)
  110. : "r0", "r1", "r2", "r3", "r4", "r5");
  111. }
  112. #endif
  113. /*
  114. * Helper structure used by eBPF C program
  115. * to describe BPF map attributes to libbpf loader
  116. */
  117. struct bpf_map_def {
  118. unsigned int type;
  119. unsigned int key_size;
  120. unsigned int value_size;
  121. unsigned int max_entries;
  122. unsigned int map_flags;
  123. };
  124. enum libbpf_pin_type {
  125. LIBBPF_PIN_NONE,
  126. /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
  127. LIBBPF_PIN_BY_NAME,
  128. };
  129. enum libbpf_tristate {
  130. TRI_NO = 0,
  131. TRI_YES = 1,
  132. TRI_MODULE = 2,
  133. };
  134. #define __kconfig __attribute__((section(".kconfig")))
  135. #define __ksym __attribute__((section(".ksyms")))
  136. #ifndef ___bpf_concat
  137. #define ___bpf_concat(a, b) a ## b
  138. #endif
  139. #ifndef ___bpf_apply
  140. #define ___bpf_apply(fn, n) ___bpf_concat(fn, n)
  141. #endif
  142. #ifndef ___bpf_nth
  143. #define ___bpf_nth(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c, N, ...) N
  144. #endif
  145. #ifndef ___bpf_narg
  146. #define ___bpf_narg(...) \
  147. ___bpf_nth(_, ##__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
  148. #endif
  149. #define ___bpf_fill0(arr, p, x) do {} while (0)
  150. #define ___bpf_fill1(arr, p, x) arr[p] = x
  151. #define ___bpf_fill2(arr, p, x, args...) arr[p] = x; ___bpf_fill1(arr, p + 1, args)
  152. #define ___bpf_fill3(arr, p, x, args...) arr[p] = x; ___bpf_fill2(arr, p + 1, args)
  153. #define ___bpf_fill4(arr, p, x, args...) arr[p] = x; ___bpf_fill3(arr, p + 1, args)
  154. #define ___bpf_fill5(arr, p, x, args...) arr[p] = x; ___bpf_fill4(arr, p + 1, args)
  155. #define ___bpf_fill6(arr, p, x, args...) arr[p] = x; ___bpf_fill5(arr, p + 1, args)
  156. #define ___bpf_fill7(arr, p, x, args...) arr[p] = x; ___bpf_fill6(arr, p + 1, args)
  157. #define ___bpf_fill8(arr, p, x, args...) arr[p] = x; ___bpf_fill7(arr, p + 1, args)
  158. #define ___bpf_fill9(arr, p, x, args...) arr[p] = x; ___bpf_fill8(arr, p + 1, args)
  159. #define ___bpf_fill10(arr, p, x, args...) arr[p] = x; ___bpf_fill9(arr, p + 1, args)
  160. #define ___bpf_fill11(arr, p, x, args...) arr[p] = x; ___bpf_fill10(arr, p + 1, args)
  161. #define ___bpf_fill12(arr, p, x, args...) arr[p] = x; ___bpf_fill11(arr, p + 1, args)
  162. #define ___bpf_fill(arr, args...) \
  163. ___bpf_apply(___bpf_fill, ___bpf_narg(args))(arr, 0, args)
  164. /*
  165. * BPF_SEQ_PRINTF to wrap bpf_seq_printf to-be-printed values
  166. * in a structure.
  167. */
  168. #define BPF_SEQ_PRINTF(seq, fmt, args...) \
  169. ({ \
  170. static const char ___fmt[] = fmt; \
  171. unsigned long long ___param[___bpf_narg(args)]; \
  172. \
  173. _Pragma("GCC diagnostic push") \
  174. _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
  175. ___bpf_fill(___param, args); \
  176. _Pragma("GCC diagnostic pop") \
  177. \
  178. bpf_seq_printf(seq, ___fmt, sizeof(___fmt), \
  179. ___param, sizeof(___param)); \
  180. })
  181. /*
  182. * BPF_SNPRINTF wraps the bpf_snprintf helper with variadic arguments instead of
  183. * an array of u64.
  184. */
  185. #define BPF_SNPRINTF(out, out_size, fmt, args...) \
  186. ({ \
  187. static const char ___fmt[] = fmt; \
  188. unsigned long long ___param[___bpf_narg(args)]; \
  189. \
  190. _Pragma("GCC diagnostic push") \
  191. _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
  192. ___bpf_fill(___param, args); \
  193. _Pragma("GCC diagnostic pop") \
  194. \
  195. bpf_snprintf(out, out_size, ___fmt, \
  196. ___param, sizeof(___param)); \
  197. })
  198. #ifdef BPF_NO_GLOBAL_DATA
  199. #define BPF_PRINTK_FMT_MOD
  200. #else
  201. #define BPF_PRINTK_FMT_MOD static const
  202. #endif
  203. #define __bpf_printk(fmt, ...) \
  204. ({ \
  205. BPF_PRINTK_FMT_MOD char ____fmt[] = fmt; \
  206. bpf_trace_printk(____fmt, sizeof(____fmt), \
  207. ##__VA_ARGS__); \
  208. })
  209. /*
  210. * __bpf_vprintk wraps the bpf_trace_vprintk helper with variadic arguments
  211. * instead of an array of u64.
  212. */
  213. #define __bpf_vprintk(fmt, args...) \
  214. ({ \
  215. static const char ___fmt[] = fmt; \
  216. unsigned long long ___param[___bpf_narg(args)]; \
  217. \
  218. _Pragma("GCC diagnostic push") \
  219. _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
  220. ___bpf_fill(___param, args); \
  221. _Pragma("GCC diagnostic pop") \
  222. \
  223. bpf_trace_vprintk(___fmt, sizeof(___fmt), \
  224. ___param, sizeof(___param)); \
  225. })
  226. /* Use __bpf_printk when bpf_printk call has 3 or fewer fmt args
  227. * Otherwise use __bpf_vprintk
  228. */
  229. #define ___bpf_pick_printk(...) \
  230. ___bpf_nth(_, ##__VA_ARGS__, __bpf_vprintk, __bpf_vprintk, __bpf_vprintk, \
  231. __bpf_vprintk, __bpf_vprintk, __bpf_vprintk, __bpf_vprintk, \
  232. __bpf_vprintk, __bpf_vprintk, __bpf_printk /*3*/, __bpf_printk /*2*/,\
  233. __bpf_printk /*1*/, __bpf_printk /*0*/)
  234. /* Helper macro to print out debug messages */
  235. #define bpf_printk(fmt, args...) ___bpf_pick_printk(args)(fmt, ##args)
  236. #endif