Skip to contentSkip to conversation

Multiple Port Support for virtio-console

PATCH New Amit Shah 1 participant 15 messages Sep 9, 2009 – Sep 10, 2009

Subsystems (4)
lore
Share & tools
lore

Amit Shah

author
#0 lore raw

Hello all,

Here is a new iteration of the patch series that implements a transport for guest and host communications.

I've tested for compatibility (old qemu & new kernel, new qemu & old kernel, new qemu & new kernel) and it all works fine*. Migration works with the patch Juan just posted.

There are a few items on my todo list but this works well.

New since last send:
- migrate per-port buffers that are queued up
- in-qemu api for open/close/read/write of ports.
- the read() is a callback that's invoked when complete data corresponding to one write() request on the guest is available.
- removed comments from virtio_console.c that are no longer relevant.
- address review comments by Juan

TODO:
- Convert all config writes to little endian in qemu / convert from little endian to host endian in guest
- Address a few FIXMEs spread in the code
- Introduce a watermark to stop a rogue host process flooding guest with data

Conditions:
* Heavy IO on an hvc port and a non-hvc port causes memory corruption each time, same place. It could be realted to locking but I doubt that. This still has to be sorted out

Please review.
Amit

author
#1 lore raw

PATCH 1/1 virtio_console: Add support for multiple ports for generic guest and host communication

Expose multiple char devices ("ports") for simple communication between the host userspace and guest.

Sample offline usages can be: poking around in a guest to find out the file systems used, applications installed, etc. Online usages can be sharing of clipboard data between the guest and the host, sending information about logged-in users to the host, locking the screen or session when a vnc session is closed, and so on.

The interface presented to guest userspace is of a simple char device, so it can be used like this:

    fd = open("/dev/vcon2", O_RDWR);
    ret = read(fd, buf, 100);
    ret = write(fd, string, strlen(string));

Each port is to be assigned a unique function, for example, the first 4 ports may be reserved for libvirt usage, the next 4 for generic streaming data and so on. This port-function mapping isn't finalised yet.

For requirements, use-cases and some history see

http://www.linux-kvm.org/page/VMchannel_Requirements

---
 drivers/char/Kconfig           |    4 +-
 drivers/char/virtio_console.c  | 1069 +++++++++++++++++++++++++++++++++++-----
 include/linux/virtio_console.h |   60 +++-
 3 files changed, 994 insertions(+), 139 deletions(-)
drivers/char/Kconfig +3 -1
index b8368e2..cbbb9a1 100644
@@ -678,7 +678,9 @@ config VIRTIO_CONSOLE
 	select HVC_DRIVER
 	help
 	  Virtio console for use with lguest and other hypervisors.
-
+	  Also serves as a general-purpose serial device for data transfer
+	  between the guest and host. Character devices at /dev/vconNN will
+	  be created when corresponding ports are found.
 
 config HVCS
 	tristate "IBM Hypervisor Virtual Console Server support"
drivers/char/virtio_console.c +933 -136

1,177 lines not on this page · raw message

include/linux/virtio_console.h +58 -2

78 lines not on this page · raw message

Signature · 1 line
1.6.2.5
author
#2 lore raw

PATCH 1/1 virtio_console: Add support for multiple ports for generic guest and host communication

In reply to Amit Shah

Expose multiple char devices ("ports") for simple communication between the host userspace and guest.

Sample offline usages can be: poking around in a guest to find out the file systems used, applications installed, etc. Online usages can be sharing of clipboard data between the guest and the host, sending information about logged-in users to the host, locking the screen or session when a vnc session is closed, and so on.

The interface presented to guest userspace is of a simple char device, so it can be used like this:

    fd = open("/dev/vcon2", O_RDWR);
    ret = read(fd, buf, 100);
    ret = write(fd, string, strlen(string));

Each port is to be assigned a unique function, for example, the first 4 ports may be reserved for libvirt usage, the next 4 for generic streaming data and so on. This port-function mapping isn't finalised yet.

For requirements, use-cases and some history see

http://www.linux-kvm.org/page/VMchannel_Requirements

---
 drivers/char/Kconfig           |    4 +-
 drivers/char/virtio_console.c  | 1069 +++++++++++++++++++++++++++++++++++-----
 include/linux/virtio_console.h |   60 +++-
 3 files changed, 994 insertions(+), 139 deletions(-)
drivers/char/Kconfig +3 -1
index b8368e2..cbbb9a1 100644
@@ -678,7 +678,9 @@ config VIRTIO_CONSOLE
 	select HVC_DRIVER
 	help
 	  Virtio console for use with lguest and other hypervisors.
-
+	  Also serves as a general-purpose serial device for data transfer
+	  between the guest and host. Character devices at /dev/vconNN will
+	  be created when corresponding ports are found.
 
 config HVCS
 	tristate "IBM Hypervisor Virtual Console Server support"
drivers/char/virtio_console.c +933 -136

1,177 lines not on this page · raw message

include/linux/virtio_console.h +58 -2

78 lines not on this page · raw message

Signature · 1 line
1.6.2.5
author
#3 lore raw

PATCH 1/5 char: Emit 'OPENED' events on char device open

Notify users of the char interface whenever the file / connection is opened.

The existing RESET event is triggered when the qemu char state is reset as well; which may not be as interesting as char device open events.

---
 qemu-char.c |   14 +++++++++++++-
 qemu-char.h |    1 +
 2 files changed, 14 insertions(+), 1 deletions(-)
