summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel P. Berrange <berrange@redhat.com>2012-11-27 13:50:13 +0000
committerDaniel P. Berrange <berrange@redhat.com>2012-12-12 12:27:53 +0000
commit9cdd9ea20e89c8a7b5a67b52d22cc45b08072f65 (patch)
tree1770beb12ce63997e50bced1d9130ff8cc3b6558
parent50897ffbb627e656dfb777a38123e902ad685539 (diff)
downloadlibvirt-9cdd9ea20e89c8a7b5a67b52d22cc45b08072f65.tar.gz
Refactor virDomainHostdevFind method
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@redhat.com>
-rw-r--r--src/conf/domain_conf.c108
1 files changed, 65 insertions, 43 deletions
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index f9933f5df7..12eeb5ee80 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -7885,6 +7885,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
@@ -7896,58 +7959,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;
}