Statistics
| Branch: | Revision:

root / ui / spice-core.c @ bd5c51ee

History | View | Annotate | Download (25.6 kB)

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