af_xdp.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ======
  3. AF_XDP
  4. ======
  5. Overview
  6. ========
  7. AF_XDP is an address family that is optimized for high performance
  8. packet processing.
  9. This document assumes that the reader is familiar with BPF and XDP. If
  10. not, the Cilium project has an excellent reference guide at
  11. http://cilium.readthedocs.io/en/latest/bpf/.
  12. Using the XDP_REDIRECT action from an XDP program, the program can
  13. redirect ingress frames to other XDP enabled netdevs, using the
  14. bpf_redirect_map() function. AF_XDP sockets enable the possibility for
  15. XDP programs to redirect frames to a memory buffer in a user-space
  16. application.
  17. An AF_XDP socket (XSK) is created with the normal socket()
  18. syscall. Associated with each XSK are two rings: the RX ring and the
  19. TX ring. A socket can receive packets on the RX ring and it can send
  20. packets on the TX ring. These rings are registered and sized with the
  21. setsockopts XDP_RX_RING and XDP_TX_RING, respectively. It is mandatory
  22. to have at least one of these rings for each socket. An RX or TX
  23. descriptor ring points to a data buffer in a memory area called a
  24. UMEM. RX and TX can share the same UMEM so that a packet does not have
  25. to be copied between RX and TX. Moreover, if a packet needs to be kept
  26. for a while due to a possible retransmit, the descriptor that points
  27. to that packet can be changed to point to another and reused right
  28. away. This again avoids copying data.
  29. The UMEM consists of a number of equally sized chunks. A descriptor in
  30. one of the rings references a frame by referencing its addr. The addr
  31. is simply an offset within the entire UMEM region. The user space
  32. allocates memory for this UMEM using whatever means it feels is most
  33. appropriate (malloc, mmap, huge pages, etc). This memory area is then
  34. registered with the kernel using the new setsockopt XDP_UMEM_REG. The
  35. UMEM also has two rings: the FILL ring and the COMPLETION ring. The
  36. fill ring is used by the application to send down addr for the kernel
  37. to fill in with RX packet data. References to these frames will then
  38. appear in the RX ring once each packet has been received. The
  39. completion ring, on the other hand, contains frame addr that the
  40. kernel has transmitted completely and can now be used again by user
  41. space, for either TX or RX. Thus, the frame addrs appearing in the
  42. completion ring are addrs that were previously transmitted using the
  43. TX ring. In summary, the RX and FILL rings are used for the RX path
  44. and the TX and COMPLETION rings are used for the TX path.
  45. The socket is then finally bound with a bind() call to a device and a
  46. specific queue id on that device, and it is not until bind is
  47. completed that traffic starts to flow.
  48. The UMEM can be shared between processes, if desired. If a process
  49. wants to do this, it simply skips the registration of the UMEM and its
  50. corresponding two rings, sets the XDP_SHARED_UMEM flag in the bind
  51. call and submits the XSK of the process it would like to share UMEM
  52. with as well as its own newly created XSK socket. The new process will
  53. then receive frame addr references in its own RX ring that point to
  54. this shared UMEM. Note that since the ring structures are
  55. single-consumer / single-producer (for performance reasons), the new
  56. process has to create its own socket with associated RX and TX rings,
  57. since it cannot share this with the other process. This is also the
  58. reason that there is only one set of FILL and COMPLETION rings per
  59. UMEM. It is the responsibility of a single process to handle the UMEM.
  60. How is then packets distributed from an XDP program to the XSKs? There
  61. is a BPF map called XSKMAP (or BPF_MAP_TYPE_XSKMAP in full). The
  62. user-space application can place an XSK at an arbitrary place in this
  63. map. The XDP program can then redirect a packet to a specific index in
  64. this map and at this point XDP validates that the XSK in that map was
  65. indeed bound to that device and ring number. If not, the packet is
  66. dropped. If the map is empty at that index, the packet is also
  67. dropped. This also means that it is currently mandatory to have an XDP
  68. program loaded (and one XSK in the XSKMAP) to be able to get any
  69. traffic to user space through the XSK.
  70. AF_XDP can operate in two different modes: XDP_SKB and XDP_DRV. If the
  71. driver does not have support for XDP, or XDP_SKB is explicitly chosen
  72. when loading the XDP program, XDP_SKB mode is employed that uses SKBs
  73. together with the generic XDP support and copies out the data to user
  74. space. A fallback mode that works for any network device. On the other
  75. hand, if the driver has support for XDP, it will be used by the AF_XDP
  76. code to provide better performance, but there is still a copy of the
  77. data into user space.
  78. Concepts
  79. ========
  80. In order to use an AF_XDP socket, a number of associated objects need
  81. to be setup.
  82. Jonathan Corbet has also written an excellent article on LWN,
  83. "Accelerating networking with AF_XDP". It can be found at
  84. https://lwn.net/Articles/750845/.
  85. UMEM
  86. ----
  87. UMEM is a region of virtual contiguous memory, divided into
  88. equal-sized frames. An UMEM is associated to a netdev and a specific
  89. queue id of that netdev. It is created and configured (chunk size,
  90. headroom, start address and size) by using the XDP_UMEM_REG setsockopt
  91. system call. A UMEM is bound to a netdev and queue id, via the bind()
  92. system call.
  93. An AF_XDP is socket linked to a single UMEM, but one UMEM can have
  94. multiple AF_XDP sockets. To share an UMEM created via one socket A,
  95. the next socket B can do this by setting the XDP_SHARED_UMEM flag in
  96. struct sockaddr_xdp member sxdp_flags, and passing the file descriptor
  97. of A to struct sockaddr_xdp member sxdp_shared_umem_fd.
  98. The UMEM has two single-producer/single-consumer rings, that are used
  99. to transfer ownership of UMEM frames between the kernel and the
  100. user-space application.
  101. Rings
  102. -----
  103. There are a four different kind of rings: Fill, Completion, RX and
  104. TX. All rings are single-producer/single-consumer, so the user-space
  105. application need explicit synchronization of multiple
  106. processes/threads are reading/writing to them.
  107. The UMEM uses two rings: Fill and Completion. Each socket associated
  108. with the UMEM must have an RX queue, TX queue or both. Say, that there
  109. is a setup with four sockets (all doing TX and RX). Then there will be
  110. one Fill ring, one Completion ring, four TX rings and four RX rings.
  111. The rings are head(producer)/tail(consumer) based rings. A producer
  112. writes the data ring at the index pointed out by struct xdp_ring
  113. producer member, and increasing the producer index. A consumer reads
  114. the data ring at the index pointed out by struct xdp_ring consumer
  115. member, and increasing the consumer index.
  116. The rings are configured and created via the _RING setsockopt system
  117. calls and mmapped to user-space using the appropriate offset to mmap()
  118. (XDP_PGOFF_RX_RING, XDP_PGOFF_TX_RING, XDP_UMEM_PGOFF_FILL_RING and
  119. XDP_UMEM_PGOFF_COMPLETION_RING).
  120. The size of the rings need to be of size power of two.
  121. UMEM Fill Ring
  122. ~~~~~~~~~~~~~~
  123. The Fill ring is used to transfer ownership of UMEM frames from
  124. user-space to kernel-space. The UMEM addrs are passed in the ring. As
  125. an example, if the UMEM is 64k and each chunk is 4k, then the UMEM has
  126. 16 chunks and can pass addrs between 0 and 64k.
  127. Frames passed to the kernel are used for the ingress path (RX rings).
  128. The user application produces UMEM addrs to this ring. Note that the
  129. kernel will mask the incoming addr. E.g. for a chunk size of 2k, the
  130. log2(2048) LSB of the addr will be masked off, meaning that 2048, 2050
  131. and 3000 refers to the same chunk.
  132. UMEM Completetion Ring
  133. ~~~~~~~~~~~~~~~~~~~~~~
  134. The Completion Ring is used transfer ownership of UMEM frames from
  135. kernel-space to user-space. Just like the Fill ring, UMEM indicies are
  136. used.
  137. Frames passed from the kernel to user-space are frames that has been
  138. sent (TX ring) and can be used by user-space again.
  139. The user application consumes UMEM addrs from this ring.
  140. RX Ring
  141. ~~~~~~~
  142. The RX ring is the receiving side of a socket. Each entry in the ring
  143. is a struct xdp_desc descriptor. The descriptor contains UMEM offset
  144. (addr) and the length of the data (len).
  145. If no frames have been passed to kernel via the Fill ring, no
  146. descriptors will (or can) appear on the RX ring.
  147. The user application consumes struct xdp_desc descriptors from this
  148. ring.
  149. TX Ring
  150. ~~~~~~~
  151. The TX ring is used to send frames. The struct xdp_desc descriptor is
  152. filled (index, length and offset) and passed into the ring.
  153. To start the transfer a sendmsg() system call is required. This might
  154. be relaxed in the future.
  155. The user application produces struct xdp_desc descriptors to this
  156. ring.
  157. XSKMAP / BPF_MAP_TYPE_XSKMAP
  158. ----------------------------
  159. On XDP side there is a BPF map type BPF_MAP_TYPE_XSKMAP (XSKMAP) that
  160. is used in conjunction with bpf_redirect_map() to pass the ingress
  161. frame to a socket.
  162. The user application inserts the socket into the map, via the bpf()
  163. system call.
  164. Note that if an XDP program tries to redirect to a socket that does
  165. not match the queue configuration and netdev, the frame will be
  166. dropped. E.g. an AF_XDP socket is bound to netdev eth0 and
  167. queue 17. Only the XDP program executing for eth0 and queue 17 will
  168. successfully pass data to the socket. Please refer to the sample
  169. application (samples/bpf/) in for an example.
  170. Usage
  171. =====
  172. In order to use AF_XDP sockets there are two parts needed. The
  173. user-space application and the XDP program. For a complete setup and
  174. usage example, please refer to the sample application. The user-space
  175. side is xdpsock_user.c and the XDP side xdpsock_kern.c.
  176. Naive ring dequeue and enqueue could look like this::
  177. // struct xdp_rxtx_ring {
  178. // __u32 *producer;
  179. // __u32 *consumer;
  180. // struct xdp_desc *desc;
  181. // };
  182. // struct xdp_umem_ring {
  183. // __u32 *producer;
  184. // __u32 *consumer;
  185. // __u64 *desc;
  186. // };
  187. // typedef struct xdp_rxtx_ring RING;
  188. // typedef struct xdp_umem_ring RING;
  189. // typedef struct xdp_desc RING_TYPE;
  190. // typedef __u64 RING_TYPE;
  191. int dequeue_one(RING *ring, RING_TYPE *item)
  192. {
  193. __u32 entries = *ring->producer - *ring->consumer;
  194. if (entries == 0)
  195. return -1;
  196. // read-barrier!
  197. *item = ring->desc[*ring->consumer & (RING_SIZE - 1)];
  198. (*ring->consumer)++;
  199. return 0;
  200. }
  201. int enqueue_one(RING *ring, const RING_TYPE *item)
  202. {
  203. u32 free_entries = RING_SIZE - (*ring->producer - *ring->consumer);
  204. if (free_entries == 0)
  205. return -1;
  206. ring->desc[*ring->producer & (RING_SIZE - 1)] = *item;
  207. // write-barrier!
  208. (*ring->producer)++;
  209. return 0;
  210. }
  211. For a more optimized version, please refer to the sample application.
  212. Sample application
  213. ==================
  214. There is a xdpsock benchmarking/test application included that
  215. demonstrates how to use AF_XDP sockets with both private and shared
  216. UMEMs. Say that you would like your UDP traffic from port 4242 to end
  217. up in queue 16, that we will enable AF_XDP on. Here, we use ethtool
  218. for this::
  219. ethtool -N p3p2 rx-flow-hash udp4 fn
  220. ethtool -N p3p2 flow-type udp4 src-port 4242 dst-port 4242 \
  221. action 16
  222. Running the rxdrop benchmark in XDP_DRV mode can then be done
  223. using::
  224. samples/bpf/xdpsock -i p3p2 -q 16 -r -N
  225. For XDP_SKB mode, use the switch "-S" instead of "-N" and all options
  226. can be displayed with "-h", as usual.
  227. Credits
  228. =======
  229. - Björn Töpel (AF_XDP core)
  230. - Magnus Karlsson (AF_XDP core)
  231. - Alexander Duyck
  232. - Alexei Starovoitov
  233. - Daniel Borkmann
  234. - Jesper Dangaard Brouer
  235. - John Fastabend
  236. - Jonathan Corbet (LWN coverage)
  237. - Michael S. Tsirkin
  238. - Qi Z Zhang
  239. - Willem de Bruijn