hmp-cmds.c 91 KB

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