tstIntNet-1.cpp revision bfc2239ab730eaaa90f72029545a225bb387dd04
/* $Id$ */
/** @file
* VBox - Testcase for internal networking, simple NetFlt trunk creation.
*/
/*
* Copyright (C) 2006-2007 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <VBox/intnetinline.h>
#include <iprt/initterm.h>
#include "../Pcap.h"
/*******************************************************************************
* Global Variables *
*******************************************************************************/
static int g_cErrors = 0;
static bool g_fDhcpReply = false;
static bool g_fPingReply = false;
static uint32_t g_cOtherPkts = 0;
static uint32_t g_cArpPkts = 0;
static uint32_t g_cIpv4Pkts = 0;
static uint32_t g_cUdpPkts = 0;
static uint32_t g_cDhcpPkts = 0;
static uint32_t g_cTcpPkts = 0;
/**
* Error reporting wrapper.
*
* @param pErrStrm The stream to write the error message to. Can be NULL.
* @param pszFormat The message format string.
* @param ... Format arguments.
*/
{
if (!pErrStrm)
g_cErrors++;
}
/**
* Parses a frame an runs in thru the RTNet validation code so it gets
* some exercise.
*
* @param pvFrame Pointer to the ethernet frame.
* @param cbFrame The size of the ethernet frame.
* @param pErrStrm The error stream.
*/
{
/*
* Ethernet header.
*/
{
case RTNET_ETHERTYPE_ARP:
{
g_cArpPkts++;
break;
}
case RTNET_ETHERTYPE_IPV4:
{
g_cIpv4Pkts++;
AssertFatal(cbLeft >= 0);
{
case RTNETIPV4_PROT_ICMP:
{
/** @todo ICMP? */
break;
}
case RTNETIPV4_PROT_UDP:
{
g_cUdpPkts++;
{
g_cDhcpPkts++;
}
break;
}
case RTNETIPV4_PROT_TCP:
{
g_cTcpPkts++;
break;
}
}
break;
}
//case RTNET_ETHERTYPE_IPV6:
default:
g_cOtherPkts++;
break;
}
}
/**
* Transmits one frame after appending the CRC.
*
* @param hIf The interface handle.
* @param pSession The session.
* @param pBuf The shared interface buffer.
* @param pvFrame The frame without a crc.
* @param cbFrame The size of it.
* @param pFileRaw The file to write the raw data to (optional).
* @param pFileText The file to write a textual packet summary to (optional).
*/
static void doXmitFrame(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, PINTNETBUF pBuf, void *pvFrame, size_t cbFrame, PRTSTREAM pFileRaw, PRTSTREAM pFileText)
{
/*
* Log it.
*/
if (pFileText)
{
}
/*
* Run in thru the frame validator to test the RTNet code.
*/
/*
* Write the frame and push the queue.
*
* Don't bother with dealing with overflows like DrvIntNet does, because
* it's not supposed to happen here in this testcase.
*/
if (RT_SUCCESS(rc))
{
if (pFileRaw)
}
else
{
RTPrintf("tstIntNet-1: IntNetRingWriteFrame failed, %Rrc; cbFrame=%d pBuf->cbSend=%d\n", rc, cbFrame, pBuf->cbSend);
g_cErrors++;
}
if (RT_FAILURE(rc))
{
g_cErrors++;
}
}
/**
* Does the transmit test.
*
* @param hIf The interface handle.
* @param pSession The session.
* @param pBuf The shared interface buffer.
* @param pSrcMac The mac address to use as source.
* @param pFileRaw The file to write the raw data to (optional).
* @param pFileText The file to write a textual packet summary to (optional).
*/
static void doXmitTest(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, PINTNETBUF pBuf, PCRTMAC pSrcMac, PRTSTREAM pFileRaw, PRTSTREAM pFileText)
{
/*
* Create a simple DHCP broadcast request.
*/
*pbOpt++ = 1;
*pbOpt++ = 1;
/* UDP */
/* IP */
/* calc the UDP checksum. */
/* Ethernet */
doXmitFrame(hIf, pSession, pBuf, &abFrame[0], (uint8_t *)(pDhcpMsg + 1) - (uint8_t *)&abFrame[0], pFileRaw, pFileText);
}
{
int cSum = 0;
while (cbLeft > 1)
{
cbLeft -= 2;
}
if (cbLeft == 1)
{
}
return Result;
}
/**
* Does the rudimentary ping test with fixed destination and source IPs.
*
* @param hIf The interface handle.
* @param pSession The session.
* @param pBuf The shared interface buffer.
* @param pSrcMac The mac address to use as source.
* @param pFileRaw The file to write the raw data to (optional).
* @param pFileText The file to write a textual packet summary to (optional).
*/
static void doPingTest(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, PINTNETBUF pBuf, PCRTMAC pSrcMac, PRTSTREAM pFileRaw, PRTSTREAM pFileText)
{
/*
* Create a simple ping request.
*/
/* IP */
/* Ethernet */
#if 0 /* Enable with host's real Mac address for testing of the testcase. */
#endif
doXmitFrame(hIf, pSession, pBuf, &abFrame[0], (uint8_t *)(pIcmpEcho + 1) + cbPad - (uint8_t *)&abFrame[0], pFileRaw, pFileText);
}
/**
* Does packet sniffing for a given period of time.
*
* @param hIf The interface handle.
* @param pSession The session.
* @param pBuf The shared interface buffer.
* @param cMillies The time period, ms.
* @param pFileRaw The file to write the raw data to (optional).
* @param pFileText The file to write a textual packet summary to (optional).
* @param pSrcMac Out MAC address.
*/
static void doPacketSniffing(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, PINTNETBUF pBuf, uint32_t cMillies,
{
/*
* The loop.
*/
for (;;)
{
/*
* Wait for a packet to become available.
*/
if (cElapsedMillies >= cMillies)
break;
break;
if (RT_FAILURE(rc))
{
g_cErrors++;
break;
}
/*
* Process the receive buffer.
*/
{
{
if (pFileRaw)
if (pFileText)
/* Loop for the DHCP reply. */
if ( cbFrame > 64
{
{
{
g_fDhcpReply = true;
RTPrintf("tstIntNet-1: DHCP server reply! My IP: %d.%d.%d.%d\n",
}
}
{
#if 0
/** Enable with the host's real Mac address for testing of the testcase.*/
#else
#endif
)
{
g_fPingReply = true;
RTPrintf("tstIntNet-1: Ping reply! From %d.%d.%d.%d\n",
}
else
RTPrintf("type=%d seq=%d dstmac=%.6Rhxs ip=%d.%d.%d.%d\n", pIcmpHdr->icmp_type, pIcmpEcho->icmp_seq,
&pEthHdr->DstMac, pIpHdr->ip_dst.au8[0], pIpHdr->ip_dst.au8[1], pIpHdr->ip_dst.au8[2], pIpHdr->ip_dst.au8[3]);
}
}
}
{
{
if (pFileRaw)
if (pFileText)
}
else
{
g_cErrors++;
}
}
{
g_cErrors++;
}
/* Advance to the next frame. */
}
}
"%3RU64.%09u: stopped. cRecvs=%RU64 cbRecv=%RU64 cLost=%RU64 cOYs=%RU64 cNYs=%RU64\n",
pBuf->cStatYieldsOk.c,
);
"%3RU64.%09u: cOtherPkts=%RU32 cArpPkts=%RU32 cIpv4Pkts=%RU32 cTcpPkts=%RU32 cUdpPkts=%RU32 cDhcpPkts=%RU32\n",
}
{
/*
* Init the runtime and parse the arguments.
*/
static RTGETOPTDEF const s_aOptions[] =
{
};
#ifdef RT_OS_DARWIN
const char *pszIf = "en0";
#elif defined(RT_OS_LINUX)
const char *pszIf = "eth0";
#elif defined(RT_OS_SOLARIS)
const char* pszIf = "rge0";
#else
const char *pszIf = "em0";
#endif
bool fMacSharing = false;
const char *pszNetwork = "tstIntNet-1";
bool fPromiscuous = false;
bool fSniffer = false;
bool fXmitTest = false;
bool fPingTest = false;
int rc;
int ch;
int iArg = 1;
switch (ch)
{
case 'd':
{
RTPrintf("tstIntNet-1: warning duration overflowed\n");
}
break;
case 'f':
if (RT_FAILURE(rc))
{
return 1;
}
break;
case 'i':
{
RTPrintf("tstIntNet-1: Interface name is too long (max %d chars): %s\n", INTNET_MAX_TRUNK_NAME - 1, pszIf);
return 1;
}
break;
case 'm':
fMacSharing = true;
break;
case 'n':
{
RTPrintf("tstIntNet-1: Network name is too long (max %d chars): %s\n", INTNET_MAX_NETWORK_NAME - 1, pszNetwork);
return 1;
}
break;
case 'p':
fPromiscuous = true;
break;
case 'r':
break;
case 's':
break;
case 'S':
fSniffer = true;
break;
case 't':
else
{
if (RT_FAILURE(rc))
{
return 1;
}
}
break;
case 'x':
fXmitTest = true;
break;
case 'P':
fPingTest = true;
break;
case 'h':
RTPrintf("syntax: tstIntNet-1 <options>\n"
"\n"
"Options:\n");
RTPrintf("\n"
"Examples:\n"
" tstIntNet-1 -r 8192 -s 4096 -xS\n"
" tstIntNet-1 -n VBoxNetDhcp -r 4096 -s 4096 -i \"\" -xS\n");
return 1;
case 'V':
RTPrintf("$Revision$\n");
return 0;
default:
}
RTPrintf("tstIntNet-1: TESTING...\n");
/*
* Open the session, load ring-0 and issue the request.
*/
if (RT_FAILURE(rc))
{
return 1;
}
char szPath[RTPATH_MAX];
if (RT_FAILURE(rc))
{
return 1;
}
char szAbsPath[RTPATH_MAX];
if (RT_FAILURE(rc))
{
return 1;
}
if (RT_FAILURE(rc))
{
return 1;
}
/*
* Create the request, picking the network and trunk names from argv[2]
* and argv[1] if present.
*/
/*
* Issue the request.
*/
if (RT_SUCCESS(rc))
{
/*
* Get the ring-3 address of the shared interface buffer.
*/
rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_INTNET_IF_GET_BUFFER_PTRS, 0, &GetBufferPtrsReq.Hdr);
if (RT_SUCCESS(rc))
{
RTPrintf("tstIntNet-1: pBuf=%p cbBuf=%d cbSend=%d cbRecv=%d\n",
if (fPromiscuous)
{
PromiscReq.fPromiscuous = true;
rc = SUPR3CallVMMR0Ex(NIL_RTR0PTR, NIL_VMCPUID, VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE, 0, &PromiscReq.Hdr);
if (RT_SUCCESS(rc))
RTPrintf("tstIntNet-1: interface in promiscuous mode\n");
}
if (RT_SUCCESS(rc))
{
/*
* Activate the interface.
*/
if (RT_SUCCESS(rc))
{
/*
* Start the stop watch, init the pcap file.
*/
g_StartTS = RTTimeNanoTS();
if (pFileRaw)
/*
* Do the transmit test first and so we can sniff for the response.
*/
if (fXmitTest)
if (fPingTest)
/*
* Either enter sniffing mode or do a timeout thing.
*/
if (fSniffer)
{
if ( fXmitTest
&& !g_fDhcpReply)
{
RTPrintf("tstIntNet-1: Error! The DHCP server didn't reply... (Perhaps you don't have one?)\n", rc);
g_cErrors++;
}
if ( fPingTest
&& !g_fPingReply)
{
g_cErrors++;
}
}
else
}
else
{
RTPrintf("tstIntNet-1: SUPR3CallVMMR0Ex(,VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE,) failed, rc=%Rrc\n", rc);
g_cErrors++;
}
}
else
{
RTPrintf("tstIntNet-1: SUPR3CallVMMR0Ex(,VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE,) failed, rc=%Rrc\n", rc);
g_cErrors++;
}
}
else
{
RTPrintf("tstIntNet-1: SUPR3CallVMMR0Ex(,VMMR0_DO_INTNET_IF_GET_BUFFER_PTRS,) failed, rc=%Rrc\n", rc);
g_cErrors++;
}
}
else
{
g_cErrors++;
}
SUPR3Term(false /*fForced*/);
/* close open files */
if (pFileRaw)
/*
* Summary.
*/
if (!g_cErrors)
RTPrintf("tstIntNet-1: SUCCESS\n");
else
return !!g_cErrors;
}