Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

doc: xdp: Clarify driver implementation for XDP Rx metadata

Clarify that drivers must remove device-reserved metadata from the
data_meta area before passing frames to XDP programs.

Additionally, expand the explanation of how userspace and BPF programs
should coordinate the use of METADATA_SIZE, and add a detailed diagram
to illustrate pointer adjustments and metadata layout.

Also describe the requirements and constraints enforced by
bpf_xdp_adjust_meta().

Signed-off-by: Song Yoong Siang <yoong.siang.song@intel.com>
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Acked-by: Stanislav Fomichev <sdf@fomichev.me>
Link: https://lore.kernel.org/r/20250716154846.3513575-1-yoong.siang.song@intel.com

authored by

Song Yoong Siang and committed by
Martin KaFai Lau
ef57dc6f 6e375b23

+33
+33
Documentation/networking/xdp-rx-metadata.rst
··· 120 120 netlink. See ``xdp-rx-metadata-features`` attribute set in 121 121 ``Documentation/netlink/specs/netdev.yaml``. 122 122 123 + Driver Implementation 124 + ===================== 125 + 126 + Certain devices may prepend metadata to received packets. However, as of now, 127 + ``AF_XDP`` lacks the ability to communicate the size of the ``data_meta`` area 128 + to the consumer. Therefore, it is the responsibility of the driver to copy any 129 + device-reserved metadata out from the metadata area and ensure that 130 + ``xdp_buff->data_meta`` is pointing to ``xdp_buff->data`` before presenting the 131 + frame to the XDP program. This is necessary so that, after the XDP program 132 + adjusts the metadata area, the consumer can reliably retrieve the metadata 133 + address using ``METADATA_SIZE`` offset. 134 + 135 + The following diagram shows how custom metadata is positioned relative to the 136 + packet data and how pointers are adjusted for metadata access:: 137 + 138 + |<-- bpf_xdp_adjust_meta(xdp_buff, -METADATA_SIZE) --| 139 + new xdp_buff->data_meta old xdp_buff->data_meta 140 + | | 141 + | xdp_buff->data 142 + | | 143 + +----------+----------------------------------------------------+------+ 144 + | headroom | custom metadata | data | 145 + +----------+----------------------------------------------------+------+ 146 + | | 147 + | xdp_desc->addr 148 + |<------ xsk_umem__get_data() - METADATA_SIZE -------| 149 + 150 + ``bpf_xdp_adjust_meta`` ensures that ``METADATA_SIZE`` is aligned to 4 bytes, 151 + does not exceed 252 bytes, and leaves sufficient space for building the 152 + xdp_frame. If these conditions are not met, it returns a negative error. In this 153 + case, the BPF program should not proceed to populate data into the ``data_meta`` 154 + area. 155 + 123 156 Example 124 157 ======= 125 158