The QEMU driver calls into the network driver to get the first IP
address of the network. This information is readily available via the
formal public API by fetching the XML doc and then parsing it.
Signed-off-by: Daniel P. Berrangé <berrange(a)redhat.com>
---
src/network/bridge_driver.c | 98 -----------------------------------------
src/network/bridge_driver.h | 6 ---
src/qemu/qemu_process.c | 103 +++++++++++++++++++++++++++++++++++++++++---
3 files changed, 96 insertions(+), 111 deletions(-)
diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c
index a0e45d1f65..ee200f1343 100644
--- a/src/network/bridge_driver.c
+++ b/src/network/bridge_driver.c
@@ -5104,104 +5104,6 @@ networkReleaseActualDevice(virDomainDefPtr dom,
}
-/*
- * networkGetNetworkAddress:
- * @netname: the name of a network
- * @netaddr: string representation of IP address for that network.
- *
- * Attempt to return an IP address associated with the named
- * 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 first IP 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 or -2 if
- * completely unsupported.
- */
-int
-networkGetNetworkAddress(const char *netname,
- char **netaddr)
-{
- virNetworkDriverStatePtr driver = networkGetDriver();
- int ret = -1;
- virNetworkObjPtr obj;
- virNetworkDefPtr netdef;
- virNetworkIPDefPtr ipdef;
- virSocketAddr addr;
- virSocketAddrPtr addrptr = NULL;
- char *dev_name = NULL;
-
- *netaddr = NULL;
- obj = virNetworkObjFindByName(driver->networks, netname);
- if (!obj) {
- virReportError(VIR_ERR_NO_NETWORK,
- _("no network with matching name '%s'"),
- netname);
- goto cleanup;
- }
- netdef = virNetworkObjGetDef(obj);
-
- switch (netdef->forward.type) {
- case VIR_NETWORK_FORWARD_NONE:
- case VIR_NETWORK_FORWARD_NAT:
- case VIR_NETWORK_FORWARD_ROUTE:
- case VIR_NETWORK_FORWARD_OPEN:
- ipdef = virNetworkDefGetIPByIndex(netdef, AF_UNSPEC, 0);
- if (!ipdef) {
- virReportError(VIR_ERR_INTERNAL_ERROR,
- _("network '%s' doesn't have an IP
address"),
- netdef->name);
- goto cleanup;
- }
- addrptr = &ipdef->address;
- break;
-
- case VIR_NETWORK_FORWARD_BRIDGE:
- if ((dev_name = netdef->bridge))
- break;
- /*
- * fall through if netdef->bridge wasn't set, since that is
- * macvtap bridge mode network.
- */
- ATTRIBUTE_FALLTHROUGH;
-
- case VIR_NETWORK_FORWARD_PRIVATE:
- case VIR_NETWORK_FORWARD_VEPA:
- case VIR_NETWORK_FORWARD_PASSTHROUGH:
- if ((netdef->forward.nifs > 0) && netdef->forward.ifs)
- dev_name = netdef->forward.ifs[0].device.dev;
-
- if (!dev_name) {
- virReportError(VIR_ERR_INTERNAL_ERROR,
- _("network '%s' has no associated interface or
bridge"),
- netdef->name);
- goto cleanup;
- }
- break;
- }
-
- if (dev_name) {
- if (virNetDevIPAddrGet(dev_name, &addr) < 0)
- goto cleanup;
- addrptr = &addr;
- }
-
- if (!(addrptr &&
- (*netaddr = virSocketAddrFormat(addrptr)))) {
- goto cleanup;
- }
-
- ret = 0;
- cleanup:
- virNetworkObjEndAPI(&obj);
- return ret;
-}
-
/* networkGetActualType:
* @dom: domain definition that @iface belongs to
diff --git a/src/network/bridge_driver.h b/src/network/bridge_driver.h
index 098f9e5c5b..6fa6432d13 100644
--- a/src/network/bridge_driver.h
+++ b/src/network/bridge_driver.h
@@ -36,11 +36,6 @@ networkRegister(void);
# if WITH_NETWORK
-int
-networkGetNetworkAddress(const char *netname,
- char **netaddr)
- ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
-
int
networkGetActualType(virDomainNetDefPtr iface)
ATTRIBUTE_NONNULL(1);
@@ -55,7 +50,6 @@ networkDnsmasqConfContents(virNetworkObjPtr obj,
# else
/* Define no-op replacements that don't drag in any link dependencies. */
# define networkGetActualType(iface) (iface->type)
-# define networkGetNetworkAddress(netname, netaddr) (-2)
# define networkDnsmasqConfContents(network, pidfile, configstr, \
dctx, caps) 0
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index 29af643160..2c352523fe 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -4259,9 +4259,95 @@ qemuProcessGraphicsAllocatePorts(virQEMUDriverPtr driver,
return 0;
}
+static int
+qemuProcessGetNetworkAddress(virConnectPtr conn,
+ const char *netname,
+ char **netaddr)
+{
+ int ret = -1;
+ virNetworkPtr net;
+ virNetworkDefPtr netdef = NULL;
+ virNetworkIPDefPtr ipdef;
+ virSocketAddr addr;
+ virSocketAddrPtr addrptr = NULL;
+ char *dev_name = NULL;
+ char *xml = NULL;
+
+ *netaddr = NULL;
+ net = virNetworkLookupByName(conn, netname);
+ if (!net)
+ goto cleanup;
+
+ xml = virNetworkGetXMLDesc(net, 0);
+ if (!xml)
+ goto cleanup;
+
+ netdef = virNetworkDefParseString(xml);
+ if (!netdef)
+ goto cleanup;
+
+ switch (netdef->forward.type) {
+ case VIR_NETWORK_FORWARD_NONE:
+ case VIR_NETWORK_FORWARD_NAT:
+ case VIR_NETWORK_FORWARD_ROUTE:
+ case VIR_NETWORK_FORWARD_OPEN:
+ ipdef = virNetworkDefGetIPByIndex(netdef, AF_UNSPEC, 0);
+ if (!ipdef) {
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("network '%s' doesn't have an IP
address"),
+ netdef->name);
+ goto cleanup;
+ }
+ addrptr = &ipdef->address;
+ break;
+
+ case VIR_NETWORK_FORWARD_BRIDGE:
+ if ((dev_name = netdef->bridge))
+ break;
+ /*
+ * fall through if netdef->bridge wasn't set, since that is
+ * macvtap bridge mode network.
+ */
+ ATTRIBUTE_FALLTHROUGH;
+
+ case VIR_NETWORK_FORWARD_PRIVATE:
+ case VIR_NETWORK_FORWARD_VEPA:
+ case VIR_NETWORK_FORWARD_PASSTHROUGH:
+ if ((netdef->forward.nifs > 0) && netdef->forward.ifs)
+ dev_name = netdef->forward.ifs[0].device.dev;
+
+ if (!dev_name) {
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("network '%s' has no associated interface or
bridge"),
+ netdef->name);
+ goto cleanup;
+ }
+ break;
+ }
+
+ if (dev_name) {
+ if (virNetDevIPAddrGet(dev_name, &addr) < 0)
+ goto cleanup;
+ addrptr = &addr;
+ }
+
+ if (!(addrptr &&
+ (*netaddr = virSocketAddrFormat(addrptr)))) {
+ goto cleanup;
+ }
+
+ ret = 0;
+ cleanup:
+ virNetworkDefFree(netdef);
+ virObjectUnref(net);
+ VIR_FREE(xml);
+ return ret;
+}
+
static int
-qemuProcessGraphicsSetupNetworkAddress(virDomainGraphicsListenDefPtr glisten,
+qemuProcessGraphicsSetupNetworkAddress(virConnectPtr conn,
+ virDomainGraphicsListenDefPtr glisten,
const char *listenAddr)
{
int rc;
@@ -4273,7 +4359,7 @@ qemuProcessGraphicsSetupNetworkAddress(virDomainGraphicsListenDefPtr
glisten,
return 0;
}
- rc = networkGetNetworkAddress(glisten->network, &glisten->address);
+ rc = qemuProcessGetNetworkAddress(conn, glisten->network,
&glisten->address);
if (rc <= -2) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("network-based listen isn't possible, "
@@ -4288,7 +4374,8 @@ qemuProcessGraphicsSetupNetworkAddress(virDomainGraphicsListenDefPtr
glisten,
static int
-qemuProcessGraphicsSetupListen(virQEMUDriverPtr driver,
+qemuProcessGraphicsSetupListen(virConnectPtr conn,
+ virQEMUDriverPtr driver,
virDomainGraphicsDefPtr graphics,
virDomainObjPtr vm)
{
@@ -4346,7 +4433,8 @@ qemuProcessGraphicsSetupListen(virQEMUDriverPtr driver,
if (glisten->address || !listenAddr)
continue;
- if (qemuProcessGraphicsSetupNetworkAddress(glisten,
+ if (qemuProcessGraphicsSetupNetworkAddress(conn,
+ glisten,
listenAddr) < 0)
goto cleanup;
break;
@@ -4375,7 +4463,8 @@ qemuProcessGraphicsSetupListen(virQEMUDriverPtr driver,
static int
-qemuProcessSetupGraphics(virQEMUDriverPtr driver,
+qemuProcessSetupGraphics(virConnectPtr conn,
+ virQEMUDriverPtr driver,
virDomainObjPtr vm,
unsigned int flags)
{
@@ -4387,7 +4476,7 @@ qemuProcessSetupGraphics(virQEMUDriverPtr driver,
for (i = 0; i < vm->def->ngraphics; i++) {
graphics = vm->def->graphics[i];
- if (qemuProcessGraphicsSetupListen(driver, graphics, vm) < 0)
+ if (qemuProcessGraphicsSetupListen(conn, driver, graphics, vm) < 0)
goto cleanup;
}
@@ -5529,7 +5618,7 @@ qemuProcessPrepareDomain(virConnectPtr conn,
goto cleanup;
VIR_DEBUG("Setting graphics devices");
- if (qemuProcessSetupGraphics(driver, vm, flags) < 0)
+ if (qemuProcessSetupGraphics(conn, driver, vm, flags) < 0)
goto cleanup;
VIR_DEBUG("Create domain masterKey");
--
2.14.3