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