1 #include "config-host.h"
3 #include "ui/qemu-spice.h"
4 #include "sysemu/char.h"
6 #include <spice-experimental.h>
7 #include <spice/protocol.h>
9 #include "qemu/osdep.h"
11 typedef struct SpiceCharDriver
{
13 SpiceCharDeviceInstance sin
;
19 ssize_t bufsize
, datalen
;
20 QLIST_ENTRY(SpiceCharDriver
) next
;
23 typedef struct SpiceCharSource
{
28 static QLIST_HEAD(, SpiceCharDriver
) spice_chars
=
29 QLIST_HEAD_INITIALIZER(spice_chars
);
31 static int vmc_write(SpiceCharDeviceInstance
*sin
, const uint8_t *buf
, int len
)
33 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
36 uint8_t* p
= (uint8_t*)buf
;
39 last_out
= MIN(len
, qemu_chr_be_can_write(scd
->chr
));
43 qemu_chr_be_write(scd
->chr
, p
, last_out
);
49 trace_spice_vmc_write(out
, len
+ out
);
53 static int vmc_read(SpiceCharDeviceInstance
*sin
, uint8_t *buf
, int len
)
55 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
56 int bytes
= MIN(len
, scd
->datalen
);
59 memcpy(buf
, scd
->datapos
, bytes
);
60 scd
->datapos
+= bytes
;
61 scd
->datalen
-= bytes
;
62 assert(scd
->datalen
>= 0);
64 if (scd
->datalen
== 0) {
68 trace_spice_vmc_read(bytes
, len
);
72 #if SPICE_SERVER_VERSION >= 0x000c02
73 static void vmc_event(SpiceCharDeviceInstance
*sin
, uint8_t event
)
75 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
79 case SPICE_PORT_EVENT_BREAK
:
80 chr_event
= CHR_EVENT_BREAK
;
86 trace_spice_vmc_event(chr_event
);
87 qemu_chr_be_event(scd
->chr
, chr_event
);
91 static void vmc_state(SpiceCharDeviceInstance
*sin
, int connected
)
93 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
95 if ((scd
->chr
->be_open
&& connected
) ||
96 (!scd
->chr
->be_open
&& !connected
)) {
100 qemu_chr_be_event(scd
->chr
,
101 connected
? CHR_EVENT_OPENED
: CHR_EVENT_CLOSED
);
104 static SpiceCharDeviceInterface vmc_interface
= {
105 .base
.type
= SPICE_INTERFACE_CHAR_DEVICE
,
106 .base
.description
= "spice virtual channel char device",
107 .base
.major_version
= SPICE_INTERFACE_CHAR_DEVICE_MAJOR
,
108 .base
.minor_version
= SPICE_INTERFACE_CHAR_DEVICE_MINOR
,
112 #if SPICE_SERVER_VERSION >= 0x000c02
118 static void vmc_register_interface(SpiceCharDriver
*scd
)
123 scd
->sin
.base
.sif
= &vmc_interface
.base
;
124 qemu_spice_add_interface(&scd
->sin
.base
);
126 trace_spice_vmc_register_interface(scd
);
129 static void vmc_unregister_interface(SpiceCharDriver
*scd
)
134 spice_server_remove_interface(&scd
->sin
.base
);
136 trace_spice_vmc_unregister_interface(scd
);
139 static gboolean
spice_char_source_prepare(GSource
*source
, gint
*timeout
)
141 SpiceCharSource
*src
= (SpiceCharSource
*)source
;
145 return !src
->scd
->blocked
;
148 static gboolean
spice_char_source_check(GSource
*source
)
150 SpiceCharSource
*src
= (SpiceCharSource
*)source
;
152 return !src
->scd
->blocked
;
155 static gboolean
spice_char_source_dispatch(GSource
*source
,
156 GSourceFunc callback
, gpointer user_data
)
158 GIOFunc func
= (GIOFunc
)callback
;
160 return func(NULL
, G_IO_OUT
, user_data
);
163 GSourceFuncs SpiceCharSourceFuncs
= {
164 .prepare
= spice_char_source_prepare
,
165 .check
= spice_char_source_check
,
166 .dispatch
= spice_char_source_dispatch
,
169 static GSource
*spice_chr_add_watch(CharDriverState
*chr
, GIOCondition cond
)
171 SpiceCharDriver
*scd
= chr
->opaque
;
172 SpiceCharSource
*src
;
174 assert(cond
== G_IO_OUT
);
176 src
= (SpiceCharSource
*)g_source_new(&SpiceCharSourceFuncs
,
177 sizeof(SpiceCharSource
));
180 return (GSource
*)src
;
183 static int spice_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
185 SpiceCharDriver
*s
= chr
->opaque
;
188 assert(s
->datalen
== 0);
189 if (s
->bufsize
< len
) {
191 s
->buffer
= g_realloc(s
->buffer
, s
->bufsize
);
193 memcpy(s
->buffer
, buf
, len
);
194 s
->datapos
= s
->buffer
;
196 spice_server_char_device_wakeup(&s
->sin
);
197 read_bytes
= len
- s
->datalen
;
198 if (read_bytes
!= len
) {
199 /* We'll get passed in the unconsumed data with the next call */
207 static void spice_chr_close(struct CharDriverState
*chr
)
209 SpiceCharDriver
*s
= chr
->opaque
;
211 vmc_unregister_interface(s
);
212 QLIST_REMOVE(s
, next
);
214 g_free((char *)s
->sin
.subtype
);
215 #if SPICE_SERVER_VERSION >= 0x000c02
216 g_free((char *)s
->sin
.portname
);
221 static void spice_chr_set_fe_open(struct CharDriverState
*chr
, int fe_open
)
223 SpiceCharDriver
*s
= chr
->opaque
;
225 vmc_register_interface(s
);
227 vmc_unregister_interface(s
);
231 static void print_allowed_subtypes(void)
233 const char** psubtype
;
236 fprintf(stderr
, "allowed names: ");
237 for(i
=0, psubtype
= spice_server_char_device_recognized_subtypes();
238 *psubtype
!= NULL
; ++psubtype
, ++i
) {
240 fprintf(stderr
, "%s", *psubtype
);
242 fprintf(stderr
, ", %s", *psubtype
);
245 fprintf(stderr
, "\n");
248 static CharDriverState
*chr_open(const char *subtype
)
250 CharDriverState
*chr
;
253 chr
= g_malloc0(sizeof(CharDriverState
));
254 s
= g_malloc0(sizeof(SpiceCharDriver
));
257 s
->sin
.subtype
= g_strdup(subtype
);
259 chr
->chr_write
= spice_chr_write
;
260 chr
->chr_add_watch
= spice_chr_add_watch
;
261 chr
->chr_close
= spice_chr_close
;
262 chr
->chr_set_fe_open
= spice_chr_set_fe_open
;
264 QLIST_INSERT_HEAD(&spice_chars
, s
, next
);
269 CharDriverState
*qemu_chr_open_spice_vmc(const char *type
)
271 const char **psubtype
= spice_server_char_device_recognized_subtypes();
274 fprintf(stderr
, "spice-qemu-char: missing name parameter\n");
275 print_allowed_subtypes();
278 for (; *psubtype
!= NULL
; ++psubtype
) {
279 if (strcmp(type
, *psubtype
) == 0) {
283 if (*psubtype
== NULL
) {
284 fprintf(stderr
, "spice-qemu-char: unsupported type: %s\n", type
);
285 print_allowed_subtypes();
289 return chr_open(type
);
292 #if SPICE_SERVER_VERSION >= 0x000c02
293 CharDriverState
*qemu_chr_open_spice_port(const char *name
)
295 CharDriverState
*chr
;
299 fprintf(stderr
, "spice-qemu-char: missing name parameter\n");
303 chr
= chr_open("port");
305 s
->sin
.portname
= g_strdup(name
);
310 void qemu_spice_register_ports(void)
314 QLIST_FOREACH(s
, &spice_chars
, next
) {
315 if (s
->sin
.portname
== NULL
) {
318 vmc_register_interface(s
);
323 static void qemu_chr_parse_spice_vmc(QemuOpts
*opts
, ChardevBackend
*backend
,
326 const char *name
= qemu_opt_get(opts
, "name");
329 error_setg(errp
, "chardev: spice channel: no name given");
332 backend
->spicevmc
= g_new0(ChardevSpiceChannel
, 1);
333 backend
->spicevmc
->type
= g_strdup(name
);
336 static void qemu_chr_parse_spice_port(QemuOpts
*opts
, ChardevBackend
*backend
,
339 const char *name
= qemu_opt_get(opts
, "name");
342 error_setg(errp
, "chardev: spice port: no name given");
345 backend
->spiceport
= g_new0(ChardevSpicePort
, 1);
346 backend
->spiceport
->fqdn
= g_strdup(name
);
349 static void register_types(void)
351 register_char_driver_qapi("spicevmc", CHARDEV_BACKEND_KIND_SPICEVMC
,
352 qemu_chr_parse_spice_vmc
);
353 register_char_driver_qapi("spiceport", CHARDEV_BACKEND_KIND_SPICEPORT
,
354 qemu_chr_parse_spice_port
);
357 type_init(register_types
);