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"
26 #include "ui/console.h"
27 #include "ui/dbus-display.h"
28 #include "sysemu/kvm.h"
29 #include "sysemu/runstate.h"
30 #include "sysemu/runstate-action.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/type-helpers.h"
41 #include "qapi/qmp/qerror.h"
42 #include "exec/ramlist.h"
43 #include "hw/mem/memory-device.h"
44 #include "hw/acpi/acpi_dev_interface.h"
45 #include "hw/intc/intc.h"
46 #include "hw/rdma/rdma.h"
48 NameInfo
*qmp_query_name(Error
**errp
)
50 NameInfo
*info
= g_malloc0(sizeof(*info
));
53 info
->has_name
= true;
54 info
->name
= g_strdup(qemu_name
);
60 KvmInfo
*qmp_query_kvm(Error
**errp
)
62 KvmInfo
*info
= g_malloc0(sizeof(*info
));
64 info
->enabled
= kvm_enabled();
65 info
->present
= accel_find("kvm");
70 UuidInfo
*qmp_query_uuid(Error
**errp
)
72 UuidInfo
*info
= g_malloc0(sizeof(*info
));
74 info
->UUID
= qemu_uuid_unparse_strdup(&qemu_uuid
);
78 void qmp_quit(Error
**errp
)
80 shutdown_action
= SHUTDOWN_ACTION_POWEROFF
;
81 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP_QUIT
);
84 void qmp_stop(Error
**errp
)
86 /* if there is a dump in background, we should wait until the dump
88 if (dump_in_progress()) {
89 error_setg(errp
, "There is a dump in process, please wait.");
93 if (runstate_check(RUN_STATE_INMIGRATE
)) {
96 vm_stop(RUN_STATE_PAUSED
);
100 void qmp_system_reset(Error
**errp
)
102 qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET
);
105 void qmp_system_powerdown(Error
**errp
)
107 qemu_system_powerdown_request();
110 void qmp_cont(Error
**errp
)
114 Error
*local_err
= NULL
;
116 /* if there is a dump in background, we should wait until the dump
118 if (dump_in_progress()) {
119 error_setg(errp
, "There is a dump in process, please wait.");
123 if (runstate_needs_reset()) {
124 error_setg(errp
, "Resetting the Virtual Machine is required");
126 } else if (runstate_check(RUN_STATE_SUSPENDED
)) {
128 } else if (runstate_check(RUN_STATE_FINISH_MIGRATE
)) {
129 error_setg(errp
, "Migration is not finalized yet");
133 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
134 blk_iostatus_reset(blk
);
137 for (job
= block_job_next(NULL
); job
; job
= block_job_next(job
)) {
138 block_job_iostatus_reset(job
);
141 /* Continuing after completed migration. Images have been inactivated to
142 * allow the destination to take control. Need to get control back now.
144 * If there are no inactive block nodes (e.g. because the VM was just
145 * paused rather than completing a migration), bdrv_inactivate_all() simply
146 * doesn't do anything. */
147 bdrv_invalidate_cache_all(&local_err
);
149 error_propagate(errp
, local_err
);
153 if (runstate_check(RUN_STATE_INMIGRATE
)) {
160 void qmp_system_wakeup(Error
**errp
)
162 if (!qemu_wakeup_suspend_enabled()) {
164 "wake-up from suspend is not supported by this guest");
168 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
, errp
);
171 void qmp_set_password(const char *protocol
, const char *password
,
172 bool has_connected
, const char *connected
, Error
**errp
)
174 int disconnect_if_connected
= 0;
175 int fail_if_connected
= 0;
179 if (strcmp(connected
, "fail") == 0) {
180 fail_if_connected
= 1;
181 } else if (strcmp(connected
, "disconnect") == 0) {
182 disconnect_if_connected
= 1;
183 } else if (strcmp(connected
, "keep") == 0) {
186 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
191 if (strcmp(protocol
, "spice") == 0) {
192 if (!qemu_using_spice(errp
)) {
195 rc
= qemu_spice
.set_passwd(password
, fail_if_connected
,
196 disconnect_if_connected
);
197 } else if (strcmp(protocol
, "vnc") == 0) {
198 if (fail_if_connected
|| disconnect_if_connected
) {
199 /* vnc supports "connected=keep" only */
200 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
203 /* Note that setting an empty password will not disable login through
205 rc
= vnc_display_password(NULL
, password
);
207 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol",
213 error_setg(errp
, "Could not set password");
217 void qmp_expire_password(const char *protocol
, const char *whenstr
,
223 if (strcmp(whenstr
, "now") == 0) {
225 } else if (strcmp(whenstr
, "never") == 0) {
227 } else if (whenstr
[0] == '+') {
228 when
= time(NULL
) + strtoull(whenstr
+1, NULL
, 10);
230 when
= strtoull(whenstr
, NULL
, 10);
233 if (strcmp(protocol
, "spice") == 0) {
234 if (!qemu_using_spice(errp
)) {
237 rc
= qemu_spice
.set_pw_expire(when
);
238 } else if (strcmp(protocol
, "vnc") == 0) {
239 rc
= vnc_display_pw_expire(NULL
, when
);
241 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol",
247 error_setg(errp
, "Could not set password expire time");
252 void qmp_change_vnc_password(const char *password
, Error
**errp
)
254 if (vnc_display_password(NULL
, password
) < 0) {
255 error_setg(errp
, "Could not set password");
260 void qmp_add_client(const char *protocol
, const char *fdname
,
261 bool has_skipauth
, bool skipauth
, bool has_tls
, bool tls
,
267 fd
= monitor_get_fd(monitor_cur(), fdname
, errp
);
272 if (strcmp(protocol
, "spice") == 0) {
273 if (!qemu_using_spice(errp
)) {
277 skipauth
= has_skipauth
? skipauth
: false;
278 tls
= has_tls
? tls
: false;
279 if (qemu_spice
.display_add_client(fd
, skipauth
, tls
) < 0) {
280 error_setg(errp
, "spice failed to add client");
285 } else if (strcmp(protocol
, "vnc") == 0) {
286 skipauth
= has_skipauth
? skipauth
: false;
287 vnc_display_add_client(NULL
, fd
, skipauth
);
290 #ifdef CONFIG_DBUS_DISPLAY
291 } else if (strcmp(protocol
, "@dbus-display") == 0) {
292 if (!qemu_using_dbus_display(errp
)) {
296 if (!qemu_dbus_display
.add_client(fd
, errp
)) {
302 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
303 if (qemu_chr_add_client(s
, fd
) < 0) {
304 error_setg(errp
, "failed to add client");
311 error_setg(errp
, "protocol '%s' is invalid", protocol
);
316 MemoryDeviceInfoList
*qmp_query_memory_devices(Error
**errp
)
318 return qmp_memory_device_list();
321 ACPIOSTInfoList
*qmp_query_acpi_ospm_status(Error
**errp
)
324 ACPIOSTInfoList
*head
= NULL
;
325 ACPIOSTInfoList
**prev
= &head
;
326 Object
*obj
= object_resolve_path_type("", TYPE_ACPI_DEVICE_IF
, &ambig
);
329 AcpiDeviceIfClass
*adevc
= ACPI_DEVICE_IF_GET_CLASS(obj
);
330 AcpiDeviceIf
*adev
= ACPI_DEVICE_IF(obj
);
332 adevc
->ospm_status(adev
, &prev
);
334 error_setg(errp
, "command is not supported, missing ACPI device");
340 MemoryInfo
*qmp_query_memory_size_summary(Error
**errp
)
342 MemoryInfo
*mem_info
= g_malloc0(sizeof(MemoryInfo
));
343 MachineState
*ms
= MACHINE(qdev_get_machine());
345 mem_info
->base_memory
= ms
->ram_size
;
347 mem_info
->plugged_memory
= get_plugged_memory_size();
348 mem_info
->has_plugged_memory
=
349 mem_info
->plugged_memory
!= (uint64_t)-1;
354 void qmp_display_reload(DisplayReloadOptions
*arg
, Error
**errp
)
357 case DISPLAY_RELOAD_TYPE_VNC
:
359 if (arg
->u
.vnc
.has_tls_certs
&& arg
->u
.vnc
.tls_certs
) {
360 vnc_display_reload_certs(NULL
, errp
);
363 error_setg(errp
, "vnc is invalid, missing 'CONFIG_VNC'");
371 #ifdef CONFIG_PROFILER
375 HumanReadableText
*qmp_x_query_profile(Error
**errp
)
377 g_autoptr(GString
) buf
= g_string_new("");
378 static int64_t last_cpu_exec_time
;
379 int64_t cpu_exec_time
;
382 cpu_exec_time
= tcg_cpu_exec_time();
383 delta
= cpu_exec_time
- last_cpu_exec_time
;
385 g_string_append_printf(buf
, "async time %" PRId64
" (%0.3f)\n",
386 dev_time
, dev_time
/ (double)NANOSECONDS_PER_SECOND
);
387 g_string_append_printf(buf
, "qemu time %" PRId64
" (%0.3f)\n",
388 delta
, delta
/ (double)NANOSECONDS_PER_SECOND
);
389 last_cpu_exec_time
= cpu_exec_time
;
392 return human_readable_text_from_str(buf
);
395 HumanReadableText
*qmp_x_query_profile(Error
**errp
)
397 error_setg(errp
, "Internal profiler not compiled");
402 static int qmp_x_query_rdma_foreach(Object
*obj
, void *opaque
)
405 RdmaProviderClass
*k
;
406 GString
*buf
= opaque
;
408 if (object_dynamic_cast(obj
, INTERFACE_RDMA_PROVIDER
)) {
409 rdma
= RDMA_PROVIDER(obj
);
410 k
= RDMA_PROVIDER_GET_CLASS(obj
);
411 if (k
->format_statistics
) {
412 k
->format_statistics(rdma
, buf
);
414 g_string_append_printf(buf
,
415 "RDMA statistics not available for %s.\n",
416 object_get_typename(obj
));
423 HumanReadableText
*qmp_x_query_rdma(Error
**errp
)
425 g_autoptr(GString
) buf
= g_string_new("");
427 object_child_foreach_recursive(object_get_root(),
428 qmp_x_query_rdma_foreach
, buf
);
430 return human_readable_text_from_str(buf
);
433 HumanReadableText
*qmp_x_query_ramblock(Error
**errp
)
435 g_autoptr(GString
) buf
= ram_block_format();
437 return human_readable_text_from_str(buf
);
440 static int qmp_x_query_irq_foreach(Object
*obj
, void *opaque
)
442 InterruptStatsProvider
*intc
;
443 InterruptStatsProviderClass
*k
;
444 GString
*buf
= opaque
;
446 if (object_dynamic_cast(obj
, TYPE_INTERRUPT_STATS_PROVIDER
)) {
447 intc
= INTERRUPT_STATS_PROVIDER(obj
);
448 k
= INTERRUPT_STATS_PROVIDER_GET_CLASS(obj
);
449 uint64_t *irq_counts
;
450 unsigned int nb_irqs
, i
;
451 if (k
->get_statistics
&&
452 k
->get_statistics(intc
, &irq_counts
, &nb_irqs
)) {
454 g_string_append_printf(buf
, "IRQ statistics for %s:\n",
455 object_get_typename(obj
));
456 for (i
= 0; i
< nb_irqs
; i
++) {
457 if (irq_counts
[i
] > 0) {
458 g_string_append_printf(buf
, "%2d: %" PRId64
"\n", i
,
464 g_string_append_printf(buf
,
465 "IRQ statistics not available for %s.\n",
466 object_get_typename(obj
));
473 HumanReadableText
*qmp_x_query_irq(Error
**errp
)
475 g_autoptr(GString
) buf
= g_string_new("");
477 object_child_foreach_recursive(object_get_root(),
478 qmp_x_query_irq_foreach
, buf
);
480 return human_readable_text_from_str(buf
);