Netdev List
 help / color / mirror / Atom feed
* [PATCH v11 net-next 0/3] Implement PTP support in netdevsim
@ 2026-10-31 16:28 Maciek Machnikowski
  2026-10-31 16:28 ` [PATCH v11 net-next 1/3] ptp_mock: Expose ptp_clock_info to external drivers Maciek Machnikowski
                   ` (4 more replies)
  0 siblings, 5 replies; 9+ messages in thread
From: Maciek Machnikowski @ 2026-10-31 16:28 UTC (permalink / raw)
  To: netdev
  Cc: kuba, maciek, richardcochran, milena.olech, willemdebruijn.kernel,
	andrew, vadim.fedorenko, horms

This patchset adds support to the PTP HW timestamping emulation in the
netdevsim. It uses existing binding between netdevsim and ptp_mock
driver to generate transmit and receive timestamps.

It also adds the selftest to verify the hw timestamping functionality
running over netdevsim.

v11:
- Avoid aliasing the RX hwtstamp write with AF_XDP completion metadata
  (skb_shared_info's hwtstamps/xsk_meta union) by orphaning the skb
  first, addressing the Critical finding from Simon Horman's review.
  Link: https://lore.kernel.org/netdev/20260819101706.GP265046@horms.kernel.org/
- Only unshare the skb when an RX timestamp is written into it
- Reject non-zero hwtstamp flags and accept the PTP V2 L2 RX filters
- Use READ_ONCE() in ndo_hwtstamp_get and stop overwriting the whole
  config struct
- Skip the selftest if either the local or the remote device lacks
  hardware timestamping support
- Forward declare struct ptp_clock_info in ptp_mock.h
- Fix the ptp_mock commit message wording

v10:
- Drop the local_irq_save()/local_irq_restore() around the PHC reads.

v9:
- Unshare the skb instead of copying it
- Move the PTP timestamping off the xmit fast path
- Sample the TX and RX PHCs with IRQs disabled to keep the reads adjacent
- Validate the hwtstamp config before touching device state
- Report a -1 PHC index from ethtool for ports without a PHC (VFs)
- Clamp the ptp4l poll timeout in the selftest to a non-negative value

v8:
- Fix the lockless tstamp_config access in netdevsim with
  READ_ONCE()/WRITE_ONCE()
- Clarify the TX/RX timestamp handling in netdevsim comments

v7:
- Moved the test script to tools/testing/selftest/drivers/net
- Optimized implementation to avoid potential edge cases reported

v6:
- Enable PTP Mock in the tools/testing/selftests/net/config

v5:
- Rebase

v4:
- Check if Rx timestamps are enabled before generating a timestamp
- Replace bash selftest script with a python one
- Optimized Tx timestamp generation

v3:
- Fixed shellcheck issues in the selftest/net/ptp.sh
- Added selftest/net/ptp.sh to the selftest/net/Makefile
- Modified ptp_mock to use spin_lock_bh

v2:
- Added selftest/net/ptp.sh
- Modified ptp_mock to use spin_lock_bh
- Populate ethtool defaults using ethtool_op_get_ts_info

Maciek Machnikowski (3):
  ptp_mock: Expose ptp_clock_info to external drivers
  netdevsim: Implement basic ptp support
  selftests: drivers/net: Implement ptp4l sync test using netdevsim

 drivers/net/netdevsim/ethtool.c              |  15 +++
 drivers/net/netdevsim/netdev.c               | 118 +++++++++++++++++++
 drivers/net/netdevsim/netdevsim.h            |   1 +
 drivers/ptp/ptp_mock.c                       |  26 ++--
 include/linux/ptp_mock.h                     |   7 ++
 tools/testing/selftests/drivers/net/Makefile |   1 +
 tools/testing/selftests/drivers/net/config   |   1 +
 tools/testing/selftests/drivers/net/ptp.py   |  97 +++++++++++++++
 8 files changed, 258 insertions(+), 8 deletions(-)
 create mode 100755 tools/testing/selftests/drivers/net/ptp.py

-- 
2.56.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v11 net-next 1/3] ptp_mock: Expose ptp_clock_info to external drivers
  2026-10-31 16:28 [PATCH v11 net-next 0/3] Implement PTP support in netdevsim Maciek Machnikowski
