Sarthak-Pandey opened a new issue, #22596:
URL: https://github.com/apache/nuttx/issues/22596

   ### Description / Steps to reproduce the issue
   
   ### Description of the Issue
   
   There are two major issues in the network stack's TX path during L2 address 
resolution (`arp_send()` and `icmpv6_neighbor()`):
   
   1. **Non-blocking sockets block synchronously on address resolution (POSIX 
violation):**
      - In `net/tcp/tcp_send_buffered.c`, `net/tcp/tcp_send_unbuffered.c`, 
`net/udp/udp_sendto_buffered.c`, and `net/udp/udp_sendto_unbuffered.c`, 
`arp_send()` (or `icmpv6_neighbor()`) is invoked synchronously *before* 
checking whether the socket is non-blocking (`_SS_ISNONBLOCK` or 
`MSG_DONTWAIT`).
      - If the destination IP is not already in the ARP/Neighbor table (e.g., 
cold cache, peer rebooted, or entry expired), `arp_send()` enters a synchronous 
wait loop (`conn_dev_sem_timedwait()`) for `CONFIG_ARP_SEND_MAXTRIES * 
CONFIG_ARP_SEND_DELAYMSEC` (default 5 * 20ms = 100ms+), and `icmpv6_neighbor()` 
blocks for multiple retry intervals.
      - Furthermore, in `net/inet/inet_sockif.c` (lines 1747 and 1755), 
`inet_send()` for `SOCK_DGRAM` hardcodes `0` instead of forwarding `flags` to 
`psock_udp_sendto()`:
        ```c
        ret = _SS_ISCONNECTED(conn->s_flags) ?
          psock_udp_sendto(psock, buf, len, 0, NULL, 0) : -ENOTCONN;
        ```
        This causes `send(fd, buf, len, MSG_DONTWAIT)` on connected UDP sockets 
to silently drop `MSG_DONTWAIT` and block.
   
   2. **Heavy allocation and locking overhead on TX hot-path even on cache 
hits:**
      - On every single `send()` / `sendto()` packet on a connected TCP or UDP 
socket, `arp_send()` performs:
        - `netdev_findby_ripv4addr()` (acquiring `netdev_list_lock()` and 
routing table lookup).
        - `mempool_allocate(&g_arp_send_infos, UINT_MAX)` + `memset()` + 
`nxsem_init()`.
        - `netdev_lock(dev)`.
        - Only then does it call `arp_find()`. Upon cache hit, it destroys the 
semaphore and frees the allocated memory block back to the mempool.
      - For IPv6, `icmpv6_neighbor()` allocates a devif callback 
(`devif_callback_alloc()`, calling `netdev_verify()` and locking the dev list) 
and initializes a semaphore on every single call before `neighbor_lookup()` is 
checked.
   
   ---
   
   ### Steps to Reproduce
   
   1. Configure NuttX with IPv4/IPv6 networking, ARP, and ICMPv6 neighbor 
support (e.g., `rv-virt:netnsh64` or `sim:nettest`).
   2. Create a UDP or TCP socket, configure it with `O_NONBLOCK` (or pass 
`MSG_DONTWAIT` to `sendto()`/`send()`).
   3. Send a packet to an un-cached destination IP on the local subnet.
   4. Measure the time spent in `sendto()`:
      - **Expected:** `sendto()` returns immediately with `-1` and `errno = 
EAGAIN` / `EWOULDBLOCK` (in ≤ 10ms), while triggering address resolution 
asynchronously in the background.
      - **Actual on `master`:** `sendto()` blocks for ~122 ms (IPv4) or hangs 
