VirtualBox

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

Last change on this file since 82477 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

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