From a7abf28e02282b32479f4bbaf2d90f09d2a60f4c Mon Sep 17 00:00:00 2001 From: xueyunfei Date: Thu, 9 Feb 2023 11:57:35 +0800 Subject: [PATCH] lwip:optimization dhcp coarse timer --- src/core/ipv4/dhcp.c | 43 +++++++++++++++++++++++------------------ src/include/lwip/dhcp.h | 6 ++---- test/unit/lwipopts.h | 6 ------ 3 files changed, 26 insertions(+), 29 deletions(-) diff --git a/src/core/ipv4/dhcp.c b/src/core/ipv4/dhcp.c index 88ce1186..39a2aca6 100644 --- a/src/core/ipv4/dhcp.c +++ b/src/core/ipv4/dhcp.c @@ -97,6 +97,26 @@ static bool is_tmr_start = false; #define ESP_LWIP_DHCP_FINE_CLOSE() #endif /* ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND */ +#ifndef DHCP_DEFINE_CUSTOM_TIMEOUTS +static inline u32_t timeout_from_offered(u32_t lease, u32_t min, u32_t max) +{ + u32_t timeout = (lease + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS; + if (timeout > max) { + timeout = max; + } + if (timeout == min) { + timeout = 1; + } + return timeout; +} + +#define DHCP_CALC_TIMEOUT_FROM_OFFERED_T0_LEASE(dhcp) timeout_from_offered((dhcp)->offered_t0_lease, 0, 0xffff) +#define DHCP_CALC_TIMEOUT_FROM_OFFERED_T1_RENEW(dhcp) timeout_from_offered((dhcp)->offered_t1_renew, 0, 0xffff) +#define DHCP_CALC_TIMEOUT_FROM_OFFERED_T2_REBIND(dhcp) timeout_from_offered((dhcp)->offered_t2_rebind, 0, 0xffff) +#define DHCP_REQUEST_TIMEOUT_SEQUENCE(state, tries) (u16_t)(((tries) < 6 ? 1 << (tries) : 60) * 1000) + +#endif /* DHCP_DEFINE_CUSTOM_TIMEOUTS */ + #ifdef LWIP_HOOK_FILENAME #include LWIP_HOOK_FILENAME #endif @@ -147,9 +167,6 @@ static bool is_tmr_start = false; static u32_t dhcp_option_vsi[DHCP_OPTION_VSI_MAX] = {0}; #endif -#ifndef DHCP_DEFINE_CUSTOM_TIMEOUTS -#define DHCP_REQUEST_TIMEOUT_SEQUENCE(state, tries) (u16_t)(( (tries) < 6 ? 1 << (tries) : 60) * 1000) -#endif /** Option handling: options are parsed in dhcp_parse_reply * and saved in an array where other functions can load them from. * This might be moved into the struct dhcp (not necessarily since @@ -1263,7 +1280,6 @@ dhcp_discover(struct netif *netif) static void dhcp_bind(struct netif *netif) { - u32_t timeout; struct dhcp *dhcp; ip4_addr_t sn_mask, gw_addr; LWIP_ERROR("dhcp_bind: netif != NULL", (netif != NULL), return;); @@ -1278,11 +1294,7 @@ dhcp_bind(struct netif *netif) if (dhcp->offered_t0_lease != 0xffffffffUL) { /* set renewal period timer */ LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t0 renewal timer %"U32_F" secs\n", dhcp->offered_t0_lease)); - timeout = dhcp->offered_t0_lease; - dhcp->t0_timeout = timeout; - if (dhcp->t0_timeout == 0) { - dhcp->t0_timeout = 120; - } + dhcp->t0_timeout = DHCP_CALC_TIMEOUT_FROM_OFFERED_T0_LEASE(dhcp); LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t0_lease*1000)); } @@ -1290,22 +1302,14 @@ dhcp_bind(struct netif *netif) if (dhcp->offered_t1_renew != 0xffffffffUL) { /* set renewal period timer */ LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t1 renewal timer %"U32_F" secs\n", dhcp->offered_t1_renew)); - timeout = dhcp->offered_t1_renew; - dhcp->t1_timeout = timeout; - if (dhcp->t1_timeout == 0) { - dhcp->t1_timeout = dhcp->t0_timeout>>1; - } + dhcp->t1_timeout = DHCP_CALC_TIMEOUT_FROM_OFFERED_T1_RENEW(dhcp); LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t1_renew*1000)); dhcp->t1_renew_time = dhcp->t1_timeout; } /* set renewal period timer */ if (dhcp->offered_t2_rebind != 0xffffffffUL) { LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t2 rebind timer %"U32_F" secs\n", dhcp->offered_t2_rebind)); - timeout = dhcp->offered_t2_rebind; - dhcp->t2_timeout = timeout; - if (dhcp->t2_timeout == 0) { - dhcp->t2_timeout = (dhcp->t0_timeout>>3)*7; - } + dhcp->t2_timeout = DHCP_CALC_TIMEOUT_FROM_OFFERED_T2_REBIND(dhcp); LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t2_rebind*1000)); dhcp->t2_rebind_time = dhcp->t2_timeout; } @@ -1315,6 +1319,7 @@ dhcp_bind(struct netif *netif) dhcp->t1_timeout = 0; } #else + u32_t timeout; if (dhcp->offered_t0_lease != 0xffffffffUL) { /* set renewal period timer */ LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t0 renewal timer %"U32_F" secs\n", dhcp->offered_t0_lease)); diff --git a/src/include/lwip/dhcp.h b/src/include/lwip/dhcp.h index 56e35223..bea1354c 100644 --- a/src/include/lwip/dhcp.h +++ b/src/include/lwip/dhcp.h @@ -50,11 +50,9 @@ extern "C" { #endif /** period (in seconds) of the application calling dhcp_coarse_tmr() */ -#if ESP_DHCP +#ifndef DHCP_COARSE_TIMER_SECS #define DHCP_COARSE_TIMER_SECS 1 -#else -#define DHCP_COARSE_TIMER_SECS 60 -#endif +#endif /* DHCP_COARSE_TIMER_SECS */ /** period (in milliseconds) of the application calling dhcp_coarse_tmr() */ #define DHCP_COARSE_TIMER_MSECS (DHCP_COARSE_TIMER_SECS * 1000UL) /** period (in milliseconds) of the application calling dhcp_fine_tmr() */ diff --git a/test/unit/lwipopts.h b/test/unit/lwipopts.h index 8a08ae1d..d70a9758 100644 --- a/test/unit/lwipopts.h +++ b/test/unit/lwipopts.h @@ -116,12 +116,6 @@ #define ESP_DHCP_DISABLE_VENDOR_CLASS_IDENTIFIER 0 #define ESP_IP_FORWARD 1 -/* DHCP options*/ -#define DHCP_DEFINE_CUSTOM_TIMEOUTS 1 -#define DHCP_REQUEST_TIMEOUT_SEQUENCE(state, tries) (state == DHCP_STATE_REQUESTING ? \ - (uint16_t)(1 * 1000) : \ - (uint16_t)(((tries) < 6 ? 1 << (tries) : 60) * 250)) - #ifdef IP_NAPT #define IP_NAPT_MAX 16 #undef LWIP_RAND