qemu-char.c +13 -1
index c25ed1c..5fe882a 100644
@@ -474,6 +474,8 @@ static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
     chr->chr_write = mux_chr_write;
     chr->chr_update_read_handler = mux_chr_update_read_handler;
     chr->chr_accept_input = mux_chr_accept_input;
+
+    qemu_chr_event(chr, CHR_EVENT_OPENED);
     return chr;
 }
 
@@ -622,6 +624,7 @@ static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
     chr->chr_close = fd_chr_close;
 
     qemu_chr_reset(chr);
+    qemu_chr_event(chr, CHR_EVENT_OPENED);
 
     return chr;
 }
@@ -913,8 +916,10 @@ static void pty_chr_state(CharDriverState *chr, int connected)
          * the virtual device linked to our pty. */
         qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
     } else {
-        if (!s->connected)
+        if (!s->connected) {
             qemu_chr_reset(chr);
+            qemu_chr_event(chr, CHR_EVENT_OPENED);
+        }
         s->connected = 1;
     }
 }
@@ -1152,6 +1157,7 @@ static CharDriverState *qemu_chr_open_tty(const char *filename)
     }
     chr->chr_ioctl = tty_serial_ioctl;
     qemu_chr_reset(chr);
+    qemu_chr_event(chr, CHR_EVENT_OPENED);
     return chr;
 }
 #else  /* ! __linux__ && ! __sun__ */
@@ -1297,6 +1303,7 @@ static CharDriverState *qemu_chr_open_pp(const char *filename)
     chr->opaque = drv;
 
     qemu_chr_reset(chr);
+    qemu_chr_event(chr, CHR_EVENT_OPENED);
 
     return chr;
 }
@@ -1576,6 +1583,7 @@ static CharDriverState *qemu_chr_open_win(const char *filename)
         return NULL;
     }
     qemu_chr_reset(chr);
+    qemu_chr_event(chr, CHR_EVENT_OPENED);
     return chr;
 }
 
@@ -1675,6 +1683,7 @@ static CharDriverState *qemu_chr_open_win_pipe(const char *filename)
         return NULL;
     }
     qemu_chr_reset(chr);
+    qemu_chr_event(chr, CHR_EVENT_OPENED);
     return chr;
 }
 
@@ -1689,6 +1698,7 @@ static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
     chr->opaque = s;
     chr->chr_write = win_chr_write;
     qemu_chr_reset(chr);
+    qemu_chr_event(chr, CHR_EVENT_OPENED);
     return chr;
 }
 
@@ -1823,6 +1833,7 @@ static CharDriverState *qemu_chr_open_udp(const char *def)
     chr->chr_write = udp_chr_write;
     chr->chr_update_read_handler = udp_chr_update_read_handler;
     chr->chr_close = udp_chr_close;
+    qemu_chr_event(chr, CHR_EVENT_OPENED);
     return chr;
 
 return_err:
@@ -2029,6 +2040,7 @@ static void tcp_chr_connect(void *opaque)
     qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
                          tcp_chr_read, NULL, chr);
     qemu_chr_reset(chr);
+    qemu_chr_event(chr, CHR_EVENT_OPENED);
 }
 
 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
qemu-char.h +1 -0
index df620bc..d725d48 100644
@@ -12,6 +12,7 @@
 #define CHR_EVENT_MUX_IN  3 /* mux-focus was set to this terminal */
 #define CHR_EVENT_MUX_OUT 4 /* mux-focus will move on */
 #define CHR_EVENT_CLOSED  5 /* connection closed */
+#define CHR_EVENT_OPENED  6 /* connection opened */
 
 
 #define CHR_IOCTL_SERIAL_SET_PARAMS   1
Signature · 1 line
1.6.2.5
author
#4 lore raw

PATCH 1/5 char: Emit 'OPENED' events on char device open

In reply to Amit Shah

Notify users of the char interface whenever the file / connection is opened.

The existing RESET event is triggered when the qemu char state is reset as well; which may not be as interesting as char device open events.

---
 qemu-char.c |   14 +++++++++++++-
 qemu-char.h |    1 +
 2 files changed, 14 insertions(+), 1 deletions(-)
qemu-char.c +13 -1
index c25ed1c..5fe882a 100644
@@ -474,6 +474,8 @@ static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
     chr->chr_write = mux_chr_write;
     chr->chr_update_read_handler = mux_chr_update_read_handler;
     chr->chr_accept_input = mux_chr_accept_input;
+
+    qemu_chr_event(chr, CHR_EVENT_OPENED);
     return chr;
 }
 
@@ -622,6 +624,7 @@ static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
     chr->chr_close = fd_chr_close;
 
     qemu_chr_reset(chr);
+    qemu_chr_event(chr, CHR_EVENT_OPENED);
 
     return chr;
 }
@@ -913,8 +916,10 @@ static void pty_chr_state(CharDriverState *chr, int connected)
          * the virtual device linked to our pty. */
         qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
     } else {
-        if (!s->connected)
+        if (!s->connected) {
             qemu_chr_reset(chr);
+            qemu_chr_event(chr, CHR_EVENT_OPENED);
+        }
         s->connected = 1;
     }
 }
@@ -1152,6 +1157,7 @@ static CharDriverState *qemu_chr_open_tty(const char *filename)
     }
     chr->chr_ioctl = tty_serial_ioctl;
     qemu_chr_reset(chr);
