doc: Document user creatable object types in help text
[qemu/cris-port.git] / ui / spice-core.c
bloba30da3cf9fdc9955e2cd906316a01a41db8d165a
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/thread.h"
26 #include "qemu/timer.h"
27 #include "qemu/queue.h"
28 #include "qemu-x509.h"
29 #include "qemu/sockets.h"
30 #include "qmp-commands.h"
31 #include "qapi/qmp/qint.h"
32 #include "qapi/qmp/qbool.h"
33 #include "qapi/qmp/qstring.h"
34 #include "qapi/qmp/qjson.h"
35 #include "qemu/notify.h"
36 #include "migration/migration.h"
37 #include "hw/hw.h"
38 #include "ui/spice-display.h"
39 #include "qapi-event.h"
41 /* core bits */
43 static SpiceServer *spice_server;
44 static Notifier migration_state;
45 static const char *auth = "spice";
46 static char *auth_passwd;
47 static time_t auth_expires = TIME_MAX;
48 static int spice_migration_completed;
49 static int spice_display_is_running;
50 static int spice_have_target_host;
51 int using_spice = 0;
53 static QemuThread me;
55 struct SpiceTimer {
56 QEMUTimer *timer;
57 QTAILQ_ENTRY(SpiceTimer) next;
59 static QTAILQ_HEAD(, SpiceTimer) timers = QTAILQ_HEAD_INITIALIZER(timers);
61 static SpiceTimer *timer_add(SpiceTimerFunc func, void *opaque)
63 SpiceTimer *timer;
65 timer = g_malloc0(sizeof(*timer));
66 timer->timer = timer_new_ms(QEMU_CLOCK_REALTIME, func, opaque);
67 QTAILQ_INSERT_TAIL(&timers, timer, next);
68 return timer;
71 static void timer_start(SpiceTimer *timer, uint32_t ms)
73 timer_mod(timer->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + ms);
76 static void timer_cancel(SpiceTimer *timer)
78 timer_del(timer->timer);
81 static void timer_remove(SpiceTimer *timer)
83 timer_del(timer->timer);
84 timer_free(timer->timer);
85 QTAILQ_REMOVE(&timers, timer, next);
86 g_free(timer);
89 struct SpiceWatch {
90 int fd;
91 int event_mask;
92 SpiceWatchFunc func;
93 void *opaque;
94 QTAILQ_ENTRY(SpiceWatch) next;
96 static QTAILQ_HEAD(, SpiceWatch) watches = QTAILQ_HEAD_INITIALIZER(watches);
98 static void watch_read(void *opaque)
100 SpiceWatch *watch = opaque;
101 watch->func(watch->fd, SPICE_WATCH_EVENT_READ, watch->opaque);
104 static void watch_write(void *opaque)
106 SpiceWatch *watch = opaque;
107 watch->func(watch->fd, SPICE_WATCH_EVENT_WRITE, watch->opaque);
110 static void watch_update_mask(SpiceWatch *watch, int event_mask)
112 IOHandler *on_read = NULL;
113 IOHandler *on_write = NULL;
115 watch->event_mask = event_mask;
116 if (watch->event_mask & SPICE_WATCH_EVENT_READ) {
117 on_read = watch_read;
119 if (watch->event_mask & SPICE_WATCH_EVENT_WRITE) {
120 on_write = watch_write;
122 qemu_set_fd_handler(watch->fd, on_read, on_write, watch);
125 static SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque)
127 SpiceWatch *watch;
129 watch = g_malloc0(sizeof(*watch));
130 watch->fd = fd;
131 watch->func = func;
132 watch->opaque = opaque;
133 QTAILQ_INSERT_TAIL(&watches, watch, next);
135 watch_update_mask(watch, event_mask);
136 return watch;
139 static void watch_remove(SpiceWatch *watch)
141 qemu_set_fd_handler(watch->fd, NULL, NULL, NULL);
142 QTAILQ_REMOVE(&watches, watch, next);
143 g_free(watch);
146 typedef struct ChannelList ChannelList;
147 struct ChannelList {
148 SpiceChannelEventInfo *info;
149 QTAILQ_ENTRY(ChannelList) link;
151 static QTAILQ_HEAD(, ChannelList) channel_list = QTAILQ_HEAD_INITIALIZER(channel_list);
153 static void channel_list_add(SpiceChannelEventInfo *info)
155 ChannelList *item;
157 item = g_malloc0(sizeof(*item));
158 item->info = info;
159 QTAILQ_INSERT_TAIL(&channel_list, item, link);
162 static void channel_list_del(SpiceChannelEventInfo *info)
164 ChannelList *item;
166 QTAILQ_FOREACH(item, &channel_list, link) {
167 if (item->info != info) {
168 continue;
170 QTAILQ_REMOVE(&channel_list, item, link);
171 g_free(item);
172 return;
176 static void add_addr_info(SpiceBasicInfo *info, struct sockaddr *addr, int len)
178 char host[NI_MAXHOST], port[NI_MAXSERV];
180 getnameinfo(addr, len, host, sizeof(host), port, sizeof(port),
181 NI_NUMERICHOST | NI_NUMERICSERV);
183 info->host = g_strdup(host);
184 info->port = g_strdup(port);
185 info->family = inet_netfamily(addr->sa_family);
188 static void add_channel_info(SpiceChannel *sc, SpiceChannelEventInfo *info)
190 int tls = info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS;
192 sc->connection_id = info->connection_id;
193 sc->channel_type = info->type;
194 sc->channel_id = info->id;
195 sc->tls = !!tls;
198 static void channel_event(int event, SpiceChannelEventInfo *info)
200 SpiceServerInfo *server = g_malloc0(sizeof(*server));
201 SpiceChannel *client = g_malloc0(sizeof(*client));
202 server->base = g_malloc0(sizeof(*server->base));
203 client->base = g_malloc0(sizeof(*client->base));
206 * Spice server might have called us from spice worker thread
207 * context (happens on display channel disconnects). Spice should
208 * not do that. It isn't that easy to fix it in spice and even
209 * when it is fixed we still should cover the already released
210 * spice versions. So detect that we've been called from another
211 * thread and grab the iothread lock if so before calling qemu
212 * functions.
214 bool need_lock = !qemu_thread_is_self(&me);
215 if (need_lock) {
216 qemu_mutex_lock_iothread();
219 if (info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT) {
220 add_addr_info(client->base, (struct sockaddr *)&info->paddr_ext,
221 info->plen_ext);
222 add_addr_info(server->base, (struct sockaddr *)&info->laddr_ext,
223 info->llen_ext);
224 } else {
225 error_report("spice: %s, extended address is expected",
226 __func__);
229 switch (event) {
230 case SPICE_CHANNEL_EVENT_CONNECTED:
231 qapi_event_send_spice_connected(server->base, client->base, &error_abort);
232 break;
233 case SPICE_CHANNEL_EVENT_INITIALIZED:
234 if (auth) {
235 server->has_auth = true;
236 server->auth = g_strdup(auth);
238 add_channel_info(client, info);
239 channel_list_add(info);
240 qapi_event_send_spice_initialized(server, client, &error_abort);
241 break;
242 case SPICE_CHANNEL_EVENT_DISCONNECTED:
243 channel_list_del(info);
244 qapi_event_send_spice_disconnected(server->base, client->base, &error_abort);
245 break;
246 default:
247 break;
250 if (need_lock) {
251 qemu_mutex_unlock_iothread();
254 qapi_free_SpiceServerInfo(server);
255 qapi_free_SpiceChannel(client);
258 static SpiceCoreInterface core_interface = {
259 .base.type = SPICE_INTERFACE_CORE,
260 .base.description = "qemu core services",
261 .base.major_version = SPICE_INTERFACE_CORE_MAJOR,
262 .base.minor_version = SPICE_INTERFACE_CORE_MINOR,
264 .timer_add = timer_add,
265 .timer_start = timer_start,
266 .timer_cancel = timer_cancel,
267 .timer_remove = timer_remove,
269 .watch_add = watch_add,
270 .watch_update_mask = watch_update_mask,
271 .watch_remove = watch_remove,
273 .channel_event = channel_event,
276 static void migrate_connect_complete_cb(SpiceMigrateInstance *sin);
277 static void migrate_end_complete_cb(SpiceMigrateInstance *sin);
279 static const SpiceMigrateInterface migrate_interface = {
280 .base.type = SPICE_INTERFACE_MIGRATION,
281 .base.description = "migration",
282 .base.major_version = SPICE_INTERFACE_MIGRATION_MAJOR,
283 .base.minor_version = SPICE_INTERFACE_MIGRATION_MINOR,
284 .migrate_connect_complete = migrate_connect_complete_cb,
285 .migrate_end_complete = migrate_end_complete_cb,
288 static SpiceMigrateInstance spice_migrate;
290 static void migrate_connect_complete_cb(SpiceMigrateInstance *sin)
292 /* nothing, but libspice-server expects this cb being present. */
295 static void migrate_end_complete_cb(SpiceMigrateInstance *sin)
297 qapi_event_send_spice_migrate_completed(&error_abort);
298 spice_migration_completed = true;
301 /* config string parsing */
303 static int name2enum(const char *string, const char *table[], int entries)
305 int i;
307 if (string) {
308 for (i = 0; i < entries; i++) {
309 if (!table[i]) {
310 continue;
312 if (strcmp(string, table[i]) != 0) {
313 continue;
315 return i;
318 return -1;
321 static int parse_name(const char *string, const char *optname,
322 const char *table[], int entries)
324 int value = name2enum(string, table, entries);
326 if (value != -1) {
327 return value;
329 error_report("spice: invalid %s: %s", optname, string);
330 exit(1);
333 static const char *stream_video_names[] = {
334 [ SPICE_STREAM_VIDEO_OFF ] = "off",
335 [ SPICE_STREAM_VIDEO_ALL ] = "all",
336 [ SPICE_STREAM_VIDEO_FILTER ] = "filter",
338 #define parse_stream_video(_name) \
339 parse_name(_name, "stream video control", \
340 stream_video_names, ARRAY_SIZE(stream_video_names))
342 static const char *compression_names[] = {
343 [ SPICE_IMAGE_COMPRESS_OFF ] = "off",
344 [ SPICE_IMAGE_COMPRESS_AUTO_GLZ ] = "auto_glz",
345 [ SPICE_IMAGE_COMPRESS_AUTO_LZ ] = "auto_lz",
346 [ SPICE_IMAGE_COMPRESS_QUIC ] = "quic",
347 [ SPICE_IMAGE_COMPRESS_GLZ ] = "glz",
348 [ SPICE_IMAGE_COMPRESS_LZ ] = "lz",
350 #define parse_compression(_name) \
351 parse_name(_name, "image compression", \
352 compression_names, ARRAY_SIZE(compression_names))
354 static const char *wan_compression_names[] = {
355 [ SPICE_WAN_COMPRESSION_AUTO ] = "auto",
356 [ SPICE_WAN_COMPRESSION_NEVER ] = "never",
357 [ SPICE_WAN_COMPRESSION_ALWAYS ] = "always",
359 #define parse_wan_compression(_name) \
360 parse_name(_name, "wan compression", \
361 wan_compression_names, ARRAY_SIZE(wan_compression_names))
363 /* functions for the rest of qemu */
365 static SpiceChannelList *qmp_query_spice_channels(void)
367 SpiceChannelList *cur_item = NULL, *head = NULL;
368 ChannelList *item;
370 QTAILQ_FOREACH(item, &channel_list, link) {
371 SpiceChannelList *chan;
372 char host[NI_MAXHOST], port[NI_MAXSERV];
373 struct sockaddr *paddr;
374 socklen_t plen;
376 assert(item->info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT);
378 chan = g_malloc0(sizeof(*chan));
379 chan->value = g_malloc0(sizeof(*chan->value));
380 chan->value->base = g_malloc0(sizeof(*chan->value->base));
382 paddr = (struct sockaddr *)&item->info->paddr_ext;
383 plen = item->info->plen_ext;
384 getnameinfo(paddr, plen,
385 host, sizeof(host), port, sizeof(port),
386 NI_NUMERICHOST | NI_NUMERICSERV);
387 chan->value->base->host = g_strdup(host);
388 chan->value->base->port = g_strdup(port);
389 chan->value->base->family = inet_netfamily(paddr->sa_family);
391 chan->value->connection_id = item->info->connection_id;
392 chan->value->channel_type = item->info->type;
393 chan->value->channel_id = item->info->id;
394 chan->value->tls = item->info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS;
396 /* XXX: waiting for the qapi to support GSList */
397 if (!cur_item) {
398 head = cur_item = chan;
399 } else {
400 cur_item->next = chan;
401 cur_item = chan;
405 return head;
408 static QemuOptsList qemu_spice_opts = {
409 .name = "spice",
410 .head = QTAILQ_HEAD_INITIALIZER(qemu_spice_opts.head),
411 .desc = {
413 .name = "port",
414 .type = QEMU_OPT_NUMBER,
416 .name = "tls-port",
417 .type = QEMU_OPT_NUMBER,
419 .name = "addr",
420 .type = QEMU_OPT_STRING,
422 .name = "ipv4",
423 .type = QEMU_OPT_BOOL,
425 .name = "ipv6",
426 .type = QEMU_OPT_BOOL,
427 #ifdef SPICE_ADDR_FLAG_UNIX_ONLY
429 .name = "unix",
430 .type = QEMU_OPT_BOOL,
431 #endif
433 .name = "password",
434 .type = QEMU_OPT_STRING,
436 .name = "disable-ticketing",
437 .type = QEMU_OPT_BOOL,
439 .name = "disable-copy-paste",
440 .type = QEMU_OPT_BOOL,
442 .name = "disable-agent-file-xfer",
443 .type = QEMU_OPT_BOOL,
445 .name = "sasl",
446 .type = QEMU_OPT_BOOL,
448 .name = "x509-dir",
449 .type = QEMU_OPT_STRING,
451 .name = "x509-key-file",
452 .type = QEMU_OPT_STRING,
454 .name = "x509-key-password",
455 .type = QEMU_OPT_STRING,
457 .name = "x509-cert-file",
458 .type = QEMU_OPT_STRING,
460 .name = "x509-cacert-file",
461 .type = QEMU_OPT_STRING,
463 .name = "x509-dh-key-file",
464 .type = QEMU_OPT_STRING,
466 .name = "tls-ciphers",
467 .type = QEMU_OPT_STRING,
469 .name = "tls-channel",
470 .type = QEMU_OPT_STRING,
472 .name = "plaintext-channel",
473 .type = QEMU_OPT_STRING,
475 .name = "image-compression",
476 .type = QEMU_OPT_STRING,
478 .name = "jpeg-wan-compression",
479 .type = QEMU_OPT_STRING,
481 .name = "zlib-glz-wan-compression",
482 .type = QEMU_OPT_STRING,
484 .name = "streaming-video",
485 .type = QEMU_OPT_STRING,
487 .name = "agent-mouse",
488 .type = QEMU_OPT_BOOL,
490 .name = "playback-compression",
491 .type = QEMU_OPT_BOOL,
492 }, {
493 .name = "seamless-migration",
494 .type = QEMU_OPT_BOOL,
496 { /* end of list */ }
500 SpiceInfo *qmp_query_spice(Error **errp)
502 QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
503 int port, tls_port;
504 const char *addr;
505 SpiceInfo *info;
506 unsigned int major;
507 unsigned int minor;
508 unsigned int micro;
510 info = g_malloc0(sizeof(*info));
512 if (!spice_server || !opts) {
513 info->enabled = false;
514 return info;
517 info->enabled = true;
518 info->migrated = spice_migration_completed;
520 addr = qemu_opt_get(opts, "addr");
521 port = qemu_opt_get_number(opts, "port", 0);
522 tls_port = qemu_opt_get_number(opts, "tls-port", 0);
524 info->has_auth = true;
525 info->auth = g_strdup(auth);
527 info->has_host = true;
528 info->host = g_strdup(addr ? addr : "*");
530 info->has_compiled_version = true;
531 major = (SPICE_SERVER_VERSION & 0xff0000) >> 16;
532 minor = (SPICE_SERVER_VERSION & 0xff00) >> 8;
533 micro = SPICE_SERVER_VERSION & 0xff;
534 info->compiled_version = g_strdup_printf("%d.%d.%d", major, minor, micro);
536 if (port) {
537 info->has_port = true;
538 info->port = port;
540 if (tls_port) {
541 info->has_tls_port = true;
542 info->tls_port = tls_port;
545 info->mouse_mode = spice_server_is_server_mouse(spice_server) ?
546 SPICE_QUERY_MOUSE_MODE_SERVER :
547 SPICE_QUERY_MOUSE_MODE_CLIENT;
549 /* for compatibility with the original command */
550 info->has_channels = true;
551 info->channels = qmp_query_spice_channels();
553 return info;
556 static void migration_state_notifier(Notifier *notifier, void *data)
558 MigrationState *s = data;
560 if (!spice_have_target_host) {
561 return;
564 if (migration_in_setup(s)) {
565 spice_server_migrate_start(spice_server);
566 } else if (migration_has_finished(s)) {
567 spice_server_migrate_end(spice_server, true);
568 spice_have_target_host = false;
569 } else if (migration_has_failed(s)) {
570 spice_server_migrate_end(spice_server, false);
571 spice_have_target_host = false;
575 int qemu_spice_migrate_info(const char *hostname, int port, int tls_port,
576 const char *subject)
578 int ret;
580 ret = spice_server_migrate_connect(spice_server, hostname,
581 port, tls_port, subject);
582 spice_have_target_host = true;
583 return ret;
586 static int add_channel(void *opaque, const char *name, const char *value,
587 Error **errp)
589 int security = 0;
590 int rc;
592 if (strcmp(name, "tls-channel") == 0) {
593 int *tls_port = opaque;
594 if (!*tls_port) {
595 error_report("spice: tried to setup tls-channel"
596 " without specifying a TLS port");
597 exit(1);
599 security = SPICE_CHANNEL_SECURITY_SSL;
601 if (strcmp(name, "plaintext-channel") == 0) {
602 security = SPICE_CHANNEL_SECURITY_NONE;
604 if (security == 0) {
605 return 0;
607 if (strcmp(value, "default") == 0) {
608 rc = spice_server_set_channel_security(spice_server, NULL, security);
609 } else {
610 rc = spice_server_set_channel_security(spice_server, value, security);
612 if (rc != 0) {
613 error_report("spice: failed to set channel security for %s", value);
614 exit(1);
616 return 0;
619 static void vm_change_state_handler(void *opaque, int running,
620 RunState state)
622 if (running) {
623 qemu_spice_display_start();
624 } else {
625 qemu_spice_display_stop();
629 void qemu_spice_init(void)
631 QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
632 const char *password, *str, *x509_dir, *addr,
633 *x509_key_password = NULL,
634 *x509_dh_file = NULL,
635 *tls_ciphers = NULL;
636 char *x509_key_file = NULL,
637 *x509_cert_file = NULL,
638 *x509_cacert_file = NULL;
639 int port, tls_port, addr_flags;
640 spice_image_compression_t compression;
641 spice_wan_compression_t wan_compr;
642 bool seamless_migration;
644 qemu_thread_get_self(&me);
646 if (!opts) {
647 return;
649 port = qemu_opt_get_number(opts, "port", 0);
650 tls_port = qemu_opt_get_number(opts, "tls-port", 0);
651 if (port < 0 || port > 65535) {
652 error_report("spice port is out of range");
653 exit(1);
655 if (tls_port < 0 || tls_port > 65535) {
656 error_report("spice tls-port is out of range");
657 exit(1);
659 password = qemu_opt_get(opts, "password");
661 if (tls_port) {
662 x509_dir = qemu_opt_get(opts, "x509-dir");
663 if (!x509_dir) {
664 x509_dir = ".";
667 str = qemu_opt_get(opts, "x509-key-file");
668 if (str) {
669 x509_key_file = g_strdup(str);
670 } else {
671 x509_key_file = g_strdup_printf("%s/%s", x509_dir,
672 X509_SERVER_KEY_FILE);
675 str = qemu_opt_get(opts, "x509-cert-file");
676 if (str) {
677 x509_cert_file = g_strdup(str);
678 } else {
679 x509_cert_file = g_strdup_printf("%s/%s", x509_dir,
680 X509_SERVER_CERT_FILE);
683 str = qemu_opt_get(opts, "x509-cacert-file");
684 if (str) {
685 x509_cacert_file = g_strdup(str);
686 } else {
687 x509_cacert_file = g_strdup_printf("%s/%s", x509_dir,
688 X509_CA_CERT_FILE);
691 x509_key_password = qemu_opt_get(opts, "x509-key-password");
692 x509_dh_file = qemu_opt_get(opts, "x509-dh-key-file");
693 tls_ciphers = qemu_opt_get(opts, "tls-ciphers");
696 addr = qemu_opt_get(opts, "addr");
697 addr_flags = 0;
698 if (qemu_opt_get_bool(opts, "ipv4", 0)) {
699 addr_flags |= SPICE_ADDR_FLAG_IPV4_ONLY;
700 } else if (qemu_opt_get_bool(opts, "ipv6", 0)) {
701 addr_flags |= SPICE_ADDR_FLAG_IPV6_ONLY;
702 #ifdef SPICE_ADDR_FLAG_UNIX_ONLY
703 } else if (qemu_opt_get_bool(opts, "unix", 0)) {
704 addr_flags |= SPICE_ADDR_FLAG_UNIX_ONLY;
705 #endif
708 spice_server = spice_server_new();
709 spice_server_set_addr(spice_server, addr ? addr : "", addr_flags);
710 if (port) {
711 spice_server_set_port(spice_server, port);
713 if (tls_port) {
714 spice_server_set_tls(spice_server, tls_port,
715 x509_cacert_file,
716 x509_cert_file,
717 x509_key_file,
718 x509_key_password,
719 x509_dh_file,
720 tls_ciphers);
722 if (password) {
723 qemu_spice_set_passwd(password, false, false);
725 if (qemu_opt_get_bool(opts, "sasl", 0)) {
726 if (spice_server_set_sasl_appname(spice_server, "qemu") == -1 ||
727 spice_server_set_sasl(spice_server, 1) == -1) {
728 error_report("spice: failed to enable sasl");
729 exit(1);
731 auth = "sasl";
733 if (qemu_opt_get_bool(opts, "disable-ticketing", 0)) {
734 auth = "none";
735 spice_server_set_noauth(spice_server);
738 if (qemu_opt_get_bool(opts, "disable-copy-paste", 0)) {
739 spice_server_set_agent_copypaste(spice_server, false);
742 if (qemu_opt_get_bool(opts, "disable-agent-file-xfer", 0)) {
743 #if SPICE_SERVER_VERSION >= 0x000c04
744 spice_server_set_agent_file_xfer(spice_server, false);
745 #else
746 error_report("this qemu build does not support the "
747 "\"disable-agent-file-xfer\" option");
748 exit(1);
749 #endif
752 compression = SPICE_IMAGE_COMPRESS_AUTO_GLZ;
753 str = qemu_opt_get(opts, "image-compression");
754 if (str) {
755 compression = parse_compression(str);
757 spice_server_set_image_compression(spice_server, compression);
759 wan_compr = SPICE_WAN_COMPRESSION_AUTO;
760 str = qemu_opt_get(opts, "jpeg-wan-compression");
761 if (str) {
762 wan_compr = parse_wan_compression(str);
764 spice_server_set_jpeg_compression(spice_server, wan_compr);
766 wan_compr = SPICE_WAN_COMPRESSION_AUTO;
767 str = qemu_opt_get(opts, "zlib-glz-wan-compression");
768 if (str) {
769 wan_compr = parse_wan_compression(str);
771 spice_server_set_zlib_glz_compression(spice_server, wan_compr);
773 str = qemu_opt_get(opts, "streaming-video");
774 if (str) {
775 int streaming_video = parse_stream_video(str);
776 spice_server_set_streaming_video(spice_server, streaming_video);
777 } else {
778 spice_server_set_streaming_video(spice_server, SPICE_STREAM_VIDEO_OFF);
781 spice_server_set_agent_mouse
782 (spice_server, qemu_opt_get_bool(opts, "agent-mouse", 1));
783 spice_server_set_playback_compression
784 (spice_server, qemu_opt_get_bool(opts, "playback-compression", 1));
786 qemu_opt_foreach(opts, add_channel, &tls_port, NULL);
788 spice_server_set_name(spice_server, qemu_name);
789 spice_server_set_uuid(spice_server, qemu_uuid);
791 seamless_migration = qemu_opt_get_bool(opts, "seamless-migration", 0);
792 spice_server_set_seamless_migration(spice_server, seamless_migration);
793 if (spice_server_init(spice_server, &core_interface) != 0) {
794 error_report("failed to initialize spice server");
795 exit(1);
797 using_spice = 1;
799 migration_state.notify = migration_state_notifier;
800 add_migration_state_change_notifier(&migration_state);
801 spice_migrate.base.sif = &migrate_interface.base;
802 qemu_spice_add_interface(&spice_migrate.base);
804 qemu_spice_input_init();
805 qemu_spice_audio_init();
807 qemu_add_vm_change_state_handler(vm_change_state_handler, NULL);
808 qemu_spice_display_stop();
810 g_free(x509_key_file);
811 g_free(x509_cert_file);
812 g_free(x509_cacert_file);
814 #if SPICE_SERVER_VERSION >= 0x000c02
815 qemu_spice_register_ports();
816 #endif
819 int qemu_spice_add_interface(SpiceBaseInstance *sin)
821 if (!spice_server) {
822 if (QTAILQ_FIRST(&qemu_spice_opts.head) != NULL) {
823 error_report("Oops: spice configured but not active");
824 exit(1);
827 * Create a spice server instance.
828 * It does *not* listen on the network.
829 * It handles QXL local rendering only.
831 * With a command line like '-vnc :0 -vga qxl' you'll end up here.
833 spice_server = spice_server_new();
834 spice_server_set_sasl_appname(spice_server, "qemu");
835 spice_server_init(spice_server, &core_interface);
836 qemu_add_vm_change_state_handler(vm_change_state_handler, NULL);
839 return spice_server_add_interface(spice_server, sin);
842 static GSList *spice_consoles;
844 bool qemu_spice_have_display_interface(QemuConsole *con)
846 if (g_slist_find(spice_consoles, con)) {
847 return true;
849 return false;
852 int qemu_spice_add_display_interface(QXLInstance *qxlin, QemuConsole *con)
854 if (g_slist_find(spice_consoles, con)) {
855 return -1;
857 qxlin->id = qemu_console_get_index(con);
858 spice_consoles = g_slist_append(spice_consoles, con);
859 return qemu_spice_add_interface(&qxlin->base);
862 static int qemu_spice_set_ticket(bool fail_if_conn, bool disconnect_if_conn)
864 time_t lifetime, now = time(NULL);
865 char *passwd;
867 if (now < auth_expires) {
868 passwd = auth_passwd;
869 lifetime = (auth_expires - now);
870 if (lifetime > INT_MAX) {
871 lifetime = INT_MAX;
873 } else {
874 passwd = NULL;
875 lifetime = 1;
877 return spice_server_set_ticket(spice_server, passwd, lifetime,
878 fail_if_conn, disconnect_if_conn);
881 int qemu_spice_set_passwd(const char *passwd,
882 bool fail_if_conn, bool disconnect_if_conn)
884 if (strcmp(auth, "spice") != 0) {
885 return -1;
888 g_free(auth_passwd);
889 auth_passwd = g_strdup(passwd);
890 return qemu_spice_set_ticket(fail_if_conn, disconnect_if_conn);
893 int qemu_spice_set_pw_expire(time_t expires)
895 auth_expires = expires;
896 return qemu_spice_set_ticket(false, false);
899 int qemu_spice_display_add_client(int csock, int skipauth, int tls)
901 if (tls) {
902 return spice_server_add_ssl_client(spice_server, csock, skipauth);
903 } else {
904 return spice_server_add_client(spice_server, csock, skipauth);
908 void qemu_spice_display_start(void)
910 spice_display_is_running = true;
911 spice_server_vm_start(spice_server);
914 void qemu_spice_display_stop(void)
916 spice_server_vm_stop(spice_server);
917 spice_display_is_running = false;
920 int qemu_spice_display_is_running(SimpleSpiceDisplay *ssd)
922 return spice_display_is_running;
925 static void spice_register_config(void)
927 qemu_add_opts(&qemu_spice_opts);
929 machine_init(spice_register_config);