diff --git a/sfputil/main.py b/sfputil/main.py index 1e6416e14c..1e46b90291 100644 --- a/sfputil/main.py +++ b/sfputil/main.py @@ -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 @@ -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() diff --git a/tests/sfputil_test.py b/tests/sfputil_test.py index 5f1374be12..11572efeba 100644 --- a/tests/sfputil_test.py +++ b/tests/sfputil_test.py @@ -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 \ No newline at end of file