VirtualBox

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

Last change on this file since 22217 was 22217, checked in by vboxsync, 16 years ago

NAT: remove RX-check via PDM Queue experiment

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