tstIntNetR0.cpp revision 9a94c20d8d26ee0123f5c81c9743b34281632da2
/** @file
*
* VBox - Testcase for the Ring-0 part of internal networking.
*/
/*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* 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.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define IN_INTNET_TESTCASE
#define IN_INTNET_R3
#define INTNETR0DECL INTNETR3DECL
/*******************************************************************************
* 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 error count. */
unsigned g_cErrors = 0;
/** Fake session handle. */
/** Testframe 0 */
struct TESTFRAME
{
INTNETR3DECL(void *) SUPR0ObjRegister(PSUPDRVSESSION pSession, SUPDRVOBJTYPE enmType, PFNSUPDRVDESTRUCTOR pfnDestructor, void *pvUser1, void *pvUser2)
{
if (pSession != g_pSession)
{
g_cErrors++;
return NULL;
}
if (!pRef)
return NULL;
return pRef;
}
{
if (pSession != g_pSession)
{
g_cErrors++;
return VERR_INVALID_PARAMETER;
}
return VINF_SUCCESS;
}
{
if (pSession != g_pSession)
{
g_cErrors++;
return VERR_INVALID_PARAMETER;
}
{
}
return VINF_SUCCESS;
}
INTNETR3DECL(int) SUPR0ObjVerifyAccess(void *pvObj, PSUPDRVSESSION pSession, const char *pszObjName)
{
if (pSession != g_pSession)
{
g_cErrors++;
return VERR_INVALID_PARAMETER;
}
return VINF_SUCCESS;
}
INTNETR3DECL(int) SUPR0MemAlloc(PSUPDRVSESSION pSession, uint32_t cb, PRTR0PTR ppvR0, PRTR3PTR ppvR3)
{
if (pSession != g_pSession)
{
g_cErrors++;
return VERR_INVALID_PARAMETER;
}
if (!pv)
return VERR_NO_MEMORY;
if (ppvR3)
return VINF_SUCCESS;
}
{
if (pSession != g_pSession)
{
g_cErrors++;
return VERR_INVALID_PARAMETER;
}
return VINF_SUCCESS;
}
/* ugly but necessary for making R0 code compilable for R3. */
#include "../SrvIntNetR0.cpp"
typedef struct ARGS
{
/**
* Send thread.
* This is constantly broadcasting frames to the network.
*/
{
/*
* Send 64 MB of data.
*/
unsigned iFrame = 0;
unsigned cbSent = 0;
{
#if 0
#else
if (RT_SUCCESS(rc))
#endif
if (VBOX_FAILURE(rc))
{
g_cErrors++;
RTPrintf("tstIntNetR0: Failed sending %d bytes, rc=%Vrc (%d)\n", cb, rc, INTNETRingGetWritable(&pArgs->pBuf->Send));
}
}
/*
* Termination frames.
*/
puFrame[0] = 0xffffdead;
for (unsigned c = 0; c < 20; c++)
{
int rc = INTNETR0IfSend(pArgs->pIntNet, pArgs->hIf, abBuf, sizeof(PDMMAC) * 2 + sizeof(unsigned) * 4);
if (VBOX_FAILURE(rc))
{
g_cErrors++;
}
RTThreadSleep(1);
}
RTPrintf("tstIntNetR0: sender thread %.6Rhxs terminating. iFrame=%d cbSent=%d\n", &pArgs->Mac, iFrame, cbSent);
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.
*/
{
unsigned cbReceived = 0;
unsigned cLostFrames = 0;
unsigned iFrame = ~0;
for (;;)
{
/*
* Wait for data.
*/
switch (rc)
{
case VERR_INTERRUPTED:
case VINF_SUCCESS:
break;
case VERR_SEM_DESTROYED:
RTPrintf("tstIntNetR0: receiver thread %.6Rhxs terminating. cbReceived=%u cLostFrames=%u iFrame=%u\n",
return VINF_SUCCESS;
default:
RTPrintf("tstIntNetR0: receiver thread %.6Rhxs got odd return value %Vrc! cbReceived=%u cLostFrames=%u iFrame=%u\n",
g_cErrors++;
return rc;
}
/*
* Read data.
*/
{
/* check for termination frame. */
&& puFrame[0] == 0xffffdead
{
RTPrintf("tstIntNetR0: receiver thread %.6Rhxs terminating. cbReceived=%u cLostFrames=%u iFrame=%u\n",
return VINF_SUCCESS;
}
/* validate frame header */
{
RTPrintf("tstIntNetR0: receiver thread %.6Rhxs received frame header: %.16Rhxs\n",
g_cErrors++;
}
/* frame stuff and stats. */
if (off)
{
if (off > 0)
{
RTPrintf("tstIntNetR0: receiver thread %.6Rhxs: iFrame=%d *puFrame=%d off=%d\n",
g_cErrors++;
cLostFrames++;
}
else
{
cLostFrames += -off;
#ifndef IGNORE_LOST_FRAMES
if (off < 50)
{
RTPrintf("tstIntNetR0: receiver thread %.6Rhxs: iFrame=%d *puFrame=%d off=%d\n",
g_cErrors++;
}
#endif
}
}
cbReceived += cb;
}
}
}
int main()
{
/*
* Init runtime and create an INTNET instance.
*/
RTR3Init();
RTPrintf("tstIntNetR0: TESTING...\n");
if (VBOX_FAILURE(rc))
{
RTPrintf("tstIntNetR0: INTNETR0Create failed, rc=%Vrc\n");
return 1;
}
/*
* Create two interfaces.
*/
rc = INTNETR0Open(pIntNet, g_pSession, "test", kIntNetTrunkType_None, "", 0, 1536*2 + 4, 0x8000, &hIf0);
if (VBOX_SUCCESS(rc))
{
if (hIf0 != INTNET_HANDLE_INVALID)
{
rc = INTNETR0Open(pIntNet, g_pSession, "test", kIntNetTrunkType_None, NULL, 0, 1536*2 + 4, 0x8000, &hIf1);
if (VBOX_SUCCESS(rc))
{
if (hIf1 != INTNET_HANDLE_INVALID)
{
{
g_cErrors++;
}
if (VBOX_FAILURE(rc))
{
g_cErrors++;
}
/*
* Test basic waiting.
*/
if (rc != VERR_TIMEOUT)
{
g_cErrors++;
}
if (rc != VERR_TIMEOUT)
{
g_cErrors++;
}
/*
* Send and receive.
*/
if (VBOX_SUCCESS(rc))
{
if (rc != VERR_TIMEOUT)
{
g_cErrors++;
}
if (rc == VINF_SUCCESS)
{
/* receive it */
{
RTPrintf("tstIntNetR0: %d readable bytes, expected %d!\n", INTNETRingGetReadable(&pBuf1->Recv), cbExpect);
g_cErrors++;
}
if (cb != sizeof(g_TestFrame0))
{
g_cErrors++;
}
{
RTPrintf("tstIntNetR0: Got invalid data!\n"
"received: %.*Rhxs\n"
"expected: %.*Rhxs\n",
g_cErrors++;
}
/*
* Send a packet from If1 just to set its MAC address.
*/
if (VBOX_FAILURE(rc))
{
g_cErrors++;
}
/*
* Start threaded testcase.
*/
if (!g_cErrors)
{
rc = RTThreadCreate(&ThreadRecv0, ReceiveThread, &Args0, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "RECV0");
if (VBOX_SUCCESS(rc))
rc = RTThreadCreate(&ThreadRecv1, ReceiveThread, &Args1, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "RECV1");
if (VBOX_SUCCESS(rc))
rc = RTThreadCreate(&ThreadSend0, SendThread, &Args0, 0, RTTHREADTYPE_EMULATION, RTTHREADFLAGS_WAITABLE, "SEND0");
if (VBOX_SUCCESS(rc))
rc = RTThreadCreate(&ThreadSend1, SendThread, &Args1, 0, RTTHREADTYPE_EMULATION, RTTHREADFLAGS_WAITABLE, "SEND1");
if (VBOX_SUCCESS(rc))
{
int rc2 = VINF_SUCCESS;
if ( VBOX_SUCCESS(rc)
&& VBOX_SUCCESS(rc2))
if ( VBOX_SUCCESS(rc)
&& VBOX_SUCCESS(rc2))
{
/*
* Wait a bit for the receivers to finish up.
*/
unsigned cYields = 100000;
&& cYields-- > 0)
RTPrintf("tstIntNetR0: transfered %d bytes in %RU64 ns (%RU64 KB/s)\n",
/*
* Closing time...
*/
if (VBOX_SUCCESS(rc))
{
}
else
{
g_cErrors++;
}
if (VBOX_SUCCESS(rc))
{
}
else
{
g_cErrors++;
}
{
g_cErrors++;
}
{
g_cErrors++;
}
/* check if the network still exist... */
{
g_cErrors++;
}
}
else
{
g_cErrors++;
}
}
else
{
g_cErrors++;
}
}
}
else
{
g_cErrors++;
}
}
else
{
g_cErrors++;
}
}
else
{
RTPrintf("tstIntNetR0: INTNETOpen returned invalid handle on success! (hIf1)\n");
g_cErrors++;
}
}
else
{
g_cErrors++;
}
}
else
{
RTPrintf("tstIntNetR0: INTNETOpen returned invalid handle on success! (hIf0)\n");
g_cErrors++;
}
}
else
{
g_cErrors++;
}
/*
* Destroy the service.
*/
/*
* Summary.
*/
if (!g_cErrors)
RTPrintf("tstIntNetR0: SUCCESS\n");
else
return !!g_cErrors;
}