Statistics
| Branch: | Revision:

root / spice-qemu-char.c @ d8a03a09

History | View | Annotate | Download (8.8 kB)

1
#include "config-host.h"
2
#include "trace.h"
3
#include "ui/qemu-spice.h"
4
#include "sysemu/char.h"
5
#include <spice.h>
6
#include <spice-experimental.h>
7
#include <spice/protocol.h>
8

    
9
#include "qemu/osdep.h"
10

    
11
typedef struct SpiceCharDriver {
12
    CharDriverState*      chr;
13
    SpiceCharDeviceInstance     sin;
14
    bool                  active;
15
    bool                  blocked;
16
    const uint8_t         *datapos;
17
    int                   datalen;
18
    QLIST_ENTRY(SpiceCharDriver) next;
19
} SpiceCharDriver;
20

    
21
typedef struct SpiceCharSource {
22
    GSource               source;
23
    SpiceCharDriver       *scd;
24
} SpiceCharSource;
25

    
26
static QLIST_HEAD(, SpiceCharDriver) spice_chars =
27
    QLIST_HEAD_INITIALIZER(spice_chars);
28

    
29
static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
30
{
31
    SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
32
    ssize_t out = 0;
33
    ssize_t last_out;
34
    uint8_t* p = (uint8_t*)buf;
35

    
36
    while (len > 0) {
37
        int can_write = qemu_chr_be_can_write(scd->chr);
38
        last_out = MIN(len, can_write);
39
        if (last_out <= 0) {
40
            break;
41
        }
42
        qemu_chr_be_write(scd->chr, p, last_out);
43
        out += last_out;
44
        len -= last_out;
45
        p += last_out;
46
    }
47

    
48
    trace_spice_vmc_write(out, len + out);
49
    return out;
50
}
51

    
52
static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)
53
{
54
    SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
55
    int bytes = MIN(len, scd->datalen);
56

    
57
    if (bytes > 0) {
58
        memcpy(buf, scd->datapos, bytes);
59
        scd->datapos += bytes;
60
        scd->datalen -= bytes;
61
        assert(scd->datalen >= 0);
62
    }
63
    if (scd->datalen == 0) {
64
        scd->datapos = 0;
65
        scd->blocked = false;
66
    }
67
    trace_spice_vmc_read(bytes, len);
68
    return bytes;
69
}
70

    
71
#if SPICE_SERVER_VERSION >= 0x000c02
72
static void vmc_event(SpiceCharDeviceInstance *sin, uint8_t event)
73
{
74
    SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
75
    int chr_event;
76

    
77
    switch (event) {
78
    case SPICE_PORT_EVENT_BREAK:
79
        chr_event = CHR_EVENT_BREAK;
80
        break;
81
    default:
82
        return;
83
    }
84

    
85
    trace_spice_vmc_event(chr_event);
86
    qemu_chr_be_event(scd->chr, chr_event);
87
}
88
#endif
89

    
90
static void vmc_state(SpiceCharDeviceInstance *sin, int connected)
91
{
92
    SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
93

    
94
    if ((scd->chr->be_open && connected) ||
95
        (!scd->chr->be_open && !connected)) {
96
        return;
97
    }
98

    
99
    qemu_chr_be_event(scd->chr,
100
                      connected ? CHR_EVENT_OPENED : CHR_EVENT_CLOSED);
101
}
102

    
103
static SpiceCharDeviceInterface vmc_interface = {
104
    .base.type          = SPICE_INTERFACE_CHAR_DEVICE,
105
    .base.description   = "spice virtual channel char device",
106
    .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,
107
    .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,
108
    .state              = vmc_state,
109
    .write              = vmc_write,
110
    .read               = vmc_read,
111
#if SPICE_SERVER_VERSION >= 0x000c02
112
    .event              = vmc_event,
113
#endif
114
};
115

    
116

    
117
static void vmc_register_interface(SpiceCharDriver *scd)
118
{
119
    if (scd->active) {
120
        return;
121
    }
122
    scd->sin.base.sif = &vmc_interface.base;
123
    qemu_spice_add_interface(&scd->sin.base);
124
    scd->active = true;
125
    trace_spice_vmc_register_interface(scd);
126
}
127

    
128
static void vmc_unregister_interface(SpiceCharDriver *scd)
129
{
130
    if (!scd->active) {
131
        return;
132
    }
133
    spice_server_remove_interface(&scd->sin.base);
134
    scd->active = false;
135
    trace_spice_vmc_unregister_interface(scd);
136
}
137

    
138
static gboolean spice_char_source_prepare(GSource *source, gint *timeout)
139
{
140
    SpiceCharSource *src = (SpiceCharSource *)source;
141

    
142
    *timeout = -1;
143

    
144
    return !src->scd->blocked;
145
}
146

    
147
static gboolean spice_char_source_check(GSource *source)
148
{
149
    SpiceCharSource *src = (SpiceCharSource *)source;
150

    
151
    return !src->scd->blocked;
152
}
153

    
154
static gboolean spice_char_source_dispatch(GSource *source,
155
    GSourceFunc callback, gpointer user_data)
