Browse Source

libvirt: Always escape IPv6 addresses when used in migration URI

If IPv6 is passed to URI it should be wrapped within square brackets.
This patch detects IPv6 to form migration URI properly. Domain name, IPv4 or
already bracketed IPv6 address will pass as is

Extend tests to include collapsed IPv6 addresses and IPv6 addresses with
port

Change-Id: I1201db996ea6ceaebd49479b298d74585a78b006
Closes-Bug: #1786058
(cherry picked from commit 8b019d6f1e)
(cherry picked from commit 7ee5499e55)
tags/17.0.7
Sergii Golovatiuk 1 year ago
parent
commit
551a7a459c

+ 58
- 15
nova/tests/unit/virt/libvirt/test_driver.py View File

@@ -9269,6 +9269,10 @@ class LibvirtConnTestCase(test.NoDBTestCase,
9269 9269
                              etree.tostring(config, encoding='unicode'))
9270 9270
 
9271 9271
     def test_live_migration_uri(self):
9272
+        addresses = ('127.0.0.1', '127.0.0.1:4444', '[::1]:4444',
9273
+                    '[0:0:0:0:0:0:0:1]:4444', u'127.0.0.1', u'destination',
9274
+        )
9275
+
9272 9276
         hypervisor_uri_map = (
9273 9277
             ('xen', 'xenmigr://%s/system'),
9274 9278
             ('kvm', 'qemu+tcp://%s/system'),
@@ -9277,17 +9281,42 @@ class LibvirtConnTestCase(test.NoDBTestCase,
9277 9281
             # anything else will return None
9278 9282
             ('lxc', None),
9279 9283
         )
9280
-        dest = 'destination'
9281
-        for hyperv, uri in hypervisor_uri_map:
9282
-            self.flags(virt_type=hyperv, group='libvirt')
9283
-            drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
9284
-            if uri is not None:
9285
-                uri = uri % dest
9286
-                self.assertEqual(uri, drvr._live_migration_uri(dest))
9287
-            else:
9288
-                self.assertRaises(exception.LiveMigrationURINotAvailable,
9289
-                                  drvr._live_migration_uri,
9290
-                                  dest)
9284
+
9285
+        drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
9286
+        for dest in addresses:
9287
+            for hyperv, uri in hypervisor_uri_map:
9288
+                self.flags(virt_type=hyperv, group='libvirt')
9289
+                if uri is not None:
9290
+                    uri = uri % dest
9291
+                    self.assertEqual(uri, drvr._live_migration_uri(dest))
9292
+                else:
9293
+                    self.assertRaises(exception.LiveMigrationURINotAvailable,
9294
+                                      drvr._live_migration_uri,
9295
+                                      dest)
9296
+
9297
+    def test_live_migration_uri_ipv6(self):
9298
+        addresses = ('::1', '0:0:0:0:0:0:0:1', u'::1')
9299
+
9300
+        hypervisor_uri_map = (
9301
+            ('xen', 'xenmigr://[%s]/system'),
9302
+            ('kvm', 'qemu+tcp://[%s]/system'),
9303
+            ('qemu', 'qemu+tcp://[%s]/system'),
9304
+            ('parallels', 'parallels+tcp://[%s]/system'),
9305
+            # anything else will return None
9306
+            ('lxc', None),
9307
+        )
9308
+
9309
+        for dest in addresses:
9310
+            for hyperv, uri in hypervisor_uri_map:
9311
+                self.flags(virt_type=hyperv, group='libvirt')
9312
+                drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
9313
+                if uri is not None:
9314
+                    uri = uri % dest
9315
+                    self.assertEqual(uri, drvr._live_migration_uri(dest))
9316
+                else:
9317
+                    self.assertRaises(exception.LiveMigrationURINotAvailable,
9318
+                                      drvr._live_migration_uri,
9319
+                                      dest)
9291 9320
 
9292 9321
     def test_live_migration_uri_forced(self):
9293 9322
         dest = 'destination'
@@ -9343,12 +9372,26 @@ class LibvirtConnTestCase(test.NoDBTestCase,
9343 9372
         self.flags(virt_type='kvm', group='libvirt')
9344 9373
 
9345 9374
         drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
9346
-        addresses = ('127.0.0.1', '127.0.0.1:4444',
9347
-                     '0:0:0:0:0:0:0:1', '[0:0:0:0:0:0:0:1]:4444',
9348
-                     u'127.0.0.1', u'destination')
9375
+        addresses = ('127.0.0.1', '127.0.0.1:4444', '[::1]:4444',
9376
+                    '[0:0:0:0:0:0:0:1]:4444', u'127.0.0.1', u'destination',
9377
+        )
9378
+
9379
+        for dest in addresses:
9380
+            uri = 'tcp://%s'
9381
+            result = drvr._migrate_uri(dest)
9382
+            self.assertEqual(uri % dest, result)
9383
+            self.assertIsInstance(result, str)
9384
+
9385
+    def test_migrate_uri_forced_live_migration_inboud_addr_ipv6(self):
9386
+        self.flags(virt_type='kvm', group='libvirt')
9387
+
9388
+        drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
9389
+        addresses = ('::1', '0:0:0:0:0:0:0:1', u'::1')
9390
+
9349 9391
         for dest in addresses:
9392
+            uri = 'tcp://[%s]'
9350 9393
             result = drvr._migrate_uri(dest)
9351
-            self.assertEqual('tcp://%s' % dest, result)
9394
+            self.assertEqual(uri % dest, result)
9352 9395
             self.assertIsInstance(result, str)
9353 9396
 
9354 9397
     def test_update_volume_xml_no_serial(self):

+ 5
- 0
nova/virt/libvirt/driver.py View File

@@ -59,6 +59,7 @@ from oslo_utils import encodeutils
59 59
 from oslo_utils import excutils
60 60
 from oslo_utils import fileutils
61 61
 from oslo_utils import importutils
62
+from oslo_utils import netutils as oslo_netutils
62 63
 from oslo_utils import strutils
63 64
 from oslo_utils import timeutils
64 65
 from oslo_utils import units
@@ -741,6 +742,8 @@ class LibvirtDriver(driver.ComputeDriver):
741 742
             'xen': 'xenmigr://%s/system',
742 743
             'parallels': 'parallels+tcp://%s/system',
743 744
         }
745
+        dest = oslo_netutils.escape_ipv6(dest)
746
+
744 747
         virt_type = CONF.libvirt.virt_type
745 748
         # TODO(pkoniszewski): Remove fetching live_migration_uri in Pike
746 749
         uri = CONF.libvirt.live_migration_uri
@@ -760,6 +763,8 @@ class LibvirtDriver(driver.ComputeDriver):
760 763
     @staticmethod
761 764
     def _migrate_uri(dest):
762 765
         uri = None
766
+        dest = oslo_netutils.escape_ipv6(dest)
767
+
763 768
         # Only QEMU live migrations supports migrate-uri parameter
764 769
         virt_type = CONF.libvirt.virt_type
765 770
         if virt_type in ('qemu', 'kvm'):

+ 4
- 0
releasenotes/notes/libvirt_fix_ipv6_live_migration-bbcde8f3b7d17921.yaml View File

@@ -0,0 +1,4 @@
1
+---
2
+fixes:
3
+  - A change has been introduced in the libvirt driver to correctly handle
4
+    IPv6 addresses for live migration.

Loading…
Cancel
Save