]> git.proxmox.com Git - mirror_qemu.git/blame - monitor/qmp-cmds.c
ui: add a D-Bus display backend
[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"
9c17d615 27#include "sysemu/kvm.h"
54d31236 28#include "sysemu/runstate.h"
e6dba048 29#include "sysemu/runstate-action.h"
9c17d615 30#include "sysemu/blockdev.h"
373340b2 31#include "sysemu/block-backend.h"
e688df6b 32#include "qapi/error.h"
27c9188f 33#include "qapi/qapi-commands-acpi.h"
5a16818b 34#include "qapi/qapi-commands-block.h"
fa4dcf57 35#include "qapi/qapi-commands-control.h"
8ac25c84 36#include "qapi/qapi-commands-machine.h"
112ed241
MA
37#include "qapi/qapi-commands-misc.h"
38#include "qapi/qapi-commands-ui.h"
37087fde 39#include "qapi/type-helpers.h"
cc7a8ea7 40#include "qapi/qmp/qerror.h"
ca411b7c 41#include "exec/ramlist.h"
2cc0e2e8 42#include "hw/mem/memory-device.h"
02419bcb 43#include "hw/acpi/acpi_dev_interface.h"
91f2fa70 44#include "hw/intc/intc.h"
8dbbca5c 45#include "hw/rdma/rdma.h"
48a32bed
AL
46
47NameInfo *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}
b9c15f16 58
292a2602
LC
59KvmInfo *qmp_query_kvm(Error **errp)
60{
61 KvmInfo *info = g_malloc0(sizeof(*info));
62
63 info->enabled = kvm_enabled();
4f9205be 64 info->present = accel_find("kvm");
292a2602
LC
65
66 return info;
67}
68
efab767e
LC
69UuidInfo *qmp_query_uuid(Error **errp)
70{
71 UuidInfo *info = g_malloc0(sizeof(*info));
efab767e 72
9c5ce8db 73 info->UUID = qemu_uuid_unparse_strdup(&qemu_uuid);
efab767e
LC
74 return info;
75}
76
7daecb30 77void qmp_quit(Error **errp)
7a7f325e 78{
e6dba048 79 shutdown_action = SHUTDOWN_ACTION_POWEROFF;
92548938 80 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP_QUIT);
7a7f325e
LC
81}
82
5f158f21
LC
83void qmp_stop(Error **errp)
84{
65d64f36
PX
85 /* if there is a dump in background, we should wait until the dump
86 * finished */
87 if (dump_in_progress()) {
88 error_setg(errp, "There is a dump in process, please wait.");
89 return;
90 }
91
1e998146
PB
92 if (runstate_check(RUN_STATE_INMIGRATE)) {
93 autostart = 0;
94 } else {
95 vm_stop(RUN_STATE_PAUSED);
96 }
5f158f21
LC
97}
98
38d22653
LC
99void qmp_system_reset(Error **errp)
100{
92548938 101 qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET);
38d22653 102}
5bc465e4 103
ec48595e 104void qmp_system_powerdown(Error **errp)
5bc465e4
LC
105{
106 qemu_system_powerdown_request();
107}
755f1968 108
e42e818b
LC
109void qmp_cont(Error **errp)
110{
373340b2 111 BlockBackend *blk;
68d00e42 112 BlockJob *job;
788cf9f8 113 Error *local_err = NULL;
e42e818b 114
65d64f36
PX
115 /* if there is a dump in background, we should wait until the dump
116 * finished */
117 if (dump_in_progress()) {
118 error_setg(errp, "There is a dump in process, please wait.");
119 return;
120 }
121
ede085b3 122 if (runstate_needs_reset()) {
f231b88d 123 error_setg(errp, "Resetting the Virtual Machine is required");
e42e818b 124 return;
ad02b96a
LC
125 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
126 return;
9183dd15
VSO
127 } else if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
128 error_setg(errp, "Migration is not finalized yet");
129 return;
e42e818b
LC
130 }
131
373340b2
HR
132 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
133 blk_iostatus_reset(blk);
ab31979a 134 }
7c8eece4 135
68d00e42
VSO
136 for (job = block_job_next(NULL); job; job = block_job_next(job)) {
137 block_job_iostatus_reset(job);
138 }
139
76b1c7fe 140 /* Continuing after completed migration. Images have been inactivated to
ace21a58
KW
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_invalidate_cache_all(&local_err);
147 if (local_err) {
148 error_propagate(errp, local_err);
149 return;
76b1c7fe
KW
150 }
151
1e998146
PB
152 if (runstate_check(RUN_STATE_INMIGRATE)) {
153 autostart = 1;
154 } else {
155 vm_start();
156 }
e42e818b 157}
b4b12c62 158
9b9df25a
GH
159void qmp_system_wakeup(Error **errp)
160{
fb064112
DHB
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);
9b9df25a
GH
168}
169
fbf796fd
LC
170void qmp_set_password(const char *protocol, const char *password,
171 bool has_connected, const char *connected, Error **errp)
172{
173 int disconnect_if_connected = 0;
174 int fail_if_connected = 0;
175 int rc;
176
177 if (has_connected) {
178 if (strcmp(connected, "fail") == 0) {
179 fail_if_connected = 1;
180 } else if (strcmp(connected, "disconnect") == 0) {
181 disconnect_if_connected = 1;
182 } else if (strcmp(connected, "keep") == 0) {
183 /* nothing */
184 } else {
c6bd8c70 185 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
fbf796fd
LC
186 return;
187 }
188 }
189
190 if (strcmp(protocol, "spice") == 0) {
b25d81ba 191 if (!qemu_using_spice(errp)) {
fbf796fd
LC
192 return;
193 }
08ad2626 194 rc = qemu_spice.set_passwd(password, fail_if_connected,
fbf796fd 195 disconnect_if_connected);
9272186d 196 } else if (strcmp(protocol, "vnc") == 0) {
fbf796fd
LC
197 if (fail_if_connected || disconnect_if_connected) {
198 /* vnc supports "connected=keep" only */
c6bd8c70 199 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
fbf796fd
LC
200 return;
201 }
202 /* Note that setting an empty password will not disable login through
203 * this interface. */
204 rc = vnc_display_password(NULL, password);
9272186d
MA
205 } else {
206 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol",
207 "'vnc' or 'spice'");
fbf796fd
LC
208 return;
209 }
210
9272186d
MA
211 if (rc != 0) {
212 error_setg(errp, "Could not set password");
213 }
fbf796fd 214}
9ad5372d
LC
215
216void qmp_expire_password(const char *protocol, const char *whenstr,
217 Error **errp)
218{
219 time_t when;
220 int rc;
221
222 if (strcmp(whenstr, "now") == 0) {
223 when = 0;
224 } else if (strcmp(whenstr, "never") == 0) {
225 when = TIME_MAX;
226 } else if (whenstr[0] == '+') {
227 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
228 } else {
229 when = strtoull(whenstr, NULL, 10);
230 }
231
232 if (strcmp(protocol, "spice") == 0) {
b25d81ba 233 if (!qemu_using_spice(errp)) {
9ad5372d
LC
234 return;
235 }
08ad2626 236 rc = qemu_spice.set_pw_expire(when);
9272186d 237 } else if (strcmp(protocol, "vnc") == 0) {
9ad5372d 238 rc = vnc_display_pw_expire(NULL, when);
9272186d
MA
239 } else {
240 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol",
241 "'vnc' or 'spice'");
9ad5372d
LC
242 return;
243 }
244
9272186d
MA
245 if (rc != 0) {
246 error_setg(errp, "Could not set password expire time");
247 }
9ad5372d 248}
270b243f 249
333a96ec 250#ifdef CONFIG_VNC
270b243f
LC
251void qmp_change_vnc_password(const char *password, Error **errp)
252{
253 if (vnc_display_password(NULL, password) < 0) {
9272186d 254 error_setg(errp, "Could not set password");
270b243f
LC
255 }
256}
05eb4a25 257#endif
5eeee3fa 258
b224e5e2
LC
259void qmp_add_client(const char *protocol, const char *fdname,
260 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
261 Error **errp)
262{
0ec7b3e7 263 Chardev *s;
b224e5e2
LC
264 int fd;
265
947e4744 266 fd = monitor_get_fd(monitor_cur(), fdname, errp);
b224e5e2
LC
267 if (fd < 0) {
268 return;
269 }
270
271 if (strcmp(protocol, "spice") == 0) {
b25d81ba 272 if (!qemu_using_spice(errp)) {
b224e5e2
LC
273 close(fd);
274 return;
275 }
276 skipauth = has_skipauth ? skipauth : false;
277 tls = has_tls ? tls : false;
864a024c 278 if (qemu_spice.display_add_client(fd, skipauth, tls) < 0) {
b224e5e2
LC
279 error_setg(errp, "spice failed to add client");
280 close(fd);
281 }
282 return;
283#ifdef CONFIG_VNC
284 } else if (strcmp(protocol, "vnc") == 0) {
285 skipauth = has_skipauth ? skipauth : false;
286 vnc_display_add_client(NULL, fd, skipauth);
287 return;
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
DB
357
358#ifdef CONFIG_PROFILER
359
360int64_t dev_time;
361
362HumanReadableText *qmp_x_query_profile(Error **errp)
363{
364 g_autoptr(GString) buf = g_string_new("");
365 static int64_t last_cpu_exec_time;
366 int64_t cpu_exec_time;
367 int64_t delta;
368
369 cpu_exec_time = tcg_cpu_exec_time();
370 delta = cpu_exec_time - last_cpu_exec_time;
371
372 g_string_append_printf(buf, "async time %" PRId64 " (%0.3f)\n",
373 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
374 g_string_append_printf(buf, "qemu time %" PRId64 " (%0.3f)\n",
375 delta, delta / (double)NANOSECONDS_PER_SECOND);
376 last_cpu_exec_time = cpu_exec_time;
377 dev_time = 0;
378
379 return human_readable_text_from_str(buf);
380}
381#else
382HumanReadableText *qmp_x_query_profile(Error **errp)
383{
384 error_setg(errp, "Internal profiler not compiled");
385 return NULL;
386}
387#endif
8dbbca5c
DB
388
389static int qmp_x_query_rdma_foreach(Object *obj, void *opaque)
390{
391 RdmaProvider *rdma;
392 RdmaProviderClass *k;
393 GString *buf = opaque;
394
395 if (object_dynamic_cast(obj, INTERFACE_RDMA_PROVIDER)) {
396 rdma = RDMA_PROVIDER(obj);
397 k = RDMA_PROVIDER_GET_CLASS(obj);
398 if (k->format_statistics) {
399 k->format_statistics(rdma, buf);
400 } else {
401 g_string_append_printf(buf,
402 "RDMA statistics not available for %s.\n",
403 object_get_typename(obj));
404 }
405 }
406
407 return 0;
408}
409
410HumanReadableText *qmp_x_query_rdma(Error **errp)
411{
412 g_autoptr(GString) buf = g_string_new("");
413
414 object_child_foreach_recursive(object_get_root(),
415 qmp_x_query_rdma_foreach, buf);
416
417 return human_readable_text_from_str(buf);
418}
ca411b7c
DB
419
420HumanReadableText *qmp_x_query_ramblock(Error **errp)
421{
422 g_autoptr(GString) buf = ram_block_format();
423
424 return human_readable_text_from_str(buf);
425}
91f2fa70
DB
426
427static int qmp_x_query_irq_foreach(Object *obj, void *opaque)
428{
429 InterruptStatsProvider *intc;
430 InterruptStatsProviderClass *k;
431 GString *buf = opaque;
432
433 if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
434 intc = INTERRUPT_STATS_PROVIDER(obj);
435 k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
436 uint64_t *irq_counts;
437 unsigned int nb_irqs, i;
438 if (k->get_statistics &&
439 k->get_statistics(intc, &irq_counts, &nb_irqs)) {
440 if (nb_irqs > 0) {
441 g_string_append_printf(buf, "IRQ statistics for %s:\n",
442 object_get_typename(obj));
443 for (i = 0; i < nb_irqs; i++) {
444 if (irq_counts[i] > 0) {
445 g_string_append_printf(buf, "%2d: %" PRId64 "\n", i,
446 irq_counts[i]);
447 }
448 }
449 }
450 } else {
451 g_string_append_printf(buf,
452 "IRQ statistics not available for %s.\n",
453 object_get_typename(obj));
454 }
455 }
456
457 return 0;
458}
459
460HumanReadableText *qmp_x_query_irq(Error **errp)
461{
462 g_autoptr(GString) buf = g_string_new("");
463
464 object_child_foreach_recursive(object_get_root(),
465 qmp_x_query_irq_foreach, buf);
466
467 return human_readable_text_from_str(buf);
468}