xdp_sock.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 (1 << 1)
  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. /* Protects multiple processes in the control path */
  77. struct mutex mutex;
  78. struct xsk_queue *fq_tmp; /* Only as tmp storage before bind */
  79. struct xsk_queue *cq_tmp; /* Only as tmp storage before bind */
  80. };
  81. /*
  82. * AF_XDP TX metadata hooks for network devices.
  83. * The following hooks can be defined; unless noted otherwise, they are
  84. * optional and can be filled with a null pointer.
  85. *
  86. * void (*tmo_request_timestamp)(void *priv)
  87. * Called when AF_XDP frame requested egress timestamp.
  88. *
  89. * u64 (*tmo_fill_timestamp)(void *priv)
  90. * Called when AF_XDP frame, that had requested egress timestamp,
  91. * received a completion. The hook needs to return the actual HW timestamp.
  92. *
  93. * void (*tmo_request_checksum)(u16 csum_start, u16 csum_offset, void *priv)
  94. * Called when AF_XDP frame requested HW checksum offload. csum_start
  95. * indicates position where checksumming should start.
  96. * csum_offset indicates position where checksum should be stored.
  97. *
  98. */
  99. struct xsk_tx_metadata_ops {
  100. void (*tmo_request_timestamp)(void *priv);
  101. u64 (*tmo_fill_timestamp)(void *priv);
  102. void (*tmo_request_checksum)(u16 csum_start, u16 csum_offset, void *priv);
  103. };
  104. #ifdef CONFIG_XDP_SOCKETS
  105. int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp);
  106. int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp);
  107. void __xsk_map_flush(struct list_head *flush_list);
  108. /**
  109. * xsk_tx_metadata_to_compl - Save enough relevant metadata information
  110. * to perform tx completion in the future.
  111. * @meta: pointer to AF_XDP metadata area
  112. * @compl: pointer to output struct xsk_tx_metadata_to_compl
  113. *
  114. * This function should be called by the networking device when
  115. * it prepares AF_XDP egress packet. The value of @compl should be stored
  116. * and passed to xsk_tx_metadata_complete upon TX completion.
  117. */
  118. static inline void xsk_tx_metadata_to_compl(struct xsk_tx_metadata *meta,
  119. struct xsk_tx_metadata_compl *compl)
  120. {
  121. if (!meta)
  122. return;
  123. if (meta->flags & XDP_TXMD_FLAGS_TIMESTAMP)
  124. compl->tx_timestamp = &meta->completion.tx_timestamp;
  125. else
  126. compl->tx_timestamp = NULL;
  127. }
  128. /**
  129. * xsk_tx_metadata_request - Evaluate AF_XDP TX metadata at submission
  130. * and call appropriate xsk_tx_metadata_ops operation.
  131. * @meta: pointer to AF_XDP metadata area
  132. * @ops: pointer to struct xsk_tx_metadata_ops
  133. * @priv: pointer to driver-private aread
  134. *
  135. * This function should be called by the networking device when
  136. * it prepares AF_XDP egress packet.
  137. */
  138. static inline void xsk_tx_metadata_request(const struct xsk_tx_metadata *meta,
  139. const struct xsk_tx_metadata_ops *ops,
  140. void *priv)
  141. {
  142. if (!meta)
  143. return;
  144. if (ops->tmo_request_timestamp)
  145. if (meta->flags & XDP_TXMD_FLAGS_TIMESTAMP)
  146. ops->tmo_request_timestamp(priv);
  147. if (ops->tmo_request_checksum)
  148. if (meta->flags & XDP_TXMD_FLAGS_CHECKSUM)
  149. ops->tmo_request_checksum(meta->request.csum_start,
  150. meta->request.csum_offset, priv);
  151. }
  152. /**
  153. * xsk_tx_metadata_complete - Evaluate AF_XDP TX metadata at completion
  154. * and call appropriate xsk_tx_metadata_ops operation.
  155. * @compl: pointer to completion metadata produced from xsk_tx_metadata_to_compl
  156. * @ops: pointer to struct xsk_tx_metadata_ops
  157. * @priv: pointer to driver-private aread
  158. *
  159. * This function should be called by the networking device upon
  160. * AF_XDP egress completion.
  161. */
  162. static inline void xsk_tx_metadata_complete(struct xsk_tx_metadata_compl *compl,
  163. const struct xsk_tx_metadata_ops *ops,
  164. void *priv)
  165. {
  166. if (!compl)
  167. return;
  168. if (!compl->tx_timestamp)
  169. return;
  170. *compl->tx_timestamp = ops->tmo_fill_timestamp(priv);
  171. }
  172. #else
  173. static inline int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
  174. {
  175. return -ENOTSUPP;
  176. }
  177. static inline int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp)
  178. {
  179. return -EOPNOTSUPP;
  180. }
  181. static inline void __xsk_map_flush(struct list_head *flush_list)
  182. {
  183. }
  184. static inline void xsk_tx_metadata_to_compl(struct xsk_tx_metadata *meta,
  185. struct xsk_tx_metadata_compl *compl)
  186. {
  187. }
  188. static inline void xsk_tx_metadata_request(struct xsk_tx_metadata *meta,
  189. const struct xsk_tx_metadata_ops *ops,
  190. void *priv)
  191. {
  192. }
  193. static inline void xsk_tx_metadata_complete(struct xsk_tx_metadata_compl *compl,
  194. const struct xsk_tx_metadata_ops *ops,
  195. void *priv)
  196. {
  197. }
  198. #endif /* CONFIG_XDP_SOCKETS */
  199. #endif /* _LINUX_XDP_SOCK_H */