]> git.proxmox.com Git - mirror_qemu.git/blame - spice-qemu-char.c
spice-qemu-char: Remove #ifdef-ed code for old spice-server compat
[mirror_qemu.git] / spice-qemu-char.c
CommitLineData
cbcc6336
AL
1#include "config-host.h"
2#include "trace.h"
3#include "ui/qemu-spice.h"
dccfcd0e 4#include "sysemu/char.h"
cbcc6336
AL
5#include <spice.h>
6#include <spice-experimental.h>
5a49d3e9 7#include <spice/protocol.h>
cbcc6336 8
1de7afc9 9#include "qemu/osdep.h"
cbcc6336 10
cbcc6336
AL
11typedef 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;
7a5448ce 19 QLIST_ENTRY(SpiceCharDriver) next;
cbcc6336
AL
20} SpiceCharDriver;
21
7a5448ce
MAL
22static QLIST_HEAD(, SpiceCharDriver) spice_chars =
23 QLIST_HEAD_INITIALIZER(spice_chars);
24
cbcc6336
AL
25static 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) {
07a54d70
MAL
33 last_out = MIN(len, qemu_chr_be_can_write(scd->chr));
34 if (last_out <= 0) {
cbcc6336
AL
35 break;
36 }
fa5efccb 37 qemu_chr_be_write(scd->chr, p, last_out);
35106c2d
HG
38 out += last_out;
39 len -= last_out;
40 p += last_out;
cbcc6336
AL
41 }
42
cbcc6336
AL
43 trace_spice_vmc_write(out, len + out);
44 return out;
45}
46
47static 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
cbcc6336
AL
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
5a49d3e9
MAL
65#if SPICE_SERVER_VERSION >= 0x000c02
66static 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:
5a49d3e9
MAL
76 return;
77 }
78
5a49d3e9
MAL
79 trace_spice_vmc_event(chr_event);
80 qemu_chr_be_event(scd->chr, chr_event);
81}
82#endif
83
f76e4c7f
HG
84static void vmc_state(SpiceCharDeviceInstance *sin, int connected)
85{
86 SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
87
16665b94
HG
88 if ((scd->chr->be_open && connected) ||
89 (!scd->chr->be_open && !connected)) {
f76e4c7f
HG
90 return;
91 }
92
93 qemu_chr_be_event(scd->chr,
94 connected ? CHR_EVENT_OPENED : CHR_EVENT_CLOSED);
95}
96
cbcc6336
AL
97static 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,
f76e4c7f 102 .state = vmc_state,
cbcc6336
AL
103 .write = vmc_write,
104 .read = vmc_read,
5a49d3e9
MAL
105#if SPICE_SERVER_VERSION >= 0x000c02
106 .event = vmc_event,
107#endif
cbcc6336
AL
108};
109
110
111static void vmc_register_interface(SpiceCharDriver *scd)
112{
113 if (scd->active) {
114 return;
115 }
cbcc6336
AL
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
122static void vmc_unregister_interface(SpiceCharDriver *scd)
123{
124 if (!scd->active) {
125 return;
126 }
cbcc6336
AL
127 spice_server_remove_interface(&scd->sin.base);
128 scd->active = false;
129 trace_spice_vmc_unregister_interface(scd);
130}
131
132
133static int spice_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
134{
135 SpiceCharDriver *s = chr->opaque;
136
cbcc6336
AL
137 assert(s->datalen == 0);
138 if (s->bufsize < len) {
139 s->bufsize = len;
7267c094 140 s->buffer = g_realloc(s->buffer, s->bufsize);
cbcc6336
AL
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
149static void spice_chr_close(struct CharDriverState *chr)
150{
151 SpiceCharDriver *s = chr->opaque;
152
cbcc6336 153 vmc_unregister_interface(s);
7a5448ce 154 QLIST_REMOVE(s, next);
5e9b473a
HG
155
156 g_free((char *)s->sin.subtype);
157#if SPICE_SERVER_VERSION >= 0x000c02
158 g_free((char *)s->sin.portname);
159#endif
7267c094 160 g_free(s);
cbcc6336
AL
161}
162
574b711a 163static void spice_chr_set_fe_open(struct CharDriverState *chr, int fe_open)
cd8f7df2
HG
164{
165 SpiceCharDriver *s = chr->opaque;
574b711a
HG
166 if (fe_open) {
167 vmc_register_interface(s);
168 } else {
169 vmc_unregister_interface(s);
170 }
cd8f7df2
HG
171}
172
cbcc6336
AL
173static 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
cd153e2a 190static CharDriverState *chr_open(const char *subtype)
cbcc6336
AL
191{
192 CharDriverState *chr;
193 SpiceCharDriver *s;
71b423f4
MAL
194
195 chr = g_malloc0(sizeof(CharDriverState));
196 s = g_malloc0(sizeof(SpiceCharDriver));
197 s->chr = chr;
71b423f4 198 s->active = false;
5e9b473a 199 s->sin.subtype = g_strdup(subtype);
71b423f4
MAL
200 chr->opaque = s;
201 chr->chr_write = spice_chr_write;
202 chr->chr_close = spice_chr_close;
574b711a 203 chr->chr_set_fe_open = spice_chr_set_fe_open;
71b423f4 204
7a5448ce
MAL
205 QLIST_INSERT_HEAD(&spice_chars, s, next);
206
71b423f4
MAL
207 return chr;
208}
209
cd153e2a 210CharDriverState *qemu_chr_open_spice_vmc(const char *type)
71b423f4 211{
71b423f4 212 const char **psubtype = spice_server_char_device_recognized_subtypes();
cbcc6336 213
cd153e2a 214 if (type == NULL) {
cbcc6336
AL
215 fprintf(stderr, "spice-qemu-char: missing name parameter\n");
216 print_allowed_subtypes();
1f51470d 217 return NULL;
cbcc6336 218 }
cd153e2a
GH
219 for (; *psubtype != NULL; ++psubtype) {
220 if (strcmp(type, *psubtype) == 0) {
cbcc6336
AL
221 break;
222 }
223 }
cd153e2a
GH
224 if (*psubtype == NULL) {
225 fprintf(stderr, "spice-qemu-char: unsupported type: %s\n", type);
cbcc6336 226 print_allowed_subtypes();
1f51470d 227 return NULL;
cbcc6336
AL
228 }
229
52fe0e75 230 return chr_open(type);
cbcc6336 231}
5a49d3e9
MAL
232
233#if SPICE_SERVER_VERSION >= 0x000c02
cd153e2a 234CharDriverState *qemu_chr_open_spice_port(const char *name)
5a49d3e9
MAL
235{
236 CharDriverState *chr;
237 SpiceCharDriver *s;
5a49d3e9
MAL
238
239 if (name == NULL) {
240 fprintf(stderr, "spice-qemu-char: missing name parameter\n");
241 return NULL;
242 }
243
cd153e2a 244 chr = chr_open("port");
5a49d3e9 245 s = chr->opaque;
5e9b473a 246 s->sin.portname = g_strdup(name);
5a49d3e9
MAL
247
248 return chr;
249}
afd0b409
MAL
250
251void 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}
5a49d3e9 262#endif
26c60614 263
cd153e2a
GH
264static 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
277static 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
26c60614
AL
290static void register_types(void)
291{
cd153e2a
GH
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);
26c60614
AL
296}
297
298type_init(register_types);