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