VirtualBox

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

Last change on this file was 106061, checked in by vboxsync, 3 weeks ago

Copyright year updates by scm.

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