hmp.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917
  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 "qemu/osdep.h"
  16. #include "hmp.h"
  17. #include "net/net.h"
  18. #include "net/eth.h"
  19. #include "chardev/char.h"
  20. #include "sysemu/block-backend.h"
  21. #include "sysemu/sysemu.h"
  22. #include "qemu/config-file.h"
  23. #include "qemu/option.h"
  24. #include "qemu/timer.h"
  25. #include "qemu/sockets.h"
  26. #include "monitor/monitor.h"
  27. #include "monitor/qdev.h"
  28. #include "qapi/error.h"
  29. #include "qapi/opts-visitor.h"
  30. #include "qapi/qapi-builtin-visit.h"
  31. #include "qapi/qapi-commands-block.h"
  32. #include "qapi/qapi-commands-char.h"
  33. #include "qapi/qapi-commands-migration.h"
  34. #include "qapi/qapi-commands-misc.h"
  35. #include "qapi/qapi-commands-net.h"
  36. #include "qapi/qapi-commands-rocker.h"
  37. #include "qapi/qapi-commands-run-state.h"
  38. #include "qapi/qapi-commands-tpm.h"
  39. #include "qapi/qapi-commands-ui.h"
  40. #include "qapi/qmp/qdict.h"
  41. #include "qapi/qmp/qerror.h"
  42. #include "qapi/string-input-visitor.h"
  43. #include "qapi/string-output-visitor.h"
  44. #include "qom/object_interfaces.h"
  45. #include "ui/console.h"
  46. #include "block/nbd.h"
  47. #include "block/qapi.h"
  48. #include "qemu-io.h"
  49. #include "qemu/cutils.h"
  50. #include "qemu/error-report.h"
  51. #include "exec/ramlist.h"
  52. #include "hw/intc/intc.h"
  53. #include "migration/snapshot.h"
  54. #include "migration/misc.h"
  55. #ifdef CONFIG_SPICE
  56. #include <spice/enums.h>
  57. #endif
  58. static void hmp_handle_error(Monitor *mon, Error **errp)
  59. {
  60. assert(errp);
  61. if (*errp) {
  62. error_report_err(*errp);
  63. }
  64. }
  65. void hmp_info_name(Monitor *mon, const QDict *qdict)
  66. {
  67. NameInfo *info;
  68. info = qmp_query_name(NULL);
  69. if (info->has_name) {
  70. monitor_printf(mon, "%s\n", info->name);
  71. }
  72. qapi_free_NameInfo(info);
  73. }
  74. void hmp_info_version(Monitor *mon, const QDict *qdict)
  75. {
  76. VersionInfo *info;
  77. info = qmp_query_version(NULL);
  78. monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
  79. info->qemu->major, info->qemu->minor, info->qemu->micro,
  80. info->package);
  81. qapi_free_VersionInfo(info);
  82. }
  83. void hmp_info_kvm(Monitor *mon, const QDict *qdict)
  84. {
  85. KvmInfo *info;
  86. info = qmp_query_kvm(NULL);
  87. monitor_printf(mon, "kvm support: ");
  88. if (info->present) {
  89. monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
  90. } else {
  91. monitor_printf(mon, "not compiled\n");
  92. }
  93. qapi_free_KvmInfo(info);
  94. }
  95. void hmp_info_status(Monitor *mon, const QDict *qdict)
  96. {
  97. StatusInfo *info;
  98. info = qmp_query_status(NULL);
  99. monitor_printf(mon, "VM status: %s%s",
  100. info->running ? "running" : "paused",
  101. info->singlestep ? " (single step mode)" : "");
  102. if (!info->running && info->status != RUN_STATE_PAUSED) {
  103. monitor_printf(mon, " (%s)", RunState_str(info->status));
  104. }
  105. monitor_printf(mon, "\n");
  106. qapi_free_StatusInfo(info);
  107. }
  108. void hmp_info_uuid(Monitor *mon, const QDict *qdict)
  109. {
  110. UuidInfo *info;
  111. info = qmp_query_uuid(NULL);
  112. monitor_printf(mon, "%s\n", info->UUID);
  113. qapi_free_UuidInfo(info);
  114. }
  115. void hmp_info_chardev(Monitor *mon, const QDict *qdict)
  116. {
  117. ChardevInfoList *char_info, *info;
  118. char_info = qmp_query_chardev(NULL);
  119. for (info = char_info; info; info = info->next) {
  120. monitor_printf(mon, "%s: filename=%s\n", info->value->label,
  121. info->value->filename);
  122. }
  123. qapi_free_ChardevInfoList(char_info);
  124. }
  125. void hmp_info_mice(Monitor *mon, const QDict *qdict)
  126. {
  127. MouseInfoList *mice_list, *mouse;
  128. mice_list = qmp_query_mice(NULL);
  129. if (!mice_list) {
  130. monitor_printf(mon, "No mouse devices connected\n");
  131. return;
  132. }
  133. for (mouse = mice_list; mouse; mouse = mouse->next) {
  134. monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
  135. mouse->value->current ? '*' : ' ',
  136. mouse->value->index, mouse->value->name,
  137. mouse->value->absolute ? " (absolute)" : "");
  138. }
  139. qapi_free_MouseInfoList(mice_list);
  140. }
  141. void hmp_info_migrate(Monitor *mon, const QDict *qdict)
  142. {
  143. MigrationInfo *info;
  144. MigrationCapabilityStatusList *caps, *cap;
  145. info = qmp_query_migrate(NULL);
  146. caps = qmp_query_migrate_capabilities(NULL);
  147. migration_global_dump(mon);
  148. /* do not display parameters during setup */
  149. if (info->has_status && caps) {
  150. monitor_printf(mon, "capabilities: ");
  151. for (cap = caps; cap; cap = cap->next) {
  152. monitor_printf(mon, "%s: %s ",
  153. MigrationCapability_str(cap->value->capability),
  154. cap->value->state ? "on" : "off");
  155. }
  156. monitor_printf(mon, "\n");
  157. }
  158. if (info->has_status) {
  159. monitor_printf(mon, "Migration status: %s",
  160. MigrationStatus_str(info->status));
  161. if (info->status == MIGRATION_STATUS_FAILED &&
  162. info->has_error_desc) {
  163. monitor_printf(mon, " (%s)\n", info->error_desc);
  164. } else {
  165. monitor_printf(mon, "\n");
  166. }
  167. monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
  168. info->total_time);
  169. if (info->has_expected_downtime) {
  170. monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
  171. info->expected_downtime);
  172. }
  173. if (info->has_downtime) {
  174. monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
  175. info->downtime);
  176. }
  177. if (info->has_setup_time) {
  178. monitor_printf(mon, "setup: %" PRIu64 " milliseconds\n",
  179. info->setup_time);
  180. }
  181. }
  182. if (info->has_ram) {
  183. monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
  184. info->ram->transferred >> 10);
  185. monitor_printf(mon, "throughput: %0.2f mbps\n",
  186. info->ram->mbps);
  187. monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
  188. info->ram->remaining >> 10);
  189. monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
  190. info->ram->total >> 10);
  191. monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
  192. info->ram->duplicate);
  193. monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
  194. info->ram->skipped);
  195. monitor_printf(mon, "normal: %" PRIu64 " pages\n",
  196. info->ram->normal);
  197. monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
  198. info->ram->normal_bytes >> 10);
  199. monitor_printf(mon, "dirty sync count: %" PRIu64 "\n",
  200. info->ram->dirty_sync_count);
  201. monitor_printf(mon, "page size: %" PRIu64 " kbytes\n",
  202. info->ram->page_size >> 10);
  203. if (info->ram->dirty_pages_rate) {
  204. monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
  205. info->ram->dirty_pages_rate);
  206. }
  207. if (info->ram->postcopy_requests) {
  208. monitor_printf(mon, "postcopy request count: %" PRIu64 "\n",
  209. info->ram->postcopy_requests);
  210. }
  211. }
  212. if (info->has_disk) {
  213. monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
  214. info->disk->transferred >> 10);
  215. monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
  216. info->disk->remaining >> 10);
  217. monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
  218. info->disk->total >> 10);
  219. }
  220. if (info->has_xbzrle_cache) {
  221. monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
  222. info->xbzrle_cache->cache_size);
  223. monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
  224. info->xbzrle_cache->bytes >> 10);
  225. monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
  226. info->xbzrle_cache->pages);
  227. monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
  228. info->xbzrle_cache->cache_miss);
  229. monitor_printf(mon, "xbzrle cache miss rate: %0.2f\n",
  230. info->xbzrle_cache->cache_miss_rate);
  231. monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
  232. info->xbzrle_cache->overflow);
  233. }
  234. if (info->has_cpu_throttle_percentage) {
  235. monitor_printf(mon, "cpu throttle percentage: %" PRIu64 "\n",
  236. info->cpu_throttle_percentage);
  237. }
  238. qapi_free_MigrationInfo(info);
  239. qapi_free_MigrationCapabilityStatusList(caps);
  240. }
  241. void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
  242. {
  243. MigrationCapabilityStatusList *caps, *cap;
  244. caps = qmp_query_migrate_capabilities(NULL);
  245. if (caps) {
  246. for (cap = caps; cap; cap = cap->next) {
  247. monitor_printf(mon, "%s: %s\n",
  248. MigrationCapability_str(cap->value->capability),
  249. cap->value->state ? "on" : "off");
  250. }
  251. }
  252. qapi_free_MigrationCapabilityStatusList(caps);
  253. }
  254. void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict)
  255. {
  256. MigrationParameters *params;
  257. params = qmp_query_migrate_parameters(NULL);
  258. if (params) {
  259. assert(params->has_compress_level);
  260. monitor_printf(mon, "%s: %u\n",
  261. MigrationParameter_str(MIGRATION_PARAMETER_COMPRESS_LEVEL),
  262. params->compress_level);
  263. assert(params->has_compress_threads);
  264. monitor_printf(mon, "%s: %u\n",
  265. MigrationParameter_str(MIGRATION_PARAMETER_COMPRESS_THREADS),
  266. params->compress_threads);
  267. assert(params->has_decompress_threads);
  268. monitor_printf(mon, "%s: %u\n",
  269. MigrationParameter_str(MIGRATION_PARAMETER_DECOMPRESS_THREADS),
  270. params->decompress_threads);
  271. assert(params->has_cpu_throttle_initial);
  272. monitor_printf(mon, "%s: %u\n",
  273. MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL),
  274. params->cpu_throttle_initial);
  275. assert(params->has_cpu_throttle_increment);
  276. monitor_printf(mon, "%s: %u\n",
  277. MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT),
  278. params->cpu_throttle_increment);
  279. assert(params->has_tls_creds);
  280. monitor_printf(mon, "%s: '%s'\n",
  281. MigrationParameter_str(MIGRATION_PARAMETER_TLS_CREDS),
  282. params->tls_creds);
  283. assert(params->has_tls_hostname);
  284. monitor_printf(mon, "%s: '%s'\n",
  285. MigrationParameter_str(MIGRATION_PARAMETER_TLS_HOSTNAME),
  286. params->tls_hostname);
  287. assert(params->has_max_bandwidth);
  288. monitor_printf(mon, "%s: %" PRIu64 " bytes/second\n",
  289. MigrationParameter_str(MIGRATION_PARAMETER_MAX_BANDWIDTH),
  290. params->max_bandwidth);
  291. assert(params->has_downtime_limit);
  292. monitor_printf(mon, "%s: %" PRIu64 " milliseconds\n",
  293. MigrationParameter_str(MIGRATION_PARAMETER_DOWNTIME_LIMIT),
  294. params->downtime_limit);
  295. assert(params->has_x_checkpoint_delay);
  296. monitor_printf(mon, "%s: %u\n",
  297. MigrationParameter_str(MIGRATION_PARAMETER_X_CHECKPOINT_DELAY),
  298. params->x_checkpoint_delay);
  299. assert(params->has_block_incremental);
  300. monitor_printf(mon, "%s: %s\n",
  301. MigrationParameter_str(MIGRATION_PARAMETER_BLOCK_INCREMENTAL),
  302. params->block_incremental ? "on" : "off");
  303. monitor_printf(mon, "%s: %u\n",
  304. MigrationParameter_str(MIGRATION_PARAMETER_X_MULTIFD_CHANNELS),
  305. params->x_multifd_channels);
  306. monitor_printf(mon, "%s: %u\n",
  307. MigrationParameter_str(MIGRATION_PARAMETER_X_MULTIFD_PAGE_COUNT),
  308. params->x_multifd_page_count);
  309. monitor_printf(mon, "%s: %" PRIu64 "\n",
  310. MigrationParameter_str(MIGRATION_PARAMETER_XBZRLE_CACHE_SIZE),
  311. params->xbzrle_cache_size);
  312. }
  313. qapi_free_MigrationParameters(params);
  314. }
  315. void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
  316. {
  317. monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
  318. qmp_query_migrate_cache_size(NULL) >> 10);
  319. }
  320. void hmp_info_cpus(Monitor *mon, const QDict *qdict)
  321. {
  322. CpuInfoFastList *cpu_list, *cpu;
  323. cpu_list = qmp_query_cpus_fast(NULL);
  324. for (cpu = cpu_list; cpu; cpu = cpu->next) {
  325. int active = ' ';
  326. if (cpu->value->cpu_index == monitor_get_cpu_index()) {
  327. active = '*';
  328. }
  329. monitor_printf(mon, "%c CPU #%" PRId64 ":", active,
  330. cpu->value->cpu_index);
  331. monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
  332. }
  333. qapi_free_CpuInfoFastList(cpu_list);
  334. }
  335. static void print_block_info(Monitor *mon, BlockInfo *info,
  336. BlockDeviceInfo *inserted, bool verbose)
  337. {
  338. ImageInfo *image_info;
  339. assert(!info || !info->has_inserted || info->inserted == inserted);
  340. if (info && *info->device) {
  341. monitor_printf(mon, "%s", info->device);
  342. if (inserted && inserted->has_node_name) {
  343. monitor_printf(mon, " (%s)", inserted->node_name);
  344. }
  345. } else {
  346. assert(info || inserted);
  347. monitor_printf(mon, "%s",
  348. inserted && inserted->has_node_name ? inserted->node_name
  349. : info && info->has_qdev ? info->qdev
  350. : "<anonymous>");
  351. }
  352. if (inserted) {
  353. monitor_printf(mon, ": %s (%s%s%s)\n",
  354. inserted->file,
  355. inserted->drv,
  356. inserted->ro ? ", read-only" : "",
  357. inserted->encrypted ? ", encrypted" : "");
  358. } else {
  359. monitor_printf(mon, ": [not inserted]\n");
  360. }
  361. if (info) {
  362. if (info->has_qdev) {
  363. monitor_printf(mon, " Attached to: %s\n", info->qdev);
  364. }
  365. if (info->has_io_status && info->io_status != BLOCK_DEVICE_IO_STATUS_OK) {
  366. monitor_printf(mon, " I/O status: %s\n",
  367. BlockDeviceIoStatus_str(info->io_status));
  368. }
  369. if (info->removable) {
  370. monitor_printf(mon, " Removable device: %slocked, tray %s\n",
  371. info->locked ? "" : "not ",
  372. info->tray_open ? "open" : "closed");
  373. }
  374. }
  375. if (!inserted) {
  376. return;
  377. }
  378. monitor_printf(mon, " Cache mode: %s%s%s\n",
  379. inserted->cache->writeback ? "writeback" : "writethrough",
  380. inserted->cache->direct ? ", direct" : "",
  381. inserted->cache->no_flush ? ", ignore flushes" : "");
  382. if (inserted->has_backing_file) {
  383. monitor_printf(mon,
  384. " Backing file: %s "
  385. "(chain depth: %" PRId64 ")\n",
  386. inserted->backing_file,
  387. inserted->backing_file_depth);
  388. }
  389. if (inserted->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF) {
  390. monitor_printf(mon, " Detect zeroes: %s\n",
  391. BlockdevDetectZeroesOptions_str(inserted->detect_zeroes));
  392. }
  393. if (inserted->bps || inserted->bps_rd || inserted->bps_wr ||
  394. inserted->iops || inserted->iops_rd || inserted->iops_wr)
  395. {
  396. monitor_printf(mon, " I/O throttling: bps=%" PRId64
  397. " bps_rd=%" PRId64 " bps_wr=%" PRId64
  398. " bps_max=%" PRId64
  399. " bps_rd_max=%" PRId64
  400. " bps_wr_max=%" PRId64
  401. " iops=%" PRId64 " iops_rd=%" PRId64
  402. " iops_wr=%" PRId64
  403. " iops_max=%" PRId64
  404. " iops_rd_max=%" PRId64
  405. " iops_wr_max=%" PRId64
  406. " iops_size=%" PRId64
  407. " group=%s\n",
  408. inserted->bps,
  409. inserted->bps_rd,
  410. inserted->bps_wr,
  411. inserted->bps_max,
  412. inserted->bps_rd_max,
  413. inserted->bps_wr_max,
  414. inserted->iops,
  415. inserted->iops_rd,
  416. inserted->iops_wr,
  417. inserted->iops_max,
  418. inserted->iops_rd_max,
  419. inserted->iops_wr_max,
  420. inserted->iops_size,
  421. inserted->group);
  422. }
  423. if (verbose) {
  424. monitor_printf(mon, "\nImages:\n");
  425. image_info = inserted->image;
  426. while (1) {
  427. bdrv_image_info_dump((fprintf_function)monitor_printf,
  428. mon, image_info);
  429. if (image_info->has_backing_image) {
  430. image_info = image_info->backing_image;
  431. } else {
  432. break;
  433. }
  434. }
  435. }
  436. }
  437. void hmp_info_block(Monitor *mon, const QDict *qdict)
  438. {
  439. BlockInfoList *block_list, *info;
  440. BlockDeviceInfoList *blockdev_list, *blockdev;
  441. const char *device = qdict_get_try_str(qdict, "device");
  442. bool verbose = qdict_get_try_bool(qdict, "verbose", false);
  443. bool nodes = qdict_get_try_bool(qdict, "nodes", false);
  444. bool printed = false;
  445. /* Print BlockBackend information */
  446. if (!nodes) {
  447. block_list = qmp_query_block(NULL);
  448. } else {
  449. block_list = NULL;
  450. }
  451. for (info = block_list; info; info = info->next) {
  452. if (device && strcmp(device, info->value->device)) {
  453. continue;
  454. }
  455. if (info != block_list) {
  456. monitor_printf(mon, "\n");
  457. }
  458. print_block_info(mon, info->value, info->value->has_inserted
  459. ? info->value->inserted : NULL,
  460. verbose);
  461. printed = true;
  462. }
  463. qapi_free_BlockInfoList(block_list);
  464. if ((!device && !nodes) || printed) {
  465. return;
  466. }
  467. /* Print node information */
  468. blockdev_list = qmp_query_named_block_nodes(NULL);
  469. for (blockdev = blockdev_list; blockdev; blockdev = blockdev->next) {
  470. assert(blockdev->value->has_node_name);
  471. if (device && strcmp(device, blockdev->value->node_name)) {
  472. continue;
  473. }
  474. if (blockdev != blockdev_list) {
  475. monitor_printf(mon, "\n");
  476. }
  477. print_block_info(mon, NULL, blockdev->value, verbose);
  478. }
  479. qapi_free_BlockDeviceInfoList(blockdev_list);
  480. }
  481. void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
  482. {
  483. BlockStatsList *stats_list, *stats;
  484. stats_list = qmp_query_blockstats(false, false, NULL);
  485. for (stats = stats_list; stats; stats = stats->next) {
  486. if (!stats->value->has_device) {
  487. continue;
  488. }
  489. monitor_printf(mon, "%s:", stats->value->device);
  490. monitor_printf(mon, " rd_bytes=%" PRId64
  491. " wr_bytes=%" PRId64
  492. " rd_operations=%" PRId64
  493. " wr_operations=%" PRId64
  494. " flush_operations=%" PRId64
  495. " wr_total_time_ns=%" PRId64
  496. " rd_total_time_ns=%" PRId64
  497. " flush_total_time_ns=%" PRId64
  498. " rd_merged=%" PRId64
  499. " wr_merged=%" PRId64
  500. " idle_time_ns=%" PRId64
  501. "\n",
  502. stats->value->stats->rd_bytes,
  503. stats->value->stats->wr_bytes,
  504. stats->value->stats->rd_operations,
  505. stats->value->stats->wr_operations,
  506. stats->value->stats->flush_operations,
  507. stats->value->stats->wr_total_time_ns,
  508. stats->value->stats->rd_total_time_ns,
  509. stats->value->stats->flush_total_time_ns,
  510. stats->value->stats->rd_merged,
  511. stats->value->stats->wr_merged,
  512. stats->value->stats->idle_time_ns);
  513. }
  514. qapi_free_BlockStatsList(stats_list);
  515. }
  516. /* Helper for hmp_info_vnc_clients, _servers */
  517. static void hmp_info_VncBasicInfo(Monitor *mon, VncBasicInfo *info,
  518. const char *name)
  519. {
  520. monitor_printf(mon, " %s: %s:%s (%s%s)\n",
  521. name,
  522. info->host,
  523. info->service,
  524. NetworkAddressFamily_str(info->family),
  525. info->websocket ? " (Websocket)" : "");
  526. }
  527. /* Helper displaying and auth and crypt info */
  528. static void hmp_info_vnc_authcrypt(Monitor *mon, const char *indent,
  529. VncPrimaryAuth auth,
  530. VncVencryptSubAuth *vencrypt)
  531. {
  532. monitor_printf(mon, "%sAuth: %s (Sub: %s)\n", indent,
  533. VncPrimaryAuth_str(auth),
  534. vencrypt ? VncVencryptSubAuth_str(*vencrypt) : "none");
  535. }
  536. static void hmp_info_vnc_clients(Monitor *mon, VncClientInfoList *client)
  537. {
  538. while (client) {
  539. VncClientInfo *cinfo = client->value;
  540. hmp_info_VncBasicInfo(mon, qapi_VncClientInfo_base(cinfo), "Client");
  541. monitor_printf(mon, " x509_dname: %s\n",
  542. cinfo->has_x509_dname ?
  543. cinfo->x509_dname : "none");
  544. monitor_printf(mon, " sasl_username: %s\n",
  545. cinfo->has_sasl_username ?
  546. cinfo->sasl_username : "none");
  547. client = client->next;
  548. }
  549. }
  550. static void hmp_info_vnc_servers(Monitor *mon, VncServerInfo2List *server)
  551. {
  552. while (server) {
  553. VncServerInfo2 *sinfo = server->value;
  554. hmp_info_VncBasicInfo(mon, qapi_VncServerInfo2_base(sinfo), "Server");
  555. hmp_info_vnc_authcrypt(mon, " ", sinfo->auth,
  556. sinfo->has_vencrypt ? &sinfo->vencrypt : NULL);
  557. server = server->next;
  558. }
  559. }
  560. void hmp_info_vnc(Monitor *mon, const QDict *qdict)
  561. {
  562. VncInfo2List *info2l;
  563. Error *err = NULL;
  564. info2l = qmp_query_vnc_servers(&err);
  565. if (err) {
  566. hmp_handle_error(mon, &err);
  567. return;
  568. }
  569. if (!info2l) {
  570. monitor_printf(mon, "None\n");
  571. return;
  572. }
  573. while (info2l) {
  574. VncInfo2 *info = info2l->value;
  575. monitor_printf(mon, "%s:\n", info->id);
  576. hmp_info_vnc_servers(mon, info->server);
  577. hmp_info_vnc_clients(mon, info->clients);
  578. if (!info->server) {
  579. /* The server entry displays its auth, we only
  580. * need to display in the case of 'reverse' connections
  581. * where there's no server.
  582. */
  583. hmp_info_vnc_authcrypt(mon, " ", info->auth,
  584. info->has_vencrypt ? &info->vencrypt : NULL);
  585. }
  586. if (info->has_display) {
  587. monitor_printf(mon, " Display: %s\n", info->display);
  588. }
  589. info2l = info2l->next;
  590. }
  591. qapi_free_VncInfo2List(info2l);
  592. }
  593. #ifdef CONFIG_SPICE
  594. void hmp_info_spice(Monitor *mon, const QDict *qdict)
  595. {
  596. SpiceChannelList *chan;
  597. SpiceInfo *info;
  598. const char *channel_name;
  599. const char * const channel_names[] = {
  600. [SPICE_CHANNEL_MAIN] = "main",
  601. [SPICE_CHANNEL_DISPLAY] = "display",
  602. [SPICE_CHANNEL_INPUTS] = "inputs",
  603. [SPICE_CHANNEL_CURSOR] = "cursor",
  604. [SPICE_CHANNEL_PLAYBACK] = "playback",
  605. [SPICE_CHANNEL_RECORD] = "record",
  606. [SPICE_CHANNEL_TUNNEL] = "tunnel",
  607. [SPICE_CHANNEL_SMARTCARD] = "smartcard",
  608. [SPICE_CHANNEL_USBREDIR] = "usbredir",
  609. [SPICE_CHANNEL_PORT] = "port",
  610. #if 0
  611. /* minimum spice-protocol is 0.12.3, webdav was added in 0.12.7,
  612. * no easy way to #ifdef (SPICE_CHANNEL_* is a enum). Disable
  613. * as quick fix for build failures with older versions. */
  614. [SPICE_CHANNEL_WEBDAV] = "webdav",
  615. #endif
  616. };
  617. info = qmp_query_spice(NULL);
  618. if (!info->enabled) {
  619. monitor_printf(mon, "Server: disabled\n");
  620. goto out;
  621. }
  622. monitor_printf(mon, "Server:\n");
  623. if (info->has_port) {
  624. monitor_printf(mon, " address: %s:%" PRId64 "\n",
  625. info->host, info->port);
  626. }
  627. if (info->has_tls_port) {
  628. monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
  629. info->host, info->tls_port);
  630. }
  631. monitor_printf(mon, " migrated: %s\n",
  632. info->migrated ? "true" : "false");
  633. monitor_printf(mon, " auth: %s\n", info->auth);
  634. monitor_printf(mon, " compiled: %s\n", info->compiled_version);
  635. monitor_printf(mon, " mouse-mode: %s\n",
  636. SpiceQueryMouseMode_str(info->mouse_mode));
  637. if (!info->has_channels || info->channels == NULL) {
  638. monitor_printf(mon, "Channels: none\n");
  639. } else {
  640. for (chan = info->channels; chan; chan = chan->next) {
  641. monitor_printf(mon, "Channel:\n");
  642. monitor_printf(mon, " address: %s:%s%s\n",
  643. chan->value->host, chan->value->port,
  644. chan->value->tls ? " [tls]" : "");
  645. monitor_printf(mon, " session: %" PRId64 "\n",
  646. chan->value->connection_id);
  647. monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
  648. chan->value->channel_type, chan->value->channel_id);
  649. channel_name = "unknown";
  650. if (chan->value->channel_type > 0 &&
  651. chan->value->channel_type < ARRAY_SIZE(channel_names) &&
  652. channel_names[chan->value->channel_type]) {
  653. channel_name = channel_names[chan->value->channel_type];
  654. }
  655. monitor_printf(mon, " channel name: %s\n", channel_name);
  656. }
  657. }
  658. out:
  659. qapi_free_SpiceInfo(info);
  660. }
  661. #endif
  662. void hmp_info_balloon(Monitor *mon, const QDict *qdict)
  663. {
  664. BalloonInfo *info;
  665. Error *err = NULL;
  666. info = qmp_query_balloon(&err);
  667. if (err) {
  668. hmp_handle_error(mon, &err);
  669. return;
  670. }
  671. monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
  672. qapi_free_BalloonInfo(info);
  673. }
  674. static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
  675. {
  676. PciMemoryRegionList *region;
  677. monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
  678. monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
  679. dev->slot, dev->function);
  680. monitor_printf(mon, " ");
  681. if (dev->class_info->has_desc) {
  682. monitor_printf(mon, "%s", dev->class_info->desc);
  683. } else {
  684. monitor_printf(mon, "Class %04" PRId64, dev->class_info->q_class);
  685. }
  686. monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
  687. dev->id->vendor, dev->id->device);
  688. if (dev->has_irq) {
  689. monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
  690. }
  691. if (dev->has_pci_bridge) {
  692. monitor_printf(mon, " BUS %" PRId64 ".\n",
  693. dev->pci_bridge->bus->number);
  694. monitor_printf(mon, " secondary bus %" PRId64 ".\n",
  695. dev->pci_bridge->bus->secondary);
  696. monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
  697. dev->pci_bridge->bus->subordinate);
  698. monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
  699. dev->pci_bridge->bus->io_range->base,
  700. dev->pci_bridge->bus->io_range->limit);
  701. monitor_printf(mon,
  702. " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
  703. dev->pci_bridge->bus->memory_range->base,
  704. dev->pci_bridge->bus->memory_range->limit);
  705. monitor_printf(mon, " prefetchable memory range "
  706. "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
  707. dev->pci_bridge->bus->prefetchable_range->base,
  708. dev->pci_bridge->bus->prefetchable_range->limit);
  709. }
  710. for (region = dev->regions; region; region = region->next) {
  711. uint64_t addr, size;
  712. addr = region->value->address;
  713. size = region->value->size;
  714. monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
  715. if (!strcmp(region->value->type, "io")) {
  716. monitor_printf(mon, "I/O at 0x%04" PRIx64
  717. " [0x%04" PRIx64 "].\n",
  718. addr, addr + size - 1);
  719. } else {
  720. monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
  721. " [0x%08" PRIx64 "].\n",
  722. region->value->mem_type_64 ? 64 : 32,
  723. region->value->prefetch ? " prefetchable" : "",
  724. addr, addr + size - 1);
  725. }
  726. }
  727. monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
  728. if (dev->has_pci_bridge) {
  729. if (dev->pci_bridge->has_devices) {
  730. PciDeviceInfoList *cdev;
  731. for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
  732. hmp_info_pci_device(mon, cdev->value);
  733. }
  734. }
  735. }
  736. }
  737. static int hmp_info_irq_foreach(Object *obj, void *opaque)
  738. {
  739. InterruptStatsProvider *intc;
  740. InterruptStatsProviderClass *k;
  741. Monitor *mon = opaque;
  742. if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
  743. intc = INTERRUPT_STATS_PROVIDER(obj);
  744. k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
  745. uint64_t *irq_counts;
  746. unsigned int nb_irqs, i;
  747. if (k->get_statistics &&
  748. k->get_statistics(intc, &irq_counts, &nb_irqs)) {
  749. if (nb_irqs > 0) {
  750. monitor_printf(mon, "IRQ statistics for %s:\n",
  751. object_get_typename(obj));
  752. for (i = 0; i < nb_irqs; i++) {
  753. if (irq_counts[i] > 0) {
  754. monitor_printf(mon, "%2d: %" PRId64 "\n", i,
  755. irq_counts[i]);
  756. }
  757. }
  758. }
  759. } else {
  760. monitor_printf(mon, "IRQ statistics not available for %s.\n",
  761. object_get_typename(obj));
  762. }
  763. }
  764. return 0;
  765. }
  766. void hmp_info_irq(Monitor *mon, const QDict *qdict)
  767. {
  768. object_child_foreach_recursive(object_get_root(),
  769. hmp_info_irq_foreach, mon);
  770. }
  771. static int hmp_info_pic_foreach(Object *obj, void *opaque)
  772. {
  773. InterruptStatsProvider *intc;
  774. InterruptStatsProviderClass *k;
  775. Monitor *mon = opaque;
  776. if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
  777. intc = INTERRUPT_STATS_PROVIDER(obj);
  778. k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
  779. if (k->print_info) {
  780. k->print_info(intc, mon);
  781. } else {
  782. monitor_printf(mon, "Interrupt controller information not available for %s.\n",
  783. object_get_typename(obj));
  784. }
  785. }
  786. return 0;
  787. }
  788. void hmp_info_pic(Monitor *mon, const QDict *qdict)
  789. {
  790. object_child_foreach_recursive(object_get_root(),
  791. hmp_info_pic_foreach, mon);
  792. }
  793. void hmp_info_pci(Monitor *mon, const QDict *qdict)
  794. {
  795. PciInfoList *info_list, *info;
  796. Error *err = NULL;
  797. info_list = qmp_query_pci(&err);
  798. if (err) {
  799. monitor_printf(mon, "PCI devices not supported\n");
  800. error_free(err);
  801. return;
  802. }
  803. for (info = info_list; info; info = info->next) {
  804. PciDeviceInfoList *dev;
  805. for (dev = info->value->devices; dev; dev = dev->next) {
  806. hmp_info_pci_device(mon, dev->value);
  807. }
  808. }
  809. qapi_free_PciInfoList(info_list);
  810. }
  811. void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
  812. {
  813. BlockJobInfoList *list;
  814. Error *err = NULL;
  815. list = qmp_query_block_jobs(&err);
  816. assert(!err);
  817. if (!list) {
  818. monitor_printf(mon, "No active jobs\n");
  819. return;
  820. }
  821. while (list) {
  822. if (strcmp(list->value->type, "stream") == 0) {
  823. monitor_printf(mon, "Streaming device %s: Completed %" PRId64
  824. " of %" PRId64 " bytes, speed limit %" PRId64
  825. " bytes/s\n",
  826. list->value->device,
  827. list->value->offset,
  828. list->value->len,
  829. list->value->speed);
  830. } else {
  831. monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
  832. " of %" PRId64 " bytes, speed limit %" PRId64
  833. " bytes/s\n",
  834. list->value->type,
  835. list->value->device,
  836. list->value->offset,
  837. list->value->len,
  838. list->value->speed);
  839. }
  840. list = list->next;
  841. }
  842. qapi_free_BlockJobInfoList(list);
  843. }
  844. void hmp_info_tpm(Monitor *mon, const QDict *qdict)
  845. {
  846. TPMInfoList *info_list, *info;
  847. Error *err = NULL;
  848. unsigned int c = 0;
  849. TPMPassthroughOptions *tpo;
  850. TPMEmulatorOptions *teo;
  851. info_list = qmp_query_tpm(&err);
  852. if (err) {
  853. monitor_printf(mon, "TPM device not supported\n");
  854. error_free(err);
  855. return;
  856. }
  857. if (info_list) {
  858. monitor_printf(mon, "TPM device:\n");
  859. }
  860. for (info = info_list; info; info = info->next) {
  861. TPMInfo *ti = info->value;
  862. monitor_printf(mon, " tpm%d: model=%s\n",
  863. c, TpmModel_str(ti->model));
  864. monitor_printf(mon, " \\ %s: type=%s",
  865. ti->id, TpmTypeOptionsKind_str(ti->options->type));
  866. switch (ti->options->type) {
  867. case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
  868. tpo = ti->options->u.passthrough.data;
  869. monitor_printf(mon, "%s%s%s%s",
  870. tpo->has_path ? ",path=" : "",
  871. tpo->has_path ? tpo->path : "",
  872. tpo->has_cancel_path ? ",cancel-path=" : "",
  873. tpo->has_cancel_path ? tpo->cancel_path : "");
  874. break;
  875. case TPM_TYPE_OPTIONS_KIND_EMULATOR:
  876. teo = ti->options->u.emulator.data;
  877. monitor_printf(mon, ",chardev=%s", teo->chardev);
  878. break;
  879. case TPM_TYPE_OPTIONS_KIND__MAX:
  880. break;
  881. }
  882. monitor_printf(mon, "\n");
  883. c++;
  884. }
  885. qapi_free_TPMInfoList(info_list);
  886. }
  887. void hmp_quit(Monitor *mon, const QDict *qdict)
  888. {
  889. monitor_suspend(mon);
  890. qmp_quit(NULL);
  891. }
  892. void hmp_stop(Monitor *mon, const QDict *qdict)
  893. {
  894. qmp_stop(NULL);
  895. }
  896. void hmp_system_reset(Monitor *mon, const QDict *qdict)
  897. {
  898. qmp_system_reset(NULL);
  899. }
  900. void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
  901. {
  902. qmp_system_powerdown(NULL);
  903. }
  904. void hmp_cpu(Monitor *mon, const QDict *qdict)
  905. {
  906. int64_t cpu_index;
  907. /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
  908. use it are converted to the QAPI */
  909. cpu_index = qdict_get_int(qdict, "index");
  910. if (monitor_set_cpu(cpu_index) < 0) {
  911. monitor_printf(mon, "invalid CPU index\n");
  912. }
  913. }
  914. void hmp_memsave(Monitor *mon, const QDict *qdict)
  915. {
  916. uint32_t size = qdict_get_int(qdict, "size");
  917. const char *filename = qdict_get_str(qdict, "filename");
  918. uint64_t addr = qdict_get_int(qdict, "val");
  919. Error *err = NULL;
  920. int cpu_index = monitor_get_cpu_index();
  921. if (cpu_index < 0) {
  922. monitor_printf(mon, "No CPU available\n");
  923. return;
  924. }
  925. qmp_memsave(addr, size, filename, true, cpu_index, &err);
  926. hmp_handle_error(mon, &err);
  927. }
  928. void hmp_pmemsave(Monitor *mon, const QDict *qdict)
  929. {
  930. uint32_t size = qdict_get_int(qdict, "size");
  931. const char *filename = qdict_get_str(qdict, "filename");
  932. uint64_t addr = qdict_get_int(qdict, "val");
  933. Error *err = NULL;
  934. qmp_pmemsave(addr, size, filename, &err);
  935. hmp_handle_error(mon, &err);
  936. }
  937. void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
  938. {
  939. const char *chardev = qdict_get_str(qdict, "device");
  940. const char *data = qdict_get_str(qdict, "data");
  941. Error *err = NULL;
  942. qmp_ringbuf_write(chardev, data, false, 0, &err);
  943. hmp_handle_error(mon, &err);
  944. }
  945. void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
  946. {
  947. uint32_t size = qdict_get_int(qdict, "size");
  948. const char *chardev = qdict_get_str(qdict, "device");
  949. char *data;
  950. Error *err = NULL;
  951. int i;
  952. data = qmp_ringbuf_read(chardev, size, false, 0, &err);
  953. if (err) {
  954. hmp_handle_error(mon, &err);
  955. return;
  956. }
  957. for (i = 0; data[i]; i++) {
  958. unsigned char ch = data[i];
  959. if (ch == '\\') {
  960. monitor_printf(mon, "\\\\");
  961. } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
  962. monitor_printf(mon, "\\u%04X", ch);
  963. } else {
  964. monitor_printf(mon, "%c", ch);
  965. }
  966. }
  967. monitor_printf(mon, "\n");
  968. g_free(data);
  969. }
  970. void hmp_cont(Monitor *mon, const QDict *qdict)
  971. {
  972. Error *err = NULL;
  973. qmp_cont(&err);
  974. hmp_handle_error(mon, &err);
  975. }
  976. void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
  977. {
  978. qmp_system_wakeup(NULL);
  979. }
  980. void hmp_nmi(Monitor *mon, const QDict *qdict)
  981. {
  982. Error *err = NULL;
  983. qmp_inject_nmi(&err);
  984. hmp_handle_error(mon, &err);
  985. }
  986. void hmp_set_link(Monitor *mon, const QDict *qdict)
  987. {
  988. const char *name = qdict_get_str(qdict, "name");
  989. bool up = qdict_get_bool(qdict, "up");
  990. Error *err = NULL;
  991. qmp_set_link(name, up, &err);
  992. hmp_handle_error(mon, &err);
  993. }
  994. void hmp_block_passwd(Monitor *mon, const QDict *qdict)
  995. {
  996. const char *device = qdict_get_str(qdict, "device");
  997. const char *password = qdict_get_str(qdict, "password");
  998. Error *err = NULL;
  999. qmp_block_passwd(true, device, false, NULL, password, &err);
  1000. hmp_handle_error(mon, &err);
  1001. }
  1002. void hmp_balloon(Monitor *mon, const QDict *qdict)
  1003. {
  1004. int64_t value = qdict_get_int(qdict, "value");
  1005. Error *err = NULL;
  1006. qmp_balloon(value, &err);
  1007. hmp_handle_error(mon, &err);
  1008. }
  1009. void hmp_block_resize(Monitor *mon, const QDict *qdict)
  1010. {
  1011. const char *device = qdict_get_str(qdict, "device");
  1012. int64_t size = qdict_get_int(qdict, "size");
  1013. Error *err = NULL;
  1014. qmp_block_resize(true, device, false, NULL, size, &err);
  1015. hmp_handle_error(mon, &err);
  1016. }
  1017. void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
  1018. {
  1019. const char *filename = qdict_get_str(qdict, "target");
  1020. const char *format = qdict_get_try_str(qdict, "format");
  1021. bool reuse = qdict_get_try_bool(qdict, "reuse", false);
  1022. bool full = qdict_get_try_bool(qdict, "full", false);
  1023. Error *err = NULL;
  1024. DriveMirror mirror = {
  1025. .device = (char *)qdict_get_str(qdict, "device"),
  1026. .target = (char *)filename,
  1027. .has_format = !!format,
  1028. .format = (char *)format,
  1029. .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
  1030. .has_mode = true,
  1031. .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
  1032. .unmap = true,
  1033. };
  1034. if (!filename) {
  1035. error_setg(&err, QERR_MISSING_PARAMETER, "target");
  1036. hmp_handle_error(mon, &err);
  1037. return;
  1038. }
  1039. qmp_drive_mirror(&mirror, &err);
  1040. hmp_handle_error(mon, &err);
  1041. }
  1042. void hmp_drive_backup(Monitor *mon, const QDict *qdict)
  1043. {
  1044. const char *device = qdict_get_str(qdict, "device");
  1045. const char *filename = qdict_get_str(qdict, "target");
  1046. const char *format = qdict_get_try_str(qdict, "format");
  1047. bool reuse = qdict_get_try_bool(qdict, "reuse", false);
  1048. bool full = qdict_get_try_bool(qdict, "full", false);
  1049. bool compress = qdict_get_try_bool(qdict, "compress", false);
  1050. Error *err = NULL;
  1051. DriveBackup backup = {
  1052. .device = (char *)device,
  1053. .target = (char *)filename,
  1054. .has_format = !!format,
  1055. .format = (char *)format,
  1056. .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
  1057. .has_mode = true,
  1058. .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
  1059. .has_compress = !!compress,
  1060. .compress = compress,
  1061. };
  1062. if (!filename) {
  1063. error_setg(&err, QERR_MISSING_PARAMETER, "target");
  1064. hmp_handle_error(mon, &err);
  1065. return;
  1066. }
  1067. qmp_drive_backup(&backup, &err);
  1068. hmp_handle_error(mon, &err);
  1069. }
  1070. void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
  1071. {
  1072. const char *device = qdict_get_str(qdict, "device");
  1073. const char *filename = qdict_get_try_str(qdict, "snapshot-file");
  1074. const char *format = qdict_get_try_str(qdict, "format");
  1075. bool reuse = qdict_get_try_bool(qdict, "reuse", false);
  1076. enum NewImageMode mode;
  1077. Error *err = NULL;
  1078. if (!filename) {
  1079. /* In the future, if 'snapshot-file' is not specified, the snapshot
  1080. will be taken internally. Today it's actually required. */
  1081. error_setg(&err, QERR_MISSING_PARAMETER, "snapshot-file");
  1082. hmp_handle_error(mon, &err);
  1083. return;
  1084. }
  1085. mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
  1086. qmp_blockdev_snapshot_sync(true, device, false, NULL,
  1087. filename, false, NULL,
  1088. !!format, format,
  1089. true, mode, &err);
  1090. hmp_handle_error(mon, &err);
  1091. }
  1092. void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict)
  1093. {
  1094. const char *device = qdict_get_str(qdict, "device");
  1095. const char *name = qdict_get_str(qdict, "name");
  1096. Error *err = NULL;
  1097. qmp_blockdev_snapshot_internal_sync(device, name, &err);
  1098. hmp_handle_error(mon, &err);
  1099. }
  1100. void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict)
  1101. {
  1102. const char *device = qdict_get_str(qdict, "device");
  1103. const char *name = qdict_get_str(qdict, "name");
  1104. const char *id = qdict_get_try_str(qdict, "id");
  1105. Error *err = NULL;
  1106. qmp_blockdev_snapshot_delete_internal_sync(device, !!id, id,
  1107. true, name, &err);
  1108. hmp_handle_error(mon, &err);
  1109. }
  1110. void hmp_loadvm(Monitor *mon, const QDict *qdict)
  1111. {
  1112. int saved_vm_running = runstate_is_running();
  1113. const char *name = qdict_get_str(qdict, "name");
  1114. Error *err = NULL;
  1115. vm_stop(RUN_STATE_RESTORE_VM);
  1116. if (load_snapshot(name, &err) == 0 && saved_vm_running) {
  1117. vm_start();
  1118. }
  1119. hmp_handle_error(mon, &err);
  1120. }
  1121. void hmp_savevm(Monitor *mon, const QDict *qdict)
  1122. {
  1123. Error *err = NULL;
  1124. save_snapshot(qdict_get_try_str(qdict, "name"), &err);
  1125. hmp_handle_error(mon, &err);
  1126. }
  1127. void hmp_delvm(Monitor *mon, const QDict *qdict)
  1128. {
  1129. BlockDriverState *bs;
  1130. Error *err = NULL;
  1131. const char *name = qdict_get_str(qdict, "name");
  1132. if (bdrv_all_delete_snapshot(name, &bs, &err) < 0) {
  1133. error_reportf_err(err,
  1134. "Error while deleting snapshot on device '%s': ",
  1135. bdrv_get_device_name(bs));
  1136. }
  1137. }
  1138. void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
  1139. {
  1140. BlockDriverState *bs, *bs1;
  1141. BdrvNextIterator it1;
  1142. QEMUSnapshotInfo *sn_tab, *sn;
  1143. bool no_snapshot = true;
  1144. int nb_sns, i;
  1145. int total;
  1146. int *global_snapshots;
  1147. AioContext *aio_context;
  1148. typedef struct SnapshotEntry {
  1149. QEMUSnapshotInfo sn;
  1150. QTAILQ_ENTRY(SnapshotEntry) next;
  1151. } SnapshotEntry;
  1152. typedef struct ImageEntry {
  1153. const char *imagename;
  1154. QTAILQ_ENTRY(ImageEntry) next;
  1155. QTAILQ_HEAD(, SnapshotEntry) snapshots;
  1156. } ImageEntry;
  1157. QTAILQ_HEAD(, ImageEntry) image_list =
  1158. QTAILQ_HEAD_INITIALIZER(image_list);
  1159. ImageEntry *image_entry, *next_ie;
  1160. SnapshotEntry *snapshot_entry;
  1161. bs = bdrv_all_find_vmstate_bs();
  1162. if (!bs) {
  1163. monitor_printf(mon, "No available block device supports snapshots\n");
  1164. return;
  1165. }
  1166. aio_context = bdrv_get_aio_context(bs);
  1167. aio_context_acquire(aio_context);
  1168. nb_sns = bdrv_snapshot_list(bs, &sn_tab);
  1169. aio_context_release(aio_context);
  1170. if (nb_sns < 0) {
  1171. monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
  1172. return;
  1173. }
  1174. for (bs1 = bdrv_first(&it1); bs1; bs1 = bdrv_next(&it1)) {
  1175. int bs1_nb_sns = 0;
  1176. ImageEntry *ie;
  1177. SnapshotEntry *se;
  1178. AioContext *ctx = bdrv_get_aio_context(bs1);
  1179. aio_context_acquire(ctx);
  1180. if (bdrv_can_snapshot(bs1)) {
  1181. sn = NULL;
  1182. bs1_nb_sns = bdrv_snapshot_list(bs1, &sn);
  1183. if (bs1_nb_sns > 0) {
  1184. no_snapshot = false;
  1185. ie = g_new0(ImageEntry, 1);
  1186. ie->imagename = bdrv_get_device_name(bs1);
  1187. QTAILQ_INIT(&ie->snapshots);
  1188. QTAILQ_INSERT_TAIL(&image_list, ie, next);
  1189. for (i = 0; i < bs1_nb_sns; i++) {
  1190. se = g_new0(SnapshotEntry, 1);
  1191. se->sn = sn[i];
  1192. QTAILQ_INSERT_TAIL(&ie->snapshots, se, next);
  1193. }
  1194. }
  1195. g_free(sn);
  1196. }
  1197. aio_context_release(ctx);
  1198. }
  1199. if (no_snapshot) {
  1200. monitor_printf(mon, "There is no snapshot available.\n");
  1201. return;
  1202. }
  1203. global_snapshots = g_new0(int, nb_sns);
  1204. total = 0;
  1205. for (i = 0; i < nb_sns; i++) {
  1206. SnapshotEntry *next_sn;
  1207. if (bdrv_all_find_snapshot(sn_tab[i].name, &bs1) == 0) {
  1208. global_snapshots[total] = i;
  1209. total++;
  1210. QTAILQ_FOREACH(image_entry, &image_list, next) {
  1211. QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots,
  1212. next, next_sn) {
  1213. if (!strcmp(sn_tab[i].name, snapshot_entry->sn.name)) {
  1214. QTAILQ_REMOVE(&image_entry->snapshots, snapshot_entry,
  1215. next);
  1216. g_free(snapshot_entry);
  1217. }
  1218. }
  1219. }
  1220. }
  1221. }
  1222. monitor_printf(mon, "List of snapshots present on all disks:\n");
  1223. if (total > 0) {
  1224. bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
  1225. monitor_printf(mon, "\n");
  1226. for (i = 0; i < total; i++) {
  1227. sn = &sn_tab[global_snapshots[i]];
  1228. /* The ID is not guaranteed to be the same on all images, so
  1229. * overwrite it.
  1230. */
  1231. pstrcpy(sn->id_str, sizeof(sn->id_str), "--");
  1232. bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn);
  1233. monitor_printf(mon, "\n");
  1234. }
  1235. } else {
  1236. monitor_printf(mon, "None\n");
  1237. }
  1238. QTAILQ_FOREACH(image_entry, &image_list, next) {
  1239. if (QTAILQ_EMPTY(&image_entry->snapshots)) {
  1240. continue;
  1241. }
  1242. monitor_printf(mon,
  1243. "\nList of partial (non-loadable) snapshots on '%s':\n",
  1244. image_entry->imagename);
  1245. bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
  1246. monitor_printf(mon, "\n");
  1247. QTAILQ_FOREACH(snapshot_entry, &image_entry->snapshots, next) {
  1248. bdrv_snapshot_dump((fprintf_function)monitor_printf, mon,
  1249. &snapshot_entry->sn);
  1250. monitor_printf(mon, "\n");
  1251. }
  1252. }
  1253. QTAILQ_FOREACH_SAFE(image_entry, &image_list, next, next_ie) {
  1254. SnapshotEntry *next_sn;
  1255. QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots, next,
  1256. next_sn) {
  1257. g_free(snapshot_entry);
  1258. }
  1259. g_free(image_entry);
  1260. }
  1261. g_free(sn_tab);
  1262. g_free(global_snapshots);
  1263. }
  1264. void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
  1265. {
  1266. qmp_migrate_cancel(NULL);
  1267. }
  1268. void hmp_migrate_continue(Monitor *mon, const QDict *qdict)
  1269. {
  1270. Error *err = NULL;
  1271. const char *state = qdict_get_str(qdict, "state");
  1272. int val = qapi_enum_parse(&MigrationStatus_lookup, state, -1, &err);
  1273. if (val >= 0) {
  1274. qmp_migrate_continue(val, &err);
  1275. }
  1276. hmp_handle_error(mon, &err);
  1277. }
  1278. void hmp_migrate_incoming(Monitor *mon, const QDict *qdict)
  1279. {
  1280. Error *err = NULL;
  1281. const char *uri = qdict_get_str(qdict, "uri");
  1282. qmp_migrate_incoming(uri, &err);
  1283. hmp_handle_error(mon, &err);
  1284. }
  1285. /* Kept for backwards compatibility */
  1286. void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
  1287. {
  1288. double value = qdict_get_double(qdict, "value");
  1289. qmp_migrate_set_downtime(value, NULL);
  1290. }
  1291. void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
  1292. {
  1293. int64_t value = qdict_get_int(qdict, "value");
  1294. Error *err = NULL;
  1295. qmp_migrate_set_cache_size(value, &err);
  1296. hmp_handle_error(mon, &err);
  1297. }
  1298. /* Kept for backwards compatibility */
  1299. void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
  1300. {
  1301. int64_t value = qdict_get_int(qdict, "value");
  1302. qmp_migrate_set_speed(value, NULL);
  1303. }
  1304. void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
  1305. {
  1306. const char *cap = qdict_get_str(qdict, "capability");
  1307. bool state = qdict_get_bool(qdict, "state");
  1308. Error *err = NULL;
  1309. MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
  1310. int val;
  1311. val = qapi_enum_parse(&MigrationCapability_lookup, cap, -1, &err);
  1312. if (val < 0) {
  1313. goto end;
  1314. }
  1315. caps->value = g_malloc0(sizeof(*caps->value));
  1316. caps->value->capability = val;
  1317. caps->value->state = state;
  1318. caps->next = NULL;
  1319. qmp_migrate_set_capabilities(caps, &err);
  1320. end:
  1321. qapi_free_MigrationCapabilityStatusList(caps);
  1322. hmp_handle_error(mon, &err);
  1323. }
  1324. void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
  1325. {
  1326. const char *param = qdict_get_str(qdict, "parameter");
  1327. const char *valuestr = qdict_get_str(qdict, "value");
  1328. Visitor *v = string_input_visitor_new(valuestr);
  1329. MigrateSetParameters *p = g_new0(MigrateSetParameters, 1);
  1330. uint64_t valuebw = 0;
  1331. uint64_t cache_size;
  1332. Error *err = NULL;
  1333. int val, ret;
  1334. val = qapi_enum_parse(&MigrationParameter_lookup, param, -1, &err);
  1335. if (val < 0) {
  1336. goto cleanup;
  1337. }
  1338. switch (val) {
  1339. case MIGRATION_PARAMETER_COMPRESS_LEVEL:
  1340. p->has_compress_level = true;
  1341. visit_type_int(v, param, &p->compress_level, &err);
  1342. break;
  1343. case MIGRATION_PARAMETER_COMPRESS_THREADS:
  1344. p->has_compress_threads = true;
  1345. visit_type_int(v, param, &p->compress_threads, &err);
  1346. break;
  1347. case MIGRATION_PARAMETER_DECOMPRESS_THREADS:
  1348. p->has_decompress_threads = true;
  1349. visit_type_int(v, param, &p->decompress_threads, &err);
  1350. break;
  1351. case MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL:
  1352. p->has_cpu_throttle_initial = true;
  1353. visit_type_int(v, param, &p->cpu_throttle_initial, &err);
  1354. break;
  1355. case MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT:
  1356. p->has_cpu_throttle_increment = true;
  1357. visit_type_int(v, param, &p->cpu_throttle_increment, &err);
  1358. break;
  1359. case MIGRATION_PARAMETER_TLS_CREDS:
  1360. p->has_tls_creds = true;
  1361. p->tls_creds = g_new0(StrOrNull, 1);
  1362. p->tls_creds->type = QTYPE_QSTRING;
  1363. visit_type_str(v, param, &p->tls_creds->u.s, &err);
  1364. break;
  1365. case MIGRATION_PARAMETER_TLS_HOSTNAME:
  1366. p->has_tls_hostname = true;
  1367. p->tls_hostname = g_new0(StrOrNull, 1);
  1368. p->tls_hostname->type = QTYPE_QSTRING;
  1369. visit_type_str(v, param, &p->tls_hostname->u.s, &err);
  1370. break;
  1371. case MIGRATION_PARAMETER_MAX_BANDWIDTH:
  1372. p->has_max_bandwidth = true;
  1373. /*
  1374. * Can't use visit_type_size() here, because it
  1375. * defaults to Bytes rather than Mebibytes.
  1376. */
  1377. ret = qemu_strtosz_MiB(valuestr, NULL, &valuebw);
  1378. if (ret < 0 || valuebw > INT64_MAX
  1379. || (size_t)valuebw != valuebw) {
  1380. error_setg(&err, "Invalid size %s", valuestr);
  1381. break;
  1382. }
  1383. p->max_bandwidth = valuebw;
  1384. break;
  1385. case MIGRATION_PARAMETER_DOWNTIME_LIMIT:
  1386. p->has_downtime_limit = true;
  1387. visit_type_int(v, param, &p->downtime_limit, &err);
  1388. break;
  1389. case MIGRATION_PARAMETER_X_CHECKPOINT_DELAY:
  1390. p->has_x_checkpoint_delay = true;
  1391. visit_type_int(v, param, &p->x_checkpoint_delay, &err);
  1392. break;
  1393. case MIGRATION_PARAMETER_BLOCK_INCREMENTAL:
  1394. p->has_block_incremental = true;
  1395. visit_type_bool(v, param, &p->block_incremental, &err);
  1396. break;
  1397. case MIGRATION_PARAMETER_X_MULTIFD_CHANNELS:
  1398. p->has_x_multifd_channels = true;
  1399. visit_type_int(v, param, &p->x_multifd_channels, &err);
  1400. break;
  1401. case MIGRATION_PARAMETER_X_MULTIFD_PAGE_COUNT:
  1402. p->has_x_multifd_page_count = true;
  1403. visit_type_int(v, param, &p->x_multifd_page_count, &err);
  1404. break;
  1405. case MIGRATION_PARAMETER_XBZRLE_CACHE_SIZE:
  1406. p->has_xbzrle_cache_size = true;
  1407. visit_type_size(v, param, &cache_size, &err);
  1408. if (err || cache_size > INT64_MAX
  1409. || (size_t)cache_size != cache_size) {
  1410. error_setg(&err, "Invalid size %s", valuestr);
  1411. break;
  1412. }
  1413. p->xbzrle_cache_size = cache_size;
  1414. break;
  1415. default:
  1416. assert(0);
  1417. }
  1418. if (err) {
  1419. goto cleanup;
  1420. }
  1421. qmp_migrate_set_parameters(p, &err);
  1422. cleanup:
  1423. qapi_free_MigrateSetParameters(p);
  1424. visit_free(v);
  1425. hmp_handle_error(mon, &err);
  1426. }
  1427. void hmp_client_migrate_info(Monitor *mon, const QDict *qdict)
  1428. {
  1429. Error *err = NULL;
  1430. const char *protocol = qdict_get_str(qdict, "protocol");
  1431. const char *hostname = qdict_get_str(qdict, "hostname");
  1432. bool has_port = qdict_haskey(qdict, "port");
  1433. int port = qdict_get_try_int(qdict, "port", -1);
  1434. bool has_tls_port = qdict_haskey(qdict, "tls-port");
  1435. int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
  1436. const char *cert_subject = qdict_get_try_str(qdict, "cert-subject");
  1437. qmp_client_migrate_info(protocol, hostname,
  1438. has_port, port, has_tls_port, tls_port,
  1439. !!cert_subject, cert_subject, &err);
  1440. hmp_handle_error(mon, &err);
  1441. }
  1442. void hmp_migrate_start_postcopy(Monitor *mon, const QDict *qdict)
  1443. {
  1444. Error *err = NULL;
  1445. qmp_migrate_start_postcopy(&err);
  1446. hmp_handle_error(mon, &err);
  1447. }
  1448. void hmp_x_colo_lost_heartbeat(Monitor *mon, const QDict *qdict)
  1449. {
  1450. Error *err = NULL;
  1451. qmp_x_colo_lost_heartbeat(&err);
  1452. hmp_handle_error(mon, &err);
  1453. }
  1454. void hmp_set_password(Monitor *mon, const QDict *qdict)
  1455. {
  1456. const char *protocol = qdict_get_str(qdict, "protocol");
  1457. const char *password = qdict_get_str(qdict, "password");
  1458. const char *connected = qdict_get_try_str(qdict, "connected");
  1459. Error *err = NULL;
  1460. qmp_set_password(protocol, password, !!connected, connected, &err);
  1461. hmp_handle_error(mon, &err);
  1462. }
  1463. void hmp_expire_password(Monitor *mon, const QDict *qdict)
  1464. {
  1465. const char *protocol = qdict_get_str(qdict, "protocol");
  1466. const char *whenstr = qdict_get_str(qdict, "time");
  1467. Error *err = NULL;
  1468. qmp_expire_password(protocol, whenstr, &err);
  1469. hmp_handle_error(mon, &err);
  1470. }
  1471. void hmp_eject(Monitor *mon, const QDict *qdict)
  1472. {
  1473. bool force = qdict_get_try_bool(qdict, "force", false);
  1474. const char *device = qdict_get_str(qdict, "device");
  1475. Error *err = NULL;
  1476. qmp_eject(true, device, false, NULL, true, force, &err);
  1477. hmp_handle_error(mon, &err);
  1478. }
  1479. static void hmp_change_read_arg(void *opaque, const char *password,
  1480. void *readline_opaque)
  1481. {
  1482. qmp_change_vnc_password(password, NULL);
  1483. monitor_read_command(opaque, 1);
  1484. }
  1485. void hmp_change(Monitor *mon, const QDict *qdict)
  1486. {
  1487. const char *device = qdict_get_str(qdict, "device");
  1488. const char *target = qdict_get_str(qdict, "target");
  1489. const char *arg = qdict_get_try_str(qdict, "arg");
  1490. const char *read_only = qdict_get_try_str(qdict, "read-only-mode");
  1491. BlockdevChangeReadOnlyMode read_only_mode = 0;
  1492. Error *err = NULL;
  1493. if (strcmp(device, "vnc") == 0) {
  1494. if (read_only) {
  1495. monitor_printf(mon,
  1496. "Parameter 'read-only-mode' is invalid for VNC\n");
  1497. return;
  1498. }
  1499. if (strcmp(target, "passwd") == 0 ||
  1500. strcmp(target, "password") == 0) {
  1501. if (!arg) {
  1502. monitor_read_password(mon, hmp_change_read_arg, NULL);
  1503. return;
  1504. }
  1505. }
  1506. qmp_change("vnc", target, !!arg, arg, &err);
  1507. } else {
  1508. if (read_only) {
  1509. read_only_mode =
  1510. qapi_enum_parse(&BlockdevChangeReadOnlyMode_lookup,
  1511. read_only,
  1512. BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN, &err);
  1513. if (err) {
  1514. hmp_handle_error(mon, &err);
  1515. return;
  1516. }
  1517. }
  1518. qmp_blockdev_change_medium(true, device, false, NULL, target,
  1519. !!arg, arg, !!read_only, read_only_mode,
  1520. &err);
  1521. }
  1522. hmp_handle_error(mon, &err);
  1523. }
  1524. void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
  1525. {
  1526. Error *err = NULL;
  1527. BlockIOThrottle throttle = {
  1528. .has_device = true,
  1529. .device = (char *) qdict_get_str(qdict, "device"),
  1530. .bps = qdict_get_int(qdict, "bps"),
  1531. .bps_rd = qdict_get_int(qdict, "bps_rd"),
  1532. .bps_wr = qdict_get_int(qdict, "bps_wr"),
  1533. .iops = qdict_get_int(qdict, "iops"),
  1534. .iops_rd = qdict_get_int(qdict, "iops_rd"),
  1535. .iops_wr = qdict_get_int(qdict, "iops_wr"),
  1536. };
  1537. qmp_block_set_io_throttle(&throttle, &err);
  1538. hmp_handle_error(mon, &err);
  1539. }
  1540. void hmp_block_stream(Monitor *mon, const QDict *qdict)
  1541. {
  1542. Error *error = NULL;
  1543. const char *device = qdict_get_str(qdict, "device");
  1544. const char *base = qdict_get_try_str(qdict, "base");
  1545. int64_t speed = qdict_get_try_int(qdict, "speed", 0);
  1546. qmp_block_stream(true, device, device, base != NULL, base, false, NULL,
  1547. false, NULL, qdict_haskey(qdict, "speed"), speed,
  1548. true, BLOCKDEV_ON_ERROR_REPORT, &error);
  1549. hmp_handle_error(mon, &error);
  1550. }
  1551. void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
  1552. {
  1553. Error *error = NULL;
  1554. const char *device = qdict_get_str(qdict, "device");
  1555. int64_t value = qdict_get_int(qdict, "speed");
  1556. qmp_block_job_set_speed(device, value, &error);
  1557. hmp_handle_error(mon, &error);
  1558. }
  1559. void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
  1560. {
  1561. Error *error = NULL;
  1562. const char *device = qdict_get_str(qdict, "device");
  1563. bool force = qdict_get_try_bool(qdict, "force", false);
  1564. qmp_block_job_cancel(device, true, force, &error);
  1565. hmp_handle_error(mon, &error);
  1566. }
  1567. void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
  1568. {
  1569. Error *error = NULL;
  1570. const char *device = qdict_get_str(qdict, "device");
  1571. qmp_block_job_pause(device, &error);
  1572. hmp_handle_error(mon, &error);
  1573. }
  1574. void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
  1575. {
  1576. Error *error = NULL;
  1577. const char *device = qdict_get_str(qdict, "device");
  1578. qmp_block_job_resume(device, &error);
  1579. hmp_handle_error(mon, &error);
  1580. }
  1581. void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
  1582. {
  1583. Error *error = NULL;
  1584. const char *device = qdict_get_str(qdict, "device");
  1585. qmp_block_job_complete(device, &error);
  1586. hmp_handle_error(mon, &error);
  1587. }
  1588. typedef struct HMPMigrationStatus
  1589. {
  1590. QEMUTimer *timer;
  1591. Monitor *mon;
  1592. bool is_block_migration;
  1593. } HMPMigrationStatus;
  1594. static void hmp_migrate_status_cb(void *opaque)
  1595. {
  1596. HMPMigrationStatus *status = opaque;
  1597. MigrationInfo *info;
  1598. info = qmp_query_migrate(NULL);
  1599. if (!info->has_status || info->status == MIGRATION_STATUS_ACTIVE ||
  1600. info->status == MIGRATION_STATUS_SETUP) {
  1601. if (info->has_disk) {
  1602. int progress;
  1603. if (info->disk->remaining) {
  1604. progress = info->disk->transferred * 100 / info->disk->total;
  1605. } else {
  1606. progress = 100;
  1607. }
  1608. monitor_printf(status->mon, "Completed %d %%\r", progress);
  1609. monitor_flush(status->mon);
  1610. }
  1611. timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
  1612. } else {
  1613. if (status->is_block_migration) {
  1614. monitor_printf(status->mon, "\n");
  1615. }
  1616. if (info->has_error_desc) {
  1617. error_report("%s", info->error_desc);
  1618. }
  1619. monitor_resume(status->mon);
  1620. timer_del(status->timer);
  1621. g_free(status);
  1622. }
  1623. qapi_free_MigrationInfo(info);
  1624. }
  1625. void hmp_migrate(Monitor *mon, const QDict *qdict)
  1626. {
  1627. bool detach = qdict_get_try_bool(qdict, "detach", false);
  1628. bool blk = qdict_get_try_bool(qdict, "blk", false);
  1629. bool inc = qdict_get_try_bool(qdict, "inc", false);
  1630. const char *uri = qdict_get_str(qdict, "uri");
  1631. Error *err = NULL;
  1632. qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
  1633. if (err) {
  1634. hmp_handle_error(mon, &err);
  1635. return;
  1636. }
  1637. if (!detach) {
  1638. HMPMigrationStatus *status;
  1639. if (monitor_suspend(mon) < 0) {
  1640. monitor_printf(mon, "terminal does not allow synchronous "
  1641. "migration, continuing detached\n");
  1642. return;
  1643. }
  1644. status = g_malloc0(sizeof(*status));
  1645. status->mon = mon;
  1646. status->is_block_migration = blk || inc;
  1647. status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
  1648. status);
  1649. timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
  1650. }
  1651. }
  1652. void hmp_device_add(Monitor *mon, const QDict *qdict)
  1653. {
  1654. Error *err = NULL;
  1655. qmp_device_add((QDict *)qdict, NULL, &err);
  1656. hmp_handle_error(mon, &err);
  1657. }
  1658. void hmp_device_del(Monitor *mon, const QDict *qdict)
  1659. {
  1660. const char *id = qdict_get_str(qdict, "id");
  1661. Error *err = NULL;
  1662. qmp_device_del(id, &err);
  1663. hmp_handle_error(mon, &err);
  1664. }
  1665. void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
  1666. {
  1667. Error *err = NULL;
  1668. bool paging = qdict_get_try_bool(qdict, "paging", false);
  1669. bool zlib = qdict_get_try_bool(qdict, "zlib", false);
  1670. bool lzo = qdict_get_try_bool(qdict, "lzo", false);
  1671. bool snappy = qdict_get_try_bool(qdict, "snappy", false);
  1672. const char *file = qdict_get_str(qdict, "filename");
  1673. bool has_begin = qdict_haskey(qdict, "begin");
  1674. bool has_length = qdict_haskey(qdict, "length");
  1675. bool has_detach = qdict_haskey(qdict, "detach");
  1676. int64_t begin = 0;
  1677. int64_t length = 0;
  1678. bool detach = false;
  1679. enum DumpGuestMemoryFormat dump_format = DUMP_GUEST_MEMORY_FORMAT_ELF;
  1680. char *prot;
  1681. if (zlib + lzo + snappy > 1) {
  1682. error_setg(&err, "only one of '-z|-l|-s' can be set");
  1683. hmp_handle_error(mon, &err);
  1684. return;
  1685. }
  1686. if (zlib) {
  1687. dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
  1688. }
  1689. if (lzo) {
  1690. dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
  1691. }
  1692. if (snappy) {
  1693. dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
  1694. }
  1695. if (has_begin) {
  1696. begin = qdict_get_int(qdict, "begin");
  1697. }
  1698. if (has_length) {
  1699. length = qdict_get_int(qdict, "length");
  1700. }
  1701. if (has_detach) {
  1702. detach = qdict_get_bool(qdict, "detach");
  1703. }
  1704. prot = g_strconcat("file:", file, NULL);
  1705. qmp_dump_guest_memory(paging, prot, true, detach, has_begin, begin,
  1706. has_length, length, true, dump_format, &err);
  1707. hmp_handle_error(mon, &err);
  1708. g_free(prot);
  1709. }
  1710. void hmp_netdev_add(Monitor *mon, const QDict *qdict)
  1711. {
  1712. Error *err = NULL;
  1713. QemuOpts *opts;
  1714. opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
  1715. if (err) {
  1716. goto out;
  1717. }
  1718. netdev_add(opts, &err);
  1719. if (err) {
  1720. qemu_opts_del(opts);
  1721. }
  1722. out:
  1723. hmp_handle_error(mon, &err);
  1724. }
  1725. void hmp_netdev_del(Monitor *mon, const QDict *qdict)
  1726. {
  1727. const char *id = qdict_get_str(qdict, "id");
  1728. Error *err = NULL;
  1729. qmp_netdev_del(id, &err);
  1730. hmp_handle_error(mon, &err);
  1731. }
  1732. void hmp_object_add(Monitor *mon, const QDict *qdict)
  1733. {
  1734. Error *err = NULL;
  1735. QemuOpts *opts;
  1736. Object *obj = NULL;
  1737. opts = qemu_opts_from_qdict(qemu_find_opts("object"), qdict, &err);
  1738. if (err) {
  1739. hmp_handle_error(mon, &err);
  1740. return;
  1741. }
  1742. obj = user_creatable_add_opts(opts, &err);
  1743. qemu_opts_del(opts);
  1744. if (err) {
  1745. hmp_handle_error(mon, &err);
  1746. }
  1747. if (obj) {
  1748. object_unref(obj);
  1749. }
  1750. }
  1751. void hmp_getfd(Monitor *mon, const QDict *qdict)
  1752. {
  1753. const char *fdname = qdict_get_str(qdict, "fdname");
  1754. Error *err = NULL;
  1755. qmp_getfd(fdname, &err);
  1756. hmp_handle_error(mon, &err);
  1757. }
  1758. void hmp_closefd(Monitor *mon, const QDict *qdict)
  1759. {
  1760. const char *fdname = qdict_get_str(qdict, "fdname");
  1761. Error *err = NULL;
  1762. qmp_closefd(fdname, &err);
  1763. hmp_handle_error(mon, &err);
  1764. }
  1765. void hmp_sendkey(Monitor *mon, const QDict *qdict)
  1766. {
  1767. const char *keys = qdict_get_str(qdict, "keys");
  1768. KeyValueList *keylist, *head = NULL, *tmp = NULL;
  1769. int has_hold_time = qdict_haskey(qdict, "hold-time");
  1770. int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
  1771. Error *err = NULL;
  1772. char *separator;
  1773. int keyname_len;
  1774. while (1) {
  1775. separator = strchr(keys, '-');
  1776. keyname_len = separator ? separator - keys : strlen(keys);
  1777. /* Be compatible with old interface, convert user inputted "<" */
  1778. if (keys[0] == '<' && keyname_len == 1) {
  1779. keys = "less";
  1780. keyname_len = 4;
  1781. }
  1782. keylist = g_malloc0(sizeof(*keylist));
  1783. keylist->value = g_malloc0(sizeof(*keylist->value));
  1784. if (!head) {
  1785. head = keylist;
  1786. }
  1787. if (tmp) {
  1788. tmp->next = keylist;
  1789. }
  1790. tmp = keylist;
  1791. if (strstart(keys, "0x", NULL)) {
  1792. char *endp;
  1793. int value = strtoul(keys, &endp, 0);
  1794. assert(endp <= keys + keyname_len);
  1795. if (endp != keys + keyname_len) {
  1796. goto err_out;
  1797. }
  1798. keylist->value->type = KEY_VALUE_KIND_NUMBER;
  1799. keylist->value->u.number.data = value;
  1800. } else {
  1801. int idx = index_from_key(keys, keyname_len);
  1802. if (idx == Q_KEY_CODE__MAX) {
  1803. goto err_out;
  1804. }
  1805. keylist->value->type = KEY_VALUE_KIND_QCODE;
  1806. keylist->value->u.qcode.data = idx;
  1807. }
  1808. if (!separator) {
  1809. break;
  1810. }
  1811. keys = separator + 1;
  1812. }
  1813. qmp_send_key(head, has_hold_time, hold_time, &err);
  1814. hmp_handle_error(mon, &err);
  1815. out:
  1816. qapi_free_KeyValueList(head);
  1817. return;
  1818. err_out:
  1819. monitor_printf(mon, "invalid parameter: %.*s\n", keyname_len, keys);
  1820. goto out;
  1821. }
  1822. void hmp_screendump(Monitor *mon, const QDict *qdict)
  1823. {
  1824. const char *filename = qdict_get_str(qdict, "filename");
  1825. const char *id = qdict_get_try_str(qdict, "device");
  1826. int64_t head = qdict_get_try_int(qdict, "head", 0);
  1827. Error *err = NULL;
  1828. qmp_screendump(filename, id != NULL, id, id != NULL, head, &err);
  1829. hmp_handle_error(mon, &err);
  1830. }
  1831. void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
  1832. {
  1833. const char *uri = qdict_get_str(qdict, "uri");
  1834. bool writable = qdict_get_try_bool(qdict, "writable", false);
  1835. bool all = qdict_get_try_bool(qdict, "all", false);
  1836. Error *local_err = NULL;
  1837. BlockInfoList *block_list, *info;
  1838. SocketAddress *addr;
  1839. if (writable && !all) {
  1840. error_setg(&local_err, "-w only valid together with -a");
  1841. goto exit;
  1842. }
  1843. /* First check if the address is valid and start the server. */
  1844. addr = socket_parse(uri, &local_err);
  1845. if (local_err != NULL) {
  1846. goto exit;
  1847. }
  1848. nbd_server_start(addr, NULL, &local_err);
  1849. qapi_free_SocketAddress(addr);
  1850. if (local_err != NULL) {
  1851. goto exit;
  1852. }
  1853. if (!all) {
  1854. return;
  1855. }
  1856. /* Then try adding all block devices. If one fails, close all and
  1857. * exit.
  1858. */
  1859. block_list = qmp_query_block(NULL);
  1860. for (info = block_list; info; info = info->next) {
  1861. if (!info->value->has_inserted) {
  1862. continue;
  1863. }
  1864. qmp_nbd_server_add(info->value->device, false, NULL,
  1865. true, writable, &local_err);
  1866. if (local_err != NULL) {
  1867. qmp_nbd_server_stop(NULL);
  1868. break;
  1869. }
  1870. }
  1871. qapi_free_BlockInfoList(block_list);
  1872. exit:
  1873. hmp_handle_error(mon, &local_err);
  1874. }
  1875. void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
  1876. {
  1877. const char *device = qdict_get_str(qdict, "device");
  1878. const char *name = qdict_get_try_str(qdict, "name");
  1879. bool writable = qdict_get_try_bool(qdict, "writable", false);
  1880. Error *local_err = NULL;
  1881. qmp_nbd_server_add(device, !!name, name, true, writable, &local_err);
  1882. hmp_handle_error(mon, &local_err);
  1883. }
  1884. void hmp_nbd_server_remove(Monitor *mon, const QDict *qdict)
  1885. {
  1886. const char *name = qdict_get_str(qdict, "name");
  1887. bool force = qdict_get_try_bool(qdict, "force", false);
  1888. Error *err = NULL;
  1889. /* Rely on NBD_SERVER_REMOVE_MODE_SAFE being the default */
  1890. qmp_nbd_server_remove(name, force, NBD_SERVER_REMOVE_MODE_HARD, &err);
  1891. hmp_handle_error(mon, &err);
  1892. }
  1893. void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
  1894. {
  1895. Error *err = NULL;
  1896. qmp_nbd_server_stop(&err);
  1897. hmp_handle_error(mon, &err);
  1898. }
  1899. void hmp_cpu_add(Monitor *mon, const QDict *qdict)
  1900. {
  1901. int cpuid;
  1902. Error *err = NULL;
  1903. cpuid = qdict_get_int(qdict, "id");
  1904. qmp_cpu_add(cpuid, &err);
  1905. hmp_handle_error(mon, &err);
  1906. }
  1907. void hmp_chardev_add(Monitor *mon, const QDict *qdict)
  1908. {
  1909. const char *args = qdict_get_str(qdict, "args");
  1910. Error *err = NULL;
  1911. QemuOpts *opts;
  1912. opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), args, true);
  1913. if (opts == NULL) {
  1914. error_setg(&err, "Parsing chardev args failed");
  1915. } else {
  1916. qemu_chr_new_from_opts(opts, &err);
  1917. qemu_opts_del(opts);
  1918. }
  1919. hmp_handle_error(mon, &err);
  1920. }
  1921. void hmp_chardev_change(Monitor *mon, const QDict *qdict)
  1922. {
  1923. const char *args = qdict_get_str(qdict, "args");
  1924. const char *id;
  1925. Error *err = NULL;
  1926. ChardevBackend *backend = NULL;
  1927. ChardevReturn *ret = NULL;
  1928. QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), args,
  1929. true);
  1930. if (!opts) {
  1931. error_setg(&err, "Parsing chardev args failed");
  1932. goto end;
  1933. }
  1934. id = qdict_get_str(qdict, "id");
  1935. if (qemu_opts_id(opts)) {
  1936. error_setg(&err, "Unexpected 'id' parameter");
  1937. goto end;
  1938. }
  1939. backend = qemu_chr_parse_opts(opts, &err);
  1940. if (!backend) {
  1941. goto end;
  1942. }
  1943. ret = qmp_chardev_change(id, backend, &err);
  1944. end:
  1945. qapi_free_ChardevReturn(ret);
  1946. qapi_free_ChardevBackend(backend);
  1947. qemu_opts_del(opts);
  1948. hmp_handle_error(mon, &err);
  1949. }
  1950. void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
  1951. {
  1952. Error *local_err = NULL;
  1953. qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
  1954. hmp_handle_error(mon, &local_err);
  1955. }
  1956. void hmp_chardev_send_break(Monitor *mon, const QDict *qdict)
  1957. {
  1958. Error *local_err = NULL;
  1959. qmp_chardev_send_break(qdict_get_str(qdict, "id"), &local_err);
  1960. hmp_handle_error(mon, &local_err);
  1961. }
  1962. void hmp_qemu_io(Monitor *mon, const QDict *qdict)
  1963. {
  1964. BlockBackend *blk;
  1965. BlockBackend *local_blk = NULL;
  1966. const char* device = qdict_get_str(qdict, "device");
  1967. const char* command = qdict_get_str(qdict, "command");
  1968. Error *err = NULL;
  1969. int ret;
  1970. blk = blk_by_name(device);
  1971. if (!blk) {
  1972. BlockDriverState *bs = bdrv_lookup_bs(NULL, device, &err);
  1973. if (bs) {
  1974. blk = local_blk = blk_new(0, BLK_PERM_ALL);
  1975. ret = blk_insert_bs(blk, bs, &err);
  1976. if (ret < 0) {
  1977. goto fail;
  1978. }
  1979. } else {
  1980. goto fail;
  1981. }
  1982. }
  1983. /*
  1984. * Notably absent: Proper permission management. This is sad, but it seems
  1985. * almost impossible to achieve without changing the semantics and thereby
  1986. * limiting the use cases of the qemu-io HMP command.
  1987. *
  1988. * In an ideal world we would unconditionally create a new BlockBackend for
  1989. * qemuio_command(), but we have commands like 'reopen' and want them to
  1990. * take effect on the exact BlockBackend whose name the user passed instead
  1991. * of just on a temporary copy of it.
  1992. *
  1993. * Another problem is that deleting the temporary BlockBackend involves
  1994. * draining all requests on it first, but some qemu-iotests cases want to
  1995. * issue multiple aio_read/write requests and expect them to complete in
  1996. * the background while the monitor has already returned.
  1997. *
  1998. * This is also what prevents us from saving the original permissions and
  1999. * restoring them later: We can't revoke permissions until all requests
  2000. * have completed, and we don't know when that is nor can we really let
  2001. * anything else run before we have revoken them to avoid race conditions.
  2002. *
  2003. * What happens now is that command() in qemu-io-cmds.c can extend the
  2004. * permissions if necessary for the qemu-io command. And they simply stay
  2005. * extended, possibly resulting in a read-only guest device keeping write
  2006. * permissions. Ugly, but it appears to be the lesser evil.
  2007. */
  2008. qemuio_command(blk, command);
  2009. fail:
  2010. blk_unref(local_blk);
  2011. hmp_handle_error(mon, &err);
  2012. }
  2013. void hmp_object_del(Monitor *mon, const QDict *qdict)
  2014. {
  2015. const char *id = qdict_get_str(qdict, "id");
  2016. Error *err = NULL;
  2017. user_creatable_del(id, &err);
  2018. hmp_handle_error(mon, &err);
  2019. }
  2020. void hmp_info_memdev(Monitor *mon, const QDict *qdict)
  2021. {
  2022. Error *err = NULL;
  2023. MemdevList *memdev_list = qmp_query_memdev(&err);
  2024. MemdevList *m = memdev_list;
  2025. Visitor *v;
  2026. char *str;
  2027. while (m) {
  2028. v = string_output_visitor_new(false, &str);
  2029. visit_type_uint16List(v, NULL, &m->value->host_nodes, NULL);
  2030. monitor_printf(mon, "memory backend: %s\n", m->value->id);
  2031. monitor_printf(mon, " size: %" PRId64 "\n", m->value->size);
  2032. monitor_printf(mon, " merge: %s\n",
  2033. m->value->merge ? "true" : "false");
  2034. monitor_printf(mon, " dump: %s\n",
  2035. m->value->dump ? "true" : "false");
  2036. monitor_printf(mon, " prealloc: %s\n",
  2037. m->value->prealloc ? "true" : "false");
  2038. monitor_printf(mon, " policy: %s\n",
  2039. HostMemPolicy_str(m->value->policy));
  2040. visit_complete(v, &str);
  2041. monitor_printf(mon, " host nodes: %s\n", str);
  2042. g_free(str);
  2043. visit_free(v);
  2044. m = m->next;
  2045. }
  2046. monitor_printf(mon, "\n");
  2047. qapi_free_MemdevList(memdev_list);
  2048. hmp_handle_error(mon, &err);
  2049. }
  2050. void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
  2051. {
  2052. Error *err = NULL;
  2053. MemoryDeviceInfoList *info_list = qmp_query_memory_devices(&err);
  2054. MemoryDeviceInfoList *info;
  2055. MemoryDeviceInfo *value;
  2056. PCDIMMDeviceInfo *di;
  2057. for (info = info_list; info; info = info->next) {
  2058. value = info->value;
  2059. if (value) {
  2060. switch (value->type) {
  2061. case MEMORY_DEVICE_INFO_KIND_DIMM:
  2062. di = value->u.dimm.data;
  2063. break;
  2064. case MEMORY_DEVICE_INFO_KIND_NVDIMM:
  2065. di = value->u.nvdimm.data;
  2066. break;
  2067. default:
  2068. di = NULL;
  2069. break;
  2070. }
  2071. if (di) {
  2072. monitor_printf(mon, "Memory device [%s]: \"%s\"\n",
  2073. MemoryDeviceInfoKind_str(value->type),
  2074. di->id ? di->id : "");
  2075. monitor_printf(mon, " addr: 0x%" PRIx64 "\n", di->addr);
  2076. monitor_printf(mon, " slot: %" PRId64 "\n", di->slot);
  2077. monitor_printf(mon, " node: %" PRId64 "\n", di->node);
  2078. monitor_printf(mon, " size: %" PRIu64 "\n", di->size);
  2079. monitor_printf(mon, " memdev: %s\n", di->memdev);
  2080. monitor_printf(mon, " hotplugged: %s\n",
  2081. di->hotplugged ? "true" : "false");
  2082. monitor_printf(mon, " hotpluggable: %s\n",
  2083. di->hotpluggable ? "true" : "false");
  2084. }
  2085. }
  2086. }
  2087. qapi_free_MemoryDeviceInfoList(info_list);
  2088. hmp_handle_error(mon, &err);
  2089. }
  2090. void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
  2091. {
  2092. IOThreadInfoList *info_list = qmp_query_iothreads(NULL);
  2093. IOThreadInfoList *info;
  2094. IOThreadInfo *value;
  2095. for (info = info_list; info; info = info->next) {
  2096. value = info->value;
  2097. monitor_printf(mon, "%s:\n", value->id);
  2098. monitor_printf(mon, " thread_id=%" PRId64 "\n", value->thread_id);
  2099. monitor_printf(mon, " poll-max-ns=%" PRId64 "\n", value->poll_max_ns);
  2100. monitor_printf(mon, " poll-grow=%" PRId64 "\n", value->poll_grow);
  2101. monitor_printf(mon, " poll-shrink=%" PRId64 "\n", value->poll_shrink);
  2102. }
  2103. qapi_free_IOThreadInfoList(info_list);
  2104. }
  2105. void hmp_qom_list(Monitor *mon, const QDict *qdict)
  2106. {
  2107. const char *path = qdict_get_try_str(qdict, "path");
  2108. ObjectPropertyInfoList *list;
  2109. Error *err = NULL;
  2110. if (path == NULL) {
  2111. monitor_printf(mon, "/\n");
  2112. return;
  2113. }
  2114. list = qmp_qom_list(path, &err);
  2115. if (err == NULL) {
  2116. ObjectPropertyInfoList *start = list;
  2117. while (list != NULL) {
  2118. ObjectPropertyInfo *value = list->value;
  2119. monitor_printf(mon, "%s (%s)\n",
  2120. value->name, value->type);
  2121. list = list->next;
  2122. }
  2123. qapi_free_ObjectPropertyInfoList(start);
  2124. }
  2125. hmp_handle_error(mon, &err);
  2126. }
  2127. void hmp_qom_set(Monitor *mon, const QDict *qdict)
  2128. {
  2129. const char *path = qdict_get_str(qdict, "path");
  2130. const char *property = qdict_get_str(qdict, "property");
  2131. const char *value = qdict_get_str(qdict, "value");
  2132. Error *err = NULL;
  2133. bool ambiguous = false;
  2134. Object *obj;
  2135. obj = object_resolve_path(path, &ambiguous);
  2136. if (obj == NULL) {
  2137. error_set(&err, ERROR_CLASS_DEVICE_NOT_FOUND,
  2138. "Device '%s' not found", path);
  2139. } else {
  2140. if (ambiguous) {
  2141. monitor_printf(mon, "Warning: Path '%s' is ambiguous\n", path);
  2142. }
  2143. object_property_parse(obj, value, property, &err);
  2144. }
  2145. hmp_handle_error(mon, &err);
  2146. }
  2147. void hmp_rocker(Monitor *mon, const QDict *qdict)
  2148. {
  2149. const char *name = qdict_get_str(qdict, "name");
  2150. RockerSwitch *rocker;
  2151. Error *err = NULL;
  2152. rocker = qmp_query_rocker(name, &err);
  2153. if (err != NULL) {
  2154. hmp_handle_error(mon, &err);
  2155. return;
  2156. }
  2157. monitor_printf(mon, "name: %s\n", rocker->name);
  2158. monitor_printf(mon, "id: 0x%" PRIx64 "\n", rocker->id);
  2159. monitor_printf(mon, "ports: %d\n", rocker->ports);
  2160. qapi_free_RockerSwitch(rocker);
  2161. }
  2162. void hmp_rocker_ports(Monitor *mon, const QDict *qdict)
  2163. {
  2164. RockerPortList *list, *port;
  2165. const char *name = qdict_get_str(qdict, "name");
  2166. Error *err = NULL;
  2167. list = qmp_query_rocker_ports(name, &err);
  2168. if (err != NULL) {
  2169. hmp_handle_error(mon, &err);
  2170. return;
  2171. }
  2172. monitor_printf(mon, " ena/ speed/ auto\n");
  2173. monitor_printf(mon, " port link duplex neg?\n");
  2174. for (port = list; port; port = port->next) {
  2175. monitor_printf(mon, "%10s %-4s %-3s %2s %-3s\n",
  2176. port->value->name,
  2177. port->value->enabled ? port->value->link_up ?
  2178. "up" : "down" : "!ena",
  2179. port->value->speed == 10000 ? "10G" : "??",
  2180. port->value->duplex ? "FD" : "HD",
  2181. port->value->autoneg ? "Yes" : "No");
  2182. }
  2183. qapi_free_RockerPortList(list);
  2184. }
  2185. void hmp_rocker_of_dpa_flows(Monitor *mon, const QDict *qdict)
  2186. {
  2187. RockerOfDpaFlowList *list, *info;
  2188. const char *name = qdict_get_str(qdict, "name");
  2189. uint32_t tbl_id = qdict_get_try_int(qdict, "tbl_id", -1);
  2190. Error *err = NULL;
  2191. list = qmp_query_rocker_of_dpa_flows(name, tbl_id != -1, tbl_id, &err);
  2192. if (err != NULL) {
  2193. hmp_handle_error(mon, &err);
  2194. return;
  2195. }
  2196. monitor_printf(mon, "prio tbl hits key(mask) --> actions\n");
  2197. for (info = list; info; info = info->next) {
  2198. RockerOfDpaFlow *flow = info->value;
  2199. RockerOfDpaFlowKey *key = flow->key;
  2200. RockerOfDpaFlowMask *mask = flow->mask;
  2201. RockerOfDpaFlowAction *action = flow->action;
  2202. if (flow->hits) {
  2203. monitor_printf(mon, "%-4d %-3d %-4" PRIu64,
  2204. key->priority, key->tbl_id, flow->hits);
  2205. } else {
  2206. monitor_printf(mon, "%-4d %-3d ",
  2207. key->priority, key->tbl_id);
  2208. }
  2209. if (key->has_in_pport) {
  2210. monitor_printf(mon, " pport %d", key->in_pport);
  2211. if (mask->has_in_pport) {
  2212. monitor_printf(mon, "(0x%x)", mask->in_pport);
  2213. }
  2214. }
  2215. if (key->has_vlan_id) {
  2216. monitor_printf(mon, " vlan %d",
  2217. key->vlan_id & VLAN_VID_MASK);
  2218. if (mask->has_vlan_id) {
  2219. monitor_printf(mon, "(0x%x)", mask->vlan_id);
  2220. }
  2221. }
  2222. if (key->has_tunnel_id) {
  2223. monitor_printf(mon, " tunnel %d", key->tunnel_id);
  2224. if (mask->has_tunnel_id) {
  2225. monitor_printf(mon, "(0x%x)", mask->tunnel_id);
  2226. }
  2227. }
  2228. if (key->has_eth_type) {
  2229. switch (key->eth_type) {
  2230. case 0x0806:
  2231. monitor_printf(mon, " ARP");
  2232. break;
  2233. case 0x0800:
  2234. monitor_printf(mon, " IP");
  2235. break;
  2236. case 0x86dd:
  2237. monitor_printf(mon, " IPv6");
  2238. break;
  2239. case 0x8809:
  2240. monitor_printf(mon, " LACP");
  2241. break;
  2242. case 0x88cc:
  2243. monitor_printf(mon, " LLDP");
  2244. break;
  2245. default:
  2246. monitor_printf(mon, " eth type 0x%04x", key->eth_type);
  2247. break;
  2248. }
  2249. }
  2250. if (key->has_eth_src) {
  2251. if ((strcmp(key->eth_src, "01:00:00:00:00:00") == 0) &&
  2252. (mask->has_eth_src) &&
  2253. (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
  2254. monitor_printf(mon, " src <any mcast/bcast>");
  2255. } else if ((strcmp(key->eth_src, "00:00:00:00:00:00") == 0) &&
  2256. (mask->has_eth_src) &&
  2257. (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
  2258. monitor_printf(mon, " src <any ucast>");
  2259. } else {
  2260. monitor_printf(mon, " src %s", key->eth_src);
  2261. if (mask->has_eth_src) {
  2262. monitor_printf(mon, "(%s)", mask->eth_src);
  2263. }
  2264. }
  2265. }
  2266. if (key->has_eth_dst) {
  2267. if ((strcmp(key->eth_dst, "01:00:00:00:00:00") == 0) &&
  2268. (mask->has_eth_dst) &&
  2269. (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
  2270. monitor_printf(mon, " dst <any mcast/bcast>");
  2271. } else if ((strcmp(key->eth_dst, "00:00:00:00:00:00") == 0) &&
  2272. (mask->has_eth_dst) &&
  2273. (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
  2274. monitor_printf(mon, " dst <any ucast>");
  2275. } else {
  2276. monitor_printf(mon, " dst %s", key->eth_dst);
  2277. if (mask->has_eth_dst) {
  2278. monitor_printf(mon, "(%s)", mask->eth_dst);
  2279. }
  2280. }
  2281. }
  2282. if (key->has_ip_proto) {
  2283. monitor_printf(mon, " proto %d", key->ip_proto);
  2284. if (mask->has_ip_proto) {
  2285. monitor_printf(mon, "(0x%x)", mask->ip_proto);
  2286. }
  2287. }
  2288. if (key->has_ip_tos) {
  2289. monitor_printf(mon, " TOS %d", key->ip_tos);
  2290. if (mask->has_ip_tos) {
  2291. monitor_printf(mon, "(0x%x)", mask->ip_tos);
  2292. }
  2293. }
  2294. if (key->has_ip_dst) {
  2295. monitor_printf(mon, " dst %s", key->ip_dst);
  2296. }
  2297. if (action->has_goto_tbl || action->has_group_id ||
  2298. action->has_new_vlan_id) {
  2299. monitor_printf(mon, " -->");
  2300. }
  2301. if (action->has_new_vlan_id) {
  2302. monitor_printf(mon, " apply new vlan %d",
  2303. ntohs(action->new_vlan_id));
  2304. }
  2305. if (action->has_group_id) {
  2306. monitor_printf(mon, " write group 0x%08x", action->group_id);
  2307. }
  2308. if (action->has_goto_tbl) {
  2309. monitor_printf(mon, " goto tbl %d", action->goto_tbl);
  2310. }
  2311. monitor_printf(mon, "\n");
  2312. }
  2313. qapi_free_RockerOfDpaFlowList(list);
  2314. }
  2315. void hmp_rocker_of_dpa_groups(Monitor *mon, const QDict *qdict)
  2316. {
  2317. RockerOfDpaGroupList *list, *g;
  2318. const char *name = qdict_get_str(qdict, "name");
  2319. uint8_t type = qdict_get_try_int(qdict, "type", 9);
  2320. Error *err = NULL;
  2321. bool set = false;
  2322. list = qmp_query_rocker_of_dpa_groups(name, type != 9, type, &err);
  2323. if (err != NULL) {
  2324. hmp_handle_error(mon, &err);
  2325. return;
  2326. }
  2327. monitor_printf(mon, "id (decode) --> buckets\n");
  2328. for (g = list; g; g = g->next) {
  2329. RockerOfDpaGroup *group = g->value;
  2330. monitor_printf(mon, "0x%08x", group->id);
  2331. monitor_printf(mon, " (type %s", group->type == 0 ? "L2 interface" :
  2332. group->type == 1 ? "L2 rewrite" :
  2333. group->type == 2 ? "L3 unicast" :
  2334. group->type == 3 ? "L2 multicast" :
  2335. group->type == 4 ? "L2 flood" :
  2336. group->type == 5 ? "L3 interface" :
  2337. group->type == 6 ? "L3 multicast" :
  2338. group->type == 7 ? "L3 ECMP" :
  2339. group->type == 8 ? "L2 overlay" :
  2340. "unknown");
  2341. if (group->has_vlan_id) {
  2342. monitor_printf(mon, " vlan %d", group->vlan_id);
  2343. }
  2344. if (group->has_pport) {
  2345. monitor_printf(mon, " pport %d", group->pport);
  2346. }
  2347. if (group->has_index) {
  2348. monitor_printf(mon, " index %d", group->index);
  2349. }
  2350. monitor_printf(mon, ") -->");
  2351. if (group->has_set_vlan_id && group->set_vlan_id) {
  2352. set = true;
  2353. monitor_printf(mon, " set vlan %d",
  2354. group->set_vlan_id & VLAN_VID_MASK);
  2355. }
  2356. if (group->has_set_eth_src) {
  2357. if (!set) {
  2358. set = true;
  2359. monitor_printf(mon, " set");
  2360. }
  2361. monitor_printf(mon, " src %s", group->set_eth_src);
  2362. }
  2363. if (group->has_set_eth_dst) {
  2364. if (!set) {
  2365. set = true;
  2366. monitor_printf(mon, " set");
  2367. }
  2368. monitor_printf(mon, " dst %s", group->set_eth_dst);
  2369. }
  2370. set = false;
  2371. if (group->has_ttl_check && group->ttl_check) {
  2372. monitor_printf(mon, " check TTL");
  2373. }
  2374. if (group->has_group_id && group->group_id) {
  2375. monitor_printf(mon, " group id 0x%08x", group->group_id);
  2376. }
  2377. if (group->has_pop_vlan && group->pop_vlan) {
  2378. monitor_printf(mon, " pop vlan");
  2379. }
  2380. if (group->has_out_pport) {
  2381. monitor_printf(mon, " out pport %d", group->out_pport);
  2382. }
  2383. if (group->has_group_ids) {
  2384. struct uint32List *id;
  2385. monitor_printf(mon, " groups [");
  2386. for (id = group->group_ids; id; id = id->next) {
  2387. monitor_printf(mon, "0x%08x", id->value);
  2388. if (id->next) {
  2389. monitor_printf(mon, ",");
  2390. }
  2391. }
  2392. monitor_printf(mon, "]");
  2393. }
  2394. monitor_printf(mon, "\n");
  2395. }
  2396. qapi_free_RockerOfDpaGroupList(list);
  2397. }
  2398. void hmp_info_dump(Monitor *mon, const QDict *qdict)
  2399. {
  2400. DumpQueryResult *result = qmp_query_dump(NULL);
  2401. assert(result && result->status < DUMP_STATUS__MAX);
  2402. monitor_printf(mon, "Status: %s\n", DumpStatus_str(result->status));
  2403. if (result->status == DUMP_STATUS_ACTIVE) {
  2404. float percent = 0;
  2405. assert(result->total != 0);
  2406. percent = 100.0 * result->completed / result->total;
  2407. monitor_printf(mon, "Finished: %.2f %%\n", percent);
  2408. }
  2409. qapi_free_DumpQueryResult(result);
  2410. }
  2411. void hmp_info_ramblock(Monitor *mon, const QDict *qdict)
  2412. {
  2413. ram_block_dump(mon);
  2414. }
  2415. void hmp_hotpluggable_cpus(Monitor *mon, const QDict *qdict)
  2416. {
  2417. Error *err = NULL;
  2418. HotpluggableCPUList *l = qmp_query_hotpluggable_cpus(&err);
  2419. HotpluggableCPUList *saved = l;
  2420. CpuInstanceProperties *c;
  2421. if (err != NULL) {
  2422. hmp_handle_error(mon, &err);
  2423. return;
  2424. }
  2425. monitor_printf(mon, "Hotpluggable CPUs:\n");
  2426. while (l) {
  2427. monitor_printf(mon, " type: \"%s\"\n", l->value->type);
  2428. monitor_printf(mon, " vcpus_count: \"%" PRIu64 "\"\n",
  2429. l->value->vcpus_count);
  2430. if (l->value->has_qom_path) {
  2431. monitor_printf(mon, " qom_path: \"%s\"\n", l->value->qom_path);
  2432. }
  2433. c = l->value->props;
  2434. monitor_printf(mon, " CPUInstance Properties:\n");
  2435. if (c->has_node_id) {
  2436. monitor_printf(mon, " node-id: \"%" PRIu64 "\"\n", c->node_id);
  2437. }
  2438. if (c->has_socket_id) {
  2439. monitor_printf(mon, " socket-id: \"%" PRIu64 "\"\n", c->socket_id);
  2440. }
  2441. if (c->has_core_id) {
  2442. monitor_printf(mon, " core-id: \"%" PRIu64 "\"\n", c->core_id);
  2443. }
  2444. if (c->has_thread_id) {
  2445. monitor_printf(mon, " thread-id: \"%" PRIu64 "\"\n", c->thread_id);
  2446. }
  2447. l = l->next;
  2448. }
  2449. qapi_free_HotpluggableCPUList(saved);
  2450. }
  2451. void hmp_info_vm_generation_id(Monitor *mon, const QDict *qdict)
  2452. {
  2453. Error *err = NULL;
  2454. GuidInfo *info = qmp_query_vm_generation_id(&err);
  2455. if (info) {
  2456. monitor_printf(mon, "%s\n", info->guid);
  2457. }
  2458. hmp_handle_error(mon, &err);
  2459. qapi_free_GuidInfo(info);
  2460. }
  2461. void hmp_info_memory_size_summary(Monitor *mon, const QDict *qdict)
  2462. {
  2463. Error *err = NULL;
  2464. MemoryInfo *info = qmp_query_memory_size_summary(&err);
  2465. if (info) {
  2466. monitor_printf(mon, "base memory: %" PRIu64 "\n",
  2467. info->base_memory);
  2468. if (info->has_plugged_memory) {
  2469. monitor_printf(mon, "plugged memory: %" PRIu64 "\n",
  2470. info->plugged_memory);
  2471. }
  2472. qapi_free_MemoryInfo(info);
  2473. }
  2474. hmp_handle_error(mon, &err);
  2475. }