]>
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 | 21 | #include <netdb.h> |
9c17d615 | 22 | #include "sysemu/sysemu.h" |
6f8c63fb | 23 | |
29b0040b | 24 | #include "qemu-common.h" |
28ecbaee | 25 | #include "ui/qemu-spice.h" |
1de7afc9 PB |
26 | #include "qemu/thread.h" |
27 | #include "qemu/timer.h" | |
28 | #include "qemu/queue.h" | |
c448e855 | 29 | #include "qemu-x509.h" |
1de7afc9 | 30 | #include "qemu/sockets.h" |
d1f29646 | 31 | #include "qmp-commands.h" |
7b1b5d19 PB |
32 | #include "qapi/qmp/qint.h" |
33 | #include "qapi/qmp/qbool.h" | |
34 | #include "qapi/qmp/qstring.h" | |
35 | #include "qapi/qmp/qjson.h" | |
1de7afc9 | 36 | #include "qemu/notify.h" |
caf71f86 | 37 | #include "migration/migration.h" |
e866e239 | 38 | #include "hw/hw.h" |
28ecbaee | 39 | #include "ui/spice-display.h" |
7cfadb6b | 40 | #include "qapi-event.h" |
29b0040b GH |
41 | |
42 | /* core bits */ | |
43 | ||
44 | static SpiceServer *spice_server; | |
e866e239 | 45 | static Notifier migration_state; |
6f8c63fb | 46 | static const char *auth = "spice"; |
7572150c GH |
47 | static char *auth_passwd; |
48 | static time_t auth_expires = TIME_MAX; | |
61c4efe2 | 49 | static int spice_migration_completed; |
7cc6a25f | 50 | static int spice_display_is_running; |
a76a2f72 | 51 | static int spice_have_target_host; |
29b0040b GH |
52 | int using_spice = 0; |
53 | ||
f9ab6091 | 54 | static QemuThread me; |
22b626e2 | 55 | |
29b0040b GH |
56 | struct SpiceTimer { |
57 | QEMUTimer *timer; | |
58 | QTAILQ_ENTRY(SpiceTimer) next; | |
59 | }; | |
60 | static QTAILQ_HEAD(, SpiceTimer) timers = QTAILQ_HEAD_INITIALIZER(timers); | |
61 | ||
62 | static SpiceTimer *timer_add(SpiceTimerFunc func, void *opaque) | |
63 | { | |
64 | SpiceTimer *timer; | |
65 | ||
7267c094 | 66 | timer = g_malloc0(sizeof(*timer)); |
bc72ad67 | 67 | timer->timer = timer_new_ms(QEMU_CLOCK_REALTIME, func, opaque); |
29b0040b GH |
68 | QTAILQ_INSERT_TAIL(&timers, timer, next); |
69 | return timer; | |
70 | } | |
71 | ||
72 | static void timer_start(SpiceTimer *timer, uint32_t ms) | |
73 | { | |
bc72ad67 | 74 | timer_mod(timer->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + ms); |
29b0040b GH |
75 | } |
76 | ||
77 | static void timer_cancel(SpiceTimer *timer) | |
78 | { | |
bc72ad67 | 79 | timer_del(timer->timer); |
29b0040b GH |
80 | } |
81 | ||
82 | static void timer_remove(SpiceTimer *timer) | |
83 | { | |
bc72ad67 AB |
84 | timer_del(timer->timer); |
85 | timer_free(timer->timer); | |
29b0040b | 86 | QTAILQ_REMOVE(&timers, timer, next); |
7267c094 | 87 | g_free(timer); |
29b0040b GH |
88 | } |
89 | ||
90 | struct SpiceWatch { | |
91 | int fd; | |
92 | int event_mask; | |
93 | SpiceWatchFunc func; | |
94 | void *opaque; | |
95 | QTAILQ_ENTRY(SpiceWatch) next; | |
96 | }; | |
97 | static QTAILQ_HEAD(, SpiceWatch) watches = QTAILQ_HEAD_INITIALIZER(watches); | |
98 | ||
99 | static void watch_read(void *opaque) | |
100 | { | |
101 | SpiceWatch *watch = opaque; | |
102 | watch->func(watch->fd, SPICE_WATCH_EVENT_READ, watch->opaque); | |
103 | } | |
104 | ||
105 | static void watch_write(void *opaque) | |
106 | { | |
107 | SpiceWatch *watch = opaque; | |
108 | watch->func(watch->fd, SPICE_WATCH_EVENT_WRITE, watch->opaque); | |
109 | } | |
110 | ||
111 | static void watch_update_mask(SpiceWatch *watch, int event_mask) | |
112 | { | |
113 | IOHandler *on_read = NULL; | |
114 | IOHandler *on_write = NULL; | |
115 | ||
116 | watch->event_mask = event_mask; | |
117 | if (watch->event_mask & SPICE_WATCH_EVENT_READ) { | |
118 | on_read = watch_read; | |
119 | } | |
120 | if (watch->event_mask & SPICE_WATCH_EVENT_WRITE) { | |
3d6d306c | 121 | on_write = watch_write; |
29b0040b GH |
122 | } |
123 | qemu_set_fd_handler(watch->fd, on_read, on_write, watch); | |
124 | } | |
125 | ||
126 | static SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque) | |
127 | { | |
128 | SpiceWatch *watch; | |
129 | ||
7267c094 | 130 | watch = g_malloc0(sizeof(*watch)); |
29b0040b GH |
131 | watch->fd = fd; |
132 | watch->func = func; | |
133 | watch->opaque = opaque; | |
134 | QTAILQ_INSERT_TAIL(&watches, watch, next); | |
135 | ||
136 | watch_update_mask(watch, event_mask); | |
137 | return watch; | |
138 | } | |
139 | ||
140 | static void watch_remove(SpiceWatch *watch) | |
141 | { | |
08cc67f3 | 142 | qemu_set_fd_handler(watch->fd, NULL, NULL, NULL); |
29b0040b | 143 | QTAILQ_REMOVE(&watches, watch, next); |
7267c094 | 144 | g_free(watch); |
29b0040b GH |
145 | } |
146 | ||
cb42a870 GH |
147 | typedef struct ChannelList ChannelList; |
148 | struct ChannelList { | |
149 | SpiceChannelEventInfo *info; | |
150 | QTAILQ_ENTRY(ChannelList) link; | |
151 | }; | |
152 | static QTAILQ_HEAD(, ChannelList) channel_list = QTAILQ_HEAD_INITIALIZER(channel_list); | |
153 | ||
154 | static void channel_list_add(SpiceChannelEventInfo *info) | |
155 | { | |
156 | ChannelList *item; | |
157 | ||
7267c094 | 158 | item = g_malloc0(sizeof(*item)); |
cb42a870 GH |
159 | item->info = info; |
160 | QTAILQ_INSERT_TAIL(&channel_list, item, link); | |
161 | } | |
162 | ||
163 | static void channel_list_del(SpiceChannelEventInfo *info) | |
164 | { | |
165 | ChannelList *item; | |
166 | ||
167 | QTAILQ_FOREACH(item, &channel_list, link) { | |
168 | if (item->info != info) { | |
169 | continue; | |
170 | } | |
171 | QTAILQ_REMOVE(&channel_list, item, link); | |
7267c094 | 172 | g_free(item); |
cb42a870 GH |
173 | return; |
174 | } | |
175 | } | |
176 | ||
7cfadb6b | 177 | static void add_addr_info(SpiceBasicInfo *info, struct sockaddr *addr, int len) |
6f8c63fb GH |
178 | { |
179 | char host[NI_MAXHOST], port[NI_MAXSERV]; | |
6f8c63fb GH |
180 | |
181 | getnameinfo(addr, len, host, sizeof(host), port, sizeof(port), | |
182 | NI_NUMERICHOST | NI_NUMERICSERV); | |
6f8c63fb | 183 | |
7cfadb6b WX |
184 | info->host = g_strdup(host); |
185 | info->port = g_strdup(port); | |
186 | info->family = inet_netfamily(addr->sa_family); | |
6f8c63fb GH |
187 | } |
188 | ||
7cfadb6b | 189 | static void add_channel_info(SpiceChannel *sc, SpiceChannelEventInfo *info) |
6f8c63fb GH |
190 | { |
191 | int tls = info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS; | |
192 | ||
7cfadb6b WX |
193 | sc->connection_id = info->connection_id; |
194 | sc->channel_type = info->type; | |
195 | sc->channel_id = info->id; | |
196 | sc->tls = !!tls; | |
6f8c63fb GH |
197 | } |
198 | ||
199 | static void channel_event(int event, SpiceChannelEventInfo *info) | |
200 | { | |
7cfadb6b WX |
201 | SpiceServerInfo *server = g_malloc0(sizeof(*server)); |
202 | SpiceChannel *client = g_malloc0(sizeof(*client)); | |
203 | server->base = g_malloc0(sizeof(*server->base)); | |
204 | client->base = g_malloc0(sizeof(*client->base)); | |
6f8c63fb | 205 | |
22b626e2 GH |
206 | /* |
207 | * Spice server might have called us from spice worker thread | |
208 | * context (happens on display channel disconnects). Spice should | |
209 | * not do that. It isn't that easy to fix it in spice and even | |
210 | * when it is fixed we still should cover the already released | |
211 | * spice versions. So detect that we've been called from another | |
212 | * thread and grab the iothread lock if so before calling qemu | |
213 | * functions. | |
214 | */ | |
f9ab6091 | 215 | bool need_lock = !qemu_thread_is_self(&me); |
22b626e2 GH |
216 | if (need_lock) { |
217 | qemu_mutex_lock_iothread(); | |
218 | } | |
219 | ||
faa98223 | 220 | if (info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT) { |
7cfadb6b | 221 | add_addr_info(client->base, (struct sockaddr *)&info->paddr_ext, |
faa98223 | 222 | info->plen_ext); |
7cfadb6b | 223 | add_addr_info(server->base, (struct sockaddr *)&info->laddr_ext, |
faa98223 YH |
224 | info->llen_ext); |
225 | } else { | |
339a475f CF |
226 | error_report("spice: %s, extended address is expected", |
227 | __func__); | |
faa98223 | 228 | } |
6f8c63fb | 229 | |
7cfadb6b WX |
230 | switch (event) { |
231 | case SPICE_CHANNEL_EVENT_CONNECTED: | |
232 | qapi_event_send_spice_connected(server->base, client->base, &error_abort); | |
233 | break; | |
234 | case SPICE_CHANNEL_EVENT_INITIALIZED: | |
235 | if (auth) { | |
236 | server->has_auth = true; | |
237 | server->auth = g_strdup(auth); | |
238 | } | |
6f8c63fb | 239 | add_channel_info(client, info); |
cb42a870 | 240 | channel_list_add(info); |
7cfadb6b WX |
241 | qapi_event_send_spice_initialized(server, client, &error_abort); |
242 | break; | |
243 | case SPICE_CHANNEL_EVENT_DISCONNECTED: | |
cb42a870 | 244 | channel_list_del(info); |
7cfadb6b WX |
245 | qapi_event_send_spice_disconnected(server->base, client->base, &error_abort); |
246 | break; | |
247 | default: | |
248 | break; | |
6f8c63fb GH |
249 | } |
250 | ||
22b626e2 GH |
251 | if (need_lock) { |
252 | qemu_mutex_unlock_iothread(); | |
253 | } | |
7cfadb6b WX |
254 | |
255 | qapi_free_SpiceServerInfo(server); | |
256 | qapi_free_SpiceChannel(client); | |
6f8c63fb GH |
257 | } |
258 | ||
29b0040b GH |
259 | static SpiceCoreInterface core_interface = { |
260 | .base.type = SPICE_INTERFACE_CORE, | |
261 | .base.description = "qemu core services", | |
262 | .base.major_version = SPICE_INTERFACE_CORE_MAJOR, | |
263 | .base.minor_version = SPICE_INTERFACE_CORE_MINOR, | |
264 | ||
265 | .timer_add = timer_add, | |
266 | .timer_start = timer_start, | |
267 | .timer_cancel = timer_cancel, | |
268 | .timer_remove = timer_remove, | |
269 | ||
270 | .watch_add = watch_add, | |
271 | .watch_update_mask = watch_update_mask, | |
272 | .watch_remove = watch_remove, | |
6f8c63fb | 273 | |
6f8c63fb | 274 | .channel_event = channel_event, |
29b0040b GH |
275 | }; |
276 | ||
026f773f YH |
277 | typedef struct SpiceMigration { |
278 | SpiceMigrateInstance sin; | |
279 | struct { | |
280 | MonitorCompletion *cb; | |
281 | void *opaque; | |
282 | } connect_complete; | |
283 | } SpiceMigration; | |
284 | ||
285 | static void migrate_connect_complete_cb(SpiceMigrateInstance *sin); | |
2fdd16e2 | 286 | static void migrate_end_complete_cb(SpiceMigrateInstance *sin); |
026f773f YH |
287 | |
288 | static const SpiceMigrateInterface migrate_interface = { | |
289 | .base.type = SPICE_INTERFACE_MIGRATION, | |
290 | .base.description = "migration", | |
291 | .base.major_version = SPICE_INTERFACE_MIGRATION_MAJOR, | |
292 | .base.minor_version = SPICE_INTERFACE_MIGRATION_MINOR, | |
293 | .migrate_connect_complete = migrate_connect_complete_cb, | |
2fdd16e2 | 294 | .migrate_end_complete = migrate_end_complete_cb, |
026f773f YH |
295 | }; |
296 | ||
297 | static SpiceMigration spice_migrate; | |
298 | ||
299 | static void migrate_connect_complete_cb(SpiceMigrateInstance *sin) | |
300 | { | |
301 | SpiceMigration *sm = container_of(sin, SpiceMigration, sin); | |
302 | if (sm->connect_complete.cb) { | |
303 | sm->connect_complete.cb(sm->connect_complete.opaque, NULL); | |
304 | } | |
305 | sm->connect_complete.cb = NULL; | |
306 | } | |
2fdd16e2 YH |
307 | |
308 | static void migrate_end_complete_cb(SpiceMigrateInstance *sin) | |
309 | { | |
7cfadb6b | 310 | qapi_event_send_spice_migrate_completed(&error_abort); |
61c4efe2 | 311 | spice_migration_completed = true; |
2fdd16e2 | 312 | } |
026f773f | 313 | |
9f04e09e YH |
314 | /* config string parsing */ |
315 | ||
316 | static int name2enum(const char *string, const char *table[], int entries) | |
317 | { | |
318 | int i; | |
319 | ||
320 | if (string) { | |
321 | for (i = 0; i < entries; i++) { | |
322 | if (!table[i]) { | |
323 | continue; | |
324 | } | |
325 | if (strcmp(string, table[i]) != 0) { | |
326 | continue; | |
327 | } | |
328 | return i; | |
329 | } | |
330 | } | |
331 | return -1; | |
332 | } | |
333 | ||
334 | static int parse_name(const char *string, const char *optname, | |
335 | const char *table[], int entries) | |
336 | { | |
337 | int value = name2enum(string, table, entries); | |
338 | ||
339 | if (value != -1) { | |
340 | return value; | |
341 | } | |
339a475f | 342 | error_report("spice: invalid %s: %s", optname, string); |
9f04e09e YH |
343 | exit(1); |
344 | } | |
345 | ||
84a23f25 GH |
346 | static const char *stream_video_names[] = { |
347 | [ SPICE_STREAM_VIDEO_OFF ] = "off", | |
348 | [ SPICE_STREAM_VIDEO_ALL ] = "all", | |
349 | [ SPICE_STREAM_VIDEO_FILTER ] = "filter", | |
350 | }; | |
351 | #define parse_stream_video(_name) \ | |
835cab85 CF |
352 | parse_name(_name, "stream video control", \ |
353 | stream_video_names, ARRAY_SIZE(stream_video_names)) | |
84a23f25 | 354 | |
9f04e09e YH |
355 | static const char *compression_names[] = { |
356 | [ SPICE_IMAGE_COMPRESS_OFF ] = "off", | |
357 | [ SPICE_IMAGE_COMPRESS_AUTO_GLZ ] = "auto_glz", | |
358 | [ SPICE_IMAGE_COMPRESS_AUTO_LZ ] = "auto_lz", | |
359 | [ SPICE_IMAGE_COMPRESS_QUIC ] = "quic", | |
360 | [ SPICE_IMAGE_COMPRESS_GLZ ] = "glz", | |
361 | [ SPICE_IMAGE_COMPRESS_LZ ] = "lz", | |
362 | }; | |
363 | #define parse_compression(_name) \ | |
364 | parse_name(_name, "image compression", \ | |
365 | compression_names, ARRAY_SIZE(compression_names)) | |
366 | ||
367 | static const char *wan_compression_names[] = { | |
368 | [ SPICE_WAN_COMPRESSION_AUTO ] = "auto", | |
369 | [ SPICE_WAN_COMPRESSION_NEVER ] = "never", | |
370 | [ SPICE_WAN_COMPRESSION_ALWAYS ] = "always", | |
371 | }; | |
372 | #define parse_wan_compression(_name) \ | |
373 | parse_name(_name, "wan compression", \ | |
374 | wan_compression_names, ARRAY_SIZE(wan_compression_names)) | |
375 | ||
29b0040b GH |
376 | /* functions for the rest of qemu */ |
377 | ||
d1f29646 | 378 | static SpiceChannelList *qmp_query_spice_channels(void) |
cb42a870 | 379 | { |
d1f29646 LC |
380 | SpiceChannelList *cur_item = NULL, *head = NULL; |
381 | ChannelList *item; | |
cb42a870 | 382 | |
d1f29646 LC |
383 | QTAILQ_FOREACH(item, &channel_list, link) { |
384 | SpiceChannelList *chan; | |
385 | char host[NI_MAXHOST], port[NI_MAXSERV]; | |
faa98223 YH |
386 | struct sockaddr *paddr; |
387 | socklen_t plen; | |
d1f29646 | 388 | |
26defe81 MAL |
389 | if (!(item->info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT)) { |
390 | error_report("invalid channel event"); | |
391 | return NULL; | |
392 | } | |
393 | ||
d1f29646 LC |
394 | chan = g_malloc0(sizeof(*chan)); |
395 | chan->value = g_malloc0(sizeof(*chan->value)); | |
a589569f | 396 | chan->value->base = g_malloc0(sizeof(*chan->value->base)); |
d1f29646 | 397 | |
26defe81 MAL |
398 | paddr = (struct sockaddr *)&item->info->paddr_ext; |
399 | plen = item->info->plen_ext; | |
faa98223 | 400 | getnameinfo(paddr, plen, |
d1f29646 LC |
401 | host, sizeof(host), port, sizeof(port), |
402 | NI_NUMERICHOST | NI_NUMERICSERV); | |
a589569f WX |
403 | chan->value->base->host = g_strdup(host); |
404 | chan->value->base->port = g_strdup(port); | |
405 | chan->value->base->family = inet_netfamily(paddr->sa_family); | |
d1f29646 LC |
406 | |
407 | chan->value->connection_id = item->info->connection_id; | |
408 | chan->value->channel_type = item->info->type; | |
409 | chan->value->channel_id = item->info->id; | |
410 | chan->value->tls = item->info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS; | |
411 | ||
412 | /* XXX: waiting for the qapi to support GSList */ | |
413 | if (!cur_item) { | |
414 | head = cur_item = chan; | |
415 | } else { | |
416 | cur_item->next = chan; | |
417 | cur_item = chan; | |
418 | } | |
cb42a870 GH |
419 | } |
420 | ||
d1f29646 | 421 | return head; |
cb42a870 GH |
422 | } |
423 | ||
4d454574 PB |
424 | static QemuOptsList qemu_spice_opts = { |
425 | .name = "spice", | |
426 | .head = QTAILQ_HEAD_INITIALIZER(qemu_spice_opts.head), | |
427 | .desc = { | |
428 | { | |
429 | .name = "port", | |
430 | .type = QEMU_OPT_NUMBER, | |
431 | },{ | |
432 | .name = "tls-port", | |
433 | .type = QEMU_OPT_NUMBER, | |
434 | },{ | |
435 | .name = "addr", | |
436 | .type = QEMU_OPT_STRING, | |
437 | },{ | |
438 | .name = "ipv4", | |
439 | .type = QEMU_OPT_BOOL, | |
440 | },{ | |
441 | .name = "ipv6", | |
442 | .type = QEMU_OPT_BOOL, | |
443 | },{ | |
444 | .name = "password", | |
445 | .type = QEMU_OPT_STRING, | |
446 | },{ | |
447 | .name = "disable-ticketing", | |
448 | .type = QEMU_OPT_BOOL, | |
449 | },{ | |
450 | .name = "disable-copy-paste", | |
451 | .type = QEMU_OPT_BOOL, | |
5ad24e5f HG |
452 | },{ |
453 | .name = "disable-agent-file-xfer", | |
454 | .type = QEMU_OPT_BOOL, | |
4d454574 PB |
455 | },{ |
456 | .name = "sasl", | |
457 | .type = QEMU_OPT_BOOL, | |
458 | },{ | |
459 | .name = "x509-dir", | |
460 | .type = QEMU_OPT_STRING, | |
461 | },{ | |
462 | .name = "x509-key-file", | |
463 | .type = QEMU_OPT_STRING, | |
464 | },{ | |
465 | .name = "x509-key-password", | |
466 | .type = QEMU_OPT_STRING, | |
467 | },{ | |
468 | .name = "x509-cert-file", | |
469 | .type = QEMU_OPT_STRING, | |
470 | },{ | |
471 | .name = "x509-cacert-file", | |
472 | .type = QEMU_OPT_STRING, | |
473 | },{ | |
474 | .name = "x509-dh-key-file", | |
475 | .type = QEMU_OPT_STRING, | |
476 | },{ | |
477 | .name = "tls-ciphers", | |
478 | .type = QEMU_OPT_STRING, | |
479 | },{ | |
480 | .name = "tls-channel", | |
481 | .type = QEMU_OPT_STRING, | |
482 | },{ | |
483 | .name = "plaintext-channel", | |
484 | .type = QEMU_OPT_STRING, | |
485 | },{ | |
486 | .name = "image-compression", | |
487 | .type = QEMU_OPT_STRING, | |
488 | },{ | |
489 | .name = "jpeg-wan-compression", | |
490 | .type = QEMU_OPT_STRING, | |
491 | },{ | |
492 | .name = "zlib-glz-wan-compression", | |
493 | .type = QEMU_OPT_STRING, | |
494 | },{ | |
495 | .name = "streaming-video", | |
496 | .type = QEMU_OPT_STRING, | |
497 | },{ | |
498 | .name = "agent-mouse", | |
499 | .type = QEMU_OPT_BOOL, | |
500 | },{ | |
501 | .name = "playback-compression", | |
502 | .type = QEMU_OPT_BOOL, | |
503 | }, { | |
504 | .name = "seamless-migration", | |
505 | .type = QEMU_OPT_BOOL, | |
506 | }, | |
507 | { /* end of list */ } | |
508 | }, | |
509 | }; | |
510 | ||
d1f29646 | 511 | SpiceInfo *qmp_query_spice(Error **errp) |
cb42a870 GH |
512 | { |
513 | QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head); | |
cb42a870 | 514 | int port, tls_port; |
d1f29646 LC |
515 | const char *addr; |
516 | SpiceInfo *info; | |
6735aa99 CF |
517 | unsigned int major; |
518 | unsigned int minor; | |
519 | unsigned int micro; | |
cb42a870 | 520 | |
d1f29646 LC |
521 | info = g_malloc0(sizeof(*info)); |
522 | ||
3bb781f3 | 523 | if (!spice_server || !opts) { |
d1f29646 LC |
524 | info->enabled = false; |
525 | return info; | |
cb42a870 GH |
526 | } |
527 | ||
d1f29646 | 528 | info->enabled = true; |
61c4efe2 | 529 | info->migrated = spice_migration_completed; |
d1f29646 | 530 | |
cb42a870 GH |
531 | addr = qemu_opt_get(opts, "addr"); |
532 | port = qemu_opt_get_number(opts, "port", 0); | |
533 | tls_port = qemu_opt_get_number(opts, "tls-port", 0); | |
cb42a870 | 534 | |
d1f29646 LC |
535 | info->has_auth = true; |
536 | info->auth = g_strdup(auth); | |
537 | ||
538 | info->has_host = true; | |
4f60af9a | 539 | info->host = g_strdup(addr ? addr : "*"); |
d1f29646 LC |
540 | |
541 | info->has_compiled_version = true; | |
6735aa99 CF |
542 | major = (SPICE_SERVER_VERSION & 0xff0000) >> 16; |
543 | minor = (SPICE_SERVER_VERSION & 0xff00) >> 8; | |
544 | micro = SPICE_SERVER_VERSION & 0xff; | |
545 | info->compiled_version = g_strdup_printf("%d.%d.%d", major, minor, micro); | |
d1f29646 | 546 | |
cb42a870 | 547 | if (port) { |
d1f29646 LC |
548 | info->has_port = true; |
549 | info->port = port; | |
cb42a870 GH |
550 | } |
551 | if (tls_port) { | |
d1f29646 LC |
552 | info->has_tls_port = true; |
553 | info->tls_port = tls_port; | |
cb42a870 GH |
554 | } |
555 | ||
4efee029 AL |
556 | info->mouse_mode = spice_server_is_server_mouse(spice_server) ? |
557 | SPICE_QUERY_MOUSE_MODE_SERVER : | |
558 | SPICE_QUERY_MOUSE_MODE_CLIENT; | |
67be6726 | 559 | |
d1f29646 LC |
560 | /* for compatibility with the original command */ |
561 | info->has_channels = true; | |
562 | info->channels = qmp_query_spice_channels(); | |
563 | ||
564 | return info; | |
cb42a870 GH |
565 | } |
566 | ||
9e8dd451 | 567 | static void migration_state_notifier(Notifier *notifier, void *data) |
e866e239 | 568 | { |
7073693b | 569 | MigrationState *s = data; |
e866e239 | 570 | |
a76a2f72 GH |
571 | if (!spice_have_target_host) { |
572 | return; | |
573 | } | |
574 | ||
02edd2e7 | 575 | if (migration_in_setup(s)) { |
026f773f | 576 | spice_server_migrate_start(spice_server); |
026f773f | 577 | } else if (migration_has_finished(s)) { |
026f773f | 578 | spice_server_migrate_end(spice_server, true); |
a76a2f72 | 579 | spice_have_target_host = false; |
026f773f YH |
580 | } else if (migration_has_failed(s)) { |
581 | spice_server_migrate_end(spice_server, false); | |
a76a2f72 | 582 | spice_have_target_host = false; |
e866e239 GH |
583 | } |
584 | } | |
585 | ||
586 | int qemu_spice_migrate_info(const char *hostname, int port, int tls_port, | |
edc5cb1a YH |
587 | const char *subject, |
588 | MonitorCompletion *cb, void *opaque) | |
e866e239 | 589 | { |
edc5cb1a | 590 | int ret; |
67be6726 | 591 | |
026f773f YH |
592 | spice_migrate.connect_complete.cb = cb; |
593 | spice_migrate.connect_complete.opaque = opaque; | |
594 | ret = spice_server_migrate_connect(spice_server, hostname, | |
595 | port, tls_port, subject); | |
a76a2f72 | 596 | spice_have_target_host = true; |
edc5cb1a | 597 | return ret; |
e866e239 GH |
598 | } |
599 | ||
17b6dea0 GH |
600 | static int add_channel(const char *name, const char *value, void *opaque) |
601 | { | |
602 | int security = 0; | |
603 | int rc; | |
604 | ||
605 | if (strcmp(name, "tls-channel") == 0) { | |
35c63329 CF |
606 | int *tls_port = opaque; |
607 | if (!*tls_port) { | |
608 | error_report("spice: tried to setup tls-channel" | |
609 | " without specifying a TLS port"); | |
610 | exit(1); | |
611 | } | |
17b6dea0 GH |
612 | security = SPICE_CHANNEL_SECURITY_SSL; |
613 | } | |
614 | if (strcmp(name, "plaintext-channel") == 0) { | |
615 | security = SPICE_CHANNEL_SECURITY_NONE; | |
616 | } | |
617 | if (security == 0) { | |
618 | return 0; | |
619 | } | |
620 | if (strcmp(value, "default") == 0) { | |
621 | rc = spice_server_set_channel_security(spice_server, NULL, security); | |
622 | } else { | |
623 | rc = spice_server_set_channel_security(spice_server, value, security); | |
624 | } | |
625 | if (rc != 0) { | |
339a475f | 626 | error_report("spice: failed to set channel security for %s", value); |
17b6dea0 GH |
627 | exit(1); |
628 | } | |
629 | return 0; | |
630 | } | |
631 | ||
f5bb039c YH |
632 | static void vm_change_state_handler(void *opaque, int running, |
633 | RunState state) | |
634 | { | |
f5bb039c | 635 | if (running) { |
71d388d4 | 636 | qemu_spice_display_start(); |
f5bb039c | 637 | } else { |
71d388d4 | 638 | qemu_spice_display_stop(); |
f5bb039c | 639 | } |
f5bb039c YH |
640 | } |
641 | ||
29b0040b GH |
642 | void qemu_spice_init(void) |
643 | { | |
644 | QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head); | |
333b0eeb | 645 | const char *password, *str, *x509_dir, *addr, |
c448e855 GH |
646 | *x509_key_password = NULL, |
647 | *x509_dh_file = NULL, | |
648 | *tls_ciphers = NULL; | |
649 | char *x509_key_file = NULL, | |
650 | *x509_cert_file = NULL, | |
651 | *x509_cacert_file = NULL; | |
6735aa99 | 652 | int port, tls_port, addr_flags; |
9f04e09e YH |
653 | spice_image_compression_t compression; |
654 | spice_wan_compression_t wan_compr; | |
8c957053 | 655 | bool seamless_migration; |
29b0040b | 656 | |
f9ab6091 | 657 | qemu_thread_get_self(&me); |
22b626e2 | 658 | |
ad1be899 | 659 | if (!opts) { |
29b0040b GH |
660 | return; |
661 | } | |
662 | port = qemu_opt_get_number(opts, "port", 0); | |
c448e855 GH |
663 | tls_port = qemu_opt_get_number(opts, "tls-port", 0); |
664 | if (!port && !tls_port) { | |
339a475f | 665 | error_report("neither port nor tls-port specified for spice"); |
df9cb669 GH |
666 | exit(1); |
667 | } | |
668 | if (port < 0 || port > 65535) { | |
339a475f | 669 | error_report("spice port is out of range"); |
df9cb669 GH |
670 | exit(1); |
671 | } | |
672 | if (tls_port < 0 || tls_port > 65535) { | |
339a475f | 673 | error_report("spice tls-port is out of range"); |
df9cb669 | 674 | exit(1); |
29b0040b GH |
675 | } |
676 | password = qemu_opt_get(opts, "password"); | |
677 | ||
c448e855 GH |
678 | if (tls_port) { |
679 | x509_dir = qemu_opt_get(opts, "x509-dir"); | |
680 | if (NULL == x509_dir) { | |
681 | x509_dir = "."; | |
682 | } | |
c448e855 GH |
683 | |
684 | str = qemu_opt_get(opts, "x509-key-file"); | |
685 | if (str) { | |
7267c094 | 686 | x509_key_file = g_strdup(str); |
c448e855 | 687 | } else { |
6735aa99 CF |
688 | x509_key_file = g_strdup_printf("%s/%s", x509_dir, |
689 | X509_SERVER_KEY_FILE); | |
c448e855 GH |
690 | } |
691 | ||
692 | str = qemu_opt_get(opts, "x509-cert-file"); | |
693 | if (str) { | |
7267c094 | 694 | x509_cert_file = g_strdup(str); |
c448e855 | 695 | } else { |
6735aa99 CF |
696 | x509_cert_file = g_strdup_printf("%s/%s", x509_dir, |
697 | X509_SERVER_CERT_FILE); | |
c448e855 GH |
698 | } |
699 | ||
700 | str = qemu_opt_get(opts, "x509-cacert-file"); | |
701 | if (str) { | |
7267c094 | 702 | x509_cacert_file = g_strdup(str); |
c448e855 | 703 | } else { |
6735aa99 CF |
704 | x509_cacert_file = g_strdup_printf("%s/%s", x509_dir, |
705 | X509_CA_CERT_FILE); | |
c448e855 GH |
706 | } |
707 | ||
708 | x509_key_password = qemu_opt_get(opts, "x509-key-password"); | |
9995c0b7 | 709 | x509_dh_file = qemu_opt_get(opts, "x509-dh-key-file"); |
c448e855 GH |
710 | tls_ciphers = qemu_opt_get(opts, "tls-ciphers"); |
711 | } | |
712 | ||
333b0eeb GH |
713 | addr = qemu_opt_get(opts, "addr"); |
714 | addr_flags = 0; | |
715 | if (qemu_opt_get_bool(opts, "ipv4", 0)) { | |
716 | addr_flags |= SPICE_ADDR_FLAG_IPV4_ONLY; | |
717 | } else if (qemu_opt_get_bool(opts, "ipv6", 0)) { | |
718 | addr_flags |= SPICE_ADDR_FLAG_IPV6_ONLY; | |
719 | } | |
720 | ||
29b0040b | 721 | spice_server = spice_server_new(); |
333b0eeb | 722 | spice_server_set_addr(spice_server, addr ? addr : "", addr_flags); |
c448e855 GH |
723 | if (port) { |
724 | spice_server_set_port(spice_server, port); | |
725 | } | |
726 | if (tls_port) { | |
727 | spice_server_set_tls(spice_server, tls_port, | |
728 | x509_cacert_file, | |
729 | x509_cert_file, | |
730 | x509_key_file, | |
731 | x509_key_password, | |
732 | x509_dh_file, | |
733 | tls_ciphers); | |
734 | } | |
29b0040b GH |
735 | if (password) { |
736 | spice_server_set_ticket(spice_server, password, 0, 0, 0); | |
737 | } | |
48b3ed0a | 738 | if (qemu_opt_get_bool(opts, "sasl", 0)) { |
48b3ed0a MAL |
739 | if (spice_server_set_sasl_appname(spice_server, "qemu") == -1 || |
740 | spice_server_set_sasl(spice_server, 1) == -1) { | |
339a475f | 741 | error_report("spice: failed to enable sasl"); |
48b3ed0a MAL |
742 | exit(1); |
743 | } | |
b1ea7b79 | 744 | auth = "sasl"; |
48b3ed0a | 745 | } |
29b0040b | 746 | if (qemu_opt_get_bool(opts, "disable-ticketing", 0)) { |
6f8c63fb | 747 | auth = "none"; |
29b0040b GH |
748 | spice_server_set_noauth(spice_server); |
749 | } | |
750 | ||
d4970b07 HG |
751 | if (qemu_opt_get_bool(opts, "disable-copy-paste", 0)) { |
752 | spice_server_set_agent_copypaste(spice_server, false); | |
753 | } | |
d4970b07 | 754 | |
5ad24e5f HG |
755 | if (qemu_opt_get_bool(opts, "disable-agent-file-xfer", 0)) { |
756 | #if SPICE_SERVER_VERSION >= 0x000c04 | |
757 | spice_server_set_agent_file_xfer(spice_server, false); | |
758 | #else | |
759 | error_report("this qemu build does not support the " | |
760 | "\"disable-agent-file-xfer\" option"); | |
761 | exit(1); | |
762 | #endif | |
763 | } | |
764 | ||
9f04e09e YH |
765 | compression = SPICE_IMAGE_COMPRESS_AUTO_GLZ; |
766 | str = qemu_opt_get(opts, "image-compression"); | |
767 | if (str) { | |
768 | compression = parse_compression(str); | |
769 | } | |
770 | spice_server_set_image_compression(spice_server, compression); | |
771 | ||
772 | wan_compr = SPICE_WAN_COMPRESSION_AUTO; | |
773 | str = qemu_opt_get(opts, "jpeg-wan-compression"); | |
774 | if (str) { | |
775 | wan_compr = parse_wan_compression(str); | |
776 | } | |
777 | spice_server_set_jpeg_compression(spice_server, wan_compr); | |
778 | ||
779 | wan_compr = SPICE_WAN_COMPRESSION_AUTO; | |
780 | str = qemu_opt_get(opts, "zlib-glz-wan-compression"); | |
781 | if (str) { | |
782 | wan_compr = parse_wan_compression(str); | |
783 | } | |
784 | spice_server_set_zlib_glz_compression(spice_server, wan_compr); | |
29b0040b | 785 | |
84a23f25 GH |
786 | str = qemu_opt_get(opts, "streaming-video"); |
787 | if (str) { | |
f61d6960 | 788 | int streaming_video = parse_stream_video(str); |
84a23f25 | 789 | spice_server_set_streaming_video(spice_server, streaming_video); |
f1d3e586 GH |
790 | } else { |
791 | spice_server_set_streaming_video(spice_server, SPICE_STREAM_VIDEO_OFF); | |
84a23f25 GH |
792 | } |
793 | ||
794 | spice_server_set_agent_mouse | |
795 | (spice_server, qemu_opt_get_bool(opts, "agent-mouse", 1)); | |
796 | spice_server_set_playback_compression | |
797 | (spice_server, qemu_opt_get_bool(opts, "playback-compression", 1)); | |
798 | ||
35c63329 | 799 | qemu_opt_foreach(opts, add_channel, &tls_port, 0); |
17b6dea0 | 800 | |
d0638b18 MAL |
801 | spice_server_set_name(spice_server, qemu_name); |
802 | spice_server_set_uuid(spice_server, qemu_uuid); | |
d0638b18 | 803 | |
8c957053 YH |
804 | seamless_migration = qemu_opt_get_bool(opts, "seamless-migration", 0); |
805 | spice_server_set_seamless_migration(spice_server, seamless_migration); | |
fba810f1 | 806 | if (0 != spice_server_init(spice_server, &core_interface)) { |
339a475f | 807 | error_report("failed to initialize spice server"); |
fba810f1 GH |
808 | exit(1); |
809 | }; | |
29b0040b | 810 | using_spice = 1; |
864401c2 | 811 | |
e866e239 GH |
812 | migration_state.notify = migration_state_notifier; |
813 | add_migration_state_change_notifier(&migration_state); | |
026f773f YH |
814 | spice_migrate.sin.base.sif = &migrate_interface.base; |
815 | spice_migrate.connect_complete.cb = NULL; | |
816 | qemu_spice_add_interface(&spice_migrate.sin.base); | |
e866e239 | 817 | |
864401c2 | 818 | qemu_spice_input_init(); |
3e313753 | 819 | qemu_spice_audio_init(); |
c448e855 | 820 | |
bfb82a28 | 821 | qemu_add_vm_change_state_handler(vm_change_state_handler, NULL); |
f5bb039c | 822 | |
7267c094 AL |
823 | g_free(x509_key_file); |
824 | g_free(x509_cert_file); | |
825 | g_free(x509_cacert_file); | |
afd0b409 MAL |
826 | |
827 | #if SPICE_SERVER_VERSION >= 0x000c02 | |
828 | qemu_spice_register_ports(); | |
829 | #endif | |
29b0040b GH |
830 | } |
831 | ||
832 | int qemu_spice_add_interface(SpiceBaseInstance *sin) | |
833 | { | |
a19cbfb3 GH |
834 | if (!spice_server) { |
835 | if (QTAILQ_FIRST(&qemu_spice_opts.head) != NULL) { | |
339a475f | 836 | error_report("Oops: spice configured but not active"); |
a19cbfb3 GH |
837 | exit(1); |
838 | } | |
839 | /* | |
840 | * Create a spice server instance. | |
841 | * It does *not* listen on the network. | |
842 | * It handles QXL local rendering only. | |
843 | * | |
844 | * With a command line like '-vnc :0 -vga qxl' you'll end up here. | |
845 | */ | |
846 | spice_server = spice_server_new(); | |
764eb39d | 847 | spice_server_set_sasl_appname(spice_server, "qemu"); |
a19cbfb3 | 848 | spice_server_init(spice_server, &core_interface); |
bfb82a28 | 849 | qemu_add_vm_change_state_handler(vm_change_state_handler, NULL); |
a19cbfb3 | 850 | } |
71d388d4 | 851 | |
9fa03286 GH |
852 | return spice_server_add_interface(spice_server, sin); |
853 | } | |
854 | ||
855 | static GSList *spice_consoles; | |
856 | static int display_id; | |
857 | ||
858 | bool qemu_spice_have_display_interface(QemuConsole *con) | |
859 | { | |
860 | if (g_slist_find(spice_consoles, con)) { | |
861 | return true; | |
58ae52a8 | 862 | } |
9fa03286 GH |
863 | return false; |
864 | } | |
58ae52a8 | 865 | |
9fa03286 GH |
866 | int qemu_spice_add_display_interface(QXLInstance *qxlin, QemuConsole *con) |
867 | { | |
868 | if (g_slist_find(spice_consoles, con)) { | |
869 | return -1; | |
870 | } | |
871 | qxlin->id = display_id++; | |
872 | spice_consoles = g_slist_append(spice_consoles, con); | |
873 | return qemu_spice_add_interface(&qxlin->base); | |
29b0040b GH |
874 | } |
875 | ||
7572150c GH |
876 | static int qemu_spice_set_ticket(bool fail_if_conn, bool disconnect_if_conn) |
877 | { | |
878 | time_t lifetime, now = time(NULL); | |
879 | char *passwd; | |
880 | ||
881 | if (now < auth_expires) { | |
882 | passwd = auth_passwd; | |
883 | lifetime = (auth_expires - now); | |
884 | if (lifetime > INT_MAX) { | |
885 | lifetime = INT_MAX; | |
886 | } | |
887 | } else { | |
888 | passwd = NULL; | |
889 | lifetime = 1; | |
890 | } | |
891 | return spice_server_set_ticket(spice_server, passwd, lifetime, | |
892 | fail_if_conn, disconnect_if_conn); | |
893 | } | |
894 | ||
895 | int qemu_spice_set_passwd(const char *passwd, | |
896 | bool fail_if_conn, bool disconnect_if_conn) | |
897 | { | |
b1ea7b79 GH |
898 | if (strcmp(auth, "spice") != 0) { |
899 | return -1; | |
900 | } | |
901 | ||
fd3bea3f MA |
902 | g_free(auth_passwd); |
903 | auth_passwd = g_strdup(passwd); | |
7572150c GH |
904 | return qemu_spice_set_ticket(fail_if_conn, disconnect_if_conn); |
905 | } | |
906 | ||
907 | int qemu_spice_set_pw_expire(time_t expires) | |
908 | { | |
909 | auth_expires = expires; | |
910 | return qemu_spice_set_ticket(false, false); | |
911 | } | |
912 | ||
f1f5f407 DB |
913 | int qemu_spice_display_add_client(int csock, int skipauth, int tls) |
914 | { | |
f1f5f407 DB |
915 | if (tls) { |
916 | return spice_server_add_ssl_client(spice_server, csock, skipauth); | |
917 | } else { | |
918 | return spice_server_add_client(spice_server, csock, skipauth); | |
919 | } | |
f1f5f407 DB |
920 | } |
921 | ||
7cc6a25f GH |
922 | void qemu_spice_display_start(void) |
923 | { | |
924 | spice_display_is_running = true; | |
b50f3e42 | 925 | spice_server_vm_start(spice_server); |
7cc6a25f GH |
926 | } |
927 | ||
928 | void qemu_spice_display_stop(void) | |
929 | { | |
b50f3e42 | 930 | spice_server_vm_stop(spice_server); |
7cc6a25f GH |
931 | spice_display_is_running = false; |
932 | } | |
933 | ||
934 | int qemu_spice_display_is_running(SimpleSpiceDisplay *ssd) | |
935 | { | |
936 | return spice_display_is_running; | |
937 | } | |
938 | ||
29b0040b GH |
939 | static void spice_register_config(void) |
940 | { | |
941 | qemu_add_opts(&qemu_spice_opts); | |
942 | } | |
943 | machine_init(spice_register_config); |