VirtualBox

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

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

net[flt|adp]/win: disable delayed packet processing when intnet is on

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 15.7 KB
Line 
1/* $Id: VBoxNetFltInternal.h 29108 2010-05-05 20:17:42Z 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 /** Whether this is a VLAN interface or not. */
206 bool volatile fVLAN;
207 /** Layered device handle to the interface. */
208 ldi_handle_t hIface;
209 /** The MAC address of the interface. */
210 RTMAC MacAddr;
211 /** Mutex protection used for loopback. */
212 RTSEMFASTMUTEX hFastMtx;
213 /** Mutex protection used for dynamic IPv6 attaches. */
214 RTSEMFASTMUTEX hPollMtx;
215# endif
216 /** @} */
217# elif defined(RT_OS_FREEBSD)
218 /** @name FreeBSD instance data.
219 * @{ */
220 /** Interface handle */
221 struct ifnet *ifp;
222 /** Netgraph node handle */
223 node_p node;
224 /** Input hook */
225 hook_p input;
226 /** Output hook */
227 hook_p output;
228 /** Original interface flags */
229 unsigned int flags;
230 /** Input queue */
231 struct ifqueue inq;
232 /** Output queue */
233 struct ifqueue outq;
234 /** Input task */
235 struct task tskin;
236 /** Output task */
237 struct task tskout;
238 /** The MAC address of the interface. */
239 RTMAC MacAddr;
240 /** @} */
241# elif defined(RT_OS_WINDOWS)
242 /** @name Windows instance data.
243 * @{ */
244 /** Filter driver device context. */
245 ADAPT IfAdaptor;
246
247 volatile uint32_t cModeNetFltRefs;
248 volatile uint32_t cModePassThruRefs;
249#ifndef VBOXNETFLT_NO_PACKET_QUEUE
250 /** Packet worker thread info */
251 PACKET_QUEUE_WORKER PacketQueueWorker;
252#endif
253 /** The MAC address of the interface. Caching MAC for performance reasons. */
254 RTMAC MacAddr;
255 /** mutex used to synchronize ADAPT init/deinit */
256 RTSEMMUTEX hAdaptMutex;
257 /** @} */
258# else
259# error "PORTME"
260# endif
261 } s;
262#endif
263 /** Padding. */
264#if defined(RT_OS_WINDOWS)
265# if defined(VBOX_NETFLT_ONDEMAND_BIND)
266 uint8_t abPadding[192];
267# elif defined(VBOXNETADP)
268 uint8_t abPadding[256];
269# else
270 uint8_t abPadding[1024];
271# endif
272#elif defined(RT_OS_LINUX)
273 uint8_t abPadding[320];
274#elif defined(RT_OS_FREEBSD)
275 uint8_t abPadding[320];
276#else
277 uint8_t abPadding[128];
278#endif
279 } u;
280
281 /** The interface name. */
282 char szName[1];
283} VBOXNETFLTINS;
284/** Pointer to the instance data of a host network filter driver. */
285typedef struct VBOXNETFLTINS *PVBOXNETFLTINS;
286
287AssertCompileMemberAlignment(VBOXNETFLTINS, NanoTSLastRediscovery, 8);
288#ifdef VBOXNETFLT_OS_SPECFIC
289AssertCompile(RT_SIZEOFMEMB(VBOXNETFLTINS, u.s) <= RT_SIZEOFMEMB(VBOXNETFLTINS, u.abPadding));
290#endif
291
292
293/**
294 * The global data of the VBox filter driver.
295 *
296 * This contains the bit required for communicating with support driver, VBoxDrv
297 * (start out as SupDrv).
298 */
299typedef struct VBOXNETFLTGLOBALS
300{
301 /** Mutex protecting the list of instances and state changes. */
302 RTSEMFASTMUTEX hFastMtx;
303 /** Pointer to a list of instance data. */
304 PVBOXNETFLTINS pInstanceHead;
305
306 /** The INTNET trunk network interface factory. */
307 INTNETTRUNKFACTORY TrunkFactory;
308 /** The SUPDRV component factory registration. */
309 SUPDRVFACTORY SupDrvFactory;
310 /** The number of current factory references. */
311 int32_t volatile cFactoryRefs;
312 /** Whether the IDC connection is open or not.
313 * This is only for cleaning up correctly after the separate IDC init on Windows. */
314 bool fIDCOpen;
315 /** The SUPDRV IDC handle (opaque struct). */
316 SUPDRVIDCHANDLE SupDrvIDC;
317} VBOXNETFLTGLOBALS;
318
319
320DECLHIDDEN(int) vboxNetFltInitGlobalsAndIdc(PVBOXNETFLTGLOBALS pGlobals);
321DECLHIDDEN(int) vboxNetFltInitGlobals(PVBOXNETFLTGLOBALS pGlobals);
322DECLHIDDEN(int) vboxNetFltInitIdc(PVBOXNETFLTGLOBALS pGlobals);
323DECLHIDDEN(int) vboxNetFltTryDeleteIdcAndGlobals(PVBOXNETFLTGLOBALS pGlobals);
324DECLHIDDEN(void) vboxNetFltDeleteGlobals(PVBOXNETFLTGLOBALS pGlobals);
325DECLHIDDEN(int) vboxNetFltTryDeleteIdc(PVBOXNETFLTGLOBALS pGlobals);
326
327DECLHIDDEN(bool) vboxNetFltCanUnload(PVBOXNETFLTGLOBALS pGlobals);
328DECLHIDDEN(PVBOXNETFLTINS) vboxNetFltFindInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName);
329
330DECLHIDDEN(void) vboxNetFltRetain(PVBOXNETFLTINS pThis, bool fBusy);
331DECLHIDDEN(bool) vboxNetFltTryRetainBusyActive(PVBOXNETFLTINS pThis);
332DECLHIDDEN(bool) vboxNetFltTryRetainBusyNotDisconnected(PVBOXNETFLTINS pThis);
333DECLHIDDEN(void) vboxNetFltRelease(PVBOXNETFLTINS pThis, bool fBusy);
334
335#ifdef VBOXNETFLT_STATIC_CONFIG
336DECLHIDDEN(int) vboxNetFltSearchCreateInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName, PVBOXNETFLTINS *ppInstance, void * pContext);
337#endif
338
339
340
341/** @name The OS specific interface.
342 * @{ */
343/**
344 * Try rediscover the host interface.
345 *
346 * This is called periodically from the transmit path if we're marked as
347 * disconnected from the host. There is no chance of a race here.
348 *
349 * @returns true if the interface was successfully rediscovered and reattach,
350 * otherwise false.
351 * @param pThis The new instance.
352 */
353DECLHIDDEN(bool) vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis);
354
355/**
356 * Transmits a frame.
357 *
358 * @return IPRT status code.
359 * @param pThis The new instance.
360 * @param pSG The (scatter/)gather list.
361 * @param fDst The destination mask. At least one bit will be set.
362 *
363 * @remarks Owns the out-bound trunk port semaphore.
364 */
365DECLHIDDEN(int) vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, PINTNETSG pSG, uint32_t fDst);
366
367/**
368 * This is called when activating or suspending the instance.
369 *
370 * Use this method to enable and disable promiscuous mode on
371 * the interface to prevent unnecessary interrupt load.
372 *
373 * It is only called when the state changes.
374 *
375 * @param pThis The instance.
376 *
377 * @remarks Owns the lock for the out-bound trunk port.
378 */
379DECLHIDDEN(void) vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive);
380
381/**
382 * This is called to when disconnecting from a network.
383 *
384 * @return IPRT status code.
385 * @param pThis The new instance.
386 *
387 * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
388 */
389DECLHIDDEN(int) vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis);
390
391/**
392 * This is called to when connecting to a network.
393 *
394 * @return IPRT status code.
395 * @param pThis The new instance.
396 *
397 * @remarks Owns the semaphores for the global list, the network lock and the out-bound trunk port.
398 */
399DECLHIDDEN(int) vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis);
400
401/**
402 * Counter part to vboxNetFltOsInitInstance().
403 *
404 * @return IPRT status code.
405 * @param pThis The new instance.
406 *
407 * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
408 */
409DECLHIDDEN(void) vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis);
410
411/**
412 * This is called to attach to the actual host interface
413 * after linking the instance into the list.
414 *
415 * The MAC address as well promiscuousness and GSO capabilities should be
416 * reported by this function.
417 *
418 * @return IPRT status code.
419 * @param pThis The new instance.
420 * @param pvContext The user supplied context in the static config only.
421 * NULL in the dynamic config.
422 *
423 * @remarks Owns no locks.
424 */
425DECLHIDDEN(int) vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis, void *pvContext);
426
427/**
428 * This is called to perform structure initializations.
429 *
430 * @return IPRT status code.
431 * @param pThis The new instance.
432 *
433 * @remarks Owns no locks.
434 */
435DECLHIDDEN(int) vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis);
436/** @} */
437
438
439RT_C_DECLS_END
440
441#endif
442
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