Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mellanox] Fixes issue: CLI sfputil does not work based on sonic platform API #61

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 2 additions & 24 deletions platform/mellanox/mlnx-platform-api/sonic_platform/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
import subprocess
from sonic_platform_base.platform_base import PlatformBase
from sonic_platform.chassis import Chassis
from . import utils
except ImportError as e:
raise ImportError(str(e) + "- required module not found")

class Platform(PlatformBase):
def __init__(self):
PlatformBase.__init__(self)
if self._is_host():
if utils.is_host():
self._chassis = Chassis()
self._chassis.initialize_components()
self._chassis.initizalize_system_led()
Expand All @@ -27,26 +28,3 @@ def __init__(self):
self._chassis.initialize_fan()
self._chassis.initialize_eeprom()
self._chassis.initialize_thermals()

def _is_host(self):
"""
Test whether current process is running on the host or an docker
return True for host and False for docker
"""
is_host = False
try:
proc = subprocess.Popen("docker --version 2>/dev/null",
stdout=subprocess.PIPE,
shell=True,
stderr=subprocess.STDOUT,
universal_newlines=True)
stdout = proc.communicate()[0]
proc.wait()
result = stdout.rstrip('\n')
if result != '':
is_host = True

except OSError as e:
pass

return is_host
67 changes: 51 additions & 16 deletions platform/mellanox/mlnx-platform-api/sonic_platform/sfp.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

try:
import subprocess
import time
from sonic_platform_base.sfp_base import SfpBase
from sonic_platform_base.sonic_eeprom import eeprom_dts
from sonic_platform_base.sonic_sfp.sff8472 import sff8472InterfaceId
Expand All @@ -21,6 +20,7 @@
from sonic_platform_base.sonic_sfp.qsfp_dd import qsfp_dd_InterfaceId
from sonic_platform_base.sonic_sfp.qsfp_dd import qsfp_dd_Dom
from sonic_py_common.logger import Logger
from . import utils

except ImportError as e:
raise ImportError (str(e) + "- required module not found")
Expand Down Expand Up @@ -1496,9 +1496,18 @@ def get_lpmode(self):
Returns:
A Boolean, True if lpmode is enabled, False if disabled
"""
admin_pwr_mode, oper_pwr_mode = self.mgmt_phy_mod_pwr_attr_get(SX_MGMT_PHY_MOD_PWR_ATTR_PWR_MODE_E)

return oper_pwr_mode == SX_MGMT_PHY_MOD_PWR_MODE_LOW_E
if utils.is_host():
lpm_cmd = "docker exec syncd python /usr/share/sonic/platform/plugins/sfplpmget.py {}".format(self.index)
Copy link
Collaborator

@keboliu keboliu Mar 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the way here should remove the call to the legacy plugins, instead, we should load the platform and call the relevant platform API in pmon docker, like "docker exec pmon python {load platform API and call get API in one line}", is it possible?


try:
output = subprocess.check_output(lpm_cmd, shell=True, universal_newlines=True)
return 'LPM ON' in output
except subprocess.CalledProcessError as e:
print("Error! Unable to get LPM for {}, rc = {}, err msg: {}".format(self.index, e.returncode, e.output))
return False
else:
_, oper_pwr_mode = self.mgmt_phy_mod_pwr_attr_get(SX_MGMT_PHY_MOD_PWR_ATTR_PWR_MODE_E)
return oper_pwr_mode == SX_MGMT_PHY_MOD_PWR_MODE_LOW_E


def get_power_override(self):
Expand Down Expand Up @@ -1872,11 +1881,21 @@ def reset(self):

refer plugins/sfpreset.py
"""
rc = sx_mgmt_phy_mod_reset(self.sdk_handle, self.sdk_index)
if rc != SX_STATUS_SUCCESS:
logger.log_warning("sx_mgmt_phy_mod_reset failed, rc = %d" % rc)
if utils.is_host():
lpm_cmd = "docker exec syncd python /usr/share/sonic/platform/plugins/sfpreset.py {}".format(self.index)

