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

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



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 default6
     Expiry Time          MAC address / IAID        Protocol   IP address                Hostname     Client ID / DUID
     -------------------------------------------------------------------------------------------------------------------
     2013-09-26 12:46:15  140483776435424           ipv6       2001:db8:ca2:2:1::92/24   (null)       00:01:00:01:19:d6:0c:59:52:54:00:2e:1d:22
     2013-09-26 12:26:29  140483776438128           ipv6       2001:db8:ca2:2:1::4e/24   (null)       00:01:00:01:19:d5:e0:86:52:54:00:72:0f:1e
     2013-09-26 12:34:31  52:54:00:2e:1d:22         ipv4       192.168.122.80/24         (null)       (null)
     2013-09-26 12:13:24  52:54:00:72:0f:1e         ipv4       192.168.122.119/24        (null)       (null)

tools/virsh.pod
   * Document new command

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

diff --git a/tools/virsh-network.c b/tools/virsh-network.c
index 8ddd5ca..5786ca2 100644
--- a/tools/virsh-network.c
+++ b/tools/virsh-network.c
@@ -1129,6 +1129,150 @@ 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)
+{
+    char *id1 = NULL;
+    char *id2 = NULL;
+    int rv = -1;
+
+    virNetworkDHCPLeasesPtr *lease1 = (virNetworkDHCPLeasesPtr *) a;
+    virNetworkDHCPLeasesPtr *lease2 = (virNetworkDHCPLeasesPtr *) b;
+
+    if (*lease1 && !*lease2)
+        return -1;
+
+    if (!*lease1)
+        return *lease2 != NULL;
+
+
+    switch ((*lease1)->type) {
+    case VIR_IP_ADDR_TYPE_IPV4:
+        ignore_value(virAsprintf(&id1, "%s", (*lease1)->id.mac));
+        break;
+    case VIR_IP_ADDR_TYPE_IPV6:
+        ignore_value(virAsprintf(&id1, "%lu", (*lease1)->id.iaid));
+        break;
+    }
+    switch ((*lease2)->type) {
+    case VIR_IP_ADDR_TYPE_IPV4:
+        ignore_value(virAsprintf(&id2, "%s", (*lease2)->id.mac));
+        break;
+    case VIR_IP_ADDR_TYPE_IPV6:
+        ignore_value(virAsprintf(&id2, "%lu", (*lease2)->id.iaid));
+        break;
+    }
+
+    rv = vshStrcasecmp(id1, id2);
+    VIR_FREE(id1);
+    VIR_FREE(id2);
+    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 %-25s %-10s %-25s %-12s %s\n%s%s\n",
+                  _("Expiry Time"), _("MAC address / IAID"), _("Protocol"),
+                  _("IP address"), _("Hostname"), _("Client ID / DUID"),
+                  "----------------------------------------------------------",
+                  "---------------------------------------------------------");
+
+    for (i = 0; i < nleases; i++) {
+        const char *type = NULL;
+        char *id = 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);
+
+        switch (lease->type) {
+        case VIR_IP_ADDR_TYPE_IPV4:
+            type = "ipv4";
+            ignore_value(virAsprintf(&id, "%s", lease->id.mac));
+            break;
+        case VIR_IP_ADDR_TYPE_IPV6:
+            type = "ipv6";
+            ignore_value(virAsprintf(&id, "%lu", lease->id.iaid));
+            break;
+        }
+
+        ignore_value(virAsprintf(&cidr_format, "%s/%d",
+                                 lease->ipaddr, lease->prefix));
+
+        vshPrintExtra(ctl, " %-20s %-25s %-10s %-25s %-12s %s\n",
+                      expirytime, NULLSTR(id), NULLSTR(type), cidr_format,
+                      NULLSTR(lease->hostname), NULLSTR(lease->clientid));
+        VIR_FREE(id);
+    }
+
+    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 +1353,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 e12a800..ed5fc62 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -2333,6 +2333,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.7.11.7


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