add dhcp correlation

This commit is contained in:
xueyunfei
2019-06-28 18:28:41 +08:00
parent cac95ecea5
commit 03d5d6d2d6
8 changed files with 189 additions and 5 deletions
+126 -3
View File
@@ -285,6 +285,13 @@ dhcp_handle_nak(struct netif *netif)
dhcp_set_state(dhcp, DHCP_STATE_BACKING_OFF);
/* remove IP address from interface (must no longer be used, as per RFC2131) */
netif_set_addr(netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4);
if (dhcp->cb != NULL) {
#ifdef ESP_DHCP
dhcp->cb(netif);
#else
dhcp->cb();
#endif
}
/* We can immediately restart discovery */
dhcp_discover(netif);
}
@@ -558,7 +565,12 @@ dhcp_t1_timeout(struct netif *netif)
DHCP_STATE_RENEWING, not DHCP_STATE_BOUND */
dhcp_renew(netif);
/* Calculate next timeout */
if (((dhcp->t2_timeout - dhcp->lease_used) / 2) >= ((60 + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS)) {
#if ESP_DHCP
if (((dhcp->t2_timeout - dhcp->lease_used) / 2)*DHCP_COARSE_TIMER_SECS >= 3)
#else
if (((dhcp->t2_timeout - dhcp->lease_used) / 2) >= ((60 + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS))
#endif
{
dhcp->t1_renew_time = (u16_t)((dhcp->t2_timeout - dhcp->lease_used) / 2);
}
}
@@ -584,7 +596,12 @@ dhcp_t2_timeout(struct netif *netif)
DHCP_STATE_REBINDING, not DHCP_STATE_BOUND */
dhcp_rebind(netif);
/* Calculate next timeout */
if (((dhcp->t0_timeout - dhcp->lease_used) / 2) >= ((60 + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS)) {
#if ESP_DHCP
if (((dhcp->t0_timeout - dhcp->lease_used) / 2)*DHCP_COARSE_TIMER_SECS >= 3)
#else
if (((dhcp->t0_timeout - dhcp->lease_used) / 2) >= ((60 + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS))
#endif
{
dhcp->t2_rebind_time = (u16_t)((dhcp->t0_timeout - dhcp->lease_used) / 2);
}
}
@@ -725,6 +742,31 @@ void dhcp_cleanup(struct netif *netif)
}
}
/* Espressif add start. */
/** Set callback for dhcp, reserved parameter for future use.
*
* @param netif the netif from which to remove the struct dhcp
* @param cb callback for dhcp
*/
#ifdef ESP_DHCP
void dhcp_set_cb(struct netif *netif, void (*cb)(struct netif*))
#else
void dhcp_set_cb(struct netif *netif, void (*cb)(void))
#endif
{
struct dhcp *dhcp;
dhcp = netif_dhcp_data(netif);
LWIP_ASSERT("netif != NULL", netif != NULL);
if (dhcp != NULL) {
dhcp->cb = cb;
}
}
/* Espressif add end. */
/**
* @ingroup dhcp4
* Start DHCP negotiation for a network interface.
@@ -795,6 +837,16 @@ dhcp_start(struct netif *netif)
return ERR_OK;
}
#ifdef ESP_DHCP
/* Try to restore last valid ip address obtained from DHCP server.*/
/* If no valid ip is available, run dhcp_discover instead.*/
if(LWIP_DHCP_IP_ADDR_RESTORE()) {
dhcp_set_state(dhcp, DHCP_STATE_BOUND);
dhcp_network_changed(netif);
return ERR_OK;
}
#endif
/* (re)start the DHCP negotiation */
result = dhcp_discover(netif);
if (result != ERR_OK) {
@@ -1029,7 +1081,14 @@ dhcp_discover(struct netif *netif)
autoip_start(netif);
}
#endif /* LWIP_DHCP_AUTOIP_COOP */
msecs = (u16_t)((dhcp->tries < 6 ? 1 << dhcp->tries : 60) * 1000);
#if ESP_DHCP
/* Since for embedded devices it's not that hard to miss a discover packet, so lower
* the discover retry backoff time from (2,4,8,16,32,60,60)s to (500m,1,2,4,8,15,15)s.
*/
msecs = (dhcp->tries < 6 ? 1 << dhcp->tries : 60) * 250;
#else
msecs = (dhcp->tries < 6 ? 1 << dhcp->tries : 60) * 1000;
#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));
return result;
@@ -1055,6 +1114,47 @@ dhcp_bind(struct netif *netif)
/* reset time used of lease */
dhcp->lease_used = 0;
#if ESP_DHCP
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;
}
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t0_lease*1000));
}
/* temporary DHCP lease? */
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;
}
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;
}
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;
}
/* If we have sub 1 minute lease, t2 and t1 will kick in at the same time. */
if ((dhcp->t1_timeout >= dhcp->t2_timeout) && (dhcp->t2_timeout > 0)) {
dhcp->t1_timeout = 0;
}
#else
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));
@@ -1103,6 +1203,7 @@ dhcp_bind(struct netif *netif)
if ((dhcp->t1_timeout >= dhcp->t2_timeout) && (dhcp->t2_timeout > 0)) {
dhcp->t1_timeout = 0;
}
#endif
if (dhcp->subnet_mask_given) {
/* copy offered network mask */
@@ -1143,6 +1244,20 @@ dhcp_bind(struct netif *netif)
netif_set_addr(netif, &dhcp->offered_ip_addr, &sn_mask, &gw_addr);
/* interface is used by routing now that an address is set */
/* Espressif add start. */
#ifdef ESP_DHCP
LWIP_DHCP_IP_ADDR_STORE();
#endif
if (dhcp->cb != NULL) {
#ifdef ESP_DHCP
dhcp->cb(netif);
#else
dhcp->cb();
#endif
}
/* Espressif add end. */
}
/**
@@ -1373,6 +1488,14 @@ dhcp_release_and_stop(struct netif *netif)
/* remove IP address from interface (prevents routing from selecting this interface) */
netif_set_addr(netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4);
if (dhcp->cb != NULL) {
#ifdef ESP_DHCP
dhcp->cb(netif);
#else
dhcp->cb();
#endif
}
}
#if LWIP_DHCP_AUTOIP_COOP
+6
View File
@@ -776,6 +776,12 @@ netif_remove(struct netif *netif)
/* set netif down before removing (call callback function) */
netif_set_down(netif);
}
#if ESP_DHCP
#if LWIP_DHCP
/* netif not under DHCP control by default */
dhcp_cleanup(netif);
#endif /* LWIP_DHCP */
#endif
mib2_remove_ip4(netif);
+7
View File
@@ -69,6 +69,10 @@
#define LWIP_MAX_TIMEOUT 0x7fffffff
#if ESP_DHCPS_TIMER
extern void dhcps_coarse_tmr(void);
#endif
/* Check if timer's expiry time is greater than time and care about u32_t wraparounds */
#define TIME_LESS_THAN(t, compare_to) ( (((u32_t)((t)-(compare_to))) > LWIP_MAX_TIMEOUT) ? 1 : 0 )
@@ -91,6 +95,9 @@ const struct lwip_cyclic_timer lwip_cyclic_timers[] = {
{DHCP_COARSE_TIMER_MSECS, HANDLER(dhcp_coarse_tmr)},
{DHCP_FINE_TIMER_MSECS, HANDLER(dhcp_fine_tmr)},
#endif /* LWIP_DHCP */
#if ESP_DHCPS_TIMER
{DHCP_COARSE_TIMER_MSECS, HANDLER(dhcps_coarse_tmr)},
#endif
#if LWIP_AUTOIP
{AUTOIP_TMR_INTERVAL, HANDLER(autoip_tmr)},
#endif /* LWIP_AUTOIP */
+23 -1
View File
@@ -50,7 +50,11 @@ extern "C" {
#endif
/** period (in seconds) of the application calling dhcp_coarse_tmr() */
#define DHCP_COARSE_TIMER_SECS 60
#if ESP_DHCP
#define DHCP_COARSE_TIMER_SECS 1
#else
#define DHCP_COARSE_TIMER_SECS 60
#endif
/** 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() */
@@ -98,6 +102,14 @@ struct dhcp
ip4_addr_t offered_si_addr;
char boot_file_name[DHCP_BOOT_FILE_LEN];
#endif /* LWIP_DHCP_BOOTPFILE */
/* Espressif add start. */
#ifdef ESP_DHCP
void (*cb)(struct netif*); /* callback for dhcp, add a parameter to show dhcp status if needed */
#else
void (*cb)(void); /* callback for dhcp, add a parameter to show dhcp status if needed */
#endif
/* Espressif add end. */
};
@@ -112,6 +124,16 @@ void dhcp_stop(struct netif *netif);
void dhcp_release_and_stop(struct netif *netif);
void dhcp_inform(struct netif *netif);
void dhcp_network_changed(struct netif *netif);
/* Espressif add start. */
/** set callback for DHCP */
#ifdef ESP_DHCP
void dhcp_set_cb(struct netif *netif, void (*cb)(struct netif*));
#else
void dhcp_set_cb(struct netif *netif, void (*cb)(void));
#endif
/* Espressif add end. */
#if DHCP_DOES_ARP_CHECK
void dhcp_arp_reply(struct netif *netif, const ip4_addr_t *addr);
#endif
+11
View File
@@ -254,6 +254,11 @@ struct netif_hint {
#define LWIP_NETIF_USE_HINTS 0
#endif /* LWIP_NETIF_HWADDRHINT */
#if ESP_DHCP
/*add DHCP event processing by LiuHan*/
typedef void (*dhcp_event_fn)(void);
#endif
/** Generic data structure used for all lwIP network interfaces.
* The following fields should be filled in by the initialization
* function for the device driver: hwaddr_len, hwaddr[], mtu, flags */
@@ -324,6 +329,12 @@ struct netif {
#ifdef netif_get_client_data
void* client_data[LWIP_NETIF_CLIENT_DATA_INDEX_MAX + LWIP_NUM_NETIF_CLIENT_DATA];
#endif
#if ESP_DHCP
struct udp_pcb *dhcps_pcb;
dhcp_event_fn dhcp_event;
#endif
#if LWIP_NETIF_HOSTNAME
/* the hostname for this netif, NULL is a valid value */
const char* hostname;
+12
View File
@@ -965,6 +965,18 @@
* @}
*/
#ifndef LWIP_DHCP_IP_ADDR_RESTORE
#define LWIP_DHCP_IP_ADDR_RESTORE() 0
#endif
#ifndef LWIP_DHCP_IP_ADDR_STORE
#define LWIP_DHCP_IP_ADDR_STORE()
#endif
#ifndef LWIP_DHCP_IP_ADDR_ERASE
#define LWIP_DHCP_IP_ADDR_ERASE()
#endif
/*
------------------------------------
---------- AUTOIP options ----------
+4
View File
@@ -135,7 +135,11 @@ static void tick_lwip(void)
if (tick % 5 == 0) {
dhcp_fine_tmr();
}
#if ESP_DHCP
if (tick % 10 == 0) {
#else
if (tick % 600 == 0) {
#endif
dhcp_coarse_tmr();
}
}
-1
View File
@@ -88,7 +88,6 @@
/* ESP specific cofiguration */
#define ESP_LWIP 1
#define ESP_DHCP 1
#define ESP_DHCP_TIMER 1
#define ESP_DHCPS_TIMER 0
#define ESP_STATS_DROP 0
#define ESP_PBUF 1