| 1 |
|
| 2 |
#ifndef __BPF_HELPERS__ |
| 3 |
#define __BPF_HELPERS__ |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#include "bpf_helper_defs.h" |
| 12 |
|
| 13 |
#define __uint(name, val) int (*name)[val] |
| 14 |
#define __type(name, val) typeof(val) *name |
| 15 |
#define __array(name, val) typeof(val) *name[] |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#define SEC(name) \ |
| 26 |
_Pragma("GCC diagnostic push") \ |
| 27 |
_Pragma("GCC diagnostic ignored \"-Wignored-attributes\"") \ |
| 28 |
__attribute__((section(name), used)) \ |
| 29 |
_Pragma("GCC diagnostic pop") \ |
| 30 |
|
| 31 |
|
| 32 |
#undef __always_inline |
| 33 |
#define __always_inline inline __attribute__((always_inline)) |
| 34 |
|
| 35 |
#ifndef __noinline |
| 36 |
#define __noinline __attribute__((noinline)) |
| 37 |
#endif |
| 38 |
#ifndef __weak |
| 39 |
#define __weak __attribute__((weak)) |
| 40 |
#endif |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
#define __hidden __attribute__((visibility("hidden"))) |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
#ifndef NULL |
| 57 |
#define NULL ((void *)0) |
| 58 |
#endif |
| 59 |
|
| 60 |
#ifndef KERNEL_VERSION |
| 61 |
#define KERNEL_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + ((c) > 255 ? 255 : (c))) |
| 62 |
#endif |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
#ifndef offsetof |
| 68 |
#define offsetof(TYPE, MEMBER) ((unsigned long)&((TYPE *)0)->MEMBER) |
| 69 |
#endif |
| 70 |
#ifndef container_of |
| 71 |
#define container_of(ptr, type, member) \ |
| 72 |
({ \ |
| 73 |
void *__mptr = (void *)(ptr); \ |
| 74 |
((type *)(__mptr - offsetof(type, member))); \ |
| 75 |
}) |
| 76 |
#endif |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
#ifndef __bpf_unreachable |
| 91 |
# define __bpf_unreachable() __builtin_trap() |
| 92 |
#endif |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
#if __clang_major__ >= 8 && defined(__bpf__) |
| 98 |
static __always_inline void |
| 99 |
bpf_tail_call_static(void *ctx, const void *map, const __u32 slot) |
| 100 |
{ |
| 101 |
if (!__builtin_constant_p(slot)) |
| 102 |
__bpf_unreachable(); |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
asm volatile("r1 = %[ctx]\n\t" |
| 118 |
"r2 = %[map]\n\t" |
| 119 |
"r3 = %[slot]\n\t" |
| 120 |
"call 12" |
| 121 |
:: [ctx]"r"(ctx), [map]"r"(map), [slot]"i"(slot) |
| 122 |
: "r0", "r1", "r2", "r3", "r4", "r5"); |
| 123 |
} |
| 124 |
#endif |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
struct bpf_map_def { |
| 131 |
unsigned int type; |
| 132 |
unsigned int key_size; |
| 133 |
unsigned int value_size; |
| 134 |
unsigned int max_entries; |
| 135 |
unsigned int map_flags; |
| 136 |
}; |
| 137 |
|
| 138 |
enum libbpf_pin_type { |
| 139 |
LIBBPF_PIN_NONE, |
| 140 |
|
| 141 |
LIBBPF_PIN_BY_NAME, |
| 142 |
}; |
| 143 |
|
| 144 |
enum libbpf_tristate { |
| 145 |
TRI_NO = 0, |
| 146 |
TRI_YES = 1, |
| 147 |
TRI_MODULE = 2, |
| 148 |
}; |
| 149 |
|
| 150 |
#define __kconfig __attribute__((section(".kconfig"))) |
| 151 |
#define __ksym __attribute__((section(".ksyms"))) |
| 152 |
|
| 153 |
#ifndef ___bpf_concat |
| 154 |
#define ___bpf_concat(a, b) a ## b |
| 155 |
#endif |
| 156 |
#ifndef ___bpf_apply |
| 157 |
#define ___bpf_apply(fn, n) ___bpf_concat(fn, n) |
| 158 |
#endif |
| 159 |
#ifndef ___bpf_nth |
| 160 |
#define ___bpf_nth(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c, N, ...) N |
| 161 |
#endif |
| 162 |
#ifndef ___bpf_narg |
| 163 |
#define ___bpf_narg(...) \ |
| 164 |
___bpf_nth(_, ##__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) |
| 165 |
#endif |
| 166 |
|
| 167 |
#define ___bpf_fill0(arr, p, x) do {} while (0) |
| 168 |
#define ___bpf_fill1(arr, p, x) arr[p] = x |
| 169 |
#define ___bpf_fill2(arr, p, x, args...) arr[p] = x; ___bpf_fill1(arr, p + 1, args) |
| 170 |
#define ___bpf_fill3(arr, p, x, args...) arr[p] = x; ___bpf_fill2(arr, p + 1, args) |
| 171 |
#define ___bpf_fill4(arr, p, x, args...) arr[p] = x; ___bpf_fill3(arr, p + 1, args) |
| 172 |
#define ___bpf_fill5(arr, p, x, args...) arr[p] = x; ___bpf_fill4(arr, p + 1, args) |
| 173 |
#define ___bpf_fill6(arr, p, x, args...) arr[p] = x; ___bpf_fill5(arr, p + 1, args) |
| 174 |
#define ___bpf_fill7(arr, p, x, args...) arr[p] = x; ___bpf_fill6(arr, p + 1, args) |
| 175 |
#define ___bpf_fill8(arr, p, x, args...) arr[p] = x; ___bpf_fill7(arr, p + 1, args) |
| 176 |
#define ___bpf_fill9(arr, p, x, args...) arr[p] = x; ___bpf_fill8(arr, p + 1, args) |
| 177 |
#define ___bpf_fill10(arr, p, x, args...) arr[p] = x; ___bpf_fill9(arr, p + 1, args) |
| 178 |
#define ___bpf_fill11(arr, p, x, args...) arr[p] = x; ___bpf_fill10(arr, p + 1, args) |
| 179 |
#define ___bpf_fill12(arr, p, x, args...) arr[p] = x; ___bpf_fill11(arr, p + 1, args) |
| 180 |
#define ___bpf_fill(arr, args...) \ |
| 181 |
___bpf_apply(___bpf_fill, ___bpf_narg(args))(arr, 0, args) |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
#define BPF_SEQ_PRINTF(seq, fmt, args...) \ |
| 188 |
({ \ |
| 189 |
static const char ___fmt[] = fmt; \ |
| 190 |
unsigned long long ___param[___bpf_narg(args)]; \ |
| 191 |
\ |
| 192 |
_Pragma("GCC diagnostic push") \ |
| 193 |
_Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ |
| 194 |
___bpf_fill(___param, args); \ |
| 195 |
_Pragma("GCC diagnostic pop") \ |
| 196 |
\ |
| 197 |
bpf_seq_printf(seq, ___fmt, sizeof(___fmt), \ |
| 198 |
___param, sizeof(___param)); \ |
| 199 |
}) |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
#define BPF_SNPRINTF(out, out_size, fmt, args...) \ |
| 206 |
({ \ |
| 207 |
static const char ___fmt[] = fmt; \ |
| 208 |
unsigned long long ___param[___bpf_narg(args)]; \ |
| 209 |
\ |
| 210 |
_Pragma("GCC diagnostic push") \ |
| 211 |
_Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ |
| 212 |
___bpf_fill(___param, args); \ |
| 213 |
_Pragma("GCC diagnostic pop") \ |
| 214 |
\ |
| 215 |
bpf_snprintf(out, out_size, ___fmt, \ |
| 216 |
___param, sizeof(___param)); \ |
| 217 |
}) |
| 218 |
|
| 219 |
#ifdef BPF_NO_GLOBAL_DATA |
| 220 |
#define BPF_PRINTK_FMT_MOD |
| 221 |
#else |
| 222 |
#define BPF_PRINTK_FMT_MOD static const |
| 223 |
#endif |
| 224 |
|
| 225 |
#define __bpf_printk(fmt, ...) \ |
| 226 |
({ \ |
| 227 |
BPF_PRINTK_FMT_MOD char ____fmt[] = fmt; \ |
| 228 |
bpf_trace_printk(____fmt, sizeof(____fmt), \ |
| 229 |
##__VA_ARGS__); \ |
| 230 |
}) |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
#define __bpf_vprintk(fmt, args...) \ |
| 237 |
({ \ |
| 238 |
static const char ___fmt[] = fmt; \ |
| 239 |
unsigned long long ___param[___bpf_narg(args)]; \ |
| 240 |
\ |
| 241 |
_Pragma("GCC diagnostic push") \ |
| 242 |
_Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ |
| 243 |
___bpf_fill(___param, args); \ |
| 244 |
_Pragma("GCC diagnostic pop") \ |
| 245 |
\ |
| 246 |
bpf_trace_vprintk(___fmt, sizeof(___fmt), \ |
| 247 |
___param, sizeof(___param)); \ |
| 248 |
}) |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
#define ___bpf_pick_printk(...) \ |
| 254 |
___bpf_nth(_, ##__VA_ARGS__, __bpf_vprintk, __bpf_vprintk, __bpf_vprintk, \ |
| 255 |
__bpf_vprintk, __bpf_vprintk, __bpf_vprintk, __bpf_vprintk, \ |
| 256 |
__bpf_vprintk, __bpf_vprintk, __bpf_printk , __bpf_printk ,\ |
| 257 |
__bpf_printk , __bpf_printk ) |
| 258 |
|
| 259 |
|
| 260 |
#define bpf_printk(fmt, args...) ___bpf_pick_printk(args)(fmt, ##args) |
| 261 |
|
| 262 |
#endif |