@ 2026-10-31 16:28 ` Maciek Machnikowski
  2026-11-03  2:49   ` Mina Almasry
  2026-10-31 16:28 ` [PATCH v11 net-next 2/3] netdevsim: Implement basic ptp support Maciek Machnikowski
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 9+ messages in thread
From: Maciek Machnikowski @ 2026-10-31 16:28 UTC (permalink / raw)
  To: netdev
  Cc: kuba, maciek, richardcochran, milena.olech, willemdebruijn.kernel,
	andrew, vadim.fedorenko, horms

Allow exposing the ptp_clock_info of the ptp_mock to the external drivers.
Use spin_lock_irqsave() in the PHC callbacks to allow gettime to be called
from the netdevsim transmit path.
This is a prerequisite for implementing ptp support on netdevsim.

Co-developed-by: Milena Olech <milena.olech@intel.com>
Signed-off-by: Milena Olech <milena.olech@intel.com>
Signed-off-by: Maciek Machnikowski <maciek@machnikowski.net>
Reviewed-by: Vadim Fedorenko <vadim.fedorenko@linux.dev>
---
 drivers/ptp/ptp_mock.c   | 26 ++++++++++++++++++--------
 include/linux/ptp_mock.h |  7 +++++++
 2 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/drivers/ptp/ptp_mock.c b/drivers/ptp/ptp_mock.c
index 4d66b6147121..7a4e5f3274a6 100644
--- a/drivers/ptp/ptp_mock.c
+++ b/drivers/ptp/ptp_mock.c
@@ -49,15 +49,16 @@ static u64 mock_phc_cc_read(struct cyclecounter *cc)
 static int mock_phc_adjfine(struct ptp_clock_info *info, long scaled_ppm)
 {
 	struct mock_phc *phc = info_to_phc(info);
+	unsigned long flags;
 	s64 adj;
 
 	adj = (s64)scaled_ppm << MOCK_PHC_FADJ_SHIFT;
 	adj = div_s64(adj, MOCK_PHC_FADJ_DENOMINATOR);
 
-	spin_lock(&phc->lock);
+	spin_lock_irqsave(&phc->lock, flags);
 	timecounter_read(&phc->tc);
 	phc->cc.mult = MOCK_PHC_CC_MULT + adj;
-	spin_unlock(&phc->lock);
+	spin_unlock_irqrestore(&phc->lock, flags);
 
 	return 0;
 }
@@ -65,10 +66,11 @@ static int mock_phc_adjfine(struct ptp_clock_info *info, long scaled_ppm)
 static int mock_phc_adjtime(struct ptp_clock_info *info, s64 delta)
 {
 	struct mock_phc *phc = info_to_phc(info);
+	unsigned long flags;
 
-	spin_lock(&phc->lock);
+	spin_lock_irqsave(&phc->lock, flags);
 	timecounter_adjtime(&phc->tc, delta);
-	spin_unlock(&phc->lock);
+	spin_unlock_irqrestore(&phc->lock, flags);
 
 	return 0;
 }
@@ -78,10 +80,11 @@ static int mock_phc_settime64(struct ptp_clock_info *info,
 {
 	struct mock_phc *phc = info_to_phc(info);
 	u64 ns = timespec64_to_ns(ts);
+	unsigned long flags;
 
-	spin_lock(&phc->lock);
+	spin_lock_irqsave(&phc->lock, flags);
 	timecounter_init(&phc->tc, &phc->cc, ns);
-	spin_unlock(&phc->lock);
+	spin_unlock_irqrestore(&phc->lock, flags);
 
 	return 0;
 }
@@ -89,11 +92,12 @@ static int mock_phc_settime64(struct ptp_clock_info *info,
 static int mock_phc_gettime64(struct ptp_clock_info *info, struct timespec64 *ts)
 {
 	struct mock_phc *phc = info_to_phc(info);
+	unsigned long flags;
 	u64 ns;
 
-	spin_lock(&phc->lock);
+	spin_lock_irqsave(&phc->lock, flags);
 	ns = timecounter_read(&phc->tc);
-	spin_unlock(&phc->lock);
+	spin_unlock_irqrestore(&phc->lock, flags);
 
 	*ts = ns_to_timespec64(ns);
 
@@ -171,5 +175,11 @@ void mock_phc_destroy(struct mock_phc *phc)
 }
 EXPORT_SYMBOL_GPL(mock_phc_destroy);
 
+struct ptp_clock_info *mock_phc_get_ptp_info(struct mock_phc *phc)
+{
+	return &phc->info;
+}
+EXPORT_SYMBOL_GPL(mock_phc_get_ptp_info);
+
 MODULE_DESCRIPTION("Mock-up PTP Hardware Clock driver");
 MODULE_LICENSE("GPL");
diff --git a/include/linux/ptp_mock.h b/include/linux/ptp_mock.h
index 72eb401034d9..8f5d248db65c 100644
--- a/include/linux/ptp_mock.h
+++ b/include/linux/ptp_mock.h
@@ -10,12 +10,14 @@
 
 struct device;
 struct mock_phc;
+struct ptp_clock_info;
 
 #if IS_ENABLED(CONFIG_PTP_1588_CLOCK_MOCK)
 
 struct mock_phc *mock_phc_create(struct device *dev);
 void mock_phc_destroy(struct mock_phc *phc);
 int mock_phc_index(struct mock_phc *phc);
+struct ptp_clock_info *mock_phc_get_ptp_info(struct mock_phc *phc);
 
 #else
 
@@ -33,6 +35,11 @@ static inline int mock_phc_index(struct mock_phc *phc)
 	return -1;
 }
 
+static inline struct ptp_clock_info *mock_phc_get_ptp_info(struct mock_phc *phc)
+{
+	return NULL;
+}
+
 #endif
 
 #endif /* _PTP_MOCK_H_ */
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v11 net-next 2/3] netdevsim: Implement basic ptp support
  2026-10-31 16:28 [PATCH v11 net-next 0/3] Implement PTP support in netdevsim Maciek Machnikowski
  2026-10-31 16:28 ` [PATCH v11 net-next 1/3] ptp_mock: Expose ptp_clock_info to external drivers Maciek Machnikowski
