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