2 * QEMU Management Protocol commands
4 * Copyright IBM, Corp. 2011
7 * Anthony Liguori <aliguori@us.ibm.com>
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/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"
27 #include "sysemu/kvm.h"
28 #include "sysemu/runstate.h"
29 #include "sysemu/runstate-action.h"
30 #include "sysemu/arch_init.h"
31 #include "sysemu/blockdev.h"
32 #include "sysemu/block-backend.h"
33 #include "qapi/error.h"
34 #include "qapi/qapi-commands-acpi.h"
35 #include "qapi/qapi-commands-block.h"
36 #include "qapi/qapi-commands-control.h"
37 #include "qapi/qapi-commands-machine.h"
38 #include "qapi/qapi-commands-misc.h"
39 #include "qapi/qapi-commands-ui.h"
40 #include "qapi/qmp/qerror.h"
41 #include "hw/mem/memory-device.h"
42 #include "hw/acpi/acpi_dev_interface.h"
44 NameInfo
*qmp_query_name(Error
**errp
)
46 NameInfo
*info
= g_malloc0(sizeof(*info
));
49 info
->has_name
= true;
50 info
->name
= g_strdup(qemu_name
);
56 KvmInfo
*qmp_query_kvm(Error
**errp
)
58 KvmInfo
*info
= g_malloc0(sizeof(*info
));
60 info
->enabled
= kvm_enabled();
61 info
->present
= kvm_available();
66 UuidInfo
*qmp_query_uuid(Error
**errp
)
68 UuidInfo
*info
= g_malloc0(sizeof(*info
));
70 info
->UUID
= qemu_uuid_unparse_strdup(&qemu_uuid
);
74 void qmp_quit(Error
**errp
)
76 shutdown_action
= SHUTDOWN_ACTION_POWEROFF
;
77 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP_QUIT
);
80 void qmp_stop(Error
**errp
)
82 /* if there is a dump in background, we should wait until the dump
84 if (dump_in_progress()) {
85 error_setg(errp
, "There is a dump in process, please wait.");
89 if (runstate_check(RUN_STATE_INMIGRATE
)) {
92 vm_stop(RUN_STATE_PAUSED
);
96 void qmp_system_reset(Error
**errp
)
98 qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET
);
101 void qmp_system_powerdown(Error
**errp
)
103 qemu_system_powerdown_request();
106 void qmp_cont(Error
**errp
)
110 Error
*local_err
= NULL
;
112 /* if there is a dump in background, we should wait until the dump
114 if (dump_in_progress()) {
115 error_setg(errp
, "There is a dump in process, please wait.");
119 if (runstate_needs_reset()) {
120 error_setg(errp
, "Resetting the Virtual Machine is required");
122 } else if (runstate_check(RUN_STATE_SUSPENDED
)) {
124 } else if (runstate_check(RUN_STATE_FINISH_MIGRATE
)) {
125 error_setg(errp
, "Migration is not finalized yet");
129 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
130 blk_iostatus_reset(blk
);
133 for (job
= block_job_next(NULL
); job
; job
= block_job_next(job
)) {
134 block_job_iostatus_reset(job
);
137 /* Continuing after completed migration. Images have been inactivated to
138 * allow the destination to take control. Need to get control back now.
140 * If there are no inactive block nodes (e.g. because the VM was just
141 * paused rather than completing a migration), bdrv_inactivate_all() simply
142 * doesn't do anything. */
143 bdrv_invalidate_cache_all(&local_err
);
145 error_propagate(errp
, local_err
);
149 if (runstate_check(RUN_STATE_INMIGRATE
)) {
156 void qmp_system_wakeup(Error
**errp
)
158 if (!qemu_wakeup_suspend_enabled()) {
160 "wake-up from suspend is not supported by this guest");
164 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
, errp
);
167 void qmp_set_password(const char *protocol
, const char *password
,
168 bool has_connected
, const char *connected
, Error
**errp
)
170 int disconnect_if_connected
= 0;
171 int fail_if_connected
= 0;
175 if (strcmp(connected
, "fail") == 0) {
176 fail_if_connected
= 1;
177 } else if (strcmp(connected
, "disconnect") == 0) {
178 disconnect_if_connected
= 1;
179 } else if (strcmp(connected
, "keep") == 0) {
182 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
187 if (strcmp(protocol
, "spice") == 0) {
188 if (!qemu_using_spice(errp
)) {
191 rc
= qemu_spice
.set_passwd(password
, fail_if_connected
,
192 disconnect_if_connected
);
193 } else if (strcmp(protocol
, "vnc") == 0) {
194 if (fail_if_connected
|| disconnect_if_connected
) {
195 /* vnc supports "connected=keep" only */
196 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
199 /* Note that setting an empty password will not disable login through
201 rc
= vnc_display_password(NULL
, password
);
203 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol",
209 error_setg(errp
, "Could not set password");
213 void qmp_expire_password(const char *protocol
, const char *whenstr
,
219 if (strcmp(whenstr
, "now") == 0) {
221 } else if (strcmp(whenstr
, "never") == 0) {
223 } else if (whenstr
[0] == '+') {
224 when
= time(NULL
) + strtoull(whenstr
+1, NULL
, 10);
226 when
= strtoull(whenstr
, NULL
, 10);
229 if (strcmp(protocol
, "spice") == 0) {
230 if (!qemu_using_spice(errp
)) {
233 rc
= qemu_spice
.set_pw_expire(when
);
234 } else if (strcmp(protocol
, "vnc") == 0) {
235 rc
= vnc_display_pw_expire(NULL
, when
);
237 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol",
243 error_setg(errp
, "Could not set password expire time");
248 void qmp_change_vnc_password(const char *password
, Error
**errp
)
250 if (vnc_display_password(NULL
, password
) < 0) {
251 error_setg(errp
, "Could not set password");
255 static void qmp_change_vnc_listen(const char *target
, Error
**errp
)
257 QemuOptsList
*olist
= qemu_find_opts("vnc");
260 if (strstr(target
, "id=")) {
261 error_setg(errp
, "id not supported");
265 opts
= qemu_opts_find(olist
, "default");
269 opts
= vnc_parse(target
, errp
);
274 vnc_display_open("default", errp
);
277 static void qmp_change_vnc(const char *target
, bool has_arg
, const char *arg
,
280 if (strcmp(target
, "passwd") == 0 || strcmp(target
, "password") == 0) {
282 error_setg(errp
, QERR_MISSING_PARAMETER
, "password");
284 qmp_change_vnc_password(arg
, errp
);
287 qmp_change_vnc_listen(target
, errp
);
290 #endif /* !CONFIG_VNC */
292 void qmp_change(const char *device
, const char *target
,
293 bool has_arg
, const char *arg
, Error
**errp
)
295 if (strcmp(device
, "vnc") == 0) {
297 qmp_change_vnc(target
, has_arg
, arg
, errp
);
299 error_setg(errp
, QERR_FEATURE_DISABLED
, "vnc");
302 qmp_blockdev_change_medium(true, device
, false, NULL
, target
,
303 has_arg
, arg
, false, 0, errp
);
307 void qmp_add_client(const char *protocol
, const char *fdname
,
308 bool has_skipauth
, bool skipauth
, bool has_tls
, bool tls
,
314 fd
= monitor_get_fd(monitor_cur(), fdname
, errp
);
319 if (strcmp(protocol
, "spice") == 0) {
320 if (!qemu_using_spice(errp
)) {
324 skipauth
= has_skipauth
? skipauth
: false;
325 tls
= has_tls
? tls
: false;
326 if (qemu_spice
.display_add_client(fd
, skipauth
, tls
) < 0) {
327 error_setg(errp
, "spice failed to add client");
332 } else if (strcmp(protocol
, "vnc") == 0) {
333 skipauth
= has_skipauth
? skipauth
: false;
334 vnc_display_add_client(NULL
, fd
, skipauth
);
337 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
338 if (qemu_chr_add_client(s
, fd
) < 0) {
339 error_setg(errp
, "failed to add client");
346 error_setg(errp
, "protocol '%s' is invalid", protocol
);
351 MemoryDeviceInfoList
*qmp_query_memory_devices(Error
**errp
)
353 return qmp_memory_device_list();
356 ACPIOSTInfoList
*qmp_query_acpi_ospm_status(Error
**errp
)
359 ACPIOSTInfoList
*head
= NULL
;
360 ACPIOSTInfoList
**prev
= &head
;
361 Object
*obj
= object_resolve_path_type("", TYPE_ACPI_DEVICE_IF
, &ambig
);
364 AcpiDeviceIfClass
*adevc
= ACPI_DEVICE_IF_GET_CLASS(obj
);
365 AcpiDeviceIf
*adev
= ACPI_DEVICE_IF(obj
);
367 adevc
->ospm_status(adev
, &prev
);
369 error_setg(errp
, "command is not supported, missing ACPI device");
375 MemoryInfo
*qmp_query_memory_size_summary(Error
**errp
)
377 MemoryInfo
*mem_info
= g_malloc0(sizeof(MemoryInfo
));
378 MachineState
*ms
= MACHINE(qdev_get_machine());
380 mem_info
->base_memory
= ms
->ram_size
;
382 mem_info
->plugged_memory
= get_plugged_memory_size();
383 mem_info
->has_plugged_memory
=
384 mem_info
->plugged_memory
!= (uint64_t)-1;