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