casaroli opened a new pull request, #21960:
URL: https://github.com/apache/nuttx/pull/21960

   ## Summary
   
   `tcp_sendfile()` is the `sendfile()` path for a TCP socket with 
`CONFIG_NET_SENDFILE`. With `SO_SNDTIMEO` set, it waits for progress with that 
timeout. After a wait with no new acknowledgment it returned -1 with `EAGAIN`, 
also when some bytes went out in an earlier wait. A signal had the same effect 
with `EINTR`. The caller does not know that these bytes went out and sends them 
again.
   
   Also `*offset` was the file position after the last file read, not the 
offset of the caller plus the bytes sent. A retransmission reads the file again 
at an earlier position, so the result is wrong after a retransmission. On an 
error, `*offset` became the file position, also when the caller gave a 
different offset. Without an offset, the file position stayed after the last 
file read.
   
   When the wait ends with a timeout or a signal after some bytes went out, 
`tcp_sendfile()` now returns the count, as `write()` does. `*offset` moves by 
the count. Without an offset, the file position moves to the byte after the 
last byte sent. This is the same rule as the copy path of `sendfile()` 
(`fs/vfs/fs_sendfile.c`).
   
   ## Impact
   
   Only `CONFIG_NET_SENDFILE` builds change. A call that sends all bytes, or no 
byte, returns the same result as before. A call with `SO_SNDTIMEO` that times 
out after some bytes went out now returns the count, not -1 with `EAGAIN`. 
Headers and options do not change. The code is common network code, the same in 
flat, protected and kernel builds.
   
   In-tree callers: `netutils/ftpc`, `netutils/netcat` and `netutils/webserver` 
in nuttx-apps do not set `SO_SNDTIMEO`, so the timeout case does not occur for 
them. `ftpc` and `netcat` continue from `*offset` and subtract the count; they 
get a correct `*offset` also after a retransmission.
   
   ## Testing
   
   A test program (`tcpsfc`) sends a 1 MiB file with `sendfile()` over TCP on 
127.0.0.1 to a reader thread that starts after 1.5 s, with `SO_SNDTIMEO` of 200 
ms: the first half with an offset, the second half without. It checks after 
each call that `*offset` (or the file position) moved by the returned count, 
that -1 with `EAGAIN` left them unchanged, the sum of the counts, and every 
byte at the reader. A second run without the delay is the control.
   
   | Configuration | master e9eefaa0e9a | this PR |
   |---|---|---|
   | `qemu-armv8a:nsh` (flat, arm64, QEMU TCG), `CONFIG_NET_SENDFILE`, 
`CONFIG_NET_TCP_WRITE_BUFFERS` | FAIL: call 1 returns -1 with `EAGAIN` and 
`*offset` moved by 16258; the reader gets 1064834 bytes instead of 1048576 | 
PASS: call 1 returns a partial count; 1048576 bytes |
   | same, control without the delay | PASS | PASS |
   | `qemu-armv8a:knsh` (kernel, arm64, QEMU TCG), same options | FAIL: the 
same as above | PASS |
   | same, 4 MiB to a forked child and to a thread with `recv()` (test 
`tcploop`) | PASS | PASS |
   
   Without `CONFIG_NET_TCP_WRITE_BUFFERS`, the same test also shows the fix 
(the partial count 17736 is returned, `*offset` is correct), but a later call 
fails with `ENOMEM` on master and with this PR: `sendfile_eventhandler()` ends 
the call when `devif_file_send()` finds no free IOB for a moment. That is a 
separate problem and not changed here.
   
   ostest: status 0 on `qemu-armv8a:nsh` and `qemu-armv8a:knsh` (TCG) on master 
and with this PR. Builds: `sim:tcpblaster` (`CONFIG_NET_SENDFILE`, x86_64), 
`qemu-armv8a:nsh` with `CONFIG_NET_SENDFILE` with and without 
`CONFIG_NET_TCP_WRITE_BUFFERS`, `qemu-armv8a:knsh` with `CONFIG_NET_SENDFILE`; 
all in the NuttX CI image (GCC 13.2), no new warnings. Make only; no build file 
changed. Not run: a protected build, real hardware, a retransmission (loopback 
does not lose segments). `tools/checkpatch.sh -c -u -m -g` passes. No 
Documentation page describes `CONFIG_NET_SENDFILE` behaviour.
   


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