+    qemu_chr_event(chr, CHR_EVENT_OPENED);
     return chr;
 }
 #else  /* ! __linux__ && ! __sun__ */
@@ -1297,6 +1303,7 @@ static CharDriverState *qemu_chr_open_pp(const char *filename)
     chr->opaque = drv;
 
     qemu_chr_reset(chr);
+    qemu_chr_event(chr, CHR_EVENT_OPENED);
 
     return chr;
 }
@@ -1576,6 +1583,7 @@ static CharDriverState *qemu_chr_open_win(const char *filename)
         return NULL;
     }
     qemu_chr_reset(chr);
+    qemu_chr_event(chr, CHR_EVENT_OPENED);
     return chr;
 }
 
@@ -1675,6 +1683,7 @@ static CharDriverState *qemu_chr_open_win_pipe(const char *filename)
         return NULL;
     }
     qemu_chr_reset(chr);
+    qemu_chr_event(chr, CHR_EVENT_OPENED);
     return chr;
 }
 
@@ -1689,6 +1698,7 @@ static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
     chr->opaque = s;
     chr->chr_write = win_chr_write;
     qemu_chr_reset(chr);
+    qemu_chr_event(chr, CHR_EVENT_OPENED);
     return chr;
 }
 
@@ -1823,6 +1833,7 @@ static CharDriverState *qemu_chr_open_udp(const char *def)
     chr->chr_write = udp_chr_write;
     chr->chr_update_read_handler = udp_chr_update_read_handler;
     chr->chr_close = udp_chr_close;
+    qemu_chr_event(chr, CHR_EVENT_OPENED);
     return chr;
 
 return_err:
@@ -2029,6 +2040,7 @@ static void tcp_chr_connect(void *opaque)
     qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
                          tcp_chr_read, NULL, chr);
     qemu_chr_reset(chr);
+    qemu_chr_event(chr, CHR_EVENT_OPENED);
 }
 
 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
qemu-char.h +1 -0
index df620bc..d725d48 100644
@@ -12,6 +12,7 @@
 #define CHR_EVENT_MUX_IN  3 /* mux-focus was set to this terminal */
 #define CHR_EVENT_MUX_OUT 4 /* mux-focus will move on */
 #define CHR_EVENT_CLOSED  5 /* connection closed */
+#define CHR_EVENT_OPENED  6 /* connection opened */
 
 
 #define CHR_IOCTL_SERIAL_SET_PARAMS   1
Signature · 1 line
1.6.2.5
author
#5 lore raw

PATCH 2/5 virtio-console: Add support for multiple ports for generic guest-host communication

This interface extends the virtio-console device to handle multiple ports that present a char device from which bits can be sent and read.

Sample uses for such a device can be obtaining info from the guest like the file systems used, apps installed, etc. for offline usage and logged-in users, clipboard copy-paste, etc. for online usage.

Each port is to be assigned a unique function, for example, the first 4 ports may be reserved for libvirt usage, the next 4 for generic streaming data and so on. This port-function mapping isn't finalised yet.

For requirements, use-cases and some history see

http://www.linux-kvm.org/page/VMchannel_Requirements

---
 hw/pc.c             |   16 +-
 hw/virtio-console.c |  631 ++++++++++++++++++++++++++++++++++++++++++++++----
 hw/virtio-console.h |   53 +++++
 monitor.c           |    7 +
 qemu-monitor.hx     |   10 +
 qemu-options.hx     |    2 +-
 sysemu.h            |   10 +-
 vl.c                |   41 ++--
 8 files changed, 690 insertions(+), 80 deletions(-)
hw/pc.c +11 -5
index a84b2d3..46751e6 100644
@@ -1459,11 +1459,17 @@ static void pc_init1(ram_addr_t ram_size,
     }
 
     /* Add virtio console devices */
-    if (pci_enabled) {
-        for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
-            if (virtcon_hds[i]) {
-                pci_create_simple(pci_bus, -1, "virtio-console-pci");
-            }
+    if (pci_enabled && virtcon_nr_ports) {
+        void *dev;
+
+        dev = pci_create_simple(pci_bus, -1, "virtio-console-pci");
+        if (!dev) {
+            fprintf(stderr, "qemu: could not create virtio console pci device\n");
+            exit(1);
+        }
+
+        for (i = 0; i < virtcon_nr_ports; i++) {
+                virtio_console_new_port(dev, virtcon_idx[i]);
         }
     }
 
hw/virtio-console.c +580 -51

722 lines not on this page · raw message

hw/virtio-console.h +53 -0

69 lines not on this page · raw message

monitor.c +7 -0
index 9839371..5aaeb74 100644
@@ -45,6 +45,7 @@
 #include "kvm.h"
 #include "acl.h"
 #include "exec-all.h"
+#include "hw/virtio-console.h"
 
 #include "qemu-kvm.h"
 
@@ -1779,6 +1780,12 @@ int monitor_get_fd(Monitor *mon, const char *fdname)
     return -1;
 }
 
