VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/DrvVDE.cpp@ 97441

Last change on this file since 97441 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.8 KB
Line 
1/* $Id: DrvVDE.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * VDE network transport driver.
4 */
5
6/*
7 * Contributed by Renzo Davoli. VirtualSquare. University of Bologna, 2010
8 *
9 * Copyright (C) 2006-2022 Oracle and/or its affiliates.
10 *
11 * This file is part of VirtualBox base platform packages, as
12 * available from https://www.virtualbox.org.
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation, in version 3 of the
17 * License.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see <https://www.gnu.org/licenses>.
26 *
27 * SPDX-License-Identifier: GPL-3.0-only
28 */
29
30
31/*********************************************************************************************************************************
32* Header Files *
33*********************************************************************************************************************************/
34#define LOG_GROUP LOG_GROUP_DRV_TUN
35#include <VBox/log.h>
36#include <VBox/vmm/pdmdrv.h>
37#include <VBox/vmm/pdmnetifs.h>
38#include <VBox/vmm/pdmnetinline.h>
39#include <VBox/VDEPlug.h>
40
41#include <iprt/asm.h>
42#include <iprt/assert.h>
43#include <iprt/ctype.h>
44#include <iprt/file.h>
45#include <iprt/mem.h>
46#include <iprt/param.h>
47#include <iprt/path.h>
48#include <iprt/pipe.h>
49#include <iprt/semaphore.h>
50#include <iprt/string.h>
51#include <iprt/thread.h>
52#include <iprt/uuid.h>
53
54#include <sys/ioctl.h>
55#include <sys/poll.h>
56#include <sys/fcntl.h>
57#include <errno.h>
58#include <unistd.h>
59
60#include "VBoxDD.h"
61
62
63/*********************************************************************************************************************************
64* Structures and Typedefs *
65*********************************************************************************************************************************/
66/**
67 * VDE driver instance data.
68 *
69 * @implements PDMINETWORKUP
70 */
71typedef struct DRVVDE
72{
73 /** The network interface. */
74 PDMINETWORKUP INetworkUp;
75 /** The network interface. */
76 PPDMINETWORKDOWN pIAboveNet;
77 /** Pointer to the driver instance. */
78 PPDMDRVINS pDrvIns;
79 /** The configured VDE device name. */
80 char *pszDeviceName;
81 /** The write end of the control pipe. */
82 RTPIPE hPipeWrite;
83 /** The read end of the control pipe. */
84 RTPIPE hPipeRead;
85 /** Reader thread. */
86 PPDMTHREAD pThread;
87 /** The connection to the VDE switch */
88 VDECONN *pVdeConn;
89
90 /** @todo The transmit thread. */
91 /** Transmit lock used by drvTAPNetworkUp_BeginXmit. */
92 RTCRITSECT XmitLock;
93
94#ifdef VBOX_WITH_STATISTICS
95 /** Number of sent packets. */
96 STAMCOUNTER StatPktSent;
97 /** Number of sent bytes. */
98 STAMCOUNTER StatPktSentBytes;
99 /** Number of received packets. */
100 STAMCOUNTER StatPktRecv;
101 /** Number of received bytes. */
102 STAMCOUNTER StatPktRecvBytes;
103 /** Profiling packet transmit runs. */
104 STAMPROFILE StatTransmit;
105 /** Profiling packet receive runs. */
106 STAMPROFILEADV StatReceive;
107#endif /* VBOX_WITH_STATISTICS */
108
109#ifdef LOG_ENABLED
110 /** The nano ts of the last transfer. */
111 uint64_t u64LastTransferTS;
112 /** The nano ts of the last receive. */
113 uint64_t u64LastReceiveTS;
114#endif
115} DRVVDE, *PDRVVDE;
116
117
118/** Converts a pointer to VDE::INetworkUp to a PRDVVDE. */
119#define PDMINETWORKUP_2_DRVVDE(pInterface) ( (PDRVVDE)((uintptr_t)pInterface - RT_UOFFSETOF(DRVVDE, INetworkUp)) )
120
121
122/*********************************************************************************************************************************
123* Internal Functions *
124*********************************************************************************************************************************/
125
126
127
128/**
129 * @interface_method_impl{PDMINETWORKUP,pfnBeginXmit}
130 */
131static DECLCALLBACK(int) drvVDENetworkUp_BeginXmit(PPDMINETWORKUP pInterface, bool fOnWorkerThread)
132{
133 RT_NOREF(fOnWorkerThread);
134 PDRVVDE pThis = PDMINETWORKUP_2_DRVVDE(pInterface);
135 int rc = RTCritSectTryEnter(&pThis->XmitLock);
136 if (RT_FAILURE(rc))
137 {
138 /** @todo XMIT thread */
139 rc = VERR_TRY_AGAIN;
140 }
141 return rc;
142}
143
144
145/**
146 * @interface_method_impl{PDMINETWORKUP,pfnAllocBuf}
147 */
148static DECLCALLBACK(int) drvVDENetworkUp_AllocBuf(PPDMINETWORKUP pInterface, size_t cbMin,
149 PCPDMNETWORKGSO pGso, PPPDMSCATTERGATHER ppSgBuf)
150{
151 RT_NOREF(pInterface);
152#ifdef VBOX_STRICT
153 PDRVVDE pThis = PDMINETWORKUP_2_DRVVDE(pInterface);
154 Assert(RTCritSectIsOwner(&pThis->XmitLock));
155#endif
156
157 /*
158 * Allocate a scatter / gather buffer descriptor that is immediately
159 * followed by the buffer space of its single segment. The GSO context
160 * comes after that again.
161 */
162 PPDMSCATTERGATHER pSgBuf = (PPDMSCATTERGATHER)RTMemAlloc( RT_ALIGN_Z(sizeof(*pSgBuf), 16)
163 + RT_ALIGN_Z(cbMin, 16)
164 + (pGso ? RT_ALIGN_Z(sizeof(*pGso), 16) : 0));
165 if (!pSgBuf)
166 return VERR_NO_MEMORY;
167
168 /*
169 * Initialize the S/G buffer and return.
170 */
171 pSgBuf->fFlags = PDMSCATTERGATHER_FLAGS_MAGIC | PDMSCATTERGATHER_FLAGS_OWNER_1;
172 pSgBuf->cbUsed = 0;
173 pSgBuf->cbAvailable = RT_ALIGN_Z(cbMin, 16);
174 pSgBuf->pvAllocator = NULL;
175 if (!pGso)
176 pSgBuf->pvUser = NULL;
177 else
178 {
179 pSgBuf->pvUser = (uint8_t *)(pSgBuf + 1) + pSgBuf->cbAvailable;
180 *(PPDMNETWORKGSO)pSgBuf->pvUser = *pGso;
181 }
182 pSgBuf->cSegs = 1;
183 pSgBuf->aSegs[0].cbSeg = pSgBuf->cbAvailable;
184 pSgBuf->aSegs[0].pvSeg = pSgBuf + 1;
185
186#if 0 /* poison */
187 memset(pSgBuf->aSegs[0].pvSeg, 'F', pSgBuf->aSegs[0].cbSeg);
188#endif
189 *ppSgBuf = pSgBuf;
190 return VINF_SUCCESS;
191}
192
193
194/**
195 * @interface_method_impl{PDMINETWORKUP,pfnFreeBuf}
196 */
197static DECLCALLBACK(int) drvVDENetworkUp_FreeBuf(PPDMINETWORKUP pInterface, PPDMSCATTERGATHER pSgBuf)
198{
199 RT_NOREF(pInterface);
200#ifdef VBOX_STRICT
201 PDRVVDE pThis = PDMINETWORKUP_2_DRVVDE(pInterface);
202 Assert(RTCritSectIsOwner(&pThis->XmitLock));
203#endif
204 if (pSgBuf)
205 {
206 Assert((pSgBuf->fFlags & PDMSCATTERGATHER_FLAGS_MAGIC_MASK) == PDMSCATTERGATHER_FLAGS_MAGIC);
207 pSgBuf->fFlags = 0;
208 RTMemFree(pSgBuf);
209 }
210 return VINF_SUCCESS;
211}
212
213
214/**
215 * @interface_method_impl{PDMINETWORKUP,pfnSendBuf}
216 */
217static DECLCALLBACK(int) drvVDENetworkUp_SendBuf(PPDMINETWORKUP pInterface, PPDMSCATTERGATHER pSgBuf, bool fOnWorkerThread)
218{
219 RT_NOREF(fOnWorkerThread);
220 PDRVVDE pThis = PDMINETWORKUP_2_DRVVDE(pInterface);
221 STAM_COUNTER_INC(&pThis->StatPktSent);
222 STAM_COUNTER_ADD(&pThis->StatPktSentBytes, pSgBuf->cbUsed);
223 STAM_PROFILE_START(&pThis->StatTransmit, a);
224
225 AssertPtr(pSgBuf);
226 Assert((pSgBuf->fFlags & PDMSCATTERGATHER_FLAGS_MAGIC_MASK) == PDMSCATTERGATHER_FLAGS_MAGIC);
227 Assert(RTCritSectIsOwner(&pThis->XmitLock));
228
229 int rc;
230 if (!pSgBuf->pvUser)
231 {
232#ifdef LOG_ENABLED
233 uint64_t u64Now = RTTimeProgramNanoTS();
234 LogFlow(("drvVDESend: %-4d bytes at %llu ns deltas: r=%llu t=%llu\n",
235 pSgBuf->cbUsed, u64Now, u64Now - pThis->u64LastReceiveTS, u64Now - pThis->u64LastTransferTS));
236 pThis->u64LastTransferTS = u64Now;
237#endif
238 Log2(("drvVDESend: pSgBuf->aSegs[0].pvSeg=%p pSgBuf->cbUsed=%#x\n"
239 "%.*Rhxd\n",
240 pSgBuf->aSegs[0].pvSeg, pSgBuf->cbUsed, pSgBuf->cbUsed, pSgBuf->aSegs[0].pvSeg));
241
242 ssize_t cbSent;
243 cbSent = vde_send(pThis->pVdeConn, pSgBuf->aSegs[0].pvSeg, pSgBuf->cbUsed, 0);
244 rc = cbSent < 0 ? RTErrConvertFromErrno(-cbSent) : VINF_SUCCESS;
245 }
246 else
247 {
248 uint8_t abHdrScratch[256];
249 uint8_t const *pbFrame = (uint8_t const *)pSgBuf->aSegs[0].pvSeg;
250 PCPDMNETWORKGSO pGso = (PCPDMNETWORKGSO)pSgBuf->pvUser;
251 uint32_t const cSegs = PDMNetGsoCalcSegmentCount(pGso, pSgBuf->cbUsed); Assert(cSegs > 1);
252 rc = 0;
253 for (size_t iSeg = 0; iSeg < cSegs; iSeg++)
254 {
255 uint32_t cbSegFrame;
256 void *pvSegFrame = PDMNetGsoCarveSegmentQD(pGso, (uint8_t *)pbFrame, pSgBuf->cbUsed, abHdrScratch,
257 iSeg, cSegs, &cbSegFrame);
258 ssize_t cbSent;
259 cbSent = vde_send(pThis->pVdeConn, pvSegFrame, cbSegFrame, 0);
260 rc = cbSent < 0 ? RTErrConvertFromErrno(-cbSent) : VINF_SUCCESS;
261 if (RT_FAILURE(rc))
262 break;
263 }
264 }
265
266 pSgBuf->fFlags = 0;
267 RTMemFree(pSgBuf);
268
269 STAM_PROFILE_STOP(&pThis->StatTransmit, a);
270 AssertRC(rc);
271 if (RT_FAILURE(rc))
272 rc = rc == VERR_NO_MEMORY ? VERR_NET_NO_BUFFER_SPACE : VERR_NET_DOWN;
273 return rc;
274}
275
276
277/**
278 * @interface_method_impl{PDMINETWORKUP,pfnEndXmit}
279 */
280static DECLCALLBACK(void) drvVDENetworkUp_EndXmit(PPDMINETWORKUP pInterface)
281{
282 PDRVVDE pThis = PDMINETWORKUP_2_DRVVDE(pInterface);
283 RTCritSectLeave(&pThis->XmitLock);
284}
285
286
287/**
288 * @interface_method_impl{PDMINETWORKUP,pfnSetPromiscuousMode}
289 */
290static DECLCALLBACK(void) drvVDENetworkUp_SetPromiscuousMode(PPDMINETWORKUP pInterface, bool fPromiscuous)
291{
292 RT_NOREF(pInterface, fPromiscuous);
293 LogFlow(("drvVDESetPromiscuousMode: fPromiscuous=%d\n", fPromiscuous));
294 /* nothing to do */
295}
296
297
298/**
299 * Notification on link status changes.
300 *
301 * @param pInterface Pointer to the interface structure containing the called function pointer.
302 * @param enmLinkState The new link state.
303 * @thread EMT
304 */
305static DECLCALLBACK(void) drvVDENetworkUp_NotifyLinkChanged(PPDMINETWORKUP pInterface, PDMNETWORKLINKSTATE enmLinkState)
306{
307 RT_NOREF(pInterface, enmLinkState);
308 LogFlow(("drvNATNetworkUp_NotifyLinkChanged: enmLinkState=%d\n", enmLinkState));
309 /** @todo take action on link down and up. Stop the polling and such like. */
310}
311
312
313/**
314 * Asynchronous I/O thread for handling receive.
315 *
316 * @returns VINF_SUCCESS (ignored).
317 * @param Thread Thread handle.
318 * @param pvUser Pointer to a DRVVDE structure.
319 */
320static DECLCALLBACK(int) drvVDEAsyncIoThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
321{
322 PDRVVDE pThis = PDMINS_2_DATA(pDrvIns, PDRVVDE);
323 LogFlow(("drvVDEAsyncIoThread: pThis=%p\n", pThis));
324
325 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
326 return VINF_SUCCESS;
327
328 STAM_PROFILE_ADV_START(&pThis->StatReceive, a);
329
330 /*
331 * Polling loop.
332 */
333 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
334 {
335 /*
336 * Wait for something to become available.
337 */
338 struct pollfd aFDs[2];
339 aFDs[0].fd = vde_datafd(pThis->pVdeConn);
340 aFDs[0].events = POLLIN | POLLPRI;
341 aFDs[0].revents = 0;
342 aFDs[1].fd = RTPipeToNative(pThis->hPipeRead);
343 aFDs[1].events = POLLIN | POLLPRI | POLLERR | POLLHUP;
344 aFDs[1].revents = 0;
345 STAM_PROFILE_ADV_STOP(&pThis->StatReceive, a);
346 errno=0;
347 int rc = poll(&aFDs[0], RT_ELEMENTS(aFDs), -1 /* infinite */);
348
349 /* this might have changed in the meantime */
350 if (pThread->enmState != PDMTHREADSTATE_RUNNING)
351 break;
352
353 STAM_PROFILE_ADV_START(&pThis->StatReceive, a);
354 if ( rc > 0
355 && (aFDs[0].revents & (POLLIN | POLLPRI))
356 && !aFDs[1].revents)
357 {
358 /*
359 * Read the frame.
360 */
361 char achBuf[16384];
362 ssize_t cbRead = 0;
363 cbRead = vde_recv(pThis->pVdeConn, achBuf, sizeof(achBuf), 0);
364 rc = cbRead < 0 ? RTErrConvertFromErrno(-cbRead) : VINF_SUCCESS;
365 if (RT_SUCCESS(rc))
366 {
367 /*
368 * Wait for the device to have space for this frame.
369 * Most guests use frame-sized receive buffers, hence non-zero cbMax
370 * automatically means there is enough room for entire frame. Some
371 * guests (eg. Solaris) use large chains of small receive buffers
372 * (each 128 or so bytes large). We will still start receiving as soon
373 * as cbMax is non-zero because:
374 * - it would be quite expensive for pfnCanReceive to accurately
375 * determine free receive buffer space
376 * - if we were waiting for enough free buffers, there is a risk
377 * of deadlocking because the guest could be waiting for a receive
378 * overflow error to allocate more receive buffers
379 */
380 STAM_PROFILE_ADV_STOP(&pThis->StatReceive, a);
381 int rc1 = pThis->pIAboveNet->pfnWaitReceiveAvail(pThis->pIAboveNet, RT_INDEFINITE_WAIT);
382 STAM_PROFILE_ADV_START(&pThis->StatReceive, a);
383
384 /*
385 * A return code != VINF_SUCCESS means that we were woken up during a VM
386 * state transition. Drop the packet and wait for the next one.
387 */
388 if (RT_FAILURE(rc1))
389 continue;
390
391 /*
392 * Pass the data up.
393 */
394#ifdef LOG_ENABLED
395 uint64_t u64Now = RTTimeProgramNanoTS();
396 LogFlow(("drvVDEAsyncIoThread: %-4d bytes at %llu ns deltas: r=%llu t=%llu\n",
397 cbRead, u64Now, u64Now - pThis->u64LastReceiveTS, u64Now - pThis->u64LastTransferTS));
398 pThis->u64LastReceiveTS = u64Now;
399#endif
400 Log2(("drvVDEAsyncIoThread: cbRead=%#x\n" "%.*Rhxd\n", cbRead, cbRead, achBuf));
401 STAM_COUNTER_INC(&pThis->StatPktRecv);
402 STAM_COUNTER_ADD(&pThis->StatPktRecvBytes, cbRead);
403 rc1 = pThis->pIAboveNet->pfnReceive(pThis->pIAboveNet, achBuf, cbRead);
404 AssertRC(rc1);
405 }
406 else
407 {
408 LogFlow(("drvVDEAsyncIoThread: RTFileRead -> %Rrc\n", rc));
409 if (rc == VERR_INVALID_HANDLE)
410 break;
411 RTThreadYield();
412 }
413 }
414 else if ( rc > 0
415 && aFDs[1].revents)
416 {
417 LogFlow(("drvVDEAsyncIoThread: Control message: enmState=%d revents=%#x\n", pThread->enmState, aFDs[1].revents));
418 if (aFDs[1].revents & (POLLHUP | POLLERR | POLLNVAL))
419 break;
420
421 /* drain the pipe */
422 char ch;
423 size_t cbRead;
424 RTPipeRead(pThis->hPipeRead, &ch, 1, &cbRead);
425 }
426 else
427 {
428 /*
429 * poll() failed for some reason. Yield to avoid eating too much CPU.
430 *
431 * EINTR errors have been seen frequently. They should be harmless, even
432 * if they are not supposed to occur in our setup.
433 */
434 if (errno == EINTR)
435 Log(("rc=%d revents=%#x,%#x errno=%p %s\n", rc, aFDs[0].revents, aFDs[1].revents, errno, strerror(errno)));
436 else
437 AssertMsgFailed(("rc=%d revents=%#x,%#x errno=%p %s\n", rc, aFDs[0].revents, aFDs[1].revents, errno, strerror(errno)));
438 RTThreadYield();
439 }
440 }
441
442
443 LogFlow(("drvVDEAsyncIoThread: returns %Rrc\n", VINF_SUCCESS));
444 STAM_PROFILE_ADV_STOP(&pThis->StatReceive, a);
445 return VINF_SUCCESS;
446}
447
448
449/**
450 * Unblock the send thread so it can respond to a state change.
451 *
452 * @returns VBox status code.
453 * @param pDevIns The pcnet device instance.
454 * @param pThread The send thread.
455 */
456static DECLCALLBACK(int) drvVDEAsyncIoWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
457{
458 RT_NOREF(pThread);
459 PDRVVDE pThis = PDMINS_2_DATA(pDrvIns, PDRVVDE);
460
461 size_t cbIgnored;
462 int rc = RTPipeWrite(pThis->hPipeWrite, "", 1, &cbIgnored);
463 AssertRC(rc);
464
465 return VINF_SUCCESS;
466}
467
468
469/* -=-=-=-=- PDMIBASE -=-=-=-=- */
470
471/**
472 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
473 */
474static DECLCALLBACK(void *) drvVDEQueryInterface(PPDMIBASE pInterface, const char *pszIID)
475{
476 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
477 PDRVVDE pThis = PDMINS_2_DATA(pDrvIns, PDRVVDE);
478
479 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
480 PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKUP, &pThis->INetworkUp);
481 return NULL;
482}
483
484/* -=-=-=-=- PDMDRVREG -=-=-=-=- */
485
486/**
487 * Destruct a driver instance.
488 *
489 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
490 * resources can be freed correctly.
491 *
492 * @param pDrvIns The driver instance data.
493 */
494static DECLCALLBACK(void) drvVDEDestruct(PPDMDRVINS pDrvIns)
495{
496 LogFlow(("drvVDEDestruct\n"));
497 PDRVVDE pThis = PDMINS_2_DATA(pDrvIns, PDRVVDE);
498 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
499
500 /*
501 * Terminate the control pipe.
502 */
503 if (pThis->hPipeWrite != NIL_RTPIPE)
504 {
505 RTPipeClose(pThis->hPipeWrite);
506 pThis->hPipeWrite = NIL_RTPIPE;
507 }
508 if (pThis->hPipeRead != NIL_RTPIPE)
509 {
510 RTPipeClose(pThis->hPipeRead);
511 pThis->hPipeRead = NIL_RTPIPE;
512 }
513
514 PDMDrvHlpMMHeapFree(pDrvIns, pThis->pszDeviceName);
515 pThis->pszDeviceName = NULL;
516
517 /*
518 * Kill the xmit lock.
519 */
520 if (RTCritSectIsInitialized(&pThis->XmitLock))
521 RTCritSectDelete(&pThis->XmitLock);
522
523 if (pThis->pVdeConn)
524 {
525 vde_close(pThis->pVdeConn);
526 pThis->pVdeConn = NULL;
527 }
528
529#ifdef VBOX_WITH_STATISTICS
530 /*
531 * Deregister statistics.
532 */
533 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatPktSent);
534 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatPktSentBytes);
535 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatPktRecv);
536 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatPktRecvBytes);
537 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatTransmit);
538 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatReceive);
539#endif /* VBOX_WITH_STATISTICS */
540}
541
542
543/**
544 * Construct a VDE network transport driver instance.
545 *
546 * @copydoc FNPDMDRVCONSTRUCT
547 */
548static DECLCALLBACK(int) drvVDEConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
549{
550 RT_NOREF(fFlags);
551 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
552 PDRVVDE pThis = PDMINS_2_DATA(pDrvIns, PDRVVDE);
553 PCPDMDRVHLPR3 pHlp = pDrvIns->pHlpR3;
554
555 /*
556 * Init the static parts.
557 */
558 pThis->pDrvIns = pDrvIns;
559 pThis->pszDeviceName = NULL;
560 pThis->hPipeRead = NIL_RTPIPE;
561 pThis->hPipeWrite = NIL_RTPIPE;
562
563 /* IBase */
564 pDrvIns->IBase.pfnQueryInterface = drvVDEQueryInterface;
565 /* INetwork */
566 pThis->INetworkUp.pfnBeginXmit = drvVDENetworkUp_BeginXmit;
567 pThis->INetworkUp.pfnAllocBuf = drvVDENetworkUp_AllocBuf;
568 pThis->INetworkUp.pfnFreeBuf = drvVDENetworkUp_FreeBuf;
569 pThis->INetworkUp.pfnSendBuf = drvVDENetworkUp_SendBuf;
570 pThis->INetworkUp.pfnEndXmit = drvVDENetworkUp_EndXmit;
571 pThis->INetworkUp.pfnSetPromiscuousMode = drvVDENetworkUp_SetPromiscuousMode;
572 pThis->INetworkUp.pfnNotifyLinkChanged = drvVDENetworkUp_NotifyLinkChanged;
573
574#ifdef VBOX_WITH_STATISTICS
575 /*
576 * Statistics.
577 */
578 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktSent, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of sent packets.", "/Drivers/VDE%d/Packets/Sent", pDrvIns->iInstance);
579 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktSentBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Number of sent bytes.", "/Drivers/VDE%d/Bytes/Sent", pDrvIns->iInstance);
580 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktRecv, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of received packets.", "/Drivers/VDE%d/Packets/Received", pDrvIns->iInstance);
581 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktRecvBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Number of received bytes.", "/Drivers/VDE%d/Bytes/Received", pDrvIns->iInstance);
582 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatTransmit, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling packet transmit runs.", "/Drivers/VDE%d/Transmit", pDrvIns->iInstance);
583 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatReceive, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling packet receive runs.", "/Drivers/VDE%d/Receive", pDrvIns->iInstance);
584#endif /* VBOX_WITH_STATISTICS */
585
586 /*
587 * Validate the config.
588 */
589 PDMDRV_VALIDATE_CONFIG_RETURN(pDrvIns, "network", "");
590
591 /*
592 * Check that no-one is attached to us.
593 */
594 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
595 ("Configuration error: Not possible to attach anything to this driver!\n"),
596 VERR_PDM_DRVINS_NO_ATTACH);
597
598 /*
599 * Query the network port interface.
600 */
601 pThis->pIAboveNet = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMINETWORKDOWN);
602 if (!pThis->pIAboveNet)
603 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE,
604 N_("Configuration error: The above device/driver didn't export the network port interface"));
605
606 /*
607 * Read the configuration.
608 */
609 int rc;
610 char szNetwork[RTPATH_MAX];
611 rc = pHlp->pfnCFGMQueryString(pCfg, "network", szNetwork, sizeof(szNetwork));
612 if (RT_FAILURE(rc))
613 *szNetwork=0;
614
615 if (RT_FAILURE(DrvVDELoadVDEPlug()))
616 return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_HIF_OPEN_FAILED, RT_SRC_POS,
617 N_("VDEplug library: not found"));
618 pThis->pVdeConn = vde_open(szNetwork, "VirtualBOX", NULL);
619 if (pThis->pVdeConn == NULL)
620 return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_PDM_HIF_OPEN_FAILED, RT_SRC_POS,
621 N_("Failed to connect to the VDE SWITCH"));
622
623 /*
624 * Create the transmit lock.
625 */
626 rc = RTCritSectInit(&pThis->XmitLock);
627 AssertRCReturn(rc, rc);
628
629 /*
630 * Create the control pipe.
631 */
632 rc = RTPipeCreate(&pThis->hPipeRead, &pThis->hPipeWrite, 0 /*fFlags*/);
633 AssertRCReturn(rc, rc);
634
635 /*
636 * Create the async I/O thread.
637 */
638 rc = PDMDrvHlpThreadCreate(pDrvIns, &pThis->pThread, pThis, drvVDEAsyncIoThread, drvVDEAsyncIoWakeup, 128 * _1K, RTTHREADTYPE_IO, "VDE");
639 AssertRCReturn(rc, rc);
640
641 return rc;
642}
643
644
645/**
646 * VDE network transport driver registration record.
647 */
648const PDMDRVREG g_DrvVDE =
649{
650 /* u32Version */
651 PDM_DRVREG_VERSION,
652 /* szName */
653 "VDE",
654 /* szRCMod */
655 "",
656 /* szR0Mod */
657 "",
658 /* pszDescription */
659 "VDE Network Transport Driver",
660 /* fFlags */
661 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
662 /* fClass. */
663 PDM_DRVREG_CLASS_NETWORK,
664 /* cMaxInstances */
665 ~0U,
666 /* cbInstance */
667 sizeof(DRVVDE),
668 /* pfnConstruct */
669 drvVDEConstruct,
670 /* pfnDestruct */
671 drvVDEDestruct,
672 /* pfnRelocate */
673 NULL,
674 /* pfnIOCtl */
675 NULL,
676 /* pfnPowerOn */
677 NULL,
678 /* pfnReset */
679 NULL,
680 /* pfnSuspend */
681 NULL, /** @todo Do power on, suspend and resume handlers! */
682 /* pfnResume */
683 NULL,
684 /* pfnAttach */
685 NULL,
686 /* pfnDetach */
687 NULL,
688 /* pfnPowerOff */
689 NULL,
690 /* pfnSoftReset */
691 NULL,
692 /* u32EndVersion */
693 PDM_DRVREG_VERSION
694};
695
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