1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* XDP user-space ring structure
3 * Copyright(c) 2018 Intel Corporation.
6 #ifndef _LINUX_XSK_QUEUE_H
7 #define _LINUX_XSK_QUEUE_H
9 #include <linux/types.h>
10 #include <linux/if_xdp.h>
11 #include <net/xdp_sock.h>
13 #define RX_BATCH_SIZE 16
14 #define LAZY_UPDATE_THRESHOLD 128
17 u32 producer ____cacheline_aligned_in_smp;
18 u32 consumer ____cacheline_aligned_in_smp;
21 /* Used for the RX and TX queues for packets */
22 struct xdp_rxtx_ring {
24 struct xdp_desc desc[0] ____cacheline_aligned_in_smp;
27 /* Used for the fill and completion queues for buffers */
28 struct xdp_umem_ring {
30 u64 desc[0] ____cacheline_aligned_in_smp;
34 struct xdp_umem_props umem_props;
41 struct xdp_ring *ring;
45 /* Common functions operating for both RXTX and umem queues */
47 static inline u64 xskq_nb_invalid_descs(struct xsk_queue *q)
49 return q ? q->invalid_descs : 0;
52 static inline u32 xskq_nb_avail(struct xsk_queue *q, u32 dcnt)
54 u32 entries = q->prod_tail - q->cons_tail;
57 /* Refresh the local pointer */
58 q->prod_tail = READ_ONCE(q->ring->producer);
59 entries = q->prod_tail - q->cons_tail;
62 return (entries > dcnt) ? dcnt : entries;
65 static inline u32 xskq_nb_free(struct xsk_queue *q, u32 producer, u32 dcnt)
67 u32 free_entries = q->nentries - (producer - q->cons_tail);
69 if (free_entries >= dcnt)
72 /* Refresh the local tail pointer */
73 q->cons_tail = READ_ONCE(q->ring->consumer);
74 return q->nentries - (producer - q->cons_tail);
79 static inline bool xskq_is_valid_addr(struct xsk_queue *q, u64 addr)
81 if (addr >= q->umem_props.size) {
89 static inline u64 *xskq_validate_addr(struct xsk_queue *q, u64 *addr)
91 while (q->cons_tail != q->cons_head) {
92 struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring;
93 unsigned int idx = q->cons_tail & q->ring_mask;
95 *addr = READ_ONCE(ring->desc[idx]) & q->umem_props.chunk_mask;
96 if (xskq_is_valid_addr(q, *addr))
105 static inline u64 *xskq_peek_addr(struct xsk_queue *q, u64 *addr)
107 if (q->cons_tail == q->cons_head) {
108 WRITE_ONCE(q->ring->consumer, q->cons_tail);
109 q->cons_head = q->cons_tail + xskq_nb_avail(q, RX_BATCH_SIZE);
111 /* Order consumer and data */
115 return xskq_validate_addr(q, addr);
118 static inline void xskq_discard_addr(struct xsk_queue *q)
123 static inline int xskq_produce_addr(struct xsk_queue *q, u64 addr)
125 struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring;
127 if (xskq_nb_free(q, q->prod_tail, 1) == 0)
130 ring->desc[q->prod_tail++ & q->ring_mask] = addr;
132 /* Order producer and data */
135 WRITE_ONCE(q->ring->producer, q->prod_tail);
139 static inline int xskq_produce_addr_lazy(struct xsk_queue *q, u64 addr)
141 struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring;
143 if (xskq_nb_free(q, q->prod_head, LAZY_UPDATE_THRESHOLD) == 0)
146 ring->desc[q->prod_head++ & q->ring_mask] = addr;
150 static inline void xskq_produce_flush_addr_n(struct xsk_queue *q,
153 /* Order producer and data */
156 q->prod_tail += nb_entries;
157 WRITE_ONCE(q->ring->producer, q->prod_tail);
160 static inline int xskq_reserve_addr(struct xsk_queue *q)
162 if (xskq_nb_free(q, q->prod_head, 1) == 0)
171 static inline bool xskq_is_valid_desc(struct xsk_queue *q, struct xdp_desc *d)
173 if (!xskq_is_valid_addr(q, d->addr))
176 if (((d->addr + d->len) & q->umem_props.chunk_mask) !=
177 (d->addr & q->umem_props.chunk_mask)) {
185 static inline struct xdp_desc *xskq_validate_desc(struct xsk_queue *q,
186 struct xdp_desc *desc)
188 while (q->cons_tail != q->cons_head) {
189 struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring;
190 unsigned int idx = q->cons_tail & q->ring_mask;
192 *desc = READ_ONCE(ring->desc[idx]);
193 if (xskq_is_valid_desc(q, desc))
202 static inline struct xdp_desc *xskq_peek_desc(struct xsk_queue *q,
203 struct xdp_desc *desc)
205 if (q->cons_tail == q->cons_head) {
206 WRITE_ONCE(q->ring->consumer, q->cons_tail);
207 q->cons_head = q->cons_tail + xskq_nb_avail(q, RX_BATCH_SIZE);
209 /* Order consumer and data */
213 return xskq_validate_desc(q, desc);
216 static inline void xskq_discard_desc(struct xsk_queue *q)
221 static inline int xskq_produce_batch_desc(struct xsk_queue *q,
224 struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring;
227 if (xskq_nb_free(q, q->prod_head, 1) == 0)
230 idx = (q->prod_head++) & q->ring_mask;
231 ring->desc[idx].addr = addr;
232 ring->desc[idx].len = len;
237 static inline void xskq_produce_flush_desc(struct xsk_queue *q)
239 /* Order producer and data */
242 q->prod_tail = q->prod_head;
243 WRITE_ONCE(q->ring->producer, q->prod_tail);
246 static inline bool xskq_full_desc(struct xsk_queue *q)
248 /* No barriers needed since data is not accessed */
249 return READ_ONCE(q->ring->producer) - READ_ONCE(q->ring->consumer) ==
253 static inline bool xskq_empty_desc(struct xsk_queue *q)
255 /* No barriers needed since data is not accessed */
256 return READ_ONCE(q->ring->consumer) == READ_ONCE(q->ring->producer);
259 void xskq_set_umem(struct xsk_queue *q, struct xdp_umem_props *umem_props);
260 struct xsk_queue *xskq_create(u32 nentries, bool umem_queue);
261 void xskq_destroy(struct xsk_queue *q_ops);
263 #endif /* _LINUX_XSK_QUEUE_H */