]> git.ipfire.org Git - thirdparty/qemu.git/blame - ui/spice-core.c
qerror: Move #include out of qerror.h
[thirdparty/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>
29b0040b 19
6f8c63fb 20#include <netdb.h>
9c17d615 21#include "sysemu/sysemu.h"
6f8c63fb 22
29b0040b 23#include "qemu-common.h"
28ecbaee 24#include "ui/qemu-spice.h"
d49b6836 25#include "qemu/error-report.h"
1de7afc9
PB
26#include "qemu/thread.h"
27#include "qemu/timer.h"
28#include "qemu/queue.h"
c448e855 29#include "qemu-x509.h"
1de7afc9 30#include "qemu/sockets.h"
d1f29646 31#include "qmp-commands.h"
7b1b5d19
PB
32#include "qapi/qmp/qint.h"
33#include "qapi/qmp/qbool.h"
34#include "qapi/qmp/qstring.h"
35#include "qapi/qmp/qjson.h"
1de7afc9 36#include "qemu/notify.h"
caf71f86 37#include "migration/migration.h"
e866e239 38#include "hw/hw.h"
28ecbaee 39#include "ui/spice-display.h"
7cfadb6b 40#include "qapi-event.h"
29b0040b
GH
41
42/* core bits */
43
44static SpiceServer *spice_server;
e866e239 45static Notifier migration_state;
6f8c63fb 46static const char *auth = "spice";
7572150c
GH
47static char *auth_passwd;
48static time_t auth_expires = TIME_MAX;
61c4efe2 49static int spice_migration_completed;
7cc6a25f 50static int spice_display_is_running;
a76a2f72 51static int spice_have_target_host;
29b0040b
GH
52int using_spice = 0;
53
f9ab6091 54static QemuThread me;
22b626e2 55
29b0040b
GH
56struct SpiceTimer {
57 QEMUTimer *timer;
58 QTAILQ_ENTRY(SpiceTimer) next;
59};
60static QTAILQ_HEAD(, SpiceTimer) timers = QTAILQ_HEAD_INITIALIZER(timers);
61
62static SpiceTimer *timer_add(SpiceTimerFunc func, void *opaque)
63{
64 SpiceTimer *timer;
65
7267c094 66 timer = g_malloc0(sizeof(*timer));
bc72ad67 67 timer->timer = timer_new_ms(QEMU_CLOCK_REALTIME, func, opaque);
29b0040b
GH
68 QTAILQ_INSERT_TAIL(&timers, timer, next);
69 return timer;
70}
71
72static void timer_start(SpiceTimer *timer, uint32_t ms)
73{
bc72ad67 74 timer_mod(timer->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + ms);
29b0040b
GH
75}
76
77static void timer_cancel(SpiceTimer *timer)
78{
bc72ad67 79 timer_del(timer->timer);
29b0040b
GH
80}
81
82static void timer_remove(SpiceTimer *timer)
83{
bc72ad67
AB
84 timer_del(timer->timer);
85 timer_free(timer->timer);
29b0040b 86 QTAILQ_REMOVE(&timers, timer, next);
7267c094 87 g_free(timer);
29b0040b
GH
88}
89
90struct SpiceWatch {
91 int fd;
92 int event_mask;
93 SpiceWatchFunc func;
94 void *opaque;
95 QTAILQ_ENTRY(SpiceWatch) next;
96};
97static QTAILQ_HEAD(, SpiceWatch) watches = QTAILQ_HEAD_INITIALIZER(watches);
98
99static void watch_read(void *opaque)
100{
101 SpiceWatch *watch = opaque;
102 watch->func(watch->fd, SPICE_WATCH_EVENT_READ, watch->opaque);
103}
104
105static void watch_write(void *opaque)
106{
107 SpiceWatch *watch = opaque;
108 watch->func(watch->fd, SPICE_WATCH_EVENT_WRITE, watch->opaque);
109}
110
111static void watch_update_mask(SpiceWatch *watch, int event_mask)
112{
113 IOHandler *on_read = NULL;
114 IOHandler *on_write = NULL;
115
116 watch->event_mask = event_mask;
117 if (watch->event_mask & SPICE_WATCH_EVENT_READ) {
118 on_read = watch_read;
119 }
120 if (watch->event_mask & SPICE_WATCH_EVENT_WRITE) {
3d6d306c 121 on_write = watch_write;
29b0040b
GH
122 }
123 qemu_set_fd_handler(watch->fd, on_read, on_write, watch);
124}
125
126static SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque)
127{
128 SpiceWatch *watch;
129
7267c094 130 watch = g_malloc0(sizeof(*watch));
29b0040b
GH
131 watch->fd = fd;
132 watch->func = func;
133 watch->opaque = opaque;
134 QTAILQ_INSERT_TAIL(&watches, watch, next);
135
136 watch_update_mask(watch, event_mask);
137 return watch;
138}
139
140static void watch_remove(SpiceWatch *watch)
141{
08cc67f3 142 qemu_set_fd_handler(watch->fd, NULL, NULL, NULL);
29b0040b 143 QTAILQ_REMOVE(&watches, watch, next);
7267c094 144 g_free(watch);
29b0040b
GH
145}
146
cb42a870
GH
147typedef struct ChannelList ChannelList;
148struct ChannelList {
149 SpiceChannelEventInfo *info;
150 QTAILQ_ENTRY(ChannelList) link;
151};
152static QTAILQ_HEAD(, ChannelList) channel_list = QTAILQ_HEAD_INITIALIZER(channel_list);
153
154static void channel_list_add(SpiceChannelEventInfo *info)
155{
156 ChannelList *item;
157
7267c094 158 item = g_malloc0(sizeof(*item));
cb42a870
GH
159 item->info = info;
160 QTAILQ_INSERT_TAIL(&channel_list, item, link);
161}
162
163static void channel_list_del(SpiceChannelEventInfo *info)
164{
165 ChannelList *item;
166
167 QTAILQ_FOREACH(item, &channel_list, link) {
168 if (item->info != info) {
169 continue;
170 }
171 QTAILQ_REMOVE(&channel_list, item, link);
7267c094 172 g_free(item);
cb42a870
GH
173 return;
174 }
175}
176
7cfadb6b 177static void add_addr_info(SpiceBasicInfo *info, struct sockaddr *addr, int len)
6f8c63fb
GH
178{
179 char host[NI_MAXHOST], port[NI_MAXSERV];
6f8c63fb
GH
180
181 getnameinfo(addr, len, host, sizeof(host), port, sizeof(port),
182 NI_NUMERICHOST | NI_NUMERICSERV);
6f8c63fb 183
7cfadb6b
WX
184 info->host = g_strdup(host);
185 info->port = g_strdup(port);
186 info->family = inet_netfamily(addr->sa_family);
6f8c63fb
GH
187}
188
7cfadb6b 189static void add_channel_info(SpiceChannel *sc, SpiceChannelEventInfo *info)
6f8c63fb
GH
190{
191 int tls = info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS;
192
7cfadb6b
WX
193 sc->connection_id = info->connection_id;
194 sc->channel_type = info->type;
195 sc->channel_id = info->id;
196 sc->tls = !!tls;
6f8c63fb
GH
197}
198
199static void channel_event(int event, SpiceChannelEventInfo *info)
200{
7cfadb6b
WX
201 SpiceServerInfo *server = g_malloc0(sizeof(*server));
202 SpiceChannel *client = g_malloc0(sizeof(*client));
203 server->base = g_malloc0(sizeof(*server->base));
204 client->base = g_malloc0(sizeof(*client->base));
6f8c63fb 205
22b626e2
GH
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 */
f9ab6091 215 bool need_lock = !qemu_thread_is_self(&me);
22b626e2
GH
216 if (need_lock) {
217 qemu_mutex_lock_iothread();
218 }
219
faa98223 220 if (info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT) {
7cfadb6b 221 add_addr_info(client->base, (struct sockaddr *)&info->paddr_ext,
faa98223 222 info->plen_ext);
7cfadb6b 223 add_addr_info(server->base, (struct sockaddr *)&info->laddr_ext,
faa98223
YH
224 info->llen_ext);
225 } else {
339a475f
CF
226 error_report("spice: %s, extended address is expected",
227 __func__);
faa98223 228 }
6f8c63fb 229
7cfadb6b
WX
230 switch (event) {
231 case SPICE_CHANNEL_EVENT_CONNECTED:
232 qapi_event_send_spice_connected(server->base, client->base, &error_abort);
233 break;
234 case SPICE_CHANNEL_EVENT_INITIALIZED:
235 if (auth) {
236 server->has_auth = true;
237 server->auth = g_strdup(auth);
238 }
6f8c63fb 239 add_channel_info(client, info);
cb42a870 240 channel_list_add(info);
7cfadb6b
WX
241 qapi_event_send_spice_initialized(server, client, &error_abort);
242 break;
243 case SPICE_CHANNEL_EVENT_DISCONNECTED:
cb42a870 244 channel_list_del(info);
7cfadb6b
WX
245 qapi_event_send_spice_disconnected(server->base, client->base, &error_abort);
246 break;
247 default:
248 break;
6f8c63fb
GH
249 }
250
22b626e2
GH
251 if (need_lock) {
252 qemu_mutex_unlock_iothread();
253 }
7cfadb6b
WX
254
255 qapi_free_SpiceServerInfo(server);
256 qapi_free_SpiceChannel(client);
6f8c63fb
GH
257}
258
29b0040b
GH
259static SpiceCoreInterface core_interface = {
260 .base.type = SPICE_INTERFACE_CORE,
261 .base.description = "qemu core services",
262 .base.major_version = SPICE_INTERFACE_CORE_MAJOR,
263 .base.minor_version = SPICE_INTERFACE_CORE_MINOR,
264
265 .timer_add = timer_add,
266 .timer_start = timer_start,
267 .timer_cancel = timer_cancel,
268 .timer_remove = timer_remove,
269
270 .watch_add = watch_add,
271 .watch_update_mask = watch_update_mask,
272 .watch_remove = watch_remove,
6f8c63fb 273
6f8c63fb 274 .channel_event = channel_event,
29b0040b
GH
275};
276
026f773f 277static void migrate_connect_complete_cb(SpiceMigrateInstance *sin);
2fdd16e2 278static void migrate_end_complete_cb(SpiceMigrateInstance *sin);
026f773f
YH
279
280static const SpiceMigrateInterface migrate_interface = {
281 .base.type = SPICE_INTERFACE_MIGRATION,
282 .base.description = "migration",
283 .base.major_version = SPICE_INTERFACE_MIGRATION_MAJOR,
284 .base.minor_version = SPICE_INTERFACE_MIGRATION_MINOR,
285 .migrate_connect_complete = migrate_connect_complete_cb,
2fdd16e2 286 .migrate_end_complete = migrate_end_complete_cb,
026f773f
YH
287};
288
3b5704b2 289static SpiceMigrateInstance spice_migrate;
026f773f
YH
290
291static void migrate_connect_complete_cb(SpiceMigrateInstance *sin)
292{
3b5704b2 293 /* nothing, but libspice-server expects this cb being present. */
026f773f 294}
2fdd16e2
YH
295
296static void migrate_end_complete_cb(SpiceMigrateInstance *sin)
297{
7cfadb6b 298 qapi_event_send_spice_migrate_completed(&error_abort);
61c4efe2 299 spice_migration_completed = true;
2fdd16e2 300}
026f773f 301
9f04e09e
YH
302/* config string parsing */
303
304static int name2enum(const char *string, const char *table[], int entries)
305{
306 int i;
307
308 if (string) {
309 for (i = 0; i < entries; i++) {
310 if (!table[i]) {
311 continue;
312 }
313 if (strcmp(string, table[i]) != 0) {
314 continue;
315 }
316 return i;
317 }
318 }
319 return -1;
320}
321
322static int parse_name(const char *string, const char *optname,
323 const char *table[], int entries)
324{
325 int value = name2enum(string, table, entries);
326
327 if (value != -1) {
328 return value;
329 }
339a475f 330 error_report("spice: invalid %s: %s", optname, string);
9f04e09e
YH
331 exit(1);
332}
333
84a23f25
GH
334static const char *stream_video_names[] = {
335 [ SPICE_STREAM_VIDEO_OFF ] = "off",
336 [ SPICE_STREAM_VIDEO_ALL ] = "all",
337 [ SPICE_STREAM_VIDEO_FILTER ] = "filter",
338};
339#define parse_stream_video(_name) \
835cab85
CF
340 parse_name(_name, "stream video control", \
341 stream_video_names, ARRAY_SIZE(stream_video_names))
84a23f25 342
9f04e09e
YH
343static const char *compression_names[] = {
344 [ SPICE_IMAGE_COMPRESS_OFF ] = "off",
345 [ SPICE_IMAGE_COMPRESS_AUTO_GLZ ] = "auto_glz",
346 [ SPICE_IMAGE_COMPRESS_AUTO_LZ ] = "auto_lz",
347 [ SPICE_IMAGE_COMPRESS_QUIC ] = "quic",
348 [ SPICE_IMAGE_COMPRESS_GLZ ] = "glz",
349 [ SPICE_IMAGE_COMPRESS_LZ ] = "lz",
350};
351#define parse_compression(_name) \
352 parse_name(_name, "image compression", \
353 compression_names, ARRAY_SIZE(compression_names))
354
355static const char *wan_compression_names[] = {
356 [ SPICE_WAN_COMPRESSION_AUTO ] = "auto",
357 [ SPICE_WAN_COMPRESSION_NEVER ] = "never",
358 [ SPICE_WAN_COMPRESSION_ALWAYS ] = "always",
359};
360#define parse_wan_compression(_name) \
361 parse_name(_name, "wan compression", \
362 wan_compression_names, ARRAY_SIZE(wan_compression_names))
363
29b0040b
GH
364/* functions for the rest of qemu */
365
d1f29646 366static SpiceChannelList *qmp_query_spice_channels(void)
cb42a870 367{
d1f29646
LC
368 SpiceChannelList *cur_item = NULL, *head = NULL;
369 ChannelList *item;
cb42a870 370
d1f29646
LC
371 QTAILQ_FOREACH(item, &channel_list, link) {
372 SpiceChannelList *chan;
373 char host[NI_MAXHOST], port[NI_MAXSERV];
faa98223
YH
374 struct sockaddr *paddr;
375 socklen_t plen;
d1f29646 376
a4164270 377 assert(item->info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT);
26defe81 378
d1f29646
LC
379 chan = g_malloc0(sizeof(*chan));
380 chan->value = g_malloc0(sizeof(*chan->value));
a589569f 381 chan->value->base = g_malloc0(sizeof(*chan->value->base));
d1f29646 382
26defe81
MAL
383 paddr = (struct sockaddr *)&item->info->paddr_ext;
384 plen = item->info->plen_ext;
faa98223 385 getnameinfo(paddr, plen,
d1f29646
LC
386 host, sizeof(host), port, sizeof(port),
387 NI_NUMERICHOST | NI_NUMERICSERV);
a589569f
WX
388 chan->value->base->host = g_strdup(host);
389 chan->value->base->port = g_strdup(port);
390 chan->value->base->family = inet_netfamily(paddr->sa_family);
d1f29646
LC
391
392 chan->value->connection_id = item->info->connection_id;
393 chan->value->channel_type = item->info->type;
394 chan->value->channel_id = item->info->id;
395 chan->value->tls = item->info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS;
396
397 /* XXX: waiting for the qapi to support GSList */
398 if (!cur_item) {
399 head = cur_item = chan;
400 } else {
401 cur_item->next = chan;
402 cur_item = chan;
403 }
cb42a870
GH
404 }
405
d1f29646 406 return head;
cb42a870
GH
407}
408
4d454574
PB
409static QemuOptsList qemu_spice_opts = {
410 .name = "spice",
411 .head = QTAILQ_HEAD_INITIALIZER(qemu_spice_opts.head),
412 .desc = {
413 {
414 .name = "port",
415 .type = QEMU_OPT_NUMBER,
416 },{
417 .name = "tls-port",
418 .type = QEMU_OPT_NUMBER,
419 },{
420 .name = "addr",
421 .type = QEMU_OPT_STRING,
422 },{
423 .name = "ipv4",
424 .type = QEMU_OPT_BOOL,
425 },{
426 .name = "ipv6",
427 .type = QEMU_OPT_BOOL,
fe4831b1
MAL
428#ifdef SPICE_ADDR_FLAG_UNIX_ONLY
429 },{
430 .name = "unix",
431 .type = QEMU_OPT_BOOL,
432#endif
4d454574
PB
433 },{
434 .name = "password",
435 .type = QEMU_OPT_STRING,
436 },{
437 .name = "disable-ticketing",
438 .type = QEMU_OPT_BOOL,
439 },{
440 .name = "disable-copy-paste",
441 .type = QEMU_OPT_BOOL,
5ad24e5f
HG
442 },{
443 .name = "disable-agent-file-xfer",
444 .type = QEMU_OPT_BOOL,
4d454574
PB
445 },{
446 .name = "sasl",
447 .type = QEMU_OPT_BOOL,
448 },{
449 .name = "x509-dir",
450 .type = QEMU_OPT_STRING,
451 },{
452 .name = "x509-key-file",
453 .type = QEMU_OPT_STRING,
454 },{
455 .name = "x509-key-password",
456 .type = QEMU_OPT_STRING,
457 },{
458 .name = "x509-cert-file",
459 .type = QEMU_OPT_STRING,
460 },{
461 .name = "x509-cacert-file",
462 .type = QEMU_OPT_STRING,
463 },{
464 .name = "x509-dh-key-file",
465 .type = QEMU_OPT_STRING,
466 },{
467 .name = "tls-ciphers",
468 .type = QEMU_OPT_STRING,
469 },{
470 .name = "tls-channel",
471 .type = QEMU_OPT_STRING,
472 },{
473 .name = "plaintext-channel",
474 .type = QEMU_OPT_STRING,
475 },{
476 .name = "image-compression",
477 .type = QEMU_OPT_STRING,
478 },{
479 .name = "jpeg-wan-compression",
480 .type = QEMU_OPT_STRING,
481 },{
482 .name = "zlib-glz-wan-compression",
483 .type = QEMU_OPT_STRING,
484 },{
485 .name = "streaming-video",
486 .type = QEMU_OPT_STRING,
487 },{
488 .name = "agent-mouse",
489 .type = QEMU_OPT_BOOL,
490 },{
491 .name = "playback-compression",
492 .type = QEMU_OPT_BOOL,
493 }, {
494 .name = "seamless-migration",
495 .type = QEMU_OPT_BOOL,
496 },
497 { /* end of list */ }
498 },
499};
500
d1f29646 501SpiceInfo *qmp_query_spice(Error **errp)
cb42a870
GH
502{
503 QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
cb42a870 504 int port, tls_port;
d1f29646
LC
505 const char *addr;
506 SpiceInfo *info;
6735aa99
CF
507 unsigned int major;
508 unsigned int minor;
509 unsigned int micro;
cb42a870 510
d1f29646
LC
511 info = g_malloc0(sizeof(*info));
512
3bb781f3 513 if (!spice_server || !opts) {
d1f29646
LC
514 info->enabled = false;
515 return info;
cb42a870
GH
516 }
517
d1f29646 518 info->enabled = true;
61c4efe2 519 info->migrated = spice_migration_completed;
d1f29646 520
cb42a870
GH
521 addr = qemu_opt_get(opts, "addr");
522 port = qemu_opt_get_number(opts, "port", 0);
523 tls_port = qemu_opt_get_number(opts, "tls-port", 0);
cb42a870 524
d1f29646
LC
525 info->has_auth = true;
526 info->auth = g_strdup(auth);
527
528 info->has_host = true;
4f60af9a 529 info->host = g_strdup(addr ? addr : "*");
d1f29646
LC
530
531 info->has_compiled_version = true;
6735aa99
CF
532 major = (SPICE_SERVER_VERSION & 0xff0000) >> 16;
533 minor = (SPICE_SERVER_VERSION & 0xff00) >> 8;
534 micro = SPICE_SERVER_VERSION & 0xff;
535 info->compiled_version = g_strdup_printf("%d.%d.%d", major, minor, micro);
d1f29646 536
cb42a870 537 if (port) {
d1f29646
LC
538 info->has_port = true;
539 info->port = port;
cb42a870
GH
540 }
541 if (tls_port) {
d1f29646
LC
542 info->has_tls_port = true;
543 info->tls_port = tls_port;
cb42a870
GH
544 }
545
4efee029
AL
546 info->mouse_mode = spice_server_is_server_mouse(spice_server) ?
547 SPICE_QUERY_MOUSE_MODE_SERVER :
548 SPICE_QUERY_MOUSE_MODE_CLIENT;
67be6726 549
d1f29646
LC
550 /* for compatibility with the original command */
551 info->has_channels = true;
552 info->channels = qmp_query_spice_channels();
553
554 return info;
cb42a870
GH
555}
556
9e8dd451 557static void migration_state_notifier(Notifier *notifier, void *data)
e866e239 558{
7073693b 559 MigrationState *s = data;
e866e239 560
a76a2f72
GH
561 if (!spice_have_target_host) {
562 return;
563 }
564
02edd2e7 565 if (migration_in_setup(s)) {
026f773f 566 spice_server_migrate_start(spice_server);
026f773f 567 } else if (migration_has_finished(s)) {
026f773f 568 spice_server_migrate_end(spice_server, true);
a76a2f72 569 spice_have_target_host = false;
026f773f
YH
570 } else if (migration_has_failed(s)) {
571 spice_server_migrate_end(spice_server, false);
a76a2f72 572 spice_have_target_host = false;
e866e239
GH
573 }
574}
575
576int qemu_spice_migrate_info(const char *hostname, int port, int tls_port,
3b5704b2 577 const char *subject)
e866e239 578{
edc5cb1a 579 int ret;
67be6726 580
026f773f
YH
581 ret = spice_server_migrate_connect(spice_server, hostname,
582 port, tls_port, subject);
a76a2f72 583 spice_have_target_host = true;
edc5cb1a 584 return ret;
e866e239
GH
585}
586
71df1d83
MA
587static int add_channel(void *opaque, const char *name, const char *value,
588 Error **errp)
17b6dea0
GH
589{
590 int security = 0;
591 int rc;
592
593 if (strcmp(name, "tls-channel") == 0) {
35c63329
CF
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 }
17b6dea0
GH
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) {
339a475f 614 error_report("spice: failed to set channel security for %s", value);
17b6dea0
GH
615 exit(1);
616 }
617 return 0;
618}
619
f5bb039c
YH
620static void vm_change_state_handler(void *opaque, int running,
621 RunState state)
622{
f5bb039c 623 if (running) {
71d388d4 624 qemu_spice_display_start();
f5bb039c 625 } else {
71d388d4 626 qemu_spice_display_stop();
f5bb039c 627 }
f5bb039c
YH
628}
629
29b0040b
GH
630void qemu_spice_init(void)
631{
632 QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
333b0eeb 633 const char *password, *str, *x509_dir, *addr,
c448e855
GH
634 *x509_key_password = NULL,
635 *x509_dh_file = NULL,
636 *tls_ciphers = NULL;
637 char *x509_key_file = NULL,
638 *x509_cert_file = NULL,
639 *x509_cacert_file = NULL;
6735aa99 640 int port, tls_port, addr_flags;
9f04e09e
YH
641 spice_image_compression_t compression;
642 spice_wan_compression_t wan_compr;
8c957053 643 bool seamless_migration;
29b0040b 644
f9ab6091 645 qemu_thread_get_self(&me);
22b626e2 646
ad1be899 647 if (!opts) {
29b0040b
GH
648 return;
649 }
650 port = qemu_opt_get_number(opts, "port", 0);
c448e855 651 tls_port = qemu_opt_get_number(opts, "tls-port", 0);
df9cb669 652 if (port < 0 || port > 65535) {
339a475f 653 error_report("spice port is out of range");
df9cb669
GH
654 exit(1);
655 }
656 if (tls_port < 0 || tls_port > 65535) {
339a475f 657 error_report("spice tls-port is out of range");
df9cb669 658 exit(1);
29b0040b
GH
659 }
660 password = qemu_opt_get(opts, "password");
661
c448e855
GH
662 if (tls_port) {
663 x509_dir = qemu_opt_get(opts, "x509-dir");
fe8e8327 664 if (!x509_dir) {
c448e855
GH
665 x509_dir = ".";
666 }
c448e855
GH
667
668 str = qemu_opt_get(opts, "x509-key-file");
669 if (str) {
7267c094 670 x509_key_file = g_strdup(str);
c448e855 671 } else {
6735aa99
CF
672 x509_key_file = g_strdup_printf("%s/%s", x509_dir,
673 X509_SERVER_KEY_FILE);
c448e855
GH
674 }
675
676 str = qemu_opt_get(opts, "x509-cert-file");
677 if (str) {
7267c094 678 x509_cert_file = g_strdup(str);
c448e855 679 } else {
6735aa99
CF
680 x509_cert_file = g_strdup_printf("%s/%s", x509_dir,
681 X509_SERVER_CERT_FILE);
c448e855
GH
682 }
683
684 str = qemu_opt_get(opts, "x509-cacert-file");
685 if (str) {
7267c094 686 x509_cacert_file = g_strdup(str);
c448e855 687 } else {
6735aa99
CF
688 x509_cacert_file = g_strdup_printf("%s/%s", x509_dir,
689 X509_CA_CERT_FILE);
c448e855
GH
690 }
691
692 x509_key_password = qemu_opt_get(opts, "x509-key-password");
9995c0b7 693 x509_dh_file = qemu_opt_get(opts, "x509-dh-key-file");
c448e855
GH
694 tls_ciphers = qemu_opt_get(opts, "tls-ciphers");
695 }
696
333b0eeb
GH
697 addr = qemu_opt_get(opts, "addr");
698 addr_flags = 0;
699 if (qemu_opt_get_bool(opts, "ipv4", 0)) {
700 addr_flags |= SPICE_ADDR_FLAG_IPV4_ONLY;
701 } else if (qemu_opt_get_bool(opts, "ipv6", 0)) {
702 addr_flags |= SPICE_ADDR_FLAG_IPV6_ONLY;
fe4831b1
MAL
703#ifdef SPICE_ADDR_FLAG_UNIX_ONLY
704 } else if (qemu_opt_get_bool(opts, "unix", 0)) {
705 addr_flags |= SPICE_ADDR_FLAG_UNIX_ONLY;
706#endif
333b0eeb
GH
707 }
708
29b0040b 709 spice_server = spice_server_new();
333b0eeb 710 spice_server_set_addr(spice_server, addr ? addr : "", addr_flags);
c448e855
GH
711 if (port) {
712 spice_server_set_port(spice_server, port);
713 }
714 if (tls_port) {
715 spice_server_set_tls(spice_server, tls_port,
716 x509_cacert_file,
717 x509_cert_file,
718 x509_key_file,
719 x509_key_password,
720 x509_dh_file,
721 tls_ciphers);
722 }
29b0040b 723 if (password) {
07d49a53 724 qemu_spice_set_passwd(password, false, false);
29b0040b 725 }
48b3ed0a 726 if (qemu_opt_get_bool(opts, "sasl", 0)) {
48b3ed0a
MAL
727 if (spice_server_set_sasl_appname(spice_server, "qemu") == -1 ||
728 spice_server_set_sasl(spice_server, 1) == -1) {
339a475f 729 error_report("spice: failed to enable sasl");
48b3ed0a
MAL
730 exit(1);
731 }
b1ea7b79 732 auth = "sasl";
48b3ed0a 733 }
29b0040b 734 if (qemu_opt_get_bool(opts, "disable-ticketing", 0)) {
6f8c63fb 735 auth = "none";
29b0040b
GH
736 spice_server_set_noauth(spice_server);
737 }
738
d4970b07
HG
739 if (qemu_opt_get_bool(opts, "disable-copy-paste", 0)) {
740 spice_server_set_agent_copypaste(spice_server, false);
741 }
d4970b07 742
5ad24e5f
HG
743 if (qemu_opt_get_bool(opts, "disable-agent-file-xfer", 0)) {
744#if SPICE_SERVER_VERSION >= 0x000c04
745 spice_server_set_agent_file_xfer(spice_server, false);
746#else
747 error_report("this qemu build does not support the "
748 "\"disable-agent-file-xfer\" option");
749 exit(1);
750#endif
751 }
752
9f04e09e
YH
753 compression = SPICE_IMAGE_COMPRESS_AUTO_GLZ;
754 str = qemu_opt_get(opts, "image-compression");
755 if (str) {
756 compression = parse_compression(str);
757 }
758 spice_server_set_image_compression(spice_server, compression);
759
760 wan_compr = SPICE_WAN_COMPRESSION_AUTO;
761 str = qemu_opt_get(opts, "jpeg-wan-compression");
762 if (str) {
763 wan_compr = parse_wan_compression(str);
764 }
765 spice_server_set_jpeg_compression(spice_server, wan_compr);
766
767 wan_compr = SPICE_WAN_COMPRESSION_AUTO;
768 str = qemu_opt_get(opts, "zlib-glz-wan-compression");
769 if (str) {
770 wan_compr = parse_wan_compression(str);
771 }
772 spice_server_set_zlib_glz_compression(spice_server, wan_compr);
29b0040b 773
84a23f25
GH
774 str = qemu_opt_get(opts, "streaming-video");
775 if (str) {
f61d6960 776 int streaming_video = parse_stream_video(str);
84a23f25 777 spice_server_set_streaming_video(spice_server, streaming_video);
f1d3e586
GH
778 } else {
779 spice_server_set_streaming_video(spice_server, SPICE_STREAM_VIDEO_OFF);
84a23f25
GH
780 }
781
782 spice_server_set_agent_mouse
783 (spice_server, qemu_opt_get_bool(opts, "agent-mouse", 1));
784 spice_server_set_playback_compression
785 (spice_server, qemu_opt_get_bool(opts, "playback-compression", 1));
786
71df1d83 787 qemu_opt_foreach(opts, add_channel, &tls_port, NULL);
17b6dea0 788
d0638b18
MAL
789 spice_server_set_name(spice_server, qemu_name);
790 spice_server_set_uuid(spice_server, qemu_uuid);
d0638b18 791
8c957053
YH
792 seamless_migration = qemu_opt_get_bool(opts, "seamless-migration", 0);
793 spice_server_set_seamless_migration(spice_server, seamless_migration);
fe8e8327 794 if (spice_server_init(spice_server, &core_interface) != 0) {
339a475f 795 error_report("failed to initialize spice server");
fba810f1
GH
796 exit(1);
797 };
29b0040b 798 using_spice = 1;
864401c2 799
e866e239
GH
800 migration_state.notify = migration_state_notifier;
801 add_migration_state_change_notifier(&migration_state);
3b5704b2
MA
802 spice_migrate.base.sif = &migrate_interface.base;
803 qemu_spice_add_interface(&spice_migrate.base);
e866e239 804
864401c2 805 qemu_spice_input_init();
3e313753 806 qemu_spice_audio_init();
c448e855 807
bfb82a28 808 qemu_add_vm_change_state_handler(vm_change_state_handler, NULL);
641381c1 809 qemu_spice_display_stop();
f5bb039c 810
7267c094
AL
811 g_free(x509_key_file);
812 g_free(x509_cert_file);
813 g_free(x509_cacert_file);
afd0b409
MAL
814
815#if SPICE_SERVER_VERSION >= 0x000c02
816 qemu_spice_register_ports();
817#endif
29b0040b
GH
818}
819
820int qemu_spice_add_interface(SpiceBaseInstance *sin)
821{
a19cbfb3
GH
822 if (!spice_server) {
823 if (QTAILQ_FIRST(&qemu_spice_opts.head) != NULL) {
339a475f 824 error_report("Oops: spice configured but not active");
a19cbfb3
GH
825 exit(1);
826 }
827 /*
828 * Create a spice server instance.
829 * It does *not* listen on the network.
830 * It handles QXL local rendering only.
831 *
832 * With a command line like '-vnc :0 -vga qxl' you'll end up here.
833 */
834 spice_server = spice_server_new();
764eb39d 835 spice_server_set_sasl_appname(spice_server, "qemu");
a19cbfb3 836 spice_server_init(spice_server, &core_interface);
bfb82a28 837 qemu_add_vm_change_state_handler(vm_change_state_handler, NULL);
a19cbfb3 838 }
71d388d4 839
9fa03286
GH
840 return spice_server_add_interface(spice_server, sin);
841}
842
843static GSList *spice_consoles;
9fa03286
GH
844
845bool qemu_spice_have_display_interface(QemuConsole *con)
846{
847 if (g_slist_find(spice_consoles, con)) {
848 return true;
58ae52a8 849 }
9fa03286
GH
850 return false;
851}
58ae52a8 852
9fa03286
GH
853int qemu_spice_add_display_interface(QXLInstance *qxlin, QemuConsole *con)
854{
855 if (g_slist_find(spice_consoles, con)) {
856 return -1;
857 }
cd56cc6b 858 qxlin->id = qemu_console_get_index(con);
9fa03286
GH
859 spice_consoles = g_slist_append(spice_consoles, con);
860 return qemu_spice_add_interface(&qxlin->base);
29b0040b
GH
861}
862
7572150c
GH
863static 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
882int qemu_spice_set_passwd(const char *passwd,
883 bool fail_if_conn, bool disconnect_if_conn)
884{
b1ea7b79
GH
885 if (strcmp(auth, "spice") != 0) {
886 return -1;
887 }
888
fd3bea3f
MA
889 g_free(auth_passwd);
890 auth_passwd = g_strdup(passwd);
7572150c
GH
891 return qemu_spice_set_ticket(fail_if_conn, disconnect_if_conn);
892}
893
894int qemu_spice_set_pw_expire(time_t expires)
895{
896 auth_expires = expires;
897 return qemu_spice_set_ticket(false, false);
898}
899
f1f5f407
DB
900int qemu_spice_display_add_client(int csock, int skipauth, int tls)
901{
f1f5f407
DB
902 if (tls) {
903 return spice_server_add_ssl_client(spice_server, csock, skipauth);
904 } else {
905 return spice_server_add_client(spice_server, csock, skipauth);
906 }
f1f5f407
DB
907}
908
7cc6a25f
GH
909void qemu_spice_display_start(void)
910{
911 spice_display_is_running = true;
b50f3e42 912 spice_server_vm_start(spice_server);
7cc6a25f
GH
913}
914
915void qemu_spice_display_stop(void)
916{
b50f3e42 917 spice_server_vm_stop(spice_server);
7cc6a25f
GH
918 spice_display_is_running = false;
919}
920
921int qemu_spice_display_is_running(SimpleSpiceDisplay *ssd)
922{
923 return spice_display_is_running;
924}
925
29b0040b
GH
926static void spice_register_config(void)
927{
928 qemu_add_opts(&qemu_spice_opts);
929}
930machine_init(spice_register_config);