1 #include "config-host.h"
3 #include "ui/qemu-spice.h"
6 #include <spice-experimental.h>
8 #include "qemu/osdep.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 #define VMC_MAX_HOST_WRITE 2048
20 typedef struct SpiceCharDriver
{
22 SpiceCharDeviceInstance sin
;
27 ssize_t bufsize
, datalen
;
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
, VMC_MAX_HOST_WRITE
);
40 if (qemu_chr_be_can_write(scd
->chr
) < last_out
) {
43 qemu_chr_be_write(scd
->chr
, p
, last_out
);
49 dprintf(scd
, 3, "%s: %zu/%zd\n", __func__
, out
, len
+ out
);
50 trace_spice_vmc_write(out
, len
+ out
);
54 static int vmc_read(SpiceCharDeviceInstance
*sin
, uint8_t *buf
, int len
)
56 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
57 int bytes
= MIN(len
, scd
->datalen
);
59 dprintf(scd
, 2, "%s: %p %d/%d/%zd\n", __func__
, scd
->datapos
, len
, bytes
, scd
->datalen
);
61 memcpy(buf
, scd
->datapos
, bytes
);
62 scd
->datapos
+= bytes
;
63 scd
->datalen
-= bytes
;
64 assert(scd
->datalen
>= 0);
65 if (scd
->datalen
== 0) {
69 trace_spice_vmc_read(bytes
, len
);
73 static void vmc_state(SpiceCharDeviceInstance
*sin
, int connected
)
75 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
77 #if SPICE_SERVER_VERSION < 0x000901
79 * spice-server calls the state callback for the agent channel when the
80 * spice client connects / disconnects. Given that not the client but
81 * the server is doing the parsing of the messages this is wrong as the
82 * server is still listening. Worse, this causes the parser in the server
83 * to go out of sync, so we ignore state calls for subtype vdagent
84 * spicevmc chardevs. For the full story see:
85 * http://lists.freedesktop.org/archives/spice-devel/2011-July/004837.html
87 if (strcmp(sin
->subtype
, "vdagent") == 0) {
92 if ((scd
->chr
->opened
&& connected
) ||
93 (!scd
->chr
->opened
&& !connected
)) {
97 qemu_chr_be_event(scd
->chr
,
98 connected
? CHR_EVENT_OPENED
: CHR_EVENT_CLOSED
);
101 static SpiceCharDeviceInterface vmc_interface
= {
102 .base
.type
= SPICE_INTERFACE_CHAR_DEVICE
,
103 .base
.description
= "spice virtual channel char device",
104 .base
.major_version
= SPICE_INTERFACE_CHAR_DEVICE_MAJOR
,
105 .base
.minor_version
= SPICE_INTERFACE_CHAR_DEVICE_MINOR
,
112 static void vmc_register_interface(SpiceCharDriver
*scd
)
117 dprintf(scd
, 1, "%s\n", __func__
);
118 scd
->sin
.base
.sif
= &vmc_interface
.base
;
119 qemu_spice_add_interface(&scd
->sin
.base
);
121 trace_spice_vmc_register_interface(scd
);
124 static void vmc_unregister_interface(SpiceCharDriver
*scd
)
129 dprintf(scd
, 1, "%s\n", __func__
);
130 spice_server_remove_interface(&scd
->sin
.base
);
132 trace_spice_vmc_unregister_interface(scd
);
136 static int spice_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
138 SpiceCharDriver
*s
= chr
->opaque
;
140 dprintf(s
, 2, "%s: %d\n", __func__
, len
);
141 vmc_register_interface(s
);
142 assert(s
->datalen
== 0);
143 if (s
->bufsize
< len
) {
145 s
->buffer
= g_realloc(s
->buffer
, s
->bufsize
);
147 memcpy(s
->buffer
, buf
, len
);
148 s
->datapos
= s
->buffer
;
150 spice_server_char_device_wakeup(&s
->sin
);
154 static void spice_chr_close(struct CharDriverState
*chr
)
156 SpiceCharDriver
*s
= chr
->opaque
;
158 printf("%s\n", __func__
);
159 vmc_unregister_interface(s
);
163 static void spice_chr_guest_open(struct CharDriverState
*chr
)
165 SpiceCharDriver
*s
= chr
->opaque
;
166 vmc_register_interface(s
);
169 static void spice_chr_guest_close(struct CharDriverState
*chr
)
171 SpiceCharDriver
*s
= chr
->opaque
;
172 vmc_unregister_interface(s
);
175 static void print_allowed_subtypes(void)
177 const char** psubtype
;
180 fprintf(stderr
, "allowed names: ");
181 for(i
=0, psubtype
= spice_server_char_device_recognized_subtypes();
182 *psubtype
!= NULL
; ++psubtype
, ++i
) {
184 fprintf(stderr
, "%s", *psubtype
);
186 fprintf(stderr
, ", %s", *psubtype
);
189 fprintf(stderr
, "\n");
192 CharDriverState
*qemu_chr_open_spice(QemuOpts
*opts
)
194 CharDriverState
*chr
;
196 const char* name
= qemu_opt_get(opts
, "name");
197 uint32_t debug
= qemu_opt_get_number(opts
, "debug", 0);
198 const char** psubtype
= spice_server_char_device_recognized_subtypes();
199 const char *subtype
= NULL
;
202 fprintf(stderr
, "spice-qemu-char: missing name parameter\n");
203 print_allowed_subtypes();
206 for(;*psubtype
!= NULL
; ++psubtype
) {
207 if (strcmp(name
, *psubtype
) == 0) {
212 if (subtype
== NULL
) {
213 fprintf(stderr
, "spice-qemu-char: unsupported name: %s\n", name
);
214 print_allowed_subtypes();
218 chr
= g_malloc0(sizeof(CharDriverState
));
219 s
= g_malloc0(sizeof(SpiceCharDriver
));
223 s
->sin
.subtype
= subtype
;
225 chr
->chr_write
= spice_chr_write
;
226 chr
->chr_close
= spice_chr_close
;
227 chr
->chr_guest_open
= spice_chr_guest_open
;
228 chr
->chr_guest_close
= spice_chr_guest_close
;
230 #if SPICE_SERVER_VERSION < 0x000901
231 /* See comment in vmc_state() */
232 if (strcmp(subtype
, "vdagent") == 0) {
233 qemu_chr_generic_open(chr
);