migration.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * QEMU live migration
  3. *
  4. * Copyright IBM, Corp. 2008
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. *
  12. */
  13. #ifndef QEMU_MIGRATION_H
  14. #define QEMU_MIGRATION_H
  15. #include "exec/cpu-common.h"
  16. #include "hw/qdev-core.h"
  17. #include "qapi/qapi-types-migration.h"
  18. #include "qemu/thread.h"
  19. #include "qemu/coroutine_int.h"
  20. #include "io/channel.h"
  21. #include "net/announce.h"
  22. #include "qom/object.h"
  23. struct PostcopyBlocktimeContext;
  24. #define MIGRATION_RESUME_ACK_VALUE (1)
  25. /*
  26. * 1<<6=64 pages -> 256K chunk when page size is 4K. This gives us
  27. * the benefit that all the chunks are 64 pages aligned then the
  28. * bitmaps are always aligned to LONG.
  29. */
  30. #define CLEAR_BITMAP_SHIFT_MIN 6
  31. /*
  32. * 1<<18=256K pages -> 1G chunk when page size is 4K. This is the
  33. * default value to use if no one specified.
  34. */
  35. #define CLEAR_BITMAP_SHIFT_DEFAULT 18
  36. /*
  37. * 1<<31=2G pages -> 8T chunk when page size is 4K. This should be
  38. * big enough and make sure we won't overflow easily.
  39. */
  40. #define CLEAR_BITMAP_SHIFT_MAX 31
  41. /* State for the incoming migration */
  42. struct MigrationIncomingState {
  43. QEMUFile *from_src_file;
  44. /*
  45. * Free at the start of the main state load, set as the main thread finishes
  46. * loading state.
  47. */
  48. QemuEvent main_thread_load_event;
  49. /* For network announces */
  50. AnnounceTimer announce_timer;
  51. size_t largest_page_size;
  52. bool have_fault_thread;
  53. QemuThread fault_thread;
  54. QemuSemaphore fault_thread_sem;
  55. /* Set this when we want the fault thread to quit */
  56. bool fault_thread_quit;
  57. bool have_listen_thread;
  58. QemuThread listen_thread;
  59. QemuSemaphore listen_thread_sem;
  60. /* For the kernel to send us notifications */
  61. int userfault_fd;
  62. /* To notify the fault_thread to wake, e.g., when need to quit */
  63. int userfault_event_fd;
  64. QEMUFile *to_src_file;
  65. QemuMutex rp_mutex; /* We send replies from multiple threads */
  66. /* RAMBlock of last request sent to source */
  67. RAMBlock *last_rb;
  68. void *postcopy_tmp_page;
  69. void *postcopy_tmp_zero_page;
  70. /* PostCopyFD's for external userfaultfds & handlers of shared memory */
  71. GArray *postcopy_remote_fds;
  72. QEMUBH *bh;
  73. int state;
  74. bool have_colo_incoming_thread;
  75. QemuThread colo_incoming_thread;
  76. /* The coroutine we should enter (back) after failover */
  77. Coroutine *migration_incoming_co;
  78. QemuSemaphore colo_incoming_sem;
  79. /*
  80. * PostcopyBlocktimeContext to keep information for postcopy
  81. * live migration, to calculate vCPU block time
  82. * */
  83. struct PostcopyBlocktimeContext *blocktime_ctx;
  84. /* notify PAUSED postcopy incoming migrations to try to continue */
  85. bool postcopy_recover_triggered;
  86. QemuSemaphore postcopy_pause_sem_dst;
  87. QemuSemaphore postcopy_pause_sem_fault;
  88. /* List of listening socket addresses */
  89. SocketAddressList *socket_address_list;
  90. };
  91. MigrationIncomingState *migration_incoming_get_current(void);
  92. void migration_incoming_state_destroy(void);
  93. /*
  94. * Functions to work with blocktime context
  95. */
  96. void fill_destination_postcopy_migration_info(MigrationInfo *info);
  97. #define TYPE_MIGRATION "migration"
  98. typedef struct MigrationClass MigrationClass;
  99. DECLARE_OBJ_CHECKERS(MigrationState, MigrationClass,
  100. MIGRATION_OBJ, TYPE_MIGRATION)
  101. struct MigrationClass {
  102. /*< private >*/
  103. DeviceClass parent_class;
  104. };
  105. struct MigrationState
  106. {
  107. /*< private >*/
  108. DeviceState parent_obj;
  109. /*< public >*/
  110. QemuThread thread;
  111. QEMUBH *cleanup_bh;
  112. QEMUFile *to_dst_file;
  113. /*
  114. * Protects to_dst_file pointer. We need to make sure we won't
  115. * yield or hang during the critical section, since this lock will
  116. * be used in OOB command handler.
  117. */
  118. QemuMutex qemu_file_lock;
  119. /*
  120. * Used to allow urgent requests to override rate limiting.
  121. */
  122. QemuSemaphore rate_limit_sem;
  123. /* pages already send at the beginning of current iteration */
  124. uint64_t iteration_initial_pages;
  125. /* pages transferred per second */
  126. double pages_per_second;
  127. /* bytes already send at the beginning of current iteration */
  128. uint64_t iteration_initial_bytes;
  129. /* time at the start of current iteration */
  130. int64_t iteration_start_time;
  131. /*
  132. * The final stage happens when the remaining data is smaller than
  133. * this threshold; it's calculated from the requested downtime and
  134. * measured bandwidth
  135. */
  136. int64_t threshold_size;
  137. /* params from 'migrate-set-parameters' */
  138. MigrationParameters parameters;
  139. int state;
  140. /* State related to return path */
  141. struct {
  142. QEMUFile *from_dst_file;
  143. QemuThread rp_thread;
  144. bool error;
  145. QemuSemaphore rp_sem;
  146. } rp_state;
  147. double mbps;
  148. /* Timestamp when recent migration starts (ms) */
  149. int64_t start_time;
  150. /* Total time used by latest migration (ms) */
  151. int64_t total_time;
  152. /* Timestamp when VM is down (ms) to migrate the last stuff */
  153. int64_t downtime_start;
  154. int64_t downtime;
  155. int64_t expected_downtime;
  156. bool enabled_capabilities[MIGRATION_CAPABILITY__MAX];
  157. int64_t setup_time;
  158. /*
  159. * Whether guest was running when we enter the completion stage.
  160. * If migration is interrupted by any reason, we need to continue
  161. * running the guest on source.
  162. */
  163. bool vm_was_running;
  164. /* Flag set once the migration has been asked to enter postcopy */
  165. bool start_postcopy;
  166. /* Flag set after postcopy has sent the device state */
  167. bool postcopy_after_devices;
  168. /* Flag set once the migration thread is running (and needs joining) */
  169. bool migration_thread_running;
  170. /* Flag set once the migration thread called bdrv_inactivate_all */
  171. bool block_inactive;
  172. /* Migration is waiting for guest to unplug device */
  173. QemuSemaphore wait_unplug_sem;
  174. /* Migration is paused due to pause-before-switchover */
  175. QemuSemaphore pause_sem;
  176. /* The semaphore is used to notify COLO thread that failover is finished */
  177. QemuSemaphore colo_exit_sem;
  178. /* The event is used to notify COLO thread to do checkpoint */
  179. QemuEvent colo_checkpoint_event;
  180. int64_t colo_checkpoint_time;
  181. QEMUTimer *colo_delay_timer;
  182. /* The first error that has occurred.
  183. We used the mutex to be able to return the 1st error message */
  184. Error *error;
  185. /* mutex to protect errp */
  186. QemuMutex error_mutex;
  187. /* Do we have to clean up -b/-i from old migrate parameters */
  188. /* This feature is deprecated and will be removed */
  189. bool must_remove_block_options;
  190. /*
  191. * Global switch on whether we need to store the global state
  192. * during migration.
  193. */
  194. bool store_global_state;
  195. /* Whether we send QEMU_VM_CONFIGURATION during migration */
  196. bool send_configuration;
  197. /* Whether we send section footer during migration */
  198. bool send_section_footer;
  199. /* Needed by postcopy-pause state */
  200. QemuSemaphore postcopy_pause_sem;
  201. QemuSemaphore postcopy_pause_rp_sem;
  202. /*
  203. * Whether we abort the migration if decompression errors are
  204. * detected at the destination. It is left at false for qemu
  205. * older than 3.0, since only newer qemu sends streams that
  206. * do not trigger spurious decompression errors.
  207. */
  208. bool decompress_error_check;
  209. /*
  210. * This decides the size of guest memory chunk that will be used
  211. * to track dirty bitmap clearing. The size of memory chunk will
  212. * be GUEST_PAGE_SIZE << N. Say, N=0 means we will clear dirty
  213. * bitmap for each page to send (1<<0=1); N=10 means we will clear
  214. * dirty bitmap only once for 1<<10=1K continuous guest pages
  215. * (which is in 4M chunk).
  216. */
  217. uint8_t clear_bitmap_shift;
  218. /*
  219. * This save hostname when out-going migration starts
  220. */
  221. char *hostname;
  222. };
  223. void migrate_set_state(int *state, int old_state, int new_state);
  224. void migration_fd_process_incoming(QEMUFile *f, Error **errp);
  225. void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp);
  226. void migration_incoming_process(void);
  227. bool migration_has_all_channels(void);
  228. uint64_t migrate_max_downtime(void);
  229. void migrate_set_error(MigrationState *s, const Error *error);
  230. void migrate_fd_error(MigrationState *s, const Error *error);
  231. void migrate_fd_connect(MigrationState *s, Error *error_in);
  232. bool migration_is_setup_or_active(int state);
  233. bool migration_is_running(int state);
  234. void migrate_init(MigrationState *s);
  235. bool migration_is_blocked(Error **errp);
  236. /* True if outgoing migration has entered postcopy phase */
  237. bool migration_in_postcopy(void);
  238. MigrationState *migrate_get_current(void);
  239. bool migrate_postcopy(void);
  240. bool migrate_release_ram(void);
  241. bool migrate_postcopy_ram(void);
  242. bool migrate_zero_blocks(void);
  243. bool migrate_dirty_bitmaps(void);
  244. bool migrate_ignore_shared(void);
  245. bool migrate_validate_uuid(void);
  246. bool migrate_auto_converge(void);
  247. bool migrate_use_multifd(void);
  248. bool migrate_pause_before_switchover(void);
  249. int migrate_multifd_channels(void);
  250. MultiFDCompression migrate_multifd_compression(void);
  251. int migrate_multifd_zlib_level(void);
  252. int migrate_multifd_zstd_level(void);
  253. int migrate_use_xbzrle(void);
  254. int64_t migrate_xbzrle_cache_size(void);
  255. bool migrate_colo_enabled(void);
  256. bool migrate_use_block(void);
  257. bool migrate_use_block_incremental(void);
  258. int migrate_max_cpu_throttle(void);
  259. bool migrate_use_return_path(void);
  260. uint64_t ram_get_total_transferred_pages(void);
  261. bool migrate_use_compression(void);
  262. int migrate_compress_level(void);
  263. int migrate_compress_threads(void);
  264. int migrate_compress_wait_thread(void);
  265. int migrate_decompress_threads(void);
  266. bool migrate_use_events(void);
  267. bool migrate_postcopy_blocktime(void);
  268. /* Sending on the return path - generic and then for each message type */
  269. void migrate_send_rp_shut(MigrationIncomingState *mis,
  270. uint32_t value);
  271. void migrate_send_rp_pong(MigrationIncomingState *mis,
  272. uint32_t value);
  273. int migrate_send_rp_req_pages(MigrationIncomingState *mis, RAMBlock *rb,
  274. ram_addr_t start);
  275. void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
  276. char *block_name);
  277. void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value);
  278. void dirty_bitmap_mig_before_vm_start(void);
  279. void dirty_bitmap_mig_cancel_outgoing(void);
  280. void dirty_bitmap_mig_cancel_incoming(void);
  281. bool check_dirty_bitmap_mig_alias_map(const BitmapMigrationNodeAliasList *bbm,
  282. Error **errp);
  283. void migrate_add_address(SocketAddress *address);
  284. int foreach_not_ignored_block(RAMBlockIterFunc func, void *opaque);
  285. #define qemu_ram_foreach_block \
  286. #warning "Use foreach_not_ignored_block in migration code"
  287. void migration_make_urgent_request(void);
  288. void migration_consume_urgent_request(void);
  289. bool migration_rate_limit(void);
  290. #endif