public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
From: Gerhard Engleder <gerhard@engleder-embedded.com>
To: Kohei Enju <kohei@enjuk.jp>, netdev@vger.kernel.org
Cc: "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>,
	Kuniyuki Iwashima <kuniyu@google.com>,
	Willem de Bruijn <willemb@google.com>,
	David Ahern <dsahern@kernel.org>,
	Neal Cardwell <ncardwell@google.com>,
	Jonathan Lemon <jonathan.lemon@gmail.com>,
	Richard Cochran <richardcochran@gmail.com>
Subject: Re: [PATCH net v1 1/3] net: introduce helper to resolve hardware timestamps from skb
Date: Fri, 1 May 2026 22:25:59 +0200	[thread overview]
Message-ID: <1c81fd5a-1699-408d-a7ca-f08a13764186@engleder-embedded.com> (raw)
In-Reply-To: <20260429091632.26509-2-kohei@enjuk.jp>

On 29.04.26 11:16, Kohei Enju wrote:
> Move the logic that resolves a hardware timestamp from an skb, including
> late timestamp resolution via netdev_get_tstamp(), from net/socket.c to
> a common helper.
> 
> Let's allow other networking code to reuse the same resolution path.
> 
> Signed-off-by: Kohei Enju <kohei@enjuk.jp>
> ---
>   include/linux/skbuff.h | 11 +++++++++++
>   net/core/skbuff.c      | 27 +++++++++++++++++++++++++++
>   net/socket.c           | 27 +++------------------------
>   3 files changed, 41 insertions(+), 24 deletions(-)
> 
> diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h
> index 2bcf78a4de7b..651a5ae8b11c 100644
> --- a/include/linux/skbuff.h
> +++ b/include/linux/skbuff.h
> @@ -4731,6 +4731,17 @@ void __skb_tstamp_tx(struct sk_buff *orig_skb, const struct sk_buff *ack_skb,
>   void skb_tstamp_tx(struct sk_buff *orig_skb,
>   		   struct skb_shared_hwtstamps *hwtstamps);
>   
> +/**
> + * skb_get_hwtstamp - resolve a hardware timestamp from an skb
> + * @skb:	skb carrying the timestamp
> + * @cycles:	true to request the free-running cycle-based timestamp
> + * @if_index:	optional return pointer for the originating netdev ifindex
> + *
> + * Return: resolved hardware timestamp, or the stored skb hwtstamp when no
> + * device-specific late timestamp resolution is needed.
> + */
> +ktime_t skb_get_hwtstamp(struct sk_buff *skb, bool cycles, int *if_index);
> +
>   /**
>    * skb_tx_timestamp() - Driver hook for transmit timestamping
>    *
> diff --git a/net/core/skbuff.c b/net/core/skbuff.c
> index 7dad68e3b518..d11f4e2e9391 100644
> --- a/net/core/skbuff.c
> +++ b/net/core/skbuff.c
> @@ -5729,6 +5729,33 @@ void skb_tstamp_tx(struct sk_buff *orig_skb,
>   }
>   EXPORT_SYMBOL_GPL(skb_tstamp_tx);
>   
> +ktime_t skb_get_hwtstamp(struct sk_buff *skb, bool cycles, int *if_index)
> +{
> +	struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
> +	struct net_device *orig_dev;
> +	ktime_t hwtstamp;
> +
> +	if (if_index)
> +		*if_index = 0;

if_index is set to 0 here the second time when called from
__sock_recv_timestamp(). IMO you can remove these two
lines, because in skb_get_tx_timestamp() if_index has been
removed by your commit.

> +
> +	if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV))
> +		return shhwtstamps->hwtstamp;
> +
> +	rcu_read_lock();
> +	orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
> +	if (orig_dev) {
> +		if (if_index)
> +			*if_index = orig_dev->ifindex;
> +		hwtstamp = netdev_get_tstamp(orig_dev, shhwtstamps, cycles);
> +	} else {
> +		hwtstamp = shhwtstamps->hwtstamp;
> +	}
> +	rcu_read_unlock();
> +
> +	return hwtstamp;
> +}
> +EXPORT_SYMBOL_GPL(skb_get_hwtstamp);
> +
>   #ifdef CONFIG_WIRELESS
>   void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
>   {
> diff --git a/net/socket.c b/net/socket.c
> index 22a412fdec07..95b21b16a0fc 100644
> --- a/net/socket.c
> +++ b/net/socket.c
> @@ -876,21 +876,7 @@ static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp)
>   static ktime_t get_timestamp(struct sock *sk, struct sk_buff *skb, int *if_index)
>   {
>   	bool cycles = READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_BIND_PHC;
> -	struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
> -	struct net_device *orig_dev;
> -	ktime_t hwtstamp;
> -
> -	rcu_read_lock();
> -	orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
> -	if (orig_dev) {
> -		*if_index = orig_dev->ifindex;
> -		hwtstamp = netdev_get_tstamp(orig_dev, shhwtstamps, cycles);
> -	} else {
> -		hwtstamp = shhwtstamps->hwtstamp;
> -	}
> -	rcu_read_unlock();
> -
> -	return hwtstamp;
> +	return skb_get_hwtstamp(skb, cycles, if_index);
>   }
>   
>   static void put_ts_pktinfo(struct msghdr *msg, struct sk_buff *skb,
> @@ -940,7 +926,6 @@ int skb_get_tx_timestamp(struct sk_buff *skb, struct sock *sk,
>   {
>   	u32 tsflags = READ_ONCE(sk->sk_tsflags);
>   	ktime_t hwtstamp;
> -	int if_index = 0;

if_index is useless in this function, so it can be removed.
This was introduced with 2410251cde0b. Maybe removing
if_index here could be a separate commit, as this clean up
makes sense on its own.

>   
>   	if ((tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
>   	    ktime_to_timespec64_cond(skb->tstamp, ts))
> @@ -950,10 +935,7 @@ int skb_get_tx_timestamp(struct sk_buff *skb, struct sock *sk,
>   	    skb_is_swtx_tstamp(skb, false))
>   		return -ENOENT;
>   
> -	if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV)
> -		hwtstamp = get_timestamp(sk, skb, &if_index);
> -	else
> -		hwtstamp = skb_hwtstamps(skb)->hwtstamp;
> +	hwtstamp = get_timestamp(sk, skb, NULL);
>   
>   	if (tsflags & SOF_TIMESTAMPING_BIND_PHC)
>   		hwtstamp = ptp_convert_timestamp(&hwtstamp,
> @@ -1033,10 +1015,7 @@ void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
>   	      !(tsflags & SOF_TIMESTAMPING_OPT_RX_FILTER))) &&
>   	    !skb_is_swtx_tstamp(skb, false_tstamp)) {
>   		if_index = 0;
> -		if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV)
> -			hwtstamp = get_timestamp(sk, skb, &if_index);
> -		else
> -			hwtstamp = shhwtstamps->hwtstamp;
> +		hwtstamp = get_timestamp(sk, skb, &if_index);
>   
>   		if (tsflags & SOF_TIMESTAMPING_BIND_PHC)
>   			hwtstamp = ptp_convert_timestamp(&hwtstamp,

Gerhard

  parent reply	other threads:[~2026-05-01 20:47 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-29  9:16 [PATCH net v1 0/3] af_packet/tcp: fix late hardware timestamp handling Kohei Enju
2026-04-29  9:16 ` [PATCH net v1 1/3] net: introduce helper to resolve hardware timestamps from skb Kohei Enju
2026-04-29 21:04   ` Willem de Bruijn
2026-04-30  4:50     ` Kohei Enju
2026-05-01 20:25   ` Gerhard Engleder [this message]
2026-04-29  9:16 ` [PATCH net v1 2/3] af_packet: use skb_get_hwtstamp() for hardware timestamps Kohei Enju
2026-04-29  9:16 ` [PATCH net v1 3/3] tcp: " Kohei Enju
2026-04-29 21:09   ` Willem de Bruijn
2026-04-30  5:07     ` Kohei Enju
2026-04-30  6:09       ` Eric Dumazet
2026-04-30  6:52         ` Kohei Enju

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=1c81fd5a-1699-408d-a7ca-f08a13764186@engleder-embedded.com \
    --to=gerhard@engleder-embedded.com \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=jonathan.lemon@gmail.com \
    --cc=kohei@enjuk.jp \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=ncardwell@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=richardcochran@gmail.com \
    --cc=willemb@google.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