qemu

FORK: QEMU emulator
git clone https://git.neptards.moe/neptards/qemu.git
Log | Files | Refs | Submodules | LICENSE

virtio-serial.h (6365B)


      1 /*
      2  * Virtio Serial / Console Support
      3  *
      4  * Copyright IBM, Corp. 2008
      5  * Copyright Red Hat, Inc. 2009, 2010
      6  *
      7  * Authors:
      8  *  Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
      9  *  Amit Shah <amit.shah@redhat.com>
     10  *
     11  * This work is licensed under the terms of the GNU GPL, version 2.  See
     12  * the COPYING file in the top-level directory.
     13  *
     14  */
     15 
     16 #ifndef QEMU_VIRTIO_SERIAL_H
     17 #define QEMU_VIRTIO_SERIAL_H
     18 
     19 #include "standard-headers/linux/virtio_console.h"
     20 #include "hw/virtio/virtio.h"
     21 #include "qom/object.h"
     22 
     23 struct virtio_serial_conf {
     24     /* Max. number of ports we can have for a virtio-serial device */
     25     uint32_t max_virtserial_ports;
     26 };
     27 
     28 #define TYPE_VIRTIO_SERIAL_PORT "virtio-serial-port"
     29 OBJECT_DECLARE_TYPE(VirtIOSerialPort, VirtIOSerialPortClass,
     30                     VIRTIO_SERIAL_PORT)
     31 
     32 typedef struct VirtIOSerial VirtIOSerial;
     33 
     34 #define TYPE_VIRTIO_SERIAL_BUS "virtio-serial-bus"
     35 OBJECT_DECLARE_SIMPLE_TYPE(VirtIOSerialBus, VIRTIO_SERIAL_BUS)
     36 
     37 
     38 struct VirtIOSerialPortClass {
     39     DeviceClass parent_class;
     40 
     41     /* Is this a device that binds with hvc in the guest? */
     42     bool is_console;
     43 
     44     /*
     45      * The per-port (or per-app) realize function that's called when a
     46      * new device is found on the bus.
     47      */
     48     DeviceRealize realize;
     49     /*
     50      * Per-port unrealize function that's called when a port gets
     51      * hot-unplugged or removed.
     52      */
     53     DeviceUnrealize unrealize;
     54 
     55     /* Callbacks for guest events */
     56         /* Guest opened/closed device. */
     57     void (*set_guest_connected)(VirtIOSerialPort *port, int guest_connected);
     58 
     59     /* Enable/disable backend for virtio serial port */
     60     void (*enable_backend)(VirtIOSerialPort *port, bool enable);
     61 
     62         /* Guest is now ready to accept data (virtqueues set up). */
     63     void (*guest_ready)(VirtIOSerialPort *port);
     64 
     65         /*
     66          * Guest has enqueued a buffer for the host to write into.
     67          * Called each time a buffer is enqueued by the guest;
     68          * irrespective of whether there already were free buffers the
     69          * host could have consumed.
     70          *
     71          * This is dependent on both the guest and host end being
     72          * connected.
     73          */
     74     void (*guest_writable)(VirtIOSerialPort *port);
     75 
     76     /*
     77      * Guest wrote some data to the port. This data is handed over to
     78      * the app via this callback.  The app can return a size less than
     79      * 'len'.  In this case, throttling will be enabled for this port.
     80      */
     81     ssize_t (*have_data)(VirtIOSerialPort *port, const uint8_t *buf,
     82                          ssize_t len);
     83 };
     84 
     85 /*
     86  * This is the state that's shared between all the ports.  Some of the
     87  * state is configurable via command-line options. Some of it can be
     88  * set by individual devices in their initfn routines. Some of the
     89  * state is set by the generic qdev device init routine.
     90  */
     91 struct VirtIOSerialPort {
     92     DeviceState dev;
     93 
     94     QTAILQ_ENTRY(VirtIOSerialPort) next;
     95 
     96     /*
     97      * This field gives us the virtio device as well as the qdev bus
     98      * that we are associated with
     99      */
    100     VirtIOSerial *vser;
    101 
    102     VirtQueue *ivq, *ovq;
    103 
    104     /*
    105      * This name is sent to the guest and exported via sysfs.
    106      * The guest could create symlinks based on this information.
    107      * The name is in the reverse fqdn format, like org.qemu.console.0
    108      */
    109     char *name;
    110 
    111     /*
    112      * This id helps identify ports between the guest and the host.
    113      * The guest sends a "header" with this id with each data packet
    114      * that it sends and the host can then find out which associated
    115      * device to send out this data to
    116      */
    117     uint32_t id;
    118 
    119     /*
    120      * This is the elem that we pop from the virtqueue.  A slow
    121      * backend that consumes guest data (e.g. the file backend for
    122      * qemu chardevs) can cause the guest to block till all the output
    123      * is flushed.  This isn't desired, so we keep a note of the last
    124      * element popped and continue consuming it once the backend
    125      * becomes writable again.
    126      */
    127     VirtQueueElement *elem;
    128 
    129     /*
    130      * The index and the offset into the iov buffer that was popped in
    131      * elem above.
    132      */
    133     uint32_t iov_idx;
    134     uint64_t iov_offset;
    135 
    136     /*
    137      * When unthrottling we use a bottom-half to call flush_queued_data.
    138      */
    139     QEMUBH *bh;
    140 
    141     /* Is the corresponding guest device open? */
    142     bool guest_connected;
    143     /* Is this device open for IO on the host? */
    144     bool host_connected;
    145     /* Do apps not want to receive data? */
    146     bool throttled;
    147 };
    148 
    149 /* The virtio-serial bus on top of which the ports will ride as devices */
    150 struct VirtIOSerialBus {
    151     BusState qbus;
    152 
    153     /* This is the parent device that provides the bus for ports. */
    154     VirtIOSerial *vser;
    155 
    156     /* The maximum number of ports that can ride on top of this bus */
    157     uint32_t max_nr_ports;
    158 };
    159 
    160 typedef struct VirtIOSerialPostLoad {
    161     QEMUTimer *timer;
    162     uint32_t nr_active_ports;
    163     struct {
    164         VirtIOSerialPort *port;
    165         uint8_t host_connected;
    166     } *connected;
    167 } VirtIOSerialPostLoad;
    168 
    169 struct VirtIOSerial {
    170     VirtIODevice parent_obj;
    171 
    172     VirtQueue *c_ivq, *c_ovq;
    173     /* Arrays of ivqs and ovqs: one per port */
    174     VirtQueue **ivqs, **ovqs;
    175 
    176     VirtIOSerialBus bus;
    177 
    178     QTAILQ_HEAD(, VirtIOSerialPort) ports;
    179 
    180     QLIST_ENTRY(VirtIOSerial) next;
    181 
    182     /* bitmap for identifying active ports */
    183     uint32_t *ports_map;
    184 
    185     struct VirtIOSerialPostLoad *post_load;
    186 
    187     virtio_serial_conf serial;
    188 
    189     uint64_t host_features;
    190 };
    191 
    192 /* Interface to the virtio-serial bus */
    193 
    194 /*
    195  * Open a connection to the port
    196  *   Returns 0 on success (always).
    197  */
    198 int virtio_serial_open(VirtIOSerialPort *port);
    199 
    200 /*
    201  * Close the connection to the port
    202  *   Returns 0 on success (always).
    203  */
    204 int virtio_serial_close(VirtIOSerialPort *port);
    205 
    206 /*
    207  * Send data to Guest
    208  */
    209 ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
    210                             size_t size);
    211 
    212 /*
    213  * Query whether a guest is ready to receive data.
    214  */
    215 size_t virtio_serial_guest_ready(VirtIOSerialPort *port);
    216 
    217 /*
    218  * Flow control: Ports can signal to the virtio-serial core to stop
    219  * sending data or re-start sending data, depending on the 'throttle'
    220  * value here.
    221  */
    222 void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle);
    223 
    224 #define TYPE_VIRTIO_SERIAL "virtio-serial-device"
    225 OBJECT_DECLARE_SIMPLE_TYPE(VirtIOSerial, VIRTIO_SERIAL)
    226 
    227 #endif