Skip to content

Commit 755aa11

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Reattach mdevs to guest on resume"
2 parents 9f296d7 + 16f7c60 commit 755aa11

File tree

4 files changed

+107
-7
lines changed

4 files changed

+107
-7
lines changed

doc/source/admin/virtual-gpu.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,10 @@ Caveats
301301
that will cause the instance to be set back to ACTIVE. The ``suspend`` action
302302
in the ``os-instance-actions`` API will have an *Error* state.
303303

304+
.. versionchanged:: 25.0.0
305+
306+
This has been resolved in the Yoga release. See `bug 1948705`_.
307+
304308
* Resizing an instance with a new flavor that has vGPU resources doesn't
305309
allocate those vGPUs to the instance (the instance is created without
306310
vGPU resources). The proposed workaround is to rebuild the instance after
@@ -350,6 +354,7 @@ For nested vGPUs:
350354

351355
.. _bug 1778563: https://bugs.launchpad.net/nova/+bug/1778563
352356
.. _bug 1762688: https://bugs.launchpad.net/nova/+bug/1762688
357+
.. _bug 1948705: https://bugs.launchpad.net/nova/+bug/1948705
353358

354359
.. Links
355360
.. _Intel GVT-g: https://01.org/igvt-g

nova/tests/unit/virt/libvirt/test_driver.py

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16604,9 +16604,15 @@ def test_resume(self):
1660416604
mock.patch.object(guest, 'sync_guest_time'),
1660516605
mock.patch.object(drvr, '_wait_for_running',
1660616606
side_effect=loopingcall.LoopingCallDone()),
16607+
mock.patch.object(drvr,
16608+
'_get_mdevs_from_guest_config',
16609+
return_value='fake_mdevs'),
16610+
mock.patch.object(drvr, '_attach_mediated_devices'),
1660716611
) as (_get_existing_domain_xml, _create_guest_with_network,
1660816612
_attach_pci_devices, get_instance_pci_devs, get_image_metadata,
16609-
mock_sync_time, mock_wait):
16613+
mock_sync_time, mock_wait,
16614+
_get_mdevs_from_guest_config,
16615+
_attach_mediated_devices):
1661016616
get_image_metadata.return_value = {'bar': 234}
1661116617

1661216618
drvr.resume(self.context, instance, network_info,
@@ -16621,6 +16627,9 @@ def test_resume(self):
1662116627
self.assertTrue(mock_sync_time.called)
1662216628
_attach_pci_devices.assert_has_calls([mock.call(guest,
1662316629
'fake_pci_devs')])
16630+
_attach_mediated_devices.assert_has_calls(
16631+
[mock.call(guest, 'fake_mdevs')]
16632+
)
1662416633

1662516634
@mock.patch.object(host.Host, '_get_domain')
1662616635
@mock.patch.object(libvirt_driver.LibvirtDriver, 'get_info')
@@ -26116,6 +26125,55 @@ def test_detach_mediated_devices_raises_exc(self):
2611626125
self.assertRaises(test.TestingException,
2611726126
self._test_detach_mediated_devices, exc)
2611826127

26128+
@mock.patch.object(libvirt_guest.Guest, 'attach_device')
26129+
def _test_attach_mediated_devices(self, side_effect, attach_device):
26130+
dom_without_vgpu = (
26131+
"""<domain> <devices>
26132+
<disk type='file' device='disk'>
26133+
<driver name='qemu' type='qcow2' cache='none'/>
26134+
<source file='xxx'/>
26135+
<target dev='vda' bus='virtio'/>
26136+
<alias name='virtio-disk0'/>
26137+
<address type='pci' domain='0x0000' bus='0x00'
26138+
slot='0x04' function='0x0'/>
26139+
</disk>
26140+
</devices></domain>""")
26141+
26142+
vgpu_xml = (
26143+
"""<domain> <devices>
26144+
<hostdev mode='subsystem' type='mdev' managed='no'
26145+
model='vfio-pci'>
26146+
<source>
26147+
<address uuid='81db53c6-6659-42a0-a34c-1507fdc72983'/>
26148+
</source>
26149+
<alias name='hostdev0'/>
26150+
<address type='pci' domain='0x0000' bus='0x00' slot='0x05'
26151+
function='0x0'/>
26152+
</hostdev>
26153+
</devices></domain>""")
26154+
26155+
attach_device.side_effect = side_effect
26156+
26157+
drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), True)
26158+
guest = libvirt_guest.Guest(FakeVirtDomain(fake_xml=dom_without_vgpu))
26159+
mdevs = drvr._get_mdevs_from_guest_config(vgpu_xml)
26160+
drvr._attach_mediated_devices(guest, mdevs)
26161+
return attach_device
26162+
26163+
def test_attach_mediated_devices(self):
26164+
def fake_attach_device(cfg_obj, **kwargs):
26165+
self.assertIsInstance(cfg_obj,
26166+
vconfig.LibvirtConfigGuestHostdevMDEV)
26167+
26168+
attach_mock = self._test_attach_mediated_devices(fake_attach_device)
26169+
attach_mock.assert_called_once_with(mock.ANY, live=True)
26170+
26171+
def test_attach_mediated_devices_raises_exc(self):
26172+
exc = test.TestingException()
26173+
26174+
self.assertRaises(test.TestingException,
26175+
self._test_attach_mediated_devices, exc)
26176+
2611926177
def test_storage_bus_traits__qemu_kvm(self):
2612026178
"""Test getting storage bus traits per virt type.
2612126179
"""

