/** * A simple wrapper function that allows you to free heap memory from * interrupt context. * * @param m the heap memory to free * @return ERR_OK if callback could be enqueued, an err_t if not */ err_t mem_free_callback(void *m) { return tcpip_callback_with_block(mem_free, m, 0); }
/** * @brief LWIP handling thread. * * @param[in] p pointer to a @p lwipthread_opts structure or @p NULL * @return The function does not return. */ msg_t lwip_thread(void *p) { event_timer_t evt; event_listener_t el0, el1; struct ip_addr ip, gateway, netmask; static struct netif thisif; static const MACConfig mac_config = {thisif.hwaddr}; chRegSetThreadName("lwipthread"); /* Initializes the thing.*/ tcpip_init(NULL, NULL); /* TCP/IP parameters, runtime or compile time.*/ if (p) { struct lwipthread_opts *opts = p; unsigned i; for (i = 0; i < 6; i++) thisif.hwaddr[i] = opts->macaddress[i]; ip.addr = opts->address; gateway.addr = opts->gateway; netmask.addr = opts->netmask; } else { thisif.hwaddr[0] = LWIP_ETHADDR_0; thisif.hwaddr[1] = LWIP_ETHADDR_1; thisif.hwaddr[2] = LWIP_ETHADDR_2; thisif.hwaddr[3] = LWIP_ETHADDR_3; thisif.hwaddr[4] = LWIP_ETHADDR_4; thisif.hwaddr[5] = LWIP_ETHADDR_5; LWIP_IPADDR(&ip); LWIP_GATEWAY(&gateway); LWIP_NETMASK(&netmask); } macStart(ÐD1, &mac_config); netif_add(&thisif, &ip, &netmask, &gateway, NULL, ethernetif_init, tcpip_input); netif_set_default(&thisif); netif_set_up(&thisif); /* Setup event sources.*/ evtObjectInit(&evt, LWIP_LINK_POLL_INTERVAL); evtStart(&evt); chEvtRegisterMask(&evt.et_es, &el0, PERIODIC_TIMER_ID); chEvtRegisterMask(macGetReceiveEventSource(ÐD1), &el1, FRAME_RECEIVED_ID); chEvtAddEvents(PERIODIC_TIMER_ID | FRAME_RECEIVED_ID); /* Goes to the final priority after initialization.*/ chThdSetPriority(LWIP_THREAD_PRIORITY); while (TRUE) { eventmask_t mask = chEvtWaitAny(ALL_EVENTS); if (mask & PERIODIC_TIMER_ID) { bool_t current_link_status = macPollLinkStatus(ÐD1); if (current_link_status != netif_is_link_up(&thisif)) { if (current_link_status) tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_up, &thisif, 0); else tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_down, &thisif, 0); } } if (mask & FRAME_RECEIVED_ID) { struct pbuf *p; while ((p = low_level_input(&thisif)) != NULL) { struct eth_hdr *ethhdr = p->payload; switch (htons(ethhdr->type)) { /* IP or ARP packet? */ case ETHTYPE_IP: case ETHTYPE_ARP: #if PPPOE_SUPPORT /* PPPoE packet? */ case ETHTYPE_PPPOEDISC: case ETHTYPE_PPPOE: #endif /* PPPOE_SUPPORT */ /* full packet send to tcpip_thread to process */ if (thisif.input(p, &thisif) == ERR_OK) break; LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n")); default: pbuf_free(p); } } } } return 0; }
/** * @note: this work on Event thread. */ HRESULT VBoxNetLwipNAT::HandleEvent(VBoxEventType_T aEventType, IEvent *pEvent) { HRESULT hrc = S_OK; switch (aEventType) { case VBoxEventType_OnNATNetworkSetting: { ComPtr<INATNetworkSettingEvent> pSettingsEvent(pEvent); com::Bstr networkName; hrc = pSettingsEvent->COMGETTER(NetworkName)(networkName.asOutParam()); AssertComRCReturn(hrc, hrc); if (networkName.compare(getNetworkName().c_str())) break; /* change not for our network */ // XXX: only handle IPv6 default route for now if (!m_ProxyOptions.ipv6_enabled) break; BOOL fIPv6DefaultRoute = FALSE; hrc = pSettingsEvent->COMGETTER(AdvertiseDefaultIPv6RouteEnabled)(&fIPv6DefaultRoute); AssertComRCReturn(hrc, hrc); if (m_ProxyOptions.ipv6_defroute == fIPv6DefaultRoute) break; m_ProxyOptions.ipv6_defroute = fIPv6DefaultRoute; tcpip_callback_with_block(proxy_rtadvd_do_quick, &m_LwipNetIf, 0); break; } case VBoxEventType_OnNATNetworkPortForward: { ComPtr<INATNetworkPortForwardEvent> pForwardEvent = pEvent; com::Bstr networkName; hrc = pForwardEvent->COMGETTER(NetworkName)(networkName.asOutParam()); AssertComRCReturn(hrc, hrc); if (networkName.compare(getNetworkName().c_str())) break; /* change not for our network */ BOOL fCreateFW; hrc = pForwardEvent->COMGETTER(Create)(&fCreateFW); AssertComRCReturn(hrc, hrc); BOOL fIPv6FW; hrc = pForwardEvent->COMGETTER(Ipv6)(&fIPv6FW); AssertComRCReturn(hrc, hrc); com::Bstr name; hrc = pForwardEvent->COMGETTER(Name)(name.asOutParam()); AssertComRCReturn(hrc, hrc); NATProtocol_T proto = NATProtocol_TCP; hrc = pForwardEvent->COMGETTER(Proto)(&proto); AssertComRCReturn(hrc, hrc); com::Bstr strHostAddr; hrc = pForwardEvent->COMGETTER(HostIp)(strHostAddr.asOutParam()); AssertComRCReturn(hrc, hrc); LONG lHostPort; hrc = pForwardEvent->COMGETTER(HostPort)(&lHostPort); AssertComRCReturn(hrc, hrc); com::Bstr strGuestAddr; hrc = pForwardEvent->COMGETTER(GuestIp)(strGuestAddr.asOutParam()); AssertComRCReturn(hrc, hrc); LONG lGuestPort; hrc = pForwardEvent->COMGETTER(GuestPort)(&lGuestPort); AssertComRCReturn(hrc, hrc); VECNATSERVICEPF& rules = fIPv6FW ? m_vecPortForwardRule6 : m_vecPortForwardRule4; NATSEVICEPORTFORWARDRULE r; RT_ZERO(r); r.Pfr.fPfrIPv6 = fIPv6FW; switch (proto) { case NATProtocol_TCP: r.Pfr.iPfrProto = IPPROTO_TCP; break; case NATProtocol_UDP: r.Pfr.iPfrProto = IPPROTO_UDP; break; default: LogRel(("Event: %s %s port-forwarding rule \"%s\":" " invalid protocol %d\n", fCreateFW ? "Add" : "Remove", fIPv6FW ? "IPv6" : "IPv4", com::Utf8Str(name).c_str(), (int)proto)); goto port_forward_done; } LogRel(("Event: %s %s port-forwarding rule \"%s\":" " %s %s%s%s:%d -> %s%s%s:%d\n", fCreateFW ? "Add" : "Remove", fIPv6FW ? "IPv6" : "IPv4", com::Utf8Str(name).c_str(), proto == NATProtocol_TCP ? "TCP" : "UDP", /* from */ fIPv6FW ? "[" : "", com::Utf8Str(strHostAddr).c_str(), fIPv6FW ? "]" : "", lHostPort, /* to */ fIPv6FW ? "[" : "", com::Utf8Str(strGuestAddr).c_str(), fIPv6FW ? "]" : "", lGuestPort)); if (name.length() > sizeof(r.Pfr.szPfrName)) { hrc = E_INVALIDARG; goto port_forward_done; } RTStrPrintf(r.Pfr.szPfrName, sizeof(r.Pfr.szPfrName), "%s", com::Utf8Str(name).c_str()); RTStrPrintf(r.Pfr.szPfrHostAddr, sizeof(r.Pfr.szPfrHostAddr), "%s", com::Utf8Str(strHostAddr).c_str()); /* XXX: limits should be checked */ r.Pfr.u16PfrHostPort = (uint16_t)lHostPort; RTStrPrintf(r.Pfr.szPfrGuestAddr, sizeof(r.Pfr.szPfrGuestAddr), "%s", com::Utf8Str(strGuestAddr).c_str()); /* XXX: limits should be checked */ r.Pfr.u16PfrGuestPort = (uint16_t)lGuestPort; if (fCreateFW) /* Addition */ { int rc = natServicePfRegister(r); if (RT_SUCCESS(rc)) rules.push_back(r); } else /* Deletion */ { ITERATORNATSERVICEPF it; for (it = rules.begin(); it != rules.end(); ++it) { /* compare */ NATSEVICEPORTFORWARDRULE& natFw = *it; if ( natFw.Pfr.iPfrProto == r.Pfr.iPfrProto && natFw.Pfr.u16PfrHostPort == r.Pfr.u16PfrHostPort && (strncmp(natFw.Pfr.szPfrHostAddr, r.Pfr.szPfrHostAddr, INET6_ADDRSTRLEN) == 0) && natFw.Pfr.u16PfrGuestPort == r.Pfr.u16PfrGuestPort && (strncmp(natFw.Pfr.szPfrGuestAddr, r.Pfr.szPfrGuestAddr, INET6_ADDRSTRLEN) == 0)) { RTCMemAutoPtr<fwspec> pFwCopy; if (RT_UNLIKELY(!pFwCopy.alloc())) break; memcpy(pFwCopy.get(), &natFw.FWSpec, sizeof(natFw.FWSpec)); int status = portfwd_rule_del(pFwCopy.get()); if (status != 0) break; pFwCopy.release(); /* owned by lwip thread now */ rules.erase(it); break; } } /* loop over vector elements */ } /* condition add or delete */ port_forward_done: /* clean up strings */ name.setNull(); strHostAddr.setNull(); strGuestAddr.setNull(); break; } case VBoxEventType_OnHostNameResolutionConfigurationChange: { const char **ppcszNameServers = getHostNameservers(); err_t error; error = tcpip_callback_with_block(pxdns_set_nameservers, ppcszNameServers, /* :block */ 0); if (error != ERR_OK && ppcszNameServers != NULL) RTMemFree(ppcszNameServers); break; } case VBoxEventType_OnNATNetworkStartStop: { ComPtr <INATNetworkStartStopEvent> pStartStopEvent = pEvent; com::Bstr networkName; hrc = pStartStopEvent->COMGETTER(NetworkName)(networkName.asOutParam()); AssertComRCReturn(hrc, hrc); if (networkName.compare(getNetworkName().c_str())) break; /* change not for our network */ BOOL fStart = TRUE; hrc = pStartStopEvent->COMGETTER(StartEvent)(&fStart); AssertComRCReturn(hrc, hrc); if (!fStart) shutdown(); break; } } return hrc; }
/** * A simple wrapper function that allows you to free a pbuf from interrupt context. * * @param p The pbuf (chain) to be dereferenced. * @return ERR_OK if callback could be enqueued, an err_t if not */ err_t pbuf_free_callback(struct pbuf *p) { return tcpip_callback_with_block(pbuf_free_int, p, 0); }
/* LWIP kickoff and PHY link monitor thread */ static void vSetupIFTask(void *pvParameters) { ip_addr_t ipaddr, netmask, gw; volatile s32_t tcpipdone = 0; uint32_t physts; static int prt_ip = 0; DEBUGSTR("LWIP HTTP Web Server FreeRTOS Demo...\r\n"); /* Wait until the TCP/IP thread is finished before continuing or wierd things may happen */ DEBUGSTR("Waiting for TCPIP thread to initialize...\r\n"); tcpip_init(tcpip_init_done_signal, (void *) &tcpipdone); while (!tcpipdone) { msDelay(1); } DEBUGSTR("Starting LWIP HTTP server...\r\n"); /* Static IP assignment */ #if LWIP_DHCP IP4_ADDR(&gw, 0, 0, 0, 0); IP4_ADDR(&ipaddr, 0, 0, 0, 0); IP4_ADDR(&netmask, 0, 0, 0, 0); #else IP4_ADDR(&gw, 10, 1, 10, 1); IP4_ADDR(&ipaddr, 10, 1, 10, 234); IP4_ADDR(&netmask, 255, 255, 255, 0); #endif /* Add netif interface for lpc17xx_8x */ memset(&lpc_netif, 0, sizeof(lpc_netif)); if (!netif_add(&lpc_netif, &ipaddr, &netmask, &gw, NULL, lpc_enetif_init, tcpip_input)) { DEBUGSTR("Net interface failed to initialize\r\n"); while(1); } netif_set_default(&lpc_netif); netif_set_up(&lpc_netif); /* Enable MAC interrupts only after LWIP is ready */ NVIC_SetPriority(ETHERNET_IRQn, config_ETHERNET_INTERRUPT_PRIORITY); NVIC_EnableIRQ(ETHERNET_IRQn); #if LWIP_DHCP dhcp_start(&lpc_netif); #endif /* Initialize and start application */ http_server_netconn_init(); /* This loop monitors the PHY link and will handle cable events via the PHY driver. */ while (1) { /* Call the PHY status update state machine once in a while to keep the link status up-to-date */ physts = lpcPHYStsPoll(); /* Only check for connection state when the PHY status has changed */ if (physts & PHY_LINK_CHANGED) { if (physts & PHY_LINK_CONNECTED) { Board_LED_Set(0, true); prt_ip = 0; /* Set interface speed and duplex */ if (physts & PHY_LINK_SPEED100) { Chip_ENET_Set100Mbps(LPC_ETHERNET); NETIF_INIT_SNMP(&lpc_netif, snmp_ifType_ethernet_csmacd, 100000000); } else { Chip_ENET_Set10Mbps(LPC_ETHERNET); NETIF_INIT_SNMP(&lpc_netif, snmp_ifType_ethernet_csmacd, 10000000); } if (physts & PHY_LINK_FULLDUPLX) { Chip_ENET_SetFullDuplex(LPC_ETHERNET); } else { Chip_ENET_SetHalfDuplex(LPC_ETHERNET); } tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_up, (void *) &lpc_netif, 1); } else { Board_LED_Set(0, false); tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_down, (void *) &lpc_netif, 1); } /* Delay for link detection (250mS) */ vTaskDelay(configTICK_RATE_HZ / 4); } /* Print IP address info */ if (!prt_ip) { if (lpc_netif.ip_addr.addr) { static char tmp_buff[16]; DEBUGOUT("IP_ADDR : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &lpc_netif.ip_addr, tmp_buff, 16)); DEBUGOUT("NET_MASK : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &lpc_netif.netmask, tmp_buff, 16)); DEBUGOUT("GATEWAY_IP : %s\r\n", ipaddr_ntoa_r((const ip_addr_t *) &lpc_netif.gw, tmp_buff, 16)); prt_ip = 1; } } } }
/* This is somewhat different to other ports: we have a main loop here: * a dedicated task that waits for packets to arrive. This would normally be * done from interrupt context with embedded hardware, but we don't get an * interrupt in windows for that :-) */ void main_loop() { #if !NO_SYS err_t err; sys_sem_t init_sem; #endif /* NO_SYS */ #if PPP_SUPPORT #if !USE_ETHERNET int count; u8_t rxbuf[1024]; #endif volatile int callClosePpp = 0; #endif /* PPP_SUPPORT */ /* initialize lwIP stack, network interfaces and applications */ #if NO_SYS lwip_init(); test_init(NULL); #else /* NO_SYS */ err = sys_sem_new(&init_sem, 0); tcpip_init(test_init, &init_sem); /* we have to wait for initialization to finish before * calling update_adapter()! */ sys_sem_wait(&init_sem); sys_sem_free(&init_sem); #endif /* NO_SYS */ /* MAIN LOOP for driver update (and timers if NO_SYS) */ while (!_kbhit()) { #if NO_SYS /* handle timers (already done in tcpip.c when NO_SYS=0) */ sys_check_timeouts(); #endif /* NO_SYS */ #if USE_ETHERNET #if !PCAPIF_RX_USE_THREAD /* check for packets and link status*/ pcapif_poll(&netif); /* When pcapif_poll comes back, there are not packets, so sleep to prevent 100% CPU load. Don't do this in an embedded system since it increases latency! */ sys_msleep(1); #else /* !PCAPIF_RX_USE_THREAD */ sys_msleep(50); #endif /* !PCAPIF_RX_USE_THREAD */ #else /* USE_ETHERNET */ #if 0 /* set this to 1 if PPP_INPROC_OWNTHREAD==0 or not defined (see ppp.c) */ /* try to read characters from serial line and pass them to PPPoS */ count = sio_read(ppp_sio, (u8_t*)rxbuf, 1024); if(count > 0) { pppos_input(ppp_desc, rxbuf, count); } else #endif { /* nothing received, give other tasks a chance to run */ sys_msleep(1); } #endif /* USE_ETHERNET */ #if USE_SLIPIF slipif_poll(&slipif1); #if USE_SLIPIF > 1 slipif_poll(&slipif2); #endif /* USE_SLIPIF > 1 */ #endif /* USE_SLIPIF */ #if ENABLE_LOOPBACK && !LWIP_NETIF_LOOPBACK_MULTITHREADING /* check for loopback packets on all netifs */ netif_poll_all(); #endif /* ENABLE_LOOPBACK && !LWIP_NETIF_LOOPBACK_MULTITHREADING */ #if PPP_SUPPORT { int do_hup = 0; if(do_hup) { pppSigHUP(ppp_desc); do_hup = 0; } } if(callClosePpp && (ppp_desc >= 0)) { /* make sure to disconnect PPP before stopping the program... */ callClosePpp = 0; #if NO_SYS pppClose(ppp_desc); #else tcpip_callback_with_block(pppCloseCallback, (void*)ppp_desc, 0); #endif ppp_desc = -1; } #endif /* PPP_SUPPORT */ } #if PPP_SUPPORT if(ppp_desc >= 0) { u32_t started; printf("Closing PPP connection...\n"); /* make sure to disconnect PPP before stopping the program... */ #if NO_SYS pppClose(ppp_desc); #else tcpip_callback_with_block(pppCloseCallback, (void*)ppp_desc, 0); #endif ppp_desc = -1; /* Wait for some time to let PPP finish... */ started = sys_now(); do { #if USE_ETHERNET && !PCAPIF_RX_USE_THREAD pcapif_poll(&netif); #else /* USE_ETHERNET && !PCAPIF_RX_USE_THREAD */ sys_msleep(50); #endif /* USE_ETHERNET && !PCAPIF_RX_USE_THREAD */ /* @todo: need a better check here: only wait until PPP is down */ } while(sys_now() - started < 5000); } #endif /* PPP_SUPPORT */ #if USE_ETHERNET /* release the pcap library... */ pcapif_shutdown(&netif); #endif /* USE_ETHERNET */ }
/** * Network interface setup function * (Only used in case of FreeRTOS/uCOS-III configuration) */ static void vSetupIFTask(void *pvParameters) { ip_addr_t ipaddr, netmask, gw; volatile s32_t tcpipdone = 0; uint32_t physts; static int prt_ip = 0; /* Wait until the TCP/IP thread is finished before continuing or wierd things may happen */ DEBUGSTR("Waiting for TCPIP thread to initialize...\r\n"); tcpip_init(tcpip_init_done_signal, (void *) &tcpipdone); while (!tcpipdone) ; DEBUGSTR("Starting LWIP HTTP server...\r\n"); /* Static IP assignment */ #if LWIP_DHCP IP4_ADDR(&gw, 0, 0, 0, 0); IP4_ADDR(&ipaddr, 0, 0, 0, 0); IP4_ADDR(&netmask, 0, 0, 0, 0); #else IP4_ADDR(&gw, 192, 168, 200, 1); IP4_ADDR(&ipaddr, 192, 168, 200, 99); IP4_ADDR(&netmask, 255, 255, 255, 0); #endif /* Add netif interface for lpc17xx_8x */ memset(&lpc_netif, 0, sizeof(lpc_netif)); if (!netif_add(&lpc_netif, &ipaddr, &netmask, &gw, NULL, lpc_enetif_init, tcpip_input)) { DEBUGSTR("Net interface failed to initialize ..\r\n"); while (1) ; } netif_set_default(&lpc_netif); netif_set_up(&lpc_netif); /* Enable MAC interrupts only after LWIP is ready */ NVIC_SetPriority(ETHERNET_IRQn, IRQ_PRIO_ETHERNET); NVIC_EnableIRQ(ETHERNET_IRQn); #if LWIP_DHCP dhcp_start(&lpc_netif); #endif /* Initialize and start application */ //http_server_netconn_init(); /* This loop monitors the PHY link and will handle cable events via the PHY driver. */ while (1) { /* Call the PHY status update state machine once in a while to keep the link status up-to-date */ physts = lpcPHYStsPoll(); /* Only check for connection state when the PHY status has changed */ if (physts & PHY_LINK_CHANGED) { if (physts & PHY_LINK_CONNECTED) { /* Set interface speed and duplex */ if (physts & PHY_LINK_SPEED100) { NETIF_INIT_SNMP(&lpc_netif, snmp_ifType_ethernet_csmacd, 100000000); } else { NETIF_INIT_SNMP(&lpc_netif, snmp_ifType_ethernet_csmacd, 10000000); } if (physts & PHY_LINK_FULLDUPLX) { Chip_ENET_SetDuplex(LPC_ETHERNET, true); } else { Chip_ENET_SetDuplex(LPC_ETHERNET, false); } tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_up, (void *) &lpc_netif, 1); } else { tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_down, (void *) &lpc_netif, 1); } } // DEBUGOUT("Link connect status: %d\n", ((physts & PHY_LINK_CONNECTED) != 0)); /* Delay for link detection */ msDelay(250); if (!prt_ip && lpc_netif.ip_addr.addr) { prt_ip = 1; ip_addr_changed(&lpc_netif); } } }
/** \brief Update PHY status from passed value * * This function updates the current PHY status based on the * passed PHY status word. The PHY status indicate if the link * is active, the connection speed, and duplex. * * \param[in] netif NETIF structure * \param[in] linksts Status word from PHY * \return 1 if the status has changed, otherwise 0 */ static s32_t lpc_update_phy_sts(struct netif *netif, u32_t linksts) { s32_t changed = 0; /* Update link active status */ if (linksts & DP8_VALID_LINK) physts.phy_link_active = 1; else physts.phy_link_active = 0; /* Full or half duplex */ if (linksts & DP8_FULLDUPLEX) physts.phy_full_duplex = 1; else physts.phy_full_duplex = 0; /* Configure 100MBit/10MBit mode. */ if (linksts & DP8_SPEED10MBPS) physts.phy_speed_100mbs = 0; else physts.phy_speed_100mbs = 1; if (physts.phy_speed_100mbs != olddphysts.phy_speed_100mbs) { changed = 1; if (physts.phy_speed_100mbs) { /* 100MBit mode. */ lpc_emac_set_speed(1); NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 100000000); } else { /* 10MBit mode. */ lpc_emac_set_speed(0); NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 10000000); } olddphysts.phy_speed_100mbs = physts.phy_speed_100mbs; } if (physts.phy_full_duplex != olddphysts.phy_full_duplex) { changed = 1; if (physts.phy_full_duplex) lpc_emac_set_duplex(1); else lpc_emac_set_duplex(0); olddphysts.phy_full_duplex = physts.phy_full_duplex; } if (physts.phy_link_active != olddphysts.phy_link_active) { changed = 1; #if NO_SYS == 1 if (physts.phy_link_active) netif_set_link_up(netif); else netif_set_link_down(netif); #else if (physts.phy_link_active) tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_up, (void*) netif, 1); else tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_down, (void*) netif, 1); #endif olddphysts.phy_link_active = physts.phy_link_active; } return changed; }
/** * Send an IP packet to be received on the same netif (loopif-like). * The pbuf is simply copied and handed back to netif->input. * In multithreaded mode, this is done directly since netif->input must put * the packet on a queue. * In callback mode, the packet is put on an internal queue and is fed to * netif->input by netif_poll(). * * @param netif the lwip network interface structure * @param p the (IP) packet to 'send' * @return ERR_OK if the packet has been sent * ERR_MEM if the pbuf used to copy the packet couldn't be allocated */ err_t netif_loop_output(struct netif *netif, struct pbuf *p) { struct pbuf *r; err_t err; struct pbuf *last; #if LWIP_LOOPBACK_MAX_PBUFS u8_t clen = 0; #endif /* LWIP_LOOPBACK_MAX_PBUFS */ /* If we have a loopif, SNMP counters are adjusted for it, * if not they are adjusted for 'netif'. */ #if LWIP_SNMP #if LWIP_HAVE_LOOPIF struct netif *stats_if = &loop_netif; #else /* LWIP_HAVE_LOOPIF */ struct netif *stats_if = netif; #endif /* LWIP_HAVE_LOOPIF */ #endif /* LWIP_SNMP */ SYS_ARCH_DECL_PROTECT(lev); /* Allocate a new pbuf */ r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM); if (r == NULL) { LINK_STATS_INC(link.memerr); LINK_STATS_INC(link.drop); snmp_inc_ifoutdiscards(stats_if); return ERR_MEM; } #if LWIP_LOOPBACK_MAX_PBUFS clen = pbuf_clen(r); /* check for overflow or too many pbuf on queue */ if(((netif->loop_cnt_current + clen) < netif->loop_cnt_current) || ((netif->loop_cnt_current + clen) > LWIP_LOOPBACK_MAX_PBUFS)) { pbuf_free(r); LINK_STATS_INC(link.memerr); LINK_STATS_INC(link.drop); snmp_inc_ifoutdiscards(stats_if); return ERR_MEM; } netif->loop_cnt_current += clen; #endif /* LWIP_LOOPBACK_MAX_PBUFS */ /* Copy the whole pbuf queue p into the single pbuf r */ if ((err = pbuf_copy(r, p)) != ERR_OK) { pbuf_free(r); LINK_STATS_INC(link.memerr); LINK_STATS_INC(link.drop); snmp_inc_ifoutdiscards(stats_if); return err; } /* Put the packet on a linked list which gets emptied through calling netif_poll(). */ /* let last point to the last pbuf in chain r */ for (last = r; last->next != NULL; last = last->next); SYS_ARCH_PROTECT(lev); if (netif->loop_first != NULL) { LWIP_ASSERT("if first != NULL, last must also be != NULL", netif->loop_last != NULL); netif->loop_last->next = r; netif->loop_last = last; } else { netif->loop_first = r; netif->loop_last = last; } SYS_ARCH_UNPROTECT(lev); LINK_STATS_INC(link.xmit); snmp_add_ifoutoctets(stats_if, p->tot_len); snmp_inc_ifoutucastpkts(stats_if); #if LWIP_NETIF_LOOPBACK_MULTITHREADING /* For multithreading environment, schedule a call to netif_poll */ tcpip_callback_with_block((tcpip_callback_fn)netif_poll, netif, 0); #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */ return ERR_OK; }