VirtualBox

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

Last change on this file since 30351 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

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