3 * Copyright (c) 2015 PLUMgrid, Inc.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 #ifndef __BPF_HELPERS_H
18 #define __BPF_HELPERS_H
20 /* In Linux 5.4 asm_inline was introduced, but it's not supported by clang.
21 * Redefine it to just asm to enable successful compilation.
25 #define asm_inline asm
28 /* Before bpf_helpers.h is included, uapi bpf.h has been
29 * included, which references linux/types.h. This may bring
30 * in asm_volatile_goto definition if permitted based on
31 * compiler setup and kernel configs.
33 * clang does not support "asm volatile goto" yet.
34 * So redefine asm_volatile_goto to some invalid asm code.
35 * If asm_volatile_goto is actually used by the bpf program,
36 * a compilation error will appear.
38 #ifdef asm_volatile_goto
39 #undef asm_volatile_goto
41 #define asm_volatile_goto(x...) asm volatile("invalid use of asm_volatile_goto")
43 /* In 4.18 and later, when CONFIG_FUNCTION_TRACER is defined, kernel Makefile adds
44 * -DCC_USING_FENTRY. Let do the same for bpf programs.
46 #if defined(CONFIG_FUNCTION_TRACER)
47 #define CC_USING_FENTRY
50 #include <uapi/linux/bpf.h>
51 #include <uapi/linux/if_packet.h>
52 #include <linux/version.h>
53 #include <linux/log2.h>
56 #ifndef CONFIG_BPF_SYSCALL
57 #error "CONFIG_BPF_SYSCALL is undefined, please check your .config or ask your Linux distro to enable this feature"
60 #ifdef PERF_MAX_STACK_DEPTH
61 #define BPF_MAX_STACK_DEPTH PERF_MAX_STACK_DEPTH
63 #define BPF_MAX_STACK_DEPTH 127
66 /* helper macro to place programs, maps, license in
67 * different sections in elf_bpf file. Section names
68 * are interpreted by elf_bpf loader
70 #define BCC_SEC(NAME) __attribute__((section(NAME), used))
72 // Associate map with its key/value types
73 #define BPF_ANNOTATE_KV_PAIR(name, type_key, type_val) \
74 struct ____btf_map_##name { \
78 struct ____btf_map_##name \
79 __attribute__ ((section(".maps." #name), used)) \
80 ____btf_map_##name = { }
82 // Associate map with its key/value types for QUEUE/STACK map types
83 #define BPF_ANNOTATE_KV_PAIR_QUEUESTACK(name, type_val) \
84 struct ____btf_map_##name { \
87 struct ____btf_map_##name \
88 __attribute__ ((section(".maps." #name), used)) \
89 ____btf_map_##name = { }
91 // Changes to the macro require changes in BFrontendAction classes
92 #define BPF_F_TABLE(_table_type, _key_type, _leaf_type, _name, _max_entries, _flags) \
93 struct _name##_table_t { \
96 _leaf_type * (*lookup) (_key_type *); \
97 _leaf_type * (*lookup_or_init) (_key_type *, _leaf_type *); \
98 _leaf_type * (*lookup_or_try_init) (_key_type *, _leaf_type *); \
99 int (*update) (_key_type *, _leaf_type *); \
100 int (*insert) (_key_type *, _leaf_type *); \
101 int (*delete) (_key_type *); \
102 void (*call) (void *, int index); \
103 void (*increment) (_key_type, ...); \
104 int (*get_stackid) (void *, u64); \
108 __attribute__((section("maps/" _table_type))) \
109 struct _name##_table_t _name = { .flags = (_flags), .max_entries = (_max_entries) }; \
110 BPF_ANNOTATE_KV_PAIR(_name, _key_type, _leaf_type)
113 // Changes to the macro require changes in BFrontendAction classes
114 #define BPF_QUEUESTACK(_table_type, _name, _leaf_type, _max_entries, _flags) \
115 struct _name##_table_t { \
117 int * (*peek) (_leaf_type *); \
118 int * (*pop) (_leaf_type *); \
119 int * (*push) (_leaf_type *, u64); \
123 __attribute__((section("maps/" _table_type))) \
124 struct _name##_table_t _name = { .flags = (_flags), .max_entries = (_max_entries) }; \
125 BPF_ANNOTATE_KV_PAIR_QUEUESTACK(_name, _leaf_type)
127 // define queue with 3 parameters (_type=queue/stack automatically) and default flags to 0
128 #define BPF_QUEUE_STACK3(_type, _name, _leaf_type, _max_entries) \
129 BPF_QUEUESTACK(_type, _name, _leaf_type, _max_entries, 0)
131 // define queue with 4 parameters (_type=queue/stack automatically)
132 #define BPF_QUEUE_STACK4(_type, _name, _leaf_type, _max_entries, _flags) \
133 BPF_QUEUESTACK(_type, _name, _leaf_type, _max_entries, _flags)
135 // helper for default-variable macro function
136 #define BPF_QUEUE_STACKX(_1, _2, _3, _4, NAME, ...) NAME
138 #define BPF_QUEUE(...) \
139 BPF_QUEUE_STACKX(__VA_ARGS__, BPF_QUEUE_STACK4, BPF_QUEUE_STACK3)("queue", __VA_ARGS__)
141 #define BPF_STACK(...) \
142 BPF_QUEUE_STACKX(__VA_ARGS__, BPF_QUEUE_STACK4, BPF_QUEUE_STACK3)("stack", __VA_ARGS__)
144 #define BPF_QUEUESTACK_PINNED(_table_type, _name, _leaf_type, _max_entries, _flags, _pinned) \
145 BPF_QUEUESTACK(_table_type ":" _pinned, _name, _leaf_type, _max_entries, _flags)
147 #define BPF_QUEUESTACK_PUBLIC(_table_type, _name, _leaf_type, _max_entries, _flags) \
148 BPF_QUEUESTACK(_table_type, _name, _leaf_type, _max_entries, _flags); \
149 __attribute__((section("maps/export"))) \
150 struct _name##_table_t __##_name
152 #define BPF_QUEUESTACK_SHARED(_table_type, _name, _leaf_type, _max_entries, _flags) \
153 BPF_QUEUESTACK(_table_type, _name, _leaf_type, _max_entries, _flags); \
154 __attribute__((section("maps/shared"))) \
155 struct _name##_table_t __##_name
157 #define BPF_TABLE(_table_type, _key_type, _leaf_type, _name, _max_entries) \
158 BPF_F_TABLE(_table_type, _key_type, _leaf_type, _name, _max_entries, 0)
160 #define BPF_TABLE_PINNED(_table_type, _key_type, _leaf_type, _name, _max_entries, _pinned) \
161 BPF_TABLE(_table_type ":" _pinned, _key_type, _leaf_type, _name, _max_entries)
163 // define a table same as above but allow it to be referenced by other modules
164 #define BPF_TABLE_PUBLIC(_table_type, _key_type, _leaf_type, _name, _max_entries) \
165 BPF_TABLE(_table_type, _key_type, _leaf_type, _name, _max_entries); \
166 __attribute__((section("maps/export"))) \
167 struct _name##_table_t __##_name
169 // define a table that is shared across the programs in the same namespace
170 #define BPF_TABLE_SHARED(_table_type, _key_type, _leaf_type, _name, _max_entries) \
171 BPF_TABLE(_table_type, _key_type, _leaf_type, _name, _max_entries); \
172 __attribute__((section("maps/shared"))) \
173 struct _name##_table_t __##_name
175 // Identifier for current CPU used in perf_submit and perf_read
176 // Prefer BPF_F_CURRENT_CPU flag, falls back to call helper for older kernel
177 // Can be overridden from BCC
178 #ifndef CUR_CPU_IDENTIFIER
179 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0)
180 #define CUR_CPU_IDENTIFIER BPF_F_CURRENT_CPU
182 #define CUR_CPU_IDENTIFIER bpf_get_smp_processor_id()
186 // Table for pushing custom events to userspace via perf ring buffer
187 #define BPF_PERF_OUTPUT(_name) \
188 struct _name##_table_t { \
191 /* map.perf_submit(ctx, data, data_size) */ \
192 int (*perf_submit) (void *, void *, u32); \
193 int (*perf_submit_skb) (void *, u32, void *, u32); \
196 __attribute__((section("maps/perf_output"))) \
197 struct _name##_table_t _name = { .max_entries = 0 }
199 // Table for pushing custom events to userspace via ring buffer
200 #define BPF_RINGBUF_OUTPUT(_name, _num_pages) \
201 struct _name##_table_t { \
204 /* map.ringbuf_output(data, data_size, flags) */ \
205 int (*ringbuf_output) (void *, u64, u64); \
206 /* map.ringbuf_reserve(data_size) */ \
207 void* (*ringbuf_reserve) (u64); \
208 /* map.ringbuf_discard(data, flags) */ \
209 void (*ringbuf_discard) (void *, u64); \
210 /* map.ringbuf_submit(data, flags) */ \
211 void (*ringbuf_submit) (void *, u64); \
214 __attribute__((section("maps/ringbuf"))) \
215 struct _name##_table_t _name = { .max_entries = ((_num_pages) * PAGE_SIZE) }
217 // Table for reading hw perf cpu counters
218 #define BPF_PERF_ARRAY(_name, _max_entries) \
219 struct _name##_table_t { \
222 /* counter = map.perf_read(index) */ \
223 u64 (*perf_read) (int); \
224 int (*perf_counter_value) (int, void *, u32); \
227 __attribute__((section("maps/perf_array"))) \
228 struct _name##_table_t _name = { .max_entries = (_max_entries) }
230 // Table for cgroup file descriptors
231 #define BPF_CGROUP_ARRAY(_name, _max_entries) \
232 struct _name##_table_t { \
235 int (*check_current_task) (int); \
238 __attribute__((section("maps/cgroup_array"))) \
239 struct _name##_table_t _name = { .max_entries = (_max_entries) }
241 #define BPF_HASH1(_name) \
242 BPF_TABLE("hash", u64, u64, _name, 10240)
243 #define BPF_HASH2(_name, _key_type) \
244 BPF_TABLE("hash", _key_type, u64, _name, 10240)
245 #define BPF_HASH3(_name, _key_type, _leaf_type) \
246 BPF_TABLE("hash", _key_type, _leaf_type, _name, 10240)
247 #define BPF_HASH4(_name, _key_type, _leaf_type, _size) \
248 BPF_TABLE("hash", _key_type, _leaf_type, _name, _size)
250 // helper for default-variable macro function
251 #define BPF_HASHX(_1, _2, _3, _4, NAME, ...) NAME
253 // Define a hash function, some arguments optional
254 // BPF_HASH(name, key_type=u64, leaf_type=u64, size=10240)
255 #define BPF_HASH(...) \
256 BPF_HASHX(__VA_ARGS__, BPF_HASH4, BPF_HASH3, BPF_HASH2, BPF_HASH1)(__VA_ARGS__)
258 #define BPF_PERCPU_HASH1(_name) \
259 BPF_TABLE("percpu_hash", u64, u64, _name, 10240)
260 #define BPF_PERCPU_HASH2(_name, _key_type) \
261 BPF_TABLE("percpu_hash", _key_type, u64, _name, 10240)
262 #define BPF_PERCPU_HASH3(_name, _key_type, _leaf_type) \
263 BPF_TABLE("percpu_hash", _key_type, _leaf_type, _name, 10240)
264 #define BPF_PERCPU_HASH4(_name, _key_type, _leaf_type, _size) \
265 BPF_TABLE("percpu_hash", _key_type, _leaf_type, _name, _size)
267 // helper for default-variable macro function
268 #define BPF_PERCPU_HASHX(_1, _2, _3, _4, NAME, ...) NAME
270 // Define a hash function, some arguments optional
271 // BPF_PERCPU_HASH(name, key_type=u64, leaf_type=u64, size=10240)
272 #define BPF_PERCPU_HASH(...) \
274 __VA_ARGS__, BPF_PERCPU_HASH4, BPF_PERCPU_HASH3, BPF_PERCPU_HASH2, BPF_PERCPU_HASH1) \
277 #define BPF_ARRAY1(_name) \
278 BPF_TABLE("array", int, u64, _name, 10240)
279 #define BPF_ARRAY2(_name, _leaf_type) \
280 BPF_TABLE("array", int, _leaf_type, _name, 10240)
281 #define BPF_ARRAY3(_name, _leaf_type, _size) \
282 BPF_TABLE("array", int, _leaf_type, _name, _size)
284 // helper for default-variable macro function
285 #define BPF_ARRAYX(_1, _2, _3, NAME, ...) NAME
287 // Define an array function, some arguments optional
288 // BPF_ARRAY(name, leaf_type=u64, size=10240)
289 #define BPF_ARRAY(...) \
290 BPF_ARRAYX(__VA_ARGS__, BPF_ARRAY3, BPF_ARRAY2, BPF_ARRAY1)(__VA_ARGS__)
292 #define BPF_PERCPU_ARRAY1(_name) \
293 BPF_TABLE("percpu_array", int, u64, _name, 10240)
294 #define BPF_PERCPU_ARRAY2(_name, _leaf_type) \
295 BPF_TABLE("percpu_array", int, _leaf_type, _name, 10240)
296 #define BPF_PERCPU_ARRAY3(_name, _leaf_type, _size) \
297 BPF_TABLE("percpu_array", int, _leaf_type, _name, _size)
299 // helper for default-variable macro function
300 #define BPF_PERCPU_ARRAYX(_1, _2, _3, NAME, ...) NAME
302 // Define an array function (per CPU), some arguments optional
303 // BPF_PERCPU_ARRAY(name, leaf_type=u64, size=10240)
304 #define BPF_PERCPU_ARRAY(...) \
306 __VA_ARGS__, BPF_PERCPU_ARRAY3, BPF_PERCPU_ARRAY2, BPF_PERCPU_ARRAY1) \
309 #define BPF_HIST1(_name) \
310 BPF_TABLE("histogram", int, u64, _name, 64)
311 #define BPF_HIST2(_name, _key_type) \
312 BPF_TABLE("histogram", _key_type, u64, _name, 64)
313 #define BPF_HIST3(_name, _key_type, _size) \
314 BPF_TABLE("histogram", _key_type, u64, _name, _size)
315 #define BPF_HISTX(_1, _2, _3, NAME, ...) NAME
317 // Define a histogram, some arguments optional
318 // BPF_HISTOGRAM(name, key_type=int, size=64)
319 #define BPF_HISTOGRAM(...) \
320 BPF_HISTX(__VA_ARGS__, BPF_HIST3, BPF_HIST2, BPF_HIST1)(__VA_ARGS__)
322 #define BPF_LPM_TRIE1(_name) \
323 BPF_F_TABLE("lpm_trie", u64, u64, _name, 10240, BPF_F_NO_PREALLOC)
324 #define BPF_LPM_TRIE2(_name, _key_type) \
325 BPF_F_TABLE("lpm_trie", _key_type, u64, _name, 10240, BPF_F_NO_PREALLOC)
326 #define BPF_LPM_TRIE3(_name, _key_type, _leaf_type) \
327 BPF_F_TABLE("lpm_trie", _key_type, _leaf_type, _name, 10240, BPF_F_NO_PREALLOC)
328 #define BPF_LPM_TRIE4(_name, _key_type, _leaf_type, _size) \
329 BPF_F_TABLE("lpm_trie", _key_type, _leaf_type, _name, _size, BPF_F_NO_PREALLOC)
330 #define BPF_LPM_TRIEX(_1, _2, _3, _4, NAME, ...) NAME
332 // Define a LPM trie function, some arguments optional
333 // BPF_LPM_TRIE(name, key_type=u64, leaf_type=u64, size=10240)
334 #define BPF_LPM_TRIE(...) \
335 BPF_LPM_TRIEX(__VA_ARGS__, BPF_LPM_TRIE4, BPF_LPM_TRIE3, BPF_LPM_TRIE2, BPF_LPM_TRIE1)(__VA_ARGS__)
337 struct bpf_stacktrace {
338 u64 ip[BPF_MAX_STACK_DEPTH];
341 struct bpf_stacktrace_buildid {
342 struct bpf_stack_build_id trace[BPF_MAX_STACK_DEPTH];
345 #define BPF_STACK_TRACE(_name, _max_entries) \
346 BPF_TABLE("stacktrace", int, struct bpf_stacktrace, _name, roundup_pow_of_two(_max_entries))
348 #define BPF_STACK_TRACE_BUILDID(_name, _max_entries) \
349 BPF_F_TABLE("stacktrace", int, struct bpf_stacktrace_buildid, _name, roundup_pow_of_two(_max_entries), BPF_F_STACK_BUILD_ID)
351 #define BPF_PROG_ARRAY(_name, _max_entries) \
352 BPF_TABLE("prog", u32, u32, _name, _max_entries)
354 #define BPF_XDP_REDIRECT_MAP(_table_type, _leaf_type, _name, _max_entries) \
355 struct _name##_table_t { \
358 /* xdp_act = map.redirect_map(index, flag) */ \
359 u64 (*redirect_map) (int, int); \
362 __attribute__((section("maps/"_table_type))) \
363 struct _name##_table_t _name = { .max_entries = (_max_entries) }
365 #define BPF_DEVMAP(_name, _max_entries) \
366 BPF_XDP_REDIRECT_MAP("devmap", int, _name, _max_entries)
368 #define BPF_CPUMAP(_name, _max_entries) \
369 BPF_XDP_REDIRECT_MAP("cpumap", u32, _name, _max_entries)
371 #define BPF_XSKMAP(_name, _max_entries) \
372 struct _name##_table_t { \
375 int * (*lookup) (int *); \
376 /* xdp_act = map.redirect_map(index, flag) */ \
377 u64 (*redirect_map) (int, int); \
380 __attribute__((section("maps/xskmap"))) \
381 struct _name##_table_t _name = { .max_entries = (_max_entries) }
383 #define BPF_ARRAY_OF_MAPS(_name, _inner_map_name, _max_entries) \
384 BPF_TABLE("array_of_maps$" _inner_map_name, int, int, _name, _max_entries)
386 #define BPF_HASH_OF_MAPS(_name, _inner_map_name, _max_entries) \
387 BPF_TABLE("hash_of_maps$" _inner_map_name, int, int, _name, _max_entries)
389 #define BPF_SK_STORAGE(_name, _leaf_type) \
390 struct _name##_table_t { \
393 void * (*sk_storage_get) (void *, void *, int); \
394 int (*sk_storage_delete) (void *); \
397 __attribute__((section("maps/sk_storage"))) \
398 struct _name##_table_t _name = { .flags = BPF_F_NO_PREALLOC }; \
399 BPF_ANNOTATE_KV_PAIR(_name, int, _leaf_type)
401 #define BPF_SOCKMAP_COMMON(_name, _max_entries, _kind, _helper_name) \
402 struct _name##_table_t { \
405 int (*update) (u32 *, int *); \
406 int (*delete) (u32 *); \
407 /* ret = map.sock_map_update(ctx, key, flag) */ \
408 int (* _helper_name) (void *, void *, u64); \
411 __attribute__((section("maps/" _kind))) \
412 struct _name##_table_t _name = { .max_entries = (_max_entries) }; \
413 BPF_ANNOTATE_KV_PAIR(_name, u32, int)
415 #define BPF_SOCKMAP(_name, _max_entries) \
416 BPF_SOCKMAP_COMMON(_name, _max_entries, "sockmap", sock_map_update)
418 #define BPF_SOCKHASH_COMMON(_name, _key_type, _max_entries) \
419 struct _name##_table_t {\
422 int (*update) (_key_type *, int *); \
423 int (*delete) (_key_type *); \
424 int (*sock_hash_update) (void *, void *, u64); \
427 __attribute__((section("maps/sockhash"))) \
428 struct _name##_table_t _name = { .max_entries = (_max_entries) }; \
429 BPF_ANNOTATE_KV_PAIR(_name, _key_type, int)
431 #define BPF_SOCKHASH1(_name) \
432 BPF_SOCKHASH_COMMON(_name, u32, 10240)
433 #define BPF_SOCKHASH2(_name, _key_type) \
434 BPF_SOCKHASH_COMMON(_name, _key_type, 10240)
435 #define BPF_SOCKHASH3(_name, _key_type, _max_entries) \
436 BPF_SOCKHASH_COMMON(_name, _key_type, _max_entries)
438 #define BPF_SOCKHASHX(_1, _2, _3, NAME, ...) NAME
439 // We can define a five-tuple as the key, and basically never define the val type.
440 // BPF_SOCKHASH(name, key_type=u64, size=10240)
441 #define BPF_SOCKHASH(...) \
442 BPF_SOCKHASHX(__VA_ARGS__, BPF_SOCKHASH3, BPF_SOCKHASH2, BPF_SOCKHASH1)(__VA_ARGS__)
444 #define BPF_CGROUP_STORAGE_COMMON(_name, _leaf_type, _kind) \
445 struct _name##_table_t { \
446 struct bpf_cgroup_storage_key key; \
448 _leaf_type * (*lookup) (struct bpf_cgroup_storage_key *); \
449 int (*update) (struct bpf_cgroup_storage_key *, _leaf_type *); \
450 int (*get_local_storage) (u64); \
452 __attribute__((section("maps/" _kind))) \
453 struct _name##_table_t _name = { 0 }; \
454 BPF_ANNOTATE_KV_PAIR(_name, struct bpf_cgroup_storage_key, _leaf_type)
456 #define BPF_CGROUP_STORAGE(_name, _leaf_type) \
457 BPF_CGROUP_STORAGE_COMMON(_name, _leaf_type, "cgroup_storage")
459 #define BPF_PERCPU_CGROUP_STORAGE(_name, _leaf_type) \
460 BPF_CGROUP_STORAGE_COMMON(_name, _leaf_type, "percpu_cgroup_storage")
462 // packet parsing state machine helpers
463 #define cursor_advance(_cursor, _len) \
464 ({ void *_tmp = _cursor; _cursor += _len; _tmp; })
466 #ifdef LINUX_VERSION_CODE_OVERRIDE
467 unsigned _version BCC_SEC("version") = LINUX_VERSION_CODE_OVERRIDE;
469 unsigned _version BCC_SEC("version") = LINUX_VERSION_CODE;
472 /* helper functions called from eBPF programs written in C */
473 static void *(*bpf_map_lookup_elem)(void *map, void *key) =
474 (void *) BPF_FUNC_map_lookup_elem;
475 static int (*bpf_map_update_elem)(void *map, void *key, void *value, u64 flags) =
476 (void *) BPF_FUNC_map_update_elem;
477 static int (*bpf_map_delete_elem)(void *map, void *key) =
478 (void *) BPF_FUNC_map_delete_elem;
479 static int (*bpf_probe_read)(void *dst, u64 size, const void *unsafe_ptr) =
480 (void *) BPF_FUNC_probe_read;
481 static u64 (*bpf_ktime_get_ns)(void) =
482 (void *) BPF_FUNC_ktime_get_ns;
483 static u32 (*bpf_get_prandom_u32)(void) =
484 (void *) BPF_FUNC_get_prandom_u32;
485 static int (*bpf_trace_printk_)(const char *fmt, u64 fmt_size, ...) =
486 (void *) BPF_FUNC_trace_printk;
487 static int (*bpf_probe_read_str)(void *dst, u64 size, const void *unsafe_ptr) =
488 (void *) BPF_FUNC_probe_read_str;
489 int bpf_trace_printk(const char *fmt, ...) asm("llvm.bpf.extra");
490 static inline __attribute__((always_inline))
491 void bpf_tail_call_(u64 map_fd, void *ctx, int index) {
492 ((void (*)(void *, u64, int))BPF_FUNC_tail_call)(ctx, map_fd, index);
494 static int (*bpf_clone_redirect)(void *ctx, int ifindex, u32 flags) =
495 (void *) BPF_FUNC_clone_redirect;
496 static u64 (*bpf_get_smp_processor_id)(void) =
497 (void *) BPF_FUNC_get_smp_processor_id;
498 static u64 (*bpf_get_current_pid_tgid)(void) =
499 (void *) BPF_FUNC_get_current_pid_tgid;
500 static u64 (*bpf_get_current_uid_gid)(void) =
501 (void *) BPF_FUNC_get_current_uid_gid;
502 static int (*bpf_get_current_comm)(void *buf, int buf_size) =
503 (void *) BPF_FUNC_get_current_comm;
504 static u64 (*bpf_get_cgroup_classid)(void *ctx) =
505 (void *) BPF_FUNC_get_cgroup_classid;
506 static u64 (*bpf_skb_vlan_push)(void *ctx, u16 proto, u16 vlan_tci) =
507 (void *) BPF_FUNC_skb_vlan_push;
508 static u64 (*bpf_skb_vlan_pop)(void *ctx) =
509 (void *) BPF_FUNC_skb_vlan_pop;
510 static int (*bpf_skb_get_tunnel_key)(void *ctx, void *to, u32 size, u64 flags) =
511 (void *) BPF_FUNC_skb_get_tunnel_key;
512 static int (*bpf_skb_set_tunnel_key)(void *ctx, void *from, u32 size, u64 flags) =
513 (void *) BPF_FUNC_skb_set_tunnel_key;
514 static u64 (*bpf_perf_event_read)(void *map, u64 flags) =
515 (void *) BPF_FUNC_perf_event_read;
516 static int (*bpf_redirect)(int ifindex, u32 flags) =
517 (void *) BPF_FUNC_redirect;
518 static u32 (*bpf_get_route_realm)(void *ctx) =
519 (void *) BPF_FUNC_get_route_realm;
520 static int (*bpf_perf_event_output)(void *ctx, void *map, u64 index, void *data, u32 size) =
521 (void *) BPF_FUNC_perf_event_output;
522 static int (*bpf_skb_load_bytes)(void *ctx, int offset, void *to, u32 len) =
523 (void *) BPF_FUNC_skb_load_bytes;
524 static int (*bpf_perf_event_read_value)(void *map, u64 flags, void *buf, u32 buf_size) =
525 (void *) BPF_FUNC_perf_event_read_value;
526 static int (*bpf_perf_prog_read_value)(void *ctx, void *buf, u32 buf_size) =
527 (void *) BPF_FUNC_perf_prog_read_value;
528 static int (*bpf_current_task_under_cgroup)(void *map, int index) =
529 (void *) BPF_FUNC_current_task_under_cgroup;
530 static u32 (*bpf_get_socket_cookie)(void *ctx) =
531 (void *) BPF_FUNC_get_socket_cookie;
532 static u64 (*bpf_get_socket_uid)(void *ctx) =
533 (void *) BPF_FUNC_get_socket_uid;
534 static int (*bpf_getsockopt)(void *ctx, int level, int optname, void *optval, int optlen) =
535 (void *) BPF_FUNC_getsockopt;
536 static int (*bpf_redirect_map)(void *map, int key, int flags) =
537 (void *) BPF_FUNC_redirect_map;
538 static int (*bpf_set_hash)(void *ctx, u32 hash) =
539 (void *) BPF_FUNC_set_hash;
540 static int (*bpf_setsockopt)(void *ctx, int level, int optname, void *optval, int optlen) =
541 (void *) BPF_FUNC_setsockopt;
542 static int (*bpf_skb_adjust_room)(void *ctx, int len_diff, u32 mode, u64 flags) =
543 (void *) BPF_FUNC_skb_adjust_room;
544 static int (*bpf_skb_under_cgroup)(void *ctx, void *map, int index) =
545 (void *) BPF_FUNC_skb_under_cgroup;
546 static struct bpf_sock *(*bpf_skc_lookup_tcp)(void *ctx, struct bpf_sock_tuple *tuple, int size,
547 unsigned long long netns_id,
548 unsigned long long flags) =
549 (void *) BPF_FUNC_skc_lookup_tcp;
550 static int (*bpf_sk_redirect_map)(void *ctx, void *map, int key, int flags) =
551 (void *) BPF_FUNC_sk_redirect_map;
552 static int (*bpf_sock_map_update)(void *map, void *key, void *value, unsigned long long flags) =
553 (void *) BPF_FUNC_sock_map_update;
554 static int (*bpf_strtol)(const char *buf, size_t buf_len, u64 flags, long *res) =
555 (void *) BPF_FUNC_strtol;
556 static int (*bpf_strtoul)(const char *buf, size_t buf_len, u64 flags, unsigned long *res) =
557 (void *) BPF_FUNC_strtoul;
558 static int (*bpf_sysctl_get_current_value)(struct bpf_sysctl *ctx, char *buf, size_t buf_len) =
559 (void *) BPF_FUNC_sysctl_get_current_value;
560 static int (*bpf_sysctl_get_name)(struct bpf_sysctl *ctx, char *buf, size_t buf_len, u64 flags) =
561 (void *) BPF_FUNC_sysctl_get_name;
562 static int (*bpf_sysctl_get_new_value)(struct bpf_sysctl *ctx, char *buf, size_t buf_len) =
563 (void *) BPF_FUNC_sysctl_get_new_value;
564 static int (*bpf_sysctl_set_new_value)(struct bpf_sysctl *ctx, const char *buf, size_t buf_len) =
565 (void *) BPF_FUNC_sysctl_set_new_value;
566 static int (*bpf_tcp_check_syncookie)(void *sk, void *ip, int ip_len, void *tcp,
568 (void *) BPF_FUNC_tcp_check_syncookie;
569 static int (*bpf_xdp_adjust_meta)(void *ctx, int offset) =
570 (void *) BPF_FUNC_xdp_adjust_meta;
572 /* bcc_get_stackid will return a negative value in the case of an error
574 * BPF_STACK_TRACE(_name, _size) will allocate space for _size stack traces.
575 * -ENOMEM will be returned when this limit is reached.
577 * -EFAULT is typically returned when requesting user-space stack straces (using
578 * BPF_F_USER_STACK) for kernel threads. However, a valid stackid may be
579 * returned in some cases; consider a tracepoint or kprobe executing in the
580 * kernel context. Given this you can typically ignore -EFAULT errors when
581 * retrieving user-space stack traces.
583 static int (*bcc_get_stackid_)(void *ctx, void *map, u64 flags) =
584 (void *) BPF_FUNC_get_stackid;
585 static inline __attribute__((always_inline))
586 int bcc_get_stackid(uintptr_t map, void *ctx, u64 flags) {
587 return bcc_get_stackid_(ctx, (void *)map, flags);
590 static int (*bpf_csum_diff)(void *from, u64 from_size, void *to, u64 to_size, u64 seed) =
591 (void *) BPF_FUNC_csum_diff;
592 static int (*bpf_skb_get_tunnel_opt)(void *ctx, void *md, u32 size) =
593 (void *) BPF_FUNC_skb_get_tunnel_opt;
594 static int (*bpf_skb_set_tunnel_opt)(void *ctx, void *md, u32 size) =
595 (void *) BPF_FUNC_skb_set_tunnel_opt;
596 static int (*bpf_skb_change_proto)(void *ctx, u16 proto, u64 flags) =
597 (void *) BPF_FUNC_skb_change_proto;
598 static int (*bpf_skb_change_type)(void *ctx, u32 type) =
599 (void *) BPF_FUNC_skb_change_type;
600 static u32 (*bpf_get_hash_recalc)(void *ctx) =
601 (void *) BPF_FUNC_get_hash_recalc;
602 static u64 (*bpf_get_current_task)(void) =
603 (void *) BPF_FUNC_get_current_task;
604 static int (*bpf_probe_write_user)(void *dst, void *src, u32 size) =
605 (void *) BPF_FUNC_probe_write_user;
606 static int (*bpf_skb_change_tail)(void *ctx, u32 new_len, u64 flags) =
607 (void *) BPF_FUNC_skb_change_tail;
608 static int (*bpf_skb_pull_data)(void *ctx, u32 len) =
609 (void *) BPF_FUNC_skb_pull_data;
610 static int (*bpf_csum_update)(void *ctx, u16 csum) =
611 (void *) BPF_FUNC_csum_update;
612 static int (*bpf_set_hash_invalid)(void *ctx) =
613 (void *) BPF_FUNC_set_hash_invalid;
614 static int (*bpf_get_numa_node_id)(void) =
615 (void *) BPF_FUNC_get_numa_node_id;
616 static int (*bpf_skb_change_head)(void *ctx, u32 len, u64 flags) =
617 (void *) BPF_FUNC_skb_change_head;
618 static int (*bpf_xdp_adjust_head)(void *ctx, int offset) =
619 (void *) BPF_FUNC_xdp_adjust_head;
620 static int (*bpf_override_return)(void *pt_regs, unsigned long rc) =
621 (void *) BPF_FUNC_override_return;
622 static int (*bpf_sock_ops_cb_flags_set)(void *skops, int flags) =
623 (void *) BPF_FUNC_sock_ops_cb_flags_set;
624 static int (*bpf_msg_redirect_map)(void *msg, void *map, u32 key, u64 flags) =
625 (void *) BPF_FUNC_msg_redirect_map;
626 static int (*bpf_msg_apply_bytes)(void *msg, u32 bytes) =
627 (void *) BPF_FUNC_msg_apply_bytes;
628 static int (*bpf_msg_cork_bytes)(void *msg, u32 bytes) =
629 (void *) BPF_FUNC_msg_cork_bytes;
630 static int (*bpf_msg_pull_data)(void *msg, u32 start, u32 end, u64 flags) =
631 (void *) BPF_FUNC_msg_pull_data;
632 static int (*bpf_bind)(void *ctx, void *addr, int addr_len) =
633 (void *) BPF_FUNC_bind;
634 static int (*bpf_xdp_adjust_tail)(void *ctx, int offset) =
635 (void *) BPF_FUNC_xdp_adjust_tail;
636 static int (*bpf_skb_get_xfrm_state)(void *ctx, u32 index, void *xfrm_state, u32 size, u64 flags) =
637 (void *) BPF_FUNC_skb_get_xfrm_state;
638 static int (*bpf_get_stack)(void *ctx, void *buf, u32 size, u64 flags) =
639 (void *) BPF_FUNC_get_stack;
640 static int (*bpf_skb_load_bytes_relative)(void *ctx, u32 offset, void *to, u32 len, u32 start_header) =
641 (void *) BPF_FUNC_skb_load_bytes_relative;
642 static int (*bpf_fib_lookup)(void *ctx, void *params, int plen, u32 flags) =
643 (void *) BPF_FUNC_fib_lookup;
644 static int (*bpf_sock_hash_update)(void *ctx, void *map, void *key, u64 flags) =
645 (void *) BPF_FUNC_sock_hash_update;
646 static int (*bpf_msg_redirect_hash)(void *ctx, void *map, void *key, u64 flags) =
647 (void *) BPF_FUNC_msg_redirect_hash;
648 static int (*bpf_sk_redirect_hash)(void *ctx, void *map, void *key, u64 flags) =
649 (void *) BPF_FUNC_sk_redirect_hash;
650 static int (*bpf_lwt_push_encap)(void *skb, u32 type, void *hdr, u32 len) =
651 (void *) BPF_FUNC_lwt_push_encap;
652 static int (*bpf_lwt_seg6_store_bytes)(void *ctx, u32 offset, const void *from, u32 len) =
653 (void *) BPF_FUNC_lwt_seg6_store_bytes;
654 static int (*bpf_lwt_seg6_adjust_srh)(void *ctx, u32 offset, s32 delta) =
655 (void *) BPF_FUNC_lwt_seg6_adjust_srh;
656 static int (*bpf_lwt_seg6_action)(void *ctx, u32 action, void *param, u32 param_len) =
657 (void *) BPF_FUNC_lwt_seg6_action;
658 static int (*bpf_rc_keydown)(void *ctx, u32 protocol, u64 scancode, u32 toggle) =
659 (void *) BPF_FUNC_rc_keydown;
660 static int (*bpf_rc_repeat)(void *ctx) =
661 (void *) BPF_FUNC_rc_repeat;
662 static u64 (*bpf_skb_cgroup_id)(void *skb) =
663 (void *) BPF_FUNC_skb_cgroup_id;
664 static u64 (*bpf_get_current_cgroup_id)(void) =
665 (void *) BPF_FUNC_get_current_cgroup_id;
666 static u64 (*bpf_skb_ancestor_cgroup_id)(void *skb, int ancestor_level) =
667 (void *) BPF_FUNC_skb_ancestor_cgroup_id;
668 static void * (*bpf_get_local_storage)(void *map, u64 flags) =
669 (void *) BPF_FUNC_get_local_storage;
670 static int (*bpf_sk_select_reuseport)(void *reuse, void *map, void *key, u64 flags) =
671 (void *) BPF_FUNC_sk_select_reuseport;
672 static struct bpf_sock *(*bpf_sk_lookup_tcp)(void *ctx,
673 struct bpf_sock_tuple *tuple,
674 int size, unsigned int netns_id,
675 unsigned long long flags) =
676 (void *) BPF_FUNC_sk_lookup_tcp;
677 static struct bpf_sock *(*bpf_sk_lookup_udp)(void *ctx,
678 struct bpf_sock_tuple *tuple,
679 int size, unsigned int netns_id,
680 unsigned long long flags) =
681 (void *) BPF_FUNC_sk_lookup_udp;
682 static int (*bpf_sk_release)(void *sk) =
683 (void *) BPF_FUNC_sk_release;
684 static int (*bpf_map_push_elem)(void *map, const void *value, u64 flags) =
685 (void *) BPF_FUNC_map_push_elem;
686 static int (*bpf_map_pop_elem)(void *map, void *value) =
687 (void *) BPF_FUNC_map_pop_elem;
688 static int (*bpf_map_peek_elem)(void *map, void *value) =
689 (void *) BPF_FUNC_map_peek_elem;
690 static int (*bpf_msg_push_data)(void *skb, u32 start, u32 len, u64 flags) =
691 (void *) BPF_FUNC_msg_push_data;
692 static int (*bpf_msg_pop_data)(void *msg, u32 start, u32 pop, u64 flags) =
693 (void *) BPF_FUNC_msg_pop_data;
694 static int (*bpf_rc_pointer_rel)(void *ctx, s32 rel_x, s32 rel_y) =
695 (void *) BPF_FUNC_rc_pointer_rel;
696 static void (*bpf_spin_lock)(struct bpf_spin_lock *lock) =
697 (void *) BPF_FUNC_spin_lock;
698 static void (*bpf_spin_unlock)(struct bpf_spin_lock *lock) =
699 (void *) BPF_FUNC_spin_unlock;
700 static struct bpf_sock *(*bpf_sk_fullsock)(struct bpf_sock *sk) =
701 (void *) BPF_FUNC_sk_fullsock;
702 static struct bpf_tcp_sock *(*bpf_tcp_sock)(struct bpf_sock *sk) =
703 (void *) BPF_FUNC_tcp_sock;
704 static int (*bpf_skb_ecn_set_ce)(void *ctx) =
705 (void *) BPF_FUNC_skb_ecn_set_ce;
706 static struct bpf_sock *(*bpf_get_listener_sock)(struct bpf_sock *sk) =
707 (void *) BPF_FUNC_get_listener_sock;
708 static void *(*bpf_sk_storage_get)(void *map, void *sk,
709 void *value, __u64 flags) =
710 (void *) BPF_FUNC_sk_storage_get;
711 static int (*bpf_sk_storage_delete)(void *map, void *sk) =
712 (void *)BPF_FUNC_sk_storage_delete;
713 static int (*bpf_send_signal)(unsigned sig) = (void *)BPF_FUNC_send_signal;
714 static long long (*bpf_tcp_gen_syncookie)(void *sk, void *ip,
715 int ip_len, void *tcp, int tcp_len) =
716 (void *) BPF_FUNC_tcp_gen_syncookie;
717 static int (*bpf_skb_output)(void *ctx, void *map, __u64 flags, void *data,
719 (void *)BPF_FUNC_skb_output;
721 static int (*bpf_probe_read_user)(void *dst, __u32 size,
722 const void *unsafe_ptr) =
723 (void *)BPF_FUNC_probe_read_user;
724 static int (*bpf_probe_read_kernel)(void *dst, __u32 size,
725 const void *unsafe_ptr) =
726 (void *)BPF_FUNC_probe_read_kernel;
727 static int (*bpf_probe_read_user_str)(void *dst, __u32 size,
728 const void *unsafe_ptr) =
729 (void *)BPF_FUNC_probe_read_user_str;
730 static int (*bpf_probe_read_kernel_str)(void *dst, __u32 size,
731 const void *unsafe_ptr) =
732 (void *)BPF_FUNC_probe_read_kernel_str;
733 static int (*bpf_tcp_send_ack)(void *tp, __u32 rcv_nxt) =
734 (void *)BPF_FUNC_tcp_send_ack;
735 static int (*bpf_send_signal_thread)(__u32 sig) =
736 (void *)BPF_FUNC_send_signal_thread;
737 static __u64 (*bpf_jiffies64)(void) = (void *)BPF_FUNC_jiffies64;
739 struct bpf_perf_event_data;
740 static int (*bpf_read_branch_records)(struct bpf_perf_event_data *ctx, void *buf,
741 __u32 size, __u64 flags) =
742 (void *)BPF_FUNC_read_branch_records;
743 static int (*bpf_get_ns_current_pid_tgid)(__u64 dev, __u64 ino,
744 struct bpf_pidns_info *nsdata,
746 (void *)BPF_FUNC_get_ns_current_pid_tgid;
749 static int (*bpf_xdp_output)(void *ctx, struct bpf_map *map, __u64 flags,
750 void *data, __u64 size) =
751 (void *)BPF_FUNC_xdp_output;
752 static __u64 (*bpf_get_netns_cookie)(void *ctx) = (void *)BPF_FUNC_get_netns_cookie;
753 static __u64 (*bpf_get_current_ancestor_cgroup_id)(int ancestor_level) =
754 (void *)BPF_FUNC_get_current_ancestor_cgroup_id;
757 static int (*bpf_sk_assign)(void *skb, void *sk, __u64 flags) =
758 (void *)BPF_FUNC_sk_assign;
760 static __u64 (*bpf_ktime_get_boot_ns)(void) = (void *)BPF_FUNC_ktime_get_boot_ns;
763 static int (*bpf_seq_printf)(struct seq_file *m, const char *fmt, __u32 fmt_size,
764 const void *data, __u32 data_len) =
765 (void *)BPF_FUNC_seq_printf;
766 static int (*bpf_seq_write)(struct seq_file *m, const void *data, __u32 len) =
767 (void *)BPF_FUNC_seq_write;
769 static __u64 (*bpf_sk_cgroup_id)(void *sk) = (void *)BPF_FUNC_sk_cgroup_id;
770 static __u64 (*bpf_sk_ancestor_cgroup_id)(void *sk, int ancestor_level) =
771 (void *)BPF_FUNC_sk_ancestor_cgroup_id;
773 static int (*bpf_ringbuf_output)(void *ringbuf, void *data, __u64 size, __u64 flags) =
774 (void *)BPF_FUNC_ringbuf_output;
775 static void *(*bpf_ringbuf_reserve)(void *ringbuf, __u64 size, __u64 flags) =
776 (void *)BPF_FUNC_ringbuf_reserve;
777 static void (*bpf_ringbuf_submit)(void *data, __u64 flags) =
778 (void *)BPF_FUNC_ringbuf_submit;
779 static void (*bpf_ringbuf_discard)(void *data, __u64 flags) =
780 (void *)BPF_FUNC_ringbuf_discard;
781 static __u64 (*bpf_ringbuf_query)(void *ringbuf, __u64 flags) =
782 (void *)BPF_FUNC_ringbuf_query;
784 static int (*bpf_csum_level)(struct __sk_buff *skb, __u64 level) =
785 (void *)BPF_FUNC_csum_level;
789 struct tcp_timewait_sock;
790 struct tcp_request_sock;
792 static struct tcp6_sock *(*bpf_skc_to_tcp6_sock)(void *sk) =
793 (void *)BPF_FUNC_skc_to_tcp6_sock;
794 static struct tcp_sock *(*bpf_skc_to_tcp_sock)(void *sk) =
795 (void *)BPF_FUNC_skc_to_tcp_sock;
796 static struct tcp_timewait_sock *(*bpf_skc_to_tcp_timewait_sock)(void *sk) =
797 (void *)BPF_FUNC_skc_to_tcp_timewait_sock;
798 static struct tcp_request_sock *(*bpf_skc_to_tcp_request_sock)(void *sk) =
799 (void *)BPF_FUNC_skc_to_tcp_request_sock;
800 static struct udp6_sock *(*bpf_skc_to_udp6_sock)(void *sk) =
801 (void *)BPF_FUNC_skc_to_udp6_sock;
804 static long (*bpf_get_task_stack)(struct task_struct *task, void *buf,
805 __u32 size, __u64 flags) =
806 (void *)BPF_FUNC_get_task_stack;
809 static long (*bpf_load_hdr_opt)(struct bpf_sock_ops *skops, void *searchby_res,
810 u32 len, u64 flags) =
811 (void *)BPF_FUNC_load_hdr_opt;
812 static long (*bpf_store_hdr_opt)(struct bpf_sock_ops *skops, const void *from,
813 u32 len, u64 flags) =
814 (void *)BPF_FUNC_store_hdr_opt;
815 static long (*bpf_reserve_hdr_opt)(struct bpf_sock_ops *skops, u32 len,
817 (void *)BPF_FUNC_reserve_hdr_opt;
818 static void *(*bpf_inode_storage_get)(struct bpf_map *map, void *inode,
819 void *value, u64 flags) =
820 (void *)BPF_FUNC_inode_storage_get;
821 static int (*bpf_inode_storage_delete)(struct bpf_map *map, void *inode) =
822 (void *)BPF_FUNC_inode_storage_delete;
824 static long (*bpf_d_path)(struct path *path, char *buf, u32 sz) =
825 (void *)BPF_FUNC_d_path;
826 static long (*bpf_copy_from_user)(void *dst, u32 size, const void *user_ptr) =
827 (void *)BPF_FUNC_copy_from_user;
829 static long (*bpf_snprintf_btf)(char *str, u32 str_size, struct btf_ptr *ptr,
830 u32 btf_ptr_size, u64 flags) =
831 (void *)BPF_FUNC_snprintf_btf;
832 static long (*bpf_seq_printf_btf)(struct seq_file *m, struct btf_ptr *ptr,
833 u32 ptr_size, u64 flags) =
834 (void *)BPF_FUNC_seq_printf_btf;
835 static u64 (*bpf_skb_cgroup_classid)(struct sk_buff *skb) =
836 (void *)BPF_FUNC_skb_cgroup_classid;
837 static long (*bpf_redirect_neigh)(u32 ifindex, struct bpf_redir_neigh *params,
839 (void *)BPF_FUNC_redirect_neigh;
840 static void * (*bpf_per_cpu_ptr)(const void *percpu_ptr, u32 cpu) =
841 (void *)BPF_FUNC_per_cpu_ptr;
842 static void * (*bpf_this_cpu_ptr)(const void *percpu_ptr) =
843 (void *)BPF_FUNC_this_cpu_ptr;
844 static long (*bpf_redirect_peer)(u32 ifindex, u64 flags) = (void *)BPF_FUNC_redirect_peer;
846 static void *(*bpf_task_storage_get)(void *map, struct task_struct *task,
847 void *value, __u64 flags) =
848 (void *)BPF_FUNC_task_storage_get;
849 static long (*bpf_task_storage_delete)(void *map, struct task_struct *task) =
850 (void *)BPF_FUNC_task_storage_delete;
851 static struct task_struct *(*bpf_get_current_task_btf)(void) =
852 (void *)BPF_FUNC_get_current_task_btf;
854 static long (*bpf_bprm_opts_set)(struct linux_binprm *bprm, __u64 flags) =
855 (void *)BPF_FUNC_bprm_opts_set;
856 static __u64 (*bpf_ktime_get_coarse_ns)(void) = (void *)BPF_FUNC_ktime_get_coarse_ns;
858 static long (*bpf_ima_inode_hash)(struct inode *inode, void *dst, __u32 size) =
859 (void *)BPF_FUNC_ima_inode_hash;
861 static struct socket *(*bpf_sock_from_file)(struct file *file) =
862 (void *)BPF_FUNC_sock_from_file;
863 static long (*bpf_check_mtu)(void *ctx, __u32 ifindex, __u32 *mtu_len,
864 __s32 len_diff, __u64 flags) =
865 (void *)BPF_FUNC_check_mtu;
866 static long (*bpf_for_each_map_elem)(void *map, void *callback_fn,
867 void *callback_ctx, __u64 flags) =
868 (void *)BPF_FUNC_for_each_map_elem;
869 static long (*bpf_snprintf)(char *str, __u32 str_size, const char *fmt,
870 __u64 *data, __u32 data_len) =
871 (void *)BPF_FUNC_snprintf;
873 /* llvm builtin functions that eBPF C program may use to
874 * emit BPF_LD_ABS and BPF_LD_IND instructions
876 unsigned long long load_byte(void *skb,
877 unsigned long long off) asm("llvm.bpf.load.byte");
878 unsigned long long load_half(void *skb,
879 unsigned long long off) asm("llvm.bpf.load.half");
880 unsigned long long load_word(void *skb,
881 unsigned long long off) asm("llvm.bpf.load.word");
883 /* a helper structure used by eBPF C program
884 * to describe map attributes to elf_bpf loader
888 unsigned int key_size;
889 unsigned int value_size;
890 unsigned int max_entries;
893 static int (*bpf_skb_store_bytes)(void *ctx, unsigned long long off, void *from,
894 unsigned long long len, unsigned long long flags) =
895 (void *) BPF_FUNC_skb_store_bytes;
896 static int (*bpf_l3_csum_replace)(void *ctx, unsigned long long off, unsigned long long from,
897 unsigned long long to, unsigned long long flags) =
898 (void *) BPF_FUNC_l3_csum_replace;
899 static int (*bpf_l4_csum_replace)(void *ctx, unsigned long long off, unsigned long long from,
900 unsigned long long to, unsigned long long flags) =
901 (void *) BPF_FUNC_l4_csum_replace;
903 static inline __attribute__((always_inline))
904 u16 bpf_ntohs(u16 val) {
905 /* will be recognized by gcc into rotate insn and eventually rolw 8 */
906 return (val << 8) | (val >> 8);
909 static inline __attribute__((always_inline))
910 u32 bpf_ntohl(u32 val) {
911 /* gcc will use bswapsi2 insn */
912 return __builtin_bswap32(val);
915 static inline __attribute__((always_inline))
916 u64 bpf_ntohll(u64 val) {
917 /* gcc will use bswapdi2 insn */
918 return __builtin_bswap64(val);
921 static inline __attribute__((always_inline))
922 unsigned __int128 bpf_ntoh128(unsigned __int128 val) {
923 return (((unsigned __int128)bpf_ntohll(val) << 64) | (u64)bpf_ntohll(val >> 64));
926 static inline __attribute__((always_inline))
927 u16 bpf_htons(u16 val) {
928 return bpf_ntohs(val);
931 static inline __attribute__((always_inline))
932 u32 bpf_htonl(u32 val) {
933 return bpf_ntohl(val);
936 static inline __attribute__((always_inline))
937 u64 bpf_htonll(u64 val) {
938 return bpf_ntohll(val);
941 static inline __attribute__((always_inline))
942 unsigned __int128 bpf_hton128(unsigned __int128 val) {
943 return bpf_ntoh128(val);
946 static inline __attribute__((always_inline))
947 u64 load_dword(void *skb, u64 off) {
948 return ((u64)load_word(skb, off) << 32) | load_word(skb, off + 4);
951 void bpf_store_byte(void *skb, u64 off, u64 val) asm("llvm.bpf.store.byte");
952 void bpf_store_half(void *skb, u64 off, u64 val) asm("llvm.bpf.store.half");
953 void bpf_store_word(void *skb, u64 off, u64 val) asm("llvm.bpf.store.word");
954 u64 bpf_pseudo_fd(u64, u64) asm("llvm.bpf.pseudo");
956 static inline void __attribute__((always_inline))
957 bpf_store_dword(void *skb, u64 off, u64 val) {
958 bpf_store_word(skb, off, (u32)val);
959 bpf_store_word(skb, off + 4, val >> 32);
962 #define MASK(_n) ((_n) < 64 ? (1ull << (_n)) - 1 : ((u64)-1LL))
963 #define MASK128(_n) ((_n) < 128 ? ((unsigned __int128)1 << (_n)) - 1 : ((unsigned __int128)-1))
965 static inline __attribute__((always_inline))
966 unsigned int bpf_log2(unsigned int v)
971 r = (v > 0xFFFF) << 4; v >>= r;
972 shift = (v > 0xFF) << 3; v >>= shift; r |= shift;
973 shift = (v > 0xF) << 2; v >>= shift; r |= shift;
974 shift = (v > 0x3) << 1; v >>= shift; r |= shift;
979 static inline __attribute__((always_inline))
980 unsigned int bpf_log2l(unsigned long v)
982 unsigned int hi = v >> 32;
984 return bpf_log2(hi) + 32 + 1;
986 return bpf_log2(v) + 1;
991 static inline __attribute__((always_inline))
993 u64 bpf_dext_pkt(void *pkt, u64 off, u64 bofs, u64 bsz) {
994 if (bofs == 0 && bsz == 8) {
995 return load_byte(pkt, off);
996 } else if (bofs + bsz <= 8) {
997 return load_byte(pkt, off) >> (8 - (bofs + bsz)) & MASK(bsz);
998 } else if (bofs == 0 && bsz == 16) {
999 return load_half(pkt, off);
1000 } else if (bofs + bsz <= 16) {
1001 return load_half(pkt, off) >> (16 - (bofs + bsz)) & MASK(bsz);
1002 } else if (bofs == 0 && bsz == 32) {
1003 return load_word(pkt, off);
1004 } else if (bofs + bsz <= 32) {
1005 return load_word(pkt, off) >> (32 - (bofs + bsz)) & MASK(bsz);
1006 } else if (bofs == 0 && bsz == 64) {
1007 return load_dword(pkt, off);
1008 } else if (bofs + bsz <= 64) {
1009 return load_dword(pkt, off) >> (64 - (bofs + bsz)) & MASK(bsz);
1014 static inline __attribute__((always_inline))
1016 void bpf_dins_pkt(void *pkt, u64 off, u64 bofs, u64 bsz, u64 val) {
1017 // The load_xxx function does a bswap before returning the short/word/dword,
1018 // so the value in register will always be host endian. However, the bytes
1019 // written back need to be in network order.
1020 if (bofs == 0 && bsz == 8) {
1021 bpf_skb_store_bytes(pkt, off, &val, 1, 0);
1022 } else if (bofs + bsz <= 8) {
1023 u8 v = load_byte(pkt, off);
1024 v &= ~(MASK(bsz) << (8 - (bofs + bsz)));
1025 v |= ((val & MASK(bsz)) << (8 - (bofs + bsz)));
1026 bpf_skb_store_bytes(pkt, off, &v, 1, 0);
1027 } else if (bofs == 0 && bsz == 16) {
1028 u16 v = bpf_htons(val);
1029 bpf_skb_store_bytes(pkt, off, &v, 2, 0);
1030 } else if (bofs + bsz <= 16) {
1031 u16 v = load_half(pkt, off);
1032 v &= ~(MASK(bsz) << (16 - (bofs + bsz)));
1033 v |= ((val & MASK(bsz)) << (16 - (bofs + bsz)));
1035 bpf_skb_store_bytes(pkt, off, &v, 2, 0);
1036 } else if (bofs == 0 && bsz == 32) {
1037 u32 v = bpf_htonl(val);
1038 bpf_skb_store_bytes(pkt, off, &v, 4, 0);
1039 } else if (bofs + bsz <= 32) {
1040 u32 v = load_word(pkt, off);
1041 v &= ~(MASK(bsz) << (32 - (bofs + bsz)));
1042 v |= ((val & MASK(bsz)) << (32 - (bofs + bsz)));
1044 bpf_skb_store_bytes(pkt, off, &v, 4, 0);
1045 } else if (bofs == 0 && bsz == 64) {
1046 u64 v = bpf_htonll(val);
1047 bpf_skb_store_bytes(pkt, off, &v, 8, 0);
1048 } else if (bofs + bsz <= 64) {
1049 u64 v = load_dword(pkt, off);
1050 v &= ~(MASK(bsz) << (64 - (bofs + bsz)));
1051 v |= ((val & MASK(bsz)) << (64 - (bofs + bsz)));
1053 bpf_skb_store_bytes(pkt, off, &v, 8, 0);
1057 static inline __attribute__((always_inline))
1059 void * bpf_map_lookup_elem_(uintptr_t map, void *key) {
1060 return bpf_map_lookup_elem((void *)map, key);
1063 static inline __attribute__((always_inline))
1065 int bpf_map_update_elem_(uintptr_t map, void *key, void *value, u64 flags) {
1066 return bpf_map_update_elem((void *)map, key, value, flags);
1069 static inline __attribute__((always_inline))
1071 int bpf_map_delete_elem_(uintptr_t map, void *key) {
1072 return bpf_map_delete_elem((void *)map, key);
1075 static inline __attribute__((always_inline))
1077 int bpf_l3_csum_replace_(void *ctx, u64 off, u64 from, u64 to, u64 flags) {
1078 switch (flags & 0xf) {
1080 return bpf_l3_csum_replace(ctx, off, bpf_htons(from), bpf_htons(to), flags);
1082 return bpf_l3_csum_replace(ctx, off, bpf_htonl(from), bpf_htonl(to), flags);
1084 return bpf_l3_csum_replace(ctx, off, bpf_htonll(from), bpf_htonll(to), flags);
1088 return bpf_l3_csum_replace(ctx, off, from, to, flags);
1091 static inline __attribute__((always_inline))
1093 int bpf_l4_csum_replace_(void *ctx, u64 off, u64 from, u64 to, u64 flags) {
1094 switch (flags & 0xf) {
1096 return bpf_l4_csum_replace(ctx, off, bpf_htons(from), bpf_htons(to), flags);
1098 return bpf_l4_csum_replace(ctx, off, bpf_htonl(from), bpf_htonl(to), flags);
1100 return bpf_l4_csum_replace(ctx, off, bpf_htonll(from), bpf_htonll(to), flags);
1104 return bpf_l4_csum_replace(ctx, off, from, to, flags);
1107 int incr_cksum_l3(void *off, u64 oldval, u64 newval) asm("llvm.bpf.extra");
1108 int incr_cksum_l4(void *off, u64 oldval, u64 newval, u64 flags) asm("llvm.bpf.extra");
1109 int bpf_num_cpus() asm("llvm.bpf.extra");
1112 int bpf_usdt_readarg(int argc, struct pt_regs *ctx, void *arg) asm("llvm.bpf.extra");
1113 int bpf_usdt_readarg_p(int argc, struct pt_regs *ctx, void *buf, u64 len) asm("llvm.bpf.extra");
1115 /* Scan the ARCH passed in from ARCH env variable (see kbuild_helper.cc) */
1116 #if defined(__TARGET_ARCH_x86)
1117 #define bpf_target_x86
1118 #define bpf_target_defined
1119 #elif defined(__TARGET_ARCH_s390x)
1120 #define bpf_target_s390x
1121 #define bpf_target_defined
1122 #elif defined(__TARGET_ARCH_arm64)
1123 #define bpf_target_arm64
1124 #define bpf_target_defined
1125 #elif defined(__TARGET_ARCH_powerpc)
1126 #define bpf_target_powerpc
1127 #define bpf_target_defined
1128 #elif defined(__TARGET_ARCH_mips)
1129 #define bpf_target_mips
1130 #define bpf_target_defined
1132 #undef bpf_target_defined
1135 /* Fall back to what the compiler says */
1136 #ifndef bpf_target_defined
1137 #if defined(__x86_64__)
1138 #define bpf_target_x86
1139 #elif defined(__s390x__)
1140 #define bpf_target_s390x
1141 #elif defined(__aarch64__)
1142 #define bpf_target_arm64
1143 #elif defined(__powerpc__)
1144 #define bpf_target_powerpc
1145 #elif defined(__mips__)
1146 #define bpf_target_mips
1150 #if defined(bpf_target_powerpc)
1151 #define PT_REGS_PARM1(ctx) ((ctx)->gpr[3])
1152 #define PT_REGS_PARM2(ctx) ((ctx)->gpr[4])
1153 #define PT_REGS_PARM3(ctx) ((ctx)->gpr[5])
1154 #define PT_REGS_PARM4(ctx) ((ctx)->gpr[6])
1155 #define PT_REGS_PARM5(ctx) ((ctx)->gpr[7])
1156 #define PT_REGS_PARM6(ctx) ((ctx)->gpr[8])
1157 #define PT_REGS_RC(ctx) ((ctx)->gpr[3])
1158 #define PT_REGS_IP(ctx) ((ctx)->nip)
1159 #define PT_REGS_SP(ctx) ((ctx)->gpr[1])
1160 #elif defined(bpf_target_s390x)
1161 #define PT_REGS_PARM1(x) ((x)->gprs[2])
1162 #define PT_REGS_PARM2(x) ((x)->gprs[3])
1163 #define PT_REGS_PARM3(x) ((x)->gprs[4])
1164 #define PT_REGS_PARM4(x) ((x)->gprs[5])
1165 #define PT_REGS_PARM5(x) ((x)->gprs[6])
1166 #define PT_REGS_RET(x) ((x)->gprs[14])
1167 #define PT_REGS_FP(x) ((x)->gprs[11]) /* Works only with CONFIG_FRAME_POINTER */
1168 #define PT_REGS_RC(x) ((x)->gprs[2])
1169 #define PT_REGS_SP(x) ((x)->gprs[15])
1170 #define PT_REGS_IP(x) ((x)->psw.addr)
1171 #elif defined(bpf_target_x86)
1172 #define PT_REGS_PARM1(ctx) ((ctx)->di)
1173 #define PT_REGS_PARM2(ctx) ((ctx)->si)
1174 #define PT_REGS_PARM3(ctx) ((ctx)->dx)
1175 #define PT_REGS_PARM4(ctx) ((ctx)->cx)
1176 #define PT_REGS_PARM5(ctx) ((ctx)->r8)
1177 #define PT_REGS_PARM6(ctx) ((ctx)->r9)
1178 #define PT_REGS_RET(ctx) ((ctx)->sp)
1179 #define PT_REGS_FP(ctx) ((ctx)->bp) /* Works only with CONFIG_FRAME_POINTER */
1180 #define PT_REGS_RC(ctx) ((ctx)->ax)
1181 #define PT_REGS_IP(ctx) ((ctx)->ip)
1182 #define PT_REGS_SP(ctx) ((ctx)->sp)
1183 #elif defined(bpf_target_arm64)
1184 #define PT_REGS_PARM1(x) ((x)->regs[0])
1185 #define PT_REGS_PARM2(x) ((x)->regs[1])
1186 #define PT_REGS_PARM3(x) ((x)->regs[2])
1187 #define PT_REGS_PARM4(x) ((x)->regs[3])
1188 #define PT_REGS_PARM5(x) ((x)->regs[4])
1189 #define PT_REGS_PARM6(x) ((x)->regs[5])
1190 #define PT_REGS_RET(x) ((x)->regs[30])
1191 #define PT_REGS_FP(x) ((x)->regs[29]) /* Works only with CONFIG_FRAME_POINTER */
1192 #define PT_REGS_RC(x) ((x)->regs[0])
1193 #define PT_REGS_SP(x) ((x)->sp)
1194 #define PT_REGS_IP(x) ((x)->pc)
1195 #elif defined(bpf_target_mips)
1196 #define PT_REGS_PARM1(x) ((x)->regs[4])
1197 #define PT_REGS_PARM2(x) ((x)->regs[5])
1198 #define PT_REGS_PARM3(x) ((x)->regs[6])
1199 #define PT_REGS_PARM4(x) ((x)->regs[7])
1200 #define PT_REGS_PARM5(x) ((x)->regs[8])
1201 #define PT_REGS_PARM6(x) ((x)->regs[9])
1202 #define PT_REGS_RET(x) ((x)->regs[31])
1203 #define PT_REGS_FP(x) ((x)->regs[30]) /* Works only with CONFIG_FRAME_POINTER */
1204 #define PT_REGS_RC(x) ((x)->regs[2])
1205 #define PT_REGS_SP(x) ((x)->regs[29])
1206 #define PT_REGS_IP(x) ((x)->cp0_epc)
1208 #error "bcc does not support this platform yet"
1211 #if defined(CONFIG_ARCH_HAS_SYSCALL_WRAPPER) && !defined(__s390x__)
1212 #define PT_REGS_SYSCALL_CTX(ctx) ((struct pt_regs *)PT_REGS_PARM1(ctx))
1214 #define PT_REGS_SYSCALL_CTX(ctx) (ctx)
1216 /* Helpers for syscall params. Pass in a ctx returned from PT_REGS_SYSCALL_CTX.
1218 #define PT_REGS_PARM1_SYSCALL(ctx) PT_REGS_PARM1(ctx)
1219 #define PT_REGS_PARM2_SYSCALL(ctx) PT_REGS_PARM2(ctx)
1220 #define PT_REGS_PARM3_SYSCALL(ctx) PT_REGS_PARM3(ctx)
1221 #if defined(bpf_target_x86)
1222 #define PT_REGS_PARM4_SYSCALL(ctx) ((ctx)->r10) /* for syscall only */
1224 #define PT_REGS_PARM4_SYSCALL(ctx) PT_REGS_PARM4(ctx)
1226 #define PT_REGS_PARM5_SYSCALL(ctx) PT_REGS_PARM5(ctx)
1227 #ifdef PT_REGS_PARM6
1228 #define PT_REGS_PARM6_SYSCALL(ctx) PT_REGS_PARM6(ctx)
1231 #define lock_xadd(ptr, val) ((void)__sync_fetch_and_add(ptr, val))
1233 #define TRACEPOINT_PROBE(category, event) \
1234 int tracepoint__##category##__##event(struct tracepoint__##category##__##event *args)
1236 #define RAW_TRACEPOINT_PROBE(event) \
1237 int raw_tracepoint__##event(struct bpf_raw_tracepoint_args *ctx)
1239 /* BPF_PROG macro allows to define trampoline function,
1240 * borrowed from kernel bpf selftest code.
1242 #define ___bpf_concat(a, b) a ## b
1243 #define ___bpf_apply(fn, n) ___bpf_concat(fn, n)
1244 #define ___bpf_nth(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c, N, ...) N
1245 #define ___bpf_narg(...) \
1246 ___bpf_nth(_, ##__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
1248 #define ___bpf_ctx_cast0() ctx
1249 #define ___bpf_ctx_cast1(x) ___bpf_ctx_cast0(), (void *)ctx[0]
1250 #define ___bpf_ctx_cast2(x, args...) ___bpf_ctx_cast1(args), (void *)ctx[1]
1251 #define ___bpf_ctx_cast3(x, args...) ___bpf_ctx_cast2(args), (void *)ctx[2]
1252 #define ___bpf_ctx_cast4(x, args...) ___bpf_ctx_cast3(args), (void *)ctx[3]
1253 #define ___bpf_ctx_cast5(x, args...) ___bpf_ctx_cast4(args), (void *)ctx[4]
1254 #define ___bpf_ctx_cast6(x, args...) ___bpf_ctx_cast5(args), (void *)ctx[5]
1255 #define ___bpf_ctx_cast7(x, args...) ___bpf_ctx_cast6(args), (void *)ctx[6]
1256 #define ___bpf_ctx_cast8(x, args...) ___bpf_ctx_cast7(args), (void *)ctx[7]
1257 #define ___bpf_ctx_cast9(x, args...) ___bpf_ctx_cast8(args), (void *)ctx[8]
1258 #define ___bpf_ctx_cast10(x, args...) ___bpf_ctx_cast9(args), (void *)ctx[9]
1259 #define ___bpf_ctx_cast11(x, args...) ___bpf_ctx_cast10(args), (void *)ctx[10]
1260 #define ___bpf_ctx_cast12(x, args...) ___bpf_ctx_cast11(args), (void *)ctx[11]
1261 #define ___bpf_ctx_cast(args...) \
1262 ___bpf_apply(___bpf_ctx_cast, ___bpf_narg(args))(args)
1264 #define BPF_PROG(name, args...) \
1265 int name(unsigned long long *ctx); \
1266 __attribute__((always_inline)) \
1267 static int ____##name(unsigned long long *ctx, ##args); \
1268 int name(unsigned long long *ctx) \
1272 _Pragma("GCC diagnostic push") \
1273 _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \
1274 __ret = ____##name(___bpf_ctx_cast(args)); \
1275 _Pragma("GCC diagnostic pop") \
1278 static int ____##name(unsigned long long *ctx, ##args)
1280 #define KFUNC_PROBE(event, args...) \
1281 BPF_PROG(kfunc__ ## event, args)
1283 #define KRETFUNC_PROBE(event, args...) \
1284 BPF_PROG(kretfunc__ ## event, args)
1286 #define KMOD_RET(event, args...) \
1287 BPF_PROG(kmod_ret__ ## event, args)
1289 #define LSM_PROBE(event, args...) \
1290 BPF_PROG(lsm__ ## event, args)
1292 #define BPF_ITER(target) \
1293 int bpf_iter__ ## target (struct bpf_iter__ ## target *ctx)
1295 #define TP_DATA_LOC_READ_CONST(dst, field, length) \
1297 unsigned short __offset = args->data_loc_##field & 0xFFFF; \
1298 bpf_probe_read((void *)dst, length, (char *)args + __offset); \
1301 #define TP_DATA_LOC_READ(dst, field) \
1303 unsigned short __offset = args->data_loc_##field & 0xFFFF; \
1304 unsigned short __length = args->data_loc_##field >> 16; \
1305 bpf_probe_read((void *)dst, __length, (char *)args + __offset); \