2
0

hmp.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760
  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/net.h"
  17. #include "sysemu/char.h"
  18. #include "qemu/option.h"
  19. #include "qemu/timer.h"
  20. #include "qmp-commands.h"
  21. #include "qemu/sockets.h"
  22. #include "monitor/monitor.h"
  23. #include "qapi/opts-visitor.h"
  24. #include "qapi/string-output-visitor.h"
  25. #include "qapi-visit.h"
  26. #include "ui/console.h"
  27. #include "block/qapi.h"
  28. #include "qemu-io.h"
  29. static void hmp_handle_error(Monitor *mon, Error **errp)
  30. {
  31. assert(errp);
  32. if (*errp) {
  33. monitor_printf(mon, "%s\n", error_get_pretty(*errp));
  34. error_free(*errp);
  35. }
  36. }
  37. void hmp_info_name(Monitor *mon, const QDict *qdict)
  38. {
  39. NameInfo *info;
  40. info = qmp_query_name(NULL);
  41. if (info->has_name) {
  42. monitor_printf(mon, "%s\n", info->name);
  43. }
  44. qapi_free_NameInfo(info);
  45. }
  46. void hmp_info_version(Monitor *mon, const QDict *qdict)
  47. {
  48. VersionInfo *info;
  49. info = qmp_query_version(NULL);
  50. monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
  51. info->qemu.major, info->qemu.minor, info->qemu.micro,
  52. info->package);
  53. qapi_free_VersionInfo(info);
  54. }
  55. void hmp_info_kvm(Monitor *mon, const QDict *qdict)
  56. {
  57. KvmInfo *info;
  58. info = qmp_query_kvm(NULL);
  59. monitor_printf(mon, "kvm support: ");
  60. if (info->present) {
  61. monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
  62. } else {
  63. monitor_printf(mon, "not compiled\n");
  64. }
  65. qapi_free_KvmInfo(info);
  66. }
  67. void hmp_info_status(Monitor *mon, const QDict *qdict)
  68. {
  69. StatusInfo *info;
  70. info = qmp_query_status(NULL);
  71. monitor_printf(mon, "VM status: %s%s",
  72. info->running ? "running" : "paused",
  73. info->singlestep ? " (single step mode)" : "");
  74. if (!info->running && info->status != RUN_STATE_PAUSED) {
  75. monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
  76. }
  77. monitor_printf(mon, "\n");
  78. qapi_free_StatusInfo(info);
  79. }
  80. void hmp_info_uuid(Monitor *mon, const QDict *qdict)
  81. {
  82. UuidInfo *info;
  83. info = qmp_query_uuid(NULL);
  84. monitor_printf(mon, "%s\n", info->UUID);
  85. qapi_free_UuidInfo(info);
  86. }
  87. void hmp_info_chardev(Monitor *mon, const QDict *qdict)
  88. {
  89. ChardevInfoList *char_info, *info;
  90. char_info = qmp_query_chardev(NULL);
  91. for (info = char_info; info; info = info->next) {
  92. monitor_printf(mon, "%s: filename=%s\n", info->value->label,
  93. info->value->filename);
  94. }
  95. qapi_free_ChardevInfoList(char_info);
  96. }
  97. void hmp_info_mice(Monitor *mon, const QDict *qdict)
  98. {
  99. MouseInfoList *mice_list, *mouse;
  100. mice_list = qmp_query_mice(NULL);
  101. if (!mice_list) {
  102. monitor_printf(mon, "No mouse devices connected\n");
  103. return;
  104. }
  105. for (mouse = mice_list; mouse; mouse = mouse->next) {
  106. monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
  107. mouse->value->current ? '*' : ' ',
  108. mouse->value->index, mouse->value->name,
  109. mouse->value->absolute ? " (absolute)" : "");
  110. }
  111. qapi_free_MouseInfoList(mice_list);
  112. }
  113. void hmp_info_migrate(Monitor *mon, const QDict *qdict)
  114. {
  115. MigrationInfo *info;
  116. MigrationCapabilityStatusList *caps, *cap;
  117. info = qmp_query_migrate(NULL);
  118. caps = qmp_query_migrate_capabilities(NULL);
  119. /* do not display parameters during setup */
  120. if (info->has_status && caps) {
  121. monitor_printf(mon, "capabilities: ");
  122. for (cap = caps; cap; cap = cap->next) {
  123. monitor_printf(mon, "%s: %s ",
  124. MigrationCapability_lookup[cap->value->capability],
  125. cap->value->state ? "on" : "off");
  126. }
  127. monitor_printf(mon, "\n");
  128. }
  129. if (info->has_status) {
  130. monitor_printf(mon, "Migration status: %s\n", info->status);
  131. monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
  132. info->total_time);
  133. if (info->has_expected_downtime) {
  134. monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
  135. info->expected_downtime);
  136. }
  137. if (info->has_downtime) {
  138. monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
  139. info->downtime);
  140. }
  141. if (info->has_setup_time) {
  142. monitor_printf(mon, "setup: %" PRIu64 " milliseconds\n",
  143. info->setup_time);
  144. }
  145. }
  146. if (info->has_ram) {
  147. monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
  148. info->ram->transferred >> 10);
  149. monitor_printf(mon, "throughput: %0.2f mbps\n",
  150. info->ram->mbps);
  151. monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
  152. info->ram->remaining >> 10);
  153. monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
  154. info->ram->total >> 10);
  155. monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
  156. info->ram->duplicate);
  157. monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
  158. info->ram->skipped);
  159. monitor_printf(mon, "normal: %" PRIu64 " pages\n",
  160. info->ram->normal);
  161. monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
  162. info->ram->normal_bytes >> 10);
  163. monitor_printf(mon, "dirty sync count: %" PRIu64 "\n",
  164. info->ram->dirty_sync_count);
  165. if (info->ram->dirty_pages_rate) {
  166. monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
  167. info->ram->dirty_pages_rate);
  168. }
  169. }
  170. if (info->has_disk) {
  171. monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
  172. info->disk->transferred >> 10);
  173. monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
  174. info->disk->remaining >> 10);
  175. monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
  176. info->disk->total >> 10);
  177. }
  178. if (info->has_xbzrle_cache) {
  179. monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
  180. info->xbzrle_cache->cache_size);
  181. monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
  182. info->xbzrle_cache->bytes >> 10);
  183. monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
  184. info->xbzrle_cache->pages);
  185. monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
  186. info->xbzrle_cache->cache_miss);
  187. monitor_printf(mon, "xbzrle cache miss rate: %0.2f\n",
  188. info->xbzrle_cache->cache_miss_rate);
  189. monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
  190. info->xbzrle_cache->overflow);
  191. }
  192. qapi_free_MigrationInfo(info);
  193. qapi_free_MigrationCapabilityStatusList(caps);
  194. }
  195. void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
  196. {
  197. MigrationCapabilityStatusList *caps, *cap;
  198. caps = qmp_query_migrate_capabilities(NULL);
  199. if (caps) {
  200. monitor_printf(mon, "capabilities: ");
  201. for (cap = caps; cap; cap = cap->next) {
  202. monitor_printf(mon, "%s: %s ",
  203. MigrationCapability_lookup[cap->value->capability],
  204. cap->value->state ? "on" : "off");
  205. }
  206. monitor_printf(mon, "\n");
  207. }
  208. qapi_free_MigrationCapabilityStatusList(caps);
  209. }
  210. void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
  211. {
  212. monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
  213. qmp_query_migrate_cache_size(NULL) >> 10);
  214. }
  215. void hmp_info_cpus(Monitor *mon, const QDict *qdict)
  216. {
  217. CpuInfoList *cpu_list, *cpu;
  218. cpu_list = qmp_query_cpus(NULL);
  219. for (cpu = cpu_list; cpu; cpu = cpu->next) {
  220. int active = ' ';
  221. if (cpu->value->CPU == monitor_get_cpu_index()) {
  222. active = '*';
  223. }
  224. monitor_printf(mon, "%c CPU #%" PRId64 ":", active, cpu->value->CPU);
  225. if (cpu->value->has_pc) {
  226. monitor_printf(mon, " pc=0x%016" PRIx64, cpu->value->pc);
  227. }
  228. if (cpu->value->has_nip) {
  229. monitor_printf(mon, " nip=0x%016" PRIx64, cpu->value->nip);
  230. }
  231. if (cpu->value->has_npc) {
  232. monitor_printf(mon, " npc=0x%016" PRIx64, cpu->value->npc);
  233. }
  234. if (cpu->value->has_PC) {
  235. monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->PC);
  236. }
  237. if (cpu->value->halted) {
  238. monitor_printf(mon, " (halted)");
  239. }
  240. monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
  241. }
  242. qapi_free_CpuInfoList(cpu_list);
  243. }
  244. void hmp_info_block(Monitor *mon, const QDict *qdict)
  245. {
  246. BlockInfoList *block_list, *info;
  247. ImageInfo *image_info;
  248. const char *device = qdict_get_try_str(qdict, "device");
  249. bool verbose = qdict_get_try_bool(qdict, "verbose", 0);
  250. block_list = qmp_query_block(NULL);
  251. for (info = block_list; info; info = info->next) {
  252. if (device && strcmp(device, info->value->device)) {
  253. continue;
  254. }
  255. if (info != block_list) {
  256. monitor_printf(mon, "\n");
  257. }
  258. monitor_printf(mon, "%s", info->value->device);
  259. if (info->value->has_inserted) {
  260. monitor_printf(mon, ": %s (%s%s%s)\n",
  261. info->value->inserted->file,
  262. info->value->inserted->drv,
  263. info->value->inserted->ro ? ", read-only" : "",
  264. info->value->inserted->encrypted ? ", encrypted" : "");
  265. } else {
  266. monitor_printf(mon, ": [not inserted]\n");
  267. }
  268. if (info->value->has_io_status && info->value->io_status != BLOCK_DEVICE_IO_STATUS_OK) {
  269. monitor_printf(mon, " I/O status: %s\n",
  270. BlockDeviceIoStatus_lookup[info->value->io_status]);
  271. }
  272. if (info->value->removable) {
  273. monitor_printf(mon, " Removable device: %slocked, tray %s\n",
  274. info->value->locked ? "" : "not ",
  275. info->value->tray_open ? "open" : "closed");
  276. }
  277. if (!info->value->has_inserted) {
  278. continue;
  279. }
  280. if (info->value->inserted->has_backing_file) {
  281. monitor_printf(mon,
  282. " Backing file: %s "
  283. "(chain depth: %" PRId64 ")\n",
  284. info->value->inserted->backing_file,
  285. info->value->inserted->backing_file_depth);
  286. }
  287. if (info->value->inserted->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF) {
  288. monitor_printf(mon, " Detect zeroes: %s\n",
  289. BlockdevDetectZeroesOptions_lookup[info->value->inserted->detect_zeroes]);
  290. }
  291. if (info->value->inserted->bps
  292. || info->value->inserted->bps_rd
  293. || info->value->inserted->bps_wr
  294. || info->value->inserted->iops
  295. || info->value->inserted->iops_rd
  296. || info->value->inserted->iops_wr)
  297. {
  298. monitor_printf(mon, " I/O throttling: bps=%" PRId64
  299. " bps_rd=%" PRId64 " bps_wr=%" PRId64
  300. " bps_max=%" PRId64
  301. " bps_rd_max=%" PRId64
  302. " bps_wr_max=%" PRId64
  303. " iops=%" PRId64 " iops_rd=%" PRId64
  304. " iops_wr=%" PRId64
  305. " iops_max=%" PRId64
  306. " iops_rd_max=%" PRId64
  307. " iops_wr_max=%" PRId64
  308. " iops_size=%" PRId64 "\n",
  309. info->value->inserted->bps,
  310. info->value->inserted->bps_rd,
  311. info->value->inserted->bps_wr,
  312. info->value->inserted->bps_max,
  313. info->value->inserted->bps_rd_max,
  314. info->value->inserted->bps_wr_max,
  315. info->value->inserted->iops,
  316. info->value->inserted->iops_rd,
  317. info->value->inserted->iops_wr,
  318. info->value->inserted->iops_max,
  319. info->value->inserted->iops_rd_max,
  320. info->value->inserted->iops_wr_max,
  321. info->value->inserted->iops_size);
  322. }
  323. if (verbose) {
  324. monitor_printf(mon, "\nImages:\n");
  325. image_info = info->value->inserted->image;
  326. while (1) {
  327. bdrv_image_info_dump((fprintf_function)monitor_printf,
  328. mon, image_info);
  329. if (image_info->has_backing_image) {
  330. image_info = image_info->backing_image;
  331. } else {
  332. break;
  333. }
  334. }
  335. }
  336. }
  337. qapi_free_BlockInfoList(block_list);
  338. }
  339. void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
  340. {
  341. BlockStatsList *stats_list, *stats;
  342. stats_list = qmp_query_blockstats(NULL);
  343. for (stats = stats_list; stats; stats = stats->next) {
  344. if (!stats->value->has_device) {
  345. continue;
  346. }
  347. monitor_printf(mon, "%s:", stats->value->device);
  348. monitor_printf(mon, " rd_bytes=%" PRId64
  349. " wr_bytes=%" PRId64
  350. " rd_operations=%" PRId64
  351. " wr_operations=%" PRId64
  352. " flush_operations=%" PRId64
  353. " wr_total_time_ns=%" PRId64
  354. " rd_total_time_ns=%" PRId64
  355. " flush_total_time_ns=%" PRId64
  356. "\n",
  357. stats->value->stats->rd_bytes,
  358. stats->value->stats->wr_bytes,
  359. stats->value->stats->rd_operations,
  360. stats->value->stats->wr_operations,
  361. stats->value->stats->flush_operations,
  362. stats->value->stats->wr_total_time_ns,
  363. stats->value->stats->rd_total_time_ns,
  364. stats->value->stats->flush_total_time_ns);
  365. }
  366. qapi_free_BlockStatsList(stats_list);
  367. }
  368. void hmp_info_vnc(Monitor *mon, const QDict *qdict)
  369. {
  370. VncInfo *info;
  371. Error *err = NULL;
  372. VncClientInfoList *client;
  373. info = qmp_query_vnc(&err);
  374. if (err) {
  375. monitor_printf(mon, "%s\n", error_get_pretty(err));
  376. error_free(err);
  377. return;
  378. }
  379. if (!info->enabled) {
  380. monitor_printf(mon, "Server: disabled\n");
  381. goto out;
  382. }
  383. monitor_printf(mon, "Server:\n");
  384. if (info->has_host && info->has_service) {
  385. monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
  386. }
  387. if (info->has_auth) {
  388. monitor_printf(mon, " auth: %s\n", info->auth);
  389. }
  390. if (!info->has_clients || info->clients == NULL) {
  391. monitor_printf(mon, "Client: none\n");
  392. } else {
  393. for (client = info->clients; client; client = client->next) {
  394. monitor_printf(mon, "Client:\n");
  395. monitor_printf(mon, " address: %s:%s\n",
  396. client->value->base->host,
  397. client->value->base->service);
  398. monitor_printf(mon, " x509_dname: %s\n",
  399. client->value->x509_dname ?
  400. client->value->x509_dname : "none");
  401. monitor_printf(mon, " username: %s\n",
  402. client->value->has_sasl_username ?
  403. client->value->sasl_username : "none");
  404. }
  405. }
  406. out:
  407. qapi_free_VncInfo(info);
  408. }
  409. void hmp_info_spice(Monitor *mon, const QDict *qdict)
  410. {
  411. SpiceChannelList *chan;
  412. SpiceInfo *info;
  413. info = qmp_query_spice(NULL);
  414. if (!info->enabled) {
  415. monitor_printf(mon, "Server: disabled\n");
  416. goto out;
  417. }
  418. monitor_printf(mon, "Server:\n");
  419. if (info->has_port) {
  420. monitor_printf(mon, " address: %s:%" PRId64 "\n",
  421. info->host, info->port);
  422. }
  423. if (info->has_tls_port) {
  424. monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
  425. info->host, info->tls_port);
  426. }
  427. monitor_printf(mon, " migrated: %s\n",
  428. info->migrated ? "true" : "false");
  429. monitor_printf(mon, " auth: %s\n", info->auth);
  430. monitor_printf(mon, " compiled: %s\n", info->compiled_version);
  431. monitor_printf(mon, " mouse-mode: %s\n",
  432. SpiceQueryMouseMode_lookup[info->mouse_mode]);
  433. if (!info->has_channels || info->channels == NULL) {
  434. monitor_printf(mon, "Channels: none\n");
  435. } else {
  436. for (chan = info->channels; chan; chan = chan->next) {
  437. monitor_printf(mon, "Channel:\n");
  438. monitor_printf(mon, " address: %s:%s%s\n",
  439. chan->value->base->host, chan->value->base->port,
  440. chan->value->tls ? " [tls]" : "");
  441. monitor_printf(mon, " session: %" PRId64 "\n",
  442. chan->value->connection_id);
  443. monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
  444. chan->value->channel_type, chan->value->channel_id);
  445. }
  446. }
  447. out:
  448. qapi_free_SpiceInfo(info);
  449. }
  450. void hmp_info_balloon(Monitor *mon, const QDict *qdict)
  451. {
  452. BalloonInfo *info;
  453. Error *err = NULL;
  454. info = qmp_query_balloon(&err);
  455. if (err) {
  456. monitor_printf(mon, "%s\n", error_get_pretty(err));
  457. error_free(err);
  458. return;
  459. }
  460. monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
  461. qapi_free_BalloonInfo(info);
  462. }
  463. static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
  464. {
  465. PciMemoryRegionList *region;
  466. monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
  467. monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
  468. dev->slot, dev->function);
  469. monitor_printf(mon, " ");
  470. if (dev->class_info.has_desc) {
  471. monitor_printf(mon, "%s", dev->class_info.desc);
  472. } else {
  473. monitor_printf(mon, "Class %04" PRId64, dev->class_info.q_class);
  474. }
  475. monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
  476. dev->id.vendor, dev->id.device);
  477. if (dev->has_irq) {
  478. monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
  479. }
  480. if (dev->has_pci_bridge) {
  481. monitor_printf(mon, " BUS %" PRId64 ".\n",
  482. dev->pci_bridge->bus.number);
  483. monitor_printf(mon, " secondary bus %" PRId64 ".\n",
  484. dev->pci_bridge->bus.secondary);
  485. monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
  486. dev->pci_bridge->bus.subordinate);
  487. monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
  488. dev->pci_bridge->bus.io_range->base,
  489. dev->pci_bridge->bus.io_range->limit);
  490. monitor_printf(mon,
  491. " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
  492. dev->pci_bridge->bus.memory_range->base,
  493. dev->pci_bridge->bus.memory_range->limit);
  494. monitor_printf(mon, " prefetchable memory range "
  495. "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
  496. dev->pci_bridge->bus.prefetchable_range->base,
  497. dev->pci_bridge->bus.prefetchable_range->limit);
  498. }
  499. for (region = dev->regions; region; region = region->next) {
  500. uint64_t addr, size;
  501. addr = region->value->address;
  502. size = region->value->size;
  503. monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
  504. if (!strcmp(region->value->type, "io")) {
  505. monitor_printf(mon, "I/O at 0x%04" PRIx64
  506. " [0x%04" PRIx64 "].\n",
  507. addr, addr + size - 1);
  508. } else {
  509. monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
  510. " [0x%08" PRIx64 "].\n",
  511. region->value->mem_type_64 ? 64 : 32,
  512. region->value->prefetch ? " prefetchable" : "",
  513. addr, addr + size - 1);
  514. }
  515. }
  516. monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
  517. if (dev->has_pci_bridge) {
  518. if (dev->pci_bridge->has_devices) {
  519. PciDeviceInfoList *cdev;
  520. for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
  521. hmp_info_pci_device(mon, cdev->value);
  522. }
  523. }
  524. }
  525. }
  526. void hmp_info_pci(Monitor *mon, const QDict *qdict)
  527. {
  528. PciInfoList *info_list, *info;
  529. Error *err = NULL;
  530. info_list = qmp_query_pci(&err);
  531. if (err) {
  532. monitor_printf(mon, "PCI devices not supported\n");
  533. error_free(err);
  534. return;
  535. }
  536. for (info = info_list; info; info = info->next) {
  537. PciDeviceInfoList *dev;
  538. for (dev = info->value->devices; dev; dev = dev->next) {
  539. hmp_info_pci_device(mon, dev->value);
  540. }
  541. }
  542. qapi_free_PciInfoList(info_list);
  543. }
  544. void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
  545. {
  546. BlockJobInfoList *list;
  547. Error *err = NULL;
  548. list = qmp_query_block_jobs(&err);
  549. assert(!err);
  550. if (!list) {
  551. monitor_printf(mon, "No active jobs\n");
  552. return;
  553. }
  554. while (list) {
  555. if (strcmp(list->value->type, "stream") == 0) {
  556. monitor_printf(mon, "Streaming device %s: Completed %" PRId64
  557. " of %" PRId64 " bytes, speed limit %" PRId64
  558. " bytes/s\n",
  559. list->value->device,
  560. list->value->offset,
  561. list->value->len,
  562. list->value->speed);
  563. } else {
  564. monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
  565. " of %" PRId64 " bytes, speed limit %" PRId64
  566. " bytes/s\n",
  567. list->value->type,
  568. list->value->device,
  569. list->value->offset,
  570. list->value->len,
  571. list->value->speed);
  572. }
  573. list = list->next;
  574. }
  575. qapi_free_BlockJobInfoList(list);
  576. }
  577. void hmp_info_tpm(Monitor *mon, const QDict *qdict)
  578. {
  579. TPMInfoList *info_list, *info;
  580. Error *err = NULL;
  581. unsigned int c = 0;
  582. TPMPassthroughOptions *tpo;
  583. info_list = qmp_query_tpm(&err);
  584. if (err) {
  585. monitor_printf(mon, "TPM device not supported\n");
  586. error_free(err);
  587. return;
  588. }
  589. if (info_list) {
  590. monitor_printf(mon, "TPM device:\n");
  591. }
  592. for (info = info_list; info; info = info->next) {
  593. TPMInfo *ti = info->value;
  594. monitor_printf(mon, " tpm%d: model=%s\n",
  595. c, TpmModel_lookup[ti->model]);
  596. monitor_printf(mon, " \\ %s: type=%s",
  597. ti->id, TpmTypeOptionsKind_lookup[ti->options->kind]);
  598. switch (ti->options->kind) {
  599. case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
  600. tpo = ti->options->passthrough;
  601. monitor_printf(mon, "%s%s%s%s",
  602. tpo->has_path ? ",path=" : "",
  603. tpo->has_path ? tpo->path : "",
  604. tpo->has_cancel_path ? ",cancel-path=" : "",
  605. tpo->has_cancel_path ? tpo->cancel_path : "");
  606. break;
  607. case TPM_TYPE_OPTIONS_KIND_MAX:
  608. break;
  609. }
  610. monitor_printf(mon, "\n");
  611. c++;
  612. }
  613. qapi_free_TPMInfoList(info_list);
  614. }
  615. void hmp_quit(Monitor *mon, const QDict *qdict)
  616. {
  617. monitor_suspend(mon);
  618. qmp_quit(NULL);
  619. }
  620. void hmp_stop(Monitor *mon, const QDict *qdict)
  621. {
  622. qmp_stop(NULL);
  623. }
  624. void hmp_system_reset(Monitor *mon, const QDict *qdict)
  625. {
  626. qmp_system_reset(NULL);
  627. }
  628. void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
  629. {
  630. qmp_system_powerdown(NULL);
  631. }
  632. void hmp_cpu(Monitor *mon, const QDict *qdict)
  633. {
  634. int64_t cpu_index;
  635. /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
  636. use it are converted to the QAPI */
  637. cpu_index = qdict_get_int(qdict, "index");
  638. if (monitor_set_cpu(cpu_index) < 0) {
  639. monitor_printf(mon, "invalid CPU index\n");
  640. }
  641. }
  642. void hmp_memsave(Monitor *mon, const QDict *qdict)
  643. {
  644. uint32_t size = qdict_get_int(qdict, "size");
  645. const char *filename = qdict_get_str(qdict, "filename");
  646. uint64_t addr = qdict_get_int(qdict, "val");
  647. Error *err = NULL;
  648. qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &err);
  649. hmp_handle_error(mon, &err);
  650. }
  651. void hmp_pmemsave(Monitor *mon, const QDict *qdict)
  652. {
  653. uint32_t size = qdict_get_int(qdict, "size");
  654. const char *filename = qdict_get_str(qdict, "filename");
  655. uint64_t addr = qdict_get_int(qdict, "val");
  656. Error *err = NULL;
  657. qmp_pmemsave(addr, size, filename, &err);
  658. hmp_handle_error(mon, &err);
  659. }
  660. void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
  661. {
  662. const char *chardev = qdict_get_str(qdict, "device");
  663. const char *data = qdict_get_str(qdict, "data");
  664. Error *err = NULL;
  665. qmp_ringbuf_write(chardev, data, false, 0, &err);
  666. hmp_handle_error(mon, &err);
  667. }
  668. void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
  669. {
  670. uint32_t size = qdict_get_int(qdict, "size");
  671. const char *chardev = qdict_get_str(qdict, "device");
  672. char *data;
  673. Error *err = NULL;
  674. int i;
  675. data = qmp_ringbuf_read(chardev, size, false, 0, &err);
  676. if (err) {
  677. monitor_printf(mon, "%s\n", error_get_pretty(err));
  678. error_free(err);
  679. return;
  680. }
  681. for (i = 0; data[i]; i++) {
  682. unsigned char ch = data[i];
  683. if (ch == '\\') {
  684. monitor_printf(mon, "\\\\");
  685. } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
  686. monitor_printf(mon, "\\u%04X", ch);
  687. } else {
  688. monitor_printf(mon, "%c", ch);
  689. }
  690. }
  691. monitor_printf(mon, "\n");
  692. g_free(data);
  693. }
  694. static void hmp_cont_cb(void *opaque, int err)
  695. {
  696. if (!err) {
  697. qmp_cont(NULL);
  698. }
  699. }
  700. static bool key_is_missing(const BlockInfo *bdev)
  701. {
  702. return (bdev->inserted && bdev->inserted->encryption_key_missing);
  703. }
  704. void hmp_cont(Monitor *mon, const QDict *qdict)
  705. {
  706. BlockInfoList *bdev_list, *bdev;
  707. Error *err = NULL;
  708. bdev_list = qmp_query_block(NULL);
  709. for (bdev = bdev_list; bdev; bdev = bdev->next) {
  710. if (key_is_missing(bdev->value)) {
  711. monitor_read_block_device_key(mon, bdev->value->device,
  712. hmp_cont_cb, NULL);
  713. goto out;
  714. }
  715. }
  716. qmp_cont(&err);
  717. hmp_handle_error(mon, &err);
  718. out:
  719. qapi_free_BlockInfoList(bdev_list);
  720. }
  721. void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
  722. {
  723. qmp_system_wakeup(NULL);
  724. }
  725. void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
  726. {
  727. Error *err = NULL;
  728. qmp_inject_nmi(&err);
  729. hmp_handle_error(mon, &err);
  730. }
  731. void hmp_set_link(Monitor *mon, const QDict *qdict)
  732. {
  733. const char *name = qdict_get_str(qdict, "name");
  734. int up = qdict_get_bool(qdict, "up");
  735. Error *err = NULL;
  736. qmp_set_link(name, up, &err);
  737. hmp_handle_error(mon, &err);
  738. }
  739. void hmp_block_passwd(Monitor *mon, const QDict *qdict)
  740. {
  741. const char *device = qdict_get_str(qdict, "device");
  742. const char *password = qdict_get_str(qdict, "password");
  743. Error *err = NULL;
  744. qmp_block_passwd(true, device, false, NULL, password, &err);
  745. hmp_handle_error(mon, &err);
  746. }
  747. void hmp_balloon(Monitor *mon, const QDict *qdict)
  748. {
  749. int64_t value = qdict_get_int(qdict, "value");
  750. Error *err = NULL;
  751. qmp_balloon(value, &err);
  752. if (err) {
  753. monitor_printf(mon, "balloon: %s\n", error_get_pretty(err));
  754. error_free(err);
  755. }
  756. }
  757. void hmp_block_resize(Monitor *mon, const QDict *qdict)
  758. {
  759. const char *device = qdict_get_str(qdict, "device");
  760. int64_t size = qdict_get_int(qdict, "size");
  761. Error *err = NULL;
  762. qmp_block_resize(true, device, false, NULL, size, &err);
  763. hmp_handle_error(mon, &err);
  764. }
  765. void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
  766. {
  767. const char *device = qdict_get_str(qdict, "device");
  768. const char *filename = qdict_get_str(qdict, "target");
  769. const char *format = qdict_get_try_str(qdict, "format");
  770. int reuse = qdict_get_try_bool(qdict, "reuse", 0);
  771. int full = qdict_get_try_bool(qdict, "full", 0);
  772. enum NewImageMode mode;
  773. Error *err = NULL;
  774. if (!filename) {
  775. error_set(&err, QERR_MISSING_PARAMETER, "target");
  776. hmp_handle_error(mon, &err);
  777. return;
  778. }
  779. if (reuse) {
  780. mode = NEW_IMAGE_MODE_EXISTING;
  781. } else {
  782. mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
  783. }
  784. qmp_drive_mirror(device, filename, !!format, format,
  785. false, NULL, false, NULL,
  786. full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
  787. true, mode, false, 0, false, 0, false, 0,
  788. false, 0, false, 0, &err);
  789. hmp_handle_error(mon, &err);
  790. }
  791. void hmp_drive_backup(Monitor *mon, const QDict *qdict)
  792. {
  793. const char *device = qdict_get_str(qdict, "device");
  794. const char *filename = qdict_get_str(qdict, "target");
  795. const char *format = qdict_get_try_str(qdict, "format");
  796. int reuse = qdict_get_try_bool(qdict, "reuse", 0);
  797. int full = qdict_get_try_bool(qdict, "full", 0);
  798. enum NewImageMode mode;
  799. Error *err = NULL;
  800. if (!filename) {
  801. error_set(&err, QERR_MISSING_PARAMETER, "target");
  802. hmp_handle_error(mon, &err);
  803. return;
  804. }
  805. if (reuse) {
  806. mode = NEW_IMAGE_MODE_EXISTING;
  807. } else {
  808. mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
  809. }
  810. qmp_drive_backup(device, filename, !!format, format,
  811. full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
  812. true, mode, false, 0, false, 0, false, 0, &err);
  813. hmp_handle_error(mon, &err);
  814. }
  815. void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
  816. {
  817. const char *device = qdict_get_str(qdict, "device");
  818. const char *filename = qdict_get_try_str(qdict, "snapshot-file");
  819. const char *format = qdict_get_try_str(qdict, "format");
  820. int reuse = qdict_get_try_bool(qdict, "reuse", 0);
  821. enum NewImageMode mode;
  822. Error *err = NULL;
  823. if (!filename) {
  824. /* In the future, if 'snapshot-file' is not specified, the snapshot
  825. will be taken internally. Today it's actually required. */
  826. error_set(&err, QERR_MISSING_PARAMETER, "snapshot-file");
  827. hmp_handle_error(mon, &err);
  828. return;
  829. }
  830. mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
  831. qmp_blockdev_snapshot_sync(true, device, false, NULL,
  832. filename, false, NULL,
  833. !!format, format,
  834. true, mode, &err);
  835. hmp_handle_error(mon, &err);
  836. }
  837. void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict)
  838. {
  839. const char *device = qdict_get_str(qdict, "device");
  840. const char *name = qdict_get_str(qdict, "name");
  841. Error *err = NULL;
  842. qmp_blockdev_snapshot_internal_sync(device, name, &err);
  843. hmp_handle_error(mon, &err);
  844. }
  845. void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict)
  846. {
  847. const char *device = qdict_get_str(qdict, "device");
  848. const char *name = qdict_get_str(qdict, "name");
  849. const char *id = qdict_get_try_str(qdict, "id");
  850. Error *err = NULL;
  851. qmp_blockdev_snapshot_delete_internal_sync(device, !!id, id,
  852. true, name, &err);
  853. hmp_handle_error(mon, &err);
  854. }
  855. void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
  856. {
  857. qmp_migrate_cancel(NULL);
  858. }
  859. void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
  860. {
  861. double value = qdict_get_double(qdict, "value");
  862. qmp_migrate_set_downtime(value, NULL);
  863. }
  864. void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
  865. {
  866. int64_t value = qdict_get_int(qdict, "value");
  867. Error *err = NULL;
  868. qmp_migrate_set_cache_size(value, &err);
  869. if (err) {
  870. monitor_printf(mon, "%s\n", error_get_pretty(err));
  871. error_free(err);
  872. return;
  873. }
  874. }
  875. void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
  876. {
  877. int64_t value = qdict_get_int(qdict, "value");
  878. qmp_migrate_set_speed(value, NULL);
  879. }
  880. void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
  881. {
  882. const char *cap = qdict_get_str(qdict, "capability");
  883. bool state = qdict_get_bool(qdict, "state");
  884. Error *err = NULL;
  885. MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
  886. int i;
  887. for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
  888. if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
  889. caps->value = g_malloc0(sizeof(*caps->value));
  890. caps->value->capability = i;
  891. caps->value->state = state;
  892. caps->next = NULL;
  893. qmp_migrate_set_capabilities(caps, &err);
  894. break;
  895. }
  896. }
  897. if (i == MIGRATION_CAPABILITY_MAX) {
  898. error_set(&err, QERR_INVALID_PARAMETER, cap);
  899. }
  900. qapi_free_MigrationCapabilityStatusList(caps);
  901. if (err) {
  902. monitor_printf(mon, "migrate_set_capability: %s\n",
  903. error_get_pretty(err));
  904. error_free(err);
  905. }
  906. }
  907. void hmp_set_password(Monitor *mon, const QDict *qdict)
  908. {
  909. const char *protocol = qdict_get_str(qdict, "protocol");
  910. const char *password = qdict_get_str(qdict, "password");
  911. const char *connected = qdict_get_try_str(qdict, "connected");
  912. Error *err = NULL;
  913. qmp_set_password(protocol, password, !!connected, connected, &err);
  914. hmp_handle_error(mon, &err);
  915. }
  916. void hmp_expire_password(Monitor *mon, const QDict *qdict)
  917. {
  918. const char *protocol = qdict_get_str(qdict, "protocol");
  919. const char *whenstr = qdict_get_str(qdict, "time");
  920. Error *err = NULL;
  921. qmp_expire_password(protocol, whenstr, &err);
  922. hmp_handle_error(mon, &err);
  923. }
  924. void hmp_eject(Monitor *mon, const QDict *qdict)
  925. {
  926. int force = qdict_get_try_bool(qdict, "force", 0);
  927. const char *device = qdict_get_str(qdict, "device");
  928. Error *err = NULL;
  929. qmp_eject(device, true, force, &err);
  930. hmp_handle_error(mon, &err);
  931. }
  932. static void hmp_change_read_arg(void *opaque, const char *password,
  933. void *readline_opaque)
  934. {
  935. qmp_change_vnc_password(password, NULL);
  936. monitor_read_command(opaque, 1);
  937. }
  938. void hmp_change(Monitor *mon, const QDict *qdict)
  939. {
  940. const char *device = qdict_get_str(qdict, "device");
  941. const char *target = qdict_get_str(qdict, "target");
  942. const char *arg = qdict_get_try_str(qdict, "arg");
  943. Error *err = NULL;
  944. if (strcmp(device, "vnc") == 0 &&
  945. (strcmp(target, "passwd") == 0 ||
  946. strcmp(target, "password") == 0)) {
  947. if (!arg) {
  948. monitor_read_password(mon, hmp_change_read_arg, NULL);
  949. return;
  950. }
  951. }
  952. qmp_change(device, target, !!arg, arg, &err);
  953. if (err &&
  954. error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
  955. error_free(err);
  956. monitor_read_block_device_key(mon, device, NULL, NULL);
  957. return;
  958. }
  959. hmp_handle_error(mon, &err);
  960. }
  961. void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
  962. {
  963. Error *err = NULL;
  964. qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
  965. qdict_get_int(qdict, "bps"),
  966. qdict_get_int(qdict, "bps_rd"),
  967. qdict_get_int(qdict, "bps_wr"),
  968. qdict_get_int(qdict, "iops"),
  969. qdict_get_int(qdict, "iops_rd"),
  970. qdict_get_int(qdict, "iops_wr"),
  971. false, /* no burst max via HMP */
  972. 0,
  973. false,
  974. 0,
  975. false,
  976. 0,
  977. false,
  978. 0,
  979. false,
  980. 0,
  981. false,
  982. 0,
  983. false, /* No default I/O size */
  984. 0, &err);
  985. hmp_handle_error(mon, &err);
  986. }
  987. void hmp_block_stream(Monitor *mon, const QDict *qdict)
  988. {
  989. Error *error = NULL;
  990. const char *device = qdict_get_str(qdict, "device");
  991. const char *base = qdict_get_try_str(qdict, "base");
  992. int64_t speed = qdict_get_try_int(qdict, "speed", 0);
  993. qmp_block_stream(device, base != NULL, base, false, NULL,
  994. qdict_haskey(qdict, "speed"), speed,
  995. true, BLOCKDEV_ON_ERROR_REPORT, &error);
  996. hmp_handle_error(mon, &error);
  997. }
  998. void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
  999. {
  1000. Error *error = NULL;
  1001. const char *device = qdict_get_str(qdict, "device");
  1002. int64_t value = qdict_get_int(qdict, "speed");
  1003. qmp_block_job_set_speed(device, value, &error);
  1004. hmp_handle_error(mon, &error);
  1005. }
  1006. void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
  1007. {
  1008. Error *error = NULL;
  1009. const char *device = qdict_get_str(qdict, "device");
  1010. bool force = qdict_get_try_bool(qdict, "force", 0);
  1011. qmp_block_job_cancel(device, true, force, &error);
  1012. hmp_handle_error(mon, &error);
  1013. }
  1014. void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
  1015. {
  1016. Error *error = NULL;
  1017. const char *device = qdict_get_str(qdict, "device");
  1018. qmp_block_job_pause(device, &error);
  1019. hmp_handle_error(mon, &error);
  1020. }
  1021. void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
  1022. {
  1023. Error *error = NULL;
  1024. const char *device = qdict_get_str(qdict, "device");
  1025. qmp_block_job_resume(device, &error);
  1026. hmp_handle_error(mon, &error);
  1027. }
  1028. void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
  1029. {
  1030. Error *error = NULL;
  1031. const char *device = qdict_get_str(qdict, "device");
  1032. qmp_block_job_complete(device, &error);
  1033. hmp_handle_error(mon, &error);
  1034. }
  1035. typedef struct MigrationStatus
  1036. {
  1037. QEMUTimer *timer;
  1038. Monitor *mon;
  1039. bool is_block_migration;
  1040. } MigrationStatus;
  1041. static void hmp_migrate_status_cb(void *opaque)
  1042. {
  1043. MigrationStatus *status = opaque;
  1044. MigrationInfo *info;
  1045. info = qmp_query_migrate(NULL);
  1046. if (!info->has_status || strcmp(info->status, "active") == 0 ||
  1047. strcmp(info->status, "setup") == 0) {
  1048. if (info->has_disk) {
  1049. int progress;
  1050. if (info->disk->remaining) {
  1051. progress = info->disk->transferred * 100 / info->disk->total;
  1052. } else {
  1053. progress = 100;
  1054. }
  1055. monitor_printf(status->mon, "Completed %d %%\r", progress);
  1056. monitor_flush(status->mon);
  1057. }
  1058. timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
  1059. } else {
  1060. if (status->is_block_migration) {
  1061. monitor_printf(status->mon, "\n");
  1062. }
  1063. monitor_resume(status->mon);
  1064. timer_del(status->timer);
  1065. g_free(status);
  1066. }
  1067. qapi_free_MigrationInfo(info);
  1068. }
  1069. void hmp_migrate(Monitor *mon, const QDict *qdict)
  1070. {
  1071. int detach = qdict_get_try_bool(qdict, "detach", 0);
  1072. int blk = qdict_get_try_bool(qdict, "blk", 0);
  1073. int inc = qdict_get_try_bool(qdict, "inc", 0);
  1074. const char *uri = qdict_get_str(qdict, "uri");
  1075. Error *err = NULL;
  1076. qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
  1077. if (err) {
  1078. monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
  1079. error_free(err);
  1080. return;
  1081. }
  1082. if (!detach) {
  1083. MigrationStatus *status;
  1084. if (monitor_suspend(mon) < 0) {
  1085. monitor_printf(mon, "terminal does not allow synchronous "
  1086. "migration, continuing detached\n");
  1087. return;
  1088. }
  1089. status = g_malloc0(sizeof(*status));
  1090. status->mon = mon;
  1091. status->is_block_migration = blk || inc;
  1092. status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
  1093. status);
  1094. timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
  1095. }
  1096. }
  1097. void hmp_device_del(Monitor *mon, const QDict *qdict)
  1098. {
  1099. const char *id = qdict_get_str(qdict, "id");
  1100. Error *err = NULL;
  1101. qmp_device_del(id, &err);
  1102. hmp_handle_error(mon, &err);
  1103. }
  1104. void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
  1105. {
  1106. Error *err = NULL;
  1107. int paging = qdict_get_try_bool(qdict, "paging", 0);
  1108. int zlib = qdict_get_try_bool(qdict, "zlib", 0);
  1109. int lzo = qdict_get_try_bool(qdict, "lzo", 0);
  1110. int snappy = qdict_get_try_bool(qdict, "snappy", 0);
  1111. const char *file = qdict_get_str(qdict, "filename");
  1112. bool has_begin = qdict_haskey(qdict, "begin");
  1113. bool has_length = qdict_haskey(qdict, "length");
  1114. int64_t begin = 0;
  1115. int64_t length = 0;
  1116. enum DumpGuestMemoryFormat dump_format = DUMP_GUEST_MEMORY_FORMAT_ELF;
  1117. char *prot;
  1118. if (zlib + lzo + snappy > 1) {
  1119. error_setg(&err, "only one of '-z|-l|-s' can be set");
  1120. hmp_handle_error(mon, &err);
  1121. return;
  1122. }
  1123. if (zlib) {
  1124. dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
  1125. }
  1126. if (lzo) {
  1127. dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
  1128. }
  1129. if (snappy) {
  1130. dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
  1131. }
  1132. if (has_begin) {
  1133. begin = qdict_get_int(qdict, "begin");
  1134. }
  1135. if (has_length) {
  1136. length = qdict_get_int(qdict, "length");
  1137. }
  1138. prot = g_strconcat("file:", file, NULL);
  1139. qmp_dump_guest_memory(paging, prot, has_begin, begin, has_length, length,
  1140. true, dump_format, &err);
  1141. hmp_handle_error(mon, &err);
  1142. g_free(prot);
  1143. }
  1144. void hmp_netdev_add(Monitor *mon, const QDict *qdict)
  1145. {
  1146. Error *err = NULL;
  1147. QemuOpts *opts;
  1148. opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
  1149. if (err) {
  1150. goto out;
  1151. }
  1152. netdev_add(opts, &err);
  1153. if (err) {
  1154. qemu_opts_del(opts);
  1155. }
  1156. out:
  1157. hmp_handle_error(mon, &err);
  1158. }
  1159. void hmp_netdev_del(Monitor *mon, const QDict *qdict)
  1160. {
  1161. const char *id = qdict_get_str(qdict, "id");
  1162. Error *err = NULL;
  1163. qmp_netdev_del(id, &err);
  1164. hmp_handle_error(mon, &err);
  1165. }
  1166. void hmp_object_add(Monitor *mon, const QDict *qdict)
  1167. {
  1168. Error *err = NULL;
  1169. Error *err_end = NULL;
  1170. QemuOpts *opts;
  1171. char *type = NULL;
  1172. char *id = NULL;
  1173. void *dummy = NULL;
  1174. OptsVisitor *ov;
  1175. QDict *pdict;
  1176. opts = qemu_opts_from_qdict(qemu_find_opts("object"), qdict, &err);
  1177. if (err) {
  1178. goto out;
  1179. }
  1180. ov = opts_visitor_new(opts);
  1181. pdict = qdict_clone_shallow(qdict);
  1182. visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
  1183. if (err) {
  1184. goto out_clean;
  1185. }
  1186. qdict_del(pdict, "qom-type");
  1187. visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
  1188. if (err) {
  1189. goto out_end;
  1190. }
  1191. qdict_del(pdict, "id");
  1192. visit_type_str(opts_get_visitor(ov), &id, "id", &err);
  1193. if (err) {
  1194. goto out_end;
  1195. }
  1196. object_add(type, id, pdict, opts_get_visitor(ov), &err);
  1197. out_end:
  1198. visit_end_struct(opts_get_visitor(ov), &err_end);
  1199. if (!err && err_end) {
  1200. qmp_object_del(id, NULL);
  1201. }
  1202. error_propagate(&err, err_end);
  1203. out_clean:
  1204. opts_visitor_cleanup(ov);
  1205. QDECREF(pdict);
  1206. qemu_opts_del(opts);
  1207. g_free(id);
  1208. g_free(type);
  1209. g_free(dummy);
  1210. out:
  1211. hmp_handle_error(mon, &err);
  1212. }
  1213. void hmp_getfd(Monitor *mon, const QDict *qdict)
  1214. {
  1215. const char *fdname = qdict_get_str(qdict, "fdname");
  1216. Error *err = NULL;
  1217. qmp_getfd(fdname, &err);
  1218. hmp_handle_error(mon, &err);
  1219. }
  1220. void hmp_closefd(Monitor *mon, const QDict *qdict)
  1221. {
  1222. const char *fdname = qdict_get_str(qdict, "fdname");
  1223. Error *err = NULL;
  1224. qmp_closefd(fdname, &err);
  1225. hmp_handle_error(mon, &err);
  1226. }
  1227. void hmp_send_key(Monitor *mon, const QDict *qdict)
  1228. {
  1229. const char *keys = qdict_get_str(qdict, "keys");
  1230. KeyValueList *keylist, *head = NULL, *tmp = NULL;
  1231. int has_hold_time = qdict_haskey(qdict, "hold-time");
  1232. int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
  1233. Error *err = NULL;
  1234. char keyname_buf[16];
  1235. char *separator;
  1236. int keyname_len;
  1237. while (1) {
  1238. separator = strchr(keys, '-');
  1239. keyname_len = separator ? separator - keys : strlen(keys);
  1240. pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
  1241. /* Be compatible with old interface, convert user inputted "<" */
  1242. if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
  1243. pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
  1244. keyname_len = 4;
  1245. }
  1246. keyname_buf[keyname_len] = 0;
  1247. keylist = g_malloc0(sizeof(*keylist));
  1248. keylist->value = g_malloc0(sizeof(*keylist->value));
  1249. if (!head) {
  1250. head = keylist;
  1251. }
  1252. if (tmp) {
  1253. tmp->next = keylist;
  1254. }
  1255. tmp = keylist;
  1256. if (strstart(keyname_buf, "0x", NULL)) {
  1257. char *endp;
  1258. int value = strtoul(keyname_buf, &endp, 0);
  1259. if (*endp != '\0') {
  1260. goto err_out;
  1261. }
  1262. keylist->value->kind = KEY_VALUE_KIND_NUMBER;
  1263. keylist->value->number = value;
  1264. } else {
  1265. int idx = index_from_key(keyname_buf);
  1266. if (idx == Q_KEY_CODE_MAX) {
  1267. goto err_out;
  1268. }
  1269. keylist->value->kind = KEY_VALUE_KIND_QCODE;
  1270. keylist->value->qcode = idx;
  1271. }
  1272. if (!separator) {
  1273. break;
  1274. }
  1275. keys = separator + 1;
  1276. }
  1277. qmp_send_key(head, has_hold_time, hold_time, &err);
  1278. hmp_handle_error(mon, &err);
  1279. out:
  1280. qapi_free_KeyValueList(head);
  1281. return;
  1282. err_out:
  1283. monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
  1284. goto out;
  1285. }
  1286. void hmp_screen_dump(Monitor *mon, const QDict *qdict)
  1287. {
  1288. const char *filename = qdict_get_str(qdict, "filename");
  1289. Error *err = NULL;
  1290. qmp_screendump(filename, &err);
  1291. hmp_handle_error(mon, &err);
  1292. }
  1293. void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
  1294. {
  1295. const char *uri = qdict_get_str(qdict, "uri");
  1296. int writable = qdict_get_try_bool(qdict, "writable", 0);
  1297. int all = qdict_get_try_bool(qdict, "all", 0);
  1298. Error *local_err = NULL;
  1299. BlockInfoList *block_list, *info;
  1300. SocketAddress *addr;
  1301. if (writable && !all) {
  1302. error_setg(&local_err, "-w only valid together with -a");
  1303. goto exit;
  1304. }
  1305. /* First check if the address is valid and start the server. */
  1306. addr = socket_parse(uri, &local_err);
  1307. if (local_err != NULL) {
  1308. goto exit;
  1309. }
  1310. qmp_nbd_server_start(addr, &local_err);
  1311. qapi_free_SocketAddress(addr);
  1312. if (local_err != NULL) {
  1313. goto exit;
  1314. }
  1315. if (!all) {
  1316. return;
  1317. }
  1318. /* Then try adding all block devices. If one fails, close all and
  1319. * exit.
  1320. */
  1321. block_list = qmp_query_block(NULL);
  1322. for (info = block_list; info; info = info->next) {
  1323. if (!info->value->has_inserted) {
  1324. continue;
  1325. }
  1326. qmp_nbd_server_add(info->value->device, true, writable, &local_err);
  1327. if (local_err != NULL) {
  1328. qmp_nbd_server_stop(NULL);
  1329. break;
  1330. }
  1331. }
  1332. qapi_free_BlockInfoList(block_list);
  1333. exit:
  1334. hmp_handle_error(mon, &local_err);
  1335. }
  1336. void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
  1337. {
  1338. const char *device = qdict_get_str(qdict, "device");
  1339. int writable = qdict_get_try_bool(qdict, "writable", 0);
  1340. Error *local_err = NULL;
  1341. qmp_nbd_server_add(device, true, writable, &local_err);
  1342. if (local_err != NULL) {
  1343. hmp_handle_error(mon, &local_err);
  1344. }
  1345. }
  1346. void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
  1347. {
  1348. Error *err = NULL;
  1349. qmp_nbd_server_stop(&err);
  1350. hmp_handle_error(mon, &err);
  1351. }
  1352. void hmp_cpu_add(Monitor *mon, const QDict *qdict)
  1353. {
  1354. int cpuid;
  1355. Error *err = NULL;
  1356. cpuid = qdict_get_int(qdict, "id");
  1357. qmp_cpu_add(cpuid, &err);
  1358. hmp_handle_error(mon, &err);
  1359. }
  1360. void hmp_chardev_add(Monitor *mon, const QDict *qdict)
  1361. {
  1362. const char *args = qdict_get_str(qdict, "args");
  1363. Error *err = NULL;
  1364. QemuOpts *opts;
  1365. opts = qemu_opts_parse(qemu_find_opts("chardev"), args, 1);
  1366. if (opts == NULL) {
  1367. error_setg(&err, "Parsing chardev args failed");
  1368. } else {
  1369. qemu_chr_new_from_opts(opts, NULL, &err);
  1370. }
  1371. hmp_handle_error(mon, &err);
  1372. }
  1373. void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
  1374. {
  1375. Error *local_err = NULL;
  1376. qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
  1377. hmp_handle_error(mon, &local_err);
  1378. }
  1379. void hmp_qemu_io(Monitor *mon, const QDict *qdict)
  1380. {
  1381. BlockDriverState *bs;
  1382. const char* device = qdict_get_str(qdict, "device");
  1383. const char* command = qdict_get_str(qdict, "command");
  1384. Error *err = NULL;
  1385. bs = bdrv_find(device);
  1386. if (bs) {
  1387. qemuio_command(bs, command);
  1388. } else {
  1389. error_set(&err, QERR_DEVICE_NOT_FOUND, device);
  1390. }
  1391. hmp_handle_error(mon, &err);
  1392. }
  1393. void hmp_object_del(Monitor *mon, const QDict *qdict)
  1394. {
  1395. const char *id = qdict_get_str(qdict, "id");
  1396. Error *err = NULL;
  1397. qmp_object_del(id, &err);
  1398. hmp_handle_error(mon, &err);
  1399. }
  1400. void hmp_info_memdev(Monitor *mon, const QDict *qdict)
  1401. {
  1402. Error *err = NULL;
  1403. MemdevList *memdev_list = qmp_query_memdev(&err);
  1404. MemdevList *m = memdev_list;
  1405. StringOutputVisitor *ov;
  1406. char *str;
  1407. int i = 0;
  1408. while (m) {
  1409. ov = string_output_visitor_new(false);
  1410. visit_type_uint16List(string_output_get_visitor(ov),
  1411. &m->value->host_nodes, NULL, NULL);
  1412. monitor_printf(mon, "memory backend: %d\n", i);
  1413. monitor_printf(mon, " size: %" PRId64 "\n", m->value->size);
  1414. monitor_printf(mon, " merge: %s\n",
  1415. m->value->merge ? "true" : "false");
  1416. monitor_printf(mon, " dump: %s\n",
  1417. m->value->dump ? "true" : "false");
  1418. monitor_printf(mon, " prealloc: %s\n",
  1419. m->value->prealloc ? "true" : "false");
  1420. monitor_printf(mon, " policy: %s\n",
  1421. HostMemPolicy_lookup[m->value->policy]);
  1422. str = string_output_get_string(ov);
  1423. monitor_printf(mon, " host nodes: %s\n", str);
  1424. g_free(str);
  1425. string_output_visitor_cleanup(ov);
  1426. m = m->next;
  1427. i++;
  1428. }
  1429. monitor_printf(mon, "\n");
  1430. qapi_free_MemdevList(memdev_list);
  1431. }
  1432. void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
  1433. {
  1434. Error *err = NULL;
  1435. MemoryDeviceInfoList *info_list = qmp_query_memory_devices(&err);
  1436. MemoryDeviceInfoList *info;
  1437. MemoryDeviceInfo *value;
  1438. PCDIMMDeviceInfo *di;
  1439. for (info = info_list; info; info = info->next) {
  1440. value = info->value;
  1441. if (value) {
  1442. switch (value->kind) {
  1443. case MEMORY_DEVICE_INFO_KIND_DIMM:
  1444. di = value->dimm;
  1445. monitor_printf(mon, "Memory device [%s]: \"%s\"\n",
  1446. MemoryDeviceInfoKind_lookup[value->kind],
  1447. di->id ? di->id : "");
  1448. monitor_printf(mon, " addr: 0x%" PRIx64 "\n", di->addr);
  1449. monitor_printf(mon, " slot: %" PRId64 "\n", di->slot);
  1450. monitor_printf(mon, " node: %" PRId64 "\n", di->node);
  1451. monitor_printf(mon, " size: %" PRIu64 "\n", di->size);
  1452. monitor_printf(mon, " memdev: %s\n", di->memdev);
  1453. monitor_printf(mon, " hotplugged: %s\n",
  1454. di->hotplugged ? "true" : "false");
  1455. monitor_printf(mon, " hotpluggable: %s\n",
  1456. di->hotpluggable ? "true" : "false");
  1457. break;
  1458. default:
  1459. break;
  1460. }
  1461. }
  1462. }
  1463. qapi_free_MemoryDeviceInfoList(info_list);
  1464. }