Statistics
| Branch: | Revision:

root / ui / spice-core.c @ c448e855

History | View | Annotate | Download (6.7 kB)

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"
25
#include "qemu-x509.h"
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

    
140
/* functions for the rest of qemu */
141

    
142
void qemu_spice_init(void)
143
{
144
    QemuOpts *opts = QTAILQ_FIRST(&qemu_spice_opts.head);
145
    const char *password, *str, *x509_dir,
146
        *x509_key_password = NULL,
147
        *x509_dh_file = NULL,
148
        *tls_ciphers = NULL;
149
    char *x509_key_file = NULL,
150
        *x509_cert_file = NULL,
151
        *x509_cacert_file = NULL;
152
    int port, tls_port, len;
153

    
154
    if (!opts) {
155
        return;
156
    }
157
    port = qemu_opt_get_number(opts, "port", 0);
158
    tls_port = qemu_opt_get_number(opts, "tls-port", 0);
159
    if (!port && !tls_port) {
160
        return;
161
    }
162
    password = qemu_opt_get(opts, "password");
163

    
164
    if (tls_port) {
165
        x509_dir = qemu_opt_get(opts, "x509-dir");
166
        if (NULL == x509_dir) {
167
            x509_dir = ".";
168
        }
169
        len = strlen(x509_dir) + 32;
170

    
171
        str = qemu_opt_get(opts, "x509-key-file");
172
        if (str) {
173
            x509_key_file = qemu_strdup(str);
174
        } else {
175
            x509_key_file = qemu_malloc(len);
176
            snprintf(x509_key_file, len, "%s/%s", x509_dir, X509_SERVER_KEY_FILE);
177
        }
178

    
179
        str = qemu_opt_get(opts, "x509-cert-file");
180
        if (str) {
181
            x509_cert_file = qemu_strdup(str);
182
        } else {
183
            x509_cert_file = qemu_malloc(len);
184
            snprintf(x509_cert_file, len, "%s/%s", x509_dir, X509_SERVER_CERT_FILE);
185
        }
186

    
187
        str = qemu_opt_get(opts, "x509-cacert-file");
188
        if (str) {
189
            x509_cacert_file = qemu_strdup(str);
190
        } else {
191
            x509_cacert_file = qemu_malloc(len);
192
            snprintf(x509_cacert_file, len, "%s/%s", x509_dir, X509_CA_CERT_FILE);
193
        }
194

    
195
        x509_key_password = qemu_opt_get(opts, "x509-key-password");
196
        x509_dh_file = qemu_opt_get(opts, "x509-dh-file");
197
        tls_ciphers = qemu_opt_get(opts, "tls-ciphers");
198
    }
199

    
200
    spice_server = spice_server_new();
201
    if (port) {
202
        spice_server_set_port(spice_server, port);
203
    }
204
    if (tls_port) {
205
        spice_server_set_tls(spice_server, tls_port,
206
                             x509_cacert_file,
207
                             x509_cert_file,
208
                             x509_key_file,
209
                             x509_key_password,
210
                             x509_dh_file,
211
                             tls_ciphers);
212
    }
213
    if (password) {
214
        spice_server_set_ticket(spice_server, password, 0, 0, 0);
215
    }
216
    if (qemu_opt_get_bool(opts, "disable-ticketing", 0)) {
217
        spice_server_set_noauth(spice_server);
218
    }
219

    
220
    /* TODO: make configurable via cmdline */
221
    spice_server_set_image_compression(spice_server, SPICE_IMAGE_COMPRESS_AUTO_GLZ);
222

    
223
    spice_server_init(spice_server, &core_interface);
224
    using_spice = 1;
225

    
226
    qemu_spice_input_init();
227

    
228
    qemu_free(x509_key_file);
229
    qemu_free(x509_cert_file);
230
    qemu_free(x509_cacert_file);
231
}
232

    
233
int qemu_spice_add_interface(SpiceBaseInstance *sin)
234
{
235
    return spice_server_add_interface(spice_server, sin);
236
}
237

    
238
static void spice_register_config(void)
239
{
240
    qemu_add_opts(&qemu_spice_opts);
241
}
242
machine_init(spice_register_config);
243

    
244
static void spice_initialize(void)
245
{
246
    qemu_spice_init();
247
}
248
device_init(spice_initialize);