Skip to content

Commit 0a2b9af

Browse files
stephenxsskbarista
authored andcommitted
[Mellanox] Support new platform API get_port_or_cage_type for RJ45 ports (sonic-net#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 79fd1e1 commit 0a2b9af

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_list = extract_RJ45_ports_index()
115116

@@ -120,9 +121,8 @@ def __del__(self):
120121
self.sfp_event.deinitialize()
121122

122123
if self._sfp_list:
123-
from .sfp import SFP, deinitialize_sdk_handle
124-
if SFP.shared_sdk_handle:
125-
deinitialize_sdk_handle(SFP.shared_sdk_handle)
124+
if self.sfp_module.SFP.shared_sdk_handle:
125+
self.sfp_module.deinitialize_sdk_handle(sfp_module.SFP.shared_sdk_handle)
126126

127127
##############################################
128128
# PSU methods
@@ -241,39 +241,45 @@ def get_fan_drawer(self, index):
241241
# SFP methods
242242
##############################################
243243

244+
def _import_sfp_module(self):
245+
if not self.sfp_module:
246+
from . import sfp as sfp_module
247+
self.sfp_module = sfp_module
248+
return self.sfp_module
249+
244250
def initialize_single_sfp(self, index):
245251
sfp_count = self.get_num_sfps()
246252
if index < sfp_count:
247253
if not self._sfp_list:
248254
self._sfp_list = [None] * sfp_count
249255

250256
if not self._sfp_list[index]:
251-
from .sfp import SFP
257+
sfp_module = self._import_sfp_module()
252258
if self.RJ45_port_list and index in self.RJ45_port_list:
253-
self._sfp_list[index] = RJ45Port(index)
259+
self._sfp_list[index] = sfp_module.RJ45Port(index)
254260
else:
255-
self._sfp_list[index] = SFP(index)
261+
self._sfp_list[index] = sfp_module.SFP(index)
256262
self.sfp_initialized_count += 1
257263

258264
def initialize_sfp(self):
259265
if not self._sfp_list:
260-
from .sfp import SFP
266+
sfp_module = self._import_sfp_module()
261267
sfp_count = self.get_num_sfps()
262268
for index in range(sfp_count):
263269
if self.RJ45_port_list and index in self.RJ45_port_list:
264-
sfp_module = RJ45Port(index)
270+
sfp_object = sfp_module.RJ45Port(index)
265271
else:
266-
sfp_module = SFP(index)
267-
self._sfp_list.append(sfp_module)
272+
sfp_object = sfp_module.SFP(index)
273+
self._sfp_list.append(sfp_object)
268274
self.sfp_initialized_count = sfp_count
269275
elif self.sfp_initialized_count != len(self._sfp_list):
270-
from .sfp import SFP
276+
sfp_module = self._import_sfp_module()
271277
for index in range(len(self._sfp_list)):
272278
if self._sfp_list[index] is None:
273279
if self.RJ45_port_list and index in self.RJ45_port_list:
274-
self._sfp_list[index] = RJ45Port(index)
280+
self._sfp_list[index] = sfp_module.RJ45Port(index)
275281
else:
276-
self._sfp_list[index] = SFP(index)
282+
self._sfp_list[index] = sfp_module.SFP(index)
277283
self.sfp_initialized_count = len(self._sfp_list)
278284

279285
def get_num_sfps(self):
@@ -313,6 +319,30 @@ def get_sfp(self, index):
313319
self.initialize_single_sfp(index)
314320
return super(Chassis, self).get_sfp(index)
315321

322+
def get_port_or_cage_type(self, index):
323+
"""
324+
Retrieves sfp port or cage type corresponding to physical port <index>
325+
326+
Args:
327+
index: An integer (>=0), the index of the sfp to retrieve.
328+
The index should correspond to the physical port in a chassis.
329+
For example:-
330+
1 for Ethernet0, 2 for Ethernet4 and so on for one platform.
331+
0 for Ethernet0, 1 for Ethernet4 and so on for another platform.
332+
333+
Returns:
334+
The masks of all types of port or cage that can be supported on the port
335+
Types are defined in sfp_base.py
336+
Eg.
337+
Both SFP and SFP+ are supported on the port, the return value should be 0x0a
338+
which is 0x02 | 0x08
339+
"""
340+
index = index - 1
341+
if self.RJ45_port_list and index in self.RJ45_port_list:
342+
from sonic_platform_base.sfp_base import SfpBase
343+
return SfpBase.SFP_PORT_TYPE_BIT_RJ45
344+
raise NotImplementedError
345+
316346
def get_change_event(self, timeout=0):
317347
"""
318348
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)