1 #include "config-host.h"
3 #include "ui/qemu-spice.h"
5 #include <spice-experimental.h>
6 #include <spice/protocol.h>
10 #define dprintf(_scd, _level, _fmt, ...) \
12 static unsigned __dprintf_counter = 0; \
13 if (_scd->debug >= _level) { \
14 fprintf(stderr, "scd: %3d: " _fmt, ++__dprintf_counter, ## __VA_ARGS__);\
18 typedef struct SpiceCharDriver
{
20 SpiceCharDeviceInstance sin
;
25 ssize_t bufsize
, datalen
;
27 QLIST_ENTRY(SpiceCharDriver
) next
;
30 static QLIST_HEAD(, SpiceCharDriver
) spice_chars
=
31 QLIST_HEAD_INITIALIZER(spice_chars
);
33 static int vmc_write(SpiceCharDeviceInstance
*sin
, const uint8_t *buf
, int len
)
35 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
38 uint8_t* p
= (uint8_t*)buf
;
41 last_out
= MIN(len
, qemu_chr_be_can_write(scd
->chr
));
45 qemu_chr_be_write(scd
->chr
, p
, last_out
);
51 dprintf(scd
, 3, "%s: %zu/%zd\n", __func__
, out
, len
+ out
);
52 trace_spice_vmc_write(out
, len
+ out
);
56 static int vmc_read(SpiceCharDeviceInstance
*sin
, uint8_t *buf
, int len
)
58 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
59 int bytes
= MIN(len
, scd
->datalen
);
61 dprintf(scd
, 2, "%s: %p %d/%d/%zd\n", __func__
, scd
->datapos
, len
, bytes
, scd
->datalen
);
63 memcpy(buf
, scd
->datapos
, bytes
);
64 scd
->datapos
+= bytes
;
65 scd
->datalen
-= bytes
;
66 assert(scd
->datalen
>= 0);
67 if (scd
->datalen
== 0) {
71 trace_spice_vmc_read(bytes
, len
);
75 #if SPICE_SERVER_VERSION >= 0x000c02
76 static void vmc_event(SpiceCharDeviceInstance
*sin
, uint8_t event
)
78 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
82 case SPICE_PORT_EVENT_BREAK
:
83 chr_event
= CHR_EVENT_BREAK
;
86 dprintf(scd
, 2, "%s: unknown %d\n", __func__
, event
);
90 dprintf(scd
, 2, "%s: %d\n", __func__
, event
);
91 trace_spice_vmc_event(chr_event
);
92 qemu_chr_be_event(scd
->chr
, chr_event
);
96 static void vmc_state(SpiceCharDeviceInstance
*sin
, int connected
)
98 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
100 #if SPICE_SERVER_VERSION < 0x000901
102 * spice-server calls the state callback for the agent channel when the
103 * spice client connects / disconnects. Given that not the client but
104 * the server is doing the parsing of the messages this is wrong as the
105 * server is still listening. Worse, this causes the parser in the server
106 * to go out of sync, so we ignore state calls for subtype vdagent
107 * spicevmc chardevs. For the full story see:
108 * http://lists.freedesktop.org/archives/spice-devel/2011-July/004837.html
110 if (strcmp(sin
->subtype
, "vdagent") == 0) {
115 if ((scd
->chr
->opened
&& connected
) ||
116 (!scd
->chr
->opened
&& !connected
)) {
120 qemu_chr_be_event(scd
->chr
,
121 connected
? CHR_EVENT_OPENED
: CHR_EVENT_CLOSED
);
124 static SpiceCharDeviceInterface vmc_interface
= {
125 .base
.type
= SPICE_INTERFACE_CHAR_DEVICE
,
126 .base
.description
= "spice virtual channel char device",
127 .base
.major_version
= SPICE_INTERFACE_CHAR_DEVICE_MAJOR
,
128 .base
.minor_version
= SPICE_INTERFACE_CHAR_DEVICE_MINOR
,
132 #if SPICE_SERVER_VERSION >= 0x000c02
138 static void vmc_register_interface(SpiceCharDriver
*scd
)
143 dprintf(scd
, 1, "%s\n", __func__
);
144 scd
->sin
.base
.sif
= &vmc_interface
.base
;
145 qemu_spice_add_interface(&scd
->sin
.base
);
147 trace_spice_vmc_register_interface(scd
);
150 static void vmc_unregister_interface(SpiceCharDriver
*scd
)
155 dprintf(scd
, 1, "%s\n", __func__
);
156 spice_server_remove_interface(&scd
->sin
.base
);
158 trace_spice_vmc_unregister_interface(scd
);
162 static int spice_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
164 SpiceCharDriver
*s
= chr
->opaque
;
166 dprintf(s
, 2, "%s: %d\n", __func__
, len
);
167 vmc_register_interface(s
);
168 assert(s
->datalen
== 0);
169 if (s
->bufsize
< len
) {
171 s
->buffer
= g_realloc(s
->buffer
, s
->bufsize
);
173 memcpy(s
->buffer
, buf
, len
);
174 s
->datapos
= s
->buffer
;
176 spice_server_char_device_wakeup(&s
->sin
);
180 static void spice_chr_close(struct CharDriverState
*chr
)
182 SpiceCharDriver
*s
= chr
->opaque
;
184 printf("%s\n", __func__
);
185 vmc_unregister_interface(s
);
186 QLIST_REMOVE(s
, next
);
190 static void spice_chr_guest_open(struct CharDriverState
*chr
)
192 SpiceCharDriver
*s
= chr
->opaque
;
193 vmc_register_interface(s
);
196 static void spice_chr_guest_close(struct CharDriverState
*chr
)
198 SpiceCharDriver
*s
= chr
->opaque
;
199 vmc_unregister_interface(s
);
202 static void print_allowed_subtypes(void)
204 const char** psubtype
;
207 fprintf(stderr
, "allowed names: ");
208 for(i
=0, psubtype
= spice_server_char_device_recognized_subtypes();
209 *psubtype
!= NULL
; ++psubtype
, ++i
) {
211 fprintf(stderr
, "%s", *psubtype
);
213 fprintf(stderr
, ", %s", *psubtype
);
216 fprintf(stderr
, "\n");
219 static CharDriverState
*chr_open(QemuOpts
*opts
, const char *subtype
)
221 CharDriverState
*chr
;
223 uint32_t debug
= qemu_opt_get_number(opts
, "debug", 0);
225 chr
= g_malloc0(sizeof(CharDriverState
));
226 s
= g_malloc0(sizeof(SpiceCharDriver
));
230 s
->sin
.subtype
= subtype
;
232 chr
->chr_write
= spice_chr_write
;
233 chr
->chr_close
= spice_chr_close
;
234 chr
->chr_guest_open
= spice_chr_guest_open
;
235 chr
->chr_guest_close
= spice_chr_guest_close
;
237 QLIST_INSERT_HEAD(&spice_chars
, s
, next
);
242 CharDriverState
*qemu_chr_open_spice(QemuOpts
*opts
)
244 CharDriverState
*chr
;
245 const char *name
= qemu_opt_get(opts
, "name");
246 const char **psubtype
= spice_server_char_device_recognized_subtypes();
247 const char *subtype
= NULL
;
250 fprintf(stderr
, "spice-qemu-char: missing name parameter\n");
251 print_allowed_subtypes();
254 for(;*psubtype
!= NULL
; ++psubtype
) {
255 if (strcmp(name
, *psubtype
) == 0) {
260 if (subtype
== NULL
) {
261 fprintf(stderr
, "spice-qemu-char: unsupported name: %s\n", name
);
262 print_allowed_subtypes();
266 chr
= chr_open(opts
, subtype
);
268 #if SPICE_SERVER_VERSION < 0x000901
269 /* See comment in vmc_state() */
270 if (strcmp(subtype
, "vdagent") == 0) {
271 qemu_chr_generic_open(chr
);
278 #if SPICE_SERVER_VERSION >= 0x000c02
279 CharDriverState
*qemu_chr_open_spice_port(QemuOpts
*opts
)
281 CharDriverState
*chr
;
283 const char *name
= qemu_opt_get(opts
, "name");
286 fprintf(stderr
, "spice-qemu-char: missing name parameter\n");
290 chr
= chr_open(opts
, "port");
292 s
->sin
.portname
= name
;