--- /dev/null
+# Copyright (c) 2014 OpenStack Foundation
+# All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+"""Exceptions used by SRIOV Mechanism Driver."""
+
+from neutron.common import exceptions
+
+
+class SriovUnsupportedNetworkType(exceptions.NeutronException):
+ """Method was invoked for unsupported network type."""
+ message = _("Unsupported network type %(net_type)s.")
from neutron.openstack.common import log
from neutron.plugins.common import constants as p_const
from neutron.plugins.ml2 import driver_api as api
+from neutron.plugins.ml2.drivers.mech_sriov import exceptions as exc
LOG = log.getLogger(__name__)
+FLAT_VLAN = 0
sriov_opts = [
cfg.ListOpt('supported_pci_vendor_devs',
self.supported_vnic_types = supported_vnic_types
self.vif_type = vif_type
self.vif_details = vif_details
+ self.supported_network_types = (p_const.TYPE_VLAN, p_const.TYPE_FLAT)
def initialize(self):
try:
if self.check_segment(segment, agent):
context.set_binding(segment[api.ID],
self.vif_type,
- self.get_vif_details(context, segment),
+ self._get_vif_details(segment),
constants.PORT_STATUS_ACTIVE)
LOG.debug("Bound using segment: %s", segment)
return True
:returns: True if segment can be bound for agent
"""
network_type = segment[api.NETWORK_TYPE]
- if network_type == p_const.TYPE_VLAN:
+ if network_type in self.supported_network_types:
if agent:
mappings = agent['configurations'].get('device_mappings', {})
LOG.debug("Checking segment: %(segment)s "
return True
return False
- def get_vif_details(self, context, segment):
- if segment[api.NETWORK_TYPE] == p_const.TYPE_VLAN:
- vlan_id = str(segment[api.SEGMENTATION_ID])
- self.vif_details[portbindings.VIF_DETAILS_VLAN] = vlan_id
- return self.vif_details
+ def _get_vif_details(self, segment):
+ network_type = segment[api.NETWORK_TYPE]
+ if network_type == p_const.TYPE_FLAT:
+ vlan_id = FLAT_VLAN
+ elif network_type == p_const.TYPE_VLAN:
+ vlan_id = segment[api.SEGMENTATION_ID]
+ else:
+ raise exc.SriovUnsupportedNetworkType(net_type=network_type)
+ vif_details = self.vif_details.copy()
+ vif_details[portbindings.VIF_DETAILS_VLAN] = str(vlan_id)
+ return vif_details
def _parse_pci_vendor_config(self, pci_vendor_list):
parsed_list = []
import mock
from oslo.config import cfg
+import testtools
from neutron.common import constants
from neutron.extensions import portbindings
from neutron.plugins.common import constants as p_const
from neutron.plugins.ml2 import config # noqa
from neutron.plugins.ml2 import driver_api as api
+from neutron.plugins.ml2.drivers.mech_sriov import exceptions as exc
from neutron.plugins.ml2.drivers.mech_sriov import mech_driver
from neutron.tests.unit.ml2 import _test_mech_agent as base
segment[api.NETWORK_TYPE] = p_const.TYPE_GRE
self.assertFalse(self.driver.check_segment(segment))
+ def test_check_segment_allows_supported_network_types(self):
+ for network_type in self.driver.supported_network_types:
+ segment = {api.NETWORK_TYPE: network_type}
+ self.assertTrue(self.driver.check_segment(segment))
+
class SriovMechVlanTestCase(SriovNicSwitchMechanismBaseTestCase,
base.AgentMechanismBaseTestCase):
vlan_id = int(vif_details.get(portbindings.VIF_DETAILS_VLAN))
self.assertEqual(1234, vlan_id)
+ def test_get_vif_details_for_flat_network(self):
+ segment = {api.NETWORK_TYPE: p_const.TYPE_FLAT}
+ vif_details = self.driver._get_vif_details(segment)
+ vlan_id = vif_details[portbindings.VIF_DETAILS_VLAN]
+ self.assertEqual('0', vlan_id)
+
+ def test_get_vif_details_unsupported_net(self):
+ segment = {api.NETWORK_TYPE: 'foo'}
+ with testtools.ExpectedException(exc.SriovUnsupportedNetworkType):
+ self.driver._get_vif_details(segment)
+
class SriovSwitchMechConfigTestCase(SriovNicSwitchMechanismBaseTestCase):
def _set_config(self, pci_devs=['aa:bb']):
def test_pci_vendor_config_wrong_entry(self):
self._set_config('wrong_entry')
- self.assertRaises(cfg.Error, self.driver.initialize)
+ self.assertRaises(cfg.Error, self.driver.initialize)
\ No newline at end of file