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