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