xref: /linux/net/xdp/xsk_queue.h (revision 59e6295fac26b8e85c1ea859cdd89fa1e47519d7)
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 	/* Mutual exclusion of the completion ring in the SKB mode.
50 	 * Protect: when sockets share a single cq when the same netdev
51 	 * and queue id is shared.
52 	 */
53 	spinlock_t cq_cached_prod_lock;
54 };
55 
56 struct parsed_desc {
57 	u32 mb;
58 	u32 valid;
59 };
60 
61 struct xsk_tx_batch {
62 	u32 tx_descs;
63 	u32 reclaim_descs;
64 	bool budget_limited;
65 };
66 
67 static inline u32 xsk_tx_batch_cq_descs(const struct xsk_tx_batch *batch)
68 {
69 	return batch->tx_descs + batch->reclaim_descs;
70 }
71 
72 /* The structure of the shared state of the rings are a simple
73  * circular buffer, as outlined in
74  * Documentation/core-api/circular-buffers.rst. For the Rx and
75  * completion ring, the kernel is the producer and user space is the
76  * consumer. For the Tx and fill rings, the kernel is the consumer and
77  * user space is the producer.
78  *
79  * producer                         consumer
80  *
81  * if (LOAD ->consumer) {  (A)      LOAD.acq ->producer  (C)
82  *    STORE $data                   LOAD $data
83  *    STORE.rel ->producer (B)      STORE.rel ->consumer (D)
84  * }
85  *
86  * (A) pairs with (D), and (B) pairs with (C).
87  *
88  * Starting with (B), it protects the data from being written after
89  * the producer pointer. If this barrier was missing, the consumer
90  * could observe the producer pointer being set and thus load the data
91  * before the producer has written the new data. The consumer would in
92  * this case load the old data.
93  *
94  * (C) protects the consumer from speculatively loading the data before
95  * the producer pointer actually has been read. If we do not have this
96  * barrier, some architectures could load old data as speculative loads
97  * are not discarded as the CPU does not know there is a dependency
98  * between ->producer and data.
99  *
100  * (A) is a control dependency that separates the load of ->consumer
101  * from the stores of $data. In case ->consumer indicates there is no
102  * room in the buffer to store $data we do not. The dependency will
103  * order both of the stores after the loads. So no barrier is needed.
104  *
105  * (D) protects the load of the data to be observed to happen after the
106  * store of the consumer pointer. If we did not have this memory
107  * barrier, the producer could observe the consumer pointer being set
108  * and overwrite the data with a new value before the consumer got the
109  * chance to read the old value. The consumer would thus miss reading
110  * the old entry and very likely read the new entry twice, once right
111  * now and again after circling through the ring.
112  */
113 
114 /* The operations on the rings are the following:
115  *
116  * producer                           consumer
117  *
118  * RESERVE entries                    PEEK in the ring for entries
119  * WRITE data into the ring           READ data from the ring
120  * SUBMIT entries                     RELEASE entries
121  *
122  * The producer reserves one or more entries in the ring. It can then
123  * fill in these entries and finally submit them so that they can be
124  * seen and read by the consumer.
125  *
126  * The consumer peeks into the ring to see if the producer has written
127  * any new entries. If so, the consumer can then read these entries
128  * and when it is done reading them release them back to the producer
129  * so that the producer can use these slots to fill in new entries.
130  *
131  * The function names below reflect these operations.
132  */
133 
134 /* Functions that read and validate content from consumer rings. */
135 
136 static inline void __xskq_cons_read_addr_unchecked(struct xsk_queue *q, u32 cached_cons, u64 *addr)
137 {
138 	struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring;
139 	u32 idx = cached_cons & q->ring_mask;
140 
141 	*addr = ring->desc[idx];
142 }
143 
144 static inline bool xskq_cons_read_addr_unchecked(struct xsk_queue *q, u64 *addr)
145 {
146 	if (q->cached_cons != q->cached_prod) {
147 		__xskq_cons_read_addr_unchecked(q, q->cached_cons, addr);
148 		return true;
149 	}
150 
151 	return false;
152 }
153 
154 static inline bool xp_unused_options_set(u32 options)
155 {
156 	return options & ~(XDP_PKT_CONTD | XDP_TX_METADATA);
157 }
158 
159 static inline bool xp_aligned_validate_desc(struct xsk_buff_pool *pool,
160 					    struct xdp_desc *desc)
161 {
162 	u64 len = desc->len;
163 	u64 addr, offset;
164 
165 	if (!len)
166 		return false;
167 
168 	/* Can overflow if desc->addr < pool->tx_metadata_len */
169 	if (check_sub_overflow(desc->addr, pool->tx_metadata_len, &addr))
170 		return false;
171 
172 	offset = addr & (pool->chunk_size - 1);
173 
174 	/*
175 	 * Can't overflow: @offset is guaranteed to be < ``U32_MAX``
176 	 * (pool->chunk_size is ``u32``), @len is guaranteed
177 	 * to be <= ``U32_MAX``.
178 	 */
179 	if (offset + len + pool->tx_metadata_len > pool->chunk_size)
180 		return false;
181 
182 	if (addr >= pool->addrs_cnt)
183 		return false;
184 
185 	if (xp_unused_options_set(desc->options))
186 		return false;
187 
188 	return true;
189 }
190 
191 static inline bool xp_unaligned_validate_desc(struct xsk_buff_pool *pool,
192 					      struct xdp_desc *desc)
193 {
194 	u64 len = desc->len;
195 	u64 addr, end;
196 
197 	if (!len)
198 		return false;
199 
200 	/* Can't overflow: @len is guaranteed to be <= ``U32_MAX`` */
201 	len += pool->tx_metadata_len;
202 	if (len > pool->chunk_size)
203 		return false;
204 
205 	/* Can overflow if desc->addr is close to 0 */
206 	if (check_sub_overflow(xp_unaligned_add_offset_to_addr(desc->addr),
207 			       pool->tx_metadata_len, &addr))
208 		return false;
209 
210 	if (addr >= pool->addrs_cnt)
211 		return false;
212 
213 	/* Can overflow if pool->addrs_cnt is high enough */
214 	if (check_add_overflow(addr, len, &end) || end > pool->addrs_cnt)
215 		return false;
216 
217 	if (xp_desc_crosses_non_contig_pg(pool, addr, len))
218 		return false;
219 
220 	if (xp_unused_options_set(desc->options))
221 		return false;
222 
223 	return true;
224 }
225 
226 static inline bool xp_validate_desc(struct xsk_buff_pool *pool,
227 				    struct xdp_desc *desc)
228 {
229 	return pool->unaligned ? xp_unaligned_validate_desc(pool, desc) :
230 		xp_aligned_validate_desc(pool, desc);
231 }
232 
233 static inline bool xskq_has_descs(struct xsk_queue *q)
234 {
235 	return q->cached_cons != q->cached_prod;
236 }
237 
238 static inline bool xskq_cons_is_valid_desc(struct xsk_queue *q,
239 					   struct xdp_desc *d,
240 					   struct xsk_buff_pool *pool)
241 {
242 	if (!xp_validate_desc(pool, d)) {
243 		q->invalid_descs++;
244 		return false;
245 	}
246 	return true;
247 }
248 
249 static inline bool xskq_cons_read_desc(struct xsk_queue *q,
250 				       struct xdp_desc *desc,
251 				       struct xsk_buff_pool *pool)
252 {
253 	if (q->cached_cons != q->cached_prod) {
254 		struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring;
255 		u32 idx = q->cached_cons & q->ring_mask;
256 
257 		*desc = ring->desc[idx];
258 		return xskq_cons_is_valid_desc(q, desc, pool);
259 	}
260 
261 	q->queue_empty_descs++;
262 	return false;
263 }
264 
265 static inline void xskq_cons_release_n(struct xsk_queue *q, u32 cnt)
266 {
267 	q->cached_cons += cnt;
268 }
269 
270 static inline void parse_desc(struct xsk_queue *q, struct xsk_buff_pool *pool,
271 			      struct xdp_desc *desc, struct parsed_desc *parsed)
272 {
273 	parsed->valid = xskq_cons_is_valid_desc(q, desc, pool);
274 	parsed->mb = xp_mb_desc(desc);
275 }
276 
277 static inline struct xsk_tx_batch
278 xskq_cons_read_desc_batch(struct xdp_sock *xs, struct xsk_buff_pool *pool,
279 			  struct xdp_desc *descs, u32 max)
280 {
281 	bool drain = READ_ONCE(xs->drain_cont);
282 	u32 cached_cons, nb_entries = 0;
283 	struct xsk_tx_batch batch = {};
284 	struct xsk_queue *q = xs->tx;
285 	u32 nr_frags = 0;
286 
287 	cached_cons = q->cached_cons;
288 
289 	while (cached_cons != q->cached_prod && nb_entries < max) {
290 		struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring;
291 		u32 idx = cached_cons & q->ring_mask;
292 		struct parsed_desc parsed;
293 
294 		descs[nb_entries] = ring->desc[idx];
295 		cached_cons++;
296 		parse_desc(q, pool, &descs[nb_entries], &parsed);
297 		if (unlikely(!parsed.valid))
298 			drain = true;
299 
300 		nr_frags++;
301 		nb_entries++;
302 
303 		if (likely(!parsed.mb)) {
304 			if (unlikely(drain)) {
305 				batch.reclaim_descs = nr_frags;
306 				WRITE_ONCE(xs->drain_cont, false);
307 				nr_frags = 0;
308 				break;
309 			}
310 
311 			batch.tx_descs += nr_frags;
312 			nr_frags = 0;
313 			continue;
314 		}
315 
316 		if (nr_frags == pool->xdp_zc_max_segs)
317 			drain = true;
318 	}
319 
320 	if (nr_frags) {
321 		if (drain) {
322 			batch.reclaim_descs = nr_frags;
323 			WRITE_ONCE(xs->drain_cont, true);
324 		} else {
325 			if (nb_entries == max)
326 				batch.budget_limited = true;
327 			cached_cons -= nr_frags;
328 		}
329 	}
330 
331 	/* Release valid plus any invalid entries */
332 	xskq_cons_release_n(q, cached_cons - q->cached_cons);
333 	return batch;
334 }
335 
336 /* Functions for consumers */
337 
338 static inline void __xskq_cons_release(struct xsk_queue *q)
339 {
340 	smp_store_release(&q->ring->consumer, q->cached_cons); /* D, matchees A */
341 }
342 
343 static inline void __xskq_cons_peek(struct xsk_queue *q)
344 {
345 	/* Refresh the local pointer */
346 	q->cached_prod = smp_load_acquire(&q->ring->producer);  /* C, matches B */
347 }
348 
349 static inline void xskq_cons_get_entries(struct xsk_queue *q)
350 {
351 	__xskq_cons_release(q);
352 	__xskq_cons_peek(q);
353 }
354 
355 static inline u32 xskq_cons_nb_entries(struct xsk_queue *q, u32 max)
356 {
357 	u32 entries = q->cached_prod - q->cached_cons;
358 
359 	if (entries >= max)
360 		return max;
361 
362 	__xskq_cons_peek(q);
363 	entries = q->cached_prod - q->cached_cons;
364 
365 	return entries >= max ? max : entries;
366 }
367 
368 static inline bool xskq_cons_peek_addr_unchecked(struct xsk_queue *q, u64 *addr)
369 {
370 	if (q->cached_prod == q->cached_cons)
371 		xskq_cons_get_entries(q);
372 	return xskq_cons_read_addr_unchecked(q, addr);
373 }
374 
375 static inline bool xskq_cons_peek_desc(struct xsk_queue *q,
376 				       struct xdp_desc *desc,
377 				       struct xsk_buff_pool *pool)
378 {
379 	if (q->cached_prod == q->cached_cons)
380 		xskq_cons_get_entries(q);
381 	return xskq_cons_read_desc(q, desc, pool);
382 }
383 
384 /* To improve performance in the xskq_cons_release functions, only update local state here.
385  * Reflect this to global state when we get new entries from the ring in
386  * xskq_cons_get_entries() and whenever Rx or Tx processing are completed in the NAPI loop.
387  */
388 static inline void xskq_cons_release(struct xsk_queue *q)
389 {
390 	q->cached_cons++;
391 }
392 
393 static inline void xskq_cons_cancel_n(struct xsk_queue *q, u32 cnt)
394 {
395 	q->cached_cons -= cnt;
396 }
397 
398 static inline u32 xskq_cons_present_entries(struct xsk_queue *q)
399 {
400 	/* No barriers needed since data is not accessed */
401 	return READ_ONCE(q->ring->producer) - READ_ONCE(q->ring->consumer);
402 }
403 
404 /* Functions for producers */
405 
406 static inline u32 xskq_get_prod(struct xsk_queue *q)
407 {
408 	return READ_ONCE(q->ring->producer);
409 }
410 
411 static inline u32 xskq_prod_nb_free(struct xsk_queue *q, u32 max)
412 {
413 	u32 free_entries = q->nentries - (q->cached_prod - q->cached_cons);
414 
415 	if (free_entries >= max)
416 		return max;
417 
418 	/* Refresh the local tail pointer */
419 	q->cached_cons = READ_ONCE(q->ring->consumer);
420 	free_entries = q->nentries - (q->cached_prod - q->cached_cons);
421 
422 	return free_entries >= max ? max : free_entries;
423 }
424 
425 static inline bool xskq_prod_is_full(struct xsk_queue *q)
426 {
427 	return xskq_prod_nb_free(q, 1) ? false : true;
428 }
429 
430 static inline void xskq_prod_cancel_n(struct xsk_queue *q, u32 cnt)
431 {
432 	q->cached_prod -= cnt;
433 }
434 
435 static inline int xskq_prod_reserve(struct xsk_queue *q)
436 {
437 	if (xskq_prod_is_full(q))
438 		return -ENOSPC;
439 
440 	/* A, matches D */
441 	q->cached_prod++;
442 	return 0;
443 }
444 
445 static inline int xskq_prod_reserve_addr(struct xsk_queue *q, u64 addr)
446 {
447 	struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring;
448 
449 	if (xskq_prod_is_full(q))
450 		return -ENOSPC;
451 
452 	/* A, matches D */
453 	ring->desc[q->cached_prod++ & q->ring_mask] = addr;
454 	return 0;
455 }
456 
457 static inline void xskq_prod_write_addr(struct xsk_queue *q, u32 idx, u64 addr)
458 {
459 	struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring;
460 
461 	ring->desc[idx & q->ring_mask] = addr;
462 }
463 
464 static inline void xskq_prod_write_addr_batch(struct xsk_queue *q, struct xdp_desc *descs,
465 					      u32 nb_entries)
466 {
467 	struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring;
468 	u32 i, cached_prod;
469 
470 	/* A, matches D */
471 	cached_prod = q->cached_prod;
472 	for (i = 0; i < nb_entries; i++)
473 		ring->desc[cached_prod++ & q->ring_mask] = descs[i].addr;
474 	q->cached_prod = cached_prod;
475 }
476 
477 static inline void __xskq_prod_reserve_desc(struct xsk_queue *q,
478 					    u64 addr, u32 len, u32 flags)
479 {
480 	struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring;
481 	u32 idx;
482 
483 	/* A, matches D */
484 	idx = q->cached_prod++ & q->ring_mask;
485 	ring->desc[idx].addr = addr;
486 	ring->desc[idx].len = len;
487 	ring->desc[idx].options = flags;
488 }
489 
490 static inline int xskq_prod_reserve_desc(struct xsk_queue *q,
491 					 u64 addr, u32 len, u32 flags)
492 {
493 	if (xskq_prod_is_full(q))
494 		return -ENOBUFS;
495 
496 	__xskq_prod_reserve_desc(q, addr, len, flags);
497 
498 	return 0;
499 }
500 
501 static inline void __xskq_prod_submit(struct xsk_queue *q, u32 idx)
502 {
503 	smp_store_release(&q->ring->producer, idx); /* B, matches C */
504 }
505 
506 static inline void xskq_prod_submit(struct xsk_queue *q)
507 {
508 	__xskq_prod_submit(q, q->cached_prod);
509 }
510 
511 static inline void xskq_prod_submit_n(struct xsk_queue *q, u32 nb_entries)
512 {
513 	__xskq_prod_submit(q, q->ring->producer + nb_entries);
514 }
515 
516 static inline bool xskq_prod_is_empty(struct xsk_queue *q)
517 {
518 	/* No barriers needed since data is not accessed */
519 	return READ_ONCE(q->ring->consumer) == READ_ONCE(q->ring->producer);
520 }
521 
522 /* For both producers and consumers */
523 
524 static inline u64 xskq_nb_invalid_descs(struct xsk_queue *q)
525 {
526 	return q ? q->invalid_descs : 0;
527 }
528 
529 static inline u64 xskq_nb_queue_empty_descs(struct xsk_queue *q)
530 {
531 	return q ? q->queue_empty_descs : 0;
532 }
533 
534 struct xsk_queue *xskq_create(u32 nentries, bool umem_queue);
535 void xskq_destroy(struct xsk_queue *q_ops);
536 
537 #endif /* _LINUX_XSK_QUEUE_H */
538