diff --git a/include/libztDefs.h b/include/libztDefs.h index f594a71..6d2886d 100644 --- a/include/libztDefs.h +++ b/include/libztDefs.h @@ -50,6 +50,8 @@ #define STACK_PICO 0 #define NO_STACK 0 // for layer-2 only (this will omit all userspace network stack code) +#define LWIP_GUARDED_BUF_CHECK_INTERVAL 50 // in ms +#define LWIP_MAX_GUARDED_RX_BUF_SZ 1024 // number of frame pointers that can be cached waiting for receipt into core /* sanity checks for userspace network stack and socket API layer choices @@ -219,6 +221,8 @@ struct sockaddr_ll { /* lwIP */ /****************************************************************************/ +#define LWIP_NETIF_STATUS_CALLBACK 0 + // For LWIP configuration see: include/lwipopts.h #if defined(STACK_LWIP) diff --git a/src/lwIP.cpp b/src/lwIP.cpp index 6803b7a..4685aed 100644 --- a/src/lwIP.cpp +++ b/src/lwIP.cpp @@ -77,6 +77,10 @@ bool virt_can_provision_new_socket(int socket_type); struct netif lwipInterfaces[10]; int lwipInterfacesCount = 0; +ZeroTier::Mutex _rx_input_lock_m; +struct pbuf* lwip_frame_rxbuf[1024]; +int lwip_frame_rxbuf_tot = 0; + bool lwip_driver_initialized = false; ZeroTier::Mutex driver_m; @@ -112,6 +116,75 @@ static void tcpip_init_done(void *arg) sys_sem_signal(sem); } +void my_tcpip_callback(void *arg) +{ + Mutex::Lock _l(_rx_input_lock_m); + int loop_score = 16; // max num of packets to read per polling call + // TODO: Optimize (use Ringbuffer) + int pkt_num = 0; + int count_initial = lwip_frame_rxbuf_tot; + int count_final = 0; + while (lwip_frame_rxbuf_tot > 0 && loop_score > 0) { + + struct pbuf *p = lwip_frame_rxbuf[pkt_num]; + pkt_num += 1; + // DEBUG_INFO("copying received packet FROM guarded buffer (addr=%p) total frames in buffer = %d", p, lwip_frame_rxbuf_tot); + + // Packet routing logic. Inputs packet into correct lwip netif interface depending on protocol type + struct ip_hdr *iphdr; + //ip_addr_t iphdr_dest; + switch (((struct eth_hdr *)p->payload)->type) + { + case PP_HTONS(ETHTYPE_IPV6): { + iphdr = (struct ip_hdr *)((char *)p->payload + SIZEOF_ETH_HDR); + for (int i=0; ipayload + SIZEOF_ETH_HDR); + for (int i=0; idest.addr || ip4_addr_isbroadcast_u32(iphdr->dest.addr, &lwipInterfaces[i])) { + if (lwipInterfaces[i].input(p, &lwipInterfaces[i]) != ERR_OK) { + DEBUG_ERROR("packet input error (ipv4, p=%p, netif=%p)", p, &lwipInterfaces[i]); + break; + } + } + } + } + } break; + case PP_HTONS(ETHTYPE_ARP): { + for (int i=0; ipayload)->type) - { - case PP_HTONS(ETHTYPE_IPV6): { - iphdr = (struct ip_hdr *)((char *)p->payload + SIZEOF_ETH_HDR); - for (int i=0; ipayload + SIZEOF_ETH_HDR); - for (int i=0; idest.addr || ip4_addr_isbroadcast_u32(iphdr->dest.addr, &lwipInterfaces[i])) { - if (lwipInterfaces[i].input(p, &lwipInterfaces[i]) != ERR_OK) { - DEBUG_ERROR("packet input error (ipv4, p=%p, netif=%p)", p, &lwipInterfaces[i]); - break; - } - } - } - } - } break; - case PP_HTONS(ETHTYPE_ARP): { - for (int i=0; i