blob: 665efd30ee99231bd99558600d9ce1703ea590b2 [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>
6
7#include "osdep.h"
8
9#define dprintf(_scd, _level, _fmt, ...) \
10 do { \
11 static unsigned __dprintf_counter = 0; \
12 if (_scd->debug >= _level) { \
13 fprintf(stderr, "scd: %3d: " _fmt, ++__dprintf_counter, ## __VA_ARGS__);\
14 } \
15 } while (0)
16
Alon Levycbcc6332011-01-19 10:49:50 +020017typedef struct SpiceCharDriver {
18 CharDriverState* chr;
19 SpiceCharDeviceInstance sin;
20 char *subtype;
21 bool active;
22 uint8_t *buffer;
23 uint8_t *datapos;
24 ssize_t bufsize, datalen;
25 uint32_t debug;
26} SpiceCharDriver;
27
28static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
29{
30 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
31 ssize_t out = 0;
32 ssize_t last_out;
33 uint8_t* p = (uint8_t*)buf;
34
35 while (len > 0) {
Marc-André Lureau07a54d72012-12-05 16:15:32 +010036 last_out = MIN(len, qemu_chr_be_can_write(scd->chr));
37 if (last_out <= 0) {
Alon Levycbcc6332011-01-19 10:49:50 +020038 break;
39 }
Anthony Liguorifa5efcc2011-08-15 11:17:30 -050040 qemu_chr_be_write(scd->chr, p, last_out);
Hans de Goede35106c22011-03-22 16:28:41 +010041 out += last_out;
42 len -= last_out;
43 p += last_out;
Alon Levycbcc6332011-01-19 10:49:50 +020044 }
45
Peter Maydell7b6c7362011-08-09 23:04:35 +010046 dprintf(scd, 3, "%s: %zu/%zd\n", __func__, out, len + out);
Alon Levycbcc6332011-01-19 10:49:50 +020047 trace_spice_vmc_write(out, len + out);
48 return out;
49}
50
51static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)
52{
53 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
54 int bytes = MIN(len, scd->datalen);
55
56 dprintf(scd, 2, "%s: %p %d/%d/%zd\n", __func__, scd->datapos, len, bytes, scd->datalen);
57 if (bytes > 0) {
58 memcpy(buf, scd->datapos, bytes);
59 scd->datapos += bytes;
60 scd->datalen -= bytes;
61 assert(scd->datalen >= 0);
62 if (scd->datalen == 0) {
63 scd->datapos = 0;
64 }
65 }
66 trace_spice_vmc_read(bytes, len);
67 return bytes;
68}
69
Hans de Goedef76e4c72011-11-19 10:22:44 +010070static void vmc_state(SpiceCharDeviceInstance *sin, int connected)
71{
72 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
73
74#if SPICE_SERVER_VERSION < 0x000901
75 /*
76 * spice-server calls the state callback for the agent channel when the
77 * spice client connects / disconnects. Given that not the client but
78 * the server is doing the parsing of the messages this is wrong as the
79 * server is still listening. Worse, this causes the parser in the server
80 * to go out of sync, so we ignore state calls for subtype vdagent
81 * spicevmc chardevs. For the full story see:
82 * http://lists.freedesktop.org/archives/spice-devel/2011-July/004837.html
83 */
84 if (strcmp(sin->subtype, "vdagent") == 0) {
85 return;
86 }
87#endif
88
89 if ((scd->chr->opened && connected) ||
90 (!scd->chr->opened && !connected)) {
91 return;
92 }
93
94 qemu_chr_be_event(scd->chr,
95 connected ? CHR_EVENT_OPENED : CHR_EVENT_CLOSED);
96}
97
Alon Levycbcc6332011-01-19 10:49:50 +020098static SpiceCharDeviceInterface vmc_interface = {
99 .base.type = SPICE_INTERFACE_CHAR_DEVICE,
100 .base.description = "spice virtual channel char device",
101 .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,
102 .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,
Hans de Goedef76e4c72011-11-19 10:22:44 +0100103 .state = vmc_state,
Alon Levycbcc6332011-01-19 10:49:50 +0200104 .write = vmc_write,
105 .read = vmc_read,
106};
107
108
109static void vmc_register_interface(SpiceCharDriver *scd)
110{
111 if (scd->active) {
112 return;
113 }
114 dprintf(scd, 1, "%s\n", __func__);
115 scd->sin.base.sif = &vmc_interface.base;
116 qemu_spice_add_interface(&scd->sin.base);
117 scd->active = true;
118 trace_spice_vmc_register_interface(scd);
119}
120
121static void vmc_unregister_interface(SpiceCharDriver *scd)
122{
123 if (!scd->active) {
124 return;
125 }
126 dprintf(scd, 1, "%s\n", __func__);
127 spice_server_remove_interface(&scd->sin.base);
128 scd->active = false;
129 trace_spice_vmc_unregister_interface(scd);
130}
131
132
133static int spice_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
134{
135 SpiceCharDriver *s = chr->opaque;
136
137 dprintf(s, 2, "%s: %d\n", __func__, len);
138 vmc_register_interface(s);
139 assert(s->datalen == 0);
140 if (s->bufsize < len) {
141 s->bufsize = len;
Anthony Liguori7267c092011-08-20 22:09:37 -0500142 s->buffer = g_realloc(s->buffer, s->bufsize);
Alon Levycbcc6332011-01-19 10:49:50 +0200143 }
144 memcpy(s->buffer, buf, len);
145 s->datapos = s->buffer;
146 s->datalen = len;
147 spice_server_char_device_wakeup(&s->sin);
148 return len;
149}
150
151static void spice_chr_close(struct CharDriverState *chr)
152{
153 SpiceCharDriver *s = chr->opaque;
154
155 printf("%s\n", __func__);
156 vmc_unregister_interface(s);
Anthony Liguori7267c092011-08-20 22:09:37 -0500157 g_free(s);
Alon Levycbcc6332011-01-19 10:49:50 +0200158}
159
Hans de Goedecd8f7df2011-03-24 11:12:04 +0100160static void spice_chr_guest_open(struct CharDriverState *chr)
161{
162 SpiceCharDriver *s = chr->opaque;
163 vmc_register_interface(s);
164}
165
166static void spice_chr_guest_close(struct CharDriverState *chr)
167{
168 SpiceCharDriver *s = chr->opaque;
169 vmc_unregister_interface(s);
170}
171
Alon Levycbcc6332011-01-19 10:49:50 +0200172static void print_allowed_subtypes(void)
173{
174 const char** psubtype;
175 int i;
176
177 fprintf(stderr, "allowed names: ");
178 for(i=0, psubtype = spice_server_char_device_recognized_subtypes();
179 *psubtype != NULL; ++psubtype, ++i) {
180 if (i == 0) {
181 fprintf(stderr, "%s", *psubtype);
182 } else {
183 fprintf(stderr, ", %s", *psubtype);
184 }
185 }
186 fprintf(stderr, "\n");
187}
188
Markus Armbruster1f514702012-02-07 15:09:08 +0100189CharDriverState *qemu_chr_open_spice(QemuOpts *opts)
Alon Levycbcc6332011-01-19 10:49:50 +0200190{
191 CharDriverState *chr;
192 SpiceCharDriver *s;
193 const char* name = qemu_opt_get(opts, "name");
194 uint32_t debug = qemu_opt_get_number(opts, "debug", 0);
195 const char** psubtype = spice_server_char_device_recognized_subtypes();
196 const char *subtype = NULL;
197
198 if (name == NULL) {
199 fprintf(stderr, "spice-qemu-char: missing name parameter\n");
200 print_allowed_subtypes();
Markus Armbruster1f514702012-02-07 15:09:08 +0100201 return NULL;
Alon Levycbcc6332011-01-19 10:49:50 +0200202 }
203 for(;*psubtype != NULL; ++psubtype) {
204 if (strcmp(name, *psubtype) == 0) {
205 subtype = *psubtype;
206 break;
207 }
208 }
209 if (subtype == NULL) {
Eduardo Elias Ferreira4f5c0172012-04-16 09:51:42 -0300210 fprintf(stderr, "spice-qemu-char: unsupported name: %s\n", name);
Alon Levycbcc6332011-01-19 10:49:50 +0200211 print_allowed_subtypes();
Markus Armbruster1f514702012-02-07 15:09:08 +0100212 return NULL;
Alon Levycbcc6332011-01-19 10:49:50 +0200213 }
214
Anthony Liguori7267c092011-08-20 22:09:37 -0500215 chr = g_malloc0(sizeof(CharDriverState));
216 s = g_malloc0(sizeof(SpiceCharDriver));
Alon Levycbcc6332011-01-19 10:49:50 +0200217 s->chr = chr;
218 s->debug = debug;
219 s->active = false;
220 s->sin.subtype = subtype;
221 chr->opaque = s;
222 chr->chr_write = spice_chr_write;
223 chr->chr_close = spice_chr_close;
Hans de Goedecd8f7df2011-03-24 11:12:04 +0100224 chr->chr_guest_open = spice_chr_guest_open;
225 chr->chr_guest_close = spice_chr_guest_close;
Alon Levycbcc6332011-01-19 10:49:50 +0200226
Hans de Goedef76e4c72011-11-19 10:22:44 +0100227#if SPICE_SERVER_VERSION < 0x000901
228 /* See comment in vmc_state() */
229 if (strcmp(subtype, "vdagent") == 0) {
230 qemu_chr_generic_open(chr);
231 }
232#endif
Alon Levycbcc6332011-01-19 10:49:50 +0200233
Markus Armbruster1f514702012-02-07 15:09:08 +0100234 return chr;
Alon Levycbcc6332011-01-19 10:49:50 +0200235}