VirtualBox

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

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

build/alignment fix.

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