DrvUDPTunnel.cpp revision c58f1213e628a545081c70e26c6b67a841cff880
/* $Id$ */
/** @file
* DrvUDPTunnel - UDP tunnel network transport driver
*
* Based on code contributed by Christophe Devriese
*/
/*
* Copyright (C) 2009-2012 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_UDPTUNNEL
#include <iprt/critsect.h>
#include "VBoxDD.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* UDP tunnel driver instance data.
*
* @implements PDMINETWORKUP
*/
typedef struct DRVUDPTUNNEL
{
/** The network interface. */
/** The network interface. */
/** Pointer to the driver instance. */
/** UDP tunnel source port. */
/** UDP tunnel destination port. */
/** UDP tunnel destination IP address. */
char *pszDestIP;
/** UDP tunnel instance string. */
char *pszInstance;
/** UDP destination address. */
/** Transmit lock used by drvUDPTunnelUp_BeginXmit. */
/** Server data structure for UDP communication. */
/** Flag whether the link is down. */
bool volatile fLinkDown;
#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 UDPTUNNEL::INetworkUp to a PRDVUDPTUNNEL. */
#define PDMINETWORKUP_2_DRVUDPTUNNEL(pInterface) ( (PDRVUDPTUNNEL)((uintptr_t)pInterface - RT_OFFSETOF(DRVUDPTUNNEL, 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) drvUDPTunnelUp_FreeBuf(PPDMINETWORKUP pInterface, PPDMSCATTERGATHER pSgBuf)
{
if (pSgBuf)
{
}
return VINF_SUCCESS;
}
/**
* @interface_method_impl{PDMINETWORKUP,pfnSendBuf}
*/
static DECLCALLBACK(int) drvUDPTunnelUp_SendBuf(PPDMINETWORKUP pInterface, PPDMSCATTERGATHER pSgBuf, bool fOnWorkerThread)
{
/* Set an FTM checkpoint as this operation changes the state permanently. */
int rc;
{
#ifdef LOG_ENABLED
LogFunc(("%-4d bytes at %llu ns deltas: r=%llu t=%llu\n",
#endif
Log2(("pSgBuf->aSegs[0].pvSeg=%p pSgBuf->cbUsed=%#x\n%.*Rhxd\n",
}
else
{
rc = VINF_SUCCESS;
{
}
}
if (RT_FAILURE(rc))
{
if (rc == VERR_NO_MEMORY)
else
rc = VERR_NET_DOWN;
}
return rc;
}
/**
* @interface_method_impl{PDMINETWORKUP,pfnEndXmit}
*/
{
}
/**
* @interface_method_impl{PDMINETWORKUP,pfnSetPromiscuousMode}
*/
static DECLCALLBACK(void) drvUDPTunnelUp_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) drvUDPTunnelUp_NotifyLinkChanged(PPDMINETWORKUP pInterface, PDMNETWORKLINKSTATE enmLinkState)
{
bool fLinkDown;
switch (enmLinkState)
{
case PDMNETWORKLINKSTATE_DOWN:
fLinkDown = true;
break;
default:
case PDMNETWORKLINKSTATE_UP:
fLinkDown = false;
break;
}
}
{
/*
* 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(rc))
{
return VINF_SUCCESS;
}
/*
* Pass the data up.
*/
#ifdef LOG_ENABLED
LogFunc(("%-4d bytes at %llu ns deltas: r=%llu t=%llu\n",
#endif
}
}
else
{
if (rc == VERR_INVALID_HANDLE)
return VERR_UDP_SERVER_STOP;
}
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.
*/
{
LogFlowFunc(("\n"));
if (pThis->pszInstance)
{
}
/*
* Kill the xmit lock.
*/
#ifdef VBOX_WITH_STATISTICS
/*
* Deregister statistics.
*/
#endif /* VBOX_WITH_STATISTICS */
}
/**
* Construct a UDP tunnel 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/UDPTunnel%d/Packets/Sent", pDrvIns->iInstance);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktSentBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Number of sent bytes.", "/Drivers/UDPTunnel%d/Bytes/Sent", pDrvIns->iInstance);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktRecv, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of received packets.", "/Drivers/UDPTunnel%d/Packets/Received", pDrvIns->iInstance);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktRecvBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Number of received bytes.", "/Drivers/UDPTunnel%d/Bytes/Received", pDrvIns->iInstance);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatTransmit, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling packet transmit runs.", "/Drivers/UDPTunnel%d/Transmit", pDrvIns->iInstance);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatReceive, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling packet receive runs.", "/Drivers/UDPTunnel%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 szVal[16];
if (RT_FAILURE(rc))
N_("DrvUDPTunnel: Configuration error: Querying \"sport\" as string failed"));
if (RT_FAILURE(rc))
N_("DrvUDPTunnel: Configuration error: Converting \"sport\" to integer failed"));
if (RT_FAILURE(rc))
N_("DrvUDPTunnel: Configuration error: Querying \"dport\" as string failed"));
if (RT_FAILURE(rc))
N_("DrvUDPTunnel: Configuration error: Converting \"dport\" to integer failed"));
if (RT_FAILURE(rc))
N_("DrvUDPTunnel: Configuration error: Querying \"dest\" as string failed"));
LogRel(("UDPTunnel#%d: sport=%d;dest=%s;dport=%d\n", pDrvIns->iInstance, pThis->uSrcPort, pThis->pszDestIP, pThis->uDestPort));
/*
* Set up destination address for UDP.
*/
/*
* Create unique thread name for the UDP receiver.
*/
/*
* Start the UDP receiving thread.
*/
if (RT_FAILURE(rc))
N_("UDPTunnel: Failed to start the UDP tunnel server"));
/*
* Create the transmit lock.
*/
return rc;
}
/**
* Suspend notification.
*
* @param pDrvIns The driver instance.
*/
{
LogFlowFunc(("\n"));
{
}
}
/**
* Resume notification.
*
* @param pDrvIns The driver instance.
*/
{
LogFlowFunc(("\n"));
if (RT_FAILURE(rc))
N_("UDPTunnel: Failed to start the UDP tunnel server"));
}
/**
* UDP tunnel network transport driver registration record.
*/
const PDMDRVREG g_DrvUDPTunnel =
{
/* u32Version */
/* szName */
"UDPTunnel",
/* szRCMod */
"",
/* szR0Mod */
"",
/* pszDescription */
"UDP Tunnel Network Transport Driver",
/* fFlags */
/* fClass. */
/* cMaxInstances */
~0U,
/* cbInstance */
sizeof(DRVUDPTUNNEL),
/* pfnConstruct */
/* pfnDestruct */
/* pfnRelocate */
NULL,
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
NULL,
/* pfnSuspend */
/* pfnResume */
/* pfnAttach */
NULL,
/* pfnDetach */
NULL,
/* pfnPowerOff */
NULL,
/* pfnSoftReset */
NULL,
/* u32EndVersion */
};