Skip to contentSkip to conversation

qapi: net: add unix socket type support to netdev backend

RFC PATCH Superseded Laurent Vivier 4 participants 18 messages May 9, 2022 – May 11, 2022

Subsystems (2)

v1 of 14 Superseded Latest is v14 →

lore
Share & tools
lore
#0 lore raw

cover qapi: net: add unix socket type support to netdev backend

"-netdev socket" only supports inet sockets.

It's not a complex task to add support for unix sockets, but the socket netdev parameters are not defined to manage well unix socket parameters.

As discussed in:

"socket.c added support for unix domain socket datagram transport"
https://lore.kernel.org/qemu-devel/[email protected]/

This series adds support of unix socket type using SocketAddress QAPI structure.

A new netdev backend "socket-ng" is added, that is barely a copy of "socket" backend but it uses the SocketAddress QAPI to provide socket parameters. And then it also implement unix sockets (TCP and UDP).

Some examples of CLI syntax:

for TCP:

-netdev socket-ng,id=socket0,mode=server,addr.type=inet,addr.host=localhost,addr.port=1234 -netdev socket-ng,id=socket0,mode=client,addr.type=inet,addr.host=localhost,addr.port=1234

  -netdev socket-ng,id=socket0,mode=dgram,\
          local.type=inet,local.host=localhost,local.port=1234,\
          remote.type=inet,remote.host=localhost,remote.port=1235

for UNIX:

-netdev socket-ng,id=socket0,mode=server,addr.type=unix,addr.path=/tmp/qemu0 -netdev socket-ng,id=socket0,mode=client,addr.type=unix,addr.path=/tmp/qemu0

  -netdev socket-ng,id=socket0,mode=dgram,\
          local.type=unix,local.path=/tmp/qemu0,\
          remote.type=unix,remote.path=/tmp/qemu1

for FD:

-netdev socket-ng,id=socket0,mode=server,addr.type=fd,addr.str=4 -netdev socket-ng,id=socket0,mode=client,addr.type=fd,addr.str=5

-netdev socket-ng,id=socket0,mode=dgram,local.type=fd,addr.str=4

CC Ralph Schmieder <[email protected]>
CC Stefano Brivio <[email protected]>
CC Daniel P. Berrangé <[email protected]>
CC Markus Armbruster <[email protected]>

Laurent Vivier (6):
net: introduce convert_host_port()
qapi: net: add socket-ng netdev
net: socket-ng: add unix socket for server and client mode.
net: socket-ng: make dgram_dst generic
net: socket-ng: move mcast specific code from
net_socket_fd_init_dgram()
net: socket-ng: add unix socket for dgram mode

 hmp-commands.hx        |    2 +-
 include/qemu/sockets.h |    2 +
 net/clients.h          |    3 +
 net/hub.c              |    1 +
 net/meson.build        |    1 +
 net/net.c              |  123 +++--
 net/socket-ng.c        | 1060 ++++++++++++++++++++++++++++++++++++++++
 qapi/net.json          |   41 +-
 8 files changed, 1200 insertions(+), 33 deletions(-)
 create mode 100644 net/socket-ng.c
Signature · 1 line
2.35.3
#1 lore raw

RFC PATCH 1/6 net: introduce convert_host_port()

---
 include/qemu/sockets.h |  2 ++
 net/net.c              | 62 ++++++++++++++++++++++--------------------
 2 files changed, 34 insertions(+), 30 deletions(-)
include/qemu/sockets.h +2 -0
index 038faa157f59..47194b9732f8 100644
@@ -47,6 +47,8 @@ void socket_listen_cleanup(int fd, Error **errp);
 int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp);
 
 /* Old, ipv4 only bits.  Don't use for new code. */
+int convert_host_port(struct sockaddr_in *saddr, const char *host,
+                      const char *port, Error **errp);
 int parse_host_port(struct sockaddr_in *saddr, const char *str,
                     Error **errp);
 int socket_init(void);
net/net.c +32 -30

87 lines not on this page · raw message

Signature · 1 line
2.35.3
#2 lore raw

Re: [RFC PATCH 1/6] net: introduce convert_host_port()

Quoted 51 lines · On Mon, 9 May 2022 19:36:13 +0200 Laurent Vivier <[email protected]> wrote:
---
 include/qemu/sockets.h |  2 ++
 net/net.c              | 62 ++++++++++++++++++++++--------------------
 2 files changed, 34 insertions(+), 30 deletions(-)
include/qemu/sockets.h +2 -0
index 038faa157f59..47194b9732f8 100644
@@ -47,6 +47,8 @@ void socket_listen_cleanup(int fd, Error **errp);
 int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp);
 
 /* Old, ipv4 only bits.  Don't use for new code. */
+int convert_host_port(struct sockaddr_in *saddr, const char *host,
+                      const char *port, Error **errp);
 int parse_host_port(struct sockaddr_in *saddr, const char *str,
                     Error **errp);
 int socket_init(void);
net/net.c +4 -15
index a094cf1d2929..58c05c200622 100644
@@ -66,55 +66,57 @@ static QTAILQ_HEAD(, NetClientState) net_clients;
 /***********************************************************/
 /* network device redirectors */
 
