[PATCHv2 3/6] virnetdevovs: Extract common code block to a single function

jx8zjs jx8zjs at 126.com
Tue Aug 17 04:38:13 UTC 2021


From: Jinsheng Zhang <zhangjl02 at inspur.com>

Signed-off-by: zhangjl02 <zhangjl02 at inspur.com>

s

Signed-off-by: zhangjl02 <zhangjl02 at inspur.com>

s

Signed-off-by: zhangjl02 <zhangjl02 at inspur.com>
---
 src/util/virnetdevopenvswitch.c | 60 ++++++++++++++-------------------
 1 file changed, 26 insertions(+), 34 deletions(-)

diff --git a/src/util/virnetdevopenvswitch.c b/src/util/virnetdevopenvswitch.c
index dfbebf8535..e8ec06d3db 100644
--- a/src/util/virnetdevopenvswitch.c
+++ b/src/util/virnetdevopenvswitch.c
@@ -619,6 +619,23 @@ int virNetDevOpenvswitchUpdateVlan(const char *ifname,
     return 0;
 }
 
+static char*
+virNetDevOpenvswitchFindUUID(const char *table,
+                             const char *vmid_ex_id,
+                             const char *ifname_ex_id)
+{
+    g_autoptr(virCommand) cmd = NULL;
+    char *uuid = NULL;
+
+    cmd = virNetDevOpenvswitchCreateCmd();
+    virCommandAddArgList(cmd, "--no-heading", "--columns=_uuid", "find", table,
+                         vmid_ex_id, ifname_ex_id, NULL);
+    virCommandSetOutputBuffer(cmd, &uuid);
+    if (virCommandRun(cmd, NULL) < 0) {
+        VIR_WARN("Unable to find queue on port with %s", ifname_ex_id);
+    }
+    return uuid;
+}
 
 /**
  * virNetDevOpenvswitchInterfaceSetQos:
@@ -698,30 +715,15 @@ virNetDevOpenvswitchInterfaceSetQos(const char *ifname,
         if (tx->peak)
             peak = g_strdup_printf("%llu", tx->peak * VIR_NETDEV_TX_TO_OVS);
 
-        /* find queue */
-        cmd = virNetDevOpenvswitchCreateCmd();
         virUUIDFormat(vmuuid, vmuuidstr);
         vmid_ex_id = g_strdup_printf("external-ids:vm-id=\"%s\"", vmuuidstr);
         ifname_ex_id = g_strdup_printf("external-ids:ifname=\"%s\"", ifname);
-        virCommandAddArgList(cmd, "--no-heading", "--columns=_uuid", "find", "queue",
-                             vmid_ex_id, ifname_ex_id, NULL);
-        virCommandSetOutputBuffer(cmd, &queue_uuid);
-        if (virCommandRun(cmd, NULL) < 0) {
-            VIR_WARN("Unable to find queue on port %s", ifname);
-        }
-
+        /* find queue */
+        queue_uuid = virNetDevOpenvswitchFindUUID("queue", vmid_ex_id, ifname_ex_id);
         /* find qos */
-        virCommandFree(cmd);
-        cmd = virNetDevOpenvswitchCreateCmd();
-        virCommandAddArgList(cmd, "--no-heading", "--columns=_uuid", "find", "qos",
-                             vmid_ex_id, ifname_ex_id, NULL);
-        virCommandSetOutputBuffer(cmd, &qos_uuid);
-        if (virCommandRun(cmd, NULL) < 0) {
-            VIR_WARN("Unable to find qos on port %s", ifname);
-        }
+        qos_uuid = virNetDevOpenvswitchFindUUID("qos", vmid_ex_id, ifname_ex_id);
 
         /* create qos and set */
-        virCommandFree(cmd);
         cmd = virNetDevOpenvswitchCreateCmd();
         if (queue_uuid && *queue_uuid) {
             g_auto(GStrv) lines = g_strsplit(queue_uuid, "\n", 0);
@@ -808,6 +810,7 @@ virNetDevOpenvswitchInterfaceClearQos(const char *ifname,
 {
     char vmuuidstr[VIR_UUID_STRING_BUFLEN];
     g_autoptr(virCommand) cmd = NULL;
+    g_autofree char *ifname_ex_id = NULL;
     g_autofree char *vmid_ex_id = NULL;
     g_autofree char *qos_uuid = NULL;
     g_autofree char *queue_uuid = NULL;
@@ -815,24 +818,13 @@ virNetDevOpenvswitchInterfaceClearQos(const char *ifname,
     size_t i;
 
     /* find qos */
-    cmd = virNetDevOpenvswitchCreateCmd();
     virUUIDFormat(vmuuid, vmuuidstr);
     vmid_ex_id = g_strdup_printf("external-ids:vm-id=\"%s\"", vmuuidstr);
-    virCommandAddArgList(cmd, "--no-heading", "--columns=_uuid", "find", "qos", vmid_ex_id, NULL);
-    virCommandSetOutputBuffer(cmd, &qos_uuid);
-    if (virCommandRun(cmd, NULL) < 0) {
-        VIR_WARN("Unable to find qos on port %s", ifname);
-    }
-
+    ifname_ex_id = g_strdup_printf("external-ids:ifname=\"%s\"", ifname);
     /* find queue */
-    virCommandFree(cmd);
-    cmd = virNetDevOpenvswitchCreateCmd();
-    vmid_ex_id = g_strdup_printf("external-ids:vm-id=\"%s\"", vmuuidstr);
-    virCommandAddArgList(cmd, "--no-heading", "--columns=_uuid", "find", "queue", vmid_ex_id, NULL);
-    virCommandSetOutputBuffer(cmd, &queue_uuid);
-    if (virCommandRun(cmd, NULL) < 0) {
-        VIR_WARN("Unable to find queue on port %s", ifname);
-    }
+    queue_uuid = virNetDevOpenvswitchFindUUID("queue", vmid_ex_id, ifname_ex_id);
+    /* find qos */
+    qos_uuid = virNetDevOpenvswitchFindUUID("qos", vmid_ex_id, ifname_ex_id);
 
     if (qos_uuid && *qos_uuid) {
         g_auto(GStrv) lines = g_strsplit(qos_uuid, "\n", 0);
@@ -846,7 +838,7 @@ virNetDevOpenvswitchInterfaceClearQos(const char *ifname,
             virCommandFree(cmd);
             cmd = virNetDevOpenvswitchCreateCmd();
             virCommandAddArgList(cmd, "--no-heading", "--columns=_uuid", "--if-exists",
-                    "list", "port", ifname, "qos", NULL);
+                                 "list", "port", ifname, "qos", NULL);
             virCommandSetOutputBuffer(cmd, &port_qos);
             if (virCommandRun(cmd, NULL) < 0) {
                 VIR_WARN("Unable to remove port qos on port %s", ifname);
-- 
2.30.2.windows.1




More information about the libvir-list mailing list