Skip to contentSkip to conversation

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

RFC PATCH Superseded Laurent Vivier 4 participants 20 messages May 12, 2022 – Jun 20, 2022

Subsystems (2)

v2 of 14 Superseded Latest is v14 →

lore
Share & tools
lore
#0 lore raw

v2 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.

Two new netdev backends, "stream" and "dgram" are added, that are barely a copy of "socket" backend but they use the SocketAddress QAPI to provide socket parameters. And then they also implement unix sockets (TCP and UDP).

Some examples of CLI syntax:

for TCP:

-netdev stream,id=socket0,addr.type=inet,addr.host=localhost,addr.port=1234 -netdev stream,id=socket0,server=off,addr.type=inet,addr.host=localhost,addr.port=1234

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

for UNIX:

-netdev stream,id=socket0,addr.type=unix,addr.path=/tmp/qemu0 -netdev stream,id=socket0,server=off,addr.type=unix,addr.path=/tmp/qemu0

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

for FD:

-netdev stream,id=socket0,addr.type=fd,addr.str=4 -netdev stream,id=socket0,server=off,addr.type=fd,addr.str=5

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

v2:
- use "stream" and "dgram" rather than "socket-ng,mode=stream" and ""socket-ng,mode=dgram"
- extract code to bypass qemu_opts_parse_noisily() to a new patch
- do not ignore EINVAL (Stefano)
- fix "-net" option

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

Laurent Vivier (7):
net: introduce convert_host_port()
qapi: net: introduce a way to bypass qemu_opts_parse_noisily()
qapi: net: add stream and dgram netdevs
net: stream: add unix socket
net: dgram: make dgram_dst generic
net: dgram: move mcast specific code from net_socket_fd_init_dgram()
net: dgram: add unix socket

Stefano Brivio (1):
net: stream: Don't ignore EINVAL on netdev socket connection

 hmp-commands.hx        |   2 +-
 include/qemu/sockets.h |   2 +
 net/clients.h          |   6 +
 net/dgram.c            | 706 +++++++++++++++++++++++++++++++++++++++++
 net/hub.c              |   2 +
 net/meson.build        |   2 +
 net/net.c              | 138 ++++++--
 net/stream.c           | 516 ++++++++++++++++++++++++++++++
 qapi/net.json          |  38 ++-
 9 files changed, 1379 insertions(+), 33 deletions(-)
 create mode 100644 net/dgram.c
 create mode 100644 net/stream.c
Signature · 1 line
2.35.3
#1 lore raw

RFC PATCH v2 1/8 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

RFC PATCH v2 2/8 qapi: net: introduce a way to bypass qemu_opts_parse_noisily()

In reply to Laurent Vivier

As qemu_opts_parse_noisily() flattens the QAPI structures ("type" field of Netdev structure can collides with "type" field of SocketAddress), we introduce a way to bypass qemu_opts_parse_noisily() and use directly visit_type_Netdev() to parse the backend parameters.

---
 net/net.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
net/net.c +54 -0
index 58c05c200622..2aab7167316c 100644
@@ -54,6 +54,7 @@
 #include "net/colo-compare.h"
 #include "net/filter.h"
 #include "qapi/string-output-visitor.h"
+#include "qapi/qobject-input-visitor.h"
 
 /* Net bridge is currently not supported for W32. */
 #if !defined(_WIN32)
@@ -63,6 +64,17 @@
 static VMChangeStateEntry *net_change_state_entry;
 static QTAILQ_HEAD(, NetClientState) net_clients;
 
+typedef struct NetdevQueueEntry {
+    bool is_netdev;
+    Netdev *nd;
+    Location loc;
+    QSIMPLEQ_ENTRY(NetdevQueueEntry) entry;
+} NetdevQueueEntry;
+
+typedef QSIMPLEQ_HEAD(, NetdevQueueEntry) NetdevQueue;
+
+static NetdevQueue nd_queue = QSIMPLEQ_HEAD_INITIALIZER(nd_queue);
+
 /***********************************************************/
 /* network device redirectors */
 
@@ -1559,6 +1571,19 @@ int net_init_clients(Error **errp)
 
     QTAILQ_INIT(&net_clients);
 
+    while (!QSIMPLEQ_EMPTY(&nd_queue)) {
+        NetdevQueueEntry *nd = QSIMPLEQ_FIRST(&nd_queue);
+
+        QSIMPLEQ_REMOVE_HEAD(&nd_queue, entry);
+        loc_push_restore(&nd->loc);
+        if (net_client_init1(nd->nd, nd->is_netdev, errp) < 0) {
+            return -1;
+        }
+        loc_pop(&nd->loc);
+        qapi_free_Netdev(nd->nd);
+        g_free(nd);
+    }
+
     if (qemu_opts_foreach(qemu_find_opts("netdev"),
                           net_init_netdev, NULL, errp)) {
         return -1;
@@ -1575,8 +1600,37 @@ int net_init_clients(Error **errp)
     return 0;
 }
 
