Skip to contentSkip to conversation

VSOCK: add vsock_test test suite

PATCH In review Stefan Hajnoczi 3 participants 11 messages Dec 13, 2017 – Jan 3, 2018

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

cover VSOCK: add vsock_test test suite

The vsock_diag.ko module already has a test suite but the core AF_VSOCK functionality has no tests. This patch series adds several test cases that exercise AF_VSOCK SOCK_STREAM socket semantics (send/recv, connect/accept, half-closed connections, simultaneous connections).

The test suite is modest but I hope to cover additional cases in the future. My goal is to have a shared test suite so VMCI, Hyper-V, and KVM can ensure that our transports behave the same.

I have tested virtio-vsock.

Jorgen: Please test the VMCI transport and let me know if anything needs to be adjusted. See tools/testing/vsock/README for information on how to run the test suite.

Dexuan: I'm not sure if this test suite is useful for the Hyper-V transport since the host is Windows and uses a different API for AF_HYPERV?

Stefan Hajnoczi (5):
VSOCK: extract utility functions from vsock_diag_test.c
VSOCK: extract connect/accept functions from vsock_diag_test.c
VSOCK: add full barrier between test cases
VSOCK: add send_byte()/recv_byte() test utilities
VSOCK: add AF_VSOCK test cases

 tools/testing/vsock/Makefile          |   7 +-
 tools/testing/vsock/util.h            |  43 +++++
 tools/testing/vsock/util.c            | 291 ++++++++++++++++++++++++++++++
 tools/testing/vsock/vsock_diag_test.c | 167 +++---------------
 tools/testing/vsock/vsock_test.c      | 321 ++++++++++++++++++++++++++++++++++
 tools/testing/vsock/.gitignore        |   1 +
 tools/testing/vsock/README            |   1 +
 7 files changed, 685 insertions(+), 146 deletions(-)
 create mode 100644 tools/testing/vsock/util.h
 create mode 100644 tools/testing/vsock/util.c
 create mode 100644 tools/testing/vsock/vsock_test.c
Signature · 1 line
2.14.3
#1 lore raw

PATCH 1/5 VSOCK: extract utility functions from vsock_diag_test.c

Move useful functions into a separate file in preparation for more vsock test programs.

---
 tools/testing/vsock/Makefile          |  2 +-
 tools/testing/vsock/util.h            | 35 +++++++++++++
 tools/testing/vsock/util.c            | 70 ++++++++++++++++++++++++++
 tools/testing/vsock/vsock_diag_test.c | 92 +++++++++--------------------------
 4 files changed, 128 insertions(+), 71 deletions(-)
 create mode 100644 tools/testing/vsock/util.h
 create mode 100644 tools/testing/vsock/util.c
tools/testing/vsock/Makefile +1 -1
index 66ba0924194d..cf0650007fa8 100644
@@ -1,6 +1,6 @@
 all: test
 test: vsock_diag_test
-vsock_diag_test: vsock_diag_test.o timeout.o control.o
+vsock_diag_test: vsock_diag_test.o timeout.o control.o util.o
 
 CFLAGS += -g -O2 -Werror -Wall -I. -I../../include/uapi -I../../include -Wno-pointer-sign -fno-strict-overflow -fno-strict-aliasing -fno-common -MMD -U_FORTIFY_SOURCE -D_GNU_SOURCE
 .PHONY: all test clean
tools/testing/vsock/util.h new file +35 -0
new file mode 100644
index 000000000000..a51e17578ccf
@@ -0,0 +1,35 @@
+#ifndef UTIL_H
+#define UTIL_H
+
+/* Tests can either run as the client or the server */
+enum test_mode {
+	TEST_MODE_UNSET,
+	TEST_MODE_CLIENT,
+	TEST_MODE_SERVER
+};
+
+/* Test runner options */
+struct test_opts {
+	enum test_mode mode;
+	unsigned int peer_cid;
+};
+
+/* A test case definition.  Test functions must print failures to stderr and
+ * terminate with exit(EXIT_FAILURE).
+ */
+struct test_case {
+	const char *name; /* human-readable name */
+
+	/* Called when test mode is TEST_MODE_CLIENT */
+	void (*run_client)(const struct test_opts *opts);
+
+	/* Called when test mode is TEST_MODE_SERVER */
+	void (*run_server)(const struct test_opts *opts);
+};
+
+void init_signals(void);
+unsigned int parse_cid(const char *str);
+void run_tests(const struct test_case *test_cases,
+	       const struct test_opts *opts);
+
+#endif /* UTIL_H */
tools/testing/vsock/util.c new file +70 -0

