fix sparse support (?)
[qemu.git] / ui / spice-core.c
blob27a1ced430a31a068fc00063cdbf49873d61e815
1 /*
2 * Copyright (C) 2010 Red Hat, Inc.
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.
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.
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/>.
18 #include <spice.h>
19 #include <spice-experimental.h>
21 #include <netdb.h>
23 #include "qemu-common.h"
24 #include "qemu-spice.h"
25 #include "qemu-timer.h"
26 #include "qemu-queue.h"
27 #include "qemu-x509.h"
28 #include "qemu_socket.h"
29 #include "qint.h"
30 #include "qbool.h"
31 #include "qstring.h"
32 #include "qjson.h"
33 #include "monitor.h"
35 /* core bits */
37 static SpiceServer *spice_server;
38 static const char *auth = "spice";
39 static char *auth_passwd;
40 static time_t auth_expires = TIME_MAX;
41 int using_spice = 0;
43 struct SpiceTimer {
44 QEMUTimer *timer;
45 QTAILQ_ENTRY(SpiceTimer) next;
47 static QTAILQ_HEAD(, SpiceTimer) timers = QTAILQ_HEAD_INITIALIZER(timers);
49 static SpiceTimer *timer_add(SpiceTimerFunc func, void *opaque)
51 SpiceTimer *timer;
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;
59 static void timer_start(SpiceTimer *timer, uint32_t ms)
61 qemu_mod_timer(timer->timer, qemu_get_clock(rt_clock) + ms);
64 static void timer_cancel(SpiceTimer *timer)
66 qemu_del_timer(timer->timer);
69 static void timer_remove(SpiceTimer *timer)
71 qemu_del_timer(timer->timer);
72 qemu_free_timer(timer->timer);
73 QTAILQ_REMOVE(&timers, timer, next);
74 qemu_free(timer);
77 struct SpiceWatch {
78 int fd;
79 int event_mask;
80 SpiceWatchFunc func;
81 void *opaque;
82 QTAILQ_ENTRY(SpiceWatch) next;
84 static QTAILQ_HEAD(, SpiceWatch) watches = QTAILQ_HEAD_INITIALIZER(watches);
86 static void watch_read(void *opaque)
88 SpiceWatch *watch = opaque;
89 watch->func(watch->fd, SPICE_WATCH_EVENT_READ, watch->opaque);
92 static void watch_write(void *opaque)
94 SpiceWatch *watch = opaque;
95 watch->func(watch->fd, SPICE_WATCH_EVENT_WRITE, watch->opaque);
98 static void watch_update_mask(SpiceWatch *watch, int event_mask)
100 IOHandler *on_read = NULL;
101 IOHandler *on_write = NULL;
103 watch->event_mask = event_mask;
104 if (watch->event_mask & SPICE_WATCH_EVENT_READ) {
105 on_read = watch_read;
107 if (watch->event_mask & SPICE_WATCH_EVENT_WRITE) {
108 on_write = watch_write;
110 qemu_set_fd_handler(watch->fd, on_read, on_write, watch);
113 static SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque)
115 SpiceWatch *watch;
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);
123 watch_update_mask(watch, event_mask);
124 return watch;
127 static void watch_remove(SpiceWatch *watch)
129 watch_update_mask(watch, 0);
130 QTAILQ_REMOVE(&watches, watch, next);
131 qemu_free(watch);
134 #if SPICE_INTERFACE_CORE_MINOR >= 3
136 typedef struct ChannelList ChannelList;
137 struct ChannelList {
138 SpiceChannelEventInfo *info;
139 QTAILQ_ENTRY(ChannelList) link;
141 static QTAILQ_HEAD(, ChannelList) channel_list = QTAILQ_HEAD_INITIALIZER(channel_list);
143 static void channel_list_add(SpiceChannelEventInfo *info)
145 ChannelList *item;
147 item = qemu_mallocz(sizeof(*item));
148 item->info = info;
149 QTAILQ_INSERT_TAIL(&channel_list, item, link);
152 static void channel_list_del(SpiceChannelEventInfo *info)
154 ChannelList *item;
156 QTAILQ_FOREACH(item, &channel_list, link) {
157 if (item->info != info) {
158 continue;
160 QTAILQ_REMOVE(&channel_list, item, link);
161 qemu_free(item);
162 return;
166 static void add_addr_info(QDict *dict, struct sockaddr *addr, int len)
168 char host[NI_MAXHOST], port[NI_MAXSERV];
169 const char *family;
171 getnameinfo(addr, len, host, sizeof(host), port, sizeof(port),
172 NI_NUMERICHOST | NI_NUMERICSERV);
173 family = inet_strfamily(addr->sa_family);
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));
180 static void add_channel_info(QDict *dict, SpiceChannelEventInfo *info)
182 int tls = info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS;
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));
190 static QList *channel_list_get(void)
192 ChannelList *item;
193 QList *list;
194 QDict *dict;
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);
203 return list;
206 static void channel_event(int event, SpiceChannelEventInfo *info)
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,
213 QDict *server, *client;
214 QObject *data;
216 client = qdict_new();
217 add_addr_info(client, &info->paddr, info->plen);
219 server = qdict_new();
220 add_addr_info(server, &info->laddr, info->llen);
222 if (event == SPICE_CHANNEL_EVENT_INITIALIZED) {
223 qdict_put(server, "auth", qstring_from_str(auth));
224 add_channel_info(client, info);
225 channel_list_add(info);
227 if (event == SPICE_CHANNEL_EVENT_DISCONNECTED) {
228 channel_list_del(info);
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);
237 #else /* SPICE_INTERFACE_CORE_MINOR >= 3 */
239 static QList *channel_list_get(void)
241 return NULL;
244 #endif /* SPICE_INTERFACE_CORE_MINOR >= 3 */
246 static 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,
252 .timer_add = timer_add,
253 .timer_start = timer_start,
254 .timer_cancel = timer_cancel,
255 .timer_remove = timer_remove,
257 .watch_add = watch_add,
258 .watch_update_mask = watch_update_mask,
259 .watch_remove = watch_remove,
261 #if SPICE_INTERFACE_CORE_MINOR >= 3
262 .channel_event = channel_event,
263 #endif
266 /* config string parsing */
268 static int name2enum(const char *string, const char *table[], int entries)
270 int i;
272 if (string) {
273 for (i = 0; i < entries; i++) {
274 if (!table[i]) {
275 continue;
277 if (strcmp(string, table[i]) != 0) {
278 continue;
280 return i;
283 return -1;
286 static int parse_name(const char *string, const char *optname,
287 const char *table[], int entries)
289 int value = name2enum(string, table, entries);
291 if (value != -1) {
292 return value;
294 fprintf(stderr, "spice: invalid %s: %s\n", optname, string);
295 exit(1);
298 #if SPICE_SERVER_VERSION >= 0x000600 /* 0.6.0 */
300 static const char *stream_video_names[] = {
301 [ SPICE_STREAM_VIDEO_OFF ] = "off",
302 [ SPICE_STREAM_VIDEO_ALL ] = "all",
303 [ SPICE_STREAM_VIDEO_FILTER ] = "filter",
305 #define parse_stream_video(_name) \
306 name2enum(_name, stream_video_names, ARRAY_SIZE(stream_video_names))
308 #endif /* >= 0.6.0 */
310 static 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",
318 #define parse_compression(_name) \
319 parse_name(_name, "image compression", \
320 compression_names, ARRAY_SIZE(compression_names))
322 static const char *wan_compression_names[] = {
323 [ SPICE_WAN_COMPRESSION_AUTO ] = "auto",
324 [ SPICE_WAN_COMPRESSION_NEVER ] = "never",
325 [ SPICE_WAN_COMPRESSION_ALWAYS ] = "always",
327 #define parse_wan_compression(_name) \
328 parse_name(_name, "wan compression", \
329 wan_compression_names, ARRAY_SIZE(wan_compression_names))
331 /* functions for the rest of qemu */
333 static void info_spice_iter(QObject *obj, void *opaque)
335 QDict *client;
336 Monitor *mon = opaque;
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"));
351 void do_info_spice_print(Monitor *mon, const QObject *data)
353 QDict *server;
354 QList *channels;
355 const char *host;
356 int port;
358 server = qobject_to_qdict(data);
359 if (qdict_get_bool(server, "enabled") == 0) {
360 monitor_printf(mon, "Server: disabled\n");
361 return;
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);
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);
374 monitor_printf(mon, " auth: %s\n", qdict_get_str(server, "auth"));
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);
384 void do_info_spice(Monitor *mon, QObject **ret_data)
386 QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
387 QDict *server;
388 QList *clist;
389 const char *addr;
390 int port, tls_port;
392 if (!spice_server) {
393 *ret_data = qobject_from_jsonf("{ 'enabled': false }");
394 return;
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();
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));
409 if (tls_port) {
410 qdict_put(server, "tls-port", qint_from_int(tls_port));
412 if (clist) {
413 qdict_put(server, "channels", clist);
416 *ret_data = QOBJECT(server);
419 static int add_channel(const char *name, const char *value, void *opaque)
421 int security = 0;
422 int rc;
424 if (strcmp(name, "tls-channel") == 0) {
425 security = SPICE_CHANNEL_SECURITY_SSL;
427 if (strcmp(name, "plaintext-channel") == 0) {
428 security = SPICE_CHANNEL_SECURITY_NONE;
430 if (security == 0) {
431 return 0;
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);
438 if (rc != 0) {
439 fprintf(stderr, "spice: failed to set channel security for %s\n", value);
440 exit(1);
442 return 0;
445 void qemu_spice_init(void)
447 QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
448 const char *password, *str, *x509_dir, *addr,
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;
455 int port, tls_port, len, addr_flags;
456 spice_image_compression_t compression;
457 spice_wan_compression_t wan_compr;
459 if (!opts) {
460 return;
462 port = qemu_opt_get_number(opts, "port", 0);
463 tls_port = qemu_opt_get_number(opts, "tls-port", 0);
464 if (!port && !tls_port) {
465 return;
467 password = qemu_opt_get(opts, "password");
469 if (tls_port) {
470 x509_dir = qemu_opt_get(opts, "x509-dir");
471 if (NULL == x509_dir) {
472 x509_dir = ".";
474 len = strlen(x509_dir) + 32;
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);
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);
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);
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");
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;
513 spice_server = spice_server_new();
514 spice_server_set_addr(spice_server, addr ? addr : "", addr_flags);
515 if (port) {
516 spice_server_set_port(spice_server, port);
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);
527 if (password) {
528 spice_server_set_ticket(spice_server, password, 0, 0, 0);
530 if (qemu_opt_get_bool(opts, "disable-ticketing", 0)) {
531 auth = "none";
532 spice_server_set_noauth(spice_server);
535 compression = SPICE_IMAGE_COMPRESS_AUTO_GLZ;
536 str = qemu_opt_get(opts, "image-compression");
537 if (str) {
538 compression = parse_compression(str);
540 spice_server_set_image_compression(spice_server, compression);
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);
547 spice_server_set_jpeg_compression(spice_server, wan_compr);
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);
554 spice_server_set_zlib_glz_compression(spice_server, wan_compr);
556 #if SPICE_SERVER_VERSION >= 0x000600 /* 0.6.0 */
558 str = qemu_opt_get(opts, "streaming-video");
559 if (str) {
560 int streaming_video = parse_stream_video(str);
561 spice_server_set_streaming_video(spice_server, streaming_video);
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));
569 #endif /* >= 0.6.0 */
571 qemu_opt_foreach(opts, add_channel, NULL, 0);
573 spice_server_init(spice_server, &core_interface);
574 using_spice = 1;
576 qemu_spice_input_init();
577 qemu_spice_audio_init();
579 qemu_free(x509_key_file);
580 qemu_free(x509_cert_file);
581 qemu_free(x509_cacert_file);
584 int qemu_spice_add_interface(SpiceBaseInstance *sin)
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);
592 * Create a spice server instance.
593 * It does *not* listen on the network.
594 * It handles QXL local rendering only.
596 * With a command line like '-vnc :0 -vga qxl' you'll end up here.
598 spice_server = spice_server_new();
599 spice_server_init(spice_server, &core_interface);
601 return spice_server_add_interface(spice_server, sin);
604 static int qemu_spice_set_ticket(bool fail_if_conn, bool disconnect_if_conn)
606 time_t lifetime, now = time(NULL);
607 char *passwd;
609 if (now < auth_expires) {
610 passwd = auth_passwd;
611 lifetime = (auth_expires - now);
612 if (lifetime > INT_MAX) {
613 lifetime = INT_MAX;
615 } else {
616 passwd = NULL;
617 lifetime = 1;
619 return spice_server_set_ticket(spice_server, passwd, lifetime,
620 fail_if_conn, disconnect_if_conn);
623 int qemu_spice_set_passwd(const char *passwd,
624 bool fail_if_conn, bool disconnect_if_conn)
626 free(auth_passwd);
627 auth_passwd = strdup(passwd);
628 return qemu_spice_set_ticket(fail_if_conn, disconnect_if_conn);
631 int qemu_spice_set_pw_expire(time_t expires)
633 auth_expires = expires;
634 return qemu_spice_set_ticket(false, false);
637 static void spice_register_config(void)
639 qemu_add_opts(&qemu_spice_opts);
641 machine_init(spice_register_config);
643 static void spice_initialize(void)
645 qemu_spice_init();
647 device_init(spice_initialize);