xref: /linux/tools/testing/selftests/bpf/xsk.h (revision 0526b56cbc3c489642bd6a5fe4b718dea7ef0ee8)
1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 
3 /*
4  * AF_XDP user-space access library.
5  *
6  * Copyright (c) 2018 - 2019 Intel Corporation.
7  * Copyright (c) 2019 Facebook
8  *
9  * Author(s): Magnus Karlsson <magnus.karlsson@intel.com>
10  */
11 
12 #ifndef __XSK_H
13 #define __XSK_H
14 
15 #include <stdio.h>
16 #include <stdint.h>
17 #include <stdbool.h>
18 #include <linux/if_xdp.h>
19 
20 #include <bpf/libbpf.h>
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 /* Do not access these members directly. Use the functions below. */
27 #define DEFINE_XSK_RING(name) \
28 struct name { \
29 	__u32 cached_prod; \
30 	__u32 cached_cons; \
31 	__u32 mask; \
32 	__u32 size; \
33 	__u32 *producer; \
34 	__u32 *consumer; \
35 	void *ring; \
36 	__u32 *flags; \
37 }
38 
39 DEFINE_XSK_RING(xsk_ring_prod);
40 DEFINE_XSK_RING(xsk_ring_cons);
41 
42 /* For a detailed explanation on the memory barriers associated with the
43  * ring, please take a look at net/xdp/xsk_queue.h.
44  */
45 
46 struct xsk_umem;
47 struct xsk_socket;
48 
49 static inline __u64 *xsk_ring_prod__fill_addr(struct xsk_ring_prod *fill,
50 					      __u32 idx)
51 {
52 	__u64 *addrs = (__u64 *)fill->ring;
53 
54 	return &addrs[idx & fill->mask];
55 }
56 
57 static inline const __u64 *
58 xsk_ring_cons__comp_addr(const struct xsk_ring_cons *comp, __u32 idx)
59 {
60 	const __u64 *addrs = (const __u64 *)comp->ring;
61 
62 	return &addrs[idx & comp->mask];
63 }
64 
65 static inline struct xdp_desc *xsk_ring_prod__tx_desc(struct xsk_ring_prod *tx,
66 						      __u32 idx)
67 {
68 	struct xdp_desc *descs = (struct xdp_desc *)tx->ring;
69 
70 	return &descs[idx & tx->mask];
71 }
72 
73 static inline const struct xdp_desc *
74 xsk_ring_cons__rx_desc(const struct xsk_ring_cons *rx, __u32 idx)
75 {
76 	const struct xdp_desc *descs = (const struct xdp_desc *)rx->ring;
77 
78 	return &descs[idx & rx->mask];
79 }
80 
81 static inline int xsk_ring_prod__needs_wakeup(const struct xsk_ring_prod *r)
82 {
83 	return *r->flags & XDP_RING_NEED_WAKEUP;
84 }
85 
86 static inline __u32 xsk_prod_nb_free(struct xsk_ring_prod *r, __u32 nb)
87 {
88 	__u32 free_entries = r->cached_cons - r->cached_prod;
89 
90 	if (free_entries >= nb)
91 		return free_entries;
92 
93 	/* Refresh the local tail pointer.
94 	 * cached_cons is r->size bigger than the real consumer pointer so
95 	 * that this addition can be avoided in the more frequently
96 	 * executed code that computs free_entries in the beginning of
97 	 * this function. Without this optimization it whould have been
98 	 * free_entries = r->cached_prod - r->cached_cons + r->size.
99 	 */
100 	r->cached_cons = __atomic_load_n(r->consumer, __ATOMIC_ACQUIRE);
101 	r->cached_cons += r->size;
102 
103 	return r->cached_cons - r->cached_prod;
104 }
105 
106 static inline __u32 xsk_cons_nb_avail(struct xsk_ring_cons *r, __u32 nb)
107 {
108 	__u32 entries = r->cached_prod - r->cached_cons;
109 
110 	if (entries == 0) {
111 		r->cached_prod = __atomic_load_n(r->producer, __ATOMIC_ACQUIRE);
112 		entries = r->cached_prod - r->cached_cons;
113 	}
114 
115 	return (entries > nb) ? nb : entries;
116 }
117 
118 static inline __u32 xsk_ring_prod__reserve(struct xsk_ring_prod *prod, __u32 nb, __u32 *idx)
119 {
120 	if (xsk_prod_nb_free(prod, nb) < nb)
121 		return 0;
122 
123 	*idx = prod->cached_prod;
124 	prod->cached_prod += nb;
125 
126 	return nb;
127 }
128 
129 static inline void xsk_ring_prod__submit(struct xsk_ring_prod *prod, __u32 nb)
130 {
131 	/* Make sure everything has been written to the ring before indicating
132 	 * this to the kernel by writing the producer pointer.
133 	 */
134 	__atomic_store_n(prod->producer, *prod->producer + nb, __ATOMIC_RELEASE);
135 }
136 
137 static inline __u32 xsk_ring_cons__peek(struct xsk_ring_cons *cons, __u32 nb, __u32 *idx)
138 {
139 	__u32 entries = xsk_cons_nb_avail(cons, nb);
140 
141 	if (entries > 0) {
142 		*idx = cons->cached_cons;
143 		cons->cached_cons += entries;
144 	}
145 
146 	return entries;
147 }
148 
149 static inline void xsk_ring_cons__cancel(struct xsk_ring_cons *cons, __u32 nb)
150 {
151 	cons->cached_cons -= nb;
152 }
153 
154 static inline void xsk_ring_cons__release(struct xsk_ring_cons *cons, __u32 nb)
155 {
156 	/* Make sure data has been read before indicating we are done
157 	 * with the entries by updating the consumer pointer.
158 	 */
159 	__atomic_store_n(cons->consumer, *cons->consumer + nb, __ATOMIC_RELEASE);
160 }
161 
162 static inline void *xsk_umem__get_data(void *umem_area, __u64 addr)
163 {
164 	return &((char *)umem_area)[addr];
165 }
166 
167 static inline __u64 xsk_umem__extract_addr(__u64 addr)
168 {
169 	return addr & XSK_UNALIGNED_BUF_ADDR_MASK;
170 }
171 
172 static inline __u64 xsk_umem__extract_offset(__u64 addr)
173 {
174 	return addr >> XSK_UNALIGNED_BUF_OFFSET_SHIFT;
175 }
176 
177 static inline __u64 xsk_umem__add_offset_to_addr(__u64 addr)
178 {
179 	return xsk_umem__extract_addr(addr) + xsk_umem__extract_offset(addr);
180 }
181 
182 int xsk_umem__fd(const struct xsk_umem *umem);
183 int xsk_socket__fd(const struct xsk_socket *xsk);
184 
185 #define XSK_RING_CONS__DEFAULT_NUM_DESCS      2048
186 #define XSK_RING_PROD__DEFAULT_NUM_DESCS      2048
187 #define XSK_UMEM__DEFAULT_FRAME_SHIFT    12 /* 4096 bytes */
188 #define XSK_UMEM__DEFAULT_FRAME_SIZE     (1 << XSK_UMEM__DEFAULT_FRAME_SHIFT)
189 #define XSK_UMEM__DEFAULT_FRAME_HEADROOM 0
190 #define XSK_UMEM__DEFAULT_FLAGS 0
191 
192 struct xsk_umem_config {
193 	__u32 fill_size;
194 	__u32 comp_size;
195 	__u32 frame_size;
196 	__u32 frame_headroom;
197 	__u32 flags;
198 };
199 
200 int xsk_attach_xdp_program(struct bpf_program *prog, int ifindex, u32 xdp_flags);
201 void xsk_detach_xdp_program(int ifindex, u32 xdp_flags);
202 int xsk_update_xskmap(struct bpf_map *map, struct xsk_socket *xsk);
203 void xsk_clear_xskmap(struct bpf_map *map);
204 bool xsk_is_in_mode(u32 ifindex, int mode);
205 
206 struct xsk_socket_config {
207 	__u32 rx_size;
208 	__u32 tx_size;
209 	__u16 bind_flags;
210 };
211 
212 /* Set config to NULL to get the default configuration. */
213 int xsk_umem__create(struct xsk_umem **umem,
214 		     void *umem_area, __u64 size,
215 		     struct xsk_ring_prod *fill,
216 		     struct xsk_ring_cons *comp,
217 		     const struct xsk_umem_config *config);
218 int xsk_socket__create(struct xsk_socket **xsk,
219 		       int ifindex, __u32 queue_id,
220 		       struct xsk_umem *umem,
221 		       struct xsk_ring_cons *rx,
222 		       struct xsk_ring_prod *tx,
223 		       const struct xsk_socket_config *config);
224 int xsk_socket__create_shared(struct xsk_socket **xsk_ptr,
225 			      int ifindex,
226 			      __u32 queue_id, struct xsk_umem *umem,
227 			      struct xsk_ring_cons *rx,
228 			      struct xsk_ring_prod *tx,
229 			      struct xsk_ring_prod *fill,
230 			      struct xsk_ring_cons *comp,
231 			      const struct xsk_socket_config *config);
232 
233 /* Returns 0 for success and -EBUSY if the umem is still in use. */
234 int xsk_umem__delete(struct xsk_umem *umem);
235 void xsk_socket__delete(struct xsk_socket *xsk);
236 
237 #ifdef __cplusplus
238 } /* extern "C" */
239 #endif
240 
241 #endif /* __XSK_H */
242