[mlnx-platform] enhancement for xcvrd implementation on mlnx platform (#1937)
* mlnx enhancement for xcvrd implementation * fix conflict
This commit is contained in:
parent
ec8a7f5e54
commit
5a17a04c82
@ -30,6 +30,7 @@ class SfpUtil(SfpUtilBase):
|
|||||||
db_sel_object = None
|
db_sel_object = None
|
||||||
db_sel_tbl = None
|
db_sel_tbl = None
|
||||||
state_db = None
|
state_db = None
|
||||||
|
sfpd_status_tbl = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def port_start(self):
|
def port_start(self):
|
||||||
@ -176,6 +177,12 @@ class SfpUtil(SfpUtilBase):
|
|||||||
self.db_sel.addSelectable(self.db_sel_tbl)
|
self.db_sel.addSelectable(self.db_sel_tbl)
|
||||||
self.db_sel_timeout = swsscommon.Select.TIMEOUT
|
self.db_sel_timeout = swsscommon.Select.TIMEOUT
|
||||||
self.db_sel_object = swsscommon.Select.OBJECT
|
self.db_sel_object = swsscommon.Select.OBJECT
|
||||||
|
self.sfpd_status_tbl = swsscommon.Table(self.state_db, 'MLNX_SFPD_TASK')
|
||||||
|
|
||||||
|
# Check the liveness of mlnx-sfpd, if it failed, return false
|
||||||
|
keys = self.sfpd_status_tbl.getKeys()
|
||||||
|
if 'LIVENESS' not in keys:
|
||||||
|
return False, phy_port_dict
|
||||||
|
|
||||||
(state, c) = self.db_sel.select(timeout)
|
(state, c) = self.db_sel.select(timeout)
|
||||||
if state == self.db_sel_timeout:
|
if state == self.db_sel_timeout:
|
||||||
|
@ -30,6 +30,7 @@ class SfpUtil(SfpUtilBase):
|
|||||||
db_sel_object = None
|
db_sel_object = None
|
||||||
db_sel_tbl = None
|
db_sel_tbl = None
|
||||||
state_db = None
|
state_db = None
|
||||||
|
sfpd_status_tbl = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def port_start(self):
|
def port_start(self):
|
||||||
@ -48,7 +49,7 @@ class SfpUtil(SfpUtilBase):
|
|||||||
return self._port_to_eeprom_mapping
|
return self._port_to_eeprom_mapping
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
eeprom_path = "/sys/class/i2c-adapter/i2c-2/2-0048/hwmon/hwmon6/qsfp{0}_eeprom"
|
eeprom_path = "/sys/class/i2c-adapter/i2c-2/2-0048/hwmon/hwmon7/qsfp{0}_eeprom"
|
||||||
|
|
||||||
for x in range(0, self.port_end + 1):
|
for x in range(0, self.port_end + 1):
|
||||||
self._port_to_eeprom_mapping[x] = eeprom_path.format(x + self.EEPROM_OFFSET)
|
self._port_to_eeprom_mapping[x] = eeprom_path.format(x + self.EEPROM_OFFSET)
|
||||||
@ -177,6 +178,12 @@ class SfpUtil(SfpUtilBase):
|
|||||||
self.db_sel.addSelectable(self.db_sel_tbl)
|
self.db_sel.addSelectable(self.db_sel_tbl)
|
||||||
self.db_sel_timeout = swsscommon.Select.TIMEOUT
|
self.db_sel_timeout = swsscommon.Select.TIMEOUT
|
||||||
self.db_sel_object = swsscommon.Select.OBJECT
|
self.db_sel_object = swsscommon.Select.OBJECT
|
||||||
|
self.sfpd_status_tbl = swsscommon.Table(self.state_db, 'MLNX_SFPD_TASK')
|
||||||
|
|
||||||
|
# Check the liveness of mlnx-sfpd, if it failed, return false
|
||||||
|
keys = self.sfpd_status_tbl.getKeys()
|
||||||
|
if 'LIVENESS' not in keys:
|
||||||
|
return False, phy_port_dict
|
||||||
|
|
||||||
(state, c) = self.db_sel.select(timeout)
|
(state, c) = self.db_sel.select(timeout)
|
||||||
if state == self.db_sel_timeout:
|
if state == self.db_sel_timeout:
|
||||||
|
@ -30,6 +30,7 @@ class SfpUtil(SfpUtilBase):
|
|||||||
db_sel_object = None
|
db_sel_object = None
|
||||||
db_sel_tbl = None
|
db_sel_tbl = None
|
||||||
state_db = None
|
state_db = None
|
||||||
|
sfpd_status_tbl = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def port_start(self):
|
def port_start(self):
|
||||||
@ -176,6 +177,12 @@ class SfpUtil(SfpUtilBase):
|
|||||||
self.db_sel.addSelectable(self.db_sel_tbl)
|
self.db_sel.addSelectable(self.db_sel_tbl)
|
||||||
self.db_sel_timeout = swsscommon.Select.TIMEOUT
|
self.db_sel_timeout = swsscommon.Select.TIMEOUT
|
||||||
self.db_sel_object = swsscommon.Select.OBJECT
|
self.db_sel_object = swsscommon.Select.OBJECT
|
||||||
|
self.sfpd_status_tbl = swsscommon.Table(self.state_db, 'MLNX_SFPD_TASK')
|
||||||
|
|
||||||
|
# Check the liveness of mlnx-sfpd, if it failed, return false
|
||||||
|
keys = self.sfpd_status_tbl.getKeys()
|
||||||
|
if 'LIVENESS' not in keys:
|
||||||
|
return False, phy_port_dict
|
||||||
|
|
||||||
(state, c) = self.db_sel.select(timeout)
|
(state, c) = self.db_sel.select(timeout)
|
||||||
if state == self.db_sel_timeout:
|
if state == self.db_sel_timeout:
|
||||||
|
@ -30,6 +30,7 @@ class SfpUtil(SfpUtilBase):
|
|||||||
db_sel_object = None
|
db_sel_object = None
|
||||||
db_sel_tbl = None
|
db_sel_tbl = None
|
||||||
state_db = None
|
state_db = None
|
||||||
|
sfpd_status_tbl = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def port_start(self):
|
def port_start(self):
|
||||||
@ -176,6 +177,12 @@ class SfpUtil(SfpUtilBase):
|
|||||||
self.db_sel.addSelectable(self.db_sel_tbl)
|
self.db_sel.addSelectable(self.db_sel_tbl)
|
||||||
self.db_sel_timeout = swsscommon.Select.TIMEOUT
|
self.db_sel_timeout = swsscommon.Select.TIMEOUT
|
||||||
self.db_sel_object = swsscommon.Select.OBJECT
|
self.db_sel_object = swsscommon.Select.OBJECT
|
||||||
|
self.sfpd_status_tbl = swsscommon.Table(self.state_db, 'MLNX_SFPD_TASK')
|
||||||
|
|
||||||
|
# Check the liveness of mlnx-sfpd, if it failed, return false
|
||||||
|
keys = self.sfpd_status_tbl.getKeys()
|
||||||
|
if 'LIVENESS' not in keys:
|
||||||
|
return False, phy_port_dict
|
||||||
|
|
||||||
(state, c) = self.db_sel.select(timeout)
|
(state, c) = self.db_sel.select(timeout)
|
||||||
if state == self.db_sel_timeout:
|
if state == self.db_sel_timeout:
|
||||||
|
@ -11,6 +11,7 @@ import time
|
|||||||
import syslog
|
import syslog
|
||||||
import signal
|
import signal
|
||||||
import json
|
import json
|
||||||
|
import threading
|
||||||
from python_sdk_api.sx_api import *
|
from python_sdk_api.sx_api import *
|
||||||
from swsssdk import SonicV2Connector
|
from swsssdk import SonicV2Connector
|
||||||
|
|
||||||
@ -26,10 +27,11 @@ STATUS_PLUGIN = '1'
|
|||||||
STATUS_PLUGOUT = '0'
|
STATUS_PLUGOUT = '0'
|
||||||
STATUS_UNKNOWN = '2'
|
STATUS_UNKNOWN = '2'
|
||||||
|
|
||||||
|
SFPD_LIVENESS_UPDATE_INTERVAL_SECS = 30
|
||||||
|
|
||||||
sfp_value_status_dict = {SDK_SFP_STATE_IN:STATUS_PLUGIN, SDK_SFP_STATE_OUT:STATUS_PLUGOUT}
|
sfp_value_status_dict = {SDK_SFP_STATE_IN:STATUS_PLUGIN, SDK_SFP_STATE_OUT:STATUS_PLUGOUT}
|
||||||
|
|
||||||
# ========================== Syslog wrappers ==========================
|
# ========================== Syslog wrappers ==========================
|
||||||
|
|
||||||
def log_info(msg, also_print_to_console=False):
|
def log_info(msg, also_print_to_console=False):
|
||||||
syslog.openlog(SYSLOG_IDENTIFIER)
|
syslog.openlog(SYSLOG_IDENTIFIER)
|
||||||
syslog.syslog(syslog.LOG_INFO, msg)
|
syslog.syslog(syslog.LOG_INFO, msg)
|
||||||
@ -55,7 +57,6 @@ def log_error(msg, also_print_to_console=False):
|
|||||||
print(msg)
|
print(msg)
|
||||||
|
|
||||||
# ========================== Signal Handling ==========================
|
# ========================== Signal Handling ==========================
|
||||||
|
|
||||||
def signal_handler(sig, frame):
|
def signal_handler(sig, frame):
|
||||||
if sig == signal.SIGHUP:
|
if sig == signal.SIGHUP:
|
||||||
log_info("Caught SIGHUP - ignoring...")
|
log_info("Caught SIGHUP - ignoring...")
|
||||||
@ -83,11 +84,14 @@ def sx_recv(fd_p, handle):
|
|||||||
port_cnt_p = new_uint32_t_p()
|
port_cnt_p = new_uint32_t_p()
|
||||||
uint32_t_p_assign(port_cnt_p,64)
|
uint32_t_p_assign(port_cnt_p,64)
|
||||||
label_port_list = []
|
label_port_list = []
|
||||||
|
status = True
|
||||||
|
module_state = 0
|
||||||
|
|
||||||
rc = sx_lib_host_ifc_recv(fd_p, pkt, pkt_size_p, recv_info_p)
|
rc = sx_lib_host_ifc_recv(fd_p, pkt, pkt_size_p, recv_info_p)
|
||||||
if rc != 0:
|
if rc != 0:
|
||||||
log_error("event receive exit with error, rc %d" % rc)
|
log_error("event receive exit with error, rc %d" % rc)
|
||||||
exit(rc)
|
status = False
|
||||||
|
return status, label_port_list, module_state
|
||||||
|
|
||||||
pmpe_t = recv_info_p.event_info.pmpe
|
pmpe_t = recv_info_p.event_info.pmpe
|
||||||
port_list_size = pmpe_t.list_size
|
port_list_size = pmpe_t.list_size
|
||||||
@ -106,7 +110,7 @@ def sx_recv(fd_p, handle):
|
|||||||
break
|
break
|
||||||
label_port_list.append(lable_port)
|
label_port_list.append(lable_port)
|
||||||
|
|
||||||
return label_port_list, module_state
|
return status, label_port_list, module_state,
|
||||||
|
|
||||||
def send_sfp_notification(db, interface, state):
|
def send_sfp_notification(db, interface, state):
|
||||||
sfp_notify = [interface, state]
|
sfp_notify = [interface, state]
|
||||||
@ -114,6 +118,35 @@ def send_sfp_notification(db, interface, state):
|
|||||||
db.publish('STATE_DB', 'TRANSCEIVER_NOTIFY', msg)
|
db.publish('STATE_DB', 'TRANSCEIVER_NOTIFY', msg)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
def update_sfpd_liveness_key(db, timeout_secs):
|
||||||
|
if db.exists('STATE_DB', 'MLNX_SFPD_TASK|LIVENESS'):
|
||||||
|
db.expire('STATE_DB', 'MLNX_SFPD_TASK|LIVENESS', timeout_secs)
|
||||||
|
else:
|
||||||
|
db.set('STATE_DB', 'MLNX_SFPD_TASK|LIVENESS', 'value', 'ok')
|
||||||
|
db.expire('STATE_DB', 'MLNX_SFPD_TASK|LIVENESS', timeout_secs)
|
||||||
|
|
||||||
|
# Timer thread wrapper class to update mlnx-sfpd liveness info to DB periodically
|
||||||
|
class sfpd_liveness_update_task:
|
||||||
|
def __init__(self, db):
|
||||||
|
self.task_stopping_event = threading.Event()
|
||||||
|
self.task_timer = None
|
||||||
|
self.state_db = db
|
||||||
|
|
||||||
|
def task_run(self):
|
||||||
|
if self.task_stopping_event.isSet():
|
||||||
|
log_error("Error: sfpd liveness update thread received stop event, exiting...")
|
||||||
|
return
|
||||||
|
|
||||||
|
update_sfpd_liveness_key(self.state_db, 2*SFPD_LIVENESS_UPDATE_INTERVAL_SECS)
|
||||||
|
|
||||||
|
self.task_timer = threading.Timer(SFPD_LIVENESS_UPDATE_INTERVAL_SECS, self.task_run)
|
||||||
|
self.task_timer.start()
|
||||||
|
|
||||||
|
def task_stop(self):
|
||||||
|
self.task_stopping_event.set()
|
||||||
|
self.task_timer.join()
|
||||||
|
|
||||||
|
|
||||||
# main start
|
# main start
|
||||||
def main():
|
def main():
|
||||||
# Register our signal handlers
|
# Register our signal handlers
|
||||||
@ -121,7 +154,11 @@ def main():
|
|||||||
signal.signal(signal.SIGINT, signal_handler)
|
signal.signal(signal.SIGINT, signal_handler)
|
||||||
signal.signal(signal.SIGTERM, signal_handler)
|
signal.signal(signal.SIGTERM, signal_handler)
|
||||||
|
|
||||||
#open SDK handler
|
# Connect to state db for notification sending
|
||||||
|
state_db = SonicV2Connector(host=REDIS_HOSTIP)
|
||||||
|
state_db.connect(state_db.STATE_DB)
|
||||||
|
|
||||||
|
# Open SDK handler
|
||||||
log_info("starting mlnx-sfpd...")
|
log_info("starting mlnx-sfpd...")
|
||||||
rc, handle = sx_api_open(None)
|
rc, handle = sx_api_open(None)
|
||||||
retry_time = 1
|
retry_time = 1
|
||||||
@ -130,17 +167,17 @@ def main():
|
|||||||
retry_time += 1
|
retry_time += 1
|
||||||
rc, handle = sx_api_open(None)
|
rc, handle = sx_api_open(None)
|
||||||
if retry_time > 20:
|
if retry_time > 20:
|
||||||
log_error("Failed to open api handle.\nPlease check that SDK is running.")
|
log_error("Failed to open api handle. Please check that SDK is running.")
|
||||||
sys.exit(errno.EACCES)
|
sys.exit(errno.EACCES)
|
||||||
|
|
||||||
#open recv fd
|
# Open recv fd
|
||||||
rx_fd_p = new_sx_fd_t_p()
|
rx_fd_p = new_sx_fd_t_p()
|
||||||
rc = sx_api_host_ifc_open(handle, rx_fd_p)
|
rc = sx_api_host_ifc_open(handle, rx_fd_p)
|
||||||
if rc != 0:
|
if rc != 0:
|
||||||
log_error("sx_api_host_ifc_open exit with error, rc %d" % rc)
|
log_error("sx_api_host_ifc_open exit with error, rc %d" % rc)
|
||||||
exit(rc)
|
exit(rc)
|
||||||
|
|
||||||
# set up general host ifc parameters
|
# Set up general host ifc parameters
|
||||||
swid = 0
|
swid = 0
|
||||||
cmd = SX_ACCESS_CMD_REGISTER
|
cmd = SX_ACCESS_CMD_REGISTER
|
||||||
uc_p = new_sx_user_channel_t_p()
|
uc_p = new_sx_user_channel_t_p()
|
||||||
@ -153,26 +190,34 @@ def main():
|
|||||||
log_error("sx_api_host_ifc_trap_id_register_set exit with error, rc %d" % rc)
|
log_error("sx_api_host_ifc_trap_id_register_set exit with error, rc %d" % rc)
|
||||||
exit(rc)
|
exit(rc)
|
||||||
|
|
||||||
#connect to state db for notification sending
|
liveness_info_update = sfpd_liveness_update_task(state_db)
|
||||||
state_db = SonicV2Connector(host=REDIS_HOSTIP)
|
liveness_info_update.task_run()
|
||||||
state_db.connect(state_db.STATE_DB)
|
|
||||||
|
|
||||||
#main loop for sfp event listening
|
# Main loop for sfp event listening
|
||||||
log_info("mlnx-sfpd started")
|
log_info("mlnx-sfpd started")
|
||||||
while True:
|
while True:
|
||||||
state = STATUS_UNKNOWN
|
sfp_state = STATUS_UNKNOWN
|
||||||
port_list, module_state = sx_recv(rx_fd_p, handle)
|
rc, port_list, module_state = sx_recv(rx_fd_p, handle)
|
||||||
if module_state in sfp_value_status_dict: state = sfp_value_status_dict[module_state]
|
if not rc:
|
||||||
|
log_error("Failed to recv event from SDK, please check that SDK is running.")
|
||||||
|
break
|
||||||
|
|
||||||
if state != STATUS_UNKNOWN:
|
if module_state in sfp_value_status_dict: sfp_state = sfp_value_status_dict[module_state]
|
||||||
|
|
||||||
|
if sfp_state != STATUS_UNKNOWN:
|
||||||
for port in port_list:
|
for port in port_list:
|
||||||
log_info("SFP on port %d state %s" % (port, state))
|
log_info("SFP on port %d state %s" % (port, sfp_state))
|
||||||
send_sfp_notification(state_db, str(port), state)
|
send_sfp_notification(state_db, str(port), sfp_state)
|
||||||
|
|
||||||
log_info("sfp change event handling done")
|
log_info("sfp change event handling done")
|
||||||
|
|
||||||
'''
|
# Stop liveness update task
|
||||||
# TODO: clean open handlers before exit, need find out which errors can be raised by SDK in this case.
|
liveness_info_update.task_stop()
|
||||||
|
|
||||||
|
# Remove mlnx-sfpd liveness key in DB if not expired yet.
|
||||||
|
if state_db.exists('STATE_DB', 'MLNX_SFPD_TASK|LIVENESS'):
|
||||||
|
state_db.delete(state_db, 'MLNX_SFPD_TASK|LIVENESS')
|
||||||
|
|
||||||
# unregister trap id
|
# unregister trap id
|
||||||
cmd = SX_ACCESS_CMD_DEREGISTER
|
cmd = SX_ACCESS_CMD_DEREGISTER
|
||||||
rc = sx_api_host_ifc_trap_id_register_set(handle, cmd, swid, trap_id, uc_p)
|
rc = sx_api_host_ifc_trap_id_register_set(handle, cmd, swid, trap_id, uc_p)
|
||||||
@ -180,20 +225,20 @@ def main():
|
|||||||
log_error("sx_api_host_ifc_trap_id_register_set exit with error, rc %d" % rc)
|
log_error("sx_api_host_ifc_trap_id_register_set exit with error, rc %d" % rc)
|
||||||
exit(rc)
|
exit(rc)
|
||||||
|
|
||||||
# close read fp
|
# Close read fp
|
||||||
rc = sx_api_host_ifc_close(handle, rx_fd_p)
|
rc = sx_api_host_ifc_close(handle, rx_fd_p)
|
||||||
if rc != 0:
|
if rc != 0:
|
||||||
log_error("sx_api_host_ifc_close exit with error, rc %d" % rc)
|
log_error("sx_api_host_ifc_close exit with error, rc %d" % rc)
|
||||||
exit(rc)
|
exit(rc)
|
||||||
|
|
||||||
# close sdk handler
|
# Close sdk handler
|
||||||
rc = sx_api_close(handle)
|
rc = sx_api_close(handle)
|
||||||
if rc != 0:
|
if rc != 0:
|
||||||
log_error("exit with error, rc %d" % rc)
|
log_error("sx_api_close exit with error, rc %d" % rc)
|
||||||
exit(rc)
|
exit(rc)
|
||||||
|
|
||||||
log_info("mlnx-sfpd exited")
|
log_info("mlnx-sfpd exited")
|
||||||
'''
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user