migration-hmp-cmds.c 28 KB

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