master
c 257 lines 6.83 KB
Raw
1 /*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 #include "qemu/osdep.h"
26 #include "chardev/char.h"
27 #include "qapi/error.h"
28 #include "qapi/qapi-commands-char.h"
29 #include "qemu/base64.h"
30 #include "qemu/module.h"
31 #include "qemu/option.h"
32 #include "qom/object.h"
33
34 /* Ring buffer chardev */
35
36 struct RingBufChardev {
37 Chardev parent;
38 size_t size;
39 size_t prod;
40 size_t cons;
41 uint8_t *cbuf;
42 };
43 typedef struct RingBufChardev RingBufChardev;
44
45 DECLARE_INSTANCE_CHECKER(RingBufChardev, RINGBUF_CHARDEV,
46 TYPE_CHARDEV_RINGBUF)
47
48 static size_t ringbuf_count(const Chardev *chr)
49 {
50 const RingBufChardev *d = RINGBUF_CHARDEV(chr);
51
52 return d->prod - d->cons;
53 }
54
55 static int ringbuf_chr_write(Chardev *chr, const uint8_t *buf, int len)
56 {
57 RingBufChardev *d = RINGBUF_CHARDEV(chr);
58 int i;
59
60 if (!buf || (len < 0)) {
61 return -1;
62 }
63
64 for (i = 0; i < len; i++) {
65 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
66 if (d->prod - d->cons > d->size) {
67 d->cons = d->prod - d->size;
68 }
69 }
70
71 return len;
72 }
73
74 static int ringbuf_chr_read(Chardev *chr, uint8_t *buf, int len)
75 {
76 RingBufChardev *d = RINGBUF_CHARDEV(chr);
77 int i;
78
79 qemu_mutex_lock(&chr->chr_write_lock);
80 for (i = 0; i < len && d->cons != d->prod; i++) {
81 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
82 }
83 qemu_mutex_unlock(&chr->chr_write_lock);
84
85 return i;
86 }
87
88 static void char_ringbuf_finalize(Object *obj)
89 {
90 RingBufChardev *d = RINGBUF_CHARDEV(obj);
91
92 g_free(d->cbuf);
93 }
94
95 static bool ringbuf_chr_open(Chardev *chr,
96 ChardevBackend *backend,
97 Error **errp)
98 {
99 ChardevRingbuf *opts = backend->u.ringbuf.data;
100 RingBufChardev *d = RINGBUF_CHARDEV(chr);
101
102 d->size = opts->has_size ? opts->size : 65536;
103
104 /* The size must be power of 2 */
105 if (d->size & (d->size - 1)) {
106 error_setg(errp, "size of ringbuf chardev must be power of two");
107 return false;
108 }
109
110 d->prod = 0;
111 d->cons = 0;
112 d->cbuf = g_malloc0(d->size);
113
114 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
115 return true;
116 }
117
118 void qmp_ringbuf_write(const char *device, const char *data,
119 bool has_format, enum DataFormat format,
120 Error **errp)
121 {
122 Chardev *chr;
123 const uint8_t *write_data;
124 int ret;
125 gsize write_count;
126
127 chr = qemu_chr_find(device);
128 if (!chr) {
129 error_setg(errp, "Device '%s' not found", device);
130 return;
131 }
132
133 if (!CHARDEV_IS_RINGBUF(chr)) {
134 error_setg(errp, "%s is not a ringbuf device", device);
135 return;
136 }
137
138 if (has_format && (format == DATA_FORMAT_BASE64)) {
139 write_data = qbase64_decode(data, -1,
140 &write_count,
141 errp);
142 if (!write_data) {
143 return;
144 }
145 } else {
146 write_data = (uint8_t *)data;
147 write_count = strlen(data);
148 }
149
150 ret = ringbuf_chr_write(chr, write_data, write_count);
151
152 if (write_data != (uint8_t *)data) {
153 g_free((void *)write_data);
154 }
155
156 if (ret < 0) {
157 error_setg(errp, "Failed to write to device %s", device);
158 return;
159 }
160 }
161
162 char *qmp_ringbuf_read(const char *device, int64_t size,
163 bool has_format, enum DataFormat format,
164 Error **errp)
165 {
166 Chardev *chr;
167 uint8_t *read_data;
168 size_t count;
169 char *data;
170
171 chr = qemu_chr_find(device);
172 if (!chr) {
173 error_setg(errp, "Device '%s' not found", device);
174 return NULL;
175 }
176
177 if (!CHARDEV_IS_RINGBUF(chr)) {
178 error_setg(errp, "%s is not a ringbuf device", device);
179 return NULL;
180 }
181
182 if (size <= 0) {
183 error_setg(errp, "size must be greater than zero");
184 return NULL;
185 }
186
187 count = ringbuf_count(chr);
188 size = size > count ? count : size;
189 read_data = g_malloc(size + 1);
190
191 ringbuf_chr_read(chr, read_data, size);
192
193 if (has_format && (format == DATA_FORMAT_BASE64)) {
194 data = g_base64_encode(read_data, size);
195 g_free(read_data);
196 } else {
197 /*
198 * FIXME should read only complete, valid UTF-8 characters up
199 * to @size bytes. Invalid sequences should be replaced by a
200 * suitable replacement character. Except when (and only
201 * when) ring buffer lost characters since last read, initial
202 * continuation characters should be dropped.
203 */
204 read_data[size] = 0;
205 data = (char *)read_data;
206 }
207
208 return data;
209 }
210
211 static void ringbuf_chr_parse(QemuOpts *opts, ChardevBackend *backend,
212 Error **errp)
213 {
214 int val;
215 ChardevRingbuf *ringbuf;
216
217 backend->type = CHARDEV_BACKEND_KIND_RINGBUF;
218 ringbuf = backend->u.ringbuf.data = g_new0(ChardevRingbuf, 1);
219 qemu_chr_parse_common(opts, qapi_ChardevRingbuf_base(ringbuf));
220
221 val = qemu_opt_get_size(opts, "size", 0);
222 if (val != 0) {
223 ringbuf->has_size = true;
224 ringbuf->size = val;
225 }
226 }
227
228 static void char_ringbuf_class_init(ObjectClass *oc, const void *data)
229 {
230 ChardevClass *cc = CHARDEV_CLASS(oc);
231
232 cc->chr_parse = ringbuf_chr_parse;
233 cc->chr_open = ringbuf_chr_open;
234 cc->chr_write = ringbuf_chr_write;
235 }
236
237 static const TypeInfo char_ringbuf_type_info = {
238 .name = TYPE_CHARDEV_RINGBUF,
239 .parent = TYPE_CHARDEV,
240 .class_init = char_ringbuf_class_init,
241 .instance_size = sizeof(RingBufChardev),
242 .instance_finalize = char_ringbuf_finalize,
243 };
244
245 /* Bug-compatibility: */
246 static const TypeInfo char_memory_type_info = {
247 .name = TYPE_CHARDEV_MEMORY,
248 .parent = TYPE_CHARDEV_RINGBUF,
249 };
250
251 static void register_types(void)
252 {
253 type_register_static(&char_ringbuf_type_info);
254 type_register_static(&char_memory_type_info);
255 }
256
257 type_init(register_types);