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