1 #pragma once 2 3 #ifdef ENABLE_LIBBPF 4 5 // Either vmlinux.h or linux/types.h must be included before bpf/bpf_helpers.h 6 #ifdef USE_VMLINUX 7 // When using vmlinux.h, you can't use any system level headers. 8 #include <vmlinux.h> 9 #else 10 #include <linux/types.h> 11 #endif // USE_VMLINUX 12 #include <bpf/bpf_helpers.h> 13 14 #define DEFINE_BPF_MAP_BASE(the_map, TYPE, KeyType, ValueType, num_entries, gid) \ 15 struct { \ 16 __uint(type, BPF_MAP_TYPE_##TYPE); \ 17 __type(key, KeyType); \ 18 __type(value, ValueType); \ 19 __uint(max_entries, num_entries); \ 20 } the_map SEC(".maps"); \ 21 \ 22 static inline __always_inline __unused ValueType* bpf_##the_map##_lookup_elem( \ 23 const KeyType* k) { \ 24 return bpf_map_lookup_elem(&the_map, k); \ 25 }; \ 26 \ 27 static inline __always_inline __unused int bpf_##the_map##_update_elem( \ 28 const KeyType* k, const ValueType* v, unsigned long long flags) { \ 29 return bpf_map_update_elem(&the_map, k, v, flags); \ 30 }; \ 31 \ 32 static inline __always_inline __unused int bpf_##the_map##_delete_elem(const KeyType* k) { \ 33 return bpf_map_delete_elem(&the_map, k); \ 34 }; 35 36 #define DEFINE_BPF_MAP_GRW(the_map, TYPE, KeyType, ValueType, num_entries, gid) \ 37 DEFINE_BPF_MAP_BASE(the_map, TYPE, KeyType, ValueType, num_entries, gid) 38 #define DEFINE_BPF_MAP_GWO(the_map, TYPE, KeyType, ValueType, num_entries, gid) \ 39 DEFINE_BPF_MAP_BASE(the_map, TYPE, KeyType, ValueType, num_entries, gid) 40 #define DEFINE_BPF_MAP_GRO(the_map, TYPE, KeyType, ValueType, num_entries, gid) \ 41 DEFINE_BPF_MAP_BASE(the_map, TYPE, KeyType, ValueType, num_entries, gid) 42 43 #define DEFINE_BPF_PROG(SECTION_NAME, prog_uid, prog_gid, the_prog) \ 44 SEC(SECTION_NAME) \ 45 int the_prog 46 47 #define LICENSE(NAME) char _license[] SEC("license") = (NAME) 48 49 #else // LIBBPF DISABLED 50 51 #include <bpf_helpers.h> 52 53 #define bpf_printk(fmt, ...) \ 54 ({ \ 55 char ____fmt[] = fmt; \ 56 bpf_trace_printk(____fmt, sizeof(____fmt), ##__VA_ARGS__); \ 57 }) 58 59 #endif // ENABLE_LIBBPF 60