1 | /* $Id: DrvTAP.cpp 80074 2019-07-31 14:18:34Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DrvTAP - Universal TAP network transport driver.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2019 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 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_DRV_TUN
|
---|
23 | #include <VBox/log.h>
|
---|
24 | #include <VBox/vmm/pdmdrv.h>
|
---|
25 | #include <VBox/vmm/pdmnetifs.h>
|
---|
26 | #include <VBox/vmm/pdmnetinline.h>
|
---|
27 |
|
---|
28 | #include <iprt/asm.h>
|
---|
29 | #include <iprt/assert.h>
|
---|
30 | #include <iprt/ctype.h>
|
---|
31 | #include <iprt/file.h>
|
---|
32 | #include <iprt/mem.h>
|
---|
33 | #include <iprt/path.h>
|
---|
34 | #include <iprt/pipe.h>
|
---|
35 | #include <iprt/semaphore.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 | #include <iprt/thread.h>
|
---|
38 | #include <iprt/uuid.h>
|
---|
39 | #ifdef RT_OS_SOLARIS
|
---|
40 | # include <iprt/process.h>
|
---|
41 | # include <iprt/env.h>
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | #include <sys/ioctl.h>
|
---|
45 | #include <sys/poll.h>
|
---|
46 | #ifdef RT_OS_SOLARIS
|
---|
47 | # include <sys/stat.h>
|
---|
48 | # include <sys/ethernet.h>
|
---|
49 | # include <sys/sockio.h>
|
---|
50 | # include <netinet/in.h>
|
---|
51 | # include <netinet/in_systm.h>
|
---|
52 | # include <netinet/ip.h>
|
---|
53 | # include <netinet/ip_icmp.h>
|
---|
54 | # include <netinet/udp.h>
|
---|
55 | # include <netinet/tcp.h>
|
---|
56 | # include <net/if.h>
|
---|
57 | # include <stropts.h>
|
---|
58 | # include <fcntl.h>
|
---|
59 | # include <stdlib.h>
|
---|
60 | # include <stdio.h>
|
---|
61 | #else
|
---|
62 | # include <sys/fcntl.h>
|
---|
63 | #endif
|
---|
64 | #include <errno.h>
|
---|
65 | #include <unistd.h>
|
---|
66 |
|
---|
67 | #include "VBoxDD.h"
|
---|
68 |
|
---|
69 |
|
---|
70 | /*********************************************************************************************************************************
|
---|
71 | * Structures and Typedefs *
|
---|
72 | *********************************************************************************************************************************/
|
---|
73 | /**
|
---|
74 | * TAP driver instance data.
|
---|
75 | *
|
---|
76 | * @implements PDMINETWORKUP
|
---|
77 | */
|
---|
78 | typedef struct DRVTAP
|
---|
79 | {
|
---|
80 | /** The network interface. */
|
---|
81 | PDMINETWORKUP INetworkUp;
|
---|
82 | /** The network interface. */
|
---|
83 | PPDMINETWORKDOWN pIAboveNet;
|
---|
84 | /** Pointer to the driver instance. */
|
---|
85 | PPDMDRVINS pDrvIns;
|
---|
86 | /** TAP device file handle. */
|
---|
87 | RTFILE hFileDevice;
|
---|
88 | /** The configured TAP device name. */
|
---|
89 | char *pszDeviceName;
|
---|
90 | #ifdef RT_OS_SOLARIS
|
---|
91 | /** IP device file handle (/dev/udp). */
|
---|
92 | int iIPFileDes;
|
---|
93 | /** Whether device name is obtained from setup application. */
|
---|
94 | bool fStatic;
|
---|
95 | #endif
|
---|
96 | /** TAP setup application. */
|
---|
97 | char *pszSetupApplication;
|
---|
98 | /** TAP terminate application. */
|
---|
99 | char *pszTerminateApplication;
|
---|
100 | /** The write end of the control pipe. */
|
---|
101 | RTPIPE hPipeWrite;
|
---|
102 | /** The read end of the control pipe. */
|
---|
103 | RTPIPE hPipeRead;
|
---|
104 | /** Reader thread. */
|
---|
105 | PPDMTHREAD pThread;
|
---|
106 |
|
---|
107 | /** @todo The transmit thread. */
|
---|
108 | /** Transmit lock used by drvTAPNetworkUp_BeginXmit. */
|
---|
109 | RTCRITSECT XmitLock;
|
---|
110 |
|
---|
111 | #ifdef VBOX_WITH_STATISTICS
|
---|
112 | /** Number of sent packets. */
|
---|
113 | STAMCOUNTER StatPktSent;
|
---|
114 | /** Number of sent bytes. */
|
---|
115 | STAMCOUNTER StatPktSentBytes;
|
---|
116 | /** Number of received packets. */
|
---|
117 | STAMCOUNTER StatPktRecv;
|
---|
118 | /** Number of received bytes. */
|
---|
119 | STAMCOUNTER StatPktRecvBytes;
|
---|
120 | /** Profiling packet transmit runs. */
|
---|
121 | STAMPROFILE StatTransmit;
|
---|
122 | /** Profiling packet receive runs. */
|
---|
123 | STAMPROFILEADV StatReceive;
|
---|
124 | #endif /* VBOX_WITH_STATISTICS */
|
---|
125 |
|
---|
126 | #ifdef LOG_ENABLED
|
---|
127 | /** The nano ts of the last transfer. */
|
---|
128 | uint64_t u64LastTransferTS;
|
---|
129 | /** The nano ts of the last receive. */
|
---|
130 | uint64_t u64LastReceiveTS;
|
---|
131 | #endif
|
---|
132 | } DRVTAP, *PDRVTAP;
|
---|
133 |
|
---|
134 |
|
---|
135 | /** Converts a pointer to TAP::INetworkUp to a PRDVTAP. */
|
---|
136 | #define PDMINETWORKUP_2_DRVTAP(pInterface) ( (PDRVTAP)((uintptr_t)pInterface - RT_UOFFSETOF(DRVTAP, INetworkUp)) )
|
---|
137 |
|
---|
138 |
|
---|
139 | /*********************************************************************************************************************************
|
---|
140 | * Internal Functions *
|
---|
141 | *********************************************************************************************************************************/
|
---|
142 | #ifdef RT_OS_SOLARIS
|
---|
143 | static int SolarisTAPAttach(PDRVTAP pThis);
|
---|
144 | #endif
|
---|
145 |
|
---|
146 |
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * @interface_method_impl{PDMINETWORKUP,pfnBeginXmit}
|
---|
150 | */
|
---|
151 | static DECLCALLBACK(int) drvTAPNetworkUp_BeginXmit(PPDMINETWORKUP pInterface, bool fOnWorkerThread)
|
---|
152 | {
|
---|
153 | RT_NOREF(fOnWorkerThread);
|
---|
154 | PDRVTAP pThis = PDMINETWORKUP_2_DRVTAP(pInterface);
|
---|
155 | int rc = RTCritSectTryEnter(&pThis->XmitLock);
|
---|
156 | if (RT_FAILURE(rc))
|
---|
157 | {
|
---|
158 | /** @todo XMIT thread */
|
---|
159 | rc = VERR_TRY_AGAIN;
|
---|
160 | }
|
---|
161 | return rc;
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * @interface_method_impl{PDMINETWORKUP,pfnAllocBuf}
|
---|
167 | */
|
---|
168 | static DECLCALLBACK(int) drvTAPNetworkUp_AllocBuf(PPDMINETWORKUP pInterface, size_t cbMin,
|
---|
169 | PCPDMNETWORKGSO pGso, PPPDMSCATTERGATHER ppSgBuf)
|
---|
170 | {
|
---|
171 | RT_NOREF(pInterface);
|
---|
172 | #ifdef VBOX_STRICT
|
---|
173 | PDRVTAP pThis = PDMINETWORKUP_2_DRVTAP(pInterface);
|
---|
174 | Assert(RTCritSectIsOwner(&pThis->XmitLock));
|
---|
175 | #endif
|
---|
176 |
|
---|
177 | /*
|
---|
178 | * Allocate a scatter / gather buffer descriptor that is immediately
|
---|
179 | * followed by the buffer space of its single segment. The GSO context
|
---|
180 | * comes after that again.
|
---|
181 | */
|
---|
182 | PPDMSCATTERGATHER pSgBuf = (PPDMSCATTERGATHER)RTMemAlloc( RT_ALIGN_Z(sizeof(*pSgBuf), 16)
|
---|
183 | + RT_ALIGN_Z(cbMin, 16)
|
---|
184 | + (pGso ? RT_ALIGN_Z(sizeof(*pGso), 16) : 0));
|
---|
185 | if (!pSgBuf)
|
---|
186 | return VERR_NO_MEMORY;
|
---|
187 |
|
---|
188 | /*
|
---|
189 | * Initialize the S/G buffer and return.
|
---|
190 | */
|
---|
191 | pSgBuf->fFlags = PDMSCATTERGATHER_FLAGS_MAGIC | PDMSCATTERGATHER_FLAGS_OWNER_1;
|
---|
192 | pSgBuf->cbUsed = 0;
|
---|
193 | pSgBuf->cbAvailable = RT_ALIGN_Z(cbMin, 16);
|
---|
194 | pSgBuf->pvAllocator = NULL;
|
---|
195 | if (!pGso)
|
---|
196 | pSgBuf->pvUser = NULL;
|
---|
197 | else
|
---|
198 | {
|
---|
199 | pSgBuf->pvUser = (uint8_t *)(pSgBuf + 1) + pSgBuf->cbAvailable;
|
---|
200 | *(PPDMNETWORKGSO)pSgBuf->pvUser = *pGso;
|
---|
201 | }
|
---|
202 | pSgBuf->cSegs = 1;
|
---|
203 | pSgBuf->aSegs[0].cbSeg = pSgBuf->cbAvailable;
|
---|
204 | pSgBuf->aSegs[0].pvSeg = pSgBuf + 1;
|
---|
205 |
|
---|
206 | #if 0 /* poison */
|
---|
207 | memset(pSgBuf->aSegs[0].pvSeg, 'F', pSgBuf->aSegs[0].cbSeg);
|
---|
208 | #endif
|
---|
209 | *ppSgBuf = pSgBuf;
|
---|
210 | return VINF_SUCCESS;
|
---|
211 | }
|
---|
212 |
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * @interface_method_impl{PDMINETWORKUP,pfnFreeBuf}
|
---|
216 | */
|
---|
217 | static DECLCALLBACK(int) drvTAPNetworkUp_FreeBuf(PPDMINETWORKUP pInterface, PPDMSCATTERGATHER pSgBuf)
|
---|
218 | {
|
---|
219 | RT_NOREF(pInterface);
|
---|
220 | #ifdef VBOX_STRICT
|
---|
221 | PDRVTAP pThis = PDMINETWORKUP_2_DRVTAP(pInterface);
|
---|
222 | Assert(RTCritSectIsOwner(&pThis->XmitLock));
|
---|
223 | #endif
|
---|
224 |
|
---|
225 | if (pSgBuf)
|
---|
226 | {
|
---|
227 | Assert((pSgBuf->fFlags & PDMSCATTERGATHER_FLAGS_MAGIC_MASK) == PDMSCATTERGATHER_FLAGS_MAGIC);
|
---|
228 | pSgBuf->fFlags = 0;
|
---|
229 | RTMemFree(pSgBuf);
|
---|
230 | }
|
---|
231 | return VINF_SUCCESS;
|
---|
232 | }
|
---|
233 |
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * @interface_method_impl{PDMINETWORKUP,pfnSendBuf}
|
---|
237 | */
|
---|
238 | static DECLCALLBACK(int) drvTAPNetworkUp_SendBuf(PPDMINETWORKUP pInterface, PPDMSCATTERGATHER pSgBuf, bool fOnWorkerThread)
|
---|
239 | {
|
---|
240 | RT_NOREF(fOnWorkerThread);
|
---|
241 | PDRVTAP pThis = PDMINETWORKUP_2_DRVTAP(pInterface);
|
---|
242 | STAM_COUNTER_INC(&pThis->StatPktSent);
|
---|
243 | STAM_COUNTER_ADD(&pThis->StatPktSentBytes, pSgBuf->cbUsed);
|
---|
244 | STAM_PROFILE_START(&pThis->StatTransmit, a);
|
---|
245 |
|
---|
246 | AssertPtr(pSgBuf);
|
---|
247 | Assert((pSgBuf->fFlags & PDMSCATTERGATHER_FLAGS_MAGIC_MASK) == PDMSCATTERGATHER_FLAGS_MAGIC);
|
---|
248 | Assert(RTCritSectIsOwner(&pThis->XmitLock));
|
---|
249 |
|
---|
250 | int rc;
|
---|
251 | if (!pSgBuf->pvUser)
|
---|
252 | {
|
---|
253 | #ifdef LOG_ENABLED
|
---|
254 | uint64_t u64Now = RTTimeProgramNanoTS();
|
---|
255 | LogFlow(("drvTAPSend: %-4d bytes at %llu ns deltas: r=%llu t=%llu\n",
|
---|
256 | pSgBuf->cbUsed, u64Now, u64Now - pThis->u64LastReceiveTS, u64Now - pThis->u64LastTransferTS));
|
---|
257 | pThis->u64LastTransferTS = u64Now;
|
---|
258 | #endif
|
---|
259 | Log2(("drvTAPSend: pSgBuf->aSegs[0].pvSeg=%p pSgBuf->cbUsed=%#x\n"
|
---|
260 | "%.*Rhxd\n",
|
---|
261 | pSgBuf->aSegs[0].pvSeg, pSgBuf->cbUsed, pSgBuf->cbUsed, pSgBuf->aSegs[0].pvSeg));
|
---|
262 |
|
---|
263 | rc = RTFileWrite(pThis->hFileDevice, pSgBuf->aSegs[0].pvSeg, pSgBuf->cbUsed, NULL);
|
---|
264 | }
|
---|
265 | else
|
---|
266 | {
|
---|
267 | uint8_t abHdrScratch[256];
|
---|
268 | uint8_t const *pbFrame = (uint8_t const *)pSgBuf->aSegs[0].pvSeg;
|
---|
269 | PCPDMNETWORKGSO pGso = (PCPDMNETWORKGSO)pSgBuf->pvUser;
|
---|
270 | uint32_t const cSegs = PDMNetGsoCalcSegmentCount(pGso, pSgBuf->cbUsed); Assert(cSegs > 1);
|
---|
271 | rc = VINF_SUCCESS;
|
---|
272 | for (size_t iSeg = 0; iSeg < cSegs; iSeg++)
|
---|
273 | {
|
---|
274 | uint32_t cbSegFrame;
|
---|
275 | void *pvSegFrame = PDMNetGsoCarveSegmentQD(pGso, (uint8_t *)pbFrame, pSgBuf->cbUsed, abHdrScratch,
|
---|
276 | iSeg, cSegs, &cbSegFrame);
|
---|
277 | rc = RTFileWrite(pThis->hFileDevice, pvSegFrame, cbSegFrame, NULL);
|
---|
278 | if (RT_FAILURE(rc))
|
---|
279 | break;
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 | pSgBuf->fFlags = 0;
|
---|
284 | RTMemFree(pSgBuf);
|
---|
285 |
|
---|
286 | STAM_PROFILE_STOP(&pThis->StatTransmit, a);
|
---|
287 | AssertRC(rc);
|
---|
288 | if (RT_FAILURE(rc))
|
---|
289 | rc = rc == VERR_NO_MEMORY ? VERR_NET_NO_BUFFER_SPACE : VERR_NET_DOWN;
|
---|
290 | return rc;
|
---|
291 | }
|
---|
292 |
|
---|
293 |
|
---|
294 | /**
|
---|
295 | * @interface_method_impl{PDMINETWORKUP,pfnEndXmit}
|
---|
296 | */
|
---|
297 | static DECLCALLBACK(void) drvTAPNetworkUp_EndXmit(PPDMINETWORKUP pInterface)
|
---|
298 | {
|
---|
299 | PDRVTAP pThis = PDMINETWORKUP_2_DRVTAP(pInterface);
|
---|
300 | RTCritSectLeave(&pThis->XmitLock);
|
---|
301 | }
|
---|
302 |
|
---|
303 |
|
---|
304 | /**
|
---|
305 | * @interface_method_impl{PDMINETWORKUP,pfnSetPromiscuousMode}
|
---|
306 | */
|
---|
307 | static DECLCALLBACK(void) drvTAPNetworkUp_SetPromiscuousMode(PPDMINETWORKUP pInterface, bool fPromiscuous)
|
---|
308 | {
|
---|
309 | RT_NOREF(pInterface, fPromiscuous);
|
---|
310 | LogFlow(("drvTAPNetworkUp_SetPromiscuousMode: fPromiscuous=%d\n", fPromiscuous));
|
---|
311 | /* nothing to do */
|
---|
312 | }
|
---|
313 |
|
---|
314 |
|
---|
315 | /**
|
---|
316 | * Notification on link status changes.
|
---|
317 | *
|
---|
318 | * @param pInterface Pointer to the interface structure containing the called function pointer.
|
---|
319 | * @param enmLinkState The new link state.
|
---|
320 | * @thread EMT
|
---|
321 | */
|
---|
322 | static DECLCALLBACK(void) drvTAPNetworkUp_NotifyLinkChanged(PPDMINETWORKUP pInterface, PDMNETWORKLINKSTATE enmLinkState)
|
---|
323 | {
|
---|
324 | RT_NOREF(pInterface, enmLinkState);
|
---|
325 | LogFlow(("drvTAPNetworkUp_NotifyLinkChanged: enmLinkState=%d\n", enmLinkState));
|
---|
326 | /** @todo take action on link down and up. Stop the polling and such like. */
|
---|
327 | }
|
---|
328 |
|
---|
329 |
|
---|
330 | /**
|
---|
331 | * Asynchronous I/O thread for handling receive.
|
---|
332 | *
|
---|
333 | * @returns VINF_SUCCESS (ignored).
|
---|
334 | * @param Thread Thread handle.
|
---|
335 | * @param pvUser Pointer to a DRVTAP structure.
|
---|
336 | */
|
---|
337 | static DECLCALLBACK(int) drvTAPAsyncIoThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
|
---|
338 | {
|
---|
339 | PDRVTAP pThis = PDMINS_2_DATA(pDrvIns, PDRVTAP);
|
---|
340 | LogFlow(("drvTAPAsyncIoThread: pThis=%p\n", pThis));
|
---|
341 |
|
---|
342 | if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
|
---|
343 | return VINF_SUCCESS;
|
---|
344 |
|
---|
345 | STAM_PROFILE_ADV_START(&pThis->StatReceive, a);
|
---|
346 |
|
---|
347 | /*
|
---|
348 | * Polling loop.
|
---|
349 | */
|
---|
350 | while (pThread->enmState == PDMTHREADSTATE_RUNNING)
|
---|
351 | {
|
---|
352 | /*
|
---|
353 | * Wait for something to become available.
|
---|
354 | */
|
---|
355 | struct pollfd aFDs[2];
|
---|
356 | aFDs[0].fd = RTFileToNative(pThis->hFileDevice);
|
---|
357 | aFDs[0].events = POLLIN | POLLPRI;
|
---|
358 | aFDs[0].revents = 0;
|
---|
359 | aFDs[1].fd = RTPipeToNative(pThis->hPipeRead);
|
---|
360 | aFDs[1].events = POLLIN | POLLPRI | POLLERR | POLLHUP;
|
---|
361 | aFDs[1].revents = 0;
|
---|
362 | STAM_PROFILE_ADV_STOP(&pThis->StatReceive, a);
|
---|
363 | errno=0;
|
---|
364 | int rc = poll(&aFDs[0], RT_ELEMENTS(aFDs), -1 /* infinite */);
|
---|
365 |
|
---|
366 | /* this might have changed in the meantime */
|
---|
367 | if (pThread->enmState != PDMTHREADSTATE_RUNNING)
|
---|
368 | break;
|
---|
369 |
|
---|
370 | STAM_PROFILE_ADV_START(&pThis->StatReceive, a);
|
---|
371 | if ( rc > 0
|
---|
372 | && (aFDs[0].revents & (POLLIN | POLLPRI))
|
---|
373 | && !aFDs[1].revents)
|
---|
374 | {
|
---|
375 | /*
|
---|
376 | * Read the frame.
|
---|
377 | */
|
---|
378 | char achBuf[16384];
|
---|
379 | size_t cbRead = 0;
|
---|
380 | /** @note At least on Linux we will never receive more than one network packet
|
---|
381 | * after poll() returned successfully. I don't know why but a second
|
---|
382 | * RTFileRead() operation will return with VERR_TRY_AGAIN in any case. */
|
---|
383 | rc = RTFileRead(pThis->hFileDevice, achBuf, sizeof(achBuf), &cbRead);
|
---|
384 | if (RT_SUCCESS(rc))
|
---|
385 | {
|
---|
386 | /*
|
---|
387 | * Wait for the device to have space for this frame.
|
---|
388 | * Most guests use frame-sized receive buffers, hence non-zero cbMax
|
---|
389 | * automatically means there is enough room for entire frame. Some
|
---|
390 | * guests (eg. Solaris) use large chains of small receive buffers
|
---|
391 | * (each 128 or so bytes large). We will still start receiving as soon
|
---|
392 | * as cbMax is non-zero because:
|
---|
393 | * - it would be quite expensive for pfnCanReceive to accurately
|
---|
394 | * determine free receive buffer space
|
---|
395 | * - if we were waiting for enough free buffers, there is a risk
|
---|
396 | * of deadlocking because the guest could be waiting for a receive
|
---|
397 | * overflow error to allocate more receive buffers
|
---|
398 | */
|
---|
399 | STAM_PROFILE_ADV_STOP(&pThis->StatReceive, a);
|
---|
400 | int rc1 = pThis->pIAboveNet->pfnWaitReceiveAvail(pThis->pIAboveNet, RT_INDEFINITE_WAIT);
|
---|
401 | STAM_PROFILE_ADV_START(&pThis->StatReceive, a);
|
---|
402 |
|
---|
403 | /*
|
---|
404 | * A return code != VINF_SUCCESS means that we were woken up during a VM
|
---|
405 | * state transition. Drop the packet and wait for the next one.
|
---|
406 | */
|
---|
407 | if (RT_FAILURE(rc1))
|
---|
408 | continue;
|
---|
409 |
|
---|
410 | /*
|
---|
411 | * Pass the data up.
|
---|
412 | */
|
---|
413 | #ifdef LOG_ENABLED
|
---|
414 | uint64_t u64Now = RTTimeProgramNanoTS();
|
---|
415 | LogFlow(("drvTAPAsyncIoThread: %-4d bytes at %llu ns deltas: r=%llu t=%llu\n",
|
---|
416 | cbRead, u64Now, u64Now - pThis->u64LastReceiveTS, u64Now - pThis->u64LastTransferTS));
|
---|
417 | pThis->u64LastReceiveTS = u64Now;
|
---|
418 | #endif
|
---|
419 | Log2(("drvTAPAsyncIoThread: cbRead=%#x\n" "%.*Rhxd\n", cbRead, cbRead, achBuf));
|
---|
420 | STAM_COUNTER_INC(&pThis->StatPktRecv);
|
---|
421 | STAM_COUNTER_ADD(&pThis->StatPktRecvBytes, cbRead);
|
---|
422 | rc1 = pThis->pIAboveNet->pfnReceive(pThis->pIAboveNet, achBuf, cbRead);
|
---|
423 | AssertRC(rc1);
|
---|
424 | }
|
---|
425 | else
|
---|
426 | {
|
---|
427 | LogFlow(("drvTAPAsyncIoThread: RTFileRead -> %Rrc\n", rc));
|
---|
428 | if (rc == VERR_INVALID_HANDLE)
|
---|
429 | break;
|
---|
430 | RTThreadYield();
|
---|
431 | }
|
---|
432 | }
|
---|
433 | else if ( rc > 0
|
---|
434 | && aFDs[1].revents)
|
---|
435 | {
|
---|
436 | LogFlow(("drvTAPAsyncIoThread: Control message: enmState=%d revents=%#x\n", pThread->enmState, aFDs[1].revents));
|
---|
437 | if (aFDs[1].revents & (POLLHUP | POLLERR | POLLNVAL))
|
---|
438 | break;
|
---|
439 |
|
---|
440 | /* drain the pipe */
|
---|
441 | char ch;
|
---|
442 | size_t cbRead;
|
---|
443 | RTPipeRead(pThis->hPipeRead, &ch, 1, &cbRead);
|
---|
444 | }
|
---|
445 | else
|
---|
446 | {
|
---|
447 | /*
|
---|
448 | * poll() failed for some reason. Yield to avoid eating too much CPU.
|
---|
449 | *
|
---|
450 | * EINTR errors have been seen frequently. They should be harmless, even
|
---|
451 | * if they are not supposed to occur in our setup.
|
---|
452 | */
|
---|
453 | if (errno == EINTR)
|
---|
454 | Log(("rc=%d revents=%#x,%#x errno=%p %s\n", rc, aFDs[0].revents, aFDs[1].revents, errno, strerror(errno)));
|
---|
455 | else
|
---|
456 | AssertMsgFailed(("rc=%d revents=%#x,%#x errno=%p %s\n", rc, aFDs[0].revents, aFDs[1].revents, errno, strerror(errno)));
|
---|
457 | RTThreadYield();
|
---|
458 | }
|
---|
459 | }
|
---|
460 |
|
---|
461 |
|
---|
462 | LogFlow(("drvTAPAsyncIoThread: returns %Rrc\n", VINF_SUCCESS));
|
---|
463 | STAM_PROFILE_ADV_STOP(&pThis->StatReceive, a);
|
---|
464 | return VINF_SUCCESS;
|
---|
465 | }
|
---|
466 |
|
---|
467 |
|
---|
468 | /**
|
---|
469 | * Unblock the send thread so it can respond to a state change.
|
---|
470 | *
|
---|
471 | * @returns VBox status code.
|
---|
472 | * @param pDevIns The pcnet device instance.
|
---|
473 | * @param pThread The send thread.
|
---|
474 | */
|
---|
475 | static DECLCALLBACK(int) drvTapAsyncIoWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
|
---|
476 | {
|
---|
477 | RT_NOREF(pThread);
|
---|
478 | PDRVTAP pThis = PDMINS_2_DATA(pDrvIns, PDRVTAP);
|
---|
479 |
|
---|
480 | size_t cbIgnored;
|
---|
481 | int rc = RTPipeWrite(pThis->hPipeWrite, "", 1, &cbIgnored);
|
---|
482 | AssertRC(rc);
|
---|
483 |
|
---|
484 | return VINF_SUCCESS;
|
---|
485 | }
|
---|
486 |
|
---|
487 |
|
---|
488 | #if defined(RT_OS_SOLARIS)
|
---|
489 | /**
|
---|
490 | * Calls OS-specific TAP setup application/script.
|
---|
491 | *
|
---|
492 | * @returns VBox error code.
|
---|
493 | * @param pThis The instance data.
|
---|
494 | */
|
---|
495 | static int drvTAPSetupApplication(PDRVTAP pThis)
|
---|
496 | {
|
---|
497 | char szCommand[4096];
|
---|
498 |
|
---|
499 | RTStrPrintf(szCommand, sizeof(szCommand), "%s %s", pThis->pszSetupApplication,
|
---|
500 | pThis->fStatic ? pThis->pszDeviceName : "");
|
---|
501 |
|
---|
502 | /* Pipe open the setup application. */
|
---|
503 | Log2(("Starting TAP setup application: %s\n", szCommand));
|
---|
504 | FILE* pfSetupHandle = popen(szCommand, "r");
|
---|
505 | if (pfSetupHandle == 0)
|
---|
506 | {
|
---|
507 | LogRel(("TAP#%d: Failed to run TAP setup application: %s\n", pThis->pDrvIns->iInstance,
|
---|
508 | pThis->pszSetupApplication, strerror(errno)));
|
---|
509 | return VERR_HOSTIF_INIT_FAILED;
|
---|
510 | }
|
---|
511 | if (!pThis->fStatic)
|
---|
512 | {
|
---|
513 | /* Obtain device name from setup application. */
|
---|
514 | char acBuffer[64];
|
---|
515 | size_t cBufSize;
|
---|
516 | fgets(acBuffer, sizeof(acBuffer), pfSetupHandle);
|
---|
517 | cBufSize = strlen(acBuffer);
|
---|
518 | /* The script must return the name of the interface followed by a carriage return as the
|
---|
519 | first line of its output. We need a null-terminated string. */
|
---|
520 | if ((cBufSize < 2) || (acBuffer[cBufSize - 1] != '\n'))
|
---|
521 | {
|
---|
522 | pclose(pfSetupHandle);
|
---|
523 | LogRel(("The TAP interface setup script did not return the name of a TAP device.\n"));
|
---|
524 | return VERR_HOSTIF_INIT_FAILED;
|
---|
525 | }
|
---|
526 | /* Overwrite the terminating newline character. */
|
---|
527 | acBuffer[cBufSize - 1] = 0;
|
---|
528 | RTStrAPrintf(&pThis->pszDeviceName, "%s", acBuffer);
|
---|
529 | }
|
---|
530 | int rc = pclose(pfSetupHandle);
|
---|
531 | if (!WIFEXITED(rc))
|
---|
532 | {
|
---|
533 | LogRel(("The TAP interface setup script terminated abnormally.\n"));
|
---|
534 | return VERR_HOSTIF_INIT_FAILED;
|
---|
535 | }
|
---|
536 | if (WEXITSTATUS(rc) != 0)
|
---|
537 | {
|
---|
538 | LogRel(("The TAP interface setup script returned a non-zero exit code.\n"));
|
---|
539 | return VERR_HOSTIF_INIT_FAILED;
|
---|
540 | }
|
---|
541 | return VINF_SUCCESS;
|
---|
542 | }
|
---|
543 |
|
---|
544 |
|
---|
545 | /**
|
---|
546 | * Calls OS-specific TAP terminate application/script.
|
---|
547 | *
|
---|
548 | * @returns VBox error code.
|
---|
549 | * @param pThis The instance data.
|
---|
550 | */
|
---|
551 | static int drvTAPTerminateApplication(PDRVTAP pThis)
|
---|
552 | {
|
---|
553 | char *pszArgs[3];
|
---|
554 | pszArgs[0] = pThis->pszTerminateApplication;
|
---|
555 | pszArgs[1] = pThis->pszDeviceName;
|
---|
556 | pszArgs[2] = NULL;
|
---|
557 |
|
---|
558 | Log2(("Starting TAP terminate application: %s %s\n", pThis->pszTerminateApplication, pThis->pszDeviceName));
|
---|
559 | RTPROCESS pid = NIL_RTPROCESS;
|
---|
560 | int rc = RTProcCreate(pszArgs[0], pszArgs, RTENV_DEFAULT, 0, &pid);
|
---|
561 | if (RT_SUCCESS(rc))
|
---|
562 | {
|
---|
563 | RTPROCSTATUS Status;
|
---|
564 | rc = RTProcWait(pid, 0, &Status);
|
---|
565 | if (RT_SUCCESS(rc))
|
---|
566 | {
|
---|
567 | if ( Status.iStatus == 0
|
---|
568 | && Status.enmReason == RTPROCEXITREASON_NORMAL)
|
---|
569 | return VINF_SUCCESS;
|
---|
570 |
|
---|
571 | LogRel(("TAP#%d: Error running TAP terminate application: %s\n", pThis->pDrvIns->iInstance, pThis->pszTerminateApplication));
|
---|
572 | }
|
---|
573 | else
|
---|
574 | LogRel(("TAP#%d: RTProcWait failed for: %s\n", pThis->pDrvIns->iInstance, pThis->pszTerminateApplication));
|
---|
575 | }
|
---|
576 | else
|
---|
577 | {
|
---|
578 | /* Bad. RTProcCreate() failed! */
|
---|
579 | LogRel(("TAP#%d: Failed to fork() process for running TAP terminate application: %s\n", pThis->pDrvIns->iInstance,
|
---|
580 | pThis->pszTerminateApplication, strerror(errno)));
|
---|
581 | }
|
---|
582 | return VERR_HOSTIF_TERM_FAILED;
|
---|
583 | }
|
---|
584 |
|
---|
585 | #endif /* RT_OS_SOLARIS */
|
---|
586 |
|
---|
587 |
|
---|
588 | #ifdef RT_OS_SOLARIS
|
---|
589 | /** From net/if_tun.h, installed by Universal TUN/TAP driver */
|
---|
590 | # define TUNNEWPPA (('T'<<16) | 0x0001)
|
---|
591 | /** Whether to enable ARP for TAP. */
|
---|
592 | # define VBOX_SOLARIS_TAP_ARP 1
|
---|
593 |
|
---|
594 | /**
|
---|
595 | * Creates/Attaches TAP device to IP.
|
---|
596 | *
|
---|
597 | * @returns VBox error code.
|
---|
598 | * @param pThis The instance data.
|
---|
599 | */
|
---|
600 | static DECLCALLBACK(int) SolarisTAPAttach(PDRVTAP pThis)
|
---|
601 | {
|
---|
602 | LogFlow(("SolarisTapAttach: pThis=%p\n", pThis));
|
---|
603 |
|
---|
604 |
|
---|
605 | int IPFileDes = open("/dev/udp", O_RDWR, 0);
|
---|
606 | if (IPFileDes < 0)
|
---|
607 | return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_PDM_HIF_OPEN_FAILED, RT_SRC_POS,
|
---|
608 | N_("Failed to open /dev/udp. errno=%d"), errno);
|
---|
609 |
|
---|
610 | int TapFileDes = open("/dev/tap", O_RDWR, 0);
|
---|
611 | if (TapFileDes < 0)
|
---|
612 | return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_PDM_HIF_OPEN_FAILED, RT_SRC_POS,
|
---|
613 | N_("Failed to open /dev/tap for TAP. errno=%d"), errno);
|
---|
614 |
|
---|
615 | /* Use the PPA from the ifname if possible (e.g "tap2", then use 2 as PPA) */
|
---|
616 | int iPPA = -1;
|
---|
617 | if (pThis->pszDeviceName)
|
---|
618 | {
|
---|
619 | size_t cch = strlen(pThis->pszDeviceName);
|
---|
620 | if (cch > 1 && RT_C_IS_DIGIT(pThis->pszDeviceName[cch - 1]) != 0)
|
---|
621 | iPPA = pThis->pszDeviceName[cch - 1] - '0';
|
---|
622 | }
|
---|
623 |
|
---|
624 | struct strioctl ioIF;
|
---|
625 | ioIF.ic_cmd = TUNNEWPPA;
|
---|
626 | ioIF.ic_len = sizeof(iPPA);
|
---|
627 | ioIF.ic_dp = (char *)(&iPPA);
|
---|
628 | ioIF.ic_timout = 0;
|
---|
629 | iPPA = ioctl(TapFileDes, I_STR, &ioIF);
|
---|
630 | if (iPPA < 0)
|
---|
631 | {
|
---|
632 | close(TapFileDes);
|
---|
633 | return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_HOSTIF_IOCTL, RT_SRC_POS,
|
---|
634 | N_("Failed to get new interface. errno=%d"), errno);
|
---|
635 | }
|
---|
636 |
|
---|
637 | int InterfaceFD = open("/dev/tap", O_RDWR, 0);
|
---|
638 | if (!InterfaceFD)
|
---|
639 | return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_PDM_HIF_OPEN_FAILED, RT_SRC_POS,
|
---|
640 | N_("Failed to open interface /dev/tap. errno=%d"), errno);
|
---|
641 |
|
---|
642 | if (ioctl(InterfaceFD, I_PUSH, "ip") == -1)
|
---|
643 | {
|
---|
644 | close(InterfaceFD);
|
---|
645 | return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_HOSTIF_IOCTL, RT_SRC_POS,
|
---|
646 | N_("Failed to push IP. errno=%d"), errno);
|
---|
647 | }
|
---|
648 |
|
---|
649 | struct lifreq ifReq;
|
---|
650 | memset(&ifReq, 0, sizeof(ifReq));
|
---|
651 | if (ioctl(InterfaceFD, SIOCGLIFFLAGS, &ifReq) == -1)
|
---|
652 | LogRel(("TAP#%d: Failed to get interface flags.\n", pThis->pDrvIns->iInstance));
|
---|
653 |
|
---|
654 | ifReq.lifr_ppa = iPPA;
|
---|
655 | RTStrCopy(ifReq.lifr_name, sizeof(ifReq.lifr_name), pThis->pszDeviceName);
|
---|
656 |
|
---|
657 | if (ioctl(InterfaceFD, SIOCSLIFNAME, &ifReq) == -1)
|
---|
658 | LogRel(("TAP#%d: Failed to set PPA. errno=%d\n", pThis->pDrvIns->iInstance, errno));
|
---|
659 |
|
---|
660 | if (ioctl(InterfaceFD, SIOCGLIFFLAGS, &ifReq) == -1)
|
---|
661 | LogRel(("TAP#%d: Failed to get interface flags after setting PPA. errno=%d\n", pThis->pDrvIns->iInstance, errno));
|
---|
662 |
|
---|
663 | # ifdef VBOX_SOLARIS_TAP_ARP
|
---|
664 | /* Interface */
|
---|
665 | if (ioctl(InterfaceFD, I_PUSH, "arp") == -1)
|
---|
666 | LogRel(("TAP#%d: Failed to push ARP to Interface FD. errno=%d\n", pThis->pDrvIns->iInstance, errno));
|
---|
667 |
|
---|
668 | /* IP */
|
---|
669 | if (ioctl(IPFileDes, I_POP, NULL) == -1)
|
---|
670 | LogRel(("TAP#%d: Failed I_POP from IP FD. errno=%d\n", pThis->pDrvIns->iInstance, errno));
|
---|
671 |
|
---|
672 | if (ioctl(IPFileDes, I_PUSH, "arp") == -1)
|
---|
673 | LogRel(("TAP#%d: Failed to push ARP to IP FD. errno=%d\n", pThis->pDrvIns->iInstance, errno));
|
---|
674 |
|
---|
675 | /* ARP */
|
---|
676 | int ARPFileDes = open("/dev/tap", O_RDWR, 0);
|
---|
677 | if (ARPFileDes < 0)
|
---|
678 | LogRel(("TAP#%d: Failed to open for /dev/tap for ARP. errno=%d", pThis->pDrvIns->iInstance, errno));
|
---|
679 |
|
---|
680 | if (ioctl(ARPFileDes, I_PUSH, "arp") == -1)
|
---|
681 | LogRel(("TAP#%d: Failed to push ARP to ARP FD. errno=%d\n", pThis->pDrvIns->iInstance, errno));
|
---|
682 |
|
---|
683 | ioIF.ic_cmd = SIOCSLIFNAME;
|
---|
684 | ioIF.ic_timout = 0;
|
---|
685 | ioIF.ic_len = sizeof(ifReq);
|
---|
686 | ioIF.ic_dp = (char *)&ifReq;
|
---|
687 | if (ioctl(ARPFileDes, I_STR, &ioIF) == -1)
|
---|
688 | LogRel(("TAP#%d: Failed to set interface name to ARP.\n", pThis->pDrvIns->iInstance));
|
---|
689 | # endif
|
---|
690 |
|
---|
691 | /* We must use I_LINK and not I_PLINK as I_PLINK makes the link persistent.
|
---|
692 | * Then we would not be able unlink the interface if we reuse it.
|
---|
693 | * Even 'unplumb' won't work after that.
|
---|
694 | */
|
---|
695 | int IPMuxID = ioctl(IPFileDes, I_LINK, InterfaceFD);
|
---|
696 | if (IPMuxID == -1)
|
---|
697 | {
|
---|
698 | close(InterfaceFD);
|
---|
699 | # ifdef VBOX_SOLARIS_TAP_ARP
|
---|
700 | close(ARPFileDes);
|
---|
701 | # endif
|
---|
702 | LogRel(("TAP#%d: Cannot link TAP device to IP.\n", pThis->pDrvIns->iInstance));
|
---|
703 | return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_HOSTIF_IOCTL, RT_SRC_POS,
|
---|
704 | N_("Failed to link TAP device to IP. Check TAP interface name. errno=%d"), errno);
|
---|
705 | }
|
---|
706 |
|
---|
707 | # ifdef VBOX_SOLARIS_TAP_ARP
|
---|
708 | int ARPMuxID = ioctl(IPFileDes, I_LINK, ARPFileDes);
|
---|
709 | if (ARPMuxID == -1)
|
---|
710 | LogRel(("TAP#%d: Failed to link TAP device to ARP\n", pThis->pDrvIns->iInstance));
|
---|
711 |
|
---|
712 | close(ARPFileDes);
|
---|
713 | # endif
|
---|
714 | close(InterfaceFD);
|
---|
715 |
|
---|
716 | /* Reuse ifReq */
|
---|
717 | memset(&ifReq, 0, sizeof(ifReq));
|
---|
718 | RTStrCopy(ifReq.lifr_name, sizeof(ifReq.lifr_name), pThis->pszDeviceName);
|
---|
719 | ifReq.lifr_ip_muxid = IPMuxID;
|
---|
720 | # ifdef VBOX_SOLARIS_TAP_ARP
|
---|
721 | ifReq.lifr_arp_muxid = ARPMuxID;
|
---|
722 | # endif
|
---|
723 |
|
---|
724 | if (ioctl(IPFileDes, SIOCSLIFMUXID, &ifReq) == -1)
|
---|
725 | {
|
---|
726 | # ifdef VBOX_SOLARIS_TAP_ARP
|
---|
727 | ioctl(IPFileDes, I_PUNLINK, ARPMuxID);
|
---|
728 | # endif
|
---|
729 | ioctl(IPFileDes, I_PUNLINK, IPMuxID);
|
---|
730 | close(IPFileDes);
|
---|
731 | LogRel(("TAP#%d: Failed to set Mux ID.\n", pThis->pDrvIns->iInstance));
|
---|
732 | return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_HOSTIF_IOCTL, RT_SRC_POS,
|
---|
733 | N_("Failed to set Mux ID. Check TAP interface name. errno=%d"), errno);
|
---|
734 | }
|
---|
735 |
|
---|
736 | int rc = RTFileFromNative(&pThis->hFileDevice, TapFileDes);
|
---|
737 | AssertLogRelRC(rc);
|
---|
738 | if (RT_FAILURE(rc))
|
---|
739 | {
|
---|
740 | close(IPFileDes);
|
---|
741 | close(TapFileDes);
|
---|
742 | }
|
---|
743 | pThis->iIPFileDes = IPFileDes;
|
---|
744 |
|
---|
745 | return VINF_SUCCESS;
|
---|
746 | }
|
---|
747 |
|
---|
748 | #endif /* RT_OS_SOLARIS */
|
---|
749 |
|
---|
750 | /* -=-=-=-=- PDMIBASE -=-=-=-=- */
|
---|
751 |
|
---|
752 | /**
|
---|
753 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
754 | */
|
---|
755 | static DECLCALLBACK(void *) drvTAPQueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
756 | {
|
---|
757 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
|
---|
758 | PDRVTAP pThis = PDMINS_2_DATA(pDrvIns, PDRVTAP);
|
---|
759 |
|
---|
760 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
|
---|
761 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKUP, &pThis->INetworkUp);
|
---|
762 | return NULL;
|
---|
763 | }
|
---|
764 |
|
---|
765 | /* -=-=-=-=- PDMDRVREG -=-=-=-=- */
|
---|
766 |
|
---|
767 | /**
|
---|
768 | * Destruct a driver instance.
|
---|
769 | *
|
---|
770 | * Most VM resources are freed by the VM. This callback is provided so that any non-VM
|
---|
771 | * resources can be freed correctly.
|
---|
772 | *
|
---|
773 | * @param pDrvIns The driver instance data.
|
---|
774 | */
|
---|
775 | static DECLCALLBACK(void) drvTAPDestruct(PPDMDRVINS pDrvIns)
|
---|
776 | {
|
---|
777 | LogFlow(("drvTAPDestruct\n"));
|
---|
778 | PDRVTAP pThis = PDMINS_2_DATA(pDrvIns, PDRVTAP);
|
---|
779 | PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
|
---|
780 |
|
---|
781 | /*
|
---|
782 | * Terminate the control pipe.
|
---|
783 | */
|
---|
784 | int rc;
|
---|
785 | if (pThis->hPipeWrite != NIL_RTPIPE)
|
---|
786 | {
|
---|
787 | rc = RTPipeClose(pThis->hPipeWrite); AssertRC(rc);
|
---|
788 | pThis->hPipeWrite = NIL_RTPIPE;
|
---|
789 | }
|
---|
790 | if (pThis->hPipeRead != NIL_RTPIPE)
|
---|
791 | {
|
---|
792 | rc = RTPipeClose(pThis->hPipeRead); AssertRC(rc);
|
---|
793 | pThis->hPipeRead = NIL_RTPIPE;
|
---|
794 | }
|
---|
795 |
|
---|
796 | #ifdef RT_OS_SOLARIS
|
---|
797 | /** @todo r=bird: This *does* need checking against ConsoleImpl2.cpp if used on non-solaris systems. */
|
---|
798 | if (pThis->hFileDevice != NIL_RTFILE)
|
---|
799 | {
|
---|
800 | int rc = RTFileClose(pThis->hFileDevice); AssertRC(rc);
|
---|
801 | pThis->hFileDevice = NIL_RTFILE;
|
---|
802 | }
|
---|
803 |
|
---|
804 | /*
|
---|
805 | * Call TerminateApplication after closing the device otherwise
|
---|
806 | * TerminateApplication would not be able to unplumb it.
|
---|
807 | */
|
---|
808 | if (pThis->pszTerminateApplication)
|
---|
809 | drvTAPTerminateApplication(pThis);
|
---|
810 |
|
---|
811 | #endif /* RT_OS_SOLARIS */
|
---|
812 |
|
---|
813 | #ifdef RT_OS_SOLARIS
|
---|
814 | if (!pThis->fStatic)
|
---|
815 | RTStrFree(pThis->pszDeviceName); /* allocated by drvTAPSetupApplication */
|
---|
816 | else
|
---|
817 | MMR3HeapFree(pThis->pszDeviceName);
|
---|
818 | #else
|
---|
819 | MMR3HeapFree(pThis->pszDeviceName);
|
---|
820 | #endif
|
---|
821 | pThis->pszDeviceName = NULL;
|
---|
822 | MMR3HeapFree(pThis->pszSetupApplication);
|
---|
823 | pThis->pszSetupApplication = NULL;
|
---|
824 | MMR3HeapFree(pThis->pszTerminateApplication);
|
---|
825 | pThis->pszTerminateApplication = NULL;
|
---|
826 |
|
---|
827 | /*
|
---|
828 | * Kill the xmit lock.
|
---|
829 | */
|
---|
830 | if (RTCritSectIsInitialized(&pThis->XmitLock))
|
---|
831 | RTCritSectDelete(&pThis->XmitLock);
|
---|
832 |
|
---|
833 | #ifdef VBOX_WITH_STATISTICS
|
---|
834 | /*
|
---|
835 | * Deregister statistics.
|
---|
836 | */
|
---|
837 | PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatPktSent);
|
---|
838 | PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatPktSentBytes);
|
---|
839 | PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatPktRecv);
|
---|
840 | PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatPktRecvBytes);
|
---|
841 | PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatTransmit);
|
---|
842 | PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatReceive);
|
---|
843 | #endif /* VBOX_WITH_STATISTICS */
|
---|
844 | }
|
---|
845 |
|
---|
846 |
|
---|
847 | /**
|
---|
848 | * Construct a TAP network transport driver instance.
|
---|
849 | *
|
---|
850 | * @copydoc FNPDMDRVCONSTRUCT
|
---|
851 | */
|
---|
852 | static DECLCALLBACK(int) drvTAPConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
|
---|
853 | {
|
---|
854 | RT_NOREF(fFlags);
|
---|
855 | PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
|
---|
856 | PDRVTAP pThis = PDMINS_2_DATA(pDrvIns, PDRVTAP);
|
---|
857 |
|
---|
858 | /*
|
---|
859 | * Init the static parts.
|
---|
860 | */
|
---|
861 | pThis->pDrvIns = pDrvIns;
|
---|
862 | pThis->hFileDevice = NIL_RTFILE;
|
---|
863 | pThis->hPipeWrite = NIL_RTPIPE;
|
---|
864 | pThis->hPipeRead = NIL_RTPIPE;
|
---|
865 | pThis->pszDeviceName = NULL;
|
---|
866 | #ifdef RT_OS_SOLARIS
|
---|
867 | pThis->iIPFileDes = -1;
|
---|
868 | pThis->fStatic = true;
|
---|
869 | #endif
|
---|
870 | pThis->pszSetupApplication = NULL;
|
---|
871 | pThis->pszTerminateApplication = NULL;
|
---|
872 |
|
---|
873 | /* IBase */
|
---|
874 | pDrvIns->IBase.pfnQueryInterface = drvTAPQueryInterface;
|
---|
875 | /* INetwork */
|
---|
876 | pThis->INetworkUp.pfnBeginXmit = drvTAPNetworkUp_BeginXmit;
|
---|
877 | pThis->INetworkUp.pfnAllocBuf = drvTAPNetworkUp_AllocBuf;
|
---|
878 | pThis->INetworkUp.pfnFreeBuf = drvTAPNetworkUp_FreeBuf;
|
---|
879 | pThis->INetworkUp.pfnSendBuf = drvTAPNetworkUp_SendBuf;
|
---|
880 | pThis->INetworkUp.pfnEndXmit = drvTAPNetworkUp_EndXmit;
|
---|
881 | pThis->INetworkUp.pfnSetPromiscuousMode = drvTAPNetworkUp_SetPromiscuousMode;
|
---|
882 | pThis->INetworkUp.pfnNotifyLinkChanged = drvTAPNetworkUp_NotifyLinkChanged;
|
---|
883 |
|
---|
884 | #ifdef VBOX_WITH_STATISTICS
|
---|
885 | /*
|
---|
886 | * Statistics.
|
---|
887 | */
|
---|
888 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktSent, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of sent packets.", "/Drivers/TAP%d/Packets/Sent", pDrvIns->iInstance);
|
---|
889 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktSentBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Number of sent bytes.", "/Drivers/TAP%d/Bytes/Sent", pDrvIns->iInstance);
|
---|
890 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktRecv, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of received packets.", "/Drivers/TAP%d/Packets/Received", pDrvIns->iInstance);
|
---|
891 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktRecvBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Number of received bytes.", "/Drivers/TAP%d/Bytes/Received", pDrvIns->iInstance);
|
---|
892 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatTransmit, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling packet transmit runs.", "/Drivers/TAP%d/Transmit", pDrvIns->iInstance);
|
---|
893 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatReceive, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling packet receive runs.", "/Drivers/TAP%d/Receive", pDrvIns->iInstance);
|
---|
894 | #endif /* VBOX_WITH_STATISTICS */
|
---|
895 |
|
---|
896 | /*
|
---|
897 | * Validate the config.
|
---|
898 | */
|
---|
899 | if (!CFGMR3AreValuesValid(pCfg, "Device\0InitProg\0TermProg\0FileHandle\0TAPSetupApplication\0TAPTerminateApplication\0MAC"))
|
---|
900 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES, "");
|
---|
901 |
|
---|
902 | /*
|
---|
903 | * Check that no-one is attached to us.
|
---|
904 | */
|
---|
905 | AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
|
---|
906 | ("Configuration error: Not possible to attach anything to this driver!\n"),
|
---|
907 | VERR_PDM_DRVINS_NO_ATTACH);
|
---|
908 |
|
---|
909 | /*
|
---|
910 | * Query the network port interface.
|
---|
911 | */
|
---|
912 | pThis->pIAboveNet = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMINETWORKDOWN);
|
---|
913 | if (!pThis->pIAboveNet)
|
---|
914 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE,
|
---|
915 | N_("Configuration error: The above device/driver didn't export the network port interface"));
|
---|
916 |
|
---|
917 | /*
|
---|
918 | * Read the configuration.
|
---|
919 | */
|
---|
920 | int rc;
|
---|
921 | #if defined(RT_OS_SOLARIS) /** @todo Other platforms' TAP code should be moved here from ConsoleImpl. */
|
---|
922 | rc = CFGMR3QueryStringAlloc(pCfg, "TAPSetupApplication", &pThis->pszSetupApplication);
|
---|
923 | if (RT_SUCCESS(rc))
|
---|
924 | {
|
---|
925 | if (!RTPathExists(pThis->pszSetupApplication))
|
---|
926 | return PDMDrvHlpVMSetError(pDrvIns, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS,
|
---|
927 | N_("Invalid TAP setup program path: %s"), pThis->pszSetupApplication);
|
---|
928 | }
|
---|
929 | else if (rc != VERR_CFGM_VALUE_NOT_FOUND)
|
---|
930 | return PDMDRV_SET_ERROR(pDrvIns, rc, N_("Configuration error: failed to query \"TAPTerminateApplication\""));
|
---|
931 |
|
---|
932 | rc = CFGMR3QueryStringAlloc(pCfg, "TAPTerminateApplication", &pThis->pszTerminateApplication);
|
---|
933 | if (RT_SUCCESS(rc))
|
---|
934 | {
|
---|
935 | if (!RTPathExists(pThis->pszTerminateApplication))
|
---|
936 | return PDMDrvHlpVMSetError(pDrvIns, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS,
|
---|
937 | N_("Invalid TAP terminate program path: %s"), pThis->pszTerminateApplication);
|
---|
938 | }
|
---|
939 | else if (rc != VERR_CFGM_VALUE_NOT_FOUND)
|
---|
940 | return PDMDRV_SET_ERROR(pDrvIns, rc, N_("Configuration error: failed to query \"TAPTerminateApplication\""));
|
---|
941 |
|
---|
942 | rc = CFGMR3QueryStringAlloc(pCfg, "Device", &pThis->pszDeviceName);
|
---|
943 | if (RT_FAILURE(rc))
|
---|
944 | pThis->fStatic = false;
|
---|
945 |
|
---|
946 | /* Obtain the device name from the setup application (if none was specified). */
|
---|
947 | if (pThis->pszSetupApplication)
|
---|
948 | {
|
---|
949 | rc = drvTAPSetupApplication(pThis);
|
---|
950 | if (RT_FAILURE(rc))
|
---|
951 | return PDMDrvHlpVMSetError(pDrvIns, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS,
|
---|
952 | N_("Error running TAP setup application. rc=%d"), rc);
|
---|
953 | }
|
---|
954 |
|
---|
955 | /*
|
---|
956 | * Do the setup.
|
---|
957 | */
|
---|
958 | rc = SolarisTAPAttach(pThis);
|
---|
959 | if (RT_FAILURE(rc))
|
---|
960 | return rc;
|
---|
961 |
|
---|
962 | #else /* !RT_OS_SOLARIS */
|
---|
963 |
|
---|
964 | uint64_t u64File;
|
---|
965 | rc = CFGMR3QueryU64(pCfg, "FileHandle", &u64File);
|
---|
966 | if (RT_FAILURE(rc))
|
---|
967 | return PDMDRV_SET_ERROR(pDrvIns, rc,
|
---|
968 | N_("Configuration error: Query for \"FileHandle\" 32-bit signed integer failed"));
|
---|
969 | pThis->hFileDevice = (RTFILE)(uintptr_t)u64File;
|
---|
970 | if (!RTFileIsValid(pThis->hFileDevice))
|
---|
971 | return PDMDrvHlpVMSetError(pDrvIns, VERR_INVALID_HANDLE, RT_SRC_POS,
|
---|
972 | N_("The TAP file handle %RTfile is not valid"), pThis->hFileDevice);
|
---|
973 | #endif /* !RT_OS_SOLARIS */
|
---|
974 |
|
---|
975 | /*
|
---|
976 | * Create the transmit lock.
|
---|
977 | */
|
---|
978 | rc = RTCritSectInit(&pThis->XmitLock);
|
---|
979 | AssertRCReturn(rc, rc);
|
---|
980 |
|
---|
981 | /*
|
---|
982 | * Make sure the descriptor is non-blocking and valid.
|
---|
983 | *
|
---|
984 | * We should actually query if it's a TAP device, but I haven't
|
---|
985 | * found any way to do that.
|
---|
986 | */
|
---|
987 | if (fcntl(RTFileToNative(pThis->hFileDevice), F_SETFL, O_NONBLOCK) == -1)
|
---|
988 | return PDMDrvHlpVMSetError(pDrvIns, VERR_HOSTIF_IOCTL, RT_SRC_POS,
|
---|
989 | N_("Configuration error: Failed to configure /dev/net/tun. errno=%d"), errno);
|
---|
990 | /** @todo determine device name. This can be done by reading the link /proc/<pid>/fd/<fd> */
|
---|
991 | Log(("drvTAPContruct: %d (from fd)\n", (intptr_t)pThis->hFileDevice));
|
---|
992 | rc = VINF_SUCCESS;
|
---|
993 |
|
---|
994 | /*
|
---|
995 | * Create the control pipe.
|
---|
996 | */
|
---|
997 | rc = RTPipeCreate(&pThis->hPipeRead, &pThis->hPipeWrite, 0 /*fFlags*/);
|
---|
998 | AssertRCReturn(rc, rc);
|
---|
999 |
|
---|
1000 | /*
|
---|
1001 | * Create the async I/O thread.
|
---|
1002 | */
|
---|
1003 | rc = PDMDrvHlpThreadCreate(pDrvIns, &pThis->pThread, pThis, drvTAPAsyncIoThread, drvTapAsyncIoWakeup, 128 * _1K, RTTHREADTYPE_IO, "TAP");
|
---|
1004 | AssertRCReturn(rc, rc);
|
---|
1005 |
|
---|
1006 | return rc;
|
---|
1007 | }
|
---|
1008 |
|
---|
1009 |
|
---|
1010 | /**
|
---|
1011 | * TAP network transport driver registration record.
|
---|
1012 | */
|
---|
1013 | const PDMDRVREG g_DrvHostInterface =
|
---|
1014 | {
|
---|
1015 | /* u32Version */
|
---|
1016 | PDM_DRVREG_VERSION,
|
---|
1017 | /* szName */
|
---|
1018 | "HostInterface",
|
---|
1019 | /* szRCMod */
|
---|
1020 | "",
|
---|
1021 | /* szR0Mod */
|
---|
1022 | "",
|
---|
1023 | /* pszDescription */
|
---|
1024 | "TAP Network Transport Driver",
|
---|
1025 | /* fFlags */
|
---|
1026 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
1027 | /* fClass. */
|
---|
1028 | PDM_DRVREG_CLASS_NETWORK,
|
---|
1029 | /* cMaxInstances */
|
---|
1030 | ~0U,
|
---|
1031 | /* cbInstance */
|
---|
1032 | sizeof(DRVTAP),
|
---|
1033 | /* pfnConstruct */
|
---|
1034 | drvTAPConstruct,
|
---|
1035 | /* pfnDestruct */
|
---|
1036 | drvTAPDestruct,
|
---|
1037 | /* pfnRelocate */
|
---|
1038 | NULL,
|
---|
1039 | /* pfnIOCtl */
|
---|
1040 | NULL,
|
---|
1041 | /* pfnPowerOn */
|
---|
1042 | NULL,
|
---|
1043 | /* pfnReset */
|
---|
1044 | NULL,
|
---|
1045 | /* pfnSuspend */
|
---|
1046 | NULL, /** @todo Do power on, suspend and resume handlers! */
|
---|
1047 | /* pfnResume */
|
---|
1048 | NULL,
|
---|
1049 | /* pfnAttach */
|
---|
1050 | NULL,
|
---|
1051 | /* pfnDetach */
|
---|
1052 | NULL,
|
---|
1053 | /* pfnPowerOff */
|
---|
1054 | NULL,
|
---|
1055 | /* pfnSoftReset */
|
---|
1056 | NULL,
|
---|
1057 | /* u32EndVersion */
|
---|
1058 | PDM_DRVREG_VERSION
|
---|
1059 | };
|
---|
1060 |
|
---|