2
0

colo.c 26 KB

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