8000 esp32: Apply the LWIP active TCP socket limit. · micropython/micropython@a7d85e6 · GitHub
[go: up one dir, main page]

Skip to content

Commit a7d85e6

Browse files
committed
esp32: Apply the LWIP active TCP socket limit.
This is a workaround for a bug in ESP-IDF where the configuration setting for maximum active TCP sockets (PCBs) is not applied. Fixes cases where a lot of short-lived TCP connections can cause: - Excessive memory usage (unbounded number of sockets in TIME-WAIT). - Much higher risk of stalled connections due to repeated port numbers. The maximum number of active TCP PCBs is reduced from 16 to 12 to further reduce this risk (trade-off against possibility of TIME-WAIT Assassination as described in RFC1337). This is not a watertight fix for the second point: a peer can still reuse a port number while a previous socket is in TIME-WAIT, and LWIP will reject that connection (in an RFC compliant way) causing the peer to stall. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
1 parent 17d8234 commit a7d85e6

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

ports/esp32/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,9 @@ list(APPEND EXTRA_COMPONENT_DIRS main_${IDF_TARGET})
7474
# Enable the panic handler wrapper
7575
idf_build_set_property(LINK_OPTIONS "-Wl,--wrap=esp_panic_handler" APPEND)
7676

77+
# Patch LWIP memory pool allocators (see lwip_patch.c)
78+
idf_build_set_property(LINK_OPTIONS "-Wl,--wrap=memp_malloc" APPEND)
79+
idf_build_set_property(LINK_OPTIONS "-Wl,--wrap=memp_free" APPEND)
80+
7781
# Define the project.
7882
project(micropython)

ports/esp32/boards/sdkconfig.base

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,7 @@ CONFIG_ETH_SPI_ETHERNET_DM9051=y
127127
# formatting in ROM instead and should override this, check
128128
# ESP_ROM_HAS_NEWLIB_NANO_FORMAT.
129129
CONFIG_NEWLIB_NANO_FORMAT=y
130+
131+
# Further limit total sockets in TIME-WAIT when there are many short-lived
132+
# connections.
133+
CONFIG_LWIP_MAX_ACTIVE_TCP=12

ports/esp32/esp32_common.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ list(APPEND MICROPY_SOURCE_PORT
7777
network_wlan.c
7878
mpnimbleport.c
7979
modsocket.c
80+
lwip_patch.c
8081
modesp.c
8182
esp32_nvs.c
8283
esp32_partition.c

ports/esp32/lwip_patch.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* Development of the code in this file was sponsored by Microbric Pty Ltd
5+
* and Mnemote Pty Ltd
6+
*
7+
* The MIT License (MIT)
8+
*
9+
* Copyright (c) 2024 Angus Gratton
10+
*
11+
* Permission is hereby granted, free of charge, to any person obtaining a copy
12+
* of this software and associated documentation files (the "Software"), to deal
13+
* in the Software without restriction, including without limitation the rights
14+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15+
* copies of the Software, and to permit persons to whom the Software is
16+
* furnished to do so, subject to the following conditions:
17+
*
18+
* The above copyright notice and this permission notice shall be included in
19+
* all copies or substantial portions of the Software.
20+
*
21+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27+
* THE SOFTWARE.
28+
*/
29+
#include "lwip/memp.h"
30+
31+
// This is a link-time patch to enforce the limit of max active TCP PCBs. A
32+
// workaround for upstream issue https://github.com/espressif/esp-idf/issues/9670
33+
//
34+
// Without this limit the number of TCP PCBs in TIME-WAIT is unbounded, which can
35+
// have two problems on systems with a lot of short-lived TCP connections:
36+
//
37+
// - Higher memory usage.
38+
// - Increased chance of stalled TCP connections due to port reuse.
39+
40+
static unsigned active_tcp_pcbs;
41+
42+
void *__real_memp_malloc(memp_t type);
43+
void __real_memp_free(memp_t type, void *mem);
44+
45+
void *__wrap_memp_malloc(memp_t type) {
46+
if (type != MEMP_TCP_PCB) {
47+
return __real_memp_malloc(type);
48+
}
49+
50+
if (active_tcp_pcbs >= MEMP_NUM_TCP_PCB) {
51+
return NULL;
52+
}
53+
54+
void *res = __real_memp_malloc(MEMP_TCP_PCB);
55+
if (res != NULL) {
56+
++active_tcp_pcbs;
57+
}
58+
return res;
59+
}
60+
61+
void __wrap_memp_free(memp_t type, void *mem) {
62+
__real_memp_free(type, mem);
63+
if (type == MEMP_TCP_PCB && mem != NULL) {
64+
assert(active_tcp_pcbs);
65+
--active_tcp_pcbs;
66+
}
67+
}

0 commit comments

Comments
 (0)
0