qemu-file.c 24 KB

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