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