+/*
+ * netdev_is_modern() returns true when the backend needs to bypass
+ * qemu_opts_parse_noisily()
+ */
+static bool netdev_is_modern(const char *optarg)
+{
+    return false;
+}
+
 int net_client_parse(QemuOptsList *opts_list, const char *optarg)
 {
+    if (netdev_is_modern(optarg)) {
+            /*
+             * We need to bypass qemu_opts_parse_noisily() to accept
+             * new style object like addr.type=inet in SocketAddress
+             */
+            Visitor *v;
+            NetdevQueueEntry *nd;
+
+            v = qobject_input_visitor_new_str(optarg, "type",
+                                              &error_fatal);
+            nd = g_new(NetdevQueueEntry, 1);
+            visit_type_Netdev(v, NULL, &nd->nd, &error_fatal);
+            visit_free(v);
+            loc_save(&nd->loc);
+            nd->is_netdev = strcmp(opts_list->name, "netdev") == 0;
+
+            QSIMPLEQ_INSERT_TAIL(&nd_queue, nd, entry);
+            return 0;
+    }
+
     if (!qemu_opts_parse_noisily(opts_list, optarg, true)) {
         return -1;
     }
Signature · 1 line
2.35.3
#3 lore raw

Re: [RFC PATCH v2 2/8] qapi: net: introduce a way to bypass qemu_opts_parse_noisily()

Quoted 2 lines · Laurent Vivier

As qemu_opts_parse_noisily() flattens the QAPI structures ("type" field of Netdev structure can collides with "type" field of SocketAddress),

To remember how this works, I have to write a more verbose version of the above. Why not post it then, so here goes.

qemu_init() passes the argument of -netdev, -nic, and -net to net_client_parse().

net_client_parse() parses with qemu_opts_parse_noisily(), passing QemuOptsList qemu_netdev_opts for -netdev, qemu_nic_opts for -nic, and qemu_net_opts for -net. Their desc[] are all empty, which means any keys are accepted. The result of the parse (a QemuOpts) is stored in the QemuOptsList.

Note that QemuOpts is flat by design. In some places, we layer non-flat on top using dotted keys convention, but not here.

net_init_clients() iterates over the stored QemuOpts, and passes them to net_init_netdev(), net_param_nic(), or net_init_client(), respectively.

These functions pass the QemuOpts to net_client_init(). They also do other things with the QemuOpts, which we can ignore here.

net_client_init() uses the opts visitor to convert the (flat) QemOpts to a (non-flat) QAPI object Netdev. Netdev is also the argument of QMP command netdev_add.

The opts visitor was an early attempt to support QAPI in (QemuOpts-based) CLI. It restricts QAPI types to a certain shape; see commit eb7ee2cbeb "qapi: introduce OptsVisitor".

A more modern way to support QAPI is qobject_input_visitor_new_str(). It uses keyval_parse() instead of QemuOpts for KEY=VALUE,... syntax, and it also supports JSON syntax. The former isn't quite as expressive as JSON, but it's a lot closer than QemuOpts + opts visitor.

Quoted 2 lines

we introduce a way to bypass qemu_opts_parse_noisily() and use directly visit_type_Netdev() to parse the backend parameters.

This commit paves the way to use of the modern way instead.

Quoted 36 lines
---
 net/net.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
net/net.c +12 -0
index 58c05c200622..2aab7167316c 100644
@@ -54,6 +54,7 @@
 #include "net/colo-compare.h"
 #include "net/filter.h"
 #include "qapi/string-output-visitor.h"
+#include "qapi/qobject-input-visitor.h"
 
 /* Net bridge is currently not supported for W32. */
 #if !defined(_WIN32)
@@ -63,6 +64,17 @@
 static VMChangeStateEntry *net_change_state_entry;
 static QTAILQ_HEAD(, NetClientState) net_clients;
 
+typedef struct NetdevQueueEntry {
+    bool is_netdev;
+    Netdev *nd;
+    Location loc;
+    QSIMPLEQ_ENTRY(NetdevQueueEntry) entry;
+} NetdevQueueEntry;
+
+typedef QSIMPLEQ_HEAD(, NetdevQueueEntry) NetdevQueue;
+
+static NetdevQueue nd_queue = QSIMPLEQ_HEAD_INITIALIZER(nd_queue);
+
 /***********************************************************/
 /* network device redirectors */
 

net/net.c int net_init_clients(Error **errp) · line 1579

Quoted 9 lines
net/net.c +6 -0
@@ -1559,6 +1571,19 @@ int net_init_clients(Error **errp)
 
     QTAILQ_INIT(&net_clients);
 
+    while (!QSIMPLEQ_EMPTY(&nd_queue)) {
+        NetdevQueueEntry *nd = QSIMPLEQ_FIRST(&nd_queue);
+
+        QSIMPLEQ_REMOVE_HEAD(&nd_queue, entry);
+        loc_push_restore(&nd->loc);
+        if (net_client_init1(nd->nd, nd->is_netdev, errp) < 0) {

I think you need to loc_pop() here.

net/net.c int net_init_clients(Error **errp) · line 1581

Quoted 2 lines
net/net.c +2 -0
+            return -1;
+        }

Since the only caller passes &error_fatal, I'd be tempted to ditch the @errp argument, and simply do

           net_client_init1(nd->nd, nd->is_netdev, &error_fatal);

It's what we do for -blockdev, -device, and -object.

Quoted 18 lines
net/net.c +11 -0
+        loc_pop(&nd->loc);
+        qapi_free_Netdev(nd->nd);
+        g_free(nd);
+    }
+
     if (qemu_opts_foreach(qemu_find_opts("netdev"),
                           net_init_netdev, NULL, errp)) {
         return -1;
@@ -1575,8 +1600,37 @@ int net_init_clients(Error **errp)
     return 0;
 }
 
+/*
+ * netdev_is_modern() returns true when the backend needs to bypass
+ * qemu_opts_parse_noisily()
+ */
+static bool netdev_is_modern(const char *optarg)
+{

net/net.c int net_init_clients(Error **errp) · line 1618

Quoted 10 lines
net/net.c +8 -0
+    return false;
+}
+
 int net_client_parse(QemuOptsList *opts_list, const char *optarg)
 {
+    if (netdev_is_modern(optarg)) {
+            /*
+             * We need to bypass qemu_opts_parse_noisily() to accept
+             * new style object like addr.type=inet in SocketAddress
+             */

I'm not sure this will makes sense to future readers.

What about "Use modern, more expressive syntax"?

Quoted 4 lines
net/net.c +4 -0
+            Visitor *v;
+            NetdevQueueEntry *nd;
+
+            v = qobject_input_visitor_new_str(optarg, "type",

net/net.c int net_init_clients(Error **errp) · line 1632

Quoted 10 lines
net/net.c +10 -0
+                                              &error_fatal);
+            nd = g_new(NetdevQueueEntry, 1);
+            visit_type_Netdev(v, NULL, &nd->nd, &error_fatal);
+            visit_free(v);
+            loc_save(&nd->loc);
+            nd->is_netdev = strcmp(opts_list->name, "netdev") == 0;
+
+            QSIMPLEQ_INSERT_TAIL(&nd_queue, nd, entry);
+            return 0;
+    }

Matches what we do for -blockdev, except we additionally have nd->is_netdev. We need it for calling net_client_init1().

If netdev_is_modern(optarg), then the only use of parameter @opts_list is opts_list->name in the initialization of nd->is_netdev.

There's a bit of code smell, I'm afraid.

I believe @is_netdev needs to be true for -netdev, -nic, and netdev_add; false for -net.

Will we ever use the modern syntax with -net?

Any chance we can deprecate -net?

Quoted 4 lines
net/net.c +1 -0
+
     if (!qemu_opts_parse_noisily(opts_list, optarg, true)) {
         return -1;
     }
#4 lore raw

Re: [RFC PATCH v2 2/8] qapi: net: introduce a way to bypass qemu_opts_parse_noisily()

Quoted 39 lines · Markus Armbruster
Quoted 2 lines · Laurent Vivier

As qemu_opts_parse_noisily() flattens the QAPI structures ("type" field of Netdev structure can collides with "type" field of SocketAddress),

To remember how this works, I have to write a more verbose version of the above. Why not post it then, so here goes.

qemu_init() passes the argument of -netdev, -nic, and -net to net_client_parse().

net_client_parse() parses with qemu_opts_parse_noisily(), passing QemuOptsList qemu_netdev_opts for -netdev, qemu_nic_opts for -nic, and qemu_net_opts for -net. Their desc[] are all empty, which means any keys are accepted. The result of the parse (a QemuOpts) is stored in the QemuOptsList.

Note that QemuOpts is flat by design. In some places, we layer non-flat on top using dotted keys convention, but not here.

net_init_clients() iterates over the stored QemuOpts, and passes them to net_init_netdev(), net_param_nic(), or net_init_client(), respectively.

These functions pass the QemuOpts to net_client_init(). They also do other things with the QemuOpts, which we can ignore here.

net_client_init() uses the opts visitor to convert the (flat) QemOpts to a (non-flat) QAPI object Netdev. Netdev is also the argument of QMP command netdev_add.

The opts visitor was an early attempt to support QAPI in (QemuOpts-based) CLI. It restricts QAPI types to a certain shape; see commit eb7ee2cbeb "qapi: introduce OptsVisitor".

A more modern way to support QAPI is qobject_input_visitor_new_str(). It uses keyval_parse() instead of QemuOpts for KEY=VALUE,... syntax, and it also supports JSON syntax. The former isn't quite as expressive as JSON, but it's a lot closer than QemuOpts + opts visitor.

Quoted 4 lines
Quoted 2 lines

we introduce a way to bypass qemu_opts_parse_noisily() and use directly visit_type_Netdev() to parse the backend parameters.

This commit paves the way to use of the modern way instead.

I'm going to copy your analysis to the commit message of the patch.

Quoted 52 lines
Quoted 37 lines
---
  net/net.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 54 insertions(+)
net/net.c +12 -0
index 58c05c200622..2aab7167316c 100644
@@ -54,6 +54,7 @@
  #include "net/colo-compare.h"
  #include "net/filter.h"
  #include "qapi/string-output-visitor.h"
+#include "qapi/qobject-input-visitor.h"
 
  /* Net bridge is currently not supported for W32. */
  #if !defined(_WIN32)
@@ -63,6 +64,17 @@
  static VMChangeStateEntry *net_change_state_entry;
  static QTAILQ_HEAD(, NetClientState) net_clients;
 
+typedef struct NetdevQueueEntry {
+    bool is_netdev;
+    Netdev *nd;
+    Location loc;
+    QSIMPLEQ_ENTRY(NetdevQueueEntry) entry;
+} NetdevQueueEntry;
+
+typedef QSIMPLEQ_HEAD(, NetdevQueueEntry) NetdevQueue;
+
+static NetdevQueue nd_queue = QSIMPLEQ_HEAD_INITIALIZER(nd_queue);
+
  /***********************************************************/
  /* network device redirectors */
 

net/net.c int net_init_clients(Error **errp) · line 1579

Quoted 9 lines
net/net.c +6 -0
@@ -1559,6 +1571,19 @@ int net_init_clients(Error **errp)
 
      QTAILQ_INIT(&net_clients);
 
+    while (!QSIMPLEQ_EMPTY(&nd_queue)) {
+        NetdevQueueEntry *nd = QSIMPLEQ_FIRST(&nd_queue);
+
+        QSIMPLEQ_REMOVE_HEAD(&nd_queue, entry);
+        loc_push_restore(&nd->loc);
+        if (net_client_init1(nd->nd, nd->is_netdev, errp) < 0) {

I think you need to loc_pop() here.

net/net.c int net_init_clients(Error **errp) · line 1581

Quoted 2 lines
net/net.c +2 -0
+            return -1;
+        }
Quoted 6 lines

Since the only caller passes &error_fatal, I'd be tempted to ditch the @errp argument, and simply do

            net_client_init1(nd->nd, nd->is_netdev, &error_fatal);

It's what we do for -blockdev, -device, and -object.

I've added a patch to remove the @errp from the net_init_clients() arguments.

Quoted 32 lines
Quoted 19 lines
net/net.c +11 -0
+        loc_pop(&nd->loc);
+        qapi_free_Netdev(nd->nd);
+        g_free(nd);
+    }
+
      if (qemu_opts_foreach(qemu_find_opts("netdev"),
                            net_init_netdev, NULL, errp)) {
          return -1;
@@ -1575,8 +1600,37 @@ int net_init_clients(Error **errp)
      return 0;
  }
 
+/*
+ * netdev_is_modern() returns true when the backend needs to bypass
+ * qemu_opts_parse_noisily()
+ */
+static bool netdev_is_modern(const char *optarg)
+{

net/net.c int net_init_clients(Error **errp) · line 1618

Quoted 10 lines
net/net.c +8 -0
+    return false;
+}
+
  int net_client_parse(QemuOptsList *opts_list, const char *optarg)
  {
+    if (netdev_is_modern(optarg)) {
+            /*
+             * We need to bypass qemu_opts_parse_noisily() to accept
+             * new style object like addr.type=inet in SocketAddress
+             */

I'm not sure this will makes sense to future readers.

Quoted 1 line

What about "Use modern, more expressive syntax"?

Done.

Quoted 19 lines
Quoted 5 lines
net/net.c +4 -0
+            Visitor *v;
+            NetdevQueueEntry *nd;
+
+            v = qobject_input_visitor_new_str(optarg, "type",

net/net.c int net_init_clients(Error **errp) · line 1632

Quoted 10 lines
net/net.c +10 -0
+                                              &error_fatal);
+            nd = g_new(NetdevQueueEntry, 1);
+            visit_type_Netdev(v, NULL, &nd->nd, &error_fatal);
+            visit_free(v);
+            loc_save(&nd->loc);
+            nd->is_netdev = strcmp(opts_list->name, "netdev") == 0;
+
+            QSIMPLEQ_INSERT_TAIL(&nd_queue, nd, entry);
+            return 0;
+    }

Matches what we do for -blockdev, except we additionally have nd->is_netdev. We need it for calling net_client_init1().

Quoted 4 lines

If netdev_is_modern(optarg), then the only use of parameter @opts_list is opts_list->name in the initialization of nd->is_netdev.

There's a bit of code smell, I'm afraid.

I don't see what is the problem.

Quoted 5 lines

I believe @is_netdev needs to be true for -netdev, -nic, and netdev_add; false for -net.

Will we ever use the modern syntax with -net?

Yes, I think we should support the same syntax with -netdev and -net.

My first iteration was to pass is_netdev=true to net_client_init1() and Stefano reported a problem with "-net" with things like that:

    -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
Quoted 2 lines

Any chance we can deprecate -net?

Who can decide of that?

Quoted 6 lines

net/net.c int net_init_clients(Error **errp) · line 1636

Quoted 5 lines
net/net.c +1 -0
+
      if (!qemu_opts_parse_noisily(opts_list, optarg, true)) {
          return -1;
      }

Thanks,
Laurent

#5 lore raw

Re: [RFC PATCH v2 2/8] qapi: net: introduce a way to bypass qemu_opts_parse_noisily()

Quoted 40 lines · Laurent Vivier
Quoted 39 lines · Markus Armbruster
Quoted 2 lines · Laurent Vivier

As qemu_opts_parse_noisily() flattens the QAPI structures ("type" field of Netdev structure can collides with "type" field of SocketAddress),

To remember how this works, I have to write a more verbose version of the above. Why not post it then, so here goes.

qemu_init() passes the argument of -netdev, -nic, and -net to net_client_parse().

net_client_parse() parses with qemu_opts_parse_noisily(), passing QemuOptsList qemu_netdev_opts for -netdev, qemu_nic_opts for -nic, and qemu_net_opts for -net. Their desc[] are all empty, which means any keys are accepted. The result of the parse (a QemuOpts) is stored in the QemuOptsList.

Note that QemuOpts is flat by design. In some places, we layer non-flat on top using dotted keys convention, but not here.

net_init_clients() iterates over the stored QemuOpts, and passes them to net_init_netdev(), net_param_nic(), or net_init_client(), respectively.

These functions pass the QemuOpts to net_client_init(). They also do other things with the QemuOpts, which we can ignore here.

net_client_init() uses the opts visitor to convert the (flat) QemOpts to a (non-flat) QAPI object Netdev. Netdev is also the argument of QMP command netdev_add.

The opts visitor was an early attempt to support QAPI in (QemuOpts-based) CLI. It restricts QAPI types to a certain shape; see commit eb7ee2cbeb "qapi: introduce OptsVisitor".

A more modern way to support QAPI is qobject_input_visitor_new_str(). It uses keyval_parse() instead of QemuOpts for KEY=VALUE,... syntax, and it also supports JSON syntax. The former isn't quite as expressive as JSON, but it's a lot closer than QemuOpts + opts visitor.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

we introduce a way to bypass qemu_opts_parse_noisily() and use directly visit_type_Netdev() to parse the backend parameters.

This commit paves the way to use of the modern way instead.

I'm going to copy your analysis to the commit message of the patch.

Go right ahead :)

Quoted 115 lines
Quoted 51 lines
Quoted 36 lines
---
  net/net.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 54 insertions(+)
net/net.c +12 -0
index 58c05c200622..2aab7167316c 100644
@@ -54,6 +54,7 @@
  #include "net/colo-compare.h"
  #include "net/filter.h"
  #include "qapi/string-output-visitor.h"
+#include "qapi/qobject-input-visitor.h"
 
  /* Net bridge is currently not supported for W32. */
  #if !defined(_WIN32)
@@ -63,6 +64,17 @@
  static VMChangeStateEntry *net_change_state_entry;
  static QTAILQ_HEAD(, NetClientState) net_clients;
 
+typedef struct NetdevQueueEntry {
+    bool is_netdev;
+    Netdev *nd;
+    Location loc;
+    QSIMPLEQ_ENTRY(NetdevQueueEntry) entry;
+} NetdevQueueEntry;
+
+typedef QSIMPLEQ_HEAD(, NetdevQueueEntry) NetdevQueue;
+
+static NetdevQueue nd_queue = QSIMPLEQ_HEAD_INITIALIZER(nd_queue);
+
  /***********************************************************/
  /* network device redirectors */
 

net/net.c int net_init_clients(Error **errp) · line 1579

Quoted 9 lines
net/net.c +6 -0
@@ -1559,6 +1571,19 @@ int net_init_clients(Error **errp)
 
      QTAILQ_INIT(&net_clients);
 
+    while (!QSIMPLEQ_EMPTY(&nd_queue)) {
+        NetdevQueueEntry *nd = QSIMPLEQ_FIRST(&nd_queue);
+
+        QSIMPLEQ_REMOVE_HEAD(&nd_queue, entry);
+        loc_push_restore(&nd->loc);
+        if (net_client_init1(nd->nd, nd->is_netdev, errp) < 0) {

I think you need to loc_pop() here.

net/net.c int net_init_clients(Error **errp) · line 1581

Quoted 2 lines
net/net.c +2 -0
+            return -1;
+        }
Quoted 6 lines

Since the only caller passes &error_fatal, I'd be tempted to ditch the @errp argument, and simply do

            net_client_init1(nd->nd, nd->is_netdev, &error_fatal);

It's what we do for -blockdev, -device, and -object.

I've added a patch to remove the @errp from the net_init_clients() arguments.

Quoted 32 lines
Quoted 19 lines
net/net.c +11 -0
+        loc_pop(&nd->loc);
+        qapi_free_Netdev(nd->nd);
+        g_free(nd);
+    }
+
      if (qemu_opts_foreach(qemu_find_opts("netdev"),
                            net_init_netdev, NULL, errp)) {
          return -1;
@@ -1575,8 +1600,37 @@ int net_init_clients(Error **errp)
      return 0;
  }
 
+/*
+ * netdev_is_modern() returns true when the backend needs to bypass
+ * qemu_opts_parse_noisily()
+ */
+static bool netdev_is_modern(const char *optarg)
+{

net/net.c int net_init_clients(Error **errp) · line 1618

Quoted 10 lines
net/net.c +8 -0
+    return false;
+}
+
  int net_client_parse(QemuOptsList *opts_list, const char *optarg)
  {
+    if (netdev_is_modern(optarg)) {
+            /*
+             * We need to bypass qemu_opts_parse_noisily() to accept
+             * new style object like addr.type=inet in SocketAddress
+             */

I'm not sure this will makes sense to future readers.

Quoted 1 line

What about "Use modern, more expressive syntax"?

Done.

Quoted 19 lines
Quoted 5 lines
net/net.c +4 -0
+            Visitor *v;
+            NetdevQueueEntry *nd;
+
+            v = qobject_input_visitor_new_str(optarg, "type",

net/net.c int net_init_clients(Error **errp) · line 1632

Quoted 10 lines
net/net.c +10 -0
+                                              &error_fatal);
+            nd = g_new(NetdevQueueEntry, 1);
+            visit_type_Netdev(v, NULL, &nd->nd, &error_fatal);
+            visit_free(v);
+            loc_save(&nd->loc);
+            nd->is_netdev = strcmp(opts_list->name, "netdev") == 0;
+
+            QSIMPLEQ_INSERT_TAIL(&nd_queue, nd, entry);
+            return 0;
+    }

Matches what we do for -blockdev, except we additionally have nd->is_netdev. We need it for calling net_client_init1().

Quoted 6 lines
Quoted 4 lines

If netdev_is_modern(optarg), then the only use of parameter @opts_list is opts_list->name in the initialization of nd->is_netdev.

There's a bit of code smell, I'm afraid.

I don't see what is the problem.

The function's signature

int net_client_parse(QemuOptsList *opts_list, const char *optarg)

suggests we're parsing @optarg into @opts_list.

We do only if !netdev_is_modern(optarg). In other words, the function's actual behavior doesn't match reasonable expectations based on signature. A function comment would mitigate.

If nd->is_netdev wasn't needed, we could code all this just like -blockdev. I think that would be simpler.

Quoted 10 lines
Quoted 4 lines

I believe @is_netdev needs to be true for -netdev, -nic, and netdev_add; false for -net.

Will we ever use the modern syntax with -net?

Yes, I think we should support the same syntax with -netdev and -net.

My first iteration was to pass is_netdev=true to net_client_init1() and Stefano reported a problem with "-net" with things like that:

Quoted 8 lines
     -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
Quoted 2 lines

Any chance we can deprecate -net?

Who can decide of that?

Thomas, I think you've done some work to replace use cases of -net. Do you know what's left?

Quoted 8 lines
Quoted 5 lines

net/net.c int net_init_clients(Error **errp) · line 1636

Quoted 4 lines
net/net.c +1 -0
+
      if (!qemu_opts_parse_noisily(opts_list, optarg, true)) {
          return -1;
      }

Thanks,
Laurent

#6 lore raw

RFC PATCH v2 3/8 qapi: net: add stream and dgram netdevs

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 netdev, multicast is detected according to the IP address type.
"listen" and "connect" modes are managed by stream netdev. An optional parameter "server" defines the mode (server by default)

---
 hmp-commands.hx |   2 +-
 net/clients.h   |   6 +
 net/dgram.c     | 630 ++++++++++++++++++++++++++++++++++++++++++++++++
 net/hub.c       |   2 +
 net/meson.build |   2 +
 net/net.c       |  24 +-
 net/stream.c    | 425 ++++++++++++++++++++++++++++++++
 qapi/net.json   |  38 ++-
 8 files changed, 1125 insertions(+), 4 deletions(-)
 create mode 100644 net/dgram.c
 create mode 100644 net/stream.c
hmp-commands.hx +1 -1
index 03e6a73d1f55..172dbab1dfed 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|stream|dgram|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 +6 -0
index 92f9b59aedce..c1b51d79b147 100644
@@ -40,6 +40,12 @@ 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_stream(const Netdev *netdev, const char *name,
+                    NetClientState *peer, Error **errp);
+
+int net_init_dgram(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/dgram.c new file +630 -0

630 lines not on this page · raw message

net/hub.c +2 -0
index 1375738bf121..67ca53485638 100644
@@ -313,6 +313,8 @@ 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_STREAM:
+            case NET_CLIENT_DRIVER_DGRAM:
             case NET_CLIENT_DRIVER_VDE:
             case NET_CLIENT_DRIVER_VHOST_USER:
                 has_host_dev = 1;
net/meson.build +2 -0
index c965e83b264b..2c8358baadb3 100644
@@ -13,6 +13,8 @@ softmmu_ss.add(files(
   'net.c',
   'queue.c',
   'socket.c',
+  'stream.c',
+  'dgram.c',
   'util.c',
 ))
 
net/net.c +23 -1
index 2aab7167316c..fd6b30a10c57 100644
@@ -1015,6 +1015,8 @@ static int (* const net_client_init_fun[NET_CLIENT_DRIVER__MAX])(
 #endif
         [NET_CLIENT_DRIVER_TAP]       = net_init_tap,
         [NET_CLIENT_DRIVER_SOCKET]    = net_init_socket,
+        [NET_CLIENT_DRIVER_STREAM]    = net_init_stream,
+        [NET_CLIENT_DRIVER_DGRAM]     = net_init_dgram,
 #ifdef CONFIG_VDE
         [NET_CLIENT_DRIVER_VDE]       = net_init_vde,
 #endif
@@ -1097,6 +1099,8 @@ void show_netdevs(void)
     int idx;
     const char *available_netdevs[] = {
         "socket",
+        "stream",
+        "dgram",
         "hubport",
         "tap",
 #ifdef CONFIG_SLIRP
@@ -1606,7 +1610,25 @@ int net_init_clients(Error **errp)
  */
 static bool netdev_is_modern(const char *optarg)
 {
-    return false;
+    static QemuOptsList dummy_opts = {
+        .name = "netdev",
+        .implied_opt_name = "type",
+        .head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head),
+        .desc = { { } },
+    };
+    const char *netdev;
+    QemuOpts *opts;
+    bool is_modern;
+
+    opts = qemu_opts_parse(&dummy_opts, optarg, true, &error_fatal);
+    netdev = qemu_opt_get(opts, "type");
+
+    is_modern = strcmp(netdev, "stream") == 0 ||
+                strcmp(netdev, "dgram") == 0;
+
+    qemu_opts_reset(&dummy_opts);
+
+    return is_modern;
 }
 
 int net_client_parse(QemuOptsList *opts_list, const char *optarg)
net/stream.c new file +425 -0

425 lines not on this page · raw message

qapi/net.json +36 -2
index b92f3f5fb444..eef288886e1b 100644
@@ -7,6 +7,7 @@
 ##
 
 { 'include': 'common.json' }
+{ 'include': 'sockets.json' }
 
 ##
 # @set_link:
@@ -452,6 +453,37 @@
     '*vhostdev':     'str',
     '*queues':       'int' } }
 
+##
+# @NetdevStreamOptions:
+#
+# Configuration info for stream socket netdev
+#
+# @addr: socket address to listen on (server=true)
+#        or connect to (server=false)
+# @server: create server socket (default: true)
+#
+# Since: 7.1
+##
+{ 'struct': 'NetdevStreamOptions',
+  'data': {
+    'addr':   'SocketAddress',
+    '*server': 'bool' } }
+
+##
+# @NetdevDgramOptions:
+#
+# Configuration info for datagram socket netdev.
+#
+# @remote: remote address
+# @local: local address
+#
+# Since: 7.1
+##
+{ 'struct': 'NetdevDgramOptions',
+  'data': {
+    '*local':  'SocketAddress',
+    '*remote': 'SocketAddress' } }
+
 ##
 # @NetClientDriver:
 #
@@ -462,8 +494,8 @@
 #        @vhost-vdpa since 5.1
 ##
 { 'enum': 'NetClientDriver',
-  'data': [ 'none', 'nic', 'user', 'tap', 'l2tpv3', 'socket', 'vde',
-            'bridge', 'hubport', 'netmap', 'vhost-user', 'vhost-vdpa' ] }
+  'data': [ 'none', 'nic', 'user', 'tap', 'l2tpv3', 'socket', 'stream', 'dgram',
+            'vde', 'bridge', 'hubport', 'netmap', 'vhost-user', 'vhost-vdpa' ] }
 
 ##
 # @Netdev:
@@ -487,6 +519,8 @@
     'tap':      'NetdevTapOptions',
     'l2tpv3':   'NetdevL2TPv3Options',
     'socket':   'NetdevSocketOptions',
+    'stream':   'NetdevStreamOptions',
+    'dgram':    'NetdevDgramOptions',
     'vde':      'NetdevVdeOptions',
     'bridge':   'NetdevBridgeOptions',
     'hubport':  'NetdevHubPortOptions',
Signature · 1 line
2.35.3
#7 lore raw

Re: [RFC PATCH v2 3/8] qapi: net: add stream and dgram netdevs

Quoted 27 lines · 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 netdev, multicast is detected according to the IP address type.
"listen" and "connect" modes are managed by stream netdev. An optional parameter "server" defines the mode (server by default)

---
 hmp-commands.hx |   2 +-
 net/clients.h   |   6 +
 net/dgram.c     | 630 ++++++++++++++++++++++++++++++++++++++++++++++++
 net/hub.c       |   2 +
 net/meson.build |   2 +
 net/net.c       |  24 +-
 net/stream.c    | 425 ++++++++++++++++++++++++++++++++
 qapi/net.json   |  38 ++-
 8 files changed, 1125 insertions(+), 4 deletions(-)
 create mode 100644 net/dgram.c
 create mode 100644 net/stream.c
hmp-commands.hx +0 -0
index 03e6a73d1f55..172dbab1dfed 100644

hmp-commands.hx ERST · line 1275

Quoted 8 lines
hmp-commands.hx +1 -1
@@ -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|stream|dgram|vde|bridge|hubport|netmap|vhost-user],id=str[,prop=value][,...]",
         .help       = "add host network device",
         .cmd        = hmp_netdev_add,
         .command_completion = netdev_add_completion,

Does qemu-options.hx need an update, too?

Quoted 36 lines
net/clients.h +6 -0
index 92f9b59aedce..c1b51d79b147 100644
@@ -40,6 +40,12 @@ 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_stream(const Netdev *netdev, const char *name,
+                    NetClientState *peer, Error **errp);
+
+int net_init_dgram(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/dgram.c new file +13 -0
new file mode 100644
index 000000000000..aa4240501ed0
@@ -0,0 +1,630 @@
+/*
+ * QEMU System Emulator
+ *
+ * Copyright (c) 2003-2008 Fabrice Bellard
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in

net/dgram.c line 23

Quoted 10 lines
net/dgram.c new file +10 -0
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */

Blank line here, please.

Why not GPLv2+?

Quoted 1 line
net/dgram.c new file +1 -0
+#include "qemu/osdep.h"

[...]

Quoted 32 lines
net/net.c +9 -1
index 2aab7167316c..fd6b30a10c57 100644
@@ -1015,6 +1015,8 @@ static int (* const net_client_init_fun[NET_CLIENT_DRIVER__MAX])(
 #endif
         [NET_CLIENT_DRIVER_TAP]       = net_init_tap,
         [NET_CLIENT_DRIVER_SOCKET]    = net_init_socket,
+        [NET_CLIENT_DRIVER_STREAM]    = net_init_stream,
+        [NET_CLIENT_DRIVER_DGRAM]     = net_init_dgram,
 #ifdef CONFIG_VDE
         [NET_CLIENT_DRIVER_VDE]       = net_init_vde,
 #endif
@@ -1097,6 +1099,8 @@ void show_netdevs(void)
     int idx;
     const char *available_netdevs[] = {
         "socket",
+        "stream",
+        "dgram",
         "hubport",
         "tap",
 #ifdef CONFIG_SLIRP
@@ -1606,7 +1610,25 @@ int net_init_clients(Error **errp)
  */
 static bool netdev_is_modern(const char *optarg)
 {
-    return false;
+    static QemuOptsList dummy_opts = {
+        .name = "netdev",
+        .implied_opt_name = "type",
+        .head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head),
+        .desc = { { } },

net/net.c int net_init_clients(Error **errp) · line 1627

Quoted 10 lines
net/net.c +10 -0
+    };
+    const char *netdev;
+    QemuOpts *opts;
+    bool is_modern;
+
+    opts = qemu_opts_parse(&dummy_opts, optarg, true, &error_fatal);
+    netdev = qemu_opt_get(opts, "type");
+
+    is_modern = strcmp(netdev, "stream") == 0 ||
+                strcmp(netdev, "dgram") == 0;

Crashes when user neglects to pass "type".

net/net.c int net_init_clients(Error **errp) · line 1632

Quoted 5 lines
net/net.c +4 -0
+
+    qemu_opts_reset(&dummy_opts);
+
+    return is_modern;
 }

Reminder: netdev_is_modern() governs whether to use QemuOpts + opts visitor or qobject_input_visitor_new_str().

To decide, it uses QemuOpts with a dummy QemuOptsList.

Since we parse errors are fatal here, new syntax must also parse fine as QemuOpts. Undesirable, I think.

Cleaner, I think:

    args = keyval_parse(optarg, "type", NULL, NULL);
    if (!args) {
        return false;
    }
    type = qdict_get_try_str(args, "type");
    return !g_strcmp0(type, "dgram") || !g_strcmp0(type, "stream");

Not even compile-tested.

Still probematic: syntax error reporting. When keyval_parse() fails here, we use QemuOpts, and therefore get QemuOpts syntax errors. I fear that could be quite confusing.

Quoted 21 lines
net/net.c +0 -0
 int net_client_parse(QemuOptsList *opts_list, const char *optarg)
net/stream.c new file +13 -0
new file mode 100644
index 000000000000..fdc97ee43a56
@@ -0,0 +1,425 @@
+/*
+ * QEMU System Emulator
+ *
+ * Copyright (c) 2003-2008 Fabrice Bellard
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in

net/stream.c line 23

Quoted 10 lines
net/stream.c new file +10 -0
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */

Blank line here, please.

Why not GPLv2+?

Quoted 1 line
net/stream.c new file +1 -0
+#include "qemu/osdep.h"

[...]

Quoted 29 lines
qapi/net.json +14 -0
index b92f3f5fb444..eef288886e1b 100644
@@ -7,6 +7,7 @@
 ##
 
 { 'include': 'common.json' }
+{ 'include': 'sockets.json' }
 
 ##
 # @set_link:
@@ -452,6 +453,37 @@
     '*vhostdev':     'str',
     '*queues':       'int' } }
 
+##
+# @NetdevStreamOptions:
+#
+# Configuration info for stream socket netdev
+#
+# @addr: socket address to listen on (server=true)
+#        or connect to (server=false)
+# @server: create server socket (default: true)
+#
+# Since: 7.1
+##
+{ 'struct': 'NetdevStreamOptions',
+  'data': {

qapi/net.json line 478

Quoted 10 lines
qapi/net.json +10 -0
+    'addr':   'SocketAddress',
+    '*server': 'bool' } }
+
+##
+# @NetdevDgramOptions:
+#
+# Configuration info for datagram socket netdev.
+#
+# @remote: remote address
+# @local: local address

Defaults?

Quoted 12 lines
qapi/net.json +8 -0
+#
+# Since: 7.1
+##
+{ 'struct': 'NetdevDgramOptions',
+  'data': {
+    '*local':  'SocketAddress',
+    '*remote': 'SocketAddress' } }
+
 ##
 # @NetClientDriver:
 #

qapi/net.json line 498

Quoted 7 lines
qapi/net.json +2 -2
@@ -462,8 +494,8 @@
 #        @vhost-vdpa since 5.1
 ##
 { 'enum': 'NetClientDriver',
-  'data': [ 'none', 'nic', 'user', 'tap', 'l2tpv3', 'socket', 'vde',
-            'bridge', 'hubport', 'netmap', 'vhost-user', 'vhost-vdpa' ] }
+  'data': [ 'none', 'nic', 'user', 'tap', 'l2tpv3', 'socket', 'stream', 'dgram',
+            'vde', 'bridge', 'hubport', 'netmap', 'vhost-user', 'vhost-vdpa' ] }

