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