@ 2026-10-31 16:28 ` Maciek Machnikowski
  2026-11-03  2:49   ` Mina Almasry
  2026-10-31 16:28 ` [PATCH v11 net-next 3/3] selftests: drivers/net: Implement ptp4l sync test using netdevsim Maciek Machnikowski
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 9+ messages in thread
From: Maciek Machnikowski @ 2026-10-31 16:28 UTC (permalink / raw)
  To: netdev
  Cc: kuba, maciek, richardcochran, milena.olech, willemdebruijn.kernel,
	andrew, vadim.fedorenko, horms

Add support for virtual timestamping inside the netdevsim driver.
The implementation uses two attached ptp_mock clocks, reads the timestamps
of the ones attached either to the netdevsim or its peer and returns
timestamps using standard timestamps APIs.

This implementation enables running ptp4l on netdevsim adapters and
introduces a new ptp selftest.

skb_hwtstamps(skb)->hwtstamp aliases with skb_shinfo(skb)->xsk_meta in
the skb_shared_info union. For an AF_XDP generic-xmit skb forwarded to
a peer with RX timestamping enabled, writing the RX timestamp into
that union turns xsk_meta.tx_timestamp into a PHC-derived value that
the AF_XDP destructor (xsk_destruct_skb()) later dereferences as a
pointer, corrupting memory. Orphan the skb before writing the RX
timestamp so any pending destructor (e.g. the AF_XDP completion) runs
against the original, unmodified union contents first.

Co-developed-by: Milena Olech <milena.olech@intel.com>
Signed-off-by: Milena Olech <milena.olech@intel.com>
Signed-off-by: Maciek Machnikowski <maciek@machnikowski.net>
---
 drivers/net/netdevsim/ethtool.c   |  15 ++++
 drivers/net/netdevsim/netdev.c    | 118 ++++++++++++++++++++++++++++++
 drivers/net/netdevsim/netdevsim.h |   1 +
 3 files changed, 134 insertions(+)

diff --git a/drivers/net/netdevsim/ethtool.c b/drivers/net/netdevsim/ethtool.c
index c3d2bc783d55..3f1f1510b6f8 100644
--- a/drivers/net/netdevsim/ethtool.c
+++ b/drivers/net/netdevsim/ethtool.c
@@ -200,7 +200,22 @@ static int nsim_get_ts_info(struct net_device *dev,
 {
 	struct netdevsim *ns = netdev_priv(dev);
 
+	ethtool_op_get_ts_info(dev, info);
+	if (!ns->phc) {
+		info->phc_index = -1;
+		return 0;
+	}
+
 	info->phc_index = mock_phc_index(ns->phc);
+	if (info->phc_index < 0)
+		return 0;
+
+	info->so_timestamping |= SOF_TIMESTAMPING_TX_HARDWARE |
+				 SOF_TIMESTAMPING_RX_HARDWARE |
+				 SOF_TIMESTAMPING_RAW_HARDWARE;
+
+	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON);
+	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
 
 	return 0;
 }
diff --git a/drivers/net/netdevsim/netdev.c b/drivers/net/netdevsim/netdev.c
index eebc02ccc4a9..812569a0d497 100644
--- a/drivers/net/netdevsim/netdev.c
+++ b/drivers/net/netdevsim/netdev.c
@@ -30,6 +30,8 @@
 #include <net/rtnetlink.h>
 #include <net/udp_tunnel.h>
 #include <net/busy_poll.h>
+#include <linux/ptp_clock_kernel.h>
+#include <linux/timecounter.h>
 
 #include "netdevsim.h"
 
@@ -122,7 +124,11 @@ static int nsim_forward_skb(struct net_device *tx_dev,
 
 static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)
 {
+	struct skb_shared_hwtstamps shhwtstamps = {};
+	struct ptp_clock_info *ptp_info_tx = NULL;
+	struct ptp_clock_info *ptp_info_rx = NULL;
 	struct netdevsim *ns = netdev_priv(dev);
+	struct timespec64 tx_ts, rx_ts;
 	struct skb_ext *psp_ext = NULL;
 	struct net_device *peer_dev;
 	unsigned int len = skb->len;
@@ -164,6 +170,49 @@ static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)
 		skb_linearize(skb);
 
 	skb_tx_timestamp(skb);
+
+	if (unlikely(READ_ONCE(peer_ns->tstamp_config.rx_filter) !=
+		     HWTSTAMP_FILTER_NONE))
+		ptp_info_rx = mock_phc_get_ptp_info(peer_ns->phc);
+
+	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
+	    READ_ONCE(ns->tstamp_config.tx_type) == HWTSTAMP_TX_ON)
+		ptp_info_tx = mock_phc_get_ptp_info(ns->phc);
+
+	if (unlikely(ptp_info_tx || ptp_info_rx)) {
+		/* Capture both timestamps closely */
+		if (ptp_info_tx)
+			ptp_info_tx->gettime64(ptp_info_tx, &tx_ts);
+		if (ptp_info_rx)
+			ptp_info_rx->gettime64(ptp_info_rx, &rx_ts);
+
+		/* If TX hardware timestamping is enabled report TX timestamp */
+		if (ptp_info_tx) {
+			shhwtstamps.hwtstamp = timespec64_to_ktime(tx_ts);
+			skb_tstamp_tx(skb, &shhwtstamps);
+		}
+
+		/* If RX hardware timestamping is enabled report RX timestamp */
+		if (ptp_info_rx) {
+			/* Unshare the skb, stamping it must not corrupt cloned
+			 * copies. On failure the skb is already freed.
+			 */
+			skb = skb_unclone_full(skb, GFP_ATOMIC);
+			if (unlikely(!skb)) {
+				if (psp_ext)
+					__skb_ext_put(psp_ext);
+				goto out_drop_cnt;
+			}
+
+			/* hwtstamp aliases xsk_meta; orphan first so any
+			 * pending AF_XDP completion isn't corrupted.
+			 */
+			skb_orphan(skb);
+			skb_hwtstamps(skb)->hwtstamp =
+				timespec64_to_ktime(rx_ts);
+		}
+	}
+
 	if (unlikely(nsim_forward_skb(dev, peer_dev,
 				      skb, rq, psp_ext) == NET_RX_DROP))
 		goto out_drop_cnt;
@@ -193,6 +242,73 @@ static netdev_tx_t nsim_start_xmit_vf(struct sk_buff *skb,
 	return NETDEV_TX_OK;
 }
 
+static int nsim_set_ts_config(struct net_device *netdev,
+			      struct kernel_hwtstamp_config *config,
+			      struct netlink_ext_ack *extack)
+{
+	struct netdevsim *ns = netdev_priv(netdev);
+	int rx_filter;
+
+	if (!ns->phc)
+		return -EOPNOTSUPP;
+
+	if (config->flags) {
+		NL_SET_ERR_MSG_MOD(extack, "Flags are not supported");
+		return -EINVAL;
+	}
+
+	switch (config->rx_filter) {
+	case HWTSTAMP_FILTER_NONE:
+		rx_filter = HWTSTAMP_FILTER_NONE;
+		break;
+	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
+	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
+	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
+	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
+	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
+	case HWTSTAMP_FILTER_PTP_V2_EVENT:
+	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
+	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
+	case HWTSTAMP_FILTER_PTP_V2_SYNC:
+	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
+	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
+	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
+	case HWTSTAMP_FILTER_NTP_ALL:
+	case HWTSTAMP_FILTER_ALL:
+		rx_filter = HWTSTAMP_FILTER_ALL;
+		break;
+	default:
+		return -ERANGE;
+	}
+
+	switch (config->tx_type) {
+	case HWTSTAMP_TX_OFF:
+		WRITE_ONCE(ns->tstamp_config.tx_type, HWTSTAMP_TX_OFF);
+		break;
+	case HWTSTAMP_TX_ON:
+		WRITE_ONCE(ns->tstamp_config.tx_type, HWTSTAMP_TX_ON);
+		break;
+	default:
+		return -ERANGE;
+	}
+
+	WRITE_ONCE(ns->tstamp_config.rx_filter, rx_filter);
+	config->rx_filter = rx_filter;
+
+	return 0;
+}
+
+static int nsim_get_ts_config(struct net_device *netdev,
+			      struct kernel_hwtstamp_config *config)
+{
+	struct netdevsim *ns = netdev_priv(netdev);
+
+	config->tx_type = READ_ONCE(ns->tstamp_config.tx_type);
+	config->rx_filter = READ_ONCE(ns->tstamp_config.rx_filter);
+
+	return 0;
+}
+
 static int nsim_set_rx_mode(struct net_device *dev,
 			    struct netdev_hw_addr_list *uc,
 			    struct netdev_hw_addr_list *mc)
@@ -656,6 +772,8 @@ static const struct net_device_ops nsim_netdev_ops = {
 	.ndo_vlan_rx_add_vid	= nsim_vlan_rx_add_vid,
 	.ndo_vlan_rx_kill_vid	= nsim_vlan_rx_kill_vid,
 	.net_shaper_ops		= &nsim_shaper_ops,
+	.ndo_hwtstamp_get	= nsim_get_ts_config,
+	.ndo_hwtstamp_set	= nsim_set_ts_config,
 };
 
 static const struct net_device_ops nsim_vf_netdev_ops = {
diff --git a/drivers/net/netdevsim/netdevsim.h b/drivers/net/netdevsim/netdevsim.h
index fd5599cd3bb9..b82eba97dbe5 100644
--- a/drivers/net/netdevsim/netdevsim.h
+++ b/drivers/net/netdevsim/netdevsim.h
@@ -111,6 +111,7 @@ struct netdevsim {
 	struct net_device *netdev;
 	struct nsim_dev *nsim_dev;
 	struct nsim_dev_port *nsim_dev_port;
+	struct kernel_hwtstamp_config tstamp_config;
 	struct mock_phc *phc;
 	struct nsim_rq **rq;
 
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v11 net-next 3/3] selftests: drivers/net: Implement ptp4l sync test using netdevsim
  2026-10-31 16:28 [PATCH v11 net-next 0/3] Implement PTP support in netdevsim Maciek Machnikowski
  2026-10-31 16:28 ` [PATCH v11 net-next 1/3] ptp_mock: Expose ptp_clock_info to external drivers Maciek Machnikowski
  2026-10-31 16:28 ` [PATCH v11 net-next 2/3] netdevsim: Implement basic ptp support Maciek Machnikowski
@ 2026-10-31 16:28 ` Maciek Machnikowski
  2026-11-03  2:49   ` Mina Almasry
  2026-10-31 19:12 ` [PATCH v11 net-next 0/3] Implement PTP support in netdevsim Mina Almasry
  2026-11-05 23:37 ` Jakub Kicinski
  4 siblings, 1 reply; 9+ messages in thread
From: Maciek Machnikowski @ 2026-10-31 16:28 UTC (permalink / raw)
  To: netdev
  Cc: kuba, maciek, richardcochran, milena.olech, willemdebruijn.kernel,
	andrew, vadim.fedorenko, horms

Add PTP synchronization test using ptp4l and netdevsim.
The test uses the NetDrvEpEnv to link a local netdevsim
device to a remote endpoint, runs ptp4l as leader and follower
on the two ends, and waits for the follower to report the
synchronized state (s2).

Signed-off-by: Maciek Machnikowski <maciek@machnikowski.net>
---
 tools/testing/selftests/drivers/net/Makefile |  1 +
 tools/testing/selftests/drivers/net/config   |  1 +
 tools/testing/selftests/drivers/net/ptp.py   | 97 ++++++++++++++++++++
 3 files changed, 99 insertions(+)
 create mode 100755 tools/testing/selftests/drivers/net/ptp.py

diff --git a/tools/testing/selftests/drivers/net/Makefile b/tools/testing/selftests/drivers/net/Makefile
index 79c6f998e704..ed41a69ef6ad 100644
--- a/tools/testing/selftests/drivers/net/Makefile
+++ b/tools/testing/selftests/drivers/net/Makefile
@@ -23,6 +23,7 @@ TEST_PROGS := \
 	ping.py \
 	pppoe_gro.py \
 	psp.py \
+	ptp.py \
 	queues.py \
 	ring_reconfig.py \
 	rss_key.py \
diff --git a/tools/testing/selftests/drivers/net/config b/tools/testing/selftests/drivers/net/config
index 4838adf27fa1..30e69446d41d 100644
--- a/tools/testing/selftests/drivers/net/config
+++ b/tools/testing/selftests/drivers/net/config
@@ -21,6 +21,7 @@ CONFIG_NET_SCH_INGRESS=y
 CONFIG_NET_SCH_PRIO=m
 CONFIG_PPP=y
 CONFIG_PPPOE=y
+CONFIG_PTP_1588_CLOCK_MOCK=y
 CONFIG_TLS=y
 CONFIG_VLAN_8021Q=m
 CONFIG_XDP_SOCKETS=y
diff --git a/tools/testing/selftests/drivers/net/ptp.py b/tools/testing/selftests/drivers/net/ptp.py
new file mode 100755
index 000000000000..121167256f35
--- /dev/null
+++ b/tools/testing/selftests/drivers/net/ptp.py
@@ -0,0 +1,97 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# By Maciek Machnikowski <maciek@machnikowski.net> (c) 2026
+
+"""
+Test suite for PTP sync using ptp4l.
+
+Start a ptp4l leader and follower and check that the follower locks onto the
+leader (state s2)
+"""
+
+import time
+
+from lib.py import (
+    KsftSkipEx,
+    NetDrvEpEnv,
+    bkg,
+    cmd,
+    fd_read_timeout,
+    ksft_exit,
+    ksft_pr,
+    ksft_run,
+    ksft_true,
+)
+
+PTP4L_SYNC_TIMEOUT = 40
+
+
+def _require_hw_timestamping(ifname, host=None):
+    """Skip the test unless ifname supports HW TX and RX timestamping."""
+    end = "Remote" if host else "Local"
+    ts_info = cmd(f"ethtool -T {ifname}", host=host).stdout
+    if "hardware-transmit" not in ts_info or "hardware-receive" not in ts_info:
+        raise KsftSkipEx(
+            f"{end} device does not support hardware timestamping")
+
+
+def _poll_follower_sync(follower, timeout):
+    """Read the follower stdout pipe until ptp4l reports sync state s2.
+
+    Returns a tuple (synced, output) where output is the text read so far.
+    The synchronized ptp4l log line looks like this:
+    ptp4l[18374.558]: master offset        844 s2 freq    +822 path delay       521
+    """
+    fd_file = follower.proc.stdout
+    fd = fd_file.fileno()
+    buf = b""
+    deadline = time.monotonic() + timeout
+    while time.monotonic() < deadline:
+        if b" s2 " in buf:
+            break
+        if follower.proc.poll() is not None:
+            chunk = fd_file.read()
+            if chunk:
+                buf += chunk
+            break
+        try:
+            remaining = deadline - time.monotonic()
+            buf += fd_read_timeout(fd, max(0, min(1, remaining)))
+        except TimeoutError:
+            continue
+    return b" s2 " in buf, buf.decode("utf-8", "replace")
+
+
+def ptp_sync_test(cfg):
+    """Verify ptp4l leader/follower synchronization reaches state s2."""
+    cfg.require_cmd("ptp4l", remote=True)
+    cfg.require_cmd("ethtool", remote=True)
+
+    _require_hw_timestamping(cfg.ifname)
+    _require_hw_timestamping(cfg.remote_ifname, host=cfg.remote)
+
+    leader_cmd = f"ptp4l -i {cfg.remote_ifname} -m -2"
+    follower_cmd = f"ptp4l -i {cfg.ifname} -m -s -2"
+
+    with bkg(leader_cmd, host=cfg.remote), \
+         bkg(follower_cmd) as follower:
+        synced, output = _poll_follower_sync(follower, PTP4L_SYNC_TIMEOUT)
+
+    if not synced:
+        ksft_pr(f"ptp4l follower did not reach locked state (s2) within "
+                f"{PTP4L_SYNC_TIMEOUT}s")
+        tail = output.strip().split("\n")[-10:]
+        ksft_pr("Follower log (last 10 lines): " + " | ".join(tail))
+    ksft_true(synced, "PTP sync timeout")
+
+
+def main():
+    """Run ksft tests."""
+    with NetDrvEpEnv(__file__) as cfg:
+        ksft_run([ptp_sync_test], args=(cfg, ))
+    ksft_exit()
+
+
+if __name__ == "__main__":
+    main()
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH v11 net-next 0/3] Implement PTP support in netdevsim
  2026-10-31 16:28 [PATCH v11 net-next 0/3] Implement PTP support in netdevsim Maciek Machnikowski
                   ` (2 preceding siblings ...)
  2026-10-31 16:28 ` [PATCH v11 net-next 3/3] selftests: drivers/net: Implement ptp4l sync test using netdevsim Maciek Machnikowski
@ 2026-10-31 19:12 ` Mina Almasry
  2026-11-05 23:37 ` Jakub Kicinski
  4 siblings, 0 replies; 9+ messages in thread
From: Mina Almasry @ 2026-10-31 19:12 UTC (permalink / raw)
  To: Maciek Machnikowski
  Cc: netdev, kuba, richardcochran, milena.olech, willemdebruijn.kernel,
	andrew, vadim.fedorenko, horms

On Sat, Oct 31, 2026 at 9:56 AM Maciek Machnikowski
<maciek@machnikowski.net> wrote:
>
> This patchset adds support to the PTP HW timestamping emulation in the
> netdevsim. It uses existing binding between netdevsim and ptp_mock
> driver to generate transmit and receive timestamps.
>
> It also adds the selftest to verify the hw timestamping functionality
> running over netdevsim.
>

Net-next is closed, so please repost on Nov 9th when it re-opens.

But, more importantly, I'm surprised this relatively small patch doing
something (I think) unobjectionable is on v11. What are you struggling
with? I looked at the diff log and it seems to address only
micro-bugs. What can I help with to accelerate this?

-- 
Thanks,
Mina

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v11 net-next 1/3] ptp_mock: Expose ptp_clock_info to external drivers
  2026-10-31 16:28 ` [PATCH v11 net-next 1/3] ptp_mock: Expose ptp_clock_info to external drivers Maciek Machnikowski
@ 2026-11-03  2:49   ` Mina Almasry
  0 siblings, 0 replies; 9+ messages in thread
From: Mina Almasry @ 2026-11-03  2:49 UTC (permalink / raw)
  To: Maciek Machnikowski
  Cc: netdev, kuba, richardcochran, milena.olech, willemdebruijn.kernel,
	andrew, vadim.fedorenko, horms, Mina Almasry

On Sat, Oct 31, 2026 at 9:28 AM Maciek Machnikowski <maciek@machnikowski.net> wrote:
>
> Allow exposing the ptp_clock_info of the ptp_mock to the external drivers.
> Use spin_lock_irqsave() in the PHC callbacks to allow gettime to be called
> from the netdevsim transmit path.
> This is a prerequisite for implementing ptp support on netdevsim.
>
> Co-developed-by: Milena Olech <milena.olech@intel.com>
> Signed-off-by: Milena Olech <milena.olech@intel.com>
> Signed-off-by: Maciek Machnikowski <maciek@machnikowski.net>
> Reviewed-by: Vadim Fedorenko <vadim.fedorenko@linux.dev>

Reviewed-by: Mina Almasry <almasrymina@google.com>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v11 net-next 2/3] netdevsim: Implement basic ptp support
  2026-10-31 16:28 ` [PATCH v11 net-next 2/3] netdevsim: Implement basic ptp support Maciek Machnikowski