Long lines.

Quoted 12 lines
qapi/net.json +2 -0
 ##
 # @Netdev:
@@ -487,6 +519,8 @@
     'tap':      'NetdevTapOptions',
     'l2tpv3':   'NetdevL2TPv3Options',
     'socket':   'NetdevSocketOptions',
+    'stream':   'NetdevStreamOptions',
+    'dgram':    'NetdevDgramOptions',
     'vde':      'NetdevVdeOptions',
     'bridge':   'NetdevBridgeOptions',
     'hubport':  'NetdevHubPortOptions',
#8 lore raw

Re: [RFC PATCH v2 3/8] qapi: net: add stream and dgram netdevs

Quoted 24 lines · Markus Armbruster
Quoted 22 lines · 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 netdev, multicast is detected according to the IP address type.
"listen" and "connect" modes are managed by stream netdev. An optional parameter "server" defines the mode (server by default)

---
  hmp-commands.hx |   2 +-
  net/clients.h   |   6 +
  net/dgram.c     | 630 ++++++++++++++++++++++++++++++++++++++++++++++++
  net/hub.c       |   2 +
  net/meson.build |   2 +
  net/net.c       |  24 +-
  net/stream.c    | 425 ++++++++++++++++++++++++++++++++
  qapi/net.json   |  38 ++-
  8 files changed, 1125 insertions(+), 4 deletions(-)

