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

Re: [libvirt] [PATCHv2 2/4] network: provide internal API to return IP of a network

On 07/20/2011 02:11 AM, Laine Stump wrote:
The new listenNetwork atribute needs to learn an IP address based on a


named network. This patch provides a function networkGetNetworkAddress
which provides that.

Some networks have an IP address explicitly in their configuration
(ie, those with a forward type of "none", "route", or "nat"). For
those, we can just return the IP address from the config.

The rest will have a physical device associated with them (either via
<bridge name='...'/>,<forward ... dev='...'/>, or possibly via a pool
of interfaces inside the network's<forward>  element) and we will need
to ask the kernel for the current IP address of that device (via the

s/the current/a current/ - the kernel may know more than one, but we only care about finding at least one (and as you pointed out in the previous patch, you only get the first).

newly added ifaceGetIPAddress

Missing )

If networkGetNetworkAddress encounters an error while trying to learn
the address for a network, it will return -1. In the case that libvirt
has been compiled without the network driver, a static inline version
of the function, which returns -2, will be used. This allows
differentiating between a failure of the network driver, and its
complete absence.
  src/libvirt_network.syms    |    1 +
  src/network/bridge_driver.c |  100 +++++++++++++++++++++++++++++++++++++++++++
  src/network/bridge_driver.h |    7 +++
  3 files changed, 108 insertions(+), 0 deletions(-)

+ * networkGetNetworkAddress:
+ * @netname: the name of a network
+ * @netaddr: string representation of IP address for that network.
+ *
+ * Attempt to return the IP (v4) address associated with the named

s/the IP/an IP/

+ * network. If a libvirt virtual network, that will be provided in the
+ * configuration. For host bridge and direct (macvtap) networks, we
+ * must do an ioctl to learn the address.
+ *
+ * Note: This function returns the 1st IPv4 address it finds. It might
+ * be useful if it was more flexible, but the current use (getting a
+ * listen address for qemu's vnc/spice graphics server) can only use a
+ * single address anyway.
+ *
+ * Returns 0 on success, and puts a string (which must be free'd by
+ * the caller) into *netaddr. Returns -1 on failure

Might also be worth mentioning -2 on complete lack of support.

+        /* if there's an ipv4def, get it's address */
+        ipdef = virNetworkDefGetIpByIndex(netdef, AF_INET, 0);
+        if (!ipdef) {
+            networkReportError(VIR_ERR_INTERNAL_ERROR,
+                               _("network '%s' doesn't have an IP address"),

It might have an IPv6 address though, so touch up the error message:


+        if ((devname = netdef->bridge))
+            break;
+        /*
+         * If netdef->bridge wasn't set, this is a direct-mode
+         * interface, so purposefully fall through to the next case
+         */

Coverity will probably gripe at this one.  We may need to re-word this:

/* Fall through if netdef->bridge wasn't set, since this is a direct-mode interface */

+++ b/src/network/bridge_driver.h
@@ -40,6 +40,8 @@ int networkAllocateActualDevice(virDomainNetDefPtr iface);
  int networkNotifyActualDevice(virDomainNetDefPtr iface);
  int networkReleaseActualDevice(virDomainNetDefPtr iface);

+int networkGetNetworkAddress(const char *netname, char **netaddr);


  int networkBuildDhcpDaemonCommandLine(virNetworkObjPtr network,
                                        virCommandPtr *cmdout, char *pidfile,
                                        dnsmasqContext *dctx);
@@ -57,6 +59,11 @@ networkReleaseActualDevice(virDomainNetDefPtr iface ATTRIBUTE_UNUSED)
  { return 0; }

  static inline int
+networkGetNetworkAddress(const char *netname ATTRIBUTE_UNUSED,
+                         char **netaddr ATTRIBUTE_UNUSED)
+   { return -2; }

# define networkGetNetworkAddress(netname, netaddr) (-2)

ACK with those nits fixed.

Eric Blake   eblake redhat com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

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