From 17f41c9fdf9ee184bc5f4987d8a2efd0050cd2ed Mon Sep 17 00:00:00 2001 From: xueyunfei Date: Thu, 18 Jun 2020 17:41:41 +0800 Subject: [PATCH] optimization lwip ip4 reassembly timer --- src/core/ipv4/ip4_frag.c | 36 ++++++++++++++++++++++++++++++++++++ src/core/timeouts.c | 2 +- test/unit/lwipopts.h | 2 +- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/core/ipv4/ip4_frag.c b/src/core/ipv4/ip4_frag.c index a445530e0..8c98d6e25 100644 --- a/src/core/ipv4/ip4_frag.c +++ b/src/core/ipv4/ip4_frag.c @@ -52,6 +52,13 @@ #include #if IP_REASSEMBLY + +#include "lwip/timeouts.h" +#if ESP_LWIP_IP4_REASSEMBLY_TIMERS_ONDEMAND +#include "stdbool.h" +static bool is_tmr_start = false; +#endif /* ESP_LWIP_IP4_REASSEMBLY_TIMERS_ONDEMAND */ + /** * The IP reassembly code currently has the following limitations: * - IP header options are not supported @@ -118,6 +125,15 @@ static u16_t ip_reass_pbufcount; static void ip_reass_dequeue_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev); static int ip_reass_free_complete_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev); +/** + * Wrapper function with matching prototype which calls the actual callback + */ +static void ip_reass_timeout_cb(void *arg) +{ + LWIP_UNUSED_ARG(arg); + ip_reass_tmr(); +} + /** * Reassembly timer base function * for both NO_SYS == 0 and 1 (!). @@ -128,6 +144,9 @@ void ip_reass_tmr(void) { struct ip_reassdata *r, *prev = NULL; +#if ESP_LWIP_IP4_REASSEMBLY_TIMERS_ONDEMAND + bool tmr_restart = false; +#endif /* ESP_LWIP_IP4_REASSEMBLY_TIMERS_ONDEMAND */ r = reassdatagrams; while (r != NULL) { @@ -138,6 +157,9 @@ ip_reass_tmr(void) LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass_tmr: timer dec %"U16_F"\n", (u16_t)r->timer)); prev = r; r = r->next; +#if ESP_LWIP_IP4_REASSEMBLY_TIMERS_ONDEMAND + tmr_restart = true; +#endif /* ESP_LWIP_IP4_REASSEMBLY_TIMERS_ONDEMAND */ } else { /* reassembly timed out */ struct ip_reassdata *tmp; @@ -149,6 +171,14 @@ ip_reass_tmr(void) ip_reass_free_complete_datagram(tmp, prev); } } +#if ESP_LWIP_IP4_REASSEMBLY_TIMERS_ONDEMAND + if (tmr_restart) { + sys_timeout(IP_TMR_INTERVAL, ip_reass_timeout_cb, NULL); + } else { + sys_untimeout(ip_reass_timeout_cb, NULL); + is_tmr_start = false; + } +#endif/* ESP_LWIP_IP4_REASSEMBLY_TIMERS_ONDEMAND */ } /** @@ -672,6 +702,12 @@ ip4_reass(struct pbuf *p) /* Return the pbuf chain */ return p; } +#if ESP_LWIP_IP4_REASSEMBLY_TIMERS_ONDEMAND + if (!is_tmr_start) { + sys_timeout(IP_TMR_INTERVAL, ip_reass_timeout_cb, NULL); + is_tmr_start = true; + } +#endif /* ESP_LWIP_IP4_REASSEMBLY_TIMERS_ONDEMAND */ /* the datagram is not (yet?) reassembled completely */ LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass_pbufcount: %d out\n", ip_reass_pbufcount)); return NULL; diff --git a/src/core/timeouts.c b/src/core/timeouts.c index 1045ebd60..778b0117a 100644 --- a/src/core/timeouts.c +++ b/src/core/timeouts.c @@ -85,7 +85,7 @@ const struct lwip_cyclic_timer lwip_cyclic_timers[] = { {TCP_TMR_INTERVAL, HANDLER(tcp_tmr)}, #endif /* LWIP_TCP */ #if LWIP_IPV4 -#if IP_REASSEMBLY +#if IP_REASSEMBLY && !ESP_LWIP_IP4_REASSEMBLY_TIMERS_ONDEMAND {IP_TMR_INTERVAL, HANDLER(ip_reass_tmr)}, #endif /* IP_REASSEMBLY */ #if LWIP_ARP diff --git a/test/unit/lwipopts.h b/test/unit/lwipopts.h index 055261c13..79f695969 100644 --- a/test/unit/lwipopts.h +++ b/test/unit/lwipopts.h @@ -104,6 +104,7 @@ #define ESP_LWIP_MLD6_TIMERS_ONDEMAND 1 #define ESP_LWIP_DNS_TIMERS_ONDEMAND 1 #define ESP_LWIP_IP6_REASSEMBLY_TIMERS_ONDEMAND 1 +#define ESP_LWIP_IP4_REASSEMBLY_TIMERS_ONDEMAND 1 #define ESP_GRATUITOUS_ARP 1 #define ESP_LWIP_SELECT 1 #define ESP_LWIP_LOCK 1 @@ -114,7 +115,6 @@ #define ESP_DHCP_DISABLE_VENDOR_CLASS_IDENTIFIER 0 #define ESP_IP_FORWARD 1 - #ifdef IP_NAPT #define IP_NAPT_MAX 16 #undef LWIP_RAND