]> git.proxmox.com Git - mirror_qemu.git/blob - qmp.c
qemu-iotests: Allow caller to disable underscore convertion for qmp
[mirror_qemu.git] / qmp.c
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 *
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.
14 */
15
16 #include "qemu-common.h"
17 #include "sysemu/sysemu.h"
18 #include "qmp-commands.h"
19 #include "sysemu/char.h"
20 #include "ui/qemu-spice.h"
21 #include "ui/vnc.h"
22 #include "sysemu/kvm.h"
23 #include "sysemu/arch_init.h"
24 #include "hw/qdev.h"
25 #include "sysemu/blockdev.h"
26 #include "qom/qom-qobject.h"
27 #include "qapi/qmp/qobject.h"
28 #include "qapi/qmp-input-visitor.h"
29 #include "hw/boards.h"
30 #include "qom/object_interfaces.h"
31 #include "hw/mem/pc-dimm.h"
32 #include "hw/acpi/acpi_dev_interface.h"
33
34 NameInfo *qmp_query_name(Error **errp)
35 {
36 NameInfo *info = g_malloc0(sizeof(*info));
37
38 if (qemu_name) {
39 info->has_name = true;
40 info->name = g_strdup(qemu_name);
41 }
42
43 return info;
44 }
45
46 VersionInfo *qmp_query_version(Error **errp)
47 {
48 VersionInfo *info = g_malloc0(sizeof(*info));
49 const char *version = QEMU_VERSION;
50 char *tmp;
51
52 info->qemu.major = strtol(version, &tmp, 10);
53 tmp++;
54 info->qemu.minor = strtol(tmp, &tmp, 10);
55 tmp++;
56 info->qemu.micro = strtol(tmp, &tmp, 10);
57 info->package = g_strdup(QEMU_PKGVERSION);
58
59 return info;
60 }
61
62 KvmInfo *qmp_query_kvm(Error **errp)
63 {
64 KvmInfo *info = g_malloc0(sizeof(*info));
65
66 info->enabled = kvm_enabled();
67 info->present = kvm_available();
68
69 return info;
70 }
71
72 UuidInfo *qmp_query_uuid(Error **errp)
73 {
74 UuidInfo *info = g_malloc0(sizeof(*info));
75 char uuid[64];
76
77 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
78 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
79 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
80 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
81 qemu_uuid[14], qemu_uuid[15]);
82
83 info->UUID = g_strdup(uuid);
84 return info;
85 }
86
87 void qmp_quit(Error **errp)
88 {
89 no_shutdown = 0;
90 qemu_system_shutdown_request();
91 }
92
93 void qmp_stop(Error **errp)
94 {
95 if (runstate_check(RUN_STATE_INMIGRATE)) {
96 autostart = 0;
97 } else {
98 vm_stop(RUN_STATE_PAUSED);
99 }
100 }
101
102 void qmp_system_reset(Error **errp)
103 {
104 qemu_system_reset_request();
105 }
106
107 void qmp_system_powerdown(Error **erp)
108 {
109 qemu_system_powerdown_request();
110 }
111
112 void qmp_cpu(int64_t index, Error **errp)
113 {
114 /* Just do nothing */
115 }
116
117 void qmp_cpu_add(int64_t id, Error **errp)
118 {
119 MachineClass *mc;
120
121 mc = MACHINE_GET_CLASS(current_machine);
122 if (mc->hot_add_cpu) {
123 mc->hot_add_cpu(id, errp);
124 } else {
125 error_setg(errp, "Not supported");
126 }
127 }
128
129 #ifndef CONFIG_VNC
130 /* If VNC support is enabled, the "true" query-vnc command is
131 defined in the VNC subsystem */
132 VncInfo *qmp_query_vnc(Error **errp)
133 {
134 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
135 return NULL;
136 };
137
138 VncInfo2List *qmp_query_vnc_servers(Error **errp)
139 {
140 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
141 return NULL;
142 };
143 #endif
144
145 #ifndef CONFIG_SPICE
146 /*
147 * qmp-commands.hx ensures that QMP command query-spice exists only
148 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
149 * result. However, the QAPI schema is blissfully unaware of that,
150 * and the QAPI code generator happily generates a dead
151 * qmp_marshal_input_query_spice() that calls qmp_query_spice().
152 * Provide it one, or else linking fails.
153 * FIXME Educate the QAPI schema on CONFIG_SPICE.
154 */
155 SpiceInfo *qmp_query_spice(Error **errp)
156 {
157 abort();
158 };
159 #endif
160
161 void qmp_cont(Error **errp)
162 {
163 Error *local_err = NULL;
164 BlockDriverState *bs;
165
166 if (runstate_needs_reset()) {
167 error_setg(errp, "Resetting the Virtual Machine is required");
168 return;
169 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
170 return;
171 }
172
173 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
174 bdrv_iostatus_reset(bs);
175 }
176 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
177 bdrv_add_key(bs, NULL, &local_err);
178 if (local_err) {
179 error_propagate(errp, local_err);
180 return;
181 }
182 }
183
184 if (runstate_check(RUN_STATE_INMIGRATE)) {
185 autostart = 1;
186 } else {
187 vm_start();
188 }
189 }
190
191 void qmp_system_wakeup(Error **errp)
192 {
193 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
194 }
195
196 ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
197 {
198 Object *obj;
199 bool ambiguous = false;
200 ObjectPropertyInfoList *props = NULL;
201 ObjectProperty *prop;
202
203 obj = object_resolve_path(path, &ambiguous);
204 if (obj == NULL) {
205 if (ambiguous) {
206 error_setg(errp, "Path '%s' is ambiguous", path);
207 } else {
208 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
209 }
210 return NULL;
211 }
212
213 QTAILQ_FOREACH(prop, &obj->properties, node) {
214 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
215
216 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
217 entry->next = props;
218 props = entry;
219
220 entry->value->name = g_strdup(prop->name);
221 entry->value->type = g_strdup(prop->type);
222 }
223
224 return props;
225 }
226
227 /* FIXME: teach qapi about how to pass through Visitors */
228 int qmp_qom_set(Monitor *mon, const QDict *qdict, QObject **ret)
229 {
230 const char *path = qdict_get_str(qdict, "path");
231 const char *property = qdict_get_str(qdict, "property");
232 QObject *value = qdict_get(qdict, "value");
233 Error *local_err = NULL;
234 Object *obj;
235
236 obj = object_resolve_path(path, NULL);
237 if (!obj) {
238 error_set(&local_err, QERR_DEVICE_NOT_FOUND, path);
239 goto out;
240 }
241
242 object_property_set_qobject(obj, value, property, &local_err);
243
244 out:
245 if (local_err) {
246 qerror_report_err(local_err);
247 error_free(local_err);
248 return -1;
249 }
250
251 return 0;
252 }
253
254 int qmp_qom_get(Monitor *mon, const QDict *qdict, QObject **ret)
255 {
256 const char *path = qdict_get_str(qdict, "path");
257 const char *property = qdict_get_str(qdict, "property");
258 Error *local_err = NULL;
259 Object *obj;
260
261 obj = object_resolve_path(path, NULL);
262 if (!obj) {
263 error_set(&local_err, QERR_DEVICE_NOT_FOUND, path);
264 goto out;
265 }
266
267 *ret = object_property_get_qobject(obj, property, &local_err);
268
269 out:
270 if (local_err) {
271 qerror_report_err(local_err);
272 error_free(local_err);
273 return -1;
274 }
275
276 return 0;
277 }
278
279 void qmp_set_password(const char *protocol, const char *password,
280 bool has_connected, const char *connected, Error **errp)
281 {
282 int disconnect_if_connected = 0;
283 int fail_if_connected = 0;
284 int rc;
285
286 if (has_connected) {
287 if (strcmp(connected, "fail") == 0) {
288 fail_if_connected = 1;
289 } else if (strcmp(connected, "disconnect") == 0) {
290 disconnect_if_connected = 1;
291 } else if (strcmp(connected, "keep") == 0) {
292 /* nothing */
293 } else {
294 error_set(errp, QERR_INVALID_PARAMETER, "connected");
295 return;
296 }
297 }
298
299 if (strcmp(protocol, "spice") == 0) {
300 if (!qemu_using_spice(errp)) {
301 return;
302 }
303 rc = qemu_spice_set_passwd(password, fail_if_connected,
304 disconnect_if_connected);
305 if (rc != 0) {
306 error_set(errp, QERR_SET_PASSWD_FAILED);
307 }
308 return;
309 }
310
311 if (strcmp(protocol, "vnc") == 0) {
312 if (fail_if_connected || disconnect_if_connected) {
313 /* vnc supports "connected=keep" only */
314 error_set(errp, QERR_INVALID_PARAMETER, "connected");
315 return;
316 }
317 /* Note that setting an empty password will not disable login through
318 * this interface. */
319 rc = vnc_display_password(NULL, password);
320 if (rc < 0) {
321 error_set(errp, QERR_SET_PASSWD_FAILED);
322 }
323 return;
324 }
325
326 error_set(errp, QERR_INVALID_PARAMETER, "protocol");
327 }
328
329 void qmp_expire_password(const char *protocol, const char *whenstr,
330 Error **errp)
331 {
332 time_t when;
333 int rc;
334
335 if (strcmp(whenstr, "now") == 0) {
336 when = 0;
337 } else if (strcmp(whenstr, "never") == 0) {
338 when = TIME_MAX;
339 } else if (whenstr[0] == '+') {
340 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
341 } else {
342 when = strtoull(whenstr, NULL, 10);
343 }
344
345 if (strcmp(protocol, "spice") == 0) {
346 if (!qemu_using_spice(errp)) {
347 return;
348 }
349 rc = qemu_spice_set_pw_expire(when);
350 if (rc != 0) {
351 error_set(errp, QERR_SET_PASSWD_FAILED);
352 }
353 return;
354 }
355
356 if (strcmp(protocol, "vnc") == 0) {
357 rc = vnc_display_pw_expire(NULL, when);
358 if (rc != 0) {
359 error_set(errp, QERR_SET_PASSWD_FAILED);
360 }
361 return;
362 }
363
364 error_set(errp, QERR_INVALID_PARAMETER, "protocol");
365 }
366
367 #ifdef CONFIG_VNC
368 void qmp_change_vnc_password(const char *password, Error **errp)
369 {
370 if (vnc_display_password(NULL, password) < 0) {
371 error_set(errp, QERR_SET_PASSWD_FAILED);
372 }
373 }
374
375 static void qmp_change_vnc_listen(const char *target, Error **errp)
376 {
377 QemuOptsList *olist = qemu_find_opts("vnc");
378 QemuOpts *opts;
379
380 if (strstr(target, "id=")) {
381 error_setg(errp, "id not supported");
382 return;
383 }
384
385 opts = qemu_opts_find(olist, "default");
386 if (opts) {
387 qemu_opts_del(opts);
388 }
389 opts = vnc_parse_func(target);
390 vnc_display_open("default", errp);
391 }
392
393 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
394 Error **errp)
395 {
396 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
397 if (!has_arg) {
398 error_set(errp, QERR_MISSING_PARAMETER, "password");
399 } else {
400 qmp_change_vnc_password(arg, errp);
401 }
402 } else {
403 qmp_change_vnc_listen(target, errp);
404 }
405 }
406 #else
407 void qmp_change_vnc_password(const char *password, Error **errp)
408 {
409 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
410 }
411 static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
412 Error **errp)
413 {
414 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
415 }
416 #endif /* !CONFIG_VNC */
417
418 void qmp_change(const char *device, const char *target,
419 bool has_arg, const char *arg, Error **errp)
420 {
421 if (strcmp(device, "vnc") == 0) {
422 qmp_change_vnc(target, has_arg, arg, errp);
423 } else {
424 qmp_change_blockdev(device, target, arg, errp);
425 }
426 }
427
428 static void qom_list_types_tramp(ObjectClass *klass, void *data)
429 {
430 ObjectTypeInfoList *e, **pret = data;
431 ObjectTypeInfo *info;
432
433 info = g_malloc0(sizeof(*info));
434 info->name = g_strdup(object_class_get_name(klass));
435
436 e = g_malloc0(sizeof(*e));
437 e->value = info;
438 e->next = *pret;
439 *pret = e;
440 }
441
442 ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
443 const char *implements,
444 bool has_abstract,
445 bool abstract,
446 Error **errp)
447 {
448 ObjectTypeInfoList *ret = NULL;
449
450 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
451
452 return ret;
453 }
454
455 /* Return a DevicePropertyInfo for a qdev property.
456 *
457 * If a qdev property with the given name does not exist, use the given default
458 * type. If the qdev property info should not be shown, return NULL.
459 *
460 * The caller must free the return value.
461 */
462 static DevicePropertyInfo *make_device_property_info(ObjectClass *klass,
463 const char *name,
464 const char *default_type,
465 const char *description)
466 {
467 DevicePropertyInfo *info;
468 Property *prop;
469
470 do {
471 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
472 if (strcmp(name, prop->name) != 0) {
473 continue;
474 }
475
476 /*
477 * TODO Properties without a parser are just for dirty hacks.
478 * qdev_prop_ptr is the only such PropertyInfo. It's marked
479 * for removal. This conditional should be removed along with
480 * it.
481 */
482 if (!prop->info->set) {
483 return NULL; /* no way to set it, don't show */
484 }
485
486 info = g_malloc0(sizeof(*info));
487 info->name = g_strdup(prop->name);
488 info->type = g_strdup(prop->info->name);
489 info->has_description = !!prop->info->description;
490 info->description = g_strdup(prop->info->description);
491 return info;
492 }
493 klass = object_class_get_parent(klass);
494 } while (klass != object_class_by_name(TYPE_DEVICE));
495
496 /* Not a qdev property, use the default type */
497 info = g_malloc0(sizeof(*info));
498 info->name = g_strdup(name);
499 info->type = g_strdup(default_type);
500 info->has_description = !!description;
501 info->description = g_strdup(description);
502
503 return info;
504 }
505
506 DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
507 Error **errp)
508 {
509 ObjectClass *klass;
510 Object *obj;
511 ObjectProperty *prop;
512 DevicePropertyInfoList *prop_list = NULL;
513
514 klass = object_class_by_name(typename);
515 if (klass == NULL) {
516 error_set(errp, QERR_DEVICE_NOT_FOUND, typename);
517 return NULL;
518 }
519
520 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
521 if (klass == NULL) {
522 error_set(errp, QERR_INVALID_PARAMETER_VALUE,
523 "name", TYPE_DEVICE);
524 return NULL;
525 }
526
527 obj = object_new(typename);
528
529 QTAILQ_FOREACH(prop, &obj->properties, node) {
530 DevicePropertyInfo *info;
531 DevicePropertyInfoList *entry;
532
533 /* Skip Object and DeviceState properties */
534 if (strcmp(prop->name, "type") == 0 ||
535 strcmp(prop->name, "realized") == 0 ||
536 strcmp(prop->name, "hotpluggable") == 0 ||
537 strcmp(prop->name, "hotplugged") == 0 ||
538 strcmp(prop->name, "parent_bus") == 0) {
539 continue;
540 }
541
542 /* Skip legacy properties since they are just string versions of
543 * properties that we already list.
544 */
545 if (strstart(prop->name, "legacy-", NULL)) {
546 continue;
547 }
548
549 info = make_device_property_info(klass, prop->name, prop->type,
550 prop->description);
551 if (!info) {
552 continue;
553 }
554
555 entry = g_malloc0(sizeof(*entry));
556 entry->value = info;
557 entry->next = prop_list;
558 prop_list = entry;
559 }
560
561 object_unref(obj);
562
563 return prop_list;
564 }
565
566 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
567 {
568 return arch_query_cpu_definitions(errp);
569 }
570
571 void qmp_add_client(const char *protocol, const char *fdname,
572 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
573 Error **errp)
574 {
575 CharDriverState *s;
576 int fd;
577
578 fd = monitor_get_fd(cur_mon, fdname, errp);
579 if (fd < 0) {
580 return;
581 }
582
583 if (strcmp(protocol, "spice") == 0) {
584 if (!qemu_using_spice(errp)) {
585 close(fd);
586 return;
587 }
588 skipauth = has_skipauth ? skipauth : false;
589 tls = has_tls ? tls : false;
590 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
591 error_setg(errp, "spice failed to add client");
592 close(fd);
593 }
594 return;
595 #ifdef CONFIG_VNC
596 } else if (strcmp(protocol, "vnc") == 0) {
597 skipauth = has_skipauth ? skipauth : false;
598 vnc_display_add_client(NULL, fd, skipauth);
599 return;
600 #endif
601 } else if ((s = qemu_chr_find(protocol)) != NULL) {
602 if (qemu_chr_add_client(s, fd) < 0) {
603 error_setg(errp, "failed to add client");
604 close(fd);
605 return;
606 }
607 return;
608 }
609
610 error_setg(errp, "protocol '%s' is invalid", protocol);
611 close(fd);
612 }
613
614 void object_add(const char *type, const char *id, const QDict *qdict,
615 Visitor *v, Error **errp)
616 {
617 Object *obj;
618 ObjectClass *klass;
619 const QDictEntry *e;
620 Error *local_err = NULL;
621
622 klass = object_class_by_name(type);
623 if (!klass) {
624 error_setg(errp, "invalid object type: %s", type);
625 return;
626 }
627
628 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
629 error_setg(errp, "object type '%s' isn't supported by object-add",
630 type);
631 return;
632 }
633
634 if (object_class_is_abstract(klass)) {
635 error_setg(errp, "object type '%s' is abstract", type);
636 return;
637 }
638
639 obj = object_new(type);
640 if (qdict) {
641 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
642 object_property_set(obj, v, e->key, &local_err);
643 if (local_err) {
644 goto out;
645 }
646 }
647 }
648
649 object_property_add_child(container_get(object_get_root(), "/objects"),
650 id, obj, &local_err);
651 if (local_err) {
652 goto out;
653 }
654
655 user_creatable_complete(obj, &local_err);
656 if (local_err) {
657 object_property_del(container_get(object_get_root(), "/objects"),
658 id, &error_abort);
659 goto out;
660 }
661 out:
662 if (local_err) {
663 error_propagate(errp, local_err);
664 }
665 object_unref(obj);
666 }
667
668 int qmp_object_add(Monitor *mon, const QDict *qdict, QObject **ret)
669 {
670 const char *type = qdict_get_str(qdict, "qom-type");
671 const char *id = qdict_get_str(qdict, "id");
672 QObject *props = qdict_get(qdict, "props");
673 const QDict *pdict = NULL;
674 Error *local_err = NULL;
675 QmpInputVisitor *qiv;
676
677 if (props) {
678 pdict = qobject_to_qdict(props);
679 if (!pdict) {
680 error_set(&local_err, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
681 goto out;
682 }
683 }
684
685 qiv = qmp_input_visitor_new(props);
686 object_add(type, id, pdict, qmp_input_get_visitor(qiv), &local_err);
687 qmp_input_visitor_cleanup(qiv);
688
689 out:
690 if (local_err) {
691 qerror_report_err(local_err);
692 error_free(local_err);
693 return -1;
694 }
695
696 return 0;
697 }
698
699 void qmp_object_del(const char *id, Error **errp)
700 {
701 Object *container;
702 Object *obj;
703
704 container = container_get(object_get_root(), "/objects");
705 obj = object_resolve_path_component(container, id);
706 if (!obj) {
707 error_setg(errp, "object id not found");
708 return;
709 }
710 object_unparent(obj);
711 }
712
713 MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
714 {
715 MemoryDeviceInfoList *head = NULL;
716 MemoryDeviceInfoList **prev = &head;
717
718 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
719
720 return head;
721 }
722
723 ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
724 {
725 bool ambig;
726 ACPIOSTInfoList *head = NULL;
727 ACPIOSTInfoList **prev = &head;
728 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
729
730 if (obj) {
731 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
732 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
733
734 adevc->ospm_status(adev, &prev);
735 } else {
736 error_setg(errp, "command is not supported, missing ACPI device");
737 }
738
739 return head;
740 }