Skip to contentSkip to conversation

chardev: qio related non-default context support

PATCH In review Peter Xu 5 participants 31 messages Mar 6, 2018 – Aug 17, 2018

lore
Share & tools
lore

Peter Xu

author
#0 lore raw

v2 cover chardev: qio related non-default context support

Based-on: <[email protected]>

This series is based on the QIO part:
[PATCH v3 0/6] qio: general non-default GMainContext support

v2:
- fix the reported problem by patchew in patch 5
- added some r-bs from Marc-Andre

Please review, thanks.

Peter Xu (9):
vl: export machine_init_done
chardev: fix leak in tcp_chr_telnet_init_io()
chardev: update net listener gcontext
chardev: allow telnet gsource to switch gcontext
chardev: introduce chr_machine_done hook
chardev: use chardev's gcontext for async connect
chardev: tcp: postpone async connection setup
chardev: tcp: let TLS run on chardev context
chardev: tcp: postpone TLS work until machine done

 chardev/char-mux.c         |  33 ++++++++--
 chardev/char-socket.c      | 153 ++++++++++++++++++++++++++++++++++-----------
 chardev/char.c             |  43 +++++--------
 include/chardev/char-mux.h |   2 -
 include/chardev/char.h     |   2 +
 include/sysemu/sysemu.h    |   2 +
 stubs/machine-init-done.c  |   2 +
 tests/test-char.c          |   1 -
 vl.c                       |   4 +-
 9 files changed, 169 insertions(+), 73 deletions(-)
Signature · 1 line
2.14.3
author
#1 lore raw

PATCH v2 1/9 vl: export machine_init_done

We have that variable but not exported. Export that so modules can have a way to poke on whether machine init has finished.

Meanwhile, set that up even before calling the notifiers, so that notifiers who may depend on this field will get a correct answer.

---
 include/sysemu/sysemu.h   | 2 ++
 stubs/machine-init-done.c | 2 ++
 vl.c                      | 4 ++--
 3 files changed, 6 insertions(+), 2 deletions(-)
include/sysemu/sysemu.h +2 -0
index 77bb3da582..3f0f35610b 100644
@@ -86,6 +86,8 @@ void qemu_system_guest_panicked(GuestPanicInformation *info);
 void qemu_add_exit_notifier(Notifier *notify);
 void qemu_remove_exit_notifier(Notifier *notify);
 
+extern bool machine_init_done;
+
 void qemu_add_machine_init_done_notifier(Notifier *notify);
 void qemu_remove_machine_init_done_notifier(Notifier *notify);
 
stubs/machine-init-done.c +2 -0
index 9a0d62514f..4121f1709b 100644
@@ -2,6 +2,8 @@
 #include "qemu-common.h"
 #include "sysemu/sysemu.h"
 
+bool machine_init_done = true;
+
 void qemu_add_machine_init_done_notifier(Notifier *notify)
 {
 }
vl.c +2 -2
index a33ac008fb..57777e8d47 100644
@@ -2712,7 +2712,7 @@ static void qemu_run_exit_notifiers(void)
     notifier_list_notify(&exit_notifiers, NULL);
 }
 
-static bool machine_init_done;
+bool machine_init_done;
 
 void qemu_add_machine_init_done_notifier(Notifier *notify)
 {
@@ -2729,8 +2729,8 @@ void qemu_remove_machine_init_done_notifier(Notifier *notify)
 
 static void qemu_run_machine_init_done_notifiers(void)
 {
-    notifier_list_notify(&machine_init_done_notifiers, NULL);
     machine_init_done = true;
+    notifier_list_notify(&machine_init_done_notifiers, NULL);
 }
 
 static const QEMUOption *lookup_opt(int argc, char **argv,
Signature · 1 line
2.14.3
Reviewed-by on v2 1/9
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/9] vl: export machine_init_done

Quoted 6 lines · Peter Xu

We have that variable but not exported. Export that so modules can have a way to poke on whether machine init has finished.

Meanwhile, set that up even before calling the notifiers, so that notifiers who may depend on this field will get a correct answer.

Quoted 8 lines
---
 include/sysemu/sysemu.h   | 2 ++
 stubs/machine-init-done.c | 2 ++
 vl.c                      | 4 ++--
 3 files changed, 6 insertions(+), 2 deletions(-)

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 :|
author
#3 lore raw

PATCH v2 2/9 chardev: fix leak in tcp_chr_telnet_init_io()

In reply to Peter Xu

Need to free TCPChardevTelnetInit when session established.

Since at it, switch to use G_SOURCE_* macros.

---
 chardev/char-socket.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
chardev/char-socket.c +7 -3
index 113feaf948..205ee377a4 100644
@@ -591,19 +591,23 @@ static gboolean tcp_chr_telnet_init_io(QIOChannel *ioc,
             ret = 0;
         } else {
             tcp_chr_disconnect(init->chr);
-            return FALSE;
+            goto end;
         }
     }
     init->buflen -= ret;
 
     if (init->buflen == 0) {
         tcp_chr_connect(init->chr);
-        return FALSE;
+        goto end;
     }
 
     memmove(init->buf, init->buf + ret, init->buflen);
 
-    return TRUE;
+    return G_SOURCE_CONTINUE;
+
+end:
+    g_free(init);
+    return G_SOURCE_REMOVE;
 }
 
 static void tcp_chr_telnet_init(Chardev *chr)
Signature · 1 line
2.14.3
author
#4 lore raw

PATCH v2 3/9 chardev: update net listener gcontext

In reply to Peter Xu

TCP chardevs can be using QIO network listeners working in the background when in listening mode. However the network listeners are always running in main context. This can race with chardevs that are running in non-main contexts.

To solve this, we need to re-setup the net listeners in tcp_chr_update_read_handler() with the newly cached gcontext.

---
 chardev/char-socket.c | 26 ++++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)
