qmp-cmds.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * QEMU Management Protocol 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 "qemu-common.h"
  17. #include "qemu/cutils.h"
  18. #include "qemu/option.h"
  19. #include "monitor/monitor.h"
  20. #include "sysemu/sysemu.h"
  21. #include "qemu/config-file.h"
  22. #include "qemu/uuid.h"
  23. #include "chardev/char.h"
  24. #include "ui/qemu-spice.h"
  25. #include "ui/vnc.h"
  26. #include "sysemu/kvm.h"
  27. #include "sysemu/runstate.h"
  28. #include "sysemu/arch_init.h"
  29. #include "sysemu/blockdev.h"
  30. #include "sysemu/block-backend.h"
  31. #include "qapi/error.h"
  32. #include "qapi/qapi-commands-acpi.h"
  33. #include "qapi/qapi-commands-block.h"
  34. #include "qapi/qapi-commands-control.h"
  35. #include "qapi/qapi-commands-machine.h"
  36. #include "qapi/qapi-commands-misc.h"
  37. #include "qapi/qapi-commands-ui.h"
  38. #include "qapi/qmp/qerror.h"
  39. #include "hw/mem/memory-device.h"
  40. #include "hw/acpi/acpi_dev_interface.h"
  41. NameInfo *qmp_query_name(Error **errp)
  42. {
  43. NameInfo *info = g_malloc0(sizeof(*info));
  44. if (qemu_name) {
  45. info->has_name = true;
  46. info->name = g_strdup(qemu_name);
  47. }
  48. return info;
  49. }
  50. KvmInfo *qmp_query_kvm(Error **errp)
  51. {
  52. KvmInfo *info = g_malloc0(sizeof(*info));
  53. info->enabled = kvm_enabled();
  54. info->present = kvm_available();
  55. return info;
  56. }
  57. UuidInfo *qmp_query_uuid(Error **errp)
  58. {
  59. UuidInfo *info = g_malloc0(sizeof(*info));
  60. info->UUID = qemu_uuid_unparse_strdup(&qemu_uuid);
  61. return info;
  62. }
  63. void qmp_quit(Error **errp)
  64. {
  65. no_shutdown = 0;
  66. qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP_QUIT);
  67. }
  68. void qmp_stop(Error **errp)
  69. {
  70. /* if there is a dump in background, we should wait until the dump
  71. * finished */
  72. if (dump_in_progress()) {
  73. error_setg(errp, "There is a dump in process, please wait.");
  74. return;
  75. }
  76. if (runstate_check(RUN_STATE_INMIGRATE)) {
  77. autostart = 0;
  78. } else {
  79. vm_stop(RUN_STATE_PAUSED);
  80. }
  81. }
  82. void qmp_system_reset(Error **errp)
  83. {
  84. qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET);
  85. }
  86. void qmp_system_powerdown(Error **errp)
  87. {
  88. qemu_system_powerdown_request();
  89. }
  90. void qmp_x_exit_preconfig(Error **errp)
  91. {
  92. if (!runstate_check(RUN_STATE_PRECONFIG)) {
  93. error_setg(errp, "The command is permitted only in '%s' state",
  94. RunState_str(RUN_STATE_PRECONFIG));
  95. return;
  96. }
  97. qemu_exit_preconfig_request();
  98. }
  99. void qmp_cont(Error **errp)
  100. {
  101. BlockBackend *blk;
  102. BlockJob *job;
  103. Error *local_err = NULL;
  104. /* if there is a dump in background, we should wait until the dump
  105. * finished */
  106. if (dump_in_progress()) {
  107. error_setg(errp, "There is a dump in process, please wait.");
  108. return;
  109. }
  110. if (runstate_needs_reset()) {
  111. error_setg(errp, "Resetting the Virtual Machine is required");
  112. return;
  113. } else if (runstate_check(RUN_STATE_SUSPENDED)) {
  114. return;
  115. } else if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
  116. error_setg(errp, "Migration is not finalized yet");
  117. return;
  118. }
  119. for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
  120. blk_iostatus_reset(blk);
  121. }
  122. for (job = block_job_next(NULL); job; job = block_job_next(job)) {
  123. block_job_iostatus_reset(job);
  124. }
  125. /* Continuing after completed migration. Images have been inactivated to
  126. * allow the destination to take control. Need to get control back now.
  127. *
  128. * If there are no inactive block nodes (e.g. because the VM was just
  129. * paused rather than completing a migration), bdrv_inactivate_all() simply
  130. * doesn't do anything. */
  131. bdrv_invalidate_cache_all(&local_err);
  132. if (local_err) {
  133. error_propagate(errp, local_err);
  134. return;
  135. }
  136. if (runstate_check(RUN_STATE_INMIGRATE)) {
  137. autostart = 1;
  138. } else {
  139. vm_start();
  140. }
  141. }
  142. void qmp_system_wakeup(Error **errp)
  143. {
  144. if (!qemu_wakeup_suspend_enabled()) {
  145. error_setg(errp,
  146. "wake-up from suspend is not supported by this guest");
  147. return;
  148. }
  149. qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, errp);
  150. }
  151. void qmp_set_password(const char *protocol, const char *password,
  152. bool has_connected, const char *connected, Error **errp)
  153. {
  154. int disconnect_if_connected = 0;
  155. int fail_if_connected = 0;
  156. int rc;
  157. if (has_connected) {
  158. if (strcmp(connected, "fail") == 0) {
  159. fail_if_connected = 1;
  160. } else if (strcmp(connected, "disconnect") == 0) {
  161. disconnect_if_connected = 1;
  162. } else if (strcmp(connected, "keep") == 0) {
  163. /* nothing */
  164. } else {
  165. error_setg(errp, QERR_INVALID_PARAMETER, "connected");
  166. return;
  167. }
  168. }
  169. if (strcmp(protocol, "spice") == 0) {
  170. if (!qemu_using_spice(errp)) {
  171. return;
  172. }
  173. rc = qemu_spice.set_passwd(password, fail_if_connected,
  174. disconnect_if_connected);
  175. if (rc != 0) {
  176. error_setg(errp, QERR_SET_PASSWD_FAILED);
  177. }
  178. return;
  179. }
  180. if (strcmp(protocol, "vnc") == 0) {
  181. if (fail_if_connected || disconnect_if_connected) {
  182. /* vnc supports "connected=keep" only */
  183. error_setg(errp, QERR_INVALID_PARAMETER, "connected");
  184. return;
  185. }
  186. /* Note that setting an empty password will not disable login through
  187. * this interface. */
  188. rc = vnc_display_password(NULL, password);
  189. if (rc < 0) {
  190. error_setg(errp, QERR_SET_PASSWD_FAILED);
  191. }
  192. return;
  193. }
  194. error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
  195. }
  196. void qmp_expire_password(const char *protocol, const char *whenstr,
  197. Error **errp)
  198. {
  199. time_t when;
  200. int rc;
  201. if (strcmp(whenstr, "now") == 0) {
  202. when = 0;
  203. } else if (strcmp(whenstr, "never") == 0) {
  204. when = TIME_MAX;
  205. } else if (whenstr[0] == '+') {
  206. when = time(NULL) + strtoull(whenstr+1, NULL, 10);
  207. } else {
  208. when = strtoull(whenstr, NULL, 10);
  209. }
  210. if (strcmp(protocol, "spice") == 0) {
  211. if (!qemu_using_spice(errp)) {
  212. return;
  213. }
  214. rc = qemu_spice.set_pw_expire(when);
  215. if (rc != 0) {
  216. error_setg(errp, QERR_SET_PASSWD_FAILED);
  217. }
  218. return;
  219. }
  220. if (strcmp(protocol, "vnc") == 0) {
  221. rc = vnc_display_pw_expire(NULL, when);
  222. if (rc != 0) {
  223. error_setg(errp, QERR_SET_PASSWD_FAILED);
  224. }
  225. return;
  226. }
  227. error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
  228. }
  229. #ifdef CONFIG_VNC
  230. void qmp_change_vnc_password(const char *password, Error **errp)
  231. {
  232. if (vnc_display_password(NULL, password) < 0) {
  233. error_setg(errp, QERR_SET_PASSWD_FAILED);
  234. }
  235. }
  236. static void qmp_change_vnc_listen(const char *target, Error **errp)
  237. {
  238. QemuOptsList *olist = qemu_find_opts("vnc");
  239. QemuOpts *opts;
  240. if (strstr(target, "id=")) {
  241. error_setg(errp, "id not supported");
  242. return;
  243. }
  244. opts = qemu_opts_find(olist, "default");
  245. if (opts) {
  246. qemu_opts_del(opts);
  247. }
  248. opts = vnc_parse(target, errp);
  249. if (!opts) {
  250. return;
  251. }
  252. vnc_display_open("default", errp);
  253. }
  254. static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
  255. Error **errp)
  256. {
  257. if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
  258. if (!has_arg) {
  259. error_setg(errp, QERR_MISSING_PARAMETER, "password");
  260. } else {
  261. qmp_change_vnc_password(arg, errp);
  262. }
  263. } else {
  264. qmp_change_vnc_listen(target, errp);
  265. }
  266. }
  267. #endif /* !CONFIG_VNC */
  268. void qmp_change(const char *device, const char *target,
  269. bool has_arg, const char *arg, Error **errp)
  270. {
  271. if (strcmp(device, "vnc") == 0) {
  272. #ifdef CONFIG_VNC
  273. qmp_change_vnc(target, has_arg, arg, errp);
  274. #else
  275. error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
  276. #endif
  277. } else {
  278. qmp_blockdev_change_medium(true, device, false, NULL, target,
  279. has_arg, arg, false, 0, errp);
  280. }
  281. }
  282. void qmp_add_client(const char *protocol, const char *fdname,
  283. bool has_skipauth, bool skipauth, bool has_tls, bool tls,
  284. Error **errp)
  285. {
  286. Chardev *s;
  287. int fd;
  288. fd = monitor_get_fd(monitor_cur(), fdname, errp);
  289. if (fd < 0) {
  290. return;
  291. }
  292. if (strcmp(protocol, "spice") == 0) {
  293. if (!qemu_using_spice(errp)) {
  294. close(fd);
  295. return;
  296. }
  297. skipauth = has_skipauth ? skipauth : false;
  298. tls = has_tls ? tls : false;
  299. if (qemu_spice.display_add_client(fd, skipauth, tls) < 0) {
  300. error_setg(errp, "spice failed to add client");
  301. close(fd);
  302. }
  303. return;
  304. #ifdef CONFIG_VNC
  305. } else if (strcmp(protocol, "vnc") == 0) {
  306. skipauth = has_skipauth ? skipauth : false;
  307. vnc_display_add_client(NULL, fd, skipauth);
  308. return;
  309. #endif
  310. } else if ((s = qemu_chr_find(protocol)) != NULL) {
  311. if (qemu_chr_add_client(s, fd) < 0) {
  312. error_setg(errp, "failed to add client");
  313. close(fd);
  314. return;
  315. }
  316. return;
  317. }
  318. error_setg(errp, "protocol '%s' is invalid", protocol);
  319. close(fd);
  320. }
  321. MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
  322. {
  323. return qmp_memory_device_list();
  324. }
  325. ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
  326. {
  327. bool ambig;
  328. ACPIOSTInfoList *head = NULL;
  329. ACPIOSTInfoList **prev = &head;
  330. Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
  331. if (obj) {
  332. AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
  333. AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
  334. adevc->ospm_status(adev, &prev);
  335. } else {
  336. error_setg(errp, "command is not supported, missing ACPI device");
  337. }
  338. return head;
  339. }
  340. MemoryInfo *qmp_query_memory_size_summary(Error **errp)
  341. {
  342. MemoryInfo *mem_info = g_malloc0(sizeof(MemoryInfo));
  343. mem_info->base_memory = ram_size;
  344. mem_info->plugged_memory = get_plugged_memory_size();
  345. mem_info->has_plugged_memory =
  346. mem_info->plugged_memory != (uint64_t)-1;
  347. return mem_info;
  348. }