github.com/castai/kvisor@v1.7.1-0.20240516114728-b3572a2607b5/pkg/ebpftracer/c/headers/common/common.h (about) 1 #ifndef __COMMON_COMMON_H__ 2 #define __COMMON_COMMON_H__ 3 4 #include <vmlinux.h> 5 6 #include <bpf/bpf_core_read.h> 7 8 #include <maps.h> 9 10 // PROTOTYPES 11 12 #define statfunc static __always_inline 13 14 // FUNCTIONS & MACROS 15 16 statfunc const char *get_device_name(struct device *dev) 17 { 18 struct kobject kobj = BPF_CORE_READ(dev, kobj); 19 return kobj.name; 20 } 21 22 // Workaround: Newer LLVM versions might fail to optimize has_prefix() 23 // loop unrolling with the following error: 24 // 25 // warning: loop not unrolled: the optimizer was unable to perform 26 // the requested transformation; the transformation might be 27 // disabled or specified as part of an unsupported transformation 28 // ordering 29 // 30 31 #if defined(__clang__) && __clang_major__ > 13 32 33 #define has_prefix(p, s, n) \ 34 ({ \ 35 int rc = 0; \ 36 char *pre = p, *str = s; \ 37 _Pragma("unroll") for (int z = 0; z < n; pre++, str++, z++) \ 38 { \ 39 if (!*pre) { \ 40 rc = 1; \ 41 break; \ 42 } else if (*pre != *str) { \ 43 rc = 0; \ 44 break; \ 45 } \ 46 } \ 47 rc; \ 48 }) 49 50 #else 51 52 static __inline int has_prefix(char *prefix, char *str, int n) 53 { 54 int i; 55 #pragma unroll 56 for (i = 0; i < n; prefix++, str++, i++) { 57 if (!*prefix) 58 return 1; 59 if (*prefix != *str) { 60 return 0; 61 } 62 } 63 64 // prefix is too long 65 return 0; 66 } 67 68 #endif 69 70 // helper macros for branch prediction 71 #ifndef likely 72 #define likely(x) __builtin_expect((x), 1) 73 #endif 74 #ifndef unlikely 75 #define unlikely(x) __builtin_expect((x), 0) 76 #endif 77 78 // helpers for iterating over a list_head struct 79 #define list_entry_ebpf(ptr, type, member) container_of(ptr, type, member) 80 81 #define list_next_entry_ebpf(pos, member) \ 82 list_entry_ebpf(BPF_CORE_READ(pos, member.next), typeof(*(pos)), member) 83 84 #define list_first_entry_ebpf(ptr, type, member) \ 85 list_entry_ebpf(BPF_CORE_READ(ptr, next), type, member) 86 87 #endif