VirtualBox

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

最後變更 在這個檔案從25985是 25985,由 vboxsync 提交於 15 年 前

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

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 22.9 KB
 
1/* $Id: DevINIP.cpp 25985 2010-01-23 00:51:04Z vboxsync $ */
2/** @file
3 * DevINIP - Internal Network IP stack device/service.
4 */
5
6/*
7 * Copyright (C) 2007-2009 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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_DEV_INIP
27#include <iprt/cdefs.h> /* include early to allow RT_C_DECLS_BEGIN hack */
28#include <iprt/mem.h> /* include anything of ours that the lwip headers use. */
29#include <iprt/semaphore.h>
30#include <iprt/thread.h>
31#include <iprt/alloca.h>
32/* All lwip header files are not C++ safe. So hack around this. */
33RT_C_DECLS_BEGIN
34#include "lwip/sys.h"
35#include "lwip/stats.h"
36#include "lwip/mem.h"
37#include "lwip/memp.h"
38#include "lwip/pbuf.h"
39#include "lwip/netif.h"
40#include "ipv4/lwip/ip.h"
41#include "lwip/udp.h"
42#include "lwip/tcp.h"
43#include "lwip/tcpip.h"
44#include "lwip/sockets.h"
45#include "netif/etharp.h"
46RT_C_DECLS_END
47#include <VBox/pdmdev.h>
48#include <VBox/tm.h>
49#include <iprt/assert.h>
50#include <iprt/string.h>
51#include <iprt/uuid.h>
52
53#include "../Builtins.h"
54
55
56/*******************************************************************************
57* Macros and Defines *
58*******************************************************************************/
59
60/** Maximum frame size this device can handle. */
61#define DEVINIP_MAX_FRAME 1514
62
63
64/*******************************************************************************
65* Structures and Typedefs *
66*******************************************************************************/
67
68/**
69 * Internal Network IP stack device instance data.
70 *
71 * @implements PDMIBASE
72 * @implements PDMINETWORKPORT
73 */
74typedef struct DEVINTNETIP
75{
76 /** The base interface for LUN\#0. */
77 PDMIBASE IBase;
78 /** The network port this device provides (LUN\#0). */
79 PDMINETWORKPORT INetworkPort;
80 /** The base interface of the network driver below us. */
81 PPDMIBASE pDrvBase;
82 /** The connector of the network driver below us. */
83 PPDMINETWORKCONNECTOR pDrv;
84 /** Pointer to the device instance. */
85 PPDMDEVINSR3 pDevIns;
86 /** MAC adress. */
87 RTMAC MAC;
88 /** Static IP address of the interface. */
89 char *pszIP;
90 /** Netmask of the interface. */
91 char *pszNetmask;
92 /** Gateway for the interface. */
93 char *pszGateway;
94 /** lwIP network interface description. */
95 struct netif IntNetIF;
96 /** lwIP ARP timer. */
97 PTMTIMERR3 ARPTimer;
98 /** lwIP TCP fast timer. */
99 PTMTIMERR3 TCPFastTimer;
100 /** lwIP TCP slow timer. */
101 PTMTIMERR3 TCPSlowTimer;
102 /** lwIP semaphore to coordinate TCPIP init/terminate. */
103 sys_sem_t LWIPTcpInitSem;
104 /** hack: get linking right. remove this eventually, once the device
105 * provides a proper interface to all IP stack functions. */
106 const void *pLinkHack;
107} DEVINTNETIP, *PDEVINTNETIP;
108
109
110/*******************************************************************************
111* Global Variables *
112*******************************************************************************/
113
114/**
115 * Pointer to the (only) instance data in this device.
116 */
117static PDEVINTNETIP g_pDevINIPData = NULL;
118
119/*
120 * really ugly hack to avoid linking problems on unix style platforms
121 * using .a libraries for now.
122 */
123static const PFNRT g_pDevINILinkHack[] =
124{
125 (PFNRT)lwip_socket,
126 (PFNRT)lwip_close,
127 (PFNRT)lwip_setsockopt,
128 (PFNRT)lwip_recv,
129 (PFNRT)lwip_send,
130 (PFNRT)lwip_select
131};
132
133
134/*******************************************************************************
135* Internal Functions *
136*******************************************************************************/
137static DECLCALLBACK(void) devINIPARPTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer);
138static DECLCALLBACK(void) devINIPTCPFastTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer);
139static DECLCALLBACK(void) devINIPTCPSlowTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer);
140static DECLCALLBACK(err_t) devINIPOutput(struct netif *netif, struct pbuf *p,
141 struct ip_addr *ipaddr);
142static DECLCALLBACK(err_t) devINIPOutputRaw(struct netif *netif,
143 struct pbuf *p);
144static DECLCALLBACK(err_t) devINIPInterface(struct netif *netif);
145
146/**
147 * ARP cache timeout handling for lwIP.
148 *
149 * @param pDevIns Device instance.
150 * @param pTimer Pointer to timer.
151 */
152static DECLCALLBACK(void) devINIPARPTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
153{
154 PDEVINTNETIP pThis = (PDEVINTNETIP)pvUser;
155 LogFlow(("%s: pDevIns=%p pTimer=%p\n", __FUNCTION__, pDevIns, pTimer));
156 lwip_etharp_tmr();
157 TMTimerSetMillies(pThis->ARPTimer, ARP_TMR_INTERVAL);
158 LogFlow(("%s: return\n", __FUNCTION__));
159}
160
161/**
162 * TCP fast timer handling for lwIP.
163 *
164 * @param pDevIns Device instance.
165 * @param pTimer Pointer to timer.
166 */
167static DECLCALLBACK(void) devINIPTCPFastTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
168{
169 PDEVINTNETIP pThis = (PDEVINTNETIP)pvUser;
170 LogFlow(("%s: pDevIns=%p pTimer=%p\n", __FUNCTION__, pDevIns, pTimer));
171 lwip_tcp_fasttmr();
172 TMTimerSetMillies(pThis->TCPFastTimer, TCP_FAST_INTERVAL);
173 LogFlow(("%s: return\n", __FUNCTION__));
174}
175
176/**
177 * TCP slow timer handling for lwIP.
178 *
179 * @param pDevIns Device instance.
180 * @param pTimer Pointer to timer.
181 */
182static DECLCALLBACK(void) devINIPTCPSlowTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
183{
184 PDEVINTNETIP pThis = (PDEVINTNETIP)pvUser;
185 LogFlow(("%s: pDevIns=%p pTimer=%p\n", __FUNCTION__, pDevIns, pTimer));
186 lwip_tcp_slowtmr();
187 TMTimerSetMillies(pThis->TCPSlowTimer, TCP_SLOW_INTERVAL);
188 LogFlow(("%s: return\n", __FUNCTION__));
189}
190
191/**
192 * Output a TCP/IP packet on the interface. Uses the generic lwIP ARP
193 * code to resolve the address and call the link-level packet function.
194 *
195 * @returns lwIP error code
196 * @param netif Interface on which to send IP packet.
197 * @param p Packet data.
198 * @param ipaddr Destination IP address.
199 */
200static DECLCALLBACK(err_t) devINIPOutput(struct netif *netif, struct pbuf *p,
201 struct ip_addr *ipaddr)
202{
203 err_t lrc;
204 LogFlow(("%s: netif=%p p=%p ipaddr=%#04x\n", __FUNCTION__, netif, p,
205 ipaddr->addr));
206 lrc = lwip_etharp_output(netif, ipaddr, p);
207 LogFlow(("%s: return %d\n", __FUNCTION__, lrc));
208 return lrc;
209}
210
211/**
212 * Output a raw packet on the interface.
213 *
214 * @returns lwIP error code
215 * @param netif Interface on which to send frame.
216 * @param p Frame data.
217 */
218static DECLCALLBACK(err_t) devINIPOutputRaw(struct netif *netif,
219 struct pbuf *p)
220{
221 uint8_t aFrame[DEVINIP_MAX_FRAME], *pbBuf;
222 size_t cbBuf;
223 struct pbuf *q;
224 int rc = VINF_SUCCESS;
225
226 LogFlow(("%s: netif=%p p=%p\n", __FUNCTION__, netif, p));
227 Assert(g_pDevINIPData);
228 Assert(g_pDevINIPData->pDrv);
229
230 /* Silently ignore packets being sent while lwIP isn't set up. */
231 if (!g_pDevINIPData)
232 goto out;
233
234#if ETH_PAD_SIZE
235 lwip_pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
236#endif
237
238 pbBuf = &aFrame[0];
239 cbBuf = 0;
240 for (q = p; q != NULL; q = q->next)
241 {
242 if (cbBuf + q->len <= DEVINIP_MAX_FRAME)
243 {
244 memcpy(pbBuf, q->payload, q->len);
245 pbBuf += q->len;
246 cbBuf += q->len;
247 }
248 else
249 {
250 LogRel(("INIP: exceeded frame size\n"));
251 break;
252 }
253 }
254 if (cbBuf)
255 rc = g_pDevINIPData->pDrv->pfnSend(g_pDevINIPData->pDrv,
256 &aFrame[0], cbBuf);
257
258#if ETH_PAD_SIZE
259 lwip_pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
260#endif
261
262out:
263 err_t lrc = ERR_OK;
264 if (RT_FAILURE(rc))
265 lrc = ERR_IF;
266 LogFlow(("%s: return %d (vbox: %Rrc)\n", __FUNCTION__, rc, lrc));
267 return lrc;
268}
269
270/**
271 * Implements the ethernet interface backend initialization for lwIP.
272 *
273 * @returns lwIP error code
274 * @param netif Interface to configure.
275 */
276static DECLCALLBACK(err_t) devINIPInterface(struct netif *netif)
277{
278 LogFlow(("%s: netif=%p\n", __FUNCTION__, netif));
279 Assert(g_pDevINIPData != NULL);
280 netif->state = g_pDevINIPData;
281 netif->hwaddr_len = sizeof(g_pDevINIPData->MAC);
282 memcpy(netif->hwaddr, &g_pDevINIPData->MAC, sizeof(g_pDevINIPData->MAC));
283 netif->mtu = DEVINIP_MAX_FRAME;
284 netif->flags = NETIF_FLAG_BROADCAST;
285 netif->output = devINIPOutput;
286 netif->linkoutput = devINIPOutputRaw;
287
288 lwip_etharp_init();
289 TMTimerSetMillies(g_pDevINIPData->ARPTimer, ARP_TMR_INTERVAL);
290 LogFlow(("%s: success\n", __FUNCTION__));
291 return ERR_OK;
292}
293
294/**
295 * Wait until data can be received.
296 *
297 * @returns VBox status code. VINF_SUCCESS means there is at least one receive descriptor available.
298 * @param pInterface PDM network port interface pointer.
299 * @param cMillies Number of milliseconds to wait. 0 means return immediately.
300 */
301static DECLCALLBACK(int) devINIPWaitInputAvail(PPDMINETWORKPORT pInterface, RTMSINTERVAL cMillies)
302{
303 LogFlow(("%s: pInterface=%p\n", __FUNCTION__, pInterface));
304 LogFlow(("%s: return VINF_SUCCESS\n", __FUNCTION__));
305 return VINF_SUCCESS;
306}
307
308/**
309 * Receive data and pass it to lwIP for processing.
310 *
311 * @returns VBox status code
312 * @param pInterface PDM network port interface pointer.
313 * @param pvBuf Pointer to frame data.
314 * @param cb Frame size.
315 */
316static DECLCALLBACK(int) devINIPInput(PPDMINETWORKPORT pInterface,
317 const void *pvBuf, size_t cb)
318{
319 const uint8_t *pbBuf = (const uint8_t *)pvBuf;
320 size_t len = cb;
321 const struct eth_hdr *ethhdr;
322 struct pbuf *p, *q;
323 int rc = VINF_SUCCESS;
324
325 LogFlow(("%s: pInterface=%p pvBuf=%p cb=%lu\n", __FUNCTION__, pInterface,
326 pvBuf, cb));
327 Assert(g_pDevINIPData);
328 Assert(g_pDevINIPData->pDrv);
329
330 /* Silently ignore packets being received while lwIP isn't set up. */
331 if (!g_pDevINIPData)
332 goto out;
333
334#if ETH_PAD_SIZE
335 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
336#endif
337
338 /* We allocate a pbuf chain of pbufs from the pool. */
339 p = lwip_pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
340 if (p != NULL)
341 {
342#if ETH_PAD_SIZE
343 lwip_pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
344#endif
345
346 for (q = p; q != NULL; q = q->next)
347 {
348 /* Fill the buffers, and clean out unused buffer space. */
349 memcpy(q->payload, pbBuf, RT_MIN(cb, q->len));
350 pbBuf += RT_MIN(cb, q->len);
351 if (q->len > cb)
352 memset(((uint8_t *)q->payload) + cb, '\0', q->len - cb);
353 cb -= RT_MIN(cb, q->len);
354 }
355
356 ethhdr = (const struct eth_hdr *)p->payload;
357 struct netif *iface = &g_pDevINIPData->IntNetIF;
358 err_t lrc;
359 switch (htons(ethhdr->type))
360 {
361 case ETHTYPE_IP: /* IP packet */
362 lwip_pbuf_header(p, -(ssize_t)sizeof(struct eth_hdr));
363 lrc = iface->input(p, iface);
364 if (lrc)
365 rc = VERR_NET_IO_ERROR;
366 break;
367 case ETHTYPE_ARP: /* ARP packet */
368 lwip_etharp_arp_input(iface, (struct eth_addr *)iface->hwaddr, p);
369 break;
370 default:
371 lwip_pbuf_free(p);
372 }
373 }
374
375out:
376 LogFlow(("%s: return %Rrc\n", __FUNCTION__, rc));
377 return rc;
378}
379
380/**
381 * Signals the end of lwIP TCPIP initialization.
382 *
383 * @param arg opaque argument, here the pointer to the semaphore.
384 */
385static DECLCALLBACK(void) devINIPTcpipInitDone(void *arg)
386{
387 sys_sem_t *sem = (sys_sem_t *)arg;
388 lwip_sys_sem_signal(*sem);
389}
390
391
392/**
393 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
394 */
395static DECLCALLBACK(void *) devINIPQueryInterface(PPDMIBASE pInterface,
396 const char *pszIID)
397{
398 PDEVINTNETIP pThis = RT_FROM_MEMBER(pInterface, DEVINTNETIP, IBase);
399 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->IBase);
400 PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKPORT, &pThis->INetworkPort);
401 return NULL;
402}
403
404
405/**
406 * Construct an internal networking IP stack device instance.
407 *
408 * @returns VBox status.
409 * @param pDevIns The device instance data.
410 * If the registration structure is needed, pDevIns->pDevReg points to it.
411 * @param iInstance Instance number. Use this to figure out which registers
412 * and such to use.
413 * he device number is also found in pDevIns->iInstance, but since it's
414 * likely to be freqently used PDM passes it as parameter.
415 * @param pCfgHandle Configuration node handle for the device. Use this to obtain the configuration
416 * of the device instance. It's also found in pDevIns->pCfgHandle, but like
417 * iInstance it's expected to be used a bit in this function.
418 */
419static DECLCALLBACK(int) devINIPConstruct(PPDMDEVINS pDevIns, int iInstance,
420 PCFGMNODE pCfgHandle)
421{
422 PDEVINTNETIP pThis = PDMINS_2_DATA(pDevIns, PDEVINTNETIP);
423 int rc = VINF_SUCCESS;
424 LogFlow(("%s: pDevIns=%p iInstance=%d pCfgHandle=%p\n", __FUNCTION__,
425 pDevIns, iInstance, pCfgHandle));
426
427 Assert(iInstance == 0);
428
429 /*
430 * Validate the config.
431 */
432 if (!CFGMR3AreValuesValid(pCfgHandle, "MAC\0IP\0Netmask\0Gateway\0"))
433 {
434 rc = PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
435 N_("Unknown Internal Networking IP configuration option"));
436 goto out;
437 }
438
439 /*
440 * Init the static parts.
441 */
442 pThis->pszIP = NULL;
443 pThis->pszNetmask = NULL;
444 pThis->pszGateway = NULL;
445 /* Pointer to device instance */
446 pThis->pDevIns = pDevIns;
447 /* IBase */
448 pThis->IBase.pfnQueryInterface = devINIPQueryInterface;
449 /* INetworkPort */
450 pThis->INetworkPort.pfnWaitReceiveAvail = devINIPWaitInputAvail;
451 pThis->INetworkPort.pfnReceive = devINIPInput;
452
453 /*
454 * Get the configuration settings.
455 */
456 rc = CFGMR3QueryBytes(pCfgHandle, "MAC", &pThis->MAC, sizeof(pThis->MAC));
457 if (rc == VERR_CFGM_NOT_BYTES)
458 {
459 char szMAC[64];
460 rc = CFGMR3QueryString(pCfgHandle, "MAC", &szMAC[0], sizeof(szMAC));
461 if (RT_SUCCESS(rc))
462 {
463 char *macStr = &szMAC[0];
464 char *pMac = (char *)&pThis->MAC;
465 for (uint32_t i = 0; i < 6; i++)
466 {
467 if ( !*macStr || !*(macStr + 1)
468 || *macStr == ':' || *(macStr + 1) == ':')
469 {
470 rc = PDMDEV_SET_ERROR(pDevIns,
471 VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
472 N_("Configuration error: Invalid \"MAC\" value"));
473 goto out;
474 }
475 char c1 = *macStr++ - '0';
476 if (c1 > 9)
477 c1 -= 7;
478 char c2 = *macStr++ - '0';
479 if (c2 > 9)
480 c2 -= 7;
481 *pMac++ = ((c1 & 0x0f) << 4) | (c2 & 0x0f);
482 if (i != 5 && *macStr == ':')
483 macStr++;
484 }
485 }
486 }
487 if (RT_FAILURE(rc))
488 {
489 PDMDEV_SET_ERROR(pDevIns, rc,
490 N_("Configuration error: Failed to get the \"MAC\" value"));
491 goto out;
492 }
493 rc = CFGMR3QueryStringAlloc(pCfgHandle, "IP", &pThis->pszIP);
494 if (RT_FAILURE(rc))
495 {
496 PDMDEV_SET_ERROR(pDevIns, rc,
497 N_("Configuration error: Failed to get the \"IP\" value"));
498 goto out;
499 }
500 rc = CFGMR3QueryStringAlloc(pCfgHandle, "Netmask", &pThis->pszNetmask);
501 if (RT_FAILURE(rc))
502 {
503 PDMDEV_SET_ERROR(pDevIns, rc,
504 N_("Configuration error: Failed to get the \"Netmask\" value"));
505 goto out;
506 }
507 rc = CFGMR3QueryStringAlloc(pCfgHandle, "Gateway", &pThis->pszGateway);
508 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
509 rc = VINF_SUCCESS;
510 if (RT_FAILURE(rc))
511 {
512 PDMDEV_SET_ERROR(pDevIns, rc,
513 N_("Configuration error: Failed to get the \"Gateway\" value"));
514 goto out;
515 }
516
517 /*
518 * Attach driver and query the network connector interface.
519 */
520 rc = PDMDevHlpDriverAttach(pDevIns, 0, &pThis->IBase, &pThis->pDrvBase,
521 "Network Port");
522 if (RT_FAILURE(rc))
523 {
524 pThis->pDrvBase = NULL;
525 pThis->pDrv = NULL;
526 goto out;
527 }
528 else
529 {
530 pThis->pDrv = PDMIBASE_QUERY_INTERFACE(pThis->pDrvBase, PDMINETWORKCONNECTOR);
531 if (!pThis->pDrv)
532 {
533 AssertMsgFailed(("Failed to obtain the PDMINTERFACE_NETWORK_CONNECTOR interface!\n"));
534 rc = VERR_PDM_MISSING_INTERFACE_BELOW;
535 goto out;
536 }
537 }
538
539 struct ip_addr ipaddr, netmask, gw;
540 struct in_addr ip;
541
542 if (!inet_aton(pThis->pszIP, &ip))
543 {
544 rc = PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
545 N_("Configuration error: Invalid \"IP\" value"));
546 goto out;
547 }
548 memcpy(&ipaddr, &ip, sizeof(ipaddr));
549 if (!inet_aton(pThis->pszNetmask, &ip))
550 {
551 rc = PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
552 N_("Configuration error: Invalid \"Netmask\" value"));
553 goto out;
554 }
555 memcpy(&netmask, &ip, sizeof(netmask));
556 if (pThis->pszGateway)
557 {
558 if (!inet_aton(pThis->pszGateway, &ip))
559 {
560 rc = PDMDEV_SET_ERROR(pDevIns,
561 VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
562 N_("Configuration error: Invalid \"Gateway\" value"));
563 goto out;
564 }
565 memcpy(&gw, &ip, sizeof(gw));
566 }
567 else
568 {
569 inet_aton(pThis->pszIP, &ip);
570 memcpy(&gw, &ip, sizeof(gw));
571 }
572
573 /*
574 * Initialize lwIP.
575 */
576 lwip_stats_init();
577 lwip_sys_init();
578#if MEM_LIBC_MALLOC == 0
579 lwip_mem_init();
580#endif
581 lwip_memp_init();
582 lwip_pbuf_init();
583 lwip_netif_init();
584 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, devINIPARPTimer, pThis,
585 TMTIMER_FLAGS_NO_CRIT_SECT, "lwIP ARP", &pThis->ARPTimer);
586 if (RT_FAILURE(rc))
587 goto out;
588 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, devINIPTCPFastTimer, pThis,
589 TMTIMER_FLAGS_NO_CRIT_SECT, "lwIP fast TCP", &pThis->TCPFastTimer);
590 if (RT_FAILURE(rc))
591 goto out;
592 TMTimerSetMillies(pThis->TCPFastTimer, TCP_FAST_INTERVAL);
593 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, devINIPTCPSlowTimer, pThis,
594 TMTIMER_FLAGS_NO_CRIT_SECT, "lwIP slow TCP", &pThis->TCPSlowTimer);
595 if (RT_FAILURE(rc))
596 goto out;
597 TMTimerSetMillies(pThis->TCPFastTimer, TCP_SLOW_INTERVAL);
598 pThis->LWIPTcpInitSem = lwip_sys_sem_new(0);
599 {
600 lwip_tcpip_init(devINIPTcpipInitDone, &pThis->LWIPTcpInitSem);
601 lwip_sys_sem_wait(pThis->LWIPTcpInitSem);
602 }
603
604 /*
605 * Set up global pointer to interface data.
606 */
607 g_pDevINIPData = pThis;
608
609 struct netif *ret;
610 pThis->IntNetIF.name[0] = 'I';
611 pThis->IntNetIF.name[1] = 'N';
612 ret = netif_add(&pThis->IntNetIF, &ipaddr, &netmask, &gw, NULL,
613 devINIPInterface, lwip_tcpip_input);
614 if (!ret)
615 {
616 rc = VERR_NET_NO_NETWORK;
617 goto out;
618 }
619
620 lwip_netif_set_default(&pThis->IntNetIF);
621 lwip_netif_set_up(&pThis->IntNetIF);
622
623 /* link hack */
624 pThis->pLinkHack = g_pDevINILinkHack;
625
626out:
627 LogFlow(("%s: return %Rrc\n", __FUNCTION__, rc));
628 return rc;
629}
630
631
632/**
633 * Destruct a device instance.
634 *
635 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
636 * resources can be freed correctly.
637 *
638 * @returns VBox status.
639 * @param pDevIns The device instance data.
640 */
641static DECLCALLBACK(int) devINIPDestruct(PPDMDEVINS pDevIns)
642{
643 PDEVINTNETIP pThis = PDMINS_2_DATA(pDevIns, PDEVINTNETIP);
644
645 LogFlow(("%s: pDevIns=%p\n", __FUNCTION__, pDevIns));
646
647 if (g_pDevINIPData != NULL)
648 {
649 netif_set_down(&pThis->IntNetIF);
650 netif_remove(&pThis->IntNetIF);
651 tcpip_terminate();
652 lwip_sys_sem_wait(pThis->LWIPTcpInitSem);
653 lwip_sys_sem_free(pThis->LWIPTcpInitSem);
654 }
655
656 if (pThis->pszIP)
657 MMR3HeapFree(pThis->pszIP);
658 if (pThis->pszNetmask)
659 MMR3HeapFree(pThis->pszNetmask);
660 if (pThis->pszGateway)
661 MMR3HeapFree(pThis->pszGateway);
662
663 LogFlow(("%s: success\n", __FUNCTION__));
664 return VINF_SUCCESS;
665}
666
667
668/**
669 * Query whether lwIP is initialized or not. Since there is only a single
670 * instance of this device ever for a VM, it can be a global function.
671 *
672 * @returns True if lwIP is initialized.
673 */
674bool DevINIPConfigured(void)
675{
676 return g_pDevINIPData != NULL;
677}
678
679
680/**
681 * Internal network IP stack device registration record.
682 */
683const PDMDEVREG g_DeviceINIP =
684{
685 /* u32Version */
686 PDM_DEVREG_VERSION,
687 /* szDeviceName */
688 "IntNetIP",
689 /* szRCMod/szR0Mod */
690 "",
691 "",
692 /* pszDescription */
693 "Internal Network IP stack device",
694 /* fFlags */
695 PDM_DEVREG_FLAGS_DEFAULT_BITS,
696 /* fClass. As this is used by the storage devices, it must come earlier. */
697 PDM_DEVREG_CLASS_VMM_DEV,
698 /* cMaxInstances */
699 1,
700 /* cbInstance */
701 sizeof(DEVINTNETIP),
702 /* pfnConstruct */
703 devINIPConstruct,
704 /* pfnDestruct */
705 devINIPDestruct,
706 /* pfnRelocate */
707 NULL,
708 /* pfnIOCtl */
709 NULL,
710 /* pfnPowerOn */
711 NULL,
712 /* pfnReset */
713 NULL,
714 /* pfnSuspend */
715 NULL,
716 /* pfnResume */
717 NULL,
718 /* pfnAttach */
719 NULL,
720 /* pfnDetach */
721 NULL,
722 /* pfnQueryInterface */
723 NULL,
724 /* pfnInitComplete */
725 NULL,
726 /* pfnPowerOff */
727 NULL,
728 /* pfnSoftReset */
729 NULL,
730 /* u32VersionEnd */
731 PDM_DEVREG_VERSION
732};
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette