Björn Töpel | dac09149 | 2018-05-18 14:00:21 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* XDP user-space ring structure |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 3 | * Copyright(c) 2018 Intel Corporation. |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 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> |
Björn Töpel | e61e62b9 | 2018-06-04 14:05:51 +0200 | [diff] [blame^] | 11 | #include <net/xdp_sock.h> |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 12 | |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 13 | #define RX_BATCH_SIZE 16 |
| 14 | |
Björn Töpel | b3a9e0b | 2018-05-22 09:34:59 +0200 | [diff] [blame] | 15 | struct xdp_ring { |
| 16 | u32 producer ____cacheline_aligned_in_smp; |
| 17 | u32 consumer ____cacheline_aligned_in_smp; |
| 18 | }; |
| 19 | |
| 20 | /* Used for the RX and TX queues for packets */ |
| 21 | struct xdp_rxtx_ring { |
| 22 | struct xdp_ring ptrs; |
| 23 | struct xdp_desc desc[0] ____cacheline_aligned_in_smp; |
| 24 | }; |
| 25 | |
| 26 | /* Used for the fill and completion queues for buffers */ |
| 27 | struct xdp_umem_ring { |
| 28 | struct xdp_ring ptrs; |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 29 | u64 desc[0] ____cacheline_aligned_in_smp; |
Björn Töpel | b3a9e0b | 2018-05-22 09:34:59 +0200 | [diff] [blame] | 30 | }; |
| 31 | |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 32 | struct xsk_queue { |
| 33 | struct xdp_umem_props umem_props; |
| 34 | u32 ring_mask; |
| 35 | u32 nentries; |
| 36 | u32 prod_head; |
| 37 | u32 prod_tail; |
| 38 | u32 cons_head; |
| 39 | u32 cons_tail; |
| 40 | struct xdp_ring *ring; |
| 41 | u64 invalid_descs; |
| 42 | }; |
| 43 | |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 44 | /* Common functions operating for both RXTX and umem queues */ |
| 45 | |
Magnus Karlsson | af75d9e | 2018-05-02 13:01:35 +0200 | [diff] [blame] | 46 | static inline u64 xskq_nb_invalid_descs(struct xsk_queue *q) |
| 47 | { |
| 48 | return q ? q->invalid_descs : 0; |
| 49 | } |
| 50 | |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 51 | static inline u32 xskq_nb_avail(struct xsk_queue *q, u32 dcnt) |
| 52 | { |
| 53 | u32 entries = q->prod_tail - q->cons_tail; |
| 54 | |
| 55 | if (entries == 0) { |
| 56 | /* Refresh the local pointer */ |
| 57 | q->prod_tail = READ_ONCE(q->ring->producer); |
| 58 | entries = q->prod_tail - q->cons_tail; |
| 59 | } |
| 60 | |
| 61 | return (entries > dcnt) ? dcnt : entries; |
| 62 | } |
| 63 | |
| 64 | static inline u32 xskq_nb_free(struct xsk_queue *q, u32 producer, u32 dcnt) |
| 65 | { |
| 66 | u32 free_entries = q->nentries - (producer - q->cons_tail); |
| 67 | |
| 68 | if (free_entries >= dcnt) |
| 69 | return free_entries; |
| 70 | |
| 71 | /* Refresh the local tail pointer */ |
| 72 | q->cons_tail = READ_ONCE(q->ring->consumer); |
| 73 | return q->nentries - (producer - q->cons_tail); |
| 74 | } |
| 75 | |
| 76 | /* UMEM queue */ |
| 77 | |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 78 | static inline bool xskq_is_valid_addr(struct xsk_queue *q, u64 addr) |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 79 | { |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 80 | if (addr >= q->umem_props.size) { |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 81 | q->invalid_descs++; |
| 82 | return false; |
| 83 | } |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 84 | |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 85 | return true; |
| 86 | } |
| 87 | |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 88 | static inline u64 *xskq_validate_addr(struct xsk_queue *q, u64 *addr) |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 89 | { |
| 90 | while (q->cons_tail != q->cons_head) { |
| 91 | struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring; |
| 92 | unsigned int idx = q->cons_tail & q->ring_mask; |
| 93 | |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 94 | *addr = READ_ONCE(ring->desc[idx]) & q->umem_props.chunk_mask; |
| 95 | if (xskq_is_valid_addr(q, *addr)) |
| 96 | return addr; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 97 | |
| 98 | q->cons_tail++; |
| 99 | } |
| 100 | |
| 101 | return NULL; |
| 102 | } |
| 103 | |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 104 | static inline u64 *xskq_peek_addr(struct xsk_queue *q, u64 *addr) |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 105 | { |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 106 | if (q->cons_tail == q->cons_head) { |
| 107 | WRITE_ONCE(q->ring->consumer, q->cons_tail); |
| 108 | q->cons_head = q->cons_tail + xskq_nb_avail(q, RX_BATCH_SIZE); |
| 109 | |
| 110 | /* Order consumer and data */ |
| 111 | smp_rmb(); |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 114 | return xskq_validate_addr(q, addr); |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 117 | static inline void xskq_discard_addr(struct xsk_queue *q) |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 118 | { |
| 119 | q->cons_tail++; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 120 | } |
| 121 | |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 122 | static inline int xskq_produce_addr(struct xsk_queue *q, u64 addr) |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 123 | { |
| 124 | struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring; |
| 125 | |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 126 | ring->desc[q->prod_tail++ & q->ring_mask] = addr; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 127 | |
| 128 | /* Order producer and data */ |
| 129 | smp_wmb(); |
| 130 | |
| 131 | WRITE_ONCE(q->ring->producer, q->prod_tail); |
| 132 | return 0; |
| 133 | } |
| 134 | |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 135 | static inline int xskq_reserve_addr(struct xsk_queue *q) |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 136 | { |
| 137 | if (xskq_nb_free(q, q->prod_head, 1) == 0) |
| 138 | return -ENOSPC; |
| 139 | |
| 140 | q->prod_head++; |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | /* Rx/Tx queue */ |
| 145 | |
| 146 | static inline bool xskq_is_valid_desc(struct xsk_queue *q, struct xdp_desc *d) |
| 147 | { |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 148 | if (!xskq_is_valid_addr(q, d->addr)) |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 149 | return false; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 150 | |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 151 | if (((d->addr + d->len) & q->umem_props.chunk_mask) != |
| 152 | (d->addr & q->umem_props.chunk_mask)) { |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 153 | q->invalid_descs++; |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | static inline struct xdp_desc *xskq_validate_desc(struct xsk_queue *q, |
| 161 | struct xdp_desc *desc) |
| 162 | { |
| 163 | while (q->cons_tail != q->cons_head) { |
| 164 | struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring; |
| 165 | unsigned int idx = q->cons_tail & q->ring_mask; |
| 166 | |
Björn Töpel | 4e64c83 | 2018-06-04 13:57:11 +0200 | [diff] [blame] | 167 | *desc = READ_ONCE(ring->desc[idx]); |
| 168 | if (xskq_is_valid_desc(q, desc)) |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 169 | return desc; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 170 | |
| 171 | q->cons_tail++; |
| 172 | } |
| 173 | |
| 174 | return NULL; |
| 175 | } |
| 176 | |
| 177 | static inline struct xdp_desc *xskq_peek_desc(struct xsk_queue *q, |
| 178 | struct xdp_desc *desc) |
| 179 | { |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 180 | if (q->cons_tail == q->cons_head) { |
| 181 | WRITE_ONCE(q->ring->consumer, q->cons_tail); |
| 182 | q->cons_head = q->cons_tail + xskq_nb_avail(q, RX_BATCH_SIZE); |
| 183 | |
| 184 | /* Order consumer and data */ |
| 185 | smp_rmb(); |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Björn Töpel | 4e64c83 | 2018-06-04 13:57:11 +0200 | [diff] [blame] | 188 | return xskq_validate_desc(q, desc); |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static inline void xskq_discard_desc(struct xsk_queue *q) |
| 192 | { |
| 193 | q->cons_tail++; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 194 | } |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 195 | |
| 196 | static inline int xskq_produce_batch_desc(struct xsk_queue *q, |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 197 | u64 addr, u32 len) |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 198 | { |
| 199 | struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring; |
| 200 | unsigned int idx; |
| 201 | |
| 202 | if (xskq_nb_free(q, q->prod_head, 1) == 0) |
| 203 | return -ENOSPC; |
| 204 | |
| 205 | idx = (q->prod_head++) & q->ring_mask; |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 206 | ring->desc[idx].addr = addr; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 207 | ring->desc[idx].len = len; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static inline void xskq_produce_flush_desc(struct xsk_queue *q) |
| 213 | { |
| 214 | /* Order producer and data */ |
| 215 | smp_wmb(); |
| 216 | |
| 217 | q->prod_tail = q->prod_head, |
| 218 | WRITE_ONCE(q->ring->producer, q->prod_tail); |
| 219 | } |
| 220 | |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 221 | static inline bool xskq_full_desc(struct xsk_queue *q) |
| 222 | { |
Björn Töpel | da60cf0 | 2018-05-18 14:00:23 +0200 | [diff] [blame] | 223 | return xskq_nb_avail(q, q->nentries) == q->nentries; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 224 | } |
| 225 | |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 226 | static inline bool xskq_empty_desc(struct xsk_queue *q) |
| 227 | { |
Björn Töpel | da60cf0 | 2018-05-18 14:00:23 +0200 | [diff] [blame] | 228 | return xskq_nb_free(q, q->prod_tail, 1) == q->nentries; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Magnus Karlsson | 965a9909 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 231 | void xskq_set_umem(struct xsk_queue *q, struct xdp_umem_props *umem_props); |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 232 | struct xsk_queue *xskq_create(u32 nentries, bool umem_queue); |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 233 | void xskq_destroy(struct xsk_queue *q_ops); |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 234 | |
| 235 | #endif /* _LINUX_XSK_QUEUE_H */ |