try:
subprocess.check_output(lpm_cmd, shell=True, universal_newlines=True)
return True
except subprocess.CalledProcessError as e:
print("Error! Unable to set LPM for {}, rc = {}, err msg: {}".format(self.index, e.returncode, e.output))
return False
else:
rc = sx_mgmt_phy_mod_reset(self.sdk_handle, self.sdk_index)
if rc != SX_STATUS_SUCCESS:
logger.log_warning("sx_mgmt_phy_mod_reset failed, rc = %d" % rc)

return rc == SX_STATUS_SUCCESS
return rc == SX_STATUS_SUCCESS


def tx_disable(self, tx_disable):
Expand Down Expand Up @@ -2027,15 +2046,31 @@ def set_lpmode(self, lpmode):
Returns:
A boolean, True if lpmode is set successfully, False if not
"""
log_port_list = self.get_logical_ports()
if lpmode:
self._set_lpmode_raw(log_port_list, SX_MGMT_PHY_MOD_PWR_ATTR_PWR_MODE_E, SX_MGMT_PHY_MOD_PWR_MODE_LOW_E)
logger.log_info("Enabled low power mode for module [%d]" % (self.sdk_index))
if utils.is_host():
if lpmode == self.get_lpmode():
return True

# Compose LPM command
lpm_cmd = "docker exec syncd python /usr/share/sonic/platform/plugins/sfplpmset.py {} {}".format(self.index, 'on' if lpmode else 'off')

# Set LPM
try:
subprocess.check_output(lpm_cmd, shell=True, universal_newlines=True)
except subprocess.CalledProcessError as e:
print("Error! Unable to set LPM for {}, rc = {}, err msg: {}".format(self.index, e.returncode, e.output))
return False

return True
else:
self._set_lpmode_raw(log_port_list, SX_MGMT_PHY_MOD_PWR_ATTR_PWR_MODE_E, SX_MGMT_PHY_MOD_PWR_MODE_AUTO_E)
logger.log_info( "Disabled low power mode for module [%d]" % (self.sdk_index))

return True
log_port_list = self.get_logical_ports()
if lpmode:
self._set_lpmode_raw(log_port_list, SX_MGMT_PHY_MOD_PWR_ATTR_PWR_MODE_E, SX_MGMT_PHY_MOD_PWR_MODE_LOW_E)
logger.log_info("Enabled low power mode for module [%d]" % (self.sdk_index))
else:
self._set_lpmode_raw(log_port_list, SX_MGMT_PHY_MOD_PWR_ATTR_PWR_MODE_E, SX_MGMT_PHY_MOD_PWR_MODE_AUTO_E)
logger.log_info( "Disabled low power mode for module [%d]" % (self.sdk_index))

return True


def set_power_override(self, power_override, power_set):
Expand Down
34 changes: 34 additions & 0 deletions platform/mellanox/mlnx-platform-api/sonic_platform/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import subprocess

# flags to indicate whether this process is running in docker or host
_is_host = None


def read_str_from_file(file_path, default='', raise_exception=False):
"""
Read string content from file
Expand Down Expand Up @@ -55,3 +61,31 @@ def write_file(file_path, content, raise_exception=False):
else:
raise e
return True


def is_host():
"""
Test whether current process is running on the host or an docker
return True for host and False for docker
"""
global _is_host
if _is_host is not None:
return _is_host

_is_host = False
try:
proc = subprocess.Popen("docker --version 2>/dev/null",
stdout=subprocess.PIPE,
shell=True,
stderr=subprocess.STDOUT,
universal_newlines=True)
stdout = proc.communicate()[0]
proc.wait()
result = stdout.rstrip('\n')
if result != '':
_is_host = True

except OSError as e:
pass

return _is_host