]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
vsock/test: Introduce vsock_bind()
authorMichal Luczaj <mhal@rbox.co>
Tue, 28 Jan 2025 13:15:29 +0000 (14:15 +0100)
committerJakub Kicinski <kuba@kernel.org>
Thu, 30 Jan 2025 02:50:37 +0000 (18:50 -0800)
Add a helper for socket()+bind(). Adapt callers.

Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
Reviewed-by: Luigi Leonardi <leonardi@redhat.com>
Signed-off-by: Michal Luczaj <mhal@rbox.co>
Link: https://patch.msgid.link/20250128-vsock-transport-vs-autobind-v3-3-1cf57065b770@rbox.co
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
tools/testing/vsock/util.c
tools/testing/vsock/util.h
tools/testing/vsock/vsock_test.c

index 7058dc614c25f546fc3219d6b9ade2dcef21a9bd..6e36f9371532cfc75dd14131b84bf8393ca0952b 100644 (file)
@@ -96,33 +96,43 @@ void vsock_wait_remote_close(int fd)
        close(epollfd);
 }
 
-/* Bind to <bind_port>, connect to <cid, port> and return the file descriptor. */
-int vsock_bind_connect(unsigned int cid, unsigned int port, unsigned int bind_port, int type)
+/* Create socket <type>, bind to <cid, port> and return the file descriptor. */
+int vsock_bind(unsigned int cid, unsigned int port, int type)
 {
-       struct sockaddr_vm sa_client = {
-               .svm_family = AF_VSOCK,
-               .svm_cid = VMADDR_CID_ANY,
-               .svm_port = bind_port,
-       };
-       struct sockaddr_vm sa_server = {
+       struct sockaddr_vm sa = {
                .svm_family = AF_VSOCK,
                .svm_cid = cid,
                .svm_port = port,
        };
+       int fd;
 
-       int client_fd, ret;
-
-       client_fd = socket(AF_VSOCK, type, 0);
-       if (client_fd < 0) {
+       fd = socket(AF_VSOCK, type, 0);
+       if (fd < 0) {
                perror("socket");
                exit(EXIT_FAILURE);
        }
 
-       if (bind(client_fd, (struct sockaddr *)&sa_client, sizeof(sa_client))) {
+       if (bind(fd, (struct sockaddr *)&sa, sizeof(sa))) {
                perror("bind");
                exit(EXIT_FAILURE);
        }
 
+       return fd;
+}
+
+/* Bind to <bind_port>, connect to <cid, port> and return the file descriptor. */
+int vsock_bind_connect(unsigned int cid, unsigned int port, unsigned int bind_port, int type)
+{
+       struct sockaddr_vm sa_server = {
+               .svm_family = AF_VSOCK,
+               .svm_cid = cid,
+               .svm_port = port,
+       };
+
+       int client_fd, ret;
+
+       client_fd = vsock_bind(VMADDR_CID_ANY, bind_port, type);
+
        timeout_begin(TIMEOUT);
        do {
                ret = connect(client_fd, (struct sockaddr *)&sa_server, sizeof(sa_server));
@@ -192,28 +202,9 @@ int vsock_seqpacket_connect(unsigned int cid, unsigned int port)
 /* Listen on <cid, port> and return the file descriptor. */
 static int vsock_listen(unsigned int cid, unsigned int port, int type)
 {
-       union {
-               struct sockaddr sa;
-               struct sockaddr_vm svm;
-       } addr = {
-               .svm = {
-                       .svm_family = AF_VSOCK,
-                       .svm_port = port,
-                       .svm_cid = cid,
-               },
-       };
        int fd;
 
-       fd = socket(AF_VSOCK, type, 0);
-       if (fd < 0) {
-               perror("socket");
-               exit(EXIT_FAILURE);
-       }
-
-       if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) {
-               perror("bind");
-               exit(EXIT_FAILURE);
-       }
+       fd = vsock_bind(cid, port, type);
 
        if (listen(fd, 1) < 0) {
                perror("listen");
index e62f46b2b92a7916e83e1e623b43c811b077db3e..077842905bc3e7a4bbd22caba4b7bde976de2718 100644 (file)
@@ -43,6 +43,7 @@ int vsock_connect(unsigned int cid, unsigned int port, int type);
 int vsock_accept(unsigned int cid, unsigned int port,
                 struct sockaddr_vm *clientaddrp, int type);
 int vsock_stream_connect(unsigned int cid, unsigned int port);
+int vsock_bind(unsigned int cid, unsigned int port, int type);
 int vsock_bind_connect(unsigned int cid, unsigned int port,
                       unsigned int bind_port, int type);
 int vsock_seqpacket_connect(unsigned int cid, unsigned int port);
index 1eebbc0d5f616bb1afab3ec3f9e59cb609f9f6e8..daa4f3ca9b6e7267d1bb14a7d43499da3bafb108 100644 (file)
@@ -113,24 +113,9 @@ static void test_stream_bind_only_client(const struct test_opts *opts)
 
 static void test_stream_bind_only_server(const struct test_opts *opts)
 {
-       union {
-               struct sockaddr sa;
-               struct sockaddr_vm svm;
-       } addr = {
-               .svm = {
-                       .svm_family = AF_VSOCK,
-                       .svm_port = opts->peer_port,
-                       .svm_cid = VMADDR_CID_ANY,
-               },
-       };
        int fd;
 
-       fd = socket(AF_VSOCK, SOCK_STREAM, 0);
-
-       if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) {
-               perror("bind");
-               exit(EXIT_FAILURE);
-       }
+       fd = vsock_bind(VMADDR_CID_ANY, opts->peer_port, SOCK_STREAM);
 
        /* Notify the client that the server is ready */
        control_writeln("BIND");