af_packet/tcp: fix late hardware timestamp handling
Subsystems (5)
cover af_packet/tcp: fix late hardware timestamp handling
Since commit 97dc7cd92ac6 ("ptp: Support late timestamp determination"), skb_shared_hwtstamps may carry netdev_data instead of a resolved hwtstamp. AF_PACKET and TCP could still read that storage as a ktime_t and report bogus hardware timestamps to userspace.
This series factors the late timestamp resolution logic into a common helper, then switches AF_PACKET and TCP to use it.
Notes on SOF_TIMESTAMPING_BIND_PHC:
The generic socket receive timestamping path honors it, but the AF_PACKET and TCP receive paths touched here haven't implemented that behavior.
This series doesn't change that; those paths always resolve timestamps with cycles == false and preserve their timestamp-domain semantics.
Kohei Enju (3):
net: introduce helper to resolve hardware timestamps from skb
af_packet: use skb_get_hwtstamp() for hardware timestamps
tcp: use skb_get_hwtstamp() for hardware timestamps
include/linux/skbuff.h | 11 +++++++++++ include/net/tcp.h | 2 +- net/core/skbuff.c | 27 +++++++++++++++++++++++++++ net/ipv4/tcp_input.c | 3 ++- net/ipv4/tcp_ipv4.c | 6 ++++-- net/ipv6/tcp_ipv6.c | 3 ++- net/packet/af_packet.c | 2 +- net/socket.c | 27 +++------------------------ 8 files changed, 51 insertions(+), 30 deletions(-)
Signature · 1 line
2.53.0
PATCH 1/3 net: introduce helper to resolve hardware timestamps from skb
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.
--- include/linux/skbuff.h | 11 +++++++++++ net/core/skbuff.c | 27 +++++++++++++++++++++++++++ net/socket.c | 27 +++------------------------ 3 files changed, 41 insertions(+), 24 deletions(-)
include/linux/skbuff.h +11 -0
@@ -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 *
net/core/skbuff.c +27 -0
@@ -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) {
net/socket.c +3 -24
@@ -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,
Signature · 1 line
2.53.0
Willem de Bruijn
maintainerRe: [PATCH net v1 1/3] net: introduce helper to resolve hardware timestamps from skb
Quoted 7 lines · Kohei Enju
Move the logic that resolves a hardware timestamp from an skb, including late timestamp resolution via
netdev_get_tstamp(), fromnet/socket.cto a common helper.Let's allow other networking code to reuse the same resolution path.
Signed-off-by Kohei Enju <[email protected]>
Thanks for the fix series.
Fixes require a Fixes tag. See also https://www.kernel.org/doc/html/latest/process/maintainer-netdev.html.
I suggest merging this and the second patch, as this is not a standalone fix, and the other is a one-line change.
Quoted 81 lines
--- include/linux/skbuff.h | 11 +++++++++++ net/core/skbuff.c | 27 +++++++++++++++++++++++++++ net/socket.c | 27 +++------------------------ 3 files changed, 41 insertions(+), 24 deletions(-)include/linux/skbuff.h +11
-0@@ -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 * net/core/skbuff.c +27
-0@@ -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) {net/socket.c +0
-7@@ -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) {
net/socket.c static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp) · line 880
Quoted 10 lines
net/socket.c +1
-8- *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); }
At this point simpler to remove get_timestamp entirely. It's an unnecessary layer of indirection.
Perhaps pass sk_tsflags rather than bool to skb_get_hwtstamp.
Quoted 36 lines
net/socket.c +2
-9static 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,Signature · 1 line
2.53.0
Re: [PATCH net v1 1/3] net: introduce helper to resolve hardware timestamps from skb
Quoted 7 lines · Willem de Bruijn
Quoted 6 lines · Kohei Enju
Move the logic that resolves a hardware timestamp from an skb, including late timestamp resolution via
netdev_get_tstamp(), fromnet/socket.cto a common helper.Let's allow other networking code to reuse the same resolution path.
Quoted 8 lines
Quoted 1 line
Signed-off-by Kohei Enju <[email protected]>Thanks for the fix series.
Fixes require a Fixes tag. See also https://www.kernel.org/doc/html/latest/process/maintainer-netdev.html.
I suggest merging this and the second patch, as this is not a standalone fix, and the other is a one-line change.
Thank you for the suggestion, and it looks good to me. I'll do so in v2.
Quoted 96 lines
Quoted 82 lines
--- include/linux/skbuff.h | 11 +++++++++++ net/core/skbuff.c | 27 +++++++++++++++++++++++++++ net/socket.c | 27 +++------------------------ 3 files changed, 41 insertions(+), 24 deletions(-)include/linux/skbuff.h +11
-0@@ -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 * net/core/skbuff.c +27
-0@@ -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) {net/socket.c +0
-7@@ -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) {net/socket.c static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp) · line 880
Quoted 10 lines
net/socket.c +1
-8- *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); }At this point simpler to remove
get_timestampentirely. It's an unnecessary layer of indirection.
Quoted 1 line
Perhaps pass
sk_tsflagsrather than bool toskb_get_hwtstamp.
Good points. I'll rework this in v2.
Thanks,
Kohei
Quoted 39 lines
Quoted 37 lines
net/socket.c +2
-9static 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,Signature · 1 line
2.53.0
Re: [PATCH net v1 1/3] net: introduce helper to resolve hardware timestamps from skb
In reply to Kohei EnjuQuoted 41 lines · Kohei Enju
Move the logic that resolves a hardware timestamp from an skb, including late timestamp resolution via
netdev_get_tstamp(), fromnet/socket.cto a common helper.Let's allow other networking code to reuse the same resolution path.
Signed-off-by Kohei Enju <[email protected]>--- include/linux/skbuff.h | 11 +++++++++++ net/core/skbuff.c | 27 +++++++++++++++++++++++++++ net/socket.c | 27 +++------------------------ 3 files changed, 41 insertions(+), 24 deletions(-)include/linux/skbuff.h +11
-0@@ -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 * net/core/skbuff.c +0
-0@@ -5729,6 +5729,33 @@ void skb_tstamp_tx(struct sk_buff *orig_skb, }
net/core/skbuff.c void skb_tstamp_tx(struct sk_buff *orig_skb, · line 5739
Quoted 10 lines
net/core/skbuff.c +8
-0EXPORT_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.
Quoted 50 lines
net/core/skbuff.c +19
-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) {net/socket.c +1
-15@@ -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,
net/socket.c int skb_get_tx_timestamp(struct sk_buff *skb, struct sock *sk, · line 943
Quoted 4 lines
net/socket.c +0
-1@@ -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.
Quoted 17 lines
net/socket.c +1
-4if ((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))) &&
net/socket.c void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk, · line 1021
Quoted 10 lines
net/socket.c +1
-4!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
PATCH 2/3 af_packet: use skb_get_hwtstamp() for hardware timestamps
In reply to Kohei EnjuSince commit 97dc7cd92ac6 ("ptp: Support late timestamp determination"), skb_shared_hwtstamps may contain netdev_data instead of hwtstamp. tpacket_get_timestamp() unconditionally interprets skb_shared_hwtstamps as a resolved hardware timestamp, and can report bogus hardware timestamps to userspace.
Use skb_get_hwtstamp() instead of reading hwtstamp directly, so packet sockets follow the same hardware timestamp resolution path as the socket layer.
Note that skb_get_hwtstamp() is called with cycles == false, since AF_PACKET hasn't honored SOF_TIMESTAMPING_BIND_PHC so far, and this patch doesn't change that behavior.
--- net/packet/af_packet.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/packet/af_packet.c +1 -1
@@ -458,7 +458,7 @@ static __u32 tpacket_get_timestamp(struct sk_buff *skb, struct timespec64 *ts,if (shhwtstamps && (flags &SOF_TIMESTAMPING_RAW_HARDWARE ) && -ktime_to_timespec64_cond (shhwtstamps->hwtstamp, ts)) +ktime_to_timespec64_cond (skb_get_hwtstamp (skb, false,NULL ), ts))return TP_STATUS_TS_RAW_HARDWARE ; if ((flags & SOF_TIMESTAMPING_SOFTWARE) &&
Signature · 1 line
2.53.0
PATCH 3/3 tcp: use skb_get_hwtstamp() for hardware timestamps
In reply to Kohei EnjuSince commit 97dc7cd92ac6 ("ptp: Support late timestamp determination"), skb_shared_hwtstamps may contain netdev_data instead of hwtstamp. TCP receive timestamping can then interpret the stored value as a ktime_t and report bogus hardware timestamps to userspace.
Use skb_get_hwtstamp() instead of reading hwtstamp directly, so TCP sockets follow the same hardware timestamp resolution path as the socket layer. When coalescing SKBs, resolve late timestamps before copying them to the merged skb. Additionally, recognize SKBTX_HW_TSTAMP_NETDEV as indicating a receive timestamp is present.
Note that skb_get_hwtstamp() is called with cycles == false, since TCP hasn't honored SOF_TIMESTAMPING_BIND_PHC so far, and this patch doesn't change that behavior.
--- include/net/tcp.h | 2 +- net/ipv4/tcp_input.c | 3 ++- net/ipv4/tcp_ipv4.c | 6 ++++-- net/ipv6/tcp_ipv6.c | 3 ++- 4 files changed, 9 insertions(+), 5 deletions(-)
include/net/tcp.h +1 -1
@@ -524,7 +524,7 @@ tcp_update_recv_tstamps(struct sk_buff *skb, struct scm_timestamping_internal *tss) { tss->ts[0 ] = skb->tstamp; - tss->ts[2 ] =skb_hwtstamps (skb)->hwtstamp; + tss->ts[2 ] =skb_get_hwtstamp (skb, false,NULL ); } void tcp_recv_timestamp(struct msghdr *msg, const struct sock *sk,
net/ipv4/tcp_input.c +2 -1
@@ -5237,7 +5237,8 @@ static bool tcp_try_coalesce(struct sock *sk,if (TCP_SKB_CB (from)->has_rxtstamp) {TCP_SKB_CB (to)->has_rxtstamp = true; to->tstamp = from->tstamp; -skb_hwtstamps (to)->hwtstamp =skb_hwtstamps (from)->hwtstamp; +skb_hwtstamps (to)->hwtstamp =skb_get_hwtstamp (from, false,NULL ); +skb_shinfo (to)->tx_flags &= ~SKBTX_HW_TSTAMP_NETDEV ; }return true;
net/ipv4/tcp_ipv4.c +4 -2
@@ -1993,7 +1993,8 @@ enum skb_drop_reason tcp_add_backlog(struct sock *sk, struct sk_buff *skb)if (TCP_SKB_CB (skb)->has_rxtstamp) {TCP_SKB_CB (tail)->has_rxtstamp = true; tail->tstamp = skb->tstamp; -skb_hwtstamps (tail)->hwtstamp =skb_hwtstamps (skb)->hwtstamp; +skb_hwtstamps (tail)->hwtstamp =skb_get_hwtstamp (skb, false,NULL ); +skb_shinfo (tail)->tx_flags &= ~SKBTX_HW_TSTAMP_NETDEV ; }/* Not as strict as GRO. We only need to carry mss max value */
@@ -2062,7 +2063,8 @@ static void tcp_v4_fill_cb(struct sk_buff *skb, const struct iphdr *iph,TCP_SKB_CB (skb)->ip_dsfield =ipv4_get_dsfield (iph);TCP_SKB_CB (skb)->sacked =0 ;TCP_SKB_CB (skb)->has_rxtstamp = - skb->tstamp ||skb_hwtstamps (skb)->hwtstamp; + skb->tstamp ||skb_hwtstamps (skb)->hwtstamp || + (skb_shinfo (skb)->tx_flags &SKBTX_HW_TSTAMP_NETDEV ); }/*
net/ipv6/tcp_ipv6.c +2 -1
@@ -1704,7 +1704,8 @@ static void tcp_v6_fill_cb(struct sk_buff *skb, const struct ipv6hdr *hdr,TCP_SKB_CB (skb)->ip_dsfield =ipv6_get_dsfield (hdr);TCP_SKB_CB (skb)->sacked =0 ;TCP_SKB_CB (skb)->has_rxtstamp = - skb->tstamp ||skb_hwtstamps (skb)->hwtstamp; + skb->tstamp ||skb_hwtstamps (skb)->hwtstamp || + (skb_shinfo (skb)->tx_flags &SKBTX_HW_TSTAMP_NETDEV ); } INDIRECT_CALLABLE_SCOPE int tcp_v6_rcv(struct sk_buff *skb)
Signature · 1 line
2.53.0
Willem de Bruijn
maintainerRe: [PATCH net v1 3/3] tcp: use skb_get_hwtstamp() for hardware timestamps
Quoted 9 lines · Kohei Enju
Since commit
97dc7cd92ac6("ptp: Support late timestamp determination"),skb_shared_hwtstampsmay containnetdev_datainstead of hwtstamp. TCP receive timestamping can then interpret the stored value as aktime_tand report bogus hardware timestamps to userspace.Use
skb_get_hwtstamp()instead of reading hwtstamp directly, so TCP sockets follow the same hardware timestamp resolution path as the socket layer. When coalescing SKBs, resolve late timestamps before copying them to the merged skb.
Why? Does this preclude supporting SOF_TIMESTAMPING_BIND_PHC for such sockets at a later time?
It is just as easy to coalesce the cookie as the htwtstamp.
That also avoids the need to mask out SKBTX_HW_TSTAMP_NETDEV.
Quoted 2 lines
Additionally, recognize
SKBTX_HW_TSTAMP_NETDEVas
indicating a receive timestamp is present.
What is the reason for this extra condition?
Quoted 81 lines
Note that
skb_get_hwtstamp()is called with cycles == false, since TCP hasn't honoredSOF_TIMESTAMPING_BIND_PHCso far, and this patch doesn't change that behavior.Fixes 97dc7cd92ac6 ("ptp: Support late timestamp determination")Signed-off-by Kohei Enju <[email protected]>--- include/net/tcp.h | 2 +- net/ipv4/tcp_input.c | 3 ++- net/ipv4/tcp_ipv4.c | 6 ++++-- net/ipv6/tcp_ipv6.c | 3 ++- 4 files changed, 9 insertions(+), 5 deletions(-)include/net/tcp.h +1
-1@@ -524,7 +524,7 @@ tcp_update_recv_tstamps(struct sk_buff *skb, struct scm_timestamping_internal *tss) { tss->ts[0 ] = skb->tstamp; - tss->ts[2 ] =skb_hwtstamps (skb)->hwtstamp; + tss->ts[2 ] =skb_get_hwtstamp (skb, false,NULL ); } void tcp_recv_timestamp(struct msghdr *msg, const struct sock *sk,net/ipv4/tcp_input.c +2
-1@@ -5237,7 +5237,8 @@ static bool tcp_try_coalesce(struct sock *sk,if (TCP_SKB_CB (from)->has_rxtstamp) {TCP_SKB_CB (to)->has_rxtstamp = true; to->tstamp = from->tstamp; -skb_hwtstamps (to)->hwtstamp =skb_hwtstamps (from)->hwtstamp; +skb_hwtstamps (to)->hwtstamp =skb_get_hwtstamp (from, false,NULL ); +skb_shinfo (to)->tx_flags &= ~SKBTX_HW_TSTAMP_NETDEV ; }return true;net/ipv4/tcp_ipv4.c +4
-2@@ -1993,7 +1993,8 @@ enum skb_drop_reason tcp_add_backlog(struct sock *sk, struct sk_buff *skb)if (TCP_SKB_CB (skb)->has_rxtstamp) {TCP_SKB_CB (tail)->has_rxtstamp = true; tail->tstamp = skb->tstamp; -skb_hwtstamps (tail)->hwtstamp =skb_hwtstamps (skb)->hwtstamp; +skb_hwtstamps (tail)->hwtstamp =skb_get_hwtstamp (skb, false,NULL ); +skb_shinfo (tail)->tx_flags &= ~SKBTX_HW_TSTAMP_NETDEV ; }/* Not as strict as GRO. We only need to carry mss max value */ @@ -2062,7 +2063,8 @@ static void tcp_v4_fill_cb(struct sk_buff *skb, const struct iphdr *iph,TCP_SKB_CB (skb)->ip_dsfield =ipv4_get_dsfield (iph);TCP_SKB_CB (skb)->sacked =0 ;TCP_SKB_CB (skb)->has_rxtstamp = - skb->tstamp ||skb_hwtstamps (skb)->hwtstamp; + skb->tstamp ||skb_hwtstamps (skb)->hwtstamp || + (skb_shinfo (skb)->tx_flags &SKBTX_HW_TSTAMP_NETDEV ); }/* net/ipv6/tcp_ipv6.c +2
-1@@ -1704,7 +1704,8 @@ static void tcp_v6_fill_cb(struct sk_buff *skb, const struct ipv6hdr *hdr,TCP_SKB_CB (skb)->ip_dsfield =ipv6_get_dsfield (hdr);TCP_SKB_CB (skb)->sacked =0 ;TCP_SKB_CB (skb)->has_rxtstamp = - skb->tstamp ||skb_hwtstamps (skb)->hwtstamp; + skb->tstamp ||skb_hwtstamps (skb)->hwtstamp || + (skb_shinfo (skb)->tx_flags &SKBTX_HW_TSTAMP_NETDEV ); } INDIRECT_CALLABLE_SCOPE int tcp_v6_rcv(struct sk_buff *skb)Signature · 1 line
2.53.0
Re: [PATCH net v1 3/3] tcp: use skb_get_hwtstamp() for hardware timestamps
Quoted 6 lines · Willem de Bruijn
Quoted 5 lines · Kohei Enju
Since commit
97dc7cd92ac6("ptp: Support late timestamp determination"),skb_shared_hwtstampsmay containnetdev_datainstead of hwtstamp. TCP receive timestamping can then interpret the stored value as aktime_tand report bogus hardware timestamps to userspace.
Quoted 7 lines
Quoted 4 lines
Use
skb_get_hwtstamp()instead of reading hwtstamp directly, so TCP sockets follow the same hardware timestamp resolution path as the socket layer. When coalescing SKBs, resolve late timestamps before copying them to the merged skb.Why? Does this preclude supporting
SOF_TIMESTAMPING_BIND_PHCfor such sockets at a later time?
You're right. If we want to keep the door open for SOF_TIMESTAMPING_BIND_PHC on coalesced skbs, resolving the timestamp at coalescing time is the wrong approach.
Quoted 4 lines
It is just as easy to coalesce the cookie as the htwtstamp.
That also avoids the need to mask out
SKBTX_HW_TSTAMP_NETDEV.
Indeed. We should also carry the matching @napi_id, since skb_get_hwtstamp() uses it for late resolution.
I'll update this in v2.
Quoted 5 lines
Quoted 3 lines
Additionally, recognize
SKBTX_HW_TSTAMP_NETDEVas
indicating a receive timestamp is present.What is the reason for this extra condition?
With late timestamp determination, skb_shared_hwtstamps may hold netdev_data instead of a resolved hwtstamp, and 0 can be a valid cookie value. So checking only skb_hwtstamps(skb)->hwtstamp is not enough to detect the presence of an RX hardware timestamp.
Quoted 8 lines
Quoted 8 lines
Note that
skb_get_hwtstamp()is called with cycles == false, since TCP hasn't honoredSOF_TIMESTAMPING_BIND_PHCso far, and this patch doesn't change that behavior.Fixes 97dc7cd92ac6 ("ptp: Support late timestamp determination")Signed-off-by Kohei Enju <[email protected]>---
[...]
Re: [PATCH net v1 3/3] tcp: use skb_get_hwtstamp() for hardware timestamps
Quoted 21 lines · Kohei Enju
Quoted 6 lines · Willem de Bruijn
Quoted 5 lines · Kohei Enju
Since commit
97dc7cd92ac6("ptp: Support late timestamp determination"),skb_shared_hwtstampsmay containnetdev_datainstead of hwtstamp. TCP receive timestamping can then interpret the stored value as aktime_tand report bogus hardware timestamps to userspace.Quoted 7 lines
Quoted 4 lines
Use
skb_get_hwtstamp()instead of reading hwtstamp directly, so TCP sockets follow the same hardware timestamp resolution path as the socket layer. When coalescing SKBs, resolve late timestamps before copying them to the merged skb.Why? Does this preclude supporting
SOF_TIMESTAMPING_BIND_PHCfor such sockets at a later time?You're right. If we want to keep the door open for
SOF_TIMESTAMPING_BIND_PHCon coalesced skbs, resolving the timestamp at coalescing time is the wrong approach.Quoted 1 line
Quoted 8 lines
Quoted 3 lines
It is just as easy to coalesce the cookie as the htwtstamp.
That also avoids the need to mask out
SKBTX_HW_TSTAMP_NETDEV.Indeed. We should also carry the matching @napi_id, since
skb_get_hwtstamp()uses it for late resolution.I'll update this in v2.
Do you plan adding a selftest ?
I am confused by your series, a test would really help clarify the issue at hand.
Re: [PATCH net v1 3/3] tcp: use skb_get_hwtstamp() for hardware timestamps
Quoted 29 lines · Eric Dumazet
Quoted 28 lines · Kohei Enju
Quoted 6 lines · Willem de Bruijn
Quoted 5 lines · Kohei Enju
Since commit
97dc7cd92ac6("ptp: Support late timestamp determination"),skb_shared_hwtstampsmay containnetdev_datainstead of hwtstamp. TCP receive timestamping can then interpret the stored value as aktime_tand report bogus hardware timestamps to userspace.Quoted 7 lines
Quoted 4 lines
Use
skb_get_hwtstamp()instead of reading hwtstamp directly, so TCP sockets follow the same hardware timestamp resolution path as the socket layer. When coalescing SKBs, resolve late timestamps before copying them to the merged skb.Why? Does this preclude supporting
SOF_TIMESTAMPING_BIND_PHCfor such sockets at a later time?You're right. If we want to keep the door open for
SOF_TIMESTAMPING_BIND_PHCon coalesced skbs, resolving the timestamp at coalescing time is the wrong approach.Quoted 4 lines
It is just as easy to coalesce the cookie as the htwtstamp.
That also avoids the need to mask out
SKBTX_HW_TSTAMP_NETDEV.Indeed. We should also carry the matching @napi_id, since
skb_get_hwtstamp()uses it for late resolution.
Quoted 6 lines
Quoted 1 line
I'll update this in v2.
Do you plan adding a selftest ?
I am confused by your series, a test would really help clarify the issue at hand.
Yes, I agree a selftest would help.
Since this issue is only triggered with drivers that use late RX timestamp determination (SKBTX_HW_TSTAMP_NETDEV / ndo_get_tstamp), it would likely need to live under tools/testing/selftests/drivers/net/hw/ rather than as a generic net selftest. I'm looking into whether I can add such a test.
Either way, I'll expand the cover letter to explain the issue in more detail. Sorry for the confusion.