Skip to content

Commit

Permalink
[sfputil] Add generic function to get lane mask by given subport and …
Browse files Browse the repository at this point in the history
…lane count

Signed-off-by: xinyu <xinyu0123@gmail.com>
  • Loading branch information
xinyulin committed Sep 10, 2024
1 parent 4ede797 commit c0d642d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
18 changes: 16 additions & 2 deletions sfputil/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2032,9 +2032,9 @@ def loopback(port_name, loopback_mode, enable):
sys.exit(EXIT_FAIL)

if 'host-side' in loopback_mode:
lane_mask = ((1 << host_lane_count) - 1) << ((subport - 1) * host_lane_count)
lane_mask = get_subport_lane_mask(subport, host_lane_count)
elif 'media-side' in loopback_mode:
lane_mask = ((1 << media_lane_count) - 1) << ((subport - 1) * media_lane_count)
lane_mask = get_subport_lane_mask(subport, media_lane_count)
else:
lane_mask = 0

Expand All @@ -2055,5 +2055,19 @@ def loopback(port_name, loopback_mode, enable):
click.echo("{}: Set {} loopback failed".format(port_name, loopback_mode))
sys.exit(EXIT_FAIL)


def get_subport_lane_mask(subport, lane_count):
"""Get the lane mask for the given subport and lane count
Args:
subport (int): Subport number
lane_count (int): Lane count for the subport
Returns:
int: Lane mask for the given subport and lane count
"""
return ((1 << lane_count) - 1) << ((subport - 1) * lane_count)


if __name__ == '__main__':
cli()
11 changes: 11 additions & 0 deletions tests/sfputil_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1709,3 +1709,14 @@ def test_debug_loopback(self, mock_sonic_v2_connector, mock_config_db_connector,
["Ethernet0", "media-side-input", "enable"])
assert result.output == 'Ethernet0: Failed to connect to STATE_DB\n'
assert result.exit_code == EXIT_FAIL

@pytest.mark.parametrize("subport, lane_count, expected_mask", [
(1, 1, 0b1),
(1, 4, 0b1111),
(2, 1, 0b10),
(2, 4, 0b11110000),
(3, 2, 0b1100),
(4, 1, 0b1000),
])
def test_get_subport_lane_mask(subport, lane_count, expected_mask):
assert get_subport_lane_mask(subport, lane_count) == expected_mask

0 comments on commit c0d642d

Please sign in to comment.