net/arp: reducing unnecessary ARP requests can mitigate

network congestion and avoid packet delays caused by
waiting for ARP responses

Signed-off-by: nuttxs <zhaoqing.zhang@sony.com>
This commit is contained in:
nuttxs 2025-07-07 16:02:11 +08:00 committed by Alan C. Assis
parent 571447bb06
commit 22560958bc
6 changed files with 30 additions and 13 deletions

View file

@ -425,7 +425,7 @@ void arp_notify(in_addr_t ipaddr);
struct ether_addr; /* Forward reference */ struct ether_addr; /* Forward reference */
int arp_find(in_addr_t ipaddr, FAR uint8_t *ethaddr, int arp_find(in_addr_t ipaddr, FAR uint8_t *ethaddr,
FAR struct net_driver_s *dev); FAR struct net_driver_s *dev, bool check_expiry);
/**************************************************************************** /****************************************************************************
* Name: arp_delete * Name: arp_delete
@ -620,7 +620,7 @@ void arp_acd_setup(FAR struct net_driver_s *dev);
# define arp_wait_cancel(n) (0) # define arp_wait_cancel(n) (0)
# define arp_wait(n,t) (0) # define arp_wait(n,t) (0)
# define arp_notify(i) # define arp_notify(i)
# define arp_find(i,e,d) (-ENOSYS) # define arp_find(i,e,d,u) (-ENOSYS)
# define arp_delete(i,d) (-ENOSYS) # define arp_delete(i,d) (-ENOSYS)
# define arp_cleanup(d) # define arp_cleanup(d)
# define arp_update(d,i,m); # define arp_update(d,i,m);

View file

@ -262,7 +262,7 @@ void arp_out(FAR struct net_driver_s *dev)
/* Check if we already have this destination address in the ARP table */ /* Check if we already have this destination address in the ARP table */
ret = arp_find(ipaddr, ethaddr.ether_addr_octet, dev); ret = arp_find(ipaddr, ethaddr.ether_addr_octet, dev, false);
if (ret < 0) if (ret < 0)
{ {
ninfo("ARP request for IP %08lx\n", (unsigned long)ipaddr); ninfo("ARP request for IP %08lx\n", (unsigned long)ipaddr);

View file

@ -314,7 +314,7 @@ int arp_send(in_addr_t ipaddr)
* issue. * issue.
*/ */
ret = arp_find(ipaddr, NULL, dev); ret = arp_find(ipaddr, NULL, dev, true);
if (ret >= 0) if (ret >= 0)
{ {
/* We have it! Break out with success */ /* We have it! Break out with success */

View file

@ -186,6 +186,7 @@ arp_return_old_entry(FAR struct arp_entry_s *e1, FAR struct arp_entry_s *e2)
* Input Parameters: * Input Parameters:
* ipaddr - Refers to an IP address in network order * ipaddr - Refers to an IP address in network order
* dev - Device structure * dev - Device structure
* check_expiry - Expiry check
* *
* Assumptions: * Assumptions:
* The network is locked to assure exclusive access to the ARP table. * The network is locked to assure exclusive access to the ARP table.
@ -194,7 +195,8 @@ arp_return_old_entry(FAR struct arp_entry_s *e1, FAR struct arp_entry_s *e2)
****************************************************************************/ ****************************************************************************/
static FAR struct arp_entry_s *arp_lookup(in_addr_t ipaddr, static FAR struct arp_entry_s *arp_lookup(in_addr_t ipaddr,
FAR struct net_driver_s *dev) FAR struct net_driver_s *dev,
bool check_expiry)
{ {
FAR struct arp_entry_s *tabptr; FAR struct arp_entry_s *tabptr;
int i; int i;
@ -205,10 +207,23 @@ static FAR struct arp_entry_s *arp_lookup(in_addr_t ipaddr,
{ {
tabptr = &g_arptable[i]; tabptr = &g_arptable[i];
if (tabptr->at_dev == dev && if (tabptr->at_dev == dev &&
net_ipv4addr_cmp(ipaddr, tabptr->at_ipaddr) && net_ipv4addr_cmp(ipaddr, tabptr->at_ipaddr))
clock_systime_ticks() - tabptr->at_time <= ARP_MAXAGE_TICK)
{ {
return tabptr; /* Find matching entries */
if (!check_expiry)
{
return tabptr; /* Ignore expiration time */
}
/* Check if it has expired */
if (clock_systime_ticks() - tabptr->at_time <= ARP_MAXAGE_TICK)
{
return tabptr;
}
return NULL; /* Expired */
} }
} }
@ -401,6 +416,7 @@ void arp_hdr_update(FAR struct net_driver_s *dev, FAR uint16_t *pipaddr,
* used simply to determine if the Ethernet MAC address is * used simply to determine if the Ethernet MAC address is
* available. * available.
* dev - Device structure * dev - Device structure
* check_expiry - Expiry check
* *
* Assumptions * Assumptions
* The network is locked to assure exclusive access to the ARP table. * The network is locked to assure exclusive access to the ARP table.
@ -408,14 +424,14 @@ void arp_hdr_update(FAR struct net_driver_s *dev, FAR uint16_t *pipaddr,
****************************************************************************/ ****************************************************************************/
int arp_find(in_addr_t ipaddr, FAR uint8_t *ethaddr, int arp_find(in_addr_t ipaddr, FAR uint8_t *ethaddr,
FAR struct net_driver_s *dev) FAR struct net_driver_s *dev, bool check_expiry)
{ {
FAR struct arp_entry_s *tabptr; FAR struct arp_entry_s *tabptr;
struct arp_table_info_s info; struct arp_table_info_s info;
/* Check if the IPv4 address is already in the ARP table. */ /* Check if the IPv4 address is already in the ARP table. */
tabptr = arp_lookup(ipaddr, dev); tabptr = arp_lookup(ipaddr, dev, check_expiry);
if (tabptr != NULL) if (tabptr != NULL)
{ {
/* Addresses that have failed to be searched will return a special /* Addresses that have failed to be searched will return a special
@ -485,7 +501,7 @@ int arp_delete(in_addr_t ipaddr, FAR struct net_driver_s *dev)
#endif #endif
/* Check if the IPv4 address is in the ARP table. */ /* Check if the IPv4 address is in the ARP table. */
tabptr = arp_lookup(ipaddr, dev); tabptr = arp_lookup(ipaddr, dev, false);
if (tabptr != NULL) if (tabptr != NULL)
{ {
/* Notify to netlink */ /* Notify to netlink */

View file

@ -128,7 +128,8 @@ netdev_prefixlen_findby_lipv4addr(in_addr_t lipaddr, FAR int8_t *prefixlen)
if (len > bestpref if (len > bestpref
#ifdef CONFIG_NET_ARP #ifdef CONFIG_NET_ARP
|| (len == bestpref && arp_find(lipaddr, NULL, dev) == OK) || (len == bestpref
&& arp_find(lipaddr, NULL, dev, true) == OK)
#endif #endif
) )
{ {

View file

@ -1452,7 +1452,7 @@ static int netdev_arp_ioctl(FAR struct socket *psock, int cmd,
req->arp_pa.sa_family == AF_INET) req->arp_pa.sa_family == AF_INET)
{ {
ret = arp_find(addr->sin_addr.s_addr, ret = arp_find(addr->sin_addr.s_addr,
(FAR uint8_t *)req->arp_ha.sa_data, dev); (FAR uint8_t *)req->arp_ha.sa_data, dev, true);
if (ret >= 0) if (ret >= 0)
{ {
/* Return the mapped hardware address. */ /* Return the mapped hardware address. */