2
0

qemu-file.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #ifndef MIGRATION_QEMU_FILE_H
  25. #define MIGRATION_QEMU_FILE_H
  26. #include <zlib.h>
  27. #include "exec/cpu-common.h"
  28. /* Read a chunk of data from a file at the given position. The pos argument
  29. * can be ignored if the file is only be used for streaming. The number of
  30. * bytes actually read should be returned.
  31. */
  32. typedef ssize_t (QEMUFileGetBufferFunc)(void *opaque, uint8_t *buf,
  33. int64_t pos, size_t size,
  34. Error **errp);
  35. /* Close a file
  36. *
  37. * Return negative error number on error, 0 or positive value on success.
  38. *
  39. * The meaning of return value on success depends on the specific back-end being
  40. * used.
  41. */
  42. typedef int (QEMUFileCloseFunc)(void *opaque, Error **errp);
  43. /* Called to return the OS file descriptor associated to the QEMUFile.
  44. */
  45. typedef int (QEMUFileGetFD)(void *opaque);
  46. /* Called to change the blocking mode of the file
  47. */
  48. typedef int (QEMUFileSetBlocking)(void *opaque, bool enabled, Error **errp);
  49. /*
  50. * This function writes an iovec to file. The handler must write all
  51. * of the data or return a negative errno value.
  52. */
  53. typedef ssize_t (QEMUFileWritevBufferFunc)(void *opaque, struct iovec *iov,
  54. int iovcnt, int64_t pos,
  55. Error **errp);
  56. /*
  57. * This function provides hooks around different
  58. * stages of RAM migration.
  59. * 'opaque' is the backend specific data in QEMUFile
  60. * 'data' is call specific data associated with the 'flags' value
  61. */
  62. typedef int (QEMURamHookFunc)(QEMUFile *f, void *opaque, uint64_t flags,
  63. void *data);
  64. /*
  65. * Constants used by ram_control_* hooks
  66. */
  67. #define RAM_CONTROL_SETUP 0
  68. #define RAM_CONTROL_ROUND 1
  69. #define RAM_CONTROL_HOOK 2
  70. #define RAM_CONTROL_FINISH 3
  71. #define RAM_CONTROL_BLOCK_REG 4
  72. /*
  73. * This function allows override of where the RAM page
  74. * is saved (such as RDMA, for example.)
  75. */
  76. typedef size_t (QEMURamSaveFunc)(QEMUFile *f, void *opaque,
  77. ram_addr_t block_offset,
  78. ram_addr_t offset,
  79. size_t size,
  80. uint64_t *bytes_sent);
  81. /*
  82. * Return a QEMUFile for comms in the opposite direction
  83. */
  84. typedef QEMUFile *(QEMURetPathFunc)(void *opaque);
  85. /*
  86. * Stop any read or write (depending on flags) on the underlying
  87. * transport on the QEMUFile.
  88. * Existing blocking reads/writes must be woken
  89. * Returns 0 on success, -err on error
  90. */
  91. typedef int (QEMUFileShutdownFunc)(void *opaque, bool rd, bool wr,
  92. Error **errp);
  93. typedef struct QEMUFileOps {
  94. QEMUFileGetBufferFunc *get_buffer;
  95. QEMUFileCloseFunc *close;
  96. QEMUFileSetBlocking *set_blocking;
  97. QEMUFileWritevBufferFunc *writev_buffer;
  98. QEMURetPathFunc *get_return_path;
  99. QEMUFileShutdownFunc *shut_down;
  100. } QEMUFileOps;
  101. typedef struct QEMUFileHooks {
  102. QEMURamHookFunc *before_ram_iterate;
  103. QEMURamHookFunc *after_ram_iterate;
  104. QEMURamHookFunc *hook_ram_load;
  105. QEMURamSaveFunc *save_page;
  106. } QEMUFileHooks;
  107. QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops);
  108. void qemu_file_set_hooks(QEMUFile *f, const QEMUFileHooks *hooks);
  109. int qemu_get_fd(QEMUFile *f);
  110. int qemu_fclose(QEMUFile *f);
  111. int64_t qemu_ftell(QEMUFile *f);
  112. int64_t qemu_ftell_fast(QEMUFile *f);
  113. /*
  114. * put_buffer without copying the buffer.
  115. * The buffer should be available till it is sent asynchronously.
  116. */
  117. void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,
  118. bool may_free);
  119. bool qemu_file_mode_is_not_valid(const char *mode);
  120. bool qemu_file_is_writable(QEMUFile *f);
  121. #include "migration/qemu-file-types.h"
  122. size_t qemu_peek_buffer(QEMUFile *f, uint8_t **buf, size_t size, size_t offset);
  123. size_t qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size);
  124. ssize_t qemu_put_compression_data(QEMUFile *f, z_stream *stream,
  125. const uint8_t *p, size_t size);
  126. int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src);
  127. /*
  128. * Note that you can only peek continuous bytes from where the current pointer
  129. * is; you aren't guaranteed to be able to peak to +n bytes unless you've
  130. * previously peeked +n-1.
  131. */
  132. int qemu_peek_byte(QEMUFile *f, int offset);
  133. void qemu_file_skip(QEMUFile *f, int size);
  134. void qemu_update_position(QEMUFile *f, size_t size);
  135. void qemu_file_reset_rate_limit(QEMUFile *f);
  136. void qemu_file_update_transfer(QEMUFile *f, int64_t len);
  137. void qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate);
  138. int64_t qemu_file_get_rate_limit(QEMUFile *f);
  139. int qemu_file_get_error_obj(QEMUFile *f, Error **errp);
  140. void qemu_file_set_error_obj(QEMUFile *f, int ret, Error *err);
  141. void qemu_file_set_error(QEMUFile *f, int ret);
  142. int qemu_file_shutdown(QEMUFile *f);
  143. QEMUFile *qemu_file_get_return_path(QEMUFile *f);
  144. void qemu_fflush(QEMUFile *f);
  145. void qemu_file_set_blocking(QEMUFile *f, bool block);
  146. void ram_control_before_iterate(QEMUFile *f, uint64_t flags);
  147. void ram_control_after_iterate(QEMUFile *f, uint64_t flags);
  148. void ram_control_load_hook(QEMUFile *f, uint64_t flags, void *data);
  149. /* Whenever this is found in the data stream, the flags
  150. * will be passed to ram_control_load_hook in the incoming-migration
  151. * side. This lets before_ram_iterate/after_ram_iterate add
  152. * transport-specific sections to the RAM migration data.
  153. */
  154. #define RAM_SAVE_FLAG_HOOK 0x80
  155. #define RAM_SAVE_CONTROL_NOT_SUPP -1000
  156. #define RAM_SAVE_CONTROL_DELAYED -2000
  157. size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
  158. ram_addr_t offset, size_t size,
  159. uint64_t *bytes_sent);
  160. #endif