chardev: refactoring & many bugfixes related tcp_chr_wait_connected
PATCH 3/12 chardev: forbid 'wait' option with client sockets
In reply to Daniel P. BerrangéThe 'wait'/'nowait' parameter is used to tell server sockets whether to block until a client is accepted during initialization. Client chardevs have always silently ignored this option. Various tests were mistakenly passing this option for their client chardevs.
--- chardev/char-socket.c | 12 +++++++++++- tests/ivshmem-test.c | 2 +- tests/libqtest.c | 4 ++-- tests/test-filter-redirector.c | 4 ++-- 4 files changed, 16 insertions(+), 6 deletions(-)
chardev/char-socket.c +11 -1
@@ -1047,6 +1047,12 @@ static bool qmp_chardev_validate_socket(ChardevSocket *sock,error_setg (errp,"%s" ,"Websocket client is not implemented" );return false; } +if (sock->has_wait) { +error_setg (errp,"%s" , +"'wait' option is incompatible with " +"socket in client connect mode" ); +return false; + } }return true;
@@ -1220,7 +1226,11 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, sock->tn3270 = is_tn3270; sock->has_websocket = true; sock->websocket = is_websock; - sock->has_wait = true; +/* +* We have different default to QMP for 'wait' when 'server' +* is set, hence we can't just check for existance of 'wait' +*/ + sock->has_wait =qemu_opt_find (opts,"wait" ) || is_listen; sock->wait = is_waitconnect; sock->has_reconnect =qemu_opt_find (opts,"reconnect" ); sock->reconnect = reconnect;
tests/ivshmem-test.c +1 -1
@@ -293,7 +293,7 @@ static void *server_thread(void *data)static void setup_vm_with_server (IVState *s,int nvectors,bool msi) { - char *cmd = g_strdup_printf("-chardev socket,id=chr0,path=%s,nowait " + char *cmd = g_strdup_printf("-chardev socket,id=chr0,path=%s " "-device ivshmem%s,chardev=chr0,vectors=%d", tmpserver, msi ? "-doorbell" : ",size=1M,msi=off",
tests/libqtest.c +2 -2
@@ -232,9 +232,9 @@ QTestState *qtest_init_without_qmp_handshake(const char *extra_args)qtest_add_abrt_handler (kill_qemu_hook_func, s); command =g_strdup_printf ("exec %s " -"-qtest unix:%s,nowait " +"-qtest unix:%s " "-qtest-log %s " -"-chardev socket,path=%s,nowait,id=char0 " +"-chardev socket,path=%s,id=char0 " "-mon chardev=char0,mode=control " "-machine accel=qtest " "-display none "
tests/test-filter-redirector.c +2 -2
@@ -96,7 +96,7 @@ static void test_redirector_tx(void)"-device %s,netdev=qtest-bn0,id=qtest-e0 " "-chardev socket,id=redirector0,path=%s,server,nowait " "-chardev socket,id=redirector1,path=%s,server,nowait " -"-chardev socket,id=redirector2,path=%s,nowait " +"-chardev socket,id=redirector2,path=%s " "-object filter-redirector,id=qtest-f0,netdev=qtest-bn0," "queue=tx,outdev=redirector0 " "-object filter-redirector,id=qtest-f1,netdev=qtest-bn0,"
@@ -166,7 +166,7 @@ static void test_redirector_rx(void)"-device %s,netdev=qtest-bn0,id=qtest-e0 " "-chardev socket,id=redirector0,path=%s,server,nowait " "-chardev socket,id=redirector1,path=%s,server,nowait " -"-chardev socket,id=redirector2,path=%s,nowait " +"-chardev socket,id=redirector2,path=%s " "-object filter-redirector,id=qtest-f0,netdev=qtest-bn0," "queue=rx,indev=redirector0 " "-object filter-redirector,id=qtest-f1,netdev=qtest-bn0,"
Signature · 1 line
2.20.1
PATCH 6/12 chardev: remove unused 'sioc' variable & cleanup paths
In reply to Daniel P. BerrangéThe 'sioc' variable in qmp_chardev_open_socket was unused since
commit 3e7d4d20d3a528b1ed10b1dc3d83119bfb0c5f24
Author: Peter Xu <[email protected]>
Date: Tue Mar 6 13:33:17 2018 +0800
chardev: use chardev's gcontext for async connect
--- chardev/char-socket.c | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-)
chardev/char-socket.c +7 -15
@@ -1073,7 +1073,6 @@ static void qmp_chardev_open_socket(Chardev *chr,bool is_waitconnect = sock->has_wait ? sock->wait : false;bool is_websock = sock->has_websocket ? sock->websocket : false;int64_t reconnect = sock->has_reconnect ? sock->reconnect :0 ; -QIOChannelSocket *sioc =NULL ;SocketAddress *addr; s->is_listen = is_listen;
@@ -1088,7 +1087,7 @@ static void qmp_chardev_open_socket(Chardev *chr,if (!creds) {error_setg (errp,"No TLS credentials with id '%s'" , sock->tls_creds); - goto error; +return ; } s->tls_creds = (QCryptoTLSCreds *) object_dynamic_cast(creds,
@@ -1096,20 +1095,20 @@ static void qmp_chardev_open_socket(Chardev *chr,if (!s->tls_creds) {error_setg (errp,"Object with id '%s' is not TLS credentials" , sock->tls_creds); - goto error; +return ; }object_ref (OBJECT (s->tls_creds));if (is_listen) {if (s->tls_creds->endpoint !=QCRYPTO_TLS_CREDS_ENDPOINT_SERVER ) {error_setg (errp,"%s" ,"Expected TLS credentials for server endpoint" ); - goto error; +return ; } }else {if (s->tls_creds->endpoint !=QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT ) {error_setg (errp,"%s" ,"Expected TLS credentials for client endpoint" ); - goto error; +return ; } } }
@@ -1117,7 +1116,7 @@ static void qmp_chardev_open_socket(Chardev *chr, s->addr = addr =socket_address_flatten (sock->addr);if (!qmp_chardev_validate_socket (sock, addr, errp)) { - goto error; +return ; }qemu_chr_set_feature (chr,QEMU_CHAR_FEATURE_RECONNECTABLE );
@@ -1153,7 +1152,7 @@ static void qmp_chardev_open_socket(Chardev *chr,if (qio_net_listener_open_sync (s->listener, s->addr, errp) <0 ) {object_unref (OBJECT (s->listener)); s->listener =NULL ; - goto error; +return ; }qapi_free_SocketAddress (s->addr);
@@ -1171,16 +1170,9 @@ static void qmp_chardev_open_socket(Chardev *chr, chr->gcontext); } }else if (qemu_chr_wait_connected (chr, errp) <0 ) { - goto error; +return ; } } - -return ; - -error: -if (sioc) { -object_unref (OBJECT (sioc)); - } } static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
Signature · 1 line
2.20.1
PATCH 4/12 chardev: remove many local variables in qemu_chr_parse_socket
In reply to Daniel P. BerrangéNow that all validation is separated off into a separate method, we can directly populate the ChardevSocket struct from the QemuOpts values, avoiding many local variables.
--- chardev/char-socket.c | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-)
chardev/char-socket.c +18 -22
@@ -1186,18 +1186,10 @@ error:static void qemu_chr_parse_socket (QemuOpts *opts,ChardevBackend *backend,Error **errp) { -bool is_listen =qemu_opt_get_bool (opts,"server" , false); -bool is_waitconnect = is_listen &&qemu_opt_get_bool (opts,"wait" , true); -bool is_telnet =qemu_opt_get_bool (opts,"telnet" , false); -bool is_tn3270 =qemu_opt_get_bool (opts,"tn3270" , false); -bool is_websock =qemu_opt_get_bool (opts,"websocket" , false); -bool do_nodelay = !qemu_opt_get_bool (opts,"delay" , true); -int64_t reconnect =qemu_opt_get_number (opts,"reconnect" ,0 );const char *path =qemu_opt_get (opts,"path" );const char *host =qemu_opt_get (opts,"host" );const char *port =qemu_opt_get (opts,"port" );const char *fd =qemu_opt_get (opts,"fd" ); -const char *tls_creds =qemu_opt_get (opts,"tls-creds" );SocketAddressLegacy *addr;ChardevSocket *sock;
@@ -1216,26 +1208,30 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, sock = backend->u.socket.data =g_new0 (ChardevSocket,1 );qemu_chr_parse_common (opts,qapi_ChardevSocket_base (sock)); - sock->has_nodelay = true; - sock->nodelay = do_nodelay; + sock->has_nodelay =qemu_opt_get (opts,"delay" ); + sock->nodelay = !qemu_opt_get_bool (opts,"delay" , true); +/* +* We have different default to QMP for 'server', hence +* we can't just check for existance of 'server' +*/ sock->has_server = true; - sock->server = is_listen; - sock->has_telnet = true; - sock->telnet = is_telnet; - sock->has_tn3270 = true; - sock->tn3270 = is_tn3270; - sock->has_websocket = true; - sock->websocket = is_websock; + sock->server =qemu_opt_get_bool (opts,"server" , false); + sock->has_telnet =qemu_opt_get (opts,"telnet" ); + sock->telnet =qemu_opt_get_bool (opts,"telnet" , false); + sock->has_tn3270 =qemu_opt_get (opts,"tn3270" ); + sock->tn3270 =qemu_opt_get_bool (opts,"tn3270" , false); + sock->has_websocket =qemu_opt_get (opts,"websocket" ); + sock->websocket =qemu_opt_get_bool (opts,"websocket" , false);/* * We have different default to QMP for 'wait' when 'server' * is set, hence we can't just check for existance of 'wait' */ - sock->has_wait =qemu_opt_find (opts,"wait" ) || is_listen; - sock->wait = is_waitconnect; + sock->has_wait =qemu_opt_find (opts,"wait" ) || sock->server; + sock->wait =qemu_opt_get_bool (opts,"wait" , true); sock->has_reconnect =qemu_opt_find (opts,"reconnect" ); - sock->reconnect = reconnect; - sock->has_tls_creds = tls_creds; - sock->tls_creds =g_strdup (tls_creds); + sock->reconnect =qemu_opt_get_number (opts,"reconnect" ,0 ); + sock->has_tls_creds =qemu_opt_get (opts,"tls-creds" ); + sock->tls_creds =g_strdup (qemu_opt_get (opts,"tls-creds" )); addr =g_new0 (SocketAddressLegacy,1 );if (path) {
Signature · 1 line
2.20.1
PATCH 7/12 chardev: split tcp_chr_wait_connected into two methods
In reply to Daniel P. BerrangéThe tcp_chr_wait_connected method can deal with either server or client chardevs, but some callers only care about one of these possibilities. The tcp_chr_wait_connected method will also need some refactoring to reliably deal with its primary goal of allowing a device frontend to wait for an established connection, which will interfere with other callers.
Split it into two methods, one responsible for server initiated connections, the other responsible for client initiated connections. In doing this split the tcp_char_connect_async() method is renamed to become consistent with naming of the new methods.
--- chardev/char-socket.c | 59 +++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 22 deletions(-)
chardev/char-socket.c +37 -22
@@ -886,30 +886,47 @@ static void tcp_chr_accept(QIONetListener *listener,tcp_chr_new_client (chr, cioc); } -static int tcp_chr_wait_connected (Chardev *chr,Error **errp) + +static int tcp_chr_connect_client_sync (Chardev *chr,Error **errp) +{ +SocketChardev *s =SOCKET_CHARDEV (chr); +QIOChannelSocket *sioc =qio_channel_socket_new (); +tcp_chr_set_client_ioc_name (chr, sioc); +if (qio_channel_socket_connect_sync (sioc, s->addr, errp) <0 ) { +object_unref (OBJECT (sioc)); +return -1 ; + } +tcp_chr_new_client (chr, sioc); +object_unref (OBJECT (sioc)); +return 0 ; +} + + +static void tcp_chr_accept_server_sync (Chardev *chr) {SocketChardev *s =SOCKET_CHARDEV (chr);QIOChannelSocket *sioc; +info_report ("QEMU waiting for connection on: %s" , + chr->filename); + sioc =qio_net_listener_wait_client (s->listener); +tcp_chr_set_client_ioc_name (chr, sioc); +tcp_chr_new_client (chr, sioc); +object_unref (OBJECT (sioc)); +} + +static int tcp_chr_wait_connected (Chardev *chr,Error **errp) +{ +SocketChardev *s =SOCKET_CHARDEV (chr);/* It can't wait on s->connected, since it is set asynchronously * in TLS and telnet cases, only wait for an accepted socket */ while (!s->ioc) {if (s->is_listen) { -info_report ("QEMU waiting for connection on: %s" , - chr->filename); - sioc =qio_net_listener_wait_client (s->listener); -tcp_chr_set_client_ioc_name (chr, sioc); -tcp_chr_new_client (chr, sioc); -object_unref (OBJECT (sioc)); +tcp_chr_accept_server_sync (chr); }else { - sioc =qio_channel_socket_new (); -tcp_chr_set_client_ioc_name (chr, sioc); -if (qio_channel_socket_connect_sync (sioc, s->addr, errp) <0 ) { -object_unref (OBJECT (sioc)); +if (tcp_chr_connect_client_sync (chr, errp) <0 ) {return -1 ; } -tcp_chr_new_client (chr, sioc); -object_unref (OBJECT (sioc)); } }
@@ -958,7 +975,7 @@ cleanup:object_unref (OBJECT (sioc)); } -static void tcp_chr_connect_async (Chardev *chr) +static void tcp_chr_connect_client_async (Chardev *chr) {SocketChardev *s =SOCKET_CHARDEV (chr);QIOChannelSocket *sioc;
@@ -982,7 +999,7 @@ static gboolean socket_reconnect_timeout(gpointer opaque)return false; } -tcp_chr_connect_async (chr); +tcp_chr_connect_client_async (chr);return false; }
@@ -1139,7 +1156,7 @@ static void qmp_chardev_open_socket(Chardev *chr, }if (s->reconnect_time) { -tcp_chr_connect_async (chr); +tcp_chr_connect_client_async (chr); }else {if (s->is_listen) {char *name;
@@ -1159,17 +1176,15 @@ static void qmp_chardev_open_socket(Chardev *chr, s->addr =socket_local_address (s->listener->sioc[0 ]->fd, errp);update_disconnected_filename (s); -if (is_waitconnect && -qemu_chr_wait_connected (chr, errp) <0 ) { -return ; - } -if (!s->ioc) { +if (is_waitconnect) { +tcp_chr_accept_server_sync (chr); + }else {qio_net_listener_set_client_func_full (s->listener, tcp_chr_accept, chr,NULL , chr->gcontext); } - }else if (qemu_chr_wait_connected (chr, errp) <0 ) { + }else if (tcp_chr_connect_client_sync (chr, errp) <0 ) {return ; } }
Signature · 1 line
2.20.1
PATCH 8/12 chardev: split up qmp_chardev_open_socket connection code
In reply to Daniel P. BerrangéIn qmp_chardev_open_socket the code for connecting client chardevs is split across two conditionals far apart with some server chardev code in the middle. Split up the method so that code for client connection setup is separate from code for server connection setup.
--- chardev/char-socket.c | 96 +++++++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 36 deletions(-)
chardev/char-socket.c +60 -36
@@ -1005,6 +1005,61 @@ static gboolean socket_reconnect_timeout(gpointer opaque) } +static int qmp_chardev_open_socket_server (Chardev *chr, +bool is_telnet, +bool is_waitconnect, +Error **errp) +{ +SocketChardev *s =SOCKET_CHARDEV (chr); +char *name; +if (is_telnet) { + s->do_telnetopt =1 ; + } + s->listener =qio_net_listener_new (); + + name =g_strdup_printf ("chardev-tcp-listener-%s" , chr->label); +qio_net_listener_set_name (s->listener, name); +g_free (name); + +if (qio_net_listener_open_sync (s->listener, s->addr, errp) <0 ) { +object_unref (OBJECT (s->listener)); + s->listener =NULL ; +return -1 ; + } + +qapi_free_SocketAddress (s->addr); + s->addr =socket_local_address (s->listener->sioc[0 ]->fd, errp); +update_disconnected_filename (s); + +if (is_waitconnect) { +tcp_chr_accept_server_sync (chr); + }else { +qio_net_listener_set_client_func_full (s->listener, + tcp_chr_accept, + chr,NULL , + chr->gcontext); + } + +return 0 ; +} + + +static int qmp_chardev_open_socket_client (Chardev *chr, +int64_t reconnect, +Error **errp) +{ +SocketChardev *s =SOCKET_CHARDEV (chr); + +if (reconnect >0 ) { + s->reconnect_time = reconnect; +tcp_chr_connect_client_async (chr); +return 0 ; + }else { +return tcp_chr_connect_client_sync (chr, errp); + } +} + +static bool qmp_chardev_validate_socket (ChardevSocket *sock,SocketAddress *addr,Error **errp)
@@ -1147,44 +1202,13 @@ static void qmp_chardev_open_socket(Chardev *chr,update_disconnected_filename (s); -if (is_listen) { -if (is_telnet || is_tn3270) { - s->do_telnetopt =1 ; +if (s->is_listen) { +if (qmp_chardev_open_socket_server (chr, is_telnet || is_tn3270, + is_waitconnect, errp) <0 ) { +return ; } - }else if (reconnect >0 ) { - s->reconnect_time = reconnect; - } - -if (s->reconnect_time) { -tcp_chr_connect_client_async (chr); }else { -if (s->is_listen) { -char *name; - s->listener =qio_net_listener_new (); - - name =g_strdup_printf ("chardev-tcp-listener-%s" , chr->label); -qio_net_listener_set_name (s->listener, name); -g_free (name); - -if (qio_net_listener_open_sync (s->listener, s->addr, errp) <0 ) { -object_unref (OBJECT (s->listener)); - s->listener =NULL ; -return ; - } - -qapi_free_SocketAddress (s->addr); - s->addr =socket_local_address (s->listener->sioc[0 ]->fd, errp); -update_disconnected_filename (s); - -if (is_waitconnect) { -tcp_chr_accept_server_sync (chr); - }else { -qio_net_listener_set_client_func_full (s->listener, - tcp_chr_accept, - chr,NULL , - chr->gcontext); - } - }else if (tcp_chr_connect_client_sync (chr, errp) <0 ) { +if (qmp_chardev_open_socket_client (chr, reconnect, errp) <0 ) {return ; } }
Signature · 1 line
2.20.1
PATCH 9/12 chardev: use a state machine for socket connection state
In reply to Daniel P. BerrangéThe socket connection state is indicated via the 'bool connected' field in the SocketChardev struct. This variable is somewhat misleading though, as it is only set to true once the connection has completed all required handshakes (eg for TLS, telnet or websockets). IOW there is a period of time in which the socket is connected, but the "connected" flag is still false.
The socket chardev really has three states that it can be in, disconnected, connecting and connected and those should be tracked explicitly.
--- chardev/char-socket.c | 63 +++++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 14 deletions(-)
chardev/char-socket.c +49 -14
@@ -46,6 +46,12 @@ typedef struct {size_t buflen; } TCPChardevTelnetInit; +typedef enum { +TCP_CHARDEV_STATE_DISCONNECTED , +TCP_CHARDEV_STATE_CONNECTING , +TCP_CHARDEV_STATE_CONNECTED , +}TCPChardevState ; + typedef struct { Chardev parent; QIOChannel *ioc; /* Client I/O channel */
@@ -53,7 +59,7 @@ typedef struct {QIONetListener *listener;GSource *hup_source;QCryptoTLSCreds *tls_creds; -int connected; +TCPChardevState state;int max_size;int do_telnetopt;int do_nodelay;
@@ -82,6 +88,21 @@ typedef struct {static gboolean socket_reconnect_timeout (gpointer opaque);static void tcp_chr_telnet_init (Chardev *chr); +static void tcp_chr_change_state (SocketChardev *s,TCPChardevState state) +{ +switch (state) { +case TCP_CHARDEV_STATE_DISCONNECTED : +break ; +case TCP_CHARDEV_STATE_CONNECTING : +assert (s->state ==TCP_CHARDEV_STATE_DISCONNECTED ); +break ; +case TCP_CHARDEV_STATE_CONNECTED : +assert (s->state ==TCP_CHARDEV_STATE_CONNECTING ); +break ; + } + s->state = state; +} +static void tcp_chr_reconn_timer_cancel (SocketChardev *s) {if (s->reconnect_timer) {
@@ -96,7 +117,7 @@ static void qemu_chr_socket_restart_timer(Chardev *chr)SocketChardev *s =SOCKET_CHARDEV (chr);char *name; -assert (s->connected ==0 ); +assert (s->state ==TCP_CHARDEV_STATE_DISCONNECTED ); name =g_strdup_printf ("chardev-socket-reconnect-%s" , chr->label); s->reconnect_timer = qemu_chr_timeout_add_ms(chr, s->reconnect_time * 1000,
@@ -131,7 +152,7 @@ static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len) {SocketChardev *s =SOCKET_CHARDEV (chr); -if (s->connected) { +if (s->state ==TCP_CHARDEV_STATE_CONNECTED ) {int ret =io_channel_send_full (s->ioc, buf, len, s->write_msgfds, s->write_msgfds_num);
@@ -164,7 +185,7 @@ static int tcp_chr_read_poll(void *opaque) {Chardev *chr =CHARDEV (opaque);SocketChardev *s =SOCKET_CHARDEV (opaque); -if (!s->connected) { +if (s->state !=TCP_CHARDEV_STATE_CONNECTED ) {return 0 ; } s->max_size =qemu_chr_be_can_write (chr);
@@ -277,7 +298,7 @@ static int tcp_set_msgfds(Chardev *chr, int *fds, int num) s->write_msgfds =NULL ; s->write_msgfds_num =0 ; -if (!s->connected || +if ((s->state !=TCP_CHARDEV_STATE_CONNECTED ) || !qio_channel_has_feature (s->ioc,QIO_CHANNEL_FEATURE_FD_PASS )) {return -1 ;
@@ -389,7 +410,7 @@ static void tcp_chr_free_connection(Chardev *chr) s->ioc =NULL ;g_free (chr->filename); chr->filename =NULL ; - s->connected =0 ; +tcp_chr_change_state (s,TCP_CHARDEV_STATE_DISCONNECTED ); }static const char *qemu_chr_socket_protocol (SocketChardev *s)
@@ -442,12 +463,12 @@ static void update_disconnected_filename(SocketChardev *s)/* NB may be called even if tcp_chr_connect has not been * reached, due to TLS or telnet initialization failure, -* so can *not* assume s->connected == true +* so can *not* assume s->state == TCP_CHARDEV_STATE_CONNECTED */ static void tcp_chr_disconnect (Chardev *chr) {SocketChardev *s =SOCKET_CHARDEV (chr); -bool emit_close = s->connected; +bool emit_close = s->state ==TCP_CHARDEV_STATE_CONNECTED ;tcp_chr_free_connection (chr);
@@ -471,7 +492,8 @@ static gboolean tcp_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)uint8_t buf[CHR_READ_BUF_LEN ];int len, size; -if (!s->connected || s->max_size <=0 ) { +if ((s->state !=TCP_CHARDEV_STATE_CONNECTED ) || + s->max_size <=0 ) {return TRUE; } len =sizeof (buf);
@@ -508,7 +530,7 @@ static int tcp_chr_sync_read(Chardev *chr, const uint8_t *buf, int len)SocketChardev *s =SOCKET_CHARDEV (chr);int size; -if (!s->connected) { +if (s->state !=TCP_CHARDEV_STATE_CONNECTED ) {return 0 ; }
@@ -564,7 +586,7 @@ static void update_ioc_handlers(SocketChardev *s) {Chardev *chr =CHARDEV (s); -if (!s->connected) { +if (s->state !=TCP_CHARDEV_STATE_CONNECTED ) {return ; }
@@ -589,7 +611,7 @@ static void tcp_chr_connect(void *opaque)g_free (chr->filename); chr->filename =qemu_chr_compute_filename (s); - s->connected =1 ; +tcp_chr_change_state (s,TCP_CHARDEV_STATE_CONNECTED );update_ioc_handlers (s);qemu_chr_be_event (chr,CHR_EVENT_OPENED ); }
@@ -828,7 +850,7 @@ static int tcp_chr_new_client(Chardev *chr, QIOChannelSocket *sioc) {SocketChardev *s =SOCKET_CHARDEV (chr); -if (s->ioc !=NULL ) { +if (s->state !=TCP_CHARDEV_STATE_CONNECTING ) {return -1 ; }
@@ -865,11 +887,17 @@ static int tcp_chr_add_client(Chardev *chr, int fd) {int ret;QIOChannelSocket *sioc; +SocketChardev *s =SOCKET_CHARDEV (chr); + +if (s->state !=TCP_CHARDEV_STATE_DISCONNECTED ) { +return -1 ; + } sioc =qio_channel_socket_new_fd (fd,NULL );if (!sioc) {return -1 ; } +tcp_chr_change_state (s,TCP_CHARDEV_STATE_CONNECTING );tcp_chr_set_client_ioc_name (chr, sioc); ret =tcp_chr_new_client (chr, sioc);object_unref (OBJECT (sioc));
@@ -881,7 +909,9 @@ static void tcp_chr_accept(QIONetListener *listener, void *opaque) {Chardev *chr =CHARDEV (opaque); +SocketChardev *s =SOCKET_CHARDEV (chr); +tcp_chr_change_state (s,TCP_CHARDEV_STATE_CONNECTING );tcp_chr_set_client_ioc_name (chr, cioc);tcp_chr_new_client (chr, cioc); }
@@ -891,8 +921,10 @@ static int tcp_chr_connect_client_sync(Chardev *chr, Error **errp) {SocketChardev *s =SOCKET_CHARDEV (chr);QIOChannelSocket *sioc =qio_channel_socket_new (); +tcp_chr_change_state (s,TCP_CHARDEV_STATE_CONNECTING );tcp_chr_set_client_ioc_name (chr, sioc);if (qio_channel_socket_connect_sync (sioc, s->addr, errp) <0 ) { +tcp_chr_change_state (s,TCP_CHARDEV_STATE_DISCONNECTED );object_unref (OBJECT (sioc));return -1 ; }
@@ -908,6 +940,7 @@ static void tcp_chr_accept_server_sync(Chardev *chr)QIOChannelSocket *sioc;info_report ("QEMU waiting for connection on: %s" , chr->filename); +tcp_chr_change_state (s,TCP_CHARDEV_STATE_CONNECTING ); sioc =qio_net_listener_wait_client (s->listener);tcp_chr_set_client_ioc_name (chr, sioc);tcp_chr_new_client (chr, sioc);
@@ -963,6 +996,7 @@ static void qemu_chr_socket_connected(QIOTask *task, void *opaque)Error *err =NULL ;if (qio_task_propagate_error (task, &err)) { +tcp_chr_change_state (s,TCP_CHARDEV_STATE_DISCONNECTED );check_report_connect_error (chr, err);error_free (err); goto cleanup;
@@ -980,6 +1014,7 @@ static void tcp_chr_connect_client_async(Chardev *chr)SocketChardev *s =SOCKET_CHARDEV (chr);QIOChannelSocket *sioc; +tcp_chr_change_state (s,TCP_CHARDEV_STATE_CONNECTING ); sioc =qio_channel_socket_new ();tcp_chr_set_client_ioc_name (chr, sioc); qio_channel_socket_connect_async(sioc, s->addr,
@@ -1307,7 +1342,7 @@ char_socket_get_connected(Object *obj, Error **errp) {SocketChardev *s =SOCKET_CHARDEV (obj); -return s->connected; +return s->state ==TCP_CHARDEV_STATE_CONNECTED ; }static void char_socket_class_init (ObjectClass *oc,void *data)
Signature · 1 line
2.20.1
PATCH 10/12 chardev: honour the reconnect setting in tcp_chr_wait_connected
In reply to Daniel P. BerrangéIf establishing a client connection fails, the tcp_chr_wait_connected method should sleep for the reconnect timeout and then retry the attempt. This ensures the callers don't immediately abort with an error when the initial connection fails.
--- chardev/char-socket.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
chardev/char-socket.c +9 -2
@@ -957,8 +957,15 @@ static int tcp_chr_wait_connected(Chardev *chr, Error **errp)if (s->is_listen) {tcp_chr_accept_server_sync (chr); }else { -if (tcp_chr_connect_client_sync (chr, errp) <0 ) { -return -1 ; +Error *err =NULL ; +if (tcp_chr_connect_client_sync (chr, &err) <0 ) { +if (s->reconnect_time) { +error_free (err); +g_usleep (s->reconnect_time); + }else { +error_propagate (errp, err); +return -1 ; + } } } }
Signature · 1 line
2.20.1
PATCH 11/12 chardev: disallow TLS/telnet/websocket with tcp_chr_wait_connected
In reply to Daniel P. BerrangéIn the previous commit
commit 1dc8a6695c731abb7461c637b2512c3670d82be4
Author: Marc-André Lureau <[email protected]>
Date: Tue Aug 16 12:33:32 2016 +0400
char: fix waiting for TLS and telnet connection
the tcp_chr_wait_connected() method was changed to check for a non-NULL 's->ioc' as a sign that there is already a connection present, as opposed to checking the "connected" flag to supposedly fix handling of TLS/telnet connections.
The original code would repeatedly call tcp_chr_wait_connected creating many connections as 'connected' would never become true. The changed code would still repeatedly call tcp_chr_wait_connected busy waiting because s->ioc is set but the chardev will never see CHR_EVENT_OPENED. IOW, the code is still broken with TLS/telnet, but in a different way.
Checking for a non-NULL 's->ioc' does not mean that a CHR_EVENT_OPENED will be ready for a TLS/telnet connection. These protocols (and the websocket protocol) all require the main loop to be running in order to complete the protocol handshake before emitting CHR_EVENT_OPENED. The tcp_chr_wait_connected() method is only used during early startup before a main loop is running, so TLS/telnet/websock connections can never complete initialization.
Making this work would require changing tcp_chr_wait_connected to run a main loop. This is quite complex since we must not allow GSource's that other parts of QEMU have registered to run yet. The current callers of tcp_chr_wait_connected do not require use of the TLS/telnet/websocket protocols, so the simplest option is to just forbid this combination completely for now.
--- chardev/char-socket.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)
chardev/char-socket.c +14 -2
@@ -951,8 +951,20 @@ static void tcp_chr_accept_server_sync(Chardev *chr)static int tcp_chr_wait_connected (Chardev *chr,Error **errp) {SocketChardev *s =SOCKET_CHARDEV (chr); -/* It can't wait on s->connected, since it is set asynchronously -* in TLS and telnet cases, only wait for an accepted socket */ +const char *opts[] = {"telnet" ,"tn3270" ,"websock" ,"tls-creds" }; +bool optset[] = { s->is_telnet, s->is_tn3270, s->is_websock, s->tls_creds }; +size_t i; + +QEMU_BUILD_BUG_ON (G_N_ELEMENTS (opts) !=G_N_ELEMENTS (optset)); +for (i =0 ; i <G_N_ELEMENTS (opts); i++) { +if (optset[i]) { +error_setg (errp, +"'%s' option is incompatible with waiting for " +"connection during early startup" , opts[i]); +return -1 ; + } + } +while (!s->ioc) {if (s->is_listen) {tcp_chr_accept_server_sync (chr);
Signature · 1 line
2.20.1
PATCH 12/12 chardev: fix race with client connections in tcp_chr_wait_connected
In reply to Daniel P. BerrangéWhen the 'reconnect' option is given for a client connection, the qmp_chardev_open_socket_client method will run an asynchronous connection attempt. The QIOChannel socket executes this is a single use background thread, so the connection will succeed immediately (assuming the server is listening). The chardev, however, won't get the result from this background thread until the main loop starts running and processes idle callbacks.
Thus when tcp_chr_wait_connected is run s->ioc will be NULL, and the state will still be TCP_CHARDEV_STATE_DISCONNECTED, but there will already be an established connection that will be associated with the chardev by the pending idle callback. tcp_chr_wait_connected doesn't see this and so attempts to establish another connection synchronously.
If the server allows multiple connections this is unhelpful but not a fatal problem as the duplicate connection will get ignored by the tcp_chr_new_client method when it sees the state is already connected.
If the server only supports a single connection, however, the tcp_chr_wait_connected method will hang forever because the server will not accept its synchronous connection attempt until the first connection is closed.
To deal with this we must ensure that qmp_chardev_open_socket_client does not actually start the asynchronous connection attempt. Instead it should schedule a timer with 0ms expiry time, which will only be processed once the main loop starts running. The tcp_chr_wait_connected method can now safely do a synchronous connection attempt without creating a race condition. When the timer expires it will see that a connection has already been established and take no further action.
--- chardev/char-socket.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-)
chardev/char-socket.c +28 -2
@@ -965,7 +965,25 @@ static int tcp_chr_wait_connected(Chardev *chr, Error **errp) } } -while (!s->ioc) { +/* +* We expect state to be as follows: +* +* - server +* - wait -> CONNECTED +* - nowait -> DISCONNECTED +* - client +* - reconnect == 0 -> CONNECTED +* - reconnect != 0 -> DISCONNECTED +* +*/ +if (s->state ==TCP_CHARDEV_STATE_CONNECTING ) { +error_setg (errp, +"Unexpected 'connecting' state when waiting for " +"connection during early startup" ); +return -1 ; + } + +while (s->state !=TCP_CHARDEV_STATE_CONNECTED ) {if (s->is_listen) {tcp_chr_accept_server_sync (chr); }else {
@@ -1106,7 +1124,15 @@ static int qmp_chardev_open_socket_client(Chardev *chr,if (reconnect >0 ) { s->reconnect_time = reconnect; -tcp_chr_connect_client_async (chr); +/* +* We must not start the socket connect attempt until the main +* loop is running, otherwise qemu_chr_wait_connect will not be +* able to take over connection establishment during startup +*/ + s->reconnect_timer =qemu_chr_timeout_add_ms (chr, +0 , + socket_reconnect_timeout, + chr);return 0 ; }else {return tcp_chr_connect_client_sync (chr, errp);
Signature · 1 line
2.20.1
cover chardev: refactoring & many bugfixes related tcp_chr_wait_connected
This series comes out of a discussion between myself & Yongji Xie in:
https://lists.gnu.org/archive/html/qemu-devel/2019-01/msg01881.html
I eventually understood that the problem faced was that tcp_chr_wait_connected was racing with the background connection attempt previously started, causing two connections to be established. This broke because some vhost user servers only allow a single connection.
After messing around with the code alot the final solution was in fact very easy. We simply have to delay the first background connection attempt until the main loop is running. It will then automatically turn into a no-op if tcp_chr_wait_connected has been run. This is dealt with in the last patch in this series
I believe this should solve the problem Yongji Xie faced, and thus not require us to add support for "nowait" option with client sockets at all. The reconnect=1 option effectively already implements nowait semantics, and now plays nicely with tcp_chr_wait_connected.
In investigating this I found various other bugs that needed fixing and identified some useful refactoring to simplify / clarify the code, hence this very long series.
This series passes make check-unit & check-qtest (for x86_64 target).
I did a test with tests/vhost-user-bridge too, which was in fact already broken for the same reason Yongji Xie found - it only accepts a single connection. This series fixes use of reconnect=1 with vhost-user-bridge.
Despite me not adding new functionality in this series, I think we could probably do with further unit test cover for the socket chardev backend. I'll investigate this while waiting for reviews on this series so far.
Daniel P. Berrangé (12):
chardev: fix validation of options for QMP created chardevs
chardev: forbid 'reconnect' option with server sockets
chardev: forbid 'wait' option with client sockets
chardev: remove many local variables in qemu_chr_parse_socket
chardev: ensure qemu_chr_parse_compat reports missing driver error
chardev: remove unused 'sioc' variable & cleanup paths
chardev: split tcp_chr_wait_connected into two methods
chardev: split up qmp_chardev_open_socket connection code
chardev: use a state machine for socket connection state
chardev: honour the reconnect setting in tcp_chr_wait_connected
chardev: disallow TLS/telnet/websocket with tcp_chr_wait_connected
chardev: fix race with client connections in tcp_chr_wait_connected
chardev/char-socket.c | 428 +++++++++++++++++++++++---------- chardev/char.c | 2 + tests/ivshmem-test.c | 2 +- tests/libqtest.c | 4 +- tests/test-filter-redirector.c | 4 +- 5 files changed, 303 insertions(+), 137 deletions(-)
Signature · 1 line
2.20.1
PATCH 2/12 chardev: forbid 'reconnect' option with server sockets
The 'reconnect' option is used to give the sleep time, in seconds, before a client socket attempts to re-establish a connection to the server. It does not make sense to set this for server sockets, as they will always accept a new client connection immediately after the previous one went away.
--- chardev/char-socket.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
chardev/char-socket.c +8 -1
@@ -1035,7 +1035,14 @@ static bool qmp_chardev_validate_socket(ChardevSocket *sock, }/* Validate any options which have a dependancy on client vs server */ -if (!(sock->has_server && sock->server)) { +if (!sock->has_server || sock->server) { +if (sock->has_reconnect) { +error_setg (errp, +"'reconnect' option is incompatible with " +"socket in server listen mode" ); +return false; + } + }else {if (sock->has_websocket && sock->websocket) {error_setg (errp,"%s" ,"Websocket client is not implemented" );return false;
Signature · 1 line
2.20.1
PATCH 5/12 chardev: ensure qemu_chr_parse_compat reports missing driver error
In reply to Daniel P. BerrangéIf no valid char driver was identified the qemu_chr_parse_compat method was silent, leaving callers no clue what failed.
--- chardev/char.c | 2 ++ 1 file changed, 2 insertions(+)
chardev/char.c +2 -0
@@ -490,6 +490,8 @@ QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename,return opts; } +error_report ("'%s' is not a valid char driver" , filename); + fail:qemu_opts_del (opts);return NULL ;
Signature · 1 line
2.20.1
PATCH 1/12 chardev: fix validation of options for QMP created chardevs
In reply to Daniel P. BerrangéThe TLS creds option is not valid with certain address types. The user config was only checked for errors when parsing legacy QemuOpts, thus the user could pass unsupported values via QMP.
Pull all code for validating options out into a new method qmp_chardev_validate_socket, that is called from the main qmp_chardev_open_socket method. This adds a missing check for rejecting TLS creds with the vsock address type.
--- chardev/char-socket.c | 92 +++++++++++++++++++++++++++++++------------ 1 file changed, 66 insertions(+), 26 deletions(-)
chardev/char-socket.c +66 -26
@@ -987,6 +987,65 @@ static gboolean socket_reconnect_timeout(gpointer opaque)return false; } + +static bool qmp_chardev_validate_socket (ChardevSocket *sock, +SocketAddress *addr, +Error **errp) +{ +/* Validate any options which have a dependancy on address type */ +switch (addr->type) { +case SOCKET_ADDRESS_TYPE_FD : +if (sock->has_reconnect) { +error_setg (errp, +"'reconnect' option is incompatible with " +"'fd' address type" ); +return false; + } +if (sock->has_tls_creds && + !(sock->has_server && sock->server)) { +error_setg (errp, +"'tls_creds' option is incompatible with " +"'fd' address type as client" ); +return false; + } +break ; + +case SOCKET_ADDRESS_TYPE_UNIX : +if (sock->has_tls_creds) { +error_setg (errp, +"'tls_creds' option is incompatible with " +"'unix' address type" ); +return false; + } +break ; + +case SOCKET_ADDRESS_TYPE_INET : +break ; + +case SOCKET_ADDRESS_TYPE_VSOCK : +if (sock->has_tls_creds) { +error_setg (errp, +"'tls_creds' option is incompatible with " +"'vsock' address type" ); +return false; + } + +default : +break ; + } + +/* Validate any options which have a dependancy on client vs server */ +if (!(sock->has_server && sock->server)) { +if (sock->has_websocket && sock->websocket) { +error_setg (errp,"%s" ,"Websocket client is not implemented" ); +return false; + } + } + +return true; +} + + static void qmp_chardev_open_socket(Chardev *chr, ChardevBackend *backend, bool *be_opened,
@@ -1004,11 +1063,6 @@ static void qmp_chardev_open_socket(Chardev *chr,QIOChannelSocket *sioc =NULL ;SocketAddress *addr; -if (!is_listen && is_websock) { -error_setg (errp,"%s" ,"Websocket client is not implemented" ); - goto error; - } - s->is_listen = is_listen; s->is_telnet = is_telnet; s->is_tn3270 = is_tn3270;
@@ -1049,10 +1103,10 @@ static void qmp_chardev_open_socket(Chardev *chr, s->addr = addr =socket_address_flatten (sock->addr); -if (sock->has_reconnect && addr->type ==SOCKET_ADDRESS_TYPE_FD ) { -error_setg (errp,"'reconnect' option is incompatible with 'fd'" ); +if (!qmp_chardev_validate_socket (sock, addr, errp)) { goto error; } +qemu_chr_set_feature (chr,QEMU_CHAR_FEATURE_RECONNECTABLE );/* TODO SOCKET_ADDRESS_FD where fd has AF_UNIX */ if (addr->type ==SOCKET_ADDRESS_TYPE_UNIX ) {
@@ -1140,27 +1194,12 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,return ; } - backend->type =CHARDEV_BACKEND_KIND_SOCKET ; -if (path) { -if (tls_creds) { -error_setg (errp,"TLS can only be used over TCP socket" ); -return ; - } - }else if (host) { -if (!port) { -error_setg (errp,"chardev: socket: no port given" ); -return ; - } - }else if (fd) { -/* We don't know what host to validate against when in client mode */ -if (tls_creds && !is_listen) { -error_setg (errp,"TLS can not be used with pre-opened client FD" ); -return ; - } - }else { -g_assert_not_reached (); +if (host && !port) { +error_setg (errp,"chardev: socket: no port given" ); +return ; } + backend->type =CHARDEV_BACKEND_KIND_SOCKET ; sock = backend->u.socket.data =g_new0 (ChardevSocket,1 );qemu_chr_parse_common (opts,qapi_ChardevSocket_base (sock));
@@ -1178,6 +1217,7 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, sock->wait = is_waitconnect; sock->has_reconnect =qemu_opt_find (opts,"reconnect" ); sock->reconnect = reconnect; + sock->has_tls_creds = tls_creds; sock->tls_creds =g_strdup (tls_creds); addr =g_new0 (SocketAddressLegacy,1 );
Signature · 1 line
2.20.1
Re: [Qemu-devel] [PATCH 01/12] chardev: fix validation of options for QMP created chardevs
Hi
Quoted 11 lines · Daniel P. Berrangé
The TLS creds option is not valid with certain address types. The user config was only checked for errors when parsing legacy QemuOpts, thus the user could pass unsupported values via QMP.
Pull all code for validating options out into a new method
qmp_chardev_validate_socket, that is called from the mainqmp_chardev_open_socketmethod. This adds a missing check for rejecting TLS creds with the vsock address type.Signed-off-by Daniel P. Berrangé <[email protected]>
Quoted 142 lines
--- chardev/char-socket.c | 92 +++++++++++++++++++++++++++++++------------ 1 file changed, 66 insertions(+), 26 deletions(-)chardev/char-socket.c +66
-27@@ -987,6 +987,65 @@ static gboolean socket_reconnect_timeout(gpointer opaque)return false; } + +static bool qmp_chardev_validate_socket (ChardevSocket *sock, +SocketAddress *addr, +Error **errp) +{ +/* Validate any options which have a dependancy on address type */ +switch (addr->type) { +case SOCKET_ADDRESS_TYPE_FD : +if (sock->has_reconnect) { +error_setg (errp, +"'reconnect' option is incompatible with " +"'fd' address type" ); +return false; + } +if (sock->has_tls_creds && + !(sock->has_server && sock->server)) { +error_setg (errp, +"'tls_creds' option is incompatible with " +"'fd' address type as client" ); +return false; + } +break ; + +case SOCKET_ADDRESS_TYPE_UNIX : +if (sock->has_tls_creds) { +error_setg (errp, +"'tls_creds' option is incompatible with " +"'unix' address type" ); +return false; + } +break ; + +case SOCKET_ADDRESS_TYPE_INET : +break ; + +case SOCKET_ADDRESS_TYPE_VSOCK : +if (sock->has_tls_creds) { +error_setg (errp, +"'tls_creds' option is incompatible with " +"'vsock' address type" ); +return false; + } + +default : +break ; + } + +/* Validate any options which have a dependancy on client vs server */ +if (!(sock->has_server && sock->server)) { +if (sock->has_websocket && sock->websocket) { +error_setg (errp,"%s" ,"Websocket client is not implemented" ); +return false; + } + } + +return true; +} + + static void qmp_chardev_open_socket(Chardev *chr, ChardevBackend *backend, bool *be_opened,@@ -1004,11 +1063,6 @@ static void qmp_chardev_open_socket(Chardev *chr,QIOChannelSocket *sioc =NULL ;SocketAddress *addr; -if (!is_listen && is_websock) { -error_setg (errp,"%s" ,"Websocket client is not implemented" ); - goto error; - } - s->is_listen = is_listen; s->is_telnet = is_telnet; s->is_tn3270 = is_tn3270;@@ -1049,10 +1103,10 @@ static void qmp_chardev_open_socket(Chardev *chr, s->addr = addr =socket_address_flatten (sock->addr); -if (sock->has_reconnect && addr->type ==SOCKET_ADDRESS_TYPE_FD ) { -error_setg (errp,"'reconnect' option is incompatible with 'fd'" ); +if (!qmp_chardev_validate_socket (sock, addr, errp)) { goto error; } +qemu_chr_set_feature (chr,QEMU_CHAR_FEATURE_RECONNECTABLE );/* TODO SOCKET_ADDRESS_FD where fd has AF_UNIX */ if (addr->type ==SOCKET_ADDRESS_TYPE_UNIX ) {@@ -1140,27 +1194,12 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,return ; } - backend->type =CHARDEV_BACKEND_KIND_SOCKET ; -if (path) { -if (tls_creds) { -error_setg (errp,"TLS can only be used over TCP socket" ); -return ; - } - }else if (host) { -if (!port) { -error_setg (errp,"chardev: socket: no port given" ); -return ; - } - }else if (fd) { -/* We don't know what host to validate against when in client mode */ -if (tls_creds && !is_listen) { -error_setg (errp,"TLS can not be used with pre-opened client FD" ); -return ; - } - }else { -g_assert_not_reached (); +if (host && !port) { +error_setg (errp,"chardev: socket: no port given" ); +return ; } + backend->type =CHARDEV_BACKEND_KIND_SOCKET ; sock = backend->u.socket.data =g_new0 (ChardevSocket,1 );qemu_chr_parse_common (opts,qapi_ChardevSocket_base (sock));@@ -1178,6 +1217,7 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, sock->wait = is_waitconnect; sock->has_reconnect =qemu_opt_find (opts,"reconnect" ); sock->reconnect = reconnect; + sock->has_tls_creds = tls_creds; sock->tls_creds =g_strdup (tls_creds); addr =g_new0 (SocketAddressLegacy,1 ); --2.20.1
Re: [Qemu-devel] [PATCH 02/12] chardev: forbid 'reconnect' option with server sockets
In reply to Daniel P. BerrangéQuoted 8 lines · Daniel P. Berrangé
The 'reconnect' option is used to give the sleep time, in seconds, before a client socket attempts to re-establish a connection to the server. It does not make sense to set this for server sockets, as they will always accept a new client connection immediately after the previous one went away.
Signed-off-by Daniel P. Berrangé <[email protected]>
Quoted 27 lines
--- chardev/char-socket.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)chardev/char-socket.c +8
-2@@ -1035,7 +1035,14 @@ static bool qmp_chardev_validate_socket(ChardevSocket *sock, }/* Validate any options which have a dependancy on client vs server */ -if (!(sock->has_server && sock->server)) { +if (!sock->has_server || sock->server) { +if (sock->has_reconnect) { +error_setg (errp, +"'reconnect' option is incompatible with " +"socket in server listen mode" ); +return false; + } + }else {if (sock->has_websocket && sock->websocket) {error_setg (errp,"%s" ,"Websocket client is not implemented" );return false; --2.20.1
Re: [Qemu-devel] [PATCH 03/12] chardev: forbid 'wait' option with client sockets
In reply to Daniel P. BerrangéQuoted 7 lines · Daniel P. Berrangé
The 'wait'/'nowait' parameter is used to tell server sockets whether to block until a client is accepted during initialization. Client chardevs have always silently ignored this option. Various tests were mistakenly passing this option for their client chardevs.
Signed-off-by Daniel P. Berrangé <[email protected]>
Quoted 91 lines
--- chardev/char-socket.c | 12 +++++++++++- tests/ivshmem-test.c | 2 +- tests/libqtest.c | 4 ++-- tests/test-filter-redirector.c | 4 ++-- 4 files changed, 16 insertions(+), 6 deletions(-)chardev/char-socket.c +11
-1@@ -1047,6 +1047,12 @@ static bool qmp_chardev_validate_socket(ChardevSocket *sock,error_setg (errp,"%s" ,"Websocket client is not implemented" );return false; } +if (sock->has_wait) { +error_setg (errp,"%s" , +"'wait' option is incompatible with " +"socket in client connect mode" ); +return false; + } }return true;@@ -1220,7 +1226,11 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, sock->tn3270 = is_tn3270; sock->has_websocket = true; sock->websocket = is_websock; - sock->has_wait = true; +/* +* We have different default to QMP for 'wait' when 'server' +* is set, hence we can't just check for existance of 'wait' +*/ + sock->has_wait =qemu_opt_find (opts,"wait" ) || is_listen; sock->wait = is_waitconnect; sock->has_reconnect =qemu_opt_find (opts,"reconnect" ); sock->reconnect = reconnect;tests/ivshmem-test.c +1
-1@@ -293,7 +293,7 @@ static void *server_thread(void *data)static void setup_vm_with_server (IVState *s,int nvectors,bool msi) { - char *cmd = g_strdup_printf("-chardev socket,id=chr0,path=%s,nowait " + char *cmd = g_strdup_printf("-chardev socket,id=chr0,path=%s " "-device ivshmem%s,chardev=chr0,vectors=%d", tmpserver, msi ? "-doorbell" : ",size=1M,msi=off",tests/libqtest.c +2
-2@@ -232,9 +232,9 @@ QTestState *qtest_init_without_qmp_handshake(const char *extra_args)qtest_add_abrt_handler (kill_qemu_hook_func, s); command =g_strdup_printf ("exec %s " -"-qtest unix:%s,nowait " +"-qtest unix:%s " "-qtest-log %s " -"-chardev socket,path=%s,nowait,id=char0 " +"-chardev socket,path=%s,id=char0 " "-mon chardev=char0,mode=control " "-machine accel=qtest " "-display none "tests/test-filter-redirector.c +2
-3@@ -96,7 +96,7 @@ static void test_redirector_tx(void)"-device %s,netdev=qtest-bn0,id=qtest-e0 " "-chardev socket,id=redirector0,path=%s,server,nowait " "-chardev socket,id=redirector1,path=%s,server,nowait " -"-chardev socket,id=redirector2,path=%s,nowait " +"-chardev socket,id=redirector2,path=%s " "-object filter-redirector,id=qtest-f0,netdev=qtest-bn0," "queue=tx,outdev=redirector0 " "-object filter-redirector,id=qtest-f1,netdev=qtest-bn0," @@ -166,7 +166,7 @@ static void test_redirector_rx(void)"-device %s,netdev=qtest-bn0,id=qtest-e0 " "-chardev socket,id=redirector0,path=%s,server,nowait " "-chardev socket,id=redirector1,path=%s,server,nowait " -"-chardev socket,id=redirector2,path=%s,nowait " +"-chardev socket,id=redirector2,path=%s " "-object filter-redirector,id=qtest-f0,netdev=qtest-bn0," "queue=rx,indev=redirector0 " "-object filter-redirector,id=qtest-f1,netdev=qtest-bn0," --2.20.1
Re: [Qemu-devel] [PATCH 04/12] chardev: remove many local variables in qemu_chr_parse_socket
In reply to Daniel P. BerrangéHi
Quoted 58 lines · Daniel P. Berrangé
Now that all validation is separated off into a separate method, we can directly populate the ChardevSocket struct from the QemuOpts values, avoiding many local variables.
Signed-off-by Daniel P. Berrangé <[email protected]>--- chardev/char-socket.c | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-)chardev/char-socket.c +11
-17@@ -1186,18 +1186,10 @@ error:static void qemu_chr_parse_socket (QemuOpts *opts,ChardevBackend *backend,Error **errp) { -bool is_listen =qemu_opt_get_bool (opts,"server" , false); -bool is_waitconnect = is_listen &&qemu_opt_get_bool (opts,"wait" , true); -bool is_telnet =qemu_opt_get_bool (opts,"telnet" , false); -bool is_tn3270 =qemu_opt_get_bool (opts,"tn3270" , false); -bool is_websock =qemu_opt_get_bool (opts,"websocket" , false); -bool do_nodelay = !qemu_opt_get_bool (opts,"delay" , true); -int64_t reconnect =qemu_opt_get_number (opts,"reconnect" ,0 );const char *path =qemu_opt_get (opts,"path" );const char *host =qemu_opt_get (opts,"host" );const char *port =qemu_opt_get (opts,"port" );const char *fd =qemu_opt_get (opts,"fd" ); -const char *tls_creds =qemu_opt_get (opts,"tls-creds" );SocketAddressLegacy *addr;ChardevSocket *sock;@@ -1216,26 +1208,30 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, sock = backend->u.socket.data =g_new0 (ChardevSocket,1 );qemu_chr_parse_common (opts,qapi_ChardevSocket_base (sock)); - sock->has_nodelay = true; - sock->nodelay = do_nodelay; + sock->has_nodelay =qemu_opt_get (opts,"delay" ); + sock->nodelay = !qemu_opt_get_bool (opts,"delay" , true); +/* +* We have different default to QMP for 'server', hence +* we can't just check for existance of 'server' +*/ sock->has_server = true; - sock->server = is_listen; - sock->has_telnet = true; - sock->telnet = is_telnet; - sock->has_tn3270 = true; - sock->tn3270 = is_tn3270; - sock->has_websocket = true; - sock->websocket = is_websock; + sock->server =qemu_opt_get_bool (opts,"server" , false); + sock->has_telnet =qemu_opt_get (opts,"telnet" ); + sock->telnet =qemu_opt_get_bool (opts,"telnet" , false); + sock->has_tn3270 =qemu_opt_get (opts,"tn3270" ); + sock->tn3270 =qemu_opt_get_bool (opts,"tn3270" , false);
chardev/char-socket.c static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, · line 1230
Quoted 10 lines
chardev/char-socket.c +4
-2+ sock->has_websocket =qemu_opt_get (opts,"websocket" ); + sock->websocket =qemu_opt_get_bool (opts,"websocket" , false);/* * We have different default to QMP for 'wait' when 'server' * is set, hence we can't just check for existance of 'wait' */ - sock->has_wait =qemu_opt_find (opts,"wait" ) || is_listen; - sock->wait = is_waitconnect; + sock->has_wait =qemu_opt_find (opts,"wait" ) || sock->server; + sock->wait =qemu_opt_get_bool (opts,"wait" , true);
That changes slightly the behaviour, since wait was not parsed before when !is_listen.
Since we are "correcting" (breaking?) things anyway in previous patches too:
Quoted 13 lines
chardev/char-socket.c +3
-4sock->has_reconnect =qemu_opt_find (opts,"reconnect" ); - sock->reconnect = reconnect; - sock->has_tls_creds = tls_creds; - sock->tls_creds =g_strdup (tls_creds); + sock->reconnect =qemu_opt_get_number (opts,"reconnect" ,0 ); + sock->has_tls_creds =qemu_opt_get (opts,"tls-creds" ); + sock->tls_creds =g_strdup (qemu_opt_get (opts,"tls-creds" )); addr =g_new0 (SocketAddressLegacy,1 );if (path) { --2.20.1
Re: [Qemu-devel] [PATCH 04/12] chardev: remove many local variables in qemu_chr_parse_socket
In reply to Daniel P. Berrangé[adding Markus in relation to a QemuOpts observation]
Quoted 11 lines · Daniel P. Berrangé
Now that all validation is separated off into a separate method, we can directly populate the ChardevSocket struct from the QemuOpts values, avoiding many local variables.
Signed-off-by Daniel P. Berrangé <[email protected]>--- chardev/char-socket.c | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-)
chardev/char-socket.c static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, · line 1212
Quoted 7 lines
chardev/char-socket.c +2
-2@@ -1216,26 +1208,30 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, sock = backend->u.socket.data =g_new0 (ChardevSocket,1 );qemu_chr_parse_common (opts,qapi_ChardevSocket_base (sock)); - sock->has_nodelay = true; - sock->nodelay = do_nodelay; + sock->has_nodelay =qemu_opt_get (opts,"delay" ); + sock->nodelay = !qemu_opt_get_bool (opts,"delay" , true);
Unrelated to this patch, but my recent proposal to make QemuOpts be a bit more typesafe:
https://lists.gnu.org/archive/html/qemu-devel/2019-01/msg02042.html
does not like users calling qemu_opt_get() of an option that has an integer default (as opposed to a string default), even if the only reason the caller was doing it was to learn if the option is present. I wonder if we should introduce
bool qemu_opt_has(QemuOpts *opts, const char *name)
and then replace existing callers which merely call qemu_opt_get() to learn whether an optional option was present/defaulted but without caring about its string value. I also wonder if Coccinelle can be employed to determine which callers fit that bill (that is, detect when a const char * result is ignored or solely used in a bool context, vs. when it is actually used as a string parameter to another function and/or saved in a const char * variable).
Now, on to actual review,
chardev/char-socket.c static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, · line 1215
Quoted 3 lines
chardev/char-socket.c +3
-0+/* +* We have different default to QMP for 'server', hence +* we can't just check for existance of 'server'
s/existance/existence/
Quoted 9 lines
chardev/char-socket.c +1
-7+*/ sock->has_server = true; - sock->server = is_listen; - sock->has_telnet = true; - sock->telnet = is_telnet; - sock->has_tn3270 = true; - sock->tn3270 = is_tn3270; - sock->has_websocket = true; - sock->websocket = is_websock;
chardev/char-socket.c static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, · line 1227
Quoted 10 lines
chardev/char-socket.c +7
-0+ sock->server =qemu_opt_get_bool (opts,"server" , false); + sock->has_telnet =qemu_opt_get (opts,"telnet" ); + sock->telnet =qemu_opt_get_bool (opts,"telnet" , false); + sock->has_tn3270 =qemu_opt_get (opts,"tn3270" ); + sock->tn3270 =qemu_opt_get_bool (opts,"tn3270" , false); + sock->has_websocket =qemu_opt_get (opts,"websocket" ); + sock->websocket =qemu_opt_get_bool (opts,"websocket" , false);/* * We have different default to QMP for 'wait' when 'server' * is set, hence we can't just check for existance of 'wait'
but then again, you were copy-pasting an existing typo.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
Re: [Qemu-devel] [PATCH 05/12] chardev: ensure qemu_chr_parse_compat reports missing driver error
In reply to Daniel P. BerrangéHi
Quoted 5 lines · Daniel P. Berrangé
If no valid char driver was identified the
qemu_chr_parse_compatmethod was silent, leaving callers no clue what failed.Signed-off-by Daniel P. Berrangé <[email protected]>
Quoted 20 lines
--- chardev/char.c | 2 ++ 1 file changed, 2 insertions(+)chardev/char.c +2
-1@@ -490,6 +490,8 @@ QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename,return opts; } +error_report ("'%s' is not a valid char driver" , filename); + fail:qemu_opts_del (opts);return NULL ; --2.20.1
Re: [Qemu-devel] [PATCH 06/12] chardev: remove unused 'sioc' variable & cleanup paths
In reply to Daniel P. BerrangéQuoted 10 lines · Daniel P. Berrangé
The 'sioc' variable in
qmp_chardev_open_socketwas unused sincecommit
3e7d4d20d3a528b1ed10b1dc3d83119bfb0c5f24
Author: Peter Xu <[email protected]>
Date: Tue Mar 6 13:33:17 2018 +0800chardev: use chardev's gcontext for async connect
Signed-off-by Daniel P. Berrangé <[email protected]>
Quoted 88 lines
--- chardev/char-socket.c | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-)chardev/char-socket.c +7
-16@@ -1073,7 +1073,6 @@ static void qmp_chardev_open_socket(Chardev *chr,bool is_waitconnect = sock->has_wait ? sock->wait : false;bool is_websock = sock->has_websocket ? sock->websocket : false;int64_t reconnect = sock->has_reconnect ? sock->reconnect :0 ; -QIOChannelSocket *sioc =NULL ;SocketAddress *addr; s->is_listen = is_listen;@@ -1088,7 +1087,7 @@ static void qmp_chardev_open_socket(Chardev *chr,if (!creds) {error_setg (errp,"No TLS credentials with id '%s'" , sock->tls_creds); - goto error; +return ; } s->tls_creds = (QCryptoTLSCreds *) object_dynamic_cast(creds,@@ -1096,20 +1095,20 @@ static void qmp_chardev_open_socket(Chardev *chr,if (!s->tls_creds) {error_setg (errp,"Object with id '%s' is not TLS credentials" , sock->tls_creds); - goto error; +return ; }object_ref (OBJECT (s->tls_creds));if (is_listen) {if (s->tls_creds->endpoint !=QCRYPTO_TLS_CREDS_ENDPOINT_SERVER ) {error_setg (errp,"%s" ,"Expected TLS credentials for server endpoint" ); - goto error; +return ; } }else {if (s->tls_creds->endpoint !=QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT ) {error_setg (errp,"%s" ,"Expected TLS credentials for client endpoint" ); - goto error; +return ; } } }@@ -1117,7 +1116,7 @@ static void qmp_chardev_open_socket(Chardev *chr, s->addr = addr =socket_address_flatten (sock->addr);if (!qmp_chardev_validate_socket (sock, addr, errp)) { - goto error; +return ; }qemu_chr_set_feature (chr,QEMU_CHAR_FEATURE_RECONNECTABLE );@@ -1153,7 +1152,7 @@ static void qmp_chardev_open_socket(Chardev *chr,if (qio_net_listener_open_sync (s->listener, s->addr, errp) <0 ) {object_unref (OBJECT (s->listener)); s->listener =NULL ; - goto error; +return ; }qapi_free_SocketAddress (s->addr);@@ -1171,16 +1170,9 @@ static void qmp_chardev_open_socket(Chardev *chr, chr->gcontext); } }else if (qemu_chr_wait_connected (chr, errp) <0 ) { - goto error; +return ; } } - -return ; - -error: -if (sioc) { -object_unref (OBJECT (sioc)); - } } static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, --2.20.1
Re: [Qemu-devel] [PATCH 07/12] chardev: split tcp_chr_wait_connected into two methods
In reply to Daniel P. BerrangéHi
Quoted 111 lines · Daniel P. Berrangé
The
tcp_chr_wait_connectedmethod can deal with either server or client chardevs, but some callers only care about one of these possibilities. Thetcp_chr_wait_connectedmethod will also need some refactoring to reliably deal with its primary goal of allowing a device frontend to wait for an established connection, which will interfere with other callers.Split it into two methods, one responsible for server initiated connections, the other responsible for client initiated connections. In doing this split the
tcp_char_connect_async()method is renamed to become consistent with naming of the new methods.Signed-off-by Daniel P. Berrangé <[email protected]>--- chardev/char-socket.c | 59 +++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 22 deletions(-)chardev/char-socket.c +33
-16@@ -886,30 +886,47 @@ static void tcp_chr_accept(QIONetListener *listener,tcp_chr_new_client (chr, cioc); } -static int tcp_chr_wait_connected (Chardev *chr,Error **errp) + +static int tcp_chr_connect_client_sync (Chardev *chr,Error **errp) +{ +SocketChardev *s =SOCKET_CHARDEV (chr); +QIOChannelSocket *sioc =qio_channel_socket_new (); +tcp_chr_set_client_ioc_name (chr, sioc); +if (qio_channel_socket_connect_sync (sioc, s->addr, errp) <0 ) { +object_unref (OBJECT (sioc)); +return -1 ; + } +tcp_chr_new_client (chr, sioc); +object_unref (OBJECT (sioc)); +return 0 ; +} + + +static void tcp_chr_accept_server_sync (Chardev *chr) {SocketChardev *s =SOCKET_CHARDEV (chr);QIOChannelSocket *sioc; +info_report ("QEMU waiting for connection on: %s" , + chr->filename); + sioc =qio_net_listener_wait_client (s->listener); +tcp_chr_set_client_ioc_name (chr, sioc); +tcp_chr_new_client (chr, sioc); +object_unref (OBJECT (sioc)); +} + +static int tcp_chr_wait_connected (Chardev *chr,Error **errp) +{ +SocketChardev *s =SOCKET_CHARDEV (chr);/* It can't wait on s->connected, since it is set asynchronously * in TLS and telnet cases, only wait for an accepted socket */ while (!s->ioc) {if (s->is_listen) { -info_report ("QEMU waiting for connection on: %s" , - chr->filename); - sioc =qio_net_listener_wait_client (s->listener); -tcp_chr_set_client_ioc_name (chr, sioc); -tcp_chr_new_client (chr, sioc); -object_unref (OBJECT (sioc)); +tcp_chr_accept_server_sync (chr); }else { - sioc =qio_channel_socket_new (); -tcp_chr_set_client_ioc_name (chr, sioc); -if (qio_channel_socket_connect_sync (sioc, s->addr, errp) <0 ) { -object_unref (OBJECT (sioc)); +if (tcp_chr_connect_client_sync (chr, errp) <0 ) {return -1 ; } -tcp_chr_new_client (chr, sioc); -object_unref (OBJECT (sioc)); } }@@ -958,7 +975,7 @@ cleanup:object_unref (OBJECT (sioc)); } -static void tcp_chr_connect_async (Chardev *chr) +static void tcp_chr_connect_client_async (Chardev *chr) {SocketChardev *s =SOCKET_CHARDEV (chr);QIOChannelSocket *sioc;@@ -982,7 +999,7 @@ static gboolean socket_reconnect_timeout(gpointer opaque)return false; } -tcp_chr_connect_async (chr); +tcp_chr_connect_client_async (chr);return false; }@@ -1139,7 +1156,7 @@ static void qmp_chardev_open_socket(Chardev *chr, }if (s->reconnect_time) { -tcp_chr_connect_async (chr); +tcp_chr_connect_client_async (chr); }else {if (s->is_listen) {char *name;
chardev/char-socket.c static void qmp_chardev_open_socket(Chardev *chr, · line 1180
Quoted 10 lines
chardev/char-socket.c +2
-5@@ -1159,17 +1176,15 @@ static void qmp_chardev_open_socket(Chardev *chr, s->addr =socket_local_address (s->listener->sioc[0 ]->fd, errp);update_disconnected_filename (s); -if (is_waitconnect && -qemu_chr_wait_connected (chr, errp) <0 ) { -return ; - } -if (!s->ioc) { +if (is_waitconnect) { +tcp_chr_accept_server_sync (chr);
Is the wait_connected() loop really unnecessary there? looks like it is. And there is no error path either. ok
Quoted 14 lines
chardev/char-socket.c +2
-2+ }else {qio_net_listener_set_client_func_full (s->listener, tcp_chr_accept, chr,NULL , chr->gcontext); } - }else if (qemu_chr_wait_connected (chr, errp) <0 ) { + }else if (tcp_chr_connect_client_sync (chr, errp) <0 ) {return ; } } --2.20.1
Re: [Qemu-devel] [PATCH 08/12] chardev: split up qmp_chardev_open_socket connection code
In reply to Daniel P. BerrangéQuoted 7 lines · Daniel P. Berrangé
In
qmp_chardev_open_socketthe code for connecting client chardevs is split across two conditionals far apart with some server chardev code in the middle. Split up the method so that code for client connection setup is separate from code for server connection setup.Signed-off-by Daniel P. Berrangé <[email protected]>
Quoted 123 lines
--- chardev/char-socket.c | 96 +++++++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 36 deletions(-)chardev/char-socket.c +60
-37@@ -1005,6 +1005,61 @@ static gboolean socket_reconnect_timeout(gpointer opaque) } +static int qmp_chardev_open_socket_server (Chardev *chr, +bool is_telnet, +bool is_waitconnect, +Error **errp) +{ +SocketChardev *s =SOCKET_CHARDEV (chr); +char *name; +if (is_telnet) { + s->do_telnetopt =1 ; + } + s->listener =qio_net_listener_new (); + + name =g_strdup_printf ("chardev-tcp-listener-%s" , chr->label); +qio_net_listener_set_name (s->listener, name); +g_free (name); + +if (qio_net_listener_open_sync (s->listener, s->addr, errp) <0 ) { +object_unref (OBJECT (s->listener)); + s->listener =NULL ; +return -1 ; + } + +qapi_free_SocketAddress (s->addr); + s->addr =socket_local_address (s->listener->sioc[0 ]->fd, errp); +update_disconnected_filename (s); + +if (is_waitconnect) { +tcp_chr_accept_server_sync (chr); + }else { +qio_net_listener_set_client_func_full (s->listener, + tcp_chr_accept, + chr,NULL , + chr->gcontext); + } + +return 0 ; +} + + +static int qmp_chardev_open_socket_client (Chardev *chr, +int64_t reconnect, +Error **errp) +{ +SocketChardev *s =SOCKET_CHARDEV (chr); + +if (reconnect >0 ) { + s->reconnect_time = reconnect; +tcp_chr_connect_client_async (chr); +return 0 ; + }else { +return tcp_chr_connect_client_sync (chr, errp); + } +} + +static bool qmp_chardev_validate_socket (ChardevSocket *sock,SocketAddress *addr,Error **errp)@@ -1147,44 +1202,13 @@ static void qmp_chardev_open_socket(Chardev *chr,update_disconnected_filename (s); -if (is_listen) { -if (is_telnet || is_tn3270) { - s->do_telnetopt =1 ; +if (s->is_listen) { +if (qmp_chardev_open_socket_server (chr, is_telnet || is_tn3270, + is_waitconnect, errp) <0 ) { +return ; } - }else if (reconnect >0 ) { - s->reconnect_time = reconnect; - } - -if (s->reconnect_time) { -tcp_chr_connect_client_async (chr); }else { -if (s->is_listen) { -char *name; - s->listener =qio_net_listener_new (); - - name =g_strdup_printf ("chardev-tcp-listener-%s" , chr->label); -qio_net_listener_set_name (s->listener, name); -g_free (name); - -if (qio_net_listener_open_sync (s->listener, s->addr, errp) <0 ) { -object_unref (OBJECT (s->listener)); - s->listener =NULL ; -return ; - } - -qapi_free_SocketAddress (s->addr); - s->addr =socket_local_address (s->listener->sioc[0 ]->fd, errp); -update_disconnected_filename (s); - -if (is_waitconnect) { -tcp_chr_accept_server_sync (chr); - }else { -qio_net_listener_set_client_func_full (s->listener, - tcp_chr_accept, - chr,NULL , - chr->gcontext); - } - }else if (tcp_chr_connect_client_sync (chr, errp) <0 ) { +if (qmp_chardev_open_socket_client (chr, reconnect, errp) <0 ) {return ; } } --2.20.1
Re: [Qemu-devel] [PATCH 09/12] chardev: use a state machine for socket connection state
In reply to Daniel P. BerrangéHi
Quoted 8 lines · Daniel P. Berrangé
The socket connection state is indicated via the 'bool connected' field in the SocketChardev struct. This variable is somewhat misleading though, as it is only set to true once the connection has completed all required handshakes (eg for TLS, telnet or websockets). IOW there is a period of time in which the socket is connected, but the "connected" flag is still false.
Quoted 5 lines
The socket chardev really has three states that it can be in, disconnected, connecting and connected and those should be tracked explicitly.
Signed-off-by Daniel P. Berrangé <[email protected]>
You could split that patch, to introduce CONNECTING in a separate step.
But lgtm so,
Quoted 233 lines
--- chardev/char-socket.c | 63 +++++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 14 deletions(-)chardev/char-socket.c +49
-15@@ -46,6 +46,12 @@ typedef struct {size_t buflen; } TCPChardevTelnetInit; +typedef enum { +TCP_CHARDEV_STATE_DISCONNECTED , +TCP_CHARDEV_STATE_CONNECTING , +TCP_CHARDEV_STATE_CONNECTED , +}TCPChardevState ; + typedef struct { Chardev parent; QIOChannel *ioc; /* Client I/O channel */@@ -53,7 +59,7 @@ typedef struct {QIONetListener *listener;GSource *hup_source;QCryptoTLSCreds *tls_creds; -int connected; +TCPChardevState state;int max_size;int do_telnetopt;int do_nodelay;@@ -82,6 +88,21 @@ typedef struct {static gboolean socket_reconnect_timeout (gpointer opaque);static void tcp_chr_telnet_init (Chardev *chr); +static void tcp_chr_change_state (SocketChardev *s,TCPChardevState state) +{ +switch (state) { +case TCP_CHARDEV_STATE_DISCONNECTED : +break ; +case TCP_CHARDEV_STATE_CONNECTING : +assert (s->state ==TCP_CHARDEV_STATE_DISCONNECTED ); +break ; +case TCP_CHARDEV_STATE_CONNECTED : +assert (s->state ==TCP_CHARDEV_STATE_CONNECTING ); +break ; + } + s->state = state; +} +static void tcp_chr_reconn_timer_cancel (SocketChardev *s) {if (s->reconnect_timer) {@@ -96,7 +117,7 @@ static void qemu_chr_socket_restart_timer(Chardev *chr)SocketChardev *s =SOCKET_CHARDEV (chr);char *name; -assert (s->connected ==0 ); +assert (s->state ==TCP_CHARDEV_STATE_DISCONNECTED ); name =g_strdup_printf ("chardev-socket-reconnect-%s" , chr->label); s->reconnect_timer = qemu_chr_timeout_add_ms(chr, s->reconnect_time * 1000,@@ -131,7 +152,7 @@ static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len) {SocketChardev *s =SOCKET_CHARDEV (chr); -if (s->connected) { +if (s->state ==TCP_CHARDEV_STATE_CONNECTED ) {int ret =io_channel_send_full (s->ioc, buf, len, s->write_msgfds, s->write_msgfds_num);@@ -164,7 +185,7 @@ static int tcp_chr_read_poll(void *opaque) {Chardev *chr =CHARDEV (opaque);SocketChardev *s =SOCKET_CHARDEV (opaque); -if (!s->connected) { +if (s->state !=TCP_CHARDEV_STATE_CONNECTED ) {return 0 ; } s->max_size =qemu_chr_be_can_write (chr);@@ -277,7 +298,7 @@ static int tcp_set_msgfds(Chardev *chr, int *fds, int num) s->write_msgfds =NULL ; s->write_msgfds_num =0 ; -if (!s->connected || +if ((s->state !=TCP_CHARDEV_STATE_CONNECTED ) || !qio_channel_has_feature (s->ioc,QIO_CHANNEL_FEATURE_FD_PASS )) {return -1 ;@@ -389,7 +410,7 @@ static void tcp_chr_free_connection(Chardev *chr) s->ioc =NULL ;g_free (chr->filename); chr->filename =NULL ; - s->connected =0 ; +tcp_chr_change_state (s,TCP_CHARDEV_STATE_DISCONNECTED ); }static const char *qemu_chr_socket_protocol (SocketChardev *s)@@ -442,12 +463,12 @@ static void update_disconnected_filename(SocketChardev *s)/* NB may be called even if tcp_chr_connect has not been * reached, due to TLS or telnet initialization failure, -* so can *not* assume s->connected == true +* so can *not* assume s->state == TCP_CHARDEV_STATE_CONNECTED */ static void tcp_chr_disconnect (Chardev *chr) {SocketChardev *s =SOCKET_CHARDEV (chr); -bool emit_close = s->connected; +bool emit_close = s->state ==TCP_CHARDEV_STATE_CONNECTED ;tcp_chr_free_connection (chr);@@ -471,7 +492,8 @@ static gboolean tcp_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)uint8_t buf[CHR_READ_BUF_LEN ];int len, size; -if (!s->connected || s->max_size <=0 ) { +if ((s->state !=TCP_CHARDEV_STATE_CONNECTED ) || + s->max_size <=0 ) {return TRUE; } len =sizeof (buf);@@ -508,7 +530,7 @@ static int tcp_chr_sync_read(Chardev *chr, const uint8_t *buf, int len)SocketChardev *s =SOCKET_CHARDEV (chr);int size; -if (!s->connected) { +if (s->state !=TCP_CHARDEV_STATE_CONNECTED ) {return 0 ; }@@ -564,7 +586,7 @@ static void update_ioc_handlers(SocketChardev *s) {Chardev *chr =CHARDEV (s); -if (!s->connected) { +if (s->state !=TCP_CHARDEV_STATE_CONNECTED ) {return ; }@@ -589,7 +611,7 @@ static void tcp_chr_connect(void *opaque)g_free (chr->filename); chr->filename =qemu_chr_compute_filename (s); - s->connected =1 ; +tcp_chr_change_state (s,TCP_CHARDEV_STATE_CONNECTED );update_ioc_handlers (s);qemu_chr_be_event (chr,CHR_EVENT_OPENED ); }@@ -828,7 +850,7 @@ static int tcp_chr_new_client(Chardev *chr, QIOChannelSocket *sioc) {SocketChardev *s =SOCKET_CHARDEV (chr); -if (s->ioc !=NULL ) { +if (s->state !=TCP_CHARDEV_STATE_CONNECTING ) {return -1 ; }@@ -865,11 +887,17 @@ static int tcp_chr_add_client(Chardev *chr, int fd) {int ret;QIOChannelSocket *sioc; +SocketChardev *s =SOCKET_CHARDEV (chr); + +if (s->state !=TCP_CHARDEV_STATE_DISCONNECTED ) { +return -1 ; + } sioc =qio_channel_socket_new_fd (fd,NULL );if (!sioc) {return -1 ; } +tcp_chr_change_state (s,TCP_CHARDEV_STATE_CONNECTING );tcp_chr_set_client_ioc_name (chr, sioc); ret =tcp_chr_new_client (chr, sioc);object_unref (OBJECT (sioc));@@ -881,7 +909,9 @@ static void tcp_chr_accept(QIONetListener *listener, void *opaque) {Chardev *chr =CHARDEV (opaque); +SocketChardev *s =SOCKET_CHARDEV (chr); +tcp_chr_change_state (s,TCP_CHARDEV_STATE_CONNECTING );tcp_chr_set_client_ioc_name (chr, cioc);tcp_chr_new_client (chr, cioc); }@@ -891,8 +921,10 @@ static int tcp_chr_connect_client_sync(Chardev *chr, Error **errp) {SocketChardev *s =SOCKET_CHARDEV (chr);QIOChannelSocket *sioc =qio_channel_socket_new (); +tcp_chr_change_state (s,TCP_CHARDEV_STATE_CONNECTING );tcp_chr_set_client_ioc_name (chr, sioc);if (qio_channel_socket_connect_sync (sioc, s->addr, errp) <0 ) { +tcp_chr_change_state (s,TCP_CHARDEV_STATE_DISCONNECTED );object_unref (OBJECT (sioc));return -1 ; }@@ -908,6 +940,7 @@ static void tcp_chr_accept_server_sync(Chardev *chr)QIOChannelSocket *sioc;info_report ("QEMU waiting for connection on: %s" , chr->filename); +tcp_chr_change_state (s,TCP_CHARDEV_STATE_CONNECTING ); sioc =qio_net_listener_wait_client (s->listener);tcp_chr_set_client_ioc_name (chr, sioc);tcp_chr_new_client (chr, sioc);@@ -963,6 +996,7 @@ static void qemu_chr_socket_connected(QIOTask *task, void *opaque)Error *err =NULL ;if (qio_task_propagate_error (task, &err)) { +tcp_chr_change_state (s,TCP_CHARDEV_STATE_DISCONNECTED );check_report_connect_error (chr, err);error_free (err); goto cleanup;@@ -980,6 +1014,7 @@ static void tcp_chr_connect_client_async(Chardev *chr)SocketChardev *s =SOCKET_CHARDEV (chr);QIOChannelSocket *sioc; +tcp_chr_change_state (s,TCP_CHARDEV_STATE_CONNECTING ); sioc =qio_channel_socket_new ();tcp_chr_set_client_ioc_name (chr, sioc); qio_channel_socket_connect_async(sioc, s->addr,@@ -1307,7 +1342,7 @@ char_socket_get_connected(Object *obj, Error **errp) {SocketChardev *s =SOCKET_CHARDEV (obj); -return s->connected; +return s->state ==TCP_CHARDEV_STATE_CONNECTED ; }static void char_socket_class_init (ObjectClass *oc,void *data) --2.20.1
Re: [Qemu-devel] [PATCH 10/12] chardev: honour the reconnect setting in tcp_chr_wait_connected
In reply to Daniel P. BerrangéHi
Quoted 5 lines · Daniel P. Berrangé
If establishing a client connection fails, the
tcp_chr_wait_connectedmethod should sleep for the reconnect timeout and then retry the attempt. This ensures the callers don't immediately abort with an error when the initial connection fails.
Obviously, that function is already a bit problematic, since it may block the thread with no easy way to cancel (well, the _sync functions in general..).
You change doesn't make it much worse, but it will now loop also in case of errors. That's what reconnect_time is supposed to do, so
Quoted 31 lines
Signed-off-by Daniel P. Berrangé <[email protected]>--- chardev/char-socket.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)chardev/char-socket.c +9
-3@@ -957,8 +957,15 @@ static int tcp_chr_wait_connected(Chardev *chr, Error **errp)if (s->is_listen) {tcp_chr_accept_server_sync (chr); }else { -if (tcp_chr_connect_client_sync (chr, errp) <0 ) { -return -1 ; +Error *err =NULL ; +if (tcp_chr_connect_client_sync (chr, &err) <0 ) { +if (s->reconnect_time) { +error_free (err); +g_usleep (s->reconnect_time); + }else { +error_propagate (errp, err); +return -1 ; + } } } } --2.20.1
Re: [Qemu-devel] [PATCH 11/12] chardev: disallow TLS/telnet/websocket with tcp_chr_wait_connected
In reply to Daniel P. BerrangéQuoted 28 lines · Daniel P. Berrangé
In the previous commit
commit
1dc8a6695c731abb7461c637b2512c3670d82be4
Author: Marc-André Lureau <[email protected]>
Date: Tue Aug 16 12:33:32 2016 +0400char: fix waiting for TLS and telnet connection
the
tcp_chr_wait_connected()method was changed to check for a non-NULL 's->ioc' as a sign that there is already a connection present, as opposed to checking the "connected" flag to supposedly fix handling of TLS/telnet connections.The original code would repeatedly call
tcp_chr_wait_connectedcreating many connections as 'connected' would never become true. The changed code would still repeatedly calltcp_chr_wait_connectedbusy waiting becauses->iocis set but the chardev will never seeCHR_EVENT_OPENED. IOW, the code is still broken with TLS/telnet, but in a different way.Checking for a non-NULL '
s->ioc' does not mean that aCHR_EVENT_OPENEDwill be ready for a TLS/telnet connection. These protocols (and the websocket protocol) all require the main loop to be running in order to complete the protocol handshake before emittingCHR_EVENT_OPENED. Thetcp_chr_wait_connected()method is only used during early startup before a main loop is running, so TLS/telnet/websock connections can never complete initialization.
Quoted 8 lines
Making this work would require changing
tcp_chr_wait_connectedto run a main loop. This is quite complex since we must not allow GSource's that other parts of QEMU have registered to run yet. The current callers oftcp_chr_wait_connecteddo not require use of the TLS/telnet/websocket protocols, so the simplest option is to just forbid this combination completely for now.Signed-off-by Daniel P. Berrangé <[email protected]>
Quoted 14 lines
--- chardev/char-socket.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)chardev/char-socket.c +0
-2@@ -951,8 +951,20 @@ static void tcp_chr_accept_server_sync(Chardev *chr)static int tcp_chr_wait_connected (Chardev *chr,Error **errp) {SocketChardev *s =SOCKET_CHARDEV (chr); -/* It can't wait on s->connected, since it is set asynchronously -* in TLS and telnet cases, only wait for an accepted socket */
chardev/char-socket.c static void tcp_chr_accept_server_sync(Chardev *chr) · line 963
Quoted 10 lines
chardev/char-socket.c +10
-0+const char *opts[] = {"telnet" ,"tn3270" ,"websock" ,"tls-creds" }; +bool optset[] = { s->is_telnet, s->is_tn3270, s->is_websock, s->tls_creds }; +size_t i; + +QEMU_BUILD_BUG_ON (G_N_ELEMENTS (opts) !=G_N_ELEMENTS (optset)); +for (i =0 ; i <G_N_ELEMENTS (opts); i++) { +if (optset[i]) { +error_setg (errp, +"'%s' option is incompatible with waiting for " +"connection during early startup" , opts[i]);
"during early startup" ? I think you could also reach this by using chardev-add & netdev_add.
Quoted 10 lines
chardev/char-socket.c +4
-1+return -1 ; + } + } +while (!s->ioc) {if (s->is_listen) {tcp_chr_accept_server_sync (chr); --2.20.1
Re: [Qemu-devel] [PATCH 03/12] chardev: forbid 'wait' option with client sockets
In reply to Daniel P. BerrangéQuoted 48 lines · Daniel P. Berrangé
The 'wait'/'nowait' parameter is used to tell server sockets whether to block until a client is accepted during initialization. Client chardevs have always silently ignored this option. Various tests were mistakenly passing this option for their client chardevs.
Signed-off-by Daniel P. Berrangé <[email protected]>--- chardev/char-socket.c | 12 +++++++++++- tests/ivshmem-test.c | 2 +- tests/libqtest.c | 4 ++-- tests/test-filter-redirector.c | 4 ++-- 4 files changed, 16 insertions(+), 6 deletions(-)chardev/char-socket.c +11
-1@@ -1047,6 +1047,12 @@ static bool qmp_chardev_validate_socket(ChardevSocket *sock,error_setg (errp,"%s" ,"Websocket client is not implemented" );return false; } +if (sock->has_wait) { +error_setg (errp,"%s" , +"'wait' option is incompatible with " +"socket in client connect mode" ); +return false; + } }return true;@@ -1220,7 +1226,11 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, sock->tn3270 = is_tn3270; sock->has_websocket = true; sock->websocket = is_websock; - sock->has_wait = true; +/* +* We have different default to QMP for 'wait' when 'server' +* is set, hence we can't just check for existance of 'wait' +*/ + sock->has_wait =qemu_opt_find (opts,"wait" ) || is_listen; sock->wait = is_waitconnect; sock->has_reconnect =qemu_opt_find (opts,"reconnect" ); sock->reconnect = reconnect;tests/ivshmem-test.c +0
-0
tests/ivshmem-test.c static void *server_thread(void *data) · line 299
Quoted 8 lines
tests/ivshmem-test.c +1
-1@@ -293,7 +293,7 @@ static void *server_thread(void *data)static void setup_vm_with_server (IVState *s,int nvectors,bool msi) { - char *cmd = g_strdup_printf("-chardev socket,id=chr0,path=%s,nowait " + char *cmd = g_strdup_printf("-chardev socket,id=chr0,path=%s " "-device ivshmem%s,chardev=chr0,vectors=%d", tmpserver, msi ? "-doorbell" : ",size=1M,msi=off",
I think there will be a conflict with the patch 'Remove deprecated "ivshmem" legacy device' that is currently in Michael's PULL request ... could you please rebase this patch in a day or two once that PULL has been merged?
Apart from that, patch looks fine to me.
Re: [Qemu-devel] [PATCH 02/12] chardev: forbid 'reconnect' option with server sockets
In reply to Daniel P. BerrangéQuoted 22 lines · Daniel P. Berrangé
The 'reconnect' option is used to give the sleep time, in seconds, before a client socket attempts to re-establish a connection to the server. It does not make sense to set this for server sockets, as they will always accept a new client connection immediately after the previous one went away.
Signed-off-by Daniel P. Berrangé <[email protected]>--- chardev/char-socket.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)chardev/char-socket.c +1
-1@@ -1035,7 +1035,14 @@ static bool qmp_chardev_validate_socket(ChardevSocket *sock, }/* Validate any options which have a dependancy on client vs server */ -if (!(sock->has_server && sock->server)) { +if (!sock->has_server || sock->server) {
chardev/char-socket.c static bool qmp_chardev_validate_socket(ChardevSocket *sock, · line 1048
Quoted 10 lines
chardev/char-socket.c +7
-0+if (sock->has_reconnect) { +error_setg (errp, +"'reconnect' option is incompatible with " +"socket in server listen mode" ); +return false; + } + }else {if (sock->has_websocket && sock->websocket) {error_setg (errp,"%s" ,"Websocket client is not implemented" );return false;
Re: [Qemu-devel] [PATCH 01/12] chardev: fix validation of options for QMP created chardevs
In reply to Daniel P. BerrangéQuoted 19 lines · Daniel P. Berrangé
The TLS creds option is not valid with certain address types. The user config was only checked for errors when parsing legacy QemuOpts, thus the user could pass unsupported values via QMP.
Pull all code for validating options out into a new method
qmp_chardev_validate_socket, that is called from the mainqmp_chardev_open_socketmethod. This adds a missing check for rejecting TLS creds with the vsock address type.Signed-off-by Daniel P. Berrangé <[email protected]>--- chardev/char-socket.c | 92 +++++++++++++++++++++++++++++++------------ 1 file changed, 66 insertions(+), 26 deletions(-)chardev/char-socket.c +0
-0
chardev/char-socket.c static gboolean socket_reconnect_timeout(gpointer opaque) · line 990
Quoted 4 lines
chardev/char-socket.c +1
-0@@ -987,6 +987,65 @@ static gboolean socket_reconnect_timeout(gpointer opaque)return false; } +
Please remove the additional empty line.
chardev/char-socket.c static gboolean socket_reconnect_timeout(gpointer opaque) · line 995
Quoted 5 lines
chardev/char-socket.c +5
-0+static bool qmp_chardev_validate_socket (ChardevSocket *sock, +SocketAddress *addr, +Error **errp) +{ +/* Validate any options which have a dependancy on address type */
I'd maybe rather write "dependency" which is AFAIK the more common spelling - but I'm not a native speaker, so feel free to ignore me here.
Quoted 18 lines
chardev/char-socket.c +18
-0+switch (addr->type) { +case SOCKET_ADDRESS_TYPE_FD : +if (sock->has_reconnect) { +error_setg (errp, +"'reconnect' option is incompatible with " +"'fd' address type" ); +return false; + } +if (sock->has_tls_creds && + !(sock->has_server && sock->server)) { +error_setg (errp, +"'tls_creds' option is incompatible with " +"'fd' address type as client" ); +return false; + } +break ; + +case SOCKET_ADDRESS_TYPE_UNIX :
chardev/char-socket.c static gboolean socket_reconnect_timeout(gpointer opaque) · line 1023
Quoted 10 lines
chardev/char-socket.c +10
-0+if (sock->has_tls_creds) { +error_setg (errp, +"'tls_creds' option is incompatible with " +"'unix' address type" ); +return false; + } +break ; + +case SOCKET_ADDRESS_TYPE_INET : +break ;
You could drop the empty case.
chardev/char-socket.c static gboolean socket_reconnect_timeout(gpointer opaque) · line 1034
Quoted 10 lines
chardev/char-socket.c +10
-0+case SOCKET_ADDRESS_TYPE_VSOCK : +if (sock->has_tls_creds) { +error_setg (errp, +"'tls_creds' option is incompatible with " +"'vsock' address type" ); +return false; + } + +default : +break ;
You could drop the empty default case.
Quoted 4 lines
chardev/char-socket.c +4
-0+ } + +/* Validate any options which have a dependancy on client vs server */ +if (!(sock->has_server && sock->server)) {
chardev/char-socket.c static gboolean socket_reconnect_timeout(gpointer opaque) · line 1048
Quoted 10 lines
chardev/char-socket.c +10
-0+if (sock->has_websocket && sock->websocket) { +error_setg (errp,"%s" ,"Websocket client is not implemented" ); +return false; + } + } + +return true; +} + +
No duplicated empty lines, please.
Quoted 62 lines
chardev/char-socket.c +6
-26static void qmp_chardev_open_socket(Chardev *chr, ChardevBackend *backend, bool *be_opened,@@ -1004,11 +1063,6 @@ static void qmp_chardev_open_socket(Chardev *chr,QIOChannelSocket *sioc =NULL ;SocketAddress *addr; -if (!is_listen && is_websock) { -error_setg (errp,"%s" ,"Websocket client is not implemented" ); - goto error; - } - s->is_listen = is_listen; s->is_telnet = is_telnet; s->is_tn3270 = is_tn3270;@@ -1049,10 +1103,10 @@ static void qmp_chardev_open_socket(Chardev *chr, s->addr = addr =socket_address_flatten (sock->addr); -if (sock->has_reconnect && addr->type ==SOCKET_ADDRESS_TYPE_FD ) { -error_setg (errp,"'reconnect' option is incompatible with 'fd'" ); +if (!qmp_chardev_validate_socket (sock, addr, errp)) { goto error; } +qemu_chr_set_feature (chr,QEMU_CHAR_FEATURE_RECONNECTABLE );/* TODO SOCKET_ADDRESS_FD where fd has AF_UNIX */ if (addr->type ==SOCKET_ADDRESS_TYPE_UNIX ) {@@ -1140,27 +1194,12 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,return ; } - backend->type =CHARDEV_BACKEND_KIND_SOCKET ; -if (path) { -if (tls_creds) { -error_setg (errp,"TLS can only be used over TCP socket" ); -return ; - } - }else if (host) { -if (!port) { -error_setg (errp,"chardev: socket: no port given" ); -return ; - } - }else if (fd) { -/* We don't know what host to validate against when in client mode */ -if (tls_creds && !is_listen) { -error_setg (errp,"TLS can not be used with pre-opened client FD" ); -return ; - } - }else { -g_assert_not_reached (); +if (host && !port) { +error_setg (errp,"chardev: socket: no port given" ); +return ; } + backend->type =CHARDEV_BACKEND_KIND_SOCKET ; sock = backend->u.socket.data =g_new0 (ChardevSocket,1 );qemu_chr_parse_common (opts,qapi_ChardevSocket_base (sock));
chardev/char-socket.c static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, · line 1223
Quoted 7 lines
chardev/char-socket.c +1
-0@@ -1178,6 +1217,7 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, sock->wait = is_waitconnect; sock->has_reconnect =qemu_opt_find (opts,"reconnect" ); sock->reconnect = reconnect; + sock->has_tls_creds = tls_creds; sock->tls_creds =g_strdup (tls_creds); addr =g_new0 (SocketAddressLegacy,1 );
With at least the redundant empty lines removed:
Re: [Qemu-devel] [PATCH 06/12] chardev: remove unused 'sioc' variable & cleanup paths
In reply to Daniel P. BerrangéQuoted 7 lines · Daniel P. Berrangé
The 'sioc' variable in
qmp_chardev_open_socketwas unused sincecommit
3e7d4d20d3a528b1ed10b1dc3d83119bfb0c5f24
Author: Peter Xu <[email protected]>
Date: Tue Mar 6 13:33:17 2018 +0800chardev: use chardev's gcontext for async connect
[...]
chardev/char-socket.c static void qmp_chardev_open_socket(Chardev *chr, · line 1183
Quoted 4 lines
chardev/char-socket.c +0
-4-error: -if (sioc) { -object_unref (OBJECT (sioc)); - }
So who is doing the object_unref() now in case of errors? That commit did not take care of it ... so it sounds like we could be leaving references behind in case of errors here?
Thomas
Re: [Qemu-devel] [PATCH 06/12] chardev: remove unused 'sioc' variable & cleanup paths
Quoted 9 lines · Thomas Huth
Quoted 7 lines · Daniel P. Berrangé
The 'sioc' variable in
qmp_chardev_open_socketwas unused sincecommit
3e7d4d20d3a528b1ed10b1dc3d83119bfb0c5f24
Author: Peter Xu <[email protected]>
Date: Tue Mar 6 13:33:17 2018 +0800chardev: use chardev's gcontext for async connect
[...]
Quoted 8 lines
chardev/char-socket.c static void qmp_chardev_open_socket(Chardev *chr, · line 1183
Quoted 4 lines
chardev/char-socket.c +0
-4-error: -if (sioc) { -object_unref (OBJECT (sioc)); - }So who is doing the
object_unref()now in case of errors? That commit did not take care of it ... so it sounds like we could be leaving references behind in case of errors here?
IMHO it'll be done finally in qemu_chr_socket_connected() no matter whether it's succeeded or not:
cleanup:
object_unref(OBJECT(sioc));In other words, I think the old error path is not valid even before commit 3e7d4d20d3 because IIUC when reaching the error label the sioc should never be set (and if it tries to do an object_unref here it would be a real bug).
Thanks,
Signature · 1 line
Peter Xu
Thomas Huth
Reviewed-byRe: [Qemu-devel] [PATCH 06/12] chardev: remove unused 'sioc' variable & cleanup paths
Quoted 22 lines · Peter Xu
Quoted 9 lines · Thomas Huth
Quoted 7 lines · Daniel P. Berrangé
The 'sioc' variable in
qmp_chardev_open_socketwas unused sincecommit
3e7d4d20d3a528b1ed10b1dc3d83119bfb0c5f24
Author: Peter Xu <[email protected]>
Date: Tue Mar 6 13:33:17 2018 +0800chardev: use chardev's gcontext for async connect
[...]
Quoted 8 lines
chardev/char-socket.c static void qmp_chardev_open_socket(Chardev *chr, · line 1183
Quoted 4 lines
chardev/char-socket.c +0
-4-error: -if (sioc) { -object_unref (OBJECT (sioc)); - }So who is doing the
object_unref()now in case of errors? That commit did not take care of it ... so it sounds like we could be leaving references behind in case of errors here?IMHO it'll be done finally in
qemu_chr_socket_connected()no matter whether it's succeeded or not:
Quoted 7 lines
cleanup: object_unref(OBJECT(sioc));In other words, I think the old error path is not valid even before commit
3e7d4d20d3because IIUC when reaching the error label the sioc should never be set (and if it tries to do anobject_unrefhere it would be a real bug).
Right, looking at the qmp_chardev_open_socket() function again (before the rework in 3e7d4d20d3a5), I see now that all locations that use "goto error" should have sioc = NULL. So this patch here is fine:
Re: [Qemu-devel] [PATCH 01/12] chardev: fix validation of options for QMP created chardevs
In reply to Thomas HuthQuoted 23 lines · Thomas Huth
Quoted 22 lines · Daniel P. Berrangé
The TLS creds option is not valid with certain address types. The user config was only checked for errors when parsing legacy QemuOpts, thus the user could pass unsupported values via QMP.
Pull all code for validating options out into a new method
qmp_chardev_validate_socket, that is called from the mainqmp_chardev_open_socketmethod. This adds a missing check for rejecting TLS creds with the vsock address type.Signed-off-by Daniel P. Berrangé <[email protected]>--- chardev/char-socket.c | 92 +++++++++++++++++++++++++++++++------------ 1 file changed, 66 insertions(+), 26 deletions(-)chardev/char-socket.c +0
-0@@ -987,6 +987,65 @@ static gboolean socket_reconnect_timeout(gpointer opaque)return false; }
Quoted 3 lines
chardev/char-socket.c static gboolean socket_reconnect_timeout(gpointer opaque) · line 990
Quoted 1 line
chardev/char-socket.c +1
-0+Please remove the additional empty line.
Having two blanks lines between functions is intentional to give visual separation.
Quoted 31 lines
chardev/char-socket.c static gboolean socket_reconnect_timeout(gpointer opaque) · line 995
Quoted 5 lines
chardev/char-socket.c +5
-0+static bool qmp_chardev_validate_socket (ChardevSocket *sock, +SocketAddress *addr, +Error **errp) +{ +/* Validate any options which have a dependancy on address type */ I'd maybe rather write "dependency" which is AFAIK the more common spelling - but I'm not a native speaker, so feel free to ignore me here.
Quoted 22 lines
chardev/char-socket.c +22
-0+switch (addr->type) { +case SOCKET_ADDRESS_TYPE_FD : +if (sock->has_reconnect) { +error_setg (errp, +"'reconnect' option is incompatible with " +"'fd' address type" ); +return false; + } +if (sock->has_tls_creds && + !(sock->has_server && sock->server)) { +error_setg (errp, +"'tls_creds' option is incompatible with " +"'fd' address type as client" ); +return false; + } +break ; + +case SOCKET_ADDRESS_TYPE_UNIX : +if (sock->has_tls_creds) { +error_setg (errp, +"'tls_creds' option is incompatible with " +"'unix' address type" );
Quoted 8 lines
chardev/char-socket.c static gboolean socket_reconnect_timeout(gpointer opaque) · line 1023
Quoted 6 lines
chardev/char-socket.c +6
-0+return false; + } +break ; + +case SOCKET_ADDRESS_TYPE_INET : +break ;You could drop the empty case.
I preferred to explicitly list all cases, so it is clear what needs to be handled here when further checks are added later.
Quoted 9 lines
chardev/char-socket.c static gboolean socket_reconnect_timeout(gpointer opaque) · line 1032
Quoted 9 lines
chardev/char-socket.c +8
-0+case SOCKET_ADDRESS_TYPE_VSOCK : +if (sock->has_tls_creds) { +error_setg (errp, +"'tls_creds' option is incompatible with " +"'vsock' address type" ); +return false; + } +
Opps, missing default.
Quoted 4 lines
chardev/char-socket.c static gboolean socket_reconnect_timeout(gpointer opaque) · line 1034
Quoted 2 lines
chardev/char-socket.c +2
-0+default : +break ;You could drop the empty default case.
If that is not there, then the compiler forces the listing of SOCKET_ADDRESS_TYPE__MAX instead due to -Wswitch
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 :|
Re: [Qemu-devel] [PATCH 04/12] chardev: remove many local variables in qemu_chr_parse_socket
In reply to Eric BlakeQuoted 34 lines · Eric Blake
[adding Markus in relation to a QemuOpts observation]
Quoted 11 lines · Daniel P. Berrangé
Now that all validation is separated off into a separate method, we can directly populate the ChardevSocket struct from the QemuOpts values, avoiding many local variables.
Signed-off-by Daniel P. Berrangé <[email protected]>--- chardev/char-socket.c | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-)chardev/char-socket.c static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, · line 1212
Quoted 7 lines
chardev/char-socket.c +2
-2@@ -1216,26 +1208,30 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, sock = backend->u.socket.data =g_new0 (ChardevSocket,1 );qemu_chr_parse_common (opts,qapi_ChardevSocket_base (sock)); - sock->has_nodelay = true; - sock->nodelay = do_nodelay; + sock->has_nodelay =qemu_opt_get (opts,"delay" ); + sock->nodelay = !qemu_opt_get_bool (opts,"delay" , true);Unrelated to this patch, but my recent proposal to make QemuOpts be a bit more typesafe:
https://lists.gnu.org/archive/html/qemu-devel/2019-01/msg02042.html
does not like users calling
qemu_opt_get()of an option that has an integer default (as opposed to a string default), even if the only reason the caller was doing it was to learn if the option is present. I wonder if we should introducebool
qemu_opt_has(QemuOpts *opts, const char *name)
Quoted 7 lines
and then replace existing callers which merely call
qemu_opt_get()to learn whether an optional option was present/defaulted but without caring about its string value. I also wonder if Coccinelle can be employed to determine which callers fit that bill (that is, detect when a const char * result is ignored or solely used in a bool context, vs. when it is actually used as a string parameter to another function and/or saved in a const char * variable).
Yes, if you're going to enforce type safety on qemu_opt_get, then then we definitely need to have qemu_opt_has() function to check for existance, as this is needed in other places too.
Quoted 29 lines
Now, on to actual review,
chardev/char-socket.c static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, · line 1215
Quoted 3 lines
chardev/char-socket.c +3
-0+/* +* We have different default to QMP for 'server', hence +* we can't just check for existance of 'server' s/existance/existence/
Quoted 9 lines
chardev/char-socket.c +1
-7+*/ sock->has_server = true; - sock->server = is_listen; - sock->has_telnet = true; - sock->telnet = is_telnet; - sock->has_tn3270 = true; - sock->tn3270 = is_tn3270; - sock->has_websocket = true; - sock->websocket = is_websock;chardev/char-socket.c static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, · line 1227
Quoted 10 lines
chardev/char-socket.c +7
-0+ sock->server =qemu_opt_get_bool (opts,"server" , false); + sock->has_telnet =qemu_opt_get (opts,"telnet" ); + sock->telnet =qemu_opt_get_bool (opts,"telnet" , false); + sock->has_tn3270 =qemu_opt_get (opts,"tn3270" ); + sock->tn3270 =qemu_opt_get_bool (opts,"tn3270" , false); + sock->has_websocket =qemu_opt_get (opts,"websocket" ); + sock->websocket =qemu_opt_get_bool (opts,"websocket" , false);/* * We have different default to QMP for 'wait' when 'server' * is set, hence we can't just check for existance of 'wait'
Quoted 1 line
but then again, you were copy-pasting an existing typo.
Both typos were from this series
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 :|
Re: [Qemu-devel] [PATCH 04/12] chardev: remove many local variables in qemu_chr_parse_socket
In reply to Marc-André LureauQuoted 75 lines · Marc-André Lureau
Hi
Quoted 58 lines · Daniel P. Berrangé
Now that all validation is separated off into a separate method, we can directly populate the ChardevSocket struct from the QemuOpts values, avoiding many local variables.
Signed-off-by Daniel P. Berrangé <[email protected]>--- chardev/char-socket.c | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-)chardev/char-socket.c +11
-17@@ -1186,18 +1186,10 @@ error:static void qemu_chr_parse_socket (QemuOpts *opts,ChardevBackend *backend,Error **errp) { -bool is_listen =qemu_opt_get_bool (opts,"server" , false); -bool is_waitconnect = is_listen &&qemu_opt_get_bool (opts,"wait" , true); -bool is_telnet =qemu_opt_get_bool (opts,"telnet" , false); -bool is_tn3270 =qemu_opt_get_bool (opts,"tn3270" , false); -bool is_websock =qemu_opt_get_bool (opts,"websocket" , false); -bool do_nodelay = !qemu_opt_get_bool (opts,"delay" , true); -int64_t reconnect =qemu_opt_get_number (opts,"reconnect" ,0 );const char *path =qemu_opt_get (opts,"path" );const char *host =qemu_opt_get (opts,"host" );const char *port =qemu_opt_get (opts,"port" );const char *fd =qemu_opt_get (opts,"fd" ); -const char *tls_creds =qemu_opt_get (opts,"tls-creds" );SocketAddressLegacy *addr;ChardevSocket *sock;@@ -1216,26 +1208,30 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, sock = backend->u.socket.data =g_new0 (ChardevSocket,1 );qemu_chr_parse_common (opts,qapi_ChardevSocket_base (sock)); - sock->has_nodelay = true; - sock->nodelay = do_nodelay; + sock->has_nodelay =qemu_opt_get (opts,"delay" ); + sock->nodelay = !qemu_opt_get_bool (opts,"delay" , true); +/* +* We have different default to QMP for 'server', hence +* we can't just check for existance of 'server' +*/ sock->has_server = true; - sock->server = is_listen; - sock->has_telnet = true; - sock->telnet = is_telnet; - sock->has_tn3270 = true; - sock->tn3270 = is_tn3270; - sock->has_websocket = true; - sock->websocket = is_websock; + sock->server =qemu_opt_get_bool (opts,"server" , false); + sock->has_telnet =qemu_opt_get (opts,"telnet" ); + sock->telnet =qemu_opt_get_bool (opts,"telnet" , false); + sock->has_tn3270 =qemu_opt_get (opts,"tn3270" ); + sock->tn3270 =qemu_opt_get_bool (opts,"tn3270" , false);chardev/char-socket.c static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, · line 1230
Quoted 10 lines
chardev/char-socket.c +4
-2+ sock->has_websocket =qemu_opt_get (opts,"websocket" ); + sock->websocket =qemu_opt_get_bool (opts,"websocket" , false);/* * We have different default to QMP for 'wait' when 'server' * is set, hence we can't just check for existance of 'wait' */ - sock->has_wait =qemu_opt_find (opts,"wait" ) || is_listen; - sock->wait = is_waitconnect; + sock->has_wait =qemu_opt_find (opts,"wait" ) || sock->server; + sock->wait =qemu_opt_get_bool (opts,"wait" , true);That changes slightly the behaviour, since wait was not parsed before when !is_listen.
Quoted 1 line
Since we are "correcting" (breaking?) things anyway in previous patches too:
Yes, in the !is_listen case, the previous patch will see has_wait == true and raise an error. So this patch is not a change in behaviour wrt the previous patch, but the series as a whole is a change in behaviour due to reporting errors for bad "wait" usage in clients.
Quoted 12 lines
Reviewed-by Marc-André Lureau <[email protected]>Quoted 10 lines
chardev/char-socket.c +3
-3sock->has_reconnect =qemu_opt_find (opts,"reconnect" ); - sock->reconnect = reconnect; - sock->has_tls_creds = tls_creds; - sock->tls_creds =g_strdup (tls_creds); + sock->reconnect =qemu_opt_get_number (opts,"reconnect" ,0 ); + sock->has_tls_creds =qemu_opt_get (opts,"tls-creds" ); + sock->tls_creds =g_strdup (qemu_opt_get (opts,"tls-creds" )); addr =g_new0 (SocketAddressLegacy,1 );if (path) {
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 :|
Re: [Qemu-devel] [PATCH 06/12] chardev: remove unused 'sioc' variable & cleanup paths
In reply to Thomas HuthQuoted 9 lines · Thomas Huth
Quoted 7 lines · Daniel P. Berrangé
The 'sioc' variable in
qmp_chardev_open_socketwas unused sincecommit
3e7d4d20d3a528b1ed10b1dc3d83119bfb0c5f24
Author: Peter Xu <[email protected]>
Date: Tue Mar 6 13:33:17 2018 +0800chardev: use chardev's gcontext for async connect
[...]
Quoted 8 lines
chardev/char-socket.c static void qmp_chardev_open_socket(Chardev *chr, · line 1183
Quoted 4 lines
chardev/char-socket.c +0
-4-error: -if (sioc) { -object_unref (OBJECT (sioc)); - }So who is doing the
object_unref()now in case of errors? That commit did not take care of it ... so it sounds like we could be leaving references behind in case of errors here?
As shown in the patch diff chunks, no code is ever setting "sioc" in this method, so it can't ever be non-NULL
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 :|
Re: [Qemu-devel] [PATCH 07/12] chardev: split tcp_chr_wait_connected into two methods
In reply to Marc-André LureauQuoted 117 lines · Marc-André Lureau
Hi
Quoted 114 lines · Daniel P. Berrangé
The
tcp_chr_wait_connectedmethod can deal with either server or client chardevs, but some callers only care about one of these possibilities. Thetcp_chr_wait_connectedmethod will also need some refactoring to reliably deal with its primary goal of allowing a device frontend to wait for an established connection, which will interfere with other callers.Split it into two methods, one responsible for server initiated connections, the other responsible for client initiated connections. In doing this split the
tcp_char_connect_async()method is renamed to become consistent with naming of the new methods.Signed-off-by Daniel P. Berrangé <[email protected]>--- chardev/char-socket.c | 59 +++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 22 deletions(-)chardev/char-socket.c +33
-16@@ -886,30 +886,47 @@ static void tcp_chr_accept(QIONetListener *listener,tcp_chr_new_client (chr, cioc); } -static int tcp_chr_wait_connected (Chardev *chr,Error **errp) + +static int tcp_chr_connect_client_sync (Chardev *chr,Error **errp) +{ +SocketChardev *s =SOCKET_CHARDEV (chr); +QIOChannelSocket *sioc =qio_channel_socket_new (); +tcp_chr_set_client_ioc_name (chr, sioc); +if (qio_channel_socket_connect_sync (sioc, s->addr, errp) <0 ) { +object_unref (OBJECT (sioc)); +return -1 ; + } +tcp_chr_new_client (chr, sioc); +object_unref (OBJECT (sioc)); +return 0 ; +} + + +static void tcp_chr_accept_server_sync (Chardev *chr) {SocketChardev *s =SOCKET_CHARDEV (chr);QIOChannelSocket *sioc; +info_report ("QEMU waiting for connection on: %s" , + chr->filename); + sioc =qio_net_listener_wait_client (s->listener); +tcp_chr_set_client_ioc_name (chr, sioc); +tcp_chr_new_client (chr, sioc); +object_unref (OBJECT (sioc)); +} + +static int tcp_chr_wait_connected (Chardev *chr,Error **errp) +{ +SocketChardev *s =SOCKET_CHARDEV (chr);/* It can't wait on s->connected, since it is set asynchronously * in TLS and telnet cases, only wait for an accepted socket */ while (!s->ioc) {if (s->is_listen) { -info_report ("QEMU waiting for connection on: %s" , - chr->filename); - sioc =qio_net_listener_wait_client (s->listener); -tcp_chr_set_client_ioc_name (chr, sioc); -tcp_chr_new_client (chr, sioc); -object_unref (OBJECT (sioc)); +tcp_chr_accept_server_sync (chr); }else { - sioc =qio_channel_socket_new (); -tcp_chr_set_client_ioc_name (chr, sioc); -if (qio_channel_socket_connect_sync (sioc, s->addr, errp) <0 ) { -object_unref (OBJECT (sioc)); +if (tcp_chr_connect_client_sync (chr, errp) <0 ) {return -1 ; } -tcp_chr_new_client (chr, sioc); -object_unref (OBJECT (sioc)); } }@@ -958,7 +975,7 @@ cleanup:object_unref (OBJECT (sioc)); } -static void tcp_chr_connect_async (Chardev *chr) +static void tcp_chr_connect_client_async (Chardev *chr) {SocketChardev *s =SOCKET_CHARDEV (chr);QIOChannelSocket *sioc;@@ -982,7 +999,7 @@ static gboolean socket_reconnect_timeout(gpointer opaque)return false; } -tcp_chr_connect_async (chr); +tcp_chr_connect_client_async (chr);return false; }@@ -1139,7 +1156,7 @@ static void qmp_chardev_open_socket(Chardev *chr, }if (s->reconnect_time) { -tcp_chr_connect_async (chr); +tcp_chr_connect_client_async (chr); }else {if (s->is_listen) {char *name;@@ -1159,17 +1176,15 @@ static void qmp_chardev_open_socket(Chardev *chr, s->addr =socket_local_address (s->listener->sioc[0 ]->fd, errp);update_disconnected_filename (s);
Quoted 10 lines
chardev/char-socket.c static void qmp_chardev_open_socket(Chardev *chr, · line 1180
Quoted 7 lines
chardev/char-socket.c +2
-5-if (is_waitconnect && -qemu_chr_wait_connected (chr, errp) <0 ) { -return ; - } -if (!s->ioc) { +if (is_waitconnect) { +tcp_chr_accept_server_sync (chr);Is the
wait_connected()loop really unnecessary there? looks like it is. And there is no error path either. ok
Yes, we only need to accept a single client during normal startup.
Even the loop in tcp_chr_wait_connected is actually bogus in the current code, but I've not removed that since later patches will need it again.
Quoted 16 lines
Reviewed-by Marc-André Lureau <[email protected]>Quoted 14 lines
chardev/char-socket.c +2
-2+ }else {qio_net_listener_set_client_func_full (s->listener, tcp_chr_accept, chr,NULL , chr->gcontext); } - }else if (qemu_chr_wait_connected (chr, errp) <0 ) { + }else if (tcp_chr_connect_client_sync (chr, errp) <0 ) {return ; } } --2.20.1
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 :|
Re: [Qemu-devel] [PATCH 11/12] chardev: disallow TLS/telnet/websocket with tcp_chr_wait_connected
In reply to Marc-André LureauQuoted 59 lines · Marc-André Lureau
Quoted 28 lines · Daniel P. Berrangé
In the previous commit
commit
1dc8a6695c731abb7461c637b2512c3670d82be4
Author: Marc-André Lureau <[email protected]>
Date: Tue Aug 16 12:33:32 2016 +0400char: fix waiting for TLS and telnet connection
the
tcp_chr_wait_connected()method was changed to check for a non-NULL 's->ioc' as a sign that there is already a connection present, as opposed to checking the "connected" flag to supposedly fix handling of TLS/telnet connections.The original code would repeatedly call
tcp_chr_wait_connectedcreating many connections as 'connected' would never become true. The changed code would still repeatedly calltcp_chr_wait_connectedbusy waiting becauses->iocis set but the chardev will never seeCHR_EVENT_OPENED. IOW, the code is still broken with TLS/telnet, but in a different way.Checking for a non-NULL '
s->ioc' does not mean that aCHR_EVENT_OPENEDwill be ready for a TLS/telnet connection. These protocols (and the websocket protocol) all require the main loop to be running in order to complete the protocol handshake before emittingCHR_EVENT_OPENED. Thetcp_chr_wait_connected()method is only used during early startup before a main loop is running, so TLS/telnet/websock connections can never complete initialization.Quoted 8 lines
Making this work would require changing
tcp_chr_wait_connectedto run a main loop. This is quite complex since we must not allow GSource's that other parts of QEMU have registered to run yet. The current callers oftcp_chr_wait_connecteddo not require use of the TLS/telnet/websocket protocols, so the simplest option is to just forbid this combination completely for now.Signed-off-by Daniel P. Berrangé <[email protected]>Reviewed-by Marc-André Lureau <[email protected]>Quoted 19 lines
--- chardev/char-socket.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)chardev/char-socket.c +5
-2@@ -951,8 +951,20 @@ static void tcp_chr_accept_server_sync(Chardev *chr)static int tcp_chr_wait_connected (Chardev *chr,Error **errp) {SocketChardev *s =SOCKET_CHARDEV (chr); -/* It can't wait on s->connected, since it is set asynchronously -* in TLS and telnet cases, only wait for an accepted socket */ +const char *opts[] = {"telnet" ,"tn3270" ,"websock" ,"tls-creds" }; +bool optset[] = { s->is_telnet, s->is_tn3270, s->is_websock, s->tls_creds }; +size_t i; + +QEMU_BUILD_BUG_ON (G_N_ELEMENTS (opts) !=G_N_ELEMENTS (optset));
Quoted 8 lines
chardev/char-socket.c static void tcp_chr_accept_server_sync(Chardev *chr) · line 963
Quoted 5 lines
chardev/char-socket.c +5
-0+for (i =0 ; i <G_N_ELEMENTS (opts); i++) { +if (optset[i]) { +error_setg (errp, +"'%s' option is incompatible with waiting for " +"connection during early startup" , opts[i]);"during early startup" ? I think you could also reach this by using chardev-add &
netdev_add.
Yes, I didn't realize that at first. It also means that patch 12 is in fact still broken in the hotplug case.
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 :|
Re: [Qemu-devel] [PATCH 01/12] chardev: fix validation of options for QMP created chardevs
In reply to Daniel P. BerrangéEric, there's a QAPI code generation idea at the end.
Quoted 24 lines · Daniel P. Berrangé
Quoted 23 lines · Thomas Huth
Quoted 22 lines · Daniel P. Berrangé
The TLS creds option is not valid with certain address types. The user config was only checked for errors when parsing legacy QemuOpts, thus the user could pass unsupported values via QMP.
Pull all code for validating options out into a new method
qmp_chardev_validate_socket, that is called from the mainqmp_chardev_open_socketmethod. This adds a missing check for rejecting TLS creds with the vsock address type.Signed-off-by Daniel P. Berrangé <[email protected]>--- chardev/char-socket.c | 92 +++++++++++++++++++++++++++++++------------ 1 file changed, 66 insertions(+), 26 deletions(-)chardev/char-socket.c +0
-0@@ -987,6 +987,65 @@ static gboolean socket_reconnect_timeout(gpointer opaque)return false; }
Quoted 15 lines
Quoted 3 lines
chardev/char-socket.c static gboolean socket_reconnect_timeout(gpointer opaque) · line 990
Quoted 1 line
chardev/char-socket.c +1
-0+Please remove the additional empty line.
Having two blanks lines between functions is intentional to give visual separation.
Quoted 8 lines
chardev/char-socket.c static gboolean socket_reconnect_timeout(gpointer opaque) · line 995
Quoted 5 lines
chardev/char-socket.c +5
-0+static bool qmp_chardev_validate_socket (ChardevSocket *sock, +SocketAddress *addr, +Error **errp) +{ +/* Validate any options which have a dependancy on address type */ I'd maybe rather write "dependency" which is AFAIK the more common spelling - but I'm not a native speaker, so feel free to ignore me here.
For what it's worth, my dictionary wants dependency.
Quoted 31 lines
Quoted 22 lines
Quoted 22 lines
chardev/char-socket.c +22
-0+switch (addr->type) { +case SOCKET_ADDRESS_TYPE_FD : +if (sock->has_reconnect) { +error_setg (errp, +"'reconnect' option is incompatible with " +"'fd' address type" ); +return false; + } +if (sock->has_tls_creds && + !(sock->has_server && sock->server)) { +error_setg (errp, +"'tls_creds' option is incompatible with " +"'fd' address type as client" ); +return false; + } +break ; + +case SOCKET_ADDRESS_TYPE_UNIX : +if (sock->has_tls_creds) { +error_setg (errp, +"'tls_creds' option is incompatible with " +"'unix' address type" );Quoted 8 lines
chardev/char-socket.c static gboolean socket_reconnect_timeout(gpointer opaque) · line 1023
Quoted 6 lines
chardev/char-socket.c +6
-0+return false; + } +break ; + +case SOCKET_ADDRESS_TYPE_INET : +break ;You could drop the empty case.
Quoted 2 lines
I preferred to explicitly list all cases, so it is clear what needs to be handled here when further checks are added later.
Matter of taste, your choice unless maintainer overrules.
Quoted 11 lines
Quoted 9 lines
chardev/char-socket.c static gboolean socket_reconnect_timeout(gpointer opaque) · line 1032
Quoted 9 lines
chardev/char-socket.c +8
-0+case SOCKET_ADDRESS_TYPE_VSOCK : +if (sock->has_tls_creds) { +error_setg (errp, +"'tls_creds' option is incompatible with " +"'vsock' address type" ); +return false; + } +Opps, missing default.
I guess you mean break.
Quoted 8 lines
Quoted 4 lines
chardev/char-socket.c static gboolean socket_reconnect_timeout(gpointer opaque) · line 1034
Quoted 2 lines
chardev/char-socket.c +2
-0+default : +break ;You could drop the empty default case.
If that is not there, then the compiler forces the listing of
SOCKET_ADDRESS_TYPE__MAXinstead due to -Wswitch
I wonder whether generating something like
typedef enum SocketAddressType {
SOCKET_ADDRESS_TYPE_INET,
SOCKET_ADDRESS_TYPE_UNIX,
SOCKET_ADDRESS_TYPE_VSOCK,
SOCKET_ADDRESS_TYPE_FD,
} SocketAddressType;
#define SOCKET_ADDRESS_TYPE__MAX (SOCKET_ADDRESS_TYPE_FD + 1)would be better.
Re: [Qemu-devel] [PATCH 01/12] chardev: fix validation of options for QMP created chardevs
Quoted 18 lines · Markus Armbruster
Eric, there's a QAPI code generation idea at the end.
Quoted 3 lines
If that is not there, then the compiler forces the listing of
SOCKET_ADDRESS_TYPE__MAXinstead due to -WswitchI wonder whether generating something like
typedef enum SocketAddressType { SOCKET_ADDRESS_TYPE_INET, SOCKET_ADDRESS_TYPE_UNIX, SOCKET_ADDRESS_TYPE_VSOCK, SOCKET_ADDRESS_TYPE_FD, } SocketAddressType; #define SOCKET_ADDRESS_TYPE__MAX (SOCKET_ADDRESS_TYPE_FD + 1)
Quoted 1 line
would be better.
Or even an anon enum instead of a define:
typedef enum SocketAddressType {
SOCKET_ADDRESS_TYPE_INET, SOCKET_ADDRESS_TYPE_UNIX, SOCKET_ADDRESS_TYPE_VSOCK, SOCKET_ADDRESS_TYPE_FD,
} SocketAddressType;
enum {
SOCKET_ADDRESS_TYPE__MAX = SOCKET_ADDRESS_TYPE_FD + 1
};
The idea is interesting; since it is generated code, the change is fairly easy to make. I don't know how much fall-out we'd get in the rest of the code base - only one way to find out.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
Patchew URL: https://patchew.org/QEMU/[email protected]/
Patchew URL: https://patchew.org/QEMU/[email protected]/
Hi,
This series failed the docker-mingw@fedora build test. Please find the testing commands and their output below. If you have Docker installed, you can probably reproduce it locally.
=== TEST SCRIPT BEGIN ===
#!/bin/bash
time make docker-test-mingw@fedora SHOW_ENV=1 J=14 === TEST SCRIPT END ===
CC hw/char/debugcon.o
In function 'acpi_table_install',
inlined from 'acpi_table_add' at /tmp/qemu-test/src/hw/acpi/core.c:296:5:
/tmp/qemu-test/src/hw/acpi/core.c:184:9: error: 'strncpy' specified bound 4 equals destination size [-Werror=stringop-truncation]
strncpy(ext_hdr->sig, hdrs->sig, sizeof ext_hdr->sig);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/tmp/qemu-test/src/hw/acpi/core.c:203:9: error: 'strncpy' specified bound 6 equals destination size [-Werror=stringop-truncation]
strncpy(ext_hdr->oem_id, hdrs->oem_id, sizeof ext_hdr->oem_id);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/tmp/qemu-test/src/hw/acpi/core.c:207:9: error: 'strncpy' specified bound 8 equals destination size [-Werror=stringop-truncation]
strncpy(ext_hdr->oem_table_id, hdrs->oem_table_id,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sizeof ext_hdr->oem_table_id);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/tmp/qemu-test/src/hw/acpi/core.c:216:9: error: 'strncpy' specified bound 4 equals destination size [-Werror=stringop-truncation]
strncpy(ext_hdr->asl_compiler_id, hdrs->asl_compiler_id,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sizeof ext_hdr->asl_compiler_id);---
make: *** [/tmp/qemu-test/src/rules.mak:69: hw/acpi/core.o] Error 1
make: *** Waiting for unfinished jobs..../tmp/qemu-test/src/block/sheepdog.c: In function 'find_vdi_name':/tmp/qemu-test/src/block/sheepdog.c:1239:5: error: 'strncpy' specified bound 256 equals destination size [-Werror=stringop-truncation]
strncpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~cc1: all warnings being treated as errors
make: *** [/tmp/qemu-test/src/rules.mak:69: block/sheepdog.o] Error 1/tmp/qemu-test/src/hw/acpi/aml-build.c: In function 'build_header':/tmp/qemu-test/src/hw/acpi/aml-build.c:1535:9: error: 'strncpy' specified bound 6 equals destination size [-Werror=stringop-truncation]
strncpy((char *)h->oem_id, oem_id, sizeof h->oem_id);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/tmp/qemu-test/src/hw/acpi/aml-build.c:1541:9: error: 'strncpy' specified bound 8 equals destination size [-Werror=stringop-truncation]
strncpy((char *)h->oem_table_id, oem_table_id, sizeof(h->oem_table_id));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~cc1: all warnings being treated as errors
The full log is available at
http://patchew.org/logs/[email protected]/testing.docker-mingw@fedora/?type=message.
---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to [email protected]