VirtualBox

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

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

NAT: unconditional signal.

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