]> git.proxmox.com Git - mirror_iproute2.git/blame - include/bpf_api.h
lib: make ipvrf able to use libbpf and fix function name conflicts
[mirror_iproute2.git] / include / bpf_api.h
CommitLineData
6054c1eb 1/* SPDX-License-Identifier: GPL-2.0 */
41d6e33f
DB
2#ifndef __BPF_API__
3#define __BPF_API__
4
5/* Note:
6 *
7 * This file can be included into eBPF kernel programs. It contains
8 * a couple of useful helper functions, map/section ABI (bpf_elf.h),
9 * misc macros and some eBPF specific LLVM built-ins.
10 */
11
12#include <stdint.h>
13
14#include <linux/pkt_cls.h>
15#include <linux/bpf.h>
16#include <linux/filter.h>
17
18#include <asm/byteorder.h>
19
20#include "bpf_elf.h"
21
22/** Misc macros. */
23
24#ifndef __stringify
25# define __stringify(X) #X
26#endif
27
28#ifndef __maybe_unused
29# define __maybe_unused __attribute__((__unused__))
30#endif
31
32#ifndef offsetof
33# define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE, MEMBER)
34#endif
35
36#ifndef likely
37# define likely(X) __builtin_expect(!!(X), 1)
38#endif
39
40#ifndef unlikely
41# define unlikely(X) __builtin_expect(!!(X), 0)
42#endif
43
44#ifndef htons
45# define htons(X) __constant_htons((X))
46#endif
47
48#ifndef ntohs
49# define ntohs(X) __constant_ntohs((X))
50#endif
51
52#ifndef htonl
53# define htonl(X) __constant_htonl((X))
54#endif
55
56#ifndef ntohl
fd7f9c7f 57# define ntohl(X) __constant_ntohl((X))
41d6e33f
DB
58#endif
59
92a36995
DB
60#ifndef __inline__
61# define __inline__ __attribute__((always_inline))
62#endif
63
41d6e33f
DB
64/** Section helper macros. */
65
66#ifndef __section
67# define __section(NAME) \
68 __attribute__((section(NAME), used))
69#endif
70
71#ifndef __section_tail
72# define __section_tail(ID, KEY) \
73 __section(__stringify(ID) "/" __stringify(KEY))
74#endif
75
c7272ca7
DB
76#ifndef __section_xdp_entry
77# define __section_xdp_entry \
78 __section(ELF_SECTION_PROG)
79#endif
80
41d6e33f
DB
81#ifndef __section_cls_entry
82# define __section_cls_entry \
83 __section(ELF_SECTION_CLASSIFIER)
84#endif
85
86#ifndef __section_act_entry
87# define __section_act_entry \
88 __section(ELF_SECTION_ACTION)
89#endif
90
b15f440e
TG
91#ifndef __section_lwt_entry
92# define __section_lwt_entry \
93 __section(ELF_SECTION_PROG)
94#endif
95
41d6e33f
DB
96#ifndef __section_license
97# define __section_license \
98 __section(ELF_SECTION_LICENSE)
99#endif
100
101#ifndef __section_maps
102# define __section_maps \
103 __section(ELF_SECTION_MAPS)
104#endif
105
106/** Declaration helper macros. */
107
108#ifndef BPF_LICENSE
109# define BPF_LICENSE(NAME) \
110 char ____license[] __section_license = NAME
111#endif
112
41d6e33f
DB
113/** Classifier helper */
114
115#ifndef BPF_H_DEFAULT
116# define BPF_H_DEFAULT -1
117#endif
118
92a36995 119/** BPF helper functions for tc. Individual flags are in linux/bpf.h */
41d6e33f 120
e4225669
DB
121#ifndef __BPF_FUNC
122# define __BPF_FUNC(NAME, ...) \
123 (* NAME)(__VA_ARGS__) __maybe_unused
124#endif
125
41d6e33f
DB
126#ifndef BPF_FUNC
127# define BPF_FUNC(NAME, ...) \
e4225669 128 __BPF_FUNC(NAME, __VA_ARGS__) = (void *) BPF_FUNC_##NAME
41d6e33f
DB
129#endif
130
131/* Map access/manipulation */
132static void *BPF_FUNC(map_lookup_elem, void *map, const void *key);
133static int BPF_FUNC(map_update_elem, void *map, const void *key,
134 const void *value, uint32_t flags);
135static int BPF_FUNC(map_delete_elem, void *map, const void *key);
136
137/* Time access */
138static uint64_t BPF_FUNC(ktime_get_ns);
139
140/* Debugging */
92a36995
DB
141
142/* FIXME: __attribute__ ((format(printf, 1, 3))) not possible unless
143 * llvm bug https://llvm.org/bugs/show_bug.cgi?id=26243 gets resolved.
144 * It would require ____fmt to be made const, which generates a reloc
145 * entry (non-map).
146 */
41d6e33f
DB
147static void BPF_FUNC(trace_printk, const char *fmt, int fmt_size, ...);
148
92a36995
DB
149#ifndef printt
150# define printt(fmt, ...) \
151 ({ \
152 char ____fmt[] = fmt; \
153 trace_printk(____fmt, sizeof(____fmt), ##__VA_ARGS__); \
154 })
155#endif
156
41d6e33f
DB
157/* Random numbers */
158static uint32_t BPF_FUNC(get_prandom_u32);
159
160/* Tail calls */
161static void BPF_FUNC(tail_call, struct __sk_buff *skb, void *map,
162 uint32_t index);
163
164/* System helpers */
165static uint32_t BPF_FUNC(get_smp_processor_id);
e4225669 166static uint32_t BPF_FUNC(get_numa_node_id);
41d6e33f
DB
167
168/* Packet misc meta data */
169static uint32_t BPF_FUNC(get_cgroup_classid, struct __sk_buff *skb);
e4225669
DB
170static int BPF_FUNC(skb_under_cgroup, void *map, uint32_t index);
171
41d6e33f 172static uint32_t BPF_FUNC(get_route_realm, struct __sk_buff *skb);
e4225669
DB
173static uint32_t BPF_FUNC(get_hash_recalc, struct __sk_buff *skb);
174static uint32_t BPF_FUNC(set_hash_invalid, struct __sk_buff *skb);
41d6e33f
DB
175
176/* Packet redirection */
177static int BPF_FUNC(redirect, int ifindex, uint32_t flags);
178static int BPF_FUNC(clone_redirect, struct __sk_buff *skb, int ifindex,
179 uint32_t flags);
180
181/* Packet manipulation */
92a36995
DB
182static int BPF_FUNC(skb_load_bytes, struct __sk_buff *skb, uint32_t off,
183 void *to, uint32_t len);
41d6e33f 184static int BPF_FUNC(skb_store_bytes, struct __sk_buff *skb, uint32_t off,
92a36995
DB
185 const void *from, uint32_t len, uint32_t flags);
186
41d6e33f
DB
187static int BPF_FUNC(l3_csum_replace, struct __sk_buff *skb, uint32_t off,
188 uint32_t from, uint32_t to, uint32_t flags);
189static int BPF_FUNC(l4_csum_replace, struct __sk_buff *skb, uint32_t off,
190 uint32_t from, uint32_t to, uint32_t flags);
0395711c
DB
191static int BPF_FUNC(csum_diff, const void *from, uint32_t from_size,
192 const void *to, uint32_t to_size, uint32_t seed);
e4225669
DB
193static int BPF_FUNC(csum_update, struct __sk_buff *skb, uint32_t wsum);
194
195static int BPF_FUNC(skb_change_type, struct __sk_buff *skb, uint32_t type);
196static int BPF_FUNC(skb_change_proto, struct __sk_buff *skb, uint32_t proto,
197 uint32_t flags);
198static int BPF_FUNC(skb_change_tail, struct __sk_buff *skb, uint32_t nlen,
199 uint32_t flags);
200
201static int BPF_FUNC(skb_pull_data, struct __sk_buff *skb, uint32_t len);
202
203/* Event notification */
204static int __BPF_FUNC(skb_event_output, struct __sk_buff *skb, void *map,
205 uint64_t index, const void *data, uint32_t size) =
206 (void *) BPF_FUNC_perf_event_output;
41d6e33f
DB
207
208/* Packet vlan encap/decap */
209static int BPF_FUNC(skb_vlan_push, struct __sk_buff *skb, uint16_t proto,
210 uint16_t vlan_tci);
211static int BPF_FUNC(skb_vlan_pop, struct __sk_buff *skb);
212
213/* Packet tunnel encap/decap */
214static int BPF_FUNC(skb_get_tunnel_key, struct __sk_buff *skb,
215 struct bpf_tunnel_key *to, uint32_t size, uint32_t flags);
216static int BPF_FUNC(skb_set_tunnel_key, struct __sk_buff *skb,
92a36995
DB
217 const struct bpf_tunnel_key *from, uint32_t size,
218 uint32_t flags);
41d6e33f 219
0395711c
DB
220static int BPF_FUNC(skb_get_tunnel_opt, struct __sk_buff *skb,
221 void *to, uint32_t size);
222static int BPF_FUNC(skb_set_tunnel_opt, struct __sk_buff *skb,
223 const void *from, uint32_t size);
224
92a36995 225/** LLVM built-ins, mem*() routines work for constant size */
41d6e33f
DB
226
227#ifndef lock_xadd
228# define lock_xadd(ptr, val) ((void) __sync_fetch_and_add(ptr, val))
229#endif
230
92a36995
DB
231#ifndef memset
232# define memset(s, c, n) __builtin_memset((s), (c), (n))
233#endif
234
235#ifndef memcpy
236# define memcpy(d, s, n) __builtin_memcpy((d), (s), (n))
237#endif
238
239#ifndef memmove
240# define memmove(d, s, n) __builtin_memmove((d), (s), (n))
241#endif
242
243/* FIXME: __builtin_memcmp() is not yet fully useable unless llvm bug
244 * https://llvm.org/bugs/show_bug.cgi?id=26218 gets resolved. Also
245 * this one would generate a reloc entry (non-map), otherwise.
246 */
247#if 0
248#ifndef memcmp
249# define memcmp(a, b, n) __builtin_memcmp((a), (b), (n))
250#endif
251#endif
252
41d6e33f
DB
253unsigned long long load_byte(void *skb, unsigned long long off)
254 asm ("llvm.bpf.load.byte");
255
256unsigned long long load_half(void *skb, unsigned long long off)
257 asm ("llvm.bpf.load.half");
258
259unsigned long long load_word(void *skb, unsigned long long off)
260 asm ("llvm.bpf.load.word");
261
262#endif /* __BPF_API__ */