VirtualBox

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

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

Devices/DrvIntNet,NetworkServices,Installer/darwin: First rough attempt at making the internal networking option work on macOS after all KEXTs got removed, bugref:10297 [build fix]

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