Skip to content

Commit c5590a0

Browse files
committed
port_mapping: drop the hacks for CMIS
Signed-off-by: Dante Su <[email protected]>
1 parent 999e156 commit c5590a0

File tree

2 files changed

+69
-58
lines changed

2 files changed

+69
-58
lines changed

sonic-xcvrd/xcvrd/xcvrd.py

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -834,82 +834,75 @@ def init_port_sfp_status_tbl(port_mapping, stop_event=threading.Event()):
834834

835835
class CmisManagerTask:
836836

837-
def __init__(self):
837+
def __init__(self, port_mapping):
838838
# Max. number of the worker process/thread
839839
self.worker_max = 5
840840
self.task_workers = []
841841
self.task_stopping_event = multiprocessing.Event()
842842
self.task_queue = multiprocessing.Manager().Queue()
843-
self.port_mapping = port_mapping.get_port_mapping()
843+
self.port_mapping = copy.deepcopy(port_mapping)
844+
845+
def log_notice(self, message):
846+
helper_logger.log_notice("CMIS (pid {0}): {1}".format(os.getpid(), message))
844847

845848
def on_port_config_change(self, port_change_event):
846-
if port_change_event.event_type != port_change_event.PORT_ADD:
849+
if port_change_event.event_type != port_change_event.PORT_SET:
847850
return
848-
logical_port = port_change_event.port_dict.get('name')
851+
logical_port = port_change_event.port_name
849852
physical_port = port_change_event.port_dict.get('index')
850853
speed = port_change_event.port_dict.get('speed')
851854
lanes = port_change_event.port_dict.get('lanes')
852855
if (physical_port is None) or (speed is None) or (lanes is None):
853856
return
857+
854858
try:
855859
msg = {
856860
'logical_port': logical_port,
857861
'physical_port': int(physical_port),
858862
'speed': int(speed),
859-
'lanes': len(lanes.split(','))
863+
'lanes': lanes.split(',')
860864
}
861-
except Exception as ex:
862-
helper_logger.log_notice("CMIS (pid {0}): {1}".format(os.getpid(), ex))
865+
except ValueError as ex:
866+
self.log_notice("{}".format(ex))
863867
return
864868

865869
self.task_queue.put(msg)
866870
time.sleep(0.001)
867871

868-
def task_worker(self, dispatcher):
869-
helper_logger.log_notice("Start CMIS task (pid {}): {}".format(os.getpid(), dispatcher))
872+
def task_worker(self, commander):
873+
self.log_notice("Starting...{}".format(commander))
870874

871-
if dispatcher:
875+
if commander:
872876
sel, asic_context = port_mapping.subscribe_port_config_change()
873-
while not self.task_stopping_event.is_set():
874-
if dispatcher:
877+
while not self.task_stopping_event.is_set():
875878
# Handle port change event from main thread
876-
port_mapping.handle_port_config_change(sel, asic_context, self.task_stopping_event, self.port_mapping, helper_logger, self.on_port_config_change, True)
877-
else:
878-
879-
try:
880-
msg = self.task_queue.get(block=True)
881-
except:
882-
msg = None
879+
port_mapping.handle_port_config_change(sel, asic_context, self.task_stopping_event, self.port_mapping, helper_logger, self.on_port_config_change)
880+
else:
881+
while not self.task_stopping_event.is_set():
882+
msg = self.task_queue.get()
883883
if msg is None:
884-
time.sleep(0.5)
884+
time.sleep(1)
885885
continue
886886

887-
logical_port = msg['logical_port']
888-
physical_port = msg['physical_port']
887+
lport = msg['logical_port']
888+
pport = msg['physical_port']
889889
lanes = msg['lanes']
890890
speed = msg['speed']
891-
try:
892-
sfp = platform_chassis.get_sfp(physical_port)
893-
except Exception as ex:
894-
helper_logger.log_notice("CMIS (pid {0}): {1}".format(os.getpid(), ex))
895-
continue
896-
891+
sfp = platform_chassis.get_sfp(pport)
897892
if not sfp.get_presence():
898893
continue
899894

900-
helper_logger.log_notice("CMIS (pid {0}): {1}: i={2},speed={3},lanes={4}".format(os.getpid(), logical_port, physical_port, speed, lanes))
901-
902895
try:
903-
ret = sfp.set_cmis_application(speed, lanes)
904-
helper_logger.log_notice("CMIS (pid {0}): {1}: {2}".format(os.getpid(), logical_port, ("succeeded" if ret else "failed")))
905-
except Exception as ex:
906-
helper_logger.log_notice("CMIS (pid {0}): {1}: {2}".format(os.getpid(), logical_port, ex))
896+
ret = sfp.set_cmis_application(speed, len(lanes))
897+
except (NotImplementedError, AttributeError):
898+
ret = False
899+
self.log_notice("{0}: {1}".format(lport, "succeeded" if ret else "failed"))
907900

908-
helper_logger.log_notice("Stop CMIS task (pid {0})".format(os.getpid()))
901+
self.log_notice("Stopped")
909902

910903
def task_run(self, y_cable_presence):
911904
if platform_chassis is None:
912-
helper_logger.log_notice("CMIS: Platform chassis is not available, stopping...")
905+
self.log_notice("Platform chassis is not available, stopping...")
913906
return
914907

915908
has_cmis = False
@@ -919,7 +912,7 @@ def task_run(self, y_cable_presence):
919912
break
920913

921914
if not has_cmis:
922-
helper_logger.log_notice("CMIS: None of QSFP-DD cage is detected, stopping...")
915+
self.log_notice("None of QSFP-DD cage is detected, stopping...")
923916
return
924917