+static void do_virtio_console_action(Monitor *mon,
+                                     const char *command, const char *param)
+{
+    virtio_console_monitor_command(mon, command, param);
+}
+
 static const mon_cmd_t mon_cmds[] = {
 #include "qemu-monitor.h"
     { NULL, NULL, },
qemu-monitor.hx +10 -0
index c193a1e..905cb4c 100644
@@ -572,6 +572,16 @@ STEXI
 Change watchdog action.
 ETEXI
 
+    { "virtio-console", "ss?", do_virtio_console_action,
+      "<command> [<parameters>]\n",
+      "virtio-serial write port=3,key=get,value=clipboard\n"
+      "virtio-serial add_port\n"
+      "virtio-serial add_port port=6,name=foo,protocol=keyvalue\n" },
+STEXI
+@item virtio-console
+Hot-add ports or send data to virtio-console port
+ETEXI
+
     { "acl_show", "s", do_acl_show, "aclname",
       "list rules in the access control list" },
 STEXI
qemu-options.hx +1 -1
index c1ec976..e2b8e36 100644
@@ -1596,7 +1596,7 @@ ETEXI
 
 DEF("virtioconsole", HAS_ARG, QEMU_OPTION_virtiocon, \
     "-virtioconsole c\n" \
-    "                set virtio console\n")
+    "                define virtio console\n")
 STEXI
 @item -virtioconsole @var{c}
 Set virtio console.
sysemu.h +8 -2
index 61c927a..f267058 100644
@@ -2,6 +2,7 @@
 #define SYSEMU_H
 /* Misc. things related to the system emulator.  */
 
+#include <stdbool.h>
 #include "qemu-common.h"
 #include "qemu-option.h"
 #include "sys-queue.h"
@@ -233,9 +234,14 @@ extern CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
 
 /* virtio consoles */
 
-#define MAX_VIRTIO_CONSOLES 1
+#define MAX_VIRTIO_CONSOLE_PORTS 64 /* Should be a multiple of 32 */
+#define VIRTIO_CONSOLE_PROTO_MAX_LEN 30
 
-extern CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
+extern CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLE_PORTS];
+extern uint32_t virtcon_idx[MAX_VIRTIO_CONSOLE_PORTS];
+extern int virtcon_nr_ports;
+extern int init_virtio_console_port(int port, const char *opts);
+extern void *virtio_console_new_port(PCIDevice *dev, uint32_t idx);
 
 #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
 
vl.c +20 -21

84 lines not on this page · raw message

Signature · 1 line
1.6.2.5
author
#6 lore raw

PATCH 2/5 virtio-console: Add support for multiple ports for generic guest-host communication

In reply to Amit Shah

This interface extends the virtio-console device to handle multiple ports that present a char device from which bits can be sent and read.

Sample uses for such a device can be obtaining info from the guest like the file systems used, apps installed, etc. for offline usage and logged-in users, clipboard copy-paste, etc. for online usage.

Each port is to be assigned a unique function, for example, the first 4 ports may be reserved for libvirt usage, the next 4 for generic streaming data and so on. This port-function mapping isn't finalised yet.

For requirements, use-cases and some history see

http://www.linux-kvm.org/page/VMchannel_Requirements

---
 hw/pc.c             |   16 +-
 hw/virtio-console.c |  631 ++++++++++++++++++++++++++++++++++++++++++++++----
 hw/virtio-console.h |   53 +++++
 monitor.c           |    7 +
 qemu-monitor.hx     |   10 +
 qemu-options.hx     |    2 +-
 sysemu.h            |   10 +-
 vl.c                |   41 ++--
 8 files changed, 690 insertions(+), 80 deletions(-)
hw/pc.c +11 -5
index a84b2d3..46751e6 100644
@@ -1459,11 +1459,17 @@ static void pc_init1(ram_addr_t ram_size,
     }
 
     /* Add virtio console devices */
-    if (pci_enabled) {
-        for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
-            if (virtcon_hds[i]) {
-                pci_create_simple(pci_bus, -1, "virtio-console-pci");
-            }
+    if (pci_enabled && virtcon_nr_ports) {
+        void *dev;
+
+        dev = pci_create_simple(pci_bus, -1, "virtio-console-pci");
+        if (!dev) {
+            fprintf(stderr, "qemu: could not create virtio console pci device\n");
+            exit(1);
+        }
+
+        for (i = 0; i < virtcon_nr_ports; i++) {
+                virtio_console_new_port(dev, virtcon_idx[i]);
         }
     }
 
hw/virtio-console.c +580 -51

722 lines not on this page · raw message

hw/virtio-console.h +53 -0

69 lines not on this page · raw message

monitor.c +7 -0
index 9839371..5aaeb74 100644
@@ -45,6 +45,7 @@
 #include "kvm.h"
 #include "acl.h"
 #include "exec-all.h"
+#include "hw/virtio-console.h"
 
 #include "qemu-kvm.h"
 
@@ -1779,6 +1780,12 @@ int monitor_get_fd(Monitor *mon, const char *fdname)
     return -1;
 }
 
