migration.c 12 KB

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