root / spice-qemu-char.c @ 85df3786
History | View | Annotate | Download (6.7 kB)
1 | cbcc6336 | Alon Levy | #include "config-host.h" |
---|---|---|---|
2 | cbcc6336 | Alon Levy | #include "trace.h" |
3 | cbcc6336 | Alon Levy | #include "ui/qemu-spice.h" |
4 | cbcc6336 | Alon Levy | #include <spice.h> |
5 | cbcc6336 | Alon Levy | #include <spice-experimental.h> |
6 | cbcc6336 | Alon Levy | |
7 | cbcc6336 | Alon Levy | #include "osdep.h" |
8 | cbcc6336 | Alon Levy | |
9 | cbcc6336 | Alon Levy | #define dprintf(_scd, _level, _fmt, ...) \
|
10 | cbcc6336 | Alon Levy | do { \
|
11 | cbcc6336 | Alon Levy | static unsigned __dprintf_counter = 0; \ |
12 | cbcc6336 | Alon Levy | if (_scd->debug >= _level) { \
|
13 | cbcc6336 | Alon Levy | fprintf(stderr, "scd: %3d: " _fmt, ++__dprintf_counter, ## __VA_ARGS__);\ |
14 | cbcc6336 | Alon Levy | } \ |
15 | cbcc6336 | Alon Levy | } while (0) |
16 | cbcc6336 | Alon Levy | |
17 | cbcc6336 | Alon Levy | #define VMC_MAX_HOST_WRITE 2048 |
18 | cbcc6336 | Alon Levy | |
19 | cbcc6336 | Alon Levy | typedef struct SpiceCharDriver { |
20 | cbcc6336 | Alon Levy | CharDriverState* chr; |
21 | cbcc6336 | Alon Levy | SpiceCharDeviceInstance sin; |
22 | cbcc6336 | Alon Levy | char *subtype;
|
23 | cbcc6336 | Alon Levy | bool active;
|
24 | cbcc6336 | Alon Levy | uint8_t *buffer; |
25 | cbcc6336 | Alon Levy | uint8_t *datapos; |
26 | cbcc6336 | Alon Levy | ssize_t bufsize, datalen; |
27 | cbcc6336 | Alon Levy | uint32_t debug; |
28 | cbcc6336 | Alon Levy | } SpiceCharDriver; |
29 | cbcc6336 | Alon Levy | |
30 | cbcc6336 | Alon Levy | static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len) |
31 | cbcc6336 | Alon Levy | { |
32 | cbcc6336 | Alon Levy | SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin); |
33 | cbcc6336 | Alon Levy | ssize_t out = 0;
|
34 | cbcc6336 | Alon Levy | ssize_t last_out; |
35 | cbcc6336 | Alon Levy | uint8_t* p = (uint8_t*)buf; |
36 | cbcc6336 | Alon Levy | |
37 | cbcc6336 | Alon Levy | while (len > 0) { |
38 | cbcc6336 | Alon Levy | last_out = MIN(len, VMC_MAX_HOST_WRITE); |
39 | 909cda12 | Anthony Liguori | if (qemu_chr_be_can_write(scd->chr) < last_out) {
|
40 | cbcc6336 | Alon Levy | break;
|
41 | cbcc6336 | Alon Levy | } |
42 | fa5efccb | Anthony Liguori | qemu_chr_be_write(scd->chr, p, last_out); |
43 | 35106c2d | Hans de Goede | out += last_out; |
44 | 35106c2d | Hans de Goede | len -= last_out; |
45 | 35106c2d | Hans de Goede | p += last_out; |
46 | cbcc6336 | Alon Levy | } |
47 | cbcc6336 | Alon Levy | |
48 | 7b6c7369 | Peter Maydell | dprintf(scd, 3, "%s: %zu/%zd\n", __func__, out, len + out); |
49 | cbcc6336 | Alon Levy | trace_spice_vmc_write(out, len + out); |
50 | cbcc6336 | Alon Levy | return out;
|
51 | cbcc6336 | Alon Levy | } |
52 | cbcc6336 | Alon Levy | |
53 | cbcc6336 | Alon Levy | static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len) |
54 | cbcc6336 | Alon Levy | { |
55 | cbcc6336 | Alon Levy | SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin); |
56 | cbcc6336 | Alon Levy | int bytes = MIN(len, scd->datalen);
|
57 | cbcc6336 | Alon Levy | |
58 | cbcc6336 | Alon Levy | dprintf(scd, 2, "%s: %p %d/%d/%zd\n", __func__, scd->datapos, len, bytes, scd->datalen); |
59 | cbcc6336 | Alon Levy | if (bytes > 0) { |
60 | cbcc6336 | Alon Levy | memcpy(buf, scd->datapos, bytes); |
61 | cbcc6336 | Alon Levy | scd->datapos += bytes; |
62 | cbcc6336 | Alon Levy | scd->datalen -= bytes; |
63 | cbcc6336 | Alon Levy | assert(scd->datalen >= 0);
|
64 | cbcc6336 | Alon Levy | if (scd->datalen == 0) { |
65 | cbcc6336 | Alon Levy | scd->datapos = 0;
|
66 | cbcc6336 | Alon Levy | } |
67 | cbcc6336 | Alon Levy | } |
68 | cbcc6336 | Alon Levy | trace_spice_vmc_read(bytes, len); |
69 | cbcc6336 | Alon Levy | return bytes;
|
70 | cbcc6336 | Alon Levy | } |
71 | cbcc6336 | Alon Levy | |
72 | f76e4c7f | Hans de Goede | static void vmc_state(SpiceCharDeviceInstance *sin, int connected) |
73 | f76e4c7f | Hans de Goede | { |
74 | f76e4c7f | Hans de Goede | SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin); |
75 | f76e4c7f | Hans de Goede | |
76 | f76e4c7f | Hans de Goede | #if SPICE_SERVER_VERSION < 0x000901 |
77 | f76e4c7f | Hans de Goede | /*
|
78 | f76e4c7f | Hans de Goede | * spice-server calls the state callback for the agent channel when the
|
79 | f76e4c7f | Hans de Goede | * spice client connects / disconnects. Given that not the client but
|
80 | f76e4c7f | Hans de Goede | * the server is doing the parsing of the messages this is wrong as the
|
81 | f76e4c7f | Hans de Goede | * server is still listening. Worse, this causes the parser in the server
|
82 | f76e4c7f | Hans de Goede | * to go out of sync, so we ignore state calls for subtype vdagent
|
83 | f76e4c7f | Hans de Goede | * spicevmc chardevs. For the full story see:
|
84 | f76e4c7f | Hans de Goede | * http://lists.freedesktop.org/archives/spice-devel/2011-July/004837.html
|
85 | f76e4c7f | Hans de Goede | */
|
86 | f76e4c7f | Hans de Goede | if (strcmp(sin->subtype, "vdagent") == 0) { |
87 | f76e4c7f | Hans de Goede | return;
|
88 | f76e4c7f | Hans de Goede | } |
89 | f76e4c7f | Hans de Goede | #endif
|
90 | f76e4c7f | Hans de Goede | |
91 | f76e4c7f | Hans de Goede | if ((scd->chr->opened && connected) ||
|
92 | f76e4c7f | Hans de Goede | (!scd->chr->opened && !connected)) { |
93 | f76e4c7f | Hans de Goede | return;
|
94 | f76e4c7f | Hans de Goede | } |
95 | f76e4c7f | Hans de Goede | |
96 | f76e4c7f | Hans de Goede | qemu_chr_be_event(scd->chr, |
97 | f76e4c7f | Hans de Goede | connected ? CHR_EVENT_OPENED : CHR_EVENT_CLOSED); |
98 | f76e4c7f | Hans de Goede | } |
99 | f76e4c7f | Hans de Goede | |
100 | cbcc6336 | Alon Levy | static SpiceCharDeviceInterface vmc_interface = {
|
101 | cbcc6336 | Alon Levy | .base.type = SPICE_INTERFACE_CHAR_DEVICE, |
102 | cbcc6336 | Alon Levy | .base.description = "spice virtual channel char device",
|
103 | cbcc6336 | Alon Levy | .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR, |
104 | cbcc6336 | Alon Levy | .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR, |
105 | f76e4c7f | Hans de Goede | .state = vmc_state, |
106 | cbcc6336 | Alon Levy | .write = vmc_write, |
107 | cbcc6336 | Alon Levy | .read = vmc_read, |
108 | cbcc6336 | Alon Levy | }; |
109 | cbcc6336 | Alon Levy | |
110 | cbcc6336 | Alon Levy | |
111 | cbcc6336 | Alon Levy | static void vmc_register_interface(SpiceCharDriver *scd) |
112 | cbcc6336 | Alon Levy | { |
113 | cbcc6336 | Alon Levy | if (scd->active) {
|
114 | cbcc6336 | Alon Levy | return;
|
115 | cbcc6336 | Alon Levy | } |
116 | cbcc6336 | Alon Levy | dprintf(scd, 1, "%s\n", __func__); |
117 | cbcc6336 | Alon Levy | scd->sin.base.sif = &vmc_interface.base; |
118 | cbcc6336 | Alon Levy | qemu_spice_add_interface(&scd->sin.base); |
119 | cbcc6336 | Alon Levy | scd->active = true;
|
120 | cbcc6336 | Alon Levy | trace_spice_vmc_register_interface(scd); |
121 | cbcc6336 | Alon Levy | } |
122 | cbcc6336 | Alon Levy | |
123 | cbcc6336 | Alon Levy | static void vmc_unregister_interface(SpiceCharDriver *scd) |
124 | cbcc6336 | Alon Levy | { |
125 | cbcc6336 | Alon Levy | if (!scd->active) {
|
126 | cbcc6336 | Alon Levy | return;
|
127 | cbcc6336 | Alon Levy | } |
128 | cbcc6336 | Alon Levy | dprintf(scd, 1, "%s\n", __func__); |
129 | cbcc6336 | Alon Levy | spice_server_remove_interface(&scd->sin.base); |
130 | cbcc6336 | Alon Levy | scd->active = false;
|
131 | cbcc6336 | Alon Levy | trace_spice_vmc_unregister_interface(scd); |
132 | cbcc6336 | Alon Levy | } |
133 | cbcc6336 | Alon Levy | |
134 | cbcc6336 | Alon Levy | |
135 | cbcc6336 | Alon Levy | static int spice_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
136 | cbcc6336 | Alon Levy | { |
137 | cbcc6336 | Alon Levy | SpiceCharDriver *s = chr->opaque; |
138 | cbcc6336 | Alon Levy | |
139 | cbcc6336 | Alon Levy | dprintf(s, 2, "%s: %d\n", __func__, len); |
140 | cbcc6336 | Alon Levy | vmc_register_interface(s); |
141 | cbcc6336 | Alon Levy | assert(s->datalen == 0);
|
142 | cbcc6336 | Alon Levy | if (s->bufsize < len) {
|
143 | cbcc6336 | Alon Levy | s->bufsize = len; |
144 | 7267c094 | Anthony Liguori | s->buffer = g_realloc(s->buffer, s->bufsize); |
145 | cbcc6336 | Alon Levy | } |
146 | cbcc6336 | Alon Levy | memcpy(s->buffer, buf, len); |
147 | cbcc6336 | Alon Levy | s->datapos = s->buffer; |
148 | cbcc6336 | Alon Levy | s->datalen = len; |
149 | cbcc6336 | Alon Levy | spice_server_char_device_wakeup(&s->sin); |
150 | cbcc6336 | Alon Levy | return len;
|
151 | cbcc6336 | Alon Levy | } |
152 | cbcc6336 | Alon Levy | |
153 | cbcc6336 | Alon Levy | static void spice_chr_close(struct CharDriverState *chr) |
154 | cbcc6336 | Alon Levy | { |
155 | cbcc6336 | Alon Levy | SpiceCharDriver *s = chr->opaque; |
156 | cbcc6336 | Alon Levy | |
157 | cbcc6336 | Alon Levy | printf("%s\n", __func__);
|
158 | cbcc6336 | Alon Levy | vmc_unregister_interface(s); |
159 | 7267c094 | Anthony Liguori | g_free(s); |
160 | cbcc6336 | Alon Levy | } |
161 | cbcc6336 | Alon Levy | |
162 | cd8f7df2 | Hans de Goede | static void spice_chr_guest_open(struct CharDriverState *chr) |
163 | cd8f7df2 | Hans de Goede | { |
164 | cd8f7df2 | Hans de Goede | SpiceCharDriver *s = chr->opaque; |
165 | cd8f7df2 | Hans de Goede | vmc_register_interface(s); |
166 | cd8f7df2 | Hans de Goede | } |
167 | cd8f7df2 | Hans de Goede | |
168 | cd8f7df2 | Hans de Goede | static void spice_chr_guest_close(struct CharDriverState *chr) |
169 | cd8f7df2 | Hans de Goede | { |
170 | cd8f7df2 | Hans de Goede | SpiceCharDriver *s = chr->opaque; |
171 | cd8f7df2 | Hans de Goede | vmc_unregister_interface(s); |
172 | cd8f7df2 | Hans de Goede | } |
173 | cd8f7df2 | Hans de Goede | |
174 | cbcc6336 | Alon Levy | static void print_allowed_subtypes(void) |
175 | cbcc6336 | Alon Levy | { |
176 | cbcc6336 | Alon Levy | const char** psubtype; |
177 | cbcc6336 | Alon Levy | int i;
|
178 | cbcc6336 | Alon Levy | |
179 | cbcc6336 | Alon Levy | fprintf(stderr, "allowed names: ");
|
180 | cbcc6336 | Alon Levy | for(i=0, psubtype = spice_server_char_device_recognized_subtypes(); |
181 | cbcc6336 | Alon Levy | *psubtype != NULL; ++psubtype, ++i) {
|
182 | cbcc6336 | Alon Levy | if (i == 0) { |
183 | cbcc6336 | Alon Levy | fprintf(stderr, "%s", *psubtype);
|
184 | cbcc6336 | Alon Levy | } else {
|
185 | cbcc6336 | Alon Levy | fprintf(stderr, ", %s", *psubtype);
|
186 | cbcc6336 | Alon Levy | } |
187 | cbcc6336 | Alon Levy | } |
188 | cbcc6336 | Alon Levy | fprintf(stderr, "\n");
|
189 | cbcc6336 | Alon Levy | } |
190 | cbcc6336 | Alon Levy | |
191 | 1f51470d | Markus Armbruster | CharDriverState *qemu_chr_open_spice(QemuOpts *opts) |
192 | cbcc6336 | Alon Levy | { |
193 | cbcc6336 | Alon Levy | CharDriverState *chr; |
194 | cbcc6336 | Alon Levy | SpiceCharDriver *s; |
195 | cbcc6336 | Alon Levy | const char* name = qemu_opt_get(opts, "name"); |
196 | cbcc6336 | Alon Levy | uint32_t debug = qemu_opt_get_number(opts, "debug", 0); |
197 | cbcc6336 | Alon Levy | const char** psubtype = spice_server_char_device_recognized_subtypes(); |
198 | cbcc6336 | Alon Levy | const char *subtype = NULL; |
199 | cbcc6336 | Alon Levy | |
200 | cbcc6336 | Alon Levy | if (name == NULL) { |
201 | cbcc6336 | Alon Levy | fprintf(stderr, "spice-qemu-char: missing name parameter\n");
|
202 | cbcc6336 | Alon Levy | print_allowed_subtypes(); |
203 | 1f51470d | Markus Armbruster | return NULL; |
204 | cbcc6336 | Alon Levy | } |
205 | cbcc6336 | Alon Levy | for(;*psubtype != NULL; ++psubtype) { |
206 | cbcc6336 | Alon Levy | if (strcmp(name, *psubtype) == 0) { |
207 | cbcc6336 | Alon Levy | subtype = *psubtype; |
208 | cbcc6336 | Alon Levy | break;
|
209 | cbcc6336 | Alon Levy | } |
210 | cbcc6336 | Alon Levy | } |
211 | cbcc6336 | Alon Levy | if (subtype == NULL) { |
212 | cbcc6336 | Alon Levy | fprintf(stderr, "spice-qemu-char: unsupported name\n");
|
213 | cbcc6336 | Alon Levy | print_allowed_subtypes(); |
214 | 1f51470d | Markus Armbruster | return NULL; |
215 | cbcc6336 | Alon Levy | } |
216 | cbcc6336 | Alon Levy | |
217 | 7267c094 | Anthony Liguori | chr = g_malloc0(sizeof(CharDriverState));
|
218 | 7267c094 | Anthony Liguori | s = g_malloc0(sizeof(SpiceCharDriver));
|
219 | cbcc6336 | Alon Levy | s->chr = chr; |
220 | cbcc6336 | Alon Levy | s->debug = debug; |
221 | cbcc6336 | Alon Levy | s->active = false;
|
222 | cbcc6336 | Alon Levy | s->sin.subtype = subtype; |
223 | cbcc6336 | Alon Levy | chr->opaque = s; |
224 | cbcc6336 | Alon Levy | chr->chr_write = spice_chr_write; |
225 | cbcc6336 | Alon Levy | chr->chr_close = spice_chr_close; |
226 | cd8f7df2 | Hans de Goede | chr->chr_guest_open = spice_chr_guest_open; |
227 | cd8f7df2 | Hans de Goede | chr->chr_guest_close = spice_chr_guest_close; |
228 | cbcc6336 | Alon Levy | |
229 | f76e4c7f | Hans de Goede | #if SPICE_SERVER_VERSION < 0x000901 |
230 | f76e4c7f | Hans de Goede | /* See comment in vmc_state() */
|
231 | f76e4c7f | Hans de Goede | if (strcmp(subtype, "vdagent") == 0) { |
232 | f76e4c7f | Hans de Goede | qemu_chr_generic_open(chr); |
233 | f76e4c7f | Hans de Goede | } |
234 | f76e4c7f | Hans de Goede | #endif
|
235 | cbcc6336 | Alon Levy | |
236 | 1f51470d | Markus Armbruster | return chr;
|
237 | cbcc6336 | Alon Levy | } |