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