Skip to contentSkip to conversation

vsock/test: Fix occasional failure in SIOCOUTQ tests

PATCH Applied Konstantin Shkolnyy 4 participants 7 messages May 7, 2025 – May 13, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 vsock/test: Fix occasional failure in SIOCOUTQ tests

These tests:
"SOCK_STREAM ioctl(SIOCOUTQ) 0 unsent bytes" "SOCK_SEQPACKET ioctl(SIOCOUTQ) 0 unsent bytes"
output: "Unexpected 'SIOCOUTQ' value, expected 0, got 64 (CLIENT)".

They test that the SIOCOUTQ ioctl reports 0 unsent bytes after the data have been received by the other side. However, sometimes there is a delay in updating this "unsent bytes" counter, and the test fails even though the counter properly goes to 0 several milliseconds later.

The delay occurs in the kernel because the used buffer notification callback virtio_vsock_tx_done(), called upon receipt of the data by the other side, doesn't update the counter itself. It delegates that to a kernel thread (via vsock->tx_work). Sometimes that thread is delayed more than the test expects.

Change the test to poll SIOCOUTQ until it returns 0 or a timeout occurs.

---
Changes in v2:
- Use timeout_check() to end polling, instead of counting iterations.

 tools/testing/vsock/vsock_test.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)
tools/testing/vsock/vsock_test.c +16 -12
index d0f6d253ac72..613551132a96 100644
@@ -1264,21 +1264,25 @@ static void test_unsent_bytes_client(const struct test_opts *opts, int type)
 	send_buf(fd, buf, sizeof(buf), 0, sizeof(buf));
 	control_expectln("RECEIVED");
 
-	ret = ioctl(fd, SIOCOUTQ, &sock_bytes_unsent);
-	if (ret < 0) {
-		if (errno == EOPNOTSUPP) {
-			fprintf(stderr, "Test skipped, SIOCOUTQ not supported.\n");
-		} else {
+	/* SIOCOUTQ isn't guaranteed to instantly track sent data. Even though
+	 * the "RECEIVED" message means that the other side has received the
+	 * data, there can be a delay in our kernel before updating the "unsent
+	 * bytes" counter. Repeat SIOCOUTQ until it returns 0.
+	 */
+	timeout_begin(TIMEOUT);
+	do {
+		ret = ioctl(fd, SIOCOUTQ, &sock_bytes_unsent);
+		if (ret < 0) {
+			if (errno == EOPNOTSUPP) {
+				fprintf(stderr, "Test skipped, SIOCOUTQ not supported.\n");
+				break;
+			}
 			perror("ioctl");
 			exit(EXIT_FAILURE);
 		}
-	} else if (ret == 0 && sock_bytes_unsent != 0) {
-		fprintf(stderr,
-			"Unexpected 'SIOCOUTQ' value, expected 0, got %i\n",
-			sock_bytes_unsent);
-		exit(EXIT_FAILURE);
-	}
-
+		timeout_check("SIOCOUTQ");
+	} while (sock_bytes_unsent != 0);
+	timeout_end();
 	close(fd);
 }
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 17 lines · Konstantin Shkolnyy

These tests:
"SOCK_STREAM ioctl(SIOCOUTQ) 0 unsent bytes" "SOCK_SEQPACKET ioctl(SIOCOUTQ) 0 unsent bytes"
output: "Unexpected 'SIOCOUTQ' value, expected 0, got 64 (CLIENT)".

They test that the SIOCOUTQ ioctl reports 0 unsent bytes after the data have been received by the other side. However, sometimes there is a delay in updating this "unsent bytes" counter, and the test fails even though the counter properly goes to 0 several milliseconds later.

The delay occurs in the kernel because the used buffer notification callback virtio_vsock_tx_done(), called upon receipt of the data by the other side, doesn't update the counter itself. It delegates that to a kernel thread (via vsock->tx_work). Sometimes that thread is delayed more than the test expects.

Quoted 6 lines

Change the test to poll SIOCOUTQ until it returns 0 or a timeout occurs.

---
Changes in v2:
- Use timeout_check() to end polling, instead of counting iterations.

Why removing the sleep?

