qapi: net: add unix socket type support to netdev backend
Subsystems (2)
v6 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=1235for 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/qemu1for 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
v7:
- add qtests
- update parameters table in net.json
- update socket_uri() and socket_parse()
v6:
- s/netdev option/-netdev option/ PATCH 4
- s/ / /
- update @NetdevStreamOptions and @NetdevDgramOptions comments
- update PATCH 4 description message
- add missing return in error case for unix stream socket
- split socket_uri() patch: move and rename, then change content
v5:
- remove RFC prefix
- put the change of net_client_parse() into its own patch (exit() in the function)
- update comments regarding netdev_is_modern() and netdev_parse_modern()
- update error case in net_stream_server_init()
- update qemu-options.hx with unix type
- fix HMP "info network" with unix protocol/server side.
v4:
- net_client_parse() fails with exit() rather than with return.
- keep "{ 'name': 'vmnet-host', 'if': 'CONFIG_VMNET' }" on its own line in qapi/net.json
- add a comment in qapi/net.json about parameters usage
- move netdev_is_modern() check to qemu_init()
- in netdev_is_modern(), check for JSON and use qemu_opts_do_parse() to parse parameters and detect type value.
- add a blank line after copyright comment
v3:
- remove support of "-net" for dgram and stream. They are only supported with "-netdev" option.
- use &error_fatal directly in net_client_inits()
- update qemu-options.hx
- move to QIO for stream socket
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
Laurent Vivier (13):
net: introduce convert_host_port()
net: remove the @errp argument of net_client_inits()
net: simplify net_client_parse() error management
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
qemu-sockets: move and rename SocketAddress_to_str()
qemu-sockets: update socket_uri() and socket_parse() to be consistent
net: stream: move to QIO
tests/qtest: netdev: test stream and dgram backends
Stefano Brivio (1):
net: stream: Don't ignore EINVAL on netdev socket connection
hmp-commands.hx | 2 +- include/net/net.h | 6 +- include/qemu/sockets.h | 4 +- monitor/hmp-cmds.c | 23 +- net/clients.h | 6 + net/dgram.c | 707 ++++++++++++++++++++++++++++++++++++ net/hub.c | 2 + net/meson.build | 2 + net/net.c | 169 ++++++--- net/stream.c | 376 +++++++++++++++++++ qapi/net.json | 63 +++- qemu-options.hx | 14 + softmmu/vl.c | 16 +- tests/qtest/meson.build | 1 + tests/qtest/netdev-socket.c | 322 ++++++++++++++++ util/qemu-sockets.c | 25 ++ 16 files changed, 1656 insertions(+), 82 deletions(-) create mode 100644 net/dgram.c create mode 100644 net/stream.c create mode 100644 tests/qtest/netdev-socket.c
Signature · 1 line
2.37.1
PATCH v6 1/14 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
@@ -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.37.1
PATCH v6 2/14 net: remove the @errp argument of net_client_inits()
In reply to Laurent VivierThe only caller passes &error_fatal, so use this directly in the function.
It's what we do for -blockdev, -device, and -object.
--- include/net/net.h | 2 +- net/net.c | 20 +++++++------------- softmmu/vl.c | 2 +- 3 files changed, 9 insertions(+), 15 deletions(-)
include/net/net.h +1 -1
@@ -216,7 +216,7 @@ extern const char *host_net_devices[];/* from net.c */ int net_client_parse (QemuOptsList *opts_list,const char *str);void show_netdevs (void ); -int net_init_clients (Error **errp); +void net_init_clients (void );void net_check_clients (void );void net_cleanup (void );void hmp_host_net_add (Monitor *mon,const QDict *qdict);
net/net.c +7 -13
@@ -1562,27 +1562,21 @@ out:return ret; } -int net_init_clients (Error **errp) +void net_init_clients (void ) { net_change_state_entry =qemu_add_vm_change_state_handler (net_vm_change_state_handler,NULL );QTAILQ_INIT (&net_clients); -if (qemu_opts_foreach (qemu_find_opts ("netdev" ), - net_init_netdev,NULL , errp)) { -return -1 ; - } - -if (qemu_opts_foreach (qemu_find_opts ("nic" ), net_param_nic,NULL , errp)) { -return -1 ; - } +qemu_opts_foreach (qemu_find_opts ("netdev" ), net_init_netdev,NULL , + &error_fatal); -if (qemu_opts_foreach (qemu_find_opts ("net" ), net_init_client,NULL , errp)) { -return -1 ; - } +qemu_opts_foreach (qemu_find_opts ("nic" ), net_param_nic,NULL , + &error_fatal); -return 0 ; +qemu_opts_foreach (qemu_find_opts ("net" ), net_init_client,NULL , + &error_fatal); }int net_client_parse (QemuOptsList *opts_list,const char *optarg)
softmmu/vl.c +1 -1
@@ -1904,7 +1904,7 @@ static void qemu_create_late_backends(void)qtest_server_init (qtest_chrdev, qtest_log, &error_fatal); } -net_init_clients (&error_fatal); +net_init_clients ();object_option_foreach_add (object_create_late);
Signature · 1 line
2.37.1
PATCH v6 3/14 net: simplify net_client_parse() error management
In reply to Laurent VivierAll net_client_parse() callers exit in case of error.
Move exit(1) to net_client_parse() and remove error checking from the callers.
--- include/net/net.h | 2 +- net/net.c | 6 ++---- softmmu/vl.c | 12 +++--------- 3 files changed, 6 insertions(+), 14 deletions(-)
include/net/net.h +1 -1
@@ -214,7 +214,7 @@ extern NICInfo nd_table[MAX_NICS];extern const char *host_net_devices[];/* from net.c */ -int net_client_parse (QemuOptsList *opts_list,const char *str); +void net_client_parse (QemuOptsList *opts_list,const char *str);void show_netdevs (void );void net_init_clients (void );void net_check_clients (void );
net/net.c +2 -4
@@ -1579,13 +1579,11 @@ void net_init_clients(void) &error_fatal); } -int net_client_parse (QemuOptsList *opts_list,const char *optarg) +void net_client_parse (QemuOptsList *opts_list,const char *optarg) {if (!qemu_opts_parse_noisily (opts_list, optarg, true)) { -return -1 ; +exit (1 ); } - -return 0 ; }/* From FreeBSD */
softmmu/vl.c +3 -9
@@ -2815,21 +2815,15 @@ void qemu_init(int argc, char **argv, char **envp)break ;case QEMU_OPTION_netdev: default_net =0 ; -if (net_client_parse (qemu_find_opts ("netdev" ), optarg) ==-1 ) { -exit (1 ); - } +net_client_parse (qemu_find_opts ("netdev" ), optarg);break ;case QEMU_OPTION_nic: default_net =0 ; -if (net_client_parse (qemu_find_opts ("nic" ), optarg) ==-1 ) { -exit (1 ); - } +net_client_parse (qemu_find_opts ("nic" ), optarg);break ;case QEMU_OPTION_net: default_net =0 ; -if (net_client_parse (qemu_find_opts ("net" ), optarg) ==-1 ) { -exit (1 ); - } +net_client_parse (qemu_find_opts ("net" ), optarg);break ;#ifdef CONFIG_LIBISCSI case QEMU_OPTION_iscsi:
Signature · 1 line
2.37.1
PATCH v6 4/14 qapi: net: introduce a way to bypass qemu_opts_parse_noisily()
In reply to Laurent VivierAs 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.
More details from Markus:
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.
This commit paves the way to use of the modern way instead.
--- include/net/net.h | 2 ++ net/net.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++ softmmu/vl.c | 6 ++++- 3 files changed, 64 insertions(+), 1 deletion(-)
include/net/net.h +2 -0
@@ -214,6 +214,8 @@ extern NICInfo nd_table[MAX_NICS];extern const char *host_net_devices[];/* from net.c */ +bool netdev_is_modern (const char *optarg); +void netdev_parse_modern (const char *optarg);void net_client_parse (QemuOptsList *opts_list,const char *str);void show_netdevs (void );void net_init_clients (void );
net/net.c +57 -0
87 lines not on this page · raw message
softmmu/vl.c +5 -1
@@ -2815,7 +2815,11 @@ void qemu_init(int argc, char **argv, char **envp)break ;case QEMU_OPTION_netdev: default_net =0 ; -net_client_parse (qemu_find_opts ("netdev" ), optarg); +if (netdev_is_modern (optarg)) { +netdev_parse_modern (optarg); + }else { +net_client_parse (qemu_find_opts ("netdev" ), optarg); + }break ;case QEMU_OPTION_nic: default_net =0 ;
Signature · 1 line
2.37.1
PATCH v6 5/14 qapi: net: add stream and dgram netdevs
In reply to Laurent VivierCopied 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)
The two new types need to be parsed the modern way with -netdev, because with the traditional way, the "type" field of netdev structure collides with the "type" field of SocketAddress and prevents the correct evaluation of the command line option. Moreover the traditional way doesn't allow to use the same type (SocketAddress) several times with the -netdev option (needed to specify "local" and "remote" addresses).
The previous commit paved the way for parsing the modern way, but omitted one detail: how to pick modern vs. traditional, in netdev_is_modern().
We want to pick based on the value of parameter "type". But how to extract it from the option argument?
Parsing the option argument, either the modern or the traditional way, extracts it for us, but only if parsing succeeds.
If parsing fails, there is no good option. No matter which parser we pick, it'll be the wrong one for some arguments, and the error reporting will be confusing.
Fortunately, the traditional parser accepts *anything* when called in a certain way. This maximizes our chance to extract the value of "type", and in turn minimizes the risk of confusing error reporting.
--- hmp-commands.hx | 2 +- net/clients.h | 6 + net/dgram.c | 631 ++++++++++++++++++++++++++++++++++++++++++++++++ net/hub.c | 2 + net/meson.build | 2 + net/net.c | 30 ++- net/stream.c | 423 ++++++++++++++++++++++++++++++++ qapi/net.json | 63 ++++- qemu-options.hx | 12 + 9 files changed, 1167 insertions(+), 4 deletions(-) create mode 100644 net/dgram.c create mode 100644 net/stream.c
hmp-commands.hx +1 -1
@@ -1276,7 +1276,7 @@ ERST { .name = "netdev_add", .args_type = "netdev:O", - .params = "[user|tap|socket|vde|bridge|hubport|netmap|vhost-user" + .params = "[user|tap|socket|stream|dgram|vde|bridge|hubport|netmap|vhost-user" #ifdef CONFIG_VMNET "|vmnet-host|vmnet-shared|vmnet-bridged" #endif
net/clients.h +6 -0
@@ -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 +631 -0
631 lines not on this page · raw message
net/hub.c +2 -0
@@ -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
@@ -13,6 +13,8 @@ softmmu_ss.add(files( 'net.c', 'queue.c', 'socket.c', + 'stream.c', + 'dgram.c', 'util.c', ))
net/net.c +29 -1
@@ -48,6 +48,7 @@#include "qemu/qemu-print.h" #include "qemu/main-loop.h" #include "qemu/option.h" +#include "qemu/keyval.h" #include "qapi/error.h" #include "qapi/opts-visitor.h" #include "sysemu/runstate.h"
@@ -1014,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
@@ -1101,6 +1104,8 @@ void show_netdevs(void)int idx;const char *available_netdevs[] = {"socket" , +"stream" , +"dgram" ,"hubport" ,"tap" , #ifdef CONFIG_SLIRP
@@ -1613,7 +1618,30 @@ void net_init_clients(void)*/ bool netdev_is_modern (const char *optarg) { -return false; +QemuOpts *opts; +bool is_modern; +const char *type; +static QemuOptsList dummy_opts = { + .name ="netdev" , + .implied_opt_name ="type" , + .head =QTAILQ_HEAD_INITIALIZER (dummy_opts.head), + .desc = { { } }, + }; + +if (optarg[0 ] =='{' ) { +/* This is JSON, which means it's modern syntax */ +return true; + } + + opts =qemu_opts_create (&dummy_opts,NULL , false, &error_abort); +qemu_opts_do_parse (opts, optarg, dummy_opts.implied_opt_name, + &error_abort); + type =qemu_opt_get (opts,"type" ); + is_modern = !g_strcmp0 (type,"stream" ) || !g_strcmp0 (type,"dgram" ); + +qemu_opts_reset (&dummy_opts); + +return is_modern; }/*
net/stream.c new file +423 -0
423 lines not on this page · raw message
qapi/net.json +61 -2
87 lines not on this page · raw message
qemu-options.hx +12 -0
@@ -2726,6 +2726,18 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev, "-netdev socket,id=str[,fd=h][,udp=host:port][,localaddr=host:port]\n" " configure a network backend to connect to another network\n" " using an UDP tunnel\n" + "-netdev stream,id=str[,server=on|off],addr.type=inet,addr.host=host,addr.port=port\n" + "-netdev stream,id=str[,server=on|off],addr.type=fd,addr.str=h\n" + " configure a network backend to connect to another network\n" + " using a socket connection in stream mode.\n" + "-netdev dgram,id=str,remote.type=inet,remote.host=maddr,remote.port=port[,local.type=inet,local.host=addr]\n" + "-netdev dgram,id=str,remote.type=inet,remote.host=maddr,remote.port=port[,local.type=fd,local.str=h]\n" + " configure a network backend to connect to a multicast maddr and port\n" + " use ``local.host=addr`` to specify the host address to send packets from\n" + "-netdev dgram,id=str,local.type=inet,local.host=addr,local.port=port[,remote.type=inet,remote.host=addr,remote.port=port]\n" + "-netdev dgram,id=str,local.type=fd,local.str=h\n" + " configure a network backend to connect to another network\n" + " using an UDP tunnel\n" #ifdef CONFIG_VDE "-netdev vde,id=str[,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n" " configure a network backend to connect to port 'n' of a vde switch\n"
Signature · 1 line
2.37.1
PATCH v6 6/14 net: stream: Don't ignore EINVAL on netdev socket connection
In reply to Laurent VivierFrom: 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.
--- net/stream.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
net/stream.c +1 -2
@@ -363,8 +363,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.37.1
PATCH v6 7/14 net: stream: add unix socket
In reply to Laurent Vivier--- net/stream.c | 108 +++++++++++++++++++++++++++++++++++++++++++++--- qapi/net.json | 2 +- qemu-options.hx | 1 + 3 files changed, 105 insertions(+), 6 deletions(-)
net/stream.c +103 -5
@@ -235,7 +235,7 @@ static NetStreamState *net_stream_fd_init_stream(NetClientState *peer,static void net_stream_accept (void *opaque) {NetStreamState *s = opaque; -struct sockaddr_in saddr; +struct sockaddr_storage saddr;socklen_t len;int fd;
@@ -253,9 +253,27 @@ static void net_stream_accept(void *opaque) s->fd = fd; s->nc.link_down = false;net_stream_connect (s); -snprintf (s->nc.info_str,sizeof (s->nc.info_str), -"connection from %s:%d" , -inet_ntoa (saddr.sin_addr),ntohs (saddr.sin_port)); +switch (saddr.ss_family) { +case AF_INET : { +struct sockaddr_in *saddr_in = (struct sockaddr_in *)&saddr; + +snprintf (s->nc.info_str,sizeof (s->nc.info_str), +"connection from %s:%d" , +inet_ntoa (saddr_in->sin_addr),ntohs (saddr_in->sin_port)); +break ; + } +case AF_UNIX : { +struct sockaddr_un saddr_un; + + len =sizeof (saddr_un); +getsockname (s->listen_fd, (struct sockaddr *)&saddr_un, &len); +snprintf (s->nc.info_str,sizeof (s->nc.info_str), +"connect from %s" , saddr_un.sun_path); +break ; + } +default : +g_assert_not_reached (); + } } static int net_stream_server_init(NetClientState *peer,
@@ -295,6 +313,43 @@ static int net_stream_server_init(NetClientState *peer, }break ; } +case SOCKET_ADDRESS_TYPE_UNIX : { +struct sockaddr_un saddr_un; + + ret =unlink (addr->u.q_unix.path); +if (ret <0 && errno !=ENOENT ) { +error_setg_errno (errp, errno,"failed to unlink socket %s" , + addr->u.q_unix.path); +return -1 ; + } + + saddr_un.sun_family =PF_UNIX ; + ret =snprintf (saddr_un.sun_path,sizeof (saddr_un.sun_path),"%s" , + addr->u.q_unix.path); +if (ret <0 || ret >=sizeof (saddr_un.sun_path)) { +error_setg (errp,"UNIX socket path '%s' is too long" , + addr->u.q_unix.path); +error_append_hint (errp,"Path must be less than %zu bytes\n" , +sizeof (saddr_un.sun_path)); +return -1 ; + } + + fd =qemu_socket (PF_UNIX ,SOCK_STREAM ,0 ); +if (fd <0 ) { +error_setg_errno (errp, errno,"can't create stream socket" ); +return -1 ; + } +qemu_socket_set_nonblock (fd); + + ret =bind (fd, (struct sockaddr *)&saddr_un,sizeof (saddr_un)); +if (ret <0 ) { +error_setg_errno (errp, errno,"can't create socket with path: %s" , + saddr_un.sun_path); +closesocket (fd); +return -1 ; + } +break ; + }case SOCKET_ADDRESS_TYPE_FD : fd =monitor_fd_param (monitor_cur (), addr->u.fd.str, errp);if (fd ==-1 ) {
@@ -380,6 +435,49 @@ static int net_stream_client_init(NetClientState *peer, ntohs(saddr_in.sin_port));break ; } +case SOCKET_ADDRESS_TYPE_UNIX : { +struct sockaddr_un saddr_un; + + saddr_un.sun_family =PF_UNIX ; + ret =snprintf (saddr_un.sun_path,sizeof (saddr_un.sun_path),"%s" , + addr->u.q_unix.path); +if (ret <0 || ret >=sizeof (saddr_un.sun_path)) { +error_setg (errp,"UNIX socket path '%s' is too long" , + addr->u.q_unix.path); +error_append_hint (errp,"Path must be less than %zu bytes\n" , +sizeof (saddr_un.sun_path)); +return -1 ; + } + + fd =qemu_socket (PF_UNIX ,SOCK_STREAM ,0 ); +if (fd <0 ) { +error_setg_errno (errp, errno,"can't create stream socket" ); +return -1 ; + } +qemu_socket_set_nonblock (fd); + + connected =0 ; +for (;;) { + ret =connect (fd, (struct sockaddr *)&saddr_un,sizeof (saddr_un)); +if (ret <0 ) { +if (errno ==EINTR || errno ==EWOULDBLOCK ) { +/* continue */ + }else if (errno ==EAGAIN || + errno ==EALREADY ) { +break ; + }else { +error_setg_errno (errp, errno,"can't connect socket" ); +closesocket (fd); +return -1 ; + } + }else { + connected =1 ; +break ; + } + } + info_str =g_strdup_printf (" connect to %s" , saddr_un.sun_path); +break ; + }case SOCKET_ADDRESS_TYPE_FD : fd =monitor_fd_param (monitor_cur (), addr->u.fd.str, errp);if (fd ==-1 ) {
@@ -395,7 +493,7 @@ static int net_stream_client_init(NetClientState *peer, info_str =g_strdup_printf ("connect to fd %d" , fd);break ;default : -error_setg (errp,"only support inet or fd type" ); +error_setg (errp,"only support inet, unix or fd type" );return -1 ; }
qapi/net.json +1 -1
@@ -583,7 +583,7 @@ # or connect to (server=false) # @server: create server socket (default: true) # -# Only SocketAddress types 'inet' and 'fd' are supported. +# Only SocketAddress types 'unix', 'inet' and 'fd' are supported. # # Since: 7.1 ##
qemu-options.hx +1 -0
@@ -2727,6 +2727,7 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev, " configure a network backend to connect to another network\n" " using an UDP tunnel\n" "-netdev stream,id=str[,server=on|off],addr.type=inet,addr.host=host,addr.port=port\n" + "-netdev stream,id=str[,server=on|off],addr.type=unix,addr.path=path\n" "-netdev stream,id=str[,server=on|off],addr.type=fd,addr.str=h\n" " configure a network backend to connect to another network\n" " using a socket connection in stream mode.\n"
Signature · 1 line
2.37.1
PATCH v6 8/14 net: dgram: make dgram_dst generic
In reply to Laurent Vivierdgram_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
@@ -40,9 +40,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;
@@ -86,10 +85,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); }
@@ -290,6 +288,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 = {
@@ -306,7 +306,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;
@@ -328,24 +328,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);
@@ -359,16 +360,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)); }
@@ -376,6 +374,7 @@ static NetDgramState *net_dgram_fd_init_dgram(NetClientState *peer,return s; err: +g_free (saddr);closesocket (fd);return NULL ; }
@@ -421,21 +420,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 {
@@ -444,13 +446,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 ;
@@ -458,16 +462,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 ; }
@@ -477,13 +484,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 ; }
@@ -497,8 +507,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 ) {
@@ -518,7 +528,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 ) {
@@ -552,9 +562,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 ; }
@@ -581,7 +594,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.37.1
PATCH v6 9/14 net: dgram: move mcast specific code from net_socket_fd_init_dgram()
In reply to Laurent VivierIt 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
@@ -302,52 +302,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);
@@ -359,24 +318,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)
@@ -421,6 +363,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" );
@@ -440,6 +383,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 : {
@@ -457,9 +403,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);
@@ -472,7 +423,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" );
@@ -480,9 +470,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 ;
@@ -491,8 +479,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 ;
@@ -571,7 +559,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 ;
@@ -582,23 +573,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.37.1
PATCH v6 10/14 net: dgram: add unix socket
In reply to Laurent Vivier--- net/dgram.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++--- qapi/net.json | 2 +- qemu-options.hx | 1 + 3 files changed, 64 insertions(+), 4 deletions(-)
net/dgram.c +62 -3
@@ -86,8 +86,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); }
@@ -509,7 +516,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 ; } }
@@ -559,6 +566,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;
qapi/net.json +1 -1
@@ -600,7 +600,7 @@ # @remote: remote address # @local: local address # -# Only SocketAddress types 'inet' and 'fd' are supported. +# Only SocketAddress types 'unix', 'inet' and 'fd' are supported. # # 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
qemu-options.hx +1 -0
@@ -2736,6 +2736,7 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev, " configure a network backend to connect to a multicast maddr and port\n" " use ``local.host=addr`` to specify the host address to send packets from\n" "-netdev dgram,id=str,local.type=inet,local.host=addr,local.port=port[,remote.type=inet,remote.host=addr,remote.port=port]\n" + "-netdev dgram,id=str,local.type=unix,local.path=path[,remote.type=unix,remote.path=path]\n" "-netdev dgram,id=str,local.type=fd,local.str=h\n" " configure a network backend to connect to another network\n" " using an UDP tunnel\n"
Signature · 1 line
2.37.1
On 22/07/2022 20:56, Laurent Vivier wrote:
...
Please ignore this series, bad numbering and patch 11 breaks SMTP server...
Sent v7 with another SMTP server.
Thanks,
LAurent