1 #include "qemu/osdep.h"
2 #include "trace-root.h"
3 #include "ui/qemu-spice.h"
4 #include "sysemu/char.h"
5 #include "qemu/error-report.h"
7 #include <spice/protocol.h>
10 typedef struct SpiceChardev {
13 SpiceCharDeviceInstance sin;
16 const uint8_t *datapos;
18 QLIST_ENTRY(SpiceChardev) next;
21 #define TYPE_CHARDEV_SPICE "chardev-spice"
22 #define TYPE_CHARDEV_SPICEVMC "chardev-spicevmc"
23 #define TYPE_CHARDEV_SPICEPORT "chardev-spiceport"
25 #define SPICE_CHARDEV(obj) OBJECT_CHECK(SpiceChardev, (obj), TYPE_CHARDEV_SPICE)
27 typedef struct SpiceCharSource {
32 static QLIST_HEAD(, SpiceChardev) spice_chars =
33 QLIST_HEAD_INITIALIZER(spice_chars);
35 static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
37 SpiceChardev *scd = container_of(sin, SpiceChardev, sin);
38 Chardev *chr = CHARDEV(scd);
41 uint8_t* p = (uint8_t*)buf;
44 int can_write = qemu_chr_be_can_write(chr);
45 last_out = MIN(len, can_write);
49 qemu_chr_be_write(chr, p, last_out);
55 trace_spice_vmc_write(out, len + out);
59 static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)
61 SpiceChardev *scd = container_of(sin, SpiceChardev, sin);
62 int bytes = MIN(len, scd->datalen);
65 memcpy(buf, scd->datapos, bytes);
66 scd->datapos += bytes;
67 scd->datalen -= bytes;
68 assert(scd->datalen >= 0);
70 if (scd->datalen == 0) {
74 trace_spice_vmc_read(bytes, len);
78 #if SPICE_SERVER_VERSION >= 0x000c02
79 static void vmc_event(SpiceCharDeviceInstance *sin, uint8_t event)
81 SpiceChardev *scd = container_of(sin, SpiceChardev, sin);
82 Chardev *chr = CHARDEV(scd);
86 case SPICE_PORT_EVENT_BREAK:
87 chr_event = CHR_EVENT_BREAK;
93 trace_spice_vmc_event(chr_event);
94 qemu_chr_be_event(chr, chr_event);
98 static void vmc_state(SpiceCharDeviceInstance *sin, int connected)
100 SpiceChardev *scd = container_of(sin, SpiceChardev, sin);
101 Chardev *chr = CHARDEV(scd);
103 if ((chr->be_open && connected) ||
104 (!chr->be_open && !connected)) {
108 qemu_chr_be_event(chr,
109 connected ? CHR_EVENT_OPENED : CHR_EVENT_CLOSED);
112 static SpiceCharDeviceInterface vmc_interface = {
113 .base.type = SPICE_INTERFACE_CHAR_DEVICE,
114 .base.description = "spice virtual channel char device",
115 .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,
116 .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,
120 #if SPICE_SERVER_VERSION >= 0x000c02
123 #if SPICE_SERVER_VERSION >= 0x000c06
124 .flags = SPICE_CHAR_DEVICE_NOTIFY_WRITABLE,
129 static void vmc_register_interface(SpiceChardev *scd)
134 scd->sin.base.sif = &vmc_interface.base;
135 qemu_spice_add_interface(&scd->sin.base);
137 trace_spice_vmc_register_interface(scd);
140 static void vmc_unregister_interface(SpiceChardev *scd)
145 spice_server_remove_interface(&scd->sin.base);
147 trace_spice_vmc_unregister_interface(scd);
150 static gboolean spice_char_source_prepare(GSource *source, gint *timeout)
152 SpiceCharSource *src = (SpiceCharSource *)source;
156 return !src->scd->blocked;
159 static gboolean spice_char_source_check(GSource *source)
161 SpiceCharSource *src = (SpiceCharSource *)source;
163 return !src->scd->blocked;
166 static gboolean spice_char_source_dispatch(GSource *source,
167 GSourceFunc callback, gpointer user_data)
169 GIOFunc func = (GIOFunc)callback;
171 return func(NULL, G_IO_OUT, user_data);
174 static GSourceFuncs SpiceCharSourceFuncs = {
175 .prepare = spice_char_source_prepare,
176 .check = spice_char_source_check,
177 .dispatch = spice_char_source_dispatch,
180 static GSource *spice_chr_add_watch(Chardev *chr, GIOCondition cond)
182 SpiceChardev *scd = SPICE_CHARDEV(chr);
183 SpiceCharSource *src;
185 assert(cond & G_IO_OUT);
187 src = (SpiceCharSource *)g_source_new(&SpiceCharSourceFuncs,
188 sizeof(SpiceCharSource));
191 return (GSource *)src;
194 static int spice_chr_write(Chardev *chr, const uint8_t *buf, int len)
196 SpiceChardev *s = SPICE_CHARDEV(chr);
199 assert(s->datalen == 0);
202 spice_server_char_device_wakeup(&s->sin);
203 read_bytes = len - s->datalen;
204 if (read_bytes != len) {
205 /* We'll get passed in the unconsumed data with the next call */
213 static void char_spice_finalize(Object *obj)
215 SpiceChardev *s = SPICE_CHARDEV(obj);
217 vmc_unregister_interface(s);
219 if (s->next.le_prev) {
220 QLIST_REMOVE(s, next);
223 g_free((char *)s->sin.subtype);
224 #if SPICE_SERVER_VERSION >= 0x000c02
225 g_free((char *)s->sin.portname);
229 static void spice_vmc_set_fe_open(struct Chardev *chr, int fe_open)
231 SpiceChardev *s = SPICE_CHARDEV(chr);
233 vmc_register_interface(s);
235 vmc_unregister_interface(s);
239 static void spice_port_set_fe_open(struct Chardev *chr, int fe_open)
241 #if SPICE_SERVER_VERSION >= 0x000c02
242 SpiceChardev *s = SPICE_CHARDEV(chr);
245 spice_server_port_event(&s->sin, SPICE_PORT_EVENT_OPENED);
247 spice_server_port_event(&s->sin, SPICE_PORT_EVENT_CLOSED);
252 static void spice_chr_accept_input(struct Chardev *chr)
254 SpiceChardev *s = SPICE_CHARDEV(chr);
256 spice_server_char_device_wakeup(&s->sin);
259 static void chr_open(Chardev *chr, const char *subtype)
261 SpiceChardev *s = SPICE_CHARDEV(chr);
264 s->sin.subtype = g_strdup(subtype);
266 QLIST_INSERT_HEAD(&spice_chars, s, next);
269 static void qemu_chr_open_spice_vmc(Chardev *chr,
270 ChardevBackend *backend,
274 ChardevSpiceChannel *spicevmc = backend->u.spicevmc.data;
275 const char *type = spicevmc->type;
276 const char **psubtype = spice_server_char_device_recognized_subtypes();
278 for (; *psubtype != NULL; ++psubtype) {
279 if (strcmp(type, *psubtype) == 0) {
283 if (*psubtype == NULL) {
284 char *subtypes = g_strjoinv(", ",
285 (gchar **)spice_server_char_device_recognized_subtypes());
287 error_setg(errp, "unsupported type name: %s", type);
288 error_append_hint(errp, "allowed spice char type names: %s\n",
299 #if SPICE_SERVER_VERSION >= 0x000c02
300 static void qemu_chr_open_spice_port(Chardev *chr,
301 ChardevBackend *backend,
305 ChardevSpicePort *spiceport = backend->u.spiceport.data;
306 const char *name = spiceport->fqdn;
310 error_setg(errp, "missing name parameter");
314 chr_open(chr, "port");
317 s = SPICE_CHARDEV(chr);
318 s->sin.portname = g_strdup(name);
321 void qemu_spice_register_ports(void)
325 QLIST_FOREACH(s, &spice_chars, next) {
326 if (s->sin.portname == NULL) {
329 vmc_register_interface(s);
334 static void qemu_chr_parse_spice_vmc(QemuOpts *opts, ChardevBackend *backend,
337 const char *name = qemu_opt_get(opts, "name");
338 ChardevSpiceChannel *spicevmc;
341 error_setg(errp, "chardev: spice channel: no name given");
344 backend->type = CHARDEV_BACKEND_KIND_SPICEVMC;
345 spicevmc = backend->u.spicevmc.data = g_new0(ChardevSpiceChannel, 1);
346 qemu_chr_parse_common(opts, qapi_ChardevSpiceChannel_base(spicevmc));
347 spicevmc->type = g_strdup(name);
350 static void qemu_chr_parse_spice_port(QemuOpts *opts, ChardevBackend *backend,
353 const char *name = qemu_opt_get(opts, "name");
354 ChardevSpicePort *spiceport;
357 error_setg(errp, "chardev: spice port: no name given");
360 backend->type = CHARDEV_BACKEND_KIND_SPICEPORT;
361 spiceport = backend->u.spiceport.data = g_new0(ChardevSpicePort, 1);
362 qemu_chr_parse_common(opts, qapi_ChardevSpicePort_base(spiceport));
363 spiceport->fqdn = g_strdup(name);
366 static void char_spice_class_init(ObjectClass *oc, void *data)
368 ChardevClass *cc = CHARDEV_CLASS(oc);
370 cc->chr_write = spice_chr_write;
371 cc->chr_add_watch = spice_chr_add_watch;
372 cc->chr_accept_input = spice_chr_accept_input;
375 static const TypeInfo char_spice_type_info = {
376 .name = TYPE_CHARDEV_SPICE,
377 .parent = TYPE_CHARDEV,
378 .instance_size = sizeof(SpiceChardev),
379 .instance_finalize = char_spice_finalize,
380 .class_init = char_spice_class_init,
384 static void char_spicevmc_class_init(ObjectClass *oc, void *data)
386 ChardevClass *cc = CHARDEV_CLASS(oc);
388 cc->parse = qemu_chr_parse_spice_vmc;
389 cc->open = qemu_chr_open_spice_vmc;
390 cc->chr_set_fe_open = spice_vmc_set_fe_open;
393 static const TypeInfo char_spicevmc_type_info = {
394 .name = TYPE_CHARDEV_SPICEVMC,
395 .parent = TYPE_CHARDEV_SPICE,
396 .class_init = char_spicevmc_class_init,
399 static void char_spiceport_class_init(ObjectClass *oc, void *data)
401 ChardevClass *cc = CHARDEV_CLASS(oc);
403 cc->parse = qemu_chr_parse_spice_port;
404 cc->open = qemu_chr_open_spice_port;
405 cc->chr_set_fe_open = spice_port_set_fe_open;
408 static const TypeInfo char_spiceport_type_info = {
409 .name = TYPE_CHARDEV_SPICEPORT,
410 .parent = TYPE_CHARDEV_SPICE,
411 .class_init = char_spiceport_class_init,
414 static void register_types(void)
416 type_register_static(&char_spice_type_info);
417 type_register_static(&char_spicevmc_type_info);
418 type_register_static(&char_spiceport_type_info);
421 type_init(register_types);