]> git.proxmox.com Git - qemu.git/blame - spice-qemu-char.c
qemu-char: Consolidate guest_close/guest_open into a set_fe_open callback
[qemu.git] / spice-qemu-char.c
CommitLineData
cbcc6336
AL
1#include "config-host.h"
2#include "trace.h"
3#include "ui/qemu-spice.h"
927d4878 4#include "char/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;
14 char *subtype;
15 bool active;
16 uint8_t *buffer;
17 uint8_t *datapos;
18 ssize_t bufsize, datalen;
7a5448ce 19 QLIST_ENTRY(SpiceCharDriver) next;
cbcc6336
AL
20} SpiceCharDriver;
21
7a5448ce
MAL
22static QLIST_HEAD(, SpiceCharDriver) spice_chars =
23 QLIST_HEAD_INITIALIZER(spice_chars);
24
cbcc6336
AL
25static 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) {
07a54d70
MAL
33 last_out = MIN(len, qemu_chr_be_can_write(scd->chr));
34 if (last_out <= 0) {
cbcc6336
AL
35 break;
36 }
fa5efccb 37 qemu_chr_be_write(scd->chr, p, last_out);
35106c2d
HG
38 out += last_out;
39 len -= last_out;
40 p += last_out;
cbcc6336
AL
41 }
42
cbcc6336
AL
43 trace_spice_vmc_write(out, len + out);
44 return out;
45}
46
47static 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
cbcc6336
AL
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
5a49d3e9
MAL
65#if SPICE_SERVER_VERSION >= 0x000c02
66static 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:
5a49d3e9
MAL
76 return;
77 }
78
5a49d3e9
MAL
79 trace_spice_vmc_event(chr_event);
80 qemu_chr_be_event(scd->chr, chr_event);
81}
82#endif
83
f76e4c7f
HG
84static 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
16665b94
HG
103 if ((scd->chr->be_open && connected) ||
104 (!scd->chr->be_open && !connected)) {
f76e4c7f
HG
105 return;
106 }
107
108 qemu_chr_be_event(scd->chr,
109 connected ? CHR_EVENT_OPENED : CHR_EVENT_CLOSED);
110}
111
cbcc6336
AL
112static 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,
f76e4c7f 117 .state = vmc_state,
cbcc6336
AL
118 .write = vmc_write,
119 .read = vmc_read,
5a49d3e9
MAL
120#if SPICE_SERVER_VERSION >= 0x000c02
121 .event = vmc_event,
122#endif
cbcc6336
AL
123};
124
125
126static void vmc_register_interface(SpiceCharDriver *scd)
127{
128 if (scd->active) {
129 return;
130 }
cbcc6336
AL
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
137static void vmc_unregister_interface(SpiceCharDriver *scd)
138{
139 if (!scd->active) {
140 return;
141 }
cbcc6336
AL
142 spice_server_remove_interface(&scd->sin.base);
143 scd->active = false;
144 trace_spice_vmc_unregister_interface(scd);
145}
146
147
148static int spice_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
149{
150 SpiceCharDriver *s = chr->opaque;
151
cbcc6336
AL
152 vmc_register_interface(s);
153 assert(s->datalen == 0);
154 if (s->bufsize < len) {
155 s->bufsize = len;
7267c094 156 s->buffer = g_realloc(s->buffer, s->bufsize);
cbcc6336
AL
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
165static void spice_chr_close(struct CharDriverState *chr)
166{
167 SpiceCharDriver *s = chr->opaque;
168
cbcc6336 169 vmc_unregister_interface(s);
7a5448ce 170 QLIST_REMOVE(s, next);
5e9b473a
HG
171
172 g_free((char *)s->sin.subtype);
173#if SPICE_SERVER_VERSION >= 0x000c02
174 g_free((char *)s->sin.portname);
175#endif
7267c094 176 g_free(s);
cbcc6336
AL
177}
178
574b711a 179static void spice_chr_set_fe_open(struct CharDriverState *chr, int fe_open)
cd8f7df2
HG
180{
181 SpiceCharDriver *s = chr->opaque;
574b711a
HG
182 if (fe_open) {
183 vmc_register_interface(s);
184 } else {
185 vmc_unregister_interface(s);
186 }
cd8f7df2
HG
187}
188
cbcc6336
AL
189static void print_allowed_subtypes(void)
190{
191 const char** psubtype;
192 int i;
193
194 fprintf(stderr, "allowed names: ");
195 for(i=0, psubtype = spice_server_char_device_recognized_subtypes();
196 *psubtype != NULL; ++psubtype, ++i) {
197 if (i == 0) {
198 fprintf(stderr, "%s", *psubtype);
199 } else {
200 fprintf(stderr, ", %s", *psubtype);
201 }
202 }
203 fprintf(stderr, "\n");
204}
205
cd153e2a 206static CharDriverState *chr_open(const char *subtype)
cbcc6336
AL
207{
208 CharDriverState *chr;
209 SpiceCharDriver *s;
71b423f4
MAL
210
211 chr = g_malloc0(sizeof(CharDriverState));
212 s = g_malloc0(sizeof(SpiceCharDriver));
213 s->chr = chr;
71b423f4 214 s->active = false;
5e9b473a 215 s->sin.subtype = g_strdup(subtype);
71b423f4
MAL
216 chr->opaque = s;
217 chr->chr_write = spice_chr_write;
218 chr->chr_close = spice_chr_close;
574b711a 219 chr->chr_set_fe_open = spice_chr_set_fe_open;
71b423f4 220
7a5448ce
MAL
221 QLIST_INSERT_HEAD(&spice_chars, s, next);
222
71b423f4
MAL
223 return chr;
224}
225
cd153e2a 226CharDriverState *qemu_chr_open_spice_vmc(const char *type)
71b423f4
MAL
227{
228 CharDriverState *chr;
71b423f4 229 const char **psubtype = spice_server_char_device_recognized_subtypes();
cbcc6336 230
cd153e2a 231 if (type == NULL) {
cbcc6336
AL
232 fprintf(stderr, "spice-qemu-char: missing name parameter\n");
233 print_allowed_subtypes();
1f51470d 234 return NULL;
cbcc6336 235 }
cd153e2a
GH
236 for (; *psubtype != NULL; ++psubtype) {
237 if (strcmp(type, *psubtype) == 0) {
cbcc6336
AL
238 break;
239 }
240 }
cd153e2a
GH
241 if (*psubtype == NULL) {
242 fprintf(stderr, "spice-qemu-char: unsupported type: %s\n", type);
cbcc6336 243 print_allowed_subtypes();
1f51470d 244 return NULL;
cbcc6336
AL
245 }
246
cd153e2a 247 chr = chr_open(type);
cbcc6336 248
f76e4c7f
HG
249#if SPICE_SERVER_VERSION < 0x000901
250 /* See comment in vmc_state() */
cd153e2a 251 if (strcmp(type, "vdagent") == 0) {
f76e4c7f
HG
252 qemu_chr_generic_open(chr);
253 }
254#endif
cbcc6336 255
1f51470d 256 return chr;
cbcc6336 257}
5a49d3e9
MAL
258
259#if SPICE_SERVER_VERSION >= 0x000c02
cd153e2a 260CharDriverState *qemu_chr_open_spice_port(const char *name)
5a49d3e9
MAL
261{
262 CharDriverState *chr;
263 SpiceCharDriver *s;
5a49d3e9
MAL
264
265 if (name == NULL) {
266 fprintf(stderr, "spice-qemu-char: missing name parameter\n");
267 return NULL;
268 }
269
cd153e2a 270 chr = chr_open("port");
5a49d3e9 271 s = chr->opaque;
5e9b473a 272 s->sin.portname = g_strdup(name);
5a49d3e9
MAL
273
274 return chr;
275}
afd0b409
MAL
276
277void qemu_spice_register_ports(void)
278{
279 SpiceCharDriver *s;
280
281 QLIST_FOREACH(s, &spice_chars, next) {
282 if (s->sin.portname == NULL) {
283 continue;
284 }
285 vmc_register_interface(s);
286 }
287}
5a49d3e9 288#endif
26c60614 289
cd153e2a
GH
290static void qemu_chr_parse_spice_vmc(QemuOpts *opts, ChardevBackend *backend,
291 Error **errp)
292{
293 const char *name = qemu_opt_get(opts, "name");
294
295 if (name == NULL) {
296 error_setg(errp, "chardev: spice channel: no name given");
297 return;
298 }
299 backend->spicevmc = g_new0(ChardevSpiceChannel, 1);
300 backend->spicevmc->type = g_strdup(name);
301}
302
303static void qemu_chr_parse_spice_port(QemuOpts *opts, ChardevBackend *backend,
304 Error **errp)
305{
306 const char *name = qemu_opt_get(opts, "name");
307
308 if (name == NULL) {
309 error_setg(errp, "chardev: spice port: no name given");
310 return;
311 }
312 backend->spiceport = g_new0(ChardevSpicePort, 1);
313 backend->spiceport->fqdn = g_strdup(name);
314}
315
26c60614
AL
316static void register_types(void)
317{
cd153e2a
GH
318 register_char_driver_qapi("spicevmc", CHARDEV_BACKEND_KIND_SPICEVMC,
319 qemu_chr_parse_spice_vmc);
320 register_char_driver_qapi("spiceport", CHARDEV_BACKEND_KIND_SPICEPORT,
321 qemu_chr_parse_spice_port);
26c60614
AL
322}
323
324type_init(register_types);