VirtualBox

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

Last change on this file since 34079 was 33141, checked in by vboxsync, 14 years ago

VBoxNetFlt/solaris: fix panic with using fastmutex with preemption disabled.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 16.7 KB
Line 
1/* $Id: VBoxNetFltInternal.h 33141 2010-10-14 16:35:21Z 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#if 0
119 uint32_t u32Padding; /**< Alignment padding, will assert in ASMAtomicUoWriteU64 otherwise. */
120#endif
121#endif
122 /** The timestamp of the last rediscovery. */
123 uint64_t volatile NanoTSLastRediscovery;
124 /** Reference count. */
125 uint32_t volatile cRefs;
126 /** The busy count.
127 * This counts the number of current callers and pending packet. */
128 uint32_t volatile cBusy;
129 /** The event that is signaled when we go idle and that pfnWaitForIdle blocks on. */
130 RTSEMEVENT hEventIdle;
131
132 /** @todo move MacAddr out of this structure! */
133 union
134 {
135#ifdef VBOXNETFLT_OS_SPECFIC
136 struct
137 {
138# if defined(RT_OS_DARWIN)
139 /** @name Darwin instance data.
140 * @{ */
141 /** Pointer to the darwin network interface we're attached to.
142 * This is treated as highly volatile and should only be read and retained
143 * while owning hSpinlock. Releasing references to this should not be done
144 * while owning it though as we might end up destroying it in some paths. */
145 ifnet_t volatile pIfNet;
146 /** The interface filter handle.
147 * Same access rules as with pIfNet. */
148 interface_filter_t volatile pIfFilter;
149 /** Whether we've need to set promiscuous mode when the interface comes up. */
150 bool volatile fNeedSetPromiscuous;
151 /** Whether we've successfully put the interface into to promiscuous mode.
152 * This is for dealing with the ENETDOWN case. */
153 bool volatile fSetPromiscuous;
154 /** The MAC address of the interface. */
155 RTMAC MacAddr;
156 /** @} */
157# elif defined(RT_OS_LINUX)
158 /** @name Linux instance data
159 * @{ */
160 /** Pointer to the device. */
161 struct net_device * volatile pDev;
162 /** Whether we've successfully put the interface into to promiscuous mode.
163 * This is for dealing with the ENETDOWN case. */
164 bool volatile fPromiscuousSet;
165 /** Whether device exists and physically attached. */
166 bool volatile fRegistered;
167 /** The MAC address of the interface. */
168 RTMAC MacAddr;
169 struct notifier_block Notifier;
170 struct packet_type PacketType;
171# ifndef VBOXNETFLT_LINUX_NO_XMIT_QUEUE
172 struct sk_buff_head XmitQueue;
173 struct work_struct XmitTask;
174# endif
175 /** @} */
176# elif defined(RT_OS_SOLARIS)
177 /** @name Solaris instance data.
178 * @{ */
179# ifdef VBOX_WITH_NETFLT_CROSSBOW
180 /** Whether the underlying interface is a VNIC or not. */
181 bool fIsVNIC;
182 /** Handle to list of created VNICs. */
183 list_t hVNICs;
184 /** Instance number while creating VNICs. */
185 uint64_t uInstance;
186 /** Pointer to the VNIC instance data. */
187 void *pvVNIC;
188 /** The MAC address of the host interface. */
189 RTMAC MacAddr;
190 /** Whether required capabilities have been reported. */
191 bool fReportedInfo;
192# else
193 /** Pointer to the bound IPv4 stream. */
194 struct vboxnetflt_stream_t * volatile pIp4Stream;
195 /** Pointer to the bound IPv6 stream. */
196 struct vboxnetflt_stream_t * volatile pIp6Stream;
197 /** Pointer to the bound ARP stream. */
198 struct vboxnetflt_stream_t * volatile pArpStream;
199 /** Pointer to the unbound promiscuous stream. */
200 struct vboxnetflt_promisc_stream_t * volatile pPromiscStream;
201 /** Whether we are attaching to IPv6 stream dynamically now. */
202 bool volatile fAttaching;
203 /** Whether this is a VLAN interface or not. */
204 bool volatile fVLAN;
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 kmutex_t hMtx;
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#ifndef VBOXNETFLT_NO_PACKET_QUEUE
248 /** Packet worker thread info */
249 PACKET_QUEUE_WORKER PacketQueueWorker;
250#endif
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(bool) vboxNetFltTryRetainBusyActive(PVBOXNETFLTINS pThis);
330DECLHIDDEN(bool) vboxNetFltTryRetainBusyNotDisconnected(PVBOXNETFLTINS pThis);
331DECLHIDDEN(void) vboxNetFltRelease(PVBOXNETFLTINS pThis, bool fBusy);
332
333#ifdef VBOXNETFLT_STATIC_CONFIG
334DECLHIDDEN(int) vboxNetFltSearchCreateInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName, PVBOXNETFLTINS *ppInstance, void * pContext);
335#endif
336
337
338
339/** @name The OS specific interface.
340 * @{ */
341/**
342 * Try rediscover the host interface.
343 *
344 * This is called periodically from the transmit path if we're marked as
345 * disconnected from the host. There is no chance of a race here.
346 *
347 * @returns true if the interface was successfully rediscovered and reattach,
348 * otherwise false.
349 * @param pThis The new instance.
350 */
351DECLHIDDEN(bool) vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis);
352
353/**
354 * Transmits a frame.
355 *
356 * @return IPRT status code.
357 * @param pThis The new instance.
358 * @param pvIfData Pointer to the host-private interface data.
359 * @param pSG The (scatter/)gather list.
360 * @param fDst The destination mask. At least one bit will be set.
361 *
362 * @remarks Owns the out-bound trunk port semaphore.
363 */
364DECLHIDDEN(int) vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, void *pvIfData, PINTNETSG pSG, uint32_t fDst);
365
366/**
367 * This is called when activating or suspending the instance.
368 *
369 * Use this method to enable and disable promiscuous mode on
370 * the interface to prevent unnecessary interrupt load.
371 *
372 * It is only called when the state changes.
373 *
374 * @param pThis The instance.
375 *
376 * @remarks Owns the lock for the out-bound trunk port.
377 */
378DECLHIDDEN(void) vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive);
379
380/**
381 * This is called when a network interface has obtained a new MAC address.
382 *
383 * @param pThis The instance.
384 * @param pvIfData Pointer to the private interface data.
385 * @param pMac Pointer to the new MAC address.
386 */
387DECLHIDDEN(void) vboxNetFltPortOsNotifyMacAddress(PVBOXNETFLTINS pThis, void *pvIfData, PCRTMAC pMac);
388
389/**
390 * This is called when an interface is connected to the network.
391 *
392 * @return IPRT status code.
393 * @param pThis The instance.
394 * @param pvIf Pointer to the interface.
395 * @param ppvIfData Where to store the private interface data.
396 */
397DECLHIDDEN(int) vboxNetFltPortOsConnectInterface(PVBOXNETFLTINS pThis, void *pvIf, void **ppvIfData);
398
399/**
400 * This is called when a VM host disconnects from the network.
401 *
402 * @param pThis The instance.
403 * @param pvIfData Pointer to the private interface data.
404 */
405DECLHIDDEN(int) vboxNetFltPortOsDisconnectInterface(PVBOXNETFLTINS pThis, void *pvIfData);
406
407/**
408 * This is called to when disconnecting from a network.
409 *
410 * @return IPRT status code.
411 * @param pThis The new instance.
412 *
413 * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
414 */
415DECLHIDDEN(int) vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis);
416
417/**
418 * This is called to when connecting to a network.
419 *
420 * @return IPRT status code.
421 * @param pThis The new instance.
422 *
423 * @remarks Owns the semaphores for the global list, the network lock and the out-bound trunk port.
424 */
425DECLHIDDEN(int) vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis);
426
427/**
428 * Counter part to vboxNetFltOsInitInstance().
429 *
430 * @return IPRT status code.
431 * @param pThis The new instance.
432 *
433 * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
434 */
435DECLHIDDEN(void) vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis);
436
437/**
438 * This is called to attach to the actual host interface
439 * after linking the instance into the list.
440 *
441 * The MAC address as well promiscuousness and GSO capabilities should be
442 * reported by this function.
443 *
444 * @return IPRT status code.
445 * @param pThis The new instance.
446 * @param pvContext The user supplied context in the static config only.
447 * NULL in the dynamic config.
448 *
449 * @remarks Owns no locks.
450 */
451DECLHIDDEN(int) vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis, void *pvContext);
452
453/**
454 * This is called to perform structure initializations.
455 *
456 * @return IPRT status code.
457 * @param pThis The new instance.
458 *
459 * @remarks Owns no locks.
460 */
461DECLHIDDEN(int) vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis);
462/** @} */
463
464
465RT_C_DECLS_END
466
467#endif
468
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