]> git.proxmox.com Git - qemu.git/blob - spice-qemu-char.c
spice-qemu-char: factor out CharDriverState creation
[qemu.git] / spice-qemu-char.c
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 static CharDriverState *chr_open(QemuOpts *opts, const char *subtype)
190 {
191 CharDriverState *chr;
192 SpiceCharDriver *s;
193 uint32_t debug = qemu_opt_get_number(opts, "debug", 0);
194
195 chr = g_malloc0(sizeof(CharDriverState));
196 s = g_malloc0(sizeof(SpiceCharDriver));
197 s->chr = chr;
198 s->debug = debug;
199 s->active = false;
200 s->sin.subtype = subtype;
201 chr->opaque = s;
202 chr->chr_write = spice_chr_write;
203 chr->chr_close = spice_chr_close;
204 chr->chr_guest_open = spice_chr_guest_open;
205 chr->chr_guest_close = spice_chr_guest_close;
206
207 return chr;
208 }
209
210 CharDriverState *qemu_chr_open_spice(QemuOpts *opts)
211 {
212 CharDriverState *chr;
213 const char *name = qemu_opt_get(opts, "name");
214 const char **psubtype = spice_server_char_device_recognized_subtypes();
215 const char *subtype = NULL;
216
217 if (name == NULL) {
218 fprintf(stderr, "spice-qemu-char: missing name parameter\n");
219 print_allowed_subtypes();
220 return NULL;
221 }
222 for(;*psubtype != NULL; ++psubtype) {
223 if (strcmp(name, *psubtype) == 0) {
224 subtype = *psubtype;
225 break;
226 }
227 }
228 if (subtype == NULL) {
229 fprintf(stderr, "spice-qemu-char: unsupported name: %s\n", name);
230 print_allowed_subtypes();
231 return NULL;
232 }
233
234 chr = chr_open(opts, subtype);
235
236 #if SPICE_SERVER_VERSION < 0x000901
237 /* See comment in vmc_state() */
238 if (strcmp(subtype, "vdagent") == 0) {
239 qemu_chr_generic_open(chr);
240 }
241 #endif
242
243 return chr;
244 }