Re: [PATCHv5 4/5] domifaddr: Add virsh support

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

 



On 01/09/13 21:43, Nehal J Wani wrote:
Use virDomainInterfaceAddresses in virsh

tools/virsh-domain-monitor.c
    * Introduce new command : domifaddr
      Usage: domifaddr <domain> [interface] [full]

As Daniel pointed out, it should be "[--full]"


      Example output:
      virsh # domifaddr f18
      Name       MAC address          Protocol     IP Address
      -------------------------------------------------------------------------------
      lo         00:00:00:00:00:00    ipv4         127.0.0.1/8
      -          -                    ipv6         ::1/128
      eth0       52:54:00:2e:45:ce    ipv4         10.1.33.188/24
      -          -                    ipv6         2001:db8:0:f101::2/64
      -          -                    ipv6         fe80::5054:ff:fe2e:45ce/64
      eth1       52:54:00:b1:70:19    ipv4         192.168.105.201/16
      -          -                    ipv4         192.168.201.195/16
      -          -                    ipv6         fe80::5054:ff:feb1:7019/64
      eth2       52:54:00:36:2a:e5

tools/virsh.pod
    * Document new command

---
  tools/virsh-domain-monitor.c | 119 +++++++++++++++++++++++++++++++++++++++++++
  tools/virsh.pod              |  11 ++++
  2 files changed, 130 insertions(+)

diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index b29b82a..cd1df7a 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -1871,6 +1871,119 @@ cleanup:
  }
  #undef FILTER
+/* "domifaddr" command
+ */
+static const vshCmdInfo info_domifaddr[] = {
+    {"help", N_("Get network interfaces' addresses for a running domain")},
+    {"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")},
+    {"full", VSH_OT_BOOL, VSH_OFLAG_NONE, N_("display full fields")},
+    {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;
+    int ifaces_count = 0;
+    unsigned int flags = 0;
+    bool ret = false;
+    bool optFull = vshCommandOptBool(cmd, "full");

s/optFull/full/,

+
+    if (!(dom = vshCommandOptDomain(ctl, cmd, NULL)))
+        return false;
+
+    if (vshCommandOptString(cmd, "interface", &interface) < 0) {
+        goto cleanup;
+    }
+
+    if ((ifaces_count = virDomainInterfaceAddresses(dom, &ifaces, flags)) < 0) {
+        vshError(ctl, _("Failed to query for interfaces addresses"));
+        goto cleanup;
+    }
+
+    vshPrintExtra(ctl, " %-10s %-20s %-8s     %s\n%s%s\n", _("Name"),
+                  _("MAC address"), _("Protocol"), _("Address"),
+                  _("-------------------------------------------------"),
+                  _("------------------------------"));
+
+    for (i = 0; i < ifaces_count; i++) {
+        virDomainInterfacePtr iface = ifaces[i];
+        const char *hwaddr = "";
+        const char *ip_addr_str = NULL;
+        const char *type = NULL;
+
+        if (interface && STRNEQ(interface, iface->name))
+            continue;
+
+        if (iface->hwaddr)
+            hwaddr = iface->hwaddr;

Variable "hwaddr" can be avoided? directly using iface->hwaddr
should be fine.

+
+        /* When the interface has no IP address */
+        if (!iface->naddrs) {
+            vshPrintExtra(ctl, " %-10s %-17s\n",
+                          iface->name, hwaddr);

Not harm to add "continue" here.  It will avoid the enter into the later
for loop.

And to be consistent, we use "N/A" if the field is empty in virsh, instead
of nothing in the output.

+        }
+
+        for (j = 0; j < iface->naddrs; j++) {
+            virBuffer buf = VIR_BUFFER_INITIALIZER;
+
+            switch (iface->addrs[j].type) {
+            case VIR_IP_ADDR_TYPE_IPV4:
+                type = "ipv4";
+                break;
+            case VIR_IP_ADDR_TYPE_IPV6:
+                type = "ipv6";
+                break;
+            }
+
+            virBufferAsprintf(&buf, "%-12s %s/%d",
+                              type, iface->addrs[j].addr,
+                              iface->addrs[j].prefix);
+
+            if (virBufferError(&buf)) {
+                virBufferFreeAndReset(&buf);
+                virReportOOMError();
+                return ret;

Memory leak, need "goto cleanup;"

+            }
+
+            ip_addr_str = virBufferContentAndReset(&buf);
+
+            if (!ip_addr_str)
+                ip_addr_str = "";
+
+            /* Don't repeat interface name */
+            if (optFull || !j)
+                vshPrintExtra(ctl, " %-10s %-17s    %s\n",
+                              iface->name, hwaddr, ip_addr_str);
+            else
+                vshPrintExtra(ctl, " %-10s %-17s    %s\n",
+                              "-","-",ip_addr_str);

"-", "-", ip_add_str);

I.E Add the space after ","

+
+            virBufferFreeAndReset(&buf);
+        }
+    }
+
+    ret = true;
+
+cleanup:
+    if (ifaces)
+        for (i = 0; i < ifaces_count; i++)
+            virDomainInterfaceFree(ifaces[i]);
+    VIR_FREE(ifaces);
+
+    virDomainFree(dom);
+    return ret;
+}
+
  const vshCmdDef domMonitoringCmds[] = {
      {.name = "domblkerror",
       .handler = cmdDomBlkError,
@@ -1944,5 +2057,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 0ae5178..04814fa 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -636,6 +636,17 @@ 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>] [I<--full>]
+
+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> is
+specified. Note, that I<interface> can be driver dependent, it can 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. All fields can
+be fully displayed by passing the option: I<--full>.

This doesn't tell what the real purpose of <--full>. How about:

If I<--full> is specified, the interface name is always displayed when the interface has multiple addresses or alias, otherwise it only displays the interface name for the
first address, and "-" for the others.

+
  =item B<domifstat> I<domain> I<interface-device>
Get network interface stats for a running domain.

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