spice-qemu-char.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. #include "qemu/osdep.h"
  2. #include "trace.h"
  3. #include "ui/qemu-spice.h"
  4. #include "sysemu/char.h"
  5. #include <spice.h>
  6. #include <spice/protocol.h>
  7. typedef struct SpiceCharDriver {
  8. CharDriverState* chr;
  9. SpiceCharDeviceInstance sin;
  10. bool active;
  11. bool blocked;
  12. const uint8_t *datapos;
  13. int datalen;
  14. QLIST_ENTRY(SpiceCharDriver) next;
  15. } SpiceCharDriver;
  16. typedef struct SpiceCharSource {
  17. GSource source;
  18. SpiceCharDriver *scd;
  19. } SpiceCharSource;
  20. static QLIST_HEAD(, SpiceCharDriver) spice_chars =
  21. QLIST_HEAD_INITIALIZER(spice_chars);
  22. static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
  23. {
  24. SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
  25. ssize_t out = 0;
  26. ssize_t last_out;
  27. uint8_t* p = (uint8_t*)buf;
  28. while (len > 0) {
  29. int can_write = qemu_chr_be_can_write(scd->chr);
  30. last_out = MIN(len, can_write);
  31. if (last_out <= 0) {
  32. break;
  33. }
  34. qemu_chr_be_write(scd->chr, p, last_out);
  35. out += last_out;
  36. len -= last_out;
  37. p += last_out;
  38. }
  39. trace_spice_vmc_write(out, len + out);
  40. return out;
  41. }
  42. static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)
  43. {
  44. SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
  45. int bytes = MIN(len, scd->datalen);
  46. if (bytes > 0) {
  47. memcpy(buf, scd->datapos, bytes);
  48. scd->datapos += bytes;
  49. scd->datalen -= bytes;
  50. assert(scd->datalen >= 0);
  51. }
  52. if (scd->datalen == 0) {
  53. scd->datapos = 0;
  54. scd->blocked = false;
  55. }
  56. trace_spice_vmc_read(bytes, len);
  57. return bytes;
  58. }
  59. #if SPICE_SERVER_VERSION >= 0x000c02
  60. static void vmc_event(SpiceCharDeviceInstance *sin, uint8_t event)
  61. {
  62. SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
  63. int chr_event;
  64. switch (event) {
  65. case SPICE_PORT_EVENT_BREAK:
  66. chr_event = CHR_EVENT_BREAK;
  67. break;
  68. default:
  69. return;
  70. }
  71. trace_spice_vmc_event(chr_event);
  72. qemu_chr_be_event(scd->chr, chr_event);
  73. }
  74. #endif
  75. static void vmc_state(SpiceCharDeviceInstance *sin, int connected)
  76. {
  77. SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
  78. if ((scd->chr->be_open && connected) ||
  79. (!scd->chr->be_open && !connected)) {
  80. return;
  81. }
  82. qemu_chr_be_event(scd->chr,
  83. connected ? CHR_EVENT_OPENED : CHR_EVENT_CLOSED);
  84. }
  85. static SpiceCharDeviceInterface vmc_interface = {
  86. .base.type = SPICE_INTERFACE_CHAR_DEVICE,
  87. .base.description = "spice virtual channel char device",
  88. .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,
  89. .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,
  90. .state = vmc_state,
  91. .write = vmc_write,
  92. .read = vmc_read,
  93. #if SPICE_SERVER_VERSION >= 0x000c02
  94. .event = vmc_event,
  95. #endif
  96. #if SPICE_SERVER_VERSION >= 0x000c06
  97. .flags = SPICE_CHAR_DEVICE_NOTIFY_WRITABLE,
  98. #endif
  99. };
  100. static void vmc_register_interface(SpiceCharDriver *scd)
  101. {
  102. if (scd->active) {
  103. return;
  104. }
  105. scd->sin.base.sif = &vmc_interface.base;
  106. qemu_spice_add_interface(&scd->sin.base);
  107. scd->active = true;
  108. trace_spice_vmc_register_interface(scd);
  109. }
  110. static void vmc_unregister_interface(SpiceCharDriver *scd)
  111. {
  112. if (!scd->active) {
  113. return;
  114. }
  115. spice_server_remove_interface(&scd->sin.base);
  116. scd->active = false;
  117. trace_spice_vmc_unregister_interface(scd);
  118. }
  119. static gboolean spice_char_source_prepare(GSource *source, gint *timeout)
  120. {
  121. SpiceCharSource *src = (SpiceCharSource *)source;
  122. *timeout = -1;
  123. return !src->scd->blocked;
  124. }
  125. static gboolean spice_char_source_check(GSource *source)
  126. {
  127. SpiceCharSource *src = (SpiceCharSource *)source;
  128. return !src->scd->blocked;
  129. }
  130. static gboolean spice_char_source_dispatch(GSource *source,
  131. GSourceFunc callback, gpointer user_data)
  132. {
  133. GIOFunc func = (GIOFunc)callback;
  134. return func(NULL, G_IO_OUT, user_data);
  135. }
  136. static GSourceFuncs SpiceCharSourceFuncs = {
  137. .prepare = spice_char_source_prepare,
  138. .check = spice_char_source_check,
  139. .dispatch = spice_char_source_dispatch,
  140. };
  141. static GSource *spice_chr_add_watch(CharDriverState *chr, GIOCondition cond)
  142. {
  143. SpiceCharDriver *scd = chr->opaque;
  144. SpiceCharSource *src;
  145. assert(cond & G_IO_OUT);
  146. src = (SpiceCharSource *)g_source_new(&SpiceCharSourceFuncs,
  147. sizeof(SpiceCharSource));
  148. src->scd = scd;
  149. return (GSource *)src;
  150. }
  151. static int spice_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
  152. {
  153. SpiceCharDriver *s = chr->opaque;
  154. int read_bytes;
  155. assert(s->datalen == 0);
  156. s->datapos = buf;
  157. s->datalen = len;
  158. spice_server_char_device_wakeup(&s->sin);
  159. read_bytes = len - s->datalen;
  160. if (read_bytes != len) {
  161. /* We'll get passed in the unconsumed data with the next call */
  162. s->datalen = 0;
  163. s->datapos = NULL;
  164. s->blocked = true;
  165. }
  166. return read_bytes;
  167. }
  168. static void spice_chr_close(struct CharDriverState *chr)
  169. {
  170. SpiceCharDriver *s = chr->opaque;
  171. vmc_unregister_interface(s);
  172. QLIST_REMOVE(s, next);
  173. g_free((char *)s->sin.subtype);
  174. #if SPICE_SERVER_VERSION >= 0x000c02
  175. g_free((char *)s->sin.portname);
  176. #endif
  177. g_free(s);
  178. }
  179. static void spice_vmc_set_fe_open(struct CharDriverState *chr, int fe_open)
  180. {
  181. SpiceCharDriver *s = chr->opaque;
  182. if (fe_open) {
  183. vmc_register_interface(s);
  184. } else {
  185. vmc_unregister_interface(s);
  186. }
  187. }
  188. static void spice_port_set_fe_open(struct CharDriverState *chr, int fe_open)
  189. {
  190. #if SPICE_SERVER_VERSION >= 0x000c02
  191. SpiceCharDriver *s = chr->opaque;
  192. if (fe_open) {
  193. spice_server_port_event(&s->sin, SPICE_PORT_EVENT_OPENED);
  194. } else {
  195. spice_server_port_event(&s->sin, SPICE_PORT_EVENT_CLOSED);
  196. }
  197. #endif
  198. }
  199. static void spice_chr_fe_event(struct CharDriverState *chr, int event)
  200. {
  201. #if SPICE_SERVER_VERSION >= 0x000c02
  202. SpiceCharDriver *s = chr->opaque;
  203. spice_server_port_event(&s->sin, event);
  204. #endif
  205. }
  206. static void print_allowed_subtypes(void)
  207. {
  208. const char** psubtype;
  209. int i;
  210. fprintf(stderr, "allowed names: ");
  211. for(i=0, psubtype = spice_server_char_device_recognized_subtypes();
  212. *psubtype != NULL; ++psubtype, ++i) {
  213. if (i == 0) {
  214. fprintf(stderr, "%s", *psubtype);
  215. } else {
  216. fprintf(stderr, ", %s", *psubtype);
  217. }
  218. }
  219. fprintf(stderr, "\n");
  220. }
  221. static void spice_chr_accept_input(struct CharDriverState *chr)
  222. {
  223. SpiceCharDriver *s = chr->opaque;
  224. spice_server_char_device_wakeup(&s->sin);
  225. }
  226. static CharDriverState *chr_open(const char *subtype,
  227. void (*set_fe_open)(struct CharDriverState *,
  228. int),
  229. ChardevCommon *backend,
  230. Error **errp)
  231. {
  232. CharDriverState *chr;
  233. SpiceCharDriver *s;
  234. chr = qemu_chr_alloc(backend, errp);
  235. if (!chr) {
  236. return NULL;
  237. }
  238. s = g_malloc0(sizeof(SpiceCharDriver));
  239. s->chr = chr;
  240. s->active = false;
  241. s->sin.subtype = g_strdup(subtype);
  242. chr->opaque = s;
  243. chr->chr_write = spice_chr_write;
  244. chr->chr_add_watch = spice_chr_add_watch;
  245. chr->chr_close = spice_chr_close;
  246. chr->chr_set_fe_open = set_fe_open;
  247. chr->explicit_be_open = true;
  248. chr->chr_fe_event = spice_chr_fe_event;
  249. chr->chr_accept_input = spice_chr_accept_input;
  250. QLIST_INSERT_HEAD(&spice_chars, s, next);
  251. return chr;
  252. }
  253. static CharDriverState *qemu_chr_open_spice_vmc(const char *id,
  254. ChardevBackend *backend,
  255. ChardevReturn *ret,
  256. Error **errp)
  257. {
  258. ChardevSpiceChannel *spicevmc = backend->u.spicevmc.data;
  259. const char *type = spicevmc->type;
  260. const char **psubtype = spice_server_char_device_recognized_subtypes();
  261. ChardevCommon *common = qapi_ChardevSpiceChannel_base(spicevmc);
  262. for (; *psubtype != NULL; ++psubtype) {
  263. if (strcmp(type, *psubtype) == 0) {
  264. break;
  265. }
  266. }
  267. if (*psubtype == NULL) {
  268. fprintf(stderr, "spice-qemu-char: unsupported type: %s\n", type);
  269. print_allowed_subtypes();
  270. return NULL;
  271. }
  272. return chr_open(type, spice_vmc_set_fe_open, common, errp);
  273. }
  274. #if SPICE_SERVER_VERSION >= 0x000c02
  275. static CharDriverState *qemu_chr_open_spice_port(const char *id,
  276. ChardevBackend *backend,
  277. ChardevReturn *ret,
  278. Error **errp)
  279. {
  280. ChardevSpicePort *spiceport = backend->u.spiceport.data;
  281. const char *name = spiceport->fqdn;
  282. ChardevCommon *common = qapi_ChardevSpicePort_base(spiceport);
  283. CharDriverState *chr;
  284. SpiceCharDriver *s;
  285. if (name == NULL) {
  286. fprintf(stderr, "spice-qemu-char: missing name parameter\n");
  287. return NULL;
  288. }
  289. chr = chr_open("port", spice_port_set_fe_open, common, errp);
  290. if (!chr) {
  291. return NULL;
  292. }
  293. s = chr->opaque;
  294. s->sin.portname = g_strdup(name);
  295. return chr;
  296. }
  297. void qemu_spice_register_ports(void)
  298. {
  299. SpiceCharDriver *s;
  300. QLIST_FOREACH(s, &spice_chars, next) {
  301. if (s->sin.portname == NULL) {
  302. continue;
  303. }
  304. vmc_register_interface(s);
  305. }
  306. }
  307. #endif
  308. static void qemu_chr_parse_spice_vmc(QemuOpts *opts, ChardevBackend *backend,
  309. Error **errp)
  310. {
  311. const char *name = qemu_opt_get(opts, "name");
  312. ChardevSpiceChannel *spicevmc;
  313. if (name == NULL) {
  314. error_setg(errp, "chardev: spice channel: no name given");
  315. return;
  316. }
  317. spicevmc = backend->u.spicevmc.data = g_new0(ChardevSpiceChannel, 1);
  318. qemu_chr_parse_common(opts, qapi_ChardevSpiceChannel_base(spicevmc));
  319. spicevmc->type = g_strdup(name);
  320. }
  321. static void qemu_chr_parse_spice_port(QemuOpts *opts, ChardevBackend *backend,
  322. Error **errp)
  323. {
  324. const char *name = qemu_opt_get(opts, "name");
  325. ChardevSpicePort *spiceport;
  326. if (name == NULL) {
  327. error_setg(errp, "chardev: spice port: no name given");
  328. return;
  329. }
  330. spiceport = backend->u.spiceport.data = g_new0(ChardevSpicePort, 1);
  331. qemu_chr_parse_common(opts, qapi_ChardevSpicePort_base(spiceport));
  332. spiceport->fqdn = g_strdup(name);
  333. }
  334. static void register_types(void)
  335. {
  336. register_char_driver("spicevmc", CHARDEV_BACKEND_KIND_SPICEVMC,
  337. qemu_chr_parse_spice_vmc, qemu_chr_open_spice_vmc);
  338. register_char_driver("spiceport", CHARDEV_BACKEND_KIND_SPICEPORT,
  339. qemu_chr_parse_spice_port, qemu_chr_open_spice_port);
  340. }
  341. type_init(register_types);