All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] Support for AF_UNIX -netdev socket and a small fix
@ 2021-04-23 15:30 Stefano Brivio
  2021-04-23 15:30 ` [PATCH 1/2] net: Allow also UNIX domain sockets to be used as -netdev socket Stefano Brivio
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Stefano Brivio @ 2021-04-23 15:30 UTC (permalink / raw)
  To: qemu-devel; +Cc: Ralph Schmieder, Daniel P. Berrangé, Laine Stump

Patch 1/2 introduces support for AF_UNIX stream-oriented socket to
be passed as -netdev, and patch 2/2 fixes a minor issue I found while
trying to connect to an invalid path.

Stefano Brivio (2):
  net: Allow also UNIX domain sockets to be used as -netdev socket
  net: Don't ignore EINVAL on netdev socket connection

 net/socket.c    | 109 +++++++++++++++++++++++++++++++++++++++---------
 qemu-options.hx |  12 +++---
 2 files changed, 95 insertions(+), 26 deletions(-)

-- 
2.29.2



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] net: Allow also UNIX domain sockets to be used as -netdev socket
  2021-04-23 15:30 [PATCH 0/2] Support for AF_UNIX -netdev socket and a small fix Stefano Brivio
@ 2021-04-23 15:30 ` Stefano Brivio
  2021-04-23 15:30 ` [PATCH 2/2] net: Don't ignore EINVAL on netdev socket connection Stefano Brivio
  2021-04-23 15:39 ` [PATCH 0/2] Support for AF_UNIX -netdev socket and a small fix no-reply
  2 siblings, 0 replies; 4+ messages in thread
From: Stefano Brivio @ 2021-04-23 15:30 UTC (permalink / raw)
  To: qemu-devel; +Cc: Ralph Schmieder, Daniel P. Berrangé, Laine Stump

It has lower overhead compared to TCP, doesn't need a free port
and the adaptation is trivial.

Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
---
 net/socket.c    | 106 ++++++++++++++++++++++++++++++++++++++++--------
 qemu-options.hx |  12 +++---
 2 files changed, 94 insertions(+), 24 deletions(-)

