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"
36
struct RingBufChardev {
43
typedef struct RingBufChardev RingBufChardev;
45
DECLARE_INSTANCE_CHECKER(RingBufChardev, RINGBUF_CHARDEV,
48
static size_t ringbuf_count(const Chardev *chr)
50
const RingBufChardev *d = RINGBUF_CHARDEV(chr);
52
return d->prod - d->cons;
55
static int ringbuf_chr_write(Chardev *chr, const uint8_t *buf, int len)
57
RingBufChardev *d = RINGBUF_CHARDEV(chr);
60
if (!buf || (len < 0)) {
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;
74
static int ringbuf_chr_read(Chardev *chr, uint8_t *buf, int len)
76
RingBufChardev *d = RINGBUF_CHARDEV(chr);
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)];
83
qemu_mutex_unlock(&chr->chr_write_lock);
88
static void char_ringbuf_finalize(Object *obj)
90
RingBufChardev *d = RINGBUF_CHARDEV(obj);
95
static void qemu_chr_open_ringbuf(Chardev *chr,
96
ChardevBackend *backend,
100
ChardevRingbuf *opts = backend->u.ringbuf.data;
101
RingBufChardev *d = RINGBUF_CHARDEV(chr);
103
d->size = opts->has_size ? opts->size : 65536;
106
if (d->size & (d->size - 1)) {
107
error_setg(errp, "size of ringbuf chardev must be power of two");
113
d->cbuf = g_malloc0(d->size);
116
void qmp_ringbuf_write(const char *device, const char *data,
117
bool has_format, enum DataFormat format,
121
const uint8_t *write_data;
125
chr = qemu_chr_find(device);
127
error_setg(errp, "Device '%s' not found", device);
131
if (!CHARDEV_IS_RINGBUF(chr)) {
132
error_setg(errp, "%s is not a ringbuf device", device);
136
if (has_format && (format == DATA_FORMAT_BASE64)) {
137
write_data = qbase64_decode(data, -1,
144
write_data = (uint8_t *)data;
145
write_count = strlen(data);
148
ret = ringbuf_chr_write(chr, write_data, write_count);
150
if (write_data != (uint8_t *)data) {
151
g_free((void *)write_data);
155
error_setg(errp, "Failed to write to device %s", device);
160
char *qmp_ringbuf_read(const char *device, int64_t size,
161
bool has_format, enum DataFormat format,
169
chr = qemu_chr_find(device);
171
error_setg(errp, "Device '%s' not found", device);
175
if (!CHARDEV_IS_RINGBUF(chr)) {
176
error_setg(errp, "%s is not a ringbuf device", device);
181
error_setg(errp, "size must be greater than zero");
185
count = ringbuf_count(chr);
186
size = size > count ? count : size;
187
read_data = g_malloc(size + 1);
189
ringbuf_chr_read(chr, read_data, size);
191
if (has_format && (format == DATA_FORMAT_BASE64)) {
192
data = g_base64_encode(read_data, size);
203
data = (char *)read_data;
209
static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
213
ChardevRingbuf *ringbuf;
215
backend->type = CHARDEV_BACKEND_KIND_RINGBUF;
216
ringbuf = backend->u.ringbuf.data = g_new0(ChardevRingbuf, 1);
217
qemu_chr_parse_common(opts, qapi_ChardevRingbuf_base(ringbuf));
219
val = qemu_opt_get_size(opts, "size", 0);
221
ringbuf->has_size = true;
226
static void char_ringbuf_class_init(ObjectClass *oc, void *data)
228
ChardevClass *cc = CHARDEV_CLASS(oc);
230
cc->parse = qemu_chr_parse_ringbuf;
231
cc->open = qemu_chr_open_ringbuf;
232
cc->chr_write = ringbuf_chr_write;
235
static const TypeInfo char_ringbuf_type_info = {
236
.name = TYPE_CHARDEV_RINGBUF,
237
.parent = TYPE_CHARDEV,
238
.class_init = char_ringbuf_class_init,
239
.instance_size = sizeof(RingBufChardev),
240
.instance_finalize = char_ringbuf_finalize,
244
static const TypeInfo char_memory_type_info = {
245
.name = TYPE_CHARDEV_MEMORY,
246
.parent = TYPE_CHARDEV_RINGBUF,
249
static void register_types(void)
251
type_register_static(&char_ringbuf_type_info);
252
type_register_static(&char_memory_type_info);
255
type_init(register_types);