[libvirt] [PATCH 02/23] Refactor virDomainHostdevFind method

Daniel P. Berrange berrange at redhat.com
Fri Nov 30 20:26:16 UTC 2012


From: "Daniel P. Berrange" <berrange at redhat.com>

Move the code for matching hostdev instances out of virDomainHostdevFind
and into virDomainHostdevMatch method, which in turn calls out to other
helper methods depending on the type of hostdev.

Signed-off-by: Daniel P. Berrange <berrange at redhat.com>
---
 src/conf/domain_conf.c | 108 +++++++++++++++++++++++++++++--------------------
 1 file changed, 65 insertions(+), 43 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index ed31431..dd0e841 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -7830,6 +7830,69 @@ virDomainHostdevRemove(virDomainDefPtr def, size_t i)
     return hostdev;
 }
 
+
+static int
+virDomainHostdevMatchSubsysUSB(virDomainHostdevDefPtr a,
+                               virDomainHostdevDefPtr b)
+{
+    if (b->source.subsys.u.usb.bus && b->source.subsys.u.usb.device) {
+        /* specified by bus location on host */
+        if (a->source.subsys.u.usb.bus == b->source.subsys.u.usb.bus &&
+            a->source.subsys.u.usb.device == b->source.subsys.u.usb.device)
+            return 1;
+    } else {
+        /* specified by product & vendor id */
+        if (a->source.subsys.u.usb.product == b->source.subsys.u.usb.product &&
+            a->source.subsys.u.usb.vendor == b->source.subsys.u.usb.vendor)
+            return 1;
+    }
+    return 0;
+}
+
+static int
+virDomainHostdevMatchSubsysPCI(virDomainHostdevDefPtr a,
+                               virDomainHostdevDefPtr b)
+{
+    if (a->source.subsys.u.pci.domain == b->source.subsys.u.pci.domain &&
+        a->source.subsys.u.pci.bus == b->source.subsys.u.pci.bus &&
+        a->source.subsys.u.pci.slot == b->source.subsys.u.pci.slot &&
+        a->source.subsys.u.pci.function == b->source.subsys.u.pci.function)
+        return 1;
+    return 0;
+}
+
+
+static int
+virDomainHostdevMatchSubsys(virDomainHostdevDefPtr a,
+                            virDomainHostdevDefPtr b)
+{
+    if (a->source.subsys.type != b->source.subsys.type)
+        return 0;
+
+    switch (a->source.subsys.type) {
+    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI:
+        return virDomainHostdevMatchSubsysPCI(a, b);
+    case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB:
+        return virDomainHostdevMatchSubsysUSB(a, b);
+    }
+    return 0;
+}
+
+
+static int
+virDomainHostdevMatch(virDomainHostdevDefPtr a,
+                      virDomainHostdevDefPtr b)
+{
+    if (a->mode != b->mode)
+        return 0;
+
+    switch (a->mode) {
+    case VIR_DOMAIN_HOSTDEV_MODE_SUBSYS:
+        return virDomainHostdevMatchSubsys(a, b);
+    }
+    return 0;
+}
+
 /* Find an entry in hostdevs that matches the source spec in
  * @match. return pointer to the entry in @found (if found is
  * non-NULL). Returns index (within hostdevs) of matched entry, or -1
@@ -7841,58 +7904,17 @@ virDomainHostdevFind(virDomainDefPtr def,
                      virDomainHostdevDefPtr *found)
 {
     virDomainHostdevDefPtr local_found;
-    virDomainHostdevSubsysPtr m_subsys = &match->source.subsys;
     int i;
 
     if (!found)
         found = &local_found;
     *found = NULL;
 
-    /* There is no code that uses _MODE_CAPABILITIES, and nothing to
-     * compare if it did, so don't allow it.
-     */
-    if (match->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
-        return -1;
-
     for (i = 0 ; i < def->nhostdevs ; i++) {
-        virDomainHostdevDefPtr compare = def->hostdevs[i];
-        virDomainHostdevSubsysPtr c_subsys = &compare->source.subsys;
-
-        if (compare->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS ||
-            c_subsys->type != m_subsys->type) {
-            continue;
-        }
-
-        switch (m_subsys->type)
-        {
-        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI:
-            if (c_subsys->u.pci.domain == m_subsys->u.pci.domain &&
-                c_subsys->u.pci.bus == m_subsys->u.pci.bus &&
-                c_subsys->u.pci.slot == m_subsys->u.pci.slot &&
-                c_subsys->u.pci.function == m_subsys->u.pci.function) {
-                *found = compare;
-            }
-                break;
-        case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB:
-            if (m_subsys->u.usb.bus && m_subsys->u.usb.device) {
-                /* specified by bus location on host */
-                if (c_subsys->u.usb.bus == m_subsys->u.usb.bus &&
-                    c_subsys->u.usb.device == m_subsys->u.usb.device) {
-                    *found = compare;
-                }
-            } else {
-                /* specified by product & vendor id */
-                if (c_subsys->u.usb.product == m_subsys->u.usb.product &&
-                    c_subsys->u.usb.vendor == m_subsys->u.usb.vendor) {
-                    *found = compare;
-                }
-            }
-            break;
-        default:
+        if (virDomainHostdevMatch(match, def->hostdevs[i])) {
+            *found = def->hostdevs[i];
             break;
         }
-        if (*found)
-            break;
     }
     return *found ? i : -1;
 }
-- 
1.8.0.1




More information about the libvir-list mailing list