diff --git a/net/socket.c b/net/socket.c
index 15b410e8d825..aadd11dae2b3 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -511,26 +511,60 @@ static int net_socket_listen_init(NetClientState *peer,
 {
     NetClientState *nc;
     NetSocketState *s;
-    struct sockaddr_in saddr;
-    int fd, ret;
+    struct sockaddr_storage saddr;
+    struct sockaddr_in *saddr_in = (struct sockaddr_in *)&saddr;
+    struct sockaddr_un *saddr_un = (struct sockaddr_un *)&saddr;
+    size_t saddr_size;
+    int fd, ret, pf;
+
+#ifndef WIN32
+    if (strchr(host_str, ':')) {
+#endif
+        if (parse_host_port(saddr_in, host_str, errp) < 0)
+            return -1;
 
-    if (parse_host_port(&saddr, host_str, errp) < 0) {
-        return -1;
+        pf = PF_INET;
+        saddr_size = sizeof(*saddr_in);
+#ifndef WIN32
+    } else {
+        struct stat sb;
+
+        if (stat(host_str, &sb) == -1) {
+            error_setg_errno(errp, errno, "can't stat socket path");
+            return -1;
+        }
+
+        if ((sb.st_mode & S_IFMT) != S_IFSOCK) {
+            error_setg_errno(errp, errno, "path provided is not a socket");
+            return -1;
+        }
+
+        saddr_un->sun_family = PF_UNIX;
+        strncpy(saddr_un->sun_path, host_str, sizeof(saddr_un->sun_path));
+
+        pf = PF_UNIX;
+        saddr_size = sizeof(*saddr_un);
     }
+#endif /* !WIN32 */
 
-    fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
+    fd = qemu_socket(pf, SOCK_STREAM, 0);
     if (fd < 0) {
         error_setg_errno(errp, errno, "can't create stream socket");
         return -1;
     }
     qemu_set_nonblock(fd);
 
-    socket_set_fast_reuse(fd);
+    if (pf == PF_INET)
+        socket_set_fast_reuse(fd);
 
-    ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
+    ret = bind(fd, (struct sockaddr *)&saddr, saddr_size);
     if (ret < 0) {
-        error_setg_errno(errp, errno, "can't bind ip=%s to socket",
-                         inet_ntoa(saddr.sin_addr));
+        if (pf == PF_INET)
+            error_setg_errno(errp, errno, "can't bind ip=%s to socket",
+                             inet_ntoa(saddr_in->sin_addr));
+        else if (pf == PF_UNIX)
+            error_setg_errno(errp, errno, "can't create socket with path: %s",
+                             host_str);
         closesocket(fd);
         return -1;
     }
@@ -559,14 +593,44 @@ static int net_socket_connect_init(NetClientState *peer,
                                    Error **errp)
 {
     NetSocketState *s;
-    int fd, connected, ret;
-    struct sockaddr_in saddr;
+    int fd, connected, ret, pf;
+    struct sockaddr_storage saddr;
+    size_t saddr_size;
+    struct sockaddr_in *saddr_in = (struct sockaddr_in *)&saddr;
+#ifndef WIN32
+    struct sockaddr_un *saddr_un = (struct sockaddr_un *)&saddr;
 
-    if (parse_host_port(&saddr, host_str, errp) < 0) {
-        return -1;
+
+    if (strchr(host_str, ':')) {
+#endif
+        if (parse_host_port(saddr_in, host_str, errp) < 0)
+            return -1;
+
+        pf = PF_INET;
+        saddr_size = sizeof(*saddr_in);
+#ifndef WIN32
+    } else {
+        struct stat sb;
+
+        if (stat(host_str, &sb) == -1) {
+            error_setg_errno(errp, errno, "can't stat socket path");
+            return -1;
+        }
+
+        if ((sb.st_mode & S_IFMT) != S_IFSOCK) {
+            error_setg_errno(errp, errno, "provided path is not a socket");
+            return -1;
+        }
+
+        saddr_un->sun_family = PF_UNIX;
+        strncpy(saddr_un->sun_path, host_str, sizeof(saddr_un->sun_path));
+
+        pf = PF_UNIX;
+        saddr_size = sizeof(*saddr_un);
     }
+#endif /* !WIN32 */
 
-    fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
+    fd = qemu_socket(pf, SOCK_STREAM, 0);
     if (fd < 0) {
         error_setg_errno(errp, errno, "can't create stream socket");
         return -1;
@@ -575,7 +639,7 @@ static int net_socket_connect_init(NetClientState *peer,
 
     connected = 0;
     for(;;) {
-        ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
+        ret = connect(fd, (struct sockaddr *)&saddr, saddr_size);
         if (ret < 0) {
             if (errno == EINTR || errno == EWOULDBLOCK) {
                 /* continue */
@@ -598,9 +662,15 @@ static int net_socket_connect_init(NetClientState *peer,
         return -1;
     }
 
-    snprintf(s->nc.info_str, sizeof(s->nc.info_str),
-             "socket: connect to %s:%d",
-             inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
+    if (pf == PF_INET) {
+        snprintf(s->nc.info_str, sizeof(s->nc.info_str),
+                 "socket: connect to %s:%d",
+                 inet_ntoa(saddr_in->sin_addr), ntohs(saddr_in->sin_port));
+    } else if (pf == PF_UNIX) {
+        snprintf(s->nc.info_str, sizeof(s->nc.info_str),
+                 "socket: connect to %s", saddr_un->sun_path);
+    }
+
     return 0;
 }
 
diff --git a/qemu-options.hx b/qemu-options.hx
index fd21002bd61d..625a31dcdbc8 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -2847,13 +2847,13 @@ SRST
         #connect a TAP device to bridge qemubr0
         |qemu_system| linux.img -netdev bridge,br=qemubr0,id=n1 -device virtio-net,netdev=n1
 
-``-netdev socket,id=id[,fd=h][,listen=[host]:port][,connect=host:port]``
+``-netdev socket,id=id[,fd=h][,listen=[host]:port|path][,connect=host:port|path]``
     This host network backend can be used to connect the guest's network
-    to another QEMU virtual machine using a TCP socket connection. If
-    ``listen`` is specified, QEMU waits for incoming connections on port
-    (host is optional). ``connect`` is used to connect to another QEMU
-    instance using the ``listen`` option. ``fd``\ =h specifies an
-    already opened TCP socket.
+    to another QEMU virtual machine using a TCP or a UNIX domain socket
+    connection. If ``listen`` is specified, QEMU waits for incoming
+    connections on port (host is optional), or on path. ``connect`` is used
+    to connect to another QEMU instance using the ``listen`` option.
+    ``fd``\ =h specifies an already opened TCP or UNIX domain socket.
 
     Example:
 
-- 
2.29.2



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] net: Don't ignore EINVAL on netdev socket connection
  2021-04-23 15:30 [PATCH 0/2] Support for AF_UNIX -netdev socket and a small fix Stefano Brivio
  2021-04-23 15:30 ` [PATCH 1/2] net: Allow also UNIX domain sockets to be used as -netdev socket Stefano Brivio
@ 2021-04-23 15:30 ` Stefano Brivio
  2021-04-23 15:39 ` [PATCH 0/2] Support for AF_UNIX -netdev socket and a small fix no-reply
  2 siblings, 0 replies; 4+ messages in thread
From: Stefano Brivio @ 2021-04-23 15:30 UTC (permalink / raw)
  To: qemu-devel; +Cc: Ralph Schmieder, Daniel P. Berrangé, Laine Stump

Other errors are treated as failure by net_socket_connect_init(),
but if connect() returns EINVAL, we'll fail silently. Remove the
related exception.

Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
---
 net/socket.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/net/socket.c b/net/socket.c
index aadd11dae2b3..d3293ac12e82 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -644,8 +644,7 @@ static int net_socket_connect_init(NetClientState *peer,
             if (errno == EINTR || errno == EWOULDBLOCK) {
                 /* continue */
             } else if (errno == EINPROGRESS ||
-                       errno == EALREADY ||
-                       errno == EINVAL) {
+                       errno == EALREADY) {
                 break;
             } else {
                 error_setg_errno(errp, errno, "can't connect socket");
-- 
2.29.2



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 0/2] Support for AF_UNIX -netdev socket and a small fix
  2021-04-23 15:30 [PATCH 0/2] Support for AF_UNIX -netdev socket and a small fix Stefano Brivio
  2021-04-23 15:30 ` [PATCH 1/2] net: Allow also UNIX domain sockets to be used as -netdev socket Stefano Brivio
  2021-04-23 15:30 ` [PATCH 2/2] net: Don't ignore EINVAL on netdev socket connection Stefano Brivio
@ 2021-04-23 15:39 ` no-reply
  2 siblings, 0 replies; 4+ messages in thread
From: no-reply @ 2021-04-23 15:39 UTC (permalink / raw)
  To: sbrivio; +Cc: ralph.schmieder, berrange, qemu-devel, laine

Patchew URL: https://patchew.org/QEMU/cover.1619190878.git.sbrivio@redhat.com/



Hi,

This series seems to have some coding style problems. See output below for
more information:

Type: series
Message-id: cover.1619190878.git.sbrivio@redhat.com
Subject: [PATCH 0/2] Support for AF_UNIX -netdev socket and a small fix

=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram
./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
From https://github.com/patchew-project/qemu
 - [tag update]      patchew/20210421122624.12292-1-david@redhat.com -> patchew/20210421122624.12292-1-david@redhat.com
 - [tag update]      patchew/20210422164344.283389-1-kwolf@redhat.com -> patchew/20210422164344.283389-1-kwolf@redhat.com
 - [tag update]      patchew/20210422230227.314751-1-richard.henderson@linaro.org -> patchew/20210422230227.314751-1-richard.henderson@linaro.org
 - [tag update]      patchew/20210423142440.582188-1-thuth@redhat.com -> patchew/20210423142440.582188-1-thuth@redhat.com
 * [new tag]         patchew/cover.1619190878.git.sbrivio@redhat.com -> patchew/cover.1619190878.git.sbrivio@redhat.com
Switched to a new branch 'test'
8eacf39 net: Don't ignore EINVAL on netdev socket connection
17c0b6e net: Allow also UNIX domain sockets to be used as -netdev socket

=== OUTPUT BEGIN ===
1/2 Checking commit 17c0b6ec8d6b (net: Allow also UNIX domain sockets to be used as -netdev socket)
ERROR: braces {} are necessary for all arms of this statement
#38: FILE: net/socket.c:523:
+        if (parse_host_port(saddr_in, host_str, errp) < 0)
[...]

ERROR: braces {} are necessary for all arms of this statement
#74: FILE: net/socket.c:557:
+    if (pf == PF_INET)
[...]

ERROR: braces {} are necessary for all arms of this statement
#109: FILE: net/socket.c:606:
+        if (parse_host_port(saddr_in, host_str, errp) < 0)
[...]

total: 3 errors, 0 warnings, 163 lines checked

Patch 1/2 has style problems, please review.  If any of these errors
are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.

2/2 Checking commit 8eacf3917406 (net: Don't ignore EINVAL on netdev socket connection)
=== OUTPUT END ===

Test command exited with code: 1


The full log is available at
http://patchew.org/logs/cover.1619190878.git.sbrivio@redhat.com/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to patchew-devel@redhat.com

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-04-23 15:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-23 15:30 [PATCH 0/2] Support for AF_UNIX -netdev socket and a small fix Stefano Brivio
2021-04-23 15:30 ` [PATCH 1/2] net: Allow also UNIX domain sockets to be used as -netdev socket Stefano Brivio
2021-04-23 15:30 ` [PATCH 2/2] net: Don't ignore EINVAL on netdev socket connection Stefano Brivio
2021-04-23 15:39 ` [PATCH 0/2] Support for AF_UNIX -netdev socket and a small fix no-reply

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.