s390x: get rid of cpu_s390x_create()
[qemu.git] / ui / spice-core.c
blobea04dc69b530d8ce5e85961ea4325c5ddc80a89e
1 /*
2 * Copyright (C) 2010 Red Hat, Inc.
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.
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.
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/>.
18 #include "qemu/osdep.h"
19 #include <spice.h>
21 #include <netdb.h>
22 #include "sysemu/sysemu.h"
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"
42 /* core bits */
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;
54 static QemuThread me;
56 struct SpiceTimer {
57 QEMUTimer *timer;
58 QTAILQ_ENTRY(SpiceTimer) next;
60 static QTAILQ_HEAD(, SpiceTimer) timers = QTAILQ_HEAD_INITIALIZER(timers);
62 static SpiceTimer *timer_add(SpiceTimerFunc func, void *opaque)
64 SpiceTimer *timer;
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;
72 static void timer_start(SpiceTimer *timer, uint32_t ms)
74 timer_mod(timer->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + ms);
77 static void timer_cancel(SpiceTimer *timer)
79 timer_del(timer->timer);
82 static void timer_remove(SpiceTimer *timer)
84 timer_del(timer->timer);
85 timer_free(timer->timer);
86 QTAILQ_REMOVE(&timers, timer, next);
87 g_free(timer);
90 struct SpiceWatch {
91 int fd;
92 int event_mask;
93 SpiceWatchFunc func;
94 void *opaque;
95 QTAILQ_ENTRY(SpiceWatch) next;
97 static QTAILQ_HEAD(, SpiceWatch) watches = QTAILQ_HEAD_INITIALIZER(watches);
99 static void watch_read(void *opaque)
101 SpiceWatch *watch = opaque;
102 watch->func(watch->fd, SPICE_WATCH_EVENT_READ, watch->opaque);
105 static void watch_write(void *opaque)
107 SpiceWatch *watch = opaque;
108 watch->func(watch->fd, SPICE_WATCH_EVENT_WRITE, watch->opaque);
111 static void watch_update_mask(SpiceWatch *watch, int event_mask)
113 IOHandler *on_read = NULL;
114 IOHandler *on_write = NULL;
116 watch->event_mask = event_mask;
117 if (watch->event_mask & SPICE_WATCH_EVENT_READ) {
118 on_read = watch_read;
120 if (watch->event_mask & SPICE_WATCH_EVENT_WRITE) {
121 on_write = watch_write;
123 qemu_set_fd_handler(watch->fd, on_read, on_write, watch);
126 static SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque)
128 SpiceWatch *watch;
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);
136 watch_update_mask(watch, event_mask);
137 return watch;
140 static void watch_remove(SpiceWatch *watch)
142 qemu_set_fd_handler(watch->fd, NULL, NULL, NULL);
143 QTAILQ_REMOVE(&watches, watch, next);
144 g_free(watch);
147 typedef struct ChannelList ChannelList;
148 struct ChannelList {
149 SpiceChannelEventInfo *info;
150 QTAILQ_ENTRY(ChannelList) link;
152 static QTAILQ_HEAD(, ChannelList) channel_list = QTAILQ_HEAD_INITIALIZER(channel_list);
154 static void channel_list_add(SpiceChannelEventInfo *info)
156 ChannelList *item;
158 item = g_malloc0(sizeof(*item));
159 item->info = info;
160 QTAILQ_INSERT_TAIL(&channel_list, item, link);
163 static void channel_list_del(SpiceChannelEventInfo *info)
165 ChannelList *item;
167 QTAILQ_FOREACH(item, &channel_list, link) {
168 if (item->info != info) {
169 continue;
171 QTAILQ_REMOVE(&channel_list, item, link);
172 g_free(item);
173 return;
177 static void add_addr_info(SpiceBasicInfo *info, struct sockaddr *addr, int len)
179 char host[NI_MAXHOST], port[NI_MAXSERV];
181 getnameinfo(addr, len, host, sizeof(host), port, sizeof(port),
182 NI_NUMERICHOST | NI_NUMERICSERV);
184 info->host = g_strdup(host);
185 info->port = g_strdup(port);
186 info->family = inet_netfamily(addr->sa_family);
189 static void add_channel_info(SpiceChannel *sc, SpiceChannelEventInfo *info)
191 int tls = info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS;
193 sc->connection_id = info->connection_id;
194 sc->channel_type = info->type;
195 sc->channel_id = info->id;
196 sc->tls = !!tls;
199 static void channel_event(int event, SpiceChannelEventInfo *info)
201 SpiceServerInfo *server = g_malloc0(sizeof(*server));
202 SpiceChannel *client = g_malloc0(sizeof(*client));
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.
213 bool need_lock = !qemu_thread_is_self(&me);
214 if (need_lock) {
215 qemu_mutex_lock_iothread();
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__);
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);
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;
255 if (need_lock) {
256 qemu_mutex_unlock_iothread();
259 qapi_free_SpiceServerInfo(server);
260 qapi_free_SpiceChannel(client);
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,
269 .timer_add = timer_add,
270 .timer_start = timer_start,
271 .timer_cancel = timer_cancel,
272 .timer_remove = timer_remove,
274 .watch_add = watch_add,
275 .watch_update_mask = watch_update_mask,
276 .watch_remove = watch_remove,
278 .channel_event = channel_event,
281 static void migrate_connect_complete_cb(SpiceMigrateInstance *sin);
282 static void migrate_end_complete_cb(SpiceMigrateInstance *sin);
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,
293 static SpiceMigrateInstance spice_migrate;
295 static void migrate_connect_complete_cb(SpiceMigrateInstance *sin)
297 /* nothing, but libspice-server expects this cb being present. */
300 static void migrate_end_complete_cb(SpiceMigrateInstance *sin)
302 qapi_event_send_spice_migrate_completed(&error_abort);
303 spice_migration_completed = true;
306 /* config string parsing */
308 static int name2enum(const char *string, const char *table[], int entries)
310 int i;
312 if (string) {
313 for (i = 0; i < entries; i++) {
314 if (!table[i]) {
315 continue;
317 if (strcmp(string, table[i]) != 0) {
318 continue;
320 return i;
323 return -1;
326 static int parse_name(const char *string, const char *optname,
327 const char *table[], int entries)
329 int value = name2enum(string, table, entries);
331 if (value != -1) {
332 return value;
334 error_report("spice: invalid %s: %s", optname, string);
335 exit(1);
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",
343 #define parse_stream_video(_name) \
344 parse_name(_name, "stream video control", \
345 stream_video_names, ARRAY_SIZE(stream_video_names))
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",
355 #define parse_compression(_name) \
356 parse_name(_name, "image compression", \
357 compression_names, ARRAY_SIZE(compression_names))
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",
364 #define parse_wan_compression(_name) \
365 parse_name(_name, "wan compression", \
366 wan_compression_names, ARRAY_SIZE(wan_compression_names))
368 /* functions for the rest of qemu */
370 static SpiceChannelList *qmp_query_spice_channels(void)
372 SpiceChannelList *cur_item = NULL, *head = NULL;
373 ChannelList *item;
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;
381 assert(item->info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT);
383 chan = g_malloc0(sizeof(*chan));
384 chan->value = g_malloc0(sizeof(*chan->value));
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);
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;
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;
409 return head;
412 static QemuOptsList qemu_spice_opts = {
413 .name = "spice",
414 .head = QTAILQ_HEAD_INITIALIZER(qemu_spice_opts.head),
415 .desc = {
417 .name = "port",
418 .type = QEMU_OPT_NUMBER,
420 .name = "tls-port",
421 .type = QEMU_OPT_NUMBER,
423 .name = "addr",
424 .type = QEMU_OPT_STRING,
426 .name = "ipv4",
427 .type = QEMU_OPT_BOOL,
429 .name = "ipv6",
430 .type = QEMU_OPT_BOOL,
431 #ifdef SPICE_ADDR_FLAG_UNIX_ONLY
433 .name = "unix",
434 .type = QEMU_OPT_BOOL,
435 #endif
437 .name = "password",
438 .type = QEMU_OPT_STRING,
440 .name = "disable-ticketing",
441 .type = QEMU_OPT_BOOL,
443 .name = "disable-copy-paste",
444 .type = QEMU_OPT_BOOL,
446 .name = "disable-agent-file-xfer",
447 .type = QEMU_OPT_BOOL,
449 .name = "sasl",
450 .type = QEMU_OPT_BOOL,
452 .name = "x509-dir",
453 .type = QEMU_OPT_STRING,
455 .name = "x509-key-file",
456 .type = QEMU_OPT_STRING,
458 .name = "x509-key-password",
459 .type = QEMU_OPT_STRING,
461 .name = "x509-cert-file",
462 .type = QEMU_OPT_STRING,
464 .name = "x509-cacert-file",
465 .type = QEMU_OPT_STRING,
467 .name = "x509-dh-key-file",
468 .type = QEMU_OPT_STRING,
470 .name = "tls-ciphers",
471 .type = QEMU_OPT_STRING,
473 .name = "tls-channel",
474 .type = QEMU_OPT_STRING,
476 .name = "plaintext-channel",
477 .type = QEMU_OPT_STRING,
479 .name = "image-compression",
480 .type = QEMU_OPT_STRING,
482 .name = "jpeg-wan-compression",
483 .type = QEMU_OPT_STRING,
485 .name = "zlib-glz-wan-compression",
486 .type = QEMU_OPT_STRING,
488 .name = "streaming-video",
489 .type = QEMU_OPT_STRING,
491 .name = "agent-mouse",
492 .type = QEMU_OPT_BOOL,
494 .name = "playback-compression",
495 .type = QEMU_OPT_BOOL,
497 .name = "seamless-migration",
498 .type = QEMU_OPT_BOOL,
500 .name = "display",
501 .type = QEMU_OPT_STRING,
503 .name = "head",
504 .type = QEMU_OPT_NUMBER,
505 #ifdef HAVE_SPICE_GL
507 .name = "gl",
508 .type = QEMU_OPT_BOOL,
510 .name = "rendernode",
511 .type = QEMU_OPT_STRING,
512 #endif
514 { /* end of list */ }
518 SpiceInfo *qmp_query_spice(Error **errp)
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;
528 info = g_malloc0(sizeof(*info));
530 if (!spice_server || !opts) {
531 info->enabled = false;
532 return info;
535 info->enabled = true;
536 info->migrated = spice_migration_completed;
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);
542 info->has_auth = true;
543 info->auth = g_strdup(auth);
545 info->has_host = true;
546 info->host = g_strdup(addr ? addr : "*");
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);
554 if (port) {
555 info->has_port = true;
556 info->port = port;
558 if (tls_port) {
559 info->has_tls_port = true;
560 info->tls_port = tls_port;
563 info->mouse_mode = spice_server_is_server_mouse(spice_server) ?
564 SPICE_QUERY_MOUSE_MODE_SERVER :
565 SPICE_QUERY_MOUSE_MODE_CLIENT;
567 /* for compatibility with the original command */
568 info->has_channels = true;
569 info->channels = qmp_query_spice_channels();
571 return info;
574 static void migration_state_notifier(Notifier *notifier, void *data)
576 MigrationState *s = data;
578 if (!spice_have_target_host) {
579 return;
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;
594 int qemu_spice_migrate_info(const char *hostname, int port, int tls_port,
595 const char *subject)
597 int ret;
599 ret = spice_server_migrate_connect(spice_server, hostname,
600 port, tls_port, subject);
601 spice_have_target_host = true;
602 return ret;
605 static int add_channel(void *opaque, const char *name, const char *value,
606 Error **errp)
608 int security = 0;
609 int rc;
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);
618 security = SPICE_CHANNEL_SECURITY_SSL;
620 if (strcmp(name, "plaintext-channel") == 0) {
621 security = SPICE_CHANNEL_SECURITY_NONE;
623 if (security == 0) {
624 return 0;
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);
631 if (rc != 0) {
632 error_report("spice: failed to set channel security for %s", value);
633 exit(1);
635 return 0;
638 static void vm_change_state_handler(void *opaque, int running,
639 RunState state)
641 if (running) {
642 qemu_spice_display_start();
643 } else {
644 qemu_spice_display_stop();
648 void qemu_spice_init(void)
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;
663 qemu_thread_get_self(&me);
665 if (!opts) {
666 return;
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);
674 if (tls_port < 0 || tls_port > 65535) {
675 error_report("spice tls-port is out of range");
676 exit(1);
678 password = qemu_opt_get(opts, "password");
680 if (tls_port) {
681 x509_dir = qemu_opt_get(opts, "x509-dir");
682 if (!x509_dir) {
683 x509_dir = ".";
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);
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);
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);
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");
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
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);
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);
741 if (password) {
742 qemu_spice_set_passwd(password, false, false);
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);
749 auth = "sasl";
751 if (qemu_opt_get_bool(opts, "disable-ticketing", 0)) {
752 auth = "none";
753 spice_server_set_noauth(spice_server);
756 if (qemu_opt_get_bool(opts, "disable-copy-paste", 0)) {
757 spice_server_set_agent_copypaste(spice_server, false);
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
770 compression = SPICE_IMAGE_COMPRESS_AUTO_GLZ;
771 str = qemu_opt_get(opts, "image-compression");
772 if (str) {
773 compression = parse_compression(str);
775 spice_server_set_image_compression(spice_server, compression);
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);
782 spice_server_set_jpeg_compression(spice_server, wan_compr);
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);
789 spice_server_set_zlib_glz_compression(spice_server, wan_compr);
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);
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));
804 qemu_opt_foreach(opts, add_channel, &tls_port, NULL);
806 spice_server_set_name(spice_server, qemu_name);
807 spice_server_set_uuid(spice_server, (unsigned char *)&qemu_uuid);
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);
816 using_spice = 1;
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);
823 qemu_spice_input_init();
824 qemu_spice_audio_init();
826 qemu_add_vm_change_state_handler(vm_change_state_handler, NULL);
827 qemu_spice_display_stop();
829 g_free(x509_key_file);
830 g_free(x509_cert_file);
831 g_free(x509_cacert_file);
833 #if SPICE_SERVER_VERSION >= 0x000c02
834 qemu_spice_register_ports();
835 #endif
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);
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);
848 display_opengl = 1;
849 spice_opengl = 1;
851 #endif
854 int qemu_spice_add_interface(SpiceBaseInstance *sin)
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);
862 * Create a spice server instance.
863 * It does *not* listen on the network.
864 * It handles QXL local rendering only.
866 * With a command line like '-vnc :0 -vga qxl' you'll end up here.
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);
874 return spice_server_add_interface(spice_server, sin);
877 static GSList *spice_consoles;
879 bool qemu_spice_have_display_interface(QemuConsole *con)
881 if (g_slist_find(spice_consoles, con)) {
882 return true;
884 return false;
887 int qemu_spice_add_display_interface(QXLInstance *qxlin, QemuConsole *con)
889 if (g_slist_find(spice_consoles, con)) {
890 return -1;
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);
897 static int qemu_spice_set_ticket(bool fail_if_conn, bool disconnect_if_conn)
899 time_t lifetime, now = time(NULL);
900 char *passwd;
902 if (now < auth_expires) {
903 passwd = auth_passwd;
904 lifetime = (auth_expires - now);
905 if (lifetime > INT_MAX) {
906 lifetime = INT_MAX;
908 } else {
909 passwd = NULL;
910 lifetime = 1;
912 return spice_server_set_ticket(spice_server, passwd, lifetime,
913 fail_if_conn, disconnect_if_conn);
916 int qemu_spice_set_passwd(const char *passwd,
917 bool fail_if_conn, bool disconnect_if_conn)
919 if (strcmp(auth, "spice") != 0) {
920 return -1;
923 g_free(auth_passwd);
924 auth_passwd = g_strdup(passwd);
925 return qemu_spice_set_ticket(fail_if_conn, disconnect_if_conn);
928 int qemu_spice_set_pw_expire(time_t expires)
930 auth_expires = expires;
931 return qemu_spice_set_ticket(false, false);
934 int qemu_spice_display_add_client(int csock, int skipauth, int tls)
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);
943 void qemu_spice_display_start(void)
945 spice_display_is_running = true;
946 spice_server_vm_start(spice_server);
949 void qemu_spice_display_stop(void)
951 spice_server_vm_stop(spice_server);
952 spice_display_is_running = false;
955 int qemu_spice_display_is_running(SimpleSpiceDisplay *ssd)
957 return spice_display_is_running;
960 static void spice_register_config(void)
962 qemu_add_opts(&qemu_spice_opts);
964 opts_init(spice_register_config);