Skip to contentSkip to conversation

RFC: chardev frontend open handling cleanup

PATCH In review Hans de Goede 3 participants 15 messages Mar 24, 2013 – Mar 25, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/8 RFC: chardev frontend open handling cleanup

This patch-series is the result of the
"[PATCH 1/2] char: add qemu_chr_be_is_fe_connected" discussion thread.

This patch series (tries to) make(s) the frontend open concept both more explicit and generic, and significantly cleans up the surrounding code.

Regards,

Hans

#1 lore raw

PATCH 1/8 qemu-char: Rename opened to be_open

Rename the opened variable to be_open to reflect that it contains the opened state of the backend.

---
 hw/usb/dev-serial.c | 2 +-
 hw/usb/redirect.c   | 2 +-
 include/char/char.h | 2 +-
 qemu-char.c         | 6 +++---
 spice-qemu-char.c   | 4 ++--
 5 files changed, 8 insertions(+), 8 deletions(-)
hw/usb/dev-serial.c +1 -1
index 47ac8c9..7c314dc 100644
@@ -495,7 +495,7 @@ static int usb_serial_initfn(USBDevice *dev)
                           usb_serial_event, s);
     usb_serial_handle_reset(dev);
 
-    if (s->cs->opened && !dev->attached) {
+    if (s->cs->be_open && !dev->attached) {
         usb_device_attach(dev);
     }
     return 0;
hw/usb/redirect.c +1 -1
index e0df74b..4d565ba 100644
@@ -272,7 +272,7 @@ static int usbredir_write(void *priv, uint8_t *data, int count)
     USBRedirDevice *dev = priv;
     int r;
 
-    if (!dev->cs->opened) {
+    if (!dev->cs->be_open) {
         return 0;
     }
 
include/char/char.h +1 -1
index 0326b2a..d801f92 100644
@@ -74,7 +74,7 @@ struct CharDriverState {
     int idle_tag;
     char *label;
     char *filename;
-    int opened;
+    int be_open;
     int avail_connections;
     QemuOpts *opts;
     QTAILQ_ENTRY(CharDriverState) next;
qemu-char.c +3 -3
index 4e011df..32a05af 100644
@@ -108,10 +108,10 @@ void qemu_chr_be_event(CharDriverState *s, int event)
     /* Keep track if the char device is open */
     switch (event) {
         case CHR_EVENT_OPENED:
-            s->opened = 1;
+            s->be_open = 1;
             break;
         case CHR_EVENT_CLOSED:
-            s->opened = 0;
+            s->be_open = 0;
             break;
     }
 
@@ -207,7 +207,7 @@ void qemu_chr_add_handlers(CharDriverState *s,
 
     /* We're connecting to an already opened device, so let's make sure we
        also get the open event */
-    if (s->opened) {
+    if (s->be_open) {
         qemu_chr_generic_open(s);
     }
 }
spice-qemu-char.c +2 -2
index 607abb6..613cc64 100644
@@ -91,8 +91,8 @@ static void vmc_state(SpiceCharDeviceInstance *sin, int connected)
 {
     SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
 
-    if ((scd->chr->opened && connected) ||
-        (!scd->chr->opened && !connected)) {
+    if ((scd->chr->be_open && connected) ||
+        (!scd->chr->be_open && !connected)) {
         return;
     }
 
Signature · 1 line
1.8.1.4
#2 lore raw

PATCH 2/8 qemu-char: Rename qemu_chr_generic_open to qemu_chr_be_generic_open

In reply to Hans de Goede

To better reflect that it is for handling a backend being opened.

---
 backends/baum.c     |  2 +-
 include/char/char.h |  2 +-
 qemu-char.c         | 24 ++++++++++++------------
 ui/console.c        |  2 +-
 ui/gtk.c            |  2 +-
 5 files changed, 16 insertions(+), 16 deletions(-)
backends/baum.c +1 -1
index d7d658c..ea9ffe8 100644
@@ -611,7 +611,7 @@ CharDriverState *chr_baum_init(void)
 
     qemu_set_fd_handler(baum->brlapi_fd, baum_chr_read, NULL, baum);
 
-    qemu_chr_generic_open(chr);
+    qemu_chr_be_generic_open(chr);
 
     return chr;
 
include/char/char.h +1 -1
index d801f92..dd8f39a 100644
@@ -235,7 +235,7 @@ void qemu_chr_add_handlers(CharDriverState *s,
                            IOEventHandler *fd_event,
                            void *opaque);
 
-void qemu_chr_generic_open(CharDriverState *s);
+void qemu_chr_be_generic_open(CharDriverState *s);
 void qemu_chr_accept_input(CharDriverState *s);
 int qemu_chr_add_client(CharDriverState *s, int fd);
 void qemu_chr_info_print(Monitor *mon, const QObject *ret_data);
qemu-char.c +12 -12
index 32a05af..55795d7 100644
@@ -120,7 +120,7 @@ void qemu_chr_be_event(CharDriverState *s, int event)
     s->chr_event(s->handler_opaque, event);
 }
 
-static gboolean qemu_chr_generic_open_bh(gpointer opaque)
+static gboolean qemu_chr_be_generic_open_bh(gpointer opaque)
 {
     CharDriverState *s = opaque;
     qemu_chr_be_event(s, CHR_EVENT_OPENED);
@@ -128,10 +128,10 @@ static gboolean qemu_chr_generic_open_bh(gpointer opaque)
     return FALSE;
 }
 
-void qemu_chr_generic_open(CharDriverState *s)
+void qemu_chr_be_generic_open(CharDriverState *s)
 {
     if (s->idle_tag == 0) {
-        s->idle_tag = g_idle_add(qemu_chr_generic_open_bh, s);
+        s->idle_tag = g_idle_add(qemu_chr_be_generic_open_bh, s);
     }
 }
 
@@ -208,7 +208,7 @@ void qemu_chr_add_handlers(CharDriverState *s,
     /* We're connecting to an already opened device, so let's make sure we
        also get the open event */
     if (s->be_open) {
-        qemu_chr_generic_open(s);
+        qemu_chr_be_generic_open(s);
     }
 }
 
@@ -482,7 +482,7 @@ static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
     chr->chr_guest_close = NULL;
 
     /* Muxes are always open on creation */
-    qemu_chr_generic_open(chr);
+    qemu_chr_be_generic_open(chr);
 
     return chr;
 }
@@ -836,7 +836,7 @@ static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
     chr->chr_update_read_handler = fd_chr_update_read_handler;
     chr->chr_close = fd_chr_close;
 
-    qemu_chr_generic_open(chr);
+    qemu_chr_be_generic_open(chr);
 
     return chr;
 }
@@ -1133,7 +1133,7 @@ static void pty_chr_state(CharDriverState *chr, int connected)
         pty_chr_rearm_timer(chr, 1000);
     } else {
         if (!s->connected)
-            qemu_chr_generic_open(chr);
+            qemu_chr_be_generic_open(chr);
         s->connected = 1;
     }
 }
@@ -1549,7 +1549,7 @@ static CharDriverState *qemu_chr_open_pp_fd(int fd)
     chr->chr_close = pp_close;
     chr->opaque = drv;
 
-    qemu_chr_generic_open(chr);
+    qemu_chr_be_generic_open(chr);
 
     return chr;
 }
