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