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