2 * QEMU Management Protocol commands
4 * Copyright IBM, Corp. 2011
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
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.
16 #include "qemu/osdep.h"
17 #include "qemu-common.h"
18 #include "qemu-version.h"
19 #include "qemu/cutils.h"
20 #include "qemu/option.h"
21 #include "monitor/monitor.h"
22 #include "sysemu/sysemu.h"
23 #include "qemu/config-file.h"
24 #include "qemu/uuid.h"
25 #include "chardev/char.h"
26 #include "ui/qemu-spice.h"
28 #include "sysemu/kvm.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/boards.h"
39 #include "hw/mem/memory-device.h"
40 #include "hw/acpi/acpi_dev_interface.h"
42 NameInfo *qmp_query_name(Error **errp)
44 NameInfo *info = g_malloc0(sizeof(*info));
47 info->has_name = true;
48 info->name = g_strdup(qemu_name);
54 VersionInfo *qmp_query_version(Error **errp)
56 VersionInfo *info = g_new0(VersionInfo, 1);
58 info->qemu = g_new0(VersionTriple, 1);
59 info->qemu->major = QEMU_VERSION_MAJOR;
60 info->qemu->minor = QEMU_VERSION_MINOR;
61 info->qemu->micro = QEMU_VERSION_MICRO;
62 info->package = g_strdup(QEMU_PKGVERSION);
67 KvmInfo *qmp_query_kvm(Error **errp)
69 KvmInfo *info = g_malloc0(sizeof(*info));
71 info->enabled = kvm_enabled();
72 info->present = kvm_available();
77 UuidInfo *qmp_query_uuid(Error **errp)
79 UuidInfo *info = g_malloc0(sizeof(*info));
81 info->UUID = qemu_uuid_unparse_strdup(&qemu_uuid);
85 void qmp_quit(Error **errp)
88 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP_QUIT);
91 void qmp_stop(Error **errp)
93 /* if there is a dump in background, we should wait until the dump
95 if (dump_in_progress()) {
96 error_setg(errp, "There is a dump in process, please wait.");
100 if (runstate_check(RUN_STATE_INMIGRATE)) {
103 vm_stop(RUN_STATE_PAUSED);
107 void qmp_system_reset(Error **errp)
109 qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET);
112 void qmp_system_powerdown(Error **erp)
114 qemu_system_powerdown_request();
117 void qmp_cpu_add(int64_t id, Error **errp)
121 mc = MACHINE_GET_CLASS(current_machine);
122 if (mc->hot_add_cpu) {
123 mc->hot_add_cpu(id, errp);
125 error_setg(errp, "Not supported");
129 void qmp_x_exit_preconfig(Error **errp)
131 if (!runstate_check(RUN_STATE_PRECONFIG)) {
132 error_setg(errp, "The command is permitted only in '%s' state",
133 RunState_str(RUN_STATE_PRECONFIG));
136 qemu_exit_preconfig_request();
139 void qmp_cont(Error **errp)
143 Error *local_err = NULL;
145 /* if there is a dump in background, we should wait until the dump
147 if (dump_in_progress()) {
148 error_setg(errp, "There is a dump in process, please wait.");
152 if (runstate_needs_reset()) {
153 error_setg(errp, "Resetting the Virtual Machine is required");
155 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
157 } else if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
158 error_setg(errp, "Migration is not finalized yet");
162 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
163 blk_iostatus_reset(blk);
166 for (job = block_job_next(NULL); job; job = block_job_next(job)) {
167 block_job_iostatus_reset(job);
170 /* Continuing after completed migration. Images have been inactivated to
171 * allow the destination to take control. Need to get control back now.
173 * If there are no inactive block nodes (e.g. because the VM was just
174 * paused rather than completing a migration), bdrv_inactivate_all() simply
175 * doesn't do anything. */
176 bdrv_invalidate_cache_all(&local_err);
178 error_propagate(errp, local_err);
182 if (runstate_check(RUN_STATE_INMIGRATE)) {
189 void qmp_system_wakeup(Error **errp)
191 if (!qemu_wakeup_suspend_enabled()) {
193 "wake-up from suspend is not supported by this guest");
197 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, errp);
200 void qmp_set_password(const char *protocol, const char *password,
201 bool has_connected, const char *connected, Error **errp)
203 int disconnect_if_connected = 0;
204 int fail_if_connected = 0;
208 if (strcmp(connected, "fail") == 0) {
209 fail_if_connected = 1;
210 } else if (strcmp(connected, "disconnect") == 0) {
211 disconnect_if_connected = 1;
212 } else if (strcmp(connected, "keep") == 0) {
215 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
220 if (strcmp(protocol, "spice") == 0) {
221 if (!qemu_using_spice(errp)) {
224 rc = qemu_spice_set_passwd(password, fail_if_connected,
225 disconnect_if_connected);
227 error_setg(errp, QERR_SET_PASSWD_FAILED);
232 if (strcmp(protocol, "vnc") == 0) {
233 if (fail_if_connected || disconnect_if_connected) {
234 /* vnc supports "connected=keep" only */
235 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
238 /* Note that setting an empty password will not disable login through
240 rc = vnc_display_password(NULL, password);
242 error_setg(errp, QERR_SET_PASSWD_FAILED);
247 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
250 void qmp_expire_password(const char *protocol, const char *whenstr,
256 if (strcmp(whenstr, "now") == 0) {
258 } else if (strcmp(whenstr, "never") == 0) {
260 } else if (whenstr[0] == '+') {
261 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
263 when = strtoull(whenstr, NULL, 10);
266 if (strcmp(protocol, "spice") == 0) {
267 if (!qemu_using_spice(errp)) {
270 rc = qemu_spice_set_pw_expire(when);
272 error_setg(errp, QERR_SET_PASSWD_FAILED);
277 if (strcmp(protocol, "vnc") == 0) {
278 rc = vnc_display_pw_expire(NULL, when);
280 error_setg(errp, QERR_SET_PASSWD_FAILED);
285 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
289 void qmp_change_vnc_password(const char *password, Error **errp)
291 if (vnc_display_password(NULL, password) < 0) {
292 error_setg(errp, QERR_SET_PASSWD_FAILED);
296 static void qmp_change_vnc_listen(const char *target, Error **errp)
298 QemuOptsList *olist = qemu_find_opts("vnc");
301 if (strstr(target, "id=")) {
302 error_setg(errp, "id not supported");
306 opts = qemu_opts_find(olist, "default");
310 opts = vnc_parse(target, errp);
315 vnc_display_open("default", errp);
318 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
321 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
323 error_setg(errp, QERR_MISSING_PARAMETER, "password");
325 qmp_change_vnc_password(arg, errp);
328 qmp_change_vnc_listen(target, errp);
331 #endif /* !CONFIG_VNC */
333 void qmp_change(const char *device, const char *target,
334 bool has_arg, const char *arg, Error **errp)
336 if (strcmp(device, "vnc") == 0) {
338 qmp_change_vnc(target, has_arg, arg, errp);
340 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
343 qmp_blockdev_change_medium(true, device, false, NULL, target,
344 has_arg, arg, false, 0, errp);
348 void qmp_add_client(const char *protocol, const char *fdname,
349 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
355 fd = monitor_get_fd(cur_mon, fdname, errp);
360 if (strcmp(protocol, "spice") == 0) {
361 if (!qemu_using_spice(errp)) {
365 skipauth = has_skipauth ? skipauth : false;
366 tls = has_tls ? tls : false;
367 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
368 error_setg(errp, "spice failed to add client");
373 } else if (strcmp(protocol, "vnc") == 0) {
374 skipauth = has_skipauth ? skipauth : false;
375 vnc_display_add_client(NULL, fd, skipauth);
378 } else if ((s = qemu_chr_find(protocol)) != NULL) {
379 if (qemu_chr_add_client(s, fd) < 0) {
380 error_setg(errp, "failed to add client");
387 error_setg(errp, "protocol '%s' is invalid", protocol);
392 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
394 return qmp_memory_device_list();
397 ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
400 ACPIOSTInfoList *head = NULL;
401 ACPIOSTInfoList **prev = &head;
402 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
405 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
406 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
408 adevc->ospm_status(adev, &prev);
410 error_setg(errp, "command is not supported, missing ACPI device");
416 MemoryInfo *qmp_query_memory_size_summary(Error **errp)
418 MemoryInfo *mem_info = g_malloc0(sizeof(MemoryInfo));
420 mem_info->base_memory = ram_size;
422 mem_info->plugged_memory = get_plugged_memory_size();
423 mem_info->has_plugged_memory =
424 mem_info->plugged_memory != (uint64_t)-1;