xsk_queue.h 12 KB

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