VirtualBox

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

Last change on this file since 35346 was 35346, checked in by vboxsync, 14 years ago

VMM reorg: Moving the public include files from include/VBox to include/VBox/vmm.

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