]>
Commit | Line | Data |
---|---|---|
29b0040b GH |
1 | /* |
2 | * Copyright (C) 2010 Red Hat, Inc. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License as | |
6 | * published by the Free Software Foundation; either version 2 or | |
7 | * (at your option) version 3 of the License. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
17 | ||
18 | #include <spice.h> | |
19 | #include <spice-experimental.h> | |
20 | ||
21 | #include "qemu-common.h" | |
22 | #include "qemu-spice.h" | |
23 | #include "qemu-timer.h" | |
24 | #include "qemu-queue.h" | |
c448e855 | 25 | #include "qemu-x509.h" |
29b0040b GH |
26 | #include "monitor.h" |
27 | ||
28 | /* core bits */ | |
29 | ||
30 | static SpiceServer *spice_server; | |
31 | int using_spice = 0; | |
32 | ||
33 | struct SpiceTimer { | |
34 | QEMUTimer *timer; | |
35 | QTAILQ_ENTRY(SpiceTimer) next; | |
36 | }; | |
37 | static QTAILQ_HEAD(, SpiceTimer) timers = QTAILQ_HEAD_INITIALIZER(timers); | |
38 | ||
39 | static SpiceTimer *timer_add(SpiceTimerFunc func, void *opaque) | |
40 | { | |
41 | SpiceTimer *timer; | |
42 | ||
43 | timer = qemu_mallocz(sizeof(*timer)); | |
44 | timer->timer = qemu_new_timer(rt_clock, func, opaque); | |
45 | QTAILQ_INSERT_TAIL(&timers, timer, next); | |
46 | return timer; | |
47 | } | |
48 | ||
49 | static void timer_start(SpiceTimer *timer, uint32_t ms) | |
50 | { | |
51 | qemu_mod_timer(timer->timer, qemu_get_clock(rt_clock) + ms); | |
52 | } | |
53 | ||
54 | static void timer_cancel(SpiceTimer *timer) | |
55 | { | |
56 | qemu_del_timer(timer->timer); | |
57 | } | |
58 | ||
59 | static void timer_remove(SpiceTimer *timer) | |
60 | { | |
61 | qemu_del_timer(timer->timer); | |
62 | qemu_free_timer(timer->timer); | |
63 | QTAILQ_REMOVE(&timers, timer, next); | |
64 | qemu_free(timer); | |
65 | } | |
66 | ||
67 | struct SpiceWatch { | |
68 | int fd; | |
69 | int event_mask; | |
70 | SpiceWatchFunc func; | |
71 | void *opaque; | |
72 | QTAILQ_ENTRY(SpiceWatch) next; | |
73 | }; | |
74 | static QTAILQ_HEAD(, SpiceWatch) watches = QTAILQ_HEAD_INITIALIZER(watches); | |
75 | ||
76 | static void watch_read(void *opaque) | |
77 | { | |
78 | SpiceWatch *watch = opaque; | |
79 | watch->func(watch->fd, SPICE_WATCH_EVENT_READ, watch->opaque); | |
80 | } | |
81 | ||
82 | static void watch_write(void *opaque) | |
83 | { | |
84 | SpiceWatch *watch = opaque; | |
85 | watch->func(watch->fd, SPICE_WATCH_EVENT_WRITE, watch->opaque); | |
86 | } | |
87 | ||
88 | static void watch_update_mask(SpiceWatch *watch, int event_mask) | |
89 | { | |
90 | IOHandler *on_read = NULL; | |
91 | IOHandler *on_write = NULL; | |
92 | ||
93 | watch->event_mask = event_mask; | |
94 | if (watch->event_mask & SPICE_WATCH_EVENT_READ) { | |
95 | on_read = watch_read; | |
96 | } | |
97 | if (watch->event_mask & SPICE_WATCH_EVENT_WRITE) { | |
98 | on_read = watch_write; | |
99 | } | |
100 | qemu_set_fd_handler(watch->fd, on_read, on_write, watch); | |
101 | } | |
102 | ||
103 | static SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque) | |
104 | { | |
105 | SpiceWatch *watch; | |
106 | ||
107 | watch = qemu_mallocz(sizeof(*watch)); | |
108 | watch->fd = fd; | |
109 | watch->func = func; | |
110 | watch->opaque = opaque; | |
111 | QTAILQ_INSERT_TAIL(&watches, watch, next); | |
112 | ||
113 | watch_update_mask(watch, event_mask); | |
114 | return watch; | |
115 | } | |
116 | ||
117 | static void watch_remove(SpiceWatch *watch) | |
118 | { | |
119 | watch_update_mask(watch, 0); | |
120 | QTAILQ_REMOVE(&watches, watch, next); | |
121 | qemu_free(watch); | |
122 | } | |
123 | ||
124 | static SpiceCoreInterface core_interface = { | |
125 | .base.type = SPICE_INTERFACE_CORE, | |
126 | .base.description = "qemu core services", | |
127 | .base.major_version = SPICE_INTERFACE_CORE_MAJOR, | |
128 | .base.minor_version = SPICE_INTERFACE_CORE_MINOR, | |
129 | ||
130 | .timer_add = timer_add, | |
131 | .timer_start = timer_start, | |
132 | .timer_cancel = timer_cancel, | |
133 | .timer_remove = timer_remove, | |
134 | ||
135 | .watch_add = watch_add, | |
136 | .watch_update_mask = watch_update_mask, | |
137 | .watch_remove = watch_remove, | |
138 | }; | |
139 | ||
9f04e09e YH |
140 | /* config string parsing */ |
141 | ||
142 | static int name2enum(const char *string, const char *table[], int entries) | |
143 | { | |
144 | int i; | |
145 | ||
146 | if (string) { | |
147 | for (i = 0; i < entries; i++) { | |
148 | if (!table[i]) { | |
149 | continue; | |
150 | } | |
151 | if (strcmp(string, table[i]) != 0) { | |
152 | continue; | |
153 | } | |
154 | return i; | |
155 | } | |
156 | } | |
157 | return -1; | |
158 | } | |
159 | ||
160 | static int parse_name(const char *string, const char *optname, | |
161 | const char *table[], int entries) | |
162 | { | |
163 | int value = name2enum(string, table, entries); | |
164 | ||
165 | if (value != -1) { | |
166 | return value; | |
167 | } | |
168 | fprintf(stderr, "spice: invalid %s: %s\n", optname, string); | |
169 | exit(1); | |
170 | } | |
171 | ||
172 | static const char *compression_names[] = { | |
173 | [ SPICE_IMAGE_COMPRESS_OFF ] = "off", | |
174 | [ SPICE_IMAGE_COMPRESS_AUTO_GLZ ] = "auto_glz", | |
175 | [ SPICE_IMAGE_COMPRESS_AUTO_LZ ] = "auto_lz", | |
176 | [ SPICE_IMAGE_COMPRESS_QUIC ] = "quic", | |
177 | [ SPICE_IMAGE_COMPRESS_GLZ ] = "glz", | |
178 | [ SPICE_IMAGE_COMPRESS_LZ ] = "lz", | |
179 | }; | |
180 | #define parse_compression(_name) \ | |
181 | parse_name(_name, "image compression", \ | |
182 | compression_names, ARRAY_SIZE(compression_names)) | |
183 | ||
184 | static const char *wan_compression_names[] = { | |
185 | [ SPICE_WAN_COMPRESSION_AUTO ] = "auto", | |
186 | [ SPICE_WAN_COMPRESSION_NEVER ] = "never", | |
187 | [ SPICE_WAN_COMPRESSION_ALWAYS ] = "always", | |
188 | }; | |
189 | #define parse_wan_compression(_name) \ | |
190 | parse_name(_name, "wan compression", \ | |
191 | wan_compression_names, ARRAY_SIZE(wan_compression_names)) | |
192 | ||
29b0040b GH |
193 | /* functions for the rest of qemu */ |
194 | ||
17b6dea0 GH |
195 | static int add_channel(const char *name, const char *value, void *opaque) |
196 | { | |
197 | int security = 0; | |
198 | int rc; | |
199 | ||
200 | if (strcmp(name, "tls-channel") == 0) { | |
201 | security = SPICE_CHANNEL_SECURITY_SSL; | |
202 | } | |
203 | if (strcmp(name, "plaintext-channel") == 0) { | |
204 | security = SPICE_CHANNEL_SECURITY_NONE; | |
205 | } | |
206 | if (security == 0) { | |
207 | return 0; | |
208 | } | |
209 | if (strcmp(value, "default") == 0) { | |
210 | rc = spice_server_set_channel_security(spice_server, NULL, security); | |
211 | } else { | |
212 | rc = spice_server_set_channel_security(spice_server, value, security); | |
213 | } | |
214 | if (rc != 0) { | |
215 | fprintf(stderr, "spice: failed to set channel security for %s\n", value); | |
216 | exit(1); | |
217 | } | |
218 | return 0; | |
219 | } | |
220 | ||
29b0040b GH |
221 | void qemu_spice_init(void) |
222 | { | |
223 | QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head); | |
c448e855 GH |
224 | const char *password, *str, *x509_dir, |
225 | *x509_key_password = NULL, | |
226 | *x509_dh_file = NULL, | |
227 | *tls_ciphers = NULL; | |
228 | char *x509_key_file = NULL, | |
229 | *x509_cert_file = NULL, | |
230 | *x509_cacert_file = NULL; | |
231 | int port, tls_port, len; | |
9f04e09e YH |
232 | spice_image_compression_t compression; |
233 | spice_wan_compression_t wan_compr; | |
29b0040b GH |
234 | |
235 | if (!opts) { | |
236 | return; | |
237 | } | |
238 | port = qemu_opt_get_number(opts, "port", 0); | |
c448e855 GH |
239 | tls_port = qemu_opt_get_number(opts, "tls-port", 0); |
240 | if (!port && !tls_port) { | |
29b0040b GH |
241 | return; |
242 | } | |
243 | password = qemu_opt_get(opts, "password"); | |
244 | ||
c448e855 GH |
245 | if (tls_port) { |
246 | x509_dir = qemu_opt_get(opts, "x509-dir"); | |
247 | if (NULL == x509_dir) { | |
248 | x509_dir = "."; | |
249 | } | |
250 | len = strlen(x509_dir) + 32; | |
251 | ||
252 | str = qemu_opt_get(opts, "x509-key-file"); | |
253 | if (str) { | |
254 | x509_key_file = qemu_strdup(str); | |
255 | } else { | |
256 | x509_key_file = qemu_malloc(len); | |
257 | snprintf(x509_key_file, len, "%s/%s", x509_dir, X509_SERVER_KEY_FILE); | |
258 | } | |
259 | ||
260 | str = qemu_opt_get(opts, "x509-cert-file"); | |
261 | if (str) { | |
262 | x509_cert_file = qemu_strdup(str); | |
263 | } else { | |
264 | x509_cert_file = qemu_malloc(len); | |
265 | snprintf(x509_cert_file, len, "%s/%s", x509_dir, X509_SERVER_CERT_FILE); | |
266 | } | |
267 | ||
268 | str = qemu_opt_get(opts, "x509-cacert-file"); | |
269 | if (str) { | |
270 | x509_cacert_file = qemu_strdup(str); | |
271 | } else { | |
272 | x509_cacert_file = qemu_malloc(len); | |
273 | snprintf(x509_cacert_file, len, "%s/%s", x509_dir, X509_CA_CERT_FILE); | |
274 | } | |
275 | ||
276 | x509_key_password = qemu_opt_get(opts, "x509-key-password"); | |
277 | x509_dh_file = qemu_opt_get(opts, "x509-dh-file"); | |
278 | tls_ciphers = qemu_opt_get(opts, "tls-ciphers"); | |
279 | } | |
280 | ||
29b0040b | 281 | spice_server = spice_server_new(); |
c448e855 GH |
282 | if (port) { |
283 | spice_server_set_port(spice_server, port); | |
284 | } | |
285 | if (tls_port) { | |
286 | spice_server_set_tls(spice_server, tls_port, | |
287 | x509_cacert_file, | |
288 | x509_cert_file, | |
289 | x509_key_file, | |
290 | x509_key_password, | |
291 | x509_dh_file, | |
292 | tls_ciphers); | |
293 | } | |
29b0040b GH |
294 | if (password) { |
295 | spice_server_set_ticket(spice_server, password, 0, 0, 0); | |
296 | } | |
297 | if (qemu_opt_get_bool(opts, "disable-ticketing", 0)) { | |
298 | spice_server_set_noauth(spice_server); | |
299 | } | |
300 | ||
9f04e09e YH |
301 | compression = SPICE_IMAGE_COMPRESS_AUTO_GLZ; |
302 | str = qemu_opt_get(opts, "image-compression"); | |
303 | if (str) { | |
304 | compression = parse_compression(str); | |
305 | } | |
306 | spice_server_set_image_compression(spice_server, compression); | |
307 | ||
308 | wan_compr = SPICE_WAN_COMPRESSION_AUTO; | |
309 | str = qemu_opt_get(opts, "jpeg-wan-compression"); | |
310 | if (str) { | |
311 | wan_compr = parse_wan_compression(str); | |
312 | } | |
313 | spice_server_set_jpeg_compression(spice_server, wan_compr); | |
314 | ||
315 | wan_compr = SPICE_WAN_COMPRESSION_AUTO; | |
316 | str = qemu_opt_get(opts, "zlib-glz-wan-compression"); | |
317 | if (str) { | |
318 | wan_compr = parse_wan_compression(str); | |
319 | } | |
320 | spice_server_set_zlib_glz_compression(spice_server, wan_compr); | |
29b0040b | 321 | |
17b6dea0 GH |
322 | qemu_opt_foreach(opts, add_channel, NULL, 0); |
323 | ||
29b0040b GH |
324 | spice_server_init(spice_server, &core_interface); |
325 | using_spice = 1; | |
864401c2 GH |
326 | |
327 | qemu_spice_input_init(); | |
c448e855 GH |
328 | |
329 | qemu_free(x509_key_file); | |
330 | qemu_free(x509_cert_file); | |
331 | qemu_free(x509_cacert_file); | |
29b0040b GH |
332 | } |
333 | ||
334 | int qemu_spice_add_interface(SpiceBaseInstance *sin) | |
335 | { | |
336 | return spice_server_add_interface(spice_server, sin); | |
337 | } | |
338 | ||
339 | static void spice_register_config(void) | |
340 | { | |
341 | qemu_add_opts(&qemu_spice_opts); | |
342 | } | |
343 | machine_init(spice_register_config); | |
344 | ||
345 | static void spice_initialize(void) | |
346 | { | |
347 | qemu_spice_init(); | |
348 | } | |
349 | device_init(spice_initialize); |