Skip to content

Commit 92b889b

Browse files
authored
[intfutil] Check whether the FEC mode is supported on the platform before configuring it to CONFIG_DB (sonic-net#2223)
* Support FEC capability Signed-off-by: Stephen Sun <[email protected]> * Fix review comments Signed-off-by: Stephen Sun <[email protected]> * Adjust dump table test according to STATE_DB change Signed-off-by: Stephen Sun <[email protected]> * Add missing field in the test Signed-off-by: Stephen Sun <[email protected]> * Do not change dump state test Signed-off-by: Stephen Sun <[email protected]>
1 parent dab0d06 commit 92b889b

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

config/main.py

-2
Original file line numberDiff line numberDiff line change
@@ -4192,8 +4192,6 @@ def fec(ctx, interface_name, interface_fec, verbose):
41924192
# Get the config_db connector
41934193
config_db = ctx.obj['config_db']
41944194

4195-
if interface_fec not in ["rs", "fc", "none"]:
4196-
ctx.fail("'fec not in ['rs', 'fc', 'none']!")
41974195
if clicommon.get_interface_naming_mode() == "alias":
41984196
interface_name = interface_alias_to_name(config_db, interface_name)
41994197
if interface_name is None:

scripts/portconfig

+29-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ SWITCH_CAPABILITY = "SWITCH_CAPABILITY|switch"
6565
# STATE_DB constants
6666
PORT_STATE_TABLE_NAME = "PORT_TABLE"
6767
PORT_STATE_SUPPORTED_SPEEDS = "supported_speeds"
68-
68+
PORT_STATE_SUPPORTED_FECS = "supported_fecs"
6969

7070
VALID_INTERFACE_TYPE_SET = set(['CR','CR2','CR4','CR8','SR','SR2','SR4','SR8',
7171
'LR','LR4','LR8','KR','KR4','KR8','CAUI','GMII',
@@ -131,6 +131,13 @@ class portconfig(object):
131131
def set_fec(self, port, fec):
132132
if self.verbose:
133133
print("Setting fec %s on port %s" % (fec, port))
134+
supported_fecs = self.get_supported_fecs(port)
135+
if fec not in supported_fecs:
136+
if supported_fecs:
137+
print('fec {} is not in {}'.format(fec, supported_fecs))
138+
else:
139+
print('Setting fec is not supported on port {}'.format(port))
140+
exit(1)
134141
self.db.mod_entry(PORT_TABLE_NAME, port, {PORT_FEC_CONFIG_FIELD_NAME: fec})
135142

136143
def set_mtu(self, port, mtu):
@@ -226,6 +233,26 @@ class portconfig(object):
226233
state_db.connect(state_db.STATE_DB)
227234
return state_db.get(state_db.STATE_DB, '{}|{}'.format(PORT_STATE_TABLE_NAME, port), PORT_STATE_SUPPORTED_SPEEDS)
228235

236+
def get_supported_fecs(self, port):
237+
# If there is supported_fecs exposed in STATE_DB, let's use it.
238+
# Otherwise, take the default
239+
if not self.namespace:
240+
state_db = SonicV2Connector(host="127.0.0.1")
241+
else:
242+
state_db = SonicV2Connector(host="127.0.0.1", namespace=self.namespace, use_unix_socket_path=True)
243+
state_db.connect(state_db.STATE_DB)
244+
245+
supported_fecs_str = state_db.get(state_db.STATE_DB, '{}|{}'.format(PORT_STATE_TABLE_NAME, port), PORT_STATE_SUPPORTED_FECS)
246+
if supported_fecs_str:
247+
if supported_fecs_str != 'N/A':
248+
supported_fecs_list = supported_fecs_str.split(',')
249+
else:
250+
supported_fecs_list = []
251+
else:
252+
supported_fecs_list = ["rs", "fc", "none"]
253+
254+
return supported_fecs_list
255+
229256
def set_tpid(self, port, tpid):
230257
if self.verbose:
231258
print("Setting tpid %s on port %s" % (tpid, port))
@@ -276,7 +303,7 @@ def main():
276303
parser.add_argument('-p', '--port', type=str, help='port name (e.g. Ethernet0)', required=True, default=None)
277304
parser.add_argument('-l', '--list', action='store_true', help='list port parametars', default=False)
278305
parser.add_argument('-s', '--speed', type=int, help='port speed value in Mbit', default=None)
279-
parser.add_argument('-f', '--fec', type=str, help='port fec mode value in (none, rs, fc)', default=None)
306+
parser.add_argument('-f', '--fec', type=str, help='port fec mode value (default is: none, rs, fc)', default=None)
280307
parser.add_argument('-m', '--mtu', type=int, help='port mtu value in bytes', default=None)
281308
parser.add_argument('-tp', '--tpid', type=str, help='port TPID value in hex (e.g. 0x8100)', default=None)
282309
parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0')

tests/config_an_test.py

+16
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,22 @@ def test_config_mtu(self, ctx):
8484
result = self.basic_check("mtu", ["PortChannel0001", "1514"], ctx, operator.ne)
8585
assert 'Invalid port PortChannel0001' in result.output
8686

87+
def test_config_fec(self, ctx):
88+
# Set a fec mode which is in supported_fec list but not default
89+
# on an interface with supported_fec
90+
self.basic_check("fec", ["Ethernet0", "test"], ctx)
91+
# Set a fec mode which is one of default values on an interface without supported_fecs
92+
self.basic_check("fec", ["Ethernet4", "rs"], ctx)
93+
# Negative case: Set a fec mode which is default but not in port's supported_fecs
94+
result = self.basic_check("fec", ["Ethernet0", "fc"], ctx, operator.ne)
95+
assert "fec fc is not in ['rs', 'none', 'test']" in result.output
96+
# Negative case: set a fec mode which is not default on an interface without supported_fecs
97+
result = self.basic_check("fec", ["Ethernet4", "test"], ctx, operator.ne)
98+
assert "fec test is not in ['rs', 'fc', 'none']" in result.output
99+
# Negative case: set a fec mode on a port where setting fec is not supported
100+
result = self.basic_check("fec", ["Ethernet112", "test"], ctx, operator.ne)
101+
assert "Setting fec is not supported" in result.output
102+
87103
def basic_check(self, command_name, para_list, ctx, op=operator.eq, expect_result=0):
88104
runner = CliRunner()
89105
result = runner.invoke(config.config.commands["interface"].commands[command_name], para_list, obj = ctx)

tests/mock_tables/state_db.json

+2
Original file line numberDiff line numberDiff line change
@@ -698,13 +698,15 @@
698698
"rmt_adv_speeds" : "10,100,1000",
699699
"speed" : "100000",
700700
"supported_speeds": "10000,25000,40000,100000",
701+
"supported_fecs": "rs,none,test",
701702
"link_training_status": "not_trained"
702703
},
703704
"PORT_TABLE|Ethernet32": {
704705
"link_training_status": "trained"
705706
},
706707
"PORT_TABLE|Ethernet112": {
707708
"speed": "40000",
709+
"supported_fecs": "N/A",
708710
"link_training_status": "off"
709711
},
710712
"PCIE_DEVICE|00:01.0": {

0 commit comments

Comments
 (0)