hmp.c 86 KB

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