DrvTAP.cpp revision 419e42bcfeaa6699fbcf5a406e44bf1d9b376ddb
/** @file
*
* VBox network devices:
* Linux TAP network transport driver
*/
/*
*
* Copyright (C) 2006 InnoTek Systemberatung 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 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.
*
* If you received this file as part of a commercial VirtualBox
* distribution, then only the terms of your commercial VirtualBox
* license agreement apply instead of the previous paragraph.
*
*/
#define ASYNC_NET
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DRV_TUN
#ifdef ASYNC_NET
#include <iprt/semaphore.h>
#endif
#include <errno.h>
#ifdef ASYNC_NET
#include <unistd.h>
#endif
#include "Builtins.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
typedef enum ASYNCSTATE
{
//ASYNCSTATE_SUSPENDED = 1,
} ASYNCSTATE;
/**
* Block driver instance data.
*/
typedef struct DRVTAP
{
/** The network interface. */
/** The network interface. */
/** Pointer to the driver instance. */
/** TAP device file handle. */
#ifdef ASYNC_NET
/** The write end of the control pipe. */
/** The read end of the control pipe. */
/** The thread state. */
ASYNCSTATE volatile enmState;
/** Reader thread. */
/** We are waiting for more receive buffers. */
uint32_t volatile fOutOfSpace;
/** Event semaphore for blocking on receive. */
#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_NET
#endif
#endif /* VBOX_WITH_STATISTICS */
#ifdef LOG_ENABLED
/** The nano ts of the last transfer. */
/** The nano ts of the last receive. */
#endif
/** Converts a pointer to TAP::INetworkConnector to a PRDVTAP. */
#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
*/
{
#ifdef LOG_ENABLED
LogFlow(("drvTAPSend: %-4d bytes at %llu ns deltas: r=%llu t=%llu\n",
#endif
Log2(("drvTAPSend: pvBuf=%p cb=%#x\n"
"%.*Vhxd\n",
return rc;
}
/**
* Send multiple data packets to the network.
*
* @returns VBox status code.
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @param cPackets Number of packets
* @param paPacket Packet description array
* @thread EMT
*/
static DECLCALLBACK(int) drvTAPSendEx(PPDMINETWORKCONNECTOR pInterface, uint32_t cPackets, PPDMINETWORKPACKET paPacket)
{
int rc = VERR_INVALID_PARAMETER;
{
if (VBOX_FAILURE(rc))
break;
}
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) drvTAPSetPromiscuousMode(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) drvTAPNotifyLinkChanged(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(("drvTAPNotifyCanReceive:\n"));
/* ensure we wake up only once */
}
#ifdef ASYNC_NET
/**
* Asynchronous I/O thread for handling receive.
*
* @returns VINF_SUCCESS (ignored).
* @param Thread Thread handle.
* @param pvUser Pointer to a DRVTAP structure.
*/
{
/*
* Polling loop.
*/
for (;;)
{
/*
* Wait for something to become available.
*/
errno=0;
if ( rc > 0
{
/*
* Read the frame.
*/
char achBuf[4096];
unsigned cbRead = 0;
if (VBOX_SUCCESS(rc))
{
/*
* Wait for the device to have space for this frame.
*/
{
/** @todo receive overflow handling needs serious improving! */
{
#if 1
/* We get signalled by the network driver. 50ms is just for sanity */
#else
RTThreadSleep(1);
#endif
}
break;
}
/*
* Pass the data up.
*/
#ifdef LOG_ENABLED
LogFlow(("drvTAPAsyncIoThread: %-4d bytes at %llu ns deltas: r=%llu t=%llu\n",
#endif
Log2(("drvTAPAsyncIoThread: cbRead=%#x\n"
"%.*Vhxd\n",
}
else
{
if (rc == VERR_INVALID_HANDLE)
break;
}
}
else if ( rc > 0
{
LogFlow(("drvTAPAsyncIoThread: Control message: enmState=%d revents=%#x\n", pData->enmState, aFDs[1].revents));
break;
break;
/* drain the pipe */
char ch;
unsigned cbRead;
}
else
{
/*
* poll() failed for some reason. Yield to avoid eating too much CPU.
*
* EINTR errors have been seen frequently. They should be harmless, even
* if they are not supposed to occur in our setup.
*/
Log(("rc=%d revents=%#x,%#x errno=%p %s\n", rc, aFDs[0].revents, aFDs[1].revents, errno, strerror(errno)));
else
AssertMsgFailed(("rc=%d revents=%#x,%#x errno=%p %s\n", rc, aFDs[0].revents, aFDs[1].revents, errno, strerror(errno)));
}
}
return VINF_SUCCESS;
}
#else
/**
* Poller callback.
*/
{
while (cbMax > 0)
{
/* check for data to read */
{
{
/* data waiting, read it. */
char achBuf[4096];
unsigned cbRead = 0;
if (VBOX_SUCCESS(rc))
{
/* push it up to guy over us. */
Log2(("drvTAPPoller: cbRead=%#x\n"
"%.*Vhxd\n",
}
else
break;
}
else
break;
}
else
break;
}
}
#endif
/**
* 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.
*/
{
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(("drvTAPDestruct\n"));
#ifdef ASYNC_NET
/*
* Terminate the Async I/O Thread.
*/
{
/* Ensure that it does not spin in the CanReceive loop */
}
/*
* Terminate the control pipe.
*/
{
}
{
}
#endif
}
/**
* Construct a TAP 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.
*/
#ifdef ASYNC_NET
#endif
/* 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 TAP driver!"));
/*
* Query the network port interface.
*/
pData->pPort = (PPDMINETWORKPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_NETWORK_PORT);
/*
* Read the configuration.
*/
if (VBOX_FAILURE(rc))
N_("Configuration error: Query for \"FileHandle\" 32-bit signed integer failed!"));
/*
* Make sure the descriptor is non-blocking and valid.
*
* We should actually query if it's a TAP device, but I haven't
* found any way to do that.
*/
/** @todo determine device name. This can be done by reading the link /proc/<pid>/fd/<fd> */
rc = VINF_SUCCESS;
#ifdef ASYNC_NET
/*
* Create the control pipe.
*/
int fds[2];
{
return rc;
}
/*
* Create the async I/O thread.
*/
rc = RTThreadCreate(&pData->Thread, drvTAPAsyncIoThread, pData, 128*_1K, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "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_NET
PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatRecvOverflows, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_OCCURENCE, "Profiling packet receive overflows.", "/Drivers/TAP%d/RecvOverflows", pDrvIns->iInstance);
# endif
#endif /* VBOX_WITH_STATISTICS */
return rc;
}
/**
* TAP network transport driver registration record.
*/
const PDMDRVREG g_DrvHostInterface =
{
/* u32Version */
/* szDriverName */
"HostInterface",
/* pszDescription */
"TAP Network Transport Driver",
/* fFlags */
/* fClass. */
/* cMaxInstances */
~0,
/* cbInstance */
sizeof(DRVTAP),
/* pfnConstruct */
/* pfnDestruct */
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
NULL,
/* pfnSuspend */
NULL, /** @todo Do power on, suspend and resume handlers! */
/* pfnResume */
NULL,
/* pfnDetach */
NULL,
/* pfnPowerOff */
};