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
;
29 static int vmc_write(SpiceCharDeviceInstance
*sin
, const uint8_t *buf
, int len
)
31 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
34 uint8_t* p
= (uint8_t*)buf
;
37 last_out
= MIN(len
, qemu_chr_be_can_write(scd
->chr
));
41 qemu_chr_be_write(scd
->chr
, p
, last_out
);
47 dprintf(scd
, 3, "%s: %zu/%zd\n", __func__
, out
, len
+ out
);
48 trace_spice_vmc_write(out
, len
+ out
);
52 static int vmc_read(SpiceCharDeviceInstance
*sin
, uint8_t *buf
, int len
)
54 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
55 int bytes
= MIN(len
, scd
->datalen
);
57 dprintf(scd
, 2, "%s: %p %d/%d/%zd\n", __func__
, scd
->datapos
, len
, bytes
, scd
->datalen
);
59 memcpy(buf
, scd
->datapos
, bytes
);
60 scd
->datapos
+= bytes
;
61 scd
->datalen
-= bytes
;
62 assert(scd
->datalen
>= 0);
63 if (scd
->datalen
== 0) {
67 trace_spice_vmc_read(bytes
, len
);
71 #if SPICE_SERVER_VERSION >= 0x000c02
72 static void vmc_event(SpiceCharDeviceInstance
*sin
, uint8_t event
)
74 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
78 case SPICE_PORT_EVENT_BREAK
:
79 chr_event
= CHR_EVENT_BREAK
;
82 dprintf(scd
, 2, "%s: unknown %d\n", __func__
, event
);
86 dprintf(scd
, 2, "%s: %d\n", __func__
, event
);
87 trace_spice_vmc_event(chr_event
);
88 qemu_chr_be_event(scd
->chr
, chr_event
);
92 static void vmc_state(SpiceCharDeviceInstance
*sin
, int connected
)
94 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
96 #if SPICE_SERVER_VERSION < 0x000901
98 * spice-server calls the state callback for the agent channel when the
99 * spice client connects / disconnects. Given that not the client but
100 * the server is doing the parsing of the messages this is wrong as the
101 * server is still listening. Worse, this causes the parser in the server
102 * to go out of sync, so we ignore state calls for subtype vdagent
103 * spicevmc chardevs. For the full story see:
104 * http://lists.freedesktop.org/archives/spice-devel/2011-July/004837.html
106 if (strcmp(sin
->subtype
, "vdagent") == 0) {
111 if ((scd
->chr
->opened
&& connected
) ||
112 (!scd
->chr
->opened
&& !connected
)) {
116 qemu_chr_be_event(scd
->chr
,
117 connected
? CHR_EVENT_OPENED
: CHR_EVENT_CLOSED
);
120 static SpiceCharDeviceInterface vmc_interface
= {
121 .base
.type
= SPICE_INTERFACE_CHAR_DEVICE
,
122 .base
.description
= "spice virtual channel char device",
123 .base
.major_version
= SPICE_INTERFACE_CHAR_DEVICE_MAJOR
,
124 .base
.minor_version
= SPICE_INTERFACE_CHAR_DEVICE_MINOR
,
128 #if SPICE_SERVER_VERSION >= 0x000c02
134 static void vmc_register_interface(SpiceCharDriver
*scd
)
139 dprintf(scd
, 1, "%s\n", __func__
);
140 scd
->sin
.base
.sif
= &vmc_interface
.base
;
141 qemu_spice_add_interface(&scd
->sin
.base
);
143 trace_spice_vmc_register_interface(scd
);
146 static void vmc_unregister_interface(SpiceCharDriver
*scd
)
151 dprintf(scd
, 1, "%s\n", __func__
);
152 spice_server_remove_interface(&scd
->sin
.base
);
154 trace_spice_vmc_unregister_interface(scd
);
158 static int spice_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
160 SpiceCharDriver
*s
= chr
->opaque
;
162 dprintf(s
, 2, "%s: %d\n", __func__
, len
);
163 vmc_register_interface(s
);
164 assert(s
->datalen
== 0);
165 if (s
->bufsize
< len
) {
167 s
->buffer
= g_realloc(s
->buffer
, s
->bufsize
);
169 memcpy(s
->buffer
, buf
, len
);
170 s
->datapos
= s
->buffer
;
172 spice_server_char_device_wakeup(&s
->sin
);
176 static void spice_chr_close(struct CharDriverState
*chr
)
178 SpiceCharDriver
*s
= chr
->opaque
;
180 printf("%s\n", __func__
);
181 vmc_unregister_interface(s
);
185 static void spice_chr_guest_open(struct CharDriverState
*chr
)
187 SpiceCharDriver
*s
= chr
->opaque
;
188 vmc_register_interface(s
);
191 static void spice_chr_guest_close(struct CharDriverState
*chr
)
193 SpiceCharDriver
*s
= chr
->opaque
;
194 vmc_unregister_interface(s
);
197 static void print_allowed_subtypes(void)
199 const char** psubtype
;
202 fprintf(stderr
, "allowed names: ");
203 for(i
=0, psubtype
= spice_server_char_device_recognized_subtypes();
204 *psubtype
!= NULL
; ++psubtype
, ++i
) {
206 fprintf(stderr
, "%s", *psubtype
);
208 fprintf(stderr
, ", %s", *psubtype
);
211 fprintf(stderr
, "\n");
214 static CharDriverState
*chr_open(QemuOpts
*opts
, const char *subtype
)
216 CharDriverState
*chr
;
218 uint32_t debug
= qemu_opt_get_number(opts
, "debug", 0);
220 chr
= g_malloc0(sizeof(CharDriverState
));
221 s
= g_malloc0(sizeof(SpiceCharDriver
));
225 s
->sin
.subtype
= subtype
;
227 chr
->chr_write
= spice_chr_write
;
228 chr
->chr_close
= spice_chr_close
;
229 chr
->chr_guest_open
= spice_chr_guest_open
;
230 chr
->chr_guest_close
= spice_chr_guest_close
;
235 CharDriverState
*qemu_chr_open_spice(QemuOpts
*opts
)
237 CharDriverState
*chr
;
238 const char *name
= qemu_opt_get(opts
, "name");
239 const char **psubtype
= spice_server_char_device_recognized_subtypes();
240 const char *subtype
= NULL
;
243 fprintf(stderr
, "spice-qemu-char: missing name parameter\n");
244 print_allowed_subtypes();
247 for(;*psubtype
!= NULL
; ++psubtype
) {
248 if (strcmp(name
, *psubtype
) == 0) {
253 if (subtype
== NULL
) {
254 fprintf(stderr
, "spice-qemu-char: unsupported name: %s\n", name
);
255 print_allowed_subtypes();
259 chr
= chr_open(opts
, subtype
);
261 #if SPICE_SERVER_VERSION < 0x000901
262 /* See comment in vmc_state() */
263 if (strcmp(subtype
, "vdagent") == 0) {
264 qemu_chr_generic_open(chr
);
271 #if SPICE_SERVER_VERSION >= 0x000c02
272 CharDriverState
*qemu_chr_open_spice_port(QemuOpts
*opts
)
274 CharDriverState
*chr
;
276 const char *name
= qemu_opt_get(opts
, "name");
279 fprintf(stderr
, "spice-qemu-char: missing name parameter\n");
283 chr
= chr_open(opts
, "port");
285 s
->sin
.portname
= name
;