@@ -1834,7 +1834,7 @@ static CharDriverState *qemu_chr_open_win_path(const char *filename)
         g_free(chr);
         return NULL;
     }
-    qemu_chr_generic_open(chr);
+    qemu_chr_be_generic_open(chr);
     return chr;
 }
 
@@ -1934,7 +1934,7 @@ static CharDriverState *qemu_chr_open_pipe(ChardevHostdev *opts)
         g_free(chr);
         return NULL;
     }
-    qemu_chr_generic_open(chr);
+    qemu_chr_be_generic_open(chr);
     return chr;
 }
 
@@ -1948,7 +1948,7 @@ static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
     s->hcom = fd_out;
     chr->opaque = s;
     chr->chr_write = win_chr_write;
-    qemu_chr_generic_open(chr);
+    qemu_chr_be_generic_open(chr);
     return chr;
 }
 
@@ -2513,7 +2513,7 @@ static void tcp_chr_connect(void *opaque)
     if (s->chan) {
         s->tag = io_add_watch_poll(s->chan, tcp_chr_read_poll, tcp_chr_read, chr);
     }
-    qemu_chr_generic_open(chr);
+    qemu_chr_be_generic_open(chr);
 }
 
 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
ui/console.c +1 -1
index eb7a2bc..e84ba8b 100644
@@ -1600,7 +1600,7 @@ static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
         s->t_attrib = s->t_attrib_default;
     }
 
-    qemu_chr_generic_open(chr);
+    qemu_chr_be_generic_open(chr);
     if (chr->init)
         chr->init(chr);
 }
