]> git.proxmox.com Git - mirror_qemu.git/blame - chardev/spice.c
spice: do not stop spice if VM is paused
[mirror_qemu.git] / chardev / spice.c
CommitLineData
d38ea87a 1#include "qemu/osdep.h"
6b10e573 2#include "trace.h"
cbcc6336 3#include "ui/qemu-spice.h"
8228e353 4#include "chardev/char.h"
e688df6b 5#include "qapi/error.h"
096b4894 6#include "qemu/error-report.h"
922a01a0 7#include "qemu/option.h"
cbcc6336 8#include <spice.h>
5a49d3e9 9#include <spice/protocol.h>
cbcc6336 10
cbcc6336 11
0ec7b3e7
MAL
12typedef struct SpiceChardev {
13 Chardev parent;
41ac54b2 14
0ec7b3e7 15 SpiceCharDeviceInstance sin;
cbcc6336 16 bool active;
ae893e5e 17 bool blocked;
b010cec8
AL
18 const uint8_t *datapos;
19 int datalen;
0ec7b3e7
MAL
20 QLIST_ENTRY(SpiceChardev) next;
21} SpiceChardev;
cbcc6336 22
777357d7
MAL
23#define TYPE_CHARDEV_SPICE "chardev-spice"
24#define TYPE_CHARDEV_SPICEVMC "chardev-spicevmc"
25#define TYPE_CHARDEV_SPICEPORT "chardev-spiceport"
26
27#define SPICE_CHARDEV(obj) OBJECT_CHECK(SpiceChardev, (obj), TYPE_CHARDEV_SPICE)
28
ae893e5e
HG
29typedef struct SpiceCharSource {
30 GSource source;
0ec7b3e7 31 SpiceChardev *scd;
ae893e5e
HG
32} SpiceCharSource;
33
0ec7b3e7 34static QLIST_HEAD(, SpiceChardev) spice_chars =
7a5448ce
MAL
35 QLIST_HEAD_INITIALIZER(spice_chars);
36
cbcc6336
AL
37static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
38{
0ec7b3e7 39 SpiceChardev *scd = container_of(sin, SpiceChardev, sin);
777357d7 40 Chardev *chr = CHARDEV(scd);
cbcc6336
AL
41 ssize_t out = 0;
42 ssize_t last_out;
43 uint8_t* p = (uint8_t*)buf;
44
45 while (len > 0) {
41ac54b2 46 int can_write = qemu_chr_be_can_write(chr);
75c439bc 47 last_out = MIN(len, can_write);
07a54d70 48 if (last_out <= 0) {
cbcc6336
AL
49 break;
50 }
41ac54b2 51 qemu_chr_be_write(chr, p, last_out);
35106c2d
HG
52 out += last_out;
53 len -= last_out;
54 p += last_out;
cbcc6336
AL
55 }
56
cbcc6336
AL
57 trace_spice_vmc_write(out, len + out);
58 return out;
59}
60
61static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)
62{
0ec7b3e7 63 SpiceChardev *scd = container_of(sin, SpiceChardev, sin);
cbcc6336
AL
64 int bytes = MIN(len, scd->datalen);
65
cbcc6336
AL
66 if (bytes > 0) {
67 memcpy(buf, scd->datapos, bytes);
68 scd->datapos += bytes;
69 scd->datalen -= bytes;
70 assert(scd->datalen >= 0);
ae893e5e
HG
71 }
72 if (scd->datalen == 0) {
73 scd->datapos = 0;
74 scd->blocked = false;
cbcc6336
AL
75 }
76 trace_spice_vmc_read(bytes, len);
77 return bytes;
78}
79
5a49d3e9
MAL
80static void vmc_event(SpiceCharDeviceInstance *sin, uint8_t event)
81{
0ec7b3e7 82 SpiceChardev *scd = container_of(sin, SpiceChardev, sin);
777357d7 83 Chardev *chr = CHARDEV(scd);
5a49d3e9
MAL
84 int chr_event;
85
86 switch (event) {
87 case SPICE_PORT_EVENT_BREAK:
88 chr_event = CHR_EVENT_BREAK;
89 break;
90 default:
5a49d3e9
MAL
91 return;
92 }
93
5a49d3e9 94 trace_spice_vmc_event(chr_event);
41ac54b2 95 qemu_chr_be_event(chr, chr_event);
5a49d3e9 96}
5a49d3e9 97
f76e4c7f
HG
98static void vmc_state(SpiceCharDeviceInstance *sin, int connected)
99{
0ec7b3e7 100 SpiceChardev *scd = container_of(sin, SpiceChardev, sin);
777357d7 101 Chardev *chr = CHARDEV(scd);
f76e4c7f 102
41ac54b2
MAL
103 if ((chr->be_open && connected) ||
104 (!chr->be_open && !connected)) {
f76e4c7f
HG
105 return;
106 }
107
41ac54b2 108 qemu_chr_be_event(chr,
f76e4c7f
HG
109 connected ? CHR_EVENT_OPENED : CHR_EVENT_CLOSED);
110}
111
cbcc6336
AL
112static SpiceCharDeviceInterface vmc_interface = {
113 .base.type = SPICE_INTERFACE_CHAR_DEVICE,
114 .base.description = "spice virtual channel char device",
115 .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,
116 .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,
f76e4c7f 117 .state = vmc_state,
cbcc6336
AL
118 .write = vmc_write,
119 .read = vmc_read,
5a49d3e9 120 .event = vmc_event,
e95e203c
MAL
121#if SPICE_SERVER_VERSION >= 0x000c06
122 .flags = SPICE_CHAR_DEVICE_NOTIFY_WRITABLE,
123#endif
cbcc6336
AL
124};
125
126
0ec7b3e7 127static void vmc_register_interface(SpiceChardev *scd)
cbcc6336
AL
128{
129 if (scd->active) {
130 return;
131 }
cbcc6336
AL
132 scd->sin.base.sif = &vmc_interface.base;
133 qemu_spice_add_interface(&scd->sin.base);
134 scd->active = true;
135 trace_spice_vmc_register_interface(scd);
136}
137
0ec7b3e7 138static void vmc_unregister_interface(SpiceChardev *scd)
cbcc6336
AL
139{
140 if (!scd->active) {
141 return;
142 }
cbcc6336
AL
143 spice_server_remove_interface(&scd->sin.base);
144 scd->active = false;
145 trace_spice_vmc_unregister_interface(scd);
146}
147
ae893e5e
HG
148static gboolean spice_char_source_prepare(GSource *source, gint *timeout)
149{
150 SpiceCharSource *src = (SpiceCharSource *)source;
805189ab 151 Chardev *chr = CHARDEV(src->scd);
ae893e5e
HG
152
153 *timeout = -1;
154
805189ab
MAL
155 if (!chr->be_open) {
156 return true;
157 }
158
ae893e5e
HG
159 return !src->scd->blocked;
160}
161
162static gboolean spice_char_source_check(GSource *source)
163{
164 SpiceCharSource *src = (SpiceCharSource *)source;
805189ab
MAL
165 Chardev *chr = CHARDEV(src->scd);
166
167 if (!chr->be_open) {
168 return true;
169 }
ae893e5e
HG
170
171 return !src->scd->blocked;
172}
173
174static gboolean spice_char_source_dispatch(GSource *source,
175 GSourceFunc callback, gpointer user_data)
176{
805189ab
MAL
177 SpiceCharSource *src = (SpiceCharSource *)source;
178 Chardev *chr = CHARDEV(src->scd);
ae893e5e 179 GIOFunc func = (GIOFunc)callback;
805189ab 180 GIOCondition cond = chr->be_open ? G_IO_OUT : G_IO_HUP;
ae893e5e 181
805189ab 182 return func(NULL, cond, user_data);
ae893e5e
HG
183}
184
51575c3f 185static GSourceFuncs SpiceCharSourceFuncs = {
ae893e5e
HG
186 .prepare = spice_char_source_prepare,
187 .check = spice_char_source_check,
188 .dispatch = spice_char_source_dispatch,
189};
190
0ec7b3e7 191static GSource *spice_chr_add_watch(Chardev *chr, GIOCondition cond)
ae893e5e 192{
777357d7 193 SpiceChardev *scd = SPICE_CHARDEV(chr);
ae893e5e
HG
194 SpiceCharSource *src;
195
f7a8beb5 196 assert(cond & G_IO_OUT);
ae893e5e
HG
197
198 src = (SpiceCharSource *)g_source_new(&SpiceCharSourceFuncs,
199 sizeof(SpiceCharSource));
200 src->scd = scd;
201
202 return (GSource *)src;
203}
cbcc6336 204
0ec7b3e7 205static int spice_chr_write(Chardev *chr, const uint8_t *buf, int len)
cbcc6336 206{
777357d7 207 SpiceChardev *s = SPICE_CHARDEV(chr);
ae893e5e 208 int read_bytes;
cbcc6336 209
cbcc6336 210 assert(s->datalen == 0);
a2dc3c8e
MAL
211
212 if (!chr->be_open) {
213 trace_spice_chr_discard_write(len);
214 return len;
215 }
216
b010cec8 217 s->datapos = buf;
cbcc6336
AL
218 s->datalen = len;
219 spice_server_char_device_wakeup(&s->sin);
ae893e5e
HG
220 read_bytes = len - s->datalen;
221 if (read_bytes != len) {
222 /* We'll get passed in the unconsumed data with the next call */
223 s->datalen = 0;
224 s->datapos = NULL;
225 s->blocked = true;
226 }
227 return read_bytes;
cbcc6336
AL
228}
229
18c508ac 230static void char_spice_finalize(Object *obj)
cbcc6336 231{
18c508ac 232 SpiceChardev *s = SPICE_CHARDEV(obj);
cbcc6336 233
cbcc6336 234 vmc_unregister_interface(s);
f20e6f8c
LQ
235
236 if (s->next.le_prev) {
237 QLIST_REMOVE(s, next);
238 }
5e9b473a
HG
239
240 g_free((char *)s->sin.subtype);
5e9b473a 241 g_free((char *)s->sin.portname);
cbcc6336
AL
242}
243
0ec7b3e7 244static void spice_vmc_set_fe_open(struct Chardev *chr, int fe_open)
cd8f7df2 245{
777357d7 246 SpiceChardev *s = SPICE_CHARDEV(chr);
574b711a
HG
247 if (fe_open) {
248 vmc_register_interface(s);
249 } else {
250 vmc_unregister_interface(s);
251 }
cd8f7df2
HG
252}
253
0ec7b3e7 254static void spice_port_set_fe_open(struct Chardev *chr, int fe_open)
89091146 255{
777357d7 256 SpiceChardev *s = SPICE_CHARDEV(chr);
89091146
MAL
257
258 if (fe_open) {
259 spice_server_port_event(&s->sin, SPICE_PORT_EVENT_OPENED);
260 } else {
261 spice_server_port_event(&s->sin, SPICE_PORT_EVENT_CLOSED);
262 }
89091146
MAL
263}
264
0ec7b3e7 265static void spice_chr_accept_input(struct Chardev *chr)
e95e203c 266{
777357d7 267 SpiceChardev *s = SPICE_CHARDEV(chr);
e95e203c
MAL
268
269 spice_server_char_device_wakeup(&s->sin);
270}
271
777357d7 272static void chr_open(Chardev *chr, const char *subtype)
cbcc6336 273{
777357d7 274 SpiceChardev *s = SPICE_CHARDEV(chr);
71b423f4 275
71b423f4 276 s->active = false;
5e9b473a 277 s->sin.subtype = g_strdup(subtype);
71b423f4 278
7a5448ce 279 QLIST_INSERT_HEAD(&spice_chars, s, next);
71b423f4
MAL
280}
281
777357d7
MAL
282static void qemu_chr_open_spice_vmc(Chardev *chr,
283 ChardevBackend *backend,
284 bool *be_opened,
285 Error **errp)
71b423f4 286{
32bafa8f
EB
287 ChardevSpiceChannel *spicevmc = backend->u.spicevmc.data;
288 const char *type = spicevmc->type;
71b423f4 289 const char **psubtype = spice_server_char_device_recognized_subtypes();
cbcc6336 290
cd153e2a
GH
291 for (; *psubtype != NULL; ++psubtype) {
292 if (strcmp(type, *psubtype) == 0) {
cbcc6336
AL
293 break;
294 }
295 }
cd153e2a 296 if (*psubtype == NULL) {
096b4894
MAL
297 char *subtypes = g_strjoinv(", ",
298 (gchar **)spice_server_char_device_recognized_subtypes());
299
300 error_setg(errp, "unsupported type name: %s", type);
301 error_append_hint(errp, "allowed spice char type names: %s\n",
302 subtypes);
303
304 g_free(subtypes);
777357d7 305 return;
cbcc6336
AL
306 }
307
82878dac 308 *be_opened = false;
a2dc3c8e
MAL
309#if SPICE_SERVER_VERSION < 0x000e02
310 /* Spice < 0.14.2 doesn't explicitly open smartcard chardev */
311 if (strcmp(type, "smartcard") == 0) {
312 *be_opened = true;
313 }
314#endif
777357d7 315 chr_open(chr, type);
cbcc6336 316}
5a49d3e9 317
777357d7
MAL
318static void qemu_chr_open_spice_port(Chardev *chr,
319 ChardevBackend *backend,
320 bool *be_opened,
321 Error **errp)
5a49d3e9 322{
32bafa8f
EB
323 ChardevSpicePort *spiceport = backend->u.spiceport.data;
324 const char *name = spiceport->fqdn;
0ec7b3e7 325 SpiceChardev *s;
5a49d3e9
MAL
326
327 if (name == NULL) {
096b4894 328 error_setg(errp, "missing name parameter");
777357d7 329 return;
5a49d3e9
MAL
330 }
331
777357d7
MAL
332 chr_open(chr, "port");
333
82878dac 334 *be_opened = false;
777357d7 335 s = SPICE_CHARDEV(chr);
5e9b473a 336 s->sin.portname = g_strdup(name);
5a49d3e9 337}
afd0b409
MAL
338
339void qemu_spice_register_ports(void)
340{
0ec7b3e7 341 SpiceChardev *s;
afd0b409
MAL
342
343 QLIST_FOREACH(s, &spice_chars, next) {
344 if (s->sin.portname == NULL) {
345 continue;
346 }
347 vmc_register_interface(s);
348 }
349}
26c60614 350
cd153e2a
GH
351static void qemu_chr_parse_spice_vmc(QemuOpts *opts, ChardevBackend *backend,
352 Error **errp)
353{
354 const char *name = qemu_opt_get(opts, "name");
21a933ea 355 ChardevSpiceChannel *spicevmc;
cd153e2a
GH
356
357 if (name == NULL) {
358 error_setg(errp, "chardev: spice channel: no name given");
359 return;
360 }
0b663b7d 361 backend->type = CHARDEV_BACKEND_KIND_SPICEVMC;
32bafa8f 362 spicevmc = backend->u.spicevmc.data = g_new0(ChardevSpiceChannel, 1);
21a933ea
EB
363 qemu_chr_parse_common(opts, qapi_ChardevSpiceChannel_base(spicevmc));
364 spicevmc->type = g_strdup(name);
cd153e2a
GH
365}
366
367static void qemu_chr_parse_spice_port(QemuOpts *opts, ChardevBackend *backend,
368 Error **errp)
369{
370 const char *name = qemu_opt_get(opts, "name");
21a933ea 371 ChardevSpicePort *spiceport;
cd153e2a
GH
372
373 if (name == NULL) {
374 error_setg(errp, "chardev: spice port: no name given");
375 return;
376 }
0b663b7d 377 backend->type = CHARDEV_BACKEND_KIND_SPICEPORT;
32bafa8f 378 spiceport = backend->u.spiceport.data = g_new0(ChardevSpicePort, 1);
21a933ea
EB
379 qemu_chr_parse_common(opts, qapi_ChardevSpicePort_base(spiceport));
380 spiceport->fqdn = g_strdup(name);
cd153e2a
GH
381}
382
777357d7
MAL
383static void char_spice_class_init(ObjectClass *oc, void *data)
384{
385 ChardevClass *cc = CHARDEV_CLASS(oc);
386
387 cc->chr_write = spice_chr_write;
388 cc->chr_add_watch = spice_chr_add_watch;
389 cc->chr_accept_input = spice_chr_accept_input;
777357d7
MAL
390}
391
392static const TypeInfo char_spice_type_info = {
393 .name = TYPE_CHARDEV_SPICE,
394 .parent = TYPE_CHARDEV,
395 .instance_size = sizeof(SpiceChardev),
18c508ac 396 .instance_finalize = char_spice_finalize,
777357d7
MAL
397 .class_init = char_spice_class_init,
398 .abstract = true,
399};
400
401static void char_spicevmc_class_init(ObjectClass *oc, void *data)
402{
403 ChardevClass *cc = CHARDEV_CLASS(oc);
404
88cace9f 405 cc->parse = qemu_chr_parse_spice_vmc;
777357d7
MAL
406 cc->open = qemu_chr_open_spice_vmc;
407 cc->chr_set_fe_open = spice_vmc_set_fe_open;
408}
409
410static const TypeInfo char_spicevmc_type_info = {
411 .name = TYPE_CHARDEV_SPICEVMC,
412 .parent = TYPE_CHARDEV_SPICE,
413 .class_init = char_spicevmc_class_init,
414};
415
416static void char_spiceport_class_init(ObjectClass *oc, void *data)
417{
418 ChardevClass *cc = CHARDEV_CLASS(oc);
419
88cace9f 420 cc->parse = qemu_chr_parse_spice_port;
777357d7
MAL
421 cc->open = qemu_chr_open_spice_port;
422 cc->chr_set_fe_open = spice_port_set_fe_open;
423}
424
425static const TypeInfo char_spiceport_type_info = {
426 .name = TYPE_CHARDEV_SPICEPORT,
427 .parent = TYPE_CHARDEV_SPICE,
428 .class_init = char_spiceport_class_init,
429};
430
26c60614
AL
431static void register_types(void)
432{
777357d7
MAL
433 type_register_static(&char_spice_type_info);
434 type_register_static(&char_spicevmc_type_info);
435 type_register_static(&char_spiceport_type_info);
26c60614
AL
436}
437
438type_init(register_types);