70 lines not on this page · raw message

tools/testing/vsock/vsock_diag_test.c +22 -70

191 lines not on this page · raw message

Signature · 1 line
2.14.3
#2 lore raw

PATCH 2/5 VSOCK: extract connect/accept functions from vsock_diag_test.c

In reply to Stefan Hajnoczi

Many test cases will need to connect to the server or accept incoming connections. This patch extracts these operations into utility functions that can be reused.

---
 tools/testing/vsock/util.h            |   6 ++
 tools/testing/vsock/util.c            | 108 ++++++++++++++++++++++++++++++++++
 tools/testing/vsock/vsock_diag_test.c |  81 ++-----------------------
 3 files changed, 119 insertions(+), 76 deletions(-)
tools/testing/vsock/util.h +6 -0
index a51e17578ccf..3e6971f15caa 100644
@@ -1,6 +1,9 @@
 #ifndef UTIL_H
 #define UTIL_H
 
+#include <sys/socket.h>
+#include "../../../include/uapi/linux/vm_sockets.h"
+
 /* Tests can either run as the client or the server */
 enum test_mode {
 	TEST_MODE_UNSET,
@@ -29,6 +32,9 @@ struct test_case {
 
 void init_signals(void);
 unsigned int parse_cid(const char *str);
+int vsock_stream_connect(unsigned int cid, unsigned int port);
+int vsock_stream_accept(unsigned int cid, unsigned int port,
+			struct sockaddr_vm *clientaddrp);
 void run_tests(const struct test_case *test_cases,
 	       const struct test_opts *opts);
 
tools/testing/vsock/util.c +108 -0

122 lines not on this page · raw message

tools/testing/vsock/vsock_diag_test.c +5 -76

125 lines not on this page · raw message

Signature · 1 line
2.14.3
maintainer
#3 lore raw

Re: [PATCH 2/5] VSOCK: extract connect/accept functions from vsock_diag_test.c

From: Stefan Hajnoczi <[email protected]>
Date: Wed, 13 Dec 2017 14:49:08 +0000

Quoted 3 lines
tools/testing/vsock/util.h +3 -0
+#include <sys/socket.h>
+#include "../../../include/uapi/linux/vm_sockets.h"
+

...

Quoted 2 lines

-#include "../../../include/uapi/linux/vm_sockets.h"
#include "../../../include/uapi/linux/vm_sockets_diag.h"

This really should never be necessary.

As part of the build, before tunning tests, one is required to do "make headers_install" and whether as root or a normal user, this will make the UAPI headers of the current kernel available to the build of the testcases.

So please undo this weird relative include stuff.

Thanks.

#4 lore raw

Re: [PATCH 2/5] VSOCK: extract connect/accept functions from vsock_diag_test.c

Quoted 10 lines · David Miller

From: Stefan Hajnoczi <[email protected]>
Date: Wed, 13 Dec 2017 14:49:08 +0000

Quoted 3 lines
tools/testing/vsock/util.h +3 -0
+#include <sys/socket.h>
+#include "../../../include/uapi/linux/vm_sockets.h"
+

...

Quoted 2 lines

-#include "../../../include/uapi/linux/vm_sockets.h"
#include "../../../include/uapi/linux/vm_sockets_diag.h"

Quoted 8 lines

This really should never be necessary.

As part of the build, before tunning tests, one is required to do "make headers_install" and whether as root or a normal user, this will make the UAPI headers of the current kernel available to the build of the testcases.

So please undo this weird relative include stuff.

Thanks for explaining! Will fix in v2.

Stefan

#5 lore raw

PATCH 3/5 VSOCK: add full barrier between test cases

In reply to Stefan Hajnoczi

See code comment for details.

---
 tools/testing/vsock/util.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
tools/testing/vsock/util.c +16 -2
index 75a78f295b37..2be923fe9922 100644
@@ -165,10 +165,24 @@ void run_tests(const struct test_case *test_cases,
 		printf("%s...", test_cases[i].name);
 		fflush(stdout);
 
-		if (opts->mode == TEST_MODE_CLIENT)
+		if (opts->mode == TEST_MODE_CLIENT) {
+			/* Full barrier before executing the next test.  This
+			 * ensures that client and server are executing the
+			 * same test case.  In particular, it means whoever is
+			 * faster will not see the peer still executing the
+			 * last test.  This is important because port numbers
+			 * can be used by multiple test cases.
+			 */
+			control_expectln("NEXT");
+			control_writeln("NEXT");
+
 			run = test_cases[i].run_client;
-		else
+		} else {
+			control_writeln("NEXT");
+			control_expectln("NEXT");
+
 			run = test_cases[i].run_server;
+		}
 
 		if (run)
 			run(opts);
Signature · 1 line
2.14.3
#6 lore raw

PATCH 4/5 VSOCK: add send_byte()/recv_byte() test utilities

In reply to Stefan Hajnoczi

Test cases will want to transfer data. This patch adds utility functions to do this.

---
 tools/testing/vsock/util.h |  2 +
 tools/testing/vsock/util.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 101 insertions(+)
tools/testing/vsock/util.h +2 -0
index 3e6971f15caa..cf43d42591bc 100644
@@ -35,6 +35,8 @@ unsigned int parse_cid(const char *str);
 int vsock_stream_connect(unsigned int cid, unsigned int port);
 int vsock_stream_accept(unsigned int cid, unsigned int port,
 			struct sockaddr_vm *clientaddrp);
+void send_byte(int fd, int expected_ret);
+void recv_byte(int fd, int expected_ret);
 void run_tests(const struct test_case *test_cases,
 	       const struct test_opts *opts);
 
tools/testing/vsock/util.c +99 -0

111 lines not on this page · raw message

Signature · 1 line
2.14.3
#7 lore raw

PATCH 5/5 VSOCK: add AF_VSOCK test cases

In reply to Stefan Hajnoczi

The vsock_test.c program runs a test suite of AF_VSOCK test cases.

---
 tools/testing/vsock/Makefile     |   5 +-
 tools/testing/vsock/vsock_test.c | 321 +++++++++++++++++++++++++++++++++++++++
 tools/testing/vsock/.gitignore   |   1 +
 tools/testing/vsock/README       |   1 +
 4 files changed, 326 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/vsock/vsock_test.c
tools/testing/vsock/Makefile +3 -2
index cf0650007fa8..9efb9010df2e 100644
@@ -1,9 +1,10 @@
 all: test
-test: vsock_diag_test
+test: vsock_test vsock_diag_test
+vsock_test: vsock_test.o timeout.o control.o util.o
 vsock_diag_test: vsock_diag_test.o timeout.o control.o util.o
 
 CFLAGS += -g -O2 -Werror -Wall -I. -I../../include/uapi -I../../include -Wno-pointer-sign -fno-strict-overflow -fno-strict-aliasing -fno-common -MMD -U_FORTIFY_SOURCE -D_GNU_SOURCE
 .PHONY: all test clean
 clean:
-	${RM} *.o *.d vsock_diag_test
+	${RM} *.o *.d vsock_test vsock_diag_test
 -include *.d
tools/testing/vsock/vsock_test.c new file +321 -0

321 lines not on this page · raw message

tools/testing/vsock/.gitignore +1 -0
index dc5f11faf530..7f7a2ccc30c4 100644
@@ -1,2 +1,3 @@
 *.d
+vsock_test
 vsock_diag_test
tools/testing/vsock/README +1 -0
index 2cc6d7302db6..3014d9e4959a 100644
@@ -5,6 +5,7 @@ Hyper-V.
 
 The following tests are available:
 
+  * vsock_test - core AF_VSOCK socket functionality
   * vsock_diag_test - vsock_diag.ko module for listing open sockets
 
 The following prerequisite steps are not automated and must be performed prior
Signature · 1 line
2.14.3
#8 lore raw
In reply to Stefan Hajnoczi
Quoted 12 lines · Stefan Hajnoczi

The vsock_diag.ko module already has a test suite but the core AF_VSOCK functionality has no tests. This patch series adds several test cases that exercise AF_VSOCK SOCK_STREAM socket semantics (send/recv, connect/accept, half-closed connections, simultaneous connections).

The test suite is modest but I hope to cover additional cases in the future. My goal is to have a shared test suite so VMCI, Hyper-V, and KVM can ensure that our transports behave the same.

Quoted 6 lines

I have tested virtio-vsock.

Jorgen: Please test the VMCI transport and let me know if anything needs to be adjusted. See tools/testing/vsock/README for information on how to run the test suite.

I tried running the vsock_test on VMCI, and all the tests failed in one way or another:
1) connection reset test: when the guest tries to connect to the host, we get EINVAL as the error instead of ECONNRESET. I’ll fix that.
2) client close and server close tests: On the host side, VMCI doesn’t support reading data from a socket that has been closed by the guest. When the guest closes a connection, all data is gone, and we return EOF on the host side. So the tests that try to read data after close, should not attempt that on VMCI host side. I got the tests to pass by adding a getsockname call to determine if
the local CID was the host CID, and then skip the read attempt in that case. We could add a vmci flag, that would enable
this behavior.
3) send_byte(fd, -EPIPE): for the VMCI transport, the close
isn’t necessarily visible immediately on the peer. So in most cases, these send operations would complete with success.
I was running these tests using nested virtualization, so I suspect that the problem is more likely to occur here, but
I had to add a sleep to be sure to get the EPIPE error.
4) server close test: the connect would sometimes fail. This looks like an issue where we detect the peer close on the client side before we complete the connection handshake on the client
side. There are two different channels used for the connection handshake and the disconnect. I’ll look into this to see what exactly is going on.
5) multiple connections tests: with the standard socket sizes, VMCI is only able to support about 100 concurrent stream connections so this test passes with MULTICONN_NFDS
set to 100.

