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> |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 12 | #include <net/xsk_buff_pool.h> |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 13 | |
Björn Töpel | 89e4a37 | 2020-05-20 21:20:52 +0200 | [diff] [blame] | 14 | #include "xsk.h" |
| 15 | |
Björn Töpel | b3a9e0b | 2018-05-22 09:34:59 +0200 | [diff] [blame] | 16 | struct xdp_ring { |
| 17 | u32 producer ____cacheline_aligned_in_smp; |
Magnus Karlsson | c3f01fd | 2020-10-08 16:12:18 +0200 | [diff] [blame] | 18 | /* Hinder the adjacent cache prefetcher to prefetch the consumer |
| 19 | * pointer if the producer pointer is touched and vice versa. |
| 20 | */ |
Magnus Karlsson | b8c7aec | 2020-11-16 12:12:45 +0100 | [diff] [blame] | 21 | u32 pad1 ____cacheline_aligned_in_smp; |
Björn Töpel | b3a9e0b | 2018-05-22 09:34:59 +0200 | [diff] [blame] | 22 | u32 consumer ____cacheline_aligned_in_smp; |
Magnus Karlsson | b8c7aec | 2020-11-16 12:12:45 +0100 | [diff] [blame] | 23 | u32 pad2 ____cacheline_aligned_in_smp; |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 24 | u32 flags; |
Magnus Karlsson | b8c7aec | 2020-11-16 12:12:45 +0100 | [diff] [blame] | 25 | u32 pad3 ____cacheline_aligned_in_smp; |
Björn Töpel | b3a9e0b | 2018-05-22 09:34:59 +0200 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | /* Used for the RX and TX queues for packets */ |
| 29 | struct xdp_rxtx_ring { |
| 30 | struct xdp_ring ptrs; |
Gustavo A. R. Silva | 95e486f | 2020-02-28 07:19:07 -0600 | [diff] [blame] | 31 | struct xdp_desc desc[] ____cacheline_aligned_in_smp; |
Björn Töpel | b3a9e0b | 2018-05-22 09:34:59 +0200 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | /* Used for the fill and completion queues for buffers */ |
| 35 | struct xdp_umem_ring { |
| 36 | struct xdp_ring ptrs; |
Gustavo A. R. Silva | 95e486f | 2020-02-28 07:19:07 -0600 | [diff] [blame] | 37 | u64 desc[] ____cacheline_aligned_in_smp; |
Björn Töpel | b3a9e0b | 2018-05-22 09:34:59 +0200 | [diff] [blame] | 38 | }; |
| 39 | |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 40 | struct xsk_queue { |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 41 | u32 ring_mask; |
| 42 | u32 nentries; |
Magnus Karlsson | d7012f0 | 2019-12-19 13:39:22 +0100 | [diff] [blame] | 43 | u32 cached_prod; |
Magnus Karlsson | c5ed924b | 2019-12-19 13:39:26 +0100 | [diff] [blame] | 44 | u32 cached_cons; |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 45 | struct xdp_ring *ring; |
| 46 | u64 invalid_descs; |
Ciara Loftus | 8aa5a33 | 2020-07-08 07:28:33 +0000 | [diff] [blame] | 47 | u64 queue_empty_descs; |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 48 | }; |
| 49 | |
Björn Töpel | a23b3f5 | 2021-03-05 10:41:12 +0100 | [diff] [blame] | 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. |
Magnus Karlsson | f63666de | 2019-04-16 14:58:08 +0200 | [diff] [blame] | 56 | * |
| 57 | * producer consumer |
| 58 | * |
Björn Töpel | a23b3f5 | 2021-03-05 10:41:12 +0100 | [diff] [blame] | 59 | * if (LOAD ->consumer) { (A) LOAD.acq ->producer (C) |
Magnus Karlsson | f63666de | 2019-04-16 14:58:08 +0200 | [diff] [blame] | 60 | * STORE $data LOAD $data |
Björn Töpel | a23b3f5 | 2021-03-05 10:41:12 +0100 | [diff] [blame] | 61 | * STORE.rel ->producer (B) STORE.rel ->consumer (D) |
Magnus Karlsson | f63666de | 2019-04-16 14:58:08 +0200 | [diff] [blame] | 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 |
Björn Töpel | a23b3f5 | 2021-03-05 10:41:12 +0100 | [diff] [blame] | 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. |
Magnus Karlsson | f63666de | 2019-04-16 14:58:08 +0200 | [diff] [blame] | 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 Karlsson | 15d8c91 | 2019-12-19 13:39:30 +0100 | [diff] [blame] | 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 |
Ciara Loftus | f1fc8ec | 2020-09-28 08:23:44 +0000 | [diff] [blame] | 105 | * any new entries. If so, the consumer can then read these entries |
Magnus Karlsson | 15d8c91 | 2019-12-19 13:39:30 +0100 | [diff] [blame] | 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 | */ |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 111 | |
Magnus Karlsson | 15d8c91 | 2019-12-19 13:39:30 +0100 | [diff] [blame] | 112 | /* Functions that read and validate content from consumer rings. */ |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 113 | |
Magnus Karlsson | 47e4075 | 2021-09-22 09:56:02 +0200 | [diff] [blame] | 114 | static inline void __xskq_cons_read_addr_unchecked(struct xsk_queue *q, u32 cached_cons, u64 *addr) |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 115 | { |
| 116 | struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring; |
Magnus Karlsson | 47e4075 | 2021-09-22 09:56:02 +0200 | [diff] [blame] | 117 | u32 idx = cached_cons & q->ring_mask; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 118 | |
Magnus Karlsson | 47e4075 | 2021-09-22 09:56:02 +0200 | [diff] [blame] | 119 | *addr = ring->desc[idx]; |
| 120 | } |
| 121 | |
| 122 | static inline bool xskq_cons_read_addr_unchecked(struct xsk_queue *q, u64 *addr) |
| 123 | { |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 124 | if (q->cached_cons != q->cached_prod) { |
Magnus Karlsson | 47e4075 | 2021-09-22 09:56:02 +0200 | [diff] [blame] | 125 | __xskq_cons_read_addr_unchecked(q, q->cached_cons, addr); |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 126 | return true; |
| 127 | } |
| 128 | |
| 129 | return false; |
| 130 | } |
| 131 | |
Björn Töpel | 26062b1 | 2020-05-20 21:21:02 +0200 | [diff] [blame] | 132 | static inline bool xp_aligned_validate_desc(struct xsk_buff_pool *pool, |
| 133 | struct xdp_desc *desc) |
| 134 | { |
Magnus Karlsson | f654fae | 2021-06-18 09:58:05 +0200 | [diff] [blame] | 135 | u64 chunk, chunk_end; |
Björn Töpel | 26062b1 | 2020-05-20 21:21:02 +0200 | [diff] [blame] | 136 | |
Xuan Zhuo | ac31565 | 2021-04-28 17:44:24 +0800 | [diff] [blame] | 137 | chunk = xp_aligned_extract_addr(pool, desc->addr); |
Magnus Karlsson | f654fae | 2021-06-18 09:58:05 +0200 | [diff] [blame] | 138 | if (likely(desc->len)) { |
| 139 | chunk_end = xp_aligned_extract_addr(pool, desc->addr + desc->len - 1); |
| 140 | if (chunk != chunk_end) |
| 141 | return false; |
| 142 | } |
| 143 | |
Björn Töpel | 26062b1 | 2020-05-20 21:21:02 +0200 | [diff] [blame] | 144 | if (chunk >= pool->addrs_cnt) |
| 145 | return false; |
| 146 | |
| 147 | if (desc->options) |
| 148 | return false; |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | static inline bool xp_unaligned_validate_desc(struct xsk_buff_pool *pool, |
| 153 | struct xdp_desc *desc) |
| 154 | { |
| 155 | u64 addr, base_addr; |
| 156 | |
| 157 | base_addr = xp_unaligned_extract_addr(desc->addr); |
| 158 | addr = xp_unaligned_add_offset_to_addr(desc->addr); |
| 159 | |
| 160 | if (desc->len > pool->chunk_size) |
| 161 | return false; |
| 162 | |
| 163 | if (base_addr >= pool->addrs_cnt || addr >= pool->addrs_cnt || |
| 164 | xp_desc_crosses_non_contig_pg(pool, addr, desc->len)) |
| 165 | return false; |
| 166 | |
| 167 | if (desc->options) |
| 168 | return false; |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | static inline bool xp_validate_desc(struct xsk_buff_pool *pool, |
| 173 | struct xdp_desc *desc) |
| 174 | { |
| 175 | return pool->unaligned ? xp_unaligned_validate_desc(pool, desc) : |
| 176 | xp_aligned_validate_desc(pool, desc); |
| 177 | } |
| 178 | |
Magnus Karlsson | 03896ef | 2019-12-19 13:39:27 +0100 | [diff] [blame] | 179 | static inline bool xskq_cons_is_valid_desc(struct xsk_queue *q, |
| 180 | struct xdp_desc *d, |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 181 | struct xsk_buff_pool *pool) |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 182 | { |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 183 | if (!xp_validate_desc(pool, d)) { |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 184 | q->invalid_descs++; |
| 185 | return false; |
| 186 | } |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 187 | return true; |
| 188 | } |
| 189 | |
Magnus Karlsson | 03896ef | 2019-12-19 13:39:27 +0100 | [diff] [blame] | 190 | static inline bool xskq_cons_read_desc(struct xsk_queue *q, |
| 191 | struct xdp_desc *desc, |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 192 | struct xsk_buff_pool *pool) |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 193 | { |
Magnus Karlsson | c5ed924b | 2019-12-19 13:39:26 +0100 | [diff] [blame] | 194 | while (q->cached_cons != q->cached_prod) { |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 195 | struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring; |
Magnus Karlsson | c5ed924b | 2019-12-19 13:39:26 +0100 | [diff] [blame] | 196 | u32 idx = q->cached_cons & q->ring_mask; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 197 | |
Magnus Karlsson | c34787f | 2019-12-19 13:39:29 +0100 | [diff] [blame] | 198 | *desc = ring->desc[idx]; |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 199 | if (xskq_cons_is_valid_desc(q, desc, pool)) |
Magnus Karlsson | 03896ef | 2019-12-19 13:39:27 +0100 | [diff] [blame] | 200 | return true; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 201 | |
Magnus Karlsson | c5ed924b | 2019-12-19 13:39:26 +0100 | [diff] [blame] | 202 | q->cached_cons++; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Magnus Karlsson | 03896ef | 2019-12-19 13:39:27 +0100 | [diff] [blame] | 205 | return false; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 206 | } |
| 207 | |
Magnus Karlsson | 9349eb3 | 2020-11-16 12:12:46 +0100 | [diff] [blame] | 208 | static inline u32 xskq_cons_read_desc_batch(struct xsk_queue *q, |
| 209 | struct xdp_desc *descs, |
| 210 | struct xsk_buff_pool *pool, u32 max) |
| 211 | { |
| 212 | u32 cached_cons = q->cached_cons, nb_entries = 0; |
| 213 | |
| 214 | while (cached_cons != q->cached_prod && nb_entries < max) { |
| 215 | struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring; |
| 216 | u32 idx = cached_cons & q->ring_mask; |
| 217 | |
| 218 | descs[nb_entries] = ring->desc[idx]; |
| 219 | if (unlikely(!xskq_cons_is_valid_desc(q, &descs[nb_entries], pool))) { |
| 220 | /* Skip the entry */ |
| 221 | cached_cons++; |
| 222 | continue; |
| 223 | } |
| 224 | |
| 225 | nb_entries++; |
| 226 | cached_cons++; |
| 227 | } |
| 228 | |
| 229 | return nb_entries; |
| 230 | } |
| 231 | |
Magnus Karlsson | 15d8c91 | 2019-12-19 13:39:30 +0100 | [diff] [blame] | 232 | /* Functions for consumers */ |
| 233 | |
| 234 | static inline void __xskq_cons_release(struct xsk_queue *q) |
| 235 | { |
Björn Töpel | a23b3f5 | 2021-03-05 10:41:12 +0100 | [diff] [blame] | 236 | smp_store_release(&q->ring->consumer, q->cached_cons); /* D, matchees A */ |
Magnus Karlsson | 15d8c91 | 2019-12-19 13:39:30 +0100 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static inline void __xskq_cons_peek(struct xsk_queue *q) |
| 240 | { |
| 241 | /* Refresh the local pointer */ |
Björn Töpel | a23b3f5 | 2021-03-05 10:41:12 +0100 | [diff] [blame] | 242 | q->cached_prod = smp_load_acquire(&q->ring->producer); /* C, matches B */ |
Magnus Karlsson | 15d8c91 | 2019-12-19 13:39:30 +0100 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static inline void xskq_cons_get_entries(struct xsk_queue *q) |
| 246 | { |
| 247 | __xskq_cons_release(q); |
| 248 | __xskq_cons_peek(q); |
| 249 | } |
| 250 | |
Magnus Karlsson | 9349eb3 | 2020-11-16 12:12:46 +0100 | [diff] [blame] | 251 | static inline u32 xskq_cons_nb_entries(struct xsk_queue *q, u32 max) |
Magnus Karlsson | 15d8c91 | 2019-12-19 13:39:30 +0100 | [diff] [blame] | 252 | { |
| 253 | u32 entries = q->cached_prod - q->cached_cons; |
| 254 | |
Magnus Karlsson | 9349eb3 | 2020-11-16 12:12:46 +0100 | [diff] [blame] | 255 | if (entries >= max) |
| 256 | return max; |
Magnus Karlsson | 15d8c91 | 2019-12-19 13:39:30 +0100 | [diff] [blame] | 257 | |
| 258 | __xskq_cons_peek(q); |
| 259 | entries = q->cached_prod - q->cached_cons; |
| 260 | |
Magnus Karlsson | 9349eb3 | 2020-11-16 12:12:46 +0100 | [diff] [blame] | 261 | return entries >= max ? max : entries; |
| 262 | } |
| 263 | |
| 264 | static inline bool xskq_cons_has_entries(struct xsk_queue *q, u32 cnt) |
| 265 | { |
| 266 | return xskq_cons_nb_entries(q, cnt) >= cnt ? true : false; |
Magnus Karlsson | 15d8c91 | 2019-12-19 13:39:30 +0100 | [diff] [blame] | 267 | } |
| 268 | |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 269 | static inline bool xskq_cons_peek_addr_unchecked(struct xsk_queue *q, u64 *addr) |
| 270 | { |
| 271 | if (q->cached_prod == q->cached_cons) |
| 272 | xskq_cons_get_entries(q); |
| 273 | return xskq_cons_read_addr_unchecked(q, addr); |
| 274 | } |
| 275 | |
Magnus Karlsson | 03896ef | 2019-12-19 13:39:27 +0100 | [diff] [blame] | 276 | static inline bool xskq_cons_peek_desc(struct xsk_queue *q, |
| 277 | struct xdp_desc *desc, |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 278 | struct xsk_buff_pool *pool) |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 279 | { |
Magnus Karlsson | c5ed924b | 2019-12-19 13:39:26 +0100 | [diff] [blame] | 280 | if (q->cached_prod == q->cached_cons) |
| 281 | xskq_cons_get_entries(q); |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 282 | return xskq_cons_read_desc(q, desc, pool); |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 283 | } |
| 284 | |
Magnus Karlsson | 9349eb3 | 2020-11-16 12:12:46 +0100 | [diff] [blame] | 285 | static inline u32 xskq_cons_peek_desc_batch(struct xsk_queue *q, struct xdp_desc *descs, |
| 286 | struct xsk_buff_pool *pool, u32 max) |
| 287 | { |
| 288 | u32 entries = xskq_cons_nb_entries(q, max); |
| 289 | |
| 290 | return xskq_cons_read_desc_batch(q, descs, pool, entries); |
| 291 | } |
| 292 | |
| 293 | /* To improve performance in the xskq_cons_release functions, only update local state here. |
| 294 | * Reflect this to global state when we get new entries from the ring in |
| 295 | * xskq_cons_get_entries() and whenever Rx or Tx processing are completed in the NAPI loop. |
| 296 | */ |
Magnus Karlsson | 15d8c91 | 2019-12-19 13:39:30 +0100 | [diff] [blame] | 297 | static inline void xskq_cons_release(struct xsk_queue *q) |
| 298 | { |
Magnus Karlsson | 15d8c91 | 2019-12-19 13:39:30 +0100 | [diff] [blame] | 299 | q->cached_cons++; |
| 300 | } |
| 301 | |
Magnus Karlsson | 9349eb3 | 2020-11-16 12:12:46 +0100 | [diff] [blame] | 302 | static inline void xskq_cons_release_n(struct xsk_queue *q, u32 cnt) |
| 303 | { |
| 304 | q->cached_cons += cnt; |
| 305 | } |
| 306 | |
Magnus Karlsson | 15d8c91 | 2019-12-19 13:39:30 +0100 | [diff] [blame] | 307 | static inline bool xskq_cons_is_full(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 | q->nentries; |
| 312 | } |
| 313 | |
Xuan Zhuo | 3413f04 | 2020-12-01 21:56:58 +0800 | [diff] [blame] | 314 | static inline u32 xskq_cons_present_entries(struct xsk_queue *q) |
| 315 | { |
| 316 | /* No barriers needed since data is not accessed */ |
| 317 | return READ_ONCE(q->ring->producer) - READ_ONCE(q->ring->consumer); |
| 318 | } |
| 319 | |
Magnus Karlsson | 15d8c91 | 2019-12-19 13:39:30 +0100 | [diff] [blame] | 320 | /* Functions for producers */ |
| 321 | |
Magnus Karlsson | 9349eb3 | 2020-11-16 12:12:46 +0100 | [diff] [blame] | 322 | static inline u32 xskq_prod_nb_free(struct xsk_queue *q, u32 max) |
Magnus Karlsson | 15d8c91 | 2019-12-19 13:39:30 +0100 | [diff] [blame] | 323 | { |
| 324 | u32 free_entries = q->nentries - (q->cached_prod - q->cached_cons); |
| 325 | |
Magnus Karlsson | 9349eb3 | 2020-11-16 12:12:46 +0100 | [diff] [blame] | 326 | if (free_entries >= max) |
| 327 | return max; |
Magnus Karlsson | 15d8c91 | 2019-12-19 13:39:30 +0100 | [diff] [blame] | 328 | |
| 329 | /* Refresh the local tail pointer */ |
| 330 | q->cached_cons = READ_ONCE(q->ring->consumer); |
| 331 | free_entries = q->nentries - (q->cached_prod - q->cached_cons); |
| 332 | |
Magnus Karlsson | 9349eb3 | 2020-11-16 12:12:46 +0100 | [diff] [blame] | 333 | return free_entries >= max ? max : free_entries; |
| 334 | } |
| 335 | |
| 336 | static inline bool xskq_prod_is_full(struct xsk_queue *q) |
| 337 | { |
| 338 | return xskq_prod_nb_free(q, 1) ? false : true; |
Magnus Karlsson | 15d8c91 | 2019-12-19 13:39:30 +0100 | [diff] [blame] | 339 | } |
| 340 | |
Magnus Karlsson | b1b95cb | 2020-12-18 14:45:25 +0100 | [diff] [blame] | 341 | static inline void xskq_prod_cancel(struct xsk_queue *q) |
| 342 | { |
| 343 | q->cached_prod--; |
| 344 | } |
| 345 | |
Magnus Karlsson | 15d8c91 | 2019-12-19 13:39:30 +0100 | [diff] [blame] | 346 | static inline int xskq_prod_reserve(struct xsk_queue *q) |
| 347 | { |
| 348 | if (xskq_prod_is_full(q)) |
| 349 | return -ENOSPC; |
| 350 | |
| 351 | /* A, matches D */ |
| 352 | q->cached_prod++; |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | static inline int xskq_prod_reserve_addr(struct xsk_queue *q, u64 addr) |
| 357 | { |
| 358 | struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring; |
| 359 | |
| 360 | if (xskq_prod_is_full(q)) |
| 361 | return -ENOSPC; |
| 362 | |
| 363 | /* A, matches D */ |
| 364 | ring->desc[q->cached_prod++ & q->ring_mask] = addr; |
| 365 | return 0; |
| 366 | } |
| 367 | |
Magnus Karlsson | 9349eb3 | 2020-11-16 12:12:46 +0100 | [diff] [blame] | 368 | static inline u32 xskq_prod_reserve_addr_batch(struct xsk_queue *q, struct xdp_desc *descs, |
| 369 | u32 max) |
| 370 | { |
| 371 | struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring; |
| 372 | u32 nb_entries, i, cached_prod; |
| 373 | |
| 374 | nb_entries = xskq_prod_nb_free(q, max); |
| 375 | |
| 376 | /* A, matches D */ |
| 377 | cached_prod = q->cached_prod; |
| 378 | for (i = 0; i < nb_entries; i++) |
| 379 | ring->desc[cached_prod++ & q->ring_mask] = descs[i].addr; |
| 380 | q->cached_prod = cached_prod; |
| 381 | |
| 382 | return nb_entries; |
| 383 | } |
| 384 | |
Magnus Karlsson | 59e35e5 | 2019-12-19 13:39:23 +0100 | [diff] [blame] | 385 | static inline int xskq_prod_reserve_desc(struct xsk_queue *q, |
| 386 | u64 addr, u32 len) |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 387 | { |
| 388 | struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring; |
Magnus Karlsson | 59e35e5 | 2019-12-19 13:39:23 +0100 | [diff] [blame] | 389 | u32 idx; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 390 | |
Magnus Karlsson | df0ae6f | 2019-12-19 13:39:25 +0100 | [diff] [blame] | 391 | if (xskq_prod_is_full(q)) |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 392 | return -ENOSPC; |
| 393 | |
Magnus Karlsson | f63666de | 2019-04-16 14:58:08 +0200 | [diff] [blame] | 394 | /* A, matches D */ |
Magnus Karlsson | d7012f0 | 2019-12-19 13:39:22 +0100 | [diff] [blame] | 395 | idx = q->cached_prod++ & q->ring_mask; |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 396 | ring->desc[idx].addr = addr; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 397 | ring->desc[idx].len = len; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
Magnus Karlsson | 15d8c91 | 2019-12-19 13:39:30 +0100 | [diff] [blame] | 402 | static inline void __xskq_prod_submit(struct xsk_queue *q, u32 idx) |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 403 | { |
Björn Töpel | a23b3f5 | 2021-03-05 10:41:12 +0100 | [diff] [blame] | 404 | smp_store_release(&q->ring->producer, idx); /* B, matches C */ |
Magnus Karlsson | 15d8c91 | 2019-12-19 13:39:30 +0100 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | static inline void xskq_prod_submit(struct xsk_queue *q) |
| 408 | { |
| 409 | __xskq_prod_submit(q, q->cached_prod); |
| 410 | } |
| 411 | |
| 412 | static inline void xskq_prod_submit_addr(struct xsk_queue *q, u64 addr) |
| 413 | { |
| 414 | struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring; |
| 415 | u32 idx = q->ring->producer; |
| 416 | |
| 417 | ring->desc[idx++ & q->ring_mask] = addr; |
| 418 | |
| 419 | __xskq_prod_submit(q, idx); |
| 420 | } |
| 421 | |
| 422 | static inline void xskq_prod_submit_n(struct xsk_queue *q, u32 nb_entries) |
| 423 | { |
| 424 | __xskq_prod_submit(q, q->ring->producer + nb_entries); |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 425 | } |
| 426 | |
Magnus Karlsson | 59e35e5 | 2019-12-19 13:39:23 +0100 | [diff] [blame] | 427 | static inline bool xskq_prod_is_empty(struct xsk_queue *q) |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 428 | { |
Magnus Karlsson | 11cc2d2 | 2019-12-19 13:39:21 +0100 | [diff] [blame] | 429 | /* No barriers needed since data is not accessed */ |
| 430 | return READ_ONCE(q->ring->consumer) == READ_ONCE(q->ring->producer); |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 431 | } |
| 432 | |
Magnus Karlsson | 15d8c91 | 2019-12-19 13:39:30 +0100 | [diff] [blame] | 433 | /* For both producers and consumers */ |
| 434 | |
| 435 | static inline u64 xskq_nb_invalid_descs(struct xsk_queue *q) |
| 436 | { |
| 437 | return q ? q->invalid_descs : 0; |
| 438 | } |
| 439 | |
Ciara Loftus | 8aa5a33 | 2020-07-08 07:28:33 +0000 | [diff] [blame] | 440 | static inline u64 xskq_nb_queue_empty_descs(struct xsk_queue *q) |
| 441 | { |
| 442 | return q ? q->queue_empty_descs : 0; |
| 443 | } |
| 444 | |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 445 | struct xsk_queue *xskq_create(u32 nentries, bool umem_queue); |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 446 | void xskq_destroy(struct xsk_queue *q_ops); |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 447 | |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 448 | #endif /* _LINUX_XSK_QUEUE_H */ |