[PATCH 05/23] conf: factor out virDomainBlockIoTuneValidate

Nikolay Shirokovskiy posted 23 patches 5 years ago
[PATCH 05/23] conf: factor out virDomainBlockIoTuneValidate
Posted by Nikolay Shirokovskiy 5 years ago
virDomainBlockIoTuneValidate can be reused in virDomainSetBlockIoTune
implementations.

And also simplify if conditions.

Signed-off-by: Nikolay Shirokovskiy <nshirokovskiy@virtuozzo.com>
---
 src/conf/domain_conf.c   | 78 +++++++++++++++++++++++++-----------------------
 src/conf/domain_conf.h   |  3 ++
 src/libvirt_private.syms |  1 +
 3 files changed, 44 insertions(+), 38 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 349fc28..173424a 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -8627,6 +8627,45 @@ virDomainDiskBackingStoreParse(xmlXPathContextPtr ctxt,
     return 0;
 }
 
+
+int
+virDomainBlockIoTuneValidate(virDomainBlockIoTuneInfoPtr iotune)
+{
+    if (iotune->total_bytes_sec &&
+        (iotune->read_bytes_sec || iotune->write_bytes_sec)) {
+        virReportError(VIR_ERR_XML_ERROR, "%s",
+                       _("total and read/write bytes_sec "
+                         "cannot be set at the same time"));
+        return -1;
+    }
+
+    if (iotune->total_iops_sec &&
+        (iotune->read_iops_sec || iotune->write_iops_sec)) {
+        virReportError(VIR_ERR_XML_ERROR, "%s",
+                       _("total and read/write iops_sec "
+                         "cannot be set at the same time"));
+        return -1;
+    }
+
+    if (iotune->total_bytes_sec_max &&
+        (iotune->read_bytes_sec_max || iotune->write_bytes_sec_max)) {
+        virReportError(VIR_ERR_XML_ERROR, "%s",
+                       _("total and read/write bytes_sec_max "
+                         "cannot be set at the same time"));
+        return -1;
+    }
+
+    if (iotune->total_iops_sec_max &&
+        (iotune->read_iops_sec_max || iotune->write_iops_sec_max)) {
+        virReportError(VIR_ERR_XML_ERROR, "%s",
+                       _("total and read/write iops_sec_max "
+                         "cannot be set at the same time"));
+        return -1;
+    }
+
+    return 0;
+}
+
 #define PARSE_IOTUNE(val) \
     if (virXPathULongLong("string(./iotune/" #val ")", \
                           ctxt, &def->blkdeviotune.val) == -2) { \
@@ -8665,45 +8704,8 @@ virDomainDiskDefIotuneParse(virDomainDiskDefPtr def,
     def->blkdeviotune.group_name =
         virXPathString("string(./iotune/group_name)", ctxt);
 
-    if ((def->blkdeviotune.total_bytes_sec &&
-         def->blkdeviotune.read_bytes_sec) ||
-        (def->blkdeviotune.total_bytes_sec &&
-         def->blkdeviotune.write_bytes_sec)) {
-        virReportError(VIR_ERR_XML_ERROR, "%s",
-                       _("total and read/write bytes_sec "
-                         "cannot be set at the same time"));
-        return -1;
-    }
-
-    if ((def->blkdeviotune.total_iops_sec &&
-         def->blkdeviotune.read_iops_sec) ||
-        (def->blkdeviotune.total_iops_sec &&
-         def->blkdeviotune.write_iops_sec)) {
-        virReportError(VIR_ERR_XML_ERROR, "%s",
-                       _("total and read/write iops_sec "
-                         "cannot be set at the same time"));
-        return -1;
-    }
-
-    if ((def->blkdeviotune.total_bytes_sec_max &&
-         def->blkdeviotune.read_bytes_sec_max) ||
-        (def->blkdeviotune.total_bytes_sec_max &&
-         def->blkdeviotune.write_bytes_sec_max)) {
-        virReportError(VIR_ERR_XML_ERROR, "%s",
-                       _("total and read/write bytes_sec_max "
-                         "cannot be set at the same time"));
+    if (virDomainBlockIoTuneValidate(&def->blkdeviotune) < 0)
         return -1;
-    }
-
-    if ((def->blkdeviotune.total_iops_sec_max &&
-         def->blkdeviotune.read_iops_sec_max) ||
-        (def->blkdeviotune.total_iops_sec_max &&
-         def->blkdeviotune.write_iops_sec_max)) {
-        virReportError(VIR_ERR_XML_ERROR, "%s",
-                       _("total and read/write iops_sec_max "
-                         "cannot be set at the same time"));
-        return -1;
-    }
 
     return 0;
 }
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index ec43bbe..3c42313 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -3963,3 +3963,6 @@ virHostdevIsMdevDevice(const virDomainHostdevDef *hostdev)
 bool
 virHostdevIsVFIODevice(const virDomainHostdevDef *hostdev)
     ATTRIBUTE_NONNULL(1);
+
+int
+virDomainBlockIoTuneValidate(virDomainBlockIoTuneInfoPtr iotune);
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index c325040..bfe3ee7 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -236,6 +236,7 @@ virDomainBlockIoTuneInfoHasAny;
 virDomainBlockIoTuneInfoHasBasic;
 virDomainBlockIoTuneInfoHasMax;
 virDomainBlockIoTuneInfoHasMaxLength;
+virDomainBlockIoTuneValidate;
 virDomainBootTypeFromString;
 virDomainBootTypeToString;
 virDomainCapabilitiesPolicyTypeToString;
-- 
1.8.3.1

Re: [PATCH 05/23] conf: factor out virDomainBlockIoTuneValidate
Posted by Peter Krempa 4 years, 11 months ago
On Mon, Jan 11, 2021 at 12:49:58 +0300, Nikolay Shirokovskiy wrote:
> virDomainBlockIoTuneValidate can be reused in virDomainSetBlockIoTune
> implementations.
> 
> And also simplify if conditions.
> 
> Signed-off-by: Nikolay Shirokovskiy <nshirokovskiy@virtuozzo.com>
> ---
>  src/conf/domain_conf.c   | 78 +++++++++++++++++++++++++-----------------------
>  src/conf/domain_conf.h   |  3 ++
>  src/libvirt_private.syms |  1 +
>  3 files changed, 44 insertions(+), 38 deletions(-)
> 
> diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
> index 349fc28..173424a 100644
> --- a/src/conf/domain_conf.c
> +++ b/src/conf/domain_conf.c

[...]

> +
>  #define PARSE_IOTUNE(val) \
>      if (virXPathULongLong("string(./iotune/" #val ")", \
>                            ctxt, &def->blkdeviotune.val) == -2) { \
> @@ -8665,45 +8704,8 @@ virDomainDiskDefIotuneParse(virDomainDiskDefPtr def,

[...]

> -        virReportError(VIR_ERR_XML_ERROR, "%s",
> -                       _("total and read/write bytes_sec_max "
> -                         "cannot be set at the same time"));
> +    if (virDomainBlockIoTuneValidate(&def->blkdeviotune) < 0)
>          return -1;

This should be called from 'virDomainDiskDefValidate' rather than from
the parser.

With that:

Reviewed-by: Peter Krempa <pkrempa@redhat.com>