2
0

hmp.c 48 KB

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