Skip to content

Commit 4f67b12

Browse files
[Mellanox] Add phcsync script for ASIC clock synchronization (#24972)
- Why I did it Fix a bug in HFT where the asic clock was not synchronized with the system clock, causing it to report a time around 1970. There is a dependency between this PR and: sonic-net/sonic-sairedis#1734 The PR to sonic-buildimage should be merged first, followed by the PR to sonic-sairedis. - How I did it Added phcsync script for ASIC clock synchronization on Mellanox platforms: created phcsync.sh that syncs PTP clocks with system clock every 60 seconds. - How to verify it Which release branch to backport (provide reason below if selected) Signed-off-by: Zili Bombach <[email protected]> * Added error logs to phcsync.sh and made phcsync.sh executable Signed-off-by: Zili Bombach <[email protected]> --------- Signed-off-by: Zili Bombach <[email protected]> Co-authored-by: Liat Grozovik <[email protected]>
1 parent 5ed1176 commit 4f67b12

3 files changed

Lines changed: 85 additions & 2 deletions

File tree

platform/mellanox/docker-syncd-mlnx/Dockerfile.j2

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
##
22
## SPDX-FileCopyrightText: NVIDIA CORPORATION & AFFILIATES
3-
## Copyright (c) 2016-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3+
## Copyright (c) 2016-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
44
## SPDX-License-Identifier: Apache-2.0
55
##
66
## Licensed under the Apache License, Version 2.0 (the "License");
@@ -38,7 +38,8 @@ RUN apt-get update && \
3838
python3-dev \
3939
python3-jsonschema \
4040
python-is-python3 \
41-
pciutils
41+
pciutils \
42+
linuxptp
4243

4344
RUN pip3 install --upgrade pip
4445
RUN apt-get purge -y python-pip
@@ -67,6 +68,7 @@ RUN apt-get purge -y \
6768
COPY ["supervisord.conf.j2", "/usr/share/sonic/templates/"]
6869
COPY ["critical_processes", "/etc/supervisor/"]
6970
COPY ["platform_syncd_dump.sh", "/usr/bin/"]
71+
COPY ["phcsync.sh", "/usr/bin/"]
7072

7173
COPY ["files/rdb-cli", "/usr/bin/"]
7274
RUN chmod +x /usr/bin/rdb-cli
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/bin/bash
2+
#
3+
# SPDX-FileCopyrightText: NVIDIA CORPORATION & AFFILIATES
4+
# Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5+
# SPDX-License-Identifier: Apache-2.0
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
20+
#
21+
# Script to synchronize ASIC PTP clocks with system realtime clock.
22+
# This script runs continuously, syncing clocks every 60 seconds.
23+
# This script shall not be running if PTP is enabled in the system.
24+
#
25+
26+
PHC_CTL="/usr/sbin/phc_ctl"
27+
SLEEP_INTERVAL=60
28+
29+
# Check if phc_ctl is available
30+
if [ ! -x "$PHC_CTL" ]; then
31+
echo "Error: phc_ctl not found. Exiting."
32+
exit 1
33+
fi
34+
35+
while :; do
36+
# Refresh the list of PTP devices on each iteration
37+
PTP_DEVICES=$(ls /dev/ptp[0-9]* 2>/dev/null)
38+
39+
if [ -z "$PTP_DEVICES" ]; then
40+
sleep $SLEEP_INTERVAL
41+
continue
42+
fi
43+
44+
for dev in $PTP_DEVICES; do
45+
# Extract device number from /dev/ptpXX
46+
dev_num=${dev##*/ptp}
47+
48+
clock_name_file="/sys/class/ptp/ptp${dev_num}/clock_name"
49+
if [[ ! -f "$clock_name_file" ]]; then
50+
echo "Error: clock_name file not found for $dev ($clock_name_file), skipping" >&2
51+
continue
52+
fi
53+
54+
clock_name=$(cat "$clock_name_file" 2>/dev/null)
55+
CLOCK_NAME_EXIT_CODE=$?
56+
if [[ $CLOCK_NAME_EXIT_CODE -ne 0 ]] || [[ -z "$clock_name" ]]; then
57+
echo "Error: Failed to read clock_name from $clock_name_file for $dev, skipping" >&2
58+
continue
59+
fi
60+
61+
if [[ "$clock_name" != "mlx5_ptp" ]]; then
62+
# set CLOCK_REALTIME
63+
"$PHC_CTL" "$dev" set 2>/dev/null
64+
PHC_CTL_EXIT_CODE=$?
65+
if [[ $PHC_CTL_EXIT_CODE -ne 0 ]]; then
66+
echo "Error: Failed to sync clock for $dev (phc_ctl exit code: $PHC_CTL_EXIT_CODE)" >&2
67+
fi
68+
fi
69+
done
70+
sleep $SLEEP_INTERVAL
71+
done

platform/mellanox/docker-syncd-mlnx/supervisord.conf.j2

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,13 @@ dependent_startup_wait_for=rsyslogd:running
4545
{% if ENABLE_ASAN == "y" %}
4646
environment=ASAN_OPTIONS="log_path=/var/log/asan/syncd-asan.log{{ asan_extra_options }}"
4747
{% endif %}
48+
49+
[program:phcsync]
50+
command=/usr/bin/phcsync.sh
51+
priority=3
52+
autostart=false
53+
autorestart=false
54+
stdout_logfile=syslog
55+
stderr_logfile=syslog
56+
dependent_startup=true
57+
dependent_startup_wait_for=rsyslogd:running

0 commit comments

Comments
 (0)