@ 2026-11-03  2:49   ` Mina Almasry
  0 siblings, 0 replies; 9+ messages in thread
From: Mina Almasry @ 2026-11-03  2:49 UTC (permalink / raw)
  To: Maciek Machnikowski
  Cc: netdev, kuba, richardcochran, milena.olech, willemdebruijn.kernel,
	andrew, vadim.fedorenko, horms, Mina Almasry

On Sat, Oct 31, 2026 at 9:28 AM Maciek Machnikowski <maciek@machnikowski.net> wrote:
>
> Add support for virtual timestamping inside the netdevsim driver.
> The implementation uses two attached ptp_mock clocks, reads the timestamps
> of the ones attached either to the netdevsim or its peer and returns
> timestamps using standard timestamps APIs.
>
> This implementation enables running ptp4l on netdevsim adapters and
> introduces a new ptp selftest.
>
> skb_hwtstamps(skb)->hwtstamp aliases with skb_shinfo(skb)->xsk_meta in
> the skb_shared_info union. For an AF_XDP generic-xmit skb forwarded to
> a peer with RX timestamping enabled, writing the RX timestamp into
> that union turns xsk_meta.tx_timestamp into a PHC-derived value that
> the AF_XDP destructor (xsk_destruct_skb()) later dereferences as a
> pointer, corrupting memory. Orphan the skb before writing the RX
> timestamp so any pending destructor (e.g. the AF_XDP completion) runs
> against the original, unmodified union contents first.
>
> Co-developed-by: Milena Olech <milena.olech@intel.com>
> Signed-off-by: Milena Olech <milena.olech@intel.com>
> Signed-off-by: Maciek Machnikowski <maciek@machnikowski.net>

Reviewed-by: Mina Almasry <almasrymina@google.com>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v11 net-next 3/3] selftests: drivers/net: Implement ptp4l sync test using netdevsim
  2026-10-31 16:28 ` [PATCH v11 net-next 3/3] selftests: drivers/net: Implement ptp4l sync test using netdevsim Maciek Machnikowski
@ 2026-11-03  2:49   ` Mina Almasry
  0 siblings, 0 replies; 9+ messages in thread
From: Mina Almasry @ 2026-11-03  2:49 UTC (permalink / raw)
  To: Maciek Machnikowski
  Cc: netdev, kuba, richardcochran, milena.olech, willemdebruijn.kernel,
	andrew, vadim.fedorenko, horms, Mina Almasry

On Sat, Oct 31, 2026 at 9:28 AM Maciek Machnikowski <maciek@machnikowski.net> wrote:
>
> Add PTP synchronization test using ptp4l and netdevsim.
> The test uses the NetDrvEpEnv to link a local netdevsim
> device to a remote endpoint, runs ptp4l as leader and follower
> on the two ends, and waits for the follower to report the
> synchronized state (s2).
>
> Signed-off-by: Maciek Machnikowski <maciek@machnikowski.net>

Reviewed-by: Mina Almasry <almasrymina@google.com>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v11 net-next 0/3] Implement PTP support in netdevsim
  2026-10-31 16:28 [PATCH v11 net-next 0/3] Implement PTP support in netdevsim Maciek Machnikowski
                   ` (3 preceding siblings ...)
  2026-10-31 19:12 ` [PATCH v11 net-next 0/3] Implement PTP support in netdevsim Mina Almasry
@ 2026-11-05 23:37 ` Jakub Kicinski
  4 siblings, 0 replies; 9+ messages in thread
