2 * Copyright (C) 2010 Red Hat, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 or
7 * (at your option) version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #include "qemu/osdep.h"
22 #include "sysemu/sysemu.h"
24 #include "qemu-common.h"
25 #include "ui/qemu-spice.h"
26 #include "qemu/error-report.h"
27 #include "qemu/thread.h"
28 #include "qemu/timer.h"
29 #include "qemu/queue.h"
30 #include "qemu-x509.h"
31 #include "qemu/sockets.h"
32 #include "qmp-commands.h"
33 #include "qapi/qmp/qbool.h"
34 #include "qapi/qmp/qstring.h"
35 #include "qapi/qmp/qjson.h"
36 #include "qemu/notify.h"
37 #include "migration/misc.h"
39 #include "ui/spice-display.h"
40 #include "qapi-event.h"
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
;
58 QTAILQ_ENTRY(SpiceTimer
) next
;
60 static QTAILQ_HEAD(, SpiceTimer
) timers
= QTAILQ_HEAD_INITIALIZER(timers
);
62 static SpiceTimer
*timer_add(SpiceTimerFunc func
, void *opaque
)
66 timer
= g_malloc0(sizeof(*timer
));
67 timer
->timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, func
, opaque
);
68 QTAILQ_INSERT_TAIL(&timers
, timer
, next
);
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
);
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
)
130 watch
= g_malloc0(sizeof(*watch
));
133 watch
->opaque
= opaque
;
134 QTAILQ_INSERT_TAIL(&watches
, watch
, next
);
136 watch_update_mask(watch
, event_mask
);
140 static void watch_remove(SpiceWatch
*watch
)
142 qemu_set_fd_handler(watch
->fd
, NULL
, NULL
, NULL
);
143 QTAILQ_REMOVE(&watches
, watch
, next
);
147 typedef struct ChannelList 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
)
158 item
= g_malloc0(sizeof(*item
));
160 QTAILQ_INSERT_TAIL(&channel_list
, item
, link
);
163 static void channel_list_del(SpiceChannelEventInfo
*info
)
167 QTAILQ_FOREACH(item
, &channel_list
, link
) {
168 if (item
->info
!= info
) {
171 QTAILQ_REMOVE(&channel_list
, item
, link
);
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
;
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
213 bool need_lock
= !qemu_thread_is_self(&me
);
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
,
222 add_addr_info(qapi_SpiceServerInfo_base(server
),
223 (struct sockaddr
*)&info
->laddr_ext
,
226 error_report("spice: %s, extended address is expected",
231 case SPICE_CHANNEL_EVENT_CONNECTED
:
232 qapi_event_send_spice_connected(qapi_SpiceServerInfo_base(server
),
233 qapi_SpiceChannel_base(client
),
236 case SPICE_CHANNEL_EVENT_INITIALIZED
:
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
);
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
),
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
)
313 for (i
= 0; i
< entries
; i
++) {
317 if (strcmp(string
, table
[i
]) != 0) {
326 static int parse_name(const char *string
, const char *optname
,
327 const char *table
[], int entries
)
329 int value
= name2enum(string
, table
, entries
);
334 error_report("spice: invalid %s: %s", optname
, string
);
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
;
375 QTAILQ_FOREACH(item
, &channel_list
, link
) {
376 SpiceChannelList
*chan
;
377 char host
[NI_MAXHOST
], port
[NI_MAXSERV
];
378 struct sockaddr
*paddr
;
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 */
402 head
= cur_item
= chan
;
404 cur_item
->next
= chan
;
412 static QemuOptsList qemu_spice_opts
= {
414 .head
= QTAILQ_HEAD_INITIALIZER(qemu_spice_opts
.head
),
418 .type
= QEMU_OPT_NUMBER
,
421 .type
= QEMU_OPT_NUMBER
,
424 .type
= QEMU_OPT_STRING
,
427 .type
= QEMU_OPT_BOOL
,
430 .type
= QEMU_OPT_BOOL
,
431 #ifdef SPICE_ADDR_FLAG_UNIX_ONLY
434 .type
= QEMU_OPT_BOOL
,
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
,
450 .type
= QEMU_OPT_BOOL
,
453 .type
= QEMU_OPT_STRING
,
455 .name
= "x509-key-file",
456 .type
= QEMU_OPT_STRING
,
458 .name
= "x509-key-password",
459 .type
= QEMU_OPT_STRING
,
461 .name
= "x509-cert-file",
462 .type
= QEMU_OPT_STRING
,
464 .name
= "x509-cacert-file",
465 .type
= QEMU_OPT_STRING
,
467 .name
= "x509-dh-key-file",
468 .type
= QEMU_OPT_STRING
,
470 .name
= "tls-ciphers",
471 .type
= QEMU_OPT_STRING
,
473 .name
= "tls-channel",
474 .type
= QEMU_OPT_STRING
,
476 .name
= "plaintext-channel",
477 .type
= QEMU_OPT_STRING
,
479 .name
= "image-compression",
480 .type
= QEMU_OPT_STRING
,
482 .name
= "jpeg-wan-compression",
483 .type
= QEMU_OPT_STRING
,
485 .name
= "zlib-glz-wan-compression",
486 .type
= QEMU_OPT_STRING
,
488 .name
= "streaming-video",
489 .type
= QEMU_OPT_STRING
,
491 .name
= "agent-mouse",
492 .type
= QEMU_OPT_BOOL
,
494 .name
= "playback-compression",
495 .type
= QEMU_OPT_BOOL
,
497 .name
= "seamless-migration",
498 .type
= QEMU_OPT_BOOL
,
501 .type
= QEMU_OPT_STRING
,
504 .type
= QEMU_OPT_NUMBER
,
508 .type
= QEMU_OPT_BOOL
,
510 .name
= "rendernode",
511 .type
= QEMU_OPT_STRING
,
514 { /* end of list */ }
518 SpiceInfo
*qmp_query_spice(Error
**errp
)
520 QemuOpts
*opts
= QTAILQ_FIRST(&qemu_spice_opts
.head
);
528 info
= g_malloc0(sizeof(*info
));
530 if (!spice_server
|| !opts
) {
531 info
->enabled
= false;
535 info
->enabled
= true;
536 info
->migrated
= spice_migration_completed
;
538 addr
= qemu_opt_get(opts
, "addr");
539 port
= qemu_opt_get_number(opts
, "port", 0);
540 tls_port
= qemu_opt_get_number(opts
, "tls-port", 0);
542 info
->has_auth
= true;
543 info
->auth
= g_strdup(auth
);
545 info
->has_host
= true;
546 info
->host
= g_strdup(addr
? addr
: "*");
548 info
->has_compiled_version
= true;
549 major
= (SPICE_SERVER_VERSION
& 0xff0000) >> 16;
550 minor
= (SPICE_SERVER_VERSION
& 0xff00) >> 8;
551 micro
= SPICE_SERVER_VERSION
& 0xff;
552 info
->compiled_version
= g_strdup_printf("%d.%d.%d", major
, minor
, micro
);
555 info
->has_port
= true;
559 info
->has_tls_port
= true;
560 info
->tls_port
= tls_port
;
563 info
->mouse_mode
= spice_server_is_server_mouse(spice_server
) ?
564 SPICE_QUERY_MOUSE_MODE_SERVER
:
565 SPICE_QUERY_MOUSE_MODE_CLIENT
;
567 /* for compatibility with the original command */
568 info
->has_channels
= true;
569 info
->channels
= qmp_query_spice_channels();
574 static void migration_state_notifier(Notifier
*notifier
, void *data
)
576 MigrationState
*s
= data
;
578 if (!spice_have_target_host
) {
582 if (migration_in_setup(s
)) {
583 spice_server_migrate_start(spice_server
);
584 } else if (migration_has_finished(s
) ||
585 migration_in_postcopy_after_devices(s
)) {
586 spice_server_migrate_end(spice_server
, true);
587 spice_have_target_host
= false;
588 } else if (migration_has_failed(s
)) {
589 spice_server_migrate_end(spice_server
, false);
590 spice_have_target_host
= false;
594 int qemu_spice_migrate_info(const char *hostname
, int port
, int tls_port
,
599 ret
= spice_server_migrate_connect(spice_server
, hostname
,
600 port
, tls_port
, subject
);
601 spice_have_target_host
= true;
605 static int add_channel(void *opaque
, const char *name
, const char *value
,
611 if (strcmp(name
, "tls-channel") == 0) {
612 int *tls_port
= opaque
;
614 error_report("spice: tried to setup tls-channel"
615 " without specifying a TLS port");
618 security
= SPICE_CHANNEL_SECURITY_SSL
;
620 if (strcmp(name
, "plaintext-channel") == 0) {
621 security
= SPICE_CHANNEL_SECURITY_NONE
;
626 if (strcmp(value
, "default") == 0) {
627 rc
= spice_server_set_channel_security(spice_server
, NULL
, security
);
629 rc
= spice_server_set_channel_security(spice_server
, value
, security
);
632 error_report("spice: failed to set channel security for %s", value
);
638 static void vm_change_state_handler(void *opaque
, int running
,
642 qemu_spice_display_start();
644 qemu_spice_display_stop();
648 void qemu_spice_init(void)
650 QemuOpts
*opts
= QTAILQ_FIRST(&qemu_spice_opts
.head
);
651 const char *password
, *str
, *x509_dir
, *addr
,
652 *x509_key_password
= NULL
,
653 *x509_dh_file
= NULL
,
655 char *x509_key_file
= NULL
,
656 *x509_cert_file
= NULL
,
657 *x509_cacert_file
= NULL
;
658 int port
, tls_port
, addr_flags
;
659 spice_image_compression_t compression
;
660 spice_wan_compression_t wan_compr
;
661 bool seamless_migration
;
663 qemu_thread_get_self(&me
);
668 port
= qemu_opt_get_number(opts
, "port", 0);
669 tls_port
= qemu_opt_get_number(opts
, "tls-port", 0);
670 if (port
< 0 || port
> 65535) {
671 error_report("spice port is out of range");
674 if (tls_port
< 0 || tls_port
> 65535) {
675 error_report("spice tls-port is out of range");
678 password
= qemu_opt_get(opts
, "password");
681 x509_dir
= qemu_opt_get(opts
, "x509-dir");
686 str
= qemu_opt_get(opts
, "x509-key-file");
688 x509_key_file
= g_strdup(str
);
690 x509_key_file
= g_strdup_printf("%s/%s", x509_dir
,
691 X509_SERVER_KEY_FILE
);
694 str
= qemu_opt_get(opts
, "x509-cert-file");
696 x509_cert_file
= g_strdup(str
);
698 x509_cert_file
= g_strdup_printf("%s/%s", x509_dir
,
699 X509_SERVER_CERT_FILE
);
702 str
= qemu_opt_get(opts
, "x509-cacert-file");
704 x509_cacert_file
= g_strdup(str
);
706 x509_cacert_file
= g_strdup_printf("%s/%s", x509_dir
,
710 x509_key_password
= qemu_opt_get(opts
, "x509-key-password");
711 x509_dh_file
= qemu_opt_get(opts
, "x509-dh-key-file");
712 tls_ciphers
= qemu_opt_get(opts
, "tls-ciphers");
715 addr
= qemu_opt_get(opts
, "addr");
717 if (qemu_opt_get_bool(opts
, "ipv4", 0)) {
718 addr_flags
|= SPICE_ADDR_FLAG_IPV4_ONLY
;
719 } else if (qemu_opt_get_bool(opts
, "ipv6", 0)) {
720 addr_flags
|= SPICE_ADDR_FLAG_IPV6_ONLY
;
721 #ifdef SPICE_ADDR_FLAG_UNIX_ONLY
722 } else if (qemu_opt_get_bool(opts
, "unix", 0)) {
723 addr_flags
|= SPICE_ADDR_FLAG_UNIX_ONLY
;
727 spice_server
= spice_server_new();
728 spice_server_set_addr(spice_server
, addr
? addr
: "", addr_flags
);
730 spice_server_set_port(spice_server
, port
);
733 spice_server_set_tls(spice_server
, tls_port
,
742 qemu_spice_set_passwd(password
, false, false);
744 if (qemu_opt_get_bool(opts
, "sasl", 0)) {
745 if (spice_server_set_sasl(spice_server
, 1) == -1) {
746 error_report("spice: failed to enable sasl");
751 if (qemu_opt_get_bool(opts
, "disable-ticketing", 0)) {
753 spice_server_set_noauth(spice_server
);
756 if (qemu_opt_get_bool(opts
, "disable-copy-paste", 0)) {
757 spice_server_set_agent_copypaste(spice_server
, false);
760 if (qemu_opt_get_bool(opts
, "disable-agent-file-xfer", 0)) {
761 #if SPICE_SERVER_VERSION >= 0x000c04
762 spice_server_set_agent_file_xfer(spice_server
, false);
764 error_report("this qemu build does not support the "
765 "\"disable-agent-file-xfer\" option");
770 compression
= SPICE_IMAGE_COMPRESS_AUTO_GLZ
;
771 str
= qemu_opt_get(opts
, "image-compression");
773 compression
= parse_compression(str
);
775 spice_server_set_image_compression(spice_server
, compression
);
777 wan_compr
= SPICE_WAN_COMPRESSION_AUTO
;
778 str
= qemu_opt_get(opts
, "jpeg-wan-compression");
780 wan_compr
= parse_wan_compression(str
);
782 spice_server_set_jpeg_compression(spice_server
, wan_compr
);
784 wan_compr
= SPICE_WAN_COMPRESSION_AUTO
;
785 str
= qemu_opt_get(opts
, "zlib-glz-wan-compression");
787 wan_compr
= parse_wan_compression(str
);
789 spice_server_set_zlib_glz_compression(spice_server
, wan_compr
);
791 str
= qemu_opt_get(opts
, "streaming-video");
793 int streaming_video
= parse_stream_video(str
);
794 spice_server_set_streaming_video(spice_server
, streaming_video
);
796 spice_server_set_streaming_video(spice_server
, SPICE_STREAM_VIDEO_OFF
);
799 spice_server_set_agent_mouse
800 (spice_server
, qemu_opt_get_bool(opts
, "agent-mouse", 1));
801 spice_server_set_playback_compression
802 (spice_server
, qemu_opt_get_bool(opts
, "playback-compression", 1));
804 qemu_opt_foreach(opts
, add_channel
, &tls_port
, NULL
);
806 spice_server_set_name(spice_server
, qemu_name
);
807 spice_server_set_uuid(spice_server
, (unsigned char *)&qemu_uuid
);
809 seamless_migration
= qemu_opt_get_bool(opts
, "seamless-migration", 0);
810 spice_server_set_seamless_migration(spice_server
, seamless_migration
);
811 spice_server_set_sasl_appname(spice_server
, "qemu");
812 if (spice_server_init(spice_server
, &core_interface
) != 0) {
813 error_report("failed to initialize spice server");
818 migration_state
.notify
= migration_state_notifier
;
819 add_migration_state_change_notifier(&migration_state
);
820 spice_migrate
.base
.sif
= &migrate_interface
.base
;
821 qemu_spice_add_interface(&spice_migrate
.base
);
823 qemu_spice_input_init();
824 qemu_spice_audio_init();
826 qemu_add_vm_change_state_handler(vm_change_state_handler
, NULL
);
827 qemu_spice_display_stop();
829 g_free(x509_key_file
);
830 g_free(x509_cert_file
);
831 g_free(x509_cacert_file
);
833 #if SPICE_SERVER_VERSION >= 0x000c02
834 qemu_spice_register_ports();
838 if (qemu_opt_get_bool(opts
, "gl", 0)) {
839 if ((port
!= 0) || (tls_port
!= 0)) {
840 error_report("SPICE GL support is local-only for now and "
841 "incompatible with -spice port/tls-port");
844 if (egl_rendernode_init(qemu_opt_get(opts
, "rendernode")) != 0) {
845 error_report("Failed to initialize EGL render node for SPICE GL");
853 int qemu_spice_add_interface(SpiceBaseInstance
*sin
)
856 if (QTAILQ_FIRST(&qemu_spice_opts
.head
) != NULL
) {
857 error_report("Oops: spice configured but not active");
861 * Create a spice server instance.
862 * It does *not* listen on the network.
863 * It handles QXL local rendering only.
865 * With a command line like '-vnc :0 -vga qxl' you'll end up here.
867 spice_server
= spice_server_new();
868 spice_server_set_sasl_appname(spice_server
, "qemu");
869 spice_server_init(spice_server
, &core_interface
);
870 qemu_add_vm_change_state_handler(vm_change_state_handler
, NULL
);
873 return spice_server_add_interface(spice_server
, sin
);
876 static GSList
*spice_consoles
;
878 bool qemu_spice_have_display_interface(QemuConsole
*con
)
880 if (g_slist_find(spice_consoles
, con
)) {
886 int qemu_spice_add_display_interface(QXLInstance
*qxlin
, QemuConsole
*con
)
888 if (g_slist_find(spice_consoles
, con
)) {
891 qxlin
->id
= qemu_console_get_index(con
);
892 spice_consoles
= g_slist_append(spice_consoles
, con
);
893 return qemu_spice_add_interface(&qxlin
->base
);
896 static int qemu_spice_set_ticket(bool fail_if_conn
, bool disconnect_if_conn
)
898 time_t lifetime
, now
= time(NULL
);
901 if (now
< auth_expires
) {
902 passwd
= auth_passwd
;
903 lifetime
= (auth_expires
- now
);
904 if (lifetime
> INT_MAX
) {
911 return spice_server_set_ticket(spice_server
, passwd
, lifetime
,
912 fail_if_conn
, disconnect_if_conn
);
915 int qemu_spice_set_passwd(const char *passwd
,
916 bool fail_if_conn
, bool disconnect_if_conn
)
918 if (strcmp(auth
, "spice") != 0) {
923 auth_passwd
= g_strdup(passwd
);
924 return qemu_spice_set_ticket(fail_if_conn
, disconnect_if_conn
);
927 int qemu_spice_set_pw_expire(time_t expires
)
929 auth_expires
= expires
;
930 return qemu_spice_set_ticket(false, false);
933 int qemu_spice_display_add_client(int csock
, int skipauth
, int tls
)
936 return spice_server_add_ssl_client(spice_server
, csock
, skipauth
);
938 return spice_server_add_client(spice_server
, csock
, skipauth
);
942 void qemu_spice_display_start(void)
944 spice_display_is_running
= true;
945 spice_server_vm_start(spice_server
);
948 void qemu_spice_display_stop(void)
950 spice_server_vm_stop(spice_server
);
951 spice_display_is_running
= false;
954 int qemu_spice_display_is_running(SimpleSpiceDisplay
*ssd
)
956 return spice_display_is_running
;
959 static void spice_register_config(void)
961 qemu_add_opts(&qemu_spice_opts
);
963 opts_init(spice_register_config
);