This is an automated email from the ASF dual-hosted git repository. GUIDINGLI pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 6a6b5fb940ed0c90c08bfcaa45c34cf74ebff18e Author: zhanghongyu <[email protected]> AuthorDate: Wed Nov 18 16:29:39 2026 +0800 net/arp: Allocate send state from a mempool and lock across retries. arp_send() kept its send and notify state on the caller's stack, and arp_send_async() allocated its state with kmm_zalloc(). The retry loop also took and dropped netdev_lock on every pass. Allocate the state from a pool and hold the device lock for the whole retry loop: - add struct arp_send_info_s (state + notify), allocated from a MEMPOOL_DEFINE pool sized by the new NET_ARP_PREALLOC_STATES, NET_ARP_ALLOC_STATES and NET_ARP_MAX_STATES options; - take netdev_lock() once before the loop and release it once after; - pass the device to arp_wait() and wait with conn_dev_sem_timedwait(), which releases the device lock while blocked so that the receive path can deliver the reply. The returned error codes do not change. Signed-off-by: zhanghongyu <[email protected]> --- net/arp/Kconfig | 40 +++++++++++++++++ net/arp/arp.h | 11 ++--- net/arp/arp_notify.c | 10 +++-- net/arp/arp_send.c | 124 +++++++++++++++++++++++++++++++++------------------ 4 files changed, 132 insertions(+), 53 deletions(-) diff --git a/net/arp/Kconfig b/net/arp/Kconfig index 425c3584aa0..7fb6e3724da 100644 --- a/net/arp/Kconfig +++ b/net/arp/Kconfig @@ -113,6 +113,46 @@ config ARP_SEND_DELAYMSEC on the network since it is basically the time from when an ARP request is sent until the response is received. +config NET_ARP_PREALLOC_STATES + int "Preallocated ARP send states" + default 2 + ---help--- + Number of ARP send states (all tasks). + + This number of connections will be pre-allocated during system boot. + If dynamic connections allocation is enabled, more connections may + be allocated at a later time, as the system needs them. Else this + will be the maximum number of connections available to the system + at all times. + + Set to 0 to disable (and rely only on dynamic allocations). + +config NET_ARP_ALLOC_STATES + int "Dynamic ARP send states allocation" + default 1 + ---help--- + Dynamic memory allocations for ARP send states. + + When set to 0 all dynamic allocations are disabled. + + When set to 1 a new connection will be allocated every time, + and it will be free'd when no longer needed. + + Setting this to 2 or more will allocate the connections in + batches (with batch size equal to this config). When a + connection is no longer needed, it will be returned to the + free connections pool, and it will never be deallocated! + +config NET_ARP_MAX_STATES + int "Maximum number of ARP/IP connections" + default 0 + ---help--- + If dynamic connections allocation is selected (NET_ARP_ALLOC_STATES > 0) + this will limit the number of connections that can be allocated. + + This is useful in case the system is under very heavy load (or + under attack), ensuring that the heap will not be exhausted. + endif # NET_ARP_SEND config NET_ARP_DUMP diff --git a/net/arp/arp.h b/net/arp/arp.h index aa0b9ab6a2f..5e43b39dd41 100644 --- a/net/arp/arp.h +++ b/net/arp/arp.h @@ -380,15 +380,16 @@ int arp_wait_cancel(FAR struct arp_notify_s *notify); * timeout occurs. * * Assumptions: - * This function is called from ARP send and must execute with the network - * un-locked. + * This function is called from ARP send with the device locked. The + * lock is released while waiting. * ****************************************************************************/ #ifdef CONFIG_NET_ARP_SEND -int arp_wait(FAR struct arp_notify_s *notify, unsigned int timeout); +int arp_wait(FAR struct net_driver_s *dev, FAR struct arp_notify_s *notify, + unsigned int timeout); #else -# define arp_wait(n,t) (0) +# define arp_wait(d,n,t) (0) #endif /**************************************************************************** @@ -655,7 +656,7 @@ int arp_queue_iob(FAR struct net_driver_s *dev, in_addr_t ipaddr, # define arp_poll(d,c) (0) # define arp_wait_setup(i,n) # define arp_wait_cancel(n) (0) -# define arp_wait(n,t) (0) +# define arp_wait(d,n,t) (0) # define arp_notify(i) # define arp_find(i,e,d) (-ENOSYS) # define arp_delete(i,d) (-ENOSYS) diff --git a/net/arp/arp_notify.c b/net/arp/arp_notify.c index 08567e19ad0..17faab1ce4d 100644 --- a/net/arp/arp_notify.c +++ b/net/arp/arp_notify.c @@ -35,6 +35,7 @@ #include <nuttx/spinlock.h> #include <nuttx/net/net.h> +#include "utils/utils.h" #include "arp/arp.h" #ifdef CONFIG_NET_ARP_SEND @@ -145,18 +146,19 @@ int arp_wait_cancel(FAR struct arp_notify_s *notify) * timeout occurs. * * Assumptions: - * This function is called from ARP send must execute with the network - * locked. + * This function is called from ARP send with the device locked. The + * lock is released while waiting. * ****************************************************************************/ -int arp_wait(FAR struct arp_notify_s *notify, unsigned int timeout) +int arp_wait(FAR struct net_driver_s *dev, FAR struct arp_notify_s *notify, + unsigned int timeout) { int ret; /* And wait for the ARP response (or a timeout). */ - nxsem_tickwait_uninterruptible(¬ify->nt_sem, MSEC2TICK(timeout)); + conn_dev_sem_timedwait(¬ify->nt_sem, false, timeout, NULL, dev); /* Then get the real result of the transfer */ diff --git a/net/arp/arp_send.c b/net/arp/arp_send.c index b776385448c..98ef9f58789 100644 --- a/net/arp/arp_send.c +++ b/net/arp/arp_send.c @@ -28,12 +28,15 @@ #include <unistd.h> #include <string.h> +#include <inttypes.h> #include <nuttx/debug.h> #include <netinet/in.h> #include <net/if.h> #include <nuttx/kmalloc.h> +#include <nuttx/mutex.h> +#include <nuttx/mm/mempool.h> #include <nuttx/net/net.h> #include <nuttx/net/netdev.h> #include <nuttx/net/ip.h> @@ -41,10 +44,34 @@ #include "netdev/netdev.h" #include "devif/devif.h" #include "route/route.h" +#include "utils/utils.h" #include "arp/arp.h" #ifdef CONFIG_NET_ARP_SEND +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct arp_send_info_s +{ + struct arp_send_s state; /* ARP send state, keep first */ + struct arp_notify_s notify; /* ARP notify structure */ +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* This is the pool of pre-allocated ARP send states. The pool is used to + * hold the state of an ARP request until the request is completed. + * Avoid accessing stack memory across threads. + */ + +MEMPOOL_DEFINE(g_arp_send_infos, sizeof(struct arp_send_info_s), + CONFIG_NET_ARP_PREALLOC_STATES, CONFIG_NET_ARP_MAX_STATES, + CONFIG_NET_ARP_ALLOC_STATES); + /**************************************************************************** * Private Functions ****************************************************************************/ @@ -73,7 +100,7 @@ static void arp_send_terminate(FAR struct net_driver_s *dev, nxsem_destroy(&state->snd_sem); arp_callback_free(dev, state->snd_cb); state->finish_cb(dev, result); - kmm_free(state); + mempool_release(&g_arp_send_infos, state); } } @@ -170,7 +197,7 @@ static uint32_t arp_send_eventhandler(FAR struct net_driver_s *dev, * -ENETUNREACH: No device or router reaches the address, or the * device went down * -EHOSTUNREACH: The address resolution failed - * -ENOMEM: No callback available + * -ENOMEM: No callback or send state available * * Assumptions: * This function is called from the normal tasking context. @@ -180,8 +207,9 @@ static uint32_t arp_send_eventhandler(FAR struct net_driver_s *dev, int arp_send(in_addr_t ipaddr) { FAR struct net_driver_s *dev; - struct arp_notify_s notify; - struct arp_send_s state; + FAR struct arp_send_info_s *info; + FAR struct arp_send_s *state; + FAR struct arp_notify_s *notify; bool sending = false; int ret; @@ -286,15 +314,26 @@ int arp_send(in_addr_t ipaddr) * want anything to happen until we are ready. */ - nxsem_init(&state.snd_sem, 0, 0); /* Doesn't really fail */ + info = mempool_allocate(&g_arp_send_infos, UINT_MAX); + if (info == NULL) + { + nerr("ERROR: Failed to allocate ARP send info\n"); + return -ENOMEM; + } + + memset(info, 0, sizeof(*info)); + state = &info->state; + notify = &info->notify; + + nxsem_init(&state->snd_sem, 0, 0); /* Doesn't really fail */ - state.snd_retries = 0; /* No retries yet */ - state.snd_ipaddr = ipaddr; /* IP address to query */ - state.snd_cb = NULL; /* No callback allocated yet */ + state->snd_retries = 0; /* No retries yet */ + state->snd_ipaddr = ipaddr; /* IP address to query */ + state->snd_cb = NULL; /* No callback allocated yet */ /* Remember the routing device name */ - strlcpy((FAR char *)state.snd_ifname, (FAR const char *)dev->d_ifname, + strlcpy((FAR char *)state->snd_ifname, (FAR const char *)dev->d_ifname, IFNAMSIZ); /* Now loop, testing if the address mapping is in the ARP table and re- @@ -303,7 +342,8 @@ int arp_send(in_addr_t ipaddr) ret = -ETIMEDOUT; /* Assume a timeout failure */ - while (state.snd_retries < CONFIG_ARP_SEND_MAXTRIES) + netdev_lock(dev); + while (state->snd_retries < CONFIG_ARP_SEND_MAXTRIES) { /* Check if the address mapping is present in the ARP table. This * is only really meaningful on the first time through the loop. @@ -312,13 +352,11 @@ int arp_send(in_addr_t ipaddr) * issue. */ - netdev_lock(dev); ret = arp_find(ipaddr, NULL, dev); if (ret >= 0) { /* We have it! Break out with ret value */ - netdev_unlock(dev); break; } @@ -326,26 +364,20 @@ int arp_send(in_addr_t ipaddr) { /* The resolution of the address failed a short time ago */ - netdev_unlock(dev); ret = -EHOSTUNREACH; break; } /* Set up the ARP response wait BEFORE we send the ARP request */ - arp_wait_setup(ipaddr, ¬ify); + arp_wait_setup(ipaddr, notify); if (ret == -EINPROGRESS && !sending) { /* ARP request for the same destination is in progress, directly * wait arp response notify. - * - * Drop the device lock: this path skips the netdev_unlock() - * below, and the receive path needs that same lock to deliver - * the reply. The waiter above is already installed. */ - netdev_unlock(dev); goto wait; } @@ -354,13 +386,12 @@ int arp_send(in_addr_t ipaddr) * want anything to happen until we are ready. */ - if (state.snd_cb == NULL) + if (state->snd_cb == NULL) { - state.snd_cb = arp_callback_alloc(dev); - if (!state.snd_cb) + state->snd_cb = arp_callback_alloc(dev); + if (!state->snd_cb) { nerr("ERROR: Failed to allocate a callback\n"); - netdev_unlock(dev); ret = -ENOMEM; break; } @@ -368,18 +399,18 @@ int arp_send(in_addr_t ipaddr) /* Arm/re-arm the callback */ - state.snd_sent = false; - state.snd_result = -EBUSY; - state.snd_cb->flags = (ARP_POLL | NETDEV_DOWN); - state.snd_cb->priv = (FAR void *)&state; - state.snd_cb->event = arp_send_eventhandler; - state.finish_cb = NULL; + state->snd_sent = false; + state->snd_result = -EBUSY; + state->snd_cb->flags = (ARP_POLL | NETDEV_DOWN); + state->snd_cb->priv = (FAR void *)state; + state->snd_cb->event = arp_send_eventhandler; + state->finish_cb = NULL; /* MAC address marked with all zeros to limit concurrent task * send ARP request for same destination. */ - if (state.snd_retries == 0) + if (state->snd_retries == 0) { arp_update(dev, ipaddr, NULL, 0); sending = true; @@ -393,36 +424,34 @@ int arp_send(in_addr_t ipaddr) * nxsem_tickwait will also terminate if a signal is received. */ - netdev_unlock(dev); - do { - ret = nxsem_tickwait(&state.snd_sem, - MSEC2TICK(CONFIG_ARP_SEND_DELAYMSEC)); + ret = conn_dev_sem_timedwait(&state->snd_sem, true, + CONFIG_ARP_SEND_DELAYMSEC, NULL, dev); if (ret == -ETIMEDOUT) { - arp_wait_cancel(¬ify); + arp_wait_cancel(notify); goto timeout; } } - while (!state.snd_sent); + while (!state->snd_sent); /* Check the result of the send operation */ - ret = state.snd_result; + ret = state->snd_result; if (ret < 0) { /* Break out on a send failure */ nerr("ERROR: Send failed: %d\n", ret); - arp_wait_cancel(¬ify); + arp_wait_cancel(notify); break; } /* Now wait for response to the ARP response to be received. */ wait: - ret = arp_wait(¬ify, CONFIG_ARP_SEND_DELAYMSEC); + ret = arp_wait(dev, notify, CONFIG_ARP_SEND_DELAYMSEC); /* arp_wait will return OK if and only if the matching ARP response * is received. Otherwise, it will return -ETIMEDOUT. @@ -439,12 +468,15 @@ timeout: /* Increment the retry count */ - state.snd_retries++; + state->snd_retries++; nerr("ERROR: arp_wait failed: %d, ipaddr: %u.%u.%u.%u\n", ret, ip4_addr1(ipaddr), ip4_addr2(ipaddr), ip4_addr3(ipaddr), ip4_addr4(ipaddr)); } + nxsem_destroy(&state->snd_sem); + arp_callback_free(dev, state->snd_cb); + /* No reply to any of the requests: the address resolution failed */ if (ret == -ETIMEDOUT) @@ -452,8 +484,8 @@ timeout: ret = -EHOSTUNREACH; } - nxsem_destroy(&state.snd_sem); - arp_callback_free(dev, state.snd_cb); + netdev_unlock(dev); + mempool_release(&g_arp_send_infos, info); return ret; } @@ -483,16 +515,20 @@ timeout: int arp_send_async(in_addr_t ipaddr, arp_send_finish_cb_t cb) { FAR struct net_driver_s *dev; - FAR struct arp_send_s *state = kmm_zalloc(sizeof(struct arp_send_s)); + FAR struct arp_send_info_s *info; + FAR struct arp_send_s *state; int ret = 0; - if (!state) + info = mempool_allocate(&g_arp_send_infos, UINT_MAX); + if (!info) { nerr("ERROR: Out of memory\n"); ret = -ENOMEM; goto errout; } + memset(info, 0, sizeof(*info)); + state = &info->state; dev = netdev_findby_ripv4addr(INADDR_ANY, ipaddr); if (!dev) {
