[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH 27/27] 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.

Signed-off-by: Daniel P. Berrangé <berrange redhat com>
---
 src/conf/domain_conf.c      | 125 ++++++++++++++++++--------
 src/conf/domain_conf.h      |  21 +----
 src/network/bridge_driver.c | 172 ------------------------------------
 3 files changed, 90 insertions(+), 228 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index e384c60dfd..689a397e44 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -11022,6 +11022,7 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt,
     char *type = NULL;
     char *network = NULL;
     char *portgroup = NULL;
+    char *portid = NULL;
     char *bridge = NULL;
     char *dev = NULL;
     char *ifname = NULL;
@@ -11104,6 +11105,7 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt,
                        virXMLNodeNameEqual(cur, "source")) {
                 network = virXMLPropString(cur, "network");
                 portgroup = virXMLPropString(cur, "portgroup");
+                portid = virXMLPropString(cur, "portid");
             } else if (!internal &&
                        def->type == VIR_DOMAIN_NET_TYPE_INTERNAL &&
                        virXMLNodeNameEqual(cur, "source")) {
@@ -11312,12 +11314,20 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt,
                              "specified with <interface type='network'/>"));
             goto error;
         }
+        if (portid &&
+            virUUIDParse(portid, def->data.network.portid) < 0) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("Unable to parse port id '%s'"), portid);
+            goto error;
+        }
+
         def->data.network.name = network;
         network = NULL;
         def->data.network.portgroup = portgroup;
         portgroup = NULL;
         def->data.network.actual = actual;
         actual = NULL;
+
         break;
 
     case VIR_DOMAIN_NET_TYPE_VHOSTUSER:
@@ -11830,6 +11840,7 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt,
     VIR_FREE(macaddr);
     VIR_FREE(network);
     VIR_FREE(portgroup);
+    VIR_FREE(portid);
     VIR_FREE(address);
     VIR_FREE(port);
     VIR_FREE(vhostuser_type);
@@ -24941,6 +24952,11 @@ virDomainActualNetDefContentsFormat(virBufferPtr buf,
                                   def->data.network.name);
             virBufferEscapeString(buf, " portgroup='%s'",
                                   def->data.network.portgroup);
