VirtualBox

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

Last change on this file since 28623 was 28623, checked in by vboxsync, 15 years ago

SrvIntNetR0,VBoxNetFlt: Main body of the internal locking rewrite.

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

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