VirtualBox

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

Last change on this file since 27342 was 26574, checked in by vboxsync, 15 years ago

Networking: Preparing to make the driver return a send buffer to the device emulation.

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