root / spice-qemu-char.c @ 16665b94
History | View | Annotate | Download (8.4 kB)
1 |
#include "config-host.h" |
---|---|
2 |
#include "trace.h" |
3 |
#include "ui/qemu-spice.h" |
4 |
#include "char/char.h" |
5 |
#include <spice.h> |
6 |
#include <spice-experimental.h> |
7 |
#include <spice/protocol.h> |
8 |
|
9 |
#include "qemu/osdep.h" |
10 |
|
11 |
typedef struct SpiceCharDriver { |
12 |
CharDriverState* chr; |
13 |
SpiceCharDeviceInstance sin; |
14 |
char *subtype;
|
15 |
bool active;
|
16 |
uint8_t *buffer; |
17 |
uint8_t *datapos; |
18 |
ssize_t bufsize, datalen; |
19 |
QLIST_ENTRY(SpiceCharDriver) next; |
20 |
} SpiceCharDriver; |
21 |
|
22 |
static QLIST_HEAD(, SpiceCharDriver) spice_chars =
|
23 |
QLIST_HEAD_INITIALIZER(spice_chars); |
24 |
|
25 |
static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len) |
26 |
{ |
27 |
SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin); |
28 |
ssize_t out = 0;
|
29 |
ssize_t last_out; |
30 |
uint8_t* p = (uint8_t*)buf; |
31 |
|
32 |
while (len > 0) { |
33 |
last_out = MIN(len, qemu_chr_be_can_write(scd->chr)); |
34 |
if (last_out <= 0) { |
35 |
break;
|
36 |
} |
37 |
qemu_chr_be_write(scd->chr, p, last_out); |
38 |
out += last_out; |
39 |
len -= last_out; |
40 |
p += last_out; |
41 |
} |
42 |
|
43 |
trace_spice_vmc_write(out, len + out); |
44 |
return out;
|
45 |
} |
46 |
|
47 |
static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len) |
48 |
{ |
49 |
SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin); |
50 |
int bytes = MIN(len, scd->datalen);
|
51 |
|
52 |
if (bytes > 0) { |
53 |
memcpy(buf, scd->datapos, bytes); |
54 |
scd->datapos += bytes; |
55 |
scd->datalen -= bytes; |
56 |
assert(scd->datalen >= 0);
|
57 |
if (scd->datalen == 0) { |
58 |
scd->datapos = 0;
|
59 |
} |
60 |
} |
61 |
trace_spice_vmc_read(bytes, len); |
62 |
return bytes;
|
63 |
} |
64 |
|
65 |
#if SPICE_SERVER_VERSION >= 0x000c02 |
66 |
static void vmc_event(SpiceCharDeviceInstance *sin, uint8_t event) |
67 |
{ |
68 |
SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin); |
69 |
int chr_event;
|
70 |
|
71 |
switch (event) {
|
72 |
case SPICE_PORT_EVENT_BREAK:
|
73 |
chr_event = CHR_EVENT_BREAK; |
74 |
break;
|
75 |
default:
|
76 |
return;
|
77 |
} |
78 |
|
79 |
trace_spice_vmc_event(chr_event); |
80 |
qemu_chr_be_event(scd->chr, chr_event); |
81 |
} |
82 |
#endif
|
83 |
|
84 |
static void vmc_state(SpiceCharDeviceInstance *sin, int connected) |
85 |
{ |
86 |
SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin); |
87 |
|
88 |
#if SPICE_SERVER_VERSION < 0x000901 |
89 |
/*
|
90 |
* spice-server calls the state callback for the agent channel when the
|
91 |
* spice client connects / disconnects. Given that not the client but
|
92 |
* the server is doing the parsing of the messages this is wrong as the
|
93 |
* server is still listening. Worse, this causes the parser in the server
|
94 |
* to go out of sync, so we ignore state calls for subtype vdagent
|
95 |
* spicevmc chardevs. For the full story see:
|
96 |
* http://lists.freedesktop.org/archives/spice-devel/2011-July/004837.html
|
97 |
*/
|
98 |
if (strcmp(sin->subtype, "vdagent") == 0) { |
99 |
return;
|
100 |
} |
101 |
#endif
|
102 |
|
103 |
if ((scd->chr->be_open && connected) ||
|
104 |
(!scd->chr->be_open && !connected)) { |
105 |
return;
|
106 |
} |
107 |
|
108 |
qemu_chr_be_event(scd->chr, |
109 |
connected ? CHR_EVENT_OPENED : CHR_EVENT_CLOSED); |
110 |
} |
111 |
|
112 |
static SpiceCharDeviceInterface vmc_interface = {
|
113 |
.base.type = SPICE_INTERFACE_CHAR_DEVICE, |
114 |
.base.description = "spice virtual channel char device",
|
115 |
.base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR, |
116 |
.base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR, |
117 |
.state = vmc_state, |
118 |
.write = vmc_write, |
119 |
.read = vmc_read, |
120 |
#if SPICE_SERVER_VERSION >= 0x000c02 |
121 |
.event = vmc_event, |
122 |
#endif
|
123 |
}; |
124 |
|
125 |
|
126 |
static void vmc_register_interface(SpiceCharDriver *scd) |
127 |
{ |
128 |
if (scd->active) {
|
129 |
return;
|
130 |
} |
131 |
scd->sin.base.sif = &vmc_interface.base; |
132 |
qemu_spice_add_interface(&scd->sin.base); |
133 |
scd->active = true;
|
134 |
trace_spice_vmc_register_interface(scd); |
135 |
} |
136 |
|
137 |
static void vmc_unregister_interface(SpiceCharDriver *scd) |
138 |
{ |
139 |
if (!scd->active) {
|
140 |
return;
|
141 |
} |
142 |
spice_server_remove_interface(&scd->sin.base); |
143 |
scd->active = false;
|
144 |
trace_spice_vmc_unregister_interface(scd); |
145 |
} |
146 |
|
147 |
|
148 |
static int spice_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
149 |
{ |
150 |
SpiceCharDriver *s = chr->opaque; |
151 |
|
152 |
vmc_register_interface(s); |
153 |
assert(s->datalen == 0);
|
154 |
if (s->bufsize < len) {
|
155 |
s->bufsize = len; |
156 |
s->buffer = g_realloc(s->buffer, s->bufsize); |
157 |
} |
158 |
memcpy(s->buffer, buf, len); |
159 |
s->datapos = s->buffer; |
160 |
s->datalen = len; |
161 |
spice_server_char_device_wakeup(&s->sin); |
162 |
return len;
|
163 |
} |
164 |
|
165 |
static void spice_chr_close(struct CharDriverState *chr) |
166 |
{ |
167 |
SpiceCharDriver *s = chr->opaque; |
168 |
|
169 |
vmc_unregister_interface(s); |
170 |
QLIST_REMOVE(s, next); |
171 |
|
172 |
g_free((char *)s->sin.subtype);
|
173 |
#if SPICE_SERVER_VERSION >= 0x000c02 |
174 |
g_free((char *)s->sin.portname);
|
175 |
#endif
|
176 |
g_free(s); |
177 |
} |
178 |
|
179 |
static void spice_chr_guest_open(struct CharDriverState *chr) |
180 |
{ |
181 |
SpiceCharDriver *s = chr->opaque; |
182 |
vmc_register_interface(s); |
183 |
} |
184 |
|
185 |
static void spice_chr_guest_close(struct CharDriverState *chr) |
186 |
{ |
187 |
SpiceCharDriver *s = chr->opaque; |
188 |
vmc_unregister_interface(s); |
189 |
} |
190 |
|
191 |
static void print_allowed_subtypes(void) |
192 |
{ |
193 |
const char** psubtype; |
194 |
int i;
|
195 |
|
196 |
fprintf(stderr, "allowed names: ");
|
197 |
for(i=0, psubtype = spice_server_char_device_recognized_subtypes(); |
198 |
*psubtype != NULL; ++psubtype, ++i) {
|
199 |
if (i == 0) { |
200 |
fprintf(stderr, "%s", *psubtype);
|
201 |
} else {
|
202 |
fprintf(stderr, ", %s", *psubtype);
|
203 |
} |
204 |
} |
205 |
fprintf(stderr, "\n");
|
206 |
} |
207 |
|
208 |
static CharDriverState *chr_open(const char *subtype) |
209 |
{ |
210 |
CharDriverState *chr; |
211 |
SpiceCharDriver *s; |
212 |
|
213 |
chr = g_malloc0(sizeof(CharDriverState));
|
214 |
s = g_malloc0(sizeof(SpiceCharDriver));
|
215 |
s->chr = chr; |
216 |
s->active = false;
|
217 |
s->sin.subtype = g_strdup(subtype); |
218 |
chr->opaque = s; |
219 |
chr->chr_write = spice_chr_write; |
220 |
chr->chr_close = spice_chr_close; |
221 |
chr->chr_guest_open = spice_chr_guest_open; |
222 |
chr->chr_guest_close = spice_chr_guest_close; |
223 |
|
224 |
QLIST_INSERT_HEAD(&spice_chars, s, next); |
225 |
|
226 |
return chr;
|
227 |
} |
228 |
|
229 |
CharDriverState *qemu_chr_open_spice_vmc(const char *type) |
230 |
{ |
231 |
CharDriverState *chr; |
232 |
const char **psubtype = spice_server_char_device_recognized_subtypes(); |
233 |
|
234 |
if (type == NULL) { |
235 |
fprintf(stderr, "spice-qemu-char: missing name parameter\n");
|
236 |
print_allowed_subtypes(); |
237 |
return NULL; |
238 |
} |
239 |
for (; *psubtype != NULL; ++psubtype) { |
240 |
if (strcmp(type, *psubtype) == 0) { |
241 |
break;
|
242 |
} |
243 |
} |
244 |
if (*psubtype == NULL) { |
245 |
fprintf(stderr, "spice-qemu-char: unsupported type: %s\n", type);
|
246 |
print_allowed_subtypes(); |
247 |
return NULL; |
248 |
} |
249 |
|
250 |
chr = chr_open(type); |
251 |
|
252 |
#if SPICE_SERVER_VERSION < 0x000901 |
253 |
/* See comment in vmc_state() */
|
254 |
if (strcmp(type, "vdagent") == 0) { |
255 |
qemu_chr_generic_open(chr); |
256 |
} |
257 |
#endif
|
258 |
|
259 |
return chr;
|
260 |
} |
261 |
|
262 |
#if SPICE_SERVER_VERSION >= 0x000c02 |
263 |
CharDriverState *qemu_chr_open_spice_port(const char *name) |
264 |
{ |
265 |
CharDriverState *chr; |
266 |
SpiceCharDriver *s; |
267 |
|
268 |
if (name == NULL) { |
269 |
fprintf(stderr, "spice-qemu-char: missing name parameter\n");
|
270 |
return NULL; |
271 |
} |
272 |
|
273 |
chr = chr_open("port");
|
274 |
s = chr->opaque; |
275 |
s->sin.portname = g_strdup(name); |
276 |
|
277 |
return chr;
|
278 |
} |
279 |
|
280 |
void qemu_spice_register_ports(void) |
281 |
{ |
282 |
SpiceCharDriver *s; |
283 |
|
284 |
QLIST_FOREACH(s, &spice_chars, next) { |
285 |
if (s->sin.portname == NULL) { |
286 |
continue;
|
287 |
} |
288 |
vmc_register_interface(s); |
289 |
} |
290 |
} |
291 |
#endif
|
292 |
|
293 |
static void qemu_chr_parse_spice_vmc(QemuOpts *opts, ChardevBackend *backend, |
294 |
Error **errp) |
295 |
{ |
296 |
const char *name = qemu_opt_get(opts, "name"); |
297 |
|
298 |
if (name == NULL) { |
299 |
error_setg(errp, "chardev: spice channel: no name given");
|
300 |
return;
|
301 |
} |
302 |
backend->spicevmc = g_new0(ChardevSpiceChannel, 1);
|
303 |
backend->spicevmc->type = g_strdup(name); |
304 |
} |
305 |
|
306 |
static void qemu_chr_parse_spice_port(QemuOpts *opts, ChardevBackend *backend, |
307 |
Error **errp) |
308 |
{ |
309 |
const char *name = qemu_opt_get(opts, "name"); |
310 |
|
311 |
if (name == NULL) { |
312 |
error_setg(errp, "chardev: spice port: no name given");
|
313 |
return;
|
314 |
} |
315 |
backend->spiceport = g_new0(ChardevSpicePort, 1);
|
316 |
backend->spiceport->fqdn = g_strdup(name); |
317 |
} |
318 |
|
319 |
static void register_types(void) |
320 |
{ |
321 |
register_char_driver_qapi("spicevmc", CHARDEV_BACKEND_KIND_SPICEVMC,
|
322 |
qemu_chr_parse_spice_vmc); |
323 |
register_char_driver_qapi("spiceport", CHARDEV_BACKEND_KIND_SPICEPORT,
|
324 |
qemu_chr_parse_spice_port); |
325 |
} |
326 |
|
327 |
type_init(register_types); |