+            if (virUUIDIsValid(def->data.network.portid)) {
+                char uuidstr[VIR_UUID_STRING_BUFLEN];
+                virUUIDFormat(def->data.network.portid, uuidstr);
+                virBufferAsprintf(buf, " portid='%s'", uuidstr);
+            }
         }
         if (actualType == VIR_DOMAIN_NET_TYPE_BRIDGE ||
             actualType == VIR_DOMAIN_NET_TYPE_NETWORK) {
@@ -30519,6 +30535,7 @@ virDomainNetDefActualFromNetworkPort(virDomainNetDefPtr iface,
         if (VIR_STRDUP(actual->data.bridge.brname,
                        port->plug.bridge.brname) < 0)
             goto error;
+        actual->type = VIR_DOMAIN_NET_TYPE_BRIDGE;
         actual->data.bridge.macTableManager = port->plug.bridge.macTableManager;
         break;
 
@@ -30526,10 +30543,12 @@ virDomainNetDefActualFromNetworkPort(virDomainNetDefPtr iface,
         if (VIR_STRDUP(actual->data.direct.linkdev,
                        port->plug.direct.linkdev) < 0)
             goto error;
+        actual->type = VIR_DOMAIN_NET_TYPE_DIRECT;
         actual->data.direct.mode = port->plug.direct.mode;
         break;
 
     case VIR_NETWORK_PORT_PLUG_TYPE_HOSTDEV_PCI:
+        actual->type = VIR_DOMAIN_NET_TYPE_HOSTDEV;
         actual->data.hostdev.def.parent = iface;
         actual->data.hostdev.def.info = &iface->info;
         actual->data.hostdev.def.mode = VIR_DOMAIN_HOSTDEV_MODE_SUBSYS;
@@ -30707,48 +30726,82 @@ virDomainNetDefActualToNetworkPort(virDomainDefPtr dom,
     return NULL;
 }
 
-static virDomainNetAllocateActualDeviceImpl netAllocate;
-static virDomainNetNotifyActualDeviceImpl netNotify;
-static virDomainNetReleaseActualDeviceImpl netRelease;
 static virDomainNetBandwidthChangeAllowedImpl netBandwidthChangeAllowed;
 static virDomainNetBandwidthUpdateImpl netBandwidthUpdate;
 
 
 void
-virDomainNetSetDeviceImpl(virDomainNetAllocateActualDeviceImpl allocate,
-                          virDomainNetNotifyActualDeviceImpl notify,
-                          virDomainNetReleaseActualDeviceImpl release,
-                          virDomainNetBandwidthChangeAllowedImpl bandwidthChangeAllowed,
+virDomainNetSetDeviceImpl(virDomainNetBandwidthChangeAllowedImpl bandwidthChangeAllowed,
                           virDomainNetBandwidthUpdateImpl bandwidthUpdate)
 {
-    netAllocate = allocate;
-    netNotify = notify;
-    netRelease = release;
     netBandwidthChangeAllowed = bandwidthChangeAllowed;
     netBandwidthUpdate = bandwidthUpdate;
 }
 
-int
-virDomainNetAllocateActualDevice(virConnectPtr conn,
-                                 virDomainDefPtr dom,
-                                 virDomainNetDefPtr iface)
+
+static int
+virDomainNetCreatePort(virConnectPtr conn,
+                       virDomainDefPtr dom,
+                       virDomainNetDefPtr iface,
+                       unsigned int flags)
 {
     virNetworkPtr net = NULL;
     int ret = -1;
-
-    if (!netAllocate) {
-        virReportError(VIR_ERR_NO_SUPPORT, "%s",
-                       _("Virtual networking driver is not available"));
-        return -1;
-    }
+    virNetworkPortDefPtr portdef = NULL;
+    virNetworkPortPtr port = NULL;
+    char *portxml = NULL;
+    virErrorPtr saved;
 
     if (!(net = virNetworkLookupByName(conn, iface->data.network.name)))
         return -1;
 
-    ret = netAllocate(net, dom, iface);
+    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
+virDomainNetAllocateActualDevice(virConnectPtr conn,
+                                 virDomainDefPtr dom,
+                                 virDomainNetDefPtr iface)
+{
+    return virDomainNetCreatePort(conn, dom, iface, 0);
 }
 
 void
@@ -30756,36 +30809,34 @@ virDomainNetNotifyActualDevice(virConnectPtr conn,
                                virDomainDefPtr dom,
                                virDomainNetDefPtr iface)
 {
-    virNetworkPtr net = NULL;
-
-    if (!netNotify)
+    if (virUUIDIsValid(iface->data.network.portid))
         return;
 
-    if (!(net = virNetworkLookupByName(conn, iface->data.network.name)))
-        return;
-
-    netNotify(net, dom, iface);
-
-    virObjectUnref(net);
+    virDomainNetCreatePort(conn, dom, iface,
+                           VIR_NETWORK_PORT_CREATE_RECLAIM);
 }
 
 
 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;
+
+    if (!(port = virNetworkPortLookupByUUID(net, iface->data.network.portid)))
+        goto cleanup;
 
-    ret = netRelease(net, dom, iface);
+    if (virNetworkPortDelete(port, 0) < 0)
+        goto cleanup;
 
+ cleanup:
+    virObjectUnref(port);
     virObjectUnref(net);
     return ret;
 }
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 0f672d718e..1f271fde84 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -1027,6 +1027,7 @@ struct _virDomainNetDef {
         struct {
             char *name;
             char *portgroup;
+            unsigned char portid[VIR_UUID_BUFLEN];
             /* actual has info about the currently used physical
              * device (if the network is of type
              * bridge/private/vepa/passthrough). This is saved in the
@@ -3622,21 +3623,6 @@ virNetworkPortDefPtr
 virDomainNetDefActualToNetworkPort(virDomainDefPtr dom,
                                    virDomainNetDefPtr iface);
 
-typedef int
-(*virDomainNetAllocateActualDeviceImpl)(virNetworkPtr net,
-                                        virDomainDefPtr dom,
-                                        virDomainNetDefPtr iface);
-
-typedef void
-(*virDomainNetNotifyActualDeviceImpl)(virNetworkPtr net,
-                                      virDomainDefPtr dom,
-                                      virDomainNetDefPtr iface);
-
-typedef int
-(*virDomainNetReleaseActualDeviceImpl)(virNetworkPtr net,
-                                       virDomainDefPtr dom,
-                                       virDomainNetDefPtr iface);
-
 typedef bool
 (*virDomainNetBandwidthChangeAllowedImpl)(virDomainNetDefPtr iface,
                                           virNetDevBandwidthPtr newBandwidth);
@@ -3647,10 +3633,7 @@ typedef int
 
 
 void
-virDomainNetSetDeviceImpl(virDomainNetAllocateActualDeviceImpl allocate,
-                          virDomainNetNotifyActualDeviceImpl notify,
-                          virDomainNetReleaseActualDeviceImpl release,
-                          virDomainNetBandwidthChangeAllowedImpl bandwidthChangeAllowed,
+virDomainNetSetDeviceImpl(virDomainNetBandwidthChangeAllowedImpl bandwidthChangeAllowed,
                           virDomainNetBandwidthUpdateImpl bandwidthUpdate);
 
 int
diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c
index 8401b72bc8..a8d89a4b4d 100644
--- a/src/network/bridge_driver.c
+++ b/src/network/bridge_driver.c
@@ -4765,52 +4765,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 a 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
@@ -5007,72 +4961,6 @@ networkNotifyPort(virNetworkObjPtr obj,
 }
 
 
-static void
-networkNotifyActualDevice(virNetworkPtr net,
-                          virDomainDefPtr dom,
-                          virDomainNetDefPtr iface)
-{
-    virNetworkDriverStatePtr driver = networkGetDriver();
-    virDomainNetType actualType = virDomainNetGetActualType(iface);
-    virNetworkObjPtr obj;
-    virNetworkDefPtr netdef;
-    virNetworkPortDefPtr port = NULL;
-
-    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 a 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 we're restarting libvirtd after an upgrade from a version
-     * that didn't save bridge name in actualNetDef for
-     * actualType==network, we need to copy it in so that it will be
-     * available in all cases
-     */
-    if (actualType == VIR_DOMAIN_NET_TYPE_BRIDGE &&
-        !iface->data.network.actual->data.bridge.brname &&
-        (VIR_STRDUP(iface->data.network.actual->data.bridge.brname,
-                    netdef->bridge) < 0))
-            goto cleanup;
-
-    /* Older libvirtd uses actualType==network, but we now
-     * just use actualType==bridge, as nothing needs to
-     * distinguish the two cases, and this simplifies virt
-     * drive code */
-    if (actualType == VIR_DOMAIN_NET_TYPE_NETWORK) {
-        iface->data.network.actual->type = VIR_DOMAIN_NET_TYPE_BRIDGE;
-        actualType = VIR_DOMAIN_NET_TYPE_BRIDGE;
-    }
-
-    if (!(port = virDomainNetDefActualToNetworkPort(dom, iface)))
-        goto cleanup;
-
-    if (networkNotifyPort(obj, port) < 0)
-        goto cleanup;
-
- cleanup:
-    virNetworkObjEndAPI(&obj);
-    virNetworkPortDefFree(port);
-}
-
-
 /* networkReleasePort:
  * @obj: the network to release from
  * @port: the port definition to release
@@ -5188,63 +5076,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 a 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:
@@ -5966,9 +5797,6 @@ networkRegister(void)
         return -1;
 
     virDomainNetSetDeviceImpl(
-        networkAllocateActualDevice,
-        networkNotifyActualDevice,
-        networkReleaseActualDevice,
         networkBandwidthChangeAllowed,
         networkBandwidthUpdate);
 
-- 
2.19.2


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]