blob: 2823b7c3302d0a24db4ce1f0720ca8094693bde7 [file] [log] [blame]
Björn Töpeldac091492018-05-18 14:00:21 +02001/* SPDX-License-Identifier: GPL-2.0 */
2/* XDP user-space ring structure
Magnus Karlsson423f3832018-05-02 13:01:24 +02003 * Copyright(c) 2018 Intel Corporation.
Magnus Karlsson423f3832018-05-02 13:01:24 +02004 */
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>
Björn Töpele61e62b92018-06-04 14:05:51 +020011#include <net/xdp_sock.h>
Björn Töpel2b434702020-05-20 21:20:53 +020012#include <net/xsk_buff_pool.h>
Magnus Karlsson423f3832018-05-02 13:01:24 +020013
Björn Töpel89e4a372020-05-20 21:20:52 +020014#include "xsk.h"
15
Björn Töpelb3a9e0b2018-05-22 09:34:59 +020016struct xdp_ring {
17 u32 producer ____cacheline_aligned_in_smp;
Magnus Karlssonc3f01fd2020-10-08 16:12:18 +020018 /* Hinder the adjacent cache prefetcher to prefetch the consumer
19 * pointer if the producer pointer is touched and vice versa.
20 */
Magnus Karlssonb8c7aec2020-11-16 12:12:45 +010021 u32 pad1 ____cacheline_aligned_in_smp;
Björn Töpelb3a9e0b2018-05-22 09:34:59 +020022 u32 consumer ____cacheline_aligned_in_smp;
Magnus Karlssonb8c7aec2020-11-16 12:12:45 +010023 u32 pad2 ____cacheline_aligned_in_smp;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020024 u32 flags;
Magnus Karlssonb8c7aec2020-11-16 12:12:45 +010025 u32 pad3 ____cacheline_aligned_in_smp;
Björn Töpelb3a9e0b2018-05-22 09:34:59 +020026};
27
28/* Used for the RX and TX queues for packets */
29struct xdp_rxtx_ring {
30 struct xdp_ring ptrs;
Gustavo A. R. Silva95e486f2020-02-28 07:19:07 -060031 struct xdp_desc desc[] ____cacheline_aligned_in_smp;
Björn Töpelb3a9e0b2018-05-22 09:34:59 +020032};
33
34/* Used for the fill and completion queues for buffers */
35struct xdp_umem_ring {
36 struct xdp_ring ptrs;
Gustavo A. R. Silva95e486f2020-02-28 07:19:07 -060037 u64 desc[] ____cacheline_aligned_in_smp;
Björn Töpelb3a9e0b2018-05-22 09:34:59 +020038};
39
Magnus Karlsson423f3832018-05-02 13:01:24 +020040struct xsk_queue {
Magnus Karlsson423f3832018-05-02 13:01:24 +020041 u32 ring_mask;
42 u32 nentries;
Magnus Karlssond7012f02019-12-19 13:39:22 +010043 u32 cached_prod;
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +010044 u32 cached_cons;
Magnus Karlsson423f3832018-05-02 13:01:24 +020045 struct xdp_ring *ring;
46 u64 invalid_descs;
Ciara Loftus8aa5a332020-07-08 07:28:33 +000047 u64 queue_empty_descs;
Magnus Karlsson423f3832018-05-02 13:01:24 +020048};
49
Magnus Karlssonf63666de2019-04-16 14:58:08 +020050/* The structure of the shared state of the rings are the same as the
51 * ring buffer in kernel/events/ring_buffer.c. For the Rx and completion
52 * ring, the kernel is the producer and user space is the consumer. For
53 * the Tx and fill rings, the kernel is the consumer and user space is
54 * the producer.
55 *
56 * producer consumer
57 *
58 * if (LOAD ->consumer) { LOAD ->producer
59 * (A) smp_rmb() (C)
60 * STORE $data LOAD $data
61 * smp_wmb() (B) smp_mb() (D)
62 * STORE ->producer STORE ->consumer
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. So no barrier is needed.
82 *
83 * (D) protects the load of the data to be observed to happen after the
84 * store of the consumer pointer. If we did not have this memory
85 * barrier, the producer could observe the consumer pointer being set
86 * and overwrite the data with a new value before the consumer got the
87 * chance to read the old value. The consumer would thus miss reading
88 * the old entry and very likely read the new entry twice, once right
89 * now and again after circling through the ring.
90 */
91
Magnus Karlsson15d8c912019-12-19 13:39:30 +010092/* The operations on the rings are the following:
93 *
94 * producer consumer
95 *
96 * RESERVE entries PEEK in the ring for entries
97 * WRITE data into the ring READ data from the ring
98 * SUBMIT entries RELEASE entries
99 *
100 * The producer reserves one or more entries in the ring. It can then
101 * fill in these entries and finally submit them so that they can be
102 * seen and read by the consumer.
103 *
104 * The consumer peeks into the ring to see if the producer has written
Ciara Loftusf1fc8ec2020-09-28 08:23:44 +0000105 * any new entries. If so, the consumer can then read these entries
Magnus Karlsson15d8c912019-12-19 13:39:30 +0100106 * and when it is done reading them release them back to the producer
107 * so that the producer can use these slots to fill in new entries.
108 *
109 * The function names below reflect these operations.
110 */
Björn Töpelc4971762018-05-02 13:01:27 +0200111
Magnus Karlsson15d8c912019-12-19 13:39:30 +0100112/* Functions that read and validate content from consumer rings. */
Björn Töpelc4971762018-05-02 13:01:27 +0200113
Björn Töpel2b434702020-05-20 21:20:53 +0200114static inline bool xskq_cons_read_addr_unchecked(struct xsk_queue *q, u64 *addr)
115{
116 struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring;
117
118 if (q->cached_cons != q->cached_prod) {
119 u32 idx = q->cached_cons & q->ring_mask;
120
121 *addr = ring->desc[idx];
122 return true;
123 }
124
125 return false;
126}
127
Björn Töpel26062b12020-05-20 21:21:02 +0200128static inline bool xp_aligned_validate_desc(struct xsk_buff_pool *pool,
129 struct xdp_desc *desc)
130{
131 u64 chunk, chunk_end;
132
133 chunk = xp_aligned_extract_addr(pool, desc->addr);
134 chunk_end = xp_aligned_extract_addr(pool, desc->addr + desc->len);
135 if (chunk != chunk_end)
136 return false;
137
138 if (chunk >= pool->addrs_cnt)
139 return false;
140
141 if (desc->options)
142 return false;
143 return true;
144}
145
146static inline bool xp_unaligned_validate_desc(struct xsk_buff_pool *pool,
147 struct xdp_desc *desc)
148{
149 u64 addr, base_addr;
150
151 base_addr = xp_unaligned_extract_addr(desc->addr);
152 addr = xp_unaligned_add_offset_to_addr(desc->addr);
153
154 if (desc->len > pool->chunk_size)
155 return false;
156
157 if (base_addr >= pool->addrs_cnt || addr >= pool->addrs_cnt ||
158 xp_desc_crosses_non_contig_pg(pool, addr, desc->len))
159 return false;
160
161 if (desc->options)
162 return false;
163 return true;
164}
165
166static inline bool xp_validate_desc(struct xsk_buff_pool *pool,
167 struct xdp_desc *desc)
168{
169 return pool->unaligned ? xp_unaligned_validate_desc(pool, desc) :
170 xp_aligned_validate_desc(pool, desc);
171}
172
Magnus Karlsson03896ef2019-12-19 13:39:27 +0100173static inline bool xskq_cons_is_valid_desc(struct xsk_queue *q,
174 struct xdp_desc *d,
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200175 struct xsk_buff_pool *pool)
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200176{
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200177 if (!xp_validate_desc(pool, d)) {
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200178 q->invalid_descs++;
179 return false;
180 }
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200181 return true;
182}
183
Magnus Karlsson03896ef2019-12-19 13:39:27 +0100184static inline bool xskq_cons_read_desc(struct xsk_queue *q,
185 struct xdp_desc *desc,
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200186 struct xsk_buff_pool *pool)
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200187{
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +0100188 while (q->cached_cons != q->cached_prod) {
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200189 struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring;
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +0100190 u32 idx = q->cached_cons & q->ring_mask;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200191
Magnus Karlssonc34787f2019-12-19 13:39:29 +0100192 *desc = ring->desc[idx];
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200193 if (xskq_cons_is_valid_desc(q, desc, pool))
Magnus Karlsson03896ef2019-12-19 13:39:27 +0100194 return true;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200195
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +0100196 q->cached_cons++;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200197 }
198
Magnus Karlsson03896ef2019-12-19 13:39:27 +0100199 return false;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200200}
201
Magnus Karlsson9349eb32020-11-16 12:12:46 +0100202static inline u32 xskq_cons_read_desc_batch(struct xsk_queue *q,
203 struct xdp_desc *descs,
204 struct xsk_buff_pool *pool, u32 max)
205{
206 u32 cached_cons = q->cached_cons, nb_entries = 0;
207
208 while (cached_cons != q->cached_prod && nb_entries < max) {
209 struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring;
210 u32 idx = cached_cons & q->ring_mask;
211
212 descs[nb_entries] = ring->desc[idx];
213 if (unlikely(!xskq_cons_is_valid_desc(q, &descs[nb_entries], pool))) {
214 /* Skip the entry */
215 cached_cons++;
216 continue;
217 }
218
219 nb_entries++;
220 cached_cons++;
221 }
222
223 return nb_entries;
224}
225
Magnus Karlsson15d8c912019-12-19 13:39:30 +0100226/* Functions for consumers */
227
228static inline void __xskq_cons_release(struct xsk_queue *q)
229{
230 smp_mb(); /* D, matches A */
231 WRITE_ONCE(q->ring->consumer, q->cached_cons);
232}
233
234static inline void __xskq_cons_peek(struct xsk_queue *q)
235{
236 /* Refresh the local pointer */
237 q->cached_prod = READ_ONCE(q->ring->producer);
238 smp_rmb(); /* C, matches B */
239}
240
241static inline void xskq_cons_get_entries(struct xsk_queue *q)
242{
243 __xskq_cons_release(q);
244 __xskq_cons_peek(q);
245}
246
Magnus Karlsson9349eb32020-11-16 12:12:46 +0100247static inline u32 xskq_cons_nb_entries(struct xsk_queue *q, u32 max)
Magnus Karlsson15d8c912019-12-19 13:39:30 +0100248{
249 u32 entries = q->cached_prod - q->cached_cons;
250
Magnus Karlsson9349eb32020-11-16 12:12:46 +0100251 if (entries >= max)
252 return max;
Magnus Karlsson15d8c912019-12-19 13:39:30 +0100253
254 __xskq_cons_peek(q);
255 entries = q->cached_prod - q->cached_cons;
256
Magnus Karlsson9349eb32020-11-16 12:12:46 +0100257 return entries >= max ? max : entries;
258}
259
260static inline bool xskq_cons_has_entries(struct xsk_queue *q, u32 cnt)
261{
262 return xskq_cons_nb_entries(q, cnt) >= cnt ? true : false;
Magnus Karlsson15d8c912019-12-19 13:39:30 +0100263}
264
Björn Töpel2b434702020-05-20 21:20:53 +0200265static inline bool xskq_cons_peek_addr_unchecked(struct xsk_queue *q, u64 *addr)
266{
267 if (q->cached_prod == q->cached_cons)
268 xskq_cons_get_entries(q);
269 return xskq_cons_read_addr_unchecked(q, addr);
270}
271
Magnus Karlsson03896ef2019-12-19 13:39:27 +0100272static inline bool xskq_cons_peek_desc(struct xsk_queue *q,
273 struct xdp_desc *desc,
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200274 struct xsk_buff_pool *pool)
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200275{
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +0100276 if (q->cached_prod == q->cached_cons)
277 xskq_cons_get_entries(q);
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200278 return xskq_cons_read_desc(q, desc, pool);
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200279}
280
Magnus Karlsson9349eb32020-11-16 12:12:46 +0100281static inline u32 xskq_cons_peek_desc_batch(struct xsk_queue *q, struct xdp_desc *descs,
282 struct xsk_buff_pool *pool, u32 max)
283{
284 u32 entries = xskq_cons_nb_entries(q, max);
285
286 return xskq_cons_read_desc_batch(q, descs, pool, entries);
287}
288
289/* To improve performance in the xskq_cons_release functions, only update local state here.
290 * Reflect this to global state when we get new entries from the ring in
291 * xskq_cons_get_entries() and whenever Rx or Tx processing are completed in the NAPI loop.
292 */
Magnus Karlsson15d8c912019-12-19 13:39:30 +0100293static inline void xskq_cons_release(struct xsk_queue *q)
294{
Magnus Karlsson15d8c912019-12-19 13:39:30 +0100295 q->cached_cons++;
296}
297
Magnus Karlsson9349eb32020-11-16 12:12:46 +0100298static inline void xskq_cons_release_n(struct xsk_queue *q, u32 cnt)
299{
300 q->cached_cons += cnt;
301}
302
Magnus Karlsson15d8c912019-12-19 13:39:30 +0100303static inline bool xskq_cons_is_full(struct xsk_queue *q)
304{
305 /* No barriers needed since data is not accessed */
306 return READ_ONCE(q->ring->producer) - READ_ONCE(q->ring->consumer) ==
307 q->nentries;
308}
309
Xuan Zhuo3413f042020-12-01 21:56:58 +0800310static inline u32 xskq_cons_present_entries(struct xsk_queue *q)
311{
312 /* No barriers needed since data is not accessed */
313 return READ_ONCE(q->ring->producer) - READ_ONCE(q->ring->consumer);
314}
315
Magnus Karlsson15d8c912019-12-19 13:39:30 +0100316/* Functions for producers */
317
Magnus Karlsson9349eb32020-11-16 12:12:46 +0100318static inline u32 xskq_prod_nb_free(struct xsk_queue *q, u32 max)
Magnus Karlsson15d8c912019-12-19 13:39:30 +0100319{
320 u32 free_entries = q->nentries - (q->cached_prod - q->cached_cons);
321
Magnus Karlsson9349eb32020-11-16 12:12:46 +0100322 if (free_entries >= max)
323 return max;
Magnus Karlsson15d8c912019-12-19 13:39:30 +0100324
325 /* Refresh the local tail pointer */
326 q->cached_cons = READ_ONCE(q->ring->consumer);
327 free_entries = q->nentries - (q->cached_prod - q->cached_cons);
328
Magnus Karlsson9349eb32020-11-16 12:12:46 +0100329 return free_entries >= max ? max : free_entries;
330}
331
332static inline bool xskq_prod_is_full(struct xsk_queue *q)
333{
334 return xskq_prod_nb_free(q, 1) ? false : true;
Magnus Karlsson15d8c912019-12-19 13:39:30 +0100335}
336
Magnus Karlssonb1b95cb2020-12-18 14:45:25 +0100337static inline void xskq_prod_cancel(struct xsk_queue *q)
338{
339 q->cached_prod--;
340}
341
Magnus Karlsson15d8c912019-12-19 13:39:30 +0100342static inline int xskq_prod_reserve(struct xsk_queue *q)
343{
344 if (xskq_prod_is_full(q))
345 return -ENOSPC;
346
347 /* A, matches D */
348 q->cached_prod++;
349 return 0;
350}
351
352static inline int xskq_prod_reserve_addr(struct xsk_queue *q, u64 addr)
353{
354 struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring;
355
356 if (xskq_prod_is_full(q))
357 return -ENOSPC;
358
359 /* A, matches D */
360 ring->desc[q->cached_prod++ & q->ring_mask] = addr;
361 return 0;
362}
363
Magnus Karlsson9349eb32020-11-16 12:12:46 +0100364static inline u32 xskq_prod_reserve_addr_batch(struct xsk_queue *q, struct xdp_desc *descs,
365 u32 max)
366{
367 struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring;
368 u32 nb_entries, i, cached_prod;
369
370 nb_entries = xskq_prod_nb_free(q, max);
371
372 /* A, matches D */
373 cached_prod = q->cached_prod;
374 for (i = 0; i < nb_entries; i++)
375 ring->desc[cached_prod++ & q->ring_mask] = descs[i].addr;
376 q->cached_prod = cached_prod;
377
378 return nb_entries;
379}
380
Magnus Karlsson59e35e52019-12-19 13:39:23 +0100381static inline int xskq_prod_reserve_desc(struct xsk_queue *q,
382 u64 addr, u32 len)
Björn Töpelc4971762018-05-02 13:01:27 +0200383{
384 struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring;
Magnus Karlsson59e35e52019-12-19 13:39:23 +0100385 u32 idx;
Björn Töpelc4971762018-05-02 13:01:27 +0200386
Magnus Karlssondf0ae6f2019-12-19 13:39:25 +0100387 if (xskq_prod_is_full(q))
Björn Töpelc4971762018-05-02 13:01:27 +0200388 return -ENOSPC;
389
Magnus Karlssonf63666de2019-04-16 14:58:08 +0200390 /* A, matches D */
Magnus Karlssond7012f02019-12-19 13:39:22 +0100391 idx = q->cached_prod++ & q->ring_mask;
Björn Töpelbbff2f32018-06-04 13:57:13 +0200392 ring->desc[idx].addr = addr;
Björn Töpelc4971762018-05-02 13:01:27 +0200393 ring->desc[idx].len = len;
Björn Töpelc4971762018-05-02 13:01:27 +0200394
395 return 0;
396}
397
Magnus Karlsson15d8c912019-12-19 13:39:30 +0100398static inline void __xskq_prod_submit(struct xsk_queue *q, u32 idx)
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200399{
Magnus Karlsson15d8c912019-12-19 13:39:30 +0100400 smp_wmb(); /* B, matches C */
401
402 WRITE_ONCE(q->ring->producer, idx);
403}
404
405static inline void xskq_prod_submit(struct xsk_queue *q)
406{
407 __xskq_prod_submit(q, q->cached_prod);
408}
409
410static inline void xskq_prod_submit_addr(struct xsk_queue *q, u64 addr)
411{
412 struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring;
413 u32 idx = q->ring->producer;
414
415 ring->desc[idx++ & q->ring_mask] = addr;
416
417 __xskq_prod_submit(q, idx);
418}
419
420static inline void xskq_prod_submit_n(struct xsk_queue *q, u32 nb_entries)
421{
422 __xskq_prod_submit(q, q->ring->producer + nb_entries);
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200423}
424
Magnus Karlsson59e35e52019-12-19 13:39:23 +0100425static inline bool xskq_prod_is_empty(struct xsk_queue *q)
Björn Töpelc4971762018-05-02 13:01:27 +0200426{
Magnus Karlsson11cc2d22019-12-19 13:39:21 +0100427 /* No barriers needed since data is not accessed */
428 return READ_ONCE(q->ring->consumer) == READ_ONCE(q->ring->producer);
Björn Töpelc4971762018-05-02 13:01:27 +0200429}
430
Magnus Karlsson15d8c912019-12-19 13:39:30 +0100431/* For both producers and consumers */
432
433static inline u64 xskq_nb_invalid_descs(struct xsk_queue *q)
434{
435 return q ? q->invalid_descs : 0;
436}
437
Ciara Loftus8aa5a332020-07-08 07:28:33 +0000438static inline u64 xskq_nb_queue_empty_descs(struct xsk_queue *q)
439{
440 return q ? q->queue_empty_descs : 0;
441}
442
Björn Töpelb9b6b682018-05-02 13:01:25 +0200443struct xsk_queue *xskq_create(u32 nentries, bool umem_queue);
Björn Töpelc4971762018-05-02 13:01:27 +0200444void xskq_destroy(struct xsk_queue *q_ops);
Magnus Karlsson423f3832018-05-02 13:01:24 +0200445
Magnus Karlsson423f3832018-05-02 13:01:24 +0200446#endif /* _LINUX_XSK_QUEUE_H */