int uip_netport_init(void) { struct vmm_netswitch *nsw; struct uip_port_state *s = &uip_port_state; struct uip_fw_netif *netif; uip_ipaddr_t ipaddr; char tname[64]; uip_buf = vmm_malloc(UIP_BUFSIZE + 2); if(!uip_buf) { vmm_panic("%s: uip_buf alloc failed\n", __func__); } INIT_SPIN_LOCK(&s->lock); INIT_LIST_HEAD(&s->rxbuf); INIT_COMPLETION(&s->rx_possible); /* Get the first netswitch */ nsw = vmm_netswitch_get(0); if(!nsw) { vmm_panic("No netswitch found\n"); } /* Create a port-name */ vmm_sprintf(tname, "%s-uip", nsw->name); /* Allocate a netport for this netswitch */ s->port = vmm_netport_alloc(tname); if(!s->port) { vmm_printf("UIP->netport alloc failed\n"); return VMM_EFAIL; } /* Allocate a uip_fw_netif */ netif = vmm_malloc(sizeof(struct uip_fw_netif)); if(!netif) { vmm_printf("UIP->netif alloc failed\n"); return VMM_EFAIL; } /* Register the netport */ s->port->mtu = UIP_BUFSIZE; s->port->link_changed = uip_set_link; s->port->can_receive = uip_can_receive; s->port->switch2port_xfer = uip_switch2port_xfer; s->port->priv = s; s->netif = netif; vmm_netport_register(s->port); /* Attach with the netswitch */ vmm_netswitch_port_add(nsw, s->port); /* Notify our ethernet address */ uip_setethaddr(((struct uip_eth_addr *)(s->port->macaddr))); /* Generate an IP address */ uip_ipaddr(ipaddr, 192,168,0,1); uip_fw_setipaddr(netif, ipaddr); uip_ipaddr(ipaddr, 255,255,255,0); uip_fw_setnetmask(netif, ipaddr); /* Register the netif with uip stack */ netif->output = &uip_netport_output; netif->priv = s; uip_fw_register(netif); /* Set this interface as default one */ uip_fw_default(netif); return 0; }
static int __init lwip_netstack_init(void) { int rc; struct vmm_netswitch *nsw; struct vmm_devtree_node *node; const char *attrval; u8 ip[] = {192, 168, 0, 1}; u8 mask[] = {255, 255, 255, 0}; /* Clear lwIP state */ memset(&lns, 0, sizeof(lns)); /* Get netstack device tree node if available */ node = vmm_devtree_getnode(VMM_DEVTREE_PATH_SEPARATOR_STRING VMM_DEVTREE_VMMINFO_NODE_NAME VMM_DEVTREE_PATH_SEPARATOR_STRING VMM_DEVTREE_VMMNET_NODE_NAME VMM_DEVTREE_PATH_SEPARATOR_STRING VMM_DEVTREE_NETSTACK_NODE_NAME); /* Retrive preferred IP address */ attrval = vmm_devtree_attrval(node, "ipaddr"); if (attrval) { /* Read ip address from netstack node */ str2ipaddr(ip, attrval); } /* Retrive preferred IP address */ attrval = vmm_devtree_attrval(node, "netmask"); if (attrval) { /* Read network mask from netstack node */ str2ipaddr(mask, attrval); } /* Retrive preferred netswitch */ attrval = vmm_devtree_attrval(node, "netswitch"); if (attrval) { /* Find netswitch with given name */ nsw = vmm_netswitch_find(attrval); } else { /* Get the first netswitch */ nsw = vmm_netswitch_get(0); } if (!nsw) { vmm_panic("No netswitch found\n"); } /* Allocate a netport */ lns.port = vmm_netport_alloc("lwip-netport", VMM_NETPORT_DEF_QUEUE_SIZE); if (!lns.port) { vmm_printf("lwIP netport_alloc() failed\n"); rc = VMM_EFAIL; goto fail; } /* Setup a netport */ lns.port->mtu = 1500; lns.port->link_changed = lwip_set_link; lns.port->can_receive = lwip_can_receive; lns.port->switch2port_xfer = lwip_switch2port_xfer; lns.port->priv = &lns; /* Register a netport */ rc = vmm_netport_register(lns.port); if (rc) { goto fail1; } /* Initialize lwIP + TCP/IP APIs */ tcpip_init(NULL, NULL); /* Add netif */ IP4_ADDR(&lns.ipaddr, ip[0],ip[1],ip[2],ip[3]); IP4_ADDR(&lns.netmask, mask[0],mask[1],mask[2],mask[3]); IP4_ADDR(&lns.gw, ip[0],ip[1],ip[2],ip[3]); netif_add(&lns.nif, &lns.ipaddr, &lns.netmask, &lns.gw, &lns, lwip_netstack_netif_init, ethernet_input); /* Set default netif */ netif_set_default(&lns.nif); /* Attach netport with netswitch * Note: This will cause netport link_change() */ rc = vmm_netswitch_port_add(nsw, lns.port); if (rc) { goto fail2; } #if !defined(PING_USE_SOCKETS) /* Initalize RAW PCB for ping */ ping_raw_init(); #endif return VMM_OK; fail2: vmm_netport_unregister(lns.port); fail1: vmm_netport_free(lns.port); fail: return rc; }