]> git.proxmox.com Git - mirror_qemu.git/blame - spice-qemu-char.c
block: minimal bounce buffer alignment
[mirror_qemu.git] / spice-qemu-char.c
CommitLineData
cbcc6336
AL
1#include "config-host.h"
2#include "trace.h"
3#include "ui/qemu-spice.h"
dccfcd0e 4#include "sysemu/char.h"
cbcc6336 5#include <spice.h>
5a49d3e9 6#include <spice/protocol.h>
cbcc6336 7
1de7afc9 8#include "qemu/osdep.h"
cbcc6336 9
cbcc6336
AL
10typedef struct SpiceCharDriver {
11 CharDriverState* chr;
12 SpiceCharDeviceInstance sin;
cbcc6336 13 bool active;
ae893e5e 14 bool blocked;
b010cec8
AL
15 const uint8_t *datapos;
16 int datalen;
7a5448ce 17 QLIST_ENTRY(SpiceCharDriver) next;
cbcc6336
AL
18} SpiceCharDriver;
19
ae893e5e
HG
20typedef struct SpiceCharSource {
21 GSource source;
22 SpiceCharDriver *scd;
23} SpiceCharSource;
24
7a5448ce
MAL
25static QLIST_HEAD(, SpiceCharDriver) spice_chars =
26 QLIST_HEAD_INITIALIZER(spice_chars);
27
cbcc6336
AL
28static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
29{
30 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
31 ssize_t out = 0;
32 ssize_t last_out;
33 uint8_t* p = (uint8_t*)buf;
34
35 while (len > 0) {
75c439bc
HG
36 int can_write = qemu_chr_be_can_write(scd->chr);
37 last_out = MIN(len, can_write);
07a54d70 38 if (last_out <= 0) {
cbcc6336
AL
39 break;
40 }
fa5efccb 41 qemu_chr_be_write(scd->chr, p, last_out);
35106c2d
HG
42 out += last_out;
43 len -= last_out;
44 p += last_out;
cbcc6336
AL
45 }
46
cbcc6336
AL
47 trace_spice_vmc_write(out, len + out);
48 return out;
49}
50
51static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)
52{
53 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
54 int bytes = MIN(len, scd->datalen);
55
cbcc6336
AL
56 if (bytes > 0) {
57 memcpy(buf, scd->datapos, bytes);
58 scd->datapos += bytes;
59 scd->datalen -= bytes;
60 assert(scd->datalen >= 0);
ae893e5e
HG
61 }
62 if (scd->datalen == 0) {
63 scd->datapos = 0;
64 scd->blocked = false;
cbcc6336
AL
65 }
66 trace_spice_vmc_read(bytes, len);
67 return bytes;
68}
69
5a49d3e9
MAL
70#if SPICE_SERVER_VERSION >= 0x000c02
71static void vmc_event(SpiceCharDeviceInstance *sin, uint8_t event)
72{
73 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
74 int chr_event;
75
76 switch (event) {
77 case SPICE_PORT_EVENT_BREAK:
78 chr_event = CHR_EVENT_BREAK;
79 break;
80 default:
5a49d3e9
MAL
81 return;
82 }
83
5a49d3e9
MAL
84 trace_spice_vmc_event(chr_event);
85 qemu_chr_be_event(scd->chr, chr_event);
86}
87#endif
88
f76e4c7f
HG
89static void vmc_state(SpiceCharDeviceInstance *sin, int connected)
90{
91 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
92
16665b94
HG
93 if ((scd->chr->be_open && connected) ||
94 (!scd->chr->be_open && !connected)) {
f76e4c7f
HG
95 return;
96 }
97
98 qemu_chr_be_event(scd->chr,
99 connected ? CHR_EVENT_OPENED : CHR_EVENT_CLOSED);
100}
101
cbcc6336
AL
102static SpiceCharDeviceInterface vmc_interface = {
103 .base.type = SPICE_INTERFACE_CHAR_DEVICE,
104 .base.description = "spice virtual channel char device",
105 .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,
106 .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,
f76e4c7f 107 .state = vmc_state,
cbcc6336
AL
108 .write = vmc_write,
109 .read = vmc_read,
5a49d3e9
MAL
110#if SPICE_SERVER_VERSION >= 0x000c02
111 .event = vmc_event,
112#endif
cbcc6336
AL
113};
114
115
116static void vmc_register_interface(SpiceCharDriver *scd)
117{
118 if (scd->active) {
119 return;
120 }
cbcc6336
AL
121 scd->sin.base.sif = &vmc_interface.base;
122 qemu_spice_add_interface(&scd->sin.base);
123 scd->active = true;
124 trace_spice_vmc_register_interface(scd);
125}
126
127static void vmc_unregister_interface(SpiceCharDriver *scd)
128{
129 if (!scd->active) {
130 return;
131 }
cbcc6336
AL
132 spice_server_remove_interface(&scd->sin.base);
133 scd->active = false;
134 trace_spice_vmc_unregister_interface(scd);
135}
136
ae893e5e
HG
137static gboolean spice_char_source_prepare(GSource *source, gint *timeout)
138{
139 SpiceCharSource *src = (SpiceCharSource *)source;
140
141 *timeout = -1;
142
143 return !src->scd->blocked;
144}
145
146static gboolean spice_char_source_check(GSource *source)
147{
148 SpiceCharSource *src = (SpiceCharSource *)source;
149
150 return !src->scd->blocked;
151}
152
153static gboolean spice_char_source_dispatch(GSource *source,
154 GSourceFunc callback, gpointer user_data)
155{
156 GIOFunc func = (GIOFunc)callback;
157
158 return func(NULL, G_IO_OUT, user_data);
159}
160
51575c3f 161static GSourceFuncs SpiceCharSourceFuncs = {
ae893e5e
HG
162 .prepare = spice_char_source_prepare,
163 .check = spice_char_source_check,
164 .dispatch = spice_char_source_dispatch,
165};
166
167static GSource *spice_chr_add_watch(CharDriverState *chr, GIOCondition cond)
168{
169 SpiceCharDriver *scd = chr->opaque;
170 SpiceCharSource *src;
171
172 assert(cond == G_IO_OUT);
173
174 src = (SpiceCharSource *)g_source_new(&SpiceCharSourceFuncs,
175 sizeof(SpiceCharSource));
176 src->scd = scd;
177
178 return (GSource *)src;
179}
cbcc6336
AL
180
181static int spice_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
182{
183 SpiceCharDriver *s = chr->opaque;
ae893e5e 184 int read_bytes;
cbcc6336 185
cbcc6336 186 assert(s->datalen == 0);
b010cec8 187 s->datapos = buf;
cbcc6336
AL
188 s->datalen = len;
189 spice_server_char_device_wakeup(&s->sin);
ae893e5e
HG
190 read_bytes = len - s->datalen;
191 if (read_bytes != len) {
192 /* We'll get passed in the unconsumed data with the next call */
193 s->datalen = 0;
194 s->datapos = NULL;
195 s->blocked = true;
196 }
197 return read_bytes;
cbcc6336
AL
198}
199
200static void spice_chr_close(struct CharDriverState *chr)
201{
202 SpiceCharDriver *s = chr->opaque;
203
cbcc6336 204 vmc_unregister_interface(s);
7a5448ce 205 QLIST_REMOVE(s, next);
5e9b473a
HG
206
207 g_free((char *)s->sin.subtype);
208#if SPICE_SERVER_VERSION >= 0x000c02
209 g_free((char *)s->sin.portname);
210#endif
7267c094 211 g_free(s);
cbcc6336
AL
212}
213
89091146 214static void spice_vmc_set_fe_open(struct CharDriverState *chr, int fe_open)
cd8f7df2
HG
215{
216 SpiceCharDriver *s = chr->opaque;
574b711a
HG
217 if (fe_open) {
218 vmc_register_interface(s);
219 } else {
220 vmc_unregister_interface(s);
221 }
cd8f7df2
HG
222}
223
89091146
MAL
224static void spice_port_set_fe_open(struct CharDriverState *chr, int fe_open)
225{
226#if SPICE_SERVER_VERSION >= 0x000c02
227 SpiceCharDriver *s = chr->opaque;
228
229 if (fe_open) {
230 spice_server_port_event(&s->sin, SPICE_PORT_EVENT_OPENED);
231 } else {
232 spice_server_port_event(&s->sin, SPICE_PORT_EVENT_CLOSED);
233 }
234#endif
235}
236
be733d6d
MAL
237static void spice_chr_fe_event(struct CharDriverState *chr, int event)
238{
239#if SPICE_SERVER_VERSION >= 0x000c02
240 SpiceCharDriver *s = chr->opaque;
241
242 spice_server_port_event(&s->sin, event);
243#endif
244}
245
cbcc6336
AL
246static void print_allowed_subtypes(void)
247{
248 const char** psubtype;
249 int i;
250
251 fprintf(stderr, "allowed names: ");
252 for(i=0, psubtype = spice_server_char_device_recognized_subtypes();
253 *psubtype != NULL; ++psubtype, ++i) {
254 if (i == 0) {
255 fprintf(stderr, "%s", *psubtype);
256 } else {
257 fprintf(stderr, ", %s", *psubtype);
258 }
259 }
260 fprintf(stderr, "\n");
261}
262
89091146
MAL
263static CharDriverState *chr_open(const char *subtype,
264 void (*set_fe_open)(struct CharDriverState *, int))
265
cbcc6336
AL
266{
267 CharDriverState *chr;
268 SpiceCharDriver *s;
71b423f4 269
db39fcf1 270 chr = qemu_chr_alloc();
71b423f4
MAL
271 s = g_malloc0(sizeof(SpiceCharDriver));
272 s->chr = chr;
71b423f4 273 s->active = false;
5e9b473a 274 s->sin.subtype = g_strdup(subtype);
71b423f4
MAL
275 chr->opaque = s;
276 chr->chr_write = spice_chr_write;
ae893e5e 277 chr->chr_add_watch = spice_chr_add_watch;
71b423f4 278 chr->chr_close = spice_chr_close;
89091146 279 chr->chr_set_fe_open = set_fe_open;
bd5c51ee 280 chr->explicit_be_open = true;
be733d6d 281 chr->chr_fe_event = spice_chr_fe_event;
71b423f4 282
7a5448ce
MAL
283 QLIST_INSERT_HEAD(&spice_chars, s, next);
284
71b423f4
MAL
285 return chr;
286}
287
cd153e2a 288CharDriverState *qemu_chr_open_spice_vmc(const char *type)
71b423f4 289{
71b423f4 290 const char **psubtype = spice_server_char_device_recognized_subtypes();
cbcc6336 291
cd153e2a 292 if (type == NULL) {
cbcc6336
AL
293 fprintf(stderr, "spice-qemu-char: missing name parameter\n");
294 print_allowed_subtypes();
1f51470d 295 return NULL;
cbcc6336 296 }
cd153e2a
GH
297 for (; *psubtype != NULL; ++psubtype) {
298 if (strcmp(type, *psubtype) == 0) {
cbcc6336
AL
299 break;
300 }
301 }
cd153e2a
GH
302 if (*psubtype == NULL) {
303 fprintf(stderr, "spice-qemu-char: unsupported type: %s\n", type);
cbcc6336 304 print_allowed_subtypes();
1f51470d 305 return NULL;
cbcc6336
AL
306 }
307
89091146 308 return chr_open(type, spice_vmc_set_fe_open);
cbcc6336 309}
5a49d3e9
MAL
310
311#if SPICE_SERVER_VERSION >= 0x000c02
cd153e2a 312CharDriverState *qemu_chr_open_spice_port(const char *name)
5a49d3e9
MAL
313{
314 CharDriverState *chr;
315 SpiceCharDriver *s;
5a49d3e9
MAL
316
317 if (name == NULL) {
318 fprintf(stderr, "spice-qemu-char: missing name parameter\n");
319 return NULL;
320 }
321
89091146 322 chr = chr_open("port", spice_port_set_fe_open);
5a49d3e9 323 s = chr->opaque;
5e9b473a 324 s->sin.portname = g_strdup(name);
5a49d3e9
MAL
325
326 return chr;
327}
afd0b409
MAL
328
329void qemu_spice_register_ports(void)
330{
331 SpiceCharDriver *s;
332
333 QLIST_FOREACH(s, &spice_chars, next) {
334 if (s->sin.portname == NULL) {
335 continue;
336 }
337 vmc_register_interface(s);
338 }
339}
5a49d3e9 340#endif
26c60614 341
cd153e2a
GH
342static void qemu_chr_parse_spice_vmc(QemuOpts *opts, ChardevBackend *backend,
343 Error **errp)
344{
345 const char *name = qemu_opt_get(opts, "name");
346
347 if (name == NULL) {
348 error_setg(errp, "chardev: spice channel: no name given");
349 return;
350 }
351 backend->spicevmc = g_new0(ChardevSpiceChannel, 1);
352 backend->spicevmc->type = g_strdup(name);
353}
354
355static void qemu_chr_parse_spice_port(QemuOpts *opts, ChardevBackend *backend,
356 Error **errp)
357{
358 const char *name = qemu_opt_get(opts, "name");
359
360 if (name == NULL) {
361 error_setg(errp, "chardev: spice port: no name given");
362 return;
363 }
364 backend->spiceport = g_new0(ChardevSpicePort, 1);
365 backend->spiceport->fqdn = g_strdup(name);
366}
367
26c60614
AL
368static void register_types(void)
369{
e4d50d47
PM
370 register_char_driver("spicevmc", CHARDEV_BACKEND_KIND_SPICEVMC,
371 qemu_chr_parse_spice_vmc);
372 register_char_driver("spiceport", CHARDEV_BACKEND_KIND_SPICEPORT,
373 qemu_chr_parse_spice_port);
26c60614
AL
374}
375
376type_init(register_types);