2
0

migration-hmp-cmds.c 28 KB

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