-int parse_host_port(struct sockaddr_in *saddr, const char *str,
-                    Error **errp)
+int convert_host_port(struct sockaddr_in *saddr, const char *host,
+                      const char *port, Error **errp)
 {
-    gchar **substrings;
     struct hostent *he;
-    const char *addr, *p, *r;
-    int port, ret = 0;
+    const char *r;
+    long p;
 
     memset(saddr, 0, sizeof(*saddr));
 
-    substrings = g_strsplit(str, ":", 2);
-    if (!substrings || !substrings[0] || !substrings[1]) {
-        error_setg(errp, "host address '%s' doesn't contain ':' "
-                   "separating host from port", str);
-        ret = -1;
-        goto out;
-    }
-
-    addr = substrings[0];
-    p = substrings[1];

net/net.c static QTAILQ_HEAD(, NetClientState) net_clients; · line 83

Quoted 10 lines
net/net.c +3 -4
-
     saddr->sin_family = AF_INET;
-    if (addr[0] == '\0') {
+    if (host[0] == '\0') {
         saddr->sin_addr.s_addr = 0;
     } else {
-        if (qemu_isdigit(addr[0])) {
-            if (!inet_aton(addr, &saddr->sin_addr)) {
+        if (qemu_isdigit(host[0])) {
+            if (!inet_aton(host, &saddr->sin_addr)) {

I was about to observe that this doesn't support IPv6 addresses (which I guess we'd like to have always in the RFC 3986 form "[address]:port", as the port is mandatory here), and to propose a small change to bridge this gap.

Then I realised this is (partially) using GLib, so maybe we want to use g_network_address_parse() which would, however, give us a GSocketConnectable object.

At that point, do we want to pass the GsocketConnectable thing around, or stick to struct sockaddr_in{,6}, filling it here from what GLib gives us?

Signature · 1 line
Stefano
#3 lore raw

Re: [RFC PATCH 1/6] net: introduce convert_host_port()

Quoted 70 lines · Stefano Brivio
Quoted 51 lines · On Mon, 9 May 2022 19:36:13 +0200 Laurent Vivier <[email protected]> wrote:
---
  include/qemu/sockets.h |  2 ++
  net/net.c              | 62 ++++++++++++++++++++++--------------------
  2 files changed, 34 insertions(+), 30 deletions(-)
include/qemu/sockets.h +2 -0
index 038faa157f59..47194b9732f8 100644
@@ -47,6 +47,8 @@ void socket_listen_cleanup(int fd, Error **errp);
  int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp);
 
  /* Old, ipv4 only bits.  Don't use for new code. */
+int convert_host_port(struct sockaddr_in *saddr, const char *host,
+                      const char *port, Error **errp);
  int parse_host_port(struct sockaddr_in *saddr, const char *str,
                      Error **errp);
  int socket_init(void);
net/net.c +4 -15
index a094cf1d2929..58c05c200622 100644
@@ -66,55 +66,57 @@ static QTAILQ_HEAD(, NetClientState) net_clients;
  /***********************************************************/
  /* network device redirectors */
 
-int parse_host_port(struct sockaddr_in *saddr, const char *str,
-                    Error **errp)
+int convert_host_port(struct sockaddr_in *saddr, const char *host,
+                      const char *port, Error **errp)
  {
-    gchar **substrings;
      struct hostent *he;
-    const char *addr, *p, *r;
-    int port, ret = 0;
+    const char *r;
+    long p;
 
      memset(saddr, 0, sizeof(*saddr));
 
-    substrings = g_strsplit(str, ":", 2);
-    if (!substrings || !substrings[0] || !substrings[1]) {
-        error_setg(errp, "host address '%s' doesn't contain ':' "
-                   "separating host from port", str);
-        ret = -1;
-        goto out;
-    }
-
-    addr = substrings[0];
-    p = substrings[1];

net/net.c static QTAILQ_HEAD(, NetClientState) net_clients; · line 83

Quoted 10 lines
net/net.c +3 -4
-
      saddr->sin_family = AF_INET;
-    if (addr[0] == '\0') {
+    if (host[0] == '\0') {
          saddr->sin_addr.s_addr = 0;
      } else {
-        if (qemu_isdigit(addr[0])) {
-            if (!inet_aton(addr, &saddr->sin_addr)) {
+        if (qemu_isdigit(host[0])) {
+            if (!inet_aton(host, &saddr->sin_addr)) {

I was about to observe that this doesn't support IPv6 addresses (which I guess we'd like to have always in the RFC 3986 form "[address]:port", as the port is mandatory here), and to propose a small change to bridge this gap.

Quoted 8 lines

Then I realised this is (partially) using GLib, so maybe we want to use g_network_address_parse() which would, however, give us a GSocketConnectable object.

At that point, do we want to pass the GsocketConnectable thing around, or stick to struct sockaddr_in{,6}, filling it here from what GLib gives us?

For the new netdev, we should better switch to util/qemu-sockets.c functions that take directly a SocketAddress (see socket_connect()).

Thanks,
Laurent

#4 lore raw

RFC PATCH 2/6 qapi: net: add socket-ng netdev

In reply to Laurent Vivier

Copied from socket netdev file and modified to use SocketAddress to be able to introduce new features like unix socket.

"udp" and "mcast" are squashed into dgram, multicast is detected according to the IP address type.
"listen" and "connect" modes are changed to "server" and "client".

As qemu_opts_parse_noisily() flattens the QAPI structures ("type" field of Netdev structure collides with "type" field of SocketAddress), we detect socket-ng backend and use directly visit_type_Netdev() to parse the backend parameters.

net: socket-ng: replace mode=unicast/multicast by mode=dgram

The multicast mode is detected according to the remote address type.

---
 hmp-commands.hx |   2 +-
 net/clients.h   |   3 +
 net/hub.c       |   1 +
 net/meson.build |   1 +
 net/net.c       |  61 ++++
 net/socket-ng.c | 890 ++++++++++++++++++++++++++++++++++++++++++++++++
 qapi/net.json   |  41 ++-
 7 files changed, 996 insertions(+), 3 deletions(-)
 create mode 100644 net/socket-ng.c
hmp-commands.hx +1 -1
index 03e6a73d1f55..74f7c9585ac3 100644
@@ -1269,7 +1269,7 @@ ERST
     {
         .name       = "netdev_add",
         .args_type  = "netdev:O",
-        .params     = "[user|tap|socket|vde|bridge|hubport|netmap|vhost-user],id=str[,prop=value][,...]",
+        .params     = "[user|tap|socket|socket-ng|vde|bridge|hubport|netmap|vhost-user],id=str[,prop=value][,...]",
         .help       = "add host network device",
         .cmd        = hmp_netdev_add,
         .command_completion = netdev_add_completion,
net/clients.h +3 -0
index 92f9b59aedce..8c71a3ba8e97 100644
@@ -40,6 +40,9 @@ int net_init_hubport(const Netdev *netdev, const char *name,
 int net_init_socket(const Netdev *netdev, const char *name,
                     NetClientState *peer, Error **errp);
 
+int net_init_socket_ng(const Netdev *netdev, const char *name,
+                       NetClientState *peer, Error **errp);
+
 int net_init_tap(const Netdev *netdev, const char *name,
                  NetClientState *peer, Error **errp);
 
net/hub.c +1 -0
index 1375738bf121..8f9607bfe3dc 100644
@@ -313,6 +313,7 @@ void net_hub_check_clients(void)
             case NET_CLIENT_DRIVER_USER:
             case NET_CLIENT_DRIVER_TAP:
             case NET_CLIENT_DRIVER_SOCKET:
+            case NET_CLIENT_DRIVER_SOCKET_NG:
             case NET_CLIENT_DRIVER_VDE:
             case NET_CLIENT_DRIVER_VHOST_USER:
                 has_host_dev = 1;
net/meson.build +1 -0
index c965e83b264b..92a8cde119d2 100644
@@ -13,6 +13,7 @@ softmmu_ss.add(files(
   'net.c',
   'queue.c',
   'socket.c',
+  'socket-ng.c',
   'util.c',
 ))
 
net/net.c +61 -0

99 lines not on this page · raw message

net/socket-ng.c new file +890 -0

890 lines not on this page · raw message

qapi/net.json +39 -2

65 lines not on this page · raw message

Signature · 1 line
2.35.3
#5 lore raw

Re: [RFC PATCH 2/6] qapi: net: add socket-ng netdev

Quoted 58 lines · On Mon, 9 May 2022 19:36:14 +0200 Laurent Vivier <[email protected]> wrote:

Copied from socket netdev file and modified to use SocketAddress to be able to introduce new features like unix socket.

"udp" and "mcast" are squashed into dgram, multicast is detected according to the IP address type.
"listen" and "connect" modes are changed to "server" and "client".

As qemu_opts_parse_noisily() flattens the QAPI structures ("type" field of Netdev structure collides with "type" field of SocketAddress), we detect socket-ng backend and use directly visit_type_Netdev() to parse the backend parameters.

net: socket-ng: replace mode=unicast/multicast by mode=dgram

The multicast mode is detected according to the remote address type.

---
 hmp-commands.hx |   2 +-
 net/clients.h   |   3 +
 net/hub.c       |   1 +
 net/meson.build |   1 +
 net/net.c       |  61 ++++
 net/socket-ng.c | 890 ++++++++++++++++++++++++++++++++++++++++++++++++
 qapi/net.json   |  41 ++-
 7 files changed, 996 insertions(+), 3 deletions(-)
 create mode 100644 net/socket-ng.c

[...]

net/socket-ng.c new file +25 -0
+static int net_socketng_connect_init(NetClientState *peer,
+                                   const char *model,
+                                   const char *name,
+                                   SocketAddress *addr,
+                                   Error **errp)
+{
+    NetSocketNGState *s;
+    int fd, connected, ret;
+    gchar *info_str;
+
+    switch (addr->type) {
+    case SOCKET_ADDRESS_TYPE_INET: {
+        struct sockaddr_in saddr_in;
+
+        if (convert_host_port(&saddr_in, addr->u.inet.host, addr->u.inet.port,
+                              errp) < 0) {
+            return -1;
+        }
+
+        fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
+        if (fd < 0) {
+            error_setg_errno(errp, errno, "can't create stream socket");
+            return -1;
+        }
+        qemu_socket_set_nonblock(fd);

net/socket-ng.c line 598

Quoted 10 lines
net/socket-ng.c new file +10 -0
+
+        connected = 0;
+        for (;;) {
+            ret = connect(fd, (struct sockaddr *)&saddr_in, sizeof(saddr_in));
+            if (ret < 0) {
+                if (errno == EINTR || errno == EWOULDBLOCK) {
+                    /* continue */
+                } else if (errno == EINPROGRESS ||
+                           errno == EALREADY ||
+                           errno == EINVAL) {

I guess we should report failure and close the socket on EINVAL, there are no chances the connection will eventually succeed. I actually proposed this change (after some debugging frustration) in my quick and dirty series to get AF_UNIX sockets working:

	https://lore.kernel.org/all/a6d475147682de1fe3b14eb325f4247e013e8440.1619190878.git.sbrivio@redhat.com/
Quoted 63 lines
net/socket-ng.c new file +6 -0
+                    break;
+                } else {
+                    error_setg_errno(errp, errno, "can't connect socket");
+                    closesocket(fd);
+                    return -1;
+                }
 
[...]
qapi/net.json +35 -0
index b92f3f5fb444..2b31101e6641 100644
@@ -7,6 +7,7 @@
 ##
 
 { 'include': 'common.json' }
+{ 'include': 'sockets.json' }
 
 ##
 # @set_link:
@@ -452,6 +453,40 @@
     '*vhostdev':     'str',
     '*queues':       'int' } }
 
+##
+# @NetdevSocketNGMode:
+#
+# @dgram: UDP mode
+#
+# @server: TCP server mode
+#
+# @client: TCP client mode
+#
+# Legacy NetdevSocketOptions only accepts one of:
+# "fd", "udp", "mcast" and "udp".
+# we map:
+#   "udp", "mcast" -> "dgram"
+#   "listen"       -> "server"
+#   "connect"      -> "client"
+#
+# Since: 7.1
+#
+##
+{ 'enum': 'NetdevSocketNGMode',
+  'data':  [ 'dgram', 'server', 'client' ] }
+
+##
+# @NetdevSocketNGOptions:
+#
+# Since: 7.1
+##
+{ 'struct': 'NetdevSocketNGOptions',
+  'data': {
+    'mode':    'NetdevSocketNGMode',
+    '*addr':   'SocketAddress',
+    '*remote': 'SocketAddress',
+    '*local':  'SocketAddress' } }
+
 ##
 # @NetClientDriver:
 #

qapi/net.json line 502

Quoted 6 lines
qapi/net.json +2 -1
@@ -463,7 +498,8 @@
 ##
 { 'enum': 'NetClientDriver',
   'data': [ 'none', 'nic', 'user', 'tap', 'l2tpv3', 'socket', 'vde',
-            'bridge', 'hubport', 'netmap', 'vhost-user', 'vhost-vdpa' ] }
+            'bridge', 'hubport', 'netmap', 'vhost-user', 'vhost-vdpa',
+            'socket-ng' ] }

I don't know if this is an issue, but I couldn't figure out the reason for this difference either:

- with the old socket option, I can pass something like:

-net socket,fd=5 -net nic,model=virtio

and frames are sent to/received from socket number 5 (which I pre-opened)

- with the new option, and something like:

-netdev socket-ng,id=socket0,mode=client,addr.type=unix,addr.path=/tmp/test.socket -net nic,model=virtio,id=hostnet0

I get a connection on the socket, but the virtio-net device is not connected to it (no frames received/sent).

However, if instead of "-net nic" I pass this:

-device virtio-net-pci,netdev=socket0

everything works.

Signature · 1 line
Stefano
#6 lore raw

Re: [RFC PATCH 2/6] qapi: net: add socket-ng netdev

On 10/05/2022 23:24, Stefano Brivio wrote:
...

Quoted 14 lines

I don't know if this is an issue, but I couldn't figure out the reason for this difference either:

- with the old socket option, I can pass something like:

-net socket,fd=5 -net nic,model=virtio

and frames are sent to/received from socket number 5 (which I pre-opened)

- with the new option, and something like:

-netdev socket-ng,id=socket0,mode=client,addr.type=unix,addr.path=/tmp/test.socket -net nic,model=virtio,id=hostnet0

Quoted 9 lines

I get a connection on the socket, but the virtio-net device is not connected to it (no frames received/sent).

However, if instead of "-net nic" I pass this:

-device virtio-net-pci,netdev=socket0

everything works.

I think the problem is with the hub.

When it works:

    -net socket,udp=localhost:1235,localaddr=localhost:1234 \
    -net nic,model=virtio,macaddr=9a:2b:2c:2d:2e:2f

    (qemu) info network
    hub 0
     \ hub0port1: virtio-net-pci.0: 
index=0,type=nic,model=virtio-net-pci,macaddr=9a:2b:2c:2d:2e:2f
     \ hub0port0: #net039: index=0,type=socket,socket: udp=127.0.0.1:1235

When it doesn't work (with the upcoming new syntax...):

    -net dgram,id=socket0,local.type=inet,local.host=localhost,local.port=1234,\
                          remote.type=inet,remote.host=localhost,remote.port=1235
    -net nic,model=virtio,macaddr=9a:2b:2c:2d:2e:2f

    (qemu) info network
    hub 0
     \ hub0port0: virtio-net-pci.0: 
index=0,type=nic,model=virtio-net-pci,macaddr=9a:2b:2c:2d:2e:2f
    socket0: index=0,type=dgram,udp=127.0.0.1:1234/127.0.0.1:1235

We can see socket0 is not linked to the hub (hub0port0)

With "-device" it works because it doesn't use the hub but the netdev id:

    -device virtio-net,mac=9a:2b:2c:2d:2e:2f,netdev=socket0

    virtio-net-pci.0: index=0,type=nic,model=virtio-net-pci,macaddr=9a:2b:2c:2d:2e:f
     \ socket0: index=0,type=dgram,udp=127.0.0.1:1234/127.0.0.1:1235

I'm investigating...

Thanks,
Laurent

#7 lore raw

RFC PATCH 3/6 net: socket-ng: add unix socket for server and client mode.

In reply to Laurent Vivier
---
 net/socket-ng.c | 109 ++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 101 insertions(+), 8 deletions(-)
net/socket-ng.c +101 -8
index 0876a4930389..2c70440a2b57 100644
@@ -382,7 +382,6 @@ static NetSocketNGState *net_socket_fd_init_dgram(NetClientState *peer,
             /* clone newfd to fd, close newfd */
             dup2(newfd, fd);
             close(newfd);
-
     }
 
     nc = qemu_new_net_client(&net_dgram_socket_info, peer, model, name);
@@ -463,7 +462,7 @@ static NetSocketNGState *net_socket_fd_init_stream(NetClientState *peer,
 static void net_socket_accept(void *opaque)
 {
     NetSocketNGState *s = opaque;
-    struct sockaddr_in saddr;
+    struct sockaddr_storage saddr;
     socklen_t len;
     int fd;
 
@@ -481,9 +480,27 @@ static void net_socket_accept(void *opaque)
     s->fd = fd;
     s->nc.link_down = false;
     net_socket_connect(s);
-    snprintf(s->nc.info_str, sizeof(s->nc.info_str),
-             "socket-ng: 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),
+                 "socket-ng: 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),
+                 "socket-ng: connect from %s", saddr_un.sun_path);
+        break;
+    }
+    default:
+        g_assert_not_reached();
+    }
 }
 
 static int net_socketng_listen_init(NetClientState *peer,
@@ -524,8 +541,40 @@ static int net_socketng_listen_init(NetClientState *peer,
         break;
     }
     case SOCKET_ADDRESS_TYPE_UNIX: {
-        error_setg(errp, "only support inet type");
-        return -1;
+        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));
+        }
+
+        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);
@@ -612,6 +661,50 @@ static int net_socketng_connect_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));
+        }
+
+        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 ||
+                           errno == EINVAL) {
+                    break;
+                } else {
+                    error_setg_errno(errp, errno, "can't connect socket");
+                    closesocket(fd);
+                    return -1;
+                }
+            } else {
+                connected = 1;
+                break;
+            }
+        }
+        info_str = g_strdup_printf("socket-ng: 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) {
@@ -627,7 +720,7 @@ static int net_socketng_connect_init(NetClientState *peer,
         info_str = g_strdup_printf("socket-ng: 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;
     }
 
Signature · 1 line
2.35.3
#8 lore raw

RFC PATCH 4/6 net: socket-ng: make dgram_dst generic

In reply to Laurent Vivier

dgram_dst is a sockaddr_in structure. To be able to use it with unix socket, use a pointer to a generic sockaddr structure.

---
 net/socket-ng.c | 76 ++++++++++++++++++++++++++++++-------------------
 1 file changed, 46 insertions(+), 30 deletions(-)
net/socket-ng.c +46 -30
index 2c70440a2b57..0056924dc02b 100644
@@ -40,8 +40,8 @@ typedef struct NetSocketNGState {
     int fd;
     SocketReadState rs;
     unsigned int send_index;      /* number of bytes sent (only SOCK_STREAM) */
-  /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
-    struct sockaddr_in dgram_dst;
+                     /* contains destination iff connectionless (SOCK_DGRAM) */
+    struct sockaddr *dgram_dst;
     IOHandler *send_fn;           /* differs between SOCK_STREAM/SOCK_DGRAM */
     bool read_poll;               /* waiting to receive data? */
     bool write_poll;              /* waiting to transmit data? */
@@ -122,10 +122,9 @@ static ssize_t net_socket_receive_dgram(NetClientState *nc,
     ssize_t ret;
 
     do {
-        if (s->dgram_dst.sin_family != AF_UNIX) {
-            ret = sendto(s->fd, buf, size, 0,
-                         (struct sockaddr *)&s->dgram_dst,
-                         sizeof(s->dgram_dst));
+        if (s->dgram_dst) {
+            ret = sendto(s->fd, buf, size, 0, s->dgram_dst,
+                         sizeof(struct sockaddr_in));
         } else {
             ret = send(s->fd, buf, size, 0);
         }
@@ -327,6 +326,8 @@ static void net_socket_cleanup(NetClientState *nc)
         closesocket(s->listen_fd);
         s->listen_fd = -1;
     }
+    g_free(s->dgram_dst);
+    s->dgram_dst = NULL;
 }
 
 static NetClientInfo net_dgram_socket_info = {
@@ -343,7 +344,7 @@ static NetSocketNGState *net_socket_fd_init_dgram(NetClientState *peer,
                                                 SocketAddress *mcast,
                                                 Error **errp)
 {
-    struct sockaddr_in saddr;
+    struct sockaddr_in *saddr = NULL;
     int newfd;
     NetClientState *nc;
     NetSocketNGState *s;
@@ -365,17 +366,19 @@ static NetSocketNGState *net_socket_fd_init_dgram(NetClientState *peer,
      */
 
     if (is_fd && mcast != NULL) {
-            if (convert_host_port(&saddr, mcast->u.inet.host,
-                                  mcast->u.inet.port, errp) < 0) {
+            saddr = g_new(struct sockaddr_in, 1);
+
+            if (convert_host_port(saddr, mcast->u.inet.host, mcast->u.inet.port,
+                                  errp) < 0) {
                 goto err;
             }
             /* must be bound */
-            if (saddr.sin_addr.s_addr == 0) {
+            if (saddr->sin_addr.s_addr == 0) {
                 error_setg(errp, "can't setup multicast destination address");
                 goto err;
             }
             /* clone dgram socket */
-            newfd = net_socket_mcast_create(&saddr, NULL, errp);
+            newfd = net_socket_mcast_create(saddr, NULL, errp);
             if (newfd < 0) {
                 goto err;
             }
@@ -395,16 +398,13 @@ static NetSocketNGState *net_socket_fd_init_dgram(NetClientState *peer,
     net_socket_read_poll(s, true);
 
     /* mcast: save bound address as dst */
-    if (is_fd && mcast != NULL) {
-        s->dgram_dst = saddr;
+    if (saddr) {
+        g_assert(s->dgram_dst == NULL);
+        s->dgram_dst = (struct sockaddr *)saddr;
         snprintf(nc->info_str, sizeof(nc->info_str),
                  "socket-ng: fd=%d (cloned mcast=%s:%d)",
-                 fd, inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
+                 fd, inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
     } else {
-        if (sa_type == SOCKET_ADDRESS_TYPE_UNIX) {
-            s->dgram_dst.sin_family = AF_UNIX;
-        }
-
         snprintf(nc->info_str, sizeof(nc->info_str),
                  "socket-ng: fd=%d %s", fd, SocketAddressType_str(sa_type));
     }
@@ -412,6 +412,7 @@ static NetSocketNGState *net_socket_fd_init_dgram(NetClientState *peer,
     return s;
 
 err:
+    g_free(saddr);
     closesocket(fd);
     return NULL;
 }
@@ -741,21 +742,24 @@ static int net_socketng_mcast_init(NetClientState *peer,
 {
     NetSocketNGState *s;
     int fd, ret;
-    struct sockaddr_in saddr;
+    struct sockaddr_in *saddr;
 
     if (remote->type != SOCKET_ADDRESS_TYPE_INET) {
         error_setg(errp, "multicast only support inet type");
         return -1;
     }
 
-    if (convert_host_port(&saddr, remote->u.inet.host, remote->u.inet.port,
+    saddr = g_new(struct sockaddr_in, 1);
+    if (convert_host_port(saddr, remote->u.inet.host, remote->u.inet.port,
                           errp) < 0) {
+        g_free(saddr);
         return -1;
     }
 
     if (!local) {
-        fd = net_socket_mcast_create(&saddr, NULL, errp);
+        fd = net_socket_mcast_create(saddr, NULL, errp);
         if (fd < 0) {
+            g_free(saddr);
             return -1;
         }
     } else {
@@ -764,13 +768,15 @@ static int net_socketng_mcast_init(NetClientState *peer,
             struct in_addr localaddr;
 
             if (inet_aton(local->u.inet.host, &localaddr) == 0) {
+                g_free(saddr);
                 error_setg(errp, "localaddr '%s' is not a valid IPv4 address",
                            local->u.inet.host);
                 return -1;
             }
 
-            fd = net_socket_mcast_create(&saddr, &localaddr, errp);
+            fd = net_socket_mcast_create(saddr, &localaddr, errp);
             if (fd < 0) {
+                g_free(saddr);
                 return -1;
             }
             break;
@@ -778,16 +784,19 @@ static int net_socketng_mcast_init(NetClientState *peer,
         case SOCKET_ADDRESS_TYPE_FD:
             fd = monitor_fd_param(monitor_cur(), local->u.fd.str, errp);
             if (fd == -1) {
+                g_free(saddr);
                 return -1;
             }
             ret = qemu_socket_try_set_nonblock(fd);
             if (ret < 0) {
+                g_free(saddr);
                 error_setg_errno(errp, -ret, "%s: Can't use file descriptor %d",
                                  name, fd);
                 return -1;
             }
             break;
         default:
+            g_free(saddr);
             error_setg(errp, "only support inet or fd type for local");
             return -1;
         }
@@ -797,14 +806,17 @@ static int net_socketng_mcast_init(NetClientState *peer,
                                  local->type == SOCKET_ADDRESS_TYPE_FD,
                                  remote, errp);
     if (!s) {
+        g_free(saddr);
         return -1;
     }
 
-    s->dgram_dst = saddr;
+    g_assert(s->dgram_dst == NULL);
+    s->dgram_dst = (struct sockaddr *)saddr;
 
     snprintf(s->nc.info_str, sizeof(s->nc.info_str),
              "socket-ng: mcast=%s:%d",
-             inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
+             inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
+
     return 0;
 
 }
@@ -818,8 +830,8 @@ static int net_socketng_udp_init(NetClientState *peer,
 {
     NetSocketNGState *s;
     int fd, ret;
-    struct sockaddr_in raddr_in;
     gchar *info_str;
+    struct sockaddr *dgram_dst;
 
     if (remote) {
         if (local->type == SOCKET_ADDRESS_TYPE_FD) {
@@ -840,7 +852,7 @@ static int net_socketng_udp_init(NetClientState *peer,
 
     switch (local->type) {
     case SOCKET_ADDRESS_TYPE_INET: {
-        struct sockaddr_in laddr_in;
+        struct sockaddr_in laddr_in, raddr_in;
 
         if (convert_host_port(&laddr_in, local->u.inet.host, local->u.inet.port,
                               errp) < 0) {
@@ -874,9 +886,12 @@ static int net_socketng_udp_init(NetClientState *peer,
         }
         qemu_socket_set_nonblock(fd);
 
+        dgram_dst = g_malloc(sizeof(raddr_in));
+        memcpy(dgram_dst, &raddr_in, sizeof(raddr_in));
+
         info_str = g_strdup_printf("socket-ng: udp=%s:%d/%s:%d",
-                 inet_ntoa(laddr_in.sin_addr), ntohs(laddr_in.sin_port),
-                 inet_ntoa(raddr_in.sin_addr), ntohs(raddr_in.sin_port));
+                        inet_ntoa(laddr_in.sin_addr), ntohs(laddr_in.sin_port),
+                        inet_ntoa(raddr_in.sin_addr), ntohs(raddr_in.sin_port));
 
         break;
     }
@@ -903,13 +918,14 @@ static int net_socketng_udp_init(NetClientState *peer,
     }
 
     if (remote) {
-        s->dgram_dst = raddr_in;
+        g_assert(s->dgram_dst == NULL);
+        s->dgram_dst = dgram_dst;
 
         pstrcpy(s->nc.info_str, sizeof(s->nc.info_str), info_str);
         g_free(info_str);
     }
     return 0;
-}
+};
 
 static int net_socketng_dgram_init(NetClientState *peer,
                                  const char *model,
Signature · 1 line
2.35.3
#9 lore raw

Re: [RFC PATCH 4/6] net: socket-ng: make dgram_dst generic

Quoted 19 lines · On Mon, 9 May 2022 19:36:16 +0200 Laurent Vivier <[email protected]> wrote:

dgram_dst is a sockaddr_in structure. To be able to use it with unix socket, use a pointer to a generic sockaddr structure.

---
 net/socket-ng.c | 76 ++++++++++++++++++++++++++++++-------------------
 1 file changed, 46 insertions(+), 30 deletions(-)
net/socket-ng.c +0 -0
index 2c70440a2b57..0056924dc02b 100644
 
[...]
@@ -903,13 +918,14 @@ static int net_socketng_udp_init(NetClientState *peer,
     }
 
     if (remote) {

net/socket-ng.c static int net_socketng_udp_init(NetClientState *peer, · line 928

Quoted 10 lines
net/socket-ng.c +3 -2
-        s->dgram_dst = raddr_in;
+        g_assert(s->dgram_dst == NULL);
+        s->dgram_dst = dgram_dst;
 
         pstrcpy(s->nc.info_str, sizeof(s->nc.info_str), info_str);
         g_free(info_str);
     }
     return 0;
-}
+};

Stray semicolon (I guess not reported by gcc without -pedantic).

Signature · 1 line
Stefano
#10 lore raw

RFC PATCH 5/6 net: socket-ng: move mcast specific code from net_socket_fd_init_dgram()

In reply to Laurent Vivier

It is less complex to manage special cases directly in net_socketng_mcast_init() and net_socketng_udp_init().

---
 net/socket-ng.c | 144 ++++++++++++++++++++++++------------------------
 1 file changed, 73 insertions(+), 71 deletions(-)
net/socket-ng.c +73 -71
index 0056924dc02b..aabdd0eed381 100644
@@ -340,52 +340,11 @@ static NetClientInfo net_dgram_socket_info = {
 static NetSocketNGState *net_socket_fd_init_dgram(NetClientState *peer,
                                                 const char *model,
                                                 const char *name,
-                                                int fd, int is_fd,
-                                                SocketAddress *mcast,
+                                                int fd,
                                                 Error **errp)
 {
-    struct sockaddr_in *saddr = NULL;
-    int newfd;
     NetClientState *nc;
     NetSocketNGState *s;
-    SocketAddress *sa;
-    SocketAddressType sa_type;
-
-    sa = socket_local_address(fd, errp);
-    if (!sa) {
-        return NULL;
-    }
-    sa_type = sa->type;
-    qapi_free_SocketAddress(sa);
-
-    /*
-     * fd passed: multicast: "learn" dgram_dst address from bound address and
-     * save it. Because this may be "shared" socket from a "master" process,
-     * datagrams would be recv() by ONLY ONE process: we must "clone" this
-     * dgram socket --jjo
-     */
-
-    if (is_fd && mcast != NULL) {
-            saddr = g_new(struct sockaddr_in, 1);
-
-            if (convert_host_port(saddr, mcast->u.inet.host, mcast->u.inet.port,
-                                  errp) < 0) {
-                goto err;
-            }
-            /* must be bound */
-            if (saddr->sin_addr.s_addr == 0) {
-                error_setg(errp, "can't setup multicast destination address");
-                goto err;
-            }
-            /* clone dgram socket */
-            newfd = net_socket_mcast_create(saddr, NULL, errp);
-            if (newfd < 0) {
-                goto err;
-            }
-            /* clone newfd to fd, close newfd */
-            dup2(newfd, fd);
-            close(newfd);
-    }
 
     nc = qemu_new_net_client(&net_dgram_socket_info, peer, model, name);
 
@@ -397,24 +356,7 @@ static NetSocketNGState *net_socket_fd_init_dgram(NetClientState *peer,
     net_socket_rs_init(&s->rs, net_socket_rs_finalize, false);
     net_socket_read_poll(s, true);
 
-    /* mcast: save bound address as dst */
-    if (saddr) {
-        g_assert(s->dgram_dst == NULL);
-        s->dgram_dst = (struct sockaddr *)saddr;
-        snprintf(nc->info_str, sizeof(nc->info_str),
-                 "socket-ng: fd=%d (cloned mcast=%s:%d)",
-                 fd, inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
-    } else {
-        snprintf(nc->info_str, sizeof(nc->info_str),
-                 "socket-ng: fd=%d %s", fd, SocketAddressType_str(sa_type));
-    }
-
     return s;
-
-err:
-    g_free(saddr);
-    closesocket(fd);
-    return NULL;
 }
 
 static void net_socket_connect(void *opaque)
@@ -743,6 +685,7 @@ static int net_socketng_mcast_init(NetClientState *peer,
     NetSocketNGState *s;
     int fd, ret;
     struct sockaddr_in *saddr;
+    gchar *info_str;
 
     if (remote->type != SOCKET_ADDRESS_TYPE_INET) {
         error_setg(errp, "multicast only support inet type");
@@ -762,6 +705,9 @@ static int net_socketng_mcast_init(NetClientState *peer,
             g_free(saddr);
             return -1;
         }
+        info_str = g_strdup_printf("socket-ng: mcast=%s:%d",
+                                   inet_ntoa(saddr->sin_addr),
+                                   ntohs(saddr->sin_port));
     } else {
         switch (local->type) {
         case SOCKET_ADDRESS_TYPE_INET: {
@@ -779,9 +725,14 @@ static int net_socketng_mcast_init(NetClientState *peer,
                 g_free(saddr);
                 return -1;
             }
+            info_str = g_strdup_printf("socket-ng: mcast=%s:%d",
+                                       inet_ntoa(saddr->sin_addr),
+                                       ntohs(saddr->sin_port));
             break;
         }
-        case SOCKET_ADDRESS_TYPE_FD:
+        case SOCKET_ADDRESS_TYPE_FD: {
+            int newfd;
+
             fd = monitor_fd_param(monitor_cur(), local->u.fd.str, errp);
             if (fd == -1) {
                 g_free(saddr);
@@ -794,7 +745,46 @@ static int net_socketng_mcast_init(NetClientState *peer,
                                  name, fd);
                 return -1;
             }
+
+            /*
+             * fd passed: multicast: "learn" dgram_dst address from bound
+             * address and save it. Because this may be "shared" socket from a
+             * "master" process, datagrams would be recv() by ONLY ONE process:
+             * we must "clone" this dgram socket --jjo
+             */
+
+            saddr = g_new(struct sockaddr_in, 1);
+
+            if (convert_host_port(saddr, local->u.inet.host, local->u.inet.port,
+                                  errp) < 0) {
+                g_free(saddr);
+                closesocket(fd);
+                return -1;
+            }
+
+            /* must be bound */
+            if (saddr->sin_addr.s_addr == 0) {
+                error_setg(errp, "can't setup multicast destination address");
+                g_free(saddr);
+                closesocket(fd);
+                return -1;
+            }
+            /* clone dgram socket */
+            newfd = net_socket_mcast_create(saddr, NULL, errp);
+            if (newfd < 0) {
+                g_free(saddr);
+                closesocket(fd);
+                return -1;
+            }
+            /* clone newfd to fd, close newfd */
+            dup2(newfd, fd);
+            close(newfd);
+
+            info_str = g_strdup_printf("socket-ng: fd=%d (cloned mcast=%s:%d)",
+                                       fd, inet_ntoa(saddr->sin_addr),
+                                       ntohs(saddr->sin_port));
             break;
+        }
         default:
             g_free(saddr);
             error_setg(errp, "only support inet or fd type for local");
@@ -802,9 +792,7 @@ static int net_socketng_mcast_init(NetClientState *peer,
         }
     }
 
-    s = net_socket_fd_init_dgram(peer, model, name, fd,
-                                 local->type == SOCKET_ADDRESS_TYPE_FD,
-                                 remote, errp);
+    s = net_socket_fd_init_dgram(peer, model, name, fd, errp);
     if (!s) {
         g_free(saddr);
         return -1;
@@ -813,9 +801,8 @@ static int net_socketng_mcast_init(NetClientState *peer,
     g_assert(s->dgram_dst == NULL);
     s->dgram_dst = (struct sockaddr *)saddr;
 
-    snprintf(s->nc.info_str, sizeof(s->nc.info_str),
-             "socket-ng: mcast=%s:%d",
-             inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
+    pstrcpy(s->nc.info_str, sizeof(s->nc.info_str), info_str);
+    g_free(info_str);
 
     return 0;
 
@@ -895,7 +882,10 @@ static int net_socketng_udp_init(NetClientState *peer,
 
         break;
     }
-    case SOCKET_ADDRESS_TYPE_FD:
+    case SOCKET_ADDRESS_TYPE_FD: {
+        SocketAddress *sa;
+        SocketAddressType sa_type;
+
         fd = monitor_fd_param(monitor_cur(), local->u.fd.str, errp);
         if (fd == -1) {
             return -1;
@@ -906,23 +896,35 @@ static int net_socketng_udp_init(NetClientState *peer,
                              name, fd);
             return -1;
         }
+
+        sa = socket_local_address(fd, errp);
+        if (sa) {
+            sa_type = sa->type;
+            qapi_free_SocketAddress(sa);
+
+            info_str = g_strdup_printf("socket: fd=%d %s", fd,
+                                       SocketAddressType_str(sa_type));
+        } else {
+            info_str = g_strdup_printf("socket: fd=%d", fd);
+        }
         break;
+    }
     default:
         error_setg(errp, "only support inet or fd type for local");
         return -1;
     }
 
-    s = net_socket_fd_init_dgram(peer, model, name, fd, 0, NULL, errp);
+    s = net_socket_fd_init_dgram(peer, model, name, fd, errp);
     if (!s) {
         return -1;
     }
 
+    pstrcpy(s->nc.info_str, sizeof(s->nc.info_str), info_str);
+    g_free(info_str);
+
     if (remote) {
         g_assert(s->dgram_dst == NULL);
         s->dgram_dst = dgram_dst;
-
-        pstrcpy(s->nc.info_str, sizeof(s->nc.info_str), info_str);
-        g_free(info_str);
     }
     return 0;
 };
Signature · 1 line
2.35.3
#11 lore raw

RFC PATCH 6/6 net: socket-ng: add unix socket for dgram mode

In reply to Laurent Vivier
---
 net/socket-ng.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 62 insertions(+), 3 deletions(-)
net/socket-ng.c +62 -3
index aabdd0eed381..d4457f4bc63b 100644
@@ -123,8 +123,15 @@ static ssize_t net_socket_receive_dgram(NetClientState *nc,
 
     do {
         if (s->dgram_dst) {
-            ret = sendto(s->fd, buf, size, 0, s->dgram_dst,
-                         sizeof(struct sockaddr_in));
+            socklen_t len;
+
+            if (s->dgram_dst->sa_family == AF_INET) {
+                len = sizeof(struct sockaddr_in);
+            } else {
+                len = sizeof(struct sockaddr_un);
+            }
+
+            ret = sendto(s->fd, buf, size, 0, s->dgram_dst, len);
         } else {
             ret = send(s->fd, buf, size, 0);
         }
@@ -831,7 +838,7 @@ static int net_socketng_udp_init(NetClientState *peer,
         }
     } else {
         if (local->type != SOCKET_ADDRESS_TYPE_FD) {
-            error_setg(errp, "type=inet and mode=unicast require "
+            error_setg(errp, "type=inet or unix and mode=unicast require "
                              "remote parameter");
             return -1;
         }
@@ -882,6 +889,58 @@ static int net_socketng_udp_init(NetClientState *peer,
 
         break;
     }
+    case SOCKET_ADDRESS_TYPE_UNIX: {
+        struct sockaddr_un laddr_un, raddr_un;
+
+        ret = unlink(local->u.q_unix.path);
+        if (ret < 0 && errno != ENOENT) {
+            error_setg_errno(errp, errno, "failed to unlink socket %s",
+                             local->u.q_unix.path);
+            return -1;
+        }
+
+        laddr_un.sun_family = PF_UNIX;
+        ret = snprintf(laddr_un.sun_path, sizeof(laddr_un.sun_path), "%s",
+                       local->u.q_unix.path);
+        if (ret < 0 || ret >= sizeof(laddr_un.sun_path)) {
+            error_setg(errp, "UNIX socket path '%s' is too long",
+                       local->u.q_unix.path);
+            error_append_hint(errp, "Path must be less than %zu bytes\n",
+                              sizeof(laddr_un.sun_path));
+        }
+
+        raddr_un.sun_family = PF_UNIX;
+        ret = snprintf(raddr_un.sun_path, sizeof(raddr_un.sun_path), "%s",
+                       remote->u.q_unix.path);
+        if (ret < 0 || ret >= sizeof(raddr_un.sun_path)) {
+            error_setg(errp, "UNIX socket path '%s' is too long",
+                       remote->u.q_unix.path);
+            error_append_hint(errp, "Path must be less than %zu bytes\n",
+                              sizeof(raddr_un.sun_path));
+        }
+
+        fd = qemu_socket(PF_UNIX, SOCK_DGRAM, 0);
+        if (fd < 0) {
+            error_setg_errno(errp, errno, "can't create datagram socket");
+            return -1;
+        }
+
+        ret = bind(fd, (struct sockaddr *)&laddr_un, sizeof(laddr_un));
+        if (ret < 0) {
+            error_setg_errno(errp, errno, "can't bind unix=%s to socket",
+                             laddr_un.sun_path);
+            closesocket(fd);
+            return -1;
+        }
+        qemu_socket_set_nonblock(fd);
+
+        dgram_dst = g_malloc(sizeof(raddr_un));
+        memcpy(dgram_dst, &raddr_un, sizeof(raddr_un));
+
+        info_str = g_strdup_printf("socket-ng: udp=%s:%s",
+                                   laddr_un.sun_path, raddr_un.sun_path);
+        break;
+    }
     case SOCKET_ADDRESS_TYPE_FD: {
         SocketAddress *sa;
         SocketAddressType sa_type;
Signature · 1 line
2.35.3
#12 lore raw
In reply to Laurent Vivier
Quoted 8 lines · Laurent Vivier

"-netdev socket" only supports inet sockets.

It's not a complex task to add support for unix sockets, but the socket netdev parameters are not defined to manage well unix socket parameters.

As discussed in:

Quoted 8 lines

"socket.c added support for unix domain socket datagram transport"
https://lore.kernel.org/qemu-devel/[email protected]/

This series adds support of unix socket type using SocketAddress QAPI structure.

A new netdev backend "socket-ng" is added, that is barely a copy of "socket" backend but it uses the SocketAddress QAPI to provide socket parameters. And then it also implement unix sockets (TCP and UDP).

So pulling in the QAPI from patch 2

{ 'enum': 'NetdevSocketNGMode',

     'data':  [ 'dgram', 'server', 'client' ] }
   { 'struct': 'NetdevSocketNGOptions',
     'data': {
       'mode':    'NetdevSocketNGMode',
       '*addr':   'SocketAddress',
       '*remote': 'SocketAddress',
       '*local':  'SocketAddress' } }
Quoted 20 lines

Some examples of CLI syntax:

for TCP:

-netdev socket-ng,id=socket0,mode=server,addr.type=inet,addr.host=localhost,addr.port=1234 -netdev socket-ng,id=socket0,mode=client,addr.type=inet,addr.host=localhost,addr.port=1234

  -netdev socket-ng,id=socket0,mode=dgram,\
          local.type=inet,local.host=localhost,local.port=1234,\
          remote.type=inet,remote.host=localhost,remote.port=1235

for UNIX:

-netdev socket-ng,id=socket0,mode=server,addr.type=unix,addr.path=/tmp/qemu0 -netdev socket-ng,id=socket0,mode=client,addr.type=unix,addr.path=/tmp/qemu0

  -netdev socket-ng,id=socket0,mode=dgram,\
          local.type=unix,local.path=/tmp/qemu0,\
          remote.type=unix,remote.path=/tmp/qemu1
Quoted 6 lines

for FD:

-netdev socket-ng,id=socket0,mode=server,addr.type=fd,addr.str=4 -netdev socket-ng,id=socket0,mode=client,addr.type=fd,addr.str=5

-netdev socket-ng,id=socket0,mode=dgram,local.type=fd,addr.str=4

                                                          ^^^ local.str=4

I notice that in all these examples, mode=client/server always use the 'addr' field, and mode=dgram always uses the 'local'/'remote' fields. IOW, there is almost no commonality between the dgram scenario and the stream scenario, which feels sub-optimal.

Two alternatives come to mind

- mode=client could use 'remote' and mode=server could use 'local', removing the 'addr' field entirely

- Have completely separate backends, ie '-netdev stream' for client/server TCP/UNIX sockets, and '-netdev dgram' for UDP sockets, removing 'mode' field.

I'd have a slight preference for the second, since I'm not thrilled by the 'socket-ng' name :-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#13 lore raw
Quoted 64 lines · On Tue, 10 May 2022 09:26:39 +0100 Daniel P. Berrangé <[email protected]>…
Quoted 8 lines · Laurent Vivier

"-netdev socket" only supports inet sockets.

It's not a complex task to add support for unix sockets, but the socket netdev parameters are not defined to manage well unix socket parameters.

As discussed in:

Quoted 8 lines

"socket.c added support for unix domain socket datagram transport"
https://lore.kernel.org/qemu-devel/[email protected]/

This series adds support of unix socket type using SocketAddress QAPI structure.

A new netdev backend "socket-ng" is added, that is barely a copy of "socket" backend but it uses the SocketAddress QAPI to provide socket parameters. And then it also implement unix sockets (TCP and UDP).

So pulling in the QAPI from patch 2

   { 'enum': 'NetdevSocketNGMode',
     'data':  [ 'dgram', 'server', 'client' ] }

   { 'struct': 'NetdevSocketNGOptions',
     'data': {
       'mode':    'NetdevSocketNGMode',
       '*addr':   'SocketAddress',
       '*remote': 'SocketAddress',
       '*local':  'SocketAddress' } }
Quoted 20 lines

Some examples of CLI syntax:

for TCP:

-netdev socket-ng,id=socket0,mode=server,addr.type=inet,addr.host=localhost,addr.port=1234 -netdev socket-ng,id=socket0,mode=client,addr.type=inet,addr.host=localhost,addr.port=1234

  -netdev socket-ng,id=socket0,mode=dgram,\
          local.type=inet,local.host=localhost,local.port=1234,\
          remote.type=inet,remote.host=localhost,remote.port=1235

for UNIX:

-netdev socket-ng,id=socket0,mode=server,addr.type=unix,addr.path=/tmp/qemu0 -netdev socket-ng,id=socket0,mode=client,addr.type=unix,addr.path=/tmp/qemu0

  -netdev socket-ng,id=socket0,mode=dgram,\
          local.type=unix,local.path=/tmp/qemu0,\
          remote.type=unix,remote.path=/tmp/qemu1
Quoted 6 lines

for FD:

-netdev socket-ng,id=socket0,mode=server,addr.type=fd,addr.str=4 -netdev socket-ng,id=socket0,mode=client,addr.type=fd,addr.str=5

-netdev socket-ng,id=socket0,mode=dgram,local.type=fd,addr.str=4

                                                          ^^^ local.str=4

I notice that in all these examples, mode=client/server always use the 'addr' field, and mode=dgram always uses the 'local'/'remote' fields. IOW, there is almost no commonality between the dgram scenario and the stream scenario, which feels sub-optimal.

Quoted 4 lines

Two alternatives come to mind

- mode=client could use 'remote' and mode=server could use 'local', removing the 'addr' field entirely

To me, "mode is client, address is x" sounds more intuitive than "mode is client, remote is x". I mean, of course it's the remote address -- that's a bit redundant.

Quoted 3 lines

- Have completely separate backends, ie '-netdev stream' for client/server TCP/UNIX sockets, and '-netdev dgram' for UDP sockets, removing 'mode' field.

...this won't work, though, because UNIX domain sockets can be stream-oriented or datagram-oriented.

Signature · 1 line
Stefano
#14 lore raw
Quoted 76 lines · Stefano Brivio
Quoted 64 lines · On Tue, 10 May 2022 09:26:39 +0100 Daniel P. Berrangé <[email protected]>…
Quoted 8 lines · Laurent Vivier

"-netdev socket" only supports inet sockets.

It's not a complex task to add support for unix sockets, but the socket netdev parameters are not defined to manage well unix socket parameters.

As discussed in:

Quoted 8 lines

"socket.c added support for unix domain socket datagram transport"
https://lore.kernel.org/qemu-devel/[email protected]/

This series adds support of unix socket type using SocketAddress QAPI structure.

A new netdev backend "socket-ng" is added, that is barely a copy of "socket" backend but it uses the SocketAddress QAPI to provide socket parameters. And then it also implement unix sockets (TCP and UDP).

So pulling in the QAPI from patch 2

   { 'enum': 'NetdevSocketNGMode',
     'data':  [ 'dgram', 'server', 'client' ] }

   { 'struct': 'NetdevSocketNGOptions',
     'data': {
       'mode':    'NetdevSocketNGMode',
       '*addr':   'SocketAddress',
       '*remote': 'SocketAddress',
       '*local':  'SocketAddress' } }
Quoted 20 lines

Some examples of CLI syntax:

for TCP:

-netdev socket-ng,id=socket0,mode=server,addr.type=inet,addr.host=localhost,addr.port=1234 -netdev socket-ng,id=socket0,mode=client,addr.type=inet,addr.host=localhost,addr.port=1234

  -netdev socket-ng,id=socket0,mode=dgram,\
          local.type=inet,local.host=localhost,local.port=1234,\
          remote.type=inet,remote.host=localhost,remote.port=1235

for UNIX:

-netdev socket-ng,id=socket0,mode=server,addr.type=unix,addr.path=/tmp/qemu0 -netdev socket-ng,id=socket0,mode=client,addr.type=unix,addr.path=/tmp/qemu0

  -netdev socket-ng,id=socket0,mode=dgram,\
          local.type=unix,local.path=/tmp/qemu0,\
          remote.type=unix,remote.path=/tmp/qemu1
Quoted 6 lines

for FD:

-netdev socket-ng,id=socket0,mode=server,addr.type=fd,addr.str=4 -netdev socket-ng,id=socket0,mode=client,addr.type=fd,addr.str=5

-netdev socket-ng,id=socket0,mode=dgram,local.type=fd,addr.str=4

                                                          ^^^ local.str=4

I notice that in all these examples, mode=client/server always use the 'addr' field, and mode=dgram always uses the 'local'/'remote' fields. IOW, there is almost no commonality between the dgram scenario and the stream scenario, which feels sub-optimal.

Quoted 4 lines

Two alternatives come to mind

- mode=client could use 'remote' and mode=server could use 'local', removing the 'addr' field entirely

To me, "mode is client, address is x" sounds more intuitive than "mode is client, remote is x". I mean, of course it's the remote address -- that's a bit redundant.

Quoted 6 lines
Quoted 3 lines

- Have completely separate backends, ie '-netdev stream' for client/server TCP/UNIX sockets, and '-netdev dgram' for UDP sockets, removing 'mode' field.

...this won't work, though, because UNIX domain sockets can be stream-oriented or datagram-oriented.

Sure it can work, both the 'stream' and 'dgram' backends would allow the full range of addr types as they're independant config dimensions

    -netdev stream,server=no,addr.type=inet,addr.host=localhost,addr.port=1234
    -netdev stream,server=no,addr.type=unix,addr.path=/some/stream/sock


    -netdev dgram,id=ndev0,\
            local.type=inet,local.host=localhost,local.port=1234,\
            remote.type=inet,remote.host=localhost,remote.port=1235
    -netdev dgram,id=ndev0,\
            local.type=unix,local.path=/some/dgram/sock0,
            remote.type=unix,remote.path=/some/dgram/sock1

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#15 lore raw
Quoted 73 lines · On Tue, 10 May 2022 10:22:48 +0100 Daniel P. Berrangé <[email protected]>…
Quoted 72 lines · Stefano Brivio
Quoted 68 lines · On Tue, 10 May 2022 09:26:39 +0100 Daniel P. Berrangé <[email protected]>…
Quoted 8 lines · Laurent Vivier

"-netdev socket" only supports inet sockets.

It's not a complex task to add support for unix sockets, but the socket netdev parameters are not defined to manage well unix socket parameters.

As discussed in:

Quoted 8 lines

"socket.c added support for unix domain socket datagram transport"
https://lore.kernel.org/qemu-devel/[email protected]/

This series adds support of unix socket type using SocketAddress QAPI structure.

A new netdev backend "socket-ng" is added, that is barely a copy of "socket" backend but it uses the SocketAddress QAPI to provide socket parameters. And then it also implement unix sockets (TCP and UDP).

So pulling in the QAPI from patch 2

   { 'enum': 'NetdevSocketNGMode',
     'data':  [ 'dgram', 'server', 'client' ] }

   { 'struct': 'NetdevSocketNGOptions',
     'data': {
       'mode':    'NetdevSocketNGMode',
       '*addr':   'SocketAddress',
       '*remote': 'SocketAddress',
       '*local':  'SocketAddress' } }
Quoted 20 lines

Some examples of CLI syntax:

for TCP:

-netdev socket-ng,id=socket0,mode=server,addr.type=inet,addr.host=localhost,addr.port=1234 -netdev socket-ng,id=socket0,mode=client,addr.type=inet,addr.host=localhost,addr.port=1234

  -netdev socket-ng,id=socket0,mode=dgram,\
          local.type=inet,local.host=localhost,local.port=1234,\
          remote.type=inet,remote.host=localhost,remote.port=1235

for UNIX:

-netdev socket-ng,id=socket0,mode=server,addr.type=unix,addr.path=/tmp/qemu0 -netdev socket-ng,id=socket0,mode=client,addr.type=unix,addr.path=/tmp/qemu0

  -netdev socket-ng,id=socket0,mode=dgram,\
          local.type=unix,local.path=/tmp/qemu0,\
          remote.type=unix,remote.path=/tmp/qemu1
Quoted 6 lines

for FD:

-netdev socket-ng,id=socket0,mode=server,addr.type=fd,addr.str=4 -netdev socket-ng,id=socket0,mode=client,addr.type=fd,addr.str=5

-netdev socket-ng,id=socket0,mode=dgram,local.type=fd,addr.str=4

                                                          ^^^ local.str=4

I notice that in all these examples, mode=client/server always use the 'addr' field, and mode=dgram always uses the 'local'/'remote' fields. IOW, there is almost no commonality between the dgram scenario and the stream scenario, which feels sub-optimal.

Two alternatives come to mind

- mode=client could use 'remote' and mode=server could use 'local', removing the 'addr' field entirely

Quoted 7 lines
Quoted 7 lines

To me, "mode is client, address is x" sounds more intuitive than "mode is client, remote is x". I mean, of course it's the remote address -- that's a bit redundant.

Quoted 3 lines

- Have completely separate backends, ie '-netdev stream' for client/server TCP/UNIX sockets, and '-netdev dgram' for UDP sockets, removing 'mode' field.

Well, this ^^^ is one thing ('-netdev stream' for UNIX sockets),

Quoted 8 lines
Quoted 2 lines

...this won't work, though, because UNIX domain sockets can be stream-oriented or datagram-oriented.

Sure it can work, both the 'stream' and 'dgram' backends would allow the full range of addr types as they're independant config dimensions

Quoted 10 lines
    -netdev stream,server=no,addr.type=inet,addr.host=localhost,addr.port=1234
    -netdev stream,server=no,addr.type=unix,addr.path=/some/stream/sock


    -netdev dgram,id=ndev0,\
            local.type=inet,local.host=localhost,local.port=1234,\
            remote.type=inet,remote.host=localhost,remote.port=1235
    -netdev dgram,id=ndev0,\
            local.type=unix,local.path=/some/dgram/sock0,
            remote.type=unix,remote.path=/some/dgram/sock1

...and this ('-netdev dgram' for UNIX sockets) is another one. :)

Indeed they're independent though, so I also prefer this version (with the details Laurent just provided).

Signature · 1 line
Stefano
#16 lore raw
In reply to Daniel P. Berrangé

This looks very promising to me as it provides the flexibility needed for all the permutations. Thanks for looking into it!

Quoted 122 lines · Daniel P. Berrangé
Quoted 6 lines · Stefano Brivio
Quoted 3 lines · On Tue, 10 May 2022 09:26:39 +0100 Daniel P. Berrangé <[email protected]>…
Quoted 2 lines · Laurent Vivier

"-netdev socket" only supports inet sockets.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

It's not a complex task to add support for unix sockets, but the socket netdev parameters are not defined to manage well unix socket parameters.

As discussed in:

"socket.c added support for unix domain socket datagram transport"

https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

This series adds support of unix socket type using SocketAddress

QAPI structure.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

A new netdev backend "socket-ng" is added, that is barely a copy of

"socket"

Quoted 1 line
Quoted 1 line
Quoted 1 line

backend but it uses the SocketAddress QAPI to provide socket

parameters.

Quoted 14 lines
Quoted 14 lines
Quoted 1 line

And then it also implement unix sockets (TCP and UDP).

So pulling in the QAPI from patch 2

   { 'enum': 'NetdevSocketNGMode',
     'data':  [ 'dgram', 'server', 'client' ] }

   { 'struct': 'NetdevSocketNGOptions',
     'data': {
       'mode':    'NetdevSocketNGMode',
       '*addr':   'SocketAddress',
       '*remote': 'SocketAddress',
       '*local':  'SocketAddress' } }
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Some examples of CLI syntax:

for TCP:

-netdev

socket-ng,id=socket0,mode=server,addr.type=inet,addr.host=localhost,addr.port=1234

Quoted 1 line
Quoted 1 line
Quoted 1 line

-netdev

socket-ng,id=socket0,mode=client,addr.type=inet,addr.host=localhost,addr.port=1234

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
  -netdev socket-ng,id=socket0,mode=dgram,\
          local.type=inet,local.host=localhost,local.port=1234,\
          remote.type=inet,remote.host=localhost,remote.port=1235

for UNIX:

-netdev

socket-ng,id=socket0,mode=server,addr.type=unix,addr.path=/tmp/qemu0

Quoted 1 line
Quoted 1 line
Quoted 1 line

-netdev

socket-ng,id=socket0,mode=client,addr.type=unix,addr.path=/tmp/qemu0

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
  -netdev socket-ng,id=socket0,mode=dgram,\
          local.type=unix,local.path=/tmp/qemu0,\
          remote.type=unix,remote.path=/tmp/qemu1
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

for FD:

-netdev socket-ng,id=socket0,mode=server,addr.type=fd,addr.str=4 -netdev socket-ng,id=socket0,mode=client,addr.type=fd,addr.str=5

-netdev socket-ng,id=socket0,mode=dgram,local.type=fd,addr.str=4

                                                          ^^^

local.str=4

Quoted 15 lines
Quoted 10 lines

I notice that in all these examples, mode=client/server always use the 'addr' field, and mode=dgram always uses the 'local'/'remote' fields. IOW, there is almost no commonality between the dgram scenario and the stream scenario, which feels sub-optimal.

Two alternatives come to mind

- mode=client could use 'remote' and mode=server could use 'local', removing the 'addr' field entirely

To me, "mode is client, address is x" sounds more intuitive than "mode is client, remote is x". I mean, of course it's the remote address -- that's a bit redundant.

Quoted 6 lines
Quoted 3 lines

- Have completely separate backends, ie '-netdev stream' for client/server TCP/UNIX sockets, and '-netdev dgram' for UDP sockets, removing 'mode' field.

...this won't work, though, because UNIX domain sockets can be stream-oriented or datagram-oriented.

Sure it can work, both the 'stream' and 'dgram' backends would allow the full range of addr types as they're independant config dimensions

-netdev
stream,server=no,addr.type=inet,addr.host=localhost,addr.port=1234

    -netdev stream,server=no,addr.type=unix,addr.path=/some/stream/sock


    -netdev dgram,id=ndev0,\
            local.type=inet,local.host=localhost,local.port=1234,\
            remote.type=inet,remote.host=localhost,remote.port=1235
    -netdev dgram,id=ndev0,\
            local.type=unix,local.path=/some/dgram/sock0,
            remote.type=unix,remote.path=/some/dgram/sock1

With regards,
Daniel
--
|: https://berrange.com -o-
https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org -o-
https://fstop138.berrange.com :|
|: https://entangle-photo.org -o-
https://www.instagram.com/dberrange :|

Signature · 5 lines
Ralph Schmieder
Holtzstr. 2
76135 Karlsruhe
Germany
[email protected]
#17 lore raw
In reply to Daniel P. Berrangé
Quoted 69 lines · Daniel P. Berrangé
Quoted 8 lines · Laurent Vivier

"-netdev socket" only supports inet sockets.

It's not a complex task to add support for unix sockets, but the socket netdev parameters are not defined to manage well unix socket parameters.

As discussed in:

Quoted 8 lines

"socket.c added support for unix domain socket datagram transport"
https://lore.kernel.org/qemu-devel/[email protected]/

This series adds support of unix socket type using SocketAddress QAPI structure.

A new netdev backend "socket-ng" is added, that is barely a copy of "socket" backend but it uses the SocketAddress QAPI to provide socket parameters. And then it also implement unix sockets (TCP and UDP).

So pulling in the QAPI from patch 2

    { 'enum': 'NetdevSocketNGMode',
      'data':  [ 'dgram', 'server', 'client' ] }

    { 'struct': 'NetdevSocketNGOptions',
      'data': {
        'mode':    'NetdevSocketNGMode',
        '*addr':   'SocketAddress',
        '*remote': 'SocketAddress',
        '*local':  'SocketAddress' } }
Quoted 18 lines

Some examples of CLI syntax:

   for TCP:

   -netdev socket-ng,id=socket0,mode=server,addr.type=inet,addr.host=localhost,addr.port=1234
   -netdev socket-ng,id=socket0,mode=client,addr.type=inet,addr.host=localhost,addr.port=1234

   -netdev socket-ng,id=socket0,mode=dgram,\
           local.type=inet,local.host=localhost,local.port=1234,\
           remote.type=inet,remote.host=localhost,remote.port=1235

   for UNIX:

   -netdev socket-ng,id=socket0,mode=server,addr.type=unix,addr.path=/tmp/qemu0
   -netdev socket-ng,id=socket0,mode=client,addr.type=unix,addr.path=/tmp/qemu0

   -netdev socket-ng,id=socket0,mode=dgram,\
           local.type=unix,local.path=/tmp/qemu0,\
Quoted 8 lines
           remote.type=unix,remote.path=/tmp/qemu1

   for FD:

   -netdev socket-ng,id=socket0,mode=server,addr.type=fd,addr.str=4
   -netdev socket-ng,id=socket0,mode=client,addr.type=fd,addr.str=5

   -netdev socket-ng,id=socket0,mode=dgram,local.type=fd,addr.str=4
                                                           ^^^ local.str=4

I notice that in all these examples, mode=client/server always use the 'addr' field, and mode=dgram always uses the 'local'/'remote' fields. IOW, there is almost no commonality between the dgram scenario and the stream scenario, which feels sub-optimal.

Two alternatives come to mind

- mode=client could use 'remote' and mode=server could use 'local', removing the 'addr' field entirely

Quoted 6 lines

- Have completely separate backends, ie '-netdev stream' for client/server TCP/UNIX sockets, and '-netdev dgram' for UDP sockets, removing 'mode' field.

I'd have a slight preference for the second, since I'm not thrilled by the 'socket-ng' name :-)

It seems reasonable, I'm going to update my series in this way:

   { 'struct': 'NetdevStreamOptions',
     'data': {
       'addr':   'SocketAddress' } }

   { 'struct': 'NetdevDgramOptions',
     'data': {
      '*local':   'SocketAddress',
      '*remote':   'SocketAddress' } }

Both parameters are optional because:
multicast needs 'remote' for the multicast address and optionally a local for source address or fd. unicast needs both, except for fd where local provides fd and remote is needed only if local is not an fd.

{ 'union': 'Netdev',

     'base': { 'id': 'str', 'type': 'NetClientDriver' },
     'discriminator': 'type',
     'data': {
     ...
     'stream': 'NetdevStreamOptions',
     'dgram': 'NetdevDgramOptions' } }

Thanks,
Laurent