VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/DevVirtioNet_1_0.cpp@ 92951

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

Fix error handling bug and a few typos

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 158.5 KB
Line 
1/* $Id: DevVirtioNet_1_0.cpp 92951 2021-12-15 21:03:29Z vboxsync $ $Revision: 92951 $ $Date: 2021-12-15 21:03:29 +0000 (Wed, 15 Dec 2021) $ $Author: vboxsync $ */
2
3/** @file
4 * VBox storage devices - Virtio NET Driver
5 *
6 * Log-levels used:
7 * - Level 1: The most important (but usually rare) things to note
8 * - Level 2: NET command logging
9 * - Level 3: Vector and I/O transfer summary (shows what client sent an expects and fulfillment)
10 * - Level 6: Device <-> Guest Driver negotation, traffic, notifications and state handling
11 * - Level 12: Brief formatted hex dumps of I/O data
12 */
13
14/*
15 * Copyright (C) 2006-2020 Oracle Corporation
16 *
17 * This file is part of VirtualBox Open Source Edition (OSE), as
18 * available from http://www.virtualbox.org. This file is free software;
19 * you can redistribute it and/or modify it under the terms of the GNU
20 * General Public License (GPL) as published by the Free Software
21 * Foundation, in version 2 as it comes in the "COPYING" file of the
22 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
23 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
24 */
25
26/*******************************************************************************************************************************
27* Header Files *
28***************************************************************************************************************************** **/
29//#define LOG_GROUP LOG_GROUP_DRV_NET
30#define LOG_GROUP LOG_GROUP_DEV_VIRTIO
31#define VIRTIONET_WITH_GSO
32
33#include <iprt/types.h>
34#include <iprt/errcore.h>
35#include <iprt/assert.h>
36#include <iprt/string.h>
37
38#include <VBox/sup.h>
39#include <VBox/vmm/pdmdev.h>
40#include <VBox/vmm/stam.h>
41#include <VBox/vmm/pdmcritsect.h>
42#include <VBox/vmm/pdmnetifs.h>
43#include <VBox/msi.h>
44#include <VBox/version.h>
45#include <VBox/log.h>
46
47
48#ifdef IN_RING3
49# include <VBox/VBoxPktDmp.h>
50# include <iprt/alloc.h>
51# include <iprt/memcache.h>
52# include <iprt/semaphore.h>
53# include <iprt/sg.h>
54# include <iprt/param.h>
55# include <iprt/uuid.h>
56#endif
57#include "../VirtIO/VirtioCore.h"
58
59#include "VBoxDD.h"
60
61#define VIRTIONET_TRANSITIONAL_ENABLE_FLAG 1 /** < If set behave as VirtIO "transitional" device */
62
63/** The current saved state version for the virtio core. */
64#define VIRTIONET_SAVEDSTATE_VERSION UINT32_C(1)
65#define VIRTIONET_SAVEDSTATE_VERSION_3_1_BETA1_LEGACY UINT32_C(1) /**< Grandfathered in from DevVirtioNet.cpp */
66#define VIRTIONET_SAVEDSTATE_VERSION_LEGACY UINT32_C(2) /**< Grandfathered in from DevVirtioNet.cpp */
67#define VIRTIONET_VERSION_MARKER_MAC_ADDR { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } /** SSM handling */
68
69/*
70 * Glossary of networking acronyms used in feature names below:
71 *
72 * GSO = Generic Segmentation Offload
73 * TSO = TCP Segmentation Offload
74 * UFO = UDP Fragmentation Offload
75 * ECN = Explicit Congestion Notification
76 */
77
78/** @name VirtIO 1.0 NET Host feature bits (See VirtIO 1.0 specification, Section 5.6.3)
79 * @{ */
80#define VIRTIONET_F_CSUM RT_BIT_64(0) /**< Handle packets with partial checksum */
81#define VIRTIONET_F_GUEST_CSUM RT_BIT_64(1) /**< Handles packets with partial checksum */
82#define VIRTIONET_F_CTRL_GUEST_OFFLOADS RT_BIT_64(2) /**< Control channel offloads reconfig support */
83#define VIRTIONET_F_MAC RT_BIT_64(5) /**< Device has given MAC address */
84#define VIRTIONET_F_GUEST_TSO4 RT_BIT_64(7) /**< Driver can receive TSOv4 */
85#define VIRTIONET_F_GUEST_TSO6 RT_BIT_64(8) /**< Driver can receive TSOv6 */
86#define VIRTIONET_F_GUEST_ECN RT_BIT_64(9) /**< Driver can receive TSO with ECN */
87#define VIRTIONET_F_GUEST_UFO RT_BIT_64(10) /**< Driver can receive UFO */
88#define VIRTIONET_F_HOST_TSO4 RT_BIT_64(11) /**< Device can receive TSOv4 */
89#define VIRTIONET_F_HOST_TSO6 RT_BIT_64(12) /**< Device can receive TSOv6 */
90#define VIRTIONET_F_HOST_ECN RT_BIT_64(13) /**< Device can receive TSO with ECN */
91#define VIRTIONET_F_HOST_UFO RT_BIT_64(14) /**< Device can receive UFO */
92#define VIRTIONET_F_MRG_RXBUF RT_BIT_64(15) /**< Driver can merge receive buffers */
93#define VIRTIONET_F_STATUS RT_BIT_64(16) /**< Config status field is available */
94#define VIRTIONET_F_CTRL_VQ RT_BIT_64(17) /**< Control channel is available */
95#define VIRTIONET_F_CTRL_RX RT_BIT_64(18) /**< Control channel RX mode + MAC addr filtering */
96#define VIRTIONET_F_CTRL_VLAN RT_BIT_64(19) /**< Control channel VLAN filtering */
97#define VIRTIONET_F_CTRL_RX_EXTRA RT_BIT_64(20) /**< Control channel RX mode extra functions */
98#define VIRTIONET_F_GUEST_ANNOUNCE RT_BIT_64(21) /**< Driver can send gratuitous packets */
99#define VIRTIONET_F_MQ RT_BIT_64(22) /**< Support ultiqueue with auto receive steering */
100#define VIRTIONET_F_CTRL_MAC_ADDR RT_BIT_64(23) /**< Set MAC address through control channel */
101/** @} */
102
103static const VIRTIO_FEATURES_LIST s_aDevSpecificFeatures[] =
104{
105 { VIRTIONET_F_STATUS, " STATUS Configuration status field is available.\n" },
106 { VIRTIONET_F_MAC, " MAC Host has given MAC address.\n" },
107 { VIRTIONET_F_CTRL_VQ, " CTRL_VQ Control channel is available.\n" },
108 { VIRTIONET_F_CTRL_MAC_ADDR, " CTRL_MAC_ADDR Set MAC address through control channel.\n" },
109 { VIRTIONET_F_CTRL_RX, " CTRL_RX Control channel RX mode support.\n" },
110 { VIRTIONET_F_CTRL_VLAN, " CTRL_VLAN Control channel VLAN filtering.\n" },
111 { VIRTIONET_F_CTRL_GUEST_OFFLOADS, " CTRL_GUEST_OFFLOADS Control channel offloads reconfiguration support.\n" },
112 { VIRTIONET_F_GUEST_CSUM, " GUEST_CSUM Guest handles packets with partial checksum.\n" },
113 { VIRTIONET_F_GUEST_ANNOUNCE, " GUEST_ANNOUNCE Guest can send gratuitous packets.\n" },
114 { VIRTIONET_F_GUEST_TSO4, " GUEST_TSO4 Guest can receive TSOv4.\n" },
115 { VIRTIONET_F_GUEST_TSO6, " GUEST_TSO6 Guest can receive TSOv6.\n" },
116 { VIRTIONET_F_GUEST_ECN, " GUEST_ECN Guest can receive TSO with ECN.\n" },
117 { VIRTIONET_F_GUEST_UFO, " GUEST_UFO Guest can receive UFO.\n" },
118 { VIRTIONET_F_HOST_TSO4, " HOST_TSO4 Host can receive TSOv4.\n" },
119 { VIRTIONET_F_HOST_TSO6, " HOST_TSO6 Host can receive TSOv6.\n" },
120 { VIRTIONET_F_HOST_ECN, " HOST_ECN Host can receive TSO with ECN.\n" },
121 { VIRTIONET_F_HOST_UFO, " HOST_UFO Host can receive UFO.\n" },
122 { VIRTIONET_F_MQ, " MQ Host supports multiqueue with automatic receive steering.\n" },
123 { VIRTIONET_F_CSUM, " CSUM Host handles packets with partial checksum.\n" },
124 { VIRTIONET_F_MRG_RXBUF, " MRG_RXBUF Guest can merge receive buffers.\n" },
125};
126
127#ifdef VIRTIONET_WITH_GSO
128# define VIRTIONET_HOST_FEATURES_GSO \
129 VIRTIONET_F_CSUM \
130 | VIRTIONET_F_HOST_TSO4 \
131 | VIRTIONET_F_HOST_TSO6 \
132 | VIRTIONET_F_HOST_UFO \
133 | VIRTIONET_F_GUEST_TSO4 \
134 | VIRTIONET_F_GUEST_TSO6 \
135 | VIRTIONET_F_GUEST_UFO \
136 | VIRTIONET_F_GUEST_CSUM /* @bugref(4796) Guest must handle partial chksums */
137#else
138# define VIRTIONET_HOST_FEATURES_GSO
139#endif
140
141#define VIRTIONET_HOST_FEATURES_OFFERED \
142 VIRTIONET_F_STATUS \
143 | VIRTIONET_F_GUEST_ANNOUNCE \
144 | VIRTIONET_F_MAC \
145 | VIRTIONET_F_CTRL_VQ \
146 | VIRTIONET_F_CTRL_RX \
147 | VIRTIONET_F_CTRL_VLAN \
148 | VIRTIONET_HOST_FEATURES_GSO \
149 | VIRTIONET_F_MRG_RXBUF
150
151#define FEATURE_ENABLED(feature) RT_BOOL(!!(pThis->fNegotiatedFeatures & VIRTIONET_F_##feature))
152#define FEATURE_DISABLED(feature) (!FEATURE_ENABLED(feature))
153#define FEATURE_OFFERED(feature) VIRTIONET_HOST_FEATURES_OFFERED & VIRTIONET_F_##feature
154
155#if FEATURE_OFFERED(MQ)
156/* Instance data doesn't allow an array large enough to contain VIRTIONET_CTRL_MQ_VQ_PAIRS_MAX entries */
157# define VIRTIONET_MAX_QPAIRS 1 /* This should be increased at some point and made to work */
158#else
159# define VIRTIONET_MAX_QPAIRS VIRTIONET_CTRL_MQ_VQ_PAIRS_MIN /* default, VirtIO 1.0, 5.1.6.5.5 */
160#endif
161
162#define VIRTIONET_CTRL_MQ_VQ_PAIRS 64
163#define VIRTIONET_MAX_WORKERS VIRTIONET_MAX_QPAIRS + 1
164#define VIRTIONET_MAX_VIRTQS (VIRTIONET_MAX_QPAIRS * 2 + 1)
165#define VIRTIONET_MAX_FRAME_SIZE 65535 + 18 /**< Max IP pkt size + Eth. header w/VLAN tag */
166#define VIRTIONET_MAC_FILTER_LEN 64
167#define VIRTIONET_MAX_VLAN_ID 4096
168#define VIRTIONET_RX_SEG_COUNT 32
169
170#define VIRTQNAME(uVirtqNbr) (pThis->aVirtqs[uVirtqNbr]->szName)
171#define CBVIRTQNAME(uVirtqNbr) RTStrNLen(VIRTQNAME(uVirtqNbr), sizeof(VIRTQNAME(uVirtqNbr)))
172
173#define IS_TX_VIRTQ(n) ((n) != CTRLQIDX && ((n) & 1))
174#define IS_RX_VIRTQ(n) ((n) != CTRLQIDX && !IS_TX_VIRTQ(n))
175#define IS_CTRL_VIRTQ(n) ((n) == CTRLQIDX)
176
177/*
178 * Macros to calculate queue type-pecific index number regardless of scale. VirtIO 1.0, 5.1.2
179 */
180#define RXQIDX(qPairIdx) (qPairIdx * 2)
181#define TXQIDX(qPairIdx) (RXQIDX(qPairIdx) + 1)
182#define CTRLQIDX (FEATURE_ENABLED(MQ) ? ((VIRTIONET_MAX_QPAIRS - 1) * 2 + 2) : 2)
183
184#define IS_LINK_UP(pState) !!(pState->virtioNetConfig.uStatus & VIRTIONET_F_LINK_UP)
185#define IS_LINK_DOWN(pState) !IS_LINK_UP(pState)
186
187#define SET_LINK_UP(pState) \
188 LogFunc(("SET_LINK_UP\n")); \
189 pState->virtioNetConfig.uStatus |= VIRTIONET_F_LINK_UP; \
190 virtioCoreNotifyConfigChanged(&pThis->Virtio)
191
192#define SET_LINK_DOWN(pState) \
193 LogFunc(("SET_LINK_DOWN\n")); \
194 pState->virtioNetConfig.uStatus &= ~VIRTIONET_F_LINK_UP; \
195 virtioCoreNotifyConfigChanged(&pThis->Virtio)
196
197#define IS_VIRTQ_EMPTY(pDevIns, pVirtio, uVirtqNbr) \
198 (virtioCoreVirtqAvailBufCount(pDevIns, pVirtio, uVirtqNbr) == 0)
199
200#define PCI_DEVICE_ID_VIRTIONET_HOST 0x1000 /**< VirtIO transitional device ID for network card */
201#define PCI_CLASS_BASE_NETWORK_CONTROLLER 0x0200 /**< PCI Network device class */
202#define PCI_CLASS_SUB_NET_ETHERNET_CONTROLLER 0x00 /**< PCI NET Controller subclass */
203#define PCI_CLASS_PROG_UNSPECIFIED 0x00 /**< Programming interface. N/A. */
204#define VIRTIONET_PCI_CLASS 0x01 /**< Base class Mass Storage? */
205
206/**
207 * VirtIO Network (virtio-net) device-specific configuration subregion (VirtIO 1.0, 5.1.4)
208 * Guest MMIO is processed through callback to VirtIO core which forwards references to network configuration
209 * fields to this device-specific code through a callback.
210 */
211#pragma pack(1)
212
213 typedef struct virtio_net_config
214 {
215 RTMAC uMacAddress; /**< mac */
216
217#if FEATURE_OFFERED(STATUS)
218 uint16_t uStatus; /**< status */
219#endif
220
221#if FEATURE_OFFERED(MQ)
222 uint16_t uMaxVirtqPairs; /**< max_virtq_pairs */
223#endif
224
225 } VIRTIONET_CONFIG_T, PVIRTIONET_CONFIG_T;
226
227#pragma pack()
228
229#define VIRTIONET_F_LINK_UP 1 /**< config status: Link is up */
230#define VIRTIONET_F_ANNOUNCE 2 /**< config status: Announce */
231
232/** @name VirtIO 1.0 NET Host Device device specific control types
233 * @{ */
234#define VIRTIONET_HDR_F_NEEDS_CSUM 1 /**< flags: Packet needs checksum */
235#define VIRTIONET_HDR_GSO_NONE 0 /**< gso_type: No Global Segmentation Offset */
236#define VIRTIONET_HDR_GSO_TCPV4 1 /**< gso_type: Global Segment Offset for TCPV4 */
237#define VIRTIONET_HDR_GSO_UDP 3 /**< gso_type: Global Segment Offset for UDP */
238#define VIRTIONET_HDR_GSO_TCPV6 4 /**< gso_type: Global Segment Offset for TCPV6 */
239#define VIRTIONET_HDR_GSO_ECN 0x80 /**< gso_type: Explicit Congestion Notification */
240/** @} */
241
242/* Device operation: Net header packet (VirtIO 1.0, 5.1.6) */
243#pragma pack(1)
244struct virtio_net_pkt_hdr {
245 uint8_t uFlags; /**< flags */
246 uint8_t uGsoType; /**< gso_type */
247 uint16_t uHdrLen; /**< hdr_len */
248 uint16_t uGsoSize; /**< gso_size */
249 uint16_t uChksumStart; /**< Chksum_start */
250 uint16_t uChksumOffset; /**< Chksum_offset */
251 uint16_t uNumBuffers; /**< num_buffers */
252};
253#pragma pack()
254typedef virtio_net_pkt_hdr VIRTIONETPKTHDR, *PVIRTIONETPKTHDR;
255AssertCompileSize(VIRTIONETPKTHDR, 12);
256
257/* Control virtq: Command entry (VirtIO 1.0, 5.1.6.5) */
258#pragma pack(1)
259struct virtio_net_ctrl_hdr {
260 uint8_t uClass; /**< class */
261 uint8_t uCmd; /**< command */
262};
263#pragma pack()
264typedef virtio_net_ctrl_hdr VIRTIONET_CTRL_HDR_T, *PVIRTIONET_CTRL_HDR_T;
265
266typedef uint8_t VIRTIONET_CTRL_HDR_T_ACK;
267
268/* Command entry fAck values */
269#define VIRTIONET_OK 0 /**< Internal success status */
270#define VIRTIONET_ERROR 1 /**< Internal failure status */
271
272/** @name Control virtq: Receive filtering flags (VirtIO 1.0, 5.1.6.5.1)
273 * @{ */
274#define VIRTIONET_CTRL_RX 0 /**< Control class: Receive filtering */
275#define VIRTIONET_CTRL_RX_PROMISC 0 /**< Promiscuous mode */
276#define VIRTIONET_CTRL_RX_ALLMULTI 1 /**< All-multicast receive */
277#define VIRTIONET_CTRL_RX_ALLUNI 2 /**< All-unicast receive */
278#define VIRTIONET_CTRL_RX_NOMULTI 3 /**< No multicast receive */
279#define VIRTIONET_CTRL_RX_NOUNI 4 /**< No unicast receive */
280#define VIRTIONET_CTRL_RX_NOBCAST 5 /**< No broadcast receive */
281/** @} */
282
283typedef uint8_t VIRTIONET_MAC_ADDRESS[6];
284typedef uint32_t VIRTIONET_CTRL_MAC_TABLE_LEN;
285typedef uint8_t VIRTIONET_CTRL_MAC_ENTRIES[][6];
286
287/** @name Control virtq: MAC address filtering flags (VirtIO 1.0, 5.1.6.5.2)
288 * @{ */
289#define VIRTIONET_CTRL_MAC 1 /**< Control class: MAC address filtering */
290#define VIRTIONET_CTRL_MAC_TABLE_SET 0 /**< Set MAC table */
291#define VIRTIONET_CTRL_MAC_ADDR_SET 1 /**< Set default MAC address */
292/** @} */
293
294/** @name Control virtq: MAC address filtering flags (VirtIO 1.0, 5.1.6.5.3)
295 * @{ */
296#define VIRTIONET_CTRL_VLAN 2 /**< Control class: VLAN filtering */
297#define VIRTIONET_CTRL_VLAN_ADD 0 /**< Add VLAN to filter table */
298#define VIRTIONET_CTRL_VLAN_DEL 1 /**< Delete VLAN from filter table */
299/** @} */
300
301/** @name Control virtq: Gratuitous packet sending (VirtIO 1.0, 5.1.6.5.4)
302 * @{ */
303#define VIRTIONET_CTRL_ANNOUNCE 3 /**< Control class: Gratuitous Packet Sending */
304#define VIRTIONET_CTRL_ANNOUNCE_ACK 0 /**< Gratuitous Packet Sending ACK */
305/** @} */
306
307struct virtio_net_ctrl_mq {
308 uint16_t uVirtqueuePairs; /**< virtqueue_pairs */
309};
310
311/** @name Control virtq: Receive steering in multiqueue mode (VirtIO 1.0, 5.1.6.5.5)
312 * @{ */
313#define VIRTIONET_CTRL_MQ 4 /**< Control class: Receive steering */
314#define VIRTIONET_CTRL_MQ_VQ_PAIRS_SET 0 /**< Set number of TX/RX queues */
315#define VIRTIONET_CTRL_MQ_VQ_PAIRS_MIN 1 /**< Minimum number of TX/RX queues */
316#define VIRTIONET_CTRL_MQ_VQ_PAIRS_MAX 0x8000 /**< Maximum number of TX/RX queues */
317/** @} */
318
319uint64_t uOffloads; /**< offloads */
320
321/** @name Control virtq: Setting Offloads State (VirtIO 1.0, 5.1.6.5.6.1)
322 * @{ */
323#define VIRTIONET_CTRL_GUEST_OFFLOADS 5 /**< Control class: Offloads state configuration */
324#define VIRTIONET_CTRL_GUEST_OFFLOADS_SET 0 /**< Apply new offloads configuration */
325/** @} */
326
327typedef enum VIRTIONETPKTHDRTYPE
328{
329 kVirtioNetUninitializedPktHdrType = 0, /**< Uninitialized (default) packet header type */
330 kVirtioNetModernPktHdrWithoutMrgRx = 1, /**< Packets should not be merged (modern driver) */
331 kVirtioNetModernPktHdrWithMrgRx = 2, /**< Packets should be merged (modern driver) */
332 kVirtioNetLegacyPktHdrWithoutMrgRx = 3, /**< Packets should not be merged (legacy driver) */
333 kVirtioNetLegacyPktHdrWithMrgRx = 4, /**< Packets should be merged (legacy driver) */
334 kVirtioNetFor32BitHack = 0x7fffffff
335} VIRTIONETPKTHDRTYPE;
336
337/**
338 * device-specific queue info
339 */
340struct VIRTIONETWORKER;
341struct VIRTIONETWORKERR3;
342
343typedef struct VIRTIONETVIRTQ
344{
345 uint16_t uIdx; /**< Index of this queue */
346 uint16_t align;
347 bool fCtlVirtq; /**< If set this queue is the control queue */
348 bool fHasWorker; /**< If set this queue has an associated worker */
349 bool fAttachedToVirtioCore; /**< Set if queue attached to virtio core */
350 char szName[VIRTIO_MAX_VIRTQ_NAME_SIZE]; /**< Virtq name */
351} VIRTIONETVIRTQ, *PVIRTIONETVIRTQ;
352
353/**
354 * Worker thread context, shared state.
355 */
356typedef struct VIRTIONETWORKER
357{
358 SUPSEMEVENT hEvtProcess; /**< handle of associated sleep/wake-up semaphore */
359 uint16_t uIdx; /**< Index of this worker */
360 bool volatile fSleeping; /**< Flags whether worker thread is sleeping or not */
361 bool volatile fNotified; /**< Flags whether worker thread notified */
362 bool fAssigned; /**< Flags whether worker thread has been set up */
363 uint8_t pad;
364} VIRTIONETWORKER;
365/** Pointer to a virtio net worker. */
366typedef VIRTIONETWORKER *PVIRTIONETWORKER;
367
368/**
369 * Worker thread context, ring-3 state.
370 */
371typedef struct VIRTIONETWORKERR3
372{
373 R3PTRTYPE(PPDMTHREAD) pThread; /**< pointer to worker thread's handle */
374 uint16_t uIdx; /**< Index of this worker */
375 uint16_t pad;
376} VIRTIONETWORKERR3;
377/** Pointer to a virtio net worker. */
378typedef VIRTIONETWORKERR3 *PVIRTIONETWORKERR3;
379
380/**
381 * VirtIO Host NET device state, shared edition.
382 *
383 * @extends VIRTIOCORE
384 */
385typedef struct VIRTIONET
386{
387 /** The core virtio state. */
388 VIRTIOCORE Virtio;
389
390 /** Virtio device-specific configuration */
391 VIRTIONET_CONFIG_T virtioNetConfig;
392
393 /** Per device-bound virtq worker-thread contexts (eventq slot unused) */
394 VIRTIONETWORKER aWorkers[VIRTIONET_MAX_VIRTQS];
395
396 /** Track which VirtIO queues we've attached to */
397 VIRTIONETVIRTQ aVirtqs[VIRTIONET_MAX_VIRTQS];
398
399 /** PDM device Instance name */
400 char szInst[16];
401
402 /** VirtIO features negotiated with the guest, including generic core and device specific */
403 uint64_t fNegotiatedFeatures;
404
405 /** Number of Rx/Tx queue pairs (only one if MQ feature not negotiated */
406 uint16_t cVirtqPairs;
407
408 /** Number of Rx/Tx queue pairs that have already been initialized */
409 uint16_t cInitializedVirtqPairs;
410
411 /** Number of virtqueues total (which includes each queue of each pair plus one control queue */
412 uint16_t cVirtqs;
413
414 /** Number of worker threads (one for the control queue and one for each Tx queue) */
415 uint16_t cWorkers;
416
417 /** Alignment */
418 uint16_t alignment;
419
420 /** Indicates transmission in progress -- only one thread is allowed. */
421 uint32_t uIsTransmitting;
422
423 /** Link up delay (in milliseconds). */
424 uint32_t cMsLinkUpDelay;
425
426 /** The number of actually used slots in aMacMulticastFilter. */
427 uint32_t cMulticastFilterMacs;
428
429 /** The number of actually used slots in aMacUniicastFilter. */
430 uint32_t cUnicastFilterMacs;
431
432 /** Semaphore leaf device's thread waits on until guest driver sends empty Rx bufs */
433 SUPSEMEVENT hEventRxDescAvail;
434
435 /** Array of MAC multicast addresses accepted by RX filter. */
436 RTMAC aMacMulticastFilter[VIRTIONET_MAC_FILTER_LEN];
437
438 /** Array of MAC unicast addresses accepted by RX filter. */
439 RTMAC aMacUnicastFilter[VIRTIONET_MAC_FILTER_LEN];
440
441 /** Default MAC address which rx filtering accepts */
442 RTMAC rxFilterMacDefault;
443
444 /** MAC address obtained from the configuration. */
445 RTMAC macConfigured;
446
447 /** Bit array of VLAN filter, one bit per VLAN ID. */
448 uint8_t aVlanFilter[VIRTIONET_MAX_VLAN_ID / sizeof(uint8_t)];
449
450 /** Set if PDM leaf device at the network interface is starved for Rx buffers */
451 bool volatile fLeafWantsEmptyRxBufs;
452
453 /** Number of packet being sent/received to show in debug log. */
454 uint32_t uPktNo;
455
456 /** Flags whether VirtIO core is in ready state */
457 uint8_t fVirtioReady;
458
459 /** Resetting flag */
460 uint8_t fResetting;
461
462 /** Promiscuous mode -- RX filter accepts all packets. */
463 uint8_t fPromiscuous;
464
465 /** All multicast mode -- RX filter accepts all multicast packets. */
466 uint8_t fAllMulticast;
467
468 /** All unicast mode -- RX filter accepts all unicast packets. */
469 uint8_t fAllUnicast;
470
471 /** No multicast mode - Supresses multicast receive */
472 uint8_t fNoMulticast;
473
474 /** No unicast mode - Suppresses unicast receive */
475 uint8_t fNoUnicast;
476
477 /** No broadcast mode - Supresses broadcast receive */
478 uint8_t fNoBroadcast;
479
480 /** Type of network pkt header based on guest driver version/features */
481 VIRTIONETPKTHDRTYPE ePktHdrType;
482
483 /** Size of network pkt header based on guest driver version/features */
484 uint16_t cbPktHdr;
485
486 /** True if physical cable is attached in configuration. */
487 bool fCableConnected;
488
489 /** True if this device should offer legacy virtio support to the guest */
490 bool fOfferLegacy;
491
492 /** @name Statistic
493 * @{ */
494 STAMCOUNTER StatReceiveBytes;
495 STAMCOUNTER StatTransmitBytes;
496 STAMCOUNTER StatReceiveGSO;
497 STAMCOUNTER StatTransmitPackets;
498 STAMCOUNTER StatTransmitGSO;
499 STAMCOUNTER StatTransmitCSum;
500#ifdef VBOX_WITH_STATISTICS
501 STAMPROFILE StatReceive;
502 STAMPROFILE StatReceiveStore;
503 STAMPROFILEADV StatTransmit;
504 STAMPROFILE StatTransmitSend;
505 STAMPROFILE StatRxOverflow;
506 STAMCOUNTER StatRxOverflowWakeup;
507 STAMCOUNTER StatTransmitByNetwork;
508 STAMCOUNTER StatTransmitByThread;
509 /** @} */
510#endif
511} VIRTIONET;
512/** Pointer to the shared state of the VirtIO Host NET device. */
513typedef VIRTIONET *PVIRTIONET;
514
515/**
516 * VirtIO Host NET device state, ring-3 edition.
517 *
518 * @extends VIRTIOCORER3
519 */
520typedef struct VIRTIONETR3
521{
522 /** The core virtio ring-3 state. */
523 VIRTIOCORER3 Virtio;
524
525 /** Per device-bound virtq worker-thread contexts (eventq slot unused) */
526 VIRTIONETWORKERR3 aWorkers[VIRTIONET_MAX_VIRTQS];
527
528 /** The device instance.
529 * @note This is _only_ for use whxen dealing with interface callbacks. */
530 PPDMDEVINSR3 pDevIns;
531
532 /** Status LUN: Base interface. */
533 PDMIBASE IBase;
534
535 /** Status LUN: LED port interface. */
536 PDMILEDPORTS ILeds;
537
538 /** Status LUN: LED connector (peer). */
539 R3PTRTYPE(PPDMILEDCONNECTORS) pLedsConnector;
540
541 /** Status: LED */
542 PDMLED led;
543
544 /** Attached network driver. */
545 R3PTRTYPE(PPDMIBASE) pDrvBase;
546
547 /** Network port interface (down) */
548 PDMINETWORKDOWN INetworkDown;
549
550 /** Network config port interface (main). */
551 PDMINETWORKCONFIG INetworkConfig;
552
553 /** Connector of attached network driver. */
554 R3PTRTYPE(PPDMINETWORKUP) pDrv;
555
556 /** Link Up(/Restore) Timer. */
557 TMTIMERHANDLE hLinkUpTimer;
558
559} VIRTIONETR3;
560
561/** Pointer to the ring-3 state of the VirtIO Host NET device. */
562typedef VIRTIONETR3 *PVIRTIONETR3;
563
564/**
565 * VirtIO Host NET device state, ring-0 edition.
566 */
567typedef struct VIRTIONETR0
568{
569 /** The core virtio ring-0 state. */
570 VIRTIOCORER0 Virtio;
571} VIRTIONETR0;
572/** Pointer to the ring-0 state of the VirtIO Host NET device. */
573typedef VIRTIONETR0 *PVIRTIONETR0;
574
575/**
576 * VirtIO Host NET device state, raw-mode edition.
577 */
578typedef struct VIRTIONETRC
579{
580 /** The core virtio raw-mode state. */
581 VIRTIOCORERC Virtio;
582} VIRTIONETRC;
583/** Pointer to the ring-0 state of the VirtIO Host NET device. */
584typedef VIRTIONETRC *PVIRTIONETRC;
585
586/** @typedef VIRTIONETCC
587 * The instance data for the current context. */
588typedef CTX_SUFF(VIRTIONET) VIRTIONETCC;
589
590/** @typedef PVIRTIONETCC
591 * Pointer to the instance data for the current context. */
592typedef CTX_SUFF(PVIRTIONET) PVIRTIONETCC;
593
594#ifdef IN_RING3
595static DECLCALLBACK(int) virtioNetR3WorkerThread(PPDMDEVINS pDevIns, PPDMTHREAD pThread);
596static int virtioNetR3CreateWorkerThreads(PPDMDEVINS, PVIRTIONET, PVIRTIONETCC);
597
598/**
599 * Helper function used when logging state of a VM thread.
600 *
601 * @param Thread
602 *
603 * @return Associated name of thread as a pointer to a zero-terminated string.
604 */
605DECLINLINE(const char *) virtioNetThreadStateName(PPDMTHREAD pThread)
606{
607 if (!pThread)
608 return "<null>";
609
610 switch(pThread->enmState)
611 {
612 case PDMTHREADSTATE_INVALID:
613 return "invalid state";
614 case PDMTHREADSTATE_INITIALIZING:
615 return "initializing";
616 case PDMTHREADSTATE_SUSPENDING:
617 return "suspending";
618 case PDMTHREADSTATE_SUSPENDED:
619 return "suspended";
620 case PDMTHREADSTATE_RESUMING:
621 return "resuming";
622 case PDMTHREADSTATE_RUNNING:
623 return "running";
624 case PDMTHREADSTATE_TERMINATING:
625 return "terminating";
626 case PDMTHREADSTATE_TERMINATED:
627 return "terminated";
628 default:
629 return "unknown state";
630 }
631}
632#endif
633
634/**
635 * Wakeup PDM managed downstream (e.g. hierarchically inferior device's) RX thread
636 */
637static void virtioNetWakeupRxBufWaiter(PPDMDEVINS pDevIns)
638{
639 PVIRTIONET pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIONET);
640
641 AssertReturnVoid(pThis->hEventRxDescAvail != NIL_SUPSEMEVENT);
642
643 STAM_COUNTER_INC(&pThis->StatRxOverflowWakeup);
644 if (pThis->hEventRxDescAvail != NIL_SUPSEMEVENT)
645 {
646 Log10Func(("[%s] Waking downstream device's Rx buf waiter thread\n", pThis->szInst));
647 int rc = PDMDevHlpSUPSemEventSignal(pDevIns, pThis->hEventRxDescAvail);
648 AssertRC(rc);
649 }
650}
651
652/**
653 * Guest notifying us of its activity with a queue. Figure out which queue and respond accordingly.
654 *
655 * @callback_method_impl{VIRTIOCORER0,pfnVirtqNotified}
656 */
657static DECLCALLBACK(void) virtioNetVirtqNotified(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtqNbr)
658{
659 RT_NOREF(pVirtio);
660 PVIRTIONET pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIONET);
661
662 PVIRTIONETVIRTQ pVirtq = &pThis->aVirtqs[uVirtqNbr];
663 PVIRTIONETWORKER pWorker = &pThis->aWorkers[uVirtqNbr];
664
665#if defined (IN_RING3) && defined (LOG_ENABLED)
666 RTLogFlush(NULL);
667#endif
668 if (IS_RX_VIRTQ(uVirtqNbr))
669 {
670 uint16_t cBufsAvailable = virtioCoreVirtqAvailBufCount(pDevIns, pVirtio, uVirtqNbr);
671
672 if (cBufsAvailable)
673 {
674 Log10Func(("%s %u empty bufs added to %s by guest (notifying leaf device)\n",
675 pThis->szInst, cBufsAvailable, pVirtq->szName));
676 virtioNetWakeupRxBufWaiter(pDevIns);
677 }
678 else
679 Log10Func(("%s \n\n***WARNING: %s notified but no empty bufs added by guest! (skip leaf dev. notification)\n\n",
680 pThis->szInst, pVirtq->szName));
681 }
682 else if (IS_TX_VIRTQ(uVirtqNbr) || IS_CTRL_VIRTQ(uVirtqNbr))
683 {
684 /* Wake queue's worker thread up if sleeping (e.g. a Tx queue, or the control queue */
685 if (!ASMAtomicXchgBool(&pWorker->fNotified, true))
686 {
687 if (ASMAtomicReadBool(&pWorker->fSleeping))
688 {
689 Log10Func(("[%s] %s has available buffers - waking worker.\n", pThis->szInst, pVirtq->szName));
690
691 int rc = PDMDevHlpSUPSemEventSignal(pDevIns, pWorker->hEvtProcess);
692 AssertRC(rc);
693 }
694 else
695 Log10Func(("[%s] %s has available buffers - worker already awake\n", pThis->szInst, pVirtq->szName));
696 }
697 else
698 Log10Func(("[%s] %s has available buffers - waking worker.\n", pThis->szInst, pVirtq->szName));
699 }
700 else
701 LogRelFunc(("[%s] unrecognized queue %s (idx=%d) notified\n", pThis->szInst, pVirtq->szName, uVirtqNbr));
702}
703
704#ifdef IN_RING3 /* spans most of the file, at the moment. */
705
706/**
707 * @callback_method_impl{FNPDMTHREADWAKEUPDEV}
708 */
709static DECLCALLBACK(int) virtioNetR3WakeupWorker(PPDMDEVINS pDevIns, PPDMTHREAD pThread)
710{
711 PVIRTIONET pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIONET);
712 PVIRTIONETWORKER pWorker = (PVIRTIONETWORKER)pThread->pvUser;
713
714 Log10Func(("[%s]\n", pThis->szInst));
715 RT_NOREF(pThis);
716 return PDMDevHlpSUPSemEventSignal(pDevIns, pWorker->hEvtProcess);
717}
718
719/**
720 * Set queue names, distinguishing between modern or legacy mode.
721 *
722 * @note This makes it obvious during logging which mode this transitional device is
723 * operating in, legacy or modern.
724 *
725 * @param pThis Device specific device state
726 * @param fLegacy (input) true if running in legacy mode
727 * false if running in modern mode
728 */
729DECLINLINE(void) virtioNetR3SetVirtqNames(PVIRTIONET pThis, uint32_t fLegacy)
730{
731 RTStrCopy(pThis->aVirtqs[CTRLQIDX].szName, VIRTIO_MAX_VIRTQ_NAME_SIZE, fLegacy ? "legacy-ctrlq" : " modern-ctrlq");
732 for (uint16_t qPairIdx = 0; qPairIdx < pThis->cVirtqPairs; qPairIdx++)
733 {
734 RTStrPrintf(pThis->aVirtqs[RXQIDX(qPairIdx)].szName, VIRTIO_MAX_VIRTQ_NAME_SIZE, "%s-recvq<%d>", fLegacy ? "legacy" : "modern", qPairIdx);
735 RTStrPrintf(pThis->aVirtqs[TXQIDX(qPairIdx)].szName, VIRTIO_MAX_VIRTQ_NAME_SIZE, "%s-xmitq<%d>", fLegacy ? "legacy" : "modern", qPairIdx);
736 }
737}
738
739/**
740 * Dump a packet to debug log.
741 *
742 * @param pThis The virtio-net shared instance data.
743 * @param pbPacket The packet.
744 * @param cb The size of the packet.
745 * @param pszText A string denoting direction of packet transfer.
746 */
747DECLINLINE(void) virtioNetR3PacketDump(PVIRTIONET pThis, const uint8_t *pbPacket, size_t cb, const char *pszText)
748{
749#ifdef LOG_ENABLED
750 if (!LogIs12Enabled())
751 return;
752#endif
753 vboxEthPacketDump(pThis->szInst, pszText, pbPacket, (uint32_t)cb);
754}
755
756#ifdef LOG_ENABLED
757void virtioNetDumpGcPhysRxBuf(PPDMDEVINS pDevIns, PVIRTIONETPKTHDR pRxPktHdr,
758 uint16_t cVirtqBufs, uint8_t *pvBuf, uint16_t cb, RTGCPHYS GCPhysRxBuf, uint8_t cbRxBuf)
759{
760 PVIRTIONET pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIONET);
761 pRxPktHdr->uNumBuffers = cVirtqBufs;
762 if (pRxPktHdr)
763 {
764 LogFunc(("%*c\nrxPktHdr\n"
765 " uFlags ......... %2.2x\n uGsoType ....... %2.2x\n uHdrLen ........ %4.4x\n"
766 " uGsoSize ....... %4.4x\n uChksumStart ... %4.4x\n uChksumOffset .. %4.4x\n",
767 60, ' ', pRxPktHdr->uFlags, pRxPktHdr->uGsoType, pRxPktHdr->uHdrLen, pRxPktHdr->uGsoSize,
768 pRxPktHdr->uChksumStart, pRxPktHdr->uChksumOffset));
769 if (!virtioCoreIsLegacyMode(&pThis->Virtio) || FEATURE_ENABLED(MRG_RXBUF))
770 LogFunc((" uNumBuffers .... %4.4x\n", pRxPktHdr->uNumBuffers));
771 virtioCoreHexDump((uint8_t *)pRxPktHdr, sizeof(VIRTIONETPKTHDR), 0, "Dump of virtual rPktHdr");
772 }
773 virtioNetR3PacketDump(pThis, (const uint8_t *)pvBuf, cb, "<-- Incoming");
774 LogFunc((". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\n"));
775 virtioCoreGCPhysHexDump(pDevIns, GCPhysRxBuf, cbRxBuf, 0, "Phys Mem Dump of Rx pkt");
776 LogFunc(("%*c", 60, '-'));
777}
778
779#endif /* LOG_ENABLED */
780
781/**
782 * @callback_method_impl{FNDBGFHANDLERDEV, virtio-net debugger info callback.}
783 */
784static DECLCALLBACK(void) virtioNetR3Info(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
785{
786 PVIRTIONET pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIONET);
787 PVIRTIONETCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIONETCC);
788
789 bool fNone = pszArgs && *pszArgs == '\0';
790 bool fAll = pszArgs && (*pszArgs == 'a' || *pszArgs == 'A'); /* "all" */
791 bool fNetwork = pszArgs && (*pszArgs == 'n' || *pszArgs == 'N'); /* "network" */
792 bool fFeatures = pszArgs && (*pszArgs == 'f' || *pszArgs == 'F'); /* "features" */
793 bool fState = pszArgs && (*pszArgs == 's' || *pszArgs == 'S'); /* "state" */
794 bool fPointers = pszArgs && (*pszArgs == 'p' || *pszArgs == 'P'); /* "pointers" */
795 bool fVirtqs = pszArgs && (*pszArgs == 'q' || *pszArgs == 'Q'); /* "queues */
796
797 /* Show basic information. */
798 pHlp->pfnPrintf(pHlp,
799 "\n"
800 "---------------------------------------------------------------------------\n"
801 "Debug Info: %s\n"
802 " (options: [a]ll, [n]et, [f]eatures, [s]tate, [p]ointers, [q]ueues)\n"
803 "---------------------------------------------------------------------------\n\n",
804 pThis->szInst);
805
806 if (fNone)
807 return;
808
809 /* Show offered/unoffered, accepted/rejected features */
810 if (fAll || fFeatures)
811 {
812 virtioCorePrintDeviceFeatures(&pThis->Virtio, pHlp, s_aDevSpecificFeatures,
813 RT_ELEMENTS(s_aDevSpecificFeatures));
814 pHlp->pfnPrintf(pHlp, "\n");
815 }
816
817 /* Show queues (and associate worker info if applicable) */
818 if (fAll || fVirtqs)
819 {
820 pHlp->pfnPrintf(pHlp, "Virtq information:\n\n");
821 for (int uVirtqNbr = 0; uVirtqNbr < pThis->cVirtqs; uVirtqNbr++)
822 {
823 PVIRTIONETVIRTQ pVirtq = &pThis->aVirtqs[uVirtqNbr];
824
825 if (pVirtq->fHasWorker)
826 {
827 PVIRTIONETWORKER pWorker = &pThis->aWorkers[uVirtqNbr];
828 PVIRTIONETWORKERR3 pWorkerR3 = &pThisCC->aWorkers[uVirtqNbr];
829
830 Assert((pWorker->uIdx == pVirtq->uIdx));
831 Assert((pWorkerR3->uIdx == pVirtq->uIdx));
832
833 if (pWorker->fAssigned)
834 {
835 pHlp->pfnPrintf(pHlp, " %-15s (pThread: %p %s) ",
836 pVirtq->szName,
837 pWorkerR3->pThread,
838 virtioNetThreadStateName(pWorkerR3->pThread));
839 if (pVirtq->fAttachedToVirtioCore)
840 {
841 pHlp->pfnPrintf(pHlp, "worker: ");
842 pHlp->pfnPrintf(pHlp, "%s", pWorker->fSleeping ? "blocking" : "unblocked");
843 pHlp->pfnPrintf(pHlp, "%s", pWorker->fNotified ? ", notified" : "");
844 }
845 else
846 if (pWorker->fNotified)
847 pHlp->pfnPrintf(pHlp, "not attached to virtio core");
848 }
849 }
850 else
851 {
852 pHlp->pfnPrintf(pHlp, " %-15s (INetworkDown's thread) %s", pVirtq->szName,
853 pVirtq->fAttachedToVirtioCore ? "" : "not attached to virtio core");
854 }
855 pHlp->pfnPrintf(pHlp, "\n");
856 virtioCoreR3VirtqInfo(pDevIns, pHlp, pszArgs, uVirtqNbr);
857 pHlp->pfnPrintf(pHlp, " ---------------------------------------------------------------------\n");
858 pHlp->pfnPrintf(pHlp, "\n");
859 }
860 pHlp->pfnPrintf(pHlp, "\n");
861 }
862
863 /* Show various pointers */
864 if (fAll || fPointers)
865 {
866 pHlp->pfnPrintf(pHlp, "Internal Pointers (for instance \"%s\"):\n\n", pThis->szInst);
867 pHlp->pfnPrintf(pHlp, " pDevIns ................... %p\n", pDevIns);
868 pHlp->pfnPrintf(pHlp, " PVIRTIOCORE ............... %p\n", &pThis->Virtio);
869 pHlp->pfnPrintf(pHlp, " PVIRTIONET ................ %p\n", pThis);
870 pHlp->pfnPrintf(pHlp, " PVIRTIONETCC .............. %p\n", pThisCC);
871 pHlp->pfnPrintf(pHlp, " VIRTIONETVIRTQ[] .......... %p\n", pThis->aVirtqs);
872 pHlp->pfnPrintf(pHlp, " pDrvBase .................. %p\n", pThisCC->pDrvBase);
873 pHlp->pfnPrintf(pHlp, " pDrv ...................... %p\n", pThisCC->pDrv);
874 pHlp->pfnPrintf(pHlp, " pDrv ...................... %p\n", pThisCC->pDrv);
875 pHlp->pfnPrintf(pHlp, "\n");
876 }
877
878 /* Show device state info */
879 if (fAll || fState)
880 {
881 pHlp->pfnPrintf(pHlp, "Device state:\n\n");
882 uint32_t fTransmitting = ASMAtomicReadU32(&pThis->uIsTransmitting);
883
884 pHlp->pfnPrintf(pHlp, " Transmitting: ............. %s\n", fTransmitting ? "true" : "false");
885 pHlp->pfnPrintf(pHlp, "\n");
886 pHlp->pfnPrintf(pHlp, "Misc state\n");
887 pHlp->pfnPrintf(pHlp, "\n");
888 pHlp->pfnPrintf(pHlp, " fOfferLegacy .............. %d\n", pThis->fOfferLegacy);
889 pHlp->pfnPrintf(pHlp, " fVirtioReady .............. %d\n", pThis->fVirtioReady);
890 pHlp->pfnPrintf(pHlp, " fResetting ................ %d\n", pThis->fResetting);
891 pHlp->pfnPrintf(pHlp, " fGenUpdatePending ......... %d\n", pThis->Virtio.fGenUpdatePending);
892 pHlp->pfnPrintf(pHlp, " fMsiSupport ............... %d\n", pThis->Virtio.fMsiSupport);
893 pHlp->pfnPrintf(pHlp, " uConfigGeneration ......... %d\n", pThis->Virtio.uConfigGeneration);
894 pHlp->pfnPrintf(pHlp, " uDeviceStatus ............. 0x%x\n", pThis->Virtio.fDeviceStatus);
895 pHlp->pfnPrintf(pHlp, " cVirtqPairs .,............. %d\n", pThis->cVirtqPairs);
896 pHlp->pfnPrintf(pHlp, " cVirtqs .,................. %d\n", pThis->cVirtqs);
897 pHlp->pfnPrintf(pHlp, " cWorkers .................. %d\n", pThis->cWorkers);
898 pHlp->pfnPrintf(pHlp, " MMIO mapping name ......... %d\n", pThisCC->Virtio.szMmioName);
899 pHlp->pfnPrintf(pHlp, "\n");
900 }
901
902 /* Show network related information */
903 if (fAll || fNetwork)
904 {
905 pHlp->pfnPrintf(pHlp, "Network configuration:\n\n");
906 pHlp->pfnPrintf(pHlp, " MAC: ...................... %RTmac\n", &pThis->macConfigured);
907 pHlp->pfnPrintf(pHlp, "\n");
908 pHlp->pfnPrintf(pHlp, " Cable: .................... %s\n", pThis->fCableConnected ? "connected" : "disconnected");
909 pHlp->pfnPrintf(pHlp, " Link-up delay: ............ %d ms\n", pThis->cMsLinkUpDelay);
910 pHlp->pfnPrintf(pHlp, "\n");
911 pHlp->pfnPrintf(pHlp, " Accept all multicast: ..... %s\n", pThis->fAllMulticast ? "true" : "false");
912 pHlp->pfnPrintf(pHlp, " Suppress broadcast: ....... %s\n", pThis->fNoBroadcast ? "true" : "false");
913 pHlp->pfnPrintf(pHlp, " Suppress unicast: ......... %s\n", pThis->fNoUnicast ? "true" : "false");
914 pHlp->pfnPrintf(pHlp, " Suppress multicast: ....... %s\n", pThis->fNoMulticast ? "true" : "false");
915 pHlp->pfnPrintf(pHlp, " Promiscuous: .............. %s\n", pThis->fPromiscuous ? "true" : "false");
916 pHlp->pfnPrintf(pHlp, "\n");
917 pHlp->pfnPrintf(pHlp, " Default Rx MAC filter: .... %RTmac\n", pThis->rxFilterMacDefault);
918 pHlp->pfnPrintf(pHlp, "\n");
919
920 pHlp->pfnPrintf(pHlp, " Unicast filter MACs:\n");
921
922 if (!pThis->cUnicastFilterMacs)
923 pHlp->pfnPrintf(pHlp, " <none>\n");
924
925 for (uint32_t i = 0; i < pThis->cUnicastFilterMacs; i++)
926 pHlp->pfnPrintf(pHlp, " %RTmac\n", &pThis->aMacUnicastFilter[i]);
927
928 pHlp->pfnPrintf(pHlp, "\n Multicast filter MACs:\n");
929
930 if (!pThis->cMulticastFilterMacs)
931 pHlp->pfnPrintf(pHlp, " <none>\n");
932
933 for (uint32_t i = 0; i < pThis->cMulticastFilterMacs; i++)
934 pHlp->pfnPrintf(pHlp, " %RTmac\n", &pThis->aMacMulticastFilter[i]);
935
936 pHlp->pfnPrintf(pHlp, "\n\n");
937 pHlp->pfnPrintf(pHlp, " Leaf starved: ............. %s\n", pThis->fLeafWantsEmptyRxBufs ? "true" : "false");
938 pHlp->pfnPrintf(pHlp, "\n");
939 }
940 /** @todo implement this
941 * pHlp->pfnPrintf(pHlp, "\n");
942 * virtioCoreR3Info(pDevIns, pHlp, pszArgs);
943 */
944 pHlp->pfnPrintf(pHlp, "\n");
945}
946
947/**
948 * Checks whether certain mutually dependent negotiated features are clustered in required combinations.
949 *
950 * @note See VirtIO 1.0 spec, Section 5.1.3.1
951 *
952 * @param fFeatures Bitmask of negotiated features to evaluate
953 *
954 * @returns true if valid feature combination(s) found.
955 * false if non-valid feature set.
956 */
957DECLINLINE(bool) virtioNetValidateRequiredFeatures(uint32_t fFeatures)
958{
959 uint32_t fGuestChksumRequired = fFeatures & VIRTIONET_F_GUEST_TSO4
960 || fFeatures & VIRTIONET_F_GUEST_TSO6
961 || fFeatures & VIRTIONET_F_GUEST_UFO;
962
963 uint32_t fHostChksumRequired = fFeatures & VIRTIONET_F_HOST_TSO4
964 || fFeatures & VIRTIONET_F_HOST_TSO6
965 || fFeatures & VIRTIONET_F_HOST_UFO;
966
967 uint32_t fCtrlVqRequired = fFeatures & VIRTIONET_F_CTRL_RX
968 || fFeatures & VIRTIONET_F_CTRL_VLAN
969 || fFeatures & VIRTIONET_F_GUEST_ANNOUNCE
970 || fFeatures & VIRTIONET_F_MQ
971 || fFeatures & VIRTIONET_F_CTRL_MAC_ADDR;
972
973 if (fGuestChksumRequired && !(fFeatures & VIRTIONET_F_GUEST_CSUM))
974 return false;
975
976 if (fHostChksumRequired && !(fFeatures & VIRTIONET_F_CSUM))
977 return false;
978
979 if (fCtrlVqRequired && !(fFeatures & VIRTIONET_F_CTRL_VQ))
980 return false;
981
982 if ( fFeatures & VIRTIONET_F_GUEST_ECN
983 && !( fFeatures & VIRTIONET_F_GUEST_TSO4
984 || fFeatures & VIRTIONET_F_GUEST_TSO6))
985 return false;
986
987 if ( fFeatures & VIRTIONET_F_HOST_ECN
988 && !( fFeatures & VIRTIONET_F_HOST_TSO4
989 || fFeatures & VIRTIONET_F_HOST_TSO6))
990 return false;
991 return true;
992}
993
994/**
995 * Read or write device-specific configuration parameters.
996 * This is called by VirtIO core code a guest-initiated MMIO access is made to access device-specific
997 * configuration
998 *
999 * @note See VirtIO 1.0 spec, 2.3 Device Configuration Space
1000 *
1001 * @param pThis Pointer to device-specific state
1002 * @param uOffsetOfAccess Offset (within VIRTIONET_CONFIG_T)
1003 * @param pv Pointer to data to read or write
1004 * @param cb Number of bytes to read or write
1005 * @param fWrite True if writing, false if reading
1006 *
1007 * @returns VINF_SUCCESS if successful, or VINF_IOM_MMIO_UNUSED if fails (bad offset or size)
1008 */
1009static int virtioNetR3DevCfgAccess(PVIRTIONET pThis, uint32_t uOffsetOfAccess, void *pv, uint32_t cb, bool fWrite)
1010{
1011 AssertReturn(pv && cb <= sizeof(uint32_t), fWrite ? VINF_SUCCESS : VINF_IOM_MMIO_UNUSED_00);
1012
1013 if (VIRTIO_DEV_CONFIG_SUBMATCH_MEMBER( uMacAddress, VIRTIONET_CONFIG_T, uOffsetOfAccess))
1014 VIRTIO_DEV_CONFIG_ACCESS_READONLY( uMacAddress, VIRTIONET_CONFIG_T, uOffsetOfAccess, &pThis->virtioNetConfig);
1015#if FEATURE_OFFERED(STATUS)
1016 else
1017 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uStatus, VIRTIONET_CONFIG_T, uOffsetOfAccess))
1018 VIRTIO_DEV_CONFIG_ACCESS_READONLY( uStatus, VIRTIONET_CONFIG_T, uOffsetOfAccess, &pThis->virtioNetConfig);
1019#endif
1020#if FEATURE_OFFERED(MQ)
1021 else
1022 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uMaxVirtqPairs, VIRTIONET_CONFIG_T, uOffsetOfAccess))
1023 VIRTIO_DEV_CONFIG_ACCESS_READONLY( uMaxVirtqPairs, VIRTIONET_CONFIG_T, uOffsetOfAccess, &pThis->virtioNetConfig);
1024#endif
1025 else
1026 {
1027 LogFunc(("%s Bad access by guest to virtio_net_config: off=%u (%#x), cb=%u\n",
1028 pThis->szInst, uOffsetOfAccess, uOffsetOfAccess, cb));
1029 return fWrite ? VINF_SUCCESS : VINF_IOM_MMIO_UNUSED_00;
1030 }
1031 return VINF_SUCCESS;
1032}
1033
1034/**
1035 * @callback_method_impl{VIRTIOCORER3,pfnDevCapRead}
1036 */
1037static DECLCALLBACK(int) virtioNetR3DevCapRead(PPDMDEVINS pDevIns, uint32_t uOffset, void *pv, uint32_t cb)
1038{
1039 PVIRTIONET pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIONET);
1040
1041 RT_NOREF(pThis);
1042 return virtioNetR3DevCfgAccess(PDMDEVINS_2_DATA(pDevIns, PVIRTIONET), uOffset, pv, cb, false /*fRead*/);
1043}
1044
1045/**
1046 * @callback_method_impl{VIRTIOCORER3,pfnDevCapWrite}
1047 */
1048static DECLCALLBACK(int) virtioNetR3DevCapWrite(PPDMDEVINS pDevIns, uint32_t uOffset, const void *pv, uint32_t cb)
1049{
1050 PVIRTIONET pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIONET);
1051
1052 Log10Func(("[%s] uOffset: %d, cb: %d: %.*Rhxs\n", pThis->szInst, uOffset, cb, RT_MAX(cb, 8) , pv));
1053 RT_NOREF(pThis);
1054 return virtioNetR3DevCfgAccess(PDMDEVINS_2_DATA(pDevIns, PVIRTIONET), uOffset, (void *)pv, cb, true /*fWrite*/);
1055}
1056
1057static int virtioNetR3VirtqDestroy(PVIRTIOCORE pVirtio, PVIRTIONETVIRTQ pVirtq)
1058{
1059 PVIRTIONET pThis = RT_FROM_MEMBER(pVirtio, VIRTIONET, Virtio);
1060 PVIRTIONETCC pThisCC = PDMDEVINS_2_DATA_CC(pVirtio->pDevInsR3, PVIRTIONETCC);
1061 PVIRTIONETWORKER pWorker = &pThis->aWorkers[pVirtq->uIdx];
1062 PVIRTIONETWORKERR3 pWorkerR3 = &pThisCC->aWorkers[pVirtq->uIdx];
1063
1064 int rc, rcThread;
1065 Log10Func(("[%s] Destroying \"%s\"", pThis->szInst, pVirtq->szName));
1066 if (pVirtq->fHasWorker)
1067 {
1068 Log10((" and its worker"));
1069 rc = PDMDevHlpSUPSemEventClose(pVirtio->pDevInsR3, pWorker->hEvtProcess);
1070 AssertRCReturn(rc, rc);
1071 pWorker->hEvtProcess = 0;
1072 rc = PDMDevHlpThreadDestroy(pVirtio->pDevInsR3, pWorkerR3->pThread, &rcThread);
1073 AssertRCReturn(rc, rc);
1074 pWorkerR3->pThread = 0;
1075 pVirtq->fHasWorker = false;
1076 }
1077 pWorker->fAssigned = false;
1078 pVirtq->fCtlVirtq = false;
1079 Log10(("\n"));
1080 return rc;
1081}
1082
1083
1084/*********************************************************************************************************************************
1085* Saved state *
1086*********************************************************************************************************************************/
1087
1088/**
1089 * @callback_method_impl{FNSSMDEVLOADEXEC}
1090 *
1091 * @note: This is included to accept and migrate VMs that had used the original VirtualBox legacy-only virtio-net (network card)
1092 * controller device emulator ("DevVirtioNet.cpp") to work with this superset of VirtIO compatibility known
1093 * as a transitional device (see PDM-invoked device constructor comments for more information)
1094 */
1095static DECLCALLBACK(int) virtioNetR3LegacyDeviceLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass,
1096 RTMAC uMacLoaded)
1097{
1098 PVIRTIONET pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIONET);
1099 PVIRTIONETCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIONETCC);
1100 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
1101 int rc;
1102
1103 Log7Func(("[%s] LOAD EXEC (LEGACY)!!\n", pThis->szInst));
1104
1105 if (memcmp(&uMacLoaded.au8, &pThis->macConfigured.au8, sizeof(uMacLoaded))
1106 && (uPass == 0 || !PDMDevHlpVMTeleportedAndNotFullyResumedYet(pDevIns)))
1107 LogRelFunc(("[%s]: The mac address differs: config=%RTmac saved=%RTmac\n",
1108 pThis->szInst, &pThis->macConfigured, &uMacLoaded));
1109
1110 if (uPass == SSM_PASS_FINAL)
1111 {
1112 /* Call the virtio core to have it load legacy device state */
1113 rc = virtioCoreR3LegacyDeviceLoadExec(&pThis->Virtio, pDevIns->pHlpR3, pSSM, uVersion, VIRTIONET_SAVEDSTATE_VERSION_3_1_BETA1_LEGACY);
1114 AssertRCReturn(rc, rc);
1115 /*
1116 * Scan constructor-determined virtqs to determine if they are all valid-as-restored.
1117 * If so, nudge them with a signal, otherwise destroy the unusable queue(s)
1118 * to avoid tripping up the other queue processing logic.
1119 */
1120 int cVirtqsToRemove = 0;
1121 for (int uVirtqNbr = 0; uVirtqNbr < pThis->cVirtqs; uVirtqNbr++)
1122 {
1123 PVIRTIONETVIRTQ pVirtq = &pThis->aVirtqs[uVirtqNbr];
1124 if (pVirtq->fHasWorker)
1125 {
1126 if (!virtioCoreR3VirtqIsEnabled(&pThis->Virtio, uVirtqNbr))
1127 {
1128 virtioNetR3VirtqDestroy(&pThis->Virtio, pVirtq);
1129 ++cVirtqsToRemove;
1130 }
1131 else
1132 {
1133 if (virtioCoreR3VirtqIsAttached(&pThis->Virtio, uVirtqNbr))
1134 {
1135 Log7Func(("[%s] Waking %s worker.\n", pThis->szInst, pVirtq->szName));
1136 rc = PDMDevHlpSUPSemEventSignal(pDevIns, pThis->aWorkers[pVirtq->uIdx].hEvtProcess);
1137 AssertRCReturn(rc, rc);
1138 }
1139 }
1140 }
1141 }
1142 AssertMsg(cVirtqsToRemove < 2, ("Multiple unusable queues in saved state unexpected\n"));
1143 pThis->cVirtqs -= cVirtqsToRemove;
1144
1145 pThis->virtioNetConfig.uStatus = pThis->Virtio.fDeviceStatus;
1146 pThis->fVirtioReady = pThis->Virtio.fDeviceStatus & VIRTIO_STATUS_DRIVER_OK;
1147
1148 rc = pHlp->pfnSSMGetMem(pSSM, pThis->virtioNetConfig.uMacAddress.au8, sizeof(pThis->virtioNetConfig.uMacAddress));
1149 AssertRCReturn(rc, rc);
1150
1151 if (uVersion > VIRTIONET_SAVEDSTATE_VERSION_3_1_BETA1_LEGACY)
1152 {
1153 rc = pHlp->pfnSSMGetU8( pSSM, &pThis->fPromiscuous);
1154 AssertRCReturn(rc, rc);
1155 rc = pHlp->pfnSSMGetU8( pSSM, &pThis->fAllMulticast);
1156 AssertRCReturn(rc, rc);
1157 /*
1158 * The 0.95 legacy virtio spec defines a control queue command VIRTIO_NET_CTRL_MAC_TABLE_SET,
1159 * wherein guest driver configures two variable length mac filter tables: A unicast filter,
1160 * and a multicast filter. However original VBox virtio-net saved both sets of filter entries
1161 * in a single table, abandoning the distinction between unicast and multicast filters. It preserved
1162 * only *one* filter's table length, leaving no way to separate table back out into respective unicast
1163 * and multicast tables this device implementation preserves. Deduced from legacy code, the original
1164 * assumption was that the both MAC filters are whitelists that can be processed identically
1165 * (from the standpoint of a *single* host receiver), such that the distinction between unicast and
1166 * multicast doesn't matter in any one VM's context. Little choice here but to save the undifferentiated
1167 * unicast & multicast MACs to the unicast filter table and leave multicast table empty/unused.
1168 */
1169 uint32_t cCombinedUnicastMulticastEntries;
1170 rc = pHlp->pfnSSMGetU32(pSSM, &cCombinedUnicastMulticastEntries);
1171 AssertRCReturn(rc, rc);
1172 AssertReturn(cCombinedUnicastMulticastEntries <= VIRTIONET_MAC_FILTER_LEN, VERR_OUT_OF_RANGE);
1173 pThis->cUnicastFilterMacs = cCombinedUnicastMulticastEntries;
1174 rc = pHlp->pfnSSMGetMem(pSSM, pThis->aMacUnicastFilter, cCombinedUnicastMulticastEntries * sizeof(RTMAC));
1175 AssertRCReturn(rc, rc);
1176 /* Zero-out the remainder of the Unicast/Multicast filter table */
1177 memset(&pThis->aMacUnicastFilter[pThis->cUnicastFilterMacs], 0, VIRTIONET_MAC_FILTER_LEN * sizeof(RTMAC));
1178 rc = pHlp->pfnSSMGetMem(pSSM, pThis->aVlanFilter, sizeof(pThis->aVlanFilter));
1179 AssertRCReturn(rc, rc);
1180 }
1181 else
1182 {
1183 pThis->fAllMulticast = false;
1184 pThis->cUnicastFilterMacs = 0;
1185 memset(&pThis->aMacUnicastFilter, 0, VIRTIONET_MAC_FILTER_LEN * sizeof(RTMAC));
1186
1187 memset(pThis->aVlanFilter, 0, sizeof(pThis->aVlanFilter));
1188
1189 pThis->fPromiscuous = true;
1190 if (pThisCC->pDrv)
1191 pThisCC->pDrv->pfnSetPromiscuousMode(pThisCC->pDrv, true);
1192 }
1193
1194 /*
1195 * Log the restored VirtIO feature selection.
1196 */
1197 pThis->fNegotiatedFeatures = virtioCoreGetNegotiatedFeatures(&pThis->Virtio);
1198 virtioCorePrintDeviceFeatures(&pThis->Virtio, NULL, s_aDevSpecificFeatures, RT_ELEMENTS(s_aDevSpecificFeatures));
1199
1200 /*
1201 * Configure remaining transitional device parameters presumably or deductively
1202 * as these weren't part of the legacy device code thus it didn't save them to SSM
1203 */
1204 pThis->fCableConnected = 1;
1205 pThis->fAllUnicast = 0;
1206 pThis->fNoMulticast = 0;
1207 pThis->fNoUnicast = 0;
1208 pThis->fNoBroadcast = 0;
1209
1210 /* Zero out the multicast table and count, all MAC filters, if any, are in the unicast filter table */
1211 pThis->cMulticastFilterMacs = 0;
1212 memset(&pThis->aMacMulticastFilter, 0, VIRTIONET_MAC_FILTER_LEN * sizeof(RTMAC));
1213
1214 }
1215 return VINF_SUCCESS;
1216}
1217
1218/**
1219 * @callback_method_impl{FNSSMDEVLOADEXEC}
1220 *
1221 * @note: This loads state saved by a Modern (VirtIO 1.0+) device, of which this transitional device is one,
1222 * and thus supports both legacy and modern guest virtio drivers.
1223 */
1224static DECLCALLBACK(int) virtioNetR3ModernDeviceLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
1225{
1226 PVIRTIONET pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIONET);
1227 PVIRTIONETCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIONETCC);
1228 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
1229 int rc;
1230
1231 RT_NOREF(pThisCC);
1232
1233 RTMAC uMacLoaded, uVersionMarkerMac = VIRTIONET_VERSION_MARKER_MAC_ADDR;
1234 rc = pHlp->pfnSSMGetMem(pSSM, &uMacLoaded.au8, sizeof(uMacLoaded.au8));
1235 AssertRCReturn(rc, rc);
1236 if (memcmp(&uMacLoaded.au8, uVersionMarkerMac.au8, sizeof(uVersionMarkerMac.au8)))
1237 {
1238 rc = virtioNetR3LegacyDeviceLoadExec(pDevIns, pSSM, uVersion, uPass, uMacLoaded);
1239 return rc;
1240 }
1241
1242 Log7Func(("[%s] LOAD EXEC!!\n", pThis->szInst));
1243
1244 AssertReturn(uPass == SSM_PASS_FINAL, VERR_SSM_UNEXPECTED_PASS);
1245 AssertLogRelMsgReturn(uVersion == VIRTIONET_SAVEDSTATE_VERSION,
1246 ("uVersion=%u\n", uVersion), VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION);
1247
1248 virtioNetR3SetVirtqNames(pThis, false /* fLegacy */);
1249
1250 pHlp->pfnSSMGetU64( pSSM, &pThis->fNegotiatedFeatures);
1251
1252 pHlp->pfnSSMGetU16( pSSM, &pThis->cVirtqs);
1253 AssertReturn(pThis->cVirtqs <= (VIRTIONET_MAX_QPAIRS * 2) + 1, VERR_OUT_OF_RANGE);
1254 pHlp->pfnSSMGetU16( pSSM, &pThis->cWorkers);
1255 AssertReturn(pThis->cWorkers <= VIRTIONET_MAX_WORKERS , VERR_OUT_OF_RANGE);
1256
1257 for (int uVirtqNbr = 0; uVirtqNbr < pThis->cVirtqs; uVirtqNbr++)
1258 pHlp->pfnSSMGetBool(pSSM, &pThis->aVirtqs[uVirtqNbr].fAttachedToVirtioCore);
1259
1260 /* Config checks */
1261 RTMAC macConfigured;
1262 rc = pHlp->pfnSSMGetMem(pSSM, &macConfigured.au8, sizeof(macConfigured.au8));
1263 AssertRCReturn(rc, rc);
1264 if (memcmp(&macConfigured.au8, &pThis->macConfigured.au8, sizeof(macConfigured.au8))
1265 && (uPass == 0 || !PDMDevHlpVMTeleportedAndNotFullyResumedYet(pDevIns)))
1266 LogRel(("%s: The mac address differs: config=%RTmac saved=%RTmac\n",
1267 pThis->szInst, &pThis->macConfigured, &macConfigured));
1268 memcpy(pThis->virtioNetConfig.uMacAddress.au8, macConfigured.au8, sizeof(macConfigured.au8));
1269
1270#if FEATURE_OFFERED(STATUS)
1271 uint16_t fChkStatus;
1272 pHlp->pfnSSMGetU16( pSSM, &fChkStatus);
1273 if (fChkStatus == 0xffff)
1274 {
1275 /* Dummy value in saved state because status feature wasn't enabled at the time */
1276 pThis->virtioNetConfig.uStatus = 0; /* VIRTIO_NET_S_ANNOUNCE disabled */
1277 pThis->virtioNetConfig.uStatus = !!IS_LINK_UP(pThis); /* VIRTIO_NET_IS_LINK_UP (bit 0) */
1278 }
1279 else
1280 pThis->virtioNetConfig.uStatus = fChkStatus;
1281#else
1282 uint16_t fDiscard;
1283 pHlp->pfnSSMGetU16( pSSM, &fDiscard);
1284#endif
1285
1286#if FEATURE_OFFERED(MQ)
1287 uint16_t uCheckMaxVirtqPairs;
1288 pHlp->pfnSSMGetU16( pSSM, &uCheckMaxVirtqPairs);
1289 if (uCheckMaxVirtqPairs)
1290 pThis->virtioNetConfig.uMaxVirtqPairs = uCheckMaxVirtqPairs;
1291 else
1292 pThis->virtioNetConfig.uMaxVirtqPairs = VIRTIONET_CTRL_MQ_VQ_PAIRS;
1293#else
1294 uint16_t fDiscard;
1295 pHlp->pfnSSMGetU16( pSSM, &fDiscard);
1296#endif
1297
1298 /* Save device-specific part */
1299 pHlp->pfnSSMGetBool( pSSM, &pThis->fCableConnected);
1300 pHlp->pfnSSMGetU8( pSSM, &pThis->fPromiscuous);
1301 pHlp->pfnSSMGetU8( pSSM, &pThis->fAllMulticast);
1302 pHlp->pfnSSMGetU8( pSSM, &pThis->fAllUnicast);
1303 pHlp->pfnSSMGetU8( pSSM, &pThis->fNoMulticast);
1304 pHlp->pfnSSMGetU8( pSSM, &pThis->fNoUnicast);
1305 pHlp->pfnSSMGetU8( pSSM, &pThis->fNoBroadcast);
1306
1307 pHlp->pfnSSMGetU32( pSSM, &pThis->cMulticastFilterMacs);
1308 AssertReturn(pThis->cMulticastFilterMacs <= VIRTIONET_MAC_FILTER_LEN, VERR_OUT_OF_RANGE);
1309 pHlp->pfnSSMGetMem( pSSM, pThis->aMacMulticastFilter, pThis->cMulticastFilterMacs * sizeof(RTMAC));
1310
1311 if (pThis->cMulticastFilterMacs < VIRTIONET_MAC_FILTER_LEN)
1312 memset(&pThis->aMacMulticastFilter[pThis->cMulticastFilterMacs], 0,
1313 (VIRTIONET_MAC_FILTER_LEN - pThis->cMulticastFilterMacs) * sizeof(RTMAC));
1314
1315 pHlp->pfnSSMGetU32( pSSM, &pThis->cUnicastFilterMacs);
1316 AssertReturn(pThis->cUnicastFilterMacs <= VIRTIONET_MAC_FILTER_LEN, VERR_OUT_OF_RANGE);
1317 pHlp->pfnSSMGetMem( pSSM, pThis->aMacUnicastFilter, pThis->cUnicastFilterMacs * sizeof(RTMAC));
1318
1319 if (pThis->cUnicastFilterMacs < VIRTIONET_MAC_FILTER_LEN)
1320 memset(&pThis->aMacUnicastFilter[pThis->cUnicastFilterMacs], 0,
1321 (VIRTIONET_MAC_FILTER_LEN - pThis->cUnicastFilterMacs) * sizeof(RTMAC));
1322
1323 rc = pHlp->pfnSSMGetMem(pSSM, pThis->aVlanFilter, sizeof(pThis->aVlanFilter));
1324 AssertRCReturn(rc, rc);
1325 /*
1326 * Call the virtio core to let it load its state.
1327 */
1328 rc = virtioCoreR3ModernDeviceLoadExec(&pThis->Virtio, pDevIns->pHlpR3, pSSM, uVersion,
1329 VIRTIONET_SAVEDSTATE_VERSION, pThis->cVirtqs);
1330 AssertRCReturn(rc, rc);
1331 /*
1332 * Since the control queue is created proactively in the constructor to accomodate worst-case
1333 * legacy guests, even though the queue may have been deducted from queue count while saving state,
1334 * we must explicitly remove queue and associated worker thread and context at this point,
1335 * or presence of bogus control queue will confuse operations.
1336 */
1337 PVIRTIONETVIRTQ pVirtq = &pThis->aVirtqs[CTRLQIDX];
1338 if (FEATURE_DISABLED(CTRL_VQ) || !virtioCoreIsVirtqEnabled(&pThis->Virtio, CTRLQIDX))
1339 {
1340 virtioCoreR3VirtqDetach(&pThis->Virtio, CTRLQIDX);
1341 virtioNetR3VirtqDestroy(&pThis->Virtio, pVirtq);
1342 pVirtq->fAttachedToVirtioCore = false;
1343 --pThis->cWorkers;
1344 }
1345 /*
1346 * Nudge queue workers
1347 */
1348 for (int uVirtqNbr = 0; uVirtqNbr < pThis->cVirtqs; uVirtqNbr++)
1349 {
1350 pVirtq = &pThis->aVirtqs[uVirtqNbr];
1351 if (pVirtq->fAttachedToVirtioCore)
1352 {
1353 if (pVirtq->fHasWorker)
1354 {
1355 PVIRTIONETWORKER pWorker = &pThis->aWorkers[uVirtqNbr];
1356 Log7Func(("[%s] Waking %s worker.\n", pThis->szInst, pVirtq->szName));
1357 rc = PDMDevHlpSUPSemEventSignal(pDevIns, pWorker->hEvtProcess);
1358 AssertRCReturn(rc, rc);
1359 }
1360 }
1361 }
1362 pThis->virtioNetConfig.uStatus = pThis->Virtio.fDeviceStatus; /* reflects state to guest driver */
1363 pThis->fVirtioReady = pThis->Virtio.fDeviceStatus & VIRTIO_STATUS_DRIVER_OK;
1364
1365 return rc;
1366}
1367
1368/**
1369 * @callback_method_impl{FNSSMDEVSAVEEXEC}
1370 */
1371static DECLCALLBACK(int) virtioNetR3ModernSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
1372{
1373 PVIRTIONET pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIONET);
1374 PVIRTIONETCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIONETCC);
1375 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
1376
1377 RT_NOREF(pThisCC);
1378 Log7Func(("[%s] SAVE EXEC!!\n", pThis->szInst));
1379
1380 /* Store a dummy MAC address that would never be actually assigned to a NIC
1381 * so that when load exec handler is called it can be easily determined
1382 * whether saved state is modern or legacy. This works because original
1383 * legacy code stored assigned NIC address as the first item of SSM state
1384 */
1385 RTMAC uVersionMarkerMac = VIRTIONET_VERSION_MARKER_MAC_ADDR;
1386 pHlp->pfnSSMPutMem(pSSM, &uVersionMarkerMac.au8, sizeof(uVersionMarkerMac.au8));
1387
1388 pHlp->pfnSSMPutU64( pSSM, pThis->fNegotiatedFeatures);
1389
1390 pHlp->pfnSSMPutU16( pSSM, pThis->cVirtqs);
1391 pHlp->pfnSSMPutU16( pSSM, pThis->cWorkers);
1392
1393 for (int uVirtqNbr = 0; uVirtqNbr < pThis->cVirtqs; uVirtqNbr++)
1394 pHlp->pfnSSMPutBool(pSSM, pThis->aVirtqs[uVirtqNbr].fAttachedToVirtioCore);
1395 /*
1396
1397 * Save device config area (accessed via MMIO)
1398 */
1399 pHlp->pfnSSMPutMem( pSSM, pThis->virtioNetConfig.uMacAddress.au8,
1400 sizeof(pThis->virtioNetConfig.uMacAddress.au8));
1401#if FEATURE_OFFERED(STATUS)
1402 pHlp->pfnSSMPutU16( pSSM, pThis->virtioNetConfig.uStatus);
1403#else
1404 /*
1405 * Relevant values are lower bits. Forcing this to 0xffff let's loadExec know this
1406 * feature was not enabled in saved state. VirtIO 1.0, 5.1.4
1407 */
1408 pHlp->pfnSSMPutU16( pSSM, 0xffff);
1409
1410#endif
1411#if FEATURE_OFFERED(MQ)
1412 pHlp->pfnSSMPutU16( pSSM, pThis->virtioNetConfig.uMaxVirtqPairs);
1413#else
1414 /*
1415 * Legal values for max_virtqueue_pairs are 0x1 -> 0x8000 *. Forcing zero let's loadExec know this
1416 * feature was not enabled in saved state. VirtIO 1.0, 5.1.4.1
1417 */
1418 pHlp->pfnSSMPutU16( pSSM, 0);
1419#endif
1420
1421 /* Save device-specific part */
1422 pHlp->pfnSSMPutBool( pSSM, pThis->fCableConnected);
1423 pHlp->pfnSSMPutU8( pSSM, pThis->fPromiscuous);
1424 pHlp->pfnSSMPutU8( pSSM, pThis->fAllMulticast);
1425 pHlp->pfnSSMPutU8( pSSM, pThis->fAllUnicast);
1426 pHlp->pfnSSMPutU8( pSSM, pThis->fNoMulticast);
1427 pHlp->pfnSSMPutU8( pSSM, pThis->fNoUnicast);
1428 pHlp->pfnSSMPutU8( pSSM, pThis->fNoBroadcast);
1429
1430 pHlp->pfnSSMPutU32( pSSM, pThis->cMulticastFilterMacs);
1431 pHlp->pfnSSMPutMem( pSSM, pThis->aMacMulticastFilter, pThis->cMulticastFilterMacs * sizeof(RTMAC));
1432
1433 pHlp->pfnSSMPutU32( pSSM, pThis->cUnicastFilterMacs);
1434 pHlp->pfnSSMPutMem( pSSM, pThis->aMacUnicastFilter, pThis->cUnicastFilterMacs * sizeof(RTMAC));
1435
1436 int rc = pHlp->pfnSSMPutMem(pSSM, pThis->aVlanFilter, sizeof(pThis->aVlanFilter));
1437 AssertRCReturn(rc, rc);
1438
1439 /*
1440 * Call the virtio core to let it save its state.
1441 */
1442 return virtioCoreR3SaveExec(&pThis->Virtio, pDevIns->pHlpR3, pSSM, VIRTIONET_SAVEDSTATE_VERSION, pThis->cVirtqs);
1443}
1444
1445
1446/*********************************************************************************************************************************
1447* Device interface. *
1448*********************************************************************************************************************************/
1449
1450#ifdef IN_RING3
1451
1452/**
1453 * Perform 16-bit 1's compliment checksum on provided packet in accordance with VirtIO specification,
1454 * pertinent to VIRTIO_NET_F_CSUM feature, which 'offloads' the Checksum feature from the driver
1455 * to save processor cycles, which is ironic in our case, where the controller device ('network card')
1456 * is emulated on the virtualization host.
1457 *
1458 * @note See VirtIO 1.0 spec, 5.1.6.2 Packet Transmission
1459 *
1460 * @param pBuf Pointer to r/w buffer with any portion to calculate checksum for
1461 * @param cbSize Number of bytes to checksum
1462 * @param uStart Where to start the checksum within the buffer
1463 * @param uOffset Offset past uStart point in the buffer to store checksum result
1464 *
1465 */
1466DECLINLINE(void) virtioNetR3Calc16BitChecksum(uint8_t *pBuf, size_t cb, uint16_t uStart, uint16_t uOffset)
1467{
1468 AssertReturnVoid(uStart < cb);
1469 AssertReturnVoid(uStart + uOffset + sizeof(uint16_t) <= cb);
1470
1471 uint32_t chksum = 0;
1472 uint16_t *pu = (uint16_t *)(pBuf + uStart);
1473
1474 cb -= uStart;
1475 while (cb > 1)
1476 {
1477 chksum += *pu++;
1478 cb -= 2;
1479 }
1480 if (cb)
1481 chksum += *(uint8_t *)pu;
1482 while (chksum >> 16)
1483 chksum = (chksum >> 16) + (chksum & 0xFFFF);
1484
1485 /* Store 1's compliment of calculated sum */
1486 *(uint16_t *)(pBuf + uStart + uOffset) = ~chksum;
1487}
1488
1489/**
1490 * Turns on/off the read status LED.
1491 *
1492 * @returns VBox status code.
1493 * @param pThis Pointer to the device state structure.
1494 * @param fOn New LED state.
1495 */
1496void virtioNetR3SetReadLed(PVIRTIONETR3 pThisR3, bool fOn)
1497{
1498 if (fOn)
1499 pThisR3->led.Asserted.s.fReading = pThisR3->led.Actual.s.fReading = 1;
1500 else
1501 pThisR3->led.Actual.s.fReading = fOn;
1502}
1503
1504/**
1505 * Turns on/off the write status LED.
1506 *
1507 * @returns VBox status code.
1508 * @param pThis Pointer to the device state structure.
1509 * @param fOn New LED state.
1510 */
1511void virtioNetR3SetWriteLed(PVIRTIONETR3 pThisR3, bool fOn)
1512{
1513 if (fOn)
1514 pThisR3->led.Asserted.s.fWriting = pThisR3->led.Actual.s.fWriting = 1;
1515 else
1516 pThisR3->led.Actual.s.fWriting = fOn;
1517}
1518
1519/**
1520 * Check that the core is setup and ready and co-configured with guest virtio driver,
1521 * and verifies that the VM is running.
1522 *
1523 * @returns true if VirtIO core and device are in a running and operational state
1524 */
1525DECLINLINE(bool) virtioNetIsOperational(PVIRTIONET pThis, PPDMDEVINS pDevIns)
1526{
1527 if (RT_LIKELY(pThis->fVirtioReady))
1528 {
1529 VMSTATE enmVMState = PDMDevHlpVMState(pDevIns);
1530 if (RT_LIKELY(enmVMState == VMSTATE_RUNNING || enmVMState == VMSTATE_RUNNING_LS))
1531 return true;
1532 }
1533 return false;
1534}
1535
1536/**
1537 * Check whether specific queue is ready and has Rx buffers (virtqueue descriptors)
1538 * available. This must be called before the pfnRecieve() method is called.
1539 *
1540 * @remarks As a side effect this function enables queue notification
1541 * if it cannot receive because the queue is empty.
1542 * It disables notification if it can receive.
1543 *
1544 * @returns VERR_NET_NO_BUFFER_SPACE if it cannot.
1545 * @thread RX
1546 */
1547static int virtioNetR3CheckRxBufsAvail(PPDMDEVINS pDevIns, PVIRTIONET pThis, PVIRTIONETVIRTQ pRxVirtq)
1548{
1549 int rc = VERR_INVALID_STATE;
1550 Log8Func(("[%s] ", pThis->szInst));
1551 if (!virtioNetIsOperational(pThis, pDevIns))
1552 Log8(("No Rx bufs available. (VirtIO core not ready)\n", pThis->szInst));
1553
1554 else if (!virtioCoreIsVirtqEnabled(&pThis->Virtio, pRxVirtq->uIdx))
1555 Log8(("[No Rx bufs available. (%s not enabled)\n", pRxVirtq->szName));
1556
1557 else if (IS_VIRTQ_EMPTY(pDevIns, &pThis->Virtio, pRxVirtq->uIdx))
1558 Log8(("No Rx bufs available. (%s empty)\n", pRxVirtq->szName));
1559
1560 else
1561 {
1562 Log8(("%s has %d empty guest bufs in avail ring\n", pRxVirtq->szName,
1563 virtioCoreVirtqAvailBufCount(pDevIns, &pThis->Virtio, pRxVirtq->uIdx)));
1564 rc = VINF_SUCCESS;
1565 }
1566 virtioCoreVirtqEnableNotify(&pThis->Virtio, pRxVirtq->uIdx, rc == VERR_INVALID_STATE /* fEnable */);
1567 return rc;
1568}
1569
1570/**
1571 * Find an Rx queue that has Rx packets in it, if *any* do.
1572 *
1573 * @todo When multiqueue (MQ) mode is fully supported and tested, some kind of round-robin
1574 * or randomization scheme should probably be incorporated here.
1575 *
1576 * @returns true if Rx pkts avail on queue and sets pRxVirtq to point to queue w/pkts found
1577 * @thread RX
1578 *
1579 */
1580static bool virtioNetR3AreRxBufsAvail(PPDMDEVINS pDevIns, PVIRTIONET pThis, PVIRTIONETVIRTQ *pRxVirtq)
1581{
1582 for (int uVirtqPair = 0; uVirtqPair < pThis->cVirtqPairs; uVirtqPair++)
1583 {
1584 PVIRTIONETVIRTQ pThisRxVirtq = &pThis->aVirtqs[RXQIDX(uVirtqPair)];
1585 if (RT_SUCCESS(virtioNetR3CheckRxBufsAvail(pDevIns, pThis, pThisRxVirtq)))
1586 {
1587 if (pRxVirtq)
1588 *pRxVirtq = pThisRxVirtq;
1589 return true;
1590 }
1591 }
1592 return false;
1593}
1594
1595/**
1596 * @interface_method_impl{PDMINETWORKDOWN,pfnWaitReceiveAvail}
1597 */
1598static DECLCALLBACK(int) virtioNetR3NetworkDown_WaitReceiveAvail(PPDMINETWORKDOWN pInterface, RTMSINTERVAL timeoutMs)
1599{
1600 PVIRTIONETCC pThisCC = RT_FROM_MEMBER(pInterface, VIRTIONETCC, INetworkDown);
1601 PPDMDEVINS pDevIns = pThisCC->pDevIns;
1602 PVIRTIONET pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIONET);
1603
1604 if (!virtioNetIsOperational(pThis, pDevIns))
1605 return VERR_INTERRUPTED;
1606
1607 if (virtioNetR3AreRxBufsAvail(pDevIns, pThis, NULL /* pRxVirtq */))
1608 {
1609 Log10Func(("[%s] Rx bufs available, releasing waiter...\n", pThis->szInst));
1610 return VINF_SUCCESS;
1611 }
1612 if (!timeoutMs)
1613 return VERR_NET_NO_BUFFER_SPACE;
1614
1615 LogFunc(("[%s] %s\n", pThis->szInst, timeoutMs == RT_INDEFINITE_WAIT ? "<indefinite wait>" : ""));
1616
1617 ASMAtomicXchgBool(&pThis->fLeafWantsEmptyRxBufs, true);
1618 STAM_PROFILE_START(&pThis->StatRxOverflow, a);
1619
1620 do {
1621 if (virtioNetR3AreRxBufsAvail(pDevIns, pThis, NULL /* pRxVirtq */))
1622 {
1623 Log10Func(("[%s] Rx bufs now available, releasing waiter...\n", pThis->szInst));
1624 ASMAtomicXchgBool(&pThis->fLeafWantsEmptyRxBufs, false);
1625 return VINF_SUCCESS;
1626 }
1627 Log9Func(("[%s] Starved for empty guest Rx bufs. Waiting...\n", pThis->szInst));
1628
1629 int rc = PDMDevHlpSUPSemEventWaitNoResume(pDevIns, pThis->hEventRxDescAvail, timeoutMs);
1630
1631 if (rc == VERR_TIMEOUT || rc == VERR_INTERRUPTED)
1632 {
1633 LogFunc(("Woken due to %s\n", rc == VERR_TIMEOUT ? "timeout" : "getting interrupted"));
1634
1635 if (!virtioNetIsOperational(pThis, pDevIns))
1636 break;
1637
1638 continue;
1639 }
1640 if (RT_FAILURE(rc)) {
1641 LogFunc(("Waken due to failure %Rrc\n", rc));
1642 RTThreadSleep(1);
1643 }
1644 } while (virtioNetIsOperational(pThis, pDevIns));
1645
1646 STAM_PROFILE_STOP(&pThis->StatRxOverflow, a);
1647 ASMAtomicXchgBool(&pThis->fLeafWantsEmptyRxBufs, false);
1648
1649 Log7Func(("[%s] Wait for Rx buffers available was interrupted\n", pThis->szInst));
1650 return VERR_INTERRUPTED;
1651}
1652
1653/**
1654 * Sets up the GSO context according to the Virtio header.
1655 *
1656 * @param pGso The GSO context to setup.
1657 * @param pCtx The context descriptor.
1658 */
1659DECLINLINE(PPDMNETWORKGSO) virtioNetR3SetupGsoCtx(PPDMNETWORKGSO pGso, VIRTIONETPKTHDR const *pPktHdr)
1660{
1661 pGso->u8Type = PDMNETWORKGSOTYPE_INVALID;
1662
1663 if (pPktHdr->uGsoType & VIRTIONET_HDR_GSO_ECN)
1664 {
1665 AssertMsgFailed(("Unsupported flag in virtio header: ECN\n"));
1666 return NULL;
1667 }
1668 switch (pPktHdr->uGsoType & ~VIRTIONET_HDR_GSO_ECN)
1669 {
1670 case VIRTIONET_HDR_GSO_TCPV4:
1671 pGso->u8Type = PDMNETWORKGSOTYPE_IPV4_TCP;
1672 pGso->cbHdrsSeg = pPktHdr->uHdrLen;
1673 break;
1674 case VIRTIONET_HDR_GSO_TCPV6:
1675 pGso->u8Type = PDMNETWORKGSOTYPE_IPV6_TCP;
1676 pGso->cbHdrsSeg = pPktHdr->uHdrLen;
1677 break;
1678 case VIRTIONET_HDR_GSO_UDP:
1679 pGso->u8Type = PDMNETWORKGSOTYPE_IPV4_UDP;
1680 pGso->cbHdrsSeg = pPktHdr->uChksumStart;
1681 break;
1682 default:
1683 return NULL;
1684 }
1685 if (pPktHdr->uFlags & VIRTIONET_HDR_F_NEEDS_CSUM)
1686 pGso->offHdr2 = pPktHdr->uChksumStart;
1687 else
1688 {
1689 AssertMsgFailed(("GSO without checksum offloading!\n"));
1690 return NULL;
1691 }
1692 pGso->offHdr1 = sizeof(RTNETETHERHDR);
1693 pGso->cbHdrsTotal = pPktHdr->uHdrLen;
1694 pGso->cbMaxSeg = pPktHdr->uGsoSize;
1695 return pGso;
1696}
1697
1698/**
1699 * @interface_method_impl{PDMINETWORKCONFIG,pfnGetMac}
1700 */
1701static DECLCALLBACK(int) virtioNetR3NetworkConfig_GetMac(PPDMINETWORKCONFIG pInterface, PRTMAC pMac)
1702{
1703 PVIRTIONETCC pThisCC = RT_FROM_MEMBER(pInterface, VIRTIONETCC, INetworkConfig);
1704 PVIRTIONET pThis = PDMDEVINS_2_DATA(pThisCC->pDevIns, PVIRTIONET);
1705 memcpy(pMac, pThis->virtioNetConfig.uMacAddress.au8, sizeof(RTMAC));
1706 return VINF_SUCCESS;
1707}
1708
1709/**
1710 * Returns true if it is a broadcast packet.
1711 *
1712 * @returns true if destination address indicates broadcast.
1713 * @param pvBuf The ethernet packet.
1714 */
1715DECLINLINE(bool) virtioNetR3IsBroadcast(const void *pvBuf)
1716{
1717 static const uint8_t s_abBcastAddr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
1718 return memcmp(pvBuf, s_abBcastAddr, sizeof(s_abBcastAddr)) == 0;
1719}
1720
1721/**
1722 * Returns true if it is a multicast packet.
1723 *
1724 * @remarks returns true for broadcast packets as well.
1725 * @returns true if destination address indicates multicast.
1726 * @param pvBuf The ethernet packet.
1727 */
1728DECLINLINE(bool) virtioNetR3IsMulticast(const void *pvBuf)
1729{
1730 return (*(char*)pvBuf) & 1;
1731}
1732
1733/**
1734 * Determines if the packet is to be delivered to upper layer.
1735 *
1736 * @returns true if packet is intended for this node.
1737 * @param pThis Pointer to the state structure.
1738 * @param pvBuf The ethernet packet.
1739 * @param cb Number of bytes available in the packet.
1740 */
1741static bool virtioNetR3AddressFilter(PVIRTIONET pThis, const void *pvBuf, size_t cb)
1742{
1743
1744#ifdef LOG_ENABLED
1745 if (LogIs11Enabled())
1746 {
1747 char *pszType;
1748 if (virtioNetR3IsMulticast(pvBuf))
1749 pszType = (char *)"mcast";
1750 else if (virtioNetR3IsBroadcast(pvBuf))
1751 pszType = (char *)"bcast";
1752 else
1753 pszType = (char *)"ucast";
1754
1755 LogFunc(("node(%RTmac%s%s), pkt(%RTmac, %s) ",
1756 pThis->virtioNetConfig.uMacAddress.au8,
1757 pThis->fPromiscuous ? " promisc" : "",
1758 pThis->fAllMulticast ? " all-mcast" : "",
1759 pvBuf, pszType));
1760 }
1761#endif
1762
1763 if (pThis->fPromiscuous) {
1764 Log11(("\n"));
1765 return true;
1766 }
1767
1768 /* Ignore everything outside of our VLANs */
1769 uint16_t *uPtr = (uint16_t *)pvBuf;
1770
1771 /* Compare TPID with VLAN Ether Type */
1772 if ( uPtr[6] == RT_H2BE_U16(0x8100)
1773 && !ASMBitTest(pThis->aVlanFilter, RT_BE2H_U16(uPtr[7]) & 0xFFF))
1774 {
1775 Log11Func(("\n[%s] not our VLAN, returning false\n", pThis->szInst));
1776 return false;
1777 }
1778
1779 if (virtioNetR3IsBroadcast(pvBuf))
1780 {
1781 Log11(("acpt (bcast)\n"));
1782#ifdef LOG_ENABLED
1783 if (LogIs12Enabled())
1784 virtioNetR3PacketDump(pThis, (const uint8_t *)pvBuf, cb, "<-- Incoming");
1785#endif
1786 return true;
1787 }
1788 if (pThis->fAllMulticast && virtioNetR3IsMulticast(pvBuf))
1789 {
1790 Log11(("acpt (all-mcast)\n"));
1791#ifdef LOG_ENABLED
1792 if (LogIs12Enabled())
1793 virtioNetR3PacketDump(pThis, (const uint8_t *)pvBuf, cb, "<-- Incoming");
1794#endif
1795 return true;
1796 }
1797
1798 if (!memcmp(pThis->virtioNetConfig.uMacAddress.au8, pvBuf, sizeof(RTMAC)))
1799 {
1800 Log11(("acpt (to-node)\n"));
1801#ifdef LOG_ENABLED
1802 if (LogIs12Enabled())
1803 virtioNetR3PacketDump(pThis, (const uint8_t *)pvBuf, cb, "<-- Incoming");
1804#endif
1805 return true;
1806 }
1807
1808 for (uint16_t i = 0; i < pThis->cMulticastFilterMacs; i++)
1809 {
1810 if (!memcmp(&pThis->aMacMulticastFilter[i], pvBuf, sizeof(RTMAC)))
1811 {
1812 Log11(("acpt (mcast whitelist)\n"));
1813#ifdef LOG_ENABLED
1814 if (LogIs12Enabled())
1815 virtioNetR3PacketDump(pThis, (const uint8_t *)pvBuf, cb, "<-- Incoming");
1816#endif
1817 return true;
1818 }
1819 }
1820
1821 for (uint16_t i = 0; i < pThis->cUnicastFilterMacs; i++)
1822 if (!memcmp(&pThis->aMacUnicastFilter[i], pvBuf, sizeof(RTMAC)))
1823 {
1824 Log11(("acpt (ucast whitelist)\n"));
1825 return true;
1826 }
1827#ifdef LOG_ENABLED
1828 if (LogIs11Enabled())
1829 Log(("... reject\n"));
1830#endif
1831
1832 return false;
1833}
1834
1835
1836/**
1837 * This handles the case where Rx packet must be transfered to guest driver via multiple buffers using
1838 * copy tactics slower than preferred method using a single virtq buf. Yet this is an available option
1839 * for guests. Although cited in the spec it's to accomodate guest that perhaps have memory constraints
1840 * wherein guest may benefit from smaller buffers (see MRG_RXBUF feature), in practice it is seen
1841 * that without MRG_RXBUF the linux guest enqueues 'huge' multi-segment buffers so that the largest
1842 * conceivable Rx packet can be contained in a single buffer, where for most transactions most of that
1843 * memory will be unfilled, so it is typically both wasteful and *slower* to avoid MRG_RXBUF.
1844 *
1845 * As an optimization, this multi-buffer copy is only used when:
1846 *
1847 * A. Guest has negotiated MRG_RXBUF
1848 * B. Next packet in the Rx avail queue isn't big enough to contain Rx pkt hdr+data.
1849 *
1850 * Architecture is defined in VirtIO 1.1 5.1.6 (Device Operations), which has improved
1851 * wording over the VirtIO 1.0 specification, but, as an implementation note, there is one
1852 * ambiguity that needs clarification:
1853 *
1854 * The VirtIO 1.1, 5.1.6.4 explains something in a potentially misleading way. And note,
1855 * the VirtIO spec makes a document-wide assertion that the distinction between
1856 * "SHOULD" and "MUST" is to be taken quite literally.
1857 *
1858 * The confusion is that VirtIO 1.1, 5.1.6.3.1 essentially says guest driver "SHOULD" populate
1859 * Rx queue with buffers large enough to accomodate full pkt hdr + data. That's a grammatical
1860 * error (dangling participle).
1861 *
1862 * In practice we MUST assume "SHOULD" strictly applies to the word *populate*, -not- to buffer
1863 * size, because ultimately buffer minimum size is predicated on configuration parameters,
1864 * specifically, when MRG_RXBUF feature is disabled, the driver *MUST* provide Rx bufs
1865 * (if and when it can provide them), that are *large enough* to hold pkt hdr + payload.
1866 *
1867 * Therefore, proper interpretation of 5.1.6.3.1 is, the guest *should* (ideally) keep Rx virtq
1868 * populated with appropriately sized buffers to *prevent starvation* (i.e. starvation may be
1869 * unavoidable thus can't be prohibited). As it would be a ludicrous to presume 5.1.6.3.1 is
1870 * giving guests leeway to violate MRG_RXBUF feature buf size constraints.
1871 *
1872 * @param pDevIns PDM instance
1873 * @param pThis Device instance
1874 * @param pvBuf Pointer to incoming GSO Rx data from downstream device
1875 * @param cb Amount of data given
1876 * @param rxPktHdr Rx pkt Header that's been massaged into VirtIO semantics
1877 * @param pRxVirtq Pointer to Rx virtq
1878 * @param pVirtqBuf Initial virtq buffer to start copying Rx hdr/pkt to guest into
1879 *
1880 */
1881static int virtioNetR3RxPktMultibufXfer(PPDMDEVINS pDevIns, PVIRTIONET pThis, uint8_t *pvPktBuf, size_t cb,
1882 PVIRTIONETPKTHDR pRxPktHdr, PVIRTIONETVIRTQ pRxVirtq, PVIRTQBUF pVirtqBuf)
1883{
1884
1885 size_t cbBufRemaining = pVirtqBuf->cbPhysReturn;
1886 size_t cbPktHdr = pThis->cbPktHdr;
1887
1888 AssertMsgReturn(cbBufRemaining >= pThis->cbPktHdr,
1889 ("guest-provided Rx buf not large enough to store pkt hdr"), VERR_INTERNAL_ERROR);
1890
1891 Log7Func((" Sending packet header to guest...\n"));
1892
1893 /* Copy packet header to rx buf provided by caller. */
1894 uint32_t cbHdrEnqueued = pVirtqBuf->cbPhysReturn == cbPktHdr ? cbPktHdr : 0;
1895 virtioCoreR3VirtqUsedBufPut(pDevIns, &pThis->Virtio, pRxVirtq->uIdx, cbPktHdr, pRxPktHdr, pVirtqBuf, cbHdrEnqueued);
1896
1897 /* Cache address of uNumBuffers field of pkthdr to update ex post facto */
1898 RTGCPHYS GCPhysNumBuffers = pVirtqBuf->pSgPhysReturn->paSegs[0].GCPhys + RT_UOFFSETOF(VIRTIONETPKTHDR, uNumBuffers);
1899 uint16_t cVirtqBufsUsed = 1;
1900 cbBufRemaining -= cbPktHdr;
1901 /*
1902 * Copy packet to guest using as many buffers as necessary, tracking and handling whether
1903 * the buf containing the packet header was already written to the Rx queue's used buffer ring.
1904 */
1905 uint64_t uPktOffset = 0;
1906 while(uPktOffset < cb)
1907 {
1908 Log7Func((" Sending packet data (in buffer #%d) to guest...\n", cVirtqBufsUsed));
1909 size_t cbBounded = RT_MIN(cbBufRemaining, cb - uPktOffset);
1910 (void) virtioCoreR3VirtqUsedBufPut(pDevIns, &pThis->Virtio, pRxVirtq->uIdx, cbBounded,
1911 pvPktBuf + uPktOffset, pVirtqBuf, cbBounded + (cbPktHdr - cbHdrEnqueued) /* cbEnqueue */);
1912 ++cVirtqBufsUsed;
1913 cbBufRemaining -= cbBounded;
1914 uPktOffset -= cbBounded;
1915 if (uPktOffset < cb)
1916 {
1917 cbHdrEnqueued = cbPktHdr;
1918 int rc = virtioCoreR3VirtqAvailBufGet(pDevIns, &pThis->Virtio, pRxVirtq->uIdx, &pVirtqBuf, true);
1919
1920 AssertMsgReturn(rc == VINF_SUCCESS || rc == VERR_NOT_AVAILABLE, ("%Rrc\n", rc), rc);
1921
1922 AssertMsgReturnStmt(rc == VINF_SUCCESS && pVirtqBuf->cbPhysReturn,
1923 ("Not enough Rx buffers in queue to accomodate ethernet packet\n"),
1924 virtioCoreR3VirtqBufRelease(&pThis->Virtio, pVirtqBuf),
1925 VERR_INTERNAL_ERROR);
1926 }
1927 }
1928
1929 /* Fix-up pkthdr (in guest phys. memory) with number of buffers (descriptors) that were processed */
1930 int rc = virtioCoreGCPhysWrite(&pThis->Virtio, pDevIns, GCPhysNumBuffers, &cVirtqBufsUsed, sizeof(cVirtqBufsUsed));
1931 AssertMsgRCReturn(rc, ("Failure updating descriptor count in pkt hdr in guest physical memory\n"), rc);
1932
1933 virtioCoreR3VirtqBufRelease(&pThis->Virtio, pVirtqBuf);
1934 virtioCoreVirtqUsedRingSync(pDevIns, &pThis->Virtio, pRxVirtq->uIdx);
1935 Log7(("\n"));
1936 return rc;
1937}
1938
1939/**
1940 * Pad and store received packet.
1941 *
1942 * @remarks Make sure that the packet appears to upper layer as one coming
1943 * from real Ethernet: pad it and insert FCS.
1944 *
1945 * @returns VBox status code.
1946 * @param pDevIns The device instance.
1947 * @param pThis The virtio-net shared instance data.
1948 * @param pvBuf The available data.
1949 * @param cb Number of bytes available in the buffer.
1950 * @param pGso Pointer to Global Segmentation Offload structure
1951 * @param pRxVirtq Pointer to Rx virtqueue
1952 * @thread RX
1953 */
1954
1955static int virtioNetR3CopyRxPktToGuest(PPDMDEVINS pDevIns, PVIRTIONET pThis, PVIRTIONETCC pThisCC, const void *pvBuf, size_t cb,
1956 PVIRTIONETPKTHDR pRxPktHdr, uint8_t cbPktHdr, PVIRTIONETVIRTQ pRxVirtq)
1957{
1958 RT_NOREF(pThisCC);
1959 PVIRTQBUF pVirtqBuf;
1960 int rc = virtioCoreR3VirtqAvailBufGet(pDevIns, &pThis->Virtio, pRxVirtq->uIdx, &pVirtqBuf, true);
1961
1962 AssertMsgReturn(rc == VINF_SUCCESS || rc == VERR_NOT_AVAILABLE, ("%Rrc\n", rc), rc);
1963
1964 AssertMsgReturnStmt(rc == VINF_SUCCESS && pVirtqBuf->cbPhysReturn,
1965 ("Not enough Rx buffers or capacity to accommodate ethernet packet\n"),
1966 virtioCoreR3VirtqBufRelease(&pThis->Virtio, pVirtqBuf),
1967 VERR_INTERNAL_ERROR);
1968 /*
1969 * Try to do fast (e.g. single-buffer) copy to guest, even if MRG_RXBUF feature is enabled
1970 */
1971 STAM_PROFILE_START(&pThis->StatReceiveStore, a);
1972 if (RT_LIKELY(FEATURE_DISABLED(MRG_RXBUF))
1973 || RT_LIKELY(pVirtqBuf->cbPhysReturn > cb + cbPktHdr))
1974 {
1975 Log7Func(("Send Rx packet header and data to guest (single-buffer copy)...\n"));
1976 pRxPktHdr->uNumBuffers = 1;
1977 rc = virtioCoreR3VirtqUsedBufPut(pDevIns, &pThis->Virtio, pRxVirtq->uIdx, cbPktHdr, pRxPktHdr, pVirtqBuf, 0 /* cbEnqueue */);
1978 if (rc == VINF_SUCCESS)
1979 rc = virtioCoreR3VirtqUsedBufPut(pDevIns, &pThis->Virtio, pRxVirtq->uIdx, cb, pvBuf, pVirtqBuf, cbPktHdr + cb /* cbEnqueue */);
1980 virtioCoreR3VirtqBufRelease(&pThis->Virtio, pVirtqBuf);
1981 virtioCoreVirtqUsedRingSync(pDevIns, &pThis->Virtio, pRxVirtq->uIdx);
1982 AssertMsgReturn(rc == VINF_SUCCESS, ("%Rrc\n", rc), rc);
1983 }
1984 else
1985 {
1986 Log7Func(("Send Rx pkt to guest (merged-buffer copy [MRG_RXBUF feature])...\n"));
1987 rc = virtioNetR3RxPktMultibufXfer(pDevIns, pThis, (uint8_t *)pvBuf, cb, pRxPktHdr, pRxVirtq, pVirtqBuf);
1988 return rc;
1989 }
1990 STAM_PROFILE_STOP(&pThis->StatReceiveStore, a);
1991 return VINF_SUCCESS;
1992}
1993
1994/**
1995 * @interface_method_impl{PDMINETWORKDOWN,pfnReceiveGso}
1996 */
1997static DECLCALLBACK(int) virtioNetR3NetworkDown_ReceiveGso(
1998 PPDMINETWORKDOWN pInterface, const void *pvBuf, size_t cb, PCPDMNETWORKGSO pGso)
1999{
2000 PVIRTIONETCC pThisCC = RT_FROM_MEMBER(pInterface, VIRTIONETCC, INetworkDown);
2001 PPDMDEVINS pDevIns = pThisCC->pDevIns;
2002 PVIRTIONET pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIONET);
2003 VIRTIONETPKTHDR rxPktHdr = { 0, VIRTIONET_HDR_GSO_NONE, 0, 0, 0, 0, 0 };
2004
2005 if (!pThis->fVirtioReady)
2006 {
2007 LogRelFunc(("VirtIO not ready, aborting downstream receive\n"));
2008 return VERR_INTERRUPTED;
2009 }
2010 /*
2011 * If GSO (Global Segment Offloading) was received from downstream PDM network device, massage the
2012 * PDM-provided GSO parameters into VirtIO semantics, which get passed to guest virtio-net via
2013 * Rx pkt header. See VirtIO 1.1, 5.1.6 Device Operation for more information.
2014 */
2015 if (pGso)
2016 {
2017 LogFunc(("[%s] (%RTmac) \n", pThis->szInst, pvBuf));
2018
2019 rxPktHdr.uFlags = VIRTIONET_HDR_F_NEEDS_CSUM;
2020 rxPktHdr.uHdrLen = pGso->cbHdrsTotal;
2021 rxPktHdr.uGsoSize = pGso->cbMaxSeg;
2022 rxPktHdr.uChksumStart = pGso->offHdr2;
2023
2024 switch (pGso->u8Type)
2025 {
2026 case PDMNETWORKGSOTYPE_IPV4_TCP:
2027 rxPktHdr.uGsoType = VIRTIONET_HDR_GSO_TCPV4;
2028 rxPktHdr.uChksumOffset = RT_OFFSETOF(RTNETTCP, th_sum);
2029 break;
2030 case PDMNETWORKGSOTYPE_IPV6_TCP:
2031 rxPktHdr.uGsoType = VIRTIONET_HDR_GSO_TCPV6;
2032 rxPktHdr.uChksumOffset = RT_OFFSETOF(RTNETTCP, th_sum);
2033 break;
2034 case PDMNETWORKGSOTYPE_IPV4_UDP:
2035 rxPktHdr.uGsoType = VIRTIONET_HDR_GSO_UDP;
2036 rxPktHdr.uChksumOffset = RT_OFFSETOF(RTNETUDP, uh_sum);
2037 break;
2038 default:
2039 LogFunc(("[%s] GSO type (0x%x) not supported\n", pThis->szInst, pGso->u8Type));
2040 return VERR_NOT_SUPPORTED;
2041 }
2042 STAM_REL_COUNTER_INC(&pThis->StatReceiveGSO);
2043 Log2Func(("[%s] gso type=%#x, cbHdrsTotal=%u cbHdrsSeg=%u mss=%u offHdr1=%#x offHdr2=%#x\n",
2044 pThis->szInst, pGso->u8Type, pGso->cbHdrsTotal, pGso->cbHdrsSeg,
2045 pGso->cbMaxSeg, pGso->offHdr1, pGso->offHdr2));
2046 }
2047
2048 /*
2049 * Find a virtq with Rx bufs on avail ring, if any, and copy the packet to the guest's Rx buffer.
2050 * @todo pk: PROBABLY NOT A SOPHISTICATED ENOUGH QUEUE SELECTION ALGORTITH FOR OPTIMAL MQ (FEATURE) SUPPORT
2051 */
2052 for (int uVirtqPair = 0; uVirtqPair < pThis->cVirtqPairs; uVirtqPair++)
2053 {
2054 PVIRTIONETVIRTQ pRxVirtq = &pThis->aVirtqs[RXQIDX(uVirtqPair)];
2055 if (RT_SUCCESS(virtioNetR3CheckRxBufsAvail(pDevIns, pThis, pRxVirtq)))
2056 {
2057 int rc = VINF_SUCCESS;
2058 STAM_PROFILE_START(&pThis->StatReceive, a);
2059 virtioNetR3SetReadLed(pThisCC, true);
2060 if (virtioNetR3AddressFilter(pThis, pvBuf, cb))
2061 {
2062 /* rxPktHdr is local stack variable that should not go out of scope in this use */
2063 rc = virtioNetR3CopyRxPktToGuest(pDevIns, pThis, pThisCC, pvBuf, cb, &rxPktHdr, pThis->cbPktHdr, pRxVirtq);
2064 STAM_REL_COUNTER_ADD(&pThis->StatReceiveBytes, cb);
2065 }
2066 virtioNetR3SetReadLed(pThisCC, false);
2067 STAM_PROFILE_STOP(&pThis->StatReceive, a);
2068 return rc;
2069 }
2070 }
2071 return VERR_INTERRUPTED;
2072}
2073
2074/**
2075 * @interface_method_impl{PDMINETWORKDOWN,pfnReceive}
2076 */
2077static DECLCALLBACK(int) virtioNetR3NetworkDown_Receive(PPDMINETWORKDOWN pInterface, const void *pvBuf, size_t cb)
2078{
2079
2080#ifdef LOG_ENABLED
2081 PVIRTIONETCC pThisCC = RT_FROM_MEMBER(pInterface, VIRTIONETCC, INetworkDown);
2082 PPDMDEVINS pDevIns = pThisCC->pDevIns;
2083 PVIRTIONET pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIONET);
2084 LogFunc(("[%s] (%RTmac)\n", pThis->szInst, pvBuf));
2085#endif
2086
2087 return virtioNetR3NetworkDown_ReceiveGso(pInterface, pvBuf, cb, NULL);
2088}
2089
2090/*
2091 * Dispatched to here from virtioNetR3Ctrl() to configure this virtio-net device's Rx packet receive filtering.
2092 * See VirtIO 1.0, 5.1.6.5.1
2093 *
2094 * @param pThis virtio-net instance
2095 * @param pCtrlPktHdr Control packet header (which includes command parameters)
2096 * @param pVirtqBuf Buffer from ctrlq buffer (contains command data)
2097 */
2098static uint8_t virtioNetR3CtrlRx(PVIRTIONET pThis, PVIRTIONETCC pThisCC,
2099 PVIRTIONET_CTRL_HDR_T pCtrlPktHdr, PVIRTQBUF pVirtqBuf)
2100{
2101
2102#define LOG_VIRTIONET_FLAG(fld) LogFunc(("[%s] Setting %s=%d\n", pThis->szInst, #fld, pThis->fld))
2103
2104 LogFunc(("[%s] Processing CTRL Rx command\n", pThis->szInst));
2105 switch(pCtrlPktHdr->uCmd)
2106 {
2107 case VIRTIONET_CTRL_RX_PROMISC:
2108 break;
2109 case VIRTIONET_CTRL_RX_ALLMULTI:
2110 break;
2111 case VIRTIONET_CTRL_RX_ALLUNI:
2112 /* fallthrough */
2113 case VIRTIONET_CTRL_RX_NOMULTI:
2114 /* fallthrough */
2115 case VIRTIONET_CTRL_RX_NOUNI:
2116 /* fallthrough */
2117 case VIRTIONET_CTRL_RX_NOBCAST:
2118 AssertMsgReturn(FEATURE_ENABLED(CTRL_RX_EXTRA),
2119 ("CTRL 'extra' cmd w/o VIRTIONET_F_CTRL_RX_EXTRA feature negotiated - skipping\n"),
2120 VIRTIONET_ERROR);
2121 /* fall out */
2122 }
2123
2124 uint8_t fOn, fPromiscChanged = false;
2125 virtioCoreR3VirtqBufDrain(&pThis->Virtio, pVirtqBuf, &fOn, (size_t)RT_MIN(pVirtqBuf->cbPhysSend, sizeof(fOn)));
2126
2127 switch(pCtrlPktHdr->uCmd)
2128 {
2129 case VIRTIONET_CTRL_RX_PROMISC:
2130 pThis->fPromiscuous = RT_BOOL(fOn);
2131 fPromiscChanged = true;
2132 LOG_VIRTIONET_FLAG(fPromiscuous);
2133 break;
2134 case VIRTIONET_CTRL_RX_ALLMULTI:
2135 pThis->fAllMulticast = RT_BOOL(fOn);
2136 fPromiscChanged = true;
2137 LOG_VIRTIONET_FLAG(fAllMulticast);
2138 break;
2139 case VIRTIONET_CTRL_RX_ALLUNI:
2140 pThis->fAllUnicast = RT_BOOL(fOn);
2141 LOG_VIRTIONET_FLAG(fAllUnicast);
2142 break;
2143 case VIRTIONET_CTRL_RX_NOMULTI:
2144 pThis->fNoMulticast = RT_BOOL(fOn);
2145 LOG_VIRTIONET_FLAG(fNoMulticast);
2146 break;
2147 case VIRTIONET_CTRL_RX_NOUNI:
2148 pThis->fNoUnicast = RT_BOOL(fOn);
2149 LOG_VIRTIONET_FLAG(fNoUnicast);
2150 break;
2151 case VIRTIONET_CTRL_RX_NOBCAST:
2152 pThis->fNoBroadcast = RT_BOOL(fOn);
2153 LOG_VIRTIONET_FLAG(fNoBroadcast);
2154 break;
2155 }
2156
2157 if (pThisCC->pDrv && fPromiscChanged)
2158 pThisCC->pDrv->pfnSetPromiscuousMode(pThisCC->pDrv, (pThis->fPromiscuous || pThis->fAllMulticast));
2159
2160 return VIRTIONET_OK;
2161}
2162
2163/*
2164 * Dispatched to here from virtioNetR3Ctrl() to configure this virtio-net device's MAC filter tables
2165 * See VirtIO 1.0, 5.1.6.5.2
2166 *
2167 * @param pThis virtio-net instance
2168 * @param pCtrlPktHdr Control packet header (which includes command parameters)
2169 * @param pVirtqBuf Buffer from ctrlq buffer (contains command data)
2170 */
2171static uint8_t virtioNetR3CtrlMac(PVIRTIONET pThis, PVIRTIONET_CTRL_HDR_T pCtrlPktHdr, PVIRTQBUF pVirtqBuf)
2172{
2173 LogFunc(("[%s] Processing CTRL MAC command\n", pThis->szInst));
2174
2175
2176 AssertMsgReturn(pVirtqBuf->cbPhysSend >= sizeof(*pCtrlPktHdr),
2177 ("insufficient descriptor space for ctrl pkt hdr"),
2178 VIRTIONET_ERROR);
2179
2180 size_t cbRemaining = pVirtqBuf->cbPhysSend;
2181 switch(pCtrlPktHdr->uCmd)
2182 {
2183 case VIRTIONET_CTRL_MAC_ADDR_SET:
2184 {
2185 /* Set default Rx filter MAC */
2186 AssertMsgReturn(cbRemaining >= sizeof(pThis->rxFilterMacDefault),
2187 ("DESC chain too small to process CTRL_MAC_ADDR_SET cmd\n"), VIRTIONET_ERROR);
2188
2189 virtioCoreR3VirtqBufDrain(&pThis->Virtio, pVirtqBuf, &pThis->rxFilterMacDefault, sizeof(RTMAC));
2190 break;
2191 }
2192 case VIRTIONET_CTRL_MAC_TABLE_SET:
2193 {
2194 VIRTIONET_CTRL_MAC_TABLE_LEN cMacs;
2195
2196 /* Load unicast MAC filter table */
2197 AssertMsgReturn(cbRemaining >= sizeof(cMacs),
2198 ("DESC chain too small to process CTRL_MAC_TABLE_SET cmd\n"), VIRTIONET_ERROR);
2199
2200 /* Fetch count of unicast filter MACs from guest buffer */
2201 virtioCoreR3VirtqBufDrain(&pThis->Virtio, pVirtqBuf, &cMacs, sizeof(cMacs));
2202 cbRemaining -= sizeof(cMacs);
2203
2204 Log7Func(("[%s] Guest provided %d unicast MAC Table entries\n", pThis->szInst, cMacs));
2205
2206 if (cMacs)
2207 {
2208 uint32_t cbMacs = cMacs * sizeof(RTMAC);
2209
2210 AssertMsgReturn(cbMacs <= sizeof(pThis->aMacUnicastFilter) / sizeof(RTMAC),
2211 ("Guest provided Unicast MAC filter table exceeds hardcoded table size"), VIRTIONET_ERROR);
2212
2213 AssertMsgReturn(cbRemaining >= cbMacs,
2214 ("Virtq buffer too small to process CTRL_MAC_TABLE_SET cmd\n"), VIRTIONET_ERROR);
2215
2216
2217 /* Fetch unicast table contents from guest buffer */
2218 virtioCoreR3VirtqBufDrain(&pThis->Virtio, pVirtqBuf, &pThis->aMacUnicastFilter, cbMacs);
2219 cbRemaining -= cbMacs;
2220 }
2221 pThis->cUnicastFilterMacs = cMacs;
2222
2223 /* Load multicast MAC filter table */
2224 AssertMsgReturn(cbRemaining >= sizeof(cMacs),
2225 ("Virtq buffer too small to process CTRL_MAC_TABLE_SET cmd\n"), VIRTIONET_ERROR);
2226
2227 /* Fetch count of multicast filter MACs from guest buffer */
2228 virtioCoreR3VirtqBufDrain(&pThis->Virtio, pVirtqBuf, &cMacs, sizeof(cMacs));
2229 cbRemaining -= sizeof(cMacs);
2230
2231 Log10Func(("[%s] Guest provided %d multicast MAC Table entries\n", pThis->szInst, cMacs));
2232
2233 if (cMacs)
2234 {
2235 uint32_t cbMacs = cMacs * sizeof(RTMAC);
2236
2237 AssertMsgReturn(cbMacs <= sizeof(pThis->aMacMulticastFilter) / sizeof(RTMAC),
2238 ("Guest provided Unicast MAC filter table exceeds hardcoded table size"), VIRTIONET_ERROR);
2239
2240 AssertMsgReturn(cbRemaining >= cbMacs,
2241 ("Virtq buffer too small to process CTRL_MAC_TABLE_SET cmd\n"), VIRTIONET_ERROR);
2242
2243 /* Fetch multicast table contents from guest buffer */
2244 virtioCoreR3VirtqBufDrain(&pThis->Virtio, pVirtqBuf, &pThis->aMacMulticastFilter, cbMacs);
2245 cbRemaining -= cbMacs;
2246 }
2247 pThis->cMulticastFilterMacs = cMacs;
2248
2249#ifdef LOG_ENABLED
2250 LogFunc(("[%s] unicast MACs:\n", pThis->szInst));
2251 for(unsigned i = 0; i < pThis->cUnicastFilterMacs; i++)
2252 LogFunc((" %RTmac\n", &pThis->aMacUnicastFilter[i]));
2253
2254 LogFunc(("[%s] multicast MACs:\n", pThis->szInst));
2255 for(unsigned i = 0; i < pThis->cMulticastFilterMacs; i++)
2256 LogFunc((" %RTmac\n", &pThis->aMacMulticastFilter[i]));
2257#endif
2258 break;
2259 }
2260 default:
2261 LogRelFunc(("Unrecognized MAC subcommand in CTRL pkt from guest\n"));
2262 return VIRTIONET_ERROR;
2263 }
2264 return VIRTIONET_OK;
2265}
2266
2267/*
2268 * Dispatched to here from virtioNetR3Ctrl() to configure this virtio-net device's MQ (multiqueue) operations.
2269 * See VirtIO 1.0, 5.1.6.5.5
2270 *
2271 * @param pThis virtio-net instance
2272 * @param pCtrlPktHdr Control packet header (which includes command parameters)
2273 * @param pVirtqBuf Buffer from ctrlq buffer (contains command data)
2274 */
2275static uint8_t virtioNetR3CtrlMultiQueue(PVIRTIONET pThis, PVIRTIONETCC pThisCC, PPDMDEVINS pDevIns, PVIRTIONET_CTRL_HDR_T pCtrlPktHdr, PVIRTQBUF pVirtqBuf)
2276{
2277 LogFunc(("[%s] Processing CTRL MQ command\n", pThis->szInst));
2278
2279 uint16_t cVirtqPairs;
2280 switch(pCtrlPktHdr->uCmd)
2281 {
2282 case VIRTIONET_CTRL_MQ_VQ_PAIRS_SET:
2283 {
2284 size_t cbRemaining = pVirtqBuf->cbPhysSend - sizeof(*pCtrlPktHdr);
2285
2286 AssertMsgReturn(cbRemaining > sizeof(cVirtqPairs),
2287 ("DESC chain too small for VIRTIONET_CTRL_MQ cmd processing"), VIRTIONET_ERROR);
2288
2289 /* Fetch number of virtq pairs from guest buffer */
2290 virtioCoreR3VirtqBufDrain(&pThis->Virtio, pVirtqBuf, &cVirtqPairs, sizeof(cVirtqPairs));
2291
2292 AssertMsgReturn(cVirtqPairs > VIRTIONET_MAX_QPAIRS,
2293 ("[%s] Guest CTRL MQ virtq pair count out of range)\n", pThis->szInst, cVirtqPairs), VIRTIONET_ERROR);
2294
2295 LogFunc(("[%s] Guest specifies %d VQ pairs in use\n", pThis->szInst, cVirtqPairs));
2296 pThis->cVirtqPairs = cVirtqPairs;
2297 break;
2298 }
2299 default:
2300 LogRelFunc(("Unrecognized multiqueue subcommand in CTRL pkt from guest\n"));
2301 return VIRTIONET_ERROR;
2302 }
2303
2304 /*
2305 * The MQ control function is invoked by the guest in an RPC like manner to change
2306 * the Rx/Tx queue pair count. If the new value exceeds the number of queues
2307 * (and associated workers) already initialized initialize only the new queues and
2308 * respective workers.
2309 */
2310 if (pThis->cVirtqPairs > pThis->cInitializedVirtqPairs)
2311 {
2312 virtioNetR3SetVirtqNames(pThis, virtioCoreIsLegacyMode(&pThis->Virtio));
2313 int rc = virtioNetR3CreateWorkerThreads(pDevIns, pThis, pThisCC);
2314 if (RT_FAILURE(rc))
2315 {
2316 LogRelFunc(("Failed to create worker threads\n"));
2317 return VIRTIONET_ERROR;
2318 }
2319 }
2320 return VIRTIONET_OK;
2321}
2322
2323/*
2324 * Dispatched to here from virtioNetR3Ctrl() to configure this virtio-net device's VLAN filtering.
2325 * See VirtIO 1.0, 5.1.6.5.3
2326 *
2327 * @param pThis virtio-net instance
2328 * @param pCtrlPktHdr Control packet header (which includes command parameters)
2329 * @param pVirtqBuf Buffer from ctrlq buffer (contains command data)
2330 */
2331static uint8_t virtioNetR3CtrlVlan(PVIRTIONET pThis, PVIRTIONET_CTRL_HDR_T pCtrlPktHdr, PVIRTQBUF pVirtqBuf)
2332{
2333 LogFunc(("[%s] Processing CTRL VLAN command\n", pThis->szInst));
2334
2335 uint16_t uVlanId;
2336 size_t cbRemaining = pVirtqBuf->cbPhysSend - sizeof(*pCtrlPktHdr);
2337
2338 AssertMsgReturn(cbRemaining > sizeof(uVlanId),
2339 ("DESC chain too small for VIRTIONET_CTRL_VLAN cmd processing"), VIRTIONET_ERROR);
2340
2341 /* Fetch VLAN ID from guest buffer */
2342 virtioCoreR3VirtqBufDrain(&pThis->Virtio, pVirtqBuf, &uVlanId, sizeof(uVlanId));
2343
2344 AssertMsgReturn(uVlanId > VIRTIONET_MAX_VLAN_ID,
2345 ("%s VLAN ID out of range (VLAN ID=%u)\n", pThis->szInst, uVlanId), VIRTIONET_ERROR);
2346
2347 LogFunc(("[%s] uCommand=%u VLAN ID=%u\n", pThis->szInst, pCtrlPktHdr->uCmd, uVlanId));
2348
2349 switch (pCtrlPktHdr->uCmd)
2350 {
2351 case VIRTIONET_CTRL_VLAN_ADD:
2352 ASMBitSet(pThis->aVlanFilter, uVlanId);
2353 break;
2354 case VIRTIONET_CTRL_VLAN_DEL:
2355 ASMBitClear(pThis->aVlanFilter, uVlanId);
2356 break;
2357 default:
2358 LogRelFunc(("Unrecognized VLAN subcommand in CTRL pkt from guest\n"));
2359 return VIRTIONET_ERROR;
2360 }
2361 return VIRTIONET_OK;
2362}
2363
2364/**
2365 * Processes control command from guest.
2366 * See VirtIO 1.0 spec, 5.1.6 "Device Operation" and 5.1.6.5 "Control Virtqueue".
2367 *
2368 * The control command is contained in a virtio buffer pulled from the virtio-net defined control queue (ctrlq).
2369 * Command type is parsed is dispatched to a command-specific device-configuration handler function (e.g. RX, MAC, VLAN, MQ
2370 * and ANNOUNCE).
2371 *
2372 * This function handles all parts of the host-side of the ctrlq round-trip buffer processing.
2373 *
2374 * Invoked by worker for virtio-net control queue to process a queued control command buffer.
2375 *
2376 * @param pDevIns PDM device instance
2377 * @param pThis virtio-net device instance
2378 * @param pThisCC virtio-net device instance
2379 * @param pVirtqBuf pointer to buffer pulled from virtq (input to this function)
2380 */
2381static void virtioNetR3Ctrl(PPDMDEVINS pDevIns, PVIRTIONET pThis, PVIRTIONETCC pThisCC,
2382 PVIRTQBUF pVirtqBuf)
2383{
2384 if (!(pThis->fNegotiatedFeatures & VIRTIONET_F_CTRL_VQ))
2385 LogFunc(("[%s] WARNING: Guest using CTRL queue w/o negotiating VIRTIONET_F_CTRL_VQ feature\n", pThis->szInst));
2386
2387 LogFunc(("[%s] Received CTRL packet from guest\n", pThis->szInst));
2388
2389 if (pVirtqBuf->cbPhysSend < 2)
2390 {
2391 LogFunc(("[%s] CTRL packet from guest driver incomplete. Skipping ctrl cmd\n", pThis->szInst));
2392 return;
2393 }
2394 else if (pVirtqBuf->cbPhysReturn < sizeof(VIRTIONET_CTRL_HDR_T_ACK))
2395 {
2396 LogFunc(("[%s] Guest driver didn't allocate memory to receive ctrl pkt ACK. Skipping ctrl cmd\n", pThis->szInst));
2397 return;
2398 }
2399
2400 /*
2401 * Allocate buffer and read in the control command
2402 */
2403 PVIRTIONET_CTRL_HDR_T pCtrlPktHdr = (PVIRTIONET_CTRL_HDR_T)RTMemAllocZ(sizeof(VIRTIONET_CTRL_HDR_T));
2404
2405 AssertPtrReturnVoid(pCtrlPktHdr);
2406
2407 AssertMsgReturnVoid(pVirtqBuf->cbPhysSend >= sizeof(VIRTIONET_CTRL_HDR_T),
2408 ("DESC chain too small for CTRL pkt header"));
2409
2410 virtioCoreR3VirtqBufDrain(&pThis->Virtio, pVirtqBuf, pCtrlPktHdr,
2411 RT_MIN(pVirtqBuf->cbPhysSend, sizeof(VIRTIONET_CTRL_HDR_T)));
2412
2413 Log7Func(("[%s] CTRL COMMAND: class=%d command=%d\n", pThis->szInst, pCtrlPktHdr->uClass, pCtrlPktHdr->uCmd));
2414
2415 uint8_t uAck;
2416 switch (pCtrlPktHdr->uClass)
2417 {
2418 case VIRTIONET_CTRL_RX:
2419 uAck = virtioNetR3CtrlRx(pThis, pThisCC, pCtrlPktHdr, pVirtqBuf);
2420 break;
2421 case VIRTIONET_CTRL_MAC:
2422 uAck = virtioNetR3CtrlMac(pThis, pCtrlPktHdr, pVirtqBuf);
2423 break;
2424 case VIRTIONET_CTRL_VLAN:
2425 uAck = virtioNetR3CtrlVlan(pThis, pCtrlPktHdr, pVirtqBuf);
2426 break;
2427 case VIRTIONET_CTRL_MQ:
2428 uAck = virtioNetR3CtrlMultiQueue(pThis, pThisCC, pDevIns, pCtrlPktHdr, pVirtqBuf);
2429 break;
2430 case VIRTIONET_CTRL_ANNOUNCE:
2431 uAck = VIRTIONET_OK;
2432 if (FEATURE_DISABLED(STATUS) || FEATURE_DISABLED(GUEST_ANNOUNCE))
2433 {
2434 LogFunc(("%s Ignoring CTRL class VIRTIONET_CTRL_ANNOUNCE.\n"
2435 "VIRTIO_F_STATUS or VIRTIO_F_GUEST_ANNOUNCE feature not enabled\n", pThis->szInst));
2436 break;
2437 }
2438 if (pCtrlPktHdr->uCmd != VIRTIONET_CTRL_ANNOUNCE_ACK)
2439 {
2440 LogFunc(("[%s] Ignoring CTRL class VIRTIONET_CTRL_ANNOUNCE. Unrecognized uCmd\n", pThis->szInst));
2441 break;
2442 }
2443#if FEATURE_OFFERED(STATUS)
2444 pThis->virtioNetConfig.uStatus &= ~VIRTIONET_F_ANNOUNCE;
2445#endif
2446 Log7Func(("[%s] Clearing VIRTIONET_F_ANNOUNCE in config status\n", pThis->szInst));
2447 break;
2448 default:
2449 LogRelFunc(("Unrecognized CTRL pkt hdr class (%d)\n", pCtrlPktHdr->uClass));
2450 uAck = VIRTIONET_ERROR;
2451 }
2452
2453 /* Currently CTRL pkt header just returns ack, but keeping segment logic generic/flexible
2454 * in case that changes to make adapting more straightforward */
2455 int cSegs = 1;
2456
2457 /* Return CTRL packet Ack byte (result code) to guest driver */
2458 PRTSGSEG paReturnSegs = (PRTSGSEG)RTMemAllocZ(sizeof(RTSGSEG));
2459 AssertMsgReturnVoid(paReturnSegs, ("Out of memory"));
2460
2461 RTSGSEG aStaticSegs[] = { { &uAck, sizeof(uAck) } };
2462 memcpy(paReturnSegs, aStaticSegs, sizeof(RTSGSEG));
2463
2464 PRTSGBUF pReturnSegBuf = (PRTSGBUF)RTMemAllocZ(sizeof(RTSGBUF));
2465 AssertMsgReturnVoid(pReturnSegBuf, ("Out of memory"));
2466
2467 /* Copy segment data to malloc'd memory to avoid stack out-of-scope errors sanitizer doesn't detect */
2468 for (int i = 0; i < cSegs; i++)
2469 {
2470 void *pv = paReturnSegs[i].pvSeg;
2471 paReturnSegs[i].pvSeg = RTMemAlloc(aStaticSegs[i].cbSeg);
2472 AssertMsgReturnVoid(paReturnSegs[i].pvSeg, ("Out of memory"));
2473 memcpy(paReturnSegs[i].pvSeg, pv, aStaticSegs[i].cbSeg);
2474 }
2475
2476 RTSgBufInit(pReturnSegBuf, paReturnSegs, cSegs);
2477
2478 virtioCoreR3VirtqUsedBufPut(pDevIns, &pThis->Virtio, CTRLQIDX, pReturnSegBuf, pVirtqBuf, true /* fFence */);
2479 virtioCoreVirtqUsedRingSync(pDevIns, &pThis->Virtio, CTRLQIDX);
2480
2481 for (int i = 0; i < cSegs; i++)
2482 RTMemFree(paReturnSegs[i].pvSeg);
2483
2484 RTMemFree(paReturnSegs);
2485 RTMemFree(pReturnSegBuf);
2486
2487 LogFunc(("%s Finished processing CTRL command with status %s\n",
2488 pThis->szInst, uAck == VIRTIONET_OK ? "VIRTIONET_OK" : "VIRTIONET_ERROR"));
2489}
2490
2491/**
2492 * Reads virtio-net pkt header from provided Phy. addr of virtio descriptor chain
2493 * (e.g. S/G segment from guest-driver provided buffer pulled from Tx virtq)
2494 * Verifies state and supported modes, sets TCP header size.
2495 *
2496 * @param pVirtio VirtIO core instance data
2497 * @param pThis virtio-net instance
2498 * @param pDevIns PDM device instance
2499 * @param GCPhys Phys. Address from where to read virtio-net pkt header
2500 * @param pPktHdr Where to store read Tx pkt hdr (virtio pkt hdr size is determined from instance configuration)
2501 * @param cbFrame Total pkt frame size to inform bounds check
2502 */
2503static int virtioNetR3ReadVirtioTxPktHdr(PVIRTIOCORE pVirtio, PVIRTIONET pThis, PPDMDEVINS pDevIns, RTGCPHYS GCPhys, PVIRTIONETPKTHDR pPktHdr, size_t cbFrame)
2504{
2505 int rc = virtioCoreGCPhysRead(pVirtio, pDevIns, GCPhys, pPktHdr, pThis->cbPktHdr);
2506 if (RT_FAILURE(rc))
2507 return rc;
2508
2509 LogFunc(("pktHdr (flags=%x gso-type=%x len=%x gso-size=%x Chksum-start=%x Chksum-offset=%x) cbFrame=%d\n",
2510 pPktHdr->uFlags, pPktHdr->uGsoType, pPktHdr->uHdrLen,
2511 pPktHdr->uGsoSize, pPktHdr->uChksumStart, pPktHdr->uChksumOffset, cbFrame));
2512
2513 if (pPktHdr->uGsoType)
2514 {
2515 /* Segmentation offloading cannot be done without checksumming, and we do not support ECN */
2516 AssertMsgReturn( RT_LIKELY(pPktHdr->uFlags & VIRTIONET_HDR_F_NEEDS_CSUM)
2517 && !(RT_UNLIKELY(pPktHdr->uGsoType & VIRTIONET_HDR_GSO_ECN)),
2518 ("Unsupported ECN request in pkt header\n"), VERR_NOT_SUPPORTED);
2519
2520 uint32_t uTcpHdrSize;
2521 switch (pPktHdr->uGsoType)
2522 {
2523 case VIRTIONET_HDR_GSO_TCPV4:
2524 case VIRTIONET_HDR_GSO_TCPV6:
2525 uTcpHdrSize = sizeof(RTNETTCP);
2526 break;
2527 case VIRTIONET_HDR_GSO_UDP:
2528 uTcpHdrSize = 0;
2529 break;
2530 default:
2531 LogFunc(("Bad GSO type in packet header\n"));
2532 return VERR_INVALID_PARAMETER;
2533 }
2534 /* Header + MSS must not exceed the packet size. */
2535 AssertMsgReturn(RT_LIKELY(uTcpHdrSize + pPktHdr->uChksumStart + pPktHdr->uGsoSize <= cbFrame),
2536 ("Header plus message exceeds packet size"), VERR_BUFFER_OVERFLOW);
2537 }
2538
2539 AssertMsgReturn( !pPktHdr->uFlags & VIRTIONET_HDR_F_NEEDS_CSUM
2540 || sizeof(uint16_t) + pPktHdr->uChksumStart + pPktHdr->uChksumOffset <= cbFrame,
2541 ("Checksum (%d bytes) doesn't fit into pkt header (%d bytes)\n",
2542 sizeof(uint16_t) + pPktHdr->uChksumStart + pPktHdr->uChksumOffset, cbFrame),
2543 VERR_BUFFER_OVERFLOW);
2544
2545 return VINF_SUCCESS;
2546}
2547
2548/**
2549 * Transmits single GSO frame via PDM framework to downstream PDM device, to emit from virtual NIC.
2550 *
2551 * This does final prep of GSO parameters including checksum calculation if configured
2552 * (e.g. if VIRTIONET_HDR_F_NEEDS_CSUM flag is set).
2553 *
2554 * @param pThis virtio-net instance
2555 * @param pThisCC virtio-net instance
2556 * @param pSgBuf PDM S/G buffer containing pkt and hdr to transmit
2557 * @param pGso GSO parameters used for the packet
2558 * @param pPktHdr virtio-net pkt header to adapt to PDM semantics
2559 */
2560static int virtioNetR3TransmitFrame(PVIRTIONET pThis, PVIRTIONETCC pThisCC, PPDMSCATTERGATHER pSgBuf,
2561 PPDMNETWORKGSO pGso, PVIRTIONETPKTHDR pPktHdr)
2562{
2563
2564 virtioNetR3PacketDump(pThis, (uint8_t *)pSgBuf->aSegs[0].pvSeg, pSgBuf->cbUsed, "--> Outgoing");
2565 if (pGso)
2566 {
2567 /* Some guests (RHEL) may report HdrLen excluding transport layer header!
2568 * Thus cannot use cdHdrs provided by the guest because of different ways
2569 * it gets filled out by different versions of kernels. */
2570 Log4Func(("%s HdrLen before adjustment %d.\n", pThis->szInst, pGso->cbHdrsTotal));
2571 switch (pGso->u8Type)
2572 {
2573 case PDMNETWORKGSOTYPE_IPV4_TCP:
2574 case PDMNETWORKGSOTYPE_IPV6_TCP:
2575 pGso->cbHdrsTotal = pPktHdr->uChksumStart +
2576 ((PRTNETTCP)(((uint8_t*)pSgBuf->aSegs[0].pvSeg) + pPktHdr->uChksumStart))->th_off * 4;
2577 AssertMsgReturn(pSgBuf->cbUsed > pGso->cbHdrsTotal,
2578 ("cbHdrsTotal exceeds size of frame"), VERR_BUFFER_OVERFLOW);
2579 pGso->cbHdrsSeg = pGso->cbHdrsTotal;
2580 break;
2581 case PDMNETWORKGSOTYPE_IPV4_UDP:
2582 pGso->cbHdrsTotal = (uint8_t)(pPktHdr->uChksumStart + sizeof(RTNETUDP));
2583 pGso->cbHdrsSeg = pPktHdr->uChksumStart;
2584 break;
2585 }
2586 /* Update GSO structure embedded into the frame */
2587 ((PPDMNETWORKGSO)pSgBuf->pvUser)->cbHdrsTotal = pGso->cbHdrsTotal;
2588 ((PPDMNETWORKGSO)pSgBuf->pvUser)->cbHdrsSeg = pGso->cbHdrsSeg;
2589 Log4Func(("%s adjusted HdrLen to %d.\n",
2590 pThis->szInst, pGso->cbHdrsTotal));
2591 Log2Func(("%s gso type=%x cbHdrsTotal=%u cbHdrsSeg=%u mss=%u off1=0x%x off2=0x%x\n",
2592 pThis->szInst, pGso->u8Type, pGso->cbHdrsTotal, pGso->cbHdrsSeg,
2593 pGso->cbMaxSeg, pGso->offHdr1, pGso->offHdr2));
2594 STAM_REL_COUNTER_INC(&pThis->StatTransmitGSO);
2595 }
2596 else if (pPktHdr->uFlags & VIRTIONET_HDR_F_NEEDS_CSUM)
2597 {
2598 STAM_REL_COUNTER_INC(&pThis->StatTransmitCSum);
2599 /*
2600 * This is not GSO frame but checksum offloading is requested.
2601 */
2602 virtioNetR3Calc16BitChecksum((uint8_t*)pSgBuf->aSegs[0].pvSeg, pSgBuf->cbUsed,
2603 pPktHdr->uChksumStart, pPktHdr->uChksumOffset);
2604 }
2605
2606 return pThisCC->pDrv->pfnSendBuf(pThisCC->pDrv, pSgBuf, true /* fOnWorkerThread */);
2607}
2608
2609/**
2610 * Non-reentrant function transmits all available packets from specified Tx virtq to downstream
2611 * PDM device (if cable is connected). For each Tx pkt, virtio-net pkt header is converted
2612 * to required GSO information (VBox host network stack semantics)
2613 *
2614 * @param pDevIns PDM device instance
2615 * @param pThis virtio-net device instance
2616 * @param pThisCC virtio-net device instance
2617 * @param pTxVirtq Address of transmit virtq
2618 * @param fOnWorkerThread Flag to PDM whether to use caller's or or PDM transmit worker's thread.
2619 */
2620static int virtioNetR3TransmitPkts(PPDMDEVINS pDevIns, PVIRTIONET pThis, PVIRTIONETCC pThisCC,
2621 PVIRTIONETVIRTQ pTxVirtq, bool fOnWorkerThread)
2622{
2623 PVIRTIOCORE pVirtio = &pThis->Virtio;
2624
2625
2626 if (!pThis->fVirtioReady)
2627 {
2628 LogFunc(("%s Ignoring Tx requests. VirtIO not ready (status=0x%x)\n",
2629 pThis->szInst, pThis->virtioNetConfig.uStatus));
2630 return VERR_IGNORED;
2631 }
2632
2633 if (!pThis->fCableConnected)
2634 {
2635 Log(("[%s] Ignoring transmit requests while cable is disconnected.\n", pThis->szInst));
2636 return VERR_IGNORED;
2637 }
2638
2639 /*
2640 * Only one thread is allowed to transmit at a time, others should skip transmission as the packets
2641 * will be picked up by the transmitting thread.
2642 */
2643 if (!ASMAtomicCmpXchgU32(&pThis->uIsTransmitting, 1, 0))
2644 return VERR_IGNORED;
2645
2646 PPDMINETWORKUP pDrv = pThisCC->pDrv;
2647 if (pDrv)
2648 {
2649 int rc = pDrv->pfnBeginXmit(pDrv, fOnWorkerThread);
2650 Assert(rc == VINF_SUCCESS || rc == VERR_TRY_AGAIN);
2651 if (rc == VERR_TRY_AGAIN)
2652 {
2653 ASMAtomicWriteU32(&pThis->uIsTransmitting, 0);
2654 return VERR_TRY_AGAIN;
2655 }
2656 }
2657 int cPkts = virtioCoreVirtqAvailBufCount(pVirtio->pDevInsR3, pVirtio, pTxVirtq->uIdx);
2658 if (!cPkts)
2659 {
2660 LogFunc(("[%s] No packets to send found on %s\n", pThis->szInst, pTxVirtq->szName));
2661
2662 if (pDrv)
2663 pDrv->pfnEndXmit(pDrv);
2664
2665 ASMAtomicWriteU32(&pThis->uIsTransmitting, 0);
2666 return VERR_MISSING;
2667 }
2668 LogFunc(("[%s] About to transmit %d pending packet%c\n", pThis->szInst, cPkts, cPkts == 1 ? ' ' : 's'));
2669
2670 virtioNetR3SetWriteLed(pThisCC, true);
2671
2672 int rc;
2673 PVIRTQBUF pVirtqBuf = NULL;
2674 while ((rc = virtioCoreR3VirtqAvailBufPeek(pVirtio->pDevInsR3, pVirtio, pTxVirtq->uIdx, &pVirtqBuf)) == VINF_SUCCESS)
2675 {
2676 Log10Func(("[%s] fetched descriptor chain from %s\n", pThis->szInst, pTxVirtq->szName));
2677
2678 PVIRTIOSGBUF pSgPhysSend = pVirtqBuf->pSgPhysSend;
2679 PVIRTIOSGSEG paSegsFromGuest = pSgPhysSend->paSegs;
2680 uint32_t cSegsFromGuest = pSgPhysSend->cSegs;
2681 size_t uFrameSize = 0;
2682
2683 AssertMsgReturn(paSegsFromGuest[0].cbSeg >= pThis->cbPktHdr,
2684 ("Desc chain's first seg has insufficient space for pkt header!\n"),
2685 VERR_INTERNAL_ERROR);
2686
2687 PVIRTIONETPKTHDR pPktHdr = (PVIRTIONETPKTHDR)RTMemAllocZ(pThis->cbPktHdr);
2688 AssertMsgReturn(pPktHdr, ("Out of Memory\n"), VERR_NO_MEMORY);
2689
2690 /* Compute total frame size from guest (including virtio-net pkt hdr) */
2691 for (unsigned i = 0; i < cSegsFromGuest && uFrameSize < VIRTIONET_MAX_FRAME_SIZE; i++)
2692 uFrameSize += paSegsFromGuest[i].cbSeg;
2693
2694 Log5Func(("[%s] complete frame is %u bytes.\n", pThis->szInst, uFrameSize));
2695 Assert(uFrameSize <= VIRTIONET_MAX_FRAME_SIZE);
2696
2697 /* Truncate oversized frames. */
2698 if (uFrameSize > VIRTIONET_MAX_FRAME_SIZE)
2699 uFrameSize = VIRTIONET_MAX_FRAME_SIZE;
2700
2701 if (pThisCC->pDrv)
2702 {
2703 uFrameSize -= pThis->cbPktHdr;
2704 /*
2705 * Peel off pkt header and convert to PDM/GSO semantics.
2706 */
2707 rc = virtioNetR3ReadVirtioTxPktHdr(pVirtio, pThis, pDevIns, paSegsFromGuest[0].GCPhys, pPktHdr, uFrameSize /* cbFrame */);
2708 if (RT_FAILURE(rc))
2709 return rc;
2710 virtioCoreGCPhysChainAdvance(pSgPhysSend, pThis->cbPktHdr);
2711
2712 PDMNETWORKGSO Gso, *pGso = virtioNetR3SetupGsoCtx(&Gso, pPktHdr);
2713
2714 /* Allocate PDM transmit buffer to send guest provided network frame from to VBox network leaf device */
2715 PPDMSCATTERGATHER pSgBufToPdmLeafDevice;
2716 rc = pThisCC->pDrv->pfnAllocBuf(pThisCC->pDrv, uFrameSize, pGso, &pSgBufToPdmLeafDevice);
2717
2718 /*
2719 * Copy virtio-net guest S/G buffer to PDM leaf driver S/G buffer
2720 * converting from GCphys to virt memory at the same time
2721 */
2722 if (RT_SUCCESS(rc))
2723 {
2724 STAM_REL_COUNTER_INC(&pThis->StatTransmitPackets);
2725 STAM_PROFILE_START(&pThis->StatTransmitSend, a);
2726
2727 size_t cbCopied = 0;
2728 size_t cbRemain = pSgBufToPdmLeafDevice->cbUsed = uFrameSize;
2729 uint64_t uOffset = 0;
2730 while (cbRemain)
2731 {
2732 PVIRTIOSGSEG paSeg = &pSgPhysSend->paSegs[pSgPhysSend->idxSeg];
2733 uint64_t srcSgStart = (uint64_t)paSeg->GCPhys;
2734 uint64_t srcSgLen = (uint64_t)paSeg->cbSeg;
2735 uint64_t srcSgCur = (uint64_t)pSgPhysSend->GCPhysCur;
2736 cbCopied = RT_MIN((uint64_t)cbRemain, srcSgLen - (srcSgCur - srcSgStart));
2737 virtioCoreGCPhysRead(pVirtio, pDevIns,
2738 (RTGCPHYS)pSgPhysSend->GCPhysCur,
2739 ((uint8_t *)pSgBufToPdmLeafDevice->aSegs[0].pvSeg) + uOffset, cbCopied);
2740 virtioCoreGCPhysChainAdvance(pSgPhysSend, cbCopied);
2741 cbRemain -= cbCopied;
2742 uOffset += cbCopied;
2743 }
2744
2745 LogFunc((".... Copied %lu/%lu bytes to %lu byte guest buffer. Buf residual=%lu\n",
2746 uOffset, uFrameSize, pVirtqBuf->cbPhysSend, virtioCoreGCPhysChainCalcLengthLeft(pSgPhysSend)));
2747
2748 rc = virtioNetR3TransmitFrame(pThis, pThisCC, pSgBufToPdmLeafDevice, pGso, pPktHdr);
2749 if (RT_FAILURE(rc))
2750 {
2751 LogFunc(("[%s] Failed to transmit frame, rc = %Rrc\n", pThis->szInst, rc));
2752 STAM_PROFILE_STOP(&pThis->StatTransmitSend, a);
2753 STAM_PROFILE_ADV_STOP(&pThis->StatTransmit, a);
2754 pThisCC->pDrv->pfnFreeBuf(pThisCC->pDrv, pSgBufToPdmLeafDevice);
2755 }
2756 STAM_PROFILE_STOP(&pThis->StatTransmitSend, a);
2757 STAM_REL_COUNTER_ADD(&pThis->StatTransmitBytes, uOffset);
2758 }
2759 else
2760 {
2761 Log4Func(("Failed to allocate S/G buffer: frame size=%u rc=%Rrc\n", uFrameSize, rc));
2762 /* Stop trying to fetch TX descriptors until we get more bandwidth. */
2763 virtioCoreR3VirtqBufRelease(pVirtio, pVirtqBuf);
2764 break;
2765 }
2766
2767 virtioCoreR3VirtqAvailBufNext(pVirtio, pTxVirtq->uIdx);
2768
2769 /* No data to return to guest, but necessary to put elem (e.g. desc chain head idx) on used ring */
2770 virtioCoreR3VirtqUsedBufPut(pVirtio->pDevInsR3, pVirtio, pTxVirtq->uIdx, NULL, pVirtqBuf, true /* fFence */);
2771 virtioCoreVirtqUsedRingSync(pVirtio->pDevInsR3, pVirtio, pTxVirtq->uIdx);
2772 }
2773
2774 virtioCoreR3VirtqBufRelease(pVirtio, pVirtqBuf);
2775 pVirtqBuf = NULL;
2776 }
2777 virtioNetR3SetWriteLed(pThisCC, false);
2778
2779 if (pDrv)
2780 pDrv->pfnEndXmit(pDrv);
2781
2782 ASMAtomicWriteU32(&pThis->uIsTransmitting, 0);
2783 return VINF_SUCCESS;
2784}
2785
2786/**
2787 * @interface_method_impl{PDMINETWORKDOWN,pfnXmitPending}
2788 */
2789static DECLCALLBACK(void) virtioNetR3NetworkDown_XmitPending(PPDMINETWORKDOWN pInterface)
2790{
2791 LogFunc(("\n"));
2792 PVIRTIONETCC pThisCC = RT_FROM_MEMBER(pInterface, VIRTIONETCC, INetworkDown);
2793 PPDMDEVINS pDevIns = pThisCC->pDevIns;
2794 PVIRTIONET pThis = PDMDEVINS_2_DATA(pThisCC->pDevIns, PVIRTIONET);
2795 PVIRTIONETVIRTQ pTxVirtq = &pThis->aVirtqs[TXQIDX(0)];
2796 STAM_COUNTER_INC(&pThis->StatTransmitByNetwork);
2797
2798 (void)virtioNetR3TransmitPkts(pDevIns, pThis, pThisCC, pTxVirtq, true /*fOnWorkerThread*/);
2799}
2800
2801/**
2802 * @callback_method_impl{FNTMTIMERDEV, Link Up Timer handler.}
2803 */
2804static DECLCALLBACK(void) virtioNetR3LinkUpTimer(PPDMDEVINS pDevIns, TMTIMERHANDLE hTimer, void *pvUser)
2805{
2806 PVIRTIONET pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIONET);
2807 PVIRTIONETCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIONETCC);
2808
2809 SET_LINK_UP(pThis);
2810 virtioNetWakeupRxBufWaiter(pDevIns);
2811
2812 if (pThisCC->pDrv)
2813 pThisCC->pDrv->pfnNotifyLinkChanged(pThisCC->pDrv, PDMNETWORKLINKSTATE_UP);
2814
2815 LogFunc(("[%s] Link is up\n", pThis->szInst));
2816 RT_NOREF(hTimer, pvUser);
2817}
2818
2819/**
2820 * Takes down the link temporarily if its current status is up.
2821 *
2822 * This is used during restore and when replumbing the network link.
2823 *
2824 * The temporary link outage is supposed to indicate to the OS that all network
2825 * connections have been lost and that it for instance is appropriate to
2826 * renegotiate any DHCP lease.
2827 *
2828 * @param pDevIns The device instance.
2829 * @param pThis The virtio-net shared instance data.
2830 * @param pThisCC The virtio-net ring-3 instance data.
2831 */
2832static void virtioNetR3TempLinkDown(PPDMDEVINS pDevIns, PVIRTIONET pThis, PVIRTIONETCC pThisCC)
2833{
2834 if (IS_LINK_UP(pThis))
2835 {
2836 SET_LINK_DOWN(pThis);
2837
2838 /* Re-establish link in 5 seconds. */
2839 int rc = PDMDevHlpTimerSetMillies(pDevIns, pThisCC->hLinkUpTimer, pThis->cMsLinkUpDelay);
2840 AssertRC(rc);
2841
2842 LogFunc(("[%s] Link is down temporarily\n", pThis->szInst));
2843 }
2844}
2845
2846/**
2847 * @interface_method_impl{PDMINETWORKCONFIG,pfnSetLinkState}
2848 */
2849static DECLCALLBACK(int) virtioNetR3NetworkConfig_SetLinkState(PPDMINETWORKCONFIG pInterface, PDMNETWORKLINKSTATE enmState)
2850{
2851 PVIRTIONETCC pThisCC = RT_FROM_MEMBER(pInterface, VIRTIONETCC, INetworkConfig);
2852 PPDMDEVINS pDevIns = pThisCC->pDevIns;
2853 PVIRTIONET pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIONET);
2854
2855 bool fRequestedLinkStateIsUp = (enmState == PDMNETWORKLINKSTATE_UP);
2856
2857#ifdef LOG_ENABLED
2858 if (LogIs7Enabled())
2859 {
2860 LogFunc(("[%s]", pThis->szInst));
2861 switch(enmState)
2862 {
2863 case PDMNETWORKLINKSTATE_UP:
2864 Log(("UP\n"));
2865 break;
2866 case PDMNETWORKLINKSTATE_DOWN:
2867 Log(("DOWN\n"));
2868 break;
2869 case PDMNETWORKLINKSTATE_DOWN_RESUME:
2870 Log(("DOWN (RESUME)\n"));
2871 break;
2872 default:
2873 Log(("UNKNOWN)\n"));
2874 }
2875 }
2876#endif
2877
2878 if (enmState == PDMNETWORKLINKSTATE_DOWN_RESUME)
2879 {
2880 if (IS_LINK_UP(pThis))
2881 {
2882 /*
2883 * We bother to bring the link down only if it was up previously. The UP link state
2884 * notification will be sent when the link actually goes up in virtioNetR3LinkUpTimer().
2885 */
2886 virtioNetR3TempLinkDown(pDevIns, pThis, pThisCC);
2887 if (pThisCC->pDrv)
2888 pThisCC->pDrv->pfnNotifyLinkChanged(pThisCC->pDrv, enmState);
2889 }
2890 }
2891 else if (fRequestedLinkStateIsUp != IS_LINK_UP(pThis))
2892 {
2893 if (fRequestedLinkStateIsUp)
2894 {
2895 Log(("[%s] Link is up\n", pThis->szInst));
2896 pThis->fCableConnected = true;
2897 SET_LINK_UP(pThis);
2898 virtioCoreNotifyConfigChanged(&pThis->Virtio);
2899 }
2900 else /* Link requested to be brought down */
2901 {
2902 /* The link was brought down explicitly, make sure it won't come up by timer. */
2903 PDMDevHlpTimerStop(pDevIns, pThisCC->hLinkUpTimer);
2904 Log(("[%s] Link is down\n", pThis->szInst));
2905 pThis->fCableConnected = false;
2906 SET_LINK_DOWN(pThis);
2907 virtioCoreNotifyConfigChanged(&pThis->Virtio);
2908 }
2909 if (pThisCC->pDrv)
2910 pThisCC->pDrv->pfnNotifyLinkChanged(pThisCC->pDrv, enmState);
2911 }
2912 return VINF_SUCCESS;
2913}
2914/**
2915 * @interface_method_impl{PDMINETWORKCONFIG,pfnGetLinkState}
2916 */
2917static DECLCALLBACK(PDMNETWORKLINKSTATE) virtioNetR3NetworkConfig_GetLinkState(PPDMINETWORKCONFIG pInterface)
2918{
2919 PVIRTIONETCC pThisCC = RT_FROM_MEMBER(pInterface, VIRTIONETCC, INetworkConfig);
2920 PVIRTIONET pThis = PDMDEVINS_2_DATA(pThisCC->pDevIns, PVIRTIONET);
2921
2922 return IS_LINK_UP(pThis) ? PDMNETWORKLINKSTATE_UP : PDMNETWORKLINKSTATE_DOWN;
2923}
2924
2925static int virtioNetR3DestroyWorkerThreads(PPDMDEVINS pDevIns, PVIRTIONET pThis, PVIRTIONETCC pThisCC)
2926{
2927 Log10Func(("[%s]\n", pThis->szInst));
2928 int rc = VINF_SUCCESS;
2929 for (unsigned uIdxWorker = 0; uIdxWorker < pThis->cWorkers; uIdxWorker++)
2930 {
2931 PVIRTIONETWORKER pWorker = &pThis->aWorkers[uIdxWorker];
2932 PVIRTIONETWORKERR3 pWorkerR3 = &pThisCC->aWorkers[uIdxWorker];
2933
2934 if (pWorker->hEvtProcess != NIL_SUPSEMEVENT)
2935 {
2936 PDMDevHlpSUPSemEventClose(pDevIns, pWorker->hEvtProcess);
2937 pWorker->hEvtProcess = NIL_SUPSEMEVENT;
2938 }
2939 if (pWorkerR3->pThread)
2940 {
2941 int rcThread;
2942 rc = PDMDevHlpThreadDestroy(pDevIns, pWorkerR3->pThread, &rcThread);
2943 if (RT_FAILURE(rc) || RT_FAILURE(rcThread))
2944 AssertMsgFailed(("%s Failed to destroythread rc=%Rrc rcThread=%Rrc\n", __FUNCTION__, rc, rcThread));
2945 pWorkerR3->pThread = NULL;
2946 }
2947 }
2948 return rc;
2949}
2950
2951/**
2952 * Creates a worker for specified queue, along with semaphore to throttle the worker.
2953 *
2954 * @param pDevIns - PDM device instance
2955 * @param pThis - virtio-net instance
2956 * @param pWorker - Pointer to worker state
2957 * @param pWorkerR3 - Pointer to worker state
2958 * @param pVirtq - Pointer to virtq
2959 */
2960static int virtioNetR3CreateOneWorkerThread(PPDMDEVINS pDevIns, PVIRTIONET pThis,
2961 PVIRTIONETWORKER pWorker, PVIRTIONETWORKERR3 pWorkerR3,
2962 PVIRTIONETVIRTQ pVirtq)
2963{
2964 Log10Func(("[%s]\n", pThis->szInst));
2965 RT_NOREF(pThis);
2966
2967 int rc = PDMDevHlpSUPSemEventCreate(pDevIns, &pWorker->hEvtProcess);
2968
2969 if (RT_FAILURE(rc))
2970 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
2971 N_("DevVirtioNET: Failed to create SUP event semaphore"));
2972
2973 LogFunc(("creating thread for queue %s\n", pVirtq->szName));
2974
2975 rc = PDMDevHlpThreadCreate(pDevIns, &pWorkerR3->pThread,
2976 (void *)pWorker, virtioNetR3WorkerThread,
2977 virtioNetR3WakeupWorker, 0, RTTHREADTYPE_IO, pVirtq->szName);
2978 if (RT_FAILURE(rc))
2979 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
2980 N_("Error creating thread for Virtual Virtq %s\n"), pVirtq->uIdx);
2981
2982 pWorker->fAssigned = true; /* Because worker's state in fixed-size array initialized w/empty slots */
2983
2984 LogFunc(("%s pThread: %p\n", pVirtq->szName, pWorkerR3->pThread));
2985
2986 return rc;
2987}
2988
2989static int virtioNetR3CreateWorkerThreads(PPDMDEVINS pDevIns, PVIRTIONET pThis, PVIRTIONETCC pThisCC)
2990{
2991 Log10Func(("[%s]\n", pThis->szInst));
2992 int rc;
2993
2994 /* Create the Control Queue worker anyway whether or not it is feature-negotiated or utilized by the guest.
2995 * See related comment for queue construction in the device constructor function for more context.
2996 */
2997
2998 PVIRTIONETVIRTQ pCtlVirtq = &pThis->aVirtqs[CTRLQIDX];
2999 rc = virtioNetR3CreateOneWorkerThread(pDevIns, pThis,
3000 &pThis->aWorkers[CTRLQIDX], &pThisCC->aWorkers[CTRLQIDX], pCtlVirtq);
3001 AssertRCReturn(rc, rc);
3002
3003 pCtlVirtq->fHasWorker = true;
3004
3005 for (uint16_t uVirtqPair = pThis->cInitializedVirtqPairs; uVirtqPair < pThis->cVirtqPairs; uVirtqPair++)
3006 {
3007 PVIRTIONETVIRTQ pTxVirtq = &pThis->aVirtqs[TXQIDX(uVirtqPair)];
3008 PVIRTIONETVIRTQ pRxVirtq = &pThis->aVirtqs[RXQIDX(uVirtqPair)];
3009
3010 rc = virtioNetR3CreateOneWorkerThread(pDevIns, pThis, &pThis->aWorkers[TXQIDX(uVirtqPair)],
3011 &pThisCC->aWorkers[TXQIDX(uVirtqPair)], pTxVirtq);
3012 AssertRCReturn(rc, rc);
3013
3014 pTxVirtq->fHasWorker = true;
3015 pRxVirtq->fHasWorker = false;
3016 }
3017
3018 if (pThis->cVirtqPairs > pThis->cInitializedVirtqPairs)
3019 pThis->cInitializedVirtqPairs = pThis->cVirtqPairs;
3020
3021 pThis->cWorkers = pThis->cVirtqPairs + 1 /* One control virtq */;
3022
3023 return rc;
3024}
3025
3026static void virtioNetConfigurePktHdr(PVIRTIONET pThis, uint32_t fLegacy)
3027{
3028 /* Calculate network packet header type and size based on what we know now */
3029 pThis->cbPktHdr = sizeof(VIRTIONETPKTHDR);
3030 if (!fLegacy)
3031 /* Modern (e.g. >= VirtIO 1.0) device specification's pkt size rules */
3032 if (FEATURE_ENABLED(MRG_RXBUF))
3033 pThis->ePktHdrType = kVirtioNetModernPktHdrWithMrgRx;
3034 else /* Modern guest driver with MRG_RX feature disabled */
3035 pThis->ePktHdrType = kVirtioNetModernPktHdrWithoutMrgRx;
3036 else
3037 {
3038 /* Legacy (e.g. < VirtIO 1.0) device specification's pkt size rules */
3039 if (FEATURE_ENABLED(MRG_RXBUF))
3040 pThis->ePktHdrType = kVirtioNetLegacyPktHdrWithMrgRx;
3041 else /* Legacy guest with MRG_RX feature disabled */
3042 {
3043 pThis->ePktHdrType = kVirtioNetLegacyPktHdrWithoutMrgRx;
3044 pThis->cbPktHdr -= RT_SIZEOFMEMB(VIRTIONETPKTHDR, uNumBuffers);
3045 }
3046 }
3047}
3048
3049/**
3050 * @callback_method_impl{FNPDMTHREADDEV}
3051 */
3052static DECLCALLBACK(int) virtioNetR3WorkerThread(PPDMDEVINS pDevIns, PPDMTHREAD pThread)
3053{
3054 PVIRTIONET pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIONET);
3055 PVIRTIONETCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIONETCC);
3056 PVIRTIONETWORKER pWorker = (PVIRTIONETWORKER)pThread->pvUser;
3057 PVIRTIONETVIRTQ pVirtq = &pThis->aVirtqs[pWorker->uIdx];
3058 uint16_t uIdx = pWorker->uIdx;
3059
3060 ASMAtomicWriteBool(&pWorker->fSleeping, false);
3061
3062 Assert(pWorker->uIdx == pVirtq->uIdx);
3063
3064 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
3065 return VINF_SUCCESS;
3066
3067 LogFunc(("[%s] worker thread idx=%d started for %s (virtq idx=%d)\n", pThis->szInst, pWorker->uIdx, pVirtq->szName, pVirtq->uIdx));
3068
3069 /** @todo Race w/guest enabling/disabling guest notifications cyclically.
3070 See BugRef #8651, Comment #82 */
3071 virtioCoreVirtqEnableNotify(&pThis->Virtio, uIdx, true /* fEnable */);
3072
3073 while ( pThread->enmState != PDMTHREADSTATE_TERMINATING
3074 && pThread->enmState != PDMTHREADSTATE_TERMINATED)
3075 {
3076 if (IS_VIRTQ_EMPTY(pDevIns, &pThis->Virtio, pVirtq->uIdx))
3077 {
3078 /* Precisely coordinated atomic interlocks avoid a race condition that results in hung thread
3079 * wherein a sloppily coordinated wake-up notification during a transition into or out
3080 * of sleep leaves notifier and target mutually confused about actual & intended state.
3081 */
3082 ASMAtomicWriteBool(&pWorker->fSleeping, true);
3083 bool fNotificationSent = ASMAtomicXchgBool(&pWorker->fNotified, false);
3084 if (!fNotificationSent)
3085 {
3086 Log10Func(("[%s] %s worker sleeping...\n\n", pThis->szInst, pVirtq->szName));
3087 Assert(ASMAtomicReadBool(&pWorker->fSleeping));
3088
3089 int rc = PDMDevHlpSUPSemEventWaitNoResume(pDevIns, pWorker->hEvtProcess, RT_INDEFINITE_WAIT);
3090 STAM_COUNTER_INC(&pThis->StatTransmitByThread);
3091 AssertLogRelMsgReturn(RT_SUCCESS(rc) || rc == VERR_INTERRUPTED, ("%Rrc\n", rc), rc);
3092 if (RT_UNLIKELY(pThread->enmState != PDMTHREADSTATE_RUNNING))
3093 return VINF_SUCCESS;
3094 if (rc == VERR_INTERRUPTED)
3095 continue;
3096 ASMAtomicWriteBool(&pWorker->fNotified, false);
3097 }
3098 ASMAtomicWriteBool(&pWorker->fSleeping, false);
3099 }
3100 /*
3101 * Dispatch to the handler for the queue this worker is set up to drive
3102 */
3103 if (pVirtq->fCtlVirtq)
3104 {
3105 Log10Func(("[%s] %s worker woken. Fetching desc chain\n", pThis->szInst, pVirtq->szName));
3106 PVIRTQBUF pVirtqBuf = NULL;
3107 int rc = virtioCoreR3VirtqAvailBufGet(pDevIns, &pThis->Virtio, pVirtq->uIdx, &pVirtqBuf, true);
3108 if (rc == VERR_NOT_AVAILABLE)
3109 {
3110 Log10Func(("[%s] %s worker woken. Nothing found in queue\n", pThis->szInst, pVirtq->szName));
3111 continue;
3112 }
3113 virtioNetR3Ctrl(pDevIns, pThis, pThisCC, pVirtqBuf);
3114 virtioCoreR3VirtqBufRelease(&pThis->Virtio, pVirtqBuf);
3115 }
3116 else /* Must be Tx queue */
3117 {
3118 Log10Func(("[%s] %s worker woken. Virtq has data to transmit\n", pThis->szInst, pVirtq->szName));
3119 virtioNetR3TransmitPkts(pDevIns, pThis, pThisCC, pVirtq, false /* fOnWorkerThread */);
3120 }
3121 /* Note: Surprise! Rx queues aren't handled by local worker threads. Instead, the PDM network leaf driver
3122 * invokes PDMINETWORKDOWN.pfnWaitReceiveAvail() callback, which waits until woken by virtioNetVirtqNotified()
3123 * indicating that guest IN buffers have been added to Rx virt queue.
3124 */
3125 }
3126 Log10(("[%s] %s worker thread exiting\n", pThis->szInst, pVirtq->szName));
3127 return VINF_SUCCESS;
3128}
3129
3130/**
3131 * @callback_method_impl{VIRTIOCORER3,pfnStatusChanged}
3132 *
3133 * Called back by the core code when VirtIO's ready state has changed.
3134 */
3135static DECLCALLBACK(void) virtioNetR3StatusChg(PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC, uint32_t fVirtioReady)
3136{
3137 PVIRTIONET pThis = RT_FROM_MEMBER(pVirtio, VIRTIONET, Virtio);
3138 PVIRTIONETCC pThisCC = RT_FROM_MEMBER(pVirtioCC, VIRTIONETCC, Virtio);
3139
3140 pThis->fVirtioReady = fVirtioReady;
3141
3142 if (fVirtioReady)
3143 {
3144#ifdef LOG_ENABLED
3145 Log(("\n%-23s: %s *** VirtIO Ready ***\n\n", __FUNCTION__, pThis->szInst));
3146 virtioCorePrintDeviceFeatures(&pThis->Virtio, NULL, s_aDevSpecificFeatures, RT_ELEMENTS(s_aDevSpecificFeatures));
3147#endif
3148 pThis->fResetting = false;
3149 pThis->fNegotiatedFeatures = virtioCoreGetNegotiatedFeatures(pVirtio);
3150 pThis->virtioNetConfig.uStatus = pThis->fCableConnected ? VIRTIONET_F_LINK_UP : 0;
3151
3152 for (unsigned uVirtqNbr = 0; uVirtqNbr < pThis->cVirtqs; uVirtqNbr++)
3153 {
3154 PVIRTIONETVIRTQ pVirtq = &pThis->aVirtqs[uVirtqNbr];
3155 PVIRTIONETWORKER pWorker = &pThis->aWorkers[uVirtqNbr];
3156
3157 Assert(pWorker->uIdx == uVirtqNbr);
3158 RT_NOREF(pWorker);
3159
3160 Assert(pVirtq->uIdx == pWorker->uIdx);
3161
3162 (void) virtioCoreR3VirtqAttach(&pThis->Virtio, pVirtq->uIdx, pVirtq->szName);
3163 pVirtq->fAttachedToVirtioCore = true;
3164 if (IS_VIRTQ_EMPTY(pThisCC->pDevIns, &pThis->Virtio, pVirtq->uIdx))
3165 virtioCoreVirtqEnableNotify(&pThis->Virtio, pVirtq->uIdx, true /* fEnable */);
3166 }
3167 }
3168 else
3169 {
3170 Log(("\n%-23s: %s VirtIO is resetting ***\n", __FUNCTION__, pThis->szInst));
3171
3172 pThis->virtioNetConfig.uStatus = pThis->fCableConnected ? VIRTIONET_F_LINK_UP : 0;
3173 Log7(("%-23s: %s Link is %s\n", __FUNCTION__, pThis->szInst, pThis->fCableConnected ? "up" : "down"));
3174
3175 pThis->fPromiscuous = true;
3176 pThis->fAllMulticast = false;
3177 pThis->fAllUnicast = false;
3178 pThis->fNoMulticast = false;
3179 pThis->fNoUnicast = false;
3180 pThis->fNoBroadcast = false;
3181 pThis->uIsTransmitting = 0;
3182 pThis->cUnicastFilterMacs = 0;
3183 pThis->cMulticastFilterMacs = 0;
3184
3185 memset(pThis->aMacMulticastFilter, 0, sizeof(pThis->aMacMulticastFilter));
3186 memset(pThis->aMacUnicastFilter, 0, sizeof(pThis->aMacUnicastFilter));
3187 memset(pThis->aVlanFilter, 0, sizeof(pThis->aVlanFilter));
3188
3189 pThisCC->pDrv->pfnSetPromiscuousMode(pThisCC->pDrv, true);
3190
3191 for (uint16_t uVirtqNbr = 0; uVirtqNbr < pThis->cVirtqs; uVirtqNbr++)
3192 {
3193 virtioCoreR3VirtqDetach(&pThis->Virtio, uVirtqNbr);
3194 pThis->aVirtqs[uVirtqNbr].fAttachedToVirtioCore = false;
3195 }
3196 }
3197}
3198
3199/**
3200 * @callback_method_impl{VIRTIOCORER3,pfnFeatureNegotiationComplete}
3201 */
3202static DECLCALLBACK(void) pfnFeatureNegotiationComplete(PVIRTIOCORE pVirtio, uint64_t fDriverFeatures, uint32_t fLegacy)
3203{
3204 PVIRTIONET pThis = PDMDEVINS_2_DATA(pVirtio->pDevInsR3, PVIRTIONET);
3205
3206 LogFunc(("[Feature Negotiation Complete] Guest Driver version is: %s\n", fLegacy ? "legacy" : "modern"));
3207 virtioNetConfigurePktHdr(pThis, fLegacy);
3208 virtioNetR3SetVirtqNames(pThis, fLegacy);
3209
3210 /* Senseless for modern guest to use control queue in this case. (See Note 1 in PDM-invoked device constructor) */
3211 if (!fLegacy && !(fDriverFeatures & VIRTIONET_F_CTRL_VQ))
3212 virtioNetR3VirtqDestroy(pVirtio, &pThis->aVirtqs[CTRLQIDX]);
3213}
3214
3215#endif /* IN_RING3 */
3216
3217/**
3218 * @interface_method_impl{PDMDEVREGR3,pfnDetach}
3219 *
3220 * The VM is suspended at this point.
3221 */
3222static DECLCALLBACK(void) virtioNetR3Detach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
3223{
3224 RT_NOREF(fFlags);
3225
3226 PVIRTIONET pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIONET);
3227 PVIRTIONETCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIONETCC);
3228
3229 Log7Func(("[%s]\n", pThis->szInst));
3230 RT_NOREF(pThis);
3231
3232 AssertLogRelReturnVoid(iLUN == 0);
3233
3234 pThisCC->pDrvBase = NULL;
3235 pThisCC->pDrv = NULL;
3236}
3237
3238/**
3239 * @interface_method_impl{PDMDEVREGR3,pfnAttach}
3240 *
3241 * This is called when we change block driver.
3242 */
3243static DECLCALLBACK(int) virtioNetR3Attach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
3244{
3245 PVIRTIONET pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIONET);
3246 PVIRTIONETCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIONETCC);
3247
3248 Log7Func(("[%s]", pThis->szInst));
3249 AssertLogRelReturn(iLUN == 0, VERR_PDM_NO_SUCH_LUN);
3250
3251 int rc = PDMDevHlpDriverAttach(pDevIns, 0, &pThisCC->IBase, &pThisCC->pDrvBase, "Network Port");
3252 if (RT_SUCCESS(rc))
3253 {
3254 pThisCC->pDrv = PDMIBASE_QUERY_INTERFACE(pThisCC->pDrvBase, PDMINETWORKUP);
3255 AssertMsgStmt(pThisCC->pDrv, ("Failed to obtain the PDMINETWORKUP interface!\n"),
3256 rc = VERR_PDM_MISSING_INTERFACE_BELOW);
3257 }
3258 else if ( rc == VERR_PDM_NO_ATTACHED_DRIVER
3259 || rc == VERR_PDM_CFG_MISSING_DRIVER_NAME)
3260 Log(("[%s] No attached driver!\n", pThis->szInst));
3261
3262 RT_NOREF2(pThis, fFlags);
3263 return rc;
3264}
3265
3266/**
3267 * @interface_method_impl{PDMILEDPORTS,pfnQueryStatusLed}
3268 */
3269static DECLCALLBACK(int) virtioNetR3QueryStatusLed(PPDMILEDPORTS pInterface, unsigned iLUN, PPDMLED *ppLed)
3270{
3271 PVIRTIONETR3 pThisR3 = RT_FROM_MEMBER(pInterface, VIRTIONETR3, ILeds);
3272 if (iLUN)
3273 return VERR_PDM_LUN_NOT_FOUND;
3274 *ppLed = &pThisR3->led;
3275 return VINF_SUCCESS;
3276}
3277
3278/**
3279 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
3280 */
3281static DECLCALLBACK(void *) virtioNetR3QueryInterface(struct PDMIBASE *pInterface, const char *pszIID)
3282{
3283 PVIRTIONETR3 pThisCC = RT_FROM_MEMBER(pInterface, VIRTIONETCC, IBase);
3284 PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKDOWN, &pThisCC->INetworkDown);
3285 PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKCONFIG, &pThisCC->INetworkConfig);
3286 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThisCC->IBase);
3287 PDMIBASE_RETURN_INTERFACE(pszIID, PDMILEDPORTS, &pThisCC->ILeds);
3288 return NULL;
3289}
3290
3291/**
3292 * @interface_method_impl{PDMDEVREGR3,pfnDestruct}
3293 */
3294static DECLCALLBACK(int) virtioNetR3Destruct(PPDMDEVINS pDevIns)
3295{
3296 PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
3297
3298 PVIRTIONET pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIONET);
3299 PVIRTIONETCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIONETCC);
3300
3301 Log(("[%s] Destroying instance\n", pThis->szInst));
3302 if (pThis->hEventRxDescAvail != NIL_SUPSEMEVENT)
3303 {
3304 PDMDevHlpSUPSemEventSignal(pDevIns, pThis->hEventRxDescAvail);
3305 PDMDevHlpSUPSemEventClose(pDevIns, pThis->hEventRxDescAvail);
3306 pThis->hEventRxDescAvail = NIL_SUPSEMEVENT;
3307 }
3308
3309 virtioNetR3DestroyWorkerThreads(pDevIns, pThis, pThisCC);
3310 virtioCoreR3Term(pDevIns, &pThis->Virtio, &pThisCC->Virtio);
3311 return VINF_SUCCESS;
3312}
3313
3314/**
3315 * @interface_method_impl{PDMDEVREGR3,pfnConstruct}
3316 *
3317 * Notes about revising originally VirtIO 1.0+ only virtio-net device emulator to be "transitional",
3318 * a VirtIO term meaning this now interoperates with both "legacy" (e.g. pre-1.0) and "modern" (1.0+)
3319 * guest virtio-net drivers. The changes include migrating VMs saved using prior DevVirtioNet.cpp (0.95)
3320 * saveExec/loadExec semantics to use 1.0 save/load semantics.
3321 *
3322 * Regardless of the 1.0 spec's overall helpful guidance for implementing transitional devices,
3323 * A bit is left to the imagination, e.g. some things have to be determined deductively
3324 * (AKA "the hard way").
3325 *
3326 * Case in point: According to VirtIO 0.95 ("legacy") specification, section 2.2.1, "historically"
3327 * drivers may start driving prior to feature negotiation and prior to drivers setting DRIVER_OK
3328 * status, "provided driver doesn't use features that alter early use of this device". Interpreted
3329 * here to mean a virtio-net driver must respect default settings (such as implicit pkt header default
3330 * size, as determined per Note 1 below).
3331 *
3332 * ----------------------------------------------------------------------------------------------
3333 * Transitional device initialization Note 1: Identifying default value for network Rx pkt hdr size.
3334 * (VirtIO 1.0 specification section 5.1.6.1)
3335 *
3336 * Guest virtio legacy drivers may begin operations prematurely, regardless of early spec's
3337 * initialization sequence (see note 2 below). Legacy drivers implicitly default to using the
3338 * (historically) shortest-length network packet header *unless* VIRTIONET_F_MRG_RXBUF feature is
3339 * negotiated. If feature negotiation phase is [optionally] enacted by a legacy guest (i.e. we strictly
3340 * enforce full initialization protocol for modern guests), virtioNetConfigurePktHdr() is invoked again to
3341 * finalize device's network packet header size. Best-guess at default packet header size is deduced, e.g.
3342 * isn't documented, as follows: A legacy guest with VIRTIONET_F_MRG_RXBUF not-yet-negotiated is the only
3343 * case where network I/O could possibly occur with any reasonable assumption about packet type/size,
3344 * because logically other permutations couldn't possibly be inferred until feature negotiation
3345 * is complete. Specifically, those cases are:
3346 *
3347 * 1. A modern driver (detected only when VIRTIONET_F_VERSION_1 feature is ack'd by guest, and,
3348 * simultaneously, VIRTIONET_F_MRG_RXBUF feature is accepted or declined (determining network receive-packet
3349 * processing behavior).
3350 *
3351 * 2. A legacy driver that has agreed to use VIRTIONET_F_MRG_RXBUF feature, resulting in a two-byte larger pkt hdr,
3352 * (as well as deciding Rx packet processing behavior).
3353 *
3354 * ----------------------------------------------------------------------------------------------
3355 * Transitional device initialization Note 2: Creating unnegotiated control queue.
3356 * (VirtIO 1.0 spec, sections 5.1.5 and 5.1.6.5)
3357 *
3358 * Create all queues immediately, prior to feature negotiation, including control queue (irrespective
3359 * of the fact it's too early in initialization for control feature to be approved by guest). This
3360 * transitional device must deal with legacy guests which *can* (and on linux have been seen to) use
3361 * the control queue prior to feature negotiation.
3362 *
3363 * The initial assumption is *modern" guest virtio-net drivers out in the wild could never reasonably
3364 * attempt something as obviously risky as using ctrlq without first acking VIRTIO_NET_F_CTRL_VQ
3365 * feature to establish it. For now, we create the control queue proactively to accomodate a potentially
3366 * badly behaved but officially sanctioned legacy virtio-net driver, but *destroy* that same queue
3367 * if a driver announces as 'modern' during feature finalization yet leaves VIRTIO_NET_F_CTRL_VQ un-ack'd.
3368 */
3369static DECLCALLBACK(int) virtioNetR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
3370{
3371 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
3372 PVIRTIONET pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIONET);
3373 PVIRTIONETCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIONETCC);
3374 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
3375
3376 /*
3377 * Quickly initialize state data to ensure destructor always works.
3378 */
3379 Log7Func(("PDM device instance: %d\n", iInstance));
3380 RTStrPrintf(pThis->szInst, sizeof(pThis->szInst), "virtio-net #%d", iInstance);
3381
3382 pThisCC->pDevIns = pDevIns;
3383 pThisCC->IBase.pfnQueryInterface = virtioNetR3QueryInterface;
3384 pThisCC->ILeds.pfnQueryStatusLed = virtioNetR3QueryStatusLed;
3385 pThisCC->led.u32Magic = PDMLED_MAGIC;
3386
3387 /* Interfaces */
3388 pThisCC->INetworkDown.pfnWaitReceiveAvail = virtioNetR3NetworkDown_WaitReceiveAvail;
3389 pThisCC->INetworkDown.pfnReceive = virtioNetR3NetworkDown_Receive;
3390 pThisCC->INetworkDown.pfnReceiveGso = virtioNetR3NetworkDown_ReceiveGso;
3391 pThisCC->INetworkDown.pfnXmitPending = virtioNetR3NetworkDown_XmitPending;
3392 pThisCC->INetworkConfig.pfnGetMac = virtioNetR3NetworkConfig_GetMac;
3393 pThisCC->INetworkConfig.pfnGetLinkState = virtioNetR3NetworkConfig_GetLinkState;
3394 pThisCC->INetworkConfig.pfnSetLinkState = virtioNetR3NetworkConfig_SetLinkState;
3395
3396 pThis->hEventRxDescAvail = NIL_SUPSEMEVENT;
3397
3398 /*
3399 * Validate configuration.
3400 */
3401 PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns, "MAC|CableConnected|LineSpeed|LinkUpDelay|StatNo|Legacy", "");
3402
3403 /* Get config params */
3404 int rc = pHlp->pfnCFGMQueryBytes(pCfg, "MAC", pThis->macConfigured.au8, sizeof(pThis->macConfigured));
3405 if (RT_FAILURE(rc))
3406 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to get MAC address"));
3407
3408 rc = pHlp->pfnCFGMQueryBool(pCfg, "CableConnected", &pThis->fCableConnected);
3409 if (RT_FAILURE(rc))
3410 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to get the value of 'CableConnected'"));
3411
3412 uint32_t uStatNo = iInstance;
3413 rc = pHlp->pfnCFGMQueryU32Def(pCfg, "StatNo", &uStatNo, iInstance);
3414 if (RT_FAILURE(rc))
3415 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to get the \"StatNo\" value"));
3416
3417 rc = pHlp->pfnCFGMQueryU32Def(pCfg, "LinkUpDelay", &pThis->cMsLinkUpDelay, 5000); /* ms */
3418 if (RT_FAILURE(rc))
3419 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to get the value of 'LinkUpDelay'"));
3420
3421 Assert(pThis->cMsLinkUpDelay <= 300000); /* less than 5 minutes */
3422
3423 if (pThis->cMsLinkUpDelay > 5000 || pThis->cMsLinkUpDelay < 100)
3424 LogRel(("%s WARNING! Link up delay is set to %u seconds!\n",
3425 pThis->szInst, pThis->cMsLinkUpDelay / 1000));
3426
3427 Log(("[%s] Link up delay is set to %u seconds\n", pThis->szInst, pThis->cMsLinkUpDelay / 1000));
3428
3429 /* Copy the MAC address configured for the VM to the MMIO accessible Virtio dev-specific config area */
3430 memcpy(pThis->virtioNetConfig.uMacAddress.au8, pThis->macConfigured.au8, sizeof(pThis->virtioNetConfig.uMacAddress)); /* TBD */
3431
3432 Log(("Using MAC address for %s: %2x:%2x:%2x:%2x:%2x:%2x\n", pThis->szInst,
3433 pThis->macConfigured.au8[0], pThis->macConfigured.au8[1], pThis->macConfigured.au8[2],
3434 pThis->macConfigured.au8[3], pThis->macConfigured.au8[4], pThis->macConfigured.au8[5]));
3435
3436 LogFunc(("RC=%RTbool R0=%RTbool\n", pDevIns->fRCEnabled, pDevIns->fR0Enabled));
3437
3438 /*
3439 * Configure Virtio core (generic Virtio queue and infrastructure management) parameters.
3440 */
3441# if FEATURE_OFFERED(STATUS)
3442 pThis->virtioNetConfig.uStatus = 0;
3443# endif
3444
3445 pThis->virtioNetConfig.uMaxVirtqPairs = VIRTIONET_MAX_QPAIRS;
3446 pThisCC->Virtio.pfnFeatureNegotiationComplete = pfnFeatureNegotiationComplete;
3447 pThisCC->Virtio.pfnVirtqNotified = virtioNetVirtqNotified;
3448 pThisCC->Virtio.pfnStatusChanged = virtioNetR3StatusChg;
3449 pThisCC->Virtio.pfnDevCapRead = virtioNetR3DevCapRead;
3450 pThisCC->Virtio.pfnDevCapWrite = virtioNetR3DevCapWrite;
3451
3452 VIRTIOPCIPARAMS VirtioPciParams;
3453 VirtioPciParams.uDeviceId = PCI_DEVICE_ID_VIRTIONET_HOST;
3454 VirtioPciParams.uClassBase = PCI_CLASS_BASE_NETWORK_CONTROLLER;
3455 VirtioPciParams.uClassSub = PCI_CLASS_SUB_NET_ETHERNET_CONTROLLER;
3456 VirtioPciParams.uClassProg = PCI_CLASS_PROG_UNSPECIFIED;
3457 VirtioPciParams.uSubsystemId = PCI_DEVICE_ID_VIRTIONET_HOST; /* VirtIO 1.0 allows PCI Device ID here */
3458 VirtioPciParams.uInterruptLine = 0x00;
3459 VirtioPciParams.uInterruptPin = 0x01;
3460
3461 /* Create semaphore used to synchronize/throttle the downstream LUN's Rx waiter thread. */
3462 rc = PDMDevHlpSUPSemEventCreate(pDevIns, &pThis->hEventRxDescAvail);
3463 if (RT_FAILURE(rc))
3464 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Failed to create event semaphore"));
3465
3466 pThis->fOfferLegacy = VIRTIONET_TRANSITIONAL_ENABLE_FLAG;
3467 virtioNetConfigurePktHdr(pThis, pThis->fOfferLegacy); /* set defaults */
3468
3469 /* Initialize VirtIO core. (*pfnStatusChanged)() callback occurs when both host VirtIO core & guest driver are ready) */
3470 rc = virtioCoreR3Init(pDevIns, &pThis->Virtio, &pThisCC->Virtio, &VirtioPciParams, pThis->szInst,
3471 VIRTIONET_HOST_FEATURES_OFFERED, pThis->fOfferLegacy,
3472 &pThis->virtioNetConfig /*pvDevSpecificCap*/, sizeof(pThis->virtioNetConfig));
3473 if (RT_FAILURE(rc))
3474 return PDMDEV_SET_ERROR(pDevIns, rc, N_("virtio-net: failed to initialize VirtIO"));
3475
3476 pThis->fNegotiatedFeatures = virtioCoreGetNegotiatedFeatures(&pThis->Virtio);
3477 if (!virtioNetValidateRequiredFeatures(pThis->fNegotiatedFeatures))
3478 return PDMDEV_SET_ERROR(pDevIns, rc, N_("virtio-net: Required features not successfully negotiated."));
3479 pThis->cVirtqPairs = pThis->virtioNetConfig.uMaxVirtqPairs;
3480 pThis->cVirtqs += pThis->cVirtqPairs * 2 + 1;
3481 pThis->aVirtqs[CTRLQIDX].fCtlVirtq = true;
3482
3483 virtioNetR3SetVirtqNames(pThis, pThis->fOfferLegacy);
3484 for (unsigned uVirtqNbr = 0; uVirtqNbr < pThis->cVirtqs; uVirtqNbr++)
3485 {
3486 PVIRTIONETVIRTQ pVirtq = &pThis->aVirtqs[uVirtqNbr];
3487 PVIRTIONETWORKER pWorker = &pThis->aWorkers[uVirtqNbr];
3488 PVIRTIONETWORKERR3 pWorkerR3 = &pThisCC->aWorkers[uVirtqNbr];
3489 pVirtq->uIdx = pWorker->uIdx = pWorkerR3->uIdx = uVirtqNbr;
3490 }
3491 /*
3492 * Create queue workers for life of instance. (I.e. they persist through VirtIO bounces)
3493 */
3494 rc = virtioNetR3CreateWorkerThreads(pDevIns, pThis, pThisCC);
3495 if (RT_FAILURE(rc))
3496 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Failed to create worker threads"));
3497
3498 /* Create Link Up Timer */
3499 rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_VIRTUAL, virtioNetR3LinkUpTimer, NULL,
3500 TMTIMER_FLAGS_NO_CRIT_SECT | TMTIMER_FLAGS_NO_RING0,
3501 "VirtioNet Link Up", &pThisCC->hLinkUpTimer);
3502 /*
3503 * Attach network driver instance
3504 */
3505 rc = PDMDevHlpDriverAttach(pDevIns, 0, &pThisCC->IBase, &pThisCC->pDrvBase, "Network Port");
3506 if (RT_SUCCESS(rc))
3507 {
3508 pThisCC->pDrv = PDMIBASE_QUERY_INTERFACE(pThisCC->pDrvBase, PDMINETWORKUP);
3509 AssertMsgStmt(pThisCC->pDrv, ("Failed to obtain the PDMINETWORKUP interface!\n"),
3510 rc = VERR_PDM_MISSING_INTERFACE_BELOW);
3511 }
3512 else if ( rc == VERR_PDM_NO_ATTACHED_DRIVER
3513 || rc == VERR_PDM_CFG_MISSING_DRIVER_NAME)
3514 {
3515 Log(("[%s] No attached driver!\n", pThis->szInst));
3516 AssertRCReturn(rc, rc);
3517 }
3518 /*
3519 * Status driver
3520 */
3521 PPDMIBASE pUpBase;
3522 rc = PDMDevHlpDriverAttach(pDevIns, PDM_STATUS_LUN, &pThisCC->IBase, &pUpBase, "Status Port");
3523 if (RT_FAILURE(rc) && rc != VERR_PDM_NO_ATTACHED_DRIVER)
3524 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Failed to attach the status LUN"));
3525
3526 pThisCC->pLedsConnector = PDMIBASE_QUERY_INTERFACE(pUpBase, PDMILEDCONNECTORS);
3527 /*
3528 * Register saved state.
3529 */
3530 rc = PDMDevHlpSSMRegister(pDevIns, VIRTIONET_SAVEDSTATE_VERSION, sizeof(*pThis),
3531 virtioNetR3ModernSaveExec, virtioNetR3ModernDeviceLoadExec);
3532 AssertRCReturn(rc, rc);
3533 /*
3534 * Statistics and debug stuff.
3535 * The /Public/ bits are official and used by session info in the GUI.
3536 */
3537# ifdef VBOX_WITH_STATISTICS
3538 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatReceiveBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES,
3539 "Amount of data received", "/Public/NetAdapter/%u/BytesReceived", uStatNo);
3540 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatTransmitBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES,
3541 "Amount of data transmitted", "/Public/NetAdapter/%u/BytesTransmitted", uStatNo);
3542 PDMDevHlpSTAMRegisterF(pDevIns, &pDevIns->iInstance, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_NONE,
3543 "Device instance number", "/Public/NetAdapter/%u/%s", uStatNo, pDevIns->pReg->szName);
3544
3545 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatReceiveBytes, STAMTYPE_COUNTER, "ReceiveBytes", STAMUNIT_BYTES, "Amount of data received");
3546 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTransmitBytes, STAMTYPE_COUNTER, "TransmitBytes", STAMUNIT_BYTES, "Amount of data transmitted");
3547 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatReceiveGSO, STAMTYPE_COUNTER, "Packets/ReceiveGSO", STAMUNIT_COUNT, "Number of received GSO packets");
3548 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTransmitPackets, STAMTYPE_COUNTER, "Packets/Transmit", STAMUNIT_COUNT, "Number of sent packets");
3549 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTransmitGSO, STAMTYPE_COUNTER, "Packets/Transmit-Gso", STAMUNIT_COUNT, "Number of sent GSO packets");
3550 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTransmitCSum, STAMTYPE_COUNTER, "Packets/Transmit-Csum", STAMUNIT_COUNT, "Number of completed TX checksums");
3551 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatReceive, STAMTYPE_PROFILE, "Receive/Total", STAMUNIT_TICKS_PER_CALL, "Profiling receive");
3552 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatReceiveStore, STAMTYPE_PROFILE, "Receive/Store", STAMUNIT_TICKS_PER_CALL, "Profiling receive storing");
3553 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRxOverflow, STAMTYPE_PROFILE, "RxOverflow", STAMUNIT_TICKS_PER_OCCURENCE, "Profiling RX overflows");
3554 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRxOverflowWakeup, STAMTYPE_COUNTER, "RxOverflowWakeup", STAMUNIT_OCCURENCES, "Nr of RX overflow wakeups");
3555 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTransmit, STAMTYPE_PROFILE, "Transmit/Total", STAMUNIT_TICKS_PER_CALL, "Profiling transmits in HC");
3556 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTransmitSend, STAMTYPE_PROFILE, "Transmit/Send", STAMUNIT_TICKS_PER_CALL, "Profiling send transmit in HC");
3557 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTransmitByNetwork, STAMTYPE_COUNTER, "Transmit/ByNetwork", STAMUNIT_COUNT, "Network-initiated transmissions");
3558 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTransmitByThread, STAMTYPE_COUNTER, "Transmit/ByThread", STAMUNIT_COUNT, "Thread-initiated transmissions");
3559# endif
3560 /*
3561 * Register the debugger info callback (ignore errors).
3562 */
3563 char szTmp[128];
3564 rc = PDMDevHlpDBGFInfoRegister(pDevIns, "virtio-net", "Display virtio-net info (help, net, features, state, pointers, queues, all)", virtioNetR3Info);
3565 if (RT_FAILURE(rc))
3566 LogRel(("Failed to register DBGF info for device %s\n", szTmp));
3567 return rc;
3568}
3569
3570#else /* !IN_RING3 */
3571
3572/**
3573 * @callback_method_impl{PDMDEVREGR0,pfnConstruct}
3574 */
3575static DECLCALLBACK(int) virtioNetRZConstruct(PPDMDEVINS pDevIns)
3576{
3577 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
3578 PVIRTIONET pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIONET);
3579 PVIRTIONETCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIONETCC);
3580 pThisCC->Virtio.pfnVirtqNotified = virtioNetVirtqNotified;
3581 return virtioCoreRZInit(pDevIns, &pThis->Virtio);
3582}
3583
3584#endif /* !IN_RING3 */
3585
3586/**
3587 * The device registration structure.
3588 */
3589const PDMDEVREG g_DeviceVirtioNet =
3590{
3591 /* .uVersion = */ PDM_DEVREG_VERSION,
3592 /* .uReserved0 = */ 0,
3593 /* .szName = */ "virtio-net",
3594 /* .fFlags = */ PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_NEW_STYLE | PDM_DEVREG_FLAGS_RZ,
3595 /* .fClass = */ PDM_DEVREG_CLASS_NETWORK,
3596 /* .cMaxInstances = */ ~0U,
3597 /* .uSharedVersion = */ 42,
3598 /* .cbInstanceShared = */ sizeof(VIRTIONET),
3599 /* .cbInstanceCC = */ sizeof(VIRTIONETCC),
3600 /* .cbInstanceRC = */ sizeof(VIRTIONETRC),
3601 /* .cMaxPciDevices = */ 1,
3602 /* .cMaxMsixVectors = */ VBOX_MSIX_MAX_ENTRIES,
3603 /* .pszDescription = */ "Virtio Host NET.\n",
3604#if defined(IN_RING3)
3605 /* .pszRCMod = */ "VBoxDDRC.rc",
3606 /* .pszR0Mod = */ "VBoxDDR0.r0",
3607 /* .pfnConstruct = */ virtioNetR3Construct,
3608 /* .pfnDestruct = */ virtioNetR3Destruct,
3609 /* .pfnRelocate = */ NULL,
3610 /* .pfnMemSetup = */ NULL,
3611 /* .pfnPowerOn = */ NULL,
3612 /* .pfnReset = */ NULL,
3613 /* .pfnSuspend = */ virtioNetWakeupRxBufWaiter,
3614 /* .pfnResume = */ NULL,
3615 /* .pfnAttach = */ virtioNetR3Attach,
3616 /* .pfnDetach = */ virtioNetR3Detach,
3617 /* .pfnQueryInterface = */ NULL,
3618 /* .pfnInitComplete = */ NULL,
3619 /* .pfnPowerOff = */ virtioNetWakeupRxBufWaiter,
3620 /* .pfnSoftReset = */ NULL,
3621 /* .pfnReserved0 = */ NULL,
3622 /* .pfnReserved1 = */ NULL,
3623 /* .pfnReserved2 = */ NULL,
3624 /* .pfnReserved3 = */ NULL,
3625 /* .pfnReserved4 = */ NULL,
3626 /* .pfnReserved5 = */ NULL,
3627 /* .pfnReserved6 = */ NULL,
3628 /* .pfnReserved7 = */ NULL,
3629#elif defined(IN_RING0)
3630 /* .pfnEarlyConstruct = */ NULL,
3631 /* .pfnConstruct = */ virtioNetRZConstruct,
3632 /* .pfnDestruct = */ NULL,
3633 /* .pfnFinalDestruct = */ NULL,
3634 /* .pfnRequest = */ NULL,
3635 /* .pfnReserved0 = */ NULL,
3636 /* .pfnReserved1 = */ NULL,
3637 /* .pfnReserved2 = */ NULL,
3638 /* .pfnReserved3 = */ NULL,
3639 /* .pfnReserved4 = */ NULL,
3640 /* .pfnReserved5 = */ NULL,
3641 /* .pfnReserved6 = */ NULL,
3642 /* .pfnReserved7 = */ NULL,
3643#elif defined(IN_RC)
3644 /* .pfnConstruct = */ virtioNetRZConstruct,
3645 /* .pfnReserved0 = */ NULL,
3646 /* .pfnReserved1 = */ NULL,
3647 /* .pfnReserved2 = */ NULL,
3648 /* .pfnReserved3 = */ NULL,
3649 /* .pfnReserved4 = */ NULL,
3650 /* .pfnReserved5 = */ NULL,
3651 /* .pfnReserved6 = */ NULL,
3652 /* .pfnReserved7 = */ NULL,
3653#else
3654# error "Not in IN_RING3, IN_RING0 or IN_RC!"
3655#endif
3656 /* .uVersionEnd = */ PDM_DEVREG_VERSION
3657};
3658
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