]>
Commit | Line | Data |
---|---|---|
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" | |
e866e239 GH |
33 | #include "notify.h" |
34 | #include "migration.h" | |
29b0040b | 35 | #include "monitor.h" |
e866e239 | 36 | #include "hw/hw.h" |
29b0040b GH |
37 | |
38 | /* core bits */ | |
39 | ||
40 | static SpiceServer *spice_server; | |
e866e239 | 41 | static Notifier migration_state; |
6f8c63fb | 42 | static const char *auth = "spice"; |
7572150c GH |
43 | static char *auth_passwd; |
44 | static time_t auth_expires = TIME_MAX; | |
29b0040b GH |
45 | int using_spice = 0; |
46 | ||
47 | struct SpiceTimer { | |
48 | QEMUTimer *timer; | |
49 | QTAILQ_ENTRY(SpiceTimer) next; | |
50 | }; | |
51 | static QTAILQ_HEAD(, SpiceTimer) timers = QTAILQ_HEAD_INITIALIZER(timers); | |
52 | ||
53 | static SpiceTimer *timer_add(SpiceTimerFunc func, void *opaque) | |
54 | { | |
55 | SpiceTimer *timer; | |
56 | ||
7267c094 | 57 | timer = g_malloc0(sizeof(*timer)); |
7bd427d8 | 58 | timer->timer = qemu_new_timer_ms(rt_clock, func, opaque); |
29b0040b GH |
59 | QTAILQ_INSERT_TAIL(&timers, timer, next); |
60 | return timer; | |
61 | } | |
62 | ||
63 | static void timer_start(SpiceTimer *timer, uint32_t ms) | |
64 | { | |
7bd427d8 | 65 | qemu_mod_timer(timer->timer, qemu_get_clock_ms(rt_clock) + ms); |
29b0040b GH |
66 | } |
67 | ||
68 | static void timer_cancel(SpiceTimer *timer) | |
69 | { | |
70 | qemu_del_timer(timer->timer); | |
71 | } | |
72 | ||
73 | static void timer_remove(SpiceTimer *timer) | |
74 | { | |
75 | qemu_del_timer(timer->timer); | |
76 | qemu_free_timer(timer->timer); | |
77 | QTAILQ_REMOVE(&timers, timer, next); | |
7267c094 | 78 | g_free(timer); |
29b0040b GH |
79 | } |
80 | ||
81 | struct SpiceWatch { | |
82 | int fd; | |
83 | int event_mask; | |
84 | SpiceWatchFunc func; | |
85 | void *opaque; | |
86 | QTAILQ_ENTRY(SpiceWatch) next; | |
87 | }; | |
88 | static QTAILQ_HEAD(, SpiceWatch) watches = QTAILQ_HEAD_INITIALIZER(watches); | |
89 | ||
90 | static void watch_read(void *opaque) | |
91 | { | |
92 | SpiceWatch *watch = opaque; | |
93 | watch->func(watch->fd, SPICE_WATCH_EVENT_READ, watch->opaque); | |
94 | } | |
95 | ||
96 | static void watch_write(void *opaque) | |
97 | { | |
98 | SpiceWatch *watch = opaque; | |
99 | watch->func(watch->fd, SPICE_WATCH_EVENT_WRITE, watch->opaque); | |
100 | } | |
101 | ||
102 | static void watch_update_mask(SpiceWatch *watch, int event_mask) | |
103 | { | |
104 | IOHandler *on_read = NULL; | |
105 | IOHandler *on_write = NULL; | |
106 | ||
107 | watch->event_mask = event_mask; | |
108 | if (watch->event_mask & SPICE_WATCH_EVENT_READ) { | |
109 | on_read = watch_read; | |
110 | } | |
111 | if (watch->event_mask & SPICE_WATCH_EVENT_WRITE) { | |
3d6d306c | 112 | on_write = watch_write; |
29b0040b GH |
113 | } |
114 | qemu_set_fd_handler(watch->fd, on_read, on_write, watch); | |
115 | } | |
116 | ||
117 | static SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque) | |
118 | { | |
119 | SpiceWatch *watch; | |
120 | ||
7267c094 | 121 | watch = g_malloc0(sizeof(*watch)); |
29b0040b GH |
122 | watch->fd = fd; |
123 | watch->func = func; | |
124 | watch->opaque = opaque; | |
125 | QTAILQ_INSERT_TAIL(&watches, watch, next); | |
126 | ||
127 | watch_update_mask(watch, event_mask); | |
128 | return watch; | |
129 | } | |
130 | ||
131 | static void watch_remove(SpiceWatch *watch) | |
132 | { | |
133 | watch_update_mask(watch, 0); | |
134 | QTAILQ_REMOVE(&watches, watch, next); | |
7267c094 | 135 | g_free(watch); |
29b0040b GH |
136 | } |
137 | ||
6f8c63fb GH |
138 | #if SPICE_INTERFACE_CORE_MINOR >= 3 |
139 | ||
cb42a870 GH |
140 | typedef struct ChannelList ChannelList; |
141 | struct ChannelList { | |
142 | SpiceChannelEventInfo *info; | |
143 | QTAILQ_ENTRY(ChannelList) link; | |
144 | }; | |
145 | static QTAILQ_HEAD(, ChannelList) channel_list = QTAILQ_HEAD_INITIALIZER(channel_list); | |
146 | ||
147 | static void channel_list_add(SpiceChannelEventInfo *info) | |
148 | { | |
149 | ChannelList *item; | |
150 | ||
7267c094 | 151 | item = g_malloc0(sizeof(*item)); |
cb42a870 GH |
152 | item->info = info; |
153 | QTAILQ_INSERT_TAIL(&channel_list, item, link); | |
154 | } | |
155 | ||
156 | static void channel_list_del(SpiceChannelEventInfo *info) | |
157 | { | |
158 | ChannelList *item; | |
159 | ||
160 | QTAILQ_FOREACH(item, &channel_list, link) { | |
161 | if (item->info != info) { | |
162 | continue; | |
163 | } | |
164 | QTAILQ_REMOVE(&channel_list, item, link); | |
7267c094 | 165 | g_free(item); |
cb42a870 GH |
166 | return; |
167 | } | |
168 | } | |
169 | ||
6f8c63fb GH |
170 | static void add_addr_info(QDict *dict, struct sockaddr *addr, int len) |
171 | { | |
172 | char host[NI_MAXHOST], port[NI_MAXSERV]; | |
173 | const char *family; | |
174 | ||
175 | getnameinfo(addr, len, host, sizeof(host), port, sizeof(port), | |
176 | NI_NUMERICHOST | NI_NUMERICSERV); | |
177 | family = inet_strfamily(addr->sa_family); | |
178 | ||
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)); | |
182 | } | |
183 | ||
184 | static void add_channel_info(QDict *dict, SpiceChannelEventInfo *info) | |
185 | { | |
186 | int tls = info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS; | |
187 | ||
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)); | |
192 | } | |
193 | ||
cb42a870 GH |
194 | static QList *channel_list_get(void) |
195 | { | |
196 | ChannelList *item; | |
197 | QList *list; | |
198 | QDict *dict; | |
199 | ||
200 | list = qlist_new(); | |
201 | QTAILQ_FOREACH(item, &channel_list, link) { | |
202 | dict = qdict_new(); | |
203 | add_addr_info(dict, &item->info->paddr, item->info->plen); | |
204 | add_channel_info(dict, item->info); | |
205 | qlist_append(list, dict); | |
206 | } | |
207 | return list; | |
208 | } | |
209 | ||
6f8c63fb GH |
210 | static void channel_event(int event, SpiceChannelEventInfo *info) |
211 | { | |
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, | |
216 | }; | |
217 | QDict *server, *client; | |
218 | QObject *data; | |
219 | ||
220 | client = qdict_new(); | |
221 | add_addr_info(client, &info->paddr, info->plen); | |
222 | ||
223 | server = qdict_new(); | |
224 | add_addr_info(server, &info->laddr, info->llen); | |
225 | ||
226 | if (event == SPICE_CHANNEL_EVENT_INITIALIZED) { | |
227 | qdict_put(server, "auth", qstring_from_str(auth)); | |
228 | add_channel_info(client, info); | |
cb42a870 GH |
229 | channel_list_add(info); |
230 | } | |
231 | if (event == SPICE_CHANNEL_EVENT_DISCONNECTED) { | |
232 | channel_list_del(info); | |
6f8c63fb GH |
233 | } |
234 | ||
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); | |
239 | } | |
240 | ||
241 | #else /* SPICE_INTERFACE_CORE_MINOR >= 3 */ | |
242 | ||
243 | static QList *channel_list_get(void) | |
244 | { | |
245 | return NULL; | |
246 | } | |
247 | ||
248 | #endif /* SPICE_INTERFACE_CORE_MINOR >= 3 */ | |
249 | ||
29b0040b GH |
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, | |
255 | ||
256 | .timer_add = timer_add, | |
257 | .timer_start = timer_start, | |
258 | .timer_cancel = timer_cancel, | |
259 | .timer_remove = timer_remove, | |
260 | ||
261 | .watch_add = watch_add, | |
262 | .watch_update_mask = watch_update_mask, | |
263 | .watch_remove = watch_remove, | |
6f8c63fb GH |
264 | |
265 | #if SPICE_INTERFACE_CORE_MINOR >= 3 | |
266 | .channel_event = channel_event, | |
267 | #endif | |
29b0040b GH |
268 | }; |
269 | ||
9f04e09e YH |
270 | /* config string parsing */ |
271 | ||
272 | static int name2enum(const char *string, const char *table[], int entries) | |
273 | { | |
274 | int i; | |
275 | ||
276 | if (string) { | |
277 | for (i = 0; i < entries; i++) { | |
278 | if (!table[i]) { | |
279 | continue; | |
280 | } | |
281 | if (strcmp(string, table[i]) != 0) { | |
282 | continue; | |
283 | } | |
284 | return i; | |
285 | } | |
286 | } | |
287 | return -1; | |
288 | } | |
289 | ||
290 | static int parse_name(const char *string, const char *optname, | |
291 | const char *table[], int entries) | |
292 | { | |
293 | int value = name2enum(string, table, entries); | |
294 | ||
295 | if (value != -1) { | |
296 | return value; | |
297 | } | |
298 | fprintf(stderr, "spice: invalid %s: %s\n", optname, string); | |
299 | exit(1); | |
300 | } | |
301 | ||
84a23f25 GH |
302 | static const char *stream_video_names[] = { |
303 | [ SPICE_STREAM_VIDEO_OFF ] = "off", | |
304 | [ SPICE_STREAM_VIDEO_ALL ] = "all", | |
305 | [ SPICE_STREAM_VIDEO_FILTER ] = "filter", | |
306 | }; | |
307 | #define parse_stream_video(_name) \ | |
308 | name2enum(_name, stream_video_names, ARRAY_SIZE(stream_video_names)) | |
309 | ||
9f04e09e YH |
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", | |
317 | }; | |
318 | #define parse_compression(_name) \ | |
319 | parse_name(_name, "image compression", \ | |
320 | compression_names, ARRAY_SIZE(compression_names)) | |
321 | ||
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", | |
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 |
333 | static 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 | ||
351 | void 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")); | |
8df0c7e8 AL |
375 | monitor_printf(mon, " compiled: %s\n", |
376 | qdict_get_str(server, "compiled-version")); | |
cb42a870 GH |
377 | |
378 | channels = qdict_get_qlist(server, "channels"); | |
379 | if (qlist_empty(channels)) { | |
380 | monitor_printf(mon, "Channels: none\n"); | |
381 | } else { | |
382 | qlist_iter(channels, info_spice_iter, mon); | |
383 | } | |
384 | } | |
385 | ||
386 | void do_info_spice(Monitor *mon, QObject **ret_data) | |
387 | { | |
388 | QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head); | |
389 | QDict *server; | |
390 | QList *clist; | |
391 | const char *addr; | |
392 | int port, tls_port; | |
8df0c7e8 | 393 | char version_string[20]; /* 12 = |255.255.255\0| is the max */ |
cb42a870 GH |
394 | |
395 | if (!spice_server) { | |
396 | *ret_data = qobject_from_jsonf("{ 'enabled': false }"); | |
397 | return; | |
398 | } | |
399 | ||
400 | addr = qemu_opt_get(opts, "addr"); | |
401 | port = qemu_opt_get_number(opts, "port", 0); | |
402 | tls_port = qemu_opt_get_number(opts, "tls-port", 0); | |
403 | clist = channel_list_get(); | |
404 | ||
405 | server = qdict_new(); | |
406 | qdict_put(server, "enabled", qbool_from_int(true)); | |
407 | qdict_put(server, "auth", qstring_from_str(auth)); | |
408 | qdict_put(server, "host", qstring_from_str(addr ? addr : "0.0.0.0")); | |
8df0c7e8 AL |
409 | snprintf(version_string, sizeof(version_string), "%d.%d.%d", |
410 | (SPICE_SERVER_VERSION & 0xff0000) >> 16, | |
411 | (SPICE_SERVER_VERSION & 0xff00) >> 8, | |
412 | SPICE_SERVER_VERSION & 0xff); | |
413 | qdict_put(server, "compiled-version", qstring_from_str(version_string)); | |
cb42a870 GH |
414 | if (port) { |
415 | qdict_put(server, "port", qint_from_int(port)); | |
416 | } | |
417 | if (tls_port) { | |
418 | qdict_put(server, "tls-port", qint_from_int(tls_port)); | |
419 | } | |
420 | if (clist) { | |
421 | qdict_put(server, "channels", clist); | |
422 | } | |
423 | ||
424 | *ret_data = QOBJECT(server); | |
425 | } | |
426 | ||
9e8dd451 | 427 | static void migration_state_notifier(Notifier *notifier, void *data) |
e866e239 GH |
428 | { |
429 | int state = get_migration_state(); | |
430 | ||
431 | if (state == MIG_STATE_COMPLETED) { | |
432 | #if SPICE_SERVER_VERSION >= 0x000701 /* 0.7.1 */ | |
433 | spice_server_migrate_switch(spice_server); | |
434 | #endif | |
435 | } | |
436 | } | |
437 | ||
438 | int qemu_spice_migrate_info(const char *hostname, int port, int tls_port, | |
439 | const char *subject) | |
440 | { | |
441 | return spice_server_migrate_info(spice_server, hostname, | |
442 | port, tls_port, subject); | |
443 | } | |
444 | ||
17b6dea0 GH |
445 | static int add_channel(const char *name, const char *value, void *opaque) |
446 | { | |
447 | int security = 0; | |
448 | int rc; | |
449 | ||
450 | if (strcmp(name, "tls-channel") == 0) { | |
451 | security = SPICE_CHANNEL_SECURITY_SSL; | |
452 | } | |
453 | if (strcmp(name, "plaintext-channel") == 0) { | |
454 | security = SPICE_CHANNEL_SECURITY_NONE; | |
455 | } | |
456 | if (security == 0) { | |
457 | return 0; | |
458 | } | |
459 | if (strcmp(value, "default") == 0) { | |
460 | rc = spice_server_set_channel_security(spice_server, NULL, security); | |
461 | } else { | |
462 | rc = spice_server_set_channel_security(spice_server, value, security); | |
463 | } | |
464 | if (rc != 0) { | |
465 | fprintf(stderr, "spice: failed to set channel security for %s\n", value); | |
466 | exit(1); | |
467 | } | |
468 | return 0; | |
469 | } | |
470 | ||
29b0040b GH |
471 | void qemu_spice_init(void) |
472 | { | |
473 | QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head); | |
333b0eeb | 474 | const char *password, *str, *x509_dir, *addr, |
c448e855 GH |
475 | *x509_key_password = NULL, |
476 | *x509_dh_file = NULL, | |
477 | *tls_ciphers = NULL; | |
478 | char *x509_key_file = NULL, | |
479 | *x509_cert_file = NULL, | |
480 | *x509_cacert_file = NULL; | |
f61d6960 | 481 | int port, tls_port, len, addr_flags; |
9f04e09e YH |
482 | spice_image_compression_t compression; |
483 | spice_wan_compression_t wan_compr; | |
29b0040b GH |
484 | |
485 | if (!opts) { | |
486 | return; | |
487 | } | |
488 | port = qemu_opt_get_number(opts, "port", 0); | |
c448e855 GH |
489 | tls_port = qemu_opt_get_number(opts, "tls-port", 0); |
490 | if (!port && !tls_port) { | |
df9cb669 GH |
491 | fprintf(stderr, "neither port nor tls-port specified for spice."); |
492 | exit(1); | |
493 | } | |
494 | if (port < 0 || port > 65535) { | |
495 | fprintf(stderr, "spice port is out of range"); | |
496 | exit(1); | |
497 | } | |
498 | if (tls_port < 0 || tls_port > 65535) { | |
499 | fprintf(stderr, "spice tls-port is out of range"); | |
500 | exit(1); | |
29b0040b GH |
501 | } |
502 | password = qemu_opt_get(opts, "password"); | |
503 | ||
c448e855 GH |
504 | if (tls_port) { |
505 | x509_dir = qemu_opt_get(opts, "x509-dir"); | |
506 | if (NULL == x509_dir) { | |
507 | x509_dir = "."; | |
508 | } | |
509 | len = strlen(x509_dir) + 32; | |
510 | ||
511 | str = qemu_opt_get(opts, "x509-key-file"); | |
512 | if (str) { | |
7267c094 | 513 | x509_key_file = g_strdup(str); |
c448e855 | 514 | } else { |
7267c094 | 515 | x509_key_file = g_malloc(len); |
c448e855 GH |
516 | snprintf(x509_key_file, len, "%s/%s", x509_dir, X509_SERVER_KEY_FILE); |
517 | } | |
518 | ||
519 | str = qemu_opt_get(opts, "x509-cert-file"); | |
520 | if (str) { | |
7267c094 | 521 | x509_cert_file = g_strdup(str); |
c448e855 | 522 | } else { |
7267c094 | 523 | x509_cert_file = g_malloc(len); |
c448e855 GH |
524 | snprintf(x509_cert_file, len, "%s/%s", x509_dir, X509_SERVER_CERT_FILE); |
525 | } | |
526 | ||
527 | str = qemu_opt_get(opts, "x509-cacert-file"); | |
528 | if (str) { | |
7267c094 | 529 | x509_cacert_file = g_strdup(str); |
c448e855 | 530 | } else { |
7267c094 | 531 | x509_cacert_file = g_malloc(len); |
c448e855 GH |
532 | snprintf(x509_cacert_file, len, "%s/%s", x509_dir, X509_CA_CERT_FILE); |
533 | } | |
534 | ||
535 | x509_key_password = qemu_opt_get(opts, "x509-key-password"); | |
536 | x509_dh_file = qemu_opt_get(opts, "x509-dh-file"); | |
537 | tls_ciphers = qemu_opt_get(opts, "tls-ciphers"); | |
538 | } | |
539 | ||
333b0eeb GH |
540 | addr = qemu_opt_get(opts, "addr"); |
541 | addr_flags = 0; | |
542 | if (qemu_opt_get_bool(opts, "ipv4", 0)) { | |
543 | addr_flags |= SPICE_ADDR_FLAG_IPV4_ONLY; | |
544 | } else if (qemu_opt_get_bool(opts, "ipv6", 0)) { | |
545 | addr_flags |= SPICE_ADDR_FLAG_IPV6_ONLY; | |
546 | } | |
547 | ||
29b0040b | 548 | spice_server = spice_server_new(); |
333b0eeb | 549 | spice_server_set_addr(spice_server, addr ? addr : "", addr_flags); |
c448e855 GH |
550 | if (port) { |
551 | spice_server_set_port(spice_server, port); | |
552 | } | |
553 | if (tls_port) { | |
554 | spice_server_set_tls(spice_server, tls_port, | |
555 | x509_cacert_file, | |
556 | x509_cert_file, | |
557 | x509_key_file, | |
558 | x509_key_password, | |
559 | x509_dh_file, | |
560 | tls_ciphers); | |
561 | } | |
29b0040b GH |
562 | if (password) { |
563 | spice_server_set_ticket(spice_server, password, 0, 0, 0); | |
564 | } | |
48b3ed0a MAL |
565 | if (qemu_opt_get_bool(opts, "sasl", 0)) { |
566 | #if SPICE_SERVER_VERSION >= 0x000900 /* 0.9.0 */ | |
567 | if (spice_server_set_sasl_appname(spice_server, "qemu") == -1 || | |
568 | spice_server_set_sasl(spice_server, 1) == -1) { | |
569 | fprintf(stderr, "spice: failed to enable sasl\n"); | |
570 | exit(1); | |
571 | } | |
572 | #else | |
573 | fprintf(stderr, "spice: sasl is not available (spice >= 0.9 required)\n"); | |
574 | exit(1); | |
575 | #endif | |
576 | } | |
29b0040b | 577 | if (qemu_opt_get_bool(opts, "disable-ticketing", 0)) { |
6f8c63fb | 578 | auth = "none"; |
29b0040b GH |
579 | spice_server_set_noauth(spice_server); |
580 | } | |
581 | ||
d4970b07 HG |
582 | #if SPICE_SERVER_VERSION >= 0x000801 |
583 | if (qemu_opt_get_bool(opts, "disable-copy-paste", 0)) { | |
584 | spice_server_set_agent_copypaste(spice_server, false); | |
585 | } | |
586 | #endif | |
587 | ||
9f04e09e YH |
588 | compression = SPICE_IMAGE_COMPRESS_AUTO_GLZ; |
589 | str = qemu_opt_get(opts, "image-compression"); | |
590 | if (str) { | |
591 | compression = parse_compression(str); | |
592 | } | |
593 | spice_server_set_image_compression(spice_server, compression); | |
594 | ||
595 | wan_compr = SPICE_WAN_COMPRESSION_AUTO; | |
596 | str = qemu_opt_get(opts, "jpeg-wan-compression"); | |
597 | if (str) { | |
598 | wan_compr = parse_wan_compression(str); | |
599 | } | |
600 | spice_server_set_jpeg_compression(spice_server, wan_compr); | |
601 | ||
602 | wan_compr = SPICE_WAN_COMPRESSION_AUTO; | |
603 | str = qemu_opt_get(opts, "zlib-glz-wan-compression"); | |
604 | if (str) { | |
605 | wan_compr = parse_wan_compression(str); | |
606 | } | |
607 | spice_server_set_zlib_glz_compression(spice_server, wan_compr); | |
29b0040b | 608 | |
84a23f25 GH |
609 | str = qemu_opt_get(opts, "streaming-video"); |
610 | if (str) { | |
f61d6960 | 611 | int streaming_video = parse_stream_video(str); |
84a23f25 GH |
612 | spice_server_set_streaming_video(spice_server, streaming_video); |
613 | } | |
614 | ||
615 | spice_server_set_agent_mouse | |
616 | (spice_server, qemu_opt_get_bool(opts, "agent-mouse", 1)); | |
617 | spice_server_set_playback_compression | |
618 | (spice_server, qemu_opt_get_bool(opts, "playback-compression", 1)); | |
619 | ||
17b6dea0 GH |
620 | qemu_opt_foreach(opts, add_channel, NULL, 0); |
621 | ||
fba810f1 GH |
622 | if (0 != spice_server_init(spice_server, &core_interface)) { |
623 | fprintf(stderr, "failed to initialize spice server"); | |
624 | exit(1); | |
625 | }; | |
29b0040b | 626 | using_spice = 1; |
864401c2 | 627 | |
e866e239 GH |
628 | migration_state.notify = migration_state_notifier; |
629 | add_migration_state_change_notifier(&migration_state); | |
630 | ||
864401c2 | 631 | qemu_spice_input_init(); |
3e313753 | 632 | qemu_spice_audio_init(); |
c448e855 | 633 | |
7267c094 AL |
634 | g_free(x509_key_file); |
635 | g_free(x509_cert_file); | |
636 | g_free(x509_cacert_file); | |
29b0040b GH |
637 | } |
638 | ||
639 | int qemu_spice_add_interface(SpiceBaseInstance *sin) | |
640 | { | |
a19cbfb3 GH |
641 | if (!spice_server) { |
642 | if (QTAILQ_FIRST(&qemu_spice_opts.head) != NULL) { | |
643 | fprintf(stderr, "Oops: spice configured but not active\n"); | |
644 | exit(1); | |
645 | } | |
646 | /* | |
647 | * Create a spice server instance. | |
648 | * It does *not* listen on the network. | |
649 | * It handles QXL local rendering only. | |
650 | * | |
651 | * With a command line like '-vnc :0 -vga qxl' you'll end up here. | |
652 | */ | |
653 | spice_server = spice_server_new(); | |
654 | spice_server_init(spice_server, &core_interface); | |
655 | } | |
29b0040b GH |
656 | return spice_server_add_interface(spice_server, sin); |
657 | } | |
658 | ||
7572150c GH |
659 | static int qemu_spice_set_ticket(bool fail_if_conn, bool disconnect_if_conn) |
660 | { | |
661 | time_t lifetime, now = time(NULL); | |
662 | char *passwd; | |
663 | ||
664 | if (now < auth_expires) { | |
665 | passwd = auth_passwd; | |
666 | lifetime = (auth_expires - now); | |
667 | if (lifetime > INT_MAX) { | |
668 | lifetime = INT_MAX; | |
669 | } | |
670 | } else { | |
671 | passwd = NULL; | |
672 | lifetime = 1; | |
673 | } | |
674 | return spice_server_set_ticket(spice_server, passwd, lifetime, | |
675 | fail_if_conn, disconnect_if_conn); | |
676 | } | |
677 | ||
678 | int qemu_spice_set_passwd(const char *passwd, | |
679 | bool fail_if_conn, bool disconnect_if_conn) | |
680 | { | |
681 | free(auth_passwd); | |
682 | auth_passwd = strdup(passwd); | |
683 | return qemu_spice_set_ticket(fail_if_conn, disconnect_if_conn); | |
684 | } | |
685 | ||
686 | int qemu_spice_set_pw_expire(time_t expires) | |
687 | { | |
688 | auth_expires = expires; | |
689 | return qemu_spice_set_ticket(false, false); | |
690 | } | |
691 | ||
29b0040b GH |
692 | static void spice_register_config(void) |
693 | { | |
694 | qemu_add_opts(&qemu_spice_opts); | |
695 | } | |
696 | machine_init(spice_register_config); | |
697 | ||
698 | static void spice_initialize(void) | |
699 | { | |
700 | qemu_spice_init(); | |
701 | } | |
702 | device_init(spice_initialize); |