]> git.proxmox.com Git - mirror_qemu.git/blob - chardev/char-ringbuf.c
Include qapi/error.h exactly where needed
[mirror_qemu.git] / chardev / char-ringbuf.c
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 "qmp-commands.h"
28 #include "qapi/error.h"
29 #include "qemu/base64.h"
30
31 /* Ring buffer chardev */
32
33 typedef struct {
34 Chardev parent;
35 size_t size;
36 size_t prod;
37 size_t cons;
38 uint8_t *cbuf;
39 } RingBufChardev;
40
41 #define RINGBUF_CHARDEV(obj) \
42 OBJECT_CHECK(RingBufChardev, (obj), TYPE_CHARDEV_RINGBUF)
43
44 static size_t ringbuf_count(const Chardev *chr)
45 {
46 const RingBufChardev *d = RINGBUF_CHARDEV(chr);
47
48 return d->prod - d->cons;
49 }
50
51 static int ringbuf_chr_write(Chardev *chr, const uint8_t *buf, int len)
52 {
53 RingBufChardev *d = RINGBUF_CHARDEV(chr);
54 int i;
55
56 if (!buf || (len < 0)) {
57 return -1;
58 }
59
60 for (i = 0; i < len; i++) {
61 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
62 if (d->prod - d->cons > d->size) {
63 d->cons = d->prod - d->size;
64 }
65 }
66
67 return len;
68 }
69
70 static int ringbuf_chr_read(Chardev *chr, uint8_t *buf, int len)
71 {
72 RingBufChardev *d = RINGBUF_CHARDEV(chr);
73 int i;
74
75 qemu_mutex_lock(&chr->chr_write_lock);
76 for (i = 0; i < len && d->cons != d->prod; i++) {
77 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
78 }
79 qemu_mutex_unlock(&chr->chr_write_lock);
80
81 return i;
82 }
83
84 static void char_ringbuf_finalize(Object *obj)
85 {
86 RingBufChardev *d = RINGBUF_CHARDEV(obj);
87
88 g_free(d->cbuf);
89 }
90
91 static void qemu_chr_open_ringbuf(Chardev *chr,
92 ChardevBackend *backend,
93 bool *be_opened,
94 Error **errp)
95 {
96 ChardevRingbuf *opts = backend->u.ringbuf.data;
97 RingBufChardev *d = RINGBUF_CHARDEV(chr);
98
99 d->size = opts->has_size ? opts->size : 65536;
100
101 /* The size must be power of 2 */
102 if (d->size & (d->size - 1)) {
103 error_setg(errp, "size of ringbuf chardev must be power of two");
104 return;
105 }
106
107 d->prod = 0;
108 d->cons = 0;
109 d->cbuf = g_malloc0(d->size);
110 }
111
112 void qmp_ringbuf_write(const char *device, const char *data,
113 bool has_format, enum DataFormat format,
114 Error **errp)
115 {
116 Chardev *chr;
117 const uint8_t *write_data;
118 int ret;
119 gsize write_count;
120
121 chr = qemu_chr_find(device);
122 if (!chr) {
123 error_setg(errp, "Device '%s' not found", device);
124 return;
125 }
126
127 if (!CHARDEV_IS_RINGBUF(chr)) {
128 error_setg(errp, "%s is not a ringbuf device", device);
129 return;
130 }
131
132 if (has_format && (format == DATA_FORMAT_BASE64)) {
133 write_data = qbase64_decode(data, -1,
134 &write_count,
135 errp);
136 if (!write_data) {
137 return;
138 }
139 } else {
140 write_data = (uint8_t *)data;
141 write_count = strlen(data);
142 }
143
144 ret = ringbuf_chr_write(chr, write_data, write_count);
145
146 if (write_data != (uint8_t *)data) {
147 g_free((void *)write_data);
148 }
149
150 if (ret < 0) {
151 error_setg(errp, "Failed to write to device %s", device);
152 return;
153 }
154 }
155
156 char *qmp_ringbuf_read(const char *device, int64_t size,
157 bool has_format, enum DataFormat format,
158 Error **errp)
159 {
160 Chardev *chr;
161 uint8_t *read_data;
162 size_t count;
163 char *data;
164
165 chr = qemu_chr_find(device);
166 if (!chr) {
167 error_setg(errp, "Device '%s' not found", device);
168 return NULL;
169 }
170
171 if (!CHARDEV_IS_RINGBUF(chr)) {
172 error_setg(errp, "%s is not a ringbuf device", device);
173 return NULL;
174 }
175
176 if (size <= 0) {
177 error_setg(errp, "size must be greater than zero");
178 return NULL;
179 }
180
181 count = ringbuf_count(chr);
182 size = size > count ? count : size;
183 read_data = g_malloc(size + 1);
184
185 ringbuf_chr_read(chr, read_data, size);
186
187 if (has_format && (format == DATA_FORMAT_BASE64)) {
188 data = g_base64_encode(read_data, size);
189 g_free(read_data);
190 } else {
191 /*
192 * FIXME should read only complete, valid UTF-8 characters up
193 * to @size bytes. Invalid sequences should be replaced by a
194 * suitable replacement character. Except when (and only
195 * when) ring buffer lost characters since last read, initial
196 * continuation characters should be dropped.
197 */
198 read_data[size] = 0;
199 data = (char *)read_data;
200 }
201
202 return data;
203 }
204
205 static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
206 Error **errp)
207 {
208 int val;
209 ChardevRingbuf *ringbuf;
210
211 backend->type = CHARDEV_BACKEND_KIND_RINGBUF;
212 ringbuf = backend->u.ringbuf.data = g_new0(ChardevRingbuf, 1);
213 qemu_chr_parse_common(opts, qapi_ChardevRingbuf_base(ringbuf));
214
215 val = qemu_opt_get_size(opts, "size", 0);
216 if (val != 0) {
217 ringbuf->has_size = true;
218 ringbuf->size = val;
219 }
220 }
221
222 static void char_ringbuf_class_init(ObjectClass *oc, void *data)
223 {
224 ChardevClass *cc = CHARDEV_CLASS(oc);
225
226 cc->parse = qemu_chr_parse_ringbuf;
227 cc->open = qemu_chr_open_ringbuf;
228 cc->chr_write = ringbuf_chr_write;
229 }
230
231 static const TypeInfo char_ringbuf_type_info = {
232 .name = TYPE_CHARDEV_RINGBUF,
233 .parent = TYPE_CHARDEV,
234 .class_init = char_ringbuf_class_init,
235 .instance_size = sizeof(RingBufChardev),
236 .instance_finalize = char_ringbuf_finalize,
237 };
238
239 /* Bug-compatibility: */
240 static const TypeInfo char_memory_type_info = {
241 .name = TYPE_CHARDEV_MEMORY,
242 .parent = TYPE_CHARDEV_RINGBUF,
243 };
244
245 static void register_types(void)
246 {
247 type_register_static(&char_ringbuf_type_info);
248 type_register_static(&char_memory_type_info);
249 }
250
251 type_init(register_types);