public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
From: Kohei Enju <kohei@enjuk.jp>
To: 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>,
	Gerhard Engleder <gerhard@engleder-embedded.com>,
	Jonathan Lemon <jonathan.lemon@gmail.com>,
	Richard Cochran <richardcochran@gmail.com>,
	Kohei Enju <kohei@enjuk.jp>
Subject: [PATCH net v1 1/3] net: introduce helper to resolve hardware timestamps from skb
Date: Wed, 29 Apr 2026 09:16:17 +0000	[thread overview]
Message-ID: <20260429091632.26509-2-kohei@enjuk.jp> (raw)
In-Reply-To: <20260429091632.26509-1-kohei@enjuk.jp>

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 (!(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 ((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,
-- 
2.53.0


  reply	other threads:[~2026-04-29  9:16 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 ` Kohei Enju [this message]
2026-04-29 21:04   ` [PATCH net v1 1/3] net: introduce helper to resolve hardware timestamps from skb Willem de Bruijn
2026-04-30  4:50     ` Kohei Enju
2026-05-01 20:25   ` Gerhard Engleder
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=20260429091632.26509-2-kohei@enjuk.jp \
    --to=kohei@enjuk.jp \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=edumazet@google.com \
    --cc=gerhard@engleder-embedded.com \
    --cc=horms@kernel.org \
    --cc=jonathan.lemon@gmail.com \
    --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