[libvirt] [PATCHv3 2/3] domain_conf: Add helpers to verify if device configuration is valid

Peter Krempa pkrempa at redhat.com
Mon Jul 23 12:19:14 UTC 2012


This patch adds helpers that validate domain's device configuration.
This will be needed later on to verify devices being hot-plugged to
guests. If the guest has no USB bus, then it's not valid to plug a USB
device to that guest.
---
Diff to v2:
- split out USB device checking to a separate function
- made virDomainDefHasUSB static as it's not used outside of domain_conf.c
---
 src/conf/domain_conf.c   |   52 ++++++++++++++++++++++++++++++++++++++++++++++
 src/conf/domain_conf.h   |    3 ++
 src/libvirt_private.syms |    1 +
 3 files changed, 56 insertions(+), 0 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index cf3b1c4..b980e60 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -13265,6 +13265,58 @@ error:
     return NULL;
 }

+static bool
+virDomainDefHasUSB(virDomainDefPtr def)
+{
+    int i;
+
+    for (i = 0; i < def->ncontrollers; i++) {
+        if (def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_USB &&
+            def->controllers[i]->model != VIR_DOMAIN_CONTROLLER_MODEL_USB_NONE)
+            return true;
+    }
+
+    return false;
+}
+
+static bool
+virDomainDeviceIsUSB(virDomainDeviceDefPtr dev)
+{
+    int t = dev->type;
+    if ((t == VIR_DOMAIN_DEVICE_DISK &&
+         dev->data.disk->bus == VIR_DOMAIN_DISK_BUS_USB) ||
+        (t == VIR_DOMAIN_DEVICE_CONTROLLER &&
+         dev->data.controller->type == VIR_DOMAIN_CONTROLLER_TYPE_USB) ||
+        (t == VIR_DOMAIN_DEVICE_INPUT &&
+         dev->data.input->type == VIR_DOMAIN_INPUT_BUS_USB) ||
+        (t == VIR_DOMAIN_DEVICE_HOSTDEV &&
+         dev->data.hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
+         dev->data.hostdev->source.subsys.type ==
+         VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) ||
+        (t == VIR_DOMAIN_DEVICE_HUB &&
+         dev->data.hub->type == VIR_DOMAIN_HUB_TYPE_USB) ||
+        (t == VIR_DOMAIN_DEVICE_REDIRDEV &&
+         dev->data.redirdev->bus == VIR_DOMAIN_REDIRDEV_BUS_USB))
+        return true;
+
+    return false;
+}
+
+int
+virDomainDefCompatibleDevice(virDomainDefPtr def,
+                             virDomainDeviceDefPtr dev)
+{
+    if (!virDomainDefHasUSB(def) &&
+        virDomainDeviceIsUSB(dev)) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Device configuration is not compatible: "
+                         "Domain has no USB bus support"));
+        return -1;
+    }
+
+    return 0;
+}
+
 int virDomainSaveXML(const char *configDir,
                      virDomainDefPtr def,
                      const char *xml)
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 203eebf..1c2b76b 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -1971,6 +1971,9 @@ int virDomainDefFormatInternal(virDomainDefPtr def,
                                unsigned int flags,
                                virBufferPtr buf);

+int virDomainDefCompatibleDevice(virDomainDefPtr def,
+                                 virDomainDeviceDefPtr dev);
+
 int virDomainCpuSetParse(const char *str,
                          char sep,
                          char *cpuset,
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 734c881..24a36b9 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -281,6 +281,7 @@ virDomainDefAddImplicitControllers;
 virDomainDefCheckABIStability;
 virDomainDefClearDeviceAliases;
 virDomainDefClearPCIAddresses;
+virDomainDefCompatibleDevice;
 virDomainDefFormat;
 virDomainDefFormatInternal;
 virDomainDefFree;
-- 
1.7.8.6




More information about the libvir-list mailing list