VBoxNetAdp.c revision 2a047f0d7ee5964456dbc4dec9925031482588ab
f86f2dc613a22ee06add9b878197922466df641bvboxsync/* $Id$ */
f86f2dc613a22ee06add9b878197922466df641bvboxsync/** @file
f86f2dc613a22ee06add9b878197922466df641bvboxsync * VBoxNetAdp - Virtual Network Adapter Driver (Host), Common Code.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/*
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync * Copyright (C) 2008-2009 Sun Microsystems, Inc.
f86f2dc613a22ee06add9b878197922466df641bvboxsync *
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.
f86f2dc613a22ee06add9b878197922466df641bvboxsync *
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
f86f2dc613a22ee06add9b878197922466df641bvboxsync * additional information or have any questions.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync/** @page pg_netadp VBoxNetAdp - Network Adapter
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync *
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync * This is a kernel module that creates a virtual interface that can be attached
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync * to an internal network.
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync *
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
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync *
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync */
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync/*******************************************************************************
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync* Header Files *
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync*******************************************************************************/
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync#define LOG_GROUP LOG_GROUP_NET_ADP_DRV
f86f2dc613a22ee06add9b878197922466df641bvboxsync#include "VBoxNetAdpInternal.h"
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync#include <VBox/sup.h>
f86f2dc613a22ee06add9b878197922466df641bvboxsync#include <VBox/log.h>
f86f2dc613a22ee06add9b878197922466df641bvboxsync#include <VBox/err.h>
f86f2dc613a22ee06add9b878197922466df641bvboxsync#include <VBox/version.h>
f86f2dc613a22ee06add9b878197922466df641bvboxsync#include <iprt/assert.h>
f86f2dc613a22ee06add9b878197922466df641bvboxsync#include <iprt/string.h>
f86f2dc613a22ee06add9b878197922466df641bvboxsync#include <iprt/spinlock.h>
f86f2dc613a22ee06add9b878197922466df641bvboxsync#include <iprt/uuid.h>
f86f2dc613a22ee06add9b878197922466df641bvboxsync
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync/** r=bird: why is this here in the agnositc code? */
97dc19418e21b4b87280756668cf171a4332e498vboxsync#ifndef RT_OS_SOLARIS
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync# include <net/ethernet.h>
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync# include <net/if_ether.h>
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync# include <net/if_types.h>
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync# include <sys/socket.h>
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync# include <net/if.h>
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync# include <net/if_dl.h>
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync# include <sys/errno.h>
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync# include <sys/param.h>
97dc19418e21b4b87280756668cf171a4332e498vboxsync#endif
f86f2dc613a22ee06add9b878197922466df641bvboxsync
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/*******************************************************************************
f86f2dc613a22ee06add9b878197922466df641bvboxsync* Defined Constants And Macros *
f86f2dc613a22ee06add9b878197922466df641bvboxsync*******************************************************************************/
f86f2dc613a22ee06add9b878197922466df641bvboxsync#define IFPORT_2_VBOXNETADP(pIfPort) \
f86f2dc613a22ee06add9b878197922466df641bvboxsync ( (PVBOXNETADP)((uint8_t *)pIfPort - RT_OFFSETOF(VBOXNETADP, MyPort)) )
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync/**
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync * Generate a suitable MAC address.
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync *
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync * @param pThis The instance.
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync * @param pMac Where to return the MAC address.
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(void) vboxNetAdpComposeMACAddress(PVBOXNETADP pThis, PRTMAC pMac)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync#if 0 /* Use a locally administered version of the OUI we use for the guest NICs. */
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync pMac->au8[0] = 0x08 | 2;
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync pMac->au8[1] = 0x00;
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync pMac->au8[2] = 0x27;
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync#else /* this is what \0vb comes down to. It seems to be unassigned atm. */
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync pMac->au8[0] = 0;
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync pMac->au8[1] = 0x76;
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync pMac->au8[2] = 0x62;
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync#endif
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync pMac->au8[3] = pThis->uUnit >> 16;
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync pMac->au8[4] = pThis->uUnit >> 8;
40c9dcdf4b9d1c60dc67fa671b7a689b3c0f84d4vboxsync pMac->au8[5] = pThis->uUnit;
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync
2a047f0d7ee5964456dbc4dec9925031482588abvboxsyncAssertCompileMemberSize(VBOXNETADP, enmState, sizeof(uint32_t));
8300beb17e7c288655895d07bfdc58a01f618218vboxsync
8300beb17e7c288655895d07bfdc58a01f618218vboxsync/**
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * Gets the enmState member atomically.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync *
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * Used for all reads.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync *
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * @returns The enmState value.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * @param pThis The instance.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync */
8300beb17e7c288655895d07bfdc58a01f618218vboxsyncDECLINLINE(VBOXNETADPSTATE) vboxNetAdpGetState(PVBOXNETADP pThis)
8300beb17e7c288655895d07bfdc58a01f618218vboxsync{
8300beb17e7c288655895d07bfdc58a01f618218vboxsync return (VBOXNETADPSTATE)ASMAtomicUoReadU32((uint32_t volatile *)&pThis->enmState);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync}
8300beb17e7c288655895d07bfdc58a01f618218vboxsync
8300beb17e7c288655895d07bfdc58a01f618218vboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Sets the enmState member atomically.
f86f2dc613a22ee06add9b878197922466df641bvboxsync *
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Used for all updates.
f86f2dc613a22ee06add9b878197922466df641bvboxsync *
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pThis The instance.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param enmNewState The new value.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLINLINE(void) vboxNetAdpSetState(PVBOXNETADP pThis, VBOXNETADPSTATE enmNewState)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Log(("vboxNetAdpSetState: pThis=%p, state change: %d -> %d.\n", pThis, vboxNetAdpGetState(pThis), enmNewState));
f86f2dc613a22ee06add9b878197922466df641bvboxsync ASMAtomicWriteU32((uint32_t volatile *)&pThis->enmState, enmNewState);
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * Sets the enmState member atomically.
f86f2dc613a22ee06add9b878197922466df641bvboxsync *
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * Used for all updates.
f86f2dc613a22ee06add9b878197922466df641bvboxsync *
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pThis The instance.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * @param enmNewState The new value.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
8300beb17e7c288655895d07bfdc58a01f618218vboxsyncDECLINLINE(void) vboxNetAdpSetStateWithLock(PVBOXNETADP pThis, VBOXNETADPSTATE enmNewState)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
8300beb17e7c288655895d07bfdc58a01f618218vboxsync VBOXNETADPSTATE enmState;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Log(("vboxNetAdpSetStateWithLock: pThis=%p, state=%d.\n", pThis, enmState));
8300beb17e7c288655895d07bfdc58a01f618218vboxsync RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync vboxNetAdpSetState(pThis, enmNewState);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync RTSpinlockRelease(pThis->hSpinlock, &Tmp);
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync
8300beb17e7c288655895d07bfdc58a01f618218vboxsync/**
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * Gets the enmState member with locking.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync *
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * Used for all reads.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync *
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * @returns The enmState value.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * @param pThis The instance.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync */
8300beb17e7c288655895d07bfdc58a01f618218vboxsyncDECLINLINE(VBOXNETADPSTATE) vboxNetAdpGetStateWithLock(PVBOXNETADP pThis)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
8300beb17e7c288655895d07bfdc58a01f618218vboxsync VBOXNETADPSTATE enmState;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync enmState = vboxNetAdpGetState(pThis);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync RTSpinlockRelease(pThis->hSpinlock, &Tmp);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Log(("vboxNetAdpGetStateWithLock: pThis=%p, state=%d.\n", pThis, enmState));
8300beb17e7c288655895d07bfdc58a01f618218vboxsync return enmState;
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync
8300beb17e7c288655895d07bfdc58a01f618218vboxsync/**
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * Checks and sets the enmState member atomically.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync *
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * Used for all updates.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync *
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * @returns true if the state has been changed.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * @param pThis The instance.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync * @param enmNewState The new value.
8300beb17e7c288655895d07bfdc58a01f618218vboxsync */
8300beb17e7c288655895d07bfdc58a01f618218vboxsyncDECLINLINE(bool) vboxNetAdpCheckAndSetState(PVBOXNETADP pThis, VBOXNETADPSTATE enmOldState, VBOXNETADPSTATE enmNewState)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
8300beb17e7c288655895d07bfdc58a01f618218vboxsync VBOXNETADPSTATE enmActualState;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync bool fRc = true; /* be optimistic */
8300beb17e7c288655895d07bfdc58a01f618218vboxsync RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync
8300beb17e7c288655895d07bfdc58a01f618218vboxsync RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync enmActualState = vboxNetAdpGetState(pThis); /** @todo r=bird: ASMAtomicCmpXchgU32()*/
8300beb17e7c288655895d07bfdc58a01f618218vboxsync if (enmActualState == enmOldState)
8300beb17e7c288655895d07bfdc58a01f618218vboxsync vboxNetAdpSetState(pThis, enmNewState);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync else
8300beb17e7c288655895d07bfdc58a01f618218vboxsync fRc = false;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync RTSpinlockRelease(pThis->hSpinlock, &Tmp);
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync
8300beb17e7c288655895d07bfdc58a01f618218vboxsync if (fRc)
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Log(("vboxNetAdpCheckAndSetState: pThis=%p, state changed: %d -> %d.\n", pThis, enmOldState, enmNewState));
8300beb17e7c288655895d07bfdc58a01f618218vboxsync else
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Log(("vboxNetAdpCheckAndSetState: pThis=%p, no state change: %d != %d (expected).\n", pThis, enmActualState, enmOldState));
8300beb17e7c288655895d07bfdc58a01f618218vboxsync return fRc;
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync/**
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync * Finds a instance by its name, the caller does the locking.
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync *
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync *
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.
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncstatic PVBOXNETADP vboxNetAdpFind(PVBOXNETADPGLOBALS pGlobals, const char *pszName)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
f86f2dc613a22ee06add9b878197922466df641bvboxsync unsigned i;
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync for (i = 0; i < RT_ELEMENTS(pGlobals->aAdapters); i++)
f86f2dc613a22ee06add9b878197922466df641bvboxsync {
f86f2dc613a22ee06add9b878197922466df641bvboxsync RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
f86f2dc613a22ee06add9b878197922466df641bvboxsync PVBOXNETADP pThis = &pGlobals->aAdapters[i];
f86f2dc613a22ee06add9b878197922466df641bvboxsync RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync if ( vboxNetAdpGetState(pThis)
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync && !strcmp(pThis->szName, pszName))
f86f2dc613a22ee06add9b878197922466df641bvboxsync {
f86f2dc613a22ee06add9b878197922466df641bvboxsync RTSpinlockRelease(pThis->hSpinlock, &Tmp);
f86f2dc613a22ee06add9b878197922466df641bvboxsync return pThis;
f86f2dc613a22ee06add9b878197922466df641bvboxsync }
f86f2dc613a22ee06add9b878197922466df641bvboxsync RTSpinlockRelease(pThis->hSpinlock, &Tmp);
f86f2dc613a22ee06add9b878197922466df641bvboxsync }
f86f2dc613a22ee06add9b878197922466df641bvboxsync return NULL;
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Releases a reference to the specified instance.
f86f2dc613a22ee06add9b878197922466df641bvboxsync *
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pThis The instance.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param fBusy Whether the busy counter should be decremented too.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(void) vboxNetAdpRelease(PVBOXNETADP pThis)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
f86f2dc613a22ee06add9b878197922466df641bvboxsync uint32_t cRefs;
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Paranoid Android.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync AssertPtr(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(vboxNetAdpGetState(pThis) > kVBoxNetAdpState_Invalid);
f86f2dc613a22ee06add9b878197922466df641bvboxsync AssertPtr(pThis->pGlobals);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->hEventIdle != NIL_RTSEMEVENT);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->hSpinlock != NIL_RTSPINLOCK);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->szName[0]);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * The object reference counting.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync cRefs = ASMAtomicDecU32(&pThis->cRefs);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Assert(cRefs < UINT32_MAX / 2);
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Decrements the busy counter and does idle wakeup.
f86f2dc613a22ee06add9b878197922466df641bvboxsync *
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pThis The instance.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(void) vboxNetAdpIdle(PVBOXNETADP pThis)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
f86f2dc613a22ee06add9b878197922466df641bvboxsync uint32_t cBusy;
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Paranoid Android.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync AssertPtr(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Assert(vboxNetAdpGetState(pThis) >= kVBoxNetAdpState_Connected);
f86f2dc613a22ee06add9b878197922466df641bvboxsync AssertPtr(pThis->pGlobals);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->hEventIdle != NIL_RTSEMEVENT);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync cBusy = ASMAtomicDecU32(&pThis->cBusy);
f86f2dc613a22ee06add9b878197922466df641bvboxsync if (!cBusy)
f86f2dc613a22ee06add9b878197922466df641bvboxsync {
f86f2dc613a22ee06add9b878197922466df641bvboxsync int rc = RTSemEventSignal(pThis->hEventIdle);
f86f2dc613a22ee06add9b878197922466df641bvboxsync AssertRC(rc);
f86f2dc613a22ee06add9b878197922466df641bvboxsync }
f86f2dc613a22ee06add9b878197922466df641bvboxsync else
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(cBusy < UINT32_MAX / 2);
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Retains a reference to the specified instance.
f86f2dc613a22ee06add9b878197922466df641bvboxsync *
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pThis The instance.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(void) vboxNetAdpRetain(PVBOXNETADP pThis)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
f86f2dc613a22ee06add9b878197922466df641bvboxsync uint32_t cRefs;
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Paranoid Android.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync AssertPtr(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(vboxNetAdpGetState(pThis) > kVBoxNetAdpState_Invalid);
f86f2dc613a22ee06add9b878197922466df641bvboxsync AssertPtr(pThis->pGlobals);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->hEventIdle != NIL_RTSEMEVENT);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->hSpinlock != NIL_RTSPINLOCK);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->szName[0]);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Retain the object.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync cRefs = ASMAtomicIncU32(&pThis->cRefs);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(cRefs > 1 && cRefs < UINT32_MAX / 2);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync NOREF(cRefs);
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Increments busy counter.
f86f2dc613a22ee06add9b878197922466df641bvboxsync *
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pThis The instance.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(void) vboxNetAdpBusy(PVBOXNETADP pThis)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
f86f2dc613a22ee06add9b878197922466df641bvboxsync uint32_t cBusy;
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Are we vigilant enough?
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync AssertPtr(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Assert(vboxNetAdpGetState(pThis) >= kVBoxNetAdpState_Connected);
f86f2dc613a22ee06add9b878197922466df641bvboxsync AssertPtr(pThis->pGlobals);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->hEventIdle != NIL_RTSEMEVENT);
f86f2dc613a22ee06add9b878197922466df641bvboxsync cBusy = ASMAtomicIncU32(&pThis->cBusy);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(cBusy > 0 && cBusy < UINT32_MAX / 2);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync NOREF(cBusy);
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Checks if receive is possible and increases busy and ref counters if so.
f86f2dc613a22ee06add9b878197922466df641bvboxsync *
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pThis The instance.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(bool) vboxNetAdpPrepareToReceive(PVBOXNETADP pThis)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
f86f2dc613a22ee06add9b878197922466df641bvboxsync RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
f86f2dc613a22ee06add9b878197922466df641bvboxsync bool fCanReceive = false;
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Input validation.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync AssertPtr(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
f86f2dc613a22ee06add9b878197922466df641bvboxsync RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync if (vboxNetAdpGetState(pThis) == kVBoxNetAdpState_Active)
f86f2dc613a22ee06add9b878197922466df641bvboxsync {
f86f2dc613a22ee06add9b878197922466df641bvboxsync fCanReceive = true;
f86f2dc613a22ee06add9b878197922466df641bvboxsync vboxNetAdpRetain(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync vboxNetAdpBusy(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync }
f86f2dc613a22ee06add9b878197922466df641bvboxsync RTSpinlockRelease(pThis->hSpinlock, &Tmp);
6af5a377dcdc4437fa51e183a5a8dad78abf2ec0vboxsync Log(("vboxNetAdpPrepareToReceive: fCanReceive=%d.\n", fCanReceive));
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync return fCanReceive;
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Forwards scatter/gather list to internal network and decreases busy and ref counters.
f86f2dc613a22ee06add9b878197922466df641bvboxsync *
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pThis The instance.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(void) vboxNetAdpReceive(PVBOXNETADP pThis, PINTNETSG pSG)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
f86f2dc613a22ee06add9b878197922466df641bvboxsync RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Input validation.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync AssertPtr(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync AssertPtr(pSG);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync AssertPtr(pThis->pSwitchPort);
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 vboxNetAdpIdle(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync vboxNetAdpRelease(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Decreases busy and ref counters.
f86f2dc613a22ee06add9b878197922466df641bvboxsync *
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pThis The instance.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(void) vboxNetAdpCancelReceive(PVBOXNETADP pThis)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
6af5a377dcdc4437fa51e183a5a8dad78abf2ec0vboxsync Log(("vboxNetAdpCancelReceive: cancelled.\n"));
f86f2dc613a22ee06add9b878197922466df641bvboxsync vboxNetAdpIdle(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync vboxNetAdpRelease(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync#ifdef RT_WITH_W64_UNWIND_HACK
f86f2dc613a22ee06add9b878197922466df641bvboxsync# if defined(RT_OS_WINDOWS) && defined(RT_ARCH_AMD64)
f86f2dc613a22ee06add9b878197922466df641bvboxsync# define NETADP_DECL_CALLBACK(type) DECLASM(DECLHIDDEN(type))
f86f2dc613a22ee06add9b878197922466df641bvboxsync# define NETADP_CALLBACK(_n) netfltNtWrap##_n
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsyncNETADP_DECL_CALLBACK(int) NETADP_CALLBACK(vboxNetAdpPortXmit)(PINTNETTRUNKIFPORT pIfPort, PINTNETSG pSG, uint32_t fDst);
f86f2dc613a22ee06add9b878197922466df641bvboxsyncNETADP_DECL_CALLBACK(bool) NETADP_CALLBACK(vboxNetAdpPortIsPromiscuous)(PINTNETTRUNKIFPORT pIfPort);
f86f2dc613a22ee06add9b878197922466df641bvboxsyncNETADP_DECL_CALLBACK(void) NETADP_CALLBACK(vboxNetAdpPortGetMacAddress)(PINTNETTRUNKIFPORT pIfPort, PRTMAC pMac);
f86f2dc613a22ee06add9b878197922466df641bvboxsyncNETADP_DECL_CALLBACK(bool) NETADP_CALLBACK(vboxNetAdpPortIsHostMac)(PINTNETTRUNKIFPORT pIfPort, PCRTMAC pMac);
f86f2dc613a22ee06add9b878197922466df641bvboxsyncNETADP_DECL_CALLBACK(int) NETADP_CALLBACK(vboxNetAdpPortWaitForIdle)(PINTNETTRUNKIFPORT pIfPort, uint32_t cMillies);
f86f2dc613a22ee06add9b878197922466df641bvboxsyncNETADP_DECL_CALLBACK(bool) NETADP_CALLBACK(vboxNetAdpPortSetActive)(PINTNETTRUNKIFPORT pIfPort, bool fActive);
f86f2dc613a22ee06add9b878197922466df641bvboxsyncNETADP_DECL_CALLBACK(void) NETADP_CALLBACK(vboxNetAdpPortDisconnectAndRelease)(PINTNETTRUNKIFPORT pIfPort);
f86f2dc613a22ee06add9b878197922466df641bvboxsyncNETADP_DECL_CALLBACK(void) NETADP_CALLBACK(vboxNetAdpPortRetain)(PINTNETTRUNKIFPORT pIfPort);
f86f2dc613a22ee06add9b878197922466df641bvboxsyncNETADP_DECL_CALLBACK(void) NETADP_CALLBACK(vboxNetAdpPortRelease)(PINTNETTRUNKIFPORT pIfPort);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync# else
f86f2dc613a22ee06add9b878197922466df641bvboxsync# error "UNSUPPORTED (RT_WITH_W64_UNWIND_HACK)"
f86f2dc613a22ee06add9b878197922466df641bvboxsync# endif
f86f2dc613a22ee06add9b878197922466df641bvboxsync#else
f86f2dc613a22ee06add9b878197922466df641bvboxsync# define NETADP_DECL_CALLBACK(type) static DECLCALLBACK(type)
f86f2dc613a22ee06add9b878197922466df641bvboxsync# define NETADP_CALLBACK(_n) _n
f86f2dc613a22ee06add9b878197922466df641bvboxsync#endif
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @copydoc INTNETTRUNKIFPORT::pfnXmit
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncNETADP_DECL_CALLBACK(int) vboxNetAdpPortXmit(PINTNETTRUNKIFPORT pIfPort, PINTNETSG pSG, uint32_t fDst)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
f86f2dc613a22ee06add9b878197922466df641bvboxsync RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
f86f2dc613a22ee06add9b878197922466df641bvboxsync PVBOXNETADP pThis = IFPORT_2_VBOXNETADP(pIfPort);
f86f2dc613a22ee06add9b878197922466df641bvboxsync int rc = VINF_SUCCESS;
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Input validation.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync AssertPtr(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync AssertPtr(pSG);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
6af5a377dcdc4437fa51e183a5a8dad78abf2ec0vboxsync Log(("vboxNetAdpPortXmit: outgoing packet (len=%d)\n", pSG->cbTotal));
6af5a377dcdc4437fa51e183a5a8dad78abf2ec0vboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Do a retain/busy, invoke the OS specific code.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync if (vboxNetAdpGetState(pThis) != kVBoxNetAdpState_Active)
f86f2dc613a22ee06add9b878197922466df641bvboxsync {
f86f2dc613a22ee06add9b878197922466df641bvboxsync RTSpinlockRelease(pThis->hSpinlock, &Tmp);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Log(("vboxNetAdpReceive: Dropping incoming packet for inactive interface %s.\n",
f86f2dc613a22ee06add9b878197922466df641bvboxsync pThis->szName));
f86f2dc613a22ee06add9b878197922466df641bvboxsync return VERR_INVALID_STATE;
f86f2dc613a22ee06add9b878197922466df641bvboxsync }
f86f2dc613a22ee06add9b878197922466df641bvboxsync vboxNetAdpRetain(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync vboxNetAdpBusy(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync RTSpinlockRelease(pThis->hSpinlock, &Tmp);
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync rc = vboxNetAdpPortOsXmit(pThis, pSG, fDst);
f86f2dc613a22ee06add9b878197922466df641bvboxsync vboxNetAdpIdle(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync vboxNetAdpRelease(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync return rc;
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @copydoc INTNETTRUNKIFPORT::pfnIsPromiscuous
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncNETADP_DECL_CALLBACK(bool) vboxNetAdpPortIsPromiscuous(PINTNETTRUNKIFPORT pIfPort)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
f86f2dc613a22ee06add9b878197922466df641bvboxsync PVBOXNETADP pThis = IFPORT_2_VBOXNETADP(pIfPort);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Input validation.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync AssertPtr(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
6af5a377dcdc4437fa51e183a5a8dad78abf2ec0vboxsync Assert(vboxNetAdpGetStateWithLock(pThis) == kVBoxNetAdpState_Active);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Ask the OS specific code.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync return vboxNetAdpPortOsIsPromiscuous(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @copydoc INTNETTRUNKIFPORT::pfnGetMacAddress
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncNETADP_DECL_CALLBACK(void) vboxNetAdpPortGetMacAddress(PINTNETTRUNKIFPORT pIfPort, PRTMAC pMac)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
f86f2dc613a22ee06add9b878197922466df641bvboxsync PVBOXNETADP pThis = IFPORT_2_VBOXNETADP(pIfPort);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Input validation.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync AssertPtr(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
6af5a377dcdc4437fa51e183a5a8dad78abf2ec0vboxsync Assert(vboxNetAdpGetStateWithLock(pThis) == kVBoxNetAdpState_Active);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Forward the question to the OS specific code.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync vboxNetAdpPortOsGetMacAddress(pThis, pMac);
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @copydoc INTNETTRUNKIFPORT::pfnIsHostMac
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncNETADP_DECL_CALLBACK(bool) vboxNetAdpPortIsHostMac(PINTNETTRUNKIFPORT pIfPort, PCRTMAC pMac)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
f86f2dc613a22ee06add9b878197922466df641bvboxsync PVBOXNETADP pThis = IFPORT_2_VBOXNETADP(pIfPort);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Input validation.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync AssertPtr(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Assert(vboxNetAdpGetStateWithLock(pThis) == kVBoxNetAdpState_Active);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Ask the OS specific code.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync return vboxNetAdpPortOsIsHostMac(pThis, pMac);
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @copydoc INTNETTRUNKIFPORT::pfnWaitForIdle
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncNETADP_DECL_CALLBACK(int) vboxNetAdpPortWaitForIdle(PINTNETTRUNKIFPORT pIfPort, uint32_t cMillies)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
f86f2dc613a22ee06add9b878197922466df641bvboxsync int rc;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync PVBOXNETADP pThis = IFPORT_2_VBOXNETADP(pIfPort);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Input validation.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync AssertPtr(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync AssertReturn(vboxNetAdpGetStateWithLock(pThis) >= kVBoxNetAdpState_Connected, VERR_INVALID_STATE);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Go to sleep on the semaphore after checking the busy count.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync vboxNetAdpRetain(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync rc = VINF_SUCCESS;
f86f2dc613a22ee06add9b878197922466df641bvboxsync while (pThis->cBusy && RT_SUCCESS(rc))
f86f2dc613a22ee06add9b878197922466df641bvboxsync rc = RTSemEventWait(pThis->hEventIdle, cMillies); /** @todo make interruptible? */
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync vboxNetAdpRelease(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync return rc;
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @copydoc INTNETTRUNKIFPORT::pfnSetActive
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncNETADP_DECL_CALLBACK(bool) vboxNetAdpPortSetActive(PINTNETTRUNKIFPORT pIfPort, bool fActive)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
f86f2dc613a22ee06add9b878197922466df641bvboxsync bool fPreviouslyActive;
f86f2dc613a22ee06add9b878197922466df641bvboxsync RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
f86f2dc613a22ee06add9b878197922466df641bvboxsync PVBOXNETADP pThis = IFPORT_2_VBOXNETADP(pIfPort);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Input validation.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync AssertPtr(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync AssertPtr(pThis->pGlobals);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Log(("vboxNetAdpPortSetActive: pThis=%p, fActive=%d, state before: %d.\n", pThis, fActive, vboxNetAdpGetState(pThis)));
f86f2dc613a22ee06add9b878197922466df641bvboxsync RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync
8300beb17e7c288655895d07bfdc58a01f618218vboxsync fPreviouslyActive = vboxNetAdpGetState(pThis) == kVBoxNetAdpState_Active;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync if (fPreviouslyActive != fActive)
f86f2dc613a22ee06add9b878197922466df641bvboxsync {
8300beb17e7c288655895d07bfdc58a01f618218vboxsync switch (vboxNetAdpGetState(pThis))
8300beb17e7c288655895d07bfdc58a01f618218vboxsync {
8300beb17e7c288655895d07bfdc58a01f618218vboxsync case kVBoxNetAdpState_Connected:
8300beb17e7c288655895d07bfdc58a01f618218vboxsync vboxNetAdpSetState(pThis, kVBoxNetAdpState_Active);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync break;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync case kVBoxNetAdpState_Active:
8300beb17e7c288655895d07bfdc58a01f618218vboxsync vboxNetAdpSetState(pThis, kVBoxNetAdpState_Connected);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync break;
6af5a377dcdc4437fa51e183a5a8dad78abf2ec0vboxsync default:
6af5a377dcdc4437fa51e183a5a8dad78abf2ec0vboxsync break;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync }
f86f2dc613a22ee06add9b878197922466df641bvboxsync }
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync RTSpinlockRelease(pThis->hSpinlock, &Tmp);
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync Log(("vboxNetAdpPortSetActive: state after: %RTbool.\n", vboxNetAdpGetState(pThis)));
f86f2dc613a22ee06add9b878197922466df641bvboxsync return fPreviouslyActive;
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @copydoc INTNETTRUNKIFPORT::pfnDisconnectAndRelease
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncNETADP_DECL_CALLBACK(void) vboxNetAdpPortDisconnectAndRelease(PINTNETTRUNKIFPORT pIfPort)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
f86f2dc613a22ee06add9b878197922466df641bvboxsync PVBOXNETADP pThis = IFPORT_2_VBOXNETADP(pIfPort);
f86f2dc613a22ee06add9b878197922466df641bvboxsync RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Serious paranoia.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync AssertPtr(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
f86f2dc613a22ee06add9b878197922466df641bvboxsync AssertPtr(pThis->pGlobals);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->hEventIdle != NIL_RTSEMEVENT);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pThis->hSpinlock != NIL_RTSPINLOCK);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Disconnect and release it.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync //Assert(vboxNetAdpGetState(pThis) == kVBoxNetAdpState_Connected);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Assert(!pThis->cBusy);
f86f2dc613a22ee06add9b878197922466df641bvboxsync vboxNetAdpSetState(pThis, kVBoxNetAdpState_Transitional);
f86f2dc613a22ee06add9b878197922466df641bvboxsync RTSpinlockRelease(pThis->hSpinlock, &Tmp);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync vboxNetAdpOsDisconnectIt(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync pThis->pSwitchPort = NULL;
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
f86f2dc613a22ee06add9b878197922466df641bvboxsync vboxNetAdpSetState(pThis, kVBoxNetAdpState_Available);
f86f2dc613a22ee06add9b878197922466df641bvboxsync RTSpinlockRelease(pThis->hSpinlock, &Tmp);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync vboxNetAdpRelease(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @copydoc INTNETTRUNKIFPORT::pfnRelease
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncNETADP_DECL_CALLBACK(void) vboxNetAdpPortRelease(PINTNETTRUNKIFPORT pIfPort)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
f86f2dc613a22ee06add9b878197922466df641bvboxsync PVBOXNETADP pThis = IFPORT_2_VBOXNETADP(pIfPort);
f86f2dc613a22ee06add9b878197922466df641bvboxsync vboxNetAdpRelease(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @copydoc INTNETTRUNKIFPORT::pfnRetain
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncNETADP_DECL_CALLBACK(void) vboxNetAdpPortRetain(PINTNETTRUNKIFPORT pIfPort)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
f86f2dc613a22ee06add9b878197922466df641bvboxsync PVBOXNETADP pThis = IFPORT_2_VBOXNETADP(pIfPort);
f86f2dc613a22ee06add9b878197922466df641bvboxsync vboxNetAdpRetain(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync
8300beb17e7c288655895d07bfdc58a01f618218vboxsyncint vboxNetAdpCreate (PINTNETTRUNKFACTORY pIfFactory, PVBOXNETADP *ppNew)
8300beb17e7c288655895d07bfdc58a01f618218vboxsync{
8300beb17e7c288655895d07bfdc58a01f618218vboxsync int rc;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync unsigned i;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync PVBOXNETADPGLOBALS pGlobals = (PVBOXNETADPGLOBALS)((uint8_t *)pIfFactory - RT_OFFSETOF(VBOXNETADPGLOBALS, TrunkFactory));
8300beb17e7c288655895d07bfdc58a01f618218vboxsync
8300beb17e7c288655895d07bfdc58a01f618218vboxsync for (i = 0; i < RT_ELEMENTS(pGlobals->aAdapters); i++)
8300beb17e7c288655895d07bfdc58a01f618218vboxsync {
8300beb17e7c288655895d07bfdc58a01f618218vboxsync RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync PVBOXNETADP pThis = &pGlobals->aAdapters[i];
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync
8300beb17e7c288655895d07bfdc58a01f618218vboxsync if (vboxNetAdpCheckAndSetState(pThis, kVBoxNetAdpState_Invalid, kVBoxNetAdpState_Transitional))
8300beb17e7c288655895d07bfdc58a01f618218vboxsync {
8300beb17e7c288655895d07bfdc58a01f618218vboxsync /* Found an empty slot -- use it. */
8300beb17e7c288655895d07bfdc58a01f618218vboxsync RTMAC Mac;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Assert(ASMAtomicIncU32(&pThis->cRefs) == 1);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync vboxNetAdpComposeMACAddress(pThis, &Mac);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync rc = vboxNetAdpOsCreate(pThis, &Mac);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync *ppNew = pThis;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync vboxNetAdpSetState(pThis, kVBoxNetAdpState_Available);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync RTSpinlockRelease(pThis->hSpinlock, &Tmp);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync return rc;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync }
8300beb17e7c288655895d07bfdc58a01f618218vboxsync }
8300beb17e7c288655895d07bfdc58a01f618218vboxsync
8300beb17e7c288655895d07bfdc58a01f618218vboxsync /* All slots in adapter array are busy. */
8300beb17e7c288655895d07bfdc58a01f618218vboxsync return VERR_OUT_OF_RESOURCES;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync}
8300beb17e7c288655895d07bfdc58a01f618218vboxsync
8300beb17e7c288655895d07bfdc58a01f618218vboxsyncint vboxNetAdpDestroy (PVBOXNETADP pThis)
8300beb17e7c288655895d07bfdc58a01f618218vboxsync{
8300beb17e7c288655895d07bfdc58a01f618218vboxsync int rc = VINF_SUCCESS;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync
8300beb17e7c288655895d07bfdc58a01f618218vboxsync RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync if (vboxNetAdpGetState(pThis) != kVBoxNetAdpState_Available || pThis->cBusy)
8300beb17e7c288655895d07bfdc58a01f618218vboxsync {
8300beb17e7c288655895d07bfdc58a01f618218vboxsync RTSpinlockRelease(pThis->hSpinlock, &Tmp);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync return VERR_INTNET_FLT_IF_BUSY;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync }
8300beb17e7c288655895d07bfdc58a01f618218vboxsync vboxNetAdpSetState(pThis, kVBoxNetAdpState_Transitional);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync RTSpinlockRelease(pThis->hSpinlock, &Tmp);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync vboxNetAdpRelease(pThis);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync
8300beb17e7c288655895d07bfdc58a01f618218vboxsync vboxNetAdpOsDestroy(pThis);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync
8300beb17e7c288655895d07bfdc58a01f618218vboxsync RTSpinlockAcquire(pThis->hSpinlock, &Tmp);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync vboxNetAdpSetState(pThis, kVBoxNetAdpState_Invalid);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync RTSpinlockRelease(pThis->hSpinlock, &Tmp);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync
8300beb17e7c288655895d07bfdc58a01f618218vboxsync return rc;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync}
8300beb17e7c288655895d07bfdc58a01f618218vboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Connects the instance to the specified switch port.
f86f2dc613a22ee06add9b878197922466df641bvboxsync *
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 *
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.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncstatic int vboxNetAdpConnectIt(PVBOXNETADP pThis, PINTNETTRUNKSWPORT pSwitchPort, PINTNETTRUNKIFPORT *ppIfPort)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
f86f2dc613a22ee06add9b878197922466df641bvboxsync int rc;
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Validate state.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(!pThis->cBusy);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Assert(vboxNetAdpGetStateWithLock(pThis) == kVBoxNetAdpState_Transitional);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Do the job.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Note that we're calling the os stuff while owning the semaphore here.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync pThis->pSwitchPort = pSwitchPort;
f86f2dc613a22ee06add9b878197922466df641bvboxsync rc = vboxNetAdpOsConnectIt(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync if (RT_SUCCESS(rc))
f86f2dc613a22ee06add9b878197922466df641bvboxsync {
f86f2dc613a22ee06add9b878197922466df641bvboxsync *ppIfPort = &pThis->MyPort;
f86f2dc613a22ee06add9b878197922466df641bvboxsync }
f86f2dc613a22ee06add9b878197922466df641bvboxsync else
f86f2dc613a22ee06add9b878197922466df641bvboxsync pThis->pSwitchPort = NULL;
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync return rc;
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @copydoc INTNETTRUNKFACTORY::pfnCreateAndConnect
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncstatic DECLCALLBACK(int) vboxNetAdpFactoryCreateAndConnect(PINTNETTRUNKFACTORY pIfFactory, const char *pszName,
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync PINTNETTRUNKSWPORT pSwitchPort, uint32_t fFlags,
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync PINTNETTRUNKIFPORT *ppIfPort)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync PVBOXNETADPGLOBALS pGlobals = (PVBOXNETADPGLOBALS)((uint8_t *)pIfFactory - RT_OFFSETOF(VBOXNETADPGLOBALS, TrunkFactory));
f86f2dc613a22ee06add9b878197922466df641bvboxsync PVBOXNETADP pThis;
f86f2dc613a22ee06add9b878197922466df641bvboxsync int rc;
f86f2dc613a22ee06add9b878197922466df641bvboxsync
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync LogFlow(("vboxNetAdpFactoryCreateAndConnect: pszName=%p:{%s} fFlags=%#x\n", pszName, pszName, fFlags));
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pGlobals->cFactoryRefs > 0);
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync AssertMsgReturn(fFlags,
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Find instance, check if busy, connect if not.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync pThis = vboxNetAdpFind(pGlobals, pszName);
f86f2dc613a22ee06add9b878197922466df641bvboxsync if (pThis)
f86f2dc613a22ee06add9b878197922466df641bvboxsync {
8300beb17e7c288655895d07bfdc58a01f618218vboxsync if (vboxNetAdpCheckAndSetState(pThis, kVBoxNetAdpState_Available, kVBoxNetAdpState_Transitional))
f86f2dc613a22ee06add9b878197922466df641bvboxsync {
8300beb17e7c288655895d07bfdc58a01f618218vboxsync vboxNetAdpRetain(pThis);
f86f2dc613a22ee06add9b878197922466df641bvboxsync rc = vboxNetAdpConnectIt(pThis, pSwitchPort, ppIfPort);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync vboxNetAdpSetStateWithLock(pThis, RT_SUCCESS(rc) ? kVBoxNetAdpState_Connected : kVBoxNetAdpState_Available);
f86f2dc613a22ee06add9b878197922466df641bvboxsync }
f86f2dc613a22ee06add9b878197922466df641bvboxsync else
f86f2dc613a22ee06add9b878197922466df641bvboxsync rc = VERR_INTNET_FLT_IF_BUSY;
f86f2dc613a22ee06add9b878197922466df641bvboxsync }
f86f2dc613a22ee06add9b878197922466df641bvboxsync else
f86f2dc613a22ee06add9b878197922466df641bvboxsync rc = VERR_INTNET_FLT_IF_NOT_FOUND;
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync return rc;
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @copydoc INTNETTRUNKFACTORY::pfnRelease
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncstatic DECLCALLBACK(void) vboxNetAdpFactoryRelease(PINTNETTRUNKFACTORY pIfFactory)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
f86f2dc613a22ee06add9b878197922466df641bvboxsync PVBOXNETADPGLOBALS pGlobals = (PVBOXNETADPGLOBALS)((uint8_t *)pIfFactory - RT_OFFSETOF(VBOXNETADPGLOBALS, TrunkFactory));
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync int32_t cRefs = ASMAtomicDecS32(&pGlobals->cFactoryRefs);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(cRefs >= 0); NOREF(cRefs);
f86f2dc613a22ee06add9b878197922466df641bvboxsync LogFlow(("vboxNetAdpFactoryRelease: cRefs=%d (new)\n", cRefs));
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Implements the SUPDRV component factor interface query method.
f86f2dc613a22ee06add9b878197922466df641bvboxsync *
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @returns Pointer to an interface. NULL if not supported.
f86f2dc613a22ee06add9b878197922466df641bvboxsync *
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pSupDrvFactory Pointer to the component factory registration structure.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pSession The session - unused.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pszInterfaceUuid The factory interface id.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncstatic DECLCALLBACK(void *) vboxNetAdpQueryFactoryInterface(PCSUPDRVFACTORY pSupDrvFactory, PSUPDRVSESSION pSession, const char *pszInterfaceUuid)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
f86f2dc613a22ee06add9b878197922466df641bvboxsync PVBOXNETADPGLOBALS pGlobals = (PVBOXNETADPGLOBALS)((uint8_t *)pSupDrvFactory - RT_OFFSETOF(VBOXNETADPGLOBALS, SupDrvFactory));
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Convert the UUID strings and compare them.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync RTUUID UuidReq;
f86f2dc613a22ee06add9b878197922466df641bvboxsync int rc = RTUuidFromStr(&UuidReq, pszInterfaceUuid);
f86f2dc613a22ee06add9b878197922466df641bvboxsync if (RT_SUCCESS(rc))
f86f2dc613a22ee06add9b878197922466df641bvboxsync {
f86f2dc613a22ee06add9b878197922466df641bvboxsync if (!RTUuidCompareStr(&UuidReq, INTNETTRUNKFACTORY_UUID_STR))
f86f2dc613a22ee06add9b878197922466df641bvboxsync {
f86f2dc613a22ee06add9b878197922466df641bvboxsync ASMAtomicIncS32(&pGlobals->cFactoryRefs);
f86f2dc613a22ee06add9b878197922466df641bvboxsync return &pGlobals->TrunkFactory;
f86f2dc613a22ee06add9b878197922466df641bvboxsync }
f86f2dc613a22ee06add9b878197922466df641bvboxsync#ifdef LOG_ENABLED
f86f2dc613a22ee06add9b878197922466df641bvboxsync else
f86f2dc613a22ee06add9b878197922466df641bvboxsync Log(("VBoxNetAdp: unknown factory interface query (%s)\n", pszInterfaceUuid));
f86f2dc613a22ee06add9b878197922466df641bvboxsync#endif
f86f2dc613a22ee06add9b878197922466df641bvboxsync }
f86f2dc613a22ee06add9b878197922466df641bvboxsync else
f86f2dc613a22ee06add9b878197922466df641bvboxsync Log(("VBoxNetAdp: rc=%Rrc, uuid=%s\n", rc, pszInterfaceUuid));
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync return NULL;
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Checks whether the VBoxNetAdp wossname can be unloaded.
f86f2dc613a22ee06add9b878197922466df641bvboxsync *
f86f2dc613a22ee06add9b878197922466df641bvboxsync * This will return false if someone is currently using the module.
f86f2dc613a22ee06add9b878197922466df641bvboxsync *
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @returns true if it's relatively safe to unload it, otherwise false.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pGlobals Pointer to the globals.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(bool) vboxNetAdpCanUnload(PVBOXNETADPGLOBALS pGlobals)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
f86f2dc613a22ee06add9b878197922466df641bvboxsync int rc;
f86f2dc613a22ee06add9b878197922466df641bvboxsync unsigned i;
f86f2dc613a22ee06add9b878197922466df641bvboxsync bool fRc = true; /* Assume it can be unloaded. */
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync for (i = 0; i < RT_ELEMENTS(pGlobals->aAdapters); i++)
f86f2dc613a22ee06add9b878197922466df641bvboxsync {
f86f2dc613a22ee06add9b878197922466df641bvboxsync PVBOXNETADP pThis = &pGlobals->aAdapters[i];
8300beb17e7c288655895d07bfdc58a01f618218vboxsync if (vboxNetAdpGetStateWithLock(&pGlobals->aAdapters[i]) >= kVBoxNetAdpState_Connected)
f86f2dc613a22ee06add9b878197922466df641bvboxsync {
f86f2dc613a22ee06add9b878197922466df641bvboxsync fRc = false;
f86f2dc613a22ee06add9b878197922466df641bvboxsync break; /* We already know the answer. */
f86f2dc613a22ee06add9b878197922466df641bvboxsync }
f86f2dc613a22ee06add9b878197922466df641bvboxsync }
f86f2dc613a22ee06add9b878197922466df641bvboxsync return fRc && ASMAtomicUoReadS32((int32_t volatile *)&pGlobals->cFactoryRefs) <= 0;
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
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
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @return VINF_SUCCESS on succes, VERR_WRONG_ORDER if we're busy.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(int) vboxNetAdpTryDeleteIdc(PVBOXNETADPGLOBALS pGlobals)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
f86f2dc613a22ee06add9b878197922466df641bvboxsync int rc;
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync Assert(pGlobals->hFastMtx != NIL_RTSEMFASTMUTEX);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Check before trying to deregister the factory.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync if (!vboxNetAdpCanUnload(pGlobals))
f86f2dc613a22ee06add9b878197922466df641bvboxsync return VERR_WRONG_ORDER;
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Disconnect from SUPDRV and check that nobody raced us,
f86f2dc613a22ee06add9b878197922466df641bvboxsync * reconnect if that should happen.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync rc = SUPR0IdcComponentDeregisterFactory(&pGlobals->SupDrvIDC, &pGlobals->SupDrvFactory);
f86f2dc613a22ee06add9b878197922466df641bvboxsync AssertRC(rc);
f86f2dc613a22ee06add9b878197922466df641bvboxsync if (!vboxNetAdpCanUnload(pGlobals))
f86f2dc613a22ee06add9b878197922466df641bvboxsync {
f86f2dc613a22ee06add9b878197922466df641bvboxsync rc = SUPR0IdcComponentRegisterFactory(&pGlobals->SupDrvIDC, &pGlobals->SupDrvFactory);
f86f2dc613a22ee06add9b878197922466df641bvboxsync AssertRC(rc);
f86f2dc613a22ee06add9b878197922466df641bvboxsync return VERR_WRONG_ORDER;
f86f2dc613a22ee06add9b878197922466df641bvboxsync }
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync SUPR0IdcClose(&pGlobals->SupDrvIDC);
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync return rc;
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
8300beb17e7c288655895d07bfdc58a01f618218vboxsyncstatic int vboxNetAdpSlotCreate(PVBOXNETADPGLOBALS pGlobals, unsigned uUnit, PVBOXNETADP pNew)
8300beb17e7c288655895d07bfdc58a01f618218vboxsync{
8300beb17e7c288655895d07bfdc58a01f618218vboxsync int rc;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync
8300beb17e7c288655895d07bfdc58a01f618218vboxsync pNew->MyPort.u32Version = INTNETTRUNKIFPORT_VERSION;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync pNew->MyPort.pfnRetain = NETADP_CALLBACK(vboxNetAdpPortRetain);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync pNew->MyPort.pfnRelease = NETADP_CALLBACK(vboxNetAdpPortRelease);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync pNew->MyPort.pfnDisconnectAndRelease= NETADP_CALLBACK(vboxNetAdpPortDisconnectAndRelease);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync pNew->MyPort.pfnSetActive = NETADP_CALLBACK(vboxNetAdpPortSetActive);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync pNew->MyPort.pfnWaitForIdle = NETADP_CALLBACK(vboxNetAdpPortWaitForIdle);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync pNew->MyPort.pfnGetMacAddress = NETADP_CALLBACK(vboxNetAdpPortGetMacAddress);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync pNew->MyPort.pfnIsHostMac = NETADP_CALLBACK(vboxNetAdpPortIsHostMac);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync pNew->MyPort.pfnIsPromiscuous = NETADP_CALLBACK(vboxNetAdpPortIsPromiscuous);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync pNew->MyPort.pfnXmit = NETADP_CALLBACK(vboxNetAdpPortXmit);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync pNew->MyPort.u32VersionEnd = INTNETTRUNKIFPORT_VERSION;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync pNew->pSwitchPort = NULL;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync pNew->pGlobals = pGlobals;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync pNew->hSpinlock = NIL_RTSPINLOCK;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync pNew->enmState = kVBoxNetAdpState_Invalid;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync pNew->cRefs = 0;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync pNew->cBusy = 0;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync pNew->hEventIdle = NIL_RTSEMEVENT;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync
8300beb17e7c288655895d07bfdc58a01f618218vboxsync rc = RTSpinlockCreate(&pNew->hSpinlock);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync if (RT_SUCCESS(rc))
8300beb17e7c288655895d07bfdc58a01f618218vboxsync {
8300beb17e7c288655895d07bfdc58a01f618218vboxsync rc = RTSemEventCreate(&pNew->hEventIdle);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync if (RT_SUCCESS(rc))
8300beb17e7c288655895d07bfdc58a01f618218vboxsync {
8300beb17e7c288655895d07bfdc58a01f618218vboxsync rc = vboxNetAdpOsInit(pNew);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync if (RT_SUCCESS(rc))
8300beb17e7c288655895d07bfdc58a01f618218vboxsync {
8300beb17e7c288655895d07bfdc58a01f618218vboxsync return rc;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync }
8300beb17e7c288655895d07bfdc58a01f618218vboxsync RTSemEventDestroy(pNew->hEventIdle);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync pNew->hEventIdle = NIL_RTSEMEVENT;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync }
8300beb17e7c288655895d07bfdc58a01f618218vboxsync RTSpinlockDestroy(pNew->hSpinlock);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync pNew->hSpinlock = NIL_RTSPINLOCK;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync }
8300beb17e7c288655895d07bfdc58a01f618218vboxsync return rc;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync}
8300beb17e7c288655895d07bfdc58a01f618218vboxsync
8300beb17e7c288655895d07bfdc58a01f618218vboxsyncstatic void vboxNetAdpSlotDestroy(PVBOXNETADP pThis)
8300beb17e7c288655895d07bfdc58a01f618218vboxsync{
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Assert(pThis->cRefs == 0);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Assert(pThis->cBusy == 0);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Assert(vboxNetAdpGetState(pThis) == kVBoxNetAdpState_Invalid);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync if (pThis->hEventIdle != NIL_RTSEMEVENT)
8300beb17e7c288655895d07bfdc58a01f618218vboxsync {
8300beb17e7c288655895d07bfdc58a01f618218vboxsync RTSemEventDestroy(pThis->hEventIdle);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync pThis->hEventIdle = NIL_RTSEMEVENT;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync }
8300beb17e7c288655895d07bfdc58a01f618218vboxsync if (pThis->hSpinlock != NIL_RTSPINLOCK)
8300beb17e7c288655895d07bfdc58a01f618218vboxsync {
8300beb17e7c288655895d07bfdc58a01f618218vboxsync RTSpinlockDestroy(pThis->hSpinlock);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync pThis->hSpinlock = NIL_RTSPINLOCK;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync }
8300beb17e7c288655895d07bfdc58a01f618218vboxsync}
8300beb17e7c288655895d07bfdc58a01f618218vboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
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
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(void) vboxNetAdpDeleteGlobalsBase(PVBOXNETADPGLOBALS pGlobals)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
8300beb17e7c288655895d07bfdc58a01f618218vboxsync int i;
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Release resources.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
8300beb17e7c288655895d07bfdc58a01f618218vboxsync for (i = 0; i < (int)RT_ELEMENTS(pGlobals->aAdapters); i++)
8300beb17e7c288655895d07bfdc58a01f618218vboxsync if (RT_SUCCESS(vboxNetAdpDestroy(&pGlobals->aAdapters[i])))
8300beb17e7c288655895d07bfdc58a01f618218vboxsync vboxNetAdpSlotDestroy(&pGlobals->aAdapters[i]);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync RTSemFastMutexDestroy(pGlobals->hFastMtx);
f86f2dc613a22ee06add9b878197922466df641bvboxsync pGlobals->hFastMtx = NIL_RTSEMFASTMUTEX;
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync#ifdef VBOXNETADP_STATIC_CONFIG
f86f2dc613a22ee06add9b878197922466df641bvboxsync RTSemEventDestroy(pGlobals->hTimerEvent);
f86f2dc613a22ee06add9b878197922466df641bvboxsync pGlobals->hTimerEvent = NIL_RTSEMEVENT;
f86f2dc613a22ee06add9b878197922466df641bvboxsync#endif
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Called by the native part when the OS wants the driver to unload.
f86f2dc613a22ee06add9b878197922466df641bvboxsync *
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @returns VINF_SUCCESS on succes, VERR_WRONG_ORDER if we're busy.
f86f2dc613a22ee06add9b878197922466df641bvboxsync *
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pGlobals Pointer to the globals.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(int) vboxNetAdpTryDeleteGlobals(PVBOXNETADPGLOBALS pGlobals)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
f86f2dc613a22ee06add9b878197922466df641bvboxsync int rc = vboxNetAdpTryDeleteIdc(pGlobals);
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync if (RT_SUCCESS(rc))
f86f2dc613a22ee06add9b878197922466df641bvboxsync {
f86f2dc613a22ee06add9b878197922466df641bvboxsync vboxNetAdpDeleteGlobalsBase(pGlobals);
f86f2dc613a22ee06add9b878197922466df641bvboxsync }
f86f2dc613a22ee06add9b878197922466df641bvboxsync return rc;
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
8300beb17e7c288655895d07bfdc58a01f618218vboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
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 *
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @returns VBox status code.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pGlobals Pointer to the globals. */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(int) vboxNetAdpInitGlobalsBase(PVBOXNETADPGLOBALS pGlobals)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Initialize the common portions of the structure.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync int i;
f86f2dc613a22ee06add9b878197922466df641bvboxsync int rc = RTSemFastMutexCreate(&pGlobals->hFastMtx);
f86f2dc613a22ee06add9b878197922466df641bvboxsync if (RT_SUCCESS(rc))
f86f2dc613a22ee06add9b878197922466df641bvboxsync {
f86f2dc613a22ee06add9b878197922466df641bvboxsync memset(pGlobals->aAdapters, 0, sizeof(pGlobals->aAdapters));
f86f2dc613a22ee06add9b878197922466df641bvboxsync for (i = 0; i < (int)RT_ELEMENTS(pGlobals->aAdapters); i++)
f86f2dc613a22ee06add9b878197922466df641bvboxsync {
8300beb17e7c288655895d07bfdc58a01f618218vboxsync rc = vboxNetAdpSlotCreate(pGlobals, i, &pGlobals->aAdapters[i]);
f86f2dc613a22ee06add9b878197922466df641bvboxsync if (RT_FAILURE(rc))
f86f2dc613a22ee06add9b878197922466df641bvboxsync {
f86f2dc613a22ee06add9b878197922466df641bvboxsync /* Clean up. */
f86f2dc613a22ee06add9b878197922466df641bvboxsync while (--i >= 0)
8300beb17e7c288655895d07bfdc58a01f618218vboxsync vboxNetAdpSlotDestroy(&pGlobals->aAdapters[i]);
f86f2dc613a22ee06add9b878197922466df641bvboxsync Log(("vboxNetAdpInitGlobalsBase: Failed to create fast mutex (rc=%Rrc).\n", rc));
f86f2dc613a22ee06add9b878197922466df641bvboxsync RTSemFastMutexDestroy(pGlobals->hFastMtx);
f86f2dc613a22ee06add9b878197922466df641bvboxsync return rc;
f86f2dc613a22ee06add9b878197922466df641bvboxsync }
f86f2dc613a22ee06add9b878197922466df641bvboxsync }
f86f2dc613a22ee06add9b878197922466df641bvboxsync pGlobals->TrunkFactory.pfnRelease = vboxNetAdpFactoryRelease;
f86f2dc613a22ee06add9b878197922466df641bvboxsync pGlobals->TrunkFactory.pfnCreateAndConnect = vboxNetAdpFactoryCreateAndConnect;
f86f2dc613a22ee06add9b878197922466df641bvboxsync
8300beb17e7c288655895d07bfdc58a01f618218vboxsync strcpy(pGlobals->SupDrvFactory.szName, "VBoxNetAdp");
f86f2dc613a22ee06add9b878197922466df641bvboxsync pGlobals->SupDrvFactory.pfnQueryFactoryInterface = vboxNetAdpQueryFactoryInterface;
f86f2dc613a22ee06add9b878197922466df641bvboxsync }
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync return rc;
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
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 *
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @returns VBox status code.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pGlobals Pointer to the globals. */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(int) vboxNetAdpInitIdc(PVBOXNETADPGLOBALS pGlobals)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
f86f2dc613a22ee06add9b878197922466df641bvboxsync int rc;
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Establish a connection to SUPDRV and register our component factory.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync rc = SUPR0IdcOpen(&pGlobals->SupDrvIDC, 0 /* iReqVersion = default */, 0 /* iMinVersion = default */, NULL, NULL, NULL);
f86f2dc613a22ee06add9b878197922466df641bvboxsync if (RT_SUCCESS(rc))
f86f2dc613a22ee06add9b878197922466df641bvboxsync {
f86f2dc613a22ee06add9b878197922466df641bvboxsync rc = SUPR0IdcComponentRegisterFactory(&pGlobals->SupDrvIDC, &pGlobals->SupDrvFactory);
f86f2dc613a22ee06add9b878197922466df641bvboxsync if (RT_SUCCESS(rc))
f86f2dc613a22ee06add9b878197922466df641bvboxsync {
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync#if 1 /** @todo REMOVE ME! */
8300beb17e7c288655895d07bfdc58a01f618218vboxsync PVBOXNETADP pTmp;
8300beb17e7c288655895d07bfdc58a01f618218vboxsync rc = vboxNetAdpCreate(&pGlobals->TrunkFactory, &pTmp);
8300beb17e7c288655895d07bfdc58a01f618218vboxsync if (RT_FAILURE(rc))
8300beb17e7c288655895d07bfdc58a01f618218vboxsync Log(("Failed to create vboxnet0, rc=%Rrc.\n", rc));
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync#endif
f86f2dc613a22ee06add9b878197922466df641bvboxsync Log(("VBoxNetAdp: pSession=%p\n", SUPR0IdcGetSession(&pGlobals->SupDrvIDC)));
f86f2dc613a22ee06add9b878197922466df641bvboxsync return rc;
f86f2dc613a22ee06add9b878197922466df641bvboxsync }
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /* bail out. */
f86f2dc613a22ee06add9b878197922466df641bvboxsync LogRel(("VBoxNetAdp: Failed to register component factory, rc=%Rrc\n", rc));
f86f2dc613a22ee06add9b878197922466df641bvboxsync SUPR0IdcClose(&pGlobals->SupDrvIDC);
f86f2dc613a22ee06add9b878197922466df641bvboxsync }
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync return rc;
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync/**
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Called by the native driver/kext module initialization routine.
f86f2dc613a22ee06add9b878197922466df641bvboxsync *
f86f2dc613a22ee06add9b878197922466df641bvboxsync * It will initialize the common parts of the globals, assuming the caller
f86f2dc613a22ee06add9b878197922466df641bvboxsync * has already taken care of the OS specific bits.
f86f2dc613a22ee06add9b878197922466df641bvboxsync *
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @returns VBox status code.
f86f2dc613a22ee06add9b878197922466df641bvboxsync * @param pGlobals Pointer to the globals.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsyncDECLHIDDEN(int) vboxNetAdpInitGlobals(PVBOXNETADPGLOBALS pGlobals)
f86f2dc613a22ee06add9b878197922466df641bvboxsync{
f86f2dc613a22ee06add9b878197922466df641bvboxsync /*
f86f2dc613a22ee06add9b878197922466df641bvboxsync * Initialize the common portions of the structure.
f86f2dc613a22ee06add9b878197922466df641bvboxsync */
f86f2dc613a22ee06add9b878197922466df641bvboxsync int rc = vboxNetAdpInitGlobalsBase(pGlobals);
f86f2dc613a22ee06add9b878197922466df641bvboxsync if (RT_SUCCESS(rc))
f86f2dc613a22ee06add9b878197922466df641bvboxsync {
f86f2dc613a22ee06add9b878197922466df641bvboxsync rc = vboxNetAdpInitIdc(pGlobals);
f86f2dc613a22ee06add9b878197922466df641bvboxsync if (RT_SUCCESS(rc))
f86f2dc613a22ee06add9b878197922466df641bvboxsync {
f86f2dc613a22ee06add9b878197922466df641bvboxsync return rc;
f86f2dc613a22ee06add9b878197922466df641bvboxsync }
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync /* bail out. */
f86f2dc613a22ee06add9b878197922466df641bvboxsync vboxNetAdpDeleteGlobalsBase(pGlobals);
f86f2dc613a22ee06add9b878197922466df641bvboxsync }
f86f2dc613a22ee06add9b878197922466df641bvboxsync
f86f2dc613a22ee06add9b878197922466df641bvboxsync return rc;
f86f2dc613a22ee06add9b878197922466df641bvboxsync}
2a047f0d7ee5964456dbc4dec9925031482588abvboxsync