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"
8 #include <spice/protocol.h>
11 typedef struct SpiceChardev
{
14 SpiceCharDeviceInstance sin
;
17 const uint8_t *datapos
;
19 QLIST_ENTRY(SpiceChardev
) next
;
22 #define TYPE_CHARDEV_SPICE "chardev-spice"
23 #define TYPE_CHARDEV_SPICEVMC "chardev-spicevmc"
24 #define TYPE_CHARDEV_SPICEPORT "chardev-spiceport"
26 #define SPICE_CHARDEV(obj) OBJECT_CHECK(SpiceChardev, (obj), TYPE_CHARDEV_SPICE)
28 typedef struct SpiceCharSource
{
33 static QLIST_HEAD(, SpiceChardev
) spice_chars
=
34 QLIST_HEAD_INITIALIZER(spice_chars
);
36 static int vmc_write(SpiceCharDeviceInstance
*sin
, const uint8_t *buf
, int len
)
38 SpiceChardev
*scd
= container_of(sin
, SpiceChardev
, sin
);
39 Chardev
*chr
= CHARDEV(scd
);
42 uint8_t* p
= (uint8_t*)buf
;
45 int can_write
= qemu_chr_be_can_write(chr
);
46 last_out
= MIN(len
, can_write
);
50 qemu_chr_be_write(chr
, p
, last_out
);
56 trace_spice_vmc_write(out
, len
+ out
);
60 static int vmc_read(SpiceCharDeviceInstance
*sin
, uint8_t *buf
, int len
)
62 SpiceChardev
*scd
= container_of(sin
, SpiceChardev
, sin
);
63 int bytes
= MIN(len
, scd
->datalen
);
66 memcpy(buf
, scd
->datapos
, bytes
);
67 scd
->datapos
+= bytes
;
68 scd
->datalen
-= bytes
;
69 assert(scd
->datalen
>= 0);
71 if (scd
->datalen
== 0) {
75 trace_spice_vmc_read(bytes
, len
);
79 #if SPICE_SERVER_VERSION >= 0x000c02
80 static void vmc_event(SpiceCharDeviceInstance
*sin
, uint8_t event
)
82 SpiceChardev
*scd
= container_of(sin
, SpiceChardev
, sin
);
83 Chardev
*chr
= CHARDEV(scd
);
87 case SPICE_PORT_EVENT_BREAK
:
88 chr_event
= CHR_EVENT_BREAK
;
94 trace_spice_vmc_event(chr_event
);
95 qemu_chr_be_event(chr
, chr_event
);
99 static void vmc_state(SpiceCharDeviceInstance
*sin
, int connected
)
101 SpiceChardev
*scd
= container_of(sin
, SpiceChardev
, sin
);
102 Chardev
*chr
= CHARDEV(scd
);
104 if ((chr
->be_open
&& connected
) ||
105 (!chr
->be_open
&& !connected
)) {
109 qemu_chr_be_event(chr
,
110 connected
? CHR_EVENT_OPENED
: CHR_EVENT_CLOSED
);
113 static SpiceCharDeviceInterface vmc_interface
= {
114 .base
.type
= SPICE_INTERFACE_CHAR_DEVICE
,
115 .base
.description
= "spice virtual channel char device",
116 .base
.major_version
= SPICE_INTERFACE_CHAR_DEVICE_MAJOR
,
117 .base
.minor_version
= SPICE_INTERFACE_CHAR_DEVICE_MINOR
,
121 #if SPICE_SERVER_VERSION >= 0x000c02
124 #if SPICE_SERVER_VERSION >= 0x000c06
125 .flags
= SPICE_CHAR_DEVICE_NOTIFY_WRITABLE
,
130 static void vmc_register_interface(SpiceChardev
*scd
)
135 scd
->sin
.base
.sif
= &vmc_interface
.base
;
136 qemu_spice_add_interface(&scd
->sin
.base
);
138 trace_spice_vmc_register_interface(scd
);
141 static void vmc_unregister_interface(SpiceChardev
*scd
)
146 spice_server_remove_interface(&scd
->sin
.base
);
148 trace_spice_vmc_unregister_interface(scd
);
151 static gboolean
spice_char_source_prepare(GSource
*source
, gint
*timeout
)
153 SpiceCharSource
*src
= (SpiceCharSource
*)source
;
157 return !src
->scd
->blocked
;
160 static gboolean
spice_char_source_check(GSource
*source
)
162 SpiceCharSource
*src
= (SpiceCharSource
*)source
;
164 return !src
->scd
->blocked
;
167 static gboolean
spice_char_source_dispatch(GSource
*source
,
168 GSourceFunc callback
, gpointer user_data
)
170 GIOFunc func
= (GIOFunc
)callback
;
172 return func(NULL
, G_IO_OUT
, user_data
);
175 static GSourceFuncs SpiceCharSourceFuncs
= {
176 .prepare
= spice_char_source_prepare
,
177 .check
= spice_char_source_check
,
178 .dispatch
= spice_char_source_dispatch
,
181 static GSource
*spice_chr_add_watch(Chardev
*chr
, GIOCondition cond
)
183 SpiceChardev
*scd
= SPICE_CHARDEV(chr
);
184 SpiceCharSource
*src
;
186 assert(cond
& G_IO_OUT
);
188 src
= (SpiceCharSource
*)g_source_new(&SpiceCharSourceFuncs
,
189 sizeof(SpiceCharSource
));
192 return (GSource
*)src
;
195 static int spice_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
197 SpiceChardev
*s
= SPICE_CHARDEV(chr
);
200 assert(s
->datalen
== 0);
203 spice_server_char_device_wakeup(&s
->sin
);
204 read_bytes
= len
- s
->datalen
;
205 if (read_bytes
!= len
) {
206 /* We'll get passed in the unconsumed data with the next call */
214 static void char_spice_finalize(Object
*obj
)
216 SpiceChardev
*s
= SPICE_CHARDEV(obj
);
218 vmc_unregister_interface(s
);
220 if (s
->next
.le_prev
) {
221 QLIST_REMOVE(s
, next
);
224 g_free((char *)s
->sin
.subtype
);
225 #if SPICE_SERVER_VERSION >= 0x000c02
226 g_free((char *)s
->sin
.portname
);
230 static void spice_vmc_set_fe_open(struct Chardev
*chr
, int fe_open
)
232 SpiceChardev
*s
= SPICE_CHARDEV(chr
);
234 vmc_register_interface(s
);
236 vmc_unregister_interface(s
);
240 static void spice_port_set_fe_open(struct Chardev
*chr
, int fe_open
)
242 #if SPICE_SERVER_VERSION >= 0x000c02
243 SpiceChardev
*s
= SPICE_CHARDEV(chr
);
246 spice_server_port_event(&s
->sin
, SPICE_PORT_EVENT_OPENED
);
248 spice_server_port_event(&s
->sin
, SPICE_PORT_EVENT_CLOSED
);
253 static void spice_chr_accept_input(struct Chardev
*chr
)
255 SpiceChardev
*s
= SPICE_CHARDEV(chr
);
257 spice_server_char_device_wakeup(&s
->sin
);
260 static void chr_open(Chardev
*chr
, const char *subtype
)
262 SpiceChardev
*s
= SPICE_CHARDEV(chr
);
265 s
->sin
.subtype
= g_strdup(subtype
);
267 QLIST_INSERT_HEAD(&spice_chars
, s
, next
);
270 static void qemu_chr_open_spice_vmc(Chardev
*chr
,
271 ChardevBackend
*backend
,
275 ChardevSpiceChannel
*spicevmc
= backend
->u
.spicevmc
.data
;
276 const char *type
= spicevmc
->type
;
277 const char **psubtype
= spice_server_char_device_recognized_subtypes();
279 for (; *psubtype
!= NULL
; ++psubtype
) {
280 if (strcmp(type
, *psubtype
) == 0) {
284 if (*psubtype
== NULL
) {
285 char *subtypes
= g_strjoinv(", ",
286 (gchar
**)spice_server_char_device_recognized_subtypes());
288 error_setg(errp
, "unsupported type name: %s", type
);
289 error_append_hint(errp
, "allowed spice char type names: %s\n",
300 #if SPICE_SERVER_VERSION >= 0x000c02
301 static void qemu_chr_open_spice_port(Chardev
*chr
,
302 ChardevBackend
*backend
,
306 ChardevSpicePort
*spiceport
= backend
->u
.spiceport
.data
;
307 const char *name
= spiceport
->fqdn
;
311 error_setg(errp
, "missing name parameter");
315 chr_open(chr
, "port");
318 s
= SPICE_CHARDEV(chr
);
319 s
->sin
.portname
= g_strdup(name
);
322 void qemu_spice_register_ports(void)
326 QLIST_FOREACH(s
, &spice_chars
, next
) {
327 if (s
->sin
.portname
== NULL
) {
330 vmc_register_interface(s
);
335 static void qemu_chr_parse_spice_vmc(QemuOpts
*opts
, ChardevBackend
*backend
,
338 const char *name
= qemu_opt_get(opts
, "name");
339 ChardevSpiceChannel
*spicevmc
;
342 error_setg(errp
, "chardev: spice channel: no name given");
345 backend
->type
= CHARDEV_BACKEND_KIND_SPICEVMC
;
346 spicevmc
= backend
->u
.spicevmc
.data
= g_new0(ChardevSpiceChannel
, 1);
347 qemu_chr_parse_common(opts
, qapi_ChardevSpiceChannel_base(spicevmc
));
348 spicevmc
->type
= g_strdup(name
);
351 static void qemu_chr_parse_spice_port(QemuOpts
*opts
, ChardevBackend
*backend
,
354 const char *name
= qemu_opt_get(opts
, "name");
355 ChardevSpicePort
*spiceport
;
358 error_setg(errp
, "chardev: spice port: no name given");
361 backend
->type
= CHARDEV_BACKEND_KIND_SPICEPORT
;
362 spiceport
= backend
->u
.spiceport
.data
= g_new0(ChardevSpicePort
, 1);
363 qemu_chr_parse_common(opts
, qapi_ChardevSpicePort_base(spiceport
));
364 spiceport
->fqdn
= g_strdup(name
);
367 static void char_spice_class_init(ObjectClass
*oc
, void *data
)
369 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
371 cc
->chr_write
= spice_chr_write
;
372 cc
->chr_add_watch
= spice_chr_add_watch
;
373 cc
->chr_accept_input
= spice_chr_accept_input
;
376 static const TypeInfo char_spice_type_info
= {
377 .name
= TYPE_CHARDEV_SPICE
,
378 .parent
= TYPE_CHARDEV
,
379 .instance_size
= sizeof(SpiceChardev
),
380 .instance_finalize
= char_spice_finalize
,
381 .class_init
= char_spice_class_init
,
385 static void char_spicevmc_class_init(ObjectClass
*oc
, void *data
)
387 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
389 cc
->parse
= qemu_chr_parse_spice_vmc
;
390 cc
->open
= qemu_chr_open_spice_vmc
;
391 cc
->chr_set_fe_open
= spice_vmc_set_fe_open
;
394 static const TypeInfo char_spicevmc_type_info
= {
395 .name
= TYPE_CHARDEV_SPICEVMC
,
396 .parent
= TYPE_CHARDEV_SPICE
,
397 .class_init
= char_spicevmc_class_init
,
400 static void char_spiceport_class_init(ObjectClass
*oc
, void *data
)
402 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
404 cc
->parse
= qemu_chr_parse_spice_port
;
405 cc
->open
= qemu_chr_open_spice_port
;
406 cc
->chr_set_fe_open
= spice_port_set_fe_open
;
409 static const TypeInfo char_spiceport_type_info
= {
410 .name
= TYPE_CHARDEV_SPICEPORT
,
411 .parent
= TYPE_CHARDEV_SPICE
,
412 .class_init
= char_spiceport_class_init
,
415 static void register_types(void)
417 type_register_static(&char_spice_type_info
);
418 type_register_static(&char_spicevmc_type_info
);
419 type_register_static(&char_spiceport_type_info
);
422 type_init(register_types
);