xdp.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* include/net/xdp.h
  2. *
  3. * Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc.
  4. * Released under terms in GPL version 2. See COPYING.
  5. */
  6. #ifndef __LINUX_NET_XDP_H__
  7. #define __LINUX_NET_XDP_H__
  8. /**
  9. * DOC: XDP RX-queue information
  10. *
  11. * The XDP RX-queue info (xdp_rxq_info) is associated with the driver
  12. * level RX-ring queues. It is information that is specific to how
  13. * the driver have configured a given RX-ring queue.
  14. *
  15. * Each xdp_buff frame received in the driver carry a (pointer)
  16. * reference to this xdp_rxq_info structure. This provides the XDP
  17. * data-path read-access to RX-info for both kernel and bpf-side
  18. * (limited subset).
  19. *
  20. * For now, direct access is only safe while running in NAPI/softirq
  21. * context. Contents is read-mostly and must not be updated during
  22. * driver NAPI/softirq poll.
  23. *
  24. * The driver usage API is a register and unregister API.
  25. *
  26. * The struct is not directly tied to the XDP prog. A new XDP prog
  27. * can be attached as long as it doesn't change the underlying
  28. * RX-ring. If the RX-ring does change significantly, the NIC driver
  29. * naturally need to stop the RX-ring before purging and reallocating
  30. * memory. In that process the driver MUST call unregistor (which
  31. * also apply for driver shutdown and unload). The register API is
  32. * also mandatory during RX-ring setup.
  33. */
  34. enum xdp_mem_type {
  35. MEM_TYPE_PAGE_SHARED = 0, /* Split-page refcnt based model */
  36. MEM_TYPE_PAGE_ORDER0, /* Orig XDP full page model */
  37. MEM_TYPE_PAGE_POOL,
  38. MEM_TYPE_ZERO_COPY,
  39. MEM_TYPE_MAX,
  40. };
  41. /* XDP flags for ndo_xdp_xmit */
  42. #define XDP_XMIT_FLUSH (1U << 0) /* doorbell signal consumer */
  43. #define XDP_XMIT_FLAGS_MASK XDP_XMIT_FLUSH
  44. struct xdp_mem_info {
  45. u32 type; /* enum xdp_mem_type, but known size type */
  46. u32 id;
  47. };
  48. struct page_pool;
  49. struct zero_copy_allocator {
  50. void (*free)(struct zero_copy_allocator *zca, unsigned long handle);
  51. };
  52. struct xdp_rxq_info {
  53. struct net_device *dev;
  54. u32 queue_index;
  55. u32 reg_state;
  56. struct xdp_mem_info mem;
  57. } ____cacheline_aligned; /* perf critical, avoid false-sharing */
  58. struct xdp_buff {
  59. void *data;
  60. void *data_end;
  61. void *data_meta;
  62. void *data_hard_start;
  63. unsigned long handle;
  64. struct xdp_rxq_info *rxq;
  65. };
  66. struct xdp_frame {
  67. void *data;
  68. u16 len;
  69. u16 headroom;
  70. u16 metasize;
  71. /* Lifetime of xdp_rxq_info is limited to NAPI/enqueue time,
  72. * while mem info is valid on remote CPU.
  73. */
  74. struct xdp_mem_info mem;
  75. struct net_device *dev_rx; /* used by cpumap */
  76. };
  77. /* Clear kernel pointers in xdp_frame */
  78. static inline void xdp_scrub_frame(struct xdp_frame *frame)
  79. {
  80. frame->data = NULL;
  81. frame->dev_rx = NULL;
  82. }
  83. /* Convert xdp_buff to xdp_frame */
  84. static inline
  85. struct xdp_frame *convert_to_xdp_frame(struct xdp_buff *xdp)
  86. {
  87. struct xdp_frame *xdp_frame;
  88. int metasize;
  89. int headroom;
  90. /* TODO: implement clone, copy, use "native" MEM_TYPE */
  91. if (xdp->rxq->mem.type == MEM_TYPE_ZERO_COPY)
  92. return NULL;
  93. /* Assure headroom is available for storing info */
  94. headroom = xdp->data - xdp->data_hard_start;
  95. metasize = xdp->data - xdp->data_meta;
  96. metasize = metasize > 0 ? metasize : 0;
  97. if (unlikely((headroom - metasize) < sizeof(*xdp_frame)))
  98. return NULL;
  99. /* Store info in top of packet */
  100. xdp_frame = xdp->data_hard_start;
  101. xdp_frame->data = xdp->data;
  102. xdp_frame->len = xdp->data_end - xdp->data;
  103. xdp_frame->headroom = headroom - sizeof(*xdp_frame);
  104. xdp_frame->metasize = metasize;
  105. /* rxq only valid until napi_schedule ends, convert to xdp_mem_info */
  106. xdp_frame->mem = xdp->rxq->mem;
  107. return xdp_frame;
  108. }
  109. void xdp_return_frame(struct xdp_frame *xdpf);
  110. void xdp_return_frame_rx_napi(struct xdp_frame *xdpf);
  111. void xdp_return_buff(struct xdp_buff *xdp);
  112. int xdp_rxq_info_reg(struct xdp_rxq_info *xdp_rxq,
  113. struct net_device *dev, u32 queue_index);
  114. void xdp_rxq_info_unreg(struct xdp_rxq_info *xdp_rxq);
  115. void xdp_rxq_info_unused(struct xdp_rxq_info *xdp_rxq);
  116. bool xdp_rxq_info_is_reg(struct xdp_rxq_info *xdp_rxq);
  117. int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq,
  118. enum xdp_mem_type type, void *allocator);
  119. /* Drivers not supporting XDP metadata can use this helper, which
  120. * rejects any room expansion for metadata as a result.
  121. */
  122. static __always_inline void
  123. xdp_set_data_meta_invalid(struct xdp_buff *xdp)
  124. {
  125. xdp->data_meta = xdp->data + 1;
  126. }
  127. static __always_inline bool
  128. xdp_data_meta_unsupported(const struct xdp_buff *xdp)
  129. {
  130. return unlikely(xdp->data_meta > xdp->data);
  131. }
  132. struct xdp_attachment_info {
  133. struct bpf_prog *prog;
  134. u32 flags;
  135. };
  136. struct netdev_bpf;
  137. int xdp_attachment_query(struct xdp_attachment_info *info,
  138. struct netdev_bpf *bpf);
  139. bool xdp_attachment_flags_ok(struct xdp_attachment_info *info,
  140. struct netdev_bpf *bpf);
  141. void xdp_attachment_setup(struct xdp_attachment_info *info,
  142. struct netdev_bpf *bpf);
  143. #endif /* __LINUX_NET_XDP_H__ */