Implement PTP support in netdevsim
Subsystems (6)
v10 cover Implement PTP support in netdevsim
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.
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_irqsave
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 | 104 +++++++++++++++++++ drivers/net/netdevsim/netdevsim.h | 1 + drivers/ptp/ptp_mock.c | 26 +++-- include/linux/ptp_mock.h | 5 + tools/testing/selftests/drivers/net/Makefile | 1 + tools/testing/selftests/drivers/net/config | 1 + tools/testing/selftests/drivers/net/ptp.py | 84 +++++++++++++++ 8 files changed, 229 insertions(+), 8 deletions(-) create mode 100755 tools/testing/selftests/drivers/net/ptp.py
--
2.55.0
PATCH v10 1/3 ptp_mock: Expose ptp_clock_info to external drivers
Allow exposing the ptp_clock_info of the ptp_mock to the external drivers. Convert spinlocks to SLIS to allow gettime to be called from the netdevsim. This is a prerequisite for implementing ptp support on netdevsim.
--- drivers/ptp/ptp_mock.c | 26 ++++++++++++++++++-------- include/linux/ptp_mock.h | 5 +++++ 2 files changed, 23 insertions(+), 8 deletions(-)
drivers/ptp/ptp_mock.c +18 -8
@@ -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" );
include/linux/ptp_mock.h +5 -0
@@ -16,6 +16,7 @@ struct mock_phc;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 +34,10 @@ 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.55.0
PATCH v10 2/3 netdevsim: Implement basic ptp support
In reply to Maciek MachnikowskiAdd 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.
--- drivers/net/netdevsim/ethtool.c | 15 +++++ drivers/net/netdevsim/netdev.c | 104 ++++++++++++++++++++++++++++++ drivers/net/netdevsim/netdevsim.h | 1 + 3 files changed, 120 insertions(+)
drivers/net/netdevsim/ethtool.c +15 -0
@@ -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 ; }
drivers/net/netdevsim/netdev.c +104 -0
135 lines not on this page · raw message
drivers/net/netdevsim/netdevsim.h +1 -0
@@ -108,6 +108,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;
Signature · 1 line
2.55.0
Simon Horman
reviewerRe: [PATCH v10 net-next 2/3] netdevsim: Implement basic ptp support
This is an AI-generated review of your patch. The human sending this
email has considered the AI review valid, or at least plausible.
Full review at: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/[email protected]?part=2
---
--- Patch [2]: [PATCH v10 net-next 2/3] netdevsim: Implement basic ptp support ---
commit ef2b7171af287d8c3a8f5064008830a7dd8f4db3
Author: Maciek Machnikowski <[email protected]>
netdevsim: Implement basic ptp support
Adds virtual hardware timestamping to netdevsim by reading the mock PHC attached to the transmitting netdevsim and to its peer, reporting a TX timestamp to the socket and stamping the forwarded skb for the RX side. Also wires up ndo_hwtstamp_get / ndo_hwtstamp_set and extends nsim_get_ts_info().
A few questions on the timestamping path below.
Quoted 5 lines
drivers/net/netdevsim/ethtool.c +0
-0
drivers/net/netdevsim/ethtool.c static int nsim_get_ts_info(struct net_device *dev, · line 208
Quoted 9 lines
drivers/net/netdevsim/ethtool.c +6
-0@@ -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 ; + } +
[Severity: High]
[Finding: main-1-1]
[Sources: opus-5]
This isn't a bug introduced by this patch, but the new !ns->phc guard looks like it silently fixes a NULL pointer dereference that exists today, without a mention in the changelog or a Fixes: tag.
Before this patch nsim_get_ts_info() did:
info->phc_index = mock_phc_index(ns->phc);
and nsim_ethtool_init() installs the shared nsim_ethtool_ops (including .get_ts_info = nsim_get_ts_info) on every netdevsim netdev, while only nsim_init_netdevsim() calls mock_phc_create(). nsim_init_netdevsim_vf() just assigns nsim_vf_netdev_ops and registers the netdev, so ns->phc stays NULL for VF netdevs.
With CONFIG_PTP_1588_CLOCK_MOCK=y, mock_phc_index() is the real function:
drivers/ptp/ptp_mock.c:mock_phc_index() {
return ptp_clock_index(phc->clock);
}so ethtool -T on a netdevsim VF netdev dereferences NULL. Would it make sense to split this guard into its own patch with a Fixes: tag so that stable tooling can pick it up?
Also, adding ethtool_op_get_ts_info() here changes ethtool -T output for all netdevsim devices by advertising SOF_TIMESTAMPING_TX_SOFTWARE, SOF_TIMESTAMPING_RX_SOFTWARE and SOF_TIMESTAMPING_SOFTWARE. Could the changelog mention that behaviour change?
Quoted 17 lines
drivers/net/netdevsim/ethtool.c +9
-0info->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 ; }drivers/net/netdevsim/netdev.c +0
-0
[ ... ]
Quoted 15 lines
drivers/net/netdevsim/netdev.c +11
-0@@ -164,6 +171,44 @@ 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 */
drivers/net/netdevsim/netdev.c static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev) · line 194
Quoted 10 lines
drivers/net/netdevsim/netdev.c +10
-0+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_orig, &shhwtstamps); + }
[Severity: Medium]
[Finding: gpt-5-6-sol-3-10]
[Sources: gpt-5-6-sol, opus-5]
Can a socket receive two SCM_TSTAMP_SND reports for the same packet here?
The hardware report is emitted after skb_tx_timestamp(), which already delivered the software one:
include/linux/skbuff.h:skb_tx_timestamp() {
skb_clone_tx_timestamp(skb);
if (skb_shinfo(skb)->tx_flags & (SKBTX_SW_TSTAMP | SKBTX_BPF))
skb_tstamp_tx(skb, NULL);
}The only suppression of the software report lives in __skb_tstamp_tx():
net/core/skbuff.c:__skb_tstamp_tx() {
if (!hwtstamps && !(tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
return;netdevsim never sets SKBTX_IN_PROGRESS anywhere - the new code only tests SKBTX_HW_TSTAMP - so for a socket that enabled both SOF_TIMESTAMPING_TX_SOFTWARE and SOF_TIMESTAMPING_TX_HARDWARE without SOF_TIMESTAMPING_OPT_TX_SWHW both reports are delivered.
Would following the usual driver sequence work here, i.e. test SKBTX_HW_TSTAMP and set SKBTX_IN_PROGRESS before calling skb_tx_timestamp()?
drivers/net/netdevsim/netdev.c static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev) · line 204
Quoted 10 lines
drivers/net/netdevsim/netdev.c +10
-0+ +/* Unshare the skb, stamping it must not corrupt cloned copies +* and prevents returning single tstamp for Tx and Rx +*/ + skb =skb_unshare (skb_orig,GFP_ATOMIC ); +if (unlikely (!skb)) { +if (psp_ext) +__skb_ext_put (psp_ext); + goto out_drop_cnt; + }
[Severity: Low]
[Finding: main-3-8]
[Sources: opus-5]
Should this skb_unshare() be gated on ptp_info_rx?
The enclosing condition is ptp_info_tx || ptp_info_rx, but the only write into the skb below is guarded by if (ptp_info_rx). On the TX-only path (peer rx_filter == HWTSTAMP_FILTER_NONE) every timestamped frame gets copied by skb_copy() for nothing, which also linearizes the buffer that the hds_config logic just above deliberately left non-linear, and does not carry queue_mapping.
If the GFP_ATOMIC copy then fails, the frame is dropped via out_drop_cnt even though nothing needed to be written into it, and after the TX timestamp was already handed to the socket by skb_tstamp_tx(skb_orig, ...).
[Severity: Low]
[Finding: main-5-12]
[Sources: opus-5]
Does skb_unshare() cover the case this comment describes?
include/linux/skbuff.h:skb_unshare() {
might_sleep_if(gfpflags_allow_blocking(pri));
if (skb_cloned(skb)) {
...
}The copy is made only for clones, so an skb with skb->users > 1 and cloned == 0 is returned unchanged. pktgen does refcount_add(burst, &pkt_dev->skb->users) and resubmits the same skb, so the skb_hwtstamps(skb) write below touches skb_shared_info that the other reference holder still owns, and that the peer's NAPI path may read concurrently after nsim_forward_skb() -> nsim_napi_rx() queues it on rq->skb_queue. Should the driver check skb_shared() as well, or stamp a private copy?
drivers/net/netdevsim/netdev.c static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev) · line 211
Quoted 7 lines
drivers/net/netdevsim/netdev.c +7
-0+ +/* If RX hardware timestamping is enabled report RX timestamp */ +if (ptp_info_rx) +skb_hwtstamps (skb)->hwtstamp = +timespec64_to_ktime (rx_ts); + } +
[Severity: Critical]
[Finding: main-6-13]
[Sources: opus-5]
Can this write corrupt kernel memory for AF_XDP TX skbs?
skb_hwtstamps() returns a pointer into a union in skb_shared_info:
include/linux/skbuff.h:struct skb_shared_info {
union {
struct skb_shared_hwtstamps hwtstamps;
struct xsk_tx_metadata_compl xsk_meta;
};struct xsk_tx_metadata_compl has a single member, u64 *tx_timestamp, and the AF_XDP destructor dereferences it with only a NULL check:
net/xdp/xsk.c:xsk_destruct_skb() {
struct xsk_tx_metadata_compl *compl = &skb_shinfo(skb)->xsk_meta;
if (compl->tx_timestamp) {
/* sw completion timestamp, not a real one */
*compl->tx_timestamp = ktime_get_tai_fast_ns();
}Every generic-xmit AF_XDP skb carries that destructor (xsk_skb_init_misc() sets skb->destructor = xsk_destruct_skb), and it is freshly allocated and not cloned, so skb_unshare() above returns the same skb with the destructor and the overwritten shinfo intact. __dev_forward_skb() -> skb_scrub_packet() does not orphan the skb or reset skb_shinfo(), so the value survives to the destructor.
The RX branch is gated only on the peer's setting:
if (unlikely(READ_ONCE(peer_ns->tstamp_config.rx_filter) != HWTSTAMP_FILTER_NONE)) ptp_info_rx = mock_phc_get_ptp_info(peer_ns->phc);
so any AF_XDP frame forwarded while the peer has RX timestamping enabled gets a non-NULL, PHC-derived value in xsk_meta.tx_timestamp, and the destructor performs an 8-byte write to that address. The mock PHC value is settable from userspace through clock_settime() on /dev/ptpN via mock_phc_settime64().
Real NICs avoid this because they only stamp skbs they allocated for RX. Since netdevsim reuses the TX skb as the RX skb, should it orphan the skb (clearing skb->destructor) or stamp a private copy before touching skb_shinfo()->hwtstamps?
Quoted 17 lines
drivers/net/netdevsim/netdev.c +10
-0if (unlikely (nsim_forward_skb (dev, peer_dev, skb, rq, psp_ext) ==NET_RX_DROP )) goto out_drop_cnt;@@ -185,6 +230,63 @@ static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)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 ; +
[ ... ]
drivers/net/netdevsim/netdev.c static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev) · line 288
Quoted 8 lines
drivers/net/netdevsim/netdev.c +8
-0+static int nsim_get_ts_config (struct net_device *netdev, +struct kernel_hwtstamp_config *config) +{ +struct netdevsim *ns =netdev_priv (netdev); + + *config = ns->tstamp_config; +return 0 ; +}
[Severity: Low]
[Finding: gpt-5-6-sol-2-6]
[Sources: gpt-5-6-sol, opus-5]
Should nsim_get_ts_config() carry the same !ns->phc check as nsim_set_ts_config()?
drivers/net/Kconfig allows netdevsim without the mock clock:
depends on PTP_1588_CLOCK_MOCK || PTP_1588_CLOCK_MOCK=n
and with CONFIG_PTP_1588_CLOCK_MOCK=n the stub is used:
include/linux/ptp_mock.h {
static inline struct mock_phc *mock_phc_create(struct device *dev)
{
return NULL;
}nsim_init_netdevsim() only rejects IS_ERR(), so ns->phc stays NULL. In that configuration SIOCGHWTSTAMP succeeds and reports tx_type = HWTSTAMP_TX_OFF / rx_filter = HWTSTAMP_FILTER_NONE, i.e. "hardware timestamping present but disabled", while nsim_get_ts_info() reports phc_index = -1 with no hardware so_timestamping and every set attempt is rejected with -EOPNOTSUPP. Other converted drivers keep the get/set pair symmetric here.
[ ... ]
Re: [PATCH v10 net-next 2/3] netdevsim: Implement basic ptp support
Quoted 56 lines · Simon Horman
This is an AI-generated review of your patch. The human sending this
email has considered the AI review valid, or at least plausible.
Full review at: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/[email protected]?part=2
---
--- Patch [2]: [PATCH v10 net-next 2/3] netdevsim: Implement basic ptp support ---
commitef2b7171af287d8c3a8f5064008830a7dd8f4db3
Author: Maciek Machnikowski <[email protected]>netdevsim: Implement basic ptp support
Adds virtual hardware timestamping to netdevsim by reading the mock PHC attached to the transmitting netdevsim and to its peer, reporting a TX timestamp to the socket and stamping the forwarded skb for the RX side. Also wires up
ndo_hwtstamp_get/ndo_hwtstamp_setand extendsnsim_get_ts_info().A few questions on the timestamping path below.
Quoted 5 lines
drivers/net/netdevsim/ethtool.c +0
-0drivers/net/netdevsim/ethtool.c static int nsim_get_ts_info(struct net_device *dev, · line 208
Quoted 9 lines
drivers/net/netdevsim/ethtool.c +6
-0@@ -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 ; + } +[Severity: High]
[Finding: main-1-1]
[Sources: opus-5]
This isn't a bug introduced by this patch, but the new !ns->phc guard looks like it silently fixes a NULL pointer dereference that exists today, without a mention in the changelog or a Fixes: tag.Before this patch
nsim_get_ts_info()did:info->phc_index = mock_phc_index(ns->phc);and
nsim_ethtool_init()installs the sharednsim_ethtool_ops(including .get_ts_info =nsim_get_ts_info) on every netdevsim netdev, while onlynsim_init_netdevsim()callsmock_phc_create().nsim_init_netdevsim_vf()just assignsnsim_vf_netdev_opsand registers the netdev, sons->phcstays NULL for VF netdevs.With
CONFIG_PTP_1588_CLOCK_MOCK=y,mock_phc_index()is the real function:drivers/ptp/ptp_mock.c:mock_phc_index() { return ptp_clock_index(phc->clock); }
Quoted 8 lines
so ethtool -T on a netdevsim VF netdev dereferences NULL. Would it make sense to split this guard into its own patch with a Fixes: tag so that stable tooling can pick it up?
Also, adding
ethtool_op_get_ts_info()here changes ethtool -T output for all netdevsim devices by advertisingSOF_TIMESTAMPING_TX_SOFTWARE,SOF_TIMESTAMPING_RX_SOFTWAREandSOF_TIMESTAMPING_SOFTWARE. Could the changelog mention that behaviour change?
This patchset introduces this connection - not sure what would it fix?
Quoted 68 lines
Quoted 18 lines
drivers/net/netdevsim/ethtool.c +9
-0info->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 ; }drivers/net/netdevsim/netdev.c +0
-0[ ... ]
Quoted 15 lines
drivers/net/netdevsim/netdev.c +11
-0@@ -164,6 +171,44 @@ 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 */ drivers/net/netdevsim/netdev.c static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev) · line 194
Quoted 10 lines
drivers/net/netdevsim/netdev.c +10
-0+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_orig, &shhwtstamps); + }[Severity: Medium]
[Finding: gpt-5-6-sol-3-10]
[Sources: gpt-5-6-sol, opus-5]
Can a socket receive twoSCM_TSTAMP_SNDreports for the same packet here?The hardware report is emitted after
skb_tx_timestamp(), which already delivered the software one:include/linux/skbuff.h:skb_tx_timestamp() { skb_clone_tx_timestamp(skb); if (skb_shinfo(skb)->tx_flags & (SKBTX_SW_TSTAMP | SKBTX_BPF)) skb_tstamp_tx(skb, NULL); }The only suppression of the software report lives in
__skb_tstamp_tx():net/core/skbuff.c:__skb_tstamp_tx() { if (!hwtstamps && !(tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) return;
Quoted 9 lines
netdevsim never sets
SKBTX_IN_PROGRESSanywhere - the new code only testsSKBTX_HW_TSTAMP- so for a socket that enabled bothSOF_TIMESTAMPING_TX_SOFTWAREandSOF_TIMESTAMPING_TX_HARDWAREwithoutSOF_TIMESTAMPING_OPT_TX_SWHWboth reports are delivered.Would following the usual driver sequence work here, i.e. test
SKBTX_HW_TSTAMPand setSKBTX_IN_PROGRESSbefore callingskb_tx_timestamp()?
That protection is needed for the code that delays tstamp completion into some "worker" that handles the timestamp returned by HW once the packet was transmitted out. For a netdevsim we return the tx tstamp immediately.
Quoted 23 lines
drivers/net/netdevsim/netdev.c static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev) · line 204
Quoted 10 lines
drivers/net/netdevsim/netdev.c +10
-0+ +/* Unshare the skb, stamping it must not corrupt cloned copies +* and prevents returning single tstamp for Tx and Rx +*/ + skb =skb_unshare (skb_orig,GFP_ATOMIC ); +if (unlikely (!skb)) { +if (psp_ext) +__skb_ext_put (psp_ext); + goto out_drop_cnt; + }[Severity: Low]
[Finding: main-3-8]
[Sources: opus-5]
Should thisskb_unshare()be gated onptp_info_rx?The enclosing condition is
ptp_info_tx||ptp_info_rx, but the only write into the skb below is guarded by if (ptp_info_rx). On the TX-only path (peerrx_filter==HWTSTAMP_FILTER_NONE) every timestamped frame gets copied byskb_copy()for nothing, which also linearizes the buffer that thehds_configlogic just above deliberately left non-linear, and does not carryqueue_mapping.
Quoted 4 lines
If the
GFP_ATOMICcopy then fails, the frame is dropped viaout_drop_cnteven though nothing needed to be written into it, and after the TX timestamp was already handed to the socket byskb_tstamp_tx(skb_orig, ...).
No - both paths requires skb_unshare. TX one needs it to unshare from the stack for example - for TCP retransmittion and PSP
Quoted 11 lines
[Severity: Low]
[Finding: main-5-12]
[Sources: opus-5]
Doesskb_unshare()cover the case this comment describes?include/linux/skbuff.h:skb_unshare() { might_sleep_if(gfpflags_allow_blocking(pri)); if (skb_cloned(skb)) { ... }
Quoted 9 lines
The copy is made only for clones, so an skb with
skb->users> 1 and cloned == 0 is returned unchanged. pktgen doesrefcount_add(burst, &pkt_dev->skb->users)and resubmits the same skb, so theskb_hwtstamps(skb)write below touchesskb_shared_infothat the other reference holder still owns, and that the peer's NAPI path may read concurrently afternsim_forward_skb()->nsim_napi_rx()queues it onrq->skb_queue. Should the driver checkskb_shared()as well, or stamp a private copy?
??
Quoted 51 lines
drivers/net/netdevsim/netdev.c static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev) · line 211
Quoted 7 lines
drivers/net/netdevsim/netdev.c +7
-0+ +/* If RX hardware timestamping is enabled report RX timestamp */ +if (ptp_info_rx) +skb_hwtstamps (skb)->hwtstamp = +timespec64_to_ktime (rx_ts); + } +[Severity: Critical]
[Finding: main-6-13]
[Sources: opus-5]
Can this write corrupt kernel memory forAF_XDPTX skbs?
skb_hwtstamps()returns a pointer into a union inskb_shared_info:include/linux/skbuff.h:struct skb_shared_info { union { struct skb_shared_hwtstamps hwtstamps; struct xsk_tx_metadata_compl xsk_meta; };struct
xsk_tx_metadata_complhas a single member, u64*tx_timestamp, and theAF_XDPdestructor dereferences it with only a NULL check:net/xdp/xsk.c:xsk_destruct_skb() { struct xsk_tx_metadata_compl *compl = &skb_shinfo(skb)->xsk_meta; if (compl->tx_timestamp) { /* sw completion timestamp, not a real one */ *compl->tx_timestamp = ktime_get_tai_fast_ns(); }Every generic-xmit
AF_XDPskb carries that destructor (xsk_skb_init_misc()setsskb->destructor=xsk_destruct_skb), and it is freshly allocated and not cloned, soskb_unshare()above returns the same skb with the destructor and the overwritten shinfo intact.__dev_forward_skb()->skb_scrub_packet()does not orphan the skb or resetskb_shinfo(), so the value survives to the destructor.The RX branch is gated only on the peer's setting:
if (unlikely(READ_ONCE(peer_ns->tstamp_config.rx_filter) != HWTSTAMP_FILTER_NONE)) ptp_info_rx = mock_phc_get_ptp_info(peer_ns->phc);so any
AF_XDPframe forwarded while the peer has RX timestamping enabled gets a non-NULL, PHC-derived value in xsk_meta.tx_timestamp, and the destructor performs an 8-byte write to that address. The mock PHC value is settable from userspace throughclock_settime()on/dev/ptpNviamock_phc_settime64().
Quoted 5 lines
Real NICs avoid this because they only stamp skbs they allocated for RX. Since netdevsim reuses the TX skb as the RX skb, should it orphan the skb (clearing
skb->destructor) or stamp a private copy before touching skb_shinfo()->hwtstamps?
Sounds like this should be addressed in the generic skb_scrub_packet in the __dev_forward_skb path?
Quoted 55 lines
Quoted 17 lines
drivers/net/netdevsim/netdev.c +10
-0if (unlikely (nsim_forward_skb (dev, peer_dev, skb, rq, psp_ext) ==NET_RX_DROP )) goto out_drop_cnt;@@ -185,6 +230,63 @@ static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)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 ; +[ ... ]
drivers/net/netdevsim/netdev.c static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev) · line 288
Quoted 8 lines
drivers/net/netdevsim/netdev.c +8
-0+static int nsim_get_ts_config (struct net_device *netdev, +struct kernel_hwtstamp_config *config) +{ +struct netdevsim *ns =netdev_priv (netdev); + + *config = ns->tstamp_config; +return 0 ; +}[Severity: Low]
[Finding: gpt-5-6-sol-2-6]
[Sources: gpt-5-6-sol, opus-5]
Shouldnsim_get_ts_config()carry the same !ns->phc check asnsim_set_ts_config()?
drivers/net/Kconfigallows netdevsim without the mock clock:depends on PTP_1588_CLOCK_MOCK || PTP_1588_CLOCK_MOCK=nand with
CONFIG_PTP_1588_CLOCK_MOCK=nthe stub is used:
include/linux/ptp_mock.h{
static inline structmock_phc*mock_phc_create(struct device *dev){ return NULL; }
nsim_init_netdevsim()only rejectsIS_ERR(), sons->phcstays NULL. In that configuration SIOCGHWTSTAMP succeeds and reportstx_type=HWTSTAMP_TX_OFF/rx_filter=HWTSTAMP_FILTER_NONE, i.e. "hardware timestamping present but disabled", whilensim_get_ts_info()reportsphc_index= -1 with no hardwareso_timestampingand every set attempt is rejected with-EOPNOTSUPP. Other converted drivers keep the get/set pair symmetric here.
Quoted 1 line
[ ... ]
We don't support setting, getting is always available - not sure what's the hypothetical bug there
Simon Horman
reviewerRe: [PATCH v10 net-next 2/3] netdevsim: Implement basic ptp support
Quoted 63 lines · Maciek Machnikowski
Quoted 60 lines · Simon Horman
This is an AI-generated review of your patch. The human sending this
email has considered the AI review valid, or at least plausible.
Full review at: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/[email protected]?part=2
---
--- Patch [2]: [PATCH v10 net-next 2/3] netdevsim: Implement basic ptp support ---
commitef2b7171af287d8c3a8f5064008830a7dd8f4db3
Author: Maciek Machnikowski <[email protected]>netdevsim: Implement basic ptp support
Adds virtual hardware timestamping to netdevsim by reading the mock PHC attached to the transmitting netdevsim and to its peer, reporting a TX timestamp to the socket and stamping the forwarded skb for the RX side. Also wires up
ndo_hwtstamp_get/ndo_hwtstamp_setand extendsnsim_get_ts_info().A few questions on the timestamping path below.
Quoted 5 lines
drivers/net/netdevsim/ethtool.c +0
-0drivers/net/netdevsim/ethtool.c static int nsim_get_ts_info(struct net_device *dev, · line 208
Quoted 9 lines
drivers/net/netdevsim/ethtool.c +6
-0@@ -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 ; + } +[Severity: High]
[Finding: main-1-1]
[Sources: opus-5]
This isn't a bug introduced by this patch, but the new !ns->phc guard looks like it silently fixes a NULL pointer dereference that exists today, without a mention in the changelog or a Fixes: tag.Before this patch
nsim_get_ts_info()did:info->phc_index = mock_phc_index(ns->phc);and
nsim_ethtool_init()installs the sharednsim_ethtool_ops(including .get_ts_info =nsim_get_ts_info) on every netdevsim netdev, while onlynsim_init_netdevsim()callsmock_phc_create().nsim_init_netdevsim_vf()just assignsnsim_vf_netdev_opsand registers the netdev, sons->phcstays NULL for VF netdevs.With
CONFIG_PTP_1588_CLOCK_MOCK=y,mock_phc_index()is the real function:drivers/ptp/ptp_mock.c:mock_phc_index() { return ptp_clock_index(phc->clock); }so ethtool -T on a netdevsim VF netdev dereferences NULL. Would it make sense to split this guard into its own patch with a Fixes: tag so that stable tooling can pick it up?
Quoted 5 lines
Quoted 4 lines
Also, adding
ethtool_op_get_ts_info()here changes ethtool -T output for all netdevsim devices by advertisingSOF_TIMESTAMPING_TX_SOFTWARE,SOF_TIMESTAMPING_RX_SOFTWAREandSOF_TIMESTAMPING_SOFTWARE. Could the changelog mention that behaviour change?This patchset introduces this connection - not sure what would it fix?
Thanks, I agree that seems to be a false positive. Sorry for not noticing that earlier.
Quoted 72 lines
Quoted 72 lines
Quoted 17 lines
drivers/net/netdevsim/ethtool.c +9
-0info->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 ; }drivers/net/netdevsim/netdev.c +0
-0[ ... ]
Quoted 15 lines
drivers/net/netdevsim/netdev.c +11
-0@@ -164,6 +171,44 @@ 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 */ drivers/net/netdevsim/netdev.c static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev) · line 194
Quoted 10 lines
drivers/net/netdevsim/netdev.c +10
-0+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_orig, &shhwtstamps); + }[Severity: Medium]
[Finding: gpt-5-6-sol-3-10]
[Sources: gpt-5-6-sol, opus-5]
Can a socket receive twoSCM_TSTAMP_SNDreports for the same packet here?The hardware report is emitted after
skb_tx_timestamp(), which already delivered the software one:include/linux/skbuff.h:skb_tx_timestamp() { skb_clone_tx_timestamp(skb); if (skb_shinfo(skb)->tx_flags & (SKBTX_SW_TSTAMP | SKBTX_BPF)) skb_tstamp_tx(skb, NULL); }The only suppression of the software report lives in
__skb_tstamp_tx():net/core/skbuff.c:__skb_tstamp_tx() { if (!hwtstamps && !(tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) return;netdevsim never sets
SKBTX_IN_PROGRESSanywhere - the new code only testsSKBTX_HW_TSTAMP- so for a socket that enabled bothSOF_TIMESTAMPING_TX_SOFTWAREandSOF_TIMESTAMPING_TX_HARDWAREwithoutSOF_TIMESTAMPING_OPT_TX_SWHWboth reports are delivered.
Quoted 8 lines
Quoted 4 lines
Would following the usual driver sequence work here, i.e. test
SKBTX_HW_TSTAMPand setSKBTX_IN_PROGRESSbefore callingskb_tx_timestamp()?That protection is needed for the code that delays tstamp completion into some "worker" that handles the timestamp returned by HW once the packet was transmitted out. For a netdevsim we return the tx tstamp immediately.
Thanks, I think we cal let this one rest.
Quoted 23 lines
Quoted 23 lines
drivers/net/netdevsim/netdev.c static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev) · line 204
Quoted 10 lines
drivers/net/netdevsim/netdev.c +10
-0+ +/* Unshare the skb, stamping it must not corrupt cloned copies +* and prevents returning single tstamp for Tx and Rx +*/ + skb =skb_unshare (skb_orig,GFP_ATOMIC ); +if (unlikely (!skb)) { +if (psp_ext) +__skb_ext_put (psp_ext); + goto out_drop_cnt; + }[Severity: Low]
[Finding: main-3-8]
[Sources: opus-5]
Should thisskb_unshare()be gated onptp_info_rx?The enclosing condition is
ptp_info_tx||ptp_info_rx, but the only write into the skb below is guarded by if (ptp_info_rx). On the TX-only path (peerrx_filter==HWTSTAMP_FILTER_NONE) every timestamped frame gets copied byskb_copy()for nothing, which also linearizes the buffer that thehds_configlogic just above deliberately left non-linear, and does not carryqueue_mapping.
Quoted 6 lines
Quoted 4 lines
If the
GFP_ATOMICcopy then fails, the frame is dropped viaout_drop_cnteven though nothing needed to be written into it, and after the TX timestamp was already handed to the socket byskb_tstamp_tx(skb_orig, ...).No - both paths requires
skb_unshare. TX one needs it to unshare from the stack for example - for TCP retransmittion and PSP
Likewise, thanks. I see your point.
Quoted 5 lines
Quoted 5 lines
[Severity: Low]
[Finding: main-5-12]
[Sources: opus-5]
Doesskb_unshare()cover the case this comment describes?
d
Quoted 14 lines
Quoted 5 lines
might_sleep_if(gfpflags_allow_blocking(pri)); if (skb_cloned(skb)) { ... }Quoted 9 lines
The copy is made only for clones, so an skb with
skb->users> 1 and cloned == 0 is returned unchanged. pktgen doesrefcount_add(burst, &pkt_dev->skb->users)and resubmits the same skb, so theskb_hwtstamps(skb)write below touchesskb_shared_infothat the other reference holder still owns, and that the peer's NAPI path may read concurrently afternsim_forward_skb()->nsim_napi_rx()queues it onrq->skb_queue. Should the driver checkskb_shared()as well, or stamp a private copy?
Quoted 1 line
??
My understanding here is that it is possible for skbs to be shared without being clones. E.g. pktgen.
And that as a software driver that simulates hardware timestamps (unique in the Kernel?) this needs to be taken into account by copying skbs that are either shared or cloned.
Quoted 52 lines
Quoted 52 lines
drivers/net/netdevsim/netdev.c static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev) · line 211
Quoted 8 lines
drivers/net/netdevsim/netdev.c +7
-0+ +/* If RX hardware timestamping is enabled report RX timestamp */ +if (ptp_info_rx) +skb_hwtstamps (skb)->hwtstamp = +timespec64_to_ktime (rx_ts); + } +[Severity: Critical]
[Finding: main-6-13]
[Sources: opus-5]
Can this write corrupt kernel memory forAF_XDPTX skbs?
skb_hwtstamps()returns a pointer into a union inskb_shared_info:include/linux/skbuff.h:struct skb_shared_info { union { struct skb_shared_hwtstamps hwtstamps; struct xsk_tx_metadata_compl xsk_meta; };struct
xsk_tx_metadata_complhas a single member, u64*tx_timestamp, and theAF_XDPdestructor dereferences it with only a NULL check:net/xdp/xsk.c:xsk_destruct_skb() { struct xsk_tx_metadata_compl *compl = &skb_shinfo(skb)->xsk_meta; if (compl->tx_timestamp) { /* sw completion timestamp, not a real one */ *compl->tx_timestamp = ktime_get_tai_fast_ns(); }Every generic-xmit
AF_XDPskb carries that destructor (xsk_skb_init_misc()setsskb->destructor=xsk_destruct_skb), and it is freshly allocated and not cloned, soskb_unshare()above returns the same skb with the destructor and the overwritten shinfo intact.__dev_forward_skb()->skb_scrub_packet()does not orphan the skb or resetskb_shinfo(), so the value survives to the destructor.The RX branch is gated only on the peer's setting:
if (unlikely(READ_ONCE(peer_ns->tstamp_config.rx_filter) != HWTSTAMP_FILTER_NONE)) ptp_info_rx = mock_phc_get_ptp_info(peer_ns->phc);so any
AF_XDPframe forwarded while the peer has RX timestamping enabled gets a non-NULL, PHC-derived value in xsk_meta.tx_timestamp, and the destructor performs an 8-byte write to that address. The mock PHC value is settable from userspace throughclock_settime()on/dev/ptpNviamock_phc_settime64().
Quoted 7 lines
Quoted 5 lines
Real NICs avoid this because they only stamp skbs they allocated for RX. Since netdevsim reuses the TX skb as the RX skb, should it orphan the skb (clearing
skb->destructor) or stamp a private copy before touching skb_shinfo()->hwtstamps?Sounds like this should be addressed in the generic
skb_scrub_packetin the__dev_forward_skbpath?
My understanding is that would be outside the scope of skb_scrub_packet. And doing so would would break socket memory accounting and the AF_XDP completion signal.
I think that this problem occurs because netdev sim both uses the same skbs for both RX and TX, and now modifies shared metadata. As this is an unusual (unique?) scenario, it seems reasonable to me for the driver to address the issue.
Quoted 56 lines
Quoted 56 lines
Quoted 18 lines
drivers/net/netdevsim/netdev.c +10
-0if (unlikely (nsim_forward_skb (dev, peer_dev, skb, rq, psp_ext) ==NET_RX_DROP )) goto out_drop_cnt;@@ -185,6 +230,63 @@ static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)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 ; +[ ... ]
drivers/net/netdevsim/netdev.c static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev) · line 288
Quoted 8 lines
drivers/net/netdevsim/netdev.c +8
-0+static int nsim_get_ts_config (struct net_device *netdev, +struct kernel_hwtstamp_config *config) +{ +struct netdevsim *ns =netdev_priv (netdev); + + *config = ns->tstamp_config; +return 0 ; +}[Severity: Low]
[Finding: gpt-5-6-sol-2-6]
[Sources: gpt-5-6-sol, opus-5]
Shouldnsim_get_ts_config()carry the same !ns->phc check asnsim_set_ts_config()?
drivers/net/Kconfigallows netdevsim without the mock clock:depends on PTP_1588_CLOCK_MOCK || PTP_1588_CLOCK_MOCK=nand with
CONFIG_PTP_1588_CLOCK_MOCK=nthe stub is used:
include/linux/ptp_mock.h{
static inline structmock_phc*mock_phc_create(struct device *dev){ return NULL; }
nsim_init_netdevsim()only rejectsIS_ERR(), sons->phcstays NULL. In that configuration SIOCGHWTSTAMP succeeds and reportstx_type=HWTSTAMP_TX_OFF/rx_filter=HWTSTAMP_FILTER_NONE, i.e. "hardware timestamping present but disabled", whilensim_get_ts_info()reportsphc_index= -1 with no hardwareso_timestampingand every set attempt is rejected with-EOPNOTSUPP. Other converted drivers keep the get/set pair symmetric here.
Quoted 3 lines
Quoted 1 line
[ ... ]
We don't support setting, getting is always available - not sure what's the hypothetical bug there
I don't think there is a bug here.
Rather, I think it's a question about API symmetry.
It seems that drivers have some leeway on this one, so I think you can use your own best judgement.
Re: [PATCH v10 net-next 2/3] netdevsim: Implement basic ptp support
Quoted 22 lines · Simon Horman
Quoted 5 lines · Maciek Machnikowski
Quoted 5 lines
[Severity: Low]
[Finding: main-5-12]
[Sources: opus-5]
Doesskb_unshare()cover the case this comment describes?d
Quoted 14 lines
Quoted 14 lines
might_sleep_if(gfpflags_allow_blocking(pri)); if (skb_cloned(skb)) { ... }The copy is made only for clones, so an skb with
skb->users> 1 and cloned == 0 is returned unchanged. pktgen doesrefcount_add(burst, &pkt_dev->skb->users)and resubmits the same skb, so theskb_hwtstamps(skb)write below touchesskb_shared_infothat the other reference holder still owns, and that the peer's NAPI path may read concurrently afternsim_forward_skb()->nsim_napi_rx()queues it onrq->skb_queue. Should the driver checkskb_shared()as well, or stamp a private copy?
Quoted 9 lines
Quoted 1 line
??
My understanding here is that it is possible for skbs to be shared without being clones. E.g. pktgen.
And that as a software driver that simulates hardware timestamps (unique in the Kernel?) this needs to be taken into account by copying skbs that are either shared or cloned.
I believe it should not be an issue - if the SKB gets recycled through the stack it'll just get a new RX tstamp when it re-enters this part of the code. If someone disables Rx tstamps - the packet with a tstamp without a consumer would be recycled.
Quoted 60 lines
Quoted 52 lines
Quoted 52 lines
drivers/net/netdevsim/netdev.c static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev) · line 211
Quoted 8 lines
drivers/net/netdevsim/netdev.c +7
-0+ +/* If RX hardware timestamping is enabled report RX timestamp */ +if (ptp_info_rx) +skb_hwtstamps (skb)->hwtstamp = +timespec64_to_ktime (rx_ts); + } +[Severity: Critical]
[Finding: main-6-13]
[Sources: opus-5]
Can this write corrupt kernel memory forAF_XDPTX skbs?
skb_hwtstamps()returns a pointer into a union inskb_shared_info:include/linux/skbuff.h:struct skb_shared_info { union { struct skb_shared_hwtstamps hwtstamps; struct xsk_tx_metadata_compl xsk_meta; };struct
xsk_tx_metadata_complhas a single member, u64*tx_timestamp, and theAF_XDPdestructor dereferences it with only a NULL check:net/xdp/xsk.c:xsk_destruct_skb() { struct xsk_tx_metadata_compl *compl = &skb_shinfo(skb)->xsk_meta; if (compl->tx_timestamp) { /* sw completion timestamp, not a real one */ *compl->tx_timestamp = ktime_get_tai_fast_ns(); }Every generic-xmit
AF_XDPskb carries that destructor (xsk_skb_init_misc()setsskb->destructor=xsk_destruct_skb), and it is freshly allocated and not cloned, soskb_unshare()above returns the same skb with the destructor and the overwritten shinfo intact.__dev_forward_skb()->skb_scrub_packet()does not orphan the skb or resetskb_shinfo(), so the value survives to the destructor.The RX branch is gated only on the peer's setting:
if (unlikely(READ_ONCE(peer_ns->tstamp_config.rx_filter) != HWTSTAMP_FILTER_NONE)) ptp_info_rx = mock_phc_get_ptp_info(peer_ns->phc);so any
AF_XDPframe forwarded while the peer has RX timestamping enabled gets a non-NULL, PHC-derived value in xsk_meta.tx_timestamp, and the destructor performs an 8-byte write to that address. The mock PHC value is settable from userspace throughclock_settime()on/dev/ptpNviamock_phc_settime64().Quoted 7 lines
Quoted 5 lines
Real NICs avoid this because they only stamp skbs they allocated for RX. Since netdevsim reuses the TX skb as the RX skb, should it orphan the skb (clearing
skb->destructor) or stamp a private copy before touching skb_shinfo()->hwtstamps?Sounds like this should be addressed in the generic
skb_scrub_packetin the__dev_forward_skbpath?
Quoted 9 lines
My understanding is that would be outside the scope of
skb_scrub_packet. And doing so would would break socket memory accounting and theAF_XDPcompletion signal.I think that this problem occurs because netdev sim both uses the same skbs for both RX and TX, and now modifies shared metadata. As this is an unusual (unique?) scenario, it seems reasonable to me for the driver to address the issue.
I do understand your point of view, but I think it's generally something we should scrub when forwarding the SKB. It doesn't seem right to deliver a AF_XDP packet to a destination RX queue with some "random" destructor.
But I'm not AF_XDP expert, so would like to get an opinion from someone more familiar with it - Kuba - can you help with this one?
PATCH v10 3/3 selftests: drivers/net: Implement ptp4l sync test using netdevsim
In reply to Maciek MachnikowskiAdd 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).
--- tools/testing/selftests/drivers/net/Makefile | 1 + tools/testing/selftests/drivers/net/config | 1 + tools/testing/selftests/drivers/net/ptp.py | 84 ++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100755 tools/testing/selftests/drivers/net/ptp.py
tools/testing/selftests/drivers/net/Makefile +1 -0
@@ -19,6 +19,7 @@ TEST_PROGS := \ netpoll_basic.py \ ping.py \ psp.py \ + ptp.py \ queues.py \ ring_reconfig.py \ shaper.py \
tools/testing/selftests/drivers/net/config +1 -0
@@ -21,5 +21,6 @@ CONFIG_NET_SCH_INGRESS=y CONFIG_NET_SCH_PRIO=m CONFIG_PPP=y CONFIG_PPPOE=y +CONFIG_PTP_1588_CLOCK_MOCK=y CONFIG_VLAN_8021Q=m CONFIG_XDP_SOCKETS=y
tools/testing/selftests/drivers/net/ptp.py new file +84 -0
84 lines not on this page · raw message
Signature · 1 line
2.55.0
Jakub Kicinski
maintainerQuoted 6 lines · On Sat, 15 Aug 2026 08:31:45 +0200 Maciek Machnikowski wrote:
This patchset adds support to the PTP HW timestamping emulation in the netdevsim. It uses existing binding between netdevsim and
ptp_mockdriver to generate transmit and receive timestamps.It also adds the selftest to verify the hw timestamping functionality running over netdevsim.
FTR I held this off because I wanted to check if it passes on any HW we have in NIPA. First there was a bug in NIPA itself:
https://github.com/linux-netdev/nipa/commit/459e2575ec36bbc47659db8a455a1b2eebde59b3
then we were missing linuxptp on the runners.
Next run will hopefully tell.
Paolo Abeni
maintainerQuoted 6 lines · Maciek Machnikowski
This patchset adds support to the PTP HW timestamping emulation in the netdevsim. It uses existing binding between netdevsim and
ptp_mockdriver to generate transmit and receive timestamps.It also adds the selftest to verify the hw timestamping functionality running over netdevsim.
## Form letter - net-next-closed
We have already submitted our pull request with net-next material for v7.3, and therefore net-next is closed for new drivers, features, code refactoring and optimizations. We are currently accepting bug fixes only.
Please repost when net-next reopens after Aug 31st.
RFC patches sent for review only are obviously welcome at any time.
See: https://www.kernel.org/doc/html/next/process/maintainer-netdev.html#development-cycle