DrvIntNet.cpp revision 02e50df6f5a05a0b01164c1a6fe605f328bd62d7
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* $Id$ */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/** @file
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * DrvIntNet - Internal network transport driver.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Copyright (C) 2006-2007 Sun Microsystems, Inc.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * This file is part of VirtualBox Open Source Edition (OSE), as
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * available from http://www.virtualbox.org. This file is free software;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * you can redistribute it and/or modify it under the terms of the GNU
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * General Public License (GPL) as published by the Free Software
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Foundation, in version 2 as it comes in the "COPYING" file of the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Clara, CA 95054 USA or visit http://www.sun.com if you need
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * additional information or have any questions.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*******************************************************************************
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte* Header Files *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte*******************************************************************************/
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define LOG_GROUP LOG_GROUP_DRV_INTNET
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <VBox/pdmdrv.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <VBox/cfgm.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <VBox/intnet.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <VBox/vmm.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <VBox/err.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <VBox/log.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <iprt/asm.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <iprt/assert.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <iprt/thread.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <iprt/semaphore.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <iprt/string.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <iprt/time.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "Builtins.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*******************************************************************************
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte* Structures and Typedefs *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte*******************************************************************************/
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The state of the asynchronous thread.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortetypedef enum ASYNCSTATE
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** The thread is suspended. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ASYNCSTATE_SUSPENDED = 1,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** The thread is running. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ASYNCSTATE_RUNNING,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** The thread must (/has) terminate. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ASYNCSTATE_TERMINATE,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** The usual 32-bit type blowup. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ASYNCSTATE_32BIT_HACK = 0x7fffffff
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte} ASYNCSTATE;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Block driver instance data.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortetypedef struct DRVINTNET
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** The network interface. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte PDMINETWORKCONNECTOR INetworkConnector;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** The network interface. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte PPDMINETWORKPORT pPort;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** Pointer to the driver instance. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte PPDMDRVINS pDrvIns;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** Interface handle. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte INTNETIFHANDLE hIf;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** Pointer to the communication buffer. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte PINTNETBUF pBuf;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** The thread state. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ASYNCSTATE volatile enmState;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** Reader thread. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte RTTHREAD Thread;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** Event semaphore the Thread waits on while the VM is suspended. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte RTSEMEVENT EventSuspended;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** Set if the link is down.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * When the link is down all incoming packets will be dropped. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bool volatile fLinkDown;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** Set if data transmission should start immediately and deactivate
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * as late as possible. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bool fActivateEarlyDeactivateLate;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifdef VBOX_WITH_STATISTICS
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** Profiling packet transmit runs. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte STAMPROFILE StatTransmit;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** Profiling packet receive runs. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte STAMPROFILEADV StatReceive;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif /* VBOX_WITH_STATISTICS */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifdef LOG_ENABLED
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** The nano ts of the last transfer. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint64_t u64LastTransferTS;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** The nano ts of the last receive. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint64_t u64LastReceiveTS;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** The network name. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char szNetwork[INTNET_MAX_NETWORK_NAME];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte} DRVINTNET, *PDRVINTNET;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/** Converts a pointer to DRVINTNET::INetworkConnector to a PDRVINTNET. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define PDMINETWORKCONNECTOR_2_DRVINTNET(pInterface) ( (PDRVINTNET)((uintptr_t)pInterface - RT_OFFSETOF(DRVINTNET, INetworkConnector)) )
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Writes a frame packet to the buffer.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @returns VBox status code.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param pBuf The buffer.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param pRingBuf The ring buffer to read from.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param pvFrame The frame to write.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param cbFrame The size of the frame.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @remark This is the same as INTNETRingWriteFrame
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int drvIntNetRingWriteFrame(PINTNETBUF pBuf, PINTNETRINGBUF pRingBuf, const void *pvFrame, uint32_t cbFrame)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Validate input.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Assert(pBuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Assert(pRingBuf);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Assert(pvFrame);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Assert(cbFrame >= sizeof(PDMMAC) * 2);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint32_t offWrite = pRingBuf->offWrite;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Assert(offWrite == RT_ALIGN_32(offWrite, sizeof(INTNETHDR)));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint32_t offRead = pRingBuf->offRead;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Assert(offRead == RT_ALIGN_32(offRead, sizeof(INTNETHDR)));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const uint32_t cb = RT_ALIGN_32(cbFrame, sizeof(INTNETHDR));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (offRead <= offWrite)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Try fit it all before the end of the buffer.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (pRingBuf->offEnd - offWrite >= cb + sizeof(INTNETHDR))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte PINTNETHDR pHdr = (PINTNETHDR)((uint8_t *)pBuf + offWrite);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pHdr->u16Type = INTNETHDR_TYPE_FRAME;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pHdr->cbFrame = cbFrame;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pHdr->offFrame = sizeof(INTNETHDR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte memcpy(pHdr + 1, pvFrame, cbFrame);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte offWrite += cb + sizeof(INTNETHDR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Assert(offWrite <= pRingBuf->offEnd && offWrite >= pRingBuf->offStart);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (offWrite >= pRingBuf->offEnd)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte offWrite = pRingBuf->offStart;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Log2(("WriteFrame: offWrite: %#x -> %#x (1)\n", pRingBuf->offWrite, offWrite));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ASMAtomicXchgU32(&pRingBuf->offWrite, offWrite);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return VINF_SUCCESS;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Try fit the frame at the start of the buffer.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * (The header fits before the end of the buffer because of alignment.)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte AssertMsg(pRingBuf->offEnd - offWrite >= sizeof(INTNETHDR), ("offEnd=%x offWrite=%x\n", pRingBuf->offEnd, offWrite));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (offRead - pRingBuf->offStart > cb) /* not >= ! */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte PINTNETHDR pHdr = (PINTNETHDR)((uint8_t *)pBuf + offWrite);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte void *pvFrameOut = (PINTNETHDR)((uint8_t *)pBuf + pRingBuf->offStart);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pHdr->u16Type = INTNETHDR_TYPE_FRAME;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pHdr->cbFrame = cbFrame;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pHdr->offFrame = (intptr_t)pvFrameOut - (intptr_t)pHdr;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte memcpy(pvFrameOut, pvFrame, cbFrame);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte offWrite = pRingBuf->offStart + cb;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ASMAtomicXchgU32(&pRingBuf->offWrite, offWrite);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Log2(("WriteFrame: offWrite: %#x -> %#x (2)\n", pRingBuf->offWrite, offWrite));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return VINF_SUCCESS;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The reader is ahead of the writer, try fit it into that space.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte else if (offRead - offWrite > cb + sizeof(INTNETHDR)) /* not >= ! */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte PINTNETHDR pHdr = (PINTNETHDR)((uint8_t *)pBuf + offWrite);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pHdr->u16Type = INTNETHDR_TYPE_FRAME;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pHdr->cbFrame = cbFrame;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pHdr->offFrame = sizeof(INTNETHDR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte memcpy(pHdr + 1, pvFrame, cbFrame);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte offWrite += cb + sizeof(INTNETHDR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ASMAtomicXchgU32(&pRingBuf->offWrite, offWrite);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Log2(("WriteFrame: offWrite: %#x -> %#x (3)\n", pRingBuf->offWrite, offWrite));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return VINF_SUCCESS;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* (it didn't fit) */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /** @todo stats */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return VERR_BUFFER_OVERFLOW;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Send data to the network.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @returns VBox status code.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param pInterface Pointer to the interface structure containing the called function pointer.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param pvBuf Data to send.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param cb Number of bytes to send.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @thread EMT
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic DECLCALLBACK(int) drvIntNetSend(PPDMINETWORKCONNECTOR pInterface, const void *pvBuf, size_t cb)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte PDRVINTNET pThis = PDMINETWORKCONNECTOR_2_DRVINTNET(pInterface);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte STAM_PROFILE_START(&pThis->StatTransmit, a);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifdef LOG_ENABLED
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint64_t u64Now = RTTimeProgramNanoTS();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte LogFlow(("drvIntNetSend: %-4d bytes at %llu ns deltas: r=%llu t=%llu\n",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cb, u64Now, u64Now - pThis->u64LastReceiveTS, u64Now - pThis->u64LastTransferTS));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pThis->u64LastTransferTS = u64Now;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Log2(("drvIntNetSend: pvBuf=%p cb=%#x\n"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "%.*Vhxd\n",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pvBuf, cb, cb, pvBuf));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Add the frame to the send buffer and push it onto the network.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int rc = drvIntNetRingWriteFrame(pThis->pBuf, &pThis->pBuf->Send, pvBuf, cb);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ( rc == VERR_BUFFER_OVERFLOW
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte && pThis->pBuf->cbSend < cb)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte INTNETIFSENDREQ SendReq;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SendReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SendReq.Hdr.cbReq = sizeof(SendReq);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SendReq.pSession = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SendReq.hIf = pThis->hIf;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pThis->pDrvIns->pDrvHlp->pfnSUPCallVMMR0Ex(pThis->pDrvIns, VMMR0_DO_INTNET_IF_SEND, &SendReq, sizeof(SendReq));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rc = drvIntNetRingWriteFrame(pThis->pBuf, &pThis->pBuf->Send, pvBuf, cb);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (RT_SUCCESS(rc))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte INTNETIFSENDREQ SendReq;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SendReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SendReq.Hdr.cbReq = sizeof(SendReq);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SendReq.pSession = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte SendReq.hIf = pThis->hIf;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rc = pThis->pDrvIns->pDrvHlp->pfnSUPCallVMMR0Ex(pThis->pDrvIns, VMMR0_DO_INTNET_IF_SEND, &SendReq, sizeof(SendReq));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte STAM_PROFILE_STOP(&pThis->StatTransmit, a);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte AssertRC(rc);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return rc;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Set promiscuous mode.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * This is called when the promiscuous mode is set. This means that there doesn't have
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * to be a mode change when it's called.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param pInterface Pointer to the interface structure containing the called function pointer.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param fPromiscuous Set if the adaptor is now in promiscuous mode. Clear if it is not.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @thread EMT
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic DECLCALLBACK(void) drvIntNetSetPromiscuousMode(PPDMINETWORKCONNECTOR pInterface, bool fPromiscuous)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte PDRVINTNET pThis = PDMINETWORKCONNECTOR_2_DRVINTNET(pInterface);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte INTNETIFSETPROMISCUOUSMODEREQ Req;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Req.Hdr.cbReq = sizeof(Req);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Req.pSession = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Req.hIf = pThis->hIf;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Req.fPromiscuous = fPromiscuous;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int rc = pThis->pDrvIns->pDrvHlp->pfnSUPCallVMMR0Ex(pThis->pDrvIns, VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE, &Req, sizeof(Req));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte LogFlow(("drvIntNetSetPromiscuousMode: fPromiscuous=%RTbool\n", fPromiscuous));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte AssertRC(rc);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Notification on link status changes.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param pInterface Pointer to the interface structure containing the called function pointer.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param enmLinkState The new link state.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @thread EMT
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic DECLCALLBACK(void) drvIntNetNotifyLinkChanged(PPDMINETWORKCONNECTOR pInterface, PDMNETWORKLINKSTATE enmLinkState)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte PDRVINTNET pThis = PDMINETWORKCONNECTOR_2_DRVINTNET(pInterface);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bool fLinkDown;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte switch (enmLinkState)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case PDMNETWORKLINKSTATE_DOWN:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case PDMNETWORKLINKSTATE_DOWN_RESUME:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fLinkDown = true;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte default:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte AssertMsgFailed(("enmLinkState=%d\n", enmLinkState));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case PDMNETWORKLINKSTATE_UP:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fLinkDown = false;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte LogFlow(("drvIntNetNotifyLinkChanged: enmLinkState=%d %d->%d\n", enmLinkState, pThis->fLinkDown, fLinkDown));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ASMAtomicXchgSize(&pThis->fLinkDown, fLinkDown);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Wait for space to become available up the driver/device chain.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @returns VINF_SUCCESS if space is available.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @returns VERR_STATE_CHANGED if the state changed.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @returns VBox status code on other errors.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param pThis Pointer to the instance data.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int drvIntNetAsyncIoWaitForSpace(PDRVINTNET pThis)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte LogFlow(("drvIntNetAsyncIoWaitForSpace:\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte STAM_PROFILE_ADV_STOP(&pThis->StatReceive, a);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int rc = pThis->pPort->pfnWaitReceiveAvail(pThis->pPort, RT_INDEFINITE_WAIT);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte STAM_PROFILE_ADV_START(&pThis->StatReceive, a);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte LogFlow(("drvIntNetAsyncIoWaitForSpace: returns %Vrc\n", rc));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return rc;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Executes async I/O (RUNNING mode).
*
* @returns VERR_STATE_CHANGED if the state changed.
* @returns Appropriate VBox status code (error) on fatal error.
* @param pThis The driver instance data.
*/
static int drvIntNetAsyncIoRun(PDRVINTNET pThis)
{
PPDMDRVINS pDrvIns = pThis->pDrvIns;
LogFlow(("drvIntNetAsyncIoRun: pThis=%p\n", pThis));
/*
* The running loop - processing received data and waiting for more to arrive.
*/
STAM_PROFILE_ADV_START(&pThis->StatReceive, a);
PINTNETBUF pBuf = pThis->pBuf;
PINTNETRINGBUF pRingBuf = &pThis->pBuf->Recv;
for (;;)
{
/*
* Process the receive buffer.
*/
while (INTNETRingGetReadable(pRingBuf) > 0)
{
/*
* Check the state and then inspect the packet.
*/
if (pThis->enmState != ASYNCSTATE_RUNNING)
{
STAM_PROFILE_ADV_STOP(&pThis->StatReceive, a);
LogFlow(("drvIntNetAsyncIoRun: returns VERR_STATE_CHANGED (state changed - #0)\n"));
return VERR_STATE_CHANGED;
}
PINTNETHDR pHdr = (PINTNETHDR)((uintptr_t)pBuf + pRingBuf->offRead);
Log2(("pHdr=%p offRead=%#x: %.8Rhxs\n", pHdr, pRingBuf->offRead, pHdr));
if ( pHdr->u16Type == INTNETHDR_TYPE_FRAME
&& !pThis->fLinkDown)
{
/*
* Check if there is room for the frame and pass it up.
*/
size_t cbFrame = pHdr->cbFrame;
int rc = pThis->pPort->pfnWaitReceiveAvail(pThis->pPort, 0);
if (rc == VINF_SUCCESS)
{
#ifdef LOG_ENABLED
uint64_t u64Now = RTTimeProgramNanoTS();
LogFlow(("drvIntNetAsyncIoRun: %-4d bytes at %llu ns deltas: r=%llu t=%llu\n",
cbFrame, u64Now, u64Now - pThis->u64LastReceiveTS, u64Now - pThis->u64LastTransferTS));
pThis->u64LastReceiveTS = u64Now;
Log2(("drvIntNetAsyncIoRun: cbFrame=%#x\n"
"%.*Vhxd\n",
cbFrame, cbFrame, INTNETHdrGetFramePtr(pHdr, pBuf)));
#endif
int rc = pThis->pPort->pfnReceive(pThis->pPort, INTNETHdrGetFramePtr(pHdr, pBuf), cbFrame);
AssertRC(rc);
/* skip to the next frame. */
INTNETRingSkipFrame(pBuf, pRingBuf);
}
else
{
/*
* Wait for sufficient space to become available and then retry.
*/
rc = drvIntNetAsyncIoWaitForSpace(pThis);
if (VBOX_FAILURE(rc))
{
STAM_PROFILE_ADV_STOP(&pThis->StatReceive, a);
LogFlow(("drvIntNetAsyncIoRun: returns %Vrc (wait-for-space)\n", rc));
return rc;
}
}
}
else
{
/*
* Link down or unknown frame - skip to the next frame.
*/
AssertMsg(pHdr->u16Type == INTNETHDR_TYPE_FRAME, ("Unknown frame type %RX16! offRead=%#x\n",
pHdr->u16Type, pRingBuf->offRead));
INTNETRingSkipFrame(pBuf, pRingBuf);
}
} /* while more received data */
/*
* Wait for data, checking the state before we block.
*/
if (pThis->enmState != ASYNCSTATE_RUNNING)
{
STAM_PROFILE_ADV_STOP(&pThis->StatReceive, a);
LogFlow(("drvIntNetAsyncIoRun: returns VINF_SUCCESS (state changed - #1)\n"));
return VERR_STATE_CHANGED;
}
INTNETIFWAITREQ WaitReq;
WaitReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
WaitReq.Hdr.cbReq = sizeof(WaitReq);
WaitReq.pSession = NULL;
WaitReq.hIf = pThis->hIf;
WaitReq.cMillies = 30000; /* 30s - don't wait forever, timeout now and then. */
STAM_PROFILE_ADV_STOP(&pThis->StatReceive, a);
int rc = pDrvIns->pDrvHlp->pfnSUPCallVMMR0Ex(pDrvIns, VMMR0_DO_INTNET_IF_WAIT, &WaitReq, sizeof(WaitReq));
if ( VBOX_FAILURE(rc)
&& rc != VERR_TIMEOUT
&& rc != VERR_INTERRUPTED)
{
LogFlow(("drvIntNetAsyncIoRun: returns %Vrc\n", rc));
return rc;
}
STAM_PROFILE_ADV_START(&pThis->StatReceive, a);
}
}
/**
* Asynchronous I/O thread for handling receive.
*
* @returns VINF_SUCCESS (ignored).
* @param ThreadSelf Thread handle.
* @param pvUser Pointer to a DRVINTNET structure.
*/
static DECLCALLBACK(int) drvIntNetAsyncIoThread(RTTHREAD ThreadSelf, void *pvUser)
{
PDRVINTNET pThis = (PDRVINTNET)pvUser;
LogFlow(("drvIntNetAsyncIoThread: pThis=%p\n", pThis));
STAM_PROFILE_ADV_START(&pThis->StatReceive, a);
/*
* The main loop - acting on state.
*/
for (;;)
{
ASYNCSTATE enmState = pThis->enmState;
switch (enmState)
{
case ASYNCSTATE_SUSPENDED:
{
int rc = RTSemEventWait(pThis->EventSuspended, 30000);
if ( VBOX_FAILURE(rc)
&& rc != VERR_TIMEOUT)
{
LogFlow(("drvIntNetAsyncIoThread: returns %Vrc\n", rc));
return rc;
}
break;
}
case ASYNCSTATE_RUNNING:
{
int rc = drvIntNetAsyncIoRun(pThis);
if ( rc != VERR_STATE_CHANGED
&& VBOX_FAILURE(rc))
{
LogFlow(("drvIntNetAsyncIoThread: returns %Vrc\n", rc));
return rc;
}
break;
}
default:
AssertMsgFailed(("Invalid state %d\n", enmState));
case ASYNCSTATE_TERMINATE:
LogFlow(("drvIntNetAsyncIoThread: returns VINF_SUCCESS\n"));
return VINF_SUCCESS;
}
}
}
/**
* Queries an interface to the driver.
*
* @returns Pointer to interface.
* @returns NULL if the interface was not supported by the driver.
* @param pInterface Pointer to this interface structure.
* @param enmInterface The requested interface identification.
* @thread Any thread.
*/
static DECLCALLBACK(void *) drvIntNetQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
{
PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
PDRVINTNET pThis = PDMINS2DATA(pDrvIns, PDRVINTNET);
switch (enmInterface)
{
case PDMINTERFACE_BASE:
return &pDrvIns->IBase;
case PDMINTERFACE_NETWORK_CONNECTOR:
return &pThis->INetworkConnector;
default:
return NULL;
}
}
/**
* Power Off notification.
*
* @param pDrvIns The driver instance.
*/
static DECLCALLBACK(void) drvIntNetPowerOff(PPDMDRVINS pDrvIns)
{
LogFlow(("drvIntNetPowerOff\n"));
PDRVINTNET pThis = PDMINS2DATA(pDrvIns, PDRVINTNET);
if (!pThis->fActivateEarlyDeactivateLate)
ASMAtomicXchgSize(&pThis->enmState, ASYNCSTATE_SUSPENDED);
}
/**
* Resume notification.
*
* @param pDrvIns The driver instance.
*/
static DECLCALLBACK(void) drvIntNetResume(PPDMDRVINS pDrvIns)
{
LogFlow(("drvIntNetPowerResume\n"));
PDRVINTNET pThis = PDMINS2DATA(pDrvIns, PDRVINTNET);
if (!pThis->fActivateEarlyDeactivateLate)
{
ASMAtomicXchgSize(&pThis->enmState, ASYNCSTATE_RUNNING);
RTSemEventSignal(pThis->EventSuspended);
}
}
/**
* Suspend notification.
*
* @param pDrvIns The driver instance.
*/
static DECLCALLBACK(void) drvIntNetSuspend(PPDMDRVINS pDrvIns)
{
LogFlow(("drvIntNetPowerSuspend\n"));
PDRVINTNET pThis = PDMINS2DATA(pDrvIns, PDRVINTNET);
if (!pThis->fActivateEarlyDeactivateLate)
ASMAtomicXchgSize(&pThis->enmState, ASYNCSTATE_SUSPENDED);
}
/**
* Power On notification.
*
* @param pDrvIns The driver instance.
*/
static DECLCALLBACK(void) drvIntNetPowerOn(PPDMDRVINS pDrvIns)
{
LogFlow(("drvIntNetPowerOn\n"));
PDRVINTNET pThis = PDMINS2DATA(pDrvIns, PDRVINTNET);
if (!pThis->fActivateEarlyDeactivateLate)
{
ASMAtomicXchgSize(&pThis->enmState, ASYNCSTATE_RUNNING);
RTSemEventSignal(pThis->EventSuspended);
}
}
/**
* Destruct a driver instance.
*
* Most VM resources are freed by the VM. This callback is provided so that any non-VM
* resources can be freed correctly.
*
* @param pDrvIns The driver instance data.
*/
static DECLCALLBACK(void) drvIntNetDestruct(PPDMDRVINS pDrvIns)
{
LogFlow(("drvIntNetDestruct\n"));
PDRVINTNET pThis = PDMINS2DATA(pDrvIns, PDRVINTNET);
/*
* Indicate to the thread that it's time to quit.
*/
ASMAtomicXchgSize(&pThis->enmState, ASYNCSTATE_TERMINATE);
ASMAtomicXchgSize(&pThis->fLinkDown, true);
RTSEMEVENT EventSuspended = pThis->EventSuspended;
pThis->EventSuspended = NIL_RTSEMEVENT;
/*
* Close the interface
*/
if (pThis->hIf != INTNET_HANDLE_INVALID)
{
INTNETIFCLOSEREQ CloseReq;
CloseReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
CloseReq.Hdr.cbReq = sizeof(CloseReq);
CloseReq.pSession = NULL;
CloseReq.hIf = pThis->hIf;
pThis->hIf = INTNET_HANDLE_INVALID;
int rc = pDrvIns->pDrvHlp->pfnSUPCallVMMR0Ex(pDrvIns, VMMR0_DO_INTNET_IF_CLOSE, &CloseReq, sizeof(CloseReq));
AssertRC(rc);
}
/*
* Wait for the thread to terminate.
*/
if (pThis->Thread != NIL_RTTHREAD)
{
if (EventSuspended != NIL_RTSEMEVENT)
RTSemEventSignal(EventSuspended);
int rc = RTThreadWait(pThis->Thread, 5000, NULL);
AssertRC(rc);
pThis->Thread = NIL_RTTHREAD;
}
/*
* Destroy the semaphores.
*/
if (EventSuspended != NIL_RTSEMEVENT)
RTSemEventDestroy(EventSuspended);
}
/**
* Construct a TAP network transport driver instance.
*
* @returns VBox status.
* @param pDrvIns The driver instance data.
* If the registration structure is needed, pDrvIns->pDrvReg points to it.
* @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
* of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
* iInstance it's expected to be used a bit in this function.
*/
static DECLCALLBACK(int) drvIntNetConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
{
PDRVINTNET pThis = PDMINS2DATA(pDrvIns, PDRVINTNET);
/*
* Init the static parts.
*/
pThis->pDrvIns = pDrvIns;
pThis->hIf = INTNET_HANDLE_INVALID;
pThis->Thread = NIL_RTTHREAD;
pThis->EventSuspended = NIL_RTSEMEVENT;
pThis->enmState = ASYNCSTATE_SUSPENDED;
pThis->fActivateEarlyDeactivateLate = false;
/* IBase */
pDrvIns->IBase.pfnQueryInterface = drvIntNetQueryInterface;
/* INetwork */
pThis->INetworkConnector.pfnSend = drvIntNetSend;
pThis->INetworkConnector.pfnSetPromiscuousMode = drvIntNetSetPromiscuousMode;
pThis->INetworkConnector.pfnNotifyLinkChanged = drvIntNetNotifyLinkChanged;
/*
* Validate the config.
*/
if (!CFGMR3AreValuesValid(pCfgHandle, "Network\0ReceiveBufferSize\0SendBufferSize\0RestrictAccess\0IsService\0"))
return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
/*
* Check that no-one is attached to us.
*/
int rc = pDrvIns->pDrvHlp->pfnAttach(pDrvIns, NULL);
if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
{
AssertMsgFailed(("Configuration error: Cannot attach drivers to the TAP driver!\n"));
return VERR_PDM_DRVINS_NO_ATTACH;
}
/*
* Query the network port interface.
*/
pThis->pPort = (PPDMINETWORKPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_NETWORK_PORT);
if (!pThis->pPort)
{
AssertMsgFailed(("Configuration error: the above device/driver didn't export the network port interface!\n"));
return VERR_PDM_MISSING_INTERFACE_ABOVE;
}
/*
* Read the configuration.
*/
INTNETOPENREQ OpenReq;
memset(&OpenReq, 0, sizeof(OpenReq));
OpenReq.Hdr.cbReq = sizeof(OpenReq);
OpenReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
OpenReq.pSession = NULL;
/** @cfgm{Network, string}
* The name of the internal network to connect to.
*/
rc = CFGMR3QueryString(pCfgHandle, "Network", OpenReq.szNetwork, sizeof(OpenReq.szNetwork));
if (VBOX_FAILURE(rc))
return PDMDRV_SET_ERROR(pDrvIns, rc,
N_("Configuration error: Failed to get the \"Network\" value"));
strcpy(pThis->szNetwork, OpenReq.szNetwork);
/** @cfgm{TrunkType, uint32_t, kIntNetTrunkType_None}
* The trunk connection type see INTNETTRUNKTYPE.
*/
uint32_t u32TrunkType;
rc = CFGMR3QueryU32(pCfgHandle, "TrunkType", &u32TrunkType);
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
u32TrunkType = kIntNetTrunkType_None;
else if (VBOX_FAILURE(rc))
return PDMDRV_SET_ERROR(pDrvIns, rc,
N_("Configuration error: Failed to get the \"TrunkType\" value"));
OpenReq.enmTrunkType = (INTNETTRUNKTYPE)u32TrunkType;
/** @cfgm{Trunk, string, ""}
* The name of the trunk connection.
*/
rc = CFGMR3QueryString(pCfgHandle, "Trunk", OpenReq.szTrunk, sizeof(OpenReq.szTrunk));
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
OpenReq.szTrunk[0] = '\0';
else if (VBOX_FAILURE(rc))
return PDMDRV_SET_ERROR(pDrvIns, rc,
N_("Configuration error: Failed to get the \"Trunk\" value"));
/** @cfgm{RestrictAccess, boolean, true}
* Whether to restrict the access to the network or if it should be public. Everyone on
* the computer can connect to a public network. Don't change this.
*/
bool fRestrictAccess;
rc = CFGMR3QueryBool(pCfgHandle, "RestrictAccess", &fRestrictAccess);
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
fRestrictAccess = true;
else if (VBOX_FAILURE(rc))
return PDMDRV_SET_ERROR(pDrvIns, rc,
N_("Configuration error: Failed to get the \"RestrictAccess\" value"));
OpenReq.fFlags = fRestrictAccess ? 0 : INTNET_OPEN_FLAGS_PUBLIC;
/** @cfgm{ReceiveBufferSize, uint32_t, 256 KB}
* The size of the receive buffer.
*/
rc = CFGMR3QueryU32(pCfgHandle, "ReceiveBufferSize", &OpenReq.cbRecv);
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
OpenReq.cbRecv = _256K;
else if (VBOX_FAILURE(rc))
return PDMDRV_SET_ERROR(pDrvIns, rc,
N_("Configuration error: Failed to get the \"ReceiveBufferSize\" value"));
/** @cfgm{SendBufferSize, uint32_t, 4 KB}
* The size of the send (transmit) buffer.
*/
rc = CFGMR3QueryU32(pCfgHandle, "SendBufferSize", &OpenReq.cbSend);
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
OpenReq.cbSend = _4K;
else if (VBOX_FAILURE(rc))
return PDMDRV_SET_ERROR(pDrvIns, rc,
N_("Configuration error: Failed to get the \"SendBufferSize\" value"));
if (OpenReq.cbSend < 16)
return PDMDRV_SET_ERROR(pDrvIns, rc,
N_("Configuration error: The \"SendBufferSize\" value is too small"));
if (OpenReq.cbSend < 1536*2 + 4)
LogRel(("DrvIntNet: Warning! SendBufferSize=%u, Recommended minimum size %u butes.\n", OpenReq.cbSend, 1536*2 + 4));
/** @cfgm{IsService, boolean, true}
* This alterns the way the thread is suspended and resumed. When it's being used by
* a service such as LWIP/iSCSI it shouldn't suspend immediately like for a NIC.
*/
rc = CFGMR3QueryBool(pCfgHandle, "IsService", &pThis->fActivateEarlyDeactivateLate);
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
pThis->fActivateEarlyDeactivateLate = false;
else if (VBOX_FAILURE(rc))
return PDMDRV_SET_ERROR(pDrvIns, rc,
N_("Configuration error: Failed to get the \"IsService\" value"));
LogRel(("IntNet#%u: szNetwork={%s} enmTrunkType=%d szTrunk={%s} fFlags=%#x cbRecv=%u cbSend=%u\n",
pDrvIns->iInstance, OpenReq.szNetwork, OpenReq.enmTrunkType, OpenReq.szTrunk, OpenReq.fFlags,
OpenReq.cbRecv, OpenReq.cbSend));
/*
* Create the event semaphores
*/
rc = RTSemEventCreate(&pThis->EventSuspended);
if (VBOX_FAILURE(rc))
return rc;
/*
* Create the interface.
*/
OpenReq.hIf = INTNET_HANDLE_INVALID;
rc = pDrvIns->pDrvHlp->pfnSUPCallVMMR0Ex(pDrvIns, VMMR0_DO_INTNET_OPEN, &OpenReq, sizeof(OpenReq));
if (VBOX_FAILURE(rc))
return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
N_("Failed to open/create the internal network '%s'"), pThis->szNetwork);
AssertRelease(OpenReq.hIf != INTNET_HANDLE_INVALID);
pThis->hIf = OpenReq.hIf;
Log(("IntNet%d: hIf=%RX32 '%s'\n", pDrvIns->iInstance, pThis->hIf, pThis->szNetwork));
/*
* Get default buffer.
*/
INTNETIFGETRING3BUFFERREQ GetRing3BufferReq;
GetRing3BufferReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
GetRing3BufferReq.Hdr.cbReq = sizeof(GetRing3BufferReq);
GetRing3BufferReq.pSession = NULL;
GetRing3BufferReq.hIf = pThis->hIf;
GetRing3BufferReq.pRing3Buf = NULL;
rc = pDrvIns->pDrvHlp->pfnSUPCallVMMR0Ex(pDrvIns, VMMR0_DO_INTNET_IF_GET_RING3_BUFFER, &GetRing3BufferReq, sizeof(GetRing3BufferReq));
if (VBOX_FAILURE(rc))
return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
N_("Failed to get ring-3 buffer for the newly created interface to '%s'"), pThis->szNetwork);
AssertRelease(VALID_PTR(GetRing3BufferReq.pRing3Buf));
pThis->pBuf = GetRing3BufferReq.pRing3Buf;
/*
* Create the async I/O thread.
* Note! Using a PDM thread here doesn't fit with the IsService=true operation.
*/
rc = RTThreadCreate(&pThis->Thread, drvIntNetAsyncIoThread, pThis, _128K, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "INTNET");
if (VBOX_FAILURE(rc))
{
AssertRC(rc);
return rc;
}
char szStatName[64];
RTStrPrintf(szStatName, sizeof(szStatName), "/Net/IntNet%d/Bytes/Received", pDrvIns->iInstance);
pDrvIns->pDrvHlp->pfnSTAMRegister(pDrvIns, &pThis->pBuf->cbStatRecv, STAMTYPE_COUNTER, szStatName, STAMUNIT_BYTES, "Number of received bytes.");
RTStrPrintf(szStatName, sizeof(szStatName), "/Net/IntNet%d/Bytes/Sent", pDrvIns->iInstance);
pDrvIns->pDrvHlp->pfnSTAMRegister(pDrvIns, &pThis->pBuf->cbStatSend, STAMTYPE_COUNTER, szStatName, STAMUNIT_BYTES, "Number of sent bytes.");
RTStrPrintf(szStatName, sizeof(szStatName), "/Net/IntNet%d/Packets/Received", pDrvIns->iInstance);
pDrvIns->pDrvHlp->pfnSTAMRegister(pDrvIns, &pThis->pBuf->cStatRecvs, STAMTYPE_COUNTER, szStatName, STAMUNIT_OCCURENCES, "Number of received packets.");
RTStrPrintf(szStatName, sizeof(szStatName), "/Net/IntNet%d/Packets/Sent", pDrvIns->iInstance);
pDrvIns->pDrvHlp->pfnSTAMRegister(pDrvIns, &pThis->pBuf->cStatSends, STAMTYPE_COUNTER, szStatName, STAMUNIT_OCCURENCES, "Number of sent packets.");
RTStrPrintf(szStatName, sizeof(szStatName), "/Net/IntNet%d/Packets/Lost", pDrvIns->iInstance);
pDrvIns->pDrvHlp->pfnSTAMRegister(pDrvIns, &pThis->pBuf->cStatLost, STAMTYPE_COUNTER, szStatName, STAMUNIT_OCCURENCES, "Number of lost packets.");
RTStrPrintf(szStatName, sizeof(szStatName), "/Net/IntNet%d/YieldOk", pDrvIns->iInstance);
pDrvIns->pDrvHlp->pfnSTAMRegister(pDrvIns, &pThis->pBuf->cStatYieldsOk, STAMTYPE_COUNTER, szStatName, STAMUNIT_OCCURENCES, "Number of times yielding fixed an overflow.");
RTStrPrintf(szStatName, sizeof(szStatName), "/Net/IntNet%d/YieldNok", pDrvIns->iInstance);
pDrvIns->pDrvHlp->pfnSTAMRegister(pDrvIns, &pThis->pBuf->cStatYieldsNok, STAMTYPE_COUNTER, szStatName, STAMUNIT_OCCURENCES, "Number of times yielding didn't help fix an overflow.");
#ifdef VBOX_WITH_STATISTICS
RTStrPrintf(szStatName, sizeof(szStatName), "/Net/IntNet%d/Receive", pDrvIns->iInstance);
pDrvIns->pDrvHlp->pfnSTAMRegister(pDrvIns, &pThis->StatReceive, STAMTYPE_PROFILE, szStatName, STAMUNIT_TICKS_PER_CALL, "Profiling packet receive runs.");
RTStrPrintf(szStatName, sizeof(szStatName), "/Net/IntNet%d/Transmit", pDrvIns->iInstance);
pDrvIns->pDrvHlp->pfnSTAMRegister(pDrvIns, &pThis->StatTransmit, STAMTYPE_PROFILE, szStatName, STAMUNIT_TICKS_PER_CALL, "Profiling packet transmit runs.");
#endif
/*
* Activate data transmission as early as possible
*/
if (pThis->fActivateEarlyDeactivateLate)
{
ASMAtomicXchgSize(&pThis->enmState, ASYNCSTATE_RUNNING);
RTSemEventSignal(pThis->EventSuspended);
}
return rc;
}
/**
* Internal networking transport driver registration record.
*/
const PDMDRVREG g_DrvIntNet =
{
/* u32Version */
PDM_DRVREG_VERSION,
/* szDriverName */
"IntNet",
/* pszDescription */
"Internal Networking Transport Driver",
/* fFlags */
PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
/* fClass. */
PDM_DRVREG_CLASS_NETWORK,
/* cMaxInstances */
~0,
/* cbInstance */
sizeof(DRVINTNET),
/* pfnConstruct */
drvIntNetConstruct,
/* pfnDestruct */
drvIntNetDestruct,
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
drvIntNetPowerOn,
/* pfnReset */
NULL,
/* pfnSuspend */
drvIntNetSuspend,
/* pfnResume */
drvIntNetResume,
/* pfnDetach */
NULL,
/* pfnPowerOff */
drvIntNetPowerOff
};