DrvIntNet.cpp revision 1d8cb4b9afff2549ac88e5ebc861303809282446
/** @file
*
* VBox network devices:
* Internal network transport driver
*/
/*
* 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 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DRV_INTNET
#include <iprt/semaphore.h>
#include "Builtins.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* The state of the asynchronous thread.
*/
typedef enum ASYNCSTATE
{
/** The thread is suspended. */
ASYNCSTATE_SUSPENDED = 1,
/** The thread is running. */
/** The thread must (/has) terminate. */
/** The usual 32-bit type blowup. */
ASYNCSTATE_32BIT_HACK = 0x7fffffff
} ASYNCSTATE;
/**
* Block driver instance data.
*/
typedef struct DRVINTNET
{
/** The network interface. */
/** The network interface. */
/** Pointer to the driver instance. */
/** Interface handle. */
/** Pointer to the communication buffer. */
/** The thread state. */
ASYNCSTATE volatile enmState;
/** Reader thread. */
/** Event semaphore the Thread waits on while the VM is suspended. */
/** Indicates that we're in waiting for recieve space to become available. */
bool volatile fOutOfSpace;
/** Event semaphore the Thread sleeps on while polling for more
* buffer space to become available.
* @todo We really need the network device to signal this! */
/** Set if the link is down.
* When the link is down all incoming packets will be dropped. */
bool volatile fLinkDown;
#ifdef VBOX_WITH_STATISTICS
/** Profiling packet transmit runs. */
/** Profiling packet receive runs. */
/** Number of receive overflows. */
#endif /* VBOX_WITH_STATISTICS */
#ifdef LOG_ENABLED
/** The nano ts of the last transfer. */
/** The nano ts of the last receive. */
#endif
/** The network name. */
char szNetwork[INTNET_MAX_NETWORK_NAME];
} DRVINTNET, *PDRVINTNET;
/** Converts a pointer to DRVINTNET::INetworkConnector to a PDRVINTNET. */
#define PDMINETWORKCONNECTOR_2_DRVINTNET(pInterface) ( (PDRVINTNET)((uintptr_t)pInterface - RT_OFFSETOF(DRVINTNET, INetworkConnector)) )
/**
* Writes a frame packet to the buffer.
*
* @returns VBox status code.
* @param pBuf The buffer.
* @param pRingBuf The ring buffer to read from.
* @param pvFrame The frame to write.
* @param cbFrame The size of the frame.
* @remark This is the same as INTNETRingWriteFrame
*/
static int drvIntNetRingWriteFrame(PINTNETBUF pBuf, PINTNETRINGBUF pRingBuf, const void *pvFrame, uint32_t cbFrame)
{
/*
* Validate input.
*/
{
/*
* Try fit it all before the end of the buffer.
*/
{
return VINF_SUCCESS;
}
/*
* Try fit the frame at the start of the buffer.
* (The header fits before the end of the buffer because of alignment.)
*/
AssertMsg(pRingBuf->offEnd - offWrite >= sizeof(INTNETHDR), ("offEnd=%x offWrite=%x\n", pRingBuf->offEnd, offWrite));
{
return VINF_SUCCESS;
}
}
/*
* The reader is ahead of the writer, try fit it into that space.
*/
{
return VINF_SUCCESS;
}
/* (it didn't fit) */
/** @todo stats */
return VERR_BUFFER_OVERFLOW;
}
/**
* 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) drvIntNetSend(PPDMINETWORKCONNECTOR pInterface, const void *pvBuf, size_t cb)
{
#ifdef LOG_ENABLED
LogFlow(("drvIntNetSend: %-4d bytes at %llu ns deltas: r=%llu t=%llu\n",
Log2(("drvIntNetSend: pvBuf=%p cb=%#x\n"
"%.*Vhxd\n",
#endif
/*
* Add the frame to the send buffer and push it onto the network.
*/
if ( rc == VERR_BUFFER_OVERFLOW
{
pThis->pDrvIns->pDrvHlp->pfnSUPCallVMMR0Ex(pThis->pDrvIns, VMMR0_DO_INTNET_IF_SEND, &SendReq, sizeof(SendReq));
}
if (RT_SUCCESS(rc))
{
rc = pThis->pDrvIns->pDrvHlp->pfnSUPCallVMMR0Ex(pThis->pDrvIns, VMMR0_DO_INTNET_IF_SEND, &SendReq, sizeof(SendReq));
}
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) drvIntNetSetPromiscuousMode(PPDMINETWORKCONNECTOR pInterface, bool fPromiscuous)
{
int rc = pThis->pDrvIns->pDrvHlp->pfnSUPCallVMMR0Ex(pThis->pDrvIns, VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE, &Req, sizeof(Req));
}
/**
* 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) drvIntNetNotifyLinkChanged(PPDMINETWORKCONNECTOR pInterface, PDMNETWORKLINKSTATE enmLinkState)
{
bool fLinkDown;
switch (enmLinkState)
{
case PDMNETWORKLINKSTATE_DOWN:
fLinkDown = true;
break;
default:
case PDMNETWORKLINKSTATE_UP:
fLinkDown = false;
break;
}
LogFlow(("drvIntNetNotifyLinkChanged: enmLinkState=%d %d->%d\n", enmLinkState, pThis->fLinkDown, fLinkDown));
}
/**
* 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.
* @remark This function isn't called by pcnet nor yet.
* @thread EMT
*/
{
if (pThis->fOutOfSpace)
{
LogFlow(("drvIntNetNotifyCanReceive: signaling\n"));
}
}
/**
*
* @returns VINF_SUCCESS if space is available.
* @returns VERR_STATE_CHANGED if the state changed.
* @returns VBox status code on other errors.
* @param pThis Pointer to the instance data.
* @param cbFrame The frame size.
*/
{
int rc;
unsigned cYields = 0;
for (;;)
{
if ( !RTThreadYield()
|| ++cYields % 100 == 0)
{
/** @todo we need a callback from the device which can wake us up here. */
if ( VBOX_FAILURE(rc)
&& rc != VERR_TIMEOUT)
break;
}
{
break;
}
/* retry */
{
rc = VINF_SUCCESS;
break;
}
}
return rc;
}
/**
* Executes async I/O (RUNNING mode).
*
* @returns VERR_STATE_CHANGED if the state changed.
* @returns Appropriate VBox status code (error) on fatal error.
* @param pThis The driver instance data.
*/
{
/*
* The running loop - processing received data and waiting for more to arrive.
*/
for (;;)
{
/*
* Process the receive buffer.
*/
while (INTNETRingGetReadable(pRingBuf) > 0)
{
/*
* Check the state and then inspect the packet.
*/
{
LogFlow(("drvIntNetAsyncIoRun: returns VERR_STATE_CHANGED (state changed - #0)\n"));
return VERR_STATE_CHANGED;
}
{
/*
* Check if there is room for the frame and pass it up.
*/
{
#ifdef LOG_ENABLED
LogFlow(("drvIntNetAsyncIoRun: %-4d bytes at %llu ns deltas: r=%llu t=%llu\n",
Log2(("drvIntNetAsyncIoRun: cbFrame=%#x\n"
"%.*Vhxd\n",
#endif
/* skip to the next frame. */
}
else
{
/*
* Wait for sufficient space to become available and then retry.
*/
if (VBOX_FAILURE(rc))
{
return rc;
}
}
}
else
{
/*
* Link down or unknown frame - skip to the next frame.
*/
}
} /* while more received data */
/*
* Wait for data, checking the state before we block.
*/
{
LogFlow(("drvIntNetAsyncIoRun: returns VINF_SUCCESS (state changed - #1)\n"));
return VERR_STATE_CHANGED;
}
int rc = pDrvIns->pDrvHlp->pfnSUPCallVMMR0Ex(pDrvIns, VMMR0_DO_INTNET_IF_WAIT, &WaitReq, sizeof(WaitReq));
if ( VBOX_FAILURE(rc)
&& rc != VERR_TIMEOUT
&& rc != VERR_INTERRUPTED)
{
return rc;
}
}
}
/**
* Asynchronous I/O thread for handling receive.
*
* @returns VINF_SUCCESS (ignored).
* @param ThreadSelf Thread handle.
* @param pvUser Pointer to a DRVINTNET structure.
*/
{
/*
* The main loop - acting on state.
*/
for (;;)
{
switch (enmState)
{
case ASYNCSTATE_SUSPENDED:
{
if ( VBOX_FAILURE(rc)
&& rc != VERR_TIMEOUT)
{
return rc;
}
break;
}
case ASYNCSTATE_RUNNING:
{
if ( rc != VERR_STATE_CHANGED
&& VBOX_FAILURE(rc))
{
return rc;
}
break;
}
default:
case ASYNCSTATE_TERMINATE:
LogFlow(("drvIntNetAsyncIoThread: returns VINF_SUCCESS\n"));
return VINF_SUCCESS;
}
}
}
/**
* 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 *) drvIntNetQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
{
switch (enmInterface)
{
case PDMINTERFACE_BASE:
return &pThis->INetworkConnector;
default:
return NULL;
}
}
/**
* Power Off notification.
*
* @param pDrvIns The driver instance.
*/
{
LogFlow(("drvIntNetPowerOff\n"));
}
/**
* Resume notification.
*
* @param pDrvIns The driver instance.
*/
{
LogFlow(("drvIntNetPowerResume\n"));
}
/**
* Suspend notification.
*
* @param pDrvIns The driver instance.
*/
{
LogFlow(("drvIntNetPowerSuspend\n"));
}
/**
* Power On notification.
*
* @param pDrvIns The driver instance.
*/
{
LogFlow(("drvIntNetPowerOn\n"));
}
/**
* 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(("drvIntNetDestruct\n"));
/*
* Indicate to the thread that it's time to quit.
*/
/*
* Close the interface
*/
{
int rc = pDrvIns->pDrvHlp->pfnSUPCallVMMR0Ex(pDrvIns, VMMR0_DO_INTNET_IF_CLOSE, &CloseReq, sizeof(CloseReq));
}
/*
* Wait for the thread to terminate.
*/
{
if (EventOutOfSpace != NIL_RTSEMEVENT)
if (EventSuspended != NIL_RTSEMEVENT)
}
/*
* Destroy the semaphores.
*/
if (EventOutOfSpace != NIL_RTSEMEVENT)
if (EventSuspended != NIL_RTSEMEVENT)
}
/**
* 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.
*/
/* IBase */
/* INetwork */
/*
* Validate the config.
*/
if (!CFGMR3AreValuesValid(pCfgHandle, "Network\0ReceiveBufferSize\0SendBufferSize\0RestrictAccess\0"))
/*
* Check that no-one is attached to us.
*/
if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
{
AssertMsgFailed(("Configuration error: Cannot attach drivers to the TAP driver!\n"));
return VERR_PDM_DRVINS_NO_ATTACH;
}
/*
* Query the network port interface.
*/
pThis->pPort = (PPDMINETWORKPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_NETWORK_PORT);
{
AssertMsgFailed(("Configuration error: the above device/driver didn't export the network port interface!\n"));
return VERR_PDM_MISSING_INTERFACE_ABOVE;
}
/*
* Read the configuration.
*/
if (VBOX_FAILURE(rc))
N_("Configuration error: Failed to get the \"Network\" value"));
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
else if (VBOX_FAILURE(rc))
N_("Configuration error: Failed to get the \"ReceiveBufferSize\" value"));
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
else if (VBOX_FAILURE(rc))
N_("Configuration error: Failed to get the \"SendBufferSize\" value"));
N_("Configuration error: The \"SendBufferSize\" value is too small."));
LogRel(("DrvIntNet: Warning! SendBufferSize=%u, Recommended minimum size %u butes.\n", OpenReq.cbSend, 1536*2 + 4));
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
OpenReq.fRestrictAccess = true;
else if (VBOX_FAILURE(rc))
N_("Configuration error: Failed to get the \"RestrictAccess\" value"));
/*
* Create the event semaphores
*/
if (VBOX_FAILURE(rc))
return rc;
if (VBOX_FAILURE(rc))
return rc;
/*
* Create the interface.
*/
if (VBOX_FAILURE(rc))
/*
* Get default buffer.
*/
rc = pDrvIns->pDrvHlp->pfnSUPCallVMMR0Ex(pDrvIns, VMMR0_DO_INTNET_IF_GET_RING3_BUFFER, &GetRing3BufferReq, sizeof(GetRing3BufferReq));
if (VBOX_FAILURE(rc))
/*
* Create the async I/O thread.
*/
rc = RTThreadCreate(&pThis->Thread, drvIntNetAsyncIoThread, pThis, _128K, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "INTNET");
if (VBOX_FAILURE(rc))
{
return rc;
}
char szStatName[64];
pDrvIns->pDrvHlp->pfnSTAMRegister(pDrvIns, &pThis->pBuf->cbStatRecv, STAMTYPE_COUNTER, szStatName, STAMUNIT_BYTES, "Number of received bytes.");
pDrvIns->pDrvHlp->pfnSTAMRegister(pDrvIns, &pThis->pBuf->cbStatSend, STAMTYPE_COUNTER, szStatName, STAMUNIT_BYTES, "Number of sent bytes.");
pDrvIns->pDrvHlp->pfnSTAMRegister(pDrvIns, &pThis->pBuf->cStatRecvs, STAMTYPE_COUNTER, szStatName, STAMUNIT_OCCURENCES, "Number of received packets.");
pDrvIns->pDrvHlp->pfnSTAMRegister(pDrvIns, &pThis->pBuf->cStatSends, STAMTYPE_COUNTER, szStatName, STAMUNIT_OCCURENCES, "Number of sent packets.");
pDrvIns->pDrvHlp->pfnSTAMRegister(pDrvIns, &pThis->pBuf->cStatLost, STAMTYPE_COUNTER, szStatName, STAMUNIT_OCCURENCES, "Number of lost packets.");
pDrvIns->pDrvHlp->pfnSTAMRegister(pDrvIns, &pThis->pBuf->cStatYieldsOk, STAMTYPE_COUNTER, szStatName, STAMUNIT_OCCURENCES, "Number of times yielding fixed an overflow.");
pDrvIns->pDrvHlp->pfnSTAMRegister(pDrvIns, &pThis->pBuf->cStatYieldsNok, STAMTYPE_COUNTER, szStatName, STAMUNIT_OCCURENCES, "Number of times yielding didn't help fix an overflow.");
#ifdef VBOX_WITH_STATISTICS
pDrvIns->pDrvHlp->pfnSTAMRegister(pDrvIns, &pThis->StatReceive, STAMTYPE_PROFILE, szStatName, STAMUNIT_TICKS_PER_CALL, "Profiling packet receive runs.");
pDrvIns->pDrvHlp->pfnSTAMRegister(pDrvIns, &pThis->StatRecvOverflows, STAMTYPE_PROFILE, szStatName, STAMUNIT_TICKS_PER_OCCURENCE, "Profiling packet receive overflows.");
pDrvIns->pDrvHlp->pfnSTAMRegister(pDrvIns, &pThis->StatTransmit, STAMTYPE_PROFILE, szStatName, STAMUNIT_TICKS_PER_CALL, "Profiling packet transmit runs.");
#endif
LogRel(("IntNet#%u: cbRecv=%u cbSend=%u fRestrictAccess=%d\n", pDrvIns->iInstance, OpenReq.cbRecv, OpenReq.cbSend, OpenReq.fRestrictAccess));
return rc;
}
/**
* Internal networking transport driver registration record.
*/
const PDMDRVREG g_DrvIntNet =
{
/* u32Version */
/* szDriverName */
"IntNet",
/* pszDescription */
"Internal Networking Transport Driver",
/* fFlags */
/* fClass. */
/* cMaxInstances */
~0,
/* cbInstance */
sizeof(DRVINTNET),
/* pfnConstruct */
/* pfnDestruct */
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
/* pfnReset */
NULL,
/* pfnSuspend */
/* pfnResume */
/* pfnDetach */
NULL,
/* pfnPowerOff */
};