xref: /openbmc/linux/net/xdp/xsk_queue.h (revision cbafa54a)
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 };
49 
50 /* The structure of the shared state of the rings are a simple
51  * circular buffer, as outlined in
52  * Documentation/core-api/circular-buffers.rst. For the Rx and
53  * completion ring, the kernel is the producer and user space is the
54  * consumer. For the Tx and fill rings, the kernel is the consumer and
55  * user space is the producer.
56  *
57  * producer                         consumer
58  *
59  * if (LOAD ->consumer) {  (A)      LOAD.acq ->producer  (C)
60  *    STORE $data                   LOAD $data
61  *    STORE.rel ->producer (B)      STORE.rel ->consumer (D)
62  * }
63  *
64  * (A) pairs with (D), and (B) pairs with (C).
65  *
66  * Starting with (B), it protects the data from being written after
67  * the producer pointer. If this barrier was missing, the consumer
68  * could observe the producer pointer being set and thus load the data
69  * before the producer has written the new data. The consumer would in
70  * this case load the old data.
71  *
72  * (C) protects the consumer from speculatively loading the data before
73  * the producer pointer actually has been read. If we do not have this
74  * barrier, some architectures could load old data as speculative loads
75  * are not discarded as the CPU does not know there is a dependency
76  * between ->producer and data.
77  *
78  * (A) is a control dependency that separates the load of ->consumer
79  * from the stores of $data. In case ->consumer indicates there is no
80  * room in the buffer to store $data we do not. The dependency will
81  * order both of the stores after the loads. 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 
92 /* 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
105  * any new entries. If so, the consumer can then read these entries
106  * 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  */
111 
112 /* Functions that read and validate content from consumer rings. */
113 
114 static 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 
128 static inline bool xp_aligned_validate_desc(struct xsk_buff_pool *pool,
129 					    struct xdp_desc *desc)
130 {
131 	u64 chunk;
132 
133 	if (desc->len > pool->chunk_size)
134 		return false;
135 
136 	chunk = xp_aligned_extract_addr(pool, desc->addr);
137 	if (chunk >= pool->addrs_cnt)
138 		return false;
139 
140 	if (desc->options)
141 		return false;
142 	return true;
143 }
144 
145 static inline bool xp_unaligned_validate_desc(struct xsk_buff_pool *pool,
146 					      struct xdp_desc *desc)
147 {
148 	u64 addr, base_addr;
149 
150 	base_addr = xp_unaligned_extract_addr(desc->addr);
151 	addr = xp_unaligned_add_offset_to_addr(desc->addr);
152 
153 	if (desc->len > pool->chunk_size)
154 		return false;
155 
156 	if (base_addr >= pool->addrs_cnt || addr >= pool->addrs_cnt ||
157 	    xp_desc_crosses_non_contig_pg(pool, addr, desc->len))
158 		return false;
159 
160 	if (desc->options)
161 		return false;
162 	return true;
163 }
164 
165 static inline bool xp_validate_desc(struct xsk_buff_pool *pool,
166 				    struct xdp_desc *desc)
167 {
168 	return pool->unaligned ? xp_unaligned_validate_desc(pool, desc) :
169 		xp_aligned_validate_desc(pool, desc);
170 }
171 
172 static inline bool xskq_cons_is_valid_desc(struct xsk_queue *q,
173 					   struct xdp_desc *d,
174 					   struct xsk_buff_pool *pool)
175 {
176 	if (!xp_validate_desc(pool, d)) {
177 		q->invalid_descs++;
178 		return false;
179 	}
180 	return true;
181 }
182 
183 static inline bool xskq_cons_read_desc(struct xsk_queue *q,
184 				       struct xdp_desc *desc,
185 				       struct xsk_buff_pool *pool)
186 {
187 	while (q->cached_cons != q->cached_prod) {
188 		struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring;
189 		u32 idx = q->cached_cons & q->ring_mask;
190 
191 		*desc = ring->desc[idx];
192 		if (xskq_cons_is_valid_desc(q, desc, pool))
193 			return true;
194 
195 		q->cached_cons++;
196 	}
197 
198 	return false;
199 }
200 
201 static inline u32 xskq_cons_read_desc_batch(struct xsk_queue *q,
202 					    struct xdp_desc *descs,
203 					    struct xsk_buff_pool *pool, u32 max)
204 {
205 	u32 cached_cons = q->cached_cons, nb_entries = 0;
206 
207 	while (cached_cons != q->cached_prod && nb_entries < max) {
208 		struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring;
209 		u32 idx = cached_cons & q->ring_mask;
210 
211 		descs[nb_entries] = ring->desc[idx];
212 		if (unlikely(!xskq_cons_is_valid_desc(q, &descs[nb_entries], pool))) {
213 			/* Skip the entry */
214 			cached_cons++;
215 			continue;
216 		}
217 
218 		nb_entries++;
219 		cached_cons++;
220 	}
221 
222 	return nb_entries;
223 }
224 
225 /* Functions for consumers */
226 
227 static inline void __xskq_cons_release(struct xsk_queue *q)
228 {
229 	smp_store_release(&q->ring->consumer, q->cached_cons); /* D, matchees A */
230 }
231 
232 static inline void __xskq_cons_peek(struct xsk_queue *q)
233 {
234 	/* Refresh the local pointer */
235 	q->cached_prod = smp_load_acquire(&q->ring->producer);  /* C, matches B */
236 }
237 
238 static inline void xskq_cons_get_entries(struct xsk_queue *q)
239 {
240 	__xskq_cons_release(q);
241 	__xskq_cons_peek(q);
242 }
243 
244 static inline u32 xskq_cons_nb_entries(struct xsk_queue *q, u32 max)
245 {
246 	u32 entries = q->cached_prod - q->cached_cons;
247 
248 	if (entries >= max)
249 		return max;
250 
251 	__xskq_cons_peek(q);
252 	entries = q->cached_prod - q->cached_cons;
253 
254 	return entries >= max ? max : entries;
255 }
256 
257 static inline bool xskq_cons_has_entries(struct xsk_queue *q, u32 cnt)
258 {
259 	return xskq_cons_nb_entries(q, cnt) >= cnt ? true : false;
260 }
261 
262 static inline bool xskq_cons_peek_addr_unchecked(struct xsk_queue *q, u64 *addr)
263 {
264 	if (q->cached_prod == q->cached_cons)
265 		xskq_cons_get_entries(q);
266 	return xskq_cons_read_addr_unchecked(q, addr);
267 }
268 
269 static inline bool xskq_cons_peek_desc(struct xsk_queue *q,
270 				       struct xdp_desc *desc,
271 				       struct xsk_buff_pool *pool)
272 {
273 	if (q->cached_prod == q->cached_cons)
274 		xskq_cons_get_entries(q);
275 	return xskq_cons_read_desc(q, desc, pool);
276 }
277 
278 static inline u32 xskq_cons_peek_desc_batch(struct xsk_queue *q, struct xdp_desc *descs,
279 					    struct xsk_buff_pool *pool, u32 max)
280 {
281 	u32 entries = xskq_cons_nb_entries(q, max);
282 
283 	return xskq_cons_read_desc_batch(q, descs, pool, entries);
284 }
285 
286 /* To improve performance in the xskq_cons_release functions, only update local state here.
287  * Reflect this to global state when we get new entries from the ring in
288  * xskq_cons_get_entries() and whenever Rx or Tx processing are completed in the NAPI loop.
289  */
290 static inline void xskq_cons_release(struct xsk_queue *q)
291 {
292 	q->cached_cons++;
293 }
294 
295 static inline void xskq_cons_release_n(struct xsk_queue *q, u32 cnt)
296 {
297 	q->cached_cons += cnt;
298 }
299 
300 static inline bool xskq_cons_is_full(struct xsk_queue *q)
301 {
302 	/* No barriers needed since data is not accessed */
303 	return READ_ONCE(q->ring->producer) - READ_ONCE(q->ring->consumer) ==
304 		q->nentries;
305 }
306 
307 static inline u32 xskq_cons_present_entries(struct xsk_queue *q)
308 {
309 	/* No barriers needed since data is not accessed */
310 	return READ_ONCE(q->ring->producer) - READ_ONCE(q->ring->consumer);
311 }
312 
313 /* Functions for producers */
314 
315 static inline u32 xskq_prod_nb_free(struct xsk_queue *q, u32 max)
316 {
317 	u32 free_entries = q->nentries - (q->cached_prod - q->cached_cons);
318 
319 	if (free_entries >= max)
320 		return max;
321 
322 	/* Refresh the local tail pointer */
323 	q->cached_cons = READ_ONCE(q->ring->consumer);
324 	free_entries = q->nentries - (q->cached_prod - q->cached_cons);
325 
326 	return free_entries >= max ? max : free_entries;
327 }
328 
329 static inline bool xskq_prod_is_full(struct xsk_queue *q)
330 {
331 	return xskq_prod_nb_free(q, 1) ? false : true;
332 }
333 
334 static inline void xskq_prod_cancel(struct xsk_queue *q)
335 {
336 	q->cached_prod--;
337 }
338 
339 static inline int xskq_prod_reserve(struct xsk_queue *q)
340 {
341 	if (xskq_prod_is_full(q))
342 		return -ENOSPC;
343 
344 	/* A, matches D */
345 	q->cached_prod++;
346 	return 0;
347 }
348 
349 static inline int xskq_prod_reserve_addr(struct xsk_queue *q, u64 addr)
350 {
351 	struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring;
352 
353 	if (xskq_prod_is_full(q))
354 		return -ENOSPC;
355 
356 	/* A, matches D */
357 	ring->desc[q->cached_prod++ & q->ring_mask] = addr;
358 	return 0;
359 }
360 
361 static inline u32 xskq_prod_reserve_addr_batch(struct xsk_queue *q, struct xdp_desc *descs,
362 					       u32 max)
363 {
364 	struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring;
365 	u32 nb_entries, i, cached_prod;
366 
367 	nb_entries = xskq_prod_nb_free(q, max);
368 
369 	/* A, matches D */
370 	cached_prod = q->cached_prod;
371 	for (i = 0; i < nb_entries; i++)
372 		ring->desc[cached_prod++ & q->ring_mask] = descs[i].addr;
373 	q->cached_prod = cached_prod;
374 
375 	return nb_entries;
376 }
377 
378 static inline int xskq_prod_reserve_desc(struct xsk_queue *q,
379 					 u64 addr, u32 len)
380 {
381 	struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring;
382 	u32 idx;
383 
384 	if (xskq_prod_is_full(q))
385 		return -ENOSPC;
386 
387 	/* A, matches D */
388 	idx = q->cached_prod++ & q->ring_mask;
389 	ring->desc[idx].addr = addr;
390 	ring->desc[idx].len = len;
391 
392 	return 0;
393 }
394 
395 static inline void __xskq_prod_submit(struct xsk_queue *q, u32 idx)
396 {
397 	smp_store_release(&q->ring->producer, idx); /* B, matches C */
398 }
399 
400 static inline void xskq_prod_submit(struct xsk_queue *q)
401 {
402 	__xskq_prod_submit(q, q->cached_prod);
403 }
404 
405 static inline void xskq_prod_submit_addr(struct xsk_queue *q, u64 addr)
406 {
407 	struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring;
408 	u32 idx = q->ring->producer;
409 
410 	ring->desc[idx++ & q->ring_mask] = addr;
411 
412 	__xskq_prod_submit(q, idx);
413 }
414 
415 static inline void xskq_prod_submit_n(struct xsk_queue *q, u32 nb_entries)
416 {
417 	__xskq_prod_submit(q, q->ring->producer + nb_entries);
418 }
419 
420 static inline bool xskq_prod_is_empty(struct xsk_queue *q)
421 {
422 	/* No barriers needed since data is not accessed */
423 	return READ_ONCE(q->ring->consumer) == READ_ONCE(q->ring->producer);
424 }
425 
426 /* For both producers and consumers */
427 
428 static inline u64 xskq_nb_invalid_descs(struct xsk_queue *q)
429 {
430 	return q ? q->invalid_descs : 0;
431 }
432 
433 static inline u64 xskq_nb_queue_empty_descs(struct xsk_queue *q)
434 {
435 	return q ? q->queue_empty_descs : 0;
436 }
437 
438 struct xsk_queue *xskq_create(u32 nentries, bool umem_queue);
439 void xskq_destroy(struct xsk_queue *q_ops);
440 
441 #endif /* _LINUX_XSK_QUEUE_H */
442