]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Fix _device_to_port_id for non-tap devices
authorKevin Benton <blak111@gmail.com>
Mon, 30 Mar 2015 18:29:44 +0000 (11:29 -0700)
committerKevin Benton <kevinbenton@buttewifi.com>
Wed, 29 Apr 2015 14:53:43 +0000 (14:53 +0000)
This adjusts the _device_to_port_id function in ML2
to recognize other interfaces that belong to Neutron
under different name prefixes.

Adds unit tests to achieve full converage of _device_to_port_id
method.

Closes-Bug: #1443710
Change-Id: I80284ee67e5876cf5689e49e1592ca1351ae5fa1

neutron/agent/l3/dvr_snat_ns.py
neutron/common/constants.py
neutron/plugins/ml2/plugin.py
neutron/tests/unit/plugins/ml2/test_plugin.py

index 63478cbb8939e6c9dc8a0e48abeab282f6d07064..2e360cca3aeec61669cfcb0bf632032dafb98c67 100644 (file)
@@ -14,10 +14,11 @@ from oslo_log import log as logging
 
 from neutron.agent.l3 import namespaces
 from neutron.agent.linux import ip_lib
+from neutron.common import constants
 
 LOG = logging.getLogger(__name__)
 SNAT_NS_PREFIX = 'snat-'
-SNAT_INT_DEV_PREFIX = 'sg-'
+SNAT_INT_DEV_PREFIX = constants.SNAT_INT_DEV_PREFIX
 
 
 class SnatNamespace(namespaces.Namespace):
index 1bb4891183df99fd3fe789c725ce8371043ef87b..1539ed61aa89d71f78b83ae1545b4b133b13e721 100644 (file)
@@ -141,6 +141,16 @@ DEVICE_NAME_MAX_LEN = 15
 
 # Device names start with "tap"
 TAP_DEVICE_PREFIX = 'tap'
+# The vswitch side of a veth pair for a nova iptables filter setup
+VETH_DEVICE_PREFIX = 'qvo'
+# prefix for SNAT interface in DVR
+SNAT_INT_DEV_PREFIX = 'sg-'
+
+# Possible prefixes to partial port IDs in interface names used by the OVS,
+# Linux Bridge, and IVS VIF drivers in Nova and the neutron agents. See the
+# 'get_ovs_interfaceid' method in Nova (nova/virt/libvirt/vif.py) for details.
+INTERFACE_PREFIXES = (TAP_DEVICE_PREFIX, VETH_DEVICE_PREFIX,
+                      SNAT_INT_DEV_PREFIX)
 
 ATTRIBUTES_TO_UPDATE = 'attributes_to_update'
 
index c668454ac8e15f9d97375c39034c44c3a3aadba4..2f209db7723a58d27d69a9d8ce9882c69e05a5aa 100644 (file)
@@ -1464,17 +1464,18 @@ class Ml2Plugin(db_base_plugin_v2.NeutronDbPluginV2,
 
         return ports
 
-    def _device_to_port_id(self, device):
+    @staticmethod
+    def _device_to_port_id(device):
         # REVISIT(rkukura): Consider calling into MechanismDrivers to
         # process device names, or having MechanismDrivers supply list
         # of device prefixes to strip.
-        if device.startswith(const.TAP_DEVICE_PREFIX):
-            return device[len(const.TAP_DEVICE_PREFIX):]
-        else:
-            # REVISIT(irenab): Consider calling into bound MD to
-            # handle the get_device_details RPC, then remove the 'else' clause
-            if not uuidutils.is_uuid_like(device):
-                port = db.get_port_from_device_mac(device)
-                if port:
-                    return port.id
+        for prefix in const.INTERFACE_PREFIXES:
+            if device.startswith(prefix):
+                return device[len(prefix):]
+        # REVISIT(irenab): Consider calling into bound MD to
+        # handle the get_device_details RPC
+        if not uuidutils.is_uuid_like(device):
+            port = db.get_port_from_device_mac(device)
+            if port:
+                return port.id
         return device
index cc51029fdfa2ab558094e60f9d4f6797870515c8..21b90976a321b0639067c050d85924754c30fd30 100644 (file)
@@ -38,6 +38,7 @@ from neutron.extensions import multiprovidernet as mpnet
 from neutron.extensions import portbindings
 from neutron.extensions import providernet as pnet
 from neutron import manager
+from neutron.openstack.common import uuidutils
 from neutron.plugins.common import constants as p_const
 from neutron.plugins.ml2.common import exceptions as ml2_exc
 from neutron.plugins.ml2 import config
@@ -607,6 +608,30 @@ class TestMl2PluginOnly(Ml2PluginV2TestCase):
         with testtools.ExpectedException(exc.PortBound):
             self._test_check_mac_update_allowed(portbindings.VIF_TYPE_OVS)
 
+    def test__device_to_port_id_prefix_names(self):
+        input_output = [('sg-abcdefg', 'abcdefg'),
+                        ('tap123456', '123456'),
+                        ('qvo567890', '567890')]
+        for device, expected in input_output:
+            self.assertEqual(expected,
+                             ml2_plugin.Ml2Plugin._device_to_port_id(device))
+
+    def test__device_to_port_id_mac_address(self):
+        with self.port() as p:
+            mac = p['port']['mac_address']
+            port_id = p['port']['id']
+            self.assertEqual(port_id,
+                             ml2_plugin.Ml2Plugin._device_to_port_id(mac))
+
+    def test__device_to_port_id_not_uuid_not_mac(self):
+        dev = '1234567'
+        self.assertEqual(dev, ml2_plugin.Ml2Plugin._device_to_port_id(dev))
+
+    def test__device_to_port_id_UUID(self):
+        port_id = uuidutils.generate_uuid()
+        self.assertEqual(port_id,
+                         ml2_plugin.Ml2Plugin._device_to_port_id(port_id))
+
 
 class TestMl2DvrPortsV2(TestMl2PortsV2):
     def setUp(self):