925918
for n in range(0, self.worker_max):
@@ -1625,7 +1618,7 @@ def run(self):
16251618
port_mapping_data, retry_eeprom_set = self.init()
16261619

16271620
# Start the CMIS manager
1628-
cmis_manager = CmisManagerTask()
1621+
cmis_manager = CmisManagerTask(port_mapping_data)
16291622
cmis_manager.task_run(self.y_cable_presence)
16301623

16311624
# Start the dom sensor info update thread

sonic-xcvrd/xcvrd/xcvrd_utilities/port_mapping.py

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
class PortChangeEvent:
99
PORT_ADD = 0
1010
PORT_REMOVE = 1
11+
PORT_SET = 2
12+
PORT_DEL = 3
13+
1114
def __init__(self, port_name, port_index, asic_id, event_type, port_dict=None):
1215
# Logical port name, e.g. Ethernet0
1316
self.port_name = port_name
@@ -99,7 +102,7 @@ def subscribe_port_config_change():
99102
return sel, asic_context
100103

101104

102-
def handle_port_config_change(sel, asic_context, stop_event, port_mapping, logger, port_change_event_handler, is_cmis=False):
105+
def handle_port_config_change(sel, asic_context, stop_event, port_mapping, logger, port_change_event_handler):
103106
"""Select CONFIG_DB PORT table changes, once there is a port configuration add/remove, notify observers
104107
"""
105108
if not stop_event.is_set():
@@ -110,50 +113,65 @@ def handle_port_config_change(sel, asic_context, stop_event, port_mapping, logge
110113
logger.log_warning('sel.select() did not return swsscommon.Select.OBJECT')
111114
return
112115

113-
read_port_config_change(asic_context, port_mapping, logger, port_change_event_handler, is_cmis)
116+
read_port_config_change(asic_context, port_mapping, logger, port_change_event_handler)
114117

115-
def read_port_config_change(asic_context, port_mapping, logger, port_change_event_handler, is_cmis=False):
118+
def read_port_config_change(asic_context, port_mapping, logger, port_change_event_handler):
116119
for port_tbl in asic_context.keys():
117120
while True:
118121
(key, op, fvp) = port_tbl.pop()
119122
if not key:
120123
break
124+
fvp = dict(fvp) if fvp is not None else {}
121125
if op == swsscommon.SET_COMMAND:
122-
fvp = dict(fvp)
123126
if 'index' not in fvp:
124127
continue
125128

126129
new_physical_index = int(fvp['index'])
127-
if is_cmis:
128-
fvp['name'] = key
129-
port_change_event = PortChangeEvent(key, new_physical_index, asic_context[port_tbl], PortChangeEvent.PORT_ADD, fvp)
130-
port_change_event_handler(port_change_event)
131-
elif not port_mapping.is_logical_port(key):
130+
131+
port_change_event = PortChangeEvent(key, new_physical_index, asic_context[port_tbl], PortChangeEvent.PORT_SET, fvp)
132+
port_change_event_handler(port_change_event)
133+
134+
if not port_mapping.is_logical_port(key):
132135
# New logical port created
133-
port_change_event = PortChangeEvent(key, new_physical_index, asic_context[port_tbl], PortChangeEvent.PORT_ADD)
136+
port_change_event = PortChangeEvent(key, new_physical_index, asic_context[port_tbl], PortChangeEvent.PORT_ADD, fvp)
134137
port_change_event_handler(port_change_event)
135138
else:
136139
current_physical_index = port_mapping.get_logical_to_physical(key)[0]
137140
if current_physical_index != new_physical_index:
138-
port_change_event = PortChangeEvent(key,
139-
current_physical_index,
140-
asic_context[port_tbl],
141-
PortChangeEvent.PORT_REMOVE)
141+
port_change_event = PortChangeEvent(key,
142+
current_physical_index,
143+
asic_context[port_tbl],
144+
PortChangeEvent.PORT_REMOVE,
145+
fvp)
142146
port_change_event_handler(port_change_event)
143147

144-
port_change_event = PortChangeEvent(key, new_physical_index, asic_context[port_tbl], PortChangeEvent.PORT_ADD)
148+
port_change_event = PortChangeEvent(key, new_physical_index, asic_context[port_tbl], PortChangeEvent.PORT_ADD, fvp)
145149
port_change_event_handler(port_change_event)
146150
elif op == swsscommon.DEL_COMMAND:
151+
if 'index' in fvp:
152+
physical_index = int(fvp['index'])
153+
elif port_mapping.is_logical_port(key):
154+
physical_index = port_mapping.get_logical_to_physical(key)[0]
155+
else:
156+
physical_index = 0
157+
158+
port_change_event = PortChangeEvent(key,
159+
physical_index,
160+
asic_context[port_tbl],
161+
PortChangeEvent.PORT_DEL,
162+
fvp)
163+
port_change_event_handler(port_change_event)
164+
147165
if port_mapping.is_logical_port(key):
148-
port_change_event = PortChangeEvent(key,
149-
port_mapping.get_logical_to_physical(key)[0],
150-
asic_context[port_tbl],
151-
PortChangeEvent.PORT_REMOVE)
166+
port_change_event = PortChangeEvent(key,
167+
physical_index,
168+
asic_context[port_tbl],
169+
PortChangeEvent.PORT_REMOVE,
170+
fvp)
152171
port_change_event_handler(port_change_event)
153172
else:
154173
logger.log_warning('Invalid DB operation: {}'.format(op))
155174

156-
157175
def get_port_mapping():
158176
"""Get port mapping from CONFIG_DB
159177
"""

0 commit comments

Comments
 (0)