spice: remove spice-experimental.h include
[qemu.git] / ui / spice-core.c
blob497670c4d5e25a617221af043d83e0676c8a3f2e
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 typedef struct SpiceMigration {
277 SpiceMigrateInstance sin;
278 struct {
279 MonitorCompletion *cb;
280 void *opaque;
281 } connect_complete;
282 } SpiceMigration;
284 static void migrate_connect_complete_cb(SpiceMigrateInstance *sin);
285 static void migrate_end_complete_cb(SpiceMigrateInstance *sin);
287 static const SpiceMigrateInterface migrate_interface = {
288 .base.type = SPICE_INTERFACE_MIGRATION,
289 .base.description = "migration",
290 .base.major_version = SPICE_INTERFACE_MIGRATION_MAJOR,
291 .base.minor_version = SPICE_INTERFACE_MIGRATION_MINOR,
292 .migrate_connect_complete = migrate_connect_complete_cb,
293 .migrate_end_complete = migrate_end_complete_cb,
296 static SpiceMigration spice_migrate;
298 static void migrate_connect_complete_cb(SpiceMigrateInstance *sin)
300 SpiceMigration *sm = container_of(sin, SpiceMigration, sin);
301 if (sm->connect_complete.cb) {
302 sm->connect_complete.cb(sm->connect_complete.opaque, NULL);
304 sm->connect_complete.cb = NULL;
307 static void migrate_end_complete_cb(SpiceMigrateInstance *sin)
309 qapi_event_send_spice_migrate_completed(&error_abort);
310 spice_migration_completed = true;
313 /* config string parsing */
315 static int name2enum(const char *string, const char *table[], int entries)
317 int i;
319 if (string) {
320 for (i = 0; i < entries; i++) {
321 if (!table[i]) {
322 continue;
324 if (strcmp(string, table[i]) != 0) {
325 continue;
327 return i;
330 return -1;
333 static int parse_name(const char *string, const char *optname,
334 const char *table[], int entries)
336 int value = name2enum(string, table, entries);
338 if (value != -1) {
339 return value;
341 error_report("spice: invalid %s: %s", optname, string);
342 exit(1);
345 static const char *stream_video_names[] = {
346 [ SPICE_STREAM_VIDEO_OFF ] = "off",
347 [ SPICE_STREAM_VIDEO_ALL ] = "all",
348 [ SPICE_STREAM_VIDEO_FILTER ] = "filter",
350 #define parse_stream_video(_name) \
351 parse_name(_name, "stream video control", \
352 stream_video_names, ARRAY_SIZE(stream_video_names))
354 static const char *compression_names[] = {
355 [ SPICE_IMAGE_COMPRESS_OFF ] = "off",
356 [ SPICE_IMAGE_COMPRESS_AUTO_GLZ ] = "auto_glz",
357 [ SPICE_IMAGE_COMPRESS_AUTO_LZ ] = "auto_lz",
358 [ SPICE_IMAGE_COMPRESS_QUIC ] = "quic",
359 [ SPICE_IMAGE_COMPRESS_GLZ ] = "glz",
360 [ SPICE_IMAGE_COMPRESS_LZ ] = "lz",
362 #define parse_compression(_name) \
363 parse_name(_name, "image compression", \
364 compression_names, ARRAY_SIZE(compression_names))
366 static const char *wan_compression_names[] = {
367 [ SPICE_WAN_COMPRESSION_AUTO ] = "auto",
368 [ SPICE_WAN_COMPRESSION_NEVER ] = "never",
369 [ SPICE_WAN_COMPRESSION_ALWAYS ] = "always",
371 #define parse_wan_compression(_name) \
372 parse_name(_name, "wan compression", \
373 wan_compression_names, ARRAY_SIZE(wan_compression_names))
375 /* functions for the rest of qemu */
377 static SpiceChannelList *qmp_query_spice_channels(void)
379 SpiceChannelList *cur_item = NULL, *head = NULL;
380 ChannelList *item;
382 QTAILQ_FOREACH(item, &channel_list, link) {
383 SpiceChannelList *chan;
384 char host[NI_MAXHOST], port[NI_MAXSERV];
385 struct sockaddr *paddr;
386 socklen_t plen;
388 if (!(item->info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT)) {
389 error_report("invalid channel event");
390 return NULL;
393 chan = g_malloc0(sizeof(*chan));
394 chan->value = g_malloc0(sizeof(*chan->value));
395 chan->value->base = g_malloc0(sizeof(*chan->value->base));
397 paddr = (struct sockaddr *)&item->info->paddr_ext;
398 plen = item->info->plen_ext;
399 getnameinfo(paddr, plen,
400 host, sizeof(host), port, sizeof(port),
401 NI_NUMERICHOST | NI_NUMERICSERV);
402 chan->value->base->host = g_strdup(host);
403 chan->value->base->port = g_strdup(port);
404 chan->value->base->family = inet_netfamily(paddr->sa_family);
406 chan->value->connection_id = item->info->connection_id;
407 chan->value->channel_type = item->info->type;
408 chan->value->channel_id = item->info->id;
409 chan->value->tls = item->info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS;
411 /* XXX: waiting for the qapi to support GSList */
412 if (!cur_item) {
413 head = cur_item = chan;
414 } else {
415 cur_item->next = chan;
416 cur_item = chan;
420 return head;
423 static QemuOptsList qemu_spice_opts = {
424 .name = "spice",
425 .head = QTAILQ_HEAD_INITIALIZER(qemu_spice_opts.head),
426 .desc = {
428 .name = "port",
429 .type = QEMU_OPT_NUMBER,
431 .name = "tls-port",
432 .type = QEMU_OPT_NUMBER,
434 .name = "addr",
435 .type = QEMU_OPT_STRING,
437 .name = "ipv4",
438 .type = QEMU_OPT_BOOL,
440 .name = "ipv6",
441 .type = QEMU_OPT_BOOL,
443 .name = "password",
444 .type = QEMU_OPT_STRING,
446 .name = "disable-ticketing",
447 .type = QEMU_OPT_BOOL,
449 .name = "disable-copy-paste",
450 .type = QEMU_OPT_BOOL,
452 .name = "disable-agent-file-xfer",
453 .type = QEMU_OPT_BOOL,
455 .name = "sasl",
456 .type = QEMU_OPT_BOOL,
458 .name = "x509-dir",
459 .type = QEMU_OPT_STRING,
461 .name = "x509-key-file",
462 .type = QEMU_OPT_STRING,
464 .name = "x509-key-password",
465 .type = QEMU_OPT_STRING,
467 .name = "x509-cert-file",
468 .type = QEMU_OPT_STRING,
470 .name = "x509-cacert-file",
471 .type = QEMU_OPT_STRING,
473 .name = "x509-dh-key-file",
474 .type = QEMU_OPT_STRING,
476 .name = "tls-ciphers",
477 .type = QEMU_OPT_STRING,
479 .name = "tls-channel",
480 .type = QEMU_OPT_STRING,
482 .name = "plaintext-channel",
483 .type = QEMU_OPT_STRING,
485 .name = "image-compression",
486 .type = QEMU_OPT_STRING,
488 .name = "jpeg-wan-compression",
489 .type = QEMU_OPT_STRING,
491 .name = "zlib-glz-wan-compression",
492 .type = QEMU_OPT_STRING,
494 .name = "streaming-video",
495 .type = QEMU_OPT_STRING,
497 .name = "agent-mouse",
498 .type = QEMU_OPT_BOOL,
500 .name = "playback-compression",
501 .type = QEMU_OPT_BOOL,
502 }, {
503 .name = "seamless-migration",
504 .type = QEMU_OPT_BOOL,
506 { /* end of list */ }
510 SpiceInfo *qmp_query_spice(Error **errp)
512 QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
513 int port, tls_port;
514 const char *addr;
515 SpiceInfo *info;
516 unsigned int major;
517 unsigned int minor;
518 unsigned int micro;
520 info = g_malloc0(sizeof(*info));
522 if (!spice_server || !opts) {
523 info->enabled = false;
524 return info;
527 info->enabled = true;
528 info->migrated = spice_migration_completed;
530 addr = qemu_opt_get(opts, "addr");
531 port = qemu_opt_get_number(opts, "port", 0);
532 tls_port = qemu_opt_get_number(opts, "tls-port", 0);
534 info->has_auth = true;
535 info->auth = g_strdup(auth);
537 info->has_host = true;
538 info->host = g_strdup(addr ? addr : "*");
540 info->has_compiled_version = true;
541 major = (SPICE_SERVER_VERSION & 0xff0000) >> 16;
542 minor = (SPICE_SERVER_VERSION & 0xff00) >> 8;
543 micro = SPICE_SERVER_VERSION & 0xff;
544 info->compiled_version = g_strdup_printf("%d.%d.%d", major, minor, micro);
546 if (port) {
547 info->has_port = true;
548 info->port = port;
550 if (tls_port) {
551 info->has_tls_port = true;
552 info->tls_port = tls_port;
555 info->mouse_mode = spice_server_is_server_mouse(spice_server) ?
556 SPICE_QUERY_MOUSE_MODE_SERVER :
557 SPICE_QUERY_MOUSE_MODE_CLIENT;
559 /* for compatibility with the original command */
560 info->has_channels = true;
561 info->channels = qmp_query_spice_channels();
563 return info;
566 static void migration_state_notifier(Notifier *notifier, void *data)
568 MigrationState *s = data;
570 if (!spice_have_target_host) {
571 return;
574 if (migration_in_setup(s)) {
575 spice_server_migrate_start(spice_server);
576 } else if (migration_has_finished(s)) {
577 spice_server_migrate_end(spice_server, true);
578 spice_have_target_host = false;
579 } else if (migration_has_failed(s)) {
580 spice_server_migrate_end(spice_server, false);
581 spice_have_target_host = false;
585 int qemu_spice_migrate_info(const char *hostname, int port, int tls_port,
586 const char *subject,
587 MonitorCompletion *cb, void *opaque)
589 int ret;
591 spice_migrate.connect_complete.cb = cb;
592 spice_migrate.connect_complete.opaque = opaque;
593 ret = spice_server_migrate_connect(spice_server, hostname,
594 port, tls_port, subject);
595 spice_have_target_host = true;
596 return ret;
599 static int add_channel(const char *name, const char *value, void *opaque)
601 int security = 0;
602 int rc;
604 if (strcmp(name, "tls-channel") == 0) {
605 int *tls_port = opaque;
606 if (!*tls_port) {
607 error_report("spice: tried to setup tls-channel"
608 " without specifying a TLS port");
609 exit(1);
611 security = SPICE_CHANNEL_SECURITY_SSL;
613 if (strcmp(name, "plaintext-channel") == 0) {
614 security = SPICE_CHANNEL_SECURITY_NONE;
616 if (security == 0) {
617 return 0;
619 if (strcmp(value, "default") == 0) {
620 rc = spice_server_set_channel_security(spice_server, NULL, security);
621 } else {
622 rc = spice_server_set_channel_security(spice_server, value, security);
624 if (rc != 0) {
625 error_report("spice: failed to set channel security for %s", value);
626 exit(1);
628 return 0;
631 static void vm_change_state_handler(void *opaque, int running,
632 RunState state)
634 if (running) {
635 qemu_spice_display_start();
636 } else {
637 qemu_spice_display_stop();
641 void qemu_spice_init(void)
643 QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
644 const char *password, *str, *x509_dir, *addr,
645 *x509_key_password = NULL,
646 *x509_dh_file = NULL,
647 *tls_ciphers = NULL;
648 char *x509_key_file = NULL,
649 *x509_cert_file = NULL,
650 *x509_cacert_file = NULL;
651 int port, tls_port, addr_flags;
652 spice_image_compression_t compression;
653 spice_wan_compression_t wan_compr;
654 bool seamless_migration;
656 qemu_thread_get_self(&me);
658 if (!opts) {
659 return;
661 port = qemu_opt_get_number(opts, "port", 0);
662 tls_port = qemu_opt_get_number(opts, "tls-port", 0);
663 if (port < 0 || port > 65535) {
664 error_report("spice port is out of range");
665 exit(1);
667 if (tls_port < 0 || tls_port > 65535) {
668 error_report("spice tls-port is out of range");
669 exit(1);
671 password = qemu_opt_get(opts, "password");
673 if (tls_port) {
674 x509_dir = qemu_opt_get(opts, "x509-dir");
675 if (!x509_dir) {
676 x509_dir = ".";
679 str = qemu_opt_get(opts, "x509-key-file");
680 if (str) {
681 x509_key_file = g_strdup(str);
682 } else {
683 x509_key_file = g_strdup_printf("%s/%s", x509_dir,
684 X509_SERVER_KEY_FILE);
687 str = qemu_opt_get(opts, "x509-cert-file");
688 if (str) {
689 x509_cert_file = g_strdup(str);
690 } else {
691 x509_cert_file = g_strdup_printf("%s/%s", x509_dir,
692 X509_SERVER_CERT_FILE);
695 str = qemu_opt_get(opts, "x509-cacert-file");
696 if (str) {
697 x509_cacert_file = g_strdup(str);
698 } else {
699 x509_cacert_file = g_strdup_printf("%s/%s", x509_dir,
700 X509_CA_CERT_FILE);
703 x509_key_password = qemu_opt_get(opts, "x509-key-password");
704 x509_dh_file = qemu_opt_get(opts, "x509-dh-key-file");
705 tls_ciphers = qemu_opt_get(opts, "tls-ciphers");
708 addr = qemu_opt_get(opts, "addr");
709 addr_flags = 0;
710 if (qemu_opt_get_bool(opts, "ipv4", 0)) {
711 addr_flags |= SPICE_ADDR_FLAG_IPV4_ONLY;
712 } else if (qemu_opt_get_bool(opts, "ipv6", 0)) {
713 addr_flags |= SPICE_ADDR_FLAG_IPV6_ONLY;
716 spice_server = spice_server_new();
717 spice_server_set_addr(spice_server, addr ? addr : "", addr_flags);
718 if (port) {
719 spice_server_set_port(spice_server, port);
721 if (tls_port) {
722 spice_server_set_tls(spice_server, tls_port,
723 x509_cacert_file,
724 x509_cert_file,
725 x509_key_file,
726 x509_key_password,
727 x509_dh_file,
728 tls_ciphers);
730 if (password) {
731 qemu_spice_set_passwd(password, false, false);
733 if (qemu_opt_get_bool(opts, "sasl", 0)) {
734 if (spice_server_set_sasl_appname(spice_server, "qemu") == -1 ||
735 spice_server_set_sasl(spice_server, 1) == -1) {
736 error_report("spice: failed to enable sasl");
737 exit(1);
739 auth = "sasl";
741 if (qemu_opt_get_bool(opts, "disable-ticketing", 0)) {
742 auth = "none";
743 spice_server_set_noauth(spice_server);
746 if (qemu_opt_get_bool(opts, "disable-copy-paste", 0)) {
747 spice_server_set_agent_copypaste(spice_server, false);
750 if (qemu_opt_get_bool(opts, "disable-agent-file-xfer", 0)) {
751 #if SPICE_SERVER_VERSION >= 0x000c04
752 spice_server_set_agent_file_xfer(spice_server, false);
753 #else
754 error_report("this qemu build does not support the "
755 "\"disable-agent-file-xfer\" option");
756 exit(1);
757 #endif
760 compression = SPICE_IMAGE_COMPRESS_AUTO_GLZ;
761 str = qemu_opt_get(opts, "image-compression");
762 if (str) {
763 compression = parse_compression(str);
765 spice_server_set_image_compression(spice_server, compression);
767 wan_compr = SPICE_WAN_COMPRESSION_AUTO;
768 str = qemu_opt_get(opts, "jpeg-wan-compression");
769 if (str) {
770 wan_compr = parse_wan_compression(str);
772 spice_server_set_jpeg_compression(spice_server, wan_compr);
774 wan_compr = SPICE_WAN_COMPRESSION_AUTO;
775 str = qemu_opt_get(opts, "zlib-glz-wan-compression");
776 if (str) {
777 wan_compr = parse_wan_compression(str);
779 spice_server_set_zlib_glz_compression(spice_server, wan_compr);
781 str = qemu_opt_get(opts, "streaming-video");
782 if (str) {
783 int streaming_video = parse_stream_video(str);
784 spice_server_set_streaming_video(spice_server, streaming_video);
785 } else {
786 spice_server_set_streaming_video(spice_server, SPICE_STREAM_VIDEO_OFF);
789 spice_server_set_agent_mouse
790 (spice_server, qemu_opt_get_bool(opts, "agent-mouse", 1));
791 spice_server_set_playback_compression
792 (spice_server, qemu_opt_get_bool(opts, "playback-compression", 1));
794 qemu_opt_foreach(opts, add_channel, &tls_port, 0);
796 spice_server_set_name(spice_server, qemu_name);
797 spice_server_set_uuid(spice_server, qemu_uuid);
799 seamless_migration = qemu_opt_get_bool(opts, "seamless-migration", 0);
800 spice_server_set_seamless_migration(spice_server, seamless_migration);
801 if (spice_server_init(spice_server, &core_interface) != 0) {
802 error_report("failed to initialize spice server");
803 exit(1);
805 using_spice = 1;
807 migration_state.notify = migration_state_notifier;
808 add_migration_state_change_notifier(&migration_state);
809 spice_migrate.sin.base.sif = &migrate_interface.base;
810 spice_migrate.connect_complete.cb = NULL;
811 qemu_spice_add_interface(&spice_migrate.sin.base);
813 qemu_spice_input_init();
814 qemu_spice_audio_init();
816 qemu_add_vm_change_state_handler(vm_change_state_handler, NULL);
818 g_free(x509_key_file);
819 g_free(x509_cert_file);
820 g_free(x509_cacert_file);
822 #if SPICE_SERVER_VERSION >= 0x000c02
823 qemu_spice_register_ports();
824 #endif
827 int qemu_spice_add_interface(SpiceBaseInstance *sin)
829 if (!spice_server) {
830 if (QTAILQ_FIRST(&qemu_spice_opts.head) != NULL) {
831 error_report("Oops: spice configured but not active");
832 exit(1);
835 * Create a spice server instance.
836 * It does *not* listen on the network.
837 * It handles QXL local rendering only.
839 * With a command line like '-vnc :0 -vga qxl' you'll end up here.
841 spice_server = spice_server_new();
842 spice_server_set_sasl_appname(spice_server, "qemu");
843 spice_server_init(spice_server, &core_interface);
844 qemu_add_vm_change_state_handler(vm_change_state_handler, NULL);
847 return spice_server_add_interface(spice_server, sin);
850 static GSList *spice_consoles;
852 bool qemu_spice_have_display_interface(QemuConsole *con)
854 if (g_slist_find(spice_consoles, con)) {
855 return true;
857 return false;
860 int qemu_spice_add_display_interface(QXLInstance *qxlin, QemuConsole *con)
862 if (g_slist_find(spice_consoles, con)) {
863 return -1;
865 qxlin->id = qemu_console_get_index(con);
866 spice_consoles = g_slist_append(spice_consoles, con);
867 return qemu_spice_add_interface(&qxlin->base);
870 static int qemu_spice_set_ticket(bool fail_if_conn, bool disconnect_if_conn)
872 time_t lifetime, now = time(NULL);
873 char *passwd;
875 if (now < auth_expires) {
876 passwd = auth_passwd;
877 lifetime = (auth_expires - now);
878 if (lifetime > INT_MAX) {
879 lifetime = INT_MAX;
881 } else {
882 passwd = NULL;
883 lifetime = 1;
885 return spice_server_set_ticket(spice_server, passwd, lifetime,
886 fail_if_conn, disconnect_if_conn);
889 int qemu_spice_set_passwd(const char *passwd,
890 bool fail_if_conn, bool disconnect_if_conn)
892 if (strcmp(auth, "spice") != 0) {
893 return -1;
896 g_free(auth_passwd);
897 auth_passwd = g_strdup(passwd);
898 return qemu_spice_set_ticket(fail_if_conn, disconnect_if_conn);
901 int qemu_spice_set_pw_expire(time_t expires)
903 auth_expires = expires;
904 return qemu_spice_set_ticket(false, false);
907 int qemu_spice_display_add_client(int csock, int skipauth, int tls)
909 if (tls) {
910 return spice_server_add_ssl_client(spice_server, csock, skipauth);
911 } else {
912 return spice_server_add_client(spice_server, csock, skipauth);
916 void qemu_spice_display_start(void)
918 spice_display_is_running = true;
919 spice_server_vm_start(spice_server);
922 void qemu_spice_display_stop(void)
924 spice_server_vm_stop(spice_server);
925 spice_display_is_running = false;
928 int qemu_spice_display_is_running(SimpleSpiceDisplay *ssd)
930 return spice_display_is_running;
933 static void spice_register_config(void)
935 qemu_add_opts(&qemu_spice_opts);
937 machine_init(spice_register_config);