xdp_sock.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/workqueue.h>
  8. #include <linux/if_xdp.h>
  9. #include <linux/mutex.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/mm.h>
  12. #include <net/sock.h>
  13. struct net_device;
  14. struct xsk_queue;
  15. struct xdp_umem_props {
  16. u64 chunk_mask;
  17. u64 size;
  18. };
  19. struct xdp_umem_page {
  20. void *addr;
  21. dma_addr_t dma;
  22. };
  23. struct xdp_umem {
  24. struct xsk_queue *fq;
  25. struct xsk_queue *cq;
  26. struct xdp_umem_page *pages;
  27. struct xdp_umem_props props;
  28. u32 headroom;
  29. u32 chunk_size_nohr;
  30. struct user_struct *user;
  31. unsigned long address;
  32. refcount_t users;
  33. struct work_struct work;
  34. struct page **pgs;
  35. u32 npgs;
  36. struct net_device *dev;
  37. u16 queue_id;
  38. bool zc;
  39. spinlock_t xsk_list_lock;
  40. struct list_head xsk_list;
  41. };
  42. struct xdp_sock {
  43. /* struct sock must be the first member of struct xdp_sock */
  44. struct sock sk;
  45. struct xsk_queue *rx;
  46. struct net_device *dev;
  47. struct xdp_umem *umem;
  48. struct list_head flush_node;
  49. u16 queue_id;
  50. struct xsk_queue *tx ____cacheline_aligned_in_smp;
  51. struct list_head list;
  52. bool zc;
  53. /* Protects multiple processes in the control path */
  54. struct mutex mutex;
  55. /* Mutual exclusion of NAPI TX thread and sendmsg error paths
  56. * in the SKB destructor callback.
  57. */
  58. spinlock_t tx_completion_lock;
  59. u64 rx_dropped;
  60. };
  61. struct xdp_buff;
  62. #ifdef CONFIG_XDP_SOCKETS
  63. int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp);
  64. int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp);
  65. void xsk_flush(struct xdp_sock *xs);
  66. bool xsk_is_setup_for_bpf_map(struct xdp_sock *xs);
  67. /* Used from netdev driver */
  68. u64 *xsk_umem_peek_addr(struct xdp_umem *umem, u64 *addr);
  69. void xsk_umem_discard_addr(struct xdp_umem *umem);
  70. void xsk_umem_complete_tx(struct xdp_umem *umem, u32 nb_entries);
  71. bool xsk_umem_consume_tx(struct xdp_umem *umem, dma_addr_t *dma, u32 *len);
  72. void xsk_umem_consume_tx_done(struct xdp_umem *umem);
  73. #else
  74. static inline int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
  75. {
  76. return -ENOTSUPP;
  77. }
  78. static inline int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
  79. {
  80. return -ENOTSUPP;
  81. }
  82. static inline void xsk_flush(struct xdp_sock *xs)
  83. {
  84. }
  85. static inline bool xsk_is_setup_for_bpf_map(struct xdp_sock *xs)
  86. {
  87. return false;
  88. }
  89. #endif /* CONFIG_XDP_SOCKETS */
  90. #endif /* _LINUX_XDP_SOCK_H */