1 #include "config-host.h"
3 #include "ui/qemu-spice.h"
5 #include <spice-experimental.h>
9 #define dprintf(_scd, _level, _fmt, ...) \
11 static unsigned __dprintf_counter = 0; \
12 if (_scd->debug >= _level) { \
13 fprintf(stderr, "scd: %3d: " _fmt, ++__dprintf_counter, ## __VA_ARGS__);\
17 #define VMC_MAX_HOST_WRITE 2048
19 typedef struct SpiceCharDriver
{
21 SpiceCharDeviceInstance sin
;
26 ssize_t bufsize
, datalen
;
30 static int vmc_write(SpiceCharDeviceInstance
*sin
, const uint8_t *buf
, int len
)
32 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
35 uint8_t* p
= (uint8_t*)buf
;
38 last_out
= MIN(len
, VMC_MAX_HOST_WRITE
);
39 if (qemu_chr_be_can_write(scd
->chr
) < last_out
) {
42 qemu_chr_be_write(scd
->chr
, p
, last_out
);
48 dprintf(scd
, 3, "%s: %zu/%zd\n", __func__
, out
, len
+ 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
);
58 dprintf(scd
, 2, "%s: %p %d/%d/%zd\n", __func__
, scd
->datapos
, len
, bytes
, scd
->datalen
);
60 memcpy(buf
, scd
->datapos
, bytes
);
61 scd
->datapos
+= bytes
;
62 scd
->datalen
-= bytes
;
63 assert(scd
->datalen
>= 0);
64 if (scd
->datalen
== 0) {
68 trace_spice_vmc_read(bytes
, len
);
72 static void vmc_state(SpiceCharDeviceInstance
*sin
, int connected
)
74 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
76 #if SPICE_SERVER_VERSION < 0x000901
78 * spice-server calls the state callback for the agent channel when the
79 * spice client connects / disconnects. Given that not the client but
80 * the server is doing the parsing of the messages this is wrong as the
81 * server is still listening. Worse, this causes the parser in the server
82 * to go out of sync, so we ignore state calls for subtype vdagent
83 * spicevmc chardevs. For the full story see:
84 * http://lists.freedesktop.org/archives/spice-devel/2011-July/004837.html
86 if (strcmp(sin
->subtype
, "vdagent") == 0) {
91 if ((scd
->chr
->opened
&& connected
) ||
92 (!scd
->chr
->opened
&& !connected
)) {
96 qemu_chr_be_event(scd
->chr
,
97 connected
? CHR_EVENT_OPENED
: CHR_EVENT_CLOSED
);
100 static SpiceCharDeviceInterface vmc_interface
= {
101 .base
.type
= SPICE_INTERFACE_CHAR_DEVICE
,
102 .base
.description
= "spice virtual channel char device",
103 .base
.major_version
= SPICE_INTERFACE_CHAR_DEVICE_MAJOR
,
104 .base
.minor_version
= SPICE_INTERFACE_CHAR_DEVICE_MINOR
,
111 static void vmc_register_interface(SpiceCharDriver
*scd
)
116 dprintf(scd
, 1, "%s\n", __func__
);
117 scd
->sin
.base
.sif
= &vmc_interface
.base
;
118 qemu_spice_add_interface(&scd
->sin
.base
);
120 trace_spice_vmc_register_interface(scd
);
123 static void vmc_unregister_interface(SpiceCharDriver
*scd
)
128 dprintf(scd
, 1, "%s\n", __func__
);
129 spice_server_remove_interface(&scd
->sin
.base
);
131 trace_spice_vmc_unregister_interface(scd
);
135 static int spice_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
137 SpiceCharDriver
*s
= chr
->opaque
;
139 dprintf(s
, 2, "%s: %d\n", __func__
, len
);
140 vmc_register_interface(s
);
141 assert(s
->datalen
== 0);
142 if (s
->bufsize
< len
) {
144 s
->buffer
= g_realloc(s
->buffer
, s
->bufsize
);
146 memcpy(s
->buffer
, buf
, len
);
147 s
->datapos
= s
->buffer
;
149 spice_server_char_device_wakeup(&s
->sin
);
153 static void spice_chr_close(struct CharDriverState
*chr
)
155 SpiceCharDriver
*s
= chr
->opaque
;
157 printf("%s\n", __func__
);
158 vmc_unregister_interface(s
);
162 static void spice_chr_guest_open(struct CharDriverState
*chr
)
164 SpiceCharDriver
*s
= chr
->opaque
;
165 vmc_register_interface(s
);
168 static void spice_chr_guest_close(struct CharDriverState
*chr
)
170 SpiceCharDriver
*s
= chr
->opaque
;
171 vmc_unregister_interface(s
);
174 static void print_allowed_subtypes(void)
176 const char** psubtype
;
179 fprintf(stderr
, "allowed names: ");
180 for(i
=0, psubtype
= spice_server_char_device_recognized_subtypes();
181 *psubtype
!= NULL
; ++psubtype
, ++i
) {
183 fprintf(stderr
, "%s", *psubtype
);
185 fprintf(stderr
, ", %s", *psubtype
);
188 fprintf(stderr
, "\n");
191 CharDriverState
*qemu_chr_open_spice(QemuOpts
*opts
)
193 CharDriverState
*chr
;
195 const char* name
= qemu_opt_get(opts
, "name");
196 uint32_t debug
= qemu_opt_get_number(opts
, "debug", 0);
197 const char** psubtype
= spice_server_char_device_recognized_subtypes();
198 const char *subtype
= NULL
;
201 fprintf(stderr
, "spice-qemu-char: missing name parameter\n");
202 print_allowed_subtypes();
205 for(;*psubtype
!= NULL
; ++psubtype
) {
206 if (strcmp(name
, *psubtype
) == 0) {
211 if (subtype
== NULL
) {
212 fprintf(stderr
, "spice-qemu-char: unsupported name\n");
213 print_allowed_subtypes();
217 chr
= g_malloc0(sizeof(CharDriverState
));
218 s
= g_malloc0(sizeof(SpiceCharDriver
));
222 s
->sin
.subtype
= subtype
;
224 chr
->chr_write
= spice_chr_write
;
225 chr
->chr_close
= spice_chr_close
;
226 chr
->chr_guest_open
= spice_chr_guest_open
;
227 chr
->chr_guest_close
= spice_chr_guest_close
;
229 #if SPICE_SERVER_VERSION < 0x000901
230 /* See comment in vmc_state() */
231 if (strcmp(subtype
, "vdagent") == 0) {
232 qemu_chr_generic_open(chr
);