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/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/console.h"
26 #include "ui/dbus-display.h"
27 #include "sysemu/kvm.h"
28 #include "sysemu/runstate.h"
29 #include "sysemu/runstate-action.h"
30 #include "sysemu/blockdev.h"
31 #include "sysemu/block-backend.h"
32 #include "qapi/error.h"
33 #include "qapi/qapi-commands-acpi.h"
34 #include "qapi/qapi-commands-block.h"
35 #include "qapi/qapi-commands-control.h"
36 #include "qapi/qapi-commands-machine.h"
37 #include "qapi/qapi-commands-misc.h"
38 #include "qapi/qapi-commands-stats.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"
47 #include "monitor/stats.h"
49 NameInfo
*qmp_query_name(Error
**errp
)
51 NameInfo
*info
= g_malloc0(sizeof(*info
));
53 info
->name
= g_strdup(qemu_name
);
57 KvmInfo
*qmp_query_kvm(Error
**errp
)
59 KvmInfo
*info
= g_malloc0(sizeof(*info
));
61 info
->enabled
= kvm_enabled();
62 info
->present
= accel_find("kvm");
67 UuidInfo
*qmp_query_uuid(Error
**errp
)
69 UuidInfo
*info
= g_malloc0(sizeof(*info
));
71 info
->UUID
= qemu_uuid_unparse_strdup(&qemu_uuid
);
75 void qmp_quit(Error
**errp
)
77 shutdown_action
= SHUTDOWN_ACTION_POWEROFF
;
78 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP_QUIT
);
81 void qmp_stop(Error
**errp
)
83 /* if there is a dump in background, we should wait until the dump
85 if (qemu_system_dump_in_progress()) {
86 error_setg(errp
, "There is a dump in process, please wait.");
90 if (runstate_check(RUN_STATE_INMIGRATE
)) {
93 vm_stop(RUN_STATE_PAUSED
);
97 void qmp_system_reset(Error
**errp
)
99 qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET
);
102 void qmp_system_powerdown(Error
**errp
)
104 qemu_system_powerdown_request();
107 void qmp_cont(Error
**errp
)
111 Error
*local_err
= NULL
;
113 /* if there is a dump in background, we should wait until the dump
115 if (qemu_system_dump_in_progress()) {
116 error_setg(errp
, "There is a dump in process, please wait.");
120 if (runstate_needs_reset()) {
121 error_setg(errp
, "Resetting the Virtual Machine is required");
123 } else if (runstate_check(RUN_STATE_SUSPENDED
)) {
125 } else if (runstate_check(RUN_STATE_FINISH_MIGRATE
)) {
126 error_setg(errp
, "Migration is not finalized yet");
130 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
131 blk_iostatus_reset(blk
);
134 WITH_JOB_LOCK_GUARD() {
135 for (job
= block_job_next_locked(NULL
); job
;
136 job
= block_job_next_locked(job
)) {
137 block_job_iostatus_reset_locked(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_activate_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(SetPasswordOptions
*opts
, Error
**errp
)
175 if (opts
->protocol
== DISPLAY_PROTOCOL_SPICE
) {
176 if (!qemu_using_spice(errp
)) {
179 rc
= qemu_spice
.set_passwd(opts
->password
,
180 opts
->connected
== SET_PASSWORD_ACTION_FAIL
,
181 opts
->connected
== SET_PASSWORD_ACTION_DISCONNECT
);
183 assert(opts
->protocol
== DISPLAY_PROTOCOL_VNC
);
184 if (opts
->connected
!= SET_PASSWORD_ACTION_KEEP
) {
185 /* vnc supports "connected=keep" only */
186 error_setg(errp
, QERR_INVALID_PARAMETER
, "connected");
189 /* Note that setting an empty password will not disable login through
191 rc
= vnc_display_password(opts
->u
.vnc
.display
, opts
->password
);
195 error_setg(errp
, "Could not set password");
199 void qmp_expire_password(ExpirePasswordOptions
*opts
, Error
**errp
)
203 const char *whenstr
= opts
->time
;
205 if (strcmp(whenstr
, "now") == 0) {
207 } else if (strcmp(whenstr
, "never") == 0) {
209 } else if (whenstr
[0] == '+') {
210 when
= time(NULL
) + strtoull(whenstr
+1, NULL
, 10);
212 when
= strtoull(whenstr
, NULL
, 10);
215 if (opts
->protocol
== DISPLAY_PROTOCOL_SPICE
) {
216 if (!qemu_using_spice(errp
)) {
219 rc
= qemu_spice
.set_pw_expire(when
);
221 assert(opts
->protocol
== DISPLAY_PROTOCOL_VNC
);
222 rc
= vnc_display_pw_expire(opts
->u
.vnc
.display
, when
);
226 error_setg(errp
, "Could not set password expire time");
231 void qmp_change_vnc_password(const char *password
, Error
**errp
)
233 if (vnc_display_password(NULL
, password
) < 0) {
234 error_setg(errp
, "Could not set password");
239 void qmp_add_client(const char *protocol
, const char *fdname
,
240 bool has_skipauth
, bool skipauth
, bool has_tls
, bool tls
,
246 fd
= monitor_get_fd(monitor_cur(), fdname
, errp
);
251 if (strcmp(protocol
, "spice") == 0) {
252 if (!qemu_using_spice(errp
)) {
256 skipauth
= has_skipauth
? skipauth
: false;
257 tls
= has_tls
? tls
: false;
258 if (qemu_spice
.display_add_client(fd
, skipauth
, tls
) < 0) {
259 error_setg(errp
, "spice failed to add client");
264 } else if (strcmp(protocol
, "vnc") == 0) {
265 skipauth
= has_skipauth
? skipauth
: false;
266 vnc_display_add_client(NULL
, fd
, skipauth
);
269 #ifdef CONFIG_DBUS_DISPLAY
270 } else if (strcmp(protocol
, "@dbus-display") == 0) {
271 if (!qemu_using_dbus_display(errp
)) {
275 if (!qemu_dbus_display
.add_client(fd
, errp
)) {
281 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
282 if (qemu_chr_add_client(s
, fd
) < 0) {
283 error_setg(errp
, "failed to add client");
290 error_setg(errp
, "protocol '%s' is invalid", protocol
);
295 MemoryDeviceInfoList
*qmp_query_memory_devices(Error
**errp
)
297 return qmp_memory_device_list();
300 ACPIOSTInfoList
*qmp_query_acpi_ospm_status(Error
**errp
)
303 ACPIOSTInfoList
*head
= NULL
;
304 ACPIOSTInfoList
**prev
= &head
;
305 Object
*obj
= object_resolve_path_type("", TYPE_ACPI_DEVICE_IF
, &ambig
);
308 AcpiDeviceIfClass
*adevc
= ACPI_DEVICE_IF_GET_CLASS(obj
);
309 AcpiDeviceIf
*adev
= ACPI_DEVICE_IF(obj
);
311 adevc
->ospm_status(adev
, &prev
);
313 error_setg(errp
, "command is not supported, missing ACPI device");
319 MemoryInfo
*qmp_query_memory_size_summary(Error
**errp
)
321 MemoryInfo
*mem_info
= g_new0(MemoryInfo
, 1);
322 MachineState
*ms
= MACHINE(qdev_get_machine());
324 mem_info
->base_memory
= ms
->ram_size
;
326 mem_info
->plugged_memory
= get_plugged_memory_size();
327 mem_info
->has_plugged_memory
=
328 mem_info
->plugged_memory
!= (uint64_t)-1;
333 void qmp_display_reload(DisplayReloadOptions
*arg
, Error
**errp
)
336 case DISPLAY_RELOAD_TYPE_VNC
:
338 if (arg
->u
.vnc
.has_tls_certs
&& arg
->u
.vnc
.tls_certs
) {
339 vnc_display_reload_certs(NULL
, errp
);
342 error_setg(errp
, "vnc is invalid, missing 'CONFIG_VNC'");
350 void qmp_display_update(DisplayUpdateOptions
*arg
, Error
**errp
)
353 case DISPLAY_UPDATE_TYPE_VNC
:
355 vnc_display_update(&arg
->u
.vnc
, errp
);
357 error_setg(errp
, "vnc is invalid, missing 'CONFIG_VNC'");
365 static int qmp_x_query_rdma_foreach(Object
*obj
, void *opaque
)
368 RdmaProviderClass
*k
;
369 GString
*buf
= opaque
;
371 if (object_dynamic_cast(obj
, INTERFACE_RDMA_PROVIDER
)) {
372 rdma
= RDMA_PROVIDER(obj
);
373 k
= RDMA_PROVIDER_GET_CLASS(obj
);
374 if (k
->format_statistics
) {
375 k
->format_statistics(rdma
, buf
);
377 g_string_append_printf(buf
,
378 "RDMA statistics not available for %s.\n",
379 object_get_typename(obj
));
386 HumanReadableText
*qmp_x_query_rdma(Error
**errp
)
388 g_autoptr(GString
) buf
= g_string_new("");
390 object_child_foreach_recursive(object_get_root(),
391 qmp_x_query_rdma_foreach
, buf
);
393 return human_readable_text_from_str(buf
);
396 HumanReadableText
*qmp_x_query_ramblock(Error
**errp
)
398 g_autoptr(GString
) buf
= ram_block_format();
400 return human_readable_text_from_str(buf
);
403 static int qmp_x_query_irq_foreach(Object
*obj
, void *opaque
)
405 InterruptStatsProvider
*intc
;
406 InterruptStatsProviderClass
*k
;
407 GString
*buf
= opaque
;
409 if (object_dynamic_cast(obj
, TYPE_INTERRUPT_STATS_PROVIDER
)) {
410 intc
= INTERRUPT_STATS_PROVIDER(obj
);
411 k
= INTERRUPT_STATS_PROVIDER_GET_CLASS(obj
);
412 uint64_t *irq_counts
;
413 unsigned int nb_irqs
, i
;
414 if (k
->get_statistics
&&
415 k
->get_statistics(intc
, &irq_counts
, &nb_irqs
)) {
417 g_string_append_printf(buf
, "IRQ statistics for %s:\n",
418 object_get_typename(obj
));
419 for (i
= 0; i
< nb_irqs
; i
++) {
420 if (irq_counts
[i
] > 0) {
421 g_string_append_printf(buf
, "%2d: %" PRId64
"\n", i
,
427 g_string_append_printf(buf
,
428 "IRQ statistics not available for %s.\n",
429 object_get_typename(obj
));
436 HumanReadableText
*qmp_x_query_irq(Error
**errp
)
438 g_autoptr(GString
) buf
= g_string_new("");
440 object_child_foreach_recursive(object_get_root(),
441 qmp_x_query_irq_foreach
, buf
);
443 return human_readable_text_from_str(buf
);
446 typedef struct StatsCallbacks
{
447 StatsProvider provider
;
448 StatRetrieveFunc
*stats_cb
;
449 SchemaRetrieveFunc
*schemas_cb
;
450 QTAILQ_ENTRY(StatsCallbacks
) next
;
453 static QTAILQ_HEAD(, StatsCallbacks
) stats_callbacks
=
454 QTAILQ_HEAD_INITIALIZER(stats_callbacks
);
456 void add_stats_callbacks(StatsProvider provider
,
457 StatRetrieveFunc
*stats_fn
,
458 SchemaRetrieveFunc
*schemas_fn
)
460 StatsCallbacks
*entry
= g_new(StatsCallbacks
, 1);
461 entry
->provider
= provider
;
462 entry
->stats_cb
= stats_fn
;
463 entry
->schemas_cb
= schemas_fn
;
465 QTAILQ_INSERT_TAIL(&stats_callbacks
, entry
, next
);
468 static bool invoke_stats_cb(StatsCallbacks
*entry
,
469 StatsResultList
**stats_results
,
470 StatsFilter
*filter
, StatsRequest
*request
,
474 strList
*targets
= NULL
;
475 strList
*names
= NULL
;
478 if (request
->provider
!= entry
->provider
) {
481 if (request
->has_names
&& !request
->names
) {
484 names
= request
->has_names
? request
->names
: NULL
;
487 switch (filter
->target
) {
488 case STATS_TARGET_VM
:
490 case STATS_TARGET_VCPU
:
491 if (filter
->u
.vcpu
.has_vcpus
) {
492 if (!filter
->u
.vcpu
.vcpus
) {
493 /* No targets allowed? Return no statistics. */
496 targets
= filter
->u
.vcpu
.vcpus
;
503 entry
->stats_cb(stats_results
, filter
->target
, names
, targets
, errp
);
505 qapi_free_StatsResultList(*stats_results
);
506 *stats_results
= NULL
;
512 StatsResultList
*qmp_query_stats(StatsFilter
*filter
, Error
**errp
)
514 StatsResultList
*stats_results
= NULL
;
515 StatsCallbacks
*entry
;
516 StatsRequestList
*request
;
518 QTAILQ_FOREACH(entry
, &stats_callbacks
, next
) {
519 if (filter
->has_providers
) {
520 for (request
= filter
->providers
; request
; request
= request
->next
) {
521 if (!invoke_stats_cb(entry
, &stats_results
, filter
,
522 request
->value
, errp
)) {
527 if (!invoke_stats_cb(entry
, &stats_results
, filter
, NULL
, errp
)) {
533 return stats_results
;
536 StatsSchemaList
*qmp_query_stats_schemas(bool has_provider
,
537 StatsProvider provider
,
541 StatsSchemaList
*stats_results
= NULL
;
542 StatsCallbacks
*entry
;
544 QTAILQ_FOREACH(entry
, &stats_callbacks
, next
) {
545 if (!has_provider
|| provider
== entry
->provider
) {
546 entry
->schemas_cb(&stats_results
, errp
);
548 qapi_free_StatsSchemaList(stats_results
);
554 return stats_results
;
557 void add_stats_entry(StatsResultList
**stats_results
, StatsProvider provider
,
558 const char *qom_path
, StatsList
*stats_list
)
560 StatsResult
*entry
= g_new0(StatsResult
, 1);
562 entry
->provider
= provider
;
563 entry
->qom_path
= g_strdup(qom_path
);
564 entry
->stats
= stats_list
;
566 QAPI_LIST_PREPEND(*stats_results
, entry
);
569 void add_stats_schema(StatsSchemaList
**schema_results
,
570 StatsProvider provider
, StatsTarget target
,
571 StatsSchemaValueList
*stats_list
)
573 StatsSchema
*entry
= g_new0(StatsSchema
, 1);
575 entry
->provider
= provider
;
576 entry
->target
= target
;
577 entry
->stats
= stats_list
;
578 QAPI_LIST_PREPEND(*schema_results
, entry
);
581 bool apply_str_list_filter(const char *string
, strList
*list
)
583 strList
*str_list
= NULL
;
588 for (str_list
= list
; str_list
; str_list
= str_list
->next
) {
589 if (g_str_equal(string
, str_list
->value
)) {