]> git.proxmox.com Git - mirror_qemu.git/blame - ui/spice-core.c
Update version for 1.6.0-rc0
[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>
9c17d615 22#include "sysemu/sysemu.h"
6f8c63fb 23
29b0040b 24#include "qemu-common.h"
28ecbaee 25#include "ui/qemu-spice.h"
1de7afc9
PB
26#include "qemu/thread.h"
27#include "qemu/timer.h"
28#include "qemu/queue.h"
c448e855 29#include "qemu-x509.h"
1de7afc9 30#include "qemu/sockets.h"
d1f29646 31#include "qmp-commands.h"
7b1b5d19
PB
32#include "qapi/qmp/qint.h"
33#include "qapi/qmp/qbool.h"
34#include "qapi/qmp/qstring.h"
35#include "qapi/qmp/qjson.h"
1de7afc9 36#include "qemu/notify.h"
caf71f86 37#include "migration/migration.h"
83c9089e 38#include "monitor/monitor.h"
e866e239 39#include "hw/hw.h"
28ecbaee 40#include "ui/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 225
faa98223
YH
226 if (info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT) {
227 add_addr_info(client, (struct sockaddr *)&info->paddr_ext,
228 info->plen_ext);
229 add_addr_info(server, (struct sockaddr *)&info->laddr_ext,
230 info->llen_ext);
231 } else {
339a475f
CF
232 error_report("spice: %s, extended address is expected",
233 __func__);
faa98223 234 }
6f8c63fb
GH
235
236 if (event == SPICE_CHANNEL_EVENT_INITIALIZED) {
237 qdict_put(server, "auth", qstring_from_str(auth));
238 add_channel_info(client, info);
cb42a870
GH
239 channel_list_add(info);
240 }
241 if (event == SPICE_CHANNEL_EVENT_DISCONNECTED) {
242 channel_list_del(info);
6f8c63fb
GH
243 }
244
245 data = qobject_from_jsonf("{ 'client': %p, 'server': %p }",
246 QOBJECT(client), QOBJECT(server));
247 monitor_protocol_event(qevent[event], data);
248 qobject_decref(data);
22b626e2
GH
249
250 if (need_lock) {
251 qemu_mutex_unlock_iothread();
252 }
6f8c63fb
GH
253}
254
29b0040b
GH
255static SpiceCoreInterface core_interface = {
256 .base.type = SPICE_INTERFACE_CORE,
257 .base.description = "qemu core services",
258 .base.major_version = SPICE_INTERFACE_CORE_MAJOR,
259 .base.minor_version = SPICE_INTERFACE_CORE_MINOR,
260
261 .timer_add = timer_add,
262 .timer_start = timer_start,
263 .timer_cancel = timer_cancel,
264 .timer_remove = timer_remove,
265
266 .watch_add = watch_add,
267 .watch_update_mask = watch_update_mask,
268 .watch_remove = watch_remove,
6f8c63fb 269
6f8c63fb 270 .channel_event = channel_event,
29b0040b
GH
271};
272
026f773f
YH
273typedef struct SpiceMigration {
274 SpiceMigrateInstance sin;
275 struct {
276 MonitorCompletion *cb;
277 void *opaque;
278 } connect_complete;
279} SpiceMigration;
280
281static void migrate_connect_complete_cb(SpiceMigrateInstance *sin);
2fdd16e2 282static void migrate_end_complete_cb(SpiceMigrateInstance *sin);
026f773f
YH
283
284static const SpiceMigrateInterface migrate_interface = {
285 .base.type = SPICE_INTERFACE_MIGRATION,
286 .base.description = "migration",
287 .base.major_version = SPICE_INTERFACE_MIGRATION_MAJOR,
288 .base.minor_version = SPICE_INTERFACE_MIGRATION_MINOR,
289 .migrate_connect_complete = migrate_connect_complete_cb,
2fdd16e2 290 .migrate_end_complete = migrate_end_complete_cb,
026f773f
YH
291};
292
293static SpiceMigration spice_migrate;
294
295static void migrate_connect_complete_cb(SpiceMigrateInstance *sin)
296{
297 SpiceMigration *sm = container_of(sin, SpiceMigration, sin);
298 if (sm->connect_complete.cb) {
299 sm->connect_complete.cb(sm->connect_complete.opaque, NULL);
300 }
301 sm->connect_complete.cb = NULL;
302}
2fdd16e2
YH
303
304static void migrate_end_complete_cb(SpiceMigrateInstance *sin)
305{
306 monitor_protocol_event(QEVENT_SPICE_MIGRATE_COMPLETED, NULL);
61c4efe2 307 spice_migration_completed = true;
2fdd16e2 308}
026f773f 309
9f04e09e
YH
310/* config string parsing */
311
312static int name2enum(const char *string, const char *table[], int entries)
313{
314 int i;
315
316 if (string) {
317 for (i = 0; i < entries; i++) {
318 if (!table[i]) {
319 continue;
320 }
321 if (strcmp(string, table[i]) != 0) {
322 continue;
323 }
324 return i;
325 }
326 }
327 return -1;
328}
329
330static int parse_name(const char *string, const char *optname,
331 const char *table[], int entries)
332{
333 int value = name2enum(string, table, entries);
334
335 if (value != -1) {
336 return value;
337 }
339a475f 338 error_report("spice: invalid %s: %s", optname, string);
9f04e09e
YH
339 exit(1);
340}
341
84a23f25
GH
342static const char *stream_video_names[] = {
343 [ SPICE_STREAM_VIDEO_OFF ] = "off",
344 [ SPICE_STREAM_VIDEO_ALL ] = "all",
345 [ SPICE_STREAM_VIDEO_FILTER ] = "filter",
346};
347#define parse_stream_video(_name) \
835cab85
CF
348 parse_name(_name, "stream video control", \
349 stream_video_names, ARRAY_SIZE(stream_video_names))
84a23f25 350
9f04e09e
YH
351static const char *compression_names[] = {
352 [ SPICE_IMAGE_COMPRESS_OFF ] = "off",
353 [ SPICE_IMAGE_COMPRESS_AUTO_GLZ ] = "auto_glz",
354 [ SPICE_IMAGE_COMPRESS_AUTO_LZ ] = "auto_lz",
355 [ SPICE_IMAGE_COMPRESS_QUIC ] = "quic",
356 [ SPICE_IMAGE_COMPRESS_GLZ ] = "glz",
357 [ SPICE_IMAGE_COMPRESS_LZ ] = "lz",
358};
359#define parse_compression(_name) \
360 parse_name(_name, "image compression", \
361 compression_names, ARRAY_SIZE(compression_names))
362
363static const char *wan_compression_names[] = {
364 [ SPICE_WAN_COMPRESSION_AUTO ] = "auto",
365 [ SPICE_WAN_COMPRESSION_NEVER ] = "never",
366 [ SPICE_WAN_COMPRESSION_ALWAYS ] = "always",
367};
368#define parse_wan_compression(_name) \
369 parse_name(_name, "wan compression", \
370 wan_compression_names, ARRAY_SIZE(wan_compression_names))
371
29b0040b
GH
372/* functions for the rest of qemu */
373
d1f29646 374static SpiceChannelList *qmp_query_spice_channels(void)
cb42a870 375{
d1f29646
LC
376 SpiceChannelList *cur_item = NULL, *head = NULL;
377 ChannelList *item;
cb42a870 378
d1f29646
LC
379 QTAILQ_FOREACH(item, &channel_list, link) {
380 SpiceChannelList *chan;
381 char host[NI_MAXHOST], port[NI_MAXSERV];
faa98223
YH
382 struct sockaddr *paddr;
383 socklen_t plen;
d1f29646
LC
384
385 chan = g_malloc0(sizeof(*chan));
386 chan->value = g_malloc0(sizeof(*chan->value));
387
faa98223
YH
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 {
faa98223
YH
392 paddr = &item->info->paddr;
393 plen = item->info->plen;
faa98223 394 }
faa98223
YH
395
396 getnameinfo(paddr, plen,
d1f29646
LC
397 host, sizeof(host), port, sizeof(port),
398 NI_NUMERICHOST | NI_NUMERICSERV);
399 chan->value->host = g_strdup(host);
400 chan->value->port = g_strdup(port);
faa98223 401 chan->value->family = g_strdup(inet_strfamily(paddr->sa_family));
d1f29646
LC
402
403 chan->value->connection_id = item->info->connection_id;
404 chan->value->channel_type = item->info->type;
405 chan->value->channel_id = item->info->id;
406 chan->value->tls = item->info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS;
407
408 /* XXX: waiting for the qapi to support GSList */
409 if (!cur_item) {
410 head = cur_item = chan;
411 } else {
412 cur_item->next = chan;
413 cur_item = chan;
414 }
cb42a870
GH
415 }
416
d1f29646 417 return head;
cb42a870
GH
418}
419
4d454574
PB
420static QemuOptsList qemu_spice_opts = {
421 .name = "spice",
422 .head = QTAILQ_HEAD_INITIALIZER(qemu_spice_opts.head),
423 .desc = {
424 {
425 .name = "port",
426 .type = QEMU_OPT_NUMBER,
427 },{
428 .name = "tls-port",
429 .type = QEMU_OPT_NUMBER,
430 },{
431 .name = "addr",
432 .type = QEMU_OPT_STRING,
433 },{
434 .name = "ipv4",
435 .type = QEMU_OPT_BOOL,
436 },{
437 .name = "ipv6",
438 .type = QEMU_OPT_BOOL,
439 },{
440 .name = "password",
441 .type = QEMU_OPT_STRING,
442 },{
443 .name = "disable-ticketing",
444 .type = QEMU_OPT_BOOL,
445 },{
446 .name = "disable-copy-paste",
447 .type = QEMU_OPT_BOOL,
5ad24e5f
HG
448 },{
449 .name = "disable-agent-file-xfer",
450 .type = QEMU_OPT_BOOL,
4d454574
PB
451 },{
452 .name = "sasl",
453 .type = QEMU_OPT_BOOL,
454 },{
455 .name = "x509-dir",
456 .type = QEMU_OPT_STRING,
457 },{
458 .name = "x509-key-file",
459 .type = QEMU_OPT_STRING,
460 },{
461 .name = "x509-key-password",
462 .type = QEMU_OPT_STRING,
463 },{
464 .name = "x509-cert-file",
465 .type = QEMU_OPT_STRING,
466 },{
467 .name = "x509-cacert-file",
468 .type = QEMU_OPT_STRING,
469 },{
470 .name = "x509-dh-key-file",
471 .type = QEMU_OPT_STRING,
472 },{
473 .name = "tls-ciphers",
474 .type = QEMU_OPT_STRING,
475 },{
476 .name = "tls-channel",
477 .type = QEMU_OPT_STRING,
478 },{
479 .name = "plaintext-channel",
480 .type = QEMU_OPT_STRING,
481 },{
482 .name = "image-compression",
483 .type = QEMU_OPT_STRING,
484 },{
485 .name = "jpeg-wan-compression",
486 .type = QEMU_OPT_STRING,
487 },{
488 .name = "zlib-glz-wan-compression",
489 .type = QEMU_OPT_STRING,
490 },{
491 .name = "streaming-video",
492 .type = QEMU_OPT_STRING,
493 },{
494 .name = "agent-mouse",
495 .type = QEMU_OPT_BOOL,
496 },{
497 .name = "playback-compression",
498 .type = QEMU_OPT_BOOL,
499 }, {
500 .name = "seamless-migration",
501 .type = QEMU_OPT_BOOL,
502 },
503 { /* end of list */ }
504 },
505};
506
d1f29646 507SpiceInfo *qmp_query_spice(Error **errp)
cb42a870
GH
508{
509 QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
cb42a870 510 int port, tls_port;
d1f29646
LC
511 const char *addr;
512 SpiceInfo *info;
8df0c7e8 513 char version_string[20]; /* 12 = |255.255.255\0| is the max */
cb42a870 514
d1f29646
LC
515 info = g_malloc0(sizeof(*info));
516
3bb781f3 517 if (!spice_server || !opts) {
d1f29646
LC
518 info->enabled = false;
519 return info;
cb42a870
GH
520 }
521
d1f29646 522 info->enabled = true;
61c4efe2 523 info->migrated = spice_migration_completed;
d1f29646 524
cb42a870
GH
525 addr = qemu_opt_get(opts, "addr");
526 port = qemu_opt_get_number(opts, "port", 0);
527 tls_port = qemu_opt_get_number(opts, "tls-port", 0);
cb42a870 528
d1f29646
LC
529 info->has_auth = true;
530 info->auth = g_strdup(auth);
531
532 info->has_host = true;
533 info->host = g_strdup(addr ? addr : "0.0.0.0");
534
535 info->has_compiled_version = true;
8df0c7e8
AL
536 snprintf(version_string, sizeof(version_string), "%d.%d.%d",
537 (SPICE_SERVER_VERSION & 0xff0000) >> 16,
538 (SPICE_SERVER_VERSION & 0xff00) >> 8,
539 SPICE_SERVER_VERSION & 0xff);
d1f29646
LC
540 info->compiled_version = g_strdup(version_string);
541
cb42a870 542 if (port) {
d1f29646
LC
543 info->has_port = true;
544 info->port = port;
cb42a870
GH
545 }
546 if (tls_port) {
d1f29646
LC
547 info->has_tls_port = true;
548 info->tls_port = tls_port;
cb42a870
GH
549 }
550
4efee029
AL
551 info->mouse_mode = spice_server_is_server_mouse(spice_server) ?
552 SPICE_QUERY_MOUSE_MODE_SERVER :
553 SPICE_QUERY_MOUSE_MODE_CLIENT;
67be6726 554
d1f29646
LC
555 /* for compatibility with the original command */
556 info->has_channels = true;
557 info->channels = qmp_query_spice_channels();
558
559 return info;
cb42a870
GH
560}
561
9e8dd451 562static void migration_state_notifier(Notifier *notifier, void *data)
e866e239 563{
7073693b 564 MigrationState *s = data;
e866e239 565
02edd2e7 566 if (migration_in_setup(s)) {
026f773f 567 spice_server_migrate_start(spice_server);
026f773f 568 } else if (migration_has_finished(s)) {
026f773f
YH
569 spice_server_migrate_end(spice_server, true);
570 } else if (migration_has_failed(s)) {
571 spice_server_migrate_end(spice_server, false);
e866e239
GH
572 }
573}
574
575int qemu_spice_migrate_info(const char *hostname, int port, int tls_port,
edc5cb1a
YH
576 const char *subject,
577 MonitorCompletion *cb, void *opaque)
e866e239 578{
edc5cb1a 579 int ret;
67be6726 580
026f773f
YH
581 spice_migrate.connect_complete.cb = cb;
582 spice_migrate.connect_complete.opaque = opaque;
583 ret = spice_server_migrate_connect(spice_server, hostname,
584 port, tls_port, subject);
edc5cb1a 585 return ret;
e866e239
GH
586}
587
17b6dea0
GH
588static int add_channel(const char *name, const char *value, void *opaque)
589{
590 int security = 0;
591 int rc;
592
593 if (strcmp(name, "tls-channel") == 0) {
35c63329
CF
594 int *tls_port = opaque;
595 if (!*tls_port) {
596 error_report("spice: tried to setup tls-channel"
597 " without specifying a TLS port");
598 exit(1);
599 }
17b6dea0
GH
600 security = SPICE_CHANNEL_SECURITY_SSL;
601 }
602 if (strcmp(name, "plaintext-channel") == 0) {
603 security = SPICE_CHANNEL_SECURITY_NONE;
604 }
605 if (security == 0) {
606 return 0;
607 }
608 if (strcmp(value, "default") == 0) {
609 rc = spice_server_set_channel_security(spice_server, NULL, security);
610 } else {
611 rc = spice_server_set_channel_security(spice_server, value, security);
612 }
613 if (rc != 0) {
339a475f 614 error_report("spice: failed to set channel security for %s", value);
17b6dea0
GH
615 exit(1);
616 }
617 return 0;
618}
619
f5bb039c
YH
620static void vm_change_state_handler(void *opaque, int running,
621 RunState state)
622{
f5bb039c 623 if (running) {
71d388d4 624 qemu_spice_display_start();
f5bb039c
YH
625 spice_server_vm_start(spice_server);
626 } else {
627 spice_server_vm_stop(spice_server);
71d388d4 628 qemu_spice_display_stop();
f5bb039c 629 }
f5bb039c
YH
630}
631
29b0040b
GH
632void qemu_spice_init(void)
633{
634 QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
333b0eeb 635 const char *password, *str, *x509_dir, *addr,
c448e855
GH
636 *x509_key_password = NULL,
637 *x509_dh_file = NULL,
638 *tls_ciphers = NULL;
639 char *x509_key_file = NULL,
640 *x509_cert_file = NULL,
641 *x509_cacert_file = NULL;
f61d6960 642 int port, tls_port, len, addr_flags;
9f04e09e
YH
643 spice_image_compression_t compression;
644 spice_wan_compression_t wan_compr;
8c957053 645 bool seamless_migration;
29b0040b 646
f9ab6091 647 qemu_thread_get_self(&me);
22b626e2 648
ad1be899 649 if (!opts) {
29b0040b
GH
650 return;
651 }
652 port = qemu_opt_get_number(opts, "port", 0);
c448e855
GH
653 tls_port = qemu_opt_get_number(opts, "tls-port", 0);
654 if (!port && !tls_port) {
339a475f 655 error_report("neither port nor tls-port specified for spice");
df9cb669
GH
656 exit(1);
657 }
658 if (port < 0 || port > 65535) {
339a475f 659 error_report("spice port is out of range");
df9cb669
GH
660 exit(1);
661 }
662 if (tls_port < 0 || tls_port > 65535) {
339a475f 663 error_report("spice tls-port is out of range");
df9cb669 664 exit(1);
29b0040b
GH
665 }
666 password = qemu_opt_get(opts, "password");
667
c448e855
GH
668 if (tls_port) {
669 x509_dir = qemu_opt_get(opts, "x509-dir");
670 if (NULL == x509_dir) {
671 x509_dir = ".";
672 }
673 len = strlen(x509_dir) + 32;
674
675 str = qemu_opt_get(opts, "x509-key-file");
676 if (str) {
7267c094 677 x509_key_file = g_strdup(str);
c448e855 678 } else {
7267c094 679 x509_key_file = g_malloc(len);
c448e855
GH
680 snprintf(x509_key_file, len, "%s/%s", x509_dir, X509_SERVER_KEY_FILE);
681 }
682
683 str = qemu_opt_get(opts, "x509-cert-file");
684 if (str) {
7267c094 685 x509_cert_file = g_strdup(str);
c448e855 686 } else {
7267c094 687 x509_cert_file = g_malloc(len);
c448e855
GH
688 snprintf(x509_cert_file, len, "%s/%s", x509_dir, X509_SERVER_CERT_FILE);
689 }
690
691 str = qemu_opt_get(opts, "x509-cacert-file");
692 if (str) {
7267c094 693 x509_cacert_file = g_strdup(str);
c448e855 694 } else {
7267c094 695 x509_cacert_file = g_malloc(len);
c448e855
GH
696 snprintf(x509_cacert_file, len, "%s/%s", x509_dir, X509_CA_CERT_FILE);
697 }
698
699 x509_key_password = qemu_opt_get(opts, "x509-key-password");
9995c0b7 700 x509_dh_file = qemu_opt_get(opts, "x509-dh-key-file");
c448e855
GH
701 tls_ciphers = qemu_opt_get(opts, "tls-ciphers");
702 }
703
333b0eeb
GH
704 addr = qemu_opt_get(opts, "addr");
705 addr_flags = 0;
706 if (qemu_opt_get_bool(opts, "ipv4", 0)) {
707 addr_flags |= SPICE_ADDR_FLAG_IPV4_ONLY;
708 } else if (qemu_opt_get_bool(opts, "ipv6", 0)) {
709 addr_flags |= SPICE_ADDR_FLAG_IPV6_ONLY;
710 }
711
29b0040b 712 spice_server = spice_server_new();
333b0eeb 713 spice_server_set_addr(spice_server, addr ? addr : "", addr_flags);
c448e855
GH
714 if (port) {
715 spice_server_set_port(spice_server, port);
716 }
717 if (tls_port) {
718 spice_server_set_tls(spice_server, tls_port,
719 x509_cacert_file,
720 x509_cert_file,
721 x509_key_file,
722 x509_key_password,
723 x509_dh_file,
724 tls_ciphers);
725 }
29b0040b
GH
726 if (password) {
727 spice_server_set_ticket(spice_server, password, 0, 0, 0);
728 }
48b3ed0a 729 if (qemu_opt_get_bool(opts, "sasl", 0)) {
48b3ed0a
MAL
730 if (spice_server_set_sasl_appname(spice_server, "qemu") == -1 ||
731 spice_server_set_sasl(spice_server, 1) == -1) {
339a475f 732 error_report("spice: failed to enable sasl");
48b3ed0a
MAL
733 exit(1);
734 }
48b3ed0a 735 }
29b0040b 736 if (qemu_opt_get_bool(opts, "disable-ticketing", 0)) {
6f8c63fb 737 auth = "none";
29b0040b
GH
738 spice_server_set_noauth(spice_server);
739 }
740
d4970b07
HG
741 if (qemu_opt_get_bool(opts, "disable-copy-paste", 0)) {
742 spice_server_set_agent_copypaste(spice_server, false);
743 }
d4970b07 744
5ad24e5f
HG
745 if (qemu_opt_get_bool(opts, "disable-agent-file-xfer", 0)) {
746#if SPICE_SERVER_VERSION >= 0x000c04
747 spice_server_set_agent_file_xfer(spice_server, false);
748#else
749 error_report("this qemu build does not support the "
750 "\"disable-agent-file-xfer\" option");
751 exit(1);
752#endif
753 }
754
9f04e09e
YH
755 compression = SPICE_IMAGE_COMPRESS_AUTO_GLZ;
756 str = qemu_opt_get(opts, "image-compression");
757 if (str) {
758 compression = parse_compression(str);
759 }
760 spice_server_set_image_compression(spice_server, compression);
761
762 wan_compr = SPICE_WAN_COMPRESSION_AUTO;
763 str = qemu_opt_get(opts, "jpeg-wan-compression");
764 if (str) {
765 wan_compr = parse_wan_compression(str);
766 }
767 spice_server_set_jpeg_compression(spice_server, wan_compr);
768
769 wan_compr = SPICE_WAN_COMPRESSION_AUTO;
770 str = qemu_opt_get(opts, "zlib-glz-wan-compression");
771 if (str) {
772 wan_compr = parse_wan_compression(str);
773 }
774 spice_server_set_zlib_glz_compression(spice_server, wan_compr);
29b0040b 775
84a23f25
GH
776 str = qemu_opt_get(opts, "streaming-video");
777 if (str) {
f61d6960 778 int streaming_video = parse_stream_video(str);
84a23f25
GH
779 spice_server_set_streaming_video(spice_server, streaming_video);
780 }
781
782 spice_server_set_agent_mouse
783 (spice_server, qemu_opt_get_bool(opts, "agent-mouse", 1));
784 spice_server_set_playback_compression
785 (spice_server, qemu_opt_get_bool(opts, "playback-compression", 1));
786
35c63329 787 qemu_opt_foreach(opts, add_channel, &tls_port, 0);
17b6dea0 788
d0638b18
MAL
789 spice_server_set_name(spice_server, qemu_name);
790 spice_server_set_uuid(spice_server, qemu_uuid);
d0638b18 791
8c957053
YH
792 seamless_migration = qemu_opt_get_bool(opts, "seamless-migration", 0);
793 spice_server_set_seamless_migration(spice_server, seamless_migration);
fba810f1 794 if (0 != spice_server_init(spice_server, &core_interface)) {
339a475f 795 error_report("failed to initialize spice server");
fba810f1
GH
796 exit(1);
797 };
29b0040b 798 using_spice = 1;
864401c2 799
e866e239
GH
800 migration_state.notify = migration_state_notifier;
801 add_migration_state_change_notifier(&migration_state);
026f773f
YH
802 spice_migrate.sin.base.sif = &migrate_interface.base;
803 spice_migrate.connect_complete.cb = NULL;
804 qemu_spice_add_interface(&spice_migrate.sin.base);
e866e239 805
864401c2 806 qemu_spice_input_init();
3e313753 807 qemu_spice_audio_init();
c448e855 808
bfb82a28 809 qemu_add_vm_change_state_handler(vm_change_state_handler, NULL);
f5bb039c 810
7267c094
AL
811 g_free(x509_key_file);
812 g_free(x509_cert_file);
813 g_free(x509_cacert_file);
afd0b409
MAL
814
815#if SPICE_SERVER_VERSION >= 0x000c02
816 qemu_spice_register_ports();
817#endif
29b0040b
GH
818}
819
820int qemu_spice_add_interface(SpiceBaseInstance *sin)
821{
a19cbfb3
GH
822 if (!spice_server) {
823 if (QTAILQ_FIRST(&qemu_spice_opts.head) != NULL) {
339a475f 824 error_report("Oops: spice configured but not active");
a19cbfb3
GH
825 exit(1);
826 }
827 /*
828 * Create a spice server instance.
829 * It does *not* listen on the network.
830 * It handles QXL local rendering only.
831 *
832 * With a command line like '-vnc :0 -vga qxl' you'll end up here.
833 */
834 spice_server = spice_server_new();
835 spice_server_init(spice_server, &core_interface);
bfb82a28 836 qemu_add_vm_change_state_handler(vm_change_state_handler, NULL);
a19cbfb3 837 }
71d388d4 838
29b0040b
GH
839 return spice_server_add_interface(spice_server, sin);
840}
841
7572150c
GH
842static int qemu_spice_set_ticket(bool fail_if_conn, bool disconnect_if_conn)
843{
844 time_t lifetime, now = time(NULL);
845 char *passwd;
846
847 if (now < auth_expires) {
848 passwd = auth_passwd;
849 lifetime = (auth_expires - now);
850 if (lifetime > INT_MAX) {
851 lifetime = INT_MAX;
852 }
853 } else {
854 passwd = NULL;
855 lifetime = 1;
856 }
857 return spice_server_set_ticket(spice_server, passwd, lifetime,
858 fail_if_conn, disconnect_if_conn);
859}
860
861int qemu_spice_set_passwd(const char *passwd,
862 bool fail_if_conn, bool disconnect_if_conn)
863{
fd3bea3f
MA
864 g_free(auth_passwd);
865 auth_passwd = g_strdup(passwd);
7572150c
GH
866 return qemu_spice_set_ticket(fail_if_conn, disconnect_if_conn);
867}
868
869int qemu_spice_set_pw_expire(time_t expires)
870{
871 auth_expires = expires;
872 return qemu_spice_set_ticket(false, false);
873}
874
f1f5f407
DB
875int qemu_spice_display_add_client(int csock, int skipauth, int tls)
876{
f1f5f407
DB
877 if (tls) {
878 return spice_server_add_ssl_client(spice_server, csock, skipauth);
879 } else {
880 return spice_server_add_client(spice_server, csock, skipauth);
881 }
f1f5f407
DB
882}
883
29b0040b
GH
884static void spice_register_config(void)
885{
886 qemu_add_opts(&qemu_spice_opts);
887}
888machine_init(spice_register_config);