VirtualBox

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

Last change on this file since 54558 was 52394, checked in by vboxsync, 10 years ago

intnetR0TrunkIfPortDisconnect: Must always release the busy reference to the interface side (netflt) and must not trust pThis->pIfPort to be working as it's cleared before doing the busy waiting in intnetR0TrunkIfDestroy. So, pfnDisconnect has been extended with a pIfPort pointer as well as a function pointer to a busy-release function for pIfPort.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 17.0 KB
Line 
1/* $Id: VBoxNetFltInternal.h 52394 2014-08-15 22:25:31Z vboxsync $ */
2/** @file
3 * VBoxNetFlt - Network Filter Driver (Host), Internal Header.
4 */
5
6/*
7 * Copyright (C) 2008-2012 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 /** MTU of host's interface. */
163 uint32_t cbMtu;
164 /** Whether we've successfully put the interface into to promiscuous mode.
165 * This is for dealing with the ENETDOWN case. */
166 bool volatile fPromiscuousSet;
167 /** Whether device exists and physically attached. */
168 bool volatile fRegistered;
169 /** Whether our packet handler is installed. */
170 bool volatile fPacketHandler;
171 /** The MAC address of the interface. */
172 RTMAC MacAddr;
173 struct notifier_block Notifier;
174 struct packet_type PacketType;
175# ifndef VBOXNETFLT_LINUX_NO_XMIT_QUEUE
176 struct sk_buff_head XmitQueue;
177 struct work_struct XmitTask;
178# endif
179 /** @} */
180# elif defined(RT_OS_SOLARIS)
181 /** @name Solaris instance data.
182 * @{ */
183# ifdef VBOX_WITH_NETFLT_CROSSBOW
184 /** Whether the underlying interface is a VNIC or not. */
185 bool fIsVNIC;
186 /** Whether the underlying interface is a VNIC template or not. */
187 bool fIsVNICTemplate;
188 /** Handle to list of created VNICs. */
189 list_t hVNICs;
190 /** The MAC address of the host interface. */
191 RTMAC MacAddr;
192 /** Handle of this interface (lower MAC). */
193 mac_handle_t hInterface;
194 /** Handle to link state notifier. */
195 mac_notify_handle_t hNotify;
196# else
197 /** Pointer to the bound IPv4 stream. */
198 struct vboxnetflt_stream_t * volatile pIp4Stream;
199 /** Pointer to the bound IPv6 stream. */
200 struct vboxnetflt_stream_t * volatile pIp6Stream;
201 /** Pointer to the bound ARP stream. */
202 struct vboxnetflt_stream_t * volatile pArpStream;
203 /** Pointer to the unbound promiscuous stream. */
204 struct vboxnetflt_promisc_stream_t * volatile pPromiscStream;
205 /** Whether we are attaching to IPv6 stream dynamically now. */
206 bool volatile fAttaching;
207 /** Whether this is a VLAN interface or not. */
208 bool volatile fVLAN;
209 /** Layered device handle to the interface. */
210 ldi_handle_t hIface;
211 /** The MAC address of the interface. */
212 RTMAC MacAddr;
213 /** Mutex protection used for loopback. */
214 kmutex_t hMtx;
215 /** Mutex protection used for dynamic IPv6 attaches. */
216 RTSEMFASTMUTEX hPollMtx;
217# endif
218 /** @} */
219# elif defined(RT_OS_FREEBSD)
220 /** @name FreeBSD instance data.
221 * @{ */
222 /** Interface handle */
223 struct ifnet *ifp;
224 /** Netgraph node handle */
225 node_p node;
226 /** Input hook */
227 hook_p input;
228 /** Output hook */
229 hook_p output;
230 /** Original interface flags */
231 unsigned int flags;
232 /** Input queue */
233 struct ifqueue inq;
234 /** Output queue */
235 struct ifqueue outq;
236 /** Input task */
237 struct task tskin;
238 /** Output task */
239 struct task tskout;
240 /** The MAC address of the interface. */
241 RTMAC MacAddr;
242 /** @} */
243# elif defined(RT_OS_WINDOWS)
244 /** @name Windows instance data.
245 * @{ */
246 /** Filter driver device context. */
247 VBOXNETFLTWIN WinIf;
248
249 volatile uint32_t cModeNetFltRefs;
250 volatile uint32_t cModePassThruRefs;
251#ifndef VBOXNETFLT_NO_PACKET_QUEUE
252 /** Packet worker thread info */
253 PACKET_QUEUE_WORKER PacketQueueWorker;
254#endif
255 /** The MAC address of the interface. Caching MAC for performance reasons. */
256 RTMAC MacAddr;
257 /** mutex used to synchronize WinIf init/deinit */
258 RTSEMMUTEX hWinIfMutex;
259 /** @} */
260# else
261# error "PORTME"
262# endif
263 } s;
264#endif
265 /** Padding. */
266#if defined(RT_OS_WINDOWS)
267# if defined(VBOX_NETFLT_ONDEMAND_BIND)
268 uint8_t abPadding[192];
269# elif defined(VBOXNETADP)
270 uint8_t abPadding[256];
271# else
272 uint8_t abPadding[1024];
273# endif
274#elif defined(RT_OS_LINUX)
275 uint8_t abPadding[320];
276#elif defined(RT_OS_FREEBSD)
277 uint8_t abPadding[320];
278#else
279 uint8_t abPadding[128];
280#endif
281 } u;
282
283 /** The interface name. */
284 char szName[1];
285} VBOXNETFLTINS;
286/** Pointer to the instance data of a host network filter driver. */
287typedef struct VBOXNETFLTINS *PVBOXNETFLTINS;
288
289AssertCompileMemberAlignment(VBOXNETFLTINS, NanoTSLastRediscovery, 8);
290#ifdef VBOXNETFLT_OS_SPECFIC
291AssertCompile(RT_SIZEOFMEMB(VBOXNETFLTINS, u.s) <= RT_SIZEOFMEMB(VBOXNETFLTINS, u.abPadding));
292#endif
293
294
295/**
296 * The global data of the VBox filter driver.
297 *
298 * This contains the bit required for communicating with support driver, VBoxDrv
299 * (start out as SupDrv).
300 */
301typedef struct VBOXNETFLTGLOBALS
302{
303 /** Mutex protecting the list of instances and state changes. */
304 RTSEMFASTMUTEX hFastMtx;
305 /** Pointer to a list of instance data. */
306 PVBOXNETFLTINS pInstanceHead;
307
308 /** The INTNET trunk network interface factory. */
309 INTNETTRUNKFACTORY TrunkFactory;
310 /** The SUPDRV component factory registration. */
311 SUPDRVFACTORY SupDrvFactory;
312 /** The number of current factory references. */
313 int32_t volatile cFactoryRefs;
314 /** Whether the IDC connection is open or not.
315 * This is only for cleaning up correctly after the separate IDC init on Windows. */
316 bool fIDCOpen;
317 /** The SUPDRV IDC handle (opaque struct). */
318 SUPDRVIDCHANDLE SupDrvIDC;
319} VBOXNETFLTGLOBALS;
320
321
322DECLHIDDEN(int) vboxNetFltInitGlobalsAndIdc(PVBOXNETFLTGLOBALS pGlobals);
323DECLHIDDEN(int) vboxNetFltInitGlobals(PVBOXNETFLTGLOBALS pGlobals);
324DECLHIDDEN(int) vboxNetFltInitIdc(PVBOXNETFLTGLOBALS pGlobals);
325DECLHIDDEN(int) vboxNetFltTryDeleteIdcAndGlobals(PVBOXNETFLTGLOBALS pGlobals);
326DECLHIDDEN(void) vboxNetFltDeleteGlobals(PVBOXNETFLTGLOBALS pGlobals);
327DECLHIDDEN(int) vboxNetFltTryDeleteIdc(PVBOXNETFLTGLOBALS pGlobals);
328
329DECLHIDDEN(bool) vboxNetFltCanUnload(PVBOXNETFLTGLOBALS pGlobals);
330DECLHIDDEN(PVBOXNETFLTINS) vboxNetFltFindInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName);
331
332DECLHIDDEN(DECLCALLBACK(void)) vboxNetFltPortReleaseBusy(PINTNETTRUNKIFPORT pIfPort);
333DECLHIDDEN(void) vboxNetFltRetain(PVBOXNETFLTINS pThis, bool fBusy);
334DECLHIDDEN(bool) vboxNetFltTryRetainBusyActive(PVBOXNETFLTINS pThis);
335DECLHIDDEN(bool) vboxNetFltTryRetainBusyNotDisconnected(PVBOXNETFLTINS pThis);
336DECLHIDDEN(void) vboxNetFltRelease(PVBOXNETFLTINS pThis, bool fBusy);
337
338#ifdef VBOXNETFLT_STATIC_CONFIG
339DECLHIDDEN(int) vboxNetFltSearchCreateInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName, PVBOXNETFLTINS *ppInstance, void * pContext);
340#endif
341
342
343
344/** @name The OS specific interface.
345 * @{ */
346/**
347 * Try rediscover the host interface.
348 *
349 * This is called periodically from the transmit path if we're marked as
350 * disconnected from the host. There is no chance of a race here.
351 *
352 * @returns true if the interface was successfully rediscovered and reattach,
353 * otherwise false.
354 * @param pThis The new instance.
355 */
356DECLHIDDEN(bool) vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis);
357
358/**
359 * Transmits a frame.
360 *
361 * @return IPRT status code.
362 * @param pThis The new instance.
363 * @param pvIfData Pointer to the host-private interface data.
364 * @param pSG The (scatter/)gather list.
365 * @param fDst The destination mask. At least one bit will be set.
366 *
367 * @remarks Owns the out-bound trunk port semaphore.
368 */
369DECLHIDDEN(int) vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, void *pvIfData, PINTNETSG pSG, uint32_t fDst);
370
371/**
372 * This is called when activating or suspending the instance.
373 *
374 * Use this method to enable and disable promiscuous mode on
375 * the interface to prevent unnecessary interrupt load.
376 *
377 * It is only called when the state changes.
378 *
379 * @param pThis The instance.
380 *
381 * @remarks Owns the lock for the out-bound trunk port.
382 */
383DECLHIDDEN(void) vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive);
384
385/**
386 * This is called when a network interface has obtained a new MAC address.
387 *
388 * @param pThis The instance.
389 * @param pvIfData Pointer to the private interface data.
390 * @param pMac Pointer to the new MAC address.
391 */
392DECLHIDDEN(void) vboxNetFltPortOsNotifyMacAddress(PVBOXNETFLTINS pThis, void *pvIfData, PCRTMAC pMac);
393
394/**
395 * This is called when an interface is connected to the network.
396 *
397 * @return IPRT status code.
398 * @param pThis The instance.
399 * @param pvIf Pointer to the interface.
400 * @param ppvIfData Where to store the private interface data.
401 */
402DECLHIDDEN(int) vboxNetFltPortOsConnectInterface(PVBOXNETFLTINS pThis, void *pvIf, void **ppvIfData);
403
404/**
405 * This is called when a VM host disconnects from the network.
406 *
407 * @param pThis The instance.
408 * @param pvIfData Pointer to the private interface data.
409 */
410DECLHIDDEN(int) vboxNetFltPortOsDisconnectInterface(PVBOXNETFLTINS pThis, void *pvIfData);
411
412/**
413 * This is called to when disconnecting from a network.
414 *
415 * @return IPRT status code.
416 * @param pThis The new instance.
417 *
418 * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
419 */
420DECLHIDDEN(int) vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis);
421
422/**
423 * This is called to when connecting to a network.
424 *
425 * @return IPRT status code.
426 * @param pThis The new instance.
427 *
428 * @remarks Owns the semaphores for the global list, the network lock and the out-bound trunk port.
429 */
430DECLHIDDEN(int) vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis);
431
432/**
433 * Counter part to vboxNetFltOsInitInstance().
434 *
435 * @return IPRT status code.
436 * @param pThis The new instance.
437 *
438 * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
439 */
440DECLHIDDEN(void) vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis);
441
442/**
443 * This is called to attach to the actual host interface
444 * after linking the instance into the list.
445 *
446 * The MAC address as well promiscuousness and GSO capabilities should be
447 * reported by this function.
448 *
449 * @return IPRT status code.
450 * @param pThis The new instance.
451 * @param pvContext The user supplied context in the static config only.
452 * NULL in the dynamic config.
453 *
454 * @remarks Owns no locks.
455 */
456DECLHIDDEN(int) vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis, void *pvContext);
457
458/**
459 * This is called to perform structure initializations.
460 *
461 * @return IPRT status code.
462 * @param pThis The new instance.
463 *
464 * @remarks Owns no locks.
465 */
466DECLHIDDEN(int) vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis);
467/** @} */
468
469
470RT_C_DECLS_END
471
472#endif
473
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