hypervisor: Introduce and use virDomainInterfaceVportRemove()

Both LXC and QEMU drivers have the same code to remove vport when
removing a domain's interface. Instead of repeating the same
pattern in both drivers, move the code into hypervisor agnostic
location (src/hypervisor/) and switch to calling this new
function.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
This commit is contained in:
Michal Privoznik 2024-04-11 14:12:37 +02:00
parent 767d92f098
commit f8b5bd855f
6 changed files with 40 additions and 49 deletions

View File

@ -374,6 +374,32 @@ virDomainInterfaceStopDevices(virDomainDef *def)
return 0; return 0;
} }
/**
* virDomainInterfaceVportRemove:
* @net: a net definition in the VM
*
* Removes vport profile from corresponding bridge.
* NOP if no vport profile is present in @net.
*/
void
virDomainInterfaceVportRemove(virDomainNetDef *net)
{
const virNetDevVPortProfile *vport = virDomainNetGetActualVirtPortProfile(net);
const char *brname;
if (!vport)
return;
if (vport->virtPortType == VIR_NETDEV_VPORT_PROFILE_MIDONET) {
ignore_value(virNetDevMidonetUnbindPort(vport));
} else if (vport->virtPortType == VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH) {
brname = virDomainNetGetActualBridgeName(net);
ignore_value(virNetDevOpenvswitchRemovePort(brname, net->ifname));
}
}
/** /**
* virDomainInterfaceDeleteDevice: * virDomainInterfaceDeleteDevice:
* @def: domain definition * @def: domain definition
@ -390,10 +416,8 @@ virDomainInterfaceDeleteDevice(virDomainDef *def,
bool priv_net_created, bool priv_net_created,
char *stateDir) char *stateDir)
{ {
const virNetDevVPortProfile *vport = NULL;
g_autoptr(virConnect) conn = NULL; g_autoptr(virConnect) conn = NULL;
vport = virDomainNetGetActualVirtPortProfile(net);
switch (virDomainNetGetActualType(net)) { switch (virDomainNetGetActualType(net)) {
case VIR_DOMAIN_NET_TYPE_DIRECT: case VIR_DOMAIN_NET_TYPE_DIRECT:
if (priv_net_created) { if (priv_net_created) {
@ -435,15 +459,7 @@ virDomainInterfaceDeleteDevice(virDomainDef *def,
/* release the physical device (or any other resources used by /* release the physical device (or any other resources used by
* this interface in the network driver * this interface in the network driver
*/ */
if (vport) { virDomainInterfaceVportRemove(net);
if (vport->virtPortType == VIR_NETDEV_VPORT_PROFILE_MIDONET) {
ignore_value(virNetDevMidonetUnbindPort(vport));
} else if (vport->virtPortType == VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH) {
ignore_value(virNetDevOpenvswitchRemovePort(
virDomainNetGetActualBridgeName(net),
net->ifname));
}
}
/* kick the device out of the hostdev list too */ /* kick the device out of the hostdev list too */
virDomainNetRemoveHostdev(def, net); virDomainNetRemoveHostdev(def, net);

View File

