VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/testcase/tstIntNetR0.cpp@ 39091

Last change on this file since 39091 was 39091, checked in by vboxsync, 13 years ago

More parameter warning fixes; made PciIch9 check the saved state version.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 26.3 KB
Line 
1/* $Id: tstIntNetR0.cpp 39091 2011-10-24 13:58:22Z vboxsync $ */
2/** @file
3 * Internal networking - Usermode testcase for the kernel mode bits.
4 *
5 * This is a bit hackish as we're mixing context here, however it is
6 * very useful when making changes to the internal networking service.
7 */
8
9/*
10 * Copyright (C) 2006-2010 Oracle Corporation
11 *
12 * This file is part of VirtualBox Open Source Edition (OSE), as
13 * available from http://www.virtualbox.org. This file is free software;
14 * you can redistribute it and/or modify it under the terms of the GNU
15 * General Public License (GPL) as published by the Free Software
16 * Foundation, in version 2 as it comes in the "COPYING" file of the
17 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
18 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
19 */
20
21
22/*******************************************************************************
23* Header Files *
24*******************************************************************************/
25#define IN_INTNET_TESTCASE
26#define IN_INTNET_R3
27#include <VBox/cdefs.h>
28#undef INTNETR0DECL
29#define INTNETR0DECL INTNETR3DECL
30#undef DECLR0CALLBACKMEMBER
31#define DECLR0CALLBACKMEMBER(type, name, args) DECLR3CALLBACKMEMBER(type, name, args)
32#include <VBox/types.h>
33typedef void *MYPSUPDRVSESSION;
34#define PSUPDRVSESSION MYPSUPDRVSESSION
35
36#include <VBox/intnet.h>
37#include <VBox/sup.h>
38#include <VBox/err.h>
39#include <iprt/asm.h>
40#include <iprt/getopt.h>
41#include <iprt/initterm.h>
42#include <iprt/mem.h>
43#include <iprt/mp.h>
44#include <iprt/stream.h>
45#include <iprt/thread.h>
46#include <iprt/time.h>
47#include <iprt/test.h>
48
49
50/*******************************************************************************
51* Structures and Typedefs *
52*******************************************************************************/
53/**
54 * Security objectype.
55 */
56typedef enum SUPDRVOBJTYPE
57{
58 /** The usual invalid object. */
59 SUPDRVOBJTYPE_INVALID = 0,
60 /** Internal network. */
61 SUPDRVOBJTYPE_INTERNAL_NETWORK,
62 /** Internal network interface. */
63 SUPDRVOBJTYPE_INTERNAL_NETWORK_INTERFACE,
64 /** The first invalid object type in this end. */
65 SUPDRVOBJTYPE_END,
66 /** The usual 32-bit type size hack. */
67 SUPDRVOBJTYPE_32_BIT_HACK = 0x7ffffff
68} SUPDRVOBJTYPE;
69
70/**
71 * Object destructor callback.
72 * This is called for reference counted objectes when the count reaches 0.
73 *
74 * @param pvObj The object pointer.
75 * @param pvUser1 The first user argument.
76 * @param pvUser2 The second user argument.
77 */
78typedef DECLCALLBACK(void) FNSUPDRVDESTRUCTOR(void *pvObj, void *pvUser1, void *pvUser2);
79/** Pointer to a FNSUPDRVDESTRUCTOR(). */
80typedef FNSUPDRVDESTRUCTOR *PFNSUPDRVDESTRUCTOR;
81
82
83/**
84 * Dummy
85 */
86typedef struct OBJREF
87{
88 PFNSUPDRVDESTRUCTOR pfnDestructor;
89 void *pvUser1;
90 void *pvUser2;
91 uint32_t volatile cRefs;
92} OBJREF, *POBJREF;
93
94
95/*******************************************************************************
96* Global Variables *
97*******************************************************************************/
98/** The test handle.*/
99static RTTEST g_hTest = NIL_RTTEST;
100/** The size (in bytes) of the large transfer tests. */
101static uint32_t g_cbTransfer = _1M * 384;
102/** Fake session handle. */
103const PSUPDRVSESSION g_pSession = (PSUPDRVSESSION)0xdeadface;
104
105
106INTNETR3DECL(void *) SUPR0ObjRegister(PSUPDRVSESSION pSession, SUPDRVOBJTYPE enmType,
107 PFNSUPDRVDESTRUCTOR pfnDestructor, void *pvUser1, void *pvUser2)
108{
109 RTTEST_CHECK_RET(g_hTest, pSession == g_pSession, NULL);
110 POBJREF pRef = (POBJREF)RTTestGuardedAllocTail(g_hTest, sizeof(OBJREF));
111 if (!pRef)
112 return NULL;
113 pRef->cRefs = 1;
114 pRef->pfnDestructor = pfnDestructor;
115 pRef->pvUser1 = pvUser1;
116 pRef->pvUser2 = pvUser2;
117 NOREF(enmType);
118 return pRef;
119}
120
121INTNETR3DECL(int) SUPR0ObjAddRefEx(void *pvObj, PSUPDRVSESSION pSession, bool fNoBlocking)
122{
123 RTTEST_CHECK_RET(g_hTest, pSession == g_pSession, VERR_INVALID_PARAMETER);
124 POBJREF pRef = (POBJREF)pvObj;
125 ASMAtomicIncU32(&pRef->cRefs);
126 NOREF(fNoBlocking);
127 return VINF_SUCCESS;
128}
129
130INTNETR3DECL(int) SUPR0ObjAddRef(void *pvObj, PSUPDRVSESSION pSession)
131{
132 return SUPR0ObjAddRefEx(pvObj, pSession, false);
133}
134
135INTNETR3DECL(int) SUPR0ObjRelease(void *pvObj, PSUPDRVSESSION pSession)
136{
137 RTTEST_CHECK_RET(g_hTest, pSession == g_pSession, VERR_INVALID_PARAMETER);
138 POBJREF pRef = (POBJREF)pvObj;
139 if (!ASMAtomicDecU32(&pRef->cRefs))
140 {
141 pRef->pfnDestructor(pRef, pRef->pvUser1, pRef->pvUser2);
142 RTTestGuardedFree(g_hTest, pRef);
143 return VINF_OBJECT_DESTROYED;
144 }
145 return VINF_SUCCESS;
146}
147
148INTNETR3DECL(int) SUPR0ObjVerifyAccess(void *pvObj, PSUPDRVSESSION pSession, const char *pszObjName)
149{
150 RTTEST_CHECK_RET(g_hTest, pSession == g_pSession, VERR_INVALID_PARAMETER);
151 NOREF(pvObj); NOREF(pszObjName);
152 return VINF_SUCCESS;
153}
154
155INTNETR3DECL(int) SUPR0MemAlloc(PSUPDRVSESSION pSession, uint32_t cb, PRTR0PTR ppvR0, PRTR3PTR ppvR3)
156{
157 RTTEST_CHECK_RET(g_hTest, pSession == g_pSession, VERR_INVALID_PARAMETER);
158 void *pv = RTTestGuardedAllocTail(g_hTest, cb);
159 if (!pv)
160 return VERR_NO_MEMORY;
161 *ppvR0 = (RTR0PTR)pv;
162 if (ppvR3)
163 *ppvR3 = pv;
164 return VINF_SUCCESS;
165}
166
167INTNETR3DECL(int) SUPR0MemFree(PSUPDRVSESSION pSession, RTHCUINTPTR uPtr)
168{
169 RTTEST_CHECK_RET(g_hTest, pSession == g_pSession, VERR_INVALID_PARAMETER);
170 RTTestGuardedFree(g_hTest, (void *)uPtr);
171 return VINF_SUCCESS;
172}
173
174/* Fake non-existing ring-0 APIs. */
175#define RTThreadIsInInterrupt(hThread) false
176#define RTThreadPreemptIsEnabled(hThread) true
177#define RTMpCpuId() 0
178
179/* No CLI/POPF, please. */
180#define RTSpinlockAcquireNoInts RTSpinlockAcquire
181#define RTSpinlockReleaseNoInts RTSpinlockRelease
182
183
184/* ugly but necessary for making R0 code compilable for R3. */
185#undef LOG_GROUP
186#include "../SrvIntNetR0.cpp"
187
188
189/**
190 * Sends the data @a pvBuf points to.
191 */
192static int tstIntNetSendBuf(PINTNETRINGBUF pRingBuf, INTNETIFHANDLE hIf,
193 PSUPDRVSESSION pSession, void const *pvBuf, size_t cbBuf)
194{
195 INTNETSG Sg;
196 IntNetSgInitTemp(&Sg, (void *)pvBuf, cbBuf);
197 int rc = intnetR0RingWriteFrame(pRingBuf, &Sg, NULL);
198 if (RT_SUCCESS(rc))
199 rc = IntNetR0IfSend(hIf, pSession);
200 return rc;
201}
202
203
204typedef struct MYARGS
205{
206 PINTNETBUF pBuf;
207 INTNETIFHANDLE hIf;
208 RTMAC Mac;
209 uint32_t cbFrame;
210 uint64_t u64Start;
211 uint64_t u64End;
212} MYARGS, *PMYARGS;
213
214
215/**
216 * Frame header used when testing.
217 */
218#pragma pack(1)
219typedef struct MYFRAMEHDR
220{
221 RTMAC SrcMac;
222 RTMAC DstMac;
223 uint32_t iFrame;
224 uint32_t auEos[3];
225} MYFRAMEHDR;
226#pragma pack()
227
228/**
229 * Send thread.
230 * This is constantly sending frames to the other interface.
231 */
232DECLCALLBACK(int) SendThread(RTTHREAD hThreadSelf, void *pvArg)
233{
234 PMYARGS pArgs = (PMYARGS)pvArg;
235 int rc;
236 NOREF(hThreadSelf);
237
238 /*
239 * Send g_cbTransfer of data.
240 */
241 uint8_t abBuf[16384] = {0};
242 MYFRAMEHDR *pHdr = (MYFRAMEHDR *)&abBuf[0];
243 uint32_t iFrame = 0;
244 uint32_t cbSent = 0;
245
246 pHdr->SrcMac = pArgs->Mac;
247 pHdr->DstMac = pArgs->Mac;
248 pHdr->DstMac.au16[2] = (pArgs->Mac.au16[2] + 1) % 2;
249
250 pArgs->u64Start = RTTimeNanoTS();
251 for (; cbSent < g_cbTransfer; iFrame++)
252 {
253 const unsigned cb = pArgs->cbFrame
254 ? pArgs->cbFrame
255 : iFrame % 1519 + sizeof(RTMAC) * 2 + sizeof(unsigned);
256 pHdr->iFrame = iFrame;
257
258 INTNETSG Sg;
259 IntNetSgInitTemp(&Sg, abBuf, cb);
260 RTTEST_CHECK_RC_OK(g_hTest, rc = intnetR0RingWriteFrame(&pArgs->pBuf->Send, &Sg, NULL));
261 if (RT_SUCCESS(rc))
262 RTTEST_CHECK_RC_OK(g_hTest, rc = IntNetR0IfSend(pArgs->hIf, g_pSession));
263 cbSent += cb;
264 }
265
266 /*
267 * Termination frames.
268 */
269 pHdr->iFrame = 0xffffdead;
270 pHdr->auEos[0] = 0xffffdead;
271 pHdr->auEos[1] = 0xffffdead;
272 pHdr->auEos[2] = 0xffffdead;
273 for (unsigned c = 0; c < 20; c++)
274 {
275 RTTEST_CHECK_RC_OK(g_hTest, rc = tstIntNetSendBuf(&pArgs->pBuf->Send, pArgs->hIf, g_pSession,
276 abBuf, sizeof(RTMAC) * 2 + sizeof(unsigned) * 4));
277 RTThreadSleep(1);
278 }
279
280 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
281 "sender thread %.6Rhxs terminating.\n"
282 "iFrame=%u cb=%'u\n",
283 &pArgs->Mac, iFrame, cbSent);
284 return 0;
285}
286
287
288/** Ignore lost frames. It only makes things worse to bitch about it. */
289#define IGNORE_LOST_FRAMES
290
291/**
292 * Receive thread.
293 * This is reading stuff from the network.
294 */
295DECLCALLBACK(int) ReceiveThread(RTTHREAD hThreadSelf, void *pvArg)
296{
297 uint32_t cbReceived = 0;
298 uint32_t cLostFrames = 0;
299 uint32_t iFrame = UINT32_MAX;
300 PMYARGS pArgs = (PMYARGS)pvArg;
301 NOREF(hThreadSelf);
302
303 for (;;)
304 {
305 /*
306 * Read data.
307 */
308 while (IntNetRingHasMoreToRead(&pArgs->pBuf->Recv))
309 {
310 uint8_t abBuf[16384 + 1024];
311 MYFRAMEHDR *pHdr = (MYFRAMEHDR *)&abBuf[0];
312 uint32_t cb = IntNetRingReadAndSkipFrame(&pArgs->pBuf->Recv, abBuf);
313
314 /* check for termination frame. */
315 if ( pHdr->iFrame == 0xffffdead
316 && pHdr->auEos[0] == 0xffffdead
317 && pHdr->auEos[1] == 0xffffdead
318 && pHdr->auEos[2] == 0xffffdead)
319 {
320 pArgs->u64End = RTTimeNanoTS();
321 RTThreadSleep(10);
322 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
323 "receiver thread %.6Rhxs terminating.\n"
324 " iFrame=%u cb=%'u c=%'u %'uKB/s %'ufps cLost=%'u \n",
325 &pArgs->Mac, iFrame, cbReceived, iFrame - cLostFrames,
326 (unsigned)(cbReceived * 1000000000.0 / 1024 / (pArgs->u64End - pArgs->u64Start)),
327 (unsigned)((iFrame - cLostFrames) * 1000000000.0 / (pArgs->u64End - pArgs->u64Start)),
328 cLostFrames);
329 return VINF_SUCCESS;
330 }
331
332 /* validate frame header */
333 if ( pHdr->DstMac.au16[0] != pArgs->Mac.au16[0]
334 || pHdr->DstMac.au16[1] != pArgs->Mac.au16[1]
335 || pHdr->DstMac.au16[2] != pArgs->Mac.au16[2]
336 || pHdr->SrcMac.au16[0] != pArgs->Mac.au16[0]
337 || pHdr->SrcMac.au16[1] != pArgs->Mac.au16[1]
338 || pHdr->SrcMac.au16[2] != (pArgs->Mac.au16[2] + 1) % 2)
339 {
340 RTTestFailed(g_hTest, "receiver thread %.6Rhxs received frame header: %.16Rhxs\n", &pArgs->Mac, abBuf);
341 }
342
343 /* frame stuff and stats. */
344 int32_t off = pHdr->iFrame - (iFrame + 1);
345 if (off)
346 {
347 if (off > 0)
348 {
349#ifndef IGNORE_LOST_FRAMES
350 RTTestFailed(g_hTest, "receiver thread %.6Rhxs: iFrame=%#x *puFrame=%#x off=%d\n",
351 &pArgs->Mac, iFrame, pHdr->iFrame, off);
352#endif
353 cLostFrames += off;
354 }
355 else
356 {
357 cLostFrames++;
358 RTTestFailed(g_hTest, "receiver thread %.6Rhxs: iFrame=%#x *puFrame=%#x off=%d\n",
359 &pArgs->Mac, iFrame, pHdr->iFrame, off);
360 }
361 }
362 iFrame = pHdr->iFrame;
363 cbReceived += cb;
364 }
365
366 /*
367 * Wait for data.
368 */
369 int rc = IntNetR0IfWait(pArgs->hIf, g_pSession, RT_INDEFINITE_WAIT);
370 switch (rc)
371 {
372 case VERR_INTERRUPTED:
373 case VINF_SUCCESS:
374 break;
375 case VERR_SEM_DESTROYED:
376 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
377 "receiver thread %.6Rhxs terminating. iFrame=%u cb=%'u c=%'u cLost=%'u\n",
378 &pArgs->Mac, iFrame, cbReceived, iFrame - cLostFrames, cLostFrames);
379 return VINF_SUCCESS;
380
381 default:
382 RTTestFailed(g_hTest, "receiver thread %.6Rhxs got odd return value %Rrc! iFrame=%u cb=%'u c=%'u cLost=%'u\n",
383 &pArgs->Mac, rc, iFrame, cbReceived, iFrame - cLostFrames, cLostFrames);
384 return rc;
385 }
386
387 }
388}
389
390
391/**
392 * Test state.
393 */
394typedef struct TSTSTATE
395{
396 PINTNETBUF pBuf0;
397 INTNETIFHANDLE hIf0;
398
399 PINTNETBUF pBuf1;
400 INTNETIFHANDLE hIf1;
401} TSTSTATE;
402typedef TSTSTATE *PTSTSTATE;
403
404
405/**
406 * Open two internal network interfaces.
407 *
408 * @returns IPRT status of the first failure.
409 * @param pThis The test instance.
410 */
411static int tstOpenInterfaces(PTSTSTATE pThis, const char *pszNetwork, uint32_t cbSend, uint32_t cbRecv)
412{
413 pThis->hIf0 = INTNET_HANDLE_INVALID;
414 RTTESTI_CHECK_RC_OK_RET(IntNetR0Open(g_pSession, pszNetwork, kIntNetTrunkType_None, "",
415 0/*fFlags*/, cbSend, cbRecv, &pThis->hIf0), rcCheck);
416 RTTESTI_CHECK_RET(pThis->hIf0 != INTNET_HANDLE_INVALID, VERR_INTERNAL_ERROR);
417 RTTESTI_CHECK_RC_RET(IntNetR0IfGetBufferPtrs(pThis->hIf0, g_pSession, &pThis->pBuf0, NULL), VINF_SUCCESS, rcCheck);
418 RTTESTI_CHECK_RET(pThis->pBuf0, VERR_INTERNAL_ERROR);
419
420
421 pThis->hIf1 = INTNET_HANDLE_INVALID;
422 RTTESTI_CHECK_RC_OK_RET(IntNetR0Open(g_pSession, pszNetwork, kIntNetTrunkType_None, "",
423 0/*fFlags*/, cbSend, cbRecv, &pThis->hIf1), rcCheck);
424 RTTESTI_CHECK_RET(pThis->hIf1 != INTNET_HANDLE_INVALID, VERR_INTERNAL_ERROR);
425 RTTESTI_CHECK_RC_RET(IntNetR0IfGetBufferPtrs(pThis->hIf1, g_pSession, &pThis->pBuf1, NULL), VINF_SUCCESS, rcCheck);
426 RTTESTI_CHECK_RET(pThis->pBuf1, VERR_INTERNAL_ERROR);
427
428 return VINF_SUCCESS;
429}
430
431/**
432 * Close the interfaces.
433 *
434 * @param pThis The test instance.
435 */
436static void tstCloseInterfaces(PTSTSTATE pThis)
437{
438 int rc;
439 RTTESTI_CHECK_RC_OK(rc = IntNetR0IfClose(pThis->hIf0, g_pSession));
440 if (RT_SUCCESS(rc))
441 {
442 pThis->hIf0 = INTNET_HANDLE_INVALID;
443 pThis->pBuf0 = NULL;
444 }
445
446 RTTESTI_CHECK_RC_OK(rc = IntNetR0IfClose(pThis->hIf1, g_pSession));
447 if (RT_SUCCESS(rc))
448 {
449 pThis->hIf1 = INTNET_HANDLE_INVALID;
450 pThis->pBuf1 = NULL;
451 }
452
453 /* The network should be dead now. */
454 RTTESTI_CHECK(IntNetR0GetNetworkCount() == 0);
455}
456
457/**
458 * Do the bi-directional transfer test.
459 */
460static void tstBidirectionalTransfer(PTSTSTATE pThis, uint32_t cbFrame)
461{
462 MYARGS Args0;
463 RT_ZERO(Args0);
464 Args0.hIf = pThis->hIf0;
465 Args0.pBuf = pThis->pBuf0;
466 Args0.Mac.au16[0] = 0x8086;
467 Args0.Mac.au16[1] = 0;
468 Args0.Mac.au16[2] = 0;
469 Args0.cbFrame = cbFrame;
470
471 MYARGS Args1;
472 RT_ZERO(Args1);
473 Args1.hIf = pThis->hIf1;
474 Args1.pBuf = pThis->pBuf1;
475 Args1.Mac.au16[0] = 0x8086;
476 Args1.Mac.au16[1] = 0;
477 Args1.Mac.au16[2] = 1;
478 Args1.cbFrame = cbFrame;
479
480 RTTHREAD ThreadRecv0 = NIL_RTTHREAD;
481 RTTHREAD ThreadRecv1 = NIL_RTTHREAD;
482 RTTHREAD ThreadSend0 = NIL_RTTHREAD;
483 RTTHREAD ThreadSend1 = NIL_RTTHREAD;
484 RTTESTI_CHECK_RC_OK_RETV(RTThreadCreate(&ThreadRecv0, ReceiveThread, &Args0, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "RECV0"));
485 RTTESTI_CHECK_RC_OK_RETV(RTThreadCreate(&ThreadRecv1, ReceiveThread, &Args1, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "RECV1"));
486 RTTESTI_CHECK_RC_OK_RETV(RTThreadCreate(&ThreadSend0, SendThread, &Args0, 0, RTTHREADTYPE_EMULATION, RTTHREADFLAGS_WAITABLE, "SEND0"));
487 RTTESTI_CHECK_RC_OK_RETV(RTThreadCreate(&ThreadSend1, SendThread, &Args1, 0, RTTHREADTYPE_EMULATION, RTTHREADFLAGS_WAITABLE, "SEND1"));
488
489 int rc2 = VINF_SUCCESS;
490 int rc;
491 RTTESTI_CHECK_RC_OK(rc = RTThreadWait(ThreadSend0, 5*60*1000, &rc2));
492 if (RT_SUCCESS(rc))
493 {
494 RTTESTI_CHECK_RC_OK(rc2);
495 ThreadSend0 = NIL_RTTHREAD;
496 RTTESTI_CHECK_RC_OK(rc = RTThreadWait(ThreadSend1, 5*60*1000, RT_SUCCESS(rc2) ? &rc2 : NULL));
497 if (RT_SUCCESS(rc))
498 {
499 ThreadSend1 = NIL_RTTHREAD;
500 RTTESTI_CHECK_RC_OK(rc2);
501 }
502 }
503 if (RTTestErrorCount(g_hTest) == 0)
504 {
505 /*
506 * Wait a bit for the receivers to finish up.
507 */
508 unsigned cYields = 100000;
509 while ( ( IntNetRingHasMoreToRead(&pThis->pBuf0->Recv)
510 || IntNetRingHasMoreToRead(&pThis->pBuf1->Recv))
511 && cYields-- > 0)
512 RTThreadYield();
513
514 uint64_t u64Elapsed = RT_MAX(Args0.u64End, Args1.u64End) - RT_MIN(Args0.u64Start, Args1.u64Start);
515 uint64_t u64Speed = (uint64_t)((2 * g_cbTransfer / 1024) / (u64Elapsed / 1000000000.0));
516 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
517 "transferred %u bytes in %'RU64 ns (%'RU64 KB/s)\n",
518 2 * g_cbTransfer, u64Elapsed, u64Speed);
519
520 /*
521 * Wait for the threads to finish up...
522 */
523 RTTESTI_CHECK_RC_OK(rc = RTThreadWait(ThreadRecv0, 5000, &rc2));
524 if (RT_SUCCESS(rc))
525 {
526 RTTESTI_CHECK_RC_OK(rc2);
527 ThreadRecv0 = NIL_RTTHREAD;
528 }
529
530 RTTESTI_CHECK_RC_OK(rc = RTThreadWait(ThreadRecv1, 5000, &rc2));
531 if (RT_SUCCESS(rc))
532 {
533 RTTESTI_CHECK_RC_OK(rc2);
534 ThreadRecv1 = NIL_RTTHREAD;
535 }
536 }
537
538 /*
539 * Give them a chance to complete...
540 */
541 RTThreadWait(ThreadRecv0, 5000, NULL);
542 RTThreadWait(ThreadRecv1, 5000, NULL);
543 RTThreadWait(ThreadSend0, 5000, NULL);
544 RTThreadWait(ThreadSend1, 5000, NULL);
545
546
547 /*
548 * Display statistics.
549 */
550 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
551 "Buf0: Yields-OK=%llu Yields-NOK=%llu Lost=%llu Bad=%llu\n",
552 pThis->pBuf0->cStatYieldsOk.c,
553 pThis->pBuf0->cStatYieldsNok.c,
554 pThis->pBuf0->cStatLost.c,
555 pThis->pBuf0->cStatBadFrames.c);
556 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
557 "Buf0.Recv: Frames=%llu Bytes=%llu Overflows=%llu\n",
558 pThis->pBuf0->Recv.cStatFrames,
559 pThis->pBuf0->Recv.cbStatWritten.c,
560 pThis->pBuf0->Recv.cOverflows.c);
561 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
562 "Buf0.Send: Frames=%llu Bytes=%llu Overflows=%llu\n",
563 pThis->pBuf0->Send.cStatFrames,
564 pThis->pBuf0->Send.cbStatWritten.c,
565 pThis->pBuf0->Send.cOverflows.c);
566
567 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
568 "Buf1: Yields-OK=%llu Yields-NOK=%llu Lost=%llu Bad=%llu\n",
569 pThis->pBuf1->cStatYieldsOk.c,
570 pThis->pBuf1->cStatYieldsNok.c,
571 pThis->pBuf1->cStatLost.c,
572 pThis->pBuf1->cStatBadFrames.c);
573 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
574 "Buf1.Recv: Frames=%llu Bytes=%llu Overflows=%llu\n",
575 pThis->pBuf1->Recv.cStatFrames,
576 pThis->pBuf1->Recv.cbStatWritten.c,
577 pThis->pBuf1->Recv.cOverflows.c);
578 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
579 "Buf1.Send: Frames=%llu Bytes=%llu Overflows=%llu\n",
580 pThis->pBuf1->Send.cStatFrames,
581 pThis->pBuf1->Send.cbStatWritten.c,
582 pThis->pBuf1->Send.cOverflows.c);
583
584}
585
586/**
587 * Performs a simple broadcast test.
588 *
589 * @param pThis The test instance.
590 * @param fHeadGuard Whether to use a head or tail guard.
591 */
592static void doBroadcastTest(PTSTSTATE pThis, bool fHeadGuard)
593{
594 static uint16_t const s_au16Frame[7] = { /* dst:*/ 0xffff, 0xffff, 0xffff, /*src:*/0x8086, 0, 0, 0x0800 };
595
596 RTTESTI_CHECK_RC_RETV(tstIntNetSendBuf(&pThis->pBuf0->Send, pThis->hIf0,
597 g_pSession, &s_au16Frame, sizeof(s_au16Frame)),
598 VINF_SUCCESS);
599
600 /* No echo, please */
601 RTTESTI_CHECK_RC_RETV(IntNetR0IfWait(pThis->hIf0, g_pSession, 1), VERR_TIMEOUT);
602
603 /* The other interface should see it though. But Wait should only return once, thank you. */
604 RTTESTI_CHECK_RC_RETV(IntNetR0IfWait(pThis->hIf1, g_pSession, 1), VINF_SUCCESS);
605 RTTESTI_CHECK_RC_RETV(IntNetR0IfWait(pThis->hIf1, g_pSession, 0), VERR_TIMEOUT);
606
607 /* Receive the data. */
608 const unsigned cbExpect = RT_ALIGN(sizeof(s_au16Frame) + sizeof(INTNETHDR), sizeof(INTNETHDR));
609 RTTESTI_CHECK_MSG(IntNetRingGetReadable(&pThis->pBuf1->Recv) == cbExpect,
610 ("%#x vs. %#x\n", IntNetRingGetReadable(&pThis->pBuf1->Recv), cbExpect));
611
612 void *pvBuf;
613 RTTESTI_CHECK_RC_OK_RETV(RTTestGuardedAlloc(g_hTest, sizeof(s_au16Frame), 1, fHeadGuard, &pvBuf));
614 uint32_t cb;
615 RTTESTI_CHECK_MSG_RETV((cb = IntNetRingReadAndSkipFrame(&pThis->pBuf1->Recv, pvBuf)) == sizeof(s_au16Frame),
616 ("%#x vs. %#x\n", cb, sizeof(s_au16Frame)));
617
618 if (memcmp(pvBuf, &s_au16Frame, sizeof(s_au16Frame)))
619 RTTestIFailed("Got invalid data!\n"
620 "received: %.*Rhxs\n"
621 "expected: %.*Rhxs\n",
622 cb, pvBuf, sizeof(s_au16Frame), &s_au16Frame);
623}
624
625/**
626 * Performs a simple unicast test.
627 *
628 * @param pThis The test instance.
629 * @param fHeadGuard Whether to use a head or tail guard.
630 */
631static void doUnicastTest(PTSTSTATE pThis, bool fHeadGuard)
632{
633 static uint16_t const s_au16Frame[7] = { /* dst:*/ 0x8086, 0, 0, /*src:*/0x8086, 0, 1, 0x0800 };
634
635 RTTESTI_CHECK_RC_RETV(tstIntNetSendBuf(&pThis->pBuf1->Send, pThis->hIf1,
636 g_pSession, s_au16Frame, sizeof(s_au16Frame)),
637 VINF_SUCCESS);
638
639 /* No echo, please */
640 RTTESTI_CHECK_RC_RETV(IntNetR0IfWait(pThis->hIf1, g_pSession, 1), VERR_TIMEOUT);
641
642 /* The other interface should see it though. But Wait should only return once, thank you. */
643 RTTESTI_CHECK_RC_RETV(IntNetR0IfWait(pThis->hIf0, g_pSession, 1), VINF_SUCCESS);
644 RTTESTI_CHECK_RC_RETV(IntNetR0IfWait(pThis->hIf0, g_pSession, 0), VERR_TIMEOUT);
645
646 /* Receive the data. */
647 const unsigned cbExpect = RT_ALIGN(sizeof(s_au16Frame) + sizeof(INTNETHDR), sizeof(INTNETHDR));
648 RTTESTI_CHECK_MSG(IntNetRingGetReadable(&pThis->pBuf0->Recv) == cbExpect,
649 ("%#x vs. %#x\n", IntNetRingGetReadable(&pThis->pBuf0->Recv), cbExpect));
650
651 void *pvBuf;
652 RTTESTI_CHECK_RC_OK_RETV(RTTestGuardedAlloc(g_hTest, sizeof(s_au16Frame), 1, fHeadGuard, &pvBuf));
653 uint32_t cb;
654 RTTESTI_CHECK_MSG_RETV((cb = IntNetRingReadAndSkipFrame(&pThis->pBuf0->Recv, pvBuf)) == sizeof(s_au16Frame),
655 ("%#x vs. %#x\n", cb, sizeof(s_au16Frame)));
656
657 if (memcmp(pvBuf, &s_au16Frame, sizeof(s_au16Frame)))
658 RTTestIFailed("Got invalid data!\n"
659 "received: %.*Rhxs\n"
660 "expected: %.*Rhxs\n",
661 cb, pvBuf, sizeof(s_au16Frame), s_au16Frame);
662}
663
664static void doTest(PTSTSTATE pThis, uint32_t cbRecv, uint32_t cbSend)
665{
666
667 /*
668 * Create an INTNET instance.
669 */
670 RTTestISub("IntNetR0Init");
671 RTTESTI_CHECK_RC_RETV(IntNetR0Init(), VINF_SUCCESS);
672
673 /*
674 * Create two interfaces and activate them.
675 */
676 RTTestISub("Network creation");
677 int rc = tstOpenInterfaces(pThis, "test", cbSend, cbRecv);
678 if (RT_FAILURE(rc))
679 return;
680 RTTESTI_CHECK_RC(IntNetR0IfSetActive(pThis->hIf0, g_pSession, true), VINF_SUCCESS);
681 RTTESTI_CHECK_RC(IntNetR0IfSetActive(pThis->hIf1, g_pSession, true), VINF_SUCCESS);
682
683 /*
684 * Test basic waiting.
685 */
686 RTTestISub("IntNetR0IfWait");
687 RTTESTI_CHECK_RC(IntNetR0IfWait(pThis->hIf0, g_pSession, 1), VERR_TIMEOUT);
688 RTTESTI_CHECK_RC(IntNetR0IfWait(pThis->hIf0, g_pSession, 0), VERR_TIMEOUT);
689 RTTESTI_CHECK_RC(IntNetR0IfWait(pThis->hIf1, g_pSession, 1), VERR_TIMEOUT);
690 RTTESTI_CHECK_RC(IntNetR0IfWait(pThis->hIf1, g_pSession, 0), VERR_TIMEOUT);
691
692 /*
693 * Broadcast send and receive.
694 * (This establishes the MAC address of the 1st interface.)
695 */
696 RTTestISub("Broadcast");
697 doBroadcastTest(pThis, false /*fHeadGuard*/);
698 doBroadcastTest(pThis, true /*fHeadGuard*/);
699
700 /*
701 * Unicast send and receive.
702 * (This establishes the MAC address of the 2nd interface.)
703 */
704 RTTestISub("Unicast");
705 doUnicastTest(pThis, false /*fHeadGuard*/);
706 doUnicastTest(pThis, true /*fHeadGuard*/);
707
708 /*
709 * Do the big bi-directional transfer test if the basics worked out.
710 */
711 if (!RTTestIErrorCount())
712 {
713 RTTestISubF("bi-directional benchmark, cbSend=%u, cbRecv=%u, cbTransfer=%u",
714 pThis->pBuf0->cbSend, pThis->pBuf0->cbRecv, g_cbTransfer);
715 tstBidirectionalTransfer(pThis, 256);
716
717 for (uint32_t cbFrame = 64; cbFrame < cbSend - 64; cbFrame += 8)
718 {
719 RTTestISubF("bi-directional benchmark, cbSend=%u, cbRecv=%u, cbTransfer=%u, cbFrame=%u",
720 pThis->pBuf0->cbSend, pThis->pBuf0->cbRecv, g_cbTransfer, cbFrame);
721 tstBidirectionalTransfer(pThis, cbFrame);
722 }
723 }
724
725 /*
726 * Destroy the service.
727 */
728 tstCloseInterfaces(pThis);
729 IntNetR0Term();
730}
731
732
733int main(int argc, char **argv)
734{
735 int rc = RTTestInitAndCreate("tstIntNetR0", &g_hTest);
736 if (rc)
737 return rc;
738
739 /*
740 * Parse the arguments.
741 */
742 static RTGETOPTDEF const s_aOptions[] =
743 {
744 { "--recv-buffer", 'r', RTGETOPT_REQ_UINT32 },
745 { "--send-buffer", 's', RTGETOPT_REQ_UINT32 },
746 { "--transfer-size", 'l', RTGETOPT_REQ_UINT32 },
747 };
748
749 uint32_t cbSend = 1536*2 + 4;
750 uint32_t cbRecv = 0x8000;
751
752 int ch;
753 RTGETOPTUNION Value;
754 RTGETOPTSTATE GetState;
755 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
756 while ((ch = RTGetOpt(&GetState, &Value)))
757 switch (ch)
758 {
759 case 'l':
760 g_cbTransfer = Value.u32;
761 break;
762
763 case 'r':
764 cbRecv = Value.u32;
765 break;
766
767 case 's':
768 cbSend = Value.u32;
769 break;
770
771 default:
772 return RTGetOptPrintError(ch, &Value);
773 }
774
775 /*
776 * Do the testing and report summary.
777 */
778 TSTSTATE This;
779 RT_ZERO(This);
780 doTest(&This, cbRecv, cbSend);
781
782 return RTTestSummaryAndDestroy(g_hTest);
783}
784
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette