DrvTAPWin32.cpp revision c18940aaee473ca75acc33491c1dc77b3e8094e0
/** @file
*
* VBox network devices:
*/
/*
* Copyright (C) 2006-2007 innotek GmbH
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#define ASYNC_NETIO
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DRV_TUN
#ifdef ASYNC_NETIO
#include <iprt/semaphore.h>
#endif
#include <windows.h>
#include <VBox/tapwin32.h>
#include "Builtins.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Block driver instance data.
*/
typedef struct
{
/** The network interface. */
/** The network interface. */
/** Pointer to the driver instance. */
/** TAP device file handle. */
#ifdef ASYNC_NETIO
/** The thread handle. NIL_RTTHREAD if no thread. */
/** The event semaphore the thread is waiting on. */
/** We are waiting for more receive buffers. */
uint32_t volatile fOutOfSpace;
/** Event semaphore for blocking on receive. */
#endif
#ifdef DEBUG
#endif
#ifdef VBOX_WITH_STATISTICS
/** Number of sent packets. */
/** Number of sent bytes. */
/** Number of received packets. */
/** Number of received bytes. */
/** Profiling packet transmit runs. */
/** Profiling packet receive runs. */
# ifdef ASYNC_NETIO
# endif
#endif /* VBOX_WITH_STATISTICS */
/** Converts a pointer to TUN::INetworkConnector to a PRDVTUN. */
#define PDMINETWORKCONNECTOR_2_DRVTAP(pInterface) ( (PDRVTAP)((uintptr_t)pInterface - RT_OFFSETOF(DRVTAP, INetworkConnector)) )
/**
* Send data to the network.
*
* @returns VBox status code.
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @param pvBuf Data to send.
* @param cb Number of bytes to send.
* @thread EMT
*/
static DECLCALLBACK(int) drvTAPW32Send(PPDMINETWORKCONNECTOR pInterface, const void *pvBuf, size_t cb)
{
int rc;
Log2(("drvTAPW32Send%d: pvBuf=%p cb=%#x\n"
#ifdef DEBUG
Log(("drvTAPW32Send %d bytes at %08x - delta %x\n", cb, pData->dwLastReadTime, pData->dwLastReadTime - pData->dwLastWriteTime));
#endif
rc = VINF_SUCCESS;
{
if (GetLastError() == ERROR_IO_PENDING)
{
Log(("drvTAPW32Send: IO pending!!\n"));
rc = VINF_SUCCESS;
}
else
{
}
}
return rc;
}
/**
* Set promiscuous mode.
*
* This is called when the promiscuous mode is set. This means that there doesn't have
* to be a mode change when it's called.
*
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @param fPromiscuous Set if the adaptor is now in promiscuous mode. Clear if it is not.
* @thread EMT
*/
static DECLCALLBACK(void) drvTAPW32SetPromiscuousMode(PPDMINETWORKCONNECTOR pInterface, bool fPromiscuous)
{
/* nothing to do */
}
/**
* Notification on link status changes.
*
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @param enmLinkState The new link state.
* @thread EMT
*/
static DECLCALLBACK(void) drvTAPW32NotifyLinkChanged(PPDMINETWORKCONNECTOR pInterface, PDMNETWORKLINKSTATE enmLinkState)
{
/** @todo take action on link down and up. Stop the polling and such like. */
}
/**
* More receive buffer has become available.
*
* This is called when the NIC frees up receive buffers.
*
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @thread EMT
*/
{
LogFlow(("drvTAPW32NotifyCanReceive:\n"));
}
#ifndef ASYNC_NETIO
/**
* Poller callback.
*/
{
while (cbMax)
{
{
bRet = ReadFile(pData->hFile, pData->readBuffer, cbMax, &pData->dwNumberOfBytesRead, &pData->overlappedRead);
{
rc = GetLastError();
AssertMsg(rc == ERROR_SUCCESS || rc == ERROR_IO_PENDING || rc == ERROR_MORE_DATA, ("ReadFileEx failed with rc=%d\n", rc));
break;
}
}
if (cbMax)
{
if (GetOverlappedResult(pData->hFile, &pData->overlappedRead, &dwNumberOfBytesTransferred, FALSE) == TRUE)
{
/* push it to the driver. */
Log2(("drvTAPW32Poller%d: cbRead=%#x\n"
#ifdef DEBUG
#endif
}
else
{
rc = GetLastError();
/* reset overlapped structure on aborted read operation */
if (rc != ERROR_IO_INCOMPLETE)
{
}
break;
}
}
}
}
#else /* !ASYNC_NETIO */
/**
* Async I/O thread for an interface.
*/
{
while(1)
{
{
rc = GetLastError();
break;
AssertMsg(rc == WAIT_OBJECT_0 || rc == WAIT_OBJECT_0+1, ("WaitForSingleObject failed with %x\n", rc));
if (rc != WAIT_OBJECT_0)
break; /* asked to quit or fatal error. */
/* If GetOverlappedResult() returned with TRUE, the operation was finished successfully */
}
/* Not very nice, but what else can we do? */
if (cbMax == 0)
{
while (cbMax == 0)
{
#if 1
#else
#endif
/* Check if the VM was terminated */
if (rc == WAIT_OBJECT_0)
{
goto exit_thread;
}
}
}
#ifdef DEBUG
#endif
}
Log(("drvTAPW32AsyncIo: exit thread!!\n"));
return VINF_SUCCESS;
}
#endif /* !ASYNC_NETIO */
/**
* Queries an interface to the driver.
*
* @returns Pointer to interface.
* @returns NULL if the interface was not supported by the driver.
* @param pInterface Pointer to this interface structure.
* @param enmInterface The requested interface identification.
* @thread Any thread.
*/
static DECLCALLBACK(void *) drvTAPW32QueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
{
switch (enmInterface)
{
case PDMINTERFACE_BASE:
return &pData->INetworkConnector;
default:
return NULL;
}
}
/**
* Destruct a driver instance.
*
* Most VM resources are freed by the VM. This callback is provided so that any non-VM
* resources can be freed correctly.
*
* @param pDrvIns The driver instance data.
*/
{
LogFlow(("drvTAPW32Destruct\n"));
#ifdef ASYNC_NETIO
/** @todo this isn't a safe method to notify the async thread; it might be using the instance
* data after we've been destroyed; could wait for it to terminate, but that's not
* without risks either.
*/
/* Ensure that it does not spin in the CanReceive loop. Do it _after_ we set set the
* hHaltAsyncEventSem to ensure that we don't go into the loop again immediately. */
/* Yield or else our async thread will never acquire the event semaphore */
RTThreadSleep(16);
/* Wait for the async thread to quit; up to half a second */
#endif
}
/**
* Construct a TUN network transport driver instance.
*
* @returns VBox status.
* @param pDrvIns The driver instance data.
* If the registration structure is needed, pDrvIns->pDrvReg points to it.
* @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
* of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
* iInstance it's expected to be used a bit in this function.
*/
{
/*
* Init the static parts.
*/
/* IBase */
/* INetwork */
/*
* Validate the config.
*/
/*
* Check that no-one is attached to us.
*/
if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
N_("Configuration error: Cannot attach drivers to the TUN driver"));
/*
* Query the network port interface.
*/
pData->pPort = (PPDMINETWORKPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_NETWORK_PORT);
/*
* Read the configuration.
*/
char *pszHostDriver = NULL;
if (VBOX_FAILURE(rc))
N_("Configuration error: query for \"HostInterfaceName\" failed"));
char szFullDriverName[256];
char szDriverGUID[256] = {0};
if (VBOX_FAILURE(rc))
N_("Configuration error: could not query GUID"));
RTStrPrintfEx(NULL, NULL, szFullDriverName, sizeof(szFullDriverName), "\\\\.\\Global\\%s.tap", szDriverGUID);
{
rc = GetLastError();
AssertMsgFailed(("Configuration error: TAP device name %s is not valid! (rc=%d)\n", szFullDriverName, rc));
if (rc == ERROR_SHARING_VIOLATION)
return VERR_PDM_HIF_SHARING_VIOLATION;
N_("Failed to open Host Interface Networking device driver"));
}
BOOL ret = DeviceIoControl(pData->hFile, TAP_IOCTL_GET_VERSION, &pData->tapVersion, sizeof (pData->tapVersion),
{
N_("Failed to get the Host Interface Networking device driver version"));;
}
/* Must be at least version 8.1 */
{
N_("Invalid Host Interface Networking device driver version"));;
}
ret = DeviceIoControl(pData->hFile, TAP_IOCTL_SET_MEDIA_STATUS, &mediastatus, sizeof(mediastatus), NULL, 0, &length, NULL);
{
}
if (pszHostDriver)
#ifdef ASYNC_NETIO
/* Create asynchronous thread */
rc = RTThreadCreate(&pData->hThread, drvTAPW32AsyncIo, (void *)pData, 128*1024, RTTHREADTYPE_IO, 0, "TAP");
#else
/*
* Register poller
*/
#endif
#ifdef VBOX_WITH_STATISTICS
/*
* Statistics.
*/
PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatPktSent, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of sent packets.", "/Drivers/TAP%d/Packets/Sent", pDrvIns->iInstance);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatPktSentBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Number of sent bytes.", "/Drivers/TAP%d/Bytes/Sent", pDrvIns->iInstance);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatPktRecv, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of received packets.", "/Drivers/TAP%d/Packets/Received", pDrvIns->iInstance);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatPktRecvBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Number of received bytes.", "/Drivers/TAP%d/Bytes/Received", pDrvIns->iInstance);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatTransmit, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling packet transmit runs.", "/Drivers/TAP%d/Transmit", pDrvIns->iInstance);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatReceive, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling packet receive runs.", "/Drivers/TAP%d/Receive", pDrvIns->iInstance);
# ifdef ASYNC_NETIO
PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatRecvOverflows,STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_OCCURENCE, "Profiling packet receive overflows.", "/Drivers/TAP%d/RecvOverflows", pDrvIns->iInstance);
# endif
#endif
return rc;
}
/**
* Host Interface network transport driver registration record.
*/
const PDMDRVREG g_DrvHostInterface =
{
/* u32Version */
/* szDriverName */
"HostInterface",
/* pszDescription */
"Host Interface Network Transport Driver",
/* fFlags */
/* fClass. */
/* cMaxInstances */
~0,
/* cbInstance */
sizeof(DRVTAP),
/* pfnConstruct */
/* pfnDestruct */
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
NULL,
/* pfnSuspend */
NULL,
/* pfnResume */
NULL,
/* pfnDetach */
NULL,
/* pfnPowerOff */
};