VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/DrvNAT.cpp@ 26495

Last change on this file since 26495 was 26495, checked in by vboxsync, 15 years ago

Devices: whitespace cleanup

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 41.4 KB
Line 
1/* $Id: DrvNAT.cpp 26495 2010-02-14 07:59:48Z vboxsync $ */
2/** @file
3 * DrvNAT - NAT network transport driver.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#define LOG_GROUP LOG_GROUP_DRV_NAT
27#define __STDC_LIMIT_MACROS
28#define __STDC_CONSTANT_MACROS
29#include "slirp/libslirp.h"
30#include "slirp/ctl.h"
31#include <VBox/pdmdrv.h>
32#include <VBox/pdmnetifs.h>
33#include <iprt/assert.h>
34#include <iprt/file.h>
35#include <iprt/mem.h>
36#include <iprt/string.h>
37#include <iprt/critsect.h>
38#include <iprt/cidr.h>
39#include <iprt/stream.h>
40#include <iprt/uuid.h>
41
42#include "Builtins.h"
43
44#ifndef RT_OS_WINDOWS
45# include <unistd.h>
46# include <fcntl.h>
47# include <poll.h>
48# include <errno.h>
49#endif
50#ifdef RT_OS_FREEBSD
51# include <netinet/in.h>
52#endif
53#include <iprt/semaphore.h>
54#include <iprt/req.h>
55
56#define COUNTERS_INIT
57#include "counters.h"
58
59
60/*******************************************************************************
61* Defined Constants And Macros *
62*******************************************************************************/
63#define GET_EXTRADATA(pthis, node, name, rc, type, type_name, var) \
64do { \
65 (rc) = CFGMR3Query ## type((node), name, &(var)); \
66 if (RT_FAILURE((rc)) && (rc) != VERR_CFGM_VALUE_NOT_FOUND) \
67 return PDMDrvHlpVMSetError((pthis)->pDrvIns, (rc), RT_SRC_POS, N_("NAT#%d: configuration query for \""name"\" " #type_name " failed"), \
68 (pthis)->pDrvIns->iInstance); \
69} while (0)
70
71#define GET_ED_STRICT(pthis, node, name, rc, type, type_name, var) \
72do { \
73 (rc) = CFGMR3Query ## type((node), name, &(var)); \
74 if (RT_FAILURE((rc))) \
75 return PDMDrvHlpVMSetError((pthis)->pDrvIns, (rc), RT_SRC_POS, N_("NAT#%d: configuration query for \""name"\" " #type_name " failed"), \
76 (pthis)->pDrvIns->iInstance); \
77} while (0)
78
79#define GET_EXTRADATA_N(pthis, node, name, rc, type, type_name, var, var_size) \
80do { \
81 (rc) = CFGMR3Query ## type((node), name, &(var), var_size); \
82 if (RT_FAILURE((rc)) && (rc) != VERR_CFGM_VALUE_NOT_FOUND) \
83 return PDMDrvHlpVMSetError((pthis)->pDrvIns, (rc), RT_SRC_POS, N_("NAT#%d: configuration query for \""name"\" " #type_name " failed"), \
84 (pthis)->pDrvIns->iInstance); \
85} while (0)
86
87#define GET_BOOL(rc, pthis, node, name, var) \
88 GET_EXTRADATA(pthis, node, name, (rc), Bool, bolean, (var))
89#define GET_STRING(rc, pthis, node, name, var, var_size) \
90 GET_EXTRADATA_N(pthis, node, name, (rc), String, string, (var), (var_size))
91#define GET_STRING_ALLOC(rc, pthis, node, name, var) \
92 GET_EXTRADATA(pthis, node, name, (rc), StringAlloc, string, (var))
93#define GET_S32(rc, pthis, node, name, var) \
94 GET_EXTRADATA(pthis, node, name, (rc), S32, int, (var))
95#define GET_S32_STRICT(rc, pthis, node, name, var) \
96 GET_ED_STRICT(pthis, node, name, (rc), S32, int, (var))
97
98
99
100#define DO_GET_IP(rc, node, instance, status, x) \
101do { \
102 char sz##x[32]; \
103 GET_STRING((rc), (node), (instance), #x, sz ## x[0], sizeof(sz ## x)); \
104 if (rc != VERR_CFGM_VALUE_NOT_FOUND) \
105 (status) = inet_aton(sz ## x, &x); \
106} while (0)
107
108#define GETIP_DEF(rc, node, instance, x, def) \
109do \
110{ \
111 int status = 0; \
112 DO_GET_IP((rc), (node), (instance), status, x); \
113 if (status == 0 || rc == VERR_CFGM_VALUE_NOT_FOUND) \
114 x.s_addr = def; \
115} while (0)
116
117/*******************************************************************************
118* Structures and Typedefs *
119*******************************************************************************/
120/**
121 * NAT network transport driver instance data.
122 *
123 * @implements PDMINETWORKUP
124 */
125typedef struct DRVNAT
126{
127 /** The network interface. */
128 PDMINETWORKUP INetworkUp;
129 /** The port we're attached to. */
130 PPDMINETWORKDOWN pIAboveNet;
131 /** The network config of the port we're attached to. */
132 PPDMINETWORKCONFIG pIAboveConfig;
133 /** Pointer to the driver instance. */
134 PPDMDRVINS pDrvIns;
135 /** Link state */
136 PDMNETWORKLINKSTATE enmLinkState;
137 /** NAT state for this instance. */
138 PNATState pNATState;
139 /** TFTP directory prefix. */
140 char *pszTFTPPrefix;
141 /** Boot file name to provide in the DHCP server response. */
142 char *pszBootFile;
143 /** tftp server name to provide in the DHCP server response. */
144 char *pszNextServer;
145 /* polling thread */
146 PPDMTHREAD pSlirpThread;
147 /** Queue for NAT-thread-external events. */
148 PRTREQQUEUE pSlirpReqQueue;
149 /** The guest IP for port-forwarding. */
150 uint32_t GuestIP;
151 uint32_t alignment1;
152
153#ifdef VBOX_WITH_SLIRP_MT
154 PPDMTHREAD pGuestThread;
155#endif
156#ifndef RT_OS_WINDOWS
157 /** The write end of the control pipe. */
158 RTFILE PipeWrite;
159 /** The read end of the control pipe. */
160 RTFILE PipeRead;
161# if HC_ARCH_BITS == 32
162 /** Alignment padding. */
163 //uint32_t alignment2;
164# endif
165#else
166 /** for external notification */
167 HANDLE hWakeupEvent;
168#endif
169
170#define DRV_PROFILE_COUNTER(name, dsc) STAMPROFILE Stat ## name
171#define DRV_COUNTING_COUNTER(name, dsc) STAMCOUNTER Stat ## name
172#include "counters.h"
173 /** thread delivering packets for receiving by the guest */
174 PPDMTHREAD pRecvThread;
175 /** thread delivering urg packets for receiving by the guest */
176 PPDMTHREAD pUrgRecvThread;
177 /** event to wakeup the guest receive thread */
178 RTSEMEVENT EventRecv;
179 /** event to wakeup the guest urgent receive thread */
180 RTSEMEVENT EventUrgRecv;
181 /** Receive Req queue (deliver packets to the guest) */
182 PRTREQQUEUE pRecvReqQueue;
183 /** Receive Urgent Req queue (deliver packets to the guest) */
184 PRTREQQUEUE pUrgRecvReqQueue;
185
186 /* makes access to device func RecvAvail and Recv atomical */
187 RTCRITSECT csDevAccess;
188 volatile uint32_t cUrgPkt;
189 volatile uint32_t cPkt;
190 PTMTIMERR3 pTmrSlow;
191 PTMTIMERR3 pTmrFast;
192} DRVNAT;
193AssertCompileMemberAlignment(DRVNAT, StatNATRecvWakeups, 8);
194/** Pointer the NAT driver instance data. */
195typedef DRVNAT *PDRVNAT;
196
197/**
198 * NAT queue item.
199 */
200typedef struct DRVNATQUEUITEM
201{
202 /** The core part owned by the queue manager. */
203 PDMQUEUEITEMCORE Core;
204 /** The buffer for output to guest. */
205 const uint8_t *pu8Buf;
206 /* size of buffer */
207 size_t cb;
208 void *mbuf;
209} DRVNATQUEUITEM;
210/** Pointer to a NAT queue item. */
211typedef DRVNATQUEUITEM *PDRVNATQUEUITEM;
212
213
214static void drvNATNotifyNATThread(PDRVNAT pThis);
215static DECLCALLBACK(void) drvNATSlowTimer(PPDMDRVINS pDrvIns, PTMTIMER pTimer, void *pvUser);
216static DECLCALLBACK(void) drvNATFast(PPDMDRVINS pDrvIns, PTMTIMER pTimer, void *pvUser);
217
218
219/** Converts a pointer to NAT::INetworkUp to a PRDVNAT. */
220#define PDMINETWORKUP_2_DRVNAT(pInterface) ( (PDRVNAT)((uintptr_t)pInterface - RT_OFFSETOF(DRVNAT, INetworkUp)) )
221
222static DECLCALLBACK(void) drvNATSlowTimer(PPDMDRVINS pDrvIns, PTMTIMER pTimer, void *pvUser)
223{
224 Assert(pvUser);
225 PDRVNAT pThis = (PDRVNAT)pvUser;
226 drvNATNotifyNATThread(pThis);
227}
228
229static DECLCALLBACK(void) drvNATFastTimer(PPDMDRVINS pDrvIns, PTMTIMER pTimer, void *pvUser)
230{
231 Assert(pvUser);
232 PDRVNAT pThis = (PDRVNAT)pvUser;
233 drvNATNotifyNATThread(pThis);
234}
235
236
237static DECLCALLBACK(int) drvNATRecv(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
238{
239 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
240
241 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
242 return VINF_SUCCESS;
243
244 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
245 {
246 RTReqProcess(pThis->pRecvReqQueue, 0);
247 if (ASMAtomicReadU32(&pThis->cPkt) == 0)
248 RTSemEventWait(pThis->EventRecv, RT_INDEFINITE_WAIT);
249 }
250 return VINF_SUCCESS;
251}
252
253
254static DECLCALLBACK(int) drvNATRecvWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
255{
256 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
257 int rc;
258 rc = RTSemEventSignal(pThis->EventRecv);
259
260 STAM_COUNTER_INC(&pThis->StatNATRecvWakeups);
261 return VINF_SUCCESS;
262}
263
264static DECLCALLBACK(int) drvNATUrgRecv(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
265{
266 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
267
268 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
269 return VINF_SUCCESS;
270
271 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
272 {
273 RTReqProcess(pThis->pUrgRecvReqQueue, 0);
274 if (ASMAtomicReadU32(&pThis->cUrgPkt) == 0)
275 {
276 int rc = RTSemEventWait(pThis->EventUrgRecv, RT_INDEFINITE_WAIT);
277 AssertRC(rc);
278 }
279 }
280 return VINF_SUCCESS;
281}
282static DECLCALLBACK(int) drvNATUrgRecvWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
283{
284 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
285 int rc = RTSemEventSignal(pThis->EventUrgRecv);
286 AssertRC(rc);
287
288 return VINF_SUCCESS;
289}
290
291static DECLCALLBACK(void) drvNATUrgRecvWorker(PDRVNAT pThis, uint8_t *pu8Buf, int cb, void *pvArg)
292{
293 int rc = RTCritSectEnter(&pThis->csDevAccess);
294 AssertRC(rc);
295 rc = pThis->pIAboveNet->pfnWaitReceiveAvail(pThis->pIAboveNet, RT_INDEFINITE_WAIT);
296 if (RT_SUCCESS(rc))
297 {
298 rc = pThis->pIAboveNet->pfnReceive(pThis->pIAboveNet, pu8Buf, cb);
299 AssertRC(rc);
300 }
301 else if ( RT_FAILURE(rc)
302 && ( rc == VERR_TIMEOUT
303 && rc == VERR_INTERRUPTED))
304 {
305 AssertRC(rc);
306 }
307
308 rc = RTCritSectLeave(&pThis->csDevAccess);
309 AssertRC(rc);
310
311 slirp_ext_m_free(pThis->pNATState, pvArg);
312#ifdef VBOX_WITH_SLIRP_BSD_MBUF
313 RTMemFree(pu8Buf);
314#endif
315 if (ASMAtomicDecU32(&pThis->cUrgPkt) == 0)
316 {
317 drvNATRecvWakeup(pThis->pDrvIns, pThis->pRecvThread);
318 drvNATNotifyNATThread(pThis);
319 }
320}
321
322
323static DECLCALLBACK(void) drvNATRecvWorker(PDRVNAT pThis, uint8_t *pu8Buf, int cb, void *pvArg)
324{
325 int rc;
326 STAM_PROFILE_START(&pThis->StatNATRecv, a);
327
328 STAM_PROFILE_START(&pThis->StatNATRecvWait, b);
329
330 while(ASMAtomicReadU32(&pThis->cUrgPkt) != 0)
331 {
332 rc = RTSemEventWait(pThis->EventRecv, RT_INDEFINITE_WAIT);
333 if ( RT_FAILURE(rc)
334 && ( rc == VERR_TIMEOUT
335 || rc == VERR_INTERRUPTED))
336 goto done_unlocked;
337 }
338
339 rc = RTCritSectEnter(&pThis->csDevAccess);
340 AssertRC(rc);
341
342 rc = pThis->pIAboveNet->pfnWaitReceiveAvail(pThis->pIAboveNet, RT_INDEFINITE_WAIT);
343 if (RT_SUCCESS(rc))
344 {
345 rc = pThis->pIAboveNet->pfnReceive(pThis->pIAboveNet, pu8Buf, cb);
346 AssertRC(rc);
347 }
348 else if ( RT_FAILURE(rc)
349 && ( rc != VERR_TIMEOUT
350 && rc != VERR_INTERRUPTED))
351 {
352 AssertRC(rc);
353 }
354
355 rc = RTCritSectLeave(&pThis->csDevAccess);
356 AssertRC(rc);
357
358done_unlocked:
359 slirp_ext_m_free(pThis->pNATState, pvArg);
360#ifdef VBOX_WITH_SLIRP_BSD_MBUF
361 RTMemFree(pu8Buf);
362#endif
363 ASMAtomicDecU32(&pThis->cPkt);
364
365 drvNATNotifyNATThread(pThis);
366
367 STAM_PROFILE_STOP(&pThis->StatNATRecvWait, b);
368 STAM_PROFILE_STOP(&pThis->StatNATRecv, a);
369}
370
371/**
372 * Worker function for drvNATSend().
373 * @thread "NAT" thread.
374 */
375static void drvNATSendWorker(PDRVNAT pThis, void *pvBuf, size_t cb)
376{
377 Assert(pThis->enmLinkState == PDMNETWORKLINKSTATE_UP);
378 if (pThis->enmLinkState == PDMNETWORKLINKSTATE_UP)
379 slirp_input(pThis->pNATState, (uint8_t *)pvBuf);
380}
381
382
383/**
384 * @interface_method_impl{PDMINETWORKUP,pfnSendDeprecated}
385 */
386static DECLCALLBACK(int) drvNATSendDeprecated(PPDMINETWORKUP pInterface, const void *pvBuf, size_t cb)
387{
388 PDRVNAT pThis = PDMINETWORKUP_2_DRVNAT(pInterface);
389
390 LogFlow(("drvNATSend: pvBuf=%p cb=%#x\n", pvBuf, cb));
391 Log2(("drvNATSend: pvBuf=%p cb=%#x\n%.*Rhxd\n", pvBuf, cb, cb, pvBuf));
392
393 PRTREQ pReq = NULL;
394 int rc;
395 void *buf;
396
397 /* don't queue new requests when the NAT thread is about to stop */
398 if (pThis->pSlirpThread->enmState != PDMTHREADSTATE_RUNNING)
399 return VINF_SUCCESS;
400
401#ifndef VBOX_WITH_SLIRP_MT
402 rc = RTReqAlloc(pThis->pSlirpReqQueue, &pReq, RTREQTYPE_INTERNAL);
403#else
404 rc = RTReqAlloc((PRTREQQUEUE)slirp_get_queue(pThis->pNATState), &pReq, RTREQTYPE_INTERNAL);
405#endif
406 AssertRC(rc);
407
408 /* @todo: Here we should get mbuf instead temporal buffer */
409#ifndef VBOX_WITH_SLIRP_BSD_MBUF
410 void *pvmBuf = slirp_ext_m_get(pThis->pNATState);
411 Assert(pvmBuf);
412 slirp_ext_m_append(pThis->pNATState, pvmBuf, (uint8_t *)pvBuf, cb);
413#else
414 void *pvmBuf = slirp_ext_m_get(pThis->pNATState, (uint8_t *)pvBuf, cb);
415 Assert(pvmBuf);
416#endif
417
418 pReq->u.Internal.pfn = (PFNRT)drvNATSendWorker;
419 pReq->u.Internal.cArgs = 2;
420 pReq->u.Internal.aArgs[0] = (uintptr_t)pThis;
421 pReq->u.Internal.aArgs[1] = (uintptr_t)pvmBuf;
422 pReq->fFlags = RTREQFLAGS_VOID|RTREQFLAGS_NO_WAIT;
423
424 rc = RTReqQueue(pReq, 0); /* don't wait, we have to wakeup the NAT thread fist */
425 AssertRC(rc);
426 drvNATNotifyNATThread(pThis);
427 LogFlow(("drvNATSend: end\n"));
428 return VINF_SUCCESS;
429}
430
431
432/**
433 * Get the NAT thread out of poll/WSAWaitForMultipleEvents
434 */
435static void drvNATNotifyNATThread(PDRVNAT pThis)
436{
437 int rc;
438#ifndef RT_OS_WINDOWS
439 /* kick select() */
440 rc = RTFileWrite(pThis->PipeWrite, "", 1, NULL);
441#else
442 /* kick WSAWaitForMultipleEvents */
443 rc = WSASetEvent(pThis->hWakeupEvent);
444#endif
445 AssertRC(rc);
446}
447
448
449/**
450 * @interface_method_impl{PDMINETWORKUP,pfnSetPromiscuousMode}
451 */
452static DECLCALLBACK(void) drvNATSetPromiscuousMode(PPDMINETWORKUP pInterface, bool fPromiscuous)
453{
454 LogFlow(("drvNATSetPromiscuousMode: fPromiscuous=%d\n", fPromiscuous));
455 /* nothing to do */
456}
457
458/**
459 * Worker function for drvNATNotifyLinkChanged().
460 * @thread "NAT" thread.
461 */
462static void drvNATNotifyLinkChangedWorker(PDRVNAT pThis, PDMNETWORKLINKSTATE enmLinkState)
463{
464 pThis->enmLinkState = enmLinkState;
465
466 switch (enmLinkState)
467 {
468 case PDMNETWORKLINKSTATE_UP:
469 LogRel(("NAT: link up\n"));
470 slirp_link_up(pThis->pNATState);
471 break;
472
473 case PDMNETWORKLINKSTATE_DOWN:
474 case PDMNETWORKLINKSTATE_DOWN_RESUME:
475 LogRel(("NAT: link down\n"));
476 slirp_link_down(pThis->pNATState);
477 break;
478
479 default:
480 AssertMsgFailed(("drvNATNotifyLinkChanged: unexpected link state %d\n", enmLinkState));
481 }
482}
483
484
485/**
486 * Notification on link status changes.
487 *
488 * @param pInterface Pointer to the interface structure containing the called function pointer.
489 * @param enmLinkState The new link state.
490 * @thread EMT
491 */
492static DECLCALLBACK(void) drvNATNotifyLinkChanged(PPDMINETWORKUP pInterface, PDMNETWORKLINKSTATE enmLinkState)
493{
494 PDRVNAT pThis = PDMINETWORKUP_2_DRVNAT(pInterface);
495
496 LogFlow(("drvNATNotifyLinkChanged: enmLinkState=%d\n", enmLinkState));
497
498 PRTREQ pReq = NULL;
499
500 /* don't queue new requests when the NAT thread is about to stop */
501 if (pThis->pSlirpThread->enmState != PDMTHREADSTATE_RUNNING)
502 return;
503
504 int rc = RTReqAlloc(pThis->pSlirpReqQueue, &pReq, RTREQTYPE_INTERNAL);
505 AssertRC(rc);
506 pReq->u.Internal.pfn = (PFNRT)drvNATNotifyLinkChangedWorker;
507 pReq->u.Internal.cArgs = 2;
508 pReq->u.Internal.aArgs[0] = (uintptr_t)pThis;
509 pReq->u.Internal.aArgs[1] = (uintptr_t)enmLinkState;
510 pReq->fFlags = RTREQFLAGS_VOID;
511 rc = RTReqQueue(pReq, 0); /* don't wait, we have to wakeup the NAT thread fist */
512 if (RT_LIKELY(rc == VERR_TIMEOUT))
513 {
514 drvNATNotifyNATThread(pThis);
515 rc = RTReqWait(pReq, RT_INDEFINITE_WAIT);
516 AssertRC(rc);
517 }
518 else
519 AssertRC(rc);
520 RTReqFree(pReq);
521}
522
523/**
524 * NAT thread handling the slirp stuff. The slirp implementation is single-threaded
525 * so we execute this enginre in a dedicated thread. We take care that this thread
526 * does not become the bottleneck: If the guest wants to send, a request is enqueued
527 * into the pSlirpReqQueue and handled asynchronously by this thread. If this thread
528 * wants to deliver packets to the guest, it enqueues a request into pRecvReqQueue
529 * which is later handled by the Recv thread.
530 */
531static DECLCALLBACK(int) drvNATAsyncIoThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
532{
533 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
534 int nFDs = -1;
535 int ms;
536#ifdef RT_OS_WINDOWS
537 DWORD event;
538 HANDLE *phEvents;
539 unsigned int cBreak = 0;
540#else /* RT_OS_WINDOWS */
541 struct pollfd *polls = NULL;
542 unsigned int cPollNegRet = 0;
543#endif /* !RT_OS_WINDOWS */
544
545 LogFlow(("drvNATAsyncIoThread: pThis=%p\n", pThis));
546
547 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
548 return VINF_SUCCESS;
549
550#ifdef RT_OS_WINDOWS
551 phEvents = slirp_get_events(pThis->pNATState);
552#endif /* RT_OS_WINDOWS */
553
554 /*
555 * Polling loop.
556 */
557 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
558 {
559 nFDs = -1;
560 /*
561 * To prevent concurent execution of sending/receving threads
562 */
563#ifndef RT_OS_WINDOWS
564 nFDs = slirp_get_nsock(pThis->pNATState);
565 polls = NULL;
566 /* allocation for all sockets + Management pipe */
567 polls = (struct pollfd *)RTMemAlloc((1 + nFDs) * sizeof(struct pollfd) + sizeof(uint32_t));
568 if (polls == NULL)
569 return VERR_NO_MEMORY;
570
571 /* don't pass the managemant pipe */
572 slirp_select_fill(pThis->pNATState, &nFDs, &polls[1]);
573#if 0
574 ms = slirp_get_timeout_ms(pThis->pNATState);
575#else
576 ms = 0;
577#endif
578
579 polls[0].fd = pThis->PipeRead;
580 /* POLLRDBAND usually doesn't used on Linux but seems used on Solaris */
581 polls[0].events = POLLRDNORM|POLLPRI|POLLRDBAND;
582 polls[0].revents = 0;
583
584 int cChangedFDs = poll(polls, nFDs + 1, ms ? ms : -1);
585 if (cChangedFDs < 0)
586 {
587 if (errno == EINTR)
588 {
589 Log2(("NAT: signal was caught while sleep on poll\n"));
590 /* No error, just process all outstanding requests but don't wait */
591 cChangedFDs = 0;
592 }
593 else if (cPollNegRet++ > 128)
594 {
595 LogRel(("NAT:Poll returns (%s) suppressed %d\n", strerror(errno), cPollNegRet));
596 cPollNegRet = 0;
597 }
598 }
599
600 if (cChangedFDs >= 0)
601 {
602 slirp_select_poll(pThis->pNATState, &polls[1], nFDs);
603 if (polls[0].revents & (POLLRDNORM|POLLPRI|POLLRDBAND))
604 {
605 /* drain the pipe */
606 char ch[1];
607 size_t cbRead;
608 int counter = 0;
609 /*
610 * drvNATSend decoupled so we don't know how many times
611 * device's thread sends before we've entered multiplex,
612 * so to avoid false alarm drain pipe here to the very end
613 *
614 * @todo: Probably we should counter drvNATSend to count how
615 * deep pipe has been filed before drain.
616 *
617 * XXX:Make it reading exactly we need to drain the pipe.
618 */
619 RTFileRead(pThis->PipeRead, &ch, 1, &cbRead);
620 }
621 }
622 /* process _all_ outstanding requests but don't wait */
623 RTReqProcess(pThis->pSlirpReqQueue, 0);
624 RTMemFree(polls);
625#else /* RT_OS_WINDOWS */
626 slirp_select_fill(pThis->pNATState, &nFDs);
627#if 0
628 ms = slirp_get_timeout_ms(pThis->pNATState);
629#else
630 ms = 0;
631#endif
632 struct timeval tv = { 0, ms*1000 };
633 event = WSAWaitForMultipleEvents(nFDs, phEvents, FALSE, ms ? ms : WSA_INFINITE, FALSE);
634 if ( (event < WSA_WAIT_EVENT_0 || event > WSA_WAIT_EVENT_0 + nFDs - 1)
635 && event != WSA_WAIT_TIMEOUT)
636 {
637 int error = WSAGetLastError();
638 LogRel(("NAT: WSAWaitForMultipleEvents returned %d (error %d)\n", event, error));
639 RTAssertPanic();
640 }
641
642 if (event == WSA_WAIT_TIMEOUT)
643 {
644 /* only check for slow/fast timers */
645 slirp_select_poll(pThis->pNATState, /* fTimeout=*/true, /*fIcmp=*/false);
646 continue;
647 }
648 /* poll the sockets in any case */
649 Log2(("%s: poll\n", __FUNCTION__));
650 slirp_select_poll(pThis->pNATState, /* fTimeout=*/false, /* fIcmp=*/(event == WSA_WAIT_EVENT_0));
651 /* process _all_ outstanding requests but don't wait */
652 RTReqProcess(pThis->pSlirpReqQueue, 0);
653# ifdef VBOX_NAT_DELAY_HACK
654 if (cBreak++ > 128)
655 {
656 cBreak = 0;
657 RTThreadSleep(2);
658 }
659# endif
660#endif /* RT_OS_WINDOWS */
661 }
662
663 return VINF_SUCCESS;
664}
665
666
667/**
668 * Unblock the send thread so it can respond to a state change.
669 *
670 * @returns VBox status code.
671 * @param pDevIns The pcnet device instance.
672 * @param pThread The send thread.
673 */
674static DECLCALLBACK(int) drvNATAsyncIoWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
675{
676 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
677
678 drvNATNotifyNATThread(pThis);
679 return VINF_SUCCESS;
680}
681
682#ifdef VBOX_WITH_SLIRP_MT
683
684static DECLCALLBACK(int) drvNATAsyncIoGuest(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
685{
686 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
687
688 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
689 return VINF_SUCCESS;
690
691 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
692 slirp_process_queue(pThis->pNATState);
693
694 return VINF_SUCCESS;
695}
696
697
698static DECLCALLBACK(int) drvNATAsyncIoGuestWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
699{
700 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
701
702 return VINF_SUCCESS;
703}
704
705#endif /* VBOX_WITH_SLIRP_MT */
706
707void slirp_arm_fast_timer(void *pvUser)
708{
709 PDRVNAT pThis = (PDRVNAT)pvUser;
710 Assert(pThis);
711 TMTimerSetMillies(pThis->pTmrFast, 2);
712}
713
714void slirp_arm_slow_timer(void *pvUser)
715{
716 PDRVNAT pThis = (PDRVNAT)pvUser;
717 Assert(pThis);
718 TMTimerSetMillies(pThis->pTmrSlow, 500);
719}
720
721/**
722 * Function called by slirp to check if it's possible to feed incoming data to the network port.
723 * @returns 1 if possible.
724 * @returns 0 if not possible.
725 */
726int slirp_can_output(void *pvUser)
727{
728 return 1;
729}
730
731void slirp_push_recv_thread(void *pvUser)
732{
733 PDRVNAT pThis = (PDRVNAT)pvUser;
734 Assert(pThis);
735 drvNATUrgRecvWakeup(pThis->pDrvIns, pThis->pUrgRecvThread);
736}
737
738void slirp_urg_output(void *pvUser, void *pvArg, const uint8_t *pu8Buf, int cb)
739{
740 PDRVNAT pThis = (PDRVNAT)pvUser;
741 Assert(pThis);
742
743 PRTREQ pReq = NULL;
744
745 /* don't queue new requests when the NAT thread is about to stop */
746 if (pThis->pSlirpThread->enmState != PDMTHREADSTATE_RUNNING)
747 return;
748
749 int rc = RTReqAlloc(pThis->pUrgRecvReqQueue, &pReq, RTREQTYPE_INTERNAL);
750 AssertRC(rc);
751 ASMAtomicIncU32(&pThis->cUrgPkt);
752 pReq->u.Internal.pfn = (PFNRT)drvNATUrgRecvWorker;
753 pReq->u.Internal.cArgs = 4;
754 pReq->u.Internal.aArgs[0] = (uintptr_t)pThis;
755 pReq->u.Internal.aArgs[1] = (uintptr_t)pu8Buf;
756 pReq->u.Internal.aArgs[2] = (uintptr_t)cb;
757 pReq->u.Internal.aArgs[3] = (uintptr_t)pvArg;
758 pReq->fFlags = RTREQFLAGS_VOID|RTREQFLAGS_NO_WAIT;
759 rc = RTReqQueue(pReq, 0);
760 AssertRC(rc);
761 drvNATUrgRecvWakeup(pThis->pDrvIns, pThis->pUrgRecvThread);
762}
763
764/**
765 * Function called by slirp to feed incoming data to the network port.
766 */
767void slirp_output(void *pvUser, void *pvArg, const uint8_t *pu8Buf, int cb)
768{
769 PDRVNAT pThis = (PDRVNAT)pvUser;
770 Assert(pThis);
771
772 LogFlow(("slirp_output BEGIN %x %d\n", pu8Buf, cb));
773 Log2(("slirp_output: pu8Buf=%p cb=%#x (pThis=%p)\n%.*Rhxd\n", pu8Buf, cb, pThis, cb, pu8Buf));
774
775 PRTREQ pReq = NULL;
776
777 /* don't queue new requests when the NAT thread is about to stop */
778 if (pThis->pSlirpThread->enmState != PDMTHREADSTATE_RUNNING)
779 return;
780
781 int rc = RTReqAlloc(pThis->pRecvReqQueue, &pReq, RTREQTYPE_INTERNAL);
782 AssertRC(rc);
783 ASMAtomicIncU32(&pThis->cPkt);
784 pReq->u.Internal.pfn = (PFNRT)drvNATRecvWorker;
785 pReq->u.Internal.cArgs = 4;
786 pReq->u.Internal.aArgs[0] = (uintptr_t)pThis;
787 pReq->u.Internal.aArgs[1] = (uintptr_t)pu8Buf;
788 pReq->u.Internal.aArgs[2] = (uintptr_t)cb;
789 pReq->u.Internal.aArgs[3] = (uintptr_t)pvArg;
790 pReq->fFlags = RTREQFLAGS_VOID|RTREQFLAGS_NO_WAIT;
791 rc = RTReqQueue(pReq, 0);
792 AssertRC(rc);
793 drvNATRecvWakeup(pThis->pDrvIns, pThis->pRecvThread);
794 STAM_COUNTER_INC(&pThis->StatQueuePktSent);
795}
796
797
798/**
799 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
800 */
801static DECLCALLBACK(void *) drvNATQueryInterface(PPDMIBASE pInterface, const char *pszIID)
802{
803 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
804 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
805
806 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
807 PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKUP, &pThis->INetworkUp);
808 return NULL;
809}
810
811
812/**
813 * Get the MAC address into the slirp stack.
814 *
815 * Called by drvNATLoadDone and drvNATPowerOn.
816 */
817static void drvNATSetMac(PDRVNAT pThis)
818{
819 if (pThis->pIAboveConfig)
820 {
821 RTMAC Mac;
822 pThis->pIAboveConfig->pfnGetMac(pThis->pIAboveConfig, &Mac);
823 /* Re-activate the port forwarding. If */
824 slirp_set_ethaddr_and_activate_port_forwarding(pThis->pNATState, Mac.au8, pThis->GuestIP);
825 }
826}
827
828
829/**
830 * After loading we have to pass the MAC address of the ethernet device to the slirp stack.
831 * Otherwise the guest is not reachable until it performs a DHCP request or an ARP request
832 * (usually done during guest boot).
833 */
834static DECLCALLBACK(int) drvNATLoadDone(PPDMDRVINS pDrvIns, PSSMHANDLE pSSMHandle)
835{
836 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
837 drvNATSetMac(pThis);
838 return VINF_SUCCESS;
839}
840
841
842/**
843 * Some guests might not use DHCP to retrieve an IP but use a static IP.
844 */
845static DECLCALLBACK(void) drvNATPowerOn(PPDMDRVINS pDrvIns)
846{
847 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
848 drvNATSetMac(pThis);
849}
850
851
852/**
853 * Sets up the redirectors.
854 *
855 * @returns VBox status code.
856 * @param pCfg The configuration handle.
857 */
858static int drvNATConstructRedir(unsigned iInstance, PDRVNAT pThis, PCFGMNODE pCfg, RTIPV4ADDR Network)
859{
860 RTMAC Mac;
861 memset(&Mac, 0, sizeof(RTMAC)); /*can't get MAC here */
862 /*
863 * Enumerate redirections.
864 */
865 for (PCFGMNODE pNode = CFGMR3GetFirstChild(pCfg); pNode; pNode = CFGMR3GetNextChild(pNode))
866 {
867 /*
868 * Validate the port forwarding config.
869 */
870 if (!CFGMR3AreValuesValid(pNode, "Protocol\0UDP\0HostPort\0GuestPort\0GuestIP\0BindIP\0"))
871 return PDMDRV_SET_ERROR(pThis->pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES, N_("Unknown configuration in port forwarding"));
872
873 /* protocol type */
874 bool fUDP;
875 char szProtocol[32];
876 int rc;
877 GET_STRING(rc, pThis, pNode, "Protocol", szProtocol[0], sizeof(szProtocol));
878 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
879 {
880 fUDP = false;
881 GET_BOOL(rc, pThis, pNode, "UDP", fUDP);
882 }
883 else if (RT_SUCCESS(rc))
884 {
885 if (!RTStrICmp(szProtocol, "TCP"))
886 fUDP = false;
887 else if (!RTStrICmp(szProtocol, "UDP"))
888 fUDP = true;
889 else
890 return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_INVALID_PARAMETER, RT_SRC_POS,
891 N_("NAT#%d: Invalid configuration value for \"Protocol\": \"%s\""),
892 iInstance, szProtocol);
893 }
894 /* host port */
895 int32_t iHostPort;
896 GET_S32_STRICT(rc, pThis, pNode, "HostPort", iHostPort);
897
898 /* guest port */
899 int32_t iGuestPort;
900 GET_S32_STRICT(rc, pThis, pNode, "GuestPort", iGuestPort);
901
902 /* guest address */
903 struct in_addr GuestIP;
904 /* @todo (vvl) use CTL_* */
905 GETIP_DEF(rc, pThis, pNode, GuestIP, htonl(Network | CTL_GUEST));
906
907 /* Store the guest IP for re-establishing the port-forwarding rules. Note that GuestIP
908 * is not documented. Without */
909 if (pThis->GuestIP == INADDR_ANY)
910 pThis->GuestIP = GuestIP.s_addr;
911
912 /*
913 * Call slirp about it.
914 */
915 struct in_addr BindIP;
916 GETIP_DEF(rc, pThis, pNode, BindIP, INADDR_ANY);
917 if (slirp_redir(pThis->pNATState, fUDP, BindIP, iHostPort, GuestIP, iGuestPort, Mac.au8) < 0)
918 return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_NAT_REDIR_SETUP, RT_SRC_POS,
919 N_("NAT#%d: configuration error: failed to set up "
920 "redirection of %d to %d. Probably a conflict with "
921 "existing services or other rules"), iInstance, iHostPort,
922 iGuestPort);
923 } /* for each redir rule */
924
925 return VINF_SUCCESS;
926}
927
928
929/**
930 * Destruct a driver instance.
931 *
932 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
933 * resources can be freed correctly.
934 *
935 * @param pDrvIns The driver instance data.
936 */
937static DECLCALLBACK(void) drvNATDestruct(PPDMDRVINS pDrvIns)
938{
939 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
940 LogFlow(("drvNATDestruct:\n"));
941 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
942
943 slirp_term(pThis->pNATState);
944 slirp_deregister_statistics(pThis->pNATState, pDrvIns);
945 pThis->pNATState = NULL;
946#ifdef VBOX_WITH_STATISTICS
947# define DRV_PROFILE_COUNTER(name, dsc) DEREGISTER_COUNTER(name, pThis)
948# define DRV_COUNTING_COUNTER(name, dsc) DEREGISTER_COUNTER(name, pThis)
949# include "counters.h"
950#endif
951}
952
953
954/**
955 * Construct a NAT network transport driver instance.
956 *
957 * @copydoc FNPDMDRVCONSTRUCT
958 */
959static DECLCALLBACK(int) drvNATConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
960{
961 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
962 LogFlow(("drvNATConstruct:\n"));
963 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
964
965 /*
966 * Validate the config.
967 */
968 if (!CFGMR3AreValuesValid(pCfg,
969 "PassDomain\0TFTPPrefix\0BootFile\0Network"
970 "\0NextServer\0DNSProxy\0BindIP\0UseHostResolver\0"
971 "SlirpMTU\0"
972 "SockRcv\0SockSnd\0TcpRcv\0TcpSnd\0"))
973 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES,
974 N_("Unknown NAT configuration option, only supports PassDomain,"
975 " TFTPPrefix, BootFile and Network"));
976
977 /*
978 * Init the static parts.
979 */
980 pThis->pDrvIns = pDrvIns;
981 pThis->pNATState = NULL;
982 pThis->pszTFTPPrefix = NULL;
983 pThis->pszBootFile = NULL;
984 pThis->pszNextServer = NULL;
985 /* IBase */
986 pDrvIns->IBase.pfnQueryInterface = drvNATQueryInterface;
987 /* INetwork */
988/** @todo implement the new INetworkUp interfaces. */
989 pThis->INetworkUp.pfnSendDeprecated = drvNATSendDeprecated;
990 pThis->INetworkUp.pfnSetPromiscuousMode = drvNATSetPromiscuousMode;
991 pThis->INetworkUp.pfnNotifyLinkChanged = drvNATNotifyLinkChanged;
992
993 /*
994 * Get the configuration settings.
995 */
996 int rc;
997 bool fPassDomain = true;
998 GET_BOOL(rc, pThis, pCfg, "PassDomain", fPassDomain);
999
1000 GET_STRING_ALLOC(rc, pThis, pCfg, "TFTPPrefix", pThis->pszTFTPPrefix);
1001 GET_STRING_ALLOC(rc, pThis, pCfg, "BootFile", pThis->pszBootFile);
1002 GET_STRING_ALLOC(rc, pThis, pCfg, "NextServer", pThis->pszNextServer);
1003
1004 int fDNSProxy = 0;
1005 GET_S32(rc, pThis, pCfg, "DNSProxy", fDNSProxy);
1006 int fUseHostResolver = 0;
1007 GET_S32(rc, pThis, pCfg, "UseHostResolver", fUseHostResolver);
1008#ifdef VBOX_WITH_SLIRP_BSD_MBUF
1009 int MTU = 1500;
1010 GET_S32(rc, pThis, pCfg, "SlirpMTU", MTU);
1011#endif
1012
1013 /*
1014 * Query the network port interface.
1015 */
1016 pThis->pIAboveNet = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMINETWORKDOWN);
1017 if (!pThis->pIAboveNet)
1018 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE,
1019 N_("Configuration error: the above device/driver didn't "
1020 "export the network port interface"));
1021 pThis->pIAboveConfig = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMINETWORKCONFIG);
1022 if (!pThis->pIAboveConfig)
1023 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE,
1024 N_("Configuration error: the above device/driver didn't "
1025 "export the network config interface"));
1026
1027 /* Generate a network address for this network card. */
1028 char szNetwork[32]; /* xxx.xxx.xxx.xxx/yy */
1029 GET_STRING(rc, pThis, pCfg, "Network", szNetwork[0], sizeof(szNetwork));
1030 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1031 RTStrPrintf(szNetwork, sizeof(szNetwork), "10.0.%d.0/24", pDrvIns->iInstance + 2);
1032
1033 RTIPV4ADDR Network;
1034 RTIPV4ADDR Netmask;
1035 rc = RTCidrStrToIPv4(szNetwork, &Network, &Netmask);
1036 if (RT_FAILURE(rc))
1037 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: Configuration error: "
1038 "network '%s' describes not a valid IPv4 network"),
1039 pDrvIns->iInstance, szNetwork);
1040
1041 char szNetAddr[16];
1042 RTStrPrintf(szNetAddr, sizeof(szNetAddr), "%d.%d.%d.%d",
1043 (Network & 0xFF000000) >> 24, (Network & 0xFF0000) >> 16,
1044 (Network & 0xFF00) >> 8, Network & 0xFF);
1045
1046 /*
1047 * Initialize slirp.
1048 */
1049 rc = slirp_init(&pThis->pNATState, &szNetAddr[0], Netmask, fPassDomain, !!fUseHostResolver, pThis);
1050 if (RT_SUCCESS(rc))
1051 {
1052 slirp_set_dhcp_TFTP_prefix(pThis->pNATState, pThis->pszTFTPPrefix);
1053 slirp_set_dhcp_TFTP_bootfile(pThis->pNATState, pThis->pszBootFile);
1054 slirp_set_dhcp_next_server(pThis->pNATState, pThis->pszNextServer);
1055 slirp_set_dhcp_dns_proxy(pThis->pNATState, !!fDNSProxy);
1056#ifdef VBOX_WITH_SLIRP_BSD_MBUF
1057 slirp_set_mtu(pThis->pNATState, MTU);
1058#endif
1059 char *pszBindIP = NULL;
1060 GET_STRING_ALLOC(rc, pThis, pCfg, "BindIP", pszBindIP);
1061 rc = slirp_set_binding_address(pThis->pNATState, pszBindIP);
1062 if (rc != 0)
1063 LogRel(("NAT: value of BindIP has been ignored\n"));
1064
1065 if(pszBindIP != NULL)
1066 MMR3HeapFree(pszBindIP);
1067#define SLIRP_SET_TUNING_VALUE(name, setter) \
1068 do \
1069 { \
1070 int len = 0; \
1071 rc = CFGMR3QueryS32(pCfg, name, &len); \
1072 if (RT_SUCCESS(rc)) \
1073 setter(pThis->pNATState, len); \
1074 } while(0)
1075
1076 SLIRP_SET_TUNING_VALUE("SockRcv", slirp_set_rcvbuf);
1077 SLIRP_SET_TUNING_VALUE("SockSnd", slirp_set_sndbuf);
1078 SLIRP_SET_TUNING_VALUE("TcpRcv", slirp_set_tcp_rcvspace);
1079 SLIRP_SET_TUNING_VALUE("TcpSnd", slirp_set_tcp_sndspace);
1080
1081 slirp_register_statistics(pThis->pNATState, pDrvIns);
1082#ifdef VBOX_WITH_STATISTICS
1083# define DRV_PROFILE_COUNTER(name, dsc) REGISTER_COUNTER(name, pThis, STAMTYPE_PROFILE, STAMUNIT_TICKS_PER_CALL, dsc)
1084# define DRV_COUNTING_COUNTER(name, dsc) REGISTER_COUNTER(name, pThis, STAMTYPE_COUNTER, STAMUNIT_COUNT, dsc)
1085# include "counters.h"
1086#endif
1087
1088 int rc2 = drvNATConstructRedir(pDrvIns->iInstance, pThis, pCfg, Network);
1089 if (RT_SUCCESS(rc2))
1090 {
1091 /*
1092 * Register a load done notification to get the MAC address into the slirp
1093 * engine after we loaded a guest state.
1094 */
1095 rc2 = PDMDrvHlpSSMRegisterLoadDone(pDrvIns, drvNATLoadDone);
1096 AssertRC(rc2);
1097 rc = RTReqCreateQueue(&pThis->pSlirpReqQueue);
1098 if (RT_FAILURE(rc))
1099 {
1100 LogRel(("NAT: Can't create request queue\n"));
1101 return rc;
1102 }
1103
1104
1105 rc = RTReqCreateQueue(&pThis->pRecvReqQueue);
1106 if (RT_FAILURE(rc))
1107 {
1108 LogRel(("NAT: Can't create request queue\n"));
1109 return rc;
1110 }
1111 rc = RTReqCreateQueue(&pThis->pUrgRecvReqQueue);
1112 if (RT_FAILURE(rc))
1113 {
1114 LogRel(("NAT: Can't create request queue\n"));
1115 return rc;
1116 }
1117 rc = PDMDrvHlpPDMThreadCreate(pDrvIns, &pThis->pRecvThread, pThis, drvNATRecv,
1118 drvNATRecvWakeup, 128 * _1K, RTTHREADTYPE_IO, "NATRX");
1119 AssertRC(rc);
1120 rc = RTSemEventCreate(&pThis->EventRecv);
1121
1122 rc = PDMDrvHlpPDMThreadCreate(pDrvIns, &pThis->pUrgRecvThread, pThis, drvNATUrgRecv,
1123 drvNATUrgRecvWakeup, 128 * _1K, RTTHREADTYPE_IO, "NATURGRX");
1124 AssertRC(rc);
1125 rc = RTSemEventCreate(&pThis->EventRecv);
1126 rc = RTSemEventCreate(&pThis->EventUrgRecv);
1127 rc = RTCritSectInit(&pThis->csDevAccess);
1128 rc = PDMDrvHlpTMTimerCreate(pThis->pDrvIns, TMCLOCK_REAL/*enmClock*/, drvNATSlowTimer,
1129 pThis, TMTIMER_FLAGS_NO_CRIT_SECT/*flags*/, "NATSlowTmr", &pThis->pTmrSlow);
1130 rc = PDMDrvHlpTMTimerCreate(pThis->pDrvIns, TMCLOCK_REAL/*enmClock*/, drvNATFastTimer,
1131 pThis, TMTIMER_FLAGS_NO_CRIT_SECT/*flags*/, "NATFastTmr", &pThis->pTmrFast);
1132
1133#ifndef RT_OS_WINDOWS
1134 /*
1135 * Create the control pipe.
1136 */
1137 int fds[2];
1138 if (pipe(&fds[0]) != 0) /** @todo RTPipeCreate() or something... */
1139 {
1140 rc = RTErrConvertFromErrno(errno);
1141 AssertRC(rc);
1142 return rc;
1143 }
1144 pThis->PipeRead = fds[0];
1145 pThis->PipeWrite = fds[1];
1146#else
1147 pThis->hWakeupEvent = CreateEvent(NULL, FALSE, FALSE, NULL); /* auto-reset event */
1148 slirp_register_external_event(pThis->pNATState, pThis->hWakeupEvent,
1149 VBOX_WAKEUP_EVENT_INDEX);
1150#endif
1151
1152 rc = PDMDrvHlpPDMThreadCreate(pDrvIns, &pThis->pSlirpThread, pThis, drvNATAsyncIoThread,
1153 drvNATAsyncIoWakeup, 128 * _1K, RTTHREADTYPE_IO, "NAT");
1154 AssertRC(rc);
1155
1156#ifdef VBOX_WITH_SLIRP_MT
1157 rc = PDMDrvHlpPDMThreadCreate(pDrvIns, &pThis->pGuestThread, pThis, drvNATAsyncIoGuest,
1158 drvNATAsyncIoGuestWakeup, 128 * _1K, RTTHREADTYPE_IO, "NATGUEST");
1159 AssertRC(rc);
1160#endif
1161
1162 pThis->enmLinkState = PDMNETWORKLINKSTATE_UP;
1163
1164 /* might return VINF_NAT_DNS */
1165 return rc;
1166 }
1167 /* failure path */
1168 rc = rc2;
1169 slirp_term(pThis->pNATState);
1170 pThis->pNATState = NULL;
1171 }
1172 else
1173 {
1174 PDMDRV_SET_ERROR(pDrvIns, rc, N_("Unknown error during NAT networking setup: "));
1175 AssertMsgFailed(("Add error message for rc=%d (%Rrc)\n", rc, rc));
1176 }
1177
1178 return rc;
1179}
1180
1181
1182/**
1183 * NAT network transport driver registration record.
1184 */
1185const PDMDRVREG g_DrvNAT =
1186{
1187 /* u32Version */
1188 PDM_DRVREG_VERSION,
1189 /* szName */
1190 "NAT",
1191 /* szRCMod */
1192 "",
1193 /* szR0Mod */
1194 "",
1195 /* pszDescription */
1196 "NAT Network Transport Driver",
1197 /* fFlags */
1198 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1199 /* fClass. */
1200 PDM_DRVREG_CLASS_NETWORK,
1201 /* cMaxInstances */
1202 16,
1203 /* cbInstance */
1204 sizeof(DRVNAT),
1205 /* pfnConstruct */
1206 drvNATConstruct,
1207 /* pfnDestruct */
1208 drvNATDestruct,
1209 /* pfnRelocate */
1210 NULL,
1211 /* pfnIOCtl */
1212 NULL,
1213 /* pfnPowerOn */
1214 drvNATPowerOn,
1215 /* pfnReset */
1216 NULL,
1217 /* pfnSuspend */
1218 NULL,
1219 /* pfnResume */
1220 NULL,
1221 /* pfnAttach */
1222 NULL,
1223 /* pfnDetach */
1224 NULL,
1225 /* pfnPowerOff */
1226 NULL,
1227 /* pfnSoftReset */
1228 NULL,
1229 /* u32EndVersion */
1230 PDM_DRVREG_VERSION
1231};
1232
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