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 static void add_addr_info(QDict
*dict
, struct sockaddr
*addr
, int len
)
136 char host
[NI_MAXHOST
], port
[NI_MAXSERV
];
139 getnameinfo(addr
, len
, host
, sizeof(host
), port
, sizeof(port
),
140 NI_NUMERICHOST
| NI_NUMERICSERV
);
141 family
= inet_strfamily(addr
->sa_family
);
143 qdict_put(dict
, "host", qstring_from_str(host
));
144 qdict_put(dict
, "port", qstring_from_str(port
));
145 qdict_put(dict
, "family", qstring_from_str(family
));
148 static void add_channel_info(QDict
*dict
, SpiceChannelEventInfo
*info
)
150 int tls
= info
->flags
& SPICE_CHANNEL_EVENT_FLAG_TLS
;
152 qdict_put(dict
, "connection-id", qint_from_int(info
->connection_id
));
153 qdict_put(dict
, "channel-type", qint_from_int(info
->type
));
154 qdict_put(dict
, "channel-id", qint_from_int(info
->id
));
155 qdict_put(dict
, "tls", qbool_from_int(tls
));
158 static void channel_event(int event
, SpiceChannelEventInfo
*info
)
160 static const int qevent
[] = {
161 [ SPICE_CHANNEL_EVENT_CONNECTED
] = QEVENT_SPICE_CONNECTED
,
162 [ SPICE_CHANNEL_EVENT_INITIALIZED
] = QEVENT_SPICE_INITIALIZED
,
163 [ SPICE_CHANNEL_EVENT_DISCONNECTED
] = QEVENT_SPICE_DISCONNECTED
,
165 QDict
*server
, *client
;
168 client
= qdict_new();
169 add_addr_info(client
, &info
->paddr
, info
->plen
);
171 server
= qdict_new();
172 add_addr_info(server
, &info
->laddr
, info
->llen
);
174 if (event
== SPICE_CHANNEL_EVENT_INITIALIZED
) {
175 qdict_put(server
, "auth", qstring_from_str(auth
));
176 add_channel_info(client
, info
);
179 data
= qobject_from_jsonf("{ 'client': %p, 'server': %p }",
180 QOBJECT(client
), QOBJECT(server
));
181 monitor_protocol_event(qevent
[event
], data
);
182 qobject_decref(data
);
185 #else /* SPICE_INTERFACE_CORE_MINOR >= 3 */
187 static QList
*channel_list_get(void)
192 #endif /* SPICE_INTERFACE_CORE_MINOR >= 3 */
194 static SpiceCoreInterface core_interface
= {
195 .base
.type
= SPICE_INTERFACE_CORE
,
196 .base
.description
= "qemu core services",
197 .base
.major_version
= SPICE_INTERFACE_CORE_MAJOR
,
198 .base
.minor_version
= SPICE_INTERFACE_CORE_MINOR
,
200 .timer_add
= timer_add
,
201 .timer_start
= timer_start
,
202 .timer_cancel
= timer_cancel
,
203 .timer_remove
= timer_remove
,
205 .watch_add
= watch_add
,
206 .watch_update_mask
= watch_update_mask
,
207 .watch_remove
= watch_remove
,
209 #if SPICE_INTERFACE_CORE_MINOR >= 3
210 .channel_event
= channel_event
,
214 /* config string parsing */
216 static int name2enum(const char *string
, const char *table
[], int entries
)
221 for (i
= 0; i
< entries
; i
++) {
225 if (strcmp(string
, table
[i
]) != 0) {
234 static int parse_name(const char *string
, const char *optname
,
235 const char *table
[], int entries
)
237 int value
= name2enum(string
, table
, entries
);
242 fprintf(stderr
, "spice: invalid %s: %s\n", optname
, string
);
246 #if SPICE_SERVER_VERSION >= 0x000600 /* 0.6.0 */
248 static const char *stream_video_names
[] = {
249 [ SPICE_STREAM_VIDEO_OFF
] = "off",
250 [ SPICE_STREAM_VIDEO_ALL
] = "all",
251 [ SPICE_STREAM_VIDEO_FILTER
] = "filter",
253 #define parse_stream_video(_name) \
254 name2enum(_name, stream_video_names, ARRAY_SIZE(stream_video_names))
256 #endif /* >= 0.6.0 */
258 static const char *compression_names
[] = {
259 [ SPICE_IMAGE_COMPRESS_OFF
] = "off",
260 [ SPICE_IMAGE_COMPRESS_AUTO_GLZ
] = "auto_glz",
261 [ SPICE_IMAGE_COMPRESS_AUTO_LZ
] = "auto_lz",
262 [ SPICE_IMAGE_COMPRESS_QUIC
] = "quic",
263 [ SPICE_IMAGE_COMPRESS_GLZ
] = "glz",
264 [ SPICE_IMAGE_COMPRESS_LZ
] = "lz",
266 #define parse_compression(_name) \
267 parse_name(_name, "image compression", \
268 compression_names, ARRAY_SIZE(compression_names))
270 static const char *wan_compression_names
[] = {
271 [ SPICE_WAN_COMPRESSION_AUTO
] = "auto",
272 [ SPICE_WAN_COMPRESSION_NEVER
] = "never",
273 [ SPICE_WAN_COMPRESSION_ALWAYS
] = "always",
275 #define parse_wan_compression(_name) \
276 parse_name(_name, "wan compression", \
277 wan_compression_names, ARRAY_SIZE(wan_compression_names))
279 /* functions for the rest of qemu */
281 static int add_channel(const char *name
, const char *value
, void *opaque
)
286 if (strcmp(name
, "tls-channel") == 0) {
287 security
= SPICE_CHANNEL_SECURITY_SSL
;
289 if (strcmp(name
, "plaintext-channel") == 0) {
290 security
= SPICE_CHANNEL_SECURITY_NONE
;
295 if (strcmp(value
, "default") == 0) {
296 rc
= spice_server_set_channel_security(spice_server
, NULL
, security
);
298 rc
= spice_server_set_channel_security(spice_server
, value
, security
);
301 fprintf(stderr
, "spice: failed to set channel security for %s\n", value
);
307 void qemu_spice_init(void)
309 QemuOpts
*opts
= QTAILQ_FIRST(&qemu_spice_opts
.head
);
310 const char *password
, *str
, *x509_dir
, *addr
,
311 *x509_key_password
= NULL
,
312 *x509_dh_file
= NULL
,
314 char *x509_key_file
= NULL
,
315 *x509_cert_file
= NULL
,
316 *x509_cacert_file
= NULL
;
317 int port
, tls_port
, len
, addr_flags
;
318 spice_image_compression_t compression
;
319 spice_wan_compression_t wan_compr
;
324 port
= qemu_opt_get_number(opts
, "port", 0);
325 tls_port
= qemu_opt_get_number(opts
, "tls-port", 0);
326 if (!port
&& !tls_port
) {
329 password
= qemu_opt_get(opts
, "password");
332 x509_dir
= qemu_opt_get(opts
, "x509-dir");
333 if (NULL
== x509_dir
) {
336 len
= strlen(x509_dir
) + 32;
338 str
= qemu_opt_get(opts
, "x509-key-file");
340 x509_key_file
= qemu_strdup(str
);
342 x509_key_file
= qemu_malloc(len
);
343 snprintf(x509_key_file
, len
, "%s/%s", x509_dir
, X509_SERVER_KEY_FILE
);
346 str
= qemu_opt_get(opts
, "x509-cert-file");
348 x509_cert_file
= qemu_strdup(str
);
350 x509_cert_file
= qemu_malloc(len
);
351 snprintf(x509_cert_file
, len
, "%s/%s", x509_dir
, X509_SERVER_CERT_FILE
);
354 str
= qemu_opt_get(opts
, "x509-cacert-file");
356 x509_cacert_file
= qemu_strdup(str
);
358 x509_cacert_file
= qemu_malloc(len
);
359 snprintf(x509_cacert_file
, len
, "%s/%s", x509_dir
, X509_CA_CERT_FILE
);
362 x509_key_password
= qemu_opt_get(opts
, "x509-key-password");
363 x509_dh_file
= qemu_opt_get(opts
, "x509-dh-file");
364 tls_ciphers
= qemu_opt_get(opts
, "tls-ciphers");
367 addr
= qemu_opt_get(opts
, "addr");
369 if (qemu_opt_get_bool(opts
, "ipv4", 0)) {
370 addr_flags
|= SPICE_ADDR_FLAG_IPV4_ONLY
;
371 } else if (qemu_opt_get_bool(opts
, "ipv6", 0)) {
372 addr_flags
|= SPICE_ADDR_FLAG_IPV6_ONLY
;
375 spice_server
= spice_server_new();
376 spice_server_set_addr(spice_server
, addr
? addr
: "", addr_flags
);
378 spice_server_set_port(spice_server
, port
);
381 spice_server_set_tls(spice_server
, tls_port
,
390 spice_server_set_ticket(spice_server
, password
, 0, 0, 0);
392 if (qemu_opt_get_bool(opts
, "disable-ticketing", 0)) {
394 spice_server_set_noauth(spice_server
);
397 compression
= SPICE_IMAGE_COMPRESS_AUTO_GLZ
;
398 str
= qemu_opt_get(opts
, "image-compression");
400 compression
= parse_compression(str
);
402 spice_server_set_image_compression(spice_server
, compression
);
404 wan_compr
= SPICE_WAN_COMPRESSION_AUTO
;
405 str
= qemu_opt_get(opts
, "jpeg-wan-compression");
407 wan_compr
= parse_wan_compression(str
);
409 spice_server_set_jpeg_compression(spice_server
, wan_compr
);
411 wan_compr
= SPICE_WAN_COMPRESSION_AUTO
;
412 str
= qemu_opt_get(opts
, "zlib-glz-wan-compression");
414 wan_compr
= parse_wan_compression(str
);
416 spice_server_set_zlib_glz_compression(spice_server
, wan_compr
);
418 #if SPICE_SERVER_VERSION >= 0x000600 /* 0.6.0 */
420 str
= qemu_opt_get(opts
, "streaming-video");
422 int streaming_video
= parse_stream_video(str
);
423 spice_server_set_streaming_video(spice_server
, streaming_video
);
426 spice_server_set_agent_mouse
427 (spice_server
, qemu_opt_get_bool(opts
, "agent-mouse", 1));
428 spice_server_set_playback_compression
429 (spice_server
, qemu_opt_get_bool(opts
, "playback-compression", 1));
431 #endif /* >= 0.6.0 */
433 qemu_opt_foreach(opts
, add_channel
, NULL
, 0);
435 spice_server_init(spice_server
, &core_interface
);
438 qemu_spice_input_init();
439 qemu_spice_audio_init();
441 qemu_free(x509_key_file
);
442 qemu_free(x509_cert_file
);
443 qemu_free(x509_cacert_file
);
446 int qemu_spice_add_interface(SpiceBaseInstance
*sin
)
449 if (QTAILQ_FIRST(&qemu_spice_opts
.head
) != NULL
) {
450 fprintf(stderr
, "Oops: spice configured but not active\n");
454 * Create a spice server instance.
455 * It does *not* listen on the network.
456 * It handles QXL local rendering only.
458 * With a command line like '-vnc :0 -vga qxl' you'll end up here.
460 spice_server
= spice_server_new();
461 spice_server_init(spice_server
, &core_interface
);
463 return spice_server_add_interface(spice_server
, sin
);
466 static void spice_register_config(void)
468 qemu_add_opts(&qemu_spice_opts
);
470 machine_init(spice_register_config
);
472 static void spice_initialize(void)
476 device_init(spice_initialize
);