Skip to content

Commit 81600fa

Browse files
authored
[Mellanox] Support new platform API get_port_or_cage_type for RJ45 ports (#11336)
- Why I did it Support get_port_or_cage_type for RJ45 ports - How I did it Implement the new platform API get_port_or_cage_type Fix the issue: unable to import SFP when chassis object is destructed - How to verify it Manually test and regression test Signed-off-by: Stephen Sun <[email protected]>
1 parent cf7a8f8 commit 81600fa

File tree

2 files changed

+58
-14
lines changed

2 files changed

+58
-14
lines changed

platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py

+44-14
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
from .utils import extract_RJ45_ports_index
3131
from . import utils
3232
from .device_data import DeviceDataManager
33-
from .sfp import SFP, RJ45Port, deinitialize_sdk_handle
3433
except ImportError as e:
3534
raise ImportError (str(e) + "- required module not found")
3635

@@ -110,6 +109,8 @@ def __init__(self):
110109
self.sfp_event = None
111110
self.reboot_cause_initialized = False
112111

112+
self.sfp_module = None
113+
113114
# Build the RJ45 port list from platform.json and hwsku.json
114115
self._RJ45_port_inited = False
115116
self._RJ45_port_list = None
@@ -121,9 +122,8 @@ def __del__(self):
121122
self.sfp_event.deinitialize()
122123

123124
if self._sfp_list:
124-
from .sfp import SFP, deinitialize_sdk_handle
125-
if SFP.shared_sdk_handle:
126-
deinitialize_sdk_handle(SFP.shared_sdk_handle)
125+
if self.sfp_module.SFP.shared_sdk_handle:
126+
self.sfp_module.deinitialize_sdk_handle(sfp_module.SFP.shared_sdk_handle)
127127

128128
@property
129129
def RJ45_port_list(self):
@@ -249,39 +249,45 @@ def get_fan_drawer(self, index):
249249
# SFP methods
250250
##############################################
251251

252+
def _import_sfp_module(self):
253+
if not self.sfp_module:
254+
from . import sfp as sfp_module
255+
self.sfp_module = sfp_module
256+
return self.sfp_module
257+
252258
def initialize_single_sfp(self, index):
253259
sfp_count = self.get_num_sfps()
254260
if index < sfp_count:
255261
if not self._sfp_list:
256262
self._sfp_list = [None] * sfp_count
257263

258264
if not self._sfp_list[index]:
259-
from .sfp import SFP
265+
sfp_module = self._import_sfp_module()
260266
if self.RJ45_port_list and index in self.RJ45_port_list:
261-
self._sfp_list[index] = RJ45Port(index)
267+
self._sfp_list[index] = sfp_module.RJ45Port(index)
262268
else:
263-
self._sfp_list[index] = SFP(index)
269+
self._sfp_list[index] = sfp_module.SFP(index)
264270
self.sfp_initialized_count += 1
265271

266272
def initialize_sfp(self):
267273
if not self._sfp_list:
268-
from .sfp import SFP
274+
sfp_module = self._import_sfp_module()
269275
sfp_count = self.get_num_sfps()
270276
for index in range(sfp_count):
271277
if self.RJ45_port_list and index in self.RJ45_port_list:
272-
sfp_module = RJ45Port(index)
278+
sfp_object = sfp_module.RJ45Port(index)
273279
else:
274-
sfp_module = SFP(index)
275-
self._sfp_list.append(sfp_module)
280+
sfp_object = sfp_module.SFP(index)
281+
self._sfp_list.append(sfp_object)
276282
self.sfp_initialized_count = sfp_count
277283
elif self.sfp_initialized_count != len(self._sfp_list):
278-
from .sfp import SFP
284+
sfp_module = self._import_sfp_module()
279285
for index in range(len(self._sfp_list)):
280286
if self._sfp_list[index] is None:
281287
if self.RJ45_port_list and index in self.RJ45_port_list:
282-
self._sfp_list[index] = RJ45Port(index)
288+
self._sfp_list[index] = sfp_module.RJ45Port(index)
283289
else:
284-
self._sfp_list[index] = SFP(index)
290+
self._sfp_list[index] = sfp_module.SFP(index)
285291
self.sfp_initialized_count = len(self._sfp_list)
286292

287293
def get_num_sfps(self):
@@ -321,6 +327,30 @@ def get_sfp(self, index):
321327
self.initialize_single_sfp(index)
322328
return super(Chassis, self).get_sfp(index)
323329

330+
def get_port_or_cage_type(self, index):
331+
"""
332+
Retrieves sfp port or cage type corresponding to physical port <index>
333+
334+
Args:
335+
index: An integer (>=0), the index of the sfp to retrieve.
336+
The index should correspond to the physical port in a chassis.
337+
For example:-
338+
1 for Ethernet0, 2 for Ethernet4 and so on for one platform.
339+
0 for Ethernet0, 1 for Ethernet4 and so on for another platform.
340+
341+
Returns:
342+
The masks of all types of port or cage that can be supported on the port
343+
Types are defined in sfp_base.py
344+
Eg.
345+
Both SFP and SFP+ are supported on the port, the return value should be 0x0a
346+
which is 0x02 | 0x08
347+
"""
348+
index = index - 1
349+
if self.RJ45_port_list and index in self.RJ45_port_list:
350+
from sonic_platform_base.sfp_base import SfpBase
351+
return SfpBase.SFP_PORT_TYPE_BIT_RJ45
352+
raise NotImplementedError
353+
324354
def get_change_event(self, timeout=0):
325355
"""
326356
Returns a nested dictionary containing all devices which have

platform/mellanox/mlnx-platform-api/tests/test_chassis.py

+14
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
sys.path.insert(0, modules_path)
3030

3131
import sonic_platform.chassis
32+
from sonic_platform_base.sfp_base import SfpBase
3233
from sonic_platform.chassis import Chassis
3334
from sonic_platform.device_data import DeviceDataManager
3435

@@ -282,3 +283,16 @@ def test_revision_permission(self):
282283
sonic_platform.chassis.DMI_FILE = old_dmi_file
283284
os.system("rm -f " + new_dmi_file)
284285
assert rev == "N/A"
286+
287+
def test_get_port_or_cage_type(self):
288+
chassis = Chassis()
289+
chassis.RJ45_port_list = [0]
290+
assert SfpBase.SFP_PORT_TYPE_BIT_RJ45 == chassis.get_port_or_cage_type(1)
291+
292+
exceptionRaised = False
293+
try:
294+
chassis.get_port_or_cage_type(2)
295+
except NotImplementedError:
296+
exceptionRaised = True
297+
298+
assert exceptionRaised

0 commit comments

Comments
 (0)