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