2
0

hmp.c 40 KB

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