Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 1 | #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é Lureau | 5a49d3e | 2012-12-05 16:15:34 +0100 | [diff] [blame] | 6 | #include <spice/protocol.h> |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 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 | |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 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; |
Marc-André Lureau | 7a5448c | 2012-12-05 16:15:35 +0100 | [diff] [blame] | 27 | QLIST_ENTRY(SpiceCharDriver) next; |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 28 | } SpiceCharDriver; |
| 29 | |
Marc-André Lureau | 7a5448c | 2012-12-05 16:15:35 +0100 | [diff] [blame] | 30 | static QLIST_HEAD(, SpiceCharDriver) spice_chars = |
| 31 | QLIST_HEAD_INITIALIZER(spice_chars); |
| 32 | |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 33 | static 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) { |
Marc-André Lureau | 07a54d7 | 2012-12-05 16:15:32 +0100 | [diff] [blame] | 41 | last_out = MIN(len, qemu_chr_be_can_write(scd->chr)); |
| 42 | if (last_out <= 0) { |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 43 | break; |
| 44 | } |
Anthony Liguori | fa5efcc | 2011-08-15 11:17:30 -0500 | [diff] [blame] | 45 | qemu_chr_be_write(scd->chr, p, last_out); |
Hans de Goede | 35106c2 | 2011-03-22 16:28:41 +0100 | [diff] [blame] | 46 | out += last_out; |
| 47 | len -= last_out; |
| 48 | p += last_out; |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 49 | } |
| 50 | |
Peter Maydell | 7b6c736 | 2011-08-09 23:04:35 +0100 | [diff] [blame] | 51 | dprintf(scd, 3, "%s: %zu/%zd\n", __func__, out, len + out); |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 52 | trace_spice_vmc_write(out, len + out); |
| 53 | return out; |
| 54 | } |
| 55 | |
| 56 | static 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 | |
Marc-André Lureau | 5a49d3e | 2012-12-05 16:15:34 +0100 | [diff] [blame] | 75 | #if SPICE_SERVER_VERSION >= 0x000c02 |
| 76 | static 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 | |
Hans de Goede | f76e4c7 | 2011-11-19 10:22:44 +0100 | [diff] [blame] | 96 | static 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 | |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 124 | static 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, |
Hans de Goede | f76e4c7 | 2011-11-19 10:22:44 +0100 | [diff] [blame] | 129 | .state = vmc_state, |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 130 | .write = vmc_write, |
| 131 | .read = vmc_read, |
Marc-André Lureau | 5a49d3e | 2012-12-05 16:15:34 +0100 | [diff] [blame] | 132 | #if SPICE_SERVER_VERSION >= 0x000c02 |
| 133 | .event = vmc_event, |
| 134 | #endif |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | |
| 138 | static 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 | |
| 150 | static 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 | |
| 162 | static 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; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 171 | s->buffer = g_realloc(s->buffer, s->bufsize); |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 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 | |
| 180 | static void spice_chr_close(struct CharDriverState *chr) |
| 181 | { |
| 182 | SpiceCharDriver *s = chr->opaque; |
| 183 | |
| 184 | printf("%s\n", __func__); |
| 185 | vmc_unregister_interface(s); |
Marc-André Lureau | 7a5448c | 2012-12-05 16:15:35 +0100 | [diff] [blame] | 186 | QLIST_REMOVE(s, next); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 187 | g_free(s); |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Hans de Goede | cd8f7df | 2011-03-24 11:12:04 +0100 | [diff] [blame] | 190 | static void spice_chr_guest_open(struct CharDriverState *chr) |
| 191 | { |
| 192 | SpiceCharDriver *s = chr->opaque; |
| 193 | vmc_register_interface(s); |
| 194 | } |
| 195 | |
| 196 | static void spice_chr_guest_close(struct CharDriverState *chr) |
| 197 | { |
| 198 | SpiceCharDriver *s = chr->opaque; |
| 199 | vmc_unregister_interface(s); |
| 200 | } |
| 201 | |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 202 | static 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 | |
Marc-André Lureau | 71b423f | 2012-12-05 16:15:33 +0100 | [diff] [blame] | 219 | static CharDriverState *chr_open(QemuOpts *opts, const char *subtype) |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 220 | { |
| 221 | CharDriverState *chr; |
| 222 | SpiceCharDriver *s; |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 223 | uint32_t debug = qemu_opt_get_number(opts, "debug", 0); |
Marc-André Lureau | 71b423f | 2012-12-05 16:15:33 +0100 | [diff] [blame] | 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 | |
Marc-André Lureau | 7a5448c | 2012-12-05 16:15:35 +0100 | [diff] [blame] | 237 | QLIST_INSERT_HEAD(&spice_chars, s, next); |
| 238 | |
Marc-André Lureau | 71b423f | 2012-12-05 16:15:33 +0100 | [diff] [blame] | 239 | return chr; |
| 240 | } |
| 241 | |
| 242 | CharDriverState *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(); |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 247 | const char *subtype = NULL; |
| 248 | |
| 249 | if (name == NULL) { |
| 250 | fprintf(stderr, "spice-qemu-char: missing name parameter\n"); |
| 251 | print_allowed_subtypes(); |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 252 | return NULL; |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 253 | } |
| 254 | for(;*psubtype != NULL; ++psubtype) { |
| 255 | if (strcmp(name, *psubtype) == 0) { |
| 256 | subtype = *psubtype; |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | if (subtype == NULL) { |
Eduardo Elias Ferreira | 4f5c017 | 2012-04-16 09:51:42 -0300 | [diff] [blame] | 261 | fprintf(stderr, "spice-qemu-char: unsupported name: %s\n", name); |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 262 | print_allowed_subtypes(); |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 263 | return NULL; |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 264 | } |
| 265 | |
Marc-André Lureau | 71b423f | 2012-12-05 16:15:33 +0100 | [diff] [blame] | 266 | chr = chr_open(opts, subtype); |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 267 | |
Hans de Goede | f76e4c7 | 2011-11-19 10:22:44 +0100 | [diff] [blame] | 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 |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 274 | |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 275 | return chr; |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 276 | } |
Marc-André Lureau | 5a49d3e | 2012-12-05 16:15:34 +0100 | [diff] [blame] | 277 | |
| 278 | #if SPICE_SERVER_VERSION >= 0x000c02 |
| 279 | CharDriverState *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 | } |
Marc-André Lureau | afd0b40 | 2012-12-05 16:15:36 +0100 | [diff] [blame^] | 296 | |
| 297 | void 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 | } |
Marc-André Lureau | 5a49d3e | 2012-12-05 16:15:34 +0100 | [diff] [blame] | 308 | #endif |