You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3126 lines
97KB

  1. /*
  2. * Human Monitor Interface
  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 "hmp.h"
  17. #include "net/net.h"
  18. #include "net/eth.h"
  19. #include "chardev/char.h"
  20. #include "sysemu/block-backend.h"
  21. #include "sysemu/sysemu.h"
  22. #include "qemu/config-file.h"
  23. #include "qemu/option.h"
  24. #include "qemu/timer.h"
  25. #include "qemu/sockets.h"
  26. #include "monitor/monitor.h"
  27. #include "monitor/qdev.h"
  28. #include "qapi/error.h"
  29. #include "qapi/opts-visitor.h"
  30. #include "qapi/qapi-builtin-visit.h"
  31. #include "qapi/qapi-commands-block.h"
  32. #include "qapi/qapi-commands-char.h"
  33. #include "qapi/qapi-commands-migration.h"
  34. #include "qapi/qapi-commands-misc.h"
  35. #include "qapi/qapi-commands-net.h"
  36. #include "qapi/qapi-commands-rocker.h"
  37. #include "qapi/qapi-commands-run-state.h"
  38. #include "qapi/qapi-commands-tpm.h"
  39. #include "qapi/qapi-commands-ui.h"
  40. #include "qapi/qmp/qdict.h"
  41. #include "qapi/qmp/qerror.h"
  42. #include "qapi/string-input-visitor.h"
  43. #include "qapi/string-output-visitor.h"
  44. #include "qom/object_interfaces.h"
  45. #include "ui/console.h"
  46. #include "block/nbd.h"
  47. #include "block/qapi.h"
  48. #include "qemu-io.h"
  49. #include "qemu/cutils.h"
  50. #include "qemu/error-report.h"
  51. #include "exec/ramlist.h"
  52. #include "hw/intc/intc.h"
  53. #include "migration/snapshot.h"
  54. #include "migration/misc.h"
  55. #ifdef CONFIG_SPICE
  56. #include <spice/enums.h>
  57. #endif
  58. static void hmp_handle_error(Monitor *mon, Error **errp)
  59. {
  60. assert(errp);
  61. if (*errp) {
  62. error_reportf_err(*errp, "Error: ");
  63. }
  64. }
  65. void hmp_info_name(Monitor *mon, const QDict *qdict)
  66. {
  67. NameInfo *info;
  68. info = qmp_query_name(NULL);
  69. if (info->has_name) {
  70. monitor_printf(mon, "%s\n", info->name);
  71. }
  72. qapi_free_NameInfo(info);
  73. }
  74. void hmp_info_version(Monitor *mon, const QDict *qdict)
  75. {
  76. VersionInfo *info;
  77. info = qmp_query_version(NULL);
  78. monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
  79. info->qemu->major, info->qemu->minor, info->qemu->micro,
  80. info->package);
  81. qapi_free_VersionInfo(info);
  82. }
  83. void hmp_info_kvm(Monitor *mon, const QDict *qdict)
  84. {
  85. KvmInfo *info;
  86. info = qmp_query_kvm(NULL);
  87. monitor_printf(mon, "kvm support: ");
  88. if (info->present) {
  89. monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
  90. } else {
  91. monitor_printf(mon, "not compiled\n");
  92. }
  93. qapi_free_KvmInfo(info);
  94. }
  95. void hmp_info_status(Monitor *mon, const QDict *qdict)
  96. {
  97. StatusInfo *info;
  98. info = qmp_query_status(NULL);
  99. monitor_printf(mon, "VM status: %s%s",
  100. info->running ? "running" : "paused",
  101. info->singlestep ? " (single step mode)" : "");
  102. if (!info->running && info->status != RUN_STATE_PAUSED) {
  103. monitor_printf(mon, " (%s)", RunState_str(info->status));
  104. }
  105. monitor_printf(mon, "\n");
  106. qapi_free_StatusInfo(info);
  107. }
  108. void hmp_info_uuid(Monitor *mon, const QDict *qdict)
  109. {
  110. UuidInfo *info;
  111. info = qmp_query_uuid(NULL);
  112. monitor_printf(mon, "%s\n", info->UUID);
  113. qapi_free_UuidInfo(info);
  114. }
  115. void hmp_info_chardev(Monitor *mon, const QDict *qdict)
  116. {
  117. ChardevInfoList *char_info, *info;
  118. char_info = qmp_query_chardev(NULL);
  119. for (info = char_info; info; info = info->next) {
  120. monitor_printf(mon, "%s: filename=%s\n", info->value->label,
  121. info->value->filename);
  122. }
  123. qapi_free_ChardevInfoList(char_info);
  124. }
  125. void hmp_info_mice(Monitor *mon, const QDict *qdict)
  126. {
  127. MouseInfoList *mice_list, *mouse;
  128. mice_list = qmp_query_mice(NULL);
  129. if (!mice_list) {
  130. monitor_printf(mon, "No mouse devices connected\n");
  131. return;
  132. }
  133. for (mouse = mice_list; mouse; mouse = mouse->next) {
  134. monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
  135. mouse->value->current ? '*' : ' ',
  136. mouse->value->index, mouse->value->name,
  137. mouse->value->absolute ? " (absolute)" : "");
  138. }
  139. qapi_free_MouseInfoList(mice_list);
  140. }
  141. static char *SocketAddress_to_str(SocketAddress *addr)
  142. {
  143. switch (addr->type) {
  144. case SOCKET_ADDRESS_TYPE_INET:
  145. return g_strdup_printf("tcp:%s:%s",
  146. addr->u.inet.host,
  147. addr->u.inet.port);
  148. case SOCKET_ADDRESS_TYPE_UNIX:
  149. return g_strdup_printf("unix:%s",
  150. addr->u.q_unix.path);
  151. case SOCKET_ADDRESS_TYPE_FD:
  152. return g_strdup_printf("fd:%s", addr->u.fd.str);
  153. case SOCKET_ADDRESS_TYPE_VSOCK:
  154. return g_strdup_printf("tcp:%s:%s",
  155. addr->u.vsock.cid,
  156. addr->u.vsock.port);
  157. default:
  158. return g_strdup("unknown address type");
  159. }
  160. }
  161. void hmp_info_migrate(Monitor *mon, const QDict *qdict)
  162. {
  163. MigrationInfo *info;
  164. MigrationCapabilityStatusList *caps, *cap;
  165. info = qmp_query_migrate(NULL);
  166. caps = qmp_query_migrate_capabilities(NULL);
  167. migration_global_dump(mon);
  168. /* do not display parameters during setup */
  169. if (info->has_status && caps) {
  170. monitor_printf(mon, "capabilities: ");
  171. for (cap = caps; cap; cap = cap->next) {
  172. monitor_printf(mon, "%s: %s ",
  173. MigrationCapability_str(cap->value->capability),
  174. cap->value->state ? "on" : "off");
  175. }
  176. monitor_printf(mon, "\n");
  177. }
  178. if (info->has_status) {
  179. monitor_printf(mon, "Migration status: %s",
  180. MigrationStatus_str(info->status));
  181. if (info->status == MIGRATION_STATUS_FAILED &&
  182. info->has_error_desc) {
  183. monitor_printf(mon, " (%s)\n", info->error_desc);
  184. } else {
  185. monitor_printf(mon, "\n");
  186. }
  187. monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
  188. info->total_time);
  189. if (info->has_expected_downtime) {
  190. monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
  191. info->expected_downtime);
  192. }
  193. if (info->has_downtime) {
  194. monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
  195. info->downtime);
  196. }
  197. if (info->has_setup_time) {
  198. monitor_printf(mon, "setup: %" PRIu64 " milliseconds\n",
  199. info->setup_time);
  200. }
  201. }
  202. if (info->has_ram) {
  203. monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
  204. info->ram->transferred >> 10);
  205. monitor_printf(mon, "throughput: %0.2f mbps\n",
  206. info->ram->mbps);
  207. monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
  208. info->ram->remaining >> 10);
  209. monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
  210. info->ram->total >> 10);
  211. monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
  212. info->ram->duplicate);
  213. monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
  214. info->ram->skipped);
  215. monitor_printf(mon, "normal: %" PRIu64 " pages\n",
  216. info->ram->normal);
  217. monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
  218. info->ram->normal_bytes >> 10);
  219. monitor_printf(mon, "dirty sync count: %" PRIu64 "\n",
  220. info->ram->dirty_sync_count);
  221. monitor_printf(mon, "page size: %" PRIu64 " kbytes\n",
  222. info->ram->page_size >> 10);
  223. monitor_printf(mon, "multifd bytes: %" PRIu64 " kbytes\n",
  224. info->ram->multifd_bytes >> 10);
  225. monitor_printf(mon, "pages-per-second: %" PRIu64 "\n",
  226. info->ram->pages_per_second);
  227. if (info->ram->dirty_pages_rate) {
  228. monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
  229. info->ram->dirty_pages_rate);
  230. }
  231. if (info->ram->postcopy_requests) {
  232. monitor_printf(mon, "postcopy request count: %" PRIu64 "\n",
  233. info->ram->postcopy_requests);
  234. }
  235. }
  236. if (info->has_disk) {
  237. monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
  238. info->disk->transferred >> 10);
  239. monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
  240. info->disk->remaining >> 10);
  241. monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
  242. info->disk->total >> 10);
  243. }
  244. if (info->has_xbzrle_cache) {
  245. monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
  246. info->xbzrle_cache->cache_size);
  247. monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
  248. info->xbzrle_cache->bytes >> 10);
  249. monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
  250. info->xbzrle_cache->pages);
  251. monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
  252. info->xbzrle_cache->cache_miss);
  253. monitor_printf(mon, "xbzrle cache miss rate: %0.2f\n",
  254. info->xbzrle_cache->cache_miss_rate);
  255. monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
  256. info->xbzrle_cache->overflow);
  257. }
  258. if (info->has_compression) {
  259. monitor_printf(mon, "compression pages: %" PRIu64 " pages\n",
  260. info->compression->pages);
  261. monitor_printf(mon, "compression busy: %" PRIu64 "\n",
  262. info->compression->busy);
  263. monitor_printf(mon, "compression busy rate: %0.2f\n",
  264. info->compression->busy_rate);
  265. monitor_printf(mon, "compressed size: %" PRIu64 "\n",
  266. info->compression->compressed_size);
  267. monitor_printf(mon, "compression rate: %0.2f\n",
  268. info->compression->compression_rate);
  269. }
  270. if (info->has_cpu_throttle_percentage) {
  271. monitor_printf(mon, "cpu throttle percentage: %" PRIu64 "\n",
  272. info->cpu_throttle_percentage);
  273. }
  274. if (info->has_postcopy_blocktime) {
  275. monitor_printf(mon, "postcopy blocktime: %u\n",
  276. info->postcopy_blocktime);
  277. }
  278. if (info->has_postcopy_vcpu_blocktime) {
  279. Visitor *v;
  280. char *str;
  281. v = string_output_visitor_new(false, &str);
  282. visit_type_uint32List(v, NULL, &info->postcopy_vcpu_blocktime, NULL);
  283. visit_complete(v, &str);
  284. monitor_printf(mon, "postcopy vcpu blocktime: %s\n", str);
  285. g_free(str);
  286. visit_free(v);
  287. }
  288. if (info->has_socket_address) {
  289. SocketAddressList *addr;
  290. monitor_printf(mon, "socket address: [\n");
  291. for (addr = info->socket_address; addr; addr = addr->next) {
  292. char *s = SocketAddress_to_str(addr->value);
  293. monitor_printf(mon, "\t%s\n", s);
  294. g_free(s);
  295. }
  296. monitor_printf(mon, "]\n");
  297. }
  298. qapi_free_MigrationInfo(info);
  299. qapi_free_MigrationCapabilityStatusList(caps);
  300. }
  301. void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
  302. {
  303. MigrationCapabilityStatusList *caps, *cap;
  304. caps = qmp_query_migrate_capabilities(NULL);
  305. if (caps) {
  306. for (cap = caps; cap; cap = cap->next) {
  307. monitor_printf(mon, "%s: %s\n",
  308. MigrationCapability_str(cap->value->capability),
  309. cap->value->state ? "on" : "off");
  310. }
  311. }
  312. qapi_free_MigrationCapabilityStatusList(caps);
  313. }
  314. void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict)
  315. {
  316. MigrationParameters *params;
  317. params = qmp_query_migrate_parameters(NULL);
  318. if (params) {
  319. monitor_printf(mon, "%s: %" PRIu64 " ms\n",
  320. MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_INITIAL),
  321. params->announce_initial);
  322. monitor_printf(mon, "%s: %" PRIu64 " ms\n",
  323. MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_MAX),
  324. params->announce_max);
  325. monitor_printf(mon, "%s: %" PRIu64 "\n",
  326. MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_ROUNDS),
  327. params->announce_rounds);
  328. monitor_printf(mon, "%s: %" PRIu64 " ms\n",
  329. MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_STEP),
  330. params->announce_step);
  331. assert(params->has_compress_level);
  332. monitor_printf(mon, "%s: %u\n",
  333. MigrationParameter_str(MIGRATION_PARAMETER_COMPRESS_LEVEL),
  334. params->compress_level);
  335. assert(params->has_compress_threads);
  336. monitor_printf(mon, "%s: %u\n",
  337. MigrationParameter_str(MIGRATION_PARAMETER_COMPRESS_THREADS),
  338. params->compress_threads);
  339. assert(params->has_compress_wait_thread);
  340. monitor_printf(mon, "%s: %s\n",
  341. MigrationParameter_str(MIGRATION_PARAMETER_COMPRESS_WAIT_THREAD),
  342. params->compress_wait_thread ? "on" : "off");
  343. assert(params->has_decompress_threads);
  344. monitor_printf(mon, "%s: %u\n",
  345. MigrationParameter_str(MIGRATION_PARAMETER_DECOMPRESS_THREADS),
  346. params->decompress_threads);
  347. assert(params->has_cpu_throttle_initial);
  348. monitor_printf(mon, "%s: %u\n",
  349. MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL),
  350. params->cpu_throttle_initial);
  351. assert(params->has_cpu_throttle_increment);
  352. monitor_printf(mon, "%s: %u\n",
  353. MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT),
  354. params->cpu_throttle_increment);
  355. assert(params->has_max_cpu_throttle);
  356. monitor_printf(mon, "%s: %u\n",
  357. MigrationParameter_str(MIGRATION_PARAMETER_MAX_CPU_THROTTLE),
  358. params->max_cpu_throttle);
  359. assert(params->has_tls_creds);
  360. monitor_printf(mon, "%s: '%s'\n",
  361. MigrationParameter_str(MIGRATION_PARAMETER_TLS_CREDS),
  362. params->tls_creds);
  363. assert(params->has_tls_hostname);
  364. monitor_printf(mon, "%s: '%s'\n",
  365. MigrationParameter_str(MIGRATION_PARAMETER_TLS_HOSTNAME),
  366. params->tls_hostname);
  367. assert(params->has_max_bandwidth);
  368. monitor_printf(mon, "%s: %" PRIu64 " bytes/second\n",
  369. MigrationParameter_str(MIGRATION_PARAMETER_MAX_BANDWIDTH),
  370. params->max_bandwidth);
  371. assert(params->has_downtime_limit);
  372. monitor_printf(mon, "%s: %" PRIu64 " milliseconds\n",
  373. MigrationParameter_str(MIGRATION_PARAMETER_DOWNTIME_LIMIT),
  374. params->downtime_limit);
  375. assert(params->has_x_checkpoint_delay);
  376. monitor_printf(mon, "%s: %u\n",
  377. MigrationParameter_str(MIGRATION_PARAMETER_X_CHECKPOINT_DELAY),
  378. params->x_checkpoint_delay);
  379. assert(params->has_block_incremental);
  380. monitor_printf(mon, "%s: %s\n",
  381. MigrationParameter_str(MIGRATION_PARAMETER_BLOCK_INCREMENTAL),
  382. params->block_incremental ? "on" : "off");
  383. monitor_printf(mon, "%s: %u\n",
  384. MigrationParameter_str(MIGRATION_PARAMETER_X_MULTIFD_CHANNELS),
  385. params->x_multifd_channels);
  386. monitor_printf(mon, "%s: %u\n",
  387. MigrationParameter_str(MIGRATION_PARAMETER_X_MULTIFD_PAGE_COUNT),
  388. params->x_multifd_page_count);
  389. monitor_printf(mon, "%s: %" PRIu64 "\n",
  390. MigrationParameter_str(MIGRATION_PARAMETER_XBZRLE_CACHE_SIZE),
  391. params->xbzrle_cache_size);
  392. monitor_printf(mon, "%s: %" PRIu64 "\n",
  393. MigrationParameter_str(MIGRATION_PARAMETER_MAX_POSTCOPY_BANDWIDTH),
  394. params->max_postcopy_bandwidth);
  395. }
  396. qapi_free_MigrationParameters(params);
  397. }
  398. void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
  399. {
  400. monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
  401. qmp_query_migrate_cache_size(NULL) >> 10);
  402. }
  403. void hmp_info_cpus(Monitor *mon, const QDict *qdict)
  404. {
  405. CpuInfoFastList *cpu_list, *cpu;
  406. cpu_list = qmp_query_cpus_fast(NULL);
  407. for (cpu = cpu_list; cpu; cpu = cpu->next) {
  408. int active = ' ';
  409. if (cpu->value->cpu_index == monitor_get_cpu_index()) {
  410. active = '*';
  411. }
  412. monitor_printf(mon, "%c CPU #%" PRId64 ":", active,
  413. cpu->value->cpu_index);
  414. monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
  415. }
  416. qapi_free_CpuInfoFastList(cpu_list);
  417. }
  418. static void print_block_info(Monitor *mon, BlockInfo *info,
  419. BlockDeviceInfo *inserted, bool verbose)
  420. {
  421. ImageInfo *image_info;
  422. assert(!info || !info->has_inserted || info->inserted == inserted);
  423. if (info && *info->device) {
  424. monitor_printf(mon, "%s", info->device);
  425. if (inserted && inserted->has_node_name) {
  426. monitor_printf(mon, " (%s)", inserted->node_name);
  427. }
  428. } else {
  429. assert(info || inserted);
  430. monitor_printf(mon, "%s",
  431. inserted && inserted->has_node_name ? inserted->node_name
  432. : info && info->has_qdev ? info->qdev
  433. : "<anonymous>");
  434. }
  435. if (inserted) {
  436. monitor_printf(mon, ": %s (%s%s%s)\n",
  437. inserted->file,
  438. inserted->drv,
  439. inserted->ro ? ", read-only" : "",
  440. inserted->encrypted ? ", encrypted" : "");
  441. } else {
  442. monitor_printf(mon, ": [not inserted]\n");
  443. }
  444. if (info) {
  445. if (info->has_qdev) {
  446. monitor_printf(mon, " Attached to: %s\n", info->qdev);
  447. }
  448. if (info->has_io_status && info->io_status != BLOCK_DEVICE_IO_STATUS_OK) {
  449. monitor_printf(mon, " I/O status: %s\n",
  450. BlockDeviceIoStatus_str(info->io_status));
  451. }
  452. if (info->removable) {
  453. monitor_printf(mon, " Removable device: %slocked, tray %s\n",
  454. info->locked ? "" : "not ",
  455. info->tray_open ? "open" : "closed");
  456. }
  457. }
  458. if (!inserted) {
  459. return;
  460. }
  461. monitor_printf(mon, " Cache mode: %s%s%s\n",
  462. inserted->cache->writeback ? "writeback" : "writethrough",
  463. inserted->cache->direct ? ", direct" : "",
  464. inserted->cache->no_flush ? ", ignore flushes" : "");
  465. if (inserted->has_backing_file) {
  466. monitor_printf(mon,
  467. " Backing file: %s "
  468. "(chain depth: %" PRId64 ")\n",
  469. inserted->backing_file,
  470. inserted->backing_file_depth);
  471. }
  472. if (inserted->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF) {
  473. monitor_printf(mon, " Detect zeroes: %s\n",
  474. BlockdevDetectZeroesOptions_str(inserted->detect_zeroes));
  475. }
  476. if (inserted->bps || inserted->bps_rd || inserted->bps_wr ||
  477. inserted->iops || inserted->iops_rd || inserted->iops_wr)
  478. {
  479. monitor_printf(mon, " I/O throttling: bps=%" PRId64
  480. " bps_rd=%" PRId64 " bps_wr=%" PRId64
  481. " bps_max=%" PRId64
  482. " bps_rd_max=%" PRId64
  483. " bps_wr_max=%" PRId64
  484. " iops=%" PRId64 " iops_rd=%" PRId64
  485. " iops_wr=%" PRId64
  486. " iops_max=%" PRId64
  487. " iops_rd_max=%" PRId64
  488. " iops_wr_max=%" PRId64
  489. " iops_size=%" PRId64
  490. " group=%s\n",
  491. inserted->bps,
  492. inserted->bps_rd,
  493. inserted->bps_wr,
  494. inserted->bps_max,
  495. inserted->bps_rd_max,
  496. inserted->bps_wr_max,
  497. inserted->iops,
  498. inserted->iops_rd,
  499. inserted->iops_wr,
  500. inserted->iops_max,
  501. inserted->iops_rd_max,
  502. inserted->iops_wr_max,
  503. inserted->iops_size,
  504. inserted->group);
  505. }
  506. if (verbose) {
  507. monitor_printf(mon, "\nImages:\n");
  508. image_info = inserted->image;
  509. while (1) {
  510. bdrv_image_info_dump((fprintf_function)monitor_printf,
  511. mon, image_info);
  512. if (image_info->has_backing_image) {
  513. image_info = image_info->backing_image;
  514. } else {
  515. break;
  516. }
  517. }
  518. }
  519. }
  520. void hmp_info_block(Monitor *mon, const QDict *qdict)
  521. {
  522. BlockInfoList *block_list, *info;
  523. BlockDeviceInfoList *blockdev_list, *blockdev;
  524. const char *device = qdict_get_try_str(qdict, "device");
  525. bool verbose = qdict_get_try_bool(qdict, "verbose", false);
  526. bool nodes = qdict_get_try_bool(qdict, "nodes", false);
  527. bool printed = false;
  528. /* Print BlockBackend information */
  529. if (!nodes) {
  530. block_list = qmp_query_block(NULL);
  531. } else {
  532. block_list = NULL;
  533. }
  534. for (info = block_list; info; info = info->next) {
  535. if (device && strcmp(device, info->value->device)) {
  536. continue;
  537. }
  538. if (info != block_list) {
  539. monitor_printf(mon, "\n");
  540. }
  541. print_block_info(mon, info->value, info->value->has_inserted
  542. ? info->value->inserted : NULL,
  543. verbose);
  544. printed = true;
  545. }
  546. qapi_free_BlockInfoList(block_list);
  547. if ((!device && !nodes) || printed) {
  548. return;
  549. }
  550. /* Print node information */
  551. blockdev_list = qmp_query_named_block_nodes(NULL);
  552. for (blockdev = blockdev_list; blockdev; blockdev = blockdev->next) {
  553. assert(blockdev->value->has_node_name);
  554. if (device && strcmp(device, blockdev->value->node_name)) {
  555. continue;
  556. }
  557. if (blockdev != blockdev_list) {
  558. monitor_printf(mon, "\n");
  559. }
  560. print_block_info(mon, NULL, blockdev->value, verbose);
  561. }
  562. qapi_free_BlockDeviceInfoList(blockdev_list);
  563. }
  564. void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
  565. {
  566. BlockStatsList *stats_list, *stats;
  567. stats_list = qmp_query_blockstats(false, false, NULL);
  568. for (stats = stats_list; stats; stats = stats->next) {
  569. if (!stats->value->has_device) {
  570. continue;
  571. }
  572. monitor_printf(mon, "%s:", stats->value->device);
  573. monitor_printf(mon, " rd_bytes=%" PRId64
  574. " wr_bytes=%" PRId64
  575. " rd_operations=%" PRId64
  576. " wr_operations=%" PRId64
  577. " flush_operations=%" PRId64
  578. " wr_total_time_ns=%" PRId64
  579. " rd_total_time_ns=%" PRId64
  580. " flush_total_time_ns=%" PRId64
  581. " rd_merged=%" PRId64
  582. " wr_merged=%" PRId64
  583. " idle_time_ns=%" PRId64
  584. "\n",
  585. stats->value->stats->rd_bytes,
  586. stats->value->stats->wr_bytes,
  587. stats->value->stats->rd_operations,
  588. stats->value->stats->wr_operations,
  589. stats->value->stats->flush_operations,
  590. stats->value->stats->wr_total_time_ns,
  591. stats->value->stats->rd_total_time_ns,
  592. stats->value->stats->flush_total_time_ns,
  593. stats->value->stats->rd_merged,
  594. stats->value->stats->wr_merged,
  595. stats->value->stats->idle_time_ns);
  596. }
  597. qapi_free_BlockStatsList(stats_list);
  598. }
  599. #ifdef CONFIG_VNC
  600. /* Helper for hmp_info_vnc_clients, _servers */
  601. static void hmp_info_VncBasicInfo(Monitor *mon, VncBasicInfo *info,
  602. const char *name)
  603. {
  604. monitor_printf(mon, " %s: %s:%s (%s%s)\n",
  605. name,
  606. info->host,
  607. info->service,
  608. NetworkAddressFamily_str(info->family),
  609. info->websocket ? " (Websocket)" : "");
  610. }
  611. /* Helper displaying and auth and crypt info */
  612. static void hmp_info_vnc_authcrypt(Monitor *mon, const char *indent,
  613. VncPrimaryAuth auth,
  614. VncVencryptSubAuth *vencrypt)
  615. {
  616. monitor_printf(mon, "%sAuth: %s (Sub: %s)\n", indent,
  617. VncPrimaryAuth_str(auth),
  618. vencrypt ? VncVencryptSubAuth_str(*vencrypt) : "none");
  619. }
  620. static void hmp_info_vnc_clients(Monitor *mon, VncClientInfoList *client)
  621. {
  622. while (client) {
  623. VncClientInfo *cinfo = client->value;
  624. hmp_info_VncBasicInfo(mon, qapi_VncClientInfo_base(cinfo), "Client");
  625. monitor_printf(mon, " x509_dname: %s\n",
  626. cinfo->has_x509_dname ?
  627. cinfo->x509_dname : "none");
  628. monitor_printf(mon, " sasl_username: %s\n",
  629. cinfo->has_sasl_username ?
  630. cinfo->sasl_username : "none");
  631. client = client->next;
  632. }
  633. }
  634. static void hmp_info_vnc_servers(Monitor *mon, VncServerInfo2List *server)
  635. {
  636. while (server) {
  637. VncServerInfo2 *sinfo = server->value;
  638. hmp_info_VncBasicInfo(mon, qapi_VncServerInfo2_base(sinfo), "Server");
  639. hmp_info_vnc_authcrypt(mon, " ", sinfo->auth,
  640. sinfo->has_vencrypt ? &sinfo->vencrypt : NULL);
  641. server = server->next;
  642. }
  643. }
  644. void hmp_info_vnc(Monitor *mon, const QDict *qdict)
  645. {
  646. VncInfo2List *info2l;
  647. Error *err = NULL;
  648. info2l = qmp_query_vnc_servers(&err);
  649. if (err) {
  650. hmp_handle_error(mon, &err);
  651. return;
  652. }
  653. if (!info2l) {
  654. monitor_printf(mon, "None\n");
  655. return;
  656. }
  657. while (info2l) {
  658. VncInfo2 *info = info2l->value;
  659. monitor_printf(mon, "%s:\n", info->id);
  660. hmp_info_vnc_servers(mon, info->server);
  661. hmp_info_vnc_clients(mon, info->clients);
  662. if (!info->server) {
  663. /* The server entry displays its auth, we only
  664. * need to display in the case of 'reverse' connections
  665. * where there's no server.
  666. */
  667. hmp_info_vnc_authcrypt(mon, " ", info->auth,
  668. info->has_vencrypt ? &info->vencrypt : NULL);
  669. }
  670. if (info->has_display) {
  671. monitor_printf(mon, " Display: %s\n", info->display);
  672. }
  673. info2l = info2l->next;
  674. }
  675. qapi_free_VncInfo2List(info2l);
  676. }
  677. #endif
  678. #ifdef CONFIG_SPICE
  679. void hmp_info_spice(Monitor *mon, const QDict *qdict)
  680. {
  681. SpiceChannelList *chan;
  682. SpiceInfo *info;
  683. const char *channel_name;
  684. const char * const channel_names[] = {
  685. [SPICE_CHANNEL_MAIN] = "main",
  686. [SPICE_CHANNEL_DISPLAY] = "display",
  687. [SPICE_CHANNEL_INPUTS] = "inputs",
  688. [SPICE_CHANNEL_CURSOR] = "cursor",
  689. [SPICE_CHANNEL_PLAYBACK] = "playback",
  690. [SPICE_CHANNEL_RECORD] = "record",
  691. [SPICE_CHANNEL_TUNNEL] = "tunnel",
  692. [SPICE_CHANNEL_SMARTCARD] = "smartcard",
  693. [SPICE_CHANNEL_USBREDIR] = "usbredir",
  694. [SPICE_CHANNEL_PORT] = "port",
  695. #if 0
  696. /* minimum spice-protocol is 0.12.3, webdav was added in 0.12.7,
  697. * no easy way to #ifdef (SPICE_CHANNEL_* is a enum). Disable
  698. * as quick fix for build failures with older versions. */
  699. [SPICE_CHANNEL_WEBDAV] = "webdav",
  700. #endif
  701. };
  702. info = qmp_query_spice(NULL);
  703. if (!info->enabled) {
  704. monitor_printf(mon, "Server: disabled\n");
  705. goto out;
  706. }
  707. monitor_printf(mon, "Server:\n");
  708. if (info->has_port) {
  709. monitor_printf(mon, " address: %s:%" PRId64 "\n",
  710. info->host, info->port);
  711. }
  712. if (info->has_tls_port) {
  713. monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
  714. info->host, info->tls_port);
  715. }
  716. monitor_printf(mon, " migrated: %s\n",
  717. info->migrated ? "true" : "false");
  718. monitor_printf(mon, " auth: %s\n", info->auth);
  719. monitor_printf(mon, " compiled: %s\n", info->compiled_version);
  720. monitor_printf(mon, " mouse-mode: %s\n",
  721. SpiceQueryMouseMode_str(info->mouse_mode));
  722. if (!info->has_channels || info->channels == NULL) {
  723. monitor_printf(mon, "Channels: none\n");
  724. } else {
  725. for (chan = info->channels; chan; chan = chan->next) {
  726. monitor_printf(mon, "Channel:\n");
  727. monitor_printf(mon, " address: %s:%s%s\n",
  728. chan->value->host, chan->value->port,
  729. chan->value->tls ? " [tls]" : "");
  730. monitor_printf(mon, " session: %" PRId64 "\n",
  731. chan->value->connection_id);
  732. monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
  733. chan->value->channel_type, chan->value->channel_id);
  734. channel_name = "unknown";
  735. if (chan->value->channel_type > 0 &&
  736. chan->value->channel_type < ARRAY_SIZE(channel_names) &&
  737. channel_names[chan->value->channel_type]) {
  738. channel_name = channel_names[chan->value->channel_type];
  739. }
  740. monitor_printf(mon, " channel name: %s\n", channel_name);
  741. }
  742. }
  743. out:
  744. qapi_free_SpiceInfo(info);
  745. }
  746. #endif
  747. void hmp_info_balloon(Monitor *mon, const QDict *qdict)
  748. {
  749. BalloonInfo *info;
  750. Error *err = NULL;
  751. info = qmp_query_balloon(&err);
  752. if (err) {
  753. hmp_handle_error(mon, &err);
  754. return;
  755. }
  756. monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
  757. qapi_free_BalloonInfo(info);
  758. }
  759. static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
  760. {
  761. PciMemoryRegionList *region;
  762. monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
  763. monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
  764. dev->slot, dev->function);
  765. monitor_printf(mon, " ");
  766. if (dev->class_info->has_desc) {
  767. monitor_printf(mon, "%s", dev->class_info->desc);
  768. } else {
  769. monitor_printf(mon, "Class %04" PRId64, dev->class_info->q_class);
  770. }
  771. monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
  772. dev->id->vendor, dev->id->device);
  773. if (dev->id->has_subsystem_vendor && dev->id->has_subsystem) {
  774. monitor_printf(mon, " PCI subsystem %04" PRIx64 ":%04" PRIx64 "\n",
  775. dev->id->subsystem_vendor, dev->id->subsystem);
  776. }
  777. if (dev->has_irq) {
  778. monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
  779. }
  780. if (dev->has_pci_bridge) {
  781. monitor_printf(mon, " BUS %" PRId64 ".\n",
  782. dev->pci_bridge->bus->number);
  783. monitor_printf(mon, " secondary bus %" PRId64 ".\n",
  784. dev->pci_bridge->bus->secondary);
  785. monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
  786. dev->pci_bridge->bus->subordinate);
  787. monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
  788. dev->pci_bridge->bus->io_range->base,
  789. dev->pci_bridge->bus->io_range->limit);
  790. monitor_printf(mon,
  791. " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
  792. dev->pci_bridge->bus->memory_range->base,
  793. dev->pci_bridge->bus->memory_range->limit);
  794. monitor_printf(mon, " prefetchable memory range "
  795. "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
  796. dev->pci_bridge->bus->prefetchable_range->base,
  797. dev->pci_bridge->bus->prefetchable_range->limit);
  798. }
  799. for (region = dev->regions; region; region = region->next) {
  800. uint64_t addr, size;
  801. addr = region->value->address;
  802. size = region->value->size;
  803. monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
  804. if (!strcmp(region->value->type, "io")) {
  805. monitor_printf(mon, "I/O at 0x%04" PRIx64
  806. " [0x%04" PRIx64 "].\n",
  807. addr, addr + size - 1);
  808. } else {
  809. monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
  810. " [0x%08" PRIx64 "].\n",
  811. region->value->mem_type_64 ? 64 : 32,
  812. region->value->prefetch ? " prefetchable" : "",
  813. addr, addr + size - 1);
  814. }
  815. }
  816. monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
  817. if (dev->has_pci_bridge) {
  818. if (dev->pci_bridge->has_devices) {
  819. PciDeviceInfoList *cdev;
  820. for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
  821. hmp_info_pci_device(mon, cdev->value);
  822. }
  823. }
  824. }
  825. }
  826. static int hmp_info_irq_foreach(Object *obj, void *opaque)
  827. {
  828. InterruptStatsProvider *intc;
  829. InterruptStatsProviderClass *k;
  830. Monitor *mon = opaque;
  831. if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
  832. intc = INTERRUPT_STATS_PROVIDER(obj);
  833. k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
  834. uint64_t *irq_counts;
  835. unsigned int nb_irqs, i;
  836. if (k->get_statistics &&
  837. k->get_statistics(intc, &irq_counts, &nb_irqs)) {
  838. if (nb_irqs > 0) {
  839. monitor_printf(mon, "IRQ statistics for %s:\n",
  840. object_get_typename(obj));
  841. for (i = 0; i < nb_irqs; i++) {
  842. if (irq_counts[i] > 0) {
  843. monitor_printf(mon, "%2d: %" PRId64 "\n", i,
  844. irq_counts[i]);
  845. }
  846. }
  847. }
  848. } else {
  849. monitor_printf(mon, "IRQ statistics not available for %s.\n",
  850. object_get_typename(obj));
  851. }
  852. }
  853. return 0;
  854. }
  855. void hmp_info_irq(Monitor *mon, const QDict *qdict)
  856. {
  857. object_child_foreach_recursive(object_get_root(),
  858. hmp_info_irq_foreach, mon);
  859. }
  860. static int hmp_info_pic_foreach(Object *obj, void *opaque)
  861. {
  862. InterruptStatsProvider *intc;
  863. InterruptStatsProviderClass *k;
  864. Monitor *mon = opaque;
  865. if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
  866. intc = INTERRUPT_STATS_PROVIDER(obj);
  867. k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
  868. if (k->print_info) {
  869. k->print_info(intc, mon);
  870. } else {
  871. monitor_printf(mon, "Interrupt controller information not available for %s.\n",
  872. object_get_typename(obj));
  873. }
  874. }
  875. return 0;
  876. }
  877. void hmp_info_pic(Monitor *mon, const QDict *qdict)
  878. {
  879. object_child_foreach_recursive(object_get_root(),
  880. hmp_info_pic_foreach, mon);
  881. }
  882. void hmp_info_pci(Monitor *mon, const QDict *qdict)
  883. {
  884. PciInfoList *info_list, *info;
  885. Error *err = NULL;
  886. info_list = qmp_query_pci(&err);
  887. if (err) {
  888. monitor_printf(mon, "PCI devices not supported\n");
  889. error_free(err);
  890. return;
  891. }
  892. for (info = info_list; info; info = info->next) {
  893. PciDeviceInfoList *dev;
  894. for (dev = info->value->devices; dev; dev = dev->next) {
  895. hmp_info_pci_device(mon, dev->value);
  896. }
  897. }
  898. qapi_free_PciInfoList(info_list);
  899. }
  900. void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
  901. {
  902. BlockJobInfoList *list;
  903. Error *err = NULL;
  904. list = qmp_query_block_jobs(&err);
  905. assert(!err);
  906. if (!list) {
  907. monitor_printf(mon, "No active jobs\n");
  908. return;
  909. }
  910. while (list) {
  911. if (strcmp(list->value->type, "stream") == 0) {
  912. monitor_printf(mon, "Streaming device %s: Completed %" PRId64
  913. " of %" PRId64 " bytes, speed limit %" PRId64
  914. " bytes/s\n",
  915. list->value->device,
  916. list->value->offset,
  917. list->value->len,
  918. list->value->speed);
  919. } else {
  920. monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
  921. " of %" PRId64 " bytes, speed limit %" PRId64
  922. " bytes/s\n",
  923. list->value->type,
  924. list->value->device,
  925. list->value->offset,
  926. list->value->len,
  927. list->value->speed);
  928. }
  929. list = list->next;
  930. }
  931. qapi_free_BlockJobInfoList(list);
  932. }
  933. void hmp_info_tpm(Monitor *mon, const QDict *qdict)
  934. {
  935. TPMInfoList *info_list, *info;
  936. Error *err = NULL;
  937. unsigned int c = 0;
  938. TPMPassthroughOptions *tpo;
  939. TPMEmulatorOptions *teo;
  940. info_list = qmp_query_tpm(&err);
  941. if (err) {
  942. monitor_printf(mon, "TPM device not supported\n");
  943. error_free(err);
  944. return;
  945. }
  946. if (info_list) {
  947. monitor_printf(mon, "TPM device:\n");
  948. }
  949. for (info = info_list; info; info = info->next) {
  950. TPMInfo *ti = info->value;
  951. monitor_printf(mon, " tpm%d: model=%s\n",
  952. c, TpmModel_str(ti->model));
  953. monitor_printf(mon, " \\ %s: type=%s",
  954. ti->id, TpmTypeOptionsKind_str(ti->options->type));
  955. switch (ti->options->type) {
  956. case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
  957. tpo = ti->options->u.passthrough.data;
  958. monitor_printf(mon, "%s%s%s%s",
  959. tpo->has_path ? ",path=" : "",
  960. tpo->has_path ? tpo->path : "",
  961. tpo->has_cancel_path ? ",cancel-path=" : "",
  962. tpo->has_cancel_path ? tpo->cancel_path : "");
  963. break;
  964. case TPM_TYPE_OPTIONS_KIND_EMULATOR:
  965. teo = ti->options->u.emulator.data;
  966. monitor_printf(mon, ",chardev=%s", teo->chardev);
  967. break;
  968. case TPM_TYPE_OPTIONS_KIND__MAX:
  969. break;
  970. }
  971. monitor_printf(mon, "\n");
  972. c++;
  973. }
  974. qapi_free_TPMInfoList(info_list);
  975. }
  976. void hmp_quit(Monitor *mon, const QDict *qdict)
  977. {
  978. monitor_suspend(mon);
  979. qmp_quit(NULL);
  980. }
  981. void hmp_stop(Monitor *mon, const QDict *qdict)
  982. {
  983. qmp_stop(NULL);
  984. }
  985. void hmp_sync_profile(Monitor *mon, const QDict *qdict)
  986. {
  987. const char *op = qdict_get_try_str(qdict, "op");
  988. if (op == NULL) {
  989. bool on = qsp_is_enabled();
  990. monitor_printf(mon, "sync-profile is %s\n", on ? "on" : "off");
  991. return;
  992. }
  993. if (!strcmp(op, "on")) {
  994. qsp_enable();
  995. } else if (!strcmp(op, "off")) {
  996. qsp_disable();
  997. } else if (!strcmp(op, "reset")) {
  998. qsp_reset();
  999. } else {
  1000. Error *err = NULL;
  1001. error_setg(&err, QERR_INVALID_PARAMETER, op);
  1002. hmp_handle_error(mon, &err);
  1003. }
  1004. }
  1005. void hmp_system_reset(Monitor *mon, const QDict *qdict)
  1006. {
  1007. qmp_system_reset(NULL);
  1008. }
  1009. void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
  1010. {
  1011. qmp_system_powerdown(NULL);
  1012. }
  1013. void hmp_exit_preconfig(Monitor *mon, const QDict *qdict)
  1014. {
  1015. Error *err = NULL;
  1016. qmp_x_exit_preconfig(&err);
  1017. hmp_handle_error(mon, &err);
  1018. }
  1019. void hmp_cpu(Monitor *mon, const QDict *qdict)
  1020. {
  1021. int64_t cpu_index;
  1022. /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
  1023. use it are converted to the QAPI */
  1024. cpu_index = qdict_get_int(qdict, "index");
  1025. if (monitor_set_cpu(cpu_index) < 0) {
  1026. monitor_printf(mon, "invalid CPU index\n");
  1027. }
  1028. }
  1029. void hmp_memsave(Monitor *mon, const QDict *qdict)
  1030. {
  1031. uint32_t size = qdict_get_int(qdict, "size");
  1032. const char *filename = qdict_get_str(qdict, "filename");
  1033. uint64_t addr = qdict_get_int(qdict, "val");
  1034. Error *err = NULL;
  1035. int cpu_index = monitor_get_cpu_index();
  1036. if (cpu_index < 0) {
  1037. monitor_printf(mon, "No CPU available\n");
  1038. return;
  1039. }
  1040. qmp_memsave(addr, size, filename, true, cpu_index, &err);
  1041. hmp_handle_error(mon, &err);
  1042. }
  1043. void hmp_pmemsave(Monitor *mon, const QDict *qdict)
  1044. {
  1045. uint32_t size = qdict_get_int(qdict, "size");
  1046. const char *filename = qdict_get_str(qdict, "filename");
  1047. uint64_t addr = qdict_get_int(qdict, "val");
  1048. Error *err = NULL;
  1049. qmp_pmemsave(addr, size, filename, &err);
  1050. hmp_handle_error(mon, &err);
  1051. }
  1052. void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
  1053. {
  1054. const char *chardev = qdict_get_str(qdict, "device");
  1055. const char *data = qdict_get_str(qdict, "data");
  1056. Error *err = NULL;
  1057. qmp_ringbuf_write(chardev, data, false, 0, &err);
  1058. hmp_handle_error(mon, &err);
  1059. }
  1060. void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
  1061. {
  1062. uint32_t size = qdict_get_int(qdict, "size");
  1063. const char *chardev = qdict_get_str(qdict, "device");
  1064. char *data;
  1065. Error *err = NULL;
  1066. int i;
  1067. data = qmp_ringbuf_read(chardev, size, false, 0, &err);
  1068. if (err) {
  1069. hmp_handle_error(mon, &err);
  1070. return;
  1071. }
  1072. for (i = 0; data[i]; i++) {
  1073. unsigned char ch = data[i];
  1074. if (ch == '\\') {
  1075. monitor_printf(mon, "\\\\");
  1076. } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
  1077. monitor_printf(mon, "\\u%04X", ch);
  1078. } else {
  1079. monitor_printf(mon, "%c", ch);
  1080. }
  1081. }
  1082. monitor_printf(mon, "\n");
  1083. g_free(data);
  1084. }
  1085. void hmp_cont(Monitor *mon, const QDict *qdict)
  1086. {
  1087. Error *err = NULL;
  1088. qmp_cont(&err);
  1089. hmp_handle_error(mon, &err);
  1090. }
  1091. void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
  1092. {
  1093. Error *err = NULL;
  1094. qmp_system_wakeup(&err);
  1095. hmp_handle_error(mon, &err);
  1096. }
  1097. void hmp_nmi(Monitor *mon, const QDict *qdict)
  1098. {
  1099. Error *err = NULL;
  1100. qmp_inject_nmi(&err);
  1101. hmp_handle_error(mon, &err);
  1102. }
  1103. void hmp_set_link(Monitor *mon, const QDict *qdict)
  1104. {
  1105. const char *name = qdict_get_str(qdict, "name");
  1106. bool up = qdict_get_bool(qdict, "up");
  1107. Error *err = NULL;
  1108. qmp_set_link(name, up, &err);
  1109. hmp_handle_error(mon, &err);
  1110. }
  1111. void hmp_block_passwd(Monitor *mon, const QDict *qdict)
  1112. {
  1113. const char *device = qdict_get_str(qdict, "device");
  1114. const char *password = qdict_get_str(qdict, "password");
  1115. Error *err = NULL;
  1116. qmp_block_passwd(true, device, false, NULL, password, &err);
  1117. hmp_handle_error(mon, &err);
  1118. }
  1119. void hmp_balloon(Monitor *mon, const QDict *qdict)
  1120. {
  1121. int64_t value = qdict_get_int(qdict, "value");
  1122. Error *err = NULL;
  1123. qmp_balloon(value, &err);
  1124. hmp_handle_error(mon, &err);
  1125. }
  1126. void hmp_block_resize(Monitor *mon, const QDict *qdict)
  1127. {
  1128. const char *device = qdict_get_str(qdict, "device");
  1129. int64_t size = qdict_get_int(qdict, "size");
  1130. Error *err = NULL;
  1131. qmp_block_resize(true, device, false, NULL, size, &err);
  1132. hmp_handle_error(mon, &err);
  1133. }
  1134. void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
  1135. {
  1136. const char *filename = qdict_get_str(qdict, "target");
  1137. const char *format = qdict_get_try_str(qdict, "format");
  1138. bool reuse = qdict_get_try_bool(qdict, "reuse", false);
  1139. bool full = qdict_get_try_bool(qdict, "full", false);
  1140. Error *err = NULL;
  1141. DriveMirror mirror = {
  1142. .device = (char *)qdict_get_str(qdict, "device"),
  1143. .target = (char *)filename,
  1144. .has_format = !!format,
  1145. .format = (char *)format,
  1146. .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
  1147. .has_mode = true,
  1148. .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
  1149. .unmap = true,
  1150. };
  1151. if (!filename) {
  1152. error_setg(&err, QERR_MISSING_PARAMETER, "target");
  1153. hmp_handle_error(mon, &err);
  1154. return;
  1155. }
  1156. qmp_drive_mirror(&mirror, &err);
  1157. hmp_handle_error(mon, &err);
  1158. }
  1159. void hmp_drive_backup(Monitor *mon, const QDict *qdict)
  1160. {
  1161. const char *device = qdict_get_str(qdict, "device");
  1162. const char *filename = qdict_get_str(qdict, "target");
  1163. const char *format = qdict_get_try_str(qdict, "format");
  1164. bool reuse = qdict_get_try_bool(qdict, "reuse", false);
  1165. bool full = qdict_get_try_bool(qdict, "full", false);
  1166. bool compress = qdict_get_try_bool(qdict, "compress", false);
  1167. Error *err = NULL;
  1168. DriveBackup backup = {
  1169. .device = (char *)device,
  1170. .target = (char *)filename,
  1171. .has_format = !!format,
  1172. .format = (char *)format,
  1173. .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
  1174. .has_mode = true,
  1175. .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
  1176. .has_compress = !!compress,
  1177. .compress = compress,
  1178. };
  1179. if (!filename) {
  1180. error_setg(&err, QERR_MISSING_PARAMETER, "target");
  1181. hmp_handle_error(mon, &err);
  1182. return;
  1183. }
  1184. qmp_drive_backup(&backup, &err);
  1185. hmp_handle_error(mon, &err);
  1186. }
  1187. void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
  1188. {
  1189. const char *device = qdict_get_str(qdict, "device");
  1190. const char *filename = qdict_get_try_str(qdict, "snapshot-file");
  1191. const char *format = qdict_get_try_str(qdict, "format");
  1192. bool reuse = qdict_get_try_bool(qdict, "reuse", false);
  1193. enum NewImageMode mode;
  1194. Error *err = NULL;
  1195. if (!filename) {
  1196. /* In the future, if 'snapshot-file' is not specified, the snapshot
  1197. will be taken internally. Today it's actually required. */
  1198. error_setg(&err, QERR_MISSING_PARAMETER, "snapshot-file");
  1199. hmp_handle_error(mon, &err);
  1200. return;
  1201. }
  1202. mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
  1203. qmp_blockdev_snapshot_sync(true, device, false, NULL,
  1204. filename, false, NULL,
  1205. !!format, format,
  1206. true, mode, &err);
  1207. hmp_handle_error(mon, &err);
  1208. }
  1209. void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict)
  1210. {
  1211. const char *device = qdict_get_str(qdict, "device");
  1212. const char *name = qdict_get_str(qdict, "name");
  1213. Error *err = NULL;
  1214. qmp_blockdev_snapshot_internal_sync(device, name, &err);
  1215. hmp_handle_error(mon, &err);
  1216. }
  1217. void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict)
  1218. {
  1219. const char *device = qdict_get_str(qdict, "device");
  1220. const char *name = qdict_get_str(qdict, "name");
  1221. const char *id = qdict_get_try_str(qdict, "id");
  1222. Error *err = NULL;
  1223. qmp_blockdev_snapshot_delete_internal_sync(device, !!id, id,
  1224. true, name, &err);
  1225. hmp_handle_error(mon, &err);
  1226. }
  1227. void hmp_loadvm(Monitor *mon, const QDict *qdict)
  1228. {
  1229. int saved_vm_running = runstate_is_running();
  1230. const char *name = qdict_get_str(qdict, "name");
  1231. Error *err = NULL;
  1232. vm_stop(RUN_STATE_RESTORE_VM);
  1233. if (load_snapshot(name, &err) == 0 && saved_vm_running) {
  1234. vm_start();
  1235. }
  1236. hmp_handle_error(mon, &err);
  1237. }
  1238. void hmp_savevm(Monitor *mon, const QDict *qdict)
  1239. {
  1240. Error *err = NULL;
  1241. save_snapshot(qdict_get_try_str(qdict, "name"), &err);
  1242. hmp_handle_error(mon, &err);
  1243. }
  1244. void hmp_delvm(Monitor *mon, const QDict *qdict)
  1245. {
  1246. BlockDriverState *bs;
  1247. Error *err = NULL;
  1248. const char *name = qdict_get_str(qdict, "name");
  1249. if (bdrv_all_delete_snapshot(name, &bs, &err) < 0) {
  1250. error_reportf_err(err,
  1251. "Error while deleting snapshot on device '%s': ",
  1252. bdrv_get_device_name(bs));
  1253. }
  1254. }
  1255. void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
  1256. {
  1257. BlockDriverState *bs, *bs1;
  1258. BdrvNextIterator it1;
  1259. QEMUSnapshotInfo *sn_tab, *sn;
  1260. bool no_snapshot = true;
  1261. int nb_sns, i;
  1262. int total;
  1263. int *global_snapshots;
  1264. AioContext *aio_context;
  1265. typedef struct SnapshotEntry {
  1266. QEMUSnapshotInfo sn;
  1267. QTAILQ_ENTRY(SnapshotEntry) next;
  1268. } SnapshotEntry;
  1269. typedef struct ImageEntry {
  1270. const char *imagename;
  1271. QTAILQ_ENTRY(ImageEntry) next;
  1272. QTAILQ_HEAD(, SnapshotEntry) snapshots;
  1273. } ImageEntry;
  1274. QTAILQ_HEAD(, ImageEntry) image_list =
  1275. QTAILQ_HEAD_INITIALIZER(image_list);
  1276. ImageEntry *image_entry, *next_ie;
  1277. SnapshotEntry *snapshot_entry;
  1278. bs = bdrv_all_find_vmstate_bs();
  1279. if (!bs) {
  1280. monitor_printf(mon, "No available block device supports snapshots\n");
  1281. return;
  1282. }
  1283. aio_context = bdrv_get_aio_context(bs);
  1284. aio_context_acquire(aio_context);
  1285. nb_sns = bdrv_snapshot_list(bs, &sn_tab);
  1286. aio_context_release(aio_context);
  1287. if (nb_sns < 0) {
  1288. monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
  1289. return;
  1290. }
  1291. for (bs1 = bdrv_first(&it1); bs1; bs1 = bdrv_next(&it1)) {
  1292. int bs1_nb_sns = 0;
  1293. ImageEntry *ie;
  1294. SnapshotEntry *se;
  1295. AioContext *ctx = bdrv_get_aio_context(bs1);
  1296. aio_context_acquire(ctx);
  1297. if (bdrv_can_snapshot(bs1)) {
  1298. sn = NULL;
  1299. bs1_nb_sns = bdrv_snapshot_list(bs1, &sn);
  1300. if (bs1_nb_sns > 0) {
  1301. no_snapshot = false;
  1302. ie = g_new0(ImageEntry, 1);
  1303. ie->imagename = bdrv_get_device_name(bs1);
  1304. QTAILQ_INIT(&ie->snapshots);
  1305. QTAILQ_INSERT_TAIL(&image_list, ie, next);
  1306. for (i = 0; i < bs1_nb_sns; i++) {
  1307. se = g_new0(SnapshotEntry, 1);
  1308. se->sn = sn[i];
  1309. QTAILQ_INSERT_TAIL(&ie->snapshots, se, next);
  1310. }
  1311. }
  1312. g_free(sn);
  1313. }
  1314. aio_context_release(ctx);
  1315. }
  1316. if (no_snapshot) {
  1317. monitor_printf(mon, "There is no snapshot available.\n");
  1318. return;
  1319. }
  1320. global_snapshots = g_new0(int, nb_sns);
  1321. total = 0;
  1322. for (i = 0; i < nb_sns; i++) {
  1323. SnapshotEntry *next_sn;
  1324. if (bdrv_all_find_snapshot(sn_tab[i].name, &bs1) == 0) {
  1325. global_snapshots[total] = i;
  1326. total++;
  1327. QTAILQ_FOREACH(image_entry, &image_list, next) {
  1328. QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots,
  1329. next, next_sn) {
  1330. if (!strcmp(sn_tab[i].name, snapshot_entry->sn.name)) {
  1331. QTAILQ_REMOVE(&image_entry->snapshots, snapshot_entry,
  1332. next);
  1333. g_free(snapshot_entry);
  1334. }
  1335. }
  1336. }
  1337. }
  1338. }
  1339. monitor_printf(mon, "List of snapshots present on all disks:\n");
  1340. if (total > 0) {
  1341. bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
  1342. monitor_printf(mon, "\n");
  1343. for (i = 0; i < total; i++) {
  1344. sn = &sn_tab[global_snapshots[i]];
  1345. /* The ID is not guaranteed to be the same on all images, so
  1346. * overwrite it.
  1347. */
  1348. pstrcpy(sn->id_str, sizeof(sn->id_str), "--");
  1349. bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn);
  1350. monitor_printf(mon, "\n");
  1351. }
  1352. } else {
  1353. monitor_printf(mon, "None\n");
  1354. }
  1355. QTAILQ_FOREACH(image_entry, &image_list, next) {
  1356. if (QTAILQ_EMPTY(&image_entry->snapshots)) {
  1357. continue;
  1358. }
  1359. monitor_printf(mon,
  1360. "\nList of partial (non-loadable) snapshots on '%s':\n",
  1361. image_entry->imagename);
  1362. bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
  1363. monitor_printf(mon, "\n");
  1364. QTAILQ_FOREACH(snapshot_entry, &image_entry->snapshots, next) {
  1365. bdrv_snapshot_dump((fprintf_function)monitor_printf, mon,
  1366. &snapshot_entry->sn);
  1367. monitor_printf(mon, "\n");
  1368. }
  1369. }
  1370. QTAILQ_FOREACH_SAFE(image_entry, &image_list, next, next_ie) {
  1371. SnapshotEntry *next_sn;
  1372. QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots, next,
  1373. next_sn) {
  1374. g_free(snapshot_entry);
  1375. }
  1376. g_free(image_entry);
  1377. }
  1378. g_free(sn_tab);
  1379. g_free(global_snapshots);
  1380. }
  1381. void hmp_announce_self(Monitor *mon, const QDict *qdict)
  1382. {
  1383. qmp_announce_self(migrate_announce_params(), NULL);
  1384. }
  1385. void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
  1386. {
  1387. qmp_migrate_cancel(NULL);
  1388. }
  1389. void hmp_migrate_continue(Monitor *mon, const QDict *qdict)
  1390. {
  1391. Error *err = NULL;
  1392. const char *state = qdict_get_str(qdict, "state");
  1393. int val = qapi_enum_parse(&MigrationStatus_lookup, state, -1, &err);
  1394. if (val >= 0) {
  1395. qmp_migrate_continue(val, &err);
  1396. }
  1397. hmp_handle_error(mon, &err);
  1398. }
  1399. void hmp_migrate_incoming(Monitor *mon, const QDict *qdict)
  1400. {
  1401. Error *err = NULL;
  1402. const char *uri = qdict_get_str(qdict, "uri");
  1403. qmp_migrate_incoming(uri, &err);
  1404. hmp_handle_error(mon, &err);
  1405. }
  1406. void hmp_migrate_recover(Monitor *mon, const QDict *qdict)
  1407. {
  1408. Error *err = NULL;
  1409. const char *uri = qdict_get_str(qdict, "uri");
  1410. qmp_migrate_recover(uri, &err);
  1411. hmp_handle_error(mon, &err);
  1412. }
  1413. void hmp_migrate_pause(Monitor *mon, const QDict *qdict)
  1414. {
  1415. Error *err = NULL;
  1416. qmp_migrate_pause(&err);
  1417. hmp_handle_error(mon, &err);
  1418. }
  1419. /* Kept for backwards compatibility */
  1420. void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
  1421. {
  1422. double value = qdict_get_double(qdict, "value");
  1423. qmp_migrate_set_downtime(value, NULL);
  1424. }
  1425. void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
  1426. {
  1427. int64_t value = qdict_get_int(qdict, "value");
  1428. Error *err = NULL;
  1429. qmp_migrate_set_cache_size(value, &err);
  1430. hmp_handle_error(mon, &err);
  1431. }
  1432. /* Kept for backwards compatibility */
  1433. void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
  1434. {
  1435. int64_t value = qdict_get_int(qdict, "value");
  1436. qmp_migrate_set_speed(value, NULL);
  1437. }
  1438. void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
  1439. {
  1440. const char *cap = qdict_get_str(qdict, "capability");
  1441. bool state = qdict_get_bool(qdict, "state");
  1442. Error *err = NULL;
  1443. MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
  1444. int val;
  1445. val = qapi_enum_parse(&MigrationCapability_lookup, cap, -1, &err);
  1446. if (val < 0) {
  1447. goto end;
  1448. }
  1449. caps->value = g_malloc0(sizeof(*caps->value));
  1450. caps->value->capability = val;
  1451. caps->value->state = state;
  1452. caps->next = NULL;
  1453. qmp_migrate_set_capabilities(caps, &err);
  1454. end:
  1455. qapi_free_MigrationCapabilityStatusList(caps);
  1456. hmp_handle_error(mon, &err);
  1457. }
  1458. void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
  1459. {
  1460. const char *param = qdict_get_str(qdict, "parameter");
  1461. const char *valuestr = qdict_get_str(qdict, "value");
  1462. Visitor *v = string_input_visitor_new(valuestr);
  1463. MigrateSetParameters *p = g_new0(MigrateSetParameters, 1);
  1464. uint64_t valuebw = 0;
  1465. uint64_t cache_size;
  1466. Error *err = NULL;
  1467. int val, ret;
  1468. val = qapi_enum_parse(&MigrationParameter_lookup, param, -1, &err);
  1469. if (val < 0) {
  1470. goto cleanup;
  1471. }
  1472. switch (val) {
  1473. case MIGRATION_PARAMETER_COMPRESS_LEVEL:
  1474. p->has_compress_level = true;
  1475. visit_type_int(v, param, &p->compress_level, &err);
  1476. break;
  1477. case MIGRATION_PARAMETER_COMPRESS_THREADS:
  1478. p->has_compress_threads = true;
  1479. visit_type_int(v, param, &p->compress_threads, &err);
  1480. break;
  1481. case MIGRATION_PARAMETER_COMPRESS_WAIT_THREAD:
  1482. p->has_compress_wait_thread = true;
  1483. visit_type_bool(v, param, &p->compress_wait_thread, &err);
  1484. break;
  1485. case MIGRATION_PARAMETER_DECOMPRESS_THREADS:
  1486. p->has_decompress_threads = true;
  1487. visit_type_int(v, param, &p->decompress_threads, &err);
  1488. break;
  1489. case MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL:
  1490. p->has_cpu_throttle_initial = true;
  1491. visit_type_int(v, param, &p->cpu_throttle_initial, &err);
  1492. break;
  1493. case MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT:
  1494. p->has_cpu_throttle_increment = true;
  1495. visit_type_int(v, param, &p->cpu_throttle_increment, &err);
  1496. break;
  1497. case MIGRATION_PARAMETER_MAX_CPU_THROTTLE:
  1498. p->has_max_cpu_throttle = true;
  1499. visit_type_int(v, param, &p->max_cpu_throttle, &err);
  1500. break;
  1501. case MIGRATION_PARAMETER_TLS_CREDS:
  1502. p->has_tls_creds = true;
  1503. p->tls_creds = g_new0(StrOrNull, 1);
  1504. p->tls_creds->type = QTYPE_QSTRING;
  1505. visit_type_str(v, param, &p->tls_creds->u.s, &err);
  1506. break;
  1507. case MIGRATION_PARAMETER_TLS_HOSTNAME:
  1508. p->has_tls_hostname = true;
  1509. p->tls_hostname = g_new0(StrOrNull, 1);
  1510. p->tls_hostname->type = QTYPE_QSTRING;
  1511. visit_type_str(v, param, &p->tls_hostname->u.s, &err);
  1512. break;
  1513. case MIGRATION_PARAMETER_MAX_BANDWIDTH:
  1514. p->has_max_bandwidth = true;
  1515. /*
  1516. * Can't use visit_type_size() here, because it
  1517. * defaults to Bytes rather than Mebibytes.
  1518. */
  1519. ret = qemu_strtosz_MiB(valuestr, NULL, &valuebw);
  1520. if (ret < 0 || valuebw > INT64_MAX
  1521. || (size_t)valuebw != valuebw) {
  1522. error_setg(&err, "Invalid size %s", valuestr);
  1523. break;
  1524. }
  1525. p->max_bandwidth = valuebw;
  1526. break;
  1527. case MIGRATION_PARAMETER_DOWNTIME_LIMIT:
  1528. p->has_downtime_limit = true;
  1529. visit_type_int(v, param, &p->downtime_limit, &err);
  1530. break;
  1531. case MIGRATION_PARAMETER_X_CHECKPOINT_DELAY:
  1532. p->has_x_checkpoint_delay = true;
  1533. visit_type_int(v, param, &p->x_checkpoint_delay, &err);
  1534. break;
  1535. case MIGRATION_PARAMETER_BLOCK_INCREMENTAL:
  1536. p->has_block_incremental = true;
  1537. visit_type_bool(v, param, &p->block_incremental, &err);
  1538. break;
  1539. case MIGRATION_PARAMETER_X_MULTIFD_CHANNELS:
  1540. p->has_x_multifd_channels = true;
  1541. visit_type_int(v, param, &p->x_multifd_channels, &err);
  1542. break;
  1543. case MIGRATION_PARAMETER_X_MULTIFD_PAGE_COUNT:
  1544. p->has_x_multifd_page_count = true;
  1545. visit_type_int(v, param, &p->x_multifd_page_count, &err);
  1546. break;
  1547. case MIGRATION_PARAMETER_XBZRLE_CACHE_SIZE:
  1548. p->has_xbzrle_cache_size = true;
  1549. visit_type_size(v, param, &cache_size, &err);
  1550. if (err || cache_size > INT64_MAX
  1551. || (size_t)cache_size != cache_size) {
  1552. error_setg(&err, "Invalid size %s", valuestr);
  1553. break;
  1554. }
  1555. p->xbzrle_cache_size = cache_size;
  1556. break;
  1557. case MIGRATION_PARAMETER_MAX_POSTCOPY_BANDWIDTH:
  1558. p->has_max_postcopy_bandwidth = true;
  1559. visit_type_size(v, param, &p->max_postcopy_bandwidth, &err);
  1560. break;
  1561. case MIGRATION_PARAMETER_ANNOUNCE_INITIAL:
  1562. p->has_announce_initial = true;
  1563. visit_type_size(v, param, &p->announce_initial, &err);
  1564. break;
  1565. case MIGRATION_PARAMETER_ANNOUNCE_MAX:
  1566. p->has_announce_max = true;
  1567. visit_type_size(v, param, &p->announce_max, &err);
  1568. break;
  1569. case MIGRATION_PARAMETER_ANNOUNCE_ROUNDS:
  1570. p->has_announce_rounds = true;
  1571. visit_type_size(v, param, &p->announce_rounds, &err);
  1572. break;
  1573. case MIGRATION_PARAMETER_ANNOUNCE_STEP:
  1574. p->has_announce_step = true;
  1575. visit_type_size(v, param, &p->announce_step, &err);
  1576. break;
  1577. default:
  1578. assert(0);
  1579. }
  1580. if (err) {
  1581. goto cleanup;
  1582. }
  1583. qmp_migrate_set_parameters(p, &err);
  1584. cleanup:
  1585. qapi_free_MigrateSetParameters(p);
  1586. visit_free(v);
  1587. hmp_handle_error(mon, &err);
  1588. }
  1589. void hmp_client_migrate_info(Monitor *mon, const QDict *qdict)
  1590. {
  1591. Error *err = NULL;
  1592. const char *protocol = qdict_get_str(qdict, "protocol");
  1593. const char *hostname = qdict_get_str(qdict, "hostname");
  1594. bool has_port = qdict_haskey(qdict, "port");
  1595. int port = qdict_get_try_int(qdict, "port", -1);
  1596. bool has_tls_port = qdict_haskey(qdict, "tls-port");
  1597. int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
  1598. const char *cert_subject = qdict_get_try_str(qdict, "cert-subject");
  1599. qmp_client_migrate_info(protocol, hostname,
  1600. has_port, port, has_tls_port, tls_port,
  1601. !!cert_subject, cert_subject, &err);
  1602. hmp_handle_error(mon, &err);
  1603. }
  1604. void hmp_migrate_start_postcopy(Monitor *mon, const QDict *qdict)
  1605. {
  1606. Error *err = NULL;
  1607. qmp_migrate_start_postcopy(&err);
  1608. hmp_handle_error(mon, &err);
  1609. }
  1610. void hmp_x_colo_lost_heartbeat(Monitor *mon, const QDict *qdict)
  1611. {
  1612. Error *err = NULL;
  1613. qmp_x_colo_lost_heartbeat(&err);
  1614. hmp_handle_error(mon, &err);
  1615. }
  1616. void hmp_set_password(Monitor *mon, const QDict *qdict)
  1617. {
  1618. const char *protocol = qdict_get_str(qdict, "protocol");
  1619. const char *password = qdict_get_str(qdict, "password");
  1620. const char *connected = qdict_get_try_str(qdict, "connected");
  1621. Error *err = NULL;
  1622. qmp_set_password(protocol, password, !!connected, connected, &err);
  1623. hmp_handle_error(mon, &err);
  1624. }
  1625. void hmp_expire_password(Monitor *mon, const QDict *qdict)
  1626. {
  1627. const char *protocol = qdict_get_str(qdict, "protocol");
  1628. const char *whenstr = qdict_get_str(qdict, "time");
  1629. Error *err = NULL;
  1630. qmp_expire_password(protocol, whenstr, &err);
  1631. hmp_handle_error(mon, &err);
  1632. }
  1633. void hmp_eject(Monitor *mon, const QDict *qdict)
  1634. {
  1635. bool force = qdict_get_try_bool(qdict, "force", false);
  1636. const char *device = qdict_get_str(qdict, "device");
  1637. Error *err = NULL;
  1638. qmp_eject(true, device, false, NULL, true, force, &err);
  1639. hmp_handle_error(mon, &err);
  1640. }
  1641. #ifdef CONFIG_VNC
  1642. static void hmp_change_read_arg(void *opaque, const char *password,
  1643. void *readline_opaque)
  1644. {
  1645. qmp_change_vnc_password(password, NULL);
  1646. monitor_read_command(opaque, 1);
  1647. }
  1648. #endif
  1649. void hmp_change(Monitor *mon, const QDict *qdict)
  1650. {
  1651. const char *device = qdict_get_str(qdict, "device");
  1652. const char *target = qdict_get_str(qdict, "target");
  1653. const char *arg = qdict_get_try_str(qdict, "arg");
  1654. const char *read_only = qdict_get_try_str(qdict, "read-only-mode");
  1655. BlockdevChangeReadOnlyMode read_only_mode = 0;
  1656. Error *err = NULL;
  1657. #ifdef CONFIG_VNC
  1658. if (strcmp(device, "vnc") == 0) {
  1659. if (read_only) {
  1660. monitor_printf(mon,
  1661. "Parameter 'read-only-mode' is invalid for VNC\n");
  1662. return;
  1663. }
  1664. if (strcmp(target, "passwd") == 0 ||
  1665. strcmp(target, "password") == 0) {
  1666. if (!arg) {
  1667. monitor_read_password(mon, hmp_change_read_arg, NULL);
  1668. return;
  1669. }
  1670. }
  1671. qmp_change("vnc", target, !!arg, arg, &err);
  1672. } else
  1673. #endif
  1674. {
  1675. if (read_only) {
  1676. read_only_mode =
  1677. qapi_enum_parse(&BlockdevChangeReadOnlyMode_lookup,
  1678. read_only,
  1679. BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN, &err);
  1680. if (err) {
  1681. hmp_handle_error(mon, &err);
  1682. return;
  1683. }
  1684. }
  1685. qmp_blockdev_change_medium(true, device, false, NULL, target,
  1686. !!arg, arg, !!read_only, read_only_mode,
  1687. &err);
  1688. }
  1689. hmp_handle_error(mon, &err);
  1690. }
  1691. void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
  1692. {
  1693. Error *err = NULL;
  1694. char *device = (char *) qdict_get_str(qdict, "device");
  1695. BlockIOThrottle throttle = {
  1696. .bps = qdict_get_int(qdict, "bps"),
  1697. .bps_rd = qdict_get_int(qdict, "bps_rd"),
  1698. .bps_wr = qdict_get_int(qdict, "bps_wr"),
  1699. .iops = qdict_get_int(qdict, "iops"),
  1700. .iops_rd = qdict_get_int(qdict, "iops_rd"),
  1701. .iops_wr = qdict_get_int(qdict, "iops_wr"),
  1702. };
  1703. /* qmp_block_set_io_throttle has separate parameters for the
  1704. * (deprecated) block device name and the qdev ID but the HMP
  1705. * version has only one, so we must decide which one to pass. */
  1706. if (blk_by_name(device)) {
  1707. throttle.has_device = true;
  1708. throttle.device = device;
  1709. } else {
  1710. throttle.has_id = true;
  1711. throttle.id = device;
  1712. }
  1713. qmp_block_set_io_throttle(&throttle, &err);
  1714. hmp_handle_error(mon, &err);
  1715. }
  1716. void hmp_block_stream(Monitor *mon, const QDict *qdict)
  1717. {
  1718. Error *error = NULL;
  1719. const char *device = qdict_get_str(qdict, "device");
  1720. const char *base = qdict_get_try_str(qdict, "base");
  1721. int64_t speed = qdict_get_try_int(qdict, "speed", 0);
  1722. qmp_block_stream(true, device, device, base != NULL, base, false, NULL,
  1723. false, NULL, qdict_haskey(qdict, "speed"), speed, true,
  1724. BLOCKDEV_ON_ERROR_REPORT, false, false, false, false,
  1725. &error);
  1726. hmp_handle_error(mon, &error);
  1727. }
  1728. void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
  1729. {
  1730. Error *error = NULL;
  1731. const char *device = qdict_get_str(qdict, "device");
  1732. int64_t value = qdict_get_int(qdict, "speed");
  1733. qmp_block_job_set_speed(device, value, &error);
  1734. hmp_handle_error(mon, &error);
  1735. }
  1736. void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
  1737. {
  1738. Error *error = NULL;
  1739. const char *device = qdict_get_str(qdict, "device");
  1740. bool force = qdict_get_try_bool(qdict, "force", false);
  1741. qmp_block_job_cancel(device, true, force, &error);
  1742. hmp_handle_error(mon, &error);
  1743. }
  1744. void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
  1745. {
  1746. Error *error = NULL;
  1747. const char *device = qdict_get_str(qdict, "device");
  1748. qmp_block_job_pause(device, &error);
  1749. hmp_handle_error(mon, &error);
  1750. }
  1751. void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
  1752. {
  1753. Error *error = NULL;
  1754. const char *device = qdict_get_str(qdict, "device");
  1755. qmp_block_job_resume(device, &error);
  1756. hmp_handle_error(mon, &error);
  1757. }
  1758. void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
  1759. {
  1760. Error *error = NULL;
  1761. const char *device = qdict_get_str(qdict, "device");
  1762. qmp_block_job_complete(device, &error);
  1763. hmp_handle_error(mon, &error);
  1764. }
  1765. typedef struct HMPMigrationStatus
  1766. {
  1767. QEMUTimer *timer;
  1768. Monitor *mon;
  1769. bool is_block_migration;
  1770. } HMPMigrationStatus;
  1771. static void hmp_migrate_status_cb(void *opaque)
  1772. {
  1773. HMPMigrationStatus *status = opaque;
  1774. MigrationInfo *info;
  1775. info = qmp_query_migrate(NULL);
  1776. if (!info->has_status || info->status == MIGRATION_STATUS_ACTIVE ||
  1777. info->status == MIGRATION_STATUS_SETUP) {
  1778. if (info->has_disk) {
  1779. int progress;
  1780. if (info->disk->remaining) {
  1781. progress = info->disk->transferred * 100 / info->disk->total;
  1782. } else {
  1783. progress = 100;
  1784. }
  1785. monitor_printf(status->mon, "Completed %d %%\r", progress);
  1786. monitor_flush(status->mon);
  1787. }
  1788. timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
  1789. } else {
  1790. if (status->is_block_migration) {
  1791. monitor_printf(status->mon, "\n");
  1792. }
  1793. if (info->has_error_desc) {
  1794. error_report("%s", info->error_desc);
  1795. }
  1796. monitor_resume(status->mon);
  1797. timer_del(status->timer);
  1798. timer_free(status->timer);
  1799. g_free(status);
  1800. }
  1801. qapi_free_MigrationInfo(info);
  1802. }
  1803. void hmp_migrate(Monitor *mon, const QDict *qdict)
  1804. {
  1805. bool detach = qdict_get_try_bool(qdict, "detach", false);
  1806. bool blk = qdict_get_try_bool(qdict, "blk", false);
  1807. bool inc = qdict_get_try_bool(qdict, "inc", false);
  1808. bool resume = qdict_get_try_bool(qdict, "resume", false);
  1809. const char *uri = qdict_get_str(qdict, "uri");
  1810. Error *err = NULL;
  1811. qmp_migrate(uri, !!blk, blk, !!inc, inc,
  1812. false, false, true, resume, &err);
  1813. if (err) {
  1814. hmp_handle_error(mon, &err);
  1815. return;
  1816. }
  1817. if (!detach) {
  1818. HMPMigrationStatus *status;
  1819. if (monitor_suspend(mon) < 0) {
  1820. monitor_printf(mon, "terminal does not allow synchronous "
  1821. "migration, continuing detached\n");
  1822. return;
  1823. }
  1824. status = g_malloc0(sizeof(*status));
  1825. status->mon = mon;
  1826. status->is_block_migration = blk || inc;
  1827. status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
  1828. status);
  1829. timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
  1830. }
  1831. }
  1832. void hmp_device_add(Monitor *mon, const QDict *qdict)
  1833. {
  1834. Error *err = NULL;
  1835. qmp_device_add((QDict *)qdict, NULL, &err);
  1836. hmp_handle_error(mon, &err);
  1837. }
  1838. void hmp_device_del(Monitor *mon, const QDict *qdict)
  1839. {
  1840. const char *id = qdict_get_str(qdict, "id");
  1841. Error *err = NULL;
  1842. qmp_device_del(id, &err);
  1843. hmp_handle_error(mon, &err);
  1844. }
  1845. void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
  1846. {
  1847. Error *err = NULL;
  1848. bool win_dmp = qdict_get_try_bool(qdict, "windmp", false);
  1849. bool paging = qdict_get_try_bool(qdict, "paging", false);
  1850. bool zlib = qdict_get_try_bool(qdict, "zlib", false);
  1851. bool lzo = qdict_get_try_bool(qdict, "lzo", false);
  1852. bool snappy = qdict_get_try_bool(qdict, "snappy", false);
  1853. const char *file = qdict_get_str(qdict, "filename");
  1854. bool has_begin = qdict_haskey(qdict, "begin");
  1855. bool has_length = qdict_haskey(qdict, "length");
  1856. bool has_detach = qdict_haskey(qdict, "detach");
  1857. int64_t begin = 0;
  1858. int64_t length = 0;
  1859. bool detach = false;
  1860. enum DumpGuestMemoryFormat dump_format = DUMP_GUEST_MEMORY_FORMAT_ELF;
  1861. char *prot;
  1862. if (zlib + lzo + snappy + win_dmp > 1) {
  1863. error_setg(&err, "only one of '-z|-l|-s|-w' can be set");
  1864. hmp_handle_error(mon, &err);
  1865. return;
  1866. }
  1867. if (win_dmp) {
  1868. dump_format = DUMP_GUEST_MEMORY_FORMAT_WIN_DMP;
  1869. }
  1870. if (zlib) {
  1871. dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
  1872. }
  1873. if (lzo) {
  1874. dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
  1875. }
  1876. if (snappy) {
  1877. dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
  1878. }
  1879. if (has_begin) {
  1880. begin = qdict_get_int(qdict, "begin");
  1881. }
  1882. if (has_length) {
  1883. length = qdict_get_int(qdict, "length");
  1884. }
  1885. if (has_detach) {
  1886. detach = qdict_get_bool(qdict, "detach");
  1887. }
  1888. prot = g_strconcat("file:", file, NULL);
  1889. qmp_dump_guest_memory(paging, prot, true, detach, has_begin, begin,
  1890. has_length, length, true, dump_format, &err);
  1891. hmp_handle_error(mon, &err);
  1892. g_free(prot);
  1893. }
  1894. void hmp_netdev_add(Monitor *mon, const QDict *qdict)
  1895. {
  1896. Error *err = NULL;
  1897. QemuOpts *opts;
  1898. opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
  1899. if (err) {
  1900. goto out;
  1901. }
  1902. netdev_add(opts, &err);
  1903. if (err) {
  1904. qemu_opts_del(opts);
  1905. }
  1906. out:
  1907. hmp_handle_error(mon, &err);
  1908. }
  1909. void hmp_netdev_del(Monitor *mon, const QDict *qdict)
  1910. {
  1911. const char *id = qdict_get_str(qdict, "id");
  1912. Error *err = NULL;
  1913. qmp_netdev_del(id, &err);
  1914. hmp_handle_error(mon, &err);
  1915. }
  1916. void hmp_object_add(Monitor *mon, const QDict *qdict)
  1917. {
  1918. Error *err = NULL;
  1919. QemuOpts *opts;
  1920. Object *obj = NULL;
  1921. opts = qemu_opts_from_qdict(qemu_find_opts("object"), qdict, &err);
  1922. if (err) {
  1923. hmp_handle_error(mon, &err);
  1924. return;
  1925. }
  1926. obj = user_creatable_add_opts(opts, &err);
  1927. qemu_opts_del(opts);
  1928. if (err) {
  1929. hmp_handle_error(mon, &err);
  1930. }
  1931. if (obj) {
  1932. object_unref(obj);
  1933. }
  1934. }
  1935. void hmp_getfd(Monitor *mon, const QDict *qdict)
  1936. {
  1937. const char *fdname = qdict_get_str(qdict, "fdname");
  1938. Error *err = NULL;
  1939. qmp_getfd(fdname, &err);
  1940. hmp_handle_error(mon, &err);
  1941. }
  1942. void hmp_closefd(Monitor *mon, const QDict *qdict)
  1943. {
  1944. const char *fdname = qdict_get_str(qdict, "fdname");
  1945. Error *err = NULL;
  1946. qmp_closefd(fdname, &err);
  1947. hmp_handle_error(mon, &err);
  1948. }
  1949. void hmp_sendkey(Monitor *mon, const QDict *qdict)
  1950. {
  1951. const char *keys = qdict_get_str(qdict, "keys");
  1952. KeyValueList *keylist, *head = NULL, *tmp = NULL;
  1953. int has_hold_time = qdict_haskey(qdict, "hold-time");
  1954. int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
  1955. Error *err = NULL;
  1956. const char *separator;
  1957. int keyname_len;
  1958. while (1) {
  1959. separator = qemu_strchrnul(keys, '-');
  1960. keyname_len = separator - keys;
  1961. /* Be compatible with old interface, convert user inputted "<" */
  1962. if (keys[0] == '<' && keyname_len == 1) {
  1963. keys = "less";
  1964. keyname_len = 4;
  1965. }
  1966. keylist = g_malloc0(sizeof(*keylist));
  1967. keylist->value = g_malloc0(sizeof(*keylist->value));
  1968. if (!head) {
  1969. head = keylist;
  1970. }
  1971. if (tmp) {
  1972. tmp->next = keylist;
  1973. }
  1974. tmp = keylist;
  1975. if (strstart(keys, "0x", NULL)) {
  1976. char *endp;
  1977. int value = strtoul(keys, &endp, 0);
  1978. assert(endp <= keys + keyname_len);
  1979. if (endp != keys + keyname_len) {
  1980. goto err_out;
  1981. }
  1982. keylist->value->type = KEY_VALUE_KIND_NUMBER;
  1983. keylist->value->u.number.data = value;
  1984. } else {
  1985. int idx = index_from_key(keys, keyname_len);
  1986. if (idx == Q_KEY_CODE__MAX) {
  1987. goto err_out;
  1988. }
  1989. keylist->value->type = KEY_VALUE_KIND_QCODE;
  1990. keylist->value->u.qcode.data = idx;
  1991. }
  1992. if (!*separator) {
  1993. break;
  1994. }
  1995. keys = separator + 1;
  1996. }
  1997. qmp_send_key(head, has_hold_time, hold_time, &err);
  1998. hmp_handle_error(mon, &err);
  1999. out:
  2000. qapi_free_KeyValueList(head);
  2001. return;
  2002. err_out:
  2003. monitor_printf(mon, "invalid parameter: %.*s\n", keyname_len, keys);
  2004. goto out;
  2005. }
  2006. void hmp_screendump(Monitor *mon, const QDict *qdict)
  2007. {
  2008. const char *filename = qdict_get_str(qdict, "filename");
  2009. const char *id = qdict_get_try_str(qdict, "device");
  2010. int64_t head = qdict_get_try_int(qdict, "head", 0);
  2011. Error *err = NULL;
  2012. qmp_screendump(filename, id != NULL, id, id != NULL, head, &err);
  2013. hmp_handle_error(mon, &err);
  2014. }
  2015. void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
  2016. {
  2017. const char *uri = qdict_get_str(qdict, "uri");
  2018. bool writable = qdict_get_try_bool(qdict, "writable", false);
  2019. bool all = qdict_get_try_bool(qdict, "all", false);
  2020. Error *local_err = NULL;
  2021. BlockInfoList *block_list, *info;
  2022. SocketAddress *addr;
  2023. if (writable && !all) {
  2024. error_setg(&local_err, "-w only valid together with -a");
  2025. goto exit;
  2026. }
  2027. /* First check if the address is valid and start the server. */
  2028. addr = socket_parse(uri, &local_err);
  2029. if (local_err != NULL) {
  2030. goto exit;
  2031. }
  2032. nbd_server_start(addr, NULL, &local_err);
  2033. qapi_free_SocketAddress(addr);
  2034. if (local_err != NULL) {
  2035. goto exit;
  2036. }
  2037. if (!all) {
  2038. return;
  2039. }
  2040. /* Then try adding all block devices. If one fails, close all and
  2041. * exit.
  2042. */
  2043. block_list = qmp_query_block(NULL);
  2044. for (info = block_list; info; info = info->next) {
  2045. if (!info->value->has_inserted) {
  2046. continue;
  2047. }
  2048. qmp_nbd_server_add(info->value->device, false, NULL,
  2049. true, writable, false, NULL, &local_err);
  2050. if (local_err != NULL) {
  2051. qmp_nbd_server_stop(NULL);
  2052. break;
  2053. }
  2054. }
  2055. qapi_free_BlockInfoList(block_list);
  2056. exit:
  2057. hmp_handle_error(mon, &local_err);
  2058. }
  2059. void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
  2060. {
  2061. const char *device = qdict_get_str(qdict, "device");
  2062. const char *name = qdict_get_try_str(qdict, "name");
  2063. bool writable = qdict_get_try_bool(qdict, "writable", false);
  2064. Error *local_err = NULL;
  2065. qmp_nbd_server_add(device, !!name, name, true, writable,
  2066. false, NULL, &local_err);
  2067. hmp_handle_error(mon, &local_err);
  2068. }
  2069. void hmp_nbd_server_remove(Monitor *mon, const QDict *qdict)
  2070. {
  2071. const char *name = qdict_get_str(qdict, "name");
  2072. bool force = qdict_get_try_bool(qdict, "force", false);
  2073. Error *err = NULL;
  2074. /* Rely on NBD_SERVER_REMOVE_MODE_SAFE being the default */
  2075. qmp_nbd_server_remove(name, force, NBD_SERVER_REMOVE_MODE_HARD, &err);
  2076. hmp_handle_error(mon, &err);
  2077. }
  2078. void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
  2079. {
  2080. Error *err = NULL;
  2081. qmp_nbd_server_stop(&err);
  2082. hmp_handle_error(mon, &err);
  2083. }
  2084. void hmp_cpu_add(Monitor *mon, const QDict *qdict)
  2085. {
  2086. int cpuid;
  2087. Error *err = NULL;
  2088. error_report("cpu_add is deprecated, please use device_add instead");
  2089. cpuid = qdict_get_int(qdict, "id");
  2090. qmp_cpu_add(cpuid, &err);
  2091. hmp_handle_error(mon, &err);
  2092. }
  2093. void hmp_chardev_add(Monitor *mon, const QDict *qdict)
  2094. {
  2095. const char *args = qdict_get_str(qdict, "args");
  2096. Error *err = NULL;
  2097. QemuOpts *opts;
  2098. opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), args, true);
  2099. if (opts == NULL) {
  2100. error_setg(&err, "Parsing chardev args failed");
  2101. } else {
  2102. qemu_chr_new_from_opts(opts, NULL, &err);
  2103. qemu_opts_del(opts);
  2104. }
  2105. hmp_handle_error(mon, &err);
  2106. }
  2107. void hmp_chardev_change(Monitor *mon, const QDict *qdict)
  2108. {
  2109. const char *args = qdict_get_str(qdict, "args");
  2110. const char *id;
  2111. Error *err = NULL;
  2112. ChardevBackend *backend = NULL;
  2113. ChardevReturn *ret = NULL;
  2114. QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), args,
  2115. true);
  2116. if (!opts) {
  2117. error_setg(&err, "Parsing chardev args failed");
  2118. goto end;
  2119. }
  2120. id = qdict_get_str(qdict, "id");
  2121. if (qemu_opts_id(opts)) {
  2122. error_setg(&err, "Unexpected 'id' parameter");
  2123. goto end;
  2124. }
  2125. backend = qemu_chr_parse_opts(opts, &err);
  2126. if (!backend) {
  2127. goto end;
  2128. }
  2129. ret = qmp_chardev_change(id, backend, &err);
  2130. end:
  2131. qapi_free_ChardevReturn(ret);
  2132. qapi_free_ChardevBackend(backend);
  2133. qemu_opts_del(opts);
  2134. hmp_handle_error(mon, &err);
  2135. }
  2136. void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
  2137. {
  2138. Error *local_err = NULL;
  2139. qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
  2140. hmp_handle_error(mon, &local_err);
  2141. }
  2142. void hmp_chardev_send_break(Monitor *mon, const QDict *qdict)
  2143. {
  2144. Error *local_err = NULL;
  2145. qmp_chardev_send_break(qdict_get_str(qdict, "id"), &local_err);
  2146. hmp_handle_error(mon, &local_err);
  2147. }
  2148. void hmp_qemu_io(Monitor *mon, const QDict *qdict)
  2149. {
  2150. BlockBackend *blk;
  2151. BlockBackend *local_blk = NULL;
  2152. const char* device = qdict_get_str(qdict, "device");
  2153. const char* command = qdict_get_str(qdict, "command");
  2154. Error *err = NULL;
  2155. int ret;
  2156. blk = blk_by_name(device);
  2157. if (!blk) {
  2158. BlockDriverState *bs = bdrv_lookup_bs(NULL, device, &err);
  2159. if (bs) {
  2160. blk = local_blk = blk_new(0, BLK_PERM_ALL);
  2161. ret = blk_insert_bs(blk, bs, &err);
  2162. if (ret < 0) {
  2163. goto fail;
  2164. }
  2165. } else {
  2166. goto fail;
  2167. }
  2168. }
  2169. /*
  2170. * Notably absent: Proper permission management. This is sad, but it seems
  2171. * almost impossible to achieve without changing the semantics and thereby
  2172. * limiting the use cases of the qemu-io HMP command.
  2173. *
  2174. * In an ideal world we would unconditionally create a new BlockBackend for
  2175. * qemuio_command(), but we have commands like 'reopen' and want them to
  2176. * take effect on the exact BlockBackend whose name the user passed instead
  2177. * of just on a temporary copy of it.
  2178. *
  2179. * Another problem is that deleting the temporary BlockBackend involves
  2180. * draining all requests on it first, but some qemu-iotests cases want to
  2181. * issue multiple aio_read/write requests and expect them to complete in
  2182. * the background while the monitor has already returned.
  2183. *
  2184. * This is also what prevents us from saving the original permissions and
  2185. * restoring them later: We can't revoke permissions until all requests
  2186. * have completed, and we don't know when that is nor can we really let
  2187. * anything else run before we have revoken them to avoid race conditions.
  2188. *
  2189. * What happens now is that command() in qemu-io-cmds.c can extend the
  2190. * permissions if necessary for the qemu-io command. And they simply stay
  2191. * extended, possibly resulting in a read-only guest device keeping write
  2192. * permissions. Ugly, but it appears to be the lesser evil.
  2193. */
  2194. qemuio_command(blk, command);
  2195. fail:
  2196. blk_unref(local_blk);
  2197. hmp_handle_error(mon, &err);
  2198. }
  2199. void hmp_object_del(Monitor *mon, const QDict *qdict)
  2200. {
  2201. const char *id = qdict_get_str(qdict, "id");
  2202. Error *err = NULL;
  2203. user_creatable_del(id, &err);
  2204. hmp_handle_error(mon, &err);
  2205. }
  2206. void hmp_info_memdev(Monitor *mon, const QDict *qdict)
  2207. {
  2208. Error *err = NULL;
  2209. MemdevList *memdev_list = qmp_query_memdev(&err);
  2210. MemdevList *m = memdev_list;
  2211. Visitor *v;
  2212. char *str;
  2213. while (m) {
  2214. v = string_output_visitor_new(false, &str);
  2215. visit_type_uint16List(v, NULL, &m->value->host_nodes, NULL);
  2216. monitor_printf(mon, "memory backend: %s\n", m->value->id);
  2217. monitor_printf(mon, " size: %" PRId64 "\n", m->value->size);
  2218. monitor_printf(mon, " merge: %s\n",
  2219. m->value->merge ? "true" : "false");
  2220. monitor_printf(mon, " dump: %s\n",
  2221. m->value->dump ? "true" : "false");
  2222. monitor_printf(mon, " prealloc: %s\n",
  2223. m->value->prealloc ? "true" : "false");
  2224. monitor_printf(mon, " policy: %s\n",
  2225. HostMemPolicy_str(m->value->policy));
  2226. visit_complete(v, &str);
  2227. monitor_printf(mon, " host nodes: %s\n", str);
  2228. g_free(str);
  2229. visit_free(v);
  2230. m = m->next;
  2231. }
  2232. monitor_printf(mon, "\n");
  2233. qapi_free_MemdevList(memdev_list);
  2234. hmp_handle_error(mon, &err);
  2235. }
  2236. void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
  2237. {
  2238. Error *err = NULL;
  2239. MemoryDeviceInfoList *info_list = qmp_query_memory_devices(&err);
  2240. MemoryDeviceInfoList *info;
  2241. MemoryDeviceInfo *value;
  2242. PCDIMMDeviceInfo *di;
  2243. for (info = info_list; info; info = info->next) {
  2244. value = info->value;
  2245. if (value) {
  2246. switch (value->type) {
  2247. case MEMORY_DEVICE_INFO_KIND_DIMM:
  2248. di = value->u.dimm.data;
  2249. break;
  2250. case MEMORY_DEVICE_INFO_KIND_NVDIMM:
  2251. di = value->u.nvdimm.data;
  2252. break;
  2253. default:
  2254. di = NULL;
  2255. break;
  2256. }
  2257. if (di) {
  2258. monitor_printf(mon, "Memory device [%s]: \"%s\"\n",
  2259. MemoryDeviceInfoKind_str(value->type),
  2260. di->id ? di->id : "");
  2261. monitor_printf(mon, " addr: 0x%" PRIx64 "\n", di->addr);
  2262. monitor_printf(mon, " slot: %" PRId64 "\n", di->slot);
  2263. monitor_printf(mon, " node: %" PRId64 "\n", di->node);
  2264. monitor_printf(mon, " size: %" PRIu64 "\n", di->size);
  2265. monitor_printf(mon, " memdev: %s\n", di->memdev);
  2266. monitor_printf(mon, " hotplugged: %s\n",
  2267. di->hotplugged ? "true" : "false");
  2268. monitor_printf(mon, " hotpluggable: %s\n",
  2269. di->hotpluggable ? "true" : "false");
  2270. }
  2271. }
  2272. }
  2273. qapi_free_MemoryDeviceInfoList(info_list);
  2274. hmp_handle_error(mon, &err);
  2275. }
  2276. void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
  2277. {
  2278. IOThreadInfoList *info_list = qmp_query_iothreads(NULL);
  2279. IOThreadInfoList *info;
  2280. IOThreadInfo *value;
  2281. for (info = info_list; info; info = info->next) {
  2282. value = info->value;
  2283. monitor_printf(mon, "%s:\n", value->id);
  2284. monitor_printf(mon, " thread_id=%" PRId64 "\n", value->thread_id);
  2285. monitor_printf(mon, " poll-max-ns=%" PRId64 "\n", value->poll_max_ns);
  2286. monitor_printf(mon, " poll-grow=%" PRId64 "\n", value->poll_grow);
  2287. monitor_printf(mon, " poll-shrink=%" PRId64 "\n", value->poll_shrink);
  2288. }
  2289. qapi_free_IOThreadInfoList(info_list);
  2290. }
  2291. void hmp_qom_list(Monitor *mon, const QDict *qdict)
  2292. {
  2293. const char *path = qdict_get_try_str(qdict, "path");
  2294. ObjectPropertyInfoList *list;
  2295. Error *err = NULL;
  2296. if (path == NULL) {
  2297. monitor_printf(mon, "/\n");
  2298. return;
  2299. }
  2300. list = qmp_qom_list(path, &err);
  2301. if (err == NULL) {
  2302. ObjectPropertyInfoList *start = list;
  2303. while (list != NULL) {
  2304. ObjectPropertyInfo *value = list->value;
  2305. monitor_printf(mon, "%s (%s)\n",
  2306. value->name, value->type);
  2307. list = list->next;
  2308. }
  2309. qapi_free_ObjectPropertyInfoList(start);
  2310. }
  2311. hmp_handle_error(mon, &err);
  2312. }
  2313. void hmp_qom_set(Monitor *mon, const QDict *qdict)
  2314. {
  2315. const char *path = qdict_get_str(qdict, "path");
  2316. const char *property = qdict_get_str(qdict, "property");
  2317. const char *value = qdict_get_str(qdict, "value");
  2318. Error *err = NULL;
  2319. bool ambiguous = false;
  2320. Object *obj;
  2321. obj = object_resolve_path(path, &ambiguous);
  2322. if (obj == NULL) {
  2323. error_set(&err, ERROR_CLASS_DEVICE_NOT_FOUND,
  2324. "Device '%s' not found", path);
  2325. } else {
  2326. if (ambiguous) {
  2327. monitor_printf(mon, "Warning: Path '%s' is ambiguous\n", path);
  2328. }
  2329. object_property_parse(obj, value, property, &err);
  2330. }
  2331. hmp_handle_error(mon, &err);
  2332. }
  2333. void hmp_rocker(Monitor *mon, const QDict *qdict)
  2334. {
  2335. const char *name = qdict_get_str(qdict, "name");
  2336. RockerSwitch *rocker;
  2337. Error *err = NULL;
  2338. rocker = qmp_query_rocker(name, &err);
  2339. if (err != NULL) {
  2340. hmp_handle_error(mon, &err);
  2341. return;
  2342. }
  2343. monitor_printf(mon, "name: %s\n", rocker->name);
  2344. monitor_printf(mon, "id: 0x%" PRIx64 "\n", rocker->id);
  2345. monitor_printf(mon, "ports: %d\n", rocker->ports);
  2346. qapi_free_RockerSwitch(rocker);
  2347. }
  2348. void hmp_rocker_ports(Monitor *mon, const QDict *qdict)
  2349. {
  2350. RockerPortList *list, *port;
  2351. const char *name = qdict_get_str(qdict, "name");
  2352. Error *err = NULL;
  2353. list = qmp_query_rocker_ports(name, &err);
  2354. if (err != NULL) {
  2355. hmp_handle_error(mon, &err);
  2356. return;
  2357. }
  2358. monitor_printf(mon, " ena/ speed/ auto\n");
  2359. monitor_printf(mon, " port link duplex neg?\n");
  2360. for (port = list; port; port = port->next) {
  2361. monitor_printf(mon, "%10s %-4s %-3s %2s %-3s\n",
  2362. port->value->name,
  2363. port->value->enabled ? port->value->link_up ?
  2364. "up" : "down" : "!ena",
  2365. port->value->speed == 10000 ? "10G" : "??",
  2366. port->value->duplex ? "FD" : "HD",
  2367. port->value->autoneg ? "Yes" : "No");
  2368. }
  2369. qapi_free_RockerPortList(list);
  2370. }
  2371. void hmp_rocker_of_dpa_flows(Monitor *mon, const QDict *qdict)
  2372. {
  2373. RockerOfDpaFlowList *list, *info;
  2374. const char *name = qdict_get_str(qdict, "name");
  2375. uint32_t tbl_id = qdict_get_try_int(qdict, "tbl_id", -1);
  2376. Error *err = NULL;
  2377. list = qmp_query_rocker_of_dpa_flows(name, tbl_id != -1, tbl_id, &err);
  2378. if (err != NULL) {
  2379. hmp_handle_error(mon, &err);
  2380. return;
  2381. }
  2382. monitor_printf(mon, "prio tbl hits key(mask) --> actions\n");
  2383. for (info = list; info; info = info->next) {
  2384. RockerOfDpaFlow *flow = info->value;
  2385. RockerOfDpaFlowKey *key = flow->key;
  2386. RockerOfDpaFlowMask *mask = flow->mask;
  2387. RockerOfDpaFlowAction *action = flow->action;
  2388. if (flow->hits) {
  2389. monitor_printf(mon, "%-4d %-3d %-4" PRIu64,
  2390. key->priority, key->tbl_id, flow->hits);
  2391. } else {
  2392. monitor_printf(mon, "%-4d %-3d ",
  2393. key->priority, key->tbl_id);
  2394. }
  2395. if (key->has_in_pport) {
  2396. monitor_printf(mon, " pport %d", key->in_pport);
  2397. if (mask->has_in_pport) {
  2398. monitor_printf(mon, "(0x%x)", mask->in_pport);
  2399. }
  2400. }
  2401. if (key->has_vlan_id) {
  2402. monitor_printf(mon, " vlan %d",
  2403. key->vlan_id & VLAN_VID_MASK);
  2404. if (mask->has_vlan_id) {
  2405. monitor_printf(mon, "(0x%x)", mask->vlan_id);
  2406. }
  2407. }
  2408. if (key->has_tunnel_id) {
  2409. monitor_printf(mon, " tunnel %d", key->tunnel_id);
  2410. if (mask->has_tunnel_id) {
  2411. monitor_printf(mon, "(0x%x)", mask->tunnel_id);
  2412. }
  2413. }
  2414. if (key->has_eth_type) {
  2415. switch (key->eth_type) {
  2416. case 0x0806:
  2417. monitor_printf(mon, " ARP");
  2418. break;
  2419. case 0x0800:
  2420. monitor_printf(mon, " IP");
  2421. break;
  2422. case 0x86dd:
  2423. monitor_printf(mon, " IPv6");
  2424. break;
  2425. case 0x8809:
  2426. monitor_printf(mon, " LACP");
  2427. break;
  2428. case 0x88cc:
  2429. monitor_printf(mon, " LLDP");
  2430. break;
  2431. default:
  2432. monitor_printf(mon, " eth type 0x%04x", key->eth_type);
  2433. break;
  2434. }
  2435. }
  2436. if (key->has_eth_src) {
  2437. if ((strcmp(key->eth_src, "01:00:00:00:00:00") == 0) &&
  2438. (mask->has_eth_src) &&
  2439. (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
  2440. monitor_printf(mon, " src <any mcast/bcast>");
  2441. } else if ((strcmp(key->eth_src, "00:00:00:00:00:00") == 0) &&
  2442. (mask->has_eth_src) &&
  2443. (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
  2444. monitor_printf(mon, " src <any ucast>");
  2445. } else {
  2446. monitor_printf(mon, " src %s", key->eth_src);
  2447. if (mask->has_eth_src) {
  2448. monitor_printf(mon, "(%s)", mask->eth_src);
  2449. }
  2450. }
  2451. }
  2452. if (key->has_eth_dst) {
  2453. if ((strcmp(key->eth_dst, "01:00:00:00:00:00") == 0) &&
  2454. (mask->has_eth_dst) &&
  2455. (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
  2456. monitor_printf(mon, " dst <any mcast/bcast>");
  2457. } else if ((strcmp(key->eth_dst, "00:00:00:00:00:00") == 0) &&
  2458. (mask->has_eth_dst) &&
  2459. (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
  2460. monitor_printf(mon, " dst <any ucast>");
  2461. } else {
  2462. monitor_printf(mon, " dst %s", key->eth_dst);
  2463. if (mask->has_eth_dst) {
  2464. monitor_printf(mon, "(%s)", mask->eth_dst);
  2465. }
  2466. }
  2467. }
  2468. if (key->has_ip_proto) {
  2469. monitor_printf(mon, " proto %d", key->ip_proto);
  2470. if (mask->has_ip_proto) {
  2471. monitor_printf(mon, "(0x%x)", mask->ip_proto);
  2472. }
  2473. }
  2474. if (key->has_ip_tos) {
  2475. monitor_printf(mon, " TOS %d", key->ip_tos);
  2476. if (mask->has_ip_tos) {
  2477. monitor_printf(mon, "(0x%x)", mask->ip_tos);
  2478. }
  2479. }
  2480. if (key->has_ip_dst) {
  2481. monitor_printf(mon, " dst %s", key->ip_dst);
  2482. }
  2483. if (action->has_goto_tbl || action->has_group_id ||
  2484. action->has_new_vlan_id) {
  2485. monitor_printf(mon, " -->");
  2486. }
  2487. if (action->has_new_vlan_id) {
  2488. monitor_printf(mon, " apply new vlan %d",
  2489. ntohs(action->new_vlan_id));
  2490. }
  2491. if (action->has_group_id) {
  2492. monitor_printf(mon, " write group 0x%08x", action->group_id);
  2493. }
  2494. if (action->has_goto_tbl) {
  2495. monitor_printf(mon, " goto tbl %d", action->goto_tbl);
  2496. }
  2497. monitor_printf(mon, "\n");
  2498. }
  2499. qapi_free_RockerOfDpaFlowList(list);
  2500. }
  2501. void hmp_rocker_of_dpa_groups(Monitor *mon, const QDict *qdict)
  2502. {
  2503. RockerOfDpaGroupList *list, *g;
  2504. const char *name = qdict_get_str(qdict, "name");
  2505. uint8_t type = qdict_get_try_int(qdict, "type", 9);
  2506. Error *err = NULL;
  2507. bool set = false;
  2508. list = qmp_query_rocker_of_dpa_groups(name, type != 9, type, &err);
  2509. if (err != NULL) {
  2510. hmp_handle_error(mon, &err);
  2511. return;
  2512. }
  2513. monitor_printf(mon, "id (decode) --> buckets\n");
  2514. for (g = list; g; g = g->next) {
  2515. RockerOfDpaGroup *group = g->value;
  2516. monitor_printf(mon, "0x%08x", group->id);
  2517. monitor_printf(mon, " (type %s", group->type == 0 ? "L2 interface" :
  2518. group->type == 1 ? "L2 rewrite" :
  2519. group->type == 2 ? "L3 unicast" :
  2520. group->type == 3 ? "L2 multicast" :
  2521. group->type == 4 ? "L2 flood" :
  2522. group->type == 5 ? "L3 interface" :
  2523. group->type == 6 ? "L3 multicast" :
  2524. group->type == 7 ? "L3 ECMP" :
  2525. group->type == 8 ? "L2 overlay" :
  2526. "unknown");
  2527. if (group->has_vlan_id) {
  2528. monitor_printf(mon, " vlan %d", group->vlan_id);
  2529. }
  2530. if (group->has_pport) {
  2531. monitor_printf(mon, " pport %d", group->pport);
  2532. }
  2533. if (group->has_index) {
  2534. monitor_printf(mon, " index %d", group->index);
  2535. }
  2536. monitor_printf(mon, ") -->");
  2537. if (group->has_set_vlan_id && group->set_vlan_id) {
  2538. set = true;
  2539. monitor_printf(mon, " set vlan %d",
  2540. group->set_vlan_id & VLAN_VID_MASK);
  2541. }
  2542. if (group->has_set_eth_src) {
  2543. if (!set) {
  2544. set = true;
  2545. monitor_printf(mon, " set");
  2546. }
  2547. monitor_printf(mon, " src %s", group->set_eth_src);
  2548. }
  2549. if (group->has_set_eth_dst) {
  2550. if (!set) {
  2551. set = true;
  2552. monitor_printf(mon, " set");
  2553. }
  2554. monitor_printf(mon, " dst %s", group->set_eth_dst);
  2555. }
  2556. set = false;
  2557. if (group->has_ttl_check && group->ttl_check) {
  2558. monitor_printf(mon, " check TTL");
  2559. }
  2560. if (group->has_group_id && group->group_id) {
  2561. monitor_printf(mon, " group id 0x%08x", group->group_id);
  2562. }
  2563. if (group->has_pop_vlan && group->pop_vlan) {
  2564. monitor_printf(mon, " pop vlan");
  2565. }
  2566. if (group->has_out_pport) {
  2567. monitor_printf(mon, " out pport %d", group->out_pport);
  2568. }
  2569. if (group->has_group_ids) {
  2570. struct uint32List *id;
  2571. monitor_printf(mon, " groups [");
  2572. for (id = group->group_ids; id; id = id->next) {
  2573. monitor_printf(mon, "0x%08x", id->value);
  2574. if (id->next) {
  2575. monitor_printf(mon, ",");
  2576. }
  2577. }
  2578. monitor_printf(mon, "]");
  2579. }
  2580. monitor_printf(mon, "\n");
  2581. }
  2582. qapi_free_RockerOfDpaGroupList(list);
  2583. }
  2584. void hmp_info_dump(Monitor *mon, const QDict *qdict)
  2585. {
  2586. DumpQueryResult *result = qmp_query_dump(NULL);
  2587. assert(result && result->status < DUMP_STATUS__MAX);
  2588. monitor_printf(mon, "Status: %s\n", DumpStatus_str(result->status));
  2589. if (result->status == DUMP_STATUS_ACTIVE) {
  2590. float percent = 0;
  2591. assert(result->total != 0);
  2592. percent = 100.0 * result->completed / result->total;
  2593. monitor_printf(mon, "Finished: %.2f %%\n", percent);
  2594. }
  2595. qapi_free_DumpQueryResult(result);
  2596. }
  2597. void hmp_info_ramblock(Monitor *mon, const QDict *qdict)
  2598. {
  2599. ram_block_dump(mon);
  2600. }
  2601. void hmp_hotpluggable_cpus(Monitor *mon, const QDict *qdict)
  2602. {
  2603. Error *err = NULL;
  2604. HotpluggableCPUList *l = qmp_query_hotpluggable_cpus(&err);
  2605. HotpluggableCPUList *saved = l;
  2606. CpuInstanceProperties *c;
  2607. if (err != NULL) {
  2608. hmp_handle_error(mon, &err);
  2609. return;
  2610. }
  2611. monitor_printf(mon, "Hotpluggable CPUs:\n");
  2612. while (l) {
  2613. monitor_printf(mon, " type: \"%s\"\n", l->value->type);
  2614. monitor_printf(mon, " vcpus_count: \"%" PRIu64 "\"\n",
  2615. l->value->vcpus_count);
  2616. if (l->value->has_qom_path) {
  2617. monitor_printf(mon, " qom_path: \"%s\"\n", l->value->qom_path);
  2618. }
  2619. c = l->value->props;
  2620. monitor_printf(mon, " CPUInstance Properties:\n");
  2621. if (c->has_node_id) {
  2622. monitor_printf(mon, " node-id: \"%" PRIu64 "\"\n", c->node_id);
  2623. }
  2624. if (c->has_socket_id) {
  2625. monitor_printf(mon, " socket-id: \"%" PRIu64 "\"\n", c->socket_id);
  2626. }
  2627. if (c->has_core_id) {
  2628. monitor_printf(mon, " core-id: \"%" PRIu64 "\"\n", c->core_id);
  2629. }
  2630. if (c->has_thread_id) {
  2631. monitor_printf(mon, " thread-id: \"%" PRIu64 "\"\n", c->thread_id);
  2632. }
  2633. l = l->next;
  2634. }
  2635. qapi_free_HotpluggableCPUList(saved);
  2636. }
  2637. void hmp_info_vm_generation_id(Monitor *mon, const QDict *qdict)
  2638. {
  2639. Error *err = NULL;
  2640. GuidInfo *info = qmp_query_vm_generation_id(&err);
  2641. if (info) {
  2642. monitor_printf(mon, "%s\n", info->guid);
  2643. }
  2644. hmp_handle_error(mon, &err);
  2645. qapi_free_GuidInfo(info);
  2646. }
  2647. void hmp_info_memory_size_summary(Monitor *mon, const QDict *qdict)
  2648. {
  2649. Error *err = NULL;
  2650. MemoryInfo *info = qmp_query_memory_size_summary(&err);
  2651. if (info) {
  2652. monitor_printf(mon, "base memory: %" PRIu64 "\n",
  2653. info->base_memory);
  2654. if (info->has_plugged_memory) {
  2655. monitor_printf(mon, "plugged memory: %" PRIu64 "\n",
  2656. info->plugged_memory);
  2657. }
  2658. qapi_free_MemoryInfo(info);
  2659. }
  2660. hmp_handle_error(mon, &err);
  2661. }