VBoxNetAdp.c revision 72d7681289c829fb514dcc953c9b07ec4d20a28b
f86f2dc613a22ee06add9b878197922466df641bvboxsync * VBoxNetAdp - Virtual Network Adapter Driver (Host), Common Code.
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2008-2009 Oracle Corporation
f86f2dc613a22ee06add9b878197922466df641bvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
f86f2dc613a22ee06add9b878197922466df641bvboxsync * available from http://www.virtualbox.org. This file is free software;
f86f2dc613a22ee06add9b878197922466df641bvboxsync * you can redistribute it and/or modify it under the terms of the GNU
f86f2dc613a22ee06add9b878197922466df641bvboxsync * General Public License (GPL) as published by the Free Software
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
f86f2dc613a22ee06add9b878197922466df641bvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
f86f2dc613a22ee06add9b878197922466df641bvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync/** @page pg_netadp VBoxNetAdp - Network Adapter
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync * This is a kernel module that creates a virtual interface that can be attached
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync * to an internal network.
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync * In the big picture we're one of the three trunk interface on the internal
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync * network, the one named "TAP Interface": @image html Networking_Overview.gif
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync/*******************************************************************************
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync* Header Files *
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync*******************************************************************************/
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync/** r=bird: why is this here in the agnostic code? */
f86f2dc613a22ee06add9b878197922466df641bvboxsync/*******************************************************************************
f86f2dc613a22ee06add9b878197922466df641bvboxsync* Defined Constants And Macros *
f86f2dc613a22ee06add9b878197922466df641bvboxsync*******************************************************************************/
f86f2dc613a22ee06add9b878197922466df641bvboxsync ( (PVBOXNETADP)((uint8_t *)pIfPort - RT_OFFSETOF(VBOXNETADP, MyPort)) )
2a047f0d7ee5964456dbc4dec9925031482588abvboxsyncAssertCompileMemberSize(VBOXNETADP, enmState, sizeof(uint32_t));
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * Gets the enmState member atomically.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * Used for all reads.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * @returns The enmState value.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * @param pThis The instance.
8300beb17e7c288655895d07bfdc58a01f618218vboxsyncDECLINLINE(VBOXNETADPSTATE) vboxNetAdpGetState(PVBOXNETADP pThis)
8300beb17e7c288655895d07bfdc58a01f618218vboxsync return (VBOXNETADPSTATE)ASMAtomicUoReadU32((uint32_t volatile *)&pThis->enmState);
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Sets the enmState member atomically.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Used for all updates.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pThis The instance.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param enmNewState The new value.
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLINLINE(void) vboxNetAdpSetState(PVBOXNETADP pThis, VBOXNETADPSTATE enmNewState)
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Log(("vboxNetAdpSetState: pThis=%p, state change: %d -> %d.\n", pThis, vboxNetAdpGetState(pThis), enmNewState));
f86f2dc613a22ee06add9b878197922466df641bvboxsync ASMAtomicWriteU32((uint32_t volatile *)&pThis->enmState, enmNewState);
ccc948c886b751603889a67909fbd4a5fcaeac85vboxsync * Sets the enmState member atomically after first acquiring the spinlock.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * Used for all updates.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pThis The instance.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * @param enmNewState The new value.
8300beb17e7c288655895d07bfdc58a01f618218vboxsyncDECLINLINE(void) vboxNetAdpSetStateWithLock(PVBOXNETADP pThis, VBOXNETADPSTATE enmNewState)
ccc948c886b751603889a67909fbd4a5fcaeac85vboxsync Log(("vboxNetAdpSetStateWithLock: pThis=%p, state=%d.\n", pThis, enmNewState));
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * Gets the enmState member with locking.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * Used for all reads.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * @returns The enmState value.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * @param pThis The instance.
8300beb17e7c288655895d07bfdc58a01f618218vboxsyncDECLINLINE(VBOXNETADPSTATE) vboxNetAdpGetStateWithLock(PVBOXNETADP pThis)
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Log(("vboxNetAdpGetStateWithLock: pThis=%p, state=%d.\n", pThis, enmState));
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * Checks and sets the enmState member atomically.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * Used for all updates.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * @returns true if the state has been changed.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * @param pThis The instance.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * @param enmNewState The new value.
8300beb17e7c288655895d07bfdc58a01f618218vboxsyncDECLINLINE(bool) vboxNetAdpCheckAndSetState(PVBOXNETADP pThis, VBOXNETADPSTATE enmOldState, VBOXNETADPSTATE enmNewState)
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync enmActualState = vboxNetAdpGetState(pThis); /** @todo r=bird: ASMAtomicCmpXchgU32()*/
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Log(("vboxNetAdpCheckAndSetState: pThis=%p, state changed: %d -> %d.\n", pThis, enmOldState, enmNewState));
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Log(("vboxNetAdpCheckAndSetState: pThis=%p, no state change: %d != %d (expected).\n", pThis, enmActualState, enmOldState));
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync * Finds a instance by its name, the caller does the locking.
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync * @returns Pointer to the instance by the given name. NULL if not found.
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync * @param pGlobals The globals.
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync * @param pszName The name of the instance.
f86f2dc613a22ee06add9b878197922466df641bvboxsyncstatic PVBOXNETADP vboxNetAdpFind(PVBOXNETADPGLOBALS pGlobals, const char *pszName)
f86f2dc613a22ee06add9b878197922466df641bvboxsync unsigned i;
f86f2dc613a22ee06add9b878197922466df641bvboxsync for (i = 0; i < RT_ELEMENTS(pGlobals->aAdapters); i++)
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Releases a reference to the specified instance.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pThis The instance.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param fBusy Whether the busy counter should be decremented too.
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(void) vboxNetAdpRelease(PVBOXNETADP pThis)
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Paranoid Android.
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(vboxNetAdpGetState(pThis) > kVBoxNetAdpState_Invalid);
f86f2dc613a22ee06add9b878197922466df641bvboxsync * The object reference counting.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Decrements the busy counter and does idle wakeup.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pThis The instance.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Paranoid Android.
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Assert(vboxNetAdpGetState(pThis) >= kVBoxNetAdpState_Connected);
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Retains a reference to the specified instance.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pThis The instance.
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(void) vboxNetAdpRetain(PVBOXNETADP pThis)
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Paranoid Android.
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(vboxNetAdpGetState(pThis) > kVBoxNetAdpState_Invalid);
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Retain the object.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Increments busy counter.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pThis The instance.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Are we vigilant enough?
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Assert(vboxNetAdpGetState(pThis) >= kVBoxNetAdpState_Connected);
ccc948c886b751603889a67909fbd4a5fcaeac85vboxsync * Generate a suitable MAC address.
ccc948c886b751603889a67909fbd4a5fcaeac85vboxsync * @param pThis The instance.
ccc948c886b751603889a67909fbd4a5fcaeac85vboxsync * @param pMac Where to return the MAC address.
ccc948c886b751603889a67909fbd4a5fcaeac85vboxsyncDECLHIDDEN(void) vboxNetAdpComposeMACAddress(PVBOXNETADP pThis, PRTMAC pMac)
ccc948c886b751603889a67909fbd4a5fcaeac85vboxsync#if 0 /* Use a locally administered version of the OUI we use for the guest NICs. */
ccc948c886b751603889a67909fbd4a5fcaeac85vboxsync#else /* this is what \0vb comes down to. It seems to be unassigned atm. */
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Checks if receive is possible and increases busy and ref counters if so.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pThis The instance.
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(bool) vboxNetAdpPrepareToReceive(PVBOXNETADP pThis)
f86f2dc613a22ee06add9b878197922466df641bvboxsync bool fCanReceive = false;
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Input validation.
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync if (vboxNetAdpGetState(pThis) == kVBoxNetAdpState_Active)
6af5a377dcdc4437fa51e183a5a8dad78abf2ec0vboxsync Log(("vboxNetAdpPrepareToReceive: fCanReceive=%d.\n", fCanReceive));
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Forwards scatter/gather list to internal network and decreases busy and ref counters.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pThis The instance.
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(void) vboxNetAdpReceive(PVBOXNETADP pThis, PINTNETSG pSG)
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Input validation.
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
6af5a377dcdc4437fa51e183a5a8dad78abf2ec0vboxsync Log(("vboxNetAdpReceive: forwarding packet to internal net...\n"));
f86f2dc613a22ee06add9b878197922466df641bvboxsync pThis->pSwitchPort->pfnRecv(pThis->pSwitchPort, pSG, INTNETTRUNKDIR_HOST);
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Decreases busy and ref counters.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pThis The instance.
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(void) vboxNetAdpCancelReceive(PVBOXNETADP pThis)
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @copydoc INTNETTRUNKIFPORT::pfnXmit
5a9f1d57ea268b1d88ff2492d9d59a696b1c693evboxsyncstatic DECLCALLBACK(int) vboxNetAdpPortXmit(PINTNETTRUNKIFPORT pIfPort, PINTNETSG pSG, uint32_t fDst)
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Input validation.
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
6af5a377dcdc4437fa51e183a5a8dad78abf2ec0vboxsync Log(("vboxNetAdpPortXmit: outgoing packet (len=%d)\n", pSG->cbTotal));
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Do a retain/busy, invoke the OS specific code.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync if (vboxNetAdpGetState(pThis) != kVBoxNetAdpState_Active)
f86f2dc613a22ee06add9b878197922466df641bvboxsync Log(("vboxNetAdpReceive: Dropping incoming packet for inactive interface %s.\n",
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @copydoc INTNETTRUNKIFPORT::pfnGetMacAddress
5a9f1d57ea268b1d88ff2492d9d59a696b1c693evboxsyncstatic DECLCALLBACK(void) vboxNetAdpPortGetMacAddress(PINTNETTRUNKIFPORT pIfPort, PRTMAC pMac)
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Input validation.
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
6af5a377dcdc4437fa51e183a5a8dad78abf2ec0vboxsync Assert(vboxNetAdpGetStateWithLock(pThis) == kVBoxNetAdpState_Active);
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Forward the question to the OS specific code.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @copydoc INTNETTRUNKIFPORT::pfnWaitForIdle
5a9f1d57ea268b1d88ff2492d9d59a696b1c693evboxsyncstatic DECLCALLBACK(int) vboxNetAdpPortWaitForIdle(PINTNETTRUNKIFPORT pIfPort, uint32_t cMillies)
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Input validation.
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync AssertReturn(vboxNetAdpGetStateWithLock(pThis) >= kVBoxNetAdpState_Connected, VERR_INVALID_STATE);
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Go to sleep on the semaphore after checking the busy count.
f86f2dc613a22ee06add9b878197922466df641bvboxsync rc = RTSemEventWait(pThis->hEventIdle, cMillies); /** @todo make interruptible? */
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @copydoc INTNETTRUNKIFPORT::pfnSetActive
5a9f1d57ea268b1d88ff2492d9d59a696b1c693evboxsyncstatic DECLCALLBACK(bool) vboxNetAdpPortSetActive(PINTNETTRUNKIFPORT pIfPort, bool fActive)
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Input validation.
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Log(("vboxNetAdpPortSetActive: pThis=%p, fActive=%d, state before: %d.\n", pThis, fActive, vboxNetAdpGetState(pThis)));
8300beb17e7c288655895d07bfdc58a01f618218vboxsync fPreviouslyActive = vboxNetAdpGetState(pThis) == kVBoxNetAdpState_Active;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync vboxNetAdpSetState(pThis, kVBoxNetAdpState_Active);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync vboxNetAdpSetState(pThis, kVBoxNetAdpState_Connected);
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync Log(("vboxNetAdpPortSetActive: state after: %RTbool.\n", vboxNetAdpGetState(pThis)));
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @copydoc INTNETTRUNKIFPORT::pfnDisconnectAndRelease
5a9f1d57ea268b1d88ff2492d9d59a696b1c693evboxsyncstatic DECLCALLBACK(void) vboxNetAdpPortDisconnectAndRelease(PINTNETTRUNKIFPORT pIfPort)
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Serious paranoia.
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Disconnect and release it.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync //Assert(vboxNetAdpGetState(pThis) == kVBoxNetAdpState_Connected);
f86f2dc613a22ee06add9b878197922466df641bvboxsync vboxNetAdpSetState(pThis, kVBoxNetAdpState_Transitional);
f86f2dc613a22ee06add9b878197922466df641bvboxsync vboxNetAdpSetState(pThis, kVBoxNetAdpState_Available);
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @copydoc INTNETTRUNKIFPORT::pfnRelease
5a9f1d57ea268b1d88ff2492d9d59a696b1c693evboxsyncstatic DECLCALLBACK(void) vboxNetAdpPortRelease(PINTNETTRUNKIFPORT pIfPort)
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @copydoc INTNETTRUNKIFPORT::pfnRetain
5a9f1d57ea268b1d88ff2492d9d59a696b1c693evboxsyncstatic DECLCALLBACK(void) vboxNetAdpPortRetain(PINTNETTRUNKIFPORT pIfPort)
72d7681289c829fb514dcc953c9b07ec4d20a28bvboxsyncint vboxNetAdpCreate(PINTNETTRUNKFACTORY pIfFactory, PVBOXNETADP *ppNew)
8300beb17e7c288655895d07bfdc58a01f618218vboxsync unsigned i;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync PVBOXNETADPGLOBALS pGlobals = (PVBOXNETADPGLOBALS)((uint8_t *)pIfFactory - RT_OFFSETOF(VBOXNETADPGLOBALS, TrunkFactory));
8300beb17e7c288655895d07bfdc58a01f618218vboxsync for (i = 0; i < RT_ELEMENTS(pGlobals->aAdapters); i++)
8300beb17e7c288655895d07bfdc58a01f618218vboxsync if (vboxNetAdpCheckAndSetState(pThis, kVBoxNetAdpState_Invalid, kVBoxNetAdpState_Transitional))
8300beb17e7c288655895d07bfdc58a01f618218vboxsync /* Found an empty slot -- use it. */
8300beb17e7c288655895d07bfdc58a01f618218vboxsync vboxNetAdpSetState(pThis, kVBoxNetAdpState_Available);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync /* All slots in adapter array are busy. */
8300beb17e7c288655895d07bfdc58a01f618218vboxsync if (vboxNetAdpGetState(pThis) != kVBoxNetAdpState_Available || pThis->cBusy)
8300beb17e7c288655895d07bfdc58a01f618218vboxsync vboxNetAdpSetState(pThis, kVBoxNetAdpState_Transitional);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync vboxNetAdpSetState(pThis, kVBoxNetAdpState_Invalid);
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Connects the instance to the specified switch port.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Called while owning the lock. We're ASSUMING that the internal
f86f2dc613a22ee06add9b878197922466df641bvboxsync * networking code is already owning an recursive mutex, so, there
f86f2dc613a22ee06add9b878197922466df641bvboxsync * will be no deadlocks when vboxNetAdpOsConnectIt calls back into
f86f2dc613a22ee06add9b878197922466df641bvboxsync * it for setting preferences.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @returns VBox status code.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pThis The instance.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pSwitchPort The port on the internal network 'switch'.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param ppIfPort Where to return our port interface.
f86f2dc613a22ee06add9b878197922466df641bvboxsyncstatic int vboxNetAdpConnectIt(PVBOXNETADP pThis, PINTNETTRUNKSWPORT pSwitchPort, PINTNETTRUNKIFPORT *ppIfPort)
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Validate state.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Assert(vboxNetAdpGetStateWithLock(pThis) == kVBoxNetAdpState_Transitional);
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Do the job.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Note that we're calling the os stuff while owning the semaphore here.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @copydoc INTNETTRUNKFACTORY::pfnCreateAndConnect
f86f2dc613a22ee06add9b878197922466df641bvboxsyncstatic DECLCALLBACK(int) vboxNetAdpFactoryCreateAndConnect(PINTNETTRUNKFACTORY pIfFactory, const char *pszName,
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync PVBOXNETADPGLOBALS pGlobals = (PVBOXNETADPGLOBALS)((uint8_t *)pIfFactory - RT_OFFSETOF(VBOXNETADPGLOBALS, TrunkFactory));
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync LogFlow(("vboxNetAdpFactoryCreateAndConnect: pszName=%p:{%s} fFlags=%#x\n", pszName, pszName, fFlags));
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Find instance, check if busy, connect if not.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync if (vboxNetAdpCheckAndSetState(pThis, kVBoxNetAdpState_Available, kVBoxNetAdpState_Transitional))
f86f2dc613a22ee06add9b878197922466df641bvboxsync rc = vboxNetAdpConnectIt(pThis, pSwitchPort, ppIfPort);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync vboxNetAdpSetStateWithLock(pThis, RT_SUCCESS(rc) ? kVBoxNetAdpState_Connected : kVBoxNetAdpState_Available);
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @copydoc INTNETTRUNKFACTORY::pfnRelease
f86f2dc613a22ee06add9b878197922466df641bvboxsyncstatic DECLCALLBACK(void) vboxNetAdpFactoryRelease(PINTNETTRUNKFACTORY pIfFactory)
f86f2dc613a22ee06add9b878197922466df641bvboxsync PVBOXNETADPGLOBALS pGlobals = (PVBOXNETADPGLOBALS)((uint8_t *)pIfFactory - RT_OFFSETOF(VBOXNETADPGLOBALS, TrunkFactory));
f86f2dc613a22ee06add9b878197922466df641bvboxsync int32_t cRefs = ASMAtomicDecS32(&pGlobals->cFactoryRefs);
f86f2dc613a22ee06add9b878197922466df641bvboxsync LogFlow(("vboxNetAdpFactoryRelease: cRefs=%d (new)\n", cRefs));
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Implements the SUPDRV component factor interface query method.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @returns Pointer to an interface. NULL if not supported.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pSupDrvFactory Pointer to the component factory registration structure.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pSession The session - unused.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pszInterfaceUuid The factory interface id.
f86f2dc613a22ee06add9b878197922466df641bvboxsyncstatic DECLCALLBACK(void *) vboxNetAdpQueryFactoryInterface(PCSUPDRVFACTORY pSupDrvFactory, PSUPDRVSESSION pSession, const char *pszInterfaceUuid)
f86f2dc613a22ee06add9b878197922466df641bvboxsync PVBOXNETADPGLOBALS pGlobals = (PVBOXNETADPGLOBALS)((uint8_t *)pSupDrvFactory - RT_OFFSETOF(VBOXNETADPGLOBALS, SupDrvFactory));
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Convert the UUID strings and compare them.
f86f2dc613a22ee06add9b878197922466df641bvboxsync int rc = RTUuidFromStr(&UuidReq, pszInterfaceUuid);
f86f2dc613a22ee06add9b878197922466df641bvboxsync if (!RTUuidCompareStr(&UuidReq, INTNETTRUNKFACTORY_UUID_STR))
f86f2dc613a22ee06add9b878197922466df641bvboxsync Log(("VBoxNetAdp: unknown factory interface query (%s)\n", pszInterfaceUuid));
f86f2dc613a22ee06add9b878197922466df641bvboxsync Log(("VBoxNetAdp: rc=%Rrc, uuid=%s\n", rc, pszInterfaceUuid));
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Checks whether the VBoxNetAdp wossname can be unloaded.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * This will return false if someone is currently using the module.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @returns true if it's relatively safe to unload it, otherwise false.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pGlobals Pointer to the globals.
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(bool) vboxNetAdpCanUnload(PVBOXNETADPGLOBALS pGlobals)
ccc948c886b751603889a67909fbd4a5fcaeac85vboxsync unsigned i;
f86f2dc613a22ee06add9b878197922466df641bvboxsync for (i = 0; i < RT_ELEMENTS(pGlobals->aAdapters); i++)
8300beb17e7c288655895d07bfdc58a01f618218vboxsync if (vboxNetAdpGetStateWithLock(&pGlobals->aAdapters[i]) >= kVBoxNetAdpState_Connected)
f86f2dc613a22ee06add9b878197922466df641bvboxsync break; /* We already know the answer. */
f86f2dc613a22ee06add9b878197922466df641bvboxsync return fRc && ASMAtomicUoReadS32((int32_t volatile *)&pGlobals->cFactoryRefs) <= 0;
f86f2dc613a22ee06add9b878197922466df641bvboxsync * tries to deinitialize Idc
f86f2dc613a22ee06add9b878197922466df641bvboxsync * we separate the globals settings "base" which is actually
f86f2dc613a22ee06add9b878197922466df641bvboxsync * "general" globals settings except for Idc, and idc.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * This is needed for windows filter driver, which gets loaded prior to VBoxDrv,
f86f2dc613a22ee06add9b878197922466df641bvboxsync * thus it's not possible to make idc initialization from the driver startup routine for it,
f86f2dc613a22ee06add9b878197922466df641bvboxsync * though the "base is still needed for the driver to functions".
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pGlobals
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * @return VINF_SUCCESS on success, VERR_WRONG_ORDER if we're busy.
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(int) vboxNetAdpTryDeleteIdc(PVBOXNETADPGLOBALS pGlobals)
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Check before trying to deregister the factory.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Disconnect from SUPDRV and check that nobody raced us,
f86f2dc613a22ee06add9b878197922466df641bvboxsync * reconnect if that should happen.
f86f2dc613a22ee06add9b878197922466df641bvboxsync rc = SUPR0IdcComponentDeregisterFactory(&pGlobals->SupDrvIDC, &pGlobals->SupDrvFactory);
f86f2dc613a22ee06add9b878197922466df641bvboxsync rc = SUPR0IdcComponentRegisterFactory(&pGlobals->SupDrvIDC, &pGlobals->SupDrvFactory);
8300beb17e7c288655895d07bfdc58a01f618218vboxsyncstatic int vboxNetAdpSlotCreate(PVBOXNETADPGLOBALS pGlobals, unsigned uUnit, PVBOXNETADP pNew)
8300beb17e7c288655895d07bfdc58a01f618218vboxsync pNew->MyPort.u32Version = INTNETTRUNKIFPORT_VERSION;
5a9f1d57ea268b1d88ff2492d9d59a696b1c693evboxsync pNew->MyPort.pfnDisconnectAndRelease= vboxNetAdpPortDisconnectAndRelease;
5a9f1d57ea268b1d88ff2492d9d59a696b1c693evboxsync pNew->MyPort.pfnWaitForIdle = vboxNetAdpPortWaitForIdle;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync pNew->MyPort.u32VersionEnd = INTNETTRUNKIFPORT_VERSION;
8300beb17e7c288655895d07bfdc58a01f618218vboxsyncstatic void vboxNetAdpSlotDestroy(PVBOXNETADP pThis)
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Assert(vboxNetAdpGetState(pThis) == kVBoxNetAdpState_Invalid);
f86f2dc613a22ee06add9b878197922466df641bvboxsync * performs "base" globals deinitialization
f86f2dc613a22ee06add9b878197922466df641bvboxsync * we separate the globals settings "base" which is actually
f86f2dc613a22ee06add9b878197922466df641bvboxsync * "general" globals settings except for Idc, and idc.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * This is needed for windows filter driver, which gets loaded prior to VBoxDrv,
f86f2dc613a22ee06add9b878197922466df641bvboxsync * thus it's not possible to make idc initialization from the driver startup routine for it,
f86f2dc613a22ee06add9b878197922466df641bvboxsync * though the "base is still needed for the driver to functions".
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pGlobals
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @return none
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(void) vboxNetAdpDeleteGlobalsBase(PVBOXNETADPGLOBALS pGlobals)
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Release resources.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync for (i = 0; i < (int)RT_ELEMENTS(pGlobals->aAdapters); i++)
8300beb17e7c288655895d07bfdc58a01f618218vboxsync if (RT_SUCCESS(vboxNetAdpDestroy(&pGlobals->aAdapters[i])))
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Called by the native part when the OS wants the driver to unload.
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * @returns VINF_SUCCESS on success, VERR_WRONG_ORDER if we're busy.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pGlobals Pointer to the globals.
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(int) vboxNetAdpTryDeleteGlobals(PVBOXNETADPGLOBALS pGlobals)
f86f2dc613a22ee06add9b878197922466df641bvboxsync * performs the "base" globals initialization
f86f2dc613a22ee06add9b878197922466df641bvboxsync * we separate the globals initialization to globals "base" initialization which is actually
f86f2dc613a22ee06add9b878197922466df641bvboxsync * "general" globals initialization except for Idc not being initialized, and idc initialization.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * This is needed for windows filter driver, which gets loaded prior to VBoxDrv,
f86f2dc613a22ee06add9b878197922466df641bvboxsync * thus it's not possible to make idc initialization from the driver startup routine for it.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @returns VBox status code.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pGlobals Pointer to the globals. */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(int) vboxNetAdpInitGlobalsBase(PVBOXNETADPGLOBALS pGlobals)
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Initialize the common portions of the structure.
f86f2dc613a22ee06add9b878197922466df641bvboxsync int rc = RTSemFastMutexCreate(&pGlobals->hFastMtx);
f86f2dc613a22ee06add9b878197922466df641bvboxsync memset(pGlobals->aAdapters, 0, sizeof(pGlobals->aAdapters));
f86f2dc613a22ee06add9b878197922466df641bvboxsync for (i = 0; i < (int)RT_ELEMENTS(pGlobals->aAdapters); i++)
8300beb17e7c288655895d07bfdc58a01f618218vboxsync rc = vboxNetAdpSlotCreate(pGlobals, i, &pGlobals->aAdapters[i]);
f86f2dc613a22ee06add9b878197922466df641bvboxsync /* Clean up. */
f86f2dc613a22ee06add9b878197922466df641bvboxsync while (--i >= 0)
f86f2dc613a22ee06add9b878197922466df641bvboxsync Log(("vboxNetAdpInitGlobalsBase: Failed to create fast mutex (rc=%Rrc).\n", rc));
f86f2dc613a22ee06add9b878197922466df641bvboxsync pGlobals->TrunkFactory.pfnRelease = vboxNetAdpFactoryRelease;
f86f2dc613a22ee06add9b878197922466df641bvboxsync pGlobals->TrunkFactory.pfnCreateAndConnect = vboxNetAdpFactoryCreateAndConnect;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync strcpy(pGlobals->SupDrvFactory.szName, "VBoxNetAdp");
f86f2dc613a22ee06add9b878197922466df641bvboxsync pGlobals->SupDrvFactory.pfnQueryFactoryInterface = vboxNetAdpQueryFactoryInterface;
f86f2dc613a22ee06add9b878197922466df641bvboxsync * performs the Idc initialization
f86f2dc613a22ee06add9b878197922466df641bvboxsync * we separate the globals initialization to globals "base" initialization which is actually
f86f2dc613a22ee06add9b878197922466df641bvboxsync * "general" globals initialization except for Idc not being initialized, and idc initialization.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * This is needed for windows filter driver, which gets loaded prior to VBoxDrv,
f86f2dc613a22ee06add9b878197922466df641bvboxsync * thus it's not possible to make idc initialization from the driver startup routine for it.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @returns VBox status code.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pGlobals Pointer to the globals. */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(int) vboxNetAdpInitIdc(PVBOXNETADPGLOBALS pGlobals)
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Establish a connection to SUPDRV and register our component factory.
f86f2dc613a22ee06add9b878197922466df641bvboxsync rc = SUPR0IdcOpen(&pGlobals->SupDrvIDC, 0 /* iReqVersion = default */, 0 /* iMinVersion = default */, NULL, NULL, NULL);
f86f2dc613a22ee06add9b878197922466df641bvboxsync rc = SUPR0IdcComponentRegisterFactory(&pGlobals->SupDrvIDC, &pGlobals->SupDrvFactory);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync rc = vboxNetAdpCreate(&pGlobals->TrunkFactory, &pTmp);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Log(("Failed to create vboxnet0, rc=%Rrc.\n", rc));
f86f2dc613a22ee06add9b878197922466df641bvboxsync Log(("VBoxNetAdp: pSession=%p\n", SUPR0IdcGetSession(&pGlobals->SupDrvIDC)));
f86f2dc613a22ee06add9b878197922466df641bvboxsync /* bail out. */
f86f2dc613a22ee06add9b878197922466df641bvboxsync LogRel(("VBoxNetAdp: Failed to register component factory, rc=%Rrc\n", rc));
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Called by the native driver/kext module initialization routine.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * It will initialize the common parts of the globals, assuming the caller
f86f2dc613a22ee06add9b878197922466df641bvboxsync * has already taken care of the OS specific bits.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @returns VBox status code.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pGlobals Pointer to the globals.
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(int) vboxNetAdpInitGlobals(PVBOXNETADPGLOBALS pGlobals)
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Initialize the common portions of the structure.
f86f2dc613a22ee06add9b878197922466df641bvboxsync /* bail out. */
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync#else /* !VBOXANETADP_DO_NOT_USE_NETFLT */
83365ff77c1571f994b3a15bfbdee077d2ea8a07vboxsyncDECLINLINE(int) vboxNetAdpGetUnitByName(const char *pcszName)
72d7681289c829fb514dcc953c9b07ec4d20a28bvboxsync uint32_t iUnit = RTStrToUInt32(pcszName + sizeof(VBOXNETADP_NAME) - 1);
83365ff77c1571f994b3a15bfbdee077d2ea8a07vboxsyncDECLINLINE(int) vboxNetAdpGetNextAvailableUnit(void)
83365ff77c1571f994b3a15bfbdee077d2ea8a07vboxsync /* There is absolutely no chance that all units are taken */
72d7681289c829fb514dcc953c9b07ec4d20a28bvboxsync iUnit = ASMBitFirstClear(g_aUnits, VBOXNETADP_MAX_UNITS);
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync * Generate a suitable MAC address.
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync * @param pThis The instance.
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync * @param pMac Where to return the MAC address.
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsyncDECLHIDDEN(void) vboxNetAdpComposeMACAddress(PVBOXNETADP pThis, PRTMAC pMac)
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync /* Use a locally administered version of the OUI we use for the guest NICs. */
72d7681289c829fb514dcc953c9b07ec4d20a28bvboxsyncint vboxNetAdpCreate(PVBOXNETADP *ppNew, const char *pcszName)
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync unsigned i;
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync if (ASMAtomicCmpXchgU32((uint32_t volatile *)&pThis->enmState, kVBoxNetAdpState_Transitional, kVBoxNetAdpState_Invalid))
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync /* Found an empty slot -- use it. */
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync Log(("vboxNetAdpCreate: found empty slot: %d\n", i));
83365ff77c1571f994b3a15bfbdee077d2ea8a07vboxsync Log(("vboxNetAdpCreate: using name: %s\n", pcszName));
83365ff77c1571f994b3a15bfbdee077d2ea8a07vboxsync strncpy(pThis->szName, pcszName, sizeof(pThis->szName));
72d7681289c829fb514dcc953c9b07ec4d20a28bvboxsync Log(("vboxNetAdpCreate: pThis=%p pThis->iUnit=%d, pThis->szName=%s\n",
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync ASMAtomicWriteU32((uint32_t volatile *)&pThis->enmState, kVBoxNetAdpState_Active);
83365ff77c1571f994b3a15bfbdee077d2ea8a07vboxsync Log2(("VBoxNetAdpCreate: Created %s\n", g_aAdapters[i].szName));
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync ASMAtomicWriteU32((uint32_t volatile *)&pThis->enmState, kVBoxNetAdpState_Invalid);
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync Log(("vboxNetAdpCreate: vboxNetAdpOsCreate failed with '%Rrc'.\n", rc));
83365ff77c1571f994b3a15bfbdee077d2ea8a07vboxsync Log2(("VBoxNetAdpCreate: Scanning entry: state=%d unit=%d name=%s\n",
72d7681289c829fb514dcc953c9b07ec4d20a28bvboxsync g_aAdapters[i].enmState, g_aAdapters[i].iUnit, g_aAdapters[i].szName));
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync /* All slots in adapter array are busy. */
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync if (!ASMAtomicCmpXchgU32((uint32_t volatile *)&pThis->enmState, kVBoxNetAdpState_Transitional, kVBoxNetAdpState_Active))
72d7681289c829fb514dcc953c9b07ec4d20a28bvboxsync Assert(pThis->iUnit >= 0 && pThis->iUnit < VBOXNETADP_MAX_UNITS);
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync ASMAtomicWriteU32((uint32_t volatile *)&pThis->enmState, kVBoxNetAdpState_Invalid);
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync unsigned i;
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync * Init common members and call OS-specific init.
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync g_aAdapters[i].enmState = kVBoxNetAdpState_Invalid;
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync * Finds an adapter by its name.
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync * @returns Pointer to the instance by the given name. NULL if not found.
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync * @param pGlobals The globals.
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync * @param pszName The name of the instance.
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsyncPVBOXNETADP vboxNetAdpFindByName(const char *pszName)
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync unsigned i;
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync Log2(("VBoxNetAdp: Scanning entry: state=%d name=%s\n", pThis->enmState, pThis->szName));
62bce8f445bcb26dca9a6967cd22a1ba12af3e4cvboxsync && ASMAtomicReadU32((uint32_t volatile *)&pThis->enmState) == kVBoxNetAdpState_Active)
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync unsigned i;
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync /* Remove virtual adapters */
6a008ce8150c1391d9e0dda0cdf9485b3c806034vboxsync#endif /* !VBOXANETADP_DO_NOT_USE_NETFLT */