create mode 100644 net/dgram.c
create mode 100644 net/stream.c

Quoted 15 lines

hmp-commands.hx ERST · line 1275

Quoted 13 lines
hmp-commands.hx +1 -1
index 03e6a73d1f55..172dbab1dfed 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|stream|dgram|vde|bridge|hubport|netmap|vhost-user],id=str[,prop=value][,...]",
          .help       = "add host network device",
          .cmd        = hmp_netdev_add,
          .command_completion = netdev_add_completion,

Does qemu-options.hx need an update, too?

Done

Quoted 50 lines
Quoted 37 lines
net/clients.h +6 -0
index 92f9b59aedce..c1b51d79b147 100644
@@ -40,6 +40,12 @@ 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_stream(const Netdev *netdev, const char *name,
+                    NetClientState *peer, Error **errp);
+
+int net_init_dgram(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/dgram.c new file +13 -0
new file mode 100644
index 000000000000..aa4240501ed0
@@ -0,0 +1,630 @@
+/*
+ * QEMU System Emulator
+ *
+ * Copyright (c) 2003-2008 Fabrice Bellard
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in

net/dgram.c line 23

Quoted 10 lines
net/dgram.c new file +10 -0
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */

Blank line here, please.

Quoted 1 line

Why not GPLv2+?

I've kept the original text copied from net/socket.c, but I can move this to GPL2+

Quoted 41 lines
Quoted 2 lines
net/dgram.c new file +1 -0
+#include "qemu/osdep.h"

[...]

Quoted 36 lines
net/net.c +13 -1
index 2aab7167316c..fd6b30a10c57 100644
@@ -1015,6 +1015,8 @@ static int (* const net_client_init_fun[NET_CLIENT_DRIVER__MAX])(
  #endif
          [NET_CLIENT_DRIVER_TAP]       = net_init_tap,
          [NET_CLIENT_DRIVER_SOCKET]    = net_init_socket,
+        [NET_CLIENT_DRIVER_STREAM]    = net_init_stream,
+        [NET_CLIENT_DRIVER_DGRAM]     = net_init_dgram,
  #ifdef CONFIG_VDE
          [NET_CLIENT_DRIVER_VDE]       = net_init_vde,
  #endif
@@ -1097,6 +1099,8 @@ void show_netdevs(void)
      int idx;
      const char *available_netdevs[] = {
          "socket",
+        "stream",
+        "dgram",
          "hubport",
          "tap",
  #ifdef CONFIG_SLIRP
@@ -1606,7 +1610,25 @@ int net_init_clients(Error **errp)
   */
  static bool netdev_is_modern(const char *optarg)
  {
-    return false;
+    static QemuOptsList dummy_opts = {
+        .name = "netdev",
+        .implied_opt_name = "type",
+        .head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head),
+        .desc = { { } },
+    };
+    const char *netdev;
+    QemuOpts *opts;
+    bool is_modern;
Quoted 8 lines

net/net.c int net_init_clients(Error **errp) · line 1627

Quoted 6 lines
net/net.c +6 -0
+
+    opts = qemu_opts_parse(&dummy_opts, optarg, true, &error_fatal);
+    netdev = qemu_opt_get(opts, "type");
+
+    is_modern = strcmp(netdev, "stream") == 0 ||
+                strcmp(netdev, "dgram") == 0;

Crashes when user neglects to pass "type".

I think "type" is always passed because of the '.implied_opt_name = "type"'. Am I wrong?

Quoted 16 lines

net/net.c int net_init_clients(Error **errp) · line 1632

Quoted 5 lines
net/net.c +4 -0
+
+    qemu_opts_reset(&dummy_opts);
+
+    return is_modern;
  }

Reminder: netdev_is_modern() governs whether to use QemuOpts + opts visitor or qobject_input_visitor_new_str().

To decide, it uses QemuOpts with a dummy QemuOptsList.

Since we parse errors are fatal here, new syntax must also parse fine as QemuOpts. Undesirable, I think.

Cleaner, I think:

Quoted 8 lines
     args = keyval_parse(optarg, "type", NULL, NULL);
     if (!args) {
         return false;
     }
     type = qdict_get_try_str(args, "type");
     return !g_strcmp0(type, "dgram") || !g_strcmp0(type, "stream");

Not even compile-tested.

ok, I try that.

Quoted 38 lines

Still probematic: syntax error reporting. When keyval_parse() fails here, we use QemuOpts, and therefore get QemuOpts syntax errors. I fear that could be quite confusing.

Quoted 21 lines
net/net.c +0 -0
  int net_client_parse(QemuOptsList *opts_list, const char *optarg)
net/stream.c new file +13 -0
new file mode 100644
index 000000000000..fdc97ee43a56
@@ -0,0 +1,425 @@
+/*
+ * QEMU System Emulator
+ *
+ * Copyright (c) 2003-2008 Fabrice Bellard
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in

net/stream.c line 23

Quoted 10 lines
net/stream.c new file +10 -0
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */

Blank line here, please.

Quoted 1 line

Why not GPLv2+?

As above.

Quoted 38 lines
Quoted 2 lines
net/stream.c new file +1 -0
+#include "qemu/osdep.h"

[...]

Quoted 33 lines
qapi/net.json +18 -0
index b92f3f5fb444..eef288886e1b 100644
@@ -7,6 +7,7 @@
  ##
 
  { 'include': 'common.json' }
+{ 'include': 'sockets.json' }
 
  ##
  # @set_link:
@@ -452,6 +453,37 @@
      '*vhostdev':     'str',
      '*queues':       'int' } }
 
