2
0

hmp.c 32 KB

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