blob: d41bb745a82d437eb1d12d50d7edaa62841bd30a [file] [log] [blame]
Alon Levycbcc6332011-01-19 10:49:50 +02001#include "config-host.h"
2#include "trace.h"
3#include "ui/qemu-spice.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +02004#include "sysemu/char.h"
Alon Levycbcc6332011-01-19 10:49:50 +02005#include <spice.h>
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +01006#include <spice/protocol.h>
Alon Levycbcc6332011-01-19 10:49:50 +02007
Paolo Bonzini1de7afc2012-12-17 18:20:00 +01008#include "qemu/osdep.h"
Alon Levycbcc6332011-01-19 10:49:50 +02009
Alon Levycbcc6332011-01-19 10:49:50 +020010typedef struct SpiceCharDriver {
11 CharDriverState* chr;
12 SpiceCharDeviceInstance sin;
Alon Levycbcc6332011-01-19 10:49:50 +020013 bool active;
Hans de Goedeae893e52013-04-05 11:30:22 +020014 bool blocked;
Alon Levyb010cec2013-04-05 11:30:23 +020015 const uint8_t *datapos;
16 int datalen;
Marc-André Lureau7a5448c2012-12-05 16:15:35 +010017 QLIST_ENTRY(SpiceCharDriver) next;
Alon Levycbcc6332011-01-19 10:49:50 +020018} SpiceCharDriver;
19
Hans de Goedeae893e52013-04-05 11:30:22 +020020typedef struct SpiceCharSource {
21 GSource source;
22 SpiceCharDriver *scd;
23} SpiceCharSource;
24
Marc-André Lureau7a5448c2012-12-05 16:15:35 +010025static QLIST_HEAD(, SpiceCharDriver) spice_chars =
26 QLIST_HEAD_INITIALIZER(spice_chars);
27
Alon Levycbcc6332011-01-19 10:49:50 +020028static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
29{
30 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
31 ssize_t out = 0;
32 ssize_t last_out;
33 uint8_t* p = (uint8_t*)buf;
34
35 while (len > 0) {
Hans de Goede75c439b2013-04-05 11:30:24 +020036 int can_write = qemu_chr_be_can_write(scd->chr);
37 last_out = MIN(len, can_write);
Marc-André Lureau07a54d72012-12-05 16:15:32 +010038 if (last_out <= 0) {
Alon Levycbcc6332011-01-19 10:49:50 +020039 break;
40 }
Anthony Liguorifa5efcc2011-08-15 11:17:30 -050041 qemu_chr_be_write(scd->chr, p, last_out);
Hans de Goede35106c22011-03-22 16:28:41 +010042 out += last_out;
43 len -= last_out;
44 p += last_out;
Alon Levycbcc6332011-01-19 10:49:50 +020045 }
46
Alon Levycbcc6332011-01-19 10:49:50 +020047 trace_spice_vmc_write(out, len + out);
48 return out;
49}
50
51static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)
52{
53 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
54 int bytes = MIN(len, scd->datalen);
55
Alon Levycbcc6332011-01-19 10:49:50 +020056 if (bytes > 0) {
57 memcpy(buf, scd->datapos, bytes);
58 scd->datapos += bytes;
59 scd->datalen -= bytes;
60 assert(scd->datalen >= 0);
Hans de Goedeae893e52013-04-05 11:30:22 +020061 }
62 if (scd->datalen == 0) {
63 scd->datapos = 0;
64 scd->blocked = false;
Alon Levycbcc6332011-01-19 10:49:50 +020065 }
66 trace_spice_vmc_read(bytes, len);
67 return bytes;
68}
69
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +010070#if SPICE_SERVER_VERSION >= 0x000c02
71static void vmc_event(SpiceCharDeviceInstance *sin, uint8_t event)
72{
73 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
74 int chr_event;
75
76 switch (event) {
77 case SPICE_PORT_EVENT_BREAK:
78 chr_event = CHR_EVENT_BREAK;
79 break;
80 default:
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +010081 return;
82 }
83
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +010084 trace_spice_vmc_event(chr_event);
85 qemu_chr_be_event(scd->chr, chr_event);
86}
87#endif
88
Hans de Goedef76e4c72011-11-19 10:22:44 +010089static void vmc_state(SpiceCharDeviceInstance *sin, int connected)
90{
91 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
92
Hans de Goede16665b92013-03-26 11:07:53 +010093 if ((scd->chr->be_open && connected) ||
94 (!scd->chr->be_open && !connected)) {
Hans de Goedef76e4c72011-11-19 10:22:44 +010095 return;
96 }
97
98 qemu_chr_be_event(scd->chr,
99 connected ? CHR_EVENT_OPENED : CHR_EVENT_CLOSED);
100}
101
Alon Levycbcc6332011-01-19 10:49:50 +0200102static SpiceCharDeviceInterface vmc_interface = {
103 .base.type = SPICE_INTERFACE_CHAR_DEVICE,
104 .base.description = "spice virtual channel char device",
105 .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,
106 .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,
Hans de Goedef76e4c72011-11-19 10:22:44 +0100107 .state = vmc_state,
Alon Levycbcc6332011-01-19 10:49:50 +0200108 .write = vmc_write,
109 .read = vmc_read,
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100110#if SPICE_SERVER_VERSION >= 0x000c02
111 .event = vmc_event,
112#endif
Marc-André Lureaue95e2032015-05-05 16:58:56 +0200113#if SPICE_SERVER_VERSION >= 0x000c06
114 .flags = SPICE_CHAR_DEVICE_NOTIFY_WRITABLE,
115#endif
Alon Levycbcc6332011-01-19 10:49:50 +0200116};
117
118
119static void vmc_register_interface(SpiceCharDriver *scd)
120{
121 if (scd->active) {
122 return;
123 }
Alon Levycbcc6332011-01-19 10:49:50 +0200124 scd->sin.base.sif = &vmc_interface.base;
125 qemu_spice_add_interface(&scd->sin.base);
126 scd->active = true;
127 trace_spice_vmc_register_interface(scd);
128}
129
130static void vmc_unregister_interface(SpiceCharDriver *scd)
131{
132 if (!scd->active) {
133 return;
134 }
Alon Levycbcc6332011-01-19 10:49:50 +0200135 spice_server_remove_interface(&scd->sin.base);
136 scd->active = false;
137 trace_spice_vmc_unregister_interface(scd);
138}
139
Hans de Goedeae893e52013-04-05 11:30:22 +0200140static gboolean spice_char_source_prepare(GSource *source, gint *timeout)
141{
142 SpiceCharSource *src = (SpiceCharSource *)source;
143
144 *timeout = -1;
145
146 return !src->scd->blocked;
147}
148
149static gboolean spice_char_source_check(GSource *source)
150{
151 SpiceCharSource *src = (SpiceCharSource *)source;
152
153 return !src->scd->blocked;
154}
155
156static gboolean spice_char_source_dispatch(GSource *source,
157 GSourceFunc callback, gpointer user_data)
158{
159 GIOFunc func = (GIOFunc)callback;
160
161 return func(NULL, G_IO_OUT, user_data);
162}
163
Stefan Weil51575c32015-02-06 22:43:14 +0100164static GSourceFuncs SpiceCharSourceFuncs = {
Hans de Goedeae893e52013-04-05 11:30:22 +0200165 .prepare = spice_char_source_prepare,
166 .check = spice_char_source_check,
167 .dispatch = spice_char_source_dispatch,
168};
169
170static GSource *spice_chr_add_watch(CharDriverState *chr, GIOCondition cond)
171{
172 SpiceCharDriver *scd = chr->opaque;
173 SpiceCharSource *src;
174
Marc-André Lureauf7a8beb2015-05-28 15:04:58 +0200175 assert(cond & G_IO_OUT);
Hans de Goedeae893e52013-04-05 11:30:22 +0200176
177 src = (SpiceCharSource *)g_source_new(&SpiceCharSourceFuncs,
178 sizeof(SpiceCharSource));
179 src->scd = scd;
180
181 return (GSource *)src;
182}
Alon Levycbcc6332011-01-19 10:49:50 +0200183
184static int spice_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
185{
186 SpiceCharDriver *s = chr->opaque;
Hans de Goedeae893e52013-04-05 11:30:22 +0200187 int read_bytes;
Alon Levycbcc6332011-01-19 10:49:50 +0200188
Alon Levycbcc6332011-01-19 10:49:50 +0200189 assert(s->datalen == 0);
Alon Levyb010cec2013-04-05 11:30:23 +0200190 s->datapos = buf;
Alon Levycbcc6332011-01-19 10:49:50 +0200191 s->datalen = len;
192 spice_server_char_device_wakeup(&s->sin);
Hans de Goedeae893e52013-04-05 11:30:22 +0200193 read_bytes = len - s->datalen;
194 if (read_bytes != len) {
195 /* We'll get passed in the unconsumed data with the next call */
196 s->datalen = 0;
197 s->datapos = NULL;
198 s->blocked = true;
199 }
200 return read_bytes;
Alon Levycbcc6332011-01-19 10:49:50 +0200201}
202
203static void spice_chr_close(struct CharDriverState *chr)
204{
205 SpiceCharDriver *s = chr->opaque;
206
Alon Levycbcc6332011-01-19 10:49:50 +0200207 vmc_unregister_interface(s);
Marc-André Lureau7a5448c2012-12-05 16:15:35 +0100208 QLIST_REMOVE(s, next);
Hans de Goede5e9b4732013-03-13 10:41:31 +0100209
210 g_free((char *)s->sin.subtype);
211#if SPICE_SERVER_VERSION >= 0x000c02
212 g_free((char *)s->sin.portname);
213#endif
Anthony Liguori7267c092011-08-20 22:09:37 -0500214 g_free(s);
Alon Levycbcc6332011-01-19 10:49:50 +0200215}
216
Marc-André Lureau89091142014-01-11 00:20:24 +0100217static void spice_vmc_set_fe_open(struct CharDriverState *chr, int fe_open)
Hans de Goedecd8f7df2011-03-24 11:12:04 +0100218{
219 SpiceCharDriver *s = chr->opaque;
Hans de Goede574b7112013-03-26 11:07:58 +0100220 if (fe_open) {
221 vmc_register_interface(s);
222 } else {
223 vmc_unregister_interface(s);
224 }
Hans de Goedecd8f7df2011-03-24 11:12:04 +0100225}
226
Marc-André Lureau89091142014-01-11 00:20:24 +0100227static void spice_port_set_fe_open(struct CharDriverState *chr, int fe_open)
228{
229#if SPICE_SERVER_VERSION >= 0x000c02
230 SpiceCharDriver *s = chr->opaque;
231
232 if (fe_open) {
233 spice_server_port_event(&s->sin, SPICE_PORT_EVENT_OPENED);
234 } else {
235 spice_server_port_event(&s->sin, SPICE_PORT_EVENT_CLOSED);
236 }
237#endif
238}
239
Marc-André Lureaube733d62013-12-01 22:23:40 +0100240static void spice_chr_fe_event(struct CharDriverState *chr, int event)
241{
242#if SPICE_SERVER_VERSION >= 0x000c02
243 SpiceCharDriver *s = chr->opaque;
244
245 spice_server_port_event(&s->sin, event);
246#endif
247}
248
Alon Levycbcc6332011-01-19 10:49:50 +0200249static void print_allowed_subtypes(void)
250{
251 const char** psubtype;
252 int i;
253
254 fprintf(stderr, "allowed names: ");
255 for(i=0, psubtype = spice_server_char_device_recognized_subtypes();
256 *psubtype != NULL; ++psubtype, ++i) {
257 if (i == 0) {
258 fprintf(stderr, "%s", *psubtype);
259 } else {
260 fprintf(stderr, ", %s", *psubtype);
261 }
262 }
263 fprintf(stderr, "\n");
264}
265
Marc-André Lureaue95e2032015-05-05 16:58:56 +0200266static void spice_chr_accept_input(struct CharDriverState *chr)
267{
268 SpiceCharDriver *s = chr->opaque;
269
270 spice_server_char_device_wakeup(&s->sin);
271}
272
Marc-André Lureau89091142014-01-11 00:20:24 +0100273static CharDriverState *chr_open(const char *subtype,
274 void (*set_fe_open)(struct CharDriverState *, int))
275
Alon Levycbcc6332011-01-19 10:49:50 +0200276{
277 CharDriverState *chr;
278 SpiceCharDriver *s;
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100279
Paolo Bonzinidb39fcf2014-06-18 08:43:55 +0200280 chr = qemu_chr_alloc();
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100281 s = g_malloc0(sizeof(SpiceCharDriver));
282 s->chr = chr;
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100283 s->active = false;
Hans de Goede5e9b4732013-03-13 10:41:31 +0100284 s->sin.subtype = g_strdup(subtype);
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100285 chr->opaque = s;
286 chr->chr_write = spice_chr_write;
Hans de Goedeae893e52013-04-05 11:30:22 +0200287 chr->chr_add_watch = spice_chr_add_watch;
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100288 chr->chr_close = spice_chr_close;
Marc-André Lureau89091142014-01-11 00:20:24 +0100289 chr->chr_set_fe_open = set_fe_open;
Michael Rothbd5c51e2013-06-07 15:19:53 -0500290 chr->explicit_be_open = true;
Marc-André Lureaube733d62013-12-01 22:23:40 +0100291 chr->chr_fe_event = spice_chr_fe_event;
Marc-André Lureaue95e2032015-05-05 16:58:56 +0200292 chr->chr_accept_input = spice_chr_accept_input;
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100293
Marc-André Lureau7a5448c2012-12-05 16:15:35 +0100294 QLIST_INSERT_HEAD(&spice_chars, s, next);
295
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100296 return chr;
297}
298
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100299CharDriverState *qemu_chr_open_spice_vmc(const char *type)
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100300{
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100301 const char **psubtype = spice_server_char_device_recognized_subtypes();
Alon Levycbcc6332011-01-19 10:49:50 +0200302
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100303 if (type == NULL) {
Alon Levycbcc6332011-01-19 10:49:50 +0200304 fprintf(stderr, "spice-qemu-char: missing name parameter\n");
305 print_allowed_subtypes();
Markus Armbruster1f514702012-02-07 15:09:08 +0100306 return NULL;
Alon Levycbcc6332011-01-19 10:49:50 +0200307 }
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100308 for (; *psubtype != NULL; ++psubtype) {
309 if (strcmp(type, *psubtype) == 0) {
Alon Levycbcc6332011-01-19 10:49:50 +0200310 break;
311 }
312 }
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100313 if (*psubtype == NULL) {
314 fprintf(stderr, "spice-qemu-char: unsupported type: %s\n", type);
Alon Levycbcc6332011-01-19 10:49:50 +0200315 print_allowed_subtypes();
Markus Armbruster1f514702012-02-07 15:09:08 +0100316 return NULL;
Alon Levycbcc6332011-01-19 10:49:50 +0200317 }
318
Marc-André Lureau89091142014-01-11 00:20:24 +0100319 return chr_open(type, spice_vmc_set_fe_open);
Alon Levycbcc6332011-01-19 10:49:50 +0200320}
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100321
322#if SPICE_SERVER_VERSION >= 0x000c02
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100323CharDriverState *qemu_chr_open_spice_port(const char *name)
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100324{
325 CharDriverState *chr;
326 SpiceCharDriver *s;
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100327
328 if (name == NULL) {
329 fprintf(stderr, "spice-qemu-char: missing name parameter\n");
330 return NULL;
331 }
332
Marc-André Lureau89091142014-01-11 00:20:24 +0100333 chr = chr_open("port", spice_port_set_fe_open);
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100334 s = chr->opaque;
Hans de Goede5e9b4732013-03-13 10:41:31 +0100335 s->sin.portname = g_strdup(name);
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100336
337 return chr;
338}
Marc-André Lureauafd0b402012-12-05 16:15:36 +0100339
340void qemu_spice_register_ports(void)
341{
342 SpiceCharDriver *s;
343
344 QLIST_FOREACH(s, &spice_chars, next) {
345 if (s->sin.portname == NULL) {
346 continue;
347 }
348 vmc_register_interface(s);
349 }
350}
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100351#endif
Anthony Liguori26c60612013-03-05 23:21:29 +0530352
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100353static void qemu_chr_parse_spice_vmc(QemuOpts *opts, ChardevBackend *backend,
354 Error **errp)
355{
356 const char *name = qemu_opt_get(opts, "name");
357
358 if (name == NULL) {
359 error_setg(errp, "chardev: spice channel: no name given");
360 return;
361 }
362 backend->spicevmc = g_new0(ChardevSpiceChannel, 1);
363 backend->spicevmc->type = g_strdup(name);
364}
365
366static void qemu_chr_parse_spice_port(QemuOpts *opts, ChardevBackend *backend,
367 Error **errp)
368{
369 const char *name = qemu_opt_get(opts, "name");
370
371 if (name == NULL) {
372 error_setg(errp, "chardev: spice port: no name given");
373 return;
374 }
375 backend->spiceport = g_new0(ChardevSpicePort, 1);
376 backend->spiceport->fqdn = g_strdup(name);
377}
378
Anthony Liguori26c60612013-03-05 23:21:29 +0530379static void register_types(void)
380{
Peter Maydelle4d50d42014-09-02 11:24:17 +0100381 register_char_driver("spicevmc", CHARDEV_BACKEND_KIND_SPICEVMC,
382 qemu_chr_parse_spice_vmc);
383 register_char_driver("spiceport", CHARDEV_BACKEND_KIND_SPICEPORT,
384 qemu_chr_parse_spice_port);
Anthony Liguori26c60612013-03-05 23:21:29 +0530385}
386
387type_init(register_types);