root / spice-qemu-char.c @ 52fe0e75
History | View | Annotate | Download (7.5 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 |
char *subtype;
|
15 |
bool active;
|
16 |
uint8_t *buffer; |
17 |
uint8_t *datapos; |
18 |
ssize_t bufsize, datalen; |
19 |
QLIST_ENTRY(SpiceCharDriver) next; |
20 |
} SpiceCharDriver; |
21 |
|
22 |
static QLIST_HEAD(, SpiceCharDriver) spice_chars =
|
23 |
QLIST_HEAD_INITIALIZER(spice_chars); |
24 |
|
25 |
static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len) |
26 |
{ |
27 |
SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin); |
28 |
ssize_t out = 0;
|
29 |
ssize_t last_out; |
30 |
uint8_t* p = (uint8_t*)buf; |
31 |
|
32 |
while (len > 0) { |
33 |
last_out = MIN(len, qemu_chr_be_can_write(scd->chr)); |
34 |
if (last_out <= 0) { |
35 |
break;
|
36 |
} |
37 |
qemu_chr_be_write(scd->chr, p, last_out); |
38 |
out += last_out; |
39 |
len -= last_out; |
40 |
p += last_out; |
41 |
} |
42 |
|
43 |
trace_spice_vmc_write(out, len + out); |
44 |
return out;
|
45 |
} |
46 |
|
47 |
static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len) |
48 |
{ |
49 |
SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin); |
50 |
int bytes = MIN(len, scd->datalen);
|
51 |
|
52 |
if (bytes > 0) { |
53 |
memcpy(buf, scd->datapos, bytes); |
54 |
scd->datapos += bytes; |
55 |
scd->datalen -= bytes; |
56 |
assert(scd->datalen >= 0);
|
57 |
if (scd->datalen == 0) { |
58 |
scd->datapos = 0;
|
59 |
} |
60 |
} |
61 |
trace_spice_vmc_read(bytes, len); |
62 |
return bytes;
|
63 |
} |
64 |
|
65 |
#if SPICE_SERVER_VERSION >= 0x000c02 |
66 |
static void vmc_event(SpiceCharDeviceInstance *sin, uint8_t event) |
67 |
{ |
68 |
SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin); |
69 |
int chr_event;
|
70 |
|
71 |
switch (event) {
|
72 |
case SPICE_PORT_EVENT_BREAK:
|
73 |
chr_event = CHR_EVENT_BREAK; |
74 |
break;
|
75 |
default:
|
76 |
return;
|
77 |
} |
78 |
|
79 |
trace_spice_vmc_event(chr_event); |
80 |
qemu_chr_be_event(scd->chr, chr_event); |
81 |
} |
82 |
#endif
|
83 |
|
84 |
static void vmc_state(SpiceCharDeviceInstance *sin, int connected) |
85 |
{ |
86 |
SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin); |
87 |
|
88 |
if ((scd->chr->be_open && connected) ||
|
89 |
(!scd->chr->be_open && !connected)) { |
90 |
return;
|
91 |
} |
92 |
|
93 |
qemu_chr_be_event(scd->chr, |
94 |
connected ? CHR_EVENT_OPENED : CHR_EVENT_CLOSED); |
95 |
} |
96 |
|
97 |
static SpiceCharDeviceInterface vmc_interface = {
|
98 |
.base.type = SPICE_INTERFACE_CHAR_DEVICE, |
99 |
.base.description = "spice virtual channel char device",
|
100 |
.base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR, |
101 |
.base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR, |
102 |
.state = vmc_state, |
103 |
.write = vmc_write, |
104 |
.read = vmc_read, |
105 |
#if SPICE_SERVER_VERSION >= 0x000c02 |
106 |
.event = vmc_event, |
107 |
#endif
|
108 |
}; |
109 |
|
110 |
|
111 |
static void vmc_register_interface(SpiceCharDriver *scd) |
112 |
{ |
113 |
if (scd->active) {
|
114 |
return;
|
115 |
} |
116 |
scd->sin.base.sif = &vmc_interface.base; |
117 |
qemu_spice_add_interface(&scd->sin.base); |
118 |
scd->active = true;
|
119 |
trace_spice_vmc_register_interface(scd); |
120 |
} |
121 |
|
122 |
static void vmc_unregister_interface(SpiceCharDriver *scd) |
123 |
{ |
124 |
if (!scd->active) {
|
125 |
return;
|
126 |
} |
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 |
assert(s->datalen == 0);
|
138 |
if (s->bufsize < len) {
|
139 |
s->bufsize = len; |
140 |
s->buffer = g_realloc(s->buffer, s->bufsize); |
141 |
} |
142 |
memcpy(s->buffer, buf, len); |
143 |
s->datapos = s->buffer; |
144 |
s->datalen = len; |
145 |
spice_server_char_device_wakeup(&s->sin); |
146 |
return len;
|
147 |
} |
148 |
|
149 |
static void spice_chr_close(struct CharDriverState *chr) |
150 |
{ |
151 |
SpiceCharDriver *s = chr->opaque; |
152 |
|
153 |
vmc_unregister_interface(s); |
154 |
QLIST_REMOVE(s, next); |
155 |
|
156 |
g_free((char *)s->sin.subtype);
|
157 |
#if SPICE_SERVER_VERSION >= 0x000c02 |
158 |
g_free((char *)s->sin.portname);
|
159 |
#endif
|
160 |
g_free(s); |
161 |
} |
162 |
|
163 |
static void spice_chr_set_fe_open(struct CharDriverState *chr, int fe_open) |
164 |
{ |
165 |
SpiceCharDriver *s = chr->opaque; |
166 |
if (fe_open) {
|
167 |
vmc_register_interface(s); |
168 |
} else {
|
169 |
vmc_unregister_interface(s); |
170 |
} |
171 |
} |
172 |
|
173 |
static void print_allowed_subtypes(void) |
174 |
{ |
175 |
const char** psubtype; |
176 |
int i;
|
177 |
|
178 |
fprintf(stderr, "allowed names: ");
|
179 |
for(i=0, psubtype = spice_server_char_device_recognized_subtypes(); |
180 |
*psubtype != NULL; ++psubtype, ++i) {
|
181 |
if (i == 0) { |
182 |
fprintf(stderr, "%s", *psubtype);
|
183 |
} else {
|
184 |
fprintf(stderr, ", %s", *psubtype);
|
185 |
} |
186 |
} |
187 |
fprintf(stderr, "\n");
|
188 |
} |
189 |
|
190 |
static CharDriverState *chr_open(const char *subtype) |
191 |
{ |
192 |
CharDriverState *chr; |
193 |
SpiceCharDriver *s; |
194 |
|
195 |
chr = g_malloc0(sizeof(CharDriverState));
|
196 |
s = g_malloc0(sizeof(SpiceCharDriver));
|
197 |
s->chr = chr; |
198 |
s->active = false;
|
199 |
s->sin.subtype = g_strdup(subtype); |
200 |
chr->opaque = s; |
201 |
chr->chr_write = spice_chr_write; |
202 |
chr->chr_close = spice_chr_close; |
203 |
chr->chr_set_fe_open = spice_chr_set_fe_open; |
204 |
|
205 |
QLIST_INSERT_HEAD(&spice_chars, s, next); |
206 |
|
207 |
return chr;
|
208 |
} |
209 |
|
210 |
CharDriverState *qemu_chr_open_spice_vmc(const char *type) |
211 |
{ |
212 |
const char **psubtype = spice_server_char_device_recognized_subtypes(); |
213 |
|
214 |
if (type == NULL) { |
215 |
fprintf(stderr, "spice-qemu-char: missing name parameter\n");
|
216 |
print_allowed_subtypes(); |
217 |
return NULL; |
218 |
} |
219 |
for (; *psubtype != NULL; ++psubtype) { |
220 |
if (strcmp(type, *psubtype) == 0) { |
221 |
break;
|
222 |
} |
223 |
} |
224 |
if (*psubtype == NULL) { |
225 |
fprintf(stderr, "spice-qemu-char: unsupported type: %s\n", type);
|
226 |
print_allowed_subtypes(); |
227 |
return NULL; |
228 |
} |
229 |
|
230 |
return chr_open(type);
|
231 |
} |
232 |
|
233 |
#if SPICE_SERVER_VERSION >= 0x000c02 |
234 |
CharDriverState *qemu_chr_open_spice_port(const char *name) |
235 |
{ |
236 |
CharDriverState *chr; |
237 |
SpiceCharDriver *s; |
238 |
|
239 |
if (name == NULL) { |
240 |
fprintf(stderr, "spice-qemu-char: missing name parameter\n");
|
241 |
return NULL; |
242 |
} |
243 |
|
244 |
chr = chr_open("port");
|
245 |
s = chr->opaque; |
246 |
s->sin.portname = g_strdup(name); |
247 |
|
248 |
return chr;
|
249 |
} |
250 |
|
251 |
void qemu_spice_register_ports(void) |
252 |
{ |
253 |
SpiceCharDriver *s; |
254 |
|
255 |
QLIST_FOREACH(s, &spice_chars, next) { |
256 |
if (s->sin.portname == NULL) { |
257 |
continue;
|
258 |
} |
259 |
vmc_register_interface(s); |
260 |
} |
261 |
} |
262 |
#endif
|
263 |
|
264 |
static void qemu_chr_parse_spice_vmc(QemuOpts *opts, ChardevBackend *backend, |
265 |
Error **errp) |
266 |
{ |
267 |
const char *name = qemu_opt_get(opts, "name"); |
268 |
|
269 |
if (name == NULL) { |
270 |
error_setg(errp, "chardev: spice channel: no name given");
|
271 |
return;
|
272 |
} |
273 |
backend->spicevmc = g_new0(ChardevSpiceChannel, 1);
|
274 |
backend->spicevmc->type = g_strdup(name); |
275 |
} |
276 |
|
277 |
static void qemu_chr_parse_spice_port(QemuOpts *opts, ChardevBackend *backend, |
278 |
Error **errp) |
279 |
{ |
280 |
const char *name = qemu_opt_get(opts, "name"); |
281 |
|
282 |
if (name == NULL) { |
283 |
error_setg(errp, "chardev: spice port: no name given");
|
284 |
return;
|
285 |
} |
286 |
backend->spiceport = g_new0(ChardevSpicePort, 1);
|
287 |
backend->spiceport->fqdn = g_strdup(name); |
288 |
} |
289 |
|
290 |
static void register_types(void) |
291 |
{ |
292 |
register_char_driver_qapi("spicevmc", CHARDEV_BACKEND_KIND_SPICEVMC,
|
293 |
qemu_chr_parse_spice_vmc); |
294 |
register_char_driver_qapi("spiceport", CHARDEV_BACKEND_KIND_SPICEPORT,
|
295 |
qemu_chr_parse_spice_port); |
296 |
} |
297 |
|
298 |
type_init(register_types); |