VirtualBox

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

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

NetworkServices/IntNetSwitch,Devices/Network/DrvIntNet,Devices/Network/SrvIntNetR0: Don't require a dedicated waiting thread to get notified when there is something to receive but make it possible to register a callback which is called and sends an IPC message instead, bugref:10297 [build and scm fixes]

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