render / rpms / libvirt

Forked from rpms/libvirt 10 months ago
Clone
Blob Blame History Raw
From ad806164d0df731d6ce8b24109abb360584ea64b Mon Sep 17 00:00:00 2001
Message-Id: <ad806164d0df731d6ce8b24109abb360584ea64b@dist-git>
From: =?UTF-8?q?J=C3=A1n=20Tomko?= <jtomko@redhat.com>
Date: Thu, 21 Jul 2016 15:57:47 +0200
Subject: [PATCH] Introduce virDomainUSBDeviceDefForeach
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

A helper that will execute a callback on every USB device
in the domain definition.

With an ability to skip USB hubs, since we will want to treat
them differently in some cases.

(cherry picked from commit 492b3bfda175a846bdbff898607197b0b306d58c)
Signed-off-by: Ján Tomko <jtomko@redhat.com>

https://bugzilla.redhat.com/show_bug.cgi?id=1215968
---
 src/conf/domain_conf.c   | 89 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/conf/domain_conf.h   |  7 ++++
 src/libvirt_private.syms |  1 +
 3 files changed, 97 insertions(+)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index bb297f6..e6dc23d 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -24022,6 +24022,95 @@ virDomainSmartcardDefForeach(virDomainDefPtr def,
 }
 
 
+int
+virDomainUSBDeviceDefForeach(virDomainDefPtr def,
+                             virDomainUSBDeviceDefIterator iter,
+                             void *opaque,
+                             bool skipHubs)
+{
+    size_t i;
+
+    /* usb-hub */
+    if (!skipHubs) {
+        for (i = 0; i < def->nhubs; i++) {
+            virDomainHubDefPtr hub = def->hubs[i];
+            if (hub->type == VIR_DOMAIN_HUB_TYPE_USB) {
+                if (iter(&hub->info, opaque) < 0)
+                    return -1;
+            }
+        }
+    }
+
+    /* usb-host */
+    for (i = 0; i < def->nhostdevs; i++) {
+        virDomainHostdevDefPtr hostdev = def->hostdevs[i];
+        if (hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) {
+            if (iter(hostdev->info, opaque) < 0)
+                return -1;
+        }
+    }
+
+    /* usb-storage */
+    for (i = 0; i < def->ndisks; i++) {
+        virDomainDiskDefPtr disk = def->disks[i];
+        if (disk->bus == VIR_DOMAIN_DISK_BUS_USB) {
+            if (iter(&disk->info, opaque) < 0)
+                return -1;
+        }
+    }
+
+    /* TODO: add def->nets here when libvirt starts supporting usb-net */
+
+    /* usb-ccid */
+    for (i = 0; i < def->ncontrollers; i++) {
+        virDomainControllerDefPtr cont = def->controllers[i];
+        if (cont->type == VIR_DOMAIN_CONTROLLER_TYPE_CCID) {
+            if (iter(&cont->info, opaque) < 0)
+                return -1;
+        }
+    }
+
+    /* usb-kbd, usb-mouse, usb-tablet */
+    for (i = 0; i < def->ninputs; i++) {
+        virDomainInputDefPtr input = def->inputs[i];
+
+        if (input->bus == VIR_DOMAIN_INPUT_BUS_USB) {
+            if (iter(&input->info, opaque) < 0)
+                return -1;
+        }
+    }
+
+    /* usb-serial */
+    for (i = 0; i < def->nserials; i++) {
+        virDomainChrDefPtr serial = def->serials[i];
+        if (serial->targetType == VIR_DOMAIN_CHR_SERIAL_TARGET_TYPE_USB) {
+            if (iter(&serial->info, opaque) < 0)
+                return -1;
+        }
+    }
+
+    /* usb-audio model=usb */
+    for (i = 0; i < def->nsounds; i++) {
+        virDomainSoundDefPtr sound = def->sounds[i];
+        if (sound->model == VIR_DOMAIN_SOUND_MODEL_USB) {
+            if (iter(&sound->info, opaque) < 0)
+                return -1;
+        }
+    }
+
+    /* usb-redir */
+    for (i = 0; i < def->nredirdevs; i++) {
+        virDomainRedirdevDefPtr redirdev = def->redirdevs[i];
+        if (redirdev->bus == VIR_DOMAIN_REDIRDEV_BUS_USB) {
+            if (iter(&redirdev->info, opaque) < 0)
+                return -1;
+        }
+    }
+
+    return 0;
+}
+
+
 /* Call iter(disk, name, depth, opaque) for each element of disk and
  * its backing chain in the pre-populated disk->src.backingStore.
  * ignoreOpenFailure determines whether to warn about a chain that
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index e1990dc..f29c4bd 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -2884,6 +2884,13 @@ typedef int (*virDomainDiskDefPathIterator)(virDomainDiskDefPtr disk,
                                             size_t depth,
                                             void *opaque);
 
+typedef int (*virDomainUSBDeviceDefIterator)(virDomainDeviceInfoPtr info,
+                                             void *opaque);
+int virDomainUSBDeviceDefForeach(virDomainDefPtr def,
+                                 virDomainUSBDeviceDefIterator iter,
+                                 void *opaque,
+                                 bool skipHubs);
+
 int virDomainDiskDefForeachPath(virDomainDiskDefPtr disk,
                                 bool ignoreOpenFailure,
                                 virDomainDiskDefPathIterator iter,
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 4617f5d..de620a8 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -473,6 +473,7 @@ virDomainTPMBackendTypeToString;
 virDomainTPMDefFree;
 virDomainTPMModelTypeFromString;
 virDomainTPMModelTypeToString;
+virDomainUSBDeviceDefForeach;
 virDomainVideoDefaultRAM;
 virDomainVideoDefaultType;
 virDomainVideoDefFree;
-- 
2.9.2