2
0

migration.h 11 KB

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