xref: /openbmc/qemu/ui/spice-core.c (revision 36f96c4b)
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 
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 
263 static 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 
281 static void migrate_connect_complete_cb(SpiceMigrateInstance *sin);
282 static void migrate_end_complete_cb(SpiceMigrateInstance *sin);
283 
284 static 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 
293 static SpiceMigrateInstance spice_migrate;
294 
295 static void migrate_connect_complete_cb(SpiceMigrateInstance *sin)
296 {
297     /* nothing, but libspice-server expects this cb being present. */
298 }
299 
300 static 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 
308 static 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 
326 static 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 
338 static 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 
347 static 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 
359 static 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 
370 static 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 
412 static 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         { /* end of list */ }
501     },
502 };
503 
504 SpiceInfo *qmp_query_spice(Error **errp)
505 {
506     QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
507     int port, tls_port;
508     const char *addr;
509     SpiceInfo *info;
510     unsigned int major;
511     unsigned int minor;
512     unsigned int micro;
513 
514     info = g_malloc0(sizeof(*info));
515 
516     if (!spice_server || !opts) {
517         info->enabled = false;
518         return info;
519     }
520 
521     info->enabled = true;
522     info->migrated = spice_migration_completed;
523 
524     addr = qemu_opt_get(opts, "addr");
525     port = qemu_opt_get_number(opts, "port", 0);
526     tls_port = qemu_opt_get_number(opts, "tls-port", 0);
527 
528     info->has_auth = true;
529     info->auth = g_strdup(auth);
530 
531     info->has_host = true;
532     info->host = g_strdup(addr ? addr : "*");
533 
534     info->has_compiled_version = true;
535     major = (SPICE_SERVER_VERSION & 0xff0000) >> 16;
536     minor = (SPICE_SERVER_VERSION & 0xff00) >> 8;
537     micro = SPICE_SERVER_VERSION & 0xff;
538     info->compiled_version = g_strdup_printf("%d.%d.%d", major, minor, micro);
539 
540     if (port) {
541         info->has_port = true;
542         info->port = port;
543     }
544     if (tls_port) {
545         info->has_tls_port = true;
546         info->tls_port = tls_port;
547     }
548 
549     info->mouse_mode = spice_server_is_server_mouse(spice_server) ?
550                        SPICE_QUERY_MOUSE_MODE_SERVER :
551                        SPICE_QUERY_MOUSE_MODE_CLIENT;
552 
553     /* for compatibility with the original command */
554     info->has_channels = true;
555     info->channels = qmp_query_spice_channels();
556 
557     return info;
558 }
559 
560 static void migration_state_notifier(Notifier *notifier, void *data)
561 {
562     MigrationState *s = data;
563 
564     if (!spice_have_target_host) {
565         return;
566     }
567 
568     if (migration_in_setup(s)) {
569         spice_server_migrate_start(spice_server);
570     } else if (migration_has_finished(s)) {
571         spice_server_migrate_end(spice_server, true);
572         spice_have_target_host = false;
573     } else if (migration_has_failed(s)) {
574         spice_server_migrate_end(spice_server, false);
575         spice_have_target_host = false;
576     }
577 }
578 
579 int qemu_spice_migrate_info(const char *hostname, int port, int tls_port,
580                             const char *subject)
581 {
582     int ret;
583 
584     ret = spice_server_migrate_connect(spice_server, hostname,
585                                        port, tls_port, subject);
586     spice_have_target_host = true;
587     return ret;
588 }
589 
590 static int add_channel(void *opaque, const char *name, const char *value,
591                        Error **errp)
592 {
593     int security = 0;
594     int rc;
595 
596     if (strcmp(name, "tls-channel") == 0) {
597         int *tls_port = opaque;
598         if (!*tls_port) {
599             error_report("spice: tried to setup tls-channel"
600                          " without specifying a TLS port");
601             exit(1);
602         }
603         security = SPICE_CHANNEL_SECURITY_SSL;
604     }
605     if (strcmp(name, "plaintext-channel") == 0) {
606         security = SPICE_CHANNEL_SECURITY_NONE;
607     }
608     if (security == 0) {
609         return 0;
610     }
611     if (strcmp(value, "default") == 0) {
612         rc = spice_server_set_channel_security(spice_server, NULL, security);
613     } else {
614         rc = spice_server_set_channel_security(spice_server, value, security);
615     }
616     if (rc != 0) {
617         error_report("spice: failed to set channel security for %s", value);
618         exit(1);
619     }
620     return 0;
621 }
622 
623 static void vm_change_state_handler(void *opaque, int running,
624                                     RunState state)
625 {
626     if (running) {
627         qemu_spice_display_start();
628     } else {
629         qemu_spice_display_stop();
630     }
631 }
632 
633 void qemu_spice_init(void)
634 {
635     QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
636     const char *password, *str, *x509_dir, *addr,
637         *x509_key_password = NULL,
638         *x509_dh_file = NULL,
639         *tls_ciphers = NULL;
640     char *x509_key_file = NULL,
641         *x509_cert_file = NULL,
642         *x509_cacert_file = NULL;
643     int port, tls_port, addr_flags;
644     spice_image_compression_t compression;
645     spice_wan_compression_t wan_compr;
646     bool seamless_migration;
647 
648     qemu_thread_get_self(&me);
649 
650     if (!opts) {
651         return;
652     }
653     port = qemu_opt_get_number(opts, "port", 0);
654     tls_port = qemu_opt_get_number(opts, "tls-port", 0);
655     if (port < 0 || port > 65535) {
656         error_report("spice port is out of range");
657         exit(1);
658     }
659     if (tls_port < 0 || tls_port > 65535) {
660         error_report("spice tls-port is out of range");
661         exit(1);
662     }
663     password = qemu_opt_get(opts, "password");
664 
665     if (tls_port) {
666         x509_dir = qemu_opt_get(opts, "x509-dir");
667         if (!x509_dir) {
668             x509_dir = ".";
669         }
670 
671         str = qemu_opt_get(opts, "x509-key-file");
672         if (str) {
673             x509_key_file = g_strdup(str);
674         } else {
675             x509_key_file = g_strdup_printf("%s/%s", x509_dir,
676                                             X509_SERVER_KEY_FILE);
677         }
678 
679         str = qemu_opt_get(opts, "x509-cert-file");
680         if (str) {
681             x509_cert_file = g_strdup(str);
682         } else {
683             x509_cert_file = g_strdup_printf("%s/%s", x509_dir,
684                                              X509_SERVER_CERT_FILE);
685         }
686 
687         str = qemu_opt_get(opts, "x509-cacert-file");
688         if (str) {
689             x509_cacert_file = g_strdup(str);
690         } else {
691             x509_cacert_file = g_strdup_printf("%s/%s", x509_dir,
692                                                X509_CA_CERT_FILE);
693         }
694 
695         x509_key_password = qemu_opt_get(opts, "x509-key-password");
696         x509_dh_file = qemu_opt_get(opts, "x509-dh-key-file");
697         tls_ciphers = qemu_opt_get(opts, "tls-ciphers");
698     }
699 
700     addr = qemu_opt_get(opts, "addr");
701     addr_flags = 0;
702     if (qemu_opt_get_bool(opts, "ipv4", 0)) {
703         addr_flags |= SPICE_ADDR_FLAG_IPV4_ONLY;
704     } else if (qemu_opt_get_bool(opts, "ipv6", 0)) {
705         addr_flags |= SPICE_ADDR_FLAG_IPV6_ONLY;
706 #ifdef SPICE_ADDR_FLAG_UNIX_ONLY
707     } else if (qemu_opt_get_bool(opts, "unix", 0)) {
708         addr_flags |= SPICE_ADDR_FLAG_UNIX_ONLY;
709 #endif
710     }
711 
712     spice_server = spice_server_new();
713     spice_server_set_addr(spice_server, addr ? addr : "", addr_flags);
714     if (port) {
715         spice_server_set_port(spice_server, port);
716     }
717     if (tls_port) {
718         spice_server_set_tls(spice_server, tls_port,
719                              x509_cacert_file,
720                              x509_cert_file,
721                              x509_key_file,
722                              x509_key_password,
723                              x509_dh_file,
724                              tls_ciphers);
725     }
726     if (password) {
727         qemu_spice_set_passwd(password, false, false);
728     }
729     if (qemu_opt_get_bool(opts, "sasl", 0)) {
730         if (spice_server_set_sasl(spice_server, 1) == -1) {
731             error_report("spice: failed to enable sasl");
732             exit(1);
733         }
734         auth = "sasl";
735     }
736     if (qemu_opt_get_bool(opts, "disable-ticketing", 0)) {
737         auth = "none";
738         spice_server_set_noauth(spice_server);
739     }
740 
741     if (qemu_opt_get_bool(opts, "disable-copy-paste", 0)) {
742         spice_server_set_agent_copypaste(spice_server, false);
743     }
744 
745     if (qemu_opt_get_bool(opts, "disable-agent-file-xfer", 0)) {
746 #if SPICE_SERVER_VERSION >= 0x000c04
747         spice_server_set_agent_file_xfer(spice_server, false);
748 #else
749         error_report("this qemu build does not support the "
750                      "\"disable-agent-file-xfer\" option");
751         exit(1);
752 #endif
753     }
754 
755     compression = SPICE_IMAGE_COMPRESS_AUTO_GLZ;
756     str = qemu_opt_get(opts, "image-compression");
757     if (str) {
758         compression = parse_compression(str);
759     }
760     spice_server_set_image_compression(spice_server, compression);
761 
762     wan_compr = SPICE_WAN_COMPRESSION_AUTO;
763     str = qemu_opt_get(opts, "jpeg-wan-compression");
764     if (str) {
765         wan_compr = parse_wan_compression(str);
766     }
767     spice_server_set_jpeg_compression(spice_server, wan_compr);
768 
769     wan_compr = SPICE_WAN_COMPRESSION_AUTO;
770     str = qemu_opt_get(opts, "zlib-glz-wan-compression");
771     if (str) {
772         wan_compr = parse_wan_compression(str);
773     }
774     spice_server_set_zlib_glz_compression(spice_server, wan_compr);
775 
776     str = qemu_opt_get(opts, "streaming-video");
777     if (str) {
778         int streaming_video = parse_stream_video(str);
779         spice_server_set_streaming_video(spice_server, streaming_video);
780     } else {
781         spice_server_set_streaming_video(spice_server, SPICE_STREAM_VIDEO_OFF);
782     }
783 
784     spice_server_set_agent_mouse
785         (spice_server, qemu_opt_get_bool(opts, "agent-mouse", 1));
786     spice_server_set_playback_compression
787         (spice_server, qemu_opt_get_bool(opts, "playback-compression", 1));
788 
789     qemu_opt_foreach(opts, add_channel, &tls_port, NULL);
790 
791     spice_server_set_name(spice_server, qemu_name);
792     spice_server_set_uuid(spice_server, qemu_uuid);
793 
794     seamless_migration = qemu_opt_get_bool(opts, "seamless-migration", 0);
795     spice_server_set_seamless_migration(spice_server, seamless_migration);
796     spice_server_set_sasl_appname(spice_server, "qemu");
797     if (spice_server_init(spice_server, &core_interface) != 0) {
798         error_report("failed to initialize spice server");
799         exit(1);
800     };
801     using_spice = 1;
802 
803     migration_state.notify = migration_state_notifier;
804     add_migration_state_change_notifier(&migration_state);
805     spice_migrate.base.sif = &migrate_interface.base;
806     qemu_spice_add_interface(&spice_migrate.base);
807 
808     qemu_spice_input_init();
809     qemu_spice_audio_init();
810 
811     qemu_add_vm_change_state_handler(vm_change_state_handler, NULL);
812     qemu_spice_display_stop();
813 
814     g_free(x509_key_file);
815     g_free(x509_cert_file);
816     g_free(x509_cacert_file);
817 
818 #if SPICE_SERVER_VERSION >= 0x000c02
819     qemu_spice_register_ports();
820 #endif
821 }
822 
823 int qemu_spice_add_interface(SpiceBaseInstance *sin)
824 {
825     if (!spice_server) {
826         if (QTAILQ_FIRST(&qemu_spice_opts.head) != NULL) {
827             error_report("Oops: spice configured but not active");
828             exit(1);
829         }
830         /*
831          * Create a spice server instance.
832          * It does *not* listen on the network.
833          * It handles QXL local rendering only.
834          *
835          * With a command line like '-vnc :0 -vga qxl' you'll end up here.
836          */
837         spice_server = spice_server_new();
838         spice_server_set_sasl_appname(spice_server, "qemu");
839         spice_server_init(spice_server, &core_interface);
840         qemu_add_vm_change_state_handler(vm_change_state_handler, NULL);
841     }
842 
843     return spice_server_add_interface(spice_server, sin);
844 }
845 
846 static GSList *spice_consoles;
847 
848 bool qemu_spice_have_display_interface(QemuConsole *con)
849 {
850     if (g_slist_find(spice_consoles, con)) {
851         return true;
852     }
853     return false;
854 }
855 
856 int qemu_spice_add_display_interface(QXLInstance *qxlin, QemuConsole *con)
857 {
858     if (g_slist_find(spice_consoles, con)) {
859         return -1;
860     }
861     qxlin->id = qemu_console_get_index(con);
862     spice_consoles = g_slist_append(spice_consoles, con);
863     return qemu_spice_add_interface(&qxlin->base);
864 }
865 
866 static int qemu_spice_set_ticket(bool fail_if_conn, bool disconnect_if_conn)
867 {
868     time_t lifetime, now = time(NULL);
869     char *passwd;
870 
871     if (now < auth_expires) {
872         passwd = auth_passwd;
873         lifetime = (auth_expires - now);
874         if (lifetime > INT_MAX) {
875             lifetime = INT_MAX;
876         }
877     } else {
878         passwd = NULL;
879         lifetime = 1;
880     }
881     return spice_server_set_ticket(spice_server, passwd, lifetime,
882                                    fail_if_conn, disconnect_if_conn);
883 }
884 
885 int qemu_spice_set_passwd(const char *passwd,
886                           bool fail_if_conn, bool disconnect_if_conn)
887 {
888     if (strcmp(auth, "spice") != 0) {
889         return -1;
890     }
891 
892     g_free(auth_passwd);
893     auth_passwd = g_strdup(passwd);
894     return qemu_spice_set_ticket(fail_if_conn, disconnect_if_conn);
895 }
896 
897 int qemu_spice_set_pw_expire(time_t expires)
898 {
899     auth_expires = expires;
900     return qemu_spice_set_ticket(false, false);
901 }
902 
903 int qemu_spice_display_add_client(int csock, int skipauth, int tls)
904 {
905     if (tls) {
906         return spice_server_add_ssl_client(spice_server, csock, skipauth);
907     } else {
908         return spice_server_add_client(spice_server, csock, skipauth);
909     }
910 }
911 
912 void qemu_spice_display_start(void)
913 {
914     spice_display_is_running = true;
915     spice_server_vm_start(spice_server);
916 }
917 
918 void qemu_spice_display_stop(void)
919 {
920     spice_server_vm_stop(spice_server);
921     spice_display_is_running = false;
922 }
923 
924 int qemu_spice_display_is_running(SimpleSpiceDisplay *ssd)
925 {
926     return spice_display_is_running;
927 }
928 
929 static void spice_register_config(void)
930 {
931     qemu_add_opts(&qemu_spice_opts);
932 }
933 machine_init(spice_register_config);
934