blob: 4c819dd8cfdd23234874fae3d457f4d4e7fac1e7 [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;
Dominik Csapak92548932018-12-05 12:01:31 +010091 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP_QUIT);
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{
Dominik Csapak92548932018-12-05 12:01:31 +0100112 qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET);
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{
Daniel Henrique Barbozafb064112018-12-05 17:47:01 -0200186 if (!qemu_wakeup_suspend_enabled()) {
187 error_setg(errp,
188 "wake-up from suspend is not supported by this guest");
189 return;
190 }
191
192 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, errp);
Gerd Hoffmann9b9df252012-02-23 13:45:21 +0100193}
194
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600195ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600196{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600197 Object *obj;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600198 bool ambiguous = false;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600199 ObjectPropertyInfoList *props = NULL;
200 ObjectProperty *prop;
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000201 ObjectPropertyIterator iter;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600202
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600203 obj = object_resolve_path(path, &ambiguous);
204 if (obj == NULL) {
Michael Tokarev79772082014-05-04 12:23:59 +0400205 if (ambiguous) {
206 error_setg(errp, "Path '%s' is ambiguous", path);
207 } else {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100208 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
209 "Device '%s' not found", path);
Michael Tokarev79772082014-05-04 12:23:59 +0400210 }
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600211 return NULL;
212 }
213
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000214 object_property_iter_init(&iter, obj);
215 while ((prop = object_property_iter_next(&iter))) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600216 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600217
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600218 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600219 entry->next = props;
220 props = entry;
221
222 entry->value->name = g_strdup(prop->name);
223 entry->value->type = g_strdup(prop->type);
224 }
225
226 return props;
227}
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600228
Markus Armbruster6eb39372015-09-16 13:06:25 +0200229void qmp_qom_set(const char *path, const char *property, QObject *value,
230 Error **errp)
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600231{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600232 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600233
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600234 obj = object_resolve_path(path, NULL);
235 if (!obj) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100236 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Markus Armbruster75158eb2015-03-16 08:57:47 +0100237 "Device '%s' not found", path);
Markus Armbruster485febc2015-03-13 17:25:50 +0100238 return;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600239 }
240
Markus Armbruster485febc2015-03-13 17:25:50 +0100241 object_property_set_qobject(obj, value, property, errp);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600242}
243
Markus Armbruster6eb39372015-09-16 13:06:25 +0200244QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600245{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600246 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600247
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600248 obj = object_resolve_path(path, NULL);
249 if (!obj) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100250 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Markus Armbruster75158eb2015-03-16 08:57:47 +0100251 "Device '%s' not found", path);
Markus Armbruster6eb39372015-09-16 13:06:25 +0200252 return NULL;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600253 }
254
Markus Armbruster6eb39372015-09-16 13:06:25 +0200255 return object_property_get_qobject(obj, property, errp);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600256}
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200257
258void qmp_set_password(const char *protocol, const char *password,
259 bool has_connected, const char *connected, Error **errp)
260{
261 int disconnect_if_connected = 0;
262 int fail_if_connected = 0;
263 int rc;
264
265 if (has_connected) {
266 if (strcmp(connected, "fail") == 0) {
267 fail_if_connected = 1;
268 } else if (strcmp(connected, "disconnect") == 0) {
269 disconnect_if_connected = 1;
270 } else if (strcmp(connected, "keep") == 0) {
271 /* nothing */
272 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100273 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200274 return;
275 }
276 }
277
278 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100279 if (!qemu_using_spice(errp)) {
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200280 return;
281 }
282 rc = qemu_spice_set_passwd(password, fail_if_connected,
283 disconnect_if_connected);
284 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100285 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200286 }
287 return;
288 }
289
290 if (strcmp(protocol, "vnc") == 0) {
291 if (fail_if_connected || disconnect_if_connected) {
292 /* vnc supports "connected=keep" only */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100293 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200294 return;
295 }
296 /* Note that setting an empty password will not disable login through
297 * this interface. */
298 rc = vnc_display_password(NULL, password);
299 if (rc < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100300 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200301 }
302 return;
303 }
304
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100305 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200306}
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200307
308void qmp_expire_password(const char *protocol, const char *whenstr,
309 Error **errp)
310{
311 time_t when;
312 int rc;
313
314 if (strcmp(whenstr, "now") == 0) {
315 when = 0;
316 } else if (strcmp(whenstr, "never") == 0) {
317 when = TIME_MAX;
318 } else if (whenstr[0] == '+') {
319 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
320 } else {
321 when = strtoull(whenstr, NULL, 10);
322 }
323
324 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100325 if (!qemu_using_spice(errp)) {
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200326 return;
327 }
328 rc = qemu_spice_set_pw_expire(when);
329 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100330 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200331 }
332 return;
333 }
334
335 if (strcmp(protocol, "vnc") == 0) {
336 rc = vnc_display_pw_expire(NULL, when);
337 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100338 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200339 }
340 return;
341 }
342
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100343 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200344}
Luiz Capitulino270b2432011-12-08 11:45:55 -0200345
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200346#ifdef CONFIG_VNC
Luiz Capitulino270b2432011-12-08 11:45:55 -0200347void qmp_change_vnc_password(const char *password, Error **errp)
348{
349 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100350 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino270b2432011-12-08 11:45:55 -0200351 }
352}
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200353
Paolo Bonzini007fcd3e2012-10-18 09:01:01 +0200354static void qmp_change_vnc_listen(const char *target, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200355{
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200356 QemuOptsList *olist = qemu_find_opts("vnc");
357 QemuOpts *opts;
358
359 if (strstr(target, "id=")) {
360 error_setg(errp, "id not supported");
361 return;
362 }
363
364 opts = qemu_opts_find(olist, "default");
365 if (opts) {
366 qemu_opts_del(opts);
367 }
Markus Armbruster70b94332015-02-13 12:50:26 +0100368 opts = vnc_parse(target, errp);
Gongleif7801c52015-02-05 17:43:35 +0800369 if (!opts) {
370 return;
371 }
372
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200373 vnc_display_open("default", errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200374}
375
376static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
377 Error **errp)
378{
379 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
380 if (!has_arg) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100381 error_setg(errp, QERR_MISSING_PARAMETER, "password");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200382 } else {
383 qmp_change_vnc_password(arg, errp);
384 }
385 } else {
386 qmp_change_vnc_listen(target, errp);
387 }
388}
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200389#endif /* !CONFIG_VNC */
390
391void qmp_change(const char *device, const char *target,
Markus Armbruster7daecb32014-05-02 13:26:31 +0200392 bool has_arg, const char *arg, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200393{
394 if (strcmp(device, "vnc") == 0) {
Marc-André Lureau05eb4a22018-07-03 17:56:47 +0200395#ifdef CONFIG_VNC
Markus Armbruster7daecb32014-05-02 13:26:31 +0200396 qmp_change_vnc(target, has_arg, arg, errp);
Marc-André Lureau05eb4a22018-07-03 17:56:47 +0200397#else
398 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
399#endif
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200400 } else {
Kevin Wolf70e2cb32016-09-20 13:38:47 +0200401 qmp_blockdev_change_medium(true, device, false, NULL, target,
402 has_arg, arg, false, 0, errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200403 }
404}
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600405
406static void qom_list_types_tramp(ObjectClass *klass, void *data)
407{
408 ObjectTypeInfoList *e, **pret = data;
409 ObjectTypeInfo *info;
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300410 ObjectClass *parent = object_class_get_parent(klass);
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600411
412 info = g_malloc0(sizeof(*info));
413 info->name = g_strdup(object_class_get_name(klass));
Eduardo Habkost87467ea2017-07-07 09:22:14 -0300414 info->has_abstract = info->abstract = object_class_is_abstract(klass);
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300415 if (parent) {
416 info->has_parent = true;
417 info->parent = g_strdup(object_class_get_name(parent));
418 }
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600419
420 e = g_malloc0(sizeof(*e));
421 e->value = info;
422 e->next = *pret;
423 *pret = e;
424}
425
426ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
427 const char *implements,
428 bool has_abstract,
429 bool abstract,
430 Error **errp)
431{
432 ObjectTypeInfoList *ret = NULL;
433
434 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
435
436 return ret;
437}
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500438
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200439/* Return a DevicePropertyInfo for a qdev property.
440 *
441 * If a qdev property with the given name does not exist, use the given default
442 * type. If the qdev property info should not be shown, return NULL.
443 *
444 * The caller must free the return value.
445 */
Alexey Kardashevskiy35f63762018-03-02 00:09:38 +1100446static ObjectPropertyInfo *make_device_property_info(ObjectClass *klass,
447 const char *name,
448 const char *default_type,
449 const char *description)
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200450{
Alexey Kardashevskiy35f63762018-03-02 00:09:38 +1100451 ObjectPropertyInfo *info;
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200452 Property *prop;
453
454 do {
455 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
456 if (strcmp(name, prop->name) != 0) {
457 continue;
458 }
459
460 /*
461 * TODO Properties without a parser are just for dirty hacks.
462 * qdev_prop_ptr is the only such PropertyInfo. It's marked
463 * for removal. This conditional should be removed along with
464 * it.
465 */
Fam Zhengfaabdbb2017-07-14 10:14:51 +0800466 if (!prop->info->set && !prop->info->create) {
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200467 return NULL; /* no way to set it, don't show */
468 }
469
470 info = g_malloc0(sizeof(*info));
471 info->name = g_strdup(prop->name);
Fam Zheng75ab9052017-07-14 10:14:53 +0800472 info->type = default_type ? g_strdup(default_type)
473 : g_strdup(prop->info->name);
Gonglei07d09c52014-10-07 14:33:23 +0800474 info->has_description = !!prop->info->description;
475 info->description = g_strdup(prop->info->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200476 return info;
477 }
478 klass = object_class_get_parent(klass);
479 } while (klass != object_class_by_name(TYPE_DEVICE));
480
481 /* Not a qdev property, use the default type */
482 info = g_malloc0(sizeof(*info));
483 info->name = g_strdup(name);
484 info->type = g_strdup(default_type);
Gonglei07d09c52014-10-07 14:33:23 +0800485 info->has_description = !!description;
486 info->description = g_strdup(description);
487
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200488 return info;
489}
490
Alexey Kardashevskiy35f63762018-03-02 00:09:38 +1100491ObjectPropertyInfoList *qmp_device_list_properties(const char *typename,
492 Error **errp)
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500493{
494 ObjectClass *klass;
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200495 Object *obj;
496 ObjectProperty *prop;
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000497 ObjectPropertyIterator iter;
Alexey Kardashevskiy35f63762018-03-02 00:09:38 +1100498 ObjectPropertyInfoList *prop_list = NULL;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500499
500 klass = object_class_by_name(typename);
501 if (klass == NULL) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100502 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
503 "Device '%s' not found", typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500504 return NULL;
505 }
506
507 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
508 if (klass == NULL) {
Lin Ma7a8c1532017-01-25 13:27:03 +0800509 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename", TYPE_DEVICE);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500510 return NULL;
511 }
512
Markus Armbrusteredb15232015-10-01 10:59:57 +0200513 if (object_class_is_abstract(klass)) {
Lin Ma7a8c1532017-01-25 13:27:03 +0800514 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename",
Markus Armbrusteredb15232015-10-01 10:59:57 +0200515 "non-abstract device type");
516 return NULL;
517 }
518
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200519 obj = object_new(typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500520
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000521 object_property_iter_init(&iter, obj);
522 while ((prop = object_property_iter_next(&iter))) {
Alexey Kardashevskiy35f63762018-03-02 00:09:38 +1100523 ObjectPropertyInfo *info;
524 ObjectPropertyInfoList *entry;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500525
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200526 /* Skip Object and DeviceState properties */
527 if (strcmp(prop->name, "type") == 0 ||
528 strcmp(prop->name, "realized") == 0 ||
529 strcmp(prop->name, "hotpluggable") == 0 ||
Stefan Hajnoczi4115dd62014-07-09 14:01:31 +0200530 strcmp(prop->name, "hotplugged") == 0 ||
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200531 strcmp(prop->name, "parent_bus") == 0) {
532 continue;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500533 }
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200534
535 /* Skip legacy properties since they are just string versions of
536 * properties that we already list.
537 */
538 if (strstart(prop->name, "legacy-", NULL)) {
539 continue;
540 }
541
Gonglei07d09c52014-10-07 14:33:23 +0800542 info = make_device_property_info(klass, prop->name, prop->type,
543 prop->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200544 if (!info) {
545 continue;
546 }
547
548 entry = g_malloc0(sizeof(*entry));
549 entry->value = info;
550 entry->next = prop_list;
551 prop_list = entry;
552 }
553
554 object_unref(obj);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500555
556 return prop_list;
557}
Anthony Liguorie4e31c62012-08-10 11:04:13 -0500558
Alexey Kardashevskiy961c47b2018-03-02 00:09:39 +1100559ObjectPropertyInfoList *qmp_qom_list_properties(const char *typename,
560 Error **errp)
561{
562 ObjectClass *klass;
563 Object *obj = NULL;
564 ObjectProperty *prop;
565 ObjectPropertyIterator iter;
566 ObjectPropertyInfoList *prop_list = NULL;
567
568 klass = object_class_by_name(typename);
569 if (klass == NULL) {
570 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
571 "Class '%s' not found", typename);
572 return NULL;
573 }
574
575 klass = object_class_dynamic_cast(klass, TYPE_OBJECT);
576 if (klass == NULL) {
577 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename", TYPE_OBJECT);
578 return NULL;
579 }
580
581 if (object_class_is_abstract(klass)) {
582 object_class_property_iter_init(&iter, klass);
583 } else {
584 obj = object_new(typename);
585 object_property_iter_init(&iter, obj);
586 }
587 while ((prop = object_property_iter_next(&iter))) {
588 ObjectPropertyInfo *info;
589 ObjectPropertyInfoList *entry;
590
591 info = g_malloc0(sizeof(*info));
592 info->name = g_strdup(prop->name);
593 info->type = g_strdup(prop->type);
594 info->has_description = !!prop->description;
595 info->description = g_strdup(prop->description);
596
597 entry = g_malloc0(sizeof(*entry));
598 entry->value = info;
599 entry->next = prop_list;
600 prop_list = entry;
601 }
602
603 object_unref(obj);
604
605 return prop_list;
606}
607
Anthony Liguori76b64a72012-08-14 22:17:36 -0500608CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
609{
610 return arch_query_cpu_definitions(errp);
611}
612
David Hildenbrande09484e2016-09-05 10:52:39 +0200613CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
614 CpuModelInfo *model,
615 Error **errp)
616{
617 return arch_query_cpu_model_expansion(type, model, errp);
618}
619
David Hildenbrand0031e0d2016-09-05 10:52:40 +0200620CpuModelCompareInfo *qmp_query_cpu_model_comparison(CpuModelInfo *modela,
621 CpuModelInfo *modelb,
622 Error **errp)
623{
624 return arch_query_cpu_model_comparison(modela, modelb, errp);
625}
626
David Hildenbrandb18b6042016-09-05 10:52:41 +0200627CpuModelBaselineInfo *qmp_query_cpu_model_baseline(CpuModelInfo *modela,
628 CpuModelInfo *modelb,
629 Error **errp)
630{
631 return arch_query_cpu_model_baseline(modela, modelb, errp);
632}
633
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300634void qmp_add_client(const char *protocol, const char *fdname,
635 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
636 Error **errp)
637{
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300638 Chardev *s;
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300639 int fd;
640
641 fd = monitor_get_fd(cur_mon, fdname, errp);
642 if (fd < 0) {
643 return;
644 }
645
646 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100647 if (!qemu_using_spice(errp)) {
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300648 close(fd);
649 return;
650 }
651 skipauth = has_skipauth ? skipauth : false;
652 tls = has_tls ? tls : false;
653 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
654 error_setg(errp, "spice failed to add client");
655 close(fd);
656 }
657 return;
658#ifdef CONFIG_VNC
659 } else if (strcmp(protocol, "vnc") == 0) {
660 skipauth = has_skipauth ? skipauth : false;
661 vnc_display_add_client(NULL, fd, skipauth);
662 return;
663#endif
664 } else if ((s = qemu_chr_find(protocol)) != NULL) {
665 if (qemu_chr_add_client(s, fd) < 0) {
666 error_setg(errp, "failed to add client");
667 close(fd);
668 return;
669 }
670 return;
671 }
672
673 error_setg(errp, "protocol '%s' is invalid", protocol);
674 close(fd);
675}
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100676
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100677
Markus Armbruster6eb39372015-09-16 13:06:25 +0200678void qmp_object_add(const char *type, const char *id,
679 bool has_props, QObject *props, Error **errp)
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100680{
Marc-André Lureaue64c75a2016-09-23 00:39:25 +0400681 QDict *pdict;
Eric Blakeb70ce102016-06-09 10:48:38 -0600682 Visitor *v;
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000683 Object *obj;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100684
685 if (props) {
Max Reitz7dc847e2018-02-24 16:40:29 +0100686 pdict = qobject_to(QDict, props);
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100687 if (!pdict) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100688 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
689 return;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100690 }
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200691 qobject_ref(pdict);
Marc-André Lureaue64c75a2016-09-23 00:39:25 +0400692 } else {
693 pdict = qdict_new();
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100694 }
695
Markus Armbruster048abb72017-03-03 13:32:39 +0100696 v = qobject_input_visitor_new(QOBJECT(pdict));
Eric Blakeb70ce102016-06-09 10:48:38 -0600697 obj = user_creatable_add_type(type, id, pdict, v, errp);
698 visit_free(v);
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000699 if (obj) {
700 object_unref(obj);
701 }
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200702 qobject_unref(pdict);
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100703}
704
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100705void qmp_object_del(const char *id, Error **errp)
706{
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000707 user_creatable_del(id, errp);
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100708}
Igor Mammedov6f2e2732014-06-16 19:12:25 +0200709
710MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
711{
David Hildenbrand2cc0e2e2018-04-23 18:51:16 +0200712 return qmp_memory_device_list();
Igor Mammedov6f2e2732014-06-16 19:12:25 +0200713}
Igor Mammedov02419bc2014-06-16 19:12:28 +0200714
715ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
716{
717 bool ambig;
718 ACPIOSTInfoList *head = NULL;
719 ACPIOSTInfoList **prev = &head;
720 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
721
722 if (obj) {
723 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
724 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
725
726 adevc->ospm_status(adev, &prev);
727 } else {
728 error_setg(errp, "command is not supported, missing ACPI device");
729 }
730
731 return head;
732}
Vadim Galitsyn9aa33972017-08-29 17:30:21 +0200733
734MemoryInfo *qmp_query_memory_size_summary(Error **errp)
735{
736 MemoryInfo *mem_info = g_malloc0(sizeof(MemoryInfo));
737
738 mem_info->base_memory = ram_size;
739
740 mem_info->plugged_memory = get_plugged_memory_size();
741 mem_info->has_plugged_memory =
742 mem_info->plugged_memory != (uint64_t)-1;
743
744 return mem_info;
745}