Skip to content

Commit 8856591

Browse files
authored
Add patch to fix the maximum number of TCP PCBs in TIME_WAIT (espressif#191)
1 parent b662e2d commit 8856591

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

patches/lwip_max_tcp_pcb.diff

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
diff --git a/components/lwip/lwip/src/core/memp.c b/components/lwip/lwip/src/core/memp.c
2+
index 352ce5a55127a658b6b3c9d8541298c42df332ff..39433cf476b3456b046e337e9b1f016299964a84 100644
3+
--- a/components/lwip/lwip/src/core/memp.c
4+
+++ b/components/lwip/lwip/src/core/memp.c
5+
@@ -240,6 +240,10 @@ memp_init(void)
6+
#endif /* MEMP_OVERFLOW_CHECK >= 2 */
7+
}
8+
9+
+#if MEMP_MEM_MALLOC && ESP_LWIP && LWIP_TCP
10+
+static u32_t num_tcp_pcb = 0;
11+
+#endif
12+
+
13+
static void *
14+
#if !MEMP_OVERFLOW_CHECK
15+
do_memp_malloc_pool(const struct memp_desc *desc)
16+
@@ -251,6 +255,16 @@ do_memp_malloc_pool_fn(const struct memp_desc *desc, const char *file, const int
17+
SYS_ARCH_DECL_PROTECT(old_level);
18+
19+
#if MEMP_MEM_MALLOC
20+
+#if ESP_LWIP
21+
+#if LWIP_TCP
22+
+ if(desc == memp_pools[MEMP_TCP_PCB]){
23+
+ if(num_tcp_pcb >= MEMP_NUM_TCP_PCB){
24+
+ return NULL;
25+
+ }
26+
+ }
27+
+#endif
28+
+#endif
29+
+
30+
memp = (struct memp *)mem_malloc(MEMP_SIZE + MEMP_ALIGN_SIZE(desc->size));
31+
SYS_ARCH_PROTECT(old_level);
32+
#else /* MEMP_MEM_MALLOC */
33+
@@ -260,6 +274,12 @@ do_memp_malloc_pool_fn(const struct memp_desc *desc, const char *file, const int
34+
#endif /* MEMP_MEM_MALLOC */
35+
36+
if (memp != NULL) {
37+
+#if MEMP_MEM_MALLOC && ESP_LWIP && LWIP_TCP
38+
+ if (desc == memp_pools[MEMP_TCP_PCB]) {
39+
+ num_tcp_pcb++;
40+
+ }
41+
+#endif
42+
+
43+
#if !MEMP_MEM_MALLOC
44+
#if MEMP_OVERFLOW_CHECK == 1
45+
memp_overflow_check_element(memp, desc);
46+
@@ -369,6 +389,12 @@ do_memp_free_pool(const struct memp_desc *desc, void *mem)
47+
48+
SYS_ARCH_PROTECT(old_level);
49+
50+
+#if MEMP_MEM_MALLOC && ESP_LWIP && LWIP_TCP
51+
+ if (desc == memp_pools[MEMP_TCP_PCB]) {
52+
+ num_tcp_pcb--;
53+
+ }
54+
+#endif
55+
+
56+
#if MEMP_OVERFLOW_CHECK == 1
57+
memp_overflow_check_element(memp, desc);
58+
#endif /* MEMP_OVERFLOW_CHECK */
59+
diff --git a/components/lwip/lwip/src/core/tcp.c b/components/lwip/lwip/src/core/tcp.c
60+
index 3fbdd89ae07807208ff7466abb50f90b5e7727e4..fe6baaf250927cb4b89f8d1dbd41c73def88692b 100644
61+
--- a/components/lwip/lwip/src/core/tcp.c
62+
+++ b/components/lwip/lwip/src/core/tcp.c
63+
@@ -1765,7 +1765,9 @@ tcp_kill_state(enum tcp_state state)
64+
struct tcp_pcb *pcb, *inactive;
65+
u32_t inactivity;
66+
67+
+#if !ESP_LWIP
68+
LWIP_ASSERT("invalid state", (state == CLOSING) || (state == LAST_ACK));
69+
+#endif
70+
71+
inactivity = 0;
72+
inactive = NULL;
73+
@@ -1870,17 +1872,41 @@ tcp_alloc(u8_t prio)
74+
tcp_kill_state(CLOSING);
75+
/* Try to allocate a tcp_pcb again. */
76+
pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
77+
+#if ESP_LWIP
78+
if (pcb == NULL) {
79+
- /* Try killing oldest active connection with lower priority than the new one. */
80+
- LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing oldest connection with prio lower than %d\n", prio));
81+
- tcp_kill_prio(prio);
82+
- /* Try to allocate a tcp_pcb again. */
83+
+ /* Try killing oldest connection in FIN_WAIT_2. */
84+
+ LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest FIN_WAIT_2 connection\n"));
85+
+ tcp_kill_state(FIN_WAIT_2);
86+
pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
87+
+ if (pcb == NULL) {
88+
+ /* Try killing oldest connection in FIN_WAIT_1. */
89+
+ LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest FIN_WAIT_1 connection\n"));
90+
+ tcp_kill_state(FIN_WAIT_1);
91+
+ pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
92+
+#endif
93+
+ if (pcb == NULL) {
94+
+ /* Try killing oldest active connection with lower priority than the new one. */
95+
+ LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing oldest connection with prio lower than %d\n", prio));
96+
+ tcp_kill_prio(prio);
97+
+ /* Try to allocate a tcp_pcb again. */
98+
+ pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB);
99+
+ if (pcb != NULL) {
100+
+ /* adjust err stats: memp_malloc failed multiple times before */
101+
+ MEMP_STATS_DEC(err, MEMP_TCP_PCB);
102+
+ }
103+
+ }
104+
+#if ESP_LWIP
105+
+ if (pcb != NULL) {
106+
+ /* adjust err stats: memp_malloc failed multiple times before */
107+
+ MEMP_STATS_DEC(err, MEMP_TCP_PCB);
108+
+ }
109+
+ }
110+
if (pcb != NULL) {
111+
/* adjust err stats: memp_malloc failed multiple times before */
112+
MEMP_STATS_DEC(err, MEMP_TCP_PCB);
113+
}
114+
}
115+
+#endif
116+
if (pcb != NULL) {
117+
/* adjust err stats: memp_malloc failed multiple times before */
118+
MEMP_STATS_DEC(err, MEMP_TCP_PCB);

tools/install-esp-idf.sh

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ if [ ! -x $idf_was_installed ] || [ ! -x $commit_predefined ]; then
3939
cd $IDF_PATH
4040
patch -p1 -N -i ../patches/esp32s2_i2c_ll_master_init.diff
4141
patch -p1 -N -i ../patches/mmu_map.diff
42+
patch -p1 -N -i ../patches/lwip_max_tcp_pcb.diff
4243
cd -
4344
fi
4445

0 commit comments

Comments
 (0)