From nobody Sun Feb 8 17:46:49 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) client-ip=205.139.110.61; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1593006572; cv=none; d=zohomail.com; s=zohoarc; b=bB0iXhGPi8U6h7KJ0VJJgnLOQx3yb3AG6Zm5H4K+eHDFi1OoAnjMn/t4OBbOLWwJRpHg5eJrpXsf/09VU7E4GM3+BroRUHO7oB4BJ0lJWrWi2xBrLPHn/isE+jH4vkEFpUjT0BO1o9jxiULMB+t66rh8J2DMg1QiW6yCpMogJWs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1593006572; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=kIKcgoYXb3JLfc1M7uN0PyjSUO/9nON2TdvbcvpzND4=; b=ZOhaQw5bhjT+Pz4C0I9EpO3SErhFCJSX+cDG2MASySZr+Uk4xu+nfckVTZUR6HOv64mrYhtnI28JsLypF7SSLMjLVF3rQen/qwK/a7LwmLtNSPH1+ivgsUfLsvTK7jA8IOPVn+R64TnBfOTUjL6ry109Lz70Xdatuf4+zaUsJjo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) by mx.zohomail.com with SMTPS id 1593006572653485.08195685135956; Wed, 24 Jun 2020 06:49:32 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-402-dLSIFOx8M1Kz-Wibf6g08A-1; Wed, 24 Jun 2020 09:49:28 -0400 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 mimecast-mx01.redhat.com (Postfix) with ESMTPS id 2EB3E804003; Wed, 24 Jun 2020 13:49:23 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C61165C541; Wed, 24 Jun 2020 13:49:22 +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 776F61048FD; Wed, 24 Jun 2020 13:49:19 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 05ODnII3015643 for ; Wed, 24 Jun 2020 09:49:18 -0400 Received: by smtp.corp.redhat.com (Postfix) id 78E975DA27; Wed, 24 Jun 2020 13:49:18 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.195.7]) by smtp.corp.redhat.com (Postfix) with ESMTP id EE2105D9DC for ; Wed, 24 Jun 2020 13:49:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1593006571; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=kIKcgoYXb3JLfc1M7uN0PyjSUO/9nON2TdvbcvpzND4=; b=CHzyRLdDAZrTwvrGrosH11dI/Zh9Obs8B1gPROstM7uO67xYQ9W+vFbvZFb+ntt06ni2Yo eBqPLWW0j8Bvh1jN+TCy+AEMaejb6610CmoFoWIrHxESZp+keFyrxhj2z+5osDCgCwkODZ KezBRHpOqGm67/eS+Z0yPaNhXMQH61U= X-MC-Unique: dLSIFOx8M1Kz-Wibf6g08A-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 02/13] conf: Move and rename virDomainParseScaledValue() Date: Wed, 24 Jun 2020 15:49:00 +0200 Message-Id: <609934c6984da0ff1ee9ae136a5d77a1341777f3.1593006346.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com 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: , 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-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" There is nothing domain specific about the function, thus it should not have virDomain prefix. Also, the fact that it is a static function makes it impossible to use from other files. Move the function to virxml.c and drop the 'Domain' infix. Signed-off-by: Michal Privoznik Reviewed-by: Daniel Henrique Barboza --- src/conf/domain_conf.c | 135 ++++++++++----------------------------- src/libvirt_private.syms | 1 + src/util/virxml.c | 72 +++++++++++++++++++++ src/util/virxml.h | 8 +++ 4 files changed, 114 insertions(+), 102 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index fc7fcfb0c6..65a110b16f 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -10808,75 +10808,6 @@ virDomainDiskDefParseXML(virDomainXMLOptionPtr xml= opt, goto cleanup; } =20 -/** - * virDomainParseScaledValue: - * @xpath: XPath to memory amount - * @units_xpath: XPath to units attribute - * @ctxt: XPath context - * @val: scaled value is stored here - * @scale: default scale for @val - * @max: maximal @val allowed - * @required: is the value required? - * - * Parse a value located at @xpath within @ctxt, and store the - * result into @val. The value is scaled by units located at - * @units_xpath (or the 'unit' attribute under @xpath if - * @units_xpath is NULL). If units are not present, the default - * @scale is used. If @required is set, then the value must - * exist; otherwise, the value is optional. The resulting value - * is in bytes. - * - * Returns 1 on success, - * 0 if the value was not present and !@required, - * -1 on failure after issuing error. - */ -static int -virDomainParseScaledValue(const char *xpath, - const char *units_xpath, - xmlXPathContextPtr ctxt, - unsigned long long *val, - unsigned long long scale, - unsigned long long max, - bool required) -{ - unsigned long long bytes; - g_autofree char *xpath_full =3D NULL; - g_autofree char *unit =3D NULL; - g_autofree char *bytes_str =3D NULL; - - *val =3D 0; - xpath_full =3D g_strdup_printf("string(%s)", xpath); - - bytes_str =3D virXPathString(xpath_full, ctxt); - if (!bytes_str) { - if (!required) - return 0; - virReportError(VIR_ERR_XML_ERROR, - _("missing element or attribute '%s'"), - xpath); - return -1; - } - VIR_FREE(xpath_full); - - if (virStrToLong_ullp(bytes_str, NULL, 10, &bytes) < 0) { - virReportError(VIR_ERR_XML_ERROR, - _("Invalid value '%s' for element or attribute '%s'= "), - bytes_str, xpath); - return -1; - } - - if (units_xpath) - xpath_full =3D g_strdup_printf("string(%s)", units_xpath); - else - xpath_full =3D g_strdup_printf("string(%s/@unit)", xpath); - unit =3D virXPathString(xpath_full, ctxt); - - if (virScaleInteger(&bytes, unit, scale, max) < 0) - return -1; - - *val =3D bytes; - return 1; -} =20 =20 /** @@ -10913,8 +10844,8 @@ virDomainParseMemory(const char *xpath, =20 max =3D virMemoryMaxValue(capped); =20 - if (virDomainParseScaledValue(xpath, units_xpath, ctxt, - &bytes, 1024, max, required) < 0) + if (virParseScaledValue(xpath, units_xpath, ctxt, + &bytes, 1024, max, required) < 0) return -1; =20 /* Yes, we really do use kibibytes for our internal sizing. */ @@ -10956,9 +10887,9 @@ virDomainParseMemoryLimit(const char *xpath, int ret; unsigned long long bytes; =20 - ret =3D virDomainParseScaledValue(xpath, units_xpath, ctxt, &bytes, 10= 24, - VIR_DOMAIN_MEMORY_PARAM_UNLIMITED << 1= 0, - false); + ret =3D virParseScaledValue(xpath, units_xpath, ctxt, &bytes, 1024, + VIR_DOMAIN_MEMORY_PARAM_UNLIMITED << 10, + false); =20 if (ret < 0) return -1; @@ -11286,9 +11217,9 @@ virDomainControllerDefParseXML(virDomainXMLOptionPt= r xmlopt, "have an address")); goto error; } - if ((rc =3D virDomainParseScaledValue("./pcihole64", NULL, - ctxt, &bytes, 1024, - 1024ULL * ULONG_MAX, false= )) < 0) + if ((rc =3D virParseScaledValue("./pcihole64", NULL, + ctxt, &bytes, 1024, + 1024ULL * ULONG_MAX, false)) < 0) goto error; =20 if (rc =3D=3D 1) @@ -11529,14 +11460,14 @@ virDomainFSDefParseXML(virDomainXMLOptionPtr xmlo= pt, def->multidevs =3D VIR_DOMAIN_FS_MULTIDEVS_DEFAULT; } =20 - if (virDomainParseScaledValue("./space_hard_limit[1]", - NULL, ctxt, &def->space_hard_limit, - 1, ULLONG_MAX, false) < 0) + if (virParseScaledValue("./space_hard_limit[1]", + NULL, ctxt, &def->space_hard_limit, + 1, ULLONG_MAX, false) < 0) goto error; =20 - if (virDomainParseScaledValue("./space_soft_limit[1]", - NULL, ctxt, &def->space_soft_limit, - 1, ULLONG_MAX, false) < 0) + if (virParseScaledValue("./space_soft_limit[1]", + NULL, ctxt, &def->space_soft_limit, + 1, ULLONG_MAX, false) < 0) goto error; =20 cur =3D node->children; @@ -15388,8 +15319,8 @@ virDomainShmemDefParseXML(virDomainXMLOptionPtr xml= opt, goto cleanup; } =20 - if (virDomainParseScaledValue("./size[1]", NULL, ctxt, - &def->size, 1, ULLONG_MAX, false) < 0) + if (virParseScaledValue("./size[1]", NULL, ctxt, + &def->size, 1, ULLONG_MAX, false) < 0) goto cleanup; =20 if ((server =3D virXPathNode("./server[1]", ctxt))) { @@ -19432,13 +19363,13 @@ virDomainFeaturesDefParse(virDomainDefPtr def, VIR_FREE(tmp); } =20 - if (virDomainParseScaledValue("./features/hpt/maxpagesize", - NULL, - ctxt, - &def->hpt_maxpagesize, - 1024, - ULLONG_MAX, - false) < 0) { + if (virParseScaledValue("./features/hpt/maxpagesize", + NULL, + ctxt, + &def->hpt_maxpagesize, + 1024, + ULLONG_MAX, + false) < 0) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("Unable to parse HPT maxpagesize setting"= )); @@ -19782,13 +19713,13 @@ virDomainFeaturesDefParse(virDomainDefPtr def, } =20 if (def->features[VIR_DOMAIN_FEATURE_SMM] =3D=3D VIR_TRISTATE_SWITCH_O= N) { - int rv =3D virDomainParseScaledValue("string(./features/smm/tseg)", - "string(./features/smm/tseg/@un= it)", - ctxt, - &def->tseg_size, - 1024 * 1024, /* Defaults to meb= ibytes */ - ULLONG_MAX, - false); + int rv =3D virParseScaledValue("string(./features/smm/tseg)", + "string(./features/smm/tseg/@unit)", + ctxt, + &def->tseg_size, + 1024 * 1024, /* Defaults to mebibytes= */ + ULLONG_MAX, + false); if (rv < 0) goto error; def->tseg_specified =3D rv; @@ -20510,9 +20441,9 @@ virDomainCachetuneDefParseCache(xmlXPathContextPtr = ctxt, return -1; } =20 - if (virDomainParseScaledValue("./@size", "./@unit", - ctxt, &size, 1024, - ULLONG_MAX, true) < 0) + if (virParseScaledValue("./@size", "./@unit", + ctxt, &size, 1024, + ULLONG_MAX, true) < 0) return -1; =20 if (virResctrlAllocSetCacheSize(alloc, level, type, cache, size) < 0) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 487eea4149..9f2abc1d8d 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3484,6 +3484,7 @@ virVsockSetGuestCid; =20 =20 # util/virxml.h +virParseScaledValue; virXMLCheckIllegalChars; virXMLChildElementCount; virXMLExtractNamespaceXML; diff --git a/src/util/virxml.c b/src/util/virxml.c index 02b59ea2f8..e9429cd4ac 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -32,6 +32,7 @@ #include "viralloc.h" #include "virfile.h" #include "virstring.h" +#include "virutil.h" =20 #define VIR_FROM_THIS VIR_FROM_XML =20 @@ -1405,3 +1406,74 @@ virXMLNamespaceRegister(xmlXPathContextPtr ctxt, =20 return 0; } + + +/** + * virParseScaledValue: + * @xpath: XPath to memory amount + * @units_xpath: XPath to units attribute + * @ctxt: XPath context + * @val: scaled value is stored here + * @scale: default scale for @val + * @max: maximal @val allowed + * @required: is the value required? + * + * Parse a value located at @xpath within @ctxt, and store the + * result into @val. The value is scaled by units located at + * @units_xpath (or the 'unit' attribute under @xpath if + * @units_xpath is NULL). If units are not present, the default + * @scale is used. If @required is set, then the value must + * exist; otherwise, the value is optional. The resulting value + * is in bytes. + * + * Returns 1 on success, + * 0 if the value was not present and !@required, + * -1 on failure after issuing error. + */ +int +virParseScaledValue(const char *xpath, + const char *units_xpath, + xmlXPathContextPtr ctxt, + unsigned long long *val, + unsigned long long scale, + unsigned long long max, + bool required) +{ + unsigned long long bytes; + g_autofree char *xpath_full =3D NULL; + g_autofree char *unit =3D NULL; + g_autofree char *bytes_str =3D NULL; + + *val =3D 0; + xpath_full =3D g_strdup_printf("string(%s)", xpath); + + bytes_str =3D virXPathString(xpath_full, ctxt); + if (!bytes_str) { + if (!required) + return 0; + virReportError(VIR_ERR_XML_ERROR, + _("missing element or attribute '%s'"), + xpath); + return -1; + } + VIR_FREE(xpath_full); + + if (virStrToLong_ullp(bytes_str, NULL, 10, &bytes) < 0) { + virReportError(VIR_ERR_XML_ERROR, + _("Invalid value '%s' for element or attribute '%s'= "), + bytes_str, xpath); + return -1; + } + + if (units_xpath) + xpath_full =3D g_strdup_printf("string(%s)", units_xpath); + else + xpath_full =3D g_strdup_printf("string(%s/@unit)", xpath); + unit =3D virXPathString(xpath_full, ctxt); + + if (virScaleInteger(&bytes, unit, scale, max) < 0) + return -1; + + *val =3D bytes; + return 1; +} diff --git a/src/util/virxml.h b/src/util/virxml.h index ed178105f6..886d1e025f 100644 --- a/src/util/virxml.h +++ b/src/util/virxml.h @@ -270,3 +270,11 @@ virXMLNamespaceFormatNS(virBufferPtr buf, int virXMLNamespaceRegister(xmlXPathContextPtr ctxt, virXMLNamespace const *ns); + +int virParseScaledValue(const char *xpath, + const char *units_xpath, + xmlXPathContextPtr ctxt, + unsigned long long *val, + unsigned long long scale, + unsigned long long max, + bool required); --=20 2.26.2