+##
+# @NetdevStreamOptions:
+#
+# Configuration info for stream socket netdev
+#
+# @addr: socket address to listen on (server=true)
+#        or connect to (server=false)
+# @server: create server socket (default: true)
+#
+# Since: 7.1
+##
+{ 'struct': 'NetdevStreamOptions',
+  'data': {
+    'addr':   'SocketAddress',
+    '*server': 'bool' } }
+
+##
Quoted 8 lines

qapi/net.json line 478

Quoted 6 lines
qapi/net.json +6 -0
+# @NetdevDgramOptions:
+#
+# Configuration info for datagram socket netdev.
+#
+# @remote: remote address
+# @local: local address

Defaults?

We can't have a default because for multicast default is remoTe, and for unicast default is local.

Quoted 14 lines
Quoted 14 lines
qapi/net.json +8 -0
+#
+# Since: 7.1
+##
+{ 'struct': 'NetdevDgramOptions',
+  'data': {
+    '*local':  'SocketAddress',
+    '*remote': 'SocketAddress' } }
+
  ##
  # @NetClientDriver:
  #
@@ -462,8 +494,8 @@
  #        @vhost-vdpa since 5.1
Quoted 8 lines

qapi/net.json line 498

Quoted 6 lines
qapi/net.json +2 -2
  ##
  { 'enum': 'NetClientDriver',
-  'data': [ 'none', 'nic', 'user', 'tap', 'l2tpv3', 'socket', 'vde',
-            'bridge', 'hubport', 'netmap', 'vhost-user', 'vhost-vdpa' ] }
+  'data': [ 'none', 'nic', 'user', 'tap', 'l2tpv3', 'socket', 'stream', 'dgram',
+            'vde', 'bridge', 'hubport', 'netmap', 'vhost-user', 'vhost-vdpa' ] }

Long lines.

ok

Quoted 13 lines

qapi/net.json line 526

Quoted 12 lines
qapi/net.json +2 -0
  ##
  # @Netdev:
@@ -487,6 +519,8 @@
      'tap':      'NetdevTapOptions',
      'l2tpv3':   'NetdevL2TPv3Options',
      'socket':   'NetdevSocketOptions',
+    'stream':   'NetdevStreamOptions',
+    'dgram':    'NetdevDgramOptions',
      'vde':      'NetdevVdeOptions',
      'bridge':   'NetdevBridgeOptions',
      'hubport':  'NetdevHubPortOptions',

Thanks,
Laurent

#9 lore raw

Re: [RFC PATCH v2 3/8] qapi: net: add stream and dgram netdevs

Quoted 93 lines · Laurent Vivier
Quoted 24 lines · Markus Armbruster
Quoted 22 lines · 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 netdev, multicast is detected according to the IP address type.
"listen" and "connect" modes are managed by stream netdev. An optional parameter "server" defines the mode (server by default)

---
  hmp-commands.hx |   2 +-
  net/clients.h   |   6 +
  net/dgram.c     | 630 ++++++++++++++++++++++++++++++++++++++++++++++++
  net/hub.c       |   2 +
  net/meson.build |   2 +
  net/net.c       |  24 +-
  net/stream.c    | 425 ++++++++++++++++++++++++++++++++
  qapi/net.json   |  38 ++-
  8 files changed, 1125 insertions(+), 4 deletions(-)

create mode 100644 net/dgram.c
create mode 100644 net/stream.c

Quoted 15 lines

hmp-commands.hx ERST · line 1275

Quoted 13 lines
hmp-commands.hx +1 -1
index 03e6a73d1f55..172dbab1dfed 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|stream|dgram|vde|bridge|hubport|netmap|vhost-user],id=str[,prop=value][,...]",
          .help       = "add host network device",
          .cmd        = hmp_netdev_add,
          .command_completion = netdev_add_completion,

Does qemu-options.hx need an update, too?

Done

