2
0

qemu-file.c 20 KB

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