VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/DevVirtioNet.cpp@ 37224

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

virtio-net: fixed the issue with TX performance with newer linux kernels

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 74.3 KB
Line 
1/* $Id: DevVirtioNet.cpp 35387 2010-12-31 18:42:48Z vboxsync $ */
2/** @file
3 * DevVirtioNet - Virtio Network Device
4 */
5
6/*
7 * Copyright (C) 2009-2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19#define LOG_GROUP LOG_GROUP_DEV_VIRTIO_NET
20#define VNET_GC_SUPPORT
21#define VNET_WITH_GSO
22#define VNET_WITH_MERGEABLE_RX_BUFS
23
24#include <VBox/vmm/pdmdev.h>
25#include <VBox/vmm/pdmnetifs.h>
26#include <iprt/asm.h>
27#include <iprt/net.h>
28#include <iprt/semaphore.h>
29#ifdef IN_RING3
30# include <iprt/mem.h>
31# include <iprt/uuid.h>
32#endif /* IN_RING3 */
33#include "VBoxDD.h"
34#include "../VirtIO/Virtio.h"
35
36
37#ifndef VBOX_DEVICE_STRUCT_TESTCASE
38
39#define INSTANCE(pState) pState->VPCI.szInstance
40#define STATUS pState->config.uStatus
41
42#ifdef IN_RING3
43
44#define VNET_PCI_SUBSYSTEM_ID 1 + VIRTIO_NET_ID
45#define VNET_PCI_CLASS 0x0200
46#define VNET_N_QUEUES 3
47#define VNET_NAME_FMT "VNet%d"
48
49#if 0
50/* Virtio Block Device */
51#define VNET_PCI_SUBSYSTEM_ID 1 + VIRTIO_BLK_ID
52#define VNET_PCI_CLASS 0x0180
53#define VNET_N_QUEUES 2
54#define VNET_NAME_FMT "VBlk%d"
55#endif
56
57#endif /* IN_RING3 */
58
59/* Forward declarations ******************************************************/
60RT_C_DECLS_BEGIN
61PDMBOTHCBDECL(int) vnetIOPortIn (PPDMDEVINS pDevIns, void *pvUser, RTIOPORT port, uint32_t *pu32, unsigned cb);
62PDMBOTHCBDECL(int) vnetIOPortOut(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT port, uint32_t u32, unsigned cb);
63RT_C_DECLS_END
64
65#endif /* VBOX_DEVICE_STRUCT_TESTCASE */
66
67
68#define VNET_TX_DELAY 150 /* 150 microseconds */
69#define VNET_MAX_FRAME_SIZE 65536 // TODO: Is it the right limit?
70#define VNET_MAC_FILTER_LEN 32
71#define VNET_MAX_VID (1 << 12)
72
73/* Virtio net features */
74#define VNET_F_CSUM 0x00000001 /* Host handles pkts w/ partial csum */
75#define VNET_F_GUEST_CSUM 0x00000002 /* Guest handles pkts w/ partial csum */
76#define VNET_F_MAC 0x00000020 /* Host has given MAC address. */
77#define VNET_F_GSO 0x00000040 /* Host handles pkts w/ any GSO type */
78#define VNET_F_GUEST_TSO4 0x00000080 /* Guest can handle TSOv4 in. */
79#define VNET_F_GUEST_TSO6 0x00000100 /* Guest can handle TSOv6 in. */
80#define VNET_F_GUEST_ECN 0x00000200 /* Guest can handle TSO[6] w/ ECN in. */
81#define VNET_F_GUEST_UFO 0x00000400 /* Guest can handle UFO in. */
82#define VNET_F_HOST_TSO4 0x00000800 /* Host can handle TSOv4 in. */
83#define VNET_F_HOST_TSO6 0x00001000 /* Host can handle TSOv6 in. */
84#define VNET_F_HOST_ECN 0x00002000 /* Host can handle TSO[6] w/ ECN in. */
85#define VNET_F_HOST_UFO 0x00004000 /* Host can handle UFO in. */
86#define VNET_F_MRG_RXBUF 0x00008000 /* Host can merge receive buffers. */
87#define VNET_F_STATUS 0x00010000 /* virtio_net_config.status available */
88#define VNET_F_CTRL_VQ 0x00020000 /* Control channel available */
89#define VNET_F_CTRL_RX 0x00040000 /* Control channel RX mode support */
90#define VNET_F_CTRL_VLAN 0x00080000 /* Control channel VLAN filtering */
91
92#define VNET_S_LINK_UP 1
93
94
95#ifdef _MSC_VER
96struct VNetPCIConfig
97#else /* !_MSC_VER */
98struct __attribute__ ((__packed__)) VNetPCIConfig
99#endif /* !_MSC_VER */
100{
101 RTMAC mac;
102 uint16_t uStatus;
103};
104AssertCompileMemberOffset(struct VNetPCIConfig, uStatus, 6);
105
106/**
107 * Device state structure. Holds the current state of device.
108 *
109 * @extends VPCISTATE
110 * @implements PDMINETWORKDOWN
111 * @implements PDMINETWORKCONFIG
112 */
113struct VNetState_st
114{
115 /* VPCISTATE must be the first member! */
116 VPCISTATE VPCI;
117
118// PDMCRITSECT csRx; /**< Protects RX queue. */
119
120 PDMINETWORKDOWN INetworkDown;
121 PDMINETWORKCONFIG INetworkConfig;
122 R3PTRTYPE(PPDMIBASE) pDrvBase; /**< Attached network driver. */
123 R3PTRTYPE(PPDMINETWORKUP) pDrv; /**< Connector of attached network driver. */
124
125 R3PTRTYPE(PPDMQUEUE) pCanRxQueueR3; /**< Rx wakeup signaller - R3. */
126 R0PTRTYPE(PPDMQUEUE) pCanRxQueueR0; /**< Rx wakeup signaller - R0. */
127 RCPTRTYPE(PPDMQUEUE) pCanRxQueueRC; /**< Rx wakeup signaller - RC. */
128# if HC_ARCH_BITS == 64
129 uint32_t padding;
130# endif
131
132 /**< Link Up(/Restore) Timer. */
133 PTMTIMERR3 pLinkUpTimer;
134
135#ifdef VNET_TX_DELAY
136 /**< Transmit Delay Timer - R3. */
137 PTMTIMERR3 pTxTimerR3;
138 /**< Transmit Delay Timer - R0. */
139 PTMTIMERR0 pTxTimerR0;
140 /**< Transmit Delay Timer - GC. */
141 PTMTIMERRC pTxTimerRC;
142
143# if HC_ARCH_BITS == 64
144 uint32_t padding2;
145# endif
146
147 uint32_t u32i;
148 uint32_t u32AvgDiff;
149 uint32_t u32MinDiff;
150 uint32_t u32MaxDiff;
151 uint64_t u64NanoTS;
152#endif /* VNET_TX_DELAY */
153
154 /** Indicates transmission in progress -- only one thread is allowed. */
155 uint32_t uIsTransmitting;
156
157 /** PCI config area holding MAC address as well as TBD. */
158 struct VNetPCIConfig config;
159 /** MAC address obtained from the configuration. */
160 RTMAC macConfigured;
161 /** True if physical cable is attached in configuration. */
162 bool fCableConnected;
163
164 /** Number of packet being sent/received to show in debug log. */
165 uint32_t u32PktNo;
166
167 /** N/A: */
168 bool volatile fMaybeOutOfSpace;
169
170 /** Promiscuous mode -- RX filter accepts all packets. */
171 bool fPromiscuous;
172 /** AllMulti mode -- RX filter accepts all multicast packets. */
173 bool fAllMulti;
174 /** The number of actually used slots in aMacTable. */
175 uint32_t nMacFilterEntries;
176 /** Array of MAC addresses accepted by RX filter. */
177 RTMAC aMacFilter[VNET_MAC_FILTER_LEN];
178 /** Bit array of VLAN filter, one bit per VLAN ID. */
179 uint8_t aVlanFilter[VNET_MAX_VID / sizeof(uint8_t)];
180
181 R3PTRTYPE(PVQUEUE) pRxQueue;
182 R3PTRTYPE(PVQUEUE) pTxQueue;
183 R3PTRTYPE(PVQUEUE) pCtlQueue;
184 /* Receive-blocking-related fields ***************************************/
185
186 /** EMT: Gets signalled when more RX descriptors become available. */
187 RTSEMEVENT hEventMoreRxDescAvail;
188
189 /* Statistic fields ******************************************************/
190
191 STAMCOUNTER StatReceiveBytes;
192 STAMCOUNTER StatTransmitBytes;
193 STAMCOUNTER StatReceiveGSO;
194 STAMCOUNTER StatTransmitPackets;
195 STAMCOUNTER StatTransmitGSO;
196 STAMCOUNTER StatTransmitCSum;
197#if defined(VBOX_WITH_STATISTICS)
198 STAMPROFILE StatReceive;
199 STAMPROFILE StatReceiveStore;
200 STAMPROFILEADV StatTransmit;
201 STAMPROFILE StatTransmitSend;
202 STAMPROFILE StatRxOverflow;
203 STAMCOUNTER StatRxOverflowWakeup;
204#endif /* VBOX_WITH_STATISTICS */
205
206};
207typedef struct VNetState_st VNETSTATE;
208typedef VNETSTATE *PVNETSTATE;
209
210#ifndef VBOX_DEVICE_STRUCT_TESTCASE
211
212#define VNETHDR_F_NEEDS_CSUM 1 // Use u16CSumStart, u16CSumOffset
213
214#define VNETHDR_GSO_NONE 0 // Not a GSO frame
215#define VNETHDR_GSO_TCPV4 1 // GSO frame, IPv4 TCP (TSO)
216#define VNETHDR_GSO_UDP 3 // GSO frame, IPv4 UDP (UFO)
217#define VNETHDR_GSO_TCPV6 4 // GSO frame, IPv6 TCP
218#define VNETHDR_GSO_ECN 0x80 // TCP has ECN set
219
220struct VNetHdr
221{
222 uint8_t u8Flags;
223 uint8_t u8GSOType;
224 uint16_t u16HdrLen;
225 uint16_t u16GSOSize;
226 uint16_t u16CSumStart;
227 uint16_t u16CSumOffset;
228};
229typedef struct VNetHdr VNETHDR;
230typedef VNETHDR *PVNETHDR;
231AssertCompileSize(VNETHDR, 10);
232
233struct VNetHdrMrx
234{
235 VNETHDR Hdr;
236 uint16_t u16NumBufs;
237};
238typedef struct VNetHdrMrx VNETHDRMRX;
239typedef VNETHDRMRX *PVNETHDRMRX;
240AssertCompileSize(VNETHDRMRX, 12);
241
242AssertCompileMemberOffset(VNETSTATE, VPCI, 0);
243
244#define VNET_OK 0
245#define VNET_ERROR 1
246typedef uint8_t VNETCTLACK;
247
248#define VNET_CTRL_CLS_RX_MODE 0
249#define VNET_CTRL_CMD_RX_MODE_PROMISC 0
250#define VNET_CTRL_CMD_RX_MODE_ALLMULTI 1
251
252#define VNET_CTRL_CLS_MAC 1
253#define VNET_CTRL_CMD_MAC_TABLE_SET 0
254
255#define VNET_CTRL_CLS_VLAN 2
256#define VNET_CTRL_CMD_VLAN_ADD 0
257#define VNET_CTRL_CMD_VLAN_DEL 1
258
259
260struct VNetCtlHdr
261{
262 uint8_t u8Class;
263 uint8_t u8Command;
264};
265typedef struct VNetCtlHdr VNETCTLHDR;
266typedef VNETCTLHDR *PVNETCTLHDR;
267AssertCompileSize(VNETCTLHDR, 2);
268
269/* Returns true if large packets are written into several RX buffers. */
270DECLINLINE(bool) vnetMergeableRxBuffers(PVNETSTATE pState)
271{
272 return !!(pState->VPCI.uGuestFeatures & VNET_F_MRG_RXBUF);
273}
274
275DECLINLINE(int) vnetCsEnter(PVNETSTATE pState, int rcBusy)
276{
277 return vpciCsEnter(&pState->VPCI, rcBusy);
278}
279
280DECLINLINE(void) vnetCsLeave(PVNETSTATE pState)
281{
282 vpciCsLeave(&pState->VPCI);
283}
284
285DECLINLINE(int) vnetCsRxEnter(PVNETSTATE pState, int rcBusy)
286{
287 // STAM_PROFILE_START(&pState->CTXSUFF(StatCsRx), a);
288 // int rc = PDMCritSectEnter(&pState->csRx, rcBusy);
289 // STAM_PROFILE_STOP(&pState->CTXSUFF(StatCsRx), a);
290 // return rc;
291 return VINF_SUCCESS;
292}
293
294DECLINLINE(void) vnetCsRxLeave(PVNETSTATE pState)
295{
296 // PDMCritSectLeave(&pState->csRx);
297}
298
299/**
300 * Dump a packet to debug log.
301 *
302 * @param pState The device state structure.
303 * @param cpPacket The packet.
304 * @param cb The size of the packet.
305 * @param cszText A string denoting direction of packet transfer.
306 */
307DECLINLINE(void) vnetPacketDump(PVNETSTATE pState, const uint8_t *cpPacket, size_t cb, const char *cszText)
308{
309#ifdef DEBUG
310 Log(("%s %s packet #%d (%d bytes):\n",
311 INSTANCE(pState), cszText, ++pState->u32PktNo, cb));
312 Log3(("%.*Rhxd\n", cb, cpPacket));
313#endif
314}
315
316
317
318PDMBOTHCBDECL(uint32_t) vnetGetHostFeatures(void *pvState)
319{
320 /* We support:
321 * - Host-provided MAC address
322 * - Link status reporting in config space
323 * - Control queue
324 * - RX mode setting
325 * - MAC filter table
326 * - VLAN filter
327 */
328 return VNET_F_MAC
329 | VNET_F_STATUS
330 | VNET_F_CTRL_VQ
331 | VNET_F_CTRL_RX
332 | VNET_F_CTRL_VLAN
333#ifdef VNET_WITH_GSO
334 | VNET_F_CSUM
335 | VNET_F_HOST_TSO4
336 | VNET_F_HOST_TSO6
337 | VNET_F_HOST_UFO
338#endif
339#ifdef VNET_WITH_MERGEABLE_RX_BUFS
340 | VNET_F_MRG_RXBUF
341#endif
342 ;
343}
344
345PDMBOTHCBDECL(uint32_t) vnetGetHostMinimalFeatures(void *pvState)
346{
347 return VNET_F_MAC;
348}
349
350PDMBOTHCBDECL(void) vnetSetHostFeatures(void *pvState, uint32_t uFeatures)
351{
352 // TODO: Nothing to do here yet
353 VNETSTATE *pState = (VNETSTATE *)pvState;
354 LogFlow(("%s vnetSetHostFeatures: uFeatures=%x\n", INSTANCE(pState), uFeatures));
355}
356
357PDMBOTHCBDECL(int) vnetGetConfig(void *pvState, uint32_t port, uint32_t cb, void *data)
358{
359 VNETSTATE *pState = (VNETSTATE *)pvState;
360 if (port + cb > sizeof(struct VNetPCIConfig))
361 {
362 Log(("%s vnetGetConfig: Read beyond the config structure is attempted (port=%RTiop cb=%x).\n", INSTANCE(pState), port, cb));
363 return VERR_IOM_IOPORT_UNUSED;
364 }
365 memcpy(data, ((uint8_t*)&pState->config) + port, cb);
366 return VINF_SUCCESS;
367}
368
369PDMBOTHCBDECL(int) vnetSetConfig(void *pvState, uint32_t port, uint32_t cb, void *data)
370{
371 VNETSTATE *pState = (VNETSTATE *)pvState;
372 if (port + cb > sizeof(struct VNetPCIConfig))
373 {
374 Log(("%s vnetGetConfig: Write beyond the config structure is attempted (port=%RTiop cb=%x).\n", INSTANCE(pState), port, cb));
375 if (port < sizeof(struct VNetPCIConfig))
376 memcpy(((uint8_t*)&pState->config) + port, data,
377 sizeof(struct VNetPCIConfig) - port);
378 return VINF_SUCCESS;
379 }
380 memcpy(((uint8_t*)&pState->config) + port, data, cb);
381 return VINF_SUCCESS;
382}
383
384/**
385 * Hardware reset. Revert all registers to initial values.
386 *
387 * @param pState The device state structure.
388 */
389PDMBOTHCBDECL(int) vnetReset(void *pvState)
390{
391 VNETSTATE *pState = (VNETSTATE*)pvState;
392 Log(("%s Reset triggered\n", INSTANCE(pState)));
393
394 int rc = vnetCsRxEnter(pState, VERR_SEM_BUSY);
395 if (RT_UNLIKELY(rc != VINF_SUCCESS))
396 {
397 LogRel(("vnetReset failed to enter RX critical section!\n"));
398 return rc;
399 }
400 vpciReset(&pState->VPCI);
401 vnetCsRxLeave(pState);
402
403 // TODO: Implement reset
404 if (pState->fCableConnected)
405 STATUS = VNET_S_LINK_UP;
406 else
407 STATUS = 0;
408
409 /*
410 * By default we pass all packets up since the older guests cannot control
411 * virtio mode.
412 */
413 pState->fPromiscuous = true;
414 pState->fAllMulti = false;
415 pState->nMacFilterEntries = 0;
416 memset(pState->aMacFilter, 0, VNET_MAC_FILTER_LEN * sizeof(RTMAC));
417 memset(pState->aVlanFilter, 0, sizeof(pState->aVlanFilter));
418 pState->uIsTransmitting = 0;
419#ifndef IN_RING3
420 return VINF_IOM_HC_IOPORT_WRITE;
421#else
422 if (pState->pDrv)
423 pState->pDrv->pfnSetPromiscuousMode(pState->pDrv, true);
424 return VINF_SUCCESS;
425#endif
426}
427
428#ifdef IN_RING3
429
430/**
431 * Wakeup the RX thread.
432 */
433static void vnetWakeupReceive(PPDMDEVINS pDevIns)
434{
435 VNETSTATE *pState = PDMINS_2_DATA(pDevIns, VNETSTATE *);
436 if ( pState->fMaybeOutOfSpace
437 && pState->hEventMoreRxDescAvail != NIL_RTSEMEVENT)
438 {
439 STAM_COUNTER_INC(&pState->StatRxOverflowWakeup);
440 Log(("%s Waking up Out-of-RX-space semaphore\n", INSTANCE(pState)));
441 RTSemEventSignal(pState->hEventMoreRxDescAvail);
442 }
443}
444
445/**
446 * Link Up Timer handler.
447 *
448 * @param pDevIns Pointer to device instance structure.
449 * @param pTimer Pointer to the timer.
450 * @param pvUser NULL.
451 * @thread EMT
452 */
453static DECLCALLBACK(void) vnetLinkUpTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
454{
455 VNETSTATE *pState = (VNETSTATE *)pvUser;
456
457 int rc = vnetCsEnter(pState, VERR_SEM_BUSY);
458 if (RT_UNLIKELY(rc != VINF_SUCCESS))
459 return;
460 STATUS |= VNET_S_LINK_UP;
461 vpciRaiseInterrupt(&pState->VPCI, VERR_SEM_BUSY, VPCI_ISR_CONFIG);
462 vnetWakeupReceive(pDevIns);
463 vnetCsLeave(pState);
464}
465
466
467
468
469/**
470 * Handler for the wakeup signaller queue.
471 */
472static DECLCALLBACK(bool) vnetCanRxQueueConsumer(PPDMDEVINS pDevIns, PPDMQUEUEITEMCORE pItem)
473{
474 vnetWakeupReceive(pDevIns);
475 return true;
476}
477
478#endif /* IN_RING3 */
479
480/**
481 * This function is called when the driver becomes ready.
482 *
483 * @param pState The device state structure.
484 */
485PDMBOTHCBDECL(void) vnetReady(void *pvState)
486{
487 VNETSTATE *pState = (VNETSTATE*)pvState;
488 Log(("%s Driver became ready, waking up RX thread...\n", INSTANCE(pState)));
489#ifdef IN_RING3
490 vnetWakeupReceive(pState->VPCI.CTX_SUFF(pDevIns));
491#else
492 PPDMQUEUEITEMCORE pItem = PDMQueueAlloc(pState->CTX_SUFF(pCanRxQueue));
493 if (pItem)
494 PDMQueueInsert(pState->CTX_SUFF(pCanRxQueue), pItem);
495#endif
496}
497
498/**
499 * Port I/O Handler for IN operations.
500 *
501 * @returns VBox status code.
502 *
503 * @param pDevIns The device instance.
504 * @param pvUser Pointer to the device state structure.
505 * @param port Port number used for the IN operation.
506 * @param pu32 Where to store the result.
507 * @param cb Number of bytes read.
508 * @thread EMT
509 */
510PDMBOTHCBDECL(int) vnetIOPortIn(PPDMDEVINS pDevIns, void *pvUser,
511 RTIOPORT port, uint32_t *pu32, unsigned cb)
512{
513 return vpciIOPortIn(pDevIns, pvUser, port, pu32, cb,
514 vnetGetHostFeatures,
515 vnetGetConfig);
516}
517
518
519/**
520 * Port I/O Handler for OUT operations.
521 *
522 * @returns VBox status code.
523 *
524 * @param pDevIns The device instance.
525 * @param pvUser User argument.
526 * @param Port Port number used for the IN operation.
527 * @param u32 The value to output.
528 * @param cb The value size in bytes.
529 * @thread EMT
530 */
531PDMBOTHCBDECL(int) vnetIOPortOut(PPDMDEVINS pDevIns, void *pvUser,
532 RTIOPORT port, uint32_t u32, unsigned cb)
533{
534 return vpciIOPortOut(pDevIns, pvUser, port, u32, cb,
535 vnetGetHostMinimalFeatures,
536 vnetGetHostFeatures,
537 vnetSetHostFeatures,
538 vnetReset,
539 vnetReady,
540 vnetSetConfig);
541}
542
543
544#ifdef IN_RING3
545
546/**
547 * Check if the device can receive data now.
548 * This must be called before the pfnRecieve() method is called.
549 *
550 * @remarks As a side effect this function enables queue notification
551 * if it cannot receive because the queue is empty.
552 * It disables notification if it can receive.
553 *
554 * @returns VERR_NET_NO_BUFFER_SPACE if it cannot.
555 * @param pInterface Pointer to the interface structure containing the called function pointer.
556 * @thread RX
557 */
558static int vnetCanReceive(VNETSTATE *pState)
559{
560 int rc = vnetCsRxEnter(pState, VERR_SEM_BUSY);
561 AssertRCReturn(rc, rc);
562
563 LogFlow(("%s vnetCanReceive\n", INSTANCE(pState)));
564 if (!(pState->VPCI.uStatus & VPCI_STATUS_DRV_OK))
565 rc = VERR_NET_NO_BUFFER_SPACE;
566 else if (!vqueueIsReady(&pState->VPCI, pState->pRxQueue))
567 rc = VERR_NET_NO_BUFFER_SPACE;
568 else if (vqueueIsEmpty(&pState->VPCI, pState->pRxQueue))
569 {
570 vringSetNotification(&pState->VPCI, &pState->pRxQueue->VRing, true);
571 rc = VERR_NET_NO_BUFFER_SPACE;
572 }
573 else
574 {
575 vringSetNotification(&pState->VPCI, &pState->pRxQueue->VRing, false);
576 rc = VINF_SUCCESS;
577 }
578
579 LogFlow(("%s vnetCanReceive -> %Rrc\n", INSTANCE(pState), rc));
580 vnetCsRxLeave(pState);
581 return rc;
582}
583
584/**
585 * @interface_method_impl{PDMINETWORKDOWN,pfnWaitReceiveAvail}
586 */
587static DECLCALLBACK(int) vnetNetworkDown_WaitReceiveAvail(PPDMINETWORKDOWN pInterface, RTMSINTERVAL cMillies)
588{
589 VNETSTATE *pState = RT_FROM_MEMBER(pInterface, VNETSTATE, INetworkDown);
590 LogFlow(("%s vnetNetworkDown_WaitReceiveAvail(cMillies=%u)\n", INSTANCE(pState), cMillies));
591 int rc = vnetCanReceive(pState);
592
593 if (RT_SUCCESS(rc))
594 return VINF_SUCCESS;
595 if (RT_UNLIKELY(cMillies == 0))
596 return VERR_NET_NO_BUFFER_SPACE;
597
598 rc = VERR_INTERRUPTED;
599 ASMAtomicXchgBool(&pState->fMaybeOutOfSpace, true);
600 STAM_PROFILE_START(&pState->StatRxOverflow, a);
601
602 VMSTATE enmVMState;
603 while (RT_LIKELY( (enmVMState = PDMDevHlpVMState(pState->VPCI.CTX_SUFF(pDevIns))) == VMSTATE_RUNNING
604 || enmVMState == VMSTATE_RUNNING_LS))
605 {
606 int rc2 = vnetCanReceive(pState);
607 if (RT_SUCCESS(rc2))
608 {
609 rc = VINF_SUCCESS;
610 break;
611 }
612 Log(("%s vnetNetworkDown_WaitReceiveAvail: waiting cMillies=%u...\n",
613 INSTANCE(pState), cMillies));
614 RTSemEventWait(pState->hEventMoreRxDescAvail, cMillies);
615 }
616 STAM_PROFILE_STOP(&pState->StatRxOverflow, a);
617 ASMAtomicXchgBool(&pState->fMaybeOutOfSpace, false);
618
619 LogFlow(("%s vnetNetworkDown_WaitReceiveAvail -> %d\n", INSTANCE(pState), rc));
620 return rc;
621}
622
623
624/**
625 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
626 */
627static DECLCALLBACK(void *) vnetQueryInterface(struct PDMIBASE *pInterface, const char *pszIID)
628{
629 VNETSTATE *pThis = RT_FROM_MEMBER(pInterface, VNETSTATE, VPCI.IBase);
630 Assert(&pThis->VPCI.IBase == pInterface);
631
632 PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKDOWN, &pThis->INetworkDown);
633 PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKCONFIG, &pThis->INetworkConfig);
634 return vpciQueryInterface(pInterface, pszIID);
635}
636
637/**
638 * Returns true if it is a broadcast packet.
639 *
640 * @returns true if destination address indicates broadcast.
641 * @param pvBuf The ethernet packet.
642 */
643DECLINLINE(bool) vnetIsBroadcast(const void *pvBuf)
644{
645 static const uint8_t s_abBcastAddr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
646 return memcmp(pvBuf, s_abBcastAddr, sizeof(s_abBcastAddr)) == 0;
647}
648
649/**
650 * Returns true if it is a multicast packet.
651 *
652 * @remarks returns true for broadcast packets as well.
653 * @returns true if destination address indicates multicast.
654 * @param pvBuf The ethernet packet.
655 */
656DECLINLINE(bool) vnetIsMulticast(const void *pvBuf)
657{
658 return (*(char*)pvBuf) & 1;
659}
660
661/**
662 * Determines if the packet is to be delivered to upper layer.
663 *
664 * @returns true if packet is intended for this node.
665 * @param pState Pointer to the state structure.
666 * @param pvBuf The ethernet packet.
667 * @param cb Number of bytes available in the packet.
668 */
669static bool vnetAddressFilter(PVNETSTATE pState, const void *pvBuf, size_t cb)
670{
671 if (pState->fPromiscuous)
672 return true;
673
674 /* Ignore everything outside of our VLANs */
675 uint16_t *u16Ptr = (uint16_t*)pvBuf;
676 /* Compare TPID with VLAN Ether Type */
677 if ( u16Ptr[6] == RT_H2BE_U16(0x8100)
678 && !ASMBitTest(pState->aVlanFilter, RT_BE2H_U16(u16Ptr[7]) & 0xFFF))
679 {
680 Log4(("%s vnetAddressFilter: not our VLAN, returning false\n", INSTANCE(pState)));
681 return false;
682 }
683
684 if (vnetIsBroadcast(pvBuf))
685 return true;
686
687 if (pState->fAllMulti && vnetIsMulticast(pvBuf))
688 return true;
689
690 if (!memcmp(pState->config.mac.au8, pvBuf, sizeof(RTMAC)))
691 return true;
692 Log4(("%s vnetAddressFilter: %RTmac (conf) != %RTmac (dest)\n",
693 INSTANCE(pState), pState->config.mac.au8, pvBuf));
694
695 for (unsigned i = 0; i < pState->nMacFilterEntries; i++)
696 if (!memcmp(&pState->aMacFilter[i], pvBuf, sizeof(RTMAC)))
697 return true;
698
699 Log2(("%s vnetAddressFilter: failed all tests, returning false, packet dump follows:\n", INSTANCE(pState)));
700 vnetPacketDump(pState, (const uint8_t*)pvBuf, cb, "<-- Incoming");
701
702 return false;
703}
704
705/**
706 * Pad and store received packet.
707 *
708 * @remarks Make sure that the packet appears to upper layer as one coming
709 * from real Ethernet: pad it and insert FCS.
710 *
711 * @returns VBox status code.
712 * @param pState The device state structure.
713 * @param pvBuf The available data.
714 * @param cb Number of bytes available in the buffer.
715 * @thread RX
716 */
717static int vnetHandleRxPacket(PVNETSTATE pState, const void *pvBuf, size_t cb,
718 PCPDMNETWORKGSO pGso)
719{
720 VNETHDRMRX Hdr;
721 PVNETHDRMRX pHdr;
722 unsigned uHdrLen;
723 RTGCPHYS addrHdrMrx = 0;
724
725 if (pGso)
726 {
727 Log2(("%s vnetHandleRxPacket: gso type=%x cbHdr=%u mss=%u"
728 " off1=0x%x off2=0x%x\n", INSTANCE(pState), pGso->u8Type,
729 pGso->cbHdrs, pGso->cbMaxSeg, pGso->offHdr1, pGso->offHdr2));
730 Hdr.Hdr.u8Flags = VNETHDR_F_NEEDS_CSUM;
731 switch (pGso->u8Type)
732 {
733 case PDMNETWORKGSOTYPE_IPV4_TCP:
734 Hdr.Hdr.u8GSOType = VNETHDR_GSO_TCPV4;
735 Hdr.Hdr.u16CSumOffset = RT_OFFSETOF(RTNETTCP, th_sum);
736 break;
737 case PDMNETWORKGSOTYPE_IPV6_TCP:
738 Hdr.Hdr.u8GSOType = VNETHDR_GSO_TCPV6;
739 Hdr.Hdr.u16CSumOffset = RT_OFFSETOF(RTNETTCP, th_sum);
740 break;
741 case PDMNETWORKGSOTYPE_IPV4_UDP:
742 Hdr.Hdr.u8GSOType = VNETHDR_GSO_UDP;
743 Hdr.Hdr.u16CSumOffset = RT_OFFSETOF(RTNETUDP, uh_sum);
744 break;
745 default:
746 return VERR_INVALID_PARAMETER;
747 }
748 Hdr.Hdr.u16HdrLen = pGso->cbHdrs;
749 Hdr.Hdr.u16GSOSize = pGso->cbMaxSeg;
750 Hdr.Hdr.u16CSumStart = pGso->offHdr2;
751 STAM_REL_COUNTER_INC(&pState->StatReceiveGSO);
752 }
753 else
754 {
755 Hdr.Hdr.u8Flags = 0;
756 Hdr.Hdr.u8GSOType = VNETHDR_GSO_NONE;
757 }
758
759 if (vnetMergeableRxBuffers(pState))
760 uHdrLen = sizeof(VNETHDRMRX);
761 else
762 uHdrLen = sizeof(VNETHDR);
763
764 vnetPacketDump(pState, (const uint8_t*)pvBuf, cb, "<-- Incoming");
765
766 unsigned int uOffset = 0;
767 unsigned int nElem;
768 for (nElem = 0; uOffset < cb; nElem++)
769 {
770 VQUEUEELEM elem;
771 unsigned int nSeg = 0, uElemSize = 0, cbReserved = 0;
772
773 if (!vqueueGet(&pState->VPCI, pState->pRxQueue, &elem))
774 {
775 /*
776 * @todo: It is possible to run out of RX buffers if only a few
777 * were added and we received a big packet.
778 */
779 Log(("%s vnetHandleRxPacket: Suddenly there is no space in receive queue!\n", INSTANCE(pState)));
780 return VERR_INTERNAL_ERROR;
781 }
782
783 if (elem.nIn < 1)
784 {
785 Log(("%s vnetHandleRxPacket: No writable descriptors in receive queue!\n", INSTANCE(pState)));
786 return VERR_INTERNAL_ERROR;
787 }
788
789 if (nElem == 0)
790 {
791 if (vnetMergeableRxBuffers(pState))
792 {
793 addrHdrMrx = elem.aSegsIn[nSeg].addr;
794 cbReserved = uHdrLen;
795 }
796 else
797 {
798 /* The very first segment of the very first element gets the header. */
799 if (elem.aSegsIn[nSeg].cb != sizeof(VNETHDR))
800 {
801 Log(("%s vnetHandleRxPacket: The first descriptor does match the header size!\n", INSTANCE(pState)));
802 return VERR_INTERNAL_ERROR;
803 }
804 elem.aSegsIn[nSeg++].pv = &Hdr;
805 }
806 uElemSize += uHdrLen;
807 }
808 while (nSeg < elem.nIn && uOffset < cb)
809 {
810 unsigned int uSize = (unsigned int)RT_MIN(elem.aSegsIn[nSeg].cb - (nSeg?0:cbReserved),
811 cb - uOffset);
812 elem.aSegsIn[nSeg++].pv = (uint8_t*)pvBuf + uOffset;
813 uOffset += uSize;
814 uElemSize += uSize;
815 }
816 STAM_PROFILE_START(&pState->StatReceiveStore, a);
817 vqueuePut(&pState->VPCI, pState->pRxQueue, &elem, uElemSize, cbReserved);
818 STAM_PROFILE_STOP(&pState->StatReceiveStore, a);
819 if (!vnetMergeableRxBuffers(pState))
820 break;
821 cbReserved = 0;
822 }
823 if (vnetMergeableRxBuffers(pState))
824 {
825 Hdr.u16NumBufs = nElem;
826 int rc = PDMDevHlpPhysWrite(pState->VPCI.CTX_SUFF(pDevIns), addrHdrMrx,
827 &Hdr, sizeof(Hdr));
828 if (RT_FAILURE(rc))
829 {
830 Log(("%s vnetHandleRxPacket: Failed to write merged RX buf header: %Rrc\n",
831 INSTANCE(pState), rc));
832 return rc;
833 }
834 }
835 vqueueSync(&pState->VPCI, pState->pRxQueue);
836 if (uOffset < cb)
837 {
838 Log(("%s vnetHandleRxPacket: Packet did not fit into RX queue (packet size=%u)!\n",
839 INSTANCE(pState), cb));
840 return VERR_TOO_MUCH_DATA;
841 }
842
843 return VINF_SUCCESS;
844}
845
846/**
847 * @interface_method_impl{PDMINETWORKDOWN,pfnReceiveGso}
848 */
849static DECLCALLBACK(int) vnetNetworkDown_ReceiveGso(PPDMINETWORKDOWN pInterface,
850 const void *pvBuf, size_t cb,
851 PCPDMNETWORKGSO pGso)
852{
853 VNETSTATE *pState = RT_FROM_MEMBER(pInterface, VNETSTATE, INetworkDown);
854
855 Log2(("%s vnetNetworkDown_ReceiveGso: pvBuf=%p cb=%u pGso=%p\n",
856 INSTANCE(pState), pvBuf, cb, pGso));
857 int rc = vnetCanReceive(pState);
858 if (RT_FAILURE(rc))
859 return rc;
860
861 /* Drop packets if VM is not running or cable is disconnected. */
862 VMSTATE enmVMState = PDMDevHlpVMState(pState->VPCI.CTX_SUFF(pDevIns));
863 if (( enmVMState != VMSTATE_RUNNING
864 && enmVMState != VMSTATE_RUNNING_LS)
865 || !(STATUS & VNET_S_LINK_UP))
866 return VINF_SUCCESS;
867
868 STAM_PROFILE_START(&pState->StatReceive, a);
869 vpciSetReadLed(&pState->VPCI, true);
870 if (vnetAddressFilter(pState, pvBuf, cb))
871 {
872 rc = vnetCsRxEnter(pState, VERR_SEM_BUSY);
873 if (RT_SUCCESS(rc))
874 {
875 rc = vnetHandleRxPacket(pState, pvBuf, cb, pGso);
876 STAM_REL_COUNTER_ADD(&pState->StatReceiveBytes, cb);
877 vnetCsRxLeave(pState);
878 }
879 }
880 vpciSetReadLed(&pState->VPCI, false);
881 STAM_PROFILE_STOP(&pState->StatReceive, a);
882 return rc;
883}
884
885/**
886 * @interface_method_impl{PDMINETWORKDOWN,pfnReceive}
887 */
888static DECLCALLBACK(int) vnetNetworkDown_Receive(PPDMINETWORKDOWN pInterface, const void *pvBuf, size_t cb)
889{
890 return vnetNetworkDown_ReceiveGso(pInterface, pvBuf, cb, NULL);
891}
892
893/**
894 * Gets the current Media Access Control (MAC) address.
895 *
896 * @returns VBox status code.
897 * @param pInterface Pointer to the interface structure containing the called function pointer.
898 * @param pMac Where to store the MAC address.
899 * @thread EMT
900 */
901static DECLCALLBACK(int) vnetGetMac(PPDMINETWORKCONFIG pInterface, PRTMAC pMac)
902{
903 VNETSTATE *pState = RT_FROM_MEMBER(pInterface, VNETSTATE, INetworkConfig);
904 memcpy(pMac, pState->config.mac.au8, sizeof(RTMAC));
905 return VINF_SUCCESS;
906}
907
908/**
909 * Gets the new link state.
910 *
911 * @returns The current link state.
912 * @param pInterface Pointer to the interface structure containing the called function pointer.
913 * @thread EMT
914 */
915static DECLCALLBACK(PDMNETWORKLINKSTATE) vnetGetLinkState(PPDMINETWORKCONFIG pInterface)
916{
917 VNETSTATE *pState = RT_FROM_MEMBER(pInterface, VNETSTATE, INetworkConfig);
918 if (STATUS & VNET_S_LINK_UP)
919 return PDMNETWORKLINKSTATE_UP;
920 return PDMNETWORKLINKSTATE_DOWN;
921}
922
923
924/**
925 * Sets the new link state.
926 *
927 * @returns VBox status code.
928 * @param pInterface Pointer to the interface structure containing the called function pointer.
929 * @param enmState The new link state
930 */
931static DECLCALLBACK(int) vnetSetLinkState(PPDMINETWORKCONFIG pInterface, PDMNETWORKLINKSTATE enmState)
932{
933 VNETSTATE *pState = RT_FROM_MEMBER(pInterface, VNETSTATE, INetworkConfig);
934 bool fOldUp = !!(STATUS & VNET_S_LINK_UP);
935 bool fNewUp = enmState == PDMNETWORKLINKSTATE_UP;
936
937 if (fNewUp != fOldUp)
938 {
939 if (fNewUp)
940 {
941 Log(("%s Link is up\n", INSTANCE(pState)));
942 STATUS |= VNET_S_LINK_UP;
943 vpciRaiseInterrupt(&pState->VPCI, VERR_SEM_BUSY, VPCI_ISR_CONFIG);
944 }
945 else
946 {
947 Log(("%s Link is down\n", INSTANCE(pState)));
948 STATUS &= ~VNET_S_LINK_UP;
949 vpciRaiseInterrupt(&pState->VPCI, VERR_SEM_BUSY, VPCI_ISR_CONFIG);
950 }
951 if (pState->pDrv)
952 pState->pDrv->pfnNotifyLinkChanged(pState->pDrv, enmState);
953 }
954 return VINF_SUCCESS;
955}
956
957static DECLCALLBACK(void) vnetQueueReceive(void *pvState, PVQUEUE pQueue)
958{
959 VNETSTATE *pState = (VNETSTATE*)pvState;
960 Log(("%s Receive buffers has been added, waking up receive thread.\n", INSTANCE(pState)));
961 vnetWakeupReceive(pState->VPCI.CTX_SUFF(pDevIns));
962}
963
964/**
965 * Sets up the GSO context according to the Virtio header.
966 *
967 * @param pGso The GSO context to setup.
968 * @param pCtx The context descriptor.
969 */
970DECLINLINE(PPDMNETWORKGSO) vnetSetupGsoCtx(PPDMNETWORKGSO pGso, VNETHDR const *pHdr)
971{
972 pGso->u8Type = PDMNETWORKGSOTYPE_INVALID;
973
974 if (pHdr->u8GSOType & VNETHDR_GSO_ECN)
975 {
976 AssertMsgFailed(("Unsupported flag in virtio header: ECN\n"));
977 return NULL;
978 }
979 switch (pHdr->u8GSOType & ~VNETHDR_GSO_ECN)
980 {
981 case VNETHDR_GSO_TCPV4:
982 pGso->u8Type = PDMNETWORKGSOTYPE_IPV4_TCP;
983 break;
984 case VNETHDR_GSO_TCPV6:
985 pGso->u8Type = PDMNETWORKGSOTYPE_IPV6_TCP;
986 break;
987 case VNETHDR_GSO_UDP:
988 pGso->u8Type = PDMNETWORKGSOTYPE_IPV4_UDP;
989 break;
990 default:
991 return NULL;
992 }
993 if (pHdr->u8Flags & VNETHDR_F_NEEDS_CSUM)
994 pGso->offHdr2 = pHdr->u16CSumStart;
995 else
996 {
997 AssertMsgFailed(("GSO without checksum offloading!\n"));
998 return NULL;
999 }
1000 pGso->offHdr1 = sizeof(RTNETETHERHDR);
1001 pGso->cbHdrs = pHdr->u16HdrLen;
1002 pGso->cbMaxSeg = pHdr->u16GSOSize;
1003 return pGso;
1004}
1005
1006DECLINLINE(uint16_t) vnetCSum16(const void *pvBuf, size_t cb)
1007{
1008 uint32_t csum = 0;
1009 uint16_t *pu16 = (uint16_t *)pvBuf;
1010
1011 while (cb > 1)
1012 {
1013 csum += *pu16++;
1014 cb -= 2;
1015 }
1016 if (cb)
1017 csum += *(uint8_t*)pu16;
1018 while (csum >> 16)
1019 csum = (csum >> 16) + (csum & 0xFFFF);
1020 return ~csum;
1021}
1022
1023DECLINLINE(void) vnetCompleteChecksum(uint8_t *pBuf, unsigned cbSize, uint16_t uStart, uint16_t uOffset)
1024{
1025 *(uint16_t*)(pBuf + uStart + uOffset) = vnetCSum16(pBuf + uStart, cbSize - uStart);
1026}
1027
1028static void vnetTransmitPendingPackets(PVNETSTATE pState, PVQUEUE pQueue, bool fOnWorkerThread)
1029{
1030 /*
1031 * Only one thread is allowed to transmit at a time, others should skip
1032 * transmission as the packets will be picked up by the transmitting
1033 * thread.
1034 */
1035 if (!ASMAtomicCmpXchgU32(&pState->uIsTransmitting, 1, 0))
1036 return;
1037
1038 if ((pState->VPCI.uStatus & VPCI_STATUS_DRV_OK) == 0)
1039 {
1040 Log(("%s Ignoring transmit requests from non-existent driver (status=0x%x).\n",
1041 INSTANCE(pState), pState->VPCI.uStatus));
1042 return;
1043 }
1044
1045 PPDMINETWORKUP pDrv = pState->pDrv;
1046 if (pDrv)
1047 {
1048 int rc = pDrv->pfnBeginXmit(pDrv, fOnWorkerThread);
1049 Assert(rc == VINF_SUCCESS || rc == VERR_TRY_AGAIN);
1050 if (rc == VERR_TRY_AGAIN)
1051 {
1052 ASMAtomicWriteU32(&pState->uIsTransmitting, 0);
1053 return;
1054 }
1055 }
1056
1057 unsigned int uHdrLen;
1058 if (vnetMergeableRxBuffers(pState))
1059 uHdrLen = sizeof(VNETHDRMRX);
1060 else
1061 uHdrLen = sizeof(VNETHDR);
1062
1063 Log3(("%s vnetTransmitPendingPackets: About to transmit %d pending packets\n", INSTANCE(pState),
1064 vringReadAvailIndex(&pState->VPCI, &pState->pTxQueue->VRing) - pState->pTxQueue->uNextAvailIndex));
1065
1066 vpciSetWriteLed(&pState->VPCI, true);
1067
1068 VQUEUEELEM elem;
1069 while (vqueueGet(&pState->VPCI, pQueue, &elem))
1070 {
1071 unsigned int uOffset = 0;
1072 if (elem.nOut < 2 || elem.aSegsOut[0].cb != uHdrLen)
1073 {
1074 Log(("%s vnetQueueTransmit: The first segment is not the header! (%u < 2 || %u != %u).\n",
1075 INSTANCE(pState), elem.nOut, elem.aSegsOut[0].cb, uHdrLen));
1076 break; /* For now we simply ignore the header, but it must be there anyway! */
1077 }
1078 else
1079 {
1080 unsigned int uSize = 0;
1081 STAM_PROFILE_ADV_START(&pState->StatTransmit, a);
1082 /* Compute total frame size. */
1083 for (unsigned int i = 1; i < elem.nOut; i++)
1084 uSize += elem.aSegsOut[i].cb;
1085 Assert(uSize <= VNET_MAX_FRAME_SIZE);
1086 if (pState->pDrv)
1087 {
1088 VNETHDR Hdr;
1089 PDMNETWORKGSO Gso, *pGso;
1090
1091 PDMDevHlpPhysRead(pState->VPCI.CTX_SUFF(pDevIns), elem.aSegsOut[0].addr,
1092 &Hdr, sizeof(Hdr));
1093
1094 STAM_REL_COUNTER_INC(&pState->StatTransmitPackets);
1095
1096 STAM_PROFILE_START(&pState->StatTransmitSend, a);
1097
1098 pGso = vnetSetupGsoCtx(&Gso, &Hdr);
1099 /** @todo Optimize away the extra copying! (lazy bird) */
1100 PPDMSCATTERGATHER pSgBuf;
1101 int rc = pState->pDrv->pfnAllocBuf(pState->pDrv, uSize, pGso, &pSgBuf);
1102 if (RT_SUCCESS(rc))
1103 {
1104 Assert(pSgBuf->cSegs == 1);
1105 /* Assemble a complete frame. */
1106 for (unsigned int i = 1; i < elem.nOut; i++)
1107 {
1108 PDMDevHlpPhysRead(pState->VPCI.CTX_SUFF(pDevIns), elem.aSegsOut[i].addr,
1109 ((uint8_t*)pSgBuf->aSegs[0].pvSeg) + uOffset,
1110 elem.aSegsOut[i].cb);
1111 uOffset += elem.aSegsOut[i].cb;
1112 }
1113 pSgBuf->cbUsed = uSize;
1114 vnetPacketDump(pState, (uint8_t*)pSgBuf->aSegs[0].pvSeg, uSize, "--> Outgoing");
1115 if (pGso)
1116 {
1117 /* Some guests (RHEL) may report HdrLen excluding transport layer header! */
1118 /*
1119 * We cannot use cdHdrs provided by the guest because of different ways
1120 * it gets filled out by different versions of kernels.
1121 */
1122 //if (pGso->cbHdrs < Hdr.u16CSumStart + Hdr.u16CSumOffset + 2)
1123 {
1124 Log4(("%s vnetTransmitPendingPackets: HdrLen before adjustment %d.\n",
1125 INSTANCE(pState), pGso->cbHdrs));
1126 switch (pGso->u8Type)
1127 {
1128 case PDMNETWORKGSOTYPE_IPV4_TCP:
1129 case PDMNETWORKGSOTYPE_IPV6_TCP:
1130 pGso->cbHdrs = Hdr.u16CSumStart +
1131 ((PRTNETTCP)(((uint8_t*)pSgBuf->aSegs[0].pvSeg) + Hdr.u16CSumStart))->th_off * 4;
1132 break;
1133 case PDMNETWORKGSOTYPE_IPV4_UDP:
1134 pGso->cbHdrs = Hdr.u16CSumStart + sizeof(RTNETUDP);
1135 break;
1136 }
1137 /* Update GSO structure embedded into the frame */
1138 ((PPDMNETWORKGSO)pSgBuf->pvUser)->cbHdrs = pGso->cbHdrs;
1139 Log4(("%s vnetTransmitPendingPackets: adjusted HdrLen to %d.\n",
1140 INSTANCE(pState), pGso->cbHdrs));
1141 }
1142 Log2(("%s vnetTransmitPendingPackets: gso type=%x cbHdr=%u mss=%u"
1143 " off1=0x%x off2=0x%x\n", INSTANCE(pState), pGso->u8Type,
1144 pGso->cbHdrs, pGso->cbMaxSeg, pGso->offHdr1, pGso->offHdr2));
1145 STAM_REL_COUNTER_INC(&pState->StatTransmitGSO);
1146 }
1147 else if (Hdr.u8Flags & VNETHDR_F_NEEDS_CSUM)
1148 {
1149 STAM_REL_COUNTER_INC(&pState->StatTransmitCSum);
1150 /*
1151 * This is not GSO frame but checksum offloading is requested.
1152 */
1153 vnetCompleteChecksum((uint8_t*)pSgBuf->aSegs[0].pvSeg, uSize,
1154 Hdr.u16CSumStart, Hdr.u16CSumOffset);
1155 }
1156
1157 rc = pState->pDrv->pfnSendBuf(pState->pDrv, pSgBuf, false);
1158 }
1159 else
1160 LogRel(("virtio-net: failed to allocate SG buffer: size=%u rc=%Rrc\n", uSize, rc));
1161
1162 STAM_PROFILE_STOP(&pState->StatTransmitSend, a);
1163 STAM_REL_COUNTER_ADD(&pState->StatTransmitBytes, uOffset);
1164 }
1165 }
1166 vqueuePut(&pState->VPCI, pQueue, &elem, sizeof(VNETHDR) + uOffset);
1167 vqueueSync(&pState->VPCI, pQueue);
1168 STAM_PROFILE_ADV_STOP(&pState->StatTransmit, a);
1169 }
1170 vpciSetWriteLed(&pState->VPCI, false);
1171
1172 if (pDrv)
1173 pDrv->pfnEndXmit(pDrv);
1174 ASMAtomicWriteU32(&pState->uIsTransmitting, 0);
1175}
1176
1177/**
1178 * @interface_method_impl{PDMINETWORKDOWN,pfnXmitPending}
1179 */
1180static DECLCALLBACK(void) vnetNetworkDown_XmitPending(PPDMINETWORKDOWN pInterface)
1181{
1182 VNETSTATE *pThis = RT_FROM_MEMBER(pInterface, VNETSTATE, INetworkDown);
1183 vnetTransmitPendingPackets(pThis, pThis->pTxQueue, false /*fOnWorkerThread*/);
1184}
1185
1186#ifdef VNET_TX_DELAY
1187
1188static DECLCALLBACK(void) vnetQueueTransmit(void *pvState, PVQUEUE pQueue)
1189{
1190 VNETSTATE *pState = (VNETSTATE*)pvState;
1191
1192 if (TMTimerIsActive(pState->CTX_SUFF(pTxTimer)))
1193 {
1194 int rc = TMTimerStop(pState->CTX_SUFF(pTxTimer));
1195 Log3(("%s vnetQueueTransmit: Got kicked with notification disabled, "
1196 "re-enable notification and flush TX queue\n", INSTANCE(pState)));
1197 vnetTransmitPendingPackets(pState, pQueue, false /*fOnWorkerThread*/);
1198 if (RT_FAILURE(vnetCsEnter(pState, VERR_SEM_BUSY)))
1199 LogRel(("vnetQueueTransmit: Failed to enter critical section!/n"));
1200 else
1201 {
1202 vringSetNotification(&pState->VPCI, &pState->pTxQueue->VRing, true);
1203 vnetCsLeave(pState);
1204 }
1205 }
1206 else
1207 {
1208 if (RT_FAILURE(vnetCsEnter(pState, VERR_SEM_BUSY)))
1209 LogRel(("vnetQueueTransmit: Failed to enter critical section!/n"));
1210 else
1211 {
1212 vringSetNotification(&pState->VPCI, &pState->pTxQueue->VRing, false);
1213 TMTimerSetMicro(pState->CTX_SUFF(pTxTimer), VNET_TX_DELAY);
1214 pState->u64NanoTS = RTTimeNanoTS();
1215 vnetCsLeave(pState);
1216 }
1217 }
1218}
1219
1220/**
1221 * Transmit Delay Timer handler.
1222 *
1223 * @remarks We only get here when the timer expires.
1224 *
1225 * @param pDevIns Pointer to device instance structure.
1226 * @param pTimer Pointer to the timer.
1227 * @param pvUser NULL.
1228 * @thread EMT
1229 */
1230static DECLCALLBACK(void) vnetTxTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
1231{
1232 VNETSTATE *pState = (VNETSTATE*)pvUser;
1233
1234 uint32_t u32MicroDiff = (uint32_t)((RTTimeNanoTS() - pState->u64NanoTS)/1000);
1235 if (u32MicroDiff < pState->u32MinDiff)
1236 pState->u32MinDiff = u32MicroDiff;
1237 if (u32MicroDiff > pState->u32MaxDiff)
1238 pState->u32MaxDiff = u32MicroDiff;
1239 pState->u32AvgDiff = (pState->u32AvgDiff * pState->u32i + u32MicroDiff) / (pState->u32i + 1);
1240 pState->u32i++;
1241 Log3(("vnetTxTimer: Expired, diff %9d usec, avg %9d usec, min %9d usec, max %9d usec\n",
1242 u32MicroDiff, pState->u32AvgDiff, pState->u32MinDiff, pState->u32MaxDiff));
1243
1244// Log3(("%s vnetTxTimer: Expired\n", INSTANCE(pState)));
1245 vnetTransmitPendingPackets(pState, pState->pTxQueue, false /*fOnWorkerThread*/);
1246 if (RT_FAILURE(vnetCsEnter(pState, VERR_SEM_BUSY)))
1247 {
1248 LogRel(("vnetTxTimer: Failed to enter critical section!/n"));
1249 return;
1250 }
1251 vringSetNotification(&pState->VPCI, &pState->pTxQueue->VRing, true);
1252 vnetCsLeave(pState);
1253}
1254
1255#else /* !VNET_TX_DELAY */
1256
1257static DECLCALLBACK(void) vnetQueueTransmit(void *pvState, PVQUEUE pQueue)
1258{
1259 VNETSTATE *pState = (VNETSTATE*)pvState;
1260
1261 vnetTransmitPendingPackets(pState, pQueue, false /*fOnWorkerThread*/);
1262}
1263
1264#endif /* !VNET_TX_DELAY */
1265
1266static uint8_t vnetControlRx(PVNETSTATE pState, PVNETCTLHDR pCtlHdr, PVQUEUEELEM pElem)
1267{
1268 uint8_t u8Ack = VNET_OK;
1269 uint8_t fOn, fDrvWasPromisc = pState->fPromiscuous | pState->fAllMulti;
1270 PDMDevHlpPhysRead(pState->VPCI.CTX_SUFF(pDevIns),
1271 pElem->aSegsOut[1].addr,
1272 &fOn, sizeof(fOn));
1273 Log(("%s vnetControlRx: uCommand=%u fOn=%u\n", INSTANCE(pState), pCtlHdr->u8Command, fOn));
1274 switch (pCtlHdr->u8Command)
1275 {
1276 case VNET_CTRL_CMD_RX_MODE_PROMISC:
1277 pState->fPromiscuous = !!fOn;
1278 break;
1279 case VNET_CTRL_CMD_RX_MODE_ALLMULTI:
1280 pState->fAllMulti = !!fOn;
1281 break;
1282 default:
1283 u8Ack = VNET_ERROR;
1284 }
1285 if (fDrvWasPromisc != (pState->fPromiscuous | pState->fAllMulti) && pState->pDrv)
1286 pState->pDrv->pfnSetPromiscuousMode(pState->pDrv,
1287 (pState->fPromiscuous | pState->fAllMulti));
1288
1289 return u8Ack;
1290}
1291
1292static uint8_t vnetControlMac(PVNETSTATE pState, PVNETCTLHDR pCtlHdr, PVQUEUEELEM pElem)
1293{
1294 uint32_t nMacs = 0;
1295
1296 if (pCtlHdr->u8Command != VNET_CTRL_CMD_MAC_TABLE_SET
1297 || pElem->nOut != 3
1298 || pElem->aSegsOut[1].cb < sizeof(nMacs)
1299 || pElem->aSegsOut[2].cb < sizeof(nMacs))
1300 {
1301 Log(("%s vnetControlMac: Segment layout is wrong "
1302 "(u8Command=%u nOut=%u cb1=%u cb2=%u)\n", INSTANCE(pState),
1303 pCtlHdr->u8Command, pElem->nOut,
1304 pElem->aSegsOut[1].cb, pElem->aSegsOut[2].cb));
1305 return VNET_ERROR;
1306 }
1307
1308 /* Load unicast addresses */
1309 PDMDevHlpPhysRead(pState->VPCI.CTX_SUFF(pDevIns),
1310 pElem->aSegsOut[1].addr,
1311 &nMacs, sizeof(nMacs));
1312
1313 if (pElem->aSegsOut[1].cb < nMacs * sizeof(RTMAC) + sizeof(nMacs))
1314 {
1315 Log(("%s vnetControlMac: The unicast mac segment is too small "
1316 "(nMacs=%u cb=%u)\n", INSTANCE(pState), pElem->aSegsOut[1].cb));
1317 return VNET_ERROR;
1318 }
1319
1320 if (nMacs > VNET_MAC_FILTER_LEN)
1321 {
1322 Log(("%s vnetControlMac: MAC table is too big, have to use promiscuous"
1323 " mode (nMacs=%u)\n", INSTANCE(pState), nMacs));
1324 pState->fPromiscuous = true;
1325 }
1326 else
1327 {
1328 if (nMacs)
1329 PDMDevHlpPhysRead(pState->VPCI.CTX_SUFF(pDevIns),
1330 pElem->aSegsOut[1].addr + sizeof(nMacs),
1331 pState->aMacFilter, nMacs * sizeof(RTMAC));
1332 pState->nMacFilterEntries = nMacs;
1333#ifdef DEBUG
1334 Log(("%s vnetControlMac: unicast macs:\n", INSTANCE(pState)));
1335 for(unsigned i = 0; i < nMacs; i++)
1336 Log((" %RTmac\n", &pState->aMacFilter[i]));
1337#endif /* DEBUG */
1338 }
1339
1340 /* Load multicast addresses */
1341 PDMDevHlpPhysRead(pState->VPCI.CTX_SUFF(pDevIns),
1342 pElem->aSegsOut[2].addr,
1343 &nMacs, sizeof(nMacs));
1344
1345 if (pElem->aSegsOut[2].cb < nMacs * sizeof(RTMAC) + sizeof(nMacs))
1346 {
1347 Log(("%s vnetControlMac: The multicast mac segment is too small "
1348 "(nMacs=%u cb=%u)\n", INSTANCE(pState), pElem->aSegsOut[2].cb));
1349 return VNET_ERROR;
1350 }
1351
1352 if (nMacs > VNET_MAC_FILTER_LEN - pState->nMacFilterEntries)
1353 {
1354 Log(("%s vnetControlMac: MAC table is too big, have to use allmulti"
1355 " mode (nMacs=%u)\n", INSTANCE(pState), nMacs));
1356 pState->fAllMulti = true;
1357 }
1358 else
1359 {
1360 if (nMacs)
1361 PDMDevHlpPhysRead(pState->VPCI.CTX_SUFF(pDevIns),
1362 pElem->aSegsOut[2].addr + sizeof(nMacs),
1363 &pState->aMacFilter[pState->nMacFilterEntries],
1364 nMacs * sizeof(RTMAC));
1365#ifdef DEBUG
1366 Log(("%s vnetControlMac: multicast macs:\n", INSTANCE(pState)));
1367 for(unsigned i = 0; i < nMacs; i++)
1368 Log((" %RTmac\n",
1369 &pState->aMacFilter[i+pState->nMacFilterEntries]));
1370#endif /* DEBUG */
1371 pState->nMacFilterEntries += nMacs;
1372 }
1373
1374 return VNET_OK;
1375}
1376
1377static uint8_t vnetControlVlan(PVNETSTATE pState, PVNETCTLHDR pCtlHdr, PVQUEUEELEM pElem)
1378{
1379 uint8_t u8Ack = VNET_OK;
1380 uint16_t u16Vid;
1381
1382 if (pElem->nOut != 2 || pElem->aSegsOut[1].cb != sizeof(u16Vid))
1383 {
1384 Log(("%s vnetControlVlan: Segment layout is wrong "
1385 "(u8Command=%u nOut=%u cb=%u)\n", INSTANCE(pState),
1386 pCtlHdr->u8Command, pElem->nOut, pElem->aSegsOut[1].cb));
1387 return VNET_ERROR;
1388 }
1389
1390 PDMDevHlpPhysRead(pState->VPCI.CTX_SUFF(pDevIns),
1391 pElem->aSegsOut[1].addr,
1392 &u16Vid, sizeof(u16Vid));
1393
1394 if (u16Vid >= VNET_MAX_VID)
1395 {
1396 Log(("%s vnetControlVlan: VLAN ID is out of range "
1397 "(VID=%u)\n", INSTANCE(pState), u16Vid));
1398 return VNET_ERROR;
1399 }
1400
1401 Log(("%s vnetControlVlan: uCommand=%u VID=%u\n", INSTANCE(pState),
1402 pCtlHdr->u8Command, u16Vid));
1403
1404 switch (pCtlHdr->u8Command)
1405 {
1406 case VNET_CTRL_CMD_VLAN_ADD:
1407 ASMBitSet(pState->aVlanFilter, u16Vid);
1408 break;
1409 case VNET_CTRL_CMD_VLAN_DEL:
1410 ASMBitClear(pState->aVlanFilter, u16Vid);
1411 break;
1412 default:
1413 u8Ack = VNET_ERROR;
1414 }
1415
1416 return u8Ack;
1417}
1418
1419
1420static DECLCALLBACK(void) vnetQueueControl(void *pvState, PVQUEUE pQueue)
1421{
1422 VNETSTATE *pState = (VNETSTATE*)pvState;
1423 uint8_t u8Ack;
1424 VQUEUEELEM elem;
1425 while (vqueueGet(&pState->VPCI, pQueue, &elem))
1426 {
1427 unsigned int uOffset = 0;
1428 if (elem.nOut < 1 || elem.aSegsOut[0].cb < sizeof(VNETCTLHDR))
1429 {
1430 Log(("%s vnetQueueControl: The first 'out' segment is not the "
1431 "header! (%u < 1 || %u < %u).\n", INSTANCE(pState), elem.nOut,
1432 elem.aSegsOut[0].cb,sizeof(VNETCTLHDR)));
1433 break; /* Skip the element and hope the next one is good. */
1434 }
1435 else if ( elem.nIn < 1
1436 || elem.aSegsIn[elem.nIn - 1].cb < sizeof(VNETCTLACK))
1437 {
1438 Log(("%s vnetQueueControl: The last 'in' segment is too small "
1439 "to hold the acknowledge! (%u < 1 || %u < %u).\n",
1440 INSTANCE(pState), elem.nIn, elem.aSegsIn[elem.nIn - 1].cb,
1441 sizeof(VNETCTLACK)));
1442 break; /* Skip the element and hope the next one is good. */
1443 }
1444 else
1445 {
1446 VNETCTLHDR CtlHdr;
1447 PDMDevHlpPhysRead(pState->VPCI.CTX_SUFF(pDevIns),
1448 elem.aSegsOut[0].addr,
1449 &CtlHdr, sizeof(CtlHdr));
1450 switch (CtlHdr.u8Class)
1451 {
1452 case VNET_CTRL_CLS_RX_MODE:
1453 u8Ack = vnetControlRx(pState, &CtlHdr, &elem);
1454 break;
1455 case VNET_CTRL_CLS_MAC:
1456 u8Ack = vnetControlMac(pState, &CtlHdr, &elem);
1457 break;
1458 case VNET_CTRL_CLS_VLAN:
1459 u8Ack = vnetControlVlan(pState, &CtlHdr, &elem);
1460 break;
1461 default:
1462 u8Ack = VNET_ERROR;
1463 }
1464 Log(("%s Processed control message %u, ack=%u.\n", INSTANCE(pState),
1465 CtlHdr.u8Class, u8Ack));
1466 PDMDevHlpPhysWrite(pState->VPCI.CTX_SUFF(pDevIns),
1467 elem.aSegsIn[elem.nIn - 1].addr,
1468 &u8Ack, sizeof(u8Ack));
1469 }
1470 vqueuePut(&pState->VPCI, pQueue, &elem, sizeof(u8Ack));
1471 vqueueSync(&pState->VPCI, pQueue);
1472 }
1473}
1474
1475/**
1476 * Saves the configuration.
1477 *
1478 * @param pState The VNET state.
1479 * @param pSSM The handle to the saved state.
1480 */
1481static void vnetSaveConfig(VNETSTATE *pState, PSSMHANDLE pSSM)
1482{
1483 SSMR3PutMem(pSSM, &pState->macConfigured, sizeof(pState->macConfigured));
1484}
1485
1486/**
1487 * Live save - save basic configuration.
1488 *
1489 * @returns VBox status code.
1490 * @param pDevIns The device instance.
1491 * @param pSSM The handle to the saved state.
1492 * @param uPass
1493 */
1494static DECLCALLBACK(int) vnetLiveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uPass)
1495{
1496 VNETSTATE *pState = PDMINS_2_DATA(pDevIns, VNETSTATE*);
1497 vnetSaveConfig(pState, pSSM);
1498 return VINF_SSM_DONT_CALL_AGAIN;
1499}
1500
1501/**
1502 * Prepares for state saving.
1503 *
1504 * @returns VBox status code.
1505 * @param pDevIns The device instance.
1506 * @param pSSM The handle to the saved state.
1507 */
1508static DECLCALLBACK(int) vnetSavePrep(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
1509{
1510 VNETSTATE* pState = PDMINS_2_DATA(pDevIns, VNETSTATE*);
1511
1512 int rc = vnetCsRxEnter(pState, VERR_SEM_BUSY);
1513 if (RT_UNLIKELY(rc != VINF_SUCCESS))
1514 return rc;
1515 vnetCsRxLeave(pState);
1516 return VINF_SUCCESS;
1517}
1518
1519/**
1520 * Saves the state of device.
1521 *
1522 * @returns VBox status code.
1523 * @param pDevIns The device instance.
1524 * @param pSSM The handle to the saved state.
1525 */
1526static DECLCALLBACK(int) vnetSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
1527{
1528 VNETSTATE* pState = PDMINS_2_DATA(pDevIns, VNETSTATE*);
1529
1530 /* Save config first */
1531 vnetSaveConfig(pState, pSSM);
1532
1533 /* Save the common part */
1534 int rc = vpciSaveExec(&pState->VPCI, pSSM);
1535 AssertRCReturn(rc, rc);
1536 /* Save device-specific part */
1537 rc = SSMR3PutMem( pSSM, pState->config.mac.au8, sizeof(pState->config.mac));
1538 AssertRCReturn(rc, rc);
1539 rc = SSMR3PutBool(pSSM, pState->fPromiscuous);
1540 AssertRCReturn(rc, rc);
1541 rc = SSMR3PutBool(pSSM, pState->fAllMulti);
1542 AssertRCReturn(rc, rc);
1543 rc = SSMR3PutU32( pSSM, pState->nMacFilterEntries);
1544 AssertRCReturn(rc, rc);
1545 rc = SSMR3PutMem( pSSM, pState->aMacFilter,
1546 pState->nMacFilterEntries * sizeof(RTMAC));
1547 AssertRCReturn(rc, rc);
1548 rc = SSMR3PutMem( pSSM, pState->aVlanFilter, sizeof(pState->aVlanFilter));
1549 AssertRCReturn(rc, rc);
1550 Log(("%s State has been saved\n", INSTANCE(pState)));
1551 return VINF_SUCCESS;
1552}
1553
1554
1555/**
1556 * Serializes the receive thread, it may be working inside the critsect.
1557 *
1558 * @returns VBox status code.
1559 * @param pDevIns The device instance.
1560 * @param pSSM The handle to the saved state.
1561 */
1562static DECLCALLBACK(int) vnetLoadPrep(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
1563{
1564 VNETSTATE* pState = PDMINS_2_DATA(pDevIns, VNETSTATE*);
1565
1566 int rc = vnetCsRxEnter(pState, VERR_SEM_BUSY);
1567 if (RT_UNLIKELY(rc != VINF_SUCCESS))
1568 return rc;
1569 vnetCsRxLeave(pState);
1570 return VINF_SUCCESS;
1571}
1572
1573/**
1574 * Takes down the link temporarily if it's current status is up.
1575 *
1576 * This is used during restore and when replumbing the network link.
1577 *
1578 * The temporary link outage is supposed to indicate to the OS that all network
1579 * connections have been lost and that it for instance is appropriate to
1580 * renegotiate any DHCP lease.
1581 *
1582 * @param pThis The PCNet instance data.
1583 */
1584static void vnetTempLinkDown(PVNETSTATE pState)
1585{
1586 if (STATUS & VNET_S_LINK_UP)
1587 {
1588 STATUS &= ~VNET_S_LINK_UP;
1589 vpciRaiseInterrupt(&pState->VPCI, VERR_SEM_BUSY, VPCI_ISR_CONFIG);
1590 /* Restore the link back in 5 seconds. */
1591 int rc = TMTimerSetMillies(pState->pLinkUpTimer, 5000);
1592 AssertRC(rc);
1593 }
1594}
1595
1596
1597/**
1598 * Restore previously saved state of device.
1599 *
1600 * @returns VBox status code.
1601 * @param pDevIns The device instance.
1602 * @param pSSM The handle to the saved state.
1603 * @param uVersion The data unit version number.
1604 * @param uPass The data pass.
1605 */
1606static DECLCALLBACK(int) vnetLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
1607{
1608 VNETSTATE *pState = PDMINS_2_DATA(pDevIns, VNETSTATE*);
1609 int rc;
1610
1611 /* config checks */
1612 RTMAC macConfigured;
1613 rc = SSMR3GetMem(pSSM, &macConfigured, sizeof(macConfigured));
1614 AssertRCReturn(rc, rc);
1615 if (memcmp(&macConfigured, &pState->macConfigured, sizeof(macConfigured))
1616 && (uPass == 0 || !PDMDevHlpVMTeleportedAndNotFullyResumedYet(pDevIns)))
1617 LogRel(("%s: The mac address differs: config=%RTmac saved=%RTmac\n", INSTANCE(pState), &pState->macConfigured, &macConfigured));
1618
1619 rc = vpciLoadExec(&pState->VPCI, pSSM, uVersion, uPass, VNET_N_QUEUES);
1620 AssertRCReturn(rc, rc);
1621
1622 if (uPass == SSM_PASS_FINAL)
1623 {
1624 rc = SSMR3GetMem( pSSM, pState->config.mac.au8,
1625 sizeof(pState->config.mac));
1626 AssertRCReturn(rc, rc);
1627
1628 if (uVersion > VIRTIO_SAVEDSTATE_VERSION_3_1_BETA1)
1629 {
1630 rc = SSMR3GetBool(pSSM, &pState->fPromiscuous);
1631 AssertRCReturn(rc, rc);
1632 rc = SSMR3GetBool(pSSM, &pState->fAllMulti);
1633 AssertRCReturn(rc, rc);
1634 rc = SSMR3GetU32(pSSM, &pState->nMacFilterEntries);
1635 AssertRCReturn(rc, rc);
1636 rc = SSMR3GetMem(pSSM, pState->aMacFilter,
1637 pState->nMacFilterEntries * sizeof(RTMAC));
1638 AssertRCReturn(rc, rc);
1639 /* Clear the rest. */
1640 if (pState->nMacFilterEntries < VNET_MAC_FILTER_LEN)
1641 memset(&pState->aMacFilter[pState->nMacFilterEntries],
1642 0,
1643 (VNET_MAC_FILTER_LEN - pState->nMacFilterEntries)
1644 * sizeof(RTMAC));
1645 rc = SSMR3GetMem(pSSM, pState->aVlanFilter,
1646 sizeof(pState->aVlanFilter));
1647 AssertRCReturn(rc, rc);
1648 }
1649 else
1650 {
1651 pState->fPromiscuous = true;
1652 pState->fAllMulti = false;
1653 pState->nMacFilterEntries = 0;
1654 memset(pState->aMacFilter, 0, VNET_MAC_FILTER_LEN * sizeof(RTMAC));
1655 memset(pState->aVlanFilter, 0, sizeof(pState->aVlanFilter));
1656 if (pState->pDrv)
1657 pState->pDrv->pfnSetPromiscuousMode(pState->pDrv, true);
1658 }
1659 }
1660
1661 return rc;
1662}
1663
1664/**
1665 * Link status adjustments after loading.
1666 *
1667 * @returns VBox status code.
1668 * @param pDevIns The device instance.
1669 * @param pSSM The handle to the saved state.
1670 */
1671static DECLCALLBACK(int) vnetLoadDone(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
1672{
1673 VNETSTATE *pState = PDMINS_2_DATA(pDevIns, VNETSTATE*);
1674
1675 if (pState->pDrv)
1676 pState->pDrv->pfnSetPromiscuousMode(pState->pDrv,
1677 (pState->fPromiscuous | pState->fAllMulti));
1678 /*
1679 * Indicate link down to the guest OS that all network connections have
1680 * been lost, unless we've been teleported here.
1681 */
1682 if (!PDMDevHlpVMTeleportedAndNotFullyResumedYet(pDevIns))
1683 vnetTempLinkDown(pState);
1684
1685 return VINF_SUCCESS;
1686}
1687
1688/**
1689 * Map PCI I/O region.
1690 *
1691 * @return VBox status code.
1692 * @param pPciDev Pointer to PCI device. Use pPciDev->pDevIns to get the device instance.
1693 * @param iRegion The region number.
1694 * @param GCPhysAddress Physical address of the region. If iType is PCI_ADDRESS_SPACE_IO, this is an
1695 * I/O port, else it's a physical address.
1696 * This address is *NOT* relative to pci_mem_base like earlier!
1697 * @param cb Region size.
1698 * @param enmType One of the PCI_ADDRESS_SPACE_* values.
1699 * @thread EMT
1700 */
1701static DECLCALLBACK(int) vnetMap(PPCIDEVICE pPciDev, int iRegion,
1702 RTGCPHYS GCPhysAddress, uint32_t cb, PCIADDRESSSPACE enmType)
1703{
1704 int rc;
1705 VNETSTATE *pState = PDMINS_2_DATA(pPciDev->pDevIns, VNETSTATE*);
1706
1707 if (enmType != PCI_ADDRESS_SPACE_IO)
1708 {
1709 /* We should never get here */
1710 AssertMsgFailed(("Invalid PCI address space param in map callback"));
1711 return VERR_INTERNAL_ERROR;
1712 }
1713
1714 pState->VPCI.addrIOPort = (RTIOPORT)GCPhysAddress;
1715 rc = PDMDevHlpIOPortRegister(pPciDev->pDevIns, pState->VPCI.addrIOPort,
1716 cb, 0, vnetIOPortOut, vnetIOPortIn,
1717 NULL, NULL, "VirtioNet");
1718#ifdef VNET_GC_SUPPORT
1719 AssertRCReturn(rc, rc);
1720 rc = PDMDevHlpIOPortRegisterR0(pPciDev->pDevIns, pState->VPCI.addrIOPort,
1721 cb, 0, "vnetIOPortOut", "vnetIOPortIn",
1722 NULL, NULL, "VirtioNet");
1723 AssertRCReturn(rc, rc);
1724 rc = PDMDevHlpIOPortRegisterRC(pPciDev->pDevIns, pState->VPCI.addrIOPort,
1725 cb, 0, "vnetIOPortOut", "vnetIOPortIn",
1726 NULL, NULL, "VirtioNet");
1727#endif
1728 AssertRC(rc);
1729 return rc;
1730}
1731
1732
1733/* -=-=-=-=- PDMDEVREG -=-=-=-=- */
1734
1735/**
1736 * Detach notification.
1737 *
1738 * One port on the network card has been disconnected from the network.
1739 *
1740 * @param pDevIns The device instance.
1741 * @param iLUN The logical unit which is being detached.
1742 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
1743 */
1744static DECLCALLBACK(void) vnetDetach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
1745{
1746 VNETSTATE *pState = PDMINS_2_DATA(pDevIns, VNETSTATE*);
1747 Log(("%s vnetDetach:\n", INSTANCE(pState)));
1748
1749 AssertLogRelReturnVoid(iLUN == 0);
1750
1751 int rc = vnetCsEnter(pState, VERR_SEM_BUSY);
1752 if (RT_FAILURE(rc))
1753 {
1754 LogRel(("vnetDetach failed to enter critical section!\n"));
1755 return;
1756 }
1757
1758 /*
1759 * Zero some important members.
1760 */
1761 pState->pDrvBase = NULL;
1762 pState->pDrv = NULL;
1763
1764 vnetCsLeave(pState);
1765}
1766
1767/**
1768 * Attach the Network attachment.
1769 *
1770 * One port on the network card has been connected to a network.
1771 *
1772 * @returns VBox status code.
1773 * @param pDevIns The device instance.
1774 * @param iLUN The logical unit which is being attached.
1775 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
1776 *
1777 * @remarks This code path is not used during construction.
1778 */
1779static DECLCALLBACK(int) vnetAttach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
1780{
1781 VNETSTATE *pState = PDMINS_2_DATA(pDevIns, VNETSTATE*);
1782 LogFlow(("%s vnetAttach:\n", INSTANCE(pState)));
1783
1784 AssertLogRelReturn(iLUN == 0, VERR_PDM_NO_SUCH_LUN);
1785
1786 int rc = vnetCsEnter(pState, VERR_SEM_BUSY);
1787 if (RT_FAILURE(rc))
1788 {
1789 LogRel(("vnetAttach failed to enter critical section!\n"));
1790 return rc;
1791 }
1792
1793 /*
1794 * Attach the driver.
1795 */
1796 rc = PDMDevHlpDriverAttach(pDevIns, 0, &pState->VPCI.IBase, &pState->pDrvBase, "Network Port");
1797 if (RT_SUCCESS(rc))
1798 {
1799 if (rc == VINF_NAT_DNS)
1800 {
1801#ifdef RT_OS_LINUX
1802 PDMDevHlpVMSetRuntimeError(pDevIns, 0 /*fFlags*/, "NoDNSforNAT",
1803 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"));
1804#else
1805 PDMDevHlpVMSetRuntimeError(pDevIns, 0 /*fFlags*/, "NoDNSforNAT",
1806 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"));
1807#endif
1808 }
1809 pState->pDrv = PDMIBASE_QUERY_INTERFACE(pState->pDrvBase, PDMINETWORKUP);
1810 AssertMsgStmt(pState->pDrv, ("Failed to obtain the PDMINETWORKUP interface!\n"),
1811 rc = VERR_PDM_MISSING_INTERFACE_BELOW);
1812 }
1813 else if ( rc == VERR_PDM_NO_ATTACHED_DRIVER
1814 || rc == VERR_PDM_CFG_MISSING_DRIVER_NAME)
1815 {
1816 /* This should never happen because this function is not called
1817 * if there is no driver to attach! */
1818 Log(("%s No attached driver!\n", INSTANCE(pState)));
1819 }
1820
1821 /*
1822 * Temporary set the link down if it was up so that the guest
1823 * will know that we have change the configuration of the
1824 * network card
1825 */
1826 if (RT_SUCCESS(rc))
1827 vnetTempLinkDown(pState);
1828
1829 vnetCsLeave(pState);
1830 return rc;
1831
1832}
1833
1834/**
1835 * @copydoc FNPDMDEVSUSPEND
1836 */
1837static DECLCALLBACK(void) vnetSuspend(PPDMDEVINS pDevIns)
1838{
1839 /* Poke thread waiting for buffer space. */
1840 vnetWakeupReceive(pDevIns);
1841}
1842
1843/**
1844 * @copydoc FNPDMDEVPOWEROFF
1845 */
1846static DECLCALLBACK(void) vnetPowerOff(PPDMDEVINS pDevIns)
1847{
1848 /* Poke thread waiting for buffer space. */
1849 vnetWakeupReceive(pDevIns);
1850}
1851
1852/**
1853 * Device relocation callback.
1854 *
1855 * When this callback is called the device instance data, and if the
1856 * device have a GC component, is being relocated, or/and the selectors
1857 * have been changed. The device must use the chance to perform the
1858 * necessary pointer relocations and data updates.
1859 *
1860 * Before the GC code is executed the first time, this function will be
1861 * called with a 0 delta so GC pointer calculations can be one in one place.
1862 *
1863 * @param pDevIns Pointer to the device instance.
1864 * @param offDelta The relocation delta relative to the old location.
1865 *
1866 * @remark A relocation CANNOT fail.
1867 */
1868static DECLCALLBACK(void) vnetRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
1869{
1870 VNETSTATE* pState = PDMINS_2_DATA(pDevIns, VNETSTATE*);
1871 vpciRelocate(pDevIns, offDelta);
1872 pState->pCanRxQueueRC = PDMQueueRCPtr(pState->pCanRxQueueR3);
1873#ifdef VNET_TX_DELAY
1874 pState->pTxTimerRC = TMTimerRCPtr(pState->pTxTimerR3);
1875#endif /* VNET_TX_DELAY */
1876 // TBD
1877}
1878
1879/**
1880 * Destruct a device instance.
1881 *
1882 * We need to free non-VM resources only.
1883 *
1884 * @returns VBox status.
1885 * @param pDevIns The device instance data.
1886 * @thread EMT
1887 */
1888static DECLCALLBACK(int) vnetDestruct(PPDMDEVINS pDevIns)
1889{
1890 VNETSTATE* pState = PDMINS_2_DATA(pDevIns, VNETSTATE*);
1891 PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
1892
1893 LogRel(("TxTimer stats (avg/min/max): %7d usec %7d usec %7d usec\n",
1894 pState->u32AvgDiff, pState->u32MinDiff, pState->u32MaxDiff));
1895 Log(("%s Destroying instance\n", INSTANCE(pState)));
1896 if (pState->hEventMoreRxDescAvail != NIL_RTSEMEVENT)
1897 {
1898 RTSemEventSignal(pState->hEventMoreRxDescAvail);
1899 RTSemEventDestroy(pState->hEventMoreRxDescAvail);
1900 pState->hEventMoreRxDescAvail = NIL_RTSEMEVENT;
1901 }
1902
1903 // if (PDMCritSectIsInitialized(&pState->csRx))
1904 // PDMR3CritSectDelete(&pState->csRx);
1905
1906 return vpciDestruct(&pState->VPCI);
1907}
1908
1909/**
1910 * @interface_method_impl{PDMDEVREG,pfnConstruct}
1911 */
1912static DECLCALLBACK(int) vnetConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
1913{
1914 VNETSTATE* pState = PDMINS_2_DATA(pDevIns, VNETSTATE*);
1915 int rc;
1916 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
1917
1918 /* Initialize PCI part first. */
1919 pState->VPCI.IBase.pfnQueryInterface = vnetQueryInterface;
1920 rc = vpciConstruct(pDevIns, &pState->VPCI, iInstance,
1921 VNET_NAME_FMT, VNET_PCI_SUBSYSTEM_ID,
1922 VNET_PCI_CLASS, VNET_N_QUEUES);
1923 pState->pRxQueue = vpciAddQueue(&pState->VPCI, 256, vnetQueueReceive, "RX ");
1924 pState->pTxQueue = vpciAddQueue(&pState->VPCI, 256, vnetQueueTransmit, "TX ");
1925 pState->pCtlQueue = vpciAddQueue(&pState->VPCI, 16, vnetQueueControl, "CTL");
1926
1927 Log(("%s Constructing new instance\n", INSTANCE(pState)));
1928
1929 pState->hEventMoreRxDescAvail = NIL_RTSEMEVENT;
1930
1931 /*
1932 * Validate configuration.
1933 */
1934 if (!CFGMR3AreValuesValid(pCfg, "MAC\0" "CableConnected\0" "LineSpeed\0"))
1935 return PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
1936 N_("Invalid configuration for VirtioNet device"));
1937
1938 /* Get config params */
1939 rc = CFGMR3QueryBytes(pCfg, "MAC", pState->macConfigured.au8,
1940 sizeof(pState->macConfigured));
1941 if (RT_FAILURE(rc))
1942 return PDMDEV_SET_ERROR(pDevIns, rc,
1943 N_("Configuration error: Failed to get MAC address"));
1944 rc = CFGMR3QueryBool(pCfg, "CableConnected", &pState->fCableConnected);
1945 if (RT_FAILURE(rc))
1946 return PDMDEV_SET_ERROR(pDevIns, rc,
1947 N_("Configuration error: Failed to get the value of 'CableConnected'"));
1948
1949 /* Initialize PCI config space */
1950 memcpy(pState->config.mac.au8, pState->macConfigured.au8, sizeof(pState->config.mac.au8));
1951 pState->config.uStatus = 0;
1952
1953 /* Initialize state structure */
1954 pState->u32PktNo = 1;
1955
1956 /* Interfaces */
1957 pState->INetworkDown.pfnWaitReceiveAvail = vnetNetworkDown_WaitReceiveAvail;
1958 pState->INetworkDown.pfnReceive = vnetNetworkDown_Receive;
1959 pState->INetworkDown.pfnReceiveGso = vnetNetworkDown_ReceiveGso;
1960 pState->INetworkDown.pfnXmitPending = vnetNetworkDown_XmitPending;
1961
1962 pState->INetworkConfig.pfnGetMac = vnetGetMac;
1963 pState->INetworkConfig.pfnGetLinkState = vnetGetLinkState;
1964 pState->INetworkConfig.pfnSetLinkState = vnetSetLinkState;
1965
1966 /* Initialize critical section. */
1967 // char szTmp[sizeof(pState->VPCI.szInstance) + 2];
1968 // RTStrPrintf(szTmp, sizeof(szTmp), "%sRX", pState->VPCI.szInstance);
1969 // rc = PDMDevHlpCritSectInit(pDevIns, &pState->csRx, szTmp);
1970 // if (RT_FAILURE(rc))
1971 // return rc;
1972
1973 /* Map our ports to IO space. */
1974 rc = PDMDevHlpPCIIORegionRegister(pDevIns, 0,
1975 VPCI_CONFIG + sizeof(VNetPCIConfig),
1976 PCI_ADDRESS_SPACE_IO, vnetMap);
1977 if (RT_FAILURE(rc))
1978 return rc;
1979
1980
1981 /* Register save/restore state handlers. */
1982 rc = PDMDevHlpSSMRegisterEx(pDevIns, VIRTIO_SAVEDSTATE_VERSION, sizeof(VNETSTATE), NULL,
1983 NULL, vnetLiveExec, NULL,
1984 vnetSavePrep, vnetSaveExec, NULL,
1985 vnetLoadPrep, vnetLoadExec, vnetLoadDone);
1986 if (RT_FAILURE(rc))
1987 return rc;
1988
1989 /* Create the RX notifier signaller. */
1990 rc = PDMDevHlpQueueCreate(pDevIns, sizeof(PDMQUEUEITEMCORE), 1, 0,
1991 vnetCanRxQueueConsumer, true, "VNet-Rcv", &pState->pCanRxQueueR3);
1992 if (RT_FAILURE(rc))
1993 return rc;
1994 pState->pCanRxQueueR0 = PDMQueueR0Ptr(pState->pCanRxQueueR3);
1995 pState->pCanRxQueueRC = PDMQueueRCPtr(pState->pCanRxQueueR3);
1996
1997 /* Create Link Up Timer */
1998 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, vnetLinkUpTimer, pState,
1999 TMTIMER_FLAGS_DEFAULT_CRIT_SECT, /** @todo check locking here. */
2000 "VirtioNet Link Up Timer", &pState->pLinkUpTimer);
2001 if (RT_FAILURE(rc))
2002 return rc;
2003
2004#ifdef VNET_TX_DELAY
2005 /* Create Transmit Delay Timer */
2006 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, vnetTxTimer, pState,
2007 TMTIMER_FLAGS_DEFAULT_CRIT_SECT, /** @todo check locking here. */
2008 "VirtioNet TX Delay Timer", &pState->pTxTimerR3);
2009 if (RT_FAILURE(rc))
2010 return rc;
2011 pState->pTxTimerR0 = TMTimerR0Ptr(pState->pTxTimerR3);
2012 pState->pTxTimerRC = TMTimerRCPtr(pState->pTxTimerR3);
2013
2014 pState->u32i = pState->u32AvgDiff = pState->u32MaxDiff = 0;
2015 pState->u32MinDiff = ~0;
2016#endif /* VNET_TX_DELAY */
2017
2018 rc = PDMDevHlpDriverAttach(pDevIns, 0, &pState->VPCI.IBase, &pState->pDrvBase, "Network Port");
2019 if (RT_SUCCESS(rc))
2020 {
2021 if (rc == VINF_NAT_DNS)
2022 {
2023 PDMDevHlpVMSetRuntimeError(pDevIns, 0 /*fFlags*/, "NoDNSforNAT",
2024 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"));
2025 }
2026 pState->pDrv = PDMIBASE_QUERY_INTERFACE(pState->pDrvBase, PDMINETWORKUP);
2027 AssertMsgReturn(pState->pDrv, ("Failed to obtain the PDMINETWORKUP interface!\n"),
2028 VERR_PDM_MISSING_INTERFACE_BELOW);
2029 }
2030 else if ( rc == VERR_PDM_NO_ATTACHED_DRIVER
2031 || rc == VERR_PDM_CFG_MISSING_DRIVER_NAME )
2032 {
2033 /* No error! */
2034 Log(("%s This adapter is not attached to any network!\n", INSTANCE(pState)));
2035 }
2036 else
2037 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Failed to attach the network LUN"));
2038
2039 rc = RTSemEventCreate(&pState->hEventMoreRxDescAvail);
2040 if (RT_FAILURE(rc))
2041 return rc;
2042
2043 rc = vnetReset(pState);
2044 AssertRC(rc);
2045
2046 PDMDevHlpSTAMRegisterF(pDevIns, &pState->StatReceiveBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Amount of data received", "/Devices/VNet%d/Bytes/Receive", iInstance);
2047 PDMDevHlpSTAMRegisterF(pDevIns, &pState->StatTransmitBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Amount of data transmitted", "/Devices/VNet%d/Bytes/Transmit", iInstance);
2048 PDMDevHlpSTAMRegisterF(pDevIns, &pState->StatReceiveGSO, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Number of received GSO packets", "/Devices/VNet%d/Packets/ReceiveGSO", iInstance);
2049 PDMDevHlpSTAMRegisterF(pDevIns, &pState->StatTransmitPackets, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Number of sent packets", "/Devices/VNet%d/Packets/Transmit", iInstance);
2050 PDMDevHlpSTAMRegisterF(pDevIns, &pState->StatTransmitGSO, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Number of sent GSO packets", "/Devices/VNet%d/Packets/Transmit-Gso", iInstance);
2051 PDMDevHlpSTAMRegisterF(pDevIns, &pState->StatTransmitCSum, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Number of completed TX checksums", "/Devices/VNet%d/Packets/Transmit-Csum", iInstance);
2052#if defined(VBOX_WITH_STATISTICS)
2053 PDMDevHlpSTAMRegisterF(pDevIns, &pState->StatReceive, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling receive", "/Devices/VNet%d/Receive/Total", iInstance);
2054 PDMDevHlpSTAMRegisterF(pDevIns, &pState->StatReceiveStore, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling receive storing", "/Devices/VNet%d/Receive/Store", iInstance);
2055 PDMDevHlpSTAMRegisterF(pDevIns, &pState->StatRxOverflow, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_OCCURENCE, "Profiling RX overflows", "/Devices/VNet%d/RxOverflow", iInstance);
2056 PDMDevHlpSTAMRegisterF(pDevIns, &pState->StatRxOverflowWakeup, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Nr of RX overflow wakeups", "/Devices/VNet%d/RxOverflowWakeup", iInstance);
2057 PDMDevHlpSTAMRegisterF(pDevIns, &pState->StatTransmit, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling transmits in HC", "/Devices/VNet%d/Transmit/Total", iInstance);
2058 PDMDevHlpSTAMRegisterF(pDevIns, &pState->StatTransmitSend, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling send transmit in HC", "/Devices/VNet%d/Transmit/Send", iInstance);
2059#endif /* VBOX_WITH_STATISTICS */
2060
2061 return VINF_SUCCESS;
2062}
2063
2064/**
2065 * The device registration structure.
2066 */
2067const PDMDEVREG g_DeviceVirtioNet =
2068{
2069 /* Structure version. PDM_DEVREG_VERSION defines the current version. */
2070 PDM_DEVREG_VERSION,
2071 /* Device name. */
2072 "virtio-net",
2073 /* Name of guest context module (no path).
2074 * Only evalutated if PDM_DEVREG_FLAGS_RC is set. */
2075 "VBoxDDGC.gc",
2076 /* Name of ring-0 module (no path).
2077 * Only evalutated if PDM_DEVREG_FLAGS_RC is set. */
2078 "VBoxDDR0.r0",
2079 /* The description of the device. The UTF-8 string pointed to shall, like this structure,
2080 * remain unchanged from registration till VM destruction. */
2081 "Virtio Ethernet.\n",
2082
2083 /* Flags, combination of the PDM_DEVREG_FLAGS_* \#defines. */
2084#ifdef VNET_GC_SUPPORT
2085 PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
2086#else
2087 PDM_DEVREG_FLAGS_DEFAULT_BITS,
2088#endif
2089 /* Device class(es), combination of the PDM_DEVREG_CLASS_* \#defines. */
2090 PDM_DEVREG_CLASS_NETWORK,
2091 /* Maximum number of instances (per VM). */
2092 8,
2093 /* Size of the instance data. */
2094 sizeof(VNETSTATE),
2095
2096 /* Construct instance - required. */
2097 vnetConstruct,
2098 /* Destruct instance - optional. */
2099 vnetDestruct,
2100 /* Relocation command - optional. */
2101 vnetRelocate,
2102 /* I/O Control interface - optional. */
2103 NULL,
2104 /* Power on notification - optional. */
2105 NULL,
2106 /* Reset notification - optional. */
2107 NULL,
2108 /* Suspend notification - optional. */
2109 vnetSuspend,
2110 /* Resume notification - optional. */
2111 NULL,
2112 /* Attach command - optional. */
2113 vnetAttach,
2114 /* Detach notification - optional. */
2115 vnetDetach,
2116 /* Query a LUN base interface - optional. */
2117 NULL,
2118 /* Init complete notification - optional. */
2119 NULL,
2120 /* Power off notification - optional. */
2121 vnetPowerOff,
2122 /* pfnSoftReset */
2123 NULL,
2124 /* u32VersionEnd */
2125 PDM_DEVREG_VERSION
2126};
2127
2128#endif /* IN_RING3 */
2129#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette