VirtualBox

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

Last change on this file since 90778 was 90447, checked in by vboxsync, 3 years ago

Dev*: Checked up all the PDMDevHlpCritSectEnter calls to make sure the status code is checked. bugref:6695

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 87.1 KB
Line 
1/* $Id: DevVirtioNet.cpp 90447 2021-07-31 00:44:13Z vboxsync $ */
2/** @file
3 * DevVirtioNet - Virtio Network Device
4 */
5
6/*
7 * Copyright (C) 2009-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_DEV_VIRTIO_NET
23#define VNET_WITH_GSO
24#define VNET_WITH_MERGEABLE_RX_BUFS
25
26#include <VBox/vmm/pdmdev.h>
27#include <VBox/vmm/pdmnetifs.h>
28#include <iprt/asm.h>
29#include <iprt/net.h>
30#include <iprt/semaphore.h>
31#include <iprt/string.h>
32#ifdef IN_RING3
33# include <iprt/uuid.h>
34#endif
35#include <VBox/VBoxPktDmp.h>
36#include "VBoxDD.h"
37#include "../VirtIO/Virtio.h"
38
39
40/*********************************************************************************************************************************
41* Defined Constants And Macros *
42*********************************************************************************************************************************/
43#ifndef VBOX_DEVICE_STRUCT_TESTCASE
44
45#define INSTANCE(pThis) pThis->VPCI.szInstance
46
47#ifdef IN_RING3
48
49# define VNET_PCI_CLASS 0x0200
50# define VNET_N_QUEUES 3
51
52# if 0
53/* Virtio Block Device */
54# define VNET_PCI_CLASS 0x0180
55# define VNET_N_QUEUES 2
56# endif
57
58#endif /* IN_RING3 */
59
60#endif /* VBOX_DEVICE_STRUCT_TESTCASE */
61
62/*
63 * Commenting out VNET_TX_DELAY enables async transmission in a dedicated thread.
64 * When VNET_TX_DELAY is defined, a timer handler does the job.
65 */
66//#define VNET_TX_DELAY 150 /**< 150 microseconds */
67#define VNET_MAX_FRAME_SIZE 65535 + 18 /**< Max IP packet size + Ethernet header with VLAN tag */
68#define VNET_MAC_FILTER_LEN 32
69#define VNET_MAX_VID (1 << 12)
70
71/** @name Virtio net features
72 * @{ */
73#define VNET_F_CSUM 0x00000001 /**< Host handles pkts w/ partial csum */
74#define VNET_F_GUEST_CSUM 0x00000002 /**< Guest handles pkts w/ partial csum */
75#define VNET_F_MAC 0x00000020 /**< Host has given MAC address. */
76#define VNET_F_GSO 0x00000040 /**< Host handles pkts w/ any GSO type */
77#define VNET_F_GUEST_TSO4 0x00000080 /**< Guest can handle TSOv4 in. */
78#define VNET_F_GUEST_TSO6 0x00000100 /**< Guest can handle TSOv6 in. */
79#define VNET_F_GUEST_ECN 0x00000200 /**< Guest can handle TSO[6] w/ ECN in. */
80#define VNET_F_GUEST_UFO 0x00000400 /**< Guest can handle UFO in. */
81#define VNET_F_HOST_TSO4 0x00000800 /**< Host can handle TSOv4 in. */
82#define VNET_F_HOST_TSO6 0x00001000 /**< Host can handle TSOv6 in. */
83#define VNET_F_HOST_ECN 0x00002000 /**< Host can handle TSO[6] w/ ECN in. */
84#define VNET_F_HOST_UFO 0x00004000 /**< Host can handle UFO in. */
85#define VNET_F_MRG_RXBUF 0x00008000 /**< Host can merge receive buffers. */
86#define VNET_F_STATUS 0x00010000 /**< virtio_net_config.status available */
87#define VNET_F_CTRL_VQ 0x00020000 /**< Control channel available */
88#define VNET_F_CTRL_RX 0x00040000 /**< Control channel RX mode support */
89#define VNET_F_CTRL_VLAN 0x00080000 /**< Control channel VLAN filtering */
90/** @} */
91
92#define VNET_S_LINK_UP 1
93
94
95/*********************************************************************************************************************************
96* Structures and Typedefs *
97*********************************************************************************************************************************/
98#ifdef _MSC_VER
99struct VNetPCIConfig
100#else /* !_MSC_VER */
101struct __attribute__ ((__packed__)) VNetPCIConfig /** @todo r=bird: Use #pragma pack if necessary, that's portable! */
102#endif /* !_MSC_VER */
103{
104 RTMAC mac;
105 uint16_t uStatus;
106};
107AssertCompileMemberOffset(struct VNetPCIConfig, uStatus, 6);
108
109/**
110 * The virtio-net shared instance data.
111 *
112 * @extends VPCISTATE
113 */
114typedef struct VNETSTATE
115{
116 VPCISTATE VPCI;
117
118// PDMCRITSECT csRx; /**< Protects RX queue. */
119
120#ifdef VNET_TX_DELAY
121 /** Transmit Delay Timer. */
122 TMTIMERHANDLE hTxTimer;
123 uint32_t u32i;
124 uint32_t u32AvgDiff;
125 uint32_t u32MinDiff;
126 uint32_t u32MaxDiff;
127 uint64_t u64NanoTS;
128#else /* !VNET_TX_DELAY */
129 /** The event semaphore TX thread waits on. */
130 SUPSEMEVENT hTxEvent;
131#endif /* !VNET_TX_DELAY */
132
133 /** Indicates transmission in progress -- only one thread is allowed. */
134 uint32_t uIsTransmitting;
135
136 /** PCI config area holding MAC address as well as TBD. */
137 struct VNetPCIConfig config;
138 /** MAC address obtained from the configuration. */
139 RTMAC macConfigured;
140 /** True if physical cable is attached in configuration. */
141 bool fCableConnected;
142 /** Link up delay (in milliseconds). */
143 uint32_t cMsLinkUpDelay;
144
145 uint32_t alignment;
146
147 /** Number of packet being sent/received to show in debug log. */
148 uint32_t u32PktNo;
149
150 /** N/A: */
151 bool volatile fMaybeOutOfSpace;
152
153 /** Promiscuous mode -- RX filter accepts all packets. */
154 bool fPromiscuous;
155 /** AllMulti mode -- RX filter accepts all multicast packets. */
156 bool fAllMulti;
157 /** The number of actually used slots in aMacTable. */
158 uint32_t cMacFilterEntries;
159 /** Array of MAC addresses accepted by RX filter. */
160 RTMAC aMacFilter[VNET_MAC_FILTER_LEN];
161 /** Bit array of VLAN filter, one bit per VLAN ID. */
162 uint8_t aVlanFilter[VNET_MAX_VID / sizeof(uint8_t)];
163
164 /* Receive-blocking-related fields ***************************************/
165
166 /** EMT: Gets signalled when more RX descriptors become available. */
167 SUPSEMEVENT hEventMoreRxDescAvail;
168
169 /** Handle of the I/O port range. */
170 IOMIOPORTHANDLE hIoPorts;
171
172 /** @name Statistic
173 * @{ */
174 STAMCOUNTER StatReceiveBytes;
175 STAMCOUNTER StatTransmitBytes;
176 STAMCOUNTER StatReceiveGSO;
177 STAMCOUNTER StatTransmitPackets;
178 STAMCOUNTER StatTransmitGSO;
179 STAMCOUNTER StatTransmitCSum;
180#ifdef VBOX_WITH_STATISTICS
181 STAMPROFILE StatReceive;
182 STAMPROFILE StatReceiveStore;
183 STAMPROFILEADV StatTransmit;
184 STAMPROFILE StatTransmitSend;
185 STAMPROFILE StatRxOverflow;
186 STAMCOUNTER StatRxOverflowWakeup;
187 STAMCOUNTER StatTransmitByNetwork;
188 STAMCOUNTER StatTransmitByThread;
189#endif
190 /** @} */
191} VNETSTATE;
192/** Pointer to the virtio-net shared instance data. */
193typedef VNETSTATE *PVNETSTATE;
194
195
196/**
197 * The virtio-net ring-3 instance data.
198 *
199 * @extends VPCISTATER3
200 * @implements PDMINETWORKDOWN
201 * @implements PDMINETWORKCONFIG
202 */
203typedef struct VNETSTATER3
204{
205 VPCISTATER3 VPCI;
206
207 PDMINETWORKDOWN INetworkDown;
208 PDMINETWORKCONFIG INetworkConfig;
209 R3PTRTYPE(PPDMIBASE) pDrvBase; /**< Attached network driver. */
210 R3PTRTYPE(PPDMINETWORKUP) pDrv; /**< Connector of attached network driver. */
211 /** The device instance.
212 * @note This is _only_ for use when dealing with interface callbacks. */
213 PPDMDEVINSR3 pDevIns;
214
215#ifndef VNET_TX_DELAY
216 R3PTRTYPE(PPDMTHREAD) pTxThread;
217#endif
218
219 R3PTRTYPE(PVQUEUE) pRxQueue;
220 R3PTRTYPE(PVQUEUE) pTxQueue;
221 R3PTRTYPE(PVQUEUE) pCtlQueue;
222
223 /** Link Up(/Restore) Timer. */
224 TMTIMERHANDLE hLinkUpTimer;
225} VNETSTATER3;
226/** Pointer to the virtio-net ring-3 instance data. */
227typedef VNETSTATER3 *PVNETSTATER3;
228
229
230/**
231 * The virtio-net ring-0 instance data.
232 *
233 * @extends VPCISTATER0
234 */
235typedef struct VNETSTATER0
236{
237 VPCISTATER0 VPCI;
238} VNETSTATER0;
239/** Pointer to the virtio-net ring-0 instance data. */
240typedef VNETSTATER0 *PVNETSTATER0;
241
242
243/**
244 * The virtio-net raw-mode instance data.
245 *
246 * @extends VPCISTATERC
247 */
248typedef struct VNETSTATERC
249{
250 VPCISTATERC VPCI;
251} VNETSTATERC;
252/** Pointer to the virtio-net ring-0 instance data. */
253typedef VNETSTATERC *PVNETSTATERC;
254
255
256/** The virtio-net currenct context instance data. */
257typedef CTX_SUFF(VNETSTATE) VNETSTATECC;
258/** Pointer to the virtio-net currenct context instance data. */
259typedef CTX_SUFF(PVNETSTATE) PVNETSTATECC;
260
261
262
263#ifndef VBOX_DEVICE_STRUCT_TESTCASE
264
265#define VNETHDR_F_NEEDS_CSUM 1 // Use u16CSumStart, u16CSumOffset
266
267#define VNETHDR_GSO_NONE 0 // Not a GSO frame
268#define VNETHDR_GSO_TCPV4 1 // GSO frame, IPv4 TCP (TSO)
269#define VNETHDR_GSO_UDP 3 // GSO frame, IPv4 UDP (UFO)
270#define VNETHDR_GSO_TCPV6 4 // GSO frame, IPv6 TCP
271#define VNETHDR_GSO_ECN 0x80 // TCP has ECN set
272
273struct VNetHdr
274{
275 uint8_t u8Flags;
276 uint8_t u8GSOType;
277 uint16_t u16HdrLen;
278 uint16_t u16GSOSize;
279 uint16_t u16CSumStart;
280 uint16_t u16CSumOffset;
281};
282typedef struct VNetHdr VNETHDR;
283typedef VNETHDR *PVNETHDR;
284AssertCompileSize(VNETHDR, 10);
285
286struct VNetHdrMrx
287{
288 VNETHDR Hdr;
289 uint16_t u16NumBufs;
290};
291typedef struct VNetHdrMrx VNETHDRMRX;
292typedef VNETHDRMRX *PVNETHDRMRX;
293AssertCompileSize(VNETHDRMRX, 12);
294
295AssertCompileMemberOffset(VNETSTATE, VPCI, 0);
296
297#define VNET_OK 0
298#define VNET_ERROR 1
299typedef uint8_t VNETCTLACK;
300
301#define VNET_CTRL_CLS_RX_MODE 0
302#define VNET_CTRL_CMD_RX_MODE_PROMISC 0
303#define VNET_CTRL_CMD_RX_MODE_ALLMULTI 1
304
305#define VNET_CTRL_CLS_MAC 1
306#define VNET_CTRL_CMD_MAC_TABLE_SET 0
307
308#define VNET_CTRL_CLS_VLAN 2
309#define VNET_CTRL_CMD_VLAN_ADD 0
310#define VNET_CTRL_CMD_VLAN_DEL 1
311
312
313typedef struct VNetCtlHdr
314{
315 uint8_t u8Class;
316 uint8_t u8Command;
317} VNETCTLHDR;
318AssertCompileSize(VNETCTLHDR, 2);
319typedef VNETCTLHDR *PVNETCTLHDR;
320
321
322
323#ifdef IN_RING3
324
325/** Returns true if large packets are written into several RX buffers. */
326DECLINLINE(bool) vnetR3MergeableRxBuffers(PVNETSTATE pThis)
327{
328 return !!(pThis->VPCI.uGuestFeatures & VNET_F_MRG_RXBUF);
329}
330
331#define VNET_R3_CS_ENTER_RETURN_VOID(a_pDevIns, a_pThis) VPCI_R3_CS_ENTER_RETURN_VOID(a_pDevIns, &(a_pThis)->VPCI)
332
333DECLINLINE(int) vnetR3CsEnter(PPDMDEVINS pDevIns, PVNETSTATE pThis, int rcBusy)
334{
335 return vpciCsEnter(pDevIns, &pThis->VPCI, rcBusy);
336}
337
338DECLINLINE(void) vnetR3CsLeave(PPDMDEVINS pDevIns, PVNETSTATE pThis)
339{
340 vpciCsLeave(pDevIns, &pThis->VPCI);
341}
342
343
344DECLINLINE(int) vnetCsRxEnter(PVNETSTATE pThis, int rcBusy)
345{
346 RT_NOREF_PV(pThis);
347 RT_NOREF_PV(rcBusy);
348 // STAM_PROFILE_START(&pThis->CTXSUFF(StatCsRx), a);
349 // int rc = PDMCritSectEnter(&pThis->csRx, rcBusy);
350 // STAM_PROFILE_STOP(&pThis->CTXSUFF(StatCsRx), a);
351 // return rc;
352 return VINF_SUCCESS;
353}
354
355DECLINLINE(void) vnetCsRxLeave(PVNETSTATE pThis)
356{
357 RT_NOREF_PV(pThis);
358 // PDMCritSectLeave(&pThis->csRx);
359}
360
361/**
362 * A helper function to detect the link state to the other side of "the wire".
363 *
364 * When deciding to bring up the link we need to take into account both if the
365 * cable is connected and if our device is actually connected to the outside
366 * world. If no driver is attached we won't start the TX thread nor we will
367 * initialize the TX semaphore, which is a problem for the TX queue handler.
368 *
369 * @returns true if the device is connected to something.
370 *
371 * @param pDevIns The device instance.
372 */
373DECLINLINE(bool) vnetR3IsConnected(PPDMDEVINS pDevIns)
374{
375 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
376 PVNETSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVNETSTATECC);
377 return pThis->fCableConnected && pThisCC->pDrv;
378}
379
380/**
381 * Dump a packet to debug log.
382 *
383 * @param pThis The virtio-net shared instance data.
384 * @param pbPacket The packet.
385 * @param cb The size of the packet.
386 * @param pszText A string denoting direction of packet transfer.
387 */
388DECLINLINE(void) vnetR3PacketDump(PVNETSTATE pThis, const uint8_t *pbPacket, size_t cb, const char *pszText)
389{
390# ifdef DEBUG
391# if 0
392 Log(("%s %s packet #%d (%d bytes):\n",
393 INSTANCE(pThis), pszText, ++pThis->u32PktNo, cb));
394 Log3(("%.*Rhxd\n", cb, pbPacket));
395# else
396 vboxEthPacketDump(INSTANCE(pThis), pszText, pbPacket, (uint32_t)cb);
397# endif
398# else
399 RT_NOREF4(pThis, pbPacket, cb, pszText);
400# endif
401}
402#endif /* IN_RING3 */
403
404/**
405 * Print features given in uFeatures to debug log.
406 *
407 * @param pThis The virtio-net shared instance data.
408 * @param fFeatures Descriptions of which features to print.
409 * @param pcszText A string to print before the list of features.
410 */
411DECLINLINE(void) vnetPrintFeatures(PVNETSTATE pThis, uint32_t fFeatures, const char *pcszText)
412{
413#ifdef LOG_ENABLED
414 static struct
415 {
416 uint32_t fMask;
417 const char *pcszDesc;
418 } const s_aFeatures[] =
419 {
420 { VNET_F_CSUM, "host handles pkts w/ partial csum" },
421 { VNET_F_GUEST_CSUM, "guest handles pkts w/ partial csum" },
422 { VNET_F_MAC, "host has given MAC address" },
423 { VNET_F_GSO, "host handles pkts w/ any GSO type" },
424 { VNET_F_GUEST_TSO4, "guest can handle TSOv4 in" },
425 { VNET_F_GUEST_TSO6, "guest can handle TSOv6 in" },
426 { VNET_F_GUEST_ECN, "guest can handle TSO[6] w/ ECN in" },
427 { VNET_F_GUEST_UFO, "guest can handle UFO in" },
428 { VNET_F_HOST_TSO4, "host can handle TSOv4 in" },
429 { VNET_F_HOST_TSO6, "host can handle TSOv6 in" },
430 { VNET_F_HOST_ECN, "host can handle TSO[6] w/ ECN in" },
431 { VNET_F_HOST_UFO, "host can handle UFO in" },
432 { VNET_F_MRG_RXBUF, "host can merge receive buffers" },
433 { VNET_F_STATUS, "virtio_net_config.status available" },
434 { VNET_F_CTRL_VQ, "control channel available" },
435 { VNET_F_CTRL_RX, "control channel RX mode support" },
436 { VNET_F_CTRL_VLAN, "control channel VLAN filtering" }
437 };
438
439 Log3(("%s %s:\n", INSTANCE(pThis), pcszText));
440 for (unsigned i = 0; i < RT_ELEMENTS(s_aFeatures); ++i)
441 {
442 if (s_aFeatures[i].fMask & fFeatures)
443 Log3(("%s --> %s\n", INSTANCE(pThis), s_aFeatures[i].pcszDesc));
444 }
445#else /* !LOG_ENABLED */
446 RT_NOREF3(pThis, fFeatures, pcszText);
447#endif /* !LOG_ENABLED */
448}
449
450/**
451 * @interface_method_impl{VPCIIOCALLBACKS,pfnGetHostFeatures}
452 */
453static DECLCALLBACK(uint32_t) vnetIoCb_GetHostFeatures(PVPCISTATE pVPciState)
454{
455 RT_NOREF_PV(pVPciState);
456
457 /* We support:
458 * - Host-provided MAC address
459 * - Link status reporting in config space
460 * - Control queue
461 * - RX mode setting
462 * - MAC filter table
463 * - VLAN filter
464 */
465 return VNET_F_MAC
466 | VNET_F_STATUS
467 | VNET_F_CTRL_VQ
468 | VNET_F_CTRL_RX
469 | VNET_F_CTRL_VLAN
470#ifdef VNET_WITH_GSO
471 | VNET_F_CSUM
472 | VNET_F_HOST_TSO4
473 | VNET_F_HOST_TSO6
474 | VNET_F_HOST_UFO
475 | VNET_F_GUEST_CSUM /* We expect the guest to accept partial TCP checksums (see @bugref{4796}) */
476 | VNET_F_GUEST_TSO4
477 | VNET_F_GUEST_TSO6
478 | VNET_F_GUEST_UFO
479#endif
480#ifdef VNET_WITH_MERGEABLE_RX_BUFS
481 | VNET_F_MRG_RXBUF
482#endif
483 ;
484}
485
486/**
487 * @interface_method_impl{VPCIIOCALLBACKS,pfnGetHostMinimalFeatures}
488 */
489static DECLCALLBACK(uint32_t) vnetIoCb_GetHostMinimalFeatures(PVPCISTATE pVPciState)
490{
491 RT_NOREF_PV(pVPciState);
492 return VNET_F_MAC;
493}
494
495/**
496 * @interface_method_impl{VPCIIOCALLBACKS,pfnSetHostFeatures}
497 */
498static DECLCALLBACK(void) vnetIoCb_SetHostFeatures(PVPCISTATE pVPciState, uint32_t fFeatures)
499{
500 PVNETSTATE pThis = RT_FROM_MEMBER(pVPciState, VNETSTATE, VPCI);
501 /** @todo Nothing to do here yet */
502 LogFlow(("%s vnetIoCb_SetHostFeatures: uFeatures=%x\n", INSTANCE(pThis), fFeatures));
503 vnetPrintFeatures(pThis, fFeatures, "The guest negotiated the following features");
504}
505
506/**
507 * @interface_method_impl{VPCIIOCALLBACKS,pfnGetConfig}
508 */
509static DECLCALLBACK(int) vnetIoCb_GetConfig(PVPCISTATE pVPciState, uint32_t offCfg, uint32_t cb, void *data)
510{
511 PVNETSTATE pThis = RT_FROM_MEMBER(pVPciState, VNETSTATE, VPCI);
512 if (offCfg + cb > sizeof(struct VNetPCIConfig))
513 {
514 Log(("%s vnetIoCb_GetConfig: Read beyond the config structure is attempted (offCfg=%#x cb=%x).\n", INSTANCE(pThis), offCfg, cb));
515 return VERR_IOM_IOPORT_UNUSED;
516 }
517 memcpy(data, (uint8_t *)&pThis->config + offCfg, cb);
518 return VINF_SUCCESS;
519}
520
521/**
522 * @interface_method_impl{VPCIIOCALLBACKS,pfnSetConfig}
523 */
524static DECLCALLBACK(int) vnetIoCb_SetConfig(PVPCISTATE pVPciState, uint32_t offCfg, uint32_t cb, void *data)
525{
526 PVNETSTATE pThis = RT_FROM_MEMBER(pVPciState, VNETSTATE, VPCI);
527 if (offCfg + cb > sizeof(struct VNetPCIConfig))
528 {
529 Log(("%s vnetIoCb_SetConfig: Write beyond the config structure is attempted (offCfg=%#x cb=%x).\n", INSTANCE(pThis), offCfg, cb));
530 if (offCfg < sizeof(struct VNetPCIConfig))
531 memcpy((uint8_t *)&pThis->config + offCfg, data, sizeof(struct VNetPCIConfig) - offCfg);
532 return VINF_SUCCESS;
533 }
534 memcpy((uint8_t *)&pThis->config + offCfg, data, cb);
535 return VINF_SUCCESS;
536}
537
538/**
539 * @interface_method_impl{VPCIIOCALLBACKS,pfnReset}
540 *
541 * Hardware reset. Revert all registers to initial values.
542 */
543static DECLCALLBACK(int) vnetIoCb_Reset(PPDMDEVINS pDevIns)
544{
545#ifndef IN_RING3
546 RT_NOREF(pDevIns);
547 return VINF_IOM_R3_IOPORT_WRITE;
548#else
549 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
550 PVNETSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVNETSTATECC);
551
552 Log(("%s Reset triggered\n", INSTANCE(pThis)));
553
554 int rc = vnetCsRxEnter(pThis, VERR_SEM_BUSY);
555 if (RT_UNLIKELY(rc != VINF_SUCCESS))
556 {
557 LogRel(("vnetIoCb_Reset failed to enter RX critical section!\n"));
558 return rc;
559 }
560 vpciReset(pDevIns, &pThis->VPCI);
561 vnetCsRxLeave(pThis);
562
563 /// @todo Implement reset
564 if (vnetR3IsConnected(pDevIns))
565 pThis->config.uStatus = VNET_S_LINK_UP;
566 else
567 pThis->config.uStatus = 0;
568 Log(("%s vnetIoCb_Reset: Link is %s\n", INSTANCE(pThis), vnetR3IsConnected(pDevIns) ? "up" : "down"));
569
570 /*
571 * By default we pass all packets up since the older guests cannot control
572 * virtio mode.
573 */
574 pThis->fPromiscuous = true;
575 pThis->fAllMulti = false;
576 pThis->cMacFilterEntries = 0;
577 memset(pThis->aMacFilter, 0, VNET_MAC_FILTER_LEN * sizeof(RTMAC));
578 memset(pThis->aVlanFilter, 0, sizeof(pThis->aVlanFilter));
579 pThis->uIsTransmitting = 0;
580 if (pThisCC->pDrv)
581 pThisCC->pDrv->pfnSetPromiscuousMode(pThisCC->pDrv, true);
582 return VINF_SUCCESS;
583#endif
584}
585
586
587/**
588 * Wakeup the RX thread.
589 */
590static void vnetWakeupReceive(PPDMDEVINS pDevIns)
591{
592 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
593 if ( pThis->fMaybeOutOfSpace
594 && pThis->hEventMoreRxDescAvail != NIL_SUPSEMEVENT)
595 {
596 STAM_COUNTER_INC(&pThis->StatRxOverflowWakeup);
597 Log(("%s Waking up Out-of-RX-space semaphore\n", INSTANCE(pThis)));
598 int rc = PDMDevHlpSUPSemEventSignal(pDevIns, pThis->hEventMoreRxDescAvail);
599 AssertRC(rc);
600 }
601}
602
603#ifdef IN_RING3
604
605/**
606 * Helper function that raises an interrupt if the guest is ready to receive it.
607 */
608static int vnetR3RaiseInterrupt(PPDMDEVINS pDevIns, PVNETSTATE pThis, int rcBusy, uint8_t u8IntCause)
609{
610 if (pThis->VPCI.uStatus & VPCI_STATUS_DRV_OK)
611 return vpciRaiseInterrupt(pDevIns, &pThis->VPCI, rcBusy, u8IntCause);
612 return rcBusy;
613}
614
615
616/**
617 * Takes down the link temporarily if it's current status is up.
618 *
619 * This is used during restore and when replumbing the network link.
620 *
621 * The temporary link outage is supposed to indicate to the OS that all network
622 * connections have been lost and that it for instance is appropriate to
623 * renegotiate any DHCP lease.
624 *
625 * @param pDevIns The device instance.
626 * @param pThis The virtio-net shared instance data.
627 * @param pThisCC The virtio-net ring-3 instance data.
628 */
629static void vnetR3TempLinkDown(PPDMDEVINS pDevIns, PVNETSTATE pThis, PVNETSTATECC pThisCC)
630{
631 if (pThis->config.uStatus & VNET_S_LINK_UP)
632 {
633 pThis->config.uStatus &= ~VNET_S_LINK_UP;
634 vnetR3RaiseInterrupt(pDevIns, pThis, VERR_SEM_BUSY, VPCI_ISR_CONFIG);
635 /* Restore the link back in 5 seconds. */
636 int rc = PDMDevHlpTimerSetMillies(pDevIns, pThisCC->hLinkUpTimer, pThis->cMsLinkUpDelay);
637 AssertRC(rc);
638 Log(("%s vnetR3TempLinkDown: Link is down temporarily\n", INSTANCE(pThis)));
639 }
640}
641
642
643/**
644 * @callback_method_impl{FNTMTIMERDEV, Link Up Timer handler.}
645 */
646static DECLCALLBACK(void) vnetR3LinkUpTimer(PPDMDEVINS pDevIns, TMTIMERHANDLE hTimer, void *pvUser)
647{
648 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
649 PVNETSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVNETSTATECC);
650 RT_NOREF(hTimer, pvUser);
651
652 VNET_R3_CS_ENTER_RETURN_VOID(pDevIns, pThis);
653
654 pThis->config.uStatus |= VNET_S_LINK_UP;
655 vnetR3RaiseInterrupt(pDevIns, pThis, VERR_SEM_BUSY, VPCI_ISR_CONFIG);
656 vnetWakeupReceive(pDevIns);
657
658 vnetR3CsLeave(pDevIns, pThis);
659
660 Log(("%s vnetR3LinkUpTimer: Link is up\n", INSTANCE(pThis)));
661 if (pThisCC->pDrv)
662 pThisCC->pDrv->pfnNotifyLinkChanged(pThisCC->pDrv, PDMNETWORKLINKSTATE_UP);
663}
664
665#endif /* IN_RING3 */
666
667/**
668 * @interface_method_impl{VPCIIOCALLBACKS,pfnReady}
669 *
670 * This function is called when the driver becomes ready.
671 */
672static DECLCALLBACK(void) vnetIoCb_Ready(PPDMDEVINS pDevIns)
673{
674 Log(("%s Driver became ready, waking up RX thread...\n", INSTANCE(PDMDEVINS_2_DATA(pDevIns, PVNETSTATE))));
675 vnetWakeupReceive(pDevIns);
676}
677
678
679/**
680 * I/O port callbacks.
681 */
682static const VPCIIOCALLBACKS g_IOCallbacks =
683{
684 vnetIoCb_GetHostFeatures,
685 vnetIoCb_GetHostMinimalFeatures,
686 vnetIoCb_SetHostFeatures,
687 vnetIoCb_GetConfig,
688 vnetIoCb_SetConfig,
689 vnetIoCb_Reset,
690 vnetIoCb_Ready,
691};
692
693
694/**
695 * @callback_method_impl{FNIOMIOPORTNEWIN}
696 */
697static DECLCALLBACK(VBOXSTRICTRC) vnetIOPortIn(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t *pu32, unsigned cb)
698{
699 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
700 RT_NOREF(pvUser);
701 return vpciIOPortIn(pDevIns, &pThis->VPCI, offPort, pu32, cb, &g_IOCallbacks);
702}
703
704
705/**
706 * @callback_method_impl{FNIOMIOPORTNEWOUT}
707 */
708static DECLCALLBACK(VBOXSTRICTRC) vnetIOPortOut(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t u32, unsigned cb)
709{
710 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
711 PVNETSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVNETSTATECC);
712 RT_NOREF(pvUser);
713 return vpciIOPortOut(pDevIns, &pThis->VPCI, &pThisCC->VPCI, offPort, u32, cb, &g_IOCallbacks);
714}
715
716
717#ifdef IN_RING3
718
719/**
720 * Check if the device can receive data now.
721 * This must be called before the pfnRecieve() method is called.
722 *
723 * @remarks As a side effect this function enables queue notification
724 * if it cannot receive because the queue is empty.
725 * It disables notification if it can receive.
726 *
727 * @returns VERR_NET_NO_BUFFER_SPACE if it cannot.
728 * @thread RX
729 */
730static int vnetR3CanReceive(PPDMDEVINS pDevIns, PVNETSTATE pThis, PVNETSTATECC pThisCC)
731{
732 int rc = vnetCsRxEnter(pThis, VERR_SEM_BUSY);
733 AssertRCReturn(rc, rc);
734
735 LogFlow(("%s vnetR3CanReceive\n", INSTANCE(pThis)));
736 if (!(pThis->VPCI.uStatus & VPCI_STATUS_DRV_OK))
737 rc = VERR_NET_NO_BUFFER_SPACE;
738 else if (!vqueueIsReady(pThisCC->pRxQueue))
739 rc = VERR_NET_NO_BUFFER_SPACE;
740 else if (vqueueIsEmpty(pDevIns, pThisCC->pRxQueue))
741 {
742 vringSetNotification(pDevIns, &pThisCC->pRxQueue->VRing, true);
743 rc = VERR_NET_NO_BUFFER_SPACE;
744 }
745 else
746 {
747 vringSetNotification(pDevIns, &pThisCC->pRxQueue->VRing, false);
748 rc = VINF_SUCCESS;
749 }
750
751 LogFlow(("%s vnetR3CanReceive -> %Rrc\n", INSTANCE(pThis), rc));
752 vnetCsRxLeave(pThis);
753 return rc;
754}
755
756/**
757 * @interface_method_impl{PDMINETWORKDOWN,pfnWaitReceiveAvail}
758 */
759static DECLCALLBACK(int) vnetR3NetworkDown_WaitReceiveAvail(PPDMINETWORKDOWN pInterface, RTMSINTERVAL cMillies)
760{
761 PVNETSTATECC pThisCC = RT_FROM_MEMBER(pInterface, VNETSTATECC, INetworkDown);
762 PPDMDEVINS pDevIns = pThisCC->pDevIns;
763 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
764 LogFlow(("%s vnetR3NetworkDown_WaitReceiveAvail(cMillies=%u)\n", INSTANCE(pThis), cMillies));
765
766 int rc = vnetR3CanReceive(pDevIns, pThis, pThisCC);
767 if (RT_SUCCESS(rc))
768 return VINF_SUCCESS;
769
770 if (cMillies == 0)
771 return VERR_NET_NO_BUFFER_SPACE;
772
773 rc = VERR_INTERRUPTED;
774 ASMAtomicXchgBool(&pThis->fMaybeOutOfSpace, true);
775 STAM_PROFILE_START(&pThis->StatRxOverflow, a);
776
777 VMSTATE enmVMState;
778 while (RT_LIKELY( (enmVMState = PDMDevHlpVMState(pDevIns)) == VMSTATE_RUNNING
779 || enmVMState == VMSTATE_RUNNING_LS))
780 {
781 int rc2 = vnetR3CanReceive(pDevIns, pThis, pThisCC);
782 if (RT_SUCCESS(rc2))
783 {
784 rc = VINF_SUCCESS;
785 break;
786 }
787 Log(("%s vnetR3NetworkDown_WaitReceiveAvail: waiting cMillies=%u...\n", INSTANCE(pThis), cMillies));
788 rc2 = PDMDevHlpSUPSemEventWaitNoResume(pDevIns, pThis->hEventMoreRxDescAvail, cMillies);
789 if (RT_FAILURE(rc2) && rc2 != VERR_TIMEOUT && rc2 != VERR_INTERRUPTED)
790 RTThreadSleep(1);
791 }
792 STAM_PROFILE_STOP(&pThis->StatRxOverflow, a);
793 ASMAtomicXchgBool(&pThis->fMaybeOutOfSpace, false);
794
795 LogFlow(("%s vnetR3NetworkDown_WaitReceiveAvail -> %d\n", INSTANCE(pThis), rc));
796 return rc;
797}
798
799
800/**
801 * @interface_method_impl{PDMIBASE,pfnQueryInterface,
802 * For VNETSTATECC::VPCI.IBase}
803 */
804static DECLCALLBACK(void *) vnetQueryInterface(struct PDMIBASE *pInterface, const char *pszIID)
805{
806 PVNETSTATECC pThisCC = RT_FROM_MEMBER(pInterface, VNETSTATECC, VPCI.IBase);
807
808 PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKDOWN, &pThisCC->INetworkDown);
809 PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKCONFIG, &pThisCC->INetworkConfig);
810
811 return vpciR3QueryInterface(&pThisCC->VPCI, pszIID);
812}
813
814/**
815 * Returns true if it is a broadcast packet.
816 *
817 * @returns true if destination address indicates broadcast.
818 * @param pvBuf The ethernet packet.
819 */
820DECLINLINE(bool) vnetR3IsBroadcast(const void *pvBuf)
821{
822 static const uint8_t s_abBcastAddr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
823 return memcmp(pvBuf, s_abBcastAddr, sizeof(s_abBcastAddr)) == 0;
824}
825
826/**
827 * Returns true if it is a multicast packet.
828 *
829 * @remarks returns true for broadcast packets as well.
830 * @returns true if destination address indicates multicast.
831 * @param pvBuf The ethernet packet.
832 */
833DECLINLINE(bool) vnetR3IsMulticast(const void *pvBuf)
834{
835 return (*(char*)pvBuf) & 1;
836}
837
838/**
839 * Determines if the packet is to be delivered to upper layer.
840 *
841 * @returns true if packet is intended for this node.
842 * @param pThis Pointer to the state structure.
843 * @param pvBuf The ethernet packet.
844 * @param cb Number of bytes available in the packet.
845 */
846static bool vnetR3AddressFilter(PVNETSTATE pThis, const void *pvBuf, size_t cb)
847{
848 if (pThis->fPromiscuous)
849 return true;
850
851 /* Ignore everything outside of our VLANs */
852 uint16_t *u16Ptr = (uint16_t*)pvBuf;
853 /* Compare TPID with VLAN Ether Type */
854 if ( u16Ptr[6] == RT_H2BE_U16(0x8100)
855 && !ASMBitTest(pThis->aVlanFilter, RT_BE2H_U16(u16Ptr[7]) & 0xFFF))
856 {
857 Log4(("%s vnetR3AddressFilter: not our VLAN, returning false\n", INSTANCE(pThis)));
858 return false;
859 }
860
861 if (vnetR3IsBroadcast(pvBuf))
862 return true;
863
864 if (pThis->fAllMulti && vnetR3IsMulticast(pvBuf))
865 return true;
866
867 if (!memcmp(pThis->config.mac.au8, pvBuf, sizeof(RTMAC)))
868 return true;
869 Log4(("%s vnetR3AddressFilter: %RTmac (conf) != %RTmac (dest)\n", INSTANCE(pThis), pThis->config.mac.au8, pvBuf));
870
871 for (unsigned i = 0; i < pThis->cMacFilterEntries; i++)
872 if (!memcmp(&pThis->aMacFilter[i], pvBuf, sizeof(RTMAC)))
873 return true;
874
875 Log2(("%s vnetR3AddressFilter: failed all tests, returning false, packet dump follows:\n", INSTANCE(pThis)));
876 vnetR3PacketDump(pThis, (const uint8_t *)pvBuf, cb, "<-- Incoming");
877
878 return false;
879}
880
881/**
882 * Pad and store received packet.
883 *
884 * @remarks Make sure that the packet appears to upper layer as one coming
885 * from real Ethernet: pad it and insert FCS.
886 *
887 * @returns VBox status code.
888 * @param pDevIns The device instance.
889 * @param pThis The virtio-net shared instance data.
890 * @param pvBuf The available data.
891 * @param cb Number of bytes available in the buffer.
892 * @thread RX
893 */
894static int vnetR3HandleRxPacket(PPDMDEVINS pDevIns, PVNETSTATE pThis, PVNETSTATECC pThisCC,
895 const void *pvBuf, size_t cb, PCPDMNETWORKGSO pGso)
896{
897 VNETHDRMRX Hdr;
898 unsigned cbHdr;
899 RTGCPHYS addrHdrMrx = 0;
900
901 if (pGso)
902 {
903 Log2(("%s vnetR3HandleRxPacket: gso type=%x cbHdrsTotal=%u cbHdrsSeg=%u mss=%u off1=0x%x off2=0x%x\n",
904 INSTANCE(pThis), pGso->u8Type, pGso->cbHdrsTotal, pGso->cbHdrsSeg, pGso->cbMaxSeg, pGso->offHdr1, pGso->offHdr2));
905 Hdr.Hdr.u8Flags = VNETHDR_F_NEEDS_CSUM;
906 switch (pGso->u8Type)
907 {
908 case PDMNETWORKGSOTYPE_IPV4_TCP:
909 Hdr.Hdr.u8GSOType = VNETHDR_GSO_TCPV4;
910 Hdr.Hdr.u16CSumOffset = RT_OFFSETOF(RTNETTCP, th_sum);
911 break;
912 case PDMNETWORKGSOTYPE_IPV6_TCP:
913 Hdr.Hdr.u8GSOType = VNETHDR_GSO_TCPV6;
914 Hdr.Hdr.u16CSumOffset = RT_OFFSETOF(RTNETTCP, th_sum);
915 break;
916 case PDMNETWORKGSOTYPE_IPV4_UDP:
917 Hdr.Hdr.u8GSOType = VNETHDR_GSO_UDP;
918 Hdr.Hdr.u16CSumOffset = RT_OFFSETOF(RTNETUDP, uh_sum);
919 break;
920 default:
921 return VERR_INVALID_PARAMETER;
922 }
923 Hdr.Hdr.u16HdrLen = pGso->cbHdrsTotal;
924 Hdr.Hdr.u16GSOSize = pGso->cbMaxSeg;
925 Hdr.Hdr.u16CSumStart = pGso->offHdr2;
926 Hdr.u16NumBufs = 0;
927 STAM_REL_COUNTER_INC(&pThis->StatReceiveGSO);
928 }
929 else
930 {
931 Hdr.Hdr.u8Flags = 0;
932 Hdr.Hdr.u8GSOType = VNETHDR_GSO_NONE;
933 Hdr.Hdr.u16HdrLen = 0;
934 Hdr.Hdr.u16GSOSize = 0;
935 Hdr.Hdr.u16CSumStart = 0;
936 Hdr.Hdr.u16CSumOffset = 0;
937 Hdr.u16NumBufs = 0;
938 }
939
940 if (vnetR3MergeableRxBuffers(pThis))
941 cbHdr = sizeof(VNETHDRMRX);
942 else
943 cbHdr = sizeof(VNETHDR);
944
945 vnetR3PacketDump(pThis, (const uint8_t *)pvBuf, cb, "<-- Incoming");
946
947 unsigned int uOffset = 0;
948 unsigned int nElem;
949 for (nElem = 0; uOffset < cb; nElem++)
950 {
951 VQUEUEELEM elem;
952 unsigned int nSeg = 0, uElemSize = 0, cbReserved = 0;
953
954 if (!vqueueGet(pDevIns, &pThis->VPCI, pThisCC->pRxQueue, &elem))
955 {
956 /*
957 * @todo: It is possible to run out of RX buffers if only a few
958 * were added and we received a big packet.
959 */
960 Log(("%s vnetR3HandleRxPacket: Suddenly there is no space in receive queue!\n", INSTANCE(pThis)));
961 return VERR_INTERNAL_ERROR;
962 }
963
964 if (elem.cIn < 1)
965 {
966 Log(("%s vnetR3HandleRxPacket: No writable descriptors in receive queue!\n", INSTANCE(pThis)));
967 return VERR_INTERNAL_ERROR;
968 }
969
970 if (nElem == 0)
971 {
972 if (vnetR3MergeableRxBuffers(pThis))
973 {
974 addrHdrMrx = elem.aSegsIn[nSeg].addr;
975 cbReserved = cbHdr;
976 }
977 else
978 {
979 /* The very first segment of the very first element gets the header. */
980 if (elem.aSegsIn[nSeg].cb != sizeof(VNETHDR))
981 {
982 Log(("%s vnetR3HandleRxPacket: The first descriptor does match the header size!\n", INSTANCE(pThis)));
983 return VERR_INTERNAL_ERROR;
984 }
985 elem.aSegsIn[nSeg++].pv = &Hdr;
986 }
987 uElemSize += cbHdr;
988 }
989 while (nSeg < elem.cIn && uOffset < cb)
990 {
991 unsigned int uSize = (unsigned int)RT_MIN(elem.aSegsIn[nSeg].cb - (nSeg?0:cbReserved),
992 cb - uOffset);
993 elem.aSegsIn[nSeg++].pv = (uint8_t*)pvBuf + uOffset;
994 uOffset += uSize;
995 uElemSize += uSize;
996 }
997 STAM_PROFILE_START(&pThis->StatReceiveStore, a);
998 vqueuePut(pDevIns, &pThis->VPCI, pThisCC->pRxQueue, &elem, uElemSize, cbReserved);
999 STAM_PROFILE_STOP(&pThis->StatReceiveStore, a);
1000 if (!vnetR3MergeableRxBuffers(pThis))
1001 break;
1002 cbReserved = 0;
1003 }
1004 if (vnetR3MergeableRxBuffers(pThis))
1005 {
1006 Hdr.u16NumBufs = nElem;
1007 int rc = PDMDevHlpPCIPhysWrite(pDevIns, addrHdrMrx,
1008 &Hdr, sizeof(Hdr));
1009 if (RT_FAILURE(rc))
1010 {
1011 Log(("%s vnetR3HandleRxPacket: Failed to write merged RX buf header: %Rrc\n", INSTANCE(pThis), rc));
1012 return rc;
1013 }
1014 }
1015 vqueueSync(pDevIns, &pThis->VPCI, pThisCC->pRxQueue);
1016 if (uOffset < cb)
1017 {
1018 Log(("%s vnetR3HandleRxPacket: Packet did not fit into RX queue (packet size=%u)!\n", INSTANCE(pThis), cb));
1019 return VERR_TOO_MUCH_DATA;
1020 }
1021
1022 return VINF_SUCCESS;
1023}
1024
1025/**
1026 * @interface_method_impl{PDMINETWORKDOWN,pfnReceiveGso}
1027 */
1028static DECLCALLBACK(int) vnetR3NetworkDown_ReceiveGso(PPDMINETWORKDOWN pInterface, const void *pvBuf,
1029 size_t cb, PCPDMNETWORKGSO pGso)
1030{
1031 PVNETSTATECC pThisCC = RT_FROM_MEMBER(pInterface, VNETSTATECC, INetworkDown);
1032 PPDMDEVINS pDevIns = pThisCC->pDevIns;
1033 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
1034
1035 if (pGso)
1036 {
1037 uint32_t uFeatures = pThis->VPCI.uGuestFeatures;
1038
1039 switch (pGso->u8Type)
1040 {
1041 case PDMNETWORKGSOTYPE_IPV4_TCP:
1042 uFeatures &= VNET_F_GUEST_TSO4;
1043 break;
1044 case PDMNETWORKGSOTYPE_IPV6_TCP:
1045 uFeatures &= VNET_F_GUEST_TSO6;
1046 break;
1047 case PDMNETWORKGSOTYPE_IPV4_UDP:
1048 case PDMNETWORKGSOTYPE_IPV6_UDP:
1049 uFeatures &= VNET_F_GUEST_UFO;
1050 break;
1051 default:
1052 uFeatures = 0;
1053 break;
1054 }
1055 if (!uFeatures)
1056 {
1057 Log2(("%s vnetR3NetworkDown_ReceiveGso: GSO type (0x%x) not supported\n", INSTANCE(pThis), pGso->u8Type));
1058 return VERR_NOT_SUPPORTED;
1059 }
1060 }
1061
1062 Log2(("%s vnetR3NetworkDown_ReceiveGso: pvBuf=%p cb=%u pGso=%p\n", INSTANCE(pThis), pvBuf, cb, pGso));
1063 int rc = vnetR3CanReceive(pDevIns, pThis, pThisCC);
1064 if (RT_FAILURE(rc))
1065 return rc;
1066
1067 /* Drop packets if VM is not running or cable is disconnected. */
1068 VMSTATE enmVMState = PDMDevHlpVMState(pDevIns);
1069 if (( enmVMState != VMSTATE_RUNNING
1070 && enmVMState != VMSTATE_RUNNING_LS)
1071 || !(pThis->config.uStatus & VNET_S_LINK_UP))
1072 return VINF_SUCCESS;
1073
1074 STAM_PROFILE_START(&pThis->StatReceive, a);
1075 vpciR3SetReadLed(&pThis->VPCI, true);
1076 if (vnetR3AddressFilter(pThis, pvBuf, cb))
1077 {
1078 rc = vnetCsRxEnter(pThis, VERR_SEM_BUSY);
1079 if (RT_SUCCESS(rc))
1080 {
1081 rc = vnetR3HandleRxPacket(pDevIns, pThis, pThisCC, pvBuf, cb, pGso);
1082 STAM_REL_COUNTER_ADD(&pThis->StatReceiveBytes, cb);
1083 vnetCsRxLeave(pThis);
1084 }
1085 }
1086 vpciR3SetReadLed(&pThis->VPCI, false);
1087 STAM_PROFILE_STOP(&pThis->StatReceive, a);
1088 return rc;
1089}
1090
1091/**
1092 * @interface_method_impl{PDMINETWORKDOWN,pfnReceive}
1093 */
1094static DECLCALLBACK(int) vnetR3NetworkDown_Receive(PPDMINETWORKDOWN pInterface, const void *pvBuf, size_t cb)
1095{
1096 return vnetR3NetworkDown_ReceiveGso(pInterface, pvBuf, cb, NULL);
1097}
1098
1099/**
1100 * @interface_method_impl{PDMINETWORKCONFIG,pfnGetMac}
1101 */
1102static DECLCALLBACK(int) vnetR3NetworkConfig_GetMac(PPDMINETWORKCONFIG pInterface, PRTMAC pMac)
1103{
1104 PVNETSTATECC pThisCC = RT_FROM_MEMBER(pInterface, VNETSTATECC, INetworkConfig);
1105 PVNETSTATE pThis = PDMDEVINS_2_DATA(pThisCC->pDevIns, PVNETSTATE);
1106 memcpy(pMac, pThis->config.mac.au8, sizeof(RTMAC));
1107 return VINF_SUCCESS;
1108}
1109
1110/**
1111 * @interface_method_impl{PDMINETWORKCONFIG,pfnGetLinkState}
1112 */
1113static DECLCALLBACK(PDMNETWORKLINKSTATE) vnetR3NetworkConfig_GetLinkState(PPDMINETWORKCONFIG pInterface)
1114{
1115 PVNETSTATECC pThisCC = RT_FROM_MEMBER(pInterface, VNETSTATECC, INetworkConfig);
1116 PVNETSTATE pThis = PDMDEVINS_2_DATA(pThisCC->pDevIns, PVNETSTATE);
1117 if (pThis->config.uStatus & VNET_S_LINK_UP)
1118 return PDMNETWORKLINKSTATE_UP;
1119 return PDMNETWORKLINKSTATE_DOWN;
1120}
1121
1122/**
1123 * @interface_method_impl{PDMINETWORKCONFIG,pfnSetLinkState}
1124 */
1125static DECLCALLBACK(int) vnetR3NetworkConfig_SetLinkState(PPDMINETWORKCONFIG pInterface, PDMNETWORKLINKSTATE enmState)
1126{
1127 PVNETSTATECC pThisCC = RT_FROM_MEMBER(pInterface, VNETSTATECC, INetworkConfig);
1128 PPDMDEVINS pDevIns = pThisCC->pDevIns;
1129 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
1130 bool fOldUp = !!(pThis->config.uStatus & VNET_S_LINK_UP);
1131 bool fNewUp = enmState == PDMNETWORKLINKSTATE_UP;
1132
1133 Log(("%s vnetR3NetworkConfig_SetLinkState: enmState=%d\n", INSTANCE(pThis), enmState));
1134 if (enmState == PDMNETWORKLINKSTATE_DOWN_RESUME)
1135 {
1136 if (fOldUp)
1137 {
1138 /*
1139 * We bother to bring the link down only if it was up previously. The UP link state
1140 * notification will be sent when the link actually goes up in vnetR3LinkUpTimer().
1141 */
1142 vnetR3TempLinkDown(pDevIns, pThis, pThisCC);
1143 if (pThisCC->pDrv)
1144 pThisCC->pDrv->pfnNotifyLinkChanged(pThisCC->pDrv, enmState);
1145 }
1146 }
1147 else if (fNewUp != fOldUp)
1148 {
1149 if (fNewUp)
1150 {
1151 pThis->fCableConnected = true;
1152 /* The link state depends both on the cable connected and device attached. */
1153 if (vnetR3IsConnected(pDevIns))
1154 {
1155 Log(("%s Link is up\n", INSTANCE(pThis)));
1156 pThis->config.uStatus |= VNET_S_LINK_UP;
1157 vnetR3RaiseInterrupt(pDevIns, pThis, VERR_SEM_BUSY, VPCI_ISR_CONFIG);
1158 }
1159 }
1160 else
1161 {
1162 /* The link was brought down explicitly, make sure it won't come up by timer. */
1163 PDMDevHlpTimerStop(pDevIns, pThisCC->hLinkUpTimer);
1164 Log(("%s Link is down\n", INSTANCE(pThis)));
1165 pThis->fCableConnected = false;
1166 pThis->config.uStatus &= ~VNET_S_LINK_UP;
1167 vnetR3RaiseInterrupt(pDevIns, pThis, VERR_SEM_BUSY, VPCI_ISR_CONFIG);
1168 }
1169 if (pThisCC->pDrv)
1170 pThisCC->pDrv->pfnNotifyLinkChanged(pThisCC->pDrv, enmState);
1171 }
1172 return VINF_SUCCESS;
1173}
1174
1175/**
1176 * @callback_method_impl{FNVPCIQUEUECALLBACK, The RX queue}
1177 */
1178static DECLCALLBACK(void) vnetR3QueueReceive(PPDMDEVINS pDevIns, PVQUEUE pQueue)
1179{
1180 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
1181 RT_NOREF(pThis, pQueue);
1182 Log(("%s Receive buffers has been added, waking up receive thread.\n", INSTANCE(pThis)));
1183 vnetWakeupReceive(pDevIns);
1184}
1185
1186/**
1187 * Sets up the GSO context according to the Virtio header.
1188 *
1189 * @param pGso The GSO context to setup.
1190 * @param pCtx The context descriptor.
1191 */
1192DECLINLINE(PPDMNETWORKGSO) vnetR3SetupGsoCtx(PPDMNETWORKGSO pGso, VNETHDR const *pHdr)
1193{
1194 pGso->u8Type = PDMNETWORKGSOTYPE_INVALID;
1195
1196 if (pHdr->u8GSOType & VNETHDR_GSO_ECN)
1197 {
1198 AssertMsgFailed(("Unsupported flag in virtio header: ECN\n"));
1199 return NULL;
1200 }
1201 switch (pHdr->u8GSOType & ~VNETHDR_GSO_ECN)
1202 {
1203 case VNETHDR_GSO_TCPV4:
1204 pGso->u8Type = PDMNETWORKGSOTYPE_IPV4_TCP;
1205 pGso->cbHdrsSeg = pHdr->u16HdrLen;
1206 break;
1207 case VNETHDR_GSO_TCPV6:
1208 pGso->u8Type = PDMNETWORKGSOTYPE_IPV6_TCP;
1209 pGso->cbHdrsSeg = pHdr->u16HdrLen;
1210 break;
1211 case VNETHDR_GSO_UDP:
1212 pGso->u8Type = PDMNETWORKGSOTYPE_IPV4_UDP;
1213 pGso->cbHdrsSeg = pHdr->u16CSumStart;
1214 break;
1215 default:
1216 return NULL;
1217 }
1218 if (pHdr->u8Flags & VNETHDR_F_NEEDS_CSUM)
1219 pGso->offHdr2 = pHdr->u16CSumStart;
1220 else
1221 {
1222 AssertMsgFailed(("GSO without checksum offloading!\n"));
1223 return NULL;
1224 }
1225 pGso->offHdr1 = sizeof(RTNETETHERHDR);
1226 pGso->cbHdrsTotal = pHdr->u16HdrLen;
1227 pGso->cbMaxSeg = pHdr->u16GSOSize;
1228 return pGso;
1229}
1230
1231DECLINLINE(uint16_t) vnetR3CSum16(const void *pvBuf, size_t cb)
1232{
1233 uint32_t csum = 0;
1234 uint16_t *pu16 = (uint16_t *)pvBuf;
1235
1236 while (cb > 1)
1237 {
1238 csum += *pu16++;
1239 cb -= 2;
1240 }
1241 if (cb)
1242 csum += *(uint8_t*)pu16;
1243 while (csum >> 16)
1244 csum = (csum >> 16) + (csum & 0xFFFF);
1245 return ~csum;
1246}
1247
1248DECLINLINE(void) vnetR3CompleteChecksum(uint8_t *pBuf, size_t cbSize, uint16_t uStart, uint16_t uOffset)
1249{
1250 AssertReturnVoid(uStart < cbSize);
1251 AssertReturnVoid(uStart + uOffset + sizeof(uint16_t) <= cbSize);
1252 *(uint16_t *)(pBuf + uStart + uOffset) = vnetR3CSum16(pBuf + uStart, cbSize - uStart);
1253}
1254
1255static bool vnetR3ReadHeader(PPDMDEVINS pDevIns, RTGCPHYS GCPhys, PVNETHDR pHdr, uint32_t cbMax)
1256{
1257 int rc = PDMDevHlpPhysRead(pDevIns, GCPhys, pHdr, sizeof(*pHdr)); /** @todo r=bird: Why not PDMDevHlpPCIPhysRead? */
1258 if (RT_FAILURE(rc))
1259 return false;
1260
1261 Log4(("virtio-net: header flags=%x gso-type=%x len=%x gso-size=%x csum-start=%x csum-offset=%x cb=%x\n",
1262 pHdr->u8Flags, pHdr->u8GSOType, pHdr->u16HdrLen, pHdr->u16GSOSize, pHdr->u16CSumStart, pHdr->u16CSumOffset, cbMax));
1263
1264 if (pHdr->u8GSOType)
1265 {
1266 uint32_t u32MinHdrSize;
1267
1268 /* Segmentation offloading cannot be done without checksumming. */
1269 if (RT_UNLIKELY(!(pHdr->u8Flags & VNETHDR_F_NEEDS_CSUM)))
1270 return false;
1271 /* We do not support ECN. */
1272 if (RT_UNLIKELY(pHdr->u8GSOType & VNETHDR_GSO_ECN))
1273 return false;
1274 switch (pHdr->u8GSOType)
1275 {
1276 case VNETHDR_GSO_TCPV4:
1277 case VNETHDR_GSO_TCPV6:
1278 u32MinHdrSize = sizeof(RTNETTCP);
1279 break;
1280 case VNETHDR_GSO_UDP:
1281 u32MinHdrSize = 0;
1282 break;
1283 default:
1284 return false;
1285 }
1286 /* Header+MSS must not exceed the packet size. */
1287 if (RT_UNLIKELY(u32MinHdrSize + pHdr->u16CSumStart + pHdr->u16GSOSize > cbMax))
1288 return false;
1289 }
1290 /* Checksum must fit into the frame (validating both checksum fields). */
1291 if ( (pHdr->u8Flags & VNETHDR_F_NEEDS_CSUM)
1292 && sizeof(uint16_t) + pHdr->u16CSumStart + pHdr->u16CSumOffset > cbMax)
1293 return false;
1294 Log4(("virtio-net: return true\n"));
1295 return true;
1296}
1297
1298static int vnetR3TransmitFrame(PVNETSTATE pThis, PVNETSTATECC pThisCC, PPDMSCATTERGATHER pSgBuf,
1299 PPDMNETWORKGSO pGso, PVNETHDR pHdr)
1300{
1301 vnetR3PacketDump(pThis, (uint8_t *)pSgBuf->aSegs[0].pvSeg, pSgBuf->cbUsed, "--> Outgoing");
1302 if (pGso)
1303 {
1304 /* Some guests (RHEL) may report HdrLen excluding transport layer header! */
1305 /*
1306 * We cannot use cdHdrs provided by the guest because of different ways
1307 * it gets filled out by different versions of kernels.
1308 */
1309 //if (pGso->cbHdrs < pHdr->u16CSumStart + pHdr->u16CSumOffset + 2)
1310 {
1311 Log4(("%s vnetR3TransmitPendingPackets: HdrLen before adjustment %d.\n",
1312 INSTANCE(pThis), pGso->cbHdrsTotal));
1313 switch (pGso->u8Type)
1314 {
1315 case PDMNETWORKGSOTYPE_IPV4_TCP:
1316 case PDMNETWORKGSOTYPE_IPV6_TCP:
1317 pGso->cbHdrsTotal = pHdr->u16CSumStart +
1318 ((PRTNETTCP)(((uint8_t*)pSgBuf->aSegs[0].pvSeg) + pHdr->u16CSumStart))->th_off * 4;
1319 AssertMsgReturn(pSgBuf->cbUsed > pGso->cbHdrsTotal,
1320 ("cbHdrsTotal exceeds size of frame"), VERR_BUFFER_OVERFLOW);
1321 pGso->cbHdrsSeg = pGso->cbHdrsTotal;
1322 break;
1323 case PDMNETWORKGSOTYPE_IPV4_UDP:
1324 pGso->cbHdrsTotal = (uint8_t)(pHdr->u16CSumStart + sizeof(RTNETUDP));
1325 pGso->cbHdrsSeg = pHdr->u16CSumStart;
1326 break;
1327 }
1328 /* Update GSO structure embedded into the frame */
1329 ((PPDMNETWORKGSO)pSgBuf->pvUser)->cbHdrsTotal = pGso->cbHdrsTotal;
1330 ((PPDMNETWORKGSO)pSgBuf->pvUser)->cbHdrsSeg = pGso->cbHdrsSeg;
1331 Log4(("%s vnetR3TransmitPendingPackets: adjusted HdrLen to %d.\n",
1332 INSTANCE(pThis), pGso->cbHdrsTotal));
1333 }
1334 Log2(("%s vnetR3TransmitPendingPackets: gso type=%x cbHdrsTotal=%u cbHdrsSeg=%u mss=%u off1=0x%x off2=0x%x\n",
1335 INSTANCE(pThis), pGso->u8Type, pGso->cbHdrsTotal, pGso->cbHdrsSeg, pGso->cbMaxSeg, pGso->offHdr1, pGso->offHdr2));
1336 STAM_REL_COUNTER_INC(&pThis->StatTransmitGSO);
1337 }
1338 else if (pHdr->u8Flags & VNETHDR_F_NEEDS_CSUM)
1339 {
1340 STAM_REL_COUNTER_INC(&pThis->StatTransmitCSum);
1341 /*
1342 * This is not GSO frame but checksum offloading is requested.
1343 */
1344 vnetR3CompleteChecksum((uint8_t*)pSgBuf->aSegs[0].pvSeg, pSgBuf->cbUsed,
1345 pHdr->u16CSumStart, pHdr->u16CSumOffset);
1346 }
1347
1348 return pThisCC->pDrv->pfnSendBuf(pThisCC->pDrv, pSgBuf, false);
1349}
1350
1351static void vnetR3TransmitPendingPackets(PPDMDEVINS pDevIns, PVNETSTATE pThis, PVNETSTATECC pThisCC,
1352 PVQUEUE pQueue, bool fOnWorkerThread)
1353{
1354 /*
1355 * Let's deal with the cases we are not going to transmit anything,
1356 * to avoid setting 'uIsTransmitting' on and off.
1357 */
1358 if ((pThis->VPCI.uStatus & VPCI_STATUS_DRV_OK) == 0)
1359 {
1360 Log(("%s Ignoring transmit requests from non-existent driver (status=0x%x).\n", INSTANCE(pThis), pThis->VPCI.uStatus));
1361 return;
1362 }
1363
1364 if (!vnetR3IsConnected(pDevIns))
1365 {
1366 Log(("%s Ignoring transmit requests while cable is disconnected.\n", INSTANCE(pThis)));
1367 return;
1368 }
1369
1370 /*
1371 * Only one thread is allowed to transmit at a time, others should skip
1372 * transmission as the packets will be picked up by the transmitting
1373 * thread.
1374 */
1375 if (!ASMAtomicCmpXchgU32(&pThis->uIsTransmitting, 1, 0))
1376 return;
1377
1378 PPDMINETWORKUP pDrv = pThisCC->pDrv;
1379 if (pDrv)
1380 {
1381 int rc = pDrv->pfnBeginXmit(pDrv, fOnWorkerThread);
1382 Assert(rc == VINF_SUCCESS || rc == VERR_TRY_AGAIN);
1383 if (rc == VERR_TRY_AGAIN)
1384 {
1385 ASMAtomicWriteU32(&pThis->uIsTransmitting, 0);
1386 return;
1387 }
1388 }
1389
1390 unsigned int cbHdr;
1391 if (vnetR3MergeableRxBuffers(pThis))
1392 cbHdr = sizeof(VNETHDRMRX);
1393 else
1394 cbHdr = sizeof(VNETHDR);
1395
1396 Log3(("%s vnetR3TransmitPendingPackets: About to transmit %d pending packets\n",
1397 INSTANCE(pThis), vringReadAvailIndex(pDevIns, &pThisCC->pTxQueue->VRing) - pThisCC->pTxQueue->uNextAvailIndex));
1398
1399 vpciR3SetWriteLed(&pThis->VPCI, true);
1400
1401 /*
1402 * Do not remove descriptors from available ring yet, try to allocate the
1403 * buffer first.
1404 */
1405 VQUEUEELEM elem; /* This bugger is big! ~48KB on 64-bit hosts. */
1406 while (vqueuePeek(pDevIns, &pThis->VPCI, pQueue, &elem))
1407 {
1408 unsigned int uOffset = 0;
1409 if (elem.cOut < 2 || elem.aSegsOut[0].cb != cbHdr)
1410 {
1411 Log(("%s vnetR3QueueTransmit: The first segment is not the header! (%u < 2 || %u != %u).\n",
1412 INSTANCE(pThis), elem.cOut, elem.aSegsOut[0].cb, cbHdr));
1413 break; /* For now we simply ignore the header, but it must be there anyway! */
1414 }
1415 RT_UNTRUSTED_VALIDATED_FENCE();
1416
1417 VNETHDR Hdr;
1418 unsigned int uSize = 0;
1419 STAM_PROFILE_ADV_START(&pThis->StatTransmit, a);
1420
1421 /* Compute total frame size. */
1422 for (unsigned int i = 1; i < elem.cOut && uSize < VNET_MAX_FRAME_SIZE; i++)
1423 uSize += elem.aSegsOut[i].cb;
1424 Log5(("%s vnetR3TransmitPendingPackets: complete frame is %u bytes.\n", INSTANCE(pThis), uSize));
1425 Assert(uSize <= VNET_MAX_FRAME_SIZE);
1426
1427 /* Truncate oversized frames. */
1428 if (uSize > VNET_MAX_FRAME_SIZE)
1429 uSize = VNET_MAX_FRAME_SIZE;
1430 if (pThisCC->pDrv && vnetR3ReadHeader(pDevIns, elem.aSegsOut[0].addr, &Hdr, uSize))
1431 {
1432 RT_UNTRUSTED_VALIDATED_FENCE();
1433 STAM_REL_COUNTER_INC(&pThis->StatTransmitPackets);
1434 STAM_PROFILE_START(&pThis->StatTransmitSend, a);
1435
1436 PDMNETWORKGSO Gso;
1437 PDMNETWORKGSO *pGso = vnetR3SetupGsoCtx(&Gso, &Hdr);
1438
1439 /** @todo Optimize away the extra copying! (lazy bird) */
1440 PPDMSCATTERGATHER pSgBuf;
1441 int rc = pThisCC->pDrv->pfnAllocBuf(pThisCC->pDrv, uSize, pGso, &pSgBuf);
1442 if (RT_SUCCESS(rc))
1443 {
1444 Assert(pSgBuf->cSegs == 1);
1445 pSgBuf->cbUsed = uSize;
1446
1447 /* Assemble a complete frame. */
1448 for (unsigned int i = 1; i < elem.cOut && uSize > 0; i++)
1449 {
1450 unsigned int cbSegment = RT_MIN(uSize, elem.aSegsOut[i].cb);
1451 PDMDevHlpPhysRead(pDevIns, elem.aSegsOut[i].addr,
1452 ((uint8_t*)pSgBuf->aSegs[0].pvSeg) + uOffset,
1453 cbSegment);
1454 uOffset += cbSegment;
1455 uSize -= cbSegment;
1456 }
1457 rc = vnetR3TransmitFrame(pThis, pThisCC, pSgBuf, pGso, &Hdr);
1458 }
1459 else
1460 {
1461 Log4(("virtio-net: failed to allocate SG buffer: size=%u rc=%Rrc\n", uSize, rc));
1462 STAM_PROFILE_STOP(&pThis->StatTransmitSend, a);
1463 STAM_PROFILE_ADV_STOP(&pThis->StatTransmit, a);
1464 /* Stop trying to fetch TX descriptors until we get more bandwidth. */
1465 break;
1466 }
1467
1468 STAM_PROFILE_STOP(&pThis->StatTransmitSend, a);
1469 STAM_REL_COUNTER_ADD(&pThis->StatTransmitBytes, uOffset);
1470 }
1471
1472 /* Remove this descriptor chain from the available ring */
1473 vqueueSkip(pDevIns, &pThis->VPCI, pQueue);
1474 vqueuePut(pDevIns, &pThis->VPCI, pQueue, &elem, sizeof(VNETHDR) + uOffset);
1475 vqueueSync(pDevIns, &pThis->VPCI, pQueue);
1476 STAM_PROFILE_ADV_STOP(&pThis->StatTransmit, a);
1477 }
1478 vpciR3SetWriteLed(&pThis->VPCI, false);
1479
1480 if (pDrv)
1481 pDrv->pfnEndXmit(pDrv);
1482 ASMAtomicWriteU32(&pThis->uIsTransmitting, 0);
1483}
1484
1485/**
1486 * @interface_method_impl{PDMINETWORKDOWN,pfnXmitPending}
1487 */
1488static DECLCALLBACK(void) vnetR3NetworkDown_XmitPending(PPDMINETWORKDOWN pInterface)
1489{
1490 PVNETSTATECC pThisCC = RT_FROM_MEMBER(pInterface, VNETSTATECC, INetworkDown);
1491 PPDMDEVINS pDevIns = pThisCC->pDevIns;
1492 PVNETSTATE pThis = PDMDEVINS_2_DATA(pThisCC->pDevIns, PVNETSTATE);
1493 STAM_COUNTER_INC(&pThis->StatTransmitByNetwork);
1494 vnetR3TransmitPendingPackets(pDevIns, pThis, pThisCC, pThisCC->pTxQueue, false /*fOnWorkerThread*/);
1495}
1496
1497# ifdef VNET_TX_DELAY
1498
1499/**
1500 * @callback_method_impl{FNVPCIQUEUECALLBACK, The TX queue}
1501 */
1502static DECLCALLBACK(void) vnetR3QueueTransmit(PPDMDEVINS pDevIns, PVQUEUE pQueue)
1503{
1504 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
1505 PVNETSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVNETSTATECC);
1506
1507 if (PDMDevHlpTimerIsActive(pDevIns, pThis->hTxTimer))
1508 {
1509 PDMDevHlpTimerStop(pDevIns, pThis->hTxTimer);
1510 Log3(("%s vnetR3QueueTransmit: Got kicked with notification disabled, re-enable notification and flush TX queue\n", INSTANCE(pThis)));
1511 vnetR3TransmitPendingPackets(pDevIns, pThis, pThisCC, pQueue, false /*fOnWorkerThread*/);
1512
1513 VNET_R3_CS_ENTER_RETURN_VOID(pDevIns, pThis);
1514
1515 vringSetNotification(pDevIns, &pThisCC->pTxQueue->VRing, true);
1516
1517 vnetR3CsLeave(pDevIns, pThis);
1518 }
1519 else
1520 {
1521 VNET_R3_CS_ENTER_RETURN_VOID(pDevIns, pThis);
1522
1523 vringSetNotification(pDevIns, &pThisCC->pTxQueue->VRing, false);
1524 PDMDevHlpTimerSetMicro(pDevIns, pThis->hTxTimer, VNET_TX_DELAY);
1525 pThis->u64NanoTS = RTTimeNanoTS();
1526
1527 vnetR3CsLeave(pDevIns, pThis);
1528 }
1529}
1530
1531/**
1532 * @callback_method_impl{FNTMTIMERDEV, Transmit Delay Timer handler.}
1533 */
1534static DECLCALLBACK(void) vnetR3TxTimer(PPDMDEVINS pDevIns, TMTIMERHANDLE hTimer, void *pvUser)
1535{
1536 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
1537 PVNETSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVNETSTATECC);
1538 RT_NOREF(hTimer, pvUser);
1539
1540 uint32_t u32MicroDiff = (uint32_t)((RTTimeNanoTS() - pThis->u64NanoTS) / 1000);
1541 if (u32MicroDiff < pThis->u32MinDiff)
1542 pThis->u32MinDiff = u32MicroDiff;
1543 if (u32MicroDiff > pThis->u32MaxDiff)
1544 pThis->u32MaxDiff = u32MicroDiff;
1545 pThis->u32AvgDiff = (pThis->u32AvgDiff * pThis->u32i + u32MicroDiff) / (pThis->u32i + 1);
1546 pThis->u32i++;
1547 Log3(("vnetR3TxTimer: Expired, diff %9d usec, avg %9d usec, min %9d usec, max %9d usec\n",
1548 u32MicroDiff, pThis->u32AvgDiff, pThis->u32MinDiff, pThis->u32MaxDiff));
1549
1550// Log3(("%s vnetR3TxTimer: Expired\n", INSTANCE(pThis)));
1551 vnetR3TransmitPendingPackets(pDevIns, pThis, pThisCC, pThisCC->pTxQueue, false /*fOnWorkerThread*/);
1552
1553 VNET_R3_CS_ENTER_RETURN_VOID(pDevIns, pThis);
1554 vringSetNotification(pDevIns, &pThisCC->pTxQueue->VRing, true);
1555 vnetR3CsLeave(pDevIns, pThis);
1556}
1557
1558DECLINLINE(int) vnetR3CreateTxThreadAndEvent(PPDMDEVINS pDevIns, PVNETSTATE pThis, PVNETSTATECC pThisCC)
1559{
1560 RT_NOREF(pDevIns, pThis, pThisCC);
1561 return VINF_SUCCESS;
1562}
1563
1564DECLINLINE(void) vnetR3DestroyTxThreadAndEvent(PPDMDEVINS pDevIns, PVNETSTATE pThis, PVNETSTATECC pThisCC)
1565{
1566 RT_NOREF(pDevIns, pThis, pThisCC);
1567}
1568
1569# else /* !VNET_TX_DELAY */
1570
1571/**
1572 * @callback_method_impl{FNPDMTHREADDEV}
1573 */
1574static DECLCALLBACK(int) vnetR3TxThread(PPDMDEVINS pDevIns, PPDMTHREAD pThread)
1575{
1576 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
1577 PVNETSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVNETSTATECC);
1578
1579 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
1580 return VINF_SUCCESS;
1581
1582 int rc = VINF_SUCCESS;
1583 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
1584 {
1585 rc = PDMDevHlpSUPSemEventWaitNoResume(pDevIns, pThis->hTxEvent, RT_INDEFINITE_WAIT);
1586 if (RT_UNLIKELY(pThread->enmState != PDMTHREADSTATE_RUNNING))
1587 break;
1588 STAM_COUNTER_INC(&pThis->StatTransmitByThread);
1589 while (true)
1590 {
1591 vnetR3TransmitPendingPackets(pDevIns, pThis, pThisCC, pThisCC->pTxQueue, false /*fOnWorkerThread*/); /// @todo shouldn't it be true instead?
1592 Log(("vnetR3TxThread: enable kicking and get to sleep\n"));
1593 vringSetNotification(pDevIns, &pThisCC->pTxQueue->VRing, true);
1594 if (vqueueIsEmpty(pDevIns, pThisCC->pTxQueue))
1595 break;
1596 vringSetNotification(pDevIns, &pThisCC->pTxQueue->VRing, false);
1597 }
1598 }
1599
1600 return rc;
1601}
1602
1603/**
1604 * @callback_method_impl{FNPDMTHREADWAKEUPDEV}
1605 */
1606static DECLCALLBACK(int) vnetR3TxThreadWakeUp(PPDMDEVINS pDevIns, PPDMTHREAD pThread)
1607{
1608 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
1609 RT_NOREF(pThread);
1610 return PDMDevHlpSUPSemEventSignal(pDevIns, pThis->hTxEvent);
1611}
1612
1613static int vnetR3CreateTxThreadAndEvent(PPDMDEVINS pDevIns, PVNETSTATE pThis, PVNETSTATECC pThisCC)
1614{
1615 int rc = PDMDevHlpSUPSemEventCreate(pDevIns, &pThis->hTxEvent);
1616 if (RT_SUCCESS(rc))
1617 {
1618 rc = PDMDevHlpThreadCreate(pDevIns, &pThisCC->pTxThread, NULL, vnetR3TxThread,
1619 vnetR3TxThreadWakeUp, 0, RTTHREADTYPE_IO, INSTANCE(pThis));
1620 if (RT_FAILURE(rc))
1621 PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS, N_("VNET: Failed to create worker thread %s"), INSTANCE(pThis));
1622 }
1623 else
1624 PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS, N_("VNET: Failed to create SUP event semaphore"));
1625 return rc;
1626}
1627
1628static void vnetR3DestroyTxThreadAndEvent(PPDMDEVINS pDevIns, PVNETSTATE pThis, PVNETSTATECC pThisCC)
1629{
1630 if (pThisCC->pTxThread)
1631 {
1632 /* Destroy the thread. */
1633 int rcThread;
1634 int rc = PDMDevHlpThreadDestroy(pDevIns, pThisCC->pTxThread, &rcThread);
1635 if (RT_FAILURE(rc) || RT_FAILURE(rcThread))
1636 AssertMsgFailed(("%s Failed to destroy async IO thread rc=%Rrc rcThread=%Rrc\n", __FUNCTION__, rc, rcThread));
1637 pThisCC->pTxThread = NULL;
1638 }
1639
1640 if (pThis->hTxEvent != NIL_SUPSEMEVENT)
1641 {
1642 PDMDevHlpSUPSemEventClose(pDevIns, pThis->hTxEvent);
1643 pThis->hTxEvent = NIL_SUPSEMEVENT;
1644 }
1645}
1646
1647/**
1648 * @callback_method_impl{FNVPCIQUEUECALLBACK, The TX queue}
1649 */
1650static DECLCALLBACK(void) vnetR3QueueTransmit(PPDMDEVINS pDevIns, PVQUEUE pQueue)
1651{
1652 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
1653
1654 Log(("vnetR3QueueTransmit: disable kicking and wake up TX thread\n"));
1655 vringSetNotification(pDevIns, &pQueue->VRing, false);
1656 int rc = PDMDevHlpSUPSemEventSignal(pDevIns, pThis->hTxEvent);
1657 AssertRC(rc);
1658}
1659
1660# endif /* !VNET_TX_DELAY */
1661
1662static uint8_t vnetR3ControlRx(PPDMDEVINS pDevIns, PVNETSTATE pThis, PVNETSTATECC pThisCC,
1663 PVNETCTLHDR pCtlHdr, PVQUEUEELEM pElem)
1664{
1665 uint8_t u8Ack = VNET_OK;
1666 uint8_t fOn, fDrvWasPromisc = pThis->fPromiscuous | pThis->fAllMulti;
1667 PDMDevHlpPhysRead(pDevIns, pElem->aSegsOut[1].addr, &fOn, sizeof(fOn));
1668 Log(("%s vnetR3ControlRx: uCommand=%u fOn=%u\n", INSTANCE(pThis), pCtlHdr->u8Command, fOn));
1669 switch (pCtlHdr->u8Command)
1670 {
1671 case VNET_CTRL_CMD_RX_MODE_PROMISC:
1672 pThis->fPromiscuous = !!fOn;
1673 break;
1674 case VNET_CTRL_CMD_RX_MODE_ALLMULTI:
1675 pThis->fAllMulti = !!fOn;
1676 break;
1677 default:
1678 u8Ack = VNET_ERROR;
1679 }
1680 if (fDrvWasPromisc != (pThis->fPromiscuous | pThis->fAllMulti) && pThisCC->pDrv)
1681 pThisCC->pDrv->pfnSetPromiscuousMode(pThisCC->pDrv,
1682 (pThis->fPromiscuous | pThis->fAllMulti));
1683
1684 return u8Ack;
1685}
1686
1687static uint8_t vnetR3ControlMac(PPDMDEVINS pDevIns, PVNETSTATE pThis, PVNETCTLHDR pCtlHdr, PVQUEUEELEM pElem)
1688{
1689 uint32_t nMacs = 0;
1690
1691 if ( pCtlHdr->u8Command != VNET_CTRL_CMD_MAC_TABLE_SET
1692 || pElem->cOut != 3
1693 || pElem->aSegsOut[1].cb < sizeof(nMacs)
1694 || pElem->aSegsOut[2].cb < sizeof(nMacs))
1695 {
1696 Log(("%s vnetR3ControlMac: Segment layout is wrong (u8Command=%u nOut=%u cb1=%u cb2=%u)\n",
1697 INSTANCE(pThis), pCtlHdr->u8Command, pElem->cOut, pElem->aSegsOut[1].cb, pElem->aSegsOut[2].cb));
1698 return VNET_ERROR;
1699 }
1700
1701 /* Load unicast addresses */
1702 PDMDevHlpPhysRead(pDevIns, pElem->aSegsOut[1].addr, &nMacs, sizeof(nMacs));
1703
1704 if (pElem->aSegsOut[1].cb < nMacs * sizeof(RTMAC) + sizeof(nMacs))
1705 {
1706 Log(("%s vnetR3ControlMac: The unicast mac segment is too small (nMacs=%u cb=%u)\n",
1707 INSTANCE(pThis), nMacs, pElem->aSegsOut[1].cb));
1708 return VNET_ERROR;
1709 }
1710
1711 if (nMacs > VNET_MAC_FILTER_LEN)
1712 {
1713 Log(("%s vnetR3ControlMac: MAC table is too big, have to use promiscuous mode (nMacs=%u)\n", INSTANCE(pThis), nMacs));
1714 pThis->fPromiscuous = true;
1715 }
1716 else
1717 {
1718 if (nMacs)
1719 PDMDevHlpPhysRead(pDevIns, pElem->aSegsOut[1].addr + sizeof(nMacs), pThis->aMacFilter, nMacs * sizeof(RTMAC));
1720 pThis->cMacFilterEntries = nMacs;
1721#ifdef LOG_ENABLED
1722 Log(("%s vnetR3ControlMac: unicast macs:\n", INSTANCE(pThis)));
1723 for(unsigned i = 0; i < nMacs; i++)
1724 Log((" %RTmac\n", &pThis->aMacFilter[i]));
1725#endif
1726 }
1727
1728 /* Load multicast addresses */
1729 PDMDevHlpPhysRead(pDevIns, pElem->aSegsOut[2].addr, &nMacs, sizeof(nMacs));
1730
1731 if (pElem->aSegsOut[2].cb < nMacs * sizeof(RTMAC) + sizeof(nMacs))
1732 {
1733 Log(("%s vnetR3ControlMac: The multicast mac segment is too small (nMacs=%u cb=%u)\n",
1734 INSTANCE(pThis), nMacs, pElem->aSegsOut[2].cb));
1735 return VNET_ERROR;
1736 }
1737
1738 if (nMacs > VNET_MAC_FILTER_LEN - pThis->cMacFilterEntries)
1739 {
1740 Log(("%s vnetR3ControlMac: MAC table is too big, have to use allmulti mode (nMacs=%u)\n", INSTANCE(pThis), nMacs));
1741 pThis->fAllMulti = true;
1742 }
1743 else
1744 {
1745 if (nMacs)
1746 PDMDevHlpPhysRead(pDevIns,
1747 pElem->aSegsOut[2].addr + sizeof(nMacs),
1748 &pThis->aMacFilter[pThis->cMacFilterEntries],
1749 nMacs * sizeof(RTMAC));
1750#ifdef LOG_ENABLED
1751 Log(("%s vnetR3ControlMac: multicast macs:\n", INSTANCE(pThis)));
1752 for(unsigned i = 0; i < nMacs; i++)
1753 Log((" %RTmac\n",
1754 &pThis->aMacFilter[i+pThis->cMacFilterEntries]));
1755#endif
1756 pThis->cMacFilterEntries += nMacs;
1757 }
1758
1759 return VNET_OK;
1760}
1761
1762static uint8_t vnetR3ControlVlan(PPDMDEVINS pDevIns, PVNETSTATE pThis, PVNETCTLHDR pCtlHdr, PVQUEUEELEM pElem)
1763{
1764 uint8_t u8Ack = VNET_OK;
1765 uint16_t u16Vid;
1766
1767 if (pElem->cOut != 2 || pElem->aSegsOut[1].cb != sizeof(u16Vid))
1768 {
1769 Log(("%s vnetR3ControlVlan: Segment layout is wrong (u8Command=%u nOut=%u cb=%u)\n",
1770 INSTANCE(pThis), pCtlHdr->u8Command, pElem->cOut, pElem->aSegsOut[1].cb));
1771 return VNET_ERROR;
1772 }
1773
1774 PDMDevHlpPhysRead(pDevIns, pElem->aSegsOut[1].addr, &u16Vid, sizeof(u16Vid));
1775
1776 if (u16Vid >= VNET_MAX_VID)
1777 {
1778 Log(("%s vnetR3ControlVlan: VLAN ID is out of range (VID=%u)\n", INSTANCE(pThis), u16Vid));
1779 return VNET_ERROR;
1780 }
1781
1782 Log(("%s vnetR3ControlVlan: uCommand=%u VID=%u\n", INSTANCE(pThis), pCtlHdr->u8Command, u16Vid));
1783
1784 switch (pCtlHdr->u8Command)
1785 {
1786 case VNET_CTRL_CMD_VLAN_ADD:
1787 ASMBitSet(pThis->aVlanFilter, u16Vid);
1788 break;
1789 case VNET_CTRL_CMD_VLAN_DEL:
1790 ASMBitClear(pThis->aVlanFilter, u16Vid);
1791 break;
1792 default:
1793 u8Ack = VNET_ERROR;
1794 }
1795
1796 return u8Ack;
1797}
1798
1799
1800/**
1801 * @callback_method_impl{FNVPCIQUEUECALLBACK, The CLT queue}
1802 */
1803static DECLCALLBACK(void) vnetR3QueueControl(PPDMDEVINS pDevIns, PVQUEUE pQueue)
1804{
1805 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
1806 PVNETSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVNETSTATECC);
1807
1808 VQUEUEELEM elem;
1809 while (vqueueGet(pDevIns, &pThis->VPCI, pQueue, &elem))
1810 {
1811 if (elem.cOut < 1 || elem.aSegsOut[0].cb < sizeof(VNETCTLHDR))
1812 {
1813 Log(("%s vnetR3QueueControl: The first 'out' segment is not the header! (%u < 1 || %u < %u).\n",
1814 INSTANCE(pThis), elem.cOut, elem.aSegsOut[0].cb,sizeof(VNETCTLHDR)));
1815 break; /* Skip the element and hope the next one is good. */
1816 }
1817 if ( elem.cIn < 1
1818 || elem.aSegsIn[elem.cIn - 1].cb < sizeof(VNETCTLACK))
1819 {
1820 Log(("%s vnetR3QueueControl: The last 'in' segment is too small to hold the acknowledge! (%u < 1 || %u < %u).\n",
1821 INSTANCE(pThis), elem.cIn, elem.aSegsIn[elem.cIn - 1].cb, sizeof(VNETCTLACK)));
1822 break; /* Skip the element and hope the next one is good. */
1823 }
1824
1825 uint8_t bAck;
1826 VNETCTLHDR CtlHdr;
1827 PDMDevHlpPhysRead(pDevIns, elem.aSegsOut[0].addr, &CtlHdr, sizeof(CtlHdr));
1828 switch (CtlHdr.u8Class)
1829 {
1830 case VNET_CTRL_CLS_RX_MODE:
1831 bAck = vnetR3ControlRx(pDevIns, pThis, pThisCC, &CtlHdr, &elem);
1832 break;
1833 case VNET_CTRL_CLS_MAC:
1834 bAck = vnetR3ControlMac(pDevIns, pThis, &CtlHdr, &elem);
1835 break;
1836 case VNET_CTRL_CLS_VLAN:
1837 bAck = vnetR3ControlVlan(pDevIns, pThis, &CtlHdr, &elem);
1838 break;
1839 default:
1840 bAck = VNET_ERROR;
1841 }
1842 Log(("%s Processed control message %u, ack=%u.\n", INSTANCE(pThis), CtlHdr.u8Class, bAck));
1843 PDMDevHlpPCIPhysWrite(pDevIns, elem.aSegsIn[elem.cIn - 1].addr, &bAck, sizeof(bAck));
1844
1845 vqueuePut(pDevIns, &pThis->VPCI, pQueue, &elem, sizeof(bAck));
1846 vqueueSync(pDevIns, &pThis->VPCI, pQueue);
1847 }
1848}
1849
1850/* -=-=-=-=- Debug info item -=-=-=-=- */
1851
1852/**
1853 * @callback_method_impl{FNDBGFINFOARGVDEV}
1854 */
1855static DECLCALLBACK(void) vnetR3Info(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, int cArgs, char **papszArgs)
1856{
1857 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
1858 RT_NOREF(cArgs, papszArgs);
1859 vpciR3DumpStateWorker(&pThis->VPCI, pHlp);
1860}
1861
1862
1863/* -=-=-=-=- Saved state -=-=-=-=- */
1864
1865/**
1866 * Saves the configuration.
1867 *
1868 * @param pDevIns The device instance.
1869 * @param pThis The VNET state.
1870 * @param pSSM The handle to the saved state.
1871 */
1872static void vnetR3SaveConfig(PPDMDEVINS pDevIns, PVNETSTATE pThis, PSSMHANDLE pSSM)
1873{
1874 pDevIns->pHlpR3->pfnSSMPutMem(pSSM, &pThis->macConfigured, sizeof(pThis->macConfigured));
1875}
1876
1877
1878/**
1879 * @callback_method_impl{FNSSMDEVLIVEEXEC}
1880 */
1881static DECLCALLBACK(int) vnetR3LiveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uPass)
1882{
1883 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
1884 RT_NOREF(uPass);
1885 vnetR3SaveConfig(pDevIns, pThis, pSSM);
1886 return VINF_SSM_DONT_CALL_AGAIN;
1887}
1888
1889
1890/**
1891 * @callback_method_impl{FNSSMDEVSAVEPREP}
1892 */
1893static DECLCALLBACK(int) vnetR3SavePrep(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
1894{
1895 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
1896 RT_NOREF(pSSM);
1897
1898 int rc = vnetCsRxEnter(pThis, VERR_SEM_BUSY);
1899 if (RT_SUCCESS(rc))
1900 vnetCsRxLeave(pThis);
1901 return rc;
1902}
1903
1904
1905/**
1906 * @callback_method_impl{FNSSMDEVSAVEEXEC}
1907 */
1908static DECLCALLBACK(int) vnetR3SaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
1909{
1910 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
1911 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
1912
1913 /* Save config first */
1914 vnetR3SaveConfig(pDevIns, pThis, pSSM);
1915
1916 /* Save the common part */
1917 int rc = vpciR3SaveExec(pHlp, &pThis->VPCI, pSSM);
1918 AssertRCReturn(rc, rc);
1919
1920 /* Save device-specific part */
1921 pHlp->pfnSSMPutMem( pSSM, pThis->config.mac.au8, sizeof(pThis->config.mac));
1922 pHlp->pfnSSMPutBool( pSSM, pThis->fPromiscuous);
1923 pHlp->pfnSSMPutBool( pSSM, pThis->fAllMulti);
1924 pHlp->pfnSSMPutU32( pSSM, pThis->cMacFilterEntries);
1925 pHlp->pfnSSMPutMem( pSSM, pThis->aMacFilter, pThis->cMacFilterEntries * sizeof(RTMAC));
1926 rc = pHlp->pfnSSMPutMem(pSSM, pThis->aVlanFilter, sizeof(pThis->aVlanFilter));
1927 AssertRCReturn(rc, rc);
1928
1929 Log(("%s State has been saved\n", INSTANCE(pThis)));
1930 return VINF_SUCCESS;
1931}
1932
1933
1934/**
1935 * @callback_method_impl{FNSSMDEVLOADPREP,
1936 * Serializes the receive thread - it may be working inside the critsect. }
1937 */
1938static DECLCALLBACK(int) vnetR3LoadPrep(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
1939{
1940 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
1941 RT_NOREF(pSSM);
1942
1943 int rc = vnetCsRxEnter(pThis, VERR_SEM_BUSY);
1944 if (RT_SUCCESS(rc))
1945 vnetCsRxLeave(pThis);
1946 return rc;
1947}
1948
1949
1950/**
1951 * @callback_method_impl{FNSSMDEVLOADEXEC}
1952 */
1953static DECLCALLBACK(int) vnetR3LoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
1954{
1955 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
1956 PVNETSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVNETSTATECC);
1957 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
1958
1959 /* config checks */
1960 RTMAC macConfigured;
1961 int rc = pHlp->pfnSSMGetMem(pSSM, &macConfigured, sizeof(macConfigured));
1962 AssertRCReturn(rc, rc);
1963 if (memcmp(&macConfigured, &pThis->macConfigured, sizeof(macConfigured))
1964 && (uPass == 0 || !PDMDevHlpVMTeleportedAndNotFullyResumedYet(pDevIns)))
1965 LogRel(("%s: The mac address differs: config=%RTmac saved=%RTmac\n", INSTANCE(pThis), &pThis->macConfigured, &macConfigured));
1966
1967 rc = vpciR3LoadExec(pHlp, &pThis->VPCI, pSSM, uVersion, uPass, VNET_N_QUEUES);
1968 AssertRCReturn(rc, rc);
1969
1970 if (uPass == SSM_PASS_FINAL)
1971 {
1972 rc = pHlp->pfnSSMGetMem( pSSM, pThis->config.mac.au8, sizeof(pThis->config.mac));
1973 AssertRCReturn(rc, rc);
1974
1975 if (uVersion > VIRTIO_SAVEDSTATE_VERSION_3_1_BETA1)
1976 {
1977 pHlp->pfnSSMGetBool(pSSM, &pThis->fPromiscuous);
1978 pHlp->pfnSSMGetBool(pSSM, &pThis->fAllMulti);
1979 pHlp->pfnSSMGetU32(pSSM, &pThis->cMacFilterEntries);
1980 pHlp->pfnSSMGetMem(pSSM, pThis->aMacFilter, pThis->cMacFilterEntries * sizeof(RTMAC));
1981
1982 /* Clear the rest. */
1983 if (pThis->cMacFilterEntries < VNET_MAC_FILTER_LEN)
1984 memset(&pThis->aMacFilter[pThis->cMacFilterEntries],
1985 0,
1986 (VNET_MAC_FILTER_LEN - pThis->cMacFilterEntries) * sizeof(RTMAC));
1987 rc = pHlp->pfnSSMGetMem(pSSM, pThis->aVlanFilter, sizeof(pThis->aVlanFilter));
1988 AssertRCReturn(rc, rc);
1989 }
1990 else
1991 {
1992 pThis->fPromiscuous = true;
1993 pThis->fAllMulti = false;
1994 pThis->cMacFilterEntries = 0;
1995 memset(pThis->aMacFilter, 0, VNET_MAC_FILTER_LEN * sizeof(RTMAC));
1996 memset(pThis->aVlanFilter, 0, sizeof(pThis->aVlanFilter));
1997 if (pThisCC->pDrv)
1998 pThisCC->pDrv->pfnSetPromiscuousMode(pThisCC->pDrv, true);
1999 }
2000 }
2001
2002 return rc;
2003}
2004
2005
2006/**
2007 * @callback_method_impl{FNSSMDEVLOADDONE, Link status adjustments after
2008 * loading.}
2009 */
2010static DECLCALLBACK(int) vnetR3LoadDone(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
2011{
2012 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
2013 PVNETSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVNETSTATECC);
2014 RT_NOREF(pSSM);
2015
2016 if (pThisCC->pDrv)
2017 pThisCC->pDrv->pfnSetPromiscuousMode(pThisCC->pDrv, (pThis->fPromiscuous | pThis->fAllMulti));
2018 /*
2019 * Indicate link down to the guest OS that all network connections have
2020 * been lost, unless we've been teleported here.
2021 */
2022 if (!PDMDevHlpVMTeleportedAndNotFullyResumedYet(pDevIns))
2023 vnetR3TempLinkDown(pDevIns, pThis, pThisCC);
2024
2025 return VINF_SUCCESS;
2026}
2027
2028
2029/* -=-=-=-=- PDMDEVREG -=-=-=-=- */
2030
2031/**
2032 * @interface_method_impl{PDMDEVREGR3,pfnDetach}
2033 */
2034static DECLCALLBACK(void) vnetR3Detach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
2035{
2036 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
2037 PVNETSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVNETSTATECC);
2038 Log(("%s vnetR3Detach:\n", INSTANCE(pThis)));
2039 RT_NOREF(fFlags);
2040
2041 AssertLogRelReturnVoid(iLUN == 0);
2042
2043 VNET_R3_CS_ENTER_RETURN_VOID(pDevIns, pThis);
2044
2045 vnetR3DestroyTxThreadAndEvent(pDevIns, pThis, pThisCC);
2046
2047 /*
2048 * Zero important members.
2049 */
2050 pThisCC->pDrvBase = NULL;
2051 pThisCC->pDrv = NULL;
2052
2053 vnetR3CsLeave(pDevIns, pThis);
2054}
2055
2056
2057/**
2058 * @interface_method_impl{PDMDEVREGR3,pfnAttach}
2059 */
2060static DECLCALLBACK(int) vnetR3Attach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
2061{
2062 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
2063 PVNETSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVNETSTATECC);
2064 RT_NOREF(fFlags);
2065 LogFlow(("%s vnetR3Attach:\n", INSTANCE(pThis)));
2066
2067 AssertLogRelReturn(iLUN == 0, VERR_PDM_NO_SUCH_LUN);
2068
2069 int rc = vnetR3CsEnter(pDevIns, pThis, VERR_SEM_BUSY);
2070 AssertRCReturn(rc, rc);
2071
2072 /*
2073 * Attach the driver.
2074 */
2075 rc = PDMDevHlpDriverAttach(pDevIns, 0, &pThisCC->VPCI.IBase, &pThisCC->pDrvBase, "Network Port");
2076 if (RT_SUCCESS(rc))
2077 {
2078 pThisCC->pDrv = PDMIBASE_QUERY_INTERFACE(pThisCC->pDrvBase, PDMINETWORKUP);
2079 AssertMsgStmt(pThisCC->pDrv, ("Failed to obtain the PDMINETWORKUP interface!\n"),
2080 rc = VERR_PDM_MISSING_INTERFACE_BELOW);
2081
2082 vnetR3CreateTxThreadAndEvent(pDevIns, pThis, pThisCC);
2083 }
2084 else if ( rc == VERR_PDM_NO_ATTACHED_DRIVER
2085 || rc == VERR_PDM_CFG_MISSING_DRIVER_NAME)
2086 {
2087 /* This should never happen because this function is not called
2088 * if there is no driver to attach! */
2089 Log(("%s No attached driver!\n", INSTANCE(pThis)));
2090 }
2091
2092 /*
2093 * Temporary set the link down if it was up so that the guest
2094 * will know that we have change the configuration of the
2095 * network card
2096 */
2097 if (RT_SUCCESS(rc))
2098 vnetR3TempLinkDown(pDevIns, pThis, pThisCC);
2099
2100 vnetR3CsLeave(pDevIns, pThis);
2101 return rc;
2102}
2103
2104
2105/**
2106 * @interface_method_impl{PDMDEVREGR3,pfnSuspend}
2107 */
2108static DECLCALLBACK(void) vnetR3Suspend(PPDMDEVINS pDevIns)
2109{
2110 /* Poke thread waiting for buffer space. */
2111 vnetWakeupReceive(pDevIns);
2112}
2113
2114
2115/**
2116 * @interface_method_impl{PDMDEVREGR3,pfnPowerOff}
2117 */
2118static DECLCALLBACK(void) vnetR3PowerOff(PPDMDEVINS pDevIns)
2119{
2120 /* Poke thread waiting for buffer space. */
2121 vnetWakeupReceive(pDevIns);
2122}
2123
2124
2125/**
2126 * @interface_method_impl{PDMDEVREGR3,pfnDestruct}
2127 */
2128static DECLCALLBACK(int) vnetR3Destruct(PPDMDEVINS pDevIns)
2129{
2130 PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
2131 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
2132
2133# ifdef VNET_TX_DELAY
2134 LogRel(("TxTimer stats (avg/min/max): %7d usec %7d usec %7d usec\n", pThis->u32AvgDiff, pThis->u32MinDiff, pThis->u32MaxDiff));
2135# endif
2136
2137 Log(("%s Destroying instance\n", INSTANCE(pThis)));
2138 if (pThis->hEventMoreRxDescAvail != NIL_SUPSEMEVENT)
2139 {
2140 PDMDevHlpSUPSemEventSignal(pDevIns, pThis->hEventMoreRxDescAvail);
2141 PDMDevHlpSUPSemEventClose(pDevIns, pThis->hEventMoreRxDescAvail);
2142 pThis->hEventMoreRxDescAvail = NIL_SUPSEMEVENT;
2143 }
2144
2145 // if (PDMCritSectIsInitialized(&pThis->csRx))
2146 // PDMR3CritSectDelete(&pThis->csRx);
2147
2148 return vpciR3Term(pDevIns, &pThis->VPCI);
2149}
2150
2151
2152/**
2153 * @interface_method_impl{PDMDEVREGR3,pfnConstruct}
2154 */
2155static DECLCALLBACK(int) vnetR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
2156{
2157 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
2158 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
2159 PVNETSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVNETSTATECC);
2160 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
2161 int rc;
2162
2163 /*
2164 * Initialize the instance data suffiencently for the destructor not to blow up.
2165 */
2166 RTStrPrintf(pThis->VPCI.szInstance, sizeof(pThis->VPCI.szInstance), "VNet%d", iInstance);
2167 pThisCC->pDevIns = pDevIns;
2168 pThis->hEventMoreRxDescAvail = NIL_SUPSEMEVENT;
2169# ifndef VNET_TX_DELAY
2170 pThis->hTxEvent = NIL_SUPSEMEVENT;
2171 pThisCC->pTxThread = NULL;
2172# endif
2173
2174 /* Initialize state structure */
2175 pThis->u32PktNo = 1;
2176
2177 /* Interfaces */
2178 pThisCC->INetworkDown.pfnWaitReceiveAvail = vnetR3NetworkDown_WaitReceiveAvail;
2179 pThisCC->INetworkDown.pfnReceive = vnetR3NetworkDown_Receive;
2180 pThisCC->INetworkDown.pfnReceiveGso = vnetR3NetworkDown_ReceiveGso;
2181 pThisCC->INetworkDown.pfnXmitPending = vnetR3NetworkDown_XmitPending;
2182
2183 pThisCC->INetworkConfig.pfnGetMac = vnetR3NetworkConfig_GetMac;
2184 pThisCC->INetworkConfig.pfnGetLinkState = vnetR3NetworkConfig_GetLinkState;
2185 pThisCC->INetworkConfig.pfnSetLinkState = vnetR3NetworkConfig_SetLinkState;
2186
2187
2188 /* Do our own locking. */
2189 rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
2190 AssertRCReturn(rc, rc);
2191
2192 /*
2193 * Initialize VPCI part.
2194 */
2195 pThisCC->VPCI.IBase.pfnQueryInterface = vnetQueryInterface;
2196
2197 rc = vpciR3Init(pDevIns, &pThis->VPCI, &pThisCC->VPCI, VIRTIO_NET_ID, VNET_PCI_CLASS, VNET_N_QUEUES);
2198 AssertRCReturn(rc, rc);
2199
2200 pThisCC->pRxQueue = vpciR3AddQueue(&pThis->VPCI, &pThisCC->VPCI, 256, vnetR3QueueReceive, "RX ");
2201 pThisCC->pTxQueue = vpciR3AddQueue(&pThis->VPCI, &pThisCC->VPCI, 256, vnetR3QueueTransmit, "TX ");
2202 pThisCC->pCtlQueue = vpciR3AddQueue(&pThis->VPCI, &pThisCC->VPCI, 16, vnetR3QueueControl, "CTL");
2203 AssertLogRelReturn(pThisCC->pCtlQueue && pThisCC->pTxQueue && pThisCC->pRxQueue, VERR_INTERNAL_ERROR_5);
2204
2205 Log(("%s Constructing new instance\n", INSTANCE(pThis)));
2206
2207 /*
2208 * Validate configuration.
2209 */
2210 PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns, "MAC|CableConnected|LineSpeed|LinkUpDelay|StatNo", "");
2211
2212 /* Get config params */
2213 rc = pHlp->pfnCFGMQueryBytes(pCfg, "MAC", pThis->macConfigured.au8, sizeof(pThis->macConfigured));
2214 if (RT_FAILURE(rc))
2215 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to get MAC address"));
2216
2217 rc = pHlp->pfnCFGMQueryBool(pCfg, "CableConnected", &pThis->fCableConnected);
2218 if (RT_FAILURE(rc))
2219 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to get the value of 'CableConnected'"));
2220
2221 rc = pHlp->pfnCFGMQueryU32Def(pCfg, "LinkUpDelay", &pThis->cMsLinkUpDelay, 5000); /* ms */
2222 if (RT_FAILURE(rc))
2223 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to get the value of 'LinkUpDelay'"));
2224 Assert(pThis->cMsLinkUpDelay <= 300000); /* less than 5 minutes */
2225 if (pThis->cMsLinkUpDelay > 5000 || pThis->cMsLinkUpDelay < 100)
2226 LogRel(("%s WARNING! Link up delay is set to %u seconds!\n", INSTANCE(pThis), pThis->cMsLinkUpDelay / 1000));
2227 Log(("%s Link up delay is set to %u seconds\n", INSTANCE(pThis), pThis->cMsLinkUpDelay / 1000));
2228
2229 uint32_t uStatNo = iInstance;
2230 rc = pHlp->pfnCFGMQueryU32Def(pCfg, "StatNo", &uStatNo, iInstance);
2231 if (RT_FAILURE(rc))
2232 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to get the \"StatNo\" value"));
2233
2234 vnetPrintFeatures(pThis, vnetIoCb_GetHostFeatures(&pThis->VPCI), "Device supports the following features");
2235
2236 /* Initialize PCI config space */
2237 memcpy(pThis->config.mac.au8, pThis->macConfigured.au8, sizeof(pThis->config.mac.au8));
2238 pThis->config.uStatus = 0;
2239
2240 /* Initialize critical section. */
2241 // char szTmp[sizeof(pThis->VPCI.szInstance) + 2];
2242 // RTStrPrintf(szTmp, sizeof(szTmp), "%sRX", pThis->VPCI.szInstance);
2243 // rc = PDMDevHlpCritSectInit(pDevIns, &pThis->csRx, szTmp);
2244 // if (RT_FAILURE(rc))
2245 // return rc;
2246
2247 /* Map our ports to IO space. */
2248 rc = PDMDevHlpPCIIORegionCreateIo(pDevIns, 0 /*iPciReion*/, VPCI_CONFIG + sizeof(VNetPCIConfig),
2249 vnetIOPortOut, vnetIOPortIn, NULL /*pvUser*/, "VirtioNet", NULL /*paExtDescs*/,
2250 &pThis->hIoPorts);
2251 AssertRCReturn(rc, rc);
2252
2253 /* Register save/restore state handlers. */
2254 rc = PDMDevHlpSSMRegisterEx(pDevIns, VIRTIO_SAVEDSTATE_VERSION, sizeof(VNETSTATE), NULL,
2255 NULL, vnetR3LiveExec, NULL,
2256 vnetR3SavePrep, vnetR3SaveExec, NULL,
2257 vnetR3LoadPrep, vnetR3LoadExec, vnetR3LoadDone);
2258 AssertRCReturn(rc, rc);
2259
2260 /* Create Link Up Timer */
2261 rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_VIRTUAL, vnetR3LinkUpTimer, NULL,
2262 TMTIMER_FLAGS_NO_CRIT_SECT | TMTIMER_FLAGS_NO_RING0,
2263 "VirtioNet Link Up", &pThisCC->hLinkUpTimer);
2264 AssertRCReturn(rc, rc);
2265
2266# ifdef VNET_TX_DELAY
2267 /* Create Transmit Delay Timer */
2268 rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_VIRTUAL, vnetR3TxTimer, pThis,
2269 TMTIMER_FLAGS_NO_CRIT_SECT | TMTIMER_FLAGS_NO_RING0,
2270 "VirtioNet TX Delay", &pThis->hTxTimer);
2271 AssertRCReturn(rc, rc);
2272
2273 pThis->u32i = pThis->u32AvgDiff = pThis->u32MaxDiff = 0;
2274 pThis->u32MinDiff = UINT32_MAX;
2275# endif /* VNET_TX_DELAY */
2276
2277 rc = PDMDevHlpDriverAttach(pDevIns, 0, &pThisCC->VPCI.IBase, &pThisCC->pDrvBase, "Network Port");
2278 if (RT_SUCCESS(rc))
2279 {
2280 pThisCC->pDrv = PDMIBASE_QUERY_INTERFACE(pThisCC->pDrvBase, PDMINETWORKUP);
2281 AssertMsgReturn(pThisCC->pDrv, ("Failed to obtain the PDMINETWORKUP interface!\n"),
2282 VERR_PDM_MISSING_INTERFACE_BELOW);
2283
2284 vnetR3CreateTxThreadAndEvent(pDevIns, pThis, pThisCC);
2285 }
2286 else if ( rc == VERR_PDM_NO_ATTACHED_DRIVER
2287 || rc == VERR_PDM_CFG_MISSING_DRIVER_NAME )
2288 {
2289 /* No error! */
2290 Log(("%s This adapter is not attached to any network!\n", INSTANCE(pThis)));
2291 }
2292 else
2293 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Failed to attach the network LUN"));
2294
2295 rc = PDMDevHlpSUPSemEventCreate(pDevIns, &pThis->hEventMoreRxDescAvail);
2296 AssertRCReturn(rc, rc);
2297
2298 ASMCompilerBarrier(); /* paranoia */
2299 rc = vnetIoCb_Reset(pDevIns);
2300 AssertRCReturn(rc, rc);
2301
2302 /*
2303 * Statistics and debug stuff.
2304 * The /Public/ bits are official and used by session info in the GUI.
2305 */
2306 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatReceiveBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES,
2307 "Amount of data received", "/Public/NetAdapter/%u/BytesReceived", uStatNo);
2308 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatTransmitBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES,
2309 "Amount of data transmitted", "/Public/NetAdapter/%u/BytesTransmitted", uStatNo);
2310 PDMDevHlpSTAMRegisterF(pDevIns, &pDevIns->iInstance, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_NONE,
2311 "Device instance number", "/Public/NetAdapter/%u/%s", uStatNo, pDevIns->pReg->szName);
2312
2313 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatReceiveBytes, STAMTYPE_COUNTER, "ReceiveBytes", STAMUNIT_BYTES, "Amount of data received");
2314 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTransmitBytes, STAMTYPE_COUNTER, "TransmitBytes", STAMUNIT_BYTES, "Amount of data transmitted");
2315 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatReceiveGSO, STAMTYPE_COUNTER, "Packets/ReceiveGSO", STAMUNIT_COUNT, "Number of received GSO packets");
2316 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTransmitPackets, STAMTYPE_COUNTER, "Packets/Transmit", STAMUNIT_COUNT, "Number of sent packets");
2317 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTransmitGSO, STAMTYPE_COUNTER, "Packets/Transmit-Gso", STAMUNIT_COUNT, "Number of sent GSO packets");
2318 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTransmitCSum, STAMTYPE_COUNTER, "Packets/Transmit-Csum", STAMUNIT_COUNT, "Number of completed TX checksums");
2319# ifdef VBOX_WITH_STATISTICS
2320 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatReceive, STAMTYPE_PROFILE, "Receive/Total", STAMUNIT_TICKS_PER_CALL, "Profiling receive");
2321 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatReceiveStore, STAMTYPE_PROFILE, "Receive/Store", STAMUNIT_TICKS_PER_CALL, "Profiling receive storing");
2322 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRxOverflow, STAMTYPE_PROFILE, "RxOverflow", STAMUNIT_TICKS_PER_OCCURENCE, "Profiling RX overflows");
2323 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRxOverflowWakeup, STAMTYPE_COUNTER, "RxOverflowWakeup", STAMUNIT_OCCURENCES, "Nr of RX overflow wakeups");
2324 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTransmit, STAMTYPE_PROFILE, "Transmit/Total", STAMUNIT_TICKS_PER_CALL, "Profiling transmits in HC");
2325 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTransmitSend, STAMTYPE_PROFILE, "Transmit/Send", STAMUNIT_TICKS_PER_CALL, "Profiling send transmit in HC");
2326 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTransmitByNetwork, STAMTYPE_COUNTER, "Transmit/ByNetwork", STAMUNIT_COUNT, "Network-initiated transmissions");
2327 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTransmitByThread, STAMTYPE_COUNTER, "Transmit/ByThread", STAMUNIT_COUNT, "Thread-initiated transmissions");
2328# endif
2329
2330 char szInfo[16];
2331 RTStrPrintf(szInfo, sizeof(szInfo), pDevIns->iInstance ? "vionet%u" : "vionet", pDevIns->iInstance);
2332 PDMDevHlpDBGFInfoRegisterArgv(pDevIns, szInfo, "virtio-net info", vnetR3Info);
2333
2334 return VINF_SUCCESS;
2335}
2336
2337#else /* !IN_RING3 */
2338
2339/**
2340 * @callback_method_impl{PDMDEVREGR0,pfnConstruct}
2341 */
2342static DECLCALLBACK(int) vnetRZConstruct(PPDMDEVINS pDevIns)
2343{
2344 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
2345 PVNETSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PVNETSTATE);
2346 PVNETSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVNETSTATECC);
2347
2348 int rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
2349 AssertRCReturn(rc, rc);
2350
2351 rc = PDMDevHlpIoPortSetUpContext(pDevIns, pThis->hIoPorts, vnetIOPortOut, vnetIOPortIn, NULL /*pvUser*/);
2352 AssertRCReturn(rc, rc);
2353
2354 return vpciRZInit(pDevIns, &pThis->VPCI, &pThisCC->VPCI);
2355}
2356
2357#endif /* !IN_RING3 */
2358
2359/**
2360 * The device registration structure.
2361 */
2362const PDMDEVREG g_DeviceVirtioNet =
2363{
2364 /* .u32version = */ PDM_DEVREG_VERSION,
2365 /* .uReserved0 = */ 0,
2366 /* .szName = */ "virtio-net",
2367 /* .fFlags = */ PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RZ | PDM_DEVREG_FLAGS_NEW_STYLE,
2368 /* .fClass = */ PDM_DEVREG_CLASS_NETWORK,
2369 /* .cMaxInstances = */ ~0U,
2370 /* .uSharedVersion = */ 42,
2371 /* .cbInstanceShared = */ sizeof(VNETSTATE),
2372 /* .cbInstanceCC = */ sizeof(VNETSTATECC),
2373 /* .cbInstanceRC = */ sizeof(VNETSTATERC),
2374 /* .cMaxPciDevices = */ 1,
2375 /* .cMaxMsixVectors = */ 0,
2376 /* .pszDescription = */ "Virtio Ethernet.\n",
2377#if defined(IN_RING3)
2378 /* .pszRCMod = */ "VBoxDDRC.rc",
2379 /* .pszR0Mod = */ "VBoxDDR0.r0",
2380 /* .pfnConstruct = */ vnetR3Construct,
2381 /* .pfnDestruct = */ vnetR3Destruct,
2382 /* .pfnRelocate = */ NULL,
2383 /* .pfnMemSetup = */ NULL,
2384 /* .pfnPowerOn = */ NULL,
2385 /* .pfnReset = */ NULL,
2386 /* .pfnSuspend = */ vnetR3Suspend,
2387 /* .pfnResume = */ NULL,
2388 /* .pfnAttach = */ vnetR3Attach,
2389 /* .pfnDetach = */ vnetR3Detach,
2390 /* .pfnQueryInterface = */ NULL,
2391 /* .pfnInitComplete = */ NULL,
2392 /* .pfnPowerOff = */ vnetR3PowerOff,
2393 /* .pfnSoftReset = */ NULL,
2394 /* .pfnReserved0 = */ NULL,
2395 /* .pfnReserved1 = */ NULL,
2396 /* .pfnReserved2 = */ NULL,
2397 /* .pfnReserved3 = */ NULL,
2398 /* .pfnReserved4 = */ NULL,
2399 /* .pfnReserved5 = */ NULL,
2400 /* .pfnReserved6 = */ NULL,
2401 /* .pfnReserved7 = */ NULL,
2402#elif defined(IN_RING0)
2403 /* .pfnEarlyConstruct = */ NULL,
2404 /* .pfnConstruct = */ vnetRZConstruct,
2405 /* .pfnDestruct = */ NULL,
2406 /* .pfnFinalDestruct = */ NULL,
2407 /* .pfnRequest = */ NULL,
2408 /* .pfnReserved0 = */ NULL,
2409 /* .pfnReserved1 = */ NULL,
2410 /* .pfnReserved2 = */ NULL,
2411 /* .pfnReserved3 = */ NULL,
2412 /* .pfnReserved4 = */ NULL,
2413 /* .pfnReserved5 = */ NULL,
2414 /* .pfnReserved6 = */ NULL,
2415 /* .pfnReserved7 = */ NULL,
2416#elif defined(IN_RC)
2417 /* .pfnConstruct = */ vnetRZConstruct,
2418 /* .pfnReserved0 = */ NULL,
2419 /* .pfnReserved1 = */ NULL,
2420 /* .pfnReserved2 = */ NULL,
2421 /* .pfnReserved3 = */ NULL,
2422 /* .pfnReserved4 = */ NULL,
2423 /* .pfnReserved5 = */ NULL,
2424 /* .pfnReserved6 = */ NULL,
2425 /* .pfnReserved7 = */ NULL,
2426#else
2427# error "Not in IN_RING3, IN_RING0 or IN_RC!"
2428#endif
2429 /* .u32VersionEnd = */ PDM_DEVREG_VERSION
2430};
2431
2432#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use