+static void do_virtio_console_action(Monitor *mon,
+                                     const char *command, const char *param)
+{
+    virtio_console_monitor_command(mon, command, param);
+}
+
 static const mon_cmd_t mon_cmds[] = {
 #include "qemu-monitor.h"
     { NULL, NULL, },
qemu-monitor.hx +10 -0
index c193a1e..905cb4c 100644
@@ -572,6 +572,16 @@ STEXI
 Change watchdog action.
 ETEXI
 
+    { "virtio-console", "ss?", do_virtio_console_action,
+      "<command> [<parameters>]\n",
+      "virtio-serial write port=3,key=get,value=clipboard\n"
+      "virtio-serial add_port\n"
+      "virtio-serial add_port port=6,name=foo,protocol=keyvalue\n" },
+STEXI
+@item virtio-console
+Hot-add ports or send data to virtio-console port
+ETEXI
+
     { "acl_show", "s", do_acl_show, "aclname",
       "list rules in the access control list" },
 STEXI
qemu-options.hx +1 -1
index c1ec976..e2b8e36 100644
@@ -1596,7 +1596,7 @@ ETEXI
 
 DEF("virtioconsole", HAS_ARG, QEMU_OPTION_virtiocon, \
     "-virtioconsole c\n" \
-    "                set virtio console\n")
+    "                define virtio console\n")
 STEXI
 @item -virtioconsole @var{c}
 Set virtio console.
sysemu.h +8 -2
index 61c927a..f267058 100644
@@ -2,6 +2,7 @@
 #define SYSEMU_H
 /* Misc. things related to the system emulator.  */
 
+#include <stdbool.h>
 #include "qemu-common.h"
 #include "qemu-option.h"
 #include "sys-queue.h"
@@ -233,9 +234,14 @@ extern CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
 
 /* virtio consoles */
 
-#define MAX_VIRTIO_CONSOLES 1
+#define MAX_VIRTIO_CONSOLE_PORTS 64 /* Should be a multiple of 32 */
+#define VIRTIO_CONSOLE_PROTO_MAX_LEN 30
 
-extern CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
+extern CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLE_PORTS];
+extern uint32_t virtcon_idx[MAX_VIRTIO_CONSOLE_PORTS];
+extern int virtcon_nr_ports;
+extern int init_virtio_console_port(int port, const char *opts);
+extern void *virtio_console_new_port(PCIDevice *dev, uint32_t idx);
 
 #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
 
vl.c +20 -21

84 lines not on this page · raw message

Signature · 1 line
1.6.2.5
author
#7 lore raw

PATCH 3/5 virtio-console: in-qemu api for open/read/write/close ports

This is a simple-to-use api for opening a port, registering a callback for reading stuff, writing to a port and closing it.

Another api for hot-adding a port can be provided.

---
 hw/virtio-console.c |   61 ++++++++++++++++++++++++++++++++++++++++++++++++--
 hw/virtio-console.h |    5 ++++
 2 files changed, 63 insertions(+), 3 deletions(-)
hw/virtio-console.c +58 -3
index 135fdbb..9e78a9e 100644
@@ -52,6 +52,11 @@ struct VirtIOConsolePort {
 
     TAILQ_HEAD(, VirtIOConsolePortBuffer) unflushed_buffer_head;
 
+    /* Callback that's invoked when we have a buffer that can be consumed
+     * by an in-qemu user of this port
+     */
+    size_t (*read_callback)(const uint8_t *buf, const size_t len);
+
     bool guest_connected;
     bool host_connected;
 };
@@ -150,10 +155,15 @@ static bool has_complete_data(VirtIOConsolePort *port)
 
 static size_t flush_buf(VirtIOConsolePort *port, const uint8_t *buf, size_t len)
 {
-    if (!port->hd) {
-        return 0;
+    int ret;
+
+    ret = 0;
+    if (port->read_callback) {
+        ret = port->read_callback(buf, len);
+    } else if (port->hd) {
+        ret = qemu_chr_write(port->hd, buf, len);
     }
-    return qemu_chr_write(port->hd, buf, len);
+    return ret;
 }
 
 static void flush_queue(VirtIOConsolePort *port)
@@ -431,6 +441,51 @@ static void vcon_event(void *opaque, int event)
     send_control_event(port, &cpkt);
 }
 
