diff --git a/include/libztDefs.h b/include/libztDefs.h index 6d2886d..f1300b7 100644 --- a/include/libztDefs.h +++ b/include/libztDefs.h @@ -50,8 +50,18 @@ #define STACK_PICO 0 #define NO_STACK 0 // for layer-2 only (this will omit all userspace network stack code) +/* The following three quantities are related and govern how incoming frames are fed into the +network stack's core: + +Every LWIP_GUARDED_BUF_CHECK_INTERVAL milliseconds, a callback will be called from the core and +will input a maximum of LWIP_FRAMES_HANDLED_PER_CORE_CALL frames before returning control back +to the core. Meanwhile, incoming frames from the ZeroTier wire will be allocated and their +pointers will be cached in the receive frame buffer of the size LWIP_MAX_GUARDED_RX_BUF_SZ to +await the next callback from the core */ + #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 +#define LWIP_FRAMES_HANDLED_PER_CORE_CALL 16 /* sanity checks for userspace network stack and socket API layer choices diff --git a/src/lwIP.cpp b/src/lwIP.cpp index 4685aed..f9a86cc 100644 --- a/src/lwIP.cpp +++ b/src/lwIP.cpp @@ -78,7 +78,7 @@ struct netif lwipInterfaces[10]; int lwipInterfacesCount = 0; ZeroTier::Mutex _rx_input_lock_m; -struct pbuf* lwip_frame_rxbuf[1024]; +struct pbuf* lwip_frame_rxbuf[MAX_GUARDED_RX_BUF_SZ]; int lwip_frame_rxbuf_tot = 0; @@ -119,20 +119,15 @@ static void tcpip_init_done(void *arg) 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 + int loop_score = LWIP_FRAMES_HANDLED_PER_CORE_CALL; // 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); - + pkt_num++; // 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): { @@ -175,16 +170,14 @@ void my_tcpip_callback(void *arg) default: break; } - lwip_frame_rxbuf_tot-=1; + lwip_frame_rxbuf_tot--;; loop_score--; } - count_final = count_initial - lwip_frame_rxbuf_tot; - //DEBUG_INFO("adjusting buffer by (%d) elements, total frames in buffer = %d", count_final, lwip_frame_rxbuf_tot); - // move pbuf address buffer by the number of packets successfully fed into the stack core + int count_final = count_initial - lwip_frame_rxbuf_tot; + // move pbuf frame pointer address buffer by the number of frames successfully fed into the stack core memmove(lwip_frame_rxbuf, lwip_frame_rxbuf + count_final, sizeof(lwip_frame_rxbuf) - count_final); } - // main thread which starts the initialization process static void main_thread(void *arg) { @@ -198,8 +191,7 @@ static void main_thread(void *arg) sys_sem_wait(&sem); DEBUG_EXTRA("stack thread init complete"); - while(1) - { + while(1) { usleep(LWIP_GUARDED_BUF_CHECK_INTERVAL*1000); // Handle incoming packets from the core's thread context. // If you feed frames into the core directly you will violate the core's thread model @@ -223,8 +215,6 @@ void lwip_driver_init() NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO); } - - err_t lwip_eth_tx(struct netif *netif, struct pbuf *p) { struct pbuf *q; @@ -316,11 +306,10 @@ void lwip_eth_rx(VirtualTap *tap, const ZeroTier::MAC &from, const ZeroTier::MAC } Mutex::Lock _l(_rx_input_lock_m); if (lwip_frame_rxbuf_tot == LWIP_MAX_GUARDED_RX_BUF_SZ) { - //DEBUG_ERROR("guarded receive buffer full, adjust MAX_GUARDED_RX_BUF_SZ or "); + DEBUG_ERROR("guarded receive buffer full, adjust MAX_GUARDED_RX_BUF_SZ or LWIP_GUARDED_BUF_CHECK_INTERVAL"); } lwip_frame_rxbuf[lwip_frame_rxbuf_tot] = p; lwip_frame_rxbuf_tot += 1; - //DEBUG_INFO("copying received packet TO guarded buffer (addr=%p) total frames in buffer = %d", p, lwip_frame_rxbuf_tot); } /*