Skip to content

Commit

Permalink
Fixed xcvrd shutdown flow.
Browse files Browse the repository at this point in the history
Signed-off-by: Nazarii Hnydyn <nazariig@mellanox.com>
  • Loading branch information
nazariig committed Mar 15, 2019
1 parent 3c6a57a commit aee8250
Show file tree
Hide file tree
Showing 3 changed files with 391 additions and 200 deletions.
32 changes: 18 additions & 14 deletions sonic-ledd/scripts/ledd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

"""
ledd
Front-panel LED control daemon for SONiC
"""

Expand All @@ -15,6 +14,7 @@ try:
import sys
import syslog
from swsscommon import swsscommon
from sonic_daemon_base.daemon_base import Logger
from sonic_daemon_base.daemon_base import DaemonBase
except ImportError, e:
raise ImportError (str(e) + " - required module not found")
Expand All @@ -35,15 +35,19 @@ Options:

LED_MODULE_NAME = "led_control"
LED_CLASS_NAME = "LedControl"

SELECT_TIMEOUT = 1000

LEDUTIL_LOAD_ERROR = 1
DAEMON_INIT_ERROR = 2

logger = Logger(SYSLOG_IDENTIFIER)

class DaemonLedd(DaemonBase):
def __init__(self):
DaemonBase.__init__(self)

def __exit__(self):
DaemonBase.__exit__(self)

# Run daemon
def run(self):
# Parse options if provided
if (len(sys.argv) > 1):
Expand All @@ -67,11 +71,11 @@ class DaemonLedd(DaemonBase):
# Load platform-specific LedControl module
led_control = self.load_platform_util(LED_MODULE_NAME, LED_CLASS_NAME)
if not led_control:
self.log_error("failed to load ledutil")
sys.exit(1)
logger.log_error("Failed to load ledutil", True)
sys.exit(LEDUTIL_LOAD_ERROR)

# Open a handle to the Application database
appl_db = self.db_connect(swsscommon.APPL_DB)
appl_db = DaemonBase.db_connect(swsscommon.APPL_DB)

# Subscribe to PORT table notifications in the Application DB
sel = swsscommon.Select()
Expand All @@ -88,7 +92,7 @@ class DaemonLedd(DaemonBase):
# Do not flood log when select times out
continue
if state != swsscommon.Select.OBJECT:
self.log_warning("sel.select() did not return swsscommon.Select.OBJECT")
logger.log_warning("sel.select() did not return swsscommon.Select.OBJECT")
continue

(key, op, fvp) = sst.pop()
Expand All @@ -107,15 +111,15 @@ class DaemonLedd(DaemonBase):

def main():
if not os.geteuid() == 0:
print "Error: Must be root to run this daemon"
logger.log_error("Error: Must be root to run this daemon")
sys.exit(1)

daemon_ledd = DaemonLedd()
if not daemon_ledd:
print "Failed to instantiate LED daemon"
sys.exit(1)
ledd = DaemonLedd()
if not ledd:
logger.log_error("Failed to instantiate LED daemon")
sys.exit(DAEMON_INIT_ERROR)

daemon_ledd.run()
ledd.run()

if __name__ == '__main__':
main()
101 changes: 76 additions & 25 deletions sonic-psud/scripts/psud
Original file line number Diff line number Diff line change
Expand Up @@ -12,66 +12,117 @@
try:
import sys
import time
import signal
import threading
from swsscommon import swsscommon
from sonic_daemon_base.daemon_base import Logger
from sonic_daemon_base.daemon_base import DaemonBase
except ImportError, e:
raise ImportError (str(e) + " - required module not found")

#============================= Constants =============================
#
# Constants ====================================================================
#

SYSLOG_IDENTIFIER = "psud"

PLATFORM_SPECIFIC_MODULE_NAME = "psuutil"
PLATFORM_SPECIFIC_CLASS_NAME = "PsuUtil"

CHASSIS_INFO_NUM_PSUS_FIELD = 'num_psus'

PSU_INFO_PRESENCE_FIELD = 'presence'
PSU_INFO_STATUS_FIELD = 'status'

PSU_INFO_UPDATE_PERIOD_SECS = 3

