Statistics
| Branch: | Revision:

root / spice-qemu-char.c @ ee77854f

History | View | Annotate | Download (8.8 kB)

1 cbcc6336 Alon Levy
#include "config-host.h"
2 cbcc6336 Alon Levy
#include "trace.h"
3 cbcc6336 Alon Levy
#include "ui/qemu-spice.h"
4 dccfcd0e Paolo Bonzini
#include "sysemu/char.h"
5 cbcc6336 Alon Levy
#include <spice.h>
6 cbcc6336 Alon Levy
#include <spice-experimental.h>
7 5a49d3e9 Marc-André Lureau
#include <spice/protocol.h>
8 cbcc6336 Alon Levy
9 1de7afc9 Paolo Bonzini
#include "qemu/osdep.h"
10 cbcc6336 Alon Levy
11 cbcc6336 Alon Levy
typedef struct SpiceCharDriver {
12 cbcc6336 Alon Levy
    CharDriverState*      chr;
13 cbcc6336 Alon Levy
    SpiceCharDeviceInstance     sin;
14 cbcc6336 Alon Levy
    char                  *subtype;
15 cbcc6336 Alon Levy
    bool                  active;
16 ae893e5e Hans de Goede
    bool                  blocked;
17 b010cec8 Alon Levy
    const uint8_t         *datapos;
18 b010cec8 Alon Levy
    int                   datalen;
19 7a5448ce Marc-André Lureau
    QLIST_ENTRY(SpiceCharDriver) next;
20 cbcc6336 Alon Levy
} SpiceCharDriver;
21 cbcc6336 Alon Levy
22 ae893e5e Hans de Goede
typedef struct SpiceCharSource {
23 ae893e5e Hans de Goede
    GSource               source;
24 ae893e5e Hans de Goede
    SpiceCharDriver       *scd;
25 ae893e5e Hans de Goede
} SpiceCharSource;
26 ae893e5e Hans de Goede
27 7a5448ce Marc-André Lureau
static QLIST_HEAD(, SpiceCharDriver) spice_chars =
28 7a5448ce Marc-André Lureau
    QLIST_HEAD_INITIALIZER(spice_chars);
