xref: /qemu/monitor/qmp-cmds.c (revision 6f10a29e)
1 /*
2  * QEMU Management Protocol commands
3  *
4  * Copyright IBM, Corp. 2011
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  *
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.
14  */
15 
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"
48 
49 NameInfo *qmp_query_name(Error **errp)
50 {
51     NameInfo *info = g_malloc0(sizeof(*info));
52 
53     info->name = g_strdup(qemu_name);
54     return info;
55 }
56 
57 KvmInfo *qmp_query_kvm(Error **errp)
58 {
59     KvmInfo *info = g_malloc0(sizeof(*info));
60 
61     info->enabled = kvm_enabled();
62     info->present = accel_find("kvm");
63 
64     return info;
65 }
66 
67 UuidInfo *qmp_query_uuid(Error **errp)
68 {
69     UuidInfo *info = g_malloc0(sizeof(*info));
70 
71     info->UUID = qemu_uuid_unparse_strdup(&qemu_uuid);
72     return info;
73 }
74 
75 void qmp_quit(Error **errp)
76 {
77     shutdown_action = SHUTDOWN_ACTION_POWEROFF;
78     qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP_QUIT);
79 }
80 
81 void qmp_stop(Error **errp)
82 {
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;
88     }
89 
90     if (runstate_check(RUN_STATE_INMIGRATE)) {
91         autostart = 0;
92     } else {
93         vm_stop(RUN_STATE_PAUSED);
94     }
95 }
96 
97 void qmp_system_reset(Error **errp)
98 {
99     qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET);
100 }
101 
102 void qmp_system_powerdown(Error **errp)
103 {
104     qemu_system_powerdown_request();
105 }
106 
107 void qmp_cont(Error **errp)
108 {
109     BlockBackend *blk;
110     BlockJob *job;
111     Error *local_err = NULL;
112 
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;
118     }
119 
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;
128     }
129 
130     for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
131         blk_iostatus_reset(blk);
132     }
133 
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);
138         }
139     }
140 
141     /* Continuing after completed migration. Images have been inactivated to
142      * allow the destination to take control. Need to get control back now.
143      *
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;
151     }
152 
153     if (runstate_check(RUN_STATE_INMIGRATE)) {
154         autostart = 1;
155     } else {
156         vm_start();
157     }
158 }
159 
160 void qmp_system_wakeup(Error **errp)
161 {
162     if (!qemu_wakeup_suspend_enabled()) {
163         error_setg(errp,
164                    "wake-up from suspend is not supported by this guest");
165         return;
166     }
167 
168     qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, errp);
169 }
170 
171 void qmp_set_password(SetPasswordOptions *opts, Error **errp)
172 {
173     int rc;
174 
175     if (opts->protocol == DISPLAY_PROTOCOL_SPICE) {
176         if (!qemu_using_spice(errp)) {
177             return;
178         }
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;
188         }
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);
192     }
193 
194     if (rc != 0) {
195         error_setg(errp, "Could not set password");
196     }
197 }
198 
199 void qmp_expire_password(ExpirePasswordOptions *opts, Error **errp)
200 {
201     time_t when;
202     int rc;
203     const char *whenstr = opts->time;
204 
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);
213     }
214 
215     if (opts->protocol == DISPLAY_PROTOCOL_SPICE) {
216         if (!qemu_using_spice(errp)) {
217             return;
218         }
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);
223     }
224 
225     if (rc != 0) {
226         error_setg(errp, "Could not set password expire time");
227     }
228 }
229 
230 #ifdef CONFIG_VNC
231 void qmp_change_vnc_password(const char *password, Error **errp)
232 {
233     if (vnc_display_password(NULL, password) < 0) {
234         error_setg(errp, "Could not set password");
235     }
236 }
237 #endif
238 
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)
242 {
243     Chardev *s;
244     int fd;
245 
246     fd = monitor_get_fd(monitor_cur(), fdname, errp);
247     if (fd < 0) {
248         return;
249     }
250 
251     if (strcmp(protocol, "spice") == 0) {
252         if (!qemu_using_spice(errp)) {
253             close(fd);
254             return;
255         }
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);
261         }
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;
274         }
275         if (!qemu_dbus_display.add_client(fd, errp)) {
276             close(fd);
277             return;
278         }
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;
286         }
287         return;
288     }
289 
290     error_setg(errp, "protocol '%s' is invalid", protocol);
291     close(fd);
292 }
293 
294 
295 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
296 {
297     return qmp_memory_device_list();
298 }
299 
300 ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
301 {
302     bool ambig;
303     ACPIOSTInfoList *head = NULL;
304     ACPIOSTInfoList **prev = &head;
305     Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
306 
307     if (obj) {
308         AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
309         AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
310 
311         adevc->ospm_status(adev, &prev);
312     } else {
313         error_setg(errp, "command is not supported, missing ACPI device");
314     }
315 
316     return head;
317 }
318 
319 MemoryInfo *qmp_query_memory_size_summary(Error **errp)
320 {
321     MemoryInfo *mem_info = g_new0(MemoryInfo, 1);
322     MachineState *ms = MACHINE(qdev_get_machine());
323 
324     mem_info->base_memory = ms->ram_size;
325 
326     mem_info->plugged_memory = get_plugged_memory_size();
327     mem_info->has_plugged_memory =
328         mem_info->plugged_memory != (uint64_t)-1;
329 
330     return mem_info;
331 }
332 
333 void qmp_display_reload(DisplayReloadOptions *arg, Error **errp)
334 {
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);
340         }
341 #else
342         error_setg(errp, "vnc is invalid, missing 'CONFIG_VNC'");
343 #endif
344         break;
345     default:
346         abort();
347     }
348 }
349 
350 void qmp_display_update(DisplayUpdateOptions *arg, Error **errp)
351 {
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();
362     }
363 }
364 
365 static int qmp_x_query_rdma_foreach(Object *obj, void *opaque)
366 {
367     RdmaProvider *rdma;
368     RdmaProviderClass *k;
369     GString *buf = opaque;
370 
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));
380         }
381     }
382 
383     return 0;
384 }
385 
386 HumanReadableText *qmp_x_query_rdma(Error **errp)
387 {
388     g_autoptr(GString) buf = g_string_new("");
389 
390     object_child_foreach_recursive(object_get_root(),
391                                    qmp_x_query_rdma_foreach, buf);
392 
393     return human_readable_text_from_str(buf);
394 }
395 
396 HumanReadableText *qmp_x_query_ramblock(Error **errp)
397 {
398     g_autoptr(GString) buf = ram_block_format();
399 
400     return human_readable_text_from_str(buf);
401 }
402 
403 static int qmp_x_query_irq_foreach(Object *obj, void *opaque)
404 {
405     InterruptStatsProvider *intc;
406     InterruptStatsProviderClass *k;
407     GString *buf = opaque;
408 
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]);
423                     }
424                 }
425             }
426         } else {
427             g_string_append_printf(buf,
428                                    "IRQ statistics not available for %s.\n",
429                                    object_get_typename(obj));
430         }
431     }
432 
433     return 0;
434 }
435 
436 HumanReadableText *qmp_x_query_irq(Error **errp)
437 {
438     g_autoptr(GString) buf = g_string_new("");
439 
440     object_child_foreach_recursive(object_get_root(),
441                                    qmp_x_query_irq_foreach, buf);
442 
443     return human_readable_text_from_str(buf);
444 }
445 
446 typedef struct StatsCallbacks {
447     StatsProvider provider;
448     StatRetrieveFunc *stats_cb;
449     SchemaRetrieveFunc *schemas_cb;
450     QTAILQ_ENTRY(StatsCallbacks) next;
451 } StatsCallbacks;
452 
453 static QTAILQ_HEAD(, StatsCallbacks) stats_callbacks =
454     QTAILQ_HEAD_INITIALIZER(stats_callbacks);
455 
456 void add_stats_callbacks(StatsProvider provider,
457                          StatRetrieveFunc *stats_fn,
458                          SchemaRetrieveFunc *schemas_fn)
459 {
460     StatsCallbacks *entry = g_new(StatsCallbacks, 1);
461     entry->provider = provider;
462     entry->stats_cb = stats_fn;
463     entry->schemas_cb = schemas_fn;
464 
465     QTAILQ_INSERT_TAIL(&stats_callbacks, entry, next);
466 }
467 
468 static bool invoke_stats_cb(StatsCallbacks *entry,
469                             StatsResultList **stats_results,
470                             StatsFilter *filter, StatsRequest *request,
471                             Error **errp)
472 {
473     ERRP_GUARD();
474     strList *targets = NULL;
475     strList *names = NULL;
476 
477     if (request) {
478         if (request->provider != entry->provider) {
479             return true;
480         }
481         if (request->has_names && !request->names) {
482             return true;
483         }
484         names = request->has_names ? request->names : NULL;
485     }
486 
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;
495             }
496             targets = filter->u.vcpu.vcpus;
497         }
498         break;
499     default:
500         abort();
501     }
502 
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;
508     }
509     return true;
510 }
511 
512 StatsResultList *qmp_query_stats(StatsFilter *filter, Error **errp)
513 {
514     StatsResultList *stats_results = NULL;
515     StatsCallbacks *entry;
516     StatsRequestList *request;
517 
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;
524                 }
525             }
526         } else {
527             if (!invoke_stats_cb(entry, &stats_results, filter, NULL, errp)) {
528                 break;
529             }
530         }
531     }
532 
533     return stats_results;
534 }
535 
536 StatsSchemaList *qmp_query_stats_schemas(bool has_provider,
537                                          StatsProvider provider,
538                                          Error **errp)
539 {
540     ERRP_GUARD();
541     StatsSchemaList *stats_results = NULL;
542     StatsCallbacks *entry;
543 
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;
550             }
551         }
552     }
553 
554     return stats_results;
555 }
556 
557 void add_stats_entry(StatsResultList **stats_results, StatsProvider provider,
558                      const char *qom_path, StatsList *stats_list)
559 {
560     StatsResult *entry = g_new0(StatsResult, 1);
561 
562     entry->provider = provider;
563     entry->qom_path = g_strdup(qom_path);
564     entry->stats = stats_list;
565 
566     QAPI_LIST_PREPEND(*stats_results, entry);
567 }
568 
569 void add_stats_schema(StatsSchemaList **schema_results,
570                       StatsProvider provider, StatsTarget target,
571                       StatsSchemaValueList *stats_list)
572 {
573     StatsSchema *entry = g_new0(StatsSchema, 1);
574 
575     entry->provider = provider;
576     entry->target = target;
577     entry->stats = stats_list;
578     QAPI_LIST_PREPEND(*schema_results, entry);
579 }
580 
581 bool apply_str_list_filter(const char *string, strList *list)
582 {
583     strList *str_list = NULL;
584 
585     if (!list) {
586         return true;
587     }
588     for (str_list = list; str_list; str_list = str_list->next) {
589         if (g_str_equal(string, str_list->value)) {
590             return true;
591         }
592     }
593     return false;
594 }
595