2
0

migration-hmp-cmds.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. /*
  2. * HMP commands related to migration
  3. *
  4. * Copyright IBM, Corp. 2011
  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/osdep.h"
  16. #include "block/qapi.h"
  17. #include "migration/snapshot.h"
  18. #include "monitor/hmp.h"
  19. #include "monitor/monitor.h"
  20. #include "qapi/error.h"
  21. #include "qapi/qapi-commands-migration.h"
  22. #include "qapi/qapi-visit-migration.h"
  23. #include "qobject/qdict.h"
  24. #include "qapi/string-input-visitor.h"
  25. #include "qapi/string-output-visitor.h"
  26. #include "qemu/cutils.h"
  27. #include "qemu/error-report.h"
  28. #include "qemu/sockets.h"
  29. #include "system/runstate.h"
  30. #include "ui/qemu-spice.h"
  31. #include "system/system.h"
  32. #include "options.h"
  33. #include "migration.h"
  34. static void migration_global_dump(Monitor *mon)
  35. {
  36. MigrationState *ms = migrate_get_current();
  37. monitor_printf(mon, "globals:\n");
  38. monitor_printf(mon, "store-global-state: %s\n",
  39. ms->store_global_state ? "on" : "off");
  40. monitor_printf(mon, "only-migratable: %s\n",
  41. only_migratable ? "on" : "off");
  42. monitor_printf(mon, "send-configuration: %s\n",
  43. ms->send_configuration ? "on" : "off");
  44. monitor_printf(mon, "send-section-footer: %s\n",
  45. ms->send_section_footer ? "on" : "off");
  46. monitor_printf(mon, "send-switchover-start: %s\n",
  47. ms->send_switchover_start ? "on" : "off");
  48. monitor_printf(mon, "clear-bitmap-shift: %u\n",
  49. ms->clear_bitmap_shift);
  50. }
  51. void hmp_info_migrate(Monitor *mon, const QDict *qdict)
  52. {
  53. MigrationInfo *info;
  54. info = qmp_query_migrate(NULL);
  55. migration_global_dump(mon);
  56. if (info->blocked_reasons) {
  57. strList *reasons = info->blocked_reasons;
  58. monitor_printf(mon, "Outgoing migration blocked:\n");
  59. while (reasons) {
  60. monitor_printf(mon, " %s\n", reasons->value);
  61. reasons = reasons->next;
  62. }
  63. }
  64. if (info->has_status) {
  65. monitor_printf(mon, "Migration status: %s",
  66. MigrationStatus_str(info->status));
  67. if (info->status == MIGRATION_STATUS_FAILED && info->error_desc) {
  68. monitor_printf(mon, " (%s)\n", info->error_desc);
  69. } else {
  70. monitor_printf(mon, "\n");
  71. }
  72. monitor_printf(mon, "total time: %" PRIu64 " ms\n",
  73. info->total_time);
  74. if (info->has_expected_downtime) {
  75. monitor_printf(mon, "expected downtime: %" PRIu64 " ms\n",
  76. info->expected_downtime);
  77. }
  78. if (info->has_downtime) {
  79. monitor_printf(mon, "downtime: %" PRIu64 " ms\n",
  80. info->downtime);
  81. }
  82. if (info->has_setup_time) {
  83. monitor_printf(mon, "setup: %" PRIu64 " ms\n",
  84. info->setup_time);
  85. }
  86. }
  87. if (info->ram) {
  88. monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
  89. info->ram->transferred >> 10);
  90. monitor_printf(mon, "throughput: %0.2f mbps\n",
  91. info->ram->mbps);
  92. monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
  93. info->ram->remaining >> 10);
  94. monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
  95. info->ram->total >> 10);
  96. monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
  97. info->ram->duplicate);
  98. monitor_printf(mon, "normal: %" PRIu64 " pages\n",
  99. info->ram->normal);
  100. monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
  101. info->ram->normal_bytes >> 10);
  102. monitor_printf(mon, "dirty sync count: %" PRIu64 "\n",
  103. info->ram->dirty_sync_count);
  104. monitor_printf(mon, "page size: %" PRIu64 " kbytes\n",
  105. info->ram->page_size >> 10);
  106. monitor_printf(mon, "multifd bytes: %" PRIu64 " kbytes\n",
  107. info->ram->multifd_bytes >> 10);
  108. monitor_printf(mon, "pages-per-second: %" PRIu64 "\n",
  109. info->ram->pages_per_second);
  110. if (info->ram->dirty_pages_rate) {
  111. monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
  112. info->ram->dirty_pages_rate);
  113. }
  114. if (info->ram->postcopy_requests) {
  115. monitor_printf(mon, "postcopy request count: %" PRIu64 "\n",
  116. info->ram->postcopy_requests);
  117. }
  118. if (info->ram->precopy_bytes) {
  119. monitor_printf(mon, "precopy ram: %" PRIu64 " kbytes\n",
  120. info->ram->precopy_bytes >> 10);
  121. }
  122. if (info->ram->downtime_bytes) {
  123. monitor_printf(mon, "downtime ram: %" PRIu64 " kbytes\n",
  124. info->ram->downtime_bytes >> 10);
  125. }
  126. if (info->ram->postcopy_bytes) {
  127. monitor_printf(mon, "postcopy ram: %" PRIu64 " kbytes\n",
  128. info->ram->postcopy_bytes >> 10);
  129. }
  130. if (info->ram->dirty_sync_missed_zero_copy) {
  131. monitor_printf(mon,
  132. "Zero-copy-send fallbacks happened: %" PRIu64 " times\n",
  133. info->ram->dirty_sync_missed_zero_copy);
  134. }
  135. }
  136. if (info->xbzrle_cache) {
  137. monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
  138. info->xbzrle_cache->cache_size);
  139. monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
  140. info->xbzrle_cache->bytes >> 10);
  141. monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
  142. info->xbzrle_cache->pages);
  143. monitor_printf(mon, "xbzrle cache miss: %" PRIu64 " pages\n",
  144. info->xbzrle_cache->cache_miss);
  145. monitor_printf(mon, "xbzrle cache miss rate: %0.2f\n",
  146. info->xbzrle_cache->cache_miss_rate);
  147. monitor_printf(mon, "xbzrle encoding rate: %0.2f\n",
  148. info->xbzrle_cache->encoding_rate);
  149. monitor_printf(mon, "xbzrle overflow: %" PRIu64 "\n",
  150. info->xbzrle_cache->overflow);
  151. }
  152. if (info->has_cpu_throttle_percentage) {
  153. monitor_printf(mon, "cpu throttle percentage: %" PRIu64 "\n",
  154. info->cpu_throttle_percentage);
  155. }
  156. if (info->has_dirty_limit_throttle_time_per_round) {
  157. monitor_printf(mon, "dirty-limit throttle time: %" PRIu64 " us\n",
  158. info->dirty_limit_throttle_time_per_round);
  159. }
  160. if (info->has_dirty_limit_ring_full_time) {
  161. monitor_printf(mon, "dirty-limit ring full time: %" PRIu64 " us\n",
  162. info->dirty_limit_ring_full_time);
  163. }
  164. if (info->has_postcopy_blocktime) {
  165. monitor_printf(mon, "postcopy blocktime: %u\n",
  166. info->postcopy_blocktime);
  167. }
  168. if (info->has_postcopy_vcpu_blocktime) {
  169. Visitor *v;
  170. char *str;
  171. v = string_output_visitor_new(false, &str);
  172. visit_type_uint32List(v, NULL, &info->postcopy_vcpu_blocktime,
  173. &error_abort);
  174. visit_complete(v, &str);
  175. monitor_printf(mon, "postcopy vcpu blocktime: %s\n", str);
  176. g_free(str);
  177. visit_free(v);
  178. }
  179. if (info->has_socket_address) {
  180. SocketAddressList *addr;
  181. monitor_printf(mon, "socket address: [\n");
  182. for (addr = info->socket_address; addr; addr = addr->next) {
  183. char *s = socket_uri(addr->value);
  184. monitor_printf(mon, "\t%s\n", s);
  185. g_free(s);
  186. }
  187. monitor_printf(mon, "]\n");
  188. }
  189. if (info->vfio) {
  190. monitor_printf(mon, "vfio device transferred: %" PRIu64 " kbytes\n",
  191. info->vfio->transferred >> 10);
  192. }
  193. qapi_free_MigrationInfo(info);
  194. }
  195. void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
  196. {
  197. MigrationCapabilityStatusList *caps, *cap;
  198. caps = qmp_query_migrate_capabilities(NULL);
  199. if (caps) {
  200. for (cap = caps; cap; cap = cap->next) {
  201. monitor_printf(mon, "%s: %s\n",
  202. MigrationCapability_str(cap->value->capability),
  203. cap->value->state ? "on" : "off");
  204. }
  205. }
  206. qapi_free_MigrationCapabilityStatusList(caps);
  207. }
  208. void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict)
  209. {
  210. MigrationParameters *params;
  211. params = qmp_query_migrate_parameters(NULL);
  212. if (params) {
  213. monitor_printf(mon, "%s: %" PRIu64 " ms\n",
  214. MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_INITIAL),
  215. params->announce_initial);
  216. monitor_printf(mon, "%s: %" PRIu64 " ms\n",
  217. MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_MAX),
  218. params->announce_max);
  219. monitor_printf(mon, "%s: %" PRIu64 "\n",
  220. MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_ROUNDS),
  221. params->announce_rounds);
  222. monitor_printf(mon, "%s: %" PRIu64 " ms\n",
  223. MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_STEP),
  224. params->announce_step);
  225. assert(params->has_throttle_trigger_threshold);
  226. monitor_printf(mon, "%s: %u\n",
  227. MigrationParameter_str(MIGRATION_PARAMETER_THROTTLE_TRIGGER_THRESHOLD),
  228. params->throttle_trigger_threshold);
  229. assert(params->has_cpu_throttle_initial);
  230. monitor_printf(mon, "%s: %u\n",
  231. MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL),
  232. params->cpu_throttle_initial);
  233. assert(params->has_cpu_throttle_increment);
  234. monitor_printf(mon, "%s: %u\n",
  235. MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT),
  236. params->cpu_throttle_increment);
  237. assert(params->has_cpu_throttle_tailslow);
  238. monitor_printf(mon, "%s: %s\n",
  239. MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_TAILSLOW),
  240. params->cpu_throttle_tailslow ? "on" : "off");
  241. assert(params->has_max_cpu_throttle);
  242. monitor_printf(mon, "%s: %u\n",
  243. MigrationParameter_str(MIGRATION_PARAMETER_MAX_CPU_THROTTLE),
  244. params->max_cpu_throttle);
  245. assert(params->tls_creds);
  246. monitor_printf(mon, "%s: '%s'\n",
  247. MigrationParameter_str(MIGRATION_PARAMETER_TLS_CREDS),
  248. params->tls_creds);
  249. assert(params->tls_hostname);
  250. monitor_printf(mon, "%s: '%s'\n",
  251. MigrationParameter_str(MIGRATION_PARAMETER_TLS_HOSTNAME),
  252. params->tls_hostname);
  253. assert(params->has_max_bandwidth);
  254. monitor_printf(mon, "%s: %" PRIu64 " bytes/second\n",
  255. MigrationParameter_str(MIGRATION_PARAMETER_MAX_BANDWIDTH),
  256. params->max_bandwidth);
  257. assert(params->has_avail_switchover_bandwidth);
  258. monitor_printf(mon, "%s: %" PRIu64 " bytes/second\n",
  259. MigrationParameter_str(MIGRATION_PARAMETER_AVAIL_SWITCHOVER_BANDWIDTH),
  260. params->avail_switchover_bandwidth);
  261. assert(params->has_downtime_limit);
  262. monitor_printf(mon, "%s: %" PRIu64 " ms\n",
  263. MigrationParameter_str(MIGRATION_PARAMETER_DOWNTIME_LIMIT),
  264. params->downtime_limit);
  265. assert(params->has_x_checkpoint_delay);
  266. monitor_printf(mon, "%s: %u ms\n",
  267. MigrationParameter_str(MIGRATION_PARAMETER_X_CHECKPOINT_DELAY),
  268. params->x_checkpoint_delay);
  269. monitor_printf(mon, "%s: %u\n",
  270. MigrationParameter_str(MIGRATION_PARAMETER_MULTIFD_CHANNELS),
  271. params->multifd_channels);
  272. monitor_printf(mon, "%s: %s\n",
  273. MigrationParameter_str(MIGRATION_PARAMETER_MULTIFD_COMPRESSION),
  274. MultiFDCompression_str(params->multifd_compression));
  275. assert(params->has_zero_page_detection);
  276. monitor_printf(mon, "%s: %s\n",
  277. MigrationParameter_str(MIGRATION_PARAMETER_ZERO_PAGE_DETECTION),
  278. qapi_enum_lookup(&ZeroPageDetection_lookup,
  279. params->zero_page_detection));
  280. monitor_printf(mon, "%s: %" PRIu64 " bytes\n",
  281. MigrationParameter_str(MIGRATION_PARAMETER_XBZRLE_CACHE_SIZE),
  282. params->xbzrle_cache_size);
  283. monitor_printf(mon, "%s: %" PRIu64 "\n",
  284. MigrationParameter_str(MIGRATION_PARAMETER_MAX_POSTCOPY_BANDWIDTH),
  285. params->max_postcopy_bandwidth);
  286. monitor_printf(mon, "%s: '%s'\n",
  287. MigrationParameter_str(MIGRATION_PARAMETER_TLS_AUTHZ),
  288. params->tls_authz);
  289. if (params->has_block_bitmap_mapping) {
  290. const BitmapMigrationNodeAliasList *bmnal;
  291. monitor_printf(mon, "%s:\n",
  292. MigrationParameter_str(
  293. MIGRATION_PARAMETER_BLOCK_BITMAP_MAPPING));
  294. for (bmnal = params->block_bitmap_mapping;
  295. bmnal;
  296. bmnal = bmnal->next)
  297. {
  298. const BitmapMigrationNodeAlias *bmna = bmnal->value;
  299. const BitmapMigrationBitmapAliasList *bmbal;
  300. monitor_printf(mon, " '%s' -> '%s'\n",
  301. bmna->node_name, bmna->alias);
  302. for (bmbal = bmna->bitmaps; bmbal; bmbal = bmbal->next) {
  303. const BitmapMigrationBitmapAlias *bmba = bmbal->value;
  304. monitor_printf(mon, " '%s' -> '%s'\n",
  305. bmba->name, bmba->alias);
  306. }
  307. }
  308. }
  309. monitor_printf(mon, "%s: %" PRIu64 " ms\n",
  310. MigrationParameter_str(MIGRATION_PARAMETER_X_VCPU_DIRTY_LIMIT_PERIOD),
  311. params->x_vcpu_dirty_limit_period);
  312. monitor_printf(mon, "%s: %" PRIu64 " MB/s\n",
  313. MigrationParameter_str(MIGRATION_PARAMETER_VCPU_DIRTY_LIMIT),
  314. params->vcpu_dirty_limit);
  315. assert(params->has_mode);
  316. monitor_printf(mon, "%s: %s\n",
  317. MigrationParameter_str(MIGRATION_PARAMETER_MODE),
  318. qapi_enum_lookup(&MigMode_lookup, params->mode));
  319. if (params->has_direct_io) {
  320. monitor_printf(mon, "%s: %s\n",
  321. MigrationParameter_str(
  322. MIGRATION_PARAMETER_DIRECT_IO),
  323. params->direct_io ? "on" : "off");
  324. }
  325. }
  326. qapi_free_MigrationParameters(params);
  327. }
  328. void hmp_loadvm(Monitor *mon, const QDict *qdict)
  329. {
  330. RunState saved_state = runstate_get();
  331. const char *name = qdict_get_str(qdict, "name");
  332. Error *err = NULL;
  333. vm_stop(RUN_STATE_RESTORE_VM);
  334. if (load_snapshot(name, NULL, false, NULL, &err)) {
  335. load_snapshot_resume(saved_state);
  336. }
  337. hmp_handle_error(mon, err);
  338. }
  339. void hmp_savevm(Monitor *mon, const QDict *qdict)
  340. {
  341. Error *err = NULL;
  342. save_snapshot(qdict_get_try_str(qdict, "name"),
  343. true, NULL, false, NULL, &err);
  344. hmp_handle_error(mon, err);
  345. }
  346. void hmp_delvm(Monitor *mon, const QDict *qdict)
  347. {
  348. Error *err = NULL;
  349. const char *name = qdict_get_str(qdict, "name");
  350. delete_snapshot(name, false, NULL, &err);
  351. hmp_handle_error(mon, err);
  352. }
  353. void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
  354. {
  355. qmp_migrate_cancel(NULL);
  356. }
  357. void hmp_migrate_continue(Monitor *mon, const QDict *qdict)
  358. {
  359. Error *err = NULL;
  360. const char *state = qdict_get_str(qdict, "state");
  361. int val = qapi_enum_parse(&MigrationStatus_lookup, state, -1, &err);
  362. if (val >= 0) {
  363. qmp_migrate_continue(val, &err);
  364. }
  365. hmp_handle_error(mon, err);
  366. }
  367. void hmp_migrate_incoming(Monitor *mon, const QDict *qdict)
  368. {
  369. Error *err = NULL;
  370. const char *uri = qdict_get_str(qdict, "uri");
  371. MigrationChannelList *caps = NULL;
  372. g_autoptr(MigrationChannel) channel = NULL;
  373. if (!migrate_uri_parse(uri, &channel, &err)) {
  374. goto end;
  375. }
  376. QAPI_LIST_PREPEND(caps, g_steal_pointer(&channel));
  377. qmp_migrate_incoming(NULL, true, caps, true, false, &err);
  378. qapi_free_MigrationChannelList(caps);
  379. end:
  380. hmp_handle_error(mon, err);
  381. }
  382. void hmp_migrate_recover(Monitor *mon, const QDict *qdict)
  383. {
  384. Error *err = NULL;
  385. const char *uri = qdict_get_str(qdict, "uri");
  386. qmp_migrate_recover(uri, &err);
  387. hmp_handle_error(mon, err);
  388. }
  389. void hmp_migrate_pause(Monitor *mon, const QDict *qdict)
  390. {
  391. Error *err = NULL;
  392. qmp_migrate_pause(&err);
  393. hmp_handle_error(mon, err);
  394. }
  395. void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
  396. {
  397. const char *cap = qdict_get_str(qdict, "capability");
  398. bool state = qdict_get_bool(qdict, "state");
  399. Error *err = NULL;
  400. MigrationCapabilityStatusList *caps = NULL;
  401. MigrationCapabilityStatus *value;
  402. int val;
  403. val = qapi_enum_parse(&MigrationCapability_lookup, cap, -1, &err);
  404. if (val < 0) {
  405. goto end;
  406. }
  407. value = g_malloc0(sizeof(*value));
  408. value->capability = val;
  409. value->state = state;
  410. QAPI_LIST_PREPEND(caps, value);
  411. qmp_migrate_set_capabilities(caps, &err);
  412. qapi_free_MigrationCapabilityStatusList(caps);
  413. end:
  414. hmp_handle_error(mon, err);
  415. }
  416. void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
  417. {
  418. const char *param = qdict_get_str(qdict, "parameter");
  419. const char *valuestr = qdict_get_str(qdict, "value");
  420. Visitor *v = string_input_visitor_new(valuestr);
  421. MigrateSetParameters *p = g_new0(MigrateSetParameters, 1);
  422. uint64_t valuebw = 0;
  423. uint64_t cache_size;
  424. Error *err = NULL;
  425. int val, ret;
  426. val = qapi_enum_parse(&MigrationParameter_lookup, param, -1, &err);
  427. if (val < 0) {
  428. goto cleanup;
  429. }
  430. switch (val) {
  431. case MIGRATION_PARAMETER_THROTTLE_TRIGGER_THRESHOLD:
  432. p->has_throttle_trigger_threshold = true;
  433. visit_type_uint8(v, param, &p->throttle_trigger_threshold, &err);
  434. break;
  435. case MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL:
  436. p->has_cpu_throttle_initial = true;
  437. visit_type_uint8(v, param, &p->cpu_throttle_initial, &err);
  438. break;
  439. case MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT:
  440. p->has_cpu_throttle_increment = true;
  441. visit_type_uint8(v, param, &p->cpu_throttle_increment, &err);
  442. break;
  443. case MIGRATION_PARAMETER_CPU_THROTTLE_TAILSLOW:
  444. p->has_cpu_throttle_tailslow = true;
  445. visit_type_bool(v, param, &p->cpu_throttle_tailslow, &err);
  446. break;
  447. case MIGRATION_PARAMETER_MAX_CPU_THROTTLE:
  448. p->has_max_cpu_throttle = true;
  449. visit_type_uint8(v, param, &p->max_cpu_throttle, &err);
  450. break;
  451. case MIGRATION_PARAMETER_TLS_CREDS:
  452. p->tls_creds = g_new0(StrOrNull, 1);
  453. p->tls_creds->type = QTYPE_QSTRING;
  454. visit_type_str(v, param, &p->tls_creds->u.s, &err);
  455. break;
  456. case MIGRATION_PARAMETER_TLS_HOSTNAME:
  457. p->tls_hostname = g_new0(StrOrNull, 1);
  458. p->tls_hostname->type = QTYPE_QSTRING;
  459. visit_type_str(v, param, &p->tls_hostname->u.s, &err);
  460. break;
  461. case MIGRATION_PARAMETER_TLS_AUTHZ:
  462. p->tls_authz = g_new0(StrOrNull, 1);
  463. p->tls_authz->type = QTYPE_QSTRING;
  464. visit_type_str(v, param, &p->tls_authz->u.s, &err);
  465. break;
  466. case MIGRATION_PARAMETER_MAX_BANDWIDTH:
  467. p->has_max_bandwidth = true;
  468. /*
  469. * Can't use visit_type_size() here, because it
  470. * defaults to Bytes rather than Mebibytes.
  471. */
  472. ret = qemu_strtosz_MiB(valuestr, NULL, &valuebw);
  473. if (ret < 0 || valuebw > INT64_MAX
  474. || (size_t)valuebw != valuebw) {
  475. error_setg(&err, "Invalid size %s", valuestr);
  476. break;
  477. }
  478. p->max_bandwidth = valuebw;
  479. break;
  480. case MIGRATION_PARAMETER_AVAIL_SWITCHOVER_BANDWIDTH:
  481. p->has_avail_switchover_bandwidth = true;
  482. ret = qemu_strtosz_MiB(valuestr, NULL, &valuebw);
  483. if (ret < 0 || valuebw > INT64_MAX
  484. || (size_t)valuebw != valuebw) {
  485. error_setg(&err, "Invalid size %s", valuestr);
  486. break;
  487. }
  488. p->avail_switchover_bandwidth = valuebw;
  489. break;
  490. case MIGRATION_PARAMETER_DOWNTIME_LIMIT:
  491. p->has_downtime_limit = true;
  492. visit_type_size(v, param, &p->downtime_limit, &err);
  493. break;
  494. case MIGRATION_PARAMETER_X_CHECKPOINT_DELAY:
  495. p->has_x_checkpoint_delay = true;
  496. visit_type_uint32(v, param, &p->x_checkpoint_delay, &err);
  497. break;
  498. case MIGRATION_PARAMETER_MULTIFD_CHANNELS:
  499. p->has_multifd_channels = true;
  500. visit_type_uint8(v, param, &p->multifd_channels, &err);
  501. break;
  502. case MIGRATION_PARAMETER_MULTIFD_COMPRESSION:
  503. p->has_multifd_compression = true;
  504. visit_type_MultiFDCompression(v, param, &p->multifd_compression,
  505. &err);
  506. break;
  507. case MIGRATION_PARAMETER_MULTIFD_ZLIB_LEVEL:
  508. p->has_multifd_zlib_level = true;
  509. visit_type_uint8(v, param, &p->multifd_zlib_level, &err);
  510. break;
  511. case MIGRATION_PARAMETER_MULTIFD_QATZIP_LEVEL:
  512. p->has_multifd_qatzip_level = true;
  513. visit_type_uint8(v, param, &p->multifd_qatzip_level, &err);
  514. break;
  515. case MIGRATION_PARAMETER_MULTIFD_ZSTD_LEVEL:
  516. p->has_multifd_zstd_level = true;
  517. visit_type_uint8(v, param, &p->multifd_zstd_level, &err);
  518. break;
  519. case MIGRATION_PARAMETER_ZERO_PAGE_DETECTION:
  520. p->has_zero_page_detection = true;
  521. visit_type_ZeroPageDetection(v, param, &p->zero_page_detection, &err);
  522. break;
  523. case MIGRATION_PARAMETER_XBZRLE_CACHE_SIZE:
  524. p->has_xbzrle_cache_size = true;
  525. if (!visit_type_size(v, param, &cache_size, &err)) {
  526. break;
  527. }
  528. if (cache_size > INT64_MAX || (size_t)cache_size != cache_size) {
  529. error_setg(&err, "Invalid size %s", valuestr);
  530. break;
  531. }
  532. p->xbzrle_cache_size = cache_size;
  533. break;
  534. case MIGRATION_PARAMETER_MAX_POSTCOPY_BANDWIDTH:
  535. p->has_max_postcopy_bandwidth = true;
  536. visit_type_size(v, param, &p->max_postcopy_bandwidth, &err);
  537. break;
  538. case MIGRATION_PARAMETER_ANNOUNCE_INITIAL:
  539. p->has_announce_initial = true;
  540. visit_type_size(v, param, &p->announce_initial, &err);
  541. break;
  542. case MIGRATION_PARAMETER_ANNOUNCE_MAX:
  543. p->has_announce_max = true;
  544. visit_type_size(v, param, &p->announce_max, &err);
  545. break;
  546. case MIGRATION_PARAMETER_ANNOUNCE_ROUNDS:
  547. p->has_announce_rounds = true;
  548. visit_type_size(v, param, &p->announce_rounds, &err);
  549. break;
  550. case MIGRATION_PARAMETER_ANNOUNCE_STEP:
  551. p->has_announce_step = true;
  552. visit_type_size(v, param, &p->announce_step, &err);
  553. break;
  554. case MIGRATION_PARAMETER_BLOCK_BITMAP_MAPPING:
  555. error_setg(&err, "The block-bitmap-mapping parameter can only be set "
  556. "through QMP");
  557. break;
  558. case MIGRATION_PARAMETER_X_VCPU_DIRTY_LIMIT_PERIOD:
  559. p->has_x_vcpu_dirty_limit_period = true;
  560. visit_type_size(v, param, &p->x_vcpu_dirty_limit_period, &err);
  561. break;
  562. case MIGRATION_PARAMETER_VCPU_DIRTY_LIMIT:
  563. p->has_vcpu_dirty_limit = true;
  564. visit_type_size(v, param, &p->vcpu_dirty_limit, &err);
  565. break;
  566. case MIGRATION_PARAMETER_MODE:
  567. p->has_mode = true;
  568. visit_type_MigMode(v, param, &p->mode, &err);
  569. break;
  570. case MIGRATION_PARAMETER_DIRECT_IO:
  571. p->has_direct_io = true;
  572. visit_type_bool(v, param, &p->direct_io, &err);
  573. break;
  574. default:
  575. g_assert_not_reached();
  576. }
  577. if (err) {
  578. goto cleanup;
  579. }
  580. qmp_migrate_set_parameters(p, &err);
  581. cleanup:
  582. qapi_free_MigrateSetParameters(p);
  583. visit_free(v);
  584. hmp_handle_error(mon, err);
  585. }
  586. void hmp_migrate_start_postcopy(Monitor *mon, const QDict *qdict)
  587. {
  588. Error *err = NULL;
  589. qmp_migrate_start_postcopy(&err);
  590. hmp_handle_error(mon, err);
  591. }
  592. #ifdef CONFIG_REPLICATION
  593. void hmp_x_colo_lost_heartbeat(Monitor *mon, const QDict *qdict)
  594. {
  595. Error *err = NULL;
  596. qmp_x_colo_lost_heartbeat(&err);
  597. hmp_handle_error(mon, err);
  598. }
  599. #endif
  600. typedef struct HMPMigrationStatus {
  601. QEMUTimer *timer;
  602. Monitor *mon;
  603. } HMPMigrationStatus;
  604. static void hmp_migrate_status_cb(void *opaque)
  605. {
  606. HMPMigrationStatus *status = opaque;
  607. MigrationInfo *info;
  608. info = qmp_query_migrate(NULL);
  609. if (!info->has_status || info->status == MIGRATION_STATUS_ACTIVE ||
  610. info->status == MIGRATION_STATUS_SETUP) {
  611. timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
  612. } else {
  613. if (info->error_desc) {
  614. error_report("%s", info->error_desc);
  615. }
  616. monitor_resume(status->mon);
  617. timer_free(status->timer);
  618. g_free(status);
  619. }
  620. qapi_free_MigrationInfo(info);
  621. }
  622. void hmp_migrate(Monitor *mon, const QDict *qdict)
  623. {
  624. bool detach = qdict_get_try_bool(qdict, "detach", false);
  625. bool resume = qdict_get_try_bool(qdict, "resume", false);
  626. const char *uri = qdict_get_str(qdict, "uri");
  627. Error *err = NULL;
  628. g_autoptr(MigrationChannelList) caps = NULL;
  629. g_autoptr(MigrationChannel) channel = NULL;
  630. if (!migrate_uri_parse(uri, &channel, &err)) {
  631. hmp_handle_error(mon, err);
  632. return;
  633. }
  634. QAPI_LIST_PREPEND(caps, g_steal_pointer(&channel));
  635. qmp_migrate(NULL, true, caps, false, false, true, resume, &err);
  636. if (hmp_handle_error(mon, err)) {
  637. return;
  638. }
  639. if (!detach) {
  640. HMPMigrationStatus *status;
  641. if (monitor_suspend(mon) < 0) {
  642. monitor_printf(mon, "terminal does not allow synchronous "
  643. "migration, continuing detached\n");
  644. return;
  645. }
  646. status = g_malloc0(sizeof(*status));
  647. status->mon = mon;
  648. status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
  649. status);
  650. timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
  651. }
  652. }
  653. void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
  654. const char *str)
  655. {
  656. size_t len;
  657. len = strlen(str);
  658. readline_set_completion_index(rs, len);
  659. if (nb_args == 2) {
  660. int i;
  661. for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
  662. readline_add_completion_of(rs, str, MigrationCapability_str(i));
  663. }
  664. } else if (nb_args == 3) {
  665. readline_add_completion_of(rs, str, "on");
  666. readline_add_completion_of(rs, str, "off");
  667. }
  668. }
  669. void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
  670. const char *str)
  671. {
  672. size_t len;
  673. len = strlen(str);
  674. readline_set_completion_index(rs, len);
  675. if (nb_args == 2) {
  676. int i;
  677. for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
  678. readline_add_completion_of(rs, str, MigrationParameter_str(i));
  679. }
  680. }
  681. }
  682. static void vm_completion(ReadLineState *rs, const char *str)
  683. {
  684. size_t len;
  685. BlockDriverState *bs;
  686. BdrvNextIterator it;
  687. GRAPH_RDLOCK_GUARD_MAINLOOP();
  688. len = strlen(str);
  689. readline_set_completion_index(rs, len);
  690. for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
  691. SnapshotInfoList *snapshots, *snapshot;
  692. bool ok = false;
  693. if (bdrv_can_snapshot(bs)) {
  694. ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
  695. }
  696. if (!ok) {
  697. continue;
  698. }
  699. snapshot = snapshots;
  700. while (snapshot) {
  701. readline_add_completion_of(rs, str, snapshot->value->name);
  702. readline_add_completion_of(rs, str, snapshot->value->id);
  703. snapshot = snapshot->next;
  704. }
  705. qapi_free_SnapshotInfoList(snapshots);
  706. }
  707. }
  708. void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
  709. {
  710. if (nb_args == 2) {
  711. vm_completion(rs, str);
  712. }
  713. }
  714. void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
  715. {
  716. if (nb_args == 2) {
  717. vm_completion(rs, str);
  718. }
  719. }