xsk_queue.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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 len = desc->len;
  130. u64 addr, offset;
  131. if (!len)
  132. return false;
  133. /* Can overflow if desc->addr < pool->tx_metadata_len */
  134. if (check_sub_overflow(desc->addr, pool->tx_metadata_len, &addr))
  135. return false;
  136. offset = addr & (pool->chunk_size - 1);
  137. /*
  138. * Can't overflow: @offset is guaranteed to be < ``U32_MAX``
  139. * (pool->chunk_size is ``u32``), @len is guaranteed
  140. * to be <= ``U32_MAX``.
  141. */
  142. if (offset + len + pool->tx_metadata_len > pool->chunk_size)
  143. return false;
  144. if (addr >= pool->addrs_cnt)
  145. return false;
  146. if (xp_unused_options_set(desc->options))
  147. return false;
  148. return true;
  149. }
  150. static inline bool xp_unaligned_validate_desc(struct xsk_buff_pool *pool,
  151. struct xdp_desc *desc)
  152. {
  153. u64 len = desc->len;
  154. u64 addr, end;
  155. if (!len)
  156. return false;
  157. /* Can't overflow: @len is guaranteed to be <= ``U32_MAX`` */
  158. len += pool->tx_metadata_len;
  159. if (len > pool->chunk_size)
  160. return false;
  161. /* Can overflow if desc->addr is close to 0 */
  162. if (check_sub_overflow(xp_unaligned_add_offset_to_addr(desc->addr),
  163. pool->tx_metadata_len, &addr))
  164. return false;
  165. if (addr >= pool->addrs_cnt)
  166. return false;
  167. /* Can overflow if pool->addrs_cnt is high enough */
  168. if (check_add_overflow(addr, len, &end) || end > pool->addrs_cnt)
  169. return false;
  170. if (xp_desc_crosses_non_contig_pg(pool, addr, len))
  171. return false;
  172. if (xp_unused_options_set(desc->options))
  173. return false;
  174. return true;
  175. }
  176. static inline bool xp_validate_desc(struct xsk_buff_pool *pool,
  177. struct xdp_desc *desc)
  178. {
  179. return pool->unaligned ? xp_unaligned_validate_desc(pool, desc) :
  180. xp_aligned_validate_desc(pool, desc);
  181. }
  182. static inline bool xskq_has_descs(struct xsk_queue *q)
  183. {
  184. return q->cached_cons != q->cached_prod;
  185. }
  186. static inline bool xskq_cons_is_valid_desc(struct xsk_queue *q,
  187. struct xdp_desc *d,
  188. struct xsk_buff_pool *pool)
  189. {
  190. if (!xp_validate_desc(pool, d)) {
  191. q->invalid_descs++;
  192. return false;
  193. }
  194. return true;
  195. }
  196. static inline bool xskq_cons_read_desc(struct xsk_queue *q,
  197. struct xdp_desc *desc,
  198. struct xsk_buff_pool *pool)
  199. {
  200. if (q->cached_cons != q->cached_prod) {
  201. struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring;
  202. u32 idx = q->cached_cons & q->ring_mask;
  203. *desc = ring->desc[idx];
  204. return xskq_cons_is_valid_desc(q, desc, pool);
  205. }
  206. q->queue_empty_descs++;
  207. return false;
  208. }
  209. static inline void xskq_cons_release_n(struct xsk_queue *q, u32 cnt)
  210. {
  211. q->cached_cons += cnt;
  212. }
  213. static inline void parse_desc(struct xsk_queue *q, struct xsk_buff_pool *pool,
  214. struct xdp_desc *desc, struct parsed_desc *parsed)
  215. {
  216. parsed->valid = xskq_cons_is_valid_desc(q, desc, pool);
  217. parsed->mb = xp_mb_desc(desc);
  218. }
  219. static inline
  220. u32 xskq_cons_read_desc_batch(struct xsk_queue *q, struct xsk_buff_pool *pool,
  221. u32 max)
  222. {
  223. u32 cached_cons = q->cached_cons, nb_entries = 0;
  224. struct xdp_desc *descs = pool->tx_descs;
  225. u32 total_descs = 0, nr_frags = 0;
  226. /* track first entry, if stumble upon *any* invalid descriptor, rewind
  227. * current packet that consists of frags and stop the processing
  228. */
  229. while (cached_cons != q->cached_prod && nb_entries < max) {
  230. struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring;
  231. u32 idx = cached_cons & q->ring_mask;
  232. struct parsed_desc parsed;
  233. descs[nb_entries] = ring->desc[idx];
  234. cached_cons++;
  235. parse_desc(q, pool, &descs[nb_entries], &parsed);
  236. if (unlikely(!parsed.valid))
  237. break;
  238. if (likely(!parsed.mb)) {
  239. total_descs += (nr_frags + 1);
  240. nr_frags = 0;
  241. } else {
  242. nr_frags++;
  243. if (nr_frags == pool->netdev->xdp_zc_max_segs) {
  244. nr_frags = 0;
  245. break;
  246. }
  247. }
  248. nb_entries++;
  249. }
  250. cached_cons -= nr_frags;
  251. /* Release valid plus any invalid entries */
  252. xskq_cons_release_n(q, cached_cons - q->cached_cons);
  253. return total_descs;
  254. }
  255. /* Functions for consumers */
  256. static inline void __xskq_cons_release(struct xsk_queue *q)
  257. {
  258. smp_store_release(&q->ring->consumer, q->cached_cons); /* D, matchees A */
  259. }
  260. static inline void __xskq_cons_peek(struct xsk_queue *q)
  261. {
  262. /* Refresh the local pointer */
  263. q->cached_prod = smp_load_acquire(&q->ring->producer); /* C, matches B */
  264. }
  265. static inline void xskq_cons_get_entries(struct xsk_queue *q)
  266. {
  267. __xskq_cons_release(q);
  268. __xskq_cons_peek(q);
  269. }
  270. static inline u32 xskq_cons_nb_entries(struct xsk_queue *q, u32 max)
  271. {
  272. u32 entries = q->cached_prod - q->cached_cons;
  273. if (entries >= max)
  274. return max;
  275. __xskq_cons_peek(q);
  276. entries = q->cached_prod - q->cached_cons;
  277. return entries >= max ? max : entries;
  278. }
  279. static inline bool xskq_cons_peek_addr_unchecked(struct xsk_queue *q, u64 *addr)
  280. {
  281. if (q->cached_prod == q->cached_cons)
  282. xskq_cons_get_entries(q);
  283. return xskq_cons_read_addr_unchecked(q, addr);
  284. }
  285. static inline bool xskq_cons_peek_desc(struct xsk_queue *q,
  286. struct xdp_desc *desc,
  287. struct xsk_buff_pool *pool)
  288. {
  289. if (q->cached_prod == q->cached_cons)
  290. xskq_cons_get_entries(q);
  291. return xskq_cons_read_desc(q, desc, pool);
  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. */
  297. static inline void xskq_cons_release(struct xsk_queue *q)
  298. {
  299. q->cached_cons++;
  300. }
  301. static inline void xskq_cons_cancel_n(struct xsk_queue *q, u32 cnt)
  302. {
  303. q->cached_cons -= cnt;
  304. }
  305. static inline u32 xskq_cons_present_entries(struct xsk_queue *q)
  306. {
  307. /* No barriers needed since data is not accessed */
  308. return READ_ONCE(q->ring->producer) - READ_ONCE(q->ring->consumer);
  309. }
  310. /* Functions for producers */
  311. static inline u32 xskq_prod_nb_free(struct xsk_queue *q, u32 max)
  312. {
  313. u32 free_entries = q->nentries - (q->cached_prod - q->cached_cons);
  314. if (free_entries >= max)
  315. return max;
  316. /* Refresh the local tail pointer */
  317. q->cached_cons = READ_ONCE(q->ring->consumer);
  318. free_entries = q->nentries - (q->cached_prod - q->cached_cons);
  319. return free_entries >= max ? max : free_entries;
  320. }
  321. static inline bool xskq_prod_is_full(struct xsk_queue *q)
  322. {
  323. return xskq_prod_nb_free(q, 1) ? false : true;
  324. }
  325. static inline void xskq_prod_cancel_n(struct xsk_queue *q, u32 cnt)
  326. {
  327. q->cached_prod -= cnt;
  328. }
  329. static inline int xskq_prod_reserve(struct xsk_queue *q)
  330. {
  331. if (xskq_prod_is_full(q))
  332. return -ENOSPC;
  333. /* A, matches D */
  334. q->cached_prod++;
  335. return 0;
  336. }
  337. static inline int xskq_prod_reserve_addr(struct xsk_queue *q, u64 addr)
  338. {
  339. struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring;
  340. if (xskq_prod_is_full(q))
  341. return -ENOSPC;
  342. /* A, matches D */
  343. ring->desc[q->cached_prod++ & q->ring_mask] = addr;
  344. return 0;
  345. }
  346. static inline void xskq_prod_write_addr_batch(struct xsk_queue *q, struct xdp_desc *descs,
  347. u32 nb_entries)
  348. {
  349. struct xdp_umem_ring *ring = (struct xdp_umem_ring *)q->ring;
  350. u32 i, cached_prod;
  351. /* A, matches D */
  352. cached_prod = q->cached_prod;
  353. for (i = 0; i < nb_entries; i++)
  354. ring->desc[cached_prod++ & q->ring_mask] = descs[i].addr;
  355. q->cached_prod = cached_prod;
  356. }
  357. static inline int xskq_prod_reserve_desc(struct xsk_queue *q,
  358. u64 addr, u32 len, u32 flags)
  359. {
  360. struct xdp_rxtx_ring *ring = (struct xdp_rxtx_ring *)q->ring;
  361. u32 idx;
  362. if (xskq_prod_is_full(q))
  363. return -ENOBUFS;
  364. /* A, matches D */
  365. idx = q->cached_prod++ & q->ring_mask;
  366. ring->desc[idx].addr = addr;
  367. ring->desc[idx].len = len;
  368. ring->desc[idx].options = flags;
  369. return 0;
  370. }
  371. static inline void __xskq_prod_submit(struct xsk_queue *q, u32 idx)
  372. {
  373. smp_store_release(&q->ring->producer, idx); /* B, matches C */
  374. }
  375. static inline void xskq_prod_submit(struct xsk_queue *q)
  376. {
  377. __xskq_prod_submit(q, q->cached_prod);
  378. }
  379. static inline void xskq_prod_submit_n(struct xsk_queue *q, u32 nb_entries)
  380. {
  381. __xskq_prod_submit(q, q->ring->producer + nb_entries);
  382. }
  383. static inline bool xskq_prod_is_empty(struct xsk_queue *q)
  384. {
  385. /* No barriers needed since data is not accessed */
  386. return READ_ONCE(q->ring->consumer) == READ_ONCE(q->ring->producer);
  387. }
  388. /* For both producers and consumers */
  389. static inline u64 xskq_nb_invalid_descs(struct xsk_queue *q)
  390. {
  391. return q ? q->invalid_descs : 0;
  392. }
  393. static inline u64 xskq_nb_queue_empty_descs(struct xsk_queue *q)
  394. {
  395. return q ? q->queue_empty_descs : 0;
  396. }
  397. struct xsk_queue *xskq_create(u32 nentries, bool umem_queue);
  398. void xskq_destroy(struct xsk_queue *q_ops);
  399. #endif /* _LINUX_XSK_QUEUE_H */