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