xdp_sock.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* AF_XDP internal functions
  3. * Copyright(c) 2018 Intel Corporation.
  4. */
  5. #ifndef _LINUX_XDP_SOCK_H
  6. #define _LINUX_XDP_SOCK_H
  7. #include <linux/bpf.h>
  8. #include <linux/workqueue.h>
  9. #include <linux/if_xdp.h>
  10. #include <linux/mutex.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/mm.h>
  13. #include <net/sock.h>
  14. #define XDP_UMEM_SG_FLAG BIT(3)
  15. struct net_device;
  16. struct xsk_queue;
  17. struct xdp_buff;
  18. struct xdp_umem {
  19. void *addrs;
  20. u64 size;
  21. u32 headroom;
  22. u32 chunk_size;
  23. u32 chunks;
  24. u32 npgs;
  25. struct user_struct *user;
  26. refcount_t users;
  27. u8 flags;
  28. u8 tx_metadata_len;
  29. bool zc;
  30. struct page **pgs;
  31. int id;
  32. struct list_head xsk_dma_list;
  33. struct work_struct work;
  34. };
  35. struct xsk_map {
  36. struct bpf_map map;
  37. spinlock_t lock; /* Synchronize map updates */
  38. atomic_t count;
  39. struct xdp_sock __rcu *xsk_map[];
  40. };
  41. struct xdp_sock {
  42. /* struct sock must be the first member of struct xdp_sock */
  43. struct sock sk;
  44. struct xsk_queue *rx ____cacheline_aligned_in_smp;
  45. struct net_device *dev;
  46. struct xdp_umem *umem;
  47. struct list_head flush_node;
  48. struct xsk_buff_pool *pool;
  49. u16 queue_id;
  50. bool zc;
  51. bool sg;
  52. enum {
  53. XSK_READY = 0,
  54. XSK_BOUND,
  55. XSK_UNBOUND,
  56. } state;
  57. struct xsk_queue *tx ____cacheline_aligned_in_smp;
  58. struct list_head tx_list;
  59. /* record the number of tx descriptors sent by this xsk and
  60. * when it exceeds MAX_PER_SOCKET_BUDGET, an opportunity needs
  61. * to be given to other xsks for sending tx descriptors, thereby
  62. * preventing other XSKs from being starved.
  63. */
  64. u32 tx_budget_spent;
  65. /* Statistics */
  66. u64 rx_dropped;
  67. u64 rx_queue_full;
  68. /* When __xsk_generic_xmit() must return before it sees the EOP descriptor for the current
  69. * packet, the partially built skb is saved here so that packet building can resume in next
  70. * call of __xsk_generic_xmit().
  71. */
  72. struct sk_buff *skb;
  73. struct list_head map_list;
  74. /* Protects map_list */
  75. spinlock_t map_list_lock;
  76. u32 max_tx_budget;
  77. /* Protects multiple processes in the control path */
  78. struct mutex mutex;
  79. struct xsk_queue *fq_tmp; /* Only as tmp storage before bind */
  80. struct xsk_queue *cq_tmp; /* Only as tmp storage before bind */
  81. };
  82. /*
  83. * AF_XDP TX metadata hooks for network devices.
  84. * The following hooks can be defined; unless noted otherwise, they are
  85. * optional and can be filled with a null pointer.
  86. *
  87. * void (*tmo_request_timestamp)(void *priv)
  88. * Called when AF_XDP frame requested egress timestamp.
  89. *
  90. * u64 (*tmo_fill_timestamp)(void *priv)
  91. * Called when AF_XDP frame, that had requested egress timestamp,
  92. * received a completion. The hook needs to return the actual HW timestamp.
  93. *
  94. * void (*tmo_request_checksum)(u16 csum_start, u16 csum_offset, void *priv)
  95. * Called when AF_XDP frame requested HW checksum offload. csum_start
  96. * indicates position where checksumming should start.
  97. * csum_offset indicates position where checksum should be stored.
  98. *
  99. * void (*tmo_request_launch_time)(u64 launch_time, void *priv)
  100. * Called when AF_XDP frame requested launch time HW offload support.
  101. * launch_time indicates the PTP time at which the device can schedule the
  102. * packet for transmission.
  103. */
  104. struct xsk_tx_metadata_ops {
  105. void (*tmo_request_timestamp)(void *priv);
  106. u64 (*tmo_fill_timestamp)(void *priv);
  107. void (*tmo_request_checksum)(u16 csum_start, u16 csum_offset, void *priv);
  108. void (*tmo_request_launch_time)(u64 launch_time, void *priv);
  109. };
  110. #ifdef CONFIG_XDP_SOCKETS
  111. int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp);
  112. int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp);
  113. void __xsk_map_flush(struct list_head *flush_list);
  114. INDIRECT_CALLABLE_DECLARE(void xsk_destruct_skb(struct sk_buff *));
  115. /**
  116. * xsk_tx_metadata_to_compl - Save enough relevant metadata information
  117. * to perform tx completion in the future.
  118. * @meta: pointer to AF_XDP metadata area
  119. * @compl: pointer to output struct xsk_tx_metadata_to_compl
  120. *
  121. * This function should be called by the networking device when
  122. * it prepares AF_XDP egress packet. The value of @compl should be stored
  123. * and passed to xsk_tx_metadata_complete upon TX completion.
  124. */
  125. static inline void xsk_tx_metadata_to_compl(struct xsk_tx_metadata *meta,
  126. struct xsk_tx_metadata_compl *compl)
  127. {
  128. if (!meta)
  129. return;
  130. if (meta->flags & XDP_TXMD_FLAGS_TIMESTAMP)
  131. compl->tx_timestamp = &meta->completion.tx_timestamp;
  132. else
  133. compl->tx_timestamp = NULL;
  134. }
  135. /**
  136. * xsk_tx_metadata_request - Evaluate AF_XDP TX metadata at submission
  137. * and call appropriate xsk_tx_metadata_ops operation.
  138. * @meta: pointer to AF_XDP metadata area
  139. * @ops: pointer to struct xsk_tx_metadata_ops
  140. * @priv: pointer to driver-private aread
  141. *
  142. * This function should be called by the networking device when
  143. * it prepares AF_XDP egress packet.
  144. */
  145. static inline void xsk_tx_metadata_request(const struct xsk_tx_metadata *meta,
  146. const struct xsk_tx_metadata_ops *ops,
  147. void *priv)
  148. {
  149. if (!meta)
  150. return;
  151. if (ops->tmo_request_launch_time)
  152. if (meta->flags & XDP_TXMD_FLAGS_LAUNCH_TIME)
  153. ops->tmo_request_launch_time(meta->request.launch_time,
  154. priv);
  155. if (ops->tmo_request_timestamp)
  156. if (meta->flags & XDP_TXMD_FLAGS_TIMESTAMP)
  157. ops->tmo_request_timestamp(priv);
  158. if (ops->tmo_request_checksum)
  159. if (meta->flags & XDP_TXMD_FLAGS_CHECKSUM)
  160. ops->tmo_request_checksum(meta->request.csum_start,
  161. meta->request.csum_offset, priv);
  162. }
  163. /**
  164. * xsk_tx_metadata_complete - Evaluate AF_XDP TX metadata at completion
  165. * and call appropriate xsk_tx_metadata_ops operation.
  166. * @compl: pointer to completion metadata produced from xsk_tx_metadata_to_compl
  167. * @ops: pointer to struct xsk_tx_metadata_ops
  168. * @priv: pointer to driver-private aread
  169. *
  170. * This function should be called by the networking device upon
  171. * AF_XDP egress completion.
  172. */
  173. static inline void xsk_tx_metadata_complete(struct xsk_tx_metadata_compl *compl,
  174. const struct xsk_tx_metadata_ops *ops,
  175. void *priv)
  176. {
  177. if (!compl)
  178. return;
  179. if (!compl->tx_timestamp)
  180. return;
  181. *compl->tx_timestamp = ops->tmo_fill_timestamp(priv);
  182. }
  183. #else
  184. static inline int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
  185. {
  186. return -ENOTSUPP;
  187. }
  188. static inline int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp)
  189. {
  190. return -EOPNOTSUPP;
  191. }
  192. static inline void __xsk_map_flush(struct list_head *flush_list)
  193. {
  194. }
  195. #ifdef CONFIG_MITIGATION_RETPOLINE
  196. static inline void xsk_destruct_skb(struct sk_buff *skb)
  197. {
  198. }
  199. #endif
  200. static inline void xsk_tx_metadata_to_compl(struct xsk_tx_metadata *meta,
  201. struct xsk_tx_metadata_compl *compl)
  202. {
  203. }
  204. static inline void xsk_tx_metadata_request(struct xsk_tx_metadata *meta,
  205. const struct xsk_tx_metadata_ops *ops,
  206. void *priv)
  207. {
  208. }
  209. static inline void xsk_tx_metadata_complete(struct xsk_tx_metadata_compl *compl,
  210. const struct xsk_tx_metadata_ops *ops,
  211. void *priv)
  212. {
  213. }
  214. #endif /* CONFIG_XDP_SOCKETS */
  215. #endif /* _LINUX_XDP_SOCK_H */