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