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