VirtualBox

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

Last change on this file since 90447 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.6 KB
Line 
1/* $Id: DrvUDPTunnel.cpp 82968 2020-02-04 10:35:17Z 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-2020 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_UOFFSETOF(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 int rc;
205 if (!pSgBuf->pvUser)
206 {
207#ifdef LOG_ENABLED
208 uint64_t u64Now = RTTimeProgramNanoTS();
209 LogFunc(("%-4d bytes at %llu ns deltas: r=%llu t=%llu\n",
210 pSgBuf->cbUsed, u64Now, u64Now - pThis->u64LastReceiveTS, u64Now - pThis->u64LastTransferTS));
211 pThis->u64LastTransferTS = u64Now;
212#endif
213 Log2(("pSgBuf->aSegs[0].pvSeg=%p pSgBuf->cbUsed=%#x\n%.*Rhxd\n",
214 pSgBuf->aSegs[0].pvSeg, pSgBuf->cbUsed, pSgBuf->cbUsed, pSgBuf->aSegs[0].pvSeg));
215
216 rc = RTUdpWrite(pThis->pServer, pSgBuf->aSegs[0].pvSeg, pSgBuf->cbUsed, &pThis->DestAddress);
217 }
218 else
219 {
220 uint8_t abHdrScratch[256];
221 uint8_t const *pbFrame = (uint8_t const *)pSgBuf->aSegs[0].pvSeg;
222 PCPDMNETWORKGSO pGso = (PCPDMNETWORKGSO)pSgBuf->pvUser;
223 uint32_t const cSegs = PDMNetGsoCalcSegmentCount(pGso, pSgBuf->cbUsed); Assert(cSegs > 1);
224 rc = VINF_SUCCESS;
225 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
226 {
227 uint32_t cbSegFrame;
228 void *pvSegFrame = PDMNetGsoCarveSegmentQD(pGso, (uint8_t *)pbFrame, pSgBuf->cbUsed, abHdrScratch,
229 iSeg, cSegs, &cbSegFrame);
230 rc = RTUdpWrite(pThis->pServer, pvSegFrame, cbSegFrame, &pThis->DestAddress);
231 }
232 }
233
234 pSgBuf->fFlags = 0;
235 RTMemFree(pSgBuf);
236
237 STAM_PROFILE_STOP(&pThis->StatTransmit, a);
238 AssertRC(rc);
239 if (RT_FAILURE(rc))
240 {
241 if (rc == VERR_NO_MEMORY)
242 rc = VERR_NET_NO_BUFFER_SPACE;
243 else
244 rc = VERR_NET_DOWN;
245 }
246 return rc;
247}
248
249
250/**
251 * @interface_method_impl{PDMINETWORKUP,pfnEndXmit}
252 */
253static DECLCALLBACK(void) drvUDPTunnelUp_EndXmit(PPDMINETWORKUP pInterface)
254{
255 PDRVUDPTUNNEL pThis = PDMINETWORKUP_2_DRVUDPTUNNEL(pInterface);
256 RTCritSectLeave(&pThis->XmitLock);
257}
258
259
260/**
261 * @interface_method_impl{PDMINETWORKUP,pfnSetPromiscuousMode}
262 */
263static DECLCALLBACK(void) drvUDPTunnelUp_SetPromiscuousMode(PPDMINETWORKUP pInterface, bool fPromiscuous)
264{
265 RT_NOREF(pInterface, fPromiscuous);
266 LogFlowFunc(("fPromiscuous=%d\n", fPromiscuous));
267 /* nothing to do */
268}
269
270
271/**
272 * Notification on link status changes.
273 *
274 * @param pInterface Pointer to the interface structure containing the called function pointer.
275 * @param enmLinkState The new link state.
276 * @thread EMT
277 */
278static DECLCALLBACK(void) drvUDPTunnelUp_NotifyLinkChanged(PPDMINETWORKUP pInterface, PDMNETWORKLINKSTATE enmLinkState)
279{
280 LogFlowFunc(("enmLinkState=%d\n", enmLinkState));
281 PDRVUDPTUNNEL pThis = PDMINETWORKUP_2_DRVUDPTUNNEL(pInterface);
282
283 bool fLinkDown;
284 switch (enmLinkState)
285 {
286 case PDMNETWORKLINKSTATE_DOWN:
287 case PDMNETWORKLINKSTATE_DOWN_RESUME:
288 fLinkDown = true;
289 break;
290 default:
291 AssertMsgFailed(("enmLinkState=%d\n", enmLinkState));
292 RT_FALL_THRU();
293 case PDMNETWORKLINKSTATE_UP:
294 fLinkDown = false;
295 break;
296 }
297 ASMAtomicXchgSize(&pThis->fLinkDown, fLinkDown);
298}
299
300
301static DECLCALLBACK(int) drvUDPTunnelReceive(RTSOCKET Sock, void *pvUser)
302{
303 PDRVUDPTUNNEL pThis = PDMINS_2_DATA((PPDMDRVINS)pvUser, PDRVUDPTUNNEL);
304 LogFlowFunc(("pThis=%p\n", pThis));
305
306 STAM_PROFILE_ADV_START(&pThis->StatReceive, a);
307
308 /*
309 * Read the frame.
310 */
311 char achBuf[16384];
312 size_t cbRead = 0;
313 int rc = RTUdpRead(Sock, achBuf, sizeof(achBuf), &cbRead, NULL);
314 if (RT_SUCCESS(rc))
315 {
316 if (!pThis->fLinkDown)
317 {
318 /*
319 * Wait for the device to have space for this frame.
320 * Most guests use frame-sized receive buffers, hence non-zero cbMax
321 * automatically means there is enough room for entire frame. Some
322 * guests (eg. Solaris) use large chains of small receive buffers
323 * (each 128 or so bytes large). We will still start receiving as soon
324 * as cbMax is non-zero because:
325 * - it would be quite expensive for pfnCanReceive to accurately
326 * determine free receive buffer space
327 * - if we were waiting for enough free buffers, there is a risk
328 * of deadlocking because the guest could be waiting for a receive
329 * overflow error to allocate more receive buffers
330 */
331 STAM_PROFILE_ADV_STOP(&pThis->StatReceive, a);
332 rc = pThis->pIAboveNet->pfnWaitReceiveAvail(pThis->pIAboveNet, RT_INDEFINITE_WAIT);
333 STAM_PROFILE_ADV_START(&pThis->StatReceive, a);
334
335 /*
336 * A return code != VINF_SUCCESS means that we were woken up during a VM
337 * state transition. Drop the packet and wait for the next one.
338 */
339 if (RT_FAILURE(rc))
340 {
341 STAM_PROFILE_ADV_STOP(&pThis->StatReceive, a);
342 return VINF_SUCCESS;
343 }
344
345 /*
346 * Pass the data up.
347 */
348#ifdef LOG_ENABLED
349 uint64_t u64Now = RTTimeProgramNanoTS();
350 LogFunc(("%-4d bytes at %llu ns deltas: r=%llu t=%llu\n",
351 cbRead, u64Now, u64Now - pThis->u64LastReceiveTS, u64Now - pThis->u64LastTransferTS));
352 pThis->u64LastReceiveTS = u64Now;
353#endif
354 Log2(("cbRead=%#x\n" "%.*Rhxd\n", cbRead, cbRead, achBuf));
355 STAM_COUNTER_INC(&pThis->StatPktRecv);
356 STAM_COUNTER_ADD(&pThis->StatPktRecvBytes, cbRead);
357 rc = pThis->pIAboveNet->pfnReceive(pThis->pIAboveNet, achBuf, cbRead);
358 AssertRC(rc);
359 }
360 }
361 else
362 {
363 STAM_PROFILE_ADV_STOP(&pThis->StatReceive, a);
364 LogFunc(("RTUdpRead -> %Rrc\n", rc));
365 if (rc == VERR_INVALID_HANDLE)
366 return VERR_UDP_SERVER_STOP;
367 }
368
369 STAM_PROFILE_ADV_STOP(&pThis->StatReceive, a);
370 return VINF_SUCCESS;
371}
372
373
374/* -=-=-=-=- PDMIBASE -=-=-=-=- */
375
376/**
377 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
378 */
379static DECLCALLBACK(void *) drvUDPTunnelQueryInterface(PPDMIBASE pInterface, const char *pszIID)
380{
381 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
382 PDRVUDPTUNNEL pThis = PDMINS_2_DATA(pDrvIns, PDRVUDPTUNNEL);
383
384 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
385 PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKUP, &pThis->INetworkUp);
386 return NULL;
387}
388
389
390/* -=-=-=-=- PDMDRVREG -=-=-=-=- */
391
392/**
393 * Destruct a driver instance.
394 *
395 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
396 * resources can be freed correctly.
397 *
398 * @param pDrvIns The driver instance data.
399 */
400static DECLCALLBACK(void) drvUDPTunnelDestruct(PPDMDRVINS pDrvIns)
401{
402 LogFlowFunc(("\n"));
403 PDRVUDPTUNNEL pThis = PDMINS_2_DATA(pDrvIns, PDRVUDPTUNNEL);
404 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
405
406 ASMAtomicXchgSize(&pThis->fLinkDown, true);
407
408 if (pThis->pszInstance)
409 {
410 RTStrFree(pThis->pszInstance);
411 pThis->pszInstance = NULL;
412 }
413
414 if (pThis->pszDestIP)
415 {
416 MMR3HeapFree(pThis->pszDestIP);
417 pThis->pszDestIP = NULL;
418 }
419
420 if (pThis->pServer)
421 {
422 RTUdpServerDestroy(pThis->pServer);
423 pThis->pServer = NULL;
424 }
425
426 /*
427 * Kill the xmit lock.
428 */
429 if (RTCritSectIsInitialized(&pThis->XmitLock))
430 RTCritSectDelete(&pThis->XmitLock);
431
432#ifdef VBOX_WITH_STATISTICS
433 /*
434 * Deregister statistics.
435 */
436 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatPktSent);
437 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatPktSentBytes);
438 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatPktRecv);
439 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatPktRecvBytes);
440 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatTransmit);
441 PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatReceive);
442#endif /* VBOX_WITH_STATISTICS */
443}
444
445
446/**
447 * Construct a UDP tunnel network transport driver instance.
448 *
449 * @copydoc FNPDMDRVCONSTRUCT
450 */
451static DECLCALLBACK(int) drvUDPTunnelConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
452{
453 RT_NOREF(fFlags);
454 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
455 PDRVUDPTUNNEL pThis = PDMINS_2_DATA(pDrvIns, PDRVUDPTUNNEL);
456
457 /*
458 * Init the static parts.
459 */
460 pThis->pDrvIns = pDrvIns;
461 pThis->pszDestIP = NULL;
462 pThis->pszInstance = NULL;
463
464 /* IBase */
465 pDrvIns->IBase.pfnQueryInterface = drvUDPTunnelQueryInterface;
466 /* INetwork */
467 pThis->INetworkUp.pfnBeginXmit = drvUDPTunnelUp_BeginXmit;
468 pThis->INetworkUp.pfnAllocBuf = drvUDPTunnelUp_AllocBuf;
469 pThis->INetworkUp.pfnFreeBuf = drvUDPTunnelUp_FreeBuf;
470 pThis->INetworkUp.pfnSendBuf = drvUDPTunnelUp_SendBuf;
471 pThis->INetworkUp.pfnEndXmit = drvUDPTunnelUp_EndXmit;
472 pThis->INetworkUp.pfnSetPromiscuousMode = drvUDPTunnelUp_SetPromiscuousMode;
473 pThis->INetworkUp.pfnNotifyLinkChanged = drvUDPTunnelUp_NotifyLinkChanged;
474
475#ifdef VBOX_WITH_STATISTICS
476 /*
477 * Statistics.
478 */
479 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktSent, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of sent packets.", "/Drivers/UDPTunnel%d/Packets/Sent", pDrvIns->iInstance);
480 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktSentBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Number of sent bytes.", "/Drivers/UDPTunnel%d/Bytes/Sent", pDrvIns->iInstance);
481 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktRecv, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of received packets.", "/Drivers/UDPTunnel%d/Packets/Received", pDrvIns->iInstance);
482 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktRecvBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Number of received bytes.", "/Drivers/UDPTunnel%d/Bytes/Received", pDrvIns->iInstance);
483 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatTransmit, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling packet transmit runs.", "/Drivers/UDPTunnel%d/Transmit", pDrvIns->iInstance);
484 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatReceive, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling packet receive runs.", "/Drivers/UDPTunnel%d/Receive", pDrvIns->iInstance);
485#endif /* VBOX_WITH_STATISTICS */
486
487 /*
488 * Validate the config.
489 */
490 if (!CFGMR3AreValuesValid(pCfg, "sport\0dest\0dport"))
491 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES, "");
492
493 /*
494 * Check that no-one is attached to us.
495 */
496 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
497 ("Configuration error: Not possible to attach anything to this driver!\n"),
498 VERR_PDM_DRVINS_NO_ATTACH);
499
500 /*
501 * Query the network port interface.
502 */
503 pThis->pIAboveNet = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMINETWORKDOWN);
504 if (!pThis->pIAboveNet)
505 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE,
506 N_("Configuration error: The above device/driver didn't export the network port interface"));
507
508 /*
509 * Read the configuration.
510 */
511 int rc;
512 char szVal[16];
513 rc = CFGMR3QueryStringDef(pCfg, "sport", szVal, sizeof(szVal), "4444");
514 if (RT_FAILURE(rc))
515 rc = PDMDRV_SET_ERROR(pDrvIns, rc,
516 N_("DrvUDPTunnel: Configuration error: Querying \"sport\" as string failed"));
517 rc = RTStrToUInt16Full(szVal, 0, &pThis->uSrcPort);
518 if (RT_FAILURE(rc))
519 rc = PDMDRV_SET_ERROR(pDrvIns, rc,
520 N_("DrvUDPTunnel: Configuration error: Converting \"sport\" to integer failed"));
521 if (!pThis->uSrcPort)
522 pThis->uSrcPort = 4444;
523
524 rc = CFGMR3QueryStringDef(pCfg, "dport", szVal, sizeof(szVal), "4445");
525 if (RT_FAILURE(rc))
526 rc = PDMDRV_SET_ERROR(pDrvIns, rc,
527 N_("DrvUDPTunnel: Configuration error: Querying \"dport\" as string failed"));
528 rc = RTStrToUInt16Full(szVal, 0, &pThis->uDestPort);
529 if (RT_FAILURE(rc))
530 rc = PDMDRV_SET_ERROR(pDrvIns, rc,
531 N_("DrvUDPTunnel: Configuration error: Converting \"dport\" to integer failed"));
532 if (!pThis->uDestPort)
533 pThis->uDestPort = 4445;
534
535 rc = CFGMR3QueryStringAllocDef(pCfg, "dest", &pThis->pszDestIP, "127.0.0.1");
536 if (RT_FAILURE(rc))
537 rc = PDMDRV_SET_ERROR(pDrvIns, rc,
538 N_("DrvUDPTunnel: Configuration error: Querying \"dest\" as string failed"));
539
540 LogRel(("UDPTunnel#%d: sport=%d;dest=%s;dport=%d\n", pDrvIns->iInstance, pThis->uSrcPort, pThis->pszDestIP, pThis->uDestPort));
541
542 /*
543 * Set up destination address for UDP.
544 */
545 rc = RTSocketParseInetAddress(pThis->pszDestIP, pThis->uDestPort, &pThis->DestAddress);
546 AssertRCReturn(rc, rc);
547
548 /*
549 * Create unique thread name for the UDP receiver.
550 */
551 rc = RTStrAPrintf(&pThis->pszInstance, "UDPTunnel%d", pDrvIns->iInstance);
552 AssertRC(rc);
553
554 /*
555 * Start the UDP receiving thread.
556 */
557 rc = RTUdpServerCreate("", pThis->uSrcPort, RTTHREADTYPE_IO, pThis->pszInstance,
558 drvUDPTunnelReceive, pDrvIns, &pThis->pServer);
559 if (RT_FAILURE(rc))
560 return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_PDM_HIF_OPEN_FAILED, RT_SRC_POS,
561 N_("UDPTunnel: Failed to start the UDP tunnel server"));
562
563 /*
564 * Create the transmit lock.
565 */
566 rc = RTCritSectInit(&pThis->XmitLock);
567 AssertRCReturn(rc, rc);
568
569 return rc;
570}
571
572
573/**
574 * Suspend notification.
575 *
576 * @param pDrvIns The driver instance.
577 */
578static DECLCALLBACK(void) drvUDPTunnelSuspend(PPDMDRVINS pDrvIns)
579{
580 LogFlowFunc(("\n"));
581 PDRVUDPTUNNEL pThis = PDMINS_2_DATA(pDrvIns, PDRVUDPTUNNEL);
582
583 if (pThis->pServer)
584 {
585 RTUdpServerDestroy(pThis->pServer);
586 pThis->pServer = NULL;
587 }
588}
589
590
591/**
592 * Resume notification.
593 *
594 * @param pDrvIns The driver instance.
595 */
596static DECLCALLBACK(void) drvUDPTunnelResume(PPDMDRVINS pDrvIns)
597{
598 LogFlowFunc(("\n"));
599 PDRVUDPTUNNEL pThis = PDMINS_2_DATA(pDrvIns, PDRVUDPTUNNEL);
600
601 int rc = RTUdpServerCreate("", pThis->uSrcPort, RTTHREADTYPE_IO, pThis->pszInstance,
602 drvUDPTunnelReceive, pDrvIns, &pThis->pServer);
603 if (RT_FAILURE(rc))
604 PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_PDM_HIF_OPEN_FAILED, RT_SRC_POS,
605 N_("UDPTunnel: Failed to start the UDP tunnel server"));
606
607}
608
609
610/**
611 * UDP tunnel network transport driver registration record.
612 */
613const PDMDRVREG g_DrvUDPTunnel =
614{
615 /* u32Version */
616 PDM_DRVREG_VERSION,
617 /* szName */
618 "UDPTunnel",
619 /* szRCMod */
620 "",
621 /* szR0Mod */
622 "",
623 /* pszDescription */
624 "UDP Tunnel Network Transport Driver",
625 /* fFlags */
626 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
627 /* fClass. */
628 PDM_DRVREG_CLASS_NETWORK,
629 /* cMaxInstances */
630 ~0U,
631 /* cbInstance */
632 sizeof(DRVUDPTUNNEL),
633 /* pfnConstruct */
634 drvUDPTunnelConstruct,
635 /* pfnDestruct */
636 drvUDPTunnelDestruct,
637 /* pfnRelocate */
638 NULL,
639 /* pfnIOCtl */
640 NULL,
641 /* pfnPowerOn */
642 NULL,
643 /* pfnReset */
644 NULL,
645 /* pfnSuspend */
646 drvUDPTunnelSuspend,
647 /* pfnResume */
648 drvUDPTunnelResume,
649 /* pfnAttach */
650 NULL,
651 /* pfnDetach */
652 NULL,
653 /* pfnPowerOff */
654 NULL,
655 /* pfnSoftReset */
656 NULL,
657 /* u32EndVersion */
658 PDM_DRVREG_VERSION
659};
660
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