Statistics
| Branch: | Revision:

root / spice-qemu-char.c @ 07a54d70

History | View | Annotate | Download (6.7 kB)

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

    
7
#include "osdep.h"
8

    
9
#define dprintf(_scd, _level, _fmt, ...)                                \
10
    do {                                                                \
11
        static unsigned __dprintf_counter = 0;                          \
12
        if (_scd->debug >= _level) {                                    \
13
            fprintf(stderr, "scd: %3d: " _fmt, ++__dprintf_counter, ## __VA_ARGS__);\
14
        }                                                               \
15
    } while (0)
16

    
17
typedef struct SpiceCharDriver {
18
    CharDriverState*      chr;
19
    SpiceCharDeviceInstance     sin;
20
    char                  *subtype;
21
    bool                  active;
22
    uint8_t               *buffer;
23
    uint8_t               *datapos;
24
    ssize_t               bufsize, datalen;
25
    uint32_t              debug;
26
} SpiceCharDriver;
27

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

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

    
46
    dprintf(scd, 3, "%s: %zu/%zd\n", __func__, out, len + out);
47
    trace_spice_vmc_write(out, len + out);
48
    return out;
49
}
50

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

    
56
    dprintf(scd, 2, "%s: %p %d/%d/%zd\n", __func__, scd->datapos, len, bytes, scd->datalen);
57
    if (bytes > 0) {
58
        memcpy(buf, scd->datapos, bytes);
59
        scd->datapos += bytes;
60
        scd->datalen -= bytes;
61
        assert(scd->datalen >= 0);
62
        if (scd->datalen == 0) {
63
            scd->datapos = 0;
64
        }
65
    }
66
    trace_spice_vmc_read(bytes, len);
67
    return bytes;
