Skip to content

Commit

Permalink
dhcp: Enable custom config for timeouts, thresholds, backoff time
Browse files Browse the repository at this point in the history
Enables to configure:
* DHCP_COARSE_TIMER_SECS
* if DHCP_DEFINE_CUSTOM_TIMEOUTS defined these addtional options are
available:
  - DHCP_CALC_TIMEOUT_FROM_OFFERED_T0_LEASE to adjust the t0 lease timeout from the offered value
  - DHCP_CALC_TIMEOUT_FROM_OFFERED_T1_RENEW same for t1 renew
  - DHCP_CALC_TIMEOUT_FROM_OFFERED_T2_REBIND same for t2 rebind
  - DHCP_NEXT_TIMEOUT_THRESHOLD to adjust the period of the next timeout
  - DHCP_REQUEST_BACKOFF_SEQUENCE to adjust back-off times based on DHCP
request attempts
Also updates timeout type from u16 to u32 - eps useful when DHCP_COARSE_TIMER_SECS is a smaller number
  • Loading branch information
freakyxue authored and goldsimon committed May 11, 2023
1 parent 00f5e17 commit 5231c8d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
16 changes: 12 additions & 4 deletions src/core/ipv4/dhcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,14 @@
#define LWIP_HOOK_DHCP_PARSE_OPTION(netif, dhcp, state, msg, msg_type, option, len, pbuf, offset) do { LWIP_UNUSED_ARG(msg); } while(0)
#endif

/** Calculating and setting DHCP timeouts, thresholds and backoff sequences
/** DHCP_DEFINE_CUSTOM_TIMEOUTS: if this is defined then you can customize various DHCP timeouts using these macros:
- DHCP_SET_TIMEOUT_FROM_OFFERED_T0_LEASE() to adjust the t0 lease timeout from the offered value
- DHCP_SET_TIMEOUT_FROM_OFFERED_T1_RENEW() same for t1 renew
- DHCP_SET_TIMEOUT_FROM_OFFERED_T2_REBIND() same for t2 rebind
- DHCP_NEXT_TIMEOUT_THRESHOLD to adjust the period of the next timeout
- DHCP_REQUEST_BACKOFF_SEQUENCE to adjust back-off times based on DHCP request attempts
*/
#ifndef DHCP_DEFINE_CUSTOM_TIMEOUTS
#define SET_TIMEOUT_FROM_OFFERED(result, offered, min, max) do { \
u32_t timeout = (offered + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS; \
if (timeout > max) { \
Expand All @@ -103,7 +109,7 @@
if (timeout == min) { \
timeout = 1; \
} \
result = (u16_t)timeout; \
result = (dhcp_timeout_t)timeout; \
} while(0)

#define DHCP_SET_TIMEOUT_FROM_OFFERED_T0_LEASE(res, dhcp) SET_TIMEOUT_FROM_OFFERED(res, (dhcp)->offered_t0_lease, 0, 0xffff)
Expand All @@ -113,6 +119,8 @@
#define DHCP_NEXT_TIMEOUT_THRESHOLD ((60 + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS)
#define DHCP_REQUEST_BACKOFF_SEQUENCE(tries) (u16_t)(( (tries) < 6 ? 1 << (tries) : 60) * 1000)

#endif /* DHCP_DEFINE_CUSTOM_TIMEOUTS */

/** DHCP_CREATE_RAND_XID: if this is set to 1, the xid is created using
* LWIP_RAND() (this overrides DHCP_GLOBAL_XID)
*/
Expand Down Expand Up @@ -605,7 +613,7 @@ dhcp_t1_timeout(struct netif *netif)
dhcp_renew(netif);
/* Calculate next timeout */
if (((dhcp->t2_timeout - dhcp->lease_used) / 2) >= DHCP_NEXT_TIMEOUT_THRESHOLD) {
dhcp->t1_renew_time = (u16_t)((dhcp->t2_timeout - dhcp->lease_used) / 2);
dhcp->t1_renew_time = (dhcp_timeout_t)((dhcp->t2_timeout - dhcp->lease_used) / 2);
}
}
}
Expand All @@ -631,7 +639,7 @@ dhcp_t2_timeout(struct netif *netif)
dhcp_rebind(netif);
/* Calculate next timeout */
if (((dhcp->t0_timeout - dhcp->lease_used) / 2) >= DHCP_NEXT_TIMEOUT_THRESHOLD) {
dhcp->t2_rebind_time = (u16_t)((dhcp->t0_timeout - dhcp->lease_used) / 2);
dhcp->t2_rebind_time = (dhcp_timeout_t)((dhcp->t0_timeout - dhcp->lease_used) / 2);
}
}
}
Expand Down
23 changes: 16 additions & 7 deletions src/include/lwip/dhcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,17 @@
extern "C" {
#endif

/** Define DHCP_TIMEOUT_SIZE_T in opt.h if you want use a different integer than u16_t.
* Especially useful if DHCP_COARSE_TIMER_SECS is in smaller units, so timeouts easily reach UINT16_MAX and more */
#ifdef DHCP_TIMEOUT_SIZE_T
typedef DHCP_TIMEOUT_SIZE_T dhcp_timeout_t;
#else /* DHCP_TIMEOUT_SIZE_T */
typedef u16_t dhcp_timeout_t;
#endif /* DHCP_TIMEOUT_SIZE_T*/
/** period (in seconds) of the application calling dhcp_coarse_tmr() */
#ifndef DHCP_COARSE_TIMER_SECS
#define DHCP_COARSE_TIMER_SECS 60
#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() */
Expand Down Expand Up @@ -84,13 +93,13 @@ struct dhcp
/** see DHCP_FLAG_* */
u8_t flags;

u16_t request_timeout; /* #ticks with period DHCP_FINE_TIMER_SECS for request timeout */
u16_t t1_timeout; /* #ticks with period DHCP_COARSE_TIMER_SECS for renewal time */
u16_t t2_timeout; /* #ticks with period DHCP_COARSE_TIMER_SECS for rebind time */
u16_t t1_renew_time; /* #ticks with period DHCP_COARSE_TIMER_SECS until next renew try */
u16_t t2_rebind_time; /* #ticks with period DHCP_COARSE_TIMER_SECS until next rebind try */
u16_t lease_used; /* #ticks with period DHCP_COARSE_TIMER_SECS since last received DHCP ack */
u16_t t0_timeout; /* #ticks with period DHCP_COARSE_TIMER_SECS for lease time */
dhcp_timeout_t request_timeout; /* #ticks with period DHCP_FINE_TIMER_SECS for request timeout */
dhcp_timeout_t t1_timeout; /* #ticks with period DHCP_COARSE_TIMER_SECS for renewal time */
dhcp_timeout_t t2_timeout; /* #ticks with period DHCP_COARSE_TIMER_SECS for rebind time */
dhcp_timeout_t t1_renew_time; /* #ticks with period DHCP_COARSE_TIMER_SECS until next renew try */
dhcp_timeout_t t2_rebind_time; /* #ticks with period DHCP_COARSE_TIMER_SECS until next rebind try */
dhcp_timeout_t lease_used; /* #ticks with period DHCP_COARSE_TIMER_SECS since last received DHCP ack */
dhcp_timeout_t t0_timeout; /* #ticks with period DHCP_COARSE_TIMER_SECS for lease time */
ip_addr_t server_ip_addr; /* dhcp server address that offered this lease (ip_addr_t because passed to UDP) */
ip4_addr_t offered_ip_addr;
ip4_addr_t offered_sn_mask;
Expand Down

0 comments on commit 5231c8d

Please sign in to comment.