[PATCH net-next v5 8/9] virtio-net: add XDP rx timestamp metadata

From: Steffen Trumtrar

Date: Thu Nov 12 2026 - 06:32:26 EST


For the skb, the timestamp is read from the header before the XDP
program runs, so XDP_PASS keeps it even if the program changes the
packet. XDP therefore works with rx timestamping enabled.

Add xmo_rx_timestamp so XDP programs can read the timestamp with
bpf_xdp_metadata_rx_timestamp(). It returns -ENODATA if timestamping
is not enabled or the device did not timestamp the packet. Install the
metadata ops also if only VIRTIO_NET_F_RX_TSTAMP is negotiated;
rx_hash returns -ENODATA then.

Like the hash, the timestamp is copied into struct virtnet_xdp_buff
before the program runs, so the callback still works after the program
changed the headroom with bpf_xdp_adjust_head() or
bpf_xdp_adjust_meta().

Signed-off-by: Steffen Trumtrar <s.trumtrar@xxxxxxxxxxxxxx>
---
drivers/net/virtio_net.c | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)

diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index 57b7c32625dfb..46be686e34b79 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -511,6 +511,7 @@ struct virtnet_xdp_buff {
struct xdp_buff xdp;
u32 hash_value;
u16 hash_report;
+ u64 tstamp;
};

static inline u32
@@ -1736,6 +1737,8 @@ static void virtnet_xdp_save_hdr(const struct virtnet_info *vi,
ctx->hash_value = 0;
ctx->hash_report = VIRTIO_NET_HASH_REPORT_NONE;
}
+
+ ctx->tstamp = virtnet_record_rx_tstamp(vi, xdp->data - vi->hdr_len);
}

static int virtnet_xdp_handler(struct bpf_prog *xdp_prog, struct xdp_buff *xdp,
@@ -6896,8 +6899,21 @@ static int virtnet_xdp_rx_hash(const struct xdp_md *_ctx, u32 *hash,
return 0;
}

+static int virtnet_xdp_rx_timestamp(const struct xdp_md *_ctx, u64 *timestamp)
+{
+ const struct virtnet_xdp_buff *ctx = (void *)_ctx;
+
+ /* 0: timestamping is off or the device did not timestamp the packet */
+ if (!ctx->tstamp)
+ return -ENODATA;
+
+ *timestamp = ctx->tstamp;
+ return 0;
+}
+
static const struct xdp_metadata_ops virtnet_xdp_metadata_ops = {
.xmo_rx_hash = virtnet_xdp_rx_hash,
+ .xmo_rx_timestamp = virtnet_xdp_rx_timestamp,
};

static int virtnet_probe(struct virtio_device *vdev)
@@ -7063,6 +7079,10 @@ static int virtnet_probe(struct virtio_device *vdev)
dev->xdp_metadata_ops = &virtnet_xdp_metadata_ops;
}

+ /* rx_hash returns -ENODATA without NETIF_F_RXHASH */
+ if (vi->rx_tstamp_supported)
+ dev->xdp_metadata_ops = &virtnet_xdp_metadata_ops;
+
if (vi->rx_tstamp_supported)
vi->hdr_len = offsetofend(struct virtio_net_common_hdr, tstamp);
else if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO) ||

--
2.56.0