virtio-serial.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #ifndef _QEMU_VIRTIO_SERIAL_H
  16. #define _QEMU_VIRTIO_SERIAL_H
  17. #include "qdev.h"
  18. #include "virtio.h"
  19. /* == Interface shared between the guest kernel and qemu == */
  20. /* The Virtio ID for virtio console / serial ports */
  21. #define VIRTIO_ID_CONSOLE 3
  22. /* Features supported */
  23. #define VIRTIO_CONSOLE_F_MULTIPORT 1
  24. #define VIRTIO_CONSOLE_BAD_ID (~(uint32_t)0)
  25. struct virtio_console_config {
  26. /*
  27. * These two fields are used by VIRTIO_CONSOLE_F_SIZE which
  28. * isn't implemented here yet
  29. */
  30. uint16_t cols;
  31. uint16_t rows;
  32. uint32_t max_nr_ports;
  33. } QEMU_PACKED;
  34. struct virtio_console_control {
  35. uint32_t id; /* Port number */
  36. uint16_t event; /* The kind of control event (see below) */
  37. uint16_t value; /* Extra information for the key */
  38. };
  39. struct virtio_serial_conf {
  40. /* Max. number of ports we can have for a virtio-serial device */
  41. uint32_t max_virtserial_ports;
  42. };
  43. /* Some events for the internal messages (control packets) */
  44. #define VIRTIO_CONSOLE_DEVICE_READY 0
  45. #define VIRTIO_CONSOLE_PORT_ADD 1
  46. #define VIRTIO_CONSOLE_PORT_REMOVE 2
  47. #define VIRTIO_CONSOLE_PORT_READY 3
  48. #define VIRTIO_CONSOLE_CONSOLE_PORT 4
  49. #define VIRTIO_CONSOLE_RESIZE 5
  50. #define VIRTIO_CONSOLE_PORT_OPEN 6
  51. #define VIRTIO_CONSOLE_PORT_NAME 7
  52. /* == In-qemu interface == */
  53. #define TYPE_VIRTIO_SERIAL_PORT "virtio-serial-port"
  54. #define VIRTIO_SERIAL_PORT(obj) \
  55. OBJECT_CHECK(VirtIOSerialPort, (obj), TYPE_VIRTIO_SERIAL_PORT)
  56. #define VIRTIO_SERIAL_PORT_CLASS(klass) \
  57. OBJECT_CLASS_CHECK(VirtIOSerialPortClass, (klass), TYPE_VIRTIO_SERIAL_PORT)
  58. #define VIRTIO_SERIAL_PORT_GET_CLASS(obj) \
  59. OBJECT_GET_CLASS(VirtIOSerialPortClass, (obj), TYPE_VIRTIO_SERIAL_PORT)
  60. typedef struct VirtIOSerial VirtIOSerial;
  61. typedef struct VirtIOSerialBus VirtIOSerialBus;
  62. typedef struct VirtIOSerialPort VirtIOSerialPort;
  63. typedef struct VirtIOSerialPortClass {
  64. DeviceClass parent_class;
  65. /* Is this a device that binds with hvc in the guest? */
  66. bool is_console;
  67. /*
  68. * The per-port (or per-app) init function that's called when a
  69. * new device is found on the bus.
  70. */
  71. int (*init)(VirtIOSerialPort *port);
  72. /*
  73. * Per-port exit function that's called when a port gets
  74. * hot-unplugged or removed.
  75. */
  76. int (*exit)(VirtIOSerialPort *port);
  77. /* Callbacks for guest events */
  78. /* Guest opened device. */
  79. void (*guest_open)(VirtIOSerialPort *port);
  80. /* Guest closed device. */
  81. void (*guest_close)(VirtIOSerialPort *port);
  82. /* Guest is now ready to accept data (virtqueues set up). */
  83. void (*guest_ready)(VirtIOSerialPort *port);
  84. /*
  85. * Guest wrote some data to the port. This data is handed over to
  86. * the app via this callback. The app can return a size less than
  87. * 'len'. In this case, throttling will be enabled for this port.
  88. */
  89. ssize_t (*have_data)(VirtIOSerialPort *port, const uint8_t *buf,
  90. size_t len);
  91. } VirtIOSerialPortClass;
  92. /*
  93. * This is the state that's shared between all the ports. Some of the
  94. * state is configurable via command-line options. Some of it can be
  95. * set by individual devices in their initfn routines. Some of the
  96. * state is set by the generic qdev device init routine.
  97. */
  98. struct VirtIOSerialPort {
  99. DeviceState dev;
  100. QTAILQ_ENTRY(VirtIOSerialPort) next;
  101. /*
  102. * This field gives us the virtio device as well as the qdev bus
  103. * that we are associated with
  104. */
  105. VirtIOSerial *vser;
  106. VirtQueue *ivq, *ovq;
  107. /*
  108. * This name is sent to the guest and exported via sysfs.
  109. * The guest could create symlinks based on this information.
  110. * The name is in the reverse fqdn format, like org.qemu.console.0
  111. */
  112. char *name;
  113. /*
  114. * This id helps identify ports between the guest and the host.
  115. * The guest sends a "header" with this id with each data packet
  116. * that it sends and the host can then find out which associated
  117. * device to send out this data to
  118. */
  119. uint32_t id;
  120. /*
  121. * This is the elem that we pop from the virtqueue. A slow
  122. * backend that consumes guest data (e.g. the file backend for
  123. * qemu chardevs) can cause the guest to block till all the output
  124. * is flushed. This isn't desired, so we keep a note of the last
  125. * element popped and continue consuming it once the backend
  126. * becomes writable again.
  127. */
  128. VirtQueueElement elem;
  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. * When unthrottling we use a bottom-half to call flush_queued_data.
  137. */
  138. QEMUBH *bh;
  139. /* Is the corresponding guest device open? */
  140. bool guest_connected;
  141. /* Is this device open for IO on the host? */
  142. bool host_connected;
  143. /* Do apps not want to receive data? */
  144. bool throttled;
  145. };
  146. /* Interface to the virtio-serial bus */
  147. /*
  148. * Open a connection to the port
  149. * Returns 0 on success (always).
  150. */
  151. int virtio_serial_open(VirtIOSerialPort *port);
  152. /*
  153. * Close the connection to the port
  154. * Returns 0 on success (always).
  155. */
  156. int virtio_serial_close(VirtIOSerialPort *port);
  157. /*
  158. * Send data to Guest
  159. */
  160. ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
  161. size_t size);
  162. /*
  163. * Query whether a guest is ready to receive data.
  164. */
  165. size_t virtio_serial_guest_ready(VirtIOSerialPort *port);
  166. /*
  167. * Flow control: Ports can signal to the virtio-serial core to stop
  168. * sending data or re-start sending data, depending on the 'throttle'
  169. * value here.
  170. */
  171. void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle);
  172. #endif