[PATCH v3 11/14] vbox: Rewrite vboxNetworkUndefineDestroy

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

 



This patch rewrites two public APIs. They are vboxNetworkUndefine
and vboxNetworkDestroy. They use the same core function
vboxNetworkUndefineDestroy. I merged it in one patch.
---
 src/vbox/vbox_network.c       |  109 +++++++++++++++++++++++++++++++++
 src/vbox/vbox_tmpl.c          |  136 ++++++++++++-----------------------------
 src/vbox/vbox_uniformed_api.h |    7 +++
 3 files changed, 156 insertions(+), 96 deletions(-)

diff --git a/src/vbox/vbox_network.c b/src/vbox/vbox_network.c
index feecf0c..60fbe5e 100644
--- a/src/vbox/vbox_network.c
+++ b/src/vbox/vbox_network.c
@@ -34,6 +34,9 @@
 
 VIR_LOG_INIT("vbox.vbox_network");
 
+#define RC_SUCCEEDED(rc) NS_SUCCEEDED(rc.resultCode)
+#define RC_FAILED(rc) NS_FAILED(rc.resultCode)
+
 #define VBOX_UTF16_FREE(arg)                                            \
     do {                                                                \
         if (arg) {                                                      \
@@ -625,3 +628,109 @@ virNetworkPtr vboxNetworkDefineXML(virConnectPtr conn, const char *xml)
 {
     return vboxNetworkDefineCreateXML(conn, xml, false);
 }
+
+static int
+vboxNetworkUndefineDestroy(virNetworkPtr network, bool removeinterface)
+{
+    vboxGlobalData *data = network->conn->privateData;
+    char *networkNameUtf8 = NULL;
+    PRUnichar *networkInterfaceNameUtf16 = NULL;
+    IHostNetworkInterface *networkInterface = NULL;
+    PRUnichar *networkNameUtf16 = NULL;
+    IDHCPServer *dhcpServer = NULL;
+    PRUint32 interfaceType = 0;
+    IHost *host = NULL;
+    int ret = -1;
+
+    if (!data->vboxObj)
+        return ret;
+
+    gVBoxAPI.UIVirtualBox.GetHost(data->vboxObj, &host);
+    if (!host)
+        return ret;
+
+    /* Current limitation of the function for VirtualBox 2.2.* is
+     * that you can't delete the default hostonly adaptor namely:
+     * vboxnet0 and thus all this functions does is remove the
+     * dhcp server configuration, but the network can still be used
+     * by giving the machine static IP and also it will still
+     * show up in the net-list in virsh
+     */
+
+    if (virAsprintf(&networkNameUtf8, "HostInterfaceNetworking-%s", network->name) < 0)
+        goto cleanup;
+
+    VBOX_UTF8_TO_UTF16(network->name, &networkInterfaceNameUtf16);
+
+    gVBoxAPI.UIHost.FindHostNetworkInterfaceByName(host, networkInterfaceNameUtf16, &networkInterface);
+
+    if (!networkInterface)
+        goto cleanup;
+
+    gVBoxAPI.UIHNInterface.GetInterfaceType(networkInterface, &interfaceType);
+
+    if (interfaceType != HostNetworkInterfaceType_HostOnly)
+        goto cleanup;
+
+    if (gVBoxAPI.networkRemoveInterface && removeinterface) {
+        vboxIIDUnion iid;
+        IProgress *progress = NULL;
+        nsresult rc;
+        resultCodeUnion resultCode;
+
+        VBOX_IID_INITIALIZE(&iid);
+        rc = gVBoxAPI.UIHNInterface.GetId(networkInterface, &iid);
+
+        if (NS_FAILED(rc))
+            goto cleanup;
+
+        gVBoxAPI.UIHost.RemoveHostOnlyNetworkInterface(host, &iid, &progress);
+        vboxIIDUnalloc(&iid);
+
+        if (!progress)
+            goto cleanup;
+
+        gVBoxAPI.UIProgress.WaitForCompletion(progress, -1);
+        gVBoxAPI.UIProgress.GetResultCode(progress, &resultCode);
+        if (RC_FAILED(resultCode)) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("Error while removing hostonly network interface, rc=%08x"),
+                           resultCode.uResultCode);
+            goto cleanup;
+        }
+        VBOX_RELEASE(progress);
+    }
+
+    VBOX_UTF8_TO_UTF16(networkNameUtf8, &networkNameUtf16);
+
+    gVBoxAPI.UIVirtualBox.FindDHCPServerByNetworkName(data->vboxObj,
+                                                      networkNameUtf16,
+                                                      &dhcpServer);
+    if (!dhcpServer)
+        goto cleanup;
+
+    gVBoxAPI.UIDHCPServer.SetEnabled(dhcpServer, PR_FALSE);
+    gVBoxAPI.UIDHCPServer.Stop(dhcpServer);
+    if (removeinterface)
+        gVBoxAPI.UIVirtualBox.RemoveDHCPServer(data->vboxObj, dhcpServer);
+    ret = 0;
+    VBOX_RELEASE(dhcpServer);
+
+ cleanup:
+    VBOX_UTF16_FREE(networkNameUtf16);
+    VBOX_RELEASE(networkInterface);
+    VBOX_UTF16_FREE(networkInterfaceNameUtf16);
+    VBOX_RELEASE(host);
+    VIR_FREE(networkNameUtf8);
+    return ret;
+}
+
+int vboxNetworkUndefine(virNetworkPtr network)
+{
+    return vboxNetworkUndefineDestroy(network, true);
+}
+
+int vboxNetworkDestroy(virNetworkPtr network)
+{
+    return vboxNetworkUndefineDestroy(network, false);
+}
diff --git a/src/vbox/vbox_tmpl.c b/src/vbox/vbox_tmpl.c
index 6733021..c072246 100644
--- a/src/vbox/vbox_tmpl.c
+++ b/src/vbox/vbox_tmpl.c
@@ -2042,97 +2042,6 @@ _registerDomainEvent(virDriverPtr driver)
  * The Network Functions here on
  */
 
