[libvirt] [PATCHv5 4/4] net-dhcp-leases: Add virsh support

Nehal J Wani nehaljw.kkd1 at gmail.com
Mon Nov 25 21:06:01 UTC 2013


Use virNetworkGetDHCPLeases and virNetworkGetDHCPLeasesForMAC in virsh.

The new feature supports the follwing methods:

1. Retrieve leases info for a given virtual network

2. Retrieve leases info for given network interface

tools/virsh-domain-monitor.c
   * Introduce new command : net-dhcp-leases
     Example Usage: net-dhcp-leases <network> [mac]

     virsh # net-dhcp-leases --network default6
     Expiry Time          MAC address       Protocol   IP address                Hostname        Client ID or DUID
     -------------------------------------------------------------------------------------------------------------------
     2013-11-24 03:59:40  52:54:00:2f:ba:76 ipv4       192.168.150.153/24        (null)          (null)
     2013-11-24 03:59:41  52:54:00:2f:ba:76 ipv6       2001:db8:ca2:2:1::6c/24   (null)          00:04:76:00:cf:ae:b3:0b:fc:cd:0e:22:2e:97:76:65:74:ec
     2013-11-24 04:04:01  52:54:00:3b:16:e0 ipv4       192.168.150.207/24        (null)          (null)
     2013-11-24 04:02:44  52:54:00:44:7c:d7 ipv4       192.168.150.219/24        iiit-ad885e4aa1 01:52:54:00:44:7c:d7
     2013-11-24 04:02:44  52:54:00:44:7c:d7 ipv4       192.168.150.219/24        (null)          01:52:54:00:44:7c:d7
     2013-11-24 04:03:36  52:54:00:5d:99:92 ipv4       192.168.150.212/24        iiit-ad885e4aa1 01:52:54:00:5d:99:92
     2013-11-24 04:04:41  52:54:00:db:dd:98 ipv4       192.168.150.234/24        (null)          (null)
     2013-11-24 04:04:48  52:54:00:db:dd:98 ipv6       2001:db8:ca2:2:1::6d/24   (null)          00:04:76:00:cf:ae:b3:0b:fc:cd:0e:22:2e:97:76:65:74:ec


tools/virsh.pod
   * Document new command

---
 tools/virsh-network.c | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/virsh.pod       |   6 +++
 2 files changed, 124 insertions(+)

diff --git a/tools/virsh-network.c b/tools/virsh-network.c
index 44a676b..270f91b 100644
--- a/tools/virsh-network.c
+++ b/tools/virsh-network.c
@@ -1129,6 +1129,118 @@ cmdNetworkEdit(vshControl *ctl, const vshCmd *cmd)
 
     return ret;
 }
