]>
git.proxmox.com Git - mirror_iproute2.git/blob - include/bpf_api.h
6 * This file can be included into eBPF kernel programs. It contains
7 * a couple of useful helper functions, map/section ABI (bpf_elf.h),
8 * misc macros and some eBPF specific LLVM built-ins.
13 #include <linux/pkt_cls.h>
14 #include <linux/bpf.h>
15 #include <linux/filter.h>
17 #include <asm/byteorder.h>
24 # define __stringify(X) #X
27 #ifndef __maybe_unused
28 # define __maybe_unused __attribute__((__unused__))
32 # define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE, MEMBER)
36 # define likely(X) __builtin_expect(!!(X), 1)
40 # define unlikely(X) __builtin_expect(!!(X), 0)
44 # define htons(X) __constant_htons((X))
48 # define ntohs(X) __constant_ntohs((X))
52 # define htonl(X) __constant_htonl((X))
56 # define ntohl(X) __constant_ntohl((X))
59 /** Section helper macros. */
62 # define __section(NAME) \
63 __attribute__((section(NAME), used))
66 #ifndef __section_tail
67 # define __section_tail(ID, KEY) \
68 __section(__stringify(ID) "/" __stringify(KEY))
71 #ifndef __section_cls_entry
72 # define __section_cls_entry \
73 __section(ELF_SECTION_CLASSIFIER)
76 #ifndef __section_act_entry
77 # define __section_act_entry \
78 __section(ELF_SECTION_ACTION)
81 #ifndef __section_license
82 # define __section_license \
83 __section(ELF_SECTION_LICENSE)
86 #ifndef __section_maps
87 # define __section_maps \
88 __section(ELF_SECTION_MAPS)
91 /** Declaration helper macros. */
94 # define BPF_LICENSE(NAME) \
95 char ____license[] __section_license = NAME
99 # define __BPF_MAP(NAME, TYPE, ID, SIZE_KEY, SIZE_VALUE, PIN, MAX_ELEM) \
100 struct bpf_elf_map __section_maps NAME = { \
103 .size_key = (SIZE_KEY), \
104 .size_value = (SIZE_VALUE), \
106 .max_elem = (MAX_ELEM), \
111 # define BPF_HASH(NAME, ID, SIZE_KEY, SIZE_VALUE, PIN, MAX_ELEM) \
112 __BPF_MAP(NAME, BPF_MAP_TYPE_HASH, ID, SIZE_KEY, SIZE_VALUE, \
117 # define BPF_ARRAY(NAME, ID, SIZE_VALUE, PIN, MAX_ELEM) \
118 __BPF_MAP(NAME, BPF_MAP_TYPE_ARRAY, ID, sizeof(uint32_t), \
119 SIZE_VALUE, PIN, MAX_ELEM)
123 # define BPF_ARRAY2(NAME, ID, PIN, MAX_ELEM) \
124 BPF_ARRAY(NAME, ID, sizeof(uint16_t), PIN, MAX_ELEM)
128 # define BPF_ARRAY4(NAME, ID, PIN, MAX_ELEM) \
129 BPF_ARRAY(NAME, ID, sizeof(uint32_t), PIN, MAX_ELEM)
133 # define BPF_ARRAY8(NAME, ID, PIN, MAX_ELEM) \
134 BPF_ARRAY(NAME, ID, sizeof(uint64_t), PIN, MAX_ELEM)
137 #ifndef BPF_PROG_ARRAY
138 # define BPF_PROG_ARRAY(NAME, ID, PIN, MAX_ELEM) \
139 __BPF_MAP(NAME, BPF_MAP_TYPE_PROG_ARRAY, ID, sizeof(uint32_t), \
140 sizeof(uint32_t), PIN, MAX_ELEM)
143 /** Classifier helper */
145 #ifndef BPF_H_DEFAULT
146 # define BPF_H_DEFAULT -1
149 /** BPF helper functions for tc. */
152 # define BPF_FUNC(NAME, ...) \
153 (* NAME)(__VA_ARGS__) __maybe_unused = (void *) BPF_FUNC_##NAME
156 /* Map access/manipulation */
157 static void *BPF_FUNC(map_lookup_elem
, void *map
, const void *key
);
158 static int BPF_FUNC(map_update_elem
, void *map
, const void *key
,
159 const void *value
, uint32_t flags
);
160 static int BPF_FUNC(map_delete_elem
, void *map
, const void *key
);
163 static uint64_t BPF_FUNC(ktime_get_ns
);
166 static void BPF_FUNC(trace_printk
, const char *fmt
, int fmt_size
, ...);
169 static uint32_t BPF_FUNC(get_prandom_u32
);
172 static void BPF_FUNC(tail_call
, struct __sk_buff
*skb
, void *map
,
176 static uint32_t BPF_FUNC(get_smp_processor_id
);
178 /* Packet misc meta data */
179 static uint32_t BPF_FUNC(get_cgroup_classid
, struct __sk_buff
*skb
);
180 static uint32_t BPF_FUNC(get_route_realm
, struct __sk_buff
*skb
);
182 /* Packet redirection */
183 static int BPF_FUNC(redirect
, int ifindex
, uint32_t flags
);
184 static int BPF_FUNC(clone_redirect
, struct __sk_buff
*skb
, int ifindex
,
187 /* Packet manipulation */
188 #define BPF_PSEUDO_HDR 0x10
189 #define BPF_HAS_PSEUDO_HDR(flags) ((flags) & BPF_PSEUDO_HDR)
190 #define BPF_HDR_FIELD_SIZE(flags) ((flags) & 0x0f)
192 static int BPF_FUNC(skb_store_bytes
, struct __sk_buff
*skb
, uint32_t off
,
193 void *from
, uint32_t len
, uint32_t flags
);
194 static int BPF_FUNC(l3_csum_replace
, struct __sk_buff
*skb
, uint32_t off
,
195 uint32_t from
, uint32_t to
, uint32_t flags
);
196 static int BPF_FUNC(l4_csum_replace
, struct __sk_buff
*skb
, uint32_t off
,
197 uint32_t from
, uint32_t to
, uint32_t flags
);
199 /* Packet vlan encap/decap */
200 static int BPF_FUNC(skb_vlan_push
, struct __sk_buff
*skb
, uint16_t proto
,
202 static int BPF_FUNC(skb_vlan_pop
, struct __sk_buff
*skb
);
204 /* Packet tunnel encap/decap */
205 static int BPF_FUNC(skb_get_tunnel_key
, struct __sk_buff
*skb
,
206 struct bpf_tunnel_key
*to
, uint32_t size
, uint32_t flags
);
207 static int BPF_FUNC(skb_set_tunnel_key
, struct __sk_buff
*skb
,
208 struct bpf_tunnel_key
*from
, uint32_t size
, uint32_t flags
);
210 /** LLVM built-ins */
213 # define lock_xadd(ptr, val) ((void) __sync_fetch_and_add(ptr, val))
216 unsigned long long load_byte(void *skb
, unsigned long long off
)
217 asm ("llvm.bpf.load.byte");
219 unsigned long long load_half(void *skb
, unsigned long long off
)
220 asm ("llvm.bpf.load.half");
222 unsigned long long load_word(void *skb
, unsigned long long off
)
223 asm ("llvm.bpf.load.word");
225 #endif /* __BPF_API__ */