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