DrvVDE.cpp revision 7956ae64247e1b9d5f5a6030cbea223ce745fd67
/* $Id$ */
/** @file
* VDE network transport driver.
*/
/*
* Contributed by Renzo Davoli. VirtualSquare. University of Bologna, 2010
* Copyright (C) 2006-2010 Oracle Corporation
*
* 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 <errno.h>
#include <unistd.h>
#include "VBoxDD.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* VDE driver instance data.
*
* @implements PDMINETWORKUP
*/
typedef struct DRVVDE
{
/** The network interface. */
/** The network interface. */
/** Pointer to the driver instance. */
/** VDE device file handle. */
/** The configured VDE device name. */
char *pszDeviceName;
/** The write end of the control pipe. */
/** The read end of the control pipe. */
/** Reader thread. */
/** The connection to the VDE switch */
/** @todo The transmit thread. */
/** Transmit lock used by drvTAPNetworkUp_BeginXmit. */
#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
/** Converts a pointer to VDE::INetworkUp to a PRDVVDE. */
#define PDMINETWORKUP_2_DRVVDE(pInterface) ( (PDRVVDE)((uintptr_t)pInterface - RT_OFFSETOF(DRVVDE, INetworkUp)) )
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/**
* @interface_method_impl{PDMINETWORKUP,pfnBeginXmit}
*/
{
if (RT_FAILURE(rc))
{
/** @todo XMIT thread */
rc = VERR_TRY_AGAIN;
}
return rc;
}
/**
* @interface_method_impl{PDMINETWORKUP,pfnAllocBuf}
*/
{
/*
* Allocate a scatter / gather buffer descriptor that is immediately
* followed by the buffer space of its single segment. The GSO context
* comes after that again.
*/
if (!pSgBuf)
return VERR_NO_MEMORY;
/*
* Initialize the S/G buffer and return.
*/
if (!pGso)
else
{
}
#if 0 /* poison */
#endif
return VINF_SUCCESS;
}
/**
* @interface_method_impl{PDMINETWORKUP,pfnFreeBuf}
*/
static DECLCALLBACK(int) drvVDENetworkUp_FreeBuf(PPDMINETWORKUP pInterface, PPDMSCATTERGATHER pSgBuf)
{
if (pSgBuf)
{
}
return VINF_SUCCESS;
}
/**
* @interface_method_impl{PDMINETWORKUP,pfnSendBuf}
*/
static DECLCALLBACK(int) drvVDENetworkUp_SendBuf(PPDMINETWORKUP pInterface, PPDMSCATTERGATHER pSgBuf, bool fOnWorkerThread)
{
/* Set an FTM checkpoint as this operation changes the state permanently. */
int rc;
{
#ifdef LOG_ENABLED
LogFlow(("drvVDESend: %-4d bytes at %llu ns deltas: r=%llu t=%llu\n",
#endif
Log2(("drvVDESend: pSgBuf->aSegs[0].pvSeg=%p pSgBuf->cbUsed=%#x\n"
"%.*Rhxd\n",
}
else
{
rc = 0;
{
if (RT_FAILURE(rc))
break;
}
}
if (RT_FAILURE(rc))
return rc;
}
/**
* @interface_method_impl{PDMINETWORKUP,pfnEndXmit}
*/
{
}
/**
* @interface_method_impl{PDMINETWORKUP,pfnSetPromiscuousMode}
*/
static DECLCALLBACK(void) drvVDENetworkUp_SetPromiscuousMode(PPDMINETWORKUP 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) drvVDENetworkUp_NotifyLinkChanged(PPDMINETWORKUP pInterface, PDMNETWORKLINKSTATE enmLinkState)
{
/** @todo take action on link down and up. Stop the polling and such like. */
}
/**
* Asynchronous I/O thread for handling receive.
*
* @returns VINF_SUCCESS (ignored).
* @param Thread Thread handle.
* @param pvUser Pointer to a DRVVDE structure.
*/
{
return VINF_SUCCESS;
/*
* Polling loop.
*/
{
/*
* Wait for something to become available.
*/
errno=0;
/* this might have changed in the meantime */
break;
if ( rc > 0
{
/*
* Read the frame.
*/
char achBuf[16384];
if (RT_SUCCESS(rc))
{
/*
* Wait for the device to have space for this frame.
* Most guests use frame-sized receive buffers, hence non-zero cbMax
* automatically means there is enough room for entire frame. Some
* guests (eg. Solaris) use large chains of small receive buffers
* (each 128 or so bytes large). We will still start receiving as soon
* as cbMax is non-zero because:
* - it would be quite expensive for pfnCanReceive to accurately
* determine free receive buffer space
* - if we were waiting for enough free buffers, there is a risk
* of deadlocking because the guest could be waiting for a receive
* overflow error to allocate more receive buffers
*/
/*
* A return code != VINF_SUCCESS means that we were woken up during a VM
* state transition. Drop the packet and wait for the next one.
*/
if (RT_FAILURE(rc1))
continue;
/*
* Pass the data up.
*/
#ifdef LOG_ENABLED
LogFlow(("drvVDEAsyncIoThread: %-4d bytes at %llu ns deltas: r=%llu t=%llu\n",
#endif
}
else
{
if (rc == VERR_INVALID_HANDLE)
break;
}
}
else if ( rc > 0
{
LogFlow(("drvVDEAsyncIoThread: Control message: enmState=%d revents=%#x\n", pThread->enmState, aFDs[1].revents));
break;
/* drain the pipe */
char ch;
}
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;
}
/**
* Unblock the send thread so it can respond to a state change.
*
* @returns VBox status code.
* @param pDevIns The pcnet device instance.
* @param pThread The send thread.
*/
{
return VINF_SUCCESS;
}
/* -=-=-=-=- PDMIBASE -=-=-=-=- */
/**
* @interface_method_impl{PDMIBASE,pfnQueryInterface}
*/
{
return NULL;
}
/* -=-=-=-=- PDMDRVREG -=-=-=-=- */
/**
* 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(("drvVDEDestruct\n"));
/*
* Terminate the control pipe.
*/
{
}
{
}
/*
* Kill the xmit lock.
*/
#ifdef VBOX_WITH_STATISTICS
/*
* Deregister statistics.
*/
#endif /* VBOX_WITH_STATISTICS */
}
/**
* Construct a VDE network transport driver instance.
*
* @copydoc FNPDMDRVCONSTRUCT
*/
{
/*
* Init the static parts.
*/
/* IBase */
/* INetwork */
#ifdef VBOX_WITH_STATISTICS
/*
* Statistics.
*/
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktSent, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of sent packets.", "/Drivers/VDE%d/Packets/Sent", pDrvIns->iInstance);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktSentBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Number of sent bytes.", "/Drivers/VDE%d/Bytes/Sent", pDrvIns->iInstance);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktRecv, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of received packets.", "/Drivers/VDE%d/Packets/Received", pDrvIns->iInstance);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktRecvBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Number of received bytes.", "/Drivers/VDE%d/Bytes/Received", pDrvIns->iInstance);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatTransmit, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling packet transmit runs.", "/Drivers/VDE%d/Transmit", pDrvIns->iInstance);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatReceive, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling packet receive runs.", "/Drivers/VDE%d/Receive", pDrvIns->iInstance);
#endif /* VBOX_WITH_STATISTICS */
/*
* Validate the config.
*/
/*
* Check that no-one is attached to us.
*/
("Configuration error: Not possible to attach anything to this driver!\n"),
/*
* Query the network port interface.
*/
if (!pThis->pIAboveNet)
/*
* Read the configuration.
*/
int rc;
char szNetwork[RTPATH_MAX];
if (RT_FAILURE(rc))
*szNetwork=0;
if (RT_FAILURE(DrvVDELoadVDEPlug()))
N_("VDEplug library: not found"));
N_("Failed to connect to the VDE SWITCH"));
/*
* Create the transmit lock.
*/
/*
* Create the control pipe.
*/
int fds[2];
{
return rc;
}
/*
* Create the async I/O thread.
*/
rc = PDMDrvHlpThreadCreate(pDrvIns, &pThis->pThread, pThis, drvVDEAsyncIoThread, drvVDEAsyncIoWakeup, 128 * _1K, RTTHREADTYPE_IO, "VDE");
return rc;
}
/**
* VDE network transport driver registration record.
*/
{
/* u32Version */
/* szName */
"VDE",
/* szRCMod */
"",
/* szR0Mod */
"",
/* pszDescription */
"VDE Network Transport Driver",
/* fFlags */
/* fClass. */
/* cMaxInstances */
~0,
/* cbInstance */
sizeof(DRVVDE),
/* pfnConstruct */
/* pfnDestruct */
/* pfnRelocate */
NULL,
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
NULL,
/* pfnSuspend */
NULL, /** @todo Do power on, suspend and resume handlers! */
/* pfnResume */
NULL,
/* pfnAttach */
NULL,
/* pfnDetach */
NULL,
/* pfnPowerOff */
NULL,
/* pfnSoftReset */
NULL,
/* u32EndVersion */
};