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