From: Jakub Kicinski @ 2026-11-05 23:37 UTC (permalink / raw)
  To: Maciek Machnikowski
  Cc: netdev, richardcochran, milena.olech, willemdebruijn.kernel,
	andrew, vadim.fedorenko, horms

On Sat, 31 Oct 2026 17:28:01 +0100 Maciek Machnikowski wrote:
> This patchset adds support to the PTP HW timestamping emulation in the
> netdevsim. It uses existing binding between netdevsim and ptp_mock
> driver to generate transmit and receive timestamps.

The bot will not auto-respond, please make sure to check before you
repost:
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261031162804.1893-1-maciek%40machnikowski.net

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-11-05 23:37 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-31 16:28 [PATCH v11 net-next 0/3] Implement PTP support in netdevsim Maciek Machnikowski
2026-10-31 16:28 ` [PATCH v11 net-next 1/3] ptp_mock: Expose ptp_clock_info to external drivers Maciek Machnikowski
2026-11-03  2:49   ` Mina Almasry
2026-10-31 16:28 ` [PATCH v11 net-next 2/3] netdevsim: Implement basic ptp support Maciek Machnikowski
2026-11-03  2:49   ` Mina Almasry
2026-10-31 16:28 ` [PATCH v11 net-next 3/3] selftests: drivers/net: Implement ptp4l sync test using netdevsim Maciek Machnikowski
2026-11-03  2:49   ` Mina Almasry
2026-10-31 19:12 ` [PATCH v11 net-next 0/3] Implement PTP support in netdevsim Mina Almasry
2026-11-05 23:37 ` Jakub Kicinski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox