From 67466cb4ec00a7ae0f181c0c9a80f654dc734a9f Mon Sep 17 00:00:00 2001 From: Mykola Gerasymenko Date: Sun, 5 Dec 2021 05:50:35 +0200 Subject: [PATCH] [port] Fix port speed set (#1952) What I did Wrong port speed is set if included as a substring in a speed literal. For example if we have a valid supported speeds for a port with 4 lanes: 40000 and 100000. Then if we try to set 4000. sudo config interface speed Ethernet0 4000 As a result, we will get a wrong set speed 4G: dut:~$ show interfaces status Interface Lanes Speed MTU FEC Alias Vlan Oper Admin Type Asym PFC --------- ------ ------ ---- ---- -------- ----- ----- ------ ----------- ----- Ethernet0 0,1,2,3 4G 9100 rs Ethernet1/1 routed up up QSFP28 or later off How I did it Fixed finding the target (set) speed in the supported speeds list. How to verify it Set the wrong speed, which included as a substring in a speed literal. For example, for a speed 40000 it is 4000. If the wrong speed is set, an error message should appear: admin@sonic:~$ sudo config interface speed Ethernet0 4000 Invalid speed specified: 4000 Valid speeds:40000,100000 Only speeds from the list of "Valid speeds" have been successfully set. sudo config interface speed Ethernet0 40000 Signed-off-by: Mykola Gerasymenko --- scripts/portconfig | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/scripts/portconfig b/scripts/portconfig index 17b3255ef5a6..30321043da82 100755 --- a/scripts/portconfig +++ b/scripts/portconfig @@ -109,12 +109,12 @@ class portconfig(object): def set_speed(self, port, speed): if self.verbose: print("Setting speed %s on port %s" % (speed, port)) - supported_speeds_str = self.get_supported_speeds(port) - if supported_speeds_str: - if supported_speeds_str.find(str(speed)) == -1: - print('Invalid speed specified: {}'.format(speed)) - print('Valid speeds:{}'.format(supported_speeds_str)) - exit(1) + supported_speeds_str = self.get_supported_speeds(port) or '' + supported_speeds = [int(s) for s in supported_speeds_str.split(',') if s] + if supported_speeds and int(speed) not in supported_speeds: + print('Invalid speed specified: {}'.format(speed)) + print('Valid speeds:{}'.format(supported_speeds_str)) + exit(1) self.db.mod_entry(PORT_TABLE_NAME, port, {PORT_SPEED_CONFIG_FIELD_NAME: speed}) def set_fec(self, port, fec):