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