160fe637bSDr. David Alan Gilbert /* 260fe637bSDr. David Alan Gilbert * QEMU System Emulator 360fe637bSDr. David Alan Gilbert * 460fe637bSDr. David Alan Gilbert * Copyright (c) 2003-2008 Fabrice Bellard 560fe637bSDr. David Alan Gilbert * 660fe637bSDr. David Alan Gilbert * Permission is hereby granted, free of charge, to any person obtaining a copy 760fe637bSDr. David Alan Gilbert * of this software and associated documentation files (the "Software"), to deal 860fe637bSDr. David Alan Gilbert * in the Software without restriction, including without limitation the rights 960fe637bSDr. David Alan Gilbert * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 1060fe637bSDr. David Alan Gilbert * copies of the Software, and to permit persons to whom the Software is 1160fe637bSDr. David Alan Gilbert * furnished to do so, subject to the following conditions: 1260fe637bSDr. David Alan Gilbert * 1360fe637bSDr. David Alan Gilbert * The above copyright notice and this permission notice shall be included in 1460fe637bSDr. David Alan Gilbert * all copies or substantial portions of the Software. 1560fe637bSDr. David Alan Gilbert * 1660fe637bSDr. David Alan Gilbert * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1760fe637bSDr. David Alan Gilbert * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1860fe637bSDr. David Alan Gilbert * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1960fe637bSDr. David Alan Gilbert * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 2060fe637bSDr. David Alan Gilbert * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 2160fe637bSDr. David Alan Gilbert * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 2260fe637bSDr. David Alan Gilbert * THE SOFTWARE. 2360fe637bSDr. David Alan Gilbert */ 241393a485SPeter Maydell #include "qemu/osdep.h" 2544f0eadcSLiang Li #include <zlib.h> 26b85ea5faSPeter Maydell #include "qemu/madvise.h" 27d49b6836SMarkus Armbruster #include "qemu/error-report.h" 2860fe637bSDr. David Alan Gilbert #include "qemu/iov.h" 296666c96aSJuan Quintela #include "migration.h" 308e4b2a70SJuan Quintela #include "migration-stats.h" 3108a0aee1SJuan Quintela #include "qemu-file.h" 3260fe637bSDr. David Alan Gilbert #include "trace.h" 339d3ebbe2SJuan Quintela #include "options.h" 343d661c8aSYury Kotov #include "qapi/error.h" 3560fe637bSDr. David Alan Gilbert 36a24939f2SDaniel P. Berrange #define IO_BUF_SIZE 32768 37f9919116SEric Blake #define MAX_IOV_SIZE MIN_CONST(IOV_MAX, 64) 38a24939f2SDaniel P. Berrange 39a24939f2SDaniel P. Berrange struct QEMUFile { 40a24939f2SDaniel P. Berrange const QEMUFileHooks *hooks; 412893a288SDaniel P. Berrangé QIOChannel *ioc; 42c0c6e1e2SDaniel P. Berrangé bool is_writable; 43a24939f2SDaniel P. Berrange 44c7fc8d32SDaniel P. Berrangé /* 45c7fc8d32SDaniel P. Berrangé * Maximum amount of data in bytes to transfer during one 46c7fc8d32SDaniel P. Berrangé * rate limiting time window 47c7fc8d32SDaniel P. Berrangé */ 48bffc0441SJuan Quintela uint64_t rate_limit_max; 49c7fc8d32SDaniel P. Berrangé /* 50c7fc8d32SDaniel P. Berrangé * Total amount of data in bytes queued for transfer 51c7fc8d32SDaniel P. Berrangé * during this rate limiting time window 52c7fc8d32SDaniel P. Berrangé */ 53f87e4d6dSJuan Quintela uint64_t rate_limit_used; 54a24939f2SDaniel P. Berrange 55154d87b4SDaniel P. Berrangé /* The sum of bytes transferred on the wire */ 5661abf1ebSJuan Quintela uint64_t total_transferred; 57154d87b4SDaniel P. Berrangé 58a24939f2SDaniel P. Berrange int buf_index; 59a24939f2SDaniel P. Berrange int buf_size; /* 0 when writing */ 60a24939f2SDaniel P. Berrange uint8_t buf[IO_BUF_SIZE]; 61a24939f2SDaniel P. Berrange 6253f09a10SPavel Butsykin DECLARE_BITMAP(may_free, MAX_IOV_SIZE); 63a24939f2SDaniel P. Berrange struct iovec iov[MAX_IOV_SIZE]; 64a24939f2SDaniel P. Berrange unsigned int iovcnt; 65a24939f2SDaniel P. Berrange 66a24939f2SDaniel P. Berrange int last_error; 673d661c8aSYury Kotov Error *last_error_obj; 68a24939f2SDaniel P. Berrange }; 69a24939f2SDaniel P. Berrange 70e1a8c9b6SDr. David Alan Gilbert /* 71e1a8c9b6SDr. David Alan Gilbert * Stop a file from being read/written - not all backing files can do this 72e1a8c9b6SDr. David Alan Gilbert * typically only sockets can. 73d3c581b7SDaniel P. Berrangé * 74d3c581b7SDaniel P. Berrangé * TODO: convert to propagate Error objects instead of squashing 75d3c581b7SDaniel P. Berrangé * to a fixed errno value 76e1a8c9b6SDr. David Alan Gilbert */ 77e1a8c9b6SDr. David Alan Gilbert int qemu_file_shutdown(QEMUFile *f) 78e1a8c9b6SDr. David Alan Gilbert { 79d3c581b7SDaniel P. Berrangé int ret = 0; 80a555b809SJuan Quintela 81f5816b5cSPeter Xu /* 82f5816b5cSPeter Xu * We must set qemufile error before the real shutdown(), otherwise 83f5816b5cSPeter Xu * there can be a race window where we thought IO all went though 84f5816b5cSPeter Xu * (because last_error==NULL) but actually IO has already stopped. 85f5816b5cSPeter Xu * 86f5816b5cSPeter Xu * If without correct ordering, the race can happen like this: 87f5816b5cSPeter Xu * 88f5816b5cSPeter Xu * page receiver other thread 89f5816b5cSPeter Xu * ------------- ------------ 90f5816b5cSPeter Xu * qemu_get_buffer() 91f5816b5cSPeter Xu * do shutdown() 92f5816b5cSPeter Xu * returns 0 (buffer all zero) 93f5816b5cSPeter Xu * (we didn't check this retcode) 94f5816b5cSPeter Xu * try to detect IO error 95f5816b5cSPeter Xu * last_error==NULL, IO okay 96f5816b5cSPeter Xu * install ALL-ZERO page 97f5816b5cSPeter Xu * set last_error 98f5816b5cSPeter Xu * --> guest crash! 99f5816b5cSPeter Xu */ 100f5816b5cSPeter Xu if (!f->last_error) { 101f5816b5cSPeter Xu qemu_file_set_error(f, -EIO); 102f5816b5cSPeter Xu } 103f5816b5cSPeter Xu 104d3c581b7SDaniel P. Berrangé if (!qio_channel_has_feature(f->ioc, 105d3c581b7SDaniel P. Berrangé QIO_CHANNEL_FEATURE_SHUTDOWN)) { 106e1a8c9b6SDr. David Alan Gilbert return -ENOSYS; 107e1a8c9b6SDr. David Alan Gilbert } 108d3c581b7SDaniel P. Berrangé 109d3c581b7SDaniel P. Berrangé if (qio_channel_shutdown(f->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL) < 0) { 110d3c581b7SDaniel P. Berrangé ret = -EIO; 111d3c581b7SDaniel P. Berrangé } 112a555b809SJuan Quintela 113a555b809SJuan Quintela return ret; 114e1a8c9b6SDr. David Alan Gilbert } 115e1a8c9b6SDr. David Alan Gilbert 11660fe637bSDr. David Alan Gilbert bool qemu_file_mode_is_not_valid(const char *mode) 11760fe637bSDr. David Alan Gilbert { 11860fe637bSDr. David Alan Gilbert if (mode == NULL || 11960fe637bSDr. David Alan Gilbert (mode[0] != 'r' && mode[0] != 'w') || 12060fe637bSDr. David Alan Gilbert mode[1] != 'b' || mode[2] != 0) { 12160fe637bSDr. David Alan Gilbert fprintf(stderr, "qemu_fopen: Argument validity check failed\n"); 12260fe637bSDr. David Alan Gilbert return true; 12360fe637bSDr. David Alan Gilbert } 12460fe637bSDr. David Alan Gilbert 12560fe637bSDr. David Alan Gilbert return false; 12660fe637bSDr. David Alan Gilbert } 12760fe637bSDr. David Alan Gilbert 12877ef2dc1SDaniel P. Berrangé static QEMUFile *qemu_file_new_impl(QIOChannel *ioc, bool is_writable) 12960fe637bSDr. David Alan Gilbert { 13060fe637bSDr. David Alan Gilbert QEMUFile *f; 13160fe637bSDr. David Alan Gilbert 13297f3ad35SMarkus Armbruster f = g_new0(QEMUFile, 1); 13360fe637bSDr. David Alan Gilbert 13477ef2dc1SDaniel P. Berrangé object_ref(ioc); 1352893a288SDaniel P. Berrangé f->ioc = ioc; 136c0c6e1e2SDaniel P. Berrangé f->is_writable = is_writable; 1372893a288SDaniel P. Berrangé 13860fe637bSDr. David Alan Gilbert return f; 13960fe637bSDr. David Alan Gilbert } 14060fe637bSDr. David Alan Gilbert 14102bdbe17SDaniel P. Berrangé /* 14202bdbe17SDaniel P. Berrangé * Result: QEMUFile* for a 'return path' for comms in the opposite direction 14302bdbe17SDaniel P. Berrangé * NULL if not available 14402bdbe17SDaniel P. Berrangé */ 14502bdbe17SDaniel P. Berrangé QEMUFile *qemu_file_get_return_path(QEMUFile *f) 14602bdbe17SDaniel P. Berrangé { 14777ef2dc1SDaniel P. Berrangé return qemu_file_new_impl(f->ioc, !f->is_writable); 14802bdbe17SDaniel P. Berrangé } 14902bdbe17SDaniel P. Berrangé 15077ef2dc1SDaniel P. Berrangé QEMUFile *qemu_file_new_output(QIOChannel *ioc) 151c0c6e1e2SDaniel P. Berrangé { 15277ef2dc1SDaniel P. Berrangé return qemu_file_new_impl(ioc, true); 153c0c6e1e2SDaniel P. Berrangé } 154c0c6e1e2SDaniel P. Berrangé 15577ef2dc1SDaniel P. Berrangé QEMUFile *qemu_file_new_input(QIOChannel *ioc) 156c0c6e1e2SDaniel P. Berrangé { 15777ef2dc1SDaniel P. Berrangé return qemu_file_new_impl(ioc, false); 158c0c6e1e2SDaniel P. Berrangé } 159c0c6e1e2SDaniel P. Berrangé 1600436e09fSDaniel P. Berrange void qemu_file_set_hooks(QEMUFile *f, const QEMUFileHooks *hooks) 1610436e09fSDaniel P. Berrange { 1620436e09fSDaniel P. Berrange f->hooks = hooks; 1630436e09fSDaniel P. Berrange } 1640436e09fSDaniel P. Berrange 16560fe637bSDr. David Alan Gilbert /* 1663d661c8aSYury Kotov * Get last error for stream f with optional Error* 1673d661c8aSYury Kotov * 1683d661c8aSYury Kotov * Return negative error value if there has been an error on previous 1693d661c8aSYury Kotov * operations, return 0 if no error happened. 1703d661c8aSYury Kotov * Optional, it returns Error* in errp, but it may be NULL even if return value 1713d661c8aSYury Kotov * is not 0. 1723d661c8aSYury Kotov * 1733d661c8aSYury Kotov */ 1743d661c8aSYury Kotov int qemu_file_get_error_obj(QEMUFile *f, Error **errp) 1753d661c8aSYury Kotov { 1763d661c8aSYury Kotov if (errp) { 1773d661c8aSYury Kotov *errp = f->last_error_obj ? error_copy(f->last_error_obj) : NULL; 1783d661c8aSYury Kotov } 1793d661c8aSYury Kotov return f->last_error; 1803d661c8aSYury Kotov } 1813d661c8aSYury Kotov 1823d661c8aSYury Kotov /* 18360bb3c58SPeter Xu * Get last error for either stream f1 or f2 with optional Error*. 18460bb3c58SPeter Xu * The error returned (non-zero) can be either from f1 or f2. 18560bb3c58SPeter Xu * 18660bb3c58SPeter Xu * If any of the qemufile* is NULL, then skip the check on that file. 18760bb3c58SPeter Xu * 18860bb3c58SPeter Xu * When there is no error on both qemufile, zero is returned. 18960bb3c58SPeter Xu */ 19060bb3c58SPeter Xu int qemu_file_get_error_obj_any(QEMUFile *f1, QEMUFile *f2, Error **errp) 19160bb3c58SPeter Xu { 19260bb3c58SPeter Xu int ret = 0; 19360bb3c58SPeter Xu 19460bb3c58SPeter Xu if (f1) { 19560bb3c58SPeter Xu ret = qemu_file_get_error_obj(f1, errp); 19660bb3c58SPeter Xu /* If there's already error detected, return */ 19760bb3c58SPeter Xu if (ret) { 19860bb3c58SPeter Xu return ret; 19960bb3c58SPeter Xu } 20060bb3c58SPeter Xu } 20160bb3c58SPeter Xu 20260bb3c58SPeter Xu if (f2) { 20360bb3c58SPeter Xu ret = qemu_file_get_error_obj(f2, errp); 20460bb3c58SPeter Xu } 20560bb3c58SPeter Xu 20660bb3c58SPeter Xu return ret; 20760bb3c58SPeter Xu } 20860bb3c58SPeter Xu 20960bb3c58SPeter Xu /* 2103d661c8aSYury Kotov * Set the last error for stream f with optional Error* 2113d661c8aSYury Kotov */ 2123d661c8aSYury Kotov void qemu_file_set_error_obj(QEMUFile *f, int ret, Error *err) 2133d661c8aSYury Kotov { 2143d661c8aSYury Kotov if (f->last_error == 0 && ret) { 2153d661c8aSYury Kotov f->last_error = ret; 2163d661c8aSYury Kotov error_propagate(&f->last_error_obj, err); 2173d661c8aSYury Kotov } else if (err) { 2183d661c8aSYury Kotov error_report_err(err); 2193d661c8aSYury Kotov } 2203d661c8aSYury Kotov } 2213d661c8aSYury Kotov 2223d661c8aSYury Kotov /* 22360fe637bSDr. David Alan Gilbert * Get last error for stream f 22460fe637bSDr. David Alan Gilbert * 22560fe637bSDr. David Alan Gilbert * Return negative error value if there has been an error on previous 22660fe637bSDr. David Alan Gilbert * operations, return 0 if no error happened. 22760fe637bSDr. David Alan Gilbert * 22860fe637bSDr. David Alan Gilbert */ 22960fe637bSDr. David Alan Gilbert int qemu_file_get_error(QEMUFile *f) 23060fe637bSDr. David Alan Gilbert { 2313d661c8aSYury Kotov return qemu_file_get_error_obj(f, NULL); 23260fe637bSDr. David Alan Gilbert } 23360fe637bSDr. David Alan Gilbert 2343d661c8aSYury Kotov /* 2353d661c8aSYury Kotov * Set the last error for stream f 2363d661c8aSYury Kotov */ 23760fe637bSDr. David Alan Gilbert void qemu_file_set_error(QEMUFile *f, int ret) 23860fe637bSDr. David Alan Gilbert { 2393d661c8aSYury Kotov qemu_file_set_error_obj(f, ret, NULL); 24060fe637bSDr. David Alan Gilbert } 24160fe637bSDr. David Alan Gilbert 24260fe637bSDr. David Alan Gilbert bool qemu_file_is_writable(QEMUFile *f) 24360fe637bSDr. David Alan Gilbert { 244c0c6e1e2SDaniel P. Berrangé return f->is_writable; 24560fe637bSDr. David Alan Gilbert } 24660fe637bSDr. David Alan Gilbert 24753f09a10SPavel Butsykin static void qemu_iovec_release_ram(QEMUFile *f) 24853f09a10SPavel Butsykin { 24953f09a10SPavel Butsykin struct iovec iov; 25053f09a10SPavel Butsykin unsigned long idx; 25153f09a10SPavel Butsykin 25253f09a10SPavel Butsykin /* Find and release all the contiguous memory ranges marked as may_free. */ 25353f09a10SPavel Butsykin idx = find_next_bit(f->may_free, f->iovcnt, 0); 25453f09a10SPavel Butsykin if (idx >= f->iovcnt) { 25553f09a10SPavel Butsykin return; 25653f09a10SPavel Butsykin } 25753f09a10SPavel Butsykin iov = f->iov[idx]; 25853f09a10SPavel Butsykin 25953f09a10SPavel Butsykin /* The madvise() in the loop is called for iov within a continuous range and 26053f09a10SPavel Butsykin * then reinitialize the iov. And in the end, madvise() is called for the 26153f09a10SPavel Butsykin * last iov. 26253f09a10SPavel Butsykin */ 26353f09a10SPavel Butsykin while ((idx = find_next_bit(f->may_free, f->iovcnt, idx + 1)) < f->iovcnt) { 26453f09a10SPavel Butsykin /* check for adjacent buffer and coalesce them */ 26553f09a10SPavel Butsykin if (iov.iov_base + iov.iov_len == f->iov[idx].iov_base) { 26653f09a10SPavel Butsykin iov.iov_len += f->iov[idx].iov_len; 26753f09a10SPavel Butsykin continue; 26853f09a10SPavel Butsykin } 26953f09a10SPavel Butsykin if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) { 27053f09a10SPavel Butsykin error_report("migrate: madvise DONTNEED failed %p %zd: %s", 27153f09a10SPavel Butsykin iov.iov_base, iov.iov_len, strerror(errno)); 27253f09a10SPavel Butsykin } 27353f09a10SPavel Butsykin iov = f->iov[idx]; 27453f09a10SPavel Butsykin } 27553f09a10SPavel Butsykin if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) { 27653f09a10SPavel Butsykin error_report("migrate: madvise DONTNEED failed %p %zd: %s", 27753f09a10SPavel Butsykin iov.iov_base, iov.iov_len, strerror(errno)); 27853f09a10SPavel Butsykin } 27953f09a10SPavel Butsykin memset(f->may_free, 0, sizeof(f->may_free)); 28053f09a10SPavel Butsykin } 28153f09a10SPavel Butsykin 28277ef2dc1SDaniel P. Berrangé 28360fe637bSDr. David Alan Gilbert /** 28460fe637bSDr. David Alan Gilbert * Flushes QEMUFile buffer 28560fe637bSDr. David Alan Gilbert * 2863b348706SDr. David Alan Gilbert * This will flush all pending data. If data was only partially flushed, it 2873b348706SDr. David Alan Gilbert * will set an error state. 28860fe637bSDr. David Alan Gilbert */ 28960fe637bSDr. David Alan Gilbert void qemu_fflush(QEMUFile *f) 29060fe637bSDr. David Alan Gilbert { 29160fe637bSDr. David Alan Gilbert if (!qemu_file_is_writable(f)) { 29260fe637bSDr. David Alan Gilbert return; 29360fe637bSDr. David Alan Gilbert } 29460fe637bSDr. David Alan Gilbert 295ac7d25b8SJuan Quintela if (qemu_file_get_error(f)) { 296a555b809SJuan Quintela return; 297a555b809SJuan Quintela } 29860fe637bSDr. David Alan Gilbert if (f->iovcnt > 0) { 299ec2135eeSDaniel P. Berrangé Error *local_error = NULL; 300ec2135eeSDaniel P. Berrangé if (qio_channel_writev_all(f->ioc, 301ec2135eeSDaniel P. Berrangé f->iov, f->iovcnt, 302ec2135eeSDaniel P. Berrangé &local_error) < 0) { 303ec2135eeSDaniel P. Berrangé qemu_file_set_error_obj(f, -EIO, local_error); 304ec2135eeSDaniel P. Berrangé } else { 305de37f8b9SJuan Quintela uint64_t size = iov_size(f->iov, f->iovcnt); 306de37f8b9SJuan Quintela qemu_file_acct_rate_limit(f, size); 307de37f8b9SJuan Quintela f->total_transferred += size; 308ec2135eeSDaniel P. Berrangé } 30953f09a10SPavel Butsykin 31053f09a10SPavel Butsykin qemu_iovec_release_ram(f); 31160fe637bSDr. David Alan Gilbert } 312baf51e77SDaniel P. Berrange 31360fe637bSDr. David Alan Gilbert f->buf_index = 0; 31460fe637bSDr. David Alan Gilbert f->iovcnt = 0; 31560fe637bSDr. David Alan Gilbert } 31660fe637bSDr. David Alan Gilbert 31760fe637bSDr. David Alan Gilbert void ram_control_before_iterate(QEMUFile *f, uint64_t flags) 31860fe637bSDr. David Alan Gilbert { 31960fe637bSDr. David Alan Gilbert int ret = 0; 32060fe637bSDr. David Alan Gilbert 3210436e09fSDaniel P. Berrange if (f->hooks && f->hooks->before_ram_iterate) { 322365c0463SDaniel P. Berrangé ret = f->hooks->before_ram_iterate(f, flags, NULL); 32360fe637bSDr. David Alan Gilbert if (ret < 0) { 32460fe637bSDr. David Alan Gilbert qemu_file_set_error(f, ret); 32560fe637bSDr. David Alan Gilbert } 32660fe637bSDr. David Alan Gilbert } 32760fe637bSDr. David Alan Gilbert } 32860fe637bSDr. David Alan Gilbert 32960fe637bSDr. David Alan Gilbert void ram_control_after_iterate(QEMUFile *f, uint64_t flags) 33060fe637bSDr. David Alan Gilbert { 33160fe637bSDr. David Alan Gilbert int ret = 0; 33260fe637bSDr. David Alan Gilbert 3330436e09fSDaniel P. Berrange if (f->hooks && f->hooks->after_ram_iterate) { 334365c0463SDaniel P. Berrangé ret = f->hooks->after_ram_iterate(f, flags, NULL); 33560fe637bSDr. David Alan Gilbert if (ret < 0) { 33660fe637bSDr. David Alan Gilbert qemu_file_set_error(f, ret); 33760fe637bSDr. David Alan Gilbert } 33860fe637bSDr. David Alan Gilbert } 33960fe637bSDr. David Alan Gilbert } 34060fe637bSDr. David Alan Gilbert 341632e3a5cSDr. David Alan Gilbert void ram_control_load_hook(QEMUFile *f, uint64_t flags, void *data) 34260fe637bSDr. David Alan Gilbert { 3430436e09fSDaniel P. Berrange if (f->hooks && f->hooks->hook_ram_load) { 344cf7fe0c5SJuan Quintela int ret = f->hooks->hook_ram_load(f, flags, data); 34560fe637bSDr. David Alan Gilbert if (ret < 0) { 34660fe637bSDr. David Alan Gilbert qemu_file_set_error(f, ret); 34760fe637bSDr. David Alan Gilbert } 34860fe637bSDr. David Alan Gilbert } 349632e3a5cSDr. David Alan Gilbert } 35060fe637bSDr. David Alan Gilbert 35160fe637bSDr. David Alan Gilbert size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset, 3526e1dea46SJuan Quintela ram_addr_t offset, size_t size, 3536e1dea46SJuan Quintela uint64_t *bytes_sent) 35460fe637bSDr. David Alan Gilbert { 3550436e09fSDaniel P. Berrange if (f->hooks && f->hooks->save_page) { 356365c0463SDaniel P. Berrangé int ret = f->hooks->save_page(f, block_offset, 35760fe637bSDr. David Alan Gilbert offset, size, bytes_sent); 358ccb7e1b5SLidong Chen if (ret != RAM_SAVE_CONTROL_NOT_SUPP) { 359fae4009fSJuan Quintela qemu_file_acct_rate_limit(f, size); 360ccb7e1b5SLidong Chen } 361ccb7e1b5SLidong Chen 362ccb7e1b5SLidong Chen if (ret != RAM_SAVE_CONTROL_DELAYED && 363ccb7e1b5SLidong Chen ret != RAM_SAVE_CONTROL_NOT_SUPP) { 36460fe637bSDr. David Alan Gilbert if (bytes_sent && *bytes_sent > 0) { 3651a93bd2fSDaniel P. Berrangé qemu_file_credit_transfer(f, *bytes_sent); 36660fe637bSDr. David Alan Gilbert } else if (ret < 0) { 36760fe637bSDr. David Alan Gilbert qemu_file_set_error(f, ret); 36860fe637bSDr. David Alan Gilbert } 36960fe637bSDr. David Alan Gilbert } 37060fe637bSDr. David Alan Gilbert 37160fe637bSDr. David Alan Gilbert return ret; 37260fe637bSDr. David Alan Gilbert } 37360fe637bSDr. David Alan Gilbert 37460fe637bSDr. David Alan Gilbert return RAM_SAVE_CONTROL_NOT_SUPP; 37560fe637bSDr. David Alan Gilbert } 37660fe637bSDr. David Alan Gilbert 37760fe637bSDr. David Alan Gilbert /* 37860fe637bSDr. David Alan Gilbert * Attempt to fill the buffer from the underlying file 37960fe637bSDr. David Alan Gilbert * Returns the number of bytes read, or negative value for an error. 38060fe637bSDr. David Alan Gilbert * 38160fe637bSDr. David Alan Gilbert * Note that it can return a partially full buffer even in a not error/not EOF 38260fe637bSDr. David Alan Gilbert * case if the underlying file descriptor gives a short read, and that can 38360fe637bSDr. David Alan Gilbert * happen even on a blocking fd. 38460fe637bSDr. David Alan Gilbert */ 385394b9407SPaolo Bonzini static ssize_t coroutine_mixed_fn qemu_fill_buffer(QEMUFile *f) 38660fe637bSDr. David Alan Gilbert { 38760fe637bSDr. David Alan Gilbert int len; 38860fe637bSDr. David Alan Gilbert int pending; 3893d661c8aSYury Kotov Error *local_error = NULL; 39060fe637bSDr. David Alan Gilbert 39160fe637bSDr. David Alan Gilbert assert(!qemu_file_is_writable(f)); 39260fe637bSDr. David Alan Gilbert 39360fe637bSDr. David Alan Gilbert pending = f->buf_size - f->buf_index; 39460fe637bSDr. David Alan Gilbert if (pending > 0) { 39560fe637bSDr. David Alan Gilbert memmove(f->buf, f->buf + f->buf_index, pending); 39660fe637bSDr. David Alan Gilbert } 39760fe637bSDr. David Alan Gilbert f->buf_index = 0; 39860fe637bSDr. David Alan Gilbert f->buf_size = pending; 39960fe637bSDr. David Alan Gilbert 400ac7d25b8SJuan Quintela if (qemu_file_get_error(f)) { 401a555b809SJuan Quintela return 0; 402a555b809SJuan Quintela } 403a555b809SJuan Quintela 404f759d705SDaniel P. Berrangé do { 405f759d705SDaniel P. Berrangé len = qio_channel_read(f->ioc, 406f759d705SDaniel P. Berrangé (char *)f->buf + pending, 407f759d705SDaniel P. Berrangé IO_BUF_SIZE - pending, 408f759d705SDaniel P. Berrangé &local_error); 409f759d705SDaniel P. Berrangé if (len == QIO_CHANNEL_ERR_BLOCK) { 410f759d705SDaniel P. Berrangé if (qemu_in_coroutine()) { 411f759d705SDaniel P. Berrangé qio_channel_yield(f->ioc, G_IO_IN); 412f759d705SDaniel P. Berrangé } else { 413f759d705SDaniel P. Berrangé qio_channel_wait(f->ioc, G_IO_IN); 414f759d705SDaniel P. Berrangé } 415f759d705SDaniel P. Berrangé } else if (len < 0) { 416f759d705SDaniel P. Berrangé len = -EIO; 417f759d705SDaniel P. Berrangé } 418f759d705SDaniel P. Berrangé } while (len == QIO_CHANNEL_ERR_BLOCK); 419f759d705SDaniel P. Berrangé 42060fe637bSDr. David Alan Gilbert if (len > 0) { 42160fe637bSDr. David Alan Gilbert f->buf_size += len; 422154d87b4SDaniel P. Berrangé f->total_transferred += len; 42360fe637bSDr. David Alan Gilbert } else if (len == 0) { 4243d661c8aSYury Kotov qemu_file_set_error_obj(f, -EIO, local_error); 4253d661c8aSYury Kotov } else { 4265f87072eSDaniel P. Berrangé qemu_file_set_error_obj(f, len, local_error); 42760fe637bSDr. David Alan Gilbert } 42860fe637bSDr. David Alan Gilbert 42960fe637bSDr. David Alan Gilbert return len; 43060fe637bSDr. David Alan Gilbert } 43160fe637bSDr. David Alan Gilbert 4321a93bd2fSDaniel P. Berrangé void qemu_file_credit_transfer(QEMUFile *f, size_t size) 43360fe637bSDr. David Alan Gilbert { 434154d87b4SDaniel P. Berrangé f->total_transferred += size; 43560fe637bSDr. David Alan Gilbert } 43660fe637bSDr. David Alan Gilbert 43760fe637bSDr. David Alan Gilbert /** Closes the file 43860fe637bSDr. David Alan Gilbert * 43960fe637bSDr. David Alan Gilbert * Returns negative error value if any error happened on previous operations or 44060fe637bSDr. David Alan Gilbert * while closing the file. Returns 0 or positive number on success. 44160fe637bSDr. David Alan Gilbert * 44260fe637bSDr. David Alan Gilbert * The meaning of return value on success depends on the specific backend 44360fe637bSDr. David Alan Gilbert * being used. 44460fe637bSDr. David Alan Gilbert */ 44560fe637bSDr. David Alan Gilbert int qemu_fclose(QEMUFile *f) 44660fe637bSDr. David Alan Gilbert { 4470ae1f7f0SDaniel P. Berrangé int ret, ret2; 44860fe637bSDr. David Alan Gilbert qemu_fflush(f); 44960fe637bSDr. David Alan Gilbert ret = qemu_file_get_error(f); 45060fe637bSDr. David Alan Gilbert 4510ae1f7f0SDaniel P. Berrangé ret2 = qio_channel_close(f->ioc, NULL); 45260fe637bSDr. David Alan Gilbert if (ret >= 0) { 45360fe637bSDr. David Alan Gilbert ret = ret2; 45460fe637bSDr. David Alan Gilbert } 4550ae1f7f0SDaniel P. Berrangé g_clear_pointer(&f->ioc, object_unref); 4560ae1f7f0SDaniel P. Berrangé 45760fe637bSDr. David Alan Gilbert /* If any error was spotted before closing, we should report it 45860fe637bSDr. David Alan Gilbert * instead of the close() return value. 45960fe637bSDr. David Alan Gilbert */ 46060fe637bSDr. David Alan Gilbert if (f->last_error) { 46160fe637bSDr. David Alan Gilbert ret = f->last_error; 46260fe637bSDr. David Alan Gilbert } 4633d661c8aSYury Kotov error_free(f->last_error_obj); 46460fe637bSDr. David Alan Gilbert g_free(f); 46560fe637bSDr. David Alan Gilbert trace_qemu_file_fclose(); 46660fe637bSDr. David Alan Gilbert return ret; 46760fe637bSDr. David Alan Gilbert } 46860fe637bSDr. David Alan Gilbert 4691bf57fb3SWei Yang /* 4701bf57fb3SWei Yang * Add buf to iovec. Do flush if iovec is full. 4711bf57fb3SWei Yang * 4721bf57fb3SWei Yang * Return values: 4731bf57fb3SWei Yang * 1 iovec is full and flushed 4741bf57fb3SWei Yang * 0 iovec is not flushed 4751bf57fb3SWei Yang * 4761bf57fb3SWei Yang */ 4771bf57fb3SWei Yang static int add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size, 47853f09a10SPavel Butsykin bool may_free) 47960fe637bSDr. David Alan Gilbert { 48060fe637bSDr. David Alan Gilbert /* check for adjacent buffer and coalesce them */ 48160fe637bSDr. David Alan Gilbert if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base + 48253f09a10SPavel Butsykin f->iov[f->iovcnt - 1].iov_len && 48353f09a10SPavel Butsykin may_free == test_bit(f->iovcnt - 1, f->may_free)) 48453f09a10SPavel Butsykin { 48560fe637bSDr. David Alan Gilbert f->iov[f->iovcnt - 1].iov_len += size; 48660fe637bSDr. David Alan Gilbert } else { 487c00d434aSFeng Lin if (f->iovcnt >= MAX_IOV_SIZE) { 488c00d434aSFeng Lin /* Should only happen if a previous fflush failed */ 489ac7d25b8SJuan Quintela assert(qemu_file_get_error(f) || !qemu_file_is_writable(f)); 490c00d434aSFeng Lin return 1; 491c00d434aSFeng Lin } 49253f09a10SPavel Butsykin if (may_free) { 49353f09a10SPavel Butsykin set_bit(f->iovcnt, f->may_free); 49453f09a10SPavel Butsykin } 49560fe637bSDr. David Alan Gilbert f->iov[f->iovcnt].iov_base = (uint8_t *)buf; 49660fe637bSDr. David Alan Gilbert f->iov[f->iovcnt++].iov_len = size; 49760fe637bSDr. David Alan Gilbert } 49860fe637bSDr. David Alan Gilbert 49960fe637bSDr. David Alan Gilbert if (f->iovcnt >= MAX_IOV_SIZE) { 50060fe637bSDr. David Alan Gilbert qemu_fflush(f); 5011bf57fb3SWei Yang return 1; 5021bf57fb3SWei Yang } 5031bf57fb3SWei Yang 5041bf57fb3SWei Yang return 0; 5051bf57fb3SWei Yang } 5061bf57fb3SWei Yang 5071bf57fb3SWei Yang static void add_buf_to_iovec(QEMUFile *f, size_t len) 5081bf57fb3SWei Yang { 5091bf57fb3SWei Yang if (!add_to_iovec(f, f->buf + f->buf_index, len, false)) { 5101bf57fb3SWei Yang f->buf_index += len; 5111bf57fb3SWei Yang if (f->buf_index == IO_BUF_SIZE) { 5121bf57fb3SWei Yang qemu_fflush(f); 5131bf57fb3SWei Yang } 51460fe637bSDr. David Alan Gilbert } 51560fe637bSDr. David Alan Gilbert } 51660fe637bSDr. David Alan Gilbert 51753f09a10SPavel Butsykin void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size, 51853f09a10SPavel Butsykin bool may_free) 51960fe637bSDr. David Alan Gilbert { 52060fe637bSDr. David Alan Gilbert if (f->last_error) { 52160fe637bSDr. David Alan Gilbert return; 52260fe637bSDr. David Alan Gilbert } 52360fe637bSDr. David Alan Gilbert 52453f09a10SPavel Butsykin add_to_iovec(f, buf, size, may_free); 52560fe637bSDr. David Alan Gilbert } 52660fe637bSDr. David Alan Gilbert 52756f3835fSDr. David Alan Gilbert void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size) 52860fe637bSDr. David Alan Gilbert { 52956f3835fSDr. David Alan Gilbert size_t l; 53060fe637bSDr. David Alan Gilbert 53160fe637bSDr. David Alan Gilbert if (f->last_error) { 53260fe637bSDr. David Alan Gilbert return; 53360fe637bSDr. David Alan Gilbert } 53460fe637bSDr. David Alan Gilbert 53560fe637bSDr. David Alan Gilbert while (size > 0) { 53660fe637bSDr. David Alan Gilbert l = IO_BUF_SIZE - f->buf_index; 53760fe637bSDr. David Alan Gilbert if (l > size) { 53860fe637bSDr. David Alan Gilbert l = size; 53960fe637bSDr. David Alan Gilbert } 54060fe637bSDr. David Alan Gilbert memcpy(f->buf + f->buf_index, buf, l); 5411bf57fb3SWei Yang add_buf_to_iovec(f, l); 54260fe637bSDr. David Alan Gilbert if (qemu_file_get_error(f)) { 54360fe637bSDr. David Alan Gilbert break; 54460fe637bSDr. David Alan Gilbert } 54560fe637bSDr. David Alan Gilbert buf += l; 54660fe637bSDr. David Alan Gilbert size -= l; 54760fe637bSDr. David Alan Gilbert } 54860fe637bSDr. David Alan Gilbert } 54960fe637bSDr. David Alan Gilbert 55060fe637bSDr. David Alan Gilbert void qemu_put_byte(QEMUFile *f, int v) 55160fe637bSDr. David Alan Gilbert { 55260fe637bSDr. David Alan Gilbert if (f->last_error) { 55360fe637bSDr. David Alan Gilbert return; 55460fe637bSDr. David Alan Gilbert } 55560fe637bSDr. David Alan Gilbert 55660fe637bSDr. David Alan Gilbert f->buf[f->buf_index] = v; 5571bf57fb3SWei Yang add_buf_to_iovec(f, 1); 55860fe637bSDr. David Alan Gilbert } 55960fe637bSDr. David Alan Gilbert 56060fe637bSDr. David Alan Gilbert void qemu_file_skip(QEMUFile *f, int size) 56160fe637bSDr. David Alan Gilbert { 56260fe637bSDr. David Alan Gilbert if (f->buf_index + size <= f->buf_size) { 56360fe637bSDr. David Alan Gilbert f->buf_index += size; 56460fe637bSDr. David Alan Gilbert } 56560fe637bSDr. David Alan Gilbert } 56660fe637bSDr. David Alan Gilbert 56760fe637bSDr. David Alan Gilbert /* 5687c1e52baSDr. David Alan Gilbert * Read 'size' bytes from file (at 'offset') without moving the 5697c1e52baSDr. David Alan Gilbert * pointer and set 'buf' to point to that data. 57060fe637bSDr. David Alan Gilbert * 57160fe637bSDr. David Alan Gilbert * It will return size bytes unless there was an error, in which case it will 57260fe637bSDr. David Alan Gilbert * return as many as it managed to read (assuming blocking fd's which 57360fe637bSDr. David Alan Gilbert * all current QEMUFile are) 57460fe637bSDr. David Alan Gilbert */ 575394b9407SPaolo Bonzini size_t coroutine_mixed_fn qemu_peek_buffer(QEMUFile *f, uint8_t **buf, size_t size, size_t offset) 57660fe637bSDr. David Alan Gilbert { 57756f3835fSDr. David Alan Gilbert ssize_t pending; 57856f3835fSDr. David Alan Gilbert size_t index; 57960fe637bSDr. David Alan Gilbert 58060fe637bSDr. David Alan Gilbert assert(!qemu_file_is_writable(f)); 58160fe637bSDr. David Alan Gilbert assert(offset < IO_BUF_SIZE); 58260fe637bSDr. David Alan Gilbert assert(size <= IO_BUF_SIZE - offset); 58360fe637bSDr. David Alan Gilbert 58460fe637bSDr. David Alan Gilbert /* The 1st byte to read from */ 58560fe637bSDr. David Alan Gilbert index = f->buf_index + offset; 58660fe637bSDr. David Alan Gilbert /* The number of available bytes starting at index */ 58760fe637bSDr. David Alan Gilbert pending = f->buf_size - index; 58860fe637bSDr. David Alan Gilbert 58960fe637bSDr. David Alan Gilbert /* 59060fe637bSDr. David Alan Gilbert * qemu_fill_buffer might return just a few bytes, even when there isn't 59160fe637bSDr. David Alan Gilbert * an error, so loop collecting them until we get enough. 59260fe637bSDr. David Alan Gilbert */ 59360fe637bSDr. David Alan Gilbert while (pending < size) { 59460fe637bSDr. David Alan Gilbert int received = qemu_fill_buffer(f); 59560fe637bSDr. David Alan Gilbert 59660fe637bSDr. David Alan Gilbert if (received <= 0) { 59760fe637bSDr. David Alan Gilbert break; 59860fe637bSDr. David Alan Gilbert } 59960fe637bSDr. David Alan Gilbert 60060fe637bSDr. David Alan Gilbert index = f->buf_index + offset; 60160fe637bSDr. David Alan Gilbert pending = f->buf_size - index; 60260fe637bSDr. David Alan Gilbert } 60360fe637bSDr. David Alan Gilbert 60460fe637bSDr. David Alan Gilbert if (pending <= 0) { 60560fe637bSDr. David Alan Gilbert return 0; 60660fe637bSDr. David Alan Gilbert } 60760fe637bSDr. David Alan Gilbert if (size > pending) { 60860fe637bSDr. David Alan Gilbert size = pending; 60960fe637bSDr. David Alan Gilbert } 61060fe637bSDr. David Alan Gilbert 6117c1e52baSDr. David Alan Gilbert *buf = f->buf + index; 61260fe637bSDr. David Alan Gilbert return size; 61360fe637bSDr. David Alan Gilbert } 61460fe637bSDr. David Alan Gilbert 61560fe637bSDr. David Alan Gilbert /* 61660fe637bSDr. David Alan Gilbert * Read 'size' bytes of data from the file into buf. 61760fe637bSDr. David Alan Gilbert * 'size' can be larger than the internal buffer. 61860fe637bSDr. David Alan Gilbert * 61960fe637bSDr. David Alan Gilbert * It will return size bytes unless there was an error, in which case it will 62060fe637bSDr. David Alan Gilbert * return as many as it managed to read (assuming blocking fd's which 62160fe637bSDr. David Alan Gilbert * all current QEMUFile are) 62260fe637bSDr. David Alan Gilbert */ 623394b9407SPaolo Bonzini size_t coroutine_mixed_fn qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size) 62460fe637bSDr. David Alan Gilbert { 62556f3835fSDr. David Alan Gilbert size_t pending = size; 62656f3835fSDr. David Alan Gilbert size_t done = 0; 62760fe637bSDr. David Alan Gilbert 62860fe637bSDr. David Alan Gilbert while (pending > 0) { 62956f3835fSDr. David Alan Gilbert size_t res; 6307c1e52baSDr. David Alan Gilbert uint8_t *src; 63160fe637bSDr. David Alan Gilbert 6327c1e52baSDr. David Alan Gilbert res = qemu_peek_buffer(f, &src, MIN(pending, IO_BUF_SIZE), 0); 63360fe637bSDr. David Alan Gilbert if (res == 0) { 63460fe637bSDr. David Alan Gilbert return done; 63560fe637bSDr. David Alan Gilbert } 6367c1e52baSDr. David Alan Gilbert memcpy(buf, src, res); 63760fe637bSDr. David Alan Gilbert qemu_file_skip(f, res); 63860fe637bSDr. David Alan Gilbert buf += res; 63960fe637bSDr. David Alan Gilbert pending -= res; 64060fe637bSDr. David Alan Gilbert done += res; 64160fe637bSDr. David Alan Gilbert } 64260fe637bSDr. David Alan Gilbert return done; 64360fe637bSDr. David Alan Gilbert } 64460fe637bSDr. David Alan Gilbert 64560fe637bSDr. David Alan Gilbert /* 6469504fb51SDr. David Alan Gilbert * Read 'size' bytes of data from the file. 6479504fb51SDr. David Alan Gilbert * 'size' can be larger than the internal buffer. 6489504fb51SDr. David Alan Gilbert * 6499504fb51SDr. David Alan Gilbert * The data: 6509504fb51SDr. David Alan Gilbert * may be held on an internal buffer (in which case *buf is updated 6519504fb51SDr. David Alan Gilbert * to point to it) that is valid until the next qemu_file operation. 6529504fb51SDr. David Alan Gilbert * OR 6539504fb51SDr. David Alan Gilbert * will be copied to the *buf that was passed in. 6549504fb51SDr. David Alan Gilbert * 6559504fb51SDr. David Alan Gilbert * The code tries to avoid the copy if possible. 6569504fb51SDr. David Alan Gilbert * 6579504fb51SDr. David Alan Gilbert * It will return size bytes unless there was an error, in which case it will 6589504fb51SDr. David Alan Gilbert * return as many as it managed to read (assuming blocking fd's which 6599504fb51SDr. David Alan Gilbert * all current QEMUFile are) 6609504fb51SDr. David Alan Gilbert * 6619504fb51SDr. David Alan Gilbert * Note: Since **buf may get changed, the caller should take care to 6629504fb51SDr. David Alan Gilbert * keep a pointer to the original buffer if it needs to deallocate it. 6639504fb51SDr. David Alan Gilbert */ 664394b9407SPaolo Bonzini size_t coroutine_mixed_fn qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size) 6659504fb51SDr. David Alan Gilbert { 6669504fb51SDr. David Alan Gilbert if (size < IO_BUF_SIZE) { 6679504fb51SDr. David Alan Gilbert size_t res; 6681dfafcbdSWainer dos Santos Moschetta uint8_t *src = NULL; 6699504fb51SDr. David Alan Gilbert 6709504fb51SDr. David Alan Gilbert res = qemu_peek_buffer(f, &src, size, 0); 6719504fb51SDr. David Alan Gilbert 6729504fb51SDr. David Alan Gilbert if (res == size) { 6739504fb51SDr. David Alan Gilbert qemu_file_skip(f, res); 6749504fb51SDr. David Alan Gilbert *buf = src; 6759504fb51SDr. David Alan Gilbert return res; 6769504fb51SDr. David Alan Gilbert } 6779504fb51SDr. David Alan Gilbert } 6789504fb51SDr. David Alan Gilbert 6799504fb51SDr. David Alan Gilbert return qemu_get_buffer(f, *buf, size); 6809504fb51SDr. David Alan Gilbert } 6819504fb51SDr. David Alan Gilbert 6829504fb51SDr. David Alan Gilbert /* 68360fe637bSDr. David Alan Gilbert * Peeks a single byte from the buffer; this isn't guaranteed to work if 68460fe637bSDr. David Alan Gilbert * offset leaves a gap after the previous read/peeked data. 68560fe637bSDr. David Alan Gilbert */ 686394b9407SPaolo Bonzini int coroutine_mixed_fn qemu_peek_byte(QEMUFile *f, int offset) 68760fe637bSDr. David Alan Gilbert { 68860fe637bSDr. David Alan Gilbert int index = f->buf_index + offset; 68960fe637bSDr. David Alan Gilbert 69060fe637bSDr. David Alan Gilbert assert(!qemu_file_is_writable(f)); 69160fe637bSDr. David Alan Gilbert assert(offset < IO_BUF_SIZE); 69260fe637bSDr. David Alan Gilbert 69360fe637bSDr. David Alan Gilbert if (index >= f->buf_size) { 69460fe637bSDr. David Alan Gilbert qemu_fill_buffer(f); 69560fe637bSDr. David Alan Gilbert index = f->buf_index + offset; 69660fe637bSDr. David Alan Gilbert if (index >= f->buf_size) { 69760fe637bSDr. David Alan Gilbert return 0; 69860fe637bSDr. David Alan Gilbert } 69960fe637bSDr. David Alan Gilbert } 70060fe637bSDr. David Alan Gilbert return f->buf[index]; 70160fe637bSDr. David Alan Gilbert } 70260fe637bSDr. David Alan Gilbert 703394b9407SPaolo Bonzini int coroutine_mixed_fn qemu_get_byte(QEMUFile *f) 70460fe637bSDr. David Alan Gilbert { 70560fe637bSDr. David Alan Gilbert int result; 70660fe637bSDr. David Alan Gilbert 70760fe637bSDr. David Alan Gilbert result = qemu_peek_byte(f, 0); 70860fe637bSDr. David Alan Gilbert qemu_file_skip(f, 1); 70960fe637bSDr. David Alan Gilbert return result; 71060fe637bSDr. David Alan Gilbert } 71160fe637bSDr. David Alan Gilbert 7126da835d4SJuan Quintela uint64_t qemu_file_transferred_fast(QEMUFile *f) 71397221400SAlexander Graf { 71461abf1ebSJuan Quintela uint64_t ret = f->total_transferred; 71597221400SAlexander Graf int i; 71697221400SAlexander Graf 71797221400SAlexander Graf for (i = 0; i < f->iovcnt; i++) { 71897221400SAlexander Graf ret += f->iov[i].iov_len; 71997221400SAlexander Graf } 72097221400SAlexander Graf 72197221400SAlexander Graf return ret; 72297221400SAlexander Graf } 72397221400SAlexander Graf 7246da835d4SJuan Quintela uint64_t qemu_file_transferred(QEMUFile *f) 72560fe637bSDr. David Alan Gilbert { 72660fe637bSDr. David Alan Gilbert qemu_fflush(f); 727154d87b4SDaniel P. Berrangé return f->total_transferred; 72860fe637bSDr. David Alan Gilbert } 72960fe637bSDr. David Alan Gilbert 73060fe637bSDr. David Alan Gilbert int qemu_file_rate_limit(QEMUFile *f) 73160fe637bSDr. David Alan Gilbert { 73260fe637bSDr. David Alan Gilbert if (qemu_file_get_error(f)) { 73360fe637bSDr. David Alan Gilbert return 1; 73460fe637bSDr. David Alan Gilbert } 7358e4b2a70SJuan Quintela if (f->rate_limit_max == RATE_LIMIT_DISABLED) { 7368e4b2a70SJuan Quintela return 0; 7378e4b2a70SJuan Quintela } 7388e4b2a70SJuan Quintela if (f->rate_limit_used > f->rate_limit_max) { 73960fe637bSDr. David Alan Gilbert return 1; 74060fe637bSDr. David Alan Gilbert } 74160fe637bSDr. David Alan Gilbert return 0; 74260fe637bSDr. David Alan Gilbert } 74360fe637bSDr. David Alan Gilbert 744bffc0441SJuan Quintela uint64_t qemu_file_get_rate_limit(QEMUFile *f) 74560fe637bSDr. David Alan Gilbert { 746c7fc8d32SDaniel P. Berrangé return f->rate_limit_max; 74760fe637bSDr. David Alan Gilbert } 74860fe637bSDr. David Alan Gilbert 749bffc0441SJuan Quintela void qemu_file_set_rate_limit(QEMUFile *f, uint64_t limit) 75060fe637bSDr. David Alan Gilbert { 7519d3ebbe2SJuan Quintela /* 7529d3ebbe2SJuan Quintela * 'limit' is per second. But we check it each 100 miliseconds. 7539d3ebbe2SJuan Quintela */ 7549d3ebbe2SJuan Quintela f->rate_limit_max = limit / XFER_LIMIT_RATIO; 75560fe637bSDr. David Alan Gilbert } 75660fe637bSDr. David Alan Gilbert 75760fe637bSDr. David Alan Gilbert void qemu_file_reset_rate_limit(QEMUFile *f) 75860fe637bSDr. David Alan Gilbert { 759c7fc8d32SDaniel P. Berrangé f->rate_limit_used = 0; 76060fe637bSDr. David Alan Gilbert } 76160fe637bSDr. David Alan Gilbert 762f87e4d6dSJuan Quintela void qemu_file_acct_rate_limit(QEMUFile *f, uint64_t len) 7635d7d2558SIvan Ren { 764c7fc8d32SDaniel P. Berrangé f->rate_limit_used += len; 7655d7d2558SIvan Ren } 7665d7d2558SIvan Ren 76760fe637bSDr. David Alan Gilbert void qemu_put_be16(QEMUFile *f, unsigned int v) 76860fe637bSDr. David Alan Gilbert { 76960fe637bSDr. David Alan Gilbert qemu_put_byte(f, v >> 8); 77060fe637bSDr. David Alan Gilbert qemu_put_byte(f, v); 77160fe637bSDr. David Alan Gilbert } 77260fe637bSDr. David Alan Gilbert 77360fe637bSDr. David Alan Gilbert void qemu_put_be32(QEMUFile *f, unsigned int v) 77460fe637bSDr. David Alan Gilbert { 77560fe637bSDr. David Alan Gilbert qemu_put_byte(f, v >> 24); 77660fe637bSDr. David Alan Gilbert qemu_put_byte(f, v >> 16); 77760fe637bSDr. David Alan Gilbert qemu_put_byte(f, v >> 8); 77860fe637bSDr. David Alan Gilbert qemu_put_byte(f, v); 77960fe637bSDr. David Alan Gilbert } 78060fe637bSDr. David Alan Gilbert 78160fe637bSDr. David Alan Gilbert void qemu_put_be64(QEMUFile *f, uint64_t v) 78260fe637bSDr. David Alan Gilbert { 78360fe637bSDr. David Alan Gilbert qemu_put_be32(f, v >> 32); 78460fe637bSDr. David Alan Gilbert qemu_put_be32(f, v); 78560fe637bSDr. David Alan Gilbert } 78660fe637bSDr. David Alan Gilbert 78760fe637bSDr. David Alan Gilbert unsigned int qemu_get_be16(QEMUFile *f) 78860fe637bSDr. David Alan Gilbert { 78960fe637bSDr. David Alan Gilbert unsigned int v; 79060fe637bSDr. David Alan Gilbert v = qemu_get_byte(f) << 8; 79160fe637bSDr. David Alan Gilbert v |= qemu_get_byte(f); 79260fe637bSDr. David Alan Gilbert return v; 79360fe637bSDr. David Alan Gilbert } 79460fe637bSDr. David Alan Gilbert 79560fe637bSDr. David Alan Gilbert unsigned int qemu_get_be32(QEMUFile *f) 79660fe637bSDr. David Alan Gilbert { 79760fe637bSDr. David Alan Gilbert unsigned int v; 79890d6a673SPeter Maydell v = (unsigned int)qemu_get_byte(f) << 24; 79960fe637bSDr. David Alan Gilbert v |= qemu_get_byte(f) << 16; 80060fe637bSDr. David Alan Gilbert v |= qemu_get_byte(f) << 8; 80160fe637bSDr. David Alan Gilbert v |= qemu_get_byte(f); 80260fe637bSDr. David Alan Gilbert return v; 80360fe637bSDr. David Alan Gilbert } 80460fe637bSDr. David Alan Gilbert 80560fe637bSDr. David Alan Gilbert uint64_t qemu_get_be64(QEMUFile *f) 80660fe637bSDr. David Alan Gilbert { 80760fe637bSDr. David Alan Gilbert uint64_t v; 80860fe637bSDr. David Alan Gilbert v = (uint64_t)qemu_get_be32(f) << 32; 80960fe637bSDr. David Alan Gilbert v |= qemu_get_be32(f); 81060fe637bSDr. David Alan Gilbert return v; 81160fe637bSDr. David Alan Gilbert } 81244f0eadcSLiang Li 813dcaf446eSXiao Guangrong /* return the size after compression, or negative value on error */ 814dcaf446eSXiao Guangrong static int qemu_compress_data(z_stream *stream, uint8_t *dest, size_t dest_len, 815dcaf446eSXiao Guangrong const uint8_t *source, size_t source_len) 816dcaf446eSXiao Guangrong { 817dcaf446eSXiao Guangrong int err; 818dcaf446eSXiao Guangrong 819dcaf446eSXiao Guangrong err = deflateReset(stream); 820dcaf446eSXiao Guangrong if (err != Z_OK) { 821dcaf446eSXiao Guangrong return -1; 822dcaf446eSXiao Guangrong } 823dcaf446eSXiao Guangrong 824dcaf446eSXiao Guangrong stream->avail_in = source_len; 825dcaf446eSXiao Guangrong stream->next_in = (uint8_t *)source; 826dcaf446eSXiao Guangrong stream->avail_out = dest_len; 827dcaf446eSXiao Guangrong stream->next_out = dest; 828dcaf446eSXiao Guangrong 829dcaf446eSXiao Guangrong err = deflate(stream, Z_FINISH); 830dcaf446eSXiao Guangrong if (err != Z_STREAM_END) { 831dcaf446eSXiao Guangrong return -1; 832dcaf446eSXiao Guangrong } 833dcaf446eSXiao Guangrong 834dcaf446eSXiao Guangrong return stream->next_out - dest; 835dcaf446eSXiao Guangrong } 836dcaf446eSXiao Guangrong 837dcaf446eSXiao Guangrong /* Compress size bytes of data start at p and store the compressed 838dcaf446eSXiao Guangrong * data to the buffer of f. 839b3be2896SLiang Li * 84042d24611SWei Yang * Since the file is dummy file with empty_ops, return -1 if f has no space to 84142d24611SWei Yang * save the compressed data. 84244f0eadcSLiang Li */ 843dcaf446eSXiao Guangrong ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream, 844dcaf446eSXiao Guangrong const uint8_t *p, size_t size) 84544f0eadcSLiang Li { 84644f0eadcSLiang Li ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t); 84744f0eadcSLiang Li 84844f0eadcSLiang Li if (blen < compressBound(size)) { 849b3be2896SLiang Li return -1; 850b3be2896SLiang Li } 851dcaf446eSXiao Guangrong 852dcaf446eSXiao Guangrong blen = qemu_compress_data(stream, f->buf + f->buf_index + sizeof(int32_t), 853dcaf446eSXiao Guangrong blen, p, size); 854dcaf446eSXiao Guangrong if (blen < 0) { 85534ab9e97SXiao Guangrong return -1; 85644f0eadcSLiang Li } 85734ab9e97SXiao Guangrong 85844f0eadcSLiang Li qemu_put_be32(f, blen); 8591bf57fb3SWei Yang add_buf_to_iovec(f, blen); 86044f0eadcSLiang Li return blen + sizeof(int32_t); 86144f0eadcSLiang Li } 86244f0eadcSLiang Li 86344f0eadcSLiang Li /* Put the data in the buffer of f_src to the buffer of f_des, and 86444f0eadcSLiang Li * then reset the buf_index of f_src to 0. 86544f0eadcSLiang Li */ 86644f0eadcSLiang Li 86744f0eadcSLiang Li int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src) 86844f0eadcSLiang Li { 86944f0eadcSLiang Li int len = 0; 87044f0eadcSLiang Li 87144f0eadcSLiang Li if (f_src->buf_index > 0) { 87244f0eadcSLiang Li len = f_src->buf_index; 87344f0eadcSLiang Li qemu_put_buffer(f_des, f_src->buf, f_src->buf_index); 87444f0eadcSLiang Li f_src->buf_index = 0; 875787d134fSLiang Li f_src->iovcnt = 0; 87644f0eadcSLiang Li } 87744f0eadcSLiang Li return len; 87844f0eadcSLiang Li } 879b3af1bc9SDr. David Alan Gilbert 880b3af1bc9SDr. David Alan Gilbert /* 8814024cc85SLukas Straub * Check if the writable buffer is empty 8824024cc85SLukas Straub */ 8834024cc85SLukas Straub 8844024cc85SLukas Straub bool qemu_file_buffer_empty(QEMUFile *file) 8854024cc85SLukas Straub { 8864024cc85SLukas Straub assert(qemu_file_is_writable(file)); 8874024cc85SLukas Straub 8884024cc85SLukas Straub return !file->iovcnt; 8894024cc85SLukas Straub } 8904024cc85SLukas Straub 8914024cc85SLukas Straub /* 892b3af1bc9SDr. David Alan Gilbert * Get a string whose length is determined by a single preceding byte 893b3af1bc9SDr. David Alan Gilbert * A preallocated 256 byte buffer must be passed in. 894b3af1bc9SDr. David Alan Gilbert * Returns: len on success and a 0 terminated string in the buffer 895b3af1bc9SDr. David Alan Gilbert * else 0 896b3af1bc9SDr. David Alan Gilbert * (Note a 0 length string will return 0 either way) 897b3af1bc9SDr. David Alan Gilbert */ 898394b9407SPaolo Bonzini size_t coroutine_fn qemu_get_counted_string(QEMUFile *f, char buf[256]) 899b3af1bc9SDr. David Alan Gilbert { 900b3af1bc9SDr. David Alan Gilbert size_t len = qemu_get_byte(f); 901b3af1bc9SDr. David Alan Gilbert size_t res = qemu_get_buffer(f, (uint8_t *)buf, len); 902b3af1bc9SDr. David Alan Gilbert 903b3af1bc9SDr. David Alan Gilbert buf[res] = 0; 904b3af1bc9SDr. David Alan Gilbert 905b3af1bc9SDr. David Alan Gilbert return res == len ? res : 0; 906b3af1bc9SDr. David Alan Gilbert } 907a800cd5cSDr. David Alan Gilbert 908a800cd5cSDr. David Alan Gilbert /* 909f0d64cb7SVladimir Sementsov-Ogievskiy * Put a string with one preceding byte containing its length. The length of 910f0d64cb7SVladimir Sementsov-Ogievskiy * the string should be less than 256. 911f0d64cb7SVladimir Sementsov-Ogievskiy */ 912f0d64cb7SVladimir Sementsov-Ogievskiy void qemu_put_counted_string(QEMUFile *f, const char *str) 913f0d64cb7SVladimir Sementsov-Ogievskiy { 914f0d64cb7SVladimir Sementsov-Ogievskiy size_t len = strlen(str); 915f0d64cb7SVladimir Sementsov-Ogievskiy 916f0d64cb7SVladimir Sementsov-Ogievskiy assert(len < 256); 917f0d64cb7SVladimir Sementsov-Ogievskiy qemu_put_byte(f, len); 918f0d64cb7SVladimir Sementsov-Ogievskiy qemu_put_buffer(f, (const uint8_t *)str, len); 919f0d64cb7SVladimir Sementsov-Ogievskiy } 920f0d64cb7SVladimir Sementsov-Ogievskiy 921f0d64cb7SVladimir Sementsov-Ogievskiy /* 922a800cd5cSDr. David Alan Gilbert * Set the blocking state of the QEMUFile. 923a800cd5cSDr. David Alan Gilbert * Note: On some transports the OS only keeps a single blocking state for 924a800cd5cSDr. David Alan Gilbert * both directions, and thus changing the blocking on the main 925a800cd5cSDr. David Alan Gilbert * QEMUFile can also affect the return path. 926a800cd5cSDr. David Alan Gilbert */ 927a800cd5cSDr. David Alan Gilbert void qemu_file_set_blocking(QEMUFile *f, bool block) 928a800cd5cSDr. David Alan Gilbert { 92980ad9706SDaniel P. Berrangé qio_channel_set_blocking(f->ioc, block, NULL); 93006ad5135SDaniel P. Berrange } 931c6ad5be7SPeter Xu 932c6ad5be7SPeter Xu /* 9332893a288SDaniel P. Berrangé * qemu_file_get_ioc: 9342893a288SDaniel P. Berrangé * 9352893a288SDaniel P. Berrangé * Get the ioc object for the file, without incrementing 9362893a288SDaniel P. Berrangé * the reference count. 9372893a288SDaniel P. Berrangé * 9382893a288SDaniel P. Berrangé * Returns: the ioc object 939c6ad5be7SPeter Xu */ 940c6ad5be7SPeter Xu QIOChannel *qemu_file_get_ioc(QEMUFile *file) 941c6ad5be7SPeter Xu { 9422893a288SDaniel P. Berrangé return file->ioc; 943c6ad5be7SPeter Xu } 944c7a7db4bSAvihai Horon 945c7a7db4bSAvihai Horon /* 946c7a7db4bSAvihai Horon * Read size bytes from QEMUFile f and write them to fd. 947c7a7db4bSAvihai Horon */ 948c7a7db4bSAvihai Horon int qemu_file_get_to_fd(QEMUFile *f, int fd, size_t size) 949c7a7db4bSAvihai Horon { 950c7a7db4bSAvihai Horon while (size) { 951c7a7db4bSAvihai Horon size_t pending = f->buf_size - f->buf_index; 952c7a7db4bSAvihai Horon ssize_t rc; 953c7a7db4bSAvihai Horon 954c7a7db4bSAvihai Horon if (!pending) { 955c7a7db4bSAvihai Horon rc = qemu_fill_buffer(f); 956c7a7db4bSAvihai Horon if (rc < 0) { 957c7a7db4bSAvihai Horon return rc; 958c7a7db4bSAvihai Horon } 959c7a7db4bSAvihai Horon if (rc == 0) { 960c7a7db4bSAvihai Horon return -EIO; 961c7a7db4bSAvihai Horon } 962c7a7db4bSAvihai Horon continue; 963c7a7db4bSAvihai Horon } 964c7a7db4bSAvihai Horon 965c7a7db4bSAvihai Horon rc = write(fd, f->buf + f->buf_index, MIN(pending, size)); 966c7a7db4bSAvihai Horon if (rc < 0) { 967c7a7db4bSAvihai Horon return -errno; 968c7a7db4bSAvihai Horon } 969c7a7db4bSAvihai Horon if (rc == 0) { 970c7a7db4bSAvihai Horon return -EIO; 971c7a7db4bSAvihai Horon } 972c7a7db4bSAvihai Horon f->buf_index += rc; 973c7a7db4bSAvihai Horon size -= rc; 974c7a7db4bSAvihai Horon } 975c7a7db4bSAvihai Horon 976c7a7db4bSAvihai Horon return 0; 977c7a7db4bSAvihai Horon } 978