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