chardev/char-socket.c +20 -6
index 205ee377a4..5aa01e15ff 100644
@@ -422,8 +422,8 @@ static void tcp_chr_disconnect(Chardev *chr)
     tcp_chr_free_connection(chr);
 
     if (s->listener) {
-        qio_net_listener_set_client_func(s->listener, tcp_chr_accept,
-                                         chr, NULL);
+        qio_net_listener_set_client_func_full(s->listener, tcp_chr_accept,
+                                              chr, NULL, chr->gcontext);
     }
     update_disconnected_filename(s);
     if (emit_close) {
@@ -559,6 +559,16 @@ static void tcp_chr_update_read_handler(Chardev *chr)
 {
     SocketChardev *s = SOCKET_CHARDEV(chr);
 
+    if (s->listener) {
+        /*
+         * It's possible that chardev context is changed in
+         * qemu_chr_be_update_read_handlers().  Reset it for QIO net
+         * listener if there is.
+         */
+        qio_net_listener_set_client_func_full(s->listener, tcp_chr_accept,
+                                              chr, NULL, chr->gcontext);
+    }
+
     if (!s->connected) {
         return;
     }
@@ -743,7 +753,8 @@ static int tcp_chr_new_client(Chardev *chr, QIOChannelSocket *sioc)
         qio_channel_set_delay(s->ioc, false);
     }
     if (s->listener) {
-        qio_net_listener_set_client_func(s->listener, NULL, NULL, NULL);
+        qio_net_listener_set_client_func_full(s->listener, NULL, NULL,
+                                              NULL, chr->gcontext);
     }
 
     if (s->tls_creds) {
@@ -824,7 +835,8 @@ static void char_socket_finalize(Object *obj)
     tcp_chr_reconn_timer_cancel(s);
     qapi_free_SocketAddress(s->addr);
     if (s->listener) {
-        qio_net_listener_set_client_func(s->listener, NULL, NULL, NULL);
+        qio_net_listener_set_client_func_full(s->listener, NULL, NULL,
+                                              NULL, chr->gcontext);
         object_unref(OBJECT(s->listener));
     }
     if (s->tls_creds) {
@@ -980,8 +992,10 @@ static void qmp_chardev_open_socket(Chardev *chr,
                 return;
             }
             if (!s->ioc) {
-                qio_net_listener_set_client_func(s->listener, tcp_chr_accept,
-                                                 chr, NULL);
+                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) {
             goto error;
Signature · 1 line
2.14.3
Reviewed-by on v2 3/9
#5 lore raw

Re: [Qemu-devel] [PATCH v2 3/9] chardev: update net listener gcontext

Quoted 5 lines · Peter Xu

TCP chardevs can be using QIO network listeners working in the background when in listening mode. However the network listeners are always running in main context. This can race with chardevs that are running in non-main contexts.

Quoted 8 lines

To solve this, we need to re-setup the net listeners in tcp_chr_update_read_handler() with the newly cached gcontext.

---
 chardev/char-socket.c | 26 ++++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)

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 :|
author
#6 lore raw

PATCH v2 4/9 chardev: allow telnet gsource to switch gcontext

In reply to Peter Xu

It was originally created by qio_channel_add_watch() so it's always assigning the task to main context. Now we use the new API called qio_channel_add_watch_source() so that we get the GSource handle rather than the tag ID.

Meanwhile, caching the gsource and TCPChardevTelnetInit (which holds the handshake data) in SocketChardev.telnet_source so that we can also do dynamic context switch when update read handlers.

---
 chardev/char-socket.c | 67 +++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 51 insertions(+), 16 deletions(-)
chardev/char-socket.c +51 -16
index 5aa01e15ff..c22b3f330c 100644
@@ -39,6 +39,11 @@
 
 #define TCP_MAX_FDS 16
 
+typedef struct {
+    char buf[21];
+    size_t buflen;
+} TCPChardevTelnetInit;
+
 typedef struct {
     Chardev parent;
     QIOChannel *ioc; /* Client I/O channel */
@@ -59,6 +64,8 @@ typedef struct {
     bool is_listen;
     bool is_telnet;
     bool is_tn3270;
+    GSource *telnet_source;
+    TCPChardevTelnetInit *telnet_init;
 
     GSource *reconnect_timer;
     int64_t reconnect_time;
@@ -69,6 +76,7 @@ typedef struct {
     OBJECT_CHECK(SocketChardev, (obj), TYPE_CHARDEV_SOCKET)
 
 static gboolean socket_reconnect_timeout(gpointer opaque);
+static void tcp_chr_telnet_init(Chardev *chr);
 
 static void tcp_chr_reconn_timer_cancel(SocketChardev *s)
 {
@@ -555,6 +563,15 @@ static void tcp_chr_connect(void *opaque)
     qemu_chr_be_event(chr, CHR_EVENT_OPENED);
 }
 
+static void tcp_chr_telnet_destroy(SocketChardev *s)
+{
+    if (s->telnet_source) {
+        g_source_destroy(s->telnet_source);
+        g_source_unref(s->telnet_source);
+        s->telnet_source = NULL;
+    }
+}
+
 static void tcp_chr_update_read_handler(Chardev *chr)
 {
     SocketChardev *s = SOCKET_CHARDEV(chr);
@@ -569,6 +586,10 @@ static void tcp_chr_update_read_handler(Chardev *chr)
                                               chr, NULL, chr->gcontext);
     }
 
+    if (s->telnet_source) {
+        tcp_chr_telnet_init(CHARDEV(s));
+    }
+
     if (!s->connected) {
         return;
     }
@@ -582,32 +603,30 @@ static void tcp_chr_update_read_handler(Chardev *chr)
     }
 }
 
-typedef struct {
-    Chardev *chr;
-    char buf[21];
-    size_t buflen;
-} TCPChardevTelnetInit;
-
 static gboolean tcp_chr_telnet_init_io(QIOChannel *ioc,
                                        GIOCondition cond G_GNUC_UNUSED,
                                        gpointer user_data)
 {
-    TCPChardevTelnetInit *init = user_data;
+    SocketChardev *s = user_data;
+    Chardev *chr = CHARDEV(s);
+    TCPChardevTelnetInit *init = s->telnet_init;
     ssize_t ret;
 
+    assert(init);
+
     ret = qio_channel_write(ioc, init->buf, init->buflen, NULL);
     if (ret < 0) {
         if (ret == QIO_CHANNEL_ERR_BLOCK) {
             ret = 0;
         } else {
-            tcp_chr_disconnect(init->chr);
+            tcp_chr_disconnect(chr);
             goto end;
         }
     }
     init->buflen -= ret;
 
     if (init->buflen == 0) {
-        tcp_chr_connect(init->chr);
+        tcp_chr_connect(chr);
         goto end;
     }
 
@@ -616,16 +635,30 @@ static gboolean tcp_chr_telnet_init_io(QIOChannel *ioc,
     return G_SOURCE_CONTINUE;
 
 end:
-    g_free(init);
+    g_free(s->telnet_init);
+    s->telnet_init = NULL;
+    g_source_unref(s->telnet_source);
+    s->telnet_source = NULL;
     return G_SOURCE_REMOVE;
 }
 
 static void tcp_chr_telnet_init(Chardev *chr)
 {
     SocketChardev *s = SOCKET_CHARDEV(chr);
-    TCPChardevTelnetInit *init = g_new0(TCPChardevTelnetInit, 1);
+    TCPChardevTelnetInit *init;
     size_t n = 0;
 
+    /* Destroy existing task */
+    tcp_chr_telnet_destroy(s);
+
+    if (s->telnet_init) {
+        /* We are possibly during a handshake already */
+        goto cont;
+    }
+
+    s->telnet_init = g_new0(TCPChardevTelnetInit, 1);
+    init = s->telnet_init;
+
 #define IACSET(x, a, b, c)                      \
     do {                                        \
         x[n++] = a;                             \
@@ -633,7 +666,6 @@ static void tcp_chr_telnet_init(Chardev *chr)
         x[n++] = c;                             \
     } while (0)
 
-    init->chr = chr;
     if (!s->is_tn3270) {
         init->buflen = 12;
         /* Prep the telnet negotion to put telnet in binary,
@@ -656,10 +688,11 @@ static void tcp_chr_telnet_init(Chardev *chr)
 
 #undef IACSET
 
-    qio_channel_add_watch(
-        s->ioc, G_IO_OUT,
-        tcp_chr_telnet_init_io,
-        init, NULL);
+cont:
+    s->telnet_source = qio_channel_add_watch_source(s->ioc, G_IO_OUT,
+                                                    tcp_chr_telnet_init_io,
+                                                    s, NULL,
+                                                    chr->gcontext);
 }
 
 
@@ -834,6 +867,8 @@ static void char_socket_finalize(Object *obj)
     tcp_chr_free_connection(chr);
     tcp_chr_reconn_timer_cancel(s);
     qapi_free_SocketAddress(s->addr);
+    tcp_chr_telnet_destroy(s);
+    g_free(s->telnet_init);
     if (s->listener) {
         qio_net_listener_set_client_func_full(s->listener, NULL, NULL,
                                               NULL, chr->gcontext);
Signature · 1 line
2.14.3
Reviewed-by on v2 4/9
#7 lore raw

Re: [Qemu-devel] [PATCH v2 4/9] chardev: allow telnet gsource to switch gcontext

Quoted 5 lines · Peter Xu

It was originally created by qio_channel_add_watch() so it's always assigning the task to main context. Now we use the new API called qio_channel_add_watch_source() so that we get the GSource handle rather than the tag ID.

Quoted 8 lines

Meanwhile, caching the gsource and TCPChardevTelnetInit (which holds the handshake data) in SocketChardev.telnet_source so that we can also do dynamic context switch when update read handlers.

---
 chardev/char-socket.c | 67 +++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 51 insertions(+), 16 deletions(-)

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 :|
author
#8 lore raw

PATCH v2 5/9 chardev: introduce chr_machine_done hook

In reply to Peter Xu

Introduce ChardevClass.chr_machine_done() hook so that chardevs can run customized procedures after machine init.

There was an existing mux user already that did similar thing but used a raw machine done notifier. Generalize it into a framework, and let the mux chardevs provide such a class-specific hook to achieve the same thing. Then we can move the mux related code to the char-mux.c file.

Since at it, replace the mux_realized variable with the global machine_init_done varible.

This notifier framework will be further leverged by other type of chardevs soon.

---
 chardev/char-mux.c         | 33 +++++++++++++++++++++++++++++----
 chardev/char.c             | 43 +++++++++++++++++--------------------------
 include/chardev/char-mux.h |  2 --
 include/chardev/char.h     |  2 ++
 tests/test-char.c          |  1 -
 5 files changed, 48 insertions(+), 33 deletions(-)
chardev/char-mux.c +29 -4
index d48e78103a..1b925c8dec 100644
@@ -27,6 +27,7 @@
 #include "qemu/option.h"
 #include "chardev/char.h"
 #include "sysemu/block-backend.h"
+#include "sysemu/sysemu.h"
 #include "chardev/char-mux.h"
 
 /* MUX driver for serial I/O splitting */
@@ -230,14 +231,12 @@ static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
         }
 }
 
-bool muxes_realized;
-
 void mux_chr_send_all_event(Chardev *chr, int event)
 {
     MuxChardev *d = MUX_CHARDEV(chr);
     int i;
 
-    if (!muxes_realized) {
+    if (!machine_init_done) {
         return;
     }
 
@@ -327,7 +326,7 @@ static void qemu_chr_open_mux(Chardev *chr,
     /* only default to opened state if we've realized the initial
      * set of muxes
      */
-    *be_opened = muxes_realized;
+    *be_opened = machine_init_done;
     qemu_chr_fe_init(&d->chr, drv, errp);
 }
 
@@ -347,6 +346,31 @@ static void qemu_chr_parse_mux(QemuOpts *opts, ChardevBackend *backend,
     mux->chardev = g_strdup(chardev);
 }
 
+/**
+ * Called after processing of default and command-line-specified
+ * chardevs to deliver CHR_EVENT_OPENED events to any FEs attached
+ * to a mux chardev. This is done here to ensure that
+ * output/prompts/banners are only displayed for the FE that has
+ * focus when initial command-line processing/machine init is
+ * completed.
+ *
+ * After this point, any new FE attached to any new or existing
+ * mux will receive CHR_EVENT_OPENED notifications for the BE
+ * immediately.
+ */
+static int open_muxes(Chardev *chr)
+{
+    /* send OPENED to all already-attached FEs */
+    mux_chr_send_all_event(chr, CHR_EVENT_OPENED);
+    /*
+     * mark mux as OPENED so any new FEs will immediately receive
+     * OPENED event
+     */
+    qemu_chr_be_event(chr, CHR_EVENT_OPENED);
+
+    return 0;
+}
+
 static void char_mux_class_init(ObjectClass *oc, void *data)
 {
     ChardevClass *cc = CHARDEV_CLASS(oc);
@@ -357,6 +381,7 @@ static void char_mux_class_init(ObjectClass *oc, void *data)
     cc->chr_accept_input = mux_chr_accept_input;
     cc->chr_add_watch = mux_chr_add_watch;
     cc->chr_be_event = mux_chr_be_event;
+    cc->chr_machine_done = open_muxes;
 }
 
 static const TypeInfo char_mux_type_info = {
chardev/char.c +17 -26

64 lines not on this page · raw message

include/chardev/char-mux.h +0 -2
index 8928977897..1e13187767 100644
@@ -27,8 +27,6 @@
 #include "chardev/char.h"
 #include "chardev/char-fe.h"
 
-extern bool muxes_realized;
-
 #define MAX_MUX 4
 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2.  */
 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
include/chardev/char.h +2 -0
index a381dc3df8..1cb1f4763f 100644
@@ -247,6 +247,8 @@ typedef struct ChardevClass {
     void (*chr_set_echo)(Chardev *chr, bool echo);
     void (*chr_set_fe_open)(Chardev *chr, int fe_open);
     void (*chr_be_event)(Chardev *s, int event);
+    /* Return 0 if succeeded, 1 if failed */
+    int (*chr_machine_done)(Chardev *chr);
 } ChardevClass;
 
 Chardev *qemu_chardev_new(const char *id, const char *typename,
tests/test-char.c +0 -1
index b358620911..e49af9e398 100644
@@ -166,7 +166,6 @@ static void char_mux_test(void)
     FeHandler h1 = { 0, }, h2 = { 0, };
     CharBackend chr_be1, chr_be2;
 
-    muxes_realized = true; /* done after machine init */
     opts = qemu_opts_create(qemu_find_opts("chardev"), "mux-label",
                             1, &error_abort);
     qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
Signature · 1 line
2.14.3
Reviewed-by on v2 5/9
#9 lore raw

Re: [Qemu-devel] [PATCH v2 5/9] chardev: introduce chr_machine_done hook

Quoted 14 lines · Peter Xu

Introduce ChardevClass.chr_machine_done() hook so that chardevs can run customized procedures after machine init.

There was an existing mux user already that did similar thing but used a raw machine done notifier. Generalize it into a framework, and let the mux chardevs provide such a class-specific hook to achieve the same thing. Then we can move the mux related code to the char-mux.c file.

Since at it, replace the mux_realized variable with the global machine_init_done varible.

This notifier framework will be further leverged by other type of chardevs soon.

Quoted 8 lines
---
 chardev/char-mux.c         | 33 +++++++++++++++++++++++++++++----
 chardev/char.c             | 43 +++++++++++++++++--------------------------
 include/chardev/char-mux.h |  2 --
 include/chardev/char.h     |  2 ++
 tests/test-char.c          |  1 -
 5 files changed, 48 insertions(+), 33 deletions(-)

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 :|
author
#10 lore raw

PATCH v2 6/9 chardev: use chardev's gcontext for async connect

In reply to Peter Xu

Generalize the function to create the async QIO task connection. Also, fix the context pointer to use the chardev's gcontext.

---
 chardev/char-socket.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
chardev/char-socket.c +14 -11
index c22b3f330c..1ce5adad9a 100644
@@ -901,11 +901,22 @@ cleanup:
     object_unref(OBJECT(sioc));
 }
 
+static void tcp_chr_connect_async(Chardev *chr)
+{
+    SocketChardev *s = SOCKET_CHARDEV(chr);
+    QIOChannelSocket *sioc;
+
+    sioc = qio_channel_socket_new();
+    tcp_chr_set_client_ioc_name(chr, sioc);
+    qio_channel_socket_connect_async(sioc, s->addr,
+                                     qemu_chr_socket_connected,
+                                     chr, NULL, chr->gcontext);
+}
+
 static gboolean socket_reconnect_timeout(gpointer opaque)
 {
     Chardev *chr = CHARDEV(opaque);
     SocketChardev *s = SOCKET_CHARDEV(opaque);
-    QIOChannelSocket *sioc;
 
     g_source_unref(s->reconnect_timer);
     s->reconnect_timer = NULL;
@@ -914,11 +925,7 @@ static gboolean socket_reconnect_timeout(gpointer opaque)
         return false;
     }
 
-    sioc = qio_channel_socket_new();
-    tcp_chr_set_client_ioc_name(chr, sioc);
-    qio_channel_socket_connect_async(sioc, s->addr,
-                                     qemu_chr_socket_connected,
-                                     chr, NULL, NULL);
+    tcp_chr_connect_async(chr);
 
     return false;
 }
@@ -998,11 +1005,7 @@ static void qmp_chardev_open_socket(Chardev *chr,
     }
 
     if (s->reconnect_time) {
-        sioc = qio_channel_socket_new();
-        tcp_chr_set_client_ioc_name(chr, sioc);
-        qio_channel_socket_connect_async(sioc, s->addr,
-                                         qemu_chr_socket_connected,
-                                         chr, NULL, NULL);
+        tcp_chr_connect_async(chr);
     } else {
         if (s->is_listen) {
             char *name;
Signature · 1 line
2.14.3
Reviewed-by on v2 6/9
#11 lore raw

Re: [Qemu-devel] [PATCH v2 6/9] chardev: use chardev's gcontext for async connect

Quoted 8 lines · Peter Xu

Generalize the function to create the async QIO task connection. Also, fix the context pointer to use the chardev's gcontext.

---
 chardev/char-socket.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

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 :|
author
#12 lore raw

PATCH v2 7/9 chardev: tcp: postpone async connection setup

In reply to Peter Xu

This patch allows the socket chardev async connection be setup with non-default gcontext. We do it by postponing the setup to machine done, since until then we can know which context we should run the async operation on.

---
 chardev/char-socket.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
chardev/char-socket.c +14 -3
index 1ce5adad9a..165612845a 100644
@@ -1004,9 +1004,8 @@ static void qmp_chardev_open_socket(Chardev *chr,
         s->reconnect_time = reconnect;
     }
 
-    if (s->reconnect_time) {
-        tcp_chr_connect_async(chr);
-    } else {
+    /* If reconnect_time is set, will do that in chr_machine_done. */
+    if (!s->reconnect_time) {
         if (s->is_listen) {
             char *name;
             s->listener = qio_net_listener_new();
@@ -1138,6 +1137,17 @@ char_socket_get_connected(Object *obj, Error **errp)
     return s->connected;
 }
 
+static int tcp_chr_machine_done_hook(Chardev *chr)
+{
+    SocketChardev *s = SOCKET_CHARDEV(chr);
+
+    if (s->reconnect_time) {
+        tcp_chr_connect_async(chr);
+    }
+
+    return 0;
+}
+
 static void char_socket_class_init(ObjectClass *oc, void *data)
 {
     ChardevClass *cc = CHARDEV_CLASS(oc);
@@ -1153,6 +1163,7 @@ static void char_socket_class_init(ObjectClass *oc, void *data)
     cc->chr_add_client = tcp_chr_add_client;
     cc->chr_add_watch = tcp_chr_add_watch;
     cc->chr_update_read_handler = tcp_chr_update_read_handler;
+    cc->chr_machine_done = tcp_chr_machine_done_hook;
 
     object_class_property_add(oc, "addr", "SocketAddress",
                               char_socket_get_addr, NULL,
Signature · 1 line
2.14.3
Reviewed-by on v2 7/9
#13 lore raw

Re: [Qemu-devel] [PATCH v2 7/9] chardev: tcp: postpone async connection setup

Quoted 10 lines · Peter Xu

This patch allows the socket chardev async connection be setup with non-default gcontext. We do it by postponing the setup to machine done, since until then we can know which context we should run the async operation on.

---
 chardev/char-socket.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

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 :|
#14 lore raw

Re: [Qemu-devel] [PATCH v2 7/9] chardev: tcp: postpone async connection setup

In reply to Peter Xu

Hi

Quoted 32 lines · Peter Xu

This patch allows the socket chardev async connection be setup with non-default gcontext. We do it by postponing the setup to machine done, since until then we can know which context we should run the async operation on.

---
 chardev/char-socket.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
chardev/char-socket.c +2 -3
index 1ce5adad9a..165612845a 100644
@@ -1004,9 +1004,8 @@ static void qmp_chardev_open_socket(Chardev *chr,
         s->reconnect_time = reconnect;
     }
 
-    if (s->reconnect_time) {
-        tcp_chr_connect_async(chr);
-    } else {
+    /* If reconnect_time is set, will do that in chr_machine_done. */
+    if (!s->reconnect_time) {
         if (s->is_listen) {
             char *name;
             s->listener = qio_net_listener_new();
@@ -1138,6 +1137,17 @@ char_socket_get_connected(Object *obj, Error **errp)
     return s->connected;
 }
 

chardev/char-socket.c char_socket_get_connected(Object *obj, Error **errp) · line 1149

Quoted 10 lines
chardev/char-socket.c +10 -0
+static int tcp_chr_machine_done_hook(Chardev *chr)
+{
+    SocketChardev *s = SOCKET_CHARDEV(chr);
+
+    if (s->reconnect_time) {
+        tcp_chr_connect_async(chr);
+    }
+
+    return 0;
+}

This patch broke /vhost-user/reconnect test, since it is using reconnect chardev sockets under a test program, where no machine done hook exist.

Quoted 16 lines
chardev/char-socket.c +2 -1
+
 static void char_socket_class_init(ObjectClass *oc, void *data)
 {
     ChardevClass *cc = CHARDEV_CLASS(oc);
@@ -1153,6 +1163,7 @@ static void char_socket_class_init(ObjectClass *oc, void *data)
     cc->chr_add_client = tcp_chr_add_client;
     cc->chr_add_watch = tcp_chr_add_watch;
     cc->chr_update_read_handler = tcp_chr_update_read_handler;
+    cc->chr_machine_done = tcp_chr_machine_done_hook;
 
     object_class_property_add(oc, "addr", "SocketAddress",
                               char_socket_get_addr, NULL,
--

2.14.3

Signature · 1 line
Marc-André Lureau
#15 lore raw

Re: [Qemu-devel] [PATCH v2 7/9] chardev: tcp: postpone async connection setup

Hi

Quoted 35 lines · Marc-André Lureau

Hi

Quoted 32 lines · Peter Xu

This patch allows the socket chardev async connection be setup with non-default gcontext. We do it by postponing the setup to machine done, since until then we can know which context we should run the async operation on.

---
 chardev/char-socket.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
chardev/char-socket.c +2 -3
index 1ce5adad9a..165612845a 100644
@@ -1004,9 +1004,8 @@ static void qmp_chardev_open_socket(Chardev *chr,
         s->reconnect_time = reconnect;
     }
 
-    if (s->reconnect_time) {
-        tcp_chr_connect_async(chr);
-    } else {
+    /* If reconnect_time is set, will do that in chr_machine_done. */
+    if (!s->reconnect_time) {
         if (s->is_listen) {
             char *name;
             s->listener = qio_net_listener_new();
@@ -1138,6 +1137,17 @@ char_socket_get_connected(Object *obj, Error **errp)
     return s->connected;
 }
 
Quoted 15 lines

chardev/char-socket.c char_socket_get_connected(Object *obj, Error **errp) · line 1149

Quoted 10 lines
chardev/char-socket.c +10 -0
+static int tcp_chr_machine_done_hook(Chardev *chr)
+{
+    SocketChardev *s = SOCKET_CHARDEV(chr);
+
+    if (s->reconnect_time) {
+        tcp_chr_connect_async(chr);
+    }
+
+    return 0;
+}

This patch broke /vhost-user/reconnect test, since it is using reconnect chardev sockets under a test program, where no machine done hook exist.

What about chardev created after machine done?

Quoted 20 lines
Quoted 13 lines
chardev/char-socket.c +2 -1
+
 static void char_socket_class_init(ObjectClass *oc, void *data)
 {
     ChardevClass *cc = CHARDEV_CLASS(oc);
@@ -1153,6 +1163,7 @@ static void char_socket_class_init(ObjectClass *oc, void *data)
     cc->chr_add_client = tcp_chr_add_client;
     cc->chr_add_watch = tcp_chr_add_watch;
     cc->chr_update_read_handler = tcp_chr_update_read_handler;
+    cc->chr_machine_done = tcp_chr_machine_done_hook;
 
     object_class_property_add(oc, "addr", "SocketAddress",
                               char_socket_get_addr, NULL,
--
Quoted 3 lines

2.14.3

--
Marc-André Lureau

Signature · 1 line
Marc-André Lureau
author
#16 lore raw

Re: [Qemu-devel] [PATCH v2 7/9] chardev: tcp: postpone async connection setup

Quoted 54 lines · Marc-André Lureau

Hi

Quoted 35 lines · Marc-André Lureau

Hi

Quoted 32 lines · Peter Xu

This patch allows the socket chardev async connection be setup with non-default gcontext. We do it by postponing the setup to machine done, since until then we can know which context we should run the async operation on.

---
 chardev/char-socket.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
chardev/char-socket.c +2 -3
index 1ce5adad9a..165612845a 100644
@@ -1004,9 +1004,8 @@ static void qmp_chardev_open_socket(Chardev *chr,
         s->reconnect_time = reconnect;
     }
 
-    if (s->reconnect_time) {
-        tcp_chr_connect_async(chr);
-    } else {
+    /* If reconnect_time is set, will do that in chr_machine_done. */
+    if (!s->reconnect_time) {
         if (s->is_listen) {
             char *name;
             s->listener = qio_net_listener_new();
@@ -1138,6 +1137,17 @@ char_socket_get_connected(Object *obj, Error **errp)
     return s->connected;
 }
 
Quoted 15 lines

chardev/char-socket.c char_socket_get_connected(Object *obj, Error **errp) · line 1149

Quoted 10 lines
chardev/char-socket.c +10 -0
+static int tcp_chr_machine_done_hook(Chardev *chr)
+{
+    SocketChardev *s = SOCKET_CHARDEV(chr);
+
+    if (s->reconnect_time) {
+        tcp_chr_connect_async(chr);
+    }
+
+    return 0;
+}

This patch broke /vhost-user/reconnect test, since it is using reconnect chardev sockets under a test program, where no machine done hook exist.

Quoted 1 line

What about chardev created after machine done?

Hmm, so binding that with machine done might be problematic...

I'm thinking whether we can do that in ->chr_update_read_handler() instead.

Btw, is there a short answer on why we disabled the reconnection test in vhost-user-test? Could I just verify the problem using:

QTEST_VHOST_USER_FIXME=1 QTEST_QEMU_BINARY=x86_64-softmmu/qemu-system-x86_64 ./tests/vhost-user-test

? Or any easy way to reproduce/verify the problem?

Regards,

Signature · 1 line
Peter Xu
author
#17 lore raw

PATCH v2 8/9 chardev: tcp: let TLS run on chardev context

In reply to Peter Xu

Now qio_channel_tls_handshake() is ready to receive the context. Let socket chardev use it, then the TLS handshake of chardev will always be with the chardev's context.

---
 chardev/char-socket.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
chardev/char-socket.c +1 -1
index 165612845a..bd40864f87 100644
@@ -750,7 +750,7 @@ static void tcp_chr_tls_init(Chardev *chr)
                               tcp_chr_tls_handshake,
                               chr,
                               NULL,
-                              NULL);
+                              chr->gcontext);
 }
 
 
Signature · 1 line
2.14.3
Reviewed-by on v2 8/9
#18 lore raw

Re: [Qemu-devel] [PATCH v2 8/9] chardev: tcp: let TLS run on chardev context

Quoted 8 lines · Peter Xu

Now qio_channel_tls_handshake() is ready to receive the context. Let socket chardev use it, then the TLS handshake of chardev will always be with the chardev's context.

---
 chardev/char-socket.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

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 :|
author
#19 lore raw

PATCH v2 9/9 chardev: tcp: postpone TLS work until machine done

In reply to Peter Xu

TLS handshake may create background GSource tasks, while we won't know the correct GMainContext until the whole chardev (including frontend) inited. Let's postpone the initial TLS handshake until machine done.

For dynamically created tcp chardev, we don't postpone that by checking the init_machine_done variable.

---
 chardev/char-socket.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
chardev/char-socket.c +10 -0
index bd40864f87..997c70dd7d 100644
@@ -31,6 +31,7 @@
 #include "qemu/option.h"
 #include "qapi/error.h"
 #include "qapi/clone-visitor.h"
+#include "sysemu/sysemu.h"
 
 #include "chardev/char-io.h"
 
@@ -722,6 +723,11 @@ static void tcp_chr_tls_init(Chardev *chr)
     Error *err = NULL;
     gchar *name;
 
+    if (!machine_init_done) {
+        /* This will be postponed to machine_done notifier */
+        return;
+    }
+
     if (s->is_listen) {
         tioc = qio_channel_tls_new_server(
             s->ioc, s->tls_creds,
@@ -1145,6 +1151,10 @@ static int tcp_chr_machine_done_hook(Chardev *chr)
         tcp_chr_connect_async(chr);
     }
 
+    if (s->tls_creds) {
+        tcp_chr_tls_init(chr);
+    }
+
     return 0;
 }
 
Signature · 1 line
2.14.3
#20 lore raw

Re: [Qemu-devel] [PATCH v2 9/9] chardev: tcp: postpone TLS work until machine done

Quoted 6 lines · Peter Xu

TLS handshake may create background GSource tasks, while we won't know the correct GMainContext until the whole chardev (including frontend) inited. Let's postpone the initial TLS handshake until machine done.

For dynamically created tcp chardev, we don't postpone that by checking the init_machine_done variable.

Not sure I see the need for this one - we've already postponed the acceptance of a client in the patch 7.

Quoted 31 lines
---
 chardev/char-socket.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
chardev/char-socket.c +6 -0
index bd40864f87..997c70dd7d 100644
@@ -31,6 +31,7 @@
 #include "qemu/option.h"
 #include "qapi/error.h"
 #include "qapi/clone-visitor.h"
+#include "sysemu/sysemu.h"
 
 #include "chardev/char-io.h"
 
@@ -722,6 +723,11 @@ static void tcp_chr_tls_init(Chardev *chr)
     Error *err = NULL;
     gchar *name;
 
+    if (!machine_init_done) {
+        /* This will be postponed to machine_done notifier */
+        return;
+    }
+
     if (s->is_listen) {
         tioc = qio_channel_tls_new_server(
             s->ioc, s->tls_creds,

chardev/char-socket.c static int tcp_chr_machine_done_hook(Chardev *chr) · line 1156

Quoted 6 lines
chardev/char-socket.c +3 -0
@@ -1145,6 +1151,10 @@ static int tcp_chr_machine_done_hook(Chardev *chr)
         tcp_chr_connect_async(chr);
     }
 
+    if (s->tls_creds) {
+        tcp_chr_tls_init(chr);
+    }

This looks questionable - AFAICT, there's no guarantee we have any client connection active when the machine dnoe hook runs. Only if the chardev is set in client mode, and reconnect_time is *not* set, but this seems to be run unconditionally.

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 :|
#21 lore raw

Re: [Qemu-devel] [PATCH v2 9/9] chardev: tcp: postpone TLS work until machine done

Quoted 10 lines · Daniel P. Berrangé
Quoted 6 lines · Peter Xu

TLS handshake may create background GSource tasks, while we won't know the correct GMainContext until the whole chardev (including frontend) inited. Let's postpone the initial TLS handshake until machine done.

For dynamically created tcp chardev, we don't postpone that by checking the init_machine_done variable.

Not sure I see the need for this one - we've already postponed the acceptance of a client in the patch 7.

Opps, meant to remove this comment, in favour of the later comment - ignore this bit.

Quoted 45 lines
Quoted 32 lines
---
 chardev/char-socket.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
chardev/char-socket.c +6 -0
index bd40864f87..997c70dd7d 100644
@@ -31,6 +31,7 @@
 #include "qemu/option.h"
 #include "qapi/error.h"
 #include "qapi/clone-visitor.h"
+#include "sysemu/sysemu.h"
 
 #include "chardev/char-io.h"
 
@@ -722,6 +723,11 @@ static void tcp_chr_tls_init(Chardev *chr)
     Error *err = NULL;
     gchar *name;
 
+    if (!machine_init_done) {
+        /* This will be postponed to machine_done notifier */
+        return;
+    }
+
     if (s->is_listen) {
         tioc = qio_channel_tls_new_server(
             s->ioc, s->tls_creds,

chardev/char-socket.c static int tcp_chr_machine_done_hook(Chardev *chr) · line 1156

Quoted 6 lines
chardev/char-socket.c +3 -0
@@ -1145,6 +1151,10 @@ static int tcp_chr_machine_done_hook(Chardev *chr)
         tcp_chr_connect_async(chr);
     }
 
+    if (s->tls_creds) {
+        tcp_chr_tls_init(chr);
+    }

This looks questionable - AFAICT, there's no guarantee we have any client connection active when the machine dnoe hook runs. Only if the chardev is set in client mode, and reconnect_time is *not* set, but this seems to be run unconditionally.

Quoted 7 lines

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 :|

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 :|
#22 lore raw

Re: [Qemu-devel] [PATCH v2 9/9] chardev: tcp: postpone TLS work until machine done

Quoted 6 lines · Daniel P. Berrangé
Quoted 5 lines · Daniel P. Berrangé
Quoted 4 lines · Peter Xu

TLS handshake may create background GSource tasks, while we won't know the correct GMainContext until the whole chardev (including frontend) inited. Let's postpone the initial TLS handshake until machine done.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

For dynamically created tcp chardev, we don't postpone that by checking the init_machine_done variable.

Not sure I see the need for this one - we've already postponed the acceptance of a client in the patch 7.

Opps, meant to remove this comment, in favour of the later comment - ignore this bit.

Since time is ticking for soft freeze, I'll queue the series without this patch.

Paolo

Quoted 56 lines
Quoted 45 lines
Quoted 32 lines
---
 chardev/char-socket.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
chardev/char-socket.c +6 -0
index bd40864f87..997c70dd7d 100644
@@ -31,6 +31,7 @@
 #include "qemu/option.h"
 #include "qapi/error.h"
 #include "qapi/clone-visitor.h"
+#include "sysemu/sysemu.h"
 
 #include "chardev/char-io.h"
 
@@ -722,6 +723,11 @@ static void tcp_chr_tls_init(Chardev *chr)
     Error *err = NULL;
     gchar *name;
 
+    if (!machine_init_done) {
+        /* This will be postponed to machine_done notifier */
+        return;
+    }
+
     if (s->is_listen) {
         tioc = qio_channel_tls_new_server(
             s->ioc, s->tls_creds,

chardev/char-socket.c static int tcp_chr_machine_done_hook(Chardev *chr) · line 1156

Quoted 6 lines
chardev/char-socket.c +3 -0
@@ -1145,6 +1151,10 @@ static int tcp_chr_machine_done_hook(Chardev *chr)
         tcp_chr_connect_async(chr);
     }
 
+    if (s->tls_creds) {
+        tcp_chr_tls_init(chr);
+    }

This looks questionable - AFAICT, there's no guarantee we have any client connection active when the machine dnoe hook runs. Only if the chardev is set in client mode, and reconnect_time is *not* set, but this seems to be run unconditionally.

Quoted 7 lines

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 :|

Regards,
Daniel

author
#23 lore raw

Re: [Qemu-devel] [PATCH v2 9/9] chardev: tcp: postpone TLS work until machine done

Quoted 10 lines · Paolo Bonzini
Quoted 9 lines · Daniel P. Berrangé
Quoted 8 lines · Daniel P. Berrangé
Quoted 6 lines · Peter Xu

TLS handshake may create background GSource tasks, while we won't know the correct GMainContext until the whole chardev (including frontend) inited. Let's postpone the initial TLS handshake until machine done.

For dynamically created tcp chardev, we don't postpone that by checking the init_machine_done variable.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Not sure I see the need for this one - we've already postponed the acceptance of a client in the patch 7.

Opps, meant to remove this comment, in favour of the later comment - ignore this bit.

Since time is ticking for soft freeze, I'll queue the series without this patch.

Thanks Paolo.

Note that Dan's pull is still not merged yet, and this series will need that one.

I'll try to see whether I can prepare a good version for the last TLS patch before you start to test your next pull request.

Signature · 1 line
Peter Xu
author
#24 lore raw

Re: [Qemu-devel] [PATCH v2 9/9] chardev: tcp: postpone TLS work until machine done

In reply to Daniel P. Berrangé

On Wed, Mar 07, 2018 at 12:36:50PM +0000, Daniel P. Berrangé wrote:

[...]

Quoted 28 lines
Quoted 28 lines
chardev/char-socket.c +6 -0
index bd40864f87..997c70dd7d 100644
@@ -31,6 +31,7 @@
 #include "qemu/option.h"
 #include "qapi/error.h"
 #include "qapi/clone-visitor.h"
+#include "sysemu/sysemu.h"
 
 #include "chardev/char-io.h"
 
@@ -722,6 +723,11 @@ static void tcp_chr_tls_init(Chardev *chr)
     Error *err = NULL;
     gchar *name;
 
+    if (!machine_init_done) {
+        /* This will be postponed to machine_done notifier */
+        return;
+    }
+
     if (s->is_listen) {
         tioc = qio_channel_tls_new_server(
             s->ioc, s->tls_creds,
@@ -1145,6 +1151,10 @@ static int tcp_chr_machine_done_hook(Chardev *chr)
         tcp_chr_connect_async(chr);
     }
 
Quoted 8 lines

chardev/char-socket.c static int tcp_chr_machine_done_hook(Chardev *chr) · line 1156

Quoted 3 lines
chardev/char-socket.c +3 -0
+    if (s->tls_creds) {
+        tcp_chr_tls_init(chr);
+    }

This looks questionable - AFAICT, there's no guarantee we have any client connection active when the machine dnoe hook runs. Only if the chardev is set in client mode, and reconnect_time is *not* set, but this seems to be run unconditionally.

You are right. Thanks for spotting that.

Then how about this? It's a bit ugly, but I think it should be safe:

chardev/char-socket.c +20 -1
index bd40864f87..b4686fd23f 100644
@@ -31,6 +31,7 @@
 #include "qemu/option.h"
 #include "qapi/error.h"
 #include "qapi/clone-visitor.h"
+#include "sysemu/sysemu.h"
 
 #include "chardev/char-io.h"
 
@@ -51,6 +52,11 @@ typedef struct {
     QIONetListener *listener;
     GSource *hup_source;
     QCryptoTLSCreds *tls_creds;
+    /*
+     * This should only be used once - when we want to setup TLS for
+     * the session but we need to wait until machine init done.
+     */
+    bool tls_need_postponed_init;
     int connected;
     int max_size;
     int do_telnetopt;
@@ -791,7 +797,15 @@ static int tcp_chr_new_client(Chardev *chr, QIOChannelSocket *sioc)
     }
 
     if (s->tls_creds) {
-        tcp_chr_tls_init(chr);
+        if (machine_init_done) {
+            tcp_chr_tls_init(chr);
+        } else {
+            /*
+             * Postpone to machine init done since we need the correct
+             * context to setup the TLS handshake.
+             */
+            s->tls_need_postponed_init = true;
+        }
     } else {
         if (s->do_telnetopt) {
             tcp_chr_telnet_init(chr);
@@ -1145,6 +1159,11 @@ static int tcp_chr_machine_done_hook(Chardev *chr)
         tcp_chr_connect_async(chr);
     }
 
+    if (s->tls_need_postponed_init) {
+        assert(s->tls_creds);
+        tcp_chr_tls_init(chr);
+    }
+
     return 0;
 }
 

Thanks,

Signature · 1 line
Peter Xu
#25 lore raw

Re: [Qemu-devel] [PATCH v2 9/9] chardev: tcp: postpone TLS work until machine done

Quoted 41 lines · Peter Xu

On Wed, Mar 07, 2018 at 12:36:50PM +0000, Daniel P. Berrangé wrote:

[...]

Quoted 28 lines
Quoted 28 lines
chardev/char-socket.c +6 -0
index bd40864f87..997c70dd7d 100644
@@ -31,6 +31,7 @@
 #include "qemu/option.h"
 #include "qapi/error.h"
 #include "qapi/clone-visitor.h"
+#include "sysemu/sysemu.h"
 
 #include "chardev/char-io.h"
 
@@ -722,6 +723,11 @@ static void tcp_chr_tls_init(Chardev *chr)
     Error *err = NULL;
     gchar *name;
 
+    if (!machine_init_done) {
+        /* This will be postponed to machine_done notifier */
+        return;
+    }
+
     if (s->is_listen) {
         tioc = qio_channel_tls_new_server(
             s->ioc, s->tls_creds,
@@ -1145,6 +1151,10 @@ static int tcp_chr_machine_done_hook(Chardev *chr)
         tcp_chr_connect_async(chr);
     }
 
Quoted 8 lines

chardev/char-socket.c static int tcp_chr_new_client(Chardev *chr, QIOChannelSocket *sioc) · line 808

Quoted 3 lines
chardev/char-socket.c +3 -0
+    if (s->tls_creds) {
+        tcp_chr_tls_init(chr);
+    }

This looks questionable - AFAICT, there's no guarantee we have any client connection active when the machine dnoe hook runs. Only if the chardev is set in client mode, and reconnect_time is *not* set, but this seems to be run unconditionally.

Quoted 3 lines

You are right. Thanks for spotting that.

Then how about this? It's a bit ugly, but I think it should be safe:

Is it perhaps not possible to just check if 's->ioc' is non-NULL in the tcp_chr_machine_done_hook for your original patch ?

Quoted 54 lines
chardev/char-socket.c +20 -1
index bd40864f87..b4686fd23f 100644
@@ -31,6 +31,7 @@
 #include "qemu/option.h"
 #include "qapi/error.h"
 #include "qapi/clone-visitor.h"
+#include "sysemu/sysemu.h"
 
 #include "chardev/char-io.h"
 
@@ -51,6 +52,11 @@ typedef struct {
     QIONetListener *listener;
     GSource *hup_source;
     QCryptoTLSCreds *tls_creds;
+    /*
+     * This should only be used once - when we want to setup TLS for
+     * the session but we need to wait until machine init done.
+     */
+    bool tls_need_postponed_init;
     int connected;
     int max_size;
     int do_telnetopt;
@@ -791,7 +797,15 @@ static int tcp_chr_new_client(Chardev *chr, QIOChannelSocket *sioc)
     }
 
     if (s->tls_creds) {
-        tcp_chr_tls_init(chr);
+        if (machine_init_done) {
+            tcp_chr_tls_init(chr);
+        } else {
+            /*
+             * Postpone to machine init done since we need the correct
+             * context to setup the TLS handshake.
+             */
+            s->tls_need_postponed_init = true;
+        }
     } else {
         if (s->do_telnetopt) {
             tcp_chr_telnet_init(chr);
@@ -1145,6 +1159,11 @@ static int tcp_chr_machine_done_hook(Chardev *chr)
         tcp_chr_connect_async(chr);
     }
 
+    if (s->tls_need_postponed_init) {
+        assert(s->tls_creds);
+        tcp_chr_tls_init(chr);
+    }
+
     return 0;
 }
 
Quoted 4 lines

Thanks,

Signature · 1 line
Peter Xu

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 :|
author
#26 lore raw

Re: [Qemu-devel] [PATCH v2 9/9] chardev: tcp: postpone TLS work until machine done

Quoted 42 lines · Daniel P. Berrangé
Quoted 41 lines · Peter Xu

On Wed, Mar 07, 2018 at 12:36:50PM +0000, Daniel P. Berrangé wrote:

[...]

Quoted 36 lines
Quoted 25 lines
chardev/char-socket.c +6 -0
index bd40864f87..997c70dd7d 100644
@@ -31,6 +31,7 @@
 #include "qemu/option.h"
 #include "qapi/error.h"
 #include "qapi/clone-visitor.h"
+#include "sysemu/sysemu.h"
 
 #include "chardev/char-io.h"
 
@@ -722,6 +723,11 @@ static void tcp_chr_tls_init(Chardev *chr)
     Error *err = NULL;
     gchar *name;
 
+    if (!machine_init_done) {
+        /* This will be postponed to machine_done notifier */
+        return;
+    }
+
     if (s->is_listen) {
         tioc = qio_channel_tls_new_server(
             s->ioc, s->tls_creds,

chardev/char-socket.c static int tcp_chr_machine_done_hook(Chardev *chr) · line 1156

Quoted 6 lines
chardev/char-socket.c +3 -0
@@ -1145,6 +1151,10 @@ static int tcp_chr_machine_done_hook(Chardev *chr)
         tcp_chr_connect_async(chr);
     }
 
+    if (s->tls_creds) {
+        tcp_chr_tls_init(chr);
+    }

This looks questionable - AFAICT, there's no guarantee we have any client connection active when the machine dnoe hook runs. Only if the chardev is set in client mode, and reconnect_time is *not* set, but this seems to be run unconditionally.

Quoted 6 lines
Quoted 3 lines

You are right. Thanks for spotting that.

Then how about this? It's a bit ugly, but I think it should be safe:

Is it perhaps not possible to just check if 's->ioc' is non-NULL in the tcp_chr_machine_done_hook for your original patch ?

In tcp_chr_new_client() I see that s->ioc will be set no matter what, and that function can even be called without TLS enabled I think. Does that mean only check against s->ioc would not be enough?

Thanks,

Signature · 1 line
Peter Xu
#27 lore raw

Re: [Qemu-devel] [PATCH v2 9/9] chardev: tcp: postpone TLS work until machine done

Quoted 45 lines · Peter Xu
Quoted 44 lines · Daniel P. Berrangé
Quoted 43 lines · Peter Xu

On Wed, Mar 07, 2018 at 12:36:50PM +0000, Daniel P. Berrangé wrote:

[...]

Quoted 28 lines
Quoted 28 lines
chardev/char-socket.c +6 -0
index bd40864f87..997c70dd7d 100644
@@ -31,6 +31,7 @@
 #include "qemu/option.h"
 #include "qapi/error.h"
 #include "qapi/clone-visitor.h"
+#include "sysemu/sysemu.h"
 
 #include "chardev/char-io.h"
 
@@ -722,6 +723,11 @@ static void tcp_chr_tls_init(Chardev *chr)
     Error *err = NULL;
     gchar *name;
 
+    if (!machine_init_done) {
+        /* This will be postponed to machine_done notifier */
+        return;
+    }
+
     if (s->is_listen) {
         tioc = qio_channel_tls_new_server(
             s->ioc, s->tls_creds,
@@ -1145,6 +1151,10 @@ static int tcp_chr_machine_done_hook(Chardev *chr)
         tcp_chr_connect_async(chr);
     }
 
Quoted 8 lines

chardev/char-socket.c static int tcp_chr_machine_done_hook(Chardev *chr) · line 1156

Quoted 3 lines
chardev/char-socket.c +3 -0
+    if (s->tls_creds) {
+        tcp_chr_tls_init(chr);
+    }

This looks questionable - AFAICT, there's no guarantee we have any client connection active when the machine dnoe hook runs. Only if the chardev is set in client mode, and reconnect_time is *not* set, but this seems to be run unconditionally.

You are right. Thanks for spotting that.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Then how about this? It's a bit ugly, but I think it should be safe:

Is it perhaps not possible to just check if 's->ioc' is non-NULL in the tcp_chr_machine_done_hook for your original patch ?

In tcp_chr_new_client() I see that s->ioc will be set no matter what, and that function can even be called without TLS enabled I think. Does that mean only check against s->ioc would not be enough?

I mean like this:

chardev/char-socket.c +9 -0
index a1966aae51..19e3193817 100644
@@ -723,6 +723,11 @@ static void tcp_chr_tls_init(Chardev *chr)
     Error *err = NULL;
     gchar *name;
 
+    if (!machine_init_done) {
+        /* This will be postponed to machine_done notifier */
+        return;
+    }
+
     if (s->is_listen) {
         tioc = qio_channel_tls_new_server(
             s->ioc, s->tls_creds,
@@ -1146,6 +1151,10 @@ static int tcp_chr_machine_done_hook(Chardev *chr)
         tcp_chr_connect_async(chr);
     }
 
+    if (s->ioc && s->tls_creds) {
+        tcp_chr_tls_init(chr);
+    }
+
     return 0;
 }
 

s->ioc will only be != NULL, if some codepath in qemu_chr_parse_socket ended up calling tcp_chr_new_client(). If that happened we will have skipped setup of TLS, so calling tcp_chr_tls_init() based on s->ioc && s->tls_creds feels right to me.

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 :|
author
#28 lore raw

Re: [Qemu-devel] [PATCH v2 9/9] chardev: tcp: postpone TLS work until machine done

Quoted 86 lines · Daniel P. Berrangé
Quoted 45 lines · Peter Xu
Quoted 44 lines · Daniel P. Berrangé
Quoted 43 lines · Peter Xu

On Wed, Mar 07, 2018 at 12:36:50PM +0000, Daniel P. Berrangé wrote:

[...]

Quoted 28 lines
Quoted 28 lines
chardev/char-socket.c +6 -0
index bd40864f87..997c70dd7d 100644
@@ -31,6 +31,7 @@
 #include "qemu/option.h"
 #include "qapi/error.h"
 #include "qapi/clone-visitor.h"
+#include "sysemu/sysemu.h"
 
 #include "chardev/char-io.h"
 
@@ -722,6 +723,11 @@ static void tcp_chr_tls_init(Chardev *chr)
     Error *err = NULL;
     gchar *name;
 
+    if (!machine_init_done) {
+        /* This will be postponed to machine_done notifier */
+        return;
+    }
+
     if (s->is_listen) {
         tioc = qio_channel_tls_new_server(
             s->ioc, s->tls_creds,
@@ -1145,6 +1151,10 @@ static int tcp_chr_machine_done_hook(Chardev *chr)
         tcp_chr_connect_async(chr);
     }
 
Quoted 8 lines

chardev/char-socket.c static int tcp_chr_machine_done_hook(Chardev *chr) · line 1156

Quoted 3 lines
chardev/char-socket.c +3 -0
+    if (s->tls_creds) {
+        tcp_chr_tls_init(chr);
+    }

This looks questionable - AFAICT, there's no guarantee we have any client connection active when the machine dnoe hook runs. Only if the chardev is set in client mode, and reconnect_time is *not* set, but this seems to be run unconditionally.

You are right. Thanks for spotting that.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Then how about this? It's a bit ugly, but I think it should be safe:

Is it perhaps not possible to just check if 's->ioc' is non-NULL in the tcp_chr_machine_done_hook for your original patch ?

In tcp_chr_new_client() I see that s->ioc will be set no matter what, and that function can even be called without TLS enabled I think. Does that mean only check against s->ioc would not be enough?

I mean like this:

chardev/char-socket.c +9 -0
index a1966aae51..19e3193817 100644
@@ -723,6 +723,11 @@ static void tcp_chr_tls_init(Chardev *chr)
     Error *err = NULL;
     gchar *name;
 
+    if (!machine_init_done) {
+        /* This will be postponed to machine_done notifier */
+        return;
+    }
+
     if (s->is_listen) {
         tioc = qio_channel_tls_new_server(
             s->ioc, s->tls_creds,
@@ -1146,6 +1151,10 @@ static int tcp_chr_machine_done_hook(Chardev *chr)
         tcp_chr_connect_async(chr);
     }
 
+    if (s->ioc && s->tls_creds) {
+        tcp_chr_tls_init(chr);
+    }
+
     return 0;
 }
 
Quoted 1 line

s->ioc will only be != NULL, if some codepath in qemu_chr_parse_socket

I guess here you mean qmp_chardev_open_socket().

Quoted 3 lines

ended up calling tcp_chr_new_client(). If that happened we will have skipped setup of TLS, so calling tcp_chr_tls_init() based on s->ioc && s->tls_creds feels right to me.

Yes, it looks right to me too. :)

I'll post a complete patch soon with your authorship.

Thanks!

Signature · 1 line
Peter Xu
author
#29 lore raw

PATCH v2 9/9 chardev: tcp: postpone TLS work until machine done

In reply to Peter Xu

From: "Daniel P. Berrange" <[email protected]>

TLS handshake may create background GSource tasks, while we won't know the correct GMainContext until the whole chardev (including frontend) inited. Let's postpone the initial TLS handshake until machine done.

For dynamically created tcp chardev, we don't postpone that by checking the init_machine_done variable.

[peterx: add missing include line, do unit test]

---

Paolo: please feel free to review/queue this one if your pull test has not yet started. Thanks.

 chardev/char-socket.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
chardev/char-socket.c +10 -0
index a1966aae51..f02bf118c9 100644
@@ -32,6 +32,7 @@
 #include "qapi/error.h"
 #include "qapi/clone-visitor.h"
 #include "qapi/qapi-visit-sockets.h"
+#include "sysemu/sysemu.h"
 
 #include "chardev/char-io.h"
 
@@ -723,6 +724,11 @@ static void tcp_chr_tls_init(Chardev *chr)
     Error *err = NULL;
     gchar *name;
 
+    if (!machine_init_done) {
+        /* This will be postponed to machine_done notifier */
+        return;
+    }
+
     if (s->is_listen) {
         tioc = qio_channel_tls_new_server(
             s->ioc, s->tls_creds,
@@ -1146,6 +1152,10 @@ static int tcp_chr_machine_done_hook(Chardev *chr)
         tcp_chr_connect_async(chr);
     }
 
+    if (s->ioc && s->tls_creds) {
+        tcp_chr_tls_init(chr);
+    }
+
     return 0;
 }
 
Signature · 1 line
2.14.3
Acked-by on the v2 series
#30 lore raw
In reply to Peter Xu
Quoted 22 lines · Peter Xu

Based-on: <[email protected]>

This series is based on the QIO part:
[PATCH v3 0/6] qio: general non-default GMainContext support

v2:
- fix the reported problem by patchew in patch 5
- added some r-bs from Marc-Andre

Please review, thanks.

Peter Xu (9):
vl: export machine_init_done
chardev: fix leak in tcp_chr_telnet_init_io()
chardev: update net listener gcontext
chardev: allow telnet gsource to switch gcontext
chardev: introduce chr_machine_done hook
chardev: use chardev's gcontext for async connect
chardev: tcp: postpone async connection setup
chardev: tcp: let TLS run on chardev context
chardev: tcp: postpone TLS work until machine done

Quoted 10 lines
 chardev/char-mux.c         |  33 ++++++++--
 chardev/char-socket.c      | 153 ++++++++++++++++++++++++++++++++++-----------
 chardev/char.c             |  43 +++++--------
 include/chardev/char-mux.h |   2 -
 include/chardev/char.h     |   2 +
 include/sysemu/sysemu.h    |   2 +
 stubs/machine-init-done.c  |   2 +
 tests/test-char.c          |   1 -
 vl.c                       |   4 +-
 9 files changed, 169 insertions(+), 73 deletions(-)

I'm not very familiar with chardev or qio, so I defer this to Daniel Berrange: