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