diff --git a/hooks/neutron_ovs_utils.py b/hooks/neutron_ovs_utils.py index 92129d5d..5f39ca5c 100644 --- a/hooks/neutron_ovs_utils.py +++ b/hooks/neutron_ovs_utils.py @@ -622,9 +622,6 @@ def configure_sriov(): log("Configuring SR-IOV device" " {} with {} VF's".format(device.interface_name, device.sriov_totalvfs)) - # NOTE(fnordahl): run-time change of numvfs is disallowed - # without resetting to 0 first. - device.set_sriov_numvfs(0) device.set_sriov_numvfs(device.sriov_totalvfs) else: # Single int blanket configuration @@ -643,9 +640,6 @@ def configure_sriov(): device.sriov_totalvfs)) log("Configuring SR-IOV device {} with {} " "VFs".format(device.interface_name, numvfs)) - # NOTE(fnordahl): run-time change of numvfs is - # disallowed without resetting to 0 first. - device.set_sriov_numvfs(0) device.set_sriov_numvfs(numvfs) except ValueError: # :[ :numvfs] configuration @@ -666,9 +660,6 @@ def configure_sriov(): numvfs = device.sriov_totalvfs log("Configuring SR-IOV device {} with {} " "VF's".format(device.interface_name, numvfs)) - # NOTE(fnordahl): run-time change of numvfs is - # disallowed without resetting to 0 first. - device.set_sriov_numvfs(0) device.set_sriov_numvfs(int(numvfs)) # Trigger remote restart in parent application diff --git a/hooks/pci.py b/hooks/pci.py index d52e10fa..a4b431d0 100644 --- a/hooks/pci.py +++ b/hooks/pci.py @@ -174,21 +174,27 @@ class PCINetDevice(object): self.sriov_totalvfs = interface['sriov_totalvfs'] self.sriov_numvfs = interface['sriov_numvfs'] + def _set_sriov_numvfs(self, numvfs): + sdevice = os.path.join('/sys/class/net', + self.interface_name, + 'device', 'sriov_numvfs') + with open(sdevice, 'w') as sh: + sh.write(str(numvfs)) + self.update_attributes() + def set_sriov_numvfs(self, numvfs): - '''Set the number of VF devices for a SR-IOV PF + """Set the number of VF devices for a SR-IOV PF Assuming the device is an SR-IOV device, this function will attempt to change the number of VF's created by the PF. @param numvfs: integer to set the current number of VF's to - ''' + """ if self.sriov: - sdevice = os.path.join('/sys/class/net', - self.interface_name, - 'device', 'sriov_numvfs') - with open(sdevice, 'w') as sh: - sh.write(str(numvfs)) - self.update_attributes() + # NOTE(fnordahl): run-time change of numvfs is disallowed + # without resetting to 0 first. + self._set_sriov_numvfs(0) + self._set_sriov_numvfs(numvfs) class PCINetDevices(object): diff --git a/unit_tests/test_pci.py b/unit_tests/test_pci.py index e8615639..95ee2084 100644 --- a/unit_tests/test_pci.py +++ b/unit_tests/test_pci.py @@ -21,7 +21,7 @@ from test_pci_helper import ( mocked_islink, mocked_realpath, ) -from mock import patch, MagicMock +from mock import patch, MagicMock, call import pci TO_PATCH = [ @@ -179,7 +179,7 @@ class PCINetDeviceTest(CharmTestCase): pci.get_sysnet_interface('/sys/class/net/eth3'), 'eth3') @patch('pci.get_sysnet_interfaces_and_macs') - def test_set_sriov_numvfs(self, mock_sysnet_ints): + def test__set_sriov_numvfs(self, mock_sysnet_ints): mock_sysnet_ints.side_effect = [{ 'interface': 'eth2', 'mac_address': 'a8:9d:21:cf:93:fc', @@ -204,7 +204,7 @@ class PCINetDeviceTest(CharmTestCase): self.assertEqual(0, dev.sriov_numvfs) with patch_open() as (mock_open, mock_file): - dev.set_sriov_numvfs(4) + dev._set_sriov_numvfs(4) mock_open.assert_called_with( '/sys/class/net/eth2/device/sriov_numvfs', 'w') mock_file.write.assert_called_with("4") @@ -212,6 +212,14 @@ class PCINetDeviceTest(CharmTestCase): self.assertEqual(7, dev.sriov_totalvfs) self.assertEqual(4, dev.sriov_numvfs) + @patch('pci.PCINetDevice._set_sriov_numvfs') + def test_set_sriov_numvfs(self, mock__set_sriov_numvfs): + dev = pci.PCINetDevice('0000:10:00.0') + dev.sriov = True + dev.set_sriov_numvfs(4) + mock__set_sriov_numvfs.assert_has_calls([ + call(0), call(4)]) + class PCINetDevicesTest(CharmTestCase):