From ca77971b1c15b9f82634b72453b0221156537571 Mon Sep 17 00:00:00 2001 From: Stephen Sun <5379172+stephenxs@users.noreply.github.com> Date: Tue, 15 Jun 2021 22:57:48 +0800 Subject: [PATCH] [Mellanox] Enhance Python3 support for platform API (#7410) - Why I did it Enhance the Python3 support for platform API. Originally, some platform APIs call SDK API which didn't support Python 3. Now the Python 3 APIs have been supported in SDK 4.4.3XXX, Python3 is completely supported by platform API - How I did it Start all platform daemons from python3 1. Remove #/usr/bin/env python at the beginning of each platform API file as the platform API won't be started as daemons but be imported from other daemons. 2. Adjust SDK API calls accordingly - How to verify it Manually test and run regression platform test Signed-off-by: Stephen Sun --- .../pmon_daemon_control.json | 3 +- .../sonic_platform/chassis.py | 2 -- .../sonic_platform/component.py | 2 -- .../mlnx-platform-api/sonic_platform/fan.py | 2 -- .../sonic_platform/fan_drawer.py | 2 -- .../sonic_platform/platform.py | 2 -- .../mlnx-platform-api/sonic_platform/psu.py | 2 -- .../mlnx-platform-api/sonic_platform/sfp.py | 29 ++++++++++++------- .../sonic_platform/sfp_event.py | 1 - .../sonic_platform/thermal.py | 2 -- .../sonic_platform/watchdog.py | 2 -- 11 files changed, 19 insertions(+), 30 deletions(-) diff --git a/device/mellanox/x86_64-mlnx_msn2700-r0/pmon_daemon_control.json b/device/mellanox/x86_64-mlnx_msn2700-r0/pmon_daemon_control.json index dec2ba6d7826..86910af29e89 100644 --- a/device/mellanox/x86_64-mlnx_msn2700-r0/pmon_daemon_control.json +++ b/device/mellanox/x86_64-mlnx_msn2700-r0/pmon_daemon_control.json @@ -1,7 +1,6 @@ { "skip_ledd": true, "skip_fancontrol": true, - "delay_xcvrd": true, - "python2_daemons": ["xcvrd"] + "delay_xcvrd": true } diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py b/platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py index 09e4e1a625a3..b621ed9ffc28 100644 --- a/platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py +++ b/platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - ############################################################################# # Mellanox # diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform/component.py b/platform/mellanox/mlnx-platform-api/sonic_platform/component.py index f2a66c6d42ee..8845b3faedba 100644 --- a/platform/mellanox/mlnx-platform-api/sonic_platform/component.py +++ b/platform/mellanox/mlnx-platform-api/sonic_platform/component.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - ############################################################################# # Mellanox # diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform/fan.py b/platform/mellanox/mlnx-platform-api/sonic_platform/fan.py index d501bff7431f..b3b94c3d727a 100644 --- a/platform/mellanox/mlnx-platform-api/sonic_platform/fan.py +++ b/platform/mellanox/mlnx-platform-api/sonic_platform/fan.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - ############################################################################# # Mellanox # diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform/fan_drawer.py b/platform/mellanox/mlnx-platform-api/sonic_platform/fan_drawer.py index 0ffe69c1f321..40e7c89eeecf 100644 --- a/platform/mellanox/mlnx-platform-api/sonic_platform/fan_drawer.py +++ b/platform/mellanox/mlnx-platform-api/sonic_platform/fan_drawer.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - ############################################################################# # Mellanox # diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform/platform.py b/platform/mellanox/mlnx-platform-api/sonic_platform/platform.py index f0ea0cb5de30..4b8d1a72062d 100644 --- a/platform/mellanox/mlnx-platform-api/sonic_platform/platform.py +++ b/platform/mellanox/mlnx-platform-api/sonic_platform/platform.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - ############################################################################# # Mellanox # diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform/psu.py b/platform/mellanox/mlnx-platform-api/sonic_platform/psu.py index cb6244db4acf..b8f2a347ff48 100644 --- a/platform/mellanox/mlnx-platform-api/sonic_platform/psu.py +++ b/platform/mellanox/mlnx-platform-api/sonic_platform/psu.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - ############################################################################# # Mellanox # diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform/sfp.py b/platform/mellanox/mlnx-platform-api/sonic_platform/sfp.py index 2d366875c50f..da90f2954fca 100644 --- a/platform/mellanox/mlnx-platform-api/sonic_platform/sfp.py +++ b/platform/mellanox/mlnx-platform-api/sonic_platform/sfp.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - ############################################################################# # Mellanox # @@ -1557,9 +1555,12 @@ def mgmt_phy_mod_pwr_attr_get(cls, power_attr_type, sdk_handle, sdk_index): sx_mgmt_phy_mod_pwr_attr = sx_mgmt_phy_mod_pwr_attr_t() sx_mgmt_phy_mod_pwr_attr.power_attr_type = power_attr_type sx_mgmt_phy_mod_pwr_attr_t_p_assign(sx_mgmt_phy_mod_pwr_attr_p, sx_mgmt_phy_mod_pwr_attr) + module_id_info = sx_mgmt_module_id_info_t() + module_id_info.slot_id = 0 + module_id_info.module_id = sdk_index try: - rc = sx_mgmt_phy_mod_pwr_attr_get(sdk_handle, sdk_index, sx_mgmt_phy_mod_pwr_attr_p) - assert SX_STATUS_SUCCESS == rc, "sx_mgmt_phy_mod_pwr_attr_get failed" + rc = sx_mgmt_phy_module_pwr_attr_get(sdk_handle, module_id_info, sx_mgmt_phy_mod_pwr_attr_p) + assert SX_STATUS_SUCCESS == rc, "sx_mgmt_phy_module_pwr_attr_get failed {}".format(rc) sx_mgmt_phy_mod_pwr_attr = sx_mgmt_phy_mod_pwr_attr_t_p_value(sx_mgmt_phy_mod_pwr_attr_p) pwr_mode_attr = sx_mgmt_phy_mod_pwr_attr.pwr_mode_attr return pwr_mode_attr.admin_pwr_mode_e, pwr_mode_attr.oper_pwr_mode_e @@ -1581,7 +1582,7 @@ def get_lpmode(self): get_lpmode_code = 'from sonic_platform import sfp;\n' \ 'with sfp.SdkHandleContext() as sdk_handle:' \ 'print(sfp.SFP._get_lpmode(sdk_handle, {}))'.format(self.sdk_index) - lpm_cmd = "docker exec pmon python -c \"{}\"".format(get_lpmode_code) + lpm_cmd = "docker exec pmon python3 -c \"{}\"".format(get_lpmode_code) try: output = subprocess.check_output(lpm_cmd, shell=True, universal_newlines=True) return 'True' in output @@ -1985,7 +1986,7 @@ def reset(self): 'with sfp.SdkHandleContext() as sdk_handle:' \ 'print(sfp.SFP._reset(sdk_handle, {}))' \ .format(self.sdk_index) - reset_cmd = "docker exec pmon python -c \"{}\"".format(reset_code) + reset_cmd = "docker exec pmon python3 -c \"{}\"".format(reset_code) try: output = subprocess.check_output(reset_cmd, shell=True, universal_newlines=True) @@ -1999,9 +2000,12 @@ def reset(self): @classmethod def _reset(cls, sdk_handle, sdk_index): - rc = sx_mgmt_phy_mod_reset(sdk_handle, sdk_index) + module_id_info = sx_mgmt_module_id_info_t() + module_id_info.slot_id = 0 + module_id_info.module_id = sdk_index + rc = sx_mgmt_phy_module_reset(sdk_handle, module_id_info) if rc != SX_STATUS_SUCCESS: - logger.log_warning("sx_mgmt_phy_mod_reset failed, rc = %d" % rc) + logger.log_error("Error occurred when resetting SFP module {}, error code {}".format(sdk_index, rc)) return rc == SX_STATUS_SUCCESS @@ -2115,10 +2119,13 @@ def mgmt_phy_mod_pwr_attr_set(cls, sdk_handle, sdk_index, power_attr_type, admin sx_mgmt_phy_mod_pwr_attr.pwr_mode_attr = sx_mgmt_phy_mod_pwr_mode_attr sx_mgmt_phy_mod_pwr_attr_p = new_sx_mgmt_phy_mod_pwr_attr_t_p() sx_mgmt_phy_mod_pwr_attr_t_p_assign(sx_mgmt_phy_mod_pwr_attr_p, sx_mgmt_phy_mod_pwr_attr) + module_id_info = sx_mgmt_module_id_info_t() + module_id_info.slot_id = 0 + module_id_info.module_id = sdk_index try: - rc = sx_mgmt_phy_mod_pwr_attr_set(sdk_handle, SX_ACCESS_CMD_SET, sdk_index, sx_mgmt_phy_mod_pwr_attr_p) + rc = sx_mgmt_phy_module_pwr_attr_set(sdk_handle, SX_ACCESS_CMD_SET, module_id_info, sx_mgmt_phy_mod_pwr_attr_p) if SX_STATUS_SUCCESS != rc: - logger.log_error("sx_mgmt_phy_mod_pwr_attr_set failed, rc = %d" % rc) + logger.log_error("Error occurred when setting power mode for SFP module {}, error code {}".format(sdk_index, rc)) result = False else: result = True @@ -2168,7 +2175,7 @@ def set_lpmode(self, lpmode): 'with sfp.SdkHandleContext() as sdk_handle:' \ 'print(sfp.SFP._set_lpmode({}, sdk_handle, {}))' \ .format('True' if lpmode else 'False', self.sdk_index) - lpm_cmd = "docker exec pmon python -c \"{}\"".format(set_lpmode_code) + lpm_cmd = "docker exec pmon python3 -c \"{}\"".format(set_lpmode_code) # Set LPM try: diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform/sfp_event.py b/platform/mellanox/mlnx-platform-api/sonic_platform/sfp_event.py index b8a4ca0984c6..ce90a00a8fca 100644 --- a/platform/mellanox/mlnx-platform-api/sonic_platform/sfp_event.py +++ b/platform/mellanox/mlnx-platform-api/sonic_platform/sfp_event.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python ''' listen to the SDK for the SFP change event and return to chassis. ''' diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform/thermal.py b/platform/mellanox/mlnx-platform-api/sonic_platform/thermal.py index 7b1f523d5d50..0aa620d652e8 100644 --- a/platform/mellanox/mlnx-platform-api/sonic_platform/thermal.py +++ b/platform/mellanox/mlnx-platform-api/sonic_platform/thermal.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - ############################################################################# # Mellanox # diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform/watchdog.py b/platform/mellanox/mlnx-platform-api/sonic_platform/watchdog.py index 909ec6a8884a..06780627d52e 100644 --- a/platform/mellanox/mlnx-platform-api/sonic_platform/watchdog.py +++ b/platform/mellanox/mlnx-platform-api/sonic_platform/watchdog.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - """ Mellanox