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