1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 #ifndef __BPF_HELPERS__
3 #define __BPF_HELPERS__
6 * Note that bpf programs need to include either
7 * vmlinux.h (auto-generated from BTF) or linux/types.h
8 * in advance since bpf_helper_defs.h uses such types
11 #include "bpf_helper_defs.h"
13 #define __uint(name, val) int (*name)[val]
14 #define __type(name, val) typeof(val) *name
15 #define __array(name, val) typeof(val) *name[]
17 /* Helper macro to print out debug messages */
18 #define bpf_printk(fmt, ...) \
20 char ____fmt[] = fmt; \
21 bpf_trace_printk(____fmt, sizeof(____fmt), \
26 * Helper macro to place programs, maps, license in
27 * different sections in elf_bpf file. Section names
28 * are interpreted by elf_bpf loader
30 #define SEC(NAME) __attribute__((section(NAME), used))
32 #ifndef __always_inline
33 #define __always_inline __attribute__((always_inline))
36 #define __weak __attribute__((weak))
40 * Helper macro to manipulate data structures
43 #define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
46 #define container_of(ptr, type, member) \
48 void *__mptr = (void *)(ptr); \
49 ((type *)(__mptr - offsetof(type, member))); \
54 * Helper structure used by eBPF C program
55 * to describe BPF map attributes to libbpf loader
59 unsigned int key_size;
60 unsigned int value_size;
61 unsigned int max_entries;
62 unsigned int map_flags;
65 enum libbpf_pin_type {
67 /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
71 enum libbpf_tristate {
77 #define __kconfig __attribute__((section(".kconfig")))
78 #define __ksym __attribute__((section(".ksyms")))