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