162306a36Sopenharmony_ci=============== 262306a36Sopenharmony_ciXDP RX Metadata 362306a36Sopenharmony_ci=============== 462306a36Sopenharmony_ci 562306a36Sopenharmony_ciThis document describes how an eXpress Data Path (XDP) program can access 662306a36Sopenharmony_cihardware metadata related to a packet using a set of helper functions, 762306a36Sopenharmony_ciand how it can pass that metadata on to other consumers. 862306a36Sopenharmony_ci 962306a36Sopenharmony_ciGeneral Design 1062306a36Sopenharmony_ci============== 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_ciXDP has access to a set of kfuncs to manipulate the metadata in an XDP frame. 1362306a36Sopenharmony_ciEvery device driver that wishes to expose additional packet metadata can 1462306a36Sopenharmony_ciimplement these kfuncs. The set of kfuncs is declared in ``include/net/xdp.h`` 1562306a36Sopenharmony_civia ``XDP_METADATA_KFUNC_xxx``. 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ciCurrently, the following kfuncs are supported. In the future, as more 1862306a36Sopenharmony_cimetadata is supported, this set will grow: 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_ci.. kernel-doc:: net/core/xdp.c 2162306a36Sopenharmony_ci :identifiers: bpf_xdp_metadata_rx_timestamp bpf_xdp_metadata_rx_hash 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ciAn XDP program can use these kfuncs to read the metadata into stack 2462306a36Sopenharmony_civariables for its own consumption. Or, to pass the metadata on to other 2562306a36Sopenharmony_ciconsumers, an XDP program can store it into the metadata area carried 2662306a36Sopenharmony_ciahead of the packet. Not all packets will necessary have the requested 2762306a36Sopenharmony_cimetadata available in which case the driver returns ``-ENODATA``. 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ciNot all kfuncs have to be implemented by the device driver; when not 3062306a36Sopenharmony_ciimplemented, the default ones that return ``-EOPNOTSUPP`` will be used 3162306a36Sopenharmony_cito indicate the device driver have not implemented this kfunc. 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ciWithin an XDP frame, the metadata layout (accessed via ``xdp_buff``) is 3562306a36Sopenharmony_cias follows:: 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ci +----------+-----------------+------+ 3862306a36Sopenharmony_ci | headroom | custom metadata | data | 3962306a36Sopenharmony_ci +----------+-----------------+------+ 4062306a36Sopenharmony_ci ^ ^ 4162306a36Sopenharmony_ci | | 4262306a36Sopenharmony_ci xdp_buff->data_meta xdp_buff->data 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ciAn XDP program can store individual metadata items into this ``data_meta`` 4562306a36Sopenharmony_ciarea in whichever format it chooses. Later consumers of the metadata 4662306a36Sopenharmony_ciwill have to agree on the format by some out of band contract (like for 4762306a36Sopenharmony_cithe AF_XDP use case, see below). 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ciAF_XDP 5062306a36Sopenharmony_ci====== 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci:doc:`af_xdp` use-case implies that there is a contract between the BPF 5362306a36Sopenharmony_ciprogram that redirects XDP frames into the ``AF_XDP`` socket (``XSK``) and 5462306a36Sopenharmony_cithe final consumer. Thus the BPF program manually allocates a fixed number of 5562306a36Sopenharmony_cibytes out of metadata via ``bpf_xdp_adjust_meta`` and calls a subset 5662306a36Sopenharmony_ciof kfuncs to populate it. The userspace ``XSK`` consumer computes 5762306a36Sopenharmony_ci``xsk_umem__get_data() - METADATA_SIZE`` to locate that metadata. 5862306a36Sopenharmony_ciNote, ``xsk_umem__get_data`` is defined in ``libxdp`` and 5962306a36Sopenharmony_ci``METADATA_SIZE`` is an application-specific constant (``AF_XDP`` receive 6062306a36Sopenharmony_cidescriptor does _not_ explicitly carry the size of the metadata). 6162306a36Sopenharmony_ci 6262306a36Sopenharmony_ciHere is the ``AF_XDP`` consumer layout (note missing ``data_meta`` pointer):: 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci +----------+-----------------+------+ 6562306a36Sopenharmony_ci | headroom | custom metadata | data | 6662306a36Sopenharmony_ci +----------+-----------------+------+ 6762306a36Sopenharmony_ci ^ 6862306a36Sopenharmony_ci | 6962306a36Sopenharmony_ci rx_desc->address 7062306a36Sopenharmony_ci 7162306a36Sopenharmony_ciXDP_PASS 7262306a36Sopenharmony_ci======== 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ciThis is the path where the packets processed by the XDP program are passed 7562306a36Sopenharmony_ciinto the kernel. The kernel creates the ``skb`` out of the ``xdp_buff`` 7662306a36Sopenharmony_cicontents. Currently, every driver has custom kernel code to parse 7762306a36Sopenharmony_cithe descriptors and populate ``skb`` metadata when doing this ``xdp_buff->skb`` 7862306a36Sopenharmony_ciconversion, and the XDP metadata is not used by the kernel when building 7962306a36Sopenharmony_ci``skbs``. However, TC-BPF programs can access the XDP metadata area using 8062306a36Sopenharmony_cithe ``data_meta`` pointer. 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ciIn the future, we'd like to support a case where an XDP program 8362306a36Sopenharmony_cican override some of the metadata used for building ``skbs``. 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_cibpf_redirect_map 8662306a36Sopenharmony_ci================ 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_ci``bpf_redirect_map`` can redirect the frame to a different device. 8962306a36Sopenharmony_ciSome devices (like virtual ethernet links) support running a second XDP 9062306a36Sopenharmony_ciprogram after the redirect. However, the final consumer doesn't have 9162306a36Sopenharmony_ciaccess to the original hardware descriptor and can't access any of 9262306a36Sopenharmony_cithe original metadata. The same applies to XDP programs installed 9362306a36Sopenharmony_ciinto devmaps and cpumaps. 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ciThis means that for redirected packets only custom metadata is 9662306a36Sopenharmony_cicurrently supported, which has to be prepared by the initial XDP program 9762306a36Sopenharmony_cibefore redirect. If the frame is eventually passed to the kernel, the 9862306a36Sopenharmony_ci``skb`` created from such a frame won't have any hardware metadata populated 9962306a36Sopenharmony_ciin its ``skb``. If such a packet is later redirected into an ``XSK``, 10062306a36Sopenharmony_cithat will also only have access to the custom metadata. 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_cibpf_tail_call 10362306a36Sopenharmony_ci============= 10462306a36Sopenharmony_ci 10562306a36Sopenharmony_ciAdding programs that access metadata kfuncs to the ``BPF_MAP_TYPE_PROG_ARRAY`` 10662306a36Sopenharmony_ciis currently not supported. 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ciExample 10962306a36Sopenharmony_ci======= 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ciSee ``tools/testing/selftests/bpf/progs/xdp_metadata.c`` and 11262306a36Sopenharmony_ci``tools/testing/selftests/bpf/prog_tests/xdp_metadata.c`` for an example of 11362306a36Sopenharmony_ciBPF program that handles XDP metadata. 114