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