qapi: net: add unix socket type support to netdev backend
Subsystems (2)
v13 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
v13:
- fix server default in qtest
- use SocketAddress in event
- remove unwanted meson update
- update error message in net_init_dgram()
v12:
- replace NETDEV_STREAM_EOC by NETDEV_STREAM_DISCONNECTED
- set server=off by default
v11:
- use qemu_set_info_str() in hw/net/xen_nic.c
- fix link_down state in stream client mode
- cleanup error case in net_stream_receive()
- update qapi version to 7.2
v10:
- add Red Hat copyright
- initialize dgram_dst to NULL in SOCKET_ADDRESS_TYPE_FD
- remove redundente _stream / _dgram in functions name
- move net_dgram_init() into net_init_dgram()
- address Thomas' comments on qtest
- add a function qemu_set_info_str() to set info string
- tested stream netdev with fd type using qrap/passt and "-netdev stream,addr.type=fd,server=off,addr.str=5,id=netdev0"
v9:
- add events to report stream connection/disconnection
- remove from net/dgram.c send_fn, listen_fd, net_dgram_accept() net_dgram_connect() and net_dgram_send() that are only needed by net/stream.c
- remove from net/stream.c send_fn
- add Red Hat copyright
- add original net/socket.c Stefano's patch (EINVAL)
v8:
- test ipv4 and ipv6 parameters (stream inet)
- test abstract parameter (stream unix)
- add SocketAddressInet supported parameters in qemu-options.hx (only stream, supported by the move to QIO)
- with qio_channel_writev() replace (ret == -1 && errno == EAGAIN)
by (ret == QIO_CHANNEL_ERR_BLOCK)
v7:
- add qtests
- update parameters table in net.json
- update socket_uri() and socket_parse()
v6:
- s/netdev option/-netdev option/ PATCH 4
- s/ / /
- update @NetdevStreamOptions and @NetdevDgramOptions comments
- update PATCH 4 description message
- add missing return in error case for unix stream socket
- split socket_uri() patch: move and rename, then change content
v5:
- remove RFC prefix
- put the change of net_client_parse() into its own patch (exit() in the function)
- update comments regarding netdev_is_modern() and netdev_parse_modern()
- update error case in net_stream_server_init()
- update qemu-options.hx with unix type
- fix HMP "info network" with unix protocol/server side.
v4:
- net_client_parse() fails with exit() rather than with return.
- keep "{ 'name': 'vmnet-host', 'if': 'CONFIG_VMNET' }" on its own line in qapi/net.json
- add a comment in qapi/net.json about parameters usage
- move netdev_is_modern() check to qemu_init()
- in netdev_is_modern(), check for JSON and use qemu_opts_do_parse() to parse parameters and detect type value.
- add a blank line after copyright comment
v3:
- remove support of "-net" for dgram and stream. They are only supported with "-netdev" option.
- use &error_fatal directly in net_client_inits()
- update qemu-options.hx
- move to QIO for stream socket
v2:
- use "stream" and "dgram" rather than "socket-ng,mode=stream" and ""socket-ng,mode=dgram"
- extract code to bypass qemu_opts_parse_noisily() to a new patch
- do not ignore EINVAL (Stefano)
- fix "-net" option
Laurent Vivier (15):
net: introduce convert_host_port()
net: remove the @errp argument of net_client_inits()
net: simplify net_client_parse() error management
qapi: net: introduce a way to bypass qemu_opts_parse_noisily()
net: introduce qemu_set_info_str() function
qapi: net: add stream and dgram netdevs
net: stream: add unix socket
net: dgram: make dgram_dst generic
net: dgram: move mcast specific code from net_socket_fd_init_dgram()
net: dgram: add unix socket
qemu-sockets: move and rename SocketAddress_to_str()
qemu-sockets: update socket_uri() and socket_parse() to be consistent
net: stream: move to QIO to enable additional parameters tests/qtest: netdev: test stream and dgram backends
net: stream: add QAPI events to report connection state
Stefano Brivio (2):
net: socket: Don't ignore EINVAL on netdev socket connection
net: stream: Don't ignore EINVAL on netdev socket connection
hmp-commands.hx | 2 +- hw/net/xen_nic.c | 5 +- include/net/net.h | 7 +- include/qemu/sockets.h | 4 +- monitor/hmp-cmds.c | 23 +- net/clients.h | 6 + net/dgram.c | 623 ++++++++++++++++++++++++++++++++++++ net/hub.c | 2 + net/l2tpv3.c | 3 +- net/meson.build | 2 + net/net.c | 186 ++++++++--- net/slirp.c | 5 +- net/socket.c | 36 +-- net/stream.c | 387 ++++++++++++++++++++++ net/tap-win32.c | 3 +- net/tap.c | 13 +- net/vde.c | 3 +- net/vhost-user.c | 3 +- net/vhost-vdpa.c | 2 +- qapi/net.json | 115 ++++++- qemu-options.hx | 14 + softmmu/vl.c | 16 +- tests/qtest/meson.build | 1 + tests/qtest/netdev-socket.c | 420 ++++++++++++++++++++++++ util/qemu-sockets.c | 25 ++ 25 files changed, 1775 insertions(+), 131 deletions(-) create mode 100644 net/dgram.c create mode 100644 net/stream.c create mode 100644 tests/qtest/netdev-socket.c
Signature · 1 line
2.37.3
PATCH v13 4/17 qapi: net: introduce a way to bypass qemu_opts_parse_noisily()
As qemu_opts_parse_noisily() flattens the QAPI structures ("type" field of Netdev structure can collides with "type" field of SocketAddress), we introduce a way to bypass qemu_opts_parse_noisily() and use directly visit_type_Netdev() to parse the backend parameters.
More details from Markus:
qemu_init() passes the argument of -netdev, -nic, and -net to net_client_parse().
net_client_parse() parses with qemu_opts_parse_noisily(), passing QemuOptsList qemu_netdev_opts for -netdev, qemu_nic_opts for -nic, and qemu_net_opts for -net. Their desc[] are all empty, which means any keys are accepted. The result of the parse (a QemuOpts) is stored in the QemuOptsList.
Note that QemuOpts is flat by design. In some places, we layer non-flat on top using dotted keys convention, but not here.
net_init_clients() iterates over the stored QemuOpts, and passes them to net_init_netdev(), net_param_nic(), or net_init_client(), respectively.
These functions pass the QemuOpts to net_client_init(). They also do other things with the QemuOpts, which we can ignore here.
net_client_init() uses the opts visitor to convert the (flat) QemOpts to a (non-flat) QAPI object Netdev. Netdev is also the argument of QMP command netdev_add.
The opts visitor was an early attempt to support QAPI in (QemuOpts-based) CLI. It restricts QAPI types to a certain shape; see commit eb7ee2cbeb "qapi: introduce OptsVisitor".
A more modern way to support QAPI is qobject_input_visitor_new_str(). It uses keyval_parse() instead of QemuOpts for KEY=VALUE,... syntax, and it also supports JSON syntax. The former isn't quite as expressive as JSON, but it's a lot closer than QemuOpts + opts visitor.
This commit paves the way to use of the modern way instead.
--- include/net/net.h | 2 ++ net/net.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++ softmmu/vl.c | 6 ++++- 3 files changed, 64 insertions(+), 1 deletion(-)
include/net/net.h +2 -0
@@ -220,6 +220,8 @@ extern NICInfo nd_table[MAX_NICS];extern const char *host_net_devices[];/* from net.c */ +bool netdev_is_modern (const char *optarg); +void netdev_parse_modern (const char *optarg);void net_client_parse (QemuOptsList *opts_list,const char *str);void show_netdevs (void );void net_init_clients (void );
net/net.c +57 -0
87 lines not on this page · raw message
softmmu/vl.c +5 -1
@@ -2801,7 +2801,11 @@ void qemu_init(int argc, char **argv)break ;case QEMU_OPTION_netdev: default_net =0 ; -net_client_parse (qemu_find_opts ("netdev" ), optarg); +if (netdev_is_modern (optarg)) { +netdev_parse_modern (optarg); + }else { +net_client_parse (qemu_find_opts ("netdev" ), optarg); + }break ;case QEMU_OPTION_nic: default_net =0 ;
Signature · 1 line
2.37.3
PATCH v13 5/17 net: introduce qemu_set_info_str() function
In reply to Laurent VivierEmbed the setting of info_str in a function.
--- hw/net/xen_nic.c | 5 ++--- include/net/net.h | 1 + net/l2tpv3.c | 3 +-- net/net.c | 17 ++++++++++++----- net/slirp.c | 5 ++--- net/socket.c | 33 ++++++++++++++------------------- net/tap-win32.c | 3 +-- net/tap.c | 13 +++++-------- net/vde.c | 3 +-- net/vhost-user.c | 3 +-- net/vhost-vdpa.c | 2 +- 11 files changed, 41 insertions(+), 47 deletions(-)
hw/net/xen_nic.c +2 -3
@@ -296,9 +296,8 @@ static int net_init(struct XenLegacyDevice *xendev) netdev->nic =qemu_new_nic (&net_xen_info, &netdev->conf,"xen" ,NULL , netdev); -snprintf (qemu_get_queue (netdev->nic)->info_str, -sizeof (qemu_get_queue (netdev->nic)->info_str), -"nic: xenbus vif macaddr=%s" , netdev->mac); +qemu_set_info_str (qemu_get_queue (netdev->nic), +"nic: xenbus vif macaddr=%s" , netdev->mac);/* fill info */ xenstore_write_be_int (&netdev->xendev,"feature-rx-copy" ,1 );
include/net/net.h +1 -0
@@ -177,6 +177,7 @@ ssize_t qemu_send_packet_async(NetClientState *nc, const uint8_t *buf,void qemu_purge_queued_packets (NetClientState *nc);void qemu_flush_queued_packets (NetClientState *nc);void qemu_flush_or_purge_queued_packets (NetClientState *nc,bool purge); +void qemu_set_info_str (NetClientState *nc,const char *fmt, ...);void qemu_format_nic_info_str (NetClientState *nc,uint8_t macaddr[6 ]);bool qemu_has_ufo (NetClientState *nc);bool qemu_has_vnet_hdr (NetClientState *nc);
net/l2tpv3.c +1 -2
@@ -723,8 +723,7 @@ int net_init_l2tpv3(const Netdev *netdev,l2tpv3_read_poll (s, true); -snprintf (s->nc.info_str,sizeof (s->nc.info_str), -"l2tpv3: connected" ); +qemu_set_info_str (&s->nc,"l2tpv3: connected" );return 0 ; outerr:qemu_del_net_client (nc);
net/net.c +12 -5
@@ -141,13 +141,20 @@ char *qemu_mac_strdup_printf(const uint8_t *macaddr) macaddr[3], macaddr[4], macaddr[5]); } +void qemu_set_info_str (NetClientState *nc,const char *fmt, ...) +{ +va_list ap; + +va_start (ap, fmt); +vsnprintf (nc->info_str,sizeof (nc->info_str), fmt, ap); +va_end (ap); +} +void qemu_format_nic_info_str (NetClientState *nc,uint8_t macaddr[6 ]) { -snprintf (nc->info_str,sizeof (nc->info_str), -"model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x" , - nc->model, - macaddr[0 ], macaddr[1 ], macaddr[2 ], - macaddr[3 ], macaddr[4 ], macaddr[5 ]); +qemu_set_info_str (nc,"model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x" , + nc->model, macaddr[0 ], macaddr[1 ], macaddr[2 ], + macaddr[3 ], macaddr[4 ], macaddr[5 ]); }static int mac_table[256 ] = {0 };
net/slirp.c +2 -3
@@ -611,9 +611,8 @@ static int net_slirp_init(NetClientState *peer, const char *model, nc =qemu_new_net_client (&net_slirp_info, peer, model, name); -snprintf (nc->info_str,sizeof (nc->info_str), -"net=%s,restrict=%s" ,inet_ntoa (net), - restricted ?"on" :"off" ); +qemu_set_info_str (nc,"net=%s,restrict=%s" ,inet_ntoa (net), + restricted ?"on" :"off" ); s =DO_UPCAST (SlirpState, nc, nc);
net/socket.c +14 -19
@@ -179,7 +179,7 @@ static void net_socket_send(void *opaque) s->fd =-1 ;net_socket_rs_init (&s->rs, net_socket_rs_finalize, false); s->nc.link_down = true; -memset (s->nc.info_str,0 ,sizeof (s->nc.info_str)); +qemu_set_info_str (&s->nc,"" );return ; }
@@ -387,16 +387,15 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,/* mcast: save bound address as dst */ if (is_connected && mcast !=NULL ) { s->dgram_dst = saddr; -snprintf (nc->info_str,sizeof (nc->info_str), -"socket: fd=%d (cloned mcast=%s:%d)" , - fd,inet_ntoa (saddr.sin_addr),ntohs (saddr.sin_port)); +qemu_set_info_str (nc,"socket: 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), -"socket: fd=%d %s" , fd,SocketAddressType_str (sa_type)); +qemu_set_info_str (nc,"socket: fd=%d %s" , fd, +SocketAddressType_str (sa_type)); }return s;
@@ -430,7 +429,7 @@ static NetSocketState *net_socket_fd_init_stream(NetClientState *peer, nc =qemu_new_net_client (&net_socket_info, peer, model, name); -snprintf (nc->info_str,sizeof (nc->info_str),"socket: fd=%d" , fd); +qemu_set_info_str (nc,"socket: fd=%d" , fd); s =DO_UPCAST (NetSocketState, nc, nc);
@@ -497,9 +496,8 @@ static void net_socket_accept(void *opaque) s->fd = fd; s->nc.link_down = false;net_socket_connect (s); -snprintf (s->nc.info_str,sizeof (s->nc.info_str), -"socket: connection from %s:%d" , -inet_ntoa (saddr.sin_addr),ntohs (saddr.sin_port)); +qemu_set_info_str (&s->nc,"socket: connection from %s:%d" , +inet_ntoa (saddr.sin_addr),ntohs (saddr.sin_port)); } static int net_socket_listen_init(NetClientState *peer,
@@ -597,9 +595,8 @@ static int net_socket_connect_init(NetClientState *peer,return -1 ; } -snprintf (s->nc.info_str,sizeof (s->nc.info_str), -"socket: connect to %s:%d" , -inet_ntoa (saddr.sin_addr),ntohs (saddr.sin_port)); +qemu_set_info_str (&s->nc,"socket: connect to %s:%d" , +inet_ntoa (saddr.sin_addr),ntohs (saddr.sin_port));return 0 ; }
@@ -642,9 +639,8 @@ static int net_socket_mcast_init(NetClientState *peer, s->dgram_dst = saddr; -snprintf (s->nc.info_str,sizeof (s->nc.info_str), -"socket: mcast=%s:%d" , -inet_ntoa (saddr.sin_addr),ntohs (saddr.sin_port)); +qemu_set_info_str (&s->nc,"socket: mcast=%s:%d" , +inet_ntoa (saddr.sin_addr),ntohs (saddr.sin_port));return 0 ; }
@@ -697,9 +693,8 @@ static int net_socket_udp_init(NetClientState *peer, s->dgram_dst = raddr; -snprintf (s->nc.info_str,sizeof (s->nc.info_str), -"socket: udp=%s:%d" , -inet_ntoa (raddr.sin_addr),ntohs (raddr.sin_port)); +qemu_set_info_str (&s->nc,"socket: udp=%s:%d" ,inet_ntoa (raddr.sin_addr), +ntohs (raddr.sin_port));return 0 ; }
net/tap-win32.c +1 -2
@@ -789,8 +789,7 @@ static int tap_win32_init(NetClientState *peer, const char *model, s =DO_UPCAST (TAPState, nc, nc); -snprintf (s->nc.info_str,sizeof (s->nc.info_str), -"tap: ifname=%s" , ifname); +qemu_set_info_str (&s->nc,"tap: ifname=%s" , ifname); s->handle = handle;
net/tap.c +5 -8
@@ -630,8 +630,7 @@ int net_init_bridge(const Netdev *netdev, const char *name, } s =net_tap_fd_init (peer,"bridge" , name, fd, vnet_hdr); -snprintf (s->nc.info_str,sizeof (s->nc.info_str),"helper=%s,br=%s" , helper, - br); +qemu_set_info_str (&s->nc,"helper=%s,br=%s" , helper, br);return 0 ; }
@@ -690,14 +689,12 @@ static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer, }if (tap->has_fd || tap->has_fds) { -snprintf (s->nc.info_str,sizeof (s->nc.info_str),"fd=%d" , fd); +qemu_set_info_str (&s->nc,"fd=%d" , fd); }else if (tap->has_helper) { -snprintf (s->nc.info_str,sizeof (s->nc.info_str),"helper=%s" , - tap->helper); +qemu_set_info_str (&s->nc,"helper=%s" , tap->helper); }else { -snprintf (s->nc.info_str,sizeof (s->nc.info_str), -"ifname=%s,script=%s,downscript=%s" , ifname, script, - downscript); +qemu_set_info_str (&s->nc,"ifname=%s,script=%s,downscript=%s" , ifname, + script, downscript);if (strcmp (downscript,"no" ) !=0 ) {snprintf (s->down_script,sizeof (s->down_script),"%s" , downscript);
net/vde.c +1 -2
@@ -98,8 +98,7 @@ static int net_vde_init(NetClientState *peer, const char *model, nc =qemu_new_net_client (&net_vde_info, peer, model, name); -snprintf (nc->info_str,sizeof (nc->info_str),"sock=%s,fd=%d" , - sock,vde_datafd (vde)); +qemu_set_info_str (nc,"sock=%s,fd=%d" , sock,vde_datafd (vde)); s =DO_UPCAST (VDEState, nc, nc);
net/vhost-user.c +1 -2
@@ -341,8 +341,7 @@ static int net_vhost_user_init(NetClientState *peer, const char *device, user = g_new0(struct VhostUserState, 1);for (i =0 ; i < queues; i++) { nc =qemu_new_net_client (&net_vhost_user_info, peer, device, name); -snprintf (nc->info_str,sizeof (nc->info_str),"vhost-user%d to %s" , - i, chr->label); +qemu_set_info_str (nc,"vhost-user%d to %s" , i, chr->label); nc->queue_index = i;if (!nc0) { nc0 = nc;
net/vhost-vdpa.c +1 -1
@@ -593,7 +593,7 @@ static NetClientState *net_vhost_vdpa_init(NetClientState *peer, nc =qemu_new_net_control_client (&net_vhost_vdpa_cvq_info, peer, device, name); } -snprintf (nc->info_str,sizeof (nc->info_str),TYPE_VHOST_VDPA ); +qemu_set_info_str (nc,TYPE_VHOST_VDPA ); s =DO_UPCAST (VhostVDPAState, nc, nc); s->vhost_vdpa.device_fd = vdpa_device_fd;
Signature · 1 line
2.37.3
PATCH v13 7/17 net: socket: 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_socket_connect_init(), but if connect() returns EINVAL, we'll fail silently. Remove the related exception.
--- net/socket.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
net/socket.c +1 -2
@@ -577,8 +577,7 @@ static int net_socket_connect_init(NetClientState *peer,if (errno ==EINTR || errno ==EWOULDBLOCK ) {/* continue */ }else if (errno ==EINPROGRESS || - errno ==EALREADY || - errno ==EINVAL ) { + errno ==EALREADY ) {break ; }else {error_setg_errno (errp, errno,"can't connect socket" );
Signature · 1 line
2.37.3
PATCH v13 9/17 net: stream: add unix socket
In reply to Laurent Vivier--- net/stream.c | 107 +++++++++++++++++++++++++++++++++++++++++++++--- qapi/net.json | 2 +- qemu-options.hx | 1 + 3 files changed, 104 insertions(+), 6 deletions(-)
net/stream.c +102 -5
@@ -235,7 +235,7 @@ static NetStreamState *net_stream_fd_init(NetClientState *peer,static void net_stream_accept (void *opaque) {NetStreamState *s = opaque; -struct sockaddr_in saddr; +struct sockaddr_storage saddr;socklen_t len;int fd;
@@ -253,8 +253,26 @@ static void net_stream_accept(void *opaque) s->fd = fd; s->nc.link_down = false;net_stream_connect (s); -qemu_set_info_str (&s->nc,"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; + +qemu_set_info_str (&s->nc,"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); +qemu_set_info_str (&s->nc,"connect from %s" , saddr_un.sun_path); +break ; + } +default : +g_assert_not_reached (); + } } static int net_stream_server_init(NetClientState *peer,
@@ -294,6 +312,43 @@ static int net_stream_server_init(NetClientState *peer, }break ; } +case SOCKET_ADDRESS_TYPE_UNIX : { +struct sockaddr_un saddr_un; + + ret =unlink (addr->u.q_unix.path); +if (ret <0 && errno !=ENOENT ) { +error_setg_errno (errp, errno,"failed to unlink socket %s" , + addr->u.q_unix.path); +return -1 ; + } + + saddr_un.sun_family =PF_UNIX ; + ret =snprintf (saddr_un.sun_path,sizeof (saddr_un.sun_path),"%s" , + addr->u.q_unix.path); +if (ret <0 || ret >=sizeof (saddr_un.sun_path)) { +error_setg (errp,"UNIX socket path '%s' is too long" , + addr->u.q_unix.path); +error_append_hint (errp,"Path must be less than %zu bytes\n" , +sizeof (saddr_un.sun_path)); +return -1 ; + } + + fd =qemu_socket (PF_UNIX ,SOCK_STREAM ,0 ); +if (fd <0 ) { +error_setg_errno (errp, errno,"can't create stream socket" ); +return -1 ; + } +qemu_socket_set_nonblock (fd); + + ret =bind (fd, (struct sockaddr *)&saddr_un,sizeof (saddr_un)); +if (ret <0 ) { +error_setg_errno (errp, errno,"can't create socket with path: %s" , + saddr_un.sun_path); +closesocket (fd); +return -1 ; + } +break ; + }case SOCKET_ADDRESS_TYPE_FD : fd =monitor_fd_param (monitor_cur (), addr->u.fd.str, errp);if (fd ==-1 ) {
@@ -337,6 +392,7 @@ static int net_stream_client_init(NetClientState *peer, {NetStreamState *s;struct sockaddr_in saddr_in; +struct sockaddr_un saddr_un;int fd, connected, ret;switch (addr->type) {
@@ -373,6 +429,45 @@ static int net_stream_client_init(NetClientState *peer, } }break ; +case SOCKET_ADDRESS_TYPE_UNIX : + saddr_un.sun_family =PF_UNIX ; + ret =snprintf (saddr_un.sun_path,sizeof (saddr_un.sun_path),"%s" , + addr->u.q_unix.path); +if (ret <0 || ret >=sizeof (saddr_un.sun_path)) { +error_setg (errp,"UNIX socket path '%s' is too long" , + addr->u.q_unix.path); +error_append_hint (errp,"Path must be less than %zu bytes\n" , +sizeof (saddr_un.sun_path)); +return -1 ; + } + + fd =qemu_socket (PF_UNIX ,SOCK_STREAM ,0 ); +if (fd <0 ) { +error_setg_errno (errp, errno,"can't create stream socket" ); +return -1 ; + } +qemu_socket_set_nonblock (fd); + + connected =0 ; +for (;;) { + ret =connect (fd, (struct sockaddr *)&saddr_un,sizeof (saddr_un)); +if (ret <0 ) { +if (errno ==EINTR || errno ==EWOULDBLOCK ) { +/* continue */ + }else if (errno ==EAGAIN || + errno ==EALREADY ) { +break ; + }else { +error_setg_errno (errp, errno,"can't connect socket" ); +closesocket (fd); +return -1 ; + } + }else { + connected =1 ; +break ; + } + } +break ;case SOCKET_ADDRESS_TYPE_FD : fd =monitor_fd_param (monitor_cur (), addr->u.fd.str, errp);if (fd ==-1 ) {
@@ -387,7 +482,7 @@ static int net_stream_client_init(NetClientState *peer, connected =1 ;break ;default : -error_setg (errp,"only support inet or fd type" ); +error_setg (errp,"only support inet, unix or fd type" );return -1 ; }
@@ -399,13 +494,15 @@ static int net_stream_client_init(NetClientState *peer,inet_ntoa (saddr_in.sin_addr), ntohs(saddr_in.sin_port));break ; +case SOCKET_ADDRESS_TYPE_UNIX : +qemu_set_info_str (&s->nc," connect to %s" , saddr_un.sun_path); +break ;case SOCKET_ADDRESS_TYPE_FD :qemu_set_info_str (&s->nc,"connect to fd %d" , fd);break ;default :g_assert_not_reached (); } -return 0 ; }
qapi/net.json +1 -1
@@ -583,7 +583,7 @@ # or connect to (server=false) # @server: create server socket (default: false) # -# Only SocketAddress types 'inet' and 'fd' are supported. +# Only SocketAddress types 'unix', 'inet' and 'fd' are supported. # # Since: 7.2 ##
qemu-options.hx +1 -0
@@ -2773,6 +2773,7 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev, " configure a network backend to connect to another network\n" " using an UDP tunnel\n" "-netdev stream,id=str[,server=on|off],addr.type=inet,addr.host=host,addr.port=port\n" + "-netdev stream,id=str[,server=on|off],addr.type=unix,addr.path=path\n" "-netdev stream,id=str[,server=on|off],addr.type=fd,addr.str=file-descriptor\n" " configure a network backend to connect to another network\n" " using a socket connection in stream mode.\n"
Signature · 1 line
2.37.3
PATCH v13 10/17 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.
Rename it dest_addr, and store socket length in dest_len.
--- net/dgram.c | 82 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 29 deletions(-)
net/dgram.c +53 -29
@@ -40,9 +40,11 @@ typedef struct NetDgramState {NetClientState nc;int fd;SocketReadState rs; -struct sockaddr_in dgram_dst;/* contains destination iff connectionless */ bool read_poll;/* waiting to receive data? */ bool write_poll;/* waiting to transmit data? */ +/* contains destination iff connectionless */ +struct sockaddr *dest_addr; +socklen_t dest_len; } NetDgramState;static void net_dgram_send (void *opaque);
@@ -84,10 +86,8 @@ static ssize_t net_dgram_receive(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->dest_addr) { + ret = sendto(s->fd, buf, size, 0, s->dest_addr, s->dest_len); } else { ret = send(s->fd, buf, size, 0); }
@@ -244,6 +244,9 @@ static void net_dgram_cleanup(NetClientState *nc)close (s->fd); s->fd =-1 ; } +g_free (s->dest_addr); + s->dest_addr =NULL ; + s->dest_len =0 ; }static NetClientInfo net_dgram_socket_info = {
@@ -260,7 +263,7 @@ static NetDgramState *net_dgram_fd_init(NetClientState *peer, SocketAddress *mcast, Error **errp) { -struct sockaddr_in saddr; +struct sockaddr_in *saddr =NULL ;int newfd;NetClientState *nc;NetDgramState *s;
@@ -275,31 +278,32 @@ static NetDgramState *net_dgram_fd_init(NetClientState *peer,qapi_free_SocketAddress (sa);/* -* fd passed: multicast: "learn" dgram_dst address from bound address and +* fd passed: multicast: "learn" dest_addr 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 ) { + 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);
@@ -311,21 +315,20 @@ static NetDgramState *net_dgram_fd_init(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->dest_addr ==NULL ); + s->dest_addr = (struct sockaddr *)saddr; + s->dest_len =sizeof (*saddr);qemu_set_info_str (nc,"fd=%d (cloned mcast=%s:%d)" , fd, -inet_ntoa (saddr.sin_addr),ntohs (saddr.sin_port)); +inet_ntoa (saddr->sin_addr),ntohs (saddr->sin_port)); }else { -if (sa_type ==SOCKET_ADDRESS_TYPE_UNIX ) { - s->dgram_dst.sin_family =AF_UNIX ; - } -qemu_set_info_str (nc,"fd=%d %s" , fd,SocketAddressType_str (sa_type)); }return s; err: +g_free (saddr);closesocket (fd);return NULL ; }
@@ -339,21 +342,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 {
@@ -362,13 +368,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 ;
@@ -376,16 +384,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 ; }
@@ -395,13 +406,17 @@ 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->dest_addr ==NULL ); + s->dest_addr = (struct sockaddr *)saddr; + s->dest_len =sizeof (*saddr); + +qemu_set_info_str (&s->nc,"mcast=%s:%d" ,inet_ntoa (saddr->sin_addr), +ntohs (saddr->sin_port)); -qemu_set_info_str (&s->nc,"mcast=%s:%d" ,inet_ntoa (saddr.sin_addr), -ntohs (saddr.sin_port));return 0 ; }
@@ -412,9 +427,10 @@ int net_init_dgram(const Netdev *netdev, const char *name, {NetDgramState *s;int fd, ret; -struct sockaddr_in raddr_in; -struct sockaddr_in laddr_in;SocketAddress *remote, *local; +struct sockaddr *dest_addr; +struct sockaddr_in laddr_in, raddr_in; +socklen_t dest_len;assert (netdev->type ==NET_CLIENT_DRIVER_DGRAM );
@@ -491,6 +507,10 @@ int net_init_dgram(const Netdev *netdev, const char *name,return -1 ; }qemu_socket_set_nonblock (fd); + + dest_len =sizeof (raddr_in); + dest_addr =g_malloc (dest_len); +memcpy (dest_addr, &raddr_in, dest_len);break ;case SOCKET_ADDRESS_TYPE_FD : fd =monitor_fd_param (monitor_cur (), local->u.fd.str, errp);
@@ -503,6 +523,8 @@ int net_init_dgram(const Netdev *netdev, const char *name, name, fd);return -1 ; } + dest_addr =NULL ; + dest_len =0 ;break ;default :error_setg (errp,"only support inet or fd type for local" );
@@ -515,7 +537,9 @@ int net_init_dgram(const Netdev *netdev, const char *name, }if (remote) { - s->dgram_dst = raddr_in; +g_assert (s->dest_addr ==NULL ); + s->dest_addr = dest_addr; + s->dest_len = dest_len; }switch (local->type) {
Signature · 1 line
2.37.3
PATCH v13 12/17 net: dgram: add unix socket
In reply to Laurent Vivier--- net/dgram.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++- qapi/net.json | 2 +- qemu-options.hx | 1 + 3 files changed, 56 insertions(+), 2 deletions(-)
net/dgram.c +54 -1
@@ -426,6 +426,7 @@ int net_init_dgram(const Netdev *netdev, const char *name,SocketAddress *remote, *local;struct sockaddr *dest_addr;struct sockaddr_in laddr_in, raddr_in; +struct sockaddr_un laddr_un, raddr_un;socklen_t dest_len;assert (netdev->type ==NET_CLIENT_DRIVER_DGRAM );
@@ -465,7 +466,8 @@ int net_init_dgram(const Netdev *netdev, const char *name, } }else {if (local->type !=SOCKET_ADDRESS_TYPE_FD ) { -error_setg (errp,"type=inet requires remote parameter" ); +error_setg (errp, +"type=inet or type=unix requires remote parameter" );return -1 ; } }
@@ -508,6 +510,53 @@ int net_init_dgram(const Netdev *netdev, const char *name, dest_addr =g_malloc (dest_len);memcpy (dest_addr, &raddr_in, dest_len);break ; +case SOCKET_ADDRESS_TYPE_UNIX : + 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); + + dest_len =sizeof (raddr_un); + dest_addr =g_malloc (dest_len); +memcpy (dest_addr, &raddr_un, dest_len); +break ;case SOCKET_ADDRESS_TYPE_FD : fd =monitor_fd_param (monitor_cur (), local->u.fd.str, errp);if (fd ==-1 ) {
@@ -546,6 +595,10 @@ int net_init_dgram(const Netdev *netdev, const char *name,inet_ntoa (raddr_in.sin_addr), ntohs(raddr_in.sin_port));break ; +case SOCKET_ADDRESS_TYPE_UNIX : +qemu_set_info_str (&s->nc,"udp=%s:%s" , + laddr_un.sun_path, raddr_un.sun_path); +break ;case SOCKET_ADDRESS_TYPE_FD : {SocketAddress *sa;SocketAddressType sa_type;
qapi/net.json +1 -1
@@ -600,7 +600,7 @@ # @remote: remote address # @local: local address # -# Only SocketAddress types 'inet' and 'fd' are supported. +# Only SocketAddress types 'unix', 'inet' and 'fd' are supported. # # If remote address is present and it's a multicast address, local address # is optional. Otherwise local address is required and remote address is
qemu-options.hx +1 -0
@@ -2782,6 +2782,7 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev, " configure a network backend to connect to a multicast maddr and port\n" " use ``local.host=addr`` to specify the host address to send packets from\n" "-netdev dgram,id=str,local.type=inet,local.host=addr,local.port=port[,remote.type=inet,remote.host=addr,remote.port=port]\n" + "-netdev dgram,id=str,local.type=unix,local.path=path[,remote.type=unix,remote.path=path]\n" "-netdev dgram,id=str,local.type=fd,local.str=file-descriptor\n" " configure a network backend to connect to another network\n" " using an UDP tunnel\n"
Signature · 1 line
2.37.3
PATCH v13 14/17 qemu-sockets: update socket_uri() and socket_parse() to be consistent
In reply to Laurent VivierTo be consistent with socket_uri(), add 'tcp:' prefix for inet type in socket_parse(), by default socket_parse() use tcp when no prefix is provided (format is host:port).
In socket_uri(), use 'vsock:' prefix for vsock type rather than 'tcp:' because it makes a vsock address look like an inet address with CID misinterpreted as host.
Goes back to commit 9aca82ba31 "migration: Create socket-address parameter"
--- util/qemu-sockets.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
util/qemu-sockets.c +6 -1
@@ -1090,7 +1090,7 @@ char *socket_uri(SocketAddress *addr)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" , +return g_strdup_printf ("vsock:%s:%s" , addr->u.vsock.cid, addr->u.vsock.port);default :
@@ -1124,6 +1124,11 @@ SocketAddress *socket_parse(const char *str, Error **errp)if (vsock_parse (&addr->u.vsock, str +strlen ("vsock:" ), errp)) { goto fail; } + }else if (strstart (str,"tcp:" ,NULL )) { + addr->type =SOCKET_ADDRESS_TYPE_INET ; +if (inet_parse (&addr->u.inet, str +strlen ("tcp:" ), errp)) { + goto fail; + } }else { addr->type =SOCKET_ADDRESS_TYPE_INET ;if (inet_parse (&addr->u.inet, str, errp)) {
Signature · 1 line
2.37.3
PATCH v13 15/17 net: stream: move to QIO to enable additional parameters
In reply to Laurent VivierUse QIOChannel, QIOChannelSocket and QIONetListener.
This allows net/stream to use all the available parameters provided by SocketAddress.
--- net/stream.c | 493 +++++++++++++++++------------------------------- qemu-options.hx | 4 +- 2 files changed, 179 insertions(+), 318 deletions(-)
net/stream.c +177 -316
@@ -35,48 +35,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*/ -bool read_poll;/* waiting to receive data? */ -bool write_poll;/* waiting to transmit data? */ }NetStreamState ; -static void net_stream_send (void *opaque); -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 ? net_stream_send :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,
@@ -93,13 +81,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); - -if (ret ==-1 && errno ==EAGAIN ) { + 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 ==QIO_CHANNEL_ERR_BLOCK ) { ret =0 ;/* handled further down */ }if (ret ==-1 ) {
@@ -108,19 +98,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 ); } }
@@ -131,19 +127,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;
@@ -151,51 +152,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 ; } -closesocket (s->fd); +if (s->listener) { +qio_net_listener_set_client_func (s->listener, net_stream_listen, + s,NULL ); + } +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;qemu_set_info_str (&s->nc,"" ); -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; -net_stream_read_poll (s, true); -} -static NetClientInfo net_stream_info = { .type =NET_CLIENT_DRIVER_STREAM , .size =sizeof (NetStreamState),
@@ -203,76 +216,67 @@ static NetClientInfo net_stream_info = { .cleanup = net_stream_cleanup, }; -static NetStreamState *net_stream_fd_init (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; - - nc =qemu_new_net_client (&net_stream_info, peer, model, name); +NetStreamState *s = opaque; +SocketAddress *addr; +char *uri; -qemu_set_info_str (nc,"fd=%d" , fd); +object_ref (OBJECT (cioc)); - s =DO_UPCAST (NetStreamState, nc, nc); +qio_net_listener_set_client_func (s->listener,NULL , s,NULL ); - s->fd = fd; - s->listen_fd =-1 ; -net_socket_rs_init (&s->rs, net_stream_rs_finalize, false); + s->ioc =QIO_CHANNEL (cioc); +qio_channel_set_name (s->ioc,"stream-server" ); + s->nc.link_down = false; -/* Disable Nagle algorithm on TCP sockets to reduce latency */ -socket_set_nodelay (fd); + s->ioc_read_tag =qio_channel_add_watch (s->ioc,G_IO_IN , net_stream_send, + s,NULL ); -if (is_connected) { -net_stream_connect (s); +if (cioc->localAddr.ss_family ==AF_UNIX ) { + addr =qio_channel_socket_get_local_address (cioc,NULL ); }else { -qemu_set_fd_handler (s->fd,NULL , net_stream_connect, s); + addr =qio_channel_socket_get_remote_address (cioc,NULL ); } -return s; +g_assert (addr !=NULL ); + uri =socket_uri (addr); +qemu_set_info_str (&s->nc, uri); +g_free (uri); +qapi_free_SocketAddress (addr); + } -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 ; - } - } +QIOChannelSocket *listen_sioc =QIO_CHANNEL_SOCKET (s->listen_ioc); +SocketAddress *addr; +int ret; - 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; - -qemu_set_info_str (&s->nc,"connection from %s:%d" , -inet_ntoa (saddr_in->sin_addr), -ntohs (saddr_in->sin_port)); -break ; +if (listen_sioc->fd <0 ) { +qemu_set_info_str (&s->nc,"connection error" ); +return ; } -case AF_UNIX : { -struct sockaddr_un saddr_un; - len =sizeof (saddr_un); -getsockname (s->listen_fd, (struct sockaddr *)&saddr_un, &len); -qemu_set_info_str (&s->nc,"connect from %s" , saddr_un.sun_path); -break ; - } -default : -g_assert_not_reached (); + 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 ) { +qemu_set_info_str (&s->nc,"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,105 +287,61 @@ static int net_stream_server_init(NetClientState *peer, {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 ; - } +QIOChannelSocket *listen_sioc =qio_channel_socket_new (); - 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); + nc =qemu_new_net_client (&net_stream_info, peer, model, name); + s =DO_UPCAST (NetStreamState, nc, nc); -socket_set_fast_reuse (fd); + s->listen_ioc =QIO_CHANNEL (listen_sioc); +qio_channel_socket_listen_async (listen_sioc, addr,0 , + net_stream_server_listening, s, +NULL ,NULL ); - 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 : { -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 ; - } +return 0 ; +} - saddr_un.sun_family =PF_UNIX ; - ret =snprintf (saddr_un.sun_path,sizeof (saddr_un.sun_path),"%s" , - addr->u.q_unix.path); -if (ret <0 || ret >=sizeof (saddr_un.sun_path)) { -error_setg (errp,"UNIX socket path '%s' is too long" , - addr->u.q_unix.path); -error_append_hint (errp,"Path must be less than %zu bytes\n" , -sizeof (saddr_un.sun_path)); -return -1 ; - } +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; - 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 : -error_setg (errp,"only support inet or fd type" ); -return -1 ; +if (sioc->fd <0 ) { +qemu_set_info_str (&s->nc,"connection error" ); + goto error; } - ret =listen (fd,0 ); -if (ret <0 ) { -error_setg_errno (errp, errno,"can't listen on socket" ); -closesocket (fd); -return -1 ; + addr =qio_channel_socket_get_remote_address (sioc,NULL ); +g_assert (addr !=NULL ); + uri =socket_uri (addr); +qemu_set_info_str (&s->nc, uri); +g_free (uri); + + ret =qemu_socket_try_set_nonblock (sioc->fd); +if (addr->type ==SOCKET_ADDRESS_TYPE_FD && ret <0 ) { +qemu_set_info_str (&s->nc,"can't use file descriptor %s (errno %d)" , + addr->u.fd.str, -ret); +qapi_free_SocketAddress (addr); + goto error; } +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 ); + s->nc.link_down = false; + +return ; +error: +object_unref (OBJECT (s->ioc)); + s->ioc =NULL ; } static int net_stream_client_init(NetClientState *peer,
@@ -391,118 +351,19 @@ static int net_stream_client_init(NetClientState *peer, Error **errp) {NetStreamState *s; -struct sockaddr_in saddr_in; -struct sockaddr_un saddr_un; -int fd, connected, ret; - -switch (addr->type) { -case SOCKET_ADDRESS_TYPE_INET : -if (convert_host_port (&saddr_in, addr->u.inet.host, addr->u.inet.port, - errp) <0 ) { -return -1 ; - } +NetClientState *nc; +QIOChannelSocket *sioc =qio_channel_socket_new (); - 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 ; - } - } -break ; -case SOCKET_ADDRESS_TYPE_UNIX : - saddr_un.sun_family =PF_UNIX ; - ret =snprintf (saddr_un.sun_path,sizeof (saddr_un.sun_path),"%s" , - addr->u.q_unix.path); -if (ret <0 || ret >=sizeof (saddr_un.sun_path)) { -error_setg (errp,"UNIX socket path '%s' is too long" , - addr->u.q_unix.path); -error_append_hint (errp,"Path must be less than %zu bytes\n" , -sizeof (saddr_un.sun_path)); -return -1 ; - } + nc =qemu_new_net_client (&net_stream_info, peer, model, name); + s =DO_UPCAST (NetStreamState, nc, nc); - 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 ; - } - } -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 ; -break ; -default : -error_setg (errp,"only support inet, unix or fd type" ); -return -1 ; - } + s->ioc =QIO_CHANNEL (sioc); + s->nc.link_down = true; + +qio_channel_socket_connect_async (sioc, addr, + net_stream_client_connected, s, +NULL ,NULL ); - s =net_stream_fd_init (peer, model, name, fd, connected); - -switch (addr->type) { -case SOCKET_ADDRESS_TYPE_INET : -qemu_set_info_str (&s->nc,"connect to %s:%d" , -inet_ntoa (saddr_in.sin_addr), -ntohs (saddr_in.sin_port)); -break ; -case SOCKET_ADDRESS_TYPE_UNIX : -qemu_set_info_str (&s->nc," connect to %s" , saddr_un.sun_path); -break ; -case SOCKET_ADDRESS_TYPE_FD : -qemu_set_info_str (&s->nc,"connect to fd %d" , fd); -break ; -default : -g_assert_not_reached (); - }return 0 ; }
qemu-options.hx +2 -2
@@ -2772,8 +2772,8 @@ 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=unix,addr.path=path\n" + "-netdev stream,id=str[,server=on|off],addr.type=inet,addr.host=host,addr.port=port[,to=maxport][,numeric=on|off][,keep-alive=on|off][,mptcp=on|off][,addr.ipv4=on|off][,addr.ipv6=on|off]\n" + "-netdev stream,id=str[,server=on|off],addr.type=unix,addr.path=path[,abstract=on|off][,tight=on|off]\n" "-netdev stream,id=str[,server=on|off],addr.type=fd,addr.str=file-descriptor\n" " configure a network backend to connect to another network\n" " using a socket connection in stream mode.\n"
Signature · 1 line
2.37.3
PATCH v13 17/17 net: stream: add QAPI events to report connection state
In reply to Laurent VivierThe netdev reports NETDEV_STREAM_CONNECTED event when the backend is connected, and NETDEV_STREAM_DISCONNECTED when it is disconnected.
The NETDEV_STREAM_CONNECTED event includes the URI of the destination address.
--- net/stream.c | 9 +++++++-- qapi/net.json | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-)
net/stream.c +7 -2
@@ -38,6 +38,7 @@#include "io/channel.h" #include "io/channel-socket.h" #include "io/net-listener.h" +#include "qapi/qapi-events-net.h" typedef struct NetStreamState { NetClientState nc;
@@ -168,6 +169,8 @@ static gboolean net_stream_send(QIOChannel *ioc, s->nc.link_down = true;qemu_set_info_str (&s->nc,"" ); +qapi_event_send_netdev_stream_disconnected (s->nc.name); +return G_SOURCE_REMOVE ; } buf = buf1;
@@ -244,8 +247,8 @@ static void net_stream_listen(QIONetListener *listener, uri =socket_uri (addr);qemu_set_info_str (&s->nc, uri);g_free (uri); +qapi_event_send_netdev_stream_connected (s->nc.name, addr);qapi_free_SocketAddress (addr); - }static void net_stream_server_listening (QIOTask *task,gpointer opaque)
@@ -327,7 +330,6 @@ static void net_stream_client_connected(QIOTask *task, gpointer opaque) goto error; }g_assert (ret ==0 ); -qapi_free_SocketAddress (addr);net_socket_rs_init (&s->rs, net_stream_rs_finalize, false);
@@ -338,6 +340,9 @@ static void net_stream_client_connected(QIOTask *task, gpointer opaque) s, NULL); s->nc.link_down = false; +qapi_event_send_netdev_stream_connected (s->nc.name, addr); +qapi_free_SocketAddress (addr); +return ; error:object_unref (OBJECT (s->ioc));
qapi/net.json +49 -0
52 lines not on this page · raw message
Signature · 1 line
2.37.3
Re: [PATCH v13 17/17] net: stream: add QAPI events to report connection state
Quoted 2 lines · Laurent Vivier
The netdev reports
NETDEV_STREAM_CONNECTEDevent when the backend is connected, andNETDEV_STREAM_DISCONNECTEDwhen it is disconnected.
Use cases?
Could similar event signalling be useful for other kinds of netdev backends?
Quoted 2 lines
The
NETDEV_STREAM_CONNECTEDevent includes the URI of the destination address.
No more. Easy fix: scratch "the URI of".
Quoted 29 lines
Signed-off-by Laurent Vivier <[email protected]>--- net/stream.c | 9 +++++++-- qapi/net.json | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-)net/stream.c +3
-0@@ -38,6 +38,7 @@#include "io/channel.h" #include "io/channel-socket.h" #include "io/net-listener.h" +#include "qapi/qapi-events-net.h" typedef struct NetStreamState { NetClientState nc;@@ -168,6 +169,8 @@ static gboolean net_stream_send(QIOChannel *ioc, s->nc.link_down = true;qemu_set_info_str (&s->nc,"" ); +qapi_event_send_netdev_stream_disconnected (s->nc.name); +return G_SOURCE_REMOVE ; } buf = buf1;
net/stream.c static void net_stream_listen(QIONetListener *listener, · line 248
Quoted 6 lines
net/stream.c +1
-1@@ -244,8 +247,8 @@ static void net_stream_listen(QIONetListener *listener, uri =socket_uri (addr);qemu_set_info_str (&s->nc, uri);g_free (uri); +qapi_event_send_netdev_stream_connected (s->nc.name, addr);qapi_free_SocketAddress (addr); -
Don't add this blank line in PATCH 15, please.
Quoted 12 lines
net/stream.c +0
-1}static void net_stream_server_listening (QIOTask *task,gpointer opaque)@@ -327,7 +330,6 @@ static void net_stream_client_connected(QIOTask *task, gpointer opaque) goto error; }g_assert (ret ==0 ); -qapi_free_SocketAddress (addr);net_socket_rs_init (&s->rs, net_stream_rs_finalize, false);
net/stream.c static void net_stream_client_connected(QIOTask *task, gpointer opaque) · line 348
Quoted 9 lines
net/stream.c +3
-0@@ -338,6 +340,9 @@ static void net_stream_client_connected(QIOTask *task, gpointer opaque) s, NULL); s->nc.link_down = false; +qapi_event_send_netdev_stream_connected (s->nc.name, addr); +qapi_free_SocketAddress (addr); +return ; error:object_unref (OBJECT (s->ioc));
Could put the qapi_free_SocketAddress() in its final place in PATCH 15 already to reduce churn. Up to you.
Quoted 47 lines
qapi/net.json +39
-0@@ -895,3 +895,52 @@ ## { 'event': 'FAILOVER_NEGOTIATED', 'data': {'device-id': 'str'} } + +## +# @NETDEV_STREAM_CONNECTED: +# +# Emitted when the netdev stream backend is connected +# +# @netdev-id: QEMU netdev id that is connected +# @addr: The destination address +# +# Since: 7.2 +# +# Example: +# +# <- { "event": "NETDEV_STREAM_CONNECTED", +# "data": { "netdev-id": "netdev0", +# "addr": { "port": "47666", "ipv6": true, +# "host": "::1", "type": "inet" } }, +# "timestamp": { "seconds": 1666269863, "microseconds": 311222 } } +# +# or +# +# <- { "event": "NETDEV_STREAM_CONNECTED", +# "data": { "netdev-id": "netdev0", +# "addr": { "path": "/tmp/qemu0", "type": "unix" } }, +# "timestamp": { "seconds": 1666269706, "microseconds": 413651 } } +# +## +{ 'event': 'NETDEV_STREAM_CONNECTED', + 'data': { 'netdev-id': 'str', + 'addr': 'SocketAddress' } } + +## +# @NETDEV_STREAM_DISCONNECTED: +# +# Emitted when the netdev stream backend is disconnected +# +# @netdev-id: QEMU netdev id that is disconnected +# +# Since: 7.2
qapi/net.json line 946
Quoted 10 lines
qapi/net.json +10
-0+# +# Example: +# +# <- { 'event': 'NETDEV_STREAM_DISCONNECTED', +# 'data': {'netdev-id': 'netdev0'}, +# 'timestamp': {'seconds': 1663330937, 'microseconds': 526695} } +# +## +{ 'event': 'NETDEV_STREAM_DISCONNECTED', + 'data': { 'netdev-id': 'str' } }
Schema looks good to me.
Re: [PATCH v13 17/17] net: stream: add QAPI events to report connection state
Quoted 6 lines · Markus Armbruster
Quoted 2 lines · Laurent Vivier
The netdev reports
NETDEV_STREAM_CONNECTEDevent when the backend is connected, andNETDEV_STREAM_DISCONNECTEDwhen it is disconnected.Use cases?
This is asked by Stefano Brivio to allow libvirt to detect if connection to passt is lost and to restart passt.
I have also a patch to add a "reconnect=seconds" option, but I didn't want to add it to this series.
Quoted 3 lines
Could similar event signalling be useful for other kinds of netdev backends?
I was wondering, but it becomes more complicated to be generic.
Thanks,
Laurent
Re: [PATCH v13 17/17] net: stream: add QAPI events to report connection state
Quoted 9 lines · Laurent Vivier
Quoted 6 lines · Markus Armbruster
Quoted 2 lines · Laurent Vivier
The netdev reports
NETDEV_STREAM_CONNECTEDevent when the backend is connected, andNETDEV_STREAM_DISCONNECTEDwhen it is disconnected.Use cases?
This is asked by Stefano Brivio to allow libvirt to detect if connection to passt is lost and to restart passt.
Let's add something like this to the commit message:
This lets libvirt notice when the connection is lost somehow, and restart the peer (such as passt).
Who's working on the libvirt part?
Quoted 1 line
I have also a patch to add a "reconnect=seconds" option, but I didn't want to add it to this series.
It's okay to mention future work in commit messages, but not required.
Quoted 4 lines
Quoted 2 lines
Could similar event signalling be useful for other kinds of netdev backends?
I was wondering, but it becomes more complicated to be generic.
Making something complicated and generic where a simpler special solution would do is the worst.
Not quite as bad (but still plenty bad) is making a few special solutions first, then replace them all with a generic solution.
I believe we should have a good, hard think on possible applications of a generic solution now.
There is no need to hold back this series for that.
If we conclude a generic solution is called for, we better replace this special solution before it becomes ABI. Either by replacing it before we release it, or by keeping it unstable until we replace it.
Re: [PATCH v13 17/17] net: stream: add QAPI events to report connection state
Quoted 36 lines · Markus Armbruster
Cc Stefano BrivioQuoted 9 lines · Laurent Vivier
Quoted 6 lines · Markus Armbruster
Quoted 2 lines · Laurent Vivier
The netdev reports
NETDEV_STREAM_CONNECTEDevent when the backend is connected, andNETDEV_STREAM_DISCONNECTEDwhen it is disconnected.Use cases?
This is asked by Stefano Brivio to allow libvirt to detect if connection to passt is lost and to restart passt.
Let's add something like this to the commit message:
This lets libvirt notice when the connection is lost somehow, and restart the peer (such as passt).
Who's working on the libvirt part?
Quoted 1 line
I have also a patch to add a "reconnect=seconds" option, but I didn't want to add it to this series.
It's okay to mention future work in commit messages, but not required.
Quoted 4 lines
Quoted 2 lines
Could similar event signalling be useful for other kinds of netdev backends?
I was wondering, but it becomes more complicated to be generic.
Making something complicated and generic where a simpler special solution would do is the worst.
Not quite as bad (but still plenty bad) is making a few special solutions first, then replace them all with a generic solution.
Quoted 9 lines
I believe we should have a good, hard think on possible applications of a generic solution now.
There is no need to hold back this series for that.
If we conclude a generic solution is called for, we better replace this special solution before it becomes ABI. Either by replacing it before we release it, or by keeping it unstable until we replace it.
I sent the v14 few minutes before this email.
Jason, perhaps we can remove PATCH 17 from the series and only merge PATCH 1 to 16?
I will resend PATCH 17 in a new series with the reconnect option patch once this series is merged.
Thanks,
Laurent
Re: [PATCH v13 17/17] net: stream: add QAPI events to report connection state
Quoted 47 lines · Laurent Vivier
Quoted 36 lines · Markus Armbruster
Cc Stefano BrivioQuoted 9 lines · Laurent Vivier
Quoted 6 lines · Markus Armbruster
Quoted 2 lines · Laurent Vivier
The netdev reports
NETDEV_STREAM_CONNECTEDevent when the backend is connected, andNETDEV_STREAM_DISCONNECTEDwhen it is disconnected.Use cases?
This is asked by Stefano Brivio to allow libvirt to detect if connection to passt is lost and to restart passt.
Let's add something like this to the commit message:
This lets libvirt notice when the connection is lost somehow, and restart the peer (such as passt).
Who's working on the libvirt part?
Quoted 1 line
I have also a patch to add a "reconnect=seconds" option, but I didn't want to add it to this series.
It's okay to mention future work in commit messages, but not required.
Quoted 4 lines
Quoted 2 lines
Could similar event signalling be useful for other kinds of netdev backends?
I was wondering, but it becomes more complicated to be generic.
Making something complicated and generic where a simpler special solution would do is the worst.
Not quite as bad (but still plenty bad) is making a few special solutions first, then replace them all with a generic solution.
Quoted 9 lines
I believe we should have a good, hard think on possible applications of a generic solution now.
There is no need to hold back this series for that.
If we conclude a generic solution is called for, we better replace this special solution before it becomes ABI. Either by replacing it before we release it, or by keeping it unstable until we replace it.
Quoted 6 lines
I sent the v14 few minutes before this email.
Jason, perhaps we can remove PATCH 17 from the series and only merge PATCH 1 to 16?
I will resend PATCH 17 in a new series with the reconnect option patch once this series is merged.
Certainly works for me. Thanks for your patience!
Re: [PATCH v13 17/17] net: stream: add QAPI events to report connection state
In reply to Markus Armbruster[Cc: Laine, full quote]
Quoted 12 lines · On Fri, 21 Oct 2022 11:12:20 +0200 Markus Armbruster <[email protected]> wrote:
Cc Stefano BrivioQuoted 8 lines · Laurent Vivier
Quoted 6 lines · Markus Armbruster
Quoted 2 lines · Laurent Vivier
The netdev reports
NETDEV_STREAM_CONNECTEDevent when the backend is connected, andNETDEV_STREAM_DISCONNECTEDwhen it is disconnected.Use cases?
Quoted 8 lines
Quoted 1 line
This is asked by Stefano Brivio to allow libvirt to detect if connection to passt is lost and to restart passt.
Let's add something like this to the commit message:
This lets libvirt notice when the connection is lost somehow, and restart the peer (such as passt).
Who's working on the libvirt part?
Laine Stump and myself. Nothing to show yet, though.
Quoted 23 lines
Quoted 1 line
I have also a patch to add a "reconnect=seconds" option, but I didn't want to add it to this series.
It's okay to mention future work in commit messages, but not required.
Quoted 4 lines
Quoted 2 lines
Could similar event signalling be useful for other kinds of netdev backends?
I was wondering, but it becomes more complicated to be generic.
Making something complicated and generic where a simpler special solution would do is the worst.
Not quite as bad (but still plenty bad) is making a few special solutions first, then replace them all with a generic solution.
I believe we should have a good, hard think on possible applications of a generic solution now.
There is no need to hold back this series for that.
If we conclude a generic solution is called for, we better replace this special solution before it becomes ABI. Either by replacing it before we release it, or by keeping it unstable until we replace it.
Signature · 1 line
Stefano
Re: [PATCH v13 17/17] net: stream: add QAPI events to report connection state
Quoted 19 lines · Stefano Brivio
[Cc: Laine, full quote]
Quoted 14 lines · On Fri, 21 Oct 2022 11:12:20 +0200 Markus Armbruster <[email protected]> wrote:
Cc Stefano BrivioQuoted 9 lines · Laurent Vivier
Quoted 6 lines · Markus Armbruster
Quoted 2 lines · Laurent Vivier
The netdev reports
NETDEV_STREAM_CONNECTEDevent when the backend is connected, andNETDEV_STREAM_DISCONNECTEDwhen it is disconnected.Use cases?
This is asked by Stefano Brivio to allow libvirt to detect if connection to passt is lost and to restart passt.
Quoted 8 lines
Quoted 6 lines
Let's add something like this to the commit message:
This lets libvirt notice when the connection is lost somehow, and restart the peer (such as passt).
Who's working on the libvirt part?
Laine Stump and myself. Nothing to show yet, though.
Good enough for me :)
[...]
Re: [PATCH v13 17/17] net: stream: add QAPI events to report connection state
In reply to Markus ArmbrusterQuoted 13 lines · Markus Armbruster
Cc Stefano BrivioQuoted 9 lines · Laurent Vivier
Quoted 6 lines · Markus Armbruster
Quoted 2 lines · Laurent Vivier
The netdev reports
NETDEV_STREAM_CONNECTEDevent when the backend is connected, andNETDEV_STREAM_DISCONNECTEDwhen it is disconnected.Use cases?
This is asked by Stefano Brivio to allow libvirt to detect if connection to passt is lost and to restart passt.
[...]
Quoted 11 lines
Quoted 4 lines
Quoted 2 lines
Could similar event signalling be useful for other kinds of netdev backends?
I was wondering, but it becomes more complicated to be generic.
Making something complicated and generic where a simpler special solution would do is the worst.
Not quite as bad (but still plenty bad) is making a few special solutions first, then replace them all with a generic solution.
Quoted 8 lines
I believe we should have a good, hard think on possible applications of a generic solution now.
There is no need to hold back this series for that.
If we conclude a generic solution is called for, we better replace this special solution before it becomes ABI. Either by replacing it before we release it, or by keeping it unstable until we replace it.
Stefano, any thoughts on this?
Re: [PATCH v13 17/17] net: stream: add QAPI events to report connection state
Quoted 32 lines · On Mon, 24 Oct 2022 13:00:09 +0200 Markus Armbruster <[email protected]> wrote:
Quoted 13 lines · Markus Armbruster
Cc Stefano BrivioQuoted 9 lines · Laurent Vivier
Quoted 6 lines · Markus Armbruster
Quoted 2 lines · Laurent Vivier
The netdev reports
NETDEV_STREAM_CONNECTEDevent when the backend is connected, andNETDEV_STREAM_DISCONNECTEDwhen it is disconnected.Use cases?
This is asked by Stefano Brivio to allow libvirt to detect if connection to passt is lost and to restart passt.
[...]
Quoted 14 lines
Quoted 4 lines
Quoted 2 lines
Could similar event signalling be useful for other kinds of netdev backends?
I was wondering, but it becomes more complicated to be generic.
Making something complicated and generic where a simpler special solution would do is the worst.
Not quite as bad (but still plenty bad) is making a few special solutions first, then replace them all with a generic solution.
I believe we should have a good, hard think on possible applications of a generic solution now.
Quoted 7 lines
Quoted 5 lines
There is no need to hold back this series for that.
If we conclude a generic solution is called for, we better replace this special solution before it becomes ABI. Either by replacing it before we release it, or by keeping it unstable until we replace it.
Stefano, any thoughts on this?
Actually, to me, it already looks as generic as it can be: stream back-ends are the only ones connecting and disconnecting.
I quickly tried to think about possible, similar events for other back-ends:
- user: handled by libslirp, there's no connection, and probably not much we can or want to export from libslirp itself
- tap, bridge: the closest equivalent would be interfaces changing states, but that's something that's also externally observable with a netlink socket, in case one needs to know. And in any case, it's logically very different from a connection or disconnection. If we want events for that, they should have different names
- vhost-user, vde: we could implement something similar if the need arises, but it should logically have a different name
- l2tpv3: stateless, same as datagram-oriented socket. No states, no events to report, I guess.
All in all, to me, NETDEV_STREAM_{,DIS}CONNECTED events here don't look very "special" or hackish.
Signature · 1 line
Stefano
Re: [PATCH v13 17/17] net: stream: add QAPI events to report connection state
Quoted 58 lines · Stefano Brivio
Quoted 32 lines · On Mon, 24 Oct 2022 13:00:09 +0200 Markus Armbruster <[email protected]> wrote:
Quoted 13 lines · Markus Armbruster
Cc Stefano BrivioQuoted 9 lines · Laurent Vivier
Quoted 6 lines · Markus Armbruster
Quoted 2 lines · Laurent Vivier
The netdev reports
NETDEV_STREAM_CONNECTEDevent when the backend is connected, andNETDEV_STREAM_DISCONNECTEDwhen it is disconnected.Use cases?
This is asked by Stefano Brivio to allow libvirt to detect if connection to passt is lost and to restart passt.
[...]
Quoted 14 lines
Quoted 4 lines
Quoted 2 lines
Could similar event signalling be useful for other kinds of netdev backends?
I was wondering, but it becomes more complicated to be generic.
Making something complicated and generic where a simpler special solution would do is the worst.
Not quite as bad (but still plenty bad) is making a few special solutions first, then replace them all with a generic solution.
I believe we should have a good, hard think on possible applications of a generic solution now.
Quoted 7 lines
Quoted 5 lines
There is no need to hold back this series for that.
If we conclude a generic solution is called for, we better replace this special solution before it becomes ABI. Either by replacing it before we release it, or by keeping it unstable until we replace it.
Stefano, any thoughts on this?
Actually, to me, it already looks as generic as it can be: stream back-ends are the only ones connecting and disconnecting.
I quickly tried to think about possible, similar events for other back-ends:
- user: handled by libslirp, there's no connection, and probably not much we can or want to export from libslirp itself
- tap, bridge: the closest equivalent would be interfaces changing states, but that's something that's also externally observable with a netlink socket, in case one needs to know. And in any case, it's logically very different from a connection or disconnection. If we want events for that, they should have different names
Quoted 8 lines
- vhost-user, vde: we could implement something similar if the need arises, but it should logically have a different name
- l2tpv3: stateless, same as datagram-oriented socket. No states, no events to report, I guess.
All in all, to me, NETDEV_STREAM_{,DIS}CONNECTED events here don't look very "special" or hackish.
Thanks!
Quoted 43 lines · Laurent Vivier
"-netdev socket" only supports inet sockets.
It's not a complex task to add support for unix sockets, but the socket netdev parameters are not defined to manage well unix socket parameters.
As discussed in:
"
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).
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
Quoted 7 lines
-netdev dgram,id=socket0,local.type=fd,addr.str=4
v13:
- fix server default in qtest
- use SocketAddress in event
- remove unwanted meson update
- update error message innet_init_dgram()
I've queued this version and will send pull requests shortly.
Any future comment we can do patches on top.
Thanks
Quoted 145 lines
v12:
- replaceNETDEV_STREAM_EOCbyNETDEV_STREAM_DISCONNECTED
- set server=off by defaultv11:
- useqemu_set_info_str()inhw/net/xen_nic.c
- fixlink_downstate in stream client mode
- cleanup error case innet_stream_receive()
- update qapi version to 7.2v10:
- add Red Hat copyright
- initializedgram_dstto NULL inSOCKET_ADDRESS_TYPE_FD
- remove redundente_stream/_dgramin functions name
- movenet_dgram_init()intonet_init_dgram()
- address Thomas' comments on qtest
- add a functionqemu_set_info_str()to set info string
- tested stream netdev with fd type using qrap/passt and "-netdev stream,addr.type=fd,server=off,addr.str=5,id=netdev0"v9:
- add events to report stream connection/disconnection
- remove fromnet/dgram.csend_fn,listen_fd,net_dgram_accept()net_dgram_connect()andnet_dgram_send()that are only needed bynet/stream.c
- remove fromnet/stream.csend_fn
- add Red Hat copyright
- add originalnet/socket.cStefano's patch (EINVAL)v8:
- test ipv4 and ipv6 parameters (stream inet)
- test abstract parameter (stream unix)
- add SocketAddressInet supported parameters in qemu-options.hx (only stream, supported by the move to QIO)
- withqio_channel_writev()replace (ret == -1 && errno == EAGAIN)by (ret == QIO_CHANNEL_ERR_BLOCK)v7:
- add qtests
- update parameters table in net.json
- updatesocket_uri()andsocket_parse()v6:
- s/netdev option/-netdev option/ PATCH 4
- s/ / /
- update @NetdevStreamOptions and @NetdevDgramOptions comments
- update PATCH 4 description message
- add missing return in error case for unix stream socket
- splitsocket_uri()patch: move and rename, then change contentv5:
- remove RFC prefix
- put the change ofnet_client_parse()into its own patch (exit()in the function)
- update comments regardingnetdev_is_modern()andnetdev_parse_modern()
- update error case innet_stream_server_init()
- update qemu-options.hx with unix type
- fix HMP "info network" with unix protocol/server side.v4:
-net_client_parse()fails withexit()rather than with return.
- keep "{ 'name': 'vmnet-host', 'if': 'CONFIG_VMNET' }" on its own line inqapi/net.json
- add a comment inqapi/net.jsonabout parameters usage
- movenetdev_is_modern()check toqemu_init()
- innetdev_is_modern(), check for JSON and useqemu_opts_do_parse()to parse parameters and detect type value.
- add a blank line after copyright commentv3:
- 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 (15):
net: introduceconvert_host_port()
net: remove the @errp argument ofnet_client_inits()
net: simplifynet_client_parse()error management
qapi: net: introduce a way to bypassqemu_opts_parse_noisily()
net: introduceqemu_set_info_str()function
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: move and renameSocketAddress_to_str()
qemu-sockets: updatesocket_uri()andsocket_parse()to be consistent
net: stream: move to QIO to enable additional parameters tests/qtest: netdev: test stream and dgram backends
net: stream: add QAPI events to report connection stateStefano Brivio (2):
net: socket: Don't ignore EINVAL on netdev socket connection
net: stream: Don't ignore EINVAL on netdev socket connectionhmp-commands.hx | 2 +- hw/net/xen_nic.c | 5 +- include/net/net.h | 7 +- include/qemu/sockets.h | 4 +- monitor/hmp-cmds.c | 23 +- net/clients.h | 6 + net/dgram.c | 623 ++++++++++++++++++++++++++++++++++++ net/hub.c | 2 + net/l2tpv3.c | 3 +- net/meson.build | 2 + net/net.c | 186 ++++++++--- net/slirp.c | 5 +- net/socket.c | 36 +-- net/stream.c | 387 ++++++++++++++++++++++ net/tap-win32.c | 3 +- net/tap.c | 13 +- net/vde.c | 3 +- net/vhost-user.c | 3 +- net/vhost-vdpa.c | 2 +- qapi/net.json | 115 ++++++- qemu-options.hx | 14 + softmmu/vl.c | 16 +- tests/qtest/meson.build | 1 + tests/qtest/netdev-socket.c | 420 ++++++++++++++++++++++++ util/qemu-sockets.c | 25 ++ 25 files changed, 1775 insertions(+), 131 deletions(-) create mode 100644 net/dgram.c create mode 100644 net/stream.c create mode 100644 tests/qtest/netdev-socket.c--
2.37.3
Quoted 3 lines · Jason Wang
I've queued this version and will send pull requests shortly.
Any future comment we can do patches on top.
Please give Laurent and me a few hours to try to improve PATCH 17's commit message. Which you could then integrate without a respin.
Quoted 9 lines · Markus Armbruster
Quoted 3 lines · Jason Wang
I've queued this version and will send pull requests shortly.
Any future comment we can do patches on top.
Please give Laurent and me a few hours to try to improve PATCH 17's commit message. Which you could then integrate without a respin.
Ok.
Thanks
Quoted 1 line
Quoted 11 lines · Jason Wang
Quoted 9 lines · Markus Armbruster
Quoted 3 lines · Jason Wang
I've queued this version and will send pull requests shortly.
Any future comment we can do patches on top.
Please give Laurent and me a few hours to try to improve PATCH 17's commit message. Which you could then integrate without a respin.
I'm going to send a new version, only patches 15 and 17 change. I moved some changes from PATCH 17 to 15 as asked by Markus, I have updated the commit message for patch 17:
net: stream: add QAPI events to report connection state
The netdev reports NETDEV_STREAM_CONNECTED event when the backend is connected, and NETDEV_STREAM_DISCONNECTED when it is disconnected.
The NETDEV_STREAM_CONNECTED event includes the destination address.
This allows a system manager like libvirt to detect when the server fails.
For instance with passt:
{ 'execute': 'qmp_capabilities' }
{ "return": { } }
{ "timestamp": { "seconds": 1666341395, "microseconds": 505347 },
"event": "NETDEV_STREAM_CONNECTED",
"data": { "netdev-id": "netdev0",
"addr": { "path": "/tmp/passt_1.socket", "type": "unix" } } }
[killing passt here]
{ "timestamp": { "seconds": 1666341430, "microseconds": 968694 },
"event": "NETDEV_STREAM_DISCONNECTED",
"data": { "netdev-id": "netdev0" } }
Thanks,
Laurent