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