PSUUTIL_LOAD_ERROR = 1
DAEMON_INIT_ERROR = 2

logger = Logger(SYSLOG_IDENTIFIER)

#
# Helper functions =============================================================
#

def psu_db_update(psuutil, psu_tbl, num_psus):
for psu_index in range(1, num_psus + 1):
fvs = swsscommon.FieldValuePairs([(PSU_INFO_PRESENCE_FIELD,
'true' if psuutil.get_psu_presence(psu_index) else 'false'),
(PSU_INFO_STATUS_FIELD,
'true' if psuutil.get_psu_status(psu_index) else 'false')])
psu_tbl.set("PSU {}".format(psu_index), fvs)

#
# Daemon =======================================================================
#

class DaemonPsud(DaemonBase):
def __init__(self):
DaemonBase.__init__(self)

def __exit__(self):
DaemonBase.__exit__(self)
self.stop = threading.Event()

# Signal handler
def signal_handler(self, sig, frame):
if sig == signal.SIGHUP:
logger.log_info("Caught SIGHUP - ignoring...")
elif sig == signal.SIGINT:
logger.log_info("Caught SIGINT - exiting...")
self.stop.set()
elif sig == signal.SIGTERM:
logger.log_info("Caught SIGTERM - exiting...")
self.stop.set()
else:
logger.log_warning("Caught unhandled signal '" + sig + "'")

# Run daemon
def run(self):
logger.log_info("Starting up...")

# Load platform-specific psuutil class
platform_psuutil = self.load_platform_util(PLATFORM_SPECIFIC_MODULE_NAME, PLATFORM_SPECIFIC_CLASS_NAME)
if not platform_psuutil:
self.log_error("failed to load psuutil")
sys.exit(1)
logger.log_error("Failed to load psuutil", True)
sys.exit(PSUUTIL_LOAD_ERROR)

state_db = self.db_connect(swsscommon.STATE_DB)
# Connect to STATE_DB and create psu/chassis info tables
state_db = DaemonBase.db_connect(swsscommon.STATE_DB)
psu_tbl = swsscommon.Table(state_db, "PSU_INFO")
chassis_tbl = swsscommon.Table(state_db, "CHASSIS_INFO")

# Post psu number info to STATE_DB
num_psus = platform_psuutil.get_num_psus()
fvs = swsscommon.FieldValuePairs([('num_psus', str(num_psus))])
fvs = swsscommon.FieldValuePairs([(CHASSIS_INFO_NUM_PSUS_FIELD, str(num_psus))])
chassis_tbl.set('chassis 1', fvs)

# Start main loop to listen to the PSU change event.
self.log_info("Start main loop")
while True:
# Start main loop
logger.log_info("Start daemon main loop")

while not self.stop.wait(PSU_INFO_UPDATE_PERIOD_SECS):
psu_db_update(platform_psuutil, psu_tbl, num_psus)
time.sleep(PSU_INFO_UPDATE_PERIOD_SECS)

# Clean all the information from DB and then exit
logger.log_info("Stop daemon main loop")

# Delete all the information from DB and then exit
for psu_index in range(1, num_psus + 1):
psu_tbl._del("PSU {}".format(psu_index))

chassis_tbl._del('chassis 1')
return 1

def psu_db_update(psuutil, psu_tbl, num_psus):
for psu_index in range(1, num_psus + 1):
fvs = swsscommon.FieldValuePairs([('presence',
'true' if psuutil.get_psu_presence(psu_index) else 'false'),
('status',
'true' if psuutil.get_psu_status(psu_index) else 'false')])
psu_tbl.set("PSU {}".format(psu_index), fvs)
logger.log_info("Shutting down...")

#
# Main =========================================================================
#

def main():
daemon_psud = DaemonPsud()
if not daemon_psud:
print "Failed to load psu daemon utilities"
sys.exit(1)
psud = DaemonPsud()
if not psud:
logger.log_error("Failed to instantiate PSU daemon")
sys.exit(DAEMON_INIT_ERROR)

daemon_psud.run()
psud.run()

if __name__ == '__main__':
main()
Loading

0 comments on commit aee8250

Please sign in to comment.