[libvirt PATCH v3 4/5] qemu: add guest interface information in 'qemuDomainGetGuestInfo'

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

 



Support return guest interface information from guest agent

Signed-off-by: zhanglei <zhanglei@xxxxxxxxxx>
---
 src/qemu/qemu_driver.c | 88 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 87 insertions(+), 1 deletion(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 5071faab5e..38a12d18b6 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -19896,7 +19896,8 @@ static const unsigned int qemuDomainGetGuestInfoSupportedTypes =
     VIR_DOMAIN_GUEST_INFO_TIMEZONE |
     VIR_DOMAIN_GUEST_INFO_HOSTNAME |
     VIR_DOMAIN_GUEST_INFO_FILESYSTEM |
-    VIR_DOMAIN_GUEST_INFO_DISKS;
+    VIR_DOMAIN_GUEST_INFO_DISKS |
+    VIR_DOMAIN_GUEST_INFO_INTERFACES;
 
 static int
 qemuDomainGetGuestInfoCheckSupport(unsigned int types,
@@ -20095,6 +20096,71 @@ qemuAgentFSInfoFormatParams(qemuAgentFSInfo **fsinfo,
     }
 }
 
+static void
+virDomainInterfaceFormatParams(virDomainInterfacePtr *ifaces,
+                               int nifaces,
+                               virTypedParameterPtr *params,
+                               int *nparams, int * maxparams)
+{
+    size_t i;
+    size_t j;
+    const char *type = NULL;
+
+    if (virTypedParamsAddUInt(params, nparams, maxparams,
+                             "if.count", nifaces) < 0)
+        return;
+
+    for (i = 0; i < nifaces; i++) {
+        char param_name[VIR_TYPED_PARAM_FIELD_LENGTH];
+
+        g_snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH,
+                   "if.%zu.name", i);
+        if (virTypedParamsAddString(params, nparams, maxparams,
+                                    param_name, ifaces[i]->name) < 0)
+            return;
+
+        g_snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH,
+                   "if.%zu.hwaddr", i);
+        if (virTypedParamsAddString(params, nparams, maxparams,
+                                    param_name, ifaces[i]->hwaddr) < 0)
+            return;
+
+        g_snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH,
+                   "if.%zu.addr.count", i);
+        if (virTypedParamsAddUInt(params, nparams, maxparams,
+                                  param_name, ifaces[i]->naddrs) < 0)
+            return;
+
+        for (j = 0; j < ifaces[i]->naddrs; j++) {
+            switch (ifaces[i]->addrs[j].type) {
+                case VIR_IP_ADDR_TYPE_IPV4:
+                    type = "ipv4";
+                    break;
+                case VIR_IP_ADDR_TYPE_IPV6:
+                    type = "ipv6";
+                    break;
+            }
+
+            g_snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH,
+                       "if.%zu.addr.%zu.type", i, j);
+            if (virTypedParamsAddString(params, nparams, maxparams,
+                                        param_name, type) < 0)
+            return;
+
+            g_snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH,
+                       "if.%zu.addr.%zu.addr", i, j);
+            if (virTypedParamsAddString(params, nparams, maxparams,
+                                        param_name, ifaces[i]->addrs[j].addr) < 0)
+            return;
+
+            g_snprintf(param_name, VIR_TYPED_PARAM_FIELD_LENGTH,
+                       "if.%zu.addr.%zu.prefix", i, j);
+            if (virTypedParamsAddUInt(params, nparams, maxparams,
+                                      param_name, ifaces[i]->addrs[j].prefix) < 0)
+            return;
+        }
+    }
+}
 
 static int
 qemuDomainGetGuestInfo(virDomainPtr dom,
@@ -20116,6 +20182,8 @@ qemuDomainGetGuestInfo(virDomainPtr dom,
     qemuAgentFSInfo **agentfsinfo = NULL;
     size_t ndisks = 0;
     qemuAgentDiskInfo **agentdiskinfo = NULL;
+    virDomainInterfacePtr *ifaces = NULL;
+    size_t nifaces = 0;
     size_t i;
 
     virCheckFlags(0, -1);
@@ -20181,6 +20249,15 @@ qemuDomainGetGuestInfo(virDomainPtr dom,
         }
     }
 
+    if (supportedTypes & VIR_DOMAIN_GUEST_INFO_INTERFACES) {
+        rc = qemuAgentGetInterfaces(agent, &ifaces, report_unsupported);
+        if (rc == -1) {
+            goto exitagent;
+        } else if (rc >= 0) {
+            nifaces = rc;
+        }
+    }
+
     qemuDomainObjExitAgent(vm, agent);
     qemuDomainObjEndAgentJob(vm);
 
@@ -20203,6 +20280,10 @@ qemuDomainGetGuestInfo(virDomainPtr dom,
         qemuDomainObjEndJob(driver, vm);
     }
 
+    if (nifaces > 0) {
+        virDomainInterfaceFormatParams(ifaces, nifaces, params, nparams, &maxparams);
+    }
+
     ret = 0;
 
  cleanup:
@@ -20212,6 +20293,11 @@ qemuDomainGetGuestInfo(virDomainPtr dom,
     for (i = 0; i < ndisks; i++)
         qemuAgentDiskInfoFree(agentdiskinfo[i]);
     g_free(agentdiskinfo);
+    if (ifaces && nifaces > 0) {
+        for (i = 0; i < nifaces; i++)
+            virDomainInterfaceFree(ifaces[i]);
+    }
+    g_free(ifaces);
 
     virDomainObjEndAPI(&vm);
     return ret;
-- 
2.31.1




[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]

  Powered by Linux