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