[PATCH 04/12] virsh: Use virTypedParams* APIs in domiftune

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



---
 tools/virsh-domain.c | 122 ++++++++++++++++++++-------------------------------
 1 file changed, 48 insertions(+), 74 deletions(-)

diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 74ea4b4..10b0a11 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -2093,6 +2093,7 @@ cmdDomIftune(vshControl *ctl, const vshCmd *cmd)
                *inboundStr = NULL, *outboundStr = NULL;
     unsigned int flags = 0;
     int nparams = 0;
+    int maxparams = 0;
     virTypedParameterPtr params = NULL;
     bool ret = false;
     bool current = vshCommandOptBool(cmd, "current");
@@ -2117,10 +2118,8 @@ cmdDomIftune(vshControl *ctl, const vshCmd *cmd)
     if (!(dom = vshCommandOptDomain(ctl, cmd, &name)))
         return false;
 
-    if (vshCommandOptString(cmd, "interface", &device) <= 0) {
-        virDomainFree(dom);
-        return false;
-    }
+    if (vshCommandOptString(cmd, "interface", &device) <= 0)
+        goto cleanup;
 
     if (vshCommandOptString(cmd, "inbound", &inboundStr) < 0 ||
         vshCommandOptString(cmd, "outbound", &outboundStr) < 0) {
@@ -2140,10 +2139,25 @@ cmdDomIftune(vshControl *ctl, const vshCmd *cmd)
             vshError(ctl, _("inbound average is mandatory"));
             goto cleanup;
         }
-        nparams++; /* average */
-        if (inbound.peak) nparams++;
-        if (inbound.burst) nparams++;
+
+        if (virTypedParamsAddUInt(&params, &nparams, &maxparams,
+                                  VIR_DOMAIN_BANDWIDTH_IN_AVERAGE,
+                                  inbound.average) < 0)
+            goto save_error;
+
+        if (inbound.peak &&
+            virTypedParamsAddUInt(&params, &nparams, &maxparams,
+                                  VIR_DOMAIN_BANDWIDTH_IN_PEAK,
+                                  inbound.peak) < 0)
+            goto save_error;
+
+        if (inbound.burst &&
+            virTypedParamsAddUInt(&params, &nparams, &maxparams,
+                                  VIR_DOMAIN_BANDWIDTH_IN_BURST,
+                                  inbound.burst) < 0)
+            goto save_error;
     }
+
     if (outboundStr) {
         if (parseRateStr(outboundStr, &outbound) < 0) {
             vshError(ctl, _("outbound format is incorrect"));
@@ -2153,9 +2167,23 @@ cmdDomIftune(vshControl *ctl, const vshCmd *cmd)
             vshError(ctl, _("outbound average is mandatory"));
             goto cleanup;
         }
-        nparams++; /* average */
-        if (outbound.peak) nparams++;
-        if (outbound.burst) nparams++;
+
+        if (virTypedParamsAddUInt(&params, &nparams, &maxparams,
+                                  VIR_DOMAIN_BANDWIDTH_OUT_AVERAGE,
+                                  outbound.average) < 0)
+            goto save_error;
+
+        if (outbound.peak &&
+            virTypedParamsAddUInt(&params, &nparams, &maxparams,
+                                  VIR_DOMAIN_BANDWIDTH_OUT_PEAK,
+                                  outbound.peak) < 0)
+            goto save_error;
+
+        if (outbound.burst &&
+            virTypedParamsAddUInt(&params, &nparams, &maxparams,
+                                  VIR_DOMAIN_BANDWIDTH_OUT_BURST,
+                                  outbound.burst) < 0)
+            goto save_error;
     }
 
     if (nparams == 0) {
@@ -2174,10 +2202,6 @@ cmdDomIftune(vshControl *ctl, const vshCmd *cmd)
 
         /* get all interface parameters */
         params = vshCalloc(ctl, nparams, sizeof(*params));
-        if (!params) {
-            virReportOOMError();
-            goto cleanup;
-        }
         if (virDomainGetInterfaceParameters(dom, device, params, &nparams, flags) != 0) {
             vshError(ctl, "%s", _("Unable to get interface parameters"));
             goto cleanup;
@@ -2189,73 +2213,23 @@ cmdDomIftune(vshControl *ctl, const vshCmd *cmd)
             VIR_FREE(str);
         }
     } else {
-        /* set the interface parameters */
-        params = vshCalloc(ctl, nparams, sizeof(*params));
-        if (!params) {
-            virReportOOMError();
-            goto cleanup;
-        }
-
-        for (i = 0; i < nparams; i++)
-            params[i].type = VIR_TYPED_PARAM_UINT;
-
-        i = 0;
-        if (inbound.average && i < nparams) {
-            if (!virStrcpy(params[i].field, VIR_DOMAIN_BANDWIDTH_IN_AVERAGE,
-                           sizeof(params[i].field)))
-                goto cleanup;
-            params[i].value.ui = inbound.average;
-            i++;
-        }
-        if (inbound.peak && i < nparams) {
-            if (!virStrcpy(params[i].field, VIR_DOMAIN_BANDWIDTH_IN_PEAK,
-                           sizeof(params[i].field)))
-                goto cleanup;
-            params[i].value.ui = inbound.peak;
-            i++;
-        }
-        if (inbound.burst && i < nparams) {
-            if (!virStrcpy(params[i].field, VIR_DOMAIN_BANDWIDTH_IN_BURST,
-                           sizeof(params[i].field)))
-                goto cleanup;
-            params[i].value.ui = inbound.burst;
-            i++;
-        }
-        if (outbound.average && i < nparams) {
-            if (!virStrcpy(params[i].field, VIR_DOMAIN_BANDWIDTH_OUT_AVERAGE,
-                           sizeof(params[i].field)))
-                goto cleanup;
-            params[i].value.ui = outbound.average;
-            i++;
-        }
-        if (outbound.peak && i < nparams) {
-            if (!virStrcpy(params[i].field, VIR_DOMAIN_BANDWIDTH_OUT_PEAK,
-                           sizeof(params[i].field)))
-                goto cleanup;
-            params[i].value.ui = outbound.peak;
-            i++;
-        }
-        if (outbound.burst && i < nparams) {
-            if (!virStrcpy(params[i].field, VIR_DOMAIN_BANDWIDTH_OUT_BURST,
-                           sizeof(params[i].field)))
-                goto cleanup;
-            params[i].value.ui = outbound.burst;
-            i++;
-        }
-
-        if (virDomainSetInterfaceParameters(dom, device, params, nparams, flags) != 0) {
-            vshError(ctl, "%s", _("Unable to set interface parameters"));
-            goto cleanup;
-        }
+        if (virDomainSetInterfaceParameters(dom, device, params,
+                                            nparams, flags) != 0)
+            goto error;
     }
 
     ret = true;
 
 cleanup:
-    virTypedParameterArrayClear(params, nparams);
-    VIR_FREE(params);
+    virTypedParamsFree(params, nparams);
     virDomainFree(dom);
     return ret;
+
+save_error:
+    vshSaveLibvirtError();
+error:
+    vshError(ctl, "%s", _("Unable to set interface parameters"));
+    goto cleanup;
 }
 
 /*
-- 
1.8.1.1

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]