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