VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/DrvTAP.cpp@ 37637

Last change on this file since 37637 was 37596, checked in by vboxsync, 13 years ago

*: RTFILE becomes a pointer, RTFileOpen++ expands it's flags paramter from uint32_t to uint64_t.

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