Netdev List
 help / color / mirror / Atom feed
From: Ren Wei <weir@nebusec.ai>
To: virtualization@lists.linux.dev, netdev@vger.kernel.org
Cc: sgarzare@redhat.com, bobbyeshleman@meta.com, davem@davemloft.net,
	edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com,
	willemb@google.com, horms@kernel.org,
	arseny.krasnov@kaspersky.com, vega@nebusec.ai,
	caoruide123@gmail.com, weir@nebusec.ai
Subject: [PATCH net 2/2] vsock/test: add stream MSG_WAITALL receive timeout test
Date: Fri, 27 Nov 2026 13:19:14 +0800	[thread overview]
Message-ID: <503c1d5538f5b41f8eda756f5ea4491f0bd44518.1795591531.git.caoruide123@gmail.com> (raw)
In-Reply-To: <cover.1795587333.git.caoruide123@gmail.com>

From: Ruide Cao <caoruide123@gmail.com>

test_seqpacket_timeout covers SOCK_SEQPACKET with SO_RCVTIMEO, but there
is no SOCK_STREAM test for a cumulative receive timeout across partial
reads.

Have the peer drip-feed one byte every 0.8s while the client issues
recv(MSG_WAITALL) with a 1s SO_RCVTIMEO. Without the timeout being
preserved, the wait budget is restarted after each byte and the recv
completes after several intervals. With the fix, the call must return
around the original timeout, either with EAGAIN or with a partial read
shorter than the MSG_WAITALL request.

Keep the data socket open until both sides confirm: the client writes
WAITDONE after recv returns, the server finishes sending all six bytes
and acknowledges, and only then is the socket closed. Failure paths
complete the same handshake so the server does not hang.

Signed-off-by: Ruide Cao <caoruide123@gmail.com>
Signed-off-by: Ren Wei <weir@nebusec.ai>
---
 tools/testing/vsock/vsock_test.c | 112 +++++++++++++++++++++++++++++++
 1 file changed, 112 insertions(+)

diff --git a/tools/testing/vsock/vsock_test.c b/tools/testing/vsock/vsock_test.c
index 10b9074fb87f..868792f7729c 100644
--- a/tools/testing/vsock/vsock_test.c
+++ b/tools/testing/vsock/vsock_test.c
@@ -662,6 +662,113 @@ static void test_seqpacket_timeout_server(const struct test_opts *opts)
 	close(fd);
 }
 
