]> git.proxmox.com Git - mirror_qemu.git/blame - qmp.c
block: resize backing image during active layer commit, if needed
[mirror_qemu.git] / qmp.c
CommitLineData
48a32bed
AL
1/*
2 * QEMU Management Protocol
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
16#include "qemu-common.h"
9c17d615 17#include "sysemu/sysemu.h"
48a32bed 18#include "qmp-commands.h"
dccfcd0e 19#include "sysemu/char.h"
fbf796fd
LC
20#include "ui/qemu-spice.h"
21#include "ui/vnc.h"
9c17d615
PB
22#include "sysemu/kvm.h"
23#include "sysemu/arch_init.h"
b4b12c62 24#include "hw/qdev.h"
9c17d615 25#include "sysemu/blockdev.h"
14cccb61 26#include "qom/qom-qobject.h"
cff8b2c6
PB
27#include "qapi/qmp/qobject.h"
28#include "qapi/qmp-input-visitor.h"
69ca3ea5 29#include "hw/boards.h"
48a32bed
AL
30
31NameInfo *qmp_query_name(Error **errp)
32{
33 NameInfo *info = g_malloc0(sizeof(*info));
34
35 if (qemu_name) {
36 info->has_name = true;
37 info->name = g_strdup(qemu_name);
38 }
39
40 return info;
41}
b9c15f16
LC
42
43VersionInfo *qmp_query_version(Error **err)
44{
45 VersionInfo *info = g_malloc0(sizeof(*info));
46 const char *version = QEMU_VERSION;
47 char *tmp;
48
49 info->qemu.major = strtol(version, &tmp, 10);
50 tmp++;
51 info->qemu.minor = strtol(tmp, &tmp, 10);
52 tmp++;
53 info->qemu.micro = strtol(tmp, &tmp, 10);
54 info->package = g_strdup(QEMU_PKGVERSION);
55
56 return info;
57}
292a2602
LC
58
59KvmInfo *qmp_query_kvm(Error **errp)
60{
61 KvmInfo *info = g_malloc0(sizeof(*info));
62
63 info->enabled = kvm_enabled();
64 info->present = kvm_available();
65
66 return info;
67}
68
efab767e
LC
69UuidInfo *qmp_query_uuid(Error **errp)
70{
71 UuidInfo *info = g_malloc0(sizeof(*info));
72 char uuid[64];
73
74 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
75 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
76 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
77 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
78 qemu_uuid[14], qemu_uuid[15]);
79
80 info->UUID = g_strdup(uuid);
81 return info;
82}
83
7a7f325e
LC
84void qmp_quit(Error **err)
85{
86 no_shutdown = 0;
87 qemu_system_shutdown_request();
88}
89
5f158f21
LC
90void qmp_stop(Error **errp)
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{
101 qemu_system_reset_request();
102}
5bc465e4
LC
103
104void qmp_system_powerdown(Error **erp)
105{
106 qemu_system_powerdown_request();
107}
755f1968
LC
108
109void qmp_cpu(int64_t index, Error **errp)
110{
111 /* Just do nothing */
112}
2b54aa87 113
69ca3ea5
IM
114void qmp_cpu_add(int64_t id, Error **errp)
115{
116 if (current_machine->hot_add_cpu) {
117 current_machine->hot_add_cpu(id, errp);
118 } else {
119 error_setg(errp, "Not supported");
120 }
121}
122
2b54aa87
LC
123#ifndef CONFIG_VNC
124/* If VNC support is enabled, the "true" query-vnc command is
125 defined in the VNC subsystem */
126VncInfo *qmp_query_vnc(Error **errp)
127{
128 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
129 return NULL;
130};
131#endif
d1f29646
LC
132
133#ifndef CONFIG_SPICE
134/* If SPICE support is enabled, the "true" query-spice command is
135 defined in the SPICE subsystem. Also note that we use a small
136 trick to maintain query-spice's original behavior, which is not
137 to be available in the namespace if SPICE is not compiled in */
138SpiceInfo *qmp_query_spice(Error **errp)
139{
140 error_set(errp, QERR_COMMAND_NOT_FOUND, "query-spice");
141 return NULL;
142};
143#endif
e42e818b
LC
144
145static void iostatus_bdrv_it(void *opaque, BlockDriverState *bs)
146{
147 bdrv_iostatus_reset(bs);
148}
149
150static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
151{
152 Error **err = opaque;
153
154 if (!error_is_set(err) && bdrv_key_required(bs)) {
903a8814
LC
155 error_set(err, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
156 bdrv_get_encrypted_filename(bs));
e42e818b
LC
157 }
158}
159
160void qmp_cont(Error **errp)
161{
162 Error *local_err = NULL;
163
ede085b3 164 if (runstate_needs_reset()) {
e42e818b
LC
165 error_set(errp, QERR_RESET_REQUIRED);
166 return;
ad02b96a
LC
167 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
168 return;
e42e818b
LC
169 }
170
171 bdrv_iterate(iostatus_bdrv_it, NULL);
172 bdrv_iterate(encrypted_bdrv_it, &local_err);
173 if (local_err) {
174 error_propagate(errp, local_err);
175 return;
176 }
177
1e998146
PB
178 if (runstate_check(RUN_STATE_INMIGRATE)) {
179 autostart = 1;
180 } else {
181 vm_start();
182 }
e42e818b 183}
b4b12c62 184
9b9df25a
GH
185void qmp_system_wakeup(Error **errp)
186{
187 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
188}
189
57c9fafe 190ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
b4b12c62 191{
57c9fafe 192 Object *obj;
b4b12c62 193 bool ambiguous = false;
57c9fafe
AL
194 ObjectPropertyInfoList *props = NULL;
195 ObjectProperty *prop;
b4b12c62 196
57c9fafe
AL
197 obj = object_resolve_path(path, &ambiguous);
198 if (obj == NULL) {
b4b12c62
AL
199 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
200 return NULL;
201 }
202
57c9fafe
AL
203 QTAILQ_FOREACH(prop, &obj->properties, node) {
204 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
b4b12c62 205
57c9fafe 206 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
b4b12c62
AL
207 entry->next = props;
208 props = entry;
209
210 entry->value->name = g_strdup(prop->name);
211 entry->value->type = g_strdup(prop->type);
212 }
213
214 return props;
215}
eb6e8ea5
AL
216
217/* FIXME: teach qapi about how to pass through Visitors */
218int qmp_qom_set(Monitor *mon, const QDict *qdict, QObject **ret)
219{
220 const char *path = qdict_get_str(qdict, "path");
221 const char *property = qdict_get_str(qdict, "property");
222 QObject *value = qdict_get(qdict, "value");
223 Error *local_err = NULL;
57c9fafe 224 Object *obj;
eb6e8ea5 225
57c9fafe
AL
226 obj = object_resolve_path(path, NULL);
227 if (!obj) {
eb6e8ea5
AL
228 error_set(&local_err, QERR_DEVICE_NOT_FOUND, path);
229 goto out;
230 }
231
9f5f1350 232 object_property_set_qobject(obj, value, property, &local_err);
eb6e8ea5
AL
233
234out:
235 if (local_err) {
236 qerror_report_err(local_err);
237 error_free(local_err);
238 return -1;
239 }
240
241 return 0;
242}
243
244int qmp_qom_get(Monitor *mon, const QDict *qdict, QObject **ret)
245{
246 const char *path = qdict_get_str(qdict, "path");
247 const char *property = qdict_get_str(qdict, "property");
248 Error *local_err = NULL;
57c9fafe 249 Object *obj;
eb6e8ea5 250
57c9fafe
AL
251 obj = object_resolve_path(path, NULL);
252 if (!obj) {
eb6e8ea5
AL
253 error_set(&local_err, QERR_DEVICE_NOT_FOUND, path);
254 goto out;
255 }
256
9f5f1350 257 *ret = object_property_get_qobject(obj, property, &local_err);
eb6e8ea5
AL
258
259out:
260 if (local_err) {
261 qerror_report_err(local_err);
262 error_free(local_err);
263 return -1;
264 }
265
266 return 0;
267}
fbf796fd
LC
268
269void qmp_set_password(const char *protocol, const char *password,
270 bool has_connected, const char *connected, Error **errp)
271{
272 int disconnect_if_connected = 0;
273 int fail_if_connected = 0;
274 int rc;
275
276 if (has_connected) {
277 if (strcmp(connected, "fail") == 0) {
278 fail_if_connected = 1;
279 } else if (strcmp(connected, "disconnect") == 0) {
280 disconnect_if_connected = 1;
281 } else if (strcmp(connected, "keep") == 0) {
282 /* nothing */
283 } else {
284 error_set(errp, QERR_INVALID_PARAMETER, "connected");
285 return;
286 }
287 }
288
289 if (strcmp(protocol, "spice") == 0) {
290 if (!using_spice) {
291 /* correct one? spice isn't a device ,,, */
292 error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice");
293 return;
294 }
295 rc = qemu_spice_set_passwd(password, fail_if_connected,
296 disconnect_if_connected);
297 if (rc != 0) {
298 error_set(errp, QERR_SET_PASSWD_FAILED);
299 }
300 return;
301 }
302
303 if (strcmp(protocol, "vnc") == 0) {
304 if (fail_if_connected || disconnect_if_connected) {
305 /* vnc supports "connected=keep" only */
306 error_set(errp, QERR_INVALID_PARAMETER, "connected");
307 return;
308 }
309 /* Note that setting an empty password will not disable login through
310 * this interface. */
311 rc = vnc_display_password(NULL, password);
312 if (rc < 0) {
313 error_set(errp, QERR_SET_PASSWD_FAILED);
314 }
315 return;
316 }
317
318 error_set(errp, QERR_INVALID_PARAMETER, "protocol");
319}
9ad5372d
LC
320
321void qmp_expire_password(const char *protocol, const char *whenstr,
322 Error **errp)
323{
324 time_t when;
325 int rc;
326
327 if (strcmp(whenstr, "now") == 0) {
328 when = 0;
329 } else if (strcmp(whenstr, "never") == 0) {
330 when = TIME_MAX;
331 } else if (whenstr[0] == '+') {
332 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
333 } else {
334 when = strtoull(whenstr, NULL, 10);
335 }
336
337 if (strcmp(protocol, "spice") == 0) {
338 if (!using_spice) {
339 /* correct one? spice isn't a device ,,, */
340 error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice");
341 return;
342 }
343 rc = qemu_spice_set_pw_expire(when);
344 if (rc != 0) {
345 error_set(errp, QERR_SET_PASSWD_FAILED);
346 }
347 return;
348 }
349
350 if (strcmp(protocol, "vnc") == 0) {
351 rc = vnc_display_pw_expire(NULL, when);
352 if (rc != 0) {
353 error_set(errp, QERR_SET_PASSWD_FAILED);
354 }
355 return;
356 }
357
358 error_set(errp, QERR_INVALID_PARAMETER, "protocol");
359}
270b243f 360
333a96ec 361#ifdef CONFIG_VNC
270b243f
LC
362void qmp_change_vnc_password(const char *password, Error **errp)
363{
364 if (vnc_display_password(NULL, password) < 0) {
365 error_set(errp, QERR_SET_PASSWD_FAILED);
366 }
367}
333a96ec 368
007fcd3e 369static void qmp_change_vnc_listen(const char *target, Error **errp)
333a96ec 370{
007fcd3e 371 vnc_display_open(NULL, target, errp);
333a96ec
LC
372}
373
374static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
375 Error **errp)
376{
377 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
378 if (!has_arg) {
379 error_set(errp, QERR_MISSING_PARAMETER, "password");
380 } else {
381 qmp_change_vnc_password(arg, errp);
382 }
383 } else {
384 qmp_change_vnc_listen(target, errp);
385 }
386}
387#else
388void qmp_change_vnc_password(const char *password, Error **errp)
389{
390 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
391}
392static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
393 Error **errp)
394{
395 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
396}
397#endif /* !CONFIG_VNC */
398
399void qmp_change(const char *device, const char *target,
400 bool has_arg, const char *arg, Error **err)
401{
402 if (strcmp(device, "vnc") == 0) {
403 qmp_change_vnc(target, has_arg, arg, err);
404 } else {
314f7ea7 405 qmp_change_blockdev(device, target, arg, err);
333a96ec
LC
406 }
407}
5eeee3fa
AL
408
409static void qom_list_types_tramp(ObjectClass *klass, void *data)
410{
411 ObjectTypeInfoList *e, **pret = data;
412 ObjectTypeInfo *info;
413
414 info = g_malloc0(sizeof(*info));
415 info->name = g_strdup(object_class_get_name(klass));
416
417 e = g_malloc0(sizeof(*e));
418 e->value = info;
419 e->next = *pret;
420 *pret = e;
421}
422
423ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
424 const char *implements,
425 bool has_abstract,
426 bool abstract,
427 Error **errp)
428{
429 ObjectTypeInfoList *ret = NULL;
430
431 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
432
433 return ret;
434}
1daa31b9
AL
435
436DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
437 Error **errp)
438{
439 ObjectClass *klass;
440 Property *prop;
441 DevicePropertyInfoList *prop_list = NULL;
442
443 klass = object_class_by_name(typename);
444 if (klass == NULL) {
445 error_set(errp, QERR_DEVICE_NOT_FOUND, typename);
446 return NULL;
447 }
448
449 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
450 if (klass == NULL) {
451 error_set(errp, QERR_INVALID_PARAMETER_VALUE,
452 "name", TYPE_DEVICE);
453 return NULL;
454 }
455
456 do {
457 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
458 DevicePropertyInfoList *entry;
459 DevicePropertyInfo *info;
460
461 /*
462 * TODO Properties without a parser are just for dirty hacks.
463 * qdev_prop_ptr is the only such PropertyInfo. It's marked
464 * for removal. This conditional should be removed along with
465 * it.
466 */
467 if (!prop->info->set) {
468 continue; /* no way to set it, don't show */
469 }
470
471 info = g_malloc0(sizeof(*info));
472 info->name = g_strdup(prop->name);
473 info->type = g_strdup(prop->info->legacy_name ?: prop->info->name);
474
475 entry = g_malloc0(sizeof(*entry));
476 entry->value = info;
477 entry->next = prop_list;
478 prop_list = entry;
479 }
480 klass = object_class_get_parent(klass);
481 } while (klass != object_class_by_name(TYPE_DEVICE));
482
483 return prop_list;
484}
e4e31c63 485
76b64a7a
AL
486CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
487{
488 return arch_query_cpu_definitions(errp);
489}
490
b224e5e2
LC
491void qmp_add_client(const char *protocol, const char *fdname,
492 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
493 Error **errp)
494{
495 CharDriverState *s;
496 int fd;
497
498 fd = monitor_get_fd(cur_mon, fdname, errp);
499 if (fd < 0) {
500 return;
501 }
502
503 if (strcmp(protocol, "spice") == 0) {
504 if (!using_spice) {
505 error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice");
506 close(fd);
507 return;
508 }
509 skipauth = has_skipauth ? skipauth : false;
510 tls = has_tls ? tls : false;
511 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
512 error_setg(errp, "spice failed to add client");
513 close(fd);
514 }
515 return;
516#ifdef CONFIG_VNC
517 } else if (strcmp(protocol, "vnc") == 0) {
518 skipauth = has_skipauth ? skipauth : false;
519 vnc_display_add_client(NULL, fd, skipauth);
520 return;
521#endif
522 } else if ((s = qemu_chr_find(protocol)) != NULL) {
523 if (qemu_chr_add_client(s, fd) < 0) {
524 error_setg(errp, "failed to add client");
525 close(fd);
526 return;
527 }
528 return;
529 }
530
531 error_setg(errp, "protocol '%s' is invalid", protocol);
532 close(fd);
533}
ab2d0531 534
cff8b2c6
PB
535void object_add(const char *type, const char *id, const QDict *qdict,
536 Visitor *v, Error **errp)
537{
538 Object *obj;
539 const QDictEntry *e;
540 Error *local_err = NULL;
541
542 if (!object_class_by_name(type)) {
543 error_setg(errp, "invalid class name");
544 return;
545 }
546
547 obj = object_new(type);
548 if (qdict) {
549 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
550 object_property_set(obj, v, e->key, &local_err);
551 if (local_err) {
552 error_propagate(errp, local_err);
553 object_unref(obj);
554 return;
555 }
556 }
557 }
558
559 object_property_add_child(container_get(object_get_root(), "/objects"),
560 id, obj, errp);
561 object_unref(obj);
562}
563
564int qmp_object_add(Monitor *mon, const QDict *qdict, QObject **ret)
565{
566 const char *type = qdict_get_str(qdict, "qom-type");
567 const char *id = qdict_get_str(qdict, "id");
568 QObject *props = qdict_get(qdict, "props");
569 const QDict *pdict = NULL;
570 Error *local_err = NULL;
571 QmpInputVisitor *qiv;
572
573 if (props) {
574 pdict = qobject_to_qdict(props);
575 if (!pdict) {
576 error_set(&local_err, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
577 goto out;
578 }
579 }
580
581 qiv = qmp_input_visitor_new(props);
582 object_add(type, id, pdict, qmp_input_get_visitor(qiv), &local_err);
583 qmp_input_visitor_cleanup(qiv);
584
585out:
586 if (local_err) {
587 qerror_report_err(local_err);
588 error_free(local_err);
589 return -1;
590 }
591
592 return 0;
593}
594
ab2d0531
PB
595void qmp_object_del(const char *id, Error **errp)
596{
597 Object *container;
598 Object *obj;
599
600 container = container_get(object_get_root(), "/objects");
601 obj = object_resolve_path_component(container, id);
602 if (!obj) {
603 error_setg(errp, "object id not found");
604 return;
605 }
606 object_unparent(obj);
607}