DrvTAPOs2.cpp revision a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fc
/** $Id$ */
/** @file
* VBox network devices: OS/2 TAP 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 (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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DRV_TUN
#include <iprt/semaphore.h>
#include "Builtins.h"
#define INCL_BASE
#include <os2.h>
#include "DrvTAPOs2.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Block driver instance data.
*/
typedef struct DRVTAPOS2
{
/** The network interface. */
/** The network interface. */
/** Pointer to the driver instance. */
/** TAP device file handle. */
/** Out LAN number. */
/** The LAN number we're connected to. -1 if not connected. */
/** Receiver thread. */
/** Event semaphore for blocking on receive. */
/** We are checking or waiting for more receive buffers. */
bool volatile fMaybeOutOfSpace;
/** Set if the link is down.
* When the link is down all incoming packets will be dropped. */
bool volatile fLinkDown;
/** The log and thread name. */
char szName[16];
/** The \DEV\TAP$ device name. */
char szDevice[32];
#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. */
#endif /* VBOX_WITH_STATISTICS */
#ifdef LOG_ENABLED
/** The nano ts of the last transfer. */
/** The nano ts of the last receive. */
#endif
} DRVTAPOS2, *PDRVTAPOS2;
/** Converts a pointer to TAP::INetworkConnector to a PRDVTAP. */
#define PDMINETWORKCONNECTOR_2_DRVTAPOS2(pInterface) ( (PDRVTAPOS2)((uintptr_t)pInterface - RT_OFFSETOF(DRVTAPOS2, 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) drvTAPOs2Send(PPDMINETWORKCONNECTOR pInterface, const void *pvBuf, size_t cb)
{
/*
* If the pvBuf is a high address, we'll have to copy it onto a
* stack buffer of the tap driver will trap.
*/
{
}
#ifdef LOG_ENABLED
Log2(("%s Send: pvBuf=%p cb=%#zx\n"
"%.*Vhxd\n",
#endif
{
static unsigned cComplaints = 0;
if (cComplaints++ < 256)
LogRel(("%s: send failed. rc=%d Parm={%ld,%ld} cb=%d\n",
if (rc)
else
}
Log3(("%s: Send completed %d ns\n", pThis->szName, RTTimeProgramNanoTS() - pThis->u64LastTransferTS));
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) drvTAPOs2SetPromiscuousMode(PPDMINETWORKCONNECTOR pInterface, bool fPromiscuous)
{
/** @todo is it always in promiscuous mode? */
}
/**
* 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) drvTAPOs2NotifyLinkChanged(PPDMINETWORKCONNECTOR pInterface, PDMNETWORKLINKSTATE enmLinkState)
{
bool fLinkDown;
switch (enmLinkState)
{
case PDMNETWORKLINKSTATE_DOWN:
fLinkDown = true;
break;
default:
case PDMNETWORKLINKSTATE_UP:
fLinkDown = false;
break;
}
LogFlow(("%s: NotifyLinkChanged: enmLinkState=%d %d->%d\n", pThis->szName, 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.
* @thread EMT
*/
{
/* don't waste time signalling the semaphore unnecessary */
if (!pThis->fMaybeOutOfSpace)
else
{
}
}
/**
* Receiver thread.
*
* @returns VBox status code. Returning failure will naturally terminate the thread.
* @param pDrvIns The pcnet device instance.
* @param pThread The thread.
*/
{
/*
* No initialization work to do, just return immediately.
*/
return VINF_SUCCESS;
/*
* Loop while the thread is running, quit immediately when
* we're supposed to suspend or terminate.
*/
{
/*
* Read a frame, this will block for a while if nothing to read.
*/
char abBuf[4096];
break;
if ( !rc
&& !Parm[0]
&& cbRead > 0 /* cbRead */)
{
/*
* Wait for the device to have some room.
*/
if (cbMax == 0)
{
while ( cbMax == 0
{
}
}
break; /* just drop it, no big deal. */
/*
* Pass the data up.
*/
#ifdef LOG_ENABLED
LogFlow(("%s: ReceiveThread: %-4d bytes at %RU64 ns deltas: recv=%RU64 xmit=%RU64\n", pThis->szName,
#endif
Log2(("%s: ReceiveThread: cbRead=%#x\n"
"%.*Vhxd\n",
}
/* we'll be returning ~1 per second with no data; rc=0 Parm[0] = 1, Parm[1] = 0. */
else if (rc)
{
LogFlow(("%s: ReceiveThread: DoDevIOCtl -> %s Parm={%ld, %ld}\n",
if (rc == VERR_INVALID_HANDLE)
return rc;
}
}
/* The thread is being suspended or terminated. */
return VINF_SUCCESS;
}
/**
* Unblock the send thread so it can respond to a state change.
*
* @returns VBox status code.
* @param pDrvIns The pcnet device instance.
* @param pThread The send thread.
*/
{
/* cancel any pending reads */
/* wake it up if it's waiting for receive buffers. */
if (pThis->fMaybeOutOfSpace)
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 *) drvTAPOs2QueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
{
switch (enmInterface)
{
case PDMINTERFACE_BASE:
return &pThis->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.
*/
{
/* PDM will destroy the thread for us, it's suspended right now. */
/*
* Destroy the out-of-space event semaphore.
*/
{
}
/*
* Disconnect from the lan if we made a connection and close it.
*/
{
if ( orc
|| Parm[0])
LogRel(("%s: Failed to disconnect %d from %d! orc=%d Parm={%ld,%ld}\n",
}
{
}
}
/**
* 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.
*/
/*
* 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.
*/
pThis->pPort = (PPDMINETWORKPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_NETWORK_PORT);
/*
* Read the configuration.
*/
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
else if (VBOX_FAILURE(rc))
N_("Configuration error: Query for \"Device\" failed!"));
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
iConnectTo = -1;
else if (VBOX_FAILURE(rc))
N_("Configuration error: Query for \"ConnectTo\" failed!"));
/*
* Open the device.
* Keep in mind that the destructor is always called!
*/
if (VBOX_FAILURE(rc))
if (orc)
else if (Parm[0])
if (VBOX_FAILURE(rc))
N_("Failed to query LanNumber! orc=%d Parm={%ld,%ld}\n"),
/*
* Connect it requested.
*/
if (iConnectTo != -1)
{
Data = iConnectTo;
if (orc)
else if (Parm[0])
if (VBOX_FAILURE(rc))
N_("Failed to connect %d to %d! orc=%d Parm={%ld,%ld}\n"),
}
/*
* Log the config.
*/
if ( !orc
&& !Parm[0]
/*&& !Parm[1]?*/)
LogRel(("%s: iLan=%d iConnectedTo=%d Mac=%02x:%02x:%02x:%02x:%02x:%02x\n",
else
LogRel(("%s: iLan=%d iConnectedTo Mac=failed - orc=%d Parm={%ld,%ld}\n",
/*
* Create the out-of-space semaphore and the async receiver thread.
*/
rc = PDMDrvHlpPDMThreadCreate(pDrvIns, &pThis->pThread, pThis, drvTAPOs2ReceiveThread, drvTAPOs2WakeupReceiveThread,
#ifdef VBOX_WITH_STATISTICS
/*
* 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);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatRecvOverflows, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_OCCURENCE, "Profiling packet receive overflows.", "/Drivers/TAP%d/RecvOverflows", pDrvIns->iInstance);
#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(DRVTAPOS2),
/* pfnConstruct */
/* pfnDestruct */
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
NULL,
/* pfnSuspend */
NULL,
/* pfnResume */
NULL,
/* pfnDetach */
NULL,
/* pfnPowerOff */
};