qmp-cmds.c 10 KB

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