VirtualBox

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

Last change on this file since 97583 was 97338, checked in by vboxsync, 2 years ago

NetworkServices/IntNetSwitch,Devices/Network/DrvIntNet,Devices/Network/SrvIntNetR0: Don't require a dedicated waiting thread to get notified when there is something to receive but make it possible to register a callback which is called and sends an IPC message instead, bugref:10297

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette