DevVirtioNet.cpp revision d99197f8edcfa672509d9418fa6441c0c66af33c
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster/* $Id$ */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster/** @file
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * DevVirtioNet - Virtio Network Device
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster/*
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * Copyright (C) 2009 Sun Microsystems, Inc.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster *
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * This file is part of VirtualBox Open Source Edition (OSE), as
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * available from http://www.virtualbox.org. This file is free software;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * you can redistribute it and/or modify it under the terms of the GNU
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * General Public License (GPL) as published by the Free Software
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * Foundation, in version 2 as it comes in the "COPYING" file of the
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster *
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * Clara, CA 95054 USA or visit http://www.sun.com if you need
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * additional information or have any questions.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define LOG_GROUP LOG_GROUP_DEV_VIRTIO_NET
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_GC_SUPPORT
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#include <VBox/pdmdev.h>
c070f56622b9a37191894cf0937d85e943ef6033Peter Major#include <iprt/semaphore.h>
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major#ifdef IN_RING3
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster# include <iprt/mem.h>
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster# include <iprt/uuid.h>
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#endif /* IN_RING3 */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#include "../Builtins.h"
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#include "../VirtIO/Virtio.h"
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#ifndef VBOX_DEVICE_STRUCT_TESTCASE
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define INSTANCE(pState) pState->VPCI.szInstance
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define IFACE_TO_STATE(pIface, ifaceName) \
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster ((VNETSTATE *)((char*)pIface - RT_OFFSETOF(VNETSTATE, ifaceName)))
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define STATUS pState->config.uStatus
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#ifdef IN_RING3
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_PCI_SUBSYSTEM_ID 1 + VIRTIO_NET_ID
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_PCI_CLASS 0x0200
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major#define VNET_N_QUEUES 3
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_NAME_FMT "VNet%d"
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#if 0
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster/* Virtio Block Device */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_PCI_SUBSYSTEM_ID 1 + VIRTIO_BLK_ID
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_PCI_CLASS 0x0180
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_N_QUEUES 2
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_NAME_FMT "VBlk%d"
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#endif
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#endif /* IN_RING3 */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster/* Forward declarations ******************************************************/
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan FosterRT_C_DECLS_BEGIN
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan FosterPDMBOTHCBDECL(int) vnetIOPortIn (PPDMDEVINS pDevIns, void *pvUser, RTIOPORT port, uint32_t *pu32, unsigned cb);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan FosterPDMBOTHCBDECL(int) vnetIOPortOut(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT port, uint32_t u32, unsigned cb);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan FosterRT_C_DECLS_END
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major#endif /* VBOX_DEVICE_STRUCT_TESTCASE */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
c070f56622b9a37191894cf0937d85e943ef6033Peter Major#define VNET_TX_DELAY 150 /* 150 microseconds */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_MAX_FRAME_SIZE 65536 // TODO: Is it the right limit?
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_MAC_FILTER_LEN 32
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_MAX_VID (1 << 12)
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster/* Virtio net features */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_F_CSUM 0x00000001 /* Host handles pkts w/ partial csum */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_F_GUEST_CSUM 0x00000002 /* Guest handles pkts w/ partial csum */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_F_MAC 0x00000020 /* Host has given MAC address. */
c070f56622b9a37191894cf0937d85e943ef6033Peter Major#define VNET_F_GSO 0x00000040 /* Host handles pkts w/ any GSO type */
c070f56622b9a37191894cf0937d85e943ef6033Peter Major#define VNET_F_GUEST_TSO4 0x00000080 /* Guest can handle TSOv4 in. */
c070f56622b9a37191894cf0937d85e943ef6033Peter Major#define VNET_F_GUEST_TSO6 0x00000100 /* Guest can handle TSOv6 in. */
c070f56622b9a37191894cf0937d85e943ef6033Peter Major#define VNET_F_GUEST_ECN 0x00000200 /* Guest can handle TSO[6] w/ ECN in. */
c070f56622b9a37191894cf0937d85e943ef6033Peter Major#define VNET_F_GUEST_UFO 0x00000400 /* Guest can handle UFO in. */
c070f56622b9a37191894cf0937d85e943ef6033Peter Major#define VNET_F_HOST_TSO4 0x00000800 /* Host can handle TSOv4 in. */
c070f56622b9a37191894cf0937d85e943ef6033Peter Major#define VNET_F_HOST_TSO6 0x00001000 /* Host can handle TSOv6 in. */
c070f56622b9a37191894cf0937d85e943ef6033Peter Major#define VNET_F_HOST_ECN 0x00002000 /* Host can handle TSO[6] w/ ECN in. */
c070f56622b9a37191894cf0937d85e943ef6033Peter Major#define VNET_F_HOST_UFO 0x00004000 /* Host can handle UFO in. */
c070f56622b9a37191894cf0937d85e943ef6033Peter Major#define VNET_F_MRG_RXBUF 0x00008000 /* Host can merge receive buffers. */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_F_STATUS 0x00010000 /* virtio_net_config.status available */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_F_CTRL_VQ 0x00020000 /* Control channel available */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_F_CTRL_RX 0x00040000 /* Control channel RX mode support */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_F_CTRL_VLAN 0x00080000 /* Control channel VLAN filtering */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_S_LINK_UP 1
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#ifdef _MSC_VER
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Fosterstruct VNetPCIConfig
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#else /* !_MSC_VER */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Fosterstruct __attribute__ ((__packed__)) VNetPCIConfig
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#endif /* !_MSC_VER */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster{
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster RTMAC mac;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster uint16_t uStatus;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster};
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan FosterAssertCompileMemberOffset(struct VNetPCIConfig, uStatus, 6);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster/**
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * Device state structure. Holds the current state of device.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster *
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * @extends VPCISTATE
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * @implements PDMINETWORKPORT
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * @implements PDMINETWORKCONFIG
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Fosterstruct VNetState_st
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster{
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major /* VPCISTATE must be the first member! */
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major VPCISTATE VPCI;
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major// PDMCRITSECT csRx; /**< Protects RX queue. */
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major PDMINETWORKPORT INetworkPort;
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major PDMINETWORKCONFIG INetworkConfig;
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major R3PTRTYPE(PPDMIBASE) pDrvBase; /**< Attached network driver. */
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major R3PTRTYPE(PPDMINETWORKCONNECTOR) pDrv; /**< Connector of attached network driver. */
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major R3PTRTYPE(PPDMQUEUE) pCanRxQueueR3; /**< Rx wakeup signaller - R3. */
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major R0PTRTYPE(PPDMQUEUE) pCanRxQueueR0; /**< Rx wakeup signaller - R0. */
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major RCPTRTYPE(PPDMQUEUE) pCanRxQueueRC; /**< Rx wakeup signaller - RC. */
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major#if HC_ARCH_BITS == 64
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major uint32_t padding;
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major#endif
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major /** transmit buffer */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster R3PTRTYPE(uint8_t*) pTxBuf;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /**< Link Up(/Restore) Timer. */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster PTMTIMERR3 pLinkUpTimer;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#ifdef VNET_TX_DELAY
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /**< Transmit Delay Timer - R3. */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster PTMTIMERR3 pTxTimerR3;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /**< Transmit Delay Timer - R0. */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster PTMTIMERR0 pTxTimerR0;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /**< Transmit Delay Timer - GC. */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster PTMTIMERRC pTxTimerRC;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#if HC_ARCH_BITS == 64
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster uint32_t padding2;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#endif
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster uint32_t u32i;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster uint32_t u32AvgDiff;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster uint32_t u32MinDiff;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster uint32_t u32MaxDiff;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster uint64_t u64NanoTS;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#endif /* VNET_TX_DELAY */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /** Indicates transmission in progress -- only one thread is allowed. */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster uint32_t uIsTransmitting;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /** PCI config area holding MAC address as well as TBD. */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster struct VNetPCIConfig config;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /** MAC address obtained from the configuration. */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster RTMAC macConfigured;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /** True if physical cable is attached in configuration. */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster bool fCableConnected;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /** Number of packet being sent/received to show in debug log. */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster uint32_t u32PktNo;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /** N/A: */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster bool volatile fMaybeOutOfSpace;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /** Promiscuous mode -- RX filter accepts all packets. */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster bool fPromiscuous;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /** AllMulti mode -- RX filter accepts all multicast packets. */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster bool fAllMulti;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /** The number of actually used slots in aMacTable. */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster uint32_t nMacFilterEntries;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /** Array of MAC addresses accepted by RX filter. */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster RTMAC aMacFilter[VNET_MAC_FILTER_LEN];
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /** Bit array of VLAN filter, one bit per VLAN ID. */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster uint8_t aVlanFilter[VNET_MAX_VID / sizeof(uint8_t)];
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#if HC_ARCH_BITS != 64
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster uint32_t padding3;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#endif
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster R3PTRTYPE(PVQUEUE) pRxQueue;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster R3PTRTYPE(PVQUEUE) pTxQueue;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster R3PTRTYPE(PVQUEUE) pCtlQueue;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /* Receive-blocking-related fields ***************************************/
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /** EMT: Gets signalled when more RX descriptors become available. */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster RTSEMEVENT hEventMoreRxDescAvail;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /* Statistic fields ******************************************************/
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster STAMCOUNTER StatReceiveBytes;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster STAMCOUNTER StatTransmitBytes;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#if defined(VBOX_WITH_STATISTICS)
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster STAMPROFILE StatReceive;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster STAMPROFILE StatReceiveStore;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster STAMPROFILEADV StatTransmit;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster STAMPROFILE StatTransmitSend;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster STAMPROFILE StatRxOverflow;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster STAMCOUNTER StatRxOverflowWakeup;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#endif /* VBOX_WITH_STATISTICS */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster};
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Fostertypedef struct VNetState_st VNETSTATE;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Fostertypedef VNETSTATE *PVNETSTATE;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#ifndef VBOX_DEVICE_STRUCT_TESTCASE
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNETHDR_GSO_NONE 0
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Fosterstruct VNetHdr
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster{
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster uint8_t u8Flags;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster uint8_t u8GSOType;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster uint16_t u16HdrLen;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster uint16_t u16GSOSize;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster uint16_t u16CSumStart;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster uint16_t u16CSumOffset;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster};
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Fostertypedef struct VNetHdr VNETHDR;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Fostertypedef VNETHDR *PVNETHDR;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan FosterAssertCompileSize(VNETHDR, 10);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan FosterAssertCompileMemberOffset(VNETSTATE, VPCI, 0);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_OK 0
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_ERROR 1
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Fostertypedef uint8_t VNETCTLACK;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_CTRL_CLS_RX_MODE 0
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_CTRL_CMD_RX_MODE_PROMISC 0
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_CTRL_CMD_RX_MODE_ALLMULTI 1
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_CTRL_CLS_MAC 1
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_CTRL_CMD_MAC_TABLE_SET 0
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_CTRL_CLS_VLAN 2
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_CTRL_CMD_VLAN_ADD 0
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#define VNET_CTRL_CMD_VLAN_DEL 1
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Fosterstruct VNetCtlHdr
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster{
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster uint8_t u8Class;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster uint8_t u8Command;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster};
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Fostertypedef struct VNetCtlHdr VNETCTLHDR;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Fostertypedef VNETCTLHDR *PVNETCTLHDR;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan FosterAssertCompileSize(VNETCTLHDR, 2);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan FosterDECLINLINE(int) vnetCsEnter(PVNETSTATE pState, int rcBusy)
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster{
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster return vpciCsEnter(&pState->VPCI, rcBusy);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster}
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan FosterDECLINLINE(void) vnetCsLeave(PVNETSTATE pState)
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster{
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster vpciCsLeave(&pState->VPCI);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster}
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter MajorDECLINLINE(int) vnetCsRxEnter(PVNETSTATE pState, int rcBusy)
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major{
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major // STAM_PROFILE_START(&pState->CTXSUFF(StatCsRx), a);
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major // int rc = PDMCritSectEnter(&pState->csRx, rcBusy);
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major // STAM_PROFILE_STOP(&pState->CTXSUFF(StatCsRx), a);
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major // return rc;
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major return VINF_SUCCESS;
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major}
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter MajorDECLINLINE(void) vnetCsRxLeave(PVNETSTATE pState)
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major{
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major // PDMCritSectLeave(&pState->csRx);
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster}
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major/**
31e5c48be26b4540b5388450d642bbc38c0afcc0Peter Major * Dump a packet to debug log.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster *
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * @param pState The device state structure.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * @param cpPacket The packet.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * @param cb The size of the packet.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * @param cszText A string denoting direction of packet transfer.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster */
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan FosterDECLINLINE(void) vnetPacketDump(PVNETSTATE pState, const uint8_t *cpPacket, size_t cb, const char *cszText)
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster{
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#ifdef DEBUG
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster Log(("%s %s packet #%d (%d bytes):\n",
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster INSTANCE(pState), cszText, ++pState->u32PktNo, cb));
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster //Log3(("%.*Rhxd\n", cb, cpPacket));
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster#endif
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster}
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan FosterPDMBOTHCBDECL(uint32_t) vnetGetHostFeatures(void *pvState)
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster{
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster /* We support:
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * - Host-provided MAC address
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * - Link status reporting in config space
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * - Control queue
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * - RX mode setting
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * - MAC filter table
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster * - VLAN filter
*/
return VNET_F_MAC
| VNET_F_STATUS
| VNET_F_CTRL_VQ
| VNET_F_CTRL_RX
| VNET_F_CTRL_VLAN;
}
PDMBOTHCBDECL(uint32_t) vnetGetHostMinimalFeatures(void *pvState)
{
return VNET_F_MAC;
}
PDMBOTHCBDECL(void) vnetSetHostFeatures(void *pvState, uint32_t uFeatures)
{
// TODO: Nothing to do here yet
VNETSTATE *pState = (VNETSTATE *)pvState;
LogFlow(("%s vnetSetHostFeatures: uFeatures=%x\n", INSTANCE(pState), uFeatures));
}
PDMBOTHCBDECL(int) vnetGetConfig(void *pvState, uint32_t port, uint32_t cb, void *data)
{
VNETSTATE *pState = (VNETSTATE *)pvState;
if (port + cb > sizeof(struct VNetPCIConfig))
{
Log(("%s vnetGetConfig: Read beyond the config structure is attempted (port=%RTiop cb=%x).\n", INSTANCE(pState), port, cb));
return VERR_IOM_IOPORT_UNUSED;
}
memcpy(data, ((uint8_t*)&pState->config) + port, cb);
return VINF_SUCCESS;
}
PDMBOTHCBDECL(int) vnetSetConfig(void *pvState, uint32_t port, uint32_t cb, void *data)
{
VNETSTATE *pState = (VNETSTATE *)pvState;
if (port + cb > sizeof(struct VNetPCIConfig))
{
Log(("%s vnetGetConfig: Write beyond the config structure is attempted (port=%RTiop cb=%x).\n", INSTANCE(pState), port, cb));
if (port < sizeof(struct VNetPCIConfig))
memcpy(((uint8_t*)&pState->config) + port, data,
sizeof(struct VNetPCIConfig) - port);
return VINF_SUCCESS;
}
memcpy(((uint8_t*)&pState->config) + port, data, cb);
return VINF_SUCCESS;
}
/**
* Hardware reset. Revert all registers to initial values.
*
* @param pState The device state structure.
*/
PDMBOTHCBDECL(void) vnetReset(void *pvState)
{
VNETSTATE *pState = (VNETSTATE*)pvState;
Log(("%s Reset triggered\n", INSTANCE(pState)));
int rc = vnetCsRxEnter(pState, VERR_SEM_BUSY);
if (RT_UNLIKELY(rc != VINF_SUCCESS))
{
LogRel(("vnetReset failed to enter RX critical section!\n"));
return;
}
vpciReset(&pState->VPCI);
vnetCsRxLeave(pState);
// TODO: Implement reset
if (pState->fCableConnected)
STATUS = VNET_S_LINK_UP;
else
STATUS = 0;
/*
* By default we pass all packets up since the older guests cannot control
* virtio mode.
*/
pState->fPromiscuous = true;
pState->fAllMulti = false;
pState->nMacFilterEntries = 0;
memset(pState->aMacFilter, 0, VNET_MAC_FILTER_LEN * sizeof(RTMAC));
memset(pState->aVlanFilter, 0, sizeof(pState->aVlanFilter));
pState->uIsTransmitting = 0;
}
#ifdef IN_RING3
/**
* Wakeup the RX thread.
*/
static void vnetWakeupReceive(PPDMDEVINS pDevIns)
{
VNETSTATE *pState = PDMINS_2_DATA(pDevIns, VNETSTATE *);
if ( pState->fMaybeOutOfSpace
&& pState->hEventMoreRxDescAvail != NIL_RTSEMEVENT)
{
STAM_COUNTER_INC(&pState->StatRxOverflowWakeup);
Log(("%s Waking up Out-of-RX-space semaphore\n", INSTANCE(pState)));
RTSemEventSignal(pState->hEventMoreRxDescAvail);
}
}
/**
* Link Up Timer handler.
*
* @param pDevIns Pointer to device instance structure.
* @param pTimer Pointer to the timer.
* @param pvUser NULL.
* @thread EMT
*/
static DECLCALLBACK(void) vnetLinkUpTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
{
VNETSTATE *pState = (VNETSTATE *)pvUser;
int rc = vnetCsEnter(pState, VERR_SEM_BUSY);
if (RT_UNLIKELY(rc != VINF_SUCCESS))
return;
STATUS |= VNET_S_LINK_UP;
vpciRaiseInterrupt(&pState->VPCI, VERR_SEM_BUSY, VPCI_ISR_CONFIG);
vnetWakeupReceive(pDevIns);
vnetCsLeave(pState);
}
/**
* Handler for the wakeup signaller queue.
*/
static DECLCALLBACK(bool) vnetCanRxQueueConsumer(PPDMDEVINS pDevIns, PPDMQUEUEITEMCORE pItem)
{
vnetWakeupReceive(pDevIns);
return true;
}
#endif /* IN_RING3 */
/**
* This function is called when the driver becomes ready.
*
* @param pState The device state structure.
*/
PDMBOTHCBDECL(void) vnetReady(void *pvState)
{
VNETSTATE *pState = (VNETSTATE*)pvState;
Log(("%s Driver became ready, waking up RX thread...\n", INSTANCE(pState)));
#ifdef IN_RING3
vnetWakeupReceive(pState->VPCI.CTX_SUFF(pDevIns));
#else
PPDMQUEUEITEMCORE pItem = PDMQueueAlloc(pState->CTX_SUFF(pCanRxQueue));
if (pItem)
PDMQueueInsert(pState->CTX_SUFF(pCanRxQueue), pItem);
#endif
}
/**
* Port I/O Handler for IN operations.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser Pointer to the device state structure.
* @param port Port number used for the IN operation.
* @param pu32 Where to store the result.
* @param cb Number of bytes read.
* @thread EMT
*/
PDMBOTHCBDECL(int) vnetIOPortIn(PPDMDEVINS pDevIns, void *pvUser,
RTIOPORT port, uint32_t *pu32, unsigned cb)
{
return vpciIOPortIn(pDevIns, pvUser, port, pu32, cb,
vnetGetHostFeatures,
vnetGetConfig);
}
/**
* Port I/O Handler for OUT operations.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser User argument.
* @param Port Port number used for the IN operation.
* @param u32 The value to output.
* @param cb The value size in bytes.
* @thread EMT
*/
PDMBOTHCBDECL(int) vnetIOPortOut(PPDMDEVINS pDevIns, void *pvUser,
RTIOPORT port, uint32_t u32, unsigned cb)
{
return vpciIOPortOut(pDevIns, pvUser, port, u32, cb,
vnetGetHostMinimalFeatures,
vnetGetHostFeatures,
vnetSetHostFeatures,
vnetReset,
vnetReady,
vnetSetConfig);
}
#ifdef IN_RING3
/**
* Check if the device can receive data now.
* This must be called before the pfnRecieve() method is called.
*
* @remarks As a side effect this function enables queue notification
* if it cannot receive because the queue is empty.
* It disables notification if it can receive.
*
* @returns VERR_NET_NO_BUFFER_SPACE if it cannot.
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @thread RX
*/
static int vnetCanReceive(VNETSTATE *pState)
{
int rc = vnetCsRxEnter(pState, VERR_SEM_BUSY);
AssertRCReturn(rc, rc);
LogFlow(("%s vnetCanReceive\n", INSTANCE(pState)));
if (!(pState->VPCI.uStatus & VPCI_STATUS_DRV_OK))
rc = VERR_NET_NO_BUFFER_SPACE;
else if (!vqueueIsReady(&pState->VPCI, pState->pRxQueue))
rc = VERR_NET_NO_BUFFER_SPACE;
else if (vqueueIsEmpty(&pState->VPCI, pState->pRxQueue))
{
vringSetNotification(&pState->VPCI, &pState->pRxQueue->VRing, true);
rc = VERR_NET_NO_BUFFER_SPACE;
}
else
{
vringSetNotification(&pState->VPCI, &pState->pRxQueue->VRing, false);
rc = VINF_SUCCESS;
}
LogFlow(("%s vnetCanReceive -> %Rrc\n", INSTANCE(pState), rc));
vnetCsRxLeave(pState);
return rc;
}
static DECLCALLBACK(int) vnetWaitReceiveAvail(PPDMINETWORKPORT pInterface, RTMSINTERVAL cMillies)
{
VNETSTATE *pState = IFACE_TO_STATE(pInterface, INetworkPort);
LogFlow(("%s vnetWaitReceiveAvail(cMillies=%u)\n", INSTANCE(pState), cMillies));
int rc = vnetCanReceive(pState);
if (RT_SUCCESS(rc))
return VINF_SUCCESS;
if (RT_UNLIKELY(cMillies == 0))
return VERR_NET_NO_BUFFER_SPACE;
rc = VERR_INTERRUPTED;
ASMAtomicXchgBool(&pState->fMaybeOutOfSpace, true);
STAM_PROFILE_START(&pState->StatRxOverflow, a);
VMSTATE enmVMState;
while (RT_LIKELY( (enmVMState = PDMDevHlpVMState(pState->VPCI.CTX_SUFF(pDevIns))) == VMSTATE_RUNNING
|| enmVMState == VMSTATE_RUNNING_LS))
{
int rc2 = vnetCanReceive(pState);
if (RT_SUCCESS(rc2))
{
rc = VINF_SUCCESS;
break;
}
Log(("%s vnetWaitReceiveAvail: waiting cMillies=%u...\n",
INSTANCE(pState), cMillies));
RTSemEventWait(pState->hEventMoreRxDescAvail, cMillies);
}
STAM_PROFILE_STOP(&pState->StatRxOverflow, a);
ASMAtomicXchgBool(&pState->fMaybeOutOfSpace, false);
LogFlow(("%s vnetWaitReceiveAvail -> %d\n", INSTANCE(pState), rc));
return rc;
}
/**
* @interface_method_impl{PDMIBASE,pfnQueryInterface}
*/
static DECLCALLBACK(void *) vnetQueryInterface(struct PDMIBASE *pInterface, const char *pszIID)
{
VNETSTATE *pThis = IFACE_TO_STATE(pInterface, VPCI.IBase);
Assert(&pThis->VPCI.IBase == pInterface);
PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKPORT, &pThis->INetworkPort);
PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKCONFIG, &pThis->INetworkConfig);
return vpciQueryInterface(pInterface, pszIID);
}
/**
* Returns true if it is a broadcast packet.
*
* @returns true if destination address indicates broadcast.
* @param pvBuf The ethernet packet.
*/
DECLINLINE(bool) vnetIsBroadcast(const void *pvBuf)
{
static const uint8_t s_abBcastAddr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
return memcmp(pvBuf, s_abBcastAddr, sizeof(s_abBcastAddr)) == 0;
}
/**
* Returns true if it is a multicast packet.
*
* @remarks returns true for broadcast packets as well.
* @returns true if destination address indicates multicast.
* @param pvBuf The ethernet packet.
*/
DECLINLINE(bool) vnetIsMulticast(const void *pvBuf)
{
return (*(char*)pvBuf) & 1;
}
/**
* Determines if the packet is to be delivered to upper layer.
*
* @returns true if packet is intended for this node.
* @param pState Pointer to the state structure.
* @param pvBuf The ethernet packet.
* @param cb Number of bytes available in the packet.
*/
static bool vnetAddressFilter(PVNETSTATE pState, const void *pvBuf, size_t cb)
{
if (pState->fPromiscuous)
return true;
/* Ignore everything outside of our VLANs */
uint16_t *u16Ptr = (uint16_t*)pvBuf;
/* Compare TPID with VLAN Ether Type */
if ( u16Ptr[6] == RT_H2BE_U16(0x8100)
&& !ASMBitTest(pState->aVlanFilter, RT_BE2H_U16(u16Ptr[7]) & 0xFFF))
return false;
if (vnetIsBroadcast(pvBuf))
return true;
if (pState->fAllMulti && vnetIsMulticast(pvBuf))
return true;
if (!memcmp(pState->config.mac.au8, pvBuf, sizeof(RTMAC)))
return true;
for (unsigned i = 0; i < pState->nMacFilterEntries; i++)
if (!memcmp(&pState->aMacFilter[i], pvBuf, sizeof(RTMAC)))
return true;
return false;
}
/**
* Pad and store received packet.
*
* @remarks Make sure that the packet appears to upper layer as one coming
* from real Ethernet: pad it and insert FCS.
*
* @returns VBox status code.
* @param pState The device state structure.
* @param pvBuf The available data.
* @param cb Number of bytes available in the buffer.
* @thread RX
*/
static int vnetHandleRxPacket(PVNETSTATE pState, const void *pvBuf, size_t cb)
{
VNETHDR hdr;
hdr.u8Flags = 0;
hdr.u8GSOType = VNETHDR_GSO_NONE;
vnetPacketDump(pState, (const uint8_t*)pvBuf, cb, "<-- Incoming");
unsigned int uOffset = 0;
for (unsigned int nElem = 0; uOffset < cb; nElem++)
{
VQUEUEELEM elem;
unsigned int nSeg = 0, uElemSize = 0;
if (!vqueueGet(&pState->VPCI, pState->pRxQueue, &elem))
{
Log(("%s vnetHandleRxPacket: Suddenly there is no space in receive queue!\n", INSTANCE(pState)));
return VERR_INTERNAL_ERROR;
}
if (elem.nIn < 1)
{
Log(("%s vnetHandleRxPacket: No writable descriptors in receive queue!\n", INSTANCE(pState)));
return VERR_INTERNAL_ERROR;
}
if (nElem == 0)
{
/* The very first segment of the very first element gets the header. */
if (elem.aSegsIn[nSeg].cb != sizeof(VNETHDR))
{
Log(("%s vnetHandleRxPacket: The first descriptor does match the header size!\n", INSTANCE(pState)));
return VERR_INTERNAL_ERROR;
}
elem.aSegsIn[nSeg++].pv = &hdr;
uElemSize += sizeof(VNETHDR);
}
while (nSeg < elem.nIn && uOffset < cb)
{
unsigned int uSize = RT_MIN(elem.aSegsIn[nSeg].cb, cb - uOffset);
elem.aSegsIn[nSeg++].pv = (uint8_t*)pvBuf + uOffset;
uOffset += uSize;
uElemSize += uSize;
}
STAM_PROFILE_START(&pState->StatReceiveStore, a);
vqueuePut(&pState->VPCI, pState->pRxQueue, &elem, uElemSize);
STAM_PROFILE_STOP(&pState->StatReceiveStore, a);
}
vqueueSync(&pState->VPCI, pState->pRxQueue);
return VINF_SUCCESS;
}
/**
* Receive data from the network.
*
* @returns VBox status code.
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @param pvBuf The available data.
* @param cb Number of bytes available in the buffer.
* @thread RX
*/
static DECLCALLBACK(int) vnetReceive(PPDMINETWORKPORT pInterface, const void *pvBuf, size_t cb)
{
VNETSTATE *pState = IFACE_TO_STATE(pInterface, INetworkPort);
Log2(("%s vnetReceive: pvBuf=%p cb=%u\n", INSTANCE(pState), pvBuf, cb));
int rc = vnetCanReceive(pState);
if (RT_FAILURE(rc))
return rc;
/* Drop packets if VM is not running or cable is disconnected. */
VMSTATE enmVMState = PDMDevHlpVMState(pState->VPCI.CTX_SUFF(pDevIns));
if (( enmVMState != VMSTATE_RUNNING
&& enmVMState != VMSTATE_RUNNING_LS)
|| !(STATUS & VNET_S_LINK_UP))
return VINF_SUCCESS;
STAM_PROFILE_START(&pState->StatReceive, a);
vpciSetReadLed(&pState->VPCI, true);
if (vnetAddressFilter(pState, pvBuf, cb))
{
rc = vnetCsRxEnter(pState, VERR_SEM_BUSY);
if (RT_SUCCESS(rc))
{
rc = vnetHandleRxPacket(pState, pvBuf, cb);
STAM_REL_COUNTER_ADD(&pState->StatReceiveBytes, cb);
vnetCsRxLeave(pState);
}
}
vpciSetReadLed(&pState->VPCI, false);
STAM_PROFILE_STOP(&pState->StatReceive, a);
return rc;
}
/**
* Gets the current Media Access Control (MAC) address.
*
* @returns VBox status code.
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @param pMac Where to store the MAC address.
* @thread EMT
*/
static DECLCALLBACK(int) vnetGetMac(PPDMINETWORKCONFIG pInterface, PRTMAC pMac)
{
VNETSTATE *pState = IFACE_TO_STATE(pInterface, INetworkConfig);
memcpy(pMac, pState->config.mac.au8, sizeof(RTMAC));
return VINF_SUCCESS;
}
/**
* Gets the new link state.
*
* @returns The current link state.
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @thread EMT
*/
static DECLCALLBACK(PDMNETWORKLINKSTATE) vnetGetLinkState(PPDMINETWORKCONFIG pInterface)
{
VNETSTATE *pState = IFACE_TO_STATE(pInterface, INetworkConfig);
if (STATUS & VNET_S_LINK_UP)
return PDMNETWORKLINKSTATE_UP;
return PDMNETWORKLINKSTATE_DOWN;
}
/**
* Sets the new link state.
*
* @returns VBox status code.
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @param enmState The new link state
*/
static DECLCALLBACK(int) vnetSetLinkState(PPDMINETWORKCONFIG pInterface, PDMNETWORKLINKSTATE enmState)
{
VNETSTATE *pState = IFACE_TO_STATE(pInterface, INetworkConfig);
bool fOldUp = !!(STATUS & VNET_S_LINK_UP);
bool fNewUp = enmState == PDMNETWORKLINKSTATE_UP;
if (fNewUp != fOldUp)
{
if (fNewUp)
{
Log(("%s Link is up\n", INSTANCE(pState)));
STATUS |= VNET_S_LINK_UP;
vpciRaiseInterrupt(&pState->VPCI, VERR_SEM_BUSY, VPCI_ISR_CONFIG);
}
else
{
Log(("%s Link is down\n", INSTANCE(pState)));
STATUS &= ~VNET_S_LINK_UP;
vpciRaiseInterrupt(&pState->VPCI, VERR_SEM_BUSY, VPCI_ISR_CONFIG);
}
if (pState->pDrv)
pState->pDrv->pfnNotifyLinkChanged(pState->pDrv, enmState);
}
return VINF_SUCCESS;
}
static DECLCALLBACK(void) vnetQueueReceive(void *pvState, PVQUEUE pQueue)
{
VNETSTATE *pState = (VNETSTATE*)pvState;
Log(("%s Receive buffers has been added, waking up receive thread.\n", INSTANCE(pState)));
vnetWakeupReceive(pState->VPCI.CTX_SUFF(pDevIns));
}
static DECLCALLBACK(void) vnetTransmitPendingPackets(PVNETSTATE pState, PVQUEUE pQueue)
{
/*
* Only one thread is allowed to transmit at a time, others should skip
* transmission as the packets will be picked up by the transmitting
* thread.
*/
if (!ASMAtomicCmpXchgU32(&pState->uIsTransmitting, 1, 0))
return;
if ((pState->VPCI.uStatus & VPCI_STATUS_DRV_OK) == 0)
{
Log(("%s Ignoring transmit requests from non-existent driver (status=0x%x).\n",
INSTANCE(pState), pState->VPCI.uStatus));
return;
}
Log3(("%s vnetTransmitPendingPackets: About to trasmit %d pending packets\n", INSTANCE(pState),
vringReadAvailIndex(&pState->VPCI, &pState->pTxQueue->VRing) - pState->pTxQueue->uNextAvailIndex));
vpciSetWriteLed(&pState->VPCI, true);
VQUEUEELEM elem;
while (vqueueGet(&pState->VPCI, pQueue, &elem))
{
unsigned int uOffset = 0;
if (elem.nOut < 2 || elem.aSegsOut[0].cb != sizeof(VNETHDR))
{
Log(("%s vnetQueueTransmit: The first segment is not the header! (%u < 2 || %u != %u).\n",
INSTANCE(pState), elem.nOut, elem.aSegsOut[0].cb, sizeof(VNETHDR)));
break; /* For now we simply ignore the header, but it must be there anyway! */
}
else
{
STAM_PROFILE_ADV_START(&pState->StatTransmit, a);
/* Assemble a complete frame. */
for (unsigned int i = 1; i < elem.nOut && uOffset < VNET_MAX_FRAME_SIZE; i++)
{
unsigned int uSize = elem.aSegsOut[i].cb;
if (uSize > VNET_MAX_FRAME_SIZE - uOffset)
{
Log(("%s vnetQueueTransmit: Packet is too big (>64k), truncating...\n", INSTANCE(pState)));
uSize = VNET_MAX_FRAME_SIZE - uOffset;
}
PDMDevHlpPhysRead(pState->VPCI.CTX_SUFF(pDevIns), elem.aSegsOut[i].addr,
pState->pTxBuf + uOffset, uSize);
uOffset += uSize;
}
if (pState->pDrv)
{
vnetPacketDump(pState, pState->pTxBuf, uOffset, "--> Outgoing");
STAM_PROFILE_START(&pState->StatTransmitSend, a);
int rc = pState->pDrv->pfnSend(pState->pDrv, pState->pTxBuf, uOffset);
STAM_PROFILE_STOP(&pState->StatTransmitSend, a);
STAM_REL_COUNTER_ADD(&pState->StatTransmitBytes, uOffset);
}
}
vqueuePut(&pState->VPCI, pQueue, &elem, sizeof(VNETHDR) + uOffset);
vqueueSync(&pState->VPCI, pQueue);
STAM_PROFILE_ADV_STOP(&pState->StatTransmit, a);
}
vpciSetWriteLed(&pState->VPCI, false);
ASMAtomicWriteU32(&pState->uIsTransmitting, 0);
}
#ifdef VNET_TX_DELAY
static DECLCALLBACK(void) vnetQueueTransmit(void *pvState, PVQUEUE pQueue)
{
VNETSTATE *pState = (VNETSTATE*)pvState;
if (TMTimerIsActive(pState->CTX_SUFF(pTxTimer)))
{
int rc = TMTimerStop(pState->CTX_SUFF(pTxTimer));
Log3(("%s vnetQueueTransmit: Got kicked with notification disabled, "
"re-enable notification and flush TX queue\n", INSTANCE(pState)));
vnetTransmitPendingPackets(pState, pQueue);
if (RT_FAILURE(vnetCsEnter(pState, VERR_SEM_BUSY)))
LogRel(("vnetQueueTransmit: Failed to enter critical section!/n"));
else
{
vringSetNotification(&pState->VPCI, &pState->pTxQueue->VRing, true);
vnetCsLeave(pState);
}
}
else
{
if (RT_FAILURE(vnetCsEnter(pState, VERR_SEM_BUSY)))
LogRel(("vnetQueueTransmit: Failed to enter critical section!/n"));
else
{
vringSetNotification(&pState->VPCI, &pState->pTxQueue->VRing, false);
TMTimerSetMicro(pState->CTX_SUFF(pTxTimer), VNET_TX_DELAY);
pState->u64NanoTS = RTTimeNanoTS();
vnetCsLeave(pState);
}
}
}
/**
* Transmit Delay Timer handler.
*
* @remarks We only get here when the timer expires.
*
* @param pDevIns Pointer to device instance structure.
* @param pTimer Pointer to the timer.
* @param pvUser NULL.
* @thread EMT
*/
static DECLCALLBACK(void) vnetTxTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
{
VNETSTATE *pState = (VNETSTATE*)pvUser;
uint32_t u32MicroDiff = (uint32_t)((RTTimeNanoTS() - pState->u64NanoTS)/1000);
if (u32MicroDiff < pState->u32MinDiff)
pState->u32MinDiff = u32MicroDiff;
if (u32MicroDiff > pState->u32MaxDiff)
pState->u32MaxDiff = u32MicroDiff;
pState->u32AvgDiff = (pState->u32AvgDiff * pState->u32i + u32MicroDiff) / (pState->u32i + 1);
pState->u32i++;
Log3(("vnetTxTimer: Expired, diff %9d usec, avg %9d usec, min %9d usec, max %9d usec\n",
u32MicroDiff, pState->u32AvgDiff, pState->u32MinDiff, pState->u32MaxDiff));
// Log3(("%s vnetTxTimer: Expired\n", INSTANCE(pState)));
vnetTransmitPendingPackets(pState, pState->pTxQueue);
if (RT_FAILURE(vnetCsEnter(pState, VERR_SEM_BUSY)))
{
LogRel(("vnetTxTimer: Failed to enter critical section!/n"));
return;
}
vringSetNotification(&pState->VPCI, &pState->pTxQueue->VRing, true);
vnetCsLeave(pState);
}
#else /* !VNET_TX_DELAY */
static DECLCALLBACK(void) vnetQueueTransmit(void *pvState, PVQUEUE pQueue)
{
VNETSTATE *pState = (VNETSTATE*)pvState;
vnetTransmitPendingPackets(pState, pQueue);
}
#endif /* !VNET_TX_DELAY */
static uint8_t vnetControlRx(PVNETSTATE pState, PVNETCTLHDR pCtlHdr, PVQUEUEELEM pElem)
{
uint8_t u8Ack = VNET_OK;
uint8_t fOn;
PDMDevHlpPhysRead(pState->VPCI.CTX_SUFF(pDevIns),
pElem->aSegsOut[1].addr,
&fOn, sizeof(fOn));
Log(("%s vnetControlRx: uCommand=%u fOn=%u\n", INSTANCE(pState), pCtlHdr->u8Command, fOn));
switch (pCtlHdr->u8Command)
{
case VNET_CTRL_CMD_RX_MODE_PROMISC:
pState->fPromiscuous = !!fOn;
break;
case VNET_CTRL_CMD_RX_MODE_ALLMULTI:
pState->fAllMulti = !!fOn;
break;
default:
u8Ack = VNET_ERROR;
}
return u8Ack;
}
static uint8_t vnetControlMac(PVNETSTATE pState, PVNETCTLHDR pCtlHdr, PVQUEUEELEM pElem)
{
uint32_t nMacs = 0;
if (pCtlHdr->u8Command != VNET_CTRL_CMD_MAC_TABLE_SET
|| pElem->nOut != 3
|| pElem->aSegsOut[1].cb < sizeof(nMacs)
|| pElem->aSegsOut[2].cb < sizeof(nMacs))
{
Log(("%s vnetControlMac: Segment layout is wrong "
"(u8Command=%u nOut=%u cb1=%u cb2=%u)\n", INSTANCE(pState),
pCtlHdr->u8Command, pElem->nOut,
pElem->aSegsOut[1].cb, pElem->aSegsOut[2].cb));
return VNET_ERROR;
}
/* Load unicast addresses */
PDMDevHlpPhysRead(pState->VPCI.CTX_SUFF(pDevIns),
pElem->aSegsOut[1].addr,
&nMacs, sizeof(nMacs));
if (pElem->aSegsOut[1].cb < nMacs * sizeof(RTMAC) + sizeof(nMacs))
{
Log(("%s vnetControlMac: The unicast mac segment is too small "
"(nMacs=%u cb=%u)\n", INSTANCE(pState), pElem->aSegsOut[1].cb));
return VNET_ERROR;
}
if (nMacs > VNET_MAC_FILTER_LEN)
{
Log(("%s vnetControlMac: MAC table is too big, have to use promiscuous"
" mode (nMacs=%u)\n", INSTANCE(pState), nMacs));
pState->fPromiscuous = true;
}
else
{
if (nMacs)
PDMDevHlpPhysRead(pState->VPCI.CTX_SUFF(pDevIns),
pElem->aSegsOut[1].addr + sizeof(nMacs),
pState->aMacFilter, nMacs * sizeof(RTMAC));
pState->nMacFilterEntries = nMacs;
#ifdef DEBUG
Log(("%s vnetControlMac: unicast macs:\n", INSTANCE(pState)));
for(unsigned i = 0; i < nMacs; i++)
Log((" %RTmac\n", &pState->aMacFilter[i]));
#endif /* DEBUG */
}
/* Load multicast addresses */
PDMDevHlpPhysRead(pState->VPCI.CTX_SUFF(pDevIns),
pElem->aSegsOut[2].addr,
&nMacs, sizeof(nMacs));
if (pElem->aSegsOut[2].cb < nMacs * sizeof(RTMAC) + sizeof(nMacs))
{
Log(("%s vnetControlMac: The multicast mac segment is too small "
"(nMacs=%u cb=%u)\n", INSTANCE(pState), pElem->aSegsOut[2].cb));
return VNET_ERROR;
}
if (nMacs > VNET_MAC_FILTER_LEN - pState->nMacFilterEntries)
{
Log(("%s vnetControlMac: MAC table is too big, have to use allmulti"
" mode (nMacs=%u)\n", INSTANCE(pState), nMacs));
pState->fAllMulti = true;
}
else
{
if (nMacs)
PDMDevHlpPhysRead(pState->VPCI.CTX_SUFF(pDevIns),
pElem->aSegsOut[2].addr + sizeof(nMacs),
&pState->aMacFilter[pState->nMacFilterEntries],
nMacs * sizeof(RTMAC));
#ifdef DEBUG
Log(("%s vnetControlMac: multicast macs:\n", INSTANCE(pState)));
for(unsigned i = 0; i < nMacs; i++)
Log((" %RTmac\n",
&pState->aMacFilter[i+pState->nMacFilterEntries]));
#endif /* DEBUG */
pState->nMacFilterEntries += nMacs;
}
return VNET_OK;
}
static uint8_t vnetControlVlan(PVNETSTATE pState, PVNETCTLHDR pCtlHdr, PVQUEUEELEM pElem)
{
uint8_t u8Ack = VNET_OK;
uint16_t u16Vid;
if (pElem->nOut != 2 || pElem->aSegsOut[1].cb != sizeof(u16Vid))
{
Log(("%s vnetControlVlan: Segment layout is wrong "
"(u8Command=%u nOut=%u cb=%u)\n", INSTANCE(pState),
pCtlHdr->u8Command, pElem->nOut, pElem->aSegsOut[1].cb));
return VNET_ERROR;
}
PDMDevHlpPhysRead(pState->VPCI.CTX_SUFF(pDevIns),
pElem->aSegsOut[1].addr,
&u16Vid, sizeof(u16Vid));
if (u16Vid >= VNET_MAX_VID)
{
Log(("%s vnetControlVlan: VLAN ID is out of range "
"(VID=%u)\n", INSTANCE(pState), u16Vid));
return VNET_ERROR;
}
Log(("%s vnetControlVlan: uCommand=%u VID=%u\n", INSTANCE(pState),
pCtlHdr->u8Command, u16Vid));
switch (pCtlHdr->u8Command)
{
case VNET_CTRL_CMD_VLAN_ADD:
ASMBitSet(pState->aVlanFilter, u16Vid);
break;
case VNET_CTRL_CMD_VLAN_DEL:
ASMBitClear(pState->aVlanFilter, u16Vid);
break;
default:
u8Ack = VNET_ERROR;
}
return u8Ack;
}
static DECLCALLBACK(void) vnetQueueControl(void *pvState, PVQUEUE pQueue)
{
VNETSTATE *pState = (VNETSTATE*)pvState;
uint8_t u8Ack;
VQUEUEELEM elem;
while (vqueueGet(&pState->VPCI, pQueue, &elem))
{
unsigned int uOffset = 0;
if (elem.nOut < 1 || elem.aSegsOut[0].cb < sizeof(VNETCTLHDR))
{
Log(("%s vnetQueueControl: The first 'out' segment is not the "
"header! (%u < 1 || %u < %u).\n", INSTANCE(pState), elem.nOut,
elem.aSegsOut[0].cb,sizeof(VNETCTLHDR)));
break; /* Skip the element and hope the next one is good. */
}
else if ( elem.nIn < 1
|| elem.aSegsIn[elem.nIn - 1].cb < sizeof(VNETCTLACK))
{
Log(("%s vnetQueueControl: The last 'in' segment is too small "
"to hold the acknowledge! (%u < 1 || %u < %u).\n",
INSTANCE(pState), elem.nIn, elem.aSegsIn[elem.nIn - 1].cb,
sizeof(VNETCTLACK)));
break; /* Skip the element and hope the next one is good. */
}
else
{
VNETCTLHDR CtlHdr;
PDMDevHlpPhysRead(pState->VPCI.CTX_SUFF(pDevIns),
elem.aSegsOut[0].addr,
&CtlHdr, sizeof(CtlHdr));
switch (CtlHdr.u8Class)
{
case VNET_CTRL_CLS_RX_MODE:
u8Ack = vnetControlRx(pState, &CtlHdr, &elem);
break;
case VNET_CTRL_CLS_MAC:
u8Ack = vnetControlMac(pState, &CtlHdr, &elem);
break;
case VNET_CTRL_CLS_VLAN:
u8Ack = vnetControlVlan(pState, &CtlHdr, &elem);
break;
default:
u8Ack = VNET_ERROR;
}
Log(("%s Processed control message %u, ack=%u.\n", INSTANCE(pState),
CtlHdr.u8Class, u8Ack));
PDMDevHlpPhysWrite(pState->VPCI.CTX_SUFF(pDevIns),
elem.aSegsIn[elem.nIn - 1].addr,
&u8Ack, sizeof(u8Ack));
}
vqueuePut(&pState->VPCI, pQueue, &elem, sizeof(u8Ack));
vqueueSync(&pState->VPCI, pQueue);
}
}
/**
* Saves the configuration.
*
* @param pState The VNET state.
* @param pSSM The handle to the saved state.
*/
static void vnetSaveConfig(VNETSTATE *pState, PSSMHANDLE pSSM)
{
SSMR3PutMem(pSSM, &pState->macConfigured, sizeof(pState->macConfigured));
}
/**
* Live save - save basic configuration.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pSSM The handle to the saved state.
* @param uPass
*/
static DECLCALLBACK(int) vnetLiveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uPass)
{
VNETSTATE *pState = PDMINS_2_DATA(pDevIns, VNETSTATE*);
vnetSaveConfig(pState, pSSM);
return VINF_SSM_DONT_CALL_AGAIN;
}
/**
* Prepares for state saving.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pSSM The handle to the saved state.
*/
static DECLCALLBACK(int) vnetSavePrep(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
{
VNETSTATE* pState = PDMINS_2_DATA(pDevIns, VNETSTATE*);
int rc = vnetCsRxEnter(pState, VERR_SEM_BUSY);
if (RT_UNLIKELY(rc != VINF_SUCCESS))
return rc;
vnetCsRxLeave(pState);
return VINF_SUCCESS;
}
/**
* Saves the state of device.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pSSM The handle to the saved state.
*/
static DECLCALLBACK(int) vnetSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
{
VNETSTATE* pState = PDMINS_2_DATA(pDevIns, VNETSTATE*);
/* Save config first */
vnetSaveConfig(pState, pSSM);
/* Save the common part */
int rc = vpciSaveExec(&pState->VPCI, pSSM);
AssertRCReturn(rc, rc);
/* Save device-specific part */
rc = SSMR3PutMem( pSSM, pState->config.mac.au8, sizeof(pState->config.mac));
AssertRCReturn(rc, rc);
rc = SSMR3PutBool(pSSM, pState->fPromiscuous);
AssertRCReturn(rc, rc);
rc = SSMR3PutBool(pSSM, pState->fAllMulti);
AssertRCReturn(rc, rc);
rc = SSMR3PutU32( pSSM, pState->nMacFilterEntries);
AssertRCReturn(rc, rc);
rc = SSMR3PutMem( pSSM, pState->aMacFilter,
pState->nMacFilterEntries * sizeof(RTMAC));
AssertRCReturn(rc, rc);
rc = SSMR3PutMem( pSSM, pState->aVlanFilter, sizeof(pState->aVlanFilter));
AssertRCReturn(rc, rc);
Log(("%s State has been saved\n", INSTANCE(pState)));
return VINF_SUCCESS;
}
/**
* Serializes the receive thread, it may be working inside the critsect.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pSSM The handle to the saved state.
*/
static DECLCALLBACK(int) vnetLoadPrep(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
{
VNETSTATE* pState = PDMINS_2_DATA(pDevIns, VNETSTATE*);
int rc = vnetCsRxEnter(pState, VERR_SEM_BUSY);
if (RT_UNLIKELY(rc != VINF_SUCCESS))
return rc;
vnetCsRxLeave(pState);
return VINF_SUCCESS;
}
/* Takes down the link temporarily if it's current status is up.
*
* This is used during restore and when replumbing the network link.
*
* The temporary link outage is supposed to indicate to the OS that all network
* connections have been lost and that it for instance is appropriate to
* renegotiate any DHCP lease.
*
* @param pThis The PCNet instance data.
*/
static void vnetTempLinkDown(PVNETSTATE pState)
{
if (STATUS & VNET_S_LINK_UP)
{
STATUS &= ~VNET_S_LINK_UP;
vpciRaiseInterrupt(&pState->VPCI, VERR_SEM_BUSY, VPCI_ISR_CONFIG);
/* Restore the link back in 5 seconds. */
int rc = TMTimerSetMillies(pState->pLinkUpTimer, 5000);
AssertRC(rc);
}
}
/**
* Restore previously saved state of device.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pSSM The handle to the saved state.
* @param uVersion The data unit version number.
* @param uPass The data pass.
*/
static DECLCALLBACK(int) vnetLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
{
VNETSTATE *pState = PDMINS_2_DATA(pDevIns, VNETSTATE*);
int rc;
/* config checks */
RTMAC macConfigured;
rc = SSMR3GetMem(pSSM, &macConfigured, sizeof(macConfigured));
AssertRCReturn(rc, rc);
if (memcmp(&macConfigured, &pState->macConfigured, sizeof(macConfigured))
&& (uPass == 0 || !PDMDevHlpVMTeleportedAndNotFullyResumedYet(pDevIns)))
LogRel(("%s: The mac address differs: config=%RTmac saved=%RTmac\n", INSTANCE(pState), &pState->macConfigured, &macConfigured));
rc = vpciLoadExec(&pState->VPCI, pSSM, uVersion, uPass, VNET_N_QUEUES);
AssertRCReturn(rc, rc);
if (uPass == SSM_PASS_FINAL)
{
rc = SSMR3GetMem( pSSM, pState->config.mac.au8,
sizeof(pState->config.mac));
AssertRCReturn(rc, rc);
if (uVersion > VIRTIO_SAVEDSTATE_VERSION_3_1_BETA1)
{
rc = SSMR3GetBool(pSSM, &pState->fPromiscuous);
AssertRCReturn(rc, rc);
rc = SSMR3GetBool(pSSM, &pState->fAllMulti);
AssertRCReturn(rc, rc);
rc = SSMR3GetU32(pSSM, &pState->nMacFilterEntries);
AssertRCReturn(rc, rc);
rc = SSMR3GetMem(pSSM, pState->aMacFilter,
pState->nMacFilterEntries * sizeof(RTMAC));
AssertRCReturn(rc, rc);
/* Clear the rest. */
if (pState->nMacFilterEntries < VNET_MAC_FILTER_LEN)
memset(&pState->aMacFilter[pState->nMacFilterEntries],
0,
(VNET_MAC_FILTER_LEN - pState->nMacFilterEntries)
* sizeof(RTMAC));
rc = SSMR3GetMem(pSSM, pState->aVlanFilter,
sizeof(pState->aVlanFilter));
AssertRCReturn(rc, rc);
}
else
{
pState->fPromiscuous = true;
pState->fAllMulti = false;
pState->nMacFilterEntries = 0;
memset(pState->aMacFilter, 0, VNET_MAC_FILTER_LEN * sizeof(RTMAC));
memset(pState->aVlanFilter, 0, sizeof(pState->aVlanFilter));
}
}
return rc;
}
/**
* Link status adjustments after loading.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pSSM The handle to the saved state.
*/
static DECLCALLBACK(int) vnetLoadDone(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
{
VNETSTATE *pState = PDMINS_2_DATA(pDevIns, VNETSTATE*);
/*
* Indicate link down to the guest OS that all network connections have
* been lost, unless we've been teleported here.
*/
if (!PDMDevHlpVMTeleportedAndNotFullyResumedYet(pDevIns))
vnetTempLinkDown(pState);
return VINF_SUCCESS;
}
/**
* Map PCI I/O region.
*
* @return VBox status code.
* @param pPciDev Pointer to PCI device. Use pPciDev->pDevIns to get the device instance.
* @param iRegion The region number.
* @param GCPhysAddress Physical address of the region. If iType is PCI_ADDRESS_SPACE_IO, this is an
* I/O port, else it's a physical address.
* This address is *NOT* relative to pci_mem_base like earlier!
* @param cb Region size.
* @param enmType One of the PCI_ADDRESS_SPACE_* values.
* @thread EMT
*/
static DECLCALLBACK(int) vnetMap(PPCIDEVICE pPciDev, int iRegion,
RTGCPHYS GCPhysAddress, uint32_t cb, PCIADDRESSSPACE enmType)
{
int rc;
VNETSTATE *pState = PDMINS_2_DATA(pPciDev->pDevIns, VNETSTATE*);
if (enmType != PCI_ADDRESS_SPACE_IO)
{
/* We should never get here */
AssertMsgFailed(("Invalid PCI address space param in map callback"));
return VERR_INTERNAL_ERROR;
}
pState->VPCI.addrIOPort = (RTIOPORT)GCPhysAddress;
rc = PDMDevHlpIOPortRegister(pPciDev->pDevIns, pState->VPCI.addrIOPort,
cb, 0, vnetIOPortOut, vnetIOPortIn,
NULL, NULL, "VirtioNet");
#ifdef VNET_GC_SUPPORT
AssertRCReturn(rc, rc);
rc = PDMDevHlpIOPortRegisterR0(pPciDev->pDevIns, pState->VPCI.addrIOPort,
cb, 0, "vnetIOPortOut", "vnetIOPortIn",
NULL, NULL, "VirtioNet");
AssertRCReturn(rc, rc);
rc = PDMDevHlpIOPortRegisterRC(pPciDev->pDevIns, pState->VPCI.addrIOPort,
cb, 0, "vnetIOPortOut", "vnetIOPortIn",
NULL, NULL, "VirtioNet");
#endif
AssertRC(rc);
return rc;
}
/* -=-=-=-=- PDMDEVREG -=-=-=-=- */
#ifdef VBOX_DYNAMIC_NET_ATTACH
/**
* Detach notification.
*
* One port on the network card has been disconnected from the network.
*
* @param pDevIns The device instance.
* @param iLUN The logical unit which is being detached.
* @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
*/
static DECLCALLBACK(void) vnetDetach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
{
VNETSTATE *pState = PDMINS_2_DATA(pDevIns, VNETSTATE*);
Log(("%s vnetDetach:\n", INSTANCE(pState)));
AssertLogRelReturnVoid(iLUN == 0);
int rc = vnetCsEnter(pState, VERR_SEM_BUSY);
if (RT_FAILURE(rc))
{
LogRel(("vnetDetach failed to enter critical section!\n"));
return;
}
/*
* Zero some important members.
*/
pState->pDrvBase = NULL;
pState->pDrv = NULL;
vnetCsLeave(pState);
}
/**
* Attach the Network attachment.
*
* One port on the network card has been connected to a network.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param iLUN The logical unit which is being attached.
* @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
*
* @remarks This code path is not used during construction.
*/
static DECLCALLBACK(int) vnetAttach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
{
VNETSTATE *pState = PDMINS_2_DATA(pDevIns, VNETSTATE*);
LogFlow(("%s vnetAttach:\n", INSTANCE(pState)));
AssertLogRelReturn(iLUN == 0, VERR_PDM_NO_SUCH_LUN);
int rc = vnetCsEnter(pState, VERR_SEM_BUSY);
if (RT_FAILURE(rc))
{
LogRel(("vnetAttach failed to enter critical section!\n"));
return rc;
}
/*
* Attach the driver.
*/
rc = PDMDevHlpDriverAttach(pDevIns, 0, &pState->VPCI.IBase, &pState->pDrvBase, "Network Port");
if (RT_SUCCESS(rc))
{
if (rc == VINF_NAT_DNS)
{
#ifdef RT_OS_LINUX
PDMDevHlpVMSetRuntimeError(pDevIns, 0 /*fFlags*/, "NoDNSforNAT",
N_("A Domain Name Server (DNS) for NAT networking could not be determined. Please check your /etc/resolv.conf for <tt>nameserver</tt> entries. Either add one manually (<i>man resolv.conf</i>) or ensure that your host is correctly connected to an ISP. If you ignore this warning the guest will not be able to perform nameserver lookups and it will probably observe delays if trying so"));
#else
PDMDevHlpVMSetRuntimeError(pDevIns, 0 /*fFlags*/, "NoDNSforNAT",
N_("A Domain Name Server (DNS) for NAT networking could not be determined. Ensure that your host is correctly connected to an ISP. If you ignore this warning the guest will not be able to perform nameserver lookups and it will probably observe delays if trying so"));
#endif
}
pState->pDrv = PDMIBASE_QUERY_INTERFACE(pState->pDrvBase, PDMINETWORKCONNECTOR);
AssertMsgStmt(pState->pDrv, ("Failed to obtain the PDMINETWORKCONNECTOR interface!\n"),
rc = VERR_PDM_MISSING_INTERFACE_BELOW);
}
else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
Log(("%s No attached driver!\n", INSTANCE(pState)));
/*
* Temporary set the link down if it was up so that the guest
* will know that we have change the configuration of the
* network card
*/
if (RT_SUCCESS(rc))
vnetTempLinkDown(pState);
vnetCsLeave(pState);
return rc;
}
#endif /* VBOX_DYNAMIC_NET_ATTACH */
/**
* @copydoc FNPDMDEVSUSPEND
*/
static DECLCALLBACK(void) vnetSuspend(PPDMDEVINS pDevIns)
{
/* Poke thread waiting for buffer space. */
vnetWakeupReceive(pDevIns);
}
/**
* @copydoc FNPDMDEVPOWEROFF
*/
static DECLCALLBACK(void) vnetPowerOff(PPDMDEVINS pDevIns)
{
/* Poke thread waiting for buffer space. */
vnetWakeupReceive(pDevIns);
}
/**
* Device relocation callback.
*
* When this callback is called the device instance data, and if the
* device have a GC component, is being relocated, or/and the selectors
* have been changed. The device must use the chance to perform the
* necessary pointer relocations and data updates.
*
* Before the GC code is executed the first time, this function will be
* called with a 0 delta so GC pointer calculations can be one in one place.
*
* @param pDevIns Pointer to the device instance.
* @param offDelta The relocation delta relative to the old location.
*
* @remark A relocation CANNOT fail.
*/
static DECLCALLBACK(void) vnetRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
{
VNETSTATE* pState = PDMINS_2_DATA(pDevIns, VNETSTATE*);
vpciRelocate(pDevIns, offDelta);
pState->pCanRxQueueRC = PDMQueueRCPtr(pState->pCanRxQueueR3);
#ifdef VNET_TX_DELAY
pState->pTxTimerRC = TMTimerRCPtr(pState->pTxTimerR3);
#endif /* VNET_TX_DELAY */
// TBD
}
/**
* Destruct a device instance.
*
* We need to free non-VM resources only.
*
* @returns VBox status.
* @param pDevIns The device instance data.
* @thread EMT
*/
static DECLCALLBACK(int) vnetDestruct(PPDMDEVINS pDevIns)
{
VNETSTATE* pState = PDMINS_2_DATA(pDevIns, VNETSTATE*);
PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
LogRel(("TxTimer stats (avg/min/max): %7d usec %7d usec %7d usec\n",
pState->u32AvgDiff, pState->u32MinDiff, pState->u32MaxDiff));
Log(("%s Destroying instance\n", INSTANCE(pState)));
if (pState->hEventMoreRxDescAvail != NIL_RTSEMEVENT)
{
RTSemEventSignal(pState->hEventMoreRxDescAvail);
RTSemEventDestroy(pState->hEventMoreRxDescAvail);
pState->hEventMoreRxDescAvail = NIL_RTSEMEVENT;
}
if (pState->pTxBuf)
{
RTMemFree(pState->pTxBuf);
pState->pTxBuf = NULL;
}
// if (PDMCritSectIsInitialized(&pState->csRx))
// PDMR3CritSectDelete(&pState->csRx);
return vpciDestruct(&pState->VPCI);
}
/**
* @interface_method_impl{PDMDEVREG,pfnConstruct}
*/
static DECLCALLBACK(int) vnetConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
{
VNETSTATE* pState = PDMINS_2_DATA(pDevIns, VNETSTATE*);
int rc;
PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
/* Initialize PCI part first. */
pState->VPCI.IBase.pfnQueryInterface = vnetQueryInterface;
rc = vpciConstruct(pDevIns, &pState->VPCI, iInstance,
VNET_NAME_FMT, VNET_PCI_SUBSYSTEM_ID,
VNET_PCI_CLASS, VNET_N_QUEUES);
pState->pRxQueue = vpciAddQueue(&pState->VPCI, 256, vnetQueueReceive, "RX ");
pState->pTxQueue = vpciAddQueue(&pState->VPCI, 256, vnetQueueTransmit, "TX ");
pState->pCtlQueue = vpciAddQueue(&pState->VPCI, 16, vnetQueueControl, "CTL");
Log(("%s Constructing new instance\n", INSTANCE(pState)));
pState->hEventMoreRxDescAvail = NIL_RTSEMEVENT;
/*
* Validate configuration.
*/
if (!CFGMR3AreValuesValid(pCfg, "MAC\0" "CableConnected\0" "LineSpeed\0"))
return PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
N_("Invalid configuration for VirtioNet device"));
/* Get config params */
rc = CFGMR3QueryBytes(pCfg, "MAC", pState->macConfigured.au8,
sizeof(pState->macConfigured));
if (RT_FAILURE(rc))
return PDMDEV_SET_ERROR(pDevIns, rc,
N_("Configuration error: Failed to get MAC address"));
rc = CFGMR3QueryBool(pCfg, "CableConnected", &pState->fCableConnected);
if (RT_FAILURE(rc))
return PDMDEV_SET_ERROR(pDevIns, rc,
N_("Configuration error: Failed to get the value of 'CableConnected'"));
/* Initialize PCI config space */
memcpy(pState->config.mac.au8, pState->macConfigured.au8, sizeof(pState->config.mac.au8));
pState->config.uStatus = 0;
/* Initialize state structure */
pState->u32PktNo = 1;
/* Interfaces */
pState->INetworkPort.pfnWaitReceiveAvail = vnetWaitReceiveAvail;
pState->INetworkPort.pfnReceive = vnetReceive;
pState->INetworkConfig.pfnGetMac = vnetGetMac;
pState->INetworkConfig.pfnGetLinkState = vnetGetLinkState;
pState->INetworkConfig.pfnSetLinkState = vnetSetLinkState;
pState->pTxBuf = (uint8_t *)RTMemAllocZ(VNET_MAX_FRAME_SIZE);
AssertMsgReturn(pState->pTxBuf,
("Cannot allocate TX buffer for virtio-net device\n"), VERR_NO_MEMORY);
/* Initialize critical section. */
// char szTmp[sizeof(pState->VPCI.szInstance) + 2];
// RTStrPrintf(szTmp, sizeof(szTmp), "%sRX", pState->VPCI.szInstance);
// rc = PDMDevHlpCritSectInit(pDevIns, &pState->csRx, szTmp);
// if (RT_FAILURE(rc))
// return rc;
/* Map our ports to IO space. */
rc = PDMDevHlpPCIIORegionRegister(pDevIns, 0,
VPCI_CONFIG + sizeof(VNetPCIConfig),
PCI_ADDRESS_SPACE_IO, vnetMap);
if (RT_FAILURE(rc))
return rc;
/* Register save/restore state handlers. */
rc = PDMDevHlpSSMRegisterEx(pDevIns, VIRTIO_SAVEDSTATE_VERSION, sizeof(VNETSTATE), NULL,
NULL, vnetLiveExec, NULL,
vnetSavePrep, vnetSaveExec, NULL,
vnetLoadPrep, vnetLoadExec, vnetLoadDone);
if (RT_FAILURE(rc))
return rc;
/* Create the RX notifier signaller. */
rc = PDMDevHlpQueueCreate(pDevIns, sizeof(PDMQUEUEITEMCORE), 1, 0,
vnetCanRxQueueConsumer, true, "VNet-Rcv", &pState->pCanRxQueueR3);
if (RT_FAILURE(rc))
return rc;
pState->pCanRxQueueR0 = PDMQueueR0Ptr(pState->pCanRxQueueR3);
pState->pCanRxQueueRC = PDMQueueRCPtr(pState->pCanRxQueueR3);
/* Create Link Up Timer */
rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, vnetLinkUpTimer, pState,
TMTIMER_FLAGS_DEFAULT_CRIT_SECT, /** @todo check locking here. */
"VirtioNet Link Up Timer", &pState->pLinkUpTimer);
if (RT_FAILURE(rc))
return rc;
#ifdef VNET_TX_DELAY
/* Create Transmit Delay Timer */
rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, vnetTxTimer, pState,
TMTIMER_FLAGS_DEFAULT_CRIT_SECT, /** @todo check locking here. */
"VirtioNet TX Delay Timer", &pState->pTxTimerR3);
if (RT_FAILURE(rc))
return rc;
pState->pTxTimerR0 = TMTimerR0Ptr(pState->pTxTimerR3);
pState->pTxTimerRC = TMTimerRCPtr(pState->pTxTimerR3);
pState->u32i = pState->u32AvgDiff = pState->u32MaxDiff = 0;
pState->u32MinDiff = ~0;
#endif /* VNET_TX_DELAY */
rc = PDMDevHlpDriverAttach(pDevIns, 0, &pState->VPCI.IBase, &pState->pDrvBase, "Network Port");
if (RT_SUCCESS(rc))
{
if (rc == VINF_NAT_DNS)
{
PDMDevHlpVMSetRuntimeError(pDevIns, 0 /*fFlags*/, "NoDNSforNAT",
N_("A Domain Name Server (DNS) for NAT networking could not be determined. Ensure that your host is correctly connected to an ISP. If you ignore this warning the guest will not be able to perform nameserver lookups and it will probably observe delays if trying so"));
}
pState->pDrv = PDMIBASE_QUERY_INTERFACE(pState->pDrvBase, PDMINETWORKCONNECTOR);
AssertMsgReturn(pState->pDrv, ("Failed to obtain the PDMINETWORKCONNECTOR interface!\n"),
VERR_PDM_MISSING_INTERFACE_BELOW);
}
else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
{
Log(("%s This adapter is not attached to any network!\n", INSTANCE(pState)));
}
else
return PDMDEV_SET_ERROR(pDevIns, rc, N_("Failed to attach the network LUN"));
rc = RTSemEventCreate(&pState->hEventMoreRxDescAvail);
if (RT_FAILURE(rc))
return rc;
vnetReset(pState);
PDMDevHlpSTAMRegisterF(pDevIns, &pState->StatReceiveBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Amount of data received", "/Devices/VNet%d/ReceiveBytes", iInstance);
PDMDevHlpSTAMRegisterF(pDevIns, &pState->StatTransmitBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Amount of data transmitted", "/Devices/VNet%d/TransmitBytes", iInstance);
#if defined(VBOX_WITH_STATISTICS)
PDMDevHlpSTAMRegisterF(pDevIns, &pState->StatReceive, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling receive", "/Devices/VNet%d/Receive/Total", iInstance);
PDMDevHlpSTAMRegisterF(pDevIns, &pState->StatReceiveStore, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling receive storing", "/Devices/VNet%d/Receive/Store", iInstance);
PDMDevHlpSTAMRegisterF(pDevIns, &pState->StatRxOverflow, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_OCCURENCE, "Profiling RX overflows", "/Devices/VNet%d/RxOverflow", iInstance);
PDMDevHlpSTAMRegisterF(pDevIns, &pState->StatRxOverflowWakeup, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Nr of RX overflow wakeups", "/Devices/VNet%d/RxOverflowWakeup", iInstance);
PDMDevHlpSTAMRegisterF(pDevIns, &pState->StatTransmit, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling transmits in HC", "/Devices/VNet%d/Transmit/Total", iInstance);
PDMDevHlpSTAMRegisterF(pDevIns, &pState->StatTransmitSend, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling send transmit in HC", "/Devices/VNet%d/Transmit/Send", iInstance);
#endif /* VBOX_WITH_STATISTICS */
return VINF_SUCCESS;
}
/**
* The device registration structure.
*/
const PDMDEVREG g_DeviceVirtioNet =
{
/* Structure version. PDM_DEVREG_VERSION defines the current version. */
PDM_DEVREG_VERSION,
/* Device name. */
"virtio-net",
/* Name of guest context module (no path).
* Only evalutated if PDM_DEVREG_FLAGS_RC is set. */
"VBoxDDGC.gc",
/* Name of ring-0 module (no path).
* Only evalutated if PDM_DEVREG_FLAGS_RC is set. */
"VBoxDDR0.r0",
/* The description of the device. The UTF-8 string pointed to shall, like this structure,
* remain unchanged from registration till VM destruction. */
"Virtio Ethernet.\n",
/* Flags, combination of the PDM_DEVREG_FLAGS_* \#defines. */
#ifdef VNET_GC_SUPPORT
PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
#else
PDM_DEVREG_FLAGS_DEFAULT_BITS,
#endif
/* Device class(es), combination of the PDM_DEVREG_CLASS_* \#defines. */
PDM_DEVREG_CLASS_NETWORK,
/* Maximum number of instances (per VM). */
8,
/* Size of the instance data. */
sizeof(VNETSTATE),
/* Construct instance - required. */
vnetConstruct,
/* Destruct instance - optional. */
vnetDestruct,
/* Relocation command - optional. */
vnetRelocate,
/* I/O Control interface - optional. */
NULL,
/* Power on notification - optional. */
NULL,
/* Reset notification - optional. */
NULL,
/* Suspend notification - optional. */
vnetSuspend,
/* Resume notification - optional. */
NULL,
#ifdef VBOX_DYNAMIC_NET_ATTACH
/* Attach command - optional. */
vnetAttach,
/* Detach notification - optional. */
vnetDetach,
#else /* !VBOX_DYNAMIC_NET_ATTACH */
/* Attach command - optional. */
NULL,
/* Detach notification - optional. */
NULL,
#endif /* !VBOX_DYNAMIC_NET_ATTACH */
/* Query a LUN base interface - optional. */
NULL,
/* Init complete notification - optional. */
NULL,
/* Power off notification - optional. */
vnetPowerOff,
/* pfnSoftReset */
NULL,
/* u32VersionEnd */
PDM_DEVREG_VERSION
};
#endif /* IN_RING3 */
#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */