From 368d623e1af2a43a08a5ec1b5983a75b70ca4071 Mon Sep 17 00:00:00 2001 From: liu zhifu Date: Thu, 13 Feb 2020 14:26:48 +0800 Subject: [PATCH 1/7] esp_wifi: optimize WiFi TX performance --- components/esp_common/src/esp_err_to_name.c | 6 ++ components/esp_netif/include/esp_netif.h | 16 ++++ .../esp_netif/include/esp_netif_net_stack.h | 14 ++++ .../esp_netif/include/esp_netif_types.h | 1 + components/esp_netif/lwip/esp_netif_lwip.c | 19 +++++ .../esp_netif/lwip/esp_netif_lwip_internal.h | 1 + components/esp_wifi/Kconfig | 16 +++- .../esp_wifi/include/esp_private/wifi.h | 78 +++++++++++++++---- components/esp_wifi/include/esp_wifi.h | 19 +++-- components/esp_wifi/lib | 2 +- components/esp_wifi/src/wifi_default.c | 4 + components/esp_wifi/src/wifi_init.c | 3 + components/esp_wifi/src/wifi_netif.c | 11 +++ components/lwip/port/esp32/netif/wlanif.c | 16 +++- 14 files changed, 178 insertions(+), 28 deletions(-) diff --git a/components/esp_common/src/esp_err_to_name.c b/components/esp_common/src/esp_err_to_name.c index daf22a1ecd..5d073e9508 100644 --- a/components/esp_common/src/esp_err_to_name.c +++ b/components/esp_common/src/esp_err_to_name.c @@ -354,6 +354,12 @@ static const esp_err_msg_t esp_err_msg_table[] = { # endif # ifdef ESP_ERR_WIFI_STOP_STATE ERR_TBL_IT(ESP_ERR_WIFI_STOP_STATE), /* 12308 0x3014 Returned when WiFi is stopping */ +# endif +# ifdef ESP_ERR_WIFI_NOT_ASSOC + ERR_TBL_IT(ESP_ERR_WIFI_NOT_ASSOC), /* 12309 0x3015 The WiFi connection is not associated */ +# endif +# ifdef ESP_ERR_WIFI_TX_DISALLOW + ERR_TBL_IT(ESP_ERR_WIFI_TX_DISALLOW), /* 12310 0x3016 The WiFi TX is disallowed */ # endif // components/wpa_supplicant/include/esp_supplicant/esp_wps.h # ifdef ESP_ERR_WIFI_REGISTRAR diff --git a/components/esp_netif/include/esp_netif.h b/components/esp_netif/include/esp_netif.h index a8df60e674..5cb6e8029b 100644 --- a/components/esp_netif/include/esp_netif.h +++ b/components/esp_netif/include/esp_netif.h @@ -815,6 +815,22 @@ esp_netif_t *esp_netif_next(esp_netif_t *esp_netif); */ size_t esp_netif_get_nr_of_ifs(void); +/** + * @brief increase the reference counter of net stack buffer + * + * @param[in] netstack_buf the net stack buffer + * + */ +void esp_netif_netstack_buf_ref(void *netstack_buf); + +/** + * @brief free the netstack buffer + * + * @param[in] netstack_buf the net stack buffer + * + */ +void esp_netif_netstack_buf_free(void *netstack_buf); + /** * @} */ diff --git a/components/esp_netif/include/esp_netif_net_stack.h b/components/esp_netif/include/esp_netif_net_stack.h index c38297a9f7..ab6d6a07fd 100644 --- a/components/esp_netif/include/esp_netif_net_stack.h +++ b/components/esp_netif/include/esp_netif_net_stack.h @@ -69,6 +69,20 @@ void* esp_netif_get_netif_impl(esp_netif_t *esp_netif); */ esp_err_t esp_netif_transmit(esp_netif_t *esp_netif, void* data, size_t len); +/** + * @brief Outputs packets from the TCP/IP stack to the media to be transmitted + * + * This function gets called from network stack to output packets to IO driver. + * + * @param[in] esp_netif Handle to esp-netif instance + * @param[in] data Data to be transmitted + * @param[in] len Length of the data frame + * @param[in] netstack_buf net stack buffer + * + * @return ESP_OK on success, an error passed from the I/O driver otherwise + */ +esp_err_t esp_netif_transmit_wrap(esp_netif_t *esp_netif, void *data, size_t len, void *netstack_buf); + /** * @brief Free the rx buffer allocated by the media driver * diff --git a/components/esp_netif/include/esp_netif_types.h b/components/esp_netif/include/esp_netif_types.h index 0e3613932b..036655da4b 100644 --- a/components/esp_netif/include/esp_netif_types.h +++ b/components/esp_netif/include/esp_netif_types.h @@ -184,6 +184,7 @@ typedef struct esp_netif_driver_base_s { struct esp_netif_driver_ifconfig { esp_netif_iodriver_handle handle; esp_err_t (*transmit)(void *h, void *buffer, size_t len); + esp_err_t (*transmit_wrap)(void *h, void *buffer, size_t len, void *netstack_buffer); void (*driver_free_rx_buffer)(void *h, void* buffer); }; diff --git a/components/esp_netif/lwip/esp_netif_lwip.c b/components/esp_netif/lwip/esp_netif_lwip.c index e8f34c24be..ec183f37a6 100644 --- a/components/esp_netif/lwip/esp_netif_lwip.c +++ b/components/esp_netif/lwip/esp_netif_lwip.c @@ -368,6 +368,9 @@ static esp_err_t esp_netif_init_configuration(esp_netif_t *esp_netif, const esp_ if (esp_netif_driver_config->transmit) { esp_netif->driver_transmit = esp_netif_driver_config->transmit; } + if (esp_netif_driver_config->transmit_wrap) { + esp_netif->driver_transmit_wrap = esp_netif_driver_config->transmit_wrap; + } if (esp_netif_driver_config->driver_free_rx_buffer) { esp_netif->driver_free_rx_buffer = esp_netif_driver_config->driver_free_rx_buffer; } @@ -522,6 +525,7 @@ esp_err_t esp_netif_set_driver_config(esp_netif_t *esp_netif, } esp_netif->driver_handle = driver_config->handle; esp_netif->driver_transmit = driver_config->transmit; + esp_netif->driver_transmit_wrap = driver_config->transmit_wrap; esp_netif->driver_free_rx_buffer = driver_config->driver_free_rx_buffer; return ESP_OK; } @@ -728,6 +732,16 @@ esp_err_t esp_netif_stop(esp_netif_t *esp_netif) return esp_netif_lwip_ipc_call(esp_netif_stop_api, esp_netif, NULL); } +void esp_netif_netstack_buf_ref(void *pbuf) +{ + pbuf_ref(pbuf); +} + +void esp_netif_netstack_buf_free(void *pbuf) +{ + pbuf_free(pbuf); +} + // // IO translate functions // @@ -742,6 +756,11 @@ esp_err_t esp_netif_transmit(esp_netif_t *esp_netif, void* data, size_t len) return (esp_netif->driver_transmit)(esp_netif->driver_handle, data, len); } +esp_err_t esp_netif_transmit_wrap(esp_netif_t *esp_netif, void *data, size_t len, void *pbuf) +{ + return (esp_netif->driver_transmit_wrap)(esp_netif->driver_handle, data, len, pbuf); +} + esp_err_t esp_netif_receive(esp_netif_t *esp_netif, void *buffer, size_t len, void *eb) { esp_netif->lwip_input_fn(esp_netif->netif_handle, buffer, len, eb); diff --git a/components/esp_netif/lwip/esp_netif_lwip_internal.h b/components/esp_netif/lwip/esp_netif_lwip_internal.h index 853b0629e8..fd802b0e29 100644 --- a/components/esp_netif/lwip/esp_netif_lwip_internal.h +++ b/components/esp_netif/lwip/esp_netif_lwip_internal.h @@ -81,6 +81,7 @@ struct esp_netif_obj { // io driver related void* driver_handle; esp_err_t (*driver_transmit)(void *h, void *buffer, size_t len); + esp_err_t (*driver_transmit_wrap)(void *h, void *buffer, size_t len, void *pbuf); void (*driver_free_rx_buffer)(void *h, void* buffer); // dhcp related diff --git a/components/esp_wifi/Kconfig b/components/esp_wifi/Kconfig index 74af9ba6e6..661cae116b 100644 --- a/components/esp_wifi/Kconfig +++ b/components/esp_wifi/Kconfig @@ -90,6 +90,19 @@ menu "Wi-Fi" layer can deliver frames faster than WiFi layer can transmit. In these cases, we may run out of TX buffers. + config ESP32_WIFI_CACHE_TX_BUFFER_NUM + int "Max number of WiFi cache TX buffers" + depends on (ESP32_SPIRAM_SUPPORT || ESP32S2_SPIRAM_SUPPORT) + range 16 128 + default 32 + help + Set the number of WiFi cache TX buffer number. + + For each TX packet from uplayer, such as LWIP etc, WiFi driver needs to allocate a static TX + buffer and makes a copy of uplayer packet. If WiFi driver fails to allocate the static TX buffer, + it caches the uplayer packets to a dedicated buffer queue, this option is used to configure the + size of the cached TX queue. + config ESP32_WIFI_DYNAMIC_TX_BUFFER_NUM int "Max number of WiFi dynamic TX buffers" depends on ESP32_WIFI_DYNAMIC_TX_BUFFER @@ -139,8 +152,7 @@ menu "Wi-Fi" config ESP32_WIFI_RX_BA_WIN int "WiFi AMPDU RX BA window size" depends on ESP32_WIFI_AMPDU_RX_ENABLED - range 2 32 if !SPIRAM_TRY_ALLOCATE_WIFI_LWIP - range 16 32 if SPIRAM_TRY_ALLOCATE_WIFI_LWIP + range 2 32 default 6 if !SPIRAM_TRY_ALLOCATE_WIFI_LWIP default 16 if SPIRAM_TRY_ALLOCATE_WIFI_LWIP help diff --git a/components/esp_wifi/include/esp_private/wifi.h b/components/esp_wifi/include/esp_private/wifi.h index 8d21c64e8e..04a306fbea 100644 --- a/components/esp_wifi/include/esp_private/wifi.h +++ b/components/esp_wifi/include/esp_private/wifi.h @@ -119,15 +119,6 @@ esp_err_t esp_wifi_init_internal(const wifi_init_config_t *config); */ esp_err_t esp_wifi_deinit_internal(void); -/** - * @brief get whether the wifi driver is allowed to transmit data or not - * - * @return - * - true : upper layer should stop to transmit data to wifi driver - * - false : upper layer can transmit data to wifi driver - */ -bool esp_wifi_internal_tx_is_stop(void); - /** * @brief free the rx buffer which allocated by wifi driver * @@ -138,18 +129,79 @@ void esp_wifi_internal_free_rx_buffer(void* buffer); /** * @brief transmit the buffer via wifi driver * + * This API makes a copy of the input buffer and then forwards the buffer + * copy to WiFi driver. + * * @param wifi_interface_t wifi_if : wifi interface id * @param void *buffer : the buffer to be tansmit * @param uint16_t len : the length of buffer * * @return - * - ERR_OK : Successfully transmit the buffer to wifi driver - * - ERR_MEM : Out of memory - * - ERR_IF : WiFi driver error - * - ERR_ARG : Invalid argument + * - ESP_OK : Successfully transmit the buffer to wifi driver + * - ESP_ERR_NO_MEM: out of memory + * - ESP_ERR_WIFI_ARG: invalid argument + * - ESP_ERR_WIFI_IF : WiFi interface is invalid + * - ESP_ERR_WIFI_CONN : WiFi interface is not created, e.g. send the data to STA while WiFi mode is AP mode + * - ESP_ERR_WIFI_NOT_STARTED : WiFi is not started + * - ESP_ERR_WIFI_STATE : WiFi internal state is not ready, e.g. WiFi is not started + * - ESP_ERR_WIFI_NOT_ASSOC : WiFi is not associated + * - ESP_ERR_WIFI_TX_DISALLOW : WiFi TX is disallowed, e.g. WiFi hasn't pass the authentication + * - ESP_ERR_WIFI_POST : caller fails to post event to WiFi task */ int esp_wifi_internal_tx(wifi_interface_t wifi_if, void *buffer, uint16_t len); +/** + * @brief The net stack buffer reference counter callback function + * + */ +typedef void (*wifi_netstack_buf_ref_cb_t)(void *netstack_buf); + +/** + * @brief The net stack buffer free callback function + * + */ +typedef void (*wifi_netstack_buf_free_cb_t)(void *netstack_buf); + +/** + * @brief transmit the buffer by reference via wifi driver + * + * This API firstly increases the reference counter of the input buffer and + * then forwards the buffer to WiFi driver. The WiFi driver will free the buffer + * after processing it. Use esp_wifi_internal_tx() if the uplayer buffer doesn't + * supports reference counter. + * + * @param wifi_if : wifi interface id + * @param buffer : the buffer to be tansmit + * @param len : the length of buffer + * @param netstack_buf : the netstack buffer related to bufffer + * + * @return + * - ESP_OK : Successfully transmit the buffer to wifi driver + * - ESP_ERR_NO_MEM: out of memory + * - ESP_ERR_WIFI_ARG: invalid argument + * - ESP_ERR_WIFI_IF : WiFi interface is invalid + * - ESP_ERR_WIFI_CONN : WiFi interface is not created, e.g. send the data to STA while WiFi mode is AP mode + * - ESP_ERR_WIFI_NOT_STARTED : WiFi is not started + * - ESP_ERR_WIFI_STATE : WiFi internal state is not ready, e.g. WiFi is not started + * - ESP_ERR_WIFI_NOT_ASSOC : WiFi is not associated + * - ESP_ERR_WIFI_TX_DISALLOW : WiFi TX is disallowed, e.g. WiFi hasn't pass the authentication + * - ESP_ERR_WIFI_POST : caller fails to post event to WiFi task + */ +esp_err_t esp_wifi_internal_tx_by_ref(wifi_interface_t ifx, void *buffer, size_t len, void *netstack_buf); + +/** + * @brief register the net stack buffer reference increasing and free callback + * + * @param ref : net stack buffer reference callback + * @param free: net stack buffer free callback + * + * @return + * - ESP_OK : Successfully transmit the buffer to wifi driver + * - others : failed to register the callback + */ +esp_err_t esp_wifi_internal_reg_netstack_buf_cb(wifi_netstack_buf_ref_cb_t ref, wifi_netstack_buf_free_cb_t free); + + /** * @brief The WiFi RX callback function * diff --git a/components/esp_wifi/include/esp_wifi.h b/components/esp_wifi/include/esp_wifi.h index 4b2f2656bb..ba38b737be 100644 --- a/components/esp_wifi/include/esp_wifi.h +++ b/components/esp_wifi/include/esp_wifi.h @@ -88,6 +88,8 @@ extern "C" { #define ESP_ERR_WIFI_POST (ESP_ERR_WIFI_BASE + 18) /*!< Failed to post the event to WiFi task */ #define ESP_ERR_WIFI_INIT_STATE (ESP_ERR_WIFI_BASE + 19) /*!< Invalod WiFi state when init/deinit is called */ #define ESP_ERR_WIFI_STOP_STATE (ESP_ERR_WIFI_BASE + 20) /*!< Returned when WiFi is stopping */ +#define ESP_ERR_WIFI_NOT_ASSOC (ESP_ERR_WIFI_BASE + 21) /*!< The WiFi connection is not associated */ +#define ESP_ERR_WIFI_TX_DISALLOW (ESP_ERR_WIFI_BASE + 22) /*!< The WiFi TX is disallowed */ /** * @brief WiFi stack configuration parameters passed to esp_wifi_init call. @@ -101,12 +103,12 @@ typedef struct { int tx_buf_type; /**< WiFi TX buffer type */ int static_tx_buf_num; /**< WiFi static TX buffer number */ int dynamic_tx_buf_num; /**< WiFi dynamic TX buffer number */ + int cache_tx_buf_num; /**< WiFi TX cache buffer number */ int csi_enable; /**< WiFi channel state information enable flag */ int ampdu_rx_enable; /**< WiFi AMPDU RX feature enable flag */ int ampdu_tx_enable; /**< WiFi AMPDU TX feature enable flag */ int nvs_enable; /**< WiFi NVS flash enable flag */ int nano_enable; /**< Nano option for printf/scan family enable flag */ - int tx_ba_win; /**< WiFi Block Ack TX window size */ int rx_ba_win; /**< WiFi Block Ack RX window size */ int wifi_task_core_id; /**< WiFi Task Core ID */ int beacon_max_len; /**< WiFi softAP maximum length of the beacon */ @@ -121,6 +123,12 @@ typedef struct { #define WIFI_STATIC_TX_BUFFER_NUM 0 #endif +#if (CONFIG_ESP32_SPIRAM_SUPPORT | CONFIG_ESP32S2_SPIRAM_SUPPORT) +#define WIFI_CACHE_TX_BUFFER_NUM CONFIG_ESP32_WIFI_CACHE_TX_BUFFER_NUM +#else +#define WIFI_CACHE_TX_BUFFER_NUM 0 +#endif + #ifdef CONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER_NUM #define WIFI_DYNAMIC_TX_BUFFER_NUM CONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER_NUM #else @@ -162,12 +170,6 @@ extern uint64_t g_wifi_feature_caps; #define WIFI_INIT_CONFIG_MAGIC 0x1F2F3F4F -#ifdef CONFIG_ESP32_WIFI_AMPDU_TX_ENABLED -#define WIFI_DEFAULT_TX_BA_WIN CONFIG_ESP32_WIFI_TX_BA_WIN -#else -#define WIFI_DEFAULT_TX_BA_WIN 0 /* unused if ampdu_tx_enable == false */ -#endif - #ifdef CONFIG_ESP32_WIFI_AMPDU_RX_ENABLED #define WIFI_DEFAULT_RX_BA_WIN CONFIG_ESP32_WIFI_RX_BA_WIN #else @@ -193,6 +195,7 @@ extern uint64_t g_wifi_feature_caps; #endif #define CONFIG_FEATURE_WPA3_SAE_BIT (1<<0) +#define CONFIG_FEATURE_CACHE_TX_BUF_BIT (1<<1) #define WIFI_INIT_CONFIG_DEFAULT() { \ .event_handler = &esp_event_send_internal, \ @@ -203,12 +206,12 @@ extern uint64_t g_wifi_feature_caps; .tx_buf_type = CONFIG_ESP32_WIFI_TX_BUFFER_TYPE,\ .static_tx_buf_num = WIFI_STATIC_TX_BUFFER_NUM,\ .dynamic_tx_buf_num = WIFI_DYNAMIC_TX_BUFFER_NUM,\ + .cache_tx_buf_num = WIFI_CACHE_TX_BUFFER_NUM,\ .csi_enable = WIFI_CSI_ENABLED,\ .ampdu_rx_enable = WIFI_AMPDU_RX_ENABLED,\ .ampdu_tx_enable = WIFI_AMPDU_TX_ENABLED,\ .nvs_enable = WIFI_NVS_ENABLED,\ .nano_enable = WIFI_NANO_FORMAT_ENABLED,\ - .tx_ba_win = WIFI_DEFAULT_TX_BA_WIN,\ .rx_ba_win = WIFI_DEFAULT_RX_BA_WIN,\ .wifi_task_core_id = WIFI_TASK_CORE_ID,\ .beacon_max_len = WIFI_SOFTAP_BEACON_MAX_LEN, \ diff --git a/components/esp_wifi/lib b/components/esp_wifi/lib index c934be7c20..0848a01774 160000 --- a/components/esp_wifi/lib +++ b/components/esp_wifi/lib @@ -1 +1 @@ -Subproject commit c934be7c20279db86e825fd44f7bc7eef9929ac1 +Subproject commit 0848a017746bd66be29999515cba20e0fd530819 diff --git a/components/esp_wifi/src/wifi_default.c b/components/esp_wifi/src/wifi_default.c index 7e0e41afad..bc494857c4 100644 --- a/components/esp_wifi/src/wifi_default.c +++ b/components/esp_wifi/src/wifi_default.c @@ -58,6 +58,10 @@ static void wifi_start(void *esp_netif, esp_event_base_t base, int32_t event_id, } } + if ((ret = esp_wifi_internal_reg_netstack_buf_cb(esp_netif_netstack_buf_ref, esp_netif_netstack_buf_free)) != ESP_OK) { + ESP_LOGE(TAG, "netstack cb reg failed with %d", ret); + return; + } esp_netif_set_mac(esp_netif, mac); esp_netif_action_start(esp_netif, base, event_id, data); } diff --git a/components/esp_wifi/src/wifi_init.c b/components/esp_wifi/src/wifi_init.c index 2912fcb961..898e462aae 100644 --- a/components/esp_wifi/src/wifi_init.c +++ b/components/esp_wifi/src/wifi_init.c @@ -50,6 +50,9 @@ uint64_t g_wifi_feature_caps = #if CONFIG_ESP32_WIFI_ENABLE_WPA3_SAE CONFIG_FEATURE_WPA3_SAE_BIT | #endif +#if (CONFIG_ESP32_SPIRAM_SUPPORT | CONFIG_ESP32S2_SPIRAM_SUPPORT) + CONFIG_FEATURE_CACHE_TX_BUF_BIT | +#endif 0; static const char* TAG = "wifi_init"; diff --git a/components/esp_wifi/src/wifi_netif.c b/components/esp_wifi/src/wifi_netif.c index ab2216cc30..168b1a0c49 100644 --- a/components/esp_wifi/src/wifi_netif.c +++ b/components/esp_wifi/src/wifi_netif.c @@ -63,6 +63,16 @@ static esp_err_t wifi_transmit(void *h, void *buffer, size_t len) return esp_wifi_internal_tx(driver->wifi_if, buffer, len); } +static esp_err_t wifi_transmit_wrap(void *h, void *buffer, size_t len, void *netstack_buf) +{ + wifi_netif_driver_t driver = h; +#if (CONFIG_ESP32_SPIRAM_SUPPORT | CONFIG_ESP32S2_SPIRAM_SUPPORT) + return esp_wifi_internal_tx_by_ref(driver->wifi_if, buffer, len, netstack_buf); +#else + return esp_wifi_internal_tx(driver->wifi_if, buffer, len); +#endif +} + static esp_err_t wifi_driver_start(esp_netif_t * esp_netif, void * args) { wifi_netif_driver_t driver = args; @@ -70,6 +80,7 @@ static esp_err_t wifi_driver_start(esp_netif_t * esp_netif, void * args) esp_netif_driver_ifconfig_t driver_ifconfig = { .handle = driver, .transmit = wifi_transmit, + .transmit_wrap= wifi_transmit_wrap, .driver_free_rx_buffer = wifi_free }; diff --git a/components/lwip/port/esp32/netif/wlanif.c b/components/lwip/port/esp32/netif/wlanif.c index 1555b0ce90..c5522dceb4 100644 --- a/components/lwip/port/esp32/netif/wlanif.c +++ b/components/lwip/port/esp32/netif/wlanif.c @@ -131,10 +131,10 @@ low_level_output(struct netif *netif, struct pbuf *p) } struct pbuf *q = p; - err_t ret; + esp_err_t ret; if(q->next == NULL) { - ret = esp_netif_transmit(esp_netif, q->payload, q->len); + ret = esp_netif_transmit_wrap(esp_netif, q->payload, q->len, q); } else { LWIP_DEBUGF(PBUF_DEBUG, ("low_level_output: pbuf is a list, application may has bug")); @@ -145,12 +145,20 @@ low_level_output(struct netif *netif, struct pbuf *p) } else { return ERR_MEM; } - ret = esp_netif_transmit(esp_netif, q->payload, q->len); + ret = esp_netif_transmit_wrap(esp_netif, q->payload, q->len, q); pbuf_free(q); } - return ret; + if (ret == ESP_OK) { + return ERR_OK; + } else if (ret == ESP_ERR_NO_MEM) { + return ERR_MEM; + } else if (ret == ESP_ERR_INVALID_ARG) { + return ERR_ARG; + } else { + return ERR_IF; + } } /** From 627a05d80e51dade00a94f58db3af0ad1658591a Mon Sep 17 00:00:00 2001 From: liu zhifu Date: Fri, 14 Aug 2020 23:42:57 +0800 Subject: [PATCH 2/7] esp_wifi: optimize WiFi debug log 1. Add esp_wifi_statis_dump() 2. Optimize WiFi related debug log --- components/esp_wifi/include/esp_wifi.h | 11 +++++++ components/esp_wifi/include/esp_wifi_types.h | 6 ++++ components/esp_wifi/lib | 2 +- components/esp_wifi/src/wifi_init.c | 30 ++++++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/components/esp_wifi/include/esp_wifi.h b/components/esp_wifi/include/esp_wifi.h index ba38b737be..4dad75b9a8 100644 --- a/components/esp_wifi/include/esp_wifi.h +++ b/components/esp_wifi/include/esp_wifi.h @@ -1142,6 +1142,17 @@ esp_err_t esp_wifi_set_inactive_time(wifi_interface_t ifx, uint16_t sec); */ esp_err_t esp_wifi_get_inactive_time(wifi_interface_t ifx, uint16_t *sec); +/** + * @brief Dump WiFi statistics + * + * @param modules statistic modules to be dumped + * + * @return + * - ESP_OK: succeed + * - others: failed + */ +esp_err_t esp_wifi_statis_dump(uint32_t modules); + #ifdef __cplusplus } #endif diff --git a/components/esp_wifi/include/esp_wifi_types.h b/components/esp_wifi/include/esp_wifi_types.h index c1d73d0632..af3c856934 100644 --- a/components/esp_wifi/include/esp_wifi_types.h +++ b/components/esp_wifi/include/esp_wifi_types.h @@ -612,6 +612,12 @@ typedef struct { uint8_t mac[6]; /**< MAC address of the station which send probe request */ } wifi_event_ap_probe_req_rx_t; +#define WIFI_STATIS_BUFFER (1<<0) +#define WIFI_STATIS_RXTX (1<<1) +#define WIFI_STATIS_HW (1<<2) +#define WIFI_STATIS_DIAG (1<<3) +#define WIFI_STATIS_ALL (-1) + #ifdef __cplusplus } #endif diff --git a/components/esp_wifi/lib b/components/esp_wifi/lib index 0848a01774..9b1c723417 160000 --- a/components/esp_wifi/lib +++ b/components/esp_wifi/lib @@ -1 +1 @@ -Subproject commit 0848a017746bd66be29999515cba20e0fd530819 +Subproject commit 9b1c723417805409f17490931128b12c5521ddd0 diff --git a/components/esp_wifi/src/wifi_init.c b/components/esp_wifi/src/wifi_init.c index 898e462aae..3229e3d8c4 100644 --- a/components/esp_wifi/src/wifi_init.c +++ b/components/esp_wifi/src/wifi_init.c @@ -142,6 +142,35 @@ esp_err_t esp_wifi_deinit(void) return err; } +static void esp_wifi_config_info(void) +{ +#ifdef CONFIG_ESP32_WIFI_RX_BA_WIN + ESP_LOGI(TAG, "rx ba win: %d", CONFIG_ESP32_WIFI_RX_BA_WIN); +#endif + ESP_LOGI(TAG, "tcpip mbox: %d", CONFIG_LWIP_TCPIP_RECVMBOX_SIZE); + ESP_LOGI(TAG, "udp mbox: %d", CONFIG_LWIP_UDP_RECVMBOX_SIZE); + ESP_LOGI(TAG, "tcp mbox: %d", CONFIG_LWIP_TCP_RECVMBOX_SIZE); + ESP_LOGI(TAG, "tcp tx win: %d", CONFIG_LWIP_TCP_SND_BUF_DEFAULT); + ESP_LOGI(TAG, "tcp rx win: %d", CONFIG_LWIP_TCP_WND_DEFAULT); + ESP_LOGI(TAG, "tcp mss: %d", CONFIG_LWIP_TCP_MSS); + +#ifdef CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP + ESP_LOGI(TAG, "WiFi/LWIP prefer SPIRAM"); +#endif + +#ifdef CONFIG_ESP32_WIFI_IRAM_OPT + ESP_LOGI(TAG, "WiFi IRAM OP enabled"); +#endif + +#ifdef CONFIG_ESP32_WIFI_RX_IRAM_OPT + ESP_LOGI(TAG, "WiFi RX IRAM OP enabled"); +#endif + +#ifdef CONFIG_LWIP_IRAM_OPTIMIZATION + ESP_LOGI(TAG, "LWIP IRAM OP enabled"); +#endif +} + esp_err_t esp_wifi_init(const wifi_init_config_t *config) { #ifdef CONFIG_PM_ENABLE @@ -190,6 +219,7 @@ esp_err_t esp_wifi_init(const wifi_init_config_t *config) #if CONFIG_IDF_TARGET_ESP32S2 adc2_cal_include(); //This enables the ADC2 calibration constructor at start up. #endif + esp_wifi_config_info(); return result; } From 8757cefeec281f461ecf4e0548c7aba70bee1ddb Mon Sep 17 00:00:00 2001 From: xiehang Date: Mon, 17 Aug 2020 10:31:19 +0800 Subject: [PATCH 3/7] esp_wifi: add rf test long short support --- components/esp_wifi/lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/esp_wifi/lib b/components/esp_wifi/lib index 9b1c723417..3d951fb6c4 160000 --- a/components/esp_wifi/lib +++ b/components/esp_wifi/lib @@ -1 +1 @@ -Subproject commit 9b1c723417805409f17490931128b12c5521ddd0 +Subproject commit 3d951fb6c4fce3a2655e63d475b80b09c6acfddf From 594a1963645241bd0b075e5b5a60ce7c98db041c Mon Sep 17 00:00:00 2001 From: "kapil.gupta" Date: Mon, 24 Aug 2020 16:47:22 +0530 Subject: [PATCH 4/7] esp_wifi: Fix null pointer dereferences in mgmt rx --- components/esp_wifi/lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/esp_wifi/lib b/components/esp_wifi/lib index 3d951fb6c4..96cdc6ac10 160000 --- a/components/esp_wifi/lib +++ b/components/esp_wifi/lib @@ -1 +1 @@ -Subproject commit 3d951fb6c4fce3a2655e63d475b80b09c6acfddf +Subproject commit 96cdc6ac10c7fa443e3815c5d6809c0a05ed6138 From ddfb11e1c55a5d6a4141d7a5978c817e7b0bc590 Mon Sep 17 00:00:00 2001 From: aditi_lonkar Date: Tue, 28 Jul 2020 17:57:55 +0530 Subject: [PATCH 5/7] wifi: Fix esp_wifi log levels Closes https://github.com/espressif/esp-idf/issues/5721 --- components/esp_wifi/include/esp_private/wifi.h | 3 ++- components/esp_wifi/lib | 2 +- components/esp_wifi/src/wifi_init.c | 4 +++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/components/esp_wifi/include/esp_private/wifi.h b/components/esp_wifi/include/esp_private/wifi.h index 04a306fbea..b85d60eed7 100644 --- a/components/esp_wifi/include/esp_private/wifi.h +++ b/components/esp_wifi/include/esp_private/wifi.h @@ -53,7 +53,8 @@ typedef struct { * */ typedef enum { - WIFI_LOG_ERROR = 0, /*enabled by default*/ + WIFI_LOG_NONE = 0, + WIFI_LOG_ERROR , /*enabled by default*/ WIFI_LOG_WARNING, /*enabled by default*/ WIFI_LOG_INFO, /*enabled by default*/ WIFI_LOG_DEBUG, /*can be set in menuconfig*/ diff --git a/components/esp_wifi/lib b/components/esp_wifi/lib index 96cdc6ac10..ce232cafe7 160000 --- a/components/esp_wifi/lib +++ b/components/esp_wifi/lib @@ -1 +1 @@ -Subproject commit 96cdc6ac10c7fa443e3815c5d6809c0a05ed6138 +Subproject commit ce232cafe7d973f07f919deee8de95f397453f47 diff --git a/components/esp_wifi/src/wifi_init.c b/components/esp_wifi/src/wifi_init.c index 3229e3d8c4..0159e48fa4 100644 --- a/components/esp_wifi/src/wifi_init.c +++ b/components/esp_wifi/src/wifi_init.c @@ -63,8 +63,10 @@ static void __attribute__((constructor)) s_set_default_wifi_log_level(void) so set it at runtime startup. Done here not in esp_wifi_init() to allow the user to set the level again before esp_wifi_init() is called. */ - esp_log_level_set("wifi", CONFIG_LOG_DEFAULT_LEVEL); + esp_log_level_set("wifi", CONFIG_LOG_DEFAULT_LEVEL); esp_log_level_set("mesh", CONFIG_LOG_DEFAULT_LEVEL); + esp_log_level_set("smartconfig", CONFIG_LOG_DEFAULT_LEVEL); + esp_log_level_set("ESPNOW", CONFIG_LOG_DEFAULT_LEVEL); } static void esp_wifi_set_debug_log(void) From 089b78b91605bf52affab7d183e73767a64fc600 Mon Sep 17 00:00:00 2001 From: zhangyanjiao Date: Tue, 1 Sep 2020 14:46:50 +0800 Subject: [PATCH 6/7] esp_wifi: 1. get primary channel from HT info 2. fix the bug that set_channel doesn't work for softAP when STA connects --- components/esp_wifi/lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/esp_wifi/lib b/components/esp_wifi/lib index ce232cafe7..0cfa71d94a 160000 --- a/components/esp_wifi/lib +++ b/components/esp_wifi/lib @@ -1 +1 @@ -Subproject commit ce232cafe7d973f07f919deee8de95f397453f47 +Subproject commit 0cfa71d94ab3f92ef5db627a5d44a5409835c21b From d17e6a75e06bc7d16ae0575df759f408a0305a3a Mon Sep 17 00:00:00 2001 From: Nachiket Kukade Date: Wed, 2 Sep 2020 10:45:08 +0800 Subject: [PATCH 7/7] espnow/pmf: Implement ESPNOW + PMF Co-existance H/W decryption of Mgmt frames was disabled for PMF and done through S/W. If ESPNOW packets go through this path, it affects backward compatibility since method of decrypting Mgmt packets is different in H/W. To address PMF + ESPNOW Co-existance, CCMP decryption method is modified for ESPNOW packets so that they can be decrypted correctly. Since Tx of ESPNOW packets can still be done in H/W alongside PMF, no change required in encryption method in S/W. Co-Authored-By: Nachiket Kukade Co-Authored-By: zhangyanjiao Co-Authored-By: kapil.gupta --- .../esp_wifi/include/esp_wifi_crypto_types.h | 4 ++- components/esp_wifi/lib | 2 +- .../wpa_supplicant/src/crypto/aes-ccm.c | 10 +++++-- components/wpa_supplicant/src/crypto/aes.h | 4 +-- components/wpa_supplicant/src/crypto/ccmp.c | 27 ++++++++++--------- components/wpa_supplicant/src/crypto/ccmp.h | 2 +- 6 files changed, 29 insertions(+), 20 deletions(-) diff --git a/components/esp_wifi/include/esp_wifi_crypto_types.h b/components/esp_wifi/include/esp_wifi_crypto_types.h index 47620802e0..cda53946a4 100644 --- a/components/esp_wifi/include/esp_wifi_crypto_types.h +++ b/components/esp_wifi/include/esp_wifi_crypto_types.h @@ -336,10 +336,12 @@ typedef int (*esp_omac1_aes_128_t)(const uint8_t *key, const uint8_t *data, size * @data: Pointer to encrypted data buffer * @data_len: Encrypted data length in bytes * @decrypted_len: Length of decrypted data + * @espnow_pkt: Indicates if it's an ESPNOW packet * Returns: Pointer to decrypted data on success, NULL on failure */ typedef uint8_t * (*esp_ccmp_decrypt_t)(const uint8_t *tk, const uint8_t *ieee80211_hdr, - const uint8_t *data, size_t data_len, size_t *decrypted_len); + const uint8_t *data, size_t data_len, + size_t *decrypted_len, bool espnow_pkt); /** * @brief Encrypt data using CCMP (Counter Mode CBC-MAC Protocol OR diff --git a/components/esp_wifi/lib b/components/esp_wifi/lib index 0cfa71d94a..41abe30914 160000 --- a/components/esp_wifi/lib +++ b/components/esp_wifi/lib @@ -1 +1 @@ -Subproject commit 0cfa71d94ab3f92ef5db627a5d44a5409835c21b +Subproject commit 41abe309140a05939bda776b7d1a744f76612582 diff --git a/components/wpa_supplicant/src/crypto/aes-ccm.c b/components/wpa_supplicant/src/crypto/aes-ccm.c index ca3acc26e9..53e43cbd8b 100644 --- a/components/wpa_supplicant/src/crypto/aes-ccm.c +++ b/components/wpa_supplicant/src/crypto/aes-ccm.c @@ -177,9 +177,10 @@ int aes_ccm_ae(const u8 *key, size_t key_len, const u8 *nonce, /* AES-CCM with fixed L=2 and aad_len <= 30 assumption */ -int aes_ccm_ad(const u8 *key, size_t key_len, const u8 *nonce, +int aes_ccm_ad(const u8 *key, size_t key_len, u8 *nonce, size_t M, const u8 *crypt, size_t crypt_len, - const u8 *aad, size_t aad_len, const u8 *auth, u8 *plain) + const u8 *aad, size_t aad_len, const u8 *auth, + u8 *plain, bool skip_auth) { const size_t L = 2; void *aes; @@ -200,6 +201,11 @@ int aes_ccm_ad(const u8 *key, size_t key_len, const u8 *nonce, /* plaintext = msg XOR (S_1 | S_2 | ... | S_n) */ aes_ccm_encr(aes, L, crypt, crypt_len, plain, a); + if (skip_auth) { + aes_encrypt_deinit(aes); + return 0; + } + aes_ccm_auth_start(aes, M, L, nonce, aad, aad_len, crypt_len, x); aes_ccm_auth(aes, plain, crypt_len, x); diff --git a/components/wpa_supplicant/src/crypto/aes.h b/components/wpa_supplicant/src/crypto/aes.h index 309d24dbac..ee71688b92 100644 --- a/components/wpa_supplicant/src/crypto/aes.h +++ b/components/wpa_supplicant/src/crypto/aes.h @@ -29,8 +29,8 @@ int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac); int aes_ccm_ae(const u8 *key, size_t key_len, const u8 *nonce, size_t M, const u8 *plain, size_t plain_len, const u8 *aad, size_t aad_len, u8 *crypt, u8 *auth); -int aes_ccm_ad(const u8 *key, size_t key_len, const u8 *nonce, +int aes_ccm_ad(const u8 *key, size_t key_len, u8 *nonce, size_t M, const u8 *crypt, size_t crypt_len, const u8 *aad, size_t aad_len, const u8 *auth, - u8 *plain); + u8 *plain, bool skip_auth); #endif /* AES_H */ diff --git a/components/wpa_supplicant/src/crypto/ccmp.c b/components/wpa_supplicant/src/crypto/ccmp.c index f5814a7844..152c23417b 100644 --- a/components/wpa_supplicant/src/crypto/ccmp.c +++ b/components/wpa_supplicant/src/crypto/ccmp.c @@ -16,7 +16,7 @@ #include "aes_wrap.h" static void ccmp_aad_nonce(const struct ieee80211_hdr *hdr, const u8 *data, - u8 *aad, size_t *aad_len, u8 *nonce) + u8 *aad, size_t *aad_len, u8 *nonce, bool espnow_pkt) { u16 fc, stype, seq; int qos = 0, addr4 = 0; @@ -41,7 +41,7 @@ static void ccmp_aad_nonce(const struct ieee80211_hdr *hdr, const u8 *data, qc += ETH_ALEN; nonce[0] = qc[0] & 0x0f; } - } else if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT) + } else if (!espnow_pkt && WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT) nonce[0] |= 0x10; /* Management */ fc &= ~(WLAN_FC_RETRY | WLAN_FC_PWRMGT | WLAN_FC_MOREDATA); @@ -136,7 +136,7 @@ static void ccmp_aad_nonce_pv1(const u8 *hdr, const u8 *a1, const u8 *a2, u8 * ccmp_decrypt(const u8 *tk, const u8 *hdr, const u8 *data, - size_t data_len, size_t *decrypted_len) + size_t data_len, size_t *decrypted_len, bool espnow_pkt) { u8 aad[30], nonce[13]; size_t aad_len; @@ -153,22 +153,22 @@ u8 * ccmp_decrypt(const u8 *tk, const u8 *hdr, const u8 *data, mlen = data_len - 8 - 8; os_memset(aad, 0, sizeof(aad)); - ccmp_aad_nonce((const struct ieee80211_hdr *)hdr, data, aad, &aad_len, nonce); - //wpa_hexdump(MSG_DEBUG, "CCMP AAD", aad, aad_len); - //wpa_hexdump(MSG_DEBUG, "CCMP nonce", nonce, 13); + ccmp_aad_nonce((const struct ieee80211_hdr *)hdr, data, aad, &aad_len, + nonce, espnow_pkt); + wpa_hexdump(MSG_DEBUG, "CCMP AAD", aad, aad_len); + wpa_hexdump(MSG_DEBUG, "CCMP nonce", nonce, 13); if (aes_ccm_ad(tk, 16, nonce, 8, data + 8, mlen, aad, aad_len, - data + 8 + mlen, plain) < 0) { + data + 8 + mlen, plain, espnow_pkt ? true : false) < 0) { os_free(plain); return NULL; } - //wpa_hexdump(MSG_DEBUG, "CCMP decrypted", plain, mlen); + wpa_hexdump(MSG_DEBUG, "CCMP decrypted", plain, mlen); *decrypted_len = mlen; return plain; } - void ccmp_get_pn(u8 *pn, const u8 *data) { pn[0] = data[7]; /* PN5 */ @@ -210,7 +210,7 @@ u8 * ccmp_encrypt(const u8 *tk, u8 *frame, size_t len, size_t hdrlen, *pos++ = pn[0]; /* PN5 */ os_memset(aad, 0, sizeof(aad)); - ccmp_aad_nonce(hdr, crypt + hdrlen, aad, &aad_len, nonce); + ccmp_aad_nonce(hdr, crypt + hdrlen, aad, &aad_len, nonce, false); wpa_hexdump(MSG_DEBUG, "CCMP AAD", aad, aad_len); wpa_hexdump(MSG_DEBUG, "CCMP nonce", nonce, 13); @@ -288,12 +288,13 @@ u8 * ccmp_256_decrypt(const u8 *tk, const u8 *hdr, const u8 *data, mlen = data_len - 8 - 16; os_memset(aad, 0, sizeof(aad)); - ccmp_aad_nonce((const struct ieee80211_hdr *)hdr, data, aad, &aad_len, nonce); + ccmp_aad_nonce((const struct ieee80211_hdr *)hdr, data, aad, + &aad_len, nonce, false); wpa_hexdump(MSG_DEBUG, "CCMP-256 AAD", aad, aad_len); wpa_hexdump(MSG_DEBUG, "CCMP-256 nonce", nonce, 13); if (aes_ccm_ad(tk, 32, nonce, 16, data + 8, mlen, aad, aad_len, - data + 8 + mlen, plain) < 0) { + data + 8 + mlen, plain, false) < 0) { os_free(plain); return NULL; } @@ -334,7 +335,7 @@ u8 * ccmp_256_encrypt(const u8 *tk, u8 *frame, size_t len, size_t hdrlen, *pos++ = pn[0]; /* PN5 */ os_memset(aad, 0, sizeof(aad)); - ccmp_aad_nonce(hdr, crypt + hdrlen, aad, &aad_len, nonce); + ccmp_aad_nonce(hdr, crypt + hdrlen, aad, &aad_len, nonce, false); wpa_hexdump(MSG_DEBUG, "CCMP-256 AAD", aad, aad_len); wpa_hexdump(MSG_DEBUG, "CCMP-256 nonce", nonce, 13); diff --git a/components/wpa_supplicant/src/crypto/ccmp.h b/components/wpa_supplicant/src/crypto/ccmp.h index aeca06022e..0cf157bdb5 100644 --- a/components/wpa_supplicant/src/crypto/ccmp.h +++ b/components/wpa_supplicant/src/crypto/ccmp.h @@ -11,7 +11,7 @@ #define CCMP_H u8 * ccmp_decrypt(const u8 *tk, const u8 *hdr, const u8 *data, - size_t data_len, size_t *decrypted_len); + size_t data_len, size_t *decrypted_len, bool espnow_pkt); u8 * ccmp_encrypt(const u8 *tk, u8 *frame, size_t len, size_t hdrlen, u8 *pn, int keyid, size_t *encrypted_len); u8 * ccmp_encrypt_pv1(const u8 *tk, const u8 *a1, const u8 *a2, const u8 *a3,