]> git.proxmox.com Git - mirror_qemu.git/blame - ui/spice-core.c
spice: support the new migration interface (spice 0.8.3)
[mirror_qemu.git] / ui / spice-core.c
CommitLineData
29b0040b
GH
1/*
2 * Copyright (C) 2010 Red Hat, Inc.
3 *
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.
8 *
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.
13 *
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/>.
16 */
17
18#include <spice.h>
19#include <spice-experimental.h>
20
6f8c63fb 21#include <netdb.h>
22b626e2 22#include <pthread.h>
6f8c63fb 23
29b0040b
GH
24#include "qemu-common.h"
25#include "qemu-spice.h"
26#include "qemu-timer.h"
27#include "qemu-queue.h"
c448e855 28#include "qemu-x509.h"
6f8c63fb
GH
29#include "qemu_socket.h"
30#include "qint.h"
31#include "qbool.h"
32#include "qstring.h"
33#include "qjson.h"
e866e239
GH
34#include "notify.h"
35#include "migration.h"
29b0040b 36#include "monitor.h"
e866e239 37#include "hw/hw.h"
29b0040b
GH
38
39/* core bits */
40
41static SpiceServer *spice_server;
e866e239 42static Notifier migration_state;
6f8c63fb 43static const char *auth = "spice";
7572150c
GH
44static char *auth_passwd;
45static time_t auth_expires = TIME_MAX;
29b0040b
GH
46int using_spice = 0;
47
22b626e2
GH
48static pthread_t me;
49
29b0040b
GH
50struct SpiceTimer {
51 QEMUTimer *timer;
52 QTAILQ_ENTRY(SpiceTimer) next;
53};
54static QTAILQ_HEAD(, SpiceTimer) timers = QTAILQ_HEAD_INITIALIZER(timers);
55
56static SpiceTimer *timer_add(SpiceTimerFunc func, void *opaque)
57{
58 SpiceTimer *timer;
59
7267c094 60 timer = g_malloc0(sizeof(*timer));
7bd427d8 61 timer->timer = qemu_new_timer_ms(rt_clock, func, opaque);
29b0040b
GH
62 QTAILQ_INSERT_TAIL(&timers, timer, next);
63 return timer;
64}
65
66static void timer_start(SpiceTimer *timer, uint32_t ms)
67{
7bd427d8 68 qemu_mod_timer(timer->timer, qemu_get_clock_ms(rt_clock) + ms);
29b0040b
GH
69}
70
71static void timer_cancel(SpiceTimer *timer)
72{
73 qemu_del_timer(timer->timer);
74}
75
76static void timer_remove(SpiceTimer *timer)
77{
78 qemu_del_timer(timer->timer);
79 qemu_free_timer(timer->timer);
80 QTAILQ_REMOVE(&timers, timer, next);
7267c094 81 g_free(timer);
29b0040b
GH
82}
83
84struct SpiceWatch {
85 int fd;
86 int event_mask;
87 SpiceWatchFunc func;
88 void *opaque;
89 QTAILQ_ENTRY(SpiceWatch) next;
90};
91static QTAILQ_HEAD(, SpiceWatch) watches = QTAILQ_HEAD_INITIALIZER(watches);
92
93static void watch_read(void *opaque)
94{
95 SpiceWatch *watch = opaque;
96 watch->func(watch->fd, SPICE_WATCH_EVENT_READ, watch->opaque);
97}
98
99static void watch_write(void *opaque)
100{
101 SpiceWatch *watch = opaque;
102 watch->func(watch->fd, SPICE_WATCH_EVENT_WRITE, watch->opaque);
103}
104
105static void watch_update_mask(SpiceWatch *watch, int event_mask)
106{
107 IOHandler *on_read = NULL;
108 IOHandler *on_write = NULL;
109
110 watch->event_mask = event_mask;
111 if (watch->event_mask & SPICE_WATCH_EVENT_READ) {
112 on_read = watch_read;
113 }
114 if (watch->event_mask & SPICE_WATCH_EVENT_WRITE) {
3d6d306c 115 on_write = watch_write;
29b0040b
GH
116 }
117 qemu_set_fd_handler(watch->fd, on_read, on_write, watch);
118}
119
120static SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque)
121{
122 SpiceWatch *watch;
123
7267c094 124 watch = g_malloc0(sizeof(*watch));
29b0040b
GH
125 watch->fd = fd;
126 watch->func = func;
127 watch->opaque = opaque;
128 QTAILQ_INSERT_TAIL(&watches, watch, next);
129
130 watch_update_mask(watch, event_mask);
131 return watch;
132}
133
134static void watch_remove(SpiceWatch *watch)
135{
136 watch_update_mask(watch, 0);
137 QTAILQ_REMOVE(&watches, watch, next);
7267c094 138 g_free(watch);
29b0040b
GH
139}
140
6f8c63fb
GH
141#if SPICE_INTERFACE_CORE_MINOR >= 3
142
cb42a870
GH
143typedef struct ChannelList ChannelList;
144struct ChannelList {
145 SpiceChannelEventInfo *info;
146 QTAILQ_ENTRY(ChannelList) link;
147};
148static QTAILQ_HEAD(, ChannelList) channel_list = QTAILQ_HEAD_INITIALIZER(channel_list);
149
150static void channel_list_add(SpiceChannelEventInfo *info)
151{
152 ChannelList *item;
153
7267c094 154 item = g_malloc0(sizeof(*item));
cb42a870
GH
155 item->info = info;
156 QTAILQ_INSERT_TAIL(&channel_list, item, link);
157}
158
159static void channel_list_del(SpiceChannelEventInfo *info)
160{
161 ChannelList *item;
162
163 QTAILQ_FOREACH(item, &channel_list, link) {
164 if (item->info != info) {
165 continue;
166 }
167 QTAILQ_REMOVE(&channel_list, item, link);
7267c094 168 g_free(item);
cb42a870
GH
169 return;
170 }
171}
172
6f8c63fb
GH
173static void add_addr_info(QDict *dict, struct sockaddr *addr, int len)
174{
175 char host[NI_MAXHOST], port[NI_MAXSERV];
176 const char *family;
177
178 getnameinfo(addr, len, host, sizeof(host), port, sizeof(port),
179 NI_NUMERICHOST | NI_NUMERICSERV);
180 family = inet_strfamily(addr->sa_family);
181
182 qdict_put(dict, "host", qstring_from_str(host));
183 qdict_put(dict, "port", qstring_from_str(port));
184 qdict_put(dict, "family", qstring_from_str(family));
185}
186
187static void add_channel_info(QDict *dict, SpiceChannelEventInfo *info)
188{
189 int tls = info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS;
190
191 qdict_put(dict, "connection-id", qint_from_int(info->connection_id));
192 qdict_put(dict, "channel-type", qint_from_int(info->type));
193 qdict_put(dict, "channel-id", qint_from_int(info->id));
194 qdict_put(dict, "tls", qbool_from_int(tls));
195}
196
cb42a870
GH
197static QList *channel_list_get(void)
198{
199 ChannelList *item;
200 QList *list;
201 QDict *dict;
202
203 list = qlist_new();
204 QTAILQ_FOREACH(item, &channel_list, link) {
205 dict = qdict_new();
206 add_addr_info(dict, &item->info->paddr, item->info->plen);
207 add_channel_info(dict, item->info);
208 qlist_append(list, dict);
209 }
210 return list;
211}
212
6f8c63fb
GH
213static void channel_event(int event, SpiceChannelEventInfo *info)
214{
215 static const int qevent[] = {
216 [ SPICE_CHANNEL_EVENT_CONNECTED ] = QEVENT_SPICE_CONNECTED,
217 [ SPICE_CHANNEL_EVENT_INITIALIZED ] = QEVENT_SPICE_INITIALIZED,
218 [ SPICE_CHANNEL_EVENT_DISCONNECTED ] = QEVENT_SPICE_DISCONNECTED,
219 };
220 QDict *server, *client;
221 QObject *data;
222
22b626e2
GH
223 /*
224 * Spice server might have called us from spice worker thread
225 * context (happens on display channel disconnects). Spice should
226 * not do that. It isn't that easy to fix it in spice and even
227 * when it is fixed we still should cover the already released
228 * spice versions. So detect that we've been called from another
229 * thread and grab the iothread lock if so before calling qemu
230 * functions.
231 */
232 bool need_lock = !pthread_equal(me, pthread_self());
233 if (need_lock) {
234 qemu_mutex_lock_iothread();
235 }
236
6f8c63fb
GH
237 client = qdict_new();
238 add_addr_info(client, &info->paddr, info->plen);
239
240 server = qdict_new();
241 add_addr_info(server, &info->laddr, info->llen);
242
243 if (event == SPICE_CHANNEL_EVENT_INITIALIZED) {
244 qdict_put(server, "auth", qstring_from_str(auth));
245 add_channel_info(client, info);
cb42a870
GH
246 channel_list_add(info);
247 }
248 if (event == SPICE_CHANNEL_EVENT_DISCONNECTED) {
249 channel_list_del(info);
6f8c63fb
GH
250 }
251
252 data = qobject_from_jsonf("{ 'client': %p, 'server': %p }",
253 QOBJECT(client), QOBJECT(server));
254 monitor_protocol_event(qevent[event], data);
255 qobject_decref(data);
22b626e2
GH
256
257 if (need_lock) {
258 qemu_mutex_unlock_iothread();
259 }
6f8c63fb
GH
260}
261
262#else /* SPICE_INTERFACE_CORE_MINOR >= 3 */
263
264static QList *channel_list_get(void)
265{
266 return NULL;
267}
268
269#endif /* SPICE_INTERFACE_CORE_MINOR >= 3 */
270
29b0040b
GH
271static SpiceCoreInterface core_interface = {
272 .base.type = SPICE_INTERFACE_CORE,
273 .base.description = "qemu core services",
274 .base.major_version = SPICE_INTERFACE_CORE_MAJOR,
275 .base.minor_version = SPICE_INTERFACE_CORE_MINOR,
276
277 .timer_add = timer_add,
278 .timer_start = timer_start,
279 .timer_cancel = timer_cancel,
280 .timer_remove = timer_remove,
281
282 .watch_add = watch_add,
283 .watch_update_mask = watch_update_mask,
284 .watch_remove = watch_remove,
6f8c63fb
GH
285
286#if SPICE_INTERFACE_CORE_MINOR >= 3
287 .channel_event = channel_event,
288#endif
29b0040b
GH
289};
290
026f773f
YH
291#ifdef SPICE_INTERFACE_MIGRATION
292typedef struct SpiceMigration {
293 SpiceMigrateInstance sin;
294 struct {
295 MonitorCompletion *cb;
296 void *opaque;
297 } connect_complete;
298} SpiceMigration;
299
300static void migrate_connect_complete_cb(SpiceMigrateInstance *sin);
301
302static const SpiceMigrateInterface migrate_interface = {
303 .base.type = SPICE_INTERFACE_MIGRATION,
304 .base.description = "migration",
305 .base.major_version = SPICE_INTERFACE_MIGRATION_MAJOR,
306 .base.minor_version = SPICE_INTERFACE_MIGRATION_MINOR,
307 .migrate_connect_complete = migrate_connect_complete_cb,
308 .migrate_end_complete = NULL,
309};
310
311static SpiceMigration spice_migrate;
312
313static void migrate_connect_complete_cb(SpiceMigrateInstance *sin)
314{
315 SpiceMigration *sm = container_of(sin, SpiceMigration, sin);
316 if (sm->connect_complete.cb) {
317 sm->connect_complete.cb(sm->connect_complete.opaque, NULL);
318 }
319 sm->connect_complete.cb = NULL;
320}
321#endif
322
9f04e09e
YH
323/* config string parsing */
324
325static int name2enum(const char *string, const char *table[], int entries)
326{
327 int i;
328
329 if (string) {
330 for (i = 0; i < entries; i++) {
331 if (!table[i]) {
332 continue;
333 }
334 if (strcmp(string, table[i]) != 0) {
335 continue;
336 }
337 return i;
338 }
339 }
340 return -1;
341}
342
343static int parse_name(const char *string, const char *optname,
344 const char *table[], int entries)
345{
346 int value = name2enum(string, table, entries);
347
348 if (value != -1) {
349 return value;
350 }
351 fprintf(stderr, "spice: invalid %s: %s\n", optname, string);
352 exit(1);
353}
354
84a23f25
GH
355static const char *stream_video_names[] = {
356 [ SPICE_STREAM_VIDEO_OFF ] = "off",
357 [ SPICE_STREAM_VIDEO_ALL ] = "all",
358 [ SPICE_STREAM_VIDEO_FILTER ] = "filter",
359};
360#define parse_stream_video(_name) \
361 name2enum(_name, stream_video_names, ARRAY_SIZE(stream_video_names))
362
9f04e09e
YH
363static const char *compression_names[] = {
364 [ SPICE_IMAGE_COMPRESS_OFF ] = "off",
365 [ SPICE_IMAGE_COMPRESS_AUTO_GLZ ] = "auto_glz",
366 [ SPICE_IMAGE_COMPRESS_AUTO_LZ ] = "auto_lz",
367 [ SPICE_IMAGE_COMPRESS_QUIC ] = "quic",
368 [ SPICE_IMAGE_COMPRESS_GLZ ] = "glz",
369 [ SPICE_IMAGE_COMPRESS_LZ ] = "lz",
370};
371#define parse_compression(_name) \
372 parse_name(_name, "image compression", \
373 compression_names, ARRAY_SIZE(compression_names))
374
375static const char *wan_compression_names[] = {
376 [ SPICE_WAN_COMPRESSION_AUTO ] = "auto",
377 [ SPICE_WAN_COMPRESSION_NEVER ] = "never",
378 [ SPICE_WAN_COMPRESSION_ALWAYS ] = "always",
379};
380#define parse_wan_compression(_name) \
381 parse_name(_name, "wan compression", \
382 wan_compression_names, ARRAY_SIZE(wan_compression_names))
383
29b0040b
GH
384/* functions for the rest of qemu */
385
cb42a870
GH
386static void info_spice_iter(QObject *obj, void *opaque)
387{
388 QDict *client;
389 Monitor *mon = opaque;
390
391 client = qobject_to_qdict(obj);
392 monitor_printf(mon, "Channel:\n");
393 monitor_printf(mon, " address: %s:%s%s\n",
394 qdict_get_str(client, "host"),
395 qdict_get_str(client, "port"),
396 qdict_get_bool(client, "tls") ? " [tls]" : "");
397 monitor_printf(mon, " session: %" PRId64 "\n",
398 qdict_get_int(client, "connection-id"));
399 monitor_printf(mon, " channel: %d:%d\n",
400 (int)qdict_get_int(client, "channel-type"),
401 (int)qdict_get_int(client, "channel-id"));
402}
403
404void do_info_spice_print(Monitor *mon, const QObject *data)
405{
406 QDict *server;
407 QList *channels;
408 const char *host;
409 int port;
410
411 server = qobject_to_qdict(data);
412 if (qdict_get_bool(server, "enabled") == 0) {
413 monitor_printf(mon, "Server: disabled\n");
414 return;
415 }
416
417 monitor_printf(mon, "Server:\n");
418 host = qdict_get_str(server, "host");
419 port = qdict_get_try_int(server, "port", -1);
420 if (port != -1) {
421 monitor_printf(mon, " address: %s:%d\n", host, port);
422 }
423 port = qdict_get_try_int(server, "tls-port", -1);
424 if (port != -1) {
425 monitor_printf(mon, " address: %s:%d [tls]\n", host, port);
426 }
427 monitor_printf(mon, " auth: %s\n", qdict_get_str(server, "auth"));
8df0c7e8
AL
428 monitor_printf(mon, " compiled: %s\n",
429 qdict_get_str(server, "compiled-version"));
cb42a870
GH
430
431 channels = qdict_get_qlist(server, "channels");
432 if (qlist_empty(channels)) {
433 monitor_printf(mon, "Channels: none\n");
434 } else {
435 qlist_iter(channels, info_spice_iter, mon);
436 }
437}
438
439void do_info_spice(Monitor *mon, QObject **ret_data)
440{
441 QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
442 QDict *server;
443 QList *clist;
444 const char *addr;
445 int port, tls_port;
8df0c7e8 446 char version_string[20]; /* 12 = |255.255.255\0| is the max */
cb42a870
GH
447
448 if (!spice_server) {
449 *ret_data = qobject_from_jsonf("{ 'enabled': false }");
450 return;
451 }
452
453 addr = qemu_opt_get(opts, "addr");
454 port = qemu_opt_get_number(opts, "port", 0);
455 tls_port = qemu_opt_get_number(opts, "tls-port", 0);
456 clist = channel_list_get();
457
458 server = qdict_new();
459 qdict_put(server, "enabled", qbool_from_int(true));
460 qdict_put(server, "auth", qstring_from_str(auth));
461 qdict_put(server, "host", qstring_from_str(addr ? addr : "0.0.0.0"));
8df0c7e8
AL
462 snprintf(version_string, sizeof(version_string), "%d.%d.%d",
463 (SPICE_SERVER_VERSION & 0xff0000) >> 16,
464 (SPICE_SERVER_VERSION & 0xff00) >> 8,
465 SPICE_SERVER_VERSION & 0xff);
466 qdict_put(server, "compiled-version", qstring_from_str(version_string));
cb42a870
GH
467 if (port) {
468 qdict_put(server, "port", qint_from_int(port));
469 }
470 if (tls_port) {
471 qdict_put(server, "tls-port", qint_from_int(tls_port));
472 }
473 if (clist) {
474 qdict_put(server, "channels", clist);
475 }
476
477 *ret_data = QOBJECT(server);
478}
479
9e8dd451 480static void migration_state_notifier(Notifier *notifier, void *data)
e866e239 481{
7073693b 482 MigrationState *s = data;
e866e239 483
026f773f
YH
484 if (migration_is_active(s)) {
485#ifdef SPICE_INTERFACE_MIGRATION
486 spice_server_migrate_start(spice_server);
487#endif
488 } else if (migration_has_finished(s)) {
e866e239 489#if SPICE_SERVER_VERSION >= 0x000701 /* 0.7.1 */
026f773f 490#ifndef SPICE_INTERFACE_MIGRATION
e866e239 491 spice_server_migrate_switch(spice_server);
026f773f
YH
492#else
493 spice_server_migrate_end(spice_server, true);
494 } else if (migration_has_failed(s)) {
495 spice_server_migrate_end(spice_server, false);
496#endif
e866e239
GH
497#endif
498 }
499}
500
501int qemu_spice_migrate_info(const char *hostname, int port, int tls_port,
edc5cb1a
YH
502 const char *subject,
503 MonitorCompletion *cb, void *opaque)
e866e239 504{
edc5cb1a 505 int ret;
026f773f
YH
506#ifdef SPICE_INTERFACE_MIGRATION
507 spice_migrate.connect_complete.cb = cb;
508 spice_migrate.connect_complete.opaque = opaque;
509 ret = spice_server_migrate_connect(spice_server, hostname,
510 port, tls_port, subject);
511#else
edc5cb1a
YH
512 ret = spice_server_migrate_info(spice_server, hostname,
513 port, tls_port, subject);
514 cb(opaque, NULL);
026f773f 515#endif
edc5cb1a 516 return ret;
e866e239
GH
517}
518
17b6dea0
GH
519static int add_channel(const char *name, const char *value, void *opaque)
520{
521 int security = 0;
522 int rc;
523
524 if (strcmp(name, "tls-channel") == 0) {
525 security = SPICE_CHANNEL_SECURITY_SSL;
526 }
527 if (strcmp(name, "plaintext-channel") == 0) {
528 security = SPICE_CHANNEL_SECURITY_NONE;
529 }
530 if (security == 0) {
531 return 0;
532 }
533 if (strcmp(value, "default") == 0) {
534 rc = spice_server_set_channel_security(spice_server, NULL, security);
535 } else {
536 rc = spice_server_set_channel_security(spice_server, value, security);
537 }
538 if (rc != 0) {
539 fprintf(stderr, "spice: failed to set channel security for %s\n", value);
540 exit(1);
541 }
542 return 0;
543}
544
29b0040b
GH
545void qemu_spice_init(void)
546{
547 QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
333b0eeb 548 const char *password, *str, *x509_dir, *addr,
c448e855
GH
549 *x509_key_password = NULL,
550 *x509_dh_file = NULL,
551 *tls_ciphers = NULL;
552 char *x509_key_file = NULL,
553 *x509_cert_file = NULL,
554 *x509_cacert_file = NULL;
f61d6960 555 int port, tls_port, len, addr_flags;
9f04e09e
YH
556 spice_image_compression_t compression;
557 spice_wan_compression_t wan_compr;
29b0040b 558
22b626e2
GH
559 me = pthread_self();
560
561 if (!opts) {
29b0040b
GH
562 return;
563 }
564 port = qemu_opt_get_number(opts, "port", 0);
c448e855
GH
565 tls_port = qemu_opt_get_number(opts, "tls-port", 0);
566 if (!port && !tls_port) {
df9cb669
GH
567 fprintf(stderr, "neither port nor tls-port specified for spice.");
568 exit(1);
569 }
570 if (port < 0 || port > 65535) {
571 fprintf(stderr, "spice port is out of range");
572 exit(1);
573 }
574 if (tls_port < 0 || tls_port > 65535) {
575 fprintf(stderr, "spice tls-port is out of range");
576 exit(1);
29b0040b
GH
577 }
578 password = qemu_opt_get(opts, "password");
579
c448e855
GH
580 if (tls_port) {
581 x509_dir = qemu_opt_get(opts, "x509-dir");
582 if (NULL == x509_dir) {
583 x509_dir = ".";
584 }
585 len = strlen(x509_dir) + 32;
586
587 str = qemu_opt_get(opts, "x509-key-file");
588 if (str) {
7267c094 589 x509_key_file = g_strdup(str);
c448e855 590 } else {
7267c094 591 x509_key_file = g_malloc(len);
c448e855
GH
592 snprintf(x509_key_file, len, "%s/%s", x509_dir, X509_SERVER_KEY_FILE);
593 }
594
595 str = qemu_opt_get(opts, "x509-cert-file");
596 if (str) {
7267c094 597 x509_cert_file = g_strdup(str);
c448e855 598 } else {
7267c094 599 x509_cert_file = g_malloc(len);
c448e855
GH
600 snprintf(x509_cert_file, len, "%s/%s", x509_dir, X509_SERVER_CERT_FILE);
601 }
602
603 str = qemu_opt_get(opts, "x509-cacert-file");
604 if (str) {
7267c094 605 x509_cacert_file = g_strdup(str);
c448e855 606 } else {
7267c094 607 x509_cacert_file = g_malloc(len);
c448e855
GH
608 snprintf(x509_cacert_file, len, "%s/%s", x509_dir, X509_CA_CERT_FILE);
609 }
610
611 x509_key_password = qemu_opt_get(opts, "x509-key-password");
612 x509_dh_file = qemu_opt_get(opts, "x509-dh-file");
613 tls_ciphers = qemu_opt_get(opts, "tls-ciphers");
614 }
615
333b0eeb
GH
616 addr = qemu_opt_get(opts, "addr");
617 addr_flags = 0;
618 if (qemu_opt_get_bool(opts, "ipv4", 0)) {
619 addr_flags |= SPICE_ADDR_FLAG_IPV4_ONLY;
620 } else if (qemu_opt_get_bool(opts, "ipv6", 0)) {
621 addr_flags |= SPICE_ADDR_FLAG_IPV6_ONLY;
622 }
623
29b0040b 624 spice_server = spice_server_new();
333b0eeb 625 spice_server_set_addr(spice_server, addr ? addr : "", addr_flags);
c448e855
GH
626 if (port) {
627 spice_server_set_port(spice_server, port);
628 }
629 if (tls_port) {
630 spice_server_set_tls(spice_server, tls_port,
631 x509_cacert_file,
632 x509_cert_file,
633 x509_key_file,
634 x509_key_password,
635 x509_dh_file,
636 tls_ciphers);
637 }
29b0040b
GH
638 if (password) {
639 spice_server_set_ticket(spice_server, password, 0, 0, 0);
640 }
48b3ed0a
MAL
641 if (qemu_opt_get_bool(opts, "sasl", 0)) {
642#if SPICE_SERVER_VERSION >= 0x000900 /* 0.9.0 */
643 if (spice_server_set_sasl_appname(spice_server, "qemu") == -1 ||
644 spice_server_set_sasl(spice_server, 1) == -1) {
645 fprintf(stderr, "spice: failed to enable sasl\n");
646 exit(1);
647 }
648#else
649 fprintf(stderr, "spice: sasl is not available (spice >= 0.9 required)\n");
650 exit(1);
651#endif
652 }
29b0040b 653 if (qemu_opt_get_bool(opts, "disable-ticketing", 0)) {
6f8c63fb 654 auth = "none";
29b0040b
GH
655 spice_server_set_noauth(spice_server);
656 }
657
d4970b07
HG
658#if SPICE_SERVER_VERSION >= 0x000801
659 if (qemu_opt_get_bool(opts, "disable-copy-paste", 0)) {
660 spice_server_set_agent_copypaste(spice_server, false);
661 }
662#endif
663
9f04e09e
YH
664 compression = SPICE_IMAGE_COMPRESS_AUTO_GLZ;
665 str = qemu_opt_get(opts, "image-compression");
666 if (str) {
667 compression = parse_compression(str);
668 }
669 spice_server_set_image_compression(spice_server, compression);
670
671 wan_compr = SPICE_WAN_COMPRESSION_AUTO;
672 str = qemu_opt_get(opts, "jpeg-wan-compression");
673 if (str) {
674 wan_compr = parse_wan_compression(str);
675 }
676 spice_server_set_jpeg_compression(spice_server, wan_compr);
677
678 wan_compr = SPICE_WAN_COMPRESSION_AUTO;
679 str = qemu_opt_get(opts, "zlib-glz-wan-compression");
680 if (str) {
681 wan_compr = parse_wan_compression(str);
682 }
683 spice_server_set_zlib_glz_compression(spice_server, wan_compr);
29b0040b 684
84a23f25
GH
685 str = qemu_opt_get(opts, "streaming-video");
686 if (str) {
f61d6960 687 int streaming_video = parse_stream_video(str);
84a23f25
GH
688 spice_server_set_streaming_video(spice_server, streaming_video);
689 }
690
691 spice_server_set_agent_mouse
692 (spice_server, qemu_opt_get_bool(opts, "agent-mouse", 1));
693 spice_server_set_playback_compression
694 (spice_server, qemu_opt_get_bool(opts, "playback-compression", 1));
695
17b6dea0
GH
696 qemu_opt_foreach(opts, add_channel, NULL, 0);
697
fba810f1
GH
698 if (0 != spice_server_init(spice_server, &core_interface)) {
699 fprintf(stderr, "failed to initialize spice server");
700 exit(1);
701 };
29b0040b 702 using_spice = 1;
864401c2 703
e866e239
GH
704 migration_state.notify = migration_state_notifier;
705 add_migration_state_change_notifier(&migration_state);
026f773f
YH
706#ifdef SPICE_INTERFACE_MIGRATION
707 spice_migrate.sin.base.sif = &migrate_interface.base;
708 spice_migrate.connect_complete.cb = NULL;
709 qemu_spice_add_interface(&spice_migrate.sin.base);
710#endif
e866e239 711
864401c2 712 qemu_spice_input_init();
3e313753 713 qemu_spice_audio_init();
c448e855 714
7267c094
AL
715 g_free(x509_key_file);
716 g_free(x509_cert_file);
717 g_free(x509_cacert_file);
29b0040b
GH
718}
719
720int qemu_spice_add_interface(SpiceBaseInstance *sin)
721{
a19cbfb3
GH
722 if (!spice_server) {
723 if (QTAILQ_FIRST(&qemu_spice_opts.head) != NULL) {
724 fprintf(stderr, "Oops: spice configured but not active\n");
725 exit(1);
726 }
727 /*
728 * Create a spice server instance.
729 * It does *not* listen on the network.
730 * It handles QXL local rendering only.
731 *
732 * With a command line like '-vnc :0 -vga qxl' you'll end up here.
733 */
734 spice_server = spice_server_new();
735 spice_server_init(spice_server, &core_interface);
736 }
29b0040b
GH
737 return spice_server_add_interface(spice_server, sin);
738}
739
7572150c
GH
740static int qemu_spice_set_ticket(bool fail_if_conn, bool disconnect_if_conn)
741{
742 time_t lifetime, now = time(NULL);
743 char *passwd;
744
745 if (now < auth_expires) {
746 passwd = auth_passwd;
747 lifetime = (auth_expires - now);
748 if (lifetime > INT_MAX) {
749 lifetime = INT_MAX;
750 }
751 } else {
752 passwd = NULL;
753 lifetime = 1;
754 }
755 return spice_server_set_ticket(spice_server, passwd, lifetime,
756 fail_if_conn, disconnect_if_conn);
757}
758
759int qemu_spice_set_passwd(const char *passwd,
760 bool fail_if_conn, bool disconnect_if_conn)
761{
762 free(auth_passwd);
763 auth_passwd = strdup(passwd);
764 return qemu_spice_set_ticket(fail_if_conn, disconnect_if_conn);
765}
766
767int qemu_spice_set_pw_expire(time_t expires)
768{
769 auth_expires = expires;
770 return qemu_spice_set_ticket(false, false);
771}
772
29b0040b
GH
773static void spice_register_config(void)
774{
775 qemu_add_opts(&qemu_spice_opts);
776}
777machine_init(spice_register_config);
778
779static void spice_initialize(void)
780{
781 qemu_spice_init();
782}
783device_init(spice_initialize);