qemu/hw/char/virtio-console.c
<<
>>
Prefs
   1/*
   2 * Virtio Console and Generic Serial Port Devices
   3 *
   4 * Copyright Red Hat, Inc. 2009, 2010
   5 *
   6 * Authors:
   7 *  Amit Shah <amit.shah@redhat.com>
   8 *
   9 * This work is licensed under the terms of the GNU GPL, version 2.  See
  10 * the COPYING file in the top-level directory.
  11 */
  12
  13#include "qemu/osdep.h"
  14#include "chardev/char-fe.h"
  15#include "qemu/error-report.h"
  16#include "trace.h"
  17#include "hw/virtio/virtio-serial.h"
  18#include "qapi/error.h"
  19#include "qapi/qapi-events-char.h"
  20
  21#define TYPE_VIRTIO_CONSOLE_SERIAL_PORT "virtserialport"
  22#define VIRTIO_CONSOLE(obj) \
  23    OBJECT_CHECK(VirtConsole, (obj), TYPE_VIRTIO_CONSOLE_SERIAL_PORT)
  24
  25typedef struct VirtConsole {
  26    VirtIOSerialPort parent_obj;
  27
  28    CharBackend chr;
  29    guint watch;
  30} VirtConsole;
  31
  32/*
  33 * Callback function that's called from chardevs when backend becomes
  34 * writable.
  35 */
  36static gboolean chr_write_unblocked(GIOChannel *chan, GIOCondition cond,
  37                                    void *opaque)
  38{
  39    VirtConsole *vcon = opaque;
  40
  41    vcon->watch = 0;
  42    virtio_serial_throttle_port(VIRTIO_SERIAL_PORT(vcon), false);
  43    return FALSE;
  44}
  45
  46/* Callback function that's called when the guest sends us data */
  47static ssize_t flush_buf(VirtIOSerialPort *port,
  48                         const uint8_t *buf, ssize_t len)
  49{
  50    VirtConsole *vcon = VIRTIO_CONSOLE(port);
  51    ssize_t ret;
  52
  53    if (!qemu_chr_fe_backend_connected(&vcon->chr)) {
  54        /* If there's no backend, we can just say we consumed all data. */
  55        return len;
  56    }
  57
  58    ret = qemu_chr_fe_write(&vcon->chr, buf, len);
  59    trace_virtio_console_flush_buf(port->id, len, ret);
  60
  61    if (ret < len) {
  62        VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
  63
  64        /*
  65         * Ideally we'd get a better error code than just -1, but
  66         * that's what the chardev interface gives us right now.  If
  67         * we had a finer-grained message, like -EPIPE, we could close
  68         * this connection.
  69         */
  70        if (ret < 0)
  71            ret = 0;
  72
  73        /* XXX we should be queuing data to send later for the
  74         * console devices too rather than silently dropping
  75         * console data on EAGAIN. The Linux virtio-console
  76         * hvc driver though does sends with spinlocks held,
  77         * so if we enable throttling that'll stall the entire
  78         * guest kernel, not merely the process writing to the
  79         * console.
  80         *
  81         * While we could queue data for later write without
  82         * enabling throttling, this would result in the guest
  83         * being able to trigger arbitrary memory usage in QEMU
  84         * buffering data for later writes.
  85         *
  86         * So fixing this problem likely requires fixing the
  87         * Linux virtio-console hvc driver to not hold spinlocks
  88         * while writing, and instead merely block the process
  89         * that's writing. QEMU would then need some way to detect
  90         * if the guest had the fixed driver too, before we can
  91         * use throttling on host side.
  92         */
  93        if (!k->is_console) {
  94            virtio_serial_throttle_port(port, true);
  95            if (!vcon->watch) {
  96                vcon->watch = qemu_chr_fe_add_watch(&vcon->chr,
  97                                                    G_IO_OUT|G_IO_HUP,
  98                                                    chr_write_unblocked, vcon);
  99            }
 100        }
 101    }
 102    return ret;
 103}
 104
 105/* Callback function that's called when the guest opens/closes the port */
 106static void set_guest_connected(VirtIOSerialPort *port, int guest_connected)
 107{
 108    VirtConsole *vcon = VIRTIO_CONSOLE(port);
 109    DeviceState *dev = DEVICE(port);
 110    VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
 111
 112    if (!k->is_console) {
 113        qemu_chr_fe_set_open(&vcon->chr, guest_connected);
 114    }
 115
 116    if (dev->id) {
 117        qapi_event_send_vserport_change(dev->id, guest_connected);
 118    }
 119}
 120
 121static void guest_writable(VirtIOSerialPort *port)
 122{
 123    VirtConsole *vcon = VIRTIO_CONSOLE(port);
 124
 125    qemu_chr_fe_accept_input(&vcon->chr);
 126}
 127
 128/* Readiness of the guest to accept data on a port */
 129static int chr_can_read(void *opaque)
 130{
 131    VirtConsole *vcon = opaque;
 132
 133    return virtio_serial_guest_ready(VIRTIO_SERIAL_PORT(vcon));
 134}
 135
 136/* Send data from a char device over to the guest */
 137static void chr_read(void *opaque, const uint8_t *buf, int size)
 138{
 139    VirtConsole *vcon = opaque;
 140    VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon);
 141
 142    trace_virtio_console_chr_read(port->id, size);
 143    virtio_serial_write(port, buf, size);
 144}
 145
 146static void chr_event(void *opaque, int event)
 147{
 148    VirtConsole *vcon = opaque;
 149    VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon);
 150
 151    trace_virtio_console_chr_event(port->id, event);
 152    switch (event) {
 153    case CHR_EVENT_OPENED:
 154        virtio_serial_open(port);
 155        break;
 156    case CHR_EVENT_CLOSED:
 157        if (vcon->watch) {
 158            g_source_remove(vcon->watch);
 159            vcon->watch = 0;
 160        }
 161        virtio_serial_close(port);
 162        break;
 163    }
 164}
 165
 166static int chr_be_change(void *opaque)
 167{
 168    VirtConsole *vcon = opaque;
 169    VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(vcon);
 170    VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
 171
 172    if (k->is_console) {
 173        qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
 174                                 NULL, chr_be_change, vcon, NULL, true);
 175    } else {
 176        qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
 177                                 chr_event, chr_be_change, vcon, NULL, false);
 178    }
 179
 180    if (vcon->watch) {
 181        g_source_remove(vcon->watch);
 182        vcon->watch = qemu_chr_fe_add_watch(&vcon->chr,
 183                                            G_IO_OUT | G_IO_HUP,
 184                                            chr_write_unblocked, vcon);
 185    }
 186
 187    return 0;
 188}
 189
 190static void virtconsole_enable_backend(VirtIOSerialPort *port, bool enable)
 191{
 192    VirtConsole *vcon = VIRTIO_CONSOLE(port);
 193
 194    if (!qemu_chr_fe_backend_connected(&vcon->chr)) {
 195        return;
 196    }
 197
 198    if (enable) {
 199        VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
 200
 201        qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
 202                                 k->is_console ? NULL : chr_event,
 203                                 chr_be_change, vcon, NULL, false);
 204    } else {
 205        qemu_chr_fe_set_handlers(&vcon->chr, NULL, NULL, NULL,
 206                                 NULL, NULL, NULL, false);
 207    }
 208}
 209
 210static void virtconsole_realize(DeviceState *dev, Error **errp)
 211{
 212    VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
 213    VirtConsole *vcon = VIRTIO_CONSOLE(dev);
 214    VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(dev);
 215
 216    if (port->id == 0 && !k->is_console) {
 217        error_setg(errp, "Port number 0 on virtio-serial devices reserved "
 218                   "for virtconsole devices for backward compatibility.");
 219        return;
 220    }
 221
 222    if (qemu_chr_fe_backend_connected(&vcon->chr)) {
 223        /*
 224         * For consoles we don't block guest data transfer just
 225         * because nothing is connected - we'll just let it go
 226         * whetherever the chardev wants - /dev/null probably.
 227         *
 228         * For serial ports we need 100% reliable data transfer
 229         * so we use the opened/closed signals from chardev to
 230         * trigger open/close of the device
 231         */
 232        if (k->is_console) {
 233            qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
 234                                     NULL, chr_be_change,
 235                                     vcon, NULL, true);
 236            virtio_serial_open(port);
 237        } else {
 238            qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
 239                                     chr_event, chr_be_change,
 240                                     vcon, NULL, false);
 241        }
 242    }
 243}
 244
 245static void virtconsole_unrealize(DeviceState *dev, Error **errp)
 246{
 247    VirtConsole *vcon = VIRTIO_CONSOLE(dev);
 248
 249    if (vcon->watch) {
 250        g_source_remove(vcon->watch);
 251    }
 252}
 253
 254static void virtconsole_class_init(ObjectClass *klass, void *data)
 255{
 256    VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
 257
 258    k->is_console = true;
 259}
 260
 261static const TypeInfo virtconsole_info = {
 262    .name          = "virtconsole",
 263    .parent        = TYPE_VIRTIO_CONSOLE_SERIAL_PORT,
 264    .class_init    = virtconsole_class_init,
 265};
 266
 267static Property virtserialport_properties[] = {
 268    DEFINE_PROP_CHR("chardev", VirtConsole, chr),
 269    DEFINE_PROP_END_OF_LIST(),
 270};
 271
 272static void virtserialport_class_init(ObjectClass *klass, void *data)
 273{
 274    DeviceClass *dc = DEVICE_CLASS(klass);
 275    VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
 276
 277    k->realize = virtconsole_realize;
 278    k->unrealize = virtconsole_unrealize;
 279    k->have_data = flush_buf;
 280    k->set_guest_connected = set_guest_connected;
 281    k->enable_backend = virtconsole_enable_backend;
 282    k->guest_writable = guest_writable;
 283    dc->props = virtserialport_properties;
 284}
 285
 286static const TypeInfo virtserialport_info = {
 287    .name          = TYPE_VIRTIO_CONSOLE_SERIAL_PORT,
 288    .parent        = TYPE_VIRTIO_SERIAL_PORT,
 289    .instance_size = sizeof(VirtConsole),
 290    .class_init    = virtserialport_class_init,
 291};
 292
 293static void virtconsole_register_types(void)
 294{
 295    type_register_static(&virtserialport_info);
 296    type_register_static(&virtconsole_info);
 297}
 298
 299type_init(virtconsole_register_types)
 300