Skip to content

Commit 82e69df

Browse files
projectgusdpgeorge
authored andcommitted
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 <[email protected]>
1 parent 05ac693 commit 82e69df

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-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
@@ -133,3 +133,7 @@ CONFIG_NEWLIB_NANO_FORMAT=y
133133
# Due to limitations in the PMP system this feature breaks native emitters
134134
# so is disabled by default.
135135
CONFIG_ESP_SYSTEM_PMP_IDRAM_SPLIT=n
136+
137+
# Further limit total sockets in TIME-WAIT when there are many short-lived
138+
# connections.
139+
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
@@ -108,6 +108,7 @@ list(APPEND MICROPY_SOURCE_PORT
108108
network_wlan.c
109109
mpnimbleport.c
110110
modsocket.c
111+
lwip_patch.c
111112
modesp.c
112113
esp32_nvs.c
113114
esp32_partition.c

ports/esp32/lwip_patch.c

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

0 commit comments

Comments
 (0)