tcg/aarch64: add ext argument to tcg_out_insn_3310
[qemu.git] / ui / spice-core.c
blobbf4fd07499e29875339778de667b05276ee3470d
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));
203 server->base = g_malloc0(sizeof(*server->base));
204 client->base = g_malloc0(sizeof(*client->base));
207 * Spice server might have called us from spice worker thread
208 * context (happens on display channel disconnects). Spice should
209 * not do that. It isn't that easy to fix it in spice and even
210 * when it is fixed we still should cover the already released
211 * spice versions. So detect that we've been called from another
212 * thread and grab the iothread lock if so before calling qemu
213 * functions.
215 bool need_lock = !qemu_thread_is_self(&me);
216 if (need_lock) {
217 qemu_mutex_lock_iothread();
220 if (info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT) {
221 add_addr_info(client->base, (struct sockaddr *)&info->paddr_ext,
222 info->plen_ext);
223 add_addr_info(server->base, (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(server->base, client->base, &error_abort);
233 break;
234 case SPICE_CHANNEL_EVENT_INITIALIZED:
235 if (auth) {
236 server->has_auth = true;
237 server->auth = g_strdup(auth);
239 add_channel_info(client, info);
240 channel_list_add(info);
241 qapi_event_send_spice_initialized(server, client, &error_abort);
242 break;
243 case SPICE_CHANNEL_EVENT_DISCONNECTED:
244 channel_list_del(info);
245 qapi_event_send_spice_disconnected(server->base, client->base, &error_abort);
246 break;
247 default:
248 break;
251 if (need_lock) {
252 qemu_mutex_unlock_iothread();
255 qapi_free_SpiceServerInfo(server);
256 qapi_free_SpiceChannel(client);
259 static SpiceCoreInterface core_interface = {
260 .base.type = SPICE_INTERFACE_CORE,
261 .base.description = "qemu core services",
262 .base.major_version = SPICE_INTERFACE_CORE_MAJOR,
263 .base.minor_version = SPICE_INTERFACE_CORE_MINOR,
265 .timer_add = timer_add,
266 .timer_start = timer_start,
267 .timer_cancel = timer_cancel,
268 .timer_remove = timer_remove,
270 .watch_add = watch_add,
271 .watch_update_mask = watch_update_mask,
272 .watch_remove = watch_remove,
274 .channel_event = channel_event,
277 static void migrate_connect_complete_cb(SpiceMigrateInstance *sin);
278 static void migrate_end_complete_cb(SpiceMigrateInstance *sin);
280 static const SpiceMigrateInterface migrate_interface = {
281 .base.type = SPICE_INTERFACE_MIGRATION,
282 .base.description = "migration",
283 .base.major_version = SPICE_INTERFACE_MIGRATION_MAJOR,
284 .base.minor_version = SPICE_INTERFACE_MIGRATION_MINOR,
285 .migrate_connect_complete = migrate_connect_complete_cb,
286 .migrate_end_complete = migrate_end_complete_cb,
289 static SpiceMigrateInstance spice_migrate;
291 static void migrate_connect_complete_cb(SpiceMigrateInstance *sin)
293 /* nothing, but libspice-server expects this cb being present. */
296 static void migrate_end_complete_cb(SpiceMigrateInstance *sin)
298 qapi_event_send_spice_migrate_completed(&error_abort);
299 spice_migration_completed = true;
302 /* config string parsing */
304 static int name2enum(const char *string, const char *table[], int entries)
306 int i;
308 if (string) {
309 for (i = 0; i < entries; i++) {
310 if (!table[i]) {
311 continue;
313 if (strcmp(string, table[i]) != 0) {
314 continue;
316 return i;
319 return -1;
322 static int parse_name(const char *string, const char *optname,
323 const char *table[], int entries)
325 int value = name2enum(string, table, entries);
327 if (value != -1) {
328 return value;
330 error_report("spice: invalid %s: %s", optname, string);
331 exit(1);
334 static const char *stream_video_names[] = {
335 [ SPICE_STREAM_VIDEO_OFF ] = "off",
336 [ SPICE_STREAM_VIDEO_ALL ] = "all",
337 [ SPICE_STREAM_VIDEO_FILTER ] = "filter",
339 #define parse_stream_video(_name) \
340 parse_name(_name, "stream video control", \
341 stream_video_names, ARRAY_SIZE(stream_video_names))
343 static const char *compression_names[] = {
344 [ SPICE_IMAGE_COMPRESS_OFF ] = "off",
345 [ SPICE_IMAGE_COMPRESS_AUTO_GLZ ] = "auto_glz",
346 [ SPICE_IMAGE_COMPRESS_AUTO_LZ ] = "auto_lz",
347 [ SPICE_IMAGE_COMPRESS_QUIC ] = "quic",
348 [ SPICE_IMAGE_COMPRESS_GLZ ] = "glz",
349 [ SPICE_IMAGE_COMPRESS_LZ ] = "lz",
351 #define parse_compression(_name) \
352 parse_name(_name, "image compression", \
353 compression_names, ARRAY_SIZE(compression_names))
355 static const char *wan_compression_names[] = {
356 [ SPICE_WAN_COMPRESSION_AUTO ] = "auto",
357 [ SPICE_WAN_COMPRESSION_NEVER ] = "never",
358 [ SPICE_WAN_COMPRESSION_ALWAYS ] = "always",
360 #define parse_wan_compression(_name) \
361 parse_name(_name, "wan compression", \
362 wan_compression_names, ARRAY_SIZE(wan_compression_names))
364 /* functions for the rest of qemu */
366 static SpiceChannelList *qmp_query_spice_channels(void)
368 SpiceChannelList *cur_item = NULL, *head = NULL;
369 ChannelList *item;
371 QTAILQ_FOREACH(item, &channel_list, link) {
372 SpiceChannelList *chan;
373 char host[NI_MAXHOST], port[NI_MAXSERV];
374 struct sockaddr *paddr;
375 socklen_t plen;
377 assert(item->info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT);
379 chan = g_malloc0(sizeof(*chan));
380 chan->value = g_malloc0(sizeof(*chan->value));
381 chan->value->base = g_malloc0(sizeof(*chan->value->base));
383 paddr = (struct sockaddr *)&item->info->paddr_ext;
384 plen = item->info->plen_ext;
385 getnameinfo(paddr, plen,
386 host, sizeof(host), port, sizeof(port),
387 NI_NUMERICHOST | NI_NUMERICSERV);
388 chan->value->base->host = g_strdup(host);
389 chan->value->base->port = g_strdup(port);
390 chan->value->base->family = inet_netfamily(paddr->sa_family);
392 chan->value->connection_id = item->info->connection_id;
393 chan->value->channel_type = item->info->type;
394 chan->value->channel_id = item->info->id;
395 chan->value->tls = item->info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS;
397 /* XXX: waiting for the qapi to support GSList */
398 if (!cur_item) {
399 head = cur_item = chan;
400 } else {
401 cur_item->next = chan;
402 cur_item = chan;
406 return head;
409 static QemuOptsList qemu_spice_opts = {
410 .name = "spice",
411 .head = QTAILQ_HEAD_INITIALIZER(qemu_spice_opts.head),
412 .desc = {
414 .name = "port",
415 .type = QEMU_OPT_NUMBER,
417 .name = "tls-port",
418 .type = QEMU_OPT_NUMBER,
420 .name = "addr",
421 .type = QEMU_OPT_STRING,
423 .name = "ipv4",
424 .type = QEMU_OPT_BOOL,
426 .name = "ipv6",
427 .type = QEMU_OPT_BOOL,
428 #ifdef SPICE_ADDR_FLAG_UNIX_ONLY
430 .name = "unix",
431 .type = QEMU_OPT_BOOL,
432 #endif
434 .name = "password",
435 .type = QEMU_OPT_STRING,
437 .name = "disable-ticketing",
438 .type = QEMU_OPT_BOOL,
440 .name = "disable-copy-paste",
441 .type = QEMU_OPT_BOOL,
443 .name = "disable-agent-file-xfer",
444 .type = QEMU_OPT_BOOL,
446 .name = "sasl",
447 .type = QEMU_OPT_BOOL,
449 .name = "x509-dir",
450 .type = QEMU_OPT_STRING,
452 .name = "x509-key-file",
453 .type = QEMU_OPT_STRING,
455 .name = "x509-key-password",
456 .type = QEMU_OPT_STRING,
458 .name = "x509-cert-file",
459 .type = QEMU_OPT_STRING,
461 .name = "x509-cacert-file",
462 .type = QEMU_OPT_STRING,
464 .name = "x509-dh-key-file",
465 .type = QEMU_OPT_STRING,
467 .name = "tls-ciphers",
468 .type = QEMU_OPT_STRING,
470 .name = "tls-channel",
471 .type = QEMU_OPT_STRING,
473 .name = "plaintext-channel",
474 .type = QEMU_OPT_STRING,
476 .name = "image-compression",
477 .type = QEMU_OPT_STRING,
479 .name = "jpeg-wan-compression",
480 .type = QEMU_OPT_STRING,
482 .name = "zlib-glz-wan-compression",
483 .type = QEMU_OPT_STRING,
485 .name = "streaming-video",
486 .type = QEMU_OPT_STRING,
488 .name = "agent-mouse",
489 .type = QEMU_OPT_BOOL,
491 .name = "playback-compression",
492 .type = QEMU_OPT_BOOL,
493 }, {
494 .name = "seamless-migration",
495 .type = QEMU_OPT_BOOL,
497 { /* end of list */ }
501 SpiceInfo *qmp_query_spice(Error **errp)
503 QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
504 int port, tls_port;
505 const char *addr;
506 SpiceInfo *info;
507 unsigned int major;
508 unsigned int minor;
509 unsigned int micro;
511 info = g_malloc0(sizeof(*info));
513 if (!spice_server || !opts) {
514 info->enabled = false;
515 return info;
518 info->enabled = true;
519 info->migrated = spice_migration_completed;
521 addr = qemu_opt_get(opts, "addr");
522 port = qemu_opt_get_number(opts, "port", 0);
523 tls_port = qemu_opt_get_number(opts, "tls-port", 0);
525 info->has_auth = true;
526 info->auth = g_strdup(auth);
528 info->has_host = true;
529 info->host = g_strdup(addr ? addr : "*");
531 info->has_compiled_version = true;
532 major = (SPICE_SERVER_VERSION & 0xff0000) >> 16;
533 minor = (SPICE_SERVER_VERSION & 0xff00) >> 8;
534 micro = SPICE_SERVER_VERSION & 0xff;
535 info->compiled_version = g_strdup_printf("%d.%d.%d", major, minor, micro);
537 if (port) {
538 info->has_port = true;
539 info->port = port;
541 if (tls_port) {
542 info->has_tls_port = true;
543 info->tls_port = tls_port;
546 info->mouse_mode = spice_server_is_server_mouse(spice_server) ?
547 SPICE_QUERY_MOUSE_MODE_SERVER :
548 SPICE_QUERY_MOUSE_MODE_CLIENT;
550 /* for compatibility with the original command */
551 info->has_channels = true;
552 info->channels = qmp_query_spice_channels();
554 return info;
557 static void migration_state_notifier(Notifier *notifier, void *data)
559 MigrationState *s = data;
561 if (!spice_have_target_host) {
562 return;
565 if (migration_in_setup(s)) {
566 spice_server_migrate_start(spice_server);
567 } else if (migration_has_finished(s)) {
568 spice_server_migrate_end(spice_server, true);
569 spice_have_target_host = false;
570 } else if (migration_has_failed(s)) {
571 spice_server_migrate_end(spice_server, false);
572 spice_have_target_host = false;
576 int qemu_spice_migrate_info(const char *hostname, int port, int tls_port,
577 const char *subject)
579 int ret;
581 ret = spice_server_migrate_connect(spice_server, hostname,
582 port, tls_port, subject);
583 spice_have_target_host = true;
584 return ret;
587 static int add_channel(void *opaque, const char *name, const char *value,
588 Error **errp)
590 int security = 0;
591 int rc;
593 if (strcmp(name, "tls-channel") == 0) {
594 int *tls_port = opaque;
595 if (!*tls_port) {
596 error_report("spice: tried to setup tls-channel"
597 " without specifying a TLS port");
598 exit(1);
600 security = SPICE_CHANNEL_SECURITY_SSL;
602 if (strcmp(name, "plaintext-channel") == 0) {
603 security = SPICE_CHANNEL_SECURITY_NONE;
605 if (security == 0) {
606 return 0;
608 if (strcmp(value, "default") == 0) {
609 rc = spice_server_set_channel_security(spice_server, NULL, security);
610 } else {
611 rc = spice_server_set_channel_security(spice_server, value, security);
613 if (rc != 0) {
614 error_report("spice: failed to set channel security for %s", value);
615 exit(1);
617 return 0;
620 static void vm_change_state_handler(void *opaque, int running,
621 RunState state)
623 if (running) {
624 qemu_spice_display_start();
625 } else {
626 qemu_spice_display_stop();
630 void qemu_spice_init(void)
632 QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
633 const char *password, *str, *x509_dir, *addr,
634 *x509_key_password = NULL,
635 *x509_dh_file = NULL,
636 *tls_ciphers = NULL;
637 char *x509_key_file = NULL,
638 *x509_cert_file = NULL,
639 *x509_cacert_file = NULL;
640 int port, tls_port, addr_flags;
641 spice_image_compression_t compression;
642 spice_wan_compression_t wan_compr;
643 bool seamless_migration;
645 qemu_thread_get_self(&me);
647 if (!opts) {
648 return;
650 port = qemu_opt_get_number(opts, "port", 0);
651 tls_port = qemu_opt_get_number(opts, "tls-port", 0);
652 if (port < 0 || port > 65535) {
653 error_report("spice port is out of range");
654 exit(1);
656 if (tls_port < 0 || tls_port > 65535) {
657 error_report("spice tls-port is out of range");
658 exit(1);
660 password = qemu_opt_get(opts, "password");
662 if (tls_port) {
663 x509_dir = qemu_opt_get(opts, "x509-dir");
664 if (!x509_dir) {
665 x509_dir = ".";
668 str = qemu_opt_get(opts, "x509-key-file");
669 if (str) {
670 x509_key_file = g_strdup(str);
671 } else {
672 x509_key_file = g_strdup_printf("%s/%s", x509_dir,
673 X509_SERVER_KEY_FILE);
676 str = qemu_opt_get(opts, "x509-cert-file");
677 if (str) {
678 x509_cert_file = g_strdup(str);
679 } else {
680 x509_cert_file = g_strdup_printf("%s/%s", x509_dir,
681 X509_SERVER_CERT_FILE);
684 str = qemu_opt_get(opts, "x509-cacert-file");
685 if (str) {
686 x509_cacert_file = g_strdup(str);
687 } else {
688 x509_cacert_file = g_strdup_printf("%s/%s", x509_dir,
689 X509_CA_CERT_FILE);
692 x509_key_password = qemu_opt_get(opts, "x509-key-password");
693 x509_dh_file = qemu_opt_get(opts, "x509-dh-key-file");
694 tls_ciphers = qemu_opt_get(opts, "tls-ciphers");
697 addr = qemu_opt_get(opts, "addr");
698 addr_flags = 0;
699 if (qemu_opt_get_bool(opts, "ipv4", 0)) {
700 addr_flags |= SPICE_ADDR_FLAG_IPV4_ONLY;
701 } else if (qemu_opt_get_bool(opts, "ipv6", 0)) {
702 addr_flags |= SPICE_ADDR_FLAG_IPV6_ONLY;
703 #ifdef SPICE_ADDR_FLAG_UNIX_ONLY
704 } else if (qemu_opt_get_bool(opts, "unix", 0)) {
705 addr_flags |= SPICE_ADDR_FLAG_UNIX_ONLY;
706 #endif
709 spice_server = spice_server_new();
710 spice_server_set_addr(spice_server, addr ? addr : "", addr_flags);
711 if (port) {
712 spice_server_set_port(spice_server, port);
714 if (tls_port) {
715 spice_server_set_tls(spice_server, tls_port,
716 x509_cacert_file,
717 x509_cert_file,
718 x509_key_file,
719 x509_key_password,
720 x509_dh_file,
721 tls_ciphers);
723 if (password) {
724 qemu_spice_set_passwd(password, false, false);
726 if (qemu_opt_get_bool(opts, "sasl", 0)) {
727 if (spice_server_set_sasl_appname(spice_server, "qemu") == -1 ||
728 spice_server_set_sasl(spice_server, 1) == -1) {
729 error_report("spice: failed to enable sasl");
730 exit(1);
732 auth = "sasl";
734 if (qemu_opt_get_bool(opts, "disable-ticketing", 0)) {
735 auth = "none";
736 spice_server_set_noauth(spice_server);
739 if (qemu_opt_get_bool(opts, "disable-copy-paste", 0)) {
740 spice_server_set_agent_copypaste(spice_server, false);
743 if (qemu_opt_get_bool(opts, "disable-agent-file-xfer", 0)) {
744 #if SPICE_SERVER_VERSION >= 0x000c04
745 spice_server_set_agent_file_xfer(spice_server, false);
746 #else
747 error_report("this qemu build does not support the "
748 "\"disable-agent-file-xfer\" option");
749 exit(1);
750 #endif
753 compression = SPICE_IMAGE_COMPRESS_AUTO_GLZ;
754 str = qemu_opt_get(opts, "image-compression");
755 if (str) {
756 compression = parse_compression(str);
758 spice_server_set_image_compression(spice_server, compression);
760 wan_compr = SPICE_WAN_COMPRESSION_AUTO;
761 str = qemu_opt_get(opts, "jpeg-wan-compression");
762 if (str) {
763 wan_compr = parse_wan_compression(str);
765 spice_server_set_jpeg_compression(spice_server, wan_compr);
767 wan_compr = SPICE_WAN_COMPRESSION_AUTO;
768 str = qemu_opt_get(opts, "zlib-glz-wan-compression");
769 if (str) {
770 wan_compr = parse_wan_compression(str);
772 spice_server_set_zlib_glz_compression(spice_server, wan_compr);
774 str = qemu_opt_get(opts, "streaming-video");
775 if (str) {
776 int streaming_video = parse_stream_video(str);
777 spice_server_set_streaming_video(spice_server, streaming_video);
778 } else {
779 spice_server_set_streaming_video(spice_server, SPICE_STREAM_VIDEO_OFF);
782 spice_server_set_agent_mouse
783 (spice_server, qemu_opt_get_bool(opts, "agent-mouse", 1));
784 spice_server_set_playback_compression
785 (spice_server, qemu_opt_get_bool(opts, "playback-compression", 1));
787 qemu_opt_foreach(opts, add_channel, &tls_port, NULL);
789 spice_server_set_name(spice_server, qemu_name);
790 spice_server_set_uuid(spice_server, qemu_uuid);
792 seamless_migration = qemu_opt_get_bool(opts, "seamless-migration", 0);
793 spice_server_set_seamless_migration(spice_server, seamless_migration);
794 if (spice_server_init(spice_server, &core_interface) != 0) {
795 error_report("failed to initialize spice server");
796 exit(1);
798 using_spice = 1;
800 migration_state.notify = migration_state_notifier;
801 add_migration_state_change_notifier(&migration_state);
802 spice_migrate.base.sif = &migrate_interface.base;
803 qemu_spice_add_interface(&spice_migrate.base);
805 qemu_spice_input_init();
806 qemu_spice_audio_init();
808 qemu_add_vm_change_state_handler(vm_change_state_handler, NULL);
809 qemu_spice_display_stop();
811 g_free(x509_key_file);
812 g_free(x509_cert_file);
813 g_free(x509_cacert_file);
815 #if SPICE_SERVER_VERSION >= 0x000c02
816 qemu_spice_register_ports();
817 #endif
820 int qemu_spice_add_interface(SpiceBaseInstance *sin)
822 if (!spice_server) {
823 if (QTAILQ_FIRST(&qemu_spice_opts.head) != NULL) {
824 error_report("Oops: spice configured but not active");
825 exit(1);
828 * Create a spice server instance.
829 * It does *not* listen on the network.
830 * It handles QXL local rendering only.
832 * With a command line like '-vnc :0 -vga qxl' you'll end up here.
834 spice_server = spice_server_new();
835 spice_server_set_sasl_appname(spice_server, "qemu");
836 spice_server_init(spice_server, &core_interface);
837 qemu_add_vm_change_state_handler(vm_change_state_handler, NULL);
840 return spice_server_add_interface(spice_server, sin);
843 static GSList *spice_consoles;
845 bool qemu_spice_have_display_interface(QemuConsole *con)
847 if (g_slist_find(spice_consoles, con)) {
848 return true;
850 return false;
853 int qemu_spice_add_display_interface(QXLInstance *qxlin, QemuConsole *con)
855 if (g_slist_find(spice_consoles, con)) {
856 return -1;
858 qxlin->id = qemu_console_get_index(con);
859 spice_consoles = g_slist_append(spice_consoles, con);
860 return qemu_spice_add_interface(&qxlin->base);
863 static int qemu_spice_set_ticket(bool fail_if_conn, bool disconnect_if_conn)
865 time_t lifetime, now = time(NULL);
866 char *passwd;
868 if (now < auth_expires) {
869 passwd = auth_passwd;
870 lifetime = (auth_expires - now);
871 if (lifetime > INT_MAX) {
872 lifetime = INT_MAX;
874 } else {
875 passwd = NULL;
876 lifetime = 1;
878 return spice_server_set_ticket(spice_server, passwd, lifetime,
879 fail_if_conn, disconnect_if_conn);
882 int qemu_spice_set_passwd(const char *passwd,
883 bool fail_if_conn, bool disconnect_if_conn)
885 if (strcmp(auth, "spice") != 0) {
886 return -1;
889 g_free(auth_passwd);
890 auth_passwd = g_strdup(passwd);
891 return qemu_spice_set_ticket(fail_if_conn, disconnect_if_conn);
894 int qemu_spice_set_pw_expire(time_t expires)
896 auth_expires = expires;
897 return qemu_spice_set_ticket(false, false);
900 int qemu_spice_display_add_client(int csock, int skipauth, int tls)
902 if (tls) {
903 return spice_server_add_ssl_client(spice_server, csock, skipauth);
904 } else {
905 return spice_server_add_client(spice_server, csock, skipauth);
909 void qemu_spice_display_start(void)
911 spice_display_is_running = true;
912 spice_server_vm_start(spice_server);
915 void qemu_spice_display_stop(void)
917 spice_server_vm_stop(spice_server);
918 spice_display_is_running = false;
921 int qemu_spice_display_is_running(SimpleSpiceDisplay *ssd)
923 return spice_display_is_running;
926 static void spice_register_config(void)
928 qemu_add_opts(&qemu_spice_opts);
930 machine_init(spice_register_config);