Thanks,
Stefano

Quoted 49 lines
 tools/testing/vsock/vsock_test.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)
tools/testing/vsock/vsock_test.c +16 -13
index d0f6d253ac72..613551132a96 100644
@@ -1264,21 +1264,25 @@ static void test_unsent_bytes_client(const struct test_opts *opts, int type)
        send_buf(fd, buf, sizeof(buf), 0, sizeof(buf));
        control_expectln("RECEIVED");
 
-       ret = ioctl(fd, SIOCOUTQ, &sock_bytes_unsent);
-       if (ret < 0) {
-               if (errno == EOPNOTSUPP) {
-                       fprintf(stderr, "Test skipped, SIOCOUTQ not supported.\n");
-               } else {
+       /* SIOCOUTQ isn't guaranteed to instantly track sent data. Even though
+        * the "RECEIVED" message means that the other side has received the
+        * data, there can be a delay in our kernel before updating the "unsent
+        * bytes" counter. Repeat SIOCOUTQ until it returns 0.
+        */
+       timeout_begin(TIMEOUT);
+       do {
+               ret = ioctl(fd, SIOCOUTQ, &sock_bytes_unsent);
+               if (ret < 0) {
+                       if (errno == EOPNOTSUPP) {
+                               fprintf(stderr, "Test skipped, SIOCOUTQ not supported.\n");
+                               break;
+                       }
                        perror("ioctl");
                        exit(EXIT_FAILURE);
                }
-       } else if (ret == 0 && sock_bytes_unsent != 0) {
-               fprintf(stderr,
-                       "Unexpected 'SIOCOUTQ' value, expected 0, got %i\n",
-                       sock_bytes_unsent);
-               exit(EXIT_FAILURE);
-       }
-
+               timeout_check("SIOCOUTQ");
+       } while (sock_bytes_unsent != 0);
+       timeout_end();
        close(fd);
 }
 
--

2.34.1

#2 lore raw
Quoted 18 lines · Stefano Garzarella
Quoted 17 lines · Konstantin Shkolnyy

These tests:
"SOCK_STREAM ioctl(SIOCOUTQ) 0 unsent bytes" "SOCK_SEQPACKET ioctl(SIOCOUTQ) 0 unsent bytes"
output: "Unexpected 'SIOCOUTQ' value, expected 0, got 64 (CLIENT)".

They test that the SIOCOUTQ ioctl reports 0 unsent bytes after the data have been received by the other side. However, sometimes there is a delay in updating this "unsent bytes" counter, and the test fails even though the counter properly goes to 0 several milliseconds later.

The delay occurs in the kernel because the used buffer notification callback virtio_vsock_tx_done(), called upon receipt of the data by the other side, doesn't update the counter itself. It delegates that to a kernel thread (via vsock->tx_work). Sometimes that thread is delayed more than the test expects.

Quoted 8 lines
Quoted 6 lines

Change the test to poll SIOCOUTQ until it returns 0 or a timeout occurs.

---
Changes in v2:
- Use timeout_check() to end polling, instead of counting iterations.

Why removing the sleep?

I just imagined that whoever uses SIOCOUTQ might want to repeat it without a delay, so why not do it, it's a test. Is there a reason to insert a sleep?

maintainer Reviewed-by
#3 lore raw
Quoted 27 lines · Konstantin Shkolnyy
Quoted 25 lines · Stefano Garzarella
Quoted 23 lines · Konstantin Shkolnyy

These tests:
"SOCK_STREAM ioctl(SIOCOUTQ) 0 unsent bytes" "SOCK_SEQPACKET ioctl(SIOCOUTQ) 0 unsent bytes"
output: "Unexpected 'SIOCOUTQ' value, expected 0, got 64 (CLIENT)".

They test that the SIOCOUTQ ioctl reports 0 unsent bytes after the data have been received by the other side. However, sometimes there is a delay in updating this "unsent bytes" counter, and the test fails even though the counter properly goes to 0 several milliseconds later.

The delay occurs in the kernel because the used buffer notification callback virtio_vsock_tx_done(), called upon receipt of the data by the other side, doesn't update the counter itself. It delegates that to a kernel thread (via vsock->tx_work). Sometimes that thread is delayed more than the test expects.

