DrvIntNet.cpp revision 02e50df6f5a05a0b01164c1a6fe605f328bd62d7
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * DrvIntNet - Internal network transport driver.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Copyright (C) 2006-2007 Sun Microsystems, Inc.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * This file is part of VirtualBox Open Source Edition (OSE), as
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * available from http://www.virtualbox.org. This file is free software;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * you can redistribute it and/or modify it under the terms of the GNU
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * General Public License (GPL) as published by the Free Software
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Foundation, in version 2 as it comes in the "COPYING" file of the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Clara, CA 95054 USA or visit http://www.sun.com if you need
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * additional information or have any questions.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*******************************************************************************
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte* Header Files *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte*******************************************************************************/
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*******************************************************************************
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte* Structures and Typedefs *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte*******************************************************************************/
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The state of the asynchronous thread.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** The thread is suspended. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** The thread is running. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** The thread must (/has) terminate. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** The usual 32-bit type blowup. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Block driver instance data.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortetypedef struct DRVINTNET
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** The network interface. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** The network interface. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** Pointer to the driver instance. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** Interface handle. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** Pointer to the communication buffer. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** The thread state. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** Reader thread. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** Event semaphore the Thread waits on while the VM is suspended. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** Set if the link is down.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * When the link is down all incoming packets will be dropped. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bool volatile fLinkDown;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** Set if data transmission should start immediately and deactivate
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * as late as possible. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** Profiling packet transmit runs. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** Profiling packet receive runs. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif /* VBOX_WITH_STATISTICS */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** The nano ts of the last transfer. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** The nano ts of the last receive. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** The network name. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/** Converts a pointer to DRVINTNET::INetworkConnector to a PDRVINTNET. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define PDMINETWORKCONNECTOR_2_DRVINTNET(pInterface) ( (PDRVINTNET)((uintptr_t)pInterface - RT_OFFSETOF(DRVINTNET, INetworkConnector)) )
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Writes a frame packet to the buffer.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @returns VBox status code.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param pBuf The buffer.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param pRingBuf The ring buffer to read from.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param pvFrame The frame to write.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param cbFrame The size of the frame.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @remark This is the same as INTNETRingWriteFrame
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int drvIntNetRingWriteFrame(PINTNETBUF pBuf, PINTNETRINGBUF pRingBuf, const void *pvFrame, uint32_t cbFrame)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Validate input.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Assert(offWrite == RT_ALIGN_32(offWrite, sizeof(INTNETHDR)));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Assert(offRead == RT_ALIGN_32(offRead, sizeof(INTNETHDR)));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const uint32_t cb = RT_ALIGN_32(cbFrame, sizeof(INTNETHDR));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Try fit it all before the end of the buffer.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (pRingBuf->offEnd - offWrite >= cb + sizeof(INTNETHDR))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte PINTNETHDR pHdr = (PINTNETHDR)((uint8_t *)pBuf + offWrite);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Assert(offWrite <= pRingBuf->offEnd && offWrite >= pRingBuf->offStart);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Log2(("WriteFrame: offWrite: %#x -> %#x (1)\n", pRingBuf->offWrite, offWrite));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Try fit the frame at the start of the buffer.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * (The header fits before the end of the buffer because of alignment.)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte AssertMsg(pRingBuf->offEnd - offWrite >= sizeof(INTNETHDR), ("offEnd=%x offWrite=%x\n", pRingBuf->offEnd, offWrite));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (offRead - pRingBuf->offStart > cb) /* not >= ! */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte PINTNETHDR pHdr = (PINTNETHDR)((uint8_t *)pBuf + offWrite);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte void *pvFrameOut = (PINTNETHDR)((uint8_t *)pBuf + pRingBuf->offStart);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pHdr->offFrame = (intptr_t)pvFrameOut - (intptr_t)pHdr;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Log2(("WriteFrame: offWrite: %#x -> %#x (2)\n", pRingBuf->offWrite, offWrite));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The reader is ahead of the writer, try fit it into that space.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte else if (offRead - offWrite > cb + sizeof(INTNETHDR)) /* not >= ! */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte PINTNETHDR pHdr = (PINTNETHDR)((uint8_t *)pBuf + offWrite);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Log2(("WriteFrame: offWrite: %#x -> %#x (3)\n", pRingBuf->offWrite, offWrite));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* (it didn't fit) */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** @todo stats */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Send data to the network.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @returns VBox status code.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param pInterface Pointer to the interface structure containing the called function pointer.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param pvBuf Data to send.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param cb Number of bytes to send.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @thread EMT
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic DECLCALLBACK(int) drvIntNetSend(PPDMINETWORKCONNECTOR pInterface, const void *pvBuf, size_t cb)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte PDRVINTNET pThis = PDMINETWORKCONNECTOR_2_DRVINTNET(pInterface);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte LogFlow(("drvIntNetSend: %-4d bytes at %llu ns deltas: r=%llu t=%llu\n",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cb, u64Now, u64Now - pThis->u64LastReceiveTS, u64Now - pThis->u64LastTransferTS));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "%.*Vhxd\n",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Add the frame to the send buffer and push it onto the network.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int rc = drvIntNetRingWriteFrame(pThis->pBuf, &pThis->pBuf->Send, pvBuf, cb);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pThis->pDrvIns->pDrvHlp->pfnSUPCallVMMR0Ex(pThis->pDrvIns, VMMR0_DO_INTNET_IF_SEND, &SendReq, sizeof(SendReq));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rc = drvIntNetRingWriteFrame(pThis->pBuf, &pThis->pBuf->Send, pvBuf, cb);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rc = pThis->pDrvIns->pDrvHlp->pfnSUPCallVMMR0Ex(pThis->pDrvIns, VMMR0_DO_INTNET_IF_SEND, &SendReq, sizeof(SendReq));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Set promiscuous mode.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * This is called when the promiscuous mode is set. This means that there doesn't have
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * to be a mode change when it's called.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param pInterface Pointer to the interface structure containing the called function pointer.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param fPromiscuous Set if the adaptor is now in promiscuous mode. Clear if it is not.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @thread EMT
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic DECLCALLBACK(void) drvIntNetSetPromiscuousMode(PPDMINETWORKCONNECTOR pInterface, bool fPromiscuous)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte PDRVINTNET pThis = PDMINETWORKCONNECTOR_2_DRVINTNET(pInterface);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int rc = pThis->pDrvIns->pDrvHlp->pfnSUPCallVMMR0Ex(pThis->pDrvIns, VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE, &Req, sizeof(Req));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte LogFlow(("drvIntNetSetPromiscuousMode: fPromiscuous=%RTbool\n", fPromiscuous));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Notification on link status changes.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param pInterface Pointer to the interface structure containing the called function pointer.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param enmLinkState The new link state.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @thread EMT
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic DECLCALLBACK(void) drvIntNetNotifyLinkChanged(PPDMINETWORKCONNECTOR pInterface, PDMNETWORKLINKSTATE enmLinkState)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte PDRVINTNET pThis = PDMINETWORKCONNECTOR_2_DRVINTNET(pInterface);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte AssertMsgFailed(("enmLinkState=%d\n", enmLinkState));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte LogFlow(("drvIntNetNotifyLinkChanged: enmLinkState=%d %d->%d\n", enmLinkState, pThis->fLinkDown, fLinkDown));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Wait for space to become available up the driver/device chain.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @returns VINF_SUCCESS if space is available.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @returns VERR_STATE_CHANGED if the state changed.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @returns VBox status code on other errors.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param pThis Pointer to the instance data.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int drvIntNetAsyncIoWaitForSpace(PDRVINTNET pThis)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int rc = pThis->pPort->pfnWaitReceiveAvail(pThis->pPort, RT_INDEFINITE_WAIT);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte LogFlow(("drvIntNetAsyncIoWaitForSpace: returns %Vrc\n", rc));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Executes async I/O (RUNNING mode).
return VERR_STATE_CHANGED;
#ifdef LOG_ENABLED
return rc;
return VERR_STATE_CHANGED;
int rc = pDrvIns->pDrvHlp->pfnSUPCallVMMR0Ex(pDrvIns, VMMR0_DO_INTNET_IF_WAIT, &WaitReq, sizeof(WaitReq));
return rc;
switch (enmState)
case ASYNCSTATE_SUSPENDED:
return rc;
case ASYNCSTATE_RUNNING:
return rc;
case ASYNCSTATE_TERMINATE:
return VINF_SUCCESS;
static DECLCALLBACK(void *) drvIntNetQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
switch (enmInterface)
case PDMINTERFACE_BASE:
return NULL;
int rc = pDrvIns->pDrvHlp->pfnSUPCallVMMR0Ex(pDrvIns, VMMR0_DO_INTNET_IF_CLOSE, &CloseReq, sizeof(CloseReq));
if (!CFGMR3AreValuesValid(pCfgHandle, "Network\0ReceiveBufferSize\0SendBufferSize\0RestrictAccess\0IsService\0"))
return VERR_PDM_DRVINS_NO_ATTACH;
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;
bool fRestrictAccess;
fRestrictAccess = true;
LogRel(("DrvIntNet: Warning! SendBufferSize=%u, Recommended minimum size %u butes.\n", OpenReq.cbSend, 1536*2 + 4));
return rc;
rc = pDrvIns->pDrvHlp->pfnSUPCallVMMR0Ex(pDrvIns, VMMR0_DO_INTNET_IF_GET_RING3_BUFFER, &GetRing3BufferReq, sizeof(GetRing3BufferReq));
rc = RTThreadCreate(&pThis->Thread, drvIntNetAsyncIoThread, pThis, _128K, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "INTNET");
return rc;
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->StatTransmit, STAMTYPE_PROFILE, szStatName, STAMUNIT_TICKS_PER_CALL, "Profiling packet transmit runs.");
return rc;
sizeof(DRVINTNET),
NULL,
NULL,
NULL,