until timeout (IPv6).
   
   ---
   
   ### Real Test Execution Logs
   
   Tested on `rv-virt:netnsh64` with QEMU virtio-net:
   
   #### On `upstream/master` (Failure Log):
   ```text
   nsh> cmocka_net_neighbor
   [    1.868205] [==========] neighbor_tests: Running 3 test(s).
   [    1.875503] [ RUN      ] test_neighbor_nonblock_ipv4
   [    2.001178] IPv4 MSG_DONTWAIT sendto: ret=-1 errno=113 elapsed=121 ms
   [    2.004024] [  ERROR   ] --- ms < NEIGHBOR_MAX_NONBLOCK_MS
   [   LINE   ] --- neighbor/test_neighbor_nonblock.c:302: error: Failure!
   [    2.004679] [  FAILED  ] test_neighbor_nonblock_ipv4
   [    2.005067] [ RUN      ] test_neighbor_connected_cache_inval
   [    2.062111] Connected post-SIOCDARP MSG_DONTWAIT: ret=1 errno=0 elapsed=0 
ms
   [    2.063285] [  ERROR   ] --- 1 != -1
   [   LINE   ] --- neighbor/test_neighbor_nonblock.c:403: error: Failure!
   [    2.063848] [  FAILED  ] test_neighbor_connected_cache_inval
   [    2.064150] [ RUN      ] test_neighbor_nonblock_ipv6
   QEMU: Terminated (timed out after 25s waiting for blocking IPv6 solicitation)
   ```
   
   ---
   
   ### Proposed Solution
   
   1. **Fast-path cache check before resource allocation:**
      - In `arp_send_nonblock()` and `icmpv6_neighbor_nonblock()`, perform 
`arp_find()` / `neighbor_lookup()` under `netdev_lock(dev)` *before* calling 
`mempool_allocate()` or `devif_callback_alloc()`. If the entry is already 
resolved, return `OK` immediately with zero allocations.
   2. **Non-blocking asynchronous resolution:**
      - Pass `bool nonblock` into `arp_send_nonblock()` and 
`icmpv6_neighbor_nonblock()`.
      - When `nonblock == true` and the entry is missing:
        - With `CONFIG_NET_ARP_SEND_QUEUE=y`, queue the packet to `d_arpout` 
and return `OK`.
        - With `CONFIG_NET_ARP_SEND_QUEUE=n`, trigger an asynchronous 
solicitation and return `-EAGAIN`.
   3. **Connection-level generation counter caching (`s_neigh_gen`):**
      - Add a global table generation counter (`g_arp_gen` / `g_neigh_gen`) 
that increments whenever an entry is added, updated, invalidated, or when 
network routes/interfaces change.
      - Cache `s_neigh_gen` on connected sockets (`struct socket_conn_s`). On 
subsequent `send()` calls, skip `arp_resolve()` / `icmpv6_resolve()` completely 
($O(1)$) if the table generation has not changed.
   4. **Fix `inet_send()` flag forwarding:**
      - Forward `flags` instead of hardcoded `0` in `inet_send()`.
   
   #### Verification with Proposed Patch:
   ```text
   nsh> cmocka_net_neighbor
   [    1.127755] [==========] neighbor_tests: Running 3 test(s).
   [    1.134246] [ RUN      ] test_neighbor_nonblock_ipv4
   [    1.151054] IPv4 MSG_DONTWAIT sendto: ret=-1 errno=11 elapsed=8 ms
   [    1.155675] IPv4 O_NONBLOCK connect+send: ret=-1 errno=11 elapsed=0 ms
   [    1.160638] [       OK ] test_neighbor_nonblock_ipv4
   [    1.161159] [ RUN      ] test_neighbor_connected_cache_inval
   [    1.372110] Connected post-SIOCDARP MSG_DONTWAIT: ret=-1 errno=11 
elapsed=0 ms
   [    1.374069] [       OK ] test_neighbor_connected_cache_inval
   [    1.374679] [ RUN      ] test_neighbor_nonblock_ipv6
   [    1.377940] IPv6 MSG_DONTWAIT sendto: ret=-1 errno=11 elapsed=2 ms
   [    1.379051] [       OK ] test_neighbor_nonblock_ipv6
   [    1.379669] [==========] neighbor_tests: 3 test(s) run.
   [    1.379997] [  PASSED  ] 3 test(s).
   ```
   
   ### On which OS does this issue occur?
   
   [OS: Windows]
   
   ### What is the version of your OS?
   
   Windows 11
   
   ### NuttX Version
   
   master
   
   ### Issue Architecture
   
   [Arch: all]
   
   ### Issue Area
   
   [Area: Networking]
   
   ### Host information
   
   _No response_
   
   ### Verification
   
   - [x] I have verified before submitting the report.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to