From eb5f12f8f37400835b58070e25b12b8b5da71dcc Mon Sep 17 00:00:00 2001 From: tshalvi Date: Sun, 2 Jun 2024 17:42:58 +0300 Subject: [PATCH 1/4] Updated get_presence() logic to rely on hw_present or present sysfs values --- .../mellanox/mlnx-platform-api/sonic_platform/sfp.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform/sfp.py b/platform/mellanox/mlnx-platform-api/sonic_platform/sfp.py index 7b21f22ca11..4884a56d6fc 100644 --- a/platform/mellanox/mlnx-platform-api/sonic_platform/sfp.py +++ b/platform/mellanox/mlnx-platform-api/sonic_platform/sfp.py @@ -483,6 +483,9 @@ def get_presence(self): Returns: bool: True if device is present, False if not """ + presence_sysfs = f'/sys/module/sx_core/asic0/module{self.sdk_index}/hw_present' if self.is_sw_control() else f'/sys/module/sx_core/asic0/module{self.sdk_index}/present' + if utils.read_int_from_file(presence_sysfs) != 1: + return False eeprom_raw = self._read_eeprom(0, 1, log_on_error=False) return eeprom_raw is not None @@ -1061,6 +1064,8 @@ def get_temperature(self): 0.0 if module temperature is not supported or module is under initialization other float value if module temperature is available """ + if not self.get_presence(): + return None try: if not self.is_sw_control(): temp_file = f'/sys/module/sx_core/asic0/module{self.sdk_index}/temperature/input' @@ -1085,6 +1090,8 @@ def get_temperature_warning_threshold(self): 0.0 if warning threshold is not supported or module is under initialization other float value if warning threshold is available """ + if not self.get_presence(): + return None try: self.is_sw_control() except: @@ -1107,6 +1114,8 @@ def get_temperature_critical_threshold(self): 0.0 if critical threshold is not supported or module is under initialization other float value if critical threshold is available """ + if not self.get_presence(): + return None try: self.is_sw_control() except: @@ -1938,3 +1947,4 @@ def get_module_status(self): """ status = super().get_module_status() return SFP_STATUS_REMOVED if status == SFP_STATUS_UNKNOWN else status + From d85ea2da37c722cc2ecea00e7bcdde9f43854699 Mon Sep 17 00:00:00 2001 From: tshalvi Date: Sun, 2 Jun 2024 18:01:27 +0300 Subject: [PATCH 2/4] Removed redundant new-line --- platform/mellanox/mlnx-platform-api/sonic_platform/sfp.py | 1 - 1 file changed, 1 deletion(-) diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform/sfp.py b/platform/mellanox/mlnx-platform-api/sonic_platform/sfp.py index 4884a56d6fc..9e41728c2f9 100644 --- a/platform/mellanox/mlnx-platform-api/sonic_platform/sfp.py +++ b/platform/mellanox/mlnx-platform-api/sonic_platform/sfp.py @@ -1947,4 +1947,3 @@ def get_module_status(self): """ status = super().get_module_status() return SFP_STATUS_REMOVED if status == SFP_STATUS_UNKNOWN else status - From a1df5e4b15ddcc6f4cc21b66f194cfa0a52a0d9d Mon Sep 17 00:00:00 2001 From: tshalvi Date: Mon, 3 Jun 2024 11:40:43 +0300 Subject: [PATCH 3/4] Moved presence validations to thermal.py --- platform/mellanox/mlnx-platform-api/sonic_platform/sfp.py | 6 ------ .../mellanox/mlnx-platform-api/sonic_platform/thermal.py | 6 ++++++ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform/sfp.py b/platform/mellanox/mlnx-platform-api/sonic_platform/sfp.py index 9e41728c2f9..58fe71b84c8 100644 --- a/platform/mellanox/mlnx-platform-api/sonic_platform/sfp.py +++ b/platform/mellanox/mlnx-platform-api/sonic_platform/sfp.py @@ -1064,8 +1064,6 @@ def get_temperature(self): 0.0 if module temperature is not supported or module is under initialization other float value if module temperature is available """ - if not self.get_presence(): - return None try: if not self.is_sw_control(): temp_file = f'/sys/module/sx_core/asic0/module{self.sdk_index}/temperature/input' @@ -1090,8 +1088,6 @@ def get_temperature_warning_threshold(self): 0.0 if warning threshold is not supported or module is under initialization other float value if warning threshold is available """ - if not self.get_presence(): - return None try: self.is_sw_control() except: @@ -1114,8 +1110,6 @@ def get_temperature_critical_threshold(self): 0.0 if critical threshold is not supported or module is under initialization other float value if critical threshold is available """ - if not self.get_presence(): - return None try: self.is_sw_control() except: diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform/thermal.py b/platform/mellanox/mlnx-platform-api/sonic_platform/thermal.py index 7ac703b7841..219fe241808 100644 --- a/platform/mellanox/mlnx-platform-api/sonic_platform/thermal.py +++ b/platform/mellanox/mlnx-platform-api/sonic_platform/thermal.py @@ -435,6 +435,8 @@ def get_temperature(self): A float number of current temperature in Celsius up to nearest thousandth of one degree Celsius, e.g. 30.125 """ + if not self.sfp.get_presence(): + return None value = self.sfp.get_temperature() return value if (value != 0.0 and value is not None) else None @@ -446,6 +448,8 @@ def get_high_threshold(self): A float number, the high threshold temperature of thermal in Celsius up to nearest thousandth of one degree Celsius, e.g. 30.125 """ + if not self.sfp.get_presence(): + return None value = self.sfp.get_temperature_warning_threshold() return value if (value != 0.0 and value is not None) else None @@ -457,6 +461,8 @@ def get_high_critical_threshold(self): A float number, the high critical threshold temperature of thermal in Celsius up to nearest thousandth of one degree Celsius, e.g. 30.125 """ + if not self.sfp.get_presence(): + return None value = self.sfp.get_temperature_critical_threshold() return value if (value != 0.0 and value is not None) else None From fcb92bb1e2e2c588ef667f783bd52857e502d2e3 Mon Sep 17 00:00:00 2001 From: tshalvi Date: Mon, 3 Jun 2024 18:17:15 +0300 Subject: [PATCH 4/4] Updated relevant unit tests --- .../mellanox/mlnx-platform-api/tests/test_sfp.py | 12 ++++++++++-- .../mellanox/mlnx-platform-api/tests/test_thermal.py | 9 +++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/platform/mellanox/mlnx-platform-api/tests/test_sfp.py b/platform/mellanox/mlnx-platform-api/tests/test_sfp.py index 92f5d0d5161..947736f3660 100644 --- a/platform/mellanox/mlnx-platform-api/tests/test_sfp.py +++ b/platform/mellanox/mlnx-platform-api/tests/test_sfp.py @@ -247,15 +247,23 @@ def test_get_page_and_page_offset(self, mock_get_type_str, mock_eeprom_path, moc assert page == '/tmp/1/data' assert page_offset is 0 + @mock.patch('sonic_platform.utils.read_int_from_file') @mock.patch('sonic_platform.sfp.SFP._read_eeprom') - def test_sfp_get_presence(self, mock_read): + def test_sfp_get_presence(self, mock_read, mock_read_int): sfp = SFP(0) + + mock_read_int.return_value = 1 mock_read.return_value = None assert not sfp.get_presence() - mock_read.return_value = 0 assert sfp.get_presence() + mock_read_int.return_value = 0 + mock_read.return_value = None + assert not sfp.get_presence() + mock_read.return_value = 0 + assert not sfp.get_presence() + @mock.patch('sonic_platform.utils.read_int_from_file') def test_rj45_get_presence(self, mock_read_int): sfp = RJ45Port(0) diff --git a/platform/mellanox/mlnx-platform-api/tests/test_thermal.py b/platform/mellanox/mlnx-platform-api/tests/test_thermal.py index e17d91cb081..a92322264ee 100644 --- a/platform/mellanox/mlnx-platform-api/tests/test_thermal.py +++ b/platform/mellanox/mlnx-platform-api/tests/test_thermal.py @@ -159,12 +159,21 @@ def test_sfp_thermal(self): assert thermal.get_name() == rule['name'].format(start_index) assert thermal.get_position_in_parent() == 1 assert thermal.is_replaceable() == False + sfp.get_presence = mock.MagicMock(return_value=True) sfp.get_temperature = mock.MagicMock(return_value=35.4) sfp.get_temperature_warning_threshold = mock.MagicMock(return_value=70) sfp.get_temperature_critical_threshold = mock.MagicMock(return_value=80) assert thermal.get_temperature() == 35.4 assert thermal.get_high_threshold() == 70 assert thermal.get_high_critical_threshold() == 80 + sfp.get_presence = mock.MagicMock(return_value=False) + sfp.get_temperature = mock.MagicMock(return_value=35.4) + sfp.get_temperature_warning_threshold = mock.MagicMock(return_value=70) + sfp.get_temperature_critical_threshold = mock.MagicMock(return_value=80) + assert thermal.get_temperature() is None + assert thermal.get_high_threshold() is None + assert thermal.get_high_critical_threshold() is None + sfp.get_presence = mock.MagicMock(return_value=True) sfp.get_temperature = mock.MagicMock(return_value=0) sfp.get_temperature_warning_threshold = mock.MagicMock(return_value=0) sfp.get_temperature_critical_threshold = mock.MagicMock(return_value=None)