VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxNetAdp/VBoxNetAdp.c@ 55980

Last change on this file since 55980 was 52618, checked in by vboxsync, 10 years ago

HostDrivers, Runtime, Devices, Additions: TSC delta measurement and other changes resulting from bumping supdrv major version. TSC delta measurement currently disabled.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 36.2 KB
Line 
1/* $Id: VBoxNetAdp.c 52618 2014-09-05 12:07:29Z vboxsync $ */
2/** @file
3 * VBoxNetAdp - Virtual Network Adapter Driver (Host), Common Code.
4 */
5
6/*
7 * Copyright (C) 2008-2014 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/** @page pg_netadp VBoxNetAdp - Network Adapter
19 *
20 * This is a kernel module that creates a virtual interface that can be attached
21 * to an internal network.
22 *
23 * In the big picture we're one of the three trunk interface on the internal
24 * network, the one named "TAP Interface": @image html Networking_Overview.gif
25 *
26 */
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#define LOG_GROUP LOG_GROUP_NET_ADP_DRV
32#include "VBoxNetAdpInternal.h"
33
34#include <VBox/log.h>
35#include <VBox/err.h>
36#include <iprt/string.h>
37
38#ifdef VBOXANETADP_DO_NOT_USE_NETFLT
39#error "this code is broken"
40
41#include <VBox/sup.h>
42#include <iprt/assert.h>
43#include <iprt/spinlock.h>
44#include <iprt/uuid.h>
45#include <VBox/version.h>
46
47/** r=bird: why is this here in the agnostic code? */
48#ifdef RT_OS_DARWIN
49# include <net/ethernet.h>
50# include <net/if_ether.h>
51# include <net/if_types.h>
52# include <sys/socket.h>
53# include <net/if.h>
54# include <net/if_dl.h>
55# include <sys/errno.h>
56# include <sys/param.h>
57#endif
58
59
60/*******************************************************************************
61* Defined Constants And Macros *
62*******************************************************************************/
63#define IFPORT_2_VBOXNETADP(pIfPort) \
64 ( (PVBOXNETADP)((uint8_t *)pIfPort - RT_OFFSETOF(VBOXNETADP, MyPort)) )
65
66
67AssertCompileMemberSize(VBOXNETADP, enmState, sizeof(uint32_t));
68
69/**
70 * Gets the enmState member atomically.
71 *
72 * Used for all reads.
73 *
74 * @returns The enmState value.
75 * @param pThis The instance.
76 */
77DECLINLINE(VBOXNETADPSTATE) vboxNetAdpGetState(PVBOXNETADP pThis)
78{
79 return (VBOXNETADPSTATE)ASMAtomicUoReadU32((uint32_t volatile *)&pThis->enmState);
80}
81
82
83/**
84 * Sets the enmState member atomically.
85 *
86 * Used for all updates.
87 *
88 * @param pThis The instance.
89 * @param enmNewState The new value.
90 */
91DECLINLINE(void) vboxNetAdpSetState(PVBOXNETADP pThis, VBOXNETADPSTATE enmNewState)
92{
93 Log(("vboxNetAdpSetState: pThis=%p, state change: %d -> %d.\n", pThis, vboxNetAdpGetState(pThis), enmNewState));
94 ASMAtomicWriteU32((uint32_t volatile *)&pThis->enmState, enmNewState);
95}
96
97
98/**
99 * Sets the enmState member atomically after first acquiring the spinlock.
100 *
101 * Used for all updates.
102 *
103 * @param pThis The instance.
104 * @param enmNewState The new value.
105 */
106DECLINLINE(void) vboxNetAdpSetStateWithLock(PVBOXNETADP pThis, VBOXNETADPSTATE enmNewState)
107{
108 Log(("vboxNetAdpSetStateWithLock: pThis=%p, state=%d.\n", pThis, enmNewState));
109 RTSpinlockAcquire(pThis->hSpinlock);
110 vboxNetAdpSetState(pThis, enmNewState);
111 RTSpinlockRelease(pThis->hSpinlock);
112}
113
114
115/**
116 * Gets the enmState member with locking.
117 *
118 * Used for all reads.
119 *
120 * @returns The enmState value.
121 * @param pThis The instance.
122 */
123DECLINLINE(VBOXNETADPSTATE) vboxNetAdpGetStateWithLock(PVBOXNETADP pThis)
124{
125 VBOXNETADPSTATE enmState;
126 RTSpinlockAcquire(pThis->hSpinlock);
127 enmState = vboxNetAdpGetState(pThis);
128 RTSpinlockRelease(pThis->hSpinlock);
129 Log(("vboxNetAdpGetStateWithLock: pThis=%p, state=%d.\n", pThis, enmState));
130 return enmState;
131}
132
133
134/**
135 * Checks and sets the enmState member atomically.
136 *
137 * Used for all updates.
138 *
139 * @returns true if the state has been changed.
140 * @param pThis The instance.
141 * @param enmNewState The new value.
142 */
143DECLINLINE(bool) vboxNetAdpCheckAndSetState(PVBOXNETADP pThis, VBOXNETADPSTATE enmOldState, VBOXNETADPSTATE enmNewState)
144{
145 VBOXNETADPSTATE enmActualState;
146 bool fRc = true; /* be optimistic */
147
148 RTSpinlockAcquire(pThis->hSpinlock);
149 enmActualState = vboxNetAdpGetState(pThis); /** @todo r=bird: ASMAtomicCmpXchgU32()*/
150 if (enmActualState == enmOldState)
151 vboxNetAdpSetState(pThis, enmNewState);
152 else
153 fRc = false;
154 RTSpinlockRelease(pThis->hSpinlock);
155
156 if (fRc)
157 Log(("vboxNetAdpCheckAndSetState: pThis=%p, state changed: %d -> %d.\n", pThis, enmOldState, enmNewState));
158 else
159 Log(("vboxNetAdpCheckAndSetState: pThis=%p, no state change: %d != %d (expected).\n", pThis, enmActualState, enmOldState));
160 return fRc;
161}
162
163
164/**
165 * Finds a instance by its name, the caller does the locking.
166 *
167 * @returns Pointer to the instance by the given name. NULL if not found.
168 * @param pGlobals The globals.
169 * @param pszName The name of the instance.
170 */
171static PVBOXNETADP vboxNetAdpFind(PVBOXNETADPGLOBALS pGlobals, const char *pszName)
172{
173 unsigned i;
174
175 for (i = 0; i < RT_ELEMENTS(pGlobals->aAdapters); i++)
176 {
177 PVBOXNETADP pThis = &pGlobals->aAdapters[i];
178 RTSpinlockAcquire(pThis->hSpinlock);
179 if ( vboxNetAdpGetState(pThis)
180 && !strcmp(pThis->szName, pszName))
181 {
182 RTSpinlockRelease(pThis->hSpinlock);
183 return pThis;
184 }
185 RTSpinlockRelease(pThis->hSpinlock);
186 }
187 return NULL;
188}
189
190
191/**
192 * Releases a reference to the specified instance.
193 *
194 * @param pThis The instance.
195 * @param fBusy Whether the busy counter should be decremented too.
196 */
197DECLHIDDEN(void) vboxNetAdpRelease(PVBOXNETADP pThis)
198{
199 uint32_t cRefs;
200
201 /*
202 * Paranoid Android.
203 */
204 AssertPtr(pThis);
205 Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
206 Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
207 Assert(vboxNetAdpGetState(pThis) > kVBoxNetAdpState_Invalid);
208 AssertPtr(pThis->pGlobals);
209 Assert(pThis->hEventIdle != NIL_RTSEMEVENT);
210 Assert(pThis->hSpinlock != NIL_RTSPINLOCK);
211 Assert(pThis->szName[0]);
212
213 /*
214 * The object reference counting.
215 */
216 cRefs = ASMAtomicDecU32(&pThis->cRefs);
217 Assert(cRefs < UINT32_MAX / 2);
218}
219
220
221/**
222 * Decrements the busy counter and does idle wakeup.
223 *
224 * @param pThis The instance.
225 */
226DECLHIDDEN(void) vboxNetAdpIdle(PVBOXNETADP pThis)
227{
228 uint32_t cBusy;
229
230 /*
231 * Paranoid Android.
232 */
233 AssertPtr(pThis);
234 Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
235 Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
236 Assert(vboxNetAdpGetState(pThis) >= kVBoxNetAdpState_Connected);
237 AssertPtr(pThis->pGlobals);
238 Assert(pThis->hEventIdle != NIL_RTSEMEVENT);
239
240 cBusy = ASMAtomicDecU32(&pThis->cBusy);
241 if (!cBusy)
242 {
243 int rc = RTSemEventSignal(pThis->hEventIdle);
244 AssertRC(rc);
245 }
246 else
247 Assert(cBusy < UINT32_MAX / 2);
248}
249
250
251/**
252 * Retains a reference to the specified instance.
253 *
254 * @param pThis The instance.
255 */
256DECLHIDDEN(void) vboxNetAdpRetain(PVBOXNETADP pThis)
257{
258 uint32_t cRefs;
259
260 /*
261 * Paranoid Android.
262 */
263 AssertPtr(pThis);
264 Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
265 Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
266 Assert(vboxNetAdpGetState(pThis) > kVBoxNetAdpState_Invalid);
267 AssertPtr(pThis->pGlobals);
268 Assert(pThis->hEventIdle != NIL_RTSEMEVENT);
269 Assert(pThis->hSpinlock != NIL_RTSPINLOCK);
270 Assert(pThis->szName[0]);
271
272 /*
273 * Retain the object.
274 */
275 cRefs = ASMAtomicIncU32(&pThis->cRefs);
276 Assert(cRefs > 1 && cRefs < UINT32_MAX / 2);
277
278 NOREF(cRefs);
279}
280
281
282/**
283 * Increments busy counter.
284 *
285 * @param pThis The instance.
286 */
287DECLHIDDEN(void) vboxNetAdpBusy(PVBOXNETADP pThis)
288{
289 uint32_t cBusy;
290
291 /*
292 * Are we vigilant enough?
293 */
294 AssertPtr(pThis);
295 Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
296 Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
297 Assert(vboxNetAdpGetState(pThis) >= kVBoxNetAdpState_Connected);
298 AssertPtr(pThis->pGlobals);
299 Assert(pThis->hEventIdle != NIL_RTSEMEVENT);
300 cBusy = ASMAtomicIncU32(&pThis->cBusy);
301 Assert(cBusy > 0 && cBusy < UINT32_MAX / 2);
302
303 NOREF(cBusy);
304}
305
306
307/**
308 * Generate a suitable MAC address.
309 *
310 * @param pThis The instance.
311 * @param pMac Where to return the MAC address.
312 */
313DECLHIDDEN(void) vboxNetAdpComposeMACAddress(PVBOXNETADP pThis, PRTMAC pMac)
314{
315#if 0 /* Use a locally administered version of the OUI we use for the guest NICs. */
316 pMac->au8[0] = 0x08 | 2;
317 pMac->au8[1] = 0x00;
318 pMac->au8[2] = 0x27;
319#else /* this is what \0vb comes down to. It seems to be unassigned atm. */
320 pMac->au8[0] = 0;
321 pMac->au8[1] = 0x76;
322 pMac->au8[2] = 0x62;
323#endif
324
325 pMac->au8[3] = 0; /* pThis->uUnit >> 16; */
326 pMac->au8[4] = 0; /* pThis->uUnit >> 8; */
327 pMac->au8[5] = pThis->uUnit;
328}
329
330
331/**
332 * Checks if receive is possible and increases busy and ref counters if so.
333 *
334 * @param pThis The instance.
335 */
336DECLHIDDEN(bool) vboxNetAdpPrepareToReceive(PVBOXNETADP pThis)
337{
338 bool fCanReceive = false;
339 /*
340 * Input validation.
341 */
342 AssertPtr(pThis);
343 Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
344 RTSpinlockAcquire(pThis->hSpinlock);
345 if (vboxNetAdpGetState(pThis) == kVBoxNetAdpState_Active)
346 {
347 fCanReceive = true;
348 vboxNetAdpRetain(pThis);
349 vboxNetAdpBusy(pThis);
350 }
351 RTSpinlockRelease(pThis->hSpinlock);
352 Log(("vboxNetAdpPrepareToReceive: fCanReceive=%d.\n", fCanReceive));
353
354 return fCanReceive;
355}
356
357
358/**
359 * Forwards scatter/gather list to internal network and decreases busy and ref counters.
360 *
361 * @param pThis The instance.
362 */
363DECLHIDDEN(void) vboxNetAdpReceive(PVBOXNETADP pThis, PINTNETSG pSG)
364{
365 /*
366 * Input validation.
367 */
368 AssertPtr(pThis);
369 AssertPtr(pSG);
370 AssertPtr(pThis->pSwitchPort);
371 Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
372 Log(("vboxNetAdpReceive: forwarding packet to internal net...\n"));
373 pThis->pSwitchPort->pfnRecv(pThis->pSwitchPort, pSG, INTNETTRUNKDIR_HOST);
374 vboxNetAdpIdle(pThis);
375 vboxNetAdpRelease(pThis);
376}
377
378
379/**
380 * Decreases busy and ref counters.
381 *
382 * @param pThis The instance.
383 */
384DECLHIDDEN(void) vboxNetAdpCancelReceive(PVBOXNETADP pThis)
385{
386 Log(("vboxNetAdpCancelReceive: cancelled.\n"));
387 vboxNetAdpIdle(pThis);
388 vboxNetAdpRelease(pThis);
389}
390
391
392/**
393 * @copydoc INTNETTRUNKIFPORT::pfnXmit
394 */
395static DECLCALLBACK(int) vboxNetAdpPortXmit(PINTNETTRUNKIFPORT pIfPort, PINTNETSG pSG, uint32_t fDst)
396{
397 PVBOXNETADP pThis = IFPORT_2_VBOXNETADP(pIfPort);
398 int rc = VINF_SUCCESS;
399
400 /*
401 * Input validation.
402 */
403 AssertPtr(pThis);
404 AssertPtr(pSG);
405 Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
406
407 Log(("vboxNetAdpPortXmit: outgoing packet (len=%d)\n", pSG->cbTotal));
408
409 /*
410 * Do a retain/busy, invoke the OS specific code.
411 */
412 RTSpinlockAcquire(pThis->hSpinlock);
413 if (vboxNetAdpGetState(pThis) != kVBoxNetAdpState_Active)
414 {
415 RTSpinlockRelease(pThis->hSpinlock);
416 Log(("vboxNetAdpReceive: Dropping incoming packet for inactive interface %s.\n",
417 pThis->szName));
418 return VERR_INVALID_STATE;
419 }
420 vboxNetAdpRetain(pThis);
421 vboxNetAdpBusy(pThis);
422 RTSpinlockRelease(pThis->hSpinlock);
423
424 rc = vboxNetAdpPortOsXmit(pThis, pSG, fDst);
425 vboxNetAdpIdle(pThis);
426 vboxNetAdpRelease(pThis);
427
428 return rc;
429}
430
431
432/**
433 * @copydoc INTNETTRUNKIFPORT::pfnGetMacAddress
434 */
435static DECLCALLBACK(void) vboxNetAdpPortGetMacAddress(PINTNETTRUNKIFPORT pIfPort, PRTMAC pMac)
436{
437 PVBOXNETADP pThis = IFPORT_2_VBOXNETADP(pIfPort);
438
439 /*
440 * Input validation.
441 */
442 AssertPtr(pThis);
443 Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
444 Assert(vboxNetAdpGetStateWithLock(pThis) == kVBoxNetAdpState_Active);
445
446 /*
447 * Forward the question to the OS specific code.
448 */
449 vboxNetAdpPortOsGetMacAddress(pThis, pMac);
450}
451
452
453/**
454 * @copydoc INTNETTRUNKIFPORT::pfnWaitForIdle
455 */
456static DECLCALLBACK(int) vboxNetAdpPortWaitForIdle(PINTNETTRUNKIFPORT pIfPort, uint32_t cMillies)
457{
458 int rc;
459 PVBOXNETADP pThis = IFPORT_2_VBOXNETADP(pIfPort);
460
461 /*
462 * Input validation.
463 */
464 AssertPtr(pThis);
465 Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
466 AssertReturn(vboxNetAdpGetStateWithLock(pThis) >= kVBoxNetAdpState_Connected, VERR_INVALID_STATE);
467
468 /*
469 * Go to sleep on the semaphore after checking the busy count.
470 */
471 vboxNetAdpRetain(pThis);
472
473 rc = VINF_SUCCESS;
474 while (pThis->cBusy && RT_SUCCESS(rc))
475 rc = RTSemEventWait(pThis->hEventIdle, cMillies); /** @todo make interruptible? */
476
477 vboxNetAdpRelease(pThis);
478
479 return rc;
480}
481
482
483/**
484 * @copydoc INTNETTRUNKIFPORT::pfnSetActive
485 */
486static DECLCALLBACK(bool) vboxNetAdpPortSetActive(PINTNETTRUNKIFPORT pIfPort, bool fActive)
487{
488 bool fPreviouslyActive;
489 PVBOXNETADP pThis = IFPORT_2_VBOXNETADP(pIfPort);
490
491 /*
492 * Input validation.
493 */
494 AssertPtr(pThis);
495 AssertPtr(pThis->pGlobals);
496 Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
497
498 Log(("vboxNetAdpPortSetActive: pThis=%p, fActive=%d, state before: %d.\n", pThis, fActive, vboxNetAdpGetState(pThis)));
499 RTSpinlockAcquire(pThis->hSpinlock);
500
501 fPreviouslyActive = vboxNetAdpGetState(pThis) == kVBoxNetAdpState_Active;
502 if (fPreviouslyActive != fActive)
503 {
504 switch (vboxNetAdpGetState(pThis))
505 {
506 case kVBoxNetAdpState_Connected:
507 vboxNetAdpSetState(pThis, kVBoxNetAdpState_Active);
508 break;
509 case kVBoxNetAdpState_Active:
510 vboxNetAdpSetState(pThis, kVBoxNetAdpState_Connected);
511 break;
512 default:
513 break;
514 }
515 }
516
517 RTSpinlockRelease(pThis->hSpinlock);
518 Log(("vboxNetAdpPortSetActive: state after: %RTbool.\n", vboxNetAdpGetState(pThis)));
519 return fPreviouslyActive;
520}
521
522
523/**
524 * @copydoc INTNETTRUNKIFPORT::pfnDisconnectAndRelease
525 */
526static DECLCALLBACK(void) vboxNetAdpPortDisconnectAndRelease(PINTNETTRUNKIFPORT pIfPort)
527{
528 PVBOXNETADP pThis = IFPORT_2_VBOXNETADP(pIfPort);
529
530 /*
531 * Serious paranoia.
532 */
533 AssertPtr(pThis);
534 Assert(pThis->MyPort.u32Version == INTNETTRUNKIFPORT_VERSION);
535 Assert(pThis->MyPort.u32VersionEnd == INTNETTRUNKIFPORT_VERSION);
536 AssertPtr(pThis->pGlobals);
537 Assert(pThis->hEventIdle != NIL_RTSEMEVENT);
538 Assert(pThis->hSpinlock != NIL_RTSPINLOCK);
539
540
541 /*
542 * Disconnect and release it.
543 */
544 RTSpinlockAcquire(pThis->hSpinlock);
545 //Assert(vboxNetAdpGetState(pThis) == kVBoxNetAdpState_Connected);
546 Assert(!pThis->cBusy);
547 vboxNetAdpSetState(pThis, kVBoxNetAdpState_Transitional);
548 RTSpinlockRelease(pThis->hSpinlock);
549
550 vboxNetAdpOsDisconnectIt(pThis);
551 pThis->pSwitchPort = NULL;
552
553 RTSpinlockAcquire(pThis->hSpinlock);
554 vboxNetAdpSetState(pThis, kVBoxNetAdpState_Available);
555 RTSpinlockRelease(pThis->hSpinlock);
556
557 vboxNetAdpRelease(pThis);
558}
559
560
561/**
562 * @copydoc INTNETTRUNKIFPORT::pfnRelease
563 */
564static DECLCALLBACK(void) vboxNetAdpPortRelease(PINTNETTRUNKIFPORT pIfPort)
565{
566 PVBOXNETADP pThis = IFPORT_2_VBOXNETADP(pIfPort);
567 vboxNetAdpRelease(pThis);
568}
569
570
571/**
572 * @copydoc INTNETTRUNKIFPORT::pfnRetain
573 */
574static DECLCALLBACK(void) vboxNetAdpPortRetain(PINTNETTRUNKIFPORT pIfPort)
575{
576 PVBOXNETADP pThis = IFPORT_2_VBOXNETADP(pIfPort);
577 vboxNetAdpRetain(pThis);
578}
579
580
581int vboxNetAdpCreate(PINTNETTRUNKFACTORY pIfFactory, PVBOXNETADP *ppNew)
582{
583 PVBOXNETADPGLOBALS pGlobals = (PVBOXNETADPGLOBALS)((uint8_t *)pIfFactory - RT_OFFSETOF(VBOXNETADPGLOBALS, TrunkFactory));
584 unsigned i;
585 int rc;
586
587 for (i = 0; i < RT_ELEMENTS(pGlobals->aAdapters); i++)
588 {
589 PVBOXNETADP pThis = &pGlobals->aAdapters[i];
590
591 if (vboxNetAdpCheckAndSetState(pThis, kVBoxNetAdpState_Invalid, kVBoxNetAdpState_Transitional))
592 {
593 /* Found an empty slot -- use it. */
594 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
595 Assert(cRefs == 1);
596 RTMAC Mac;
597 vboxNetAdpComposeMACAddress(pThis, &Mac);
598 rc = vboxNetAdpOsCreate(pThis, &Mac);
599 *ppNew = pThis;
600
601 RTSpinlockAcquire(pThis->hSpinlock);
602 vboxNetAdpSetState(pThis, kVBoxNetAdpState_Available);
603 RTSpinlockRelease(pThis->hSpinlock);
604 return rc;
605 }
606 }
607
608 /* All slots in adapter array are busy. */
609 return VERR_OUT_OF_RESOURCES;
610}
611
612int vboxNetAdpDestroy(PVBOXNETADP pThis)
613{
614 int rc = VINF_SUCCESS;
615
616 RTSpinlockAcquire(pThis->hSpinlock);
617 if (vboxNetAdpGetState(pThis) != kVBoxNetAdpState_Available || pThis->cBusy)
618 {
619 RTSpinlockRelease(pThis->hSpinlock);
620 return VERR_INTNET_FLT_IF_BUSY;
621 }
622 vboxNetAdpSetState(pThis, kVBoxNetAdpState_Transitional);
623 RTSpinlockRelease(pThis->hSpinlock);
624 vboxNetAdpRelease(pThis);
625
626 vboxNetAdpOsDestroy(pThis);
627
628 RTSpinlockAcquire(pThis->hSpinlock);
629 vboxNetAdpSetState(pThis, kVBoxNetAdpState_Invalid);
630 RTSpinlockRelease(pThis->hSpinlock);
631
632 return rc;
633}
634
635/**
636 * Connects the instance to the specified switch port.
637 *
638 * Called while owning the lock. We're ASSUMING that the internal
639 * networking code is already owning an recursive mutex, so, there
640 * will be no deadlocks when vboxNetAdpOsConnectIt calls back into
641 * it for setting preferences.
642 *
643 * @returns VBox status code.
644 * @param pThis The instance.
645 * @param pSwitchPort The port on the internal network 'switch'.
646 * @param ppIfPort Where to return our port interface.
647 */
648static int vboxNetAdpConnectIt(PVBOXNETADP pThis, PINTNETTRUNKSWPORT pSwitchPort, PINTNETTRUNKIFPORT *ppIfPort)
649{
650 int rc;
651
652 /*
653 * Validate state.
654 */
655 Assert(!pThis->cBusy);
656 Assert(vboxNetAdpGetStateWithLock(pThis) == kVBoxNetAdpState_Transitional);
657
658 /*
659 * Do the job.
660 * Note that we're calling the os stuff while owning the semaphore here.
661 */
662 pThis->pSwitchPort = pSwitchPort;
663 rc = vboxNetAdpOsConnectIt(pThis);
664 if (RT_SUCCESS(rc))
665 {
666 *ppIfPort = &pThis->MyPort;
667 }
668 else
669 pThis->pSwitchPort = NULL;
670
671 return rc;
672}
673
674
675/**
676 * @copydoc INTNETTRUNKFACTORY::pfnCreateAndConnect
677 */
678static DECLCALLBACK(int) vboxNetAdpFactoryCreateAndConnect(PINTNETTRUNKFACTORY pIfFactory, const char *pszName,
679 PINTNETTRUNKSWPORT pSwitchPort, uint32_t fFlags,
680 PINTNETTRUNKIFPORT *ppIfPort)
681{
682 PVBOXNETADPGLOBALS pGlobals = (PVBOXNETADPGLOBALS)((uint8_t *)pIfFactory - RT_OFFSETOF(VBOXNETADPGLOBALS, TrunkFactory));
683 PVBOXNETADP pThis;
684 int rc;
685
686 LogFlow(("vboxNetAdpFactoryCreateAndConnect: pszName=%p:{%s} fFlags=%#x\n", pszName, pszName, fFlags));
687 Assert(pGlobals->cFactoryRefs > 0);
688 AssertMsgReturn(!fFlags,
689 ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
690
691 /*
692 * Find instance, check if busy, connect if not.
693 */
694 pThis = vboxNetAdpFind(pGlobals, pszName);
695 if (pThis)
696 {
697 if (vboxNetAdpCheckAndSetState(pThis, kVBoxNetAdpState_Available, kVBoxNetAdpState_Transitional))
698 {
699 vboxNetAdpRetain(pThis);
700 rc = vboxNetAdpConnectIt(pThis, pSwitchPort, ppIfPort);
701 vboxNetAdpSetStateWithLock(pThis, RT_SUCCESS(rc) ? kVBoxNetAdpState_Connected : kVBoxNetAdpState_Available);
702 }
703 else
704 rc = VERR_INTNET_FLT_IF_BUSY;
705 }
706 else
707 rc = VERR_INTNET_FLT_IF_NOT_FOUND;
708
709 return rc;
710}
711
712
713/**
714 * @copydoc INTNETTRUNKFACTORY::pfnRelease
715 */
716static DECLCALLBACK(void) vboxNetAdpFactoryRelease(PINTNETTRUNKFACTORY pIfFactory)
717{
718 PVBOXNETADPGLOBALS pGlobals = (PVBOXNETADPGLOBALS)((uint8_t *)pIfFactory - RT_OFFSETOF(VBOXNETADPGLOBALS, TrunkFactory));
719
720 int32_t cRefs = ASMAtomicDecS32(&pGlobals->cFactoryRefs);
721 Assert(cRefs >= 0); NOREF(cRefs);
722 LogFlow(("vboxNetAdpFactoryRelease: cRefs=%d (new)\n", cRefs));
723}
724
725
726/**
727 * Implements the SUPDRV component factor interface query method.
728 *
729 * @returns Pointer to an interface. NULL if not supported.
730 *
731 * @param pSupDrvFactory Pointer to the component factory registration structure.
732 * @param pSession The session - unused.
733 * @param pszInterfaceUuid The factory interface id.
734 */
735static DECLCALLBACK(void *) vboxNetAdpQueryFactoryInterface(PCSUPDRVFACTORY pSupDrvFactory, PSUPDRVSESSION pSession, const char *pszInterfaceUuid)
736{
737 PVBOXNETADPGLOBALS pGlobals = (PVBOXNETADPGLOBALS)((uint8_t *)pSupDrvFactory - RT_OFFSETOF(VBOXNETADPGLOBALS, SupDrvFactory));
738
739 /*
740 * Convert the UUID strings and compare them.
741 */
742 RTUUID UuidReq;
743 int rc = RTUuidFromStr(&UuidReq, pszInterfaceUuid);
744 if (RT_SUCCESS(rc))
745 {
746 if (!RTUuidCompareStr(&UuidReq, INTNETTRUNKFACTORY_UUID_STR))
747 {
748 ASMAtomicIncS32(&pGlobals->cFactoryRefs);
749 return &pGlobals->TrunkFactory;
750 }
751#ifdef LOG_ENABLED
752 else
753 Log(("VBoxNetAdp: unknown factory interface query (%s)\n", pszInterfaceUuid));
754#endif
755 }
756 else
757 Log(("VBoxNetAdp: rc=%Rrc, uuid=%s\n", rc, pszInterfaceUuid));
758
759 return NULL;
760}
761
762
763/**
764 * Checks whether the VBoxNetAdp wossname can be unloaded.
765 *
766 * This will return false if someone is currently using the module.
767 *
768 * @returns true if it's relatively safe to unload it, otherwise false.
769 * @param pGlobals Pointer to the globals.
770 */
771DECLHIDDEN(bool) vboxNetAdpCanUnload(PVBOXNETADPGLOBALS pGlobals)
772{
773 bool fRc = true; /* Assume it can be unloaded. */
774 unsigned i;
775
776 for (i = 0; i < RT_ELEMENTS(pGlobals->aAdapters); i++)
777 {
778 PVBOXNETADP pThis = &pGlobals->aAdapters[i];
779 if (vboxNetAdpGetStateWithLock(&pGlobals->aAdapters[i]) >= kVBoxNetAdpState_Connected)
780 {
781 fRc = false;
782 break; /* We already know the answer. */
783 }
784 }
785 return fRc && ASMAtomicUoReadS32((int32_t volatile *)&pGlobals->cFactoryRefs) <= 0;
786}
787
788/**
789 * tries to deinitialize Idc
790 * we separate the globals settings "base" which is actually
791 * "general" globals settings except for Idc, and idc.
792 * This is needed for windows filter driver, which gets loaded prior to VBoxDrv,
793 * thus it's not possible to make idc initialization from the driver startup routine for it,
794 * though the "base is still needed for the driver to functions".
795 * @param pGlobals
796 * @return VINF_SUCCESS on success, VERR_WRONG_ORDER if we're busy.
797 */
798DECLHIDDEN(int) vboxNetAdpTryDeleteIdc(PVBOXNETADPGLOBALS pGlobals)
799{
800 int rc;
801
802 Assert(pGlobals->hFastMtx != NIL_RTSEMFASTMUTEX);
803
804 /*
805 * Check before trying to deregister the factory.
806 */
807 if (!vboxNetAdpCanUnload(pGlobals))
808 return VERR_WRONG_ORDER;
809
810 /*
811 * Disconnect from SUPDRV and check that nobody raced us,
812 * reconnect if that should happen.
813 */
814 rc = SUPR0IdcComponentDeregisterFactory(&pGlobals->SupDrvIDC, &pGlobals->SupDrvFactory);
815 AssertRC(rc);
816 if (!vboxNetAdpCanUnload(pGlobals))
817 {
818 rc = SUPR0IdcComponentRegisterFactory(&pGlobals->SupDrvIDC, &pGlobals->SupDrvFactory);
819 AssertRC(rc);
820 return VERR_WRONG_ORDER;
821 }
822
823 SUPR0IdcClose(&pGlobals->SupDrvIDC);
824
825 return rc;
826}
827
828static int vboxNetAdpSlotCreate(PVBOXNETADPGLOBALS pGlobals, unsigned uUnit, PVBOXNETADP pNew)
829{
830 int rc;
831
832 pNew->MyPort.u32Version = INTNETTRUNKIFPORT_VERSION;
833 pNew->MyPort.pfnRetain = vboxNetAdpPortRetain;
834 pNew->MyPort.pfnRelease = vboxNetAdpPortRelease;
835 pNew->MyPort.pfnDisconnectAndRelease= vboxNetAdpPortDisconnectAndRelease;
836 pNew->MyPort.pfnSetState = vboxNetAdpPortSetState;
837 pNew->MyPort.pfnWaitForIdle = vboxNetAdpPortWaitForIdle;
838 pNew->MyPort.pfnXmit = vboxNetAdpPortXmit;
839 pNew->MyPort.u32VersionEnd = INTNETTRUNKIFPORT_VERSION;
840 pNew->pSwitchPort = NULL;
841 pNew->pGlobals = pGlobals;
842 pNew->hSpinlock = NIL_RTSPINLOCK;
843 pNew->enmState = kVBoxNetAdpState_Invalid;
844 pNew->cRefs = 0;
845 pNew->cBusy = 0;
846 pNew->hEventIdle = NIL_RTSEMEVENT;
847
848 rc = RTSpinlockCreate(&pNew->hSpinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "VBoxNetAdptSlotCreate");
849 if (RT_SUCCESS(rc))
850 {
851 rc = RTSemEventCreate(&pNew->hEventIdle);
852 if (RT_SUCCESS(rc))
853 {
854 rc = vboxNetAdpOsInit(pNew);
855 if (RT_SUCCESS(rc))
856 {
857 return rc;
858 }
859 RTSemEventDestroy(pNew->hEventIdle);
860 pNew->hEventIdle = NIL_RTSEMEVENT;
861 }
862 RTSpinlockDestroy(pNew->hSpinlock);
863 pNew->hSpinlock = NIL_RTSPINLOCK;
864 }
865 return rc;
866}
867
868static void vboxNetAdpSlotDestroy(PVBOXNETADP pThis)
869{
870 Assert(pThis->cRefs == 0);
871 Assert(pThis->cBusy == 0);
872 Assert(vboxNetAdpGetState(pThis) == kVBoxNetAdpState_Invalid);
873 if (pThis->hEventIdle != NIL_RTSEMEVENT)
874 {
875 RTSemEventDestroy(pThis->hEventIdle);
876 pThis->hEventIdle = NIL_RTSEMEVENT;
877 }
878 if (pThis->hSpinlock != NIL_RTSPINLOCK)
879 {
880 RTSpinlockDestroy(pThis->hSpinlock);
881 pThis->hSpinlock = NIL_RTSPINLOCK;
882 }
883}
884
885/**
886 * performs "base" globals deinitialization
887 * we separate the globals settings "base" which is actually
888 * "general" globals settings except for Idc, and idc.
889 * This is needed for windows filter driver, which gets loaded prior to VBoxDrv,
890 * thus it's not possible to make idc initialization from the driver startup routine for it,
891 * though the "base is still needed for the driver to functions".
892 * @param pGlobals
893 * @return none
894 */
895DECLHIDDEN(void) vboxNetAdpDeleteGlobalsBase(PVBOXNETADPGLOBALS pGlobals)
896{
897 int i;
898 /*
899 * Release resources.
900 */
901 for (i = 0; i < (int)RT_ELEMENTS(pGlobals->aAdapters); i++)
902 if (RT_SUCCESS(vboxNetAdpDestroy(&pGlobals->aAdapters[i])))
903 vboxNetAdpSlotDestroy(&pGlobals->aAdapters[i]);
904
905 RTSemFastMutexDestroy(pGlobals->hFastMtx);
906 pGlobals->hFastMtx = NIL_RTSEMFASTMUTEX;
907
908#ifdef VBOXNETADP_STATIC_CONFIG
909 RTSemEventDestroy(pGlobals->hTimerEvent);
910 pGlobals->hTimerEvent = NIL_RTSEMEVENT;
911#endif
912
913}
914
915
916/**
917 * Called by the native part when the OS wants the driver to unload.
918 *
919 * @returns VINF_SUCCESS on success, VERR_WRONG_ORDER if we're busy.
920 *
921 * @param pGlobals Pointer to the globals.
922 */
923DECLHIDDEN(int) vboxNetAdpTryDeleteGlobals(PVBOXNETADPGLOBALS pGlobals)
924{
925 int rc = vboxNetAdpTryDeleteIdc(pGlobals);
926 if (RT_SUCCESS(rc))
927 {
928 vboxNetAdpDeleteGlobalsBase(pGlobals);
929 }
930 return rc;
931}
932
933
934/**
935 * performs the "base" globals initialization
936 * we separate the globals initialization to globals "base" initialization which is actually
937 * "general" globals initialization except for Idc not being initialized, and idc initialization.
938 * This is needed for windows filter driver, which gets loaded prior to VBoxDrv,
939 * thus it's not possible to make idc initialization from the driver startup routine for it.
940 *
941 * @returns VBox status code.
942 * @param pGlobals Pointer to the globals. */
943DECLHIDDEN(int) vboxNetAdpInitGlobalsBase(PVBOXNETADPGLOBALS pGlobals)
944{
945 /*
946 * Initialize the common portions of the structure.
947 */
948 int i;
949 int rc = RTSemFastMutexCreate(&pGlobals->hFastMtx);
950 if (RT_SUCCESS(rc))
951 {
952 memset(pGlobals->aAdapters, 0, sizeof(pGlobals->aAdapters));
953 for (i = 0; i < (int)RT_ELEMENTS(pGlobals->aAdapters); i++)
954 {
955 rc = vboxNetAdpSlotCreate(pGlobals, i, &pGlobals->aAdapters[i]);
956 if (RT_FAILURE(rc))
957 {
958 /* Clean up. */
959 while (--i >= 0)
960 vboxNetAdpSlotDestroy(&pGlobals->aAdapters[i]);
961 Log(("vboxNetAdpInitGlobalsBase: Failed to create fast mutex (rc=%Rrc).\n", rc));
962 RTSemFastMutexDestroy(pGlobals->hFastMtx);
963 return rc;
964 }
965 }
966 pGlobals->TrunkFactory.pfnRelease = vboxNetAdpFactoryRelease;
967 pGlobals->TrunkFactory.pfnCreateAndConnect = vboxNetAdpFactoryCreateAndConnect;
968
969 strcpy(pGlobals->SupDrvFactory.szName, "VBoxNetAdp");
970 pGlobals->SupDrvFactory.pfnQueryFactoryInterface = vboxNetAdpQueryFactoryInterface;
971 }
972
973 return rc;
974}
975
976/**
977 * performs the Idc initialization
978 * we separate the globals initialization to globals "base" initialization which is actually
979 * "general" globals initialization except for Idc not being initialized, and idc initialization.
980 * This is needed for windows filter driver, which gets loaded prior to VBoxDrv,
981 * thus it's not possible to make idc initialization from the driver startup routine for it.
982 *
983 * @returns VBox status code.
984 * @param pGlobals Pointer to the globals. */
985DECLHIDDEN(int) vboxNetAdpInitIdc(PVBOXNETADPGLOBALS pGlobals)
986{
987 int rc;
988 /*
989 * Establish a connection to SUPDRV and register our component factory.
990 */
991 rc = SUPR0IdcOpen(&pGlobals->SupDrvIDC, 0 /* iReqVersion = default */, 0 /* iMinVersion = default */, NULL, NULL, NULL);
992 if (RT_SUCCESS(rc))
993 {
994 rc = SUPR0IdcComponentRegisterFactory(&pGlobals->SupDrvIDC, &pGlobals->SupDrvFactory);
995 if (RT_SUCCESS(rc))
996 {
997#if 1 /** @todo REMOVE ME! */
998 PVBOXNETADP pTmp;
999 rc = vboxNetAdpCreate(&pGlobals->TrunkFactory, &pTmp);
1000 if (RT_FAILURE(rc))
1001 Log(("Failed to create vboxnet0, rc=%Rrc.\n", rc));
1002#endif
1003 Log(("VBoxNetAdp: pSession=%p\n", SUPR0IdcGetSession(&pGlobals->SupDrvIDC)));
1004 return rc;
1005 }
1006
1007 /* bail out. */
1008 LogRel(("VBoxNetAdp: Failed to register component factory, rc=%Rrc\n", rc));
1009 SUPR0IdcClose(&pGlobals->SupDrvIDC);
1010 }
1011
1012 return rc;
1013}
1014
1015/**
1016 * Called by the native driver/kext module initialization routine.
1017 *
1018 * It will initialize the common parts of the globals, assuming the caller
1019 * has already taken care of the OS specific bits.
1020 *
1021 * @returns VBox status code.
1022 * @param pGlobals Pointer to the globals.
1023 */
1024DECLHIDDEN(int) vboxNetAdpInitGlobals(PVBOXNETADPGLOBALS pGlobals)
1025{
1026 /*
1027 * Initialize the common portions of the structure.
1028 */
1029 int rc = vboxNetAdpInitGlobalsBase(pGlobals);
1030 if (RT_SUCCESS(rc))
1031 {
1032 rc = vboxNetAdpInitIdc(pGlobals);
1033 if (RT_SUCCESS(rc))
1034 {
1035 return rc;
1036 }
1037
1038 /* bail out. */
1039 vboxNetAdpDeleteGlobalsBase(pGlobals);
1040 }
1041
1042 return rc;
1043}
1044
1045#else /* !VBOXANETADP_DO_NOT_USE_NETFLT */
1046
1047
1048VBOXNETADP g_aAdapters[VBOXNETADP_MAX_INSTANCES];
1049static uint8_t g_aUnits[VBOXNETADP_MAX_UNITS/8];
1050
1051
1052DECLINLINE(int) vboxNetAdpGetUnitByName(const char *pcszName)
1053{
1054 uint32_t iUnit = RTStrToUInt32(pcszName + sizeof(VBOXNETADP_NAME) - 1);
1055 bool fOld;
1056
1057 if (iUnit >= VBOXNETADP_MAX_UNITS)
1058 return -1;
1059
1060 fOld = ASMAtomicBitTestAndSet(g_aUnits, iUnit);
1061 return fOld ? -1 : (int)iUnit;
1062}
1063
1064DECLINLINE(int) vboxNetAdpGetNextAvailableUnit(void)
1065{
1066 bool fOld;
1067 int iUnit;
1068 /* There is absolutely no chance that all units are taken */
1069 do {
1070 iUnit = ASMBitFirstClear(g_aUnits, VBOXNETADP_MAX_UNITS);
1071 if (iUnit < 0)
1072 break;
1073 fOld = ASMAtomicBitTestAndSet(g_aUnits, iUnit);
1074 } while (fOld);
1075
1076 return iUnit;
1077}
1078
1079DECLINLINE(void) vboxNetAdpReleaseUnit(int iUnit)
1080{
1081 bool fSet = ASMAtomicBitTestAndClear(g_aUnits, iUnit);
1082 NOREF(fSet);
1083 Assert(fSet);
1084}
1085
1086/**
1087 * Generate a suitable MAC address.
1088 *
1089 * @param pThis The instance.
1090 * @param pMac Where to return the MAC address.
1091 */
1092DECLHIDDEN(void) vboxNetAdpComposeMACAddress(PVBOXNETADP pThis, PRTMAC pMac)
1093{
1094 /* Use a locally administered version of the OUI we use for the guest NICs. */
1095 pMac->au8[0] = 0x08 | 2;
1096 pMac->au8[1] = 0x00;
1097 pMac->au8[2] = 0x27;
1098
1099 pMac->au8[3] = 0; /* pThis->iUnit >> 16; */
1100 pMac->au8[4] = 0; /* pThis->iUnit >> 8; */
1101 pMac->au8[5] = pThis->iUnit;
1102}
1103
1104int vboxNetAdpCreate(PVBOXNETADP *ppNew, const char *pcszName)
1105{
1106 int rc;
1107 unsigned i;
1108 for (i = 0; i < RT_ELEMENTS(g_aAdapters); i++)
1109 {
1110 PVBOXNETADP pThis = &g_aAdapters[i];
1111
1112 if (ASMAtomicCmpXchgU32((uint32_t volatile *)&pThis->enmState, kVBoxNetAdpState_Transitional, kVBoxNetAdpState_Invalid))
1113 {
1114 RTMAC Mac;
1115 /* Found an empty slot -- use it. */
1116 Log(("vboxNetAdpCreate: found empty slot: %d\n", i));
1117 if (pcszName)
1118 {
1119 Log(("vboxNetAdpCreate: using name: %s\n", pcszName));
1120 pThis->iUnit = vboxNetAdpGetUnitByName(pcszName);
1121 strncpy(pThis->szName, pcszName, sizeof(pThis->szName));
1122 pThis->szName[sizeof(pThis->szName) - 1] = '\0';
1123 }
1124 else
1125 {
1126 pThis->iUnit = vboxNetAdpGetNextAvailableUnit();
1127 pThis->szName[0] = '\0';
1128 }
1129 if (pThis->iUnit < 0)
1130 rc = VERR_INVALID_PARAMETER;
1131 else
1132 {
1133 vboxNetAdpComposeMACAddress(pThis, &Mac);
1134 rc = vboxNetAdpOsCreate(pThis, &Mac);
1135 Log(("vboxNetAdpCreate: pThis=%p pThis->iUnit=%d, pThis->szName=%s\n",
1136 pThis, pThis->iUnit, pThis->szName));
1137 }
1138 if (RT_SUCCESS(rc))
1139 {
1140 *ppNew = pThis;
1141 ASMAtomicWriteU32((uint32_t volatile *)&pThis->enmState, kVBoxNetAdpState_Active);
1142 Log2(("VBoxNetAdpCreate: Created %s\n", g_aAdapters[i].szName));
1143 }
1144 else
1145 {
1146 ASMAtomicWriteU32((uint32_t volatile *)&pThis->enmState, kVBoxNetAdpState_Invalid);
1147 Log(("vboxNetAdpCreate: vboxNetAdpOsCreate failed with '%Rrc'.\n", rc));
1148 }
1149 for (i = 0; i < RT_ELEMENTS(g_aAdapters); i++)
1150 Log2(("VBoxNetAdpCreate: Scanning entry: state=%d unit=%d name=%s\n",
1151 g_aAdapters[i].enmState, g_aAdapters[i].iUnit, g_aAdapters[i].szName));
1152 return rc;
1153 }
1154 }
1155 Log(("vboxNetAdpCreate: no empty slots!\n"));
1156
1157 /* All slots in adapter array are busy. */
1158 return VERR_OUT_OF_RESOURCES;
1159}
1160
1161int vboxNetAdpDestroy(PVBOXNETADP pThis)
1162{
1163 int rc = VINF_SUCCESS;
1164
1165 if (!ASMAtomicCmpXchgU32((uint32_t volatile *)&pThis->enmState, kVBoxNetAdpState_Transitional, kVBoxNetAdpState_Active))
1166 return VERR_INTNET_FLT_IF_BUSY;
1167
1168 Assert(pThis->iUnit >= 0 && pThis->iUnit < VBOXNETADP_MAX_UNITS);
1169 vboxNetAdpOsDestroy(pThis);
1170 vboxNetAdpReleaseUnit(pThis->iUnit);
1171 pThis->iUnit = -1;
1172 pThis->szName[0] = '\0';
1173
1174 ASMAtomicWriteU32((uint32_t volatile *)&pThis->enmState, kVBoxNetAdpState_Invalid);
1175
1176 return rc;
1177}
1178
1179int vboxNetAdpInit(void)
1180{
1181 unsigned i;
1182 /*
1183 * Init common members and call OS-specific init.
1184 */
1185 memset(g_aUnits, 0, sizeof(g_aUnits));
1186 memset(g_aAdapters, 0, sizeof(g_aAdapters));
1187 LogFlow(("vboxnetadp: max host-only interfaces supported: %d (%d bytes)\n",
1188 VBOXNETADP_MAX_INSTANCES, sizeof(g_aAdapters)));
1189 for (i = 0; i < RT_ELEMENTS(g_aAdapters); i++)
1190 {
1191 g_aAdapters[i].enmState = kVBoxNetAdpState_Invalid;
1192 g_aAdapters[i].iUnit = -1;
1193 vboxNetAdpOsInit(&g_aAdapters[i]);
1194 }
1195
1196 return VINF_SUCCESS;
1197}
1198
1199/**
1200 * Finds an adapter by its name.
1201 *
1202 * @returns Pointer to the instance by the given name. NULL if not found.
1203 * @param pGlobals The globals.
1204 * @param pszName The name of the instance.
1205 */
1206PVBOXNETADP vboxNetAdpFindByName(const char *pszName)
1207{
1208 unsigned i;
1209
1210 for (i = 0; i < RT_ELEMENTS(g_aAdapters); i++)
1211 {
1212 PVBOXNETADP pThis = &g_aAdapters[i];
1213 Log2(("VBoxNetAdp: Scanning entry: state=%d name=%s\n", pThis->enmState, pThis->szName));
1214 if ( strcmp(pThis->szName, pszName) == 0
1215 && ASMAtomicReadU32((uint32_t volatile *)&pThis->enmState) == kVBoxNetAdpState_Active)
1216 return pThis;
1217 }
1218 return NULL;
1219}
1220
1221void vboxNetAdpShutdown(void)
1222{
1223 unsigned i;
1224
1225 /* Remove virtual adapters */
1226 for (i = 0; i < RT_ELEMENTS(g_aAdapters); i++)
1227 vboxNetAdpDestroy(&g_aAdapters[i]);
1228}
1229#endif /* !VBOXANETADP_DO_NOT_USE_NETFLT */
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