VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxNetFlt/VBoxNetFltInternal.h@ 28666

Last change on this file since 28666 was 28666, checked in by vboxsync, 15 years ago

SrvIntNet,VBoxNetFlt: Changing from getting to reporting (VBoxNetFlt reports stuff to SrvIntNet).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 15.5 KB
Line 
1/* $Id: VBoxNetFltInternal.h 28666 2010-04-23 17:25:58Z vboxsync $ */
2/** @file
3 * VBoxNetFlt - 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 ___VBoxNetFltInternal_h___
23#define ___VBoxNetFltInternal_h___
24
25#include <VBox/sup.h>
26#include <VBox/intnet.h>
27#include <iprt/semaphore.h>
28#include <iprt/assert.h>
29
30
31RT_C_DECLS_BEGIN
32
33/** Pointer to the globals. */
34typedef struct VBOXNETFLTGLOBALS *PVBOXNETFLTGLOBALS;
35
36
37/**
38 * The state of a filter driver instance.
39 *
40 * The state machine differs a bit between the platforms because of
41 * the way we hook into the stack. On some hosts we can dynamically
42 * attach when required (on CreateInstance) and on others we will
43 * have to connect when the network stack is bound up. These modes
44 * are called static and dynamic config and governed at compile time
45 * by the VBOXNETFLT_STATIC_CONFIG define.
46 *
47 * See sec_netflt_msc for more details on locking and synchronization.
48 */
49typedef enum VBOXNETFTLINSSTATE
50{
51 /** The usual invalid state. */
52 kVBoxNetFltInsState_Invalid = 0,
53 /** Initialization.
54 * We've reserved the interface name but need to attach to the actual
55 * network interface outside the lock to avoid deadlocks.
56 * In the dynamic case this happens during a Create(Instance) call.
57 * In the static case it happens during driver initialization. */
58 kVBoxNetFltInsState_Initializing,
59#ifdef VBOXNETFLT_STATIC_CONFIG
60 /** Unconnected, not hooked up to a switch (static only).
61 * The filter driver instance has been instantiated and hooked up,
62 * waiting to be connected to an internal network. */
63 kVBoxNetFltInsState_Unconnected,
64#endif
65 /** Connected to an internal network. */
66 kVBoxNetFltInsState_Connected,
67 /** Disconnecting from the internal network and possibly the host network interface.
68 * Partly for reasons of deadlock avoidance again. */
69 kVBoxNetFltInsState_Disconnecting,
70 /** The instance has been disconnected from both the host and the internal network. */
71 kVBoxNetFltInsState_Destroyed,
72
73 /** The habitual 32-bit enum hack. */
74 kVBoxNetFltInsState_32BitHack = 0x7fffffff
75} VBOXNETFTLINSSTATE;
76
77
78/**
79 * The per-instance data of the VBox filter driver.
80 *
81 * This is data associated with a network interface / NIC / wossname which
82 * the filter driver has been or may be attached to. When possible it is
83 * attached dynamically, but this may not be possible on all OSes so we have
84 * to be flexible about things.
85 *
86 * A network interface / NIC / wossname can only have one filter driver
87 * instance attached to it. So, attempts at connecting an internal network
88 * to an interface that's already in use (connected to another internal network)
89 * will result in a VERR_SHARING_VIOLATION.
90 *
91 * Only one internal network can connect to a filter driver instance.
92 */
93typedef struct VBOXNETFLTINS
94{
95 /** Pointer to the next interface in the list. (VBOXNETFLTGLOBAL::pInstanceHead) */
96 struct VBOXNETFLTINS *pNext;
97 /** Our RJ-45 port.
98 * This is what the internal network plugs into. */
99 INTNETTRUNKIFPORT MyPort;
100 /** The RJ-45 port on the INTNET "switch".
101 * This is what we're connected to. */
102 PINTNETTRUNKSWPORT pSwitchPort;
103 /** Pointer to the globals. */
104 PVBOXNETFLTGLOBALS pGlobals;
105
106 /** The spinlock protecting the state variables and host interface handle. */
107 RTSPINLOCK hSpinlock;
108 /** The current interface state. */
109 VBOXNETFTLINSSTATE volatile enmState;
110 /** Active / Suspended indicator. */
111 bool volatile fActive;
112 /** Disconnected from the host network interface. */
113 bool volatile fDisconnectedFromHost;
114 /** Rediscovery is pending.
115 * cBusy will never reach zero during rediscovery, so which
116 * takes care of serializing rediscovery and disconnecting. */
117 bool volatile fRediscoveryPending;
118 /** Whether we should not attempt to set promiscuous mode at all. */
119 bool fDisablePromiscuous;
120#if (ARCH_BITS == 32) && defined(__GNUC__)
121 uint32_t u32Padding; /**< Alignment padding, will assert in ASMAtomicUoWriteU64 otherwise. */
122#endif
123 /** The timestamp of the last rediscovery. */
124 uint64_t volatile NanoTSLastRediscovery;
125 /** Reference count. */
126 uint32_t volatile cRefs;
127 /** The busy count.
128 * This counts the number of current callers and pending packet. */
129 uint32_t volatile cBusy;
130 /** The event that is signaled when we go idle and that pfnWaitForIdle blocks on. */
131 RTSEMEVENT hEventIdle;
132
133 /** @todo move MacAddr out of this structure! */
134 union
135 {
136#ifdef VBOXNETFLT_OS_SPECFIC
137 struct
138 {
139# if defined(RT_OS_DARWIN)
140 /** @name Darwin instance data.
141 * @{ */
142 /** Pointer to the darwin network interface we're attached to.
143 * This is treated as highly volatile and should only be read and retained
144 * while owning hSpinlock. Releasing references to this should not be done
145 * while owning it though as we might end up destroying it in some paths. */
146 ifnet_t volatile pIfNet;
147 /** The interface filter handle.
148 * Same access rules as with pIfNet. */
149 interface_filter_t volatile pIfFilter;
150 /** Whether we've need to set promiscuous mode when the interface comes up. */
151 bool volatile fNeedSetPromiscuous;
152 /** Whether we've successfully put the interface into to promiscuous mode.
153 * This is for dealing with the ENETDOWN case. */
154 bool volatile fSetPromiscuous;
155 /** The MAC address of the interface. */
156 RTMAC MacAddr;
157 /** @} */
158# elif defined(RT_OS_LINUX)
159 /** @name Linux instance data
160 * @{ */
161 /** Pointer to the device. */
162 struct net_device volatile *pDev;
163 /** Whether we've successfully put the interface into to promiscuous mode.
164 * This is for dealing with the ENETDOWN case. */
165 bool volatile fPromiscuousSet;
166 /** Whether device exists and physically attached. */
167 bool volatile fRegistered;
168 /** The MAC address of the interface. */
169 RTMAC MacAddr;
170 struct notifier_block Notifier;
171 struct packet_type PacketType;
172 struct sk_buff_head XmitQueue;
173 struct work_struct XmitTask;
174 /** @} */
175# elif defined(RT_OS_SOLARIS)
176 /** @name Solaris instance data.
177 * @{ */
178# ifdef VBOX_WITH_NETFLT_CROSSBOW
179 /** The link Id of the VNIC */
180 datalink_id_t VNICLinkId;
181 /** Instance number of VNIC */
182 uint16_t uInstance;
183 /** Whether we created the VNIC or not */
184 bool fCreatedVNIC;
185 /** The lower MAC handle */
186 mac_handle_t hInterface;
187 /** The client MAC handle */
188 mac_client_handle_t hClient;
189 /** The unicast address handle */
190 mac_unicast_handle_t hUnicast;
191 /** The promiscuous handle */
192 mac_promisc_handle_t hPromiscuous;
193 /** The MAC address of the interface */
194 RTMAC MacAddr;
195# else
196 /** Pointer to the bound IPv4 stream. */
197 void volatile *pvIp4Stream;
198 /** Pointer to the bound IPv6 stream. */
199 void volatile *pvIp6Stream;
200 /** Pointer to the bound ARP stream. */
201 void volatile *pvArpStream;
202 /** Pointer to the unbound promiscuous stream. */
203 void volatile *pvPromiscStream;
204 /** Whether we are attaching to IPv6 stream dynamically now. */
205 bool volatile fAttaching;
206 /** Layered device handle to the interface. */
207 ldi_handle_t hIface;
208 /** The MAC address of the interface. */
209 RTMAC MacAddr;
210 /** Mutex protection used for loopback. */
211 RTSEMFASTMUTEX hFastMtx;
212 /** Mutex protection used for dynamic IPv6 attaches. */
213 RTSEMFASTMUTEX hPollMtx;
214# endif
215 /** @} */
216# elif defined(RT_OS_FREEBSD)
217 /** @name FreeBSD instance data.
218 * @{ */
219 /** Interface handle */
220 struct ifnet *ifp;
221 /** Netgraph node handle */
222 node_p node;
223 /** Input hook */
224 hook_p input;
225 /** Output hook */
226 hook_p output;
227 /** Original interface flags */
228 unsigned int flags;
229 /** Input queue */
230 struct ifqueue inq;
231 /** Output queue */
232 struct ifqueue outq;
233 /** Input task */
234 struct task tskin;
235 /** Output task */
236 struct task tskout;
237 /** The MAC address of the interface. */
238 RTMAC MacAddr;
239 /** @} */
240# elif defined(RT_OS_WINDOWS)
241 /** @name Windows instance data.
242 * @{ */
243 /** Filter driver device context. */
244 ADAPT IfAdaptor;
245
246 volatile uint32_t cModeNetFltRefs;
247 volatile uint32_t cModePassThruRefs;
248
249 /** Packet worker thread info */
250 PACKET_QUEUE_WORKER PacketQueueWorker;
251 /** The MAC address of the interface. Caching MAC for performance reasons. */
252 RTMAC MacAddr;
253 /** mutex used to synchronize ADAPT init/deinit */
254 RTSEMMUTEX hAdaptMutex;
255 /** @} */
256# else
257# error "PORTME"
258# endif
259 } s;
260#endif
261 /** Padding. */
262#if defined(RT_OS_WINDOWS)
263# if defined(VBOX_NETFLT_ONDEMAND_BIND)
264 uint8_t abPadding[192];
265# elif defined(VBOXNETADP)
266 uint8_t abPadding[256];
267# else
268 uint8_t abPadding[1024];
269# endif
270#elif defined(RT_OS_LINUX)
271 uint8_t abPadding[320];
272#elif defined(RT_OS_FREEBSD)
273 uint8_t abPadding[320];
274#else
275 uint8_t abPadding[128];
276#endif
277 } u;
278
279 /** The interface name. */
280 char szName[1];
281} VBOXNETFLTINS;
282/** Pointer to the instance data of a host network filter driver. */
283typedef struct VBOXNETFLTINS *PVBOXNETFLTINS;
284
285AssertCompileMemberAlignment(VBOXNETFLTINS, NanoTSLastRediscovery, 8);
286#ifdef VBOXNETFLT_OS_SPECFIC
287AssertCompile(RT_SIZEOFMEMB(VBOXNETFLTINS, u.s) <= RT_SIZEOFMEMB(VBOXNETFLTINS, u.abPadding));
288#endif
289
290
291/**
292 * The global data of the VBox filter driver.
293 *
294 * This contains the bit required for communicating with support driver, VBoxDrv
295 * (start out as SupDrv).
296 */
297typedef struct VBOXNETFLTGLOBALS
298{
299 /** Mutex protecting the list of instances and state changes. */
300 RTSEMFASTMUTEX hFastMtx;
301 /** Pointer to a list of instance data. */
302 PVBOXNETFLTINS pInstanceHead;
303
304 /** The INTNET trunk network interface factory. */
305 INTNETTRUNKFACTORY TrunkFactory;
306 /** The SUPDRV component factory registration. */
307 SUPDRVFACTORY SupDrvFactory;
308 /** The number of current factory references. */
309 int32_t volatile cFactoryRefs;
310 /** Whether the IDC connection is open or not.
311 * This is only for cleaning up correctly after the separate IDC init on Windows. */
312 bool fIDCOpen;
313 /** The SUPDRV IDC handle (opaque struct). */
314 SUPDRVIDCHANDLE SupDrvIDC;
315} VBOXNETFLTGLOBALS;
316
317
318DECLHIDDEN(int) vboxNetFltInitGlobalsAndIdc(PVBOXNETFLTGLOBALS pGlobals);
319DECLHIDDEN(int) vboxNetFltInitGlobals(PVBOXNETFLTGLOBALS pGlobals);
320DECLHIDDEN(int) vboxNetFltInitIdc(PVBOXNETFLTGLOBALS pGlobals);
321DECLHIDDEN(int) vboxNetFltTryDeleteIdcAndGlobals(PVBOXNETFLTGLOBALS pGlobals);
322DECLHIDDEN(void) vboxNetFltDeleteGlobals(PVBOXNETFLTGLOBALS pGlobals);
323DECLHIDDEN(int) vboxNetFltTryDeleteIdc(PVBOXNETFLTGLOBALS pGlobals);
324
325DECLHIDDEN(bool) vboxNetFltCanUnload(PVBOXNETFLTGLOBALS pGlobals);
326DECLHIDDEN(PVBOXNETFLTINS) vboxNetFltFindInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName);
327
328DECLHIDDEN(void) vboxNetFltRetain(PVBOXNETFLTINS pThis, bool fBusy);
329DECLHIDDEN(void) vboxNetFltRelease(PVBOXNETFLTINS pThis, bool fBusy);
330
331#ifdef VBOXNETFLT_STATIC_CONFIG
332DECLHIDDEN(int) vboxNetFltSearchCreateInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName, PVBOXNETFLTINS *ppInstance, void * pContext);
333#endif
334
335
336
337/** @name The OS specific interface.
338 * @{ */
339/**
340 * Try rediscover the host interface.
341 *
342 * This is called periodically from the transmit path if we're marked as
343 * disconnected from the host. There is no chance of a race here.
344 *
345 * @returns true if the interface was successfully rediscovered and reattach,
346 * otherwise false.
347 * @param pThis The new instance.
348 */
349DECLHIDDEN(bool) vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis);
350
351/**
352 * Transmits a frame.
353 *
354 * @return IPRT status code.
355 * @param pThis The new instance.
356 * @param pSG The (scatter/)gather list.
357 * @param fDst The destination mask. At least one bit will be set.
358 *
359 * @remarks Owns the out-bound trunk port semaphore.
360 */
361DECLHIDDEN(int) vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, PINTNETSG pSG, uint32_t fDst);
362
363/**
364 * This is called when activating or suspending the instance.
365 *
366 * Use this method to enable and disable promiscuous mode on
367 * the interface to prevent unnecessary interrupt load.
368 *
369 * It is only called when the state changes.
370 *
371 * @param pThis The instance.
372 *
373 * @remarks Owns the lock for the out-bound trunk port.
374 */
375DECLHIDDEN(void) vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive);
376
377/**
378 * This is called to when disconnecting from a network.
379 *
380 * @return IPRT status code.
381 * @param pThis The new instance.
382 *
383 * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
384 */
385DECLHIDDEN(int) vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis);
386
387/**
388 * This is called to when connecting to a network.
389 *
390 * @return IPRT status code.
391 * @param pThis The new instance.
392 *
393 * @remarks Owns the semaphores for the global list, the network lock and the out-bound trunk port.
394 */
395DECLHIDDEN(int) vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis);
396
397/**
398 * Counter part to vboxNetFltOsInitInstance().
399 *
400 * @return IPRT status code.
401 * @param pThis The new instance.
402 *
403 * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
404 */
405DECLHIDDEN(void) vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis);
406
407/**
408 * This is called to attach to the actual host interface
409 * after linking the instance into the list.
410 *
411 * The MAC address as well promiscuousness and GSO capabilities should be
412 * reported by this function.
413 *
414 * @return IPRT status code.
415 * @param pThis The new instance.
416 * @param pvContext The user supplied context in the static config only.
417 * NULL in the dynamic config.
418 *
419 * @remarks Owns no locks.
420 */
421DECLHIDDEN(int) vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis, void *pvContext);
422
423/**
424 * This is called to perform structure initializations.
425 *
426 * @return IPRT status code.
427 * @param pThis The new instance.
428 *
429 * @remarks Owns no locks.
430 */
431DECLHIDDEN(int) vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis);
432/** @} */
433
434
435RT_C_DECLS_END
436
437#endif
438
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