]> git.proxmox.com Git - qemu.git/blob - spice-qemu-char.c
spice-qemu-char: add spiceport chardev
[qemu.git] / spice-qemu-char.c
1 #include "config-host.h"
2 #include "trace.h"
3 #include "ui/qemu-spice.h"
4 #include <spice.h>
5 #include <spice-experimental.h>
6 #include <spice/protocol.h>
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
18 typedef 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
29 static 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) {
37 last_out = MIN(len, qemu_chr_be_can_write(scd->chr));
38 if (last_out <= 0) {
39 break;
40 }
41 qemu_chr_be_write(scd->chr, p, last_out);
42 out += last_out;
43 len -= last_out;
44 p += last_out;
45 }
46
47 dprintf(scd, 3, "%s: %zu/%zd\n", __func__, out, len + out);
48 trace_spice_vmc_write(out, len + out);
49 return out;
50 }
51
52 static 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
71 #if SPICE_SERVER_VERSION >= 0x000c02
72 static 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
92 static 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
120 static 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,
125 .state = vmc_state,
126 .write = vmc_write,
127 .read = vmc_read,
128 #if SPICE_SERVER_VERSION >= 0x000c02
129 .event = vmc_event,
130 #endif
131 };
132
133
134 static 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
146 static 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
158 static 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;
167 s->buffer = g_realloc(s->buffer, s->bufsize);
168 }
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
176 static void spice_chr_close(struct CharDriverState *chr)
177 {
178 SpiceCharDriver *s = chr->opaque;
179
180 printf("%s\n", __func__);
181 vmc_unregister_interface(s);
182 g_free(s);
183 }
184
185 static void spice_chr_guest_open(struct CharDriverState *chr)
186 {
187 SpiceCharDriver *s = chr->opaque;
188 vmc_register_interface(s);
189 }
190
191 static void spice_chr_guest_close(struct CharDriverState *chr)
192 {
193 SpiceCharDriver *s = chr->opaque;
194 vmc_unregister_interface(s);
195 }
196
197 static 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
214 static CharDriverState *chr_open(QemuOpts *opts, const char *subtype)
215 {
216 CharDriverState *chr;
217 SpiceCharDriver *s;
218 uint32_t debug = qemu_opt_get_number(opts, "debug", 0);
219
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
235 CharDriverState *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();
240 const char *subtype = NULL;
241
242 if (name == NULL) {
243 fprintf(stderr, "spice-qemu-char: missing name parameter\n");
244 print_allowed_subtypes();
245 return NULL;
246 }
247 for(;*psubtype != NULL; ++psubtype) {
248 if (strcmp(name, *psubtype) == 0) {
249 subtype = *psubtype;
250 break;
251 }
252 }
253 if (subtype == NULL) {
254 fprintf(stderr, "spice-qemu-char: unsupported name: %s\n", name);
255 print_allowed_subtypes();
256 return NULL;
257 }
258
259 chr = chr_open(opts, subtype);
260
261 #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
267
268 return chr;
269 }
270
271 #if SPICE_SERVER_VERSION >= 0x000c02
272 CharDriverState *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