2008-10-13 03:10:22 +00:00
|
|
|
/*
|
|
|
|
* QEMU buffered QEMUFile
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2008
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2. See
|
|
|
|
* the COPYING file in the top-level directory.
|
|
|
|
*
|
2012-01-13 16:44:23 +00:00
|
|
|
* Contributions after 2012-01-13 are licensed under the terms of the
|
|
|
|
* GNU GPL, version 2 or (at your option) any later version.
|
2008-10-13 03:10:22 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu-common.h"
|
|
|
|
#include "hw/hw.h"
|
|
|
|
#include "qemu-timer.h"
|
|
|
|
#include "qemu-char.h"
|
|
|
|
#include "buffered_file.h"
|
|
|
|
|
|
|
|
//#define DEBUG_BUFFERED_FILE
|
|
|
|
|
|
|
|
typedef struct QEMUFileBuffered
|
|
|
|
{
|
|
|
|
BufferedPutFunc *put_buffer;
|
|
|
|
BufferedPutReadyFunc *put_ready;
|
|
|
|
BufferedWaitForUnfreezeFunc *wait_for_unfreeze;
|
|
|
|
BufferedCloseFunc *close;
|
|
|
|
void *opaque;
|
|
|
|
QEMUFile *file;
|
|
|
|
int freeze_output;
|
|
|
|
size_t bytes_xfer;
|
|
|
|
size_t xfer_limit;
|
|
|
|
uint8_t *buffer;
|
|
|
|
size_t buffer_size;
|
|
|
|
size_t buffer_capacity;
|
|
|
|
QEMUTimer *timer;
|
|
|
|
} QEMUFileBuffered;
|
|
|
|
|
|
|
|
#ifdef DEBUG_BUFFERED_FILE
|
2010-02-06 23:03:50 +00:00
|
|
|
#define DPRINTF(fmt, ...) \
|
2008-10-13 03:10:22 +00:00
|
|
|
do { printf("buffered-file: " fmt, ## __VA_ARGS__); } while (0)
|
|
|
|
#else
|
2010-02-06 23:03:50 +00:00
|
|
|
#define DPRINTF(fmt, ...) \
|
2008-10-13 03:10:22 +00:00
|
|
|
do { } while (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void buffered_append(QEMUFileBuffered *s,
|
|
|
|
const uint8_t *buf, size_t size)
|
|
|
|
{
|
|
|
|
if (size > (s->buffer_capacity - s->buffer_size)) {
|
|
|
|
void *tmp;
|
|
|
|
|
2010-02-06 23:03:50 +00:00
|
|
|
DPRINTF("increasing buffer capacity from %zu by %zu\n",
|
2008-10-13 03:10:22 +00:00
|
|
|
s->buffer_capacity, size + 1024);
|
|
|
|
|
|
|
|
s->buffer_capacity += size + 1024;
|
|
|
|
|
2011-08-21 03:09:37 +00:00
|
|
|
tmp = g_realloc(s->buffer, s->buffer_capacity);
|
2008-10-13 03:10:22 +00:00
|
|
|
if (tmp == NULL) {
|
|
|
|
fprintf(stderr, "qemu file buffer expansion failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
s->buffer = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(s->buffer + s->buffer_size, buf, size);
|
|
|
|
s->buffer_size += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void buffered_flush(QEMUFileBuffered *s)
|
|
|
|
{
|
|
|
|
size_t offset = 0;
|
2011-10-04 23:14:46 +00:00
|
|
|
int error;
|
2008-10-13 03:10:22 +00:00
|
|
|
|
2011-10-04 23:14:46 +00:00
|
|
|
error = qemu_file_get_error(s->file);
|
|
|
|
if (error != 0) {
|
|
|
|
DPRINTF("flush when error, bailing: %s\n", strerror(-error));
|
2008-10-13 03:10:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-06 23:03:50 +00:00
|
|
|
DPRINTF("flushing %zu byte(s) of data\n", s->buffer_size);
|
2008-10-13 03:10:22 +00:00
|
|
|
|
|
|
|
while (offset < s->buffer_size) {
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
ret = s->put_buffer(s->opaque, s->buffer + offset,
|
|
|
|
s->buffer_size - offset);
|
|
|
|
if (ret == -EAGAIN) {
|
2010-02-06 23:03:50 +00:00
|
|
|
DPRINTF("backend not ready, freezing\n");
|
2008-10-13 03:10:22 +00:00
|
|
|
s->freeze_output = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret <= 0) {
|
2010-02-06 23:03:50 +00:00
|
|
|
DPRINTF("error flushing data, %zd\n", ret);
|
2011-09-21 21:01:54 +00:00
|
|
|
qemu_file_set_error(s->file, ret);
|
2008-10-13 03:10:22 +00:00
|
|
|
break;
|
|
|
|
} else {
|
2010-02-06 23:03:50 +00:00
|
|
|
DPRINTF("flushed %zd byte(s)\n", ret);
|
2008-10-13 03:10:22 +00:00
|
|
|
offset += ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-06 23:03:50 +00:00
|
|
|
DPRINTF("flushed %zu of %zu byte(s)\n", offset, s->buffer_size);
|
2008-10-13 03:10:22 +00:00
|
|
|
memmove(s->buffer, s->buffer + offset, s->buffer_size - offset);
|
|
|
|
s->buffer_size -= offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int buffered_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
|
|
|
|
{
|
|
|
|
QEMUFileBuffered *s = opaque;
|
2011-10-04 23:14:46 +00:00
|
|
|
int offset = 0, error;
|
2008-10-13 03:10:22 +00:00
|
|
|
ssize_t ret;
|
|
|
|
|
2010-02-06 23:03:50 +00:00
|
|
|
DPRINTF("putting %d bytes at %" PRId64 "\n", size, pos);
|
2008-10-13 03:10:22 +00:00
|
|
|
|
2011-10-04 23:14:46 +00:00
|
|
|
error = qemu_file_get_error(s->file);
|
|
|
|
if (error) {
|
|
|
|
DPRINTF("flush when error, bailing: %s\n", strerror(-error));
|
|
|
|
return error;
|
2008-10-13 03:10:22 +00:00
|
|
|
}
|
|
|
|
|
2010-02-06 23:03:50 +00:00
|
|
|
DPRINTF("unfreezing output\n");
|
2008-10-13 03:10:22 +00:00
|
|
|
s->freeze_output = 0;
|
|
|
|
|
|
|
|
buffered_flush(s);
|
|
|
|
|
|
|
|
while (!s->freeze_output && offset < size) {
|
|
|
|
if (s->bytes_xfer > s->xfer_limit) {
|
2010-02-06 23:03:50 +00:00
|
|
|
DPRINTF("transfer limit exceeded when putting\n");
|
2008-10-13 03:10:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = s->put_buffer(s->opaque, buf + offset, size - offset);
|
|
|
|
if (ret == -EAGAIN) {
|
2010-02-06 23:03:50 +00:00
|
|
|
DPRINTF("backend not ready, freezing\n");
|
2008-10-13 03:10:22 +00:00
|
|
|
s->freeze_output = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret <= 0) {
|
2010-02-06 23:03:50 +00:00
|
|
|
DPRINTF("error putting\n");
|
2011-09-21 21:01:54 +00:00
|
|
|
qemu_file_set_error(s->file, ret);
|
2008-10-13 03:10:22 +00:00
|
|
|
offset = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-02-06 23:03:50 +00:00
|
|
|
DPRINTF("put %zd byte(s)\n", ret);
|
2008-10-13 03:10:22 +00:00
|
|
|
offset += ret;
|
|
|
|
s->bytes_xfer += ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset >= 0) {
|
2010-02-06 23:03:50 +00:00
|
|
|
DPRINTF("buffering %d bytes\n", size - offset);
|
2008-10-13 03:10:22 +00:00
|
|
|
buffered_append(s, buf + offset, size - offset);
|
|
|
|
offset = size;
|
|
|
|
}
|
|
|
|
|
2010-07-07 16:44:22 +00:00
|
|
|
if (pos == 0 && size == 0) {
|
|
|
|
DPRINTF("file is ready\n");
|
|
|
|
if (s->bytes_xfer <= s->xfer_limit) {
|
|
|
|
DPRINTF("notifying client\n");
|
|
|
|
s->put_ready(s->opaque);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-13 03:10:22 +00:00
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int buffered_close(void *opaque)
|
|
|
|
{
|
|
|
|
QEMUFileBuffered *s = opaque;
|
|
|
|
int ret;
|
|
|
|
|
2010-02-06 23:03:50 +00:00
|
|
|
DPRINTF("closing\n");
|
2008-10-13 03:10:22 +00:00
|
|
|
|
2011-10-04 23:02:52 +00:00
|
|
|
while (!qemu_file_get_error(s->file) && s->buffer_size) {
|
2008-10-13 03:10:22 +00:00
|
|
|
buffered_flush(s);
|
|
|
|
if (s->freeze_output)
|
2011-09-21 16:12:58 +00:00
|
|
|
s->wait_for_unfreeze(s->opaque);
|
2008-10-13 03:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = s->close(s->opaque);
|
|
|
|
|
|
|
|
qemu_del_timer(s->timer);
|
|
|
|
qemu_free_timer(s->timer);
|
2011-08-21 03:09:37 +00:00
|
|
|
g_free(s->buffer);
|
|
|
|
g_free(s);
|
2008-10-13 03:10:22 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-02-23 19:17:45 +00:00
|
|
|
/*
|
|
|
|
* The meaning of the return values is:
|
|
|
|
* 0: We can continue sending
|
|
|
|
* 1: Time to stop
|
2011-10-04 23:14:46 +00:00
|
|
|
* negative: There has been an error
|
2011-02-23 19:17:45 +00:00
|
|
|
*/
|
2008-10-13 03:10:22 +00:00
|
|
|
static int buffered_rate_limit(void *opaque)
|
|
|
|
{
|
|
|
|
QEMUFileBuffered *s = opaque;
|
2011-10-04 23:14:46 +00:00
|
|
|
int ret;
|
2008-10-13 03:10:22 +00:00
|
|
|
|
2011-10-04 23:14:46 +00:00
|
|
|
ret = qemu_file_get_error(s->file);
|
|
|
|
if (ret) {
|
|
|
|
return ret;
|
2011-02-23 19:17:45 +00:00
|
|
|
}
|
2008-10-13 03:10:22 +00:00
|
|
|
if (s->freeze_output)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (s->bytes_xfer > s->xfer_limit)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-23 17:05:54 +00:00
|
|
|
static int64_t buffered_set_rate_limit(void *opaque, int64_t new_rate)
|
2009-05-20 22:26:57 +00:00
|
|
|
{
|
|
|
|
QEMUFileBuffered *s = opaque;
|
2011-10-04 23:02:52 +00:00
|
|
|
if (qemu_file_get_error(s->file)) {
|
2009-05-20 22:26:57 +00:00
|
|
|
goto out;
|
2011-10-04 23:02:52 +00:00
|
|
|
}
|
2010-11-23 17:05:54 +00:00
|
|
|
if (new_rate > SIZE_MAX) {
|
|
|
|
new_rate = SIZE_MAX;
|
|
|
|
}
|
|
|
|
|
2009-05-20 22:26:57 +00:00
|
|
|
s->xfer_limit = new_rate / 10;
|
|
|
|
|
|
|
|
out:
|
|
|
|
return s->xfer_limit;
|
|
|
|
}
|
|
|
|
|
2010-11-23 17:05:54 +00:00
|
|
|
static int64_t buffered_get_rate_limit(void *opaque)
|
2009-11-02 13:40:58 +00:00
|
|
|
{
|
|
|
|
QEMUFileBuffered *s = opaque;
|
|
|
|
|
|
|
|
return s->xfer_limit;
|
|
|
|
}
|
|
|
|
|
2008-10-13 03:10:22 +00:00
|
|
|
static void buffered_rate_tick(void *opaque)
|
|
|
|
{
|
|
|
|
QEMUFileBuffered *s = opaque;
|
|
|
|
|
2011-10-04 23:02:52 +00:00
|
|
|
if (qemu_file_get_error(s->file)) {
|
2010-08-19 13:18:39 +00:00
|
|
|
buffered_close(s);
|
2008-10-13 03:10:22 +00:00
|
|
|
return;
|
2010-08-19 13:18:39 +00:00
|
|
|
}
|
2008-10-13 03:10:22 +00:00
|
|
|
|
2011-03-11 15:47:48 +00:00
|
|
|
qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 100);
|
2008-10-13 03:10:22 +00:00
|
|
|
|
|
|
|
if (s->freeze_output)
|
|
|
|
return;
|
|
|
|
|
|
|
|
s->bytes_xfer = 0;
|
|
|
|
|
|
|
|
buffered_flush(s);
|
|
|
|
|
|
|
|
/* Add some checks around this */
|
|
|
|
s->put_ready(s->opaque);
|
|
|
|
}
|
|
|
|
|
|
|
|
QEMUFile *qemu_fopen_ops_buffered(void *opaque,
|
|
|
|
size_t bytes_per_sec,
|
|
|
|
BufferedPutFunc *put_buffer,
|
|
|
|
BufferedPutReadyFunc *put_ready,
|
|
|
|
BufferedWaitForUnfreezeFunc *wait_for_unfreeze,
|
|
|
|
BufferedCloseFunc *close)
|
|
|
|
{
|
|
|
|
QEMUFileBuffered *s;
|
|
|
|
|
2011-08-21 03:09:37 +00:00
|
|
|
s = g_malloc0(sizeof(*s));
|
2008-10-13 03:10:22 +00:00
|
|
|
|
|
|
|
s->opaque = opaque;
|
|
|
|
s->xfer_limit = bytes_per_sec / 10;
|
|
|
|
s->put_buffer = put_buffer;
|
|
|
|
s->put_ready = put_ready;
|
|
|
|
s->wait_for_unfreeze = wait_for_unfreeze;
|
|
|
|
s->close = close;
|
|
|
|
|
|
|
|
s->file = qemu_fopen_ops(s, buffered_put_buffer, NULL,
|
2009-05-20 22:26:57 +00:00
|
|
|
buffered_close, buffered_rate_limit,
|
2009-11-02 13:40:58 +00:00
|
|
|
buffered_set_rate_limit,
|
|
|
|
buffered_get_rate_limit);
|
2008-10-13 03:10:22 +00:00
|
|
|
|
2011-03-11 15:47:48 +00:00
|
|
|
s->timer = qemu_new_timer_ms(rt_clock, buffered_rate_tick, s);
|
2008-10-13 03:10:22 +00:00
|
|
|
|
2011-03-11 15:47:48 +00:00
|
|
|
qemu_mod_timer(s->timer, qemu_get_clock_ms(rt_clock) + 100);
|
2008-10-13 03:10:22 +00:00
|
|
|
|
|
|
|
return s->file;
|
|
|
|
}
|