blob: 4b03143f68e7466f0a8e26beab8a3214ba99e3f0 [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"
Paolo Bonzini927d4872012-12-17 18:20:05 +01004#include "char/char.h"
Alon Levycbcc6332011-01-19 10:49:50 +02005#include <spice.h>
6#include <spice-experimental.h>
7
Paolo Bonzini1de7afc2012-12-17 18:20:00 +01008#include "qemu/osdep.h"
Alon Levycbcc6332011-01-19 10:49:50 +02009
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#define VMC_MAX_HOST_WRITE 2048
19
20typedef struct SpiceCharDriver {
21 CharDriverState* chr;
22 SpiceCharDeviceInstance sin;
23 char *subtype;
24 bool active;
25 uint8_t *buffer;
26 uint8_t *datapos;
27 ssize_t bufsize, datalen;
28 uint32_t debug;
29} SpiceCharDriver;
30
31static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
32{
33 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
34 ssize_t out = 0;
35 ssize_t last_out;
36 uint8_t* p = (uint8_t*)buf;
37
38 while (len > 0) {
39 last_out = MIN(len, VMC_MAX_HOST_WRITE);
Anthony Liguori909cda12011-08-15 11:17:31 -050040 if (qemu_chr_be_can_write(scd->chr) < last_out) {
Alon Levycbcc6332011-01-19 10:49:50 +020041 break;
42 }
Anthony Liguorifa5efcc2011-08-15 11:17:30 -050043 qemu_chr_be_write(scd->chr, p, last_out);
Hans de Goede35106c22011-03-22 16:28:41 +010044 out += last_out;
45 len -= last_out;
46 p += last_out;
Alon Levycbcc6332011-01-19 10:49:50 +020047 }
48
Peter Maydell7b6c7362011-08-09 23:04:35 +010049 dprintf(scd, 3, "%s: %zu/%zd\n", __func__, out, len + out);
Alon Levycbcc6332011-01-19 10:49:50 +020050 trace_spice_vmc_write(out, len + out);
51 return out;
52}
53
54static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)
55{
56 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
57 int bytes = MIN(len, scd->datalen);
58
59 dprintf(scd, 2, "%s: %p %d/%d/%zd\n", __func__, scd->datapos, len, bytes, scd->datalen);
60 if (bytes > 0) {
61 memcpy(buf, scd->datapos, bytes);
62 scd->datapos += bytes;
63 scd->datalen -= bytes;
64 assert(scd->datalen >= 0);
65 if (scd->datalen == 0) {
66 scd->datapos = 0;
67 }
68 }
69 trace_spice_vmc_read(bytes, len);
70 return bytes;
71}
72
Hans de Goedef76e4c72011-11-19 10:22:44 +010073static void vmc_state(SpiceCharDeviceInstance *sin, int connected)
74{
75 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
76
77#if SPICE_SERVER_VERSION < 0x000901
78 /*
79 * spice-server calls the state callback for the agent channel when the
80 * spice client connects / disconnects. Given that not the client but
81 * the server is doing the parsing of the messages this is wrong as the
82 * server is still listening. Worse, this causes the parser in the server
83 * to go out of sync, so we ignore state calls for subtype vdagent
84 * spicevmc chardevs. For the full story see:
85 * http://lists.freedesktop.org/archives/spice-devel/2011-July/004837.html
86 */
87 if (strcmp(sin->subtype, "vdagent") == 0) {
88 return;
89 }
90#endif
91
92 if ((scd->chr->opened && connected) ||
93 (!scd->chr->opened && !connected)) {
94 return;
95 }
96
97 qemu_chr_be_event(scd->chr,
98 connected ? CHR_EVENT_OPENED : CHR_EVENT_CLOSED);
99}
100
Alon Levycbcc6332011-01-19 10:49:50 +0200101static SpiceCharDeviceInterface vmc_interface = {
102 .base.type = SPICE_INTERFACE_CHAR_DEVICE,
103 .base.description = "spice virtual channel char device",
104 .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,
105 .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,
Hans de Goedef76e4c72011-11-19 10:22:44 +0100106 .state = vmc_state,
Alon Levycbcc6332011-01-19 10:49:50 +0200107 .write = vmc_write,
108 .read = vmc_read,
109};
110
111
112static void vmc_register_interface(SpiceCharDriver *scd)
113{
114 if (scd->active) {
115 return;
116 }
117 dprintf(scd, 1, "%s\n", __func__);
118 scd->sin.base.sif = &vmc_interface.base;
119 qemu_spice_add_interface(&scd->sin.base);
120 scd->active = true;
121 trace_spice_vmc_register_interface(scd);
122}
123
124static void vmc_unregister_interface(SpiceCharDriver *scd)
125{
126 if (!scd->active) {
127 return;
128 }
129 dprintf(scd, 1, "%s\n", __func__);
130 spice_server_remove_interface(&scd->sin.base);
131 scd->active = false;
132 trace_spice_vmc_unregister_interface(scd);
133}
134
135
136static int spice_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
137{
138 SpiceCharDriver *s = chr->opaque;
139
140 dprintf(s, 2, "%s: %d\n", __func__, len);
141 vmc_register_interface(s);
142 assert(s->datalen == 0);
143 if (s->bufsize < len) {
144 s->bufsize = len;
Anthony Liguori7267c092011-08-20 22:09:37 -0500145 s->buffer = g_realloc(s->buffer, s->bufsize);
Alon Levycbcc6332011-01-19 10:49:50 +0200146 }
147 memcpy(s->buffer, buf, len);
148 s->datapos = s->buffer;
149 s->datalen = len;
150 spice_server_char_device_wakeup(&s->sin);
151 return len;
152}
153
154static void spice_chr_close(struct CharDriverState *chr)
155{
156 SpiceCharDriver *s = chr->opaque;
157
158 printf("%s\n", __func__);
159 vmc_unregister_interface(s);
Anthony Liguori7267c092011-08-20 22:09:37 -0500160 g_free(s);
Alon Levycbcc6332011-01-19 10:49:50 +0200161}
162
Hans de Goedecd8f7df2011-03-24 11:12:04 +0100163static void spice_chr_guest_open(struct CharDriverState *chr)
164{
165 SpiceCharDriver *s = chr->opaque;
166 vmc_register_interface(s);
167}
168
169static void spice_chr_guest_close(struct CharDriverState *chr)
170{
171 SpiceCharDriver *s = chr->opaque;
172 vmc_unregister_interface(s);
173}
174
Alon Levycbcc6332011-01-19 10:49:50 +0200175static void print_allowed_subtypes(void)
176{
177 const char** psubtype;
178 int i;
179
180 fprintf(stderr, "allowed names: ");
181 for(i=0, psubtype = spice_server_char_device_recognized_subtypes();
182 *psubtype != NULL; ++psubtype, ++i) {
183 if (i == 0) {
184 fprintf(stderr, "%s", *psubtype);
185 } else {
186 fprintf(stderr, ", %s", *psubtype);
187 }
188 }
189 fprintf(stderr, "\n");
190}
191
Markus Armbruster1f514702012-02-07 15:09:08 +0100192CharDriverState *qemu_chr_open_spice(QemuOpts *opts)
Alon Levycbcc6332011-01-19 10:49:50 +0200193{
194 CharDriverState *chr;
195 SpiceCharDriver *s;
196 const char* name = qemu_opt_get(opts, "name");
197 uint32_t debug = qemu_opt_get_number(opts, "debug", 0);
198 const char** psubtype = spice_server_char_device_recognized_subtypes();
199 const char *subtype = NULL;
200
201 if (name == NULL) {
202 fprintf(stderr, "spice-qemu-char: missing name parameter\n");
203 print_allowed_subtypes();
Markus Armbruster1f514702012-02-07 15:09:08 +0100204 return NULL;
Alon Levycbcc6332011-01-19 10:49:50 +0200205 }
206 for(;*psubtype != NULL; ++psubtype) {
207 if (strcmp(name, *psubtype) == 0) {
208 subtype = *psubtype;
209 break;
210 }
211 }
212 if (subtype == NULL) {
Eduardo Elias Ferreira4f5c0172012-04-16 09:51:42 -0300213 fprintf(stderr, "spice-qemu-char: unsupported name: %s\n", name);
Alon Levycbcc6332011-01-19 10:49:50 +0200214 print_allowed_subtypes();
Markus Armbruster1f514702012-02-07 15:09:08 +0100215 return NULL;
Alon Levycbcc6332011-01-19 10:49:50 +0200216 }
217
Anthony Liguori7267c092011-08-20 22:09:37 -0500218 chr = g_malloc0(sizeof(CharDriverState));
219 s = g_malloc0(sizeof(SpiceCharDriver));
Alon Levycbcc6332011-01-19 10:49:50 +0200220 s->chr = chr;
221 s->debug = debug;
222 s->active = false;
223 s->sin.subtype = subtype;
224 chr->opaque = s;
225 chr->chr_write = spice_chr_write;
226 chr->chr_close = spice_chr_close;
Hans de Goedecd8f7df2011-03-24 11:12:04 +0100227 chr->chr_guest_open = spice_chr_guest_open;
228 chr->chr_guest_close = spice_chr_guest_close;
Alon Levycbcc6332011-01-19 10:49:50 +0200229
Hans de Goedef76e4c72011-11-19 10:22:44 +0100230#if SPICE_SERVER_VERSION < 0x000901
231 /* See comment in vmc_state() */
232 if (strcmp(subtype, "vdagent") == 0) {
233 qemu_chr_generic_open(chr);
234 }
235#endif
Alon Levycbcc6332011-01-19 10:49:50 +0200236
Markus Armbruster1f514702012-02-07 15:09:08 +0100237 return chr;
Alon Levycbcc6332011-01-19 10:49:50 +0200238}