VirtualBox

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

Last change on this file since 96407 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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