xref: /openbmc/qemu/monitor/qmp-cmds.c (revision 5dd0be53)
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-ui.h"
39 #include "qapi/type-helpers.h"
40 #include "qapi/qmp/qerror.h"
41 #include "exec/ramlist.h"
42 #include "hw/mem/memory-device.h"
43 #include "hw/acpi/acpi_dev_interface.h"
44 #include "hw/intc/intc.h"
45 #include "hw/rdma/rdma.h"
46 
47 NameInfo *qmp_query_name(Error **errp)
48 {
49     NameInfo *info = g_malloc0(sizeof(*info));
50 
51     if (qemu_name) {
52         info->has_name = true;
53         info->name = g_strdup(qemu_name);
54     }
55 
56     return info;
57 }
58 
59 KvmInfo *qmp_query_kvm(Error **errp)
60 {
61     KvmInfo *info = g_malloc0(sizeof(*info));
62 
63     info->enabled = kvm_enabled();
64     info->present = accel_find("kvm");
65 
66     return info;
67 }
68 
69 UuidInfo *qmp_query_uuid(Error **errp)
70 {
71     UuidInfo *info = g_malloc0(sizeof(*info));
72 
73     info->UUID = qemu_uuid_unparse_strdup(&qemu_uuid);
74     return info;
75 }
76 
77 void qmp_quit(Error **errp)
78 {
79     shutdown_action = SHUTDOWN_ACTION_POWEROFF;
80     qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP_QUIT);
81 }
82 
83 void qmp_stop(Error **errp)
84 {
85     /* if there is a dump in background, we should wait until the dump
86      * finished */
87     if (qemu_system_dump_in_progress()) {
88         error_setg(errp, "There is a dump in process, please wait.");
89         return;
90     }
91 
92     if (runstate_check(RUN_STATE_INMIGRATE)) {
93         autostart = 0;
94     } else {
95         vm_stop(RUN_STATE_PAUSED);
96     }
97 }
98 
99 void qmp_system_reset(Error **errp)
100 {
101     qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET);
102 }
103 
104 void qmp_system_powerdown(Error **errp)
105 {
106     qemu_system_powerdown_request();
107 }
108 
109 void qmp_cont(Error **errp)
110 {
111     BlockBackend *blk;
112     BlockJob *job;
113     Error *local_err = NULL;
114 
115     /* if there is a dump in background, we should wait until the dump
116      * finished */
117     if (qemu_system_dump_in_progress()) {
118         error_setg(errp, "There is a dump in process, please wait.");
119         return;
120     }
121 
122     if (runstate_needs_reset()) {
123         error_setg(errp, "Resetting the Virtual Machine is required");
124         return;
125     } else if (runstate_check(RUN_STATE_SUSPENDED)) {
126         return;
127     } else if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
128         error_setg(errp, "Migration is not finalized yet");
129         return;
130     }
131 
132     for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
133         blk_iostatus_reset(blk);
134     }
135 
136     for (job = block_job_next(NULL); job; job = block_job_next(job)) {
137         block_job_iostatus_reset(job);
138     }
139 
140     /* Continuing after completed migration. Images have been inactivated to
141      * allow the destination to take control. Need to get control back now.
142      *
143      * If there are no inactive block nodes (e.g. because the VM was just
144      * paused rather than completing a migration), bdrv_inactivate_all() simply
145      * doesn't do anything. */
146     bdrv_activate_all(&local_err);
147     if (local_err) {
148         error_propagate(errp, local_err);
149         return;
150     }
151 
152     if (runstate_check(RUN_STATE_INMIGRATE)) {
153         autostart = 1;
154     } else {
155         vm_start();
156     }
157 }
158 
159 void qmp_system_wakeup(Error **errp)
160 {
161     if (!qemu_wakeup_suspend_enabled()) {
162         error_setg(errp,
163                    "wake-up from suspend is not supported by this guest");
164         return;
165     }
166 
167     qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, errp);
168 }
169 
170 void qmp_set_password(SetPasswordOptions *opts, Error **errp)
171 {
172     int rc;
173 
174     if (opts->protocol == DISPLAY_PROTOCOL_SPICE) {
175         if (!qemu_using_spice(errp)) {
176             return;
177         }
178         rc = qemu_spice.set_passwd(opts->password,
179                 opts->connected == SET_PASSWORD_ACTION_FAIL,
180                 opts->connected == SET_PASSWORD_ACTION_DISCONNECT);
181     } else {
182         assert(opts->protocol == DISPLAY_PROTOCOL_VNC);
183         if (opts->connected != SET_PASSWORD_ACTION_KEEP) {
184             /* vnc supports "connected=keep" only */
185             error_setg(errp, QERR_INVALID_PARAMETER, "connected");
186             return;
187         }
188         /* Note that setting an empty password will not disable login through
189          * this interface. */
190         rc = vnc_display_password(opts->u.vnc.display, opts->password);
191     }
192 
193     if (rc != 0) {
194         error_setg(errp, "Could not set password");
195     }
196 }
197 
198 void qmp_expire_password(ExpirePasswordOptions *opts, Error **errp)
199 {
200     time_t when;
201     int rc;
202     const char *whenstr = opts->time;
203 
204     if (strcmp(whenstr, "now") == 0) {
205         when = 0;
206     } else if (strcmp(whenstr, "never") == 0) {
207         when = TIME_MAX;
208     } else if (whenstr[0] == '+') {
209         when = time(NULL) + strtoull(whenstr+1, NULL, 10);
210     } else {
211         when = strtoull(whenstr, NULL, 10);
212     }
213 
214     if (opts->protocol == DISPLAY_PROTOCOL_SPICE) {
215         if (!qemu_using_spice(errp)) {
216             return;
217         }
218         rc = qemu_spice.set_pw_expire(when);
219     } else {
220         assert(opts->protocol == DISPLAY_PROTOCOL_VNC);
221         rc = vnc_display_pw_expire(opts->u.vnc.display, when);
222     }
223 
224     if (rc != 0) {
225         error_setg(errp, "Could not set password expire time");
226     }
227 }
228 
229 #ifdef CONFIG_VNC
230 void qmp_change_vnc_password(const char *password, Error **errp)
231 {
232     if (vnc_display_password(NULL, password) < 0) {
233         error_setg(errp, "Could not set password");
234     }
235 }
236 #endif
237 
238 void qmp_add_client(const char *protocol, const char *fdname,
239                     bool has_skipauth, bool skipauth, bool has_tls, bool tls,
240                     Error **errp)
241 {
242     Chardev *s;
243     int fd;
244 
245     fd = monitor_get_fd(monitor_cur(), fdname, errp);
246     if (fd < 0) {
247         return;
248     }
249 
250     if (strcmp(protocol, "spice") == 0) {
251         if (!qemu_using_spice(errp)) {
252             close(fd);
253             return;
254         }
255         skipauth = has_skipauth ? skipauth : false;
256         tls = has_tls ? tls : false;
257         if (qemu_spice.display_add_client(fd, skipauth, tls) < 0) {
258             error_setg(errp, "spice failed to add client");
259             close(fd);
260         }
261         return;
262 #ifdef CONFIG_VNC
263     } else if (strcmp(protocol, "vnc") == 0) {
264         skipauth = has_skipauth ? skipauth : false;
265         vnc_display_add_client(NULL, fd, skipauth);
266         return;
267 #endif
268 #ifdef CONFIG_DBUS_DISPLAY
269     } else if (strcmp(protocol, "@dbus-display") == 0) {
270         if (!qemu_using_dbus_display(errp)) {
271             close(fd);
272             return;
273         }
274         if (!qemu_dbus_display.add_client(fd, errp)) {
275             close(fd);
276             return;
277         }
278         return;
279 #endif
280     } else if ((s = qemu_chr_find(protocol)) != NULL) {
281         if (qemu_chr_add_client(s, fd) < 0) {
282             error_setg(errp, "failed to add client");
283             close(fd);
284             return;
285         }
286         return;
287     }
288 
289     error_setg(errp, "protocol '%s' is invalid", protocol);
290     close(fd);
291 }
292 
293 
294 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
295 {
296     return qmp_memory_device_list();
297 }
298 
299 ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
300 {
301     bool ambig;
302     ACPIOSTInfoList *head = NULL;
303     ACPIOSTInfoList **prev = &head;
304     Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
305 
306     if (obj) {
307         AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
308         AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
309 
310         adevc->ospm_status(adev, &prev);
311     } else {
312         error_setg(errp, "command is not supported, missing ACPI device");
313     }
314 
315     return head;
316 }
317 
318 MemoryInfo *qmp_query_memory_size_summary(Error **errp)
319 {
320     MemoryInfo *mem_info = g_new0(MemoryInfo, 1);
321     MachineState *ms = MACHINE(qdev_get_machine());
322 
323     mem_info->base_memory = ms->ram_size;
324 
325     mem_info->plugged_memory = get_plugged_memory_size();
326     mem_info->has_plugged_memory =
327         mem_info->plugged_memory != (uint64_t)-1;
328 
329     return mem_info;
330 }
331 
332 void qmp_display_reload(DisplayReloadOptions *arg, Error **errp)
333 {
334     switch (arg->type) {
335     case DISPLAY_RELOAD_TYPE_VNC:
336 #ifdef CONFIG_VNC
337         if (arg->u.vnc.has_tls_certs && arg->u.vnc.tls_certs) {
338             vnc_display_reload_certs(NULL, errp);
339         }
340 #else
341         error_setg(errp, "vnc is invalid, missing 'CONFIG_VNC'");
342 #endif
343         break;
344     default:
345         abort();
346     }
347 }
348 
349 static int qmp_x_query_rdma_foreach(Object *obj, void *opaque)
350 {
351     RdmaProvider *rdma;
352     RdmaProviderClass *k;
353     GString *buf = opaque;
354 
355     if (object_dynamic_cast(obj, INTERFACE_RDMA_PROVIDER)) {
356         rdma = RDMA_PROVIDER(obj);
357         k = RDMA_PROVIDER_GET_CLASS(obj);
358         if (k->format_statistics) {
359             k->format_statistics(rdma, buf);
360         } else {
361             g_string_append_printf(buf,
362                                    "RDMA statistics not available for %s.\n",
363                                    object_get_typename(obj));
364         }
365     }
366 
367     return 0;
368 }
369 
370 HumanReadableText *qmp_x_query_rdma(Error **errp)
371 {
372     g_autoptr(GString) buf = g_string_new("");
373 
374     object_child_foreach_recursive(object_get_root(),
375                                    qmp_x_query_rdma_foreach, buf);
376 
377     return human_readable_text_from_str(buf);
378 }
379 
380 HumanReadableText *qmp_x_query_ramblock(Error **errp)
381 {
382     g_autoptr(GString) buf = ram_block_format();
383 
384     return human_readable_text_from_str(buf);
385 }
386 
387 static int qmp_x_query_irq_foreach(Object *obj, void *opaque)
388 {
389     InterruptStatsProvider *intc;
390     InterruptStatsProviderClass *k;
391     GString *buf = opaque;
392 
393     if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
394         intc = INTERRUPT_STATS_PROVIDER(obj);
395         k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
396         uint64_t *irq_counts;
397         unsigned int nb_irqs, i;
398         if (k->get_statistics &&
399             k->get_statistics(intc, &irq_counts, &nb_irqs)) {
400             if (nb_irqs > 0) {
401                 g_string_append_printf(buf, "IRQ statistics for %s:\n",
402                                        object_get_typename(obj));
403                 for (i = 0; i < nb_irqs; i++) {
404                     if (irq_counts[i] > 0) {
405                         g_string_append_printf(buf, "%2d: %" PRId64 "\n", i,
406                                                irq_counts[i]);
407                     }
408                 }
409             }
410         } else {
411             g_string_append_printf(buf,
412                                    "IRQ statistics not available for %s.\n",
413                                    object_get_typename(obj));
414         }
415     }
416 
417     return 0;
418 }
419 
420 HumanReadableText *qmp_x_query_irq(Error **errp)
421 {
422     g_autoptr(GString) buf = g_string_new("");
423 
424     object_child_foreach_recursive(object_get_root(),
425                                    qmp_x_query_irq_foreach, buf);
426 
427     return human_readable_text_from_str(buf);
428 }
429