blob: 21885c526ba5bbb88ff38579ea72362937ea2389 [file] [log] [blame]
Peter Maydelld38ea872016-01-29 17:50:05 +00001#include "qemu/osdep.h"
Alon Levycbcc6332011-01-19 10:49:50 +02002#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
Alon Levycbcc6332011-01-19 10:49:50 +02008
Alon Levycbcc6332011-01-19 10:49:50 +02009typedef struct SpiceCharDriver {
10 CharDriverState* chr;
11 SpiceCharDeviceInstance sin;
Alon Levycbcc6332011-01-19 10:49:50 +020012 bool active;
Hans de Goedeae893e52013-04-05 11:30:22 +020013 bool blocked;
Alon Levyb010cec2013-04-05 11:30:23 +020014 const uint8_t *datapos;
15 int datalen;
Marc-André Lureau7a5448c2012-12-05 16:15:35 +010016 QLIST_ENTRY(SpiceCharDriver) next;
Alon Levycbcc6332011-01-19 10:49:50 +020017} SpiceCharDriver;
18
Hans de Goedeae893e52013-04-05 11:30:22 +020019typedef struct SpiceCharSource {
20 GSource source;
21 SpiceCharDriver *scd;
22} SpiceCharSource;
23
Marc-André Lureau7a5448c2012-12-05 16:15:35 +010024static QLIST_HEAD(, SpiceCharDriver) spice_chars =
25 QLIST_HEAD_INITIALIZER(spice_chars);
26
Alon Levycbcc6332011-01-19 10:49:50 +020027static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
28{
29 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
30 ssize_t out = 0;
31 ssize_t last_out;
32 uint8_t* p = (uint8_t*)buf;
33
34 while (len > 0) {
Hans de Goede75c439b2013-04-05 11:30:24 +020035 int can_write = qemu_chr_be_can_write(scd->chr);
36 last_out = MIN(len, can_write);
Marc-André Lureau07a54d72012-12-05 16:15:32 +010037 if (last_out <= 0) {
Alon Levycbcc6332011-01-19 10:49:50 +020038 break;
39 }
Anthony Liguorifa5efcc2011-08-15 11:17:30 -050040 qemu_chr_be_write(scd->chr, p, last_out);
Hans de Goede35106c22011-03-22 16:28:41 +010041 out += last_out;
42 len -= last_out;
43 p += last_out;
Alon Levycbcc6332011-01-19 10:49:50 +020044 }
45
Alon Levycbcc6332011-01-19 10:49:50 +020046 trace_spice_vmc_write(out, len + out);
47 return out;
48}
49
50static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)
51{
52 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
53 int bytes = MIN(len, scd->datalen);
54
Alon Levycbcc6332011-01-19 10:49:50 +020055 if (bytes > 0) {
56 memcpy(buf, scd->datapos, bytes);
57 scd->datapos += bytes;
58 scd->datalen -= bytes;
59 assert(scd->datalen >= 0);
Hans de Goedeae893e52013-04-05 11:30:22 +020060 }
61 if (scd->datalen == 0) {
62 scd->datapos = 0;
63 scd->blocked = false;
Alon Levycbcc6332011-01-19 10:49:50 +020064 }
65 trace_spice_vmc_read(bytes, len);
66 return bytes;
67}
68
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +010069#if SPICE_SERVER_VERSION >= 0x000c02
70static void vmc_event(SpiceCharDeviceInstance *sin, uint8_t event)
71{
72 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
73 int chr_event;
74
75 switch (event) {
76 case SPICE_PORT_EVENT_BREAK:
77 chr_event = CHR_EVENT_BREAK;
78 break;
79 default:
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +010080 return;
81 }
82
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +010083 trace_spice_vmc_event(chr_event);
84 qemu_chr_be_event(scd->chr, chr_event);
85}
86#endif
87
Hans de Goedef76e4c72011-11-19 10:22:44 +010088static void vmc_state(SpiceCharDeviceInstance *sin, int connected)
89{
90 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
91
Hans de Goede16665b92013-03-26 11:07:53 +010092 if ((scd->chr->be_open && connected) ||
93 (!scd->chr->be_open && !connected)) {
Hans de Goedef76e4c72011-11-19 10:22:44 +010094 return;
95 }
96
97 qemu_chr_be_event(scd->chr,
98 connected ? CHR_EVENT_OPENED : CHR_EVENT_CLOSED);
99}
100
Alon Levycbcc6332011-01-19 10:49:50 +0200101static SpiceCharDeviceInterface vmc_interface = {
102 .base.type = SPICE_INTERFACE_CHAR_DEVICE,
103 .base.description = "spice virtual channel char device",
104 .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,
105 .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,
Hans de Goedef76e4c72011-11-19 10:22:44 +0100106 .state = vmc_state,
Alon Levycbcc6332011-01-19 10:49:50 +0200107 .write = vmc_write,
108 .read = vmc_read,
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100109#if SPICE_SERVER_VERSION >= 0x000c02
110 .event = vmc_event,
111#endif
Marc-André Lureaue95e2032015-05-05 16:58:56 +0200112#if SPICE_SERVER_VERSION >= 0x000c06
113 .flags = SPICE_CHAR_DEVICE_NOTIFY_WRITABLE,
114#endif
Alon Levycbcc6332011-01-19 10:49:50 +0200115};
116
117
118static void vmc_register_interface(SpiceCharDriver *scd)
119{
120 if (scd->active) {
121 return;
122 }
Alon Levycbcc6332011-01-19 10:49:50 +0200123 scd->sin.base.sif = &vmc_interface.base;
124 qemu_spice_add_interface(&scd->sin.base);
125 scd->active = true;
126 trace_spice_vmc_register_interface(scd);
127}
128
129static void vmc_unregister_interface(SpiceCharDriver *scd)
130{
131 if (!scd->active) {
132 return;
133 }
Alon Levycbcc6332011-01-19 10:49:50 +0200134 spice_server_remove_interface(&scd->sin.base);
135 scd->active = false;
136 trace_spice_vmc_unregister_interface(scd);
137}
138
Hans de Goedeae893e52013-04-05 11:30:22 +0200139static gboolean spice_char_source_prepare(GSource *source, gint *timeout)
140{
141 SpiceCharSource *src = (SpiceCharSource *)source;
142
143 *timeout = -1;
144
145 return !src->scd->blocked;
146}
147
148static gboolean spice_char_source_check(GSource *source)
149{
150 SpiceCharSource *src = (SpiceCharSource *)source;
151
152 return !src->scd->blocked;
153}
154
155static gboolean spice_char_source_dispatch(GSource *source,
156 GSourceFunc callback, gpointer user_data)
157{
158 GIOFunc func = (GIOFunc)callback;
159
160 return func(NULL, G_IO_OUT, user_data);
161}
162
Stefan Weil51575c32015-02-06 22:43:14 +0100163static GSourceFuncs SpiceCharSourceFuncs = {
Hans de Goedeae893e52013-04-05 11:30:22 +0200164 .prepare = spice_char_source_prepare,
165 .check = spice_char_source_check,
166 .dispatch = spice_char_source_dispatch,
167};
168
169static GSource *spice_chr_add_watch(CharDriverState *chr, GIOCondition cond)
170{
171 SpiceCharDriver *scd = chr->opaque;
172 SpiceCharSource *src;
173
Marc-André Lureauf7a8beb2015-05-28 15:04:58 +0200174 assert(cond & G_IO_OUT);
Hans de Goedeae893e52013-04-05 11:30:22 +0200175
176 src = (SpiceCharSource *)g_source_new(&SpiceCharSourceFuncs,
177 sizeof(SpiceCharSource));
178 src->scd = scd;
179
180 return (GSource *)src;
181}
Alon Levycbcc6332011-01-19 10:49:50 +0200182
183static int spice_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
184{
185 SpiceCharDriver *s = chr->opaque;
Hans de Goedeae893e52013-04-05 11:30:22 +0200186 int read_bytes;
Alon Levycbcc6332011-01-19 10:49:50 +0200187
Alon Levycbcc6332011-01-19 10:49:50 +0200188 assert(s->datalen == 0);
Alon Levyb010cec2013-04-05 11:30:23 +0200189 s->datapos = buf;
Alon Levycbcc6332011-01-19 10:49:50 +0200190 s->datalen = len;
191 spice_server_char_device_wakeup(&s->sin);
Hans de Goedeae893e52013-04-05 11:30:22 +0200192 read_bytes = len - s->datalen;
193 if (read_bytes != len) {
194 /* We'll get passed in the unconsumed data with the next call */
195 s->datalen = 0;
196 s->datapos = NULL;
197 s->blocked = true;
198 }
199 return read_bytes;
Alon Levycbcc6332011-01-19 10:49:50 +0200200}
201
202static void spice_chr_close(struct CharDriverState *chr)
203{
204 SpiceCharDriver *s = chr->opaque;
205
Alon Levycbcc6332011-01-19 10:49:50 +0200206 vmc_unregister_interface(s);
Marc-André Lureau7a5448c2012-12-05 16:15:35 +0100207 QLIST_REMOVE(s, next);
Hans de Goede5e9b4732013-03-13 10:41:31 +0100208
209 g_free((char *)s->sin.subtype);
210#if SPICE_SERVER_VERSION >= 0x000c02
211 g_free((char *)s->sin.portname);
212#endif
Anthony Liguori7267c092011-08-20 22:09:37 -0500213 g_free(s);
Alon Levycbcc6332011-01-19 10:49:50 +0200214}
215
Marc-André Lureau89091142014-01-11 00:20:24 +0100216static void spice_vmc_set_fe_open(struct CharDriverState *chr, int fe_open)
Hans de Goedecd8f7df2011-03-24 11:12:04 +0100217{
218 SpiceCharDriver *s = chr->opaque;
Hans de Goede574b7112013-03-26 11:07:58 +0100219 if (fe_open) {
220 vmc_register_interface(s);
221 } else {
222 vmc_unregister_interface(s);
223 }
Hans de Goedecd8f7df2011-03-24 11:12:04 +0100224}
225
Marc-André Lureau89091142014-01-11 00:20:24 +0100226static void spice_port_set_fe_open(struct CharDriverState *chr, int fe_open)
227{
228#if SPICE_SERVER_VERSION >= 0x000c02
229 SpiceCharDriver *s = chr->opaque;
230
231 if (fe_open) {
232 spice_server_port_event(&s->sin, SPICE_PORT_EVENT_OPENED);
233 } else {
234 spice_server_port_event(&s->sin, SPICE_PORT_EVENT_CLOSED);
235 }
236#endif
237}
238
Marc-André Lureaube733d62013-12-01 22:23:40 +0100239static void spice_chr_fe_event(struct CharDriverState *chr, int event)
240{
241#if SPICE_SERVER_VERSION >= 0x000c02
242 SpiceCharDriver *s = chr->opaque;
243
244 spice_server_port_event(&s->sin, event);
245#endif
246}
247
Alon Levycbcc6332011-01-19 10:49:50 +0200248static void print_allowed_subtypes(void)
249{
250 const char** psubtype;
251 int i;
252
253 fprintf(stderr, "allowed names: ");
254 for(i=0, psubtype = spice_server_char_device_recognized_subtypes();
255 *psubtype != NULL; ++psubtype, ++i) {
256 if (i == 0) {
257 fprintf(stderr, "%s", *psubtype);
258 } else {
259 fprintf(stderr, ", %s", *psubtype);
260 }
261 }
262 fprintf(stderr, "\n");
263}
264
Marc-André Lureaue95e2032015-05-05 16:58:56 +0200265static void spice_chr_accept_input(struct CharDriverState *chr)
266{
267 SpiceCharDriver *s = chr->opaque;
268
269 spice_server_char_device_wakeup(&s->sin);
270}
271
Marc-André Lureau89091142014-01-11 00:20:24 +0100272static CharDriverState *chr_open(const char *subtype,
Daniel P. Berranged0d77082016-01-11 12:44:41 +0000273 void (*set_fe_open)(struct CharDriverState *,
274 int),
275 ChardevCommon *backend,
276 Error **errp)
Alon Levycbcc6332011-01-19 10:49:50 +0200277{
278 CharDriverState *chr;
279 SpiceCharDriver *s;
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100280
Daniel P. Berranged0d77082016-01-11 12:44:41 +0000281 chr = qemu_chr_alloc(backend, errp);
282 if (!chr) {
283 return NULL;
284 }
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100285 s = g_malloc0(sizeof(SpiceCharDriver));
286 s->chr = chr;
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100287 s->active = false;
Hans de Goede5e9b4732013-03-13 10:41:31 +0100288 s->sin.subtype = g_strdup(subtype);
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100289 chr->opaque = s;
290 chr->chr_write = spice_chr_write;
Hans de Goedeae893e52013-04-05 11:30:22 +0200291 chr->chr_add_watch = spice_chr_add_watch;
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100292 chr->chr_close = spice_chr_close;
Marc-André Lureau89091142014-01-11 00:20:24 +0100293 chr->chr_set_fe_open = set_fe_open;
Michael Rothbd5c51e2013-06-07 15:19:53 -0500294 chr->explicit_be_open = true;
Marc-André Lureaube733d62013-12-01 22:23:40 +0100295 chr->chr_fe_event = spice_chr_fe_event;
Marc-André Lureaue95e2032015-05-05 16:58:56 +0200296 chr->chr_accept_input = spice_chr_accept_input;
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100297
Marc-André Lureau7a5448c2012-12-05 16:15:35 +0100298 QLIST_INSERT_HEAD(&spice_chars, s, next);
299
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100300 return chr;
301}
302
Paolo Bonzini68145e12015-09-29 15:45:47 +0200303static CharDriverState *qemu_chr_open_spice_vmc(const char *id,
304 ChardevBackend *backend,
305 ChardevReturn *ret,
306 Error **errp)
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100307{
Eric Blake130257d2015-10-26 16:34:57 -0600308 const char *type = backend->u.spicevmc->type;
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100309 const char **psubtype = spice_server_char_device_recognized_subtypes();
Daniel P. Berranged0d77082016-01-11 12:44:41 +0000310 ChardevCommon *common = qapi_ChardevSpiceChannel_base(backend->u.spicevmc);
Alon Levycbcc6332011-01-19 10:49:50 +0200311
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100312 for (; *psubtype != NULL; ++psubtype) {
313 if (strcmp(type, *psubtype) == 0) {
Alon Levycbcc6332011-01-19 10:49:50 +0200314 break;
315 }
316 }
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100317 if (*psubtype == NULL) {
318 fprintf(stderr, "spice-qemu-char: unsupported type: %s\n", type);
Alon Levycbcc6332011-01-19 10:49:50 +0200319 print_allowed_subtypes();
Markus Armbruster1f514702012-02-07 15:09:08 +0100320 return NULL;
Alon Levycbcc6332011-01-19 10:49:50 +0200321 }
322
Daniel P. Berranged0d77082016-01-11 12:44:41 +0000323 return chr_open(type, spice_vmc_set_fe_open, common, errp);
Alon Levycbcc6332011-01-19 10:49:50 +0200324}
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100325
326#if SPICE_SERVER_VERSION >= 0x000c02
Paolo Bonzini68145e12015-09-29 15:45:47 +0200327static CharDriverState *qemu_chr_open_spice_port(const char *id,
328 ChardevBackend *backend,
329 ChardevReturn *ret,
330 Error **errp)
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100331{
Eric Blake130257d2015-10-26 16:34:57 -0600332 const char *name = backend->u.spiceport->fqdn;
Daniel P. Berranged0d77082016-01-11 12:44:41 +0000333 ChardevCommon *common = qapi_ChardevSpicePort_base(backend->u.spiceport);
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100334 CharDriverState *chr;
335 SpiceCharDriver *s;
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100336
337 if (name == NULL) {
338 fprintf(stderr, "spice-qemu-char: missing name parameter\n");
339 return NULL;
340 }
341
Daniel P. Berranged0d77082016-01-11 12:44:41 +0000342 chr = chr_open("port", spice_port_set_fe_open, common, errp);
343 if (!chr) {
344 return NULL;
345 }
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100346 s = chr->opaque;
Hans de Goede5e9b4732013-03-13 10:41:31 +0100347 s->sin.portname = g_strdup(name);
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100348
349 return chr;
350}
Marc-André Lureauafd0b402012-12-05 16:15:36 +0100351
352void qemu_spice_register_ports(void)
353{
354 SpiceCharDriver *s;
355
356 QLIST_FOREACH(s, &spice_chars, next) {
357 if (s->sin.portname == NULL) {
358 continue;
359 }
360 vmc_register_interface(s);
361 }
362}
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100363#endif
Anthony Liguori26c60612013-03-05 23:21:29 +0530364
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100365static void qemu_chr_parse_spice_vmc(QemuOpts *opts, ChardevBackend *backend,
366 Error **errp)
367{
368 const char *name = qemu_opt_get(opts, "name");
Eric Blake21a933e2016-02-19 17:19:31 -0700369 ChardevSpiceChannel *spicevmc;
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100370
371 if (name == NULL) {
372 error_setg(errp, "chardev: spice channel: no name given");
373 return;
374 }
Eric Blake21a933e2016-02-19 17:19:31 -0700375 spicevmc = backend->u.spicevmc = g_new0(ChardevSpiceChannel, 1);
376 qemu_chr_parse_common(opts, qapi_ChardevSpiceChannel_base(spicevmc));
377 spicevmc->type = g_strdup(name);
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100378}
379
380static void qemu_chr_parse_spice_port(QemuOpts *opts, ChardevBackend *backend,
381 Error **errp)
382{
383 const char *name = qemu_opt_get(opts, "name");
Eric Blake21a933e2016-02-19 17:19:31 -0700384 ChardevSpicePort *spiceport;
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100385
386 if (name == NULL) {
387 error_setg(errp, "chardev: spice port: no name given");
388 return;
389 }
Eric Blake21a933e2016-02-19 17:19:31 -0700390 spiceport = backend->u.spiceport = g_new0(ChardevSpicePort, 1);
391 qemu_chr_parse_common(opts, qapi_ChardevSpicePort_base(spiceport));
392 spiceport->fqdn = g_strdup(name);
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100393}
394
Anthony Liguori26c60612013-03-05 23:21:29 +0530395static void register_types(void)
396{
Peter Maydelle4d50d42014-09-02 11:24:17 +0100397 register_char_driver("spicevmc", CHARDEV_BACKEND_KIND_SPICEVMC,
Paolo Bonzini68145e12015-09-29 15:45:47 +0200398 qemu_chr_parse_spice_vmc, qemu_chr_open_spice_vmc);
Peter Maydelle4d50d42014-09-02 11:24:17 +0100399 register_char_driver("spiceport", CHARDEV_BACKEND_KIND_SPICEPORT,
Paolo Bonzini68145e12015-09-29 15:45:47 +0200400 qemu_chr_parse_spice_port, qemu_chr_open_spice_port);
Anthony Liguori26c60612013-03-05 23:21:29 +0530401}
402
403type_init(register_types);