spice-qemu-char.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include "config-host.h"
  2. #include "trace.h"
  3. #include "ui/qemu-spice.h"
  4. #include <spice.h>
  5. #include <spice-experimental.h>
  6. #include "osdep.h"
  7. #define dprintf(_scd, _level, _fmt, ...) \
  8. do { \
  9. static unsigned __dprintf_counter = 0; \
  10. if (_scd->debug >= _level) { \
  11. fprintf(stderr, "scd: %3d: " _fmt, ++__dprintf_counter, ## __VA_ARGS__);\
  12. } \
  13. } while (0)
  14. #define VMC_MAX_HOST_WRITE 2048
  15. typedef struct SpiceCharDriver {
  16. CharDriverState* chr;
  17. SpiceCharDeviceInstance sin;
  18. char *subtype;
  19. bool active;
  20. uint8_t *buffer;
  21. uint8_t *datapos;
  22. ssize_t bufsize, datalen;
  23. uint32_t debug;
  24. } SpiceCharDriver;
  25. static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
  26. {
  27. SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
  28. ssize_t out = 0;
  29. ssize_t last_out;
  30. uint8_t* p = (uint8_t*)buf;
  31. while (len > 0) {
  32. last_out = MIN(len, VMC_MAX_HOST_WRITE);
  33. if (qemu_chr_can_read(scd->chr) < last_out) {
  34. break;
  35. }
  36. qemu_chr_read(scd->chr, p, last_out);
  37. out += last_out;
  38. len -= last_out;
  39. p += last_out;
  40. }
  41. dprintf(scd, 3, "%s: %lu/%zd\n", __func__, out, len + out);
  42. trace_spice_vmc_write(out, len + out);
  43. return out;
  44. }
  45. static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)
  46. {
  47. SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
  48. int bytes = MIN(len, scd->datalen);
  49. dprintf(scd, 2, "%s: %p %d/%d/%zd\n", __func__, scd->datapos, len, bytes, scd->datalen);
  50. if (bytes > 0) {
  51. memcpy(buf, scd->datapos, bytes);
  52. scd->datapos += bytes;
  53. scd->datalen -= bytes;
  54. assert(scd->datalen >= 0);
  55. if (scd->datalen == 0) {
  56. scd->datapos = 0;
  57. }
  58. }
  59. trace_spice_vmc_read(bytes, len);
  60. return bytes;
  61. }
  62. static SpiceCharDeviceInterface vmc_interface = {
  63. .base.type = SPICE_INTERFACE_CHAR_DEVICE,
  64. .base.description = "spice virtual channel char device",
  65. .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,
  66. .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,
  67. .write = vmc_write,
  68. .read = vmc_read,
  69. };
  70. static void vmc_register_interface(SpiceCharDriver *scd)
  71. {
  72. if (scd->active) {
  73. return;
  74. }
  75. dprintf(scd, 1, "%s\n", __func__);
  76. scd->sin.base.sif = &vmc_interface.base;
  77. qemu_spice_add_interface(&scd->sin.base);
  78. scd->active = true;
  79. trace_spice_vmc_register_interface(scd);
  80. }
  81. static void vmc_unregister_interface(SpiceCharDriver *scd)
  82. {
  83. if (!scd->active) {
  84. return;
  85. }
  86. dprintf(scd, 1, "%s\n", __func__);
  87. spice_server_remove_interface(&scd->sin.base);
  88. scd->active = false;
  89. trace_spice_vmc_unregister_interface(scd);
  90. }
  91. static int spice_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  92. {
  93. SpiceCharDriver *s = chr->opaque;
  94. dprintf(s, 2, "%s: %d\n", __func__, len);
  95. vmc_register_interface(s);
  96. assert(s->datalen == 0);
  97. if (s->bufsize < len) {
  98. s->bufsize = len;
  99. s->buffer = qemu_realloc(s->buffer, s->bufsize);
  100. }
  101. memcpy(s->buffer, buf, len);
  102. s->datapos = s->buffer;
  103. s->datalen = len;
  104. spice_server_char_device_wakeup(&s->sin);
  105. return len;
  106. }
  107. static void spice_chr_close(struct CharDriverState *chr)
  108. {
  109. SpiceCharDriver *s = chr->opaque;
  110. printf("%s\n", __func__);
  111. vmc_unregister_interface(s);
  112. qemu_free(s);
  113. }
  114. static void spice_chr_guest_open(struct CharDriverState *chr)
  115. {
  116. SpiceCharDriver *s = chr->opaque;
  117. vmc_register_interface(s);
  118. }
  119. static void spice_chr_guest_close(struct CharDriverState *chr)
  120. {
  121. SpiceCharDriver *s = chr->opaque;
  122. vmc_unregister_interface(s);
  123. }
  124. static void print_allowed_subtypes(void)
  125. {
  126. const char** psubtype;
  127. int i;
  128. fprintf(stderr, "allowed names: ");
  129. for(i=0, psubtype = spice_server_char_device_recognized_subtypes();
  130. *psubtype != NULL; ++psubtype, ++i) {
  131. if (i == 0) {
  132. fprintf(stderr, "%s", *psubtype);
  133. } else {
  134. fprintf(stderr, ", %s", *psubtype);
  135. }
  136. }
  137. fprintf(stderr, "\n");
  138. }
  139. int qemu_chr_open_spice(QemuOpts *opts, CharDriverState **_chr)
  140. {
  141. CharDriverState *chr;
  142. SpiceCharDriver *s;
  143. const char* name = qemu_opt_get(opts, "name");
  144. uint32_t debug = qemu_opt_get_number(opts, "debug", 0);
  145. const char** psubtype = spice_server_char_device_recognized_subtypes();
  146. const char *subtype = NULL;
  147. if (name == NULL) {
  148. fprintf(stderr, "spice-qemu-char: missing name parameter\n");
  149. print_allowed_subtypes();
  150. return -EINVAL;
  151. }
  152. for(;*psubtype != NULL; ++psubtype) {
  153. if (strcmp(name, *psubtype) == 0) {
  154. subtype = *psubtype;
  155. break;
  156. }
  157. }
  158. if (subtype == NULL) {
  159. fprintf(stderr, "spice-qemu-char: unsupported name\n");
  160. print_allowed_subtypes();
  161. return -EINVAL;
  162. }
  163. chr = qemu_mallocz(sizeof(CharDriverState));
  164. s = qemu_mallocz(sizeof(SpiceCharDriver));
  165. s->chr = chr;
  166. s->debug = debug;
  167. s->active = false;
  168. s->sin.subtype = subtype;
  169. chr->opaque = s;
  170. chr->chr_write = spice_chr_write;
  171. chr->chr_close = spice_chr_close;
  172. chr->chr_guest_open = spice_chr_guest_open;
  173. chr->chr_guest_close = spice_chr_guest_close;
  174. qemu_chr_generic_open(chr);
  175. *_chr = chr;
  176. return 0;
  177. }