+#define STREAM_RCVTIMEO_DRIP_BYTES	6
+#define STREAM_RCVTIMEO_DRIP_USEC	800000 /* 0.8s, inside the 1s timeout */
+
+/* Keep the data socket open until both sides confirm. The client
+ * writes WAITDONE when recv() returns, but must not close until the
+ * server has finished drip-feeding all 6 bytes and acknowledged;
+ * otherwise later sends hit EPIPE. Failure paths complete the same
+ * handshake so the server is not left in control_expectln("WAITDONE").
+ */
+static void test_stream_timeout_waitall_done(int fd)
+{
+	control_writeln("WAITDONE");
+	control_expectln("SRVDONE");
+	close(fd);
+}
+
+static void test_stream_timeout_waitall_client(const struct test_opts *opts)
+{
+	char buf[STREAM_RCVTIMEO_DRIP_BYTES];
+	time_t read_enter_ns;
+	struct timeval tv;
+	time_t read_ns;
+	int failed = 0;
+	ssize_t n;
+	int fd;
+
+	fd = vsock_stream_connect(opts->peer_cid, opts->peer_port);
+	if (fd < 0) {
+		perror("connect");
+		exit(EXIT_FAILURE);
+	}
+
+	tv.tv_sec = RCVTIMEO_TIMEOUT_SEC;
+	tv.tv_usec = 0;
+
+	setsockopt_timeval_check(fd, SOL_SOCKET, SO_RCVTIMEO, tv,
+				 "setsockopt(SO_RCVTIMEO)");
+
+	/* Enter recv() before the peer starts drip-feeding, so the first
+	 * byte cannot already be queued.
+	 */
+	control_writeln("CLNSENT");
+
+	read_enter_ns = current_nsec();
+	n = recv(fd, buf, sizeof(buf), MSG_WAITALL);
+	read_ns = current_nsec() - read_enter_ns;
+
+	/* Unfixed kernels restart the full SO_RCVTIMEO after each partial
+	 * read, so this recv() returns 6 bytes after ~4.8s. A cumulative
+	 * 1s timeout returns much sooner; use 2s as the cap so the two
+	 * outcomes cannot be confused.
+	 */
+	if (read_ns >= 2ULL * NSEC_PER_SEC * RCVTIMEO_TIMEOUT_SEC) {
+		fprintf(stderr,
+			"recv(MSG_WAITALL) exceeded SO_RCVTIMEO, %lu ns\n",
+			read_ns);
+		failed = 1;
+	}
+
+	if (n == sizeof(buf)) {
+		fprintf(stderr,
+			"recv(MSG_WAITALL) completed under drip-feed\n");
+		failed = 1;
+	}
+
+	if (n < 0 && errno != EAGAIN) {
+		perror("EAGAIN expected");
+		failed = 1;
+	}
+
+	if (n == 0) {
+		fprintf(stderr, "unexpected EOF on recv(MSG_WAITALL)\n");
+		failed = 1;
+	}
+
+	test_stream_timeout_waitall_done(fd);
+	if (failed)
+		exit(EXIT_FAILURE);
+}
+
+static void test_stream_timeout_waitall_server(const struct test_opts *opts)
+{
+	int fd;
+	int i;
+
+	fd = vsock_stream_accept(VMADDR_CID_ANY, opts->peer_port, NULL);
+	if (fd < 0) {
+		perror("accept");
+		exit(EXIT_FAILURE);
+	}
+
+	control_expectln("CLNSENT");
+
+	/* Drip-feed all 6 bytes even after the client times out, so an
+	 * unfixed kernel still collects a full 6-byte recv and fails.
+	 * The client keeps the data socket open until SRVDONE.
+	 */
+	for (i = 0; i < STREAM_RCVTIMEO_DRIP_BYTES; i++) {
+		timeout_usleep(STREAM_RCVTIMEO_DRIP_USEC);
+		send_byte(fd, 1, 0);
+	}
+
+	control_expectln("WAITDONE");
+	control_writeln("SRVDONE");
+	close(fd);
+}
+
 static void test_seqpacket_bigmsg_client(const struct test_opts *opts)
 {
 	unsigned long long sock_buf_size;
@@ -2739,6 +2846,11 @@ static struct test_case test_cases[] = {
 		.run_client = test_seqpacket_timeout_client,
 		.run_server = test_seqpacket_timeout_server,
 	},
+	{
+		.name = "SOCK_STREAM MSG_WAITALL timeout",
+		.run_client = test_stream_timeout_waitall_client,
+		.run_server = test_stream_timeout_waitall_server,
+	},
 	{
 		.name = "SOCK_SEQPACKET invalid receive buffer",
 		.run_client = test_seqpacket_invalid_rec_buffer_client,
-- 
2.47.3


      parent reply	other threads:[~2026-11-27  5:19 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27  5:19 [PATCH net 0/2] vsock: preserve receive timeout across partial reads Ren Wei
2026-11-27  5:19 ` [PATCH net 1/2] " Ren Wei
2026-11-27  5:19 ` Ren Wei [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=503c1d5538f5b41f8eda756f5ea4491f0bd44518.1795591531.git.caoruide123@gmail.com \
    --to=weir@nebusec.ai \
    --cc=arseny.krasnov@kaspersky.com \
    --cc=bobbyeshleman@meta.com \
    --cc=caoruide123@gmail.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sgarzare@redhat.com \
    --cc=vega@nebusec.ai \
    --cc=virtualization@lists.linux.dev \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox