]> git.proxmox.com Git - mirror_qemu.git/blame - migration/qemu-file.c
block/copy-before-write: implement cbw-timeout option
[mirror_qemu.git] / migration / qemu-file.c
CommitLineData
9a4ac51f
EH
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 */
1393a485 24#include "qemu/osdep.h"
44f0eadc 25#include <zlib.h>
b85ea5fa 26#include "qemu/madvise.h"
d49b6836 27#include "qemu/error-report.h"
093c455a 28#include "qemu/iov.h"
6666c96a 29#include "migration.h"
08a0aee1 30#include "qemu-file.h"
9013dca5 31#include "trace.h"
3d661c8a 32#include "qapi/error.h"
093c455a 33
a24939f2 34#define IO_BUF_SIZE 32768
f9919116 35#define MAX_IOV_SIZE MIN_CONST(IOV_MAX, 64)
a24939f2
DB
36
37struct QEMUFile {
a24939f2 38 const QEMUFileHooks *hooks;
2893a288 39 QIOChannel *ioc;
c0c6e1e2 40 bool is_writable;
a24939f2 41
c7fc8d32
DB
42 /*
43 * Maximum amount of data in bytes to transfer during one
44 * rate limiting time window
45 */
46 int64_t rate_limit_max;
47 /*
48 * Total amount of data in bytes queued for transfer
49 * during this rate limiting time window
50 */
51 int64_t rate_limit_used;
a24939f2 52
154d87b4
DB
53 /* The sum of bytes transferred on the wire */
54 int64_t total_transferred;
55
a24939f2
DB
56 int buf_index;
57 int buf_size; /* 0 when writing */
58 uint8_t buf[IO_BUF_SIZE];
59
53f09a10 60 DECLARE_BITMAP(may_free, MAX_IOV_SIZE);
a24939f2
DB
61 struct iovec iov[MAX_IOV_SIZE];
62 unsigned int iovcnt;
63
64 int last_error;
3d661c8a 65 Error *last_error_obj;
a555b809
JQ
66 /* has the file has been shutdown */
67 bool shutdown;
a24939f2
DB
68};
69
e1a8c9b6
DDAG
70/*
71 * Stop a file from being read/written - not all backing files can do this
72 * typically only sockets can.
d3c581b7
DB
73 *
74 * TODO: convert to propagate Error objects instead of squashing
75 * to a fixed errno value
e1a8c9b6
DDAG
76 */
77int qemu_file_shutdown(QEMUFile *f)
78{
d3c581b7 79 int ret = 0;
a555b809
JQ
80
81 f->shutdown = true;
d3c581b7
DB
82 if (!qio_channel_has_feature(f->ioc,
83 QIO_CHANNEL_FEATURE_SHUTDOWN)) {
e1a8c9b6
DDAG
84 return -ENOSYS;
85 }
d3c581b7
DB
86
87 if (qio_channel_shutdown(f->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL) < 0) {
88 ret = -EIO;
89 }
a555b809
JQ
90
91 if (!f->last_error) {
92 qemu_file_set_error(f, -EIO);
93 }
94 return ret;
e1a8c9b6
DDAG
95}
96
093c455a
EH
97bool qemu_file_mode_is_not_valid(const char *mode)
98{
99 if (mode == NULL ||
100 (mode[0] != 'r' && mode[0] != 'w') ||
101 mode[1] != 'b' || mode[2] != 0) {
102 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
103 return true;
104 }
105
106 return false;
107}
108
77ef2dc1 109static QEMUFile *qemu_file_new_impl(QIOChannel *ioc, bool is_writable)
093c455a
EH
110{
111 QEMUFile *f;
112
97f3ad35 113 f = g_new0(QEMUFile, 1);
093c455a 114
77ef2dc1 115 object_ref(ioc);
2893a288 116 f->ioc = ioc;
c0c6e1e2 117 f->is_writable = is_writable;
2893a288 118
093c455a
EH
119 return f;
120}
121
02bdbe17
DB
122/*
123 * Result: QEMUFile* for a 'return path' for comms in the opposite direction
124 * NULL if not available
125 */
126QEMUFile *qemu_file_get_return_path(QEMUFile *f)
127{
77ef2dc1 128 return qemu_file_new_impl(f->ioc, !f->is_writable);
02bdbe17
DB
129}
130
77ef2dc1 131QEMUFile *qemu_file_new_output(QIOChannel *ioc)
c0c6e1e2 132{
77ef2dc1 133 return qemu_file_new_impl(ioc, true);
c0c6e1e2
DB
134}
135
77ef2dc1 136QEMUFile *qemu_file_new_input(QIOChannel *ioc)
c0c6e1e2 137{
77ef2dc1 138 return qemu_file_new_impl(ioc, false);
c0c6e1e2
DB
139}
140
0436e09f
DB
141void qemu_file_set_hooks(QEMUFile *f, const QEMUFileHooks *hooks)
142{
143 f->hooks = hooks;
144}
145
093c455a 146/*
3d661c8a 147 * Get last error for stream f with optional Error*
093c455a
EH
148 *
149 * Return negative error value if there has been an error on previous
150 * operations, return 0 if no error happened.
3d661c8a
YK
151 * Optional, it returns Error* in errp, but it may be NULL even if return value
152 * is not 0.
093c455a
EH
153 *
154 */
3d661c8a 155int qemu_file_get_error_obj(QEMUFile *f, Error **errp)
093c455a 156{
3d661c8a
YK
157 if (errp) {
158 *errp = f->last_error_obj ? error_copy(f->last_error_obj) : NULL;
159 }
093c455a
EH
160 return f->last_error;
161}
162
3d661c8a
YK
163/*
164 * Set the last error for stream f with optional Error*
165 */
166void qemu_file_set_error_obj(QEMUFile *f, int ret, Error *err)
093c455a 167{
3d661c8a 168 if (f->last_error == 0 && ret) {
093c455a 169 f->last_error = ret;
3d661c8a
YK
170 error_propagate(&f->last_error_obj, err);
171 } else if (err) {
172 error_report_err(err);
093c455a
EH
173 }
174}
175
3d661c8a
YK
176/*
177 * Get last error for stream f
178 *
179 * Return negative error value if there has been an error on previous
180 * operations, return 0 if no error happened.
181 *
182 */
183int qemu_file_get_error(QEMUFile *f)
184{
185 return qemu_file_get_error_obj(f, NULL);
186}
187
188/*
189 * Set the last error for stream f
190 */
191void qemu_file_set_error(QEMUFile *f, int ret)
192{
193 qemu_file_set_error_obj(f, ret, NULL);
194}
195
e68dd365 196bool qemu_file_is_writable(QEMUFile *f)
093c455a 197{
c0c6e1e2 198 return f->is_writable;
093c455a
EH
199}
200
53f09a10
PB
201static void qemu_iovec_release_ram(QEMUFile *f)
202{
203 struct iovec iov;
204 unsigned long idx;
205
206 /* Find and release all the contiguous memory ranges marked as may_free. */
207 idx = find_next_bit(f->may_free, f->iovcnt, 0);
208 if (idx >= f->iovcnt) {
209 return;
210 }
211 iov = f->iov[idx];
212
213 /* The madvise() in the loop is called for iov within a continuous range and
214 * then reinitialize the iov. And in the end, madvise() is called for the
215 * last iov.
216 */
217 while ((idx = find_next_bit(f->may_free, f->iovcnt, idx + 1)) < f->iovcnt) {
218 /* check for adjacent buffer and coalesce them */
219 if (iov.iov_base + iov.iov_len == f->iov[idx].iov_base) {
220 iov.iov_len += f->iov[idx].iov_len;
221 continue;
222 }
223 if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) {
224 error_report("migrate: madvise DONTNEED failed %p %zd: %s",
225 iov.iov_base, iov.iov_len, strerror(errno));
226 }
227 iov = f->iov[idx];
228 }
229 if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) {
230 error_report("migrate: madvise DONTNEED failed %p %zd: %s",
231 iov.iov_base, iov.iov_len, strerror(errno));
232 }
233 memset(f->may_free, 0, sizeof(f->may_free));
234}
235
77ef2dc1 236
093c455a
EH
237/**
238 * Flushes QEMUFile buffer
239 *
3b348706
DDAG
240 * This will flush all pending data. If data was only partially flushed, it
241 * will set an error state.
093c455a
EH
242 */
243void qemu_fflush(QEMUFile *f)
244{
093c455a
EH
245 if (!qemu_file_is_writable(f)) {
246 return;
247 }
248
a555b809
JQ
249 if (f->shutdown) {
250 return;
251 }
11808bb0 252 if (f->iovcnt > 0) {
ec2135ee
DB
253 Error *local_error = NULL;
254 if (qio_channel_writev_all(f->ioc,
255 f->iov, f->iovcnt,
256 &local_error) < 0) {
257 qemu_file_set_error_obj(f, -EIO, local_error);
258 } else {
259 f->total_transferred += iov_size(f->iov, f->iovcnt);
260 }
53f09a10
PB
261
262 qemu_iovec_release_ram(f);
093c455a 263 }
baf51e77 264
093c455a
EH
265 f->buf_index = 0;
266 f->iovcnt = 0;
093c455a
EH
267}
268
269void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
270{
271 int ret = 0;
272
0436e09f 273 if (f->hooks && f->hooks->before_ram_iterate) {
365c0463 274 ret = f->hooks->before_ram_iterate(f, flags, NULL);
093c455a
EH
275 if (ret < 0) {
276 qemu_file_set_error(f, ret);
277 }
278 }
279}
280
281void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
282{
283 int ret = 0;
284
0436e09f 285 if (f->hooks && f->hooks->after_ram_iterate) {
365c0463 286 ret = f->hooks->after_ram_iterate(f, flags, NULL);
093c455a
EH
287 if (ret < 0) {
288 qemu_file_set_error(f, ret);
289 }
290 }
291}
292
632e3a5c 293void ram_control_load_hook(QEMUFile *f, uint64_t flags, void *data)
093c455a
EH
294{
295 int ret = -EINVAL;
296
0436e09f 297 if (f->hooks && f->hooks->hook_ram_load) {
365c0463 298 ret = f->hooks->hook_ram_load(f, flags, data);
093c455a
EH
299 if (ret < 0) {
300 qemu_file_set_error(f, ret);
301 }
302 } else {
632e3a5c
DDAG
303 /*
304 * Hook is a hook specifically requested by the source sending a flag
305 * that expects there to be a hook on the destination.
306 */
307 if (flags == RAM_CONTROL_HOOK) {
308 qemu_file_set_error(f, ret);
309 }
093c455a
EH
310 }
311}
312
313size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
6e1dea46
JQ
314 ram_addr_t offset, size_t size,
315 uint64_t *bytes_sent)
093c455a 316{
0436e09f 317 if (f->hooks && f->hooks->save_page) {
365c0463 318 int ret = f->hooks->save_page(f, block_offset,
0436e09f 319 offset, size, bytes_sent);
ccb7e1b5 320 if (ret != RAM_SAVE_CONTROL_NOT_SUPP) {
c7fc8d32 321 f->rate_limit_used += size;
ccb7e1b5
LC
322 }
323
324 if (ret != RAM_SAVE_CONTROL_DELAYED &&
325 ret != RAM_SAVE_CONTROL_NOT_SUPP) {
093c455a 326 if (bytes_sent && *bytes_sent > 0) {
1a93bd2f 327 qemu_file_credit_transfer(f, *bytes_sent);
093c455a
EH
328 } else if (ret < 0) {
329 qemu_file_set_error(f, ret);
330 }
331 }
332
333 return ret;
334 }
335
336 return RAM_SAVE_CONTROL_NOT_SUPP;
337}
338
548f52ea
DDAG
339/*
340 * Attempt to fill the buffer from the underlying file
341 * Returns the number of bytes read, or negative value for an error.
342 *
343 * Note that it can return a partially full buffer even in a not error/not EOF
344 * case if the underlying file descriptor gives a short read, and that can
345 * happen even on a blocking fd.
346 */
347static ssize_t qemu_fill_buffer(QEMUFile *f)
093c455a
EH
348{
349 int len;
350 int pending;
3d661c8a 351 Error *local_error = NULL;
093c455a
EH
352
353 assert(!qemu_file_is_writable(f));
354
355 pending = f->buf_size - f->buf_index;
356 if (pending > 0) {
357 memmove(f->buf, f->buf + f->buf_index, pending);
358 }
359 f->buf_index = 0;
360 f->buf_size = pending;
361
a555b809
JQ
362 if (f->shutdown) {
363 return 0;
364 }
365
f759d705
DB
366 do {
367 len = qio_channel_read(f->ioc,
368 (char *)f->buf + pending,
369 IO_BUF_SIZE - pending,
370 &local_error);
371 if (len == QIO_CHANNEL_ERR_BLOCK) {
372 if (qemu_in_coroutine()) {
373 qio_channel_yield(f->ioc, G_IO_IN);
374 } else {
375 qio_channel_wait(f->ioc, G_IO_IN);
376 }
377 } else if (len < 0) {
378 len = -EIO;
379 }
380 } while (len == QIO_CHANNEL_ERR_BLOCK);
381
093c455a
EH
382 if (len > 0) {
383 f->buf_size += len;
154d87b4 384 f->total_transferred += len;
093c455a 385 } else if (len == 0) {
3d661c8a 386 qemu_file_set_error_obj(f, -EIO, local_error);
093c455a 387 } else if (len != -EAGAIN) {
3d661c8a
YK
388 qemu_file_set_error_obj(f, len, local_error);
389 } else {
390 error_free(local_error);
093c455a 391 }
548f52ea
DDAG
392
393 return len;
093c455a
EH
394}
395
1a93bd2f 396void qemu_file_credit_transfer(QEMUFile *f, size_t size)
093c455a 397{
154d87b4 398 f->total_transferred += size;
093c455a
EH
399}
400
401/** Closes the file
402 *
403 * Returns negative error value if any error happened on previous operations or
404 * while closing the file. Returns 0 or positive number on success.
405 *
406 * The meaning of return value on success depends on the specific backend
407 * being used.
408 */
409int qemu_fclose(QEMUFile *f)
410{
0ae1f7f0 411 int ret, ret2;
093c455a
EH
412 qemu_fflush(f);
413 ret = qemu_file_get_error(f);
414
0ae1f7f0
DB
415 ret2 = qio_channel_close(f->ioc, NULL);
416 if (ret >= 0) {
417 ret = ret2;
093c455a 418 }
0ae1f7f0
DB
419 g_clear_pointer(&f->ioc, object_unref);
420
093c455a
EH
421 /* If any error was spotted before closing, we should report it
422 * instead of the close() return value.
423 */
424 if (f->last_error) {
425 ret = f->last_error;
426 }
3d661c8a 427 error_free(f->last_error_obj);
093c455a 428 g_free(f);
9013dca5 429 trace_qemu_file_fclose();
093c455a
EH
430 return ret;
431}
432
1bf57fb3
WY
433/*
434 * Add buf to iovec. Do flush if iovec is full.
435 *
436 * Return values:
437 * 1 iovec is full and flushed
438 * 0 iovec is not flushed
439 *
440 */
441static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
442 bool may_free)
093c455a
EH
443{
444 /* check for adjacent buffer and coalesce them */
445 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
53f09a10
PB
446 f->iov[f->iovcnt - 1].iov_len &&
447 may_free == test_bit(f->iovcnt - 1, f->may_free))
448 {
093c455a
EH
449 f->iov[f->iovcnt - 1].iov_len += size;
450 } else {
c00d434a
FL
451 if (f->iovcnt >= MAX_IOV_SIZE) {
452 /* Should only happen if a previous fflush failed */
453 assert(f->shutdown || !qemu_file_is_writable(f));
454 return 1;
455 }
53f09a10
PB
456 if (may_free) {
457 set_bit(f->iovcnt, f->may_free);
458 }
093c455a
EH
459 f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
460 f->iov[f->iovcnt++].iov_len = size;
461 }
462
463 if (f->iovcnt >= MAX_IOV_SIZE) {
464 qemu_fflush(f);
1bf57fb3
WY
465 return 1;
466 }
467
468 return 0;
469}
470
471static void add_buf_to_iovec(QEMUFile *f, size_t len)
472{
473 if (!add_to_iovec(f, f->buf + f->buf_index, len, false)) {
474 f->buf_index += len;
475 if (f->buf_index == IO_BUF_SIZE) {
476 qemu_fflush(f);
477 }
093c455a
EH
478 }
479}
480
53f09a10
PB
481void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,
482 bool may_free)
093c455a 483{
093c455a
EH
484 if (f->last_error) {
485 return;
486 }
487
c7fc8d32 488 f->rate_limit_used += size;
53f09a10 489 add_to_iovec(f, buf, size, may_free);
093c455a
EH
490}
491
56f3835f 492void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
093c455a 493{
56f3835f 494 size_t l;
093c455a
EH
495
496 if (f->last_error) {
497 return;
498 }
499
500 while (size > 0) {
501 l = IO_BUF_SIZE - f->buf_index;
502 if (l > size) {
503 l = size;
504 }
505 memcpy(f->buf + f->buf_index, buf, l);
c7fc8d32 506 f->rate_limit_used += l;
1bf57fb3 507 add_buf_to_iovec(f, l);
093c455a
EH
508 if (qemu_file_get_error(f)) {
509 break;
510 }
511 buf += l;
512 size -= l;
513 }
514}
515
516void qemu_put_byte(QEMUFile *f, int v)
517{
518 if (f->last_error) {
519 return;
520 }
521
522 f->buf[f->buf_index] = v;
c7fc8d32 523 f->rate_limit_used++;
1bf57fb3 524 add_buf_to_iovec(f, 1);
093c455a
EH
525}
526
527void qemu_file_skip(QEMUFile *f, int size)
528{
529 if (f->buf_index + size <= f->buf_size) {
530 f->buf_index += size;
531 }
532}
533
548f52ea 534/*
7c1e52ba
DDAG
535 * Read 'size' bytes from file (at 'offset') without moving the
536 * pointer and set 'buf' to point to that data.
548f52ea
DDAG
537 *
538 * It will return size bytes unless there was an error, in which case it will
539 * return as many as it managed to read (assuming blocking fd's which
540 * all current QEMUFile are)
541 */
56f3835f 542size_t qemu_peek_buffer(QEMUFile *f, uint8_t **buf, size_t size, size_t offset)
093c455a 543{
56f3835f
DDAG
544 ssize_t pending;
545 size_t index;
093c455a
EH
546
547 assert(!qemu_file_is_writable(f));
548f52ea
DDAG
548 assert(offset < IO_BUF_SIZE);
549 assert(size <= IO_BUF_SIZE - offset);
093c455a 550
548f52ea 551 /* The 1st byte to read from */
093c455a 552 index = f->buf_index + offset;
548f52ea 553 /* The number of available bytes starting at index */
093c455a 554 pending = f->buf_size - index;
548f52ea
DDAG
555
556 /*
557 * qemu_fill_buffer might return just a few bytes, even when there isn't
558 * an error, so loop collecting them until we get enough.
559 */
560 while (pending < size) {
561 int received = qemu_fill_buffer(f);
562
563 if (received <= 0) {
564 break;
565 }
566
093c455a
EH
567 index = f->buf_index + offset;
568 pending = f->buf_size - index;
569 }
570
571 if (pending <= 0) {
572 return 0;
573 }
574 if (size > pending) {
575 size = pending;
576 }
577
7c1e52ba 578 *buf = f->buf + index;
093c455a
EH
579 return size;
580}
581
548f52ea
DDAG
582/*
583 * Read 'size' bytes of data from the file into buf.
584 * 'size' can be larger than the internal buffer.
585 *
586 * It will return size bytes unless there was an error, in which case it will
587 * return as many as it managed to read (assuming blocking fd's which
588 * all current QEMUFile are)
589 */
56f3835f 590size_t qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size)
093c455a 591{
56f3835f
DDAG
592 size_t pending = size;
593 size_t done = 0;
093c455a
EH
594
595 while (pending > 0) {
56f3835f 596 size_t res;
7c1e52ba 597 uint8_t *src;
093c455a 598
7c1e52ba 599 res = qemu_peek_buffer(f, &src, MIN(pending, IO_BUF_SIZE), 0);
093c455a
EH
600 if (res == 0) {
601 return done;
602 }
7c1e52ba 603 memcpy(buf, src, res);
093c455a
EH
604 qemu_file_skip(f, res);
605 buf += res;
606 pending -= res;
607 done += res;
608 }
609 return done;
610}
611
9504fb51
DDAG
612/*
613 * Read 'size' bytes of data from the file.
614 * 'size' can be larger than the internal buffer.
615 *
616 * The data:
617 * may be held on an internal buffer (in which case *buf is updated
618 * to point to it) that is valid until the next qemu_file operation.
619 * OR
620 * will be copied to the *buf that was passed in.
621 *
622 * The code tries to avoid the copy if possible.
623 *
624 * It will return size bytes unless there was an error, in which case it will
625 * return as many as it managed to read (assuming blocking fd's which
626 * all current QEMUFile are)
627 *
628 * Note: Since **buf may get changed, the caller should take care to
629 * keep a pointer to the original buffer if it needs to deallocate it.
630 */
631size_t qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size)
632{
633 if (size < IO_BUF_SIZE) {
634 size_t res;
1dfafcbd 635 uint8_t *src = NULL;
9504fb51
DDAG
636
637 res = qemu_peek_buffer(f, &src, size, 0);
638
639 if (res == size) {
640 qemu_file_skip(f, res);
641 *buf = src;
642 return res;
643 }
644 }
645
646 return qemu_get_buffer(f, *buf, size);
647}
648
548f52ea
DDAG
649/*
650 * Peeks a single byte from the buffer; this isn't guaranteed to work if
651 * offset leaves a gap after the previous read/peeked data.
652 */
093c455a
EH
653int qemu_peek_byte(QEMUFile *f, int offset)
654{
655 int index = f->buf_index + offset;
656
657 assert(!qemu_file_is_writable(f));
548f52ea 658 assert(offset < IO_BUF_SIZE);
093c455a
EH
659
660 if (index >= f->buf_size) {
661 qemu_fill_buffer(f);
662 index = f->buf_index + offset;
663 if (index >= f->buf_size) {
664 return 0;
665 }
666 }
667 return f->buf[index];
668}
669
670int qemu_get_byte(QEMUFile *f)
671{
672 int result;
673
674 result = qemu_peek_byte(f, 0);
675 qemu_file_skip(f, 1);
676 return result;
677}
678
fbfa6404 679int64_t qemu_file_total_transferred_fast(QEMUFile *f)
97221400 680{
154d87b4 681 int64_t ret = f->total_transferred;
97221400
AG
682 int i;
683
11808bb0
DB
684 for (i = 0; i < f->iovcnt; i++) {
685 ret += f->iov[i].iov_len;
97221400
AG
686 }
687
688 return ret;
689}
690
fbfa6404 691int64_t qemu_file_total_transferred(QEMUFile *f)
093c455a
EH
692{
693 qemu_fflush(f);
154d87b4 694 return f->total_transferred;
093c455a
EH
695}
696
697int qemu_file_rate_limit(QEMUFile *f)
698{
a555b809
JQ
699 if (f->shutdown) {
700 return 1;
701 }
093c455a
EH
702 if (qemu_file_get_error(f)) {
703 return 1;
704 }
c7fc8d32 705 if (f->rate_limit_max > 0 && f->rate_limit_used > f->rate_limit_max) {
093c455a
EH
706 return 1;
707 }
708 return 0;
709}
710
711int64_t qemu_file_get_rate_limit(QEMUFile *f)
712{
c7fc8d32 713 return f->rate_limit_max;
093c455a
EH
714}
715
716void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
717{
c7fc8d32 718 f->rate_limit_max = limit;
093c455a
EH
719}
720
721void qemu_file_reset_rate_limit(QEMUFile *f)
722{
c7fc8d32 723 f->rate_limit_used = 0;
093c455a
EH
724}
725
bc698c36 726void qemu_file_acct_rate_limit(QEMUFile *f, int64_t len)
5d7d2558 727{
c7fc8d32 728 f->rate_limit_used += len;
5d7d2558
IR
729}
730
093c455a
EH
731void qemu_put_be16(QEMUFile *f, unsigned int v)
732{
733 qemu_put_byte(f, v >> 8);
734 qemu_put_byte(f, v);
735}
736
737void qemu_put_be32(QEMUFile *f, unsigned int v)
738{
739 qemu_put_byte(f, v >> 24);
740 qemu_put_byte(f, v >> 16);
741 qemu_put_byte(f, v >> 8);
742 qemu_put_byte(f, v);
743}
744
745void qemu_put_be64(QEMUFile *f, uint64_t v)
746{
747 qemu_put_be32(f, v >> 32);
748 qemu_put_be32(f, v);
749}
750
751unsigned int qemu_get_be16(QEMUFile *f)
752{
753 unsigned int v;
754 v = qemu_get_byte(f) << 8;
755 v |= qemu_get_byte(f);
756 return v;
757}
758
759unsigned int qemu_get_be32(QEMUFile *f)
760{
761 unsigned int v;
90d6a673 762 v = (unsigned int)qemu_get_byte(f) << 24;
093c455a
EH
763 v |= qemu_get_byte(f) << 16;
764 v |= qemu_get_byte(f) << 8;
765 v |= qemu_get_byte(f);
766 return v;
767}
768
769uint64_t qemu_get_be64(QEMUFile *f)
770{
771 uint64_t v;
772 v = (uint64_t)qemu_get_be32(f) << 32;
773 v |= qemu_get_be32(f);
774 return v;
775}
44f0eadc 776
dcaf446e
XG
777/* return the size after compression, or negative value on error */
778static int qemu_compress_data(z_stream *stream, uint8_t *dest, size_t dest_len,
779 const uint8_t *source, size_t source_len)
780{
781 int err;
782
783 err = deflateReset(stream);
784 if (err != Z_OK) {
785 return -1;
786 }
787
788 stream->avail_in = source_len;
789 stream->next_in = (uint8_t *)source;
790 stream->avail_out = dest_len;
791 stream->next_out = dest;
792
793 err = deflate(stream, Z_FINISH);
794 if (err != Z_STREAM_END) {
795 return -1;
796 }
797
798 return stream->next_out - dest;
799}
800
801/* Compress size bytes of data start at p and store the compressed
802 * data to the buffer of f.
b3be2896 803 *
42d24611
WY
804 * Since the file is dummy file with empty_ops, return -1 if f has no space to
805 * save the compressed data.
44f0eadc 806 */
dcaf446e
XG
807ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream,
808 const uint8_t *p, size_t size)
44f0eadc
LL
809{
810 ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t);
811
812 if (blen < compressBound(size)) {
42d24611 813 return -1;
44f0eadc 814 }
dcaf446e
XG
815
816 blen = qemu_compress_data(stream, f->buf + f->buf_index + sizeof(int32_t),
817 blen, p, size);
818 if (blen < 0) {
34ab9e97 819 return -1;
44f0eadc 820 }
34ab9e97 821
44f0eadc 822 qemu_put_be32(f, blen);
1bf57fb3 823 add_buf_to_iovec(f, blen);
44f0eadc
LL
824 return blen + sizeof(int32_t);
825}
826
827/* Put the data in the buffer of f_src to the buffer of f_des, and
828 * then reset the buf_index of f_src to 0.
829 */
830
831int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src)
832{
833 int len = 0;
834
835 if (f_src->buf_index > 0) {
836 len = f_src->buf_index;
837 qemu_put_buffer(f_des, f_src->buf, f_src->buf_index);
838 f_src->buf_index = 0;
787d134f 839 f_src->iovcnt = 0;
44f0eadc
LL
840 }
841 return len;
842}
b3af1bc9
DDAG
843
844/*
845 * Get a string whose length is determined by a single preceding byte
846 * A preallocated 256 byte buffer must be passed in.
847 * Returns: len on success and a 0 terminated string in the buffer
848 * else 0
849 * (Note a 0 length string will return 0 either way)
850 */
851size_t qemu_get_counted_string(QEMUFile *f, char buf[256])
852{
853 size_t len = qemu_get_byte(f);
854 size_t res = qemu_get_buffer(f, (uint8_t *)buf, len);
855
856 buf[res] = 0;
857
858 return res == len ? res : 0;
859}
a800cd5c 860
f0d64cb7
VSO
861/*
862 * Put a string with one preceding byte containing its length. The length of
863 * the string should be less than 256.
864 */
865void qemu_put_counted_string(QEMUFile *f, const char *str)
866{
867 size_t len = strlen(str);
868
869 assert(len < 256);
870 qemu_put_byte(f, len);
871 qemu_put_buffer(f, (const uint8_t *)str, len);
872}
873
a800cd5c
DDAG
874/*
875 * Set the blocking state of the QEMUFile.
876 * Note: On some transports the OS only keeps a single blocking state for
877 * both directions, and thus changing the blocking on the main
878 * QEMUFile can also affect the return path.
879 */
880void qemu_file_set_blocking(QEMUFile *f, bool block)
881{
80ad9706 882 qio_channel_set_blocking(f->ioc, block, NULL);
a800cd5c 883}
c6ad5be7
PX
884
885/*
2893a288
DB
886 * qemu_file_get_ioc:
887 *
888 * Get the ioc object for the file, without incrementing
889 * the reference count.
890 *
891 * Returns: the ioc object
c6ad5be7
PX
892 */
893QIOChannel *qemu_file_get_ioc(QEMUFile *file)
894{
2893a288 895 return file->ioc;
c6ad5be7 896}