qapi: net: add unix socket type support to netdev backend
Subsystems (2)
v3 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
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 (10):
net: introduce convert_host_port()
net: remove the @errp argument of net_client_inits()
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: introduce socket_uri()
net: stream: move to QIO
Stefano Brivio (1):
net: stream: Don't ignore EINVAL on netdev socket connection
hmp-commands.hx | 2 +- include/net/net.h | 3 +- include/qemu/sockets.h | 4 +- monitor/hmp-cmds.c | 23 +- net/clients.h | 6 + net/dgram.c | 706 +++++++++++++++++++++++++++++++++++++++++ net/hub.c | 2 + net/meson.build | 2 + net/net.c | 149 ++++++--- net/stream.c | 371 ++++++++++++++++++++++ qapi/net.json | 40 ++- qemu-options.hx | 12 + softmmu/vl.c | 5 +- util/qemu-sockets.c | 20 ++ 14 files changed, 1277 insertions(+), 68 deletions(-) create mode 100644 net/dgram.c create mode 100644 net/stream.c
Signature · 1 line
2.36.1
RFC PATCH v3 1/11 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.36.1
RFC PATCH v3 2/11 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
@@ -1925,7 +1925,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.36.1
Re: [RFC PATCH v3 02/11] net: remove the @errp argument of net_client_inits()
Quoted 5 lines · Laurent Vivier
The only caller passes
&error_fatal, so use this directly in the function.It's what we do for -blockdev, -device, and -object.
Signed-off-by Laurent Vivier <[email protected]>
RFC PATCH v3 3/11 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 | 1 + net/net.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++ softmmu/vl.c | 3 ++- 3 files changed, 63 insertions(+), 1 deletion(-)
include/net/net.h +1 -0
@@ -214,6 +214,7 @@ extern NICInfo nd_table[MAX_NICS];extern const char *host_net_devices[];/* from net.c */ +int netdev_parse_modern (const char *optarg);int net_client_parse (QemuOptsList *opts_list,const char *str);void show_netdevs (void );void net_init_clients (void );
net/net.c +60 -0
90 lines not on this page · raw message
softmmu/vl.c +2 -1
@@ -2839,7 +2839,8 @@ 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 ) { +if (netdev_parse_modern (optarg) ==-1 && +net_client_parse (qemu_find_opts ("netdev" ), optarg) ==-1 ) {exit (1 ); }break ;
Signature · 1 line
2.36.1
Re: [RFC PATCH v3 03/11] qapi: net: introduce a way to bypass qemu_opts_parse_noisily()
Quoted 163 lines · 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 bypassqemu_opts_parse_noisily()and use directlyvisit_type_Netdev()to parse the backend parameters.More details from Markus:
qemu_init()passes the argument of -netdev, -nic, and -net tonet_client_parse().
net_client_parse()parses withqemu_opts_parse_noisily(), passing QemuOptsListqemu_netdev_optsfor -netdev,qemu_nic_optsfor -nic, andqemu_net_optsfor -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 tonet_init_netdev(),net_param_nic(), ornet_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 commandnetdev_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 useskeyval_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.
Signed-off-by Laurent Vivier <[email protected]>--- include/net/net.h | 1 + net/net.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++ softmmu/vl.c | 3 ++- 3 files changed, 63 insertions(+), 1 deletion(-)include/net/net.h +1
-0@@ -214,6 +214,7 @@ extern NICInfo nd_table[MAX_NICS];extern const char *host_net_devices[];/* from net.c */ +int netdev_parse_modern (const char *optarg);int net_client_parse (QemuOptsList *opts_list,const char *str);void show_netdevs (void );void net_init_clients (void );net/net.c +60
-0@@ -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,16 @@static VMChangeStateEntry *net_change_state_entry; static QTAILQ_HEAD(, NetClientState) net_clients; +typedef struct NetdevQueueEntry { +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 */ @@ -1562,6 +1573,20 @@ out:return ret; } +static void netdev_init_modern (void ) +{ +while (!QSIMPLEQ_EMPTY (&nd_queue)) { +NetdevQueueEntry *nd =QSIMPLEQ_FIRST (&nd_queue); + +QSIMPLEQ_REMOVE_HEAD (&nd_queue, entry); +loc_push_restore (&nd->loc); +net_client_init1 (nd->nd, true, &error_fatal); +loc_pop (&nd->loc); +qapi_free_Netdev (nd->nd); +g_free (nd); + } +} +void net_init_clients (void ) { net_change_state_entry =@@ -1569,6 +1594,8 @@ void net_init_clients(void)QTAILQ_INIT (&net_clients); +netdev_init_modern (); +qemu_opts_foreach (qemu_find_opts ("netdev" ), net_init_netdev,NULL , &error_fatal);@@ -1579,6 +1606,39 @@ void net_init_clients(void) &error_fatal); } +/* +* 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; +} + +/* +* netdev_parse_modern() uses modern, more expressive syntax than +* net_client_parse(), supports only the netdev option. +*/ +int netdev_parse_modern (const char *optarg) +{ +Visitor *v; +NetdevQueueEntry *nd; + +if (!netdev_is_modern (optarg)) { +return -1 ; + } + + 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); + +QSIMPLEQ_INSERT_TAIL (&nd_queue, nd, entry); + +return 0 ; +} +int net_client_parse (QemuOptsList *opts_list,const char *optarg) {if (!qemu_opts_parse_noisily (opts_list, optarg, true)) {softmmu/vl.c +0
-0
softmmu/vl.c void qemu_init(int argc, char **argv, char **envp) · line 2846
Quoted 9 lines
softmmu/vl.c +2
-1@@ -2839,7 +2839,8 @@ 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 ) { +if (netdev_parse_modern (optarg) ==-1 && +net_client_parse (qemu_find_opts ("netdev" ), optarg) ==-1 ) {exit (1 ); }break ;
To make this work, netdev_parse_modern() must
* either succeeed, or
* fail without reporting an error, or
* report an error and exit()
Recommend to spell that out in its function comment.
Alternatively:
if (netdev_is_modern(optarg)) {
netdev_parse_modern(optarg);
} else {
if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
exit(1);
}
}netdev_is_modern() needs external linkage, and netdev_parse_modern() loses its return value.
Note that all callers net_client_parse() handle failure exactly the same way. If we let net_client_parse() exit(), then this becomes
if (netdev_is_modern(optarg)) {
netdev_parse_modern(optarg);
} else {
net_client_parse(qemu_find_opts("netdev"), optarg);
}I like this one best. What do you think?
Regardless of which alternative we pick: error reporting depends on netdev_is_modern(). To be discussed in my review of the next patch.
Re: [RFC PATCH v3 03/11] qapi: net: introduce a way to bypass qemu_opts_parse_noisily()
Quoted 201 lines · Markus Armbruster
Quoted 163 lines · 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 bypassqemu_opts_parse_noisily()and use directlyvisit_type_Netdev()to parse the backend parameters.More details from Markus:
qemu_init()passes the argument of -netdev, -nic, and -net tonet_client_parse().
net_client_parse()parses withqemu_opts_parse_noisily(), passing QemuOptsListqemu_netdev_optsfor -netdev,qemu_nic_optsfor -nic, andqemu_net_optsfor -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 tonet_init_netdev(),net_param_nic(), ornet_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 commandnetdev_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 useskeyval_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.
Signed-off-by Laurent Vivier <[email protected]>--- include/net/net.h | 1 + net/net.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++ softmmu/vl.c | 3 ++- 3 files changed, 63 insertions(+), 1 deletion(-)include/net/net.h +1
-0@@ -214,6 +214,7 @@ extern NICInfo nd_table[MAX_NICS];extern const char *host_net_devices[];/* from net.c */ +int netdev_parse_modern (const char *optarg);int net_client_parse (QemuOptsList *opts_list,const char *str);void show_netdevs (void );void net_init_clients (void );net/net.c +60
-0@@ -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,16 @@static VMChangeStateEntry *net_change_state_entry; static QTAILQ_HEAD(, NetClientState) net_clients; +typedef struct NetdevQueueEntry { +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 */ @@ -1562,6 +1573,20 @@ out:return ret; } +static void netdev_init_modern (void ) +{ +while (!QSIMPLEQ_EMPTY (&nd_queue)) { +NetdevQueueEntry *nd =QSIMPLEQ_FIRST (&nd_queue); + +QSIMPLEQ_REMOVE_HEAD (&nd_queue, entry); +loc_push_restore (&nd->loc); +net_client_init1 (nd->nd, true, &error_fatal); +loc_pop (&nd->loc); +qapi_free_Netdev (nd->nd); +g_free (nd); + } +} +void net_init_clients (void ) { net_change_state_entry =@@ -1569,6 +1594,8 @@ void net_init_clients(void)QTAILQ_INIT (&net_clients); +netdev_init_modern (); +qemu_opts_foreach (qemu_find_opts ("netdev" ), net_init_netdev,NULL , &error_fatal);@@ -1579,6 +1606,39 @@ void net_init_clients(void) &error_fatal); } +/* +* 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; +} + +/* +* netdev_parse_modern() uses modern, more expressive syntax than +* net_client_parse(), supports only the netdev option. +*/ +int netdev_parse_modern (const char *optarg) +{ +Visitor *v; +NetdevQueueEntry *nd; + +if (!netdev_is_modern (optarg)) { +return -1 ; + } + + 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); + +QSIMPLEQ_INSERT_TAIL (&nd_queue, nd, entry); + +return 0 ; +} +int net_client_parse (QemuOptsList *opts_list,const char *optarg) {if (!qemu_opts_parse_noisily (opts_list, optarg, true)) {softmmu/vl.c +0
-0softmmu/vl.c void qemu_init(int argc, char **argv, char **envp) · line 2846
Quoted 9 lines
softmmu/vl.c +2
-1@@ -2839,7 +2839,8 @@ 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 ) { +if (netdev_parse_modern (optarg) ==-1 && +net_client_parse (qemu_find_opts ("netdev" ), optarg) ==-1 ) {exit (1 ); }break ;To make this work,
netdev_parse_modern()must* either succeeed, or
* fail without reporting an error, or
* report an error and
exit()Recommend to spell that out in its function comment.
Alternatively:
if (netdev_is_modern(optarg)) { netdev_parse_modern(optarg); } else { if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) { exit(1); } }
netdev_is_modern()needs external linkage, andnetdev_parse_modern()loses its return value.Note that all callers
net_client_parse()handle failure exactly the same way. If we letnet_client_parse()exit(), then this becomes
Quoted 8 lines
if (netdev_is_modern(optarg)) { netdev_parse_modern(optarg); } else { net_client_parse(qemu_find_opts("netdev"), optarg); }I like this one best. What do you think?
Me too.
I wanted to have only entry point in net.c it's why I didn't export netdev_is_modern() but I think it's better to export it not to mix error return (parse has failed) and checking result (is modern or not).
And I agree it's more consistent to have both parse functions behaving in the same way (exit or not exit...).
I update the patch in that way.
Thanks,
Laurent
Re: [RFC PATCH v3 03/11] qapi: net: introduce a way to bypass qemu_opts_parse_noisily()
In reply to Laurent VivierQuoted 90 lines · 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 bypassqemu_opts_parse_noisily()and use directlyvisit_type_Netdev()to parse the backend parameters.More details from Markus:
qemu_init()passes the argument of -netdev, -nic, and -net tonet_client_parse().
net_client_parse()parses withqemu_opts_parse_noisily(), passing QemuOptsListqemu_netdev_optsfor -netdev,qemu_nic_optsfor -nic, andqemu_net_optsfor -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 tonet_init_netdev(),net_param_nic(), ornet_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 commandnetdev_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 useskeyval_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.
Signed-off-by Laurent Vivier <[email protected]>--- include/net/net.h | 1 + net/net.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++ softmmu/vl.c | 3 ++- 3 files changed, 63 insertions(+), 1 deletion(-)include/net/net.h +1
-0@@ -214,6 +214,7 @@ extern NICInfo nd_table[MAX_NICS];extern const char *host_net_devices[];/* from net.c */ +int netdev_parse_modern (const char *optarg);int net_client_parse (QemuOptsList *opts_list,const char *str);void show_netdevs (void );void net_init_clients (void );net/net.c +11
-0@@ -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,16 @@static VMChangeStateEntry *net_change_state_entry; static QTAILQ_HEAD(, NetClientState) net_clients; +typedef struct NetdevQueueEntry { +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 */ @@ -1562,6 +1573,20 @@ out:return ret;
net/net.c out: · line 1583
Quoted 10 lines
net/net.c +8
-0} +static void netdev_init_modern (void ) +{ +while (!QSIMPLEQ_EMPTY (&nd_queue)) { +NetdevQueueEntry *nd =QSIMPLEQ_FIRST (&nd_queue); + +QSIMPLEQ_REMOVE_HEAD (&nd_queue, entry); +loc_push_restore (&nd->loc); +net_client_init1 (nd->nd, true, &error_fatal);
Accepts malformed IDs:
$ qemu-system-x86_64 -netdev type=stream,id=_,addr.type=inet,addr.host=localhost,addr.port=1234
qemu-system-x86_64: warning: netdev _ has no peerCompare:
$ qemu-system-x86_64 -netdev type=user,id=_
qemu-system-x86_64: -netdev type=user,id=_: Parameter 'id' expects an identifier
Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.Calling qmp_netdev_add() instead catches the error. It won't provide the hint, though. Some callers of id_wellformed() do, some don't. Factoring out bool check_id_wellformed(const char *id, Error **errp) could make sense.
Quoted 6 lines
net/net.c +6
-0+loc_pop (&nd->loc); +qapi_free_Netdev (nd->nd); +g_free (nd); + } +} +
[...]
RFC PATCH v3 4/11 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)
--- hmp-commands.hx | 2 +- net/clients.h | 6 + net/dgram.c | 630 ++++++++++++++++++++++++++++++++++++++++++++++++ net/hub.c | 2 + net/meson.build | 2 + net/net.c | 19 +- net/stream.c | 425 ++++++++++++++++++++++++++++++++ qapi/net.json | 40 ++- qemu-options.hx | 12 + 9 files changed, 1133 insertions(+), 5 deletions(-) create mode 100644 net/dgram.c create mode 100644 net/stream.c
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" + .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 +630 -0
630 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 +18 -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
@@ -1612,7 +1617,19 @@ void net_init_clients(void)*/ static bool netdev_is_modern (const char *optarg) { -return false; +QDict *args; +const char *type; +bool is_modern; + + args =keyval_parse (optarg,"type" ,NULL ,NULL ); +if (!args) { +return false; + } + type =qdict_get_try_str (args,"type" ); + is_modern = !g_strcmp0 (type,"stream" ) || !g_strcmp0 (type,"dgram" ); +qobject_unref (args); + +return is_modern; }/*
net/stream.c new file +425 -0
425 lines not on this page · raw message
qapi/net.json +37 -3
@@ -7,6 +7,7 @@ ## { 'include': 'common.json' } +{ 'include': 'sockets.json' } ## # @set_link:
@@ -566,6 +567,37 @@ '*isolated': 'bool' }, 'if': 'CONFIG_VMNET' } +## +# @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: #
@@ -579,9 +611,9 @@ # @vmnet-bridged since 7.1 ## { 'enum': 'NetClientDriver', - 'data': [ 'none', 'nic', 'user', 'tap', 'l2tpv3', 'socket', 'vde', - 'bridge', 'hubport', 'netmap', 'vhost-user', 'vhost-vdpa', - { 'name': 'vmnet-host', 'if': 'CONFIG_VMNET' }, + 'data': [ 'none', 'nic', 'user', 'tap', 'l2tpv3', 'socket', 'stream', + 'dgram', 'vde', 'bridge', 'hubport', 'netmap', 'vhost-user', + 'vhost-vdpa', { 'name': 'vmnet-host', 'if': 'CONFIG_VMNET' }, { 'name': 'vmnet-shared', 'if': 'CONFIG_VMNET' }, { 'name': 'vmnet-bridged', 'if': 'CONFIG_VMNET' }] }
@@ -610,6 +642,8 @@ 'tap': 'NetdevTapOptions', 'l2tpv3': 'NetdevL2TPv3Options', 'socket': 'NetdevSocketOptions', + 'stream': 'NetdevStreamOptions', + 'dgram': 'NetdevDgramOptions', 'vde': 'NetdevVdeOptions', 'bridge': 'NetdevBridgeOptions', 'hubport': 'NetdevHubPortOptions',
qemu-options.hx +12 -0
@@ -2727,6 +2727,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=host,local.port=port[,remote.type=inet,remote.host=host,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.36.1
Re: [RFC PATCH v3 04/11] qapi: net: add stream and dgram netdevs
Quoted 73 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)Signed-off-by Laurent Vivier <[email protected]>Reviewed-by Stefano Brivio <[email protected]>--- hmp-commands.hx | 2 +- net/clients.h | 6 + net/dgram.c | 630 ++++++++++++++++++++++++++++++++++++++++++++++++ net/hub.c | 2 + net/meson.build | 2 + net/net.c | 19 +- net/stream.c | 425 ++++++++++++++++++++++++++++++++ qapi/net.json | 40 ++- qemu-options.hx | 12 + 9 files changed, 1133 insertions(+), 5 deletions(-) create mode 100644 net/dgram.c create mode 100644 net/stream.chmp-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" + .params = "[user|tap|socket|stream|dgram|vde|bridge|hubport|netmap|vhost-user" #ifdef CONFIG_VMNET "|vmnet-host|vmnet-shared|vmnet-bridged" #endifnet/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 +13
-0@@ -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 672 lines
net/dgram.c new file +607
-0+#include "qemu/osdep.h" + +#include "net/net.h" +#include "clients.h" +#include "monitor/monitor.h" +#include "qapi/error.h" +#include "qemu/error-report.h" +#include "qemu/option.h" +#include "qemu/sockets.h" +#include "qemu/iov.h" +#include "qemu/main-loop.h" +#include "qemu/cutils.h" + +typedef struct NetDgramState { +NetClientState nc; +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 */ +bool read_poll;/* waiting to receive data? */ +bool write_poll;/* waiting to transmit data? */ +}NetDgramState ; + +static void net_dgram_accept (void *opaque); +static void net_dgram_writable (void *opaque); + +static void net_dgram_update_fd_handler (NetDgramState *s) +{ +qemu_set_fd_handler (s->fd, + s->read_poll ? s->send_fn :NULL , + s->write_poll ? net_dgram_writable :NULL , + s); +} + +static void net_dgram_read_poll (NetDgramState *s,bool enable) +{ + s->read_poll = enable; +net_dgram_update_fd_handler (s); +} + +static void net_dgram_write_poll (NetDgramState *s,bool enable) +{ + s->write_poll = enable; +net_dgram_update_fd_handler (s); +} + +static void net_dgram_writable (void *opaque) +{ +NetDgramState *s = opaque; + +net_dgram_write_poll (s, false); + +qemu_flush_queued_packets (&s->nc); +} + +static ssize_t net_dgram_receive_dgram (NetClientState *nc, +const uint8_t *buf,size_t size) +{ +NetDgramState *s =DO_UPCAST (NetDgramState, nc, 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)); + }else { + ret =send (s->fd, buf, size,0 ); + } + }while (ret ==-1 && errno ==EINTR ); + +if (ret ==-1 && errno ==EAGAIN ) { +net_dgram_write_poll (s, true); +return 0 ; + } +return ret; +} + +static void net_dgram_send_completed (NetClientState *nc,ssize_t len) +{ +NetDgramState *s =DO_UPCAST (NetDgramState, nc, nc); + +if (!s->read_poll) { +net_dgram_read_poll (s, true); + } +} + +static void net_dgram_rs_finalize (SocketReadState *rs) +{ +NetDgramState *s =container_of (rs, NetDgramState, rs); + +if (qemu_send_packet_async (&s->nc, rs->buf, + rs->packet_len, + net_dgram_send_completed) ==0 ) { +net_dgram_read_poll (s, false); + } +} + +static void net_dgram_send (void *opaque) +{ +NetDgramState *s = opaque; +int size; +int ret; +uint8_t buf1[NET_BUFSIZE ]; +const uint8_t *buf; + + size =recv (s->fd, buf1,sizeof (buf1),0 ); +if (size <0 ) { +if (errno !=EWOULDBLOCK ) { + goto eoc; + } + }else if (size ==0 ) { +/* end of connection */ + eoc: +net_dgram_read_poll (s, false); +net_dgram_write_poll (s, false); +if (s->listen_fd !=-1 ) { +qemu_set_fd_handler (s->listen_fd, net_dgram_accept,NULL , s); + } +closesocket (s->fd); + + s->fd =-1 ; +net_socket_rs_init (&s->rs, net_dgram_rs_finalize, false); + s->nc.link_down = true; +memset (s->nc.info_str,0 ,sizeof (s->nc.info_str)); + +return ; + } + buf = buf1; + + ret =net_fill_rstate (&s->rs, buf, size); + +if (ret ==-1 ) { + goto eoc; + } +} + +static void net_dgram_send_dgram (void *opaque) +{ +NetDgramState *s = opaque; +int size; + + size =recv (s->fd, s->rs.buf,sizeof (s->rs.buf),0 ); +if (size <0 ) { +return ; + } +if (size ==0 ) { +/* end of connection */ +net_dgram_read_poll (s, false); +net_dgram_write_poll (s, false); +return ; + } +if (qemu_send_packet_async (&s->nc, s->rs.buf, size, + net_dgram_send_completed) ==0 ) { +net_dgram_read_poll (s, false); + } +} + +static int net_dgram_mcast_create (struct sockaddr_in *mcastaddr, +struct in_addr *localaddr, +Error **errp) +{ +struct ip_mreq imr; +int fd; +int val, ret; +#ifdef __OpenBSD__ +unsigned char loop; +#else +int loop; +#endif + +if (!IN_MULTICAST (ntohl (mcastaddr->sin_addr.s_addr))) { +error_setg (errp,"specified mcastaddr %s (0x%08x) " +"does not contain a multicast address" , +inet_ntoa (mcastaddr->sin_addr), + (int )ntohl (mcastaddr->sin_addr.s_addr)); +return -1 ; + } + + fd =qemu_socket (PF_INET ,SOCK_DGRAM ,0 ); +if (fd <0 ) { +error_setg_errno (errp, errno,"can't create datagram socket" ); +return -1 ; + } + +/* +* Allow multiple sockets to bind the same multicast ip and port by setting +* SO_REUSEADDR. This is the only situation where SO_REUSEADDR should be set +* on windows. Use socket_set_fast_reuse otherwise as it sets SO_REUSEADDR +* only on posix systems. +*/ + val =1 ; + ret =setsockopt (fd,SOL_SOCKET ,SO_REUSEADDR , &val,sizeof (val)); +if (ret <0 ) { +error_setg_errno (errp, errno,"can't set socket option SO_REUSEADDR" ); + goto fail; + } + + ret =bind (fd, (struct sockaddr *)mcastaddr,sizeof (*mcastaddr)); +if (ret <0 ) { +error_setg_errno (errp, errno,"can't bind ip=%s to socket" , +inet_ntoa (mcastaddr->sin_addr)); + goto fail; + } + +/* Add host to multicast group */ + imr.imr_multiaddr = mcastaddr->sin_addr; +if (localaddr) { + imr.imr_interface = *localaddr; + }else { + imr.imr_interface.s_addr =htonl (INADDR_ANY ); + } + + ret =setsockopt (fd,IPPROTO_IP ,IP_ADD_MEMBERSHIP , + &imr,sizeof (struct ip_mreq )); +if (ret <0 ) { +error_setg_errno (errp, errno, +"can't add socket to multicast group %s" , +inet_ntoa (imr.imr_multiaddr)); + goto fail; + } + +/* Force mcast msgs to loopback (eg. several QEMUs in same host */ + loop =1 ; + ret =setsockopt (fd,IPPROTO_IP ,IP_MULTICAST_LOOP , + &loop,sizeof (loop)); +if (ret <0 ) { +error_setg_errno (errp, errno, +"can't force multicast message to loopback" ); + goto fail; + } + +/* If a bind address is given, only send packets from that address */ +if (localaddr !=NULL ) { + ret =setsockopt (fd,IPPROTO_IP ,IP_MULTICAST_IF , + localaddr,sizeof (*localaddr)); +if (ret <0 ) { +error_setg_errno (errp, errno, +"can't set the default network send interface" ); + goto fail; + } + } + +qemu_socket_set_nonblock (fd); +return fd; +fail: +if (fd >=0 ) { +closesocket (fd); + } +return -1 ; +} + +static void net_dgram_cleanup (NetClientState *nc) +{ +NetDgramState *s =DO_UPCAST (NetDgramState, nc, nc); +if (s->fd !=-1 ) { +net_dgram_read_poll (s, false); +net_dgram_write_poll (s, false); +close (s->fd); + s->fd =-1 ; + } +if (s->listen_fd !=-1 ) { +qemu_set_fd_handler (s->listen_fd,NULL ,NULL ,NULL ); +closesocket (s->listen_fd); + s->listen_fd =-1 ; + } +} + +static NetClientInfo net_dgram_socket_info = { + .type =NET_CLIENT_DRIVER_DGRAM , + .size =sizeof (NetDgramState), + .receive = net_dgram_receive_dgram, + .cleanup = net_dgram_cleanup, +}; + +static NetDgramState *net_dgram_fd_init_dgram (NetClientState *peer, +const char *model, +const char *name, +int fd,int is_fd, +SocketAddress *mcast, +Error **errp) +{ +struct sockaddr_in saddr; +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 ) { +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); + + s =DO_UPCAST (NetDgramState, nc, nc); + + s->fd = fd; + s->listen_fd =-1 ; + s->send_fn = net_dgram_send_dgram; +net_socket_rs_init (&s->rs, net_dgram_rs_finalize, false); +net_dgram_read_poll (s, true); + +/* mcast: save bound address as dst */ +if (is_fd && mcast !=NULL ) { + s->dgram_dst = 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 { +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)); + } + +return s; + +err: +closesocket (fd); +return NULL ; +} + +static void net_dgram_connect (void *opaque) +{ +NetDgramState *s = opaque; + s->send_fn = net_dgram_send; +net_dgram_read_poll (s, true); +} + +static void net_dgram_accept (void *opaque) +{ +NetDgramState *s = opaque; +struct sockaddr_in saddr; +socklen_t len; +int fd; + +for (;;) { + len =sizeof (saddr); + fd =qemu_accept (s->listen_fd, (struct sockaddr *)&saddr, &len); +if (fd <0 && errno !=EINTR ) { +return ; + }else if (fd >=0 ) { +qemu_set_fd_handler (s->listen_fd,NULL ,NULL ,NULL ); +break ; + } + } + + s->fd = fd; + s->nc.link_down = false; +net_dgram_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)); +} + +static int net_dgram_mcast_init (NetClientState *peer, +const char *model, +const char *name, +SocketAddress *remote, +SocketAddress *local, +Error **errp) +{ +NetDgramState *s; +int fd, ret; +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, + errp) <0 ) { +return -1 ; + } + +if (!local) { + fd =net_dgram_mcast_create (&saddr,NULL , errp); +if (fd <0 ) { +return -1 ; + } + }else { +switch (local->type) { +case SOCKET_ADDRESS_TYPE_INET : { +struct in_addr localaddr; + +if (inet_aton (local->u.inet.host, &localaddr) ==0 ) { +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); +if (fd <0 ) { +return -1 ; + } +break ; + } +case SOCKET_ADDRESS_TYPE_FD : + fd =monitor_fd_param (monitor_cur (), local->u.fd.str, errp); +if (fd ==-1 ) { +return -1 ; + } + ret =qemu_socket_try_set_nonblock (fd); +if (ret <0 ) { +error_setg_errno (errp, -ret,"%s: Can't use file descriptor %d" , + name, fd); +return -1 ; + } +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, + local->type ==SOCKET_ADDRESS_TYPE_FD , + remote, errp); +if (!s) { +return -1 ; + } + + s->dgram_dst = saddr; + +snprintf (s->nc.info_str,sizeof (s->nc.info_str),"mcast=%s:%d" , +inet_ntoa (saddr.sin_addr),ntohs (saddr.sin_port)); +return 0 ; + +} + +static int net_dgram_udp_init (NetClientState *peer, +const char *model, +const char *name, +SocketAddress *remote, +SocketAddress *local, +Error **errp) +{ +NetDgramState *s; +int fd, ret; +struct sockaddr_in raddr_in; +gchar *info_str; + +if (remote) { +if (local->type ==SOCKET_ADDRESS_TYPE_FD ) { +error_setg (errp,"don't set remote with local.fd" ); +return -1 ; + } +if (remote->type != local->type) { +error_setg (errp,"remote and local types must be the same" ); +return -1 ; + } + }else { +if (local->type !=SOCKET_ADDRESS_TYPE_FD ) { +error_setg (errp,"type=inet requires remote parameter" ); +return -1 ; + } + } + +switch (local->type) { +case SOCKET_ADDRESS_TYPE_INET : { +struct sockaddr_in laddr_in; + +if (convert_host_port (&laddr_in, local->u.inet.host, local->u.inet.port, + errp) <0 ) { +return -1 ; + } + +if (convert_host_port (&raddr_in, remote->u.inet.host, + remote->u.inet.port, errp) <0 ) { +return -1 ; + } + + fd =qemu_socket (PF_INET ,SOCK_DGRAM ,0 ); +if (fd <0 ) { +error_setg_errno (errp, errno,"can't create datagram socket" ); +return -1 ; + } + + ret =socket_set_fast_reuse (fd); +if (ret <0 ) { +error_setg_errno (errp, errno, +"can't set socket option SO_REUSEADDR" ); +closesocket (fd); +return -1 ; + } + ret =bind (fd, (struct sockaddr *)&laddr_in,sizeof (laddr_in)); +if (ret <0 ) { +error_setg_errno (errp, errno,"can't bind ip=%s to socket" , +inet_ntoa (laddr_in.sin_addr)); +closesocket (fd); +return -1 ; + } +qemu_socket_set_nonblock (fd); + + 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)); + +break ; + } +case SOCKET_ADDRESS_TYPE_FD : + fd =monitor_fd_param (monitor_cur (), local->u.fd.str, errp); +if (fd ==-1 ) { +return -1 ; + } + ret =qemu_socket_try_set_nonblock (fd); +if (ret <0 ) { +error_setg_errno (errp, -ret,"%s: Can't use file descriptor %d" , + name, fd); +return -1 ; + } +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); +if (!s) { +return -1 ; + } + +if (remote) { + s->dgram_dst = raddr_in; + +pstrcpy (s->nc.info_str,sizeof (s->nc.info_str), info_str); +g_free (info_str); + } +return 0 ; +} + +static int net_dgram_init (NetClientState *peer, +const char *model, +const char *name, +SocketAddress *remote, +SocketAddress *local, +Error **errp) +{ +/* detect multicast address */ +if (remote && remote->type ==SOCKET_ADDRESS_TYPE_INET ) { +struct sockaddr_in mcastaddr; + +if (convert_host_port (&mcastaddr, remote->u.inet.host, + remote->u.inet.port, errp) <0 ) { +return -1 ; + } + +if (IN_MULTICAST (ntohl (mcastaddr.sin_addr.s_addr))) { +return net_dgram_mcast_init (peer, model, name, remote, local, + errp); + } + } +/* unicast address */ +if (!local) { +error_setg (errp,"dgram requires local= parameter" ); +return -1 ; + } +return net_dgram_udp_init (peer, model, name, remote, local, errp); +} + +int net_init_dgram (const Netdev *netdev,const char *name, +NetClientState *peer,Error **errp) +{ +const NetdevDgramOptions *sock; + +assert (netdev->type ==NET_CLIENT_DRIVER_DGRAM ); + sock = &netdev->u.dgram; + +return net_dgram_init (peer,"dgram" , name, sock->remote, sock->local, + errp); +}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 +9
-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@@ -1612,7 +1617,19 @@ void net_init_clients(void)*/ static bool netdev_is_modern (const char *optarg) { -return false; +QDict *args; +const char *type; +bool is_modern; +
net/net.c void net_init_clients(void) · line 1633
Quoted 10 lines
net/net.c +9
-0+ args =keyval_parse (optarg,"type" ,NULL ,NULL ); +if (!args) { +return false; + } + type =qdict_get_try_str (args,"type" ); + is_modern = !g_strcmp0 (type,"stream" ) || !g_strcmp0 (type,"dgram" ); +qobject_unref (args); + +return is_modern; }
You could use g_autoptr here:
g_autoptr(QDict) args = NULL;
const char *type;
bool is_modern;
args = keyval_parse(optarg, "type", NULL, NULL);
if (!args) {
return false;
}
type = qdict_get_try_str(args, "type");
return !g_strcmp0(type, "stream") || !g_strcmp0(type, "dgram");Matter of taste; you decide.
Now recall how this function is used: it decides whether to parse the modern way (with qobject_input_visitor_new_str()) or the traditional way (with qemu_opts_parse_noisily()).
qemu_opts_parse_noisily() parses into a QemuOpts, for later use with the opts visitor.
qobject_input_visitor_new_str() supports both dotted keys and JSON. The former is parsed with keyval_parse(), the latter with qobject_from_json(). It returns the resulting parse tree wrapped in a suitable QAPI input visitor.
Issue 1: since we get there only when keyval_parse() succeeds, JSON is unreachable. Reproducer:
$ qemu-system-x86_64 -netdev '{"id":"foo"}'
upstream-qemu: -netdev {"id":"foo"}: Parameter 'id' is missingThis is parsed with qemu_opts_parse_noisily(), resulting in a QemuOpts with a single option 'type' with value '{"id":"foo"}'. The error message comes from the opts visitor.
To fix this, make netdev_is_modern() return true when optarg[0] == '{'. This matches how qobject_input_visitor_new_str() recognizes JSON.
Issue 2: when keyval_parse() detects an error, we throw it away and fall back to QemuOpts. This is commonly what we want. But not always. For instance:
$ qemu-system-x86_64 -netdev 'type=stream,id=foo,addr.type=inet,addr.host=localhost,addr.port=1234,addr.ipv4-off'
Note the typo "ipv4-off" instead of ipv4=off. The error reporting is crap:
qemu-system-x86_64: -netdev type=stream,id=foo,addr.type=inet,addr.host=localhost,addr.port=1234,addr.ipv4-off: warning: short-form boolean option 'addr.ipv4-off' deprecated Please use addr.ipv4-off=on instead
qemu-system-x86_64: -netdev type=stream,id=foo,addr.type=inet,addr.host=localhost,addr.port=1234,addr.ipv4-off: Parameter 'type' is missing
We get this because netdev_is_modern() guesses wrongly: keyval_parse() fails with the perfectly reasonable error message "Expected '=' after parameter 'addr.ipv4-off'", but netdev_is_modern() ignores the error, and fails. We fall back to QemuOpts, and confusion ensues.
I'm not sure we can do much better with reasonable effort. If we decide to accept this behavior, it should be documented at least in the source code.
Quoted 21 lines
net/net.c +0
-0/* net/stream.c new file +13
-0@@ -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 438 lines
net/stream.c new file +402
-0+#include "qemu/osdep.h" + +#include "net/net.h" +#include "clients.h" +#include "monitor/monitor.h" +#include "qapi/error.h" +#include "qemu/error-report.h" +#include "qemu/option.h" +#include "qemu/sockets.h" +#include "qemu/iov.h" +#include "qemu/main-loop.h" +#include "qemu/cutils.h" + +typedef struct NetStreamState { +NetClientState nc; +int listen_fd; +int fd; +SocketReadState rs; +unsigned int send_index;/* number of bytes sent*/ +IOHandler *send_fn; +bool read_poll;/* waiting to receive data? */ +bool write_poll;/* waiting to transmit data? */ +}NetStreamState ; + +static void net_stream_accept (void *opaque); +static void net_stream_writable (void *opaque); + +static void net_stream_update_fd_handler (NetStreamState *s) +{ +qemu_set_fd_handler (s->fd, + s->read_poll ? s->send_fn :NULL , + s->write_poll ? net_stream_writable :NULL , + s); +} + +static void net_stream_read_poll (NetStreamState *s,bool enable) +{ + s->read_poll = enable; +net_stream_update_fd_handler (s); +} + +static void net_stream_write_poll (NetStreamState *s,bool enable) +{ + s->write_poll = enable; +net_stream_update_fd_handler (s); +} + +static void net_stream_writable (void *opaque) +{ +NetStreamState *s = opaque; + +net_stream_write_poll (s, false); + +qemu_flush_queued_packets (&s->nc); +} + +static ssize_t net_stream_receive (NetClientState *nc,const uint8_t *buf, +size_t size) +{ +NetStreamState *s =DO_UPCAST (NetStreamState, nc, nc); +uint32_t len =htonl (size); +struct iovec iov[] = { + { + .iov_base = &len, + .iov_len =sizeof (len), + }, { + .iov_base = (void *)buf, + .iov_len = size, + }, + }; +size_t remaining; +ssize_t ret; + + remaining =iov_size (iov,2 ) - s->send_index; + ret =iov_send (s->fd, iov,2 , s->send_index, remaining); + +if (ret ==-1 && errno ==EAGAIN ) { + ret =0 ;/* handled further down */ + } +if (ret ==-1 ) { + s->send_index =0 ; +return -errno; + } +if (ret < (ssize_t )remaining) { + s->send_index += ret; +net_stream_write_poll (s, true); +return 0 ; + } + s->send_index =0 ; +return size; +} + +static void net_stream_send_completed (NetClientState *nc,ssize_t len) +{ +NetStreamState *s =DO_UPCAST (NetStreamState, nc, nc); + +if (!s->read_poll) { +net_stream_read_poll (s, true); + } +} + +static void net_stream_rs_finalize (SocketReadState *rs) +{ +NetStreamState *s =container_of (rs, NetStreamState, rs); + +if (qemu_send_packet_async (&s->nc, rs->buf, + rs->packet_len, + net_stream_send_completed) ==0 ) { +net_stream_read_poll (s, false); + } +} + +static void net_stream_send (void *opaque) +{ +NetStreamState *s = opaque; +int size; +int ret; +uint8_t buf1[NET_BUFSIZE ]; +const uint8_t *buf; + + size =recv (s->fd, buf1,sizeof (buf1),0 ); +if (size <0 ) { +if (errno !=EWOULDBLOCK ) { + goto eoc; + } + }else if (size ==0 ) { +/* end of connection */ + eoc: +net_stream_read_poll (s, false); +net_stream_write_poll (s, false); +if (s->listen_fd !=-1 ) { +qemu_set_fd_handler (s->listen_fd, net_stream_accept,NULL , s); + } +closesocket (s->fd); + + s->fd =-1 ; +net_socket_rs_init (&s->rs, net_stream_rs_finalize, false); + s->nc.link_down = true; +memset (s->nc.info_str,0 ,sizeof (s->nc.info_str)); + +return ; + } + buf = buf1; + + ret =net_fill_rstate (&s->rs, buf, size); + +if (ret ==-1 ) { + goto eoc; + } +} + +static void net_stream_cleanup (NetClientState *nc) +{ +NetStreamState *s =DO_UPCAST (NetStreamState, nc, nc); +if (s->fd !=-1 ) { +net_stream_read_poll (s, false); +net_stream_write_poll (s, false); +close (s->fd); + s->fd =-1 ; + } +if (s->listen_fd !=-1 ) { +qemu_set_fd_handler (s->listen_fd,NULL ,NULL ,NULL ); +closesocket (s->listen_fd); + s->listen_fd =-1 ; + } +} + +static void net_stream_connect (void *opaque) +{ +NetStreamState *s = opaque; + s->send_fn = net_stream_send; +net_stream_read_poll (s, true); +} + +static NetClientInfo net_stream_info = { + .type =NET_CLIENT_DRIVER_STREAM , + .size =sizeof (NetStreamState), + .receive = net_stream_receive, + .cleanup = net_stream_cleanup, +}; + +static NetStreamState *net_stream_fd_init_stream (NetClientState *peer, +const char *model, +const char *name, +int fd,int is_connected) +{ +NetClientState *nc; +NetStreamState *s; + + nc =qemu_new_net_client (&net_stream_info, peer, model, name); + +snprintf (nc->info_str,sizeof (nc->info_str),"fd=%d" , fd); + + s =DO_UPCAST (NetStreamState, nc, nc); + + s->fd = fd; + s->listen_fd =-1 ; +net_socket_rs_init (&s->rs, net_stream_rs_finalize, false); + +/* Disable Nagle algorithm on TCP sockets to reduce latency */ +socket_set_nodelay (fd); + +if (is_connected) { +net_stream_connect (s); + }else { +qemu_set_fd_handler (s->fd,NULL , net_stream_connect, s); + } +return s; +} + +static void net_stream_accept (void *opaque) +{ +NetStreamState *s = opaque; +struct sockaddr_in saddr; +socklen_t len; +int fd; + +for (;;) { + len =sizeof (saddr); + fd =qemu_accept (s->listen_fd, (struct sockaddr *)&saddr, &len); +if (fd <0 && errno !=EINTR ) { +return ; + }else if (fd >=0 ) { +qemu_set_fd_handler (s->listen_fd,NULL ,NULL ,NULL ); +break ; + } + } + + 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)); +} + +static int net_stream_server_init (NetClientState *peer, +const char *model, +const char *name, +SocketAddress *addr, +Error **errp) +{ +NetClientState *nc; +NetStreamState *s; +int fd, ret; + +switch (addr->type) { +case SOCKET_ADDRESS_TYPE_INET : { +struct sockaddr_in saddr_in; + +if (convert_host_port (&saddr_in, addr->u.inet.host, addr->u.inet.port, + errp) <0 ) { +return -1 ; + } + + fd =qemu_socket (PF_INET ,SOCK_STREAM ,0 ); +if (fd <0 ) { +error_setg_errno (errp, errno,"can't create stream socket" ); +return -1 ; + } +qemu_socket_set_nonblock (fd); + +socket_set_fast_reuse (fd); + + ret =bind (fd, (struct sockaddr *)&saddr_in,sizeof (saddr_in)); +if (ret <0 ) { +error_setg_errno (errp, errno,"can't bind ip=%s to socket" , +inet_ntoa (saddr_in.sin_addr)); +closesocket (fd); +return -1 ; + } +break ; + } +case SOCKET_ADDRESS_TYPE_UNIX : { +error_setg (errp,"only support inet type" ); +return -1 ; + } +case SOCKET_ADDRESS_TYPE_FD : + fd =monitor_fd_param (monitor_cur (), addr->u.fd.str, errp); +if (fd ==-1 ) { +return -1 ; + } + ret =qemu_socket_try_set_nonblock (fd); +if (ret <0 ) { +error_setg_errno (errp, -ret,"%s: Can't use file descriptor %d" , + name, fd); +return -1 ; + } +break ; +default : +g_assert_not_reached (); + } + + ret =listen (fd,0 ); +if (ret <0 ) { +error_setg_errno (errp, errno,"can't listen on socket" ); +closesocket (fd); +return -1 ; + } + + nc =qemu_new_net_client (&net_stream_info, peer, model, name); + s =DO_UPCAST (NetStreamState, nc, nc); + s->fd =-1 ; + s->listen_fd = fd; + s->nc.link_down = true; +net_socket_rs_init (&s->rs, net_stream_rs_finalize, false); + +qemu_set_fd_handler (s->listen_fd, net_stream_accept,NULL , s); +return 0 ; +} + +static int net_stream_client_init (NetClientState *peer, +const char *model, +const char *name, +SocketAddress *addr, +Error **errp) +{ +NetStreamState *s; +int fd, connected, ret; +gchar *info_str; + +switch (addr->type) { +case SOCKET_ADDRESS_TYPE_INET : { +struct sockaddr_in saddr_in; + +if (convert_host_port (&saddr_in, addr->u.inet.host, addr->u.inet.port, + errp) <0 ) { +return -1 ; + } + + fd =qemu_socket (PF_INET ,SOCK_STREAM ,0 ); +if (fd <0 ) { +error_setg_errno (errp, errno,"can't create stream socket" ); +return -1 ; + } +qemu_socket_set_nonblock (fd); + + connected =0 ; +for (;;) { + ret =connect (fd, (struct sockaddr *)&saddr_in,sizeof (saddr_in)); +if (ret <0 ) { +if (errno ==EINTR || errno ==EWOULDBLOCK ) { +/* continue */ + }else if (errno ==EINPROGRESS || + errno ==EALREADY || + errno ==EINVAL ) { +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:%d" , +inet_ntoa (saddr_in.sin_addr), +ntohs (saddr_in.sin_port)); +break ; + } +case SOCKET_ADDRESS_TYPE_FD : + fd =monitor_fd_param (monitor_cur (), addr->u.fd.str, errp); +if (fd ==-1 ) { +return -1 ; + } + ret =qemu_socket_try_set_nonblock (fd); +if (ret <0 ) { +error_setg_errno (errp, -ret,"%s: Can't use file descriptor %d" , + name, fd); +return -1 ; + } + connected =1 ; + info_str =g_strdup_printf ("connect to fd %d" , fd); +break ; +default : +error_setg (errp,"only support inet or fd type" ); +return -1 ; + } + + s =net_stream_fd_init_stream (peer, model, name, fd, connected); + +pstrcpy (s->nc.info_str,sizeof (s->nc.info_str), info_str); +g_free (info_str); + +return 0 ; +} + +int net_init_stream (const Netdev *netdev,const char *name, +NetClientState *peer,Error **errp) +{ +const NetdevStreamOptions *sock; + +assert (netdev->type ==NET_CLIENT_DRIVER_STREAM ); + sock = &netdev->u.stream; + +if (!sock->has_server || sock->server) { +return net_stream_server_init (peer,"stream" , name, sock->addr, errp); + } +return net_stream_client_init (peer,"stream" , name, sock->addr, errp); +}qapi/net.json +21
-0@@ -7,6 +7,7 @@ ## { 'include': 'common.json' } +{ 'include': 'sockets.json' } ## # @set_link:@@ -566,6 +567,37 @@ '*isolated': 'bool' }, 'if': 'CONFIG_VMNET' } +## +# @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.
qapi/net.json line 599
Quoted 10 lines
qapi/net.json +10
-0+# +# @remote: remote address +# @local: local address +# +# Since: 7.1 +## +{ 'struct': 'NetdevDgramOptions', + 'data': { + '*local': 'SocketAddress', + '*remote': 'SocketAddress' } }
In review of v2, I inquired about behavior when members are absent. You wrote:
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.
Please work that into the doc comment.
Quoted 6 lines
qapi/net.json +1
-0+ ## # @NetClientDriver: #@@ -579,9 +611,9 @@ # @vmnet-bridged since 7.1
qapi/net.json line 618
Quoted 10 lines
qapi/net.json +3
-3## { 'enum': 'NetClientDriver', - 'data': [ 'none', 'nic', 'user', 'tap', 'l2tpv3', 'socket', 'vde', - 'bridge', 'hubport', 'netmap', 'vhost-user', 'vhost-vdpa', - { 'name': 'vmnet-host', 'if': 'CONFIG_VMNET' }, + 'data': [ 'none', 'nic', 'user', 'tap', 'l2tpv3', 'socket', 'stream', + 'dgram', 'vde', 'bridge', 'hubport', 'netmap', 'vhost-user', + 'vhost-vdpa', { 'name': 'vmnet-host', 'if': 'CONFIG_VMNET' }, { 'name': 'vmnet-shared', 'if': 'CONFIG_VMNET' }, { 'name': 'vmnet-bridged', 'if': 'CONFIG_VMNET' }] }
Opportunity to put vmnet-host on its own line for readability.
Quoted 33 lines
qapi/net.json +2
-0@@ -610,6 +642,8 @@ 'tap': 'NetdevTapOptions', 'l2tpv3': 'NetdevL2TPv3Options', 'socket': 'NetdevSocketOptions', + 'stream': 'NetdevStreamOptions', + 'dgram': 'NetdevDgramOptions', 'vde': 'NetdevVdeOptions', 'bridge': 'NetdevBridgeOptions', 'hubport': 'NetdevHubPortOptions',qemu-options.hx +12
-0@@ -2727,6 +2727,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=host,local.port=port[,remote.type=inet,remote.host=host,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"
Re: [RFC PATCH v3 04/11] qapi: net: add stream and dgram netdevs
Quoted 26 lines · Markus Armbruster
Quoted 24 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)Signed-off-by Laurent Vivier <[email protected]>Reviewed-by Stefano Brivio <[email protected]>--- hmp-commands.hx | 2 +- net/clients.h | 6 + net/dgram.c | 630 ++++++++++++++++++++++++++++++++++++++++++++++++ net/hub.c | 2 + net/meson.build | 2 + net/net.c | 19 +- net/stream.c | 425 ++++++++++++++++++++++++++++++++ qapi/net.json | 40 ++- qemu-options.hx | 12 + 9 files changed, 1133 insertions(+), 5 deletions(-)create mode 100644
net/dgram.c
create mode 100644net/stream.c
...
Quoted 23 lines
Quoted 23 lines
net/dgram.c new file +17
-0@@ -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 +* 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,
Quoted 8 lines
net/dgram.c line 23
Quoted 6 lines
net/dgram.c new file +6
-0+* 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.
Done
...
Quoted 4 lines
Quoted 4 lines
net/net.c +0
-0
...
Quoted 33 lines
Quoted 9 lines
net/net.c +4
-1@@ -1612,7 +1617,19 @@ void net_init_clients(void)*/ static bool netdev_is_modern (const char *optarg) { -return false; +QDict *args; +const char *type; +bool is_modern; +net/net.c void net_init_clients(void) · line 1633
Quoted 10 lines
net/net.c +9
-0+ args =keyval_parse (optarg,"type" ,NULL ,NULL ); +if (!args) { +return false; + } + type =qdict_get_try_str (args,"type" ); + is_modern = !g_strcmp0 (type,"stream" ) || !g_strcmp0 (type,"dgram" ); +qobject_unref (args); + +return is_modern; }You could use
g_autoptrhere:g_autoptr(QDict) args = NULL; const char *type; bool is_modern; args = keyval_parse(optarg, "type", NULL, NULL); if (!args) { return false; } type = qdict_get_try_str(args, "type"); return !g_strcmp0(type, "stream") || !g_strcmp0(type, "dgram");
Quoted 1 line
Matter of taste; you decide.
Looks good. We already had some series to convert existing code to g_autoptr(), so it seems the way to do.
Quoted 19 lines
Now recall how this function is used: it decides whether to parse the modern way (with
qobject_input_visitor_new_str()) or the traditional way (withqemu_opts_parse_noisily()).
qemu_opts_parse_noisily()parses into a QemuOpts, for later use with the opts visitor.
qobject_input_visitor_new_str()supports both dotted keys and JSON. The former is parsed withkeyval_parse(), the latter withqobject_from_json(). It returns the resulting parse tree wrapped in a suitable QAPI input visitor.Issue 1: since we get there only when
keyval_parse()succeeds, JSON is unreachable. Reproducer:$ qemu-system-x86_64 -netdev '{"id":"foo"}' upstream-qemu: -netdev {"id":"foo"}: Parameter 'id' is missing
Quoted 6 lines
This is parsed with
qemu_opts_parse_noisily(), resulting in a QemuOpts with a single option 'type' with value '{"id":"foo"}'. The error message comes from the opts visitor.To fix this, make
netdev_is_modern()return true when optarg[0] == '{'. This matches howqobject_input_visitor_new_str()recognizes JSON.
OK
Quoted 13 lines
Issue 2: when
keyval_parse()detects an error, we throw it away and fall back to QemuOpts. This is commonly what we want. But not always. For instance:$ qemu-system-x86_64 -netdev 'type=stream,id=foo,addr.type=inet,addr.host=localhost,addr.port=1234,addr.ipv4-off'Note the typo "ipv4-off" instead of ipv4=off. The error reporting is crap:
qemu-system-x86_64: -netdev type=stream,id=foo,addr.type=inet,addr.host=localhost,addr.port=1234,addr.ipv4-off: warning: short-form boolean option 'addr.ipv4-off' deprecated Please use addr.ipv4-off=on instead
qemu-system-x86_64: -netdev type=stream,id=foo,addr.type=inet,addr.host=localhost,addr.port=1234,addr.ipv4-off: Parameter 'type' is missing
Quoted 8 lines
We get this because
netdev_is_modern()guesses wrongly:keyval_parse()fails with the perfectly reasonable error message "Expected '=' after parameter 'addr.ipv4-off'", butnetdev_is_modern()ignores the error, and fails. We fall back to QemuOpts, and confusion ensues.I'm not sure we can do much better with reasonable effort. If we decide to accept this behavior, it should be documented at least in the source code.
What about using modern syntax by default?
args = keyval_parse(optarg, "type", NULL, NULL);
if (!args) {
/* cannot detect the syntax, use new style syntax */
return true;
}Quoted 25 lines
Quoted 25 lines
net/net.c +0
-0/* net/stream.c new file +17
-0@@ -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 +* 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,
Quoted 8 lines
net/stream.c line 23
Quoted 6 lines
net/stream.c new file +6
-0+* 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.
Done
...
Quoted 4 lines
Quoted 4 lines
qapi/net.json +0
-0
...
Quoted 38 lines
Quoted 24 lines
qapi/net.json +20
-0@@ -566,6 +567,37 @@ '*isolated': 'bool' }, 'if': 'CONFIG_VMNET' } +## +# @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.qapi/net.json line 599
Quoted 10 lines
qapi/net.json +10
-0+# +# @remote: remote address +# @local: local address +# +# Since: 7.1 +## +{ 'struct': 'NetdevDgramOptions', + 'data': { + '*local': 'SocketAddress', + '*remote': 'SocketAddress' } }In review of v2, I inquired about behavior when members are absent. You wrote:
Quoted 7 lines
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.
Please work that into the doc comment.
OK
Quoted 11 lines
Quoted 11 lines
qapi/net.json +1
-2+ ## # @NetClientDriver: #@@ -579,9 +611,9 @@ # @vmnet-bridged since 7.1 ## { 'enum': 'NetClientDriver', - 'data': [ 'none', 'nic', 'user', 'tap', 'l2tpv3', 'socket', 'vde', - 'bridge', 'hubport', 'netmap', 'vhost-user', 'vhost-vdpa',
Quoted 9 lines
qapi/net.json line 618
Quoted 6 lines
qapi/net.json +3
-1- { 'name': 'vmnet-host', 'if': 'CONFIG_VMNET' }, + 'data': [ 'none', 'nic', 'user', 'tap', 'l2tpv3', 'socket', 'stream', + 'dgram', 'vde', 'bridge', 'hubport', 'netmap', 'vhost-user', + 'vhost-vdpa', { 'name': 'vmnet-host', 'if': 'CONFIG_VMNET' }, { 'name': 'vmnet-shared', 'if': 'CONFIG_VMNET' }, { 'name': 'vmnet-bridged', 'if': 'CONFIG_VMNET' }] }Opportunity to put vmnet-host on its own line for readability.
OK
Thanks,
Laurent
Re: [RFC PATCH v3 04/11] qapi: net: add stream and dgram netdevs
Quoted 14 lines · Laurent Vivier
Quoted 13 lines · Markus Armbruster
Quoted 11 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)Signed-off-by Laurent Vivier <[email protected]>Reviewed-by Stefano Brivio <[email protected]>---
[...]
Quoted 93 lines
Quoted 4 lines
Quoted 4 lines
net/net.c +0
-0...
Quoted 33 lines
Quoted 9 lines
net/net.c +4
-1@@ -1612,7 +1617,19 @@ void net_init_clients(void)*/ static bool netdev_is_modern (const char *optarg) { -return false; +QDict *args; +const char *type; +bool is_modern; +net/net.c void net_init_clients(void) · line 1633
Quoted 10 lines
net/net.c +9
-0+ args =keyval_parse (optarg,"type" ,NULL ,NULL ); +if (!args) { +return false; + } + type =qdict_get_try_str (args,"type" ); + is_modern = !g_strcmp0 (type,"stream" ) || !g_strcmp0 (type,"dgram" ); +qobject_unref (args); + +return is_modern; }You could use
g_autoptrhere:g_autoptr(QDict) args = NULL; const char *type; bool is_modern; args = keyval_parse(optarg, "type", NULL, NULL); if (!args) { return false; } type = qdict_get_try_str(args, "type"); return !g_strcmp0(type, "stream") || !g_strcmp0(type, "dgram");Quoted 1 line
Matter of taste; you decide.
Looks good. We already had some series to convert existing code to
g_autoptr(), so it seems the way to do.Quoted 19 lines
Now recall how this function is used: it decides whether to parse the modern way (with
qobject_input_visitor_new_str()) or the traditional way (withqemu_opts_parse_noisily()).
qemu_opts_parse_noisily()parses into a QemuOpts, for later use with the opts visitor.
qobject_input_visitor_new_str()supports both dotted keys and JSON. The former is parsed withkeyval_parse(), the latter withqobject_from_json(). It returns the resulting parse tree wrapped in a suitable QAPI input visitor.Issue 1: since we get there only when
keyval_parse()succeeds, JSON is unreachable. Reproducer:$ qemu-system-x86_64 -netdev '{"id":"foo"}' upstream-qemu: -netdev {"id":"foo"}: Parameter 'id' is missingQuoted 6 lines
This is parsed with
qemu_opts_parse_noisily(), resulting in a QemuOpts with a single option 'type' with value '{"id":"foo"}'. The error message comes from the opts visitor.To fix this, make
netdev_is_modern()return true when optarg[0] == '{'. This matches howqobject_input_visitor_new_str()recognizes JSON.OK
Quoted 13 lines
Issue 2: when
keyval_parse()detects an error, we throw it away and fall back to QemuOpts. This is commonly what we want. But not always. For instance:$ qemu-system-x86_64 -netdev 'type=stream,id=foo,addr.type=inet,addr.host=localhost,addr.port=1234,addr.ipv4-off'Note the typo "ipv4-off" instead of ipv4=off. The error reporting is crap:
qemu-system-x86_64: -netdev type=stream,id=foo,addr.type=inet,addr.host=localhost,addr.port=1234,addr.ipv4-off: warning: short-form boolean option 'addr.ipv4-off' deprecated Please use addr.ipv4-off=on instead
qemu-system-x86_64: -netdev type=stream,id=foo,addr.type=inet,addr.host=localhost,addr.port=1234,addr.ipv4-off: Parameter 'type' is missingQuoted 8 lines
We get this because
netdev_is_modern()guesses wrongly:keyval_parse()fails with the perfectly reasonable error message "Expected '=' after parameter 'addr.ipv4-off'", butnetdev_is_modern()ignores the error, and fails. We fall back to QemuOpts, and confusion ensues.I'm not sure we can do much better with reasonable effort. If we decide to accept this behavior, it should be documented at least in the source code.
Quoted 7 lines
What about using modern syntax by default?
args = keyval_parse(optarg, "type", NULL, NULL); if (!args) { /* cannot detect the syntax, use new style syntax */ return true; }
As is, netdev_is_modern() has three cases:
1. keyval_parse() fails
2. keyval_parse() succeeds, but value of @type is not modern
3. keyval_parse() succeeds, and value of @type is modern
In case 3. we're sure, because even if qemu_opts_parse_noisily() also succeeded, it would result in the same value of @type.
In case 2, assuming traditional seems reasonable. The assumption can be wrong when the user intends modern, but fat-fingers the type=T part.
In case 1, we know nothing.
Guessing modern is wrong when the user intends traditional. This happens when a meant-to-be-traditional @optarg also parses as modern. Quite possible.
Guessing traditional is wrong when the user intends modern. This happens when a meant-to-be-modern @optarg fails to parse as modern, i.e. whenever the user screws up modern syntax.
Which guess is less bad? I'm not sure. Thoughts?
Note that additionally checking whether qemu_opts_parse() succeeds would be next to useless, since qemu_opts_parse() accepts pretty much anything.
[...]
Re: [RFC PATCH v3 04/11] qapi: net: add stream and dgram netdevs
Quoted 131 lines · Markus Armbruster
Quoted 14 lines · Laurent Vivier
Quoted 13 lines · Markus Armbruster
Quoted 11 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)Signed-off-by Laurent Vivier <[email protected]>Reviewed-by Stefano Brivio <[email protected]>---
[...]
Quoted 93 lines
Quoted 4 lines
Quoted 4 lines
net/net.c +0
-0...
Quoted 33 lines
Quoted 9 lines
net/net.c +4
-1@@ -1612,7 +1617,19 @@ void net_init_clients(void)*/ static bool netdev_is_modern (const char *optarg) { -return false; +QDict *args; +const char *type; +bool is_modern; +net/net.c void net_init_clients(void) · line 1633
Quoted 10 lines
net/net.c +9
-0+ args =keyval_parse (optarg,"type" ,NULL ,NULL ); +if (!args) { +return false; + } + type =qdict_get_try_str (args,"type" ); + is_modern = !g_strcmp0 (type,"stream" ) || !g_strcmp0 (type,"dgram" ); +qobject_unref (args); + +return is_modern; }You could use
g_autoptrhere:g_autoptr(QDict) args = NULL; const char *type; bool is_modern; args = keyval_parse(optarg, "type", NULL, NULL); if (!args) { return false; } type = qdict_get_try_str(args, "type"); return !g_strcmp0(type, "stream") || !g_strcmp0(type, "dgram");Quoted 1 line
Matter of taste; you decide.
Looks good. We already had some series to convert existing code to
g_autoptr(), so it seems the way to do.Quoted 19 lines
Now recall how this function is used: it decides whether to parse the modern way (with
qobject_input_visitor_new_str()) or the traditional way (withqemu_opts_parse_noisily()).
qemu_opts_parse_noisily()parses into a QemuOpts, for later use with the opts visitor.
qobject_input_visitor_new_str()supports both dotted keys and JSON. The former is parsed withkeyval_parse(), the latter withqobject_from_json(). It returns the resulting parse tree wrapped in a suitable QAPI input visitor.Issue 1: since we get there only when
keyval_parse()succeeds, JSON is unreachable. Reproducer:$ qemu-system-x86_64 -netdev '{"id":"foo"}' upstream-qemu: -netdev {"id":"foo"}: Parameter 'id' is missingQuoted 6 lines
This is parsed with
qemu_opts_parse_noisily(), resulting in a QemuOpts with a single option 'type' with value '{"id":"foo"}'. The error message comes from the opts visitor.To fix this, make
netdev_is_modern()return true when optarg[0] == '{'. This matches howqobject_input_visitor_new_str()recognizes JSON.OK
Quoted 13 lines
Issue 2: when
keyval_parse()detects an error, we throw it away and fall back to QemuOpts. This is commonly what we want. But not always. For instance:$ qemu-system-x86_64 -netdev 'type=stream,id=foo,addr.type=inet,addr.host=localhost,addr.port=1234,addr.ipv4-off'Note the typo "ipv4-off" instead of ipv4=off. The error reporting is crap:
qemu-system-x86_64: -netdev type=stream,id=foo,addr.type=inet,addr.host=localhost,addr.port=1234,addr.ipv4-off: warning: short-form boolean option 'addr.ipv4-off' deprecated Please use addr.ipv4-off=on instead
qemu-system-x86_64: -netdev type=stream,id=foo,addr.type=inet,addr.host=localhost,addr.port=1234,addr.ipv4-off: Parameter 'type' is missingQuoted 8 lines
We get this because
netdev_is_modern()guesses wrongly:keyval_parse()fails with the perfectly reasonable error message "Expected '=' after parameter 'addr.ipv4-off'", butnetdev_is_modern()ignores the error, and fails. We fall back to QemuOpts, and confusion ensues.I'm not sure we can do much better with reasonable effort. If we decide to accept this behavior, it should be documented at least in the source code.
Quoted 7 lines
What about using modern syntax by default?
args = keyval_parse(optarg, "type", NULL, NULL); if (!args) { /* cannot detect the syntax, use new style syntax */ return true; }As is,
netdev_is_modern()has three cases:1.
keyval_parse()fails2.
keyval_parse()succeeds, but value of @type is not modern3.
keyval_parse()succeeds, and value of @type is modernIn case 3. we're sure, because even if
qemu_opts_parse_noisily()also succeeded, it would result in the same value of @type.
Quoted 8 lines
In case 2, assuming traditional seems reasonable. The assumption can be wrong when the user intends modern, but fat-fingers the type=T part.
In case 1, we know nothing.
Guessing modern is wrong when the user intends traditional. This happens when a meant-to-be-traditional @optarg also parses as modern. Quite possible.
I don't see why keyval_parse() fails in this case. Any example?
Quoted 3 lines
Guessing traditional is wrong when the user intends modern. This happens when a meant-to-be-modern @optarg fails to parse as modern, i.e. whenever the user screws up modern syntax.
This one is the example you gave (ipv4-off)
Quoted 1 line
Which guess is less bad? I'm not sure. Thoughts?
Perhaps we can simply fail if keyval_parse() fails?
something like:
args = keyval_parse(optarg, "type", NULL, &error_fatal);
type = qdict_get_try_str(args, "type");
return !g_strcmp0(type, "stream") || !g_strcmp0(type, "dgram");Thanks,
Laurent
Re: [RFC PATCH v3 04/11] qapi: net: add stream and dgram netdevs
Quoted 135 lines · Laurent Vivier
Quoted 134 lines · Markus Armbruster
Quoted 14 lines · Laurent Vivier
Quoted 13 lines · Markus Armbruster
Quoted 11 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)Signed-off-by Laurent Vivier <[email protected]>Reviewed-by Stefano Brivio <[email protected]>---
[...]
Quoted 93 lines
Quoted 4 lines
Quoted 4 lines
net/net.c +0
-0...
Quoted 33 lines
Quoted 9 lines
net/net.c +4
-1@@ -1612,7 +1617,19 @@ void net_init_clients(void)*/ static bool netdev_is_modern (const char *optarg) { -return false; +QDict *args; +const char *type; +bool is_modern; +net/net.c void net_init_clients(void) · line 1633
Quoted 10 lines
net/net.c +9
-0+ args =keyval_parse (optarg,"type" ,NULL ,NULL ); +if (!args) { +return false; + } + type =qdict_get_try_str (args,"type" ); + is_modern = !g_strcmp0 (type,"stream" ) || !g_strcmp0 (type,"dgram" ); +qobject_unref (args); + +return is_modern; }You could use
g_autoptrhere:g_autoptr(QDict) args = NULL; const char *type; bool is_modern; args = keyval_parse(optarg, "type", NULL, NULL); if (!args) { return false; } type = qdict_get_try_str(args, "type"); return !g_strcmp0(type, "stream") || !g_strcmp0(type, "dgram");Quoted 1 line
Matter of taste; you decide.
Looks good. We already had some series to convert existing code to
g_autoptr(), so it seems the way to do.Quoted 19 lines
Now recall how this function is used: it decides whether to parse the modern way (with
qobject_input_visitor_new_str()) or the traditional way (withqemu_opts_parse_noisily()).
qemu_opts_parse_noisily()parses into a QemuOpts, for later use with the opts visitor.
qobject_input_visitor_new_str()supports both dotted keys and JSON. The former is parsed withkeyval_parse(), the latter withqobject_from_json(). It returns the resulting parse tree wrapped in a suitable QAPI input visitor.Issue 1: since we get there only when
keyval_parse()succeeds, JSON is unreachable. Reproducer:$ qemu-system-x86_64 -netdev '{"id":"foo"}' upstream-qemu: -netdev {"id":"foo"}: Parameter 'id' is missingQuoted 6 lines
This is parsed with
qemu_opts_parse_noisily(), resulting in a QemuOpts with a single option 'type' with value '{"id":"foo"}'. The error message comes from the opts visitor.To fix this, make
netdev_is_modern()return true when optarg[0] == '{'. This matches howqobject_input_visitor_new_str()recognizes JSON.OK
Quoted 13 lines
Issue 2: when
keyval_parse()detects an error, we throw it away and fall back to QemuOpts. This is commonly what we want. But not always. For instance:$ qemu-system-x86_64 -netdev 'type=stream,id=foo,addr.type=inet,addr.host=localhost,addr.port=1234,addr.ipv4-off'Note the typo "ipv4-off" instead of ipv4=off. The error reporting is crap:
qemu-system-x86_64: -netdev type=stream,id=foo,addr.type=inet,addr.host=localhost,addr.port=1234,addr.ipv4-off: warning: short-form boolean option 'addr.ipv4-off' deprecated Please use addr.ipv4-off=on instead
qemu-system-x86_64: -netdev type=stream,id=foo,addr.type=inet,addr.host=localhost,addr.port=1234,addr.ipv4-off: Parameter 'type' is missingQuoted 8 lines
We get this because
netdev_is_modern()guesses wrongly:keyval_parse()fails with the perfectly reasonable error message "Expected '=' after parameter 'addr.ipv4-off'", butnetdev_is_modern()ignores the error, and fails. We fall back to QemuOpts, and confusion ensues.I'm not sure we can do much better with reasonable effort. If we decide to accept this behavior, it should be documented at least in the source code.
Quoted 7 lines
What about using modern syntax by default?
args = keyval_parse(optarg, "type", NULL, NULL); if (!args) { /* cannot detect the syntax, use new style syntax */ return true; }As is,
netdev_is_modern()has three cases:1.
keyval_parse()fails2.
keyval_parse()succeeds, but value of @type is not modern3.
keyval_parse()succeeds, and value of @type is modernIn case 3. we're sure, because even if
qemu_opts_parse_noisily()also succeeded, it would result in the same value of @type.In case 2, assuming traditional seems reasonable. The assumption can be wrong when the user intends modern, but fat-fingers the type=T part.
Quoted 7 lines
Quoted 5 lines
In case 1, we know nothing.
Guessing modern is wrong when the user intends traditional. This happens when a meant-to-be-traditional @optarg also parses as modern. Quite possible.
I don't see why
keyval_parse()fails in this case. Any example?
Brain cramp on my part, I'm afraid %-} Let me start over.
Guessing modern is wrong when the user intends traditional. Two sub-cases then:
* @optarg parses fine as traditional. For instance,
$ qemu-system-x86_64 -netdev type=user,id=foo,ipv4
parses with a warning:
option 'ipv4' deprecated
Please use ipv4=on instead
This is how current master behaves.
Guessing modern makes this fail instead:
qemu-system-x86_64: -netdev type=user,id=foo,ipv4: Expected '=' after parameter 'ipv4'
Regression.
* @optarg fails to parse traditional, too. The error reporting is for modern even though the user intends traditional. Can be misleading. Example:
$ qemu-system-x86_64 -netdev type=user,id=_,ipv4
Current master:
qemu-system-x86_64: -netdev type=user,id=_,ipv4: Parameter 'id' expects an identifier Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.
Guessing modern instead:
qemu-system-x86_64: -netdev type=user,id=_,ipv4: Expected '=' after parameter 'ipv4'
This should be rare in practice, as traditional parsing detects very few errors.
Quoted 5 lines
Quoted 3 lines
Guessing traditional is wrong when the user intends modern. This happens when a meant-to-be-modern @optarg fails to parse as modern, i.e. whenever the user screws up modern syntax.
This one is the example you gave (ipv4-off)
Two sub-cases then:
* @optarg parses fine as traditional. The parse result is unlikely to make sense, though. For instance,
$ qemu-system-x86_64 -netdev type=stream,id=foo,server
parses with a warning:
qemu-system-x86_64: -netdev type=stream,id=foo,server: warning: short-form boolean option 'server' deprecated Please use server=on instead
But the result fails in the opts visitor:
qemu-system-x86_64: -netdev type=stream,id=foo,server: Parameter 'type' is missing
In this case, we're better off with guessing modern:
qemu-system-x86_64: -netdev type=stream,id=foo,server: Expected '=' after parameter
* @optarg fails to parse traditional, too. The error reporting is for traditional even though the user intends modern. Can be misleading.
This is my ipv4-off example.
Can't win. Parsers simply don't compose that way.
Quoted 9 lines
Quoted 1 line
Which guess is less bad? I'm not sure. Thoughts?
Perhaps we can simply fail if
keyval_parse()fails?something like:
args = keyval_parse(optarg, "type", NULL, &error_fatal); type = qdict_get_try_str(args, "type"); return !g_strcmp0(type, "stream") || !g_strcmp0(type, "dgram");
This rejects working option arguments that don't also parse as modern, such as "-netdev type=user,id=foo,ipv4".
Guessing traditional seems to be the least bad solution so far.
Supporting both traditional and modern syntax in an option argument is a swamp. Can we bypass it somehow?
-object uses traditional QemuOpts parsing for key=value,..., and modern parsing for JSON. Sticking to traditional sidesteps compatibility issues. But you have to use JSON for things traditional can't express.
Thoughts?
Re: [RFC PATCH v3 04/11] qapi: net: add stream and dgram netdevs
Quoted 233 lines · Markus Armbruster
Quoted 135 lines · Laurent Vivier
Quoted 134 lines · Markus Armbruster
Quoted 14 lines · Laurent Vivier
Quoted 13 lines · Markus Armbruster
Quoted 11 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)Signed-off-by Laurent Vivier <[email protected]>Reviewed-by Stefano Brivio <[email protected]>---
[...]
Quoted 93 lines
Quoted 4 lines
Quoted 4 lines
net/net.c +0
-0...
Quoted 33 lines
Quoted 9 lines
net/net.c +4
-1@@ -1612,7 +1617,19 @@ void net_init_clients(void)*/ static bool netdev_is_modern (const char *optarg) { -return false; +QDict *args; +const char *type; +bool is_modern; +net/net.c void net_init_clients(void) · line 1633
Quoted 10 lines
net/net.c +9
-0+ args =keyval_parse (optarg,"type" ,NULL ,NULL ); +if (!args) { +return false; + } + type =qdict_get_try_str (args,"type" ); + is_modern = !g_strcmp0 (type,"stream" ) || !g_strcmp0 (type,"dgram" ); +qobject_unref (args); + +return is_modern; }You could use
g_autoptrhere:g_autoptr(QDict) args = NULL; const char *type; bool is_modern; args = keyval_parse(optarg, "type", NULL, NULL); if (!args) { return false; } type = qdict_get_try_str(args, "type"); return !g_strcmp0(type, "stream") || !g_strcmp0(type, "dgram");Quoted 1 line
Matter of taste; you decide.
Looks good. We already had some series to convert existing code to
g_autoptr(), so it seems the way to do.Quoted 19 lines
Now recall how this function is used: it decides whether to parse the modern way (with
qobject_input_visitor_new_str()) or the traditional way (withqemu_opts_parse_noisily()).
qemu_opts_parse_noisily()parses into a QemuOpts, for later use with the opts visitor.
qobject_input_visitor_new_str()supports both dotted keys and JSON. The former is parsed withkeyval_parse(), the latter withqobject_from_json(). It returns the resulting parse tree wrapped in a suitable QAPI input visitor.Issue 1: since we get there only when
keyval_parse()succeeds, JSON is unreachable. Reproducer:$ qemu-system-x86_64 -netdev '{"id":"foo"}' upstream-qemu: -netdev {"id":"foo"}: Parameter 'id' is missingQuoted 6 lines
This is parsed with
qemu_opts_parse_noisily(), resulting in a QemuOpts with a single option 'type' with value '{"id":"foo"}'. The error message comes from the opts visitor.To fix this, make
netdev_is_modern()return true when optarg[0] == '{'. This matches howqobject_input_visitor_new_str()recognizes JSON.OK
Quoted 13 lines
Issue 2: when
keyval_parse()detects an error, we throw it away and fall back to QemuOpts. This is commonly what we want. But not always. For instance:$ qemu-system-x86_64 -netdev 'type=stream,id=foo,addr.type=inet,addr.host=localhost,addr.port=1234,addr.ipv4-off'Note the typo "ipv4-off" instead of ipv4=off. The error reporting is crap:
qemu-system-x86_64: -netdev type=stream,id=foo,addr.type=inet,addr.host=localhost,addr.port=1234,addr.ipv4-off: warning: short-form boolean option 'addr.ipv4-off' deprecated Please use addr.ipv4-off=on instead
qemu-system-x86_64: -netdev type=stream,id=foo,addr.type=inet,addr.host=localhost,addr.port=1234,addr.ipv4-off: Parameter 'type' is missingQuoted 8 lines
We get this because
netdev_is_modern()guesses wrongly:keyval_parse()fails with the perfectly reasonable error message "Expected '=' after parameter 'addr.ipv4-off'", butnetdev_is_modern()ignores the error, and fails. We fall back to QemuOpts, and confusion ensues.I'm not sure we can do much better with reasonable effort. If we decide to accept this behavior, it should be documented at least in the source code.
Quoted 7 lines
What about using modern syntax by default?
args = keyval_parse(optarg, "type", NULL, NULL); if (!args) { /* cannot detect the syntax, use new style syntax */ return true; }As is,
netdev_is_modern()has three cases:1.
keyval_parse()fails2.
keyval_parse()succeeds, but value of @type is not modern3.
keyval_parse()succeeds, and value of @type is modernIn case 3. we're sure, because even if
qemu_opts_parse_noisily()also succeeded, it would result in the same value of @type.In case 2, assuming traditional seems reasonable. The assumption can be wrong when the user intends modern, but fat-fingers the type=T part.
Quoted 7 lines
Quoted 5 lines
In case 1, we know nothing.
Guessing modern is wrong when the user intends traditional. This happens when a meant-to-be-traditional @optarg also parses as modern. Quite possible.
I don't see why
keyval_parse()fails in this case. Any example?Brain cramp on my part, I'm afraid %-} Let me start over.
Guessing modern is wrong when the user intends traditional. Two sub-cases then:
* @optarg parses fine as traditional. For instance,
$ qemu-system-x86_64 -netdev type=user,id=foo,ipv4parses with a warning:
option 'ipv4' deprecated
Please use ipv4=on insteadThis is how current master behaves.
Guessing modern makes this fail instead:
qemu-system-x86_64: -netdev type=user,id=foo,ipv4: Expected '=' after parameter 'ipv4'
Regression.
* @optarg fails to parse traditional, too. The error reporting is for modern even though the user intends traditional. Can be misleading. Example:
$ qemu-system-x86_64 -netdev type=user,id=_,ipv4Current master:
qemu-system-x86_64: -netdev type=user,id=_,ipv4: Parameter 'id' expects an identifier Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.
Guessing modern instead:
qemu-system-x86_64: -netdev type=user,id=_,ipv4: Expected '=' after parameter 'ipv4'This should be rare in practice, as traditional parsing detects very few errors.
Quoted 5 lines
Quoted 3 lines
Guessing traditional is wrong when the user intends modern. This happens when a meant-to-be-modern @optarg fails to parse as modern, i.e. whenever the user screws up modern syntax.
This one is the example you gave (ipv4-off)
Two sub-cases then:
* @optarg parses fine as traditional. The parse result is unlikely to make sense, though. For instance,
$ qemu-system-x86_64 -netdev type=stream,id=foo,serverparses with a warning:
qemu-system-x86_64: -netdev type=stream,id=foo,server: warning: short-form boolean option 'server' deprecated Please use server=on instead
But the result fails in the opts visitor:
qemu-system-x86_64: -netdev type=stream,id=foo,server: Parameter 'type' is missing
In this case, we're better off with guessing modern:
qemu-system-x86_64: -netdev type=stream,id=foo,server: Expected '=' after parameter
* @optarg fails to parse traditional, too. The error reporting is for traditional even though the user intends modern. Can be misleading.
This is my ipv4-off example.
Can't win. Parsers simply don't compose that way.
Quoted 9 lines
Quoted 1 line
Which guess is less bad? I'm not sure. Thoughts?
Perhaps we can simply fail if
keyval_parse()fails?something like:
args = keyval_parse(optarg, "type", NULL, &error_fatal); type = qdict_get_try_str(args, "type"); return !g_strcmp0(type, "stream") || !g_strcmp0(type, "dgram");This rejects working option arguments that don't also parse as modern, such as "-netdev type=user,id=foo,ipv4".
Guessing traditional seems to be the least bad solution so far.
Quoted 9 lines
Supporting both traditional and modern syntax in an option argument is a swamp. Can we bypass it somehow?
-object uses traditional QemuOpts parsing for key=value,..., and modern parsing for JSON. Sticking to traditional sidesteps compatibility issues. But you have to use JSON for things traditional can't express.
Thoughts?
I don't want to force user to switch to JSON to ease move from "-netdev socket" to "-netdev stream" and "-netdev dgram". But I need to be able to parse SocketAddress to specify unix and inet socket address.
As we want to keep the same behavior on error cases (it's what I understand from your analysis), perhaps we can rely on traditional mechanism to detect the type: qemu_opts_parse()?
bool netdev_is_modern(const char *optarg)
{
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] == '{') {
return true;
}
opts = qemu_opts_parse(&dummy_opts, optarg, true, &error_fatal);
type = qemu_opt_get(opts, "type");
is_modern = !g_strcmp0(type, "stream") || !g_strcmp0(type, "dgram");
qemu_opts_reset(&dummy_opts);
return is_modern;
}The errors are directly reported by qemu_opts_parse(..., &error_fatal), and are the ones expected in the traditional case.
But the error reported for the modern case are not correct anymore.
I really don't think there is a good solution to our problem.
We must accept an incorrect error report in one of these cases.
To not break existing error report seems to be the way to go (qemu_opt_parse()).
Thanks,
Laurent
Re: [RFC PATCH v3 04/11] qapi: net: add stream and dgram netdevs
Quoted 241 lines · Laurent Vivier
Quoted 240 lines · Markus Armbruster
Quoted 135 lines · Laurent Vivier
Quoted 134 lines · Markus Armbruster
Quoted 14 lines · Laurent Vivier
Quoted 13 lines · Markus Armbruster
Quoted 11 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)Signed-off-by Laurent Vivier <[email protected]>Reviewed-by Stefano Brivio <[email protected]>---
[...]
Quoted 93 lines
Quoted 4 lines
Quoted 4 lines
net/net.c +0
-0...
Quoted 33 lines
Quoted 9 lines
net/net.c +4
-1@@ -1612,7 +1617,19 @@ void net_init_clients(void)*/ static bool netdev_is_modern (const char *optarg) { -return false; +QDict *args; +const char *type; +bool is_modern; +net/net.c void net_init_clients(void) · line 1633
Quoted 10 lines
net/net.c +9
-0+ args =keyval_parse (optarg,"type" ,NULL ,NULL ); +if (!args) { +return false; + } + type =qdict_get_try_str (args,"type" ); + is_modern = !g_strcmp0 (type,"stream" ) || !g_strcmp0 (type,"dgram" ); +qobject_unref (args); + +return is_modern; }You could use
g_autoptrhere:g_autoptr(QDict) args = NULL; const char *type; bool is_modern; args = keyval_parse(optarg, "type", NULL, NULL); if (!args) { return false; } type = qdict_get_try_str(args, "type"); return !g_strcmp0(type, "stream") || !g_strcmp0(type, "dgram");Quoted 1 line
Matter of taste; you decide.
Looks good. We already had some series to convert existing code to
g_autoptr(), so it seems the way to do.Quoted 19 lines
Now recall how this function is used: it decides whether to parse the modern way (with
qobject_input_visitor_new_str()) or the traditional way (withqemu_opts_parse_noisily()).
qemu_opts_parse_noisily()parses into a QemuOpts, for later use with the opts visitor.
qobject_input_visitor_new_str()supports both dotted keys and JSON. The former is parsed withkeyval_parse(), the latter withqobject_from_json(). It returns the resulting parse tree wrapped in a suitable QAPI input visitor.Issue 1: since we get there only when
keyval_parse()succeeds, JSON is unreachable. Reproducer:$ qemu-system-x86_64 -netdev '{"id":"foo"}' upstream-qemu: -netdev {"id":"foo"}: Parameter 'id' is missingQuoted 6 lines
This is parsed with
qemu_opts_parse_noisily(), resulting in a QemuOpts with a single option 'type' with value '{"id":"foo"}'. The error message comes from the opts visitor.To fix this, make
netdev_is_modern()return true when optarg[0] == '{'. This matches howqobject_input_visitor_new_str()recognizes JSON.OK
Quoted 13 lines
Issue 2: when
keyval_parse()detects an error, we throw it away and fall back to QemuOpts. This is commonly what we want. But not always. For instance:$ qemu-system-x86_64 -netdev 'type=stream,id=foo,addr.type=inet,addr.host=localhost,addr.port=1234,addr.ipv4-off'Note the typo "ipv4-off" instead of ipv4=off. The error reporting is crap:
qemu-system-x86_64: -netdev type=stream,id=foo,addr.type=inet,addr.host=localhost,addr.port=1234,addr.ipv4-off: warning: short-form boolean option 'addr.ipv4-off' deprecated Please use addr.ipv4-off=on instead
qemu-system-x86_64: -netdev type=stream,id=foo,addr.type=inet,addr.host=localhost,addr.port=1234,addr.ipv4-off: Parameter 'type' is missingQuoted 8 lines
We get this because
netdev_is_modern()guesses wrongly:keyval_parse()fails with the perfectly reasonable error message "Expected '=' after parameter 'addr.ipv4-off'", butnetdev_is_modern()ignores the error, and fails. We fall back to QemuOpts, and confusion ensues.I'm not sure we can do much better with reasonable effort. If we decide to accept this behavior, it should be documented at least in the source code.
Quoted 7 lines
What about using modern syntax by default?
args = keyval_parse(optarg, "type", NULL, NULL); if (!args) { /* cannot detect the syntax, use new style syntax */ return true; }As is,
netdev_is_modern()has three cases:1.
keyval_parse()fails2.
keyval_parse()succeeds, but value of @type is not modern3.
keyval_parse()succeeds, and value of @type is modernIn case 3. we're sure, because even if
qemu_opts_parse_noisily()also succeeded, it would result in the same value of @type.In case 2, assuming traditional seems reasonable. The assumption can be wrong when the user intends modern, but fat-fingers the type=T part.
Quoted 7 lines
Quoted 5 lines
In case 1, we know nothing.
Guessing modern is wrong when the user intends traditional. This happens when a meant-to-be-traditional @optarg also parses as modern. Quite possible.
I don't see why
keyval_parse()fails in this case. Any example?Brain cramp on my part, I'm afraid %-} Let me start over.
Guessing modern is wrong when the user intends traditional. Two sub-cases then:
* @optarg parses fine as traditional. For instance,
$ qemu-system-x86_64 -netdev type=user,id=foo,ipv4parses with a warning:
option 'ipv4' deprecated
Please use ipv4=on insteadThis is how current master behaves.
Guessing modern makes this fail instead:
qemu-system-x86_64: -netdev type=user,id=foo,ipv4: Expected '=' after parameter 'ipv4'
Regression.
* @optarg fails to parse traditional, too. The error reporting is for modern even though the user intends traditional. Can be misleading. Example:
$ qemu-system-x86_64 -netdev type=user,id=_,ipv4Current master:
qemu-system-x86_64: -netdev type=user,id=_,ipv4: Parameter 'id' expects an identifier Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.
Guessing modern instead:
qemu-system-x86_64: -netdev type=user,id=_,ipv4: Expected '=' after parameter 'ipv4'This should be rare in practice, as traditional parsing detects very few errors.
Quoted 5 lines
Quoted 3 lines
Guessing traditional is wrong when the user intends modern. This happens when a meant-to-be-modern @optarg fails to parse as modern, i.e. whenever the user screws up modern syntax.
This one is the example you gave (ipv4-off)
Two sub-cases then:
* @optarg parses fine as traditional. The parse result is unlikely to make sense, though. For instance,
$ qemu-system-x86_64 -netdev type=stream,id=foo,serverparses with a warning:
qemu-system-x86_64: -netdev type=stream,id=foo,server: warning: short-form boolean option 'server' deprecated Please use server=on instead
But the result fails in the opts visitor:
qemu-system-x86_64: -netdev type=stream,id=foo,server: Parameter 'type' is missing
In this case, we're better off with guessing modern:
qemu-system-x86_64: -netdev type=stream,id=foo,server: Expected '=' after parameter
* @optarg fails to parse traditional, too. The error reporting is for traditional even though the user intends modern. Can be misleading.
This is my ipv4-off example.
Can't win. Parsers simply don't compose that way.
Quoted 9 lines
Quoted 1 line
Which guess is less bad? I'm not sure. Thoughts?
Perhaps we can simply fail if
keyval_parse()fails?something like:
args = keyval_parse(optarg, "type", NULL, &error_fatal); type = qdict_get_try_str(args, "type"); return !g_strcmp0(type, "stream") || !g_strcmp0(type, "dgram");This rejects working option arguments that don't also parse as modern, such as "-netdev type=user,id=foo,ipv4".
Guessing traditional seems to be the least bad solution so far.
Supporting both traditional and modern syntax in an option argument is a swamp. Can we bypass it somehow?
-object uses traditional QemuOpts parsing for key=value,..., and modern parsing for JSON. Sticking to traditional sidesteps compatibility issues. But you have to use JSON for things traditional can't express.
Quoted 6 lines
Quoted 2 lines
Thoughts?
I don't want to force user to switch to JSON to ease move from "-netdev socket" to "-netdev stream" and "-netdev dgram".
But I need to be able to parse SocketAddress to specify unix and inet socket address.
That's fair.
Quoted 2 lines
As we want to keep the same behavior on error cases (it's what I understand from your analysis),
We must not break working usage.
We may change how erroneous usage fails, but we should try to avoid bad error reporting. Common errors are more important here. Whether they are in existing or in new options doesn't matter.
Quoted 30 lines
perhaps we can rely on traditional mechanism to detect the type:
qemu_opts_parse()?bool
netdev_is_modern(const char *optarg){ 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] == '{') { return true; } opts = qemu_opts_parse(&dummy_opts, optarg, true, &error_fatal); type = qemu_opt_get(opts, "type"); is_modern = !g_strcmp0(type, "stream") || !g_strcmp0(type, "dgram"); qemu_opts_reset(&dummy_opts); return is_modern; }The errors are directly reported by
qemu_opts_parse(..., &error_fatal), and are the ones expected in the traditional case.
Quoted 7 lines
But the error reported for the modern case are not correct anymore.
I really don't think there is a good solution to our problem.
We must accept an incorrect error report in one of these cases.
To not break existing error report seems to be the way to go (
qemu_opt_parse()).
I'm not sure about passing &error_fatal to qemu_opts_parse(). Let's examine the possible errors.
qemu_opts_parse() calls opts_parse().
opts_parse() extracts the value of parameter "id" with opts_parse_id().
It then creates a QemuOpts with qemu_opts_create(). Errors:
* "Invalid parameter 'id'" is impossible since !dummy_opts.merge_lists.
* "Parameter "id" expects an identifier" when value of "id" is not wellformed.
* "Duplicate ID 'FOO' for netdev" is impossible since @dummy_opts is always empty.
It then parses for real with opts_do_parse(). Errors can only come from opt_validate():
* "Invalid parameter 'FOO'" is impossible since
opts_accepts_any(&dummy_opts)
* a bunch from qemu_opt_parse(), all impossible since !dummy_opt.desc.
Conclusion: can only error out when value of "id" is not wellformed.
Detecting this error is counter-productive here, because it masks the value of "type", which is what we're after.
Better, I think: use opts_do_parse() directly. Something like
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");Note this cannot fail. It may be unable to extract the value of type if the user messes up badly enough. We then assume traditional syntax. Least bad option so far, I think.
For once, QemuOpts having basically no syntax checks comes it handy. I'm surprised.
RFC PATCH v3 5/11 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
@@ -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.36.1
RFC PATCH v3 6/11 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
@@ -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.36.1
RFC PATCH v3 7/11 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
@@ -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.36.1
RFC PATCH v3 8/11 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
@@ -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.36.1
RFC PATCH v3 9/11 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
@@ -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.36.1
RFC PATCH v3 10/11 qemu-sockets: introduce socket_uri()
In reply to Laurent VivierFormat a string URI from a SocketAddress.
Original code from hmp-cmds.c:SocketAddress_to_str()
Replace 'tcp:' by 'inet:' (because 'inet' can be also 'udp'). Replace 'tcp:' by 'vsock:' with vsock socket type.
--- include/qemu/sockets.h | 2 +- monitor/hmp-cmds.c | 23 +---------------------- util/qemu-sockets.c | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+), 23 deletions(-)
include/qemu/sockets.h +1 -1
@@ -41,6 +41,7 @@ int unix_listen(const char *path, Error **errp);int unix_connect (const char *path,Error **errp);SocketAddress *socket_parse (const char *str,Error **errp); +char *socket_uri (SocketAddress *addr);int socket_connect (SocketAddress *addr,Error **errp);int socket_listen (SocketAddress *addr,int num,Error **errp);void socket_listen_cleanup (int fd,Error **errp);
@@ -123,5 +124,4 @@ SocketAddress *socket_address_flatten(SocketAddressLegacy *addr);* Return 0 on success. */ int socket_address_parse_named_fd (SocketAddress *addr,Error **errp); -#endif /* QEMU_SOCKETS_H */
monitor/hmp-cmds.c +1 -22
@@ -195,27 +195,6 @@ void hmp_info_mice(Monitor *mon, const QDict *qdict)qapi_free_MouseInfoList (mice_list); } -static char *SocketAddress_to_str (SocketAddress *addr) -{ -switch (addr->type) { -case SOCKET_ADDRESS_TYPE_INET : -return g_strdup_printf ("tcp:%s:%s" , - addr->u.inet.host, - addr->u.inet.port); -case SOCKET_ADDRESS_TYPE_UNIX : -return g_strdup_printf ("unix:%s" , - addr->u.q_unix.path); -case SOCKET_ADDRESS_TYPE_FD : -return g_strdup_printf ("fd:%s" , addr->u.fd.str); -case SOCKET_ADDRESS_TYPE_VSOCK : -return g_strdup_printf ("tcp:%s:%s" , - addr->u.vsock.cid, - addr->u.vsock.port); -default : -return g_strdup ("unknown address type" ); - } -} -void hmp_info_migrate (Monitor *mon,const QDict *qdict) {MigrationInfo *info;
@@ -373,7 +352,7 @@ void hmp_info_migrate(Monitor *mon, const QDict *qdict)monitor_printf (mon,"socket address: [\n" );for (addr = info->socket_address; addr; addr = addr->next) { -char *s =SocketAddress_to_str (addr->value); +char *s =socket_uri (addr->value);monitor_printf (mon,"\t%s\n" , s);g_free (s); }
util/qemu-sockets.c +20 -0
@@ -1098,6 +1098,26 @@ int unix_connect(const char *path, Error **errp)return sock; } +char *socket_uri (SocketAddress *addr) +{ +switch (addr->type) { +case SOCKET_ADDRESS_TYPE_INET : +return g_strdup_printf ("inet:%s:%s" , + addr->u.inet.host, + addr->u.inet.port); +case SOCKET_ADDRESS_TYPE_UNIX : +return g_strdup_printf ("unix:%s" , + addr->u.q_unix.path); +case SOCKET_ADDRESS_TYPE_FD : +return g_strdup_printf ("fd:%s" , addr->u.fd.str); +case SOCKET_ADDRESS_TYPE_VSOCK : +return g_strdup_printf ("vsock:%s:%s" , + addr->u.vsock.cid, + addr->u.vsock.port); +default : +return g_strdup ("unknown address type" ); + } +}SocketAddress *socket_parse (const char *str,Error **errp) {
Signature · 1 line
2.36.1
RFC PATCH v3 11/11 net: stream: move to QIO
In reply to Laurent VivierUse QIOChannel, QIOChannelSocket and QIONetListener.
--- net/stream.c | 475 ++++++++++++++++++--------------------------------- 1 file changed, 165 insertions(+), 310 deletions(-)
net/stream.c +165 -310
@@ -33,48 +33,36 @@#include "qemu/iov.h" #include "qemu/main-loop.h" #include "qemu/cutils.h" +#include "io/channel.h" +#include "io/channel-socket.h" +#include "io/net-listener.h" typedef struct NetStreamState {NetClientState nc; -int listen_fd; -int fd; +QIOChannel *listen_ioc; +QIONetListener *listener; +QIOChannel *ioc; +guint ioc_read_tag; +guint ioc_write_tag;SocketReadState rs;unsigned int send_index;/* number of bytes sent*/ -IOHandler *send_fn; -bool read_poll;/* waiting to receive data? */ -bool write_poll;/* waiting to transmit data? */ }NetStreamState ; -static void net_stream_accept (void *opaque); -static void net_stream_writable (void *opaque); +static void net_stream_listen (QIONetListener *listener, +QIOChannelSocket *cioc, +void *opaque); -static void net_stream_update_fd_handler (NetStreamState *s) +static gboolean net_stream_writable (QIOChannel *ioc, +GIOCondition condition, +gpointer data) { -qemu_set_fd_handler (s->fd, - s->read_poll ? s->send_fn :NULL , - s->write_poll ? net_stream_writable :NULL , - s); -} - -static void net_stream_read_poll (NetStreamState *s,bool enable) -{ - s->read_poll = enable; -net_stream_update_fd_handler (s); -} - -static void net_stream_write_poll (NetStreamState *s,bool enable) -{ - s->write_poll = enable; -net_stream_update_fd_handler (s); -} - -static void net_stream_writable (void *opaque) -{ -NetStreamState *s = opaque; +NetStreamState *s = data; -net_stream_write_poll (s, false); + s->ioc_write_tag =0 ;qemu_flush_queued_packets (&s->nc); + +return G_SOURCE_REMOVE ; } static ssize_t net_stream_receive(NetClientState *nc, const uint8_t *buf,
@@ -91,12 +79,15 @@ static ssize_t net_stream_receive(NetClientState *nc, const uint8_t *buf, .iov_len = size, }, }; +struct iovec local_iov[2 ]; +unsigned int nlocal_iov;size_t remaining;ssize_t ret; - remaining =iov_size (iov,2 ) - s->send_index; - ret =iov_send (s->fd, iov,2 , s->send_index, remaining); + remaining =iov_size (iov,2 ) - s->send_index; + nlocal_iov =iov_copy (local_iov,2 , iov,2 , s->send_index, remaining); + ret =qio_channel_writev (s->ioc, local_iov, nlocal_iov,NULL );if (ret ==-1 && errno ==EAGAIN ) { ret =0 ;/* handled further down */ }
@@ -106,19 +97,25 @@ static ssize_t net_stream_receive(NetClientState *nc, const uint8_t *buf, }if (ret < (ssize_t )remaining) { s->send_index += ret; -net_stream_write_poll (s, true); + s->ioc_write_tag =qio_channel_add_watch (s->ioc,G_IO_OUT , + net_stream_writable, s,NULL );return 0 ; } s->send_index =0 ;return size; } +static gboolean net_stream_send (QIOChannel *ioc, +GIOCondition condition, +gpointer data); +static void net_stream_send_completed (NetClientState *nc,ssize_t len) {NetStreamState *s =DO_UPCAST (NetStreamState, nc, nc); -if (!s->read_poll) { -net_stream_read_poll (s, true); +if (!s->ioc_read_tag) { + s->ioc_read_tag =qio_channel_add_watch (s->ioc,G_IO_IN , + net_stream_send, s,NULL ); } }
@@ -129,19 +126,24 @@ static void net_stream_rs_finalize(SocketReadState *rs)if (qemu_send_packet_async (&s->nc, rs->buf, rs->packet_len, net_stream_send_completed) ==0 ) { -net_stream_read_poll (s, false); +if (s->ioc_read_tag) { +g_source_remove (s->ioc_read_tag); + s->ioc_read_tag =0 ; + } } } -static void net_stream_send (void *opaque) +static gboolean net_stream_send (QIOChannel *ioc, +GIOCondition condition, +gpointer data) { -NetStreamState *s = opaque; +NetStreamState *s = data;int size;int ret; -uint8_t buf1[NET_BUFSIZE ]; -const uint8_t *buf; +char buf1[NET_BUFSIZE ]; +const char *buf; - size =recv (s->fd, buf1,sizeof (buf1),0 ); + size =qio_channel_read (s->ioc, buf1,sizeof (buf1),NULL );if (size <0 ) {if (errno !=EWOULDBLOCK ) { goto eoc;
@@ -149,52 +151,63 @@ static void net_stream_send(void *opaque) }else if (size ==0 ) {/* end of connection */ eoc: -net_stream_read_poll (s, false); -net_stream_write_poll (s, false); -if (s->listen_fd !=-1 ) { -qemu_set_fd_handler (s->listen_fd, net_stream_accept,NULL , s); + s->ioc_read_tag =0 ; +if (s->ioc_write_tag) { +g_source_remove (s->ioc_write_tag); + s->ioc_write_tag =0 ; + } +if (s->listener) { +qio_net_listener_set_client_func (s->listener, net_stream_listen, + s,NULL ); } -closesocket (s->fd); +object_unref (OBJECT (s->ioc)); + s->ioc =NULL ; - s->fd =-1 ;net_socket_rs_init (&s->rs, net_stream_rs_finalize, false); s->nc.link_down = true;memset (s->nc.info_str,0 ,sizeof (s->nc.info_str)); -return ; +return G_SOURCE_REMOVE ; } buf = buf1; - ret =net_fill_rstate (&s->rs, buf, size); + ret =net_fill_rstate (&s->rs, (const uint8_t *)buf, size);if (ret ==-1 ) { goto eoc; } + +return G_SOURCE_CONTINUE ; }static void net_stream_cleanup (NetClientState *nc) {NetStreamState *s =DO_UPCAST (NetStreamState, nc, nc); -if (s->fd !=-1 ) { -net_stream_read_poll (s, false); -net_stream_write_poll (s, false); -close (s->fd); - s->fd =-1 ; +if (s->ioc) { +if (QIO_CHANNEL_SOCKET (s->ioc)->fd !=-1 ) { +if (s->ioc_read_tag) { +g_source_remove (s->ioc_read_tag); + s->ioc_read_tag =0 ; + } +if (s->ioc_write_tag) { +g_source_remove (s->ioc_write_tag); + s->ioc_write_tag =0 ; + } + } +object_unref (OBJECT (s->ioc)); + s->ioc =NULL ; } -if (s->listen_fd !=-1 ) { -qemu_set_fd_handler (s->listen_fd,NULL ,NULL ,NULL ); -closesocket (s->listen_fd); - s->listen_fd =-1 ; +if (s->listen_ioc) { +if (s->listener) { +qio_net_listener_disconnect (s->listener); +object_unref (OBJECT (s->listener)); + s->listener =NULL ; + } +object_unref (OBJECT (s->listen_ioc)); + s->listen_ioc =NULL ; } } -static void net_stream_connect (void *opaque) -{ -NetStreamState *s = opaque; - s->send_fn = net_stream_send; -net_stream_read_poll (s, true); -} -static NetClientInfo net_stream_info = { .type =NET_CLIENT_DRIVER_STREAM , .size =sizeof (NetStreamState),
@@ -202,77 +215,64 @@ static NetClientInfo net_stream_info = { .cleanup = net_stream_cleanup, }; -static NetStreamState *net_stream_fd_init_stream (NetClientState *peer, -const char *model, -const char *name, -int fd,int is_connected) +static void net_stream_listen (QIONetListener *listener, +QIOChannelSocket *cioc, +void *opaque) { -NetClientState *nc; -NetStreamState *s; +NetStreamState *s = opaque; +SocketAddress *addr; +char *uri; - nc =qemu_new_net_client (&net_stream_info, peer, model, name); +object_ref (OBJECT (cioc)); -snprintf (nc->info_str,sizeof (nc->info_str),"fd=%d" , fd); +qio_net_listener_set_client_func (s->listener,NULL , s,NULL ); - s =DO_UPCAST (NetStreamState, nc, nc); + s->ioc =QIO_CHANNEL (cioc); +qio_channel_set_name (s->ioc,"stream-server" ); + s->nc.link_down = false; - s->fd = fd; - s->listen_fd =-1 ; -net_socket_rs_init (&s->rs, net_stream_rs_finalize, false); + s->ioc_read_tag =qio_channel_add_watch (s->ioc,G_IO_IN , net_stream_send, + s,NULL ); -/* Disable Nagle algorithm on TCP sockets to reduce latency */ -socket_set_nodelay (fd); + addr =qio_channel_socket_get_remote_address (cioc,NULL ); +g_assert (addr !=NULL ); + uri =socket_uri (addr); +pstrcpy (s->nc.info_str,sizeof (s->nc.info_str), uri); +g_free (uri); +qapi_free_SocketAddress (addr); -if (is_connected) { -net_stream_connect (s); - }else { -qemu_set_fd_handler (s->fd,NULL , net_stream_connect, s); - } -return s; } -static void net_stream_accept (void *opaque) +static void net_stream_server_listening (QIOTask *task,gpointer opaque) {NetStreamState *s = opaque; -struct sockaddr_storage saddr; -socklen_t len; -int fd; - -for (;;) { - len =sizeof (saddr); - fd =qemu_accept (s->listen_fd, (struct sockaddr *)&saddr, &len); -if (fd <0 && errno !=EINTR ) { -return ; - }else if (fd >=0 ) { -qemu_set_fd_handler (s->listen_fd,NULL ,NULL ,NULL ); -break ; - } - } - - s->fd = fd; - s->nc.link_down = false; -net_stream_connect (s); -switch (saddr.ss_family) { -case AF_INET : { -struct sockaddr_in *saddr_in = (struct sockaddr_in *)&saddr; +QIOChannelSocket *listen_sioc =QIO_CHANNEL_SOCKET (s->listen_ioc); +SocketAddress *addr; +int ret; -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 ; +if (listen_sioc->fd <0 ) { +snprintf (s->nc.info_str,sizeof (s->nc.info_str),"connection error" ); +return ; } -case AF_UNIX : { -struct sockaddr_un saddr_un; - len =sizeof (saddr_un); -getsockname (s->listen_fd, (struct sockaddr *)&saddr_un, &len); + addr =qio_channel_socket_get_local_address (listen_sioc,NULL ); +g_assert (addr !=NULL ); + ret =qemu_socket_try_set_nonblock (listen_sioc->fd); +if (addr->type ==SOCKET_ADDRESS_TYPE_FD && ret <0 ) {snprintf (s->nc.info_str,sizeof (s->nc.info_str), -"connect from %s" , saddr_un.sun_path); -break ; - } -default : -g_assert_not_reached (); +"can't use file descriptor %s (errno %d)" , + addr->u.fd.str, -ret); +return ; } +g_assert (ret ==0 ); +qapi_free_SocketAddress (addr); + + s->nc.link_down = true; + s->listener =qio_net_listener_new (); + +net_socket_rs_init (&s->rs, net_stream_rs_finalize, false); +qio_net_listener_set_client_func (s->listener, net_stream_listen, s,NULL ); +qio_net_listener_add (s->listener, listen_sioc); } static int net_stream_server_init(NetClientState *peer,
@@ -283,103 +283,55 @@ static int net_stream_server_init(NetClientState *peer, {NetClientState *nc;NetStreamState *s; -int fd, ret; +QIOChannelSocket *listen_sioc =qio_channel_socket_new (); -switch (addr->type) { -case SOCKET_ADDRESS_TYPE_INET : { -struct sockaddr_in saddr_in; + nc =qemu_new_net_client (&net_stream_info, peer, model, name); + s =DO_UPCAST (NetStreamState, nc, nc); -if (convert_host_port (&saddr_in, addr->u.inet.host, addr->u.inet.port, - errp) <0 ) { -return -1 ; - } + s->listen_ioc =QIO_CHANNEL (listen_sioc); +qio_channel_socket_listen_async (listen_sioc, addr,0 , + net_stream_server_listening, s, +NULL ,NULL ); - fd =qemu_socket (PF_INET ,SOCK_STREAM ,0 ); -if (fd <0 ) { -error_setg_errno (errp, errno,"can't create stream socket" ); -return -1 ; - } -qemu_socket_set_nonblock (fd); +return 0 ; +} -socket_set_fast_reuse (fd); +static void net_stream_client_connected (QIOTask *task,gpointer opaque) +{ +NetStreamState *s = opaque; +QIOChannelSocket *sioc =QIO_CHANNEL_SOCKET (s->ioc); +SocketAddress *addr; +gchar *uri; +int ret; - ret =bind (fd, (struct sockaddr *)&saddr_in,sizeof (saddr_in)); -if (ret <0 ) { -error_setg_errno (errp, errno,"can't bind ip=%s to socket" , -inet_ntoa (saddr_in.sin_addr)); -closesocket (fd); -return -1 ; - } -break ; +if (sioc->fd <0 ) { +snprintf (s->nc.info_str,sizeof (s->nc.info_str),"connection error" ); +return ; } -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)); - } - 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 ) { -return -1 ; - } - ret =qemu_socket_try_set_nonblock (fd); -if (ret <0 ) { -error_setg_errno (errp, -ret,"%s: Can't use file descriptor %d" , - name, fd); -return -1 ; - } -break ; -default : -g_assert_not_reached (); - } + addr =qio_channel_socket_get_remote_address (sioc,NULL ); +g_assert (addr !=NULL ); + uri =socket_uri (addr); +pstrcpy (s->nc.info_str,sizeof (s->nc.info_str), uri); +g_free (uri); - ret =listen (fd,0 ); -if (ret <0 ) { -error_setg_errno (errp, errno,"can't listen on socket" ); -closesocket (fd); -return -1 ; + ret =qemu_socket_try_set_nonblock (sioc->fd); +if (addr->type ==SOCKET_ADDRESS_TYPE_FD && ret <0 ) { +snprintf (s->nc.info_str,sizeof (s->nc.info_str), +"can't use file descriptor %s (errno %d)" , + addr->u.fd.str, -ret); +return ; } +g_assert (ret ==0 ); +qapi_free_SocketAddress (addr); - nc =qemu_new_net_client (&net_stream_info, peer, model, name); - s =DO_UPCAST (NetStreamState, nc, nc); - s->fd =-1 ; - s->listen_fd = fd; - s->nc.link_down = true;net_socket_rs_init (&s->rs, net_stream_rs_finalize, false); -qemu_set_fd_handler (s->listen_fd, net_stream_accept,NULL , s); -return 0 ; +/* Disable Nagle algorithm on TCP sockets to reduce latency */ +qio_channel_set_delay (s->ioc, false); + + s->ioc_read_tag =qio_channel_add_watch (s->ioc,G_IO_IN , net_stream_send, + s,NULL ); } static int net_stream_client_init(NetClientState *peer,
@@ -389,114 +341,17 @@ static int net_stream_client_init(NetClientState *peer, Error **errp) {NetStreamState *s; -int fd, connected, ret; -gchar *info_str; - -switch (addr->type) { -case SOCKET_ADDRESS_TYPE_INET : { -struct sockaddr_in saddr_in; - -if (convert_host_port (&saddr_in, addr->u.inet.host, addr->u.inet.port, - errp) <0 ) { -return -1 ; - } - - fd =qemu_socket (PF_INET ,SOCK_STREAM ,0 ); -if (fd <0 ) { -error_setg_errno (errp, errno,"can't create stream socket" ); -return -1 ; - } -qemu_socket_set_nonblock (fd); - - connected =0 ; -for (;;) { - ret =connect (fd, (struct sockaddr *)&saddr_in,sizeof (saddr_in)); -if (ret <0 ) { -if (errno ==EINTR || errno ==EWOULDBLOCK ) { -/* continue */ - }else if (errno ==EINPROGRESS || - errno ==EALREADY ) { -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:%d" , -inet_ntoa (saddr_in.sin_addr), -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)); - } +NetClientState *nc; +QIOChannelSocket *sioc =qio_channel_socket_new (); - 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 ) { -return -1 ; - } - ret =qemu_socket_try_set_nonblock (fd); -if (ret <0 ) { -error_setg_errno (errp, -ret,"%s: Can't use file descriptor %d" , - name, fd); -return -1 ; - } - connected =1 ; - info_str =g_strdup_printf ("connect to fd %d" , fd); -break ; -default : -error_setg (errp,"only support inet, unix or fd type" ); -return -1 ; - } + nc =qemu_new_net_client (&net_stream_info, peer, model, name); + s =DO_UPCAST (NetStreamState, nc, nc); - s =net_stream_fd_init_stream (peer, model, name, fd, connected); + s->ioc =QIO_CHANNEL (sioc); -pstrcpy (s->nc.info_str,sizeof (s->nc.info_str), info_str); -g_free (info_str); +qio_channel_socket_connect_async (sioc, addr, + net_stream_client_connected, s, +NULL ,NULL );return 0 ; }
Signature · 1 line
2.36.1
Quoted 8 lines · * 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:
Quoted 8 lines
"
socket.cadded 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).
Had you considered a -netdev chardev?
Dave
Quoted 83 lines
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
v3:
- remove support of "-net" for dgram and stream. They are only supported with "-netdev" option.
- use&error_fataldirectly innet_client_inits()
- update qemu-options.hx
- move to QIO for stream socketv2:
- use "stream" and "dgram" rather than "socket-ng,mode=stream" and ""socket-ng,mode=dgram"
- extract code to bypassqemu_opts_parse_noisily()to a new patch
- do not ignore EINVAL (Stefano)
- fix "-net" optionCC Ralph Schmieder <[email protected]>CC Stefano Brivio <[email protected]>CC Daniel P. Berrangé <[email protected]>CC Markus Armbruster <[email protected]>Laurent Vivier (10):
net: introduceconvert_host_port()
net: remove the @errp argument ofnet_client_inits()
qapi: net: introduce a way to bypassqemu_opts_parse_noisily()
qapi: net: add stream and dgram netdevs
net: stream: add unix socket
net: dgram: makedgram_dstgeneric
net: dgram: move mcast specific code fromnet_socket_fd_init_dgram()
net: dgram: add unix socket
qemu-sockets: introducesocket_uri()
net: stream: move to QIOStefano Brivio (1):
net: stream: Don't ignore EINVAL on netdev socket connectionhmp-commands.hx | 2 +- include/net/net.h | 3 +- include/qemu/sockets.h | 4 +- monitor/hmp-cmds.c | 23 +- net/clients.h | 6 + net/dgram.c | 706 +++++++++++++++++++++++++++++++++++++++++ net/hub.c | 2 + net/meson.build | 2 + net/net.c | 149 ++++++--- net/stream.c | 371 ++++++++++++++++++++++ qapi/net.json | 40 ++- qemu-options.hx | 12 + softmmu/vl.c | 5 +- util/qemu-sockets.c | 20 ++ 14 files changed, 1277 insertions(+), 68 deletions(-) create mode 100644 net/dgram.c create mode 100644 net/stream.cSignature · 1 line
2.36.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Quoted 12 lines · Dr. David Alan Gilbert
Quoted 11 lines · * 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.cadded support for unix domain socket datagram transport"
https://lore.kernel.org/qemu-devel/[email protected]/
Quoted 8 lines
Quoted 5 lines
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).
Had you considered a -netdev chardev?
I think by definition a "chardev" doesn't behave like a "netdev". Moreover "chardev" is already a frontend for several backends (socket, udp, ...), this would mean we use the frontend "chardev" as a backend of a "netdev". More and more layers...
And in the case of "-netdev dgram", we can use unix socket and sendto()/recv() while something like "-chardev udp,id=char0 -netdev chardev,chardev=char0,id=net0" doesn't support unix (see qio_channel_socket_dgram_sync()/socket_dgram()) and uses a "connect()/sendmsg()/recv()" (that really changes the behaviour of the backend)
The aim of this series is to add unix socket support.
Thanks,
Laurent
Quoted 19 lines · * Laurent Vivier ([email protected]) wrote:
Quoted 18 lines · Dr. David Alan Gilbert
Quoted 16 lines · * 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.cadded 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).
Quoted 7 lines
Quoted 2 lines
Had you considered a -netdev chardev?
I think by definition a "chardev" doesn't behave like a "netdev". Moreover "chardev" is already a frontend for several backends (socket, udp, ...), this would mean we use the frontend "chardev" as a backend of a "netdev". More and more layers...
Yeh definitely more layers; but perhaps avoiding some duplication.
Quoted 6 lines
And in the case of "-netdev dgram", we can use unix socket and sendto()/recv() while something like "-chardev udp,id=char0 -netdev chardev,chardev=char0,id=net0" doesn't support unix (see qio_channel_socket_dgram_sync()/socket_dgram()) and uses a "connect()/sendmsg()/recv()" (that really changes the behaviour of the backend)
It was -chardev socket, path=/unix/socket/path that I was thinking
of; -chardev socket supports both tcp and unix already.
Quoted 1 line
The aim of this series is to add unix socket support.
Yes.
Dave
Quoted 3 lines
Thanks,
Laurent
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Quoted 37 lines · Dr. David Alan Gilbert
Quoted 19 lines · * Laurent Vivier ([email protected]) wrote:
Quoted 18 lines · Dr. David Alan Gilbert
Quoted 16 lines · * 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.cadded 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).
Quoted 7 lines
Quoted 2 lines
Had you considered a -netdev chardev?
I think by definition a "chardev" doesn't behave like a "netdev". Moreover "chardev" is already a frontend for several backends (socket, udp, ...), this would mean we use the frontend "chardev" as a backend of a "netdev". More and more layers...
Yeh definitely more layers; but perhaps avoiding some duplication.
Quoted 6 lines
And in the case of "-netdev dgram", we can use unix socket and sendto()/recv() while something like "-chardev udp,id=char0 -netdev chardev,chardev=char0,id=net0" doesn't support unix (see qio_channel_socket_dgram_sync()/socket_dgram()) and uses a "connect()/sendmsg()/recv()" (that really changes the behaviour of the backend)
Quoted 2 lines
It was -chardev socket, path=/unix/socket/path that I was thinking
of; -chardev socket supports both tcp and unix already.
IMHO we've over-used & abused chardevs in contexts where we really should not have done. The chardev API is passable when all you need is a persistent bidirectional channel, but is a really bad fit for backends wanting to be aware of the dynamic connection oriented semantics that sockets offer. The hoops we've had to jump through in places to deal with having chardevs open asynchronously or deal with automatic chardev re-connection is quite gross.
Chardev in the past was convenient to use, because we were not so great at doing CLI syntax modelling & implementation, so it was useful to re-use the chardev code for socket address handling on the CLI. We also didn't historically have nice APIs for dealing with sockets - if you didn't use chardevs, you were stuck with the raw sockets APIs. With our aim for CLI to be modelled & implemented with QAPI these days, that benefit of re-using chardevs for CLI is largely eliminated. With our QIOChannel APIs, the benefits of re-using chardevs from an impl POV is also largely eliminated.
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 :|
Quoted 49 lines · * Daniel P. Berrangé ([email protected]) wrote:
Quoted 37 lines · Dr. David Alan Gilbert
Quoted 19 lines · * Laurent Vivier ([email protected]) wrote:
Quoted 18 lines · Dr. David Alan Gilbert
Quoted 16 lines · * 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.cadded 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).
Quoted 7 lines
Quoted 2 lines
Had you considered a -netdev chardev?
I think by definition a "chardev" doesn't behave like a "netdev". Moreover "chardev" is already a frontend for several backends (socket, udp, ...), this would mean we use the frontend "chardev" as a backend of a "netdev". More and more layers...
Yeh definitely more layers; but perhaps avoiding some duplication.
Quoted 6 lines
And in the case of "-netdev dgram", we can use unix socket and sendto()/recv() while something like "-chardev udp,id=char0 -netdev chardev,chardev=char0,id=net0" doesn't support unix (see qio_channel_socket_dgram_sync()/socket_dgram()) and uses a "connect()/sendmsg()/recv()" (that really changes the behaviour of the backend)
Quoted 2 lines
It was -chardev socket, path=/unix/socket/path that I was thinking
of; -chardev socket supports both tcp and unix already.IMHO we've over-used & abused chardevs in contexts where we really should not have done. The chardev API is passable when all you need is a persistent bidirectional channel, but is a really bad fit for backends wanting to be aware of the dynamic connection oriented semantics that sockets offer. The hoops we've had to jump through in places to deal with having chardevs open asynchronously or deal with automatic chardev re-connection is quite gross.
Quoted 10 lines
Chardev in the past was convenient to use, because we were not so great at doing CLI syntax modelling & implementation, so it was useful to re-use the chardev code for socket address handling on the CLI. We also didn't historically have nice APIs for dealing with sockets - if you didn't use chardevs, you were stuck with the raw sockets APIs. With our aim for CLI to be modelled & implemented with QAPI these days, that benefit of re-using chardevs for CLI is largely eliminated. With our QIOChannel APIs, the benefits of re-using chardevs from an impl POV is also largely eliminated.
OK, fair enough.
Dave
Quoted 8 lines
With regards,
DanielSignature · 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 :|
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK