Sarthak-Pandey opened a new pull request, #20627:
URL: https://github.com/apache/nuttx/pull/20627

   ## Summary
   This PR implements complete TCP urgent (Out-of-Band / OOB) data support for 
the Apache NuttX networking stack under `CONFIG_NET_TCPURGDATA`, resolving 
long-standing issue #2327 and addressing critical design review bottlenecks 
across RX, TX, and socket API layers.
   
   When `CONFIG_NET_TCPURGDATA` is disabled (`n`), the build output and memory 
footprint remain 100% unchanged.
   
   ---
   
   ## Key Changes & Architecture
   
   ### 1. Data Structures & Semantics
   - **Standard Semantics:** Adopts BSD/Linux standard semantics 
(`tcp_stdurg=0`), where the urgent pointer points one byte past the urgent 
byte, and the single OOB byte is `payload[urgp - 1]`.
   - **SO_OOBINLINE:** When `SO_OOBINLINE` is enabled, urgent data remains 
in-line in the normal byte stream without being extracted.
   - **Bounded Buffering:** Implemented BSD single-byte in-place overwrite 
policy in `conn->urgreadahead` to prevent IOB pool exhaustion on high-frequency 
URG floods.
   
   ### 2. RX Path (`net/tcp/tcp_input.c`)
   - Extracted the single urgent byte into `conn->urgreadahead` and compacted 
remaining payload in-place using `memmove()` without distorting stream sequence 
numbers.
   - **Allocation Failure Safety:** If IOB allocation fails, the packet is 
dropped without advancing `rcvseq`, forcing the peer TCP stack to retransmit 
(avoiding silent data loss).
   - **Pure-Urgent ACKs:** Added `TCP_SNDACK` trigger when urgent data is 
consumed so that pure-urgent single-byte segments (`d_len == 0`) receive an 
immediate ACK.
   
   ### 3. Socket API & Polling (`net/tcp/tcp_recvfrom.c`, 
`net/tcp/tcp_netpoll.c`, `net/devif/devif.h`)
   - Implemented `tcp_recv_oob()` for `recv(..., MSG_OOB)`.
   - **Safe Waiter Lifecycle:** Replaced fragile connection-level semaphore 
(`urg_sem`) with the standard `devif_callback` pattern (`TCP_URGDATA | 
TCP_DISCONN_EVENTS | TCP_RXCLOSE`). This eliminates use-after-free crashes on 
`close()`, hangs on remote RST / abort / timeout, and stale semaphore counts.
   - **Reliable `POLLPRI` Wakeup:** Defined conflict-free event flag 
`TCP_URGDATA (1 << 25)` in `devif.h` to immediately awaken threads blocked in 
`poll(POLLPRI)` even when payload length drops to 0.
   
   ### 4. TX Path (`net/tcp/tcp_send.c`, `net/tcp/tcp_send_buffered.c`, 
`net/tcp/tcp_send_unbuffered.c`)
   - Removed inbound URG flag inheritance in `tcp_send()` so that plain ACKs 
and replies never leak incoming `TCP_URG` or stale `urgp`.
   - In-order buffer queueing via `sq_addlast` prevents wire reordering of 
prior normal data.
   - Per-segment urgent pointer calculation: `conn->urg_offset = (wrb->wb_urg - 
TCP_WBSENT(wrb))`.
   
   ---
   
   ## ❓ Questions for Maintainers / Reviewers
   
   Before merging, I would love feedback from the maintainers on the following 
points:
   
   1. **Urgent Buffering Policy:**
      - Currently, we follow the BSD/Linux default policy of keeping at most 
**1 unread urgent byte** per connection (new urgent byte overwrites the 
previous unread byte in-place, consuming 0 additional IOBs).
      - *Question for maintainers:* Is this single-byte overwrite policy 
preferred, or would you like me to introduce an optional Kconfig knob (e.g. 
`CONFIG_NET_TCP_URG_BUFCAP`, defaulting to 1 but allowing a FIFO queue of up to 
N bytes before trimming the oldest)?
   
   2. **SO_OOBINLINE & `sockatmark()`:**
      - With `SO_OOBINLINE` active, urgent bytes stay inline in the normal read 
stream. Should we also introduce `sockatmark()` support in this PR, or track it 
as a subsequent follow-up?
   
   3. **Nagle Bypass on Urgent Buffers:**
      - In `tcp_send_buffered.c`, urgent buffers are queued in-order. Would 
maintainers prefer an explicit Nagle bypass (forcing immediate segment flush 
with `TCP_PSH`) when `wb_urg > 0` is present?
   
   ---
   
   ## Verification
   - Clean run on `tools/nxstyle` across all modified files.
   - Build verified with:
     - `CONFIG_NET_TCPURGDATA=n` (verified zero diff in binary/data layout)
     - `CONFIG_NET_TCPURGDATA=y` with `CONFIG_NET_TCP_WRITE_BUFFERS=y`
     - `CONFIG_NET_TCPURGDATA=y` with `CONFIG_NET_TCP_WRITE_BUFFERS=n`
   - Verified no remaining `tcp->flags & TCP_URG` reads from inbound headers.
   - Verified no remaining `sq_addfirst` for urgent buffers.
   


-- 
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