map_xskmap.rst 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. .. SPDX-License-Identifier: GPL-2.0-only
  2. .. Copyright (C) 2022 Red Hat, Inc.
  3. ===================
  4. BPF_MAP_TYPE_XSKMAP
  5. ===================
  6. .. note::
  7. - ``BPF_MAP_TYPE_XSKMAP`` was introduced in kernel version 4.18
  8. The ``BPF_MAP_TYPE_XSKMAP`` is used as a backend map for XDP BPF helper
  9. call ``bpf_redirect_map()`` and ``XDP_REDIRECT`` action, like 'devmap' and 'cpumap'.
  10. This map type redirects raw XDP frames to `AF_XDP`_ sockets (XSKs), a new type of
  11. address family in the kernel that allows redirection of frames from a driver to
  12. user space without having to traverse the full network stack. An AF_XDP socket
  13. binds to a single netdev queue. A mapping of XSKs to queues is shown below:
  14. .. code-block:: none
  15. +---------------------------------------------------+
  16. | xsk A | xsk B | xsk C |<---+ User space
  17. =========================================================|==========
  18. | Queue 0 | Queue 1 | Queue 2 | | Kernel
  19. +---------------------------------------------------+ |
  20. | Netdev eth0 | |
  21. +---------------------------------------------------+ |
  22. | +=============+ | |
  23. | | key | xsk | | |
  24. | +---------+ +=============+ | |
  25. | | | | 0 | xsk A | | |
  26. | | | +-------------+ | |
  27. | | | | 1 | xsk B | | |
  28. | | BPF |-- redirect -->+-------------+-------------+
  29. | | prog | | 2 | xsk C | |
  30. | | | +-------------+ |
  31. | | | |
  32. | | | |
  33. | +---------+ |
  34. | |
  35. +---------------------------------------------------+
  36. .. note::
  37. An AF_XDP socket that is bound to a certain <netdev/queue_id> will *only*
  38. accept XDP frames from that <netdev/queue_id>. If an XDP program tries to redirect
  39. from a <netdev/queue_id> other than what the socket is bound to, the frame will
  40. not be received on the socket.
  41. Typically an XSKMAP is created per netdev. This map contains an array of XSK File
  42. Descriptors (FDs). The number of array elements is typically set or adjusted using
  43. the ``max_entries`` map parameter. For AF_XDP ``max_entries`` is equal to the number
  44. of queues supported by the netdev.
  45. .. note::
  46. Both the map key and map value size must be 4 bytes.
  47. Usage
  48. =====
  49. Kernel BPF
  50. ----------
  51. bpf_redirect_map()
  52. ^^^^^^^^^^^^^^^^^^
  53. .. code-block:: c
  54. long bpf_redirect_map(struct bpf_map *map, u32 key, u64 flags)
  55. Redirect the packet to the endpoint referenced by ``map`` at index ``key``.
  56. For ``BPF_MAP_TYPE_XSKMAP`` this map contains references to XSK FDs
  57. for sockets attached to a netdev's queues.
  58. .. note::
  59. If the map is empty at an index, the packet is dropped. This means that it is
  60. necessary to have an XDP program loaded with at least one XSK in the
  61. XSKMAP to be able to get any traffic to user space through the socket.
  62. bpf_map_lookup_elem()
  63. ^^^^^^^^^^^^^^^^^^^^^
  64. .. code-block:: c
  65. void *bpf_map_lookup_elem(struct bpf_map *map, const void *key)
  66. XSK entry references of type ``struct xdp_sock *`` can be retrieved using the
  67. ``bpf_map_lookup_elem()`` helper.
  68. User space
  69. ----------
  70. .. note::
  71. XSK entries can only be updated/deleted from user space and not from
  72. a BPF program. Trying to call these functions from a kernel BPF program will
  73. result in the program failing to load and a verifier warning.
  74. bpf_map_update_elem()
  75. ^^^^^^^^^^^^^^^^^^^^^
  76. .. code-block:: c
  77. int bpf_map_update_elem(int fd, const void *key, const void *value, __u64 flags)
  78. XSK entries can be added or updated using the ``bpf_map_update_elem()``
  79. helper. The ``key`` parameter is equal to the queue_id of the queue the XSK
  80. is attaching to. And the ``value`` parameter is the FD value of that socket.
  81. Under the hood, the XSKMAP update function uses the XSK FD value to retrieve the
  82. associated ``struct xdp_sock`` instance.
  83. The flags argument can be one of the following:
  84. - BPF_ANY: Create a new element or update an existing element.
  85. - BPF_NOEXIST: Create a new element only if it did not exist.
  86. - BPF_EXIST: Update an existing element.
  87. bpf_map_lookup_elem()
  88. ^^^^^^^^^^^^^^^^^^^^^
  89. .. code-block:: c
  90. int bpf_map_lookup_elem(int fd, const void *key, void *value)
  91. Returns ``struct xdp_sock *`` or negative error in case of failure.
  92. bpf_map_delete_elem()
  93. ^^^^^^^^^^^^^^^^^^^^^
  94. .. code-block:: c
  95. int bpf_map_delete_elem(int fd, const void *key)
  96. XSK entries can be deleted using the ``bpf_map_delete_elem()``
  97. helper. This helper will return 0 on success, or negative error in case of
  98. failure.
  99. .. note::
  100. When `libxdp`_ deletes an XSK it also removes the associated socket
  101. entry from the XSKMAP.
  102. Examples
  103. ========
  104. Kernel
  105. ------
  106. The following code snippet shows how to declare a ``BPF_MAP_TYPE_XSKMAP`` called
  107. ``xsks_map`` and how to redirect packets to an XSK.
  108. .. code-block:: c
  109. struct {
  110. __uint(type, BPF_MAP_TYPE_XSKMAP);
  111. __type(key, __u32);
  112. __type(value, __u32);
  113. __uint(max_entries, 64);
  114. } xsks_map SEC(".maps");
  115. SEC("xdp")
  116. int xsk_redir_prog(struct xdp_md *ctx)
  117. {
  118. __u32 index = ctx->rx_queue_index;
  119. if (bpf_map_lookup_elem(&xsks_map, &index))
  120. return bpf_redirect_map(&xsks_map, index, 0);
  121. return XDP_PASS;
  122. }
  123. User space
  124. ----------
  125. The following code snippet shows how to update an XSKMAP with an XSK entry.
  126. .. code-block:: c
  127. int update_xsks_map(struct bpf_map *xsks_map, int queue_id, int xsk_fd)
  128. {
  129. int ret;
  130. ret = bpf_map_update_elem(bpf_map__fd(xsks_map), &queue_id, &xsk_fd, 0);
  131. if (ret < 0)
  132. fprintf(stderr, "Failed to update xsks_map: %s\n", strerror(errno));
  133. return ret;
  134. }
  135. For an example on how create AF_XDP sockets, please see the AF_XDP-example and
  136. AF_XDP-forwarding programs in the `bpf-examples`_ directory in the `libxdp`_ repository.
  137. For a detailed explanation of the AF_XDP interface please see:
  138. - `libxdp-readme`_.
  139. - `AF_XDP`_ kernel documentation.
  140. .. note::
  141. The most comprehensive resource for using XSKMAPs and AF_XDP is `libxdp`_.
  142. .. _libxdp: https://github.com/xdp-project/xdp-tools/tree/master/lib/libxdp
  143. .. _AF_XDP: https://www.kernel.org/doc/html/latest/networking/af_xdp.html
  144. .. _bpf-examples: https://github.com/xdp-project/bpf-examples
  145. .. _libxdp-readme: https://github.com/xdp-project/xdp-tools/tree/master/lib/libxdp#using-af_xdp-sockets