migration.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 "migration.h"
  17. #include "monitor.h"
  18. #include "buffered_file.h"
  19. #include "sysemu.h"
  20. #include "block.h"
  21. #include "qemu_socket.h"
  22. #include "block-migration.h"
  23. #include "qmp-commands.h"
  24. //#define DEBUG_MIGRATION
  25. #ifdef DEBUG_MIGRATION
  26. #define DPRINTF(fmt, ...) \
  27. do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
  28. #else
  29. #define DPRINTF(fmt, ...) \
  30. do { } while (0)
  31. #endif
  32. enum {
  33. MIG_STATE_ERROR,
  34. MIG_STATE_SETUP,
  35. MIG_STATE_CANCELLED,
  36. MIG_STATE_ACTIVE,
  37. MIG_STATE_COMPLETED,
  38. };
  39. #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */
  40. static NotifierList migration_state_notifiers =
  41. NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
  42. /* When we add fault tolerance, we could have several
  43. migrations at once. For now we don't need to add
  44. dynamic creation of migration */
  45. static MigrationState *migrate_get_current(void)
  46. {
  47. static MigrationState current_migration = {
  48. .state = MIG_STATE_SETUP,
  49. .bandwidth_limit = MAX_THROTTLE,
  50. };
  51. return &current_migration;
  52. }
  53. int qemu_start_incoming_migration(const char *uri, Error **errp)
  54. {
  55. const char *p;
  56. int ret;
  57. if (strstart(uri, "tcp:", &p))
  58. ret = tcp_start_incoming_migration(p, errp);
  59. #if !defined(WIN32)
  60. else if (strstart(uri, "exec:", &p))
  61. ret = exec_start_incoming_migration(p);
  62. else if (strstart(uri, "unix:", &p))
  63. ret = unix_start_incoming_migration(p);
  64. else if (strstart(uri, "fd:", &p))
  65. ret = fd_start_incoming_migration(p);
  66. #endif
  67. else {
  68. fprintf(stderr, "unknown migration protocol: %s\n", uri);
  69. ret = -EPROTONOSUPPORT;
  70. }
  71. return ret;
  72. }
  73. void process_incoming_migration(QEMUFile *f)
  74. {
  75. if (qemu_loadvm_state(f) < 0) {
  76. fprintf(stderr, "load of migration failed\n");
  77. exit(0);
  78. }
  79. qemu_announce_self();
  80. DPRINTF("successfully loaded vm state\n");
  81. bdrv_clear_incoming_migration_all();
  82. /* Make sure all file formats flush their mutable metadata */
  83. bdrv_invalidate_cache_all();
  84. if (autostart) {
  85. vm_start();
  86. } else {
  87. runstate_set(RUN_STATE_PRELAUNCH);
  88. }
  89. }
  90. /* amount of nanoseconds we are willing to wait for migration to be down.
  91. * the choice of nanoseconds is because it is the maximum resolution that
  92. * get_clock() can achieve. It is an internal measure. All user-visible
  93. * units must be in seconds */
  94. static uint64_t max_downtime = 30000000;
  95. uint64_t migrate_max_downtime(void)
  96. {
  97. return max_downtime;
  98. }
  99. MigrationInfo *qmp_query_migrate(Error **errp)
  100. {
  101. MigrationInfo *info = g_malloc0(sizeof(*info));
  102. MigrationState *s = migrate_get_current();
  103. switch (s->state) {
  104. case MIG_STATE_SETUP:
  105. /* no migration has happened ever */
  106. break;
  107. case MIG_STATE_ACTIVE:
  108. info->has_status = true;
  109. info->status = g_strdup("active");
  110. info->has_ram = true;
  111. info->ram = g_malloc0(sizeof(*info->ram));
  112. info->ram->transferred = ram_bytes_transferred();
  113. info->ram->remaining = ram_bytes_remaining();
  114. info->ram->total = ram_bytes_total();
  115. if (blk_mig_active()) {
  116. info->has_disk = true;
  117. info->disk = g_malloc0(sizeof(*info->disk));
  118. info->disk->transferred = blk_mig_bytes_transferred();
  119. info->disk->remaining = blk_mig_bytes_remaining();
  120. info->disk->total = blk_mig_bytes_total();
  121. }
  122. break;
  123. case MIG_STATE_COMPLETED:
  124. info->has_status = true;
  125. info->status = g_strdup("completed");
  126. break;
  127. case MIG_STATE_ERROR:
  128. info->has_status = true;
  129. info->status = g_strdup("failed");
  130. break;
  131. case MIG_STATE_CANCELLED:
  132. info->has_status = true;
  133. info->status = g_strdup("cancelled");
  134. break;
  135. }
  136. return info;
  137. }
  138. /* shared migration helpers */
  139. static int migrate_fd_cleanup(MigrationState *s)
  140. {
  141. int ret = 0;
  142. qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
  143. if (s->file) {
  144. DPRINTF("closing file\n");
  145. ret = qemu_fclose(s->file);
  146. s->file = NULL;
  147. }
  148. if (s->fd != -1) {
  149. close(s->fd);
  150. s->fd = -1;
  151. }
  152. return ret;
  153. }
  154. void migrate_fd_error(MigrationState *s)
  155. {
  156. DPRINTF("setting error state\n");
  157. s->state = MIG_STATE_ERROR;
  158. notifier_list_notify(&migration_state_notifiers, s);
  159. migrate_fd_cleanup(s);
  160. }
  161. static void migrate_fd_completed(MigrationState *s)
  162. {
  163. DPRINTF("setting completed state\n");
  164. if (migrate_fd_cleanup(s) < 0) {
  165. s->state = MIG_STATE_ERROR;
  166. } else {
  167. s->state = MIG_STATE_COMPLETED;
  168. runstate_set(RUN_STATE_POSTMIGRATE);
  169. }
  170. notifier_list_notify(&migration_state_notifiers, s);
  171. }
  172. static void migrate_fd_put_notify(void *opaque)
  173. {
  174. MigrationState *s = opaque;
  175. qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
  176. qemu_file_put_notify(s->file);
  177. if (s->file && qemu_file_get_error(s->file)) {
  178. migrate_fd_error(s);
  179. }
  180. }
  181. static ssize_t migrate_fd_put_buffer(void *opaque, const void *data,
  182. size_t size)
  183. {
  184. MigrationState *s = opaque;
  185. ssize_t ret;
  186. if (s->state != MIG_STATE_ACTIVE) {
  187. return -EIO;
  188. }
  189. do {
  190. ret = s->write(s, data, size);
  191. } while (ret == -1 && ((s->get_error(s)) == EINTR));
  192. if (ret == -1)
  193. ret = -(s->get_error(s));
  194. if (ret == -EAGAIN) {
  195. qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
  196. }
  197. return ret;
  198. }
  199. static void migrate_fd_put_ready(void *opaque)
  200. {
  201. MigrationState *s = opaque;
  202. int ret;
  203. if (s->state != MIG_STATE_ACTIVE) {
  204. DPRINTF("put_ready returning because of non-active state\n");
  205. return;
  206. }
  207. DPRINTF("iterate\n");
  208. ret = qemu_savevm_state_iterate(s->file);
  209. if (ret < 0) {
  210. migrate_fd_error(s);
  211. } else if (ret == 1) {
  212. int old_vm_running = runstate_is_running();
  213. DPRINTF("done iterating\n");
  214. qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
  215. vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
  216. if (qemu_savevm_state_complete(s->file) < 0) {
  217. migrate_fd_error(s);
  218. } else {
  219. migrate_fd_completed(s);
  220. }
  221. if (s->state != MIG_STATE_COMPLETED) {
  222. if (old_vm_running) {
  223. vm_start();
  224. }
  225. }
  226. }
  227. }
  228. static void migrate_fd_cancel(MigrationState *s)
  229. {
  230. if (s->state != MIG_STATE_ACTIVE)
  231. return;
  232. DPRINTF("cancelling migration\n");
  233. s->state = MIG_STATE_CANCELLED;
  234. notifier_list_notify(&migration_state_notifiers, s);
  235. qemu_savevm_state_cancel(s->file);
  236. migrate_fd_cleanup(s);
  237. }
  238. static void migrate_fd_wait_for_unfreeze(void *opaque)
  239. {
  240. MigrationState *s = opaque;
  241. int ret;
  242. DPRINTF("wait for unfreeze\n");
  243. if (s->state != MIG_STATE_ACTIVE)
  244. return;
  245. do {
  246. fd_set wfds;
  247. FD_ZERO(&wfds);
  248. FD_SET(s->fd, &wfds);
  249. ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
  250. } while (ret == -1 && (s->get_error(s)) == EINTR);
  251. if (ret == -1) {
  252. qemu_file_set_error(s->file, -s->get_error(s));
  253. }
  254. }
  255. static int migrate_fd_close(void *opaque)
  256. {
  257. MigrationState *s = opaque;
  258. qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
  259. return s->close(s);
  260. }
  261. void add_migration_state_change_notifier(Notifier *notify)
  262. {
  263. notifier_list_add(&migration_state_notifiers, notify);
  264. }
  265. void remove_migration_state_change_notifier(Notifier *notify)
  266. {
  267. notifier_remove(notify);
  268. }
  269. bool migration_is_active(MigrationState *s)
  270. {
  271. return s->state == MIG_STATE_ACTIVE;
  272. }
  273. bool migration_has_finished(MigrationState *s)
  274. {
  275. return s->state == MIG_STATE_COMPLETED;
  276. }
  277. bool migration_has_failed(MigrationState *s)
  278. {
  279. return (s->state == MIG_STATE_CANCELLED ||
  280. s->state == MIG_STATE_ERROR);
  281. }
  282. void migrate_fd_connect(MigrationState *s)
  283. {
  284. int ret;
  285. s->state = MIG_STATE_ACTIVE;
  286. s->file = qemu_fopen_ops_buffered(s,
  287. s->bandwidth_limit,
  288. migrate_fd_put_buffer,
  289. migrate_fd_put_ready,
  290. migrate_fd_wait_for_unfreeze,
  291. migrate_fd_close);
  292. DPRINTF("beginning savevm\n");
  293. ret = qemu_savevm_state_begin(s->file, s->blk, s->shared);
  294. if (ret < 0) {
  295. DPRINTF("failed, %d\n", ret);
  296. migrate_fd_error(s);
  297. return;
  298. }
  299. migrate_fd_put_ready(s);
  300. }
  301. static MigrationState *migrate_init(int blk, int inc)
  302. {
  303. MigrationState *s = migrate_get_current();
  304. int64_t bandwidth_limit = s->bandwidth_limit;
  305. memset(s, 0, sizeof(*s));
  306. s->bandwidth_limit = bandwidth_limit;
  307. s->blk = blk;
  308. s->shared = inc;
  309. s->bandwidth_limit = bandwidth_limit;
  310. s->state = MIG_STATE_SETUP;
  311. return s;
  312. }
  313. static GSList *migration_blockers;
  314. void migrate_add_blocker(Error *reason)
  315. {
  316. migration_blockers = g_slist_prepend(migration_blockers, reason);
  317. }
  318. void migrate_del_blocker(Error *reason)
  319. {
  320. migration_blockers = g_slist_remove(migration_blockers, reason);
  321. }
  322. void qmp_migrate(const char *uri, bool has_blk, bool blk,
  323. bool has_inc, bool inc, bool has_detach, bool detach,
  324. Error **errp)
  325. {
  326. MigrationState *s = migrate_get_current();
  327. const char *p;
  328. int ret;
  329. if (s->state == MIG_STATE_ACTIVE) {
  330. error_set(errp, QERR_MIGRATION_ACTIVE);
  331. return;
  332. }
  333. if (qemu_savevm_state_blocked(errp)) {
  334. return;
  335. }
  336. if (migration_blockers) {
  337. *errp = error_copy(migration_blockers->data);
  338. return;
  339. }
  340. s = migrate_init(blk, inc);
  341. if (strstart(uri, "tcp:", &p)) {
  342. ret = tcp_start_outgoing_migration(s, p, errp);
  343. #if !defined(WIN32)
  344. } else if (strstart(uri, "exec:", &p)) {
  345. ret = exec_start_outgoing_migration(s, p);
  346. } else if (strstart(uri, "unix:", &p)) {
  347. ret = unix_start_outgoing_migration(s, p);
  348. } else if (strstart(uri, "fd:", &p)) {
  349. ret = fd_start_outgoing_migration(s, p);
  350. #endif
  351. } else {
  352. error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol");
  353. return;
  354. }
  355. if (ret < 0) {
  356. if (!error_is_set(errp)) {
  357. DPRINTF("migration failed: %s\n", strerror(-ret));
  358. /* FIXME: we should return meaningful errors */
  359. error_set(errp, QERR_UNDEFINED_ERROR);
  360. }
  361. return;
  362. }
  363. notifier_list_notify(&migration_state_notifiers, s);
  364. }
  365. void qmp_migrate_cancel(Error **errp)
  366. {
  367. migrate_fd_cancel(migrate_get_current());
  368. }
  369. void qmp_migrate_set_speed(int64_t value, Error **errp)
  370. {
  371. MigrationState *s;
  372. if (value < 0) {
  373. value = 0;
  374. }
  375. s = migrate_get_current();
  376. s->bandwidth_limit = value;
  377. qemu_file_set_rate_limit(s->file, s->bandwidth_limit);
  378. }
  379. void qmp_migrate_set_downtime(double value, Error **errp)
  380. {
  381. value *= 1e9;
  382. value = MAX(0, MIN(UINT64_MAX, value));
  383. max_downtime = (uint64_t)value;
  384. }