VirtualBox

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

Last change on this file since 63211 was 63211, checked in by vboxsync, 9 years ago

Devices: warnings (gcc)

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette