From 9a3f9af2db534f993b22cd71cba8f13648fd2ee9 Mon Sep 17 00:00:00 2001 From: Liu Zhi Fu Date: Mon, 28 Nov 2016 18:36:14 +0800 Subject: [PATCH 1/3] esp32/lwip: some misc changes 1. Update phy to fix HT40 rx issue 2. Add code about RX_DONE/TX_DONE/Lock-free optimization 3. Fix wifi ioctl return value error 4. Add lwip statistics debug code 5. Modify TCP window size to 10 and send buffer size to 5 --- components/esp32/lib | 2 +- components/lwip/api/api_msg.c | 12 +++++-- components/lwip/api/lwip_debug.c | 21 +++++++++++ components/lwip/api/tcpip.c | 13 +++---- components/lwip/core/stats.c | 18 ++++++++++ components/lwip/include/lwip/lwip/stats.h | 30 ++++++++++++++++ components/lwip/include/lwip/port/lwipopts.h | 37 ++++++++++++++++---- components/lwip/port/freertos/sys_arch.c | 2 ++ components/lwip/port/netif/wlanif.c | 11 ++---- 9 files changed, 122 insertions(+), 24 deletions(-) diff --git a/components/esp32/lib b/components/esp32/lib index c0804cdc87..db05e57658 160000 --- a/components/esp32/lib +++ b/components/esp32/lib @@ -1 +1 @@ -Subproject commit c0804cdc879a17774258785093be4369db79c9c4 +Subproject commit db05e57658e532d5fe6b9e9dd5e75d48955efd0a diff --git a/components/lwip/api/api_msg.c b/components/lwip/api/api_msg.c index d504bfb877..2d98734b67 100755 --- a/components/lwip/api/api_msg.c +++ b/components/lwip/api/api_msg.c @@ -128,6 +128,7 @@ recv_raw(void *arg, struct raw_pcb *pcb, struct pbuf *p, len = q->tot_len; if (sys_mbox_trypost(&conn->recvmbox, buf) != ERR_OK) { + ESP_STATS_INC(esp.rx_rawmbox_post_fail); netbuf_delete(buf); return 0; } else { @@ -203,6 +204,7 @@ recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p, len = p->tot_len; if (sys_mbox_trypost(&conn->recvmbox, buf) != ERR_OK) { + ESP_STATS_INC(esp.rx_udpmbox_post_fail); netbuf_delete(buf); return; } else { @@ -262,6 +264,7 @@ recv_tcp(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) } if (sys_mbox_trypost(&conn->recvmbox, p) != ERR_OK) { + ESP_STATS_INC(esp.rx_tcpmbox_post_fail); /* don't deallocate p: it is presented to us later again from tcp_fasttmr! */ return ERR_MEM; } else { @@ -391,12 +394,16 @@ err_tcp(void *arg, err_t err) /* pass NULL-message to recvmbox to wake up pending recv */ if (sys_mbox_valid(&conn->recvmbox)) { /* use trypost to prevent deadlock */ - sys_mbox_trypost(&conn->recvmbox, NULL); + if (sys_mbox_trypost(&conn->recvmbox, NULL) != ERR_OK){ + ESP_STATS_INC(esp.err_tcp_rxmbox_post_fail); + } } /* pass NULL-message to acceptmbox to wake up pending accept */ if (sys_mbox_valid(&conn->acceptmbox)) { /* use trypost to preven deadlock */ - sys_mbox_trypost(&conn->acceptmbox, NULL); + if (sys_mbox_trypost(&conn->acceptmbox, NULL) != ERR_OK) { + ESP_STATS_INC(esp.err_tcp_rxmbox_post_fail); + } } if ((old_state == NETCONN_WRITE) || (old_state == NETCONN_CLOSE) || @@ -476,6 +483,7 @@ accept_function(void *arg, struct tcp_pcb *newpcb, err_t err) newconn->last_err = err; if (sys_mbox_trypost(&conn->acceptmbox, newconn) != ERR_OK) { + ESP_STATS_INC(esp.acceptmbox_post_fail); /* When returning != ERR_OK, the pcb is aborted in tcp_process(), so do nothing here! */ /* remove all references to this netconn from the pcb */ diff --git a/components/lwip/api/lwip_debug.c b/components/lwip/api/lwip_debug.c index 08869149a3..89a0712b60 100644 --- a/components/lwip/api/lwip_debug.c +++ b/components/lwip/api/lwip_debug.c @@ -18,6 +18,7 @@ #include "lwip/tcp.h" #include "lwip/udp.h" #include "lwip/priv/tcp_priv.h" +#include "lwip/stats.h" #include "lwip/priv/memp_priv.h" #include "lwip/memp.h" @@ -129,6 +130,26 @@ void dbg_lwip_udp_rxtx_show(void) printf("TBC\n"); } +void dbg_lwip_stats_show(void) +{ + TCP_STATS_DISPLAY(); + UDP_STATS_DISPLAY(); + ICMP_STATS_DISPLAY(); + IGMP_STATS_DISPLAY(); + IP_STATS_DISPLAY(); + IPFRAG_STATS_DISPLAY(); + ETHARP_STATS_DISPLAY(); + LINK_STATS_DISPLAY(); + MEM_STATS_DISPLAY(); + SYS_STATS_DISPLAY(); + IP6_STATS_DISPLAY(); + ICMP6_STATS_DISPLAY(); + IP6_FRAG_STATS_DISPLAY(); + MLD6_STATS_DISPLAY(); + ND6_STATS_DISPLAY(); + ESP_STATS_DISPLAY(); +} + #if (ESP_CNT_DEBUG == 1) uint32_t g_lwip_mem_cnt[MEMP_MAX][2]; diff --git a/components/lwip/api/tcpip.c b/components/lwip/api/tcpip.c index 0ad60721e4..72de714e2a 100755 --- a/components/lwip/api/tcpip.c +++ b/components/lwip/api/tcpip.c @@ -56,9 +56,6 @@ #define TCPIP_MSG_VAR_FREE(name) API_VAR_FREE(MEMP_TCPIP_MSG_API, name) /* global variables */ -#if ESP_PERF -uint32_t g_rx_post_mbox_fail_cnt = 0; -#endif static tcpip_init_done_fn tcpip_init_done; static void *tcpip_init_done_arg; static sys_mbox_t mbox; @@ -223,9 +220,7 @@ tcpip_inpkt(struct pbuf *p, struct netif *inp, netif_input_fn input_fn) msg->msg.inp.netif = inp; msg->msg.inp.input_fn = input_fn; if (sys_mbox_trypost(&mbox, msg) != ERR_OK) { -#if ESP_PERF - g_rx_post_mbox_fail_cnt ++; -#endif + ESP_STATS_INC(esp.tcpip_inpkt_post_fail); memp_free(MEMP_TCPIP_MSG_INPKT, msg); return ERR_MEM; } @@ -282,6 +277,7 @@ tcpip_callback_with_block(tcpip_callback_fn function, void *ctx, u8_t block) sys_mbox_post(&mbox, msg); } else { if (sys_mbox_trypost(&mbox, msg) != ERR_OK) { + ESP_STATS_INC(esp.tcpip_cb_post_fail); memp_free(MEMP_TCPIP_MSG_API, msg); return ERR_MEM; } @@ -497,8 +493,13 @@ tcpip_init(tcpip_init_done_fn initfunc, void *arg) #if ESP_LWIP +#if ESP_DUAL_CORE + sys_thread_t xLwipTaskHandle = 0; + xTaskCreatePinnedToCore(tcpip_thread, TCPIP_THREAD_NAME, TCPIP_THREAD_STACKSIZE, NULL, TCPIP_THREAD_PRIO, NULL, 1); +#else sys_thread_t xLwipTaskHandle = sys_thread_new(TCPIP_THREAD_NAME , tcpip_thread, NULL, TCPIP_THREAD_STACKSIZE, TCPIP_THREAD_PRIO); +#endif printf("tcpip_task_hdlxxx : %x, prio:%d,stack:%d\n", (u32_t)xLwipTaskHandle,TCPIP_THREAD_PRIO,TCPIP_THREAD_STACKSIZE); diff --git a/components/lwip/core/stats.c b/components/lwip/core/stats.c index b47ab0b7fa..9936615918 100755 --- a/components/lwip/core/stats.c +++ b/components/lwip/core/stats.c @@ -180,5 +180,23 @@ stats_display(void) } #endif /* LWIP_STATS_DISPLAY */ +#if ESP_STATS +void stats_display_esp(struct stats_esp *esp) +{ + LWIP_PLATFORM_DIAG(("\nESP\n\t")); + LWIP_PLATFORM_DIAG(("esp.rx_rawmbox_post_fail: %"U32_F"\n\t", (u32_t)esp->rx_rawmbox_post_fail)); + LWIP_PLATFORM_DIAG(("esp.rx_udpmbox_post_fail: %"U32_F"\n\t", (u32_t)esp->rx_udpmbox_post_fail)); + LWIP_PLATFORM_DIAG(("esp.rx_tcpmbox_post_fail: %"U32_F"\n\t", (u32_t)esp->rx_tcpmbox_post_fail)); + LWIP_PLATFORM_DIAG(("esp.err_tcp_rxmbox_post_fail: %"U32_F"\n\t", (u32_t)esp->err_tcp_rxmbox_post_fail)); + LWIP_PLATFORM_DIAG(("esp.err_tcp_acceptmbox_post_fail: %"U32_F"\n\t", (u32_t)esp->err_tcp_acceptmbox_post_fail)); + LWIP_PLATFORM_DIAG(("esp.acceptmbox_post_fail: %"U32_F"\n\t", (u32_t)esp->acceptmbox_post_fail)); + LWIP_PLATFORM_DIAG(("esp.free_mbox_post_fail: %"U32_F"\n\t", (u32_t)esp->free_mbox_post_fail)); + LWIP_PLATFORM_DIAG(("esp.tcpip_inpkt_post_fail: %"U32_F"\n\t", (u32_t)esp->tcpip_inpkt_post_fail)); + LWIP_PLATFORM_DIAG(("esp.tcpip_cb_post_fail: %"U32_F"\n\t", (u32_t)esp->tcpip_cb_post_fail)); + LWIP_PLATFORM_DIAG(("esp.wlanif_input_pbuf_fail: %"U32_F"\n\t", (u32_t)esp->wlanif_input_pbuf_fail)); + LWIP_PLATFORM_DIAG(("esp.wlanif_outut_pbuf_fail: %"U32_F"\n\t", (u32_t)esp->wlanif_outut_pbuf_fail)); +} +#endif + #endif /* LWIP_STATS */ diff --git a/components/lwip/include/lwip/lwip/stats.h b/components/lwip/include/lwip/lwip/stats.h index 176723d27f..fd6a4076a7 100755 --- a/components/lwip/include/lwip/lwip/stats.h +++ b/components/lwip/include/lwip/lwip/stats.h @@ -213,6 +213,23 @@ struct stats_mib2_netif_ctrs { u32_t ifouterrors; }; +struct stats_esp { + /* mbox post fail stats */ + u32_t rx_rawmbox_post_fail; + u32_t rx_udpmbox_post_fail; + u32_t rx_tcpmbox_post_fail; + u32_t err_tcp_rxmbox_post_fail; + u32_t err_tcp_acceptmbox_post_fail; + u32_t acceptmbox_post_fail; + u32_t free_mbox_post_fail; + u32_t tcpip_inpkt_post_fail; + u32_t tcpip_cb_post_fail; + + /* memory malloc/free/failed stats */ + u32_t wlanif_input_pbuf_fail; + u32_t wlanif_outut_pbuf_fail; +}; + struct stats_ { #if LINK_STATS struct stats_proto link; @@ -265,6 +282,9 @@ struct stats_ { #if MIB2_STATS struct stats_mib2 mib2; #endif +#if ESP_STATS + struct stats_esp esp; +#endif }; extern struct stats_ lwip_stats; @@ -438,6 +458,14 @@ void stats_init(void); #define MIB2_STATS_INC(x) #endif +#if ESP_STATS +#define ESP_STATS_INC(x) STATS_INC(x) +#define ESP_STATS_DISPLAY() stats_display_esp(&lwip_stats.esp); +#else +#define ESP_STATS_INC(x) +#define ESP_STATS_DISPLAY() +#endif + /* Display of statistics */ #if LWIP_STATS_DISPLAY void stats_display(void); @@ -446,6 +474,7 @@ void stats_display_igmp(struct stats_igmp *igmp, const char *name); void stats_display_mem(struct stats_mem *mem, const char *name); void stats_display_memp(struct stats_mem *mem, int index); void stats_display_sys(struct stats_sys *sys); +void stats_display_esp(struct stats_esp *esp); #else /* LWIP_STATS_DISPLAY */ #define stats_display() #define stats_display_proto(proto, name) @@ -453,6 +482,7 @@ void stats_display_sys(struct stats_sys *sys); #define stats_display_mem(mem, name) #define stats_display_memp(mem, index) #define stats_display_sys(sys) +#define stats_display_esp(esp) #endif /* LWIP_STATS_DISPLAY */ #ifdef __cplusplus diff --git a/components/lwip/include/lwip/port/lwipopts.h b/components/lwip/include/lwip/port/lwipopts.h index be93e07a8c..91c2fa4be9 100755 --- a/components/lwip/include/lwip/port/lwipopts.h +++ b/components/lwip/include/lwip/port/lwipopts.h @@ -379,22 +379,21 @@ * The queue size value itself is platform-dependent, but is passed to * sys_mbox_new() when tcpip_init is called. */ -#define TCPIP_MBOX_SIZE 16 +#define TCPIP_MBOX_SIZE 32 /** * DEFAULT_UDP_RECVMBOX_SIZE: The mailbox size for the incoming packets on a * NETCONN_UDP. The queue size value itself is platform-dependent, but is passed * to sys_mbox_new() when the recvmbox is created. */ -#define DEFAULT_UDP_RECVMBOX_SIZE 16 +#define DEFAULT_UDP_RECVMBOX_SIZE 6 /** * DEFAULT_TCP_RECVMBOX_SIZE: The mailbox size for the incoming packets on a * NETCONN_TCP. The queue size value itself is platform-dependent, but is passed * to sys_mbox_new() when the recvmbox is created. */ -#define DEFAULT_TCP_RECVMBOX_SIZE 16 -//#define DEFAULT_TCP_RECVMBOX_SIZE 6 +#define DEFAULT_TCP_RECVMBOX_SIZE 6 /** * DEFAULT_ACCEPTMBOX_SIZE: The mailbox size for the incoming connections. @@ -466,7 +465,7 @@ /** * LWIP_STATS==1: Enable statistics collection in lwip_stats. */ -#define LWIP_STATS 0 +#define LWIP_STATS 1 /* --------------------------------- @@ -556,6 +555,29 @@ */ #define TCPIP_DEBUG LWIP_DBG_OFF +/** + * statistic debugs + */ +#define TCP_STATS LWIP_DBG_ON +#define UDP_STATS LWIP_DBG_ON +#define ICMP_STATS LWIP_DBG_ON +#define IGMP_STATS LWIP_DBG_ON +#define IP_STATS LWIP_DBG_ON +#define IPFRAG_STATS LWIP_DBG_ON +#define ETHARP_STATS LWIP_DBG_ON +#define LINK_STATS LWIP_DBG_ON +#define MEM_STATS LWIP_DBG_ON +#define MEMM_STATS LWIP_DBG_ON +#define SYS_STATS LWIP_DBG_ON +#define IP6_STATS LWIP_DBG_ON +#define IP6_FRAG_STATS LWIP_DBG_ON +#define ICMP6_STATS LWIP_DBG_ON +#define MLD6_STATS LWIP_DBG_ON +#define ND6_STATS LWIP_DBG_ON +#define MIB2_STATS LWIP_DBG_ON +#define ESP_STATS LWIP_DBG_ON +#define LWIP_STATS_DISPLAY LWIP_DBG_ON + /* Enable all Espressif-only options */ #define ESP_LWIP 1 @@ -572,9 +594,10 @@ #define ESP_LIGHT_SLEEP 1 #define ESP_L2_TO_L3_COPY CONFIG_L2_TO_L3_COPY #define ESP_CNT_DEBUG 0 +#define ESP_DUAL_CORE 0 -#define TCP_WND_DEFAULT (4*TCP_MSS) -#define TCP_SND_BUF_DEFAULT (2*TCP_MSS) +#define TCP_WND_DEFAULT (10*TCP_MSS) +#define TCP_SND_BUF_DEFAULT (5*TCP_MSS) #if ESP_PER_SOC_TCP_WND #define TCP_WND(pcb) (pcb->per_soc_tcp_wnd) diff --git a/components/lwip/port/freertos/sys_arch.c b/components/lwip/port/freertos/sys_arch.c index 15ba3011d9..8d0dd08c58 100755 --- a/components/lwip/port/freertos/sys_arch.c +++ b/components/lwip/port/freertos/sys_arch.c @@ -37,6 +37,7 @@ #include "lwip/sys.h" #include "lwip/mem.h" #include "arch/sys_arch.h" +#include "lwip/stats.h" /* This is the number of threads that can be started with sys_thread_new() */ #define SYS_THREAD_MAX 4 @@ -370,6 +371,7 @@ sys_mbox_free(sys_mbox_t *mbox) if (post_null){ LWIP_DEBUGF(ESP_THREAD_SAFE_DEBUG, ("sys_mbox_free: post null to mbox\n")); if (sys_mbox_trypost( mbox, NULL) != ERR_OK){ + ESP_STATS_INC(esp.free_mbox_post_fail); LWIP_DEBUGF(ESP_THREAD_SAFE_DEBUG, ("sys_mbox_free: post null mbox fail\n")); } else { post_null = false; diff --git a/components/lwip/port/netif/wlanif.c b/components/lwip/port/netif/wlanif.c index f5d34179af..7a5464b2da 100755 --- a/components/lwip/port/netif/wlanif.c +++ b/components/lwip/port/netif/wlanif.c @@ -161,12 +161,9 @@ wlanif_input(struct netif *netif, void *buffer, u16_t len, void* eb) goto _exit; #if (ESP_L2_TO_L3_COPY == 1) - //p = pbuf_alloc(PBUF_IP, len, PBUF_POOL); p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM); if (p == NULL) { - #if ESP_PERF - g_rx_alloc_pbuf_fail_cnt++; - #endif + ESP_STATS_INC(esp.wlanif_input_pbuf_fail); esp_wifi_internal_free_rx_buffer(eb); return; } @@ -175,9 +172,7 @@ wlanif_input(struct netif *netif, void *buffer, u16_t len, void* eb) #else p = pbuf_alloc(PBUF_RAW, len, PBUF_REF); if (p == NULL){ - #if ESP_PERF - g_rx_alloc_pbuf_fail_cnt++; - #endif + ESP_STATS_INC(esp.wlanif_input_pbuf_fail); return; } p->payload = buffer; @@ -241,7 +236,7 @@ wlanif_init(struct netif *netif) * The last argument should be replaced with your link speed, in units * of bits per second. */ - NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS); + NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 100); netif->name[0] = IFNAME0; netif->name[1] = IFNAME1; From fc2883a59aa5b8a6c9b49f5ce087ca92b0ce3204 Mon Sep 17 00:00:00 2001 From: Liu Zhi Fu Date: Mon, 28 Nov 2016 19:54:22 +0800 Subject: [PATCH 2/3] lwip: modify according code review 1. Modify tcp window size/send buffer back to 4/2 2. Disable lwip statistics by default --- components/lwip/include/lwip/port/lwipopts.h | 28 +++----------------- 1 file changed, 3 insertions(+), 25 deletions(-) diff --git a/components/lwip/include/lwip/port/lwipopts.h b/components/lwip/include/lwip/port/lwipopts.h index 91c2fa4be9..4475cd4e99 100755 --- a/components/lwip/include/lwip/port/lwipopts.h +++ b/components/lwip/include/lwip/port/lwipopts.h @@ -465,7 +465,7 @@ /** * LWIP_STATS==1: Enable statistics collection in lwip_stats. */ -#define LWIP_STATS 1 +#define LWIP_STATS 0 /* --------------------------------- @@ -555,28 +555,6 @@ */ #define TCPIP_DEBUG LWIP_DBG_OFF -/** - * statistic debugs - */ -#define TCP_STATS LWIP_DBG_ON -#define UDP_STATS LWIP_DBG_ON -#define ICMP_STATS LWIP_DBG_ON -#define IGMP_STATS LWIP_DBG_ON -#define IP_STATS LWIP_DBG_ON -#define IPFRAG_STATS LWIP_DBG_ON -#define ETHARP_STATS LWIP_DBG_ON -#define LINK_STATS LWIP_DBG_ON -#define MEM_STATS LWIP_DBG_ON -#define MEMM_STATS LWIP_DBG_ON -#define SYS_STATS LWIP_DBG_ON -#define IP6_STATS LWIP_DBG_ON -#define IP6_FRAG_STATS LWIP_DBG_ON -#define ICMP6_STATS LWIP_DBG_ON -#define MLD6_STATS LWIP_DBG_ON -#define ND6_STATS LWIP_DBG_ON -#define MIB2_STATS LWIP_DBG_ON -#define ESP_STATS LWIP_DBG_ON -#define LWIP_STATS_DISPLAY LWIP_DBG_ON /* Enable all Espressif-only options */ @@ -596,8 +574,8 @@ #define ESP_CNT_DEBUG 0 #define ESP_DUAL_CORE 0 -#define TCP_WND_DEFAULT (10*TCP_MSS) -#define TCP_SND_BUF_DEFAULT (5*TCP_MSS) +#define TCP_WND_DEFAULT (4*TCP_MSS) +#define TCP_SND_BUF_DEFAULT (2*TCP_MSS) #if ESP_PER_SOC_TCP_WND #define TCP_WND(pcb) (pcb->per_soc_tcp_wnd) From 5b4361f60a89651854f90f9121cd05cd27b3de22 Mon Sep 17 00:00:00 2001 From: Liu Zhi Fu Date: Mon, 28 Nov 2016 20:11:43 +0800 Subject: [PATCH 3/3] esp32: update wifi lib to add tw8913 fix dfe73737 - tw8913 wakeup first before scan --- components/esp32/lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/esp32/lib b/components/esp32/lib index db05e57658..8fd09dcc55 160000 --- a/components/esp32/lib +++ b/components/esp32/lib @@ -1 +1 @@ -Subproject commit db05e57658e532d5fe6b9e9dd5e75d48955efd0a +Subproject commit 8fd09dcc550f66385edaa19097df66d6048979b5