Thanks,
Jorgen

#9 lore raw
Quoted 14 lines · Jorgen S. Hansen
Quoted 14 lines · Stefan Hajnoczi

The vsock_diag.ko module already has a test suite but the core AF_VSOCK functionality has no tests. This patch series adds several test cases that exercise AF_VSOCK SOCK_STREAM socket semantics (send/recv, connect/accept, half-closed connections, simultaneous connections).

The test suite is modest but I hope to cover additional cases in the future. My goal is to have a shared test suite so VMCI, Hyper-V, and KVM can ensure that our transports behave the same.

I have tested virtio-vsock.

Quoted 7 lines
Quoted 4 lines

Jorgen: Please test the VMCI transport and let me know if anything needs to be adjusted. See tools/testing/vsock/README for information on how to run the test suite.

I tried running the vsock_test on VMCI, and all the tests failed in one way or another:

Great, thank you for testing and looking into the failures!

Quoted 2 lines

1) connection reset test: when the guest tries to connect to the host, we get EINVAL as the error instead of ECONNRESET. I’ll fix that.

Yay, the tests found a bug!

Quoted 9 lines

2) client close and server close tests: On the host side, VMCI doesn’t support reading data from a socket that has been closed by the guest. When the guest closes a connection, all data is gone, and we return EOF on the host side. So the tests that try to read data after close, should not attempt that on VMCI host side. I got the tests to pass by adding a getsockname call to determine if the local CID was the host CID, and then skip the read attempt in that case. We could add a vmci flag, that would enable this behavior.

Interesting behavior. Is there a reason for disallowing half-closed sockets on the host side?

Quoted 6 lines

3) send_byte(fd, -EPIPE): for the VMCI transport, the close isn’t necessarily visible immediately on the peer. So in most cases, these send operations would complete with success. I was running these tests using nested virtualization, so I suspect that the problem is more likely to occur here, but I had to add a sleep to be sure to get the EPIPE error.

Good point, you've discovered a race condition that affects all transports. The vsock close state transition might not have occurred yet when the TCP control channel receives the "CLOSED" message.

test_stream_client_close_server() needs to wait for the socket status to change before attempting send_byte(fd, -EPIPE). I guess I'll have to use vsock_diag or another kernel interface to check the socket's state.

Quoted 4 lines

5) multiple connections tests: with the standard socket sizes, VMCI is only able to support about 100 concurrent stream connections so this test passes with MULTICONN_NFDS set to 100.

The 1000 magic number is because many distros have a maximum number of file descriptors ulimit of 1024. But it's an arbitrary number and we could lower it to 100.

Is this VMCI concurrent stream limit a denial of service vector? Can an unprivileged guest userspace process open many sockets to prevent legitimate connections from other users within the same guest?

Stefan

#10 lore raw
Quoted 43 lines · Stefan Hajnoczi
Quoted 14 lines · Jorgen S. Hansen
Quoted 14 lines · Stefan Hajnoczi

