From c23e4b05ce38026accd35471b62aa3290f36d044 Mon Sep 17 00:00:00 2001 From: xueyunfei Date: Mon, 1 Jul 2019 19:41:35 +0800 Subject: [PATCH] add code for sending gratuitous ARP periodically --- src/core/ipv4/etharp.c | 16 ++++++++++++++++ src/core/netif.c | 11 +++++++++++ src/core/timeouts.c | 3 +++ src/include/lwip/etharp.h | 10 ++++++++++ src/include/lwip/netif.h | 10 ++++++++++ src/include/lwip/opt.h | 3 ++- test/unit/lwipopts.h | 2 ++ 7 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/core/ipv4/etharp.c b/src/core/ipv4/etharp.c index 442aac08..b14a98fa 100644 --- a/src/core/ipv4/etharp.c +++ b/src/core/ipv4/etharp.c @@ -54,6 +54,7 @@ #include "lwip/autoip.h" #include "lwip/prot/iana.h" #include "netif/ethernet.h" +#include "lwip/netif.h" #include @@ -137,6 +138,21 @@ static err_t etharp_raw(struct netif *netif, const struct eth_addr *hwdst_addr, const ip4_addr_t *ipdst_addr, const u16_t opcode); +#if ESP_GRATUITOUS_ARP +void garp_tmr(void) +{ + struct netif* garp_netif = NULL; + + for (garp_netif = netif_list; garp_netif != NULL; garp_netif = garp_netif->next) { + if (netif_is_up(garp_netif) && netif_is_link_up(garp_netif) && !ip4_addr_isany_val(*netif_ip4_addr(garp_netif))) { + if ((garp_netif->flags & NETIF_FLAG_ETHARP) && (garp_netif->flags & NETIF_FLAG_GARP)) { + etharp_gratuitous(garp_netif); + } + } + } +} +#endif + #if ARP_QUEUEING /** * Free a complete queue of etharp entries diff --git a/src/core/netif.c b/src/core/netif.c index 4fca4a8c..d5ccdd5a 100644 --- a/src/core/netif.c +++ b/src/core/netif.c @@ -727,6 +727,17 @@ netif_set_addr(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t * } #endif /* LWIP_IPV4*/ + +/** + * Set the netif flags for GARP + */ +#if ESP_GRATUITOUS_ARP +void netif_set_garp_flag(struct netif *netif) +{ + netif->flags |= NETIF_FLAG_GARP; +} +#endif + /** * @ingroup netif * Remove a network interface from the list of lwIP netifs. diff --git a/src/core/timeouts.c b/src/core/timeouts.c index d8595d88..edc6e0e7 100644 --- a/src/core/timeouts.c +++ b/src/core/timeouts.c @@ -90,6 +90,9 @@ const struct lwip_cyclic_timer lwip_cyclic_timers[] = { #endif /* IP_REASSEMBLY */ #if LWIP_ARP {ARP_TMR_INTERVAL, HANDLER(etharp_tmr)}, +#if ESP_GRATUITOUS_ARP + {GARP_TMR_INTERVAL, HANDLER(garp_tmr)}, +#endif /* ESP_GRATUITOUS_ARP */ #endif /* LWIP_ARP */ #if LWIP_DHCP {DHCP_COARSE_TIMER_MSECS, HANDLER(dhcp_coarse_tmr)}, diff --git a/src/include/lwip/etharp.h b/src/include/lwip/etharp.h index 2036b246..43043d20 100644 --- a/src/include/lwip/etharp.h +++ b/src/include/lwip/etharp.h @@ -73,6 +73,16 @@ struct etharp_q_entry { }; #endif /* ARP_QUEUEING */ +#if ESP_GRATUITOUS_ARP +#ifdef CONFIG_GARP_TMR_INTERVAL +#define GARP_TMR_INTERVAL (CONFIG_GARP_TMR_INTERVAL*1000UL) +#else +#define GARP_TMR_INTERVAL 60000 +#endif + +void garp_tmr(void); +#endif + #define etharp_init() /* Compatibility define, no init needed. */ void etharp_tmr(void); ssize_t etharp_find_addr(struct netif *netif, const ip4_addr_t *ipaddr, diff --git a/src/include/lwip/netif.h b/src/include/lwip/netif.h index ebbc10a4..43858a68 100644 --- a/src/include/lwip/netif.h +++ b/src/include/lwip/netif.h @@ -106,6 +106,11 @@ extern "C" { * Set by the netif driver in its init function. */ #define NETIF_FLAG_MLD6 0x40U +#if ESP_GRATUITOUS_ARP +/** If set, the netif will send gratuitous ARP periodically */ +#define NETIF_FLAG_GARP 0x80U +#endif + /** * @} */ @@ -438,6 +443,11 @@ void netif_set_addr(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_add #else /* LWIP_IPV4 */ struct netif *netif_add(struct netif *netif, void *state, netif_init_fn init, netif_input_fn input); #endif /* LWIP_IPV4 */ + +#if ESP_GRATUITOUS_ARP +void netif_set_garp_flag(struct netif *netif); +#endif + void netif_remove(struct netif * netif); /* Returns a network interface given its name. The name is of the form diff --git a/src/include/lwip/opt.h b/src/include/lwip/opt.h index 9cd0fced..787ca131 100644 --- a/src/include/lwip/opt.h +++ b/src/include/lwip/opt.h @@ -505,8 +505,9 @@ * The number of sys timeouts used by the core stack (not apps) * The default number of timeouts is calculated here for all enabled modules. */ +#if !defined LWIP_NUM_SYS_TIMEOUT_INTERNAL || defined __DOXYGEN__ #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 /** * MEMP_NUM_SYS_TIMEOUT: the number of simultaneously active timeouts. * The default number of timeouts is calculated here for all enabled modules. diff --git a/test/unit/lwipopts.h b/test/unit/lwipopts.h index 63ca0898..8b53976d 100644 --- a/test/unit/lwipopts.h +++ b/test/unit/lwipopts.h @@ -98,5 +98,7 @@ #define ESP_PPP 1 #define ESP_LWIP_IGMP_TIMERS_ONDEMAND 1 #define ESP_LWIP_MLD6_TIMERS_ONDEMAND 1 +#define ESP_GRATUITOUS_ARP 1 + #endif /* LWIP_HDR_LWIPOPTS_H */