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