The vsock_diag.ko module already has a test suite but the core AF_VSOCK functionality has no tests. This patch series adds several test cases that exercise AF_VSOCK SOCK_STREAM socket semantics (send/recv, connect/accept, half-closed connections, simultaneous connections).

The test suite is modest but I hope to cover additional cases in the future. My goal is to have a shared test suite so VMCI, Hyper-V, and KVM can ensure that our transports behave the same.

I have tested virtio-vsock.

Quoted 7 lines
Quoted 4 lines

Jorgen: Please test the VMCI transport and let me know if anything needs to be adjusted. See tools/testing/vsock/README for information on how to run the test suite.

I tried running the vsock_test on VMCI, and all the tests failed in one way or another:

Great, thank you for testing and looking into the failures!

Quoted 2 lines

1) connection reset test: when the guest tries to connect to the host, we get EINVAL as the error instead of ECONNRESET. I’ll fix that.

Yay, the tests found a bug!

Quoted 9 lines

2) client close and server close tests: On the host side, VMCI doesn’t support reading data from a socket that has been closed by the guest. When the guest closes a connection, all data is gone, and we return EOF on the host side. So the tests that try to read data after close, should not attempt that on VMCI host side. I got the tests to pass by adding a getsockname call to determine if the local CID was the host CID, and then skip the read attempt in that case. We could add a vmci flag, that would enable this behavior.

Quoted 2 lines

Interesting behavior. Is there a reason for disallowing half-closed sockets on the host side?

This is a consequence of the way the underlying VMCI queue pairs are implemented. When the guest side closes a connection, it signals this to the peer by detaching from the VMCI queue pair used for the data transfer (the detach will result in an event being generated on the peer side). However, the VMCI queue pair is allocated as part of the guest memory, so when the guest detaches, that memory is reclaimed. So the host side would need to create a copy of the contents of that queue pair in kernel memory as part of the detach operation. When this was implemented, it was decided that it was better to avoid a potential large kernel memory allocation and the data copy at detach time than to maintain the half close behavior of INET.

Quoted 6 lines
Quoted 5 lines

5) multiple connections tests: with the standard socket sizes, VMCI is only able to support about 100 concurrent stream connections so this test passes with MULTICONN_NFDS set to 100.

Quoted 7 lines

The 1000 magic number is because many distros have a maximum number of file descriptors ulimit of 1024. But it's an arbitrary number and we could lower it to 100.

Is this VMCI concurrent stream limit a denial of service vector? Can an unprivileged guest userspace process open many sockets to prevent legitimate connections from other users within the same guest?

vSocket uses VMCI queue pairs for the stream, and the VMCI device only allows a limited amount of memory to be used for queue pairs per VM. So it is possible to exhaust this shared resource. The queue pairs are created as part of the connection establishment process, so it would require the user process to both create and connect the sockets to a host side endpoint (connections between guest processes will not allocate VMCI queue pairs).

Thanks,
Jorgen