@param pci_slot: Virtual Function address
"""
- vf_index = self.pci_slot_map.get(pci_slot)
- if vf_index is None:
- LOG.warning(_LW("Cannot find vf index for pci slot %s"),
- pci_slot)
- raise exc.InvalidPciSlotError(pci_slot=pci_slot)
+ vf_index = self._get_vf_index(pci_slot)
return self.pci_dev_wrapper.get_vf_state(vf_index)
def set_device_state(self, pci_slot, state):
@param pci_slot: Virtual Function address
@param state: link state
"""
+ vf_index = self._get_vf_index(pci_slot)
+ return self.pci_dev_wrapper.set_vf_state(vf_index, state)
+
+ def set_device_max_rate(self, pci_slot, max_kbps):
+ """Set device max rate.
+
+ @param pci_slot: Virtual Function address
+ @param max_kbps: device max rate in kbps
+ """
+ vf_index = self._get_vf_index(pci_slot)
+ return self.pci_dev_wrapper.set_vf_max_rate(vf_index, max_kbps)
+
+ def _get_vf_index(self, pci_slot):
vf_index = self.pci_slot_map.get(pci_slot)
if vf_index is None:
LOG.warning(_LW("Cannot find vf index for pci slot %s"),
pci_slot)
raise exc.InvalidPciSlotError(pci_slot=pci_slot)
- return self.pci_dev_wrapper.set_vf_state(vf_index, state)
+ return vf_index
def set_device_spoofcheck(self, pci_slot, enabled):
"""Set device spoofchecking
class ESwitchManager(object):
"""Manages logical Embedded Switch entities for physical network."""
- def __init__(self, device_mappings, exclude_devices):
+ def __new__(cls):
+ # make it a singleton
+ if not hasattr(cls, '_instance'):
+ cls._instance = super(ESwitchManager, cls).__new__(cls)
+ return cls._instance
+
+ def __init__(self):
"""Constructor.
Create Embedded Switch logical entities for all given device mappings,
self.emb_switches_map = {}
self.pci_slot_map = {}
- self._discover_devices(device_mappings, exclude_devices)
-
def device_exists(self, device_mac, pci_slot):
"""Verify if device exists.
return embedded_switch.get_device_state(pci_slot)
return False
+ def set_device_max_rate(self, device_mac, pci_slot, max_kbps):
+ """Set device max rate
+
+ Sets the device max rate in kbps
+ @param device_mac: device mac
+ @param pci_slot: pci slot
+ @param max_kbps: device max rate in kbps
+ """
+ embedded_switch = self._get_emb_eswitch(device_mac, pci_slot)
+ if embedded_switch:
+ embedded_switch.set_device_max_rate(pci_slot,
+ max_kbps)
+
def set_device_state(self, device_mac, pci_slot, admin_state_up):
"""Set device state
embedded_switch.set_device_spoofcheck(pci_slot,
enabled)
- def _discover_devices(self, device_mappings, exclude_devices):
+ def discover_devices(self, device_mappings, exclude_devices):
"""Discover which Virtual functions to manage.
Discover devices, and create embedded switch object for network device
return_value=True):
with testtools.ExpectedException(exc.InvalidDeviceError):
- esm.ESwitchManager(device_mappings, None)
+ esm.ESwitchManager().discover_devices(
+ device_mappings, None)
def test_create_eswitch_mgr_ok(self):
device_mappings = {'physnet1': 'p6p1'}
"eswitch_manager.PciOsWrapper.is_assigned_vf",
return_value=True):
- esm.ESwitchManager(device_mappings, None)
+ esm.ESwitchManager().discover_devices(device_mappings, None)
class TestESwitchManagerApi(base.BaseTestCase):
mock.patch("neutron.plugins.ml2.drivers.mech_sriov.agent."
"eswitch_manager.PciOsWrapper.is_assigned_vf",
return_value=True):
- self.eswitch_mgr = esm.ESwitchManager(device_mappings, None)
+ self.eswitch_mgr = esm.ESwitchManager()
+ self.eswitch_mgr.discover_devices(device_mappings, None)
def test_get_assigned_devices(self):
with mock.patch("neutron.plugins.ml2.drivers.mech_sriov.agent."
self.eswitch_mgr.set_device_state(self.ASSIGNED_MAC,
self.PCI_SLOT, True)
+ def test_set_device_max_rate(self):
+ with mock.patch("neutron.plugins.ml2.drivers.mech_sriov.agent."
+ "eswitch_manager.EmbSwitch.get_pci_device",
+ return_value=self.ASSIGNED_MAC) as get_pci_mock,\
+ mock.patch("neutron.plugins.ml2.drivers.mech_sriov.agent."
+ "eswitch_manager.EmbSwitch.set_device_max_rate")\
+ as set_device_max_rate_mock:
+ self.eswitch_mgr.set_device_max_rate(self.ASSIGNED_MAC,
+ self.PCI_SLOT, 1000)
+ get_pci_mock.assert_called_once_with(self.PCI_SLOT)
+ set_device_max_rate_mock.assert_called_once_with(
+ self.PCI_SLOT, 1000)
+
def test_set_device_status_mismatch(self):
with mock.patch("neutron.plugins.ml2.drivers.mech_sriov.agent."
"eswitch_manager.EmbSwitch.get_pci_device",
self.emb_switch.set_device_spoofcheck,
self.WRONG_PCI_SLOT, True)
+ def test_set_device_max_rate_ok(self):
+ with mock.patch("neutron.plugins.ml2.drivers.mech_sriov.agent.pci_lib."
+ "PciDeviceIPWrapper.set_vf_max_rate"):
+ self.emb_switch.set_device_max_rate(self.PCI_SLOT, 1000)
+
+ def test_set_device_max_rate_fail(self):
+ with mock.patch("neutron.plugins.ml2.drivers.mech_sriov.agent.pci_lib."
+ "PciDeviceIPWrapper.set_vf_max_rate"):
+ self.assertRaises(exc.InvalidPciSlotError,
+ self.emb_switch.set_device_max_rate,
+ self.WRONG_PCI_SLOT, 1000)
+
def test_get_pci_device(self):
with mock.patch("neutron.plugins.ml2.drivers.mech_sriov.agent.pci_lib."
"PciDeviceIPWrapper.get_assigned_macs",