2
0

spice-qemu-char.c 11 KB

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