xsk_buff_pool.h 7.0 KB

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