From a82054d24ff00add7ca423e36327c4be3831c910 Mon Sep 17 00:00:00 2001 From: Dirk Ziegelmeier Date: Mon, 13 Nov 2017 22:13:11 +0100 Subject: [PATCH] Follow-up to patch #9472: tcp_kill_prio: Don't kill active connection that has the same priority Correctly search for connection with lowest prio AND longest inactivity time --- src/core/tcp.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/core/tcp.c b/src/core/tcp.c index 1394f09a..f418f4c5 100644 --- a/src/core/tcp.c +++ b/src/core/tcp.c @@ -1635,25 +1635,29 @@ tcp_kill_prio(u8_t prio) mprio = LWIP_MIN(TCP_PRIO_MAX, prio); /* We want to kill connections with a lower prio, so bail out if - * supplied prio is 0 - there cannot be a no lower prio + * supplied prio is 0 - there can never be a lower prio */ if (mprio == 0) { return; } - /* We want kill connections with a lower prio, so decrement prio by one - * and start searching for oldest connection with same or lower prio than mprio. + /* We only want kill connections with a lower prio, so decrement prio by one + * and start searching for oldest connection with same or lower priority than mprio. + * We want to find the connections with the lowest possible prio, and among + * these the one with the longest inactivity time. */ mprio--; inactivity = 0; inactive = NULL; for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) { - if (pcb->prio <= mprio && - (u32_t)(tcp_ticks - pcb->tmr) >= inactivity) { + /* lower prio is always a kill candidate */ + if ((pcb->prio < mprio) || + /* longer inactivity is also a kill candidate */ + ((pcb->prio == mprio) && ((u32_t)(tcp_ticks - pcb->tmr) >= inactivity))) { inactivity = tcp_ticks - pcb->tmr; - inactive = pcb; - mprio = pcb->prio; + inactive = pcb; + mprio = pcb->prio; } } if (inactive != NULL) {