conf: switch over to use network port APIs for virt drivers

Change the domain conf so invoke the new network port public APIs instead
of the network callbacks.

Reviewed-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-24 10:45:00 +00:00
parent 90064d76bc
commit fe77197304
4 changed files with 154 additions and 293 deletions

View File

@ -30688,22 +30688,67 @@ virDomainNetDefActualToNetworkPort(virDomainDefPtr dom,
return NULL;
}
static virDomainNetAllocateActualDeviceImpl netAllocate;
static virDomainNetNotifyActualDeviceImpl netNotify;
static virDomainNetReleaseActualDeviceImpl netRelease;
static virDomainNetBandwidthUpdateImpl netBandwidthUpdate;
void
virDomainNetSetDeviceImpl(virDomainNetAllocateActualDeviceImpl allocate,
virDomainNetNotifyActualDeviceImpl notify,
virDomainNetReleaseActualDeviceImpl release,
virDomainNetBandwidthUpdateImpl bandwidthUpdate)
static int
virDomainNetCreatePort(virConnectPtr conn,
virDomainDefPtr dom,
virDomainNetDefPtr iface,
unsigned int flags)
{
netAllocate = allocate;
netNotify = notify;
netRelease = release;
netBandwidthUpdate = bandwidthUpdate;
virNetworkPtr net = NULL;
int ret = -1;
virNetworkPortDefPtr portdef = NULL;
virNetworkPortPtr port = NULL;
char *portxml = NULL;
virErrorPtr saved;
if (!(net = virNetworkLookupByName(conn, iface->data.network.name)))
return -1;
if (flags & VIR_NETWORK_PORT_CREATE_RECLAIM) {
if (!(portdef = virDomainNetDefActualToNetworkPort(dom, iface)))
goto cleanup;
} else {
if (!(portdef = virDomainNetDefToNetworkPort(dom, iface)))
goto cleanup;
}
if (!(portxml = virNetworkPortDefFormat(portdef)))
goto cleanup;
virNetworkPortDefFree(portdef);
portdef = NULL;
if (!(port = virNetworkPortCreateXML(net, portxml, flags)))
goto cleanup;
VIR_FREE(portxml);
if (!(portxml = virNetworkPortGetXMLDesc(port, 0)))
goto deleteport;
if (!(portdef = virNetworkPortDefParseString(portxml)))
goto deleteport;
if (virDomainNetDefActualFromNetworkPort(iface, portdef) < 0)
goto deleteport;
virNetworkPortGetUUID(port, iface->data.network.portid);
ret = 0;
cleanup:
virNetworkPortDefFree(portdef);
VIR_FREE(portxml);
virObjectUnref(port);
virObjectUnref(net);
return ret;
deleteport:
saved = virSaveLastError();
virNetworkPortDelete(port, 0);
virSetError(saved);
virFreeError(saved);
goto cleanup;
}
int
@ -30711,22 +30756,7 @@ virDomainNetAllocateActualDevice(virConnectPtr conn,
virDomainDefPtr dom,
virDomainNetDefPtr iface)
{
virNetworkPtr net = NULL;
int ret = -1;
if (!netAllocate) {
virReportError(VIR_ERR_NO_SUPPORT, "%s",
_("Virtual networking driver is not available"));
return -1;
}
if (!(net = virNetworkLookupByName(conn, iface->data.network.name)))
return -1;
ret = netAllocate(net, dom, iface);
virObjectUnref(net);
return ret;
return virDomainNetCreatePort(conn, dom, iface, 0);
}
void
@ -30734,16 +30764,11 @@ virDomainNetNotifyActualDevice(virConnectPtr conn,
virDomainDefPtr dom,
virDomainNetDefPtr iface)
{
virNetworkPtr net = NULL;
if (!netNotify)
return;
if (!(net = virNetworkLookupByName(conn, iface->data.network.name)))
return;
if (netNotify(net, dom, iface) < 0)
goto cleanup;
if (!virUUIDIsValid(iface->data.network.portid)) {
if (virDomainNetCreatePort(conn, dom, iface,
VIR_NETWORK_PORT_CREATE_RECLAIM) < 0)
return;
}
if (virDomainNetGetActualType(iface) == VIR_DOMAIN_NET_TYPE_BRIDGE) {
/*
@ -30751,52 +30776,118 @@ virDomainNetNotifyActualDevice(virConnectPtr conn,
* so there is no point in trying to learn the actualMTU
* (final arg to virNetDevTapReattachBridge())
*/
if (virNetDevTapReattachBridge(iface->ifname,
iface->data.network.actual->data.bridge.brname,
&iface->mac, dom->uuid,
virDomainNetGetActualVirtPortProfile(iface),
virDomainNetGetActualVlan(iface),
iface->mtu, NULL) < 0)
goto cleanup;
ignore_value(virNetDevTapReattachBridge(iface->ifname,
iface->data.network.actual->data.bridge.brname,
&iface->mac, dom->uuid,
virDomainNetGetActualVirtPortProfile(iface),
virDomainNetGetActualVlan(iface),
iface->mtu, NULL));
}
cleanup:
virObjectUnref(net);
}
int
virDomainNetReleaseActualDevice(virConnectPtr conn,
virDomainDefPtr dom,
virDomainDefPtr dom ATTRIBUTE_UNUSED,
virDomainNetDefPtr iface)
{
virNetworkPtr net = NULL;
int ret;
if (!netRelease)
return 0;
virNetworkPortPtr port = NULL;
int ret = -1;
if (!(net = virNetworkLookupByName(conn, iface->data.network.name)))
return -1;
goto cleanup;
ret = netRelease(net, dom, iface);
if (!(port = virNetworkPortLookupByUUID(net, iface->data.network.portid)))
goto cleanup;
if (virNetworkPortDelete(port, 0) < 0)
goto cleanup;
cleanup:
virObjectUnref(port);
virObjectUnref(net);
return ret;
}
static int
virDomainNetBandwidthToTypedParams(virNetDevBandwidthPtr bandwidth,
virTypedParameterPtr *params,
int *nparams)
{
int maxparams = 0;
if ((bandwidth->in != NULL) &&
(virTypedParamsAddUInt(params, nparams, &maxparams,
VIR_NETWORK_PORT_BANDWIDTH_IN_AVERAGE,
bandwidth->in->average) < 0 ||
virTypedParamsAddUInt(params, nparams, &maxparams,
VIR_NETWORK_PORT_BANDWIDTH_IN_PEAK,
bandwidth->in->peak) < 0 ||
virTypedParamsAddUInt(params, nparams, &maxparams,
VIR_NETWORK_PORT_BANDWIDTH_IN_FLOOR,
bandwidth->in->floor) < 0 ||
virTypedParamsAddUInt(params, nparams, &maxparams,
VIR_NETWORK_PORT_BANDWIDTH_IN_BURST,
bandwidth->in->burst) < 0))
goto error;
if ((bandwidth->out != NULL) &&
(virTypedParamsAddUInt(params, nparams, &maxparams,
VIR_NETWORK_PORT_BANDWIDTH_OUT_AVERAGE,
bandwidth->out->average) < 0 ||
virTypedParamsAddUInt(params, nparams, &maxparams,
VIR_NETWORK_PORT_BANDWIDTH_OUT_PEAK,
bandwidth->out->peak) < 0 ||
virTypedParamsAddUInt(params, nparams, &maxparams,
VIR_NETWORK_PORT_BANDWIDTH_OUT_BURST,
bandwidth->out->burst) < 0))
goto error;
return 0;
error:
virTypedParamsFree(*params, *nparams);
*params = NULL;
*nparams = 0;
return -1;
}
int
virDomainNetBandwidthUpdate(virDomainNetDefPtr iface,
virNetDevBandwidthPtr newBandwidth)
{
if (!netBandwidthUpdate) {
virReportError(VIR_ERR_NO_SUPPORT, "%s",
_("Virtual networking driver is not available"));
return -1;
}
virNetworkPtr net = NULL;
virNetworkPortPtr port = NULL;
virTypedParameterPtr params = NULL;
int nparams = 0;
virConnectPtr conn = NULL;
int ret = -1;
return netBandwidthUpdate(iface, newBandwidth);
if (!(conn = virGetConnectNetwork()))
goto cleanup;
if (!(net = virNetworkLookupByName(conn, iface->data.network.name)))
goto cleanup;
if (!(port = virNetworkPortLookupByUUID(net, iface->data.network.portid)))
goto cleanup;
if (virDomainNetBandwidthToTypedParams(newBandwidth, &params, &nparams) < 0)
goto cleanup;
if (virNetworkPortSetParameters(port, params, nparams, 0) < 0)
goto cleanup;
ret = 0;
cleanup:
virObjectUnref(conn);
virTypedParamsFree(params, nparams);
virObjectUnref(port);
virObjectUnref(net);
return ret;
}
/* virDomainNetResolveActualType:

View File

@ -3573,32 +3573,6 @@ virNetworkPortDefPtr
virDomainNetDefActualToNetworkPort(virDomainDefPtr dom,
virDomainNetDefPtr iface);
typedef int
(*virDomainNetAllocateActualDeviceImpl)(virNetworkPtr net,
virDomainDefPtr dom,
virDomainNetDefPtr iface);
typedef int
(*virDomainNetNotifyActualDeviceImpl)(virNetworkPtr net,
virDomainDefPtr dom,
virDomainNetDefPtr iface);
typedef int
(*virDomainNetReleaseActualDeviceImpl)(virNetworkPtr net,
virDomainDefPtr dom,
virDomainNetDefPtr iface);
typedef int
(*virDomainNetBandwidthUpdateImpl)(virDomainNetDefPtr iface,
virNetDevBandwidthPtr newBandwidth);
void
virDomainNetSetDeviceImpl(virDomainNetAllocateActualDeviceImpl allocate,
virDomainNetNotifyActualDeviceImpl notify,
virDomainNetReleaseActualDeviceImpl release,
virDomainNetBandwidthUpdateImpl bandwidthUpdate);
int
virDomainNetAllocateActualDevice(virConnectPtr conn,
virDomainDefPtr dom,

View File

@ -487,7 +487,6 @@ virDomainNetReleaseActualDevice;
virDomainNetRemove;
virDomainNetRemoveHostdev;
virDomainNetResolveActualType;
virDomainNetSetDeviceImpl;
virDomainNetSetModelString;
virDomainNetTypeFromString;
virDomainNetTypeSharesHostView;

View File

@ -4703,52 +4703,6 @@ networkAllocatePort(virNetworkObjPtr obj,
}
static int
networkAllocateActualDevice(virNetworkPtr net,
virDomainDefPtr dom,
virDomainNetDefPtr iface)
{
virNetworkDriverStatePtr driver = networkGetDriver();
virNetworkPortDefPtr port = NULL;
virNetworkObjPtr obj;
int ret = -1;
obj = virNetworkObjFindByName(driver->networks, net->name);
if (!obj) {
virReportError(VIR_ERR_NO_NETWORK,
_("no network with matching name '%s'"),
net->name);
return -1;
}
if (iface->type != VIR_DOMAIN_NET_TYPE_NETWORK) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Expected an interface for a virtual network"));
goto cleanup;
}
if (!(port = virDomainNetDefToNetworkPort(dom, iface)))
goto cleanup;
if (networkAllocatePort(obj, port) < 0)
goto cleanup;
VIR_DEBUG("Populating net def");
if (virDomainNetDefActualFromNetworkPort(iface, port) < 0)
goto cleanup;
ret = 0;
cleanup:
if (ret < 0) {
virDomainActualNetDefFree(iface->data.network.actual);
iface->data.network.actual = NULL;
}
virNetworkPortDefFree(port);
virNetworkObjEndAPI(&obj);
return ret;
}
/* networkNotifyPort:
* @obj: the network to notify
* @port: the port definition to notify
@ -4905,54 +4859,6 @@ networkNotifyPort(virNetworkObjPtr obj,
}
static int
networkNotifyActualDevice(virNetworkPtr net,
virDomainDefPtr dom,
virDomainNetDefPtr iface)
{
virNetworkDriverStatePtr driver = networkGetDriver();
virNetworkObjPtr obj;
virNetworkDefPtr netdef;
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;
}
netdef = virNetworkObjGetDef(obj);
if (!virNetworkObjIsActive(obj)) {
virReportError(VIR_ERR_OPERATION_INVALID,
_("network '%s' is not active"),
netdef->name);
goto cleanup;
}
if (!(port = virDomainNetDefActualToNetworkPort(dom, iface)))
goto cleanup;
if (networkNotifyPort(obj, port) < 0)
goto cleanup;
ret = 0;
cleanup:
virNetworkObjEndAPI(&obj);
virNetworkPortDefFree(port);
return ret;
}
/* networkReleasePort:
* @obj: the network to release from
* @port: the port definition to release
@ -5069,65 +4975,6 @@ networkReleasePort(virNetworkObjPtr obj,
}
/* 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);
if (iface->type == VIR_DOMAIN_NET_TYPE_NETWORK) {
virDomainActualNetDefFree(iface->data.network.actual);
iface->data.network.actual = NULL;
}
virNetworkPortDefFree(port);
return ret;
}
/**
* networkCheckBandwidth:
* @net: network QoS
@ -5500,49 +5347,6 @@ networkUpdatePortBandwidth(virNetworkObjPtr obj,
}
static int
networkBandwidthUpdate(virDomainNetDefPtr iface,
virNetDevBandwidthPtr newBandwidth)
{
virNetworkDriverStatePtr driver = networkGetDriver();
virNetworkObjPtr obj = NULL;
virNetDevBandwidthPtr oldBandwidth = virDomainNetGetActualBandwidth(iface);
int ret = -1;
if (iface->type != VIR_DOMAIN_NET_TYPE_NETWORK) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Expected an interface for a virtual network"));
return -1;
}
if (virDomainNetGetActualType(iface) != VIR_DOMAIN_NET_TYPE_NETWORK &&
(virDomainNetGetActualType(iface) != VIR_DOMAIN_NET_TYPE_BRIDGE ||
iface->data.network.actual->data.bridge.brname != NULL)) {
/* This is not an interface that's plugged into a bridge.
* We don't care. Thus from our POV bandwidth change is allowed. */
return 0;
}
obj = virNetworkObjFindByName(driver->networks, iface->data.network.name);
if (!obj) {
virReportError(VIR_ERR_NO_NETWORK,
_("no network with matching name '%s'"),
iface->data.network.name);
return ret;
}
ret = networkUpdatePortBandwidth(obj,
&iface->mac,
iface->data.network.actual ?
&iface->data.network.actual->class_id : NULL,
newBandwidth,
oldBandwidth);
virNetworkObjEndAPI(&obj);
return ret;
}
static virNetworkPortPtr
networkPortLookupByUUID(virNetworkPtr net,
const unsigned char *uuid)
@ -5999,12 +5803,5 @@ networkRegister(void)
return -1;
if (virRegisterStateDriver(&networkStateDriver) < 0)
return -1;
virDomainNetSetDeviceImpl(
networkAllocateActualDevice,
networkNotifyActualDevice,
networkReleaseActualDevice,
networkBandwidthUpdate);
return 0;
}