VirtualBox

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

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

PDMIBASE refactoring; use UUID as interface IDs.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.0 KB
Line 
1/* $Id: DevINIP.cpp 25966 2010-01-22 11:15:43Z 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.215389.xyz. 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 if (RTUuidCompare2Strs(pszIID, PDMIBASE_IID) == 0)
400 return &pThis->IBase;
401 if (RTUuidCompare2Strs(pszIID, PDMINTERFACE_NETWORK_PORT) == 0)
402 return &pThis->INetworkPort;
403 return NULL;
404}
405
406
407/**
408 * Construct an internal networking IP stack device instance.
409 *
410 * @returns VBox status.
411 * @param pDevIns The device instance data.
412 * If the registration structure is needed, pDevIns->pDevReg points to it.
413 * @param iInstance Instance number. Use this to figure out which registers
414 * and such to use.
415 * he device number is also found in pDevIns->iInstance, but since it's
416 * likely to be freqently used PDM passes it as parameter.
417 * @param pCfgHandle Configuration node handle for the device. Use this to obtain the configuration
418 * of the device instance. It's also found in pDevIns->pCfgHandle, but like
419 * iInstance it's expected to be used a bit in this function.
420 */
421static DECLCALLBACK(int) devINIPConstruct(PPDMDEVINS pDevIns, int iInstance,
422 PCFGMNODE pCfgHandle)
423{
424 PDEVINTNETIP pThis = PDMINS_2_DATA(pDevIns, PDEVINTNETIP);
425 int rc = VINF_SUCCESS;
426 LogFlow(("%s: pDevIns=%p iInstance=%d pCfgHandle=%p\n", __FUNCTION__,
427 pDevIns, iInstance, pCfgHandle));
428
429 Assert(iInstance == 0);
430
431 /*
432 * Validate the config.
433 */
434 if (!CFGMR3AreValuesValid(pCfgHandle, "MAC\0IP\0Netmask\0Gateway\0"))
435 {
436 rc = PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
437 N_("Unknown Internal Networking IP configuration option"));
438 goto out;
439 }
440
441 /*
442 * Init the static parts.
443 */
444 pThis->pszIP = NULL;
445 pThis->pszNetmask = NULL;
446 pThis->pszGateway = NULL;
447 /* Pointer to device instance */
448 pThis->pDevIns = pDevIns;
449 /* IBase */
450 pThis->IBase.pfnQueryInterface = devINIPQueryInterface;
451 /* INetworkPort */
452 pThis->INetworkPort.pfnWaitReceiveAvail = devINIPWaitInputAvail;
453 pThis->INetworkPort.pfnReceive = devINIPInput;
454
455 /*
456 * Get the configuration settings.
457 */
458 rc = CFGMR3QueryBytes(pCfgHandle, "MAC", &pThis->MAC, sizeof(pThis->MAC));
459 if (rc == VERR_CFGM_NOT_BYTES)
460 {
461 char szMAC[64];
462 rc = CFGMR3QueryString(pCfgHandle, "MAC", &szMAC[0], sizeof(szMAC));
463 if (RT_SUCCESS(rc))
464 {
465 char *macStr = &szMAC[0];
466 char *pMac = (char *)&pThis->MAC;
467 for (uint32_t i = 0; i < 6; i++)
468 {
469 if ( !*macStr || !*(macStr + 1)
470 || *macStr == ':' || *(macStr + 1) == ':')
471 {
472 rc = PDMDEV_SET_ERROR(pDevIns,
473 VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
474 N_("Configuration error: Invalid \"MAC\" value"));
475 goto out;
476 }
477 char c1 = *macStr++ - '0';
478 if (c1 > 9)
479 c1 -= 7;
480 char c2 = *macStr++ - '0';
481 if (c2 > 9)
482 c2 -= 7;
483 *pMac++ = ((c1 & 0x0f) << 4) | (c2 & 0x0f);
484 if (i != 5 && *macStr == ':')
485 macStr++;
486 }
487 }
488 }
489 if (RT_FAILURE(rc))
490 {
491 PDMDEV_SET_ERROR(pDevIns, rc,
492 N_("Configuration error: Failed to get the \"MAC\" value"));
493 goto out;
494 }
495 rc = CFGMR3QueryStringAlloc(pCfgHandle, "IP", &pThis->pszIP);
496 if (RT_FAILURE(rc))
497 {
498 PDMDEV_SET_ERROR(pDevIns, rc,
499 N_("Configuration error: Failed to get the \"IP\" value"));
500 goto out;
501 }
502 rc = CFGMR3QueryStringAlloc(pCfgHandle, "Netmask", &pThis->pszNetmask);
503 if (RT_FAILURE(rc))
504 {
505 PDMDEV_SET_ERROR(pDevIns, rc,
506 N_("Configuration error: Failed to get the \"Netmask\" value"));
507 goto out;
508 }
509 rc = CFGMR3QueryStringAlloc(pCfgHandle, "Gateway", &pThis->pszGateway);
510 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
511 rc = VINF_SUCCESS;
512 if (RT_FAILURE(rc))
513 {
514 PDMDEV_SET_ERROR(pDevIns, rc,
515 N_("Configuration error: Failed to get the \"Gateway\" value"));
516 goto out;
517 }
518
519 /*
520 * Attach driver and query the network connector interface.
521 */
522 rc = PDMDevHlpDriverAttach(pDevIns, 0, &pThis->IBase, &pThis->pDrvBase,
523 "Network Port");
524 if (RT_FAILURE(rc))
525 {
526 pThis->pDrvBase = NULL;
527 pThis->pDrv = NULL;
528 goto out;
529 }
530 else
531 {
532 pThis->pDrv = (PPDMINETWORKCONNECTOR)pThis->pDrvBase->pfnQueryInterface(pThis->pDrvBase, PDMINTERFACE_NETWORK_CONNECTOR);
533 if (!pThis->pDrv)
534 {
535 AssertMsgFailed(("Failed to obtain the PDMINTERFACE_NETWORK_CONNECTOR interface!\n"));
536 rc = VERR_PDM_MISSING_INTERFACE_BELOW;
537 goto out;
538 }
539 }
540
541 struct ip_addr ipaddr, netmask, gw;
542 struct in_addr ip;
543
544 if (!inet_aton(pThis->pszIP, &ip))
545 {
546 rc = PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
547 N_("Configuration error: Invalid \"IP\" value"));
548 goto out;
549 }
550 memcpy(&ipaddr, &ip, sizeof(ipaddr));
551 if (!inet_aton(pThis->pszNetmask, &ip))
552 {
553 rc = PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
554 N_("Configuration error: Invalid \"Netmask\" value"));
555 goto out;
556 }
557 memcpy(&netmask, &ip, sizeof(netmask));
558 if (pThis->pszGateway)
559 {
560 if (!inet_aton(pThis->pszGateway, &ip))
561 {
562 rc = PDMDEV_SET_ERROR(pDevIns,
563 VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
564 N_("Configuration error: Invalid \"Gateway\" value"));
565 goto out;
566 }
567 memcpy(&gw, &ip, sizeof(gw));
568 }
569 else
570 {
571 inet_aton(pThis->pszIP, &ip);
572 memcpy(&gw, &ip, sizeof(gw));
573 }
574
575 /*
576 * Initialize lwIP.
577 */
578 lwip_stats_init();
579 lwip_sys_init();
580#if MEM_LIBC_MALLOC == 0
581 lwip_mem_init();
582#endif
583 lwip_memp_init();
584 lwip_pbuf_init();
585 lwip_netif_init();
586 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, devINIPARPTimer, pThis,
587 TMTIMER_FLAGS_NO_CRIT_SECT, "lwIP ARP", &pThis->ARPTimer);
588 if (RT_FAILURE(rc))
589 goto out;
590 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, devINIPTCPFastTimer, pThis,
591 TMTIMER_FLAGS_NO_CRIT_SECT, "lwIP fast TCP", &pThis->TCPFastTimer);
592 if (RT_FAILURE(rc))
593 goto out;
594 TMTimerSetMillies(pThis->TCPFastTimer, TCP_FAST_INTERVAL);
595 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, devINIPTCPSlowTimer, pThis,
596 TMTIMER_FLAGS_NO_CRIT_SECT, "lwIP slow TCP", &pThis->TCPSlowTimer);
597 if (RT_FAILURE(rc))
598 goto out;
599 TMTimerSetMillies(pThis->TCPFastTimer, TCP_SLOW_INTERVAL);
600 pThis->LWIPTcpInitSem = lwip_sys_sem_new(0);
601 {
602 lwip_tcpip_init(devINIPTcpipInitDone, &pThis->LWIPTcpInitSem);
603 lwip_sys_sem_wait(pThis->LWIPTcpInitSem);
604 }
605
606 /*
607 * Set up global pointer to interface data.
608 */
609 g_pDevINIPData = pThis;
610
611 struct netif *ret;
612 pThis->IntNetIF.name[0] = 'I';
613 pThis->IntNetIF.name[1] = 'N';
614 ret = netif_add(&pThis->IntNetIF, &ipaddr, &netmask, &gw, NULL,
615 devINIPInterface, lwip_tcpip_input);
616 if (!ret)
617 {
618 rc = VERR_NET_NO_NETWORK;
619 goto out;
620 }
621
622 lwip_netif_set_default(&pThis->IntNetIF);
623 lwip_netif_set_up(&pThis->IntNetIF);
624
625 /* link hack */
626 pThis->pLinkHack = g_pDevINILinkHack;
627
628out:
629 LogFlow(("%s: return %Rrc\n", __FUNCTION__, rc));
630 return rc;
631}
632
633
634/**
635 * Destruct a device instance.
636 *
637 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
638 * resources can be freed correctly.
639 *
640 * @returns VBox status.
641 * @param pDevIns The device instance data.
642 */
643static DECLCALLBACK(int) devINIPDestruct(PPDMDEVINS pDevIns)
644{
645 PDEVINTNETIP pThis = PDMINS_2_DATA(pDevIns, PDEVINTNETIP);
646
647 LogFlow(("%s: pDevIns=%p\n", __FUNCTION__, pDevIns));
648
649 if (g_pDevINIPData != NULL)
650 {
651 netif_set_down(&pThis->IntNetIF);
652 netif_remove(&pThis->IntNetIF);
653 tcpip_terminate();
654 lwip_sys_sem_wait(pThis->LWIPTcpInitSem);
655 lwip_sys_sem_free(pThis->LWIPTcpInitSem);
656 }
657
658 if (pThis->pszIP)
659 MMR3HeapFree(pThis->pszIP);
660 if (pThis->pszNetmask)
661 MMR3HeapFree(pThis->pszNetmask);
662 if (pThis->pszGateway)
663 MMR3HeapFree(pThis->pszGateway);
664
665 LogFlow(("%s: success\n", __FUNCTION__));
666 return VINF_SUCCESS;
667}
668
669
670/**
671 * Query whether lwIP is initialized or not. Since there is only a single
672 * instance of this device ever for a VM, it can be a global function.
673 *
674 * @returns True if lwIP is initialized.
675 */
676bool DevINIPConfigured(void)
677{
678 return g_pDevINIPData != NULL;
679}
680
681
682/**
683 * Internal network IP stack device registration record.
684 */
685const PDMDEVREG g_DeviceINIP =
686{
687 /* u32Version */
688 PDM_DEVREG_VERSION,
689 /* szDeviceName */
690 "IntNetIP",
691 /* szRCMod/szR0Mod */
692 "",
693 "",
694 /* pszDescription */
695 "Internal Network IP stack device",
696 /* fFlags */
697 PDM_DEVREG_FLAGS_DEFAULT_BITS,
698 /* fClass. As this is used by the storage devices, it must come earlier. */
699 PDM_DEVREG_CLASS_VMM_DEV,
700 /* cMaxInstances */
701 1,
702 /* cbInstance */
703 sizeof(DEVINTNETIP),
704 /* pfnConstruct */
705 devINIPConstruct,
706 /* pfnDestruct */
707 devINIPDestruct,
708 /* pfnRelocate */
709 NULL,
710 /* pfnIOCtl */
711 NULL,
712 /* pfnPowerOn */
713 NULL,
714 /* pfnReset */
715 NULL,
716 /* pfnSuspend */
717 NULL,
718 /* pfnResume */
719 NULL,
720 /* pfnAttach */
721 NULL,
722 /* pfnDetach */
723 NULL,
724 /* pfnQueryInterface */
725 NULL,
726 /* pfnInitComplete */
727 NULL,
728 /* pfnPowerOff */
729 NULL,
730 /* pfnSoftReset */
731 NULL,
732 /* u32VersionEnd */
733 PDM_DEVREG_VERSION
734};
Note: See TracBrowser for help on using the repository browser.

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