]> git.proxmox.com Git - mirror_qemu.git/blame - monitor/qmp-cmds.c
qapi/monitor: refactor set/expire_password with enums
[mirror_qemu.git] / monitor / qmp-cmds.c
CommitLineData
48a32bed 1/*
f1b3ccfa 2 * QEMU Management Protocol commands
48a32bed
AL
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 *
6b620ca3
PB
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.
48a32bed
AL
14 */
15
d38ea87a 16#include "qemu/osdep.h"
a8d25326 17#include "qemu-common.h"
f348b6d1 18#include "qemu/cutils.h"
922a01a0 19#include "qemu/option.h"
a0b1a66e 20#include "monitor/monitor.h"
9c17d615 21#include "sysemu/sysemu.h"
213dcb06 22#include "qemu/config-file.h"
cea25275 23#include "qemu/uuid.h"
8228e353 24#include "chardev/char.h"
fbf796fd 25#include "ui/qemu-spice.h"
fb246f05 26#include "ui/console.h"
99997823 27#include "ui/dbus-display.h"
9c17d615 28#include "sysemu/kvm.h"
54d31236 29#include "sysemu/runstate.h"
e6dba048 30#include "sysemu/runstate-action.h"
9c17d615 31#include "sysemu/blockdev.h"
373340b2 32#include "sysemu/block-backend.h"
e688df6b 33#include "qapi/error.h"
27c9188f 34#include "qapi/qapi-commands-acpi.h"
5a16818b 35#include "qapi/qapi-commands-block.h"
fa4dcf57 36#include "qapi/qapi-commands-control.h"
8ac25c84 37#include "qapi/qapi-commands-machine.h"
112ed241
MA
38#include "qapi/qapi-commands-misc.h"
39#include "qapi/qapi-commands-ui.h"
37087fde 40#include "qapi/type-helpers.h"
cc7a8ea7 41#include "qapi/qmp/qerror.h"
ca411b7c 42#include "exec/ramlist.h"
2cc0e2e8 43#include "hw/mem/memory-device.h"
02419bcb 44#include "hw/acpi/acpi_dev_interface.h"
91f2fa70 45#include "hw/intc/intc.h"
8dbbca5c 46#include "hw/rdma/rdma.h"
48a32bed
AL
47
48NameInfo *qmp_query_name(Error **errp)
49{
50 NameInfo *info = g_malloc0(sizeof(*info));
51
52 if (qemu_name) {
53 info->has_name = true;
54 info->name = g_strdup(qemu_name);
55 }
56
57 return info;
58}
b9c15f16 59
292a2602
LC
60KvmInfo *qmp_query_kvm(Error **errp)
61{
62 KvmInfo *info = g_malloc0(sizeof(*info));
63
64 info->enabled = kvm_enabled();
4f9205be 65 info->present = accel_find("kvm");
292a2602
LC
66
67 return info;
68}
69
efab767e
LC
70UuidInfo *qmp_query_uuid(Error **errp)
71{
72 UuidInfo *info = g_malloc0(sizeof(*info));
efab767e 73
9c5ce8db 74 info->UUID = qemu_uuid_unparse_strdup(&qemu_uuid);
efab767e
LC
75 return info;
76}
77
7daecb30 78void qmp_quit(Error **errp)
7a7f325e 79{
e6dba048 80 shutdown_action = SHUTDOWN_ACTION_POWEROFF;
92548938 81 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP_QUIT);
7a7f325e
LC
82}
83
5f158f21
LC
84void qmp_stop(Error **errp)
85{
65d64f36
PX
86 /* if there is a dump in background, we should wait until the dump
87 * finished */
88 if (dump_in_progress()) {
89 error_setg(errp, "There is a dump in process, please wait.");
90 return;
91 }
92
1e998146
PB
93 if (runstate_check(RUN_STATE_INMIGRATE)) {
94 autostart = 0;
95 } else {
96 vm_stop(RUN_STATE_PAUSED);
97 }
5f158f21
LC
98}
99
38d22653
LC
100void qmp_system_reset(Error **errp)
101{
92548938 102 qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET);
38d22653 103}
5bc465e4 104
ec48595e 105void qmp_system_powerdown(Error **errp)
5bc465e4
LC
106{
107 qemu_system_powerdown_request();
108}
755f1968 109
e42e818b
LC
110void qmp_cont(Error **errp)
111{
373340b2 112 BlockBackend *blk;
68d00e42 113 BlockJob *job;
788cf9f8 114 Error *local_err = NULL;
e42e818b 115
65d64f36
PX
116 /* if there is a dump in background, we should wait until the dump
117 * finished */
118 if (dump_in_progress()) {
119 error_setg(errp, "There is a dump in process, please wait.");
120 return;
121 }
122
ede085b3 123 if (runstate_needs_reset()) {
f231b88d 124 error_setg(errp, "Resetting the Virtual Machine is required");
e42e818b 125 return;
ad02b96a
LC
126 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
127 return;
9183dd15
VSO
128 } else if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
129 error_setg(errp, "Migration is not finalized yet");
130 return;
e42e818b
LC
131 }
132
373340b2
HR
133 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
134 blk_iostatus_reset(blk);
ab31979a 135 }
7c8eece4 136
68d00e42
VSO
137 for (job = block_job_next(NULL); job; job = block_job_next(job)) {
138 block_job_iostatus_reset(job);
139 }
140
76b1c7fe 141 /* Continuing after completed migration. Images have been inactivated to
ace21a58
KW
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_invalidate_cache_all(&local_err);
148 if (local_err) {
149 error_propagate(errp, local_err);
150 return;
76b1c7fe
KW
151 }
152
1e998146
PB
153 if (runstate_check(RUN_STATE_INMIGRATE)) {
154 autostart = 1;
155 } else {
156 vm_start();
157 }
e42e818b 158}
b4b12c62 159
9b9df25a
GH
160void qmp_system_wakeup(Error **errp)
161{
fb064112
DHB
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);
9b9df25a
GH
169}
170
7277db91
SR
171void qmp_set_password(DisplayProtocol protocol, const char *password,
172 bool has_connected, SetPasswordAction connected,
173 Error **errp)
fbf796fd
LC
174{
175 int disconnect_if_connected = 0;
176 int fail_if_connected = 0;
177 int rc;
178
179 if (has_connected) {
7277db91
SR
180 fail_if_connected = connected == SET_PASSWORD_ACTION_FAIL;
181 disconnect_if_connected = connected == SET_PASSWORD_ACTION_DISCONNECT;
fbf796fd
LC
182 }
183
7277db91 184 if (protocol == DISPLAY_PROTOCOL_SPICE) {
b25d81ba 185 if (!qemu_using_spice(errp)) {
fbf796fd
LC
186 return;
187 }
08ad2626 188 rc = qemu_spice.set_passwd(password, fail_if_connected,
fbf796fd 189 disconnect_if_connected);
7277db91
SR
190 } else {
191 assert(protocol == DISPLAY_PROTOCOL_VNC);
fbf796fd
LC
192 if (fail_if_connected || disconnect_if_connected) {
193 /* vnc supports "connected=keep" only */
c6bd8c70 194 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
fbf796fd
LC
195 return;
196 }
197 /* Note that setting an empty password will not disable login through
198 * this interface. */
199 rc = vnc_display_password(NULL, password);
fbf796fd
LC
200 }
201
9272186d
MA
202 if (rc != 0) {
203 error_setg(errp, "Could not set password");
204 }
fbf796fd 205}
9ad5372d 206
7277db91 207void qmp_expire_password(DisplayProtocol protocol, const char *whenstr,
9ad5372d
LC
208 Error **errp)
209{
210 time_t when;
211 int rc;
212
213 if (strcmp(whenstr, "now") == 0) {
214 when = 0;
215 } else if (strcmp(whenstr, "never") == 0) {
216 when = TIME_MAX;
217 } else if (whenstr[0] == '+') {
218 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
219 } else {
220 when = strtoull(whenstr, NULL, 10);
221 }
222
7277db91 223 if (protocol == DISPLAY_PROTOCOL_SPICE) {
b25d81ba 224 if (!qemu_using_spice(errp)) {
9ad5372d
LC
225 return;
226 }
08ad2626 227 rc = qemu_spice.set_pw_expire(when);
9272186d 228 } else {
7277db91
SR
229 assert(protocol == DISPLAY_PROTOCOL_VNC);
230 rc = vnc_display_pw_expire(NULL, when);
9ad5372d
LC
231 }
232
9272186d
MA
233 if (rc != 0) {
234 error_setg(errp, "Could not set password expire time");
235 }
9ad5372d 236}
270b243f 237
333a96ec 238#ifdef CONFIG_VNC
270b243f
LC
239void qmp_change_vnc_password(const char *password, Error **errp)
240{
241 if (vnc_display_password(NULL, password) < 0) {
9272186d 242 error_setg(errp, "Could not set password");
270b243f
LC
243 }
244}
05eb4a25 245#endif
5eeee3fa 246
b224e5e2
LC
247void qmp_add_client(const char *protocol, const char *fdname,
248 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
249 Error **errp)
250{
0ec7b3e7 251 Chardev *s;
b224e5e2
LC
252 int fd;
253
947e4744 254 fd = monitor_get_fd(monitor_cur(), fdname, errp);
b224e5e2
LC
255 if (fd < 0) {
256 return;
257 }
258
259 if (strcmp(protocol, "spice") == 0) {
b25d81ba 260 if (!qemu_using_spice(errp)) {
b224e5e2
LC
261 close(fd);
262 return;
263 }
264 skipauth = has_skipauth ? skipauth : false;
265 tls = has_tls ? tls : false;
864a024c 266 if (qemu_spice.display_add_client(fd, skipauth, tls) < 0) {
b224e5e2
LC
267 error_setg(errp, "spice failed to add client");
268 close(fd);
269 }
270 return;
271#ifdef CONFIG_VNC
272 } else if (strcmp(protocol, "vnc") == 0) {
273 skipauth = has_skipauth ? skipauth : false;
274 vnc_display_add_client(NULL, fd, skipauth);
275 return;
99997823
MAL
276#endif
277#ifdef CONFIG_DBUS_DISPLAY
278 } else if (strcmp(protocol, "@dbus-display") == 0) {
279 if (!qemu_using_dbus_display(errp)) {
280 close(fd);
281 return;
282 }
283 if (!qemu_dbus_display.add_client(fd, errp)) {
284 close(fd);
285 return;
286 }
287 return;
b224e5e2
LC
288#endif
289 } else if ((s = qemu_chr_find(protocol)) != NULL) {
290 if (qemu_chr_add_client(s, fd) < 0) {
291 error_setg(errp, "failed to add client");
292 close(fd);
293 return;
294 }
295 return;
296 }
297
298 error_setg(errp, "protocol '%s' is invalid", protocol);
299 close(fd);
300}
ab2d0531 301
cff8b2c6 302
6f2e2730
IM
303MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
304{
2cc0e2e8 305 return qmp_memory_device_list();
6f2e2730 306}
02419bcb
IM
307
308ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
309{
310 bool ambig;
311 ACPIOSTInfoList *head = NULL;
312 ACPIOSTInfoList **prev = &head;
313 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
314
315 if (obj) {
316 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
317 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
318
319 adevc->ospm_status(adev, &prev);
320 } else {
321 error_setg(errp, "command is not supported, missing ACPI device");
322 }
323
324 return head;
325}
9aa3397f
VG
326
327MemoryInfo *qmp_query_memory_size_summary(Error **errp)
328{
329 MemoryInfo *mem_info = g_malloc0(sizeof(MemoryInfo));
b326b6ea 330 MachineState *ms = MACHINE(qdev_get_machine());
9aa3397f 331
b326b6ea 332 mem_info->base_memory = ms->ram_size;
9aa3397f
VG
333
334 mem_info->plugged_memory = get_plugged_memory_size();
335 mem_info->has_plugged_memory =
336 mem_info->plugged_memory != (uint64_t)-1;
337
338 return mem_info;
339}
9cc07651
ZC
340
341void qmp_display_reload(DisplayReloadOptions *arg, Error **errp)
342{
343 switch (arg->type) {
344 case DISPLAY_RELOAD_TYPE_VNC:
345#ifdef CONFIG_VNC
346 if (arg->u.vnc.has_tls_certs && arg->u.vnc.tls_certs) {
347 vnc_display_reload_certs(NULL, errp);
348 }
349#else
350 error_setg(errp, "vnc is invalid, missing 'CONFIG_VNC'");
351#endif
352 break;
353 default:
354 abort();
355 }
356}
37087fde 357
8dbbca5c
DB
358static int qmp_x_query_rdma_foreach(Object *obj, void *opaque)
359{
360 RdmaProvider *rdma;
361 RdmaProviderClass *k;
362 GString *buf = opaque;
363
364 if (object_dynamic_cast(obj, INTERFACE_RDMA_PROVIDER)) {
365 rdma = RDMA_PROVIDER(obj);
366 k = RDMA_PROVIDER_GET_CLASS(obj);
367 if (k->format_statistics) {
368 k->format_statistics(rdma, buf);
369 } else {
370 g_string_append_printf(buf,
371 "RDMA statistics not available for %s.\n",
372 object_get_typename(obj));
373 }
374 }
375
376 return 0;
377}
378
379HumanReadableText *qmp_x_query_rdma(Error **errp)
380{
381 g_autoptr(GString) buf = g_string_new("");
382
383 object_child_foreach_recursive(object_get_root(),
384 qmp_x_query_rdma_foreach, buf);
385
386 return human_readable_text_from_str(buf);
387}
ca411b7c
DB
388
389HumanReadableText *qmp_x_query_ramblock(Error **errp)
390{
391 g_autoptr(GString) buf = ram_block_format();
392
393 return human_readable_text_from_str(buf);
394}
91f2fa70
DB
395
396static int qmp_x_query_irq_foreach(Object *obj, void *opaque)
397{
398 InterruptStatsProvider *intc;
399 InterruptStatsProviderClass *k;
400 GString *buf = opaque;
401
402 if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
403 intc = INTERRUPT_STATS_PROVIDER(obj);
404 k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
405 uint64_t *irq_counts;
406 unsigned int nb_irqs, i;
407 if (k->get_statistics &&
408 k->get_statistics(intc, &irq_counts, &nb_irqs)) {
409 if (nb_irqs > 0) {
410 g_string_append_printf(buf, "IRQ statistics for %s:\n",
411 object_get_typename(obj));
412 for (i = 0; i < nb_irqs; i++) {
413 if (irq_counts[i] > 0) {
414 g_string_append_printf(buf, "%2d: %" PRId64 "\n", i,
415 irq_counts[i]);
416 }
417 }
418 }
419 } else {
420 g_string_append_printf(buf,
421 "IRQ statistics not available for %s.\n",
422 object_get_typename(obj));
423 }
424 }
425
426 return 0;
427}
428
429HumanReadableText *qmp_x_query_irq(Error **errp)
430{
431 g_autoptr(GString) buf = g_string_new("");
432
433 object_child_foreach_recursive(object_get_root(),
434 qmp_x_query_irq_foreach, buf);
435
436 return human_readable_text_from_str(buf);
437}