map_devmap.rst 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. .. SPDX-License-Identifier: GPL-2.0-only
  2. .. Copyright (C) 2022 Red Hat, Inc.
  3. =================================================
  4. BPF_MAP_TYPE_DEVMAP and BPF_MAP_TYPE_DEVMAP_HASH
  5. =================================================
  6. .. note::
  7. - ``BPF_MAP_TYPE_DEVMAP`` was introduced in kernel version 4.14
  8. - ``BPF_MAP_TYPE_DEVMAP_HASH`` was introduced in kernel version 5.4
  9. ``BPF_MAP_TYPE_DEVMAP`` and ``BPF_MAP_TYPE_DEVMAP_HASH`` are BPF maps primarily
  10. used as backend maps for the XDP BPF helper call ``bpf_redirect_map()``.
  11. ``BPF_MAP_TYPE_DEVMAP`` is backed by an array that uses the key as
  12. the index to lookup a reference to a net device. While ``BPF_MAP_TYPE_DEVMAP_HASH``
  13. is backed by a hash table that uses a key to lookup a reference to a net device.
  14. The user provides either <``key``/ ``ifindex``> or <``key``/ ``struct bpf_devmap_val``>
  15. pairs to update the maps with new net devices.
  16. .. note::
  17. - The key to a hash map doesn't have to be an ``ifindex``.
  18. - While ``BPF_MAP_TYPE_DEVMAP_HASH`` allows for densely packing the net devices
  19. it comes at the cost of a hash of the key when performing a look up.
  20. The setup and packet enqueue/send code is shared between the two types of
  21. devmap; only the lookup and insertion is different.
  22. Usage
  23. =====
  24. Kernel BPF
  25. ----------
  26. bpf_redirect_map()
  27. ^^^^^^^^^^^^^^^^^^
  28. .. code-block:: c
  29. long bpf_redirect_map(struct bpf_map *map, u32 key, u64 flags)
  30. Redirect the packet to the endpoint referenced by ``map`` at index ``key``.
  31. For ``BPF_MAP_TYPE_DEVMAP`` and ``BPF_MAP_TYPE_DEVMAP_HASH`` this map contains
  32. references to net devices (for forwarding packets through other ports).
  33. The lower two bits of *flags* are used as the return code if the map lookup
  34. fails. This is so that the return value can be one of the XDP program return
  35. codes up to ``XDP_TX``, as chosen by the caller. The higher bits of ``flags``
  36. can be set to ``BPF_F_BROADCAST`` or ``BPF_F_EXCLUDE_INGRESS`` as defined
  37. below.
  38. With ``BPF_F_BROADCAST`` the packet will be broadcast to all the interfaces
  39. in the map, with ``BPF_F_EXCLUDE_INGRESS`` the ingress interface will be excluded
  40. from the broadcast.
  41. .. note::
  42. - The key is ignored if BPF_F_BROADCAST is set.
  43. - The broadcast feature can also be used to implement multicast forwarding:
  44. simply create multiple DEVMAPs, each one corresponding to a single multicast group.
  45. This helper will return ``XDP_REDIRECT`` on success, or the value of the two
  46. lower bits of the ``flags`` argument if the map lookup fails.
  47. More information about redirection can be found :doc:`redirect`
  48. bpf_map_lookup_elem()
  49. ^^^^^^^^^^^^^^^^^^^^^
  50. .. code-block:: c
  51. void *bpf_map_lookup_elem(struct bpf_map *map, const void *key)
  52. Net device entries can be retrieved using the ``bpf_map_lookup_elem()``
  53. helper.
  54. User space
  55. ----------
  56. .. note::
  57. DEVMAP entries can only be updated/deleted from user space and not
  58. from an eBPF program. Trying to call these functions from a kernel eBPF
  59. program will result in the program failing to load and a verifier warning.
  60. bpf_map_update_elem()
  61. ^^^^^^^^^^^^^^^^^^^^^
  62. .. code-block:: c
  63. int bpf_map_update_elem(int fd, const void *key, const void *value, __u64 flags);
  64. Net device entries can be added or updated using the ``bpf_map_update_elem()``
  65. helper. This helper replaces existing elements atomically. The ``value`` parameter
  66. can be ``struct bpf_devmap_val`` or a simple ``int ifindex`` for backwards
  67. compatibility.
  68. .. code-block:: c
  69. struct bpf_devmap_val {
  70. __u32 ifindex; /* device index */
  71. union {
  72. int fd; /* prog fd on map write */
  73. __u32 id; /* prog id on map read */
  74. } bpf_prog;
  75. };
  76. The ``flags`` argument can be one of the following:
  77. - ``BPF_ANY``: Create a new element or update an existing element.
  78. - ``BPF_NOEXIST``: Create a new element only if it did not exist.
  79. - ``BPF_EXIST``: Update an existing element.
  80. DEVMAPs can associate a program with a device entry by adding a ``bpf_prog.fd``
  81. to ``struct bpf_devmap_val``. Programs are run after ``XDP_REDIRECT`` and have
  82. access to both Rx device and Tx device. The program associated with the ``fd``
  83. must have type XDP with expected attach type ``xdp_devmap``.
  84. When a program is associated with a device index, the program is run on an
  85. ``XDP_REDIRECT`` and before the buffer is added to the per-cpu queue. Examples
  86. of how to attach/use xdp_devmap progs can be found in the kernel selftests:
  87. - ``tools/testing/selftests/bpf/prog_tests/xdp_devmap_attach.c``
  88. - ``tools/testing/selftests/bpf/progs/test_xdp_with_devmap_helpers.c``
  89. bpf_map_lookup_elem()
  90. ^^^^^^^^^^^^^^^^^^^^^
  91. .. code-block:: c
  92. .. c:function::
  93. int bpf_map_lookup_elem(int fd, const void *key, void *value);
  94. Net device entries can be retrieved using the ``bpf_map_lookup_elem()``
  95. helper.
  96. bpf_map_delete_elem()
  97. ^^^^^^^^^^^^^^^^^^^^^
  98. .. code-block:: c
  99. .. c:function::
  100. int bpf_map_delete_elem(int fd, const void *key);
  101. Net device entries can be deleted using the ``bpf_map_delete_elem()``
  102. helper. This helper will return 0 on success, or negative error in case of
  103. failure.
  104. Examples
  105. ========
  106. Kernel BPF
  107. ----------
  108. The following code snippet shows how to declare a ``BPF_MAP_TYPE_DEVMAP``
  109. called tx_port.
  110. .. code-block:: c
  111. struct {
  112. __uint(type, BPF_MAP_TYPE_DEVMAP);
  113. __type(key, __u32);
  114. __type(value, __u32);
  115. __uint(max_entries, 256);
  116. } tx_port SEC(".maps");
  117. The following code snippet shows how to declare a ``BPF_MAP_TYPE_DEVMAP_HASH``
  118. called forward_map.
  119. .. code-block:: c
  120. struct {
  121. __uint(type, BPF_MAP_TYPE_DEVMAP_HASH);
  122. __type(key, __u32);
  123. __type(value, struct bpf_devmap_val);
  124. __uint(max_entries, 32);
  125. } forward_map SEC(".maps");
  126. .. note::
  127. The value type in the DEVMAP above is a ``struct bpf_devmap_val``
  128. The following code snippet shows a simple xdp_redirect_map program. This program
  129. would work with a user space program that populates the devmap ``forward_map`` based
  130. on ingress ifindexes. The BPF program (below) is redirecting packets using the
  131. ingress ``ifindex`` as the ``key``.
  132. .. code-block:: c
  133. SEC("xdp")
  134. int xdp_redirect_map_func(struct xdp_md *ctx)
  135. {
  136. int index = ctx->ingress_ifindex;
  137. return bpf_redirect_map(&forward_map, index, 0);
  138. }
  139. The following code snippet shows a BPF program that is broadcasting packets to
  140. all the interfaces in the ``tx_port`` devmap.
  141. .. code-block:: c
  142. SEC("xdp")
  143. int xdp_redirect_map_func(struct xdp_md *ctx)
  144. {
  145. return bpf_redirect_map(&tx_port, 0, BPF_F_BROADCAST | BPF_F_EXCLUDE_INGRESS);
  146. }
  147. User space
  148. ----------
  149. The following code snippet shows how to update a devmap called ``tx_port``.
  150. .. code-block:: c
  151. int update_devmap(int ifindex, int redirect_ifindex)
  152. {
  153. int ret;
  154. ret = bpf_map_update_elem(bpf_map__fd(tx_port), &ifindex, &redirect_ifindex, 0);
  155. if (ret < 0) {
  156. fprintf(stderr, "Failed to update devmap_ value: %s\n",
  157. strerror(errno));
  158. }
  159. return ret;
  160. }
  161. The following code snippet shows how to update a hash_devmap called ``forward_map``.
  162. .. code-block:: c
  163. int update_devmap(int ifindex, int redirect_ifindex)
  164. {
  165. struct bpf_devmap_val devmap_val = { .ifindex = redirect_ifindex };
  166. int ret;
  167. ret = bpf_map_update_elem(bpf_map__fd(forward_map), &ifindex, &devmap_val, 0);
  168. if (ret < 0) {
  169. fprintf(stderr, "Failed to update devmap_ value: %s\n",
  170. strerror(errno));
  171. }
  172. return ret;
  173. }
  174. References
  175. ===========
  176. - https://lwn.net/Articles/728146/
  177. - https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/commit/?id=6f9d451ab1a33728adb72d7ff66a7b374d665176
  178. - https://elixir.bootlin.com/linux/latest/source/net/core/filter.c#L4106