bpf_helpers.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. /* Helper macro to print out debug messages */
  15. #define bpf_printk(fmt, ...) \
  16. ({ \
  17. char ____fmt[] = fmt; \
  18. bpf_trace_printk(____fmt, sizeof(____fmt), \
  19. ##__VA_ARGS__); \
  20. })
  21. /*
  22. * Helper macro to place programs, maps, license in
  23. * different sections in elf_bpf file. Section names
  24. * are interpreted by elf_bpf loader
  25. */
  26. #define SEC(NAME) __attribute__((section(NAME), used))
  27. #ifndef __always_inline
  28. #define __always_inline __attribute__((always_inline))
  29. #endif
  30. #ifndef __weak
  31. #define __weak __attribute__((weak))
  32. #endif
  33. /*
  34. * Helper macro to manipulate data structures
  35. */
  36. #ifndef offsetof
  37. #define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE, MEMBER)
  38. #endif
  39. #ifndef container_of
  40. #define container_of(ptr, type, member) \
  41. ({ \
  42. void *__mptr = (void *)(ptr); \
  43. ((type *)(__mptr - offsetof(type, member))); \
  44. })
  45. #endif
  46. /*
  47. * Helper structure used by eBPF C program
  48. * to describe BPF map attributes to libbpf loader
  49. */
  50. struct bpf_map_def {
  51. unsigned int type;
  52. unsigned int key_size;
  53. unsigned int value_size;
  54. unsigned int max_entries;
  55. unsigned int map_flags;
  56. };
  57. enum libbpf_pin_type {
  58. LIBBPF_PIN_NONE,
  59. /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
  60. LIBBPF_PIN_BY_NAME,
  61. };
  62. enum libbpf_tristate {
  63. TRI_NO = 0,
  64. TRI_YES = 1,
  65. TRI_MODULE = 2,
  66. };
  67. #define __kconfig __attribute__((section(".kconfig")))
  68. #define __ksym __attribute__((section(".ksyms")))
  69. #endif