Quoted 50 lines
Quoted 37 lines
net/clients.h +6 -0
index 92f9b59aedce..c1b51d79b147 100644
@@ -40,6 +40,12 @@ 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_stream(const Netdev *netdev, const char *name,
+                    NetClientState *peer, Error **errp);
+
+int net_init_dgram(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/dgram.c new file +13 -0
new file mode 100644
index 000000000000..aa4240501ed0
@@ -0,0 +1,630 @@
+/*
+ * QEMU System Emulator
+ *
+ * Copyright (c) 2003-2008 Fabrice Bellard
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in

net/dgram.c line 23

Quoted 10 lines
net/dgram.c new file +10 -0
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */

Blank line here, please.

Quoted 3 lines
Quoted 1 line

Why not GPLv2+?

I've kept the original text copied from net/socket.c, but I can move this to GPL2+

If the file's contents is derived from net/socket.c, copying the legalese from there makes sense.

Quoted 49 lines
Quoted 40 lines
Quoted 1 line
net/dgram.c new file +1 -0
+#include "qemu/osdep.h"

[...]

Quoted 36 lines
net/net.c +13 -1
index 2aab7167316c..fd6b30a10c57 100644
@@ -1015,6 +1015,8 @@ static int (* const net_client_init_fun[NET_CLIENT_DRIVER__MAX])(
  #endif
          [NET_CLIENT_DRIVER_TAP]       = net_init_tap,
          [NET_CLIENT_DRIVER_SOCKET]    = net_init_socket,
+        [NET_CLIENT_DRIVER_STREAM]    = net_init_stream,
+        [NET_CLIENT_DRIVER_DGRAM]     = net_init_dgram,
  #ifdef CONFIG_VDE
          [NET_CLIENT_DRIVER_VDE]       = net_init_vde,
  #endif
@@ -1097,6 +1099,8 @@ void show_netdevs(void)
      int idx;
      const char *available_netdevs[] = {
          "socket",
+        "stream",
+        "dgram",
          "hubport",
          "tap",
  #ifdef CONFIG_SLIRP
@@ -1606,7 +1610,25 @@ int net_init_clients(Error **errp)
   */
  static bool netdev_is_modern(const char *optarg)
  {
-    return false;
+    static QemuOptsList dummy_opts = {
+        .name = "netdev",
+        .implied_opt_name = "type",
+        .head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head),
+        .desc = { { } },
+    };
+    const char *netdev;
+    QemuOpts *opts;
+    bool is_modern;
Quoted 8 lines

net/net.c int net_init_clients(Error **errp) · line 1627

Quoted 6 lines
net/net.c +6 -0
+
+    opts = qemu_opts_parse(&dummy_opts, optarg, true, &error_fatal);
+    netdev = qemu_opt_get(opts, "type");
+
+    is_modern = strcmp(netdev, "stream") == 0 ||
+                strcmp(netdev, "dgram") == 0;

Crashes when user neglects to pass "type".

Quoted 1 line

I think "type" is always passed because of the '.implied_opt_name = "type"'. Am I wrong?

.implied_opt_name = "type" lets you shorten "type=T,..." to "T,...". It doesn't make key "type" mandatory. "-netdev id=foo" is still permitted. Even "-netdev ''" is.

Quoted 116 lines
Quoted 16 lines

net/net.c int net_init_clients(Error **errp) · line 1632

Quoted 5 lines
net/net.c +4 -0
+
+    qemu_opts_reset(&dummy_opts);
+
+    return is_modern;
  }

Reminder: netdev_is_modern() governs whether to use QemuOpts + opts visitor or qobject_input_visitor_new_str().

To decide, it uses QemuOpts with a dummy QemuOptsList.

Since we parse errors are fatal here, new syntax must also parse fine as QemuOpts. Undesirable, I think.

Cleaner, I think:

Quoted 8 lines
     args = keyval_parse(optarg, "type", NULL, NULL);
     if (!args) {
         return false;
     }
     type = qdict_get_try_str(args, "type");
     return !g_strcmp0(type, "dgram") || !g_strcmp0(type, "stream");

Not even compile-tested.

ok, I try that.

Quoted 38 lines

Still probematic: syntax error reporting. When keyval_parse() fails here, we use QemuOpts, and therefore get QemuOpts syntax errors. I fear that could be quite confusing.

Quoted 21 lines
net/net.c +0 -0
  int net_client_parse(QemuOptsList *opts_list, const char *optarg)
net/stream.c new file +13 -0
new file mode 100644
index 000000000000..fdc97ee43a56
@@ -0,0 +1,425 @@
+/*
+ * QEMU System Emulator
+ *
+ * Copyright (c) 2003-2008 Fabrice Bellard
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in

net/stream.c line 23

Quoted 10 lines
net/stream.c new file +10 -0
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */

Blank line here, please.

Quoted 1 line

Why not GPLv2+?

As above.

Quoted 38 lines
Quoted 2 lines
net/stream.c new file +1 -0
+#include "qemu/osdep.h"

[...]

Quoted 33 lines
qapi/net.json +18 -0
index b92f3f5fb444..eef288886e1b 100644
@@ -7,6 +7,7 @@
  ##
 
  { 'include': 'common.json' }
+{ 'include': 'sockets.json' }
 
  ##
  # @set_link:
@@ -452,6 +453,37 @@
      '*vhostdev':     'str',
      '*queues':       'int' } }
 
+##
+# @NetdevStreamOptions:
+#
+# Configuration info for stream socket netdev
+#
+# @addr: socket address to listen on (server=true)
+#        or connect to (server=false)
+# @server: create server socket (default: true)
+#
+# Since: 7.1
+##
+{ 'struct': 'NetdevStreamOptions',
+  'data': {
+    'addr':   'SocketAddress',
+    '*server': 'bool' } }
+
+##
Quoted 8 lines

qapi/net.json line 478

Quoted 6 lines
qapi/net.json +6 -0
+# @NetdevDgramOptions:
+#
+# Configuration info for datagram socket netdev.
+#
+# @remote: remote address
+# @local: local address

Defaults?

Quoted 2 lines

We can't have a default because for multicast default is remoTe, and for unicast default is local.

Well, the members are optional, so there must be some default behavior, which may or may not correspond to a single default value. Regardless, what happens when a member is absent ought to be documented.

Quoted 39 lines
Quoted 14 lines
Quoted 14 lines
qapi/net.json +8 -0
+#
+# Since: 7.1
+##
+{ 'struct': 'NetdevDgramOptions',
+  'data': {
+    '*local':  'SocketAddress',
+    '*remote': 'SocketAddress' } }
+
  ##
  # @NetClientDriver:
  #
@@ -462,8 +494,8 @@
  #        @vhost-vdpa since 5.1
Quoted 8 lines

qapi/net.json line 498

Quoted 6 lines
qapi/net.json +2 -2
  ##
  { 'enum': 'NetClientDriver',
-  'data': [ 'none', 'nic', 'user', 'tap', 'l2tpv3', 'socket', 'vde',
-            'bridge', 'hubport', 'netmap', 'vhost-user', 'vhost-vdpa' ] }
+  'data': [ 'none', 'nic', 'user', 'tap', 'l2tpv3', 'socket', 'stream', 'dgram',
+            'vde', 'bridge', 'hubport', 'netmap', 'vhost-user', 'vhost-vdpa' ] }

Long lines.

ok

Quoted 13 lines

qapi/net.json line 526

Quoted 12 lines
qapi/net.json +2 -0
  ##
  # @Netdev:
@@ -487,6 +519,8 @@
      'tap':      'NetdevTapOptions',
      'l2tpv3':   'NetdevL2TPv3Options',
      'socket':   'NetdevSocketOptions',
+    'stream':   'NetdevStreamOptions',
+    'dgram':    'NetdevDgramOptions',
      'vde':      'NetdevVdeOptions',
      'bridge':   'NetdevBridgeOptions',
      'hubport':  'NetdevHubPortOptions',
Quoted 2 lines

Thanks,
Laurent

You're welcome!

#10 lore raw

Re: [RFC PATCH v2 3/8] qapi: net: add stream and dgram netdevs

Quoted 27 lines · Markus Armbruster
Quoted 25 lines · Laurent Vivier
Quoted 24 lines · Markus Armbruster
Quoted 22 lines · 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 netdev, multicast is detected according to the IP address type.
"listen" and "connect" modes are managed by stream netdev. An optional parameter "server" defines the mode (server by default)

---
   hmp-commands.hx |   2 +-
   net/clients.h   |   6 +
   net/dgram.c     | 630 ++++++++++++++++++++++++++++++++++++++++++++++++
   net/hub.c       |   2 +
   net/meson.build |   2 +
   net/net.c       |  24 +-
   net/stream.c    | 425 ++++++++++++++++++++++++++++++++
   qapi/net.json   |  38 ++-
   8 files changed, 1125 insertions(+), 4 deletions(-)

create mode 100644 net/dgram.c
create mode 100644 net/stream.c

...

