blob: e7c0a2fd60de4c0d6cec767b1c7edf0111e83be7 [file] [log] [blame]
Anthony Liguori48a32be2011-09-02 12:34:48 -05001/*
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 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010012 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
Anthony Liguori48a32be2011-09-02 12:34:48 -050014 */
15
Peter Maydelld38ea872016-01-29 17:50:05 +000016#include "qemu/osdep.h"
Fam Zheng67a1de02016-06-01 17:44:21 +080017#include "qemu-version.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020018#include "qemu/cutils.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010019#include "qemu/option.h"
Markus Armbrustera0b1a662015-03-17 18:16:21 +010020#include "monitor/monitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010021#include "sysemu/sysemu.h"
Marc-André Lureau213dcb02016-12-12 20:22:24 +030022#include "qemu/config-file.h"
Fam Zhengcea25272016-09-21 12:27:14 +080023#include "qemu/uuid.h"
Marc-André Lureau8228e352017-01-26 17:19:46 +040024#include "chardev/char.h"
Luiz Capitulinofbf796f2011-12-07 11:17:51 -020025#include "ui/qemu-spice.h"
26#include "ui/vnc.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010027#include "sysemu/kvm.h"
28#include "sysemu/arch_init.h"
Anthony Liguorib4b12c62011-12-12 14:29:34 -060029#include "hw/qdev.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010030#include "sysemu/blockdev.h"
Max Reitz373340b2015-10-19 17:53:22 +020031#include "sysemu/block-backend.h"
Paolo Bonzini14cccb62012-12-17 18:19:50 +010032#include "qom/qom-qobject.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010033#include "qapi/error.h"
Markus Armbruster112ed242018-02-26 17:13:27 -060034#include "qapi/qapi-commands-block-core.h"
35#include "qapi/qapi-commands-misc.h"
36#include "qapi/qapi-commands-ui.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010037#include "qapi/qmp/qdict.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010038#include "qapi/qmp/qerror.h"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010039#include "qapi/qobject-input-visitor.h"
Igor Mammedov69ca3ea2013-04-30 15:41:25 +020040#include "hw/boards.h"
Igor Mammedov269e09f2014-01-16 17:34:38 +010041#include "qom/object_interfaces.h"
David Hildenbrand2cc0e2e2018-04-23 18:51:16 +020042#include "hw/mem/memory-device.h"
Igor Mammedov02419bc2014-06-16 19:12:28 +020043#include "hw/acpi/acpi_dev_interface.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050044
45NameInfo *qmp_query_name(Error **errp)
46{
47 NameInfo *info = g_malloc0(sizeof(*info));
48
49 if (qemu_name) {
50 info->has_name = true;
51 info->name = g_strdup(qemu_name);
52 }
53
54 return info;
55}
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030056
Markus Armbruster7daecb32014-05-02 13:26:31 +020057VersionInfo *qmp_query_version(Error **errp)
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030058{
Eric Blake4752cdb2015-05-04 09:05:31 -060059 VersionInfo *info = g_new0(VersionInfo, 1);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030060
Eric Blake4752cdb2015-05-04 09:05:31 -060061 info->qemu = g_new0(VersionTriple, 1);
Marc-André Lureau3688d8c2016-09-12 13:18:56 +040062 info->qemu->major = QEMU_VERSION_MAJOR;
63 info->qemu->minor = QEMU_VERSION_MINOR;
64 info->qemu->micro = QEMU_VERSION_MICRO;
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030065 info->package = g_strdup(QEMU_PKGVERSION);
66
67 return info;
68}
Luiz Capitulino292a2602011-09-12 15:10:53 -030069
70KvmInfo *qmp_query_kvm(Error **errp)
71{
72 KvmInfo *info = g_malloc0(sizeof(*info));
73
74 info->enabled = kvm_enabled();
75 info->present = kvm_available();
76
77 return info;
78}
79
Luiz Capitulinoefab7672011-09-13 17:16:25 -030080UuidInfo *qmp_query_uuid(Error **errp)
81{
82 UuidInfo *info = g_malloc0(sizeof(*info));
Luiz Capitulinoefab7672011-09-13 17:16:25 -030083
Fam Zheng9c5ce8d2016-09-21 12:27:22 +080084 info->UUID = qemu_uuid_unparse_strdup(&qemu_uuid);
Luiz Capitulinoefab7672011-09-13 17:16:25 -030085 return info;
86}
87
Markus Armbruster7daecb32014-05-02 13:26:31 +020088void qmp_quit(Error **errp)
Luiz Capitulino7a7f3252011-09-15 14:20:28 -030089{
90 no_shutdown = 0;
Eric Blakecf83f142017-05-15 16:41:13 -050091 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP);
Luiz Capitulino7a7f3252011-09-15 14:20:28 -030092}
93
Luiz Capitulino5f158f22011-09-15 14:34:39 -030094void qmp_stop(Error **errp)
95{
Peter Xu65d64f32016-02-18 13:16:49 +080096 /* if there is a dump in background, we should wait until the dump
97 * finished */
98 if (dump_in_progress()) {
99 error_setg(errp, "There is a dump in process, please wait.");
100 return;
101 }
102
Paolo Bonzini1e998142012-10-23 14:54:21 +0200103 if (runstate_check(RUN_STATE_INMIGRATE)) {
104 autostart = 0;
105 } else {
106 vm_stop(RUN_STATE_PAUSED);
107 }
Luiz Capitulino5f158f22011-09-15 14:34:39 -0300108}
109
Luiz Capitulino38d22652011-09-15 14:41:46 -0300110void qmp_system_reset(Error **errp)
111{
Eric Blakecf83f142017-05-15 16:41:13 -0500112 qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP);
Luiz Capitulino38d22652011-09-15 14:41:46 -0300113}
Luiz Capitulino5bc465e2011-09-28 11:06:15 -0300114
115void qmp_system_powerdown(Error **erp)
116{
117 qemu_system_powerdown_request();
118}
Luiz Capitulino755f1962011-10-06 14:31:39 -0300119
Igor Mammedov69ca3ea2013-04-30 15:41:25 +0200120void qmp_cpu_add(int64_t id, Error **errp)
121{
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +0200122 MachineClass *mc;
123
124 mc = MACHINE_GET_CLASS(current_machine);
Marcel Apfelbaum958db902014-04-09 20:34:53 +0300125 if (mc->hot_add_cpu) {
126 mc->hot_add_cpu(id, errp);
Igor Mammedov69ca3ea2013-04-30 15:41:25 +0200127 } else {
128 error_setg(errp, "Not supported");
129 }
130}
131
Markus Armbruster361ac942018-07-05 11:14:02 +0200132void qmp_x_exit_preconfig(Error **errp)
Igor Mammedov047f7032018-05-11 19:24:43 +0200133{
134 if (!runstate_check(RUN_STATE_PRECONFIG)) {
135 error_setg(errp, "The command is permitted only in '%s' state",
136 RunState_str(RUN_STATE_PRECONFIG));
137 return;
138 }
139 qemu_exit_preconfig_request();
140}
141
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200142void qmp_cont(Error **errp)
143{
Max Reitz373340b2015-10-19 17:53:22 +0200144 BlockBackend *blk;
Daniel P. Berrange788cf9f2017-06-23 17:24:15 +0100145 Error *local_err = NULL;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200146
Peter Xu65d64f32016-02-18 13:16:49 +0800147 /* if there is a dump in background, we should wait until the dump
148 * finished */
149 if (dump_in_progress()) {
150 error_setg(errp, "There is a dump in process, please wait.");
151 return;
152 }
153
Hu Taoede085b2013-04-26 11:24:40 +0800154 if (runstate_needs_reset()) {
Cole Robinsonf231b882014-03-21 19:42:26 -0400155 error_setg(errp, "Resetting the Virtual Machine is required");
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200156 return;
Luiz Capitulinoad02b962012-04-27 13:33:36 -0300157 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
158 return;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200159 }
160
Max Reitz373340b2015-10-19 17:53:22 +0200161 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
162 blk_iostatus_reset(blk);
Markus Armbrusterab319792014-05-02 13:26:42 +0200163 }
Kevin Wolf7c8eece2016-03-22 18:58:50 +0100164
Kevin Wolf76b1c7f2015-12-22 14:07:08 +0100165 /* Continuing after completed migration. Images have been inactivated to
Kevin Wolface21a52017-05-04 18:52:36 +0200166 * allow the destination to take control. Need to get control back now.
167 *
168 * If there are no inactive block nodes (e.g. because the VM was just
169 * paused rather than completing a migration), bdrv_inactivate_all() simply
170 * doesn't do anything. */
171 bdrv_invalidate_cache_all(&local_err);
172 if (local_err) {
173 error_propagate(errp, local_err);
174 return;
Kevin Wolf76b1c7f2015-12-22 14:07:08 +0100175 }
176
Paolo Bonzini1e998142012-10-23 14:54:21 +0200177 if (runstate_check(RUN_STATE_INMIGRATE)) {
178 autostart = 1;
179 } else {
180 vm_start();
181 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200182}
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600183
Gerd Hoffmann9b9df252012-02-23 13:45:21 +0100184void qmp_system_wakeup(Error **errp)
185{
186 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
187}
188
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600189ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600190{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600191 Object *obj;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600192 bool ambiguous = false;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600193 ObjectPropertyInfoList *props = NULL;
194 ObjectProperty *prop;
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000195 ObjectPropertyIterator iter;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600196
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600197 obj = object_resolve_path(path, &ambiguous);
198 if (obj == NULL) {
Michael Tokarev79772082014-05-04 12:23:59 +0400199 if (ambiguous) {
200 error_setg(errp, "Path '%s' is ambiguous", path);
201 } else {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100202 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
203 "Device '%s' not found", path);
Michael Tokarev79772082014-05-04 12:23:59 +0400204 }
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600205 return NULL;
206 }
207
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000208 object_property_iter_init(&iter, obj);
209 while ((prop = object_property_iter_next(&iter))) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600210 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600211
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600212 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600213 entry->next = props;
214 props = entry;
215
216 entry->value->name = g_strdup(prop->name);
217 entry->value->type = g_strdup(prop->type);
218 }
219
220 return props;
221}
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600222
Markus Armbruster6eb39372015-09-16 13:06:25 +0200223void qmp_qom_set(const char *path, const char *property, QObject *value,
224 Error **errp)
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600225{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600226 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600227
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600228 obj = object_resolve_path(path, NULL);
229 if (!obj) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100230 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Markus Armbruster75158eb2015-03-16 08:57:47 +0100231 "Device '%s' not found", path);
Markus Armbruster485febc2015-03-13 17:25:50 +0100232 return;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600233 }
234
Markus Armbruster485febc2015-03-13 17:25:50 +0100235 object_property_set_qobject(obj, value, property, errp);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600236}
237
Markus Armbruster6eb39372015-09-16 13:06:25 +0200238QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600239{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600240 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600241
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600242 obj = object_resolve_path(path, NULL);
243 if (!obj) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100244 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Markus Armbruster75158eb2015-03-16 08:57:47 +0100245 "Device '%s' not found", path);
Markus Armbruster6eb39372015-09-16 13:06:25 +0200246 return NULL;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600247 }
248
Markus Armbruster6eb39372015-09-16 13:06:25 +0200249 return object_property_get_qobject(obj, property, errp);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600250}
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200251
252void qmp_set_password(const char *protocol, const char *password,
253 bool has_connected, const char *connected, Error **errp)
254{
255 int disconnect_if_connected = 0;
256 int fail_if_connected = 0;
257 int rc;
258
259 if (has_connected) {
260 if (strcmp(connected, "fail") == 0) {
261 fail_if_connected = 1;
262 } else if (strcmp(connected, "disconnect") == 0) {
263 disconnect_if_connected = 1;
264 } else if (strcmp(connected, "keep") == 0) {
265 /* nothing */
266 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100267 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200268 return;
269 }
270 }
271
272 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100273 if (!qemu_using_spice(errp)) {
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200274 return;
275 }
276 rc = qemu_spice_set_passwd(password, fail_if_connected,
277 disconnect_if_connected);
278 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100279 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200280 }
281 return;
282 }
283
284 if (strcmp(protocol, "vnc") == 0) {
285 if (fail_if_connected || disconnect_if_connected) {
286 /* vnc supports "connected=keep" only */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100287 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200288 return;
289 }
290 /* Note that setting an empty password will not disable login through
291 * this interface. */
292 rc = vnc_display_password(NULL, password);
293 if (rc < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100294 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200295 }
296 return;
297 }
298
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100299 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200300}
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200301
302void qmp_expire_password(const char *protocol, const char *whenstr,
303 Error **errp)
304{
305 time_t when;
306 int rc;
307
308 if (strcmp(whenstr, "now") == 0) {
309 when = 0;
310 } else if (strcmp(whenstr, "never") == 0) {
311 when = TIME_MAX;
312 } else if (whenstr[0] == '+') {
313 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
314 } else {
315 when = strtoull(whenstr, NULL, 10);
316 }
317
318 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100319 if (!qemu_using_spice(errp)) {
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200320 return;
321 }
322 rc = qemu_spice_set_pw_expire(when);
323 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100324 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200325 }
326 return;
327 }
328
329 if (strcmp(protocol, "vnc") == 0) {
330 rc = vnc_display_pw_expire(NULL, when);
331 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100332 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200333 }
334 return;
335 }
336
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100337 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200338}
Luiz Capitulino270b2432011-12-08 11:45:55 -0200339
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200340#ifdef CONFIG_VNC
Luiz Capitulino270b2432011-12-08 11:45:55 -0200341void qmp_change_vnc_password(const char *password, Error **errp)
342{
343 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100344 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino270b2432011-12-08 11:45:55 -0200345 }
346}
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200347
Paolo Bonzini007fcd3e2012-10-18 09:01:01 +0200348static void qmp_change_vnc_listen(const char *target, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200349{
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200350 QemuOptsList *olist = qemu_find_opts("vnc");
351 QemuOpts *opts;
352
353 if (strstr(target, "id=")) {
354 error_setg(errp, "id not supported");
355 return;
356 }
357
358 opts = qemu_opts_find(olist, "default");
359 if (opts) {
360 qemu_opts_del(opts);
361 }
Markus Armbruster70b94332015-02-13 12:50:26 +0100362 opts = vnc_parse(target, errp);
Gongleif7801c52015-02-05 17:43:35 +0800363 if (!opts) {
364 return;
365 }
366
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200367 vnc_display_open("default", errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200368}
369
370static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
371 Error **errp)
372{
373 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
374 if (!has_arg) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100375 error_setg(errp, QERR_MISSING_PARAMETER, "password");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200376 } else {
377 qmp_change_vnc_password(arg, errp);
378 }
379 } else {
380 qmp_change_vnc_listen(target, errp);
381 }
382}
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200383#endif /* !CONFIG_VNC */
384
385void qmp_change(const char *device, const char *target,
Markus Armbruster7daecb32014-05-02 13:26:31 +0200386 bool has_arg, const char *arg, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200387{
388 if (strcmp(device, "vnc") == 0) {
Marc-André Lureau05eb4a22018-07-03 17:56:47 +0200389#ifdef CONFIG_VNC
Markus Armbruster7daecb32014-05-02 13:26:31 +0200390 qmp_change_vnc(target, has_arg, arg, errp);
Marc-André Lureau05eb4a22018-07-03 17:56:47 +0200391#else
392 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
393#endif
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200394 } else {
Kevin Wolf70e2cb32016-09-20 13:38:47 +0200395 qmp_blockdev_change_medium(true, device, false, NULL, target,
396 has_arg, arg, false, 0, errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200397 }
398}
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600399
400static void qom_list_types_tramp(ObjectClass *klass, void *data)
401{
402 ObjectTypeInfoList *e, **pret = data;
403 ObjectTypeInfo *info;
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300404 ObjectClass *parent = object_class_get_parent(klass);
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600405
406 info = g_malloc0(sizeof(*info));
407 info->name = g_strdup(object_class_get_name(klass));
Eduardo Habkost87467ea2017-07-07 09:22:14 -0300408 info->has_abstract = info->abstract = object_class_is_abstract(klass);
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300409 if (parent) {
410 info->has_parent = true;
411 info->parent = g_strdup(object_class_get_name(parent));
412 }
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600413
414 e = g_malloc0(sizeof(*e));
415 e->value = info;
416 e->next = *pret;
417 *pret = e;
418}
419
420ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
421 const char *implements,
422 bool has_abstract,
423 bool abstract,
424 Error **errp)
425{
426 ObjectTypeInfoList *ret = NULL;
427
428 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
429
430 return ret;
431}
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500432
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200433/* Return a DevicePropertyInfo for a qdev property.
434 *
435 * If a qdev property with the given name does not exist, use the given default
436 * type. If the qdev property info should not be shown, return NULL.
437 *
438 * The caller must free the return value.
439 */
Alexey Kardashevskiy35f63762018-03-02 00:09:38 +1100440static ObjectPropertyInfo *make_device_property_info(ObjectClass *klass,
441 const char *name,
442 const char *default_type,
443 const char *description)
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200444{
Alexey Kardashevskiy35f63762018-03-02 00:09:38 +1100445 ObjectPropertyInfo *info;
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200446 Property *prop;
447
448 do {
449 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
450 if (strcmp(name, prop->name) != 0) {
451 continue;
452 }
453
454 /*
455 * TODO Properties without a parser are just for dirty hacks.
456 * qdev_prop_ptr is the only such PropertyInfo. It's marked
457 * for removal. This conditional should be removed along with
458 * it.
459 */
Fam Zhengfaabdbb2017-07-14 10:14:51 +0800460 if (!prop->info->set && !prop->info->create) {
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200461 return NULL; /* no way to set it, don't show */
462 }
463
464 info = g_malloc0(sizeof(*info));
465 info->name = g_strdup(prop->name);
Fam Zheng75ab9052017-07-14 10:14:53 +0800466 info->type = default_type ? g_strdup(default_type)
467 : g_strdup(prop->info->name);
Gonglei07d09c52014-10-07 14:33:23 +0800468 info->has_description = !!prop->info->description;
469 info->description = g_strdup(prop->info->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200470 return info;
471 }
472 klass = object_class_get_parent(klass);
473 } while (klass != object_class_by_name(TYPE_DEVICE));
474
475 /* Not a qdev property, use the default type */
476 info = g_malloc0(sizeof(*info));
477 info->name = g_strdup(name);
478 info->type = g_strdup(default_type);
Gonglei07d09c52014-10-07 14:33:23 +0800479 info->has_description = !!description;
480 info->description = g_strdup(description);
481
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200482 return info;
483}
484
Alexey Kardashevskiy35f63762018-03-02 00:09:38 +1100485ObjectPropertyInfoList *qmp_device_list_properties(const char *typename,
486 Error **errp)
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500487{
488 ObjectClass *klass;
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200489 Object *obj;
490 ObjectProperty *prop;
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000491 ObjectPropertyIterator iter;
Alexey Kardashevskiy35f63762018-03-02 00:09:38 +1100492 ObjectPropertyInfoList *prop_list = NULL;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500493
494 klass = object_class_by_name(typename);
495 if (klass == NULL) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100496 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
497 "Device '%s' not found", typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500498 return NULL;
499 }
500
501 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
502 if (klass == NULL) {
Lin Ma7a8c1532017-01-25 13:27:03 +0800503 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename", TYPE_DEVICE);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500504 return NULL;
505 }
506
Markus Armbrusteredb15232015-10-01 10:59:57 +0200507 if (object_class_is_abstract(klass)) {
Lin Ma7a8c1532017-01-25 13:27:03 +0800508 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename",
Markus Armbrusteredb15232015-10-01 10:59:57 +0200509 "non-abstract device type");
510 return NULL;
511 }
512
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200513 obj = object_new(typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500514
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000515 object_property_iter_init(&iter, obj);
516 while ((prop = object_property_iter_next(&iter))) {
Alexey Kardashevskiy35f63762018-03-02 00:09:38 +1100517 ObjectPropertyInfo *info;
518 ObjectPropertyInfoList *entry;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500519
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200520 /* Skip Object and DeviceState properties */
521 if (strcmp(prop->name, "type") == 0 ||
522 strcmp(prop->name, "realized") == 0 ||
523 strcmp(prop->name, "hotpluggable") == 0 ||
Stefan Hajnoczi4115dd62014-07-09 14:01:31 +0200524 strcmp(prop->name, "hotplugged") == 0 ||
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200525 strcmp(prop->name, "parent_bus") == 0) {
526 continue;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500527 }
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200528
529 /* Skip legacy properties since they are just string versions of
530 * properties that we already list.
531 */
532 if (strstart(prop->name, "legacy-", NULL)) {
533 continue;
534 }
535
Gonglei07d09c52014-10-07 14:33:23 +0800536 info = make_device_property_info(klass, prop->name, prop->type,
537 prop->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200538 if (!info) {
539 continue;
540 }
541
542 entry = g_malloc0(sizeof(*entry));
543 entry->value = info;
544 entry->next = prop_list;
545 prop_list = entry;
546 }
547
548 object_unref(obj);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500549
550 return prop_list;
551}
Anthony Liguorie4e31c62012-08-10 11:04:13 -0500552
Alexey Kardashevskiy961c47b2018-03-02 00:09:39 +1100553ObjectPropertyInfoList *qmp_qom_list_properties(const char *typename,
554 Error **errp)
555{
556 ObjectClass *klass;
557 Object *obj = NULL;
558 ObjectProperty *prop;
559 ObjectPropertyIterator iter;
560 ObjectPropertyInfoList *prop_list = NULL;
561
562 klass = object_class_by_name(typename);
563 if (klass == NULL) {
564 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
565 "Class '%s' not found", typename);
566 return NULL;
567 }
568
569 klass = object_class_dynamic_cast(klass, TYPE_OBJECT);
570 if (klass == NULL) {
571 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename", TYPE_OBJECT);
572 return NULL;
573 }
574
575 if (object_class_is_abstract(klass)) {
576 object_class_property_iter_init(&iter, klass);
577 } else {
578 obj = object_new(typename);
579 object_property_iter_init(&iter, obj);
580 }
581 while ((prop = object_property_iter_next(&iter))) {
582 ObjectPropertyInfo *info;
583 ObjectPropertyInfoList *entry;
584
585 info = g_malloc0(sizeof(*info));
586 info->name = g_strdup(prop->name);
587 info->type = g_strdup(prop->type);
588 info->has_description = !!prop->description;
589 info->description = g_strdup(prop->description);
590
591 entry = g_malloc0(sizeof(*entry));
592 entry->value = info;
593 entry->next = prop_list;
594 prop_list = entry;
595 }
596
597 object_unref(obj);
598
599 return prop_list;
600}
601
Anthony Liguori76b64a72012-08-14 22:17:36 -0500602CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
603{
604 return arch_query_cpu_definitions(errp);
605}
606
David Hildenbrande09484e2016-09-05 10:52:39 +0200607CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
608 CpuModelInfo *model,
609 Error **errp)
610{
611 return arch_query_cpu_model_expansion(type, model, errp);
612}
613
David Hildenbrand0031e0d2016-09-05 10:52:40 +0200614CpuModelCompareInfo *qmp_query_cpu_model_comparison(CpuModelInfo *modela,
615 CpuModelInfo *modelb,
616 Error **errp)
617{
618 return arch_query_cpu_model_comparison(modela, modelb, errp);
619}
620
David Hildenbrandb18b6042016-09-05 10:52:41 +0200621CpuModelBaselineInfo *qmp_query_cpu_model_baseline(CpuModelInfo *modela,
622 CpuModelInfo *modelb,
623 Error **errp)
624{
625 return arch_query_cpu_model_baseline(modela, modelb, errp);
626}
627
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300628void qmp_add_client(const char *protocol, const char *fdname,
629 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
630 Error **errp)
631{
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300632 Chardev *s;
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300633 int fd;
634
635 fd = monitor_get_fd(cur_mon, fdname, errp);
636 if (fd < 0) {
637 return;
638 }
639
640 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100641 if (!qemu_using_spice(errp)) {
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300642 close(fd);
643 return;
644 }
645 skipauth = has_skipauth ? skipauth : false;
646 tls = has_tls ? tls : false;
647 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
648 error_setg(errp, "spice failed to add client");
649 close(fd);
650 }
651 return;
652#ifdef CONFIG_VNC
653 } else if (strcmp(protocol, "vnc") == 0) {
654 skipauth = has_skipauth ? skipauth : false;
655 vnc_display_add_client(NULL, fd, skipauth);
656 return;
657#endif
658 } else if ((s = qemu_chr_find(protocol)) != NULL) {
659 if (qemu_chr_add_client(s, fd) < 0) {
660 error_setg(errp, "failed to add client");
661 close(fd);
662 return;
663 }
664 return;
665 }
666
667 error_setg(errp, "protocol '%s' is invalid", protocol);
668 close(fd);
669}
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100670
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100671
Markus Armbruster6eb39372015-09-16 13:06:25 +0200672void qmp_object_add(const char *type, const char *id,
673 bool has_props, QObject *props, Error **errp)
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100674{
Marc-André Lureaue64c75a2016-09-23 00:39:25 +0400675 QDict *pdict;
Eric Blakeb70ce1012016-06-09 10:48:38 -0600676 Visitor *v;
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000677 Object *obj;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100678
679 if (props) {
Max Reitz7dc847e2018-02-24 16:40:29 +0100680 pdict = qobject_to(QDict, props);
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100681 if (!pdict) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100682 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
683 return;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100684 }
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200685 qobject_ref(pdict);
Marc-André Lureaue64c75a2016-09-23 00:39:25 +0400686 } else {
687 pdict = qdict_new();
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100688 }
689
Markus Armbruster048abb72017-03-03 13:32:39 +0100690 v = qobject_input_visitor_new(QOBJECT(pdict));
Eric Blakeb70ce1012016-06-09 10:48:38 -0600691 obj = user_creatable_add_type(type, id, pdict, v, errp);
692 visit_free(v);
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000693 if (obj) {
694 object_unref(obj);
695 }
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200696 qobject_unref(pdict);
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100697}
698
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100699void qmp_object_del(const char *id, Error **errp)
700{
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000701 user_creatable_del(id, errp);
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100702}
Igor Mammedov6f2e2732014-06-16 19:12:25 +0200703
704MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
705{
David Hildenbrand2cc0e2e2018-04-23 18:51:16 +0200706 return qmp_memory_device_list();
Igor Mammedov6f2e2732014-06-16 19:12:25 +0200707}
Igor Mammedov02419bc2014-06-16 19:12:28 +0200708
709ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
710{
711 bool ambig;
712 ACPIOSTInfoList *head = NULL;
713 ACPIOSTInfoList **prev = &head;
714 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
715
716 if (obj) {
717 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
718 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
719
720 adevc->ospm_status(adev, &prev);
721 } else {
722 error_setg(errp, "command is not supported, missing ACPI device");
723 }
724
725 return head;
726}
Vadim Galitsyn9aa33972017-08-29 17:30:21 +0200727
728MemoryInfo *qmp_query_memory_size_summary(Error **errp)
729{
730 MemoryInfo *mem_info = g_malloc0(sizeof(MemoryInfo));
731
732 mem_info->base_memory = ram_size;
733
734 mem_info->plugged_memory = get_plugged_memory_size();
735 mem_info->has_plugged_memory =
736 mem_info->plugged_memory != (uint64_t)-1;
737
738 return mem_info;
739}