1 #include "qemu/osdep.h"
3 #include "ui/qemu-spice.h"
4 #include "chardev/char.h"
5 #include "qapi/error.h"
6 #include "qemu/error-report.h"
7 #include "qemu/option.h"
9 #include <spice/protocol.h>
12 typedef struct SpiceChardev
{
15 SpiceCharDeviceInstance sin
;
18 const uint8_t *datapos
;
20 QLIST_ENTRY(SpiceChardev
) next
;
23 #define TYPE_CHARDEV_SPICE "chardev-spice"
24 #define TYPE_CHARDEV_SPICEVMC "chardev-spicevmc"
25 #define TYPE_CHARDEV_SPICEPORT "chardev-spiceport"
27 #define SPICE_CHARDEV(obj) OBJECT_CHECK(SpiceChardev, (obj), TYPE_CHARDEV_SPICE)
29 typedef struct SpiceCharSource
{
34 static QLIST_HEAD(, SpiceChardev
) spice_chars
=
35 QLIST_HEAD_INITIALIZER(spice_chars
);
37 static int vmc_write(SpiceCharDeviceInstance
*sin
, const uint8_t *buf
, int len
)
39 SpiceChardev
*scd
= container_of(sin
, SpiceChardev
, sin
);
40 Chardev
*chr
= CHARDEV(scd
);
43 uint8_t* p
= (uint8_t*)buf
;
46 int can_write
= qemu_chr_be_can_write(chr
);
47 last_out
= MIN(len
, can_write
);
51 qemu_chr_be_write(chr
, p
, last_out
);
57 trace_spice_vmc_write(out
, len
+ out
);
61 static int vmc_read(SpiceCharDeviceInstance
*sin
, uint8_t *buf
, int len
)
63 SpiceChardev
*scd
= container_of(sin
, SpiceChardev
, sin
);
64 int bytes
= MIN(len
, scd
->datalen
);
67 memcpy(buf
, scd
->datapos
, bytes
);
68 scd
->datapos
+= bytes
;
69 scd
->datalen
-= bytes
;
70 assert(scd
->datalen
>= 0);
72 if (scd
->datalen
== 0) {
76 trace_spice_vmc_read(bytes
, len
);
80 #if SPICE_SERVER_VERSION >= 0x000c02
81 static void vmc_event(SpiceCharDeviceInstance
*sin
, uint8_t event
)
83 SpiceChardev
*scd
= container_of(sin
, SpiceChardev
, sin
);
84 Chardev
*chr
= CHARDEV(scd
);
88 case SPICE_PORT_EVENT_BREAK
:
89 chr_event
= CHR_EVENT_BREAK
;
95 trace_spice_vmc_event(chr_event
);
96 qemu_chr_be_event(chr
, chr_event
);
100 static void vmc_state(SpiceCharDeviceInstance
*sin
, int connected
)
102 SpiceChardev
*scd
= container_of(sin
, SpiceChardev
, sin
);
103 Chardev
*chr
= CHARDEV(scd
);
105 if ((chr
->be_open
&& connected
) ||
106 (!chr
->be_open
&& !connected
)) {
110 qemu_chr_be_event(chr
,
111 connected
? CHR_EVENT_OPENED
: CHR_EVENT_CLOSED
);
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
,
122 #if SPICE_SERVER_VERSION >= 0x000c02
125 #if SPICE_SERVER_VERSION >= 0x000c06
126 .flags
= SPICE_CHAR_DEVICE_NOTIFY_WRITABLE
,
131 static void vmc_register_interface(SpiceChardev
*scd
)
136 scd
->sin
.base
.sif
= &vmc_interface
.base
;
137 qemu_spice_add_interface(&scd
->sin
.base
);
139 trace_spice_vmc_register_interface(scd
);
142 static void vmc_unregister_interface(SpiceChardev
*scd
)
147 spice_server_remove_interface(&scd
->sin
.base
);
149 trace_spice_vmc_unregister_interface(scd
);
152 static gboolean
spice_char_source_prepare(GSource
*source
, gint
*timeout
)
154 SpiceCharSource
*src
= (SpiceCharSource
*)source
;
158 return !src
->scd
->blocked
;
161 static gboolean
spice_char_source_check(GSource
*source
)
163 SpiceCharSource
*src
= (SpiceCharSource
*)source
;
165 return !src
->scd
->blocked
;
168 static gboolean
spice_char_source_dispatch(GSource
*source
,
169 GSourceFunc callback
, gpointer user_data
)
171 GIOFunc func
= (GIOFunc
)callback
;
173 return func(NULL
, G_IO_OUT
, user_data
);
176 static GSourceFuncs SpiceCharSourceFuncs
= {
177 .prepare
= spice_char_source_prepare
,
178 .check
= spice_char_source_check
,
179 .dispatch
= spice_char_source_dispatch
,
182 static GSource
*spice_chr_add_watch(Chardev
*chr
, GIOCondition cond
)
184 SpiceChardev
*scd
= SPICE_CHARDEV(chr
);
185 SpiceCharSource
*src
;
187 assert(cond
& G_IO_OUT
);
189 src
= (SpiceCharSource
*)g_source_new(&SpiceCharSourceFuncs
,
190 sizeof(SpiceCharSource
));
193 return (GSource
*)src
;
196 static int spice_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
198 SpiceChardev
*s
= SPICE_CHARDEV(chr
);
201 assert(s
->datalen
== 0);
204 spice_server_char_device_wakeup(&s
->sin
);
205 read_bytes
= len
- s
->datalen
;
206 if (read_bytes
!= len
) {
207 /* We'll get passed in the unconsumed data with the next call */
215 static void char_spice_finalize(Object
*obj
)
217 SpiceChardev
*s
= SPICE_CHARDEV(obj
);
219 vmc_unregister_interface(s
);
221 if (s
->next
.le_prev
) {
222 QLIST_REMOVE(s
, next
);
225 g_free((char *)s
->sin
.subtype
);
226 #if SPICE_SERVER_VERSION >= 0x000c02
227 g_free((char *)s
->sin
.portname
);
231 static void spice_vmc_set_fe_open(struct Chardev
*chr
, int fe_open
)
233 SpiceChardev
*s
= SPICE_CHARDEV(chr
);
235 vmc_register_interface(s
);
237 vmc_unregister_interface(s
);
241 static void spice_port_set_fe_open(struct Chardev
*chr
, int fe_open
)
243 #if SPICE_SERVER_VERSION >= 0x000c02
244 SpiceChardev
*s
= SPICE_CHARDEV(chr
);
247 spice_server_port_event(&s
->sin
, SPICE_PORT_EVENT_OPENED
);
249 spice_server_port_event(&s
->sin
, SPICE_PORT_EVENT_CLOSED
);
254 static void spice_chr_accept_input(struct Chardev
*chr
)
256 SpiceChardev
*s
= SPICE_CHARDEV(chr
);
258 spice_server_char_device_wakeup(&s
->sin
);
261 static void chr_open(Chardev
*chr
, const char *subtype
)
263 SpiceChardev
*s
= SPICE_CHARDEV(chr
);
266 s
->sin
.subtype
= g_strdup(subtype
);
268 QLIST_INSERT_HEAD(&spice_chars
, s
, next
);
271 static void qemu_chr_open_spice_vmc(Chardev
*chr
,
272 ChardevBackend
*backend
,
276 ChardevSpiceChannel
*spicevmc
= backend
->u
.spicevmc
.data
;
277 const char *type
= spicevmc
->type
;
278 const char **psubtype
= spice_server_char_device_recognized_subtypes();
280 for (; *psubtype
!= NULL
; ++psubtype
) {
281 if (strcmp(type
, *psubtype
) == 0) {
285 if (*psubtype
== NULL
) {
286 char *subtypes
= g_strjoinv(", ",
287 (gchar
**)spice_server_char_device_recognized_subtypes());
289 error_setg(errp
, "unsupported type name: %s", type
);
290 error_append_hint(errp
, "allowed spice char type names: %s\n",
301 #if SPICE_SERVER_VERSION >= 0x000c02
302 static void qemu_chr_open_spice_port(Chardev
*chr
,
303 ChardevBackend
*backend
,
307 ChardevSpicePort
*spiceport
= backend
->u
.spiceport
.data
;
308 const char *name
= spiceport
->fqdn
;
312 error_setg(errp
, "missing name parameter");
316 chr_open(chr
, "port");
319 s
= SPICE_CHARDEV(chr
);
320 s
->sin
.portname
= g_strdup(name
);
323 void qemu_spice_register_ports(void)
327 QLIST_FOREACH(s
, &spice_chars
, next
) {
328 if (s
->sin
.portname
== NULL
) {
331 vmc_register_interface(s
);
336 static void qemu_chr_parse_spice_vmc(QemuOpts
*opts
, ChardevBackend
*backend
,
339 const char *name
= qemu_opt_get(opts
, "name");
340 ChardevSpiceChannel
*spicevmc
;
343 error_setg(errp
, "chardev: spice channel: no name given");
346 backend
->type
= CHARDEV_BACKEND_KIND_SPICEVMC
;
347 spicevmc
= backend
->u
.spicevmc
.data
= g_new0(ChardevSpiceChannel
, 1);
348 qemu_chr_parse_common(opts
, qapi_ChardevSpiceChannel_base(spicevmc
));
349 spicevmc
->type
= g_strdup(name
);
352 static void qemu_chr_parse_spice_port(QemuOpts
*opts
, ChardevBackend
*backend
,
355 const char *name
= qemu_opt_get(opts
, "name");
356 ChardevSpicePort
*spiceport
;
359 error_setg(errp
, "chardev: spice port: no name given");
362 backend
->type
= CHARDEV_BACKEND_KIND_SPICEPORT
;
363 spiceport
= backend
->u
.spiceport
.data
= g_new0(ChardevSpicePort
, 1);
364 qemu_chr_parse_common(opts
, qapi_ChardevSpicePort_base(spiceport
));
365 spiceport
->fqdn
= g_strdup(name
);
368 static void char_spice_class_init(ObjectClass
*oc
, void *data
)
370 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
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
;
377 static const TypeInfo char_spice_type_info
= {
378 .name
= TYPE_CHARDEV_SPICE
,
379 .parent
= TYPE_CHARDEV
,
380 .instance_size
= sizeof(SpiceChardev
),
381 .instance_finalize
= char_spice_finalize
,
382 .class_init
= char_spice_class_init
,
386 static void char_spicevmc_class_init(ObjectClass
*oc
, void *data
)
388 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
390 cc
->parse
= qemu_chr_parse_spice_vmc
;
391 cc
->open
= qemu_chr_open_spice_vmc
;
392 cc
->chr_set_fe_open
= spice_vmc_set_fe_open
;
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
,
401 static void char_spiceport_class_init(ObjectClass
*oc
, void *data
)
403 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
405 cc
->parse
= qemu_chr_parse_spice_port
;
406 cc
->open
= qemu_chr_open_spice_port
;
407 cc
->chr_set_fe_open
= spice_port_set_fe_open
;
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
,
416 static void register_types(void)
418 type_register_static(&char_spice_type_info
);
419 type_register_static(&char_spicevmc_type_info
);
420 type_register_static(&char_spiceport_type_info
);
423 type_init(register_types
);