-static int
-vboxNetworkUndefineDestroy(virNetworkPtr network, bool removeinterface)
-{
-    VBOX_OBJECT_HOST_CHECK(network->conn, int, -1);
-    char *networkNameUtf8 = NULL;
-    PRUnichar *networkInterfaceNameUtf16    = NULL;
-    IHostNetworkInterface *networkInterface = NULL;
-
-    /* Current limitation of the function for VirtualBox 2.2.* is
-     * that you can't delete the default hostonly adaptor namely:
-     * vboxnet0 and thus all this functions does is remove the
-     * dhcp server configuration, but the network can still be used
-     * by giving the machine static IP and also it will still
-     * show up in the net-list in virsh
-     */
-
-    if (virAsprintf(&networkNameUtf8, "HostInterfaceNetworking-%s", network->name) < 0)
-        goto cleanup;
-
-    VBOX_UTF8_TO_UTF16(network->name, &networkInterfaceNameUtf16);
-
-    host->vtbl->FindHostNetworkInterfaceByName(host, networkInterfaceNameUtf16, &networkInterface);
-
-    if (networkInterface) {
-        PRUint32 interfaceType = 0;
-
-        networkInterface->vtbl->GetInterfaceType(networkInterface, &interfaceType);
-
-        if (interfaceType == HostNetworkInterfaceType_HostOnly) {
-            PRUnichar *networkNameUtf16 = NULL;
-            IDHCPServer *dhcpServer     = NULL;
-
-#if VBOX_API_VERSION != 2002000
-            if (removeinterface) {
-                PRUnichar *iidUtf16 = NULL;
-                IProgress *progress = NULL;
-
-                networkInterface->vtbl->GetId(networkInterface, &iidUtf16);
-
-                if (iidUtf16) {
-# if VBOX_API_VERSION == 3000000
-                    IHostNetworkInterface *netInt = NULL;
-                    host->vtbl->RemoveHostOnlyNetworkInterface(host, iidUtf16, &netInt, &progress);
-                    VBOX_RELEASE(netInt);
-# else  /* VBOX_API_VERSION > 3000000 */
-                    host->vtbl->RemoveHostOnlyNetworkInterface(host, iidUtf16, &progress);
-# endif /* VBOX_API_VERSION > 3000000 */
-                    VBOX_UTF16_FREE(iidUtf16);
-                }
-
-                if (progress) {
-                    progress->vtbl->WaitForCompletion(progress, -1);
-                    VBOX_RELEASE(progress);
-                }
-            }
-#endif /* VBOX_API_VERSION != 2002000 */
-
-            VBOX_UTF8_TO_UTF16(networkNameUtf8, &networkNameUtf16);
-
-            data->vboxObj->vtbl->FindDHCPServerByNetworkName(data->vboxObj,
-                                                             networkNameUtf16,
-                                                             &dhcpServer);
-            if (dhcpServer) {
-                dhcpServer->vtbl->SetEnabled(dhcpServer, PR_FALSE);
-                dhcpServer->vtbl->Stop(dhcpServer);
-                if (removeinterface)
-                    data->vboxObj->vtbl->RemoveDHCPServer(data->vboxObj, dhcpServer);
-                VBOX_RELEASE(dhcpServer);
-            }
-
-            VBOX_UTF16_FREE(networkNameUtf16);
-
-        }
-        VBOX_RELEASE(networkInterface);
-    }
-
-    VBOX_UTF16_FREE(networkInterfaceNameUtf16);
-    VBOX_RELEASE(host);
-
-    ret = 0;
-
- cleanup:
-    VIR_FREE(networkNameUtf8);
-    return ret;
-}
-
-static int vboxNetworkUndefine(virNetworkPtr network)
-{
-    return vboxNetworkUndefineDestroy(network, true);
-}
-
 static int vboxNetworkCreate(virNetworkPtr network)
 {
     VBOX_OBJECT_HOST_CHECK(network->conn, int, -1);
@@ -2201,11 +2110,6 @@ static int vboxNetworkCreate(virNetworkPtr network)
     return ret;
 }
 
