]> xenbits.xensource.com Git - libvirt.git/commitdiff
hypervisor: Introduce and use virDomainInterfaceVportRemove()
authorMichal Privoznik <mprivozn@redhat.com>
Thu, 11 Apr 2024 12:12:37 +0000 (14:12 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Fri, 12 Apr 2024 18:44:52 +0000 (20:44 +0200)
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>
src/hypervisor/domain_interface.c
src/hypervisor/domain_interface.h
src/libvirt_private.syms
src/lxc/lxc_driver.c
src/lxc/lxc_process.c
src/qemu/qemu_hotplug.c

index 853814fe78a36e12a92a644240589a43335a00e2..00034120655574a9baa5c1823c3f59d3b0cdcd0c 100644 (file)
@@ -374,6 +374,32 @@ virDomainInterfaceStopDevices(virDomainDef *def)
     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:
  * @def: domain definition
@@ -390,10 +416,8 @@ virDomainInterfaceDeleteDevice(virDomainDef *def,
                                bool priv_net_created,
                                char *stateDir)
 {
-    const virNetDevVPortProfile *vport = NULL;
     g_autoptr(virConnect) conn = NULL;
 
-    vport = virDomainNetGetActualVirtPortProfile(net);
     switch (virDomainNetGetActualType(net)) {
     case VIR_DOMAIN_NET_TYPE_DIRECT:
         if (priv_net_created) {
@@ -435,15 +459,7 @@ virDomainInterfaceDeleteDevice(virDomainDef *def,
     /* release the physical device (or any other resources used by
         * this interface in the network driver
         */
-    if (vport) {
-        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));
-        }
-    }
+    virDomainInterfaceVportRemove(net);
 
     /* kick the device out of the hostdev list too */
     virDomainNetRemoveHostdev(def, net);
index 3d15e000cc7f5d0984b76a89c3ed7428da26a081..8047fdadfa929b8fd1b8377bf7f243e32d52f1d6 100644 (file)
@@ -39,6 +39,7 @@ int virDomainInterfaceStartDevice(virDomainNetDef *net);
 int virDomainInterfaceStartDevices(virDomainDef *def);
 int virDomainInterfaceStopDevice(virDomainNetDef *net);
 int virDomainInterfaceStopDevices(virDomainDef *def);
+void virDomainInterfaceVportRemove(virDomainNetDef *net);
 void virDomainInterfaceDeleteDevice(virDomainDef *def,
                                 virDomainNetDef *net,
                                 bool priv_net_created,
index 84e30b711c67bf694a8d8908873c45d91fd8d741..8642305a8bf5b774492c33f30d35f093fb53004b 100644 (file)
@@ -1642,6 +1642,7 @@ virDomainInterfaceStartDevice;
 virDomainInterfaceStartDevices;
 virDomainInterfaceStopDevice;
 virDomainInterfaceStopDevices;
+virDomainInterfaceVportRemove;
 
 
 # hypervisor/virclosecallbacks.h
index 39992bdf96f0be9f3a110cc096d6cddc23359938..89ef9416aaf07f33895159e93b9bc32a2f6a5ee7 100644 (file)
@@ -70,6 +70,7 @@
 #include "virhostdev.h"
 #include "netdev_bandwidth_conf.h"
 #include "virutil.h"
+#include "domain_interface.h"
 
 #define VIR_FROM_THIS VIR_FROM_LXC
 
@@ -4042,7 +4043,6 @@ lxcDomainDetachDeviceNetLive(virDomainObj *vm,
     int detachidx, ret = -1;
     virDomainNetType actualType;
     virDomainNetDef *detach = NULL;
-    const virNetDevVPortProfile *vport = NULL;
     virErrorPtr save_err = NULL;
 
     if ((detachidx = virDomainNetFindIdx(vm->def, dev->data.net)) < 0)
@@ -4098,11 +4098,8 @@ lxcDomainDetachDeviceNetLive(virDomainObj *vm,
 
     virDomainConfNWFilterTeardown(detach);
 
-    vport = virDomainNetGetActualVirtPortProfile(detach);
-    if (vport && vport->virtPortType == VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH)
-        ignore_value(virNetDevOpenvswitchRemovePort(
-                        virDomainNetGetActualBridgeName(detach),
-                        detach->ifname));
+    virDomainInterfaceVportRemove(detach);
+
     ret = 0;
  cleanup:
     if (!ret) {
index bfdcefd01be5f5d35d78c8452ff1d4320e8ac7eb..30ff4eb3d09292f928159400c53a1f249f160cae 100644 (file)
@@ -49,6 +49,7 @@
 #include "virprocess.h"
 #include "netdev_bandwidth_conf.h"
 #include "virutil.h"
+#include "domain_interface.h"
 
 #define VIR_FROM_THIS VIR_FROM_LXC
 
@@ -149,7 +150,6 @@ static void virLXCProcessCleanup(virLXCDriver *driver,
 {
     size_t i;
     virLXCDomainObjPrivate *priv = vm->privateData;
-    const virNetDevVPortProfile *vport = NULL;
     g_autoptr(virLXCDriverConfig) cfg = virLXCDriverGetConfig(driver);
     g_autoptr(virConnect) conn = NULL;
 
@@ -210,13 +210,9 @@ static void virLXCProcessCleanup(virLXCDriver *driver,
 
     for (i = 0; i < vm->def->nnets; i++) {
         virDomainNetDef *iface = vm->def->nets[i];
-        vport = virDomainNetGetActualVirtPortProfile(iface);
+
         if (iface->ifname) {
-            if (vport &&
-                vport->virtPortType == VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH)
-                ignore_value(virNetDevOpenvswitchRemovePort(
-                                virDomainNetGetActualBridgeName(iface),
-                                iface->ifname));
+            virDomainInterfaceVportRemove(iface);
             ignore_value(virNetDevVethDelete(iface->ifname));
         }
         if (iface->type == VIR_DOMAIN_NET_TYPE_NETWORK) {
@@ -637,11 +633,9 @@ virLXCProcessSetupInterfaces(virLXCDriver *driver,
         virErrorPreserveLast(&save_err);
         for (i = 0; i < def->nnets; i++) {
             virDomainNetDef *iface = def->nets[i];
-            const virNetDevVPortProfile *vport = virDomainNetGetActualVirtPortProfile(iface);
-            if (vport && vport->virtPortType == VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH)
-                ignore_value(virNetDevOpenvswitchRemovePort(
-                                virDomainNetGetActualBridgeName(iface),
-                                iface->ifname));
+
+            virDomainInterfaceVportRemove(iface);
+
             if (iface->type == VIR_DOMAIN_NET_TYPE_NETWORK && netconn)
                 virDomainNetReleaseActualDevice(netconn, iface);
         }
index 62dc879ed483c9f6bea95a5850e2d7062ac6ee02..054053729cfa1e5d81b6b0b6d5ed20fb01d4c86c 100644 (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
 qemuDomainAttachNetDevice(virQEMUDriver *driver,
                           virDomainObj *vm,
@@ -1414,7 +1396,7 @@ qemuDomainAttachNetDevice(virQEMUDriver *driver,
                                                        cfg->stateDir);
             }
 
-            qemuDomainNetDeviceVportRemove(net);
+            virDomainInterfaceVportRemove(net);
         }
 
         if (teardownlabel &&
@@ -4895,7 +4877,7 @@ qemuDomainRemoveNetDevice(virQEMUDriver *driver,
             VIR_WARN("Unable to restore security label on vhostuser char device");
     }
 
-    qemuDomainNetDeviceVportRemove(net);
+    virDomainInterfaceVportRemove(net);
 
     if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK) {
         g_autoptr(virConnect) conn = virGetConnectNetwork();