]>
Commit | Line | Data |
---|---|---|
29b0040b GH |
1 | /* |
2 | * Copyright (C) 2010 Red Hat, Inc. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License as | |
6 | * published by the Free Software Foundation; either version 2 or | |
7 | * (at your option) version 3 of the License. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
17 | ||
18 | #include <spice.h> | |
19 | #include <spice-experimental.h> | |
20 | ||
6f8c63fb GH |
21 | #include <netdb.h> |
22 | ||
29b0040b GH |
23 | #include "qemu-common.h" |
24 | #include "qemu-spice.h" | |
f9ab6091 | 25 | #include "qemu-thread.h" |
29b0040b GH |
26 | #include "qemu-timer.h" |
27 | #include "qemu-queue.h" | |
c448e855 | 28 | #include "qemu-x509.h" |
6f8c63fb | 29 | #include "qemu_socket.h" |
d1f29646 | 30 | #include "qmp-commands.h" |
6f8c63fb GH |
31 | #include "qint.h" |
32 | #include "qbool.h" | |
33 | #include "qstring.h" | |
34 | #include "qjson.h" | |
e866e239 GH |
35 | #include "notify.h" |
36 | #include "migration.h" | |
29b0040b | 37 | #include "monitor.h" |
e866e239 | 38 | #include "hw/hw.h" |
29b0040b GH |
39 | |
40 | /* core bits */ | |
41 | ||
42 | static SpiceServer *spice_server; | |
e866e239 | 43 | static Notifier migration_state; |
6f8c63fb | 44 | static const char *auth = "spice"; |
7572150c GH |
45 | static char *auth_passwd; |
46 | static time_t auth_expires = TIME_MAX; | |
29b0040b GH |
47 | int using_spice = 0; |
48 | ||
f9ab6091 | 49 | static QemuThread me; |
22b626e2 | 50 | |
29b0040b GH |
51 | struct SpiceTimer { |
52 | QEMUTimer *timer; | |
53 | QTAILQ_ENTRY(SpiceTimer) next; | |
54 | }; | |
55 | static QTAILQ_HEAD(, SpiceTimer) timers = QTAILQ_HEAD_INITIALIZER(timers); | |
56 | ||
57 | static SpiceTimer *timer_add(SpiceTimerFunc func, void *opaque) | |
58 | { | |
59 | SpiceTimer *timer; | |
60 | ||
7267c094 | 61 | timer = g_malloc0(sizeof(*timer)); |
7bd427d8 | 62 | timer->timer = qemu_new_timer_ms(rt_clock, func, opaque); |
29b0040b GH |
63 | QTAILQ_INSERT_TAIL(&timers, timer, next); |
64 | return timer; | |
65 | } | |
66 | ||
67 | static void timer_start(SpiceTimer *timer, uint32_t ms) | |
68 | { | |
7bd427d8 | 69 | qemu_mod_timer(timer->timer, qemu_get_clock_ms(rt_clock) + ms); |
29b0040b GH |
70 | } |
71 | ||
72 | static void timer_cancel(SpiceTimer *timer) | |
73 | { | |
74 | qemu_del_timer(timer->timer); | |
75 | } | |
76 | ||
77 | static void timer_remove(SpiceTimer *timer) | |
78 | { | |
79 | qemu_del_timer(timer->timer); | |
80 | qemu_free_timer(timer->timer); | |
81 | QTAILQ_REMOVE(&timers, timer, next); | |
7267c094 | 82 | g_free(timer); |
29b0040b GH |
83 | } |
84 | ||
85 | struct SpiceWatch { | |
86 | int fd; | |
87 | int event_mask; | |
88 | SpiceWatchFunc func; | |
89 | void *opaque; | |
90 | QTAILQ_ENTRY(SpiceWatch) next; | |
91 | }; | |
92 | static QTAILQ_HEAD(, SpiceWatch) watches = QTAILQ_HEAD_INITIALIZER(watches); | |
93 | ||
94 | static void watch_read(void *opaque) | |
95 | { | |
96 | SpiceWatch *watch = opaque; | |
97 | watch->func(watch->fd, SPICE_WATCH_EVENT_READ, watch->opaque); | |
98 | } | |
99 | ||
100 | static void watch_write(void *opaque) | |
101 | { | |
102 | SpiceWatch *watch = opaque; | |
103 | watch->func(watch->fd, SPICE_WATCH_EVENT_WRITE, watch->opaque); | |
104 | } | |
105 | ||
106 | static void watch_update_mask(SpiceWatch *watch, int event_mask) | |
107 | { | |
108 | IOHandler *on_read = NULL; | |
109 | IOHandler *on_write = NULL; | |
110 | ||
111 | watch->event_mask = event_mask; | |
112 | if (watch->event_mask & SPICE_WATCH_EVENT_READ) { | |
113 | on_read = watch_read; | |
114 | } | |
115 | if (watch->event_mask & SPICE_WATCH_EVENT_WRITE) { | |
3d6d306c | 116 | on_write = watch_write; |
29b0040b GH |
117 | } |
118 | qemu_set_fd_handler(watch->fd, on_read, on_write, watch); | |
119 | } | |
120 | ||
121 | static SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque) | |
122 | { | |
123 | SpiceWatch *watch; | |
124 | ||
7267c094 | 125 | watch = g_malloc0(sizeof(*watch)); |
29b0040b GH |
126 | watch->fd = fd; |
127 | watch->func = func; | |
128 | watch->opaque = opaque; | |
129 | QTAILQ_INSERT_TAIL(&watches, watch, next); | |
130 | ||
131 | watch_update_mask(watch, event_mask); | |
132 | return watch; | |
133 | } | |
134 | ||
135 | static void watch_remove(SpiceWatch *watch) | |
136 | { | |
08cc67f3 | 137 | qemu_set_fd_handler(watch->fd, NULL, NULL, NULL); |
29b0040b | 138 | QTAILQ_REMOVE(&watches, watch, next); |
7267c094 | 139 | g_free(watch); |
29b0040b GH |
140 | } |
141 | ||
6f8c63fb GH |
142 | #if SPICE_INTERFACE_CORE_MINOR >= 3 |
143 | ||
cb42a870 GH |
144 | typedef struct ChannelList ChannelList; |
145 | struct ChannelList { | |
146 | SpiceChannelEventInfo *info; | |
147 | QTAILQ_ENTRY(ChannelList) link; | |
148 | }; | |
149 | static QTAILQ_HEAD(, ChannelList) channel_list = QTAILQ_HEAD_INITIALIZER(channel_list); | |
150 | ||
151 | static void channel_list_add(SpiceChannelEventInfo *info) | |
152 | { | |
153 | ChannelList *item; | |
154 | ||
7267c094 | 155 | item = g_malloc0(sizeof(*item)); |
cb42a870 GH |
156 | item->info = info; |
157 | QTAILQ_INSERT_TAIL(&channel_list, item, link); | |
158 | } | |
159 | ||
160 | static void channel_list_del(SpiceChannelEventInfo *info) | |
161 | { | |
162 | ChannelList *item; | |
163 | ||
164 | QTAILQ_FOREACH(item, &channel_list, link) { | |
165 | if (item->info != info) { | |
166 | continue; | |
167 | } | |
168 | QTAILQ_REMOVE(&channel_list, item, link); | |
7267c094 | 169 | g_free(item); |
cb42a870 GH |
170 | return; |
171 | } | |
172 | } | |
173 | ||
6f8c63fb GH |
174 | static void add_addr_info(QDict *dict, struct sockaddr *addr, int len) |
175 | { | |
176 | char host[NI_MAXHOST], port[NI_MAXSERV]; | |
177 | const char *family; | |
178 | ||
179 | getnameinfo(addr, len, host, sizeof(host), port, sizeof(port), | |
180 | NI_NUMERICHOST | NI_NUMERICSERV); | |
181 | family = inet_strfamily(addr->sa_family); | |
182 | ||
183 | qdict_put(dict, "host", qstring_from_str(host)); | |
184 | qdict_put(dict, "port", qstring_from_str(port)); | |
185 | qdict_put(dict, "family", qstring_from_str(family)); | |
186 | } | |
187 | ||
188 | static void add_channel_info(QDict *dict, SpiceChannelEventInfo *info) | |
189 | { | |
190 | int tls = info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS; | |
191 | ||
192 | qdict_put(dict, "connection-id", qint_from_int(info->connection_id)); | |
193 | qdict_put(dict, "channel-type", qint_from_int(info->type)); | |
194 | qdict_put(dict, "channel-id", qint_from_int(info->id)); | |
195 | qdict_put(dict, "tls", qbool_from_int(tls)); | |
196 | } | |
197 | ||
198 | static void channel_event(int event, SpiceChannelEventInfo *info) | |
199 | { | |
200 | static const int qevent[] = { | |
201 | [ SPICE_CHANNEL_EVENT_CONNECTED ] = QEVENT_SPICE_CONNECTED, | |
202 | [ SPICE_CHANNEL_EVENT_INITIALIZED ] = QEVENT_SPICE_INITIALIZED, | |
203 | [ SPICE_CHANNEL_EVENT_DISCONNECTED ] = QEVENT_SPICE_DISCONNECTED, | |
204 | }; | |
205 | QDict *server, *client; | |
206 | QObject *data; | |
207 | ||
22b626e2 GH |
208 | /* |
209 | * Spice server might have called us from spice worker thread | |
210 | * context (happens on display channel disconnects). Spice should | |
211 | * not do that. It isn't that easy to fix it in spice and even | |
212 | * when it is fixed we still should cover the already released | |
213 | * spice versions. So detect that we've been called from another | |
214 | * thread and grab the iothread lock if so before calling qemu | |
215 | * functions. | |
216 | */ | |
f9ab6091 | 217 | bool need_lock = !qemu_thread_is_self(&me); |
22b626e2 GH |
218 | if (need_lock) { |
219 | qemu_mutex_lock_iothread(); | |
220 | } | |
221 | ||
6f8c63fb | 222 | client = qdict_new(); |
6f8c63fb | 223 | server = qdict_new(); |
faa98223 YH |
224 | |
225 | #ifdef SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT | |
226 | if (info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT) { | |
227 | add_addr_info(client, (struct sockaddr *)&info->paddr_ext, | |
228 | info->plen_ext); | |
229 | add_addr_info(server, (struct sockaddr *)&info->laddr_ext, | |
230 | info->llen_ext); | |
231 | } else { | |
232 | fprintf(stderr, "spice: %s, extended address is expected\n", | |
233 | __func__); | |
234 | #endif | |
235 | add_addr_info(client, &info->paddr, info->plen); | |
236 | add_addr_info(server, &info->laddr, info->llen); | |
237 | #ifdef SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT | |
238 | } | |
239 | #endif | |
6f8c63fb GH |
240 | |
241 | if (event == SPICE_CHANNEL_EVENT_INITIALIZED) { | |
242 | qdict_put(server, "auth", qstring_from_str(auth)); | |
243 | add_channel_info(client, info); | |
cb42a870 GH |
244 | channel_list_add(info); |
245 | } | |
246 | if (event == SPICE_CHANNEL_EVENT_DISCONNECTED) { | |
247 | channel_list_del(info); | |
6f8c63fb GH |
248 | } |
249 | ||
250 | data = qobject_from_jsonf("{ 'client': %p, 'server': %p }", | |
251 | QOBJECT(client), QOBJECT(server)); | |
252 | monitor_protocol_event(qevent[event], data); | |
253 | qobject_decref(data); | |
22b626e2 GH |
254 | |
255 | if (need_lock) { | |
256 | qemu_mutex_unlock_iothread(); | |
257 | } | |
6f8c63fb GH |
258 | } |
259 | ||
260 | #else /* SPICE_INTERFACE_CORE_MINOR >= 3 */ | |
261 | ||
262 | static QList *channel_list_get(void) | |
263 | { | |
264 | return NULL; | |
265 | } | |
266 | ||
267 | #endif /* SPICE_INTERFACE_CORE_MINOR >= 3 */ | |
268 | ||
29b0040b GH |
269 | static SpiceCoreInterface core_interface = { |
270 | .base.type = SPICE_INTERFACE_CORE, | |
271 | .base.description = "qemu core services", | |
272 | .base.major_version = SPICE_INTERFACE_CORE_MAJOR, | |
273 | .base.minor_version = SPICE_INTERFACE_CORE_MINOR, | |
274 | ||
275 | .timer_add = timer_add, | |
276 | .timer_start = timer_start, | |
277 | .timer_cancel = timer_cancel, | |
278 | .timer_remove = timer_remove, | |
279 | ||
280 | .watch_add = watch_add, | |
281 | .watch_update_mask = watch_update_mask, | |
282 | .watch_remove = watch_remove, | |
6f8c63fb GH |
283 | |
284 | #if SPICE_INTERFACE_CORE_MINOR >= 3 | |
285 | .channel_event = channel_event, | |
286 | #endif | |
29b0040b GH |
287 | }; |
288 | ||
026f773f YH |
289 | #ifdef SPICE_INTERFACE_MIGRATION |
290 | typedef struct SpiceMigration { | |
291 | SpiceMigrateInstance sin; | |
292 | struct { | |
293 | MonitorCompletion *cb; | |
294 | void *opaque; | |
295 | } connect_complete; | |
296 | } SpiceMigration; | |
297 | ||
298 | static void migrate_connect_complete_cb(SpiceMigrateInstance *sin); | |
299 | ||
300 | static const SpiceMigrateInterface migrate_interface = { | |
301 | .base.type = SPICE_INTERFACE_MIGRATION, | |
302 | .base.description = "migration", | |
303 | .base.major_version = SPICE_INTERFACE_MIGRATION_MAJOR, | |
304 | .base.minor_version = SPICE_INTERFACE_MIGRATION_MINOR, | |
305 | .migrate_connect_complete = migrate_connect_complete_cb, | |
306 | .migrate_end_complete = NULL, | |
307 | }; | |
308 | ||
309 | static SpiceMigration spice_migrate; | |
310 | ||
311 | static void migrate_connect_complete_cb(SpiceMigrateInstance *sin) | |
312 | { | |
313 | SpiceMigration *sm = container_of(sin, SpiceMigration, sin); | |
314 | if (sm->connect_complete.cb) { | |
315 | sm->connect_complete.cb(sm->connect_complete.opaque, NULL); | |
316 | } | |
317 | sm->connect_complete.cb = NULL; | |
318 | } | |
319 | #endif | |
320 | ||
9f04e09e YH |
321 | /* config string parsing */ |
322 | ||
323 | static int name2enum(const char *string, const char *table[], int entries) | |
324 | { | |
325 | int i; | |
326 | ||
327 | if (string) { | |
328 | for (i = 0; i < entries; i++) { | |
329 | if (!table[i]) { | |
330 | continue; | |
331 | } | |
332 | if (strcmp(string, table[i]) != 0) { | |
333 | continue; | |
334 | } | |
335 | return i; | |
336 | } | |
337 | } | |
338 | return -1; | |
339 | } | |
340 | ||
341 | static int parse_name(const char *string, const char *optname, | |
342 | const char *table[], int entries) | |
343 | { | |
344 | int value = name2enum(string, table, entries); | |
345 | ||
346 | if (value != -1) { | |
347 | return value; | |
348 | } | |
349 | fprintf(stderr, "spice: invalid %s: %s\n", optname, string); | |
350 | exit(1); | |
351 | } | |
352 | ||
84a23f25 GH |
353 | static const char *stream_video_names[] = { |
354 | [ SPICE_STREAM_VIDEO_OFF ] = "off", | |
355 | [ SPICE_STREAM_VIDEO_ALL ] = "all", | |
356 | [ SPICE_STREAM_VIDEO_FILTER ] = "filter", | |
357 | }; | |
358 | #define parse_stream_video(_name) \ | |
359 | name2enum(_name, stream_video_names, ARRAY_SIZE(stream_video_names)) | |
360 | ||
9f04e09e YH |
361 | static const char *compression_names[] = { |
362 | [ SPICE_IMAGE_COMPRESS_OFF ] = "off", | |
363 | [ SPICE_IMAGE_COMPRESS_AUTO_GLZ ] = "auto_glz", | |
364 | [ SPICE_IMAGE_COMPRESS_AUTO_LZ ] = "auto_lz", | |
365 | [ SPICE_IMAGE_COMPRESS_QUIC ] = "quic", | |
366 | [ SPICE_IMAGE_COMPRESS_GLZ ] = "glz", | |
367 | [ SPICE_IMAGE_COMPRESS_LZ ] = "lz", | |
368 | }; | |
369 | #define parse_compression(_name) \ | |
370 | parse_name(_name, "image compression", \ | |
371 | compression_names, ARRAY_SIZE(compression_names)) | |
372 | ||
373 | static const char *wan_compression_names[] = { | |
374 | [ SPICE_WAN_COMPRESSION_AUTO ] = "auto", | |
375 | [ SPICE_WAN_COMPRESSION_NEVER ] = "never", | |
376 | [ SPICE_WAN_COMPRESSION_ALWAYS ] = "always", | |
377 | }; | |
378 | #define parse_wan_compression(_name) \ | |
379 | parse_name(_name, "wan compression", \ | |
380 | wan_compression_names, ARRAY_SIZE(wan_compression_names)) | |
381 | ||
29b0040b GH |
382 | /* functions for the rest of qemu */ |
383 | ||
d1f29646 | 384 | static SpiceChannelList *qmp_query_spice_channels(void) |
cb42a870 | 385 | { |
d1f29646 LC |
386 | SpiceChannelList *cur_item = NULL, *head = NULL; |
387 | ChannelList *item; | |
cb42a870 | 388 | |
d1f29646 LC |
389 | QTAILQ_FOREACH(item, &channel_list, link) { |
390 | SpiceChannelList *chan; | |
391 | char host[NI_MAXHOST], port[NI_MAXSERV]; | |
faa98223 YH |
392 | struct sockaddr *paddr; |
393 | socklen_t plen; | |
d1f29646 LC |
394 | |
395 | chan = g_malloc0(sizeof(*chan)); | |
396 | chan->value = g_malloc0(sizeof(*chan->value)); | |
397 | ||
faa98223 YH |
398 | #ifdef SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT |
399 | if (item->info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT) { | |
400 | paddr = (struct sockaddr *)&item->info->paddr_ext; | |
401 | plen = item->info->plen_ext; | |
402 | } else { | |
403 | #endif | |
404 | paddr = &item->info->paddr; | |
405 | plen = item->info->plen; | |
406 | #ifdef SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT | |
407 | } | |
408 | #endif | |
409 | ||
410 | getnameinfo(paddr, plen, | |
d1f29646 LC |
411 | host, sizeof(host), port, sizeof(port), |
412 | NI_NUMERICHOST | NI_NUMERICSERV); | |
413 | chan->value->host = g_strdup(host); | |
414 | chan->value->port = g_strdup(port); | |
faa98223 | 415 | chan->value->family = g_strdup(inet_strfamily(paddr->sa_family)); |
d1f29646 LC |
416 | |
417 | chan->value->connection_id = item->info->connection_id; | |
418 | chan->value->channel_type = item->info->type; | |
419 | chan->value->channel_id = item->info->id; | |
420 | chan->value->tls = item->info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS; | |
421 | ||
422 | /* XXX: waiting for the qapi to support GSList */ | |
423 | if (!cur_item) { | |
424 | head = cur_item = chan; | |
425 | } else { | |
426 | cur_item->next = chan; | |
427 | cur_item = chan; | |
428 | } | |
cb42a870 GH |
429 | } |
430 | ||
d1f29646 | 431 | return head; |
cb42a870 GH |
432 | } |
433 | ||
d1f29646 | 434 | SpiceInfo *qmp_query_spice(Error **errp) |
cb42a870 GH |
435 | { |
436 | QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head); | |
cb42a870 | 437 | int port, tls_port; |
d1f29646 LC |
438 | const char *addr; |
439 | SpiceInfo *info; | |
8df0c7e8 | 440 | char version_string[20]; /* 12 = |255.255.255\0| is the max */ |
cb42a870 | 441 | |
d1f29646 LC |
442 | info = g_malloc0(sizeof(*info)); |
443 | ||
3bb781f3 | 444 | if (!spice_server || !opts) { |
d1f29646 LC |
445 | info->enabled = false; |
446 | return info; | |
cb42a870 GH |
447 | } |
448 | ||
d1f29646 LC |
449 | info->enabled = true; |
450 | ||
cb42a870 GH |
451 | addr = qemu_opt_get(opts, "addr"); |
452 | port = qemu_opt_get_number(opts, "port", 0); | |
453 | tls_port = qemu_opt_get_number(opts, "tls-port", 0); | |
cb42a870 | 454 | |
d1f29646 LC |
455 | info->has_auth = true; |
456 | info->auth = g_strdup(auth); | |
457 | ||
458 | info->has_host = true; | |
459 | info->host = g_strdup(addr ? addr : "0.0.0.0"); | |
460 | ||
461 | info->has_compiled_version = true; | |
8df0c7e8 AL |
462 | snprintf(version_string, sizeof(version_string), "%d.%d.%d", |
463 | (SPICE_SERVER_VERSION & 0xff0000) >> 16, | |
464 | (SPICE_SERVER_VERSION & 0xff00) >> 8, | |
465 | SPICE_SERVER_VERSION & 0xff); | |
d1f29646 LC |
466 | info->compiled_version = g_strdup(version_string); |
467 | ||
cb42a870 | 468 | if (port) { |
d1f29646 LC |
469 | info->has_port = true; |
470 | info->port = port; | |
cb42a870 GH |
471 | } |
472 | if (tls_port) { | |
d1f29646 LC |
473 | info->has_tls_port = true; |
474 | info->tls_port = tls_port; | |
cb42a870 GH |
475 | } |
476 | ||
d1f29646 LC |
477 | /* for compatibility with the original command */ |
478 | info->has_channels = true; | |
479 | info->channels = qmp_query_spice_channels(); | |
480 | ||
481 | return info; | |
cb42a870 GH |
482 | } |
483 | ||
9e8dd451 | 484 | static void migration_state_notifier(Notifier *notifier, void *data) |
e866e239 | 485 | { |
7073693b | 486 | MigrationState *s = data; |
e866e239 | 487 | |
026f773f YH |
488 | if (migration_is_active(s)) { |
489 | #ifdef SPICE_INTERFACE_MIGRATION | |
490 | spice_server_migrate_start(spice_server); | |
491 | #endif | |
492 | } else if (migration_has_finished(s)) { | |
e866e239 | 493 | #if SPICE_SERVER_VERSION >= 0x000701 /* 0.7.1 */ |
026f773f | 494 | #ifndef SPICE_INTERFACE_MIGRATION |
e866e239 | 495 | spice_server_migrate_switch(spice_server); |
026f773f YH |
496 | #else |
497 | spice_server_migrate_end(spice_server, true); | |
498 | } else if (migration_has_failed(s)) { | |
499 | spice_server_migrate_end(spice_server, false); | |
500 | #endif | |
e866e239 GH |
501 | #endif |
502 | } | |
503 | } | |
504 | ||
505 | int qemu_spice_migrate_info(const char *hostname, int port, int tls_port, | |
edc5cb1a YH |
506 | const char *subject, |
507 | MonitorCompletion *cb, void *opaque) | |
e866e239 | 508 | { |
edc5cb1a | 509 | int ret; |
026f773f YH |
510 | #ifdef SPICE_INTERFACE_MIGRATION |
511 | spice_migrate.connect_complete.cb = cb; | |
512 | spice_migrate.connect_complete.opaque = opaque; | |
513 | ret = spice_server_migrate_connect(spice_server, hostname, | |
514 | port, tls_port, subject); | |
515 | #else | |
edc5cb1a YH |
516 | ret = spice_server_migrate_info(spice_server, hostname, |
517 | port, tls_port, subject); | |
518 | cb(opaque, NULL); | |
026f773f | 519 | #endif |
edc5cb1a | 520 | return ret; |
e866e239 GH |
521 | } |
522 | ||
17b6dea0 GH |
523 | static int add_channel(const char *name, const char *value, void *opaque) |
524 | { | |
525 | int security = 0; | |
526 | int rc; | |
527 | ||
528 | if (strcmp(name, "tls-channel") == 0) { | |
529 | security = SPICE_CHANNEL_SECURITY_SSL; | |
530 | } | |
531 | if (strcmp(name, "plaintext-channel") == 0) { | |
532 | security = SPICE_CHANNEL_SECURITY_NONE; | |
533 | } | |
534 | if (security == 0) { | |
535 | return 0; | |
536 | } | |
537 | if (strcmp(value, "default") == 0) { | |
538 | rc = spice_server_set_channel_security(spice_server, NULL, security); | |
539 | } else { | |
540 | rc = spice_server_set_channel_security(spice_server, value, security); | |
541 | } | |
542 | if (rc != 0) { | |
543 | fprintf(stderr, "spice: failed to set channel security for %s\n", value); | |
544 | exit(1); | |
545 | } | |
546 | return 0; | |
547 | } | |
548 | ||
29b0040b GH |
549 | void qemu_spice_init(void) |
550 | { | |
551 | QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head); | |
333b0eeb | 552 | const char *password, *str, *x509_dir, *addr, |
c448e855 GH |
553 | *x509_key_password = NULL, |
554 | *x509_dh_file = NULL, | |
555 | *tls_ciphers = NULL; | |
556 | char *x509_key_file = NULL, | |
557 | *x509_cert_file = NULL, | |
558 | *x509_cacert_file = NULL; | |
f61d6960 | 559 | int port, tls_port, len, addr_flags; |
9f04e09e YH |
560 | spice_image_compression_t compression; |
561 | spice_wan_compression_t wan_compr; | |
29b0040b | 562 | |
f9ab6091 | 563 | qemu_thread_get_self(&me); |
22b626e2 GH |
564 | |
565 | if (!opts) { | |
29b0040b GH |
566 | return; |
567 | } | |
568 | port = qemu_opt_get_number(opts, "port", 0); | |
c448e855 GH |
569 | tls_port = qemu_opt_get_number(opts, "tls-port", 0); |
570 | if (!port && !tls_port) { | |
df9cb669 GH |
571 | fprintf(stderr, "neither port nor tls-port specified for spice."); |
572 | exit(1); | |
573 | } | |
574 | if (port < 0 || port > 65535) { | |
575 | fprintf(stderr, "spice port is out of range"); | |
576 | exit(1); | |
577 | } | |
578 | if (tls_port < 0 || tls_port > 65535) { | |
579 | fprintf(stderr, "spice tls-port is out of range"); | |
580 | exit(1); | |
29b0040b GH |
581 | } |
582 | password = qemu_opt_get(opts, "password"); | |
583 | ||
c448e855 GH |
584 | if (tls_port) { |
585 | x509_dir = qemu_opt_get(opts, "x509-dir"); | |
586 | if (NULL == x509_dir) { | |
587 | x509_dir = "."; | |
588 | } | |
589 | len = strlen(x509_dir) + 32; | |
590 | ||
591 | str = qemu_opt_get(opts, "x509-key-file"); | |
592 | if (str) { | |
7267c094 | 593 | x509_key_file = g_strdup(str); |
c448e855 | 594 | } else { |
7267c094 | 595 | x509_key_file = g_malloc(len); |
c448e855 GH |
596 | snprintf(x509_key_file, len, "%s/%s", x509_dir, X509_SERVER_KEY_FILE); |
597 | } | |
598 | ||
599 | str = qemu_opt_get(opts, "x509-cert-file"); | |
600 | if (str) { | |
7267c094 | 601 | x509_cert_file = g_strdup(str); |
c448e855 | 602 | } else { |
7267c094 | 603 | x509_cert_file = g_malloc(len); |
c448e855 GH |
604 | snprintf(x509_cert_file, len, "%s/%s", x509_dir, X509_SERVER_CERT_FILE); |
605 | } | |
606 | ||
607 | str = qemu_opt_get(opts, "x509-cacert-file"); | |
608 | if (str) { | |
7267c094 | 609 | x509_cacert_file = g_strdup(str); |
c448e855 | 610 | } else { |
7267c094 | 611 | x509_cacert_file = g_malloc(len); |
c448e855 GH |
612 | snprintf(x509_cacert_file, len, "%s/%s", x509_dir, X509_CA_CERT_FILE); |
613 | } | |
614 | ||
615 | x509_key_password = qemu_opt_get(opts, "x509-key-password"); | |
616 | x509_dh_file = qemu_opt_get(opts, "x509-dh-file"); | |
617 | tls_ciphers = qemu_opt_get(opts, "tls-ciphers"); | |
618 | } | |
619 | ||
333b0eeb GH |
620 | addr = qemu_opt_get(opts, "addr"); |
621 | addr_flags = 0; | |
622 | if (qemu_opt_get_bool(opts, "ipv4", 0)) { | |
623 | addr_flags |= SPICE_ADDR_FLAG_IPV4_ONLY; | |
624 | } else if (qemu_opt_get_bool(opts, "ipv6", 0)) { | |
625 | addr_flags |= SPICE_ADDR_FLAG_IPV6_ONLY; | |
626 | } | |
627 | ||
29b0040b | 628 | spice_server = spice_server_new(); |
333b0eeb | 629 | spice_server_set_addr(spice_server, addr ? addr : "", addr_flags); |
c448e855 GH |
630 | if (port) { |
631 | spice_server_set_port(spice_server, port); | |
632 | } | |
633 | if (tls_port) { | |
634 | spice_server_set_tls(spice_server, tls_port, | |
635 | x509_cacert_file, | |
636 | x509_cert_file, | |
637 | x509_key_file, | |
638 | x509_key_password, | |
639 | x509_dh_file, | |
640 | tls_ciphers); | |
641 | } | |
29b0040b GH |
642 | if (password) { |
643 | spice_server_set_ticket(spice_server, password, 0, 0, 0); | |
644 | } | |
48b3ed0a MAL |
645 | if (qemu_opt_get_bool(opts, "sasl", 0)) { |
646 | #if SPICE_SERVER_VERSION >= 0x000900 /* 0.9.0 */ | |
647 | if (spice_server_set_sasl_appname(spice_server, "qemu") == -1 || | |
648 | spice_server_set_sasl(spice_server, 1) == -1) { | |
649 | fprintf(stderr, "spice: failed to enable sasl\n"); | |
650 | exit(1); | |
651 | } | |
652 | #else | |
653 | fprintf(stderr, "spice: sasl is not available (spice >= 0.9 required)\n"); | |
654 | exit(1); | |
655 | #endif | |
656 | } | |
29b0040b | 657 | if (qemu_opt_get_bool(opts, "disable-ticketing", 0)) { |
6f8c63fb | 658 | auth = "none"; |
29b0040b GH |
659 | spice_server_set_noauth(spice_server); |
660 | } | |
661 | ||
d4970b07 HG |
662 | #if SPICE_SERVER_VERSION >= 0x000801 |
663 | if (qemu_opt_get_bool(opts, "disable-copy-paste", 0)) { | |
664 | spice_server_set_agent_copypaste(spice_server, false); | |
665 | } | |
666 | #endif | |
667 | ||
9f04e09e YH |
668 | compression = SPICE_IMAGE_COMPRESS_AUTO_GLZ; |
669 | str = qemu_opt_get(opts, "image-compression"); | |
670 | if (str) { | |
671 | compression = parse_compression(str); | |
672 | } | |
673 | spice_server_set_image_compression(spice_server, compression); | |
674 | ||
675 | wan_compr = SPICE_WAN_COMPRESSION_AUTO; | |
676 | str = qemu_opt_get(opts, "jpeg-wan-compression"); | |
677 | if (str) { | |
678 | wan_compr = parse_wan_compression(str); | |
679 | } | |
680 | spice_server_set_jpeg_compression(spice_server, wan_compr); | |
681 | ||
682 | wan_compr = SPICE_WAN_COMPRESSION_AUTO; | |
683 | str = qemu_opt_get(opts, "zlib-glz-wan-compression"); | |
684 | if (str) { | |
685 | wan_compr = parse_wan_compression(str); | |
686 | } | |
687 | spice_server_set_zlib_glz_compression(spice_server, wan_compr); | |
29b0040b | 688 | |
84a23f25 GH |
689 | str = qemu_opt_get(opts, "streaming-video"); |
690 | if (str) { | |
f61d6960 | 691 | int streaming_video = parse_stream_video(str); |
84a23f25 GH |
692 | spice_server_set_streaming_video(spice_server, streaming_video); |
693 | } | |
694 | ||
695 | spice_server_set_agent_mouse | |
696 | (spice_server, qemu_opt_get_bool(opts, "agent-mouse", 1)); | |
697 | spice_server_set_playback_compression | |
698 | (spice_server, qemu_opt_get_bool(opts, "playback-compression", 1)); | |
699 | ||
17b6dea0 GH |
700 | qemu_opt_foreach(opts, add_channel, NULL, 0); |
701 | ||
fba810f1 GH |
702 | if (0 != spice_server_init(spice_server, &core_interface)) { |
703 | fprintf(stderr, "failed to initialize spice server"); | |
704 | exit(1); | |
705 | }; | |
29b0040b | 706 | using_spice = 1; |
864401c2 | 707 | |
e866e239 GH |
708 | migration_state.notify = migration_state_notifier; |
709 | add_migration_state_change_notifier(&migration_state); | |
026f773f YH |
710 | #ifdef SPICE_INTERFACE_MIGRATION |
711 | spice_migrate.sin.base.sif = &migrate_interface.base; | |
712 | spice_migrate.connect_complete.cb = NULL; | |
713 | qemu_spice_add_interface(&spice_migrate.sin.base); | |
714 | #endif | |
e866e239 | 715 | |
864401c2 | 716 | qemu_spice_input_init(); |
3e313753 | 717 | qemu_spice_audio_init(); |
c448e855 | 718 | |
7267c094 AL |
719 | g_free(x509_key_file); |
720 | g_free(x509_cert_file); | |
721 | g_free(x509_cacert_file); | |
29b0040b GH |
722 | } |
723 | ||
724 | int qemu_spice_add_interface(SpiceBaseInstance *sin) | |
725 | { | |
a19cbfb3 GH |
726 | if (!spice_server) { |
727 | if (QTAILQ_FIRST(&qemu_spice_opts.head) != NULL) { | |
728 | fprintf(stderr, "Oops: spice configured but not active\n"); | |
729 | exit(1); | |
730 | } | |
731 | /* | |
732 | * Create a spice server instance. | |
733 | * It does *not* listen on the network. | |
734 | * It handles QXL local rendering only. | |
735 | * | |
736 | * With a command line like '-vnc :0 -vga qxl' you'll end up here. | |
737 | */ | |
738 | spice_server = spice_server_new(); | |
739 | spice_server_init(spice_server, &core_interface); | |
740 | } | |
29b0040b GH |
741 | return spice_server_add_interface(spice_server, sin); |
742 | } | |
743 | ||
7572150c GH |
744 | static int qemu_spice_set_ticket(bool fail_if_conn, bool disconnect_if_conn) |
745 | { | |
746 | time_t lifetime, now = time(NULL); | |
747 | char *passwd; | |
748 | ||
749 | if (now < auth_expires) { | |
750 | passwd = auth_passwd; | |
751 | lifetime = (auth_expires - now); | |
752 | if (lifetime > INT_MAX) { | |
753 | lifetime = INT_MAX; | |
754 | } | |
755 | } else { | |
756 | passwd = NULL; | |
757 | lifetime = 1; | |
758 | } | |
759 | return spice_server_set_ticket(spice_server, passwd, lifetime, | |
760 | fail_if_conn, disconnect_if_conn); | |
761 | } | |
762 | ||
763 | int qemu_spice_set_passwd(const char *passwd, | |
764 | bool fail_if_conn, bool disconnect_if_conn) | |
765 | { | |
766 | free(auth_passwd); | |
767 | auth_passwd = strdup(passwd); | |
768 | return qemu_spice_set_ticket(fail_if_conn, disconnect_if_conn); | |
769 | } | |
770 | ||
771 | int qemu_spice_set_pw_expire(time_t expires) | |
772 | { | |
773 | auth_expires = expires; | |
774 | return qemu_spice_set_ticket(false, false); | |
775 | } | |
776 | ||
f1f5f407 DB |
777 | int qemu_spice_display_add_client(int csock, int skipauth, int tls) |
778 | { | |
779 | #if SPICE_SERVER_VERSION >= 0x000a01 | |
780 | if (tls) { | |
781 | return spice_server_add_ssl_client(spice_server, csock, skipauth); | |
782 | } else { | |
783 | return spice_server_add_client(spice_server, csock, skipauth); | |
784 | } | |
785 | #else | |
786 | return -1; | |
787 | #endif | |
788 | } | |
789 | ||
29b0040b GH |
790 | static void spice_register_config(void) |
791 | { | |
792 | qemu_add_opts(&qemu_spice_opts); | |
793 | } | |
794 | machine_init(spice_register_config); | |
795 | ||
83f7d43a | 796 | static void spice_register_types(void) |
29b0040b GH |
797 | { |
798 | qemu_spice_init(); | |
799 | } | |
83f7d43a AF |
800 | |
801 | type_init(spice_register_types) |