pci: refactor set_sriov_numvfs to handle reset call

We should reset PF before to be able to allocate new VFs. This commit
is moving that part in PCIDevDevice that to refactor the code and in
order to fix related issue #1817079 in the next commit.

Change-Id: I17ba3908469ab604bf5eda3528e0b50b2e5e968f
Related-to: ##1817079
Signed-off-by: Sahid Orentino Ferdjaoui <sahid.ferdjaoui@canonical.com>
This commit is contained in:
Sahid Orentino Ferdjaoui 2019-02-28 09:11:10 +00:00 committed by sahid
parent 3957a46959
commit 450cf845be
3 changed files with 25 additions and 20 deletions

View File

@ -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:
# <device>:<numvfs>[ <device>: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

View File

@ -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):

View File

@ -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):