@ -39,6 +39,7 @@ int virDomainInterfaceStartDevice(virDomainNetDef *net);
int virDomainInterfaceStartDevices(virDomainDef *def); int virDomainInterfaceStartDevices(virDomainDef *def);
int virDomainInterfaceStopDevice(virDomainNetDef *net); int virDomainInterfaceStopDevice(virDomainNetDef *net);
int virDomainInterfaceStopDevices(virDomainDef *def); int virDomainInterfaceStopDevices(virDomainDef *def);
void virDomainInterfaceVportRemove(virDomainNetDef *net);
void virDomainInterfaceDeleteDevice(virDomainDef *def, void virDomainInterfaceDeleteDevice(virDomainDef *def,
virDomainNetDef *net, virDomainNetDef *net,
bool priv_net_created, bool priv_net_created,

View File

@ -1642,6 +1642,7 @@ virDomainInterfaceStartDevice;
virDomainInterfaceStartDevices; virDomainInterfaceStartDevices;
virDomainInterfaceStopDevice; virDomainInterfaceStopDevice;
virDomainInterfaceStopDevices; virDomainInterfaceStopDevices;
virDomainInterfaceVportRemove;
# hypervisor/virclosecallbacks.h # hypervisor/virclosecallbacks.h

View File

@ -70,6 +70,7 @@
#include "virhostdev.h" #include "virhostdev.h"
#include "netdev_bandwidth_conf.h" #include "netdev_bandwidth_conf.h"
#include "virutil.h" #include "virutil.h"
#include "domain_interface.h"
#define VIR_FROM_THIS VIR_FROM_LXC #define VIR_FROM_THIS VIR_FROM_LXC
@ -4042,7 +4043,6 @@ lxcDomainDetachDeviceNetLive(virDomainObj *vm,
int detachidx, ret = -1; int detachidx, ret = -1;
virDomainNetType actualType; virDomainNetType actualType;
virDomainNetDef *detach = NULL; virDomainNetDef *detach = NULL;
const virNetDevVPortProfile *vport = NULL;
virErrorPtr save_err = NULL; virErrorPtr save_err = NULL;
if ((detachidx = virDomainNetFindIdx(vm->def, dev->data.net)) < 0) if ((detachidx = virDomainNetFindIdx(vm->def, dev->data.net)) < 0)
@ -4098,11 +4098,8 @@ lxcDomainDetachDeviceNetLive(virDomainObj *vm,
virDomainConfNWFilterTeardown(detach); virDomainConfNWFilterTeardown(detach);
vport = virDomainNetGetActualVirtPortProfile(detach); virDomainInterfaceVportRemove(detach);
if (vport && vport->virtPortType == VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH)
ignore_value(virNetDevOpenvswitchRemovePort(
virDomainNetGetActualBridgeName(detach),
detach->ifname));
ret = 0; ret = 0;
cleanup: cleanup:
if (!ret) { if (!ret) {

View File

@ -49,6 +49,7 @@
#include "virprocess.h" #include "virprocess.h"
#include "netdev_bandwidth_conf.h" #include "netdev_bandwidth_conf.h"
#include "virutil.h" #include "virutil.h"
#include "domain_interface.h"
#define VIR_FROM_THIS VIR_FROM_LXC #define VIR_FROM_THIS VIR_FROM_LXC
@ -149,7 +150,6 @@ static void virLXCProcessCleanup(virLXCDriver *driver,
{ {
size_t i; size_t i;
virLXCDomainObjPrivate *priv = vm->privateData; virLXCDomainObjPrivate *priv = vm->privateData;
const virNetDevVPortProfile *vport = NULL;
g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver); g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver);
g_autoptr(virConnect) conn = NULL; g_autoptr(virConnect) conn = NULL;
@ -210,13 +210,9 @@ static void virLXCProcessCleanup(virLXCDriver *driver,
for (i = 0; i < vm->def->nnets; i++) { for (i = 0; i < vm->def->nnets; i++) {
virDomainNetDef *iface = vm->def->nets[i]; virDomainNetDef *iface = vm->def->nets[i];
vport = virDomainNetGetActualVirtPortProfile(iface);
if (iface->ifname) { if (iface->ifname) {
if (vport && virDomainInterfaceVportRemove(iface);
vport->virtPortType == VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH)
ignore_value(virNetDevOpenvswitchRemovePort(
virDomainNetGetActualBridgeName(iface),
iface->ifname));
ignore_value(virNetDevVethDelete(iface->ifname)); ignore_value(virNetDevVethDelete(iface->ifname));
} }
if (iface->type == VIR_DOMAIN_NET_TYPE_NETWORK) { if (iface->type == VIR_DOMAIN_NET_TYPE_NETWORK) {
@ -637,11 +633,9 @@ virLXCProcessSetupInterfaces(virLXCDriver *driver,
virErrorPreserveLast(&save_err); virErrorPreserveLast(&save_err);
for (i = 0; i < def->nnets; i++) { for (i = 0; i < def->nnets; i++) {
virDomainNetDef *iface = def->nets[i]; virDomainNetDef *iface = def->nets[i];
const virNetDevVPortProfile *vport = virDomainNetGetActualVirtPortProfile(iface);
if (vport && vport->virtPortType == VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH) virDomainInterfaceVportRemove(iface);
ignore_value(virNetDevOpenvswitchRemovePort(
virDomainNetGetActualBridgeName(iface),
iface->ifname));
if (iface->type == VIR_DOMAIN_NET_TYPE_NETWORK && netconn) if (iface->type == VIR_DOMAIN_NET_TYPE_NETWORK && netconn)
virDomainNetReleaseActualDevice(netconn, iface); virDomainNetReleaseActualDevice(netconn, iface);
} }

View File

@ -1091,24 +1091,6 @@ qemuDomainAttachDeviceDiskLive(virQEMUDriver *driver,
} }
static void
qemuDomainNetDeviceVportRemove(virDomainNetDef *net)
{
const virNetDevVPortProfile *vport = virDomainNetGetActualVirtPortProfile(net);
const char *brname;
if (!vport)
return;
if (vport->virtPortType == VIR_NETDEV_VPORT_PROFILE_MIDONET) {
ignore_value(virNetDevMidonetUnbindPort(vport));
} else if (vport->virtPortType == VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH) {
brname = virDomainNetGetActualBridgeName(net);
ignore_value(virNetDevOpenvswitchRemovePort(brname, net->ifname));
}
}
static int static int
qemuDomainAttachNetDevice(virQEMUDriver *driver, qemuDomainAttachNetDevice(virQEMUDriver *driver,
virDomainObj *vm, virDomainObj *vm,
@ -1414,7 +1396,7 @@ qemuDomainAttachNetDevice(virQEMUDriver *driver,
cfg->stateDir); cfg->stateDir);
} }
qemuDomainNetDeviceVportRemove(net); virDomainInterfaceVportRemove(net);
} }
if (teardownlabel && if (teardownlabel &&
@ -4895,7 +4877,7 @@ qemuDomainRemoveNetDevice(virQEMUDriver *driver,
VIR_WARN("Unable to restore security label on vhostuser char device"); VIR_WARN("Unable to restore security label on vhostuser char device");
} }
qemuDomainNetDeviceVportRemove(net); virDomainInterfaceVportRemove(net);
if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK) { if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK) {
g_autoptr(virConnect) conn = virGetConnectNetwork(); g_autoptr(virConnect) conn = virGetConnectNetwork();