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