colo.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. /*
  2. * COarse-grain LOck-stepping Virtual Machines for Non-stop Service (COLO)
  3. * (a.k.a. Fault Tolerance or Continuous Replication)
  4. *
  5. * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
  6. * Copyright (c) 2016 FUJITSU LIMITED
  7. * Copyright (c) 2016 Intel Corporation
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or
  10. * later. See the COPYING file in the top-level directory.
  11. */
  12. #include "qemu/osdep.h"
  13. #include "sysemu/sysemu.h"
  14. #include "qapi/error.h"
  15. #include "qapi/qapi-commands-migration.h"
  16. #include "qemu-file-channel.h"
  17. #include "migration.h"
  18. #include "qemu-file.h"
  19. #include "savevm.h"
  20. #include "migration/colo.h"
  21. #include "block.h"
  22. #include "io/channel-buffer.h"
  23. #include "trace.h"
  24. #include "qemu/error-report.h"
  25. #include "qemu/main-loop.h"
  26. #include "qemu/rcu.h"
  27. #include "migration/failover.h"
  28. #ifdef CONFIG_REPLICATION
  29. #include "replication.h"
  30. #endif
  31. #include "net/colo-compare.h"
  32. #include "net/colo.h"
  33. #include "block/block.h"
  34. #include "qapi/qapi-events-migration.h"
  35. #include "qapi/qmp/qerror.h"
  36. #include "sysemu/cpus.h"
  37. #include "sysemu/runstate.h"
  38. #include "net/filter.h"
  39. static bool vmstate_loading;
  40. static Notifier packets_compare_notifier;
  41. /* User need to know colo mode after COLO failover */
  42. static COLOMode last_colo_mode;
  43. #define COLO_BUFFER_BASE_SIZE (4 * 1024 * 1024)
  44. bool migration_in_colo_state(void)
  45. {
  46. MigrationState *s = migrate_get_current();
  47. return (s->state == MIGRATION_STATUS_COLO);
  48. }
  49. bool migration_incoming_in_colo_state(void)
  50. {
  51. MigrationIncomingState *mis = migration_incoming_get_current();
  52. return mis && (mis->state == MIGRATION_STATUS_COLO);
  53. }
  54. static bool colo_runstate_is_stopped(void)
  55. {
  56. return runstate_check(RUN_STATE_COLO) || !runstate_is_running();
  57. }
  58. static void secondary_vm_do_failover(void)
  59. {
  60. /* COLO needs enable block-replication */
  61. #ifdef CONFIG_REPLICATION
  62. int old_state;
  63. MigrationIncomingState *mis = migration_incoming_get_current();
  64. Error *local_err = NULL;
  65. /* Can not do failover during the process of VM's loading VMstate, Or
  66. * it will break the secondary VM.
  67. */
  68. if (vmstate_loading) {
  69. old_state = failover_set_state(FAILOVER_STATUS_ACTIVE,
  70. FAILOVER_STATUS_RELAUNCH);
  71. if (old_state != FAILOVER_STATUS_ACTIVE) {
  72. error_report("Unknown error while do failover for secondary VM,"
  73. "old_state: %s", FailoverStatus_str(old_state));
  74. }
  75. return;
  76. }
  77. migrate_set_state(&mis->state, MIGRATION_STATUS_COLO,
  78. MIGRATION_STATUS_COMPLETED);
  79. replication_stop_all(true, &local_err);
  80. if (local_err) {
  81. error_report_err(local_err);
  82. }
  83. /* Notify all filters of all NIC to do checkpoint */
  84. colo_notify_filters_event(COLO_EVENT_FAILOVER, &local_err);
  85. if (local_err) {
  86. error_report_err(local_err);
  87. }
  88. if (!autostart) {
  89. error_report("\"-S\" qemu option will be ignored in secondary side");
  90. /* recover runstate to normal migration finish state */
  91. autostart = true;
  92. }
  93. /*
  94. * Make sure COLO incoming thread not block in recv or send,
  95. * If mis->from_src_file and mis->to_src_file use the same fd,
  96. * The second shutdown() will return -1, we ignore this value,
  97. * It is harmless.
  98. */
  99. if (mis->from_src_file) {
  100. qemu_file_shutdown(mis->from_src_file);
  101. }
  102. if (mis->to_src_file) {
  103. qemu_file_shutdown(mis->to_src_file);
  104. }
  105. old_state = failover_set_state(FAILOVER_STATUS_ACTIVE,
  106. FAILOVER_STATUS_COMPLETED);
  107. if (old_state != FAILOVER_STATUS_ACTIVE) {
  108. error_report("Incorrect state (%s) while doing failover for "
  109. "secondary VM", FailoverStatus_str(old_state));
  110. return;
  111. }
  112. /* Notify COLO incoming thread that failover work is finished */
  113. qemu_sem_post(&mis->colo_incoming_sem);
  114. /* For Secondary VM, jump to incoming co */
  115. if (mis->migration_incoming_co) {
  116. qemu_coroutine_enter(mis->migration_incoming_co);
  117. }
  118. #else
  119. abort();
  120. #endif
  121. }
  122. static void primary_vm_do_failover(void)
  123. {
  124. #ifdef CONFIG_REPLICATION
  125. MigrationState *s = migrate_get_current();
  126. int old_state;
  127. Error *local_err = NULL;
  128. migrate_set_state(&s->state, MIGRATION_STATUS_COLO,
  129. MIGRATION_STATUS_COMPLETED);
  130. /*
  131. * kick COLO thread which might wait at
  132. * qemu_sem_wait(&s->colo_checkpoint_sem).
  133. */
  134. colo_checkpoint_notify(migrate_get_current());
  135. /*
  136. * Wake up COLO thread which may blocked in recv() or send(),
  137. * The s->rp_state.from_dst_file and s->to_dst_file may use the
  138. * same fd, but we still shutdown the fd for twice, it is harmless.
  139. */
  140. if (s->to_dst_file) {
  141. qemu_file_shutdown(s->to_dst_file);
  142. }
  143. if (s->rp_state.from_dst_file) {
  144. qemu_file_shutdown(s->rp_state.from_dst_file);
  145. }
  146. old_state = failover_set_state(FAILOVER_STATUS_ACTIVE,
  147. FAILOVER_STATUS_COMPLETED);
  148. if (old_state != FAILOVER_STATUS_ACTIVE) {
  149. error_report("Incorrect state (%s) while doing failover for Primary VM",
  150. FailoverStatus_str(old_state));
  151. return;
  152. }
  153. replication_stop_all(true, &local_err);
  154. if (local_err) {
  155. error_report_err(local_err);
  156. local_err = NULL;
  157. }
  158. /* Notify COLO thread that failover work is finished */
  159. qemu_sem_post(&s->colo_exit_sem);
  160. #else
  161. abort();
  162. #endif
  163. }
  164. COLOMode get_colo_mode(void)
  165. {
  166. if (migration_in_colo_state()) {
  167. return COLO_MODE_PRIMARY;
  168. } else if (migration_incoming_in_colo_state()) {
  169. return COLO_MODE_SECONDARY;
  170. } else {
  171. return COLO_MODE_NONE;
  172. }
  173. }
  174. void colo_do_failover(void)
  175. {
  176. /* Make sure VM stopped while failover happened. */
  177. if (!colo_runstate_is_stopped()) {
  178. vm_stop_force_state(RUN_STATE_COLO);
  179. }
  180. switch (get_colo_mode()) {
  181. case COLO_MODE_PRIMARY:
  182. primary_vm_do_failover();
  183. break;
  184. case COLO_MODE_SECONDARY:
  185. secondary_vm_do_failover();
  186. break;
  187. default:
  188. error_report("colo_do_failover failed because the colo mode"
  189. " could not be obtained");
  190. }
  191. }
  192. #ifdef CONFIG_REPLICATION
  193. void qmp_xen_set_replication(bool enable, bool primary,
  194. bool has_failover, bool failover,
  195. Error **errp)
  196. {
  197. ReplicationMode mode = primary ?
  198. REPLICATION_MODE_PRIMARY :
  199. REPLICATION_MODE_SECONDARY;
  200. if (has_failover && enable) {
  201. error_setg(errp, "Parameter 'failover' is only for"
  202. " stopping replication");
  203. return;
  204. }
  205. if (enable) {
  206. replication_start_all(mode, errp);
  207. } else {
  208. if (!has_failover) {
  209. failover = NULL;
  210. }
  211. replication_stop_all(failover, failover ? NULL : errp);
  212. }
  213. }
  214. ReplicationStatus *qmp_query_xen_replication_status(Error **errp)
  215. {
  216. Error *err = NULL;
  217. ReplicationStatus *s = g_new0(ReplicationStatus, 1);
  218. replication_get_error_all(&err);
  219. if (err) {
  220. s->error = true;
  221. s->has_desc = true;
  222. s->desc = g_strdup(error_get_pretty(err));
  223. } else {
  224. s->error = false;
  225. }
  226. error_free(err);
  227. return s;
  228. }
  229. void qmp_xen_colo_do_checkpoint(Error **errp)
  230. {
  231. replication_do_checkpoint_all(errp);
  232. /* Notify all filters of all NIC to do checkpoint */
  233. colo_notify_filters_event(COLO_EVENT_CHECKPOINT, errp);
  234. }
  235. #endif
  236. COLOStatus *qmp_query_colo_status(Error **errp)
  237. {
  238. COLOStatus *s = g_new0(COLOStatus, 1);
  239. s->mode = get_colo_mode();
  240. s->last_mode = last_colo_mode;
  241. switch (failover_get_state()) {
  242. case FAILOVER_STATUS_NONE:
  243. s->reason = COLO_EXIT_REASON_NONE;
  244. break;
  245. case FAILOVER_STATUS_COMPLETED:
  246. s->reason = COLO_EXIT_REASON_REQUEST;
  247. break;
  248. default:
  249. if (migration_in_colo_state()) {
  250. s->reason = COLO_EXIT_REASON_PROCESSING;
  251. } else {
  252. s->reason = COLO_EXIT_REASON_ERROR;
  253. }
  254. }
  255. return s;
  256. }
  257. static void colo_send_message(QEMUFile *f, COLOMessage msg,
  258. Error **errp)
  259. {
  260. int ret;
  261. if (msg >= COLO_MESSAGE__MAX) {
  262. error_setg(errp, "%s: Invalid message", __func__);
  263. return;
  264. }
  265. qemu_put_be32(f, msg);
  266. qemu_fflush(f);
  267. ret = qemu_file_get_error(f);
  268. if (ret < 0) {
  269. error_setg_errno(errp, -ret, "Can't send COLO message");
  270. }
  271. trace_colo_send_message(COLOMessage_str(msg));
  272. }
  273. static void colo_send_message_value(QEMUFile *f, COLOMessage msg,
  274. uint64_t value, Error **errp)
  275. {
  276. Error *local_err = NULL;
  277. int ret;
  278. colo_send_message(f, msg, &local_err);
  279. if (local_err) {
  280. error_propagate(errp, local_err);
  281. return;
  282. }
  283. qemu_put_be64(f, value);
  284. qemu_fflush(f);
  285. ret = qemu_file_get_error(f);
  286. if (ret < 0) {
  287. error_setg_errno(errp, -ret, "Failed to send value for message:%s",
  288. COLOMessage_str(msg));
  289. }
  290. }
  291. static COLOMessage colo_receive_message(QEMUFile *f, Error **errp)
  292. {
  293. COLOMessage msg;
  294. int ret;
  295. msg = qemu_get_be32(f);
  296. ret = qemu_file_get_error(f);
  297. if (ret < 0) {
  298. error_setg_errno(errp, -ret, "Can't receive COLO message");
  299. return msg;
  300. }
  301. if (msg >= COLO_MESSAGE__MAX) {
  302. error_setg(errp, "%s: Invalid message", __func__);
  303. return msg;
  304. }
  305. trace_colo_receive_message(COLOMessage_str(msg));
  306. return msg;
  307. }
  308. static void colo_receive_check_message(QEMUFile *f, COLOMessage expect_msg,
  309. Error **errp)
  310. {
  311. COLOMessage msg;
  312. Error *local_err = NULL;
  313. msg = colo_receive_message(f, &local_err);
  314. if (local_err) {
  315. error_propagate(errp, local_err);
  316. return;
  317. }
  318. if (msg != expect_msg) {
  319. error_setg(errp, "Unexpected COLO message %d, expected %d",
  320. msg, expect_msg);
  321. }
  322. }
  323. static uint64_t colo_receive_message_value(QEMUFile *f, uint32_t expect_msg,
  324. Error **errp)
  325. {
  326. Error *local_err = NULL;
  327. uint64_t value;
  328. int ret;
  329. colo_receive_check_message(f, expect_msg, &local_err);
  330. if (local_err) {
  331. error_propagate(errp, local_err);
  332. return 0;
  333. }
  334. value = qemu_get_be64(f);
  335. ret = qemu_file_get_error(f);
  336. if (ret < 0) {
  337. error_setg_errno(errp, -ret, "Failed to get value for COLO message: %s",
  338. COLOMessage_str(expect_msg));
  339. }
  340. return value;
  341. }
  342. static int colo_do_checkpoint_transaction(MigrationState *s,
  343. QIOChannelBuffer *bioc,
  344. QEMUFile *fb)
  345. {
  346. Error *local_err = NULL;
  347. int ret = -1;
  348. colo_send_message(s->to_dst_file, COLO_MESSAGE_CHECKPOINT_REQUEST,
  349. &local_err);
  350. if (local_err) {
  351. goto out;
  352. }
  353. colo_receive_check_message(s->rp_state.from_dst_file,
  354. COLO_MESSAGE_CHECKPOINT_REPLY, &local_err);
  355. if (local_err) {
  356. goto out;
  357. }
  358. /* Reset channel-buffer directly */
  359. qio_channel_io_seek(QIO_CHANNEL(bioc), 0, 0, NULL);
  360. bioc->usage = 0;
  361. qemu_mutex_lock_iothread();
  362. if (failover_get_state() != FAILOVER_STATUS_NONE) {
  363. qemu_mutex_unlock_iothread();
  364. goto out;
  365. }
  366. vm_stop_force_state(RUN_STATE_COLO);
  367. qemu_mutex_unlock_iothread();
  368. trace_colo_vm_state_change("run", "stop");
  369. /*
  370. * Failover request bh could be called after vm_stop_force_state(),
  371. * So we need check failover_request_is_active() again.
  372. */
  373. if (failover_get_state() != FAILOVER_STATUS_NONE) {
  374. goto out;
  375. }
  376. colo_notify_compares_event(NULL, COLO_EVENT_CHECKPOINT, &local_err);
  377. if (local_err) {
  378. goto out;
  379. }
  380. /* Disable block migration */
  381. migrate_set_block_enabled(false, &local_err);
  382. qemu_mutex_lock_iothread();
  383. #ifdef CONFIG_REPLICATION
  384. replication_do_checkpoint_all(&local_err);
  385. if (local_err) {
  386. qemu_mutex_unlock_iothread();
  387. goto out;
  388. }
  389. #else
  390. abort();
  391. #endif
  392. colo_send_message(s->to_dst_file, COLO_MESSAGE_VMSTATE_SEND, &local_err);
  393. if (local_err) {
  394. qemu_mutex_unlock_iothread();
  395. goto out;
  396. }
  397. /* Note: device state is saved into buffer */
  398. ret = qemu_save_device_state(fb);
  399. qemu_mutex_unlock_iothread();
  400. if (ret < 0) {
  401. goto out;
  402. }
  403. /*
  404. * Only save VM's live state, which not including device state.
  405. * TODO: We may need a timeout mechanism to prevent COLO process
  406. * to be blocked here.
  407. */
  408. qemu_savevm_live_state(s->to_dst_file);
  409. qemu_fflush(fb);
  410. /*
  411. * We need the size of the VMstate data in Secondary side,
  412. * With which we can decide how much data should be read.
  413. */
  414. colo_send_message_value(s->to_dst_file, COLO_MESSAGE_VMSTATE_SIZE,
  415. bioc->usage, &local_err);
  416. if (local_err) {
  417. goto out;
  418. }
  419. qemu_put_buffer(s->to_dst_file, bioc->data, bioc->usage);
  420. qemu_fflush(s->to_dst_file);
  421. ret = qemu_file_get_error(s->to_dst_file);
  422. if (ret < 0) {
  423. goto out;
  424. }
  425. colo_receive_check_message(s->rp_state.from_dst_file,
  426. COLO_MESSAGE_VMSTATE_RECEIVED, &local_err);
  427. if (local_err) {
  428. goto out;
  429. }
  430. colo_receive_check_message(s->rp_state.from_dst_file,
  431. COLO_MESSAGE_VMSTATE_LOADED, &local_err);
  432. if (local_err) {
  433. goto out;
  434. }
  435. ret = 0;
  436. qemu_mutex_lock_iothread();
  437. vm_start();
  438. qemu_mutex_unlock_iothread();
  439. trace_colo_vm_state_change("stop", "run");
  440. out:
  441. if (local_err) {
  442. error_report_err(local_err);
  443. }
  444. return ret;
  445. }
  446. static void colo_compare_notify_checkpoint(Notifier *notifier, void *data)
  447. {
  448. colo_checkpoint_notify(data);
  449. }
  450. static void colo_process_checkpoint(MigrationState *s)
  451. {
  452. QIOChannelBuffer *bioc;
  453. QEMUFile *fb = NULL;
  454. int64_t current_time = qemu_clock_get_ms(QEMU_CLOCK_HOST);
  455. Error *local_err = NULL;
  456. int ret;
  457. last_colo_mode = get_colo_mode();
  458. if (last_colo_mode != COLO_MODE_PRIMARY) {
  459. error_report("COLO mode must be COLO_MODE_PRIMARY");
  460. return;
  461. }
  462. failover_init_state();
  463. s->rp_state.from_dst_file = qemu_file_get_return_path(s->to_dst_file);
  464. if (!s->rp_state.from_dst_file) {
  465. error_report("Open QEMUFile from_dst_file failed");
  466. goto out;
  467. }
  468. packets_compare_notifier.notify = colo_compare_notify_checkpoint;
  469. colo_compare_register_notifier(&packets_compare_notifier);
  470. /*
  471. * Wait for Secondary finish loading VM states and enter COLO
  472. * restore.
  473. */
  474. colo_receive_check_message(s->rp_state.from_dst_file,
  475. COLO_MESSAGE_CHECKPOINT_READY, &local_err);
  476. if (local_err) {
  477. goto out;
  478. }
  479. bioc = qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE);
  480. fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
  481. object_unref(OBJECT(bioc));
  482. qemu_mutex_lock_iothread();
  483. #ifdef CONFIG_REPLICATION
  484. replication_start_all(REPLICATION_MODE_PRIMARY, &local_err);
  485. if (local_err) {
  486. qemu_mutex_unlock_iothread();
  487. goto out;
  488. }
  489. #else
  490. abort();
  491. #endif
  492. vm_start();
  493. qemu_mutex_unlock_iothread();
  494. trace_colo_vm_state_change("stop", "run");
  495. timer_mod(s->colo_delay_timer,
  496. current_time + s->parameters.x_checkpoint_delay);
  497. while (s->state == MIGRATION_STATUS_COLO) {
  498. if (failover_get_state() != FAILOVER_STATUS_NONE) {
  499. error_report("failover request");
  500. goto out;
  501. }
  502. qemu_sem_wait(&s->colo_checkpoint_sem);
  503. if (s->state != MIGRATION_STATUS_COLO) {
  504. goto out;
  505. }
  506. ret = colo_do_checkpoint_transaction(s, bioc, fb);
  507. if (ret < 0) {
  508. goto out;
  509. }
  510. }
  511. out:
  512. /* Throw the unreported error message after exited from loop */
  513. if (local_err) {
  514. error_report_err(local_err);
  515. }
  516. if (fb) {
  517. qemu_fclose(fb);
  518. }
  519. /*
  520. * There are only two reasons we can get here, some error happened
  521. * or the user triggered failover.
  522. */
  523. switch (failover_get_state()) {
  524. case FAILOVER_STATUS_COMPLETED:
  525. qapi_event_send_colo_exit(COLO_MODE_PRIMARY,
  526. COLO_EXIT_REASON_REQUEST);
  527. break;
  528. default:
  529. qapi_event_send_colo_exit(COLO_MODE_PRIMARY,
  530. COLO_EXIT_REASON_ERROR);
  531. }
  532. /* Hope this not to be too long to wait here */
  533. qemu_sem_wait(&s->colo_exit_sem);
  534. qemu_sem_destroy(&s->colo_exit_sem);
  535. /*
  536. * It is safe to unregister notifier after failover finished.
  537. * Besides, colo_delay_timer and colo_checkpoint_sem can't be
  538. * released befor unregister notifier, or there will be use-after-free
  539. * error.
  540. */
  541. colo_compare_unregister_notifier(&packets_compare_notifier);
  542. timer_del(s->colo_delay_timer);
  543. timer_free(s->colo_delay_timer);
  544. qemu_sem_destroy(&s->colo_checkpoint_sem);
  545. /*
  546. * Must be called after failover BH is completed,
  547. * Or the failover BH may shutdown the wrong fd that
  548. * re-used by other threads after we release here.
  549. */
  550. if (s->rp_state.from_dst_file) {
  551. qemu_fclose(s->rp_state.from_dst_file);
  552. }
  553. }
  554. void colo_checkpoint_notify(void *opaque)
  555. {
  556. MigrationState *s = opaque;
  557. int64_t next_notify_time;
  558. qemu_sem_post(&s->colo_checkpoint_sem);
  559. s->colo_checkpoint_time = qemu_clock_get_ms(QEMU_CLOCK_HOST);
  560. next_notify_time = s->colo_checkpoint_time +
  561. s->parameters.x_checkpoint_delay;
  562. timer_mod(s->colo_delay_timer, next_notify_time);
  563. }
  564. void migrate_start_colo_process(MigrationState *s)
  565. {
  566. qemu_mutex_unlock_iothread();
  567. qemu_sem_init(&s->colo_checkpoint_sem, 0);
  568. s->colo_delay_timer = timer_new_ms(QEMU_CLOCK_HOST,
  569. colo_checkpoint_notify, s);
  570. qemu_sem_init(&s->colo_exit_sem, 0);
  571. migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE,
  572. MIGRATION_STATUS_COLO);
  573. colo_process_checkpoint(s);
  574. qemu_mutex_lock_iothread();
  575. }
  576. static void colo_wait_handle_message(QEMUFile *f, int *checkpoint_request,
  577. Error **errp)
  578. {
  579. COLOMessage msg;
  580. Error *local_err = NULL;
  581. msg = colo_receive_message(f, &local_err);
  582. if (local_err) {
  583. error_propagate(errp, local_err);
  584. return;
  585. }
  586. switch (msg) {
  587. case COLO_MESSAGE_CHECKPOINT_REQUEST:
  588. *checkpoint_request = 1;
  589. break;
  590. default:
  591. *checkpoint_request = 0;
  592. error_setg(errp, "Got unknown COLO message: %d", msg);
  593. break;
  594. }
  595. }
  596. void *colo_process_incoming_thread(void *opaque)
  597. {
  598. MigrationIncomingState *mis = opaque;
  599. QEMUFile *fb = NULL;
  600. QIOChannelBuffer *bioc = NULL; /* Cache incoming device state */
  601. uint64_t total_size;
  602. uint64_t value;
  603. Error *local_err = NULL;
  604. int ret;
  605. rcu_register_thread();
  606. qemu_sem_init(&mis->colo_incoming_sem, 0);
  607. migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
  608. MIGRATION_STATUS_COLO);
  609. last_colo_mode = get_colo_mode();
  610. if (last_colo_mode != COLO_MODE_SECONDARY) {
  611. error_report("COLO mode must be COLO_MODE_SECONDARY");
  612. return NULL;
  613. }
  614. failover_init_state();
  615. mis->to_src_file = qemu_file_get_return_path(mis->from_src_file);
  616. if (!mis->to_src_file) {
  617. error_report("COLO incoming thread: Open QEMUFile to_src_file failed");
  618. goto out;
  619. }
  620. /*
  621. * Note: the communication between Primary side and Secondary side
  622. * should be sequential, we set the fd to unblocked in migration incoming
  623. * coroutine, and here we are in the COLO incoming thread, so it is ok to
  624. * set the fd back to blocked.
  625. */
  626. qemu_file_set_blocking(mis->from_src_file, true);
  627. bioc = qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE);
  628. fb = qemu_fopen_channel_input(QIO_CHANNEL(bioc));
  629. object_unref(OBJECT(bioc));
  630. qemu_mutex_lock_iothread();
  631. #ifdef CONFIG_REPLICATION
  632. replication_start_all(REPLICATION_MODE_SECONDARY, &local_err);
  633. if (local_err) {
  634. qemu_mutex_unlock_iothread();
  635. goto out;
  636. }
  637. #else
  638. abort();
  639. #endif
  640. vm_start();
  641. trace_colo_vm_state_change("stop", "run");
  642. qemu_mutex_unlock_iothread();
  643. colo_send_message(mis->to_src_file, COLO_MESSAGE_CHECKPOINT_READY,
  644. &local_err);
  645. if (local_err) {
  646. goto out;
  647. }
  648. while (mis->state == MIGRATION_STATUS_COLO) {
  649. int request = 0;
  650. colo_wait_handle_message(mis->from_src_file, &request, &local_err);
  651. if (local_err) {
  652. goto out;
  653. }
  654. assert(request);
  655. if (failover_get_state() != FAILOVER_STATUS_NONE) {
  656. error_report("failover request");
  657. goto out;
  658. }
  659. qemu_mutex_lock_iothread();
  660. vm_stop_force_state(RUN_STATE_COLO);
  661. trace_colo_vm_state_change("run", "stop");
  662. qemu_mutex_unlock_iothread();
  663. /* FIXME: This is unnecessary for periodic checkpoint mode */
  664. colo_send_message(mis->to_src_file, COLO_MESSAGE_CHECKPOINT_REPLY,
  665. &local_err);
  666. if (local_err) {
  667. goto out;
  668. }
  669. colo_receive_check_message(mis->from_src_file,
  670. COLO_MESSAGE_VMSTATE_SEND, &local_err);
  671. if (local_err) {
  672. goto out;
  673. }
  674. qemu_mutex_lock_iothread();
  675. cpu_synchronize_all_pre_loadvm();
  676. ret = qemu_loadvm_state_main(mis->from_src_file, mis);
  677. qemu_mutex_unlock_iothread();
  678. if (ret < 0) {
  679. error_report("Load VM's live state (ram) error");
  680. goto out;
  681. }
  682. value = colo_receive_message_value(mis->from_src_file,
  683. COLO_MESSAGE_VMSTATE_SIZE, &local_err);
  684. if (local_err) {
  685. goto out;
  686. }
  687. /*
  688. * Read VM device state data into channel buffer,
  689. * It's better to re-use the memory allocated.
  690. * Here we need to handle the channel buffer directly.
  691. */
  692. if (value > bioc->capacity) {
  693. bioc->capacity = value;
  694. bioc->data = g_realloc(bioc->data, bioc->capacity);
  695. }
  696. total_size = qemu_get_buffer(mis->from_src_file, bioc->data, value);
  697. if (total_size != value) {
  698. error_report("Got %" PRIu64 " VMState data, less than expected"
  699. " %" PRIu64, total_size, value);
  700. goto out;
  701. }
  702. bioc->usage = total_size;
  703. qio_channel_io_seek(QIO_CHANNEL(bioc), 0, 0, NULL);
  704. colo_send_message(mis->to_src_file, COLO_MESSAGE_VMSTATE_RECEIVED,
  705. &local_err);
  706. if (local_err) {
  707. goto out;
  708. }
  709. qemu_mutex_lock_iothread();
  710. vmstate_loading = true;
  711. ret = qemu_load_device_state(fb);
  712. if (ret < 0) {
  713. error_report("COLO: load device state failed");
  714. qemu_mutex_unlock_iothread();
  715. goto out;
  716. }
  717. #ifdef CONFIG_REPLICATION
  718. replication_get_error_all(&local_err);
  719. if (local_err) {
  720. qemu_mutex_unlock_iothread();
  721. goto out;
  722. }
  723. /* discard colo disk buffer */
  724. replication_do_checkpoint_all(&local_err);
  725. if (local_err) {
  726. qemu_mutex_unlock_iothread();
  727. goto out;
  728. }
  729. #else
  730. abort();
  731. #endif
  732. /* Notify all filters of all NIC to do checkpoint */
  733. colo_notify_filters_event(COLO_EVENT_CHECKPOINT, &local_err);
  734. if (local_err) {
  735. qemu_mutex_unlock_iothread();
  736. goto out;
  737. }
  738. vmstate_loading = false;
  739. vm_start();
  740. trace_colo_vm_state_change("stop", "run");
  741. qemu_mutex_unlock_iothread();
  742. if (failover_get_state() == FAILOVER_STATUS_RELAUNCH) {
  743. failover_set_state(FAILOVER_STATUS_RELAUNCH,
  744. FAILOVER_STATUS_NONE);
  745. failover_request_active(NULL);
  746. goto out;
  747. }
  748. colo_send_message(mis->to_src_file, COLO_MESSAGE_VMSTATE_LOADED,
  749. &local_err);
  750. if (local_err) {
  751. goto out;
  752. }
  753. }
  754. out:
  755. vmstate_loading = false;
  756. /* Throw the unreported error message after exited from loop */
  757. if (local_err) {
  758. error_report_err(local_err);
  759. }
  760. /*
  761. * There are only two reasons we can get here, some error happened
  762. * or the user triggered failover.
  763. */
  764. switch (failover_get_state()) {
  765. case FAILOVER_STATUS_COMPLETED:
  766. qapi_event_send_colo_exit(COLO_MODE_SECONDARY,
  767. COLO_EXIT_REASON_REQUEST);
  768. break;
  769. default:
  770. qapi_event_send_colo_exit(COLO_MODE_SECONDARY,
  771. COLO_EXIT_REASON_ERROR);
  772. }
  773. if (fb) {
  774. qemu_fclose(fb);
  775. }
  776. /* Hope this not to be too long to loop here */
  777. qemu_sem_wait(&mis->colo_incoming_sem);
  778. qemu_sem_destroy(&mis->colo_incoming_sem);
  779. /* Must be called after failover BH is completed */
  780. if (mis->to_src_file) {
  781. qemu_fclose(mis->to_src_file);
  782. mis->to_src_file = NULL;
  783. }
  784. rcu_unregister_thread();
  785. return NULL;
  786. }