VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/DrvUDPTunnel.cpp@ 65314

Last change on this file since 65314 was 62962, checked in by vboxsync, 8 years ago

Devices: warnings

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

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