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 typedef struct SpiceCharDriver
{
19 SpiceCharDeviceInstance sin
;
24 ssize_t bufsize
, datalen
;
28 static int vmc_write(SpiceCharDeviceInstance
*sin
, const uint8_t *buf
, int len
)
30 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
33 uint8_t* p
= (uint8_t*)buf
;
36 last_out
= MIN(len
, qemu_chr_be_can_write(scd
->chr
));
40 qemu_chr_be_write(scd
->chr
, p
, last_out
);
46 dprintf(scd
, 3, "%s: %zu/%zd\n", __func__
, out
, len
+ out
);
47 trace_spice_vmc_write(out
, len
+ out
);
51 static int vmc_read(SpiceCharDeviceInstance
*sin
, uint8_t *buf
, int len
)
53 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
54 int bytes
= MIN(len
, scd
->datalen
);
56 dprintf(scd
, 2, "%s: %p %d/%d/%zd\n", __func__
, scd
->datapos
, len
, bytes
, scd
->datalen
);
58 memcpy(buf
, scd
->datapos
, bytes
);
59 scd
->datapos
+= bytes
;
60 scd
->datalen
-= bytes
;
61 assert(scd
->datalen
>= 0);
62 if (scd
->datalen
== 0) {
66 trace_spice_vmc_read(bytes
, len
);
70 static void vmc_state(SpiceCharDeviceInstance
*sin
, int connected
)
72 SpiceCharDriver
*scd
= container_of(sin
, SpiceCharDriver
, sin
);
74 #if SPICE_SERVER_VERSION < 0x000901
76 * spice-server calls the state callback for the agent channel when the
77 * spice client connects / disconnects. Given that not the client but
78 * the server is doing the parsing of the messages this is wrong as the
79 * server is still listening. Worse, this causes the parser in the server
80 * to go out of sync, so we ignore state calls for subtype vdagent
81 * spicevmc chardevs. For the full story see:
82 * http://lists.freedesktop.org/archives/spice-devel/2011-July/004837.html
84 if (strcmp(sin
->subtype
, "vdagent") == 0) {
89 if ((scd
->chr
->opened
&& connected
) ||
90 (!scd
->chr
->opened
&& !connected
)) {
94 qemu_chr_be_event(scd
->chr
,
95 connected
? CHR_EVENT_OPENED
: CHR_EVENT_CLOSED
);
98 static SpiceCharDeviceInterface vmc_interface
= {
99 .base
.type
= SPICE_INTERFACE_CHAR_DEVICE
,
100 .base
.description
= "spice virtual channel char device",
101 .base
.major_version
= SPICE_INTERFACE_CHAR_DEVICE_MAJOR
,
102 .base
.minor_version
= SPICE_INTERFACE_CHAR_DEVICE_MINOR
,
109 static void vmc_register_interface(SpiceCharDriver
*scd
)
114 dprintf(scd
, 1, "%s\n", __func__
);
115 scd
->sin
.base
.sif
= &vmc_interface
.base
;
116 qemu_spice_add_interface(&scd
->sin
.base
);
118 trace_spice_vmc_register_interface(scd
);
121 static void vmc_unregister_interface(SpiceCharDriver
*scd
)
126 dprintf(scd
, 1, "%s\n", __func__
);
127 spice_server_remove_interface(&scd
->sin
.base
);
129 trace_spice_vmc_unregister_interface(scd
);
133 static int spice_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
135 SpiceCharDriver
*s
= chr
->opaque
;
137 dprintf(s
, 2, "%s: %d\n", __func__
, len
);
138 vmc_register_interface(s
);
139 assert(s
->datalen
== 0);
140 if (s
->bufsize
< len
) {
142 s
->buffer
= g_realloc(s
->buffer
, s
->bufsize
);
144 memcpy(s
->buffer
, buf
, len
);
145 s
->datapos
= s
->buffer
;
147 spice_server_char_device_wakeup(&s
->sin
);
151 static void spice_chr_close(struct CharDriverState
*chr
)
153 SpiceCharDriver
*s
= chr
->opaque
;
155 printf("%s\n", __func__
);
156 vmc_unregister_interface(s
);
160 static void spice_chr_guest_open(struct CharDriverState
*chr
)
162 SpiceCharDriver
*s
= chr
->opaque
;
163 vmc_register_interface(s
);
166 static void spice_chr_guest_close(struct CharDriverState
*chr
)
168 SpiceCharDriver
*s
= chr
->opaque
;
169 vmc_unregister_interface(s
);
172 static void print_allowed_subtypes(void)
174 const char** psubtype
;
177 fprintf(stderr
, "allowed names: ");
178 for(i
=0, psubtype
= spice_server_char_device_recognized_subtypes();
179 *psubtype
!= NULL
; ++psubtype
, ++i
) {
181 fprintf(stderr
, "%s", *psubtype
);
183 fprintf(stderr
, ", %s", *psubtype
);
186 fprintf(stderr
, "\n");
189 CharDriverState
*qemu_chr_open_spice(QemuOpts
*opts
)
191 CharDriverState
*chr
;
193 const char* name
= qemu_opt_get(opts
, "name");
194 uint32_t debug
= qemu_opt_get_number(opts
, "debug", 0);
195 const char** psubtype
= spice_server_char_device_recognized_subtypes();
196 const char *subtype
= NULL
;
199 fprintf(stderr
, "spice-qemu-char: missing name parameter\n");
200 print_allowed_subtypes();
203 for(;*psubtype
!= NULL
; ++psubtype
) {
204 if (strcmp(name
, *psubtype
) == 0) {
209 if (subtype
== NULL
) {
210 fprintf(stderr
, "spice-qemu-char: unsupported name: %s\n", name
);
211 print_allowed_subtypes();
215 chr
= g_malloc0(sizeof(CharDriverState
));
216 s
= g_malloc0(sizeof(SpiceCharDriver
));
220 s
->sin
.subtype
= subtype
;
222 chr
->chr_write
= spice_chr_write
;
223 chr
->chr_close
= spice_chr_close
;
224 chr
->chr_guest_open
= spice_chr_guest_open
;
225 chr
->chr_guest_close
= spice_chr_guest_close
;
227 #if SPICE_SERVER_VERSION < 0x000901
228 /* See comment in vmc_state() */
229 if (strcmp(subtype
, "vdagent") == 0) {
230 qemu_chr_generic_open(chr
);