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/>.
19 #include <spice-experimental.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"
34 #include "migration.h"
40 static SpiceServer
*spice_server
;
41 static Notifier migration_state
;
42 static const char *auth
= "spice";
43 static char *auth_passwd
;
44 static time_t auth_expires
= TIME_MAX
;
49 QTAILQ_ENTRY(SpiceTimer
) next
;
51 static QTAILQ_HEAD(, SpiceTimer
) timers
= QTAILQ_HEAD_INITIALIZER(timers
);
53 static SpiceTimer
*timer_add(SpiceTimerFunc func
, void *opaque
)
57 timer
= qemu_mallocz(sizeof(*timer
));
58 timer
->timer
= qemu_new_timer(rt_clock
, func
, opaque
);
59 QTAILQ_INSERT_TAIL(&timers
, timer
, next
);
63 static void timer_start(SpiceTimer
*timer
, uint32_t ms
)
65 qemu_mod_timer(timer
->timer
, qemu_get_clock(rt_clock
) + ms
);
68 static void timer_cancel(SpiceTimer
*timer
)
70 qemu_del_timer(timer
->timer
);
73 static void timer_remove(SpiceTimer
*timer
)
75 qemu_del_timer(timer
->timer
);
76 qemu_free_timer(timer
->timer
);
77 QTAILQ_REMOVE(&timers
, timer
, next
);
86 QTAILQ_ENTRY(SpiceWatch
) next
;
88 static QTAILQ_HEAD(, SpiceWatch
) watches
= QTAILQ_HEAD_INITIALIZER(watches
);
90 static void watch_read(void *opaque
)
92 SpiceWatch
*watch
= opaque
;
93 watch
->func(watch
->fd
, SPICE_WATCH_EVENT_READ
, watch
->opaque
);
96 static void watch_write(void *opaque
)
98 SpiceWatch
*watch
= opaque
;
99 watch
->func(watch
->fd
, SPICE_WATCH_EVENT_WRITE
, watch
->opaque
);
102 static void watch_update_mask(SpiceWatch
*watch
, int event_mask
)
104 IOHandler
*on_read
= NULL
;
105 IOHandler
*on_write
= NULL
;
107 watch
->event_mask
= event_mask
;
108 if (watch
->event_mask
& SPICE_WATCH_EVENT_READ
) {
109 on_read
= watch_read
;
111 if (watch
->event_mask
& SPICE_WATCH_EVENT_WRITE
) {
112 on_write
= watch_write
;
114 qemu_set_fd_handler(watch
->fd
, on_read
, on_write
, watch
);
117 static SpiceWatch
*watch_add(int fd
, int event_mask
, SpiceWatchFunc func
, void *opaque
)
121 watch
= qemu_mallocz(sizeof(*watch
));
124 watch
->opaque
= opaque
;
125 QTAILQ_INSERT_TAIL(&watches
, watch
, next
);
127 watch_update_mask(watch
, event_mask
);
131 static void watch_remove(SpiceWatch
*watch
)
133 watch_update_mask(watch
, 0);
134 QTAILQ_REMOVE(&watches
, watch
, next
);
138 #if SPICE_INTERFACE_CORE_MINOR >= 3
140 typedef struct ChannelList ChannelList
;
142 SpiceChannelEventInfo
*info
;
143 QTAILQ_ENTRY(ChannelList
) link
;
145 static QTAILQ_HEAD(, ChannelList
) channel_list
= QTAILQ_HEAD_INITIALIZER(channel_list
);
147 static void channel_list_add(SpiceChannelEventInfo
*info
)
151 item
= qemu_mallocz(sizeof(*item
));
153 QTAILQ_INSERT_TAIL(&channel_list
, item
, link
);
156 static void channel_list_del(SpiceChannelEventInfo
*info
)
160 QTAILQ_FOREACH(item
, &channel_list
, link
) {
161 if (item
->info
!= info
) {
164 QTAILQ_REMOVE(&channel_list
, item
, link
);
170 static void add_addr_info(QDict
*dict
, struct sockaddr
*addr
, int len
)
172 char host
[NI_MAXHOST
], port
[NI_MAXSERV
];
175 getnameinfo(addr
, len
, host
, sizeof(host
), port
, sizeof(port
),
176 NI_NUMERICHOST
| NI_NUMERICSERV
);
177 family
= inet_strfamily(addr
->sa_family
);
179 qdict_put(dict
, "host", qstring_from_str(host
));
180 qdict_put(dict
, "port", qstring_from_str(port
));
181 qdict_put(dict
, "family", qstring_from_str(family
));
184 static void add_channel_info(QDict
*dict
, SpiceChannelEventInfo
*info
)
186 int tls
= info
->flags
& SPICE_CHANNEL_EVENT_FLAG_TLS
;
188 qdict_put(dict
, "connection-id", qint_from_int(info
->connection_id
));
189 qdict_put(dict
, "channel-type", qint_from_int(info
->type
));
190 qdict_put(dict
, "channel-id", qint_from_int(info
->id
));
191 qdict_put(dict
, "tls", qbool_from_int(tls
));
194 static QList
*channel_list_get(void)
201 QTAILQ_FOREACH(item
, &channel_list
, link
) {
203 add_addr_info(dict
, &item
->info
->paddr
, item
->info
->plen
);
204 add_channel_info(dict
, item
->info
);
205 qlist_append(list
, dict
);
210 static void channel_event(int event
, SpiceChannelEventInfo
*info
)
212 static const int qevent
[] = {
213 [ SPICE_CHANNEL_EVENT_CONNECTED
] = QEVENT_SPICE_CONNECTED
,
214 [ SPICE_CHANNEL_EVENT_INITIALIZED
] = QEVENT_SPICE_INITIALIZED
,
215 [ SPICE_CHANNEL_EVENT_DISCONNECTED
] = QEVENT_SPICE_DISCONNECTED
,
217 QDict
*server
, *client
;
220 client
= qdict_new();
221 add_addr_info(client
, &info
->paddr
, info
->plen
);
223 server
= qdict_new();
224 add_addr_info(server
, &info
->laddr
, info
->llen
);
226 if (event
== SPICE_CHANNEL_EVENT_INITIALIZED
) {
227 qdict_put(server
, "auth", qstring_from_str(auth
));
228 add_channel_info(client
, info
);
229 channel_list_add(info
);
231 if (event
== SPICE_CHANNEL_EVENT_DISCONNECTED
) {
232 channel_list_del(info
);
235 data
= qobject_from_jsonf("{ 'client': %p, 'server': %p }",
236 QOBJECT(client
), QOBJECT(server
));
237 monitor_protocol_event(qevent
[event
], data
);
238 qobject_decref(data
);
241 #else /* SPICE_INTERFACE_CORE_MINOR >= 3 */
243 static QList
*channel_list_get(void)
248 #endif /* SPICE_INTERFACE_CORE_MINOR >= 3 */
250 static SpiceCoreInterface core_interface
= {
251 .base
.type
= SPICE_INTERFACE_CORE
,
252 .base
.description
= "qemu core services",
253 .base
.major_version
= SPICE_INTERFACE_CORE_MAJOR
,
254 .base
.minor_version
= SPICE_INTERFACE_CORE_MINOR
,
256 .timer_add
= timer_add
,
257 .timer_start
= timer_start
,
258 .timer_cancel
= timer_cancel
,
259 .timer_remove
= timer_remove
,
261 .watch_add
= watch_add
,
262 .watch_update_mask
= watch_update_mask
,
263 .watch_remove
= watch_remove
,
265 #if SPICE_INTERFACE_CORE_MINOR >= 3
266 .channel_event
= channel_event
,
270 /* config string parsing */
272 static int name2enum(const char *string
, const char *table
[], int entries
)
277 for (i
= 0; i
< entries
; i
++) {
281 if (strcmp(string
, table
[i
]) != 0) {
290 static int parse_name(const char *string
, const char *optname
,
291 const char *table
[], int entries
)
293 int value
= name2enum(string
, table
, entries
);
298 fprintf(stderr
, "spice: invalid %s: %s\n", optname
, string
);
302 #if SPICE_SERVER_VERSION >= 0x000600 /* 0.6.0 */
304 static const char *stream_video_names
[] = {
305 [ SPICE_STREAM_VIDEO_OFF
] = "off",
306 [ SPICE_STREAM_VIDEO_ALL
] = "all",
307 [ SPICE_STREAM_VIDEO_FILTER
] = "filter",
309 #define parse_stream_video(_name) \
310 name2enum(_name, stream_video_names, ARRAY_SIZE(stream_video_names))
312 #endif /* >= 0.6.0 */
314 static const char *compression_names
[] = {
315 [ SPICE_IMAGE_COMPRESS_OFF
] = "off",
316 [ SPICE_IMAGE_COMPRESS_AUTO_GLZ
] = "auto_glz",
317 [ SPICE_IMAGE_COMPRESS_AUTO_LZ
] = "auto_lz",
318 [ SPICE_IMAGE_COMPRESS_QUIC
] = "quic",
319 [ SPICE_IMAGE_COMPRESS_GLZ
] = "glz",
320 [ SPICE_IMAGE_COMPRESS_LZ
] = "lz",
322 #define parse_compression(_name) \
323 parse_name(_name, "image compression", \
324 compression_names, ARRAY_SIZE(compression_names))
326 static const char *wan_compression_names
[] = {
327 [ SPICE_WAN_COMPRESSION_AUTO
] = "auto",
328 [ SPICE_WAN_COMPRESSION_NEVER
] = "never",
329 [ SPICE_WAN_COMPRESSION_ALWAYS
] = "always",
331 #define parse_wan_compression(_name) \
332 parse_name(_name, "wan compression", \
333 wan_compression_names, ARRAY_SIZE(wan_compression_names))
335 /* functions for the rest of qemu */
337 static void info_spice_iter(QObject
*obj
, void *opaque
)
340 Monitor
*mon
= opaque
;
342 client
= qobject_to_qdict(obj
);
343 monitor_printf(mon
, "Channel:\n");
344 monitor_printf(mon
, " address: %s:%s%s\n",
345 qdict_get_str(client
, "host"),
346 qdict_get_str(client
, "port"),
347 qdict_get_bool(client
, "tls") ? " [tls]" : "");
348 monitor_printf(mon
, " session: %" PRId64
"\n",
349 qdict_get_int(client
, "connection-id"));
350 monitor_printf(mon
, " channel: %d:%d\n",
351 (int)qdict_get_int(client
, "channel-type"),
352 (int)qdict_get_int(client
, "channel-id"));
355 void do_info_spice_print(Monitor
*mon
, const QObject
*data
)
362 server
= qobject_to_qdict(data
);
363 if (qdict_get_bool(server
, "enabled") == 0) {
364 monitor_printf(mon
, "Server: disabled\n");
368 monitor_printf(mon
, "Server:\n");
369 host
= qdict_get_str(server
, "host");
370 port
= qdict_get_try_int(server
, "port", -1);
372 monitor_printf(mon
, " address: %s:%d\n", host
, port
);
374 port
= qdict_get_try_int(server
, "tls-port", -1);
376 monitor_printf(mon
, " address: %s:%d [tls]\n", host
, port
);
378 monitor_printf(mon
, " auth: %s\n", qdict_get_str(server
, "auth"));
380 channels
= qdict_get_qlist(server
, "channels");
381 if (qlist_empty(channels
)) {
382 monitor_printf(mon
, "Channels: none\n");
384 qlist_iter(channels
, info_spice_iter
, mon
);
388 void do_info_spice(Monitor
*mon
, QObject
**ret_data
)
390 QemuOpts
*opts
= QTAILQ_FIRST(&qemu_spice_opts
.head
);
397 *ret_data
= qobject_from_jsonf("{ 'enabled': false }");
401 addr
= qemu_opt_get(opts
, "addr");
402 port
= qemu_opt_get_number(opts
, "port", 0);
403 tls_port
= qemu_opt_get_number(opts
, "tls-port", 0);
404 clist
= channel_list_get();
406 server
= qdict_new();
407 qdict_put(server
, "enabled", qbool_from_int(true));
408 qdict_put(server
, "auth", qstring_from_str(auth
));
409 qdict_put(server
, "host", qstring_from_str(addr
? addr
: "0.0.0.0"));
411 qdict_put(server
, "port", qint_from_int(port
));
414 qdict_put(server
, "tls-port", qint_from_int(tls_port
));
417 qdict_put(server
, "channels", clist
);
420 *ret_data
= QOBJECT(server
);
423 static void migration_state_notifier(Notifier
*notifier
)
425 int state
= get_migration_state();
427 if (state
== MIG_STATE_COMPLETED
) {
428 #if SPICE_SERVER_VERSION >= 0x000701 /* 0.7.1 */
429 spice_server_migrate_switch(spice_server
);
434 int qemu_spice_migrate_info(const char *hostname
, int port
, int tls_port
,
437 return spice_server_migrate_info(spice_server
, hostname
,
438 port
, tls_port
, subject
);
441 static int add_channel(const char *name
, const char *value
, void *opaque
)
446 if (strcmp(name
, "tls-channel") == 0) {
447 security
= SPICE_CHANNEL_SECURITY_SSL
;
449 if (strcmp(name
, "plaintext-channel") == 0) {
450 security
= SPICE_CHANNEL_SECURITY_NONE
;
455 if (strcmp(value
, "default") == 0) {
456 rc
= spice_server_set_channel_security(spice_server
, NULL
, security
);
458 rc
= spice_server_set_channel_security(spice_server
, value
, security
);
461 fprintf(stderr
, "spice: failed to set channel security for %s\n", value
);
467 void qemu_spice_init(void)
469 QemuOpts
*opts
= QTAILQ_FIRST(&qemu_spice_opts
.head
);
470 const char *password
, *str
, *x509_dir
, *addr
,
471 *x509_key_password
= NULL
,
472 *x509_dh_file
= NULL
,
474 char *x509_key_file
= NULL
,
475 *x509_cert_file
= NULL
,
476 *x509_cacert_file
= NULL
;
477 int port
, tls_port
, len
, addr_flags
;
478 spice_image_compression_t compression
;
479 spice_wan_compression_t wan_compr
;
484 port
= qemu_opt_get_number(opts
, "port", 0);
485 tls_port
= qemu_opt_get_number(opts
, "tls-port", 0);
486 if (!port
&& !tls_port
) {
489 password
= qemu_opt_get(opts
, "password");
492 x509_dir
= qemu_opt_get(opts
, "x509-dir");
493 if (NULL
== x509_dir
) {
496 len
= strlen(x509_dir
) + 32;
498 str
= qemu_opt_get(opts
, "x509-key-file");
500 x509_key_file
= qemu_strdup(str
);
502 x509_key_file
= qemu_malloc(len
);
503 snprintf(x509_key_file
, len
, "%s/%s", x509_dir
, X509_SERVER_KEY_FILE
);
506 str
= qemu_opt_get(opts
, "x509-cert-file");
508 x509_cert_file
= qemu_strdup(str
);
510 x509_cert_file
= qemu_malloc(len
);
511 snprintf(x509_cert_file
, len
, "%s/%s", x509_dir
, X509_SERVER_CERT_FILE
);
514 str
= qemu_opt_get(opts
, "x509-cacert-file");
516 x509_cacert_file
= qemu_strdup(str
);
518 x509_cacert_file
= qemu_malloc(len
);
519 snprintf(x509_cacert_file
, len
, "%s/%s", x509_dir
, X509_CA_CERT_FILE
);
522 x509_key_password
= qemu_opt_get(opts
, "x509-key-password");
523 x509_dh_file
= qemu_opt_get(opts
, "x509-dh-file");
524 tls_ciphers
= qemu_opt_get(opts
, "tls-ciphers");
527 addr
= qemu_opt_get(opts
, "addr");
529 if (qemu_opt_get_bool(opts
, "ipv4", 0)) {
530 addr_flags
|= SPICE_ADDR_FLAG_IPV4_ONLY
;
531 } else if (qemu_opt_get_bool(opts
, "ipv6", 0)) {
532 addr_flags
|= SPICE_ADDR_FLAG_IPV6_ONLY
;
535 spice_server
= spice_server_new();
536 spice_server_set_addr(spice_server
, addr
? addr
: "", addr_flags
);
538 spice_server_set_port(spice_server
, port
);
541 spice_server_set_tls(spice_server
, tls_port
,
550 spice_server_set_ticket(spice_server
, password
, 0, 0, 0);
552 if (qemu_opt_get_bool(opts
, "disable-ticketing", 0)) {
554 spice_server_set_noauth(spice_server
);
557 compression
= SPICE_IMAGE_COMPRESS_AUTO_GLZ
;
558 str
= qemu_opt_get(opts
, "image-compression");
560 compression
= parse_compression(str
);
562 spice_server_set_image_compression(spice_server
, compression
);
564 wan_compr
= SPICE_WAN_COMPRESSION_AUTO
;
565 str
= qemu_opt_get(opts
, "jpeg-wan-compression");
567 wan_compr
= parse_wan_compression(str
);
569 spice_server_set_jpeg_compression(spice_server
, wan_compr
);
571 wan_compr
= SPICE_WAN_COMPRESSION_AUTO
;
572 str
= qemu_opt_get(opts
, "zlib-glz-wan-compression");
574 wan_compr
= parse_wan_compression(str
);
576 spice_server_set_zlib_glz_compression(spice_server
, wan_compr
);
578 #if SPICE_SERVER_VERSION >= 0x000600 /* 0.6.0 */
580 str
= qemu_opt_get(opts
, "streaming-video");
582 int streaming_video
= parse_stream_video(str
);
583 spice_server_set_streaming_video(spice_server
, streaming_video
);
586 spice_server_set_agent_mouse
587 (spice_server
, qemu_opt_get_bool(opts
, "agent-mouse", 1));
588 spice_server_set_playback_compression
589 (spice_server
, qemu_opt_get_bool(opts
, "playback-compression", 1));
591 #endif /* >= 0.6.0 */
593 qemu_opt_foreach(opts
, add_channel
, NULL
, 0);
595 spice_server_init(spice_server
, &core_interface
);
598 migration_state
.notify
= migration_state_notifier
;
599 add_migration_state_change_notifier(&migration_state
);
601 qemu_spice_input_init();
602 qemu_spice_audio_init();
604 qemu_free(x509_key_file
);
605 qemu_free(x509_cert_file
);
606 qemu_free(x509_cacert_file
);
609 int qemu_spice_add_interface(SpiceBaseInstance
*sin
)
612 if (QTAILQ_FIRST(&qemu_spice_opts
.head
) != NULL
) {
613 fprintf(stderr
, "Oops: spice configured but not active\n");
617 * Create a spice server instance.
618 * It does *not* listen on the network.
619 * It handles QXL local rendering only.
621 * With a command line like '-vnc :0 -vga qxl' you'll end up here.
623 spice_server
= spice_server_new();
624 spice_server_init(spice_server
, &core_interface
);
626 return spice_server_add_interface(spice_server
, sin
);
629 static int qemu_spice_set_ticket(bool fail_if_conn
, bool disconnect_if_conn
)
631 time_t lifetime
, now
= time(NULL
);
634 if (now
< auth_expires
) {
635 passwd
= auth_passwd
;
636 lifetime
= (auth_expires
- now
);
637 if (lifetime
> INT_MAX
) {
644 return spice_server_set_ticket(spice_server
, passwd
, lifetime
,
645 fail_if_conn
, disconnect_if_conn
);
648 int qemu_spice_set_passwd(const char *passwd
,
649 bool fail_if_conn
, bool disconnect_if_conn
)
652 auth_passwd
= strdup(passwd
);
653 return qemu_spice_set_ticket(fail_if_conn
, disconnect_if_conn
);
656 int qemu_spice_set_pw_expire(time_t expires
)
658 auth_expires
= expires
;
659 return qemu_spice_set_ticket(false, false);
662 static void spice_register_config(void)
664 qemu_add_opts(&qemu_spice_opts
);
666 machine_init(spice_register_config
);
668 static void spice_initialize(void)
672 device_init(spice_initialize
);