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