bpf_helpers.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* Common BPF helpers to be used by all BPF programs loaded by Android */
  2. #include <linux/bpf.h>
  3. #include <stdbool.h>
  4. #include <stdint.h>
  5. /* place things in different elf sections */
  6. #define SEC(NAME) __attribute__((section(NAME), used))
  7. /*
  8. * Helper functions called from eBPF programs written in C. These are
  9. * implemented in the kernel sources.
  10. */
  11. /* generic functions */
  12. /*
  13. * Type-unsafe bpf map functions - avoid if possible.
  14. *
  15. * Using these it is possible to pass in keys/values of the wrong type/size,
  16. * or, for 'unsafe_bpf_map_lookup_elem' receive into a pointer to the wrong type.
  17. * You will not get a compile time failure, and for certain types of errors you
  18. * might not even get a failure from the kernel's ebpf verifier during program load,
  19. * instead stuff might just not work right at runtime.
  20. *
  21. * Instead please use:
  22. * DEFINE_BPF_MAP(foo_map, TYPE, KeyType, ValueType, num_entries)
  23. * where TYPE can be something like HASH or ARRAY, and num_entries is an integer.
  24. *
  25. * This defines the map (hence this should not be used in a header file included
  26. * from multiple locations) and provides type safe accessors:
  27. * ValueType * bpf_foo_map_lookup_elem(KeyType *)
  28. * int bpf_foo_map_update_elem(KeyType *, ValueType *, flags)
  29. * int bpf_foo_map_delete_elem(KeyType *)
  30. *
  31. * This will make sure that if you change the type of a map you'll get compile
  32. * errors at any spots you forget to update with the new type.
  33. */
  34. static void* (*unsafe_bpf_map_lookup_elem)(void* map, void* key) = (void*)BPF_FUNC_map_lookup_elem;
  35. static int (*unsafe_bpf_map_update_elem)(void* map, void* key, void* value,
  36. unsigned long long flags) = (void*)
  37. BPF_FUNC_map_update_elem;
  38. static int (*unsafe_bpf_map_delete_elem)(void* map, void* key) = (void*)BPF_FUNC_map_delete_elem;
  39. /* type safe macro to declare a map and related accessor functions */
  40. #define DEFINE_BPF_MAP_NO_ACCESSORS(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries) \
  41. struct bpf_map_def SEC("maps") the_map = { \
  42. .type = BPF_MAP_TYPE_##TYPE, \
  43. .key_size = sizeof(TypeOfKey), \
  44. .value_size = sizeof(TypeOfValue), \
  45. .max_entries = (num_entries), \
  46. };
  47. #define DEFINE_BPF_MAP(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries) \
  48. DEFINE_BPF_MAP_NO_ACCESSORS(the_map, TYPE, TypeOfKey, TypeOfValue, num_entries) \
  49. \
  50. static inline __always_inline __unused TypeOfValue* bpf_##the_map##_lookup_elem( \
  51. TypeOfKey* k) { \
  52. return unsafe_bpf_map_lookup_elem(&the_map, k); \
  53. }; \
  54. \
  55. static inline __always_inline __unused int bpf_##the_map##_update_elem( \
  56. TypeOfKey* k, TypeOfValue* v, unsigned long long flags) { \
  57. return unsafe_bpf_map_update_elem(&the_map, k, v, flags); \
  58. }; \
  59. \
  60. static inline __always_inline __unused int bpf_##the_map##_delete_elem(TypeOfKey* k) { \
  61. return unsafe_bpf_map_delete_elem(&the_map, k); \
  62. };
  63. static int (*bpf_probe_read)(void* dst, int size, void* unsafe_ptr) = (void*) BPF_FUNC_probe_read;
  64. static unsigned long long (*bpf_ktime_get_ns)(void) = (void*) BPF_FUNC_ktime_get_ns;
  65. static int (*bpf_trace_printk)(const char* fmt, int fmt_size, ...) = (void*) BPF_FUNC_trace_printk;
  66. static unsigned long long (*bpf_get_current_pid_tgid)(void) = (void*) BPF_FUNC_get_current_pid_tgid;
  67. static unsigned long long (*bpf_get_current_uid_gid)(void) = (void*) BPF_FUNC_get_current_uid_gid;
  68. static unsigned long long (*bpf_get_smp_processor_id)(void) = (void*) BPF_FUNC_get_smp_processor_id;
  69. /* networking */
  70. static uint64_t (*bpf_get_socket_cookie)(struct __sk_buff* skb) = (void*)BPF_FUNC_get_socket_cookie;
  71. static uint32_t (*bpf_get_socket_uid)(struct __sk_buff* skb) = (void*)BPF_FUNC_get_socket_uid;
  72. static int (*bpf_skb_load_bytes)(struct __sk_buff* skb, int off, void* to,
  73. int len) = (void*)BPF_FUNC_skb_load_bytes;
  74. static int (*bpf_skb_change_proto)(struct __sk_buff* skb, __be16 proto,
  75. __u64 flags) = (void*)BPF_FUNC_skb_change_proto;
  76. static int (*bpf_l3_csum_replace)(struct __sk_buff* skb, __u32 offset, __u64 from, __u64 to,
  77. __u64 flags) = (void*)BPF_FUNC_l3_csum_replace;
  78. static int (*bpf_l4_csum_replace)(struct __sk_buff* skb, __u32 offset, __u64 from, __u64 to,
  79. __u64 flags) = (void*)BPF_FUNC_l4_csum_replace;
  80. static int (*bpf_redirect)(__u32 ifindex, __u64 flags) = (void*)BPF_FUNC_redirect;
  81. /*
  82. * Map structure to be used by Android eBPF C programs. The Android eBPF loader
  83. * uses this structure from eBPF object to create maps at boot time.
  84. *
  85. * The eBPF C program should define structure in the maps section using
  86. * SEC("maps") otherwise it will be ignored by the eBPF loader.
  87. *
  88. * For example:
  89. * struct bpf_map_def SEC("maps") mymap { .type=... , .key_size=... }
  90. */
  91. struct bpf_map_def {
  92. unsigned int type;
  93. unsigned int key_size;
  94. unsigned int value_size;
  95. unsigned int max_entries;
  96. unsigned int map_flags;
  97. unsigned int pad1;
  98. unsigned int pad2;
  99. };