VirtualBox

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

Last change on this file since 82254 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 26.8 KB
Line 
1/* $Id: tstIntNetR0.cpp 76553 2019-01-01 01:45:53Z 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-2019 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)(uintptr_t)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#include <iprt/spinlock.h>
181#undef RTSPINLOCK_FLAGS_INTERRUPT_SAFE
182#define RTSPINLOCK_FLAGS_INTERRUPT_SAFE RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE
183
184
185/* ugly but necessary for making R0 code compilable for R3. */
186#undef LOG_GROUP
187#include "../SrvIntNetR0.cpp"
188
189
190/**
191 * Sends the data @a pvBuf points to.
192 */
193static int tstIntNetSendBuf(PINTNETRINGBUF pRingBuf, INTNETIFHANDLE hIf,
194 PSUPDRVSESSION pSession, void const *pvBuf, size_t cbBuf)
195{
196 INTNETSG Sg;
197 IntNetSgInitTemp(&Sg, (void *)pvBuf, (uint32_t)cbBuf);
198 int rc = intnetR0RingWriteFrame(pRingBuf, &Sg, NULL);
199 if (RT_SUCCESS(rc))
200 rc = IntNetR0IfSend(hIf, pSession);
201 return rc;
202}
203
204
205typedef struct MYARGS
206{
207 PINTNETBUF pBuf;
208 INTNETIFHANDLE hIf;
209 RTMAC Mac;
210 uint32_t cbFrame;
211 uint64_t u64Start;
212 uint64_t u64End;
213} MYARGS, *PMYARGS;
214
215
216/**
217 * Frame header used when testing.
218 */
219#pragma pack(1)
220typedef struct MYFRAMEHDR
221{
222 RTMAC SrcMac;
223 RTMAC DstMac;
224 uint32_t iFrame;
225 uint32_t auEos[3];
226} MYFRAMEHDR;
227#pragma pack()
228
229/**
230 * Send thread.
231 * This is constantly sending frames to the other interface.
232 */
233static DECLCALLBACK(int) SendThread(RTTHREAD hThreadSelf, void *pvArg)
234{
235 PMYARGS pArgs = (PMYARGS)pvArg;
236 int rc;
237 NOREF(hThreadSelf);
238
239 /*
240 * Send g_cbTransfer of data.
241 */
242 uint8_t abBuf[16384] = {0};
243 MYFRAMEHDR *pHdr = (MYFRAMEHDR *)&abBuf[0];
244 uint32_t iFrame = 0;
245 uint32_t cbSent = 0;
246
247 pHdr->SrcMac = pArgs->Mac;
248 pHdr->DstMac = pArgs->Mac;
249 pHdr->DstMac.au16[2] = (pArgs->Mac.au16[2] + 1) % 2;
250
251 pArgs->u64Start = RTTimeNanoTS();
252 for (; cbSent < g_cbTransfer; iFrame++)
253 {
254 const unsigned cb = pArgs->cbFrame
255 ? pArgs->cbFrame
256 : iFrame % 1519 + sizeof(RTMAC) * 2 + sizeof(unsigned);
257 pHdr->iFrame = iFrame;
258
259 INTNETSG Sg;
260 IntNetSgInitTemp(&Sg, abBuf, cb);
261 RTTEST_CHECK_RC_OK(g_hTest, rc = intnetR0RingWriteFrame(&pArgs->pBuf->Send, &Sg, NULL));
262 if (RT_SUCCESS(rc))
263 RTTEST_CHECK_RC_OK(g_hTest, rc = IntNetR0IfSend(pArgs->hIf, g_pSession));
264 cbSent += cb;
265 }
266
267 /*
268 * Termination frames.
269 */
270 pHdr->iFrame = 0xffffdead;
271 pHdr->auEos[0] = 0xffffdead;
272 pHdr->auEos[1] = 0xffffdead;
273 pHdr->auEos[2] = 0xffffdead;
274 for (unsigned c = 0; c < 20; c++)
275 {
276 RTTEST_CHECK_RC_OK(g_hTest, rc = tstIntNetSendBuf(&pArgs->pBuf->Send, pArgs->hIf, g_pSession,
277 abBuf, sizeof(RTMAC) * 2 + sizeof(unsigned) * 4));
278 RTThreadSleep(1);
279 }
280
281 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
282 "sender thread %.6Rhxs terminating.\n"
283 "iFrame=%u cb=%'u\n",
284 &pArgs->Mac, iFrame, cbSent);
285 return 0;
286}
287
288
289/** Ignore lost frames. It only makes things worse to bitch about it. */
290#define IGNORE_LOST_FRAMES
291
292/**
293 * Receive thread.
294 * This is reading stuff from the network.
295 */
296static DECLCALLBACK(int) ReceiveThread(RTTHREAD hThreadSelf, void *pvArg)
297{
298 uint32_t cbReceived = 0;
299 uint32_t cLostFrames = 0;
300 uint32_t iFrame = UINT32_MAX;
301 PMYARGS pArgs = (PMYARGS)pvArg;
302 NOREF(hThreadSelf);
303
304 for (;;)
305 {
306 /*
307 * Read data.
308 */
309 while (IntNetRingHasMoreToRead(&pArgs->pBuf->Recv))
310 {
311 uint8_t abBuf[16384 + 1024];
312 MYFRAMEHDR *pHdr = (MYFRAMEHDR *)&abBuf[0];
313 uint32_t cb = IntNetRingReadAndSkipFrame(&pArgs->pBuf->Recv, abBuf);
314
315 /* check for termination frame. */
316 if ( pHdr->iFrame == 0xffffdead
317 && pHdr->auEos[0] == 0xffffdead
318 && pHdr->auEos[1] == 0xffffdead
319 && pHdr->auEos[2] == 0xffffdead)
320 {
321 pArgs->u64End = RTTimeNanoTS();
322 RTThreadSleep(10);
323 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
324 "receiver thread %.6Rhxs terminating.\n"
325 " iFrame=%u cb=%'u c=%'u %'uKB/s %'ufps cLost=%'u \n",
326 &pArgs->Mac, iFrame, cbReceived, iFrame - cLostFrames,
327 (unsigned)(cbReceived * 1000000000.0 / 1024 / (pArgs->u64End - pArgs->u64Start)),
328 (unsigned)((iFrame - cLostFrames) * 1000000000.0 / (pArgs->u64End - pArgs->u64Start)),
329 cLostFrames);
330 return VINF_SUCCESS;
331 }
332
333 /* validate frame header */
334 if ( pHdr->DstMac.au16[0] != pArgs->Mac.au16[0]
335 || pHdr->DstMac.au16[1] != pArgs->Mac.au16[1]
336 || pHdr->DstMac.au16[2] != pArgs->Mac.au16[2]
337 || pHdr->SrcMac.au16[0] != pArgs->Mac.au16[0]
338 || pHdr->SrcMac.au16[1] != pArgs->Mac.au16[1]
339 || pHdr->SrcMac.au16[2] != (pArgs->Mac.au16[2] + 1) % 2)
340 {
341 RTTestFailed(g_hTest, "receiver thread %.6Rhxs received frame header: %.16Rhxs\n", &pArgs->Mac, abBuf);
342 }
343
344 /* frame stuff and stats. */
345 int32_t off = pHdr->iFrame - (iFrame + 1);
346 if (off)
347 {
348 if (off > 0)
349 {
350#ifndef IGNORE_LOST_FRAMES
351 RTTestFailed(g_hTest, "receiver thread %.6Rhxs: iFrame=%#x *puFrame=%#x off=%d\n",
352 &pArgs->Mac, iFrame, pHdr->iFrame, off);
353#endif
354 cLostFrames += off;
355 }
356 else
357 {
358 cLostFrames++;
359 RTTestFailed(g_hTest, "receiver thread %.6Rhxs: iFrame=%#x *puFrame=%#x off=%d\n",
360 &pArgs->Mac, iFrame, pHdr->iFrame, off);
361 }
362 }
363 iFrame = pHdr->iFrame;
364 cbReceived += cb;
365 }
366
367 /*
368 * Wait for data.
369 */
370 int rc = IntNetR0IfWait(pArgs->hIf, g_pSession, RT_INDEFINITE_WAIT);
371 switch (rc)
372 {
373 case VERR_INTERRUPTED:
374 case VINF_SUCCESS:
375 break;
376 case VERR_SEM_DESTROYED:
377 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
378 "receiver thread %.6Rhxs terminating. iFrame=%u cb=%'u c=%'u cLost=%'u\n",
379 &pArgs->Mac, iFrame, cbReceived, iFrame - cLostFrames, cLostFrames);
380 return VINF_SUCCESS;
381
382 default:
383 RTTestFailed(g_hTest, "receiver thread %.6Rhxs got odd return value %Rrc! iFrame=%u cb=%'u c=%'u cLost=%'u\n",
384 &pArgs->Mac, rc, iFrame, cbReceived, iFrame - cLostFrames, cLostFrames);
385 return rc;
386 }
387
388 }
389}
390
391
392/**
393 * Test state.
394 */
395typedef struct TSTSTATE
396{
397 PINTNETBUF pBuf0;
398 INTNETIFHANDLE hIf0;
399
400 PINTNETBUF pBuf1;
401 INTNETIFHANDLE hIf1;
402} TSTSTATE;
403typedef TSTSTATE *PTSTSTATE;
404
405
406/**
407 * Open two internal network interfaces.
408 *
409 * @returns IPRT status of the first failure.
410 * @param pThis The test instance.
411 */
412static int tstOpenInterfaces(PTSTSTATE pThis, const char *pszNetwork, uint32_t cbSend, uint32_t cbRecv)
413{
414 pThis->hIf0 = INTNET_HANDLE_INVALID;
415 RTTESTI_CHECK_RC_OK_RET(IntNetR0Open(g_pSession, pszNetwork, kIntNetTrunkType_None, "",
416 0/*fFlags*/, cbSend, cbRecv, &pThis->hIf0), rcCheck);
417 RTTESTI_CHECK_RET(pThis->hIf0 != INTNET_HANDLE_INVALID, VERR_INTERNAL_ERROR);
418 RTTESTI_CHECK_RC_RET(IntNetR0IfGetBufferPtrs(pThis->hIf0, g_pSession, &pThis->pBuf0, NULL), VINF_SUCCESS, rcCheck);
419 RTTESTI_CHECK_RET(pThis->pBuf0, VERR_INTERNAL_ERROR);
420
421
422 pThis->hIf1 = INTNET_HANDLE_INVALID;
423 RTTESTI_CHECK_RC_OK_RET(IntNetR0Open(g_pSession, pszNetwork, kIntNetTrunkType_None, "",
424 0/*fFlags*/, cbSend, cbRecv, &pThis->hIf1), rcCheck);
425 RTTESTI_CHECK_RET(pThis->hIf1 != INTNET_HANDLE_INVALID, VERR_INTERNAL_ERROR);
426 RTTESTI_CHECK_RC_RET(IntNetR0IfGetBufferPtrs(pThis->hIf1, g_pSession, &pThis->pBuf1, NULL), VINF_SUCCESS, rcCheck);
427 RTTESTI_CHECK_RET(pThis->pBuf1, VERR_INTERNAL_ERROR);
428
429 return VINF_SUCCESS;
430}
431
432/**
433 * Close the interfaces.
434 *
435 * @param pThis The test instance.
436 */
437static void tstCloseInterfaces(PTSTSTATE pThis)
438{
439 int rc;
440 RTTESTI_CHECK_RC_OK(rc = IntNetR0IfClose(pThis->hIf0, g_pSession));
441 if (RT_SUCCESS(rc))
442 {
443 pThis->hIf0 = INTNET_HANDLE_INVALID;
444 pThis->pBuf0 = NULL;
445 }
446
447 RTTESTI_CHECK_RC_OK(rc = IntNetR0IfClose(pThis->hIf1, g_pSession));
448 if (RT_SUCCESS(rc))
449 {
450 pThis->hIf1 = INTNET_HANDLE_INVALID;
451 pThis->pBuf1 = NULL;
452 }
453
454 /* The network should be dead now. */
455 RTTESTI_CHECK(IntNetR0GetNetworkCount() == 0);
456}
457
458/**
459 * Do the bi-directional transfer test.
460 */
461static void tstBidirectionalTransfer(PTSTSTATE pThis, uint32_t cbFrame)
462{
463 MYARGS Args0;
464 RT_ZERO(Args0);
465 Args0.hIf = pThis->hIf0;
466 Args0.pBuf = pThis->pBuf0;
467 Args0.Mac.au16[0] = 0x8086;
468 Args0.Mac.au16[1] = 0;
469 Args0.Mac.au16[2] = 0;
470 Args0.cbFrame = cbFrame;
471
472 MYARGS Args1;
473 RT_ZERO(Args1);
474 Args1.hIf = pThis->hIf1;
475 Args1.pBuf = pThis->pBuf1;
476 Args1.Mac.au16[0] = 0x8086;
477 Args1.Mac.au16[1] = 0;
478 Args1.Mac.au16[2] = 1;
479 Args1.cbFrame = cbFrame;
480
481 RTTHREAD ThreadRecv0 = NIL_RTTHREAD;
482 RTTHREAD ThreadRecv1 = NIL_RTTHREAD;
483 RTTHREAD ThreadSend0 = NIL_RTTHREAD;
484 RTTHREAD ThreadSend1 = NIL_RTTHREAD;
485 RTTESTI_CHECK_RC_OK_RETV(RTThreadCreate(&ThreadRecv0, ReceiveThread, &Args0, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "RECV0"));
486 RTTESTI_CHECK_RC_OK_RETV(RTThreadCreate(&ThreadRecv1, ReceiveThread, &Args1, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "RECV1"));
487 RTTESTI_CHECK_RC_OK_RETV(RTThreadCreate(&ThreadSend0, SendThread, &Args0, 0, RTTHREADTYPE_EMULATION, RTTHREADFLAGS_WAITABLE, "SEND0"));
488 RTTESTI_CHECK_RC_OK_RETV(RTThreadCreate(&ThreadSend1, SendThread, &Args1, 0, RTTHREADTYPE_EMULATION, RTTHREADFLAGS_WAITABLE, "SEND1"));
489
490 int rc2 = VINF_SUCCESS;
491 int rc;
492 RTTESTI_CHECK_RC_OK(rc = RTThreadWait(ThreadSend0, 5*60*1000, &rc2));
493 if (RT_SUCCESS(rc))
494 {
495 RTTESTI_CHECK_RC_OK(rc2);
496 ThreadSend0 = NIL_RTTHREAD;
497 RTTESTI_CHECK_RC_OK(rc = RTThreadWait(ThreadSend1, 5*60*1000, RT_SUCCESS(rc2) ? &rc2 : NULL));
498 if (RT_SUCCESS(rc))
499 {
500 ThreadSend1 = NIL_RTTHREAD;
501 RTTESTI_CHECK_RC_OK(rc2);
502 }
503 }
504 if (RTTestErrorCount(g_hTest) == 0)
505 {
506 /*
507 * Wait a bit for the receivers to finish up.
508 */
509 unsigned cYields = 100000;
510 while ( ( IntNetRingHasMoreToRead(&pThis->pBuf0->Recv)
511 || IntNetRingHasMoreToRead(&pThis->pBuf1->Recv))
512 && cYields-- > 0)
513 RTThreadYield();
514
515 uint64_t u64Elapsed = RT_MAX(Args0.u64End, Args1.u64End) - RT_MIN(Args0.u64Start, Args1.u64Start);
516 uint64_t u64Speed = (uint64_t)((2 * g_cbTransfer / 1024) / (u64Elapsed / 1000000000.0));
517 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
518 "transferred %u bytes in %'RU64 ns (%'RU64 KB/s)\n",
519 2 * g_cbTransfer, u64Elapsed, u64Speed);
520
521 /*
522 * Wait for the threads to finish up...
523 */
524 RTTESTI_CHECK_RC_OK(rc = RTThreadWait(ThreadRecv0, 5000, &rc2));
525 if (RT_SUCCESS(rc))
526 {
527 RTTESTI_CHECK_RC_OK(rc2);
528 ThreadRecv0 = NIL_RTTHREAD;
529 }
530
531 RTTESTI_CHECK_RC_OK(rc = RTThreadWait(ThreadRecv1, 5000, &rc2));
532 if (RT_SUCCESS(rc))
533 {
534 RTTESTI_CHECK_RC_OK(rc2);
535 ThreadRecv1 = NIL_RTTHREAD;
536 }
537 }
538
539 /*
540 * Give them a chance to complete...
541 */
542 RTThreadWait(ThreadRecv0, 5000, NULL);
543 RTThreadWait(ThreadRecv1, 5000, NULL);
544 RTThreadWait(ThreadSend0, 5000, NULL);
545 RTThreadWait(ThreadSend1, 5000, NULL);
546
547
548 /*
549 * Display statistics.
550 */
551 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
552 "Buf0: Yields-OK=%llu Yields-NOK=%llu Lost=%llu Bad=%llu\n",
553 pThis->pBuf0->cStatYieldsOk.c,
554 pThis->pBuf0->cStatYieldsNok.c,
555 pThis->pBuf0->cStatLost.c,
556 pThis->pBuf0->cStatBadFrames.c);
557 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
558 "Buf0.Recv: Frames=%llu Bytes=%llu Overflows=%llu\n",
559 pThis->pBuf0->Recv.cStatFrames,
560 pThis->pBuf0->Recv.cbStatWritten.c,
561 pThis->pBuf0->Recv.cOverflows.c);
562 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
563 "Buf0.Send: Frames=%llu Bytes=%llu Overflows=%llu\n",
564 pThis->pBuf0->Send.cStatFrames,
565 pThis->pBuf0->Send.cbStatWritten.c,
566 pThis->pBuf0->Send.cOverflows.c);
567
568 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
569 "Buf1: Yields-OK=%llu Yields-NOK=%llu Lost=%llu Bad=%llu\n",
570 pThis->pBuf1->cStatYieldsOk.c,
571 pThis->pBuf1->cStatYieldsNok.c,
572 pThis->pBuf1->cStatLost.c,
573 pThis->pBuf1->cStatBadFrames.c);
574 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
575 "Buf1.Recv: Frames=%llu Bytes=%llu Overflows=%llu\n",
576 pThis->pBuf1->Recv.cStatFrames,
577 pThis->pBuf1->Recv.cbStatWritten.c,
578 pThis->pBuf1->Recv.cOverflows.c);
579 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS,
580 "Buf1.Send: Frames=%llu Bytes=%llu Overflows=%llu\n",
581 pThis->pBuf1->Send.cStatFrames,
582 pThis->pBuf1->Send.cbStatWritten.c,
583 pThis->pBuf1->Send.cOverflows.c);
584
585}
586
587/**
588 * Performs a simple broadcast test.
589 *
590 * @param pThis The test instance.
591 * @param fHeadGuard Whether to use a head or tail guard.
592 */
593static void doBroadcastTest(PTSTSTATE pThis, bool fHeadGuard)
594{
595 static uint16_t const s_au16Frame[7] = { /* dst:*/ 0xffff, 0xffff, 0xffff, /*src:*/0x8086, 0, 0, 0x0800 };
596
597 RTTESTI_CHECK_RC_RETV(tstIntNetSendBuf(&pThis->pBuf0->Send, pThis->hIf0,
598 g_pSession, &s_au16Frame, sizeof(s_au16Frame)),
599 VINF_SUCCESS);
600
601 /* No echo, please */
602 RTTESTI_CHECK_RC_RETV(IntNetR0IfWait(pThis->hIf0, g_pSession, 1), VERR_TIMEOUT);
603
604 /* The other interface should see it though. But Wait should only return once, thank you. */
605 RTTESTI_CHECK_RC_RETV(IntNetR0IfWait(pThis->hIf1, g_pSession, 1), VINF_SUCCESS);
606 RTTESTI_CHECK_RC_RETV(IntNetR0IfWait(pThis->hIf1, g_pSession, 0), VERR_TIMEOUT);
607
608 /* Receive the data. */
609 const unsigned cbExpect = RT_ALIGN(sizeof(s_au16Frame) + sizeof(INTNETHDR), sizeof(INTNETHDR));
610 RTTESTI_CHECK_MSG(IntNetRingGetReadable(&pThis->pBuf1->Recv) == cbExpect,
611 ("%#x vs. %#x\n", IntNetRingGetReadable(&pThis->pBuf1->Recv), cbExpect));
612
613 void *pvBuf;
614 RTTESTI_CHECK_RC_OK_RETV(RTTestGuardedAlloc(g_hTest, sizeof(s_au16Frame), 1, fHeadGuard, &pvBuf));
615 uint32_t cb;
616 RTTESTI_CHECK_MSG_RETV((cb = IntNetRingReadAndSkipFrame(&pThis->pBuf1->Recv, pvBuf)) == sizeof(s_au16Frame),
617 ("%#x vs. %#x\n", cb, sizeof(s_au16Frame)));
618
619 if (memcmp(pvBuf, &s_au16Frame, sizeof(s_au16Frame)))
620 RTTestIFailed("Got invalid data!\n"
621 "received: %.*Rhxs\n"
622 "expected: %.*Rhxs\n",
623 cb, pvBuf, sizeof(s_au16Frame), &s_au16Frame);
624}
625
626/**
627 * Performs a simple unicast test.
628 *
629 * @param pThis The test instance.
630 * @param fHeadGuard Whether to use a head or tail guard.
631 */
632static void doUnicastTest(PTSTSTATE pThis, bool fHeadGuard)
633{
634 static uint16_t const s_au16Frame[7] = { /* dst:*/ 0x8086, 0, 0, /*src:*/0x8086, 0, 1, 0x0800 };
635
636 RTTESTI_CHECK_RC_RETV(tstIntNetSendBuf(&pThis->pBuf1->Send, pThis->hIf1,
637 g_pSession, s_au16Frame, sizeof(s_au16Frame)),
638 VINF_SUCCESS);
639
640 /* No echo, please */
641 RTTESTI_CHECK_RC_RETV(IntNetR0IfWait(pThis->hIf1, g_pSession, 1), VERR_TIMEOUT);
642
643 /* The other interface should see it though. But Wait should only return once, thank you. */
644 RTTESTI_CHECK_RC_RETV(IntNetR0IfWait(pThis->hIf0, g_pSession, 1), VINF_SUCCESS);
645 RTTESTI_CHECK_RC_RETV(IntNetR0IfWait(pThis->hIf0, g_pSession, 0), VERR_TIMEOUT);
646
647 /* Receive the data. */
648 const unsigned cbExpect = RT_ALIGN(sizeof(s_au16Frame) + sizeof(INTNETHDR), sizeof(INTNETHDR));
649 RTTESTI_CHECK_MSG(IntNetRingGetReadable(&pThis->pBuf0->Recv) == cbExpect,
650 ("%#x vs. %#x\n", IntNetRingGetReadable(&pThis->pBuf0->Recv), cbExpect));
651
652 void *pvBuf;
653 RTTESTI_CHECK_RC_OK_RETV(RTTestGuardedAlloc(g_hTest, sizeof(s_au16Frame), 1, fHeadGuard, &pvBuf));
654 uint32_t cb;
655 RTTESTI_CHECK_MSG_RETV((cb = IntNetRingReadAndSkipFrame(&pThis->pBuf0->Recv, pvBuf)) == sizeof(s_au16Frame),
656 ("%#x vs. %#x\n", cb, sizeof(s_au16Frame)));
657
658 if (memcmp(pvBuf, &s_au16Frame, sizeof(s_au16Frame)))
659 RTTestIFailed("Got invalid data!\n"
660 "received: %.*Rhxs\n"
661 "expected: %.*Rhxs\n",
662 cb, pvBuf, sizeof(s_au16Frame), s_au16Frame);
663}
664
665static void doTest(PTSTSTATE pThis, uint32_t cbRecv, uint32_t cbSend)
666{
667
668 /*
669 * Create an INTNET instance.
670 */
671 RTTestISub("IntNetR0Init");
672 RTTESTI_CHECK_RC_RETV(IntNetR0Init(), VINF_SUCCESS);
673
674 /*
675 * Create two interfaces and activate them.
676 */
677 RTTestISub("Network creation");
678 int rc = tstOpenInterfaces(pThis, "test", cbSend, cbRecv);
679 if (RT_FAILURE(rc))
680 return;
681 RTTESTI_CHECK_RC(IntNetR0IfSetActive(pThis->hIf0, g_pSession, true), VINF_SUCCESS);
682 RTTESTI_CHECK_RC(IntNetR0IfSetActive(pThis->hIf1, g_pSession, true), VINF_SUCCESS);
683
684 /*
685 * Test basic waiting.
686 */
687 RTTestISub("IntNetR0IfWait");
688 RTTESTI_CHECK_RC(IntNetR0IfWait(pThis->hIf0, g_pSession, 1), VERR_TIMEOUT);
689 RTTESTI_CHECK_RC(IntNetR0IfWait(pThis->hIf0, g_pSession, 0), VERR_TIMEOUT);
690 RTTESTI_CHECK_RC(IntNetR0IfWait(pThis->hIf1, g_pSession, 1), VERR_TIMEOUT);
691 RTTESTI_CHECK_RC(IntNetR0IfWait(pThis->hIf1, g_pSession, 0), VERR_TIMEOUT);
692
693 /*
694 * Broadcast send and receive.
695 * (This establishes the MAC address of the 1st interface.)
696 */
697 RTTestISub("Broadcast");
698 doBroadcastTest(pThis, false /*fHeadGuard*/);
699 doBroadcastTest(pThis, true /*fHeadGuard*/);
700
701 /*
702 * Unicast send and receive.
703 * (This establishes the MAC address of the 2nd interface.)
704 */
705 RTTestISub("Unicast");
706 doUnicastTest(pThis, false /*fHeadGuard*/);
707 doUnicastTest(pThis, true /*fHeadGuard*/);
708
709 /*
710 * Do the big bi-directional transfer test if the basics worked out.
711 */
712 if (!RTTestIErrorCount())
713 {
714 RTTestISubF("bi-directional benchmark, cbSend=%u, cbRecv=%u, cbTransfer=%u",
715 pThis->pBuf0->cbSend, pThis->pBuf0->cbRecv, g_cbTransfer);
716 tstBidirectionalTransfer(pThis, 256);
717
718 for (uint32_t cbFrame = 64; cbFrame < cbSend - 64; cbFrame += 8)
719 {
720 RTTestISubF("bi-directional benchmark, cbSend=%u, cbRecv=%u, cbTransfer=%u, cbFrame=%u",
721 pThis->pBuf0->cbSend, pThis->pBuf0->cbRecv, g_cbTransfer, cbFrame);
722 tstBidirectionalTransfer(pThis, cbFrame);
723 }
724 }
725
726 /*
727 * Destroy the service.
728 */
729 tstCloseInterfaces(pThis);
730 IntNetR0Term();
731}
732
733
734int main(int argc, char **argv)
735{
736 int rc = RTTestInitAndCreate("tstIntNetR0", &g_hTest);
737 if (rc)
738 return rc;
739
740 /*
741 * Parse the arguments.
742 */
743 static RTGETOPTDEF const s_aOptions[] =
744 {
745 { "--recv-buffer", 'r', RTGETOPT_REQ_UINT32 },
746 { "--send-buffer", 's', RTGETOPT_REQ_UINT32 },
747 { "--transfer-size", 'l', RTGETOPT_REQ_UINT32 },
748 };
749
750 uint32_t cbSend = 1536*2 + 4;
751 uint32_t cbRecv = 0x8000;
752
753 int ch;
754 RTGETOPTUNION Value;
755 RTGETOPTSTATE GetState;
756 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
757 while ((ch = RTGetOpt(&GetState, &Value)))
758 switch (ch)
759 {
760 case 'l':
761 g_cbTransfer = Value.u32;
762 break;
763
764 case 'r':
765 cbRecv = Value.u32;
766 break;
767
768 case 's':
769 cbSend = Value.u32;
770 break;
771
772 default:
773 return RTGetOptPrintError(ch, &Value);
774 }
775
776 /*
777 * Do the testing and report summary.
778 */
779 TSTSTATE This;
780 RT_ZERO(This);
781 doTest(&This, cbRecv, cbSend);
782
783 return RTTestSummaryAndDestroy(g_hTest);
784}
785
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