xsk_queue.h 13 KB

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