xref: /linux/tools/lib/bpf/bpf_helpers.h (revision 5d085ad2e68cceec8332b23ea8f630a28b506366)
1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 #ifndef __BPF_HELPERS__
3 #define __BPF_HELPERS__
4 
5 /*
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
9  * as __u64.
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 
16 /* Helper macro to print out debug messages */
17 #define bpf_printk(fmt, ...)				\
18 ({							\
19 	char ____fmt[] = fmt;				\
20 	bpf_trace_printk(____fmt, sizeof(____fmt),	\
21 			 ##__VA_ARGS__);		\
22 })
23 
24 /*
25  * Helper macro to place programs, maps, license in
26  * different sections in elf_bpf file. Section names
27  * are interpreted by elf_bpf loader
28  */
29 #define SEC(NAME) __attribute__((section(NAME), used))
30 
31 #ifndef __always_inline
32 #define __always_inline __attribute__((always_inline))
33 #endif
34 #ifndef __weak
35 #define __weak __attribute__((weak))
36 #endif
37 
38 /*
39  * Helper structure used by eBPF C program
40  * to describe BPF map attributes to libbpf loader
41  */
42 struct bpf_map_def {
43 	unsigned int type;
44 	unsigned int key_size;
45 	unsigned int value_size;
46 	unsigned int max_entries;
47 	unsigned int map_flags;
48 };
49 
50 enum libbpf_pin_type {
51 	LIBBPF_PIN_NONE,
52 	/* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
53 	LIBBPF_PIN_BY_NAME,
54 };
55 
56 enum libbpf_tristate {
57 	TRI_NO = 0,
58 	TRI_YES = 1,
59 	TRI_MODULE = 2,
60 };
61 
62 #define __kconfig __attribute__((section(".kconfig")))
63 
64 #endif
65