tstIntNetR0.cpp revision f7f5cd7b1e530eb5636da51c974b48ae0c1775b3
/* $Id$ */
/** @file
* Internal networking - Usermode testcase for the kernel mode bits.
*
* This is a bit hackish as we're mixing context here, however it is
* very useful when making changes to the internal networking service.
*/
/*
* 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 IN_INTNET_TESTCASE
#define IN_INTNET_R3
#define INTNETR0DECL INTNETR3DECL
typedef void *MYPSUPDRVSESSION;
#define PSUPDRVSESSION MYPSUPDRVSESSION
#include <iprt/initterm.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Security objectype.
*/
typedef enum SUPDRVOBJTYPE
{
/** The usual invalid object. */
/** Internal network. */
/** Internal network interface. */
/** The first invalid object type in this end. */
/** The usual 32-bit type size hack. */
SUPDRVOBJTYPE_32_BIT_HACK = 0x7ffffff
/**
* Object destructor callback.
* This is called for reference counted objectes when the count reaches 0.
*
* @param pvObj The object pointer.
* @param pvUser1 The first user argument.
* @param pvUser2 The second user argument.
*/
/** Pointer to a FNSUPDRVDESTRUCTOR(). */
typedef FNSUPDRVDESTRUCTOR *PFNSUPDRVDESTRUCTOR;
/**
* Dummy
*/
typedef struct OBJREF
{
void *pvUser1;
void *pvUser2;
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** The test handle.*/
/** The size (in bytes) of the large transfer tests. */
/** Fake session handle. */
{
if (!pRef)
return NULL;
return pRef;
}
{
return VINF_SUCCESS;
}
{
}
{
{
return VINF_OBJECT_DESTROYED;
}
return VINF_SUCCESS;
}
INTNETR3DECL(int) SUPR0ObjVerifyAccess(void *pvObj, PSUPDRVSESSION pSession, const char *pszObjName)
{
return VINF_SUCCESS;
}
INTNETR3DECL(int) SUPR0MemAlloc(PSUPDRVSESSION pSession, uint32_t cb, PRTR0PTR ppvR0, PRTR3PTR ppvR3)
{
if (!pv)
return VERR_NO_MEMORY;
if (ppvR3)
return VINF_SUCCESS;
}
{
return VINF_SUCCESS;
}
/* Fake non-existing ring-0 APIs. */
#define RTThreadIsInInterrupt(hThread) false
#define RTThreadPreemptIsEnabled(hThread) true
#define RTMpCpuId() 0
/* ugly but necessary for making R0 code compilable for R3. */
#include "../SrvIntNetR0.cpp"
/**
* Sends the data @a pvBuf points to.
*/
{
if (RT_SUCCESS(rc))
return rc;
}
typedef struct MYARGS
{
/**
* Frame header used when testing.
*/
#pragma pack(1)
typedef struct MYFRAMEHDR
{
} MYFRAMEHDR;
#pragma pack()
/**
* Send thread.
* This is constantly sending frames to the other interface.
*/
{
int rc;
/*
* Send g_cbTransfer of data.
*/
{
if (RT_SUCCESS(rc))
}
/*
* Termination frames.
*/
for (unsigned c = 0; c < 20; c++)
{
RTThreadSleep(1);
}
"sender thread %.6Rhxs terminating.\n"
"iFrame=%u cb=%'u\n",
return 0;
}
/** Ignore lost frames. It only makes things worse to bitch about it. */
#define IGNORE_LOST_FRAMES
/**
* Receive thread.
* This is reading stuff from the network.
*/
{
uint32_t cbReceived = 0;
uint32_t cLostFrames = 0;
for (;;)
{
/*
* Read data.
*/
{
/* check for termination frame. */
{
RTThreadSleep(10);
"receiver thread %.6Rhxs terminating.\n"
" iFrame=%u cb=%'u c=%'u %'uKB/s %'ufps cLost=%'u \n",
return VINF_SUCCESS;
}
/* validate frame header */
{
RTTestFailed(g_hTest, "receiver thread %.6Rhxs received frame header: %.16Rhxs\n", &pArgs->Mac, abBuf);
}
/* frame stuff and stats. */
if (off)
{
if (off > 0)
{
#ifndef IGNORE_LOST_FRAMES
#endif
cLostFrames += off;
}
else
{
cLostFrames++;
}
}
cbReceived += cb;
}
/*
* Wait for data.
*/
switch (rc)
{
case VERR_INTERRUPTED:
case VINF_SUCCESS:
break;
case VERR_SEM_DESTROYED:
"receiver thread %.6Rhxs terminating. iFrame=%u cb=%'u c=%'u cLost=%'u\n",
return VINF_SUCCESS;
default:
RTTestFailed(g_hTest, "receiver thread %.6Rhxs got odd return value %Rrc! iFrame=%u cb=%'u c=%'u cLost=%'u\n",
return rc;
}
}
}
/**
* Test state.
*/
typedef struct TSTSTATE
{
} TSTSTATE;
/**
* Open two internal network interfaces.
*
* @returns IPRT status of the first failure.
* @param pThis The test instance.
*/
static int tstOpenInterfaces(PTSTSTATE pThis, const char *pszNetwork, uint32_t cbSend, uint32_t cbRecv)
{
RTTESTI_CHECK_RC_RET(IntNetR0IfGetBufferPtrs(pThis->hIf0, g_pSession, &pThis->pBuf0, NULL), VINF_SUCCESS, rcCheck);
RTTESTI_CHECK_RC_RET(IntNetR0IfGetBufferPtrs(pThis->hIf1, g_pSession, &pThis->pBuf1, NULL), VINF_SUCCESS, rcCheck);
return VINF_SUCCESS;
}
/**
* Close the interfaces.
*
* @param pThis The test instance.
*/
{
int rc;
if (RT_SUCCESS(rc))
{
}
if (RT_SUCCESS(rc))
{
}
/* The network should be dead now. */
RTTESTI_CHECK(IntNetR0GetNetworkCount() == 0);
}
/**
* Do the bi-directional transfer test.
*/
{
RTTESTI_CHECK_RC_OK_RETV(RTThreadCreate(&ThreadRecv0, ReceiveThread, &Args0, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "RECV0"));
RTTESTI_CHECK_RC_OK_RETV(RTThreadCreate(&ThreadRecv1, ReceiveThread, &Args1, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "RECV1"));
RTTESTI_CHECK_RC_OK_RETV(RTThreadCreate(&ThreadSend0, SendThread, &Args0, 0, RTTHREADTYPE_EMULATION, RTTHREADFLAGS_WAITABLE, "SEND0"));
RTTESTI_CHECK_RC_OK_RETV(RTThreadCreate(&ThreadSend1, SendThread, &Args1, 0, RTTHREADTYPE_EMULATION, RTTHREADFLAGS_WAITABLE, "SEND1"));
int rc2 = VINF_SUCCESS;
int rc;
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
}
}
if (RTTestErrorCount(g_hTest) == 0)
{
/*
* Wait a bit for the receivers to finish up.
*/
unsigned cYields = 100000;
&& cYields-- > 0)
"transferred %u bytes in %'RU64 ns (%'RU64 KB/s)\n",
/*
* Wait for the threads to finish up...
*/
if (RT_SUCCESS(rc))
{
}
if (RT_SUCCESS(rc))
{
}
}
/*
* Give them a chance to complete...
*/
/*
* Display statistics.
*/
"Buf0: Yields-OK=%llu Yields-NOK=%llu Lost=%llu Bad=%llu\n",
"Buf0.Recv: Frames=%llu Bytes=%llu Overflows=%llu\n",
"Buf0.Send: Frames=%llu Bytes=%llu Overflows=%llu\n",
"Buf1: Yields-OK=%llu Yields-NOK=%llu Lost=%llu Bad=%llu\n",
"Buf1.Recv: Frames=%llu Bytes=%llu Overflows=%llu\n",
"Buf1.Send: Frames=%llu Bytes=%llu Overflows=%llu\n",
}
/**
* Performs a simple broadcast test.
*
* @param pThis The test instance.
* @param fHeadGuard Whether to use a head or tail guard.
*/
{
static uint16_t const s_au16Frame[7] = { /* dst:*/ 0xffff, 0xffff, 0xffff, /*src:*/0x8086, 0, 0, 0x0800 };
/* No echo, please */
/* The other interface should see it though. But Wait should only return once, thank you. */
/* Receive the data. */
void *pvBuf;
RTTESTI_CHECK_MSG_RETV((cb = IntNetRingReadAndSkipFrame(&pThis->pBuf1->Recv, pvBuf)) == sizeof(s_au16Frame),
RTTestIFailed("Got invalid data!\n"
"received: %.*Rhxs\n"
"expected: %.*Rhxs\n",
}
/**
* Performs a simple unicast test.
*
* @param pThis The test instance.
* @param fHeadGuard Whether to use a head or tail guard.
*/
{
/* No echo, please */
/* The other interface should see it though. But Wait should only return once, thank you. */
/* Receive the data. */
void *pvBuf;
RTTESTI_CHECK_MSG_RETV((cb = IntNetRingReadAndSkipFrame(&pThis->pBuf0->Recv, pvBuf)) == sizeof(s_au16Frame),
RTTestIFailed("Got invalid data!\n"
"received: %.*Rhxs\n"
"expected: %.*Rhxs\n",
}
{
/*
* Create an INTNET instance.
*/
RTTestISub("IntNetR0Init");
/*
* Create two interfaces and activate them.
*/
RTTestISub("Network creation");
if (RT_FAILURE(rc))
return;
/*
* Test basic waiting.
*/
RTTestISub("IntNetR0IfWait");
/*
* Broadcast send and receive.
* (This establishes the MAC address of the 1st interface.)
*/
RTTestISub("Broadcast");
/*
* Unicast send and receive.
* (This establishes the MAC address of the 2nd interface.)
*/
RTTestISub("Unicast");
/*
* Do the big bi-directional transfer test if the basics worked out.
*/
if (!RTTestIErrorCount())
{
RTTestISubF("bi-directional benchmark, cbSend=%u, cbRecv=%u, cbTransfer=%u",
{
RTTestISubF("bi-directional benchmark, cbSend=%u, cbRecv=%u, cbTransfer=%u, cbFrame=%u",
}
}
/*
* Destroy the service.
*/
IntNetR0Term();
}
{
if (rc)
return rc;
/*
* Parse the arguments.
*/
static RTGETOPTDEF const s_aOptions[] =
{
};
int ch;
RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
switch (ch)
{
case 'l':
break;
case 'r':
break;
case 's':
break;
default:
}
/*
* Do the testing and report summary.
*/
return RTTestSummaryAndDestroy(g_hTest);
}