blob: 7e551bf46e99bad0911363205edbeed7dffb9ab2 [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>
6#include <spice-experimental.h>
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +01007#include <spice/protocol.h>
Alon Levycbcc6332011-01-19 10:49:50 +02008
Paolo Bonzini1de7afc2012-12-17 18:20:00 +01009#include "qemu/osdep.h"
Alon Levycbcc6332011-01-19 10:49:50 +020010
Alon Levycbcc6332011-01-19 10:49:50 +020011typedef struct SpiceCharDriver {
12 CharDriverState* chr;
13 SpiceCharDeviceInstance sin;
14 char *subtype;
15 bool active;
Hans de Goedeae893e52013-04-05 11:30:22 +020016 bool blocked;
Alon Levycbcc6332011-01-19 10:49:50 +020017 uint8_t *buffer;
18 uint8_t *datapos;
19 ssize_t bufsize, datalen;
Marc-André Lureau7a5448c2012-12-05 16:15:35 +010020 QLIST_ENTRY(SpiceCharDriver) next;
Alon Levycbcc6332011-01-19 10:49:50 +020021} SpiceCharDriver;
22
Hans de Goedeae893e52013-04-05 11:30:22 +020023typedef struct SpiceCharSource {
24 GSource source;
25 SpiceCharDriver *scd;
26} SpiceCharSource;
27
Marc-André Lureau7a5448c2012-12-05 16:15:35 +010028static QLIST_HEAD(, SpiceCharDriver) spice_chars =
29 QLIST_HEAD_INITIALIZER(spice_chars);
30
Alon Levycbcc6332011-01-19 10:49:50 +020031static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
32{
33 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
34 ssize_t out = 0;
35 ssize_t last_out;
36 uint8_t* p = (uint8_t*)buf;
37
38 while (len > 0) {
Marc-André Lureau07a54d72012-12-05 16:15:32 +010039 last_out = MIN(len, qemu_chr_be_can_write(scd->chr));
40 if (last_out <= 0) {
Alon Levycbcc6332011-01-19 10:49:50 +020041 break;
42 }
Anthony Liguorifa5efcc2011-08-15 11:17:30 -050043 qemu_chr_be_write(scd->chr, p, last_out);
Hans de Goede35106c22011-03-22 16:28:41 +010044 out += last_out;
45 len -= last_out;
46 p += last_out;
Alon Levycbcc6332011-01-19 10:49:50 +020047 }
48
Alon Levycbcc6332011-01-19 10:49:50 +020049 trace_spice_vmc_write(out, len + out);
50 return out;
51}
52
53static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)
54{
55 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
56 int bytes = MIN(len, scd->datalen);
57
Alon Levycbcc6332011-01-19 10:49:50 +020058 if (bytes > 0) {
59 memcpy(buf, scd->datapos, bytes);
60 scd->datapos += bytes;
61 scd->datalen -= bytes;
62 assert(scd->datalen >= 0);
Hans de Goedeae893e52013-04-05 11:30:22 +020063 }
64 if (scd->datalen == 0) {
65 scd->datapos = 0;
66 scd->blocked = false;
Alon Levycbcc6332011-01-19 10:49:50 +020067 }
68 trace_spice_vmc_read(bytes, len);
69 return bytes;
70}
71
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +010072#if SPICE_SERVER_VERSION >= 0x000c02
73static void vmc_event(SpiceCharDeviceInstance *sin, uint8_t event)
74{
75 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
76 int chr_event;
77
78 switch (event) {
79 case SPICE_PORT_EVENT_BREAK:
80 chr_event = CHR_EVENT_BREAK;
81 break;
82 default:
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +010083 return;
84 }
85
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +010086 trace_spice_vmc_event(chr_event);
87 qemu_chr_be_event(scd->chr, chr_event);
88}
89#endif
90
Hans de Goedef76e4c72011-11-19 10:22:44 +010091static void vmc_state(SpiceCharDeviceInstance *sin, int connected)
92{
93 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
94
Hans de Goede16665b92013-03-26 11:07:53 +010095 if ((scd->chr->be_open && connected) ||
96 (!scd->chr->be_open && !connected)) {
Hans de Goedef76e4c72011-11-19 10:22:44 +010097 return;
98 }
99
100 qemu_chr_be_event(scd->chr,
101 connected ? CHR_EVENT_OPENED : CHR_EVENT_CLOSED);
102}
103
Alon Levycbcc6332011-01-19 10:49:50 +0200104static SpiceCharDeviceInterface vmc_interface = {
105 .base.type = SPICE_INTERFACE_CHAR_DEVICE,
106 .base.description = "spice virtual channel char device",
107 .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,
108 .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,
Hans de Goedef76e4c72011-11-19 10:22:44 +0100109 .state = vmc_state,
Alon Levycbcc6332011-01-19 10:49:50 +0200110 .write = vmc_write,
111 .read = vmc_read,
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100112#if SPICE_SERVER_VERSION >= 0x000c02
113 .event = vmc_event,
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
163GSourceFuncs SpiceCharSourceFuncs = {
164 .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
174 assert(cond == G_IO_OUT);
175
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);
189 if (s->bufsize < len) {
190 s->bufsize = len;
Anthony Liguori7267c092011-08-20 22:09:37 -0500191 s->buffer = g_realloc(s->buffer, s->bufsize);
Alon Levycbcc6332011-01-19 10:49:50 +0200192 }
193 memcpy(s->buffer, buf, len);
194 s->datapos = s->buffer;
195 s->datalen = len;
196 spice_server_char_device_wakeup(&s->sin);
Hans de Goedeae893e52013-04-05 11:30:22 +0200197 read_bytes = len - s->datalen;
198 if (read_bytes != len) {
199 /* We'll get passed in the unconsumed data with the next call */
200 s->datalen = 0;
201 s->datapos = NULL;
202 s->blocked = true;
203 }
204 return read_bytes;
Alon Levycbcc6332011-01-19 10:49:50 +0200205}
206
207static void spice_chr_close(struct CharDriverState *chr)
208{
209 SpiceCharDriver *s = chr->opaque;
210
Alon Levycbcc6332011-01-19 10:49:50 +0200211 vmc_unregister_interface(s);
Marc-André Lureau7a5448c2012-12-05 16:15:35 +0100212 QLIST_REMOVE(s, next);
Hans de Goede5e9b4732013-03-13 10:41:31 +0100213
214 g_free((char *)s->sin.subtype);
215#if SPICE_SERVER_VERSION >= 0x000c02
216 g_free((char *)s->sin.portname);
217#endif
Anthony Liguori7267c092011-08-20 22:09:37 -0500218 g_free(s);
Alon Levycbcc6332011-01-19 10:49:50 +0200219}
220
Hans de Goede574b7112013-03-26 11:07:58 +0100221static void spice_chr_set_fe_open(struct CharDriverState *chr, int fe_open)
Hans de Goedecd8f7df2011-03-24 11:12:04 +0100222{
223 SpiceCharDriver *s = chr->opaque;
Hans de Goede574b7112013-03-26 11:07:58 +0100224 if (fe_open) {
225 vmc_register_interface(s);
226 } else {
227 vmc_unregister_interface(s);
228 }
Hans de Goedecd8f7df2011-03-24 11:12:04 +0100229}
230
Alon Levycbcc6332011-01-19 10:49:50 +0200231static void print_allowed_subtypes(void)
232{
233 const char** psubtype;
234 int i;
235
236 fprintf(stderr, "allowed names: ");
237 for(i=0, psubtype = spice_server_char_device_recognized_subtypes();
238 *psubtype != NULL; ++psubtype, ++i) {
239 if (i == 0) {
240 fprintf(stderr, "%s", *psubtype);
241 } else {
242 fprintf(stderr, ", %s", *psubtype);
243 }
244 }
245 fprintf(stderr, "\n");
246}
247
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100248static CharDriverState *chr_open(const char *subtype)
Alon Levycbcc6332011-01-19 10:49:50 +0200249{
250 CharDriverState *chr;
251 SpiceCharDriver *s;
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100252
253 chr = g_malloc0(sizeof(CharDriverState));
254 s = g_malloc0(sizeof(SpiceCharDriver));
255 s->chr = chr;
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100256 s->active = false;
Hans de Goede5e9b4732013-03-13 10:41:31 +0100257 s->sin.subtype = g_strdup(subtype);
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100258 chr->opaque = s;
259 chr->chr_write = spice_chr_write;
Hans de Goedeae893e52013-04-05 11:30:22 +0200260 chr->chr_add_watch = spice_chr_add_watch;
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100261 chr->chr_close = spice_chr_close;
Hans de Goede574b7112013-03-26 11:07:58 +0100262 chr->chr_set_fe_open = spice_chr_set_fe_open;
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100263
Marc-André Lureau7a5448c2012-12-05 16:15:35 +0100264 QLIST_INSERT_HEAD(&spice_chars, s, next);
265
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100266 return chr;
267}
268
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100269CharDriverState *qemu_chr_open_spice_vmc(const char *type)
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100270{
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100271 const char **psubtype = spice_server_char_device_recognized_subtypes();
Alon Levycbcc6332011-01-19 10:49:50 +0200272
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100273 if (type == NULL) {
Alon Levycbcc6332011-01-19 10:49:50 +0200274 fprintf(stderr, "spice-qemu-char: missing name parameter\n");
275 print_allowed_subtypes();
Markus Armbruster1f514702012-02-07 15:09:08 +0100276 return NULL;
Alon Levycbcc6332011-01-19 10:49:50 +0200277 }
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100278 for (; *psubtype != NULL; ++psubtype) {
279 if (strcmp(type, *psubtype) == 0) {
Alon Levycbcc6332011-01-19 10:49:50 +0200280 break;
281 }
282 }
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100283 if (*psubtype == NULL) {
284 fprintf(stderr, "spice-qemu-char: unsupported type: %s\n", type);
Alon Levycbcc6332011-01-19 10:49:50 +0200285 print_allowed_subtypes();
Markus Armbruster1f514702012-02-07 15:09:08 +0100286 return NULL;
Alon Levycbcc6332011-01-19 10:49:50 +0200287 }
288
Hans de Goede52fe0e72013-04-05 11:30:21 +0200289 return chr_open(type);
Alon Levycbcc6332011-01-19 10:49:50 +0200290}
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100291
292#if SPICE_SERVER_VERSION >= 0x000c02
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100293CharDriverState *qemu_chr_open_spice_port(const char *name)
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100294{
295 CharDriverState *chr;
296 SpiceCharDriver *s;
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100297
298 if (name == NULL) {
299 fprintf(stderr, "spice-qemu-char: missing name parameter\n");
300 return NULL;
301 }
302
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100303 chr = chr_open("port");
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100304 s = chr->opaque;
Hans de Goede5e9b4732013-03-13 10:41:31 +0100305 s->sin.portname = g_strdup(name);
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100306
307 return chr;
308}
Marc-André Lureauafd0b402012-12-05 16:15:36 +0100309
310void qemu_spice_register_ports(void)
311{
312 SpiceCharDriver *s;
313
314 QLIST_FOREACH(s, &spice_chars, next) {
315 if (s->sin.portname == NULL) {
316 continue;
317 }
318 vmc_register_interface(s);
319 }
320}
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100321#endif
Anthony Liguori26c60612013-03-05 23:21:29 +0530322
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100323static void qemu_chr_parse_spice_vmc(QemuOpts *opts, ChardevBackend *backend,
324 Error **errp)
325{
326 const char *name = qemu_opt_get(opts, "name");
327
328 if (name == NULL) {
329 error_setg(errp, "chardev: spice channel: no name given");
330 return;
331 }
332 backend->spicevmc = g_new0(ChardevSpiceChannel, 1);
333 backend->spicevmc->type = g_strdup(name);
334}
335
336static void qemu_chr_parse_spice_port(QemuOpts *opts, ChardevBackend *backend,
337 Error **errp)
338{
339 const char *name = qemu_opt_get(opts, "name");
340
341 if (name == NULL) {
342 error_setg(errp, "chardev: spice port: no name given");
343 return;
344 }
345 backend->spiceport = g_new0(ChardevSpicePort, 1);
346 backend->spiceport->fqdn = g_strdup(name);
347}
348
Anthony Liguori26c60612013-03-05 23:21:29 +0530349static void register_types(void)
350{
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100351 register_char_driver_qapi("spicevmc", CHARDEV_BACKEND_KIND_SPICEVMC,
352 qemu_chr_parse_spice_vmc);
353 register_char_driver_qapi("spiceport", CHARDEV_BACKEND_KIND_SPICEPORT,
354 qemu_chr_parse_spice_port);
Anthony Liguori26c60612013-03-05 23:21:29 +0530355}
356
357type_init(register_types);