VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/DrvIntNet.cpp

Last change on this file was 102797, checked in by vboxsync, 5 months ago

Devices/DrvIntNet,NetworkServices/{VBoxIntNetSwitch,IntNetIf}: Plug a
few more memory leaks in VBoxIntNetSwitch which affect macOS hosts
configured to use 'Internal Networking'. ticketref:21752

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 81.7 KB
RevLine 
[10451]1/* $Id: DrvIntNet.cpp 102797 2024-01-09 15:01:16Z vboxsync $ */
[1]2/** @file
[10451]3 * DrvIntNet - Internal network transport driver.
[1]4 */
5
6/*
[98103]7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
[1]8 *
[96407]9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
[1]26 */
27
[57358]28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
[1]32#define LOG_GROUP LOG_GROUP_DRV_INTNET
[97046]33#if defined(RT_OS_DARWIN) && defined(VBOX_WITH_INTNET_SERVICE_IN_R3)
34# include <xpc/xpc.h> /* This needs to be here because it drags PVM in and cdefs.h needs to undefine it... */
35#endif
36#include <iprt/cdefs.h>
37
[35346]38#include <VBox/vmm/pdmdrv.h>
39#include <VBox/vmm/pdmnetinline.h>
40#include <VBox/vmm/pdmnetifs.h>
41#include <VBox/vmm/cfgm.h>
[1]42#include <VBox/intnet.h>
[26574]43#include <VBox/intnetinline.h>
[35346]44#include <VBox/vmm/vmm.h>
[28328]45#include <VBox/sup.h>
[1]46#include <VBox/err.h>
47
[28095]48#include <VBox/param.h>
[1]49#include <VBox/log.h>
50#include <iprt/asm.h>
51#include <iprt/assert.h>
[23918]52#include <iprt/ctype.h>
[26574]53#include <iprt/memcache.h>
[23918]54#include <iprt/net.h>
[1]55#include <iprt/semaphore.h>
56#include <iprt/string.h>
57#include <iprt/time.h>
[23918]58#include <iprt/thread.h>
[25966]59#include <iprt/uuid.h>
[37979]60#if defined(RT_OS_DARWIN) && defined(IN_RING3)
61# include <iprt/system.h>
62#endif
[1]63
[35353]64#include "VBoxDD.h"
[1]65
66
[57358]67/*********************************************************************************************************************************
68* Defined Constants And Macros *
69*********************************************************************************************************************************/
[93652]70#if 0
[28721]71/** Enables the ring-0 part. */
72#define VBOX_WITH_DRVINTNET_IN_R0
[93652]73#endif
[28332]74
[28721]75
[57358]76/*********************************************************************************************************************************
77* Structures and Typedefs *
78*********************************************************************************************************************************/
[1]79/**
80 * The state of the asynchronous thread.
81 */
[28332]82typedef enum RECVSTATE
[1]83{
84 /** The thread is suspended. */
[28332]85 RECVSTATE_SUSPENDED = 1,
[1]86 /** The thread is running. */
[28332]87 RECVSTATE_RUNNING,
[1]88 /** The thread must (/has) terminate. */
[28332]89 RECVSTATE_TERMINATE,
[1]90 /** The usual 32-bit type blowup. */
[28332]91 RECVSTATE_32BIT_HACK = 0x7fffffff
92} RECVSTATE;
[1]93
94/**
[25993]95 * Internal networking driver instance data.
[25966]96 *
[26305]97 * @implements PDMINETWORKUP
[1]98 */
99typedef struct DRVINTNET
100{
101 /** The network interface. */
[26305]102 PDMINETWORKUP INetworkUpR3;
[1]103 /** The network interface. */
[26305]104 R3PTRTYPE(PPDMINETWORKDOWN) pIAboveNet;
[10843]105 /** The network config interface.
106 * Can (in theory at least) be NULL. */
[26305]107 R3PTRTYPE(PPDMINETWORKCONFIG) pIAboveConfigR3;
[28711]108 /** Pointer to the driver instance (ring-3). */
[25993]109 PPDMDRVINSR3 pDrvInsR3;
[28711]110 /** Pointer to the communication buffer (ring-3). */
[25993]111 R3PTRTYPE(PINTNETBUF) pBufR3;
[93652]112#ifdef VBOX_WITH_DRVINTNET_IN_R0
[28320]113 /** Ring-3 base interface for the ring-0 context. */
114 PDMIBASER0 IBaseR0;
115 /** Ring-3 base interface for the raw-mode context. */
116 PDMIBASERC IBaseRC;
117 RTR3PTR R3PtrAlignment;
118
119 /** The network interface for the ring-0 context. */
120 PDMINETWORKUPR0 INetworkUpR0;
[28711]121 /** Pointer to the driver instance (ring-0). */
[28320]122 PPDMDRVINSR0 pDrvInsR0;
[28711]123 /** Pointer to the communication buffer (ring-0). */
124 R0PTRTYPE(PINTNETBUF) pBufR0;
[28320]125
126 /** The network interface for the raw-mode context. */
127 PDMINETWORKUPRC INetworkUpRC;
128 /** Pointer to the driver instance. */
129 PPDMDRVINSRC pDrvInsRC;
130 RTRCPTR RCPtrAlignment;
[93652]131#endif
[28320]132
133 /** The transmit lock. */
134 PDMCRITSECT XmitLock;
[1]135 /** Interface handle. */
[25993]136 INTNETIFHANDLE hIf;
[28332]137 /** The receive thread state. */
[28339]138 RECVSTATE volatile enmRecvState;
[28328]139 /** The receive thread. */
140 RTTHREAD hRecvThread;
141 /** The event semaphore that the receive thread waits on. */
142 RTSEMEVENT hRecvEvt;
143 /** The transmit thread. */
144 PPDMTHREAD pXmitThread;
145 /** The event semaphore that the transmit thread waits on. */
146 SUPSEMEVENT hXmitEvt;
147 /** The support driver session handle. */
148 PSUPDRVSESSION pSupDrvSession;
[26574]149 /** Scatter/gather descriptor cache. */
150 RTMEMCACHE hSgCache;
[1]151 /** Set if the link is down.
152 * When the link is down all incoming packets will be dropped. */
[25993]153 bool volatile fLinkDown;
[28328]154 /** Set when the xmit thread has been signalled. (atomic) */
155 bool volatile fXmitSignalled;
156 /** Set if the transmit thread the one busy transmitting. */
157 bool volatile fXmitOnXmitThread;
[28711]158 /** The xmit thread should process the ring ASAP. */
159 bool fXmitProcessRing;
[9594]160 /** Set if data transmission should start immediately and deactivate
161 * as late as possible. */
[25993]162 bool fActivateEarlyDeactivateLate;
163 /** Padding. */
[28711]164 bool afReserved[HC_ARCH_BITS == 64 ? 3 : 3];
165 /** Scratch space for holding the ring-0 scatter / gather descriptor.
166 * The PDMSCATTERGATHER::fFlags member is used to indicate whether it is in
167 * use or not. Always accessed while owning the XmitLock. */
168 union
169 {
170 PDMSCATTERGATHER Sg;
171 uint8_t padding[8 * sizeof(RTUINTPTR)];
172 } u;
[25993]173 /** The network name. */
174 char szNetwork[INTNET_MAX_NETWORK_NAME];
[1]175
[28320]176 /** Number of GSO packets sent. */
177 STAMCOUNTER StatSentGso;
[33540]178 /** Number of GSO packets received. */
[28320]179 STAMCOUNTER StatReceivedGso;
[28711]180 /** Number of packets send from ring-0. */
181 STAMCOUNTER StatSentR0;
[33540]182 /** The number of times we've had to wake up the xmit thread to continue the
[28711]183 * ring-0 job. */
184 STAMCOUNTER StatXmitWakeupR0;
[33540]185 /** The number of times we've had to wake up the xmit thread to continue the
[28711]186 * ring-3 job. */
187 STAMCOUNTER StatXmitWakeupR3;
188 /** The times the xmit thread has been told to process the ring. */
189 STAMCOUNTER StatXmitProcessRing;
[28720]190#ifdef VBOX_WITH_STATISTICS
191 /** Profiling packet transmit runs. */
192 STAMPROFILE StatTransmit;
193 /** Profiling packet receive runs. */
194 STAMPROFILEADV StatReceive;
[28320]195#endif /* VBOX_WITH_STATISTICS */
[23205]196#ifdef LOG_ENABLED
197 /** The nano ts of the last transfer. */
[25993]198 uint64_t u64LastTransferTS;
[23205]199 /** The nano ts of the last receive. */
[25993]200 uint64_t u64LastReceiveTS;
[23205]201#endif
[97046]202#if defined(RT_OS_DARWIN) && defined(VBOX_WITH_INTNET_SERVICE_IN_R3)
203 /** XPC connection handle to the R3 internal network switch service. */
204 xpc_connection_t hXpcCon;
205 /** Flag whether the R3 internal network service is being used. */
206 bool fIntNetR3Svc;
207 /** Size of the communication buffer in bytes. */
208 size_t cbBuf;
209#endif
[25993]210} DRVINTNET;
[28320]211AssertCompileMemberAlignment(DRVINTNET, XmitLock, 8);
212AssertCompileMemberAlignment(DRVINTNET, StatSentGso, 8);
[25993]213/** Pointer to instance data of the internal networking driver. */
214typedef DRVINTNET *PDRVINTNET;
[23208]215
[36075]216/**
217 * Config value to flag translation structure.
218 */
219typedef struct DRVINTNETFLAG
220{
221 /** The value. */
222 const char *pszChoice;
223 /** The corresponding flag. */
224 uint32_t fFlag;
225} DRVINTNETFLAG;
226/** Pointer to a const flag value translation. */
227typedef DRVINTNETFLAG const *PCDRVINTNETFLAG;
[1]228
[36075]229
[25993]230#ifdef IN_RING3
[1]231
232
233/**
[97046]234 * Calls the internal networking switch service living in either R0 or in another R3 process.
235 *
236 * @returns VBox status code.
237 * @param pThis The internal network driver instance data.
238 * @param uOperation The operation to execute.
239 * @param pvArg Pointer to the argument data.
240 * @param cbArg Size of the argument data in bytes.
241 */
242static int drvR3IntNetCallSvc(PDRVINTNET pThis, uint32_t uOperation, void *pvArg, unsigned cbArg)
243{
244#if defined(RT_OS_DARWIN) && defined(VBOX_WITH_INTNET_SERVICE_IN_R3)
245 if (pThis->fIntNetR3Svc)
246 {
[97072]247 xpc_object_t hObj = xpc_dictionary_create(NULL, NULL, 0);
[97046]248 xpc_dictionary_set_uint64(hObj, "req-id", uOperation);
249 xpc_dictionary_set_data(hObj, "req", pvArg, cbArg);
250 xpc_object_t hObjReply = xpc_connection_send_message_with_reply_sync(pThis->hXpcCon, hObj);
[100870]251 xpc_release(hObj);
252
[97338]253 uint64_t u64Rc = xpc_dictionary_get_uint64(hObjReply, "rc");
254 if (INTNET_R3_SVC_IS_VALID_RC(u64Rc))
255 {
256 size_t cbReply = 0;
257 const void *pvData = xpc_dictionary_get_data(hObjReply, "reply", &cbReply);
258 AssertRelease(cbReply == cbArg);
259 memcpy(pvArg, pvData, cbArg);
260 xpc_release(hObjReply);
[97046]261
[97338]262 return INTNET_R3_SVC_GET_RC(u64Rc);
263 }
264
[97046]265 xpc_release(hObjReply);
[97338]266 return VERR_INVALID_STATE;
[97046]267 }
268 else
269#endif
270 return PDMDrvHlpSUPCallVMMR0Ex(pThis->pDrvInsR3, uOperation, pvArg, cbArg);
271}
272
273
[97048]274#if defined(RT_OS_DARWIN) && defined(VBOX_WITH_INTNET_SERVICE_IN_R3)
[97046]275/**
276 * Calls the internal networking switch service living in either R0 or in another R3 process.
277 *
278 * @returns VBox status code.
279 * @param pThis The internal network driver instance data.
280 * @param uOperation The operation to execute.
281 * @param pvArg Pointer to the argument data.
282 * @param cbArg Size of the argument data in bytes.
283 */
284static int drvR3IntNetCallSvcAsync(PDRVINTNET pThis, uint32_t uOperation, void *pvArg, unsigned cbArg)
285{
286 if (pThis->fIntNetR3Svc)
287 {
[97072]288 xpc_object_t hObj = xpc_dictionary_create(NULL, NULL, 0);
[97046]289 xpc_dictionary_set_uint64(hObj, "req-id", uOperation);
290 xpc_dictionary_set_data(hObj, "req", pvArg, cbArg);
291 xpc_connection_send_message(pThis->hXpcCon, hObj);
292 return VINF_SUCCESS;
293 }
294 else
295 return PDMDrvHlpSUPCallVMMR0Ex(pThis->pDrvInsR3, uOperation, pvArg, cbArg);
296}
[97048]297#endif
[97046]298
299
300/**
301 * Map the ring buffer pointer into this process R3 address space.
302 *
303 * @returns VBox status code.
304 * @param pThis The internal network driver instance data.
305 */
306static int drvR3IntNetMapBufferPointers(PDRVINTNET pThis)
307{
308 int rc = VINF_SUCCESS;
309
310 INTNETIFGETBUFFERPTRSREQ GetBufferPtrsReq;
311 GetBufferPtrsReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
312 GetBufferPtrsReq.Hdr.cbReq = sizeof(GetBufferPtrsReq);
313 GetBufferPtrsReq.pSession = NIL_RTR0PTR;
314 GetBufferPtrsReq.hIf = pThis->hIf;
315 GetBufferPtrsReq.pRing3Buf = NULL;
316 GetBufferPtrsReq.pRing0Buf = NIL_RTR0PTR;
317
318#if defined(RT_OS_DARWIN) && defined(VBOX_WITH_INTNET_SERVICE_IN_R3)
319 if (pThis->fIntNetR3Svc)
320 {
[97072]321 xpc_object_t hObj = xpc_dictionary_create(NULL, NULL, 0);
[97046]322 xpc_dictionary_set_uint64(hObj, "req-id", VMMR0_DO_INTNET_IF_GET_BUFFER_PTRS);
323 xpc_dictionary_set_data(hObj, "req", &GetBufferPtrsReq, sizeof(GetBufferPtrsReq));
324 xpc_object_t hObjReply = xpc_connection_send_message_with_reply_sync(pThis->hXpcCon, hObj);
[102797]325 xpc_release(hObj);
326
[97338]327 uint64_t u64Rc = xpc_dictionary_get_uint64(hObjReply, "rc");
328 if (INTNET_R3_SVC_IS_VALID_RC(u64Rc))
329 rc = INTNET_R3_SVC_GET_RC(u64Rc);
330 else
331 rc = VERR_INVALID_STATE;
332
[97046]333 if (RT_SUCCESS(rc))
334 {
335 /* Get the shared memory object. */
336 xpc_object_t hObjShMem = xpc_dictionary_get_value(hObjReply, "buf-ptr");
337 size_t cbMem = xpc_shmem_map(hObjShMem, (void **)&pThis->pBufR3);
338 if (!cbMem)
339 rc = VERR_NO_MEMORY;
340 else
341 pThis->cbBuf = cbMem;
342 }
[97338]343
[97046]344 xpc_release(hObjReply);
345 }
346 else
347#endif
348 {
349 rc = PDMDrvHlpSUPCallVMMR0Ex(pThis->pDrvInsR3, VMMR0_DO_INTNET_IF_GET_BUFFER_PTRS, &GetBufferPtrsReq, sizeof(GetBufferPtrsReq));
350 if (RT_SUCCESS(rc))
351 {
352 AssertRelease(RT_VALID_PTR(GetBufferPtrsReq.pRing3Buf));
353 pThis->pBufR3 = GetBufferPtrsReq.pRing3Buf;
354#ifdef VBOX_WITH_DRVINTNET_IN_R0
355 pThis->pBufR0 = GetBufferPtrsReq.pRing0Buf;
356#endif
357 }
358 }
359
360 return rc;
361}
362
363
364/**
[10843]365 * Updates the MAC address on the kernel side.
366 *
367 * @returns VBox status code.
368 * @param pThis The driver instance.
369 */
[25993]370static int drvR3IntNetUpdateMacAddress(PDRVINTNET pThis)
[10843]371{
[26305]372 if (!pThis->pIAboveConfigR3)
[10843]373 return VINF_SUCCESS;
374
375 INTNETIFSETMACADDRESSREQ SetMacAddressReq;
376 SetMacAddressReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
377 SetMacAddressReq.Hdr.cbReq = sizeof(SetMacAddressReq);
378 SetMacAddressReq.pSession = NIL_RTR0PTR;
379 SetMacAddressReq.hIf = pThis->hIf;
[26305]380 int rc = pThis->pIAboveConfigR3->pfnGetMac(pThis->pIAboveConfigR3, &SetMacAddressReq.Mac);
[10843]381 if (RT_SUCCESS(rc))
[97046]382 rc = drvR3IntNetCallSvc(pThis, VMMR0_DO_INTNET_IF_SET_MAC_ADDRESS,
383 &SetMacAddressReq, sizeof(SetMacAddressReq));
[10843]384
[25993]385 Log(("drvR3IntNetUpdateMacAddress: %.*Rhxs rc=%Rrc\n", sizeof(SetMacAddressReq.Mac), &SetMacAddressReq.Mac, rc));
[10843]386 return rc;
387}
388
389
390/**
391 * Sets the kernel interface active or inactive.
392 *
393 * Worker for poweron, poweroff, suspend and resume.
394 *
395 * @returns VBox status code.
396 * @param pThis The driver instance.
397 * @param fActive The new state.
398 */
[25993]399static int drvR3IntNetSetActive(PDRVINTNET pThis, bool fActive)
[10843]400{
[26305]401 if (!pThis->pIAboveConfigR3)
[10843]402 return VINF_SUCCESS;
403
404 INTNETIFSETACTIVEREQ SetActiveReq;
405 SetActiveReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
406 SetActiveReq.Hdr.cbReq = sizeof(SetActiveReq);
407 SetActiveReq.pSession = NIL_RTR0PTR;
408 SetActiveReq.hIf = pThis->hIf;
409 SetActiveReq.fActive = fActive;
[97046]410 int rc = drvR3IntNetCallSvc(pThis, VMMR0_DO_INTNET_IF_SET_ACTIVE,
411 &SetActiveReq, sizeof(SetActiveReq));
[10843]412
[25993]413 Log(("drvR3IntNetSetActive: fActive=%d rc=%Rrc\n", fActive, rc));
[10843]414 AssertRC(rc);
415 return rc;
416}
417
[28332]418#endif /* IN_RING3 */
[28258]419
[28208]420/* -=-=-=-=- PDMINETWORKUP -=-=-=-=- */
[10843]421
[63461]422#ifndef IN_RING3
[10843]423/**
[28711]424 * Helper for signalling the xmit thread.
425 *
426 * @returns VERR_TRY_AGAIN (convenience).
427 * @param pThis The instance data..
428 */
[63478]429DECLINLINE(int) drvR0IntNetSignalXmit(PDRVINTNET pThis)
[28711]430{
431 /// @todo if (!ASMAtomicXchgBool(&pThis->fXmitSignalled, true)) - needs careful optimizing.
432 {
433 int rc = SUPSemEventSignal(pThis->pSupDrvSession, pThis->hXmitEvt);
434 AssertRC(rc);
[28720]435 STAM_REL_COUNTER_INC(&pThis->CTX_SUFF(StatXmitWakeup));
[28711]436 }
437 return VERR_TRY_AGAIN;
438}
[63478]439#endif /* !IN_RING3 */
[28711]440
441
442/**
443 * Helper for processing the ring-0 consumer side of the xmit ring.
444 *
445 * The caller MUST own the xmit lock.
446 *
447 * @returns Status code from IntNetR0IfSend, except for VERR_TRY_AGAIN.
448 * @param pThis The instance data..
449 */
450DECLINLINE(int) drvIntNetProcessXmit(PDRVINTNET pThis)
451{
[90330]452 Assert(PDMDrvHlpCritSectIsOwner(pThis->CTX_SUFF(pDrvIns), &pThis->XmitLock));
[28711]453
454#ifdef IN_RING3
455 INTNETIFSENDREQ SendReq;
456 SendReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
457 SendReq.Hdr.cbReq = sizeof(SendReq);
458 SendReq.pSession = NIL_RTR0PTR;
459 SendReq.hIf = pThis->hIf;
[97046]460 int rc = drvR3IntNetCallSvc(pThis, VMMR0_DO_INTNET_IF_SEND, &SendReq, sizeof(SendReq));
[28711]461#else
462 int rc = IntNetR0IfSend(pThis->hIf, pThis->pSupDrvSession);
463 if (rc == VERR_TRY_AGAIN)
464 {
465 ASMAtomicUoWriteBool(&pThis->fXmitProcessRing, true);
[63478]466 drvR0IntNetSignalXmit(pThis);
[28711]467 rc = VINF_SUCCESS;
468 }
469#endif
470 return rc;
471}
472
473
474
475/**
[28258]476 * @interface_method_impl{PDMINETWORKUP,pfnBeginXmit}
477 */
[28332]478PDMBOTHCBDECL(int) drvIntNetUp_BeginXmit(PPDMINETWORKUP pInterface, bool fOnWorkerThread)
[28258]479{
[28332]480 PDRVINTNET pThis = RT_FROM_MEMBER(pInterface, DRVINTNET, CTX_SUFF(INetworkUp));
[28711]481#ifndef IN_RING3
[28332]482 Assert(!fOnWorkerThread);
[28711]483#endif
484
[90330]485 int rc = PDMDrvHlpCritSectTryEnter(pThis->CTX_SUFF(pDrvIns), &pThis->XmitLock);
[28328]486 if (RT_SUCCESS(rc))
487 {
488 if (fOnWorkerThread)
489 {
490 ASMAtomicUoWriteBool(&pThis->fXmitOnXmitThread, true);
491 ASMAtomicWriteBool(&pThis->fXmitSignalled, false);
492 }
493 }
494 else if (rc == VERR_SEM_BUSY)
495 {
496 /** @todo Does this actually make sense if the other dude is an EMT and so
[28711]497 * forth? I seriously think this is ring-0 only...
498 * We might end up waking up the xmit thread unnecessarily here, even when in
[33540]499 * ring-0... This needs some more thought and optimizations when the ring-0 bits
[28711]500 * are working. */
501#ifdef IN_RING3
[28328]502 if ( !fOnWorkerThread
[28332]503 /*&& !ASMAtomicUoReadBool(&pThis->fXmitOnXmitThread)
504 && ASMAtomicCmpXchgBool(&pThis->fXmitSignalled, true, false)*/)
[28328]505 {
506 rc = SUPSemEventSignal(pThis->pSupDrvSession, pThis->hXmitEvt);
507 AssertRC(rc);
508 }
[28258]509 rc = VERR_TRY_AGAIN;
[28711]510#else /* IN_RING0 */
[63478]511 rc = drvR0IntNetSignalXmit(pThis);
[28711]512#endif /* IN_RING0 */
[28328]513 }
[28258]514 return rc;
515}
516
517
518/**
[26574]519 * @interface_method_impl{PDMINETWORKUP,pfnAllocBuf}
[5283]520 */
[28332]521PDMBOTHCBDECL(int) drvIntNetUp_AllocBuf(PPDMINETWORKUP pInterface, size_t cbMin,
522 PCPDMNETWORKGSO pGso, PPPDMSCATTERGATHER ppSgBuf)
[5283]523{
[28332]524 PDRVINTNET pThis = RT_FROM_MEMBER(pInterface, DRVINTNET, CTX_SUFF(INetworkUp));
[26574]525 int rc = VINF_SUCCESS;
526 Assert(cbMin < UINT32_MAX / 2);
[90330]527 Assert(PDMDrvHlpCritSectIsOwner(pThis->CTX_SUFF(pDrvIns), &pThis->XmitLock));
[26574]528
[28711]529 /*
530 * Allocate a S/G descriptor.
531 * This shouldn't normally fail as the NICs usually won't allocate more
532 * than one buffer at a time and the SG gets freed on sending.
533 */
[28332]534#ifdef IN_RING3
[28711]535 PPDMSCATTERGATHER pSgBuf = (PPDMSCATTERGATHER)RTMemCacheAlloc(pThis->hSgCache);
536 if (!pSgBuf)
537 return VERR_NO_MEMORY;
538#else
539 PPDMSCATTERGATHER pSgBuf = &pThis->u.Sg;
540 if (RT_UNLIKELY(pSgBuf->fFlags != 0))
[63478]541 return drvR0IntNetSignalXmit(pThis);
[28711]542#endif
543
[5283]544 /*
[28711]545 * Allocate room in the ring buffer.
546 *
547 * In ring-3 we may have to process the xmit ring before there is
[33540]548 * sufficient buffer space since we might have stacked up a few frames to the
[28711]549 * trunk while in ring-0. (There is not point of doing this in ring-0.)
[5283]550 */
[28715]551 PINTNETHDR pHdr = NULL; /* gcc silliness */
[28711]552 if (pGso)
[28714]553 rc = IntNetRingAllocateGsoFrame(&pThis->CTX_SUFF(pBuf)->Send, (uint32_t)cbMin, pGso,
[28711]554 &pHdr, &pSgBuf->aSegs[0].pvSeg);
555 else
[28714]556 rc = IntNetRingAllocateFrame(&pThis->CTX_SUFF(pBuf)->Send, (uint32_t)cbMin,
[28711]557 &pHdr, &pSgBuf->aSegs[0].pvSeg);
558#ifdef IN_RING3
559 if ( RT_FAILURE(rc)
560 && pThis->CTX_SUFF(pBuf)->cbSend >= cbMin * 2 + sizeof(INTNETHDR))
[5283]561 {
[28711]562 drvIntNetProcessXmit(pThis);
[28025]563 if (pGso)
[28714]564 rc = IntNetRingAllocateGsoFrame(&pThis->CTX_SUFF(pBuf)->Send, (uint32_t)cbMin, pGso,
[28025]565 &pHdr, &pSgBuf->aSegs[0].pvSeg);
566 else
[28714]567 rc = IntNetRingAllocateFrame(&pThis->CTX_SUFF(pBuf)->Send, (uint32_t)cbMin,
[28025]568 &pHdr, &pSgBuf->aSegs[0].pvSeg);
[28711]569 }
[26574]570#endif
[28711]571 if (RT_SUCCESS(rc))
572 {
[5283]573 /*
[28711]574 * Set up the S/G descriptor and return successfully.
[5283]575 */
[28711]576 pSgBuf->fFlags = PDMSCATTERGATHER_FLAGS_MAGIC | PDMSCATTERGATHER_FLAGS_OWNER_1;
577 pSgBuf->cbUsed = 0;
578 pSgBuf->cbAvailable = cbMin;
579 pSgBuf->pvAllocator = pHdr;
580 pSgBuf->pvUser = pGso ? (PPDMNETWORKGSO)pSgBuf->aSegs[0].pvSeg - 1 : NULL;
581 pSgBuf->cSegs = 1;
582 pSgBuf->aSegs[0].cbSeg = cbMin;
[5283]583
[28711]584 *ppSgBuf = pSgBuf;
585 return VINF_SUCCESS;
[5283]586 }
[28711]587
588#ifdef IN_RING3
589 /*
590 * If the above fails, then we're really out of space. There are nobody
591 * competing with us here because of the xmit lock.
592 */
593 rc = VERR_NO_MEMORY;
594 RTMemCacheFree(pThis->hSgCache, pSgBuf);
595
596#else /* IN_RING0 */
597 /*
598 * If the request is reasonable, kick the xmit thread and tell it to
599 * process the xmit ring ASAP.
600 */
601 if (pThis->CTX_SUFF(pBuf)->cbSend >= cbMin * 2 + sizeof(INTNETHDR))
602 {
603 pThis->fXmitProcessRing = true;
[63478]604 rc = drvR0IntNetSignalXmit(pThis);
[28711]605 }
[26574]606 else
607 rc = VERR_NO_MEMORY;
[28711]608 pSgBuf->fFlags = 0;
609#endif /* IN_RING0 */
[26574]610 return rc;
611}
[5283]612
[28213]613
[26574]614/**
[27840]615 * @interface_method_impl{PDMINETWORKUP,pfnFreeBuf}
616 */
[28332]617PDMBOTHCBDECL(int) drvIntNetUp_FreeBuf(PPDMINETWORKUP pInterface, PPDMSCATTERGATHER pSgBuf)
[27840]618{
[28332]619 PDRVINTNET pThis = RT_FROM_MEMBER(pInterface, DRVINTNET, CTX_SUFF(INetworkUp));
[28025]620 PINTNETHDR pHdr = (PINTNETHDR)pSgBuf->pvAllocator;
[28711]621#ifdef IN_RING0
622 Assert(pSgBuf == &pThis->u.Sg);
623#endif
[27840]624 Assert(pSgBuf->fFlags == (PDMSCATTERGATHER_FLAGS_MAGIC | PDMSCATTERGATHER_FLAGS_OWNER_1));
625 Assert(pSgBuf->cbUsed <= pSgBuf->cbAvailable);
[46904]626 Assert( pHdr->u8Type == INTNETHDR_TYPE_FRAME
627 || pHdr->u8Type == INTNETHDR_TYPE_GSO);
[90330]628 Assert(PDMDrvHlpCritSectIsOwner(pThis->CTX_SUFF(pDrvIns), &pThis->XmitLock));
[27840]629
630 /** @todo LATER: try unalloc the frame. */
[46904]631 pHdr->u8Type = INTNETHDR_TYPE_PADDING;
[28714]632 IntNetRingCommitFrame(&pThis->CTX_SUFF(pBuf)->Send, pHdr);
[27840]633
[28711]634#ifdef IN_RING3
[27840]635 RTMemCacheFree(pThis->hSgCache, pSgBuf);
[28332]636#else
[28711]637 pSgBuf->fFlags = 0;
[28332]638#endif
[28711]639 return VINF_SUCCESS;
[27840]640}
641
[28213]642
[27840]643/**
[26574]644 * @interface_method_impl{PDMINETWORKUP,pfnSendBuf}
645 */
[28332]646PDMBOTHCBDECL(int) drvIntNetUp_SendBuf(PPDMINETWORKUP pInterface, PPDMSCATTERGATHER pSgBuf, bool fOnWorkerThread)
[26574]647{
[28332]648 PDRVINTNET pThis = RT_FROM_MEMBER(pInterface, DRVINTNET, CTX_SUFF(INetworkUp));
[27840]649 STAM_PROFILE_START(&pThis->StatTransmit, a);
[62610]650 RT_NOREF_PV(fOnWorkerThread);
[27840]651
652 AssertPtr(pSgBuf);
653 Assert(pSgBuf->fFlags == (PDMSCATTERGATHER_FLAGS_MAGIC | PDMSCATTERGATHER_FLAGS_OWNER_1));
654 Assert(pSgBuf->cbUsed <= pSgBuf->cbAvailable);
[90330]655 Assert(PDMDrvHlpCritSectIsOwner(pThis->CTX_SUFF(pDrvIns), &pThis->XmitLock));
[27840]656
[28134]657 if (pSgBuf->pvUser)
658 STAM_COUNTER_INC(&pThis->StatSentGso);
659
[27840]660 /*
661 * Commit the frame and push it thru the switch.
662 */
[28025]663 PINTNETHDR pHdr = (PINTNETHDR)pSgBuf->pvAllocator;
[28714]664 IntNetRingCommitFrameEx(&pThis->CTX_SUFF(pBuf)->Send, pHdr, pSgBuf->cbUsed);
[28711]665 int rc = drvIntNetProcessXmit(pThis);
666 STAM_PROFILE_STOP(&pThis->StatTransmit, a);
[27840]667
[28711]668 /*
669 * Free the descriptor and return.
670 */
671#ifdef IN_RING3
[27840]672 RTMemCacheFree(pThis->hSgCache, pSgBuf);
[28332]673#else
[28711]674 STAM_REL_COUNTER_INC(&pThis->StatSentR0);
675 pSgBuf->fFlags = 0;
[28332]676#endif
[28711]677 return rc;
[5283]678}
679
[1]680
681/**
[28258]682 * @interface_method_impl{PDMINETWORKUP,pfnEndXmit}
683 */
[28332]684PDMBOTHCBDECL(void) drvIntNetUp_EndXmit(PPDMINETWORKUP pInterface)
[28258]685{
[28332]686 PDRVINTNET pThis = RT_FROM_MEMBER(pInterface, DRVINTNET, CTX_SUFF(INetworkUp));
[28328]687 ASMAtomicUoWriteBool(&pThis->fXmitOnXmitThread, false);
[90330]688 PDMDrvHlpCritSectLeave(pThis->CTX_SUFF(pDrvIns), &pThis->XmitLock);
[28258]689}
690
691
692/**
[26305]693 * @interface_method_impl{PDMINETWORKUP,pfnSetPromiscuousMode}
[1]694 */
[28332]695PDMBOTHCBDECL(void) drvIntNetUp_SetPromiscuousMode(PPDMINETWORKUP pInterface, bool fPromiscuous)
[1]696{
[28332]697 PDRVINTNET pThis = RT_FROM_MEMBER(pInterface, DRVINTNET, CTX_SUFF(INetworkUp));
[28711]698
[28332]699#ifdef IN_RING3
[5283]700 INTNETIFSETPROMISCUOUSMODEREQ Req;
701 Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
702 Req.Hdr.cbReq = sizeof(Req);
[10762]703 Req.pSession = NIL_RTR0PTR;
[5283]704 Req.hIf = pThis->hIf;
705 Req.fPromiscuous = fPromiscuous;
[97046]706 int rc = drvR3IntNetCallSvc(pThis, VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE, &Req, sizeof(Req));
[28711]707#else /* IN_RING0 */
708 int rc = IntNetR0IfSetPromiscuousMode(pThis->hIf, pThis->pSupDrvSession, fPromiscuous);
709#endif /* IN_RING0 */
710
[28332]711 LogFlow(("drvIntNetUp_SetPromiscuousMode: fPromiscuous=%RTbool\n", fPromiscuous));
[1]712 AssertRC(rc);
713}
714
[28332]715#ifdef IN_RING3
[1]716
717/**
[26305]718 * @interface_method_impl{PDMINETWORKUP,pfnNotifyLinkChanged}
[1]719 */
[26574]720static DECLCALLBACK(void) drvR3IntNetUp_NotifyLinkChanged(PPDMINETWORKUP pInterface, PDMNETWORKLINKSTATE enmLinkState)
[1]721{
[28332]722 PDRVINTNET pThis = RT_FROM_MEMBER(pInterface, DRVINTNET, CTX_SUFF(INetworkUp));
[1]723 bool fLinkDown;
724 switch (enmLinkState)
725 {
726 case PDMNETWORKLINKSTATE_DOWN:
727 case PDMNETWORKLINKSTATE_DOWN_RESUME:
728 fLinkDown = true;
729 break;
730 default:
731 AssertMsgFailed(("enmLinkState=%d\n", enmLinkState));
[69046]732 RT_FALL_THRU();
[1]733 case PDMNETWORKLINKSTATE_UP:
734 fLinkDown = false;
735 break;
736 }
[26574]737 LogFlow(("drvR3IntNetUp_NotifyLinkChanged: enmLinkState=%d %d->%d\n", enmLinkState, pThis->fLinkDown, fLinkDown));
[1]738 ASMAtomicXchgSize(&pThis->fLinkDown, fLinkDown);
739}
740
741
[28328]742/* -=-=-=-=- Transmit Thread -=-=-=-=- */
743
[1]744/**
[33540]745 * Async I/O thread for deferred packet transmission.
[28328]746 *
747 * @returns VBox status code. Returning failure will naturally terminate the thread.
748 * @param pDrvIns The internal networking driver instance.
749 * @param pThread The thread.
750 */
751static DECLCALLBACK(int) drvR3IntNetXmitThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
752{
753 PDRVINTNET pThis = PDMINS_2_DATA(pDrvIns, PDRVINTNET);
754
755 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
756 {
757 /*
[28711]758 * Transmit any pending packets.
759 */
760 /** @todo Optimize this. We shouldn't call pfnXmitPending unless asked for.
761 * Also there is no need to call drvIntNetProcessXmit if we also
762 * called pfnXmitPending and send one or more frames. */
763 if (ASMAtomicXchgBool(&pThis->fXmitProcessRing, false))
764 {
[28720]765 STAM_REL_COUNTER_INC(&pThis->StatXmitProcessRing);
[90330]766 PDMDrvHlpCritSectEnter(pDrvIns, &pThis->XmitLock, VERR_IGNORED);
[28711]767 drvIntNetProcessXmit(pThis);
[90330]768 PDMDrvHlpCritSectLeave(pDrvIns, &pThis->XmitLock);
[28711]769 }
770
771 pThis->pIAboveNet->pfnXmitPending(pThis->pIAboveNet);
772
773 if (ASMAtomicXchgBool(&pThis->fXmitProcessRing, false))
774 {
[28720]775 STAM_REL_COUNTER_INC(&pThis->StatXmitProcessRing);
[90330]776 PDMDrvHlpCritSectEnter(pDrvIns, &pThis->XmitLock, VERR_IGNORED);
[28711]777 drvIntNetProcessXmit(pThis);
[90330]778 PDMDrvHlpCritSectLeave(pDrvIns, &pThis->XmitLock);
[28711]779 }
780
781 /*
[28328]782 * Block until we've got something to send or is supposed
783 * to leave the running state.
784 */
785 int rc = SUPSemEventWaitNoResume(pThis->pSupDrvSession, pThis->hXmitEvt, RT_INDEFINITE_WAIT);
786 AssertLogRelMsgReturn(RT_SUCCESS(rc) || rc == VERR_INTERRUPTED, ("%Rrc\n", rc), rc);
787 if (RT_UNLIKELY(pThread->enmState != PDMTHREADSTATE_RUNNING))
788 break;
789
790 }
791
792 /* The thread is being initialized, suspended or terminated. */
793 return VINF_SUCCESS;
794}
795
796
797/**
798 * @copydoc FNPDMTHREADWAKEUPDRV
799 */
800static DECLCALLBACK(int) drvR3IntNetXmitWakeUp(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
801{
[62906]802 RT_NOREF(pThread);
[28328]803 PDRVINTNET pThis = PDMINS_2_DATA(pDrvIns, PDRVINTNET);
804 return SUPSemEventSignal(pThis->pSupDrvSession, pThis->hXmitEvt);
805}
806
807
808/* -=-=-=-=- Receive Thread -=-=-=-=- */
809
810/**
[1]811 * Wait for space to become available up the driver/device chain.
812 *
813 * @returns VINF_SUCCESS if space is available.
814 * @returns VERR_STATE_CHANGED if the state changed.
815 * @returns VBox status code on other errors.
816 * @param pThis Pointer to the instance data.
817 */
[28328]818static int drvR3IntNetRecvWaitForSpace(PDRVINTNET pThis)
[1]819{
[28328]820 LogFlow(("drvR3IntNetRecvWaitForSpace:\n"));
[1]821 STAM_PROFILE_ADV_STOP(&pThis->StatReceive, a);
[26305]822 int rc = pThis->pIAboveNet->pfnWaitReceiveAvail(pThis->pIAboveNet, RT_INDEFINITE_WAIT);
[1]823 STAM_PROFILE_ADV_START(&pThis->StatReceive, a);
[28328]824 LogFlow(("drvR3IntNetRecvWaitForSpace: returns %Rrc\n", rc));
[1]825 return rc;
826}
827
828
829/**
830 * Executes async I/O (RUNNING mode).
831 *
832 * @returns VERR_STATE_CHANGED if the state changed.
833 * @returns Appropriate VBox status code (error) on fatal error.
834 * @param pThis The driver instance data.
835 */
[28328]836static int drvR3IntNetRecvRun(PDRVINTNET pThis)
[1]837{
[28328]838 LogFlow(("drvR3IntNetRecvRun: pThis=%p\n", pThis));
[1]839
840 /*
841 * The running loop - processing received data and waiting for more to arrive.
842 */
843 STAM_PROFILE_ADV_START(&pThis->StatReceive, a);
[28711]844 PINTNETBUF pBuf = pThis->CTX_SUFF(pBuf);
[25993]845 PINTNETRINGBUF pRingBuf = &pBuf->Recv;
[1]846 for (;;)
847 {
848 /*
849 * Process the receive buffer.
850 */
[26574]851 PINTNETHDR pHdr;
[28714]852 while ((pHdr = IntNetRingGetNextFrameToRead(pRingBuf)) != NULL)
[1]853 {
854 /*
855 * Check the state and then inspect the packet.
856 */
[28332]857 if (pThis->enmRecvState != RECVSTATE_RUNNING)
[1]858 {
859 STAM_PROFILE_ADV_STOP(&pThis->StatReceive, a);
[28328]860 LogFlow(("drvR3IntNetRecvRun: returns VERR_STATE_CHANGED (state changed - #0)\n"));
[1]861 return VERR_STATE_CHANGED;
862 }
863
[26574]864 Log2(("pHdr=%p offRead=%#x: %.8Rhxs\n", pHdr, pRingBuf->offReadX, pHdr));
[46904]865 uint8_t u8Type = pHdr->u8Type;
866 if ( ( u8Type == INTNETHDR_TYPE_FRAME
867 || u8Type == INTNETHDR_TYPE_GSO)
[1]868 && !pThis->fLinkDown)
869 {
870 /*
871 * Check if there is room for the frame and pass it up.
872 */
873 size_t cbFrame = pHdr->cbFrame;
[26305]874 int rc = pThis->pIAboveNet->pfnWaitReceiveAvail(pThis->pIAboveNet, 0);
[8300]875 if (rc == VINF_SUCCESS)
[1]876 {
[46904]877 if (u8Type == INTNETHDR_TYPE_FRAME)
[28025]878 {
879 /*
880 * Normal frame.
881 */
[1]882#ifdef LOG_ENABLED
[28025]883 if (LogIsEnabled())
884 {
885 uint64_t u64Now = RTTimeProgramNanoTS();
[28328]886 LogFlow(("drvR3IntNetRecvRun: %-4d bytes at %llu ns deltas: r=%llu t=%llu\n",
[28025]887 cbFrame, u64Now, u64Now - pThis->u64LastReceiveTS, u64Now - pThis->u64LastTransferTS));
888 pThis->u64LastReceiveTS = u64Now;
[28328]889 Log2(("drvR3IntNetRecvRun: cbFrame=%#x\n"
[28025]890 "%.*Rhxd\n",
[28714]891 cbFrame, cbFrame, IntNetHdrGetFramePtr(pHdr, pBuf)));
[28025]892 }
[1]893#endif
[28714]894 rc = pThis->pIAboveNet->pfnReceive(pThis->pIAboveNet, IntNetHdrGetFramePtr(pHdr, pBuf), cbFrame);
[28025]895 AssertRC(rc);
[1]896
[28025]897 /* skip to the next frame. */
[28714]898 IntNetRingSkipFrame(pRingBuf);
[28025]899 }
900 else
901 {
902 /*
903 * Generic segment offload frame (INTNETHDR_TYPE_GSO).
904 */
[28134]905 STAM_COUNTER_INC(&pThis->StatReceivedGso);
[28714]906 PCPDMNETWORKGSO pGso = IntNetHdrGetGsoContext(pHdr, pBuf);
[28025]907 if (PDMNetGsoIsValid(pGso, cbFrame, cbFrame - sizeof(PDMNETWORKGSO)))
908 {
[90103]909 if ( !pThis->pIAboveNet->pfnReceiveGso
910 || RT_FAILURE(pThis->pIAboveNet->pfnReceiveGso(pThis->pIAboveNet,
911 (uint8_t *)(pGso + 1),
912 pHdr->cbFrame - sizeof(PDMNETWORKGSO),
913 pGso)))
[33325]914 {
915 /*
916 * This is where we do the offloading since this NIC
917 * does not support large receive offload (LRO).
918 */
919 cbFrame -= sizeof(PDMNETWORKGSO);
[28060]920
[33325]921 uint8_t abHdrScratch[256];
922 uint32_t const cSegs = PDMNetGsoCalcSegmentCount(pGso, cbFrame);
[28025]923#ifdef LOG_ENABLED
[33325]924 if (LogIsEnabled())
925 {
926 uint64_t u64Now = RTTimeProgramNanoTS();
927 LogFlow(("drvR3IntNetRecvRun: %-4d bytes at %llu ns deltas: r=%llu t=%llu; GSO - %u segs\n",
928 cbFrame, u64Now, u64Now - pThis->u64LastReceiveTS, u64Now - pThis->u64LastTransferTS, cSegs));
929 pThis->u64LastReceiveTS = u64Now;
[38549]930 Log2(("drvR3IntNetRecvRun: cbFrame=%#x type=%d cbHdrsTotal=%#x cbHdrsSeg=%#x Hdr1=%#x Hdr2=%#x MMS=%#x\n"
[33325]931 "%.*Rhxd\n",
[38549]932 cbFrame, pGso->u8Type, pGso->cbHdrsTotal, pGso->cbHdrsSeg, pGso->offHdr1, pGso->offHdr2, pGso->cbMaxSeg,
[33325]933 cbFrame - sizeof(*pGso), pGso + 1));
934 }
[28025]935#endif
[62610]936 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
[28025]937 {
[33325]938 uint32_t cbSegFrame;
[62610]939 void *pvSegFrame = PDMNetGsoCarveSegmentQD(pGso, (uint8_t *)(pGso + 1), cbFrame,
940 abHdrScratch, iSeg, cSegs, &cbSegFrame);
[33325]941 rc = drvR3IntNetRecvWaitForSpace(pThis);
942 if (RT_FAILURE(rc))
943 {
[56992]944 Log(("drvR3IntNetRecvRun: drvR3IntNetRecvWaitForSpace -> %Rrc; iSeg=%u cSegs=%u\n", rc, iSeg, cSegs));
[33325]945 break; /* we drop the rest. */
946 }
947 rc = pThis->pIAboveNet->pfnReceive(pThis->pIAboveNet, pvSegFrame, cbSegFrame);
948 AssertRC(rc);
[28025]949 }
950 }
951 }
952 else
953 {
[38549]954 AssertMsgFailed(("cbFrame=%#x type=%d cbHdrsTotal=%#x cbHdrsSeg=%#x Hdr1=%#x Hdr2=%#x MMS=%#x\n",
955 cbFrame, pGso->u8Type, pGso->cbHdrsTotal, pGso->cbHdrsSeg, pGso->offHdr1, pGso->offHdr2, pGso->cbMaxSeg));
[28025]956 STAM_REL_COUNTER_INC(&pBuf->cStatBadFrames);
957 }
958
[28714]959 IntNetRingSkipFrame(pRingBuf);
[28025]960 }
[1]961 }
962 else
963 {
964 /*
965 * Wait for sufficient space to become available and then retry.
966 */
[28328]967 rc = drvR3IntNetRecvWaitForSpace(pThis);
[11266]968 if (RT_FAILURE(rc))
[1]969 {
[15541]970 if (rc == VERR_INTERRUPTED)
971 {
972 /*
973 * NIC is going down, likely because the VM is being reset. Skip the frame.
974 */
[46904]975 AssertMsg(IntNetIsValidFrameType(pHdr->u8Type), ("Unknown frame type %RX16! offRead=%#x\n", pHdr->u8Type, pRingBuf->offReadX));
[28714]976 IntNetRingSkipFrame(pRingBuf);
[15541]977 }
978 else
979 {
980 STAM_PROFILE_ADV_STOP(&pThis->StatReceive, a);
[28328]981 LogFlow(("drvR3IntNetRecvRun: returns %Rrc (wait-for-space)\n", rc));
[15541]982 return rc;
983 }
[1]984 }
985 }
986 }
987 else
988 {
989 /*
990 * Link down or unknown frame - skip to the next frame.
991 */
[46904]992 AssertMsg(IntNetIsValidFrameType(pHdr->u8Type), ("Unknown frame type %RX16! offRead=%#x\n", pHdr->u8Type, pRingBuf->offReadX));
[28714]993 IntNetRingSkipFrame(pRingBuf);
[26574]994 STAM_REL_COUNTER_INC(&pBuf->cStatBadFrames);
[1]995 }
996 } /* while more received data */
997
998 /*
999 * Wait for data, checking the state before we block.
1000 */
[28332]1001 if (pThis->enmRecvState != RECVSTATE_RUNNING)
[1]1002 {
1003 STAM_PROFILE_ADV_STOP(&pThis->StatReceive, a);
[28328]1004 LogFlow(("drvR3IntNetRecvRun: returns VINF_SUCCESS (state changed - #1)\n"));
[1]1005 return VERR_STATE_CHANGED;
1006 }
[5283]1007 INTNETIFWAITREQ WaitReq;
1008 WaitReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
1009 WaitReq.Hdr.cbReq = sizeof(WaitReq);
[10762]1010 WaitReq.pSession = NIL_RTR0PTR;
[5283]1011 WaitReq.hIf = pThis->hIf;
1012 WaitReq.cMillies = 30000; /* 30s - don't wait forever, timeout now and then. */
[1]1013 STAM_PROFILE_ADV_STOP(&pThis->StatReceive, a);
[97046]1014
1015#if defined(RT_OS_DARWIN) && defined(VBOX_WITH_INTNET_SERVICE_IN_R3)
1016 if (pThis->fIntNetR3Svc)
[1]1017 {
[97046]1018 /* Send an asynchronous message. */
1019 int rc = drvR3IntNetCallSvcAsync(pThis, VMMR0_DO_INTNET_IF_WAIT, &WaitReq, sizeof(WaitReq));
1020 if (RT_SUCCESS(rc))
1021 {
1022 /* Wait on the receive semaphore. */
1023 rc = RTSemEventWait(pThis->hRecvEvt, 30 * RT_MS_1SEC);
1024 if ( RT_FAILURE(rc)
1025 && rc != VERR_TIMEOUT
1026 && rc != VERR_INTERRUPTED)
1027 {
1028 LogFlow(("drvR3IntNetRecvRun: returns %Rrc\n", rc));
1029 return rc;
1030 }
1031 }
[1]1032 }
[97046]1033 else
1034#endif
1035 {
1036 int rc = PDMDrvHlpSUPCallVMMR0Ex(pThis->pDrvInsR3, VMMR0_DO_INTNET_IF_WAIT, &WaitReq, sizeof(WaitReq));
1037 if ( RT_FAILURE(rc)
1038 && rc != VERR_TIMEOUT
1039 && rc != VERR_INTERRUPTED)
1040 {
1041 LogFlow(("drvR3IntNetRecvRun: returns %Rrc\n", rc));
1042 return rc;
1043 }
1044 }
[1]1045 STAM_PROFILE_ADV_START(&pThis->StatReceive, a);
1046 }
1047}
1048
1049
1050/**
1051 * Asynchronous I/O thread for handling receive.
1052 *
1053 * @returns VINF_SUCCESS (ignored).
[62906]1054 * @param hThreadSelf Thread handle.
[1]1055 * @param pvUser Pointer to a DRVINTNET structure.
1056 */
[62906]1057static DECLCALLBACK(int) drvR3IntNetRecvThread(RTTHREAD hThreadSelf, void *pvUser)
[1]1058{
[62906]1059 RT_NOREF(hThreadSelf);
[1]1060 PDRVINTNET pThis = (PDRVINTNET)pvUser;
[28328]1061 LogFlow(("drvR3IntNetRecvThread: pThis=%p\n", pThis));
[1]1062 STAM_PROFILE_ADV_START(&pThis->StatReceive, a);
1063
1064 /*
1065 * The main loop - acting on state.
1066 */
1067 for (;;)
1068 {
[28332]1069 RECVSTATE enmRecvState = pThis->enmRecvState;
1070 switch (enmRecvState)
[1]1071 {
[28332]1072 case RECVSTATE_SUSPENDED:
[1]1073 {
[28328]1074 int rc = RTSemEventWait(pThis->hRecvEvt, 30000);
[11266]1075 if ( RT_FAILURE(rc)
[1]1076 && rc != VERR_TIMEOUT)
1077 {
[28328]1078 LogFlow(("drvR3IntNetRecvThread: returns %Rrc\n", rc));
[1]1079 return rc;
1080 }
1081 break;
1082 }
1083
[28332]1084 case RECVSTATE_RUNNING:
[1]1085 {
[28328]1086 int rc = drvR3IntNetRecvRun(pThis);
[1]1087 if ( rc != VERR_STATE_CHANGED
[11266]1088 && RT_FAILURE(rc))
[1]1089 {
[28328]1090 LogFlow(("drvR3IntNetRecvThread: returns %Rrc\n", rc));
[1]1091 return rc;
1092 }
1093 break;
1094 }
1095
1096 default:
[28332]1097 AssertMsgFailed(("Invalid state %d\n", enmRecvState));
[69046]1098 RT_FALL_THRU();
[28332]1099 case RECVSTATE_TERMINATE:
[28328]1100 LogFlow(("drvR3IntNetRecvThread: returns VINF_SUCCESS\n"));
[1]1101 return VINF_SUCCESS;
1102 }
1103 }
1104}
1105
[28328]1106
[93652]1107#ifdef VBOX_WITH_DRVINTNET_IN_R0
1108
[26137]1109/* -=-=-=-=- PDMIBASERC -=-=-=-=- */
[1]1110
1111/**
[26137]1112 * @interface_method_impl{PDMIBASERC,pfnQueryInterface}
1113 */
1114static DECLCALLBACK(RTRCPTR) drvR3IntNetIBaseRC_QueryInterface(PPDMIBASERC pInterface, const char *pszIID)
1115{
1116 PDRVINTNET pThis = RT_FROM_MEMBER(pInterface, DRVINTNET, IBaseRC);
[28332]1117#if 0
[28320]1118 PDMIBASERC_RETURN_INTERFACE(pThis->pDrvInsR3, pszIID, PDMINETWORKUP, &pThis->INetworkUpRC);
[62906]1119#else
1120 RT_NOREF(pThis, pszIID);
[28320]1121#endif
[26137]1122 return NIL_RTRCPTR;
1123}
1124
[28328]1125
[26137]1126/* -=-=-=-=- PDMIBASER0 -=-=-=-=- */
1127
1128/**
1129 * @interface_method_impl{PDMIBASER0,pfnQueryInterface}
1130 */
1131static DECLCALLBACK(RTR0PTR) drvR3IntNetIBaseR0_QueryInterface(PPDMIBASER0 pInterface, const char *pszIID)
1132{
1133 PDRVINTNET pThis = RT_FROM_MEMBER(pInterface, DRVINTNET, IBaseR0);
[28320]1134 PDMIBASER0_RETURN_INTERFACE(pThis->pDrvInsR3, pszIID, PDMINETWORKUP, &pThis->INetworkUpR0);
[26137]1135 return NIL_RTR0PTR;
1136}
1137
[93652]1138#endif /* VBOX_WITH_DRVINTNET_IN_R0 */
[28328]1139
[26137]1140/* -=-=-=-=- PDMIBASE -=-=-=-=- */
1141
[93652]1142
[26137]1143/**
[25966]1144 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
[1]1145 */
[26137]1146static DECLCALLBACK(void *) drvR3IntNetIBase_QueryInterface(PPDMIBASE pInterface, const char *pszIID)
[1]1147{
1148 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
[25966]1149 PDRVINTNET pThis = PDMINS_2_DATA(pDrvIns, PDRVINTNET);
1150
[25985]1151 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
[93652]1152#ifdef VBOX_WITH_DRVINTNET_IN_R0
[26137]1153 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASER0, &pThis->IBaseR0);
1154 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASERC, &pThis->IBaseRC);
[93652]1155#endif
[26305]1156 PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKUP, &pThis->INetworkUpR3);
[25966]1157 return NULL;
[1]1158}
1159
[28328]1160
[25993]1161/* -=-=-=-=- PDMDRVREG -=-=-=-=- */
[1]1162
1163/**
1164 * Power Off notification.
1165 *
1166 * @param pDrvIns The driver instance.
1167 */
[25993]1168static DECLCALLBACK(void) drvR3IntNetPowerOff(PPDMDRVINS pDrvIns)
[1]1169{
[25993]1170 LogFlow(("drvR3IntNetPowerOff\n"));
[11267]1171 PDRVINTNET pThis = PDMINS_2_DATA(pDrvIns, PDRVINTNET);
[9594]1172 if (!pThis->fActivateEarlyDeactivateLate)
[10843]1173 {
[28332]1174 ASMAtomicXchgSize(&pThis->enmRecvState, RECVSTATE_SUSPENDED);
[25993]1175 drvR3IntNetSetActive(pThis, false /* fActive */);
[10843]1176 }
[1]1177}
1178
1179
1180/**
[28213]1181 * drvR3IntNetResume helper.
1182 */
1183static int drvR3IntNetResumeSend(PDRVINTNET pThis, const void *pvBuf, size_t cb)
1184{
1185 /*
1186 * Add the frame to the send buffer and push it onto the network.
1187 */
[28714]1188 int rc = IntNetRingWriteFrame(&pThis->pBufR3->Send, pvBuf, (uint32_t)cb);
[28213]1189 if ( rc == VERR_BUFFER_OVERFLOW
1190 && pThis->pBufR3->cbSend < cb)
1191 {
1192 INTNETIFSENDREQ SendReq;
1193 SendReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
1194 SendReq.Hdr.cbReq = sizeof(SendReq);
1195 SendReq.pSession = NIL_RTR0PTR;
1196 SendReq.hIf = pThis->hIf;
[97046]1197 drvR3IntNetCallSvc(pThis, VMMR0_DO_INTNET_IF_SEND, &SendReq, sizeof(SendReq));
[28213]1198
[28714]1199 rc = IntNetRingWriteFrame(&pThis->pBufR3->Send, pvBuf, (uint32_t)cb);
[28213]1200 }
1201
1202 if (RT_SUCCESS(rc))
1203 {
1204 INTNETIFSENDREQ SendReq;
1205 SendReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
1206 SendReq.Hdr.cbReq = sizeof(SendReq);
1207 SendReq.pSession = NIL_RTR0PTR;
1208 SendReq.hIf = pThis->hIf;
[97046]1209 rc = drvR3IntNetCallSvc(pThis, VMMR0_DO_INTNET_IF_SEND, &SendReq, sizeof(SendReq));
[28213]1210 }
1211
1212 AssertRC(rc);
1213 return rc;
1214}
1215
1216
1217/**
[1]1218 * Resume notification.
1219 *
1220 * @param pDrvIns The driver instance.
1221 */
[25993]1222static DECLCALLBACK(void) drvR3IntNetResume(PPDMDRVINS pDrvIns)
[1]1223{
[25993]1224 LogFlow(("drvR3IntNetPowerResume\n"));
[11267]1225 PDRVINTNET pThis = PDMINS_2_DATA(pDrvIns, PDRVINTNET);
[47499]1226 VMRESUMEREASON enmReason = PDMDrvHlpVMGetResumeReason(pDrvIns);
1227
[9594]1228 if (!pThis->fActivateEarlyDeactivateLate)
1229 {
[28332]1230 ASMAtomicXchgSize(&pThis->enmRecvState, RECVSTATE_RUNNING);
[28328]1231 RTSemEventSignal(pThis->hRecvEvt);
[25993]1232 drvR3IntNetUpdateMacAddress(pThis); /* (could be a state restore) */
1233 drvR3IntNetSetActive(pThis, true /* fActive */);
[9594]1234 }
[47499]1235
1236 switch (enmReason)
[23918]1237 {
[47499]1238 case VMRESUMEREASON_HOST_RESUME:
[23918]1239 {
[47499]1240 uint32_t u32TrunkType;
[91883]1241 int rc = pDrvIns->pHlpR3->pfnCFGMQueryU32(pDrvIns->pCfg, "TrunkType", &u32TrunkType);
[47499]1242 AssertRC(rc);
1243
1244 /*
1245 * Only do the disconnect for bridged networking. Host-only and
1246 * internal networks are not affected by a host resume.
1247 */
1248 if ( RT_SUCCESS(rc)
1249 && u32TrunkType == kIntNetTrunkType_NetFlt)
1250 {
1251 rc = pThis->pIAboveConfigR3->pfnSetLinkState(pThis->pIAboveConfigR3,
1252 PDMNETWORKLINKSTATE_DOWN_RESUME);
1253 AssertRC(rc);
1254 }
1255 break;
1256 }
1257 case VMRESUMEREASON_TELEPORTED:
1258 case VMRESUMEREASON_TELEPORT_FAILED:
1259 {
1260 if ( PDMDrvHlpVMTeleportedAndNotFullyResumedYet(pDrvIns)
1261 && pThis->pIAboveConfigR3)
1262 {
1263 /*
1264 * We've just been teleported and need to drop a hint to the switch
1265 * since we're likely to have changed to a different port. We just
1266 * push out some ethernet frame that doesn't mean anything to anyone.
1267 * For this purpose ethertype 0x801e was chosen since it was registered
1268 * to Sun (dunno what it is/was used for though).
1269 */
1270 union
1271 {
1272 RTNETETHERHDR Hdr;
1273 uint8_t ab[128];
1274 } Frame;
1275 RT_ZERO(Frame);
1276 Frame.Hdr.DstMac.au16[0] = 0xffff;
1277 Frame.Hdr.DstMac.au16[1] = 0xffff;
1278 Frame.Hdr.DstMac.au16[2] = 0xffff;
1279 Frame.Hdr.EtherType = RT_H2BE_U16_C(0x801e);
[48947]1280 int rc = pThis->pIAboveConfigR3->pfnGetMac(pThis->pIAboveConfigR3,
[47499]1281 &Frame.Hdr.SrcMac);
1282 if (RT_SUCCESS(rc))
1283 rc = drvR3IntNetResumeSend(pThis, &Frame, sizeof(Frame));
1284 if (RT_FAILURE(rc))
[48947]1285 LogRel(("IntNet#%u: Sending dummy frame failed: %Rrc\n",
[47499]1286 pDrvIns->iInstance, rc));
1287 }
1288 break;
1289 }
1290 default: /* ignore every other resume reason else */
1291 break;
1292 } /* end of switch(enmReason) */
[1]1293}
1294
1295
1296/**
1297 * Suspend notification.
1298 *
1299 * @param pDrvIns The driver instance.
1300 */
[25993]1301static DECLCALLBACK(void) drvR3IntNetSuspend(PPDMDRVINS pDrvIns)
[1]1302{
[25993]1303 LogFlow(("drvR3IntNetPowerSuspend\n"));
[11267]1304 PDRVINTNET pThis = PDMINS_2_DATA(pDrvIns, PDRVINTNET);
[9594]1305 if (!pThis->fActivateEarlyDeactivateLate)
[10843]1306 {
[28332]1307 ASMAtomicXchgSize(&pThis->enmRecvState, RECVSTATE_SUSPENDED);
[25993]1308 drvR3IntNetSetActive(pThis, false /* fActive */);
[10843]1309 }
[1]1310}
1311
1312
1313/**
1314 * Power On notification.
1315 *
1316 * @param pDrvIns The driver instance.
1317 */
[25993]1318static DECLCALLBACK(void) drvR3IntNetPowerOn(PPDMDRVINS pDrvIns)
[1]1319{
[25993]1320 LogFlow(("drvR3IntNetPowerOn\n"));
[11267]1321 PDRVINTNET pThis = PDMINS_2_DATA(pDrvIns, PDRVINTNET);
[9594]1322 if (!pThis->fActivateEarlyDeactivateLate)
[6001]1323 {
[28332]1324 ASMAtomicXchgSize(&pThis->enmRecvState, RECVSTATE_RUNNING);
[28328]1325 RTSemEventSignal(pThis->hRecvEvt);
[25993]1326 drvR3IntNetUpdateMacAddress(pThis);
1327 drvR3IntNetSetActive(pThis, true /* fActive */);
[6001]1328 }
[1]1329}
1330
1331
1332/**
[28258]1333 * @interface_method_impl{PDMDRVREG,pfnRelocate}
1334 */
1335static DECLCALLBACK(void) drvR3IntNetRelocate(PPDMDRVINS pDrvIns, RTGCINTPTR offDelta)
1336{
1337 /* nothing to do here yet */
[62906]1338 RT_NOREF(pDrvIns, offDelta);
[28258]1339}
1340
1341
1342/**
[1]1343 * Destruct a driver instance.
1344 *
1345 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
1346 * resources can be freed correctly.
1347 *
1348 * @param pDrvIns The driver instance data.
1349 */
[25993]1350static DECLCALLBACK(void) drvR3IntNetDestruct(PPDMDRVINS pDrvIns)
[1]1351{
[25993]1352 LogFlow(("drvR3IntNetDestruct\n"));
[11267]1353 PDRVINTNET pThis = PDMINS_2_DATA(pDrvIns, PDRVINTNET);
[26001]1354 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
[1]1355
1356 /*
[29669]1357 * Indicate to the receive thread that it's time to quit.
[1]1358 */
[28332]1359 ASMAtomicXchgSize(&pThis->enmRecvState, RECVSTATE_TERMINATE);
[1]1360 ASMAtomicXchgSize(&pThis->fLinkDown, true);
[28328]1361 RTSEMEVENT hRecvEvt = pThis->hRecvEvt;
1362 pThis->hRecvEvt = NIL_RTSEMEVENT;
[1]1363
[29669]1364 if (hRecvEvt != NIL_RTSEMEVENT)
1365 RTSemEventSignal(hRecvEvt);
1366
[1]1367 if (pThis->hIf != INTNET_HANDLE_INVALID)
1368 {
[97046]1369#if defined(RT_OS_DARWIN) && defined(VBOX_WITH_INTNET_SERVICE_IN_R3)
1370 if (!pThis->fIntNetR3Svc) /* The R3 service case is handled b the hRecEvt event semaphore. */
1371#endif
1372 {
1373 INTNETIFABORTWAITREQ AbortWaitReq;
1374 AbortWaitReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
1375 AbortWaitReq.Hdr.cbReq = sizeof(AbortWaitReq);
1376 AbortWaitReq.pSession = NIL_RTR0PTR;
1377 AbortWaitReq.hIf = pThis->hIf;
1378 AbortWaitReq.fNoMoreWaits = true;
1379 int rc = drvR3IntNetCallSvc(pThis, VMMR0_DO_INTNET_IF_ABORT_WAIT, &AbortWaitReq, sizeof(AbortWaitReq));
1380 AssertMsg(RT_SUCCESS(rc) || rc == VERR_SEM_DESTROYED, ("%Rrc\n", rc)); RT_NOREF_PV(rc);
1381 }
[1]1382 }
1383
1384 /*
[29669]1385 * Wait for the threads to terminate.
[1]1386 */
[28332]1387 if (pThis->pXmitThread)
1388 {
[91945]1389 int rc = PDMDrvHlpThreadDestroy(pDrvIns, pThis->pXmitThread, NULL);
[28332]1390 AssertRC(rc);
1391 pThis->pXmitThread = NULL;
1392 }
1393
[28328]1394 if (pThis->hRecvThread != NIL_RTTHREAD)
[1]1395 {
[28328]1396 int rc = RTThreadWait(pThis->hRecvThread, 5000, NULL);
[1]1397 AssertRC(rc);
[28328]1398 pThis->hRecvThread = NIL_RTTHREAD;
[1]1399 }
1400
1401 /*
[29669]1402 * Deregister statistics in case we're being detached.
[1]1403 */
[27718]1404 if (pThis->pBufR3)
1405 {
1406 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->pBufR3->Recv.cStatFrames);
1407 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->pBufR3->Recv.cbStatWritten);
1408 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->pBufR3->Recv.cOverflows);
1409 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->pBufR3->Send.cStatFrames);
1410 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->pBufR3->Send.cbStatWritten);
1411 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->pBufR3->Send.cOverflows);
1412 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->pBufR3->cStatYieldsOk);
1413 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->pBufR3->cStatYieldsNok);
1414 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->pBufR3->cStatLost);
1415 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->pBufR3->cStatBadFrames);
[30587]1416 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->pBufR3->StatSend1);
1417 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->pBufR3->StatSend2);
1418 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->pBufR3->StatRecv1);
1419 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->pBufR3->StatRecv2);
[60076]1420 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->pBufR3->StatReserved);
[28134]1421 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatReceivedGso);
1422 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatSentGso);
[97813]1423 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatSentR0);
[20708]1424#ifdef VBOX_WITH_STATISTICS
[27718]1425 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatReceive);
1426 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatTransmit);
[20708]1427#endif
[60076]1428 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatXmitWakeupR0);
1429 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatXmitWakeupR3);
1430 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatXmitProcessRing);
[27718]1431 }
[29669]1432
1433 /*
1434 * Close the interface
1435 */
1436 if (pThis->hIf != INTNET_HANDLE_INVALID)
1437 {
1438 INTNETIFCLOSEREQ CloseReq;
1439 CloseReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
1440 CloseReq.Hdr.cbReq = sizeof(CloseReq);
1441 CloseReq.pSession = NIL_RTR0PTR;
1442 CloseReq.hIf = pThis->hIf;
1443 pThis->hIf = INTNET_HANDLE_INVALID;
[97046]1444 int rc = drvR3IntNetCallSvc(pThis, VMMR0_DO_INTNET_IF_CLOSE, &CloseReq, sizeof(CloseReq));
[29669]1445 AssertRC(rc);
1446 }
1447
[97046]1448#if defined(RT_OS_DARWIN) && defined(VBOX_WITH_INTNET_SERVICE_IN_R3)
1449 if (pThis->fIntNetR3Svc)
1450 {
1451 /* Unmap the shared buffer. */
1452 munmap(pThis->pBufR3, pThis->cbBuf);
1453 xpc_connection_cancel(pThis->hXpcCon);
1454 pThis->fIntNetR3Svc = false;
1455 pThis->hXpcCon = NULL;
1456 }
1457#endif
[29669]1458
1459 /*
1460 * Destroy the semaphores, S/G cache and xmit lock.
1461 */
1462 if (hRecvEvt != NIL_RTSEMEVENT)
1463 RTSemEventDestroy(hRecvEvt);
1464
1465 if (pThis->hXmitEvt != NIL_SUPSEMEVENT)
1466 {
1467 SUPSemEventClose(pThis->pSupDrvSession, pThis->hXmitEvt);
1468 pThis->hXmitEvt = NIL_SUPSEMEVENT;
1469 }
1470
1471 RTMemCacheDestroy(pThis->hSgCache);
1472 pThis->hSgCache = NIL_RTMEMCACHE;
1473
[90330]1474 if (PDMDrvHlpCritSectIsInitialized(pDrvIns, &pThis->XmitLock))
1475 PDMDrvHlpCritSectDelete(pDrvIns, &pThis->XmitLock);
[1]1476}
1477
1478
1479/**
[36075]1480 * Queries a policy config value and translates it into open network flag.
1481 *
1482 * @returns VBox status code (error set on failure).
1483 * @param pDrvIns The driver instance.
1484 * @param pszName The value name.
1485 * @param paFlags The open network flag descriptors.
1486 * @param cFlags The number of descriptors.
[36084]1487 * @param fFlags The fixed flag.
[36075]1488 * @param pfFlags The flags variable to update.
1489 */
[36084]1490static int drvIntNetR3CfgGetPolicy(PPDMDRVINS pDrvIns, const char *pszName, PCDRVINTNETFLAG paFlags, size_t cFlags,
1491 uint32_t fFixedFlag, uint32_t *pfFlags)
[36075]1492{
[91872]1493 PCPDMDRVHLPR3 pHlp = pDrvIns->pHlpR3;
1494
[36075]1495 char szValue[64];
[91872]1496 int rc = pHlp->pfnCFGMQueryString(pDrvIns->pCfg, pszName, szValue, sizeof(szValue));
[36084]1497 if (RT_FAILURE(rc))
[36075]1498 {
[36084]1499 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
[36075]1500 return VINF_SUCCESS;
[36084]1501 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
1502 N_("Configuration error: Failed to query value of \"%s\""), pszName);
1503 }
[36075]1504
[36084]1505 /*
1506 * Check for +fixed first, so it can be stripped off.
1507 */
1508 char *pszSep = strpbrk(szValue, "+,;");
[36085]1509 if (pszSep)
[36084]1510 {
1511 *pszSep++ = '\0';
1512 const char *pszFixed = RTStrStripL(pszSep);
1513 if (strcmp(pszFixed, "fixed"))
1514 {
1515 *pszSep = '+';
1516 return PDMDrvHlpVMSetError(pDrvIns, VERR_INVALID_PARAMETER, RT_SRC_POS,
1517 N_("Configuration error: The value of \"%s\" is unknown: \"%s\""), pszName, szValue);
1518 }
1519 *pfFlags |= fFixedFlag;
1520 RTStrStripR(szValue);
[36075]1521 }
[36084]1522
1523 /*
1524 * Match against the flag values.
1525 */
1526 size_t i = cFlags;
1527 while (i-- > 0)
1528 if (!strcmp(paFlags[i].pszChoice, szValue))
1529 {
1530 *pfFlags |= paFlags[i].fFlag;
1531 return VINF_SUCCESS;
1532 }
1533
1534 if (!strcmp(szValue, "none"))
[36075]1535 return VINF_SUCCESS;
[36084]1536
1537 if (!strcmp(szValue, "fixed"))
1538 {
1539 *pfFlags |= fFixedFlag;
1540 return VINF_SUCCESS;
1541 }
1542
1543 return PDMDrvHlpVMSetError(pDrvIns, VERR_INVALID_PARAMETER, RT_SRC_POS,
1544 N_("Configuration error: The value of \"%s\" is unknown: \"%s\""), pszName, szValue);
[36075]1545}
1546
1547
1548/**
[1]1549 * Construct a TAP network transport driver instance.
1550 *
[22277]1551 * @copydoc FNPDMDRVCONSTRUCT
[1]1552 */
[26173]1553static DECLCALLBACK(int) drvR3IntNetConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
[1]1554{
[62906]1555 RT_NOREF(fFlags);
1556 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
[91872]1557 PDRVINTNET pThis = PDMINS_2_DATA(pDrvIns, PDRVINTNET);
1558 PCPDMDRVHLPR3 pHlp = pDrvIns->pHlpR3;
[13416]1559 bool f;
[1]1560
1561 /*
1562 * Init the static parts.
1563 */
[25993]1564 pThis->pDrvInsR3 = pDrvIns;
[32167]1565#ifdef VBOX_WITH_DRVINTNET_IN_R0
1566 pThis->pDrvInsR0 = PDMDRVINS_2_R0PTR(pDrvIns);
1567#endif
[25993]1568 pThis->hIf = INTNET_HANDLE_INVALID;
[28328]1569 pThis->hRecvThread = NIL_RTTHREAD;
1570 pThis->hRecvEvt = NIL_RTSEMEVENT;
1571 pThis->pXmitThread = NULL;
1572 pThis->hXmitEvt = NIL_SUPSEMEVENT;
1573 pThis->pSupDrvSession = PDMDrvHlpGetSupDrvSession(pDrvIns);
[26574]1574 pThis->hSgCache = NIL_RTMEMCACHE;
[28332]1575 pThis->enmRecvState = RECVSTATE_SUSPENDED;
[25993]1576 pThis->fActivateEarlyDeactivateLate = false;
[26137]1577 /* IBase* */
1578 pDrvIns->IBase.pfnQueryInterface = drvR3IntNetIBase_QueryInterface;
[93652]1579#ifdef VBOX_WITH_DRVINTNET_IN_R0
[26137]1580 pThis->IBaseR0.pfnQueryInterface = drvR3IntNetIBaseR0_QueryInterface;
1581 pThis->IBaseRC.pfnQueryInterface = drvR3IntNetIBaseRC_QueryInterface;
[93652]1582#endif
[26305]1583 /* INetworkUp */
[28332]1584 pThis->INetworkUpR3.pfnBeginXmit = drvIntNetUp_BeginXmit;
1585 pThis->INetworkUpR3.pfnAllocBuf = drvIntNetUp_AllocBuf;
1586 pThis->INetworkUpR3.pfnFreeBuf = drvIntNetUp_FreeBuf;
1587 pThis->INetworkUpR3.pfnSendBuf = drvIntNetUp_SendBuf;
1588 pThis->INetworkUpR3.pfnEndXmit = drvIntNetUp_EndXmit;
1589 pThis->INetworkUpR3.pfnSetPromiscuousMode = drvIntNetUp_SetPromiscuousMode;
[26574]1590 pThis->INetworkUpR3.pfnNotifyLinkChanged = drvR3IntNetUp_NotifyLinkChanged;
[1]1591
1592 /*
1593 * Validate the config.
1594 */
[36075]1595 PDMDRV_VALIDATE_CONFIG_RETURN(pDrvIns,
1596 "Network"
1597 "|Trunk"
1598 "|TrunkType"
1599 "|ReceiveBufferSize"
1600 "|SendBufferSize"
1601 "|SharedMacOnWire"
1602 "|RestrictAccess"
1603 "|RequireExactPolicyMatch"
1604 "|RequireAsRestrictivePolicy"
1605 "|AccessPolicy"
1606 "|PromiscPolicyClients"
1607 "|PromiscPolicyHost"
1608 "|PromiscPolicyWire"
1609 "|IfPolicyPromisc"
1610 "|TrunkPolicyHost"
1611 "|TrunkPolicyWire"
1612 "|IsService"
[37979]1613 "|IgnoreConnectFailure"
1614 "|Workaround1",
[36075]1615 "");
[1]1616
1617 /*
1618 * Check that no-one is attached to us.
1619 */
[23918]1620 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
[22277]1621 ("Configuration error: Not possible to attach anything to this driver!\n"),
1622 VERR_PDM_DRVINS_NO_ATTACH);
[1]1623
1624 /*
1625 * Query the network port interface.
1626 */
[26305]1627 pThis->pIAboveNet = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMINETWORKDOWN);
1628 if (!pThis->pIAboveNet)
[1]1629 {
1630 AssertMsgFailed(("Configuration error: the above device/driver didn't export the network port interface!\n"));
1631 return VERR_PDM_MISSING_INTERFACE_ABOVE;
1632 }
[26305]1633 pThis->pIAboveConfigR3 = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMINETWORKCONFIG);
[1]1634
1635 /*
1636 * Read the configuration.
1637 */
[5283]1638 INTNETOPENREQ OpenReq;
[36075]1639 RT_ZERO(OpenReq);
[5283]1640 OpenReq.Hdr.cbReq = sizeof(OpenReq);
1641 OpenReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
[10762]1642 OpenReq.pSession = NIL_RTR0PTR;
[5283]1643
[10451]1644 /** @cfgm{Network, string}
1645 * The name of the internal network to connect to.
1646 */
[91872]1647 int rc = pHlp->pfnCFGMQueryString(pCfg, "Network", OpenReq.szNetwork, sizeof(OpenReq.szNetwork));
[11266]1648 if (RT_FAILURE(rc))
[1599]1649 return PDMDRV_SET_ERROR(pDrvIns, rc,
1650 N_("Configuration error: Failed to get the \"Network\" value"));
[5283]1651 strcpy(pThis->szNetwork, OpenReq.szNetwork);
[1]1652
[10451]1653 /** @cfgm{TrunkType, uint32_t, kIntNetTrunkType_None}
1654 * The trunk connection type see INTNETTRUNKTYPE.
1655 */
1656 uint32_t u32TrunkType;
[91872]1657 rc = pHlp->pfnCFGMQueryU32(pCfg, "TrunkType", &u32TrunkType);
[10451]1658 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1659 u32TrunkType = kIntNetTrunkType_None;
[11266]1660 else if (RT_FAILURE(rc))
[10451]1661 return PDMDRV_SET_ERROR(pDrvIns, rc,
1662 N_("Configuration error: Failed to get the \"TrunkType\" value"));
1663 OpenReq.enmTrunkType = (INTNETTRUNKTYPE)u32TrunkType;
1664
1665 /** @cfgm{Trunk, string, ""}
1666 * The name of the trunk connection.
1667 */
[91872]1668 rc = pHlp->pfnCFGMQueryString(pCfg, "Trunk", OpenReq.szTrunk, sizeof(OpenReq.szTrunk));
[10451]1669 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1670 OpenReq.szTrunk[0] = '\0';
[11266]1671 else if (RT_FAILURE(rc))
[10451]1672 return PDMDRV_SET_ERROR(pDrvIns, rc,
1673 N_("Configuration error: Failed to get the \"Trunk\" value"));
1674
[36075]1675 OpenReq.fFlags = 0;
1676
1677 /** @cfgm{SharedMacOnWire, boolean, false}
1678 * Whether to shared the MAC address of the host interface when using the wire. When
1679 * attaching to a wireless NIC this option is usually a requirement.
[10451]1680 */
[36075]1681 bool fSharedMacOnWire;
[91872]1682 rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "SharedMacOnWire", &fSharedMacOnWire, false);
[13416]1683 if (RT_FAILURE(rc))
1684 return PDMDRV_SET_ERROR(pDrvIns, rc,
[36075]1685 N_("Configuration error: Failed to get the \"SharedMacOnWire\" value"));
1686 if (fSharedMacOnWire)
1687 OpenReq.fFlags |= INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE;
[13416]1688
[36075]1689 /** @cfgm{RestrictAccess, boolean, true}
1690 * Whether to restrict the access to the network or if it should be public.
1691 * Everyone on the computer can connect to a public network.
1692 * @deprecated Use AccessPolicy instead.
1693 */
[91872]1694 rc = pHlp->pfnCFGMQueryBool(pCfg, "RestrictAccess", &f);
[36075]1695 if (RT_SUCCESS(rc))
1696 {
1697 if (f)
1698 OpenReq.fFlags |= INTNET_OPEN_FLAGS_ACCESS_RESTRICTED;
1699 else
1700 OpenReq.fFlags |= INTNET_OPEN_FLAGS_ACCESS_PUBLIC;
[36080]1701 OpenReq.fFlags |= INTNET_OPEN_FLAGS_ACCESS_FIXED;
[36075]1702 }
1703 else if (rc != VERR_CFGM_VALUE_NOT_FOUND)
[13416]1704 return PDMDRV_SET_ERROR(pDrvIns, rc,
[36075]1705 N_("Configuration error: Failed to get the \"RestrictAccess\" value"));
[13416]1706
[38630]1707 /** @cfgm{RequireExactPolicyMatch, boolean, false}
1708 * Whether to require that the current security and promiscuous policies of
1709 * the network is exactly as the ones specified in this open network
1710 * request. Use this with RequireAsRestrictivePolicy to prevent
1711 * restrictions from being lifted. If no further policy changes are
1712 * desired, apply the relevant fixed flags. */
[91872]1713 rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "RequireExactPolicyMatch", &f, false);
[38630]1714 if (RT_FAILURE(rc))
1715 return PDMDRV_SET_ERROR(pDrvIns, rc,
1716 N_("Configuration error: Failed to get the \"RequireExactPolicyMatch\" value"));
1717 if (f)
1718 OpenReq.fFlags |= INTNET_OPEN_FLAGS_REQUIRE_EXACT;
[13416]1719
[38630]1720 /** @cfgm{RequireAsRestrictivePolicy, boolean, false}
1721 * Whether to require that the security and promiscuous policies of the
1722 * network is at least as restrictive as specified this request specifies
1723 * and prevent them being lifted later on.
1724 */
[91872]1725 rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "RequireAsRestrictivePolicy", &f, false);
[38630]1726 if (RT_FAILURE(rc))
1727 return PDMDRV_SET_ERROR(pDrvIns, rc,
1728 N_("Configuration error: Failed to get the \"RequireAsRestrictivePolicy\" value"));
1729 if (f)
1730 OpenReq.fFlags |= INTNET_OPEN_FLAGS_REQUIRE_AS_RESTRICTIVE_POLICIES;
1731
[36075]1732 /** @cfgm{AccessPolicy, string, "none"}
[36084]1733 * The access policy of the network:
1734 * public, public+fixed, restricted, restricted+fixed, none or fixed.
1735 *
1736 * A "public" network is accessible to everyone on the same host, while a
[36075]1737 * "restricted" one is only accessible to VMs & services started by the
1738 * same user. The "none" policy, which is the default, means no policy
1739 * change or choice is made and that the current (existing network) or
1740 * default (new) policy should be used. */
1741 static const DRVINTNETFLAG s_aAccessPolicyFlags[] =
1742 {
1743 { "public", INTNET_OPEN_FLAGS_ACCESS_PUBLIC },
1744 { "restricted", INTNET_OPEN_FLAGS_ACCESS_RESTRICTED }
1745 };
1746 rc = drvIntNetR3CfgGetPolicy(pDrvIns, "AccessPolicy", &s_aAccessPolicyFlags[0], RT_ELEMENTS(s_aAccessPolicyFlags),
[36084]1747 INTNET_OPEN_FLAGS_ACCESS_FIXED, &OpenReq.fFlags);
[36075]1748 AssertRCReturn(rc, rc);
[13416]1749
[36075]1750 /** @cfgm{PromiscPolicyClients, string, "none"}
1751 * The network wide promiscuous mode policy for client (non-trunk)
[36084]1752 * interfaces: allow, allow+fixed, deny, deny+fixed, none or fixed. */
[36075]1753 static const DRVINTNETFLAG s_aPromiscPolicyClient[] =
1754 {
1755 { "allow", INTNET_OPEN_FLAGS_PROMISC_ALLOW_CLIENTS },
1756 { "deny", INTNET_OPEN_FLAGS_PROMISC_DENY_CLIENTS }
1757 };
1758 rc = drvIntNetR3CfgGetPolicy(pDrvIns, "PromiscPolicyClients", &s_aPromiscPolicyClient[0], RT_ELEMENTS(s_aPromiscPolicyClient),
[36084]1759 INTNET_OPEN_FLAGS_PROMISC_FIXED, &OpenReq.fFlags);
[36075]1760 AssertRCReturn(rc, rc);
1761 /** @cfgm{PromiscPolicyHost, string, "none"}
1762 * The promiscuous mode policy for the trunk-host
[36084]1763 * connection: allow, allow+fixed, deny, deny+fixed, none or fixed. */
[36075]1764 static const DRVINTNETFLAG s_aPromiscPolicyHost[] =
1765 {
1766 { "allow", INTNET_OPEN_FLAGS_PROMISC_ALLOW_TRUNK_HOST },
1767 { "deny", INTNET_OPEN_FLAGS_PROMISC_DENY_TRUNK_HOST }
1768 };
1769 rc = drvIntNetR3CfgGetPolicy(pDrvIns, "PromiscPolicyHost", &s_aPromiscPolicyHost[0], RT_ELEMENTS(s_aPromiscPolicyHost),
[36084]1770 INTNET_OPEN_FLAGS_PROMISC_FIXED, &OpenReq.fFlags);
[36075]1771 AssertRCReturn(rc, rc);
1772 /** @cfgm{PromiscPolicyWire, string, "none"}
1773 * The promiscuous mode policy for the trunk-host
[36084]1774 * connection: allow, allow+fixed, deny, deny+fixed, none or fixed. */
[36075]1775 static const DRVINTNETFLAG s_aPromiscPolicyWire[] =
1776 {
1777 { "allow", INTNET_OPEN_FLAGS_PROMISC_ALLOW_TRUNK_WIRE },
1778 { "deny", INTNET_OPEN_FLAGS_PROMISC_DENY_TRUNK_WIRE }
1779 };
1780 rc = drvIntNetR3CfgGetPolicy(pDrvIns, "PromiscPolicyWire", &s_aPromiscPolicyWire[0], RT_ELEMENTS(s_aPromiscPolicyWire),
[36084]1781 INTNET_OPEN_FLAGS_PROMISC_FIXED, &OpenReq.fFlags);
[36075]1782 AssertRCReturn(rc, rc);
[13416]1783
1784
[36075]1785 /** @cfgm{IfPolicyPromisc, string, "none"}
1786 * The promiscuous mode policy for this
[36084]1787 * interface: deny, deny+fixed, allow-all, allow-all+fixed, allow-network,
1788 * allow-network+fixed, none or fixed. */
[36075]1789 static const DRVINTNETFLAG s_aIfPolicyPromisc[] =
1790 {
[36082]1791 { "allow-all", INTNET_OPEN_FLAGS_IF_PROMISC_ALLOW | INTNET_OPEN_FLAGS_IF_PROMISC_SEE_TRUNK },
1792 { "allow-network", INTNET_OPEN_FLAGS_IF_PROMISC_ALLOW | INTNET_OPEN_FLAGS_IF_PROMISC_NO_TRUNK },
[36084]1793 { "deny", INTNET_OPEN_FLAGS_IF_PROMISC_DENY }
[36075]1794 };
1795 rc = drvIntNetR3CfgGetPolicy(pDrvIns, "IfPolicyPromisc", &s_aIfPolicyPromisc[0], RT_ELEMENTS(s_aIfPolicyPromisc),
[36084]1796 INTNET_OPEN_FLAGS_IF_FIXED, &OpenReq.fFlags);
[36075]1797 AssertRCReturn(rc, rc);
[13416]1798
1799
[36075]1800 /** @cfgm{TrunkPolicyHost, string, "none"}
[36084]1801 * The trunk-host policy: promisc, promisc+fixed, enabled, enabled+fixed,
1802 * disabled, disabled+fixed, none or fixed
1803 *
[36075]1804 * This can be used to prevent packages to be routed to the host. */
1805 static const DRVINTNETFLAG s_aTrunkPolicyHost[] =
1806 {
[36084]1807 { "promisc", INTNET_OPEN_FLAGS_TRUNK_HOST_ENABLED | INTNET_OPEN_FLAGS_TRUNK_HOST_PROMISC_MODE },
1808 { "enabled", INTNET_OPEN_FLAGS_TRUNK_HOST_ENABLED },
1809 { "disabled", INTNET_OPEN_FLAGS_TRUNK_HOST_DISABLED }
[36075]1810 };
1811 rc = drvIntNetR3CfgGetPolicy(pDrvIns, "TrunkPolicyHost", &s_aTrunkPolicyHost[0], RT_ELEMENTS(s_aTrunkPolicyHost),
[36084]1812 INTNET_OPEN_FLAGS_TRUNK_FIXED, &OpenReq.fFlags);
[36075]1813 AssertRCReturn(rc, rc);
1814 /** @cfgm{TrunkPolicyWire, string, "none"}
[36084]1815 * The trunk-host policy: promisc, promisc+fixed, enabled, enabled+fixed,
1816 * disabled, disabled+fixed, none or fixed.
1817 *
1818 * This can be used to prevent packages to be routed to the wire. */
[36075]1819 static const DRVINTNETFLAG s_aTrunkPolicyWire[] =
1820 {
[36084]1821 { "promisc", INTNET_OPEN_FLAGS_TRUNK_WIRE_ENABLED | INTNET_OPEN_FLAGS_TRUNK_WIRE_PROMISC_MODE },
1822 { "enabled", INTNET_OPEN_FLAGS_TRUNK_WIRE_ENABLED },
1823 { "disabled", INTNET_OPEN_FLAGS_TRUNK_WIRE_DISABLED }
[36075]1824 };
1825 rc = drvIntNetR3CfgGetPolicy(pDrvIns, "TrunkPolicyWire", &s_aTrunkPolicyWire[0], RT_ELEMENTS(s_aTrunkPolicyWire),
[36084]1826 INTNET_OPEN_FLAGS_TRUNK_FIXED, &OpenReq.fFlags);
[36075]1827 AssertRCReturn(rc, rc);
[34335]1828
1829
[28095]1830 /** @cfgm{ReceiveBufferSize, uint32_t, 318 KB}
[10451]1831 * The size of the receive buffer.
1832 */
[91872]1833 rc = pHlp->pfnCFGMQueryU32(pCfg, "ReceiveBufferSize", &OpenReq.cbRecv);
[1]1834 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
[28095]1835 OpenReq.cbRecv = 318 * _1K ;
[11266]1836 else if (RT_FAILURE(rc))
[1599]1837 return PDMDRV_SET_ERROR(pDrvIns, rc,
1838 N_("Configuration error: Failed to get the \"ReceiveBufferSize\" value"));
[1]1839
[28095]1840 /** @cfgm{SendBufferSize, uint32_t, 196 KB}
[10451]1841 * The size of the send (transmit) buffer.
[11259]1842 * This should be more than twice the size of the larges frame size because
1843 * the ring buffer is very simple and doesn't support splitting up frames
1844 * nor inserting padding. So, if this is too close to the frame size the
1845 * header will fragment the buffer such that the frame won't fit on either
1846 * side of it and the code will get very upset about it all.
[10451]1847 */
[91872]1848 rc = pHlp->pfnCFGMQueryU32(pCfg, "SendBufferSize", &OpenReq.cbSend);
[1]1849 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
[28095]1850 OpenReq.cbSend = RT_ALIGN_Z(VBOX_MAX_GSO_SIZE * 3, _1K);
[11266]1851 else if (RT_FAILURE(rc))
[1599]1852 return PDMDRV_SET_ERROR(pDrvIns, rc,
1853 N_("Configuration error: Failed to get the \"SendBufferSize\" value"));
[28095]1854 if (OpenReq.cbSend < 128)
[5283]1855 return PDMDRV_SET_ERROR(pDrvIns, rc,
[6300]1856 N_("Configuration error: The \"SendBufferSize\" value is too small"));
[29579]1857 if (OpenReq.cbSend < VBOX_MAX_GSO_SIZE * 3)
[28095]1858 LogRel(("DrvIntNet: Warning! SendBufferSize=%u, Recommended minimum size %u butes.\n", OpenReq.cbSend, VBOX_MAX_GSO_SIZE * 4));
[1]1859
[10451]1860 /** @cfgm{IsService, boolean, true}
1861 * This alterns the way the thread is suspended and resumed. When it's being used by
1862 * a service such as LWIP/iSCSI it shouldn't suspend immediately like for a NIC.
1863 */
[91872]1864 rc = pHlp->pfnCFGMQueryBool(pCfg, "IsService", &pThis->fActivateEarlyDeactivateLate);
[6001]1865 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
[9594]1866 pThis->fActivateEarlyDeactivateLate = false;
[11266]1867 else if (RT_FAILURE(rc))
[6001]1868 return PDMDRV_SET_ERROR(pDrvIns, rc,
1869 N_("Configuration error: Failed to get the \"IsService\" value"));
1870
[36075]1871
1872 /** @cfgm{IgnoreConnectFailure, boolean, false}
1873 * When set only raise a runtime error if we cannot connect to the internal
1874 * network. */
1875 bool fIgnoreConnectFailure;
[91872]1876 rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "IgnoreConnectFailure", &fIgnoreConnectFailure, false);
[36075]1877 if (RT_FAILURE(rc))
1878 return PDMDRV_SET_ERROR(pDrvIns, rc,
1879 N_("Configuration error: Failed to get the \"IgnoreConnectFailure\" value"));
1880
[37979]1881 /** @cfgm{Workaround1, boolean, depends}
1882 * Enables host specific workarounds, the default is depends on the whether
1883 * we think the host requires it or not.
1884 */
1885 bool fWorkaround1 = false;
1886#ifdef RT_OS_DARWIN
1887 if (OpenReq.fFlags & INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE)
1888 {
1889 char szKrnlVer[256];
1890 RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szKrnlVer, sizeof(szKrnlVer));
1891 if (strcmp(szKrnlVer, "10.7.0") >= 0)
1892 {
1893 LogRel(("IntNet#%u: Enables the workaround (ip_tos=0) for the little endian ip header checksum problem\n"));
1894 fWorkaround1 = true;
1895 }
1896 }
1897#endif
[91872]1898 rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "Workaround1", &fWorkaround1, fWorkaround1);
[37979]1899 if (RT_FAILURE(rc))
1900 return PDMDRV_SET_ERROR(pDrvIns, rc,
1901 N_("Configuration error: Failed to get the \"Workaround1\" value"));
1902 if (fWorkaround1)
1903 OpenReq.fFlags |= INTNET_OPEN_FLAGS_WORKAROUND_1;
1904
[36075]1905 LogRel(("IntNet#%u: szNetwork={%s} enmTrunkType=%d szTrunk={%s} fFlags=%#x cbRecv=%u cbSend=%u fIgnoreConnectFailure=%RTbool\n",
[10451]1906 pDrvIns->iInstance, OpenReq.szNetwork, OpenReq.enmTrunkType, OpenReq.szTrunk, OpenReq.fFlags,
[36075]1907 OpenReq.cbRecv, OpenReq.cbSend, fIgnoreConnectFailure));
[10451]1908
[10762]1909#ifdef RT_OS_DARWIN
1910 /* Temporary hack: attach to a network with the name 'if=en0' and you're hitting the wire. */
1911 if ( !OpenReq.szTrunk[0]
1912 && OpenReq.enmTrunkType == kIntNetTrunkType_None
[46326]1913 && !strncmp(pThis->szNetwork, RT_STR_TUPLE("if=en"))
[10762]1914 && RT_C_IS_DIGIT(pThis->szNetwork[sizeof("if=en") - 1])
1915 && !pThis->szNetwork[sizeof("if=en")])
1916 {
1917 OpenReq.enmTrunkType = kIntNetTrunkType_NetFlt;
1918 strcpy(OpenReq.szTrunk, &pThis->szNetwork[sizeof("if=") - 1]);
1919 }
[11059]1920 /* Temporary hack: attach to a network with the name 'wif=en0' and you're on the air. */
1921 if ( !OpenReq.szTrunk[0]
1922 && OpenReq.enmTrunkType == kIntNetTrunkType_None
[46326]1923 && !strncmp(pThis->szNetwork, RT_STR_TUPLE("wif=en"))
[11059]1924 && RT_C_IS_DIGIT(pThis->szNetwork[sizeof("wif=en") - 1])
1925 && !pThis->szNetwork[sizeof("wif=en")])
1926 {
1927 OpenReq.enmTrunkType = kIntNetTrunkType_NetFlt;
1928 OpenReq.fFlags |= INTNET_OPEN_FLAGS_SHARED_MAC_ON_WIRE;
1929 strcpy(OpenReq.szTrunk, &pThis->szNetwork[sizeof("wif=") - 1]);
1930 }
[18323]1931#endif /* DARWIN */
[11849]1932
[1]1933 /*
[28258]1934 * Create the event semaphore, S/G cache and xmit critsect.
[1]1935 */
[28328]1936 rc = RTSemEventCreate(&pThis->hRecvEvt);
[11266]1937 if (RT_FAILURE(rc))
[1]1938 return rc;
[26574]1939 rc = RTMemCacheCreate(&pThis->hSgCache, sizeof(PDMSCATTERGATHER), 0, UINT32_MAX, NULL, NULL, pThis, 0);
1940 if (RT_FAILURE(rc))
1941 return rc;
[28258]1942 rc = PDMDrvHlpCritSectInit(pDrvIns, &pThis->XmitLock, RT_SRC_POS, "IntNetXmit");
1943 if (RT_FAILURE(rc))
1944 return rc;
[1]1945
1946 /*
1947 * Create the interface.
1948 */
[97046]1949 if (SUPR3IsDriverless())
1950 {
1951#if defined(RT_OS_DARWIN) && defined(VBOX_WITH_INTNET_SERVICE_IN_R3)
1952 xpc_connection_t hXpcCon = xpc_connection_create(INTNET_R3_SVC_NAME, NULL);
1953 xpc_connection_set_event_handler(hXpcCon, ^(xpc_object_t hObj) {
1954 if (xpc_get_type(hObj) == XPC_TYPE_ERROR)
1955 {
[97058]1956 /** @todo Error handling - reconnecting. */
[97046]1957 }
1958 else
1959 {
1960 /* Out of band messages should only come when there is something to receive. */
1961 RTSemEventSignal(pThis->hRecvEvt);
1962 }
1963 });
1964
[97089]1965 xpc_connection_resume(hXpcCon);
[97046]1966 pThis->hXpcCon = hXpcCon;
1967 pThis->fIntNetR3Svc = true;
1968#else
1969 /** @todo This is probably not good enough for doing fuzz testing, but later... */
[92724]1970 return PDMDrvHlpVMSetError(pDrvIns, VERR_SUP_DRIVERLESS, RT_SRC_POS,
1971 N_("Cannot attach to '%s' in driverless mode"), pThis->szNetwork);
[97046]1972#endif
1973 }
[5283]1974 OpenReq.hIf = INTNET_HANDLE_INVALID;
[97046]1975 rc = drvR3IntNetCallSvc(pThis, VMMR0_DO_INTNET_OPEN, &OpenReq, sizeof(OpenReq));
[11266]1976 if (RT_FAILURE(rc))
[34330]1977 {
[34335]1978 if (fIgnoreConnectFailure)
1979 {
1980 /*
1981 * During VM restore it is fatal if the network is not available because the
1982 * VM settings are locked and the user has no chance to fix network settings.
1983 * Therefore don't abort but just raise a runtime warning.
1984 */
[36075]1985 PDMDrvHlpVMSetRuntimeError(pDrvIns, 0 /*fFlags*/, "HostIfNotConnecting",
1986 N_ ("Cannot connect to the network interface '%s'. The virtual "
1987 "network card will appear to work but the guest will not "
1988 "be able to connect. Please choose a different network in the "
1989 "network settings"), OpenReq.szTrunk);
[34330]1990
[34335]1991 return VERR_PDM_NO_ATTACHED_DRIVER;
1992 }
[36075]1993 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
1994 N_("Failed to open/create the internal network '%s'"), pThis->szNetwork);
[34330]1995 }
1996
[5283]1997 AssertRelease(OpenReq.hIf != INTNET_HANDLE_INVALID);
1998 pThis->hIf = OpenReq.hIf;
[1]1999 Log(("IntNet%d: hIf=%RX32 '%s'\n", pDrvIns->iInstance, pThis->hIf, pThis->szNetwork));
[17961]2000
[1]2001 /*
2002 * Get default buffer.
2003 */
[97046]2004 rc = drvR3IntNetMapBufferPointers(pThis);
[11266]2005 if (RT_FAILURE(rc))
[1599]2006 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
[1610]2007 N_("Failed to get ring-3 buffer for the newly created interface to '%s'"), pThis->szNetwork);
[1]2008
2009 /*
[28332]2010 * Register statistics.
2011 */
[30587]2012 PDMDrvHlpSTAMRegCounterEx(pDrvIns, &pThis->pBufR3->Recv.cbStatWritten, "Bytes/Received", STAMUNIT_BYTES, "Number of received bytes.");
2013 PDMDrvHlpSTAMRegCounterEx(pDrvIns, &pThis->pBufR3->Send.cbStatWritten, "Bytes/Sent", STAMUNIT_BYTES, "Number of sent bytes.");
2014 PDMDrvHlpSTAMRegCounter(pDrvIns, &pThis->pBufR3->Recv.cOverflows, "Overflows/Recv", "Number overflows.");
2015 PDMDrvHlpSTAMRegCounter(pDrvIns, &pThis->pBufR3->Send.cOverflows, "Overflows/Sent", "Number overflows.");
2016 PDMDrvHlpSTAMRegCounter(pDrvIns, &pThis->pBufR3->Recv.cStatFrames, "Packets/Received", "Number of received packets.");
2017 PDMDrvHlpSTAMRegCounter(pDrvIns, &pThis->pBufR3->Send.cStatFrames, "Packets/Sent", "Number of sent packets.");
2018 PDMDrvHlpSTAMRegCounter(pDrvIns, &pThis->StatReceivedGso, "Packets/Received-Gso", "The GSO portion of the received packets.");
2019 PDMDrvHlpSTAMRegCounter(pDrvIns, &pThis->StatSentGso, "Packets/Sent-Gso", "The GSO portion of the sent packets.");
2020 PDMDrvHlpSTAMRegCounter(pDrvIns, &pThis->StatSentR0, "Packets/Sent-R0", "The ring-0 portion of the sent packets.");
[28332]2021
[30587]2022 PDMDrvHlpSTAMRegCounter(pDrvIns, &pThis->pBufR3->cStatLost, "Packets/Lost", "Number of lost packets.");
2023 PDMDrvHlpSTAMRegCounter(pDrvIns, &pThis->pBufR3->cStatYieldsNok, "YieldOk", "Number of times yielding helped fix an overflow.");
2024 PDMDrvHlpSTAMRegCounter(pDrvIns, &pThis->pBufR3->cStatYieldsOk, "YieldNok", "Number of times yielding didn't help fix an overflow.");
2025 PDMDrvHlpSTAMRegCounter(pDrvIns, &pThis->pBufR3->cStatBadFrames, "BadFrames", "Number of bad frames seed by the consumers.");
2026 PDMDrvHlpSTAMRegProfile(pDrvIns, &pThis->pBufR3->StatSend1, "Send1", "Profiling IntNetR0IfSend.");
2027 PDMDrvHlpSTAMRegProfile(pDrvIns, &pThis->pBufR3->StatSend2, "Send2", "Profiling sending to the trunk.");
2028 PDMDrvHlpSTAMRegProfile(pDrvIns, &pThis->pBufR3->StatRecv1, "Recv1", "Reserved for future receive profiling.");
2029 PDMDrvHlpSTAMRegProfile(pDrvIns, &pThis->pBufR3->StatRecv2, "Recv2", "Reserved for future receive profiling.");
2030 PDMDrvHlpSTAMRegProfile(pDrvIns, &pThis->pBufR3->StatReserved, "Reserved", "Reserved for future use.");
[28332]2031#ifdef VBOX_WITH_STATISTICS
[30587]2032 PDMDrvHlpSTAMRegProfileAdv(pDrvIns, &pThis->StatReceive, "Receive", "Profiling packet receive runs.");
2033 PDMDrvHlpSTAMRegProfile(pDrvIns, &pThis->StatTransmit, "Transmit", "Profiling packet transmit runs.");
[28720]2034#endif
[30587]2035 PDMDrvHlpSTAMRegCounter(pDrvIns, &pThis->StatXmitWakeupR0, "XmitWakeup-R0", "Xmit thread wakeups from ring-0.");
2036 PDMDrvHlpSTAMRegCounter(pDrvIns, &pThis->StatXmitWakeupR3, "XmitWakeup-R3", "Xmit thread wakeups from ring-3.");
2037 PDMDrvHlpSTAMRegCounter(pDrvIns, &pThis->StatXmitProcessRing, "XmitProcessRing", "Time xmit thread was told to process the ring.");
[28332]2038
2039 /*
[28328]2040 * Create the async I/O threads.
[10451]2041 * Note! Using a PDM thread here doesn't fit with the IsService=true operation.
[1]2042 */
[28328]2043 rc = RTThreadCreate(&pThis->hRecvThread, drvR3IntNetRecvThread, pThis, 0,
2044 RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "INTNET-RECV");
[11266]2045 if (RT_FAILURE(rc))
[1]2046 {
2047 AssertRC(rc);
2048 return rc;
2049 }
[28328]2050
2051 rc = SUPSemEventCreate(pThis->pSupDrvSession, &pThis->hXmitEvt);
2052 AssertRCReturn(rc, rc);
2053
2054 rc = PDMDrvHlpThreadCreate(pDrvIns, &pThis->pXmitThread, pThis,
2055 drvR3IntNetXmitThread, drvR3IntNetXmitWakeUp, 0, RTTHREADTYPE_IO, "INTNET-XMIT");
2056 AssertRCReturn(rc, rc);
2057
[28332]2058#ifdef VBOX_WITH_DRVINTNET_IN_R0
[28328]2059 /*
[28332]2060 * Resolve the ring-0 context interface addresses.
[28328]2061 */
[28332]2062 rc = pDrvIns->pHlpR3->pfnLdrGetR0InterfaceSymbols(pDrvIns, &pThis->INetworkUpR0, sizeof(pThis->INetworkUpR0),
2063 "drvIntNetUp_", PDMINETWORKUP_SYM_LIST);
2064 AssertLogRelRCReturn(rc, rc);
[1]2065#endif
2066
[6001]2067 /*
2068 * Activate data transmission as early as possible
2069 */
[9594]2070 if (pThis->fActivateEarlyDeactivateLate)
[6001]2071 {
[28332]2072 ASMAtomicXchgSize(&pThis->enmRecvState, RECVSTATE_RUNNING);
[28328]2073 RTSemEventSignal(pThis->hRecvEvt);
2074
[25993]2075 drvR3IntNetUpdateMacAddress(pThis);
2076 drvR3IntNetSetActive(pThis, true /* fActive */);
[6001]2077 }
2078
[28332]2079 return rc;
[1]2080}
2081
2082
[28328]2083
[1]2084/**
2085 * Internal networking transport driver registration record.
2086 */
2087const PDMDRVREG g_DrvIntNet =
2088{
2089 /* u32Version */
2090 PDM_DRVREG_VERSION,
[26166]2091 /* szName */
[1]2092 "IntNet",
[25893]2093 /* szRCMod */
[56284]2094 "VBoxDDRC.rc",
[25893]2095 /* szR0Mod */
[28332]2096 "VBoxDDR0.r0",
[1]2097 /* pszDescription */
2098 "Internal Networking Transport Driver",
2099 /* fFlags */
[28332]2100#ifdef VBOX_WITH_DRVINTNET_IN_R0
2101 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DRVREG_FLAGS_R0,
[25993]2102#else
[1]2103 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
[25993]2104#endif
[1]2105 /* fClass. */
2106 PDM_DRVREG_CLASS_NETWORK,
2107 /* cMaxInstances */
[40282]2108 ~0U,
[1]2109 /* cbInstance */
2110 sizeof(DRVINTNET),
2111 /* pfnConstruct */
[25993]2112 drvR3IntNetConstruct,
[1]2113 /* pfnDestruct */
[25993]2114 drvR3IntNetDestruct,
[25893]2115 /* pfnRelocate */
[28258]2116 drvR3IntNetRelocate,
[1]2117 /* pfnIOCtl */
2118 NULL,
2119 /* pfnPowerOn */
[25993]2120 drvR3IntNetPowerOn,
[1]2121 /* pfnReset */
2122 NULL,
2123 /* pfnSuspend */
[25993]2124 drvR3IntNetSuspend,
[1]2125 /* pfnResume */
[25993]2126 drvR3IntNetResume,
[22277]2127 /* pfnAttach */
2128 NULL,
[1]2129 /* pfnDetach */
2130 NULL,
2131 /* pfnPowerOff */
[25993]2132 drvR3IntNetPowerOff,
[22277]2133 /* pfnSoftReset */
2134 NULL,
2135 /* u32EndVersion */
2136 PDM_DRVREG_VERSION
[1]2137};
2138
[25993]2139#endif /* IN_RING3 */
2140
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use