提交 9069331b 编写于 作者: D Daniel P. Berrangé

qemu: replace networkGetNetworkAddress with public API calls

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: NDaniel P. Berrangé <berrange@redhat.com>
上级 1438aea4
......@@ -5146,104 +5146,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
......
......@@ -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
......
......@@ -60,7 +60,6 @@
#include "domain_audit.h"
#include "domain_nwfilter.h"
#include "locking/domain_lock.h"
#include "network/bridge_driver.h"
#include "viruuid.h"
#include "virprocess.h"
#include "virtime.h"
......@@ -4344,9 +4343,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;
......@@ -4358,7 +4443,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, "
......@@ -4373,7 +4458,8 @@ qemuProcessGraphicsSetupNetworkAddress(virDomainGraphicsListenDefPtr glisten,
static int
qemuProcessGraphicsSetupListen(virQEMUDriverPtr driver,
qemuProcessGraphicsSetupListen(virConnectPtr conn,
virQEMUDriverPtr driver,
virDomainGraphicsDefPtr graphics,
virDomainObjPtr vm)
{
......@@ -4431,7 +4517,8 @@ qemuProcessGraphicsSetupListen(virQEMUDriverPtr driver,
if (glisten->address || !listenAddr)
continue;
if (qemuProcessGraphicsSetupNetworkAddress(glisten,
if (qemuProcessGraphicsSetupNetworkAddress(conn,
glisten,
listenAddr) < 0)
goto cleanup;
break;
......@@ -4460,7 +4547,8 @@ qemuProcessGraphicsSetupListen(virQEMUDriverPtr driver,
static int
qemuProcessSetupGraphics(virQEMUDriverPtr driver,
qemuProcessSetupGraphics(virConnectPtr conn,
virQEMUDriverPtr driver,
virDomainObjPtr vm,
unsigned int flags)
{
......@@ -4472,7 +4560,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;
}
......@@ -5628,7 +5716,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");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册