hmp.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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 "hmp.h"
  16. #include "qemu-timer.h"
  17. #include "qmp-commands.h"
  18. static void hmp_handle_error(Monitor *mon, Error **errp)
  19. {
  20. if (error_is_set(errp)) {
  21. monitor_printf(mon, "%s\n", error_get_pretty(*errp));
  22. error_free(*errp);
  23. }
  24. }
  25. void hmp_info_name(Monitor *mon)
  26. {
  27. NameInfo *info;
  28. info = qmp_query_name(NULL);
  29. if (info->has_name) {
  30. monitor_printf(mon, "%s\n", info->name);
  31. }
  32. qapi_free_NameInfo(info);
  33. }
  34. void hmp_info_version(Monitor *mon)
  35. {
  36. VersionInfo *info;
  37. info = qmp_query_version(NULL);
  38. monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
  39. info->qemu.major, info->qemu.minor, info->qemu.micro,
  40. info->package);
  41. qapi_free_VersionInfo(info);
  42. }
  43. void hmp_info_kvm(Monitor *mon)
  44. {
  45. KvmInfo *info;
  46. info = qmp_query_kvm(NULL);
  47. monitor_printf(mon, "kvm support: ");
  48. if (info->present) {
  49. monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
  50. } else {
  51. monitor_printf(mon, "not compiled\n");
  52. }
  53. qapi_free_KvmInfo(info);
  54. }
  55. void hmp_info_status(Monitor *mon)
  56. {
  57. StatusInfo *info;
  58. info = qmp_query_status(NULL);
  59. monitor_printf(mon, "VM status: %s%s",
  60. info->running ? "running" : "paused",
  61. info->singlestep ? " (single step mode)" : "");
  62. if (!info->running && info->status != RUN_STATE_PAUSED) {
  63. monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
  64. }
  65. monitor_printf(mon, "\n");
  66. qapi_free_StatusInfo(info);
  67. }
  68. void hmp_info_uuid(Monitor *mon)
  69. {
  70. UuidInfo *info;
  71. info = qmp_query_uuid(NULL);
  72. monitor_printf(mon, "%s\n", info->UUID);
  73. qapi_free_UuidInfo(info);
  74. }
  75. void hmp_info_chardev(Monitor *mon)
  76. {
  77. ChardevInfoList *char_info, *info;
  78. char_info = qmp_query_chardev(NULL);
  79. for (info = char_info; info; info = info->next) {
  80. monitor_printf(mon, "%s: filename=%s\n", info->value->label,
  81. info->value->filename);
  82. }
  83. qapi_free_ChardevInfoList(char_info);
  84. }
  85. void hmp_info_mice(Monitor *mon)
  86. {
  87. MouseInfoList *mice_list, *mouse;
  88. mice_list = qmp_query_mice(NULL);
  89. if (!mice_list) {
  90. monitor_printf(mon, "No mouse devices connected\n");
  91. return;
  92. }
  93. for (mouse = mice_list; mouse; mouse = mouse->next) {
  94. monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
  95. mouse->value->current ? '*' : ' ',
  96. mouse->value->index, mouse->value->name,
  97. mouse->value->absolute ? " (absolute)" : "");
  98. }
  99. qapi_free_MouseInfoList(mice_list);
  100. }
  101. void hmp_info_migrate(Monitor *mon)
  102. {
  103. MigrationInfo *info;
  104. info = qmp_query_migrate(NULL);
  105. if (info->has_status) {
  106. monitor_printf(mon, "Migration status: %s\n", info->status);
  107. }
  108. if (info->has_ram) {
  109. monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
  110. info->ram->transferred >> 10);
  111. monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
  112. info->ram->remaining >> 10);
  113. monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
  114. info->ram->total >> 10);
  115. }
  116. if (info->has_disk) {
  117. monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
  118. info->disk->transferred >> 10);
  119. monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
  120. info->disk->remaining >> 10);
  121. monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
  122. info->disk->total >> 10);
  123. }
  124. qapi_free_MigrationInfo(info);
  125. }
  126. void hmp_info_cpus(Monitor *mon)
  127. {
  128. CpuInfoList *cpu_list, *cpu;
  129. cpu_list = qmp_query_cpus(NULL);
  130. for (cpu = cpu_list; cpu; cpu = cpu->next) {
  131. int active = ' ';
  132. if (cpu->value->CPU == monitor_get_cpu_index()) {
  133. active = '*';
  134. }
  135. monitor_printf(mon, "%c CPU #%" PRId64 ": ", active, cpu->value->CPU);
  136. if (cpu->value->has_pc) {
  137. monitor_printf(mon, "pc=0x%016" PRIx64, cpu->value->pc);
  138. }
  139. if (cpu->value->has_nip) {
  140. monitor_printf(mon, "nip=0x%016" PRIx64, cpu->value->nip);
  141. }
  142. if (cpu->value->has_npc) {
  143. monitor_printf(mon, "pc=0x%016" PRIx64, cpu->value->pc);
  144. monitor_printf(mon, "npc=0x%016" PRIx64, cpu->value->npc);
  145. }
  146. if (cpu->value->has_PC) {
  147. monitor_printf(mon, "PC=0x%016" PRIx64, cpu->value->PC);
  148. }
  149. if (cpu->value->halted) {
  150. monitor_printf(mon, " (halted)");
  151. }
  152. monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
  153. }
  154. qapi_free_CpuInfoList(cpu_list);
  155. }
  156. void hmp_info_block(Monitor *mon)
  157. {
  158. BlockInfoList *block_list, *info;
  159. block_list = qmp_query_block(NULL);
  160. for (info = block_list; info; info = info->next) {
  161. monitor_printf(mon, "%s: removable=%d",
  162. info->value->device, info->value->removable);
  163. if (info->value->removable) {
  164. monitor_printf(mon, " locked=%d", info->value->locked);
  165. monitor_printf(mon, " tray-open=%d", info->value->tray_open);
  166. }
  167. if (info->value->has_io_status) {
  168. monitor_printf(mon, " io-status=%s",
  169. BlockDeviceIoStatus_lookup[info->value->io_status]);
  170. }
  171. if (info->value->has_inserted) {
  172. monitor_printf(mon, " file=");
  173. monitor_print_filename(mon, info->value->inserted->file);
  174. if (info->value->inserted->has_backing_file) {
  175. monitor_printf(mon, " backing_file=");
  176. monitor_print_filename(mon, info->value->inserted->backing_file);
  177. }
  178. monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
  179. info->value->inserted->ro,
  180. info->value->inserted->drv,
  181. info->value->inserted->encrypted);
  182. monitor_printf(mon, " bps=%" PRId64 " bps_rd=%" PRId64
  183. " bps_wr=%" PRId64 " iops=%" PRId64
  184. " iops_rd=%" PRId64 " iops_wr=%" PRId64,
  185. info->value->inserted->bps,
  186. info->value->inserted->bps_rd,
  187. info->value->inserted->bps_wr,
  188. info->value->inserted->iops,
  189. info->value->inserted->iops_rd,
  190. info->value->inserted->iops_wr);
  191. } else {
  192. monitor_printf(mon, " [not inserted]");
  193. }
  194. monitor_printf(mon, "\n");
  195. }
  196. qapi_free_BlockInfoList(block_list);
  197. }
  198. void hmp_info_blockstats(Monitor *mon)
  199. {
  200. BlockStatsList *stats_list, *stats;
  201. stats_list = qmp_query_blockstats(NULL);
  202. for (stats = stats_list; stats; stats = stats->next) {
  203. if (!stats->value->has_device) {
  204. continue;
  205. }
  206. monitor_printf(mon, "%s:", stats->value->device);
  207. monitor_printf(mon, " rd_bytes=%" PRId64
  208. " wr_bytes=%" PRId64
  209. " rd_operations=%" PRId64
  210. " wr_operations=%" PRId64
  211. " flush_operations=%" PRId64
  212. " wr_total_time_ns=%" PRId64
  213. " rd_total_time_ns=%" PRId64
  214. " flush_total_time_ns=%" PRId64
  215. "\n",
  216. stats->value->stats->rd_bytes,
  217. stats->value->stats->wr_bytes,
  218. stats->value->stats->rd_operations,
  219. stats->value->stats->wr_operations,
  220. stats->value->stats->flush_operations,
  221. stats->value->stats->wr_total_time_ns,
  222. stats->value->stats->rd_total_time_ns,
  223. stats->value->stats->flush_total_time_ns);
  224. }
  225. qapi_free_BlockStatsList(stats_list);
  226. }
  227. void hmp_info_vnc(Monitor *mon)
  228. {
  229. VncInfo *info;
  230. Error *err = NULL;
  231. VncClientInfoList *client;
  232. info = qmp_query_vnc(&err);
  233. if (err) {
  234. monitor_printf(mon, "%s\n", error_get_pretty(err));
  235. error_free(err);
  236. return;
  237. }
  238. if (!info->enabled) {
  239. monitor_printf(mon, "Server: disabled\n");
  240. goto out;
  241. }
  242. monitor_printf(mon, "Server:\n");
  243. if (info->has_host && info->has_service) {
  244. monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
  245. }
  246. if (info->has_auth) {
  247. monitor_printf(mon, " auth: %s\n", info->auth);
  248. }
  249. if (!info->has_clients || info->clients == NULL) {
  250. monitor_printf(mon, "Client: none\n");
  251. } else {
  252. for (client = info->clients; client; client = client->next) {
  253. monitor_printf(mon, "Client:\n");
  254. monitor_printf(mon, " address: %s:%s\n",
  255. client->value->host, client->value->service);
  256. monitor_printf(mon, " x509_dname: %s\n",
  257. client->value->x509_dname ?
  258. client->value->x509_dname : "none");
  259. monitor_printf(mon, " username: %s\n",
  260. client->value->has_sasl_username ?
  261. client->value->sasl_username : "none");
  262. }
  263. }
  264. out:
  265. qapi_free_VncInfo(info);
  266. }
  267. void hmp_info_spice(Monitor *mon)
  268. {
  269. SpiceChannelList *chan;
  270. SpiceInfo *info;
  271. info = qmp_query_spice(NULL);
  272. if (!info->enabled) {
  273. monitor_printf(mon, "Server: disabled\n");
  274. goto out;
  275. }
  276. monitor_printf(mon, "Server:\n");
  277. if (info->has_port) {
  278. monitor_printf(mon, " address: %s:%" PRId64 "\n",
  279. info->host, info->port);
  280. }
  281. if (info->has_tls_port) {
  282. monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
  283. info->host, info->tls_port);
  284. }
  285. monitor_printf(mon, " auth: %s\n", info->auth);
  286. monitor_printf(mon, " compiled: %s\n", info->compiled_version);
  287. monitor_printf(mon, " mouse-mode: %s\n",
  288. SpiceQueryMouseMode_lookup[info->mouse_mode]);
  289. if (!info->has_channels || info->channels == NULL) {
  290. monitor_printf(mon, "Channels: none\n");
  291. } else {
  292. for (chan = info->channels; chan; chan = chan->next) {
  293. monitor_printf(mon, "Channel:\n");
  294. monitor_printf(mon, " address: %s:%s%s\n",
  295. chan->value->host, chan->value->port,
  296. chan->value->tls ? " [tls]" : "");
  297. monitor_printf(mon, " session: %" PRId64 "\n",
  298. chan->value->connection_id);
  299. monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
  300. chan->value->channel_type, chan->value->channel_id);
  301. }
  302. }
  303. out:
  304. qapi_free_SpiceInfo(info);
  305. }
  306. void hmp_info_balloon(Monitor *mon)
  307. {
  308. BalloonInfo *info;
  309. Error *err = NULL;
  310. info = qmp_query_balloon(&err);
  311. if (err) {
  312. monitor_printf(mon, "%s\n", error_get_pretty(err));
  313. error_free(err);
  314. return;
  315. }
  316. monitor_printf(mon, "balloon: actual=%" PRId64, info->actual >> 20);
  317. if (info->has_mem_swapped_in) {
  318. monitor_printf(mon, " mem_swapped_in=%" PRId64, info->mem_swapped_in);
  319. }
  320. if (info->has_mem_swapped_out) {
  321. monitor_printf(mon, " mem_swapped_out=%" PRId64, info->mem_swapped_out);
  322. }
  323. if (info->has_major_page_faults) {
  324. monitor_printf(mon, " major_page_faults=%" PRId64,
  325. info->major_page_faults);
  326. }
  327. if (info->has_minor_page_faults) {
  328. monitor_printf(mon, " minor_page_faults=%" PRId64,
  329. info->minor_page_faults);
  330. }
  331. if (info->has_free_mem) {
  332. monitor_printf(mon, " free_mem=%" PRId64, info->free_mem);
  333. }
  334. if (info->has_total_mem) {
  335. monitor_printf(mon, " total_mem=%" PRId64, info->total_mem);
  336. }
  337. monitor_printf(mon, "\n");
  338. qapi_free_BalloonInfo(info);
  339. }
  340. static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
  341. {
  342. PciMemoryRegionList *region;
  343. monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
  344. monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
  345. dev->slot, dev->function);
  346. monitor_printf(mon, " ");
  347. if (dev->class_info.has_desc) {
  348. monitor_printf(mon, "%s", dev->class_info.desc);
  349. } else {
  350. monitor_printf(mon, "Class %04" PRId64, dev->class_info.class);
  351. }
  352. monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
  353. dev->id.vendor, dev->id.device);
  354. if (dev->has_irq) {
  355. monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
  356. }
  357. if (dev->has_pci_bridge) {
  358. monitor_printf(mon, " BUS %" PRId64 ".\n",
  359. dev->pci_bridge->bus.number);
  360. monitor_printf(mon, " secondary bus %" PRId64 ".\n",
  361. dev->pci_bridge->bus.secondary);
  362. monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
  363. dev->pci_bridge->bus.subordinate);
  364. monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
  365. dev->pci_bridge->bus.io_range->base,
  366. dev->pci_bridge->bus.io_range->limit);
  367. monitor_printf(mon,
  368. " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
  369. dev->pci_bridge->bus.memory_range->base,
  370. dev->pci_bridge->bus.memory_range->limit);
  371. monitor_printf(mon, " prefetchable memory range "
  372. "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
  373. dev->pci_bridge->bus.prefetchable_range->base,
  374. dev->pci_bridge->bus.prefetchable_range->limit);
  375. }
  376. for (region = dev->regions; region; region = region->next) {
  377. uint64_t addr, size;
  378. addr = region->value->address;
  379. size = region->value->size;
  380. monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
  381. if (!strcmp(region->value->type, "io")) {
  382. monitor_printf(mon, "I/O at 0x%04" PRIx64
  383. " [0x%04" PRIx64 "].\n",
  384. addr, addr + size - 1);
  385. } else {
  386. monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
  387. " [0x%08" PRIx64 "].\n",
  388. region->value->mem_type_64 ? 64 : 32,
  389. region->value->prefetch ? " prefetchable" : "",
  390. addr, addr + size - 1);
  391. }
  392. }
  393. monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
  394. if (dev->has_pci_bridge) {
  395. if (dev->pci_bridge->has_devices) {
  396. PciDeviceInfoList *cdev;
  397. for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
  398. hmp_info_pci_device(mon, cdev->value);
  399. }
  400. }
  401. }
  402. }
  403. void hmp_info_pci(Monitor *mon)
  404. {
  405. PciInfoList *info_list, *info;
  406. Error *err = NULL;
  407. info_list = qmp_query_pci(&err);
  408. if (err) {
  409. monitor_printf(mon, "PCI devices not supported\n");
  410. error_free(err);
  411. return;
  412. }
  413. for (info = info_list; info; info = info->next) {
  414. PciDeviceInfoList *dev;
  415. for (dev = info->value->devices; dev; dev = dev->next) {
  416. hmp_info_pci_device(mon, dev->value);
  417. }
  418. }
  419. qapi_free_PciInfoList(info_list);
  420. }
  421. void hmp_info_block_jobs(Monitor *mon)
  422. {
  423. BlockJobInfoList *list;
  424. Error *err = NULL;
  425. list = qmp_query_block_jobs(&err);
  426. assert(!err);
  427. if (!list) {
  428. monitor_printf(mon, "No active jobs\n");
  429. return;
  430. }
  431. while (list) {
  432. if (strcmp(list->value->type, "stream") == 0) {
  433. monitor_printf(mon, "Streaming device %s: Completed %" PRId64
  434. " of %" PRId64 " bytes, speed limit %" PRId64
  435. " bytes/s\n",
  436. list->value->device,
  437. list->value->offset,
  438. list->value->len,
  439. list->value->speed);
  440. } else {
  441. monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
  442. " of %" PRId64 " bytes, speed limit %" PRId64
  443. " bytes/s\n",
  444. list->value->type,
  445. list->value->device,
  446. list->value->offset,
  447. list->value->len,
  448. list->value->speed);
  449. }
  450. list = list->next;
  451. }
  452. }
  453. void hmp_quit(Monitor *mon, const QDict *qdict)
  454. {
  455. monitor_suspend(mon);
  456. qmp_quit(NULL);
  457. }
  458. void hmp_stop(Monitor *mon, const QDict *qdict)
  459. {
  460. qmp_stop(NULL);
  461. }
  462. void hmp_system_reset(Monitor *mon, const QDict *qdict)
  463. {
  464. qmp_system_reset(NULL);
  465. }
  466. void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
  467. {
  468. qmp_system_powerdown(NULL);
  469. }
  470. void hmp_cpu(Monitor *mon, const QDict *qdict)
  471. {
  472. int64_t cpu_index;
  473. /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
  474. use it are converted to the QAPI */
  475. cpu_index = qdict_get_int(qdict, "index");
  476. if (monitor_set_cpu(cpu_index) < 0) {
  477. monitor_printf(mon, "invalid CPU index\n");
  478. }
  479. }
  480. void hmp_memsave(Monitor *mon, const QDict *qdict)
  481. {
  482. uint32_t size = qdict_get_int(qdict, "size");
  483. const char *filename = qdict_get_str(qdict, "filename");
  484. uint64_t addr = qdict_get_int(qdict, "val");
  485. Error *errp = NULL;
  486. qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &errp);
  487. hmp_handle_error(mon, &errp);
  488. }
  489. void hmp_pmemsave(Monitor *mon, const QDict *qdict)
  490. {
  491. uint32_t size = qdict_get_int(qdict, "size");
  492. const char *filename = qdict_get_str(qdict, "filename");
  493. uint64_t addr = qdict_get_int(qdict, "val");
  494. Error *errp = NULL;
  495. qmp_pmemsave(addr, size, filename, &errp);
  496. hmp_handle_error(mon, &errp);
  497. }
  498. static void hmp_cont_cb(void *opaque, int err)
  499. {
  500. Monitor *mon = opaque;
  501. if (!err) {
  502. hmp_cont(mon, NULL);
  503. }
  504. }
  505. void hmp_cont(Monitor *mon, const QDict *qdict)
  506. {
  507. Error *errp = NULL;
  508. qmp_cont(&errp);
  509. if (error_is_set(&errp)) {
  510. if (error_is_type(errp, QERR_DEVICE_ENCRYPTED)) {
  511. const char *device;
  512. /* The device is encrypted. Ask the user for the password
  513. and retry */
  514. device = error_get_field(errp, "device");
  515. assert(device != NULL);
  516. monitor_read_block_device_key(mon, device, hmp_cont_cb, mon);
  517. error_free(errp);
  518. return;
  519. }
  520. hmp_handle_error(mon, &errp);
  521. }
  522. }
  523. void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
  524. {
  525. qmp_system_wakeup(NULL);
  526. }
  527. void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
  528. {
  529. Error *errp = NULL;
  530. qmp_inject_nmi(&errp);
  531. hmp_handle_error(mon, &errp);
  532. }
  533. void hmp_set_link(Monitor *mon, const QDict *qdict)
  534. {
  535. const char *name = qdict_get_str(qdict, "name");
  536. int up = qdict_get_bool(qdict, "up");
  537. Error *errp = NULL;
  538. qmp_set_link(name, up, &errp);
  539. hmp_handle_error(mon, &errp);
  540. }
  541. void hmp_block_passwd(Monitor *mon, const QDict *qdict)
  542. {
  543. const char *device = qdict_get_str(qdict, "device");
  544. const char *password = qdict_get_str(qdict, "password");
  545. Error *errp = NULL;
  546. qmp_block_passwd(device, password, &errp);
  547. hmp_handle_error(mon, &errp);
  548. }
  549. void hmp_balloon(Monitor *mon, const QDict *qdict)
  550. {
  551. int64_t value = qdict_get_int(qdict, "value");
  552. Error *errp = NULL;
  553. qmp_balloon(value, &errp);
  554. if (error_is_set(&errp)) {
  555. monitor_printf(mon, "balloon: %s\n", error_get_pretty(errp));
  556. error_free(errp);
  557. }
  558. }
  559. void hmp_block_resize(Monitor *mon, const QDict *qdict)
  560. {
  561. const char *device = qdict_get_str(qdict, "device");
  562. int64_t size = qdict_get_int(qdict, "size");
  563. Error *errp = NULL;
  564. qmp_block_resize(device, size, &errp);
  565. hmp_handle_error(mon, &errp);
  566. }
  567. void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
  568. {
  569. const char *device = qdict_get_str(qdict, "device");
  570. const char *filename = qdict_get_try_str(qdict, "snapshot-file");
  571. const char *format = qdict_get_try_str(qdict, "format");
  572. int reuse = qdict_get_try_bool(qdict, "reuse", 0);
  573. enum NewImageMode mode;
  574. Error *errp = NULL;
  575. if (!filename) {
  576. /* In the future, if 'snapshot-file' is not specified, the snapshot
  577. will be taken internally. Today it's actually required. */
  578. error_set(&errp, QERR_MISSING_PARAMETER, "snapshot-file");
  579. hmp_handle_error(mon, &errp);
  580. return;
  581. }
  582. mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
  583. qmp_blockdev_snapshot_sync(device, filename, !!format, format,
  584. true, mode, &errp);
  585. hmp_handle_error(mon, &errp);
  586. }
  587. void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
  588. {
  589. qmp_migrate_cancel(NULL);
  590. }
  591. void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
  592. {
  593. double value = qdict_get_double(qdict, "value");
  594. qmp_migrate_set_downtime(value, NULL);
  595. }
  596. void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
  597. {
  598. int64_t value = qdict_get_int(qdict, "value");
  599. qmp_migrate_set_speed(value, NULL);
  600. }
  601. void hmp_set_password(Monitor *mon, const QDict *qdict)
  602. {
  603. const char *protocol = qdict_get_str(qdict, "protocol");
  604. const char *password = qdict_get_str(qdict, "password");
  605. const char *connected = qdict_get_try_str(qdict, "connected");
  606. Error *err = NULL;
  607. qmp_set_password(protocol, password, !!connected, connected, &err);
  608. hmp_handle_error(mon, &err);
  609. }
  610. void hmp_expire_password(Monitor *mon, const QDict *qdict)
  611. {
  612. const char *protocol = qdict_get_str(qdict, "protocol");
  613. const char *whenstr = qdict_get_str(qdict, "time");
  614. Error *err = NULL;
  615. qmp_expire_password(protocol, whenstr, &err);
  616. hmp_handle_error(mon, &err);
  617. }
  618. void hmp_eject(Monitor *mon, const QDict *qdict)
  619. {
  620. int force = qdict_get_try_bool(qdict, "force", 0);
  621. const char *device = qdict_get_str(qdict, "device");
  622. Error *err = NULL;
  623. qmp_eject(device, true, force, &err);
  624. hmp_handle_error(mon, &err);
  625. }
  626. static void hmp_change_read_arg(Monitor *mon, const char *password,
  627. void *opaque)
  628. {
  629. qmp_change_vnc_password(password, NULL);
  630. monitor_read_command(mon, 1);
  631. }
  632. static void cb_hmp_change_bdrv_pwd(Monitor *mon, const char *password,
  633. void *opaque)
  634. {
  635. Error *encryption_err = opaque;
  636. Error *err = NULL;
  637. const char *device;
  638. device = error_get_field(encryption_err, "device");
  639. qmp_block_passwd(device, password, &err);
  640. hmp_handle_error(mon, &err);
  641. error_free(encryption_err);
  642. monitor_read_command(mon, 1);
  643. }
  644. void hmp_change(Monitor *mon, const QDict *qdict)
  645. {
  646. const char *device = qdict_get_str(qdict, "device");
  647. const char *target = qdict_get_str(qdict, "target");
  648. const char *arg = qdict_get_try_str(qdict, "arg");
  649. Error *err = NULL;
  650. if (strcmp(device, "vnc") == 0 &&
  651. (strcmp(target, "passwd") == 0 ||
  652. strcmp(target, "password") == 0)) {
  653. if (!arg) {
  654. monitor_read_password(mon, hmp_change_read_arg, NULL);
  655. return;
  656. }
  657. }
  658. qmp_change(device, target, !!arg, arg, &err);
  659. if (error_is_type(err, QERR_DEVICE_ENCRYPTED)) {
  660. monitor_printf(mon, "%s (%s) is encrypted.\n",
  661. error_get_field(err, "device"),
  662. error_get_field(err, "filename"));
  663. if (!monitor_get_rs(mon)) {
  664. monitor_printf(mon,
  665. "terminal does not support password prompting\n");
  666. error_free(err);
  667. return;
  668. }
  669. readline_start(monitor_get_rs(mon), "Password: ", 1,
  670. cb_hmp_change_bdrv_pwd, err);
  671. return;
  672. }
  673. hmp_handle_error(mon, &err);
  674. }
  675. void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
  676. {
  677. Error *err = NULL;
  678. qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
  679. qdict_get_int(qdict, "bps"),
  680. qdict_get_int(qdict, "bps_rd"),
  681. qdict_get_int(qdict, "bps_wr"),
  682. qdict_get_int(qdict, "iops"),
  683. qdict_get_int(qdict, "iops_rd"),
  684. qdict_get_int(qdict, "iops_wr"), &err);
  685. hmp_handle_error(mon, &err);
  686. }
  687. void hmp_block_stream(Monitor *mon, const QDict *qdict)
  688. {
  689. Error *error = NULL;
  690. const char *device = qdict_get_str(qdict, "device");
  691. const char *base = qdict_get_try_str(qdict, "base");
  692. int64_t speed = qdict_get_try_int(qdict, "speed", 0);
  693. qmp_block_stream(device, base != NULL, base,
  694. qdict_haskey(qdict, "speed"), speed, &error);
  695. hmp_handle_error(mon, &error);
  696. }
  697. void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
  698. {
  699. Error *error = NULL;
  700. const char *device = qdict_get_str(qdict, "device");
  701. int64_t value = qdict_get_int(qdict, "speed");
  702. qmp_block_job_set_speed(device, value, &error);
  703. hmp_handle_error(mon, &error);
  704. }
  705. void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
  706. {
  707. Error *error = NULL;
  708. const char *device = qdict_get_str(qdict, "device");
  709. qmp_block_job_cancel(device, &error);
  710. hmp_handle_error(mon, &error);
  711. }
  712. typedef struct MigrationStatus
  713. {
  714. QEMUTimer *timer;
  715. Monitor *mon;
  716. bool is_block_migration;
  717. } MigrationStatus;
  718. static void hmp_migrate_status_cb(void *opaque)
  719. {
  720. MigrationStatus *status = opaque;
  721. MigrationInfo *info;
  722. info = qmp_query_migrate(NULL);
  723. if (!info->has_status || strcmp(info->status, "active") == 0) {
  724. if (info->has_disk) {
  725. int progress;
  726. if (info->disk->remaining) {
  727. progress = info->disk->transferred * 100 / info->disk->total;
  728. } else {
  729. progress = 100;
  730. }
  731. monitor_printf(status->mon, "Completed %d %%\r", progress);
  732. monitor_flush(status->mon);
  733. }
  734. qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock) + 1000);
  735. } else {
  736. if (status->is_block_migration) {
  737. monitor_printf(status->mon, "\n");
  738. }
  739. monitor_resume(status->mon);
  740. qemu_del_timer(status->timer);
  741. g_free(status);
  742. }
  743. qapi_free_MigrationInfo(info);
  744. }
  745. void hmp_migrate(Monitor *mon, const QDict *qdict)
  746. {
  747. int detach = qdict_get_try_bool(qdict, "detach", 0);
  748. int blk = qdict_get_try_bool(qdict, "blk", 0);
  749. int inc = qdict_get_try_bool(qdict, "inc", 0);
  750. const char *uri = qdict_get_str(qdict, "uri");
  751. Error *err = NULL;
  752. qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
  753. if (err) {
  754. monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
  755. error_free(err);
  756. return;
  757. }
  758. if (!detach) {
  759. MigrationStatus *status;
  760. if (monitor_suspend(mon) < 0) {
  761. monitor_printf(mon, "terminal does not allow synchronous "
  762. "migration, continuing detached\n");
  763. return;
  764. }
  765. status = g_malloc0(sizeof(*status));
  766. status->mon = mon;
  767. status->is_block_migration = blk || inc;
  768. status->timer = qemu_new_timer_ms(rt_clock, hmp_migrate_status_cb,
  769. status);
  770. qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock));
  771. }
  772. }
  773. void hmp_device_del(Monitor *mon, const QDict *qdict)
  774. {
  775. const char *id = qdict_get_str(qdict, "id");
  776. Error *err = NULL;
  777. qmp_device_del(id, &err);
  778. hmp_handle_error(mon, &err);
  779. }