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