Quoted 45 lines
Quoted 45 lines
Quoted 44 lines
Quoted 32 lines
net/net.c +9 -1
index 2aab7167316c..fd6b30a10c57 100644
@@ -1015,6 +1015,8 @@ static int (* const net_client_init_fun[NET_CLIENT_DRIVER__MAX])(
   #endif
           [NET_CLIENT_DRIVER_TAP]       = net_init_tap,
           [NET_CLIENT_DRIVER_SOCKET]    = net_init_socket,
+        [NET_CLIENT_DRIVER_STREAM]    = net_init_stream,
+        [NET_CLIENT_DRIVER_DGRAM]     = net_init_dgram,
   #ifdef CONFIG_VDE
           [NET_CLIENT_DRIVER_VDE]       = net_init_vde,
   #endif
@@ -1097,6 +1099,8 @@ void show_netdevs(void)
       int idx;
       const char *available_netdevs[] = {
           "socket",
+        "stream",
+        "dgram",
           "hubport",
           "tap",
   #ifdef CONFIG_SLIRP
@@ -1606,7 +1610,25 @@ int net_init_clients(Error **errp)
    */
   static bool netdev_is_modern(const char *optarg)
   {
-    return false;
+    static QemuOptsList dummy_opts = {
+        .name = "netdev",
+        .implied_opt_name = "type",
+        .head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head),
+        .desc = { { } },

net/net.c int net_init_clients(Error **errp) · line 1627

Quoted 10 lines
net/net.c +10 -0
+    };
+    const char *netdev;
+    QemuOpts *opts;
+    bool is_modern;
+
+    opts = qemu_opts_parse(&dummy_opts, optarg, true, &error_fatal);
+    netdev = qemu_opt_get(opts, "type");
+
+    is_modern = strcmp(netdev, "stream") == 0 ||
+                strcmp(netdev, "dgram") == 0;

Crashes when user neglects to pass "type".

Quoted 5 lines
Quoted 1 line

I think "type" is always passed because of the '.implied_opt_name = "type"'. Am I wrong?

.implied_opt_name = "type" lets you shorten "type=T,..." to "T,...". It doesn't make key "type" mandatory. "-netdev id=foo" is still permitted. Even "-netdev ''" is.

In fact type is checked before by QAPI definition:

{ 'union': 'Netdev',
'base': { 'id': 'str', 'type': 'NetClientDriver' }, 'discriminator': 'type',
...

As it's the discriminator it must be there.

  $ qemu-system-x86_64 -netdev id=foo
  qemu-system-x86_64: -netdev id=foo: Parameter 'type' is missing

...

Quoted 42 lines
Quoted 42 lines
Quoted 41 lines
Quoted 29 lines
qapi/net.json +14 -0
index b92f3f5fb444..eef288886e1b 100644
@@ -7,6 +7,7 @@
   ##
 
   { 'include': 'common.json' }
+{ 'include': 'sockets.json' }
 
   ##
   # @set_link:
@@ -452,6 +453,37 @@
       '*vhostdev':     'str',
       '*queues':       'int' } }
 
+##
+# @NetdevStreamOptions:
+#
+# Configuration info for stream socket netdev
+#
+# @addr: socket address to listen on (server=true)
+#        or connect to (server=false)
+# @server: create server socket (default: true)
+#
+# Since: 7.1
+##
+{ 'struct': 'NetdevStreamOptions',
+  'data': {

qapi/net.json line 478

Quoted 10 lines
qapi/net.json +10 -0
+    'addr':   'SocketAddress',
+    '*server': 'bool' } }
+
+##
+# @NetdevDgramOptions:
+#
+# Configuration info for datagram socket netdev.
+#
+# @remote: remote address
+# @local: local address

Defaults?

Quoted 6 lines
Quoted 2 lines

We can't have a default because for multicast default is remoTe, and for unicast default is local.

Well, the members are optional, so there must be some default behavior, which may or may not correspond to a single default value. Regardless, what happens when a member is absent ought to be documented.

The code checks there is at least one of these options and reports an error if not.

if remote address is present and it's a multicast address, local address is optional.

otherwise local address is required and remote address is optional. I've updated qemu-options.hx with that syntax.

Thanks,
Laurent

#11 lore raw

Re: [RFC PATCH v2 3/8] qapi: net: add stream and dgram netdevs

Quoted 88 lines · Laurent Vivier
Quoted 27 lines · Markus Armbruster
Quoted 25 lines · Laurent Vivier
Quoted 24 lines · Markus Armbruster
Quoted 22 lines · 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 netdev, multicast is detected according to the IP address type.
"listen" and "connect" modes are managed by stream netdev. An optional parameter "server" defines the mode (server by default)

---
   hmp-commands.hx |   2 +-
   net/clients.h   |   6 +
   net/dgram.c     | 630 ++++++++++++++++++++++++++++++++++++++++++++++++
   net/hub.c       |   2 +
   net/meson.build |   2 +
   net/net.c       |  24 +-
   net/stream.c    | 425 ++++++++++++++++++++++++++++++++
   qapi/net.json   |  38 ++-
   8 files changed, 1125 insertions(+), 4 deletions(-)

create mode 100644 net/dgram.c
create mode 100644 net/stream.c

...

Quoted 45 lines
Quoted 45 lines
Quoted 44 lines
Quoted 32 lines
net/net.c +9 -1
index 2aab7167316c..fd6b30a10c57 100644
@@ -1015,6 +1015,8 @@ static int (* const net_client_init_fun[NET_CLIENT_DRIVER__MAX])(
   #endif
           [NET_CLIENT_DRIVER_TAP]       = net_init_tap,
           [NET_CLIENT_DRIVER_SOCKET]    = net_init_socket,
+        [NET_CLIENT_DRIVER_STREAM]    = net_init_stream,
+        [NET_CLIENT_DRIVER_DGRAM]     = net_init_dgram,
   #ifdef CONFIG_VDE
           [NET_CLIENT_DRIVER_VDE]       = net_init_vde,
   #endif
@@ -1097,6 +1099,8 @@ void show_netdevs(void)
       int idx;
       const char *available_netdevs[] = {
           "socket",
+        "stream",
+        "dgram",
           "hubport",
           "tap",
   #ifdef CONFIG_SLIRP
@@ -1606,7 +1610,25 @@ int net_init_clients(Error **errp)
    */
   static bool netdev_is_modern(const char *optarg)
   {
-    return false;
+    static QemuOptsList dummy_opts = {
+        .name = "netdev",
+        .implied_opt_name = "type",
+        .head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head),
+        .desc = { { } },

net/net.c int net_init_clients(Error **errp) · line 1627

Quoted 10 lines
net/net.c +10 -0
+    };
+    const char *netdev;
+    QemuOpts *opts;
+    bool is_modern;
+
+    opts = qemu_opts_parse(&dummy_opts, optarg, true, &error_fatal);
+    netdev = qemu_opt_get(opts, "type");
+
+    is_modern = strcmp(netdev, "stream") == 0 ||
+                strcmp(netdev, "dgram") == 0;

Crashes when user neglects to pass "type".

Quoted 5 lines
Quoted 1 line

I think "type" is always passed because of the '.implied_opt_name = "type"'. Am I wrong?

.implied_opt_name = "type" lets you shorten "type=T,..." to "T,...". It doesn't make key "type" mandatory. "-netdev id=foo" is still permitted. Even "-netdev ''" is.

In fact type is checked before by QAPI definition:

{ 'union': 'Netdev',
'base': { 'id': 'str', 'type': 'NetClientDriver' }, 'discriminator': 'type',
...

Quoted 4 lines

As it's the discriminator it must be there.

   $ qemu-system-x86_64 -netdev id=foo
   qemu-system-x86_64: -netdev id=foo: Parameter 'type' is missing

It does crash for me:

    (gdb) bt
    #0  0x00007ffff4d25dcb in __strcmp_avx2 () at /lib64/libc.so.6
    #1  0x0000555555b4574b in netdev_is_modern (optarg=0x7fffffffe2ae "id=foo")
        at ../net/net.c:1626
    #2  0x0000555555b457ad in net_client_parse
        (opts_list=0x555556563780 <qemu_netdev_opts>, optarg=0x7fffffffe2ae "id=foo") at ../net/net.c:1636
    #3  0x0000555555ad98de in qemu_init (argc=3, argv=0x7fffffffdf08, envp=0x0)
        at ../softmmu/vl.c:2901
    #4  0x0000555555842c01 in qemu_main (argc=3, argv=0x7fffffffdf08, envp=0x0)
        at ../softmmu/main.c:35
    #5  0x0000555555842c37 in main (argc=3, argv=0x7fffffffdf08)
        at ../softmmu/main.c:45
    (gdb) up
    #1  0x0000555555b4574b in netdev_is_modern (optarg=0x7fffffffe2ae "id=foo")
        at ../net/net.c:1626
    1626	    is_modern = strcmp(netdev, "stream") == 0 ||
    (gdb) p netdev
    $1 = 0x0

This is

     https://github.com/patchew-project/qemu tags/patchew/[email protected] 

I suspect you tested with your v3, which doesn't crash for me, either.

[...]

#12 lore raw

Re: [RFC PATCH v2 3/8] qapi: net: add stream and dgram netdevs

Quoted 116 lines · Markus Armbruster
Quoted 84 lines · Laurent Vivier
Quoted 27 lines · Markus Armbruster
Quoted 25 lines · Laurent Vivier
Quoted 24 lines · Markus Armbruster
Quoted 22 lines · 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 netdev, multicast is detected according to the IP address type.
"listen" and "connect" modes are managed by stream netdev. An optional parameter "server" defines the mode (server by default)

---
    hmp-commands.hx |   2 +-
    net/clients.h   |   6 +
    net/dgram.c     | 630 ++++++++++++++++++++++++++++++++++++++++++++++++
    net/hub.c       |   2 +
    net/meson.build |   2 +
    net/net.c       |  24 +-
    net/stream.c    | 425 ++++++++++++++++++++++++++++++++
    qapi/net.json   |  38 ++-
    8 files changed, 1125 insertions(+), 4 deletions(-)
    create mode 100644 net/dgram.c
    create mode 100644 net/stream.c

...

Quoted 45 lines
Quoted 45 lines
Quoted 44 lines
Quoted 32 lines
net/net.c +9 -1
index 2aab7167316c..fd6b30a10c57 100644
@@ -1015,6 +1015,8 @@ static int (* const net_client_init_fun[NET_CLIENT_DRIVER__MAX])(
    #endif
            [NET_CLIENT_DRIVER_TAP]       = net_init_tap,
            [NET_CLIENT_DRIVER_SOCKET]    = net_init_socket,
+        [NET_CLIENT_DRIVER_STREAM]    = net_init_stream,
+        [NET_CLIENT_DRIVER_DGRAM]     = net_init_dgram,
    #ifdef CONFIG_VDE
            [NET_CLIENT_DRIVER_VDE]       = net_init_vde,
    #endif
@@ -1097,6 +1099,8 @@ void show_netdevs(void)
        int idx;
        const char *available_netdevs[] = {
            "socket",
+        "stream",
+        "dgram",
            "hubport",
            "tap",
    #ifdef CONFIG_SLIRP
@@ -1606,7 +1610,25 @@ int net_init_clients(Error **errp)
     */
    static bool netdev_is_modern(const char *optarg)
    {
-    return false;
+    static QemuOptsList dummy_opts = {
+        .name = "netdev",
+        .implied_opt_name = "type",
+        .head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head),
+        .desc = { { } },

net/net.c int net_init_clients(Error **errp) · line 1627

Quoted 10 lines
net/net.c +10 -0
+    };
+    const char *netdev;
+    QemuOpts *opts;
+    bool is_modern;
+
+    opts = qemu_opts_parse(&dummy_opts, optarg, true, &error_fatal);
+    netdev = qemu_opt_get(opts, "type");
+
+    is_modern = strcmp(netdev, "stream") == 0 ||
+                strcmp(netdev, "dgram") == 0;

Crashes when user neglects to pass "type".

Quoted 5 lines
Quoted 1 line

I think "type" is always passed because of the '.implied_opt_name = "type"'. Am I wrong?

.implied_opt_name = "type" lets you shorten "type=T,..." to "T,...". It doesn't make key "type" mandatory. "-netdev id=foo" is still permitted. Even "-netdev ''" is.

In fact type is checked before by QAPI definition:

{ 'union': 'Netdev',

Quoted 8 lines
    'base': { 'id': 'str', 'type': 'NetClientDriver' },
    'discriminator': 'type',

...

As it's the discriminator it must be there.

    $ qemu-system-x86_64 -netdev id=foo
    qemu-system-x86_64: -netdev id=foo: Parameter 'type' is missing

It does crash for me:

     (gdb) bt
     #0  0x00007ffff4d25dcb in __strcmp_avx2 () at /lib64/libc.so.6
     #1  0x0000555555b4574b in netdev_is_modern (optarg=0x7fffffffe2ae "id=foo")
         at ../net/net.c:1626
     #2  0x0000555555b457ad in net_client_parse
         (opts_list=0x555556563780 <qemu_netdev_opts>, optarg=0x7fffffffe2ae "id=foo") at ../net/net.c:1636
     #3  0x0000555555ad98de in qemu_init (argc=3, argv=0x7fffffffdf08, envp=0x0)
         at ../softmmu/vl.c:2901
     #4  0x0000555555842c01 in qemu_main (argc=3, argv=0x7fffffffdf08, envp=0x0)
         at ../softmmu/main.c:35
     #5  0x0000555555842c37 in main (argc=3, argv=0x7fffffffdf08)
         at ../softmmu/main.c:45
     (gdb) up
     #1  0x0000555555b4574b in netdev_is_modern (optarg=0x7fffffffe2ae "id=foo")
         at ../net/net.c:1626
     1626	    is_modern = strcmp(netdev, "stream") == 0 ||
     (gdb) p netdev
     $1 = 0x0
Quoted 6 lines

This is

      https://github.com/patchew-project/qemu tags/patchew/[email protected]

I suspect you tested with your v3, which doesn't crash for me, either.

Yes, thank you. So this is fixed now :)

Laurent

#13 lore raw

RFC PATCH v2 4/8 net: stream: Don't ignore EINVAL on netdev socket connection

In reply to Laurent Vivier

From: Stefano Brivio <[email protected]>

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

[lvivier: applied to net/stream.c]
---
 net/stream.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
net/stream.c +1 -2
index fdc97ee43a56..12fc26b9f4c7 100644
@@ -365,8 +365,7 @@ static int net_stream_client_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");
Signature · 1 line
2.35.3
Reviewed-by on v2 4/8
#14 lore raw

Re: [RFC PATCH v2 4/8] net: stream: Don't ignore EINVAL on netdev socket connection

Quoted 6 lines · Laurent Vivier

From: Stefano Brivio <[email protected]>

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

Quoted 6 lines
[lvivier: applied to net/stream.c]
---
 net/stream.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

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

RFC PATCH v2 5/8 net: stream: add unix socket

In reply to Laurent Vivier
---
 net/stream.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 99 insertions(+), 7 deletions(-)
net/stream.c +99 -7
index 12fc26b9f4c7..dca50508ed84 100644
@@ -234,7 +234,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;
 
@@ -252,9 +252,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,8 +313,40 @@ static int net_stream_server_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);
@@ -382,6 +432,48 @@ 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));
+        }
+
+        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) {
@@ -397,7 +489,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;
     }
 
Signature · 1 line
2.35.3
#16 lore raw

RFC PATCH v2 6/8 net: dgram: 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/dgram.c | 76 +++++++++++++++++++++++++++++++----------------------
 1 file changed, 45 insertions(+), 31 deletions(-)
net/dgram.c +45 -31
index aa4240501ed0..16b4d4c94c81 100644
@@ -39,9 +39,8 @@ typedef struct NetDgramState {
     int listen_fd;
     int fd;
     SocketReadState rs;
-  /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
-    struct sockaddr_in dgram_dst;
-    IOHandler *send_fn;           /* differs between SOCK_STREAM/SOCK_DGRAM */
+    struct sockaddr *dgram_dst; /* contains destination iff connectionless */
+    IOHandler *send_fn;
     bool read_poll;               /* waiting to receive data? */
     bool write_poll;              /* waiting to transmit data? */
 } NetDgramState;
@@ -85,10 +84,9 @@ static ssize_t net_dgram_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);
         }
