xref: /openbmc/linux/net/xdp/xsk_queue.h (revision 07428da9e25a5dfae7252cd554c90557f9086a73)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* XDP user-space ring structure
3  * Copyright(c) 2018 Intel Corporation.
4  */
5 
6 #ifndef _LINUX_XSK_QUEUE_H
7 #define _LINUX_XSK_QUEUE_H
8 
9 #include <linux/types.h>
10 #include <linux/if_xdp.h>
11 #include <net/xdp_sock.h>
12 #include <net/xsk_buff_pool.h>
13 
14 #include "xsk.h"
15 
16 struct xdp_ring {
17 	u32 producer ____cacheline_aligned_in_smp;
18 	/* Hinder the adjacent cache prefetcher to prefetch the consumer
19 	 * pointer if the producer pointer is touched and vice versa.
20 	 */
21 	u32 pad1 ____cacheline_aligned_in_smp;
22 	u32 consumer ____cacheline_aligned_in_smp;
23 	u32 pad2 ____cacheline_aligned_in_smp;
24 	u32 flags;
25 	u32 pad3 ____cacheline_aligned_in_smp;
26 };
27 
28 /* Used for the RX and TX queues for packets */
29 struct xdp_rxtx_ring {
30 	struct xdp_ring ptrs;
31 	struct xdp_desc desc[] ____cacheline_aligned_in_smp;
32 };
33 
34 /* Used for the fill and completion queues for buffers */
35 struct xdp_umem_ring {
36 	struct xdp_ring ptrs;
37 	u64 desc[] ____cacheline_aligned_in_smp;
38 };
39 
40 struct xsk_queue {
41 	u32 ring_mask;
42 	u32 nentries;
43 	u32 cached_prod;
44 	u32 cached_cons;
45 	struct xdp_ring *ring;
46 	u64 invalid_descs;
47 	u64 queue_empty_descs;
48 	size_t ring_vmalloc_size;
49 };
50 
51 /* The structure of the shared state of the rings are a simple
52  * circular buffer, as outlined in
53  * Documentation/core-api/circular-buffers.rst. For the Rx and
54  * completion ring, the kernel is the producer and user space is the
55  * consumer. For the Tx and fill rings, the kernel is the consumer and
56  * user space is the producer.
57  *
58  * producer                         consumer
59  *
60  * if (LOAD ->consumer) {  (A)      LOAD.acq ->producer  (C)
61  *    STORE $data                   LOAD $data
62  *    STORE.rel ->producer (B)      STORE.rel ->consumer (D)
63  * }
64  *
65  * (A) pairs with (D), and (B) pairs with (C).
66  *
67  * Starting with (B), it protects the data from being written after
68  * the producer pointer. If this barrier was missing, the consumer
69  * could observe the producer pointer being set and thus load the data
70  * before the producer has written the new data. The consumer would in
71  * this case load the old data.
72  *
73  * (C) protects the consumer from speculatively loading the data before
74  * the producer pointer actually has been read. If we do not have this
75  * barrier, some architectures could load old data as speculative loads
76  * are not discarded as the CPU does not know there is a dependency
77  * between ->producer and data.
78  *
79  * (A) is a control dependency that separates the load of ->consumer
80  * from the stores of $data. In case ->consumer indicates there is no
81  * room in the buffer to store $data we do not. The dependency will
82  * order both of the stores after the loads. So no barrier is needed.
83  *
84  * (D) protects the load of the data to be observed to happen after the
85  * store of the consumer pointer. If we did not have this memory
86  * barrier, the producer could observe the consumer pointer being set
87  * and overwrite the data with a new value before the consumer got the
88  * chance to read the old value. The consumer would thus miss reading
89  * the old entry and very likely read the new entry twice, once right
90  * now and again after circling through the ring.
91  */
92 
93 /* The operations on the rings are the following:
94  *
95  * producer                           consumer
96  *
97  * RESERVE entries                    PEEK in the ring for entries
98  * WRITE data into the ring           READ data from the ring
99  * SUBMIT entries                     RELEASE entries
100  *
101  * The producer reserves one or more entries in the ring. It can then
102  * fill in these entries and finally submit them so that they can be
103  * seen and read by the consumer.
104  *
105  * The consumer peeks into the ring to see if the producer has written
106  * any new entries. If so, the consumer can then read these entries
107  * and when it is done reading them release them back to the producer
108  * so that the producer can use these slots to fill in new entries.
109  *
110  * The function names below reflect these operations.
111  */
112 
113 /* Functions that read and validate content from consumer rings. */
114 
115 static inline void __xskq_cons_read_addr_unchecked(struct xsk_queue *q, u32 cached_cons, u64 *addr)
116 {
117 	struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring;
118 	u32 idx = cached_cons & q->ring_mask;
119 
120 	*addr = ring->desc[idx];
121 }
122 
123 static inline bool xskq_cons_read_addr_unchecked(struct xsk_queue *q, u64 *addr)
124 {
125 	if (q->cached_cons != q->cached_prod) {
126 		__xskq_cons_read_addr_unchecked(q, q->cached_cons, addr);
127 		return true;
128 	}
129 
130 	return false;
131 }
132 
133 static inline bool xp_unused_options_set(u32 options)
134 {
135 	return options & ~XDP_PKT_CONTD;
136 }
137 
138 static inline bool xp_aligned_validate_desc(struct xsk_buff_pool *pool,
139 					    struct xdp_desc *desc)
140 {
141 	u64 offset = desc->addr & (pool->chunk_size - 1);
142 
143 	if (!desc->len)
144 		return false;
145 
146 	if (offset + desc->len > pool->chunk_size)
147 		return false;
148 
149 	if (desc->addr >= pool->addrs_cnt)
150 		return false;
151 
152 	if (xp_unused_options_set(desc->options))
153 		return false;
154 	return true;
155 }
156 
157 static inline bool xp_unaligned_validate_desc(struct xsk_buff_pool *pool,
158 					      struct xdp_desc *desc)
159 {
160 	u64 addr = xp_unaligned_add_offset_to_addr(desc->addr);
161 
162 	if (!desc->len)
163 		return false;
164 
165 	if (desc->len > pool->chunk_size)
166 		return false;
167 
168 	if (addr >= pool->addrs_cnt || addr + desc->len > pool->addrs_cnt ||
169 	    xp_desc_crosses_non_contig_pg(pool, addr, desc->len))
170 		return false;
171 
172 	if (xp_unused_options_set(desc->options))
173 		return false;
174 	return true;
175 }
176 
177 static inline bool xp_validate_desc(struct xsk_buff_pool *pool,
178 				    struct xdp_desc *desc)
179 {
180 	return pool->unaligned ? xp_unaligned_validate_desc(pool, desc) :
181 		xp_aligned_validate_desc(pool, desc);
182 }
183 
184 static inline bool xskq_has_descs(struct xsk_queue *q)
185 {
186 	return q->cached_cons != q->cached_prod;
187 }
188 
189 static inline bool xskq_cons_is_valid_desc(struct xsk_queue *q,
190 					   struct xdp_desc *d,
191 					   struct xsk_buff_pool *pool)
192 {
193 	if (!xp_validate_desc(pool, d)) {
194 		q->invalid_descs++;
195 		return false;
196 	}
197 	return true;
198 }
199 
200 static inline bool xskq_cons_read_desc(struct xsk_queue *q,
201 				       struct xdp_desc *desc,
202 				       struct xsk_buff_pool *pool)
203 {
204 	if (q->cached_cons != q->cached_prod) {
205 		struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring;
206 		u32 idx = q->cached_cons & q->ring_mask;
207 
208 		*desc = ring->desc[idx];
209 		return xskq_cons_is_valid_desc(q, desc, pool);
210 	}
211 
212 	q->queue_empty_descs++;
213 	return false;
214 }
215 
216 static inline void xskq_cons_release_n(struct xsk_queue *q, u32 cnt)
217 {
218 	q->cached_cons += cnt;
219 }
220 
221 static inline u32 xskq_cons_read_desc_batch(struct xsk_queue *q, struct xsk_buff_pool *pool,
222 					    u32 max)
223 {
224 	u32 cached_cons = q->cached_cons, nb_entries = 0;
225 	struct xdp_desc *descs = pool->tx_descs;
226 
227 	while (cached_cons != q->cached_prod && nb_entries < max) {
228 		struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring;
229 		u32 idx = cached_cons & q->ring_mask;
230 
231 		descs[nb_entries] = ring->desc[idx];
232 		if (unlikely(!xskq_cons_is_valid_desc(q, &descs[nb_entries], pool))) {
233 			/* Skip the entry */
234 			cached_cons++;
235 			continue;
236 		}
237 
238 		nb_entries++;
239 		cached_cons++;
240 	}
241 
242 	/* Release valid plus any invalid entries */
243 	xskq_cons_release_n(q, cached_cons - q->cached_cons);
244 	return nb_entries;
245 }
246 
247 /* Functions for consumers */
248 
249 static inline void __xskq_cons_release(struct xsk_queue *q)
250 {
251 	smp_store_release(&q->ring->consumer, q->cached_cons); /* D, matchees A */
252 }
253 
254 static inline void __xskq_cons_peek(struct xsk_queue *q)
255 {
256 	/* Refresh the local pointer */
257 	q->cached_prod = smp_load_acquire(&q->ring->producer);  /* C, matches B */
258 }
259 
260 static inline void xskq_cons_get_entries(struct xsk_queue *q)
261 {
262 	__xskq_cons_release(q);
263 	__xskq_cons_peek(q);
264 }
265 
266 static inline u32 xskq_cons_nb_entries(struct xsk_queue *q, u32 max)
267 {
268 	u32 entries = q->cached_prod - q->cached_cons;
269 
270 	if (entries >= max)
271 		return max;
272 
273 	__xskq_cons_peek(q);
274 	entries = q->cached_prod - q->cached_cons;
275 
276 	return entries >= max ? max : entries;
277 }
278 
279 static inline bool xskq_cons_has_entries(struct xsk_queue *q, u32 cnt)
280 {
281 	return xskq_cons_nb_entries(q, cnt) >= cnt;
282 }
283 
284 static inline bool xskq_cons_peek_addr_unchecked(struct xsk_queue *q, u64 *addr)
285 {
286 	if (q->cached_prod == q->cached_cons)
287 		xskq_cons_get_entries(q);
288 	return xskq_cons_read_addr_unchecked(q, addr);
289 }
290 
291 static inline bool xskq_cons_peek_desc(struct xsk_queue *q,
292 				       struct xdp_desc *desc,
293 				       struct xsk_buff_pool *pool)
294 {
295 	if (q->cached_prod == q->cached_cons)
296 		xskq_cons_get_entries(q);
297 	return xskq_cons_read_desc(q, desc, pool);
298 }
299 
300 /* To improve performance in the xskq_cons_release functions, only update local state here.
301  * Reflect this to global state when we get new entries from the ring in
302  * xskq_cons_get_entries() and whenever Rx or Tx processing are completed in the NAPI loop.
303  */
304 static inline void xskq_cons_release(struct xsk_queue *q)
305 {
306 	q->cached_cons++;
307 }
308 
309 static inline void xskq_cons_cancel_n(struct xsk_queue *q, u32 cnt)
310 {
311 	q->cached_cons -= cnt;
312 }
313 
314 static inline u32 xskq_cons_present_entries(struct xsk_queue *q)
315 {
316 	/* No barriers needed since data is not accessed */
317 	return READ_ONCE(q->ring->producer) - READ_ONCE(q->ring->consumer);
318 }
319 
320 /* Functions for producers */
321 
322 static inline u32 xskq_prod_nb_free(struct xsk_queue *q, u32 max)
323 {
324 	u32 free_entries = q->nentries - (q->cached_prod - q->cached_cons);
325 
326 	if (free_entries >= max)
327 		return max;
328 
329 	/* Refresh the local tail pointer */
330 	q->cached_cons = READ_ONCE(q->ring->consumer);
331 	free_entries = q->nentries - (q->cached_prod - q->cached_cons);
332 
333 	return free_entries >= max ? max : free_entries;
334 }
335 
336 static inline bool xskq_prod_is_full(struct xsk_queue *q)
337 {
338 	return xskq_prod_nb_free(q, 1) ? false : true;
339 }
340 
341 static inline void xskq_prod_cancel_n(struct xsk_queue *q, u32 cnt)
342 {
343 	q->cached_prod -= cnt;
344 }
345 
346 static inline int xskq_prod_reserve(struct xsk_queue *q)
347 {
348 	if (xskq_prod_is_full(q))
349 		return -ENOSPC;
350 
351 	/* A, matches D */
352 	q->cached_prod++;
353 	return 0;
354 }
355 
356 static inline int xskq_prod_reserve_addr(struct xsk_queue *q, u64 addr)
357 {
358 	struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring;
359 
360 	if (xskq_prod_is_full(q))
361 		return -ENOSPC;
362 
363 	/* A, matches D */
364 	ring->desc[q->cached_prod++ & q->ring_mask] = addr;
365 	return 0;
366 }
367 
368 static inline void xskq_prod_write_addr_batch(struct xsk_queue *q, struct xdp_desc *descs,
369 					      u32 nb_entries)
370 {
371 	struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring;
372 	u32 i, cached_prod;
373 
374 	/* A, matches D */
375 	cached_prod = q->cached_prod;
376 	for (i = 0; i < nb_entries; i++)
377 		ring->desc[cached_prod++ & q->ring_mask] = descs[i].addr;
378 	q->cached_prod = cached_prod;
379 }
380 
381 static inline int xskq_prod_reserve_desc(struct xsk_queue *q,
382 					 u64 addr, u32 len, u32 flags)
383 {
384 	struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring;
385 	u32 idx;
386 
387 	if (xskq_prod_is_full(q))
388 		return -ENOBUFS;
389 
390 	/* A, matches D */
391 	idx = q->cached_prod++ & q->ring_mask;
392 	ring->desc[idx].addr = addr;
393 	ring->desc[idx].len = len;
394 	ring->desc[idx].options = flags;
395 
396 	return 0;
397 }
398 
399 static inline void __xskq_prod_submit(struct xsk_queue *q, u32 idx)
400 {
401 	smp_store_release(&q->ring->producer, idx); /* B, matches C */
402 }
403 
404 static inline void xskq_prod_submit(struct xsk_queue *q)
405 {
406 	__xskq_prod_submit(q, q->cached_prod);
407 }
408 
409 static inline void xskq_prod_submit_n(struct xsk_queue *q, u32 nb_entries)
410 {
411 	__xskq_prod_submit(q, q->ring->producer + nb_entries);
412 }
413 
414 static inline bool xskq_prod_is_empty(struct xsk_queue *q)
415 {
416 	/* No barriers needed since data is not accessed */
417 	return READ_ONCE(q->ring->consumer) == READ_ONCE(q->ring->producer);
418 }
419 
420 /* For both producers and consumers */
421 
422 static inline u64 xskq_nb_invalid_descs(struct xsk_queue *q)
423 {
424 	return q ? q->invalid_descs : 0;
425 }
426 
427 static inline u64 xskq_nb_queue_empty_descs(struct xsk_queue *q)
428 {
429 	return q ? q->queue_empty_descs : 0;
430 }
431 
432 struct xsk_queue *xskq_create(u32 nentries, bool umem_queue);
433 void xskq_destroy(struct xsk_queue *q_ops);
434 
435 #endif /* _LINUX_XSK_QUEUE_H */
436