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