1 | /* $Id: DrvVDE.cpp 57358 2015-08-14 15:16:38Z 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-2015 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 | 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->pVdeConn, 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->pVdeConn, 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->pVdeConn);
|
---|
322 | aFDs[0].events = POLLIN | POLLPRI;
|
---|
323 | aFDs[0].revents = 0;
|
---|
324 | aFDs[1].fd = RTPipeToNative(pThis->hPipeRead);
|
---|
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->pVdeConn, 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 | RTPipeRead(pThis->hPipeRead, &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 | size_t cbIgnored;
|
---|
443 | int rc = RTPipeWrite(pThis->hPipeWrite, "", 1, &cbIgnored);
|
---|
444 | AssertRC(rc);
|
---|
445 |
|
---|
446 | return VINF_SUCCESS;
|
---|
447 | }
|
---|
448 |
|
---|
449 |
|
---|
450 | /* -=-=-=-=- PDMIBASE -=-=-=-=- */
|
---|
451 |
|
---|
452 | /**
|
---|
453 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
454 | */
|
---|
455 | static DECLCALLBACK(void *) drvVDEQueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
456 | {
|
---|
457 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
|
---|
458 | PDRVVDE pThis = PDMINS_2_DATA(pDrvIns, PDRVVDE);
|
---|
459 |
|
---|
460 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
|
---|
461 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKUP, &pThis->INetworkUp);
|
---|
462 | return NULL;
|
---|
463 | }
|
---|
464 |
|
---|
465 | /* -=-=-=-=- PDMDRVREG -=-=-=-=- */
|
---|
466 |
|
---|
467 | /**
|
---|
468 | * Destruct a driver instance.
|
---|
469 | *
|
---|
470 | * Most VM resources are freed by the VM. This callback is provided so that any non-VM
|
---|
471 | * resources can be freed correctly.
|
---|
472 | *
|
---|
473 | * @param pDrvIns The driver instance data.
|
---|
474 | */
|
---|
475 | static DECLCALLBACK(void) drvVDEDestruct(PPDMDRVINS pDrvIns)
|
---|
476 | {
|
---|
477 | LogFlow(("drvVDEDestruct\n"));
|
---|
478 | PDRVVDE pThis = PDMINS_2_DATA(pDrvIns, PDRVVDE);
|
---|
479 | PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
|
---|
480 |
|
---|
481 | /*
|
---|
482 | * Terminate the control pipe.
|
---|
483 | */
|
---|
484 | if (pThis->hPipeWrite != NIL_RTPIPE)
|
---|
485 | {
|
---|
486 | RTPipeClose(pThis->hPipeWrite);
|
---|
487 | pThis->hPipeWrite = NIL_RTPIPE;
|
---|
488 | }
|
---|
489 | if (pThis->hPipeRead != NIL_RTPIPE)
|
---|
490 | {
|
---|
491 | RTPipeClose(pThis->hPipeRead);
|
---|
492 | pThis->hPipeRead = NIL_RTPIPE;
|
---|
493 | }
|
---|
494 |
|
---|
495 | MMR3HeapFree(pThis->pszDeviceName);
|
---|
496 | pThis->pszDeviceName = NULL;
|
---|
497 |
|
---|
498 | /*
|
---|
499 | * Kill the xmit lock.
|
---|
500 | */
|
---|
501 | if (RTCritSectIsInitialized(&pThis->XmitLock))
|
---|
502 | RTCritSectDelete(&pThis->XmitLock);
|
---|
503 |
|
---|
504 | if (pThis->pVdeConn)
|
---|
505 | {
|
---|
506 | vde_close(pThis->pVdeConn);
|
---|
507 | pThis->pVdeConn = NULL;
|
---|
508 | }
|
---|
509 |
|
---|
510 | #ifdef VBOX_WITH_STATISTICS
|
---|
511 | /*
|
---|
512 | * Deregister statistics.
|
---|
513 | */
|
---|
514 | PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatPktSent);
|
---|
515 | PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatPktSentBytes);
|
---|
516 | PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatPktRecv);
|
---|
517 | PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatPktRecvBytes);
|
---|
518 | PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatTransmit);
|
---|
519 | PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatReceive);
|
---|
520 | #endif /* VBOX_WITH_STATISTICS */
|
---|
521 | }
|
---|
522 |
|
---|
523 |
|
---|
524 | /**
|
---|
525 | * Construct a VDE network transport driver instance.
|
---|
526 | *
|
---|
527 | * @copydoc FNPDMDRVCONSTRUCT
|
---|
528 | */
|
---|
529 | static DECLCALLBACK(int) drvVDEConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
|
---|
530 | {
|
---|
531 | PDRVVDE pThis = PDMINS_2_DATA(pDrvIns, PDRVVDE);
|
---|
532 | PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
|
---|
533 |
|
---|
534 | /*
|
---|
535 | * Init the static parts.
|
---|
536 | */
|
---|
537 | pThis->pDrvIns = pDrvIns;
|
---|
538 | pThis->pszDeviceName = NULL;
|
---|
539 | pThis->hPipeRead = NIL_RTPIPE;
|
---|
540 | pThis->hPipeWrite = NIL_RTPIPE;
|
---|
541 |
|
---|
542 | /* IBase */
|
---|
543 | pDrvIns->IBase.pfnQueryInterface = drvVDEQueryInterface;
|
---|
544 | /* INetwork */
|
---|
545 | pThis->INetworkUp.pfnBeginXmit = drvVDENetworkUp_BeginXmit;
|
---|
546 | pThis->INetworkUp.pfnAllocBuf = drvVDENetworkUp_AllocBuf;
|
---|
547 | pThis->INetworkUp.pfnFreeBuf = drvVDENetworkUp_FreeBuf;
|
---|
548 | pThis->INetworkUp.pfnSendBuf = drvVDENetworkUp_SendBuf;
|
---|
549 | pThis->INetworkUp.pfnEndXmit = drvVDENetworkUp_EndXmit;
|
---|
550 | pThis->INetworkUp.pfnSetPromiscuousMode = drvVDENetworkUp_SetPromiscuousMode;
|
---|
551 | pThis->INetworkUp.pfnNotifyLinkChanged = drvVDENetworkUp_NotifyLinkChanged;
|
---|
552 |
|
---|
553 | #ifdef VBOX_WITH_STATISTICS
|
---|
554 | /*
|
---|
555 | * Statistics.
|
---|
556 | */
|
---|
557 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktSent, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of sent packets.", "/Drivers/VDE%d/Packets/Sent", pDrvIns->iInstance);
|
---|
558 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktSentBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Number of sent bytes.", "/Drivers/VDE%d/Bytes/Sent", pDrvIns->iInstance);
|
---|
559 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktRecv, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of received packets.", "/Drivers/VDE%d/Packets/Received", pDrvIns->iInstance);
|
---|
560 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktRecvBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Number of received bytes.", "/Drivers/VDE%d/Bytes/Received", pDrvIns->iInstance);
|
---|
561 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatTransmit, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling packet transmit runs.", "/Drivers/VDE%d/Transmit", pDrvIns->iInstance);
|
---|
562 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatReceive, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling packet receive runs.", "/Drivers/VDE%d/Receive", pDrvIns->iInstance);
|
---|
563 | #endif /* VBOX_WITH_STATISTICS */
|
---|
564 |
|
---|
565 | /*
|
---|
566 | * Validate the config.
|
---|
567 | */
|
---|
568 | if (!CFGMR3AreValuesValid(pCfg, "network"))
|
---|
569 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES, "");
|
---|
570 |
|
---|
571 | /*
|
---|
572 | * Check that no-one is attached to us.
|
---|
573 | */
|
---|
574 | AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
|
---|
575 | ("Configuration error: Not possible to attach anything to this driver!\n"),
|
---|
576 | VERR_PDM_DRVINS_NO_ATTACH);
|
---|
577 |
|
---|
578 | /*
|
---|
579 | * Query the network port interface.
|
---|
580 | */
|
---|
581 | pThis->pIAboveNet = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMINETWORKDOWN);
|
---|
582 | if (!pThis->pIAboveNet)
|
---|
583 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE,
|
---|
584 | N_("Configuration error: The above device/driver didn't export the network port interface"));
|
---|
585 |
|
---|
586 | /*
|
---|
587 | * Read the configuration.
|
---|
588 | */
|
---|
589 | int rc;
|
---|
590 | char szNetwork[RTPATH_MAX];
|
---|
591 | rc = CFGMR3QueryString(pCfg, "network", szNetwork, sizeof(szNetwork));
|
---|
592 | if (RT_FAILURE(rc))
|
---|
593 | *szNetwork=0;
|
---|
594 |
|
---|
595 | if (RT_FAILURE(DrvVDELoadVDEPlug()))
|
---|
596 | return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_HIF_OPEN_FAILED, RT_SRC_POS,
|
---|
597 | N_("VDEplug library: not found"));
|
---|
598 | pThis->pVdeConn = vde_open(szNetwork, "VirtualBOX", NULL);
|
---|
599 | if (pThis->pVdeConn == NULL)
|
---|
600 | return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_PDM_HIF_OPEN_FAILED, RT_SRC_POS,
|
---|
601 | N_("Failed to connect to the VDE SWITCH"));
|
---|
602 |
|
---|
603 | /*
|
---|
604 | * Create the transmit lock.
|
---|
605 | */
|
---|
606 | rc = RTCritSectInit(&pThis->XmitLock);
|
---|
607 | AssertRCReturn(rc, rc);
|
---|
608 |
|
---|
609 | /*
|
---|
610 | * Create the control pipe.
|
---|
611 | */
|
---|
612 | rc = RTPipeCreate(&pThis->hPipeRead, &pThis->hPipeWrite, 0 /*fFlags*/);
|
---|
613 | AssertRCReturn(rc, rc);
|
---|
614 |
|
---|
615 | /*
|
---|
616 | * Create the async I/O thread.
|
---|
617 | */
|
---|
618 | rc = PDMDrvHlpThreadCreate(pDrvIns, &pThis->pThread, pThis, drvVDEAsyncIoThread, drvVDEAsyncIoWakeup, 128 * _1K, RTTHREADTYPE_IO, "VDE");
|
---|
619 | AssertRCReturn(rc, rc);
|
---|
620 |
|
---|
621 | return rc;
|
---|
622 | }
|
---|
623 |
|
---|
624 |
|
---|
625 | /**
|
---|
626 | * VDE network transport driver registration record.
|
---|
627 | */
|
---|
628 | const PDMDRVREG g_DrvVDE =
|
---|
629 | {
|
---|
630 | /* u32Version */
|
---|
631 | PDM_DRVREG_VERSION,
|
---|
632 | /* szName */
|
---|
633 | "VDE",
|
---|
634 | /* szRCMod */
|
---|
635 | "",
|
---|
636 | /* szR0Mod */
|
---|
637 | "",
|
---|
638 | /* pszDescription */
|
---|
639 | "VDE Network Transport Driver",
|
---|
640 | /* fFlags */
|
---|
641 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
642 | /* fClass. */
|
---|
643 | PDM_DRVREG_CLASS_NETWORK,
|
---|
644 | /* cMaxInstances */
|
---|
645 | ~0U,
|
---|
646 | /* cbInstance */
|
---|
647 | sizeof(DRVVDE),
|
---|
648 | /* pfnConstruct */
|
---|
649 | drvVDEConstruct,
|
---|
650 | /* pfnDestruct */
|
---|
651 | drvVDEDestruct,
|
---|
652 | /* pfnRelocate */
|
---|
653 | NULL,
|
---|
654 | /* pfnIOCtl */
|
---|
655 | NULL,
|
---|
656 | /* pfnPowerOn */
|
---|
657 | NULL,
|
---|
658 | /* pfnReset */
|
---|
659 | NULL,
|
---|
660 | /* pfnSuspend */
|
---|
661 | NULL, /** @todo Do power on, suspend and resume handlers! */
|
---|
662 | /* pfnResume */
|
---|
663 | NULL,
|
---|
664 | /* pfnAttach */
|
---|
665 | NULL,
|
---|
666 | /* pfnDetach */
|
---|
667 | NULL,
|
---|
668 | /* pfnPowerOff */
|
---|
669 | NULL,
|
---|
670 | /* pfnSoftReset */
|
---|
671 | NULL,
|
---|
672 | /* u32EndVersion */
|
---|
673 | PDM_DRVREG_VERSION
|
---|
674 | };
|
---|
675 |
|
---|