Re: [PATCHv2 1/4] net-dhcp-leases: Implement the public APIs

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

 



On 13/09/13 05:53, Nehal J Wani wrote:
Introduce 3 new APIs, virNetworkGetDHCPLeases, virNetworkGetDHCPLeasesForMAC
and virNetworkDHCPLeaseFree.

* virNetworkGetDHCPLeases: returns the dhcp leases information for a given
      virtual network. The information includes lease expirytime, MAC Address,
      IP Address, hostname and clientid.

Trivial, but "type" and "prefix" are new in this set.


* virNetworkGetDHCPLeasesForMAC: returns the dhcp leases information for a
      given virtual network and specified MAC Address.

* virNetworkDHCPLeaseFree: allows the upper layer application to free the
      network interface object conveniently.

There is no support for flags, so user is expected to pass 0 for
both the APIs.

include/libvirt/libvirt.h.in:
   * Define virNetworkGetDHCPLeases
   * Define virNetworkGetDHCPLeasesForMAC
   * Define virNetworkDHCPLeaseFree

python/generator.py:
   * Skip the auto-generation for virNetworkGetDHCPLeases
   * Skip the auto-generation for virNetworkGetDHCPLeasesForMAC
   * Skip the auto-generation for virNetworkDHCPLeaseFree

src/driver.h:
   * Define networkGetDHCPLeases
   * Define networkGetDHCPLeasesForMAC

src/libvirt.c:
   * Implement virNetworkGetDHCPLeases
   * Implement virNetworkGetDHCPLeasesForMAC
   * Implement virNetworkDHCPLeaseFree

src/libvirt_public.syms:
   * Export the new symbols

src/libvirt_private.syms:
   * Export the symbol: virSocketAddrGetNumNetmaskBits

---
  include/libvirt/libvirt.h.in |  33 +++++++++
  python/generator.py          |   3 +
  src/driver.h                 |  13 ++++
  src/libvirt.c                | 173 +++++++++++++++++++++++++++++++++++++++++++
  src/libvirt_private.syms     |   1 +
  src/libvirt_public.syms      |   7 ++
  6 files changed, 230 insertions(+)

diff --git a/include/libvirt/libvirt.h.in b/include/libvirt/libvirt.h.in
index a47e33c..d92a720 100644
--- a/include/libvirt/libvirt.h.in
+++ b/include/libvirt/libvirt.h.in
@@ -2800,6 +2800,39 @@ int                     virConnectNumOfDefinedInterfaces (virConnectPtr conn);
  int                     virConnectListDefinedInterfaces  (virConnectPtr conn,
                                                            char **const names,
                                                            int maxnames);
+
+typedef enum {
+    VIR_IP_ADDR_TYPE_IPV4,
+    VIR_IP_ADDR_TYPE_IPV6,
+
+#ifdef VIR_ENUM_SENTINELS
+    VIR_IP_ADDR_TYPE_LAST
+#endif
+} virIPAddrType;
+

Okay, assuming that this set of API will be pushed before the interfaceAddresses
API, this is fine.

