1 | /* $Id: VBoxNetAdpInternal.h 16960 2009-02-19 22:13:33Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxNetAdp - Network Filter Driver (Host), Internal Header.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #ifndef ___VBoxNetAdpInternal_h___
|
---|
23 | #define ___VBoxNetAdpInternal_h___
|
---|
24 |
|
---|
25 | #include <VBox/sup.h>
|
---|
26 | #include <VBox/intnet.h>
|
---|
27 | #include <iprt/semaphore.h>
|
---|
28 | #include <iprt/assert.h>
|
---|
29 |
|
---|
30 |
|
---|
31 | __BEGIN_DECLS
|
---|
32 |
|
---|
33 | /** Pointer to the globals. */
|
---|
34 | typedef struct VBOXNETADPGLOBALS *PVBOXNETADPGLOBALS;
|
---|
35 |
|
---|
36 | #define VBOXNETADP_MAX_INSTANCES 8
|
---|
37 | #define VBOXNETADP_NAME "vboxnet"
|
---|
38 | #define VBOXNETADP_MTU 1500
|
---|
39 | #if defined(RT_OS_DARWIN)
|
---|
40 | # define VBOXNETADP_MAX_FAMILIES 4
|
---|
41 | # define VBOXNETADP_DETACH_TIMEOUT 500
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Void entries mark vacant slots in adapter array. Valid entries are busy slots.
|
---|
46 | * As soon as slot is being modified its state changes to transitional.
|
---|
47 | * An entry in transitional state must only be accessed by the thread that
|
---|
48 | * put it to this state.
|
---|
49 | */
|
---|
50 | /**
|
---|
51 | * To avoid races on adapter fields we stick to the following rules:
|
---|
52 | * - rewrite: Int net port calls are serialized
|
---|
53 | * - No modifications are allowed on busy adapters (deactivate first)
|
---|
54 | * Refuse to destroy adapter until it gets to available state
|
---|
55 | * - No transfers (thus getting busy) on inactive adapters
|
---|
56 | * - Init sequence: void->available->connected->active
|
---|
57 | 1) Create
|
---|
58 | 2) Connect
|
---|
59 | 3) Activate
|
---|
60 | * - Destruction sequence: active->connected->available->void
|
---|
61 | 1) Deactivate
|
---|
62 | 2) Disconnect
|
---|
63 | 3) Destroy
|
---|
64 | */
|
---|
65 |
|
---|
66 | enum VBoxNetAdpState
|
---|
67 | {
|
---|
68 | kVBoxNetAdpState_Invalid,
|
---|
69 | kVBoxNetAdpState_Transitional,
|
---|
70 | kVBoxNetAdpState_Available,
|
---|
71 | kVBoxNetAdpState_Connected,
|
---|
72 | kVBoxNetAdpState_Active
|
---|
73 | };
|
---|
74 | typedef enum VBoxNetAdpState VBOXNETADPSTATE;
|
---|
75 |
|
---|
76 | struct VBoxNetAdapter
|
---|
77 | {
|
---|
78 | /** The spinlock protecting the state variables and host interface handle. */
|
---|
79 | RTSPINLOCK hSpinlock;
|
---|
80 |
|
---|
81 | /* --- Protected with spinlock. --- */
|
---|
82 |
|
---|
83 | /** Denotes availability of this slot in adapter array. */
|
---|
84 | VBOXNETADPSTATE enmState;
|
---|
85 |
|
---|
86 | /* --- Unprotected. Atomic access. --- */
|
---|
87 |
|
---|
88 | /** Reference count. */
|
---|
89 | uint32_t volatile cRefs;
|
---|
90 | /** The busy count.
|
---|
91 | * This counts the number of current callers and pending packet. */
|
---|
92 | uint32_t volatile cBusy;
|
---|
93 |
|
---|
94 | /* --- Unprotected. Do not modify when cBusy > 0. --- */
|
---|
95 |
|
---|
96 | /** Our RJ-45 port.
|
---|
97 | * This is what the internal network plugs into. */
|
---|
98 | INTNETTRUNKIFPORT MyPort;
|
---|
99 | /** The RJ-45 port on the INTNET "switch".
|
---|
100 | * This is what we're connected to. */
|
---|
101 | PINTNETTRUNKSWPORT pSwitchPort;
|
---|
102 | /** Pointer to the globals. */
|
---|
103 | PVBOXNETADPGLOBALS pGlobals;
|
---|
104 | /** Corresponds to the digit at the end of device name. */
|
---|
105 | uint8_t uUnit;
|
---|
106 | /** The event that is signaled when we go idle and that pfnWaitForIdle blocks on. */
|
---|
107 | RTSEMEVENT hEventIdle;
|
---|
108 |
|
---|
109 | union
|
---|
110 | {
|
---|
111 | #ifdef VBOXNETADP_OS_SPECFIC
|
---|
112 | struct
|
---|
113 | {
|
---|
114 | # if defined(RT_OS_DARWIN)
|
---|
115 | /** @name Darwin instance data.
|
---|
116 | * @{ */
|
---|
117 | /** Event to signal detachment of interface. */
|
---|
118 | RTSEMEVENT hEvtDetached;
|
---|
119 | /** Pointer to Darwin interface structure. */
|
---|
120 | ifnet_t pIface;
|
---|
121 | /** MAC address. */
|
---|
122 | RTMAC Mac;
|
---|
123 | /** Protocol families attached to this adapter. */
|
---|
124 | protocol_family_t aAttachedFamilies[VBOXNETADP_MAX_FAMILIES];
|
---|
125 | # else
|
---|
126 | # error PORTME
|
---|
127 | # endif
|
---|
128 | } s;
|
---|
129 | #endif
|
---|
130 | /** Padding. */
|
---|
131 | #if defined(RT_OS_WINDOWS)
|
---|
132 | # if defined(VBOX_NETFLT_ONDEMAND_BIND)
|
---|
133 | uint8_t abPadding[192];
|
---|
134 | # else
|
---|
135 | uint8_t abPadding[1024];
|
---|
136 | # endif
|
---|
137 | #elif defined(RT_OS_LINUX)
|
---|
138 | uint8_t abPadding[320];
|
---|
139 | #else
|
---|
140 | uint8_t abPadding[64];
|
---|
141 | #endif
|
---|
142 | } u;
|
---|
143 | /** The interface name. */
|
---|
144 | char szName[1];
|
---|
145 | };
|
---|
146 | typedef struct VBoxNetAdapter VBOXNETADP;
|
---|
147 | typedef VBOXNETADP *PVBOXNETADP;
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * The global data of the VBox filter driver.
|
---|
151 | *
|
---|
152 | * This contains the bit required for communicating with support driver, VBoxDrv
|
---|
153 | * (start out as SupDrv).
|
---|
154 | */
|
---|
155 | typedef struct VBOXNETADPGLOBALS
|
---|
156 | {
|
---|
157 | /** Mutex protecting the list of instances and state changes. */
|
---|
158 | RTSEMFASTMUTEX hFastMtx;
|
---|
159 | /** Array of adapter instances. */
|
---|
160 | VBOXNETADP aAdapters[VBOXNETADP_MAX_INSTANCES];
|
---|
161 |
|
---|
162 | /** The INTNET trunk network interface factory. */
|
---|
163 | INTNETTRUNKFACTORY TrunkFactory;
|
---|
164 | /** The SUPDRV component factory registration. */
|
---|
165 | SUPDRVFACTORY SupDrvFactory;
|
---|
166 | /** The number of current factory references. */
|
---|
167 | int32_t volatile cFactoryRefs;
|
---|
168 | /** The SUPDRV IDC handle (opaque struct). */
|
---|
169 | SUPDRVIDCHANDLE SupDrvIDC;
|
---|
170 | } VBOXNETADPGLOBALS;
|
---|
171 |
|
---|
172 |
|
---|
173 | DECLHIDDEN(void) vboxNetAdpComposeMACAddress(PVBOXNETADP pThis, PRTMAC pMac);
|
---|
174 | DECLHIDDEN(void) vboxNetAdpReceive(PVBOXNETADP pThis, PINTNETSG pSG);
|
---|
175 | DECLHIDDEN(bool) vboxNetAdpPrepareToReceive(PVBOXNETADP pThis);
|
---|
176 | DECLHIDDEN(void) vboxNetAdpCancelReceive(PVBOXNETADP pThis);
|
---|
177 |
|
---|
178 | DECLHIDDEN(int) vboxNetAdpInitGlobals(PVBOXNETADPGLOBALS pGlobals);
|
---|
179 | DECLHIDDEN(int) vboxNetAdpTryDeleteGlobals(PVBOXNETADPGLOBALS pGlobals);
|
---|
180 | DECLHIDDEN(bool) vboxNetAdpCanUnload(PVBOXNETADPGLOBALS pGlobals);
|
---|
181 |
|
---|
182 | DECLHIDDEN(void) vboxNetAdpRetain(PVBOXNETADP pThis);
|
---|
183 | DECLHIDDEN(void) vboxNetAdpRelease(PVBOXNETADP pThis);
|
---|
184 | DECLHIDDEN(void) vboxNetAdpBusy(PVBOXNETADP pThis);
|
---|
185 | DECLHIDDEN(void) vboxNetAdpIdle(PVBOXNETADP pThis);
|
---|
186 |
|
---|
187 | DECLHIDDEN(int) vboxNetAdpInitGlobalsBase(PVBOXNETADPGLOBALS pGlobals);
|
---|
188 | DECLHIDDEN(int) vboxNetAdpInitIdc(PVBOXNETADPGLOBALS pGlobals);
|
---|
189 | DECLHIDDEN(void) vboxNetAdpDeleteGlobalsBase(PVBOXNETADPGLOBALS pGlobals);
|
---|
190 | DECLHIDDEN(int) vboxNetAdpTryDeleteIdc(PVBOXNETADPGLOBALS pGlobals);
|
---|
191 |
|
---|
192 |
|
---|
193 |
|
---|
194 | /** @name The OS specific interface.
|
---|
195 | * @{ */
|
---|
196 | /**
|
---|
197 | * Transmits a frame.
|
---|
198 | *
|
---|
199 | * @return IPRT status code.
|
---|
200 | * @param pThis The new instance.
|
---|
201 | * @param pSG The (scatter/)gather list.
|
---|
202 | * @param fDst The destination mask. At least one bit will be set.
|
---|
203 | *
|
---|
204 | * @remarks Owns the out-bound trunk port semaphore.
|
---|
205 | */
|
---|
206 | DECLHIDDEN(int) vboxNetAdpPortOsXmit(PVBOXNETADP pThis, PINTNETSG pSG, uint32_t fDst);
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Checks if the interface is in promiscuous mode from the host perspective.
|
---|
210 | *
|
---|
211 | * If it is, then the internal networking switch will send frames
|
---|
212 | * heading for the wire to the host as well.
|
---|
213 | *
|
---|
214 | * @see INTNETTRUNKIFPORT::pfnIsPromiscuous for more details.
|
---|
215 | *
|
---|
216 | * @returns true / false accordingly.
|
---|
217 | * @param pThis The instance.
|
---|
218 | *
|
---|
219 | * @remarks Owns the network lock and the out-bound trunk port semaphores.
|
---|
220 | */
|
---|
221 | DECLHIDDEN(bool) vboxNetAdpPortOsIsPromiscuous(PVBOXNETADP pThis);
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Get the MAC address of the interface we're attached to.
|
---|
225 | *
|
---|
226 | * Used by the internal networking switch for implementing the
|
---|
227 | * shared-MAC-on-the-wire mode.
|
---|
228 | *
|
---|
229 | * @param pThis The instance.
|
---|
230 | * @param pMac Where to store the MAC address.
|
---|
231 | * If you don't know, set all the bits except the first (the multicast one).
|
---|
232 | *
|
---|
233 | * @remarks Owns the network lock and the out-bound trunk port semaphores.
|
---|
234 | */
|
---|
235 | DECLHIDDEN(void) vboxNetAdpPortOsGetMacAddress(PVBOXNETADP pThis, PRTMAC pMac);
|
---|
236 |
|
---|
237 | /**
|
---|
238 | * Checks if the specified MAC address is for any of the host interfaces.
|
---|
239 | *
|
---|
240 | * Used by the internal networking switch to decide the destination(s)
|
---|
241 | * of a frame.
|
---|
242 | *
|
---|
243 | * @returns true / false accordingly.
|
---|
244 | * @param pThis The instance.
|
---|
245 | * @param pMac The MAC address.
|
---|
246 | *
|
---|
247 | * @remarks Owns the network lock and the out-bound trunk port semaphores.
|
---|
248 | */
|
---|
249 | DECLHIDDEN(bool) vboxNetAdpPortOsIsHostMac(PVBOXNETADP pThis, PCRTMAC pMac);
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * This is called to when disconnecting from a network.
|
---|
253 | *
|
---|
254 | * @return IPRT status code.
|
---|
255 | * @param pThis The new instance.
|
---|
256 | *
|
---|
257 | * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
|
---|
258 | */
|
---|
259 | DECLHIDDEN(int) vboxNetAdpOsDisconnectIt(PVBOXNETADP pThis);
|
---|
260 |
|
---|
261 | /**
|
---|
262 | * This is called to when connecting to a network.
|
---|
263 | *
|
---|
264 | * @return IPRT status code.
|
---|
265 | * @param pThis The new instance.
|
---|
266 | *
|
---|
267 | * @remarks Owns the semaphores for the global list, the network lock and the out-bound trunk port.
|
---|
268 | */
|
---|
269 | DECLHIDDEN(int) vboxNetAdpOsConnectIt(PVBOXNETADP pThis);
|
---|
270 |
|
---|
271 | /**
|
---|
272 | * Counter part to vboxNetAdpOsCreate().
|
---|
273 | *
|
---|
274 | * @return IPRT status code.
|
---|
275 | * @param pThis The new instance.
|
---|
276 | *
|
---|
277 | * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
|
---|
278 | */
|
---|
279 | DECLHIDDEN(void) vboxNetAdpOsDestroy(PVBOXNETADP pThis);
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * This is called to attach to the actual host interface
|
---|
283 | * after linking the instance into the list.
|
---|
284 | *
|
---|
285 | * @return IPRT status code.
|
---|
286 | * @param pThis The new instance.
|
---|
287 | * @param pMac The MAC address to use for this instance.
|
---|
288 | *
|
---|
289 | * @remarks Owns no locks.
|
---|
290 | */
|
---|
291 | DECLHIDDEN(int) vboxNetAdpOsCreate(PVBOXNETADP pThis, PCRTMAC pMac);
|
---|
292 |
|
---|
293 | /** @} */
|
---|
294 |
|
---|
295 |
|
---|
296 | __END_DECLS
|
---|
297 |
|
---|
298 | #endif
|
---|
299 |
|
---|