-static int vboxNetworkDestroy(virNetworkPtr network)
-{
-    return vboxNetworkUndefineDestroy(network, false);
-}
-
 static char *vboxNetworkGetXMLDesc(virNetworkPtr network,
                                    unsigned int flags)
 {
@@ -4222,6 +4126,12 @@ _virtualboxCreateDHCPServer(IVirtualBox *vboxObj, PRUnichar *name, IDHCPServer *
 }
 
 static nsresult
+_virtualboxRemoveDHCPServer(IVirtualBox *vboxObj, IDHCPServer *server)
+{
+    return vboxObj->vtbl->RemoveDHCPServer(vboxObj, server);
+}
+
+static nsresult
 _machineAddStorageController(IMachine *machine, PRUnichar *name,
                              PRUint32 connectionType,
                              IStorageController **controller)
@@ -5642,6 +5552,25 @@ _hostCreateHostOnlyNetworkInterface(vboxGlobalData *data ATTRIBUTE_UNUSED,
 }
 
 static nsresult
+_hostRemoveHostOnlyNetworkInterface(IHost *host ATTRIBUTE_UNUSED,
+                                    vboxIIDUnion *iidu ATTRIBUTE_UNUSED,
+                                    IProgress **progress ATTRIBUTE_UNUSED)
+{
+#if VBOX_API_VERSION == 2002000
+    vboxUnsupported();
+    return 0;
+#elif VBOX_API_VERSION == 3000000
+    nsresult rc;
+    IHostNetworkInterface *netInt = NULL;
+    rc = host->vtbl->RemoveHostOnlyNetworkInterface(host, IID_MEMBER(value), &netInt, progress);
+    VBOX_RELEASE(netInt);
+    return rc;
+#else  /* VBOX_API_VERSION > 3000000 */
+    return host->vtbl->RemoveHostOnlyNetworkInterface(host, IID_MEMBER(value), progress);
+#endif /* VBOX_API_VERSION > 3000000 */
+}
+
+static nsresult
 _hnInterfaceGetInterfaceType(IHostNetworkInterface *hni, PRUint32 *interfaceType)
 {
     return hni->vtbl->GetInterfaceType(hni, interfaceType);
@@ -5720,6 +5649,12 @@ _dhcpServerStart(IDHCPServer *dhcpServer, PRUnichar *networkName,
                                    trunkName, trunkType);
 }
 
+static nsresult
+_dhcpServerStop(IDHCPServer *dhcpServer)
+{
+    return dhcpServer->vtbl->Stop(dhcpServer);
+}
+
 static bool _machineStateOnline(PRUint32 state)
 {
     return ((state >= MachineState_FirstOnline) &&
@@ -5807,6 +5742,7 @@ static vboxUniformedIVirtualBox _UIVirtualBox = {
     .OpenMedium = _virtualboxOpenMedium,
     .FindDHCPServerByNetworkName = _virtualboxFindDHCPServerByNetworkName,
     .CreateDHCPServer = _virtualboxCreateDHCPServer,
+    .RemoveDHCPServer = _virtualboxRemoveDHCPServer,
 };
 
 static vboxUniformedIMachine _UIMachine = {
@@ -6029,6 +5965,7 @@ static vboxUniformedIHost _UIHost = {
     .FindHostNetworkInterfaceById = _hostFindHostNetworkInterfaceById,
     .FindHostNetworkInterfaceByName = _hostFindHostNetworkInterfaceByName,
     .CreateHostOnlyNetworkInterface = _hostCreateHostOnlyNetworkInterface,
+    .RemoveHostOnlyNetworkInterface = _hostRemoveHostOnlyNetworkInterface,
 };
 
 static vboxUniformedIHNInterface _UIHNInterface = {
@@ -6045,6 +5982,7 @@ static vboxUniformedIDHCPServer _UIDHCPServer = {
     .SetEnabled = _dhcpServerSetEnabled,
     .SetConfiguration = _dhcpServerSetConfiguration,
     .Start = _dhcpServerStart,
+    .Stop = _dhcpServerStop,
 };
 
 static uniformedMachineStateChecker _machineStateChecker = {
@@ -6149,6 +6087,12 @@ void NAME(InstallUniformedAPI)(vboxUniformedAPI *pVBoxAPI)
 #else /* VBOX_API_VERSION < 4002000 */
     pVBoxAPI->vboxSnapshotRedefine = 0;
 #endif /* VBOX_API_VERSION < 4002000 */
+
+#if VBOX_API_VERSION == 2002000
+    pVBoxAPI->networkRemoveInterface = 0;
+#else /* VBOX_API_VERSION > 2002000 */
+    pVBoxAPI->networkRemoveInterface = 1;
+#endif /* VBOX_API_VERSION > 2002000 */
 }
 
 /**
diff --git a/src/vbox/vbox_uniformed_api.h b/src/vbox/vbox_uniformed_api.h
index 83c3834..8f713af 100644
--- a/src/vbox/vbox_uniformed_api.h
+++ b/src/vbox/vbox_uniformed_api.h
@@ -199,6 +199,7 @@ typedef struct {
     nsresult (*OpenMedium)(IVirtualBox *vboxObj, PRUnichar *location, PRUint32 deviceType, PRUint32 accessMode, IMedium **medium);
     nsresult (*FindDHCPServerByNetworkName)(IVirtualBox *vboxObj, PRUnichar *name, IDHCPServer **server);
     nsresult (*CreateDHCPServer)(IVirtualBox *vboxObj, PRUnichar *name, IDHCPServer **server);
+    nsresult (*RemoveDHCPServer)(IVirtualBox *vboxObj, IDHCPServer *server);
 } vboxUniformedIVirtualBox;
 
 /* Functions for IMachine */
@@ -477,6 +478,8 @@ typedef struct {
     nsresult (*CreateHostOnlyNetworkInterface)(vboxGlobalData *data,
                                                IHost *host, char *name,
                                                IHostNetworkInterface **networkInterface);
+    nsresult (*RemoveHostOnlyNetworkInterface)(IHost *host, vboxIIDUnion *iidu,
+                                               IProgress **progress);
 } vboxUniformedIHost;
 
 /* Functions for IHostNetworkInterface */
@@ -499,6 +502,7 @@ typedef struct {
                                  PRUnichar *ToIPAddress);
     nsresult (*Start)(IDHCPServer *dhcpServer, PRUnichar *networkName,
                       PRUnichar *trunkName, PRUnichar *trunkType);
+    nsresult (*Stop)(IDHCPServer *dhcpServer);
 } vboxUniformedIDHCPServer;
 
 typedef struct {
@@ -570,6 +574,7 @@ typedef struct {
     bool oldMediumInterface;
     bool vboxSnapshotRedefine;
     bool supportScreenshot;
+    bool networkRemoveInterface;
 } vboxUniformedAPI;
 
 virDomainPtr vboxDomainLookupByUUID(virConnectPtr conn,
@@ -586,6 +591,8 @@ virNetworkPtr vboxNetworkLookupByUUID(virConnectPtr conn, const unsigned char *u
 virNetworkPtr vboxNetworkLookupByName(virConnectPtr conn, const char *name);
 virNetworkPtr vboxNetworkCreateXML(virConnectPtr conn, const char *xml);
 virNetworkPtr vboxNetworkDefineXML(virConnectPtr conn, const char *xml);
+int vboxNetworkUndefine(virNetworkPtr network);
+int vboxNetworkDestroy(virNetworkPtr network);
 
 /* Version specified functions for installing uniformed API */
 void vbox22InstallUniformedAPI(vboxUniformedAPI *pVBoxAPI);
-- 
1.7.9.5

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]