VirtualBox

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

Last change on this file since 51073 was 46904, checked in by vboxsync, 12 years ago

IntNet, VirtioNet, NetFilter: Large frame support + drop oversized + UFO fix (#6821)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 16.9 KB
Line 
1/* $Id: VBoxNetFltInternal.h 46904 2013-07-02 12:59:56Z 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(void) vboxNetFltRetain(PVBOXNETFLTINS pThis, bool fBusy);
333DECLHIDDEN(bool) vboxNetFltTryRetainBusyActive(PVBOXNETFLTINS pThis);
334DECLHIDDEN(bool) vboxNetFltTryRetainBusyNotDisconnected(PVBOXNETFLTINS pThis);
335DECLHIDDEN(void) vboxNetFltRelease(PVBOXNETFLTINS pThis, bool fBusy);
336
337#ifdef VBOXNETFLT_STATIC_CONFIG
338DECLHIDDEN(int) vboxNetFltSearchCreateInstance(PVBOXNETFLTGLOBALS pGlobals, const char *pszName, PVBOXNETFLTINS *ppInstance, void * pContext);
339#endif
340
341
342
343/** @name The OS specific interface.
344 * @{ */
345/**
346 * Try rediscover the host interface.
347 *
348 * This is called periodically from the transmit path if we're marked as
349 * disconnected from the host. There is no chance of a race here.
350 *
351 * @returns true if the interface was successfully rediscovered and reattach,
352 * otherwise false.
353 * @param pThis The new instance.
354 */
355DECLHIDDEN(bool) vboxNetFltOsMaybeRediscovered(PVBOXNETFLTINS pThis);
356
357/**
358 * Transmits a frame.
359 *
360 * @return IPRT status code.
361 * @param pThis The new instance.
362 * @param pvIfData Pointer to the host-private interface data.
363 * @param pSG The (scatter/)gather list.
364 * @param fDst The destination mask. At least one bit will be set.
365 *
366 * @remarks Owns the out-bound trunk port semaphore.
367 */
368DECLHIDDEN(int) vboxNetFltPortOsXmit(PVBOXNETFLTINS pThis, void *pvIfData, PINTNETSG pSG, uint32_t fDst);
369
370/**
371 * This is called when activating or suspending the instance.
372 *
373 * Use this method to enable and disable promiscuous mode on
374 * the interface to prevent unnecessary interrupt load.
375 *
376 * It is only called when the state changes.
377 *
378 * @param pThis The instance.
379 *
380 * @remarks Owns the lock for the out-bound trunk port.
381 */
382DECLHIDDEN(void) vboxNetFltPortOsSetActive(PVBOXNETFLTINS pThis, bool fActive);
383
384/**
385 * This is called when a network interface has obtained a new MAC address.
386 *
387 * @param pThis The instance.
388 * @param pvIfData Pointer to the private interface data.
389 * @param pMac Pointer to the new MAC address.
390 */
391DECLHIDDEN(void) vboxNetFltPortOsNotifyMacAddress(PVBOXNETFLTINS pThis, void *pvIfData, PCRTMAC pMac);
392
393/**
394 * This is called when an interface is connected to the network.
395 *
396 * @return IPRT status code.
397 * @param pThis The instance.
398 * @param pvIf Pointer to the interface.
399 * @param ppvIfData Where to store the private interface data.
400 */
401DECLHIDDEN(int) vboxNetFltPortOsConnectInterface(PVBOXNETFLTINS pThis, void *pvIf, void **ppvIfData);
402
403/**
404 * This is called when a VM host disconnects from the network.
405 *
406 * @param pThis The instance.
407 * @param pvIfData Pointer to the private interface data.
408 */
409DECLHIDDEN(int) vboxNetFltPortOsDisconnectInterface(PVBOXNETFLTINS pThis, void *pvIfData);
410
411/**
412 * This is called to when disconnecting from a network.
413 *
414 * @return IPRT status code.
415 * @param pThis The new instance.
416 *
417 * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
418 */
419DECLHIDDEN(int) vboxNetFltOsDisconnectIt(PVBOXNETFLTINS pThis);
420
421/**
422 * This is called to when connecting to a network.
423 *
424 * @return IPRT status code.
425 * @param pThis The new instance.
426 *
427 * @remarks Owns the semaphores for the global list, the network lock and the out-bound trunk port.
428 */
429DECLHIDDEN(int) vboxNetFltOsConnectIt(PVBOXNETFLTINS pThis);
430
431/**
432 * Counter part to vboxNetFltOsInitInstance().
433 *
434 * @return IPRT status code.
435 * @param pThis The new instance.
436 *
437 * @remarks May own the semaphores for the global list, the network lock and the out-bound trunk port.
438 */
439DECLHIDDEN(void) vboxNetFltOsDeleteInstance(PVBOXNETFLTINS pThis);
440
441/**
442 * This is called to attach to the actual host interface
443 * after linking the instance into the list.
444 *
445 * The MAC address as well promiscuousness and GSO capabilities should be
446 * reported by this function.
447 *
448 * @return IPRT status code.
449 * @param pThis The new instance.
450 * @param pvContext The user supplied context in the static config only.
451 * NULL in the dynamic config.
452 *
453 * @remarks Owns no locks.
454 */
455DECLHIDDEN(int) vboxNetFltOsInitInstance(PVBOXNETFLTINS pThis, void *pvContext);
456
457/**
458 * This is called to perform structure initializations.
459 *
460 * @return IPRT status code.
461 * @param pThis The new instance.
462 *
463 * @remarks Owns no locks.
464 */
465DECLHIDDEN(int) vboxNetFltOsPreInitInstance(PVBOXNETFLTINS pThis);
466/** @} */
467
468
469RT_C_DECLS_END
470
471#endif
472
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