hmp.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716
  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. }
  576. void hmp_info_tpm(Monitor *mon, const QDict *qdict)
  577. {
  578. TPMInfoList *info_list, *info;
  579. Error *err = NULL;
  580. unsigned int c = 0;
  581. TPMPassthroughOptions *tpo;
  582. info_list = qmp_query_tpm(&err);
  583. if (err) {
  584. monitor_printf(mon, "TPM device not supported\n");
  585. error_free(err);
  586. return;
  587. }
  588. if (info_list) {
  589. monitor_printf(mon, "TPM device:\n");
  590. }
  591. for (info = info_list; info; info = info->next) {
  592. TPMInfo *ti = info->value;
  593. monitor_printf(mon, " tpm%d: model=%s\n",
  594. c, TpmModel_lookup[ti->model]);
  595. monitor_printf(mon, " \\ %s: type=%s",
  596. ti->id, TpmTypeOptionsKind_lookup[ti->options->kind]);
  597. switch (ti->options->kind) {
  598. case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
  599. tpo = ti->options->passthrough;
  600. monitor_printf(mon, "%s%s%s%s",
  601. tpo->has_path ? ",path=" : "",
  602. tpo->has_path ? tpo->path : "",
  603. tpo->has_cancel_path ? ",cancel-path=" : "",
  604. tpo->has_cancel_path ? tpo->cancel_path : "");
  605. break;
  606. case TPM_TYPE_OPTIONS_KIND_MAX:
  607. break;
  608. }
  609. monitor_printf(mon, "\n");
  610. c++;
  611. }
  612. qapi_free_TPMInfoList(info_list);
  613. }
  614. void hmp_quit(Monitor *mon, const QDict *qdict)
  615. {
  616. monitor_suspend(mon);
  617. qmp_quit(NULL);
  618. }
  619. void hmp_stop(Monitor *mon, const QDict *qdict)
  620. {
  621. qmp_stop(NULL);
  622. }
  623. void hmp_system_reset(Monitor *mon, const QDict *qdict)
  624. {
  625. qmp_system_reset(NULL);
  626. }
  627. void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
  628. {
  629. qmp_system_powerdown(NULL);
  630. }
  631. void hmp_cpu(Monitor *mon, const QDict *qdict)
  632. {
  633. int64_t cpu_index;
  634. /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
  635. use it are converted to the QAPI */
  636. cpu_index = qdict_get_int(qdict, "index");
  637. if (monitor_set_cpu(cpu_index) < 0) {
  638. monitor_printf(mon, "invalid CPU index\n");
  639. }
  640. }
  641. void hmp_memsave(Monitor *mon, const QDict *qdict)
  642. {
  643. uint32_t size = qdict_get_int(qdict, "size");
  644. const char *filename = qdict_get_str(qdict, "filename");
  645. uint64_t addr = qdict_get_int(qdict, "val");
  646. Error *err = NULL;
  647. qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &err);
  648. hmp_handle_error(mon, &err);
  649. }
  650. void hmp_pmemsave(Monitor *mon, const QDict *qdict)
  651. {
  652. uint32_t size = qdict_get_int(qdict, "size");
  653. const char *filename = qdict_get_str(qdict, "filename");
  654. uint64_t addr = qdict_get_int(qdict, "val");
  655. Error *err = NULL;
  656. qmp_pmemsave(addr, size, filename, &err);
  657. hmp_handle_error(mon, &err);
  658. }
  659. void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
  660. {
  661. const char *chardev = qdict_get_str(qdict, "device");
  662. const char *data = qdict_get_str(qdict, "data");
  663. Error *err = NULL;
  664. qmp_ringbuf_write(chardev, data, false, 0, &err);
  665. hmp_handle_error(mon, &err);
  666. }
  667. void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
  668. {
  669. uint32_t size = qdict_get_int(qdict, "size");
  670. const char *chardev = qdict_get_str(qdict, "device");
  671. char *data;
  672. Error *err = NULL;
  673. int i;
  674. data = qmp_ringbuf_read(chardev, size, false, 0, &err);
  675. if (err) {
  676. monitor_printf(mon, "%s\n", error_get_pretty(err));
  677. error_free(err);
  678. return;
  679. }
  680. for (i = 0; data[i]; i++) {
  681. unsigned char ch = data[i];
  682. if (ch == '\\') {
  683. monitor_printf(mon, "\\\\");
  684. } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
  685. monitor_printf(mon, "\\u%04X", ch);
  686. } else {
  687. monitor_printf(mon, "%c", ch);
  688. }
  689. }
  690. monitor_printf(mon, "\n");
  691. g_free(data);
  692. }
  693. static void hmp_cont_cb(void *opaque, int err)
  694. {
  695. if (!err) {
  696. qmp_cont(NULL);
  697. }
  698. }
  699. static bool key_is_missing(const BlockInfo *bdev)
  700. {
  701. return (bdev->inserted && bdev->inserted->encryption_key_missing);
  702. }
  703. void hmp_cont(Monitor *mon, const QDict *qdict)
  704. {
  705. BlockInfoList *bdev_list, *bdev;
  706. Error *err = NULL;
  707. bdev_list = qmp_query_block(NULL);
  708. for (bdev = bdev_list; bdev; bdev = bdev->next) {
  709. if (key_is_missing(bdev->value)) {
  710. monitor_read_block_device_key(mon, bdev->value->device,
  711. hmp_cont_cb, NULL);
  712. goto out;
  713. }
  714. }
  715. qmp_cont(&err);
  716. hmp_handle_error(mon, &err);
  717. out:
  718. qapi_free_BlockInfoList(bdev_list);
  719. }
  720. void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
  721. {
  722. qmp_system_wakeup(NULL);
  723. }
  724. void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
  725. {
  726. Error *err = NULL;
  727. qmp_inject_nmi(&err);
  728. hmp_handle_error(mon, &err);
  729. }
  730. void hmp_set_link(Monitor *mon, const QDict *qdict)
  731. {
  732. const char *name = qdict_get_str(qdict, "name");
  733. int up = qdict_get_bool(qdict, "up");
  734. Error *err = NULL;
  735. qmp_set_link(name, up, &err);
  736. hmp_handle_error(mon, &err);
  737. }
  738. void hmp_block_passwd(Monitor *mon, const QDict *qdict)
  739. {
  740. const char *device = qdict_get_str(qdict, "device");
  741. const char *password = qdict_get_str(qdict, "password");
  742. Error *err = NULL;
  743. qmp_block_passwd(true, device, false, NULL, password, &err);
  744. hmp_handle_error(mon, &err);
  745. }
  746. void hmp_balloon(Monitor *mon, const QDict *qdict)
  747. {
  748. int64_t value = qdict_get_int(qdict, "value");
  749. Error *err = NULL;
  750. qmp_balloon(value, &err);
  751. if (err) {
  752. monitor_printf(mon, "balloon: %s\n", error_get_pretty(err));
  753. error_free(err);
  754. }
  755. }
  756. void hmp_block_resize(Monitor *mon, const QDict *qdict)
  757. {
  758. const char *device = qdict_get_str(qdict, "device");
  759. int64_t size = qdict_get_int(qdict, "size");
  760. Error *err = NULL;
  761. qmp_block_resize(true, device, false, NULL, size, &err);
  762. hmp_handle_error(mon, &err);
  763. }
  764. void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
  765. {
  766. const char *device = qdict_get_str(qdict, "device");
  767. const char *filename = qdict_get_str(qdict, "target");
  768. const char *format = qdict_get_try_str(qdict, "format");
  769. int reuse = qdict_get_try_bool(qdict, "reuse", 0);
  770. int full = qdict_get_try_bool(qdict, "full", 0);
  771. enum NewImageMode mode;
  772. Error *err = NULL;
  773. if (!filename) {
  774. error_set(&err, QERR_MISSING_PARAMETER, "target");
  775. hmp_handle_error(mon, &err);
  776. return;
  777. }
  778. if (reuse) {
  779. mode = NEW_IMAGE_MODE_EXISTING;
  780. } else {
  781. mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
  782. }
  783. qmp_drive_mirror(device, filename, !!format, format,
  784. false, NULL, false, NULL,
  785. full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
  786. true, mode, false, 0, false, 0, false, 0,
  787. false, 0, false, 0, &err);
  788. hmp_handle_error(mon, &err);
  789. }
  790. void hmp_drive_backup(Monitor *mon, const QDict *qdict)
  791. {
  792. const char *device = qdict_get_str(qdict, "device");
  793. const char *filename = qdict_get_str(qdict, "target");
  794. const char *format = qdict_get_try_str(qdict, "format");
  795. int reuse = qdict_get_try_bool(qdict, "reuse", 0);
  796. int full = qdict_get_try_bool(qdict, "full", 0);
  797. enum NewImageMode mode;
  798. Error *err = NULL;
  799. if (!filename) {
  800. error_set(&err, QERR_MISSING_PARAMETER, "target");
  801. hmp_handle_error(mon, &err);
  802. return;
  803. }
  804. if (reuse) {
  805. mode = NEW_IMAGE_MODE_EXISTING;
  806. } else {
  807. mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
  808. }
  809. qmp_drive_backup(device, filename, !!format, format,
  810. full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
  811. true, mode, false, 0, false, 0, false, 0, &err);
  812. hmp_handle_error(mon, &err);
  813. }
  814. void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
  815. {
  816. const char *device = qdict_get_str(qdict, "device");
  817. const char *filename = qdict_get_try_str(qdict, "snapshot-file");
  818. const char *format = qdict_get_try_str(qdict, "format");
  819. int reuse = qdict_get_try_bool(qdict, "reuse", 0);
  820. enum NewImageMode mode;
  821. Error *err = NULL;
  822. if (!filename) {
  823. /* In the future, if 'snapshot-file' is not specified, the snapshot
  824. will be taken internally. Today it's actually required. */
  825. error_set(&err, QERR_MISSING_PARAMETER, "snapshot-file");
  826. hmp_handle_error(mon, &err);
  827. return;
  828. }
  829. mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
  830. qmp_blockdev_snapshot_sync(true, device, false, NULL,
  831. filename, false, NULL,
  832. !!format, format,
  833. true, mode, &err);
  834. hmp_handle_error(mon, &err);
  835. }
  836. void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict)
  837. {
  838. const char *device = qdict_get_str(qdict, "device");
  839. const char *name = qdict_get_str(qdict, "name");
  840. Error *err = NULL;
  841. qmp_blockdev_snapshot_internal_sync(device, name, &err);
  842. hmp_handle_error(mon, &err);
  843. }
  844. void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict)
  845. {
  846. const char *device = qdict_get_str(qdict, "device");
  847. const char *name = qdict_get_str(qdict, "name");
  848. const char *id = qdict_get_try_str(qdict, "id");
  849. Error *err = NULL;
  850. qmp_blockdev_snapshot_delete_internal_sync(device, !!id, id,
  851. true, name, &err);
  852. hmp_handle_error(mon, &err);
  853. }
  854. void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
  855. {
  856. qmp_migrate_cancel(NULL);
  857. }
  858. void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
  859. {
  860. double value = qdict_get_double(qdict, "value");
  861. qmp_migrate_set_downtime(value, NULL);
  862. }
  863. void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
  864. {
  865. int64_t value = qdict_get_int(qdict, "value");
  866. Error *err = NULL;
  867. qmp_migrate_set_cache_size(value, &err);
  868. if (err) {
  869. monitor_printf(mon, "%s\n", error_get_pretty(err));
  870. error_free(err);
  871. return;
  872. }
  873. }
  874. void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
  875. {
  876. int64_t value = qdict_get_int(qdict, "value");
  877. qmp_migrate_set_speed(value, NULL);
  878. }
  879. void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
  880. {
  881. const char *cap = qdict_get_str(qdict, "capability");
  882. bool state = qdict_get_bool(qdict, "state");
  883. Error *err = NULL;
  884. MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
  885. int i;
  886. for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
  887. if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
  888. caps->value = g_malloc0(sizeof(*caps->value));
  889. caps->value->capability = i;
  890. caps->value->state = state;
  891. caps->next = NULL;
  892. qmp_migrate_set_capabilities(caps, &err);
  893. break;
  894. }
  895. }
  896. if (i == MIGRATION_CAPABILITY_MAX) {
  897. error_set(&err, QERR_INVALID_PARAMETER, cap);
  898. }
  899. qapi_free_MigrationCapabilityStatusList(caps);
  900. if (err) {
  901. monitor_printf(mon, "migrate_set_capability: %s\n",
  902. error_get_pretty(err));
  903. error_free(err);
  904. }
  905. }
  906. void hmp_set_password(Monitor *mon, const QDict *qdict)
  907. {
  908. const char *protocol = qdict_get_str(qdict, "protocol");
  909. const char *password = qdict_get_str(qdict, "password");
  910. const char *connected = qdict_get_try_str(qdict, "connected");
  911. Error *err = NULL;
  912. qmp_set_password(protocol, password, !!connected, connected, &err);
  913. hmp_handle_error(mon, &err);
  914. }
  915. void hmp_expire_password(Monitor *mon, const QDict *qdict)
  916. {
  917. const char *protocol = qdict_get_str(qdict, "protocol");
  918. const char *whenstr = qdict_get_str(qdict, "time");
  919. Error *err = NULL;
  920. qmp_expire_password(protocol, whenstr, &err);
  921. hmp_handle_error(mon, &err);
  922. }
  923. void hmp_eject(Monitor *mon, const QDict *qdict)
  924. {
  925. int force = qdict_get_try_bool(qdict, "force", 0);
  926. const char *device = qdict_get_str(qdict, "device");
  927. Error *err = NULL;
  928. qmp_eject(device, true, force, &err);
  929. hmp_handle_error(mon, &err);
  930. }
  931. static void hmp_change_read_arg(void *opaque, const char *password,
  932. void *readline_opaque)
  933. {
  934. qmp_change_vnc_password(password, NULL);
  935. monitor_read_command(opaque, 1);
  936. }
  937. void hmp_change(Monitor *mon, const QDict *qdict)
  938. {
  939. const char *device = qdict_get_str(qdict, "device");
  940. const char *target = qdict_get_str(qdict, "target");
  941. const char *arg = qdict_get_try_str(qdict, "arg");
  942. Error *err = NULL;
  943. if (strcmp(device, "vnc") == 0 &&
  944. (strcmp(target, "passwd") == 0 ||
  945. strcmp(target, "password") == 0)) {
  946. if (!arg) {
  947. monitor_read_password(mon, hmp_change_read_arg, NULL);
  948. return;
  949. }
  950. }
  951. qmp_change(device, target, !!arg, arg, &err);
  952. if (err &&
  953. error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
  954. error_free(err);
  955. monitor_read_block_device_key(mon, device, NULL, NULL);
  956. return;
  957. }
  958. hmp_handle_error(mon, &err);
  959. }
  960. void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
  961. {
  962. Error *err = NULL;
  963. qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
  964. qdict_get_int(qdict, "bps"),
  965. qdict_get_int(qdict, "bps_rd"),
  966. qdict_get_int(qdict, "bps_wr"),
  967. qdict_get_int(qdict, "iops"),
  968. qdict_get_int(qdict, "iops_rd"),
  969. qdict_get_int(qdict, "iops_wr"),
  970. false, /* no burst max via HMP */
  971. 0,
  972. false,
  973. 0,
  974. false,
  975. 0,
  976. false,
  977. 0,
  978. false,
  979. 0,
  980. false,
  981. 0,
  982. false, /* No default I/O size */
  983. 0, &err);
  984. hmp_handle_error(mon, &err);
  985. }
  986. void hmp_block_stream(Monitor *mon, const QDict *qdict)
  987. {
  988. Error *error = NULL;
  989. const char *device = qdict_get_str(qdict, "device");
  990. const char *base = qdict_get_try_str(qdict, "base");
  991. int64_t speed = qdict_get_try_int(qdict, "speed", 0);
  992. qmp_block_stream(device, base != NULL, base, false, NULL,
  993. qdict_haskey(qdict, "speed"), speed,
  994. true, BLOCKDEV_ON_ERROR_REPORT, &error);
  995. hmp_handle_error(mon, &error);
  996. }
  997. void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
  998. {
  999. Error *error = NULL;
  1000. const char *device = qdict_get_str(qdict, "device");
  1001. int64_t value = qdict_get_int(qdict, "speed");
  1002. qmp_block_job_set_speed(device, value, &error);
  1003. hmp_handle_error(mon, &error);
  1004. }
  1005. void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
  1006. {
  1007. Error *error = NULL;
  1008. const char *device = qdict_get_str(qdict, "device");
  1009. bool force = qdict_get_try_bool(qdict, "force", 0);
  1010. qmp_block_job_cancel(device, true, force, &error);
  1011. hmp_handle_error(mon, &error);
  1012. }
  1013. void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
  1014. {
  1015. Error *error = NULL;
  1016. const char *device = qdict_get_str(qdict, "device");
  1017. qmp_block_job_pause(device, &error);
  1018. hmp_handle_error(mon, &error);
  1019. }
  1020. void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
  1021. {
  1022. Error *error = NULL;
  1023. const char *device = qdict_get_str(qdict, "device");
  1024. qmp_block_job_resume(device, &error);
  1025. hmp_handle_error(mon, &error);
  1026. }
  1027. void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
  1028. {
  1029. Error *error = NULL;
  1030. const char *device = qdict_get_str(qdict, "device");
  1031. qmp_block_job_complete(device, &error);
  1032. hmp_handle_error(mon, &error);
  1033. }
  1034. typedef struct MigrationStatus
  1035. {
  1036. QEMUTimer *timer;
  1037. Monitor *mon;
  1038. bool is_block_migration;
  1039. } MigrationStatus;
  1040. static void hmp_migrate_status_cb(void *opaque)
  1041. {
  1042. MigrationStatus *status = opaque;
  1043. MigrationInfo *info;
  1044. info = qmp_query_migrate(NULL);
  1045. if (!info->has_status || strcmp(info->status, "active") == 0 ||
  1046. strcmp(info->status, "setup") == 0) {
  1047. if (info->has_disk) {
  1048. int progress;
  1049. if (info->disk->remaining) {
  1050. progress = info->disk->transferred * 100 / info->disk->total;
  1051. } else {
  1052. progress = 100;
  1053. }
  1054. monitor_printf(status->mon, "Completed %d %%\r", progress);
  1055. monitor_flush(status->mon);
  1056. }
  1057. timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
  1058. } else {
  1059. if (status->is_block_migration) {
  1060. monitor_printf(status->mon, "\n");
  1061. }
  1062. monitor_resume(status->mon);
  1063. timer_del(status->timer);
  1064. g_free(status);
  1065. }
  1066. qapi_free_MigrationInfo(info);
  1067. }
  1068. void hmp_migrate(Monitor *mon, const QDict *qdict)
  1069. {
  1070. int detach = qdict_get_try_bool(qdict, "detach", 0);
  1071. int blk = qdict_get_try_bool(qdict, "blk", 0);
  1072. int inc = qdict_get_try_bool(qdict, "inc", 0);
  1073. const char *uri = qdict_get_str(qdict, "uri");
  1074. Error *err = NULL;
  1075. qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
  1076. if (err) {
  1077. monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
  1078. error_free(err);
  1079. return;
  1080. }
  1081. if (!detach) {
  1082. MigrationStatus *status;
  1083. if (monitor_suspend(mon) < 0) {
  1084. monitor_printf(mon, "terminal does not allow synchronous "
  1085. "migration, continuing detached\n");
  1086. return;
  1087. }
  1088. status = g_malloc0(sizeof(*status));
  1089. status->mon = mon;
  1090. status->is_block_migration = blk || inc;
  1091. status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
  1092. status);
  1093. timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
  1094. }
  1095. }
  1096. void hmp_device_del(Monitor *mon, const QDict *qdict)
  1097. {
  1098. const char *id = qdict_get_str(qdict, "id");
  1099. Error *err = NULL;
  1100. qmp_device_del(id, &err);
  1101. hmp_handle_error(mon, &err);
  1102. }
  1103. void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
  1104. {
  1105. Error *err = NULL;
  1106. int paging = qdict_get_try_bool(qdict, "paging", 0);
  1107. int zlib = qdict_get_try_bool(qdict, "zlib", 0);
  1108. int lzo = qdict_get_try_bool(qdict, "lzo", 0);
  1109. int snappy = qdict_get_try_bool(qdict, "snappy", 0);
  1110. const char *file = qdict_get_str(qdict, "filename");
  1111. bool has_begin = qdict_haskey(qdict, "begin");
  1112. bool has_length = qdict_haskey(qdict, "length");
  1113. int64_t begin = 0;
  1114. int64_t length = 0;
  1115. enum DumpGuestMemoryFormat dump_format = DUMP_GUEST_MEMORY_FORMAT_ELF;
  1116. char *prot;
  1117. if (zlib + lzo + snappy > 1) {
  1118. error_setg(&err, "only one of '-z|-l|-s' can be set");
  1119. hmp_handle_error(mon, &err);
  1120. return;
  1121. }
  1122. if (zlib) {
  1123. dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
  1124. }
  1125. if (lzo) {
  1126. dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
  1127. }
  1128. if (snappy) {
  1129. dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
  1130. }
  1131. if (has_begin) {
  1132. begin = qdict_get_int(qdict, "begin");
  1133. }
  1134. if (has_length) {
  1135. length = qdict_get_int(qdict, "length");
  1136. }
  1137. prot = g_strconcat("file:", file, NULL);
  1138. qmp_dump_guest_memory(paging, prot, has_begin, begin, has_length, length,
  1139. true, dump_format, &err);
  1140. hmp_handle_error(mon, &err);
  1141. g_free(prot);
  1142. }
  1143. void hmp_netdev_add(Monitor *mon, const QDict *qdict)
  1144. {
  1145. Error *err = NULL;
  1146. QemuOpts *opts;
  1147. opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
  1148. if (err) {
  1149. goto out;
  1150. }
  1151. netdev_add(opts, &err);
  1152. if (err) {
  1153. qemu_opts_del(opts);
  1154. }
  1155. out:
  1156. hmp_handle_error(mon, &err);
  1157. }
  1158. void hmp_netdev_del(Monitor *mon, const QDict *qdict)
  1159. {
  1160. const char *id = qdict_get_str(qdict, "id");
  1161. Error *err = NULL;
  1162. qmp_netdev_del(id, &err);
  1163. hmp_handle_error(mon, &err);
  1164. }
  1165. void hmp_object_add(Monitor *mon, const QDict *qdict)
  1166. {
  1167. Error *err = NULL;
  1168. Error *err_end = NULL;
  1169. QemuOpts *opts;
  1170. char *type = NULL;
  1171. char *id = NULL;
  1172. void *dummy = NULL;
  1173. OptsVisitor *ov;
  1174. QDict *pdict;
  1175. opts = qemu_opts_from_qdict(qemu_find_opts("object"), qdict, &err);
  1176. if (err) {
  1177. goto out;
  1178. }
  1179. ov = opts_visitor_new(opts);
  1180. pdict = qdict_clone_shallow(qdict);
  1181. visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
  1182. if (err) {
  1183. goto out_clean;
  1184. }
  1185. qdict_del(pdict, "qom-type");
  1186. visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
  1187. if (err) {
  1188. goto out_end;
  1189. }
  1190. qdict_del(pdict, "id");
  1191. visit_type_str(opts_get_visitor(ov), &id, "id", &err);
  1192. if (err) {
  1193. goto out_end;
  1194. }
  1195. object_add(type, id, pdict, opts_get_visitor(ov), &err);
  1196. out_end:
  1197. visit_end_struct(opts_get_visitor(ov), &err_end);
  1198. if (!err && err_end) {
  1199. qmp_object_del(id, NULL);
  1200. }
  1201. error_propagate(&err, err_end);
  1202. out_clean:
  1203. opts_visitor_cleanup(ov);
  1204. QDECREF(pdict);
  1205. qemu_opts_del(opts);
  1206. g_free(id);
  1207. g_free(type);
  1208. g_free(dummy);
  1209. out:
  1210. hmp_handle_error(mon, &err);
  1211. }
  1212. void hmp_getfd(Monitor *mon, const QDict *qdict)
  1213. {
  1214. const char *fdname = qdict_get_str(qdict, "fdname");
  1215. Error *err = NULL;
  1216. qmp_getfd(fdname, &err);
  1217. hmp_handle_error(mon, &err);
  1218. }
  1219. void hmp_closefd(Monitor *mon, const QDict *qdict)
  1220. {
  1221. const char *fdname = qdict_get_str(qdict, "fdname");
  1222. Error *err = NULL;
  1223. qmp_closefd(fdname, &err);
  1224. hmp_handle_error(mon, &err);
  1225. }
  1226. void hmp_send_key(Monitor *mon, const QDict *qdict)
  1227. {
  1228. const char *keys = qdict_get_str(qdict, "keys");
  1229. KeyValueList *keylist, *head = NULL, *tmp = NULL;
  1230. int has_hold_time = qdict_haskey(qdict, "hold-time");
  1231. int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
  1232. Error *err = NULL;
  1233. char keyname_buf[16];
  1234. char *separator;
  1235. int keyname_len;
  1236. while (1) {
  1237. separator = strchr(keys, '-');
  1238. keyname_len = separator ? separator - keys : strlen(keys);
  1239. pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
  1240. /* Be compatible with old interface, convert user inputted "<" */
  1241. if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
  1242. pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
  1243. keyname_len = 4;
  1244. }
  1245. keyname_buf[keyname_len] = 0;
  1246. keylist = g_malloc0(sizeof(*keylist));
  1247. keylist->value = g_malloc0(sizeof(*keylist->value));
  1248. if (!head) {
  1249. head = keylist;
  1250. }
  1251. if (tmp) {
  1252. tmp->next = keylist;
  1253. }
  1254. tmp = keylist;
  1255. if (strstart(keyname_buf, "0x", NULL)) {
  1256. char *endp;
  1257. int value = strtoul(keyname_buf, &endp, 0);
  1258. if (*endp != '\0') {
  1259. goto err_out;
  1260. }
  1261. keylist->value->kind = KEY_VALUE_KIND_NUMBER;
  1262. keylist->value->number = value;
  1263. } else {
  1264. int idx = index_from_key(keyname_buf);
  1265. if (idx == Q_KEY_CODE_MAX) {
  1266. goto err_out;
  1267. }
  1268. keylist->value->kind = KEY_VALUE_KIND_QCODE;
  1269. keylist->value->qcode = idx;
  1270. }
  1271. if (!separator) {
  1272. break;
  1273. }
  1274. keys = separator + 1;
  1275. }
  1276. qmp_send_key(head, has_hold_time, hold_time, &err);
  1277. hmp_handle_error(mon, &err);
  1278. out:
  1279. qapi_free_KeyValueList(head);
  1280. return;
  1281. err_out:
  1282. monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
  1283. goto out;
  1284. }
  1285. void hmp_screen_dump(Monitor *mon, const QDict *qdict)
  1286. {
  1287. const char *filename = qdict_get_str(qdict, "filename");
  1288. Error *err = NULL;
  1289. qmp_screendump(filename, &err);
  1290. hmp_handle_error(mon, &err);
  1291. }
  1292. void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
  1293. {
  1294. const char *uri = qdict_get_str(qdict, "uri");
  1295. int writable = qdict_get_try_bool(qdict, "writable", 0);
  1296. int all = qdict_get_try_bool(qdict, "all", 0);
  1297. Error *local_err = NULL;
  1298. BlockInfoList *block_list, *info;
  1299. SocketAddress *addr;
  1300. if (writable && !all) {
  1301. error_setg(&local_err, "-w only valid together with -a");
  1302. goto exit;
  1303. }
  1304. /* First check if the address is valid and start the server. */
  1305. addr = socket_parse(uri, &local_err);
  1306. if (local_err != NULL) {
  1307. goto exit;
  1308. }
  1309. qmp_nbd_server_start(addr, &local_err);
  1310. qapi_free_SocketAddress(addr);
  1311. if (local_err != NULL) {
  1312. goto exit;
  1313. }
  1314. if (!all) {
  1315. return;
  1316. }
  1317. /* Then try adding all block devices. If one fails, close all and
  1318. * exit.
  1319. */
  1320. block_list = qmp_query_block(NULL);
  1321. for (info = block_list; info; info = info->next) {
  1322. if (!info->value->has_inserted) {
  1323. continue;
  1324. }
  1325. qmp_nbd_server_add(info->value->device, true, writable, &local_err);
  1326. if (local_err != NULL) {
  1327. qmp_nbd_server_stop(NULL);
  1328. break;
  1329. }
  1330. }
  1331. qapi_free_BlockInfoList(block_list);
  1332. exit:
  1333. hmp_handle_error(mon, &local_err);
  1334. }
  1335. void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
  1336. {
  1337. const char *device = qdict_get_str(qdict, "device");
  1338. int writable = qdict_get_try_bool(qdict, "writable", 0);
  1339. Error *local_err = NULL;
  1340. qmp_nbd_server_add(device, true, writable, &local_err);
  1341. if (local_err != NULL) {
  1342. hmp_handle_error(mon, &local_err);
  1343. }
  1344. }
  1345. void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
  1346. {
  1347. Error *err = NULL;
  1348. qmp_nbd_server_stop(&err);
  1349. hmp_handle_error(mon, &err);
  1350. }
  1351. void hmp_cpu_add(Monitor *mon, const QDict *qdict)
  1352. {
  1353. int cpuid;
  1354. Error *err = NULL;
  1355. cpuid = qdict_get_int(qdict, "id");
  1356. qmp_cpu_add(cpuid, &err);
  1357. hmp_handle_error(mon, &err);
  1358. }
  1359. void hmp_chardev_add(Monitor *mon, const QDict *qdict)
  1360. {
  1361. const char *args = qdict_get_str(qdict, "args");
  1362. Error *err = NULL;
  1363. QemuOpts *opts;
  1364. opts = qemu_opts_parse(qemu_find_opts("chardev"), args, 1);
  1365. if (opts == NULL) {
  1366. error_setg(&err, "Parsing chardev args failed");
  1367. } else {
  1368. qemu_chr_new_from_opts(opts, NULL, &err);
  1369. }
  1370. hmp_handle_error(mon, &err);
  1371. }
  1372. void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
  1373. {
  1374. Error *local_err = NULL;
  1375. qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
  1376. hmp_handle_error(mon, &local_err);
  1377. }
  1378. void hmp_qemu_io(Monitor *mon, const QDict *qdict)
  1379. {
  1380. BlockDriverState *bs;
  1381. const char* device = qdict_get_str(qdict, "device");
  1382. const char* command = qdict_get_str(qdict, "command");
  1383. Error *err = NULL;
  1384. bs = bdrv_find(device);
  1385. if (bs) {
  1386. qemuio_command(bs, command);
  1387. } else {
  1388. error_set(&err, QERR_DEVICE_NOT_FOUND, device);
  1389. }
  1390. hmp_handle_error(mon, &err);
  1391. }
  1392. void hmp_object_del(Monitor *mon, const QDict *qdict)
  1393. {
  1394. const char *id = qdict_get_str(qdict, "id");
  1395. Error *err = NULL;
  1396. qmp_object_del(id, &err);
  1397. hmp_handle_error(mon, &err);
  1398. }
  1399. void hmp_info_memdev(Monitor *mon, const QDict *qdict)
  1400. {
  1401. Error *err = NULL;
  1402. MemdevList *memdev_list = qmp_query_memdev(&err);
  1403. MemdevList *m = memdev_list;
  1404. StringOutputVisitor *ov;
  1405. int i = 0;
  1406. while (m) {
  1407. ov = string_output_visitor_new(false);
  1408. visit_type_uint16List(string_output_get_visitor(ov),
  1409. &m->value->host_nodes, NULL, NULL);
  1410. monitor_printf(mon, "memory backend: %d\n", i);
  1411. monitor_printf(mon, " size: %" PRId64 "\n", m->value->size);
  1412. monitor_printf(mon, " merge: %s\n",
  1413. m->value->merge ? "true" : "false");
  1414. monitor_printf(mon, " dump: %s\n",
  1415. m->value->dump ? "true" : "false");
  1416. monitor_printf(mon, " prealloc: %s\n",
  1417. m->value->prealloc ? "true" : "false");
  1418. monitor_printf(mon, " policy: %s\n",
  1419. HostMemPolicy_lookup[m->value->policy]);
  1420. monitor_printf(mon, " host nodes: %s\n",
  1421. string_output_get_string(ov));
  1422. string_output_visitor_cleanup(ov);
  1423. m = m->next;
  1424. i++;
  1425. }
  1426. monitor_printf(mon, "\n");
  1427. }