"""Utilities and helper functions."""
import datetime
+import decimal
import errno
import functools
import hashlib
def camelize(s):
return ''.join(s.replace('_', ' ').title().split())
+
+
+def round_val(val):
+ # we rely on decimal module since it behaves consistently across Python
+ # versions (2.x vs. 3.x)
+ return int(decimal.Decimal(val).quantize(decimal.Decimal('1'),
+ rounding=decimal.ROUND_HALF_UP))
from oslo_log import log as logging
import six
+from neutron.common import utils
from neutron.i18n import _LE, _LW
from neutron.plugins.ml2.drivers.mech_sriov.agent.common \
import exceptions as exc
@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)
+ #(Note): ip link set max rate in Mbps therefore
+ #we need to convert the max_kbps to Mbps.
+ #Zero means to disable the rate so the lowest rate
+ #available is 1Mbps. Floating numbers are not allowed
+ if max_kbps > 0 and max_kbps < 1000:
+ max_mbps = 1
+ else:
+ max_mbps = utils.round_val(max_kbps / 1000.0)
+
+ log_dict = {
+ 'max_rate': max_mbps,
+ 'max_kbps': max_kbps,
+ 'vf_index': vf_index
+ }
+ if max_kbps % 1000 != 0:
+ LOG.debug("Maximum rate for SR-IOV ports is counted in Mbps; "
+ "setting %(max_rate)s Mbps limit for port %(vf_index)s "
+ "instead of %(max_kbps)s kbps",
+ log_dict)
+ else:
+ LOG.debug("Setting %(max_rate)s Mbps limit for port %(vf_index)s",
+ log_dict)
+
+ return self.pci_dev_wrapper.set_vf_max_rate(vf_index, max_mbps)
def _get_vf_index(self, pci_slot):
vf_index = self.pci_slot_map.get(pci_slot)
"""sets vf max rate.
@param vf_index: vf index
- @param max_tx_rate: vf max tx rate
+ @param max_tx_rate: vf max tx rate in Mbps
"""
try:
self._as_root([], "link", ("set", self.dev_name, "vf",
for s, expected in data.items():
self.assertEqual(expected, utils.camelize(s))
+
+
+class TestRoundVal(base.BaseTestCase):
+ def test_round_val_ok(self):
+ for expected, value in ((0, 0),
+ (0, 0.1),
+ (1, 0.5),
+ (1, 1.49),
+ (2, 1.5)):
+ self.assertEqual(expected, utils.round_val(value))
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)
+ "PciDeviceIPWrapper.set_vf_max_rate") as pci_lib_mock:
+ self.emb_switch.set_device_max_rate(self.PCI_SLOT, 2000)
+ pci_lib_mock.assert_called_with(0, 2)
+
+ def test_set_device_max_rate_ok2(self):
+ with mock.patch("neutron.plugins.ml2.drivers.mech_sriov.agent.pci_lib."
+ "PciDeviceIPWrapper.set_vf_max_rate") as pci_lib_mock:
+ self.emb_switch.set_device_max_rate(self.PCI_SLOT, 99)
+ pci_lib_mock.assert_called_with(0, 1)
+
+ def test_set_device_max_rate_rounded_ok(self):
+ with mock.patch("neutron.plugins.ml2.drivers.mech_sriov.agent.pci_lib."
+ "PciDeviceIPWrapper.set_vf_max_rate") as pci_lib_mock:
+ self.emb_switch.set_device_max_rate(self.PCI_SLOT, 2001)
+ pci_lib_mock.assert_called_with(0, 2)
+
+ def test_set_device_max_rate_rounded_ok2(self):
+ with mock.patch("neutron.plugins.ml2.drivers.mech_sriov.agent.pci_lib."
+ "PciDeviceIPWrapper.set_vf_max_rate") as pci_lib_mock:
+ self.emb_switch.set_device_max_rate(self.PCI_SLOT, 2499)
+ pci_lib_mock.assert_called_with(0, 2)
+
+ def test_set_device_max_rate_rounded_ok3(self):
+ with mock.patch("neutron.plugins.ml2.drivers.mech_sriov.agent.pci_lib."
+ "PciDeviceIPWrapper.set_vf_max_rate") as pci_lib_mock:
+ self.emb_switch.set_device_max_rate(self.PCI_SLOT, 2500)
+ pci_lib_mock.assert_called_with(0, 3)
+
+ def test_set_device_max_rate_disable(self):
+ with mock.patch("neutron.plugins.ml2.drivers.mech_sriov.agent.pci_lib."
+ "PciDeviceIPWrapper.set_vf_max_rate") as pci_lib_mock:
+ self.emb_switch.set_device_max_rate(self.PCI_SLOT, 0)
+ pci_lib_mock.assert_called_with(0, 0)
def test_set_device_max_rate_fail(self):
with mock.patch("neutron.plugins.ml2.drivers.mech_sriov.agent.pci_lib."