qmp-cmds.c 11 KB

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