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