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