VirtualBox

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

Last change on this file since 100870 was 100870, checked in by vboxsync, 16 months ago

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