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