]> git.proxmox.com Git - qemu.git/blame - ui/spice-core.c
Merge branch 'spice.v23.pull' of git://anongit.freedesktop.org/spice/qemu
[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
GH
21#include <netdb.h>
22
29b0040b
GH
23#include "qemu-common.h"
24#include "qemu-spice.h"
25#include "qemu-timer.h"
26#include "qemu-queue.h"
c448e855 27#include "qemu-x509.h"
6f8c63fb
GH
28#include "qemu_socket.h"
29#include "qint.h"
30#include "qbool.h"
31#include "qstring.h"
32#include "qjson.h"
29b0040b
GH
33#include "monitor.h"
34
35/* core bits */
36
37static SpiceServer *spice_server;
6f8c63fb 38static const char *auth = "spice";
7572150c
GH
39static char *auth_passwd;
40static time_t auth_expires = TIME_MAX;
29b0040b
GH
41int using_spice = 0;
42
43struct SpiceTimer {
44 QEMUTimer *timer;
45 QTAILQ_ENTRY(SpiceTimer) next;
46};
47static QTAILQ_HEAD(, SpiceTimer) timers = QTAILQ_HEAD_INITIALIZER(timers);
48
49static SpiceTimer *timer_add(SpiceTimerFunc func, void *opaque)
50{
51 SpiceTimer *timer;
52
53 timer = qemu_mallocz(sizeof(*timer));
54 timer->timer = qemu_new_timer(rt_clock, func, opaque);
55 QTAILQ_INSERT_TAIL(&timers, timer, next);
56 return timer;
57}
58
59static void timer_start(SpiceTimer *timer, uint32_t ms)
60{
61 qemu_mod_timer(timer->timer, qemu_get_clock(rt_clock) + ms);
62}
63
64static void timer_cancel(SpiceTimer *timer)
65{
66 qemu_del_timer(timer->timer);
67}
68
69static void timer_remove(SpiceTimer *timer)
70{
71 qemu_del_timer(timer->timer);
72 qemu_free_timer(timer->timer);
73 QTAILQ_REMOVE(&timers, timer, next);
74 qemu_free(timer);
75}
76
77struct SpiceWatch {
78 int fd;
79 int event_mask;
80 SpiceWatchFunc func;
81 void *opaque;
82 QTAILQ_ENTRY(SpiceWatch) next;
83};
84static QTAILQ_HEAD(, SpiceWatch) watches = QTAILQ_HEAD_INITIALIZER(watches);
85
86static void watch_read(void *opaque)
87{
88 SpiceWatch *watch = opaque;
89 watch->func(watch->fd, SPICE_WATCH_EVENT_READ, watch->opaque);
90}
91
92static void watch_write(void *opaque)
93{
94 SpiceWatch *watch = opaque;
95 watch->func(watch->fd, SPICE_WATCH_EVENT_WRITE, watch->opaque);
96}
97
98static void watch_update_mask(SpiceWatch *watch, int event_mask)
99{
100 IOHandler *on_read = NULL;
101 IOHandler *on_write = NULL;
102
103 watch->event_mask = event_mask;
104 if (watch->event_mask & SPICE_WATCH_EVENT_READ) {
105 on_read = watch_read;
106 }
107 if (watch->event_mask & SPICE_WATCH_EVENT_WRITE) {
3d6d306c 108 on_write = watch_write;
29b0040b
GH
109 }
110 qemu_set_fd_handler(watch->fd, on_read, on_write, watch);
111}
112
113static SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque)
114{
115 SpiceWatch *watch;
116
117 watch = qemu_mallocz(sizeof(*watch));
118 watch->fd = fd;
119 watch->func = func;
120 watch->opaque = opaque;
121 QTAILQ_INSERT_TAIL(&watches, watch, next);
122
123 watch_update_mask(watch, event_mask);
124 return watch;
125}
126
127static void watch_remove(SpiceWatch *watch)
128{
129 watch_update_mask(watch, 0);
130 QTAILQ_REMOVE(&watches, watch, next);
131 qemu_free(watch);
132}
133
6f8c63fb
GH
134#if SPICE_INTERFACE_CORE_MINOR >= 3
135
cb42a870
GH
136typedef struct ChannelList ChannelList;
137struct ChannelList {
138 SpiceChannelEventInfo *info;
139 QTAILQ_ENTRY(ChannelList) link;
140};
141static QTAILQ_HEAD(, ChannelList) channel_list = QTAILQ_HEAD_INITIALIZER(channel_list);
142
143static void channel_list_add(SpiceChannelEventInfo *info)
144{
145 ChannelList *item;
146
147 item = qemu_mallocz(sizeof(*item));
148 item->info = info;
149 QTAILQ_INSERT_TAIL(&channel_list, item, link);
150}
151
152static void channel_list_del(SpiceChannelEventInfo *info)
153{
154 ChannelList *item;
155
156 QTAILQ_FOREACH(item, &channel_list, link) {
157 if (item->info != info) {
158 continue;
159 }
160 QTAILQ_REMOVE(&channel_list, item, link);
161 qemu_free(item);
162 return;
163 }
164}
165
6f8c63fb
GH
166static void add_addr_info(QDict *dict, struct sockaddr *addr, int len)
167{
168 char host[NI_MAXHOST], port[NI_MAXSERV];
169 const char *family;
170
171 getnameinfo(addr, len, host, sizeof(host), port, sizeof(port),
172 NI_NUMERICHOST | NI_NUMERICSERV);
173 family = inet_strfamily(addr->sa_family);
174
175 qdict_put(dict, "host", qstring_from_str(host));
176 qdict_put(dict, "port", qstring_from_str(port));
177 qdict_put(dict, "family", qstring_from_str(family));
178}
179
180static void add_channel_info(QDict *dict, SpiceChannelEventInfo *info)
181{
182 int tls = info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS;
183
184 qdict_put(dict, "connection-id", qint_from_int(info->connection_id));
185 qdict_put(dict, "channel-type", qint_from_int(info->type));
186 qdict_put(dict, "channel-id", qint_from_int(info->id));
187 qdict_put(dict, "tls", qbool_from_int(tls));
188}
189
cb42a870
GH
190static QList *channel_list_get(void)
191{
192 ChannelList *item;
193 QList *list;
194 QDict *dict;
195
196 list = qlist_new();
197 QTAILQ_FOREACH(item, &channel_list, link) {
198 dict = qdict_new();
199 add_addr_info(dict, &item->info->paddr, item->info->plen);
200 add_channel_info(dict, item->info);
201 qlist_append(list, dict);
202 }
203 return list;
204}
205
6f8c63fb
GH
206static void channel_event(int event, SpiceChannelEventInfo *info)
207{
208 static const int qevent[] = {
209 [ SPICE_CHANNEL_EVENT_CONNECTED ] = QEVENT_SPICE_CONNECTED,
210 [ SPICE_CHANNEL_EVENT_INITIALIZED ] = QEVENT_SPICE_INITIALIZED,
211 [ SPICE_CHANNEL_EVENT_DISCONNECTED ] = QEVENT_SPICE_DISCONNECTED,
212 };
213 QDict *server, *client;
214 QObject *data;
215
216 client = qdict_new();
217 add_addr_info(client, &info->paddr, info->plen);
218
219 server = qdict_new();
220 add_addr_info(server, &info->laddr, info->llen);
221
222 if (event == SPICE_CHANNEL_EVENT_INITIALIZED) {
223 qdict_put(server, "auth", qstring_from_str(auth));
224 add_channel_info(client, info);
cb42a870
GH
225 channel_list_add(info);
226 }
227 if (event == SPICE_CHANNEL_EVENT_DISCONNECTED) {
228 channel_list_del(info);
6f8c63fb
GH
229 }
230
231 data = qobject_from_jsonf("{ 'client': %p, 'server': %p }",
232 QOBJECT(client), QOBJECT(server));
233 monitor_protocol_event(qevent[event], data);
234 qobject_decref(data);
235}
236
237#else /* SPICE_INTERFACE_CORE_MINOR >= 3 */
238
239static QList *channel_list_get(void)
240{
241 return NULL;
242}
243
244#endif /* SPICE_INTERFACE_CORE_MINOR >= 3 */
245
29b0040b
GH
246static SpiceCoreInterface core_interface = {
247 .base.type = SPICE_INTERFACE_CORE,
248 .base.description = "qemu core services",
249 .base.major_version = SPICE_INTERFACE_CORE_MAJOR,
250 .base.minor_version = SPICE_INTERFACE_CORE_MINOR,
251
252 .timer_add = timer_add,
253 .timer_start = timer_start,
254 .timer_cancel = timer_cancel,
255 .timer_remove = timer_remove,
256
257 .watch_add = watch_add,
258 .watch_update_mask = watch_update_mask,
259 .watch_remove = watch_remove,
6f8c63fb
GH
260
261#if SPICE_INTERFACE_CORE_MINOR >= 3
262 .channel_event = channel_event,
263#endif
29b0040b
GH
264};
265
9f04e09e
YH
266/* config string parsing */
267
268static int name2enum(const char *string, const char *table[], int entries)
269{
270 int i;
271
272 if (string) {
273 for (i = 0; i < entries; i++) {
274 if (!table[i]) {
275 continue;
276 }
277 if (strcmp(string, table[i]) != 0) {
278 continue;
279 }
280 return i;
281 }
282 }
283 return -1;
284}
285
286static int parse_name(const char *string, const char *optname,
287 const char *table[], int entries)
288{
289 int value = name2enum(string, table, entries);
290
291 if (value != -1) {
292 return value;
293 }
294 fprintf(stderr, "spice: invalid %s: %s\n", optname, string);
295 exit(1);
296}
297
84a23f25
GH
298#if SPICE_SERVER_VERSION >= 0x000600 /* 0.6.0 */
299
300static const char *stream_video_names[] = {
301 [ SPICE_STREAM_VIDEO_OFF ] = "off",
302 [ SPICE_STREAM_VIDEO_ALL ] = "all",
303 [ SPICE_STREAM_VIDEO_FILTER ] = "filter",
304};
305#define parse_stream_video(_name) \
306 name2enum(_name, stream_video_names, ARRAY_SIZE(stream_video_names))
307
308#endif /* >= 0.6.0 */
309
9f04e09e
YH
310static const char *compression_names[] = {
311 [ SPICE_IMAGE_COMPRESS_OFF ] = "off",
312 [ SPICE_IMAGE_COMPRESS_AUTO_GLZ ] = "auto_glz",
313 [ SPICE_IMAGE_COMPRESS_AUTO_LZ ] = "auto_lz",
314 [ SPICE_IMAGE_COMPRESS_QUIC ] = "quic",
315 [ SPICE_IMAGE_COMPRESS_GLZ ] = "glz",
316 [ SPICE_IMAGE_COMPRESS_LZ ] = "lz",
317};
318#define parse_compression(_name) \
319 parse_name(_name, "image compression", \
320 compression_names, ARRAY_SIZE(compression_names))
321
322static const char *wan_compression_names[] = {
323 [ SPICE_WAN_COMPRESSION_AUTO ] = "auto",
324 [ SPICE_WAN_COMPRESSION_NEVER ] = "never",
325 [ SPICE_WAN_COMPRESSION_ALWAYS ] = "always",
326};
327#define parse_wan_compression(_name) \
328 parse_name(_name, "wan compression", \
329 wan_compression_names, ARRAY_SIZE(wan_compression_names))
330
29b0040b
GH
331/* functions for the rest of qemu */
332
cb42a870
GH
333static void info_spice_iter(QObject *obj, void *opaque)
334{
335 QDict *client;
336 Monitor *mon = opaque;
337
338 client = qobject_to_qdict(obj);
339 monitor_printf(mon, "Channel:\n");
340 monitor_printf(mon, " address: %s:%s%s\n",
341 qdict_get_str(client, "host"),
342 qdict_get_str(client, "port"),
343 qdict_get_bool(client, "tls") ? " [tls]" : "");
344 monitor_printf(mon, " session: %" PRId64 "\n",
345 qdict_get_int(client, "connection-id"));
346 monitor_printf(mon, " channel: %d:%d\n",
347 (int)qdict_get_int(client, "channel-type"),
348 (int)qdict_get_int(client, "channel-id"));
349}
350
351void do_info_spice_print(Monitor *mon, const QObject *data)
352{
353 QDict *server;
354 QList *channels;
355 const char *host;
356 int port;
357
358 server = qobject_to_qdict(data);
359 if (qdict_get_bool(server, "enabled") == 0) {
360 monitor_printf(mon, "Server: disabled\n");
361 return;
362 }
363
364 monitor_printf(mon, "Server:\n");
365 host = qdict_get_str(server, "host");
366 port = qdict_get_try_int(server, "port", -1);
367 if (port != -1) {
368 monitor_printf(mon, " address: %s:%d\n", host, port);
369 }
370 port = qdict_get_try_int(server, "tls-port", -1);
371 if (port != -1) {
372 monitor_printf(mon, " address: %s:%d [tls]\n", host, port);
373 }
374 monitor_printf(mon, " auth: %s\n", qdict_get_str(server, "auth"));
375
376 channels = qdict_get_qlist(server, "channels");
377 if (qlist_empty(channels)) {
378 monitor_printf(mon, "Channels: none\n");
379 } else {
380 qlist_iter(channels, info_spice_iter, mon);
381 }
382}
383
384void do_info_spice(Monitor *mon, QObject **ret_data)
385{
386 QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
387 QDict *server;
388 QList *clist;
389 const char *addr;
390 int port, tls_port;
391
392 if (!spice_server) {
393 *ret_data = qobject_from_jsonf("{ 'enabled': false }");
394 return;
395 }
396
397 addr = qemu_opt_get(opts, "addr");
398 port = qemu_opt_get_number(opts, "port", 0);
399 tls_port = qemu_opt_get_number(opts, "tls-port", 0);
400 clist = channel_list_get();
401
402 server = qdict_new();
403 qdict_put(server, "enabled", qbool_from_int(true));
404 qdict_put(server, "auth", qstring_from_str(auth));
405 qdict_put(server, "host", qstring_from_str(addr ? addr : "0.0.0.0"));
406 if (port) {
407 qdict_put(server, "port", qint_from_int(port));
408 }
409 if (tls_port) {
410 qdict_put(server, "tls-port", qint_from_int(tls_port));
411 }
412 if (clist) {
413 qdict_put(server, "channels", clist);
414 }
415
416 *ret_data = QOBJECT(server);
417}
418
17b6dea0
GH
419static int add_channel(const char *name, const char *value, void *opaque)
420{
421 int security = 0;
422 int rc;
423
424 if (strcmp(name, "tls-channel") == 0) {
425 security = SPICE_CHANNEL_SECURITY_SSL;
426 }
427 if (strcmp(name, "plaintext-channel") == 0) {
428 security = SPICE_CHANNEL_SECURITY_NONE;
429 }
430 if (security == 0) {
431 return 0;
432 }
433 if (strcmp(value, "default") == 0) {
434 rc = spice_server_set_channel_security(spice_server, NULL, security);
435 } else {
436 rc = spice_server_set_channel_security(spice_server, value, security);
437 }
438 if (rc != 0) {
439 fprintf(stderr, "spice: failed to set channel security for %s\n", value);
440 exit(1);
441 }
442 return 0;
443}
444
29b0040b
GH
445void qemu_spice_init(void)
446{
447 QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
333b0eeb 448 const char *password, *str, *x509_dir, *addr,
c448e855
GH
449 *x509_key_password = NULL,
450 *x509_dh_file = NULL,
451 *tls_ciphers = NULL;
452 char *x509_key_file = NULL,
453 *x509_cert_file = NULL,
454 *x509_cacert_file = NULL;
f61d6960 455 int port, tls_port, len, addr_flags;
9f04e09e
YH
456 spice_image_compression_t compression;
457 spice_wan_compression_t wan_compr;
29b0040b
GH
458
459 if (!opts) {
460 return;
461 }
462 port = qemu_opt_get_number(opts, "port", 0);
c448e855
GH
463 tls_port = qemu_opt_get_number(opts, "tls-port", 0);
464 if (!port && !tls_port) {
29b0040b
GH
465 return;
466 }
467 password = qemu_opt_get(opts, "password");
468
c448e855
GH
469 if (tls_port) {
470 x509_dir = qemu_opt_get(opts, "x509-dir");
471 if (NULL == x509_dir) {
472 x509_dir = ".";
473 }
474 len = strlen(x509_dir) + 32;
475
476 str = qemu_opt_get(opts, "x509-key-file");
477 if (str) {
478 x509_key_file = qemu_strdup(str);
479 } else {
480 x509_key_file = qemu_malloc(len);
481 snprintf(x509_key_file, len, "%s/%s", x509_dir, X509_SERVER_KEY_FILE);
482 }
483
484 str = qemu_opt_get(opts, "x509-cert-file");
485 if (str) {
486 x509_cert_file = qemu_strdup(str);
487 } else {
488 x509_cert_file = qemu_malloc(len);
489 snprintf(x509_cert_file, len, "%s/%s", x509_dir, X509_SERVER_CERT_FILE);
490 }
491
492 str = qemu_opt_get(opts, "x509-cacert-file");
493 if (str) {
494 x509_cacert_file = qemu_strdup(str);
495 } else {
496 x509_cacert_file = qemu_malloc(len);
497 snprintf(x509_cacert_file, len, "%s/%s", x509_dir, X509_CA_CERT_FILE);
498 }
499
500 x509_key_password = qemu_opt_get(opts, "x509-key-password");
501 x509_dh_file = qemu_opt_get(opts, "x509-dh-file");
502 tls_ciphers = qemu_opt_get(opts, "tls-ciphers");
503 }
504
333b0eeb
GH
505 addr = qemu_opt_get(opts, "addr");
506 addr_flags = 0;
507 if (qemu_opt_get_bool(opts, "ipv4", 0)) {
508 addr_flags |= SPICE_ADDR_FLAG_IPV4_ONLY;
509 } else if (qemu_opt_get_bool(opts, "ipv6", 0)) {
510 addr_flags |= SPICE_ADDR_FLAG_IPV6_ONLY;
511 }
512
29b0040b 513 spice_server = spice_server_new();
333b0eeb 514 spice_server_set_addr(spice_server, addr ? addr : "", addr_flags);
c448e855
GH
515 if (port) {
516 spice_server_set_port(spice_server, port);
517 }
518 if (tls_port) {
519 spice_server_set_tls(spice_server, tls_port,
520 x509_cacert_file,
521 x509_cert_file,
522 x509_key_file,
523 x509_key_password,
524 x509_dh_file,
525 tls_ciphers);
526 }
29b0040b
GH
527 if (password) {
528 spice_server_set_ticket(spice_server, password, 0, 0, 0);
529 }
530 if (qemu_opt_get_bool(opts, "disable-ticketing", 0)) {
6f8c63fb 531 auth = "none";
29b0040b
GH
532 spice_server_set_noauth(spice_server);
533 }
534
9f04e09e
YH
535 compression = SPICE_IMAGE_COMPRESS_AUTO_GLZ;
536 str = qemu_opt_get(opts, "image-compression");
537 if (str) {
538 compression = parse_compression(str);
539 }
540 spice_server_set_image_compression(spice_server, compression);
541
542 wan_compr = SPICE_WAN_COMPRESSION_AUTO;
543 str = qemu_opt_get(opts, "jpeg-wan-compression");
544 if (str) {
545 wan_compr = parse_wan_compression(str);
546 }
547 spice_server_set_jpeg_compression(spice_server, wan_compr);
548
549 wan_compr = SPICE_WAN_COMPRESSION_AUTO;
550 str = qemu_opt_get(opts, "zlib-glz-wan-compression");
551 if (str) {
552 wan_compr = parse_wan_compression(str);
553 }
554 spice_server_set_zlib_glz_compression(spice_server, wan_compr);
29b0040b 555
84a23f25
GH
556#if SPICE_SERVER_VERSION >= 0x000600 /* 0.6.0 */
557
558 str = qemu_opt_get(opts, "streaming-video");
559 if (str) {
f61d6960 560 int streaming_video = parse_stream_video(str);
84a23f25
GH
561 spice_server_set_streaming_video(spice_server, streaming_video);
562 }
563
564 spice_server_set_agent_mouse
565 (spice_server, qemu_opt_get_bool(opts, "agent-mouse", 1));
566 spice_server_set_playback_compression
567 (spice_server, qemu_opt_get_bool(opts, "playback-compression", 1));
568
569#endif /* >= 0.6.0 */
570
17b6dea0
GH
571 qemu_opt_foreach(opts, add_channel, NULL, 0);
572
29b0040b
GH
573 spice_server_init(spice_server, &core_interface);
574 using_spice = 1;
864401c2
GH
575
576 qemu_spice_input_init();
3e313753 577 qemu_spice_audio_init();
c448e855
GH
578
579 qemu_free(x509_key_file);
580 qemu_free(x509_cert_file);
581 qemu_free(x509_cacert_file);
29b0040b
GH
582}
583
584int qemu_spice_add_interface(SpiceBaseInstance *sin)
585{
a19cbfb3
GH
586 if (!spice_server) {
587 if (QTAILQ_FIRST(&qemu_spice_opts.head) != NULL) {
588 fprintf(stderr, "Oops: spice configured but not active\n");
589 exit(1);
590 }
591 /*
592 * Create a spice server instance.
593 * It does *not* listen on the network.
594 * It handles QXL local rendering only.
595 *
596 * With a command line like '-vnc :0 -vga qxl' you'll end up here.
597 */
598 spice_server = spice_server_new();
599 spice_server_init(spice_server, &core_interface);
600 }
29b0040b
GH
601 return spice_server_add_interface(spice_server, sin);
602}
603
7572150c
GH
604static int qemu_spice_set_ticket(bool fail_if_conn, bool disconnect_if_conn)
605{
606 time_t lifetime, now = time(NULL);
607 char *passwd;
608
609 if (now < auth_expires) {
610 passwd = auth_passwd;
611 lifetime = (auth_expires - now);
612 if (lifetime > INT_MAX) {
613 lifetime = INT_MAX;
614 }
615 } else {
616 passwd = NULL;
617 lifetime = 1;
618 }
619 return spice_server_set_ticket(spice_server, passwd, lifetime,
620 fail_if_conn, disconnect_if_conn);
621}
622
623int qemu_spice_set_passwd(const char *passwd,
624 bool fail_if_conn, bool disconnect_if_conn)
625{
626 free(auth_passwd);
627 auth_passwd = strdup(passwd);
628 return qemu_spice_set_ticket(fail_if_conn, disconnect_if_conn);
629}
630
631int qemu_spice_set_pw_expire(time_t expires)
632{
633 auth_expires = expires;
634 return qemu_spice_set_ticket(false, false);
635}
636
29b0040b
GH
637static void spice_register_config(void)
638{
639 qemu_add_opts(&qemu_spice_opts);
640}
641machine_init(spice_register_config);
642
643static void spice_initialize(void)
644{
645 qemu_spice_init();
646}
647device_init(spice_initialize);