VirtualBox

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

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

Copyright year updates by scm.

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