block: Emulate bdrv_ioctl with bdrv_aio_ioctl and track both
[qemu.git] / ui / spice-core.c
blob6a62d712feca424daeed672e4b109df498b04955
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 <spice.h>
20 #include <netdb.h>
21 #include "sysemu/sysemu.h"
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"
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,
496 }, {
497 .name = "seamless-migration",
498 .type = QEMU_OPT_BOOL,
500 { /* end of list */ }
504 SpiceInfo *qmp_query_spice(Error **errp)
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;
514 info = g_malloc0(sizeof(*info));
516 if (!spice_server || !opts) {
517 info->enabled = false;
518 return info;
521 info->enabled = true;
522 info->migrated = spice_migration_completed;
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);
528 info->has_auth = true;
529 info->auth = g_strdup(auth);
531 info->has_host = true;
532 info->host = g_strdup(addr ? addr : "*");
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);
540 if (port) {
541 info->has_port = true;
542 info->port = port;
544 if (tls_port) {
545 info->has_tls_port = true;
546 info->tls_port = tls_port;
549 info->mouse_mode = spice_server_is_server_mouse(spice_server) ?
550 SPICE_QUERY_MOUSE_MODE_SERVER :
551 SPICE_QUERY_MOUSE_MODE_CLIENT;
553 /* for compatibility with the original command */
554 info->has_channels = true;
555 info->channels = qmp_query_spice_channels();
557 return info;
560 static void migration_state_notifier(Notifier *notifier, void *data)
562 MigrationState *s = data;
564 if (!spice_have_target_host) {
565 return;
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;
579 int qemu_spice_migrate_info(const char *hostname, int port, int tls_port,
580 const char *subject)
582 int ret;
584 ret = spice_server_migrate_connect(spice_server, hostname,
585 port, tls_port, subject);
586 spice_have_target_host = true;
587 return ret;
590 static int add_channel(void *opaque, const char *name, const char *value,
591 Error **errp)
593 int security = 0;
594 int rc;
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);
603 security = SPICE_CHANNEL_SECURITY_SSL;
605 if (strcmp(name, "plaintext-channel") == 0) {
606 security = SPICE_CHANNEL_SECURITY_NONE;
608 if (security == 0) {
609 return 0;
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);
616 if (rc != 0) {
617 error_report("spice: failed to set channel security for %s", value);
618 exit(1);
620 return 0;
623 static void vm_change_state_handler(void *opaque, int running,
624 RunState state)
626 if (running) {
627 qemu_spice_display_start();
628 } else {
629 qemu_spice_display_stop();
633 void qemu_spice_init(void)
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;
648 qemu_thread_get_self(&me);
650 if (!opts) {
651 return;
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);
659 if (tls_port < 0 || tls_port > 65535) {
660 error_report("spice tls-port is out of range");
661 exit(1);
663 password = qemu_opt_get(opts, "password");
665 if (tls_port) {
666 x509_dir = qemu_opt_get(opts, "x509-dir");
667 if (!x509_dir) {
668 x509_dir = ".";
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);
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);
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);
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");
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
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);
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);
726 if (password) {
727 qemu_spice_set_passwd(password, false, false);
729 if (qemu_opt_get_bool(opts, "sasl", 0)) {
730 if (spice_server_set_sasl_appname(spice_server, "qemu") == -1 ||
731 spice_server_set_sasl(spice_server, 1) == -1) {
732 error_report("spice: failed to enable sasl");
733 exit(1);
735 auth = "sasl";
737 if (qemu_opt_get_bool(opts, "disable-ticketing", 0)) {
738 auth = "none";
739 spice_server_set_noauth(spice_server);
742 if (qemu_opt_get_bool(opts, "disable-copy-paste", 0)) {
743 spice_server_set_agent_copypaste(spice_server, false);
746 if (qemu_opt_get_bool(opts, "disable-agent-file-xfer", 0)) {
747 #if SPICE_SERVER_VERSION >= 0x000c04
748 spice_server_set_agent_file_xfer(spice_server, false);
749 #else
750 error_report("this qemu build does not support the "
751 "\"disable-agent-file-xfer\" option");
752 exit(1);
753 #endif
756 compression = SPICE_IMAGE_COMPRESS_AUTO_GLZ;
757 str = qemu_opt_get(opts, "image-compression");
758 if (str) {
759 compression = parse_compression(str);
761 spice_server_set_image_compression(spice_server, compression);
763 wan_compr = SPICE_WAN_COMPRESSION_AUTO;
764 str = qemu_opt_get(opts, "jpeg-wan-compression");
765 if (str) {
766 wan_compr = parse_wan_compression(str);
768 spice_server_set_jpeg_compression(spice_server, wan_compr);
770 wan_compr = SPICE_WAN_COMPRESSION_AUTO;
771 str = qemu_opt_get(opts, "zlib-glz-wan-compression");
772 if (str) {
773 wan_compr = parse_wan_compression(str);
775 spice_server_set_zlib_glz_compression(spice_server, wan_compr);
777 str = qemu_opt_get(opts, "streaming-video");
778 if (str) {
779 int streaming_video = parse_stream_video(str);
780 spice_server_set_streaming_video(spice_server, streaming_video);
781 } else {
782 spice_server_set_streaming_video(spice_server, SPICE_STREAM_VIDEO_OFF);
785 spice_server_set_agent_mouse
786 (spice_server, qemu_opt_get_bool(opts, "agent-mouse", 1));
787 spice_server_set_playback_compression
788 (spice_server, qemu_opt_get_bool(opts, "playback-compression", 1));
790 qemu_opt_foreach(opts, add_channel, &tls_port, NULL);
792 spice_server_set_name(spice_server, qemu_name);
793 spice_server_set_uuid(spice_server, qemu_uuid);
795 seamless_migration = qemu_opt_get_bool(opts, "seamless-migration", 0);
796 spice_server_set_seamless_migration(spice_server, seamless_migration);
797 if (spice_server_init(spice_server, &core_interface) != 0) {
798 error_report("failed to initialize spice server");
799 exit(1);
801 using_spice = 1;
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);
808 qemu_spice_input_init();
809 qemu_spice_audio_init();
811 qemu_add_vm_change_state_handler(vm_change_state_handler, NULL);
812 qemu_spice_display_stop();
814 g_free(x509_key_file);
815 g_free(x509_cert_file);
816 g_free(x509_cacert_file);
818 #if SPICE_SERVER_VERSION >= 0x000c02
819 qemu_spice_register_ports();
820 #endif
823 int qemu_spice_add_interface(SpiceBaseInstance *sin)
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);
831 * Create a spice server instance.
832 * It does *not* listen on the network.
833 * It handles QXL local rendering only.
835 * With a command line like '-vnc :0 -vga qxl' you'll end up here.
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);
843 return spice_server_add_interface(spice_server, sin);
846 static GSList *spice_consoles;
848 bool qemu_spice_have_display_interface(QemuConsole *con)
850 if (g_slist_find(spice_consoles, con)) {
851 return true;
853 return false;
856 int qemu_spice_add_display_interface(QXLInstance *qxlin, QemuConsole *con)
858 if (g_slist_find(spice_consoles, con)) {
859 return -1;
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);
866 static int qemu_spice_set_ticket(bool fail_if_conn, bool disconnect_if_conn)
868 time_t lifetime, now = time(NULL);
869 char *passwd;
871 if (now < auth_expires) {
872 passwd = auth_passwd;
873 lifetime = (auth_expires - now);
874 if (lifetime > INT_MAX) {
875 lifetime = INT_MAX;
877 } else {
878 passwd = NULL;
879 lifetime = 1;
881 return spice_server_set_ticket(spice_server, passwd, lifetime,
882 fail_if_conn, disconnect_if_conn);
885 int qemu_spice_set_passwd(const char *passwd,
886 bool fail_if_conn, bool disconnect_if_conn)
888 if (strcmp(auth, "spice") != 0) {
889 return -1;
892 g_free(auth_passwd);
893 auth_passwd = g_strdup(passwd);
894 return qemu_spice_set_ticket(fail_if_conn, disconnect_if_conn);
897 int qemu_spice_set_pw_expire(time_t expires)
899 auth_expires = expires;
900 return qemu_spice_set_ticket(false, false);
903 int qemu_spice_display_add_client(int csock, int skipauth, int tls)
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);
912 void qemu_spice_display_start(void)
914 spice_display_is_running = true;
915 spice_server_vm_start(spice_server);
918 void qemu_spice_display_stop(void)
920 spice_server_vm_stop(spice_server);
921 spice_display_is_running = false;
924 int qemu_spice_display_is_running(SimpleSpiceDisplay *ssd)
926 return spice_display_is_running;
929 static void spice_register_config(void)
931 qemu_add_opts(&qemu_spice_opts);
933 machine_init(spice_register_config);