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

split initialization of sonic_platform to a per-daemon basis #3

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions files/build_templates/sonic_debian_extension.j2
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ sudo cp {{platform_common_py2_wheel_path}} $FILESYSTEM_ROOT/$PLATFORM_COMMON_PY2
sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip install $PLATFORM_COMMON_PY2_WHEEL_NAME
sudo rm -rf $FILESYSTEM_ROOT/$PLATFORM_COMMON_PY2_WHEEL_NAME

# Install sonic-daemon-base Python 2 package
DAEMON_BASE_PY2_WHEEL_NAME=$(basename {{daemon_base_py2_wheel_path}})
sudo cp {{daemon_base_py2_wheel_path}} $FILESYSTEM_ROOT/$DAEMON_BASE_PY2_WHEEL_NAME
sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip install $DAEMON_BASE_PY2_WHEEL_NAME
sudo rm -rf $FILESYSTEM_ROOT/$DAEMON_BASE_PY2_WHEEL_NAME

# Install built Python Click package (and its dependencies via 'apt-get -y install -f')
# Do this before installing sonic-utilities so that it doesn't attempt to install
# an older version as part of its dependencies
Expand Down Expand Up @@ -343,6 +349,12 @@ sudo cp $files_path/$ISSU_VERSION_FILE $FILESYSTEM_ROOT/etc/mlnx/issu-version
sudo cp $files_path/$MLNX_FFB_SCRIPT $FILESYSTEM_ROOT/usr/bin/mlnx-ffb.sh
j2 platform/mellanox/mlnx-fw-upgrade.j2 | sudo tee $FILESYSTEM_ROOT/usr/bin/mlnx-fw-upgrade.sh
sudo chmod 755 $FILESYSTEM_ROOT/usr/bin/mlnx-fw-upgrade.sh

# Install mlnx-sonic-platform-common Python 2 package
MLNX_PLATFORM_COMMON_PY2_WHEEL_NAME=$(basename {{mlnx_platform_api_py2_wheel_path}})
sudo cp {{mlnx_platform_api_py2_wheel_path}} $FILESYSTEM_ROOT/$MLNX_PLATFORM_COMMON_PY2_WHEEL_NAME
sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip install $MLNX_PLATFORM_COMMON_PY2_WHEEL_NAME
sudo rm -rf $FILESYSTEM_ROOT/$MLNX_PLATFORM_COMMON_PY2_WHEEL_NAME
{% endif %}

{%- if SONIC_ROUTING_STACK == "frr" %}
Expand Down
4 changes: 2 additions & 2 deletions files/image_config/process-reboot-cause/process-reboot-cause
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ def main():
# if there is no sonic_platform package installed, we only provide
# software-related reboot causes.
try:
import sonic_platform
import sonic_platform.platform

# Check if the previous reboot was caused by hardware
platform = sonic_platform.platform.Platform()
platform = sonic_platform.platform.Platform(True)

chassis = platform.get_chassis()

Expand Down
1 change: 1 addition & 0 deletions platform/mellanox/mlnx-platform-api.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ $(SONIC_PLATFORM_API_PY2)_SRC_PATH = $(PLATFORM_PATH)/mlnx-platform-api
$(SONIC_PLATFORM_API_PY2)_PYTHON_VERSION = 2
SONIC_PYTHON_WHEELS += $(SONIC_PLATFORM_API_PY2)

export mlnx_platform_api_py2_wheel_path="$(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_PLATFORM_API_PY2))"
44 changes: 34 additions & 10 deletions platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@

try:
from sonic_platform_base.chassis_base import ChassisBase
from sonic_platform.psu import Psu
from sonic_platform.fan import Fan
from sonic_platform.fan import FAN_PATH
from sonic_platform.sfp import SFP
from sonic_platform.watchdog import get_watchdog
from sonic_daemon_base.daemon_base import Logger
from eeprom import Eeprom
from os import listdir
from os.path import isfile, join
import io
Expand All @@ -41,9 +35,12 @@
REBOOT_CAUSE_ROOT = HWMGMT_SYSTEM_ROOT

REBOOT_CAUSE_POWER_LOSS_FILE = 'reset_main_pwr_fail'
REBOOT_CAUSE_AUX_POWER_LOSS_FILE = 'reset_aux_pwr_or_ref'
REBOOT_CAUSE_THERMAL_OVERLOAD_ASIC_FILE = 'reset_asic_thermal'
REBOOT_CAUSE_WATCHDOG_FILE = 'reset_hotswap_or_wd'
REBOOT_CAUSE_MLNX_FIRMWARE_RESET = 'reset_fw_reset'
REBOOT_CAUSE_LONG_PB = 'reset_long_pb'
REBOOT_CAUSE_SHORT_PB = 'reset_short_pb'

REBOOT_CAUSE_FILE_LENGTH = 1

Expand Down Expand Up @@ -78,14 +75,27 @@ class Chassis(ChassisBase):
def __init__(self):
super(Chassis, self).__init__()

# move the initialization of each components to their dedicated initializer
# which will be called from platform

