Merge branch 'patches' of git://qemu.weilnetz.de/git/qemu
[sdk/emulator/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 #define VMC_MAX_HOST_WRITE    2048
18
19 typedef struct SpiceCharDriver {
20     CharDriverState*      chr;
21     SpiceCharDeviceInstance     sin;
22     char                  *subtype;
23     bool                  active;
24     uint8_t               *buffer;
25     uint8_t               *datapos;
26     ssize_t               bufsize, datalen;
27     uint32_t              debug;
28 } SpiceCharDriver;
29
30 static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
31 {
32     SpiceCharDriver *scd = container_of(sin, SpiceCharDriver, sin);
33     ssize_t out = 0;
34     ssize_t last_out;
35     uint8_t* p = (uint8_t*)buf;
36
37     while (len > 0) {
38         last_out = MIN(len, VMC_MAX_HOST_WRITE);
39         qemu_chr_read(scd->chr, p, last_out);
40         if (last_out > 0) {
41             out += last_out;
42             len -= last_out;
43             p += last_out;
44         } else {
45             break;
46         }
47     }
48
49     dprintf(scd, 3, "%s: %lu/%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 SpiceCharDeviceInterface vmc_interface = {
74     .base.type          = SPICE_INTERFACE_CHAR_DEVICE,
75     .base.description   = "spice virtual channel char device",
76     .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,
77     .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,
78     .write              = vmc_write,
79     .read               = vmc_read,
80 };
81
82
83 static void vmc_register_interface(SpiceCharDriver *scd)
84 {
85     if (scd->active) {
86         return;
87     }
88     dprintf(scd, 1, "%s\n", __func__);
89     scd->sin.base.sif = &vmc_interface.base;
90     qemu_spice_add_interface(&scd->sin.base);
91     scd->active = true;
92     trace_spice_vmc_register_interface(scd);
93 }
94
95 static void vmc_unregister_interface(SpiceCharDriver *scd)
96 {
97     if (!scd->active) {
98         return;
99     }
100     dprintf(scd, 1, "%s\n", __func__);
101     spice_server_remove_interface(&scd->sin.base);
102     scd->active = false;
103     trace_spice_vmc_unregister_interface(scd);
104 }
105
106
107 static int spice_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
108 {
109     SpiceCharDriver *s = chr->opaque;
110
111     dprintf(s, 2, "%s: %d\n", __func__, len);
112     vmc_register_interface(s);
113     assert(s->datalen == 0);
114     if (s->bufsize < len) {
115         s->bufsize = len;
116         s->buffer = qemu_realloc(s->buffer, s->bufsize);
117     }
118     memcpy(s->buffer, buf, len);
119     s->datapos = s->buffer;
120     s->datalen = len;
121     spice_server_char_device_wakeup(&s->sin);
122     return len;
123 }
124
125 static void spice_chr_close(struct CharDriverState *chr)
126 {
127     SpiceCharDriver *s = chr->opaque;
128
129     printf("%s\n", __func__);
130     vmc_unregister_interface(s);
131     qemu_free(s);
132 }
133
134 static void spice_chr_guest_open(struct CharDriverState *chr)
135 {
136     SpiceCharDriver *s = chr->opaque;
137     vmc_register_interface(s);
138 }
139
140 static void spice_chr_guest_close(struct CharDriverState *chr)
141 {
142     SpiceCharDriver *s = chr->opaque;
143     vmc_unregister_interface(s);
144 }
145
146 static void print_allowed_subtypes(void)
147 {
148     const char** psubtype;
149     int i;
150
151     fprintf(stderr, "allowed names: ");
152     for(i=0, psubtype = spice_server_char_device_recognized_subtypes();
153         *psubtype != NULL; ++psubtype, ++i) {
154         if (i == 0) {
155             fprintf(stderr, "%s", *psubtype);
156         } else {
157             fprintf(stderr, ", %s", *psubtype);
158         }
159     }
160     fprintf(stderr, "\n");
161 }
162
163 CharDriverState *qemu_chr_open_spice(QemuOpts *opts)
164 {
165     CharDriverState *chr;
166     SpiceCharDriver *s;
167     const char* name = qemu_opt_get(opts, "name");
168     uint32_t debug = qemu_opt_get_number(opts, "debug", 0);
169     const char** psubtype = spice_server_char_device_recognized_subtypes();
170     const char *subtype = NULL;
171
172     if (name == NULL) {
173         fprintf(stderr, "spice-qemu-char: missing name parameter\n");
174         print_allowed_subtypes();
175         return NULL;
176     }
177     for(;*psubtype != NULL; ++psubtype) {
178         if (strcmp(name, *psubtype) == 0) {
179             subtype = *psubtype;
180             break;
181         }
182     }
183     if (subtype == NULL) {
184         fprintf(stderr, "spice-qemu-char: unsupported name\n");
185         print_allowed_subtypes();
186         return NULL;
187     }
188
189     chr = qemu_mallocz(sizeof(CharDriverState));
190     s = qemu_mallocz(sizeof(SpiceCharDriver));
191     s->chr = chr;
192     s->debug = debug;
193     s->active = false;
194     s->sin.subtype = subtype;
195     chr->opaque = s;
196     chr->chr_write = spice_chr_write;
197     chr->chr_close = spice_chr_close;
198     chr->chr_guest_open = spice_chr_guest_open;
199     chr->chr_guest_close = spice_chr_guest_close;
200
201     qemu_chr_generic_open(chr);
202
203     return chr;
204 }