Skip to content

Commit

Permalink
lwip timer:optimization dhcp fine timer
Browse files Browse the repository at this point in the history
  • Loading branch information
freakyxue committed Dec 6, 2022
1 parent 17f41c9 commit 7918216
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/core/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@
#include "lwip/memp.h"
#include "lwip/dns.h"
#include "lwip/prot/dns.h"
#include "lwip/timeouts.h"

#if ESP_LWIP_DNS_TIMERS_ONDEMAND
#include "stdbool.h"
#include "lwip/timeouts.h"
static bool is_tmr_start = false;
#endif /* ESP_LWIP_DNS_TIMERS_ONDEMAND */

Expand Down
50 changes: 50 additions & 0 deletions src/core/ipv4/dhcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@

#include <string.h>

#if ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND
#include <stdbool.h>
#include "lwip/timeouts.h"
static bool is_tmr_start = false;
#define ESP_LWIP_DHCP_FINE_TIMER_START_ONCE() if (!is_tmr_start) { \
sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_fine_timeout_cb, NULL); \
is_tmr_start = true; }
#define ESP_LWIP_DHCP_FINE_CLOSE() if (is_tmr_start) { \
sys_untimeout(dhcp_fine_timeout_cb, NULL); \
is_tmr_start = false; }
#else
#define ESP_LWIP_DHCP_FINE_TIMER_START_ONCE()
#define ESP_LWIP_DHCP_FINE_CLOSE()
#endif /* ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND */

#ifdef LWIP_HOOK_FILENAME
#include LWIP_HOOK_FILENAME
#endif
Expand Down Expand Up @@ -285,6 +300,14 @@ dhcp_dec_pcb_refcount(void)
}
}

#if ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND
void dhcp_fine_timeout_cb(void *arg)
{
LWIP_UNUSED_ARG(arg);
dhcp_fine_tmr();
}
#endif

/**
* Back-off the DHCP client (because of a received NAK response).
*
Expand Down Expand Up @@ -319,6 +342,7 @@ dhcp_handle_nak(struct netif *netif)
}
/* We can immediately restart discovery */
dhcp_discover(netif);

}

#if DHCP_DOES_ARP_CHECK
Expand Down Expand Up @@ -353,6 +377,7 @@ dhcp_check(struct netif *netif)
msecs = 500;
dhcp->request_timeout = (u16_t)((msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS);
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_check(): set request timeout %"U16_F" msecs\n", msecs));
ESP_LWIP_DHCP_FINE_TIMER_START_ONCE();
}
#endif /* DHCP_DOES_ARP_CHECK */

Expand Down Expand Up @@ -473,6 +498,7 @@ dhcp_select(struct netif *netif)
msecs = (u16_t)((dhcp->tries < 6 ? 1 << dhcp->tries : 60) * 1000);
dhcp->request_timeout = (u16_t)((msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS);
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_select(): set request timeout %"U16_F" msecs\n", msecs));
ESP_LWIP_DHCP_FINE_TIMER_START_ONCE();
return result;
}

Expand Down Expand Up @@ -541,6 +567,9 @@ void
dhcp_fine_tmr(void)
{
struct netif *netif;
#if ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND
bool tmr_restart = false;
#endif /* ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND */
/* loop through netif's */
NETIF_FOREACH(netif) {
struct dhcp *dhcp = netif_dhcp_data(netif);
Expand All @@ -549,13 +578,27 @@ dhcp_fine_tmr(void)
/* timer is active (non zero), and is about to trigger now */
if (dhcp->request_timeout > 1) {
dhcp->request_timeout--;
#if ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND
tmr_restart = true;
#endif
} else if (dhcp->request_timeout == 1) {
dhcp->request_timeout--;
/* { dhcp->request_timeout == 0 } */
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_fine_tmr(): request timeout\n"));
/* this client's request timeout triggered */
dhcp_timeout(netif);
#if ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND
tmr_restart = true;
#endif
}
#if ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND
if (tmr_restart) {
sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_fine_timeout_cb, NULL);
} else {
sys_untimeout(dhcp_fine_timeout_cb, NULL);
is_tmr_start = false;
}
#endif
}
}
}
Expand Down Expand Up @@ -940,6 +983,7 @@ dhcp_start(struct netif *netif)
}
#endif

