migration.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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. * Contributions after 2012-01-13 are licensed under the terms of the
  13. * GNU GPL, version 2 or (at your option) any later version.
  14. */
  15. #include "qemu-common.h"
  16. #include "qemu/main-loop.h"
  17. #include "migration/migration.h"
  18. #include "monitor/monitor.h"
  19. #include "migration/qemu-file.h"
  20. #include "sysemu/sysemu.h"
  21. #include "block/block.h"
  22. #include "qemu/sockets.h"
  23. #include "migration/block.h"
  24. #include "qemu/thread.h"
  25. #include "qmp-commands.h"
  26. #include "trace.h"
  27. enum {
  28. MIG_STATE_ERROR = -1,
  29. MIG_STATE_NONE,
  30. MIG_STATE_SETUP,
  31. MIG_STATE_CANCELLING,
  32. MIG_STATE_CANCELLED,
  33. MIG_STATE_ACTIVE,
  34. MIG_STATE_COMPLETED,
  35. };
  36. #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
  37. /* Amount of time to allocate to each "chunk" of bandwidth-throttled
  38. * data. */
  39. #define BUFFER_DELAY 100
  40. #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
  41. /* Migration XBZRLE default cache size */
  42. #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024)
  43. static NotifierList migration_state_notifiers =
  44. NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
  45. /* When we add fault tolerance, we could have several
  46. migrations at once. For now we don't need to add
  47. dynamic creation of migration */
  48. MigrationState *migrate_get_current(void)
  49. {
  50. static MigrationState current_migration = {
  51. .state = MIG_STATE_NONE,
  52. .bandwidth_limit = MAX_THROTTLE,
  53. .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE,
  54. .mbps = -1,
  55. };
  56. return &current_migration;
  57. }
  58. void qemu_start_incoming_migration(const char *uri, Error **errp)
  59. {
  60. const char *p;
  61. if (strstart(uri, "tcp:", &p))
  62. tcp_start_incoming_migration(p, errp);
  63. #ifdef CONFIG_RDMA
  64. else if (strstart(uri, "rdma:", &p))
  65. rdma_start_incoming_migration(p, errp);
  66. #endif
  67. #if !defined(WIN32)
  68. else if (strstart(uri, "exec:", &p))
  69. exec_start_incoming_migration(p, errp);
  70. else if (strstart(uri, "unix:", &p))
  71. unix_start_incoming_migration(p, errp);
  72. else if (strstart(uri, "fd:", &p))
  73. fd_start_incoming_migration(p, errp);
  74. #endif
  75. else {
  76. error_setg(errp, "unknown migration protocol: %s", uri);
  77. }
  78. }
  79. static void process_incoming_migration_co(void *opaque)
  80. {
  81. QEMUFile *f = opaque;
  82. Error *local_err = NULL;
  83. int ret;
  84. ret = qemu_loadvm_state(f);
  85. qemu_fclose(f);
  86. free_xbzrle_decoded_buf();
  87. if (ret < 0) {
  88. error_report("load of migration failed: %s", strerror(-ret));
  89. exit(EXIT_FAILURE);
  90. }
  91. qemu_announce_self();
  92. bdrv_clear_incoming_migration_all();
  93. /* Make sure all file formats flush their mutable metadata */
  94. bdrv_invalidate_cache_all(&local_err);
  95. if (local_err) {
  96. qerror_report_err(local_err);
  97. error_free(local_err);
  98. exit(EXIT_FAILURE);
  99. }
  100. if (autostart) {
  101. vm_start();
  102. } else {
  103. runstate_set(RUN_STATE_PAUSED);
  104. }
  105. }
  106. void process_incoming_migration(QEMUFile *f)
  107. {
  108. Coroutine *co = qemu_coroutine_create(process_incoming_migration_co);
  109. int fd = qemu_get_fd(f);
  110. assert(fd != -1);
  111. qemu_set_nonblock(fd);
  112. qemu_coroutine_enter(co, f);
  113. }
  114. /* amount of nanoseconds we are willing to wait for migration to be down.
  115. * the choice of nanoseconds is because it is the maximum resolution that
  116. * get_clock() can achieve. It is an internal measure. All user-visible
  117. * units must be in seconds */
  118. static uint64_t max_downtime = 30000000;
  119. uint64_t migrate_max_downtime(void)
  120. {
  121. return max_downtime;
  122. }
  123. MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
  124. {
  125. MigrationCapabilityStatusList *head = NULL;
  126. MigrationCapabilityStatusList *caps;
  127. MigrationState *s = migrate_get_current();
  128. int i;
  129. caps = NULL; /* silence compiler warning */
  130. for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
  131. if (head == NULL) {
  132. head = g_malloc0(sizeof(*caps));
  133. caps = head;
  134. } else {
  135. caps->next = g_malloc0(sizeof(*caps));
  136. caps = caps->next;
  137. }
  138. caps->value =
  139. g_malloc(sizeof(*caps->value));
  140. caps->value->capability = i;
  141. caps->value->state = s->enabled_capabilities[i];
  142. }
  143. return head;
  144. }
  145. static void get_xbzrle_cache_stats(MigrationInfo *info)
  146. {
  147. if (migrate_use_xbzrle()) {
  148. info->has_xbzrle_cache = true;
  149. info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
  150. info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
  151. info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred();
  152. info->xbzrle_cache->pages = xbzrle_mig_pages_transferred();
  153. info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss();
  154. info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow();
  155. }
  156. }
  157. MigrationInfo *qmp_query_migrate(Error **errp)
  158. {
  159. MigrationInfo *info = g_malloc0(sizeof(*info));
  160. MigrationState *s = migrate_get_current();
  161. switch (s->state) {
  162. case MIG_STATE_NONE:
  163. /* no migration has happened ever */
  164. break;
  165. case MIG_STATE_SETUP:
  166. info->has_status = true;
  167. info->status = g_strdup("setup");
  168. info->has_total_time = false;
  169. break;
  170. case MIG_STATE_ACTIVE:
  171. case MIG_STATE_CANCELLING:
  172. info->has_status = true;
  173. info->status = g_strdup("active");
  174. info->has_total_time = true;
  175. info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
  176. - s->total_time;
  177. info->has_expected_downtime = true;
  178. info->expected_downtime = s->expected_downtime;
  179. info->has_setup_time = true;
  180. info->setup_time = s->setup_time;
  181. info->has_ram = true;
  182. info->ram = g_malloc0(sizeof(*info->ram));
  183. info->ram->transferred = ram_bytes_transferred();
  184. info->ram->remaining = ram_bytes_remaining();
  185. info->ram->total = ram_bytes_total();
  186. info->ram->duplicate = dup_mig_pages_transferred();
  187. info->ram->skipped = skipped_mig_pages_transferred();
  188. info->ram->normal = norm_mig_pages_transferred();
  189. info->ram->normal_bytes = norm_mig_bytes_transferred();
  190. info->ram->dirty_pages_rate = s->dirty_pages_rate;
  191. info->ram->mbps = s->mbps;
  192. if (blk_mig_active()) {
  193. info->has_disk = true;
  194. info->disk = g_malloc0(sizeof(*info->disk));
  195. info->disk->transferred = blk_mig_bytes_transferred();
  196. info->disk->remaining = blk_mig_bytes_remaining();
  197. info->disk->total = blk_mig_bytes_total();
  198. }
  199. get_xbzrle_cache_stats(info);
  200. break;
  201. case MIG_STATE_COMPLETED:
  202. get_xbzrle_cache_stats(info);
  203. info->has_status = true;
  204. info->status = g_strdup("completed");
  205. info->has_total_time = true;
  206. info->total_time = s->total_time;
  207. info->has_downtime = true;
  208. info->downtime = s->downtime;
  209. info->has_setup_time = true;
  210. info->setup_time = s->setup_time;
  211. info->has_ram = true;
  212. info->ram = g_malloc0(sizeof(*info->ram));
  213. info->ram->transferred = ram_bytes_transferred();
  214. info->ram->remaining = 0;
  215. info->ram->total = ram_bytes_total();
  216. info->ram->duplicate = dup_mig_pages_transferred();
  217. info->ram->skipped = skipped_mig_pages_transferred();
  218. info->ram->normal = norm_mig_pages_transferred();
  219. info->ram->normal_bytes = norm_mig_bytes_transferred();
  220. info->ram->mbps = s->mbps;
  221. break;
  222. case MIG_STATE_ERROR:
  223. info->has_status = true;
  224. info->status = g_strdup("failed");
  225. break;
  226. case MIG_STATE_CANCELLED:
  227. info->has_status = true;
  228. info->status = g_strdup("cancelled");
  229. break;
  230. }
  231. return info;
  232. }
  233. void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
  234. Error **errp)
  235. {
  236. MigrationState *s = migrate_get_current();
  237. MigrationCapabilityStatusList *cap;
  238. if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP) {
  239. error_set(errp, QERR_MIGRATION_ACTIVE);
  240. return;
  241. }
  242. for (cap = params; cap; cap = cap->next) {
  243. s->enabled_capabilities[cap->value->capability] = cap->value->state;
  244. }
  245. }
  246. /* shared migration helpers */
  247. static void migrate_set_state(MigrationState *s, int old_state, int new_state)
  248. {
  249. if (atomic_cmpxchg(&s->state, old_state, new_state) == new_state) {
  250. trace_migrate_set_state(new_state);
  251. }
  252. }
  253. static void migrate_fd_cleanup(void *opaque)
  254. {
  255. MigrationState *s = opaque;
  256. qemu_bh_delete(s->cleanup_bh);
  257. s->cleanup_bh = NULL;
  258. if (s->file) {
  259. trace_migrate_fd_cleanup();
  260. qemu_mutex_unlock_iothread();
  261. qemu_thread_join(&s->thread);
  262. qemu_mutex_lock_iothread();
  263. qemu_fclose(s->file);
  264. s->file = NULL;
  265. }
  266. assert(s->state != MIG_STATE_ACTIVE);
  267. if (s->state != MIG_STATE_COMPLETED) {
  268. qemu_savevm_state_cancel();
  269. if (s->state == MIG_STATE_CANCELLING) {
  270. migrate_set_state(s, MIG_STATE_CANCELLING, MIG_STATE_CANCELLED);
  271. }
  272. }
  273. notifier_list_notify(&migration_state_notifiers, s);
  274. }
  275. void migrate_fd_error(MigrationState *s)
  276. {
  277. trace_migrate_fd_error();
  278. assert(s->file == NULL);
  279. s->state = MIG_STATE_ERROR;
  280. trace_migrate_set_state(MIG_STATE_ERROR);
  281. notifier_list_notify(&migration_state_notifiers, s);
  282. }
  283. static void migrate_fd_cancel(MigrationState *s)
  284. {
  285. int old_state ;
  286. trace_migrate_fd_cancel();
  287. do {
  288. old_state = s->state;
  289. if (old_state != MIG_STATE_SETUP && old_state != MIG_STATE_ACTIVE) {
  290. break;
  291. }
  292. migrate_set_state(s, old_state, MIG_STATE_CANCELLING);
  293. } while (s->state != MIG_STATE_CANCELLING);
  294. }
  295. void add_migration_state_change_notifier(Notifier *notify)
  296. {
  297. notifier_list_add(&migration_state_notifiers, notify);
  298. }
  299. void remove_migration_state_change_notifier(Notifier *notify)
  300. {
  301. notifier_remove(notify);
  302. }
  303. bool migration_in_setup(MigrationState *s)
  304. {
  305. return s->state == MIG_STATE_SETUP;
  306. }
  307. bool migration_has_finished(MigrationState *s)
  308. {
  309. return s->state == MIG_STATE_COMPLETED;
  310. }
  311. bool migration_has_failed(MigrationState *s)
  312. {
  313. return (s->state == MIG_STATE_CANCELLED ||
  314. s->state == MIG_STATE_ERROR);
  315. }
  316. static MigrationState *migrate_init(const MigrationParams *params)
  317. {
  318. MigrationState *s = migrate_get_current();
  319. int64_t bandwidth_limit = s->bandwidth_limit;
  320. bool enabled_capabilities[MIGRATION_CAPABILITY_MAX];
  321. int64_t xbzrle_cache_size = s->xbzrle_cache_size;
  322. memcpy(enabled_capabilities, s->enabled_capabilities,
  323. sizeof(enabled_capabilities));
  324. memset(s, 0, sizeof(*s));
  325. s->params = *params;
  326. memcpy(s->enabled_capabilities, enabled_capabilities,
  327. sizeof(enabled_capabilities));
  328. s->xbzrle_cache_size = xbzrle_cache_size;
  329. s->bandwidth_limit = bandwidth_limit;
  330. s->state = MIG_STATE_SETUP;
  331. trace_migrate_set_state(MIG_STATE_SETUP);
  332. s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
  333. return s;
  334. }
  335. static GSList *migration_blockers;
  336. void migrate_add_blocker(Error *reason)
  337. {
  338. migration_blockers = g_slist_prepend(migration_blockers, reason);
  339. }
  340. void migrate_del_blocker(Error *reason)
  341. {
  342. migration_blockers = g_slist_remove(migration_blockers, reason);
  343. }
  344. void qmp_migrate(const char *uri, bool has_blk, bool blk,
  345. bool has_inc, bool inc, bool has_detach, bool detach,
  346. Error **errp)
  347. {
  348. Error *local_err = NULL;
  349. MigrationState *s = migrate_get_current();
  350. MigrationParams params;
  351. const char *p;
  352. params.blk = has_blk && blk;
  353. params.shared = has_inc && inc;
  354. if (s->state == MIG_STATE_ACTIVE || s->state == MIG_STATE_SETUP ||
  355. s->state == MIG_STATE_CANCELLING) {
  356. error_set(errp, QERR_MIGRATION_ACTIVE);
  357. return;
  358. }
  359. if (qemu_savevm_state_blocked(errp)) {
  360. return;
  361. }
  362. if (migration_blockers) {
  363. *errp = error_copy(migration_blockers->data);
  364. return;
  365. }
  366. s = migrate_init(&params);
  367. if (strstart(uri, "tcp:", &p)) {
  368. tcp_start_outgoing_migration(s, p, &local_err);
  369. #ifdef CONFIG_RDMA
  370. } else if (strstart(uri, "rdma:", &p)) {
  371. rdma_start_outgoing_migration(s, p, &local_err);
  372. #endif
  373. #if !defined(WIN32)
  374. } else if (strstart(uri, "exec:", &p)) {
  375. exec_start_outgoing_migration(s, p, &local_err);
  376. } else if (strstart(uri, "unix:", &p)) {
  377. unix_start_outgoing_migration(s, p, &local_err);
  378. } else if (strstart(uri, "fd:", &p)) {
  379. fd_start_outgoing_migration(s, p, &local_err);
  380. #endif
  381. } else {
  382. error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
  383. s->state = MIG_STATE_ERROR;
  384. return;
  385. }
  386. if (local_err) {
  387. migrate_fd_error(s);
  388. error_propagate(errp, local_err);
  389. return;
  390. }
  391. }
  392. void qmp_migrate_cancel(Error **errp)
  393. {
  394. migrate_fd_cancel(migrate_get_current());
  395. }
  396. void qmp_migrate_set_cache_size(int64_t value, Error **errp)
  397. {
  398. MigrationState *s = migrate_get_current();
  399. int64_t new_size;
  400. /* Check for truncation */
  401. if (value != (size_t)value) {
  402. error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
  403. "exceeding address space");
  404. return;
  405. }
  406. /* Cache should not be larger than guest ram size */
  407. if (value > ram_bytes_total()) {
  408. error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
  409. "exceeds guest ram size ");
  410. return;
  411. }
  412. new_size = xbzrle_cache_resize(value);
  413. if (new_size < 0) {
  414. error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
  415. "is smaller than page size");
  416. return;
  417. }
  418. s->xbzrle_cache_size = new_size;
  419. }
  420. int64_t qmp_query_migrate_cache_size(Error **errp)
  421. {
  422. return migrate_xbzrle_cache_size();
  423. }
  424. void qmp_migrate_set_speed(int64_t value, Error **errp)
  425. {
  426. MigrationState *s;
  427. if (value < 0) {
  428. value = 0;
  429. }
  430. if (value > SIZE_MAX) {
  431. value = SIZE_MAX;
  432. }
  433. s = migrate_get_current();
  434. s->bandwidth_limit = value;
  435. if (s->file) {
  436. qemu_file_set_rate_limit(s->file, s->bandwidth_limit / XFER_LIMIT_RATIO);
  437. }
  438. }
  439. void qmp_migrate_set_downtime(double value, Error **errp)
  440. {
  441. value *= 1e9;
  442. value = MAX(0, MIN(UINT64_MAX, value));
  443. max_downtime = (uint64_t)value;
  444. }
  445. bool migrate_rdma_pin_all(void)
  446. {
  447. MigrationState *s;
  448. s = migrate_get_current();
  449. return s->enabled_capabilities[MIGRATION_CAPABILITY_RDMA_PIN_ALL];
  450. }
  451. bool migrate_auto_converge(void)
  452. {
  453. MigrationState *s;
  454. s = migrate_get_current();
  455. return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
  456. }
  457. bool migrate_zero_blocks(void)
  458. {
  459. MigrationState *s;
  460. s = migrate_get_current();
  461. return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
  462. }
  463. int migrate_use_xbzrle(void)
  464. {
  465. MigrationState *s;
  466. s = migrate_get_current();
  467. return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
  468. }
  469. int64_t migrate_xbzrle_cache_size(void)
  470. {
  471. MigrationState *s;
  472. s = migrate_get_current();
  473. return s->xbzrle_cache_size;
  474. }
  475. /* migration thread support */
  476. static void *migration_thread(void *opaque)
  477. {
  478. MigrationState *s = opaque;
  479. int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
  480. int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
  481. int64_t initial_bytes = 0;
  482. int64_t max_size = 0;
  483. int64_t start_time = initial_time;
  484. bool old_vm_running = false;
  485. qemu_savevm_state_begin(s->file, &s->params);
  486. s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
  487. migrate_set_state(s, MIG_STATE_SETUP, MIG_STATE_ACTIVE);
  488. while (s->state == MIG_STATE_ACTIVE) {
  489. int64_t current_time;
  490. uint64_t pending_size;
  491. if (!qemu_file_rate_limit(s->file)) {
  492. pending_size = qemu_savevm_state_pending(s->file, max_size);
  493. trace_migrate_pending(pending_size, max_size);
  494. if (pending_size && pending_size >= max_size) {
  495. qemu_savevm_state_iterate(s->file);
  496. } else {
  497. int ret;
  498. qemu_mutex_lock_iothread();
  499. start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
  500. qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
  501. old_vm_running = runstate_is_running();
  502. ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
  503. if (ret >= 0) {
  504. qemu_file_set_rate_limit(s->file, INT64_MAX);
  505. qemu_savevm_state_complete(s->file);
  506. }
  507. qemu_mutex_unlock_iothread();
  508. if (ret < 0) {
  509. migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR);
  510. break;
  511. }
  512. if (!qemu_file_get_error(s->file)) {
  513. migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_COMPLETED);
  514. break;
  515. }
  516. }
  517. }
  518. if (qemu_file_get_error(s->file)) {
  519. migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR);
  520. break;
  521. }
  522. current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
  523. if (current_time >= initial_time + BUFFER_DELAY) {
  524. uint64_t transferred_bytes = qemu_ftell(s->file) - initial_bytes;
  525. uint64_t time_spent = current_time - initial_time;
  526. double bandwidth = transferred_bytes / time_spent;
  527. max_size = bandwidth * migrate_max_downtime() / 1000000;
  528. s->mbps = time_spent ? (((double) transferred_bytes * 8.0) /
  529. ((double) time_spent / 1000.0)) / 1000.0 / 1000.0 : -1;
  530. trace_migrate_transferred(transferred_bytes, time_spent,
  531. bandwidth, max_size);
  532. /* if we haven't sent anything, we don't want to recalculate
  533. 10000 is a small enough number for our purposes */
  534. if (s->dirty_bytes_rate && transferred_bytes > 10000) {
  535. s->expected_downtime = s->dirty_bytes_rate / bandwidth;
  536. }
  537. qemu_file_reset_rate_limit(s->file);
  538. initial_time = current_time;
  539. initial_bytes = qemu_ftell(s->file);
  540. }
  541. if (qemu_file_rate_limit(s->file)) {
  542. /* usleep expects microseconds */
  543. g_usleep((initial_time + BUFFER_DELAY - current_time)*1000);
  544. }
  545. }
  546. qemu_mutex_lock_iothread();
  547. if (s->state == MIG_STATE_COMPLETED) {
  548. int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
  549. s->total_time = end_time - s->total_time;
  550. s->downtime = end_time - start_time;
  551. runstate_set(RUN_STATE_POSTMIGRATE);
  552. } else {
  553. if (old_vm_running) {
  554. vm_start();
  555. }
  556. }
  557. qemu_bh_schedule(s->cleanup_bh);
  558. qemu_mutex_unlock_iothread();
  559. return NULL;
  560. }
  561. void migrate_fd_connect(MigrationState *s)
  562. {
  563. s->state = MIG_STATE_SETUP;
  564. trace_migrate_set_state(MIG_STATE_SETUP);
  565. /* This is a best 1st approximation. ns to ms */
  566. s->expected_downtime = max_downtime/1000000;
  567. s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s);
  568. qemu_file_set_rate_limit(s->file,
  569. s->bandwidth_limit / XFER_LIMIT_RATIO);
  570. /* Notify before starting migration thread */
  571. notifier_list_notify(&migration_state_notifiers, s);
  572. qemu_thread_create(&s->thread, "migration", migration_thread, s,
  573. QEMU_THREAD_JOINABLE);
  574. }