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