ESP_LWIP_DHCP_FINE_CLOSE();
/* (re)start the DHCP negotiation */
result = dhcp_discover(netif);
if (result != ERR_OK) {
Expand Down Expand Up @@ -1126,6 +1170,7 @@ dhcp_decline(struct netif *netif)
msecs = 10 * 1000;
dhcp->request_timeout = (u16_t)((msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS);
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_decline(): set request timeout %"U16_F" msecs\n", msecs));
ESP_LWIP_DHCP_FINE_TIMER_START_ONCE();
return result;
}
#endif /* DHCP_DOES_ARP_CHECK */
Expand Down Expand Up @@ -1209,6 +1254,7 @@ dhcp_discover(struct netif *netif)
#endif
dhcp->request_timeout = (u16_t)((msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS);
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_discover(): set request timeout %"U16_F" msecs\n", msecs));
ESP_LWIP_DHCP_FINE_TIMER_START_ONCE();
return result;
}

Expand Down Expand Up @@ -1359,6 +1405,7 @@ dhcp_bind(struct netif *netif)
/* netif is now bound to DHCP leased address - set this before assigning the address
to ensure the callback can use dhcp_supplied_address() */
dhcp_set_state(dhcp, DHCP_STATE_BOUND);
ESP_LWIP_DHCP_FINE_CLOSE();
LWIP_DEBUGF(ESP_DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_bind(): dhcp state is BOUND\n"));
netif_set_addr(netif, &dhcp->offered_ip_addr, &sn_mask, &gw_addr);
/* interface is used by routing now that an address is set */
Expand Down Expand Up @@ -1440,6 +1487,7 @@ dhcp_renew(struct netif *netif)
msecs = (u16_t)(dhcp->tries < 10 ? dhcp->tries * 2000 : 20 * 1000);
dhcp->request_timeout = (u16_t)((msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS);
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_renew(): set request timeout %"U16_F" msecs\n", msecs));
ESP_LWIP_DHCP_FINE_TIMER_START_ONCE();
return result;
}

Expand Down Expand Up @@ -1502,6 +1550,7 @@ dhcp_rebind(struct netif *netif)
msecs = (u16_t)(dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000);
dhcp->request_timeout = (u16_t)((msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS);
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind(): set request timeout %"U16_F" msecs\n", msecs));
ESP_LWIP_DHCP_FINE_TIMER_START_ONCE();
return result;
}

Expand Down Expand Up @@ -1567,6 +1616,7 @@ dhcp_reboot(struct netif *netif)
msecs = (u16_t)(dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000);
dhcp->request_timeout = (u16_t)((msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS);
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_reboot(): set request timeout %"U16_F" msecs\n", msecs));
ESP_LWIP_DHCP_FINE_TIMER_START_ONCE();
return result;
}

Expand Down
4 changes: 3 additions & 1 deletion src/core/timeouts.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ const struct lwip_cyclic_timer lwip_cyclic_timers[] = {
#endif /* LWIP_ARP */
#if LWIP_DHCP
{DHCP_COARSE_TIMER_MSECS, HANDLER(dhcp_coarse_tmr)},
{DHCP_FINE_TIMER_MSECS, HANDLER(dhcp_fine_tmr)},
#endif /* LWIP_DHCP */
#if LWIP_DHCP && !ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND
{DHCP_FINE_TIMER_MSECS, HANDLER(dhcp_fine_tmr)},
#endif /* LWIP_DHCP && !ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND */
#if ESP_DHCPS_TIMER
{DHCP_COARSE_TIMER_MSECS, HANDLER(dhcps_coarse_tmr)},
#endif
Expand Down
3 changes: 3 additions & 0 deletions src/include/lwip/dhcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ u8_t dhcp_supplied_address(const struct netif *netif);
void dhcp_coarse_tmr(void);
/* to be called every half second */
void dhcp_fine_tmr(void);
#if ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND
void dhcp_fine_timeout_cb(void *arg);
#endif

#if LWIP_DHCP_GET_NTP_SRV
/** This function must exist, in other to add offered NTP servers to
Expand Down
2 changes: 1 addition & 1 deletion src/include/lwip/opt.h
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@
* The default number of timeouts is calculated here for all enabled modules.
*/
#if ESP_LWIP
#define LWIP_NUM_SYS_TIMEOUT_INTERNAL (LWIP_TCP + IP_REASSEMBLY + (LWIP_ARP + (ESP_GRATUITOUS_ARP ? 1 : 0)) + (2*LWIP_DHCP + (ESP_DHCPS_TIMER ? 1 : 0)) + LWIP_AUTOIP + LWIP_IGMP + LWIP_DNS + PPP_NUM_TIMEOUTS + (LWIP_IPV6 * (1 + LWIP_IPV6_REASS + LWIP_IPV6_MLD)))
#define LWIP_NUM_SYS_TIMEOUT_INTERNAL (LWIP_TCP + IP_REASSEMBLY + (LWIP_ARP + (ESP_GRATUITOUS_ARP ? 1 : 0)) + (ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND ? LWIP_DHCP : 2*LWIP_DHCP + (ESP_DHCPS_TIMER ? 1 : 0)) + LWIP_AUTOIP + LWIP_IGMP + (ESP_LWIP_DNS_TIMERS_ONDEMAND ? 0 : LWIP_DNS) + PPP_NUM_TIMEOUTS + (LWIP_IPV6 * (1 + LWIP_IPV6_REASS + LWIP_IPV6_MLD)))
#else
#define LWIP_NUM_SYS_TIMEOUT_INTERNAL (LWIP_TCP + IP_REASSEMBLY + LWIP_ARP + (2*LWIP_DHCP) + LWIP_AUTOIP + LWIP_IGMP + LWIP_DNS + PPP_NUM_TIMEOUTS + (LWIP_IPV6 * (1 + LWIP_IPV6_REASS + LWIP_IPV6_MLD)))
#endif
Expand Down
4 changes: 4 additions & 0 deletions test/unit/dhcp/test_dhcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "lwip/prot/dhcp.h"
#include "lwip/etharp.h"
#include "netif/ethernet.h"
#include "lwip/timeouts.h"

struct netif net_test;

Expand Down Expand Up @@ -133,6 +134,9 @@ static void tick_lwip(void)
{
tick++;
if (tick % 5 == 0) {
#if ESP_LWIP_DHCP_FINE_TIMERS_ONDEMAND
sys_untimeout(dhcp_fine_timeout_cb, NULL);
#endif
dhcp_fine_tmr();
}
#if ESP_DHCP
Expand Down
1 change: 1 addition & 0 deletions test/unit/lwipopts.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
#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_LWIP_DHCP_FINE_TIMERS_ONDEMAND 1
#define ESP_GRATUITOUS_ARP 1
#define ESP_LWIP_SELECT 1
#define ESP_LWIP_LOCK 1
Expand Down

0 comments on commit 7918216

Please sign in to comment.