2
0

virtio-console.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #include "char/char.h"
  13. #include "qemu/error-report.h"
  14. #include "trace.h"
  15. #include "virtio-serial.h"
  16. typedef struct VirtConsole {
  17. VirtIOSerialPort port;
  18. CharDriverState *chr;
  19. } VirtConsole;
  20. /* Callback function that's called when the guest sends us data */
  21. static ssize_t flush_buf(VirtIOSerialPort *port, const uint8_t *buf, size_t len)
  22. {
  23. VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
  24. ssize_t ret;
  25. if (!vcon->chr) {
  26. /* If there's no backend, we can just say we consumed all data. */
  27. return len;
  28. }
  29. ret = qemu_chr_fe_write(vcon->chr, buf, len);
  30. trace_virtio_console_flush_buf(port->id, len, ret);
  31. if (ret < 0) {
  32. /*
  33. * Ideally we'd get a better error code than just -1, but
  34. * that's what the chardev interface gives us right now. If
  35. * we had a finer-grained message, like -EPIPE, we could close
  36. * this connection. Absent such error messages, the most we
  37. * can do is to return 0 here.
  38. *
  39. * This will prevent stray -1 values to go to
  40. * virtio-serial-bus.c and cause abort()s in
  41. * do_flush_queued_data().
  42. */
  43. ret = 0;
  44. }
  45. return ret;
  46. }
  47. /* Callback function that's called when the guest opens the port */
  48. static void guest_open(VirtIOSerialPort *port)
  49. {
  50. VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
  51. if (!vcon->chr) {
  52. return;
  53. }
  54. qemu_chr_fe_open(vcon->chr);
  55. }
  56. /* Callback function that's called when the guest closes the port */
  57. static void guest_close(VirtIOSerialPort *port)
  58. {
  59. VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
  60. if (!vcon->chr) {
  61. return;
  62. }
  63. qemu_chr_fe_close(vcon->chr);
  64. }
  65. /* Readiness of the guest to accept data on a port */
  66. static int chr_can_read(void *opaque)
  67. {
  68. VirtConsole *vcon = opaque;
  69. return virtio_serial_guest_ready(&vcon->port);
  70. }
  71. /* Send data from a char device over to the guest */
  72. static void chr_read(void *opaque, const uint8_t *buf, int size)
  73. {
  74. VirtConsole *vcon = opaque;
  75. trace_virtio_console_chr_read(vcon->port.id, size);
  76. virtio_serial_write(&vcon->port, buf, size);
  77. }
  78. static void chr_event(void *opaque, int event)
  79. {
  80. VirtConsole *vcon = opaque;
  81. trace_virtio_console_chr_event(vcon->port.id, event);
  82. switch (event) {
  83. case CHR_EVENT_OPENED:
  84. virtio_serial_open(&vcon->port);
  85. break;
  86. case CHR_EVENT_CLOSED:
  87. virtio_serial_close(&vcon->port);
  88. break;
  89. }
  90. }
  91. static int virtconsole_initfn(VirtIOSerialPort *port)
  92. {
  93. VirtConsole *vcon = DO_UPCAST(VirtConsole, port, port);
  94. VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
  95. if (port->id == 0 && !k->is_console) {
  96. error_report("Port number 0 on virtio-serial devices reserved for virtconsole devices for backward compatibility.");
  97. return -1;
  98. }
  99. if (vcon->chr) {
  100. qemu_chr_add_handlers(vcon->chr, chr_can_read, chr_read, chr_event,
  101. vcon);
  102. }
  103. return 0;
  104. }
  105. static Property virtconsole_properties[] = {
  106. DEFINE_PROP_CHR("chardev", VirtConsole, chr),
  107. DEFINE_PROP_END_OF_LIST(),
  108. };
  109. static void virtconsole_class_init(ObjectClass *klass, void *data)
  110. {
  111. DeviceClass *dc = DEVICE_CLASS(klass);
  112. VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
  113. k->is_console = true;
  114. k->init = virtconsole_initfn;
  115. k->have_data = flush_buf;
  116. k->guest_open = guest_open;
  117. k->guest_close = guest_close;
  118. dc->props = virtconsole_properties;
  119. }
  120. static const TypeInfo virtconsole_info = {
  121. .name = "virtconsole",
  122. .parent = TYPE_VIRTIO_SERIAL_PORT,
  123. .instance_size = sizeof(VirtConsole),
  124. .class_init = virtconsole_class_init,
  125. };
  126. static Property virtserialport_properties[] = {
  127. DEFINE_PROP_CHR("chardev", VirtConsole, chr),
  128. DEFINE_PROP_END_OF_LIST(),
  129. };
  130. static void virtserialport_class_init(ObjectClass *klass, void *data)
  131. {
  132. DeviceClass *dc = DEVICE_CLASS(klass);
  133. VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_CLASS(klass);
  134. k->init = virtconsole_initfn;
  135. k->have_data = flush_buf;
  136. k->guest_open = guest_open;
  137. k->guest_close = guest_close;
  138. dc->props = virtserialport_properties;
  139. }
  140. static const TypeInfo virtserialport_info = {
  141. .name = "virtserialport",
  142. .parent = TYPE_VIRTIO_SERIAL_PORT,
  143. .instance_size = sizeof(VirtConsole),
  144. .class_init = virtserialport_class_init,
  145. };
  146. static void virtconsole_register_types(void)
  147. {
  148. type_register_static(&virtconsole_info);
  149. type_register_static(&virtserialport_info);
  150. }
  151. type_init(virtconsole_register_types)