diff --git a/platform/mellanox/docker-syncd-mlnx/Dockerfile.j2 b/platform/mellanox/docker-syncd-mlnx/Dockerfile.j2 index 7b418ab858..54c0028903 100755 --- a/platform/mellanox/docker-syncd-mlnx/Dockerfile.j2 +++ b/platform/mellanox/docker-syncd-mlnx/Dockerfile.j2 @@ -1,7 +1,7 @@ ## ## SPDX-FileCopyrightText: NVIDIA CORPORATION & AFFILIATES -## Copyright (c) 2016-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -## Apache-2.0 +## Copyright (c) 2016-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +## SPDX-License-Identifier: Apache-2.0 ## ## Licensed under the Apache License, Version 2.0 (the "License"); ## you may not use this file except in compliance with the License. @@ -35,7 +35,8 @@ RUN apt-get update && \ python3-pip \ python3-dev \ python3-jsonschema \ - python-is-python3 + python-is-python3 \ + linuxptp RUN pip3 install --upgrade pip RUN apt-get purge -y python-pip @@ -69,6 +70,7 @@ COPY ["supervisord.conf.j2", "/usr/share/sonic/templates/"] COPY ["files/supervisor-proc-exit-listener", "/usr/bin"] COPY ["critical_processes", "/etc/supervisor/"] COPY ["platform_syncd_dump.sh", "/usr/bin/"] +COPY ["phcsync.sh", "/usr/bin/"] RUN mkdir -p /etc/mlnx/ COPY ["sai-common.profile", "/etc/mlnx/"] diff --git a/platform/mellanox/docker-syncd-mlnx/phcsync.sh b/platform/mellanox/docker-syncd-mlnx/phcsync.sh new file mode 100755 index 0000000000..4e561ca14c --- /dev/null +++ b/platform/mellanox/docker-syncd-mlnx/phcsync.sh @@ -0,0 +1,71 @@ +#!/bin/bash +# +# SPDX-FileCopyrightText: NVIDIA CORPORATION & AFFILIATES +# Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# +# Script to synchronize ASIC PTP clocks with system realtime clock. +# This script runs continuously, syncing clocks every 60 seconds. +# This script shall not be running if PTP is enabled in the system. +# + +PHC_CTL="/usr/sbin/phc_ctl" +SLEEP_INTERVAL=60 + +# Check if phc_ctl is available +if [ ! -x "$PHC_CTL" ]; then + echo "Error: phc_ctl not found. Exiting." + exit 1 +fi + +while :; do + # Refresh the list of PTP devices on each iteration + PTP_DEVICES=$(ls /dev/ptp[0-9]* 2>/dev/null) + + if [ -z "$PTP_DEVICES" ]; then + sleep $SLEEP_INTERVAL + continue + fi + + for dev in $PTP_DEVICES; do + # Extract device number from /dev/ptpXX + dev_num=${dev##*/ptp} + + clock_name_file="/sys/class/ptp/ptp${dev_num}/clock_name" + if [[ ! -f "$clock_name_file" ]]; then + echo "Error: clock_name file not found for $dev ($clock_name_file), skipping" >&2 + continue + fi + + clock_name=$(cat "$clock_name_file" 2>/dev/null) + CLOCK_NAME_EXIT_CODE=$? + if [[ $CLOCK_NAME_EXIT_CODE -ne 0 ]] || [[ -z "$clock_name" ]]; then + echo "Error: Failed to read clock_name from $clock_name_file for $dev, skipping" >&2 + continue + fi + + if [[ "$clock_name" != "mlx5_ptp" ]]; then + # set CLOCK_REALTIME + "$PHC_CTL" "$dev" set 2>/dev/null + PHC_CTL_EXIT_CODE=$? + if [[ $PHC_CTL_EXIT_CODE -ne 0 ]]; then + echo "Error: Failed to sync clock for $dev (phc_ctl exit code: $PHC_CTL_EXIT_CODE)" >&2 + fi + fi + done + sleep $SLEEP_INTERVAL +done diff --git a/platform/mellanox/docker-syncd-mlnx/supervisord.conf.j2 b/platform/mellanox/docker-syncd-mlnx/supervisord.conf.j2 index c4290d920f..6e266d9c28 100644 --- a/platform/mellanox/docker-syncd-mlnx/supervisord.conf.j2 +++ b/platform/mellanox/docker-syncd-mlnx/supervisord.conf.j2 @@ -41,3 +41,13 @@ dependent_startup_wait_for=rsyslogd:running {% if ENABLE_ASAN == "y" %} environment=ASAN_OPTIONS="log_path=/var/log/asan/syncd-asan.log{{ asan_extra_options }}" {% endif %} + +[program:phcsync] +command=/usr/bin/phcsync.sh +priority=3 +autostart=false +autorestart=false +stdout_logfile=syslog +stderr_logfile=syslog +dependent_startup=true +dependent_startup_wait_for=rsyslogd:running