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