29 7a5448ce Marc-André Lureau
30 cbcc6336 Alon Levy
static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
31 cbcc6336 Alon Levy
{
32 cbcc6336 Alon Levy
    SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
33 cbcc6336 Alon Levy
    ssize_t out = 0;
34 cbcc6336 Alon Levy
    ssize_t last_out;
35 cbcc6336 Alon Levy
    uint8_t* p = (uint8_t*)buf;
36 cbcc6336 Alon Levy
37 cbcc6336 Alon Levy
    while (len > 0) {
38 75c439bc Hans de Goede
        int can_write = qemu_chr_be_can_write(scd->chr);
39 75c439bc Hans de Goede
        last_out = MIN(len, can_write);
40 07a54d70 Marc-André Lureau
        if (last_out <= 0) {
41 cbcc6336 Alon Levy
            break;
42 cbcc6336 Alon Levy
        }
43 fa5efccb Anthony Liguori
        qemu_chr_be_write(scd->chr, p, last_out);
44 35106c2d Hans de Goede
        out += last_out;
45 35106c2d Hans de Goede
        len -= last_out;
46 35106c2d Hans de Goede
        p += last_out;
47 cbcc6336 Alon Levy
    }
48 cbcc6336 Alon Levy
49 cbcc6336 Alon Levy
    trace_spice_vmc_write(out, len + out);
50 cbcc6336 Alon Levy
    return out;
51 cbcc6336 Alon Levy
}
52 cbcc6336 Alon Levy
53 cbcc6336 Alon Levy
static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)
54 cbcc6336 Alon Levy
{
55 cbcc6336 Alon Levy
    SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
56 cbcc6336 Alon Levy
    int bytes = MIN(len, scd->datalen);
57 cbcc6336 Alon Levy
58 cbcc6336 Alon Levy
    if (bytes > 0) {
59 cbcc6336 Alon Levy
        memcpy(buf, scd->datapos, bytes);
60 cbcc6336 Alon Levy
        scd->datapos += bytes;
61 cbcc6336 Alon Levy
        scd->datalen -= bytes;
62 cbcc6336 Alon Levy
        assert(scd->datalen >= 0);
63 ae893e5e Hans de Goede
    }
64 ae893e5e Hans de Goede
    if (scd->datalen == 0) {
65 ae893e5e Hans de Goede
        scd->datapos = 0;
66 ae893e5e Hans de Goede
        scd->blocked = false;
67 cbcc6336 Alon Levy
    }
68 cbcc6336 Alon Levy
    trace_spice_vmc_read(bytes, len);
69 cbcc6336 Alon Levy
    return bytes;
70 cbcc6336 Alon Levy
}
71 cbcc6336 Alon Levy
72 5a49d3e9 Marc-André Lureau
#if SPICE_SERVER_VERSION >= 0x000c02
73 5a49d3e9 Marc-André Lureau
static void vmc_event(SpiceCharDeviceInstance *sin, uint8_t event)
74 5a49d3e9 Marc-André Lureau
{
75 5a49d3e9 Marc-André Lureau
    SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
76 5a49d3e9 Marc-André Lureau
    int chr_event;
77 5a49d3e9 Marc-André Lureau
78 5a49d3e9 Marc-André Lureau
    switch (event) {
79 5a49d3e9 Marc-André Lureau
    case SPICE_PORT_EVENT_BREAK:
80 5a49d3e9 Marc-André Lureau
        chr_event = CHR_EVENT_BREAK;
81 5a49d3e9 Marc-André Lureau
        break;
82 5a49d3e9 Marc-André Lureau
    default:
83 5a49d3e9 Marc-André Lureau
        return;
84 5a49d3e9 Marc-André Lureau
    }
85 5a49d3e9 Marc-André Lureau
86 5a49d3e9 Marc-André Lureau
    trace_spice_vmc_event(chr_event);
87 5a49d3e9 Marc-André Lureau
    qemu_chr_be_event(scd->chr, chr_event);
88 5a49d3e9 Marc-André Lureau
}
89 5a49d3e9 Marc-André Lureau
#endif
90 5a49d3e9 Marc-André Lureau
91 f76e4c7f Hans de Goede
static void vmc_state(SpiceCharDeviceInstance *sin, int connected)
92 f76e4c7f Hans de Goede
{
93 f76e4c7f Hans de Goede
    SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
94 f76e4c7f Hans de Goede
95 16665b94 Hans de Goede
    if ((scd->chr->be_open && connected) ||
96 16665b94 Hans de Goede
        (!scd->chr->be_open && !connected)) {
97 f76e4c7f Hans de Goede
        return;
98 f76e4c7f Hans de Goede
    }
99 f76e4c7f Hans de Goede
100 f76e4c7f Hans de Goede
    qemu_chr_be_event(scd->chr,
101 f76e4c7f Hans de Goede
                      connected ? CHR_EVENT_OPENED : CHR_EVENT_CLOSED);
102 f76e4c7f Hans de Goede
}
103 f76e4c7f Hans de Goede
104 cbcc6336 Alon Levy
static SpiceCharDeviceInterface vmc_interface = {
105 cbcc6336 Alon Levy
    .base.type          = SPICE_INTERFACE_CHAR_DEVICE,
106 cbcc6336 Alon Levy
    .base.description   = "spice virtual channel char device",
107 cbcc6336 Alon Levy
    .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,
108 cbcc6336 Alon Levy
    .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,
109 f76e4c7f Hans de Goede
    .state              = vmc_state,
110 cbcc6336 Alon Levy
    .write              = vmc_write,
111 cbcc6336 Alon Levy
    .read               = vmc_read,
112 5a49d3e9 Marc-André Lureau
#if SPICE_SERVER_VERSION >= 0x000c02
113 5a49d3e9 Marc-André Lureau
    .event              = vmc_event,
114 5a49d3e9 Marc-André Lureau
#endif
115 cbcc6336 Alon Levy
};
116 cbcc6336 Alon Levy
117 cbcc6336 Alon Levy
118 cbcc6336 Alon Levy
static void vmc_register_interface(SpiceCharDriver *scd)
119 cbcc6336 Alon Levy
{
120 cbcc6336 Alon Levy
    if (scd->active) {
121 cbcc6336 Alon Levy
        return;
122 cbcc6336 Alon Levy
    }
123 cbcc6336 Alon Levy
    scd->sin.base.sif = &vmc_interface.base;
124 cbcc6336 Alon Levy
    qemu_spice_add_interface(&scd->sin.base);
125 cbcc6336 Alon Levy
    scd->active = true;
126 cbcc6336 Alon Levy
    trace_spice_vmc_register_interface(scd);
127 cbcc6336 Alon Levy
}
128 cbcc6336 Alon Levy
129 cbcc6336 Alon Levy
static void vmc_unregister_interface(SpiceCharDriver *scd)
130 cbcc6336 Alon Levy
{
131 cbcc6336 Alon Levy
    if (!scd->active) {
132 cbcc6336 Alon Levy
        return;
133 cbcc6336 Alon Levy
    }
134 cbcc6336 Alon Levy
    spice_server_remove_interface(&scd->sin.base);
135 cbcc6336 Alon Levy
    scd->active = false;
136 cbcc6336 Alon Levy
    trace_spice_vmc_unregister_interface(scd);
137 cbcc6336 Alon Levy
}
138 cbcc6336 Alon Levy
139 ae893e5e Hans de Goede
static gboolean spice_char_source_prepare(GSource *source, gint *timeout)
140 ae893e5e Hans de Goede
{
141 ae893e5e Hans de Goede
    SpiceCharSource *src = (SpiceCharSource *)source;
142 ae893e5e Hans de Goede
143 ae893e5e Hans de Goede
    *timeout = -1;
144 ae893e5e Hans de Goede
145 ae893e5e Hans de Goede
    return !src->scd->blocked;
146 ae893e5e Hans de Goede
}
147 ae893e5e Hans de Goede
148 ae893e5e Hans de Goede
static gboolean spice_char_source_check(GSource *source)
149 ae893e5e Hans de Goede
{
150 ae893e5e Hans de Goede
    SpiceCharSource *src = (SpiceCharSource *)source;
151 ae893e5e Hans de Goede
152 ae893e5e Hans de Goede
    return !src->scd->blocked;
153 ae893e5e Hans de Goede
}
154 ae893e5e Hans de Goede
155 ae893e5e Hans de Goede
static gboolean spice_char_source_dispatch(GSource *source,
156 ae893e5e Hans de Goede
    GSourceFunc callback, gpointer user_data)
