xsk_buff_pool.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright(c) 2020 Intel Corporation. */
  3. #ifndef XSK_BUFF_POOL_H_
  4. #define XSK_BUFF_POOL_H_
  5. #include <linux/if_xdp.h>
  6. #include <linux/types.h>
  7. #include <linux/dma-mapping.h>
  8. #include <linux/bpf.h>
  9. #include <net/xdp.h>
  10. struct xsk_buff_pool;
  11. struct xdp_rxq_info;
  12. struct xsk_cb_desc;
  13. struct xsk_queue;
  14. struct xdp_desc;
  15. struct xdp_umem;
  16. struct xdp_sock;
  17. struct device;
  18. struct page;
  19. #define XSK_PRIV_MAX 24
  20. struct xdp_buff_xsk {
  21. struct xdp_buff xdp;
  22. u8 cb[XSK_PRIV_MAX];
  23. dma_addr_t dma;
  24. dma_addr_t frame_dma;
  25. struct xsk_buff_pool *pool;
  26. struct list_head list_node;
  27. } __aligned_largest;
  28. #define XSK_CHECK_PRIV_TYPE(t) BUILD_BUG_ON(sizeof(t) > offsetofend(struct xdp_buff_xsk, cb))
  29. #define XSK_TX_COMPL_FITS(t) BUILD_BUG_ON(sizeof(struct xsk_tx_metadata_compl) > sizeof(t))
  30. struct xsk_dma_map {
  31. dma_addr_t *dma_pages;
  32. struct device *dev;
  33. struct net_device *netdev;
  34. refcount_t users;
  35. struct list_head list; /* Protected by the RTNL_LOCK */
  36. u32 dma_pages_cnt;
  37. };
  38. struct xsk_buff_pool {
  39. /* Members only used in the control path first. */
  40. struct device *dev;
  41. struct net_device *netdev;
  42. struct list_head xsk_tx_list;
  43. /* Protects modifications to the xsk_tx_list */
  44. spinlock_t xsk_tx_list_lock;
  45. refcount_t users;
  46. struct xdp_umem *umem;
  47. struct work_struct work;
  48. /* Protects generic receive in shared and non-shared umem mode. */
  49. spinlock_t rx_lock;
  50. struct list_head free_list;
  51. struct list_head xskb_list;
  52. u32 heads_cnt;
  53. u16 queue_id;
  54. /* Data path members as close to free_heads at the end as possible. */
  55. struct xsk_queue *fq ____cacheline_aligned_in_smp;
  56. struct xsk_queue *cq;
  57. /* For performance reasons, each buff pool has its own array of dma_pages
  58. * even when they are identical.
  59. */
  60. dma_addr_t *dma_pages;
  61. struct xdp_buff_xsk *heads;
  62. struct xdp_desc *tx_descs;
  63. u64 chunk_mask;
  64. u64 addrs_cnt;
  65. u32 free_list_cnt;
  66. u32 dma_pages_cnt;
  67. u32 free_heads_cnt;
  68. u32 headroom;
  69. u32 chunk_size;
  70. u32 chunk_shift;
  71. u32 frame_len;
  72. u32 xdp_zc_max_segs;
  73. u8 tx_metadata_len; /* inherited from umem */
  74. u8 cached_need_wakeup;
  75. bool uses_need_wakeup;
  76. bool unaligned;
  77. bool tx_sw_csum;
  78. void *addrs;
  79. /* Mutual exclusion of the completion ring in the SKB mode.
  80. * Protect: NAPI TX thread and sendmsg error paths in the SKB
  81. * destructor callback.
  82. */
  83. spinlock_t cq_prod_lock;
  84. struct xdp_buff_xsk *free_heads[];
  85. };
  86. /* Masks for xdp_umem_page flags.
  87. * The low 12-bits of the addr will be 0 since this is the page address, so we
  88. * can use them for flags.
  89. */
  90. #define XSK_NEXT_PG_CONTIG_SHIFT 0
  91. #define XSK_NEXT_PG_CONTIG_MASK BIT_ULL(XSK_NEXT_PG_CONTIG_SHIFT)
  92. /* AF_XDP core. */
  93. struct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs,
  94. struct xdp_umem *umem);
  95. int xp_assign_dev(struct xsk_buff_pool *pool, struct net_device *dev,
  96. u16 queue_id, u16 flags);
  97. int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_sock *umem_xs,
  98. struct net_device *dev, u16 queue_id);
  99. int xp_alloc_tx_descs(struct xsk_buff_pool *pool, struct xdp_sock *xs);
  100. void xp_destroy(struct xsk_buff_pool *pool);
  101. void xp_get_pool(struct xsk_buff_pool *pool);
  102. bool xp_put_pool(struct xsk_buff_pool *pool);
  103. void xp_clear_dev(struct xsk_buff_pool *pool);
  104. void xp_add_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs);
  105. void xp_del_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs);
  106. /* AF_XDP, and XDP core. */
  107. void xp_free(struct xdp_buff_xsk *xskb);
  108. static inline void xp_init_xskb_addr(struct xdp_buff_xsk *xskb, struct xsk_buff_pool *pool,
  109. u64 addr)
  110. {
  111. xskb->xdp.data_hard_start = pool->addrs + addr + pool->headroom;
  112. }
  113. static inline void xp_init_xskb_dma(struct xdp_buff_xsk *xskb, struct xsk_buff_pool *pool,
  114. dma_addr_t *dma_pages, u64 addr)
  115. {
  116. xskb->frame_dma = (dma_pages[addr >> PAGE_SHIFT] & ~XSK_NEXT_PG_CONTIG_MASK) +
  117. (addr & ~PAGE_MASK);
  118. xskb->dma = xskb->frame_dma + pool->headroom + XDP_PACKET_HEADROOM;
  119. }
  120. /* AF_XDP ZC drivers, via xdp_sock_buff.h */
  121. void xp_set_rxq_info(struct xsk_buff_pool *pool, struct xdp_rxq_info *rxq);
  122. void xp_fill_cb(struct xsk_buff_pool *pool, struct xsk_cb_desc *desc);
  123. int xp_dma_map(struct xsk_buff_pool *pool, struct device *dev,
  124. unsigned long attrs, struct page **pages, u32 nr_pages);
  125. void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs);
  126. struct xdp_buff *xp_alloc(struct xsk_buff_pool *pool);
  127. u32 xp_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max);
  128. bool xp_can_alloc(struct xsk_buff_pool *pool, u32 count);
  129. void *xp_raw_get_data(struct xsk_buff_pool *pool, u64 addr);
  130. dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr);
  131. struct xdp_desc_ctx {
  132. dma_addr_t dma;
  133. struct xsk_tx_metadata *meta;
  134. };
  135. struct xdp_desc_ctx xp_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr);
  136. static inline dma_addr_t xp_get_dma(struct xdp_buff_xsk *xskb)
  137. {
  138. return xskb->dma;
  139. }
  140. static inline dma_addr_t xp_get_frame_dma(struct xdp_buff_xsk *xskb)
  141. {
  142. return xskb->frame_dma;
  143. }
  144. static inline void xp_dma_sync_for_cpu(struct xdp_buff_xsk *xskb)
  145. {
  146. dma_sync_single_for_cpu(xskb->pool->dev, xskb->dma,
  147. xskb->pool->frame_len,
  148. DMA_BIDIRECTIONAL);
  149. }
  150. static inline void xp_dma_sync_for_device(struct xsk_buff_pool *pool,
  151. dma_addr_t dma, size_t size)
  152. {
  153. dma_sync_single_for_device(pool->dev, dma, size, DMA_BIDIRECTIONAL);
  154. }
  155. /* Masks for xdp_umem_page flags.
  156. * The low 12-bits of the addr will be 0 since this is the page address, so we
  157. * can use them for flags.
  158. */
  159. #define XSK_NEXT_PG_CONTIG_SHIFT 0
  160. #define XSK_NEXT_PG_CONTIG_MASK BIT_ULL(XSK_NEXT_PG_CONTIG_SHIFT)
  161. static inline bool xp_desc_crosses_non_contig_pg(struct xsk_buff_pool *pool,
  162. u64 addr, u32 len)
  163. {
  164. bool cross_pg = (addr & (PAGE_SIZE - 1)) + len > PAGE_SIZE;
  165. if (likely(!cross_pg))
  166. return false;
  167. return pool->dma_pages &&
  168. !(pool->dma_pages[addr >> PAGE_SHIFT] & XSK_NEXT_PG_CONTIG_MASK);
  169. }
  170. static inline bool xp_mb_desc(const struct xdp_desc *desc)
  171. {
  172. return desc->options & XDP_PKT_CONTD;
  173. }
  174. static inline u64 xp_aligned_extract_addr(struct xsk_buff_pool *pool, u64 addr)
  175. {
  176. return addr & pool->chunk_mask;
  177. }
  178. static inline u64 xp_unaligned_extract_addr(u64 addr)
  179. {
  180. return addr & XSK_UNALIGNED_BUF_ADDR_MASK;
  181. }
  182. static inline u64 xp_unaligned_extract_offset(u64 addr)
  183. {
  184. return addr >> XSK_UNALIGNED_BUF_OFFSET_SHIFT;
  185. }
  186. static inline u64 xp_unaligned_add_offset_to_addr(u64 addr)
  187. {
  188. return xp_unaligned_extract_addr(addr) +
  189. xp_unaligned_extract_offset(addr);
  190. }
  191. static inline u32 xp_aligned_extract_idx(struct xsk_buff_pool *pool, u64 addr)
  192. {
  193. return xp_aligned_extract_addr(pool, addr) >> pool->chunk_shift;
  194. }
  195. static inline void xp_release(struct xdp_buff_xsk *xskb)
  196. {
  197. if (xskb->pool->unaligned)
  198. xskb->pool->free_heads[xskb->pool->free_heads_cnt++] = xskb;
  199. }
  200. static inline u64 xp_get_handle(struct xdp_buff_xsk *xskb,
  201. struct xsk_buff_pool *pool)
  202. {
  203. u64 orig_addr = xskb->xdp.data - pool->addrs;
  204. u64 offset;
  205. if (!pool->unaligned)
  206. return orig_addr;
  207. offset = xskb->xdp.data - xskb->xdp.data_hard_start;
  208. offset += pool->headroom;
  209. orig_addr -= offset;
  210. return orig_addr + (offset << XSK_UNALIGNED_BUF_OFFSET_SHIFT);
  211. }
  212. static inline bool xp_tx_metadata_enabled(const struct xsk_buff_pool *pool)
  213. {
  214. return pool->tx_metadata_len > 0;
  215. }
  216. #endif /* XSK_BUFF_POOL_H_ */