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