def initialize_psu(self):
from sonic_platform.psu import Psu
# Initialize PSU list
self.psu_module = Psu
for index in range(MLNX_NUM_PSU):
psu = Psu(index)
self._psu_list.append(psu)

def initialize_watchdog(self):
from sonic_platform.watchdog import get_watchdog
# Initialize watchdog
self._watchdog = get_watchdog()

def initialize_fan(self):
from sonic_platform.fan import Fan
from sonic_platform.fan import FAN_PATH
self.fan_module = Fan
self.fan_path = FAN_PATH
# Initialize FAN list
multi_rotor_in_drawer = False
num_of_fan, num_of_drawer = self._extract_num_of_fans_and_fan_drawers()
Expand All @@ -98,6 +108,9 @@ def __init__(self):
fan = Fan(index, index)
self._fan_list.append(fan)

def initialize_sfp(self):
from sonic_platform.sfp import SFP
self.sfp_module = SFP
# Initialize SFP list
port_position_tuple = self._get_port_position_tuple_by_sku_name()
self.PORT_START = port_position_tuple[0]
Expand All @@ -112,9 +125,12 @@ def __init__(self):
sfp_module = SFP(index, 'SFP')
self._sfp_list.append(sfp_module)

def initialize_eeprom(self):
from eeprom import Eeprom
# Initialize EEPROM
self.eeprom = Eeprom()

