]> git.proxmox.com Git - mirror_qemu.git/blame - monitor/qmp-cmds.c
spice: move add_interface() to QemuSpiceOps.
[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
LC
25#include "ui/qemu-spice.h"
26#include "ui/vnc.h"
9c17d615 27#include "sysemu/kvm.h"
54d31236 28#include "sysemu/runstate.h"
9c17d615 29#include "sysemu/arch_init.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"
cc7a8ea7 39#include "qapi/qmp/qerror.h"
2cc0e2e8 40#include "hw/mem/memory-device.h"
02419bcb 41#include "hw/acpi/acpi_dev_interface.h"
48a32bed
AL
42
43NameInfo *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}
b9c15f16 54
292a2602
LC
55KvmInfo *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
efab767e
LC
65UuidInfo *qmp_query_uuid(Error **errp)
66{
67 UuidInfo *info = g_malloc0(sizeof(*info));
efab767e 68
9c5ce8db 69 info->UUID = qemu_uuid_unparse_strdup(&qemu_uuid);
efab767e
LC
70 return info;
71}
72
7daecb30 73void qmp_quit(Error **errp)
7a7f325e
LC
74{
75 no_shutdown = 0;
92548938 76 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP_QUIT);
7a7f325e
LC
77}
78
5f158f21
LC
79void qmp_stop(Error **errp)
80{
65d64f36
PX
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
1e998146
PB
88 if (runstate_check(RUN_STATE_INMIGRATE)) {
89 autostart = 0;
90 } else {
91 vm_stop(RUN_STATE_PAUSED);
92 }
5f158f21
LC
93}
94
38d22653
LC
95void qmp_system_reset(Error **errp)
96{
92548938 97 qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET);
38d22653 98}
5bc465e4 99
ec48595e 100void qmp_system_powerdown(Error **errp)
5bc465e4
LC
101{
102 qemu_system_powerdown_request();
103}
755f1968 104
361ac948 105void qmp_x_exit_preconfig(Error **errp)
047f7038
IM
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
e42e818b
LC
115void qmp_cont(Error **errp)
116{
373340b2 117 BlockBackend *blk;
68d00e42 118 BlockJob *job;
788cf9f8 119 Error *local_err = NULL;
e42e818b 120
65d64f36
PX
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
ede085b3 128 if (runstate_needs_reset()) {
f231b88d 129 error_setg(errp, "Resetting the Virtual Machine is required");
e42e818b 130 return;
ad02b96a
LC
131 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
132 return;
9183dd15
VSO
133 } else if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
134 error_setg(errp, "Migration is not finalized yet");
135 return;
e42e818b
LC
136 }
137
373340b2
HR
138 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
139 blk_iostatus_reset(blk);
ab31979a 140 }
7c8eece4 141
68d00e42
VSO
142 for (job = block_job_next(NULL); job; job = block_job_next(job)) {
143 block_job_iostatus_reset(job);
144 }
145
76b1c7fe 146 /* Continuing after completed migration. Images have been inactivated to
ace21a58
KW
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;
76b1c7fe
KW
156 }
157
1e998146
PB
158 if (runstate_check(RUN_STATE_INMIGRATE)) {
159 autostart = 1;
160 } else {
161 vm_start();
162 }
e42e818b 163}
b4b12c62 164
9b9df25a
GH
165void qmp_system_wakeup(Error **errp)
166{
fb064112
DHB
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);
9b9df25a
GH
174}
175
fbf796fd
LC
176void 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 {
c6bd8c70 191 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
fbf796fd
LC
192 return;
193 }
194 }
195
196 if (strcmp(protocol, "spice") == 0) {
b25d81ba 197 if (!qemu_using_spice(errp)) {
fbf796fd
LC
198 return;
199 }
200 rc = qemu_spice_set_passwd(password, fail_if_connected,
201 disconnect_if_connected);
202 if (rc != 0) {
c6bd8c70 203 error_setg(errp, QERR_SET_PASSWD_FAILED);
fbf796fd
LC
204 }
205 return;
206 }
207
208 if (strcmp(protocol, "vnc") == 0) {
209 if (fail_if_connected || disconnect_if_connected) {
210 /* vnc supports "connected=keep" only */
c6bd8c70 211 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
fbf796fd
LC
212 return;
213 }
214 /* Note that setting an empty password will not disable login through
215 * this interface. */
216 rc = vnc_display_password(NULL, password);
217 if (rc < 0) {
c6bd8c70 218 error_setg(errp, QERR_SET_PASSWD_FAILED);
fbf796fd
LC
219 }
220 return;
221 }
222
c6bd8c70 223 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
fbf796fd 224}
9ad5372d
LC
225
226void qmp_expire_password(const char *protocol, const char *whenstr,
227 Error **errp)
228{
229 time_t when;
230 int rc;
231
232 if (strcmp(whenstr, "now") == 0) {
233 when = 0;
234 } else if (strcmp(whenstr, "never") == 0) {
235 when = TIME_MAX;
236 } else if (whenstr[0] == '+') {
237 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
238 } else {
239 when = strtoull(whenstr, NULL, 10);
240 }
241
242 if (strcmp(protocol, "spice") == 0) {
b25d81ba 243 if (!qemu_using_spice(errp)) {
9ad5372d
LC
244 return;
245 }
246 rc = qemu_spice_set_pw_expire(when);
247 if (rc != 0) {
c6bd8c70 248 error_setg(errp, QERR_SET_PASSWD_FAILED);
9ad5372d
LC
249 }
250 return;
251 }
252
253 if (strcmp(protocol, "vnc") == 0) {
254 rc = vnc_display_pw_expire(NULL, when);
255 if (rc != 0) {
c6bd8c70 256 error_setg(errp, QERR_SET_PASSWD_FAILED);
9ad5372d
LC
257 }
258 return;
259 }
260
c6bd8c70 261 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
9ad5372d 262}
270b243f 263
333a96ec 264#ifdef CONFIG_VNC
270b243f
LC
265void qmp_change_vnc_password(const char *password, Error **errp)
266{
267 if (vnc_display_password(NULL, password) < 0) {
c6bd8c70 268 error_setg(errp, QERR_SET_PASSWD_FAILED);
270b243f
LC
269 }
270}
333a96ec 271
007fcd3e 272static void qmp_change_vnc_listen(const char *target, Error **errp)
333a96ec 273{
4db14629
GH
274 QemuOptsList *olist = qemu_find_opts("vnc");
275 QemuOpts *opts;
276
277 if (strstr(target, "id=")) {
278 error_setg(errp, "id not supported");
279 return;
280 }
281
282 opts = qemu_opts_find(olist, "default");
283 if (opts) {
284 qemu_opts_del(opts);
285 }
70b94331 286 opts = vnc_parse(target, errp);
f7801c5c
GA
287 if (!opts) {
288 return;
289 }
290
4db14629 291 vnc_display_open("default", errp);
333a96ec
LC
292}
293
294static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
295 Error **errp)
296{
297 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
298 if (!has_arg) {
c6bd8c70 299 error_setg(errp, QERR_MISSING_PARAMETER, "password");
333a96ec
LC
300 } else {
301 qmp_change_vnc_password(arg, errp);
302 }
303 } else {
304 qmp_change_vnc_listen(target, errp);
305 }
306}
333a96ec
LC
307#endif /* !CONFIG_VNC */
308
309void qmp_change(const char *device, const char *target,
7daecb30 310 bool has_arg, const char *arg, Error **errp)
333a96ec
LC
311{
312 if (strcmp(device, "vnc") == 0) {
05eb4a25 313#ifdef CONFIG_VNC
7daecb30 314 qmp_change_vnc(target, has_arg, arg, errp);
05eb4a25
MAL
315#else
316 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
317#endif
333a96ec 318 } else {
70e2cb3b
KW
319 qmp_blockdev_change_medium(true, device, false, NULL, target,
320 has_arg, arg, false, 0, errp);
333a96ec
LC
321 }
322}
5eeee3fa 323
b224e5e2
LC
324void qmp_add_client(const char *protocol, const char *fdname,
325 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
326 Error **errp)
327{
0ec7b3e7 328 Chardev *s;
b224e5e2
LC
329 int fd;
330
947e4744 331 fd = monitor_get_fd(monitor_cur(), fdname, errp);
b224e5e2
LC
332 if (fd < 0) {
333 return;
334 }
335
336 if (strcmp(protocol, "spice") == 0) {
b25d81ba 337 if (!qemu_using_spice(errp)) {
b224e5e2
LC
338 close(fd);
339 return;
340 }
341 skipauth = has_skipauth ? skipauth : false;
342 tls = has_tls ? tls : false;
343 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
344 error_setg(errp, "spice failed to add client");
345 close(fd);
346 }
347 return;
348#ifdef CONFIG_VNC
349 } else if (strcmp(protocol, "vnc") == 0) {
350 skipauth = has_skipauth ? skipauth : false;
351 vnc_display_add_client(NULL, fd, skipauth);
352 return;
353#endif
354 } else if ((s = qemu_chr_find(protocol)) != NULL) {
355 if (qemu_chr_add_client(s, fd) < 0) {
356 error_setg(errp, "failed to add client");
357 close(fd);
358 return;
359 }
360 return;
361 }
362
363 error_setg(errp, "protocol '%s' is invalid", protocol);
364 close(fd);
365}
ab2d0531 366
cff8b2c6 367
6f2e2730
IM
368MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
369{
2cc0e2e8 370 return qmp_memory_device_list();
6f2e2730 371}
02419bcb
IM
372
373ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
374{
375 bool ambig;
376 ACPIOSTInfoList *head = NULL;
377 ACPIOSTInfoList **prev = &head;
378 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
379
380 if (obj) {
381 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
382 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
383
384 adevc->ospm_status(adev, &prev);
385 } else {
386 error_setg(errp, "command is not supported, missing ACPI device");
387 }
388
389 return head;
390}
9aa3397f
VG
391
392MemoryInfo *qmp_query_memory_size_summary(Error **errp)
393{
394 MemoryInfo *mem_info = g_malloc0(sizeof(MemoryInfo));
395
396 mem_info->base_memory = ram_size;
397
398 mem_info->plugged_memory = get_plugged_memory_size();
399 mem_info->has_plugged_memory =
400 mem_info->plugged_memory != (uint64_t)-1;
401
402 return mem_info;
403}