blob: e9a4ecddb7a52400a6502957263509637b3cada2 [file] [log] [blame]
Andrii Nakryiko929ffa62019-08-15 22:45:43 -07001/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +02002#ifndef __BPF_HELPERS__
3#define __BPF_HELPERS__
Alexei Starovoitov249b8122014-12-01 15:06:37 -08004
Yoshiki Komachiae460c02020-04-21 09:05:27 +09005/*
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 */
Andrii Nakryiko24f25762019-10-06 20:07:38 -070011#include "bpf_helper_defs.h"
12
Andrii Nakryiko00acd002019-07-05 08:50:10 -070013#define __uint(name, val) int (*name)[val]
Andrii Nakryikoa53ba152019-10-03 21:02:11 -070014#define __type(name, val) typeof(val) *name
Andrii Nakryiko646f02f2020-04-28 17:27:39 -070015#define __array(name, val) typeof(val) *name[]
Andrii Nakryiko00acd002019-07-05 08:50:10 -070016
Andrii Nakryiko7db38222019-10-08 10:59:41 -070017/* Helper macro to print out debug messages */
Michal Rostecki37739d12019-05-23 14:53:54 +020018#define bpf_printk(fmt, ...) \
19({ \
20 char ____fmt[] = fmt; \
21 bpf_trace_printk(____fmt, sizeof(____fmt), \
22 ##__VA_ARGS__); \
23})
24
Andrii Nakryiko7db38222019-10-08 10:59:41 -070025/*
26 * Helper macro to place programs, maps, license in
Ilya Leoshkevich4ce150b2019-09-12 18:05:43 +020027 * 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
Andrii Nakryiko7db38222019-10-08 10:59:41 -070032#ifndef __always_inline
33#define __always_inline __attribute__((always_inline))
34#endif
Andrii Nakryiko166750b2019-12-13 17:47:08 -080035#ifndef __weak
36#define __weak __attribute__((weak))
37#endif
Andrii Nakryiko7db38222019-10-08 10:59:41 -070038
39/*
Yonghong Song5fbc2202020-05-09 10:59:19 -070040 * Helper macro to manipulate data structures
41 */
42#ifndef offsetof
Yonghong Song63fe3fd2020-08-10 20:08:52 -070043#define offsetof(TYPE, MEMBER) ((unsigned long)&((TYPE *)0)->MEMBER)
Yonghong Song5fbc2202020-05-09 10:59:19 -070044#endif
45#ifndef container_of
46#define container_of(ptr, type, member) \
47 ({ \
48 void *__mptr = (void *)(ptr); \
49 ((type *)(__mptr - offsetof(type, member))); \
50 })
51#endif
52
53/*
Andrii Nakryiko7db38222019-10-08 10:59:41 -070054 * Helper structure used by eBPF C program
Andrii Nakryiko36b5d472019-10-08 10:59:37 -070055 * to describe BPF map attributes to libbpf loader
Alexei Starovoitov249b8122014-12-01 15:06:37 -080056 */
57struct bpf_map_def {
58 unsigned int type;
59 unsigned int key_size;
60 unsigned int value_size;
61 unsigned int max_entries;
Alexei Starovoitov89b97602016-03-07 21:57:20 -080062 unsigned int map_flags;
Alexei Starovoitov249b8122014-12-01 15:06:37 -080063};
64
Toke Høiland-Jørgensen57a00f42019-11-02 12:09:41 +010065enum libbpf_pin_type {
66 LIBBPF_PIN_NONE,
67 /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
68 LIBBPF_PIN_BY_NAME,
69};
70
Andrii Nakryiko166750b2019-12-13 17:47:08 -080071enum libbpf_tristate {
72 TRI_NO = 0,
73 TRI_YES = 1,
74 TRI_MODULE = 2,
75};
76
Andrii Nakryiko81bfdd02019-12-18 16:28:34 -080077#define __kconfig __attribute__((section(".kconfig")))
Andrii Nakryiko1c0c7072020-06-19 16:16:56 -070078#define __ksym __attribute__((section(".ksyms")))
Andrii Nakryiko81bfdd02019-12-18 16:28:34 -080079
Alexei Starovoitov249b8122014-12-01 15:06:37 -080080#endif