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; |
| 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) { |
Marc-André Lureau | 07a54d7 | 2012-12-05 16:15:32 +0100 | [diff] [blame] | 37 | last_out = MIN(len, qemu_chr_be_can_write(scd->chr)); |
| 38 | if (last_out <= 0) { |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 39 | break; |
| 40 | } |
Anthony Liguori | fa5efcc | 2011-08-15 11:17:30 -0500 | [diff] [blame] | 41 | qemu_chr_be_write(scd->chr, p, last_out); |
Hans de Goede | 35106c2 | 2011-03-22 16:28:41 +0100 | [diff] [blame] | 42 | out += last_out; |
| 43 | len -= last_out; |
| 44 | p += last_out; |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 45 | } |
| 46 | |
Peter Maydell | 7b6c736 | 2011-08-09 23:04:35 +0100 | [diff] [blame] | 47 | dprintf(scd, 3, "%s: %zu/%zd\n", __func__, out, len + out); |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 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 | |
Marc-André Lureau | 5a49d3e | 2012-12-05 16:15:34 +0100 | [diff] [blame^] | 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 | |
Hans de Goede | f76e4c7 | 2011-11-19 10:22:44 +0100 | [diff] [blame] | 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 | |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 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, |
Hans de Goede | f76e4c7 | 2011-11-19 10:22:44 +0100 | [diff] [blame] | 125 | .state = vmc_state, |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 126 | .write = vmc_write, |
| 127 | .read = vmc_read, |
Marc-André Lureau | 5a49d3e | 2012-12-05 16:15:34 +0100 | [diff] [blame^] | 128 | #if SPICE_SERVER_VERSION >= 0x000c02 |
| 129 | .event = vmc_event, |
| 130 | #endif |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 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; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 167 | s->buffer = g_realloc(s->buffer, s->bufsize); |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 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); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 182 | g_free(s); |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 183 | } |
| 184 | |
Hans de Goede | cd8f7df | 2011-03-24 11:12:04 +0100 | [diff] [blame] | 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 | |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 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 | |
Marc-André Lureau | 71b423f | 2012-12-05 16:15:33 +0100 | [diff] [blame] | 214 | static CharDriverState *chr_open(QemuOpts *opts, const char *subtype) |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 215 | { |
| 216 | CharDriverState *chr; |
| 217 | SpiceCharDriver *s; |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 218 | uint32_t debug = qemu_opt_get_number(opts, "debug", 0); |
Marc-André Lureau | 71b423f | 2012-12-05 16:15:33 +0100 | [diff] [blame] | 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(); |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 240 | const char *subtype = NULL; |
| 241 | |
| 242 | if (name == NULL) { |
| 243 | fprintf(stderr, "spice-qemu-char: missing name parameter\n"); |
| 244 | print_allowed_subtypes(); |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 245 | return NULL; |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 246 | } |
| 247 | for(;*psubtype != NULL; ++psubtype) { |
| 248 | if (strcmp(name, *psubtype) == 0) { |
| 249 | subtype = *psubtype; |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | if (subtype == NULL) { |
Eduardo Elias Ferreira | 4f5c017 | 2012-04-16 09:51:42 -0300 | [diff] [blame] | 254 | fprintf(stderr, "spice-qemu-char: unsupported name: %s\n", name); |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 255 | print_allowed_subtypes(); |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 256 | return NULL; |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 257 | } |
| 258 | |
Marc-André Lureau | 71b423f | 2012-12-05 16:15:33 +0100 | [diff] [blame] | 259 | chr = chr_open(opts, subtype); |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 260 | |
Hans de Goede | f76e4c7 | 2011-11-19 10:22:44 +0100 | [diff] [blame] | 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 |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 267 | |
Markus Armbruster | 1f51470 | 2012-02-07 15:09:08 +0100 | [diff] [blame] | 268 | return chr; |
Alon Levy | cbcc633 | 2011-01-19 10:49:50 +0200 | [diff] [blame] | 269 | } |
Marc-André Lureau | 5a49d3e | 2012-12-05 16:15:34 +0100 | [diff] [blame^] | 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 |