ui/gtk.c +1 -1
index 305940d..903b4d4 100644
@@ -1116,7 +1116,7 @@ static GSList *gd_vc_init(GtkDisplayState *s, VirtualConsole *vc, int index, GSL
 
     gtk_menu_shell_append(GTK_MENU_SHELL(s->view_menu), vc->menu_item);
 
-    qemu_chr_generic_open(vc->chr);
+    qemu_chr_be_generic_open(vc->chr);
     if (vc->chr->init) {
         vc->chr->init(vc->chr);
     }
Signature · 1 line
1.8.1.4
#3 lore raw

PATCH 3/8 qemu-char: Add fe_open tracking

In reply to Hans de Goede

Add tracking of the fe_open state to struct CharDriverState.

---
 include/char/char.h | 1 +
 qemu-char.c         | 2 ++
 2 files changed, 3 insertions(+)
include/char/char.h +1 -0
index dd8f39a..3174575 100644
@@ -75,6 +75,7 @@ struct CharDriverState {
     char *label;
     char *filename;
     int be_open;
+    int fe_open;
     int avail_connections;
     QemuOpts *opts;
     QTAILQ_ENTRY(CharDriverState) next;
qemu-char.c +2 -0
index 55795d7..554d72f 100644
@@ -3385,6 +3385,7 @@ void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
 
 void qemu_chr_fe_open(struct CharDriverState *chr)
 {
+    chr->fe_open = 1;
     if (chr->chr_guest_open) {
         chr->chr_guest_open(chr);
     }
@@ -3392,6 +3393,7 @@ void qemu_chr_fe_open(struct CharDriverState *chr)
 
 void qemu_chr_fe_close(struct CharDriverState *chr)
 {
+    chr->fe_open = 0;
     if (chr->chr_guest_close) {
         chr->chr_guest_close(chr);
     }
Signature · 1 line
1.8.1.4
#4 lore raw

Re: [Qemu-devel] [PATCH 3/8] qemu-char: Add fe_open tracking

Quoted 33 lines · Hans de Goede

Add tracking of the fe_open state to struct CharDriverState.

---
 include/char/char.h | 1 +
 qemu-char.c         | 2 ++
 2 files changed, 3 insertions(+)
include/char/char.h +1 -0
index dd8f39a..3174575 100644
@@ -75,6 +75,7 @@ struct CharDriverState {
     char *label;
     char *filename;
     int be_open;
+    int fe_open;
     int avail_connections;
     QemuOpts *opts;
     QTAILQ_ENTRY(CharDriverState) next;
qemu-char.c +1 -0
index 55795d7..554d72f 100644
@@ -3385,6 +3385,7 @@ void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
 
 void qemu_chr_fe_open(struct CharDriverState *chr)
 {
+    chr->fe_open = 1;
     if (chr->chr_guest_open) {
         chr->chr_guest_open(chr);
     }

qemu-char.c void qemu_chr_fe_open(struct CharDriverState *chr) · line 3396

Quoted 4 lines
qemu-char.c +1 -0
@@ -3392,6 +3393,7 @@ void qemu_chr_fe_open(struct CharDriverState *chr)
 
 void qemu_chr_fe_close(struct CharDriverState *chr)
 {
+    chr->fe_open = 0;

Even though this gets rewritten later on, you should avoid calling the callback when open is called when fe_open=1. Something like

if (!chr->fe_open) {
    return;
}

Then later when this becomes set_fe_open() the backend doesn't need to deal with double open/close.

Regards,

Anthony Liguori

Quoted 5 lines
qemu-char.c +0 -0
     if (chr->chr_guest_close) {
         chr->chr_guest_close(chr);
     }
Signature · 1 line
1.8.1.4
#5 lore raw

Re: [Qemu-devel] [PATCH 3/8] qemu-char: Add fe_open tracking

Hi,

Quoted 43 lines · Anthony Liguori
Quoted 33 lines · Hans de Goede

Add tracking of the fe_open state to struct CharDriverState.

---
  include/char/char.h | 1 +
  qemu-char.c         | 2 ++
  2 files changed, 3 insertions(+)
include/char/char.h +1 -0
index dd8f39a..3174575 100644
@@ -75,6 +75,7 @@ struct CharDriverState {
      char *label;
      char *filename;
      int be_open;
+    int fe_open;
      int avail_connections;
      QemuOpts *opts;
      QTAILQ_ENTRY(CharDriverState) next;
qemu-char.c +1 -0
index 55795d7..554d72f 100644
@@ -3385,6 +3385,7 @@ void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
 
  void qemu_chr_fe_open(struct CharDriverState *chr)
  {
+    chr->fe_open = 1;
      if (chr->chr_guest_open) {
          chr->chr_guest_open(chr);
      }

qemu-char.c void qemu_chr_fe_open(struct CharDriverState *chr) · line 3396

Quoted 4 lines
qemu-char.c +1 -0
@@ -3392,6 +3393,7 @@ void qemu_chr_fe_open(struct CharDriverState *chr)
 
  void qemu_chr_fe_close(struct CharDriverState *chr)
  {
+    chr->fe_open = 0;

Even though this gets rewritten later on, you should avoid calling the callback when open is called when fe_open=1. Something like

Quoted 6 lines
if (!chr->fe_open) {
     return;
}

Then later when this becomes set_fe_open() the backend doesn't need to deal with double open/close.

Ok, will fix in the next iteration of this patchset.

Regards,

Hans

#6 lore raw

PATCH 4/8 qemu-char: Automatically do fe_open / fe_close on qemu_chr_add_handlers

In reply to Hans de Goede

Most frontends can't really determine if the guest actually has the frontend side open. So lets automatically generate fe_open / fe_close as soon as a frontend becomes ready (as signalled by calling qemu_chr_add_handlers) / becomes non ready (as signalled by setting all handlers to NULL).

And allow frontends which can actually determine if the guest is listening to opt-out of this.

---
 hw/usb/redirect.c   |  2 --
 hw/virtio-console.c |  1 +
 include/char/char.h |  1 +
 qemu-char.c         | 13 +++++++++++++
 4 files changed, 15 insertions(+), 2 deletions(-)
hw/usb/redirect.c +0 -2
index 4d565ba..0ddb081 100644
@@ -1310,7 +1310,6 @@ static int usbredir_initfn(USBDevice *udev)
     dev->compatible_speedmask = USB_SPEED_MASK_FULL | USB_SPEED_MASK_HIGH;
 
     /* Let the backend know we are ready */
-    qemu_chr_fe_open(dev->cs);
     qemu_chr_add_handlers(dev->cs, usbredir_chardev_can_read,
                           usbredir_chardev_read, usbredir_chardev_event, dev);
 
@@ -1334,7 +1333,6 @@ static void usbredir_handle_destroy(USBDevice *udev)
 {
     USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev);
 
-    qemu_chr_fe_close(dev->cs);
     qemu_chr_delete(dev->cs);
     /* Note must be done after qemu_chr_close, as that causes a close event */
     qemu_bh_delete(dev->chardev_close_bh);
hw/virtio-console.c +1 -0
index 8ef76e2..209ea38 100644
@@ -140,6 +140,7 @@ static int virtconsole_initfn(VirtIOSerialPort *port)
     }
 
     if (vcon->chr) {
+        vcon->chr->explicit_fe_open = 1;
         qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event,
                               vcon);
     }
include/char/char.h +1 -0
index 3174575..27ebbc3 100644
@@ -76,6 +76,7 @@ struct CharDriverState {
     char *filename;
     int be_open;
     int fe_open;
+    int explicit_fe_open;
     int avail_connections;
     QemuOpts *opts;
     QTAILQ_ENTRY(CharDriverState) next;
qemu-char.c +13 -0
index 554d72f..7c57971 100644
@@ -194,9 +194,14 @@ void qemu_chr_add_handlers(CharDriverState *s,
                            IOEventHandler *fd_event,
                            void *opaque)
 {
+    int fe_open;
+
     if (!opaque && !fd_can_read && !fd_read && !fd_event) {
         /* chr driver being released. */
+        fe_open = 0;
         ++s->avail_connections;
+    } else {
+        fe_open = 1;
     }
     s->chr_can_read = fd_can_read;
     s->chr_read = fd_read;
@@ -205,6 +210,14 @@ void qemu_chr_add_handlers(CharDriverState *s,
     if (s->chr_update_read_handler)
         s->chr_update_read_handler(s);
 
+    if (!s->explicit_fe_open) {
+        if (fe_open) {
+            qemu_chr_fe_open(s);
+        } else {
+            qemu_chr_fe_close(s);
+        }
+    }
+
     /* We're connecting to an already opened device, so let's make sure we
        also get the open event */
     if (s->be_open) {
Signature · 1 line
1.8.1.4
#7 lore raw

Re: [Qemu-devel] [PATCH 4/8] qemu-char: Automatically do fe_open / fe_close on qemu_chr_add_handlers

Quoted 7 lines · Hans de Goede

Most frontends can't really determine if the guest actually has the frontend side open. So lets automatically generate fe_open / fe_close as soon as a frontend becomes ready (as signalled by calling qemu_chr_add_handlers) / becomes non ready (as signalled by setting all handlers to NULL).

And allow frontends which can actually determine if the guest is listening to opt-out of this.

Could we change virtio-serial to delay calling add_handlers so that we could not introduce this variable?

Regards,

Anthony Liguori

Quoted 89 lines
---
 hw/usb/redirect.c   |  2 --
 hw/virtio-console.c |  1 +
 include/char/char.h |  1 +
 qemu-char.c         | 13 +++++++++++++
 4 files changed, 15 insertions(+), 2 deletions(-)
hw/usb/redirect.c +0 -2
index 4d565ba..0ddb081 100644
@@ -1310,7 +1310,6 @@ static int usbredir_initfn(USBDevice *udev)
     dev->compatible_speedmask = USB_SPEED_MASK_FULL | USB_SPEED_MASK_HIGH;
 
     /* Let the backend know we are ready */
-    qemu_chr_fe_open(dev->cs);
     qemu_chr_add_handlers(dev->cs, usbredir_chardev_can_read,
                           usbredir_chardev_read, usbredir_chardev_event, dev);
 
@@ -1334,7 +1333,6 @@ static void usbredir_handle_destroy(USBDevice *udev)
 {
     USBRedirDevice *dev = DO_UPCAST(USBRedirDevice, dev, udev);
 
-    qemu_chr_fe_close(dev->cs);
     qemu_chr_delete(dev->cs);
     /* Note must be done after qemu_chr_close, as that causes a close event */
     qemu_bh_delete(dev->chardev_close_bh);
hw/virtio-console.c +1 -0
index 8ef76e2..209ea38 100644
@@ -140,6 +140,7 @@ static int virtconsole_initfn(VirtIOSerialPort *port)
     }
 
     if (vcon->chr) {
+        vcon->chr->explicit_fe_open = 1;
         qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event,
                               vcon);
     }
include/char/char.h +1 -0
index 3174575..27ebbc3 100644
@@ -76,6 +76,7 @@ struct CharDriverState {
     char *filename;
     int be_open;
     int fe_open;
+    int explicit_fe_open;
     int avail_connections;
     QemuOpts *opts;
     QTAILQ_ENTRY(CharDriverState) next;
qemu-char.c +13 -0
index 554d72f..7c57971 100644
@@ -194,9 +194,14 @@ void qemu_chr_add_handlers(CharDriverState *s,
                            IOEventHandler *fd_event,
                            void *opaque)
 {
+    int fe_open;
+
     if (!opaque && !fd_can_read && !fd_read && !fd_event) {
         /* chr driver being released. */
+        fe_open = 0;
         ++s->avail_connections;
+    } else {
+        fe_open = 1;
     }
     s->chr_can_read = fd_can_read;
     s->chr_read = fd_read;
@@ -205,6 +210,14 @@ void qemu_chr_add_handlers(CharDriverState *s,
     if (s->chr_update_read_handler)
         s->chr_update_read_handler(s);
 
+    if (!s->explicit_fe_open) {
+        if (fe_open) {
+            qemu_chr_fe_open(s);
+        } else {
+            qemu_chr_fe_close(s);
+        }
+    }
+
     /* We're connecting to an already opened device, so let's make sure we
        also get the open event */
     if (s->be_open) {
Signature · 1 line
1.8.1.4
#8 lore raw

Re: [Qemu-devel] [PATCH 4/8] qemu-char: Automatically do fe_open / fe_close on qemu_chr_add_handlers

Hi,

Quoted 7 lines · Anthony Liguori
Quoted 5 lines · Hans de Goede

Most frontends can't really determine if the guest actually has the frontend side open. So lets automatically generate fe_open / fe_close as soon as a frontend becomes ready (as signalled by calling qemu_chr_add_handlers) / becomes non ready (as signalled by setting all handlers to NULL).

Quoted 5 lines
Quoted 2 lines

And allow frontends which can actually determine if the guest is listening to opt-out of this.

Could we change virtio-serial to delay calling add_handlers so that we could not introduce this variable?

Hmm, I was trying to avoid opening that can of worms. I've taken a quick look and there are 2 issues with doing this:
1) It will wreck havoc with CharDriverState.avail_connections, since that gets increased on a qemu_chr_add_handlers( NULL ), while it is decreased only once in hw/qdev-properties-system.c

This can be fixed by moving the avail_connections++ to hw/qdev-properties-system.c: release_chr, which seems the sensible thing to do wrt nicely balancing out these calls anyways.

2) It will cause the virtio-console front-end to miss various events, such as backend open/close events.

A backend open event will get "replayed" when qemu_chr_add_handlers( stuff ) is called, causing a potential double open from the virtio-console pov (one before it called qemu_chr_add_handlers( NULL ), and one on the next add_handlers call). And a close or any other events happening while the frontend side is closed will be missed.

I'll gladly add a patch fixing 1). to the next revision of this patchset, but given 2) I would prefer to stick with the explicit_fe_open flag and just register the handlers once.

Regards,

Hans

#9 lore raw

Re: [Qemu-devel] [PATCH 4/8] qemu-char: Automatically do fe_open / fe_close on qemu_chr_add_handlers

Quoted 35 lines · Hans de Goede

Hi,

Quoted 7 lines · Anthony Liguori
Quoted 5 lines · Hans de Goede

Most frontends can't really determine if the guest actually has the frontend side open. So lets automatically generate fe_open / fe_close as soon as a frontend becomes ready (as signalled by calling qemu_chr_add_handlers) / becomes non ready (as signalled by setting all handlers to NULL).

Quoted 5 lines
Quoted 2 lines

And allow frontends which can actually determine if the guest is listening to opt-out of this.

Could we change virtio-serial to delay calling add_handlers so that we could not introduce this variable?

Hmm, I was trying to avoid opening that can of worms. I've taken a quick look and there are 2 issues with doing this:
1) It will wreck havoc with CharDriverState.avail_connections, since that gets increased on a qemu_chr_add_handlers( NULL ), while it is decreased only once in hw/qdev-properties-system.c

This can be fixed by moving the avail_connections++ to hw/qdev-properties-system.c: release_chr, which seems the sensible thing to do wrt nicely balancing out these calls anyways.

2) It will cause the virtio-console front-end to miss various events, such as backend open/close events.

A backend open event will get "replayed" when qemu_chr_add_handlers( stuff ) is called, causing a potential double open from the virtio-console pov (one before it called qemu_chr_add_handlers( NULL ), and one on the next add_handlers call). And a close or any other events happening while the frontend side is closed will be missed.

Quoted 3 lines

I'll gladly add a patch fixing 1). to the next revision of this patchset, but given 2) I would prefer to stick with the explicit_fe_open flag and just register the handlers once.

Okay, seems reasonable.

REgards,

Anthony Liguori

Quoted 4 lines

Regards,

Hans

#10 lore raw

PATCH 5/8 qemu-char: Cleanup: consolidate fe_open/fe_close into fe_set_open

In reply to Hans de Goede
---
 hw/virtio-console.c |  4 ++--
 include/char/char.h | 17 ++++-------------
 qemu-char.c         | 19 +++++--------------
 3 files changed, 11 insertions(+), 29 deletions(-)
hw/virtio-console.c +2 -2
index 209ea38..c0d41c5 100644
@@ -79,7 +79,7 @@ static void guest_open(VirtIOSerialPort *port)
     if (!vcon->chr) {
         return;
     }
-    qemu_chr_fe_open(vcon->chr);
+    qemu_chr_fe_set_open(vcon->chr, 1);
 }
 
 /* Callback function that's called when the guest closes the port */
@@ -90,7 +90,7 @@ static void guest_close(VirtIOSerialPort *port)
     if (!vcon->chr) {
         return;
     }
-    qemu_chr_fe_close(vcon->chr);
+    qemu_chr_fe_set_open(vcon->chr, 0);
 }
 
 /* Readiness of the guest to accept data on a port */
include/char/char.h +4 -13
index 27ebbc3..3c8dd28 100644
@@ -129,21 +129,12 @@ void qemu_chr_delete(CharDriverState *chr);
 void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo);
 
 /**
- * @qemu_chr_fe_open:
+ * @qemu_chr_fe_set_open:
  *
- * Open a character backend.  This function call is an indication that the
- * front end is ready to begin doing I/O.
+ * Set character frontend open status.  This is an indication that the
+ * front end is ready (or not) to begin doing I/O.
  */
-void qemu_chr_fe_open(struct CharDriverState *chr);
-
-/**
- * @qemu_chr_fe_close:
- *
- * Close a character backend.  This function call indicates that the front end
- * no longer is able to process I/O.  To process I/O again, the front end will
- * call @qemu_chr_fe_open.
- */
-void qemu_chr_fe_close(struct CharDriverState *chr);
+void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open);
 
 /**
  * @qemu_chr_fe_printf:
qemu-char.c +5 -14
index 7c57971..713c154 100644
@@ -211,11 +211,7 @@ void qemu_chr_add_handlers(CharDriverState *s,
         s->chr_update_read_handler(s);
 
     if (!s->explicit_fe_open) {
-        if (fe_open) {
-            qemu_chr_fe_open(s);
-        } else {
-            qemu_chr_fe_close(s);
-        }
+        qemu_chr_fe_set_open(s, fe_open);
     }
 
     /* We're connecting to an already opened device, so let's make sure we
@@ -3396,18 +3392,13 @@ void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
     }
 }
 
-void qemu_chr_fe_open(struct CharDriverState *chr)
+void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
 {
-    chr->fe_open = 1;
-    if (chr->chr_guest_open) {
+    chr->fe_open = fe_open;
+    if (fe_open && chr->chr_guest_open) {
         chr->chr_guest_open(chr);
     }
-}
-
-void qemu_chr_fe_close(struct CharDriverState *chr)
-{
-    chr->fe_open = 0;
-    if (chr->chr_guest_close) {
+    if (!fe_open && chr->chr_guest_close) {
         chr->chr_guest_close(chr);
     }
 }
Signature · 1 line
1.8.1.4
#11 lore raw

PATCH 6/8 qemu-char: Consolidate guest_close/guest_open into a set_fe_open callback

In reply to Hans de Goede
---
 include/char/char.h |  3 +--
 qemu-char.c         | 10 +++-------
 spice-qemu-char.c   | 17 +++++++----------
 3 files changed, 11 insertions(+), 19 deletions(-)
include/char/char.h +1 -2
index 3c8dd28..1457e80 100644
@@ -68,8 +68,7 @@ struct CharDriverState {
     void (*chr_close)(struct CharDriverState *chr);
     void (*chr_accept_input)(struct CharDriverState *chr);
     void (*chr_set_echo)(struct CharDriverState *chr, bool echo);
-    void (*chr_guest_open)(struct CharDriverState *chr);
-    void (*chr_guest_close)(struct CharDriverState *chr);
+    void (*chr_set_fe_open)(struct CharDriverState *chr, int fe_open);
     void *opaque;
     int idle_tag;
     char *label;
qemu-char.c +3 -7
index 713c154..5be2ae7 100644
@@ -487,8 +487,7 @@ static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
     chr->chr_update_read_handler = mux_chr_update_read_handler;
     chr->chr_accept_input = mux_chr_accept_input;
     /* Frontend guest-open / -close notification is not support with muxes */
-    chr->chr_guest_open = NULL;
-    chr->chr_guest_close = NULL;
+    chr->chr_set_fe_open = NULL;
 
     /* Muxes are always open on creation */
     qemu_chr_be_generic_open(chr);
@@ -3395,11 +3394,8 @@ void qemu_chr_fe_set_echo(struct CharDriverState *chr, bool echo)
 void qemu_chr_fe_set_open(struct CharDriverState *chr, int fe_open)
 {
     chr->fe_open = fe_open;
-    if (fe_open && chr->chr_guest_open) {
-        chr->chr_guest_open(chr);
-    }
-    if (!fe_open && chr->chr_guest_close) {
-        chr->chr_guest_close(chr);
+    if (chr->chr_set_fe_open) {
+        chr->chr_set_fe_open(chr, fe_open);
     }
 }
 
spice-qemu-char.c +7 -10
index 613cc64..ba59374 100644
@@ -213,16 +213,14 @@ static void spice_chr_close(struct CharDriverState *chr)
     g_free(s);
 }
 
-static void spice_chr_guest_open(struct CharDriverState *chr)
+static void spice_chr_set_fe_open(struct CharDriverState *chr, int fe_open)
 {
     SpiceCharDriver *s = chr->opaque;
-    vmc_register_interface(s);
-}
-
-static void spice_chr_guest_close(struct CharDriverState *chr)
-{
-    SpiceCharDriver *s = chr->opaque;
-    vmc_unregister_interface(s);
+    if (fe_open) {
+        vmc_register_interface(s);
+    } else {
+        vmc_unregister_interface(s);
+    }
 }
 
 static void print_allowed_subtypes(void)
@@ -256,8 +254,7 @@ static CharDriverState *chr_open(const char *subtype)
     chr->chr_write = spice_chr_write;
     chr->chr_add_watch = spice_chr_add_watch;
     chr->chr_close = spice_chr_close;
-    chr->chr_guest_open = spice_chr_guest_open;
-    chr->chr_guest_close = spice_chr_guest_close;
+    chr->chr_set_fe_open = spice_chr_set_fe_open;
 
     QLIST_INSERT_HEAD(&spice_chars, s, next);
 
Signature · 1 line
1.8.1.4
#12 lore raw

PATCH 7/8 virtio-serial: Consolidate guest_open/guest_close into set_guest_connected

In reply to Hans de Goede
---
 hw/virtio-console.c    | 23 +++++------------------
 hw/virtio-serial-bus.c | 15 +++++----------
 hw/virtio-serial.h     |  6 ++----
 3 files changed, 12 insertions(+), 32 deletions(-)
hw/virtio-console.c +5 -18
index c0d41c5..e28c54f 100644
@@ -71,26 +71,15 @@ static ssize_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len)
     return ret;
 }
 
-/* Callback function that's called when the guest opens the port */
-static void guest_open(VirtIOSerialPort *port)
+/* Callback function that's called when the guest opens/closes the port */
+static void set_guest_connected(VirtIOSerialPort *port, int guest_connected)
 {
     VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
 
     if (!vcon->chr) {
         return;
     }
-    qemu_chr_fe_set_open(vcon->chr, 1);
-}
-
-/* Callback function that's called when the guest closes the port */
-static void guest_close(VirtIOSerialPort *port)
-{
-    VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
-
-    if (!vcon->chr) {
-        return;
-    }
-    qemu_chr_fe_set_open(vcon->chr, 0);
+    qemu_chr_fe_set_open(vcon->chr, guest_connected);
 }
 
 /* Readiness of the guest to accept data on a port */
@@ -173,8 +162,7 @@ static void virtconsole_class_init(ObjectClass *klass, void *data)
     k->init = virtconsole_initfn;
     k->exit = virtconsole_exitfn;
     k->have_data = flush_buf;
-    k->guest_open = guest_open;
-    k->guest_close = guest_close;
+    k->set_guest_connected = set_guest_connected;
     dc->props = virtconsole_properties;
 }
 
@@ -197,8 +185,7 @@ static void virtserialport_class_init(ObjectClass *klass, void *data)
 
     k->init = virtconsole_initfn;
     k->have_data = flush_buf;
-    k->guest_open = guest_open;
-    k->guest_close = guest_close;
+    k->set_guest_connected = set_guest_connected;
     dc->props = virtserialport_properties;
 }
 
hw/virtio-serial-bus.c +5 -10
index ab7168e..eb7af21 100644
@@ -372,14 +372,9 @@ static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
 
     case VIRTIO_CONSOLE_PORT_OPEN:
         port->guest_connected = cpkt.value;
-        if (cpkt.value && vsc->guest_open) {
+        if (vsc->set_guest_connected) {
             /* Send the guest opened notification if an app is interested */
-            vsc->guest_open(port);
-        }
-
-        if (!cpkt.value && vsc->guest_close) {
-            /* Send the guest closed notification if an app is interested */
-            vsc->guest_close(port);
+            vsc->set_guest_connected(port, cpkt.value);
         }
         break;
     }
@@ -484,9 +479,9 @@ static void guest_reset(VirtIOSerial *vser)
         vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
         if (port->guest_connected) {
             port->guest_connected = false;
-
-            if (vsc->guest_close)
-                vsc->guest_close(port);
+            if (vsc->set_guest_connected) {
+                vsc->set_guest_connected(port, false);
+            }
         }
     }
 }
hw/virtio-serial.h +2 -4
index 484dcfe..516400f 100644
@@ -92,10 +92,8 @@ typedef struct VirtIOSerialPortClass {
     int (*exit)(VirtIOSerialPort *port);
 
     /* Callbacks for guest events */
-        /* Guest opened device. */
-    void (*guest_open)(VirtIOSerialPort *port);
-        /* Guest closed device. */
-    void (*guest_close)(VirtIOSerialPort *port);
+        /* Guest opened/closed device. */
+    void (*set_guest_connected)(VirtIOSerialPort *port, int guest_connected);
 
         /* Guest is now ready to accept data (virtqueues set up). */
     void (*guest_ready)(VirtIOSerialPort *port);
Signature · 1 line
1.8.1.4
#13 lore raw

PATCH 8/8 spice-qemu-char: Drop hackish vmc_register on spice_chr_write

In reply to Hans de Goede

Now that the core takes care of fe_open tracking we no longer need this hack.

---
 spice-qemu-char.c | 1 -
 1 file changed, 1 deletion(-)
spice-qemu-char.c +0 -1
index ba59374..7e6bd2d 100644
@@ -184,7 +184,6 @@ static int spice_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
     SpiceCharDriver *s = chr->opaque;
     int read_bytes;
 
-    vmc_register_interface(s);
     assert(s->datalen == 0);
     s->datapos = buf;
     s->datalen = len;
Signature · 1 line
1.8.1.4
#14 lore raw
In reply to Hans de Goede
Quoted 8 lines

This patch-series is the result of the
"[PATCH 1/2] char: add qemu_chr_be_is_fe_connected" discussion thread.

This patch series (tries to) make(s) the frontend open concept both more
explicit and generic, and significantly cleans up the surrounding code.

The whole patch series looks good to me.

Quoted 6 lines

Regards,

Hans