156
{
157
    GIOFunc func = (GIOFunc)callback;
158

    
159
    return func(NULL, G_IO_OUT, user_data);
160
}
161

    
162
GSourceFuncs SpiceCharSourceFuncs = {
163
    .prepare  = spice_char_source_prepare,
164
    .check    = spice_char_source_check,
165
    .dispatch = spice_char_source_dispatch,
166
};
167

    
168
static GSource *spice_chr_add_watch(CharDriverState *chr, GIOCondition cond)
169
{
170
    SpiceCharDriver *scd = chr->opaque;
171
    SpiceCharSource *src;
172

    
173
    assert(cond == G_IO_OUT);
174

    
175
    src = (SpiceCharSource *)g_source_new(&SpiceCharSourceFuncs,
176
                                          sizeof(SpiceCharSource));
177
    src->scd = scd;
178

    
179
    return (GSource *)src;
180
}
181

    
182
static int spice_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
183
{
184
    SpiceCharDriver *s = chr->opaque;
185
    int read_bytes;
186

    
187
    assert(s->datalen == 0);
188
    s->datapos = buf;
189
    s->datalen = len;
190
    spice_server_char_device_wakeup(&s->sin);
191
    read_bytes = len - s->datalen;
192
    if (read_bytes != len) {
193
        /* We'll get passed in the unconsumed data with the next call */
194
        s->datalen = 0;
195
        s->datapos = NULL;
196
        s->blocked = true;
197
    }
198
    return read_bytes;
199
}
200

    
201
static void spice_chr_close(struct CharDriverState *chr)
202
{
203
    SpiceCharDriver *s = chr->opaque;
204

    
205
    vmc_unregister_interface(s);
206
    QLIST_REMOVE(s, next);
207

    
208
    g_free((char *)s->sin.subtype);
209
#if SPICE_SERVER_VERSION >= 0x000c02
210
    g_free((char *)s->sin.portname);
211
#endif
212
    g_free(s);
213
}
214

    
215
static void spice_chr_set_fe_open(struct CharDriverState *chr, int fe_open)
216
{
217
    SpiceCharDriver *s = chr->opaque;
218
    if (fe_open) {
219
        vmc_register_interface(s);
220
    } else {
221
        vmc_unregister_interface(s);
222
    }
223
}
224

    
225
static void print_allowed_subtypes(void)
226
{
227
    const char** psubtype;
228
    int i;
229

    
230
    fprintf(stderr, "allowed names: ");
231
    for(i=0, psubtype = spice_server_char_device_recognized_subtypes();
232
        *psubtype != NULL; ++psubtype, ++i) {
233
        if (i == 0) {
234
            fprintf(stderr, "%s", *psubtype);
235
        } else {
236
            fprintf(stderr, ", %s", *psubtype);
237
        }
238
    }
239
    fprintf(stderr, "\n");
240
}
241

    
242
static CharDriverState *chr_open(const char *subtype)
243
{
244
    CharDriverState *chr;
245
    SpiceCharDriver *s;
246

    
247
    chr = g_malloc0(sizeof(CharDriverState));
248
    s = g_malloc0(sizeof(SpiceCharDriver));
249
    s->chr = chr;
250
    s->active = false;
251
    s->sin.subtype = g_strdup(subtype);
252
    chr->opaque = s;
253
    chr->chr_write = spice_chr_write;
254
    chr->chr_add_watch = spice_chr_add_watch;
255
    chr->chr_close = spice_chr_close;
256
    chr->chr_set_fe_open = spice_chr_set_fe_open;
257
    chr->explicit_be_open = true;
258

    
259
    QLIST_INSERT_HEAD(&spice_chars, s, next);
260

    
261
    return chr;
262
}
263

    
264
CharDriverState *qemu_chr_open_spice_vmc(const char *type)
265
{
266
    const char **psubtype = spice_server_char_device_recognized_subtypes();
267

    
268
    if (type == NULL) {
269
        fprintf(stderr, "spice-qemu-char: missing name parameter\n");
270
        print_allowed_subtypes();
271
        return NULL;
272
    }
273
    for (; *psubtype != NULL; ++psubtype) {
274
        if (strcmp(type, *psubtype) == 0) {
275
            break;
276
        }
277
    }
278
    if (*psubtype == NULL) {
279
        fprintf(stderr, "spice-qemu-char: unsupported type: %s\n", type);
280
        print_allowed_subtypes();
281
        return NULL;
282
    }
283

    
284
    return chr_open(type);
285
}
286

    
287
#if SPICE_SERVER_VERSION >= 0x000c02
288
CharDriverState *qemu_chr_open_spice_port(const char *name)
289
{
290
    CharDriverState *chr;
291
    SpiceCharDriver *s;
292

    
293
    if (name == NULL) {
294
        fprintf(stderr, "spice-qemu-char: missing name parameter\n");
295
        return NULL;
296
    }
297

    
298
    chr = chr_open("port");
299
    s = chr->opaque;
300
    s->sin.portname = g_strdup(name);
301

    
302
    return chr;
303
}
304

    
305
void qemu_spice_register_ports(void)
306
{
307
    SpiceCharDriver *s;
308

    
309
    QLIST_FOREACH(s, &spice_chars, next) {
310
        if (s->sin.portname == NULL) {
311
            continue;
312
        }
313
        vmc_register_interface(s);
314
    }
315
}
316
#endif
317

    
318
static void qemu_chr_parse_spice_vmc(QemuOpts *opts, ChardevBackend *backend,
319
                                     Error **errp)
320
{
321
    const char *name = qemu_opt_get(opts, "name");
322

    
323
    if (name == NULL) {
324
        error_setg(errp, "chardev: spice channel: no name given");
325
        return;
326
    }
327
    backend->spicevmc = g_new0(ChardevSpiceChannel, 1);
328
    backend->spicevmc->type = g_strdup(name);
329
}
330

    
331
static void qemu_chr_parse_spice_port(QemuOpts *opts, ChardevBackend *backend,
332
                                      Error **errp)
333
{
334
    const char *name = qemu_opt_get(opts, "name");
335

    
336
    if (name == NULL) {
337
        error_setg(errp, "chardev: spice port: no name given");
338
        return;
339
    }
340
    backend->spiceport = g_new0(ChardevSpicePort, 1);
341
    backend->spiceport->fqdn = g_strdup(name);
342
}
343

    
344
static void register_types(void)
345
{
346
    register_char_driver_qapi("spicevmc", CHARDEV_BACKEND_KIND_SPICEVMC,
347
                              qemu_chr_parse_spice_vmc);
348
    register_char_driver_qapi("spiceport", CHARDEV_BACKEND_KIND_SPICEPORT,
349
                              qemu_chr_parse_spice_port);
350
}
351

    
352
type_init(register_types);