68
}
69

    
70
static void vmc_state(SpiceCharDeviceInstance *sin, int connected)
71
{
72
    SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
73

    
74
#if SPICE_SERVER_VERSION < 0x000901
75
    /*
76
     * spice-server calls the state callback for the agent channel when the
77
     * spice client connects / disconnects. Given that not the client but
78
     * the server is doing the parsing of the messages this is wrong as the
79
     * server is still listening. Worse, this causes the parser in the server
80
     * to go out of sync, so we ignore state calls for subtype vdagent
81
     * spicevmc chardevs. For the full story see:
82
     * http://lists.freedesktop.org/archives/spice-devel/2011-July/004837.html
83
     */
84
    if (strcmp(sin->subtype, "vdagent") == 0) {
85
        return;
86
    }
87
#endif
88

    
89
    if ((scd->chr->opened && connected) ||
90
        (!scd->chr->opened && !connected)) {
91
        return;
92
    }
93

    
94
    qemu_chr_be_event(scd->chr,
95
                      connected ? CHR_EVENT_OPENED : CHR_EVENT_CLOSED);
96
}
97

    
98
static SpiceCharDeviceInterface vmc_interface = {
99
    .base.type          = SPICE_INTERFACE_CHAR_DEVICE,
100
    .base.description   = "spice virtual channel char device",
101
    .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,
102
    .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,
103
    .state              = vmc_state,
104
    .write              = vmc_write,
105
    .read               = vmc_read,
106
};
107

    
108

    
109
static void vmc_register_interface(SpiceCharDriver *scd)
110
{
111
    if (scd->active) {
112
        return;
113
    }
114
    dprintf(scd, 1, "%s\n", __func__);
115
    scd->sin.base.sif = &vmc_interface.base;
116
    qemu_spice_add_interface(&scd->sin.base);
117
    scd->active = true;
118
    trace_spice_vmc_register_interface(scd);
119
}
120

    
121
static void vmc_unregister_interface(SpiceCharDriver *scd)
122
{
123
    if (!scd->active) {
124
        return;
125
    }
126
    dprintf(scd, 1, "%s\n", __func__);
127
    spice_server_remove_interface(&scd->sin.base);
128
    scd->active = false;
129
    trace_spice_vmc_unregister_interface(scd);
130
}
131

    
132

    
133
static int spice_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
134
{
135
    SpiceCharDriver *s = chr->opaque;
136

    
137
    dprintf(s, 2, "%s: %d\n", __func__, len);
138
    vmc_register_interface(s);
139
    assert(s->datalen == 0);
140
    if (s->bufsize < len) {
141
        s->bufsize = len;
142
        s->buffer = g_realloc(s->buffer, s->bufsize);
143
    }
144
    memcpy(s->buffer, buf, len);
145
    s->datapos = s->buffer;
146
    s->datalen = len;
147
    spice_server_char_device_wakeup(&s->sin);
148
    return len;
149
}
150

    
151
static void spice_chr_close(struct CharDriverState *chr)
152
{
153
    SpiceCharDriver *s = chr->opaque;
154

    
155
    printf("%s\n", __func__);
156
    vmc_unregister_interface(s);
157
    g_free(s);
158
}
159

    
160
static void spice_chr_guest_open(struct CharDriverState *chr)
161
{
162
    SpiceCharDriver *s = chr->opaque;
163
    vmc_register_interface(s);
164
}
165

    
166
static void spice_chr_guest_close(struct CharDriverState *chr)
167
{
168
    SpiceCharDriver *s = chr->opaque;
169
    vmc_unregister_interface(s);
170
}
171

    
172
static void print_allowed_subtypes(void)
173
{
174
    const char** psubtype;
175
    int i;
176

    
177
    fprintf(stderr, "allowed names: ");
178
    for(i=0, psubtype = spice_server_char_device_recognized_subtypes();
179
        *psubtype != NULL; ++psubtype, ++i) {
180
        if (i == 0) {
181
            fprintf(stderr, "%s", *psubtype);
182
        } else {
183
            fprintf(stderr, ", %s", *psubtype);
184
        }
185
    }
186
    fprintf(stderr, "\n");
187
}
188

    
189
CharDriverState *qemu_chr_open_spice(QemuOpts *opts)
190
{
191
    CharDriverState *chr;
192
    SpiceCharDriver *s;
193
    const char* name = qemu_opt_get(opts, "name");
194
    uint32_t debug = qemu_opt_get_number(opts, "debug", 0);
195
    const char** psubtype = spice_server_char_device_recognized_subtypes();
196
    const char *subtype = NULL;
197

    
198
    if (name == NULL) {
199
        fprintf(stderr, "spice-qemu-char: missing name parameter\n");
200
        print_allowed_subtypes();
201
        return NULL;
202
    }
203
    for(;*psubtype != NULL; ++psubtype) {
204
        if (strcmp(name, *psubtype) == 0) {
205
            subtype = *psubtype;
206
            break;
207
        }
208
    }
209
    if (subtype == NULL) {
210
        fprintf(stderr, "spice-qemu-char: unsupported name: %s\n", name);
211
        print_allowed_subtypes();
212
        return NULL;
213
    }
214

    
215
    chr = g_malloc0(sizeof(CharDriverState));
216
    s = g_malloc0(sizeof(SpiceCharDriver));
217
    s->chr = chr;
218
    s->debug = debug;
219
    s->active = false;
220
    s->sin.subtype = subtype;
221
    chr->opaque = s;
222
    chr->chr_write = spice_chr_write;
223
    chr->chr_close = spice_chr_close;
224
    chr->chr_guest_open = spice_chr_guest_open;
225
    chr->chr_guest_close = spice_chr_guest_close;
226

    
227
#if SPICE_SERVER_VERSION < 0x000901
228
    /* See comment in vmc_state() */
229
    if (strcmp(subtype, "vdagent") == 0) {
230
        qemu_chr_generic_open(chr);
231
    }
232
#endif
233

    
234
    return chr;
235
}