@@ -289,6 +287,8 @@ static void net_dgram_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 = {
@@ -305,7 +305,7 @@ static NetDgramState *net_dgram_fd_init_dgram(NetClientState *peer,
                                               SocketAddress *mcast,
                                               Error **errp)
 {
-    struct sockaddr_in saddr;
+    struct sockaddr_in *saddr = NULL;
     int newfd;
     NetClientState *nc;
     NetDgramState *s;
@@ -327,24 +327,25 @@ static NetDgramState *net_dgram_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_dgram_mcast_create(&saddr, NULL, errp);
+            newfd = net_dgram_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);
@@ -358,16 +359,13 @@ static NetDgramState *net_dgram_fd_init_dgram(NetClientState *peer,
     net_dgram_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),
                  "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), "fd=%d %s", fd,
                  SocketAddressType_str(sa_type));
     }
@@ -375,6 +373,7 @@ static NetDgramState *net_dgram_fd_init_dgram(NetClientState *peer,
     return s;
 
 err:
+    g_free(saddr);
     closesocket(fd);
     return NULL;
 }
@@ -420,21 +419,24 @@ static int net_dgram_mcast_init(NetClientState *peer,
 {
     NetDgramState *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_dgram_mcast_create(&saddr, NULL, errp);
+        fd = net_dgram_mcast_create(saddr, NULL, errp);
         if (fd < 0) {
+            g_free(saddr);
             return -1;
         }
     } else {
@@ -443,13 +445,15 @@ static int net_dgram_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_dgram_mcast_create(&saddr, &localaddr, errp);
+            fd = net_dgram_mcast_create(saddr, &localaddr, errp);
             if (fd < 0) {
+                g_free(saddr);
                 return -1;
             }
             break;
@@ -457,16 +461,19 @@ static int net_dgram_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;
         }
@@ -476,13 +483,16 @@ static int net_dgram_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), "mcast=%s:%d",
-             inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
+             inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port));
+
     return 0;
 
 }
@@ -496,8 +506,8 @@ static int net_dgram_udp_init(NetClientState *peer,
 {
     NetDgramState *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) {
@@ -517,7 +527,7 @@ static int net_dgram_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) {
@@ -551,9 +561,12 @@ static int net_dgram_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("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;
     }
@@ -580,7 +593,8 @@ static int net_dgram_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);
Signature · 1 line
2.35.3
#17 lore raw

RFC PATCH v2 7/8 net: dgram: 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_dgram_mcast_init() and net_dgram_udp_init().

---
 net/dgram.c | 143 +++++++++++++++++++++++++++-------------------------
 1 file changed, 73 insertions(+), 70 deletions(-)
net/dgram.c +73 -70
index 16b4d4c94c81..c0cf0410792e 100644
@@ -301,52 +301,11 @@ static NetClientInfo net_dgram_socket_info = {
 static NetDgramState *net_dgram_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;
     NetDgramState *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_dgram_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);
 
@@ -358,24 +317,7 @@ static NetDgramState *net_dgram_fd_init_dgram(NetClientState *peer,
     net_socket_rs_init(&s->rs, net_dgram_rs_finalize, false);
     net_dgram_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),
-                 "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), "fd=%d %s", fd,
-                 SocketAddressType_str(sa_type));
-    }
-
     return s;
-
-err:
-    g_free(saddr);
-    closesocket(fd);
-    return NULL;
 }
 
 static void net_dgram_connect(void *opaque)
@@ -420,6 +362,7 @@ static int net_dgram_mcast_init(NetClientState *peer,
     NetDgramState *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");
@@ -439,6 +382,9 @@ static int net_dgram_mcast_init(NetClientState *peer,
             g_free(saddr);
             return -1;
         }
+        info_str = g_strdup_printf("mcast=%s:%d",
+                                   inet_ntoa(saddr->sin_addr),
+                                   ntohs(saddr->sin_port));
     } else {
         switch (local->type) {
         case SOCKET_ADDRESS_TYPE_INET: {
@@ -456,9 +402,14 @@ static int net_dgram_mcast_init(NetClientState *peer,
                 g_free(saddr);
                 return -1;
             }
+            info_str = g_strdup_printf("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);
@@ -471,7 +422,46 @@ static int net_dgram_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_dgram_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("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");
@@ -479,9 +469,7 @@ static int net_dgram_mcast_init(NetClientState *peer,
         }
     }
 
-    s = net_dgram_fd_init_dgram(peer, model, name, fd,
-                                 local->type == SOCKET_ADDRESS_TYPE_FD,
-                                 remote, errp);
+    s = net_dgram_fd_init_dgram(peer, model, name, fd, errp);
     if (!s) {
         g_free(saddr);
         return -1;
@@ -490,8 +478,8 @@ static int net_dgram_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), "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;
 
@@ -570,7 +558,10 @@ static int net_dgram_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;
@@ -581,23 +572,35 @@ static int net_dgram_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("fd=%d %s", fd,
+                                       SocketAddressType_str(sa_type));
+        } else {
+            info_str = g_strdup_printf("fd=%d", fd);
+        }
         break;
+    }
     default:
         error_setg(errp, "only support inet or fd type for local");
         return -1;
     }
 
-    s = net_dgram_fd_init_dgram(peer, model, name, fd, 0, NULL, errp);
+    s = net_dgram_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
#18 lore raw

RFC PATCH v2 8/8 net: dgram: add unix socket

In reply to Laurent Vivier
---
 net/dgram.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 62 insertions(+), 3 deletions(-)
net/dgram.c +62 -3
index c0cf0410792e..9f20bdbc163c 100644
@@ -85,8 +85,15 @@ static ssize_t net_dgram_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);
         }
@@ -508,7 +515,7 @@ static int net_dgram_udp_init(NetClientState *peer,
         }
     } else {
         if (local->type != SOCKET_ADDRESS_TYPE_FD) {
-            error_setg(errp, "type=inet requires remote parameter");
+            error_setg(errp, "type=inet or unix require remote parameter");
             return -1;
         }
     }
@@ -558,6 +565,58 @@ static int net_dgram_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("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
Reviewed-by Tested-by on the v2 series
#19 lore raw
In reply to Laurent Vivier
Quoted 66 lines · On Thu, 12 May 2022 10:09:24 +0200 Laurent Vivier <[email protected]> wrote:

"-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.

Two new netdev backends, "stream" and "dgram" are added, that are barely a copy of "socket" backend but they use the SocketAddress QAPI to provide socket parameters. And then they also implement unix sockets (TCP and UDP).

Some examples of CLI syntax:

for TCP:

-netdev stream,id=socket0,addr.type=inet,addr.host=localhost,addr.port=1234 -netdev stream,id=socket0,server=off,addr.type=inet,addr.host=localhost,addr.port=1234

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

for UNIX:

-netdev stream,id=socket0,addr.type=unix,addr.path=/tmp/qemu0 -netdev stream,id=socket0,server=off,addr.type=unix,addr.path=/tmp/qemu0

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

for FD:

-netdev stream,id=socket0,addr.type=fd,addr.str=4 -netdev stream,id=socket0,server=off,addr.type=fd,addr.str=5

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

v2:
- use "stream" and "dgram" rather than "socket-ng,mode=stream" and ""socket-ng,mode=dgram"
- extract code to bypass qemu_opts_parse_noisily() to a new patch
- do not ignore EINVAL (Stefano)
- fix "-net" option

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

Laurent Vivier (7):
net: introduce convert_host_port()
qapi: net: introduce a way to bypass qemu_opts_parse_noisily()
qapi: net: add stream and dgram netdevs
net: stream: add unix socket
net: dgram: make dgram_dst generic
net: dgram: move mcast specific code from net_socket_fd_init_dgram()
net: dgram: add unix socket

Quoted 2 lines

Stefano Brivio (1):
net: stream: Don't ignore EINVAL on netdev socket connection

Except for 4/8:

Except for 6/8 to 8/8:

Signature · 1 line
Stefano