From 69dbc36a1c9338d5829256c8bf69cffaebbb83a8 Mon Sep 17 00:00:00 2001 From: Liu Zhi Fu Date: Wed, 16 Nov 2016 16:24:41 +0800 Subject: [PATCH 1/4] lwip: add option to memcopy packet from L2 to L3 Menuconfig add an option to copy the packet from layer2 (WIFI driver) to layer3 (LWIP), default not copy --- components/lwip/Kconfig | 10 +++++ components/lwip/include/lwip/lwip/opt.h | 7 ++++ components/lwip/include/lwip/port/lwipopts.h | 1 + components/lwip/port/netif/wlanif.c | 41 +++++++++----------- 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/components/lwip/Kconfig b/components/lwip/Kconfig index 801fa0b51..90b7c5b5c 100644 --- a/components/lwip/Kconfig +++ b/components/lwip/Kconfig @@ -1,5 +1,15 @@ menu "LWIP" +config L2_TO_L3_COPY + bool "Enable copy between Layer2 and Layer3 packets" + default 0 + help + If this feature is enabled, then all traffic from layer2(WIFI Driver) + to layer3(LWIP stack) will make a copy, the layer2 buffer will be + freed and the copy will be sent to layer3. Please make sure you fully + understand this feature before you enable this feature. + + config LWIP_MAX_SOCKETS int "Max number of open sockets" range 1 16 diff --git a/components/lwip/include/lwip/lwip/opt.h b/components/lwip/include/lwip/lwip/opt.h index 51d340e00..9a1d10afb 100755 --- a/components/lwip/include/lwip/lwip/opt.h +++ b/components/lwip/include/lwip/lwip/opt.h @@ -3008,6 +3008,13 @@ #define LWIP_PERF 0 #endif +/** + * ESP_L2_TO_L3_COPY: enable memcpy when receiving packet from L2 + */ +#ifndef ESP_L2_TO_L3_COPY +#define ESP_L2_TO_L3_COPY 1 +#endif + #ifndef ESP_THREAD_SAFE_DEBUG #define ESP_THREAD_SAFE_DEBUG 0 #endif diff --git a/components/lwip/include/lwip/port/lwipopts.h b/components/lwip/include/lwip/port/lwipopts.h index d06e75685..26bdc3a4e 100755 --- a/components/lwip/include/lwip/port/lwipopts.h +++ b/components/lwip/include/lwip/port/lwipopts.h @@ -525,6 +525,7 @@ extern unsigned long os_random(void); #define ESP_RANDOM_TCP_PORT 1 #define ESP_IP4_ATON 1 #define ESP_LIGHT_SLEEP 1 +#define ESP_L2_TO_L3_COPY CONFIG_L2_TO_L3_COPY #define TCP_WND_DEFAULT (4*TCP_MSS) #define TCP_SND_BUF_DEFAULT (2*TCP_MSS) diff --git a/components/lwip/port/netif/wlanif.c b/components/lwip/port/netif/wlanif.c index ffad69cd4..3fd2a3192 100755 --- a/components/lwip/port/netif/wlanif.c +++ b/components/lwip/port/netif/wlanif.c @@ -161,40 +161,37 @@ low_level_output(struct netif *netif, struct pbuf *p) * @param netif the lwip network interface structure for this ethernetif */ void -#if ESP_LWIP wlanif_input(struct netif *netif, void *buffer, u16_t len, void* eb) -#else -wlanif_input(struct netif *netif, void *buffer, uint16 len) -#endif { struct pbuf *p; -#if ESP_LWIP - if(buffer== NULL) + if(!buffer || !netif) goto _exit; - if(netif == NULL) - goto _exit; -#endif -#if ESP_LWIP - p = pbuf_alloc(PBUF_RAW, len, PBUF_REF); - if (p == NULL){ -#if ESP_PERF - g_rx_alloc_pbuf_fail_cnt++; -#endif - return; - } - p->payload = buffer; - p->eb = eb; -#else - p = pbuf_alloc(PBUF_IP, len, PBUF_POOL); +#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_wifi_internal_free_rx_buffer(eb); return; } memcpy(p->payload, buffer, len); + esp_wifi_internal_free_rx_buffer(eb); +#else + p = pbuf_alloc(PBUF_RAW, len, PBUF_REF); + if (p == NULL){ + #if ESP_PERF + g_rx_alloc_pbuf_fail_cnt++; + #endif + return; + } + p->payload = buffer; + p->eb = eb; #endif - /* full packet send to tcpip_thread to process */ if (netif->input(p, netif) != ERR_OK) { LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n")); From 0b2cea6e9f757191be0bfc83a6fe3ed15981ccf2 Mon Sep 17 00:00:00 2001 From: Liu Zhi Fu Date: Wed, 16 Nov 2016 21:11:17 +0800 Subject: [PATCH 2/4] lwip: modify menuconfig comments according to review --- components/lwip/Kconfig | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/components/lwip/Kconfig b/components/lwip/Kconfig index 90b7c5b5c..7661fe6cb 100644 --- a/components/lwip/Kconfig +++ b/components/lwip/Kconfig @@ -6,9 +6,14 @@ config L2_TO_L3_COPY help If this feature is enabled, then all traffic from layer2(WIFI Driver) to layer3(LWIP stack) will make a copy, the layer2 buffer will be - freed and the copy will be sent to layer3. Please make sure you fully - understand this feature before you enable this feature. - + freed and the copy will be sent to layer3. Please be notified that the + total layer2 receiving buffer is fixed and ESP32 currently supports 25 + layer2 receiving buffer, when layer2 buffer runs out of memory, then the + incoming packets will be dropped in hardware. The layer3 buffer is + allocated from the heap, so the total layer3 receiving buffer depends + on the available heap size, when heap runs out of memory, no copy will + be sent to layer3 and packet will be dropped in layer2. Please make sure + you fully understand the impact of this feature before enabling it. config LWIP_MAX_SOCKETS int "Max number of open sockets" From ceea97495f5070e50ba3b36de3b9f1a0afcf5226 Mon Sep 17 00:00:00 2001 From: Liu Zhi Fu Date: Thu, 17 Nov 2016 10:22:20 +0800 Subject: [PATCH 3/4] lwip:refractor to the description about this menuconfig option --- components/lwip/Kconfig | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/components/lwip/Kconfig b/components/lwip/Kconfig index 7661fe6cb..bf7bff15b 100644 --- a/components/lwip/Kconfig +++ b/components/lwip/Kconfig @@ -4,16 +4,17 @@ config L2_TO_L3_COPY bool "Enable copy between Layer2 and Layer3 packets" default 0 help - If this feature is enabled, then all traffic from layer2(WIFI Driver) - to layer3(LWIP stack) will make a copy, the layer2 buffer will be - freed and the copy will be sent to layer3. Please be notified that the - total layer2 receiving buffer is fixed and ESP32 currently supports 25 - layer2 receiving buffer, when layer2 buffer runs out of memory, then the - incoming packets will be dropped in hardware. The layer3 buffer is - allocated from the heap, so the total layer3 receiving buffer depends - on the available heap size, when heap runs out of memory, no copy will - be sent to layer3 and packet will be dropped in layer2. Please make sure - you fully understand the impact of this feature before enabling it. + If this feature is enabled, all traffic from layer2(WIFI Driver) will be + copied to a new buffer before sending it to layer3(LWIP stack), freeing + the layer2 buffer. + Please be notified that the total layer2 receiving buffer is fixed and + ESP32 currently supports 25 layer2 receiving buffer, when layer2 buffer + runs out of memory, then the incoming packets will be dropped in hardware. + The layer3 buffer is allocated from the heap, so the total layer3 receiving + buffer depends on the available heap size, when heap runs out of memory, + no copy will be sent to layer3 and packet will be dropped in layer2. + Please make sure you fully understand the impact of this feature before + enabling it. config LWIP_MAX_SOCKETS int "Max number of open sockets" From 5e428f21b6e07825ce041acc9a4b84bdeddb1dc9 Mon Sep 17 00:00:00 2001 From: Liu Zhi Fu Date: Thu, 17 Nov 2016 10:53:00 +0800 Subject: [PATCH 4/4] lwip: default ESP_L2_TO_L3_COPY to 0 --- components/lwip/include/lwip/lwip/opt.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/lwip/include/lwip/lwip/opt.h b/components/lwip/include/lwip/lwip/opt.h index 9a1d10afb..c42f3cd73 100755 --- a/components/lwip/include/lwip/lwip/opt.h +++ b/components/lwip/include/lwip/lwip/opt.h @@ -3012,7 +3012,7 @@ * ESP_L2_TO_L3_COPY: enable memcpy when receiving packet from L2 */ #ifndef ESP_L2_TO_L3_COPY -#define ESP_L2_TO_L3_COPY 1 +#define ESP_L2_TO_L3_COPY 0 #endif #ifndef ESP_THREAD_SAFE_DEBUG