xsk_queue.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* XDP user-space ring structure
  3. * Copyright(c) 2018 Intel Corporation.
  4. */
  5. #ifndef _LINUX_XSK_QUEUE_H
  6. #define _LINUX_XSK_QUEUE_H
  7. #include <linux/types.h>
  8. #include <linux/if_xdp.h>
  9. #include <net/xdp_sock.h>
  10. #include <net/xsk_buff_pool.h>
  11. #include "xsk.h"
  12. struct xdp_ring {
  13. u32 producer ____cacheline_aligned_in_smp;
  14. /* Hinder the adjacent cache prefetcher to prefetch the consumer
  15. * pointer if the producer pointer is touched and vice versa.
  16. */
  17. u32 pad1 ____cacheline_aligned_in_smp;
  18. u32 consumer ____cacheline_aligned_in_smp;
  19. u32 pad2 ____cacheline_aligned_in_smp;
  20. u32 flags;
  21. u32 pad3 ____cacheline_aligned_in_smp;
  22. };
  23. /* Used for the RX and TX queues for packets */
  24. struct xdp_rxtx_ring {
  25. struct xdp_ring ptrs;
  26. struct xdp_desc desc[] ____cacheline_aligned_in_smp;
  27. };
  28. /* Used for the fill and completion queues for buffers */
  29. struct xdp_umem_ring {
  30. struct xdp_ring ptrs;
  31. u64 desc[] ____cacheline_aligned_in_smp;
  32. };
  33. struct xsk_queue {
  34. u32 ring_mask;
  35. u32 nentries;
  36. u32 cached_prod;
  37. u32 cached_cons;
  38. struct xdp_ring *ring;
  39. u64 invalid_descs;
  40. u64 queue_empty_descs;
  41. size_t ring_vmalloc_size;
  42. };
  43. struct parsed_desc {
  44. u32 mb;
  45. u32 valid;
  46. };
  47. /* The structure of the shared state of the rings are a simple
  48. * circular buffer, as outlined in
  49. * Documentation/core-api/circular-buffers.rst. For the Rx and
  50. * completion ring, the kernel is the producer and user space is the
  51. * consumer. For the Tx and fill rings, the kernel is the consumer and
  52. * user space is the producer.
  53. *
  54. * producer consumer
  55. *
  56. * if (LOAD ->consumer) { (A) LOAD.acq ->producer (C)
  57. * STORE $data LOAD $data
  58. * STORE.rel ->producer (B) STORE.rel ->consumer (D)
  59. * }
  60. *
  61. * (A) pairs with (D), and (B) pairs with (C).
  62. *
  63. * Starting with (B), it protects the data from being written after
  64. * the producer pointer. If this barrier was missing, the consumer
  65. * could observe the producer pointer being set and thus load the data
  66. * before the producer has written the new data. The consumer would in
  67. * this case load the old data.
  68. *
  69. * (C) protects the consumer from speculatively loading the data before
  70. * the producer pointer actually has been read. If we do not have this
  71. * barrier, some architectures could load old data as speculative loads
  72. * are not discarded as the CPU does not know there is a dependency
  73. * between ->producer and data.
  74. *
  75. * (A) is a control dependency that separates the load of ->consumer
  76. * from the stores of $data. In case ->consumer indicates there is no
  77. * room in the buffer to store $data we do not. The dependency will
  78. * order both of the stores after the loads. So no barrier is needed.
  79. *
  80. * (D) protects the load of the data to be observed to happen after the
  81. * store of the consumer pointer. If we did not have this memory
  82. * barrier, the producer could observe the consumer pointer being set
  83. * and overwrite the data with a new value before the consumer got the
  84. * chance to read the old value. The consumer would thus miss reading
  85. * the old entry and very likely read the new entry twice, once right
  86. * now and again after circling through the ring.
  87. */
  88. /* The operations on the rings are the following:
  89. *
  90. * producer consumer
  91. *
  92. * RESERVE entries PEEK in the ring for entries
  93. * WRITE data into the ring READ data from the ring
  94. * SUBMIT entries RELEASE entries
  95. *
  96. * The producer reserves one or more entries in the ring. It can then
  97. * fill in these entries and finally submit them so that they can be
  98. * seen and read by the consumer.
  99. *
  100. * The consumer peeks into the ring to see if the producer has written
  101. * any new entries. If so, the consumer can then read these entries
  102. * and when it is done reading them release them back to the producer
  103. * so that the producer can use these slots to fill in new entries.
  104. *
  105. * The function names below reflect these operations.
  106. */
  107. /* Functions that read and validate content from consumer rings. */
  108. static inline void __xskq_cons_read_addr_unchecked(struct xsk_queue *q, u32 cached_cons, u64 *addr)
  109. {
  110. struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring;
  111. u32 idx = cached_cons & q->ring_mask;
  112. *addr = ring->desc[idx];
  113. }
  114. static inline bool xskq_cons_read_addr_unchecked(struct xsk_queue *q, u64 *addr)
  115. {
  116. if (q->cached_cons != q->cached_prod) {
  117. __xskq_cons_read_addr_unchecked(q, q->cached_cons, addr);
  118. return true;
  119. }
  120. return false;
  121. }
  122. static inline bool xp_unused_options_set(u32 options)
  123. {
  124. return options & ~(XDP_PKT_CONTD | XDP_TX_METADATA);
  125. }
  126. static inline bool xp_aligned_validate_desc(struct xsk_buff_pool *pool,
  127. struct xdp_desc *desc)
  128. {
  129. u64 addr = desc->addr - pool->tx_metadata_len;
  130. u64 len = desc->len + pool->tx_metadata_len;
  131. u64 offset = addr & (pool->chunk_size - 1);
  132. if (!desc->len)
  133. return false;
  134. if (offset + len > pool->chunk_size)
  135. return false;
  136. if (addr >= pool->addrs_cnt)
  137. return false;
  138. if (xp_unused_options_set(desc->options))
  139. return false;
  140. return true;
  141. }
  142. static inline bool xp_unaligned_validate_desc(struct xsk_buff_pool *pool,
  143. struct xdp_desc *desc)
  144. {
  145. u64 addr = xp_unaligned_add_offset_to_addr(desc->addr) - pool->tx_metadata_len;
  146. u64 len = desc->len + pool->tx_metadata_len;
  147. if (!desc->len)
  148. return false;
  149. if (len > pool->chunk_size)
  150. return false;
  151. if (addr >= pool->addrs_cnt || addr + len > pool->addrs_cnt ||
  152. xp_desc_crosses_non_contig_pg(pool, addr, len))
  153. return false;
  154. if (xp_unused_options_set(desc->options))
  155. return false;
  156. return true;
  157. }
  158. static inline bool xp_validate_desc(struct xsk_buff_pool *pool,
  159. struct xdp_desc *desc)
  160. {
  161. return pool->unaligned ? xp_unaligned_validate_desc(pool, desc) :
  162. xp_aligned_validate_desc(pool, desc);
  163. }
  164. static inline bool xskq_has_descs(struct xsk_queue *q)
  165. {
  166. return q->cached_cons != q->cached_prod;
  167. }
  168. static inline bool xskq_cons_is_valid_desc(struct xsk_queue *q,
  169. struct xdp_desc *d,
  170. struct xsk_buff_pool *pool)
  171. {
  172. if (!xp_validate_desc(pool, d)) {
  173. q->invalid_descs++;
  174. return false;
  175. }
  176. return true;
  177. }
  178. static inline bool xskq_cons_read_desc(struct xsk_queue *q,
  179. struct xdp_desc *desc,
  180. struct xsk_buff_pool *pool)
  181. {
  182. if (q->cached_cons != q->cached_prod) {
  183. struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring;
  184. u32 idx = q->cached_cons & q->ring_mask;
  185. *desc = ring->desc[idx];
  186. return xskq_cons_is_valid_desc(q, desc, pool);
  187. }
  188. q->queue_empty_descs++;
  189. return false;
  190. }
  191. static inline void xskq_cons_release_n(struct xsk_queue *q, u32 cnt)
  192. {
  193. q->cached_cons += cnt;
  194. }
  195. static inline void parse_desc(struct xsk_queue *q, struct xsk_buff_pool *pool,
  196. struct xdp_desc *desc, struct parsed_desc *parsed)
  197. {
  198. parsed->valid = xskq_cons_is_valid_desc(q, desc, pool);
  199. parsed->mb = xp_mb_desc(desc);
  200. }
  201. static inline
  202. u32 xskq_cons_read_desc_batch(struct xsk_queue *q, struct xsk_buff_pool *pool,
  203. u32 max)
  204. {
  205. u32 cached_cons = q->cached_cons, nb_entries = 0;
  206. struct xdp_desc *descs = pool->tx_descs;
  207. u32 total_descs = 0, nr_frags = 0;
  208. /* track first entry, if stumble upon *any* invalid descriptor, rewind
  209. * current packet that consists of frags and stop the processing
  210. */
  211. while (cached_cons != q->cached_prod && nb_entries < max) {
  212. struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring;
  213. u32 idx = cached_cons & q->ring_mask;
  214. struct parsed_desc parsed;
  215. descs[nb_entries] = ring->desc[idx];
  216. cached_cons++;
  217. parse_desc(q, pool, &descs[nb_entries], &parsed);
  218. if (unlikely(!parsed.valid))
  219. break;
  220. if (likely(!parsed.mb)) {
  221. total_descs += (nr_frags + 1);
  222. nr_frags = 0;
  223. } else {
  224. nr_frags++;
  225. if (nr_frags == pool->netdev->xdp_zc_max_segs) {
  226. nr_frags = 0;
  227. break;
  228. }
  229. }
  230. nb_entries++;
  231. }
  232. cached_cons -= nr_frags;
  233. /* Release valid plus any invalid entries */
  234. xskq_cons_release_n(q, cached_cons - q->cached_cons);
  235. return total_descs;
  236. }
  237. /* Functions for consumers */
  238. static inline void __xskq_cons_release(struct xsk_queue *q)
  239. {
  240. smp_store_release(&q->ring->consumer, q->cached_cons); /* D, matchees A */
  241. }
  242. static inline void __xskq_cons_peek(struct xsk_queue *q)
  243. {
  244. /* Refresh the local pointer */
  245. q->cached_prod = smp_load_acquire(&q->ring->producer); /* C, matches B */
  246. }
  247. static inline void xskq_cons_get_entries(struct xsk_queue *q)
  248. {
  249. __xskq_cons_release(q);
  250. __xskq_cons_peek(q);
  251. }
  252. static inline u32 xskq_cons_nb_entries(struct xsk_queue *q, u32 max)
  253. {
  254. u32 entries = q->cached_prod - q->cached_cons;
  255. if (entries >= max)
  256. return max;
  257. __xskq_cons_peek(q);
  258. entries = q->cached_prod - q->cached_cons;
  259. return entries >= max ? max : entries;
  260. }
  261. static inline bool xskq_cons_peek_addr_unchecked(struct xsk_queue *q, u64 *addr)
  262. {
  263. if (q->cached_prod == q->cached_cons)
  264. xskq_cons_get_entries(q);
  265. return xskq_cons_read_addr_unchecked(q, addr);
  266. }
  267. static inline bool xskq_cons_peek_desc(struct xsk_queue *q,
  268. struct xdp_desc *desc,
  269. struct xsk_buff_pool *pool)
  270. {
  271. if (q->cached_prod == q->cached_cons)
  272. xskq_cons_get_entries(q);
  273. return xskq_cons_read_desc(q, desc, pool);
  274. }
  275. /* To improve performance in the xskq_cons_release functions, only update local state here.
  276. * Reflect this to global state when we get new entries from the ring in
  277. * xskq_cons_get_entries() and whenever Rx or Tx processing are completed in the NAPI loop.
  278. */
  279. static inline void xskq_cons_release(struct xsk_queue *q)
  280. {
  281. q->cached_cons++;
  282. }
  283. static inline void xskq_cons_cancel_n(struct xsk_queue *q, u32 cnt)
  284. {
  285. q->cached_cons -= cnt;
  286. }
  287. static inline u32 xskq_cons_present_entries(struct xsk_queue *q)
  288. {
  289. /* No barriers needed since data is not accessed */
  290. return READ_ONCE(q->ring->producer) - READ_ONCE(q->ring->consumer);
  291. }
  292. /* Functions for producers */
  293. static inline u32 xskq_prod_nb_free(struct xsk_queue *q, u32 max)
  294. {
  295. u32 free_entries = q->nentries - (q->cached_prod - q->cached_cons);
  296. if (free_entries >= max)
  297. return max;
  298. /* Refresh the local tail pointer */
  299. q->cached_cons = READ_ONCE(q->ring->consumer);
  300. free_entries = q->nentries - (q->cached_prod - q->cached_cons);
  301. return free_entries >= max ? max : free_entries;
  302. }
  303. static inline bool xskq_prod_is_full(struct xsk_queue *q)
  304. {
  305. return xskq_prod_nb_free(q, 1) ? false : true;
  306. }
  307. static inline void xskq_prod_cancel_n(struct xsk_queue *q, u32 cnt)
  308. {
  309. q->cached_prod -= cnt;
  310. }
  311. static inline int xskq_prod_reserve(struct xsk_queue *q)
  312. {
  313. if (xskq_prod_is_full(q))
  314. return -ENOSPC;
  315. /* A, matches D */
  316. q->cached_prod++;
  317. return 0;
  318. }
  319. static inline int xskq_prod_reserve_addr(struct xsk_queue *q, u64 addr)
  320. {
  321. struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring;
  322. if (xskq_prod_is_full(q))
  323. return -ENOSPC;
  324. /* A, matches D */
  325. ring->desc[q->cached_prod++ & q->ring_mask] = addr;
  326. return 0;
  327. }
  328. static inline void xskq_prod_write_addr_batch(struct xsk_queue *q, struct xdp_desc *descs,
  329. u32 nb_entries)
  330. {
  331. struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring;
  332. u32 i, cached_prod;
  333. /* A, matches D */
  334. cached_prod = q->cached_prod;
  335. for (i = 0; i < nb_entries; i++)
  336. ring->desc[cached_prod++ & q->ring_mask] = descs[i].addr;
  337. q->cached_prod = cached_prod;
  338. }
  339. static inline int xskq_prod_reserve_desc(struct xsk_queue *q,
  340. u64 addr, u32 len, u32 flags)
  341. {
  342. struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring;
  343. u32 idx;
  344. if (xskq_prod_is_full(q))
  345. return -ENOBUFS;
  346. /* A, matches D */
  347. idx = q->cached_prod++ & q->ring_mask;
  348. ring->desc[idx].addr = addr;
  349. ring->desc[idx].len = len;
  350. ring->desc[idx].options = flags;
  351. return 0;
  352. }
  353. static inline void __xskq_prod_submit(struct xsk_queue *q, u32 idx)
  354. {
  355. smp_store_release(&q->ring->producer, idx); /* B, matches C */
  356. }
  357. static inline void xskq_prod_submit(struct xsk_queue *q)
  358. {
  359. __xskq_prod_submit(q, q->cached_prod);
  360. }
  361. static inline void xskq_prod_submit_n(struct xsk_queue *q, u32 nb_entries)
  362. {
  363. __xskq_prod_submit(q, q->ring->producer + nb_entries);
  364. }
  365. static inline bool xskq_prod_is_empty(struct xsk_queue *q)
  366. {
  367. /* No barriers needed since data is not accessed */
  368. return READ_ONCE(q->ring->consumer) == READ_ONCE(q->ring->producer);
  369. }
  370. /* For both producers and consumers */
  371. static inline u64 xskq_nb_invalid_descs(struct xsk_queue *q)
  372. {
  373. return q ? q->invalid_descs : 0;
  374. }
  375. static inline u64 xskq_nb_queue_empty_descs(struct xsk_queue *q)
  376. {
  377. return q ? q->queue_empty_descs : 0;
  378. }
  379. struct xsk_queue *xskq_create(u32 nentries, bool umem_queue);
  380. void xskq_destroy(struct xsk_queue *q_ops);
  381. #endif /* _LINUX_XSK_QUEUE_H */