From nobody Mon Dec 15 01:57:32 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1496769725987137.8951758730019; Tue, 6 Jun 2017 10:22:05 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id AA51481F07; Tue, 6 Jun 2017 17:21:58 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 73FE05C8BB; Tue, 6 Jun 2017 17:21:58 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 188F0180BAFD; Tue, 6 Jun 2017 17:21:58 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v559R8AG001978 for ; Mon, 5 Jun 2017 05:27:08 -0400 Received: by smtp.corp.redhat.com (Postfix) id 9B52B17CFF; Mon, 5 Jun 2017 09:27:08 +0000 (UTC) Received: from virval.usersys.redhat.com (dhcp129-92.brq.redhat.com [10.34.129.92]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 219B317CC0 for ; Mon, 5 Jun 2017 09:27:05 +0000 (UTC) Received: by virval.usersys.redhat.com (Postfix, from userid 500) id 9CCA810CCF7; Mon, 5 Jun 2017 11:27:04 +0200 (CEST) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com AA51481F07 Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com AA51481F07 From: Jiri Denemark To: libvir-list@redhat.com Date: Mon, 5 Jun 2017 11:26:50 +0200 Message-Id: In-Reply-To: References: In-Reply-To: References: Mail-Followup-To: libvir-list@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 02/15] conf: Refactor virCPUDefParseXML X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Tue, 06 Jun 2017 17:21:59 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Jiri Denemark --- src/conf/cpu_conf.c | 105 +++++++++++++++++++++++++++------------------= ---- src/conf/cpu_conf.h | 9 +++-- src/conf/domain_conf.c | 12 +----- src/cpu/cpu.c | 5 +-- tests/cputest.c | 5 +-- 5 files changed, 68 insertions(+), 68 deletions(-) diff --git a/src/conf/cpu_conf.c b/src/conf/cpu_conf.c index 79ed5e646..f7f726761 100644 --- a/src/conf/cpu_conf.c +++ b/src/conf/cpu_conf.c @@ -245,12 +245,21 @@ virCPUDefCopy(const virCPUDef *cpu) } =20 =20 -virCPUDefPtr -virCPUDefParseXML(xmlNodePtr node, - xmlXPathContextPtr ctxt, - virCPUType type) +/* + * Parses CPU definition XML from a node pointed to by @xpath. If @xpath is + * NULL, the current node of @ctxt is used (i.e., it is a shortcut to "."). + * + * Returns 0 on success, + * 1 when @xpath element cannot be found in @ctxt, + * -1 on error. + */ +int +virCPUDefParseXML(xmlXPathContextPtr ctxt, + const char *xpath, + virCPUType type, + virCPUDefPtr *cpu) { - virCPUDefPtr def; + virCPUDefPtr def =3D NULL; xmlNodePtr *nodes =3D NULL; xmlNodePtr oldnode =3D ctxt->node; int n; @@ -258,15 +267,23 @@ virCPUDefParseXML(xmlNodePtr node, char *cpuMode; char *fallback =3D NULL; char *vendor_id =3D NULL; + int ret =3D -1; =20 - if (!xmlStrEqual(node->name, BAD_CAST "cpu")) { + *cpu =3D NULL; + + if (xpath && !(ctxt->node =3D virXPathNode(xpath, ctxt))) { + ret =3D 1; + goto cleanup; + } + + if (!xmlStrEqual(ctxt->node->name, BAD_CAST "cpu")) { virReportError(VIR_ERR_XML_ERROR, "%s", _("XML does not contain expected 'cpu' element")); - return NULL; + goto cleanup; } =20 if (VIR_ALLOC(def) < 0) - return NULL; + goto cleanup; =20 if (type =3D=3D VIR_CPU_TYPE_AUTO) { if (virXPathBoolean("boolean(./arch)", ctxt)) { @@ -274,7 +291,7 @@ virCPUDefParseXML(xmlNodePtr node, virReportError(VIR_ERR_XML_ERROR, "%s", _("'arch' element cannot be used inside 'cp= u'" " element with 'match' attribute'")); - goto error; + goto cleanup; } def->type =3D VIR_CPU_TYPE_HOST; } else { @@ -284,12 +301,12 @@ virCPUDefParseXML(xmlNodePtr node, def->type =3D type; } =20 - if ((cpuMode =3D virXMLPropString(node, "mode"))) { + if ((cpuMode =3D virXMLPropString(ctxt->node, "mode"))) { if (def->type =3D=3D VIR_CPU_TYPE_HOST) { VIR_FREE(cpuMode); virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("Attribute mode is only allowed for guest CPU= ")); - goto error; + goto cleanup; } else { def->mode =3D virCPUModeTypeFromString(cpuMode); =20 @@ -298,7 +315,7 @@ virCPUDefParseXML(xmlNodePtr node, _("Invalid mode attribute '%s'"), cpuMode); VIR_FREE(cpuMode); - goto error; + goto cleanup; } VIR_FREE(cpuMode); } @@ -310,7 +327,7 @@ virCPUDefParseXML(xmlNodePtr node, } =20 if (def->type =3D=3D VIR_CPU_TYPE_GUEST) { - char *match =3D virXMLPropString(node, "match"); + char *match =3D virXMLPropString(ctxt->node, "match"); char *check; =20 if (!match) { @@ -326,11 +343,11 @@ virCPUDefParseXML(xmlNodePtr node, virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("Invalid match attribute for CPU " "specification")); - goto error; + goto cleanup; } } =20 - if ((check =3D virXMLPropString(node, "check"))) { + if ((check =3D virXMLPropString(ctxt->node, "check"))) { int value =3D virCPUCheckTypeFromString(check); VIR_FREE(check); =20 @@ -338,7 +355,7 @@ virCPUDefParseXML(xmlNodePtr node, virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("Invalid check attribute for CPU " "specification")); - goto error; + goto cleanup; } def->check =3D value; } @@ -349,13 +366,13 @@ virCPUDefParseXML(xmlNodePtr node, if (!arch) { virReportError(VIR_ERR_XML_ERROR, "%s", _("Missing CPU architecture")); - goto error; + goto cleanup; } if ((def->arch =3D virArchFromString(arch)) =3D=3D VIR_ARCH_NONE) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("Unknown architecture %s"), arch); VIR_FREE(arch); - goto error; + goto cleanup; } VIR_FREE(arch); } @@ -364,7 +381,7 @@ virCPUDefParseXML(xmlNodePtr node, def->type =3D=3D VIR_CPU_TYPE_HOST) { virReportError(VIR_ERR_XML_ERROR, "%s", _("Missing CPU model name")); - goto error; + goto cleanup; } =20 if (def->type =3D=3D VIR_CPU_TYPE_GUEST && @@ -374,7 +391,7 @@ virCPUDefParseXML(xmlNodePtr node, if ((def->fallback =3D virCPUFallbackTypeFromString(fallback))= < 0) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("Invalid fallback attribute")); - goto error; + goto cleanup; } } =20 @@ -384,14 +401,14 @@ virCPUDefParseXML(xmlNodePtr node, virReportError(VIR_ERR_XML_ERROR, _("vendor_id must be exactly %d characters = long"), VIR_CPU_VENDOR_ID_LENGTH); - goto error; + goto cleanup; } =20 /* ensure that the string can be passed to qemu*/ if (strchr(vendor_id, ',')) { virReportError(VIR_ERR_XML_ERROR, "%s", _("vendor id is invalid")); - goto error; + goto cleanup; } =20 def->vendor_id =3D vendor_id; @@ -403,61 +420,54 @@ virCPUDefParseXML(xmlNodePtr node, if (def->vendor && !def->model) { virReportError(VIR_ERR_XML_ERROR, "%s", _("CPU vendor specified without CPU model")); - goto error; + goto cleanup; } =20 if (virXPathNode("./topology[1]", ctxt)) { - int ret; unsigned long ul; =20 - ret =3D virXPathULong("string(./topology[1]/@sockets)", - ctxt, &ul); - if (ret < 0) { + if (virXPathULong("string(./topology[1]/@sockets)", ctxt, &ul) < 0= ) { virReportError(VIR_ERR_XML_ERROR, "%s", _("Missing 'sockets' attribute in CPU topology"= )); - goto error; + goto cleanup; } def->sockets =3D (unsigned int) ul; =20 - ret =3D virXPathULong("string(./topology[1]/@cores)", - ctxt, &ul); - if (ret < 0) { + if (virXPathULong("string(./topology[1]/@cores)", ctxt, &ul) < 0) { virReportError(VIR_ERR_XML_ERROR, "%s", _("Missing 'cores' attribute in CPU topology")); - goto error; + goto cleanup; } def->cores =3D (unsigned int) ul; =20 - ret =3D virXPathULong("string(./topology[1]/@threads)", - ctxt, &ul); - if (ret < 0) { + if (virXPathULong("string(./topology[1]/@threads)", ctxt, &ul) < 0= ) { virReportError(VIR_ERR_XML_ERROR, "%s", _("Missing 'threads' attribute in CPU topology"= )); - goto error; + goto cleanup; } def->threads =3D (unsigned int) ul; =20 if (!def->sockets || !def->cores || !def->threads) { virReportError(VIR_ERR_XML_ERROR, "%s", _("Invalid CPU topology")); - goto error; + goto cleanup; } } =20 if ((n =3D virXPathNodeSet("./feature", ctxt, &nodes)) < 0) - goto error; + goto cleanup; =20 if (n > 0) { if (!def->model && def->mode =3D=3D VIR_CPU_MODE_CUSTOM) { virReportError(VIR_ERR_XML_ERROR, "%s", _("Non-empty feature list specified without " "CPU model")); - goto error; + goto cleanup; } =20 if (VIR_RESIZE_N(def->features, def->nfeatures_max, def->nfeatures, n) < 0) - goto error; + goto cleanup; =20 def->nfeatures =3D n; } @@ -480,7 +490,7 @@ virCPUDefParseXML(xmlNodePtr node, if (policy < 0) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("Invalid CPU feature policy")); - goto error; + goto cleanup; } } else { policy =3D -1; @@ -490,7 +500,7 @@ virCPUDefParseXML(xmlNodePtr node, VIR_FREE(name); virReportError(VIR_ERR_XML_ERROR, "%s", _("Invalid CPU feature name")); - goto error; + goto cleanup; } =20 for (j =3D 0; j < i; j++) { @@ -499,7 +509,7 @@ virCPUDefParseXML(xmlNodePtr node, _("CPU feature '%s' specified more than onc= e"), name); VIR_FREE(name); - goto error; + goto cleanup; } } =20 @@ -542,17 +552,16 @@ virCPUDefParseXML(xmlNodePtr node, def->cache->mode =3D mode; } =20 + VIR_STEAL_PTR(*cpu, def); + ret =3D 0; + cleanup: ctxt->node =3D oldnode; VIR_FREE(fallback); VIR_FREE(vendor_id); VIR_FREE(nodes); - return def; - - error: virCPUDefFree(def); - def =3D NULL; - goto cleanup; + return ret; } =20 =20 diff --git a/src/conf/cpu_conf.h b/src/conf/cpu_conf.h index b0d891552..b44974f47 100644 --- a/src/conf/cpu_conf.h +++ b/src/conf/cpu_conf.h @@ -182,10 +182,11 @@ virCPUDefCopy(const virCPUDef *cpu); virCPUDefPtr virCPUDefCopyWithoutModel(const virCPUDef *cpu); =20 -virCPUDefPtr -virCPUDefParseXML(xmlNodePtr node, - xmlXPathContextPtr ctxt, - virCPUType mode); +int +virCPUDefParseXML(xmlXPathContextPtr ctxt, + const char *xpath, + virCPUType mode, + virCPUDefPtr *cpu); =20 bool virCPUDefIsEqual(virCPUDefPtr src, diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index a61d293c9..7144914cf 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -17444,16 +17444,8 @@ virDomainDefParseXML(xmlDocPtr xml, } VIR_FREE(nodes); =20 - /* analysis of cpu handling */ - if ((node =3D virXPathNode("./cpu[1]", ctxt)) !=3D NULL) { - xmlNodePtr oldnode =3D ctxt->node; - ctxt->node =3D node; - def->cpu =3D virCPUDefParseXML(node, ctxt, VIR_CPU_TYPE_GUEST); - ctxt->node =3D oldnode; - - if (def->cpu =3D=3D NULL) - goto error; - } + if (virCPUDefParseXML(ctxt, "./cpu[1]", VIR_CPU_TYPE_GUEST, &def->cpu)= < 0) + goto error; =20 if (virDomainNumaDefCPUParseXML(def->numa, ctxt) < 0) goto error; diff --git a/src/cpu/cpu.c b/src/cpu/cpu.c index 702b14dbb..96160901e 100644 --- a/src/cpu/cpu.c +++ b/src/cpu/cpu.c @@ -130,7 +130,7 @@ virCPUCompareXML(virArch arch, if (!(doc =3D virXMLParseStringCtxt(xml, _("(CPU_definition)"), &ctxt)= )) goto cleanup; =20 - if (!(cpu =3D virCPUDefParseXML(ctxt->node, ctxt, VIR_CPU_TYPE_AUTO))) + if (virCPUDefParseXML(ctxt, NULL, VIR_CPU_TYPE_AUTO, &cpu) < 0) goto cleanup; =20 ret =3D virCPUCompare(arch, host, cpu, failIncompatible); @@ -562,8 +562,7 @@ cpuBaselineXML(const char **xmlCPUs, if (!(doc =3D virXMLParseStringCtxt(xmlCPUs[i], _("(CPU_definition= )"), &ctxt))) goto error; =20 - cpus[i] =3D virCPUDefParseXML(ctxt->node, ctxt, VIR_CPU_TYPE_HOST); - if (cpus[i] =3D=3D NULL) + if (virCPUDefParseXML(ctxt, NULL, VIR_CPU_TYPE_HOST, &cpus[i]) < 0) goto error; =20 xmlXPathFreeContext(ctxt); diff --git a/tests/cputest.c b/tests/cputest.c index d5e023c40..89c645e64 100644 --- a/tests/cputest.c +++ b/tests/cputest.c @@ -88,7 +88,7 @@ cpuTestLoadXML(virArch arch, const char *name) if (!(doc =3D virXMLParseFileCtxt(xml, &ctxt))) goto cleanup; =20 - cpu =3D virCPUDefParseXML(ctxt->node, ctxt, VIR_CPU_TYPE_AUTO); + virCPUDefParseXML(ctxt, NULL, VIR_CPU_TYPE_AUTO, &cpu); =20 cleanup: xmlXPathFreeContext(ctxt); @@ -126,8 +126,7 @@ cpuTestLoadMultiXML(virArch arch, =20 for (i =3D 0; i < n; i++) { ctxt->node =3D nodes[i]; - cpus[i] =3D virCPUDefParseXML(nodes[i], ctxt, VIR_CPU_TYPE_HOST); - if (!cpus[i]) + if (virCPUDefParseXML(ctxt, NULL, VIR_CPU_TYPE_HOST, &cpus[i]) < 0) goto cleanup_cpus; } =20 --=20 2.13.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list