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