blob: db21db2c9428225defe66f8904e4fb7dd90bac33 [file] [log] [blame]
Gerd Hoffmann29b00402010-03-11 11:13:27 -03001/*
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
Peter Maydelle16f4c82016-01-29 17:49:51 +000018#include "qemu/osdep.h"
Gerd Hoffmann29b00402010-03-11 11:13:27 -030019#include <spice.h>
Gerd Hoffmann29b00402010-03-11 11:13:27 -030020
Paolo Bonzini9c17d612012-12-17 18:20:04 +010021#include "sysemu/sysemu.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020022#include "sysemu/runstate.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010023#include "ui/qemu-spice.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010024#include "qemu/error-report.h"
Markus Armbrusterdb725812019-08-12 07:23:50 +020025#include "qemu/main-loop.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020026#include "qemu/module.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010027#include "qemu/thread.h"
28#include "qemu/timer.h"
29#include "qemu/queue.h"
Gerd Hoffmannc448e852010-03-11 11:13:32 -030030#include "qemu-x509.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010031#include "qemu/sockets.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010032#include "qapi/error.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010033#include "qapi/qapi-commands-ui.h"
34#include "qapi/qapi-events-ui.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010035#include "qemu/notify.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010036#include "qemu/option.h"
Daniel P. Berrangé99522f62021-03-11 11:43:42 +000037#include "crypto/secret_common.h"
Juan Quintelac4b63b72017-04-24 19:02:44 +020038#include "migration/misc.h"
Lukáš Hrázkýbe812c02019-02-15 16:09:19 +010039#include "hw/pci/pci_bus.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010040#include "ui/spice-display.h"
Gerd Hoffmann29b00402010-03-11 11:13:27 -030041
42/* core bits */
43
44static SpiceServer *spice_server;
Gerd Hoffmanne866e232010-04-23 13:28:21 +020045static Notifier migration_state;
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +020046static const char *auth = "spice";
Gerd Hoffmann75721502010-10-07 12:22:54 +020047static char *auth_passwd;
48static time_t auth_expires = TIME_MAX;
Yonit Halperin61c4efe2012-08-21 11:51:58 +030049static int spice_migration_completed;
Gerd Hoffmann7cc6a252013-12-09 15:54:46 +010050static int spice_display_is_running;
Gerd Hoffmanna76a2f72014-04-29 09:27:31 +020051static int spice_have_target_host;
Gerd Hoffmann29b00402010-03-11 11:13:27 -030052
Jan Kiszkaf9ab6092011-09-20 17:14:33 +020053static QemuThread me;
Gerd Hoffmann22b626e2011-09-02 15:03:28 +020054
Gerd Hoffmann29b00402010-03-11 11:13:27 -030055struct SpiceTimer {
56 QEMUTimer *timer;
Gerd Hoffmann29b00402010-03-11 11:13:27 -030057};
Gerd Hoffmann29b00402010-03-11 11:13:27 -030058
59static SpiceTimer *timer_add(SpiceTimerFunc func, void *opaque)
60{
61 SpiceTimer *timer;
62
Anthony Liguori7267c092011-08-20 22:09:37 -050063 timer = g_malloc0(sizeof(*timer));
Alex Blighbc72ad62013-08-21 16:03:08 +010064 timer->timer = timer_new_ms(QEMU_CLOCK_REALTIME, func, opaque);
Gerd Hoffmann29b00402010-03-11 11:13:27 -030065 return timer;
66}
67
68static void timer_start(SpiceTimer *timer, uint32_t ms)
69{
Alex Blighbc72ad62013-08-21 16:03:08 +010070 timer_mod(timer->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + ms);
Gerd Hoffmann29b00402010-03-11 11:13:27 -030071}
72
73static void timer_cancel(SpiceTimer *timer)
74{
Alex Blighbc72ad62013-08-21 16:03:08 +010075 timer_del(timer->timer);
Gerd Hoffmann29b00402010-03-11 11:13:27 -030076}
77
78static void timer_remove(SpiceTimer *timer)
79{
Alex Blighbc72ad62013-08-21 16:03:08 +010080 timer_free(timer->timer);
Anthony Liguori7267c092011-08-20 22:09:37 -050081 g_free(timer);
Gerd Hoffmann29b00402010-03-11 11:13:27 -030082}
83
84struct SpiceWatch {
85 int fd;
Gerd Hoffmann29b00402010-03-11 11:13:27 -030086 SpiceWatchFunc func;
87 void *opaque;
Gerd Hoffmann29b00402010-03-11 11:13:27 -030088};
Gerd Hoffmann29b00402010-03-11 11:13:27 -030089
90static void watch_read(void *opaque)
91{
92 SpiceWatch *watch = opaque;
Marc-André Lureaue40283d2023-03-20 17:36:42 +040093 int fd = watch->fd;
94
95#ifdef WIN32
96 fd = _get_osfhandle(fd);
97#endif
98 watch->func(fd, SPICE_WATCH_EVENT_READ, watch->opaque);
Gerd Hoffmann29b00402010-03-11 11:13:27 -030099}
100
101static void watch_write(void *opaque)
102{
103 SpiceWatch *watch = opaque;
Marc-André Lureaue40283d2023-03-20 17:36:42 +0400104 int fd = watch->fd;
105
106#ifdef WIN32
107 fd = _get_osfhandle(fd);
108#endif
109 watch->func(fd, SPICE_WATCH_EVENT_WRITE, watch->opaque);
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300110}
111
112static void watch_update_mask(SpiceWatch *watch, int event_mask)
113{
114 IOHandler *on_read = NULL;
115 IOHandler *on_write = NULL;
116
Frediano Ziglio58a5d332017-11-22 13:56:24 +0000117 if (event_mask & SPICE_WATCH_EVENT_READ) {
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300118 on_read = watch_read;
119 }
Frediano Ziglio58a5d332017-11-22 13:56:24 +0000120 if (event_mask & SPICE_WATCH_EVENT_WRITE) {
Hans de Goede3d6d3062010-10-15 09:47:53 +0200121 on_write = watch_write;
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300122 }
123 qemu_set_fd_handler(watch->fd, on_read, on_write, watch);
124}
125
126static SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque)
127{
128 SpiceWatch *watch;
129
Marc-André Lureaue40283d2023-03-20 17:36:42 +0400130#ifdef WIN32
131 fd = _open_osfhandle(fd, _O_BINARY);
132 if (fd < 0) {
133 error_setg_win32(&error_warn, WSAGetLastError(), "Couldn't associate a FD with the SOCKET");
134 return NULL;
135 }
136#endif
137
Anthony Liguori7267c092011-08-20 22:09:37 -0500138 watch = g_malloc0(sizeof(*watch));
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300139 watch->fd = fd;
140 watch->func = func;
141 watch->opaque = opaque;
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300142
143 watch_update_mask(watch, event_mask);
144 return watch;
145}
146
147static void watch_remove(SpiceWatch *watch)
148{
Gerd Hoffmann08cc67f2011-10-21 15:56:21 +0200149 qemu_set_fd_handler(watch->fd, NULL, NULL, NULL);
Marc-André Lureaue40283d2023-03-20 17:36:42 +0400150#ifdef WIN32
151 /* SOCKET is owned by spice */
Marc-André Lureau7b1bde92023-03-22 11:52:56 +0400152 qemu_close_socket_osfhandle(watch->fd);
Marc-André Lureaue40283d2023-03-20 17:36:42 +0400153#endif
Anthony Liguori7267c092011-08-20 22:09:37 -0500154 g_free(watch);
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300155}
156
Gerd Hoffmanncb42a872010-11-30 11:02:51 +0100157typedef struct ChannelList ChannelList;
158struct ChannelList {
159 SpiceChannelEventInfo *info;
160 QTAILQ_ENTRY(ChannelList) link;
161};
162static QTAILQ_HEAD(, ChannelList) channel_list = QTAILQ_HEAD_INITIALIZER(channel_list);
163
164static void channel_list_add(SpiceChannelEventInfo *info)
165{
166 ChannelList *item;
167
Anthony Liguori7267c092011-08-20 22:09:37 -0500168 item = g_malloc0(sizeof(*item));
Gerd Hoffmanncb42a872010-11-30 11:02:51 +0100169 item->info = info;
170 QTAILQ_INSERT_TAIL(&channel_list, item, link);
171}
172
173static void channel_list_del(SpiceChannelEventInfo *info)
174{
175 ChannelList *item;
176
177 QTAILQ_FOREACH(item, &channel_list, link) {
178 if (item->info != info) {
179 continue;
180 }
181 QTAILQ_REMOVE(&channel_list, item, link);
Anthony Liguori7267c092011-08-20 22:09:37 -0500182 g_free(item);
Gerd Hoffmanncb42a872010-11-30 11:02:51 +0100183 return;
184 }
185}
186
Wenchao Xia7cfadb62014-06-18 08:43:50 +0200187static void add_addr_info(SpiceBasicInfo *info, struct sockaddr *addr, int len)
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +0200188{
189 char host[NI_MAXHOST], port[NI_MAXSERV];
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +0200190
191 getnameinfo(addr, len, host, sizeof(host), port, sizeof(port),
192 NI_NUMERICHOST | NI_NUMERICSERV);
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +0200193
Wenchao Xia7cfadb62014-06-18 08:43:50 +0200194 info->host = g_strdup(host);
195 info->port = g_strdup(port);
196 info->family = inet_netfamily(addr->sa_family);
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +0200197}
198
Wenchao Xia7cfadb62014-06-18 08:43:50 +0200199static void add_channel_info(SpiceChannel *sc, SpiceChannelEventInfo *info)
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +0200200{
201 int tls = info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS;
202
Wenchao Xia7cfadb62014-06-18 08:43:50 +0200203 sc->connection_id = info->connection_id;
204 sc->channel_type = info->type;
205 sc->channel_id = info->id;
206 sc->tls = !!tls;
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +0200207}
208
209static void channel_event(int event, SpiceChannelEventInfo *info)
210{
Wenchao Xia7cfadb62014-06-18 08:43:50 +0200211 SpiceServerInfo *server = g_malloc0(sizeof(*server));
212 SpiceChannel *client = g_malloc0(sizeof(*client));
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +0200213
Gerd Hoffmann22b626e2011-09-02 15:03:28 +0200214 /*
215 * Spice server might have called us from spice worker thread
216 * context (happens on display channel disconnects). Spice should
217 * not do that. It isn't that easy to fix it in spice and even
218 * when it is fixed we still should cover the already released
219 * spice versions. So detect that we've been called from another
220 * thread and grab the iothread lock if so before calling qemu
221 * functions.
222 */
Jan Kiszkaf9ab6092011-09-20 17:14:33 +0200223 bool need_lock = !qemu_thread_is_self(&me);
Gerd Hoffmann22b626e2011-09-02 15:03:28 +0200224 if (need_lock) {
225 qemu_mutex_lock_iothread();
226 }
227
Yonit Halperinfaa98222012-02-08 15:40:15 +0200228 if (info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT) {
Eric Blakeddf21902015-10-26 16:34:49 -0600229 add_addr_info(qapi_SpiceChannel_base(client),
230 (struct sockaddr *)&info->paddr_ext,
Yonit Halperinfaa98222012-02-08 15:40:15 +0200231 info->plen_ext);
Eric Blakeddf21902015-10-26 16:34:49 -0600232 add_addr_info(qapi_SpiceServerInfo_base(server),
233 (struct sockaddr *)&info->laddr_ext,
Yonit Halperinfaa98222012-02-08 15:40:15 +0200234 info->llen_ext);
235 } else {
Christophe Fergeau339a4752012-02-24 18:13:12 +0100236 error_report("spice: %s, extended address is expected",
237 __func__);
Yonit Halperinfaa98222012-02-08 15:40:15 +0200238 }
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +0200239
Wenchao Xia7cfadb62014-06-18 08:43:50 +0200240 switch (event) {
241 case SPICE_CHANNEL_EVENT_CONNECTED:
Eric Blakeddf21902015-10-26 16:34:49 -0600242 qapi_event_send_spice_connected(qapi_SpiceServerInfo_base(server),
Peter Xu3ab72382018-08-15 21:37:37 +0800243 qapi_SpiceChannel_base(client));
Wenchao Xia7cfadb62014-06-18 08:43:50 +0200244 break;
245 case SPICE_CHANNEL_EVENT_INITIALIZED:
246 if (auth) {
Wenchao Xia7cfadb62014-06-18 08:43:50 +0200247 server->auth = g_strdup(auth);
248 }
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +0200249 add_channel_info(client, info);
Gerd Hoffmanncb42a872010-11-30 11:02:51 +0100250 channel_list_add(info);
Peter Xu3ab72382018-08-15 21:37:37 +0800251 qapi_event_send_spice_initialized(server, client);
Wenchao Xia7cfadb62014-06-18 08:43:50 +0200252 break;
253 case SPICE_CHANNEL_EVENT_DISCONNECTED:
Gerd Hoffmanncb42a872010-11-30 11:02:51 +0100254 channel_list_del(info);
Eric Blakeddf21902015-10-26 16:34:49 -0600255 qapi_event_send_spice_disconnected(qapi_SpiceServerInfo_base(server),
Peter Xu3ab72382018-08-15 21:37:37 +0800256 qapi_SpiceChannel_base(client));
Wenchao Xia7cfadb62014-06-18 08:43:50 +0200257 break;
258 default:
259 break;
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +0200260 }
261
Gerd Hoffmann22b626e2011-09-02 15:03:28 +0200262 if (need_lock) {
263 qemu_mutex_unlock_iothread();
264 }
Wenchao Xia7cfadb62014-06-18 08:43:50 +0200265
266 qapi_free_SpiceServerInfo(server);
267 qapi_free_SpiceChannel(client);
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +0200268}
269
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300270static SpiceCoreInterface core_interface = {
271 .base.type = SPICE_INTERFACE_CORE,
272 .base.description = "qemu core services",
273 .base.major_version = SPICE_INTERFACE_CORE_MAJOR,
274 .base.minor_version = SPICE_INTERFACE_CORE_MINOR,
275
276 .timer_add = timer_add,
277 .timer_start = timer_start,
278 .timer_cancel = timer_cancel,
279 .timer_remove = timer_remove,
280
281 .watch_add = watch_add,
282 .watch_update_mask = watch_update_mask,
283 .watch_remove = watch_remove,
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +0200284
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +0200285 .channel_event = channel_event,
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300286};
287
Yonit Halperin026f7732011-10-17 10:03:19 +0200288static void migrate_connect_complete_cb(SpiceMigrateInstance *sin);
Yonit Halperin2fdd16e2012-08-21 11:51:57 +0300289static void migrate_end_complete_cb(SpiceMigrateInstance *sin);
Yonit Halperin026f7732011-10-17 10:03:19 +0200290
291static const SpiceMigrateInterface migrate_interface = {
292 .base.type = SPICE_INTERFACE_MIGRATION,
293 .base.description = "migration",
294 .base.major_version = SPICE_INTERFACE_MIGRATION_MAJOR,
295 .base.minor_version = SPICE_INTERFACE_MIGRATION_MINOR,
296 .migrate_connect_complete = migrate_connect_complete_cb,
Yonit Halperin2fdd16e2012-08-21 11:51:57 +0300297 .migrate_end_complete = migrate_end_complete_cb,
Yonit Halperin026f7732011-10-17 10:03:19 +0200298};
299
Markus Armbruster3b5704b2015-03-05 09:30:16 +0100300static SpiceMigrateInstance spice_migrate;
Yonit Halperin026f7732011-10-17 10:03:19 +0200301
302static void migrate_connect_complete_cb(SpiceMigrateInstance *sin)
303{
Markus Armbruster3b5704b2015-03-05 09:30:16 +0100304 /* nothing, but libspice-server expects this cb being present. */
Yonit Halperin026f7732011-10-17 10:03:19 +0200305}
Yonit Halperin2fdd16e2012-08-21 11:51:57 +0300306
307static void migrate_end_complete_cb(SpiceMigrateInstance *sin)
308{
Peter Xu3ab72382018-08-15 21:37:37 +0800309 qapi_event_send_spice_migrate_completed();
Yonit Halperin61c4efe2012-08-21 11:51:58 +0300310 spice_migration_completed = true;
Yonit Halperin2fdd16e2012-08-21 11:51:57 +0300311}
Yonit Halperin026f7732011-10-17 10:03:19 +0200312
Yonit Halperin9f04e092010-07-14 13:26:34 +0300313/* config string parsing */
314
315static int name2enum(const char *string, const char *table[], int entries)
316{
317 int i;
318
319 if (string) {
320 for (i = 0; i < entries; i++) {
321 if (!table[i]) {
322 continue;
323 }
324 if (strcmp(string, table[i]) != 0) {
325 continue;
326 }
327 return i;
328 }
329 }
330 return -1;
331}
332
333static int parse_name(const char *string, const char *optname,
334 const char *table[], int entries)
335{
336 int value = name2enum(string, table, entries);
337
338 if (value != -1) {
339 return value;
340 }
Christophe Fergeau339a4752012-02-24 18:13:12 +0100341 error_report("spice: invalid %s: %s", optname, string);
Yonit Halperin9f04e092010-07-14 13:26:34 +0300342 exit(1);
343}
344
Gerd Hoffmann84a23f22010-08-30 16:36:53 +0200345static const char *stream_video_names[] = {
346 [ SPICE_STREAM_VIDEO_OFF ] = "off",
347 [ SPICE_STREAM_VIDEO_ALL ] = "all",
348 [ SPICE_STREAM_VIDEO_FILTER ] = "filter",
349};
350#define parse_stream_video(_name) \
Christophe Fergeau835cab82012-08-13 10:32:32 +0200351 parse_name(_name, "stream video control", \
352 stream_video_names, ARRAY_SIZE(stream_video_names))
Gerd Hoffmann84a23f22010-08-30 16:36:53 +0200353
Yonit Halperin9f04e092010-07-14 13:26:34 +0300354static const char *compression_names[] = {
355 [ SPICE_IMAGE_COMPRESS_OFF ] = "off",
356 [ SPICE_IMAGE_COMPRESS_AUTO_GLZ ] = "auto_glz",
357 [ SPICE_IMAGE_COMPRESS_AUTO_LZ ] = "auto_lz",
358 [ SPICE_IMAGE_COMPRESS_QUIC ] = "quic",
359 [ SPICE_IMAGE_COMPRESS_GLZ ] = "glz",
360 [ SPICE_IMAGE_COMPRESS_LZ ] = "lz",
361};
362#define parse_compression(_name) \
363 parse_name(_name, "image compression", \
364 compression_names, ARRAY_SIZE(compression_names))
365
366static const char *wan_compression_names[] = {
367 [ SPICE_WAN_COMPRESSION_AUTO ] = "auto",
368 [ SPICE_WAN_COMPRESSION_NEVER ] = "never",
369 [ SPICE_WAN_COMPRESSION_ALWAYS ] = "always",
370};
371#define parse_wan_compression(_name) \
372 parse_name(_name, "wan compression", \
373 wan_compression_names, ARRAY_SIZE(wan_compression_names))
374
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300375/* functions for the rest of qemu */
376
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200377static SpiceChannelList *qmp_query_spice_channels(void)
Gerd Hoffmanncb42a872010-11-30 11:02:51 +0100378{
Eric Blake95b3a8c2021-01-13 16:10:13 -0600379 SpiceChannelList *head = NULL, **tail = &head;
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200380 ChannelList *item;
Gerd Hoffmanncb42a872010-11-30 11:02:51 +0100381
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200382 QTAILQ_FOREACH(item, &channel_list, link) {
Eric Blake95b3a8c2021-01-13 16:10:13 -0600383 SpiceChannel *chan;
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200384 char host[NI_MAXHOST], port[NI_MAXSERV];
Yonit Halperinfaa98222012-02-08 15:40:15 +0200385 struct sockaddr *paddr;
386 socklen_t plen;
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200387
Gongleia4164272014-12-05 16:30:10 +0800388 assert(item->info->flags & SPICE_CHANNEL_EVENT_FLAG_ADDR_EXT);
Marc-André Lureau26defe82013-10-04 13:10:46 +0200389
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200390 chan = g_malloc0(sizeof(*chan));
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200391
Marc-André Lureau26defe82013-10-04 13:10:46 +0200392 paddr = (struct sockaddr *)&item->info->paddr_ext;
393 plen = item->info->plen_ext;
Yonit Halperinfaa98222012-02-08 15:40:15 +0200394 getnameinfo(paddr, plen,
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200395 host, sizeof(host), port, sizeof(port),
396 NI_NUMERICHOST | NI_NUMERICSERV);
Eric Blake95b3a8c2021-01-13 16:10:13 -0600397 chan->host = g_strdup(host);
398 chan->port = g_strdup(port);
399 chan->family = inet_netfamily(paddr->sa_family);
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200400
Eric Blake95b3a8c2021-01-13 16:10:13 -0600401 chan->connection_id = item->info->connection_id;
402 chan->channel_type = item->info->type;
403 chan->channel_id = item->info->id;
404 chan->tls = item->info->flags & SPICE_CHANNEL_EVENT_FLAG_TLS;
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200405
Eric Blake95b3a8c2021-01-13 16:10:13 -0600406 QAPI_LIST_APPEND(tail, chan);
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200407 }
408
409 return head;
Gerd Hoffmanncb42a872010-11-30 11:02:51 +0100410}
411
Paolo Bonzini4d454572012-11-26 16:03:42 +0100412static QemuOptsList qemu_spice_opts = {
413 .name = "spice",
414 .head = QTAILQ_HEAD_INITIALIZER(qemu_spice_opts.head),
Marc-André Lureau79216712019-02-21 12:06:56 +0100415 .merge_lists = true,
Paolo Bonzini4d454572012-11-26 16:03:42 +0100416 .desc = {
417 {
418 .name = "port",
419 .type = QEMU_OPT_NUMBER,
420 },{
421 .name = "tls-port",
422 .type = QEMU_OPT_NUMBER,
423 },{
424 .name = "addr",
425 .type = QEMU_OPT_STRING,
426 },{
427 .name = "ipv4",
428 .type = QEMU_OPT_BOOL,
429 },{
430 .name = "ipv6",
431 .type = QEMU_OPT_BOOL,
Marc-André Lureaufe4831b2015-01-13 17:57:51 +0100432#ifdef SPICE_ADDR_FLAG_UNIX_ONLY
433 },{
434 .name = "unix",
435 .type = QEMU_OPT_BOOL,
436#endif
Paolo Bonzini4d454572012-11-26 16:03:42 +0100437 },{
Daniel P. Berrangé99522f62021-03-11 11:43:42 +0000438 .name = "password-secret",
439 .type = QEMU_OPT_STRING,
440 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +0100441 .name = "disable-ticketing",
442 .type = QEMU_OPT_BOOL,
443 },{
444 .name = "disable-copy-paste",
445 .type = QEMU_OPT_BOOL,
446 },{
Hans de Goede5ad24e52013-06-08 15:37:27 +0200447 .name = "disable-agent-file-xfer",
448 .type = QEMU_OPT_BOOL,
449 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +0100450 .name = "sasl",
451 .type = QEMU_OPT_BOOL,
452 },{
453 .name = "x509-dir",
454 .type = QEMU_OPT_STRING,
455 },{
456 .name = "x509-key-file",
457 .type = QEMU_OPT_STRING,
458 },{
459 .name = "x509-key-password",
460 .type = QEMU_OPT_STRING,
461 },{
462 .name = "x509-cert-file",
463 .type = QEMU_OPT_STRING,
464 },{
465 .name = "x509-cacert-file",
466 .type = QEMU_OPT_STRING,
467 },{
468 .name = "x509-dh-key-file",
469 .type = QEMU_OPT_STRING,
470 },{
471 .name = "tls-ciphers",
472 .type = QEMU_OPT_STRING,
473 },{
474 .name = "tls-channel",
475 .type = QEMU_OPT_STRING,
476 },{
477 .name = "plaintext-channel",
478 .type = QEMU_OPT_STRING,
479 },{
480 .name = "image-compression",
481 .type = QEMU_OPT_STRING,
482 },{
483 .name = "jpeg-wan-compression",
484 .type = QEMU_OPT_STRING,
485 },{
486 .name = "zlib-glz-wan-compression",
487 .type = QEMU_OPT_STRING,
488 },{
489 .name = "streaming-video",
490 .type = QEMU_OPT_STRING,
491 },{
492 .name = "agent-mouse",
493 .type = QEMU_OPT_BOOL,
494 },{
495 .name = "playback-compression",
496 .type = QEMU_OPT_BOOL,
Gerd Hoffmann474114b2015-10-13 15:39:34 +0200497 },{
Paolo Bonzini4d454572012-11-26 16:03:42 +0100498 .name = "seamless-migration",
499 .type = QEMU_OPT_BOOL,
Gerd Hoffmann8bf69b42017-02-21 08:57:37 +0100500 },{
501 .name = "display",
502 .type = QEMU_OPT_STRING,
503 },{
504 .name = "head",
505 .type = QEMU_OPT_NUMBER,
Gerd Hoffmann474114b2015-10-13 15:39:34 +0200506#ifdef HAVE_SPICE_GL
507 },{
508 .name = "gl",
509 .type = QEMU_OPT_BOOL,
Marc-André Lureau7b525502017-02-12 15:21:18 +0400510 },{
511 .name = "rendernode",
512 .type = QEMU_OPT_STRING,
Gerd Hoffmann474114b2015-10-13 15:39:34 +0200513#endif
Paolo Bonzini4d454572012-11-26 16:03:42 +0100514 },
515 { /* end of list */ }
516 },
517};
518
Gerd Hoffmanndb5732c2020-10-19 09:52:18 +0200519static SpiceInfo *qmp_query_spice_real(Error **errp)
Gerd Hoffmanncb42a872010-11-30 11:02:51 +0100520{
521 QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
Gerd Hoffmanncb42a872010-11-30 11:02:51 +0100522 int port, tls_port;
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200523 const char *addr;
524 SpiceInfo *info;
Christophe Fergeau6735aa92013-09-02 15:41:32 +0200525 unsigned int major;
526 unsigned int minor;
527 unsigned int micro;
Gerd Hoffmanncb42a872010-11-30 11:02:51 +0100528
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200529 info = g_malloc0(sizeof(*info));
530
Alon Levy3bb781f2011-10-04 13:25:53 +0200531 if (!spice_server || !opts) {
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200532 info->enabled = false;
533 return info;
Gerd Hoffmanncb42a872010-11-30 11:02:51 +0100534 }
535
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200536 info->enabled = true;
Yonit Halperin61c4efe2012-08-21 11:51:58 +0300537 info->migrated = spice_migration_completed;
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200538
Gerd Hoffmanncb42a872010-11-30 11:02:51 +0100539 addr = qemu_opt_get(opts, "addr");
540 port = qemu_opt_get_number(opts, "port", 0);
541 tls_port = qemu_opt_get_number(opts, "tls-port", 0);
Gerd Hoffmanncb42a872010-11-30 11:02:51 +0100542
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200543 info->auth = g_strdup(auth);
Gerd Hoffmann4f60af92014-04-15 08:55:44 +0200544 info->host = g_strdup(addr ? addr : "*");
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200545
Christophe Fergeau6735aa92013-09-02 15:41:32 +0200546 major = (SPICE_SERVER_VERSION & 0xff0000) >> 16;
547 minor = (SPICE_SERVER_VERSION & 0xff00) >> 8;
548 micro = SPICE_SERVER_VERSION & 0xff;
549 info->compiled_version = g_strdup_printf("%d.%d.%d", major, minor, micro);
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200550
Gerd Hoffmanncb42a872010-11-30 11:02:51 +0100551 if (port) {
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200552 info->has_port = true;
553 info->port = port;
Gerd Hoffmanncb42a872010-11-30 11:02:51 +0100554 }
555 if (tls_port) {
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200556 info->has_tls_port = true;
557 info->tls_port = tls_port;
Gerd Hoffmanncb42a872010-11-30 11:02:51 +0100558 }
559
Alon Levy4efee022012-03-29 23:23:14 +0200560 info->mouse_mode = spice_server_is_server_mouse(spice_server) ?
561 SPICE_QUERY_MOUSE_MODE_SERVER :
562 SPICE_QUERY_MOUSE_MODE_CLIENT;
Gerd Hoffmann67be6722012-09-24 10:23:40 +0200563
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200564 /* for compatibility with the original command */
565 info->has_channels = true;
566 info->channels = qmp_query_spice_channels();
567
568 return info;
Gerd Hoffmanncb42a872010-11-30 11:02:51 +0100569}
570
Jan Kiszka9e8dd452011-06-20 14:06:26 +0200571static void migration_state_notifier(Notifier *notifier, void *data)
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200572{
Juan Quintela70736932011-02-23 00:43:59 +0100573 MigrationState *s = data;
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200574
Gerd Hoffmanna76a2f72014-04-29 09:27:31 +0200575 if (!spice_have_target_host) {
576 return;
577 }
578
Stefan Hajnoczi02edd2e2013-07-29 15:01:58 +0200579 if (migration_in_setup(s)) {
Yonit Halperin026f7732011-10-17 10:03:19 +0200580 spice_server_migrate_start(spice_server);
Dr. David Alan Gilbertb82fc322016-02-22 17:17:32 +0000581 } else if (migration_has_finished(s) ||
582 migration_in_postcopy_after_devices(s)) {
Yonit Halperin026f7732011-10-17 10:03:19 +0200583 spice_server_migrate_end(spice_server, true);
Gerd Hoffmanna76a2f72014-04-29 09:27:31 +0200584 spice_have_target_host = false;
Yonit Halperin026f7732011-10-17 10:03:19 +0200585 } else if (migration_has_failed(s)) {
586 spice_server_migrate_end(spice_server, false);
Gerd Hoffmanna76a2f72014-04-29 09:27:31 +0200587 spice_have_target_host = false;
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200588 }
589}
590
591int qemu_spice_migrate_info(const char *hostname, int port, int tls_port,
Markus Armbruster3b5704b2015-03-05 09:30:16 +0100592 const char *subject)
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200593{
Yonit Halperinedc5cb12011-10-17 10:03:18 +0200594 int ret;
Gerd Hoffmann67be6722012-09-24 10:23:40 +0200595
Yonit Halperin026f7732011-10-17 10:03:19 +0200596 ret = spice_server_migrate_connect(spice_server, hostname,
597 port, tls_port, subject);
Gerd Hoffmanna76a2f72014-04-29 09:27:31 +0200598 spice_have_target_host = true;
Yonit Halperinedc5cb12011-10-17 10:03:18 +0200599 return ret;
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200600}
601
Markus Armbruster71df1d82015-03-12 08:40:25 +0100602static int add_channel(void *opaque, const char *name, const char *value,
603 Error **errp)
Gerd Hoffmann17b6dea2010-08-27 14:09:56 +0200604{
605 int security = 0;
606 int rc;
607
608 if (strcmp(name, "tls-channel") == 0) {
Christophe Fergeau35c63322012-02-24 18:28:32 +0100609 int *tls_port = opaque;
610 if (!*tls_port) {
Markus Armbruster93385702018-10-17 10:26:54 +0200611 error_setg(errp, "spice: tried to setup tls-channel"
612 " without specifying a TLS port");
613 return -1;
Christophe Fergeau35c63322012-02-24 18:28:32 +0100614 }
Gerd Hoffmann17b6dea2010-08-27 14:09:56 +0200615 security = SPICE_CHANNEL_SECURITY_SSL;
616 }
617 if (strcmp(name, "plaintext-channel") == 0) {
618 security = SPICE_CHANNEL_SECURITY_NONE;
619 }
620 if (security == 0) {
621 return 0;
622 }
623 if (strcmp(value, "default") == 0) {
624 rc = spice_server_set_channel_security(spice_server, NULL, security);
625 } else {
626 rc = spice_server_set_channel_security(spice_server, value, security);
627 }
628 if (rc != 0) {
Markus Armbruster93385702018-10-17 10:26:54 +0200629 error_setg(errp, "spice: failed to set channel security for %s",
630 value);
631 return -1;
Gerd Hoffmann17b6dea2010-08-27 14:09:56 +0200632 }
633 return 0;
634}
635
Philippe Mathieu-Daudé538f0492021-01-11 16:20:20 +0100636static void vm_change_state_handler(void *opaque, bool running,
Yonit Halperinf5bb0392012-08-21 11:51:55 +0300637 RunState state)
638{
Yonit Halperinf5bb0392012-08-21 11:51:55 +0300639 if (running) {
Yonit Halperin71d388d2012-08-21 11:51:56 +0300640 qemu_spice_display_start();
Marc-André Lureau5b1638b2019-02-21 12:06:57 +0100641 } else if (state != RUN_STATE_PAUSED) {
Yonit Halperin71d388d2012-08-21 11:51:56 +0300642 qemu_spice_display_stop();
Yonit Halperinf5bb0392012-08-21 11:51:55 +0300643 }
Yonit Halperinf5bb0392012-08-21 11:51:55 +0300644}
645
Marc-André Lureaua652b122021-01-29 19:23:51 +0400646void qemu_spice_display_init_done(void)
647{
648 if (runstate_is_running()) {
649 qemu_spice_display_start();
650 }
651 qemu_add_vm_change_state_handler(vm_change_state_handler, NULL);
652}
653
Gerd Hoffmann63be30e2020-10-19 09:52:13 +0200654static void qemu_spice_init(void)
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300655{
656 QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
Daniel P. Berrangé99522f62021-03-11 11:43:42 +0000657 char *password = NULL;
658 const char *passwordSecret;
659 const char *str, *x509_dir, *addr,
Gerd Hoffmannc448e852010-03-11 11:13:32 -0300660 *x509_key_password = NULL,
661 *x509_dh_file = NULL,
662 *tls_ciphers = NULL;
663 char *x509_key_file = NULL,
664 *x509_cert_file = NULL,
665 *x509_cacert_file = NULL;
Christophe Fergeau6735aa92013-09-02 15:41:32 +0200666 int port, tls_port, addr_flags;
Yonit Halperin9f04e092010-07-14 13:26:34 +0300667 spice_image_compression_t compression;
668 spice_wan_compression_t wan_compr;
Yonit Halperin8c957052012-08-21 11:51:59 +0300669 bool seamless_migration;
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300670
Jan Kiszkaf9ab6092011-09-20 17:14:33 +0200671 qemu_thread_get_self(&me);
Gerd Hoffmann22b626e2011-09-02 15:03:28 +0200672
Alon Levyad1be892012-03-14 20:33:37 +0200673 if (!opts) {
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300674 return;
675 }
676 port = qemu_opt_get_number(opts, "port", 0);
Gerd Hoffmannc448e852010-03-11 11:13:32 -0300677 tls_port = qemu_opt_get_number(opts, "tls-port", 0);
Gerd Hoffmanndf9cb662011-07-07 17:04:17 +0200678 if (port < 0 || port > 65535) {
Christophe Fergeau339a4752012-02-24 18:13:12 +0100679 error_report("spice port is out of range");
Gerd Hoffmanndf9cb662011-07-07 17:04:17 +0200680 exit(1);
681 }
682 if (tls_port < 0 || tls_port > 65535) {
Christophe Fergeau339a4752012-02-24 18:13:12 +0100683 error_report("spice tls-port is out of range");
Gerd Hoffmanndf9cb662011-07-07 17:04:17 +0200684 exit(1);
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300685 }
Daniel P. Berrangé99522f62021-03-11 11:43:42 +0000686 passwordSecret = qemu_opt_get(opts, "password-secret");
687 if (passwordSecret) {
Daniel P. Berrangé99522f62021-03-11 11:43:42 +0000688 password = qcrypto_secret_lookup_as_utf8(passwordSecret,
Markus Armbrusterf9734d52021-07-20 14:53:53 +0200689 &error_fatal);
Daniel P. Berrangé99522f62021-03-11 11:43:42 +0000690 }
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300691
Gerd Hoffmannc448e852010-03-11 11:13:32 -0300692 if (tls_port) {
693 x509_dir = qemu_opt_get(opts, "x509-dir");
Gongleife8e8322014-08-11 21:00:56 +0800694 if (!x509_dir) {
Gerd Hoffmannc448e852010-03-11 11:13:32 -0300695 x509_dir = ".";
696 }
Gerd Hoffmannc448e852010-03-11 11:13:32 -0300697
698 str = qemu_opt_get(opts, "x509-key-file");
699 if (str) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500700 x509_key_file = g_strdup(str);
Gerd Hoffmannc448e852010-03-11 11:13:32 -0300701 } else {
Christophe Fergeau6735aa92013-09-02 15:41:32 +0200702 x509_key_file = g_strdup_printf("%s/%s", x509_dir,
703 X509_SERVER_KEY_FILE);
Gerd Hoffmannc448e852010-03-11 11:13:32 -0300704 }
705
706 str = qemu_opt_get(opts, "x509-cert-file");
707 if (str) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500708 x509_cert_file = g_strdup(str);
Gerd Hoffmannc448e852010-03-11 11:13:32 -0300709 } else {
Christophe Fergeau6735aa92013-09-02 15:41:32 +0200710 x509_cert_file = g_strdup_printf("%s/%s", x509_dir,
711 X509_SERVER_CERT_FILE);
Gerd Hoffmannc448e852010-03-11 11:13:32 -0300712 }
713
714 str = qemu_opt_get(opts, "x509-cacert-file");
715 if (str) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500716 x509_cacert_file = g_strdup(str);
Gerd Hoffmannc448e852010-03-11 11:13:32 -0300717 } else {
Christophe Fergeau6735aa92013-09-02 15:41:32 +0200718 x509_cacert_file = g_strdup_printf("%s/%s", x509_dir,
719 X509_CA_CERT_FILE);
Gerd Hoffmannc448e852010-03-11 11:13:32 -0300720 }
721
722 x509_key_password = qemu_opt_get(opts, "x509-key-password");
Lei Li9995c0b2012-11-19 17:15:08 +0800723 x509_dh_file = qemu_opt_get(opts, "x509-dh-key-file");
Gerd Hoffmannc448e852010-03-11 11:13:32 -0300724 tls_ciphers = qemu_opt_get(opts, "tls-ciphers");
725 }
726
Gerd Hoffmann333b0ee2010-08-27 14:29:16 +0200727 addr = qemu_opt_get(opts, "addr");
728 addr_flags = 0;
729 if (qemu_opt_get_bool(opts, "ipv4", 0)) {
730 addr_flags |= SPICE_ADDR_FLAG_IPV4_ONLY;
731 } else if (qemu_opt_get_bool(opts, "ipv6", 0)) {
732 addr_flags |= SPICE_ADDR_FLAG_IPV6_ONLY;
Marc-André Lureaufe4831b2015-01-13 17:57:51 +0100733#ifdef SPICE_ADDR_FLAG_UNIX_ONLY
734 } else if (qemu_opt_get_bool(opts, "unix", 0)) {
735 addr_flags |= SPICE_ADDR_FLAG_UNIX_ONLY;
736#endif
Gerd Hoffmann333b0ee2010-08-27 14:29:16 +0200737 }
738
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300739 spice_server = spice_server_new();
Gerd Hoffmann333b0ee2010-08-27 14:29:16 +0200740 spice_server_set_addr(spice_server, addr ? addr : "", addr_flags);
Gerd Hoffmannc448e852010-03-11 11:13:32 -0300741 if (port) {
742 spice_server_set_port(spice_server, port);
743 }
744 if (tls_port) {
745 spice_server_set_tls(spice_server, tls_port,
746 x509_cacert_file,
747 x509_cert_file,
748 x509_key_file,
749 x509_key_password,
750 x509_dh_file,
751 tls_ciphers);
752 }
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300753 if (password) {
Gerd Hoffmann08ad2622020-10-19 09:52:16 +0200754 qemu_spice.set_passwd(password, false, false);
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300755 }
Marc-André Lureau48b3ed02011-05-17 10:40:33 +0200756 if (qemu_opt_get_bool(opts, "sasl", 0)) {
Christophe Fergeau06bb8812016-01-12 15:08:58 +0100757 if (spice_server_set_sasl(spice_server, 1) == -1) {
Christophe Fergeau339a4752012-02-24 18:13:12 +0100758 error_report("spice: failed to enable sasl");
Marc-André Lureau48b3ed02011-05-17 10:40:33 +0200759 exit(1);
760 }
Gerd Hoffmannb1ea7b72014-07-02 12:56:42 +0200761 auth = "sasl";
Marc-André Lureau48b3ed02011-05-17 10:40:33 +0200762 }
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300763 if (qemu_opt_get_bool(opts, "disable-ticketing", 0)) {
Gerd Hoffmann6f8c63f2010-10-11 18:03:51 +0200764 auth = "none";
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300765 spice_server_set_noauth(spice_server);
766 }
767
Hans de Goeded4970b02011-03-27 16:43:54 +0200768 if (qemu_opt_get_bool(opts, "disable-copy-paste", 0)) {
769 spice_server_set_agent_copypaste(spice_server, false);
770 }
Hans de Goeded4970b02011-03-27 16:43:54 +0200771
Hans de Goede5ad24e52013-06-08 15:37:27 +0200772 if (qemu_opt_get_bool(opts, "disable-agent-file-xfer", 0)) {
Hans de Goede5ad24e52013-06-08 15:37:27 +0200773 spice_server_set_agent_file_xfer(spice_server, false);
Hans de Goede5ad24e52013-06-08 15:37:27 +0200774 }
775
Yonit Halperin9f04e092010-07-14 13:26:34 +0300776 compression = SPICE_IMAGE_COMPRESS_AUTO_GLZ;
777 str = qemu_opt_get(opts, "image-compression");
778 if (str) {
779 compression = parse_compression(str);
780 }
781 spice_server_set_image_compression(spice_server, compression);
782
783 wan_compr = SPICE_WAN_COMPRESSION_AUTO;
784 str = qemu_opt_get(opts, "jpeg-wan-compression");
785 if (str) {
786 wan_compr = parse_wan_compression(str);
787 }
788 spice_server_set_jpeg_compression(spice_server, wan_compr);
789
790 wan_compr = SPICE_WAN_COMPRESSION_AUTO;
791 str = qemu_opt_get(opts, "zlib-glz-wan-compression");
792 if (str) {
793 wan_compr = parse_wan_compression(str);
794 }
795 spice_server_set_zlib_glz_compression(spice_server, wan_compr);
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300796
Gerd Hoffmann84a23f22010-08-30 16:36:53 +0200797 str = qemu_opt_get(opts, "streaming-video");
798 if (str) {
Gerd Hoffmannf61d6962010-11-02 12:21:50 +0100799 int streaming_video = parse_stream_video(str);
Gerd Hoffmann84a23f22010-08-30 16:36:53 +0200800 spice_server_set_streaming_video(spice_server, streaming_video);
Gerd Hoffmannf1d3e582013-12-02 11:17:04 +0100801 } else {
802 spice_server_set_streaming_video(spice_server, SPICE_STREAM_VIDEO_OFF);
Gerd Hoffmann84a23f22010-08-30 16:36:53 +0200803 }
804
805 spice_server_set_agent_mouse
806 (spice_server, qemu_opt_get_bool(opts, "agent-mouse", 1));
807 spice_server_set_playback_compression
808 (spice_server, qemu_opt_get_bool(opts, "playback-compression", 1));
809
Markus Armbruster93385702018-10-17 10:26:54 +0200810 qemu_opt_foreach(opts, add_channel, &tls_port, &error_fatal);
Gerd Hoffmann17b6dea2010-08-27 14:09:56 +0200811
Marc-André Lureau4c77ee12019-02-21 12:07:02 +0100812 spice_server_set_name(spice_server, qemu_name ?: "QEMU " QEMU_VERSION);
Fam Zheng9c5ce8d2016-09-21 12:27:22 +0800813 spice_server_set_uuid(spice_server, (unsigned char *)&qemu_uuid);
Marc-André Lureaud0638b12012-03-05 18:22:26 +0100814
Yonit Halperin8c957052012-08-21 11:51:59 +0300815 seamless_migration = qemu_opt_get_bool(opts, "seamless-migration", 0);
816 spice_server_set_seamless_migration(spice_server, seamless_migration);
Christophe Fergeau06bb8812016-01-12 15:08:58 +0100817 spice_server_set_sasl_appname(spice_server, "qemu");
Gongleife8e8322014-08-11 21:00:56 +0800818 if (spice_server_init(spice_server, &core_interface) != 0) {
Christophe Fergeau339a4752012-02-24 18:13:12 +0100819 error_report("failed to initialize spice server");
Gerd Hoffmannfba810f2011-06-15 13:11:33 +0200820 exit(1);
821 };
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300822 using_spice = 1;
Gerd Hoffmann864401c2010-03-11 11:13:28 -0300823
Steve Sistared9cda212023-06-07 07:42:34 -0700824 migration_add_notifier(&migration_state, migration_state_notifier);
Markus Armbruster3b5704b2015-03-05 09:30:16 +0100825 spice_migrate.base.sif = &migrate_interface.base;
Gerd Hoffmann05b53632020-10-19 09:52:15 +0200826 qemu_spice.add_interface(&spice_migrate.base);
Gerd Hoffmanne866e232010-04-23 13:28:21 +0200827
Gerd Hoffmann864401c2010-03-11 11:13:28 -0300828 qemu_spice_input_init();
Gerd Hoffmannc448e852010-03-11 11:13:32 -0300829
Gerd Hoffmann641381c2015-05-12 11:54:34 +0200830 qemu_spice_display_stop();
Yonit Halperinf5bb0392012-08-21 11:51:55 +0300831
Anthony Liguori7267c092011-08-20 22:09:37 -0500832 g_free(x509_key_file);
833 g_free(x509_cert_file);
834 g_free(x509_cacert_file);
Daniel P. Berrangé99522f62021-03-11 11:43:42 +0000835 g_free(password);
Marc-André Lureauafd0b402012-12-05 16:15:36 +0100836
Gerd Hoffmann474114b2015-10-13 15:39:34 +0200837#ifdef HAVE_SPICE_GL
838 if (qemu_opt_get_bool(opts, "gl", 0)) {
Christophe Fergeau569a93c2016-03-14 12:41:12 +0100839 if ((port != 0) || (tls_port != 0)) {
840 error_report("SPICE GL support is local-only for now and "
841 "incompatible with -spice port/tls-port");
842 exit(1);
843 }
Marc-André Lureau0e1be592023-02-14 16:11:24 +0400844 egl_init(qemu_opt_get(opts, "rendernode"), DISPLAYGL_MODE_ON, &error_fatal);
Gerd Hoffmannfe5c44f2017-06-06 13:06:18 +0200845 spice_opengl = 1;
Gerd Hoffmann474114b2015-10-13 15:39:34 +0200846 }
847#endif
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300848}
849
Gerd Hoffmann05b53632020-10-19 09:52:15 +0200850static int qemu_spice_add_interface(SpiceBaseInstance *sin)
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300851{
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200852 if (!spice_server) {
853 if (QTAILQ_FIRST(&qemu_spice_opts.head) != NULL) {
Christophe Fergeau339a4752012-02-24 18:13:12 +0100854 error_report("Oops: spice configured but not active");
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200855 exit(1);
856 }
857 /*
858 * Create a spice server instance.
859 * It does *not* listen on the network.
860 * It handles QXL local rendering only.
861 *
862 * With a command line like '-vnc :0 -vga qxl' you'll end up here.
863 */
864 spice_server = spice_server_new();
Christophe Fergeau764eb392013-10-16 17:52:33 +0200865 spice_server_set_sasl_appname(spice_server, "qemu");
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200866 spice_server_init(spice_server, &core_interface);
Stefan Hajnoczibfb82a22012-12-19 14:07:16 +0100867 qemu_add_vm_change_state_handler(vm_change_state_handler, NULL);
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +0200868 }
Yonit Halperin71d388d2012-08-21 11:51:56 +0300869
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300870 return spice_server_add_interface(spice_server, sin);
871}
872
Gerd Hoffmann9fa03282013-10-11 22:39:59 +0200873static GSList *spice_consoles;
Gerd Hoffmann9fa03282013-10-11 22:39:59 +0200874
875bool qemu_spice_have_display_interface(QemuConsole *con)
876{
877 if (g_slist_find(spice_consoles, con)) {
878 return true;
879 }
880 return false;
881}
882
883int qemu_spice_add_display_interface(QXLInstance *qxlin, QemuConsole *con)
884{
885 if (g_slist_find(spice_consoles, con)) {
886 return -1;
887 }
Gerd Hoffmanncd56cc62014-08-29 10:13:28 +0200888 qxlin->id = qemu_console_get_index(con);
Gerd Hoffmann9fa03282013-10-11 22:39:59 +0200889 spice_consoles = g_slist_append(spice_consoles, con);
890 return qemu_spice_add_interface(&qxlin->base);
891}
892
Gerd Hoffmann75721502010-10-07 12:22:54 +0200893static int qemu_spice_set_ticket(bool fail_if_conn, bool disconnect_if_conn)
894{
895 time_t lifetime, now = time(NULL);
896 char *passwd;
897
898 if (now < auth_expires) {
899 passwd = auth_passwd;
900 lifetime = (auth_expires - now);
901 if (lifetime > INT_MAX) {
902 lifetime = INT_MAX;
903 }
904 } else {
905 passwd = NULL;
906 lifetime = 1;
907 }
908 return spice_server_set_ticket(spice_server, passwd, lifetime,
909 fail_if_conn, disconnect_if_conn);
910}
911
Gerd Hoffmann08ad2622020-10-19 09:52:16 +0200912static int qemu_spice_set_passwd(const char *passwd,
913 bool fail_if_conn, bool disconnect_if_conn)
Gerd Hoffmann75721502010-10-07 12:22:54 +0200914{
Gerd Hoffmannb1ea7b72014-07-02 12:56:42 +0200915 if (strcmp(auth, "spice") != 0) {
916 return -1;
917 }
918
Markus Armbrusterfd3bea32013-01-22 11:08:00 +0100919 g_free(auth_passwd);
920 auth_passwd = g_strdup(passwd);
Gerd Hoffmann75721502010-10-07 12:22:54 +0200921 return qemu_spice_set_ticket(fail_if_conn, disconnect_if_conn);
922}
923
Gerd Hoffmann08ad2622020-10-19 09:52:16 +0200924static int qemu_spice_set_pw_expire(time_t expires)
Gerd Hoffmann75721502010-10-07 12:22:54 +0200925{
926 auth_expires = expires;
927 return qemu_spice_set_ticket(false, false);
928}
929
Gerd Hoffmann864a0242020-10-19 09:52:17 +0200930static int qemu_spice_display_add_client(int csock, int skipauth, int tls)
Daniel P. Berrangef1f5f402012-02-13 13:43:08 +0000931{
Marc-André Lureaue40283d2023-03-20 17:36:42 +0400932#ifdef WIN32
933 csock = qemu_close_socket_osfhandle(csock);
934#endif
Daniel P. Berrangef1f5f402012-02-13 13:43:08 +0000935 if (tls) {
936 return spice_server_add_ssl_client(spice_server, csock, skipauth);
937 } else {
938 return spice_server_add_client(spice_server, csock, skipauth);
939 }
Daniel P. Berrangef1f5f402012-02-13 13:43:08 +0000940}
941
Gerd Hoffmann7cc6a252013-12-09 15:54:46 +0100942void qemu_spice_display_start(void)
943{
Marc-André Lureau83f71802019-02-21 12:06:55 +0100944 if (spice_display_is_running) {
945 return;
946 }
947
Gerd Hoffmann7cc6a252013-12-09 15:54:46 +0100948 spice_display_is_running = true;
Gerd Hoffmannb50f3e42013-12-09 16:00:15 +0100949 spice_server_vm_start(spice_server);
Gerd Hoffmann7cc6a252013-12-09 15:54:46 +0100950}
951
952void qemu_spice_display_stop(void)
953{
Marc-André Lureau83f71802019-02-21 12:06:55 +0100954 if (!spice_display_is_running) {
955 return;
956 }
957
Gerd Hoffmannb50f3e42013-12-09 16:00:15 +0100958 spice_server_vm_stop(spice_server);
Gerd Hoffmann7cc6a252013-12-09 15:54:46 +0100959 spice_display_is_running = false;
960}
961
962int qemu_spice_display_is_running(SimpleSpiceDisplay *ssd)
963{
964 return spice_display_is_running;
965}
966
Gerd Hoffmann74774772020-10-19 09:52:12 +0200967static struct QemuSpiceOps real_spice_ops = {
Gerd Hoffmann63be30e2020-10-19 09:52:13 +0200968 .init = qemu_spice_init,
Gerd Hoffmannb192cd12020-10-19 09:52:14 +0200969 .display_init = qemu_spice_display_init,
Gerd Hoffmann74774772020-10-19 09:52:12 +0200970 .migrate_info = qemu_spice_migrate_info,
Gerd Hoffmann08ad2622020-10-19 09:52:16 +0200971 .set_passwd = qemu_spice_set_passwd,
972 .set_pw_expire = qemu_spice_set_pw_expire,
Gerd Hoffmann864a0242020-10-19 09:52:17 +0200973 .display_add_client = qemu_spice_display_add_client,
Gerd Hoffmann05b53632020-10-19 09:52:15 +0200974 .add_interface = qemu_spice_add_interface,
Gerd Hoffmanndb5732c2020-10-19 09:52:18 +0200975 .qmp_query = qmp_query_spice_real,
Gerd Hoffmann74774772020-10-19 09:52:12 +0200976};
977
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300978static void spice_register_config(void)
979{
Gerd Hoffmann74774772020-10-19 09:52:12 +0200980 qemu_spice = real_spice_ops;
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300981 qemu_add_opts(&qemu_spice_opts);
982}
Eduardo Habkost34294e22016-02-16 18:59:07 -0200983opts_init(spice_register_config);
Gerd Hoffmannb36ae1c2021-06-24 12:38:13 +0200984module_opts("spice");
985
Akihiko Odaki9a6c69d2021-07-14 14:57:35 +0900986#ifdef HAVE_SPICE_GL
Gerd Hoffmannb36ae1c2021-06-24 12:38:13 +0200987module_dep("ui-opengl");
988#endif