From 8bd10b48080c76062e84e073f0aec5632c8a0fe7 Mon Sep 17 00:00:00 2001 From: zhangyanjiao Date: Tue, 11 Sep 2018 11:30:23 +0800 Subject: [PATCH] lwip: add code for sending gratuitous ARP periodically --- components/lwip/Kconfig | 17 +++++++++++++++++ components/lwip/core/netif.c | 10 ++++++++++ components/lwip/core/timers.c | 7 +++++++ components/lwip/include/lwip/lwip/netif.h | 10 ++++++++++ components/lwip/include/lwip/lwip/opt.h | 4 ++++ components/lwip/include/lwip/netif/etharp.h | 5 +++++ components/lwip/include/lwip/port/lwipopts.h | 1 + components/lwip/netif/etharp.c | 15 +++++++++++++++ components/tcpip_adapter/tcpip_adapter_lwip.c | 6 ++++++ 9 files changed, 75 insertions(+) diff --git a/components/lwip/Kconfig b/components/lwip/Kconfig index a20d15b84..354df38b8 100644 --- a/components/lwip/Kconfig +++ b/components/lwip/Kconfig @@ -107,6 +107,23 @@ config LWIP_ETHARP_TRUST_IP_MAC So the recommendation is to disable this option. Here the LAN peer means the other side to which the ESP station or soft-AP is connected. +config ESP_GRATUITOUS_ARP + bool "Send gratuitous ARP periodically" + default y + help + Enable this option allows to send gratuitous ARP periodically. + + This option solve the compatibility issues.If the ARP table of the AP is old, and the AP + doesn't send ARP request to update it's ARP table, this will lead to the STA sending IP packet fail. + Thus we send gratuitous ARP periodically to let AP update it's ARP table. + +config GARP_TMR_INTERVAL + int "GARP timer interval(seconds)" + default 60 + depends on ESP_GRATUITOUS_ARP + help + Set the timer interval for gratuitous ARP. The default value is 60s + config TCPIP_RECVMBOX_SIZE int "TCPIP task receive mail box size" default 32 diff --git a/components/lwip/core/netif.c b/components/lwip/core/netif.c index b5995f26f..9328fffd2 100755 --- a/components/lwip/core/netif.c +++ b/components/lwip/core/netif.c @@ -332,6 +332,16 @@ 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 + /** * Remove a network interface from the list of lwIP netifs. * diff --git a/components/lwip/core/timers.c b/components/lwip/core/timers.c index a23b82647..c04e5f785 100755 --- a/components/lwip/core/timers.c +++ b/components/lwip/core/timers.c @@ -70,6 +70,10 @@ extern void dhcps_coarse_tmr(void); #endif +#if ESP_GRATUITOUS_ARP +extern void garp_tmr(void); +#endif + /** This array contains all stack-internal cyclic timers. To get the number of * timers, use LWIP_ARRAYSIZE() */ const struct lwip_cyclic_timer lwip_cyclic_timers[] = { @@ -84,6 +88,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 #endif /* LWIP_ARP */ #if LWIP_DHCP {DHCP_COARSE_TIMER_MSECS, HANDLER(dhcp_coarse_tmr)}, diff --git a/components/lwip/include/lwip/lwip/netif.h b/components/lwip/include/lwip/lwip/netif.h index bd25b8217..41aecc6de 100755 --- a/components/lwip/include/lwip/lwip/netif.h +++ b/components/lwip/include/lwip/lwip/netif.h @@ -99,6 +99,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 + #if LWIP_CHECKSUM_CTRL_PER_NETIF #define NETIF_CHECKSUM_GEN_IP 0x0001 #define NETIF_CHECKSUM_GEN_UDP 0x0002 @@ -362,6 +367,11 @@ struct netif *netif_add(struct netif *netif, void netif_set_addr(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, const ip4_addr_t *gw); #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/components/lwip/include/lwip/lwip/opt.h b/components/lwip/include/lwip/lwip/opt.h index 343eb0c64..fee9759bf 100755 --- a/components/lwip/include/lwip/lwip/opt.h +++ b/components/lwip/include/lwip/lwip/opt.h @@ -318,8 +318,12 @@ * The formula expects settings to be either '0' or '1'. */ #ifndef MEMP_NUM_SYS_TIMEOUT +#if ESP_LWIP +#define MEMP_NUM_SYS_TIMEOUT (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_SUPPORT*6*MEMP_NUM_PPP_PCB) + (LWIP_IPV6 ? (1 + LWIP_IPV6_REASS + LWIP_IPV6_MLD) : 0)) +#else #define MEMP_NUM_SYS_TIMEOUT (LWIP_TCP + IP_REASSEMBLY + LWIP_ARP + (2*LWIP_DHCP) + LWIP_AUTOIP + LWIP_IGMP + LWIP_DNS + (PPP_SUPPORT*6*MEMP_NUM_PPP_PCB) + (LWIP_IPV6 ? (1 + LWIP_IPV6_REASS + LWIP_IPV6_MLD) : 0)) #endif +#endif /** * MEMP_NUM_NETBUF: the number of struct netbufs. diff --git a/components/lwip/include/lwip/netif/etharp.h b/components/lwip/include/lwip/netif/etharp.h index 3e25c389d..20d9bc674 100755 --- a/components/lwip/include/lwip/netif/etharp.h +++ b/components/lwip/include/lwip/netif/etharp.h @@ -102,6 +102,11 @@ struct etharp_q_entry { }; #endif /* ARP_QUEUEING */ +#if ESP_GRATUITOUS_ARP +#define GARP_TMR_INTERVAL (CONFIG_GARP_TMR_INTERVAL*1000UL) +void garp_tmr(void); +#endif + #define etharp_init() /* Compatibility define, no init needed. */ void etharp_tmr(void); s8_t etharp_find_addr(struct netif *netif, const ip4_addr_t *ipaddr, diff --git a/components/lwip/include/lwip/port/lwipopts.h b/components/lwip/include/lwip/port/lwipopts.h index 60ce331b3..be636f8d8 100644 --- a/components/lwip/include/lwip/port/lwipopts.h +++ b/components/lwip/include/lwip/port/lwipopts.h @@ -732,6 +732,7 @@ #define ESP_DHCP_TIMER 1 #define ESP_LWIP_LOGI(...) ESP_LOGI("lwip", __VA_ARGS__) #define ESP_PING 1 +#define ESP_GRATUITOUS_ARP CONFIG_ESP_GRATUITOUS_ARP #define TCP_WND_DEFAULT CONFIG_TCP_WND_DEFAULT #define TCP_SND_BUF_DEFAULT CONFIG_TCP_SND_BUF_DEFAULT diff --git a/components/lwip/netif/etharp.c b/components/lwip/netif/etharp.c index d9854dbb0..e8aed0889 100755 --- a/components/lwip/netif/etharp.c +++ b/components/lwip/netif/etharp.c @@ -52,6 +52,7 @@ #include "lwip/snmp.h" #include "lwip/dhcp.h" #include "lwip/autoip.h" +#include "lwip/netif.h" #include @@ -129,6 +130,20 @@ static u8_t etharp_cached_entry; static err_t etharp_request_dst(struct netif *netif, const ip4_addr_t *ipaddr, const struct eth_addr* hw_dst_addr); +#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 /** diff --git a/components/tcpip_adapter/tcpip_adapter_lwip.c b/components/tcpip_adapter/tcpip_adapter_lwip.c index 3a41bf46b..2aeccc05e 100644 --- a/components/tcpip_adapter/tcpip_adapter_lwip.c +++ b/components/tcpip_adapter/tcpip_adapter_lwip.c @@ -26,6 +26,7 @@ #include "lwip/ip6_addr.h" #include "lwip/nd6.h" #include "lwip/priv/tcpip_priv.h" +#include "lwip/netif.h" #if LWIP_DNS /* don't build if not configured for use in lwipopts.h */ #include "lwip/dns.h" #endif @@ -175,6 +176,11 @@ esp_err_t tcpip_adapter_start(tcpip_adapter_if_t tcpip_if, uint8_t *mac, tcpip_a netif_init = tcpip_if_to_netif_init_fn(tcpip_if); assert(netif_init != NULL); netif_add(esp_netif[tcpip_if], &ip_info->ip, &ip_info->netmask, &ip_info->gw, NULL, netif_init, tcpip_input); +#if ESP_GRATUITOUS_ARP + if (tcpip_if == TCPIP_ADAPTER_IF_STA || tcpip_if == TCPIP_ADAPTER_IF_ETH) { + netif_set_garp_flag(esp_netif[tcpip_if]); + } +#endif } if (tcpip_if == TCPIP_ADAPTER_IF_AP) {