Skip to contentSkip to conversation

vsock/test: Fix occasional failure in SIOCOUTQ tests

PATCH Superseded Konstantin Shkolnyy 2 participants 2 messages May 7, 2025

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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 try SIOCOUTQ several times with small delays in between.

---
 tools/testing/vsock/vsock_test.c | 26 ++++++++++++++++----------
 1 file changed, 16 insertions(+), 10 deletions(-)
tools/testing/vsock/vsock_test.c +16 -10
index d0f6d253ac72..143f1cba2d18 100644
@@ -1264,21 +1264,27 @@ 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 */
+	for (int i = 0; i < 10; i++) {
+		ret = ioctl(fd, SIOCOUTQ, &sock_bytes_unsent);
+		if (ret == 0 && sock_bytes_unsent == 0)
+			goto success;
+
+		if (ret < 0) {
+			if (errno == EOPNOTSUPP) {
+				fprintf(stderr, "Test skipped, SIOCOUTQ not supported.\n");
+				goto success;
+			}
 			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);
+		usleep(10 * 1000);
 	}
 
+	fprintf(stderr, "Unexpected 'SIOCOUTQ' value, expected 0, got %i\n",
+		sock_bytes_unsent);
+	exit(EXIT_FAILURE);
+success:
 	close(fd);
 }
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 53 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 try SIOCOUTQ several times with small delays in between.

---
tools/testing/vsock/vsock_test.c | 26 ++++++++++++++++----------

1 file changed, 16 insertions(+), 10 deletions(-)
tools/testing/vsock/vsock_test.c +11 -8
index d0f6d253ac72..143f1cba2d18 100644
@@ -1264,21 +1264,27 @@ 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 */
+	for (int i = 0; i < 10; i++) {
+		ret = ioctl(fd, SIOCOUTQ, &sock_bytes_unsent);
+		if (ret == 0 && sock_bytes_unsent == 0)
+			goto success;
+
+		if (ret < 0) {
+			if (errno == EOPNOTSUPP) {
+				fprintf(stderr, "Test skipped, SIOCOUTQ not supported.\n");
+				goto success;
+			}
 			perror("ioctl");
 			exit(EXIT_FAILURE);
 		}
-	} else if (ret == 0 && sock_bytes_unsent != 0) {
-		fprintf(stderr,
-			"Unexpected 'SIOCOUTQ' value, expected 0, got %i\n",

tools/testing/vsock/vsock_test.c static void test_unsent_bytes_client(const struct test_opts *opts, int type) · line 1288

Quoted 10 lines
tools/testing/vsock/vsock_test.c +5 -2
-			sock_bytes_unsent);
-		exit(EXIT_FAILURE);
+		usleep(10 * 1000);
 	}
 
+	fprintf(stderr, "Unexpected 'SIOCOUTQ' value, expected 0, got %i\n",
+		sock_bytes_unsent);
+	exit(EXIT_FAILURE);
+success:
 	close(fd);

I worked on something similar but I didn't yet send it.

I like the delay you put, but I prefer to use the timeout stuff we have to retry, like I did here:

@@ -1264,20 +1270,25 @@ static void test_unsent_bytes_client(const struct test_opts *op ts, 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 {
- perror("ioctl");
- exit(EXIT_FAILURE);
+ /* Although we have a control message, we are not sure that the vsock

+        * transport has sent us notification that the buffer has been copied
+        * and cleared, so in some cases we may still see unsent bytes.
+        * Better to do a few iterations to be sure.
+        */

+ 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;
+ } else {
+ 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);

-       }
+       } while (sock_bytes_unsent != 0);
+       timeout_end();

What about combining the two?

Thanks,
Stefano