157 ae893e5e Hans de Goede
{
158 ae893e5e Hans de Goede
    GIOFunc func = (GIOFunc)callback;
159 ae893e5e Hans de Goede
160 ae893e5e Hans de Goede
    return func(NULL, G_IO_OUT, user_data);
161 ae893e5e Hans de Goede
}
162 ae893e5e Hans de Goede
163 ae893e5e Hans de Goede
GSourceFuncs SpiceCharSourceFuncs = {
164 ae893e5e Hans de Goede
    .prepare  = spice_char_source_prepare,
165 ae893e5e Hans de Goede
    .check    = spice_char_source_check,
166 ae893e5e Hans de Goede
    .dispatch = spice_char_source_dispatch,
167 ae893e5e Hans de Goede
};
168 ae893e5e Hans de Goede
169 ae893e5e Hans de Goede
static GSource *spice_chr_add_watch(CharDriverState *chr, GIOCondition cond)
170 ae893e5e Hans de Goede
{
171 ae893e5e Hans de Goede
    SpiceCharDriver *scd = chr->opaque;
172 ae893e5e Hans de Goede
    SpiceCharSource *src;
173 ae893e5e Hans de Goede
174 ae893e5e Hans de Goede
    assert(cond == G_IO_OUT);
175 ae893e5e Hans de Goede
176 ae893e5e Hans de Goede
    src = (SpiceCharSource *)g_source_new(&SpiceCharSourceFuncs,
177 ae893e5e Hans de Goede
                                          sizeof(SpiceCharSource));
178 ae893e5e Hans de Goede
    src->scd = scd;
179 ae893e5e Hans de Goede
180 ae893e5e Hans de Goede
    return (GSource *)src;
181 ae893e5e Hans de Goede
}
182 cbcc6336 Alon Levy
183 cbcc6336 Alon Levy
static int spice_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
184 cbcc6336 Alon Levy
{
185 cbcc6336 Alon Levy
    SpiceCharDriver *s = chr->opaque;
186 ae893e5e Hans de Goede
    int read_bytes;
187 cbcc6336 Alon Levy
188 cbcc6336 Alon Levy
    assert(s->datalen == 0);
189 b010cec8 Alon Levy
    s->datapos = buf;
190 cbcc6336 Alon Levy
    s->datalen = len;
191 cbcc6336 Alon Levy
    spice_server_char_device_wakeup(&s->sin);
192 ae893e5e Hans de Goede
    read_bytes = len - s->datalen;
193 ae893e5e Hans de Goede
    if (read_bytes != len) {
194 ae893e5e Hans de Goede
        /* We'll get passed in the unconsumed data with the next call */
195 ae893e5e Hans de Goede
        s->datalen = 0;
196 ae893e5e Hans de Goede
        s->datapos = NULL;
197 ae893e5e Hans de Goede
        s->blocked = true;
198 ae893e5e Hans de Goede
    }
199 ae893e5e Hans de Goede
    return read_bytes;
200 cbcc6336 Alon Levy
}
201 cbcc6336 Alon Levy
202 cbcc6336 Alon Levy
static void spice_chr_close(struct CharDriverState *chr)
203 cbcc6336 Alon Levy
{
204 cbcc6336 Alon Levy
    SpiceCharDriver *s = chr->opaque;
205 cbcc6336 Alon Levy
206 cbcc6336 Alon Levy
    vmc_unregister_interface(s);
207 7a5448ce Marc-André Lureau
    QLIST_REMOVE(s, next);
208 5e9b473a Hans de Goede
209 5e9b473a Hans de Goede
    g_free((char *)s->sin.subtype);
210 5e9b473a Hans de Goede
#if SPICE_SERVER_VERSION >= 0x000c02
211 5e9b473a Hans de Goede
    g_free((char *)s->sin.portname);
212 5e9b473a Hans de Goede
#endif
213 7267c094 Anthony Liguori
    g_free(s);
214 cbcc6336 Alon Levy
}
215 cbcc6336 Alon Levy
216 574b711a Hans de Goede
static void spice_chr_set_fe_open(struct CharDriverState *chr, int fe_open)
217 cd8f7df2 Hans de Goede
{
218 cd8f7df2 Hans de Goede
    SpiceCharDriver *s = chr->opaque;
219 574b711a Hans de Goede
    if (fe_open) {
220 574b711a Hans de Goede
        vmc_register_interface(s);
221 574b711a Hans de Goede
    } else {
222 574b711a Hans de Goede
        vmc_unregister_interface(s);
223 574b711a Hans de Goede
    }
224 cd8f7df2 Hans de Goede
}
225 cd8f7df2 Hans de Goede
226 cbcc6336 Alon Levy
static void print_allowed_subtypes(void)
227 cbcc6336 Alon Levy
{
228 cbcc6336 Alon Levy
    const char** psubtype;
229 cbcc6336 Alon Levy
    int i;
230 cbcc6336 Alon Levy
231 cbcc6336 Alon Levy
    fprintf(stderr, "allowed names: ");
232 cbcc6336 Alon Levy
    for(i=0, psubtype = spice_server_char_device_recognized_subtypes();
233 cbcc6336 Alon Levy
        *psubtype != NULL; ++psubtype, ++i) {
234 cbcc6336 Alon Levy
        if (i == 0) {
235 cbcc6336 Alon Levy
            fprintf(stderr, "%s", *psubtype);
236 cbcc6336 Alon Levy
        } else {
237 cbcc6336 Alon Levy
            fprintf(stderr, ", %s", *psubtype);
238 cbcc6336 Alon Levy
        }
239 cbcc6336 Alon Levy
    }
240 cbcc6336 Alon Levy
    fprintf(stderr, "\n");
241 cbcc6336 Alon Levy
}
242 cbcc6336 Alon Levy
243 cd153e2a Gerd Hoffmann
static CharDriverState *chr_open(const char *subtype)
244 cbcc6336 Alon Levy
{
245 cbcc6336 Alon Levy
    CharDriverState *chr;
246 cbcc6336 Alon Levy
    SpiceCharDriver *s;
247 71b423f4 Marc-André Lureau
248 71b423f4 Marc-André Lureau
    chr = g_malloc0(sizeof(CharDriverState));
249 71b423f4 Marc-André Lureau
    s = g_malloc0(sizeof(SpiceCharDriver));
250 71b423f4 Marc-André Lureau
    s->chr = chr;
251 71b423f4 Marc-André Lureau
    s->active = false;
252 5e9b473a Hans de Goede
    s->sin.subtype = g_strdup(subtype);
253 71b423f4 Marc-André Lureau
    chr->opaque = s;
254 71b423f4 Marc-André Lureau
    chr->chr_write = spice_chr_write;
255 ae893e5e Hans de Goede
    chr->chr_add_watch = spice_chr_add_watch;
256 71b423f4 Marc-André Lureau
    chr->chr_close = spice_chr_close;
257 574b711a Hans de Goede
    chr->chr_set_fe_open = spice_chr_set_fe_open;
258 bd5c51ee Michael Roth
    chr->explicit_be_open = true;
259 71b423f4 Marc-André Lureau
260 7a5448ce Marc-André Lureau
    QLIST_INSERT_HEAD(&spice_chars, s, next);
261 7a5448ce Marc-André Lureau
262 71b423f4 Marc-André Lureau
    return chr;
263 71b423f4 Marc-André Lureau
}
264 71b423f4 Marc-André Lureau
265 cd153e2a Gerd Hoffmann
CharDriverState *qemu_chr_open_spice_vmc(const char *type)
266 71b423f4 Marc-André Lureau
{
267 71b423f4 Marc-André Lureau
    const char **psubtype = spice_server_char_device_recognized_subtypes();
268 cbcc6336 Alon Levy
269 cd153e2a Gerd Hoffmann
    if (type == NULL) {
270 cbcc6336 Alon Levy
        fprintf(stderr, "spice-qemu-char: missing name parameter\n");
271 cbcc6336 Alon Levy
        print_allowed_subtypes();
272 1f51470d Markus Armbruster
        return NULL;
273 cbcc6336 Alon Levy
    }
274 cd153e2a Gerd Hoffmann
    for (; *psubtype != NULL; ++psubtype) {
275 cd153e2a Gerd Hoffmann
        if (strcmp(type, *psubtype) == 0) {
276 cbcc6336 Alon Levy
            break;
277 cbcc6336 Alon Levy
        }
278 cbcc6336 Alon Levy
    }
279 cd153e2a Gerd Hoffmann
    if (*psubtype == NULL) {
280 cd153e2a Gerd Hoffmann
        fprintf(stderr, "spice-qemu-char: unsupported type: %s\n", type);
281 cbcc6336 Alon Levy
        print_allowed_subtypes();
282 1f51470d Markus Armbruster
        return NULL;
283 cbcc6336 Alon Levy
    }
284 cbcc6336 Alon Levy
285 52fe0e75 Hans de Goede
    return chr_open(type);
286 cbcc6336 Alon Levy
}
287 5a49d3e9 Marc-André Lureau
288 5a49d3e9 Marc-André Lureau
#if SPICE_SERVER_VERSION >= 0x000c02
289 cd153e2a Gerd Hoffmann
CharDriverState *qemu_chr_open_spice_port(const char *name)
290 5a49d3e9 Marc-André Lureau
{
291 5a49d3e9 Marc-André Lureau
    CharDriverState *chr;
292 5a49d3e9 Marc-André Lureau
    SpiceCharDriver *s;
293 5a49d3e9 Marc-André Lureau
294 5a49d3e9 Marc-André Lureau
    if (name == NULL) {
295 5a49d3e9 Marc-André Lureau
        fprintf(stderr, "spice-qemu-char: missing name parameter\n");
296 5a49d3e9 Marc-André Lureau
        return NULL;
297 5a49d3e9 Marc-André Lureau
    }
298 5a49d3e9 Marc-André Lureau
299 cd153e2a Gerd Hoffmann
    chr = chr_open("port");
300 5a49d3e9 Marc-André Lureau
    s = chr->opaque;
301 5e9b473a Hans de Goede
    s->sin.portname = g_strdup(name);
302 5a49d3e9 Marc-André Lureau
303 5a49d3e9 Marc-André Lureau
    return chr;
304 5a49d3e9 Marc-André Lureau
}
305 afd0b409 Marc-André Lureau
306 afd0b409 Marc-André Lureau
void qemu_spice_register_ports(void)
307 afd0b409 Marc-André Lureau
{
308 afd0b409 Marc-André Lureau
    SpiceCharDriver *s;
309 afd0b409 Marc-André Lureau
310 afd0b409 Marc-André Lureau
    QLIST_FOREACH(s, &spice_chars, next) {
311 afd0b409 Marc-André Lureau
        if (s->sin.portname == NULL) {
312 afd0b409 Marc-André Lureau
            continue;
313 afd0b409 Marc-André Lureau
        }
314 afd0b409 Marc-André Lureau
        vmc_register_interface(s);
315 afd0b409 Marc-André Lureau
    }
316 afd0b409 Marc-André Lureau
}
317 5a49d3e9 Marc-André Lureau
#endif
318 26c60614 Anthony Liguori
319 cd153e2a Gerd Hoffmann
static void qemu_chr_parse_spice_vmc(QemuOpts *opts, ChardevBackend *backend,
320 cd153e2a Gerd Hoffmann
                                     Error **errp)
