1*9620e956SStanislav Fomichev.. SPDX-License-Identifier: GPL-2.0 2*9620e956SStanislav Fomichev 3a4aeb9d6SStanislav Fomichev=============== 4a4aeb9d6SStanislav FomichevXDP RX Metadata 5a4aeb9d6SStanislav Fomichev=============== 6a4aeb9d6SStanislav Fomichev 7a4aeb9d6SStanislav FomichevThis document describes how an eXpress Data Path (XDP) program can access 8a4aeb9d6SStanislav Fomichevhardware metadata related to a packet using a set of helper functions, 9a4aeb9d6SStanislav Fomichevand how it can pass that metadata on to other consumers. 10a4aeb9d6SStanislav Fomichev 11a4aeb9d6SStanislav FomichevGeneral Design 12a4aeb9d6SStanislav Fomichev============== 13a4aeb9d6SStanislav Fomichev 14a4aeb9d6SStanislav FomichevXDP has access to a set of kfuncs to manipulate the metadata in an XDP frame. 15a4aeb9d6SStanislav FomichevEvery device driver that wishes to expose additional packet metadata can 16a4aeb9d6SStanislav Fomichevimplement these kfuncs. The set of kfuncs is declared in ``include/net/xdp.h`` 17a4aeb9d6SStanislav Fomichevvia ``XDP_METADATA_KFUNC_xxx``. 18a4aeb9d6SStanislav Fomichev 19a4aeb9d6SStanislav FomichevCurrently, the following kfuncs are supported. In the future, as more 20a4aeb9d6SStanislav Fomichevmetadata is supported, this set will grow: 21a4aeb9d6SStanislav Fomichev 22a4aeb9d6SStanislav Fomichev.. kernel-doc:: net/core/xdp.c 23a4aeb9d6SStanislav Fomichev :identifiers: bpf_xdp_metadata_rx_timestamp bpf_xdp_metadata_rx_hash 24a4aeb9d6SStanislav Fomichev 25a4aeb9d6SStanislav FomichevAn XDP program can use these kfuncs to read the metadata into stack 26a4aeb9d6SStanislav Fomichevvariables for its own consumption. Or, to pass the metadata on to other 27a4aeb9d6SStanislav Fomichevconsumers, an XDP program can store it into the metadata area carried 28915efd8aSJesper Dangaard Brouerahead of the packet. Not all packets will necessary have the requested 29915efd8aSJesper Dangaard Brouermetadata available in which case the driver returns ``-ENODATA``. 30a4aeb9d6SStanislav Fomichev 31a4aeb9d6SStanislav FomichevNot all kfuncs have to be implemented by the device driver; when not 32915efd8aSJesper Dangaard Brouerimplemented, the default ones that return ``-EOPNOTSUPP`` will be used 33915efd8aSJesper Dangaard Brouerto indicate the device driver have not implemented this kfunc. 34915efd8aSJesper Dangaard Brouer 35a4aeb9d6SStanislav Fomichev 36a4aeb9d6SStanislav FomichevWithin an XDP frame, the metadata layout (accessed via ``xdp_buff``) is 37a4aeb9d6SStanislav Fomichevas follows:: 38a4aeb9d6SStanislav Fomichev 39a4aeb9d6SStanislav Fomichev +----------+-----------------+------+ 40a4aeb9d6SStanislav Fomichev | headroom | custom metadata | data | 41a4aeb9d6SStanislav Fomichev +----------+-----------------+------+ 42a4aeb9d6SStanislav Fomichev ^ ^ 43a4aeb9d6SStanislav Fomichev | | 44a4aeb9d6SStanislav Fomichev xdp_buff->data_meta xdp_buff->data 45a4aeb9d6SStanislav Fomichev 46a4aeb9d6SStanislav FomichevAn XDP program can store individual metadata items into this ``data_meta`` 47a4aeb9d6SStanislav Fomichevarea in whichever format it chooses. Later consumers of the metadata 48a4aeb9d6SStanislav Fomichevwill have to agree on the format by some out of band contract (like for 49a4aeb9d6SStanislav Fomichevthe AF_XDP use case, see below). 50a4aeb9d6SStanislav Fomichev 51a4aeb9d6SStanislav FomichevAF_XDP 52a4aeb9d6SStanislav Fomichev====== 53a4aeb9d6SStanislav Fomichev 54a4aeb9d6SStanislav Fomichev:doc:`af_xdp` use-case implies that there is a contract between the BPF 55a4aeb9d6SStanislav Fomichevprogram that redirects XDP frames into the ``AF_XDP`` socket (``XSK``) and 56a4aeb9d6SStanislav Fomichevthe final consumer. Thus the BPF program manually allocates a fixed number of 57a4aeb9d6SStanislav Fomichevbytes out of metadata via ``bpf_xdp_adjust_meta`` and calls a subset 58a4aeb9d6SStanislav Fomichevof kfuncs to populate it. The userspace ``XSK`` consumer computes 59a4aeb9d6SStanislav Fomichev``xsk_umem__get_data() - METADATA_SIZE`` to locate that metadata. 60a4aeb9d6SStanislav FomichevNote, ``xsk_umem__get_data`` is defined in ``libxdp`` and 61a4aeb9d6SStanislav Fomichev``METADATA_SIZE`` is an application-specific constant (``AF_XDP`` receive 62a4aeb9d6SStanislav Fomichevdescriptor does _not_ explicitly carry the size of the metadata). 63a4aeb9d6SStanislav Fomichev 64a4aeb9d6SStanislav FomichevHere is the ``AF_XDP`` consumer layout (note missing ``data_meta`` pointer):: 65a4aeb9d6SStanislav Fomichev 66a4aeb9d6SStanislav Fomichev +----------+-----------------+------+ 67a4aeb9d6SStanislav Fomichev | headroom | custom metadata | data | 68a4aeb9d6SStanislav Fomichev +----------+-----------------+------+ 69a4aeb9d6SStanislav Fomichev ^ 70a4aeb9d6SStanislav Fomichev | 71a4aeb9d6SStanislav Fomichev rx_desc->address 72a4aeb9d6SStanislav Fomichev 73a4aeb9d6SStanislav FomichevXDP_PASS 74a4aeb9d6SStanislav Fomichev======== 75a4aeb9d6SStanislav Fomichev 76a4aeb9d6SStanislav FomichevThis is the path where the packets processed by the XDP program are passed 77a4aeb9d6SStanislav Fomichevinto the kernel. The kernel creates the ``skb`` out of the ``xdp_buff`` 78a4aeb9d6SStanislav Fomichevcontents. Currently, every driver has custom kernel code to parse 79a4aeb9d6SStanislav Fomichevthe descriptors and populate ``skb`` metadata when doing this ``xdp_buff->skb`` 80a4aeb9d6SStanislav Fomichevconversion, and the XDP metadata is not used by the kernel when building 81a4aeb9d6SStanislav Fomichev``skbs``. However, TC-BPF programs can access the XDP metadata area using 82a4aeb9d6SStanislav Fomichevthe ``data_meta`` pointer. 83a4aeb9d6SStanislav Fomichev 84a4aeb9d6SStanislav FomichevIn the future, we'd like to support a case where an XDP program 85a4aeb9d6SStanislav Fomichevcan override some of the metadata used for building ``skbs``. 86a4aeb9d6SStanislav Fomichev 87a4aeb9d6SStanislav Fomichevbpf_redirect_map 88a4aeb9d6SStanislav Fomichev================ 89a4aeb9d6SStanislav Fomichev 90a4aeb9d6SStanislav Fomichev``bpf_redirect_map`` can redirect the frame to a different device. 91a4aeb9d6SStanislav FomichevSome devices (like virtual ethernet links) support running a second XDP 92a4aeb9d6SStanislav Fomichevprogram after the redirect. However, the final consumer doesn't have 93a4aeb9d6SStanislav Fomichevaccess to the original hardware descriptor and can't access any of 94a4aeb9d6SStanislav Fomichevthe original metadata. The same applies to XDP programs installed 95a4aeb9d6SStanislav Fomichevinto devmaps and cpumaps. 96a4aeb9d6SStanislav Fomichev 97a4aeb9d6SStanislav FomichevThis means that for redirected packets only custom metadata is 98a4aeb9d6SStanislav Fomichevcurrently supported, which has to be prepared by the initial XDP program 99a4aeb9d6SStanislav Fomichevbefore redirect. If the frame is eventually passed to the kernel, the 100a4aeb9d6SStanislav Fomichev``skb`` created from such a frame won't have any hardware metadata populated 101a4aeb9d6SStanislav Fomichevin its ``skb``. If such a packet is later redirected into an ``XSK``, 102a4aeb9d6SStanislav Fomichevthat will also only have access to the custom metadata. 103a4aeb9d6SStanislav Fomichev 104a4aeb9d6SStanislav Fomichevbpf_tail_call 105a4aeb9d6SStanislav Fomichev============= 106a4aeb9d6SStanislav Fomichev 107a4aeb9d6SStanislav FomichevAdding programs that access metadata kfuncs to the ``BPF_MAP_TYPE_PROG_ARRAY`` 108a4aeb9d6SStanislav Fomichevis currently not supported. 109a4aeb9d6SStanislav Fomichev 110a9c2a608SStanislav FomichevSupported Devices 111a9c2a608SStanislav Fomichev================= 112a9c2a608SStanislav Fomichev 113a9c2a608SStanislav FomichevIt is possible to query which kfunc the particular netdev implements via 114a9c2a608SStanislav Fomichevnetlink. See ``xdp-rx-metadata-features`` attribute set in 115a9c2a608SStanislav Fomichev``Documentation/netlink/specs/netdev.yaml``. 116a9c2a608SStanislav Fomichev 117a4aeb9d6SStanislav FomichevExample 118a4aeb9d6SStanislav Fomichev======= 119a4aeb9d6SStanislav Fomichev 120a4aeb9d6SStanislav FomichevSee ``tools/testing/selftests/bpf/progs/xdp_metadata.c`` and 121a4aeb9d6SStanislav Fomichev``tools/testing/selftests/bpf/prog_tests/xdp_metadata.c`` for an example of 122a4aeb9d6SStanislav FomichevBPF program that handles XDP metadata. 123