VirtualBox

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

Last change on this file since 33000 was 32226, checked in by vboxsync, 14 years ago

warning

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

© 2023 Oracle
ContactPrivacy policyTerms of Use