From d2e3d21a85bd2e92c9f5e7835eda6fa3ce451831 Mon Sep 17 00:00:00 2001 From: LiPeng Date: Wed, 20 Dec 2023 11:37:41 +0800 Subject: [PATCH] tcp: Apply faster PCB recycling in FIN_WAIT_1 or FIN_WAIT_2 state --- src/core/tcp.c | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/core/tcp.c b/src/core/tcp.c index 3fbdd89ae..fe6baaf25 100644 --- a/src/core/tcp.c +++ b/src/core/tcp.c @@ -1765,7 +1765,9 @@ tcp_kill_state(enum tcp_state state) struct tcp_pcb *pcb, *inactive; u32_t inactivity; +#if !ESP_LWIP LWIP_ASSERT("invalid state", (state == CLOSING) || (state == LAST_ACK)); +#endif inactivity = 0; inactive = NULL; @@ -1870,17 +1872,41 @@ tcp_alloc(u8_t prio) tcp_kill_state(CLOSING); /* Try to allocate a tcp_pcb again. */ pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB); +#if ESP_LWIP if (pcb == NULL) { - /* Try killing oldest active connection with lower priority than the new one. */ - LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing oldest connection with prio lower than %d\n", prio)); - tcp_kill_prio(prio); - /* Try to allocate a tcp_pcb again. */ + /* Try killing oldest connection in FIN_WAIT_2. */ + LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest FIN_WAIT_2 connection\n")); + tcp_kill_state(FIN_WAIT_2); pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB); + if (pcb == NULL) { + /* Try killing oldest connection in FIN_WAIT_1. */ + LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing off oldest FIN_WAIT_1 connection\n")); + tcp_kill_state(FIN_WAIT_1); + pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB); +#endif + if (pcb == NULL) { + /* Try killing oldest active connection with lower priority than the new one. */ + LWIP_DEBUGF(TCP_DEBUG, ("tcp_alloc: killing oldest connection with prio lower than %d\n", prio)); + tcp_kill_prio(prio); + /* Try to allocate a tcp_pcb again. */ + pcb = (struct tcp_pcb *)memp_malloc(MEMP_TCP_PCB); + if (pcb != NULL) { + /* adjust err stats: memp_malloc failed multiple times before */ + MEMP_STATS_DEC(err, MEMP_TCP_PCB); + } + } +#if ESP_LWIP + if (pcb != NULL) { + /* adjust err stats: memp_malloc failed multiple times before */ + MEMP_STATS_DEC(err, MEMP_TCP_PCB); + } + } if (pcb != NULL) { /* adjust err stats: memp_malloc failed multiple times before */ MEMP_STATS_DEC(err, MEMP_TCP_PCB); } } +#endif if (pcb != NULL) { /* adjust err stats: memp_malloc failed multiple times before */ MEMP_STATS_DEC(err, MEMP_TCP_PCB);