VirtualBox

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

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

pdmifs.h: the final batch of refactored interface ID code.

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