+/* Functions for use inside qemu to open and read from/write to ports */
+VirtIOConsolePort *virtio_console_open(uint32_t id,
+                                       size_t(*read_callback)(const uint8_t*buf,
+                                                              const size_t len))
+{
+    VirtIOConsolePort *port = get_port_from_id(id);
+    struct virtio_console_control cpkt;
+
+    if (port == NULL) {
+        return NULL;
+    }
+    /* Don't allow opening an already-open port */
+    if (port->host_connected) {
+        return NULL;
+    }
+    /* Send port open notification to the guest */
+    port->host_connected = true;
+    port->read_callback = read_callback;
+    cpkt.event = VIRTIO_CONSOLE_PORT_OPEN;
+    cpkt.value = 1;
+    send_control_event(port, &cpkt);
+    return port;
+}
+
+void virtio_console_close(VirtIOConsolePort *port)
+{
+    struct virtio_console_control cpkt;
+
+    if (!port)
+        return;
+
+    port->read_callback = NULL;
+
+    cpkt.event = VIRTIO_CONSOLE_PORT_OPEN;
+    cpkt.value = 0;
+    send_control_event(port, &cpkt);
+}
+
+size_t virtio_console_write(VirtIOConsolePort *port, uint8_t *buf, size_t size)
+{
+    if (!port || !port->host_connected) {
+        return 0;
+    }
+    return write_to_port(port, buf, size, false);
+}
 static void virtio_console_set_port_active(uint32_t idx)
 {
     int i;
hw/virtio-console.h +5 -0
index 56448a9..62d0c4b 100644
@@ -68,5 +68,10 @@ struct virtio_console_header {
 typedef struct VirtIOConsolePort VirtIOConsolePort;
 void virtio_console_monitor_command(Monitor *mon,
                                     const char *command, const char *param);
+VirtIOConsolePort *virtio_console_open(uint32_t id,
+                                       size_t(*read_callback)(const uint8_t*buf,
+                                                              const size_t len));
+void virtio_console_close(VirtIOConsolePort *port);
+size_t virtio_console_write(VirtIOConsolePort *port, uint8_t *buf, size_t size);
 
 #endif
Signature · 1 line
1.6.2.5
author
#8 lore raw

PATCH 3/5 virtio-console: in-qemu api for open/read/write/close ports

In reply to Amit Shah

This is a simple-to-use api for opening a port, registering a callback for reading stuff, writing to a port and closing it.

Another api for hot-adding a port can be provided.

---
 hw/virtio-console.c |   61 ++++++++++++++++++++++++++++++++++++++++++++++++--
 hw/virtio-console.h |    5 ++++
 2 files changed, 63 insertions(+), 3 deletions(-)
hw/virtio-console.c +58 -3
index 135fdbb..9e78a9e 100644
@@ -52,6 +52,11 @@ struct VirtIOConsolePort {
 
     TAILQ_HEAD(, VirtIOConsolePortBuffer) unflushed_buffer_head;
 
+    /* Callback that's invoked when we have a buffer that can be consumed
+     * by an in-qemu user of this port
+     */
+    size_t (*read_callback)(const uint8_t *buf, const size_t len);
+
     bool guest_connected;
     bool host_connected;
 };
@@ -150,10 +155,15 @@ static bool has_complete_data(VirtIOConsolePort *port)
 
 static size_t flush_buf(VirtIOConsolePort *port, const uint8_t *buf, size_t len)
 {
-    if (!port->hd) {
-        return 0;
+    int ret;
+
+    ret = 0;
+    if (port->read_callback) {
+        ret = port->read_callback(buf, len);
+    } else if (port->hd) {
+        ret = qemu_chr_write(port->hd, buf, len);
     }
-    return qemu_chr_write(port->hd, buf, len);
+    return ret;
 }
 
 static void flush_queue(VirtIOConsolePort *port)
@@ -431,6 +441,51 @@ static void vcon_event(void *opaque, int event)
     send_control_event(port, &cpkt);
 }
 
+/* Functions for use inside qemu to open and read from/write to ports */
+VirtIOConsolePort *virtio_console_open(uint32_t id,
+                                       size_t(*read_callback)(const uint8_t*buf,
+                                                              const size_t len))
+{
+    VirtIOConsolePort *port = get_port_from_id(id);
+    struct virtio_console_control cpkt;
+
+    if (port == NULL) {
+        return NULL;
+    }
+    /* Don't allow opening an already-open port */
+    if (port->host_connected) {
+        return NULL;
+    }
+    /* Send port open notification to the guest */
+    port->host_connected = true;
+    port->read_callback = read_callback;
+    cpkt.event = VIRTIO_CONSOLE_PORT_OPEN;
+    cpkt.value = 1;
+    send_control_event(port, &cpkt);
+    return port;
+}
+
+void virtio_console_close(VirtIOConsolePort *port)
+{
+    struct virtio_console_control cpkt;
+
+    if (!port)
+        return;
+
+    port->read_callback = NULL;
+
+    cpkt.event = VIRTIO_CONSOLE_PORT_OPEN;
+    cpkt.value = 0;
+    send_control_event(port, &cpkt);
+}
+
+size_t virtio_console_write(VirtIOConsolePort *port, uint8_t *buf, size_t size)
+{
+    if (!port || !port->host_connected) {
+        return 0;
+    }
+    return write_to_port(port, buf, size, false);
+}
 static void virtio_console_set_port_active(uint32_t idx)
 {
     int i;
hw/virtio-console.h +5 -0
index 56448a9..62d0c4b 100644
@@ -68,5 +68,10 @@ struct virtio_console_header {
 typedef struct VirtIOConsolePort VirtIOConsolePort;
 void virtio_console_monitor_command(Monitor *mon,
                                     const char *command, const char *param);
+VirtIOConsolePort *virtio_console_open(uint32_t id,
+                                       size_t(*read_callback)(const uint8_t*buf,
+                                                              const size_t len));
+void virtio_console_close(VirtIOConsolePort *port);
+size_t virtio_console_write(VirtIOConsolePort *port, uint8_t *buf, size_t size);
 
 #endif
Signature · 1 line
1.6.2.5
author
#9 lore raw

PATCH 4/5 vnc: add a is_vnc_active() helper

This helper is introduced to query the status of vnc.

---
 vnc.c |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)
vnc.c +9 -1
index 5eaef6a..ff2d4a8 100644
@@ -178,9 +178,17 @@ static void do_info_vnc_client(Monitor *mon, VncState *client)
 #endif
 }
 
-void do_info_vnc(Monitor *mon)
+static int is_vnc_active(void)
 {
     if (vnc_display == NULL || vnc_display->display == NULL) {
+        return 0;
+    }
+    return 1;
+}
+
+void do_info_vnc(Monitor *mon)
+{
+    if (!is_vnc_active()) {
         monitor_printf(mon, "Server: disabled\n");
     } else {
         char *serverAddr = vnc_socket_local_addr("     address: %s:%s\n",
Signature · 1 line
1.6.2.5
author
#10 lore raw

PATCH 4/5 vnc: add a is_vnc_active() helper

In reply to Amit Shah

This helper is introduced to query the status of vnc.

---
 vnc.c |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)
vnc.c +9 -1
index 5eaef6a..ff2d4a8 100644
@@ -178,9 +178,17 @@ static void do_info_vnc_client(Monitor *mon, VncState *client)
 #endif
 }
 
-void do_info_vnc(Monitor *mon)
+static int is_vnc_active(void)
 {
     if (vnc_display == NULL || vnc_display->display == NULL) {
+        return 0;
+    }
+    return 1;
+}
+
+void do_info_vnc(Monitor *mon)
+{
+    if (!is_vnc_active()) {
         monitor_printf(mon, "Server: disabled\n");
     } else {
         char *serverAddr = vnc_socket_local_addr("     address: %s:%s\n",
Signature · 1 line
1.6.2.5
author
#11 lore raw

PATCH 5/5 vnc: Send / receive guest clipboard if virtio-console connected to clipboard port

If a connection to the guest clipboard is open, send the host clipboard to the guest and guest clipboard to the host on any change

---
 hw/virtio-console.h |    1 +
 vnc.c               |   33 +++++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 0 deletions(-)
hw/virtio-console.h +1 -0
index 62d0c4b..bd4c5cb 100644
@@ -28,6 +28,7 @@
 /* Port number to function mapping */
 #define VIRTIO_CONSOLE_CONSOLE_PORT	0
 #define VIRTIO_CONSOLE_CONSOLE2_PORT	1
+#define VIRTIO_CONSOLE_CLIPBOARD_PORT	3
 
 /* Features supported */
 #define VIRTIO_CONSOLE_F_MULTIPORT	1
vnc.c +33 -0
index ff2d4a8..19a4cca 100644
@@ -29,6 +29,7 @@
 #include "qemu_socket.h"
 #include "qemu-timer.h"
 #include "acl.h"
+#include "hw/virtio-console.h"
 
 #define VNC_REFRESH_INTERVAL_BASE 30
 #define VNC_REFRESH_INTERVAL_INC  50
@@ -47,6 +48,7 @@
 
 static VncDisplay *vnc_display; /* needed for info vnc */
 static DisplayChangeListener *dcl;
+static VirtIOConsolePort *virtcon_port;
 
 static char *addr_to_string(const char *format,
                             struct sockaddr_storage *sa,
@@ -671,6 +673,32 @@ static void vnc_copy(VncState *vs, int src_x, int src_y, int dst_x, int dst_y, i
     vnc_flush(vs);
 }
 
+static size_t vnc_clipboard_data_from_guest(const uint8_t *buf, size_t len)
+{
+    VncState *vs;
+    VncDisplay *vd;
+    DisplayState *ds;
+
+    if (!is_vnc_active())
+        return 0;
+
+    ds = vnc_display->ds;
+    vd = ds->opaque;
+
+    for (vs = vd->clients; vs; vs = vs->next) {
+        vnc_write_u8(vs, 3);  /* ServerCutText */
+        vnc_write_u8(vs, 0);  /* Padding */
+        vnc_write_u8(vs, 0);  /* Padding */
+        vnc_write_u8(vs, 0);  /* Padding */
+        vnc_write_u32(vs, len);
+        while (len--) {
+            vnc_write_u8(vs, *(buf++));
+        }
+        vnc_flush(vs);
+    }
+    return len;
+}
+
 static void vnc_dpy_copy(DisplayState *ds, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
 {
     VncDisplay *vd = ds->opaque;
@@ -1240,6 +1268,7 @@ uint32_t read_u32(uint8_t *data, size_t offset)
 
 static void client_cut_text(VncState *vs, size_t len, uint8_t *text)
 {
+    virtio_console_write(virtcon_port, text, len);
 }
 
 static void check_pointer_type_change(VncState *vs, int absolute)
@@ -2265,6 +2294,7 @@ void vnc_display_close(DisplayState *ds)
     vs->subauth = VNC_AUTH_INVALID;
     vs->tls.x509verify = 0;
 #endif
+    virtio_console_close(virtcon_port);
 }
 
 int vnc_display_password(DisplayState *ds, const char *password)
@@ -2514,5 +2544,8 @@ int vnc_display_open(DisplayState *ds, const char *display)
             vs->display = dpy;
         }
     }
+
+    virtcon_port = virtio_console_open(VIRTIO_CONSOLE_CLIPBOARD_PORT,
+                                       vnc_clipboard_data_from_guest);
     return qemu_set_fd_handler2(vs->lsock, NULL, vnc_listen_read, NULL, vs);
 }
Signature · 1 line
1.6.2.5
author
#12 lore raw

PATCH 5/5 vnc: Send / receive guest clipboard if virtio-console connected to clipboard port

In reply to Amit Shah

If a connection to the guest clipboard is open, send the host clipboard to the guest and guest clipboard to the host on any change

---
 hw/virtio-console.h |    1 +
 vnc.c               |   33 +++++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 0 deletions(-)
hw/virtio-console.h +1 -0
index 62d0c4b..bd4c5cb 100644
@@ -28,6 +28,7 @@
 /* Port number to function mapping */
 #define VIRTIO_CONSOLE_CONSOLE_PORT	0
 #define VIRTIO_CONSOLE_CONSOLE2_PORT	1
+#define VIRTIO_CONSOLE_CLIPBOARD_PORT	3
 
 /* Features supported */
 #define VIRTIO_CONSOLE_F_MULTIPORT	1
vnc.c +33 -0
index ff2d4a8..19a4cca 100644
@@ -29,6 +29,7 @@
 #include "qemu_socket.h"
 #include "qemu-timer.h"
 #include "acl.h"
+#include "hw/virtio-console.h"
 
 #define VNC_REFRESH_INTERVAL_BASE 30
 #define VNC_REFRESH_INTERVAL_INC  50
@@ -47,6 +48,7 @@
 
 static VncDisplay *vnc_display; /* needed for info vnc */
 static DisplayChangeListener *dcl;
+static VirtIOConsolePort *virtcon_port;
 
 static char *addr_to_string(const char *format,
                             struct sockaddr_storage *sa,
@@ -671,6 +673,32 @@ static void vnc_copy(VncState *vs, int src_x, int src_y, int dst_x, int dst_y, i
     vnc_flush(vs);
 }
 
+static size_t vnc_clipboard_data_from_guest(const uint8_t *buf, size_t len)
+{
+    VncState *vs;
+    VncDisplay *vd;
+    DisplayState *ds;
+
+    if (!is_vnc_active())
+        return 0;
+
+    ds = vnc_display->ds;
+    vd = ds->opaque;
+
+    for (vs = vd->clients; vs; vs = vs->next) {
+        vnc_write_u8(vs, 3);  /* ServerCutText */
+        vnc_write_u8(vs, 0);  /* Padding */
+        vnc_write_u8(vs, 0);  /* Padding */
+        vnc_write_u8(vs, 0);  /* Padding */
+        vnc_write_u32(vs, len);
+        while (len--) {
+            vnc_write_u8(vs, *(buf++));
+        }
+        vnc_flush(vs);
+    }
+    return len;
+}
+
 static void vnc_dpy_copy(DisplayState *ds, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
 {
     VncDisplay *vd = ds->opaque;
@@ -1240,6 +1268,7 @@ uint32_t read_u32(uint8_t *data, size_t offset)
 
 static void client_cut_text(VncState *vs, size_t len, uint8_t *text)
 {
+    virtio_console_write(virtcon_port, text, len);
 }
 
 static void check_pointer_type_change(VncState *vs, int absolute)
@@ -2265,6 +2294,7 @@ void vnc_display_close(DisplayState *ds)
     vs->subauth = VNC_AUTH_INVALID;
     vs->tls.x509verify = 0;
 #endif
+    virtio_console_close(virtcon_port);
 }
 
 int vnc_display_password(DisplayState *ds, const char *password)
@@ -2514,5 +2544,8 @@ int vnc_display_open(DisplayState *ds, const char *display)
             vs->display = dpy;
         }
     }
+
+    virtcon_port = virtio_console_open(VIRTIO_CONSOLE_CLIPBOARD_PORT,
+                                       vnc_clipboard_data_from_guest);
     return qemu_set_fd_handler2(vs->lsock, NULL, vnc_listen_read, NULL, vs);
 }
Signature · 1 line
1.6.2.5
author
#13 lore raw
In reply to Amit Shah
Quoted 27 lines · Amit Shah

Hello all,

Here is a new iteration of the patch series that implements a transport for guest and host communications.

I've tested for compatibility (old qemu & new kernel, new qemu & old kernel, new qemu & new kernel) and it all works fine*. Migration works with the patch Juan just posted.

There are a few items on my todo list but this works well.

New since last send:
- migrate per-port buffers that are queued up
- in-qemu api for open/close/read/write of ports.
- the read() is a callback that's invoked when complete data corresponding to one write() request on the guest is available.
- removed comments from virtio_console.c that are no longer relevant.
- address review comments by Juan

TODO:
- Convert all config writes to little endian in qemu / convert from little endian to host endian in guest
- Address a few FIXMEs spread in the code
- Introduce a watermark to stop a rogue host process flooding guest with data

Quoted 4 lines

Conditions:
* Heavy IO on an hvc port and a non-hvc port causes memory corruption each time, same place. It could be realted to locking but I doubt that. This still has to be sorted out

I can't reproduce this badness anymore with the linux-next kernel in the guest.

So I request the maintainers to please review this patchset for and consider for inclusion.

Thanks,
Amit

author
#14 lore raw
In reply to Amit Shah
Quoted 27 lines · Amit Shah

Hello all,

Here is a new iteration of the patch series that implements a transport for guest and host communications.

I've tested for compatibility (old qemu & new kernel, new qemu & old kernel, new qemu & new kernel) and it all works fine*. Migration works with the patch Juan just posted.

There are a few items on my todo list but this works well.

New since last send:
- migrate per-port buffers that are queued up
- in-qemu api for open/close/read/write of ports.
- the read() is a callback that's invoked when complete data corresponding to one write() request on the guest is available.
- removed comments from virtio_console.c that are no longer relevant.
- address review comments by Juan

TODO:
- Convert all config writes to little endian in qemu / convert from little endian to host endian in guest
- Address a few FIXMEs spread in the code
- Introduce a watermark to stop a rogue host process flooding guest with data

Quoted 4 lines

Conditions:
* Heavy IO on an hvc port and a non-hvc port causes memory corruption each time, same place. It could be realted to locking but I doubt that. This still has to be sorted out

I can't reproduce this badness anymore with the linux-next kernel in the guest.

So I request the maintainers to please review this patchset for and consider for inclusion.

Thanks,
Amit