xdp_sock_drv.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Interface for implementing AF_XDP zero-copy support in drivers.
  3. * Copyright(c) 2020 Intel Corporation.
  4. */
  5. #ifndef _LINUX_XDP_SOCK_DRV_H
  6. #define _LINUX_XDP_SOCK_DRV_H
  7. #include <net/xdp_sock.h>
  8. #include <net/xsk_buff_pool.h>
  9. #define XDP_UMEM_MIN_CHUNK_SHIFT 11
  10. #define XDP_UMEM_MIN_CHUNK_SIZE (1 << XDP_UMEM_MIN_CHUNK_SHIFT)
  11. #ifdef CONFIG_XDP_SOCKETS
  12. void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries);
  13. bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc);
  14. u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 max);
  15. void xsk_tx_release(struct xsk_buff_pool *pool);
  16. struct xsk_buff_pool *xsk_get_pool_from_qid(struct net_device *dev,
  17. u16 queue_id);
  18. void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool);
  19. void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool);
  20. void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool);
  21. void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool);
  22. bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool);
  23. static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool)
  24. {
  25. return XDP_PACKET_HEADROOM + pool->headroom;
  26. }
  27. static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool)
  28. {
  29. return pool->chunk_size;
  30. }
  31. static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)
  32. {
  33. return xsk_pool_get_chunk_size(pool) - xsk_pool_get_headroom(pool);
  34. }
  35. static inline void xsk_pool_set_rxq_info(struct xsk_buff_pool *pool,
  36. struct xdp_rxq_info *rxq)
  37. {
  38. xp_set_rxq_info(pool, rxq);
  39. }
  40. static inline unsigned int xsk_pool_get_napi_id(struct xsk_buff_pool *pool)
  41. {
  42. #ifdef CONFIG_NET_RX_BUSY_POLL
  43. return pool->heads[0].xdp.rxq->napi_id;
  44. #else
  45. return 0;
  46. #endif
  47. }
  48. static inline void xsk_pool_dma_unmap(struct xsk_buff_pool *pool,
  49. unsigned long attrs)
  50. {
  51. xp_dma_unmap(pool, attrs);
  52. }
  53. static inline int xsk_pool_dma_map(struct xsk_buff_pool *pool,
  54. struct device *dev, unsigned long attrs)
  55. {
  56. struct xdp_umem *umem = pool->umem;
  57. return xp_dma_map(pool, dev, attrs, umem->pgs, umem->npgs);
  58. }
  59. static inline dma_addr_t xsk_buff_xdp_get_dma(struct xdp_buff *xdp)
  60. {
  61. struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
  62. return xp_get_dma(xskb);
  63. }
  64. static inline dma_addr_t xsk_buff_xdp_get_frame_dma(struct xdp_buff *xdp)
  65. {
  66. struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
  67. return xp_get_frame_dma(xskb);
  68. }
  69. static inline struct xdp_buff *xsk_buff_alloc(struct xsk_buff_pool *pool)
  70. {
  71. return xp_alloc(pool);
  72. }
  73. /* Returns as many entries as possible up to max. 0 <= N <= max. */
  74. static inline u32 xsk_buff_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
  75. {
  76. return xp_alloc_batch(pool, xdp, max);
  77. }
  78. static inline bool xsk_buff_can_alloc(struct xsk_buff_pool *pool, u32 count)
  79. {
  80. return xp_can_alloc(pool, count);
  81. }
  82. static inline void xsk_buff_free(struct xdp_buff *xdp)
  83. {
  84. struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
  85. xp_free(xskb);
  86. }
  87. static inline void xsk_buff_set_size(struct xdp_buff *xdp, u32 size)
  88. {
  89. xdp->data = xdp->data_hard_start + XDP_PACKET_HEADROOM;
  90. xdp->data_meta = xdp->data;
  91. xdp->data_end = xdp->data + size;
  92. }
  93. static inline dma_addr_t xsk_buff_raw_get_dma(struct xsk_buff_pool *pool,
  94. u64 addr)
  95. {
  96. return xp_raw_get_dma(pool, addr);
  97. }
  98. static inline void *xsk_buff_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
  99. {
  100. return xp_raw_get_data(pool, addr);
  101. }
  102. static inline void xsk_buff_dma_sync_for_cpu(struct xdp_buff *xdp, struct xsk_buff_pool *pool)
  103. {
  104. struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
  105. if (!pool->dma_need_sync)
  106. return;
  107. xp_dma_sync_for_cpu(xskb);
  108. }
  109. static inline void xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool *pool,
  110. dma_addr_t dma,
  111. size_t size)
  112. {
  113. xp_dma_sync_for_device(pool, dma, size);
  114. }
  115. #else
  116. static inline void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries)
  117. {
  118. }
  119. static inline bool xsk_tx_peek_desc(struct xsk_buff_pool *pool,
  120. struct xdp_desc *desc)
  121. {
  122. return false;
  123. }
  124. static inline u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 max)
  125. {
  126. return 0;
  127. }
  128. static inline void xsk_tx_release(struct xsk_buff_pool *pool)
  129. {
  130. }
  131. static inline struct xsk_buff_pool *
  132. xsk_get_pool_from_qid(struct net_device *dev, u16 queue_id)
  133. {
  134. return NULL;
  135. }
  136. static inline void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool)
  137. {
  138. }
  139. static inline void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool)
  140. {
  141. }
  142. static inline void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool)
  143. {
  144. }
  145. static inline void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool)
  146. {
  147. }
  148. static inline bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool)
  149. {
  150. return false;
  151. }
  152. static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool)
  153. {
  154. return 0;
  155. }
  156. static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool)
  157. {
  158. return 0;
  159. }
  160. static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)
  161. {
  162. return 0;
  163. }
  164. static inline void xsk_pool_set_rxq_info(struct xsk_buff_pool *pool,
  165. struct xdp_rxq_info *rxq)
  166. {
  167. }
  168. static inline unsigned int xsk_pool_get_napi_id(struct xsk_buff_pool *pool)
  169. {
  170. return 0;
  171. }
  172. static inline void xsk_pool_dma_unmap(struct xsk_buff_pool *pool,
  173. unsigned long attrs)
  174. {
  175. }
  176. static inline int xsk_pool_dma_map(struct xsk_buff_pool *pool,
  177. struct device *dev, unsigned long attrs)
  178. {
  179. return 0;
  180. }
  181. static inline dma_addr_t xsk_buff_xdp_get_dma(struct xdp_buff *xdp)
  182. {
  183. return 0;
  184. }
  185. static inline dma_addr_t xsk_buff_xdp_get_frame_dma(struct xdp_buff *xdp)
  186. {
  187. return 0;
  188. }
  189. static inline struct xdp_buff *xsk_buff_alloc(struct xsk_buff_pool *pool)
  190. {
  191. return NULL;
  192. }
  193. static inline u32 xsk_buff_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
  194. {
  195. return 0;
  196. }
  197. static inline bool xsk_buff_can_alloc(struct xsk_buff_pool *pool, u32 count)
  198. {
  199. return false;
  200. }
  201. static inline void xsk_buff_free(struct xdp_buff *xdp)
  202. {
  203. }
  204. static inline void xsk_buff_discard(struct xdp_buff *xdp)
  205. {
  206. }
  207. static inline void xsk_buff_set_size(struct xdp_buff *xdp, u32 size)
  208. {
  209. }
  210. static inline dma_addr_t xsk_buff_raw_get_dma(struct xsk_buff_pool *pool,
  211. u64 addr)
  212. {
  213. return 0;
  214. }
  215. static inline void *xsk_buff_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
  216. {
  217. return NULL;
  218. }
  219. static inline void xsk_buff_dma_sync_for_cpu(struct xdp_buff *xdp, struct xsk_buff_pool *pool)
  220. {
  221. }
  222. static inline void xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool *pool,
  223. dma_addr_t dma,
  224. size_t size)
  225. {
  226. }
  227. #endif /* CONFIG_XDP_SOCKETS */
  228. #endif /* _LINUX_XDP_SOCK_DRV_H */