def initialize_components_list(self):
# Initialize component list
self._component_name_list.append(COMPONENT_BIOS)
self._component_name_list.append(COMPONENT_FIRMWARE)
Expand All @@ -124,8 +140,8 @@ def __init__(self):
def _extract_num_of_fans_and_fan_drawers(self):
num_of_fan = 0
num_of_drawer = 0
for f in listdir(FAN_PATH):
if isfile(join(FAN_PATH, f)):
for f in listdir(self.fan_path):
if isfile(join(self.fan_path, f)):
match_obj = re.match('fan(\d+)_speed_get', f)
if match_obj != None:
if int(match_obj.group(1)) > num_of_fan:
Expand Down Expand Up @@ -193,7 +209,10 @@ def _verify_reboot_cause(self, filename):
/var/run/hwmanagement/system (which is defined as REBOOT_CAUSE_ROOT)
If a reboot cause file doesn't exists, returns '0'.
'''
return bool(int(self._read_generic_file(join(REBOOT_CAUSE_ROOT, filename), REBOOT_CAUSE_FILE_LENGTH).rstrip('\n')))
try:
return bool(int(self._read_generic_file(join(REBOOT_CAUSE_ROOT, filename), REBOOT_CAUSE_FILE_LENGTH).rstrip('\n')))
except:
return False

def get_reboot_cause(self):
"""
Expand All @@ -210,6 +229,8 @@ def get_reboot_cause(self):
minor_cause = ''
if self._verify_reboot_cause(REBOOT_CAUSE_POWER_LOSS_FILE):
major_cause = self.REBOOT_CAUSE_POWER_LOSS
elif self._verify_reboot_cause(REBOOT_CAUSE_AUX_POWER_LOSS_FILE):
major_cause = self.REBOOT_CAUSE_POWER_LOSS
elif self._verify_reboot_cause(REBOOT_CAUSE_THERMAL_OVERLOAD_ASIC_FILE):
major_cause = self.REBOOT_CAUSE_THERMAL_OVERLOAD_ASIC
elif self._verify_reboot_cause(REBOOT_CAUSE_WATCHDOG_FILE):
Expand All @@ -218,9 +239,12 @@ def get_reboot_cause(self):
major_cause = self.REBOOT_CAUSE_HARDWARE_OTHER
if self._verify_reboot_cause(REBOOT_CAUSE_MLNX_FIRMWARE_RESET):
minor_cause = "Reset by ASIC firmware"
elif self._verify_reboot_cause(REBOOT_CAUSE_LONG_PB):
minor_cause = "Reset by long press on power button"
elif self._verify_reboot_cause(REBOOT_CAUSE_SHORT_PB):
minor_cause = "Reset by short press on power button"
else:
major_cause = self.REBOOT_CAUSE_NON_HARDWARE

return major_cause, minor_cause

def _get_cpld_version(self, version_file):
Expand Down
27 changes: 27 additions & 0 deletions platform/mellanox/mlnx-platform-api/sonic_platform/platform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python

#############################################################################
# Mellanox
#
# implementation of new platform api
#############################################################################

try:
from sonic_platform_base.platform_base import PlatformBase
from sonic_platform.chassis import Chassis
except ImportError as e:
raise ImportError(str(e) + "- required module not found")

class Platform(PlatformBase):
def __init__(self, is_host = False):
PlatformBase.__init__(self)
if is_host:
self._chassis = Chassis()
self._chassis.initialize_watchdog()
else:
self._chassis = Chassis()
self._chassis.initialize_psu()
self._chassis.initialize_fan()
self._chassis.initialize_sfp()
self._chassis.initialize_eeprom()
self._chassis.initialize_components_list()
1 change: 1 addition & 0 deletions rules/sonic-daemon-base.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ $(SONIC_DAEMON_BASE_PY2)_SRC_PATH = $(SRC_PATH)/sonic-daemon-base
$(SONIC_DAEMON_BASE_PY2)_PYTHON_VERSION = 2
SONIC_PYTHON_WHEELS += $(SONIC_DAEMON_BASE_PY2)

export daemon_base_py2_wheel_path="$(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_DAEMON_BASE_PY2))"
3 changes: 2 additions & 1 deletion slave.mk
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,8 @@ $(addprefix $(TARGET_PATH)/, $(SONIC_INSTALLERS)) : $(TARGET_PATH)/% : \
$(addprefix $(PYTHON_DEBS_PATH)/,$(SONIC_UTILS)) \
$(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_CONFIG_ENGINE)) \
$(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_PLATFORM_COMMON_PY2)) \
$(addprefix $(PYTHON_WHEELS_PATH)/,$(REDIS_DUMP_LOAD_PY2))
$(addprefix $(PYTHON_WHEELS_PATH)/,$(REDIS_DUMP_LOAD_PY2)) \
$(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_PLATFORM_API_PY2))
$(HEADER)
# Pass initramfs and linux kernel explicitly. They are used for all platforms
export debs_path="$(STRETCH_DEBS_PATH)"
Expand Down
32 changes: 19 additions & 13 deletions src/sonic-daemon-base/sonic_daemon_base/daemon_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import subprocess
import os
import sys
import syslog
from swsscommon import swsscommon
except ImportError, e:
raise ImportError (str(e) + " - required module not found")

Expand Down Expand Up @@ -38,6 +36,7 @@
#

def db_connect(db):
from swsscommon import swsscommon
return swsscommon.DBConnector(db,
REDIS_HOSTNAME,
REDIS_PORT,
Expand All @@ -49,37 +48,39 @@ def db_connect(db):

class Logger(object):
def __init__(self, syslog_identifier):
syslog.openlog(ident=syslog_identifier, logoption=syslog.LOG_NDELAY, facility=syslog.LOG_DAEMON)
import syslog
self.syslog = syslog
self.syslog.openlog(ident=syslog_identifier, logoption=self.syslog.LOG_NDELAY, facility=self.syslog.LOG_DAEMON)

def __del__(self):
syslog.closelog()
self.syslog.closelog()

def log_error(self, msg, also_print_to_console=False):
syslog.syslog(syslog.LOG_ERR, msg)
self.syslog.syslog(self.syslog.LOG_ERR, msg)

if also_print_to_console:
print msg

def log_warning(self, msg, also_print_to_console=False):
syslog.syslog(syslog.LOG_WARNING, msg)
self.syslog.syslog(self.syslog.LOG_WARNING, msg)

if also_print_to_console:
print msg

def log_notice(self, msg, also_print_to_console=False):
syslog.syslog(syslog.LOG_NOTICE, msg)
self.syslog.syslog(self.syslog.LOG_NOTICE, msg)

if also_print_to_console:
print msg

def log_info(self, msg, also_print_to_console=False):
syslog.syslog(syslog.LOG_INFO, msg)
self.syslog.syslog(self.syslog.LOG_INFO, msg)

if also_print_to_console:
print msg

def log_debug(self, msg, also_print_to_console=False):
syslog.syslog(syslog.LOG_DEBUG, msg)
self.syslog.syslog(self.syslog.LOG_DEBUG, msg)

if also_print_to_console:
print msg
Expand All @@ -89,6 +90,11 @@ def log_debug(self, msg, also_print_to_console=False):
#

class DaemonBase(object):

DAEMON_WATCHDOG = "watch-dog"
DAEMON_SYSEEPROM = "syseepromd"
DAEMON_XCVRD = "xcvrd"
DAEMON_PSUD = "psud"
def __init__(self):
# Register our signal handlers
signal.signal(signal.SIGHUP, self.signal_handler)
Expand All @@ -98,15 +104,15 @@ def __init__(self):
# Signal handler
def signal_handler(self, sig, frame):
if sig == signal.SIGHUP:
syslog.syslog(syslog.LOG_INFO, "Caught SIGHUP - ignoring...")
self.syslog.syslog(self.syslog.LOG_INFO, "Caught SIGHUP - ignoring...")
elif sig == signal.SIGINT:
syslog.syslog(syslog.LOG_INFO, "Caught SIGINT - exiting...")
self.syslog.syslog(self.syslog.LOG_INFO, "Caught SIGINT - exiting...")
sys.exit(128 + sig)
elif sig == signal.SIGTERM:
syslog.syslog(syslog.LOG_INFO, "Caught SIGTERM - exiting...")
self.syslog.syslog(self.syslog.LOG_INFO, "Caught SIGTERM - exiting...")
sys.exit(128 + sig)
else:
syslog.syslog(syslog.LOG_WARNING, "Caught unhandled signal '" + sig + "'")
self.syslog.syslog(self.syslog.LOG_WARNING, "Caught unhandled signal '" + sig + "'")

# Returns platform and hwsku
def get_platform_and_hwsku(self):
Expand Down