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