This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 51743c55b22cb338988bf232409788ee3bc2af11
Author: zhanghongyu <[email protected]>
AuthorDate: Mon Jul 28 21:01:52 2025 +0800

    net/arp/arp_send.c: replace net_lock with netdev_lock
    
    use netdev_lock to protect the arp process
    
    Signed-off-by: zhanghongyu <[email protected]>
---
 net/arp/arp_send.c | 43 +++++++++++++++++++++++++++----------------
 1 file changed, 27 insertions(+), 16 deletions(-)

diff --git a/net/arp/arp_send.c b/net/arp/arp_send.c
index 5643583f8f9..b8081ddc893 100644
--- a/net/arp/arp_send.c
+++ b/net/arp/arp_send.c
@@ -219,8 +219,7 @@ int arp_send(in_addr_t ipaddr)
   if (!dev)
     {
       nerr("ERROR: Unreachable: %08lx\n", (unsigned long)ipaddr);
-      ret = -EHOSTUNREACH;
-      goto errout;
+      return -EHOSTUNREACH;
     }
 
   /* ARP support is only built if the Ethernet link layer is supported.
@@ -284,19 +283,11 @@ int arp_send(in_addr_t ipaddr)
    * want anything to happen until we are ready.
    */
 
-  net_lock();
-  state.snd_cb = arp_callback_alloc(dev);
-  if (!state.snd_cb)
-    {
-      nerr("ERROR: Failed to allocate a callback\n");
-      ret = -ENOMEM;
-      goto errout_with_lock;
-    }
-
   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 */
 
   /* Remember the routing device name */
 
@@ -316,11 +307,13 @@ int arp_send(in_addr_t ipaddr)
        * issue.
        */
 
+      netdev_lock(dev);
       ret = arp_find(ipaddr, NULL, dev, true);
       if (ret >= 0)
         {
           /* We have it!  Break out with success */
 
+          netdev_unlock(dev);
           goto out;
         }
       else if (ret == -ENETUNREACH)
@@ -329,6 +322,7 @@ int arp_send(in_addr_t ipaddr)
            * to try to update the ARP table.
            */
 
+          netdev_unlock(dev);
           arp_send_async(ipaddr, NULL);
           goto out;
         }
@@ -337,6 +331,23 @@ int arp_send(in_addr_t ipaddr)
 
       arp_wait_setup(ipaddr, &notify);
 
+      /* Allocate resources to receive a callback.  This and the following
+       * initialization is performed with the network lock because we don't
+       * want anything to happen until we are ready.
+       */
+
+      if (state.snd_cb == NULL)
+        {
+          state.snd_cb = arp_callback_alloc(dev);
+          if (!state.snd_cb)
+            {
+              nerr("ERROR: Failed to allocate a callback\n");
+              netdev_unlock(dev);
+              ret = -ENOMEM;
+              goto out;
+            }
+        }
+
       /* Arm/re-arm the callback */
 
       state.snd_sent      = false;
@@ -346,6 +357,8 @@ int arp_send(in_addr_t ipaddr)
       state.snd_cb->event = arp_send_eventhandler;
       state.finish_cb     = NULL;
 
+      netdev_unlock(dev);
+
       /* Notify the device driver that new TX data is available. */
 
       netdev_txnotify_dev(dev);
@@ -413,9 +426,7 @@ timeout:
 out:
   nxsem_destroy(&state.snd_sem);
   arp_callback_free(dev, state.snd_cb);
-errout_with_lock:
-  net_unlock();
-errout:
+
   return ret;
 }
 
@@ -463,7 +474,7 @@ int arp_send_async(in_addr_t ipaddr, arp_send_finish_cb_t 
cb)
       goto errout;
     }
 
-  net_lock();
+  netdev_lock(dev);
   state->snd_cb = arp_callback_alloc(dev);
   if (!state->snd_cb)
     {
@@ -492,7 +503,7 @@ int arp_send_async(in_addr_t ipaddr, arp_send_finish_cb_t 
cb)
   netdev_txnotify_dev(dev);
 
 errout_with_lock:
-  net_unlock();
+  netdev_unlock(dev);
 errout:
   return ret;
 }

Reply via email to