VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/testcase/tstIntNet-1.cpp@ 11470

Last change on this file since 11470 was 11470, checked in by vboxsync, 16 years ago

testcase/tstIntNet-1: fixed swapped dst and src mac while printing incoming packets.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 30.6 KB
Line 
1/* $Id: tstIntNet-1.cpp 11470 2008-08-18 18:13:53Z vboxsync $ */
2/** @file
3 * VBox - Testcase for internal networking, simple NetFlt trunk creation.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22/*******************************************************************************
23* Header Files *
24*******************************************************************************/
25#include <VBox/intnet.h>
26#include <VBox/sup.h>
27#include <VBox/vmm.h>
28#include <VBox/err.h>
29#include <iprt/initterm.h>
30#include <iprt/alloc.h>
31#include <iprt/path.h>
32#include <iprt/stream.h>
33#include <iprt/string.h>
34#include <iprt/thread.h>
35#include <iprt/param.h>
36#include <iprt/getopt.h>
37#include <iprt/rand.h>
38#include <iprt/log.h>
39#include <iprt/crc32.h>
40#include <iprt/net.h>
41
42#include "../Pcap.h"
43
44
45/*******************************************************************************
46* Global Variables *
47*******************************************************************************/
48static int g_cErrors = 0;
49static uint64_t g_StartTS = 0;
50static uint32_t g_DhcpXID = 0;
51static bool g_fDhcpReply = false;
52static uint32_t g_cOtherPkts = 0;
53static uint32_t g_cArpPkts = 0;
54static uint32_t g_cIpv4Pkts = 0;
55static uint32_t g_cUdpPkts = 0;
56static uint32_t g_cDhcpPkts = 0;
57static uint32_t g_cTcpPkts = 0;
58
59
60/**
61 * Error reporting wrapper.
62 *
63 * @param pErrStrm The stream to write the error message to. Can be NULL.
64 * @param pszFormat The message format string.
65 * @param ... Format arguments.
66 */
67static void tstIntNetError(PRTSTREAM pErrStrm, const char *pszFormat, ...)
68{
69 if (!pErrStrm)
70 pErrStrm = g_pStdOut;
71
72 va_list va;
73 va_start(va, pszFormat);
74 RTStrmPrintf(pErrStrm, "tstIntNet-1: ERROR - ");
75 RTStrmPrintfV(pErrStrm, pszFormat, va);
76 va_end(va);
77
78 g_cErrors++;
79}
80
81
82/**
83 * Parses a frame an runs in thru the RTNet validation code so it gets
84 * some exercise.
85 *
86 * @param pvFrame Pointer to the ethernet frame.
87 * @param cbFrame The size of the ethernet frame.
88 * @param pErrStrm The error stream.
89 */
90static void tstIntNetTestFrame(void const *pvFrame, size_t cbFrame, PRTSTREAM pErrStrm)
91{
92 /*
93 * Ethernet header.
94 */
95 PCRTNETETHERHDR pEtherHdr = (PCRTNETETHERHDR)pvFrame;
96 if (cbFrame <= sizeof(*pEtherHdr))
97 return tstIntNetError(pErrStrm, "cbFrame=%#x <= %#x (ether)\n", cbFrame, sizeof(*pEtherHdr));
98 ssize_t cbLeft = cbFrame - sizeof(*pEtherHdr);
99 uint8_t const *pbCur = (uint8_t const *)(pEtherHdr + 1);
100
101 switch (RT_BE2H_U16(pEtherHdr->EtherType))
102 {
103 case RTNET_ETHERTYPE_ARP:
104 {
105 g_cArpPkts++;
106 break;
107 }
108
109 case RTNET_ETHERTYPE_IPV4:
110 {
111 g_cIpv4Pkts++;
112
113 PCRTNETIPV4 pIpHdr = (PCRTNETIPV4)pbCur;
114 if (!RTNetIPv4IsHdrValid(pIpHdr, cbLeft, cbLeft))
115 return tstIntNetError(pErrStrm, "RTNetIPv4IsHdrValid failed\n");
116 pbCur += pIpHdr->ip_hl * 4;
117 cbLeft -= pIpHdr->ip_hl * 4;
118 AssertFatal(cbLeft >= 0);
119
120 switch (pIpHdr->ip_p)
121 {
122 case RTNETIPV4_PROT_ICMP:
123 {
124 /** @todo ICMP? */
125 break;
126 }
127
128 case RTNETIPV4_PROT_UDP:
129 {
130 g_cUdpPkts++;
131 PCRTNETUDP pUdpHdr = (PCRTNETUDP)pbCur;
132 if (!RTNetIPv4IsUDPValid(pIpHdr, pUdpHdr, pUdpHdr + 1, cbLeft))
133 return tstIntNetError(pErrStrm, "RTNetIPv4IsUDPValid failed\n");
134 pbCur += sizeof(*pUdpHdr);
135 cbLeft -= sizeof(*pUdpHdr);
136
137 if (RT_BE2H_U16(pUdpHdr->uh_dport) == RTNETIPV4_PORT_BOOTPS)
138 {
139 g_cDhcpPkts++;
140 PCRTNETBOOTP pDhcp = (PCRTNETBOOTP)pbCur;
141 if (!RTNetIPv4IsDHCPValid(pUdpHdr, pDhcp, cbLeft, NULL))
142 return tstIntNetError(pErrStrm, "RTNetIPv4IsDHCPValid failed\n");
143 }
144 break;
145 }
146
147 case RTNETIPV4_PROT_TCP:
148 {
149 g_cTcpPkts++;
150 PCRTNETTCP pTcpHdr = (PCRTNETTCP)pbCur;
151 if (!RTNetIPv4IsTCPValid(pIpHdr, pTcpHdr, cbLeft, NULL, cbLeft))
152 return tstIntNetError(pErrStrm, "RTNetIPv4IsTCPValid failed\n");
153 break;
154 }
155 }
156 break;
157 }
158
159 //case RTNET_ETHERTYPE_IPV6:
160 default:
161 g_cOtherPkts++;
162 break;
163 }
164}
165
166
167/**
168 * Writes a frame packet to the buffer.
169 *
170 * @returns VBox status code.
171 * @param pBuf The buffer.
172 * @param pRingBuf The ring buffer to read from.
173 * @param pvFrame The frame to write.
174 * @param cbFrame The size of the frame.
175 * @remark This is the same as INTNETRingWriteFrame and drvIntNetRingWriteFrame.
176 */
177static int tstIntNetWriteFrame(PINTNETBUF pBuf, PINTNETRINGBUF pRingBuf, const void *pvFrame, uint32_t cbFrame)
178{
179 /*
180 * Validate input.
181 */
182 Assert(pBuf);
183 Assert(pRingBuf);
184 Assert(pvFrame);
185 Assert(cbFrame >= sizeof(RTMAC) * 2);
186 uint32_t offWrite = pRingBuf->offWrite;
187 Assert(offWrite == RT_ALIGN_32(offWrite, sizeof(INTNETHDR)));
188 uint32_t offRead = pRingBuf->offRead;
189 Assert(offRead == RT_ALIGN_32(offRead, sizeof(INTNETHDR)));
190
191 const uint32_t cb = RT_ALIGN_32(cbFrame, sizeof(INTNETHDR));
192 if (offRead <= offWrite)
193 {
194 /*
195 * Try fit it all before the end of the buffer.
196 */
197 if (pRingBuf->offEnd - offWrite >= cb + sizeof(INTNETHDR))
198 {
199 PINTNETHDR pHdr = (PINTNETHDR)((uint8_t *)pBuf + offWrite);
200 pHdr->u16Type = INTNETHDR_TYPE_FRAME;
201 pHdr->cbFrame = cbFrame;
202 pHdr->offFrame = sizeof(INTNETHDR);
203
204 memcpy(pHdr + 1, pvFrame, cbFrame);
205
206 offWrite += cb + sizeof(INTNETHDR);
207 Assert(offWrite <= pRingBuf->offEnd && offWrite >= pRingBuf->offStart);
208 if (offWrite >= pRingBuf->offEnd)
209 offWrite = pRingBuf->offStart;
210 Log2(("WriteFrame: offWrite: %#x -> %#x (1)\n", pRingBuf->offWrite, offWrite));
211 ASMAtomicXchgU32(&pRingBuf->offWrite, offWrite);
212 return VINF_SUCCESS;
213 }
214
215 /*
216 * Try fit the frame at the start of the buffer.
217 * (The header fits before the end of the buffer because of alignment.)
218 */
219 AssertMsg(pRingBuf->offEnd - offWrite >= sizeof(INTNETHDR), ("offEnd=%x offWrite=%x\n", pRingBuf->offEnd, offWrite));
220 if (offRead - pRingBuf->offStart > cb) /* not >= ! */
221 {
222 PINTNETHDR pHdr = (PINTNETHDR)((uint8_t *)pBuf + offWrite);
223 void *pvFrameOut = (PINTNETHDR)((uint8_t *)pBuf + pRingBuf->offStart);
224 pHdr->u16Type = INTNETHDR_TYPE_FRAME;
225 pHdr->cbFrame = cbFrame;
226 pHdr->offFrame = (intptr_t)pvFrameOut - (intptr_t)pHdr;
227
228 memcpy(pvFrameOut, pvFrame, cbFrame);
229
230 offWrite = pRingBuf->offStart + cb;
231 ASMAtomicXchgU32(&pRingBuf->offWrite, offWrite);
232 Log2(("WriteFrame: offWrite: %#x -> %#x (2)\n", pRingBuf->offWrite, offWrite));
233 return VINF_SUCCESS;
234 }
235 }
236 /*
237 * The reader is ahead of the writer, try fit it into that space.
238 */
239 else if (offRead - offWrite > cb + sizeof(INTNETHDR)) /* not >= ! */
240 {
241 PINTNETHDR pHdr = (PINTNETHDR)((uint8_t *)pBuf + offWrite);
242 pHdr->u16Type = INTNETHDR_TYPE_FRAME;
243 pHdr->cbFrame = cbFrame;
244 pHdr->offFrame = sizeof(INTNETHDR);
245
246 memcpy(pHdr + 1, pvFrame, cbFrame);
247
248 offWrite += cb + sizeof(INTNETHDR);
249 ASMAtomicXchgU32(&pRingBuf->offWrite, offWrite);
250 Log2(("WriteFrame: offWrite: %#x -> %#x (3)\n", pRingBuf->offWrite, offWrite));
251 return VINF_SUCCESS;
252 }
253
254 /* (it didn't fit) */
255 /** @todo stats */
256 return VERR_BUFFER_OVERFLOW;
257}
258
259
260/**
261 * Transmits one frame after appending the CRC.
262 *
263 * @param hIf The interface handle.
264 * @param pSession The session.
265 * @param pBuf The shared interface buffer.
266 * @param pvFrame The frame without a crc.
267 * @param cbFrame The size of it.
268 * @param pFileRaw The file to write the raw data to (optional).
269 * @param pFileText The file to write a textual packet summary to (optional).
270 */
271static void doXmitFrame(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, PINTNETBUF pBuf, void *pvFrame, size_t cbFrame, PRTSTREAM pFileRaw, PRTSTREAM pFileText)
272{
273 /*
274 * Log it.
275 */
276 if (pFileText)
277 {
278 PCRTNETETHERHDR pEthHdr = (PCRTNETETHERHDR)pvFrame;
279 uint64_t NanoTS = RTTimeNanoTS() - g_StartTS;
280 RTStrmPrintf(pFileText, "%3RU64.%09u: cb=%04x dst=%.6Rhxs src=%.6Rhxs type=%04x Send!\n",
281 NanoTS / 1000000000, (uint32_t)(NanoTS % 1000000000),
282 cbFrame, &pEthHdr->SrcMac, &pEthHdr->DstMac, RT_BE2H_U16(pEthHdr->EtherType));
283 }
284
285 /*
286 * Run in thru the frame validator to test the RTNet code.
287 */
288 tstIntNetTestFrame(pvFrame, cbFrame, pFileText);
289
290 /*
291 * Write the frame and push the queue.
292 *
293 * Don't bother with dealing with overflows like DrvIntNet does, because
294 * it's not supposed to happen here in this testcase.
295 */
296 int rc = tstIntNetWriteFrame(pBuf, &pBuf->Send, pvFrame, cbFrame);
297 if (RT_SUCCESS(rc))
298 {
299 if (pFileRaw)
300 PcapStreamFrame(pFileRaw, g_StartTS, pvFrame, cbFrame, 0xffff);
301 }
302 else
303 {
304 RTPrintf("tstIntNet-1: tstIntNetWriteFrame failed, %Rrc; cbFrame=%d pBuf->cbSend=%d\n", rc, cbFrame, pBuf->cbSend);
305 g_cErrors++;
306 }
307
308 INTNETIFSENDREQ SendReq;
309 SendReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
310 SendReq.Hdr.cbReq = sizeof(SendReq);
311 SendReq.pSession = pSession;
312 SendReq.hIf = hIf;
313 rc = SUPCallVMMR0Ex(NIL_RTR0PTR, VMMR0_DO_INTNET_IF_SEND, 0, &SendReq.Hdr);
314 if (RT_FAILURE(rc))
315 {
316 RTPrintf("tstIntNet-1: SUPCallVMMR0Ex(,VMMR0_DO_INTNET_IF_SEND,) failed, rc=%Rrc\n", rc);
317 g_cErrors++;
318 }
319
320}
321
322
323/**
324 * Does the transmit test.
325 *
326 * @param hIf The interface handle.
327 * @param pSession The session.
328 * @param pBuf The shared interface buffer.
329 * @param pSrcMac The mac address to use as source.
330 * @param pFileRaw The file to write the raw data to (optional).
331 * @param pFileText The file to write a textual packet summary to (optional).
332 */
333static void doXmitTest(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, PINTNETBUF pBuf, PCRTMAC pSrcMac, PRTSTREAM pFileRaw, PRTSTREAM pFileText)
334{
335 uint8_t abFrame[4096];
336 PRTNETETHERHDR pEthHdr = (PRTNETETHERHDR)&abFrame[0];
337 PRTNETIPV4 pIpHdr = (PRTNETIPV4) (pEthHdr + 1);
338 PRTNETUDP pUdpHdr = (PRTNETUDP) (pIpHdr + 1);
339 PRTNETDHCP pDhcpMsg = (PRTNETDHCP) (pUdpHdr + 1);
340
341 /*
342 * Create a simple DHCP broadcast request.
343 */
344 memset(&abFrame, 0, sizeof(abFrame));
345
346 pDhcpMsg->Op = 1; /* request */
347 pDhcpMsg->HType = 1; /* ethernet */
348 pDhcpMsg->HLen = sizeof(RTMAC);
349 pDhcpMsg->Hops = 0;
350 pDhcpMsg->XID = g_DhcpXID = RTRandU32();
351 pDhcpMsg->Secs = 0;
352 pDhcpMsg->Flags = 0; /* unicast */ //RT_H2BE_U16(0x8000); /* broadcast */
353 pDhcpMsg->CIAddr.u = 0;
354 pDhcpMsg->YIAddr.u = 0;
355 pDhcpMsg->SIAddr.u = 0;
356 pDhcpMsg->GIAddr.u = 0;
357 memset(&pDhcpMsg->CHAddr[0], '\0', sizeof(pDhcpMsg->CHAddr));
358 memcpy(&pDhcpMsg->CHAddr[0], pSrcMac, sizeof(*pSrcMac));
359 memset(&pDhcpMsg->SName[0], '\0', sizeof(pDhcpMsg->SName));
360 memset(&pDhcpMsg->File[0], '\0', sizeof(pDhcpMsg->File));
361 pDhcpMsg->abMagic[0] = 99;
362 pDhcpMsg->abMagic[1] = 130;
363 pDhcpMsg->abMagic[2] = 83;
364 pDhcpMsg->abMagic[3] = 99;
365
366 pDhcpMsg->DhcpOpt = 53; /* DHCP Msssage Type option */
367 pDhcpMsg->DhcpLen = 1;
368 pDhcpMsg->DhcpReq = 1; /* DHCPDISCOVER */
369
370 memset(&pDhcpMsg->abOptions[0], '\0', sizeof(pDhcpMsg->abOptions));
371 uint8_t *pbOpt = &pDhcpMsg->abOptions[0];
372
373 *pbOpt++ = 116; /* DHCP Auto-Configure */
374 *pbOpt++ = 1;
375 *pbOpt++ = 1;
376
377 *pbOpt++ = 61; /* Client identifier */
378 *pbOpt++ = 1 + sizeof(*pSrcMac);
379 *pbOpt++ = 1; /* hw type: ethernet */
380 memcpy(pbOpt, pSrcMac, sizeof(*pSrcMac));
381 pbOpt += sizeof(*pSrcMac);
382
383 *pbOpt++ = 12; /* Host name */
384 *pbOpt++ = sizeof("tstIntNet-1") - 1;
385 memcpy(pbOpt, "tstIntNet-1", sizeof("tstIntNet-1") - 1);
386 pbOpt += sizeof("tstIntNet-1") - 1;
387
388 *pbOpt = 0xff; /* the end */
389
390 /* UDP */
391 pUdpHdr->uh_sport = RT_H2BE_U16(68); /* bootp */
392 pUdpHdr->uh_dport = RT_H2BE_U16(67); /* bootps */
393 pUdpHdr->uh_ulen = RT_H2BE_U16(sizeof(*pDhcpMsg) + sizeof(*pUdpHdr));
394 pUdpHdr->uh_sum = 0; /* pretend checksumming is disabled */
395
396 /* IP */
397 pIpHdr->ip_v = 4;
398 pIpHdr->ip_hl = sizeof(*pIpHdr) / sizeof(uint32_t);
399 pIpHdr->ip_tos = 0;
400 pIpHdr->ip_len = RT_H2BE_U16(sizeof(*pDhcpMsg) + sizeof(*pUdpHdr) + sizeof(*pIpHdr));
401 pIpHdr->ip_id = (uint16_t)RTRandU32();
402 pIpHdr->ip_off = 0;
403 pIpHdr->ip_ttl = 255;
404 pIpHdr->ip_p = 0x11; /* UDP */
405 pIpHdr->ip_sum = 0;
406 pIpHdr->ip_src.u = 0;
407 pIpHdr->ip_dst.u = UINT32_C(0xffffffff); /* broadcast */
408 pIpHdr->ip_sum = RTNetIPv4HdrChecksum(pIpHdr);
409
410 /* calc the UDP checksum. */
411 pUdpHdr->uh_sum = RTNetIPv4UDPChecksum(pIpHdr, pUdpHdr, pUdpHdr + 1);
412
413 /* Ethernet */
414 memset(&pEthHdr->DstMac, 0xff, sizeof(pEthHdr->DstMac)); /* broadcast */
415 pEthHdr->SrcMac = *pSrcMac;
416 pEthHdr->EtherType = RT_H2BE_U16(RTNET_ETHERTYPE_IPV4); /* IP */
417
418 doXmitFrame(hIf, pSession, pBuf, &abFrame[0], (uint8_t *)(pDhcpMsg + 1) - (uint8_t *)&abFrame[0], pFileRaw, pFileText);
419}
420
421
422/**
423 * Does packet sniffing for a given period of time.
424 *
425 * @param hIf The interface handle.
426 * @param pSession The session.
427 * @param pBuf The shared interface buffer.
428 * @param cMillies The time period, ms.
429 * @param pFileRaw The file to write the raw data to (optional).
430 * @param pFileText The file to write a textual packet summary to (optional).
431 * @param pSrcMac Out MAC address.
432 */
433static void doPacketSniffing(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, PINTNETBUF pBuf, uint32_t cMillies,
434 PRTSTREAM pFileRaw, PRTSTREAM pFileText, PCRTMAC pSrcMac)
435{
436 /*
437 * The loop.
438 */
439 PINTNETRINGBUF pRingBuf = &pBuf->Recv;
440 for (;;)
441 {
442 /*
443 * Wait for a packet to become available.
444 */
445 uint64_t cElapsedMillies = (RTTimeNanoTS() - g_StartTS) / 1000000;
446 if (cElapsedMillies >= cMillies)
447 break;
448 INTNETIFWAITREQ WaitReq;
449 WaitReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
450 WaitReq.Hdr.cbReq = sizeof(WaitReq);
451 WaitReq.pSession = pSession;
452 WaitReq.hIf = hIf;
453 WaitReq.cMillies = cMillies - (uint32_t)cElapsedMillies;
454 int rc = SUPCallVMMR0Ex(NIL_RTR0PTR, VMMR0_DO_INTNET_IF_WAIT, 0, &WaitReq.Hdr);
455 if (rc == VERR_TIMEOUT)
456 break;
457 if (RT_FAILURE(rc))
458 {
459 g_cErrors++;
460 RTPrintf("tstIntNet-1: VMMR0_DO_INTNET_IF_WAIT returned %Rrc\n", rc);
461 break;
462 }
463
464 /*
465 * Process the receive buffer.
466 */
467 while (INTNETRingGetReadable(pRingBuf) > 0)
468 {
469 PINTNETHDR pHdr = (PINTNETHDR)((uintptr_t)pBuf + pRingBuf->offRead);
470 if (pHdr->u16Type == INTNETHDR_TYPE_FRAME)
471 {
472 size_t cbFrame = pHdr->cbFrame;
473 const void *pvFrame = INTNETHdrGetFramePtr(pHdr, pBuf);
474 uint64_t NanoTS = RTTimeNanoTS() - g_StartTS;
475
476 if (pFileRaw)
477 PcapStreamFrame(pFileRaw, g_StartTS, pvFrame, cbFrame, 0xffff);
478
479 PCRTNETETHERHDR pEthHdr = (PCRTNETETHERHDR)pvFrame;
480 if (pFileText)
481 RTStrmPrintf(pFileText, "%3RU64.%09u: cb=%04x dst=%.6Rhxs src=%.6Rhxs type=%04x%s\n",
482 NanoTS / 1000000000, (uint32_t)(NanoTS % 1000000000),
483 cbFrame, &pEthHdr->DstMac, &pEthHdr->SrcMac, RT_BE2H_U16(pEthHdr->EtherType),
484 !memcmp(&pEthHdr->DstMac, pSrcMac, sizeof(*pSrcMac)) ? " Mine!" : "");
485 tstIntNetTestFrame(pvFrame, cbFrame, pFileText);
486
487 /* Loop for the DHCP reply. */
488 if ( cbFrame > 64
489 && RT_BE2H_U16(pEthHdr->EtherType) == 0x0800 /* EtherType == IP */)
490 {
491 PCRTNETIPV4 pIpHdr = (PCRTNETIPV4)(pEthHdr + 1);
492 PCRTNETUDP pUdpHdr = (PCRTNETUDP)((uint32_t *)pIpHdr + pIpHdr->ip_hl);
493 if ( pIpHdr->ip_p == 0x11 /*UDP*/
494 && RT_BE2H_U16(pUdpHdr->uh_dport) == 68 /* bootp */
495 && RT_BE2H_U16(pUdpHdr->uh_sport) == 67 /* bootps */)
496 {
497 PCRTNETDHCP pDhcpMsg = (PCRTNETDHCP)(pUdpHdr + 1);
498 if ( pDhcpMsg->Op == 2 /* boot reply */
499 && pDhcpMsg->HType == 1 /* ethernet */
500 && pDhcpMsg->HLen == sizeof(RTMAC)
501 && (pDhcpMsg->XID == g_DhcpXID || !g_DhcpXID)
502 && !memcmp(&pDhcpMsg->CHAddr[0], pSrcMac, sizeof(*pSrcMac)))
503 {
504 g_fDhcpReply = true;
505 RTPrintf("tstIntNet-1: DHCP server reply! My IP: %d.%d.%d.%d\n",
506 pDhcpMsg->YIAddr.au8[0],
507 pDhcpMsg->YIAddr.au8[1],
508 pDhcpMsg->YIAddr.au8[2],
509 pDhcpMsg->YIAddr.au8[3]);
510 }
511 }
512 }
513 }
514 else
515 {
516 RTPrintf("tstIntNet-1: Unknown frame type %d\n", pHdr->u16Type);
517 g_cErrors++;
518 }
519
520 /* Advance to the next frame. */
521 INTNETRingSkipFrame(pBuf, pRingBuf);
522 }
523 }
524
525 uint64_t NanoTS = RTTimeNanoTS() - g_StartTS;
526 RTStrmPrintf(pFileText ? pFileText : g_pStdOut,
527 "%3RU64.%09u: stopped. cRecvs=%RU64 cbRecv=%RU64 cLost=%RU64 cOYs=%RU64 cNYs=%RU64\n",
528 NanoTS / 1000000000, (uint32_t)(NanoTS % 1000000000),
529 pBuf->cStatRecvs.c,
530 pBuf->cbStatRecv.c,
531 pBuf->cStatLost.c,
532 pBuf->cStatYieldsOk.c,
533 pBuf->cStatYieldsNok.c
534 );
535 RTStrmPrintf(pFileText ? pFileText : g_pStdOut,
536 "%3RU64.%09u: cOtherPkts=%RU32 cArpPkts=%RU32 cIpv4Pkts=%RU32 cTcpPkts=%RU32 cUdpPkts=%RU32 cDhcpPkts=%RU32\n",
537 NanoTS / 1000000000, (uint32_t)(NanoTS % 1000000000),
538 g_cOtherPkts, g_cArpPkts, g_cIpv4Pkts, g_cTcpPkts, g_cUdpPkts, g_cDhcpPkts);
539}
540
541
542int main(int argc, char **argv)
543{
544 /*
545 * Init the runtime and parse the arguments.
546 */
547 RTR3Init(false, 0);
548
549 static RTOPTIONDEF const s_aOptions[] =
550 {
551 { "--duration", 'd', RTGETOPT_REQ_UINT32 },
552 { "--file", 'f', RTGETOPT_REQ_STRING },
553 { "--interface", 'i', RTGETOPT_REQ_STRING },
554 { "--mac-sharing", 'm', RTGETOPT_REQ_NOTHING },
555 { "--network", 'n', RTGETOPT_REQ_STRING },
556 { "--promiscuous", 'p', RTGETOPT_REQ_NOTHING },
557 { "--recv-buffer", 'r', RTGETOPT_REQ_UINT32 },
558 { "--send-buffer", 's', RTGETOPT_REQ_UINT32 },
559 { "--sniffer", 'S', RTGETOPT_REQ_NOTHING },
560 { "--text-file", 't', RTGETOPT_REQ_STRING },
561 { "--xmit-test", 'x', RTGETOPT_REQ_NOTHING },
562 { "--help", 'h', RTGETOPT_REQ_NOTHING },
563 { "--?", '?', RTGETOPT_REQ_NOTHING },
564 };
565
566 uint32_t cMillies = 1000;
567 PRTSTREAM pFileRaw = NULL;
568#ifdef RT_OS_DARWIN
569 const char *pszIf = "en0";
570#elif defined(RT_OS_LINUX)
571 const char *pszIf = "eth0";
572#elif defined(RT_OS_SOLARIS)
573 const char* pszIf = "rge0";
574#else
575 const char *pszIf = "em0";
576#endif
577 bool fMacSharing = false;
578 const char *pszNetwork = "tstIntNet-1";
579 bool fPromiscuous = false;
580 uint32_t cbRecv = 0;
581 uint32_t cbSend = 0;
582 bool fSniffer = false;
583 PRTSTREAM pFileText = g_pStdOut;
584 bool fXmitTest = false;
585 RTMAC SrcMac;
586 SrcMac.au8[0] = 0x08;
587 SrcMac.au8[1] = 0x03;
588 SrcMac.au8[2] = 0x86;
589 RTRandBytes(&SrcMac.au8[3], sizeof(SrcMac) - 3);
590
591 int rc;
592 int ch;
593 int iArg = 1;
594 RTOPTIONUNION Value;
595 while ((ch = RTGetOpt(argc,argv, &s_aOptions[0], RT_ELEMENTS(s_aOptions), &iArg, &Value)))
596 switch (ch)
597 {
598 case 'd':
599 cMillies = Value.u32 * 1000;
600 if (cMillies / 1000 != Value.u32)
601 {
602 RTPrintf("tstIntNet-1: warning duration overflowed\n");
603 cMillies = UINT32_MAX - 1;
604 }
605 break;
606
607 case 'f':
608 rc = RTStrmOpen(Value.psz, "w+b", &pFileRaw);
609 if (RT_FAILURE(rc))
610 {
611 RTPrintf("tstIntNet-1: Failed to creating \"%s\" for writing: %Rrc\n", Value.psz, rc);
612 return 1;
613 }
614 break;
615
616 case 'i':
617 pszIf = Value.psz;
618 if (strlen(pszIf) >= INTNET_MAX_TRUNK_NAME)
619 {
620 RTPrintf("tstIntNet-1: Interface name is too long (max %d chars): %s\n", INTNET_MAX_TRUNK_NAME - 1, pszIf);
621 return 1;
622 }
623 break;
624
625 case 'm':
626 fMacSharing = true;
627 break;
628
629 case 'n':
630 pszNetwork = Value.psz;
631 if (strlen(pszNetwork) >= INTNET_MAX_NETWORK_NAME)
632 {
633 RTPrintf("tstIntNet-1: Network name is too long (max %d chars): %s\n", INTNET_MAX_NETWORK_NAME - 1, pszNetwork);
634 return 1;
635 }
636 break;
637
638 case 'p':
639 fPromiscuous = true;
640 break;
641
642 case 'r':
643 cbRecv = Value.u32;
644 break;
645
646 case 's':
647 cbSend = Value.u32;
648 break;
649
650 case 'S':
651 fSniffer = true;
652 break;
653
654 case 't':
655 if (!*Value.psz)
656 pFileText = NULL;
657 else if (!strcmp(Value.psz, "-"))
658 pFileText = g_pStdOut;
659 else if (!strcmp(Value.psz, "!"))
660 pFileText = g_pStdErr;
661 else
662 {
663 rc = RTStrmOpen(Value.psz, "w", &pFileText);
664 if (RT_FAILURE(rc))
665 {
666 RTPrintf("tstIntNet-1: Failed to creating \"%s\" for writing: %Rrc\n", Value.psz, rc);
667 return 1;
668 }
669 }
670 break;
671
672 case 'x':
673 fXmitTest = true;
674 break;
675
676 case '?':
677 case 'h':
678 RTPrintf("syntax: tstIntNet-1 [-pStx-] [-d <secs>] [-f <file>] [-r <size>] [-s <size>]\n");
679 return 1;
680
681 default:
682 if (RT_SUCCESS(ch))
683 RTPrintf("tstIntNetR0: invalid argument (%#x): %s\n", ch, Value.psz);
684 else if (Value.pDef)
685 RTPrintf("tstIntNetR0: invalid argument: %Rrc - %s\n", ch, Value.pDef->pszLong);
686 else
687 RTPrintf("tstIntNetR0: invalid argument: %Rrc - %s\n", ch, argv[iArg]);
688 return 1;
689 }
690 if (iArg < argc)
691 {
692 RTPrintf("tstIntNetR0: invalid argument: %s\n", argv[iArg]);
693 return 1;
694 }
695
696
697 RTPrintf("tstIntNet-1: TESTING...\n");
698
699 /*
700 * Open the session, load ring-0 and issue the request.
701 */
702 PSUPDRVSESSION pSession;
703 rc = SUPInit(&pSession, 0);
704 if (RT_FAILURE(rc))
705 {
706 RTPrintf("tstIntNet-1: SUPInit -> %Rrc\n", rc);
707 return 1;
708 }
709
710 char szPath[RTPATH_MAX];
711 rc = RTPathProgram(szPath, sizeof(szPath) - sizeof("/../VMMR0.r0"));
712 if (RT_FAILURE(rc))
713 {
714 RTPrintf("tstIntNet-1: RTPathProgram -> %Rrc\n", rc);
715 return 1;
716 }
717
718 rc = SUPLoadVMM(strcat(szPath, "/../VMMR0.r0"));
719 if (RT_FAILURE(rc))
720 {
721 RTPrintf("tstIntNet-1: SUPLoadVMM(\"%s\") -> %Rrc\n", szPath, rc);
722 return 1;
723 }
724
725 /*
726 * Create the request, picking the network and trunk names from argv[2]
727 * and argv[1] if present.
728 */
729 INTNETOPENREQ OpenReq;
730 OpenReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
731 OpenReq.Hdr.cbReq = sizeof(OpenReq);
732 OpenReq.pSession = pSession;
733 strncpy(OpenReq.szNetwork, pszNetwork, sizeof(OpenReq.szNetwork));
734 strncpy(OpenReq.szTrunk, pszIf, sizeof(OpenReq.szTrunk));
735 OpenReq.enmTrunkType = kIntNetTrunkType_NetFlt;
736 OpenReq.fFlags = fMacSharing ? INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE : 0;
737 OpenReq.cbSend = cbSend;
738 OpenReq.cbRecv = cbRecv;
739 OpenReq.hIf = INTNET_HANDLE_INVALID;
740
741 /*
742 * Issue the request.
743 */
744 RTPrintf("tstIntNet-1: attempting to open/create network \"%s\" with NetFlt trunk \"%s\"...\n",
745 OpenReq.szNetwork, OpenReq.szTrunk);
746 RTStrmFlush(g_pStdOut);
747 rc = SUPCallVMMR0Ex(NIL_RTR0PTR, VMMR0_DO_INTNET_OPEN, 0, &OpenReq.Hdr);
748 if (RT_SUCCESS(rc))
749 {
750 RTPrintf("tstIntNet-1: successfully opened/created \"%s\" with NetFlt trunk \"%s\" - hIf=%#x\n",
751 OpenReq.szNetwork, OpenReq.szTrunk, OpenReq.hIf);
752 RTStrmFlush(g_pStdOut);
753
754 /*
755 * Get the ring-3 address of the shared interface buffer.
756 */
757 INTNETIFGETRING3BUFFERREQ GetRing3BufferReq;
758 GetRing3BufferReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
759 GetRing3BufferReq.Hdr.cbReq = sizeof(GetRing3BufferReq);
760 GetRing3BufferReq.pSession = pSession;
761 GetRing3BufferReq.hIf = OpenReq.hIf;
762 GetRing3BufferReq.pRing3Buf = NULL;
763 rc = SUPCallVMMR0Ex(NIL_RTR0PTR, VMMR0_DO_INTNET_IF_GET_RING3_BUFFER, 0, &GetRing3BufferReq.Hdr);
764 if (RT_SUCCESS(rc))
765 {
766 PINTNETBUF pBuf = GetRing3BufferReq.pRing3Buf;
767 RTPrintf("tstIntNet-1: pBuf=%p cbBuf=%d cbSend=%d cbRecv=%d\n",
768 pBuf, pBuf->cbBuf, pBuf->cbSend, pBuf->cbRecv);
769 RTStrmFlush(g_pStdOut);
770 if (fPromiscuous)
771 {
772 INTNETIFSETPROMISCUOUSMODEREQ PromiscReq;
773 PromiscReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
774 PromiscReq.Hdr.cbReq = sizeof(PromiscReq);
775 PromiscReq.pSession = pSession;
776 PromiscReq.hIf = OpenReq.hIf;
777 PromiscReq.fPromiscuous = true;
778 rc = SUPCallVMMR0Ex(NIL_RTR0PTR, VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE, 0, &PromiscReq.Hdr);
779 if (RT_SUCCESS(rc))
780 RTPrintf("tstIntNet-1: interface in promiscuous mode\n");
781 }
782 if (RT_SUCCESS(rc))
783 {
784 /*
785 * Activate the interface.
786 */
787 INTNETIFSETACTIVEREQ ActiveReq;
788 ActiveReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
789 ActiveReq.Hdr.cbReq = sizeof(ActiveReq);
790 ActiveReq.pSession = pSession;
791 ActiveReq.hIf = OpenReq.hIf;
792 ActiveReq.fActive = true;
793 rc = SUPCallVMMR0Ex(NIL_RTR0PTR, VMMR0_DO_INTNET_IF_SET_ACTIVE, 0, &ActiveReq.Hdr);
794 if (RT_SUCCESS(rc))
795 {
796 /*
797 * Start the stop watch, init the pcap file.
798 */
799 g_StartTS = RTTimeNanoTS();
800 if (pFileRaw)
801 PcapStreamHdr(pFileRaw, g_StartTS);
802
803 /*
804 * Do the transmit test first and so we can sniff for the response.
805 */
806 if (fXmitTest)
807 doXmitTest(OpenReq.hIf, pSession, pBuf, &SrcMac, pFileRaw, pFileText);
808
809 /*
810 * Either enter sniffing mode or do a timeout thing.
811 */
812 if (fSniffer)
813 {
814 doPacketSniffing(OpenReq.hIf, pSession, pBuf, cMillies, pFileRaw, pFileText, &SrcMac);
815 if (fXmitTest != g_fDhcpReply)
816 {
817 RTPrintf("tstIntNet-1: Error! The DHCP server didn't reply... (Perhaps you don't have one?)\n", rc);
818 g_cErrors++;
819 }
820 }
821 else
822 RTThreadSleep(cMillies);
823 }
824 else
825 {
826 RTPrintf("tstIntNet-1: SUPCallVMMR0Ex(,VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE,) failed, rc=%Rrc\n", rc);
827 g_cErrors++;
828 }
829 }
830 else
831 {
832 RTPrintf("tstIntNet-1: SUPCallVMMR0Ex(,VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE,) failed, rc=%Rrc\n", rc);
833 g_cErrors++;
834 }
835 }
836 else
837 {
838 RTPrintf("tstIntNet-1: SUPCallVMMR0Ex(,VMMR0_DO_INTNET_IF_GET_RING3_BUFFER,) failed, rc=%Rrc\n", rc);
839 g_cErrors++;
840 }
841 }
842 else
843 {
844 RTPrintf("tstIntNet-1: SUPCallVMMR0Ex(,VMMR0_DO_INTNET_OPEN,) failed, rc=%Rrc\n", rc);
845 g_cErrors++;
846 }
847
848 SUPTerm(false /* not forced */);
849
850 /* close open files */
851 if (pFileRaw)
852 RTStrmClose(pFileRaw);
853 if (pFileText && pFileText != g_pStdErr && pFileText != g_pStdOut)
854 RTStrmClose(pFileText);
855
856 /*
857 * Summary.
858 */
859 if (!g_cErrors)
860 RTPrintf("tstIntNet-1: SUCCESS\n");
861 else
862 RTPrintf("tstIntNet-1: FAILURE - %d errors\n", g_cErrors);
863
864 return !!g_cErrors;
865}
866
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