+/*
+ * "net-dhcp-leases" command
+ */
+static const vshCmdInfo info_network_dhcp_leases[] = {
+    {.name = "help",
+     .data = N_("print lease info for a given network")
+    },
+    {.name = "desc",
+     .data = N_("Print lease info for a given network")
+    },
+    {.name = NULL}
+};
+
+static const vshCmdOptDef opts_network_dhcp_leases[] = {
+    {.name = "network",
+     .type = VSH_OT_DATA,
+     .flags = VSH_OFLAG_REQ,
+     .help = N_("network name or uuid")
+    },
+    {.name = "mac",
+     .type = VSH_OT_DATA,
+     .flags = VSH_OFLAG_NONE,
+     .help = N_("MAC address")
+    },
+    {.name = NULL}
+};
+
+static int
+vshNetworkDHCPLeaseSorter(const void *a, const void *b)
+{
+    int rv = -1;
+
+    virNetworkDHCPLeasesPtr *lease1 = (virNetworkDHCPLeasesPtr *) a;
+    virNetworkDHCPLeasesPtr *lease2 = (virNetworkDHCPLeasesPtr *) b;
+
+    if (*lease1 && !*lease2)
+        return -1;
+
+    if (!*lease1)
+        return *lease2 != NULL;
+
+    rv = vshStrcasecmp((*lease1)->mac, (*lease2)->mac);
+    return rv;
+}
+
+static bool
+cmdNetworkDHCPLeases(vshControl *ctl, const vshCmd *cmd)
+{
+    const char *name = NULL;
+    const char *mac = NULL;
+    virNetworkDHCPLeasesPtr *leases = NULL;
+    int nleases = 0;
+    bool ret = false;
+    size_t i;
+    unsigned int flags = 0;
+    virNetworkPtr network = NULL;
+
+    if (vshCommandOptString(cmd, "mac", &mac) < 0)
+        return false;
+
+    if (!(network = vshCommandOptNetwork(ctl, cmd, &name)))
+        return false;
+
+    nleases = mac ? virNetworkGetDHCPLeasesForMAC(network, mac, &leases, flags)
+        : virNetworkGetDHCPLeases(network, &leases, flags);
+
+    if (nleases < 0) {
+        vshError(ctl, _("Failed to get leases info for %s"), name);
+        goto cleanup;
+    }
+
+    /* Sort the list according to MAC Address/IAID */
+    qsort(leases, nleases, sizeof(*leases), vshNetworkDHCPLeaseSorter);
+
+    vshPrintExtra(ctl, " %-20s %-17s %-10s %-25s %-15s %s\n%s%s\n",
+                  _("Expiry Time"), _("MAC address"), _("Protocol"),
+                  _("IP address"), _("Hostname"), _("Client ID or DUID"),
+                  "----------------------------------------------------------",
+                  "---------------------------------------------------------");
+
+    for (i = 0; i < nleases; i++) {
+        const char *type = NULL;
+        char *cidr_format = NULL;
+        virNetworkDHCPLeasesPtr lease = leases[i];
+        time_t expirytime_tmp = lease->expirytime;
+        struct tm ts;
+        char expirytime[32];
+        ts = *localtime_r(&expirytime_tmp, &ts);
+        strftime(expirytime, sizeof(expirytime), "%Y-%m-%d %H:%M:%S", &ts);
+
+        type = (lease->type == VIR_IP_ADDR_TYPE_IPV4) ? "ipv4" :
+            (lease->type == VIR_IP_ADDR_TYPE_IPV6) ? "ipv6" : "";
+
+        ignore_value(virAsprintf(&cidr_format, "%s/%d",
+                                 lease->ipaddr, lease->prefix));
+
+        vshPrintExtra(ctl, " %-20s %-17s %-10s %-25s %-15s %s\n",
+                      expirytime, NULLSTR(lease->mac), NULLSTR(type), cidr_format,
+                      NULLSTR(lease->hostname), NULLSTR(lease->clientid));
+    }
+
+    ret = true;
+
+cleanup:
+    if (leases) {
+        for (i = 0; i < nleases; i++)
+            virNetworkDHCPLeaseFree(leases[i]);
+        VIR_FREE(leases);
+    }
+    virNetworkFree(network);
+    return ret;
+}
 
 const vshCmdDef networkCmds[] = {
     {.name = "net-autostart",
@@ -1209,5 +1321,11 @@ const vshCmdDef networkCmds[] = {
      .info = info_network_uuid,
      .flags = 0
     },
+    {.name = "net-dhcp-leases",
+     .handler = cmdNetworkDHCPLeases,
+     .opts = opts_network_dhcp_leases,
+     .info = info_network_dhcp_leases,
+     .flags = 0,
+    },
     {.name = NULL}
 };
diff --git a/tools/virsh.pod b/tools/virsh.pod
index dac9a08..e47be66 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -2340,6 +2340,12 @@ If I<--current> is specified, affect the current network state.
 Both I<--live> and I<--config> flags may be given, but I<--current> is
 exclusive. Not specifying any flag is the same as specifying I<--current>.
 
+=item B<net-dhcp-leases> I<network> [I<mac>]
+
+Get a list of dhcp leases for all network interfaces connected to the given
+virtual I<network> or limited output just for one interface if I<mac> is
+specified.
+
 =back
 
 =head1 INTERFACE COMMANDS
-- 
1.8.1.4




More information about the libvir-list mailing list