mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Willem de Bruijn <willemdebruijn.kernel@gmail.com>
To: "Steffen Trumtrar" <s.trumtrar@pengutronix.de>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Xuan Zhuo" <xuanzhuo@linux.alibaba.com>,
	"David S. Miller" <davem@davemloft.net>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Paolo Abeni" <pabeni@redhat.com>,
	"Richard Cochran" <richardcochran@gmail.com>,
	"Willem de Bruijn" <willemdebruijn.kernel@gmail.com>,
	"Andrew Lunn" <andrew+netdev@lunn.ch>,
	"Eugenio Pérez" <eperezma@redhat.com>,
	"Jason Wang" <jasowangio@gmail.com>,
	"Alexei Starovoitov" <ast@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Jesper Dangaard Brouer" <hawk@kernel.org>,
	"John Fastabend" <john.fastabend@gmail.com>,
	"Stanislav Fomichev" <sdf@fomichev.me>,
	"Eric Dumazet" <edumazet@kernel.org>
Cc: Steffen Trumtrar <s.trumtrar@pengutronix.de>,
	 virtualization@lists.linux.dev,  netdev@vger.kernel.org,
	 linux-kernel@vger.kernel.org,  bpf@vger.kernel.org,
	 kvm@vger.kernel.org
Subject: Re: [PATCH RFC v4 5/6] virtio-net: add XDP rx timestamp metadata
Date: Sun, 01 Nov 2026 10:12:30 -0500	[thread overview]
Message-ID: <willemdebruijn.kernel.9c17365c41b3@gmail.com> (raw)
In-Reply-To: <20261026-v6-7-topic-virtio-net-ptp-v4-5-008af03cd43b@pengutronix.de>

Steffen Trumtrar wrote:
> 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 virtnet_xdp_rx_hash(), the callback finds the header relative to
> xdp->data, so it has to be called before bpf_xdp_adjust_head().
> 
> Signed-off-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
> Assisted-by: Claude:claude-opus-5-5
> ---
>  drivers/net/virtio_net.c | 26 ++++++++++++++++++++++++++
>  1 file changed, 26 insertions(+)
> 
> diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
> index 23af10c613b48..e79b65653fad7 100644
> --- a/drivers/net/virtio_net.c
> +++ b/drivers/net/virtio_net.c
> @@ -6848,8 +6848,30 @@ 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 xdp_buff *xdp = (void *)_ctx;
> +	const struct virtio_net_common_hdr *hdr;
> +	struct virtnet_info *vi;
> +	u64 tstamp;
> +
> +	vi = netdev_priv(xdp->rxq->dev);
> +	if (!READ_ONCE(vi->hwts_rx_en))
> +		return -ENODATA;
> +
> +	hdr = (const struct virtio_net_common_hdr *)(xdp->data - vi->hdr_len);

    Reading `hdr->tstamp` in place from `xdp->data - vi->hdr_len` violates the XDP
    metadata area contract (`Documentation/networking/xdp-rx-metadata.rst`: drivers
    must copy device-reserved metadata out of `[data_hard_start, data)` before
    presenting the frame to XDP) and breaks standard XDP metadata consumers:
    - `hdr->tstamp` sits at `[xdp->data - 8, xdp->data)`. Standard XDP programs—such
      as `tools/testing/selftests/bpf/progs/xdp_hw_metadata.c`—call
      `bpf_xdp_adjust_meta(ctx, -(int)sizeof(struct xdp_meta))` first, initialize
      fields in `meta` (e.g. `meta->hint_valid = 0` at `[xdp->data - 4, xdp->data)`),
      and only then call `bpf_xdp_metadata_rx_timestamp(ctx, &meta->rx_timestamp)`.
      Initializing `meta` overwrites `hdr->tstamp` (and `hdr->hash_v1_hdr`) before
      this kfunc runs.
    - Calling `bpf_xdp_adjust_head()` before reading metadata also shifts `xdp->data`,
      reading arbitrary bytes at `xdp->data - vi->hdr_len`.

    Wrap `struct xdp_buff` in a driver struct on the stack (`container_of`, and in
    `struct xdp_buff_xsk`'s `cb` verified with `XSK_CHECK_PRIV_TYPE()` for AF_XDP,
    matching `gve`/`igc`/`ice`/`mlx5`) and stash `tstamp` (already extracted in
    `receive_buf()` and `virtnet_receive_xsk_buf()` before XDP runs) and the hash
    fields before `bpf_prog_run_xdp()`.

  reply	other threads:[~2026-11-01 15:12 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-26  8:29 [PATCH RFC v4 0/6] virtio-net: add receive timestamp support Steffen Trumtrar
2026-10-26  8:29 ` [PATCH RFC v4 1/6] virtio-net: move virtio_net_common_hdr to virtio_net.h Steffen Trumtrar
2026-10-26  8:29 ` [PATCH RFC v4 2/6] virtio-net: support receive timestamp Steffen Trumtrar
2026-11-01 15:04   ` Willem de Bruijn
2026-10-26  8:29 ` [PATCH RFC v4 3/6] virtio-net: assert header sizes Steffen Trumtrar
2026-10-26  8:29 ` [PATCH RFC v4 4/6] vhost-net: support VIRTIO_NET_F_RX_TSTAMP Steffen Trumtrar
2026-10-26  8:29 ` [PATCH RFC v4 5/6] virtio-net: add XDP rx timestamp metadata Steffen Trumtrar
2026-11-01 15:12   ` Willem de Bruijn [this message]
2026-10-26  8:29 ` [PATCH RFC v4 6/6] tun: add rx timestamp to the vnet header Steffen Trumtrar
2026-11-01 14:57 ` [PATCH RFC v4 0/6] virtio-net: add receive timestamp support Willem de Bruijn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=willemdebruijn.kernel.9c17365c41b3@gmail.com \
    --to=willemdebruijn.kernel@gmail.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=eperezma@redhat.com \
    --cc=hawk@kernel.org \
    --cc=jasowangio@gmail.com \
    --cc=john.fastabend@gmail.com \
    --cc=kuba@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=richardcochran@gmail.com \
    --cc=s.trumtrar@pengutronix.de \
    --cc=sdf@fomichev.me \
    --cc=virtualization@lists.linux.dev \
    --cc=xuanzhuo@linux.alibaba.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®