Change the test to poll SIOCOUTQ until it returns 0 or a timeout occurs.

---
Changes in v2:
- Use timeout_check() to end polling, instead of counting iterations.

Quoted 6 lines
Quoted 1 line

Why removing the sleep?

I just imagined that whoever uses SIOCOUTQ might want to repeat it without a delay, so why not do it, it's a test. Is there a reason to insert a sleep?

Okay, now that I think back on it, it's the same thing I thought of when I did this.

I guess in v1 the sleep was just to limit the number of cycles.

LGTM:

maintainer
#4 lore raw
In reply to Konstantin Shkolnyy
Quoted 16 lines · Konstantin Shkolnyy

These tests:
"SOCK_STREAM ioctl(SIOCOUTQ) 0 unsent bytes" "SOCK_SEQPACKET ioctl(SIOCOUTQ) 0 unsent bytes"
output: "Unexpected 'SIOCOUTQ' value, expected 0, got 64 (CLIENT)".

They test that the SIOCOUTQ ioctl reports 0 unsent bytes after the data have been received by the other side. However, sometimes there is a delay in updating this "unsent bytes" counter, and the test fails even though the counter properly goes to 0 several milliseconds later.

The delay occurs in the kernel because the used buffer notification callback virtio_vsock_tx_done(), called upon receipt of the data by the other side, doesn't update the counter itself. It delegates that to a kernel thread (via vsock->tx_work). Sometimes that thread is delayed more than the test expects.

Quoted 3 lines

Change the test to poll SIOCOUTQ until it returns 0 or a timeout occurs.

Could you please provide a suitable fixes tag?

No need to repost, just reply here.

Thanks!

Paolo

#5 lore raw
Quoted 17 lines · Paolo Abeni
Quoted 16 lines · Konstantin Shkolnyy

These tests:
"SOCK_STREAM ioctl(SIOCOUTQ) 0 unsent bytes" "SOCK_SEQPACKET ioctl(SIOCOUTQ) 0 unsent bytes"
output: "Unexpected 'SIOCOUTQ' value, expected 0, got 64 (CLIENT)".

They test that the SIOCOUTQ ioctl reports 0 unsent bytes after the data have been received by the other side. However, sometimes there is a delay in updating this "unsent bytes" counter, and the test fails even though the counter properly goes to 0 several milliseconds later.

The delay occurs in the kernel because the used buffer notification callback virtio_vsock_tx_done(), called upon receipt of the data by the other side, doesn't update the counter itself. It delegates that to a kernel thread (via vsock->tx_work). Sometimes that thread is delayed more than the test expects.

Quoted 7 lines
Quoted 3 lines

Change the test to poll SIOCOUTQ until it returns 0 or a timeout occurs.

Could you please provide a suitable fixes tag?

No need to repost, just reply here.

I always get confused whether to use Fixes tags for tests, but I saw this patch target net, so it makes sense. BTW IMHO it can go eventually through net-next, which is the target tree I usually use for new tests but also test fixes.

In any case, the tag should be this one:

Fixes 18ee44ce97c1 ("test/vsock: add ioctl unsent bytes test")

Thanks,
Stefano

#6 lore raw
In reply to Konstantin Shkolnyy
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 11 lines · On Wed, 7 May 2025 10:14:56 -0500 you wrote:

These tests:
"SOCK_STREAM ioctl(SIOCOUTQ) 0 unsent bytes" "SOCK_SEQPACKET ioctl(SIOCOUTQ) 0 unsent bytes"
output: "Unexpected 'SIOCOUTQ' value, expected 0, got 64 (CLIENT)".

They test that the SIOCOUTQ ioctl reports 0 unsent bytes after the data have been received by the other side. However, sometimes there is a delay in updating this "unsent bytes" counter, and the test fails even though the counter properly goes to 0 several milliseconds later.

[...]

Here is the summary with links:
- [net,v2] vsock/test: Fix occasional failure in SIOCOUTQ tests

    https://git.kernel.org/netdev/net/c/7fd7ad6f36af

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html