From 5541c98cebfc66f551d8a470bfe6eaba5be6255c Mon Sep 17 00:00:00 2001 From: Karthik S Date: Thu, 25 Jul 2019 11:22:52 +0000 Subject: [PATCH] Numvfs setting during update/upgrade Earlier numvfs is set by puppet-tripleo. Now during update/upgrade, the numvfs setting and persistence across reboot shall be managed by os-net-config with the corresponding setting of numvfs via nic-configs. However the numvfs value can't be changed during the update/upgrade. Change-Id: I717d822eb64f95c129bcfd66b553784643333f0e (cherry picked from commit d9ea668972d192db3e78a493890522d0d10902a1) --- os_net_config/sriov_config.py | 20 +++++++++- os_net_config/tests/test_impl_ifcfg.py | 4 ++ os_net_config/tests/test_utils.py | 52 ++++++++++++++++++++------ os_net_config/utils.py | 4 ++ 4 files changed, 67 insertions(+), 13 deletions(-) diff --git a/os_net_config/sriov_config.py b/os_net_config/sriov_config.py index 0c99fc86..90bd6e05 100644 --- a/os_net_config/sriov_config.py +++ b/os_net_config/sriov_config.py @@ -102,6 +102,17 @@ def _get_sriov_map(): return sriov_map +def get_numvfs(ifname): + try: + sriov_numvfs_path = os.path.join(_SYS_CLASS_NET, ifname, + "device/sriov_numvfs") + with open(sriov_numvfs_path, 'r') as f: + return int(f.read()) + except IOError: + msg = ("Unable to read numvfs for %s" % ifname) + raise SRIOVNumvfsException(msg) + + def configure_sriov_pf(): # Create a context for pyudev and observe udev events for network context = pyudev.Context() @@ -118,8 +129,13 @@ def configure_sriov_pf(): if item['device_type'] == 'pf': _pf_interface_up(item) try: - sriov_numvfs_path = ("/sys/class/net/%s/device/sriov_numvfs" - % item['name']) + sriov_numvfs_path = os.path.join(_SYS_CLASS_NET, item['name'], + "device/sriov_numvfs") + curr_numvfs = get_numvfs(item['name']) + if curr_numvfs == item['numvfs']: + logger.info("Numvfs already configured for %s" + % item['name']) + continue with open(sriov_numvfs_path, 'w') as f: f.write("%d" % item['numvfs']) except IOError as exc: diff --git a/os_net_config/tests/test_impl_ifcfg.py b/os_net_config/tests/test_impl_ifcfg.py index 8415ee4e..347b672d 100644 --- a/os_net_config/tests/test_impl_ifcfg.py +++ b/os_net_config/tests/test_impl_ifcfg.py @@ -1934,6 +1934,10 @@ class TestIfcfgNetConfigApply(base.TestCase): self.assertEqual(_ROUTES, route_data) def test_sriov_pf_network_apply(self): + def get_numvfs_stub(pf_name): + return 0 + self.stub_out('os_net_config.sriov_config.get_numvfs', + get_numvfs_stub) route1 = objects.Route('192.168.1.1', default=True, route_options="metric 10") route2 = objects.Route('192.168.1.1', '172.19.0.0/24') diff --git a/os_net_config/tests/test_utils.py b/os_net_config/tests/test_utils.py index 2043e824..2e01df2c 100644 --- a/os_net_config/tests/test_utils.py +++ b/os_net_config/tests/test_utils.py @@ -119,6 +119,10 @@ class TestUtils(base.TestCase): shutil.rmtree(tmpdir) def test_update_sriov_pf_map_new(self): + def get_numvfs_stub(pf_name): + return 0 + self.stub_out('os_net_config.sriov_config.get_numvfs', + get_numvfs_stub) utils.update_sriov_pf_map('eth1', 10, False) contents = utils.get_file_data(sriov_config._SRIOV_CONFIG_FILE) sriov_pf_map = yaml.safe_load(contents) if contents else [] @@ -127,7 +131,32 @@ class TestUtils(base.TestCase): 'name': 'eth1', 'numvfs': 10}] self.assertListEqual(test_sriov_pf_map, sriov_pf_map) + def test_update_sriov_pf_map_with_same_numvfs(self): + def get_numvfs_stub(pf_name): + return 10 + self.stub_out('os_net_config.sriov_config.get_numvfs', + get_numvfs_stub) + utils.update_sriov_pf_map('eth1', 10, False) + contents = utils.get_file_data(sriov_config._SRIOV_CONFIG_FILE) + sriov_pf_map = yaml.safe_load(contents) if contents else [] + self.assertEqual(1, len(sriov_pf_map)) + test_sriov_pf_map = [{'device_type': 'pf', 'link_mode': 'legacy', + 'name': 'eth1', 'numvfs': 10}] + self.assertListEqual(test_sriov_pf_map, sriov_pf_map) + + def test_update_sriov_pf_map_with_diff_numvfs(self): + def get_numvfs_stub(pf_name): + return 12 + self.stub_out('os_net_config.sriov_config.get_numvfs', + get_numvfs_stub) + self.assertRaises(sriov_config.SRIOVNumvfsException, + utils.update_sriov_pf_map, 'eth1', 10, False) + def test_update_sriov_pf_map_new_with_promisc(self): + def get_numvfs_stub(pf_name): + return 0 + self.stub_out('os_net_config.sriov_config.get_numvfs', + get_numvfs_stub) utils.update_sriov_pf_map('eth1', 10, False, promisc='off') contents = utils.get_file_data(sriov_config._SRIOV_CONFIG_FILE) sriov_pf_map = yaml.safe_load(contents) if contents else [] @@ -137,27 +166,28 @@ class TestUtils(base.TestCase): self.assertListEqual(test_sriov_pf_map, sriov_pf_map) def test_update_sriov_pf_map_exist(self): + def get_numvfs_stub(pf_name): + return 10 + self.stub_out('os_net_config.sriov_config.get_numvfs', + get_numvfs_stub) pf_initial = [{'device_type': 'pf', 'link_mode': 'legacy', 'name': 'eth1', 'numvfs': 10}] utils.write_yaml_config(sriov_config._SRIOV_CONFIG_FILE, pf_initial) - - utils.update_sriov_pf_map('eth1', 20, False) - pf_final = [{'device_type': 'pf', 'link_mode': 'legacy', - 'name': 'eth1', 'numvfs': 20}] - contents = utils.get_file_data(sriov_config._SRIOV_CONFIG_FILE) - - pf_map = yaml.safe_load(contents) if contents else [] - self.assertEqual(1, len(pf_map)) - self.assertListEqual(pf_final, pf_map) + self.assertRaises(sriov_config.SRIOVNumvfsException, + utils.update_sriov_pf_map, 'eth1', 20, False) def test_update_sriov_pf_map_exist_with_promisc(self): + def get_numvfs_stub(pf_name): + return 10 + self.stub_out('os_net_config.sriov_config.get_numvfs', + get_numvfs_stub) pf_initial = [{'device_type': 'pf', 'link_mode': 'legacy', 'name': 'eth1', 'numvfs': 10, 'promisc': 'on'}] utils.write_yaml_config(sriov_config._SRIOV_CONFIG_FILE, pf_initial) - utils.update_sriov_pf_map('eth1', 20, False) + utils.update_sriov_pf_map('eth1', 10, False, promisc='off') pf_final = [{'device_type': 'pf', 'link_mode': 'legacy', - 'name': 'eth1', 'numvfs': 20, 'promisc': 'on'}] + 'name': 'eth1', 'numvfs': 10, 'promisc': 'off'}] contents = utils.get_file_data(sriov_config._SRIOV_CONFIG_FILE) pf_map = yaml.safe_load(contents) if contents else [] diff --git a/os_net_config/utils.py b/os_net_config/utils.py index 5af682c5..620ce269 100644 --- a/os_net_config/utils.py +++ b/os_net_config/utils.py @@ -417,6 +417,10 @@ def _get_dpdk_mac_address(name): def update_sriov_pf_map(ifname, numvfs, noop, promisc=None, link_mode='legacy'): if not noop: + cur_numvfs = sriov_config.get_numvfs(ifname) + if cur_numvfs > 0 and cur_numvfs != numvfs: + msg = ("Can't change the numvfs for %s" % ifname) + raise sriov_config.SRIOVNumvfsException(msg) sriov_map = _get_sriov_map() for item in sriov_map: if item['device_type'] == 'pf' and item['name'] == ifname: