VirtualBox

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

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

NAT: backed out r55479.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 42.0 KB
Line 
1/* $Id: DrvNAT.cpp 25125 2009-12-01 12:28:55Z 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 if (ASMAtomicReadU32(&pThis->cPkt) > 0)
252 {
253 rc = RTSemEventSignal(pThis->EventRecv);
254 }
255
256 STAM_COUNTER_INC(&pThis->StatNATRecvWakeups);
257 return VINF_SUCCESS;
258}
259
260static DECLCALLBACK(int) drvNATUrgRecv(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
261{
262 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
263
264 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
265 return VINF_SUCCESS;
266
267 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
268 {
269 RTReqProcess(pThis->pUrgRecvReqQueue, 0);
270 if (ASMAtomicReadU32(&pThis->cUrgPkt) == 0)
271 {
272 int rc = RTSemEventWait(pThis->EventUrgRecv, RT_INDEFINITE_WAIT);
273 AssertRC(rc);
274 }
275 }
276 return VINF_SUCCESS;
277}
278static DECLCALLBACK(int) drvNATUrgRecvWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
279{
280 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
281 if (ASMAtomicReadU32(&pThis->cUrgPkt) > 0)
282 {
283 int rc = RTSemEventSignal(pThis->EventUrgRecv);
284 AssertRC(rc);
285 }
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 * Queries an interface to the driver.
809 *
810 * @returns Pointer to interface.
811 * @returns NULL if the interface was not supported by the driver.
812 * @param pInterface Pointer to this interface structure.
813 * @param enmInterface The requested interface identification.
814 * @thread Any thread.
815 */
816static DECLCALLBACK(void *) drvNATQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
817{
818 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
819 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
820 switch (enmInterface)
821 {
822 case PDMINTERFACE_BASE:
823 return &pDrvIns->IBase;
824 case PDMINTERFACE_NETWORK_CONNECTOR:
825 return &pThis->INetworkConnector;
826 default:
827 return NULL;
828 }
829}
830
831
832/**
833 * Get the MAC address into the slirp stack.
834 *
835 * Called by drvNATLoadDone and drvNATPowerOn.
836 */
837static void drvNATSetMac(PDRVNAT pThis)
838{
839 if (pThis->pConfig)
840 {
841 RTMAC Mac;
842 pThis->pConfig->pfnGetMac(pThis->pConfig, &Mac);
843 slirp_set_ethaddr(pThis->pNATState, Mac.au8);
844 }
845}
846
847
848/**
849 * After loading we have to pass the MAC address of the ethernet device to the slirp stack.
850 * Otherwise the guest is not reachable until it performs a DHCP request or an ARP request
851 * (usually done during guest boot).
852 */
853static DECLCALLBACK(int) drvNATLoadDone(PPDMDRVINS pDrvIns, PSSMHANDLE pSSMHandle)
854{
855 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
856 drvNATSetMac(pThis);
857 return VINF_SUCCESS;
858}
859
860
861/**
862 * Some guests might not use DHCP to retrieve an IP but use a static IP.
863 */
864static DECLCALLBACK(void) drvNATPowerOn(PPDMDRVINS pDrvIns)
865{
866 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
867 drvNATSetMac(pThis);
868}
869
870
871/**
872 * Sets up the redirectors.
873 *
874 * @returns VBox status code.
875 * @param pCfgHandle The drivers configuration handle.
876 */
877static int drvNATConstructRedir(unsigned iInstance, PDRVNAT pThis, PCFGMNODE pCfgHandle, RTIPV4ADDR Network)
878{
879 RTMAC Mac;
880 memset(&Mac, 0, sizeof(RTMAC)); /*can't get MAC here */
881 /*
882 * Enumerate redirections.
883 */
884 for (PCFGMNODE pNode = CFGMR3GetFirstChild(pCfgHandle); pNode; pNode = CFGMR3GetNextChild(pNode))
885 {
886 /*
887 * Validate the port forwarding config.
888 */
889 if (!CFGMR3AreValuesValid(pNode, "Protocol\0UDP\0HostPort\0GuestPort\0GuestIP\0BindIP\0"))
890 return PDMDRV_SET_ERROR(pThis->pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES, N_("Unknown configuration in port forwarding"));
891
892 /* protocol type */
893 bool fUDP;
894 char szProtocol[32];
895 int rc;
896 GET_STRING(rc, pThis, pNode, "Protocol", szProtocol[0], sizeof(szProtocol));
897 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
898 {
899 fUDP = false;
900 GET_BOOL(rc, pThis, pNode, "UDP", fUDP);
901 }
902 else if (RT_SUCCESS(rc))
903 {
904 if (!RTStrICmp(szProtocol, "TCP"))
905 fUDP = false;
906 else if (!RTStrICmp(szProtocol, "UDP"))
907 fUDP = true;
908 else
909 return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_INVALID_PARAMETER, RT_SRC_POS,
910 N_("NAT#%d: Invalid configuration value for \"Protocol\": \"%s\""),
911 iInstance, szProtocol);
912 }
913 /* host port */
914 int32_t iHostPort;
915 GET_S32_STRICT(rc, pThis, pNode, "HostPort", iHostPort);
916
917 /* guest port */
918 int32_t iGuestPort;
919 GET_S32_STRICT(rc, pThis, pNode, "GuestPort", iGuestPort);
920
921 /* guest address */
922 struct in_addr GuestIP;
923 /* @todo (vvl) use CTL_* */
924 GETIP_DEF(rc, pThis, pNode, GuestIP, htonl(Network | CTL_GUEST));
925
926 /*
927 * Call slirp about it.
928 */
929 struct in_addr BindIP;
930 GETIP_DEF(rc, pThis, pNode, BindIP, INADDR_ANY);
931 if (slirp_redir(pThis->pNATState, fUDP, BindIP, iHostPort, GuestIP, iGuestPort, Mac.au8) < 0)
932 return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_NAT_REDIR_SETUP, RT_SRC_POS,
933 N_("NAT#%d: configuration error: failed to set up "
934 "redirection of %d to %d. Probably a conflict with "
935 "existing services or other rules"), iInstance, iHostPort,
936 iGuestPort);
937 } /* for each redir rule */
938
939 return VINF_SUCCESS;
940}
941
942
943/**
944 * Destruct a driver instance.
945 *
946 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
947 * resources can be freed correctly.
948 *
949 * @param pDrvIns The driver instance data.
950 */
951static DECLCALLBACK(void) drvNATDestruct(PPDMDRVINS pDrvIns)
952{
953 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
954
955 LogFlow(("drvNATDestruct:\n"));
956
957 slirp_term(pThis->pNATState);
958 slirp_deregister_statistics(pThis->pNATState, pDrvIns);
959 pThis->pNATState = NULL;
960#ifdef VBOX_WITH_STATISTICS
961# define DRV_PROFILE_COUNTER(name, dsc) DEREGISTER_COUNTER(name, pThis)
962# define DRV_COUNTING_COUNTER(name, dsc) DEREGISTER_COUNTER(name, pThis)
963# include "counters.h"
964#endif
965}
966
967
968/**
969 * Construct a NAT network transport driver instance.
970 *
971 * @copydoc FNPDMDRVCONSTRUCT
972 */
973static DECLCALLBACK(int) drvNATConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle, uint32_t fFlags)
974{
975 PDRVNAT pThis = PDMINS_2_DATA(pDrvIns, PDRVNAT);
976
977 LogFlow(("drvNATConstruct:\n"));
978
979 /*
980 * Validate the config.
981 */
982 if (!CFGMR3AreValuesValid(pCfgHandle,
983 "PassDomain\0TFTPPrefix\0BootFile\0Network"
984 "\0NextServer\0DNSProxy\0BindIP\0UseHostResolver\0"
985#ifdef VBOX_WITH_SLIRP_BSD_MBUF
986 "SlirpMTU\0"
987#endif
988 "SocketRcvBuf\0SocketSndBuf\0TcpRcvSpace\0TcpSndSpace\0"))
989 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES,
990 N_("Unknown NAT configuration option, only supports PassDomain,"
991 " TFTPPrefix, BootFile and Network"));
992
993 /*
994 * Init the static parts.
995 */
996 pThis->pDrvIns = pDrvIns;
997 pThis->pNATState = NULL;
998 pThis->pszTFTPPrefix = NULL;
999 pThis->pszBootFile = NULL;
1000 pThis->pszNextServer = NULL;
1001 /* IBase */
1002 pDrvIns->IBase.pfnQueryInterface = drvNATQueryInterface;
1003 /* INetwork */
1004 pThis->INetworkConnector.pfnSend = drvNATSend;
1005 pThis->INetworkConnector.pfnSetPromiscuousMode = drvNATSetPromiscuousMode;
1006 pThis->INetworkConnector.pfnNotifyLinkChanged = drvNATNotifyLinkChanged;
1007
1008 /*
1009 * Get the configuration settings.
1010 */
1011 int rc;
1012 bool fPassDomain = true;
1013 GET_BOOL(rc, pThis, pCfgHandle, "PassDomain", fPassDomain);
1014
1015 GET_STRING_ALLOC(rc, pThis, pCfgHandle, "TFTPPrefix", pThis->pszTFTPPrefix);
1016 GET_STRING_ALLOC(rc, pThis, pCfgHandle, "BootFile", pThis->pszBootFile);
1017 GET_STRING_ALLOC(rc, pThis, pCfgHandle, "NextServer", pThis->pszNextServer);
1018
1019 int fDNSProxy = 0;
1020 GET_S32(rc, pThis, pCfgHandle, "DNSProxy", fDNSProxy);
1021 int fUseHostResolver = 0;
1022 GET_S32(rc, pThis, pCfgHandle, "UseHostResolver", fUseHostResolver);
1023#ifdef VBOX_WITH_SLIRP_BSD_MBUF
1024 int MTU = 1500;
1025 GET_S32(rc, pThis, pCfgHandle, "SlirpMTU", MTU);
1026#endif
1027
1028 /*
1029 * Query the network port interface.
1030 */
1031 pThis->pPort =
1032 (PPDMINETWORKPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase,
1033 PDMINTERFACE_NETWORK_PORT);
1034 if (!pThis->pPort)
1035 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE,
1036 N_("Configuration error: the above device/driver didn't "
1037 "export the network port interface"));
1038 pThis->pConfig =
1039 (PPDMINETWORKCONFIG)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase,
1040 PDMINTERFACE_NETWORK_CONFIG);
1041 if (!pThis->pConfig)
1042 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE,
1043 N_("Configuration error: the above device/driver didn't "
1044 "export the network config interface"));
1045
1046 /* Generate a network address for this network card. */
1047 char szNetwork[32]; /* xxx.xxx.xxx.xxx/yy */
1048 GET_STRING(rc, pThis, pCfgHandle, "Network", szNetwork[0], sizeof(szNetwork));
1049 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1050 RTStrPrintf(szNetwork, sizeof(szNetwork), "10.0.%d.0/24", pDrvIns->iInstance + 2);
1051
1052 RTIPV4ADDR Network;
1053 RTIPV4ADDR Netmask;
1054 rc = RTCidrStrToIPv4(szNetwork, &Network, &Netmask);
1055 if (RT_FAILURE(rc))
1056 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("NAT#%d: Configuration error: "
1057 "network '%s' describes not a valid IPv4 network"),
1058 pDrvIns->iInstance, szNetwork);
1059
1060 char szNetAddr[16];
1061 RTStrPrintf(szNetAddr, sizeof(szNetAddr), "%d.%d.%d.%d",
1062 (Network & 0xFF000000) >> 24, (Network & 0xFF0000) >> 16,
1063 (Network & 0xFF00) >> 8, Network & 0xFF);
1064
1065 /*
1066 * Initialize slirp.
1067 */
1068 rc = slirp_init(&pThis->pNATState, &szNetAddr[0], Netmask, fPassDomain, !!fUseHostResolver, pThis);
1069 if (RT_SUCCESS(rc))
1070 {
1071 slirp_set_dhcp_TFTP_prefix(pThis->pNATState, pThis->pszTFTPPrefix);
1072 slirp_set_dhcp_TFTP_bootfile(pThis->pNATState, pThis->pszBootFile);
1073 slirp_set_dhcp_next_server(pThis->pNATState, pThis->pszNextServer);
1074 slirp_set_dhcp_dns_proxy(pThis->pNATState, !!fDNSProxy);
1075#ifdef VBOX_WITH_SLIRP_BSD_MBUF
1076 slirp_set_mtu(pThis->pNATState, MTU);
1077#endif
1078 char *pszBindIP = NULL;
1079 GET_STRING_ALLOC(rc, pThis, pCfgHandle, "BindIP", pszBindIP);
1080 rc = slirp_set_binding_address(pThis->pNATState, pszBindIP);
1081 if (rc != 0)
1082 LogRel(("NAT: value of BindIP has been ignored\n"));
1083
1084 if(pszBindIP != NULL)
1085 MMR3HeapFree(pszBindIP);
1086#define SLIRP_SET_TUNING_VALUE(name, setter) \
1087 do \
1088 { \
1089 int len = 0; \
1090 rc = CFGMR3QueryS32(pCfgHandle, name, &len); \
1091 if (RT_SUCCESS(rc)) \
1092 setter(pThis->pNATState, len); \
1093 } while(0)
1094
1095 SLIRP_SET_TUNING_VALUE("SocketRcvBuf", slirp_set_rcvbuf);
1096 SLIRP_SET_TUNING_VALUE("SocketSndBuf", slirp_set_sndbuf);
1097 SLIRP_SET_TUNING_VALUE("TcpRcvSpace", slirp_set_tcp_rcvspace);
1098 SLIRP_SET_TUNING_VALUE("TcpSndSpace", slirp_set_tcp_sndspace);
1099
1100 slirp_register_statistics(pThis->pNATState, pDrvIns);
1101#ifdef VBOX_WITH_STATISTICS
1102# define DRV_PROFILE_COUNTER(name, dsc) REGISTER_COUNTER(name, pThis, STAMTYPE_PROFILE, STAMUNIT_TICKS_PER_CALL, dsc)
1103# define DRV_COUNTING_COUNTER(name, dsc) REGISTER_COUNTER(name, pThis, STAMTYPE_COUNTER, STAMUNIT_COUNT, dsc)
1104# include "counters.h"
1105#endif
1106
1107 int rc2 = drvNATConstructRedir(pDrvIns->iInstance, pThis, pCfgHandle, Network);
1108 if (RT_SUCCESS(rc2))
1109 {
1110 /*
1111 * Register a load done notification to get the MAC address into the slirp
1112 * engine after we loaded a guest state.
1113 */
1114 rc2 = PDMDrvHlpSSMRegisterLoadDone(pDrvIns, drvNATLoadDone);
1115 AssertRC(rc2);
1116 rc = RTReqCreateQueue(&pThis->pSlirpReqQueue);
1117 if (RT_FAILURE(rc))
1118 {
1119 LogRel(("NAT: Can't create request queue\n"));
1120 return rc;
1121 }
1122
1123
1124 rc = RTReqCreateQueue(&pThis->pRecvReqQueue);
1125 if (RT_FAILURE(rc))
1126 {
1127 LogRel(("NAT: Can't create request queue\n"));
1128 return rc;
1129 }
1130 rc = RTReqCreateQueue(&pThis->pUrgRecvReqQueue);
1131 if (RT_FAILURE(rc))
1132 {
1133 LogRel(("NAT: Can't create request queue\n"));
1134 return rc;
1135 }
1136 rc = PDMDrvHlpPDMThreadCreate(pDrvIns, &pThis->pRecvThread, pThis, drvNATRecv,
1137 drvNATRecvWakeup, 128 * _1K, RTTHREADTYPE_IO, "NATRX");
1138 AssertRC(rc);
1139 rc = RTSemEventCreate(&pThis->EventRecv);
1140
1141 rc = PDMDrvHlpPDMThreadCreate(pDrvIns, &pThis->pUrgRecvThread, pThis, drvNATUrgRecv,
1142 drvNATUrgRecvWakeup, 128 * _1K, RTTHREADTYPE_IO, "NATURGRX");
1143 AssertRC(rc);
1144 rc = RTSemEventCreate(&pThis->EventRecv);
1145 rc = RTSemEventCreate(&pThis->EventUrgRecv);
1146 rc = RTCritSectInit(&pThis->csDevAccess);
1147 rc = PDMDrvHlpTMTimerCreate(pThis->pDrvIns, TMCLOCK_REAL/*enmClock*/, drvNATSlowTimer,
1148 pThis, TMTIMER_FLAGS_NO_CRIT_SECT/*flags*/, "NATSlowTmr", &pThis->pTmrSlow);
1149 rc = PDMDrvHlpTMTimerCreate(pThis->pDrvIns, TMCLOCK_REAL/*enmClock*/, drvNATFastTimer,
1150 pThis, TMTIMER_FLAGS_NO_CRIT_SECT/*flags*/, "NATFastTmr", &pThis->pTmrFast);
1151
1152#ifndef RT_OS_WINDOWS
1153 /*
1154 * Create the control pipe.
1155 */
1156 int fds[2];
1157 if (pipe(&fds[0]) != 0) /** @todo RTPipeCreate() or something... */
1158 {
1159 int rc = RTErrConvertFromErrno(errno);
1160 AssertRC(rc);
1161 return rc;
1162 }
1163 pThis->PipeRead = fds[0];
1164 pThis->PipeWrite = fds[1];
1165#else
1166 pThis->hWakeupEvent = CreateEvent(NULL, FALSE, FALSE, NULL); /* auto-reset event */
1167 slirp_register_external_event(pThis->pNATState, pThis->hWakeupEvent,
1168 VBOX_WAKEUP_EVENT_INDEX);
1169#endif
1170
1171 rc = PDMDrvHlpPDMThreadCreate(pDrvIns, &pThis->pSlirpThread, pThis, drvNATAsyncIoThread,
1172 drvNATAsyncIoWakeup, 128 * _1K, RTTHREADTYPE_IO, "NAT");
1173 AssertRC(rc);
1174
1175#ifdef VBOX_WITH_SLIRP_MT
1176 rc = PDMDrvHlpPDMThreadCreate(pDrvIns, &pThis->pGuestThread, pThis, drvNATAsyncIoGuest,
1177 drvNATAsyncIoGuestWakeup, 128 * _1K, RTTHREADTYPE_IO, "NATGUEST");
1178 AssertRC(rc);
1179#endif
1180
1181 pThis->enmLinkState = PDMNETWORKLINKSTATE_UP;
1182
1183 /* might return VINF_NAT_DNS */
1184 return rc;
1185 }
1186 /* failure path */
1187 rc = rc2;
1188 slirp_term(pThis->pNATState);
1189 pThis->pNATState = NULL;
1190 }
1191 else
1192 {
1193 PDMDRV_SET_ERROR(pDrvIns, rc, N_("Unknown error during NAT networking setup: "));
1194 AssertMsgFailed(("Add error message for rc=%d (%Rrc)\n", rc, rc));
1195 }
1196
1197 return rc;
1198}
1199
1200
1201/**
1202 * NAT network transport driver registration record.
1203 */
1204const PDMDRVREG g_DrvNAT =
1205{
1206 /* u32Version */
1207 PDM_DRVREG_VERSION,
1208 /* szDriverName */
1209 "NAT",
1210 /* pszDescription */
1211 "NAT Network Transport Driver",
1212 /* fFlags */
1213 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1214 /* fClass. */
1215 PDM_DRVREG_CLASS_NETWORK,
1216 /* cMaxInstances */
1217 16,
1218 /* cbInstance */
1219 sizeof(DRVNAT),
1220 /* pfnConstruct */
1221 drvNATConstruct,
1222 /* pfnDestruct */
1223 drvNATDestruct,
1224 /* pfnIOCtl */
1225 NULL,
1226 /* pfnPowerOn */
1227 drvNATPowerOn,
1228 /* pfnReset */
1229 NULL,
1230 /* pfnSuspend */
1231 NULL,
1232 /* pfnResume */
1233 NULL,
1234 /* pfnAttach */
1235 NULL,
1236 /* pfnDetach */
1237 NULL,
1238 /* pfnPowerOff */
1239 NULL,
1240 /* pfnSoftReset */
1241 NULL,
1242 /* u32EndVersion */
1243 PDM_DRVREG_VERSION
1244};
1245
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