Re: [PATCH v3 net-next 06/10] net: ptp: create a mock-up PTP Hardware Clock driver

From: Kurt Kanzenbach
Date: Wed Aug 02 2023 - 08:18:22 EST


Hi Vladimir,

this looks good. One question below.

On Tue Aug 01 2023, Vladimir Oltean wrote:
> There are several cases where virtual net devices may benefit from
> having a PTP clock, and these have to do with testing. I can see at
> least netdevsim and veth as potential users of a common mock-up PTP
> hardware clock driver.
>
> The proposed idea is to create an object which emulates PTP clock
> operations on top of the unadjustable CLOCK_MONOTONIC_RAW plus a
> software-controlled time domain via a timecounter/cyclecounter and then
> link that PHC to the netdevsim device.
>
> The driver is fully functional for its intended purpose, and it
> successfully passes the PTP selftests.
>
> $ cd tools/testing/selftests/ptp/
> $ ./phc.sh /dev/ptp2
> TEST: settime [ OK ]
> TEST: adjtime [ OK ]
> TEST: adjfreq [ OK ]
>
> Signed-off-by: Vladimir Oltean <vladimir.oltean@xxxxxxx>
> ---
> v2->v3:
> - split off ptp_mock into separate patch
> - fix ptp_mock compilation as a module
> - turn phc->lock into a global spinlock
> - drop bogus support for ptp_clock_register() ever returning NULL
> - add MAINTAINERS entry
> v1->v2: patch is new
>
> MAINTAINERS | 7 ++
> drivers/ptp/Kconfig | 11 +++
> drivers/ptp/Makefile | 1 +
> drivers/ptp/ptp_mock.c | 175 +++++++++++++++++++++++++++++++++++++++
> include/linux/ptp_mock.h | 38 +++++++++
> 5 files changed, 232 insertions(+)
> create mode 100644 drivers/ptp/ptp_mock.c
> create mode 100644 include/linux/ptp_mock.h
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index c4f95a9d03b9..164b7930f5d0 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -17175,6 +17175,13 @@ F: drivers/ptp/*
> F: include/linux/ptp_cl*
> K: (?:\b|_)ptp(?:\b|_)
>
> +PTP MOCKUP CLOCK SUPPORT
> +M: Vladimir Oltean <vladimir.oltean@xxxxxxx>
> +L: netdev@xxxxxxxxxxxxxxx
> +S: Maintained
> +F: drivers/ptp/ptp_mock.c
> +F: include/linux/ptp_mock.h
> +
> PTP VIRTUAL CLOCK SUPPORT
> M: Yangbo Lu <yangbo.lu@xxxxxxx>
> L: netdev@xxxxxxxxxxxxxxx
> diff --git a/drivers/ptp/Kconfig b/drivers/ptp/Kconfig
> index 32dff1b4f891..ed9d97a032f1 100644
> --- a/drivers/ptp/Kconfig
> +++ b/drivers/ptp/Kconfig
> @@ -155,6 +155,17 @@ config PTP_1588_CLOCK_IDTCM
> To compile this driver as a module, choose M here: the module
> will be called ptp_clockmatrix.
>
> +config PTP_1588_CLOCK_MOCK
> + tristate "Mock-up PTP clock"
> + depends on PTP_1588_CLOCK
> + help
> + This driver offers a set of PTP clock manipulation operations over
> + the system monotonic time. It can be used by virtual network device
> + drivers to emulate PTP capabilities.
> +
> + To compile this driver as a module, choose M here: the module
> + will be called ptp_mock.
> +
> config PTP_1588_CLOCK_VMW
> tristate "VMware virtual PTP clock"
> depends on ACPI && HYPERVISOR_GUEST && X86
> diff --git a/drivers/ptp/Makefile b/drivers/ptp/Makefile
> index 553f18bf3c83..dea0cebd2303 100644
> --- a/drivers/ptp/Makefile
> +++ b/drivers/ptp/Makefile
> @@ -16,6 +16,7 @@ ptp-qoriq-y += ptp_qoriq.o
> ptp-qoriq-$(CONFIG_DEBUG_FS) += ptp_qoriq_debugfs.o
> obj-$(CONFIG_PTP_1588_CLOCK_IDTCM) += ptp_clockmatrix.o
> obj-$(CONFIG_PTP_1588_CLOCK_IDT82P33) += ptp_idt82p33.o
> +obj-$(CONFIG_PTP_1588_CLOCK_MOCK) += ptp_mock.o
> obj-$(CONFIG_PTP_1588_CLOCK_VMW) += ptp_vmw.o
> obj-$(CONFIG_PTP_1588_CLOCK_OCP) += ptp_ocp.o
> obj-$(CONFIG_PTP_DFL_TOD) += ptp_dfl_tod.o
> diff --git a/drivers/ptp/ptp_mock.c b/drivers/ptp/ptp_mock.c
> new file mode 100644
> index 000000000000..1525aafca752
> --- /dev/null
> +++ b/drivers/ptp/ptp_mock.c
> @@ -0,0 +1,175 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright 2023 NXP
> + *
> + * Mock-up PTP Hardware Clock driver for virtual network devices
> + *
> + * Create a PTP clock which offers PTP time manipulation operations
> + * using a timecounter/cyclecounter on top of CLOCK_MONOTONIC_RAW.
> + */
> +
> +#include <linux/ptp_clock_kernel.h>
> +#include <linux/ptp_mock.h>
> +#include <linux/timecounter.h>
> +
> +/* Clamp scaled_ppm between -2,097,152,000 and 2,097,152,000,
> + * and thus "adj" between -68,719,476 and 68,719,476
> + */
> +#define MOCK_PHC_MAX_ADJ_PPB 32000000
> +/* Timestamps from ktime_get_raw() have 1 ns resolution, so the scale factor
> + * (MULT >> SHIFT) needs to be 1. Pick SHIFT as 31 bits, which translates
> + * MULT(freq 0) into 0x80000000.
> + */
> +#define MOCK_PHC_CC_SHIFT 31
> +#define MOCK_PHC_CC_MULT (1 << MOCK_PHC_CC_SHIFT)
> +#define MOCK_PHC_FADJ_SHIFT 9
> +#define MOCK_PHC_FADJ_DENOMINATOR 15625ULL
> +
> +/* The largest cycle_delta that timecounter_read_delta() can handle without a
> + * 64-bit overflow during the multiplication with cc->mult, given the max "adj"
> + * we permit, is ~8.3 seconds. Make sure readouts are more frequent than that.
> + */
> +#define MOCK_PHC_REFRESH_INTERVAL (HZ * 5)
> +
> +#define info_to_phc(d) container_of((d), struct mock_phc, info)
> +
> +static DEFINE_SPINLOCK(mock_phc_lock);
> +
> +struct mock_phc {
> + struct ptp_clock_info info;
> + struct ptp_clock *clock;
> + struct timecounter tc;
> + struct cyclecounter cc;
> +};
> +
> +static u64 mock_phc_cc_read(const struct cyclecounter *cc)
> +{
> + return ktime_to_ns(ktime_get_raw());

Maybe return ktime_get_raw_ns()?

> +}
> +
> +static int mock_phc_adjfine(struct ptp_clock_info *info, long scaled_ppm)
> +{
> + struct mock_phc *phc = info_to_phc(info);
> + s64 adj;
> +
> + adj = (s64)scaled_ppm << MOCK_PHC_FADJ_SHIFT;
> + adj = div_s64(adj, MOCK_PHC_FADJ_DENOMINATOR);
> +
> + spin_lock_bh(&mock_phc_lock);

Why spin_lock_bh()? What's executed in bh context here? The PTP aux
worker runs in thread context.

Thanks,
Kurt

Attachment: signature.asc
Description: PGP signature