network: introduce networkReleasePort

Separate network port deletion code from the domain driver network
callback implementation.

Reivewed-by: Laine Stump <laine@laine.org>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2018-12-20 14:01:18 +00:00
parent 8ba66562f2
commit 73c6c4e438

View File

@ -4948,9 +4948,9 @@ networkNotifyActualDevice(virNetworkPtr net,
}
/* networkReleaseActualDevice:
* @dom: domain definition that @iface belongs to
* @iface: a domain's NetDef (interface definition)
/* networkReleasePort:
* @obj: the network to release from
* @port: the port definition to release
*
* Given a domain <interface> element that previously had its <actual>
* element filled in (and possibly a physical device allocated to it),
@ -4960,40 +4960,15 @@ networkNotifyActualDevice(virNetworkPtr net,
* Returns 0 on success, -1 on failure.
*/
static int
networkReleaseActualDevice(virNetworkPtr net,
virDomainDefPtr dom,
virDomainNetDefPtr iface)
networkReleasePort(virNetworkObjPtr obj,
virNetworkPortDefPtr port)
{
virNetworkDriverStatePtr driver = networkGetDriver();
virNetworkObjPtr obj;
virNetworkDefPtr netdef;
virNetworkForwardIfDefPtr dev = NULL;
virNetworkPortDefPtr port = NULL;
size_t i;
int ret = -1;
obj = virNetworkObjFindByName(driver->networks, net->name);
if (!obj) {
virReportError(VIR_ERR_NO_NETWORK,
_("no network with matching name '%s'"),
net->name);
goto cleanup;
}
if (iface->type != VIR_DOMAIN_NET_TYPE_NETWORK) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Expected an interface for a virtual network"));
goto cleanup;
}
if (iface->data.network.actual == NULL) {
ret = 0;
goto cleanup;
}
if (!(port = virDomainNetDefActualToNetworkPort(dom, iface)))
goto cleanup;
netdef = virNetworkObjGetDef(obj);
switch ((virNetworkPortPlugType)port->plugtype) {
@ -5073,7 +5048,7 @@ networkReleaseActualDevice(virNetworkPtr net,
goto cleanup;
}
virNetworkObjMacMgrDel(obj, driver->dnsmasqStateDir, dom->name, &port->mac);
virNetworkObjMacMgrDel(obj, driver->dnsmasqStateDir, port->ownername, &port->mac);
netdef->connections--;
if (dev)
@ -5083,6 +5058,59 @@ networkReleaseActualDevice(virNetworkPtr net,
VIR_HOOK_SUBOP_BEGIN);
networkLogAllocation(netdef, dev, &port->mac, false);
ret = 0;
cleanup:
return ret;
}
/* networkReleaseActualDevice:
* @dom: domain definition that @iface belongs to
* @iface: a domain's NetDef (interface definition)
*
* Given a domain <interface> element that previously had its <actual>
* element filled in (and possibly a physical device allocated to it),
* free up the physical device for use by someone else, and free the
* virDomainActualNetDef.
*
* Returns 0 on success, -1 on failure.
*/
static int
networkReleaseActualDevice(virNetworkPtr net,
virDomainDefPtr dom,
virDomainNetDefPtr iface)
{
virNetworkDriverStatePtr driver = networkGetDriver();
virNetworkObjPtr obj;
virNetworkPortDefPtr port = NULL;
int ret = -1;
obj = virNetworkObjFindByName(driver->networks, net->name);
if (!obj) {
virReportError(VIR_ERR_NO_NETWORK,
_("no network with matching name '%s'"),
net->name);
goto cleanup;
}
if (iface->type != VIR_DOMAIN_NET_TYPE_NETWORK) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Expected an interface for a virtual network"));
goto cleanup;
}
if (iface->data.network.actual == NULL) {
ret = 0;
goto cleanup;
}
if (!(port = virDomainNetDefActualToNetworkPort(dom, iface)))
goto cleanup;
if (networkReleasePort(obj, port) < 0)
goto cleanup;
ret = 0;
cleanup:
virNetworkObjEndAPI(&obj);