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 20 #include <netdb.h> 21 #include "sysemu/sysemu.h" 22 23 #include "qemu-common.h" 24 #include "ui/qemu-spice.h" 25 #include "qemu/error-report.h" 26 #include "qemu/thread.h" 27 #include "qemu/timer.h" 28 #include "qemu/queue.h" 29 #include "qemu-x509.h" 30 #include "qemu/sockets.h" 31 #include "qmp-commands.h" 32 #include "qapi/qmp/qint.h" 33 #include "qapi/qmp/qbool.h" 34 #include "qapi/qmp/qstring.h" 35 #include "qapi/qmp/qjson.h" 36 #include "qemu/notify.h" 37 #include "migration/migration.h" 38 #include "hw/hw.h" 39 #include "ui/spice-display.h" 40 #include "qapi-event.h" 41 42 /* core bits */ 43 44 static SpiceServer *spice_server; 45 static Notifier migration_state; 46 static const char *auth = "spice"; 47 static char *auth_passwd; 48 static time_t auth_expires = TIME_MAX; 49 static int spice_migration_completed; 50 static int spice_display_is_running; 51 static int spice_have_target_host; 52 int using_spice = 0; 53 54 static QemuThread me; 55 56 struct SpiceTimer { 57 QEMUTimer *timer; 58 QTAILQ_ENTRY(SpiceTimer) next; 59 }; 60 static QTAILQ_HEAD(, SpiceTimer) timers = QTAILQ_HEAD_INITIALIZER(timers); 61 62 static SpiceTimer *timer_add(SpiceTimerFunc func, void *opaque) 63 { 64 SpiceTimer *timer; 65 66 timer = g_malloc0(sizeof(*timer)); 67 timer->timer = timer_new_ms(QEMU_CLOCK_REALTIME, func, opaque); 68 QTAILQ_INSERT_TAIL(&timers, timer, next); 69 return timer; 70 } 71 72 static void timer_start(SpiceTimer *timer, uint32_t ms) 73 { 74 timer_mod(timer->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + ms); 75 } 76 77 static void timer_cancel(SpiceTimer *timer) 78 { 79 timer_del(timer->timer); 80 } 81 82 static void timer_remove(SpiceTimer *timer) 83 { 84 timer_del(timer->timer); 85 timer_free(timer->timer); 86 QTAILQ_REMOVE(&timers, timer, next); 87 g_free(timer); 88 } 89 90 struct SpiceWatch { 91 int fd; 92 int event_mask; 93 SpiceWatchFunc func; 94 void *opaque; 95 QTAILQ_ENTRY(SpiceWatch) next; 96 }; 97 static QTAILQ_HEAD(, SpiceWatch) watches = QTAILQ_HEAD_INITIALIZER(watches); 98 99 static void watch_read(void *opaque) 100 { 101 SpiceWatch *watch = opaque; 102 watch->func(watch->fd, SPICE_WATCH_EVENT_READ, watch->opaque); 103 } 104 105 static void watch_write(void *opaque) 106 { 107 SpiceWatch *watch = opaque; 108 watch->func(watch->fd, SPICE_WATCH_EVENT_WRITE, watch->opaque); 109 } 110 111 static void watch_update_mask(SpiceWatch *watch, int event_mask) 112 { 113 IOHandler *on_read = NULL; 114 IOHandler *on_write = NULL; 115 116 watch->event_mask = event_mask; 117 if (watch->event_mask & SPICE_WATCH_EVENT_READ) { 118 on_read = watch_read; 119 } 120 if (watch->event_mask & SPICE_WATCH_EVENT_WRITE) { 121 on_write = watch_write; 122 } 123 qemu_set_fd_handler(watch->fd, on_read, on_write, watch); 124 } 125 126 static SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque) 127 { 128 SpiceWatch *watch; 129 130 watch = g_malloc0(sizeof(*watch)); 131 watch->fd = fd; 132 watch->func = func; 133 watch->opaque = opaque; 134 QTAILQ_INSERT_TAIL(&watches, watch, next); 135 136 watch_update_mask(watch, event_mask); 137 return watch; 138 } 139 140 static void watch_remove(SpiceWatch *watch) 141 { 142 qemu_set_fd_handler(watch->fd, NULL, NULL, NULL); 143 QTAILQ_REMOVE(&watches, watch, next); 144 g_free(watch); 145 } 146 147 typedef struct ChannelList ChannelList; 148 struct ChannelList { 149 SpiceChannelEventInfo *info; 150 QTAILQ_ENTRY(ChannelList) link; 151 }; 152 static QTAILQ_HEAD(, ChannelList) channel_list = QTAILQ_HEAD_INITIALIZER(channel_list); 153 154 static void channel_list_add(SpiceChannelEventInfo *info) 155 { 156 ChannelList *item; 157 158 item = g_malloc0(sizeof(*item)); 159 item->info = info; 160 QTAILQ_INSERT_TAIL(&channel_list, item, link); 161 } 162 163 static void channel_list_del(SpiceChannelEventInfo *info) 164 { 165 ChannelList *item; 166 167 QTAILQ_FOREACH(item, &channel_list, link) { 168 if (item->info != info) { 169 continue; 170 } 171 QTAILQ_REMOVE(&channel_list, item, link); 172 g_free(item); 173 return; 174 } 175 } 176 177 static void add_addr_info(SpiceBasicInfo *info, struct sockaddr *addr, int len) 178 { 179 char host[NI_MAXHOST], port[NI_MAXSERV]; 180 181 getnameinfo(addr, len, host, sizeof(host), port, sizeof(port), 182 NI_NUMERICHOST | NI_NUMERICSERV); 183 184 info->host = g_strdup(host); 185 info->port = g_strdup(port); 186 info->family = inet_netfamily(addr->sa_family); 187 } 188 189 static void add_channel_info(SpiceChannel *sc, SpiceChannelEventInfo *info) 190 { 191 int tls = info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS; 192 193 sc->connection_id = info->connection_id; 194 sc->channel_type = info->type; 195 sc->channel_id = info->id; 196 sc->tls = !!tls; 197 } 198 199 static void channel_event(int event, SpiceChannelEventInfo *info) 200 { 201 SpiceServerInfo *server = g_malloc0(sizeof(*server)); 202 SpiceChannel *client = g_malloc0(sizeof(*client)); 203 server->base = g_malloc0(sizeof(*server->base)); 204 client->base = g_malloc0(sizeof(*client->base)); 205 206 /* 207 * Spice server might have called us from spice worker thread 208 * context (happens on display channel disconnects). Spice should 209 * not do that. It isn't that easy to fix it in spice and even 210 * when it is fixed we still should cover the already released 211 * spice versions. So detect that we've been called from another 212 * thread and grab the iothread lock if so before calling qemu 213 * functions. 214 */ 215 bool need_lock = !qemu_thread_is_self(&me); 216 if (need_lock) { 217 qemu_mutex_lock_iothread(); 218 } 219 220 if (info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT) { 221 add_addr_info(client->base, (struct sockaddr *)&info->paddr_ext, 222 info->plen_ext); 223 add_addr_info(server->base, (struct sockaddr *)&info->laddr_ext, 224 info->llen_ext); 225 } else { 226 error_report("spice: %s, extended address is expected", 227 __func__); 228 } 229 230 switch (event) { 231 case SPICE_CHANNEL_EVENT_CONNECTED: 232 qapi_event_send_spice_connected(server->base, client->base, &error_abort); 233 break; 234 case SPICE_CHANNEL_EVENT_INITIALIZED: 235 if (auth) { 236 server->has_auth = true; 237 server->auth = g_strdup(auth); 238 } 239 add_channel_info(client, info); 240 channel_list_add(info); 241 qapi_event_send_spice_initialized(server, client, &error_abort); 242 break; 243 case SPICE_CHANNEL_EVENT_DISCONNECTED: 244 channel_list_del(info); 245 qapi_event_send_spice_disconnected(server->base, client->base, &error_abort); 246 break; 247 default: 248 break; 249 } 250 251 if (need_lock) { 252 qemu_mutex_unlock_iothread(); 253 } 254 255 qapi_free_SpiceServerInfo(server); 256 qapi_free_SpiceChannel(client); 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 static void migrate_connect_complete_cb(SpiceMigrateInstance *sin); 278 static void migrate_end_complete_cb(SpiceMigrateInstance *sin); 279 280 static const SpiceMigrateInterface migrate_interface = { 281 .base.type = SPICE_INTERFACE_MIGRATION, 282 .base.description = "migration", 283 .base.major_version = SPICE_INTERFACE_MIGRATION_MAJOR, 284 .base.minor_version = SPICE_INTERFACE_MIGRATION_MINOR, 285 .migrate_connect_complete = migrate_connect_complete_cb, 286 .migrate_end_complete = migrate_end_complete_cb, 287 }; 288 289 static SpiceMigrateInstance spice_migrate; 290 291 static void migrate_connect_complete_cb(SpiceMigrateInstance *sin) 292 { 293 /* nothing, but libspice-server expects this cb being present. */ 294 } 295 296 static void migrate_end_complete_cb(SpiceMigrateInstance *sin) 297 { 298 qapi_event_send_spice_migrate_completed(&error_abort); 299 spice_migration_completed = true; 300 } 301 302 /* config string parsing */ 303 304 static int name2enum(const char *string, const char *table[], int entries) 305 { 306 int i; 307 308 if (string) { 309 for (i = 0; i < entries; i++) { 310 if (!table[i]) { 311 continue; 312 } 313 if (strcmp(string, table[i]) != 0) { 314 continue; 315 } 316 return i; 317 } 318 } 319 return -1; 320 } 321 322 static int parse_name(const char *string, const char *optname, 323 const char *table[], int entries) 324 { 325 int value = name2enum(string, table, entries); 326 327 if (value != -1) { 328 return value; 329 } 330 error_report("spice: invalid %s: %s", optname, string); 331 exit(1); 332 } 333 334 static const char *stream_video_names[] = { 335 [ SPICE_STREAM_VIDEO_OFF ] = "off", 336 [ SPICE_STREAM_VIDEO_ALL ] = "all", 337 [ SPICE_STREAM_VIDEO_FILTER ] = "filter", 338 }; 339 #define parse_stream_video(_name) \ 340 parse_name(_name, "stream video control", \ 341 stream_video_names, ARRAY_SIZE(stream_video_names)) 342 343 static const char *compression_names[] = { 344 [ SPICE_IMAGE_COMPRESS_OFF ] = "off", 345 [ SPICE_IMAGE_COMPRESS_AUTO_GLZ ] = "auto_glz", 346 [ SPICE_IMAGE_COMPRESS_AUTO_LZ ] = "auto_lz", 347 [ SPICE_IMAGE_COMPRESS_QUIC ] = "quic", 348 [ SPICE_IMAGE_COMPRESS_GLZ ] = "glz", 349 [ SPICE_IMAGE_COMPRESS_LZ ] = "lz", 350 }; 351 #define parse_compression(_name) \ 352 parse_name(_name, "image compression", \ 353 compression_names, ARRAY_SIZE(compression_names)) 354 355 static const char *wan_compression_names[] = { 356 [ SPICE_WAN_COMPRESSION_AUTO ] = "auto", 357 [ SPICE_WAN_COMPRESSION_NEVER ] = "never", 358 [ SPICE_WAN_COMPRESSION_ALWAYS ] = "always", 359 }; 360 #define parse_wan_compression(_name) \ 361 parse_name(_name, "wan compression", \ 362 wan_compression_names, ARRAY_SIZE(wan_compression_names)) 363 364 /* functions for the rest of qemu */ 365 366 static SpiceChannelList *qmp_query_spice_channels(void) 367 { 368 SpiceChannelList *cur_item = NULL, *head = NULL; 369 ChannelList *item; 370 371 QTAILQ_FOREACH(item, &channel_list, link) { 372 SpiceChannelList *chan; 373 char host[NI_MAXHOST], port[NI_MAXSERV]; 374 struct sockaddr *paddr; 375 socklen_t plen; 376 377 assert(item->info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT); 378 379 chan = g_malloc0(sizeof(*chan)); 380 chan->value = g_malloc0(sizeof(*chan->value)); 381 chan->value->base = g_malloc0(sizeof(*chan->value->base)); 382 383 paddr = (struct sockaddr *)&item->info->paddr_ext; 384 plen = item->info->plen_ext; 385 getnameinfo(paddr, plen, 386 host, sizeof(host), port, sizeof(port), 387 NI_NUMERICHOST | NI_NUMERICSERV); 388 chan->value->base->host = g_strdup(host); 389 chan->value->base->port = g_strdup(port); 390 chan->value->base->family = inet_netfamily(paddr->sa_family); 391 392 chan->value->connection_id = item->info->connection_id; 393 chan->value->channel_type = item->info->type; 394 chan->value->channel_id = item->info->id; 395 chan->value->tls = item->info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS; 396 397 /* XXX: waiting for the qapi to support GSList */ 398 if (!cur_item) { 399 head = cur_item = chan; 400 } else { 401 cur_item->next = chan; 402 cur_item = chan; 403 } 404 } 405 406 return head; 407 } 408 409 static QemuOptsList qemu_spice_opts = { 410 .name = "spice", 411 .head = QTAILQ_HEAD_INITIALIZER(qemu_spice_opts.head), 412 .desc = { 413 { 414 .name = "port", 415 .type = QEMU_OPT_NUMBER, 416 },{ 417 .name = "tls-port", 418 .type = QEMU_OPT_NUMBER, 419 },{ 420 .name = "addr", 421 .type = QEMU_OPT_STRING, 422 },{ 423 .name = "ipv4", 424 .type = QEMU_OPT_BOOL, 425 },{ 426 .name = "ipv6", 427 .type = QEMU_OPT_BOOL, 428 #ifdef SPICE_ADDR_FLAG_UNIX_ONLY 429 },{ 430 .name = "unix", 431 .type = QEMU_OPT_BOOL, 432 #endif 433 },{ 434 .name = "password", 435 .type = QEMU_OPT_STRING, 436 },{ 437 .name = "disable-ticketing", 438 .type = QEMU_OPT_BOOL, 439 },{ 440 .name = "disable-copy-paste", 441 .type = QEMU_OPT_BOOL, 442 },{ 443 .name = "disable-agent-file-xfer", 444 .type = QEMU_OPT_BOOL, 445 },{ 446 .name = "sasl", 447 .type = QEMU_OPT_BOOL, 448 },{ 449 .name = "x509-dir", 450 .type = QEMU_OPT_STRING, 451 },{ 452 .name = "x509-key-file", 453 .type = QEMU_OPT_STRING, 454 },{ 455 .name = "x509-key-password", 456 .type = QEMU_OPT_STRING, 457 },{ 458 .name = "x509-cert-file", 459 .type = QEMU_OPT_STRING, 460 },{ 461 .name = "x509-cacert-file", 462 .type = QEMU_OPT_STRING, 463 },{ 464 .name = "x509-dh-key-file", 465 .type = QEMU_OPT_STRING, 466 },{ 467 .name = "tls-ciphers", 468 .type = QEMU_OPT_STRING, 469 },{ 470 .name = "tls-channel", 471 .type = QEMU_OPT_STRING, 472 },{ 473 .name = "plaintext-channel", 474 .type = QEMU_OPT_STRING, 475 },{ 476 .name = "image-compression", 477 .type = QEMU_OPT_STRING, 478 },{ 479 .name = "jpeg-wan-compression", 480 .type = QEMU_OPT_STRING, 481 },{ 482 .name = "zlib-glz-wan-compression", 483 .type = QEMU_OPT_STRING, 484 },{ 485 .name = "streaming-video", 486 .type = QEMU_OPT_STRING, 487 },{ 488 .name = "agent-mouse", 489 .type = QEMU_OPT_BOOL, 490 },{ 491 .name = "playback-compression", 492 .type = QEMU_OPT_BOOL, 493 }, { 494 .name = "seamless-migration", 495 .type = QEMU_OPT_BOOL, 496 }, 497 { /* end of list */ } 498 }, 499 }; 500 501 SpiceInfo *qmp_query_spice(Error **errp) 502 { 503 QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head); 504 int port, tls_port; 505 const char *addr; 506 SpiceInfo *info; 507 unsigned int major; 508 unsigned int minor; 509 unsigned int micro; 510 511 info = g_malloc0(sizeof(*info)); 512 513 if (!spice_server || !opts) { 514 info->enabled = false; 515 return info; 516 } 517 518 info->enabled = true; 519 info->migrated = spice_migration_completed; 520 521 addr = qemu_opt_get(opts, "addr"); 522 port = qemu_opt_get_number(opts, "port", 0); 523 tls_port = qemu_opt_get_number(opts, "tls-port", 0); 524 525 info->has_auth = true; 526 info->auth = g_strdup(auth); 527 528 info->has_host = true; 529 info->host = g_strdup(addr ? addr : "*"); 530 531 info->has_compiled_version = true; 532 major = (SPICE_SERVER_VERSION & 0xff0000) >> 16; 533 minor = (SPICE_SERVER_VERSION & 0xff00) >> 8; 534 micro = SPICE_SERVER_VERSION & 0xff; 535 info->compiled_version = g_strdup_printf("%d.%d.%d", major, minor, micro); 536 537 if (port) { 538 info->has_port = true; 539 info->port = port; 540 } 541 if (tls_port) { 542 info->has_tls_port = true; 543 info->tls_port = tls_port; 544 } 545 546 info->mouse_mode = spice_server_is_server_mouse(spice_server) ? 547 SPICE_QUERY_MOUSE_MODE_SERVER : 548 SPICE_QUERY_MOUSE_MODE_CLIENT; 549 550 /* for compatibility with the original command */ 551 info->has_channels = true; 552 info->channels = qmp_query_spice_channels(); 553 554 return info; 555 } 556 557 static void migration_state_notifier(Notifier *notifier, void *data) 558 { 559 MigrationState *s = data; 560 561 if (!spice_have_target_host) { 562 return; 563 } 564 565 if (migration_in_setup(s)) { 566 spice_server_migrate_start(spice_server); 567 } else if (migration_has_finished(s)) { 568 spice_server_migrate_end(spice_server, true); 569 spice_have_target_host = false; 570 } else if (migration_has_failed(s)) { 571 spice_server_migrate_end(spice_server, false); 572 spice_have_target_host = false; 573 } 574 } 575 576 int qemu_spice_migrate_info(const char *hostname, int port, int tls_port, 577 const char *subject) 578 { 579 int ret; 580 581 ret = spice_server_migrate_connect(spice_server, hostname, 582 port, tls_port, subject); 583 spice_have_target_host = true; 584 return ret; 585 } 586 587 static int add_channel(void *opaque, const char *name, const char *value, 588 Error **errp) 589 { 590 int security = 0; 591 int rc; 592 593 if (strcmp(name, "tls-channel") == 0) { 594 int *tls_port = opaque; 595 if (!*tls_port) { 596 error_report("spice: tried to setup tls-channel" 597 " without specifying a TLS port"); 598 exit(1); 599 } 600 security = SPICE_CHANNEL_SECURITY_SSL; 601 } 602 if (strcmp(name, "plaintext-channel") == 0) { 603 security = SPICE_CHANNEL_SECURITY_NONE; 604 } 605 if (security == 0) { 606 return 0; 607 } 608 if (strcmp(value, "default") == 0) { 609 rc = spice_server_set_channel_security(spice_server, NULL, security); 610 } else { 611 rc = spice_server_set_channel_security(spice_server, value, security); 612 } 613 if (rc != 0) { 614 error_report("spice: failed to set channel security for %s", value); 615 exit(1); 616 } 617 return 0; 618 } 619 620 static void vm_change_state_handler(void *opaque, int running, 621 RunState state) 622 { 623 if (running) { 624 qemu_spice_display_start(); 625 } else { 626 qemu_spice_display_stop(); 627 } 628 } 629 630 void qemu_spice_init(void) 631 { 632 QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head); 633 const char *password, *str, *x509_dir, *addr, 634 *x509_key_password = NULL, 635 *x509_dh_file = NULL, 636 *tls_ciphers = NULL; 637 char *x509_key_file = NULL, 638 *x509_cert_file = NULL, 639 *x509_cacert_file = NULL; 640 int port, tls_port, addr_flags; 641 spice_image_compression_t compression; 642 spice_wan_compression_t wan_compr; 643 bool seamless_migration; 644 645 qemu_thread_get_self(&me); 646 647 if (!opts) { 648 return; 649 } 650 port = qemu_opt_get_number(opts, "port", 0); 651 tls_port = qemu_opt_get_number(opts, "tls-port", 0); 652 if (port < 0 || port > 65535) { 653 error_report("spice port is out of range"); 654 exit(1); 655 } 656 if (tls_port < 0 || tls_port > 65535) { 657 error_report("spice tls-port is out of range"); 658 exit(1); 659 } 660 password = qemu_opt_get(opts, "password"); 661 662 if (tls_port) { 663 x509_dir = qemu_opt_get(opts, "x509-dir"); 664 if (!x509_dir) { 665 x509_dir = "."; 666 } 667 668 str = qemu_opt_get(opts, "x509-key-file"); 669 if (str) { 670 x509_key_file = g_strdup(str); 671 } else { 672 x509_key_file = g_strdup_printf("%s/%s", x509_dir, 673 X509_SERVER_KEY_FILE); 674 } 675 676 str = qemu_opt_get(opts, "x509-cert-file"); 677 if (str) { 678 x509_cert_file = g_strdup(str); 679 } else { 680 x509_cert_file = g_strdup_printf("%s/%s", x509_dir, 681 X509_SERVER_CERT_FILE); 682 } 683 684 str = qemu_opt_get(opts, "x509-cacert-file"); 685 if (str) { 686 x509_cacert_file = g_strdup(str); 687 } else { 688 x509_cacert_file = g_strdup_printf("%s/%s", x509_dir, 689 X509_CA_CERT_FILE); 690 } 691 692 x509_key_password = qemu_opt_get(opts, "x509-key-password"); 693 x509_dh_file = qemu_opt_get(opts, "x509-dh-key-file"); 694 tls_ciphers = qemu_opt_get(opts, "tls-ciphers"); 695 } 696 697 addr = qemu_opt_get(opts, "addr"); 698 addr_flags = 0; 699 if (qemu_opt_get_bool(opts, "ipv4", 0)) { 700 addr_flags |= SPICE_ADDR_FLAG_IPV4_ONLY; 701 } else if (qemu_opt_get_bool(opts, "ipv6", 0)) { 702 addr_flags |= SPICE_ADDR_FLAG_IPV6_ONLY; 703 #ifdef SPICE_ADDR_FLAG_UNIX_ONLY 704 } else if (qemu_opt_get_bool(opts, "unix", 0)) { 705 addr_flags |= SPICE_ADDR_FLAG_UNIX_ONLY; 706 #endif 707 } 708 709 spice_server = spice_server_new(); 710 spice_server_set_addr(spice_server, addr ? addr : "", addr_flags); 711 if (port) { 712 spice_server_set_port(spice_server, port); 713 } 714 if (tls_port) { 715 spice_server_set_tls(spice_server, tls_port, 716 x509_cacert_file, 717 x509_cert_file, 718 x509_key_file, 719 x509_key_password, 720 x509_dh_file, 721 tls_ciphers); 722 } 723 if (password) { 724 qemu_spice_set_passwd(password, false, false); 725 } 726 if (qemu_opt_get_bool(opts, "sasl", 0)) { 727 if (spice_server_set_sasl_appname(spice_server, "qemu") == -1 || 728 spice_server_set_sasl(spice_server, 1) == -1) { 729 error_report("spice: failed to enable sasl"); 730 exit(1); 731 } 732 auth = "sasl"; 733 } 734 if (qemu_opt_get_bool(opts, "disable-ticketing", 0)) { 735 auth = "none"; 736 spice_server_set_noauth(spice_server); 737 } 738 739 if (qemu_opt_get_bool(opts, "disable-copy-paste", 0)) { 740 spice_server_set_agent_copypaste(spice_server, false); 741 } 742 743 if (qemu_opt_get_bool(opts, "disable-agent-file-xfer", 0)) { 744 #if SPICE_SERVER_VERSION >= 0x000c04 745 spice_server_set_agent_file_xfer(spice_server, false); 746 #else 747 error_report("this qemu build does not support the " 748 "\"disable-agent-file-xfer\" option"); 749 exit(1); 750 #endif 751 } 752 753 compression = SPICE_IMAGE_COMPRESS_AUTO_GLZ; 754 str = qemu_opt_get(opts, "image-compression"); 755 if (str) { 756 compression = parse_compression(str); 757 } 758 spice_server_set_image_compression(spice_server, compression); 759 760 wan_compr = SPICE_WAN_COMPRESSION_AUTO; 761 str = qemu_opt_get(opts, "jpeg-wan-compression"); 762 if (str) { 763 wan_compr = parse_wan_compression(str); 764 } 765 spice_server_set_jpeg_compression(spice_server, wan_compr); 766 767 wan_compr = SPICE_WAN_COMPRESSION_AUTO; 768 str = qemu_opt_get(opts, "zlib-glz-wan-compression"); 769 if (str) { 770 wan_compr = parse_wan_compression(str); 771 } 772 spice_server_set_zlib_glz_compression(spice_server, wan_compr); 773 774 str = qemu_opt_get(opts, "streaming-video"); 775 if (str) { 776 int streaming_video = parse_stream_video(str); 777 spice_server_set_streaming_video(spice_server, streaming_video); 778 } else { 779 spice_server_set_streaming_video(spice_server, SPICE_STREAM_VIDEO_OFF); 780 } 781 782 spice_server_set_agent_mouse 783 (spice_server, qemu_opt_get_bool(opts, "agent-mouse", 1)); 784 spice_server_set_playback_compression 785 (spice_server, qemu_opt_get_bool(opts, "playback-compression", 1)); 786 787 qemu_opt_foreach(opts, add_channel, &tls_port, NULL); 788 789 spice_server_set_name(spice_server, qemu_name); 790 spice_server_set_uuid(spice_server, qemu_uuid); 791 792 seamless_migration = qemu_opt_get_bool(opts, "seamless-migration", 0); 793 spice_server_set_seamless_migration(spice_server, seamless_migration); 794 if (spice_server_init(spice_server, &core_interface) != 0) { 795 error_report("failed to initialize spice server"); 796 exit(1); 797 }; 798 using_spice = 1; 799 800 migration_state.notify = migration_state_notifier; 801 add_migration_state_change_notifier(&migration_state); 802 spice_migrate.base.sif = &migrate_interface.base; 803 qemu_spice_add_interface(&spice_migrate.base); 804 805 qemu_spice_input_init(); 806 qemu_spice_audio_init(); 807 808 qemu_add_vm_change_state_handler(vm_change_state_handler, NULL); 809 qemu_spice_display_stop(); 810 811 g_free(x509_key_file); 812 g_free(x509_cert_file); 813 g_free(x509_cacert_file); 814 815 #if SPICE_SERVER_VERSION >= 0x000c02 816 qemu_spice_register_ports(); 817 #endif 818 } 819 820 int qemu_spice_add_interface(SpiceBaseInstance *sin) 821 { 822 if (!spice_server) { 823 if (QTAILQ_FIRST(&qemu_spice_opts.head) != NULL) { 824 error_report("Oops: spice configured but not active"); 825 exit(1); 826 } 827 /* 828 * Create a spice server instance. 829 * It does *not* listen on the network. 830 * It handles QXL local rendering only. 831 * 832 * With a command line like '-vnc :0 -vga qxl' you'll end up here. 833 */ 834 spice_server = spice_server_new(); 835 spice_server_set_sasl_appname(spice_server, "qemu"); 836 spice_server_init(spice_server, &core_interface); 837 qemu_add_vm_change_state_handler(vm_change_state_handler, NULL); 838 } 839 840 return spice_server_add_interface(spice_server, sin); 841 } 842 843 static GSList *spice_consoles; 844 845 bool qemu_spice_have_display_interface(QemuConsole *con) 846 { 847 if (g_slist_find(spice_consoles, con)) { 848 return true; 849 } 850 return false; 851 } 852 853 int qemu_spice_add_display_interface(QXLInstance *qxlin, QemuConsole *con) 854 { 855 if (g_slist_find(spice_consoles, con)) { 856 return -1; 857 } 858 qxlin->id = qemu_console_get_index(con); 859 spice_consoles = g_slist_append(spice_consoles, con); 860 return qemu_spice_add_interface(&qxlin->base); 861 } 862 863 static int qemu_spice_set_ticket(bool fail_if_conn, bool disconnect_if_conn) 864 { 865 time_t lifetime, now = time(NULL); 866 char *passwd; 867 868 if (now < auth_expires) { 869 passwd = auth_passwd; 870 lifetime = (auth_expires - now); 871 if (lifetime > INT_MAX) { 872 lifetime = INT_MAX; 873 } 874 } else { 875 passwd = NULL; 876 lifetime = 1; 877 } 878 return spice_server_set_ticket(spice_server, passwd, lifetime, 879 fail_if_conn, disconnect_if_conn); 880 } 881 882 int qemu_spice_set_passwd(const char *passwd, 883 bool fail_if_conn, bool disconnect_if_conn) 884 { 885 if (strcmp(auth, "spice") != 0) { 886 return -1; 887 } 888 889 g_free(auth_passwd); 890 auth_passwd = g_strdup(passwd); 891 return qemu_spice_set_ticket(fail_if_conn, disconnect_if_conn); 892 } 893 894 int qemu_spice_set_pw_expire(time_t expires) 895 { 896 auth_expires = expires; 897 return qemu_spice_set_ticket(false, false); 898 } 899 900 int qemu_spice_display_add_client(int csock, int skipauth, int tls) 901 { 902 if (tls) { 903 return spice_server_add_ssl_client(spice_server, csock, skipauth); 904 } else { 905 return spice_server_add_client(spice_server, csock, skipauth); 906 } 907 } 908 909 void qemu_spice_display_start(void) 910 { 911 spice_display_is_running = true; 912 spice_server_vm_start(spice_server); 913 } 914 915 void qemu_spice_display_stop(void) 916 { 917 spice_server_vm_stop(spice_server); 918 spice_display_is_running = false; 919 } 920 921 int qemu_spice_display_is_running(SimpleSpiceDisplay *ssd) 922 { 923 return spice_display_is_running; 924 } 925 926 static void spice_register_config(void) 927 { 928 qemu_add_opts(&qemu_spice_opts); 929 } 930 machine_init(spice_register_config); 931