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

Re: [libvirt] [PATCH 4/5] domifaddr: Add virsh support



On 24/07/13 19:07, nehaljwani wrote:
Use virDomainInterfacesAddresses in virsh

tools/virsh-domain-monitor.c
    * Introduce new command : domifaddr
      Example Usage: domifaddr <domain-name> <interface-name (optional)>

As a habit, we indicate the optional optinon of command like:

domifaddr <domain> [interface]


tools/virsh.pod
    * Document new command

---
  tools/virsh-domain-monitor.c | 103 +++++++++++++++++++++++++++++++++++++++++++
  tools/virsh.pod              |  10 +++++
  2 files changed, 113 insertions(+)

diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index 773f96d..d85dfbd 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -1873,6 +1873,103 @@ cleanup:
  }
  #undef FILTER
+/* "domifaddr" command
+ */
+static const vshCmdInfo info_domifaddr[] = {
+    {"help", N_("Get network interfaces addresses for a running domain")},

not sure if it should be interfaces' instead of interfaces.

+    {"desc", N_("Get network interfaces addresses for a running domain")},
+    {NULL, NULL}
+};
+
+static const vshCmdOptDef opts_domifaddr[] = {
+    {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
+    {"interface", VSH_OT_DATA, VSH_OFLAG_NONE, N_("network interface name")},
+    {NULL, 0, 0, NULL}
+};
+
+static bool
+cmdDomIfAddr(vshControl *ctl, const vshCmd *cmd)
+{
+    virDomainPtr dom = NULL;
+    const char *interface = NULL;
+    virDomainInterfacePtr ifaces = NULL;
+    size_t i, j;
+    unsigned int ifaces_count = 0;
+    unsigned int flags = 0;
+    bool ret = false;
+
+    if (!(dom = vshCommandOptDomain(ctl, cmd, NULL)))
+        return false;
+
[1]

+    if (vshCommandOptString(cmd, "interface", &interface) < 0) {
+        goto cleanup;
+    }
+
+    if (virDomainInterfacesAddresses(dom, &ifaces, &ifaces_count, flags) < 0) {
+        vshError(ctl, _("Failed to query for interfaces addresses"));
+        goto cleanup;
+    }
+
+    vshPrintExtra(ctl, " %-10s %-17s    %s\n%s\n",
+                  _("Name"), _("MAC address"), _("IP address"),
+                  "---------------------------------------------------");
+
+    for (i = 0; i < ifaces_count; i++) {
+        virDomainInterfacePtr iface = &(ifaces[i]);
+        virBuffer buf = VIR_BUFFER_INITIALIZER;
+        const char *hwaddr = "";
+        const char *ip_addr_str = NULL;
+
+        if (interface && STRNEQ_NULLABLE(interface, iface->name)) {

My bad, I shouldn't tell you to use STRNEQ_NULLABLE here, because
iface->name must be NOT NULL, and checking interface is NULL or
not has logical meaning (wheter [interface] is specified, it makes code
more readable). This sentense should be written as:

    if (interface && STRNEQ(interface, iface->name)
         continue;

+            virBufferFreeAndReset(&buf);
+            continue;
+        }
+
+        if (iface->hwaddr)
+            hwaddr = iface->hwaddr;
+
+        for (j = 0; j < iface->ip_addrs_count; j++) {
+            if (j)
+                virBufferAddChar(&buf, ' ');
+            virBufferAsprintf(&buf, "%s/%d",
+                              iface->ip_addrs[j].addr,
+                              iface->ip_addrs[j].prefix);
+        }
+
+        if (virBufferError(&buf)) {
+            virBufferFreeAndReset(&buf);
+            virReportOOMError();
+            return ret;
+        }
+
+        ip_addr_str = virBufferContentAndReset(&buf);
+
+        if (!ip_addr_str)
+            ip_addr_str = "";
+
+        vshPrintExtra(ctl, " %-10s %-17s    %s\n",
+                     iface->name, hwaddr, ip_addr_str);
+
+        virBufferFreeAndReset(&buf);
+    }
+
+    ret = true;
+
+cleanup:
+    for (i = 0; i < ifaces_count; i++) {
+        VIR_FREE(ifaces[i].name);
+        VIR_FREE(ifaces[i].hwaddr);
+        for (j = 0; j < ifaces[i].ip_addrs_count; j++)
+            VIR_FREE(ifaces[i].ip_addrs[j].addr);
+        VIR_FREE(ifaces[i].ip_addrs);
+    }
+    VIR_FREE(ifaces);
+
+    if (dom)
+        virDomainFree(dom);

see [1], "dom" must be not NULL as long as it went to here.  So
no need for the checking.

+    return ret;
+}
+
  const vshCmdDef domMonitoringCmds[] = {
      {.name = "domblkerror",
       .handler = cmdDomBlkError,
@@ -1946,5 +2043,11 @@ const vshCmdDef domMonitoringCmds[] = {
       .info = info_list,
       .flags = 0
      },
+    {.name = "domifaddr",
+     .handler = cmdDomIfAddr,
+     .opts = opts_domifaddr,
+     .info = info_domifaddr,
+     .flags = 0
+    },
      {.name = NULL}
  };
diff --git a/tools/virsh.pod b/tools/virsh.pod
index 3ff6da1..bf13be4 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -633,6 +633,16 @@ B<Explanation of fields> (fields appear in the following order):
    flush_total_times - total time flush operations took (ns)
      <-- other fields provided by hypervisor -->
+
+=item B<domifaddr> I<domain> [I<interface-device>]

s/interface-device/interface/, since you are using "interface" instead of
"interface-device" in the code.

+static const vshCmdOptDef opts_domifaddr[] = {
+    {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
+    {"interface", VSH_OT_DATA, VSH_OFLAG_NONE, N_("network interface name")},
+    {NULL, 0, 0, NULL}


Otherwise, the help document from "virsh help domifaddr" and in virsh.pod
are different.

+
+Get a list of interfaces of a running domain along with their IP and MAC
+addresses, or limited output just for one interface if I<interface-device>

Like above.

+is specified. Note, that I<interface-device>  can be driver dependent, it can

Like above.

+be the name within guest OS or the name you would see in domain XML.
+Moreover, the whole command may require a guest agent to be configured
+for the queried domain under some drivers, notably qemu.
+
  =item B<domifstat> I<domain> I<interface-device>
Get network interface stats for a running domain.


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