aliguori | 39b65c2 | 2008-10-13 03:10:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU buffered QEMUFile |
| 3 | * |
| 4 | * Copyright IBM, Corp. 2008 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Anthony Liguori <aliguori@us.ibm.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 10 | * the COPYING file in the top-level directory. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include "qemu-common.h" |
| 15 | #include "hw/hw.h" |
| 16 | #include "qemu-timer.h" |
| 17 | #include "sysemu.h" |
| 18 | #include "qemu-char.h" |
| 19 | #include "buffered_file.h" |
| 20 | |
| 21 | //#define DEBUG_BUFFERED_FILE |
| 22 | |
| 23 | typedef struct QEMUFileBuffered |
| 24 | { |
| 25 | BufferedPutFunc *put_buffer; |
| 26 | BufferedPutReadyFunc *put_ready; |
| 27 | BufferedWaitForUnfreezeFunc *wait_for_unfreeze; |
| 28 | BufferedCloseFunc *close; |
| 29 | void *opaque; |
| 30 | QEMUFile *file; |
| 31 | int has_error; |
| 32 | int freeze_output; |
| 33 | size_t bytes_xfer; |
| 34 | size_t xfer_limit; |
| 35 | uint8_t *buffer; |
| 36 | size_t buffer_size; |
| 37 | size_t buffer_capacity; |
| 38 | QEMUTimer *timer; |
| 39 | } QEMUFileBuffered; |
| 40 | |
| 41 | #ifdef DEBUG_BUFFERED_FILE |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 42 | #define DPRINTF(fmt, ...) \ |
aliguori | 39b65c2 | 2008-10-13 03:10:22 +0000 | [diff] [blame] | 43 | do { printf("buffered-file: " fmt, ## __VA_ARGS__); } while (0) |
| 44 | #else |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 45 | #define DPRINTF(fmt, ...) \ |
aliguori | 39b65c2 | 2008-10-13 03:10:22 +0000 | [diff] [blame] | 46 | do { } while (0) |
| 47 | #endif |
| 48 | |
| 49 | static void buffered_append(QEMUFileBuffered *s, |
| 50 | const uint8_t *buf, size_t size) |
| 51 | { |
| 52 | if (size > (s->buffer_capacity - s->buffer_size)) { |
| 53 | void *tmp; |
| 54 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 55 | DPRINTF("increasing buffer capacity from %zu by %zu\n", |
aliguori | 39b65c2 | 2008-10-13 03:10:22 +0000 | [diff] [blame] | 56 | s->buffer_capacity, size + 1024); |
| 57 | |
| 58 | s->buffer_capacity += size + 1024; |
| 59 | |
| 60 | tmp = qemu_realloc(s->buffer, s->buffer_capacity); |
| 61 | if (tmp == NULL) { |
| 62 | fprintf(stderr, "qemu file buffer expansion failed\n"); |
| 63 | exit(1); |
| 64 | } |
| 65 | |
| 66 | s->buffer = tmp; |
| 67 | } |
| 68 | |
| 69 | memcpy(s->buffer + s->buffer_size, buf, size); |
| 70 | s->buffer_size += size; |
| 71 | } |
| 72 | |
| 73 | static void buffered_flush(QEMUFileBuffered *s) |
| 74 | { |
| 75 | size_t offset = 0; |
| 76 | |
| 77 | if (s->has_error) { |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 78 | DPRINTF("flush when error, bailing\n"); |
aliguori | 39b65c2 | 2008-10-13 03:10:22 +0000 | [diff] [blame] | 79 | return; |
| 80 | } |
| 81 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 82 | DPRINTF("flushing %zu byte(s) of data\n", s->buffer_size); |
aliguori | 39b65c2 | 2008-10-13 03:10:22 +0000 | [diff] [blame] | 83 | |
| 84 | while (offset < s->buffer_size) { |
| 85 | ssize_t ret; |
| 86 | |
| 87 | ret = s->put_buffer(s->opaque, s->buffer + offset, |
| 88 | s->buffer_size - offset); |
| 89 | if (ret == -EAGAIN) { |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 90 | DPRINTF("backend not ready, freezing\n"); |
aliguori | 39b65c2 | 2008-10-13 03:10:22 +0000 | [diff] [blame] | 91 | s->freeze_output = 1; |
| 92 | break; |
| 93 | } |
| 94 | |
| 95 | if (ret <= 0) { |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 96 | DPRINTF("error flushing data, %zd\n", ret); |
aliguori | 39b65c2 | 2008-10-13 03:10:22 +0000 | [diff] [blame] | 97 | s->has_error = 1; |
| 98 | break; |
| 99 | } else { |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 100 | DPRINTF("flushed %zd byte(s)\n", ret); |
aliguori | 39b65c2 | 2008-10-13 03:10:22 +0000 | [diff] [blame] | 101 | offset += ret; |
| 102 | } |
| 103 | } |
| 104 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 105 | DPRINTF("flushed %zu of %zu byte(s)\n", offset, s->buffer_size); |
aliguori | 39b65c2 | 2008-10-13 03:10:22 +0000 | [diff] [blame] | 106 | memmove(s->buffer, s->buffer + offset, s->buffer_size - offset); |
| 107 | s->buffer_size -= offset; |
| 108 | } |
| 109 | |
| 110 | static int buffered_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size) |
| 111 | { |
| 112 | QEMUFileBuffered *s = opaque; |
| 113 | int offset = 0; |
| 114 | ssize_t ret; |
| 115 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 116 | DPRINTF("putting %d bytes at %" PRId64 "\n", size, pos); |
aliguori | 39b65c2 | 2008-10-13 03:10:22 +0000 | [diff] [blame] | 117 | |
| 118 | if (s->has_error) { |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 119 | DPRINTF("flush when error, bailing\n"); |
aliguori | 39b65c2 | 2008-10-13 03:10:22 +0000 | [diff] [blame] | 120 | return -EINVAL; |
| 121 | } |
| 122 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 123 | DPRINTF("unfreezing output\n"); |
aliguori | 39b65c2 | 2008-10-13 03:10:22 +0000 | [diff] [blame] | 124 | s->freeze_output = 0; |
| 125 | |
| 126 | buffered_flush(s); |
| 127 | |
| 128 | while (!s->freeze_output && offset < size) { |
| 129 | if (s->bytes_xfer > s->xfer_limit) { |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 130 | DPRINTF("transfer limit exceeded when putting\n"); |
aliguori | 39b65c2 | 2008-10-13 03:10:22 +0000 | [diff] [blame] | 131 | break; |
| 132 | } |
| 133 | |
| 134 | ret = s->put_buffer(s->opaque, buf + offset, size - offset); |
| 135 | if (ret == -EAGAIN) { |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 136 | DPRINTF("backend not ready, freezing\n"); |
aliguori | 39b65c2 | 2008-10-13 03:10:22 +0000 | [diff] [blame] | 137 | s->freeze_output = 1; |
| 138 | break; |
| 139 | } |
| 140 | |
| 141 | if (ret <= 0) { |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 142 | DPRINTF("error putting\n"); |
aliguori | 39b65c2 | 2008-10-13 03:10:22 +0000 | [diff] [blame] | 143 | s->has_error = 1; |
| 144 | offset = -EINVAL; |
| 145 | break; |
| 146 | } |
| 147 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 148 | DPRINTF("put %zd byte(s)\n", ret); |
aliguori | 39b65c2 | 2008-10-13 03:10:22 +0000 | [diff] [blame] | 149 | offset += ret; |
| 150 | s->bytes_xfer += ret; |
| 151 | } |
| 152 | |
| 153 | if (offset >= 0) { |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 154 | DPRINTF("buffering %d bytes\n", size - offset); |
aliguori | 39b65c2 | 2008-10-13 03:10:22 +0000 | [diff] [blame] | 155 | buffered_append(s, buf + offset, size - offset); |
| 156 | offset = size; |
| 157 | } |
| 158 | |
Avi Kivity | 5e77aaa | 2010-07-07 19:44:22 +0300 | [diff] [blame] | 159 | if (pos == 0 && size == 0) { |
| 160 | DPRINTF("file is ready\n"); |
| 161 | if (s->bytes_xfer <= s->xfer_limit) { |
| 162 | DPRINTF("notifying client\n"); |
| 163 | s->put_ready(s->opaque); |
| 164 | } |
| 165 | } |
| 166 | |
aliguori | 39b65c2 | 2008-10-13 03:10:22 +0000 | [diff] [blame] | 167 | return offset; |
| 168 | } |
| 169 | |
| 170 | static int buffered_close(void *opaque) |
| 171 | { |
| 172 | QEMUFileBuffered *s = opaque; |
| 173 | int ret; |
| 174 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 175 | DPRINTF("closing\n"); |
aliguori | 39b65c2 | 2008-10-13 03:10:22 +0000 | [diff] [blame] | 176 | |
| 177 | while (!s->has_error && s->buffer_size) { |
| 178 | buffered_flush(s); |
| 179 | if (s->freeze_output) |
| 180 | s->wait_for_unfreeze(s); |
| 181 | } |
| 182 | |
| 183 | ret = s->close(s->opaque); |
| 184 | |
| 185 | qemu_del_timer(s->timer); |
| 186 | qemu_free_timer(s->timer); |
| 187 | qemu_free(s->buffer); |
| 188 | qemu_free(s); |
| 189 | |
| 190 | return ret; |
| 191 | } |
| 192 | |
| 193 | static int buffered_rate_limit(void *opaque) |
| 194 | { |
| 195 | QEMUFileBuffered *s = opaque; |
| 196 | |
| 197 | if (s->has_error) |
| 198 | return 0; |
| 199 | |
| 200 | if (s->freeze_output) |
| 201 | return 1; |
| 202 | |
| 203 | if (s->bytes_xfer > s->xfer_limit) |
| 204 | return 1; |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
Glauber Costa | 1962953 | 2009-05-20 18:26:57 -0400 | [diff] [blame] | 209 | static size_t buffered_set_rate_limit(void *opaque, size_t new_rate) |
| 210 | { |
| 211 | QEMUFileBuffered *s = opaque; |
| 212 | |
| 213 | if (s->has_error) |
| 214 | goto out; |
| 215 | |
| 216 | s->xfer_limit = new_rate / 10; |
| 217 | |
| 218 | out: |
| 219 | return s->xfer_limit; |
| 220 | } |
| 221 | |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 222 | static size_t buffered_get_rate_limit(void *opaque) |
| 223 | { |
| 224 | QEMUFileBuffered *s = opaque; |
| 225 | |
| 226 | return s->xfer_limit; |
| 227 | } |
| 228 | |
aliguori | 39b65c2 | 2008-10-13 03:10:22 +0000 | [diff] [blame] | 229 | static void buffered_rate_tick(void *opaque) |
| 230 | { |
| 231 | QEMUFileBuffered *s = opaque; |
| 232 | |
Marcelo Tosatti | e447b1a | 2010-08-19 10:18:39 -0300 | [diff] [blame] | 233 | if (s->has_error) { |
| 234 | buffered_close(s); |
aliguori | 39b65c2 | 2008-10-13 03:10:22 +0000 | [diff] [blame] | 235 | return; |
Marcelo Tosatti | e447b1a | 2010-08-19 10:18:39 -0300 | [diff] [blame] | 236 | } |
aliguori | 39b65c2 | 2008-10-13 03:10:22 +0000 | [diff] [blame] | 237 | |
| 238 | qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 100); |
| 239 | |
| 240 | if (s->freeze_output) |
| 241 | return; |
| 242 | |
| 243 | s->bytes_xfer = 0; |
| 244 | |
| 245 | buffered_flush(s); |
| 246 | |
| 247 | /* Add some checks around this */ |
| 248 | s->put_ready(s->opaque); |
| 249 | } |
| 250 | |
| 251 | QEMUFile *qemu_fopen_ops_buffered(void *opaque, |
| 252 | size_t bytes_per_sec, |
| 253 | BufferedPutFunc *put_buffer, |
| 254 | BufferedPutReadyFunc *put_ready, |
| 255 | BufferedWaitForUnfreezeFunc *wait_for_unfreeze, |
| 256 | BufferedCloseFunc *close) |
| 257 | { |
| 258 | QEMUFileBuffered *s; |
| 259 | |
| 260 | s = qemu_mallocz(sizeof(*s)); |
aliguori | 39b65c2 | 2008-10-13 03:10:22 +0000 | [diff] [blame] | 261 | |
| 262 | s->opaque = opaque; |
| 263 | s->xfer_limit = bytes_per_sec / 10; |
| 264 | s->put_buffer = put_buffer; |
| 265 | s->put_ready = put_ready; |
| 266 | s->wait_for_unfreeze = wait_for_unfreeze; |
| 267 | s->close = close; |
| 268 | |
| 269 | s->file = qemu_fopen_ops(s, buffered_put_buffer, NULL, |
Glauber Costa | 1962953 | 2009-05-20 18:26:57 -0400 | [diff] [blame] | 270 | buffered_close, buffered_rate_limit, |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 271 | buffered_set_rate_limit, |
| 272 | buffered_get_rate_limit); |
aliguori | 39b65c2 | 2008-10-13 03:10:22 +0000 | [diff] [blame] | 273 | |
| 274 | s->timer = qemu_new_timer(rt_clock, buffered_rate_tick, s); |
| 275 | |
| 276 | qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 100); |
| 277 | |
| 278 | return s->file; |
| 279 | } |