blob: 4be75ba353853ca1546cd9fcaf1ed8c08c79a5e7 [file] [log] [blame]
Alon Levycbcc6332011-01-19 10:49:50 +02001#include "config-host.h"
2#include "trace.h"
3#include "ui/qemu-spice.h"
4#include <spice.h>
5#include <spice-experimental.h>
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +01006#include <spice/protocol.h>
Alon Levycbcc6332011-01-19 10:49:50 +02007
8#include "osdep.h"
9
10#define dprintf(_scd, _level, _fmt, ...) \
11 do { \
12 static unsigned __dprintf_counter = 0; \
13 if (_scd->debug >= _level) { \
14 fprintf(stderr, "scd: %3d: " _fmt, ++__dprintf_counter, ## __VA_ARGS__);\
15 } \
16 } while (0)
17
Alon Levycbcc6332011-01-19 10:49:50 +020018typedef struct SpiceCharDriver {
19 CharDriverState* chr;
20 SpiceCharDeviceInstance sin;
21 char *subtype;
22 bool active;
23 uint8_t *buffer;
24 uint8_t *datapos;
25 ssize_t bufsize, datalen;
26 uint32_t debug;
27} SpiceCharDriver;
28
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) {
Marc-André Lureau07a54d72012-12-05 16:15:32 +010037 last_out = MIN(len, qemu_chr_be_can_write(scd->chr));
38 if (last_out <= 0) {
Alon Levycbcc6332011-01-19 10:49:50 +020039 break;
40 }
Anthony Liguorifa5efcc2011-08-15 11:17:30 -050041 qemu_chr_be_write(scd->chr, p, last_out);
Hans de Goede35106c22011-03-22 16:28:41 +010042 out += last_out;
43 len -= last_out;
44 p += last_out;
Alon Levycbcc6332011-01-19 10:49:50 +020045 }
46
Peter Maydell7b6c7362011-08-09 23:04:35 +010047 dprintf(scd, 3, "%s: %zu/%zd\n", __func__, out, len + out);
Alon Levycbcc6332011-01-19 10:49:50 +020048 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
57 dprintf(scd, 2, "%s: %p %d/%d/%zd\n", __func__, scd->datapos, len, bytes, scd->datalen);
58 if (bytes > 0) {
59 memcpy(buf, scd->datapos, bytes);
60 scd->datapos += bytes;
61 scd->datalen -= bytes;
62 assert(scd->datalen >= 0);
63 if (scd->datalen == 0) {
64 scd->datapos = 0;
65 }
66 }
67 trace_spice_vmc_read(bytes, len);
68 return bytes;
69}
70
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +010071#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:
82 dprintf(scd, 2, "%s: unknown %d\n", __func__, event);
83 return;
84 }
85
86 dprintf(scd, 2, "%s: %d\n", __func__, event);
87 trace_spice_vmc_event(chr_event);
88 qemu_chr_be_event(scd->chr, chr_event);
89}
90#endif
91
Hans de Goedef76e4c72011-11-19 10:22:44 +010092static void vmc_state(SpiceCharDeviceInstance *sin, int connected)
93{
94 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
95
96#if SPICE_SERVER_VERSION < 0x000901
97 /*
98 * spice-server calls the state callback for the agent channel when the
99 * spice client connects / disconnects. Given that not the client but
100 * the server is doing the parsing of the messages this is wrong as the
101 * server is still listening. Worse, this causes the parser in the server
102 * to go out of sync, so we ignore state calls for subtype vdagent
103 * spicevmc chardevs. For the full story see:
104 * http://lists.freedesktop.org/archives/spice-devel/2011-July/004837.html
105 */
106 if (strcmp(sin->subtype, "vdagent") == 0) {
107 return;
108 }
109#endif
110
111 if ((scd->chr->opened && connected) ||
112 (!scd->chr->opened && !connected)) {
113 return;
114 }
115
116 qemu_chr_be_event(scd->chr,
117 connected ? CHR_EVENT_OPENED : CHR_EVENT_CLOSED);
118}
119
Alon Levycbcc6332011-01-19 10:49:50 +0200120static SpiceCharDeviceInterface vmc_interface = {
121 .base.type = SPICE_INTERFACE_CHAR_DEVICE,
122 .base.description = "spice virtual channel char device",
123 .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,
124 .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,
Hans de Goedef76e4c72011-11-19 10:22:44 +0100125 .state = vmc_state,
Alon Levycbcc6332011-01-19 10:49:50 +0200126 .write = vmc_write,
127 .read = vmc_read,
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100128#if SPICE_SERVER_VERSION >= 0x000c02
129 .event = vmc_event,
130#endif
Alon Levycbcc6332011-01-19 10:49:50 +0200131};
132
133
134static void vmc_register_interface(SpiceCharDriver *scd)
135{
136 if (scd->active) {
137 return;
138 }
139 dprintf(scd, 1, "%s\n", __func__);
140 scd->sin.base.sif = &vmc_interface.base;
141 qemu_spice_add_interface(&scd->sin.base);
142 scd->active = true;
143 trace_spice_vmc_register_interface(scd);
144}
145
146static void vmc_unregister_interface(SpiceCharDriver *scd)
147{
148 if (!scd->active) {
149 return;
150 }
151 dprintf(scd, 1, "%s\n", __func__);
152 spice_server_remove_interface(&scd->sin.base);
153 scd->active = false;
154 trace_spice_vmc_unregister_interface(scd);
155}
156
157
158static int spice_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
159{
160 SpiceCharDriver *s = chr->opaque;
161
162 dprintf(s, 2, "%s: %d\n", __func__, len);
163 vmc_register_interface(s);
164 assert(s->datalen == 0);
165 if (s->bufsize < len) {
166 s->bufsize = len;
Anthony Liguori7267c092011-08-20 22:09:37 -0500167 s->buffer = g_realloc(s->buffer, s->bufsize);
Alon Levycbcc6332011-01-19 10:49:50 +0200168 }
169 memcpy(s->buffer, buf, len);
170 s->datapos = s->buffer;
171 s->datalen = len;
172 spice_server_char_device_wakeup(&s->sin);
173 return len;
174}
175
176static void spice_chr_close(struct CharDriverState *chr)
177{
178 SpiceCharDriver *s = chr->opaque;
179
180 printf("%s\n", __func__);
181 vmc_unregister_interface(s);
Anthony Liguori7267c092011-08-20 22:09:37 -0500182 g_free(s);
Alon Levycbcc6332011-01-19 10:49:50 +0200183}
184
Hans de Goedecd8f7df2011-03-24 11:12:04 +0100185static void spice_chr_guest_open(struct CharDriverState *chr)
186{
187 SpiceCharDriver *s = chr->opaque;
188 vmc_register_interface(s);
189}
190
191static void spice_chr_guest_close(struct CharDriverState *chr)
192{
193 SpiceCharDriver *s = chr->opaque;
194 vmc_unregister_interface(s);
195}
196
Alon Levycbcc6332011-01-19 10:49:50 +0200197static void print_allowed_subtypes(void)
198{
199 const char** psubtype;
200 int i;
201
202 fprintf(stderr, "allowed names: ");
203 for(i=0, psubtype = spice_server_char_device_recognized_subtypes();
204 *psubtype != NULL; ++psubtype, ++i) {
205 if (i == 0) {
206 fprintf(stderr, "%s", *psubtype);
207 } else {
208 fprintf(stderr, ", %s", *psubtype);
209 }
210 }
211 fprintf(stderr, "\n");
212}
213
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100214static CharDriverState *chr_open(QemuOpts *opts, const char *subtype)
Alon Levycbcc6332011-01-19 10:49:50 +0200215{
216 CharDriverState *chr;
217 SpiceCharDriver *s;
Alon Levycbcc6332011-01-19 10:49:50 +0200218 uint32_t debug = qemu_opt_get_number(opts, "debug", 0);
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100219
220 chr = g_malloc0(sizeof(CharDriverState));
221 s = g_malloc0(sizeof(SpiceCharDriver));
222 s->chr = chr;
223 s->debug = debug;
224 s->active = false;
225 s->sin.subtype = subtype;
226 chr->opaque = s;
227 chr->chr_write = spice_chr_write;
228 chr->chr_close = spice_chr_close;
229 chr->chr_guest_open = spice_chr_guest_open;
230 chr->chr_guest_close = spice_chr_guest_close;
231
232 return chr;
233}
234
235CharDriverState *qemu_chr_open_spice(QemuOpts *opts)
236{
237 CharDriverState *chr;
238 const char *name = qemu_opt_get(opts, "name");
239 const char **psubtype = spice_server_char_device_recognized_subtypes();
Alon Levycbcc6332011-01-19 10:49:50 +0200240 const char *subtype = NULL;
241
242 if (name == NULL) {
243 fprintf(stderr, "spice-qemu-char: missing name parameter\n");
244 print_allowed_subtypes();
Markus Armbruster1f514702012-02-07 15:09:08 +0100245 return NULL;
Alon Levycbcc6332011-01-19 10:49:50 +0200246 }
247 for(;*psubtype != NULL; ++psubtype) {
248 if (strcmp(name, *psubtype) == 0) {
249 subtype = *psubtype;
250 break;
251 }
252 }
253 if (subtype == NULL) {
Eduardo Elias Ferreira4f5c0172012-04-16 09:51:42 -0300254 fprintf(stderr, "spice-qemu-char: unsupported name: %s\n", name);
Alon Levycbcc6332011-01-19 10:49:50 +0200255 print_allowed_subtypes();
Markus Armbruster1f514702012-02-07 15:09:08 +0100256 return NULL;
Alon Levycbcc6332011-01-19 10:49:50 +0200257 }
258
Marc-André Lureau71b423f2012-12-05 16:15:33 +0100259 chr = chr_open(opts, subtype);
Alon Levycbcc6332011-01-19 10:49:50 +0200260
Hans de Goedef76e4c72011-11-19 10:22:44 +0100261#if SPICE_SERVER_VERSION < 0x000901
262 /* See comment in vmc_state() */
263 if (strcmp(subtype, "vdagent") == 0) {
264 qemu_chr_generic_open(chr);
265 }
266#endif
Alon Levycbcc6332011-01-19 10:49:50 +0200267
Markus Armbruster1f514702012-02-07 15:09:08 +0100268 return chr;
Alon Levycbcc6332011-01-19 10:49:50 +0200269}
Marc-André Lureau5a49d3e2012-12-05 16:15:34 +0100270
271#if SPICE_SERVER_VERSION >= 0x000c02
272CharDriverState *qemu_chr_open_spice_port(QemuOpts *opts)
273{
274 CharDriverState *chr;
275 SpiceCharDriver *s;
276 const char *name = qemu_opt_get(opts, "name");
277
278 if (name == NULL) {
279 fprintf(stderr, "spice-qemu-char: missing name parameter\n");
280 return NULL;
281 }
282
283 chr = chr_open(opts, "port");
284 s = chr->opaque;
285 s->sin.portname = name;
286
287 return chr;
288}
289#endif