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> |
| 11 | |
| 12 | #include "xdp_umem_props.h" |
| 13 | |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 14 | #define RX_BATCH_SIZE 16 |
| 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; |
| 18 | u32 consumer ____cacheline_aligned_in_smp; |
| 19 | }; |
| 20 | |
| 21 | /* Used for the RX and TX queues for packets */ |
| 22 | struct xdp_rxtx_ring { |
| 23 | struct xdp_ring ptrs; |
| 24 | struct xdp_desc desc[0] ____cacheline_aligned_in_smp; |
| 25 | }; |
| 26 | |
| 27 | /* Used for the fill and completion queues for buffers */ |
| 28 | struct xdp_umem_ring { |
| 29 | struct xdp_ring ptrs; |
| 30 | u32 desc[0] ____cacheline_aligned_in_smp; |
| 31 | }; |
| 32 | |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 33 | struct xsk_queue { |
| 34 | struct xdp_umem_props umem_props; |
| 35 | u32 ring_mask; |
| 36 | u32 nentries; |
| 37 | u32 prod_head; |
| 38 | u32 prod_tail; |
| 39 | u32 cons_head; |
| 40 | u32 cons_tail; |
| 41 | struct xdp_ring *ring; |
| 42 | u64 invalid_descs; |
| 43 | }; |
| 44 | |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 45 | /* Common functions operating for both RXTX and umem queues */ |
| 46 | |
Magnus Karlsson | af75d9e | 2018-05-02 13:01:35 +0200 | [diff] [blame] | 47 | static inline u64 xskq_nb_invalid_descs(struct xsk_queue *q) |
| 48 | { |
| 49 | return q ? q->invalid_descs : 0; |
| 50 | } |
| 51 | |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 52 | static inline u32 xskq_nb_avail(struct xsk_queue *q, u32 dcnt) |
| 53 | { |
| 54 | u32 entries = q->prod_tail - q->cons_tail; |
| 55 | |
| 56 | if (entries == 0) { |
| 57 | /* Refresh the local pointer */ |
| 58 | q->prod_tail = READ_ONCE(q->ring->producer); |
| 59 | entries = q->prod_tail - q->cons_tail; |
| 60 | } |
| 61 | |
| 62 | return (entries > dcnt) ? dcnt : entries; |
| 63 | } |
| 64 | |
| 65 | static inline u32 xskq_nb_free(struct xsk_queue *q, u32 producer, u32 dcnt) |
| 66 | { |
| 67 | u32 free_entries = q->nentries - (producer - q->cons_tail); |
| 68 | |
| 69 | if (free_entries >= dcnt) |
| 70 | return free_entries; |
| 71 | |
| 72 | /* Refresh the local tail pointer */ |
| 73 | q->cons_tail = READ_ONCE(q->ring->consumer); |
| 74 | return q->nentries - (producer - q->cons_tail); |
| 75 | } |
| 76 | |
| 77 | /* UMEM queue */ |
| 78 | |
| 79 | static inline bool xskq_is_valid_id(struct xsk_queue *q, u32 idx) |
| 80 | { |
| 81 | if (unlikely(idx >= q->umem_props.nframes)) { |
| 82 | q->invalid_descs++; |
| 83 | return false; |
| 84 | } |
| 85 | return true; |
| 86 | } |
| 87 | |
Björn Töpel | 4e64c83 | 2018-06-04 13:57:11 +0200 | [diff] [blame^] | 88 | static inline u32 *xskq_validate_id(struct xsk_queue *q, u32 *id) |
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 | 4e64c83 | 2018-06-04 13:57:11 +0200 | [diff] [blame^] | 94 | *id = READ_ONCE(ring->desc[idx]); |
| 95 | if (xskq_is_valid_id(q, *id)) |
| 96 | return id; |
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 | 4e64c83 | 2018-06-04 13:57:11 +0200 | [diff] [blame^] | 104 | static inline u32 *xskq_peek_id(struct xsk_queue *q, u32 *id) |
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 | 4e64c83 | 2018-06-04 13:57:11 +0200 | [diff] [blame^] | 114 | return xskq_validate_id(q, id); |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static inline void xskq_discard_id(struct xsk_queue *q) |
| 118 | { |
| 119 | q->cons_tail++; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 120 | } |
| 121 | |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 122 | static inline int xskq_produce_id(struct xsk_queue *q, u32 id) |
| 123 | { |
| 124 | struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring; |
| 125 | |
| 126 | ring->desc[q->prod_tail++ & q->ring_mask] = id; |
| 127 | |
| 128 | /* Order producer and data */ |
| 129 | smp_wmb(); |
| 130 | |
| 131 | WRITE_ONCE(q->ring->producer, q->prod_tail); |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static inline int xskq_reserve_id(struct xsk_queue *q) |
| 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 | { |
| 148 | u32 buff_len; |
| 149 | |
| 150 | if (unlikely(d->idx >= q->umem_props.nframes)) { |
| 151 | q->invalid_descs++; |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | buff_len = q->umem_props.frame_size; |
| 156 | if (unlikely(d->len > buff_len || d->len == 0 || |
| 157 | d->offset > buff_len || d->offset + d->len > buff_len)) { |
| 158 | q->invalid_descs++; |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | static inline struct xdp_desc *xskq_validate_desc(struct xsk_queue *q, |
| 166 | struct xdp_desc *desc) |
| 167 | { |
| 168 | while (q->cons_tail != q->cons_head) { |
| 169 | struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring; |
| 170 | unsigned int idx = q->cons_tail & q->ring_mask; |
| 171 | |
Björn Töpel | 4e64c83 | 2018-06-04 13:57:11 +0200 | [diff] [blame^] | 172 | *desc = READ_ONCE(ring->desc[idx]); |
| 173 | if (xskq_is_valid_desc(q, desc)) |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 174 | return desc; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 175 | |
| 176 | q->cons_tail++; |
| 177 | } |
| 178 | |
| 179 | return NULL; |
| 180 | } |
| 181 | |
| 182 | static inline struct xdp_desc *xskq_peek_desc(struct xsk_queue *q, |
| 183 | struct xdp_desc *desc) |
| 184 | { |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 185 | if (q->cons_tail == q->cons_head) { |
| 186 | WRITE_ONCE(q->ring->consumer, q->cons_tail); |
| 187 | q->cons_head = q->cons_tail + xskq_nb_avail(q, RX_BATCH_SIZE); |
| 188 | |
| 189 | /* Order consumer and data */ |
| 190 | smp_rmb(); |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 191 | } |
| 192 | |
Björn Töpel | 4e64c83 | 2018-06-04 13:57:11 +0200 | [diff] [blame^] | 193 | return xskq_validate_desc(q, desc); |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | static inline void xskq_discard_desc(struct xsk_queue *q) |
| 197 | { |
| 198 | q->cons_tail++; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 199 | } |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 200 | |
| 201 | static inline int xskq_produce_batch_desc(struct xsk_queue *q, |
| 202 | u32 id, u32 len, u16 offset) |
| 203 | { |
| 204 | struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring; |
| 205 | unsigned int idx; |
| 206 | |
| 207 | if (xskq_nb_free(q, q->prod_head, 1) == 0) |
| 208 | return -ENOSPC; |
| 209 | |
| 210 | idx = (q->prod_head++) & q->ring_mask; |
| 211 | ring->desc[idx].idx = id; |
| 212 | ring->desc[idx].len = len; |
| 213 | ring->desc[idx].offset = offset; |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static inline void xskq_produce_flush_desc(struct xsk_queue *q) |
| 219 | { |
| 220 | /* Order producer and data */ |
| 221 | smp_wmb(); |
| 222 | |
| 223 | q->prod_tail = q->prod_head, |
| 224 | WRITE_ONCE(q->ring->producer, q->prod_tail); |
| 225 | } |
| 226 | |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 227 | static inline bool xskq_full_desc(struct xsk_queue *q) |
| 228 | { |
Björn Töpel | da60cf0 | 2018-05-18 14:00:23 +0200 | [diff] [blame] | 229 | return xskq_nb_avail(q, q->nentries) == q->nentries; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 232 | static inline bool xskq_empty_desc(struct xsk_queue *q) |
| 233 | { |
Björn Töpel | da60cf0 | 2018-05-18 14:00:23 +0200 | [diff] [blame] | 234 | 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] | 235 | } |
| 236 | |
Magnus Karlsson | 965a9909 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 237 | 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] | 238 | struct xsk_queue *xskq_create(u32 nentries, bool umem_queue); |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 239 | void xskq_destroy(struct xsk_queue *q_ops); |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 240 | |
| 241 | #endif /* _LINUX_XSK_QUEUE_H */ |