1 /* 2 * Copyright (C) 2010 Red Hat, Inc. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License as 6 * published by the Free Software Foundation; either version 2 or 7 * (at your option) version 3 of the License. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #include <spice.h> 19 #include <spice-experimental.h> 20 21 #include <netdb.h> 22 #include "sysemu.h" 23 24 #include "qemu-common.h" 25 #include "qemu-spice.h" 26 #include "qemu-thread.h" 27 #include "qemu-timer.h" 28 #include "qemu-queue.h" 29 #include "qemu-x509.h" 30 #include "qemu_socket.h" 31 #include "qmp-commands.h" 32 #include "qint.h" 33 #include "qbool.h" 34 #include "qstring.h" 35 #include "qjson.h" 36 #include "notify.h" 37 #include "migration.h" 38 #include "monitor.h" 39 #include "hw/hw.h" 40 41 /* core bits */ 42 43 static SpiceServer *spice_server; 44 static Notifier migration_state; 45 static const char *auth = "spice"; 46 static char *auth_passwd; 47 static time_t auth_expires = TIME_MAX; 48 int using_spice = 0; 49 50 static QemuThread me; 51 52 struct SpiceTimer { 53 QEMUTimer *timer; 54 QTAILQ_ENTRY(SpiceTimer) next; 55 }; 56 static QTAILQ_HEAD(, SpiceTimer) timers = QTAILQ_HEAD_INITIALIZER(timers); 57 58 static SpiceTimer *timer_add(SpiceTimerFunc func, void *opaque) 59 { 60 SpiceTimer *timer; 61 62 timer = g_malloc0(sizeof(*timer)); 63 timer->timer = qemu_new_timer_ms(rt_clock, func, opaque); 64 QTAILQ_INSERT_TAIL(&timers, timer, next); 65 return timer; 66 } 67 68 static void timer_start(SpiceTimer *timer, uint32_t ms) 69 { 70 qemu_mod_timer(timer->timer, qemu_get_clock_ms(rt_clock) + ms); 71 } 72 73 static void timer_cancel(SpiceTimer *timer) 74 { 75 qemu_del_timer(timer->timer); 76 } 77 78 static void timer_remove(SpiceTimer *timer) 79 { 80 qemu_del_timer(timer->timer); 81 qemu_free_timer(timer->timer); 82 QTAILQ_REMOVE(&timers, timer, next); 83 g_free(timer); 84 } 85 86 struct SpiceWatch { 87 int fd; 88 int event_mask; 89 SpiceWatchFunc func; 90 void *opaque; 91 QTAILQ_ENTRY(SpiceWatch) next; 92 }; 93 static QTAILQ_HEAD(, SpiceWatch) watches = QTAILQ_HEAD_INITIALIZER(watches); 94 95 static void watch_read(void *opaque) 96 { 97 SpiceWatch *watch = opaque; 98 watch->func(watch->fd, SPICE_WATCH_EVENT_READ, watch->opaque); 99 } 100 101 static void watch_write(void *opaque) 102 { 103 SpiceWatch *watch = opaque; 104 watch->func(watch->fd, SPICE_WATCH_EVENT_WRITE, watch->opaque); 105 } 106 107 static void watch_update_mask(SpiceWatch *watch, int event_mask) 108 { 109 IOHandler *on_read = NULL; 110 IOHandler *on_write = NULL; 111 112 watch->event_mask = event_mask; 113 if (watch->event_mask & SPICE_WATCH_EVENT_READ) { 114 on_read = watch_read; 115 } 116 if (watch->event_mask & SPICE_WATCH_EVENT_WRITE) { 117 on_write = watch_write; 118 } 119 qemu_set_fd_handler(watch->fd, on_read, on_write, watch); 120 } 121 122 static SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque) 123 { 124 SpiceWatch *watch; 125 126 watch = g_malloc0(sizeof(*watch)); 127 watch->fd = fd; 128 watch->func = func; 129 watch->opaque = opaque; 130 QTAILQ_INSERT_TAIL(&watches, watch, next); 131 132 watch_update_mask(watch, event_mask); 133 return watch; 134 } 135 136 static void watch_remove(SpiceWatch *watch) 137 { 138 qemu_set_fd_handler(watch->fd, NULL, NULL, NULL); 139 QTAILQ_REMOVE(&watches, watch, next); 140 g_free(watch); 141 } 142 143 typedef struct ChannelList ChannelList; 144 struct ChannelList { 145 SpiceChannelEventInfo *info; 146 QTAILQ_ENTRY(ChannelList) link; 147 }; 148 static QTAILQ_HEAD(, ChannelList) channel_list = QTAILQ_HEAD_INITIALIZER(channel_list); 149 150 static void channel_list_add(SpiceChannelEventInfo *info) 151 { 152 ChannelList *item; 153 154 item = g_malloc0(sizeof(*item)); 155 item->info = info; 156 QTAILQ_INSERT_TAIL(&channel_list, item, link); 157 } 158 159 static void channel_list_del(SpiceChannelEventInfo *info) 160 { 161 ChannelList *item; 162 163 QTAILQ_FOREACH(item, &channel_list, link) { 164 if (item->info != info) { 165 continue; 166 } 167 QTAILQ_REMOVE(&channel_list, item, link); 168 g_free(item); 169 return; 170 } 171 } 172 173 static void add_addr_info(QDict *dict, struct sockaddr *addr, int len) 174 { 175 char host[NI_MAXHOST], port[NI_MAXSERV]; 176 const char *family; 177 178 getnameinfo(addr, len, host, sizeof(host), port, sizeof(port), 179 NI_NUMERICHOST | NI_NUMERICSERV); 180 family = inet_strfamily(addr->sa_family); 181 182 qdict_put(dict, "host", qstring_from_str(host)); 183 qdict_put(dict, "port", qstring_from_str(port)); 184 qdict_put(dict, "family", qstring_from_str(family)); 185 } 186 187 static void add_channel_info(QDict *dict, SpiceChannelEventInfo *info) 188 { 189 int tls = info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS; 190 191 qdict_put(dict, "connection-id", qint_from_int(info->connection_id)); 192 qdict_put(dict, "channel-type", qint_from_int(info->type)); 193 qdict_put(dict, "channel-id", qint_from_int(info->id)); 194 qdict_put(dict, "tls", qbool_from_int(tls)); 195 } 196 197 static void channel_event(int event, SpiceChannelEventInfo *info) 198 { 199 static const int qevent[] = { 200 [ SPICE_CHANNEL_EVENT_CONNECTED ] = QEVENT_SPICE_CONNECTED, 201 [ SPICE_CHANNEL_EVENT_INITIALIZED ] = QEVENT_SPICE_INITIALIZED, 202 [ SPICE_CHANNEL_EVENT_DISCONNECTED ] = QEVENT_SPICE_DISCONNECTED, 203 }; 204 QDict *server, *client; 205 QObject *data; 206 207 /* 208 * Spice server might have called us from spice worker thread 209 * context (happens on display channel disconnects). Spice should 210 * not do that. It isn't that easy to fix it in spice and even 211 * when it is fixed we still should cover the already released 212 * spice versions. So detect that we've been called from another 213 * thread and grab the iothread lock if so before calling qemu 214 * functions. 215 */ 216 bool need_lock = !qemu_thread_is_self(&me); 217 if (need_lock) { 218 qemu_mutex_lock_iothread(); 219 } 220 221 client = qdict_new(); 222 server = qdict_new(); 223 224 #ifdef SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT 225 if (info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT) { 226 add_addr_info(client, (struct sockaddr *)&info->paddr_ext, 227 info->plen_ext); 228 add_addr_info(server, (struct sockaddr *)&info->laddr_ext, 229 info->llen_ext); 230 } else { 231 error_report("spice: %s, extended address is expected", 232 __func__); 233 #endif 234 add_addr_info(client, &info->paddr, info->plen); 235 add_addr_info(server, &info->laddr, info->llen); 236 #ifdef SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT 237 } 238 #endif 239 240 if (event == SPICE_CHANNEL_EVENT_INITIALIZED) { 241 qdict_put(server, "auth", qstring_from_str(auth)); 242 add_channel_info(client, info); 243 channel_list_add(info); 244 } 245 if (event == SPICE_CHANNEL_EVENT_DISCONNECTED) { 246 channel_list_del(info); 247 } 248 249 data = qobject_from_jsonf("{ 'client': %p, 'server': %p }", 250 QOBJECT(client), QOBJECT(server)); 251 monitor_protocol_event(qevent[event], data); 252 qobject_decref(data); 253 254 if (need_lock) { 255 qemu_mutex_unlock_iothread(); 256 } 257 } 258 259 static SpiceCoreInterface core_interface = { 260 .base.type = SPICE_INTERFACE_CORE, 261 .base.description = "qemu core services", 262 .base.major_version = SPICE_INTERFACE_CORE_MAJOR, 263 .base.minor_version = SPICE_INTERFACE_CORE_MINOR, 264 265 .timer_add = timer_add, 266 .timer_start = timer_start, 267 .timer_cancel = timer_cancel, 268 .timer_remove = timer_remove, 269 270 .watch_add = watch_add, 271 .watch_update_mask = watch_update_mask, 272 .watch_remove = watch_remove, 273 274 .channel_event = channel_event, 275 }; 276 277 #ifdef SPICE_INTERFACE_MIGRATION 278 typedef struct SpiceMigration { 279 SpiceMigrateInstance sin; 280 struct { 281 MonitorCompletion *cb; 282 void *opaque; 283 } connect_complete; 284 } SpiceMigration; 285 286 static void migrate_connect_complete_cb(SpiceMigrateInstance *sin); 287 288 static const SpiceMigrateInterface migrate_interface = { 289 .base.type = SPICE_INTERFACE_MIGRATION, 290 .base.description = "migration", 291 .base.major_version = SPICE_INTERFACE_MIGRATION_MAJOR, 292 .base.minor_version = SPICE_INTERFACE_MIGRATION_MINOR, 293 .migrate_connect_complete = migrate_connect_complete_cb, 294 .migrate_end_complete = NULL, 295 }; 296 297 static SpiceMigration spice_migrate; 298 299 static void migrate_connect_complete_cb(SpiceMigrateInstance *sin) 300 { 301 SpiceMigration *sm = container_of(sin, SpiceMigration, sin); 302 if (sm->connect_complete.cb) { 303 sm->connect_complete.cb(sm->connect_complete.opaque, NULL); 304 } 305 sm->connect_complete.cb = NULL; 306 } 307 #endif 308 309 /* config string parsing */ 310 311 static int name2enum(const char *string, const char *table[], int entries) 312 { 313 int i; 314 315 if (string) { 316 for (i = 0; i < entries; i++) { 317 if (!table[i]) { 318 continue; 319 } 320 if (strcmp(string, table[i]) != 0) { 321 continue; 322 } 323 return i; 324 } 325 } 326 return -1; 327 } 328 329 static int parse_name(const char *string, const char *optname, 330 const char *table[], int entries) 331 { 332 int value = name2enum(string, table, entries); 333 334 if (value != -1) { 335 return value; 336 } 337 error_report("spice: invalid %s: %s", optname, string); 338 exit(1); 339 } 340 341 static const char *stream_video_names[] = { 342 [ SPICE_STREAM_VIDEO_OFF ] = "off", 343 [ SPICE_STREAM_VIDEO_ALL ] = "all", 344 [ SPICE_STREAM_VIDEO_FILTER ] = "filter", 345 }; 346 #define parse_stream_video(_name) \ 347 name2enum(_name, stream_video_names, ARRAY_SIZE(stream_video_names)) 348 349 static const char *compression_names[] = { 350 [ SPICE_IMAGE_COMPRESS_OFF ] = "off", 351 [ SPICE_IMAGE_COMPRESS_AUTO_GLZ ] = "auto_glz", 352 [ SPICE_IMAGE_COMPRESS_AUTO_LZ ] = "auto_lz", 353 [ SPICE_IMAGE_COMPRESS_QUIC ] = "quic", 354 [ SPICE_IMAGE_COMPRESS_GLZ ] = "glz", 355 [ SPICE_IMAGE_COMPRESS_LZ ] = "lz", 356 }; 357 #define parse_compression(_name) \ 358 parse_name(_name, "image compression", \ 359 compression_names, ARRAY_SIZE(compression_names)) 360 361 static const char *wan_compression_names[] = { 362 [ SPICE_WAN_COMPRESSION_AUTO ] = "auto", 363 [ SPICE_WAN_COMPRESSION_NEVER ] = "never", 364 [ SPICE_WAN_COMPRESSION_ALWAYS ] = "always", 365 }; 366 #define parse_wan_compression(_name) \ 367 parse_name(_name, "wan compression", \ 368 wan_compression_names, ARRAY_SIZE(wan_compression_names)) 369 370 /* functions for the rest of qemu */ 371 372 static SpiceChannelList *qmp_query_spice_channels(void) 373 { 374 SpiceChannelList *cur_item = NULL, *head = NULL; 375 ChannelList *item; 376 377 QTAILQ_FOREACH(item, &channel_list, link) { 378 SpiceChannelList *chan; 379 char host[NI_MAXHOST], port[NI_MAXSERV]; 380 struct sockaddr *paddr; 381 socklen_t plen; 382 383 chan = g_malloc0(sizeof(*chan)); 384 chan->value = g_malloc0(sizeof(*chan->value)); 385 386 #ifdef SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT 387 if (item->info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT) { 388 paddr = (struct sockaddr *)&item->info->paddr_ext; 389 plen = item->info->plen_ext; 390 } else { 391 #endif 392 paddr = &item->info->paddr; 393 plen = item->info->plen; 394 #ifdef SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT 395 } 396 #endif 397 398 getnameinfo(paddr, plen, 399 host, sizeof(host), port, sizeof(port), 400 NI_NUMERICHOST | NI_NUMERICSERV); 401 chan->value->host = g_strdup(host); 402 chan->value->port = g_strdup(port); 403 chan->value->family = g_strdup(inet_strfamily(paddr->sa_family)); 404 405 chan->value->connection_id = item->info->connection_id; 406 chan->value->channel_type = item->info->type; 407 chan->value->channel_id = item->info->id; 408 chan->value->tls = item->info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS; 409 410 /* XXX: waiting for the qapi to support GSList */ 411 if (!cur_item) { 412 head = cur_item = chan; 413 } else { 414 cur_item->next = chan; 415 cur_item = chan; 416 } 417 } 418 419 return head; 420 } 421 422 SpiceInfo *qmp_query_spice(Error **errp) 423 { 424 QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head); 425 int port, tls_port; 426 const char *addr; 427 SpiceInfo *info; 428 char version_string[20]; /* 12 = |255.255.255\0| is the max */ 429 430 info = g_malloc0(sizeof(*info)); 431 432 if (!spice_server || !opts) { 433 info->enabled = false; 434 return info; 435 } 436 437 info->enabled = true; 438 439 addr = qemu_opt_get(opts, "addr"); 440 port = qemu_opt_get_number(opts, "port", 0); 441 tls_port = qemu_opt_get_number(opts, "tls-port", 0); 442 443 info->has_auth = true; 444 info->auth = g_strdup(auth); 445 446 info->has_host = true; 447 info->host = g_strdup(addr ? addr : "0.0.0.0"); 448 449 info->has_compiled_version = true; 450 snprintf(version_string, sizeof(version_string), "%d.%d.%d", 451 (SPICE_SERVER_VERSION & 0xff0000) >> 16, 452 (SPICE_SERVER_VERSION & 0xff00) >> 8, 453 SPICE_SERVER_VERSION & 0xff); 454 info->compiled_version = g_strdup(version_string); 455 456 if (port) { 457 info->has_port = true; 458 info->port = port; 459 } 460 if (tls_port) { 461 info->has_tls_port = true; 462 info->tls_port = tls_port; 463 } 464 465 #if SPICE_SERVER_VERSION >= 0x000a03 /* 0.10.3 */ 466 info->mouse_mode = spice_server_is_server_mouse(spice_server) ? 467 SPICE_QUERY_MOUSE_MODE_SERVER : 468 SPICE_QUERY_MOUSE_MODE_CLIENT; 469 #else 470 info->mouse_mode = SPICE_QUERY_MOUSE_MODE_UNKNOWN; 471 #endif 472 /* for compatibility with the original command */ 473 info->has_channels = true; 474 info->channels = qmp_query_spice_channels(); 475 476 return info; 477 } 478 479 static void migration_state_notifier(Notifier *notifier, void *data) 480 { 481 MigrationState *s = data; 482 483 if (migration_is_active(s)) { 484 #ifdef SPICE_INTERFACE_MIGRATION 485 spice_server_migrate_start(spice_server); 486 #endif 487 } else if (migration_has_finished(s)) { 488 #ifndef SPICE_INTERFACE_MIGRATION 489 spice_server_migrate_switch(spice_server); 490 #else 491 spice_server_migrate_end(spice_server, true); 492 } else if (migration_has_failed(s)) { 493 spice_server_migrate_end(spice_server, false); 494 #endif 495 } 496 } 497 498 int qemu_spice_migrate_info(const char *hostname, int port, int tls_port, 499 const char *subject, 500 MonitorCompletion *cb, void *opaque) 501 { 502 int ret; 503 #ifdef SPICE_INTERFACE_MIGRATION 504 spice_migrate.connect_complete.cb = cb; 505 spice_migrate.connect_complete.opaque = opaque; 506 ret = spice_server_migrate_connect(spice_server, hostname, 507 port, tls_port, subject); 508 #else 509 ret = spice_server_migrate_info(spice_server, hostname, 510 port, tls_port, subject); 511 cb(opaque, NULL); 512 #endif 513 return ret; 514 } 515 516 static int add_channel(const char *name, const char *value, void *opaque) 517 { 518 int security = 0; 519 int rc; 520 521 if (strcmp(name, "tls-channel") == 0) { 522 int *tls_port = opaque; 523 if (!*tls_port) { 524 error_report("spice: tried to setup tls-channel" 525 " without specifying a TLS port"); 526 exit(1); 527 } 528 security = SPICE_CHANNEL_SECURITY_SSL; 529 } 530 if (strcmp(name, "plaintext-channel") == 0) { 531 security = SPICE_CHANNEL_SECURITY_NONE; 532 } 533 if (security == 0) { 534 return 0; 535 } 536 if (strcmp(value, "default") == 0) { 537 rc = spice_server_set_channel_security(spice_server, NULL, security); 538 } else { 539 rc = spice_server_set_channel_security(spice_server, value, security); 540 } 541 if (rc != 0) { 542 error_report("spice: failed to set channel security for %s", value); 543 exit(1); 544 } 545 return 0; 546 } 547 548 void qemu_spice_init(void) 549 { 550 QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head); 551 const char *password, *str, *x509_dir, *addr, 552 *x509_key_password = NULL, 553 *x509_dh_file = NULL, 554 *tls_ciphers = NULL; 555 char *x509_key_file = NULL, 556 *x509_cert_file = NULL, 557 *x509_cacert_file = NULL; 558 int port, tls_port, len, addr_flags; 559 spice_image_compression_t compression; 560 spice_wan_compression_t wan_compr; 561 562 qemu_thread_get_self(&me); 563 564 if (!opts) { 565 return; 566 } 567 port = qemu_opt_get_number(opts, "port", 0); 568 tls_port = qemu_opt_get_number(opts, "tls-port", 0); 569 if (!port && !tls_port) { 570 error_report("neither port nor tls-port specified for spice"); 571 exit(1); 572 } 573 if (port < 0 || port > 65535) { 574 error_report("spice port is out of range"); 575 exit(1); 576 } 577 if (tls_port < 0 || tls_port > 65535) { 578 error_report("spice tls-port is out of range"); 579 exit(1); 580 } 581 password = qemu_opt_get(opts, "password"); 582 583 if (tls_port) { 584 x509_dir = qemu_opt_get(opts, "x509-dir"); 585 if (NULL == x509_dir) { 586 x509_dir = "."; 587 } 588 len = strlen(x509_dir) + 32; 589 590 str = qemu_opt_get(opts, "x509-key-file"); 591 if (str) { 592 x509_key_file = g_strdup(str); 593 } else { 594 x509_key_file = g_malloc(len); 595 snprintf(x509_key_file, len, "%s/%s", x509_dir, X509_SERVER_KEY_FILE); 596 } 597 598 str = qemu_opt_get(opts, "x509-cert-file"); 599 if (str) { 600 x509_cert_file = g_strdup(str); 601 } else { 602 x509_cert_file = g_malloc(len); 603 snprintf(x509_cert_file, len, "%s/%s", x509_dir, X509_SERVER_CERT_FILE); 604 } 605 606 str = qemu_opt_get(opts, "x509-cacert-file"); 607 if (str) { 608 x509_cacert_file = g_strdup(str); 609 } else { 610 x509_cacert_file = g_malloc(len); 611 snprintf(x509_cacert_file, len, "%s/%s", x509_dir, X509_CA_CERT_FILE); 612 } 613 614 x509_key_password = qemu_opt_get(opts, "x509-key-password"); 615 x509_dh_file = qemu_opt_get(opts, "x509-dh-file"); 616 tls_ciphers = qemu_opt_get(opts, "tls-ciphers"); 617 } 618 619 addr = qemu_opt_get(opts, "addr"); 620 addr_flags = 0; 621 if (qemu_opt_get_bool(opts, "ipv4", 0)) { 622 addr_flags |= SPICE_ADDR_FLAG_IPV4_ONLY; 623 } else if (qemu_opt_get_bool(opts, "ipv6", 0)) { 624 addr_flags |= SPICE_ADDR_FLAG_IPV6_ONLY; 625 } 626 627 spice_server = spice_server_new(); 628 spice_server_set_addr(spice_server, addr ? addr : "", addr_flags); 629 if (port) { 630 spice_server_set_port(spice_server, port); 631 } 632 if (tls_port) { 633 spice_server_set_tls(spice_server, tls_port, 634 x509_cacert_file, 635 x509_cert_file, 636 x509_key_file, 637 x509_key_password, 638 x509_dh_file, 639 tls_ciphers); 640 } 641 if (password) { 642 spice_server_set_ticket(spice_server, password, 0, 0, 0); 643 } 644 if (qemu_opt_get_bool(opts, "sasl", 0)) { 645 #if SPICE_SERVER_VERSION >= 0x000900 /* 0.9.0 */ 646 if (spice_server_set_sasl_appname(spice_server, "qemu") == -1 || 647 spice_server_set_sasl(spice_server, 1) == -1) { 648 error_report("spice: failed to enable sasl"); 649 exit(1); 650 } 651 #else 652 error_report("spice: sasl is not available (spice >= 0.9 required)"); 653 exit(1); 654 #endif 655 } 656 if (qemu_opt_get_bool(opts, "disable-ticketing", 0)) { 657 auth = "none"; 658 spice_server_set_noauth(spice_server); 659 } 660 661 if (qemu_opt_get_bool(opts, "disable-copy-paste", 0)) { 662 spice_server_set_agent_copypaste(spice_server, false); 663 } 664 665 compression = SPICE_IMAGE_COMPRESS_AUTO_GLZ; 666 str = qemu_opt_get(opts, "image-compression"); 667 if (str) { 668 compression = parse_compression(str); 669 } 670 spice_server_set_image_compression(spice_server, compression); 671 672 wan_compr = SPICE_WAN_COMPRESSION_AUTO; 673 str = qemu_opt_get(opts, "jpeg-wan-compression"); 674 if (str) { 675 wan_compr = parse_wan_compression(str); 676 } 677 spice_server_set_jpeg_compression(spice_server, wan_compr); 678 679 wan_compr = SPICE_WAN_COMPRESSION_AUTO; 680 str = qemu_opt_get(opts, "zlib-glz-wan-compression"); 681 if (str) { 682 wan_compr = parse_wan_compression(str); 683 } 684 spice_server_set_zlib_glz_compression(spice_server, wan_compr); 685 686 str = qemu_opt_get(opts, "streaming-video"); 687 if (str) { 688 int streaming_video = parse_stream_video(str); 689 spice_server_set_streaming_video(spice_server, streaming_video); 690 } 691 692 spice_server_set_agent_mouse 693 (spice_server, qemu_opt_get_bool(opts, "agent-mouse", 1)); 694 spice_server_set_playback_compression 695 (spice_server, qemu_opt_get_bool(opts, "playback-compression", 1)); 696 697 qemu_opt_foreach(opts, add_channel, &tls_port, 0); 698 699 #if SPICE_SERVER_VERSION >= 0x000a02 /* 0.10.2 */ 700 spice_server_set_name(spice_server, qemu_name); 701 spice_server_set_uuid(spice_server, qemu_uuid); 702 #endif 703 704 if (0 != spice_server_init(spice_server, &core_interface)) { 705 error_report("failed to initialize spice server"); 706 exit(1); 707 }; 708 using_spice = 1; 709 710 migration_state.notify = migration_state_notifier; 711 add_migration_state_change_notifier(&migration_state); 712 #ifdef SPICE_INTERFACE_MIGRATION 713 spice_migrate.sin.base.sif = &migrate_interface.base; 714 spice_migrate.connect_complete.cb = NULL; 715 qemu_spice_add_interface(&spice_migrate.sin.base); 716 #endif 717 718 qemu_spice_input_init(); 719 qemu_spice_audio_init(); 720 721 g_free(x509_key_file); 722 g_free(x509_cert_file); 723 g_free(x509_cacert_file); 724 } 725 726 int qemu_spice_add_interface(SpiceBaseInstance *sin) 727 { 728 if (!spice_server) { 729 if (QTAILQ_FIRST(&qemu_spice_opts.head) != NULL) { 730 error_report("Oops: spice configured but not active"); 731 exit(1); 732 } 733 /* 734 * Create a spice server instance. 735 * It does *not* listen on the network. 736 * It handles QXL local rendering only. 737 * 738 * With a command line like '-vnc :0 -vga qxl' you'll end up here. 739 */ 740 spice_server = spice_server_new(); 741 spice_server_init(spice_server, &core_interface); 742 } 743 return spice_server_add_interface(spice_server, sin); 744 } 745 746 static int qemu_spice_set_ticket(bool fail_if_conn, bool disconnect_if_conn) 747 { 748 time_t lifetime, now = time(NULL); 749 char *passwd; 750 751 if (now < auth_expires) { 752 passwd = auth_passwd; 753 lifetime = (auth_expires - now); 754 if (lifetime > INT_MAX) { 755 lifetime = INT_MAX; 756 } 757 } else { 758 passwd = NULL; 759 lifetime = 1; 760 } 761 return spice_server_set_ticket(spice_server, passwd, lifetime, 762 fail_if_conn, disconnect_if_conn); 763 } 764 765 int qemu_spice_set_passwd(const char *passwd, 766 bool fail_if_conn, bool disconnect_if_conn) 767 { 768 free(auth_passwd); 769 auth_passwd = strdup(passwd); 770 return qemu_spice_set_ticket(fail_if_conn, disconnect_if_conn); 771 } 772 773 int qemu_spice_set_pw_expire(time_t expires) 774 { 775 auth_expires = expires; 776 return qemu_spice_set_ticket(false, false); 777 } 778 779 int qemu_spice_display_add_client(int csock, int skipauth, int tls) 780 { 781 #if SPICE_SERVER_VERSION >= 0x000a01 782 if (tls) { 783 return spice_server_add_ssl_client(spice_server, csock, skipauth); 784 } else { 785 return spice_server_add_client(spice_server, csock, skipauth); 786 } 787 #else 788 return -1; 789 #endif 790 } 791 792 static void spice_register_config(void) 793 { 794 qemu_add_opts(&qemu_spice_opts); 795 } 796 machine_init(spice_register_config); 797