From e920c5b6c94631f4d4564468a2d7f3b8deb91287 Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Tue, 30 Apr 2013 15:05:10 +0100 Subject: [PATCH] Simplify the Xen domain lookup driver methods Unconditionally invoke the xenHypervisorLookupDomainByID, xenHypervisorLookupDomainByUUID or xenDaemonLookupByName for looking up domains. Fallback to xenXMDomainLookupByUUID and xenXMDomainLookupByName for legacy XenD without inactive domain support Signed-off-by: Daniel P. Berrange --- src/xen/xen_driver.c | 110 ++++++++++++---------------------------- src/xen/xend_internal.c | 89 -------------------------------- src/xen/xend_internal.h | 14 ----- src/xen/xs_internal.c | 62 ---------------------- src/xen/xs_internal.h | 2 - 5 files changed, 33 insertions(+), 244 deletions(-) diff --git a/src/xen/xen_driver.c b/src/xen/xen_driver.c index ec5ef0703e..b3a539dd47 100644 --- a/src/xen/xen_driver.c +++ b/src/xen/xen_driver.c @@ -601,38 +601,17 @@ xenUnifiedDomainCreateXML(virConnectPtr conn, return xenDaemonCreateXML(conn, xmlDesc); } -/* Assumption made in underlying drivers: - * If the domain is "not found" and there is no other error, then - * the Lookup* functions return a NULL but do not set virterror. - */ static virDomainPtr xenUnifiedDomainLookupByID(virConnectPtr conn, int id) { - xenUnifiedPrivatePtr priv = conn->privateData; - virDomainPtr ret; + virDomainPtr ret = NULL; - /* Reset any connection-level errors in virterror first, in case - * there is one hanging around from a previous call. - */ - virConnResetLastError(conn); + ret = xenHypervisorLookupDomainByID(conn, id); - /* Try hypervisor/xenstore combo. */ - if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET]) { - ret = xenHypervisorLookupDomainByID(conn, id); - if (ret || conn->err.code != VIR_ERR_OK) - return ret; - } + if (!ret && virGetLastError() == NULL) + virReportError(VIR_ERR_NO_DOMAIN, __FUNCTION__); - /* Try xend. */ - if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) { - ret = xenDaemonLookupByID(conn, id); - if (ret || conn->err.code != VIR_ERR_OK) - return ret; - } - - /* Not found. */ - virReportError(VIR_ERR_NO_DOMAIN, __FUNCTION__); - return NULL; + return ret; } static virDomainPtr @@ -642,35 +621,20 @@ xenUnifiedDomainLookupByUUID(virConnectPtr conn, xenUnifiedPrivatePtr priv = conn->privateData; virDomainPtr ret; - /* Reset any connection-level errors in virterror first, in case - * there is one hanging around from a previous call. - */ - virConnResetLastError(conn); - - /* Try hypervisor/xenstore combo. */ - if (priv->opened[XEN_UNIFIED_HYPERVISOR_OFFSET]) { - ret = xenHypervisorLookupDomainByUUID(conn, uuid); - if (ret || conn->err.code != VIR_ERR_OK) - return ret; - } - - /* Try xend. */ - if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) { - ret = xenDaemonLookupByUUID(conn, uuid); - if (ret || conn->err.code != VIR_ERR_OK) - return ret; - } + ret = xenHypervisorLookupDomainByUUID(conn, uuid); /* Try XM for inactive domains. */ - if (priv->opened[XEN_UNIFIED_XM_OFFSET]) { - ret = xenXMDomainLookupByUUID(conn, uuid); - if (ret || conn->err.code != VIR_ERR_OK) - return ret; + if (!ret) { + if (priv->xendConfigVersion <= XEND_CONFIG_VERSION_3_0_3) + ret = xenXMDomainLookupByUUID(conn, uuid); + else + ret = xenDaemonLookupByUUID(conn, uuid); } - /* Not found. */ - virReportError(VIR_ERR_NO_DOMAIN, __FUNCTION__); - return NULL; + if (!ret && virGetLastError() == NULL) + virReportError(VIR_ERR_NO_DOMAIN, __FUNCTION__); + + return ret; } static virDomainPtr @@ -680,50 +644,42 @@ xenUnifiedDomainLookupByName(virConnectPtr conn, xenUnifiedPrivatePtr priv = conn->privateData; virDomainPtr ret; - /* Reset any connection-level errors in virterror first, in case - * there is one hanging around from a previous call. - */ - virConnResetLastError(conn); - - /* Try xend. */ - if (priv->opened[XEN_UNIFIED_XEND_OFFSET]) { - ret = xenDaemonLookupByName(conn, name); - if (ret || conn->err.code != VIR_ERR_OK) - return ret; - } - - /* Try xenstore for inactive domains. */ - if (priv->opened[XEN_UNIFIED_XS_OFFSET]) { - ret = xenStoreLookupByName(conn, name); - if (ret || conn->err.code != VIR_ERR_OK) - return ret; - } + ret = xenDaemonLookupByName(conn, name); /* Try XM for inactive domains. */ - if (priv->opened[XEN_UNIFIED_XM_OFFSET]) { + if (priv->xendConfigVersion <= XEND_CONFIG_VERSION_3_0_3) ret = xenXMDomainLookupByName(conn, name); - if (ret || conn->err.code != VIR_ERR_OK) - return ret; - } - /* Not found. */ - virReportError(VIR_ERR_NO_DOMAIN, __FUNCTION__); - return NULL; + if (!ret && virGetLastError() == NULL) + virReportError(VIR_ERR_NO_DOMAIN, __FUNCTION__); + + return ret; } static int xenUnifiedDomainIsActive(virDomainPtr dom) { + xenUnifiedPrivatePtr priv = dom->conn->privateData; virDomainPtr currdom; int ret = -1; /* ID field in dom may be outdated, so re-lookup */ - currdom = xenUnifiedDomainLookupByUUID(dom->conn, dom->uuid); + currdom = xenHypervisorLookupDomainByUUID(dom->conn, dom->uuid); + + /* Try XM for inactive domains. */ + if (!currdom) { + if (priv->xendConfigVersion <= XEND_CONFIG_VERSION_3_0_3) + currdom = xenXMDomainLookupByUUID(dom->conn, dom->uuid); + else + currdom = xenDaemonLookupByUUID(dom->conn, dom->uuid); + } if (currdom) { ret = currdom->id == -1 ? 0 : 1; virDomainFree(currdom); + } else if (virGetLastError() == NULL) { + virReportError(VIR_ERR_NO_DOMAIN, __FUNCTION__); } return ret; diff --git a/src/xen/xend_internal.c b/src/xen/xend_internal.c index 8981065c96..65721464d7 100644 --- a/src/xen/xend_internal.c +++ b/src/xen/xend_internal.c @@ -855,63 +855,6 @@ xenDaemonDomainLookupByName_ids(virConnectPtr xend, } -/** - * xenDaemonDomainLookupByID: - * @xend: A xend instance - * @id: The id of the domain - * @name: return value for the name if not NULL - * @uuid: return value for the UUID if not NULL - * - * This method looks up the name of a domain based on its id - * - * Returns the 0 on success; -1 (with errno) on error - */ -int -xenDaemonDomainLookupByID(virConnectPtr xend, - int id, - char **domname, - unsigned char *uuid) -{ - const char *name = NULL; - struct sexpr *root; - - memset(uuid, 0, VIR_UUID_BUFLEN); - - root = sexpr_get(xend, "/xend/domain/%d?detail=1", id); - if (root == NULL) - goto error; - - name = sexpr_node(root, "domain/name"); - if (name == NULL) { - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("domain information incomplete, missing name")); - goto error; - } - if (domname) { - *domname = strdup(name); - if (*domname == NULL) { - virReportOOMError(); - goto error; - } - } - - if (sexpr_uuid(uuid, root, "domain/uuid") < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("domain information incomplete, missing uuid")); - goto error; - } - - sexpr_free(root); - return 0; - -error: - sexpr_free(root); - if (domname) - VIR_FREE(*domname); - return -1; -} - - static int xend_detect_config_version(virConnectPtr conn) { @@ -1862,38 +1805,6 @@ xenDaemonNodeGetTopology(virConnectPtr conn, virCapsPtr caps) } -/** - * xenDaemonLookupByID: - * @conn: pointer to the hypervisor connection - * @id: the domain ID number - * - * Try to find a domain based on the hypervisor ID number - * - * Returns a new domain object or NULL in case of failure - */ -virDomainPtr -xenDaemonLookupByID(virConnectPtr conn, int id) -{ - char *name = NULL; - unsigned char uuid[VIR_UUID_BUFLEN]; - virDomainPtr ret; - - if (xenDaemonDomainLookupByID(conn, id, &name, uuid) < 0) { - goto error; - } - - ret = virGetDomain(conn, name, uuid); - if (ret == NULL) goto error; - - ret->id = id; - VIR_FREE(name); - return ret; - - error: - VIR_FREE(name); - return NULL; -} - /** * xenDaemonDomainSetVcpusFlags: * @domain: pointer to domain object diff --git a/src/xen/xend_internal.h b/src/xen/xend_internal.h index 5f82f045eb..e8713a72cc 100644 --- a/src/xen/xend_internal.h +++ b/src/xen/xend_internal.h @@ -69,19 +69,6 @@ int xenDaemonDomainLookupByName_ids(virConnectPtr xend, const char *name, unsigned char *uuid); -/** - * \brief Lookup the name of a domain - * \param xend A xend instance - * \param id The id of the domain - * \param name pointer to store a copy of the name - * \param uuid pointer to store a copy of the uuid - * - * This method looks up the name/uuid of a domain - */ -int xenDaemonDomainLookupByID(virConnectPtr xend, - int id, - char **name, unsigned char *uuid); - virDomainDefPtr xenDaemonDomainFetch(virConnectPtr xend, @@ -153,7 +140,6 @@ extern struct xenUnifiedDriver xenDaemonDriver; int xenDaemonInit (void); virDomainPtr xenDaemonCreateXML(virConnectPtr conn, const char *xmlDesc); -virDomainPtr xenDaemonLookupByID(virConnectPtr conn, int id); virDomainPtr xenDaemonLookupByUUID(virConnectPtr conn, const unsigned char *uuid); virDomainPtr xenDaemonLookupByName(virConnectPtr conn, const char *domname); int xenDaemonDomainMigratePrepare (virConnectPtr dconn, char **cookie, int *cookielen, const char *uri_in, char **uri_out, unsigned long flags, const char *dname, unsigned long resource); diff --git a/src/xen/xs_internal.c b/src/xen/xs_internal.c index dbb4ae4eda..7926535f0a 100644 --- a/src/xen/xs_internal.c +++ b/src/xen/xs_internal.c @@ -580,68 +580,6 @@ xenStoreListDomains(virConnectPtr conn, int *ids, int maxids) return ret; } -/** - * xenStoreLookupByName: - * @conn: A xend instance - * @name: The name of the domain - * - * Try to lookup a domain on the Xen Store based on its name. - * - * Returns a new domain object or NULL in case of failure - */ -virDomainPtr -xenStoreLookupByName(virConnectPtr conn, const char *name) -{ - virDomainPtr ret = NULL; - unsigned int num, i, len; - long id = -1; - char **idlist = NULL, *endptr; - char prop[200], *tmp; - int found = 0; - struct xend_domain *xenddomain = NULL; - xenUnifiedPrivatePtr priv = conn->privateData; - - if (priv->xshandle == NULL) - return NULL; - - idlist = xs_directory(priv->xshandle, 0, "/local/domain", &num); - if (idlist == NULL) - goto done; - - for (i = 0; i < num; i++) { - id = strtol(idlist[i], &endptr, 10); - if ((endptr == idlist[i]) || (*endptr != 0)) { - goto done; - } -#if 0 - if (virConnectCheckStoreID(conn, (int) id) < 0) - continue; -#endif - snprintf(prop, 199, "/local/domain/%s/name", idlist[i]); - prop[199] = 0; - tmp = xs_read(priv->xshandle, 0, prop, &len); - if (tmp != NULL) { - found = STREQ(name, tmp); - VIR_FREE(tmp); - if (found) - break; - } - } - if (!found) - goto done; - - ret = virGetDomain(conn, name, NULL); - if (ret == NULL) - goto done; - - ret->id = id; - -done: - VIR_FREE(xenddomain); - VIR_FREE(idlist); - - return ret; -} /** * xenStoreDomainShutdown: diff --git a/src/xen/xs_internal.h b/src/xen/xs_internal.h index 29f01652c8..fc7798dfca 100644 --- a/src/xen/xs_internal.h +++ b/src/xen/xs_internal.h @@ -43,8 +43,6 @@ int xenStoreNumOfDomains (virConnectPtr conn); int xenStoreListDomains (virConnectPtr conn, int *ids, int maxids); -virDomainPtr xenStoreLookupByName(virConnectPtr conn, - const char *name); unsigned long xenStoreGetMaxMemory (virDomainPtr domain); int xenStoreDomainSetMemory (virDomainPtr domain, unsigned long memory);