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"
37 static SpiceServer
*spice_server
;
38 static const char *auth
= "spice";
43 QTAILQ_ENTRY(SpiceTimer
) next
;
45 static QTAILQ_HEAD(, SpiceTimer
) timers
= QTAILQ_HEAD_INITIALIZER(timers
);
47 static SpiceTimer
*timer_add(SpiceTimerFunc func
, void *opaque
)
51 timer
= qemu_mallocz(sizeof(*timer
));
52 timer
->timer
= qemu_new_timer(rt_clock
, func
, opaque
);
53 QTAILQ_INSERT_TAIL(&timers
, timer
, next
);
57 static void timer_start(SpiceTimer
*timer
, uint32_t ms
)
59 qemu_mod_timer(timer
->timer
, qemu_get_clock(rt_clock
) + ms
);
62 static void timer_cancel(SpiceTimer
*timer
)
64 qemu_del_timer(timer
->timer
);
67 static void timer_remove(SpiceTimer
*timer
)
69 qemu_del_timer(timer
->timer
);
70 qemu_free_timer(timer
->timer
);
71 QTAILQ_REMOVE(&timers
, timer
, next
);
80 QTAILQ_ENTRY(SpiceWatch
) next
;
82 static QTAILQ_HEAD(, SpiceWatch
) watches
= QTAILQ_HEAD_INITIALIZER(watches
);
84 static void watch_read(void *opaque
)
86 SpiceWatch
*watch
= opaque
;
87 watch
->func(watch
->fd
, SPICE_WATCH_EVENT_READ
, watch
->opaque
);
90 static void watch_write(void *opaque
)
92 SpiceWatch
*watch
= opaque
;
93 watch
->func(watch
->fd
, SPICE_WATCH_EVENT_WRITE
, watch
->opaque
);
96 static void watch_update_mask(SpiceWatch
*watch
, int event_mask
)
98 IOHandler
*on_read
= NULL
;
99 IOHandler
*on_write
= NULL
;
101 watch
->event_mask
= event_mask
;
102 if (watch
->event_mask
& SPICE_WATCH_EVENT_READ
) {
103 on_read
= watch_read
;
105 if (watch
->event_mask
& SPICE_WATCH_EVENT_WRITE
) {
106 on_write
= watch_write
;
108 qemu_set_fd_handler(watch
->fd
, on_read
, on_write
, watch
);
111 static SpiceWatch
*watch_add(int fd
, int event_mask
, SpiceWatchFunc func
, void *opaque
)
115 watch
= qemu_mallocz(sizeof(*watch
));
118 watch
->opaque
= opaque
;
119 QTAILQ_INSERT_TAIL(&watches
, watch
, next
);
121 watch_update_mask(watch
, event_mask
);
125 static void watch_remove(SpiceWatch
*watch
)
127 watch_update_mask(watch
, 0);
128 QTAILQ_REMOVE(&watches
, watch
, next
);
132 #if SPICE_INTERFACE_CORE_MINOR >= 3
134 typedef struct ChannelList ChannelList
;
136 SpiceChannelEventInfo
*info
;
137 QTAILQ_ENTRY(ChannelList
) link
;
139 static QTAILQ_HEAD(, ChannelList
) channel_list
= QTAILQ_HEAD_INITIALIZER(channel_list
);
141 static void channel_list_add(SpiceChannelEventInfo
*info
)
145 item
= qemu_mallocz(sizeof(*item
));
147 QTAILQ_INSERT_TAIL(&channel_list
, item
, link
);
150 static void channel_list_del(SpiceChannelEventInfo
*info
)
154 QTAILQ_FOREACH(item
, &channel_list
, link
) {
155 if (item
->info
!= info
) {
158 QTAILQ_REMOVE(&channel_list
, item
, link
);
164 static void add_addr_info(QDict
*dict
, struct sockaddr
*addr
, int len
)
166 char host
[NI_MAXHOST
], port
[NI_MAXSERV
];
169 getnameinfo(addr
, len
, host
, sizeof(host
), port
, sizeof(port
),
170 NI_NUMERICHOST
| NI_NUMERICSERV
);
171 family
= inet_strfamily(addr
->sa_family
);
173 qdict_put(dict
, "host", qstring_from_str(host
));
174 qdict_put(dict
, "port", qstring_from_str(port
));
175 qdict_put(dict
, "family", qstring_from_str(family
));
178 static void add_channel_info(QDict
*dict
, SpiceChannelEventInfo
*info
)
180 int tls
= info
->flags
& SPICE_CHANNEL_EVENT_FLAG_TLS
;
182 qdict_put(dict
, "connection-id", qint_from_int(info
->connection_id
));
183 qdict_put(dict
, "channel-type", qint_from_int(info
->type
));
184 qdict_put(dict
, "channel-id", qint_from_int(info
->id
));
185 qdict_put(dict
, "tls", qbool_from_int(tls
));
188 static QList
*channel_list_get(void)
195 QTAILQ_FOREACH(item
, &channel_list
, link
) {
197 add_addr_info(dict
, &item
->info
->paddr
, item
->info
->plen
);
198 add_channel_info(dict
, item
->info
);
199 qlist_append(list
, dict
);
204 static void channel_event(int event
, SpiceChannelEventInfo
*info
)
206 static const int qevent
[] = {
207 [ SPICE_CHANNEL_EVENT_CONNECTED
] = QEVENT_SPICE_CONNECTED
,
208 [ SPICE_CHANNEL_EVENT_INITIALIZED
] = QEVENT_SPICE_INITIALIZED
,
209 [ SPICE_CHANNEL_EVENT_DISCONNECTED
] = QEVENT_SPICE_DISCONNECTED
,
211 QDict
*server
, *client
;
214 client
= qdict_new();
215 add_addr_info(client
, &info
->paddr
, info
->plen
);
217 server
= qdict_new();
218 add_addr_info(server
, &info
->laddr
, info
->llen
);
220 if (event
== SPICE_CHANNEL_EVENT_INITIALIZED
) {
221 qdict_put(server
, "auth", qstring_from_str(auth
));
222 add_channel_info(client
, info
);
223 channel_list_add(info
);
225 if (event
== SPICE_CHANNEL_EVENT_DISCONNECTED
) {
226 channel_list_del(info
);
229 data
= qobject_from_jsonf("{ 'client': %p, 'server': %p }",
230 QOBJECT(client
), QOBJECT(server
));
231 monitor_protocol_event(qevent
[event
], data
);
232 qobject_decref(data
);
235 #else /* SPICE_INTERFACE_CORE_MINOR >= 3 */
237 static QList
*channel_list_get(void)
242 #endif /* SPICE_INTERFACE_CORE_MINOR >= 3 */
244 static SpiceCoreInterface core_interface
= {
245 .base
.type
= SPICE_INTERFACE_CORE
,
246 .base
.description
= "qemu core services",
247 .base
.major_version
= SPICE_INTERFACE_CORE_MAJOR
,
248 .base
.minor_version
= SPICE_INTERFACE_CORE_MINOR
,
250 .timer_add
= timer_add
,
251 .timer_start
= timer_start
,
252 .timer_cancel
= timer_cancel
,
253 .timer_remove
= timer_remove
,
255 .watch_add
= watch_add
,
256 .watch_update_mask
= watch_update_mask
,
257 .watch_remove
= watch_remove
,
259 #if SPICE_INTERFACE_CORE_MINOR >= 3
260 .channel_event
= channel_event
,
264 /* config string parsing */
266 static int name2enum(const char *string
, const char *table
[], int entries
)
271 for (i
= 0; i
< entries
; i
++) {
275 if (strcmp(string
, table
[i
]) != 0) {
284 static int parse_name(const char *string
, const char *optname
,
285 const char *table
[], int entries
)
287 int value
= name2enum(string
, table
, entries
);
292 fprintf(stderr
, "spice: invalid %s: %s\n", optname
, string
);
296 #if SPICE_SERVER_VERSION >= 0x000600 /* 0.6.0 */
298 static const char *stream_video_names
[] = {
299 [ SPICE_STREAM_VIDEO_OFF
] = "off",
300 [ SPICE_STREAM_VIDEO_ALL
] = "all",
301 [ SPICE_STREAM_VIDEO_FILTER
] = "filter",
303 #define parse_stream_video(_name) \
304 name2enum(_name, stream_video_names, ARRAY_SIZE(stream_video_names))
306 #endif /* >= 0.6.0 */
308 static const char *compression_names
[] = {
309 [ SPICE_IMAGE_COMPRESS_OFF
] = "off",
310 [ SPICE_IMAGE_COMPRESS_AUTO_GLZ
] = "auto_glz",
311 [ SPICE_IMAGE_COMPRESS_AUTO_LZ
] = "auto_lz",
312 [ SPICE_IMAGE_COMPRESS_QUIC
] = "quic",
313 [ SPICE_IMAGE_COMPRESS_GLZ
] = "glz",
314 [ SPICE_IMAGE_COMPRESS_LZ
] = "lz",
316 #define parse_compression(_name) \
317 parse_name(_name, "image compression", \
318 compression_names, ARRAY_SIZE(compression_names))
320 static const char *wan_compression_names
[] = {
321 [ SPICE_WAN_COMPRESSION_AUTO
] = "auto",
322 [ SPICE_WAN_COMPRESSION_NEVER
] = "never",
323 [ SPICE_WAN_COMPRESSION_ALWAYS
] = "always",
325 #define parse_wan_compression(_name) \
326 parse_name(_name, "wan compression", \
327 wan_compression_names, ARRAY_SIZE(wan_compression_names))
329 /* functions for the rest of qemu */
331 static void info_spice_iter(QObject
*obj
, void *opaque
)
334 Monitor
*mon
= opaque
;
336 client
= qobject_to_qdict(obj
);
337 monitor_printf(mon
, "Channel:\n");
338 monitor_printf(mon
, " address: %s:%s%s\n",
339 qdict_get_str(client
, "host"),
340 qdict_get_str(client
, "port"),
341 qdict_get_bool(client
, "tls") ? " [tls]" : "");
342 monitor_printf(mon
, " session: %" PRId64
"\n",
343 qdict_get_int(client
, "connection-id"));
344 monitor_printf(mon
, " channel: %d:%d\n",
345 (int)qdict_get_int(client
, "channel-type"),
346 (int)qdict_get_int(client
, "channel-id"));
349 void do_info_spice_print(Monitor
*mon
, const QObject
*data
)
356 server
= qobject_to_qdict(data
);
357 if (qdict_get_bool(server
, "enabled") == 0) {
358 monitor_printf(mon
, "Server: disabled\n");
362 monitor_printf(mon
, "Server:\n");
363 host
= qdict_get_str(server
, "host");
364 port
= qdict_get_try_int(server
, "port", -1);
366 monitor_printf(mon
, " address: %s:%d\n", host
, port
);
368 port
= qdict_get_try_int(server
, "tls-port", -1);
370 monitor_printf(mon
, " address: %s:%d [tls]\n", host
, port
);
372 monitor_printf(mon
, " auth: %s\n", qdict_get_str(server
, "auth"));
374 channels
= qdict_get_qlist(server
, "channels");
375 if (qlist_empty(channels
)) {
376 monitor_printf(mon
, "Channels: none\n");
378 qlist_iter(channels
, info_spice_iter
, mon
);
382 void do_info_spice(Monitor
*mon
, QObject
**ret_data
)
384 QemuOpts
*opts
= QTAILQ_FIRST(&qemu_spice_opts
.head
);
391 *ret_data
= qobject_from_jsonf("{ 'enabled': false }");
395 addr
= qemu_opt_get(opts
, "addr");
396 port
= qemu_opt_get_number(opts
, "port", 0);
397 tls_port
= qemu_opt_get_number(opts
, "tls-port", 0);
398 clist
= channel_list_get();
400 server
= qdict_new();
401 qdict_put(server
, "enabled", qbool_from_int(true));
402 qdict_put(server
, "auth", qstring_from_str(auth
));
403 qdict_put(server
, "host", qstring_from_str(addr
? addr
: "0.0.0.0"));
405 qdict_put(server
, "port", qint_from_int(port
));
408 qdict_put(server
, "tls-port", qint_from_int(tls_port
));
411 qdict_put(server
, "channels", clist
);
414 *ret_data
= QOBJECT(server
);
417 static int add_channel(const char *name
, const char *value
, void *opaque
)
422 if (strcmp(name
, "tls-channel") == 0) {
423 security
= SPICE_CHANNEL_SECURITY_SSL
;
425 if (strcmp(name
, "plaintext-channel") == 0) {
426 security
= SPICE_CHANNEL_SECURITY_NONE
;
431 if (strcmp(value
, "default") == 0) {
432 rc
= spice_server_set_channel_security(spice_server
, NULL
, security
);
434 rc
= spice_server_set_channel_security(spice_server
, value
, security
);
437 fprintf(stderr
, "spice: failed to set channel security for %s\n", value
);
443 void qemu_spice_init(void)
445 QemuOpts
*opts
= QTAILQ_FIRST(&qemu_spice_opts
.head
);
446 const char *password
, *str
, *x509_dir
, *addr
,
447 *x509_key_password
= NULL
,
448 *x509_dh_file
= NULL
,
450 char *x509_key_file
= NULL
,
451 *x509_cert_file
= NULL
,
452 *x509_cacert_file
= NULL
;
453 int port
, tls_port
, len
, addr_flags
;
454 spice_image_compression_t compression
;
455 spice_wan_compression_t wan_compr
;
460 port
= qemu_opt_get_number(opts
, "port", 0);
461 tls_port
= qemu_opt_get_number(opts
, "tls-port", 0);
462 if (!port
&& !tls_port
) {
465 password
= qemu_opt_get(opts
, "password");
468 x509_dir
= qemu_opt_get(opts
, "x509-dir");
469 if (NULL
== x509_dir
) {
472 len
= strlen(x509_dir
) + 32;
474 str
= qemu_opt_get(opts
, "x509-key-file");
476 x509_key_file
= qemu_strdup(str
);
478 x509_key_file
= qemu_malloc(len
);
479 snprintf(x509_key_file
, len
, "%s/%s", x509_dir
, X509_SERVER_KEY_FILE
);
482 str
= qemu_opt_get(opts
, "x509-cert-file");
484 x509_cert_file
= qemu_strdup(str
);
486 x509_cert_file
= qemu_malloc(len
);
487 snprintf(x509_cert_file
, len
, "%s/%s", x509_dir
, X509_SERVER_CERT_FILE
);
490 str
= qemu_opt_get(opts
, "x509-cacert-file");
492 x509_cacert_file
= qemu_strdup(str
);
494 x509_cacert_file
= qemu_malloc(len
);
495 snprintf(x509_cacert_file
, len
, "%s/%s", x509_dir
, X509_CA_CERT_FILE
);
498 x509_key_password
= qemu_opt_get(opts
, "x509-key-password");
499 x509_dh_file
= qemu_opt_get(opts
, "x509-dh-file");
500 tls_ciphers
= qemu_opt_get(opts
, "tls-ciphers");
503 addr
= qemu_opt_get(opts
, "addr");
505 if (qemu_opt_get_bool(opts
, "ipv4", 0)) {
506 addr_flags
|= SPICE_ADDR_FLAG_IPV4_ONLY
;
507 } else if (qemu_opt_get_bool(opts
, "ipv6", 0)) {
508 addr_flags
|= SPICE_ADDR_FLAG_IPV6_ONLY
;
511 spice_server
= spice_server_new();
512 spice_server_set_addr(spice_server
, addr
? addr
: "", addr_flags
);
514 spice_server_set_port(spice_server
, port
);
517 spice_server_set_tls(spice_server
, tls_port
,
526 spice_server_set_ticket(spice_server
, password
, 0, 0, 0);
528 if (qemu_opt_get_bool(opts
, "disable-ticketing", 0)) {
530 spice_server_set_noauth(spice_server
);
533 compression
= SPICE_IMAGE_COMPRESS_AUTO_GLZ
;
534 str
= qemu_opt_get(opts
, "image-compression");
536 compression
= parse_compression(str
);
538 spice_server_set_image_compression(spice_server
, compression
);
540 wan_compr
= SPICE_WAN_COMPRESSION_AUTO
;
541 str
= qemu_opt_get(opts
, "jpeg-wan-compression");
543 wan_compr
= parse_wan_compression(str
);
545 spice_server_set_jpeg_compression(spice_server
, wan_compr
);
547 wan_compr
= SPICE_WAN_COMPRESSION_AUTO
;
548 str
= qemu_opt_get(opts
, "zlib-glz-wan-compression");
550 wan_compr
= parse_wan_compression(str
);
552 spice_server_set_zlib_glz_compression(spice_server
, wan_compr
);
554 #if SPICE_SERVER_VERSION >= 0x000600 /* 0.6.0 */
556 str
= qemu_opt_get(opts
, "streaming-video");
558 int streaming_video
= parse_stream_video(str
);
559 spice_server_set_streaming_video(spice_server
, streaming_video
);
562 spice_server_set_agent_mouse
563 (spice_server
, qemu_opt_get_bool(opts
, "agent-mouse", 1));
564 spice_server_set_playback_compression
565 (spice_server
, qemu_opt_get_bool(opts
, "playback-compression", 1));
567 #endif /* >= 0.6.0 */
569 qemu_opt_foreach(opts
, add_channel
, NULL
, 0);
571 spice_server_init(spice_server
, &core_interface
);
574 qemu_spice_input_init();
575 qemu_spice_audio_init();
577 qemu_free(x509_key_file
);
578 qemu_free(x509_cert_file
);
579 qemu_free(x509_cacert_file
);
582 int qemu_spice_add_interface(SpiceBaseInstance
*sin
)
585 if (QTAILQ_FIRST(&qemu_spice_opts
.head
) != NULL
) {
586 fprintf(stderr
, "Oops: spice configured but not active\n");
590 * Create a spice server instance.
591 * It does *not* listen on the network.
592 * It handles QXL local rendering only.
594 * With a command line like '-vnc :0 -vga qxl' you'll end up here.
596 spice_server
= spice_server_new();
597 spice_server_init(spice_server
, &core_interface
);
599 return spice_server_add_interface(spice_server
, sin
);
602 static void spice_register_config(void)
604 qemu_add_opts(&qemu_spice_opts
);
606 machine_init(spice_register_config
);
608 static void spice_initialize(void)
612 device_init(spice_initialize
);