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/sysemu.h" 23 24 #include "qemu-common.h" 25 #include "ui/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/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 "monitor/monitor.h" 39 #include "hw/hw.h" 40 #include "ui/spice-display.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(QDict *dict, struct sockaddr *addr, int len) 178 { 179 char host[NI_MAXHOST], port[NI_MAXSERV]; 180 const char *family; 181 182 getnameinfo(addr, len, host, sizeof(host), port, sizeof(port), 183 NI_NUMERICHOST | NI_NUMERICSERV); 184 family = inet_strfamily(addr->sa_family); 185 186 qdict_put(dict, "host", qstring_from_str(host)); 187 qdict_put(dict, "port", qstring_from_str(port)); 188 qdict_put(dict, "family", qstring_from_str(family)); 189 } 190 191 static void add_channel_info(QDict *dict, SpiceChannelEventInfo *info) 192 { 193 int tls = info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS; 194 195 qdict_put(dict, "connection-id", qint_from_int(info->connection_id)); 196 qdict_put(dict, "channel-type", qint_from_int(info->type)); 197 qdict_put(dict, "channel-id", qint_from_int(info->id)); 198 qdict_put(dict, "tls", qbool_from_int(tls)); 199 } 200 201 static void channel_event(int event, SpiceChannelEventInfo *info) 202 { 203 static const int qevent[] = { 204 [ SPICE_CHANNEL_EVENT_CONNECTED ] = QEVENT_SPICE_CONNECTED, 205 [ SPICE_CHANNEL_EVENT_INITIALIZED ] = QEVENT_SPICE_INITIALIZED, 206 [ SPICE_CHANNEL_EVENT_DISCONNECTED ] = QEVENT_SPICE_DISCONNECTED, 207 }; 208 QDict *server, *client; 209 QObject *data; 210 211 /* 212 * Spice server might have called us from spice worker thread 213 * context (happens on display channel disconnects). Spice should 214 * not do that. It isn't that easy to fix it in spice and even 215 * when it is fixed we still should cover the already released 216 * spice versions. So detect that we've been called from another 217 * thread and grab the iothread lock if so before calling qemu 218 * functions. 219 */ 220 bool need_lock = !qemu_thread_is_self(&me); 221 if (need_lock) { 222 qemu_mutex_lock_iothread(); 223 } 224 225 client = qdict_new(); 226 server = qdict_new(); 227 228 if (info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT) { 229 add_addr_info(client, (struct sockaddr *)&info->paddr_ext, 230 info->plen_ext); 231 add_addr_info(server, (struct sockaddr *)&info->laddr_ext, 232 info->llen_ext); 233 } else { 234 error_report("spice: %s, extended address is expected", 235 __func__); 236 } 237 238 if (event == SPICE_CHANNEL_EVENT_INITIALIZED) { 239 qdict_put(server, "auth", qstring_from_str(auth)); 240 add_channel_info(client, info); 241 channel_list_add(info); 242 } 243 if (event == SPICE_CHANNEL_EVENT_DISCONNECTED) { 244 channel_list_del(info); 245 } 246 247 data = qobject_from_jsonf("{ 'client': %p, 'server': %p }", 248 QOBJECT(client), QOBJECT(server)); 249 monitor_protocol_event(qevent[event], data); 250 qobject_decref(data); 251 252 if (need_lock) { 253 qemu_mutex_unlock_iothread(); 254 } 255 } 256 257 static SpiceCoreInterface core_interface = { 258 .base.type = SPICE_INTERFACE_CORE, 259 .base.description = "qemu core services", 260 .base.major_version = SPICE_INTERFACE_CORE_MAJOR, 261 .base.minor_version = SPICE_INTERFACE_CORE_MINOR, 262 263 .timer_add = timer_add, 264 .timer_start = timer_start, 265 .timer_cancel = timer_cancel, 266 .timer_remove = timer_remove, 267 268 .watch_add = watch_add, 269 .watch_update_mask = watch_update_mask, 270 .watch_remove = watch_remove, 271 272 .channel_event = channel_event, 273 }; 274 275 typedef struct SpiceMigration { 276 SpiceMigrateInstance sin; 277 struct { 278 MonitorCompletion *cb; 279 void *opaque; 280 } connect_complete; 281 } SpiceMigration; 282 283 static void migrate_connect_complete_cb(SpiceMigrateInstance *sin); 284 static void migrate_end_complete_cb(SpiceMigrateInstance *sin); 285 286 static const SpiceMigrateInterface migrate_interface = { 287 .base.type = SPICE_INTERFACE_MIGRATION, 288 .base.description = "migration", 289 .base.major_version = SPICE_INTERFACE_MIGRATION_MAJOR, 290 .base.minor_version = SPICE_INTERFACE_MIGRATION_MINOR, 291 .migrate_connect_complete = migrate_connect_complete_cb, 292 .migrate_end_complete = migrate_end_complete_cb, 293 }; 294 295 static SpiceMigration spice_migrate; 296 297 static void migrate_connect_complete_cb(SpiceMigrateInstance *sin) 298 { 299 SpiceMigration *sm = container_of(sin, SpiceMigration, sin); 300 if (sm->connect_complete.cb) { 301 sm->connect_complete.cb(sm->connect_complete.opaque, NULL); 302 } 303 sm->connect_complete.cb = NULL; 304 } 305 306 static void migrate_end_complete_cb(SpiceMigrateInstance *sin) 307 { 308 monitor_protocol_event(QEVENT_SPICE_MIGRATE_COMPLETED, NULL); 309 spice_migration_completed = true; 310 } 311 312 /* config string parsing */ 313 314 static int name2enum(const char *string, const char *table[], int entries) 315 { 316 int i; 317 318 if (string) { 319 for (i = 0; i < entries; i++) { 320 if (!table[i]) { 321 continue; 322 } 323 if (strcmp(string, table[i]) != 0) { 324 continue; 325 } 326 return i; 327 } 328 } 329 return -1; 330 } 331 332 static int parse_name(const char *string, const char *optname, 333 const char *table[], int entries) 334 { 335 int value = name2enum(string, table, entries); 336 337 if (value != -1) { 338 return value; 339 } 340 error_report("spice: invalid %s: %s", optname, string); 341 exit(1); 342 } 343 344 static const char *stream_video_names[] = { 345 [ SPICE_STREAM_VIDEO_OFF ] = "off", 346 [ SPICE_STREAM_VIDEO_ALL ] = "all", 347 [ SPICE_STREAM_VIDEO_FILTER ] = "filter", 348 }; 349 #define parse_stream_video(_name) \ 350 parse_name(_name, "stream video control", \ 351 stream_video_names, ARRAY_SIZE(stream_video_names)) 352 353 static const char *compression_names[] = { 354 [ SPICE_IMAGE_COMPRESS_OFF ] = "off", 355 [ SPICE_IMAGE_COMPRESS_AUTO_GLZ ] = "auto_glz", 356 [ SPICE_IMAGE_COMPRESS_AUTO_LZ ] = "auto_lz", 357 [ SPICE_IMAGE_COMPRESS_QUIC ] = "quic", 358 [ SPICE_IMAGE_COMPRESS_GLZ ] = "glz", 359 [ SPICE_IMAGE_COMPRESS_LZ ] = "lz", 360 }; 361 #define parse_compression(_name) \ 362 parse_name(_name, "image compression", \ 363 compression_names, ARRAY_SIZE(compression_names)) 364 365 static const char *wan_compression_names[] = { 366 [ SPICE_WAN_COMPRESSION_AUTO ] = "auto", 367 [ SPICE_WAN_COMPRESSION_NEVER ] = "never", 368 [ SPICE_WAN_COMPRESSION_ALWAYS ] = "always", 369 }; 370 #define parse_wan_compression(_name) \ 371 parse_name(_name, "wan compression", \ 372 wan_compression_names, ARRAY_SIZE(wan_compression_names)) 373 374 /* functions for the rest of qemu */ 375 376 static SpiceChannelList *qmp_query_spice_channels(void) 377 { 378 SpiceChannelList *cur_item = NULL, *head = NULL; 379 ChannelList *item; 380 381 QTAILQ_FOREACH(item, &channel_list, link) { 382 SpiceChannelList *chan; 383 char host[NI_MAXHOST], port[NI_MAXSERV]; 384 struct sockaddr *paddr; 385 socklen_t plen; 386 387 if (!(item->info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT)) { 388 error_report("invalid channel event"); 389 return NULL; 390 } 391 392 chan = g_malloc0(sizeof(*chan)); 393 chan->value = g_malloc0(sizeof(*chan->value)); 394 chan->value->base = g_malloc0(sizeof(*chan->value->base)); 395 396 paddr = (struct sockaddr *)&item->info->paddr_ext; 397 plen = item->info->plen_ext; 398 getnameinfo(paddr, plen, 399 host, sizeof(host), port, sizeof(port), 400 NI_NUMERICHOST | NI_NUMERICSERV); 401 chan->value->base->host = g_strdup(host); 402 chan->value->base->port = g_strdup(port); 403 chan->value->base->family = inet_netfamily(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 static QemuOptsList qemu_spice_opts = { 423 .name = "spice", 424 .head = QTAILQ_HEAD_INITIALIZER(qemu_spice_opts.head), 425 .desc = { 426 { 427 .name = "port", 428 .type = QEMU_OPT_NUMBER, 429 },{ 430 .name = "tls-port", 431 .type = QEMU_OPT_NUMBER, 432 },{ 433 .name = "addr", 434 .type = QEMU_OPT_STRING, 435 },{ 436 .name = "ipv4", 437 .type = QEMU_OPT_BOOL, 438 },{ 439 .name = "ipv6", 440 .type = QEMU_OPT_BOOL, 441 },{ 442 .name = "password", 443 .type = QEMU_OPT_STRING, 444 },{ 445 .name = "disable-ticketing", 446 .type = QEMU_OPT_BOOL, 447 },{ 448 .name = "disable-copy-paste", 449 .type = QEMU_OPT_BOOL, 450 },{ 451 .name = "disable-agent-file-xfer", 452 .type = QEMU_OPT_BOOL, 453 },{ 454 .name = "sasl", 455 .type = QEMU_OPT_BOOL, 456 },{ 457 .name = "x509-dir", 458 .type = QEMU_OPT_STRING, 459 },{ 460 .name = "x509-key-file", 461 .type = QEMU_OPT_STRING, 462 },{ 463 .name = "x509-key-password", 464 .type = QEMU_OPT_STRING, 465 },{ 466 .name = "x509-cert-file", 467 .type = QEMU_OPT_STRING, 468 },{ 469 .name = "x509-cacert-file", 470 .type = QEMU_OPT_STRING, 471 },{ 472 .name = "x509-dh-key-file", 473 .type = QEMU_OPT_STRING, 474 },{ 475 .name = "tls-ciphers", 476 .type = QEMU_OPT_STRING, 477 },{ 478 .name = "tls-channel", 479 .type = QEMU_OPT_STRING, 480 },{ 481 .name = "plaintext-channel", 482 .type = QEMU_OPT_STRING, 483 },{ 484 .name = "image-compression", 485 .type = QEMU_OPT_STRING, 486 },{ 487 .name = "jpeg-wan-compression", 488 .type = QEMU_OPT_STRING, 489 },{ 490 .name = "zlib-glz-wan-compression", 491 .type = QEMU_OPT_STRING, 492 },{ 493 .name = "streaming-video", 494 .type = QEMU_OPT_STRING, 495 },{ 496 .name = "agent-mouse", 497 .type = QEMU_OPT_BOOL, 498 },{ 499 .name = "playback-compression", 500 .type = QEMU_OPT_BOOL, 501 }, { 502 .name = "seamless-migration", 503 .type = QEMU_OPT_BOOL, 504 }, 505 { /* end of list */ } 506 }, 507 }; 508 509 SpiceInfo *qmp_query_spice(Error **errp) 510 { 511 QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head); 512 int port, tls_port; 513 const char *addr; 514 SpiceInfo *info; 515 unsigned int major; 516 unsigned int minor; 517 unsigned int micro; 518 519 info = g_malloc0(sizeof(*info)); 520 521 if (!spice_server || !opts) { 522 info->enabled = false; 523 return info; 524 } 525 526 info->enabled = true; 527 info->migrated = spice_migration_completed; 528 529 addr = qemu_opt_get(opts, "addr"); 530 port = qemu_opt_get_number(opts, "port", 0); 531 tls_port = qemu_opt_get_number(opts, "tls-port", 0); 532 533 info->has_auth = true; 534 info->auth = g_strdup(auth); 535 536 info->has_host = true; 537 info->host = g_strdup(addr ? addr : "*"); 538 539 info->has_compiled_version = true; 540 major = (SPICE_SERVER_VERSION & 0xff0000) >> 16; 541 minor = (SPICE_SERVER_VERSION & 0xff00) >> 8; 542 micro = SPICE_SERVER_VERSION & 0xff; 543 info->compiled_version = g_strdup_printf("%d.%d.%d", major, minor, micro); 544 545 if (port) { 546 info->has_port = true; 547 info->port = port; 548 } 549 if (tls_port) { 550 info->has_tls_port = true; 551 info->tls_port = tls_port; 552 } 553 554 info->mouse_mode = spice_server_is_server_mouse(spice_server) ? 555 SPICE_QUERY_MOUSE_MODE_SERVER : 556 SPICE_QUERY_MOUSE_MODE_CLIENT; 557 558 /* for compatibility with the original command */ 559 info->has_channels = true; 560 info->channels = qmp_query_spice_channels(); 561 562 return info; 563 } 564 565 static void migration_state_notifier(Notifier *notifier, void *data) 566 { 567 MigrationState *s = data; 568 569 if (!spice_have_target_host) { 570 return; 571 } 572 573 if (migration_in_setup(s)) { 574 spice_server_migrate_start(spice_server); 575 } else if (migration_has_finished(s)) { 576 spice_server_migrate_end(spice_server, true); 577 spice_have_target_host = false; 578 } else if (migration_has_failed(s)) { 579 spice_server_migrate_end(spice_server, false); 580 spice_have_target_host = false; 581 } 582 } 583 584 int qemu_spice_migrate_info(const char *hostname, int port, int tls_port, 585 const char *subject, 586 MonitorCompletion *cb, void *opaque) 587 { 588 int ret; 589 590 spice_migrate.connect_complete.cb = cb; 591 spice_migrate.connect_complete.opaque = opaque; 592 ret = spice_server_migrate_connect(spice_server, hostname, 593 port, tls_port, subject); 594 spice_have_target_host = true; 595 return ret; 596 } 597 598 static int add_channel(const char *name, const char *value, void *opaque) 599 { 600 int security = 0; 601 int rc; 602 603 if (strcmp(name, "tls-channel") == 0) { 604 int *tls_port = opaque; 605 if (!*tls_port) { 606 error_report("spice: tried to setup tls-channel" 607 " without specifying a TLS port"); 608 exit(1); 609 } 610 security = SPICE_CHANNEL_SECURITY_SSL; 611 } 612 if (strcmp(name, "plaintext-channel") == 0) { 613 security = SPICE_CHANNEL_SECURITY_NONE; 614 } 615 if (security == 0) { 616 return 0; 617 } 618 if (strcmp(value, "default") == 0) { 619 rc = spice_server_set_channel_security(spice_server, NULL, security); 620 } else { 621 rc = spice_server_set_channel_security(spice_server, value, security); 622 } 623 if (rc != 0) { 624 error_report("spice: failed to set channel security for %s", value); 625 exit(1); 626 } 627 return 0; 628 } 629 630 static void vm_change_state_handler(void *opaque, int running, 631 RunState state) 632 { 633 if (running) { 634 qemu_spice_display_start(); 635 } else { 636 qemu_spice_display_stop(); 637 } 638 } 639 640 void qemu_spice_init(void) 641 { 642 QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head); 643 const char *password, *str, *x509_dir, *addr, 644 *x509_key_password = NULL, 645 *x509_dh_file = NULL, 646 *tls_ciphers = NULL; 647 char *x509_key_file = NULL, 648 *x509_cert_file = NULL, 649 *x509_cacert_file = NULL; 650 int port, tls_port, addr_flags; 651 spice_image_compression_t compression; 652 spice_wan_compression_t wan_compr; 653 bool seamless_migration; 654 655 qemu_thread_get_self(&me); 656 657 if (!opts) { 658 return; 659 } 660 port = qemu_opt_get_number(opts, "port", 0); 661 tls_port = qemu_opt_get_number(opts, "tls-port", 0); 662 if (!port && !tls_port) { 663 error_report("neither port nor tls-port specified for spice"); 664 exit(1); 665 } 666 if (port < 0 || port > 65535) { 667 error_report("spice port is out of range"); 668 exit(1); 669 } 670 if (tls_port < 0 || tls_port > 65535) { 671 error_report("spice tls-port is out of range"); 672 exit(1); 673 } 674 password = qemu_opt_get(opts, "password"); 675 676 if (tls_port) { 677 x509_dir = qemu_opt_get(opts, "x509-dir"); 678 if (NULL == x509_dir) { 679 x509_dir = "."; 680 } 681 682 str = qemu_opt_get(opts, "x509-key-file"); 683 if (str) { 684 x509_key_file = g_strdup(str); 685 } else { 686 x509_key_file = g_strdup_printf("%s/%s", x509_dir, 687 X509_SERVER_KEY_FILE); 688 } 689 690 str = qemu_opt_get(opts, "x509-cert-file"); 691 if (str) { 692 x509_cert_file = g_strdup(str); 693 } else { 694 x509_cert_file = g_strdup_printf("%s/%s", x509_dir, 695 X509_SERVER_CERT_FILE); 696 } 697 698 str = qemu_opt_get(opts, "x509-cacert-file"); 699 if (str) { 700 x509_cacert_file = g_strdup(str); 701 } else { 702 x509_cacert_file = g_strdup_printf("%s/%s", x509_dir, 703 X509_CA_CERT_FILE); 704 } 705 706 x509_key_password = qemu_opt_get(opts, "x509-key-password"); 707 x509_dh_file = qemu_opt_get(opts, "x509-dh-key-file"); 708 tls_ciphers = qemu_opt_get(opts, "tls-ciphers"); 709 } 710 711 addr = qemu_opt_get(opts, "addr"); 712 addr_flags = 0; 713 if (qemu_opt_get_bool(opts, "ipv4", 0)) { 714 addr_flags |= SPICE_ADDR_FLAG_IPV4_ONLY; 715 } else if (qemu_opt_get_bool(opts, "ipv6", 0)) { 716 addr_flags |= SPICE_ADDR_FLAG_IPV6_ONLY; 717 } 718 719 spice_server = spice_server_new(); 720 spice_server_set_addr(spice_server, addr ? addr : "", addr_flags); 721 if (port) { 722 spice_server_set_port(spice_server, port); 723 } 724 if (tls_port) { 725 spice_server_set_tls(spice_server, tls_port, 726 x509_cacert_file, 727 x509_cert_file, 728 x509_key_file, 729 x509_key_password, 730 x509_dh_file, 731 tls_ciphers); 732 } 733 if (password) { 734 spice_server_set_ticket(spice_server, password, 0, 0, 0); 735 } 736 if (qemu_opt_get_bool(opts, "sasl", 0)) { 737 if (spice_server_set_sasl_appname(spice_server, "qemu") == -1 || 738 spice_server_set_sasl(spice_server, 1) == -1) { 739 error_report("spice: failed to enable sasl"); 740 exit(1); 741 } 742 } 743 if (qemu_opt_get_bool(opts, "disable-ticketing", 0)) { 744 auth = "none"; 745 spice_server_set_noauth(spice_server); 746 } 747 748 if (qemu_opt_get_bool(opts, "disable-copy-paste", 0)) { 749 spice_server_set_agent_copypaste(spice_server, false); 750 } 751 752 if (qemu_opt_get_bool(opts, "disable-agent-file-xfer", 0)) { 753 #if SPICE_SERVER_VERSION >= 0x000c04 754 spice_server_set_agent_file_xfer(spice_server, false); 755 #else 756 error_report("this qemu build does not support the " 757 "\"disable-agent-file-xfer\" option"); 758 exit(1); 759 #endif 760 } 761 762 compression = SPICE_IMAGE_COMPRESS_AUTO_GLZ; 763 str = qemu_opt_get(opts, "image-compression"); 764 if (str) { 765 compression = parse_compression(str); 766 } 767 spice_server_set_image_compression(spice_server, compression); 768 769 wan_compr = SPICE_WAN_COMPRESSION_AUTO; 770 str = qemu_opt_get(opts, "jpeg-wan-compression"); 771 if (str) { 772 wan_compr = parse_wan_compression(str); 773 } 774 spice_server_set_jpeg_compression(spice_server, wan_compr); 775 776 wan_compr = SPICE_WAN_COMPRESSION_AUTO; 777 str = qemu_opt_get(opts, "zlib-glz-wan-compression"); 778 if (str) { 779 wan_compr = parse_wan_compression(str); 780 } 781 spice_server_set_zlib_glz_compression(spice_server, wan_compr); 782 783 str = qemu_opt_get(opts, "streaming-video"); 784 if (str) { 785 int streaming_video = parse_stream_video(str); 786 spice_server_set_streaming_video(spice_server, streaming_video); 787 } else { 788 spice_server_set_streaming_video(spice_server, SPICE_STREAM_VIDEO_OFF); 789 } 790 791 spice_server_set_agent_mouse 792 (spice_server, qemu_opt_get_bool(opts, "agent-mouse", 1)); 793 spice_server_set_playback_compression 794 (spice_server, qemu_opt_get_bool(opts, "playback-compression", 1)); 795 796 qemu_opt_foreach(opts, add_channel, &tls_port, 0); 797 798 spice_server_set_name(spice_server, qemu_name); 799 spice_server_set_uuid(spice_server, qemu_uuid); 800 801 seamless_migration = qemu_opt_get_bool(opts, "seamless-migration", 0); 802 spice_server_set_seamless_migration(spice_server, seamless_migration); 803 if (0 != spice_server_init(spice_server, &core_interface)) { 804 error_report("failed to initialize spice server"); 805 exit(1); 806 }; 807 using_spice = 1; 808 809 migration_state.notify = migration_state_notifier; 810 add_migration_state_change_notifier(&migration_state); 811 spice_migrate.sin.base.sif = &migrate_interface.base; 812 spice_migrate.connect_complete.cb = NULL; 813 qemu_spice_add_interface(&spice_migrate.sin.base); 814 815 qemu_spice_input_init(); 816 qemu_spice_audio_init(); 817 818 qemu_add_vm_change_state_handler(vm_change_state_handler, NULL); 819 820 g_free(x509_key_file); 821 g_free(x509_cert_file); 822 g_free(x509_cacert_file); 823 824 #if SPICE_SERVER_VERSION >= 0x000c02 825 qemu_spice_register_ports(); 826 #endif 827 } 828 829 int qemu_spice_add_interface(SpiceBaseInstance *sin) 830 { 831 if (!spice_server) { 832 if (QTAILQ_FIRST(&qemu_spice_opts.head) != NULL) { 833 error_report("Oops: spice configured but not active"); 834 exit(1); 835 } 836 /* 837 * Create a spice server instance. 838 * It does *not* listen on the network. 839 * It handles QXL local rendering only. 840 * 841 * With a command line like '-vnc :0 -vga qxl' you'll end up here. 842 */ 843 spice_server = spice_server_new(); 844 spice_server_set_sasl_appname(spice_server, "qemu"); 845 spice_server_init(spice_server, &core_interface); 846 qemu_add_vm_change_state_handler(vm_change_state_handler, NULL); 847 } 848 849 return spice_server_add_interface(spice_server, sin); 850 } 851 852 static GSList *spice_consoles; 853 static int display_id; 854 855 bool qemu_spice_have_display_interface(QemuConsole *con) 856 { 857 if (g_slist_find(spice_consoles, con)) { 858 return true; 859 } 860 return false; 861 } 862 863 int qemu_spice_add_display_interface(QXLInstance *qxlin, QemuConsole *con) 864 { 865 if (g_slist_find(spice_consoles, con)) { 866 return -1; 867 } 868 qxlin->id = display_id++; 869 spice_consoles = g_slist_append(spice_consoles, con); 870 return qemu_spice_add_interface(&qxlin->base); 871 } 872 873 static int qemu_spice_set_ticket(bool fail_if_conn, bool disconnect_if_conn) 874 { 875 time_t lifetime, now = time(NULL); 876 char *passwd; 877 878 if (now < auth_expires) { 879 passwd = auth_passwd; 880 lifetime = (auth_expires - now); 881 if (lifetime > INT_MAX) { 882 lifetime = INT_MAX; 883 } 884 } else { 885 passwd = NULL; 886 lifetime = 1; 887 } 888 return spice_server_set_ticket(spice_server, passwd, lifetime, 889 fail_if_conn, disconnect_if_conn); 890 } 891 892 int qemu_spice_set_passwd(const char *passwd, 893 bool fail_if_conn, bool disconnect_if_conn) 894 { 895 g_free(auth_passwd); 896 auth_passwd = g_strdup(passwd); 897 return qemu_spice_set_ticket(fail_if_conn, disconnect_if_conn); 898 } 899 900 int qemu_spice_set_pw_expire(time_t expires) 901 { 902 auth_expires = expires; 903 return qemu_spice_set_ticket(false, false); 904 } 905 906 int qemu_spice_display_add_client(int csock, int skipauth, int tls) 907 { 908 if (tls) { 909 return spice_server_add_ssl_client(spice_server, csock, skipauth); 910 } else { 911 return spice_server_add_client(spice_server, csock, skipauth); 912 } 913 } 914 915 void qemu_spice_display_start(void) 916 { 917 spice_display_is_running = true; 918 spice_server_vm_start(spice_server); 919 } 920 921 void qemu_spice_display_stop(void) 922 { 923 spice_server_vm_stop(spice_server); 924 spice_display_is_running = false; 925 } 926 927 int qemu_spice_display_is_running(SimpleSpiceDisplay *ssd) 928 { 929 return spice_display_is_running; 930 } 931 932 static void spice_register_config(void) 933 { 934 qemu_add_opts(&qemu_spice_opts); 935 } 936 machine_init(spice_register_config); 937