DrvTAPOs2.cpp revision 7e74e5d9c5956c67a23435fe7d401a56c31c5ab5
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * VBox network devices: OS/2 TAP network transport driver.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * available from http://www.virtualbox.org. This file is free software;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * you can redistribute it and/or modify it under the terms of the GNU
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * General Public License (GPL) as published by the Free Software
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * additional information or have any questions.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync/*******************************************************************************
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync* Header Files *
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync*******************************************************************************/
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync/*******************************************************************************
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync* Structures and Typedefs *
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync*******************************************************************************/
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * Block driver instance data.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsynctypedef struct DRVTAPOS2
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync /** The network interface. */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync /** The network interface. */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync /** Pointer to the driver instance. */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync /** TAP device file handle. */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync /** Out LAN number. */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync /** The LAN number we're connected to. -1 if not connected. */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync /** Receiver thread. */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync /** Set if the link is down.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * When the link is down all incoming packets will be dropped. */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync bool volatile fLinkDown;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync /** The log and thread name. */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync /** The \DEV\TAP$ device name. */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync /** Number of sent packets. */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync /** Number of sent bytes. */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync /** Number of received packets. */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync /** Number of received bytes. */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync /** Profiling packet transmit runs. */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync /** Profiling packet receive runs. */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync#endif /* VBOX_WITH_STATISTICS */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync /** The nano ts of the last transfer. */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync /** The nano ts of the last receive. */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync/** Converts a pointer to TAP::INetworkConnector to a PRDVTAP. */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync#define PDMINETWORKCONNECTOR_2_DRVTAPOS2(pInterface) ( (PDRVTAPOS2)((uintptr_t)pInterface - RT_OFFSETOF(DRVTAPOS2, INetworkConnector)) )
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * Send data to the network.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * @returns VBox status code.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * @param pInterface Pointer to the interface structure containing the called function pointer.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * @param pvBuf Data to send.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * @param cb Number of bytes to send.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * @thread EMT
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncstatic DECLCALLBACK(int) drvTAPOs2Send(PPDMINETWORKCONNECTOR pInterface, const void *pvBuf, size_t cb)
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync PDRVTAPOS2 pThis = PDMINETWORKCONNECTOR_2_DRVTAPOS2(pInterface);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * If the pvBuf is a high address, we'll have to copy it onto a
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * stack buffer of the tap driver will trap.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync LogFlow(("%s: Send: %-4d bytes at %RU64 ns deltas: recv=%RU64 xmit=%RU64\n", pThis->szName,
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync cb, u64Now, u64Now - pThis->u64LastReceiveTS, u64Now - pThis->u64LastTransferTS));
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync "%.*Vhxd\n",
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync ULONG Parm[2] = { ~0UL, ~0UL }; /* mysterious output */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync int rc = DosDevIOCtl(pThis->hDevice, PROT_CATEGORY, TAP_WRITE_PACKET,
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync static unsigned cComplaints = 0;
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync LogRel(("%s: send failed. rc=%d Parm={%ld,%ld} cb=%d\n",
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync Log3(("%s: Send completed %d ns\n", pThis->szName, RTTimeProgramNanoTS() - pThis->u64LastTransferTS));
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * Set promiscuous mode.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * This is called when the promiscuous mode is set. This means that there doesn't have
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * to be a mode change when it's called.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * @param pInterface Pointer to the interface structure containing the called function pointer.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * @param fPromiscuous Set if the adaptor is now in promiscuous mode. Clear if it is not.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * @thread EMT
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncstatic DECLCALLBACK(void) drvTAPOs2SetPromiscuousMode(PPDMINETWORKCONNECTOR pInterface, bool fPromiscuous)
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync PDRVTAPOS2 pThis = PDMINETWORKCONNECTOR_2_DRVTAPOS2(pInterface);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync LogFlow(("%s: SetPromiscuousMode: fPromiscuous=%d\n", pThis->szName, fPromiscuous));
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync /** @todo is it always in promiscuous mode? */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * Notification on link status changes.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * @param pInterface Pointer to the interface structure containing the called function pointer.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * @param enmLinkState The new link state.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * @thread EMT
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncstatic DECLCALLBACK(void) drvTAPOs2NotifyLinkChanged(PPDMINETWORKCONNECTOR pInterface, PDMNETWORKLINKSTATE enmLinkState)
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync PDRVTAPOS2 pThis = PDMINETWORKCONNECTOR_2_DRVTAPOS2(pInterface);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync AssertMsgFailed(("enmLinkState=%d\n", enmLinkState));
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync LogFlow(("%s: NotifyLinkChanged: enmLinkState=%d %d->%d\n", pThis->szName, pThis->fLinkDown, fLinkDown));
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * Receiver thread.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * @returns VBox status code. Returning failure will naturally terminate the thread.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * @param pDrvIns The pcnet device instance.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * @param pThread The thread.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncstatic DECLCALLBACK(int) drvTAPOs2ReceiveThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync PDRVTAPOS2 pThis = PDMINS2DATA(pDrvIns, PDRVTAPOS2);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * No initialization work to do, just return immediately.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync Assert(pThread->enmState == PDMTHREADSTATE_RUNNING);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * Loop while the thread is running, quit immediately when
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * we're supposed to suspend or terminate.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync while (pThread->enmState == PDMTHREADSTATE_RUNNING)
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * Read a frame, this will block for a while if nothing to read.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync ULONG Parm[2] = { ~0UL, ~0UL }; /* mysterious output */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync ULONG cbParm = sizeof(Parm); /* this one is actually ignored... */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync int rc = DosDevIOCtl(pThis->hDevice, PROT_CATEGORY, TAP_READ_PACKET,
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync AssertMsg(cbRead <= 1536, ("cbRead=%d\n", cbRead));
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * Wait for the device to have some room. A return code != VINF_SUCCESS
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * means that we were woken up during a VM state transition. Drop the
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * current packet and wait for the next one.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync rc = pThis->pPort->pfnWaitReceiveAvail(pThis->pPort, RT_INDEFINITE_WAIT);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * Pass the data up.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync LogFlow(("%s: ReceiveThread: %-4d bytes at %RU64 ns deltas: recv=%RU64 xmit=%RU64\n", pThis->szName,
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync cbRead, u64Now, u64Now - pThis->u64LastReceiveTS, u64Now - pThis->u64LastTransferTS));
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync "%.*Vhxd\n",
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync STAM_COUNTER_ADD(&pThis->StatPktRecvBytes, cbRead);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync rc = pThis->pPort->pfnReceive(pThis->pPort, abBuf, cbRead);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync /* we'll be returning ~1 per second with no data; rc=0 Parm[0] = 1, Parm[1] = 0. */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync LogFlow(("%s: ReceiveThread: DoDevIOCtl -> %s Parm={%ld, %ld}\n",
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync /* The thread is being suspended or terminated. */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * Unblock the send thread so it can respond to a state change.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * @returns VBox status code.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * @param pDrvIns The pcnet device instance.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * @param pThread The send thread.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsyncstatic DECLCALLBACK(int) drvTAPOs2WakeupReceiveThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync PDRVTAPOS2 pThis = PDMINS2DATA(pDrvIns, PDRVTAPOS2);
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync LogFlow(("%s: WakeupReceiveThread\n", pThis->szName));
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync /* cancel any pending reads */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync ULONG Parm[2] = { ~0UL, ~0UL }; /* mysterious output */
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync int orc = DosDevIOCtl(pThis->hDevice, PROT_CATEGORY, TAP_CANCEL_READ,
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * Queries an interface to the driver.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * @returns Pointer to interface.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * @returns NULL if the interface was not supported by the driver.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * @param pInterface Pointer to this interface structure.
cf22150eaeeb72431bf1cf65c309a431454fb22bvboxsync * @param enmInterface The requested interface identification.
static DECLCALLBACK(void *) drvTAPOs2QueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
switch (enmInterface)
case PDMINTERFACE_BASE:
return NULL;
if ( orc
|| Parm[0])
pThis->pPort = (PPDMINETWORKPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_NETWORK_PORT);
if (orc)
else if (Parm[0])
if (orc)
else if (Parm[0])
if ( !orc
&& !Parm[0]
rc = PDMDrvHlpPDMThreadCreate(pDrvIns, &pThis->pThread, pThis, drvTAPOs2ReceiveThread, drvTAPOs2WakeupReceiveThread,
#ifdef VBOX_WITH_STATISTICS
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktSent, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of sent packets.", "/Drivers/TAP%d/Packets/Sent", pDrvIns->iInstance);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktSentBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Number of sent bytes.", "/Drivers/TAP%d/Bytes/Sent", pDrvIns->iInstance);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktRecv, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of received packets.", "/Drivers/TAP%d/Packets/Received", pDrvIns->iInstance);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktRecvBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Number of received bytes.", "/Drivers/TAP%d/Bytes/Received", pDrvIns->iInstance);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatTransmit, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling packet transmit runs.", "/Drivers/TAP%d/Transmit", pDrvIns->iInstance);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatReceive, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling packet receive runs.", "/Drivers/TAP%d/Receive", pDrvIns->iInstance);
return rc;
sizeof(DRVTAPOS2),
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,