321 cd153e2a Gerd Hoffmann
{
322 cd153e2a Gerd Hoffmann
    const char *name = qemu_opt_get(opts, "name");
323 cd153e2a Gerd Hoffmann
324 cd153e2a Gerd Hoffmann
    if (name == NULL) {
325 cd153e2a Gerd Hoffmann
        error_setg(errp, "chardev: spice channel: no name given");
326 cd153e2a Gerd Hoffmann
        return;
327 cd153e2a Gerd Hoffmann
    }
328 cd153e2a Gerd Hoffmann
    backend->spicevmc = g_new0(ChardevSpiceChannel, 1);
329 cd153e2a Gerd Hoffmann
    backend->spicevmc->type = g_strdup(name);
330 cd153e2a Gerd Hoffmann
}
331 cd153e2a Gerd Hoffmann
332 cd153e2a Gerd Hoffmann
static void qemu_chr_parse_spice_port(QemuOpts *opts, ChardevBackend *backend,
333 cd153e2a Gerd Hoffmann
                                      Error **errp)
334 cd153e2a Gerd Hoffmann
{
335 cd153e2a Gerd Hoffmann
    const char *name = qemu_opt_get(opts, "name");
336 cd153e2a Gerd Hoffmann
337 cd153e2a Gerd Hoffmann
    if (name == NULL) {
338 cd153e2a Gerd Hoffmann
        error_setg(errp, "chardev: spice port: no name given");
339 cd153e2a Gerd Hoffmann
        return;
340 cd153e2a Gerd Hoffmann
    }
341 cd153e2a Gerd Hoffmann
    backend->spiceport = g_new0(ChardevSpicePort, 1);
342 cd153e2a Gerd Hoffmann
    backend->spiceport->fqdn = g_strdup(name);
343 cd153e2a Gerd Hoffmann
}
344 cd153e2a Gerd Hoffmann
345 26c60614 Anthony Liguori
static void register_types(void)
346 26c60614 Anthony Liguori
{
347 cd153e2a Gerd Hoffmann
    register_char_driver_qapi("spicevmc", CHARDEV_BACKEND_KIND_SPICEVMC,
348 cd153e2a Gerd Hoffmann
                              qemu_chr_parse_spice_vmc);
349 cd153e2a Gerd Hoffmann
    register_char_driver_qapi("spiceport", CHARDEV_BACKEND_KIND_SPICEPORT,
350 cd153e2a Gerd Hoffmann
                              qemu_chr_parse_spice_port);
351 26c60614 Anthony Liguori
}
352 26c60614 Anthony Liguori
353 26c60614 Anthony Liguori
type_init(register_types);