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