From: Maciek Machnikowski <maciek@machnikowski.net>
To: netdev@vger.kernel.org
Cc: kuba@kernel.org, maciek@machnikowski.net,
richardcochran@gmail.com, milena.olech@intel.com,
willemdebruijn.kernel@gmail.com, andrew@lunn.ch,
vadim.fedorenko@linux.dev
Subject: [PATCH v2 net-next 3/3] selftests:net: Implement ptp4l sync test using netdevsim
Date: Sun, 22 Feb 2026 10:41:10 +0100 [thread overview]
Message-ID: <20260222094110.127927-4-maciek@machnikowski.net> (raw)
In-Reply-To: <20260222094110.127927-1-maciek@machnikowski.net>
Add PTP synchronization test using ptp4l and netdevsim.
The test creates two netdevsim adapters, links them together
and runs the ptp4l leader and ptp4l follower on two ends
of the netdevsim link and waits for the follower to report the
synchronized state (s2) in its output log.
This implementation runs the test runs over IPv4 link.
Signed-off-by: Maciek Machnikowski <maciek@machnikowski.net>
---
tools/testing/selftests/net/ptp.sh | 93 ++++++++++++++++++++++++++++++
1 file changed, 93 insertions(+)
create mode 100755 tools/testing/selftests/net/ptp.sh
diff --git a/tools/testing/selftests/net/ptp.sh b/tools/testing/selftests/net/ptp.sh
new file mode 100755
index 000000000..8b1604b96
--- /dev/null
+++ b/tools/testing/selftests/net/ptp.sh
@@ -0,0 +1,93 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0-only
+source lib.sh
+
+NSIM_DEV_1_ID=$((256 + RANDOM % 256))
+NSIM_DEV_2_ID=$((512 + RANDOM % 256))
+
+NSIM_DEV_SYS_LINK=/sys/bus/netdevsim/link_device
+NSIM_DEV_SYS_UNLINK=/sys/bus/netdevsim/unlink_device
+PTP4L_SYNC_TIMEOUT=20
+
+setup_netdevsim()
+{
+ set -e
+ ip netns add nssv
+ ip netns add nscl
+
+ NSIM_DEV_1_NAME=$(create_netdevsim $NSIM_DEV_1_ID nssv)
+ NSIM_DEV_2_NAME=$(create_netdevsim $NSIM_DEV_2_ID nscl)
+
+ ip netns exec nssv ip addr add '192.168.1.1/24' dev $NSIM_DEV_1_NAME
+ ip netns exec nscl ip addr add '192.168.1.2/24' dev $NSIM_DEV_2_NAME
+
+ set +e
+}
+
+cleanup()
+{
+ [ -n "${PTP4L_LEADER_PID-}" ] && {
+ kill $PTP4L_LEADER_PID $PTP4L_FOLLOWER_PID 2>/dev/null
+ wait $PTP4L_LEADER_PID $PTP4L_FOLLOWER_PID 2>/dev/null
+ rm -f "$PTP4L_LEADER_LOG" "$PTP4L_FOLLOWER_LOG"
+ }
+
+ [ -n "${NSIM_DEV_1_FD-}" ] && [ -n "${NSIM_DEV_1_IFIDX-}" ] && \
+ echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX" > "$NSIM_DEV_SYS_UNLINK" 2>/dev/null
+ cleanup_netdevsim "${NSIM_DEV_2_ID-}" 2>/dev/null
+ cleanup_netdevsim "${NSIM_DEV_1_ID-}" 2>/dev/null
+
+ ip netns del nscl 2>/dev/null
+ ip netns del nssv 2>/dev/null
+
+ modprobe -r netdevsim 2>/dev/null
+}
+
+###
+### Code start
+###
+if [ ! -x "$(command -v ptp4l)" ]; then
+ echo "ptp4l command not found. Skipping PTP sync test"
+ exit 4
+fi
+
+trap cleanup EXIT
+
+setup_netdevsim
+
+# Link netdevsim1 with netdevsim2
+NSIM_DEV_1_FD=$((256 + RANDOM % 256))
+exec {NSIM_DEV_1_FD}</var/run/netns/nssv
+NSIM_DEV_1_IFIDX=$(ip netns exec nssv cat /sys/class/net/$NSIM_DEV_1_NAME/ifindex)
+
+NSIM_DEV_2_FD=$((512 + RANDOM % 256))
+exec {NSIM_DEV_2_FD}</var/run/netns/nscl
+NSIM_DEV_2_IFIDX=$(ip netns exec nscl cat /sys/class/net/$NSIM_DEV_2_NAME/ifindex)
+
+if ! echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD:$NSIM_DEV_2_IFIDX" > "$NSIM_DEV_SYS_LINK"; then
+ echo "linking netdevsim1 with netdevsim2 failed"
+ exit 1
+fi
+
+# PTP synchronization test: run ptp4l leader in nssv and follower in nscl,
+# then parse follower output to verify they synchronized (servo state s2 = locked).
+PTP4L_LEADER_LOG=$(mktemp)
+PTP4L_FOLLOWER_LOG=$(mktemp)
+ip netns exec nssv ptp4l -i "$NSIM_DEV_1_NAME" -m -4 -P \
+ > "$PTP4L_LEADER_LOG" 2>&1 &
+PTP4L_LEADER_PID=$!
+
+ip netns exec nscl ptp4l -i "$NSIM_DEV_2_NAME" -s -m -4 -P \
+ > "$PTP4L_FOLLOWER_LOG" 2>&1 &
+PTP4L_FOLLOWER_PID=$!
+
+
+for _ in $(seq 1 $PTP4L_SYNC_TIMEOUT); do
+ if grep -q ' s2 ' "$PTP4L_FOLLOWER_LOG" 2>/dev/null; then
+ exit 0
+ fi
+ sleep 1
+done
+
+echo "ptp4l follower did not reach locked state (s2) within ${PTP4L_SYNC_TIMEOUT}s"
+exit 1
\ No newline at end of file
--
2.53.0
next prev parent reply other threads:[~2026-02-23 12:35 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-22 9:41 [PATCH v2 net-next 0/3] Implement PTP support in netdevsim Maciek Machnikowski
2026-02-22 9:41 ` [PATCH v2 net-next 1/3] ptp_mock: Expose ptp_clock_info to external drivers Maciek Machnikowski
2026-02-23 16:36 ` Jakub Kicinski
2026-02-22 9:41 ` [PATCH v2 net-next 2/3] netdevsim: Implement basic ptp support Maciek Machnikowski
2026-02-22 9:41 ` Maciek Machnikowski [this message]
2026-02-24 10:54 ` [PATCH v2 net-next 3/3] selftests:net: Implement ptp4l sync test using netdevsim Simon Horman
2026-02-25 10:08 ` Maciek Machnikowski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260222094110.127927-4-maciek@machnikowski.net \
--to=maciek@machnikowski.net \
--cc=andrew@lunn.ch \
--cc=kuba@kernel.org \
--cc=milena.olech@intel.com \
--cc=netdev@vger.kernel.org \
--cc=richardcochran@gmail.com \
--cc=vadim.fedorenko@linux.dev \
--cc=willemdebruijn.kernel@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox