Add a section on the skb core extension area to xdp-rx-metadata.rst. It shows where the area is in the headroom and lists the store, delete and load kfuncs. It also says when the kernel sets the stored values on the skb. The Supported Devices section names the skb-ext-pass XDP feature.
Update the XDP_PASS, bpf_redirect_map and bpf_tail_call sections, which did not cover the area. Document xdp_skb_ext_to_skb(), the skb-ext-pass XDP feature and xdp_build_skb_from_frame() in the driver section, and point to the selftests in the example section. Signed-off-by: Tianyi Gao <[email protected]> --- Documentation/networking/xdp-rx-metadata.rst | 127 +++++++++++++++++-- 1 file changed, 116 insertions(+), 11 deletions(-) diff --git a/Documentation/networking/xdp-rx-metadata.rst b/Documentation/networking/xdp-rx-metadata.rst index efdf5eeb49e7..61dcc24d7342 100644 --- a/Documentation/networking/xdp-rx-metadata.rst +++ b/Documentation/networking/xdp-rx-metadata.rst @@ -54,6 +54,83 @@ area in whichever format it chooses. Later consumers of the metadata will have to agree on the format by some out of band contract (like for the AF_XDP use case, see below). +SKB Core Extension Area +======================= + +An XDP program can also store RX metadata that the kernel sets on the +``skb`` built from the packet. It stores and deletes the values with +these kfuncs: + +.. kernel-doc:: net/core/xdp.c + :identifiers: bpf_xdp_skb_ext_store_rx_hash + +.. kernel-doc:: net/core/xdp.c + :identifiers: bpf_xdp_skb_ext_store_rx_vlan + +.. kernel-doc:: net/core/xdp.c + :identifiers: bpf_xdp_skb_ext_store_rx_csum + +.. kernel-doc:: net/core/xdp.c + :identifiers: bpf_xdp_skb_ext_del + +The values are kept in the skb core extension area. The area starts at +``data_hard_start + sizeof(struct xdp_frame)``, right after the room for +the ``xdp_frame``:: + + +-----------+-----------+----------------+---------------------+------+ + | xdp_frame | skb core | headroom | custom metadata | data | + | | extension | | | | + +-----------+-----------+----------------+---------------------+------+ + ^ ^ ^ + | | | + xdp_buff->data_hard_start xdp_buff->data_meta xdp_buff->data + +The area is not an skb extension (``struct skb_ext``) and needs no +extra allocation. It takes no space until the first store. It holds a +4-byte header and the stored entries. The header and all three entries +take 20 bytes. ``bpf_xdp_adjust_head``, ``bpf_xdp_adjust_meta`` and +``bpf_xdp_pull_data`` do not move the packet or the metadata into the +area. + +The kernel sets the stored values on the ``skb`` in these cases: + +- When cpumap or veth builds the ``skb`` from an ``xdp_frame`` after + ``XDP_REDIRECT``. +- When ``BPF_PROG_RUN`` with ``BPF_F_TEST_XDP_LIVE_FRAMES`` builds the + ``skb`` after ``XDP_PASS``. +- On ``XDP_PASS``, in a driver with the ``skb-ext-pass`` XDP feature, + see `Supported Devices`_. + +On ``XDP_PASS``, the stored values replace the values that the driver +sets from the RX descriptor. On a native XDP driver without the feature, +the kernel warns once when the program returns ``XDP_PASS`` with stored +values. When XDP runs on an ``skb``, in generic XDP or when veth receives +an ``skb`` from its peer, the kernel does not set the stored values on +``XDP_PASS`` and also warns once. + +The program that stores the values can read them back. So can the +programs that run on the ``xdp_frame`` after ``XDP_REDIRECT``, on cpumap, +devmap and veth: + +.. kernel-doc:: net/core/xdp.c + :identifiers: bpf_xdp_skb_ext_load_rx_hash + +.. kernel-doc:: net/core/xdp.c + :identifiers: bpf_xdp_skb_ext_load_rx_vlan + +.. kernel-doc:: net/core/xdp.c + :identifiers: bpf_xdp_skb_ext_load_rx_csum + +They return only the stored values. The RX metadata kfuncs above still +read the values from the RX descriptor. Unlike those, the kfuncs in this +section do not need a program that is bound to a device. + +On ``AF_XDP`` zero-copy, the store kfuncs return ``-EOPNOTSUPP``, because +user space can write the headroom. The area would not reach the skb +anyway. On ``XDP_REDIRECT``, ``xdp_convert_zc_to_xdp_frame`` copies only +the custom metadata and the packet. On ``XDP_PASS``, drivers do not apply +the area. + AF_XDP ====== @@ -83,12 +160,13 @@ This is the path where the packets processed by the XDP program are passed into the kernel. The kernel creates the ``skb`` out of the ``xdp_buff`` contents. Currently, every driver has custom kernel code to parse the descriptors and populate ``skb`` metadata when doing this ``xdp_buff->skb`` -conversion, and the XDP metadata is not used by the kernel when building +conversion, and the custom metadata is not used by the kernel when building ``skbs``. However, TC-BPF programs can access the XDP metadata area using the ``data_meta`` pointer. -In the future, we'd like to support a case where an XDP program -can override some of the metadata used for building ``skbs``. +On drivers with the ``skb-ext-pass`` XDP feature, an XDP program can use +the `SKB Core Extension Area`_ to override the hash, VLAN tag and checksum +status of the ``skb``. bpf_redirect_map ================ @@ -100,18 +178,19 @@ access to the original hardware descriptor and can't access any of the original metadata. The same applies to XDP programs installed into devmaps and cpumaps. -This means that for redirected packets only custom metadata is -currently supported, which has to be prepared by the initial XDP program -before redirect. If the frame is eventually passed to the kernel, the -``skb`` created from such a frame won't have any hardware metadata populated -in its ``skb``. If such a packet is later redirected into an ``XSK``, -that will also only have access to the custom metadata. +This means that the initial XDP program has to prepare the metadata +before the redirect, as custom metadata or in the `SKB Core Extension +Area`_. If the frame is eventually passed to the kernel, the ``skb`` +created from such a frame gets its hash, VLAN tag and checksum status +only from the area. If such a packet is later redirected into an +``XSK``, that will only have access to the custom metadata. bpf_tail_call ============= Adding programs that access metadata kfuncs to the ``BPF_MAP_TYPE_PROG_ARRAY`` -is currently not supported. +is currently not supported. The kfuncs of the `SKB Core Extension Area`_ +do not have this limit. Supported Devices ================= @@ -120,6 +199,12 @@ It is possible to query which kfunc the particular netdev implements via netlink. See ``xdp-rx-metadata-features`` attribute set in ``Documentation/netlink/specs/netdev.yaml``. +A netdev that sets the values from the `SKB Core Extension Area`_ on +``XDP_PASS`` in native XDP has the ``skb-ext-pass`` flag in its +``xdp-features`` attribute. The flag does not cover generic XDP or the +veth ``skb`` path. An XDP program can also read ``xdp_features`` of the +receiving netdev after ``bpf_cast_to_kern_ctx``. + Driver Implementation ===================== @@ -153,9 +238,29 @@ xdp_frame. If these conditions are not met, it returns a negative error. In this case, the BPF program should not proceed to populate data into the ``data_meta`` area. +A driver that builds the ``skb`` on ``XDP_PASS`` applies the +`SKB Core Extension Area`_ and sets ``NETDEV_XDP_ACT_SKB_EXT_PASS``. The +driver sets the ``skb`` fields from the RX descriptor and then calls this +function. The driver takes the area pointer with ``xdp_buff_skb_ext`` +before it reuses the ``xdp_buff``: + +.. kernel-doc:: net/core/xdp.c + :identifiers: xdp_skb_ext_to_skb + +``xdp_build_skb_from_frame`` does not apply the area. Its callers set the +RX fields themselves or transmit the ``skb``. + +A driver that moves ``data_hard_start`` after the XDP program ran calls +``xdp_buff_clear_skb_ext_flag``, because the area is no longer at the +expected place. The stored values are then lost. + Example ======= See ``tools/testing/selftests/bpf/progs/xdp_metadata.c`` and ``tools/testing/selftests/bpf/prog_tests/xdp_metadata.c`` for an example of -BPF program that handles XDP metadata. +BPF program that handles XDP metadata. See +``tools/testing/selftests/bpf/progs/test_xdp_skb_ext.c`` and +``tools/testing/selftests/bpf/prog_tests/xdp_skb_ext.c`` for the skb core +extension area, and ``tools/testing/selftests/drivers/net/hw/xdp_metadata.py`` +for a driver on ``XDP_PASS``. -- 2.56.0
