From 4bccda1473f18b544f61cf9145f39e639c04d07d Mon Sep 17 00:00:00 2001 From: Lotus Fenn Date: Wed, 15 Apr 2026 05:25:25 +0000 Subject: [PATCH 1/2] Add eth0 override to ztp Signed-off-by: Lotus Fenn --- src/usr/lib/ztp/ztp-engine.py | 53 +++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/src/usr/lib/ztp/ztp-engine.py b/src/usr/lib/ztp/ztp-engine.py index f1e7bad..95a28ae 100755 --- a/src/usr/lib/ztp/ztp-engine.py +++ b/src/usr/lib/ztp/ztp-engine.py @@ -35,6 +35,53 @@ from ztp.ZTPLib import getTimestamp, runCommand, runcmd_pids from ztp.ZTPLib import getField, getCfg, validateZtpCfg, updateActivity, systemReboot from swsscommon.swsscommon import ConfigDBConnector, SonicV2Connector +import sonic_platform.platform + + +def _get_kernel_operstate(intf): + """! + Read the kernel-reported operstate for a network interface. + + @return 'up', 'down', or other kernel operstate string. + """ + with open("/sys/class/net/{}/operstate".format(intf), "r") as fh: + return fh.readline().strip().lower() + + +def _has_platform_mgmt_link_check(): + """! + Check whether this platform overrides the kernel-reported management port + link status via the platform API (get_management_port_link_status_override). + """ + try: + chassis = sonic_platform.platform.Platform().get_chassis() + except Exception: + return False + return hasattr(chassis, "get_management_port_link_status_override") + + +def _platform_mgmt_link_check(intf): + """! + Query the platform for the real management port link status via the + platform API. If the platform provides an override (returns True/False), + use it instead of the kernel operstate. If not implemented (returns None) + or on error, return None to fall back to the kernel operstate. + + @return 'up' if the platform reports link up; + 'down' if the platform reports link down; + None if the platform does not override or on error. + """ + try: + chassis = sonic_platform.platform.Platform().get_chassis() + is_up = chassis.get_management_port_link_status_override(intf) + except Exception as e: + logger.warning("get_management_port_link_status_override failed: %s" % str(e)) + return None + if is_up is None: + return None + logger.debug("Platform mgmt link check: %s" % ("up" if is_up else "down")) + return "up" if is_up else "down" + def check_pid(pid): ## Check For the existence of a unix pid @@ -157,9 +204,9 @@ def __detect_intf_state(self): for intf in natsorted(intf_list): try: if intf[0:3] == 'eth': - fh = open('/sys/class/net/{}/operstate'.format(intf), 'r') - operstate = fh.readline().strip().lower() - fh.close() + override = _platform_mgmt_link_check(intf) if _has_platform_mgmt_link_check() else None + operstate = override if override is not None else _get_kernel_operstate(intf) + else: if self.applDB.exists(self.applDB.APPL_DB, 'PORT_TABLE:'+intf): port_entry = self.applDB.get_all(self.applDB.APPL_DB, 'PORT_TABLE:'+intf) From 64904cdb9a6b7cc43d751ef2b9d6ba3427afcdd9 Mon Sep 17 00:00:00 2001 From: Lotus Fenn Date: Wed, 22 Jul 2026 20:49:44 +0000 Subject: [PATCH 2/2] Clarify link-status override contract in ZTP mgmt-port docstring --- src/usr/lib/ztp/ztp-engine.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/usr/lib/ztp/ztp-engine.py b/src/usr/lib/ztp/ztp-engine.py index 95a28ae..3a930b6 100755 --- a/src/usr/lib/ztp/ztp-engine.py +++ b/src/usr/lib/ztp/ztp-engine.py @@ -67,9 +67,11 @@ def _platform_mgmt_link_check(intf): use it instead of the kernel operstate. If not implemented (returns None) or on error, return None to fall back to the kernel operstate. - @return 'up' if the platform reports link up; - 'down' if the platform reports link down; - None if the platform does not override or on error. + @return 'up' if the platform override reports link up; + 'down' if the platform override reports link down; + None if the platform does not override (not implemented or + error), in which case ZTP falls back to the kernel link + state (/sys/class/net//operstate). """ try: chassis = sonic_platform.platform.Platform().get_chassis()