Skip to content

Commit 420960d

Browse files
authored
remove oldest pcbs in time-wait state when max number of them is reached (#17)
1 parent 2b827f8 commit 420960d

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

glue-lwip/arduino/lwipopts.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2992,6 +2992,7 @@
29922992

29932993
/*
29942994
--------------------------------------------------
2995+
------------- End of original lwipopts -----------
29952996
--------------------------------------------------
29962997
*/
29972998

@@ -3008,4 +3009,34 @@ struct netif;
30083009
#endif
30093010
LWIP_ERR_T lwip_unhandled_packet (struct pbuf* pbuf, struct netif* netif) __attribute__((weak));
30103011

3012+
/*
3013+
--------------------------------------------------
3014+
----------------- TIME-WAIT tweak ----------------
3015+
--------------------------------------------------
3016+
port @me-no-dev time-wait tweak
3017+
https://github.com/esp8266/Arduino/commit/07f4d4c241df2c552899857f39a4295164f686f2#diff-f8258e71e25fb9985ca3799e3d8b88ecR399
3018+
*/
3019+
3020+
void tcp_kill_timewait (void);
3021+
#define TCP_TW_LIMIT(l) \
3022+
if (l) do { \
3023+
u32_t count_plus_1 = 1; \
3024+
struct tcp_pcb* tmp = tcp_tw_pcbs; \
3025+
if (tmp) \
3026+
while ((tmp = tmp->next)) \
3027+
++count_plus_1; \
3028+
while (--count_plus_1 > (l)) \
3029+
/* kill the oldest */ \
3030+
/* pcb in TW state */ \
3031+
tcp_kill_timewait(); \
3032+
} while (0)
3033+
3034+
/**
3035+
* MEMP_NUM_TCP_PCB_TIME_WAIT: the number of TCP pcbs in TIME_WAIT state.
3036+
* (requires the LWIP_TCP option, 0 = disabled)
3037+
*/
3038+
#ifndef MEMP_NUM_TCP_PCB_TIME_WAIT
3039+
#define MEMP_NUM_TCP_PCB_TIME_WAIT 5
3040+
#endif
3041+
30113042
#endif // MYLWIPOPTS_H

makefiles/Makefile.lwip2

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ OBJ = \
1414
BUILD_INCLUDES = -I$(BUILD) -I$(SDK)/include -Iinclude -I../../glue -I../../glue-lwip -I../../glue-lwip/$(target)
1515
#BUILD_INCLUDES += -I../../lwip2-contrib-src/apps/ping
1616

17-
all: $(LWIP_LIB)
17+
all: patches $(LWIP_LIB)
18+
19+
patches: .patched
20+
21+
.patched:
22+
patch -d .. -p1 < ../../patches/time-wait.patch
23+
touch .patched
1824

1925
include ../../makefiles/Makefile.defs
2026
include ../../makefiles/Makefile.rules

patches/time-wait.patch

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
diff --git a/src/core/tcp.c b/src/core/tcp.c
2+
index b5144d2..1dfe573 100644
3+
--- a/src/core/tcp.c
4+
+++ b/src/core/tcp.c
5+
@@ -281,6 +281,7 @@ tcp_close_shutdown(struct tcp_pcb *pcb, u8_t rst_on_unacked_data)
6+
/* move to TIME_WAIT since we close actively */
7+
pcb->state = TIME_WAIT;
8+
TCP_REG(&tcp_tw_pcbs, pcb);
9+
+ TCP_TW_LIMIT(MEMP_NUM_TCP_PCB_TIME_WAIT);
10+
} else {
11+
/* CLOSE_WAIT: deallocate the pcb since we already sent a RST for it */
12+
if (tcp_input_pcb == pcb) {
13+
@@ -1523,7 +1524,7 @@ tcp_kill_state(enum tcp_state state)
14+
* Kills the oldest connection that is in TIME_WAIT state.
15+
* Called from tcp_alloc() if no more connections are available.
16+
*/
17+
-static void
18+
+void
19+
tcp_kill_timewait(void)
20+
{
21+
struct tcp_pcb *pcb, *inactive;
22+
diff --git a/src/core/tcp_in.c b/src/core/tcp_in.c
23+
index 5e839ff..41c7d43 100644
24+
--- a/src/core/tcp_in.c
25+
+++ b/src/core/tcp_in.c
26+
@@ -916,6 +916,7 @@ tcp_process(struct tcp_pcb *pcb)
27+
TCP_RMV_ACTIVE(pcb);
28+
pcb->state = TIME_WAIT;
29+
TCP_REG(&tcp_tw_pcbs, pcb);
30+
+ TCP_TW_LIMIT(MEMP_NUM_TCP_PCB_TIME_WAIT);
31+
} else {
32+
tcp_ack_now(pcb);
33+
pcb->state = CLOSING;
34+
@@ -934,6 +935,7 @@ tcp_process(struct tcp_pcb *pcb)
35+
TCP_RMV_ACTIVE(pcb);
36+
pcb->state = TIME_WAIT;
37+
TCP_REG(&tcp_tw_pcbs, pcb);
38+
+ TCP_TW_LIMIT(MEMP_NUM_TCP_PCB_TIME_WAIT);
39+
}
40+
break;
41+
case CLOSING:
42+
@@ -944,6 +946,7 @@ tcp_process(struct tcp_pcb *pcb)
43+
TCP_RMV_ACTIVE(pcb);
44+
pcb->state = TIME_WAIT;
45+
TCP_REG(&tcp_tw_pcbs, pcb);
46+
+ TCP_TW_LIMIT(MEMP_NUM_TCP_PCB_TIME_WAIT);
47+
}
48+
break;
49+
case LAST_ACK:

0 commit comments

Comments
 (0)