blob: ff95fcbe011d56f9dfed2f35c4945d1a56949d10 [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 Levyb010cec2013-04-05 11:30:23 +020017 const uint8_t *datapos;
18 int datalen;
Marc-André Lureau7a5448c2012-12-05 16:15:35 +010019 QLIST_ENTRY(SpiceCharDriver) next;
Alon Levycbcc6332011-01-19 10:49:50 +020020} SpiceCharDriver;
21
Hans de Goedeae893e52013-04-05 11:30:22 +020022typedef struct SpiceCharSource {
23 GSource source;
24 SpiceCharDriver *scd;
25} SpiceCharSource;
26
Marc-André Lureau7a5448c2012-12-05 16:15:35 +010027static QLIST_HEAD(, SpiceCharDriver) spice_chars =
28 QLIST_HEAD_INITIALIZER(spice_chars);
29
Alon Levycbcc6332011-01-19 10:49:50 +020030static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
31{
32 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
33 ssize_t out = 0;
34 ssize_t last_out;
35 uint8_t* p = (uint8_t*)buf;
36
37 while (len > 0) {
Marc-André Lureau07a54d72012-12-05 16:15:32 +010038 last_out = MIN(len, qemu_chr_be_can_write(scd->chr));
39 if (last_out <= 0) {
Alon Levycbcc6332011-01-19 10:49:50 +020040 break;
41 }
Anthony Liguorifa5efcc2011-08-15 11:17:30 -050042 qemu_chr_be_write(scd->chr, p, last_out);
Hans de Goede35106c22011-03-22 16:28:41 +010043 out += last_out;
44 len -= last_out;
45 p += last_out;
Alon Levycbcc6332011-01-19 10:49:50 +020046 }
47
Alon Levycbcc6332011-01-19 10:49:50 +020048 trace_spice_vmc_write(out, len + out);
49 return out;
50}
51
52static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)
53{
54 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
55 int bytes = MIN(len, scd->datalen);
56
Alon Levycbcc6332011-01-19 10:49:50 +020057 if (bytes > 0) {
58 memcpy(buf, scd->datapos, bytes);
59 scd->datapos += bytes;
60 scd->datalen -= bytes;
61 assert(scd->datalen >= 0);
Hans de Goedeae893e52013-04-05 11:30:22 +020062 }
63 if (scd->datalen == 0) {
64 scd->datapos = 0;
65 scd->blocked = false;
Alon Levycbcc6332011-01-19 10:49:50 +020066 }
67 trace_spice_vmc_read(bytes, len);
68 return bytes;
69}
70
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +010071#if SPICE_SERVER_VERSION >= 0x000c02
72static void vmc_event(SpiceCharDeviceInstance *sin, uint8_t event)
73{
74 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
75 int chr_event;
76
77 switch (event) {
78 case SPICE_PORT_EVENT_BREAK:
79 chr_event = CHR_EVENT_BREAK;
80 break;
81 default:
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +010082 return;
83 }
84
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +010085 trace_spice_vmc_event(chr_event);
86 qemu_chr_be_event(scd->chr, chr_event);
87}
88#endif
89
Hans de Goedef76e4c72011-11-19 10:22:44 +010090static void vmc_state(SpiceCharDeviceInstance *sin, int connected)
91{
92 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
93
Hans de Goede16665b92013-03-26 11:07:53 +010094 if ((scd->chr->be_open && connected) ||
95 (!scd->chr->be_open && !connected)) {
Hans de Goedef76e4c72011-11-19 10:22:44 +010096 return;
97 }
98
99 qemu_chr_be_event(scd->chr,
100 connected ? CHR_EVENT_OPENED : CHR_EVENT_CLOSED);
101}
102
Alon Levycbcc6332011-01-19 10:49:50 +0200103static SpiceCharDeviceInterface vmc_interface = {
104 .base.type = SPICE_INTERFACE_CHAR_DEVICE,
105 .base.description = "spice virtual channel char device",
106 .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,
107 .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,
Hans de Goedef76e4c72011-11-19 10:22:44 +0100108 .state = vmc_state,
Alon Levycbcc6332011-01-19 10:49:50 +0200109 .write = vmc_write,
110 .read = vmc_read,
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100111#if SPICE_SERVER_VERSION >= 0x000c02
112 .event = vmc_event,
113#endif
Alon Levycbcc6332011-01-19 10:49:50 +0200114};
115
116
117static void vmc_register_interface(SpiceCharDriver *scd)
118{
119 if (scd->active) {
120 return;
121 }
Alon Levycbcc6332011-01-19 10:49:50 +0200122 scd->sin.base.sif = &vmc_interface.base;
123 qemu_spice_add_interface(&scd->sin.base);
124 scd->active = true;
125 trace_spice_vmc_register_interface(scd);
126}
127
128static void vmc_unregister_interface(SpiceCharDriver *scd)
129{
130 if (!scd->active) {
131 return;
132 }
Alon Levycbcc6332011-01-19 10:49:50 +0200133 spice_server_remove_interface(&scd->sin.base);
134 scd->active = false;
135 trace_spice_vmc_unregister_interface(scd);
136}
137
Hans de Goedeae893e52013-04-05 11:30:22 +0200138static gboolean spice_char_source_prepare(GSource *source, gint *timeout)
139{
140 SpiceCharSource *src = (SpiceCharSource *)source;
141
142 *timeout = -1;
143
144 return !src->scd->blocked;
145}
146
147static gboolean spice_char_source_check(GSource *source)
148{
149 SpiceCharSource *src = (SpiceCharSource *)source;
150
151 return !src->scd->blocked;
152}
153
154static gboolean spice_char_source_dispatch(GSource *source,
155 GSourceFunc callback, gpointer user_data)
156{
157 GIOFunc func = (GIOFunc)callback;
158
159 return func(NULL, G_IO_OUT, user_data);
160}
161
162GSourceFuncs SpiceCharSourceFuncs = {
163 .prepare = spice_char_source_prepare,
164 .check = spice_char_source_check,
165 .dispatch = spice_char_source_dispatch,
166};
167
168static GSource *spice_chr_add_watch(CharDriverState *chr, GIOCondition cond)
169{
170 SpiceCharDriver *scd = chr->opaque;
171 SpiceCharSource *src;
172
173 assert(cond == G_IO_OUT);
174
175 src = (SpiceCharSource *)g_source_new(&SpiceCharSourceFuncs,
176 sizeof(SpiceCharSource));
177 src->scd = scd;
178
179 return (GSource *)src;
180}
Alon Levycbcc6332011-01-19 10:49:50 +0200181
182static int spice_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
183{
184 SpiceCharDriver *s = chr->opaque;
Hans de Goedeae893e52013-04-05 11:30:22 +0200185 int read_bytes;
Alon Levycbcc6332011-01-19 10:49:50 +0200186
Alon Levycbcc6332011-01-19 10:49:50 +0200187 assert(s->datalen == 0);
Alon Levyb010cec2013-04-05 11:30:23 +0200188 s->datapos = buf;
Alon Levycbcc6332011-01-19 10:49:50 +0200189 s->datalen = len;
190 spice_server_char_device_wakeup(&s->sin);
Hans de Goedeae893e52013-04-05 11:30:22 +0200191 read_bytes = len - s->datalen;
192 if (read_bytes != len) {
193 /* We'll get passed in the unconsumed data with the next call */
194 s->datalen = 0;
195 s->datapos = NULL;
196 s->blocked = true;
197 }
198 return read_bytes;
Alon Levycbcc6332011-01-19 10:49:50 +0200199}
200
201static void spice_chr_close(struct CharDriverState *chr)
202{
203 SpiceCharDriver *s = chr->opaque;
204
Alon Levycbcc6332011-01-19 10:49:50 +0200205 vmc_unregister_interface(s);
Marc-André Lureau7a5448c2012-12-05 16:15:35 +0100206 QLIST_REMOVE(s, next);
Hans de Goede5e9b4732013-03-13 10:41:31 +0100207
208 g_free((char *)s->sin.subtype);
209#if SPICE_SERVER_VERSION >= 0x000c02
210 g_free((char *)s->sin.portname);
211#endif
Anthony Liguori7267c092011-08-20 22:09:37 -0500212 g_free(s);
Alon Levycbcc6332011-01-19 10:49:50 +0200213}
214
Hans de Goede574b7112013-03-26 11:07:58 +0100215static void spice_chr_set_fe_open(struct CharDriverState *chr, int fe_open)
Hans de Goedecd8f7df2011-03-24 11:12:04 +0100216{
217 SpiceCharDriver *s = chr->opaque;
Hans de Goede574b7112013-03-26 11:07:58 +0100218 if (fe_open) {
219 vmc_register_interface(s);
220 } else {
221 vmc_unregister_interface(s);
222 }
Hans de Goedecd8f7df2011-03-24 11:12:04 +0100223}
224
Alon Levycbcc6332011-01-19 10:49:50 +0200225static void print_allowed_subtypes(void)
226{
227 const char** psubtype;
228 int i;
229
230 fprintf(stderr, "allowed names: ");
231 for(i=0, psubtype = spice_server_char_device_recognized_subtypes();
232 *psubtype != NULL; ++psubtype, ++i) {
233 if (i == 0) {
234 fprintf(stderr, "%s", *psubtype);
235 } else {
236 fprintf(stderr, ", %s", *psubtype);
237 }
238 }
239 fprintf(stderr, "\n");
240}
241
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100242static CharDriverState *chr_open(const char *subtype)
Alon Levycbcc6332011-01-19 10:49:50 +0200243{
244 CharDriverState *chr;
245 SpiceCharDriver *s;
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100246
247 chr = g_malloc0(sizeof(CharDriverState));
248 s = g_malloc0(sizeof(SpiceCharDriver));
249 s->chr = chr;
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100250 s->active = false;
Hans de Goede5e9b4732013-03-13 10:41:31 +0100251 s->sin.subtype = g_strdup(subtype);
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100252 chr->opaque = s;
253 chr->chr_write = spice_chr_write;
Hans de Goedeae893e52013-04-05 11:30:22 +0200254 chr->chr_add_watch = spice_chr_add_watch;
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100255 chr->chr_close = spice_chr_close;
Hans de Goede574b7112013-03-26 11:07:58 +0100256 chr->chr_set_fe_open = spice_chr_set_fe_open;
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100257
Marc-André Lureau7a5448c2012-12-05 16:15:35 +0100258 QLIST_INSERT_HEAD(&spice_chars, s, next);
259
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100260 return chr;
261}
262
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100263CharDriverState *qemu_chr_open_spice_vmc(const char *type)
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100264{
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100265 const char **psubtype = spice_server_char_device_recognized_subtypes();
Alon Levycbcc6332011-01-19 10:49:50 +0200266
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100267 if (type == NULL) {
Alon Levycbcc6332011-01-19 10:49:50 +0200268 fprintf(stderr, "spice-qemu-char: missing name parameter\n");
269 print_allowed_subtypes();
Markus Armbruster1f514702012-02-07 15:09:08 +0100270 return NULL;
Alon Levycbcc6332011-01-19 10:49:50 +0200271 }
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100272 for (; *psubtype != NULL; ++psubtype) {
273 if (strcmp(type, *psubtype) == 0) {
Alon Levycbcc6332011-01-19 10:49:50 +0200274 break;
275 }
276 }
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100277 if (*psubtype == NULL) {
278 fprintf(stderr, "spice-qemu-char: unsupported type: %s\n", type);
Alon Levycbcc6332011-01-19 10:49:50 +0200279 print_allowed_subtypes();
Markus Armbruster1f514702012-02-07 15:09:08 +0100280 return NULL;
Alon Levycbcc6332011-01-19 10:49:50 +0200281 }
282
Hans de Goede52fe0e72013-04-05 11:30:21 +0200283 return chr_open(type);
Alon Levycbcc6332011-01-19 10:49:50 +0200284}
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100285
286#if SPICE_SERVER_VERSION >= 0x000c02
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100287CharDriverState *qemu_chr_open_spice_port(const char *name)
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100288{
289 CharDriverState *chr;
290 SpiceCharDriver *s;
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100291
292 if (name == NULL) {
293 fprintf(stderr, "spice-qemu-char: missing name parameter\n");
294 return NULL;
295 }
296
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100297 chr = chr_open("port");
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100298 s = chr->opaque;
Hans de Goede5e9b4732013-03-13 10:41:31 +0100299 s->sin.portname = g_strdup(name);
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100300
301 return chr;
302}
Marc-André Lureauafd0b402012-12-05 16:15:36 +0100303
304void qemu_spice_register_ports(void)
305{
306 SpiceCharDriver *s;
307
308 QLIST_FOREACH(s, &spice_chars, next) {
309 if (s->sin.portname == NULL) {
310 continue;
311 }
312 vmc_register_interface(s);
313 }
314}
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100315#endif
Anthony Liguori26c60612013-03-05 23:21:29 +0530316
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100317static void qemu_chr_parse_spice_vmc(QemuOpts *opts, ChardevBackend *backend,
318 Error **errp)
319{
320 const char *name = qemu_opt_get(opts, "name");
321
322 if (name == NULL) {
323 error_setg(errp, "chardev: spice channel: no name given");
324 return;
325 }
326 backend->spicevmc = g_new0(ChardevSpiceChannel, 1);
327 backend->spicevmc->type = g_strdup(name);
328}
329
330static void qemu_chr_parse_spice_port(QemuOpts *opts, ChardevBackend *backend,
331 Error **errp)
332{
333 const char *name = qemu_opt_get(opts, "name");
334
335 if (name == NULL) {
336 error_setg(errp, "chardev: spice port: no name given");
337 return;
338 }
339 backend->spiceport = g_new0(ChardevSpicePort, 1);
340 backend->spiceport->fqdn = g_strdup(name);
341}
342
Anthony Liguori26c60612013-03-05 23:21:29 +0530343static void register_types(void)
344{
Gerd Hoffmanncd153e22013-02-25 12:39:06 +0100345 register_char_driver_qapi("spicevmc", CHARDEV_BACKEND_KIND_SPICEVMC,
346 qemu_chr_parse_spice_vmc);
347 register_char_driver_qapi("spiceport", CHARDEV_BACKEND_KIND_SPICEPORT,
348 qemu_chr_parse_spice_port);
Anthony Liguori26c60612013-03-05 23:21:29 +0530349}
350
351type_init(register_types);