* [PATCH net-next v5 0/9] virtio-net: add receive timestamp support
@ 2026-11-12 11:19 Steffen Trumtrar
2026-11-12 11:19 ` [PATCH net-next v5 1/9] net: ethtool: tsconfig: allow hardware timestamping without a PHC Steffen Trumtrar
` (9 more replies)
0 siblings, 10 replies; 16+ messages in thread
From: Steffen Trumtrar @ 2026-11-12 11:19 UTC (permalink / raw)
To: Michael S. Tsirkin, Xuan Zhuo, David S. Miller, Jakub Kicinski,
Paolo Abeni, Richard Cochran, Willem de Bruijn, Andrew Lunn,
Eugenio Pérez, Jason Wang, Alexei Starovoitov,
Daniel Borkmann, Jesper Dangaard Brouer, John Fastabend,
Stanislav Fomichev, Eric Dumazet, Andrew Lunn, Simon Horman,
Kory Maincent, Liang Chen, Heng Qi
Cc: virtualization, netdev, linux-kernel, bpf, kvm, kernel,
Steffen Trumtrar
This series picks up Willem de Bruijn's work on a virtio-net receive
timestamp. The device puts the time of arrival (TAI, ns) into the virtio-net
header so the guest can use it for PTP and RTT estimation without
taking the timestamp itself after the host-to-guest latency.
In detail, with the points that are still open:
- The timestamp is a new field at the end of virtio_net_common_hdr
after outer_nh_offset and ipsec, offset 32 (see virtio spec),
negotiated with VIRTIO_NET_F_RX_TSTAMP (bit 49). Header size now 40
bytes.
Willem noted on v3 that virtio_net_common_hdr was not meant to be
extended this way; I kept it as the simplest approach, but a
separate struct for the timestamp header is an option.
- Timestamping is enabled at runtime with SIOCSHWTSTAMP; all
supported rx filters behave the same for now.
- XDP programs can read the timestamp with
bpf_xdp_metadata_rx_timestamp().
- vhost-net passes the feature through; the timestamp itself is
written by tun, the device side in software.
- The timestamp is in TAI. On v4 Willem and I discussed whether the
spec should fix the time base at all. Fixing it to TAI is the
simplest: a software device like tun can convert its own clock, and
the guest needs nothing else. The driver reports the timestamp as
SOF_TIMESTAMPING_RAW_HARDWARE without a PTP clock (phc_index -1).
For hardware virtio-net queues, which may not be able to convert,
the model of NIC timestamps and their PHC would fit better: the
device names a clock (e.g. a virtio_rtc clock, whose PHC the driver
reports) or virtio-net gets a PHC of its own. That needs consensus
on the virtio side; Which way would you prefer?
- With phc_index -1, ETHTOOL_MSG_TSCONFIG_GET and _SET fail with
-ENODEV (SET after the config was already applied). Other drivers
with hardware timestamps but no PHC have the same problem
(ngbe/txgbe, dpaa); patch 1 lets tsconfig handle phc_index -1.
Patches 1 to 3 fix existing code and do not depend on the rest:
tsconfig refused devices with hardware timestamps but no PHC, the tx
header was not fully cleared, and the XDP rx hash was read from the
headroom after the XDP program could have overwritten it. They can go
to net on their own if preferred.
A virtio spec change is needed as well and not yet proposed.
Testing: the feature needs a QEMU that knows VIRTIO_NET_F_RX_TSTAMP and
sets the matching vnet header size; see
https://github.com/strumtrar/qemu/tree/v11.0.1/virtio-rx-stamps
Each patch builds with W=1 on arm64 on top of net-next.
Tools: this version was developed with an LLM coding assistant (Claude
Opus 5.5 in Claude Code). From the review of v4 (Willem's and the
Sashiko findings) it wrote patches 1 to 3, the xdp_buff wrapper and
the other changes listed below, and drafts of the commit messages and
of this cover letter.
v4 carried Assisted-by tags; after commit 1e035297ffda ("docs: remove
Assisted-by references") this is said here instead.
Signed-off-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
---
Changes in v5:
- new patch: let tsconfig handle devices without a PHC
- new patch: clear the whole tx header in xmit_skb()
- new patch: copy the XDP rx hash into a wrapper around xdp_buff
before the XDP program runs
- use the same wrapper for the XDP rx timestamp
- rename has_rx_tstamp/hwts_rx_en to rx_tstamp_supported/rx_tstamp_enabled
- add VIRTIO_NET_F_RX_TSTAMP to features[] only, not to features_legacy[]
- vhost: zero the rest of the header also when num_buffers is not written
- timestamp stays TAI, open question for the virtio side in the cover letter
- drop the performance numbers from the cover letter
- drop the Assisted-by tags, tool use is described in the cover letter
- send as PATCH, no longer RFC
- rebase onto net-next/main 20261112
- Link to v4: https://patch.msgid.link/20261026-v6-7-topic-virtio-net-ptp-v4-0-008af03cd43b@pengutronix.de
Changes in v4:
- add static_asserts for header sizes
- rework hdr_len handling
- add support in vhost
- fix/add support for xdp
- Link to v3: https://patch.msgid.link/20260921-v6-7-topic-virtio-net-ptp-v3-0-3350851c4f88@pengutronix.de
Changes in v3:
- rework to use virtio_net_common_hdr instead of adding new struct
Changes in v2:
- rework patches to use flow filter instead of feature flag
- Link to v1: https://lore.kernel.org/r/20231218-v6-7-topic-virtio-net-ptp-v1-0-cac92b2d8532@pengutronix.de
---
Steffen Trumtrar (9):
net: ethtool: tsconfig: allow hardware timestamping without a PHC
virtio-net: zero the whole tx header
virtio-net: copy the XDP rx hash before the program runs
virtio-net: move virtio_net_common_hdr to virtio_net.h
virtio-net: support receive timestamp
virtio-net: assert header sizes
vhost-net: support VIRTIO_NET_F_RX_TSTAMP
virtio-net: add XDP rx timestamp metadata
tun: add rx timestamp to the vnet header
drivers/net/tun.c | 14 ++-
drivers/net/tun_vnet.h | 18 ++-
drivers/net/virtio_net.c | 250 +++++++++++++++++++++++++++++++---------
drivers/vhost/net.c | 21 +++-
include/linux/virtio_net.h | 30 +++++
include/uapi/linux/virtio_net.h | 1 +
net/ethtool/tsconfig.c | 6 +-
7 files changed, 270 insertions(+), 70 deletions(-)
---
base-commit: c88fd6989aea9237a60a4859139a265bfb7ddc0b
change-id: 20231218-v6-7-topic-virtio-net-ptp-3df023bc4f4d
Best regards,
--
Steffen Trumtrar <s.trumtrar@pengutronix.de>
^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH net-next v5 1/9] net: ethtool: tsconfig: allow hardware timestamping without a PHC
2026-11-12 11:19 [PATCH net-next v5 0/9] virtio-net: add receive timestamp support Steffen Trumtrar
@ 2026-11-12 11:19 ` Steffen Trumtrar
2026-11-12 11:19 ` [PATCH net-next v5 2/9] virtio-net: zero the whole tx header Steffen Trumtrar
` (8 subsequent siblings)
9 siblings, 0 replies; 16+ messages in thread
From: Steffen Trumtrar @ 2026-11-12 11:19 UTC (permalink / raw)
To: Michael S. Tsirkin, Xuan Zhuo, David S. Miller, Jakub Kicinski,
Paolo Abeni, Richard Cochran, Willem de Bruijn, Andrew Lunn,
Eugenio Pérez, Jason Wang, Alexei Starovoitov,
Daniel Borkmann, Jesper Dangaard Brouer, John Fastabend,
Stanislav Fomichev, Eric Dumazet, Andrew Lunn, Simon Horman,
Kory Maincent, Liang Chen, Heng Qi
Cc: virtualization, netdev, linux-kernel, bpf, kvm, kernel,
Steffen Trumtrar
A driver can do hardware timestamping without a PTP hardware clock; its
get_ts_info() then reports hardware timestamping with phc_index -1.
ngbe and txgbe (libwx) do so when no PTP clock is registered, e.g. with
CONFIG_PTP_1588_CLOCK=n, and dpaa without a ptimer-handle.
SIOCGHWTSTAMP and SIOCSHWTSTAMP work for such a device, but
tsconfig_prepare_data() refuses it with -ENODEV:
- TSCONFIG_GET fails.
- TSCONFIG_SET passes the new config to ndo_hwtstamp_set() first and
fails only when building the reply, so user space gets an error
although the config was applied.
- In a dump only -EOPNOTSUPP is skipped; the error ends the dump, and
the devices after this one are not listed.
The reply already leaves out ETHTOOL_A_TSCONFIG_HWTSTAMP_PROVIDER when
the index is -1. Let tsconfig_prepare_data() take that path instead of
failing.
Fixes: 6e9e2eed4f39 ("net: ethtool: Add support for tsconfig command to get/set hwtstamp config")
Signed-off-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
---
net/ethtool/tsconfig.c | 6 +-----
1 file changed, 1 insertion(+), 5 deletions(-)
diff --git a/net/ethtool/tsconfig.c b/net/ethtool/tsconfig.c
index 2db0e7ba8b9f5..75f9b319aa310 100644
--- a/net/ethtool/tsconfig.c
+++ b/net/ethtool/tsconfig.c
@@ -73,11 +73,7 @@ static int tsconfig_prepare_data(const struct ethnl_req_info *req_base,
if (ret)
goto out;
- if (ts_info.phc_index == -1) {
- ret = -ENODEV;
- goto out;
- }
-
+ /* -1 without a PHC: the reply leaves out the provider */
data->hwprov_desc.index = ts_info.phc_index;
data->hwprov_desc.qualifier = ts_info.phc_qualifier;
}
--
2.56.0
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH net-next v5 2/9] virtio-net: zero the whole tx header
2026-11-12 11:19 [PATCH net-next v5 0/9] virtio-net: add receive timestamp support Steffen Trumtrar
2026-11-12 11:19 ` [PATCH net-next v5 1/9] net: ethtool: tsconfig: allow hardware timestamping without a PHC Steffen Trumtrar
@ 2026-11-12 11:19 ` Steffen Trumtrar
2026-11-16 13:55 ` netdev-bot+sashiko
2026-11-12 11:19 ` [PATCH net-next v5 3/9] virtio-net: copy the XDP rx hash before the program runs Steffen Trumtrar
` (7 subsequent siblings)
9 siblings, 1 reply; 16+ messages in thread
From: Steffen Trumtrar @ 2026-11-12 11:19 UTC (permalink / raw)
To: Michael S. Tsirkin, Xuan Zhuo, David S. Miller, Jakub Kicinski,
Paolo Abeni, Richard Cochran, Willem de Bruijn, Andrew Lunn,
Eugenio Pérez, Jason Wang, Alexei Starovoitov,
Daniel Borkmann, Jesper Dangaard Brouer, John Fastabend,
Stanislav Fomichev, Eric Dumazet, Andrew Lunn, Simon Horman,
Kory Maincent, Liang Chen, Heng Qi
Cc: virtualization, netdev, linux-kernel, bpf, kvm, kernel,
Steffen Trumtrar
xmit_skb() builds the virtio-net header either in skb->cb or in the
headroom in front of the packet. virtio_net_hdr_tnl_from_skb() only
clears struct virtio_net_hdr (10 bytes) and, for tunnel GSO, the hash
fields; num_buffers is cleared only with mergeable rx buffers. With a
larger negotiated header the remaining bytes go to the device as they
were in skb->cb or the headroom:
- num_buffers with VIRTIO_F_VERSION_1 but without
VIRTIO_NET_F_MRG_RXBUF (header size 12),
- hash_value, hash_report and padding with
VIRTIO_NET_F_HASH_REPORT (header size 20),
- additionally outer_th_offset and inner_nh_offset for packets
without tunnel GSO if a UDP tunnel GSO feature was negotiated
(header size 24).
Clear the whole header before filling it in. This also covers
num_buffers, so drop the separate assignment.
The helpers still clear struct virtio_net_hdr and, for tunnel GSO, the
hash fields themselves; that is now redundant here, but they are
shared with tun, tap, af_packet and the UML vector driver and stay as
they are.
Signed-off-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
---
drivers/net/virtio_net.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index dd2c41ef6fbe7..4c740b403ad1e 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -3326,14 +3326,16 @@ static int xmit_skb(struct send_queue *sq, struct sk_buff *skb, bool orphan)
else
hdr = &skb_vnet_common_hdr(skb)->tnl_hdr;
+ /* Fields the helper below does not fill in must not carry stale
+ * skb->cb or headroom bytes to the device.
+ */
+ memset(hdr, 0, hdr_len);
+
if (virtio_net_hdr_tnl_from_skb(skb, hdr, vi->tx_tnl,
virtio_is_little_endian(vi->vdev), 0,
false, feature_hdrlen))
return -EPROTO;
- if (vi->mergeable_rx_bufs)
- hdr->hash_hdr.hdr.num_buffers = 0;
-
sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
if (can_push) {
__skb_push(skb, hdr_len);
--
2.56.0
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH net-next v5 3/9] virtio-net: copy the XDP rx hash before the program runs
2026-11-12 11:19 [PATCH net-next v5 0/9] virtio-net: add receive timestamp support Steffen Trumtrar
2026-11-12 11:19 ` [PATCH net-next v5 1/9] net: ethtool: tsconfig: allow hardware timestamping without a PHC Steffen Trumtrar
2026-11-12 11:19 ` [PATCH net-next v5 2/9] virtio-net: zero the whole tx header Steffen Trumtrar
@ 2026-11-12 11:19 ` Steffen Trumtrar
2026-11-12 11:19 ` [PATCH net-next v5 4/9] virtio-net: move virtio_net_common_hdr to virtio_net.h Steffen Trumtrar
` (6 subsequent siblings)
9 siblings, 0 replies; 16+ messages in thread
From: Steffen Trumtrar @ 2026-11-12 11:19 UTC (permalink / raw)
To: Michael S. Tsirkin, Xuan Zhuo, David S. Miller, Jakub Kicinski,
Paolo Abeni, Richard Cochran, Willem de Bruijn, Andrew Lunn,
Eugenio Pérez, Jason Wang, Alexei Starovoitov,
Daniel Borkmann, Jesper Dangaard Brouer, John Fastabend,
Stanislav Fomichev, Eric Dumazet, Andrew Lunn, Simon Horman,
Kory Maincent, Liang Chen, Heng Qi
Cc: virtualization, netdev, linux-kernel, bpf, kvm, kernel,
Steffen Trumtrar
virtnet_xdp_rx_hash() reads the hash from the vnet header at
xdp->data - vi->hdr_len when the XDP program calls
bpf_xdp_metadata_rx_hash(). That is only the header until the program
changes the buffer: bpf_xdp_adjust_meta() writes the metadata right in
front of xdp->data, over the header, and bpf_xdp_adjust_head() moves
xdp->data, so the callback then reads metadata or packet bytes.
Wrap struct xdp_buff in struct virtnet_xdp_buff, copy hash value and
report type into it in virtnet_xdp_handler() before
bpf_prog_run_xdp(), and let the callback read the copy. The small and
mergeable paths put the wrapper on the stack; for AF_XDP the fields are
in the cb of struct xdp_buff_xsk, checked with XSK_CHECK_PRIV_TYPE().
Move virtio_net_hash_value() up so the copy can use it.
Fixes: aa37f8916d20 ("virtio_net: Support RX hash XDP hint")
Signed-off-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
---
drivers/net/virtio_net.c | 86 ++++++++++++++++++++++++++++++++----------------
1 file changed, 58 insertions(+), 28 deletions(-)
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index 4c740b403ad1e..ffd7313663c18 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -491,6 +491,26 @@ struct padded_vnet_hdr {
char padding[12];
};
+/*
+ * The vnet header sits in the headroom in front of xdp->data, which the
+ * XDP program may overwrite with bpf_xdp_adjust_head() or
+ * bpf_xdp_adjust_meta(). What the metadata kfuncs report is copied from
+ * it before the program runs. For AF_XDP the fields behind xdp live in
+ * the cb of struct xdp_buff_xsk.
+ */
+struct virtnet_xdp_buff {
+ struct xdp_buff xdp;
+ u32 hash_value;
+ u16 hash_report;
+};
+
+static inline u32
+virtio_net_hash_value(const struct virtio_net_hdr_v1_hash *hdr_hash)
+{
+ return __le16_to_cpu(hdr_hash->hash_value_lo) |
+ (__le16_to_cpu(hdr_hash->hash_value_hi) << 16);
+}
+
struct virtio_net_common_hdr {
union {
struct virtio_net_hdr hdr;
@@ -1687,6 +1707,24 @@ static void put_xdp_frags(struct receive_queue *rq, struct xdp_buff *xdp)
}
}
+/* xdp must be embedded in struct virtnet_xdp_buff or struct xdp_buff_xsk */
+static void virtnet_xdp_save_hdr(const struct virtnet_info *vi,
+ struct xdp_buff *xdp)
+{
+ struct virtnet_xdp_buff *ctx = container_of(xdp, struct virtnet_xdp_buff, xdp);
+ const struct virtio_net_hdr_v1_hash *hdr_hash = xdp->data - vi->hdr_len;
+
+ XSK_CHECK_PRIV_TYPE(struct virtnet_xdp_buff);
+
+ if (vi->has_rss_hash_report) {
+ ctx->hash_value = virtio_net_hash_value(hdr_hash);
+ ctx->hash_report = __le16_to_cpu(hdr_hash->hash_report);
+ } else {
+ ctx->hash_value = 0;
+ ctx->hash_report = VIRTIO_NET_HASH_REPORT_NONE;
+ }
+}
+
static int virtnet_xdp_handler(struct bpf_prog *xdp_prog, struct xdp_buff *xdp,
struct net_device *dev,
unsigned int *xdp_xmit,
@@ -1696,6 +1734,8 @@ static int virtnet_xdp_handler(struct bpf_prog *xdp_prog, struct xdp_buff *xdp,
int err;
u32 act;
+ virtnet_xdp_save_hdr(netdev_priv(dev), xdp);
+
act = bpf_prog_run_xdp(xdp_prog, xdp);
u64_stats_inc(&stats->xdp_packets);
@@ -1867,7 +1907,8 @@ static struct sk_buff *receive_small_xdp(struct net_device *dev,
struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
struct page *page = virt_to_head_page(buf);
struct page *xdp_page;
- struct xdp_buff xdp;
+ struct virtnet_xdp_buff vxdp;
+ struct xdp_buff *xdp = &vxdp.xdp;
struct sk_buff *skb;
unsigned int metasize = 0;
u32 act;
@@ -1900,17 +1941,17 @@ static struct sk_buff *receive_small_xdp(struct net_device *dev,
page = xdp_page;
}
- xdp_init_buff(&xdp, buflen, &rq->xdp_rxq);
- xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len,
+ xdp_init_buff(xdp, buflen, &rq->xdp_rxq);
+ xdp_prepare_buff(xdp, buf + VIRTNET_RX_PAD + vi->hdr_len,
xdp_headroom, len, true);
- act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats);
+ act = virtnet_xdp_handler(xdp_prog, xdp, dev, xdp_xmit, stats);
switch (act) {
case XDP_PASS:
/* Recalculate length in case bpf program changed it */
- len = xdp.data_end - xdp.data;
- metasize = xdp.data - xdp.data_meta;
+ len = xdp->data_end - xdp->data;
+ metasize = xdp->data - xdp->data_meta;
break;
case XDP_TX:
@@ -1921,7 +1962,7 @@ static struct sk_buff *receive_small_xdp(struct net_device *dev,
goto err_xdp;
}
- skb = virtnet_build_skb(buf, buflen, xdp.data - buf, len);
+ skb = virtnet_build_skb(buf, buflen, xdp->data - buf, len);
if (unlikely(!skb)) {
u64_stats_inc(&stats->alloc_fail);
goto err;
@@ -2288,7 +2329,8 @@ static struct sk_buff *receive_mergeable_xdp(struct net_device *dev,
unsigned int xdp_frags_truesz = 0;
struct sk_buff *head_skb;
unsigned int frame_sz;
- struct xdp_buff xdp;
+ struct virtnet_xdp_buff vxdp;
+ struct xdp_buff *xdp = &vxdp.xdp;
void *data;
u32 act;
int err;
@@ -2298,16 +2340,16 @@ static struct sk_buff *receive_mergeable_xdp(struct net_device *dev,
if (unlikely(!data))
goto err_xdp;
- err = virtnet_build_xdp_buff_mrg(dev, vi, rq, &xdp, data, len, frame_sz,
+ err = virtnet_build_xdp_buff_mrg(dev, vi, rq, xdp, data, len, frame_sz,
&num_buf, &xdp_frags_truesz, stats);
if (unlikely(err))
goto err_xdp;
- act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats);
+ act = virtnet_xdp_handler(xdp_prog, xdp, dev, xdp_xmit, stats);
switch (act) {
case XDP_PASS:
- head_skb = build_skb_from_xdp_buff(dev, vi, &xdp, xdp_frags_truesz);
+ head_skb = build_skb_from_xdp_buff(dev, vi, xdp, xdp_frags_truesz);
if (unlikely(!head_skb)) {
u64_stats_inc(&stats->alloc_fail);
break;
@@ -2324,7 +2366,7 @@ static struct sk_buff *receive_mergeable_xdp(struct net_device *dev,
break;
}
- put_xdp_frags(rq, &xdp);
+ put_xdp_frags(rq, xdp);
err_xdp:
page_pool_put_page(rq->page_pool, page, -1, true);
@@ -2477,13 +2519,6 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
return NULL;
}
-static inline u32
-virtio_net_hash_value(const struct virtio_net_hdr_v1_hash *hdr_hash)
-{
- return __le16_to_cpu(hdr_hash->hash_value_lo) |
- (__le16_to_cpu(hdr_hash->hash_value_hi) << 16);
-}
-
static void virtio_skb_set_hash(const struct virtio_net_hdr_v1_hash *hdr_hash,
struct sk_buff *skb)
{
@@ -6760,23 +6795,18 @@ virtnet_xdp_rss_type[VIRTIO_NET_HASH_REPORT_MAX_TABLE] = {
static int virtnet_xdp_rx_hash(const struct xdp_md *_ctx, u32 *hash,
enum xdp_rss_hash_type *rss_type)
{
- const struct xdp_buff *xdp = (void *)_ctx;
- struct virtio_net_hdr_v1_hash *hdr_hash;
- struct virtnet_info *vi;
+ const struct virtnet_xdp_buff *ctx = (void *)_ctx;
u16 hash_report;
- if (!(xdp->rxq->dev->features & NETIF_F_RXHASH))
+ if (!(ctx->xdp.rxq->dev->features & NETIF_F_RXHASH))
return -ENODATA;
- vi = netdev_priv(xdp->rxq->dev);
- hdr_hash = (struct virtio_net_hdr_v1_hash *)(xdp->data - vi->hdr_len);
- hash_report = __le16_to_cpu(hdr_hash->hash_report);
-
+ hash_report = ctx->hash_report;
if (hash_report >= VIRTIO_NET_HASH_REPORT_MAX_TABLE)
hash_report = VIRTIO_NET_HASH_REPORT_NONE;
*rss_type = virtnet_xdp_rss_type[hash_report];
- *hash = virtio_net_hash_value(hdr_hash);
+ *hash = ctx->hash_value;
return 0;
}
--
2.56.0
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH net-next v5 4/9] virtio-net: move virtio_net_common_hdr to virtio_net.h
2026-11-12 11:19 [PATCH net-next v5 0/9] virtio-net: add receive timestamp support Steffen Trumtrar
` (2 preceding siblings ...)
2026-11-12 11:19 ` [PATCH net-next v5 3/9] virtio-net: copy the XDP rx hash before the program runs Steffen Trumtrar
@ 2026-11-12 11:19 ` Steffen Trumtrar
2026-11-12 11:19 ` [PATCH net-next v5 5/9] virtio-net: support receive timestamp Steffen Trumtrar
` (5 subsequent siblings)
9 siblings, 0 replies; 16+ messages in thread
From: Steffen Trumtrar @ 2026-11-12 11:19 UTC (permalink / raw)
To: Michael S. Tsirkin, Xuan Zhuo, David S. Miller, Jakub Kicinski,
Paolo Abeni, Richard Cochran, Willem de Bruijn, Andrew Lunn,
Eugenio Pérez, Jason Wang, Alexei Starovoitov,
Daniel Borkmann, Jesper Dangaard Brouer, John Fastabend,
Stanislav Fomichev, Eric Dumazet, Andrew Lunn, Simon Horman,
Kory Maincent, Liang Chen, Heng Qi
Cc: virtualization, netdev, linux-kernel, bpf, kvm, kernel,
Steffen Trumtrar
tun and vhost need to know the layout and size of the virtio-net
header as the driver uses it, e.g. to fill in fields behind
virtio_net_hdr_v1_hash_tunnel. Move struct virtio_net_common_hdr to
the kernel-internal header that they include already.
No functional change.
Signed-off-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
---
drivers/net/virtio_net.c | 9 ---------
include/linux/virtio_net.h | 9 +++++++++
2 files changed, 9 insertions(+), 9 deletions(-)
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index ffd7313663c18..23e7f62a6ff64 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -511,15 +511,6 @@ virtio_net_hash_value(const struct virtio_net_hdr_v1_hash *hdr_hash)
(__le16_to_cpu(hdr_hash->hash_value_hi) << 16);
}
-struct virtio_net_common_hdr {
- union {
- struct virtio_net_hdr hdr;
- struct virtio_net_hdr_mrg_rxbuf mrg_hdr;
- struct virtio_net_hdr_v1_hash hash_v1_hdr;
- struct virtio_net_hdr_v1_hash_tunnel tnl_hdr;
- };
-};
-
static struct virtio_net_common_hdr xsk_hdr;
static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf);
diff --git a/include/linux/virtio_net.h b/include/linux/virtio_net.h
index d6466f96cdd00..a6de308b8f25e 100644
--- a/include/linux/virtio_net.h
+++ b/include/linux/virtio_net.h
@@ -10,6 +10,15 @@
#include <uapi/linux/tcp.h>
#include <uapi/linux/virtio_net.h>
+struct virtio_net_common_hdr {
+ union {
+ struct virtio_net_hdr hdr;
+ struct virtio_net_hdr_mrg_rxbuf mrg_hdr;
+ struct virtio_net_hdr_v1_hash hash_v1_hdr;
+ struct virtio_net_hdr_v1_hash_tunnel tnl_hdr;
+ };
+};
+
static inline bool virtio_net_hdr_match_proto(__be16 protocol, __u8 gso_type)
{
switch (gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
--
2.56.0
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH net-next v5 5/9] virtio-net: support receive timestamp
2026-11-12 11:19 [PATCH net-next v5 0/9] virtio-net: add receive timestamp support Steffen Trumtrar
` (3 preceding siblings ...)
2026-11-12 11:19 ` [PATCH net-next v5 4/9] virtio-net: move virtio_net_common_hdr to virtio_net.h Steffen Trumtrar
@ 2026-11-12 11:19 ` Steffen Trumtrar
2026-11-16 13:55 ` netdev-bot+sashiko
2026-11-12 11:19 ` [PATCH net-next v5 6/9] virtio-net: assert header sizes Steffen Trumtrar
` (4 subsequent siblings)
9 siblings, 1 reply; 16+ messages in thread
From: Steffen Trumtrar @ 2026-11-12 11:19 UTC (permalink / raw)
To: Michael S. Tsirkin, Xuan Zhuo, David S. Miller, Jakub Kicinski,
Paolo Abeni, Richard Cochran, Willem de Bruijn, Andrew Lunn,
Eugenio Pérez, Jason Wang, Alexei Starovoitov,
Daniel Borkmann, Jesper Dangaard Brouer, John Fastabend,
Stanislav Fomichev, Eric Dumazet, Andrew Lunn, Simon Horman,
Kory Maincent, Liang Chen, Heng Qi
Cc: virtualization, netdev, linux-kernel, bpf, kvm, kernel,
Steffen Trumtrar
Add optional hardware rx timestamp offload for virtio-net.
Introduce the virtio feature VIRTIO_NET_F_RX_TSTAMP. If negotiated, the
virtio-net header grows by room for a 64 bit TAI timestamp (header
size 40 bytes), stored in struct virtio_net_common_hdr. The layout is
cumulative: the field is at the same offset whether or not the
features before it were negotiated. The fixed layout needs
VIRTIO_F_VERSION_1, so the feature is cleared without it.
Implement ndo_hwtstamp_get/set so the timestamping can be enabled
and disabled at runtime with SIOCSHWTSTAMP. The driver cannot filter
what the device timestamps, so any rx filter other than NONE becomes
ALL. The timestamp is read before the packet is handed to the XDP
program or the stack, and attached to the skb with skb_hwtstamps().
It is a raw TAI value; there is no PHC.
The header size is negotiated per device and also applies to
transmitted packets. xmit_skb() clears the whole header, so the device
sees a zero tstamp.
On receive in big packet mode, the header is placed in front of the
data in the first page. Grow padded_vnet_hdr to 48 bytes so the
40 byte header still fits there.
Tested in a guest with a patched QEMU:
guest: ./timestamping eth0 \
SOF_TIMESTAMPING_RAW_HARDWARE \
SOF_TIMESTAMPING_RX_HARDWARE
host: nc -4 -u 192.168.1.1 319
Based on work by Willem de Bruijn.
Signed-off-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
---
drivers/net/virtio_net.c | 131 +++++++++++++++++++++++++++++++++++-----
include/linux/virtio_net.h | 10 +++
include/uapi/linux/virtio_net.h | 1 +
3 files changed, 127 insertions(+), 15 deletions(-)
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index 23e7f62a6ff64..57b7c32625dfb 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -20,6 +20,7 @@
#include <linux/filter.h>
#include <linux/kernel.h>
#include <linux/dim.h>
+#include <linux/unaligned.h>
#include <net/route.h>
#include <net/xdp.h>
#include <net/net_failover.h>
@@ -418,6 +419,12 @@ struct virtnet_info {
u32 rss_hash_types_supported;
u32 rss_hash_types_saved;
+ /* Device passes time stamps to the driver */
+ bool rx_tstamp_supported;
+ bool rx_tstamp_enabled;
+
+ struct kernel_hwtstamp_config tstamp_config;
+
/* Has control virtqueue */
bool has_cvq;
@@ -482,15 +489,17 @@ static_assert(offsetof(struct virtnet_info, rss_trailer.hash_key_data) ==
offsetof(struct virtnet_info, rss_hash_key_data));
struct padded_vnet_hdr {
- struct virtio_net_hdr_v1_hash hdr;
+ struct virtio_net_common_hdr hdr;
/*
* hdr is in a separate sg buffer, and data sg buffer shares same page
* with this header sg. This padding makes next sg 16 byte aligned
* after the header.
*/
- char padding[12];
+ char padding[8];
};
+static_assert(sizeof(struct padded_vnet_hdr) % 16 == 0);
+
/*
* The vnet header sits in the headroom in front of xdp->data, which the
* XDP program may overwrite with bpf_xdp_adjust_head() or
@@ -520,7 +529,7 @@ static int virtnet_xdp_handler(struct bpf_prog *xdp_prog, struct xdp_buff *xdp,
unsigned int *xdp_xmit,
struct virtnet_rq_stats *stats);
static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue *rq,
- struct sk_buff *skb, u8 flags);
+ struct sk_buff *skb, u8 flags, ktime_t tstamp);
static struct sk_buff *virtnet_skb_append_frag(struct receive_queue *rq,
struct sk_buff *head_skb,
struct sk_buff *curr_skb,
@@ -1299,14 +1308,25 @@ static struct sk_buff *virtnet_receive_xsk_merge(struct net_device *dev, struct
return NULL;
}
+static inline ktime_t virtnet_record_rx_tstamp(const struct virtnet_info *vi,
+ const struct virtio_net_common_hdr *hdr)
+{
+ if (!READ_ONCE(vi->rx_tstamp_enabled))
+ return 0;
+
+ return ns_to_ktime(get_unaligned_le64(hdr->tstamp));
+}
+
static void virtnet_receive_xsk_buf(struct virtnet_info *vi, struct receive_queue *rq,
void *buf, u32 len,
unsigned int *xdp_xmit,
struct virtnet_rq_stats *stats)
{
struct net_device *dev = vi->dev;
+ struct virtio_net_common_hdr *hdr;
struct sk_buff *skb = NULL;
struct xdp_buff *xdp;
+ ktime_t tstamp;
u8 flags;
len -= vi->hdr_len;
@@ -1324,7 +1344,9 @@ static void virtnet_receive_xsk_buf(struct virtnet_info *vi, struct receive_queu
return;
}
- flags = ((struct virtio_net_common_hdr *)(xdp->data - vi->hdr_len))->hdr.flags;
+ hdr = (struct virtio_net_common_hdr *)(xdp->data - vi->hdr_len);
+ flags = hdr->hdr.flags;
+ tstamp = virtnet_record_rx_tstamp(vi, hdr);
if (!vi->mergeable_rx_bufs)
skb = virtnet_receive_xsk_small(dev, vi, rq, xdp, xdp_xmit, stats);
@@ -1332,7 +1354,7 @@ static void virtnet_receive_xsk_buf(struct virtnet_info *vi, struct receive_queu
skb = virtnet_receive_xsk_merge(dev, vi, rq, xdp, xdp_xmit, stats);
if (skb)
- virtnet_receive_done(vi, rq, skb, flags);
+ virtnet_receive_done(vi, rq, skb, flags, tstamp);
}
static int virtnet_add_recvbuf_xsk(struct virtnet_info *vi, struct receive_queue *rq,
@@ -2540,7 +2562,7 @@ static void virtio_skb_set_hash(const struct virtio_net_hdr_v1_hash *hdr_hash,
}
static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue *rq,
- struct sk_buff *skb, u8 flags)
+ struct sk_buff *skb, u8 flags, ktime_t tstamp)
{
struct virtio_net_common_hdr *hdr;
struct net_device *dev = vi->dev;
@@ -2549,6 +2571,9 @@ static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue *
if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report)
virtio_skb_set_hash(&hdr->hash_v1_hdr, skb);
+ if (tstamp)
+ skb_hwtstamps(skb)->hwtstamp = tstamp;
+
hdr->hdr.flags = flags;
if (virtio_net_handle_csum_offload(skb, &hdr->hdr, vi->rx_tnl_csum)) {
net_warn_ratelimited("%s: bad csum: flags: %x, gso_type: %x rx_tnl_csum %d\n",
@@ -2587,8 +2612,10 @@ static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
unsigned int *xdp_xmit,
struct virtnet_rq_stats *stats)
{
+ struct virtio_net_common_hdr *hdr;
struct net_device *dev = vi->dev;
struct sk_buff *skb;
+ ktime_t tstamp;
u8 flags;
if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
@@ -2618,23 +2645,27 @@ static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
*/
if (vi->mergeable_rx_bufs) {
- flags = ((struct virtio_net_common_hdr *)buf)->hdr.flags;
+ hdr = buf;
+ flags = hdr->hdr.flags;
+ tstamp = virtnet_record_rx_tstamp(vi, hdr);
skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
stats);
} else if (vi->big_packets) {
- void *p = page_address((struct page *)buf);
-
- flags = ((struct virtio_net_common_hdr *)p)->hdr.flags;
+ hdr = page_address((struct page *)buf);
+ flags = hdr->hdr.flags;
+ tstamp = virtnet_record_rx_tstamp(vi, hdr);
skb = receive_big(dev, vi, rq, buf, len, stats);
} else {
- flags = ((struct virtio_net_common_hdr *)buf)->hdr.flags;
+ hdr = buf;
+ flags = hdr->hdr.flags;
+ tstamp = virtnet_record_rx_tstamp(vi, hdr);
skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
}
if (unlikely(!skb))
return;
- virtnet_receive_done(vi, rq, skb, flags);
+ virtnet_receive_done(vi, rq, skb, flags, tstamp);
}
static int virtnet_rq_submit(struct receive_queue *rq, char *buf,
@@ -5575,6 +5606,27 @@ static int virtnet_get_per_queue_coalesce(struct net_device *dev,
return 0;
}
+static int virtnet_get_ts_info(struct net_device *dev,
+ struct kernel_ethtool_ts_info *info)
+{
+ struct virtnet_info *vi = netdev_priv(dev);
+
+ /* setup default software timestamp */
+ ethtool_op_get_ts_info(dev, info);
+
+ if (vi->rx_tstamp_supported) {
+ info->so_timestamping |=
+ SOF_TIMESTAMPING_RX_HARDWARE |
+ SOF_TIMESTAMPING_RAW_HARDWARE;
+
+ info->tx_types = BIT(HWTSTAMP_TX_OFF);
+ info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
+ BIT(HWTSTAMP_FILTER_ALL);
+ }
+
+ return 0;
+}
+
static void virtnet_init_settings(struct net_device *dev)
{
struct virtnet_info *vi = netdev_priv(dev);
@@ -5670,7 +5722,7 @@ static const struct ethtool_ops virtnet_ethtool_ops = {
.get_ethtool_stats = virtnet_get_ethtool_stats,
.set_channels = virtnet_set_channels,
.get_channels = virtnet_get_channels,
- .get_ts_info = ethtool_op_get_ts_info,
+ .get_ts_info = virtnet_get_ts_info,
.get_link_ksettings = virtnet_get_link_ksettings,
.set_link_ksettings = virtnet_set_link_ksettings,
.set_coalesce = virtnet_set_coalesce,
@@ -6269,6 +6321,43 @@ static void virtnet_tx_timeout(struct net_device *dev, unsigned int txqueue)
jiffies_to_usecs(jiffies - READ_ONCE(txq->trans_start)));
}
+static int virtnet_hwtstamp_get(struct net_device *dev,
+ struct kernel_hwtstamp_config *config)
+{
+ struct virtnet_info *vi = netdev_priv(dev);
+
+ if (!vi->rx_tstamp_supported)
+ return -EOPNOTSUPP;
+
+ *config = vi->tstamp_config;
+
+ return 0;
+}
+
+static int virtnet_hwtstamp_set(struct net_device *dev,
+ struct kernel_hwtstamp_config *config,
+ struct netlink_ext_ack *extack)
+{
+ struct virtnet_info *vi = netdev_priv(dev);
+
+ if (!vi->rx_tstamp_supported)
+ return -EOPNOTSUPP;
+
+ if (config->tx_type != HWTSTAMP_TX_OFF) {
+ NL_SET_ERR_MSG_MOD(extack, "TX timestamping is not supported");
+ return -ERANGE;
+ }
+
+ /* The driver cannot filter, any filter becomes ALL */
+ if (config->rx_filter != HWTSTAMP_FILTER_NONE)
+ config->rx_filter = HWTSTAMP_FILTER_ALL;
+
+ WRITE_ONCE(vi->rx_tstamp_enabled, config->rx_filter == HWTSTAMP_FILTER_ALL);
+ vi->tstamp_config = *config;
+
+ return 0;
+}
+
static int virtnet_init_irq_moder(struct virtnet_info *vi)
{
u8 profile_flags = 0, coal_flags = 0;
@@ -6329,6 +6418,8 @@ static const struct net_device_ops virtnet_netdev = {
.ndo_get_phys_port_name = virtnet_get_phys_port_name,
.ndo_set_features = virtnet_set_features,
.ndo_tx_timeout = virtnet_tx_timeout,
+ .ndo_hwtstamp_set = virtnet_hwtstamp_set,
+ .ndo_hwtstamp_get = virtnet_hwtstamp_get,
};
static void virtnet_config_changed_work(struct work_struct *work)
@@ -6740,6 +6831,10 @@ static int virtnet_validate(struct virtio_device *vdev)
__virtio_clear_bit(vdev, VIRTIO_NET_F_STANDBY);
}
+ /* The fixed header layout with tstamp at offset 32 needs VERSION_1 */
+ if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
+ __virtio_clear_bit(vdev, VIRTIO_NET_F_RX_TSTAMP);
+
return 0;
}
@@ -6932,6 +7027,9 @@ static int virtnet_probe(struct virtio_device *vdev)
if (virtio_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT))
vi->has_rss_hash_report = true;
+ if (virtio_has_feature(vdev, VIRTIO_NET_F_RX_TSTAMP))
+ vi->rx_tstamp_supported = true;
+
if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS)) {
vi->has_rss = true;
@@ -6965,8 +7063,10 @@ static int virtnet_probe(struct virtio_device *vdev)
dev->xdp_metadata_ops = &virtnet_xdp_metadata_ops;
}
- if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO) ||
- virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO))
+ 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) ||
+ virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO))
vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash_tunnel);
else if (vi->has_rss_hash_report)
vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash);
@@ -7309,6 +7409,7 @@ static unsigned int features[] = {
VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO_CSUM,
VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO,
VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO_CSUM,
+ VIRTIO_NET_F_RX_TSTAMP,
};
static unsigned int features_legacy[] = {
diff --git a/include/linux/virtio_net.h b/include/linux/virtio_net.h
index a6de308b8f25e..2abd8a614a23f 100644
--- a/include/linux/virtio_net.h
+++ b/include/linux/virtio_net.h
@@ -17,6 +17,16 @@ struct virtio_net_common_hdr {
struct virtio_net_hdr_v1_hash hash_v1_hdr;
struct virtio_net_hdr_v1_hash_tunnel tnl_hdr;
};
+ /*
+ * The wire layout is fixed and cumulative: a field sits at the same
+ * offset whether or not the features before it were negotiated; unused
+ * bytes are padding. tstamp is thus always at offset 32 and only
+ * present (header size 40) if VIRTIO_NET_F_RX_TSTAMP was negotiated.
+ * The negotiated header size tells how much of this struct is
+ * on the wire. Only ever append new fields at the end.
+ */
+ __u8 reserved[8]; /* outer_nh_offset, ipsec_resource_hdr (not implemented) */
+ __le16 tstamp[4]; /* le64 TAI receive time in ns, 0 if none */
};
static inline bool virtio_net_hdr_match_proto(__be16 protocol, __u8 gso_type)
diff --git a/include/uapi/linux/virtio_net.h b/include/uapi/linux/virtio_net.h
index 1db45b01532b5..15e8e408d50fb 100644
--- a/include/uapi/linux/virtio_net.h
+++ b/include/uapi/linux/virtio_net.h
@@ -56,6 +56,7 @@
#define VIRTIO_NET_F_MQ 22 /* Device supports Receive Flow
* Steering */
#define VIRTIO_NET_F_CTRL_MAC_ADDR 23 /* Set MAC address */
+#define VIRTIO_NET_F_RX_TSTAMP 49 /* Device sends TAI receive time */
#define VIRTIO_NET_F_DEVICE_STATS 50 /* Device can provide device-level statistics. */
#define VIRTIO_NET_F_VQ_NOTF_COAL 52 /* Device supports virtqueue notification coalescing */
#define VIRTIO_NET_F_NOTF_COAL 53 /* Device supports notifications coalescing */
--
2.56.0
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH net-next v5 6/9] virtio-net: assert header sizes
2026-11-12 11:19 [PATCH net-next v5 0/9] virtio-net: add receive timestamp support Steffen Trumtrar
` (4 preceding siblings ...)
2026-11-12 11:19 ` [PATCH net-next v5 5/9] virtio-net: support receive timestamp Steffen Trumtrar
@ 2026-11-12 11:19 ` Steffen Trumtrar
2026-11-12 11:19 ` [PATCH net-next v5 7/9] vhost-net: support VIRTIO_NET_F_RX_TSTAMP Steffen Trumtrar
` (3 subsequent siblings)
9 siblings, 0 replies; 16+ messages in thread
From: Steffen Trumtrar @ 2026-11-12 11:19 UTC (permalink / raw)
To: Michael S. Tsirkin, Xuan Zhuo, David S. Miller, Jakub Kicinski,
Paolo Abeni, Richard Cochran, Willem de Bruijn, Andrew Lunn,
Eugenio Pérez, Jason Wang, Alexei Starovoitov,
Daniel Borkmann, Jesper Dangaard Brouer, John Fastabend,
Stanislav Fomichev, Eric Dumazet, Andrew Lunn, Simon Horman,
Kory Maincent, Liang Chen, Heng Qi
Cc: virtualization, netdev, linux-kernel, bpf, kvm, kernel,
Steffen Trumtrar
The header is cast from skb->cb and from device buffers, so its layout
must not change silently. Add static_asserts for the offset of tstamp,
the total size, the alignment, and that the header fits into skb->cb.
Signed-off-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
---
include/linux/virtio_net.h | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/include/linux/virtio_net.h b/include/linux/virtio_net.h
index 2abd8a614a23f..e7776a37a486b 100644
--- a/include/linux/virtio_net.h
+++ b/include/linux/virtio_net.h
@@ -29,6 +29,17 @@ struct virtio_net_common_hdr {
__le16 tstamp[4]; /* le64 TAI receive time in ns, 0 if none */
};
+/*
+ * The header is cast from skb->cb and from device buffers, the layout must not
+ * change silently.
+ */
+static_assert(offsetof(struct virtio_net_common_hdr, tstamp) == 32);
+static_assert(sizeof(struct virtio_net_common_hdr) ==
+ offsetofend(struct virtio_net_common_hdr, tstamp));
+static_assert(__alignof__(struct virtio_net_common_hdr) == 2);
+/* virtio_net_common_hdr must fit into skb->cb */
+static_assert(sizeof(struct virtio_net_common_hdr) <= sizeof_field(struct sk_buff, cb));
+
static inline bool virtio_net_hdr_match_proto(__be16 protocol, __u8 gso_type)
{
switch (gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
--
2.56.0
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH net-next v5 7/9] vhost-net: support VIRTIO_NET_F_RX_TSTAMP
2026-11-12 11:19 [PATCH net-next v5 0/9] virtio-net: add receive timestamp support Steffen Trumtrar
` (5 preceding siblings ...)
2026-11-12 11:19 ` [PATCH net-next v5 6/9] virtio-net: assert header sizes Steffen Trumtrar
@ 2026-11-12 11:19 ` Steffen Trumtrar
2026-11-16 13:55 ` netdev-bot+sashiko
2026-11-12 11:19 ` [PATCH net-next v5 8/9] virtio-net: add XDP rx timestamp metadata Steffen Trumtrar
` (2 subsequent siblings)
9 siblings, 1 reply; 16+ messages in thread
From: Steffen Trumtrar @ 2026-11-12 11:19 UTC (permalink / raw)
To: Michael S. Tsirkin, Xuan Zhuo, David S. Miller, Jakub Kicinski,
Paolo Abeni, Richard Cochran, Willem de Bruijn, Andrew Lunn,
Eugenio Pérez, Jason Wang, Alexei Starovoitov,
Daniel Borkmann, Jesper Dangaard Brouer, John Fastabend,
Stanislav Fomichev, Eric Dumazet, Andrew Lunn, Simon Horman,
Kory Maincent, Liang Chen, Heng Qi
Cc: virtualization, netdev, linux-kernel, bpf, kvm, kernel,
Steffen Trumtrar
Advertise VIRTIO_NET_F_RX_TSTAMP to userspace and size the vnet header
for it, so a guest can negotiate the feature with a vhost-net backed
tap device.
The header is 40 bytes if the feature is negotiated. Whoever writes
the header (tun, or vhost itself with VHOST_NET_F_VIRTIO_NET_HDR) has
to fill in the timestamp; vhost only reserves the room. If vhost
writes the header, it zeroes everything behind the fields it fills in
(num_buffers only with VIRTIO_F_VERSION_1 or VIRTIO_NET_F_MRG_RXBUF),
so the timestamp is 0, which the guest ignores.
Signed-off-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
---
drivers/vhost/net.c | 21 ++++++++++++++++++++-
1 file changed, 20 insertions(+), 1 deletion(-)
diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c
index 435d8ed3851f3..1b171af105a22 100644
--- a/drivers/vhost/net.c
+++ b/drivers/vhost/net.c
@@ -77,7 +77,8 @@ static const int vhost_net_bits[] = {
VIRTIO_F_RING_RESET,
VIRTIO_F_IN_ORDER,
VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO,
- VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO
+ VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO,
+ VIRTIO_NET_F_RX_TSTAMP
};
enum {
@@ -1166,6 +1167,7 @@ static void handle_rx(struct vhost_net *net)
s16 headcount;
size_t vhost_hlen, sock_hlen;
size_t vhost_len, sock_len;
+ size_t hdr_filled;
bool busyloop_intr = false;
bool set_num_buffers;
struct socket *sock;
@@ -1193,6 +1195,9 @@ static void handle_rx(struct vhost_net *net)
mergeable = vhost_has_feature(vq, VIRTIO_NET_F_MRG_RXBUF);
set_num_buffers = mergeable ||
vhost_has_feature(vq, VIRTIO_F_VERSION_1);
+ /* Header bytes written below, if vhost supplies the header */
+ hdr_filled = sizeof(hdr) +
+ (set_num_buffers ? sizeof(num_buffers) : 0);
do {
sock_len = vhost_net_rx_peek_head_len(net, sock->sk,
@@ -1278,6 +1283,16 @@ static void handle_rx(struct vhost_net *net)
vhost_discard_vq_desc(vq, headcount, ndesc);
goto out;
}
+ /* Zero the rest of a header supplied by vhost, e.g. tstamp */
+ if (unlikely(vhost_hlen > hdr_filled)) {
+ size_t rest = vhost_hlen - hdr_filled;
+
+ if (iov_iter_zero(rest, &fixup) != rest) {
+ vq_err(vq, "Failed vnet_hdr zeroing");
+ vhost_discard_vq_desc(vq, headcount, ndesc);
+ goto out;
+ }
+ }
nvq->done_idx += headcount;
count += in_order ? 1 : headcount;
if (nvq->done_idx > VHOST_NET_BATCH) {
@@ -1684,6 +1699,10 @@ static int vhost_net_set_features(struct vhost_net *n, const u64 *features)
VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO))
hdr_len = sizeof(struct virtio_net_hdr_v1_hash_tunnel);
+ /* The header layout is cumulative, tstamp sits behind all other fields. */
+ if (virtio_features_test_bit(features, VIRTIO_NET_F_RX_TSTAMP))
+ hdr_len = offsetofend(struct virtio_net_common_hdr, tstamp);
+
if (virtio_features_test_bit(features, VHOST_NET_F_VIRTIO_NET_HDR)) {
/* vhost provides vnet_hdr */
vhost_hlen = hdr_len;
--
2.56.0
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH net-next v5 8/9] virtio-net: add XDP rx timestamp metadata
2026-11-12 11:19 [PATCH net-next v5 0/9] virtio-net: add receive timestamp support Steffen Trumtrar
` (6 preceding siblings ...)
2026-11-12 11:19 ` [PATCH net-next v5 7/9] vhost-net: support VIRTIO_NET_F_RX_TSTAMP Steffen Trumtrar
@ 2026-11-12 11:19 ` Steffen Trumtrar
2026-11-16 13:55 ` netdev-bot+sashiko
2026-11-12 11:19 ` [PATCH net-next v5 9/9] tun: add rx timestamp to the vnet header Steffen Trumtrar
2026-11-12 11:26 ` [PATCH net-next v5 0/9] virtio-net: add receive timestamp support netdev-bot+sinfo
9 siblings, 1 reply; 16+ messages in thread
From: Steffen Trumtrar @ 2026-11-12 11:19 UTC (permalink / raw)
To: Michael S. Tsirkin, Xuan Zhuo, David S. Miller, Jakub Kicinski,
Paolo Abeni, Richard Cochran, Willem de Bruijn, Andrew Lunn,
Eugenio Pérez, Jason Wang, Alexei Starovoitov,
Daniel Borkmann, Jesper Dangaard Brouer, John Fastabend,
Stanislav Fomichev, Eric Dumazet, Andrew Lunn, Simon Horman,
Kory Maincent, Liang Chen, Heng Qi
Cc: virtualization, netdev, linux-kernel, bpf, kvm, kernel,
Steffen Trumtrar
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@pengutronix.de>
---
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
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH net-next v5 9/9] tun: add rx timestamp to the vnet header
2026-11-12 11:19 [PATCH net-next v5 0/9] virtio-net: add receive timestamp support Steffen Trumtrar
` (7 preceding siblings ...)
2026-11-12 11:19 ` [PATCH net-next v5 8/9] virtio-net: add XDP rx timestamp metadata Steffen Trumtrar
@ 2026-11-12 11:19 ` Steffen Trumtrar
2026-11-16 13:55 ` netdev-bot+sashiko
2026-11-12 11:26 ` [PATCH net-next v5 0/9] virtio-net: add receive timestamp support netdev-bot+sinfo
9 siblings, 1 reply; 16+ messages in thread
From: Steffen Trumtrar @ 2026-11-12 11:19 UTC (permalink / raw)
To: Michael S. Tsirkin, Xuan Zhuo, David S. Miller, Jakub Kicinski,
Paolo Abeni, Richard Cochran, Willem de Bruijn, Andrew Lunn,
Eugenio Pérez, Jason Wang, Alexei Starovoitov,
Daniel Borkmann, Jesper Dangaard Brouer, John Fastabend,
Stanislav Fomichev, Eric Dumazet, Andrew Lunn, Simon Horman,
Kory Maincent, Liang Chen, Heng Qi
Cc: virtualization, netdev, linux-kernel, bpf, kvm, kernel,
Steffen Trumtrar
Reference implementation of the device side of VIRTIO_NET_F_RX_TSTAMP: if
the configured vnet_hdr_sz covers the tstamp field, write the current
TAI time into the header when a packet is handed to userspace or vhost.
__tun_vnet_hdr_put() now takes the full struct virtio_net_common_hdr
and copies as much of it as vnet_hdr_sz holds.
This is not intended to be merged. A full test needs a QEMU that knows
the feature and sets the matching vnet_hdr_sz in
virtio_net_set_mrg_rx_bufs(); see
https://github.com/strumtrar/qemu/tree/v11.0.1/virtio-rx-stamps
Signed-off-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
---
drivers/net/tun.c | 14 +++++++++-----
drivers/net/tun_vnet.h | 18 ++++++++++++------
2 files changed, 21 insertions(+), 11 deletions(-)
diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index 8920c797acfd7..1a8c3377753d7 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -57,6 +57,7 @@
#include <linux/math.h>
#include <linux/nsproxy.h>
#include <linux/virtio_net.h>
+#include <linux/unaligned.h>
#include <linux/rcupdate.h>
#include <net/net_namespace.h>
#include <net/netns/generic.h>
@@ -2168,12 +2169,16 @@ static ssize_t tun_put_user(struct tun_struct *tun,
}
if (vnet_hdr_sz) {
- struct virtio_net_hdr_v1_hash_tunnel hdr;
- struct virtio_net_hdr *gso;
+ struct virtio_net_common_hdr hdr;
memset(&hdr, 0, sizeof(hdr));
+
+ /* The configured header covers the tstamp field */
+ if (vnet_hdr_sz >= offsetofend(struct virtio_net_common_hdr, tstamp))
+ put_unaligned_le64(ktime_get_clocktai_ns(), hdr.tstamp);
+
ret = tun_vnet_hdr_tnl_from_skb(tun->flags, tun->dev, skb,
- &hdr);
+ &hdr.tnl_hdr);
if (ret)
return ret;
@@ -2181,9 +2186,8 @@ static ssize_t tun_put_user(struct tun_struct *tun,
* Drop the packet if the configured header size is too small
* WRT the enabled offloads.
*/
- gso = (struct virtio_net_hdr *)&hdr;
ret = __tun_vnet_hdr_put(vnet_hdr_sz, tun->dev->features,
- iter, gso);
+ iter, &hdr);
if (ret)
return ret;
}
diff --git a/drivers/net/tun_vnet.h b/drivers/net/tun_vnet.h
index f4c652b1fa44d..d4cc56dac98d6 100644
--- a/drivers/net/tun_vnet.h
+++ b/drivers/net/tun_vnet.h
@@ -153,19 +153,21 @@ static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
return __tun_vnet_hdr_get(sz, flags, 0, from, hdr);
}
+/* hdr is fully initialized, copy as much of it as the configured size holds */
static inline int __tun_vnet_hdr_put(int sz, netdev_features_t features,
struct iov_iter *iter,
- const struct virtio_net_hdr *hdr)
+ const struct virtio_net_common_hdr *hdr)
{
- unsigned int parsed_size = tun_vnet_parse_size(features);
+ unsigned int put_size = min_t(unsigned int, sizeof(*hdr), sz);
- if (unlikely(iov_iter_count(iter) < sz))
+ if (unlikely(iov_iter_count(iter) < sz ||
+ sz < tun_vnet_parse_size(features)))
return -EINVAL;
- if (unlikely(copy_to_iter(hdr, parsed_size, iter) != parsed_size))
+ if (unlikely(copy_to_iter(hdr, put_size, iter) != put_size))
return -EFAULT;
- if (iov_iter_zero(sz - parsed_size, iter) != sz - parsed_size)
+ if (iov_iter_zero(sz - put_size, iter) != sz - put_size)
return -EFAULT;
return 0;
@@ -174,7 +176,11 @@ static inline int __tun_vnet_hdr_put(int sz, netdev_features_t features,
static inline int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
const struct virtio_net_hdr *hdr)
{
- return __tun_vnet_hdr_put(sz, 0, iter, hdr);
+ struct virtio_net_common_hdr common_hdr = {};
+
+ common_hdr.hdr = *hdr;
+
+ return __tun_vnet_hdr_put(sz, 0, iter, &common_hdr);
}
static inline int tun_vnet_hdr_to_skb(unsigned int flags, struct sk_buff *skb,
--
2.56.0
^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [PATCH net-next v5 0/9] virtio-net: add receive timestamp support
2026-11-12 11:19 [PATCH net-next v5 0/9] virtio-net: add receive timestamp support Steffen Trumtrar
` (8 preceding siblings ...)
2026-11-12 11:19 ` [PATCH net-next v5 9/9] tun: add rx timestamp to the vnet header Steffen Trumtrar
@ 2026-11-12 11:26 ` netdev-bot+sinfo
9 siblings, 0 replies; 16+ messages in thread
From: netdev-bot+sinfo @ 2026-11-12 11:26 UTC (permalink / raw)
To: Steffen Trumtrar
Cc: Michael S. Tsirkin, Xuan Zhuo, David S. Miller, Jakub Kicinski,
Paolo Abeni, Richard Cochran, Willem de Bruijn, Andrew Lunn,
Eugenio Pérez, Jason Wang, Alexei Starovoitov,
Daniel Borkmann, Jesper Dangaard Brouer, John Fastabend,
Stanislav Fomichev, Eric Dumazet, Andrew Lunn, Simon Horman,
Kory Maincent, Liang Chen, Heng Qi, virtualization, netdev,
linux-kernel, bpf, kvm, kernel
Hi!
This is an automated message. This series looks like a fix, but its
commit messages seem to be missing some information:
- Whether the issue was actually triggered, or is only theoretical
(e.g. found by LLM and not proven at runtime). If it was triggered
please include the symptoms, like the stack trace or kernel messages.
Please do not repost the series just to address the above. Instead,
reply to this email with the missing information, so that reviewers
can take it into account. If the series needs another revision for
other reasons, please include the information in the commit messages
then.
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH net-next v5 2/9] virtio-net: zero the whole tx header
2026-11-12 11:19 ` [PATCH net-next v5 2/9] virtio-net: zero the whole tx header Steffen Trumtrar
@ 2026-11-16 13:55 ` netdev-bot+sashiko
0 siblings, 0 replies; 16+ messages in thread
From: netdev-bot+sashiko @ 2026-11-16 13:55 UTC (permalink / raw)
To: s.trumtrar
Cc: mst, xuanzhuo, davem, kuba, pabeni, richardcochran,
willemdebruijn.kernel, andrew+netdev, eperezma, jasowangio, ast,
daniel, hawk, john.fastabend, sdf, edumazet, andrew, horms,
kory.maincent, liangchen.linux, hengqi, virtualization, netdev,
linux-kernel, bpf, kvm, kernel
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 1
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] The commit fixes a real bug: virtio-net TX headers reach the
device with stale or uninitialised bytes.
- [Low] The commit message says the bytes the helper does not set "go to
the device as they were in skb->cb or the headroom", but it never says…
--
virtio-net: zero the whole tx header
This commit clears the whole negotiated virtio-net header in xmit_skb()
before virtio_net_hdr_tnl_from_skb() fills it in. Bytes past struct
virtio_net_hdr no longer carry stale skb->cb or headroom contents to the
device. The separate num_buffers assignment for mergeable rx buffers is
dropped because the memset covers it.
> diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
> index dd2c41ef6fbe7..4c740b403ad1e 100644
> --- a/drivers/net/virtio_net.c
> +++ b/drivers/net/virtio_net.c
> @@ -3326,14 +3326,16 @@ static int xmit_skb(struct send_queue *sq, struct sk_buff *skb, bool orphan)
> else
> hdr = &skb_vnet_common_hdr(skb)->tnl_hdr;
>
> + /* Fields the helper below does not fill in must not carry stale
> + * skb->cb or headroom bytes to the device.
> + */
> + memset(hdr, 0, hdr_len);
> +
[Severity: Medium]
This fixes uninitialized bytes reaching the device. Should it carry a Fixes:
tag and go to net on its own, rather than as part of a net-next series?
Before this change, non-tunnel skbs only went through
virtio_net_hdr_from_skb(). That clears sizeof(struct virtio_net_hdr), which
is 10 bytes.
virtnet_probe() sets vi->hdr_len to 12 (VIRTIO_F_VERSION_1 or MRG_RXBUF), 20
(hash report) or 24 (UDP tunnel GSO). So bytes 10 through hdr_len - 1 went to
the device unchanged from skb->cb or the headroom.
The 24-byte layout and the current form of the num_buffers assignment come
from this commit, which is in currently maintained stable kernels:
Fixes: 56a06bd40fab ("virtio_net: enable gso over UDP tunnel support.")
The 12-byte and 20-byte cases are older, so their origin commits could be
named as well.
Without the tag, the fix only lives in a net-next feature series (RX
timestamp support). Would stable and distro kernels then keep sending up to
14 stale bytes per packet?
> if (virtio_net_hdr_tnl_from_skb(skb, hdr, vi->tx_tnl,
> virtio_is_little_endian(vi->vdev), 0,
> false, feature_hdrlen))
> return -EPROTO;
>
> - if (vi->mergeable_rx_bufs)
> - hdr->hash_hdr.hdr.num_buffers = 0;
> -
[Severity: Low]
The commit message says the remaining bytes "go to the device as they were
in skb->cb or the headroom". It does not say that this exposes kernel memory
to the device. Could the message state that?
In the can_push case of xmit_skb(), hdr points into the skb headroom:
if (can_push)
hdr = (struct virtio_net_hdr_v1_hash_tunnel *)(skb->data -
hdr_len);
The skb allocators don't zero the headroom, and nothing writes the bytes in
front of the Ethernet header. So up to 14 bytes of uninitialized slab
content per packet ended up in buffers the device can read. This matters
when the host or backend is not trusted, for example SEV-SNP/TDX guests or
VDUSE and vhost-user backends.
Also, the virtio spec requires the driver to set num_buffers to zero on
transmit, so a non-zero value there breaks the spec.
The XDP TX path in the same driver already zeroes the whole header in
__virtnet_xdp_xmit_one():
memset(hdr, 0, vi->hdr_len);
Without words like "info leak" or "uninitialized memory", might maintainers
and stable reviewers treat this patch as a cleanup?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112-v6-7-topic-virtio-net-ptp-v5-0-7ab67b572f4f%40pengutronix.de
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH net-next v5 5/9] virtio-net: support receive timestamp
2026-11-12 11:19 ` [PATCH net-next v5 5/9] virtio-net: support receive timestamp Steffen Trumtrar
@ 2026-11-16 13:55 ` netdev-bot+sashiko
0 siblings, 0 replies; 16+ messages in thread
From: netdev-bot+sashiko @ 2026-11-16 13:55 UTC (permalink / raw)
To: s.trumtrar
Cc: mst, xuanzhuo, davem, kuba, pabeni, richardcochran,
willemdebruijn.kernel, andrew+netdev, eperezma, jasowangio, ast,
daniel, hawk, john.fastabend, sdf, edumazet, andrew, horms,
kory.maincent, liangchen.linux, hengqi, virtualization, netdev,
linux-kernel, bpf, kvm, kernel
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 0 (1) · Medium: 1 · Low: 1
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] Feature bit and wire layout are chosen without citing the
virtio spec.
- [Low] The feature bit is exported to userspace but the header layout it
enables is not.
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the patch
extends it.
--
virtio-net: support receive timestamp
This adds a VIRTIO_NET_F_RX_TSTAMP feature (bit 49) to virtio-net. When
it is negotiated, the vnet header grows to 40 bytes and carries a le64 TAI
receive timestamp at offset 32, which is attached to skbs through
skb_hwtstamps(). Timestamping is switched on and off at runtime with
ndo_hwtstamp_get/set.
> diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
> index 23e7f62a6ff64..57b7c32625dfb 100644
> --- a/drivers/net/virtio_net.c
> +++ b/drivers/net/virtio_net.c
[ ... ]
> @@ -1324,7 +1344,9 @@ static void virtnet_receive_xsk_buf(struct virtnet_info *vi, struct receive_queu
> return;
> }
>
> - flags = ((struct virtio_net_common_hdr *)(xdp->data - vi->hdr_len))->hdr.flags;
> + hdr = (struct virtio_net_common_hdr *)(xdp->data - vi->hdr_len);
> + flags = hdr->hdr.flags;
> + tstamp = virtnet_record_rx_tstamp(vi, hdr);
[Severity: High]
This is a pre-existing issue, but this patch adds a new read from the same
region. On the AF_XDP zero-copy path, is the vnet header in front of
xdp->data ever synced for the CPU?
virtnet_add_recvbuf_xsk() posts the descriptor hdr_len bytes before the xsk
DMA address:
addr = xsk_buff_xdp_get_dma(xsk_buffs[i]) - vi->hdr_len;
buf_to_xdp() only calls xsk_buff_dma_sync_for_cpu(), which ends up in:
include/net/xsk_buff_pool.h:xp_dma_sync_for_cpu() {
dma_sync_single_for_cpu(xskb->pool->dev, xskb->dma,
xskb->pool->frame_len,
DMA_BIDIRECTIONAL);
}
That range starts at xskb->dma, so it does not cover the hdr_len bytes the
device wrote in front of it.
Under swiotlb bouncing (for example SEV/TDX guests with
VIRTIO_F_ACCESS_PLATFORM, or swiotlb=force),
__swiotlb_sync_single_for_cpu() copies back only the synced range.
Non-coherent DMA has the same problem. In both cases the CPU reads stale
bytes from the umem page, and userspace or bpf_xdp_adjust_meta() may have
written those bytes.
virtnet_record_rx_tstamp() now reads hdr->tstamp from that region, and
virtnet_receive_done() reports the value through skb_hwtstamps(). Could a
stale or user-controlled value end up reported as the hardware timestamp?
The same gap already affects hdr->hdr.flags (VIRTIO_NET_HDR_F_DATA_VALID)
and num_buffers in virtnet_receive_xsk_merge(). If num_buffers reads as 0,
the while (--num_buf) loop in xsk_append_merge_buffer() wraps the u32, then
consumes and drops buffers that belong to the following packets.
Would syncing the header range too, starting at the posted address
(xskb->dma - vi->hdr_len), fix all of these?
[ ... ]
> diff --git a/include/linux/virtio_net.h b/include/linux/virtio_net.h
> index a6de308b8f25e..2abd8a614a23f 100644
> --- a/include/linux/virtio_net.h
> +++ b/include/linux/virtio_net.h
> @@ -17,6 +17,16 @@ struct virtio_net_common_hdr {
> struct virtio_net_hdr_v1_hash hash_v1_hdr;
> struct virtio_net_hdr_v1_hash_tunnel tnl_hdr;
> };
> + /*
> + * The wire layout is fixed and cumulative: a field sits at the same
> + * offset whether or not the features before it were negotiated; unused
> + * bytes are padding. tstamp is thus always at offset 32 and only
> + * present (header size 40) if VIRTIO_NET_F_RX_TSTAMP was negotiated.
> + * The negotiated header size tells how much of this struct is
> + * on the wire. Only ever append new fields at the end.
> + */
> + __u8 reserved[8]; /* outer_nh_offset, ipsec_resource_hdr (not implemented) */
> + __le16 tstamp[4]; /* le64 TAI receive time in ns, 0 if none */
> };
[Severity: Low]
Should this layout be exported in include/uapi/linux/virtio_net.h along
with the feature bit?
VIRTIO_NET_F_RX_TSTAMP is now in the uAPI header. The 40 byte layout it
turns on (reserved[8] at offset 24, le64 tstamp at offset 32) is defined
only in this kernel-internal struct. The earlier patch in the series,
"virtio-net: move virtio_net_common_hdr to virtio_net.h", moved it here,
and the series never adds a uAPI struct.
Other features that extend the header have exported wire structs. Examples
are struct virtio_net_hdr_v1_hash for VIRTIO_NET_F_HASH_REPORT and struct
virtio_net_hdr_v1_hash_tunnel for the UDP tunnel GSO bits.
Device implementations that import the uAPI header (QEMU standard-headers,
vhost-user and DPDK backends) get the bit but no definition of the offset
and size they have to produce.
reserved[] stands in for outer_nh_offset and ipsec_resource_hdr, and
neither field is in the uAPI. The cumulative layout claim therefore can't
be checked against any exported definition.
> diff --git a/include/uapi/linux/virtio_net.h b/include/uapi/linux/virtio_net.h
> index 1db45b01532b5..15e8e408d50fb 100644
> --- a/include/uapi/linux/virtio_net.h
> +++ b/include/uapi/linux/virtio_net.h
> @@ -56,6 +56,7 @@
> #define VIRTIO_NET_F_MQ 22 /* Device supports Receive Flow
> * Steering */
> #define VIRTIO_NET_F_CTRL_MAC_ADDR 23 /* Set MAC address */
> +#define VIRTIO_NET_F_RX_TSTAMP 49 /* Device sends TAI receive time */
[Severity: Medium]
Is bit 49, and this header layout, reserved in the virtio spec?
The commit message does not reference an OASIS virtio-comment change. The
only device it was tested with is "a patched QEMU". The tun patch in this
series says it is "not intended to be merged" and links an out-of-tree QEMU
branch.
A gap in this header doesn't mean a bit is free in the spec. Bit 51 is also
unassigned here, and bits 46/47 are already reused as
VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO_MAPPED and
VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO_CSUM_MAPPED. Willem de Bruijn's earlier
timestamp work used bits that the spec later gave to USO and notification
coalescing.
Placing the timestamp at offset 32 also relies on outer_nh_offset and
ipsec_resource_hdr, which are not implemented here.
Suppose the spec gives bit 49, or the timestamp offset, to something else.
A device offering bit 49 would then make virtnet_probe() choose:
if (vi->rx_tstamp_supported)
vi->hdr_len = offsetofend(struct virtio_net_common_hdr, tstamp);
for both RX and TX. RX would strip the wrong number of bytes, TX would send
a header of the wrong size, and unrelated bytes would be reported as
hardware timestamps.
The vhost-net patch later in the series also advertises the bit to
userspace. That would make it negotiated ABI as soon as this is merged.
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112-v6-7-topic-virtio-net-ptp-v5-0-7ab67b572f4f%40pengutronix.de
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH net-next v5 7/9] vhost-net: support VIRTIO_NET_F_RX_TSTAMP
2026-11-12 11:19 ` [PATCH net-next v5 7/9] vhost-net: support VIRTIO_NET_F_RX_TSTAMP Steffen Trumtrar
@ 2026-11-16 13:55 ` netdev-bot+sashiko
0 siblings, 0 replies; 16+ messages in thread
From: netdev-bot+sashiko @ 2026-11-16 13:55 UTC (permalink / raw)
To: s.trumtrar
Cc: mst, xuanzhuo, davem, kuba, pabeni, richardcochran,
willemdebruijn.kernel, andrew+netdev, eperezma, jasowangio, ast,
daniel, hawk, john.fastabend, sdf, edumazet, andrew, horms,
kory.maincent, liangchen.linux, hengqi, virtualization, netdev,
linux-kernel, bpf, kvm, kernel
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 2 · Low: 0
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] vhost-net now always offers VIRTIO_NET_F_RX_TSTAMP ("Device
sends TAI receive time") through VHOST_GET_FEATURES…
- [Medium] Sizing the vnet header to 40 bytes for VIRTIO_NET_F_RX_TSTAMP
alone (vhost_net_set_features(), drivers/vhost/net.c:1702-1704) breaks…
--
vhost-net: support VIRTIO_NET_F_RX_TSTAMP
This commit advertises VIRTIO_NET_F_RX_TSTAMP from vhost-net and sizes the
vnet header to 40 bytes when the feature is negotiated. When vhost supplies
the header itself, everything behind the fields it fills in is zeroed, so
the timestamp is left at 0.
> diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c
> index 435d8ed3851f3..1b171af105a22 100644
> --- a/drivers/vhost/net.c
> +++ b/drivers/vhost/net.c
> @@ -77,7 +77,8 @@ static const int vhost_net_bits[] = {
> VIRTIO_F_RING_RESET,
> VIRTIO_F_IN_ORDER,
> VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO,
> - VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO
> + VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO,
> + VIRTIO_NET_F_RX_TSTAMP
> };
[Severity: Medium]
Should VIRTIO_NET_F_RX_TSTAMP be offered unconditionally through
VHOST_GET_FEATURES when nothing in the RX path writes a timestamp?
With VHOST_NET_F_VIRTIO_NET_HDR, the new block in handle_rx() zeroes every
byte after hdr_filled, so tstamp is always 0:
if (unlikely(vhost_hlen > hdr_filled)) {
size_t rest = vhost_hlen - hdr_filled;
if (iov_iter_zero(rest, &fixup) != rest) {
With a socket-supplied header, the commit message says tun fills in the
timestamp. But __tun_vnet_hdr_put() in drivers/net/tun_vnet.h copies only
tun_vnet_parse_size(features) bytes (10 or 24) and zeroes the rest:
unsigned int parsed_size = tun_vnet_parse_size(features);
...
if (iov_iter_zero(sz - parsed_size, iter) != sz - parsed_size)
return -EFAULT;
tun.c, tap.c and tun_vnet.h don't reference tstamp at all.
The last patch in the series, "tun: add rx timestamp to the vnet header",
is labeled as not intended to be merged. Even with that patch applied, only
tun_put_user() fills in the timestamp. tun_put_user_xdp() and tap_put_user()
still go through tun_vnet_hdr_put() with a zero-initialized
virtio_net_common_hdr, so tstamp stays 0 on those paths.
On the guest side, negotiating the bit sets vi->rx_tstamp_supported.
virtnet_get_ts_info() then reports:
if (vi->rx_tstamp_supported) {
info->so_timestamping |=
SOF_TIMESTAMPING_RX_HARDWARE |
SOF_TIMESTAMPING_RAW_HARDWARE;
virtnet_hwtstamp_set() also accepts HWTSTAMP_FILTER_ALL. Since every
timestamp is 0, no skb ever gets a hwtstamp. A guest PTP stack that turns
on hardware RX timestamping because of the advertised capability will
never receive any timestamps.
Once the bit is in vhost_net_bits[] it is visible to userspace. Could the
feature be offered only when the attached backend can actually produce a
timestamp?
[ ... ]
> @@ -1684,6 +1699,10 @@ static int vhost_net_set_features(struct vhost_net *n, const u64 *features)
> VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO))
> hdr_len = sizeof(struct virtio_net_hdr_v1_hash_tunnel);
>
> + /* The header layout is cumulative, tstamp sits behind all other fields. */
> + if (virtio_features_test_bit(features, VIRTIO_NET_F_RX_TSTAMP))
> + hdr_len = offsetofend(struct virtio_net_common_hdr, tstamp);
> +
[Severity: Medium]
Does a 40 byte header for RX_TSTAMP alone break the way tun guesses the
guest features?
tun_vnet_hdr_guest_features() in drivers/net/tun_vnet.h infers the tunnel
features from the header size:
/*
* Tun is not aware of the negotiated guest features, guess them from the
* virtio net hdr size
*/
static inline netdev_features_t tun_vnet_hdr_guest_features(int vnet_hdr_sz)
{
if (vnet_hdr_sz >= TUN_VNET_TNL_SIZE)
return NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_UDP_TUNNEL_CSUM;
return 0;
}
Take socket-header mode with a guest that negotiates RX_TSTAMP but neither
HOST_UDP_TUNNEL_GSO nor GUEST_UDP_TUNNEL_GSO. It gets sock_hlen = 40, so
the tap's vnet_hdr_sz is 40. Both guest TX paths then treat the tunnel
features as negotiated:
tun_get_user()
features = tun_vnet_hdr_guest_features(vnet_hdr_sz);
hdr_len = __tun_vnet_hdr_get(vnet_hdr_sz, tun->flags,
features, from, gso);
vhost_net_build_xdp() ... tun_sendmsg()->tun_xdp_one()
features = tun_vnet_hdr_guest_features(READ_ONCE(tun->vnet_hdr_sz));
...
tun_vnet_hdr_tnl_to_skb(tun->flags, features, skb, tnl_hdr)
This passes tnl_hdr_negotiated = true and tnl_csum_negotiated = true into
virtio_net_hdr_tnl_to_skb(). As a result, the -EINVAL check for a header
that asks for tunnel GSO without negotiating it is skipped. The
VIRTIO_NET_HDR_F_UDP_TUNNEL_CSUM check is skipped as well.
Could a guest then get UDP tunnel GSO packets, and the tunnel csum flag,
accepted by the host even when the VMM left host UDP tunnel GSO disabled?
Before this patch, only the tunnel features made the vhost-net header 24
bytes or larger, so the heuristic held. Nothing else in the series updates
tun.
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112-v6-7-topic-virtio-net-ptp-v5-0-7ab67b572f4f%40pengutronix.de
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH net-next v5 8/9] virtio-net: add XDP rx timestamp metadata
2026-11-12 11:19 ` [PATCH net-next v5 8/9] virtio-net: add XDP rx timestamp metadata Steffen Trumtrar
@ 2026-11-16 13:55 ` netdev-bot+sashiko
0 siblings, 0 replies; 16+ messages in thread
From: netdev-bot+sashiko @ 2026-11-16 13:55 UTC (permalink / raw)
To: s.trumtrar
Cc: mst, xuanzhuo, davem, kuba, pabeni, richardcochran,
willemdebruijn.kernel, andrew+netdev, eperezma, jasowangio, ast,
daniel, hawk, john.fastabend, sdf, edumazet, andrew, horms,
kory.maincent, liangchen.linux, hengqi, virtualization, netdev,
linux-kernel, bpf, kvm, kernel
Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 0
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] virtnet_probe() now reports XDP rx-metadata capabilities the
device does not have.
--
virtio-net: add XDP rx timestamp metadata
This adds an xmo_rx_timestamp callback to virtio-net, so XDP programs can
read the rx timestamp with bpf_xdp_metadata_rx_timestamp(). The timestamp
is copied into struct virtnet_xdp_buff before the program runs. The
metadata ops are now also installed when VIRTIO_NET_F_RX_TSTAMP is the
only one of these features negotiated.
> 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
[ ... ]
> @@ -6896,8 +6899,21 @@ static int virtnet_xdp_rx_hash(const struct xdp_md *_ctx, u32 *hash,
[ ... ]
> 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;
> +
[Severity: Medium]
virtnet_probe() installs the same virtnet_xdp_metadata_ops table in both
places. Does that change what netdev netlink reports for devices that
have only one of the two features?
netdev_nl_dev_fill() sets an xdp-rx-metadata-features bit for every
xmo_* pointer that is not NULL:
net/core/netdev-genl.c:netdev_nl_dev_fill() {
...
#define XDP_METADATA_KFUNC(_, flag, __, xmo) \
if (netdev->xdp_metadata_ops && netdev->xdp_metadata_ops->xmo) \
xdp_rx_meta |= flag;
...
}
Documentation/netlink/specs/netdev.yaml describes the timestamp bit as
"Device is capable of exposing receive HW timestamp via
bpf_xdp_metadata_rx_timestamp()".
Take an existing device that negotiates VIRTIO_NET_F_RSS or
VIRTIO_NET_F_HASH_REPORT, but not VIRTIO_NET_F_RX_TSTAMP. It still gets
the table from the has_rss || has_rss_hash_report block above. After this
patch, that table has xmo_rx_timestamp, so the device now reports the
timestamp capability.
Can such a device ever deliver a timestamp? virtnet_hwtstamp_set()
rejects the request:
if (!vi->rx_tstamp_supported)
return -EOPNOTSUPP;
So rx_tstamp_enabled stays false and virtnet_record_rx_tstamp() always
returns 0. As a result, virtnet_xdp_rx_timestamp() always returns
-ENODATA.
virtnet_get_ts_info() also does not set SOF_TIMESTAMPING_RX_HARDWARE in
this case. The ethtool and netdev netlink answers then disagree.
The opposite case is the one the new comment mentions. A device with only
VIRTIO_NET_F_RX_TSTAMP never gets NETIF_F_RXHASH. It now reports the hash
capability, but virtnet_xdp_rx_hash() always returns -ENODATA.
Would separate xdp_metadata_ops tables, chosen by which features were
negotiated, keep the reported bits accurate? The commit message covers
the rx_hash case, but not the change for existing RSS/hash-only devices.
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112-v6-7-topic-virtio-net-ptp-v5-0-7ab67b572f4f%40pengutronix.de
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH net-next v5 9/9] tun: add rx timestamp to the vnet header
2026-11-12 11:19 ` [PATCH net-next v5 9/9] tun: add rx timestamp to the vnet header Steffen Trumtrar
@ 2026-11-16 13:55 ` netdev-bot+sashiko
0 siblings, 0 replies; 16+ messages in thread
From: netdev-bot+sashiko @ 2026-11-16 13:55 UTC (permalink / raw)
To: s.trumtrar
Cc: mst, xuanzhuo, davem, kuba, pabeni, richardcochran,
willemdebruijn.kernel, andrew+netdev, eperezma, jasowangio, ast,
daniel, hawk, john.fastabend, sdf, edumazet, andrew, horms,
kory.maincent, liangchen.linux, hengqi, virtualization, netdev,
linux-kernel, bpf, kvm, kernel
Thank you for your contribution! Sashiko AI review found 5 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 4 · Low: 1
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] The commit message says the TAI time is written "when a packet
is handed to userspace or vhost".
- [Medium] Whether tun writes the timestamp depends only on the
configured header size: `vnet_hdr_sz >= offsetofend(struct…
- [Medium] The value the device side reports as the 'TAI receive time' is
sampled in tun_put_user() (drivers/net/tun.c:2178,…
- [Medium] tun cannot see the negotiated virtio features, so it guesses
them from vnet_hdr_sz.
- [Low] The patch adds `sz < tun_vnet_parse_size(features)` → -EINVAL in
__tun_vnet_hdr_put() (drivers/net/tun_vnet.h:163-164).
--
tun: add rx timestamp to the vnet header
This is a reference implementation of the device side of
VIRTIO_NET_F_RX_TSTAMP. When the configured vnet_hdr_sz covers the tstamp
field, tun_put_user() writes the current TAI time into the vnet header.
__tun_vnet_hdr_put() now takes a struct virtio_net_common_hdr and copies as
much of it as vnet_hdr_sz holds.
> diff --git a/drivers/net/tun.c b/drivers/net/tun.c
> index 8920c797acfd7..1a8c3377753d7 100644
> --- a/drivers/net/tun.c
> +++ b/drivers/net/tun.c
[ ... ]
> @@ -2168,12 +2169,16 @@ static ssize_t tun_put_user(struct tun_struct *tun,
> }
>
> if (vnet_hdr_sz) {
> - struct virtio_net_hdr_v1_hash_tunnel hdr;
> - struct virtio_net_hdr *gso;
> + struct virtio_net_common_hdr hdr;
>
> memset(&hdr, 0, sizeof(hdr));
> +
> + /* The configured header covers the tstamp field */
> + if (vnet_hdr_sz >= offsetofend(struct virtio_net_common_hdr, tstamp))
[Severity: Medium]
Is the header size enough on its own to gate this?
TUNSETVNETHDRSZ in tun_vnet_ioctl() accepts any size of at least
sizeof(struct virtio_net_hdr), with no upper bound. Before this patch,
__tun_vnet_hdr_put() zero-filled everything past parsed_size (10 or 24
bytes). After it, any existing tun user with vnet_hdr_sz >= 40 starts
getting host CLOCK_TAI values in bytes 32..39.
The tstamp layout is only defined in the kernel-internal
include/linux/virtio_net.h. Its comment says the field is present only if
VIRTIO_NET_F_RX_TSTAMP was negotiated. tun cannot see that negotiation, and
the uapi header exports only the feature bit.
UDP tunnel GSO was added behind TUN_F_UDP_TUNNEL_GSO. Older kernels reject
that flag in set_offload(), so userspace can probe for it. Without a
similar TUN_F_* opt-in, how would QEMU know whether the running kernel
fills the field before it offers VIRTIO_NET_F_RX_TSTAMP to a guest?
[Severity: Medium]
Does letting a 40-byte header mean RX_TSTAMP conflict with the size-based
feature guess in tun_vnet_hdr_guest_features()?
drivers/net/tun_vnet.h:tun_vnet_hdr_guest_features() {
if (vnet_hdr_sz >= TUN_VNET_TNL_SIZE)
return NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_UDP_TUNNEL_CSUM;
return 0;
}
In this series, virtio_net and vhost-net use a 40-byte header for
VIRTIO_NET_F_RX_TSTAMP even when UDP tunnel GSO was not negotiated. On the
guest TX side, tun_get_user() and tun_xdp_one() still pass the guessed
tunnel features to the header parser:
handle_tx()
tun_sendmsg()
tun_get_user() / tun_xdp_one()
tun_vnet_hdr_guest_features(40) -> tunnel features
virtio_net_hdr_tnl_to_skb(..., tnl_hdr_negotiated = true)
That means this check in virtio_net_hdr_tnl_to_skb() never fires:
/* Tunnel not supported/negotiated, but the hdr asks for it. */
if (!tnl_hdr_negotiated)
return -EINVAL;
Could a guest that negotiated only RX_TSTAMP send the
VIRTIO_NET_HDR_GSO_UDP_TUNNEL_* gso_type bits, the
VIRTIO_NET_HDR_F_UDP_TUNNEL_CSUM flag, or outer_th_offset / inner_nh_offset
values in bytes 20..23, and have the host accept them? For that guest,
those bytes should only be padding.
> + put_unaligned_le64(ktime_get_clocktai_ns(), hdr.tstamp);
[Severity: Medium]
Is this really the receive time?
tun_put_user() runs when userspace or the vhost worker dequeues the packet
from tfile->tx_ring through read/recvmsg. The packet reached the tun device
earlier, in tun_net_xmit(). Any timestamp already on the skb is also
ignored.
The guest stores this value in skb_hwtstamps(skb)->hwtstamp in
virtnet_receive_done() and treats it as a hardware RX timestamp. So the
reported value also includes the ptr_ring backlog (up to tx_queue_len
entries) and vhost/userspace scheduling latency. Guest PTP or latency
tools would count that delay as receive time.
The uapi describes the feature as "Device sends TAI receive time". Should
the timestamp be taken at ingress in tun_net_xmit(), or come from an
existing skb timestamp? If dequeue time is intentional, could the commit
message say why? A real implementation would likely copy this reference.
> ret = tun_vnet_hdr_tnl_from_skb(tun->flags, tun->dev, skb,
> - &hdr);
> + &hdr.tnl_hdr);
> if (ret)
> return ret;
[ ... ]
> diff --git a/drivers/net/tun_vnet.h b/drivers/net/tun_vnet.h
> index f4c652b1fa44d..d4cc56dac98d6 100644
> --- a/drivers/net/tun_vnet.h
> +++ b/drivers/net/tun_vnet.h
> @@ -153,19 +153,21 @@ static inline int tun_vnet_hdr_get(int sz, unsigned int flags,
> return __tun_vnet_hdr_get(sz, flags, 0, from, hdr);
> }
>
> +/* hdr is fully initialized, copy as much of it as the configured size holds */
> static inline int __tun_vnet_hdr_put(int sz, netdev_features_t features,
> struct iov_iter *iter,
> - const struct virtio_net_hdr *hdr)
> + const struct virtio_net_common_hdr *hdr)
> {
> - unsigned int parsed_size = tun_vnet_parse_size(features);
> + unsigned int put_size = min_t(unsigned int, sizeof(*hdr), sz);
>
> - if (unlikely(iov_iter_count(iter) < sz))
> + if (unlikely(iov_iter_count(iter) < sz ||
> + sz < tun_vnet_parse_size(features)))
> return -EINVAL;
[Severity: Low]
The new sz < tun_vnet_parse_size(features) check seems to quietly fix an
existing bug. Should it go in a separate patch with a Fixes: tag?
set_offload() enables NETIF_F_GSO_UDP_TUNNEL without looking at
vnet_hdr_sz, and TUNSETVNETHDRSZ accepts 10 without re-checking features.
That makes sz = 10 with parsed_size = 24 reachable. Before this patch,
__tun_vnet_hdr_put() would then do:
copy_to_iter(hdr, parsed_size, iter)
This writes 24 bytes into a 10-byte header slot, which overruns into the
packet area of the user's buffer. Next, sz - parsed_size wraps to
0xFFFFFFF2, and iov_iter_zero() zeroes the rest of the buffer before
-EFAULT is returned.
With the new check, -EINVAL is returned before anything is written. The
commit message does not mention this change in behaviour, and the patch is
marked as not intended to be merged, so the fix would be lost. The
underlying problem appears to go back to commit 288f30435132 ("tun: enable
gso over UDP tunnel support").
[ ... ]
> @@ -174,7 +176,11 @@ static inline int __tun_vnet_hdr_put(int sz, netdev_features_t features,
> static inline int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
> const struct virtio_net_hdr *hdr)
> {
> - return __tun_vnet_hdr_put(sz, 0, iter, hdr);
> + struct virtio_net_common_hdr common_hdr = {};
> +
> + common_hdr.hdr = *hdr;
> +
> + return __tun_vnet_hdr_put(sz, 0, iter, &common_hdr);
> }
[Severity: Medium]
The commit message says the TAI time is written "when a packet is handed
to userspace or vhost", but tstamp is never set here. Do packets that go
through tun_vnet_hdr_put() always get a zero timestamp, even when sz
covers the field?
There are two such callers. The first is tun_put_user_xdp(), which handles
xdp_frames on tfile->tx_ring (for example from XDP_REDIRECT through
tun_xdp_xmit()):
if (tun->flags & IFF_VNET_HDR) {
struct virtio_net_hdr gso = { 0 };
vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
ret = tun_vnet_hdr_put(vnet_hdr_sz, iter, &gso);
The second is tap_put_user() for macvtap/ipvtap, which also calls
tun_vnet_hdr_put().
vhost-net advertises VIRTIO_NET_F_RX_TSTAMP for every backend and uses a
40-byte header once it is negotiated. The guest then turns on RX
timestamping.
So a guest on a macvtap/ipvtap backend would negotiate the feature and
never get a timestamp. On tun, one queue would mix timestamped skbs with
XDP frames that have no timestamp. Is that intended, or should these paths
fill tstamp as well?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261112-v6-7-topic-virtio-net-ptp-v5-0-7ab67b572f4f%40pengutronix.de
^ permalink raw reply [flat|nested] 16+ messages in thread
end of thread, other threads:[~2026-11-16 13:55 UTC | newest]
Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-12 11:19 [PATCH net-next v5 0/9] virtio-net: add receive timestamp support Steffen Trumtrar
2026-11-12 11:19 ` [PATCH net-next v5 1/9] net: ethtool: tsconfig: allow hardware timestamping without a PHC Steffen Trumtrar
2026-11-12 11:19 ` [PATCH net-next v5 2/9] virtio-net: zero the whole tx header Steffen Trumtrar
2026-11-16 13:55 ` netdev-bot+sashiko
2026-11-12 11:19 ` [PATCH net-next v5 3/9] virtio-net: copy the XDP rx hash before the program runs Steffen Trumtrar
2026-11-12 11:19 ` [PATCH net-next v5 4/9] virtio-net: move virtio_net_common_hdr to virtio_net.h Steffen Trumtrar
2026-11-12 11:19 ` [PATCH net-next v5 5/9] virtio-net: support receive timestamp Steffen Trumtrar
2026-11-16 13:55 ` netdev-bot+sashiko
2026-11-12 11:19 ` [PATCH net-next v5 6/9] virtio-net: assert header sizes Steffen Trumtrar
2026-11-12 11:19 ` [PATCH net-next v5 7/9] vhost-net: support VIRTIO_NET_F_RX_TSTAMP Steffen Trumtrar
2026-11-16 13:55 ` netdev-bot+sashiko
2026-11-12 11:19 ` [PATCH net-next v5 8/9] virtio-net: add XDP rx timestamp metadata Steffen Trumtrar
2026-11-16 13:55 ` netdev-bot+sashiko
2026-11-12 11:19 ` [PATCH net-next v5 9/9] tun: add rx timestamp to the vnet header Steffen Trumtrar
2026-11-16 13:55 ` netdev-bot+sashiko
2026-11-12 11:26 ` [PATCH net-next v5 0/9] virtio-net: add receive timestamp support netdev-bot+sinfo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox