]> git.proxmox.com Git - mirror_qemu.git/blame - monitor/qmp-cmds.c
Remove qemu-common.h include from most units
[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"
f348b6d1 17#include "qemu/cutils.h"
922a01a0 18#include "qemu/option.h"
a0b1a66e 19#include "monitor/monitor.h"
9c17d615 20#include "sysemu/sysemu.h"
213dcb06 21#include "qemu/config-file.h"
cea25275 22#include "qemu/uuid.h"
8228e353 23#include "chardev/char.h"
fbf796fd 24#include "ui/qemu-spice.h"
fb246f05 25#include "ui/console.h"
99997823 26#include "ui/dbus-display.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 */
544803c7 87 if (qemu_system_dump_in_progress()) {
65d64f36
PX
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 */
544803c7 117 if (qemu_system_dump_in_progress()) {
65d64f36
PX
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. */
3b717194 146 bdrv_activate_all(&local_err);
ace21a58
KW
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
675fd3c9 170void qmp_set_password(SetPasswordOptions *opts, Error **errp)
fbf796fd 171{
fbf796fd
LC
172 int rc;
173
675fd3c9 174 if (opts->protocol == DISPLAY_PROTOCOL_SPICE) {
b25d81ba 175 if (!qemu_using_spice(errp)) {
fbf796fd
LC
176 return;
177 }
675fd3c9
SR
178 rc = qemu_spice.set_passwd(opts->password,
179 opts->connected == SET_PASSWORD_ACTION_FAIL,
180 opts->connected == SET_PASSWORD_ACTION_DISCONNECT);
7277db91 181 } else {
675fd3c9
SR
182 assert(opts->protocol == DISPLAY_PROTOCOL_VNC);
183 if (opts->connected != SET_PASSWORD_ACTION_KEEP) {
fbf796fd 184 /* vnc supports "connected=keep" only */
c6bd8c70 185 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
fbf796fd
LC
186 return;
187 }
188 /* Note that setting an empty password will not disable login through
189 * this interface. */
675fd3c9 190 rc = vnc_display_password(opts->u.vnc.display, opts->password);
fbf796fd
LC
191 }
192
9272186d
MA
193 if (rc != 0) {
194 error_setg(errp, "Could not set password");
195 }
fbf796fd 196}
9ad5372d 197
675fd3c9 198void qmp_expire_password(ExpirePasswordOptions *opts, Error **errp)
9ad5372d
LC
199{
200 time_t when;
201 int rc;
675fd3c9 202 const char *whenstr = opts->time;
9ad5372d
LC
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
675fd3c9 214 if (opts->protocol == DISPLAY_PROTOCOL_SPICE) {
b25d81ba 215 if (!qemu_using_spice(errp)) {
9ad5372d
LC
216 return;
217 }
08ad2626 218 rc = qemu_spice.set_pw_expire(when);
9272186d 219 } else {
675fd3c9
SR
220 assert(opts->protocol == DISPLAY_PROTOCOL_VNC);
221 rc = vnc_display_pw_expire(opts->u.vnc.display, when);
9ad5372d
LC
222 }
223
9272186d
MA
224 if (rc != 0) {
225 error_setg(errp, "Could not set password expire time");
226 }
9ad5372d 227}
270b243f 228
333a96ec 229#ifdef CONFIG_VNC
270b243f
LC
230void qmp_change_vnc_password(const char *password, Error **errp)
231{
232 if (vnc_display_password(NULL, password) < 0) {
9272186d 233 error_setg(errp, "Could not set password");
270b243f
LC
234 }
235}
05eb4a25 236#endif
5eeee3fa 237
b224e5e2
LC
238void qmp_add_client(const char *protocol, const char *fdname,
239 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
240 Error **errp)
241{
0ec7b3e7 242 Chardev *s;
b224e5e2
LC
243 int fd;
244
947e4744 245 fd = monitor_get_fd(monitor_cur(), fdname, errp);
b224e5e2
LC
246 if (fd < 0) {
247 return;
248 }
249
250 if (strcmp(protocol, "spice") == 0) {
b25d81ba 251 if (!qemu_using_spice(errp)) {
b224e5e2
LC
252 close(fd);
253 return;
254 }
255 skipauth = has_skipauth ? skipauth : false;
256 tls = has_tls ? tls : false;
864a024c 257 if (qemu_spice.display_add_client(fd, skipauth, tls) < 0) {
b224e5e2
LC
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;
99997823
MAL
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;
b224e5e2
LC
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}
ab2d0531 292
cff8b2c6 293
6f2e2730
IM
294MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
295{
2cc0e2e8 296 return qmp_memory_device_list();
6f2e2730 297}
02419bcb
IM
298
299ACPIOSTInfoList *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}
9aa3397f
VG
317
318MemoryInfo *qmp_query_memory_size_summary(Error **errp)
319{
b21e2380 320 MemoryInfo *mem_info = g_new0(MemoryInfo, 1);
b326b6ea 321 MachineState *ms = MACHINE(qdev_get_machine());
9aa3397f 322
b326b6ea 323 mem_info->base_memory = ms->ram_size;
9aa3397f
VG
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}
9cc07651
ZC
331
332void 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}
37087fde 348
8dbbca5c
DB
349static 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
370HumanReadableText *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}
ca411b7c
DB
379
380HumanReadableText *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}
91f2fa70
DB
386
387static 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
420HumanReadableText *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}