+typedef struct _virNetworkDHCPLeases virNetworkDHCPLeases;
+typedef virNetworkDHCPLeases *virNetworkDHCPLeasesPtr;
+struct _virNetworkDHCPLeases {
+    long long expirytime;
+    char *mac;                  /* MAC address */
+    char *ipaddr;               /* IP address */
+    char *hostname;             /* Hostname */
+    char *clientid;             /* Client ID */
+    int type;                   /* virIPAddrType */
+    unsigned int prefix;        /* IP address prefix */
+};
+
+void virNetworkDHCPLeaseFree(virNetworkDHCPLeasesPtr lease);
+
+int virNetworkGetDHCPLeases(virNetworkPtr network,
+                            virNetworkDHCPLeasesPtr **leases,
+                            unsigned int flags);
+
+int virNetworkGetDHCPLeasesForMAC(virNetworkPtr network,
+                                  const char *mac,
+                                  virNetworkDHCPLeasesPtr **leases,
+                                  unsigned int flags);
+
  /*
   * virConnectListAllInterfaces:
   *
diff --git a/python/generator.py b/python/generator.py
index a91dde8..e76cbfc 100755
--- a/python/generator.py
+++ b/python/generator.py
@@ -460,6 +460,8 @@ skip_impl = (
      'virNodeGetCPUMap',
      'virDomainMigrate3',
      'virDomainMigrateToURI3',
+    'virNetworkGetDHCPLeases',
+    'virNetworkGetDHCPLeasesForMAC',
  )
lxc_skip_impl = (
@@ -560,6 +562,7 @@ skip_function = (
      "virTypedParamsGetString",
      "virTypedParamsGetUInt",
      "virTypedParamsGetULLong",
+    'virNetworkDHCPLeaseFree',
  )
lxc_skip_function = (
diff --git a/src/driver.h b/src/driver.h
index be64333..698e0ca 100644
--- a/src/driver.h
+++ b/src/driver.h
@@ -1121,6 +1121,17 @@ typedef int
                                       int cookieinlen,
                                       unsigned int flags,
                                       int cancelled);
+typedef int
+(*virDrvNetworkGetDHCPLeases)(virNetworkPtr network,
+                              virNetworkDHCPLeasesPtr **leases,
+                              unsigned int flags);
+
+typedef int
+(*virDrvNetworkGetDHCPLeasesForMAC)(virNetworkPtr network,
+                                    const char *mac,
+                                    virNetworkDHCPLeasesPtr **leases,
+                                    unsigned int flags);
+
typedef struct _virDriver virDriver;
  typedef virDriver *virDriverPtr;
@@ -1451,6 +1462,8 @@ struct _virNetworkDriver {
      virDrvNetworkSetAutostart networkSetAutostart;
      virDrvNetworkIsActive networkIsActive;
      virDrvNetworkIsPersistent networkIsPersistent;
+    virDrvNetworkGetDHCPLeases networkGetDHCPLeases;
+    virDrvNetworkGetDHCPLeasesForMAC networkGetDHCPLeasesForMAC;
  };
diff --git a/src/libvirt.c b/src/libvirt.c
index 20a2d4c..aa6a2fe 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -68,6 +68,7 @@
  #include "virstring.h"
  #include "virutil.h"
  #include "virtypedparam.h"
+#include "virmacaddr.h"
#ifdef WITH_TEST
  # include "test/test_driver.h"
@@ -21965,3 +21966,175 @@ error:
      virDispatchError(dom->conn);
      return -1;
  }
+
+/**
+ * virNetworkGetDHCPLeases:
+ * @network: pointer to network object
+ * @leases: pointer to an array of pointers pointing to the obtained leases
+ * @flags: extra flags, not used yet, so callers should always pass 0
+ *
+ * The API returns lease info for all network interfaces connected to the
+ * given virtual network.
+ *
+ * Returns the number of leases found or -1 and sets @leases to NULL in case
+ * of error. On success, the array stored into @leases is guaranteed to have an
+ * extra allocated element set to NULL but not included in the return count,
+ * to make iteration easier. The caller is responsible for calling
+ * virNetworkDHCPLeaseFree on each array element, then calling free() on @leases.
+ *
+ * Example of usage:
+ *
+ * virNetworkDHCPLeasesPtr *leases = NULL;
+ * virNetworkPtr network = ... obtain a network pointer here ...;
+ * size_t i;
+ * int nleases;
+ * unsigned int flags = 0;
+ *
+ * nleases = virNetworkGetDHCPLeases(network, &leases, flags);
+ * if (nleases < 0)
+ *     error();
+ *
+ * ... do something with returned values, for example:
+ *
+ * for (i = 0; i < nleases; i++) {
+ *     virNetworkDHCPLeasesPtr lease = leases[i];
+ *     printf("Time(epoch): %lu, MAC address: %s,

Missed the terminating " character.

+ *            IP address: %s, Hostname: %s, ClientID: %s\n",
+ *            expirytime, lease->mac,

lease->expirytime

+ *            lease->ipaddr, lease->hostname, lease->clientid);
+ * }
+ *
+ * if (leases) {
+ *     for (i = 0; i < nleases; i++)
+ *         virNetworkDHCPLeaseFree(leases[i]);
+ * }
+ * free(leases);
+ *
+ */
+int
+virNetworkGetDHCPLeases(virNetworkPtr network,
+                        virNetworkDHCPLeasesPtr **leases,
+                        unsigned int flags)
+{
+    virConnectPtr conn;
+    VIR_DEBUG("network=%p, leases=%p, flags=%x",
+               network, leases, flags);
+
+    virResetLastError();
+
+    virCheckNonNullArgGoto(network, error);
+
+    if (leases)
+        *leases = NULL;
+
+    if (!VIR_IS_CONNECTED_NETWORK(network)) {
+        virLibNetworkError(VIR_ERR_INVALID_NETWORK, __FUNCTION__);
+        virDispatchError(NULL);
+        return -1;
+    }
+
+    conn = network->conn;
+
+    if (conn->networkDriver && conn->networkDriver->networkGetDHCPLeases) {
+        int ret;
+        ret = conn->networkDriver->networkGetDHCPLeases(network,leases, flags);
+        if (ret < 0)
+            goto error;
+        return ret;
+    }
+
+    virLibConnError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
+
+error:
+    virDispatchError(network->conn);
+    return -1;
+}
+
+/**
+ * virNetworkGetDHCPLeasesForMAC:
+ * @network: pointer to network object
+ * @mac: MAC Address of an interface
+ * @leases: pointer to an array of pointers pointing to the obtained leases
+ * @flags: extra flags, not used yet, so callers should always pass 0
+ *
+ * The API returns the leases info of the interfaces which match with the

s/interfaces/interface/, s/match/matches/,

+ * given @mac. There can be multiple leases for a single @mac because this
+ * API intends to support DHCP v6 too.

s/DHCP v6/DHCPv6/,

+ *
+ * Returns the number of leases found or -1 and sets @leases to NULL in case
+ * of error. On success, the array stored into @leases is guaranteed to have an
+ * extra allocated element set to NULL but not included in the return count,
+ * to make iteration easier. The caller is responsible for calling
+ * virNetworkDHCPLeaseFree on each array element, then calling free() on @leases.
+ */
+int
+virNetworkGetDHCPLeasesForMAC(virNetworkPtr network,
+                              const char *mac,
+                              virNetworkDHCPLeasesPtr **leases,
+                              unsigned int flags)
+{
+    virConnectPtr conn;
+    virMacAddr addr;
+
+    VIR_DEBUG("network=%p, mac=%s, leases=%p, flags=%x",
+               network, mac, leases, flags);
+
+    virResetLastError();
+
+    virCheckNonNullArgGoto(network, error);
+    virCheckNonNullArgGoto(mac, error);
+
+    if (leases)
+        *leases = NULL;
+
+    if (!VIR_IS_CONNECTED_NETWORK(network)) {
+        virLibNetworkError(VIR_ERR_INVALID_NETWORK, __FUNCTION__);
+        virDispatchError(NULL);
+        return -1;
+    }
+
+    /* Validate the MAC address */
+    if (mac && virMacAddrParse(mac, &addr) < 0) {
+        virReportInvalidArg(mac,
+                            _("Given MAC Address doesn't comply "
+                              "with the standard (IEEE 802) format in %s"),
+                            __FUNCTION__);
+        goto error;
+    }
+
+    conn = network->conn;
+
+    if (conn->networkDriver &&
+        conn->networkDriver->networkGetDHCPLeasesForMAC) {
+        int ret;
+        ret = conn->networkDriver->networkGetDHCPLeasesForMAC(network, mac,
+                                                              leases, flags);
+        if (ret < 0)
+            goto error;
+        return ret;
+    }
+
+    virLibConnError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
+
+error:
+    virDispatchError(network->conn);
+    return -1;
+}
+
+/**
+ * virNetworkDHCPLeaseFree:
+ * @lease: pointer to a leases object
+ *
+ * Frees all the memory occupied by @lease.
+ */
+void
+virNetworkDHCPLeaseFree(virNetworkDHCPLeasesPtr lease)
+{
+    if (!lease)
+        return;
+    VIR_FREE(lease->mac);
+    VIR_FREE(lease->ipaddr);
+    VIR_FREE(lease->hostname);
+    VIR_FREE(lease->clientid);
+    VIR_FREE(lease);
+}
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 35f0f1b..7d60c51 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1869,6 +1869,7 @@ virSocketAddrCheckNetmask;
  virSocketAddrEqual;
  virSocketAddrFormat;
  virSocketAddrFormatFull;
+virSocketAddrGetNumNetmaskBits;

Mistake, I don't see virSocketAddrGetNumNetmaskBits is ever used in this patch.

ACK with the nits fixed.

Osier

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