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

   ### Description / Steps to reproduce the issue
   
   ### Summary
   In NuttX networking, the I/O Buffer (IOB) pool (`CONFIG_IOB_NBUFFERS`) is a 
fixed-size, statically preallocated memory pool shared across all sockets, 
drivers, and network queues. 
   
   There is an architectural flaw in `net/tcp/tcp_recvwindow.c` where each 
active TCP connection calculates its advertised receive window assuming it has 
exclusive access to 100% of the remaining available IOBs in the system:
   
   
https://github.com/apache/nuttx/blob/master/net/tcp/tcp_recvwindow.c#L176-L189
   
   ```c
   /* The optimal TCP window size is the amount of TCP data that we can
    * buffer. This logic here assumes that all IOBs are available for
    * TCP buffering. Assume that all of the available IOBs are can be used
    * for buffering on this connection.
    * But if there are multiple active sockets or other consumers of IOBs
    * then the total number of IOBs will all not be available for read-ahead
    * buffering for this connection.
    */
   recvwndo = tailroom + (niob_avail * CONFIG_IOB_BUFSIZE);
   ```
   
   ### Impact & Failure Scenario
   When two or more sockets are open simultaneously (for example, an HTTP 
client and an MQTT client):
   1. Suppose 20 IOBs are available in the free pool (`niob_avail = 20`).
   2. Socket A calculates `recvwndo = 20 * CONFIG_IOB_BUFSIZE` and advertises 
this full window to remote peer A.
   3. Socket B simultaneously calculates `recvwndo = 20 * CONFIG_IOB_BUFSIZE` 
and advertises the same full window to remote peer B.
   4. Both remote peers burst traffic according to their advertised windows (up 
to 40 IOBs worth of incoming data).
   5. The IOB pool (`g_iob_count`) is completely exhausted.
   6. Once the pool is empty:
      - Subsequent packet allocations fail (`iob_tryalloc()` returns NULL), 
causing immediate packet drops and retransmission storms.
      - Any worker thread or task calling blocking `iob_alloc(true)` (in 
`mm/iob/iob_alloc.c:201` via `nxsem_wait_uninterruptible()`) sleeps 
indefinitely if the thread responsible for consuming data (`recv()`) is blocked 
on a network lock held by that worker.
      - Unrelated critical services (DNS queries, NTP synchronization, 
SocketCAN, etc.) fail due to global IOB starvation.
   
   ### Steps to Reproduce
   1. Configure NuttX with TCP networking enabled and a default IOB pool (e.g., 
`CONFIG_IOB_NBUFFERS=24`, `CONFIG_IOB_BUFSIZE=196`).
   2. Open two concurrent TCP sockets (e.g. run a TCP server while also opening 
an outgoing connection).
   3. Have the application consume incoming data on one socket slightly slower 
than wire speed (simulating CPU-bound task before `recv()`).
   4. Send incoming data to both sockets.
   5. Observe `/proc/iobinfo` or system behavior: `nfree` drops to 0, packet 
loss escalates, and sockets stall.
   
   ### Expected Behavior
   - TCP receive window advertisement in `tcp_recvwindow.c` should account for 
multi-socket contention rather than oversubscribing the shared pool.
   - Slow consumer sockets should gracefully throttle their incoming window 
down to 0 (`TCP Zero Window`) without depleting the global IOB pool for other 
connections.
   - Network worker tasks should never enter an unconditional blocking sleep in 
`iob_alloc()`.
   
   ### Proposed Solutions / Workarounds
   1. **Multi-Socket Window Fair-Share**: In `tcp_get_recvwindow()`, divide 
available IOBs proportionally or by active connection count (`conn_count`), or 
cap each connection's maximum read-ahead IOB quota.
   2. **IOB Throttling**: Configure `CONFIG_IOB_THROTTLE=8` to reserve an 
emergency margin for critical system packets.
   3. **Non-blocking Workers**: Ensure driver and stack worker allocation paths 
use `iob_tryalloc()` or timed allocation instead of blocking indefinitely on 
`iob_alloc()`.
   
   ### 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: Memory Management]
   
   ### 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