diff mbox series

[v8,07/14] net: stream: add unix socket

Message ID 20220913064000.79353-8-lvivier@redhat.com (mailing list archive)
State New, archived
Headers show
Series qapi: net: add unix socket type support to netdev backend | expand

Commit Message

Laurent Vivier Sept. 13, 2022, 6:39 a.m. UTC
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Reviewed-by: Stefano Brivio <sbrivio@redhat.com>
---
 net/stream.c    | 108 +++++++++++++++++++++++++++++++++++++++++++++---
 qapi/net.json   |   2 +-
 qemu-options.hx |   1 +
 3 files changed, 105 insertions(+), 6 deletions(-)

Comments

David Gibson Sept. 14, 2022, 5:16 a.m. UTC | #1
On Tue, Sep 13, 2022 at 08:39:53AM +0200, Laurent Vivier wrote:
> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
> Reviewed-by: Stefano Brivio <sbrivio@redhat.com>
> ---
>  net/stream.c    | 108 +++++++++++++++++++++++++++++++++++++++++++++---
>  qapi/net.json   |   2 +-
>  qemu-options.hx |   1 +
>  3 files changed, 105 insertions(+), 6 deletions(-)
> 
> diff --git a/net/stream.c b/net/stream.c
> index e8afbaca50b6..0f91ff20df61 100644
> --- a/net/stream.c
> +++ b/net/stream.c
> @@ -235,7 +235,7 @@ static NetStreamState *net_stream_fd_init_stream(NetClientState *peer,
>  static void net_stream_accept(void *opaque)
>  {
>      NetStreamState *s = opaque;
> -    struct sockaddr_in saddr;
> +    struct sockaddr_storage saddr;
>      socklen_t len;
>      int fd;
>  
> @@ -253,9 +253,27 @@ static void net_stream_accept(void *opaque)
>      s->fd = fd;
>      s->nc.link_down = false;
>      net_stream_connect(s);
> -    snprintf(s->nc.info_str, sizeof(s->nc.info_str),
> -             "connection from %s:%d",
> -             inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
> +    switch (saddr.ss_family) {
> +    case AF_INET: {
> +        struct sockaddr_in *saddr_in = (struct sockaddr_in *)&saddr;
> +
> +        snprintf(s->nc.info_str, sizeof(s->nc.info_str),
> +                 "connection from %s:%d",
> +                 inet_ntoa(saddr_in->sin_addr), ntohs(saddr_in->sin_port));
> +        break;
> +    }
> +    case AF_UNIX: {
> +        struct sockaddr_un saddr_un;
> +
> +        len = sizeof(saddr_un);
> +        getsockname(s->listen_fd, (struct sockaddr *)&saddr_un, &len);

It seems like there's an asymmetry here in that we've gotten the
connection address outside the switch for inet sockets, but we need an
additional getsockname() for unix sockets.  Am I missing something? 

> +        snprintf(s->nc.info_str, sizeof(s->nc.info_str),
> +                 "connect from %s", saddr_un.sun_path);
> +        break;
> +    }
> +    default:
> +        g_assert_not_reached();
> +    }
>  }
>  
>  static int net_stream_server_init(NetClientState *peer,
> @@ -295,6 +313,43 @@ static int net_stream_server_init(NetClientState *peer,
>          }
>          break;
>      }
> +    case SOCKET_ADDRESS_TYPE_UNIX: {
> +        struct sockaddr_un saddr_un;
> +
> +        ret = unlink(addr->u.q_unix.path);
> +        if (ret < 0 && errno != ENOENT) {
> +            error_setg_errno(errp, errno, "failed to unlink socket %s",
> +                             addr->u.q_unix.path);
> +            return -1;
> +        }
> +
> +        saddr_un.sun_family = PF_UNIX;
> +        ret = snprintf(saddr_un.sun_path, sizeof(saddr_un.sun_path), "%s",
> +                       addr->u.q_unix.path);
> +        if (ret < 0 || ret >= sizeof(saddr_un.sun_path)) {
> +            error_setg(errp, "UNIX socket path '%s' is too long",
> +                       addr->u.q_unix.path);
> +            error_append_hint(errp, "Path must be less than %zu bytes\n",
> +                              sizeof(saddr_un.sun_path));
> +            return -1;
> +        }
> +
> +        fd = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
> +        if (fd < 0) {
> +            error_setg_errno(errp, errno, "can't create stream socket");
> +            return -1;
> +        }
> +        qemu_socket_set_nonblock(fd);
> +
> +        ret = bind(fd, (struct sockaddr *)&saddr_un, sizeof(saddr_un));
> +        if (ret < 0) {
> +            error_setg_errno(errp, errno, "can't create socket with path: %s",
> +                             saddr_un.sun_path);
> +            closesocket(fd);
> +            return -1;
> +        }
> +        break;
> +    }
>      case SOCKET_ADDRESS_TYPE_FD:
>          fd = monitor_fd_param(monitor_cur(), addr->u.fd.str, errp);
>          if (fd == -1) {
> @@ -380,6 +435,49 @@ static int net_stream_client_init(NetClientState *peer,
>                                     ntohs(saddr_in.sin_port));
>          break;
>      }
> +    case SOCKET_ADDRESS_TYPE_UNIX: {
> +        struct sockaddr_un saddr_un;
> +
> +        saddr_un.sun_family = PF_UNIX;
> +        ret = snprintf(saddr_un.sun_path, sizeof(saddr_un.sun_path), "%s",
> +                       addr->u.q_unix.path);
> +        if (ret < 0 || ret >= sizeof(saddr_un.sun_path)) {
> +            error_setg(errp, "UNIX socket path '%s' is too long",
> +                       addr->u.q_unix.path);
> +            error_append_hint(errp, "Path must be less than %zu bytes\n",
> +                              sizeof(saddr_un.sun_path));
> +            return -1;
> +        }
> +
> +        fd = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
> +        if (fd < 0) {
> +            error_setg_errno(errp, errno, "can't create stream socket");
> +            return -1;
> +        }
> +        qemu_socket_set_nonblock(fd);
> +
> +        connected = 0;
> +        for (;;) {
> +            ret = connect(fd, (struct sockaddr *)&saddr_un, sizeof(saddr_un));
> +            if (ret < 0) {
> +                if (errno == EINTR || errno == EWOULDBLOCK) {
> +                    /* continue */
> +                } else if (errno == EAGAIN ||
> +                           errno == EALREADY) {
> +                    break;
> +                } else {
> +                    error_setg_errno(errp, errno, "can't connect socket");
> +                    closesocket(fd);
> +                    return -1;
> +                }
> +            } else {
> +                connected = 1;
> +                break;
> +            }
> +        }
> +        info_str = g_strdup_printf(" connect to %s", saddr_un.sun_path);
> +        break;
> +    }
>      case SOCKET_ADDRESS_TYPE_FD:
>          fd = monitor_fd_param(monitor_cur(), addr->u.fd.str, errp);
>          if (fd == -1) {
> @@ -395,7 +493,7 @@ static int net_stream_client_init(NetClientState *peer,
>          info_str = g_strdup_printf("connect to fd %d", fd);
>          break;
>      default:
> -        error_setg(errp, "only support inet or fd type");
> +        error_setg(errp, "only support inet, unix or fd type");
>          return -1;
>      }
>  
> diff --git a/qapi/net.json b/qapi/net.json
> index e02e8001a000..bb96701a49a7 100644
> --- a/qapi/net.json
> +++ b/qapi/net.json
> @@ -583,7 +583,7 @@
>  #        or connect to (server=false)
>  # @server: create server socket (default: true)
>  #
> -# Only SocketAddress types 'inet' and 'fd' are supported.
> +# Only SocketAddress types 'unix', 'inet' and 'fd' are supported.
>  #
>  # Since: 7.1
>  ##
> diff --git a/qemu-options.hx b/qemu-options.hx
> index bb16a61bae8e..8870bcce6bcd 100644
> --- a/qemu-options.hx
> +++ b/qemu-options.hx
> @@ -2733,6 +2733,7 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev,
>      "                configure a network backend to connect to another network\n"
>      "                using an UDP tunnel\n"
>      "-netdev stream,id=str[,server=on|off],addr.type=inet,addr.host=host,addr.port=port\n"
> +    "-netdev stream,id=str[,server=on|off],addr.type=unix,addr.path=path\n"
>      "-netdev stream,id=str[,server=on|off],addr.type=fd,addr.str=h\n"
>      "                configure a network backend to connect to another network\n"
>      "                using a socket connection in stream mode.\n"
Laurent Vivier Sept. 26, 2022, 12:15 p.m. UTC | #2
On 9/14/22 07:16, David Gibson wrote:
> On Tue, Sep 13, 2022 at 08:39:53AM +0200, Laurent Vivier wrote:
>> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
>> Reviewed-by: Stefano Brivio <sbrivio@redhat.com>
>> ---
>>   net/stream.c    | 108 +++++++++++++++++++++++++++++++++++++++++++++---
>>   qapi/net.json   |   2 +-
>>   qemu-options.hx |   1 +
>>   3 files changed, 105 insertions(+), 6 deletions(-)
>>
>> diff --git a/net/stream.c b/net/stream.c
>> index e8afbaca50b6..0f91ff20df61 100644
>> --- a/net/stream.c
>> +++ b/net/stream.c
>> @@ -235,7 +235,7 @@ static NetStreamState *net_stream_fd_init_stream(NetClientState *peer,
>>   static void net_stream_accept(void *opaque)
>>   {
>>       NetStreamState *s = opaque;
>> -    struct sockaddr_in saddr;
>> +    struct sockaddr_storage saddr;
>>       socklen_t len;
>>       int fd;
>>   
>> @@ -253,9 +253,27 @@ static void net_stream_accept(void *opaque)
>>       s->fd = fd;
>>       s->nc.link_down = false;
>>       net_stream_connect(s);
>> -    snprintf(s->nc.info_str, sizeof(s->nc.info_str),
>> -             "connection from %s:%d",
>> -             inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
>> +    switch (saddr.ss_family) {
>> +    case AF_INET: {
>> +        struct sockaddr_in *saddr_in = (struct sockaddr_in *)&saddr;
>> +
>> +        snprintf(s->nc.info_str, sizeof(s->nc.info_str),
>> +                 "connection from %s:%d",
>> +                 inet_ntoa(saddr_in->sin_addr), ntohs(saddr_in->sin_port));
>> +        break;
>> +    }
>> +    case AF_UNIX: {
>> +        struct sockaddr_un saddr_un;
>> +
>> +        len = sizeof(saddr_un);
>> +        getsockname(s->listen_fd, (struct sockaddr *)&saddr_un, &len);
> 
> It seems like there's an asymmetry here in that we've gotten the
> connection address outside the switch for inet sockets, but we need an
> additional getsockname() for unix sockets.  Am I missing something?
> 

This is because unix socket are not symmetric.

connect() returns the peer socket address.

With inet, server is waiting on, for instance, 127.0.0.1:1234 and peer address will appear 
to be something like 127.0.0.1:33946. So info_str will be set with "127.0.0.1:33946".

In case of unix, server is waiting on, for instance, /tmp/qemu0 and there is no peer 
address as the client connect to this same file. So the sun_path is empty when it returns 
from accept(). So to set the path of the socket (sun_path) in info_str I use the 
getsockname() to retrieve the server sun_path.

Thanks,
Laurent
diff mbox series

Patch

diff --git a/net/stream.c b/net/stream.c
index e8afbaca50b6..0f91ff20df61 100644
--- a/net/stream.c
+++ b/net/stream.c
@@ -235,7 +235,7 @@  static NetStreamState *net_stream_fd_init_stream(NetClientState *peer,
 static void net_stream_accept(void *opaque)
 {
     NetStreamState *s = opaque;
-    struct sockaddr_in saddr;
+    struct sockaddr_storage saddr;
     socklen_t len;
     int fd;
 
@@ -253,9 +253,27 @@  static void net_stream_accept(void *opaque)
     s->fd = fd;
     s->nc.link_down = false;
     net_stream_connect(s);
-    snprintf(s->nc.info_str, sizeof(s->nc.info_str),
-             "connection from %s:%d",
-             inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
+    switch (saddr.ss_family) {
+    case AF_INET: {
+        struct sockaddr_in *saddr_in = (struct sockaddr_in *)&saddr;
+
+        snprintf(s->nc.info_str, sizeof(s->nc.info_str),
+                 "connection from %s:%d",
+                 inet_ntoa(saddr_in->sin_addr), ntohs(saddr_in->sin_port));
+        break;
+    }
+    case AF_UNIX: {
+        struct sockaddr_un saddr_un;
+
+        len = sizeof(saddr_un);
+        getsockname(s->listen_fd, (struct sockaddr *)&saddr_un, &len);
+        snprintf(s->nc.info_str, sizeof(s->nc.info_str),
+                 "connect from %s", saddr_un.sun_path);
+        break;
+    }
+    default:
+        g_assert_not_reached();
+    }
 }
 
 static int net_stream_server_init(NetClientState *peer,
@@ -295,6 +313,43 @@  static int net_stream_server_init(NetClientState *peer,
         }
         break;
     }
+    case SOCKET_ADDRESS_TYPE_UNIX: {
+        struct sockaddr_un saddr_un;
+
+        ret = unlink(addr->u.q_unix.path);
+        if (ret < 0 && errno != ENOENT) {
+            error_setg_errno(errp, errno, "failed to unlink socket %s",
+                             addr->u.q_unix.path);
+            return -1;
+        }
+
+        saddr_un.sun_family = PF_UNIX;
+        ret = snprintf(saddr_un.sun_path, sizeof(saddr_un.sun_path), "%s",
+                       addr->u.q_unix.path);
+        if (ret < 0 || ret >= sizeof(saddr_un.sun_path)) {
+            error_setg(errp, "UNIX socket path '%s' is too long",
+                       addr->u.q_unix.path);
+            error_append_hint(errp, "Path must be less than %zu bytes\n",
+                              sizeof(saddr_un.sun_path));
+            return -1;
+        }
+
+        fd = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
+        if (fd < 0) {
+            error_setg_errno(errp, errno, "can't create stream socket");
+            return -1;
+        }
+        qemu_socket_set_nonblock(fd);
+
+        ret = bind(fd, (struct sockaddr *)&saddr_un, sizeof(saddr_un));
+        if (ret < 0) {
+            error_setg_errno(errp, errno, "can't create socket with path: %s",
+                             saddr_un.sun_path);
+            closesocket(fd);
+            return -1;
+        }
+        break;
+    }
     case SOCKET_ADDRESS_TYPE_FD:
         fd = monitor_fd_param(monitor_cur(), addr->u.fd.str, errp);
         if (fd == -1) {
@@ -380,6 +435,49 @@  static int net_stream_client_init(NetClientState *peer,
                                    ntohs(saddr_in.sin_port));
         break;
     }
+    case SOCKET_ADDRESS_TYPE_UNIX: {
+        struct sockaddr_un saddr_un;
+
+        saddr_un.sun_family = PF_UNIX;
+        ret = snprintf(saddr_un.sun_path, sizeof(saddr_un.sun_path), "%s",
+                       addr->u.q_unix.path);
+        if (ret < 0 || ret >= sizeof(saddr_un.sun_path)) {
+            error_setg(errp, "UNIX socket path '%s' is too long",
+                       addr->u.q_unix.path);
+            error_append_hint(errp, "Path must be less than %zu bytes\n",
+                              sizeof(saddr_un.sun_path));
+            return -1;
+        }
+
+        fd = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
+        if (fd < 0) {
+            error_setg_errno(errp, errno, "can't create stream socket");
+            return -1;
+        }
+        qemu_socket_set_nonblock(fd);
+
+        connected = 0;
+        for (;;) {
+            ret = connect(fd, (struct sockaddr *)&saddr_un, sizeof(saddr_un));
+            if (ret < 0) {
+                if (errno == EINTR || errno == EWOULDBLOCK) {
+                    /* continue */
+                } else if (errno == EAGAIN ||
+                           errno == EALREADY) {
+                    break;
+                } else {
+                    error_setg_errno(errp, errno, "can't connect socket");
+                    closesocket(fd);
+                    return -1;
+                }
+            } else {
+                connected = 1;
+                break;
+            }
+        }
+        info_str = g_strdup_printf(" connect to %s", saddr_un.sun_path);
+        break;
+    }
     case SOCKET_ADDRESS_TYPE_FD:
         fd = monitor_fd_param(monitor_cur(), addr->u.fd.str, errp);
         if (fd == -1) {
@@ -395,7 +493,7 @@  static int net_stream_client_init(NetClientState *peer,
         info_str = g_strdup_printf("connect to fd %d", fd);
         break;
     default:
-        error_setg(errp, "only support inet or fd type");
+        error_setg(errp, "only support inet, unix or fd type");
         return -1;
     }
 
diff --git a/qapi/net.json b/qapi/net.json
index e02e8001a000..bb96701a49a7 100644
--- a/qapi/net.json
+++ b/qapi/net.json
@@ -583,7 +583,7 @@ 
 #        or connect to (server=false)
 # @server: create server socket (default: true)
 #
-# Only SocketAddress types 'inet' and 'fd' are supported.
+# Only SocketAddress types 'unix', 'inet' and 'fd' are supported.
 #
 # Since: 7.1
 ##
diff --git a/qemu-options.hx b/qemu-options.hx
index bb16a61bae8e..8870bcce6bcd 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -2733,6 +2733,7 @@  DEF("netdev", HAS_ARG, QEMU_OPTION_netdev,
     "                configure a network backend to connect to another network\n"
     "                using an UDP tunnel\n"
     "-netdev stream,id=str[,server=on|off],addr.type=inet,addr.host=host,addr.port=port\n"
+    "-netdev stream,id=str[,server=on|off],addr.type=unix,addr.path=path\n"
     "-netdev stream,id=str[,server=on|off],addr.type=fd,addr.str=h\n"
     "                configure a network backend to connect to another network\n"
     "                using a socket connection in stream mode.\n"