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