xdp-rx-metadata.rst 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ===============
  3. XDP RX Metadata
  4. ===============
  5. This document describes how an eXpress Data Path (XDP) program can access
  6. hardware metadata related to a packet using a set of helper functions,
  7. and how it can pass that metadata on to other consumers.
  8. General Design
  9. ==============
  10. XDP has access to a set of kfuncs to manipulate the metadata in an XDP frame.
  11. Every device driver that wishes to expose additional packet metadata can
  12. implement these kfuncs. The set of kfuncs is declared in ``include/net/xdp.h``
  13. via ``XDP_METADATA_KFUNC_xxx``.
  14. Currently, the following kfuncs are supported. In the future, as more
  15. metadata is supported, this set will grow:
  16. .. kernel-doc:: net/core/xdp.c
  17. :identifiers: bpf_xdp_metadata_rx_timestamp
  18. .. kernel-doc:: net/core/xdp.c
  19. :identifiers: bpf_xdp_metadata_rx_hash
  20. .. kernel-doc:: net/core/xdp.c
  21. :identifiers: bpf_xdp_metadata_rx_vlan_tag
  22. An XDP program can use these kfuncs to read the metadata into stack
  23. variables for its own consumption. Or, to pass the metadata on to other
  24. consumers, an XDP program can store it into the metadata area carried
  25. ahead of the packet. Not all packets will necessary have the requested
  26. metadata available in which case the driver returns ``-ENODATA``.
  27. Not all kfuncs have to be implemented by the device driver; when not
  28. implemented, the default ones that return ``-EOPNOTSUPP`` will be used
  29. to indicate the device driver have not implemented this kfunc.
  30. Within an XDP frame, the metadata layout (accessed via ``xdp_buff``) is
  31. as follows::
  32. +----------+-----------------+------+
  33. | headroom | custom metadata | data |
  34. +----------+-----------------+------+
  35. ^ ^
  36. | |
  37. xdp_buff->data_meta xdp_buff->data
  38. An XDP program can store individual metadata items into this ``data_meta``
  39. area in whichever format it chooses. Later consumers of the metadata
  40. will have to agree on the format by some out of band contract (like for
  41. the AF_XDP use case, see below).
  42. AF_XDP
  43. ======
  44. :doc:`af_xdp` use-case implies that there is a contract between the BPF
  45. program that redirects XDP frames into the ``AF_XDP`` socket (``XSK``) and
  46. the final consumer. Thus the BPF program manually allocates a fixed number of
  47. bytes out of metadata via ``bpf_xdp_adjust_meta`` and calls a subset
  48. of kfuncs to populate it. The userspace ``XSK`` consumer computes
  49. ``xsk_umem__get_data() - METADATA_SIZE`` to locate that metadata.
  50. Note, ``xsk_umem__get_data`` is defined in ``libxdp`` and
  51. ``METADATA_SIZE`` is an application-specific constant (``AF_XDP`` receive
  52. descriptor does _not_ explicitly carry the size of the metadata).
  53. Here is the ``AF_XDP`` consumer layout (note missing ``data_meta`` pointer)::
  54. +----------+-----------------+------+
  55. | headroom | custom metadata | data |
  56. +----------+-----------------+------+
  57. ^
  58. |
  59. rx_desc->address
  60. XDP_PASS
  61. ========
  62. This is the path where the packets processed by the XDP program are passed
  63. into the kernel. The kernel creates the ``skb`` out of the ``xdp_buff``
  64. contents. Currently, every driver has custom kernel code to parse
  65. the descriptors and populate ``skb`` metadata when doing this ``xdp_buff->skb``
  66. conversion, and the XDP metadata is not used by the kernel when building
  67. ``skbs``. However, TC-BPF programs can access the XDP metadata area using
  68. the ``data_meta`` pointer.
  69. In the future, we'd like to support a case where an XDP program
  70. can override some of the metadata used for building ``skbs``.
  71. bpf_redirect_map
  72. ================
  73. ``bpf_redirect_map`` can redirect the frame to a different device.
  74. Some devices (like virtual ethernet links) support running a second XDP
  75. program after the redirect. However, the final consumer doesn't have
  76. access to the original hardware descriptor and can't access any of
  77. the original metadata. The same applies to XDP programs installed
  78. into devmaps and cpumaps.
  79. This means that for redirected packets only custom metadata is
  80. currently supported, which has to be prepared by the initial XDP program
  81. before redirect. If the frame is eventually passed to the kernel, the
  82. ``skb`` created from such a frame won't have any hardware metadata populated
  83. in its ``skb``. If such a packet is later redirected into an ``XSK``,
  84. that will also only have access to the custom metadata.
  85. bpf_tail_call
  86. =============
  87. Adding programs that access metadata kfuncs to the ``BPF_MAP_TYPE_PROG_ARRAY``
  88. is currently not supported.
  89. Supported Devices
  90. =================
  91. It is possible to query which kfunc the particular netdev implements via
  92. netlink. See ``xdp-rx-metadata-features`` attribute set in
  93. ``Documentation/netlink/specs/netdev.yaml``.
  94. Example
  95. =======
  96. See ``tools/testing/selftests/bpf/progs/xdp_metadata.c`` and
  97. ``tools/testing/selftests/bpf/prog_tests/xdp_metadata.c`` for an example of
  98. BPF program that handles XDP metadata.