]> git.proxmox.com Git - mirror_qemu.git/blame - ui/spice-core.c
spice: remove unused watch list
[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"
7b1b5d19
PB
33#include "qapi/qmp/qbool.h"
34#include "qapi/qmp/qstring.h"
35#include "qapi/qmp/qjson.h"
1de7afc9 36#include "qemu/notify.h"
c4b63b7c 37#include "migration/misc.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;
29b0040b 95};
29b0040b
GH
96
97static void watch_read(void *opaque)
98{
99 SpiceWatch *watch = opaque;
100 watch->func(watch->fd, SPICE_WATCH_EVENT_READ, watch->opaque);
101}
102
103static void watch_write(void *opaque)
104{
105 SpiceWatch *watch = opaque;
106 watch->func(watch->fd, SPICE_WATCH_EVENT_WRITE, watch->opaque);
107}
108
109static 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) {
3d6d306c 119 on_write = watch_write;
29b0040b
GH
120 }
121 qemu_set_fd_handler(watch->fd, on_read, on_write, watch);
122}
123
124static SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque)
125{
126 SpiceWatch *watch;
127
7267c094 128 watch = g_malloc0(sizeof(*watch));
29b0040b
GH
129 watch->fd = fd;
130 watch->func = func;
131 watch->opaque = opaque;
29b0040b
GH
132
133 watch_update_mask(watch, event_mask);
134 return watch;
135}
136
137static void watch_remove(SpiceWatch *watch)
138{
08cc67f3 139 qemu_set_fd_handler(watch->fd, NULL, NULL, NULL);
7267c094 140 g_free(watch);
29b0040b
GH
141}
142
cb42a870
GH
143typedef struct ChannelList ChannelList;
144struct ChannelList {
145 SpiceChannelEventInfo *info;
146 QTAILQ_ENTRY(ChannelList) link;
147};
148static QTAILQ_HEAD(, ChannelList) channel_list = QTAILQ_HEAD_INITIALIZER(channel_list);
149
150static void channel_list_add(SpiceChannelEventInfo *info)
151{
152 ChannelList *item;
153
7267c094 154 item = g_malloc0(sizeof(*item));
cb42a870
GH
155 item->info = info;
156 QTAILQ_INSERT_TAIL(&channel_list, item, link);
157}
158
159static void channel_list_del(SpiceChannelEventInfo *info)
160{
161 ChannelList *item;
162
163 QTAILQ_FOREACH(item, &channel_list, link) {
164 if (item->info != info) {
165 continue;
166 }
167 QTAILQ_REMOVE(&channel_list, item, link);
7267c094 168 g_free(item);
cb42a870
GH
169 return;
170 }
171}
172
7cfadb6b 173static void add_addr_info(SpiceBasicInfo *info, struct sockaddr *addr, int len)
6f8c63fb
GH
174{
175 char host[NI_MAXHOST], port[NI_MAXSERV];
6f8c63fb
GH
176
177 getnameinfo(addr, len, host, sizeof(host), port, sizeof(port),
178 NI_NUMERICHOST | NI_NUMERICSERV);
6f8c63fb 179
7cfadb6b
WX
180 info->host = g_strdup(host);
181 info->port = g_strdup(port);
182 info->family = inet_netfamily(addr->sa_family);
6f8c63fb
GH
183}
184
7cfadb6b 185static void add_channel_info(SpiceChannel *sc, SpiceChannelEventInfo *info)
6f8c63fb
GH
186{
187 int tls = info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS;
188
7cfadb6b
WX
189 sc->connection_id = info->connection_id;
190 sc->channel_type = info->type;
191 sc->channel_id = info->id;
192 sc->tls = !!tls;
6f8c63fb
GH
193}
194
195static void channel_event(int event, SpiceChannelEventInfo *info)
196{
7cfadb6b
WX
197 SpiceServerInfo *server = g_malloc0(sizeof(*server));
198 SpiceChannel *client = g_malloc0(sizeof(*client));
6f8c63fb 199
22b626e2
GH
200 /*
201 * Spice server might have called us from spice worker thread
202 * context (happens on display channel disconnects). Spice should
203 * not do that. It isn't that easy to fix it in spice and even
204 * when it is fixed we still should cover the already released
205 * spice versions. So detect that we've been called from another
206 * thread and grab the iothread lock if so before calling qemu
207 * functions.
208 */
f9ab6091 209 bool need_lock = !qemu_thread_is_self(&me);
22b626e2
GH
210 if (need_lock) {
211 qemu_mutex_lock_iothread();
212 }
213
faa98223 214 if (info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT) {
ddf21908
EB
215 add_addr_info(qapi_SpiceChannel_base(client),
216 (struct sockaddr *)&info->paddr_ext,
faa98223 217 info->plen_ext);
ddf21908
EB
218 add_addr_info(qapi_SpiceServerInfo_base(server),
219 (struct sockaddr *)&info->laddr_ext,
faa98223
YH
220 info->llen_ext);
221 } else {
339a475f
CF
222 error_report("spice: %s, extended address is expected",
223 __func__);
faa98223 224 }
6f8c63fb 225
7cfadb6b
WX
226 switch (event) {
227 case SPICE_CHANNEL_EVENT_CONNECTED:
ddf21908
EB
228 qapi_event_send_spice_connected(qapi_SpiceServerInfo_base(server),
229 qapi_SpiceChannel_base(client),
230 &error_abort);
7cfadb6b
WX
231 break;
232 case SPICE_CHANNEL_EVENT_INITIALIZED:
233 if (auth) {
234 server->has_auth = true;
235 server->auth = g_strdup(auth);
236 }
6f8c63fb 237 add_channel_info(client, info);
cb42a870 238 channel_list_add(info);
7cfadb6b
WX
239 qapi_event_send_spice_initialized(server, client, &error_abort);
240 break;
241 case SPICE_CHANNEL_EVENT_DISCONNECTED:
cb42a870 242 channel_list_del(info);
ddf21908
EB
243 qapi_event_send_spice_disconnected(qapi_SpiceServerInfo_base(server),
244 qapi_SpiceChannel_base(client),
245 &error_abort);
7cfadb6b
WX
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));
381
26defe81
MAL
382 paddr = (struct sockaddr *)&item->info->paddr_ext;
383 plen = item->info->plen_ext;
faa98223 384 getnameinfo(paddr, plen,
d1f29646
LC
385 host, sizeof(host), port, sizeof(port),
386 NI_NUMERICHOST | NI_NUMERICSERV);
ddf21908
EB
387 chan->value->host = g_strdup(host);
388 chan->value->port = g_strdup(port);
389 chan->value->family = inet_netfamily(paddr->sa_family);
d1f29646
LC
390
391 chan->value->connection_id = item->info->connection_id;
392 chan->value->channel_type = item->info->type;
393 chan->value->channel_id = item->info->id;
394 chan->value->tls = item->info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS;
395
396 /* XXX: waiting for the qapi to support GSList */
397 if (!cur_item) {
398 head = cur_item = chan;
399 } else {
400 cur_item->next = chan;
401 cur_item = chan;
402 }
cb42a870
GH
403 }
404
d1f29646 405 return head;
cb42a870
GH
406}
407
4d454574
PB
408static QemuOptsList qemu_spice_opts = {
409 .name = "spice",
410 .head = QTAILQ_HEAD_INITIALIZER(qemu_spice_opts.head),
411 .desc = {
412 {
413 .name = "port",
414 .type = QEMU_OPT_NUMBER,
415 },{
416 .name = "tls-port",
417 .type = QEMU_OPT_NUMBER,
418 },{
419 .name = "addr",
420 .type = QEMU_OPT_STRING,
421 },{
422 .name = "ipv4",
423 .type = QEMU_OPT_BOOL,
424 },{
425 .name = "ipv6",
426 .type = QEMU_OPT_BOOL,
fe4831b1
MAL
427#ifdef SPICE_ADDR_FLAG_UNIX_ONLY
428 },{
429 .name = "unix",
430 .type = QEMU_OPT_BOOL,
431#endif
4d454574
PB
432 },{
433 .name = "password",
434 .type = QEMU_OPT_STRING,
435 },{
436 .name = "disable-ticketing",
437 .type = QEMU_OPT_BOOL,
438 },{
439 .name = "disable-copy-paste",
440 .type = QEMU_OPT_BOOL,
5ad24e5f
HG
441 },{
442 .name = "disable-agent-file-xfer",
443 .type = QEMU_OPT_BOOL,
4d454574
PB
444 },{
445 .name = "sasl",
446 .type = QEMU_OPT_BOOL,
447 },{
448 .name = "x509-dir",
449 .type = QEMU_OPT_STRING,
450 },{
451 .name = "x509-key-file",
452 .type = QEMU_OPT_STRING,
453 },{
454 .name = "x509-key-password",
455 .type = QEMU_OPT_STRING,
456 },{
457 .name = "x509-cert-file",
458 .type = QEMU_OPT_STRING,
459 },{
460 .name = "x509-cacert-file",
461 .type = QEMU_OPT_STRING,
462 },{
463 .name = "x509-dh-key-file",
464 .type = QEMU_OPT_STRING,
465 },{
466 .name = "tls-ciphers",
467 .type = QEMU_OPT_STRING,
468 },{
469 .name = "tls-channel",
470 .type = QEMU_OPT_STRING,
471 },{
472 .name = "plaintext-channel",
473 .type = QEMU_OPT_STRING,
474 },{
475 .name = "image-compression",
476 .type = QEMU_OPT_STRING,
477 },{
478 .name = "jpeg-wan-compression",
479 .type = QEMU_OPT_STRING,
480 },{
481 .name = "zlib-glz-wan-compression",
482 .type = QEMU_OPT_STRING,
483 },{
484 .name = "streaming-video",
485 .type = QEMU_OPT_STRING,
486 },{
487 .name = "agent-mouse",
488 .type = QEMU_OPT_BOOL,
489 },{
490 .name = "playback-compression",
491 .type = QEMU_OPT_BOOL,
474114b7 492 },{
4d454574
PB
493 .name = "seamless-migration",
494 .type = QEMU_OPT_BOOL,
8bf69b49
GH
495 },{
496 .name = "display",
497 .type = QEMU_OPT_STRING,
498 },{
499 .name = "head",
500 .type = QEMU_OPT_NUMBER,
474114b7
GH
501#ifdef HAVE_SPICE_GL
502 },{
503 .name = "gl",
504 .type = QEMU_OPT_BOOL,
7b525508
MAL
505 },{
506 .name = "rendernode",
507 .type = QEMU_OPT_STRING,
474114b7 508#endif
4d454574
PB
509 },
510 { /* end of list */ }
511 },
512};
513
d1f29646 514SpiceInfo *qmp_query_spice(Error **errp)
cb42a870
GH
515{
516 QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
cb42a870 517 int port, tls_port;
d1f29646
LC
518 const char *addr;
519 SpiceInfo *info;
6735aa99
CF
520 unsigned int major;
521 unsigned int minor;
522 unsigned int micro;
cb42a870 523
d1f29646
LC
524 info = g_malloc0(sizeof(*info));
525
3bb781f3 526 if (!spice_server || !opts) {
d1f29646
LC
527 info->enabled = false;
528 return info;
cb42a870
GH
529 }
530
d1f29646 531 info->enabled = true;
61c4efe2 532 info->migrated = spice_migration_completed;
d1f29646 533
cb42a870
GH
534 addr = qemu_opt_get(opts, "addr");
535 port = qemu_opt_get_number(opts, "port", 0);
536 tls_port = qemu_opt_get_number(opts, "tls-port", 0);
cb42a870 537
d1f29646
LC
538 info->has_auth = true;
539 info->auth = g_strdup(auth);
540
541 info->has_host = true;
4f60af9a 542 info->host = g_strdup(addr ? addr : "*");
d1f29646
LC
543
544 info->has_compiled_version = true;
6735aa99
CF
545 major = (SPICE_SERVER_VERSION & 0xff0000) >> 16;
546 minor = (SPICE_SERVER_VERSION & 0xff00) >> 8;
547 micro = SPICE_SERVER_VERSION & 0xff;
548 info->compiled_version = g_strdup_printf("%d.%d.%d", major, minor, micro);
d1f29646 549
cb42a870 550 if (port) {
d1f29646
LC
551 info->has_port = true;
552 info->port = port;
cb42a870
GH
553 }
554 if (tls_port) {
d1f29646
LC
555 info->has_tls_port = true;
556 info->tls_port = tls_port;
cb42a870
GH
557 }
558
4efee029
AL
559 info->mouse_mode = spice_server_is_server_mouse(spice_server) ?
560 SPICE_QUERY_MOUSE_MODE_SERVER :
561 SPICE_QUERY_MOUSE_MODE_CLIENT;
67be6726 562
d1f29646
LC
563 /* for compatibility with the original command */
564 info->has_channels = true;
565 info->channels = qmp_query_spice_channels();
566
567 return info;
cb42a870
GH
568}
569
9e8dd451 570static void migration_state_notifier(Notifier *notifier, void *data)
e866e239 571{
7073693b 572 MigrationState *s = data;
e866e239 573
a76a2f72
GH
574 if (!spice_have_target_host) {
575 return;
576 }
577
02edd2e7 578 if (migration_in_setup(s)) {
026f773f 579 spice_server_migrate_start(spice_server);
b82fc321
DDAG
580 } else if (migration_has_finished(s) ||
581 migration_in_postcopy_after_devices(s)) {
026f773f 582 spice_server_migrate_end(spice_server, true);
a76a2f72 583 spice_have_target_host = false;
026f773f
YH
584 } else if (migration_has_failed(s)) {
585 spice_server_migrate_end(spice_server, false);
a76a2f72 586 spice_have_target_host = false;
e866e239
GH
587 }
588}
589
590int qemu_spice_migrate_info(const char *hostname, int port, int tls_port,
3b5704b2 591 const char *subject)
e866e239 592{
edc5cb1a 593 int ret;
67be6726 594
026f773f
YH
595 ret = spice_server_migrate_connect(spice_server, hostname,
596 port, tls_port, subject);
a76a2f72 597 spice_have_target_host = true;
edc5cb1a 598 return ret;
e866e239
GH
599}
600
71df1d83
MA
601static int add_channel(void *opaque, const char *name, const char *value,
602 Error **errp)
17b6dea0
GH
603{
604 int security = 0;
605 int rc;
606
607 if (strcmp(name, "tls-channel") == 0) {
35c63329
CF
608 int *tls_port = opaque;
609 if (!*tls_port) {
610 error_report("spice: tried to setup tls-channel"
611 " without specifying a TLS port");
612 exit(1);
613 }
17b6dea0
GH
614 security = SPICE_CHANNEL_SECURITY_SSL;
615 }
616 if (strcmp(name, "plaintext-channel") == 0) {
617 security = SPICE_CHANNEL_SECURITY_NONE;
618 }
619 if (security == 0) {
620 return 0;
621 }
622 if (strcmp(value, "default") == 0) {
623 rc = spice_server_set_channel_security(spice_server, NULL, security);
624 } else {
625 rc = spice_server_set_channel_security(spice_server, value, security);
626 }
627 if (rc != 0) {
339a475f 628 error_report("spice: failed to set channel security for %s", value);
17b6dea0
GH
629 exit(1);
630 }
631 return 0;
632}
633
f5bb039c
YH
634static void vm_change_state_handler(void *opaque, int running,
635 RunState state)
636{
f5bb039c 637 if (running) {
71d388d4 638 qemu_spice_display_start();
f5bb039c 639 } else {
71d388d4 640 qemu_spice_display_stop();
f5bb039c 641 }
f5bb039c
YH
642}
643
29b0040b
GH
644void qemu_spice_init(void)
645{
646 QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
333b0eeb 647 const char *password, *str, *x509_dir, *addr,
c448e855
GH
648 *x509_key_password = NULL,
649 *x509_dh_file = NULL,
650 *tls_ciphers = NULL;
651 char *x509_key_file = NULL,
652 *x509_cert_file = NULL,
653 *x509_cacert_file = NULL;
6735aa99 654 int port, tls_port, addr_flags;
9f04e09e
YH
655 spice_image_compression_t compression;
656 spice_wan_compression_t wan_compr;
8c957053 657 bool seamless_migration;
29b0040b 658
f9ab6091 659 qemu_thread_get_self(&me);
22b626e2 660
ad1be899 661 if (!opts) {
29b0040b
GH
662 return;
663 }
664 port = qemu_opt_get_number(opts, "port", 0);
c448e855 665 tls_port = qemu_opt_get_number(opts, "tls-port", 0);
df9cb669 666 if (port < 0 || port > 65535) {
339a475f 667 error_report("spice port is out of range");
df9cb669
GH
668 exit(1);
669 }
670 if (tls_port < 0 || tls_port > 65535) {
339a475f 671 error_report("spice tls-port is out of range");
df9cb669 672 exit(1);
29b0040b
GH
673 }
674 password = qemu_opt_get(opts, "password");
675
c448e855
GH
676 if (tls_port) {
677 x509_dir = qemu_opt_get(opts, "x509-dir");
fe8e8327 678 if (!x509_dir) {
c448e855
GH
679 x509_dir = ".";
680 }
c448e855
GH
681
682 str = qemu_opt_get(opts, "x509-key-file");
683 if (str) {
7267c094 684 x509_key_file = g_strdup(str);
c448e855 685 } else {
6735aa99
CF
686 x509_key_file = g_strdup_printf("%s/%s", x509_dir,
687 X509_SERVER_KEY_FILE);
c448e855
GH
688 }
689
690 str = qemu_opt_get(opts, "x509-cert-file");
691 if (str) {
7267c094 692 x509_cert_file = g_strdup(str);
c448e855 693 } else {
6735aa99
CF
694 x509_cert_file = g_strdup_printf("%s/%s", x509_dir,
695 X509_SERVER_CERT_FILE);
c448e855
GH
696 }
697
698 str = qemu_opt_get(opts, "x509-cacert-file");
699 if (str) {
7267c094 700 x509_cacert_file = g_strdup(str);
c448e855 701 } else {
6735aa99
CF
702 x509_cacert_file = g_strdup_printf("%s/%s", x509_dir,
703 X509_CA_CERT_FILE);
c448e855
GH
704 }
705
706 x509_key_password = qemu_opt_get(opts, "x509-key-password");
9995c0b7 707 x509_dh_file = qemu_opt_get(opts, "x509-dh-key-file");
c448e855
GH
708 tls_ciphers = qemu_opt_get(opts, "tls-ciphers");
709 }
710
333b0eeb
GH
711 addr = qemu_opt_get(opts, "addr");
712 addr_flags = 0;
713 if (qemu_opt_get_bool(opts, "ipv4", 0)) {
714 addr_flags |= SPICE_ADDR_FLAG_IPV4_ONLY;
715 } else if (qemu_opt_get_bool(opts, "ipv6", 0)) {
716 addr_flags |= SPICE_ADDR_FLAG_IPV6_ONLY;
fe4831b1
MAL
717#ifdef SPICE_ADDR_FLAG_UNIX_ONLY
718 } else if (qemu_opt_get_bool(opts, "unix", 0)) {
719 addr_flags |= SPICE_ADDR_FLAG_UNIX_ONLY;
720#endif
333b0eeb
GH
721 }
722
29b0040b 723 spice_server = spice_server_new();
333b0eeb 724 spice_server_set_addr(spice_server, addr ? addr : "", addr_flags);
c448e855
GH
725 if (port) {
726 spice_server_set_port(spice_server, port);
727 }
728 if (tls_port) {
729 spice_server_set_tls(spice_server, tls_port,
730 x509_cacert_file,
731 x509_cert_file,
732 x509_key_file,
733 x509_key_password,
734 x509_dh_file,
735 tls_ciphers);
736 }
29b0040b 737 if (password) {
07d49a53 738 qemu_spice_set_passwd(password, false, false);
29b0040b 739 }
48b3ed0a 740 if (qemu_opt_get_bool(opts, "sasl", 0)) {
06bb8814 741 if (spice_server_set_sasl(spice_server, 1) == -1) {
339a475f 742 error_report("spice: failed to enable sasl");
48b3ed0a
MAL
743 exit(1);
744 }
b1ea7b79 745 auth = "sasl";
48b3ed0a 746 }
29b0040b 747 if (qemu_opt_get_bool(opts, "disable-ticketing", 0)) {
6f8c63fb 748 auth = "none";
29b0040b
GH
749 spice_server_set_noauth(spice_server);
750 }
751
d4970b07
HG
752 if (qemu_opt_get_bool(opts, "disable-copy-paste", 0)) {
753 spice_server_set_agent_copypaste(spice_server, false);
754 }
d4970b07 755
5ad24e5f
HG
756 if (qemu_opt_get_bool(opts, "disable-agent-file-xfer", 0)) {
757#if SPICE_SERVER_VERSION >= 0x000c04
758 spice_server_set_agent_file_xfer(spice_server, false);
759#else
760 error_report("this qemu build does not support the "
761 "\"disable-agent-file-xfer\" option");
762 exit(1);
763#endif
764 }
765
9f04e09e
YH
766 compression = SPICE_IMAGE_COMPRESS_AUTO_GLZ;
767 str = qemu_opt_get(opts, "image-compression");
768 if (str) {
769 compression = parse_compression(str);
770 }
771 spice_server_set_image_compression(spice_server, compression);
772
773 wan_compr = SPICE_WAN_COMPRESSION_AUTO;
774 str = qemu_opt_get(opts, "jpeg-wan-compression");
775 if (str) {
776 wan_compr = parse_wan_compression(str);
777 }
778 spice_server_set_jpeg_compression(spice_server, wan_compr);
779
780 wan_compr = SPICE_WAN_COMPRESSION_AUTO;
781 str = qemu_opt_get(opts, "zlib-glz-wan-compression");
782 if (str) {
783 wan_compr = parse_wan_compression(str);
784 }
785 spice_server_set_zlib_glz_compression(spice_server, wan_compr);
29b0040b 786
84a23f25
GH
787 str = qemu_opt_get(opts, "streaming-video");
788 if (str) {
f61d6960 789 int streaming_video = parse_stream_video(str);
84a23f25 790 spice_server_set_streaming_video(spice_server, streaming_video);
f1d3e586
GH
791 } else {
792 spice_server_set_streaming_video(spice_server, SPICE_STREAM_VIDEO_OFF);
84a23f25
GH
793 }
794
795 spice_server_set_agent_mouse
796 (spice_server, qemu_opt_get_bool(opts, "agent-mouse", 1));
797 spice_server_set_playback_compression
798 (spice_server, qemu_opt_get_bool(opts, "playback-compression", 1));
799
71df1d83 800 qemu_opt_foreach(opts, add_channel, &tls_port, NULL);
17b6dea0 801
d0638b18 802 spice_server_set_name(spice_server, qemu_name);
9c5ce8db 803 spice_server_set_uuid(spice_server, (unsigned char *)&qemu_uuid);
d0638b18 804
8c957053
YH
805 seamless_migration = qemu_opt_get_bool(opts, "seamless-migration", 0);
806 spice_server_set_seamless_migration(spice_server, seamless_migration);
06bb8814 807 spice_server_set_sasl_appname(spice_server, "qemu");
fe8e8327 808 if (spice_server_init(spice_server, &core_interface) != 0) {
339a475f 809 error_report("failed to initialize spice server");
fba810f1
GH
810 exit(1);
811 };
29b0040b 812 using_spice = 1;
864401c2 813
e866e239
GH
814 migration_state.notify = migration_state_notifier;
815 add_migration_state_change_notifier(&migration_state);
3b5704b2
MA
816 spice_migrate.base.sif = &migrate_interface.base;
817 qemu_spice_add_interface(&spice_migrate.base);
e866e239 818
864401c2 819 qemu_spice_input_init();
3e313753 820 qemu_spice_audio_init();
c448e855 821
bfb82a28 822 qemu_add_vm_change_state_handler(vm_change_state_handler, NULL);
641381c1 823 qemu_spice_display_stop();
f5bb039c 824
7267c094
AL
825 g_free(x509_key_file);
826 g_free(x509_cert_file);
827 g_free(x509_cacert_file);
afd0b409
MAL
828
829#if SPICE_SERVER_VERSION >= 0x000c02
830 qemu_spice_register_ports();
831#endif
474114b7
GH
832
833#ifdef HAVE_SPICE_GL
834 if (qemu_opt_get_bool(opts, "gl", 0)) {
569a93cb
CF
835 if ((port != 0) || (tls_port != 0)) {
836 error_report("SPICE GL support is local-only for now and "
837 "incompatible with -spice port/tls-port");
838 exit(1);
839 }
7b525508 840 if (egl_rendernode_init(qemu_opt_get(opts, "rendernode")) != 0) {
daafc661
CR
841 error_report("Failed to initialize EGL render node for SPICE GL");
842 exit(1);
474114b7 843 }
daafc661 844 display_opengl = 1;
fe5c44f9 845 spice_opengl = 1;
474114b7
GH
846 }
847#endif
29b0040b
GH
848}
849
850int qemu_spice_add_interface(SpiceBaseInstance *sin)
851{
a19cbfb3
GH
852 if (!spice_server) {
853 if (QTAILQ_FIRST(&qemu_spice_opts.head) != NULL) {
339a475f 854 error_report("Oops: spice configured but not active");
a19cbfb3
GH
855 exit(1);
856 }
857 /*
858 * Create a spice server instance.
859 * It does *not* listen on the network.
860 * It handles QXL local rendering only.
861 *
862 * With a command line like '-vnc :0 -vga qxl' you'll end up here.
863 */
864 spice_server = spice_server_new();
764eb39d 865 spice_server_set_sasl_appname(spice_server, "qemu");
a19cbfb3 866 spice_server_init(spice_server, &core_interface);
bfb82a28 867 qemu_add_vm_change_state_handler(vm_change_state_handler, NULL);
a19cbfb3 868 }
71d388d4 869
9fa03286
GH
870 return spice_server_add_interface(spice_server, sin);
871}
872
873static GSList *spice_consoles;
9fa03286
GH
874
875bool qemu_spice_have_display_interface(QemuConsole *con)
876{
877 if (g_slist_find(spice_consoles, con)) {
878 return true;
58ae52a8 879 }
9fa03286
GH
880 return false;
881}
58ae52a8 882
9fa03286
GH
883int qemu_spice_add_display_interface(QXLInstance *qxlin, QemuConsole *con)
884{
885 if (g_slist_find(spice_consoles, con)) {
886 return -1;
887 }
cd56cc6b 888 qxlin->id = qemu_console_get_index(con);
9fa03286
GH
889 spice_consoles = g_slist_append(spice_consoles, con);
890 return qemu_spice_add_interface(&qxlin->base);
29b0040b
GH
891}
892
7572150c
GH
893static int qemu_spice_set_ticket(bool fail_if_conn, bool disconnect_if_conn)
894{
895 time_t lifetime, now = time(NULL);
896 char *passwd;
897
898 if (now < auth_expires) {
899 passwd = auth_passwd;
900 lifetime = (auth_expires - now);
901 if (lifetime > INT_MAX) {
902 lifetime = INT_MAX;
903 }
904 } else {
905 passwd = NULL;
906 lifetime = 1;
907 }
908 return spice_server_set_ticket(spice_server, passwd, lifetime,
909 fail_if_conn, disconnect_if_conn);
910}
911
912int qemu_spice_set_passwd(const char *passwd,
913 bool fail_if_conn, bool disconnect_if_conn)
914{
b1ea7b79
GH
915 if (strcmp(auth, "spice") != 0) {
916 return -1;
917 }
918
fd3bea3f
MA
919 g_free(auth_passwd);
920 auth_passwd = g_strdup(passwd);
7572150c
GH
921 return qemu_spice_set_ticket(fail_if_conn, disconnect_if_conn);
922}
923
924int qemu_spice_set_pw_expire(time_t expires)
925{
926 auth_expires = expires;
927 return qemu_spice_set_ticket(false, false);
928}
929
f1f5f407
DB
930int qemu_spice_display_add_client(int csock, int skipauth, int tls)
931{
f1f5f407
DB
932 if (tls) {
933 return spice_server_add_ssl_client(spice_server, csock, skipauth);
934 } else {
935 return spice_server_add_client(spice_server, csock, skipauth);
936 }
f1f5f407
DB
937}
938
7cc6a25f
GH
939void qemu_spice_display_start(void)
940{
941 spice_display_is_running = true;
b50f3e42 942 spice_server_vm_start(spice_server);
7cc6a25f
GH
943}
944
945void qemu_spice_display_stop(void)
946{
b50f3e42 947 spice_server_vm_stop(spice_server);
7cc6a25f
GH
948 spice_display_is_running = false;
949}
950
951int qemu_spice_display_is_running(SimpleSpiceDisplay *ssd)
952{
953 return spice_display_is_running;
954}
955
29b0040b
GH
956static void spice_register_config(void)
957{
958 qemu_add_opts(&qemu_spice_opts);
959}
34294e2f 960opts_init(spice_register_config);