VirtualBox

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

Last change on this file since 103914 was 102797, checked in by vboxsync, 9 months ago

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

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