migration.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 "qemu-objects.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. /* Migration speed throttling */
  31. static int64_t max_throttle = (32 << 20);
  32. static MigrationState *current_migration;
  33. static NotifierList migration_state_notifiers =
  34. NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
  35. int qemu_start_incoming_migration(const char *uri)
  36. {
  37. const char *p;
  38. int ret;
  39. if (strstart(uri, "tcp:", &p))
  40. ret = tcp_start_incoming_migration(p);
  41. #if !defined(WIN32)
  42. else if (strstart(uri, "exec:", &p))
  43. ret = exec_start_incoming_migration(p);
  44. else if (strstart(uri, "unix:", &p))
  45. ret = unix_start_incoming_migration(p);
  46. else if (strstart(uri, "fd:", &p))
  47. ret = fd_start_incoming_migration(p);
  48. #endif
  49. else {
  50. fprintf(stderr, "unknown migration protocol: %s\n", uri);
  51. ret = -EPROTONOSUPPORT;
  52. }
  53. return ret;
  54. }
  55. void process_incoming_migration(QEMUFile *f)
  56. {
  57. if (qemu_loadvm_state(f) < 0) {
  58. fprintf(stderr, "load of migration failed\n");
  59. exit(0);
  60. }
  61. qemu_announce_self();
  62. DPRINTF("successfully loaded vm state\n");
  63. incoming_expected = false;
  64. if (autostart)
  65. vm_start();
  66. }
  67. int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
  68. {
  69. MigrationState *s = NULL;
  70. const char *p;
  71. int detach = qdict_get_try_bool(qdict, "detach", 0);
  72. int blk = qdict_get_try_bool(qdict, "blk", 0);
  73. int inc = qdict_get_try_bool(qdict, "inc", 0);
  74. const char *uri = qdict_get_str(qdict, "uri");
  75. if (current_migration &&
  76. current_migration->get_status(current_migration) == MIG_STATE_ACTIVE) {
  77. monitor_printf(mon, "migration already in progress\n");
  78. return -1;
  79. }
  80. if (qemu_savevm_state_blocked(mon)) {
  81. return -1;
  82. }
  83. if (strstart(uri, "tcp:", &p)) {
  84. s = tcp_start_outgoing_migration(mon, p, max_throttle, detach,
  85. blk, inc);
  86. #if !defined(WIN32)
  87. } else if (strstart(uri, "exec:", &p)) {
  88. s = exec_start_outgoing_migration(mon, p, max_throttle, detach,
  89. blk, inc);
  90. } else if (strstart(uri, "unix:", &p)) {
  91. s = unix_start_outgoing_migration(mon, p, max_throttle, detach,
  92. blk, inc);
  93. } else if (strstart(uri, "fd:", &p)) {
  94. s = fd_start_outgoing_migration(mon, p, max_throttle, detach,
  95. blk, inc);
  96. #endif
  97. } else {
  98. monitor_printf(mon, "unknown migration protocol: %s\n", uri);
  99. return -1;
  100. }
  101. if (s == NULL) {
  102. monitor_printf(mon, "migration failed\n");
  103. return -1;
  104. }
  105. if (current_migration) {
  106. current_migration->release(current_migration);
  107. }
  108. current_migration = s;
  109. notifier_list_notify(&migration_state_notifiers, NULL);
  110. return 0;
  111. }
  112. int do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
  113. {
  114. MigrationState *s = current_migration;
  115. if (s)
  116. s->cancel(s);
  117. return 0;
  118. }
  119. int do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
  120. {
  121. int64_t d;
  122. FdMigrationState *s;
  123. d = qdict_get_int(qdict, "value");
  124. if (d < 0) {
  125. d = 0;
  126. }
  127. max_throttle = d;
  128. s = migrate_to_fms(current_migration);
  129. if (s && s->file) {
  130. qemu_file_set_rate_limit(s->file, max_throttle);
  131. }
  132. return 0;
  133. }
  134. /* amount of nanoseconds we are willing to wait for migration to be down.
  135. * the choice of nanoseconds is because it is the maximum resolution that
  136. * get_clock() can achieve. It is an internal measure. All user-visible
  137. * units must be in seconds */
  138. static uint64_t max_downtime = 30000000;
  139. uint64_t migrate_max_downtime(void)
  140. {
  141. return max_downtime;
  142. }
  143. int do_migrate_set_downtime(Monitor *mon, const QDict *qdict,
  144. QObject **ret_data)
  145. {
  146. double d;
  147. d = qdict_get_double(qdict, "value") * 1e9;
  148. d = MAX(0, MIN(UINT64_MAX, d));
  149. max_downtime = (uint64_t)d;
  150. return 0;
  151. }
  152. static void migrate_print_status(Monitor *mon, const char *name,
  153. const QDict *status_dict)
  154. {
  155. QDict *qdict;
  156. qdict = qobject_to_qdict(qdict_get(status_dict, name));
  157. monitor_printf(mon, "transferred %s: %" PRIu64 " kbytes\n", name,
  158. qdict_get_int(qdict, "transferred") >> 10);
  159. monitor_printf(mon, "remaining %s: %" PRIu64 " kbytes\n", name,
  160. qdict_get_int(qdict, "remaining") >> 10);
  161. monitor_printf(mon, "total %s: %" PRIu64 " kbytes\n", name,
  162. qdict_get_int(qdict, "total") >> 10);
  163. }
  164. void do_info_migrate_print(Monitor *mon, const QObject *data)
  165. {
  166. QDict *qdict;
  167. qdict = qobject_to_qdict(data);
  168. monitor_printf(mon, "Migration status: %s\n",
  169. qdict_get_str(qdict, "status"));
  170. if (qdict_haskey(qdict, "ram")) {
  171. migrate_print_status(mon, "ram", qdict);
  172. }
  173. if (qdict_haskey(qdict, "disk")) {
  174. migrate_print_status(mon, "disk", qdict);
  175. }
  176. }
  177. static void migrate_put_status(QDict *qdict, const char *name,
  178. uint64_t trans, uint64_t rem, uint64_t total)
  179. {
  180. QObject *obj;
  181. obj = qobject_from_jsonf("{ 'transferred': %" PRId64 ", "
  182. "'remaining': %" PRId64 ", "
  183. "'total': %" PRId64 " }", trans, rem, total);
  184. qdict_put_obj(qdict, name, obj);
  185. }
  186. void do_info_migrate(Monitor *mon, QObject **ret_data)
  187. {
  188. QDict *qdict;
  189. MigrationState *s = current_migration;
  190. if (s) {
  191. switch (s->get_status(s)) {
  192. case MIG_STATE_ACTIVE:
  193. qdict = qdict_new();
  194. qdict_put(qdict, "status", qstring_from_str("active"));
  195. migrate_put_status(qdict, "ram", ram_bytes_transferred(),
  196. ram_bytes_remaining(), ram_bytes_total());
  197. if (blk_mig_active()) {
  198. migrate_put_status(qdict, "disk", blk_mig_bytes_transferred(),
  199. blk_mig_bytes_remaining(),
  200. blk_mig_bytes_total());
  201. }
  202. *ret_data = QOBJECT(qdict);
  203. break;
  204. case MIG_STATE_COMPLETED:
  205. *ret_data = qobject_from_jsonf("{ 'status': 'completed' }");
  206. break;
  207. case MIG_STATE_ERROR:
  208. *ret_data = qobject_from_jsonf("{ 'status': 'failed' }");
  209. break;
  210. case MIG_STATE_CANCELLED:
  211. *ret_data = qobject_from_jsonf("{ 'status': 'cancelled' }");
  212. break;
  213. }
  214. }
  215. }
  216. /* shared migration helpers */
  217. void migrate_fd_monitor_suspend(FdMigrationState *s, Monitor *mon)
  218. {
  219. s->mon = mon;
  220. if (monitor_suspend(mon) == 0) {
  221. DPRINTF("suspending monitor\n");
  222. } else {
  223. monitor_printf(mon, "terminal does not allow synchronous "
  224. "migration, continuing detached\n");
  225. }
  226. }
  227. void migrate_fd_error(FdMigrationState *s)
  228. {
  229. DPRINTF("setting error state\n");
  230. s->state = MIG_STATE_ERROR;
  231. notifier_list_notify(&migration_state_notifiers, NULL);
  232. migrate_fd_cleanup(s);
  233. }
  234. int migrate_fd_cleanup(FdMigrationState *s)
  235. {
  236. int ret = 0;
  237. qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
  238. if (s->file) {
  239. DPRINTF("closing file\n");
  240. if (qemu_fclose(s->file) != 0) {
  241. ret = -1;
  242. }
  243. s->file = NULL;
  244. }
  245. if (s->fd != -1)
  246. close(s->fd);
  247. /* Don't resume monitor until we've flushed all of the buffers */
  248. if (s->mon) {
  249. monitor_resume(s->mon);
  250. }
  251. s->fd = -1;
  252. return ret;
  253. }
  254. void migrate_fd_put_notify(void *opaque)
  255. {
  256. FdMigrationState *s = opaque;
  257. qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
  258. qemu_file_put_notify(s->file);
  259. }
  260. ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size)
  261. {
  262. FdMigrationState *s = opaque;
  263. ssize_t ret;
  264. do {
  265. ret = s->write(s, data, size);
  266. } while (ret == -1 && ((s->get_error(s)) == EINTR));
  267. if (ret == -1)
  268. ret = -(s->get_error(s));
  269. if (ret == -EAGAIN) {
  270. qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
  271. } else if (ret < 0) {
  272. if (s->mon) {
  273. monitor_resume(s->mon);
  274. }
  275. s->state = MIG_STATE_ERROR;
  276. notifier_list_notify(&migration_state_notifiers, NULL);
  277. }
  278. return ret;
  279. }
  280. void migrate_fd_connect(FdMigrationState *s)
  281. {
  282. int ret;
  283. s->file = qemu_fopen_ops_buffered(s,
  284. s->bandwidth_limit,
  285. migrate_fd_put_buffer,
  286. migrate_fd_put_ready,
  287. migrate_fd_wait_for_unfreeze,
  288. migrate_fd_close);
  289. DPRINTF("beginning savevm\n");
  290. ret = qemu_savevm_state_begin(s->mon, s->file, s->mig_state.blk,
  291. s->mig_state.shared);
  292. if (ret < 0) {
  293. DPRINTF("failed, %d\n", ret);
  294. migrate_fd_error(s);
  295. return;
  296. }
  297. migrate_fd_put_ready(s);
  298. }
  299. void migrate_fd_put_ready(void *opaque)
  300. {
  301. FdMigrationState *s = opaque;
  302. if (s->state != MIG_STATE_ACTIVE) {
  303. DPRINTF("put_ready returning because of non-active state\n");
  304. return;
  305. }
  306. DPRINTF("iterate\n");
  307. if (qemu_savevm_state_iterate(s->mon, s->file) == 1) {
  308. int state;
  309. int old_vm_running = vm_running;
  310. DPRINTF("done iterating\n");
  311. vm_stop(VMSTOP_MIGRATE);
  312. if ((qemu_savevm_state_complete(s->mon, s->file)) < 0) {
  313. if (old_vm_running) {
  314. vm_start();
  315. }
  316. state = MIG_STATE_ERROR;
  317. } else {
  318. state = MIG_STATE_COMPLETED;
  319. }
  320. if (migrate_fd_cleanup(s) < 0) {
  321. if (old_vm_running) {
  322. vm_start();
  323. }
  324. state = MIG_STATE_ERROR;
  325. }
  326. s->state = state;
  327. notifier_list_notify(&migration_state_notifiers, NULL);
  328. }
  329. }
  330. int migrate_fd_get_status(MigrationState *mig_state)
  331. {
  332. FdMigrationState *s = migrate_to_fms(mig_state);
  333. return s->state;
  334. }
  335. void migrate_fd_cancel(MigrationState *mig_state)
  336. {
  337. FdMigrationState *s = migrate_to_fms(mig_state);
  338. if (s->state != MIG_STATE_ACTIVE)
  339. return;
  340. DPRINTF("cancelling migration\n");
  341. s->state = MIG_STATE_CANCELLED;
  342. notifier_list_notify(&migration_state_notifiers, NULL);
  343. qemu_savevm_state_cancel(s->mon, s->file);
  344. migrate_fd_cleanup(s);
  345. }
  346. void migrate_fd_release(MigrationState *mig_state)
  347. {
  348. FdMigrationState *s = migrate_to_fms(mig_state);
  349. DPRINTF("releasing state\n");
  350. if (s->state == MIG_STATE_ACTIVE) {
  351. s->state = MIG_STATE_CANCELLED;
  352. notifier_list_notify(&migration_state_notifiers, NULL);
  353. migrate_fd_cleanup(s);
  354. }
  355. qemu_free(s);
  356. }
  357. void migrate_fd_wait_for_unfreeze(void *opaque)
  358. {
  359. FdMigrationState *s = opaque;
  360. int ret;
  361. DPRINTF("wait for unfreeze\n");
  362. if (s->state != MIG_STATE_ACTIVE)
  363. return;
  364. do {
  365. fd_set wfds;
  366. FD_ZERO(&wfds);
  367. FD_SET(s->fd, &wfds);
  368. ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
  369. } while (ret == -1 && (s->get_error(s)) == EINTR);
  370. }
  371. int migrate_fd_close(void *opaque)
  372. {
  373. FdMigrationState *s = opaque;
  374. qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
  375. return s->close(s);
  376. }
  377. void add_migration_state_change_notifier(Notifier *notify)
  378. {
  379. notifier_list_add(&migration_state_notifiers, notify);
  380. }
  381. void remove_migration_state_change_notifier(Notifier *notify)
  382. {
  383. notifier_list_remove(&migration_state_notifiers, notify);
  384. }
  385. int get_migration_state(void)
  386. {
  387. if (current_migration) {
  388. return migrate_fd_get_status(current_migration);
  389. } else {
  390. return MIG_STATE_ERROR;
  391. }
  392. }