nova/virt/libvirt/driver.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3987,6 +3987,10 @@ def resume(self, context, instance, network_info, block_device_info=None):
39873987
"""resume the specified instance."""
39883988
xml = self._get_existing_domain_xml(instance, network_info,
39893989
block_device_info)
3990+
# NOTE(gsantos): The mediated devices that were removed on suspension
3991+
# are still present in the xml. Let's take their references from it
3992+
# and re-attach them.
3993+
mdevs = self._get_mdevs_from_guest_config(xml)
39903994
# NOTE(efried): The instance should already have a vtpm_secret_uuid
39913995
# registered if appropriate.
39923996
guest = self._create_guest_with_network(
@@ -3996,6 +4000,7 @@ def resume(self, context, instance, network_info, block_device_info=None):
39964000
pci_manager.get_instance_pci_devs(instance))
39974001
self._attach_direct_passthrough_ports(
39984002
context, instance, guest, network_info)
4003+
self._attach_mediated_devices(guest, mdevs)
39994004
timer = loopingcall.FixedIntervalLoopingCall(self._wait_for_running,
40004005
instance)
40014006
timer.start(interval=0.5).wait()
@@ -8030,12 +8035,6 @@ def _detach_mediated_devices(self, guest):
80308035
guest.detach_device(mdev_cfg, live=True)
80318036
except libvirt.libvirtError as ex:
80328037
error_code = ex.get_error_code()
8033-
# NOTE(sbauza): There is a pending issue with libvirt that
8034-
# doesn't allow to hot-unplug mediated devices. Let's
8035-
# short-circuit the suspend action and set the instance back
8036-
# to ACTIVE.
8037-
# TODO(sbauza): Once libvirt supports this, amend the resume()
8038-
# operation to support reallocating mediated devices.
80398038
if error_code == libvirt.VIR_ERR_CONFIG_UNSUPPORTED:
80408039
reason = _("Suspend is not supported for instances having "
80418040
"attached mediated devices.")
@@ -8044,6 +8043,38 @@ def _detach_mediated_devices(self, guest):
80448043
else:
80458044
raise
80468045

8046+
def _attach_mediated_devices(self, guest, devs):
8047+
for mdev_cfg in devs:
8048+
try:
8049+
guest.attach_device(mdev_cfg, live=True)
8050+
except libvirt.libvirtError as ex:
8051+
error_code = ex.get_error_code()
8052+
if error_code == libvirt.VIR_ERR_DEVICE_MISSING:
8053+
LOG.warning("The mediated device %s was not found and "
8054+
"won't be reattached to %s.", mdev_cfg, guest)
8055+
else:
8056+
raise
8057+
8058+
def _get_mdevs_from_guest_config(self, xml):
8059+
"""Get all libvirt's mediated devices from a guest's config (XML) file.
8060+
We don't have to worry about those devices being used by another guest,
8061+
since they remain allocated for the current guest as long as they are
8062+
present in the XML.
8063+
8064+
:param xml: The XML from the guest we want to get a list of mdevs from.
8065+
8066+
:returns: A list containing the objects that represent the mediated
8067+
devices attached to the guest's config passed as argument.
8068+
"""
8069+
config = vconfig.LibvirtConfigGuest()
8070+
config.parse_str(xml)
8071+
8072+
devs = []
8073+
for dev in config.devices:
8074+
if isinstance(dev, vconfig.LibvirtConfigGuestHostdevMDEV):
8075+
devs.append(dev)
8076+
return devs
8077+
80478078
def _has_numa_support(self):
80488079
# This means that the host can support LibvirtConfigGuestNUMATune
80498080
# and the nodeset field in LibvirtConfigGuestMemoryBackingPage
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
fixes:
3+
- |
4+
Amended the guest resume operation to support mediated devices, as
5+
libvirt's minimum required version (v6.0.0) supports the hot-plug/unplug of
6+
mediated devices, which was addressed in v4.3.0.

0 commit comments

Comments
 (0)