xref: /openbmc/qemu/monitor/qmp-cmds.c (revision 7cc25f6c)
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-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/vnc.h"
27 #include "sysemu/kvm.h"
28 #include "sysemu/runstate.h"
29 #include "sysemu/arch_init.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/qmp/qerror.h"
40 #include "hw/mem/memory-device.h"
41 #include "hw/acpi/acpi_dev_interface.h"
42 
43 NameInfo *qmp_query_name(Error **errp)
44 {
45     NameInfo *info = g_malloc0(sizeof(*info));
46 
47     if (qemu_name) {
48         info->has_name = true;
49         info->name = g_strdup(qemu_name);
50     }
51 
52     return info;
53 }
54 
55 KvmInfo *qmp_query_kvm(Error **errp)
56 {
57     KvmInfo *info = g_malloc0(sizeof(*info));
58 
59     info->enabled = kvm_enabled();
60     info->present = kvm_available();
61 
62     return info;
63 }
64 
65 UuidInfo *qmp_query_uuid(Error **errp)
66 {
67     UuidInfo *info = g_malloc0(sizeof(*info));
68 
69     info->UUID = qemu_uuid_unparse_strdup(&qemu_uuid);
70     return info;
71 }
72 
73 void qmp_quit(Error **errp)
74 {
75     no_shutdown = 0;
76     qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP_QUIT);
77 }
78 
79 void qmp_stop(Error **errp)
80 {
81     /* if there is a dump in background, we should wait until the dump
82      * finished */
83     if (dump_in_progress()) {
84         error_setg(errp, "There is a dump in process, please wait.");
85         return;
86     }
87 
88     if (runstate_check(RUN_STATE_INMIGRATE)) {
89         autostart = 0;
90     } else {
91         vm_stop(RUN_STATE_PAUSED);
92     }
93 }
94 
95 void qmp_system_reset(Error **errp)
96 {
97     qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET);
98 }
99 
100 void qmp_system_powerdown(Error **errp)
101 {
102     qemu_system_powerdown_request();
103 }
104 
105 void qmp_x_exit_preconfig(Error **errp)
106 {
107     if (!runstate_check(RUN_STATE_PRECONFIG)) {
108         error_setg(errp, "The command is permitted only in '%s' state",
109                    RunState_str(RUN_STATE_PRECONFIG));
110         return;
111     }
112     qemu_exit_preconfig_request();
113 }
114 
115 void qmp_cont(Error **errp)
116 {
117     BlockBackend *blk;
118     BlockJob *job;
119     Error *local_err = NULL;
120 
121     /* if there is a dump in background, we should wait until the dump
122      * finished */
123     if (dump_in_progress()) {
124         error_setg(errp, "There is a dump in process, please wait.");
125         return;
126     }
127 
128     if (runstate_needs_reset()) {
129         error_setg(errp, "Resetting the Virtual Machine is required");
130         return;
131     } else if (runstate_check(RUN_STATE_SUSPENDED)) {
132         return;
133     } else if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
134         error_setg(errp, "Migration is not finalized yet");
135         return;
136     }
137 
138     for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
139         blk_iostatus_reset(blk);
140     }
141 
142     for (job = block_job_next(NULL); job; job = block_job_next(job)) {
143         block_job_iostatus_reset(job);
144     }
145 
146     /* Continuing after completed migration. Images have been inactivated to
147      * allow the destination to take control. Need to get control back now.
148      *
149      * If there are no inactive block nodes (e.g. because the VM was just
150      * paused rather than completing a migration), bdrv_inactivate_all() simply
151      * doesn't do anything. */
152     bdrv_invalidate_cache_all(&local_err);
153     if (local_err) {
154         error_propagate(errp, local_err);
155         return;
156     }
157 
158     if (runstate_check(RUN_STATE_INMIGRATE)) {
159         autostart = 1;
160     } else {
161         vm_start();
162     }
163 }
164 
165 void qmp_system_wakeup(Error **errp)
166 {
167     if (!qemu_wakeup_suspend_enabled()) {
168         error_setg(errp,
169                    "wake-up from suspend is not supported by this guest");
170         return;
171     }
172 
173     qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, errp);
174 }
175 
176 void qmp_set_password(const char *protocol, const char *password,
177                       bool has_connected, const char *connected, Error **errp)
178 {
179     int disconnect_if_connected = 0;
180     int fail_if_connected = 0;
181     int rc;
182 
183     if (has_connected) {
184         if (strcmp(connected, "fail") == 0) {
185             fail_if_connected = 1;
186         } else if (strcmp(connected, "disconnect") == 0) {
187             disconnect_if_connected = 1;
188         } else if (strcmp(connected, "keep") == 0) {
189             /* nothing */
190         } else {
191             error_setg(errp, QERR_INVALID_PARAMETER, "connected");
192             return;
193         }
194     }
195 
196     if (strcmp(protocol, "spice") == 0) {
197         if (!qemu_using_spice(errp)) {
198             return;
199         }
200         rc = qemu_spice.set_passwd(password, fail_if_connected,
201                                    disconnect_if_connected);
202     } else if (strcmp(protocol, "vnc") == 0) {
203         if (fail_if_connected || disconnect_if_connected) {
204             /* vnc supports "connected=keep" only */
205             error_setg(errp, QERR_INVALID_PARAMETER, "connected");
206             return;
207         }
208         /* Note that setting an empty password will not disable login through
209          * this interface. */
210         rc = vnc_display_password(NULL, password);
211     } else {
212         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol",
213                    "'vnc' or 'spice'");
214         return;
215     }
216 
217     if (rc != 0) {
218         error_setg(errp, "Could not set password");
219     }
220 }
221 
222 void qmp_expire_password(const char *protocol, const char *whenstr,
223                          Error **errp)
224 {
225     time_t when;
226     int rc;
227 
228     if (strcmp(whenstr, "now") == 0) {
229         when = 0;
230     } else if (strcmp(whenstr, "never") == 0) {
231         when = TIME_MAX;
232     } else if (whenstr[0] == '+') {
233         when = time(NULL) + strtoull(whenstr+1, NULL, 10);
234     } else {
235         when = strtoull(whenstr, NULL, 10);
236     }
237 
238     if (strcmp(protocol, "spice") == 0) {
239         if (!qemu_using_spice(errp)) {
240             return;
241         }
242         rc = qemu_spice.set_pw_expire(when);
243     } else if (strcmp(protocol, "vnc") == 0) {
244         rc = vnc_display_pw_expire(NULL, when);
245     } else {
246         error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol",
247                    "'vnc' or 'spice'");
248         return;
249     }
250 
251     if (rc != 0) {
252         error_setg(errp, "Could not set password expire time");
253     }
254 }
255 
256 #ifdef CONFIG_VNC
257 void qmp_change_vnc_password(const char *password, Error **errp)
258 {
259     if (vnc_display_password(NULL, password) < 0) {
260         error_setg(errp, "Could not set password");
261     }
262 }
263 
264 static void qmp_change_vnc_listen(const char *target, Error **errp)
265 {
266     QemuOptsList *olist = qemu_find_opts("vnc");
267     QemuOpts *opts;
268 
269     if (strstr(target, "id=")) {
270         error_setg(errp, "id not supported");
271         return;
272     }
273 
274     opts = qemu_opts_find(olist, "default");
275     if (opts) {
276         qemu_opts_del(opts);
277     }
278     opts = vnc_parse(target, errp);
279     if (!opts) {
280         return;
281     }
282 
283     vnc_display_open("default", errp);
284 }
285 
286 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
287                            Error **errp)
288 {
289     if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
290         if (!has_arg) {
291             error_setg(errp, QERR_MISSING_PARAMETER, "password");
292         } else {
293             qmp_change_vnc_password(arg, errp);
294         }
295     } else {
296         qmp_change_vnc_listen(target, errp);
297     }
298 }
299 #endif /* !CONFIG_VNC */
300 
301 void qmp_change(const char *device, const char *target,
302                 bool has_arg, const char *arg, Error **errp)
303 {
304     if (strcmp(device, "vnc") == 0) {
305 #ifdef CONFIG_VNC
306         qmp_change_vnc(target, has_arg, arg, errp);
307 #else
308         error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
309 #endif
310     } else {
311         qmp_blockdev_change_medium(true, device, false, NULL, target,
312                                    has_arg, arg, false, 0, errp);
313     }
314 }
315 
316 void qmp_add_client(const char *protocol, const char *fdname,
317                     bool has_skipauth, bool skipauth, bool has_tls, bool tls,
318                     Error **errp)
319 {
320     Chardev *s;
321     int fd;
322 
323     fd = monitor_get_fd(monitor_cur(), fdname, errp);
324     if (fd < 0) {
325         return;
326     }
327 
328     if (strcmp(protocol, "spice") == 0) {
329         if (!qemu_using_spice(errp)) {
330             close(fd);
331             return;
332         }
333         skipauth = has_skipauth ? skipauth : false;
334         tls = has_tls ? tls : false;
335         if (qemu_spice.display_add_client(fd, skipauth, tls) < 0) {
336             error_setg(errp, "spice failed to add client");
337             close(fd);
338         }
339         return;
340 #ifdef CONFIG_VNC
341     } else if (strcmp(protocol, "vnc") == 0) {
342         skipauth = has_skipauth ? skipauth : false;
343         vnc_display_add_client(NULL, fd, skipauth);
344         return;
345 #endif
346     } else if ((s = qemu_chr_find(protocol)) != NULL) {
347         if (qemu_chr_add_client(s, fd) < 0) {
348             error_setg(errp, "failed to add client");
349             close(fd);
350             return;
351         }
352         return;
353     }
354 
355     error_setg(errp, "protocol '%s' is invalid", protocol);
356     close(fd);
357 }
358 
359 
360 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
361 {
362     return qmp_memory_device_list();
363 }
364 
365 ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
366 {
367     bool ambig;
368     ACPIOSTInfoList *head = NULL;
369     ACPIOSTInfoList **prev = &head;
370     Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
371 
372     if (obj) {
373         AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
374         AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
375 
376         adevc->ospm_status(adev, &prev);
377     } else {
378         error_setg(errp, "command is not supported, missing ACPI device");
379     }
380 
381     return head;
382 }
383 
384 MemoryInfo *qmp_query_memory_size_summary(Error **errp)
385 {
386     MemoryInfo *mem_info = g_malloc0(sizeof(MemoryInfo));
387     MachineState *ms = MACHINE(qdev_get_machine());
388 
389     mem_info->base_memory = ms->ram_size;
390 
391     mem_info->plugged_memory = get_plugged_memory_size();
392     mem_info->has_plugged_memory =
393         mem_info->plugged_memory != (uint64_t)-1;
394 
395     return mem_info;
396 }
397