pc: clean up compat machines
[qemu.git] / monitor / qmp-cmds.c
blob2932b3f3a5b237582f4426453d5856df1aa7094f
1 /*
2 * QEMU Management Protocol commands
4 * Copyright IBM, Corp. 2011
6 * Authors:
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);
54 return info;
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");
64 return info;
67 UuidInfo *qmp_query_uuid(Error **errp)
69 UuidInfo *info = g_malloc0(sizeof(*info));
71 info->UUID = qemu_uuid_unparse_strdup(&qemu_uuid);
72 return info;
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
84 * finished */
85 if (qemu_system_dump_in_progress()) {
86 error_setg(errp, "There is a dump in process, please wait.");
87 return;
90 if (runstate_check(RUN_STATE_INMIGRATE)) {
91 autostart = 0;
92 } else {
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)
109 BlockBackend *blk;
110 BlockJob *job;
111 Error *local_err = NULL;
113 /* if there is a dump in background, we should wait until the dump
114 * finished */
115 if (qemu_system_dump_in_progress()) {
116 error_setg(errp, "There is a dump in process, please wait.");
117 return;
120 if (runstate_needs_reset()) {
121 error_setg(errp, "Resetting the Virtual Machine is required");
122 return;
123 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
124 return;
125 } else if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
126 error_setg(errp, "Migration is not finalized yet");
127 return;
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);
148 if (local_err) {
149 error_propagate(errp, local_err);
150 return;
153 if (runstate_check(RUN_STATE_INMIGRATE)) {
154 autostart = 1;
155 } else {
156 vm_start();
160 void qmp_system_wakeup(Error **errp)
162 if (!qemu_wakeup_suspend_enabled()) {
163 error_setg(errp,
164 "wake-up from suspend is not supported by this guest");
165 return;
168 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, errp);
171 void qmp_set_password(SetPasswordOptions *opts, Error **errp)
173 int rc;
175 if (opts->protocol == DISPLAY_PROTOCOL_SPICE) {
176 if (!qemu_using_spice(errp)) {
177 return;
179 rc = qemu_spice.set_passwd(opts->password,
180 opts->connected == SET_PASSWORD_ACTION_FAIL,
181 opts->connected == SET_PASSWORD_ACTION_DISCONNECT);
182 } else {
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");
187 return;
189 /* Note that setting an empty password will not disable login through
190 * this interface. */
191 rc = vnc_display_password(opts->u.vnc.display, opts->password);
194 if (rc != 0) {
195 error_setg(errp, "Could not set password");
199 void qmp_expire_password(ExpirePasswordOptions *opts, Error **errp)
201 time_t when;
202 int rc;
203 const char *whenstr = opts->time;
205 if (strcmp(whenstr, "now") == 0) {
206 when = 0;
207 } else if (strcmp(whenstr, "never") == 0) {
208 when = TIME_MAX;
209 } else if (whenstr[0] == '+') {
210 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
211 } else {
212 when = strtoull(whenstr, NULL, 10);
215 if (opts->protocol == DISPLAY_PROTOCOL_SPICE) {
216 if (!qemu_using_spice(errp)) {
217 return;
219 rc = qemu_spice.set_pw_expire(when);
220 } else {
221 assert(opts->protocol == DISPLAY_PROTOCOL_VNC);
222 rc = vnc_display_pw_expire(opts->u.vnc.display, when);
225 if (rc != 0) {
226 error_setg(errp, "Could not set password expire time");
230 #ifdef CONFIG_VNC
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");
237 #endif
239 void qmp_add_client(const char *protocol, const char *fdname,
240 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
241 Error **errp)
243 Chardev *s;
244 int fd;
246 fd = monitor_get_fd(monitor_cur(), fdname, errp);
247 if (fd < 0) {
248 return;
251 if (strcmp(protocol, "spice") == 0) {
252 if (!qemu_using_spice(errp)) {
253 close(fd);
254 return;
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");
260 close(fd);
262 return;
263 #ifdef CONFIG_VNC
264 } else if (strcmp(protocol, "vnc") == 0) {
265 skipauth = has_skipauth ? skipauth : false;
266 vnc_display_add_client(NULL, fd, skipauth);
267 return;
268 #endif
269 #ifdef CONFIG_DBUS_DISPLAY
270 } else if (strcmp(protocol, "@dbus-display") == 0) {
271 if (!qemu_using_dbus_display(errp)) {
272 close(fd);
273 return;
275 if (!qemu_dbus_display.add_client(fd, errp)) {
276 close(fd);
277 return;
279 return;
280 #endif
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");
284 close(fd);
285 return;
287 return;
290 error_setg(errp, "protocol '%s' is invalid", protocol);
291 close(fd);
295 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
297 return qmp_memory_device_list();
300 ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
302 bool ambig;
303 ACPIOSTInfoList *head = NULL;
304 ACPIOSTInfoList **prev = &head;
305 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
307 if (obj) {
308 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
309 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
311 adevc->ospm_status(adev, &prev);
312 } else {
313 error_setg(errp, "command is not supported, missing ACPI device");
316 return head;
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;
330 return mem_info;
333 void qmp_display_reload(DisplayReloadOptions *arg, Error **errp)
335 switch (arg->type) {
336 case DISPLAY_RELOAD_TYPE_VNC:
337 #ifdef CONFIG_VNC
338 if (arg->u.vnc.has_tls_certs && arg->u.vnc.tls_certs) {
339 vnc_display_reload_certs(NULL, errp);
341 #else
342 error_setg(errp, "vnc is invalid, missing 'CONFIG_VNC'");
343 #endif
344 break;
345 default:
346 abort();
350 void qmp_display_update(DisplayUpdateOptions *arg, Error **errp)
352 switch (arg->type) {
353 case DISPLAY_UPDATE_TYPE_VNC:
354 #ifdef CONFIG_VNC
355 vnc_display_update(&arg->u.vnc, errp);
356 #else
357 error_setg(errp, "vnc is invalid, missing 'CONFIG_VNC'");
358 #endif
359 break;
360 default:
361 abort();
365 static int qmp_x_query_rdma_foreach(Object *obj, void *opaque)
367 RdmaProvider *rdma;
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);
376 } else {
377 g_string_append_printf(buf,
378 "RDMA statistics not available for %s.\n",
379 object_get_typename(obj));
383 return 0;
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)) {
416 if (nb_irqs > 0) {
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,
422 irq_counts[i]);
426 } else {
427 g_string_append_printf(buf,
428 "IRQ statistics not available for %s.\n",
429 object_get_typename(obj));
433 return 0;
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;
451 } StatsCallbacks;
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,
471 Error **errp)
473 ERRP_GUARD();
474 strList *targets = NULL;
475 strList *names = NULL;
477 if (request) {
478 if (request->provider != entry->provider) {
479 return true;
481 if (request->has_names && !request->names) {
482 return true;
484 names = request->has_names ? request->names : NULL;
487 switch (filter->target) {
488 case STATS_TARGET_VM:
489 break;
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. */
494 return true;
496 targets = filter->u.vcpu.vcpus;
498 break;
499 default:
500 abort();
503 entry->stats_cb(stats_results, filter->target, names, targets, errp);
504 if (*errp) {
505 qapi_free_StatsResultList(*stats_results);
506 *stats_results = NULL;
507 return false;
509 return true;
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)) {
523 break;
526 } else {
527 if (!invoke_stats_cb(entry, &stats_results, filter, NULL, errp)) {
528 break;
533 return stats_results;
536 StatsSchemaList *qmp_query_stats_schemas(bool has_provider,
537 StatsProvider provider,
538 Error **errp)
540 ERRP_GUARD();
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);
547 if (*errp) {
548 qapi_free_StatsSchemaList(stats_results);
549 return NULL;
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;
585 if (!list) {
586 return true;
588 for (str_list = list; str_list; str_list = str_list->next) {
589 if (g_str_equal(string, str_list->value)) {
590 return true;
593 return false;