Skip to content

Commit 8fed381

Browse files
stephenxsyxieca
authored andcommitted
[intfutil] Check whether the FEC mode is supported on the platform before configuring it to CONFIG_DB (#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 a1a09e4 commit 8fed381

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
@@ -4097,8 +4097,6 @@ def fec(ctx, interface_name, interface_fec, verbose):
40974097
# Get the config_db connector
40984098
config_db = ctx.obj['config_db']
40994099

4100-
if interface_fec not in ["rs", "fc", "none"]:
4101-
ctx.fail("'fec not in ['rs', 'fc', 'none']!")
41024100
if clicommon.get_interface_naming_mode() == "alias":
41034101
interface_name = interface_alias_to_name(config_db, interface_name)
41044102
if interface_name is None:

scripts/portconfig

+29-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ SWITCH_CAPABILITY = "SWITCH_CAPABILITY|switch"
6060
# STATE_DB constants
6161
PORT_STATE_TABLE_NAME = "PORT_TABLE"
6262
PORT_STATE_SUPPORTED_SPEEDS = "supported_speeds"
63-
63+
PORT_STATE_SUPPORTED_FECS = "supported_fecs"
6464

6565
VALID_INTERFACE_TYPE_SET = set(['CR','CR2','CR4','SR','SR2','SR4',
6666
'LR','LR4','KR','KR4','CAUI','GMII',
@@ -126,6 +126,13 @@ class portconfig(object):
126126
def set_fec(self, port, fec):
127127
if self.verbose:
128128
print("Setting fec %s on port %s" % (fec, port))
129+
supported_fecs = self.get_supported_fecs(port)
130+
if fec not in supported_fecs:
131+
if supported_fecs:
132+
print('fec {} is not in {}'.format(fec, supported_fecs))
133+
else:
134+
print('Setting fec is not supported on port {}'.format(port))
135+
exit(1)
129136
self.db.mod_entry(PORT_TABLE_NAME, port, {PORT_FEC_CONFIG_FIELD_NAME: fec})
130137

131138
def set_mtu(self, port, mtu):
@@ -213,6 +220,26 @@ class portconfig(object):
213220
state_db.connect(state_db.STATE_DB)
214221
return state_db.get(state_db.STATE_DB, '{}|{}'.format(PORT_STATE_TABLE_NAME, port), PORT_STATE_SUPPORTED_SPEEDS)
215222

223+
def get_supported_fecs(self, port):
224+
# If there is supported_fecs exposed in STATE_DB, let's use it.
225+
# Otherwise, take the default
226+
if not self.namespace:
227+
state_db = SonicV2Connector(host="127.0.0.1")
228+
else:
229+
state_db = SonicV2Connector(host="127.0.0.1", namespace=self.namespace, use_unix_socket_path=True)
230+
state_db.connect(state_db.STATE_DB)
231+
232+
supported_fecs_str = state_db.get(state_db.STATE_DB, '{}|{}'.format(PORT_STATE_TABLE_NAME, port), PORT_STATE_SUPPORTED_FECS)
233+
if supported_fecs_str:
234+
if supported_fecs_str != 'N/A':
235+
supported_fecs_list = supported_fecs_str.split(',')
236+
else:
237+
supported_fecs_list = []
238+
else:
239+
supported_fecs_list = ["rs", "fc", "none"]
240+
241+
return supported_fecs_list
242+
216243
def set_tpid(self, port, tpid):
217244
if self.verbose:
218245
print("Setting tpid %s on port %s" % (tpid, port))
@@ -263,7 +290,7 @@ def main():
263290
parser.add_argument('-p', '--port', type=str, help='port name (e.g. Ethernet0)', required=True, default=None)
264291
parser.add_argument('-l', '--list', action='store_true', help='list port parametars', default=False)
265292
parser.add_argument('-s', '--speed', type=int, help='port speed value in Mbit', default=None)
266-
parser.add_argument('-f', '--fec', type=str, help='port fec mode value in (none, rs, fc)', default=None)
293+
parser.add_argument('-f', '--fec', type=str, help='port fec mode value (default is: none, rs, fc)', default=None)
267294
parser.add_argument('-m', '--mtu', type=int, help='port mtu value in bytes', default=None)
268295
parser.add_argument('-tp', '--tpid', type=str, help='port TPID value in hex (e.g. 0x8100)', default=None)
269296
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)