[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

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



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.
---
 src/conf/domain_conf.c   |   43 +++++++++++++++++++++++++++++++++++++++++++
 src/conf/domain_conf.h   |    5 +++++
 src/libvirt_private.syms |    2 ++
 3 files changed, 50 insertions(+), 0 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index e38cdfe..cb56f50 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -13263,6 +13263,49 @@ error:
     return NULL;
 }

+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;
+}
+
+int
+virDomainDefCompatibleDevice(virDomainDefPtr def,
+                             virDomainDeviceDefPtr dev)
+{
+    int t = dev->type;
+    if (!virDomainDefHasUSB(def) &&
+        ((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))) {
+        virDomainReportError(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 d02695d..557bffa 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -1971,6 +1971,11 @@ int virDomainDefFormatInternal(virDomainDefPtr def,
                                unsigned int flags,
                                virBufferPtr buf);

+bool virDomainDefHasUSB(virDomainDefPtr def);
+
+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 b173590..6d748b4 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -278,9 +278,11 @@ virDomainDefAddImplicitControllers;
 virDomainDefCheckABIStability;
 virDomainDefClearDeviceAliases;
 virDomainDefClearPCIAddresses;
+virDomainDefCompatibleDevice;
 virDomainDefFormat;
 virDomainDefFormatInternal;
 virDomainDefFree;
+virDomainDefHasUSB;
 virDomainDefParseFile;
 virDomainDefParseNode;
 virDomainDefParseString;
-- 
1.7.8.6


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]