blob: fa1b3c15773764f54204a57114a20a206fb4f089 [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;
Vladimir Sementsov-Ogievskiy9183dd12019-01-24 15:25:24 +0300159 } else if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
160 error_setg(errp, "Migration is not finalized yet");
161 return;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200162 }
163
Max Reitz373340b2015-10-19 17:53:22 +0200164 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
165 blk_iostatus_reset(blk);
Markus Armbrusterab319792014-05-02 13:26:42 +0200166 }
Kevin Wolf7c8eece2016-03-22 18:58:50 +0100167
Kevin Wolf76b1c7f2015-12-22 14:07:08 +0100168 /* Continuing after completed migration. Images have been inactivated to
Kevin Wolface21a52017-05-04 18:52:36 +0200169 * allow the destination to take control. Need to get control back now.
170 *
171 * If there are no inactive block nodes (e.g. because the VM was just
172 * paused rather than completing a migration), bdrv_inactivate_all() simply
173 * doesn't do anything. */
174 bdrv_invalidate_cache_all(&local_err);
175 if (local_err) {
176 error_propagate(errp, local_err);
177 return;
Kevin Wolf76b1c7f2015-12-22 14:07:08 +0100178 }
179
Paolo Bonzini1e998142012-10-23 14:54:21 +0200180 if (runstate_check(RUN_STATE_INMIGRATE)) {
181 autostart = 1;
182 } else {
183 vm_start();
184 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200185}
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600186
Gerd Hoffmann9b9df252012-02-23 13:45:21 +0100187void qmp_system_wakeup(Error **errp)
188{
Daniel Henrique Barbozafb064112018-12-05 17:47:01 -0200189 if (!qemu_wakeup_suspend_enabled()) {
190 error_setg(errp,
191 "wake-up from suspend is not supported by this guest");
192 return;
193 }
194
195 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, errp);
Gerd Hoffmann9b9df252012-02-23 13:45:21 +0100196}
197
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600198ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600199{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600200 Object *obj;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600201 bool ambiguous = false;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600202 ObjectPropertyInfoList *props = NULL;
203 ObjectProperty *prop;
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000204 ObjectPropertyIterator iter;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600205
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600206 obj = object_resolve_path(path, &ambiguous);
207 if (obj == NULL) {
Michael Tokarev79772082014-05-04 12:23:59 +0400208 if (ambiguous) {
209 error_setg(errp, "Path '%s' is ambiguous", path);
210 } else {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100211 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
212 "Device '%s' not found", path);
Michael Tokarev79772082014-05-04 12:23:59 +0400213 }
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600214 return NULL;
215 }
216
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000217 object_property_iter_init(&iter, obj);
218 while ((prop = object_property_iter_next(&iter))) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600219 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600220
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600221 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600222 entry->next = props;
223 props = entry;
224
225 entry->value->name = g_strdup(prop->name);
226 entry->value->type = g_strdup(prop->type);
227 }
228
229 return props;
230}
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600231
Markus Armbruster6eb39372015-09-16 13:06:25 +0200232void qmp_qom_set(const char *path, const char *property, QObject *value,
233 Error **errp)
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600234{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600235 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600236
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600237 obj = object_resolve_path(path, NULL);
238 if (!obj) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100239 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Markus Armbruster75158eb2015-03-16 08:57:47 +0100240 "Device '%s' not found", path);
Markus Armbruster485febc2015-03-13 17:25:50 +0100241 return;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600242 }
243
Markus Armbruster485febc2015-03-13 17:25:50 +0100244 object_property_set_qobject(obj, value, property, errp);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600245}
246
Markus Armbruster6eb39372015-09-16 13:06:25 +0200247QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600248{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600249 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600250
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600251 obj = object_resolve_path(path, NULL);
252 if (!obj) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100253 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Markus Armbruster75158eb2015-03-16 08:57:47 +0100254 "Device '%s' not found", path);
Markus Armbruster6eb39372015-09-16 13:06:25 +0200255 return NULL;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600256 }
257
Markus Armbruster6eb39372015-09-16 13:06:25 +0200258 return object_property_get_qobject(obj, property, errp);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600259}
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200260
261void qmp_set_password(const char *protocol, const char *password,
262 bool has_connected, const char *connected, Error **errp)
263{
264 int disconnect_if_connected = 0;
265 int fail_if_connected = 0;
266 int rc;
267
268 if (has_connected) {
269 if (strcmp(connected, "fail") == 0) {
270 fail_if_connected = 1;
271 } else if (strcmp(connected, "disconnect") == 0) {
272 disconnect_if_connected = 1;
273 } else if (strcmp(connected, "keep") == 0) {
274 /* nothing */
275 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100276 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200277 return;
278 }
279 }
280
281 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100282 if (!qemu_using_spice(errp)) {
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200283 return;
284 }
285 rc = qemu_spice_set_passwd(password, fail_if_connected,
286 disconnect_if_connected);
287 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100288 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200289 }
290 return;
291 }
292
293 if (strcmp(protocol, "vnc") == 0) {
294 if (fail_if_connected || disconnect_if_connected) {
295 /* vnc supports "connected=keep" only */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100296 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200297 return;
298 }
299 /* Note that setting an empty password will not disable login through
300 * this interface. */
301 rc = vnc_display_password(NULL, password);
302 if (rc < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100303 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200304 }
305 return;
306 }
307
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100308 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200309}
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200310
311void qmp_expire_password(const char *protocol, const char *whenstr,
312 Error **errp)
313{
314 time_t when;
315 int rc;
316
317 if (strcmp(whenstr, "now") == 0) {
318 when = 0;
319 } else if (strcmp(whenstr, "never") == 0) {
320 when = TIME_MAX;
321 } else if (whenstr[0] == '+') {
322 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
323 } else {
324 when = strtoull(whenstr, NULL, 10);
325 }
326
327 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100328 if (!qemu_using_spice(errp)) {
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200329 return;
330 }
331 rc = qemu_spice_set_pw_expire(when);
332 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100333 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200334 }
335 return;
336 }
337
338 if (strcmp(protocol, "vnc") == 0) {
339 rc = vnc_display_pw_expire(NULL, when);
340 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100341 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200342 }
343 return;
344 }
345
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100346 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200347}
Luiz Capitulino270b2432011-12-08 11:45:55 -0200348
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200349#ifdef CONFIG_VNC
Luiz Capitulino270b2432011-12-08 11:45:55 -0200350void qmp_change_vnc_password(const char *password, Error **errp)
351{
352 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100353 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino270b2432011-12-08 11:45:55 -0200354 }
355}
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200356
Paolo Bonzini007fcd3e2012-10-18 09:01:01 +0200357static void qmp_change_vnc_listen(const char *target, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200358{
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200359 QemuOptsList *olist = qemu_find_opts("vnc");
360 QemuOpts *opts;
361
362 if (strstr(target, "id=")) {
363 error_setg(errp, "id not supported");
364 return;
365 }
366
367 opts = qemu_opts_find(olist, "default");
368 if (opts) {
369 qemu_opts_del(opts);
370 }
Markus Armbruster70b94332015-02-13 12:50:26 +0100371 opts = vnc_parse(target, errp);
Gongleif7801c52015-02-05 17:43:35 +0800372 if (!opts) {
373 return;
374 }
375
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200376 vnc_display_open("default", errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200377}
378
379static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
380 Error **errp)
381{
382 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
383 if (!has_arg) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100384 error_setg(errp, QERR_MISSING_PARAMETER, "password");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200385 } else {
386 qmp_change_vnc_password(arg, errp);
387 }
388 } else {
389 qmp_change_vnc_listen(target, errp);
390 }
391}
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200392#endif /* !CONFIG_VNC */
393
394void qmp_change(const char *device, const char *target,
Markus Armbruster7daecb32014-05-02 13:26:31 +0200395 bool has_arg, const char *arg, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200396{
397 if (strcmp(device, "vnc") == 0) {
Marc-André Lureau05eb4a22018-07-03 17:56:47 +0200398#ifdef CONFIG_VNC
Markus Armbruster7daecb32014-05-02 13:26:31 +0200399 qmp_change_vnc(target, has_arg, arg, errp);
Marc-André Lureau05eb4a22018-07-03 17:56:47 +0200400#else
401 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
402#endif
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200403 } else {
Kevin Wolf70e2cb32016-09-20 13:38:47 +0200404 qmp_blockdev_change_medium(true, device, false, NULL, target,
405 has_arg, arg, false, 0, errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200406 }
407}
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600408
409static void qom_list_types_tramp(ObjectClass *klass, void *data)
410{
411 ObjectTypeInfoList *e, **pret = data;
412 ObjectTypeInfo *info;
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300413 ObjectClass *parent = object_class_get_parent(klass);
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600414
415 info = g_malloc0(sizeof(*info));
416 info->name = g_strdup(object_class_get_name(klass));
Eduardo Habkost87467ea2017-07-07 09:22:14 -0300417 info->has_abstract = info->abstract = object_class_is_abstract(klass);
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300418 if (parent) {
419 info->has_parent = true;
420 info->parent = g_strdup(object_class_get_name(parent));
421 }
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600422
423 e = g_malloc0(sizeof(*e));
424 e->value = info;
425 e->next = *pret;
426 *pret = e;
427}
428
429ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
430 const char *implements,
431 bool has_abstract,
432 bool abstract,
433 Error **errp)
434{
435 ObjectTypeInfoList *ret = NULL;
436
437 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
438
439 return ret;
440}
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500441
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200442/* Return a DevicePropertyInfo for a qdev property.
443 *
444 * If a qdev property with the given name does not exist, use the given default
445 * type. If the qdev property info should not be shown, return NULL.
446 *
447 * The caller must free the return value.
448 */
Alexey Kardashevskiy35f63762018-03-02 00:09:38 +1100449static ObjectPropertyInfo *make_device_property_info(ObjectClass *klass,
450 const char *name,
451 const char *default_type,
452 const char *description)
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200453{
Alexey Kardashevskiy35f63762018-03-02 00:09:38 +1100454 ObjectPropertyInfo *info;
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200455 Property *prop;
456
457 do {
458 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
459 if (strcmp(name, prop->name) != 0) {
460 continue;
461 }
462
463 /*
464 * TODO Properties without a parser are just for dirty hacks.
465 * qdev_prop_ptr is the only such PropertyInfo. It's marked
466 * for removal. This conditional should be removed along with
467 * it.
468 */
Fam Zhengfaabdbb2017-07-14 10:14:51 +0800469 if (!prop->info->set && !prop->info->create) {
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200470 return NULL; /* no way to set it, don't show */
471 }
472
473 info = g_malloc0(sizeof(*info));
474 info->name = g_strdup(prop->name);
Fam Zheng75ab9052017-07-14 10:14:53 +0800475 info->type = default_type ? g_strdup(default_type)
476 : g_strdup(prop->info->name);
Gonglei07d09c52014-10-07 14:33:23 +0800477 info->has_description = !!prop->info->description;
478 info->description = g_strdup(prop->info->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200479 return info;
480 }
481 klass = object_class_get_parent(klass);
482 } while (klass != object_class_by_name(TYPE_DEVICE));
483
484 /* Not a qdev property, use the default type */
485 info = g_malloc0(sizeof(*info));
486 info->name = g_strdup(name);
487 info->type = g_strdup(default_type);
Gonglei07d09c52014-10-07 14:33:23 +0800488 info->has_description = !!description;
489 info->description = g_strdup(description);
490
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200491 return info;
492}
493
Alexey Kardashevskiy35f63762018-03-02 00:09:38 +1100494ObjectPropertyInfoList *qmp_device_list_properties(const char *typename,
495 Error **errp)
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500496{
497 ObjectClass *klass;
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200498 Object *obj;
499 ObjectProperty *prop;
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000500 ObjectPropertyIterator iter;
Alexey Kardashevskiy35f63762018-03-02 00:09:38 +1100501 ObjectPropertyInfoList *prop_list = NULL;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500502
503 klass = object_class_by_name(typename);
504 if (klass == NULL) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100505 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
506 "Device '%s' not found", typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500507 return NULL;
508 }
509
510 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
511 if (klass == NULL) {
Lin Ma7a8c1532017-01-25 13:27:03 +0800512 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename", TYPE_DEVICE);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500513 return NULL;
514 }
515
Markus Armbrusteredb15232015-10-01 10:59:57 +0200516 if (object_class_is_abstract(klass)) {
Lin Ma7a8c1532017-01-25 13:27:03 +0800517 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename",
Markus Armbrusteredb15232015-10-01 10:59:57 +0200518 "non-abstract device type");
519 return NULL;
520 }
521
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200522 obj = object_new(typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500523
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000524 object_property_iter_init(&iter, obj);
525 while ((prop = object_property_iter_next(&iter))) {
Alexey Kardashevskiy35f63762018-03-02 00:09:38 +1100526 ObjectPropertyInfo *info;
527 ObjectPropertyInfoList *entry;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500528
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200529 /* Skip Object and DeviceState properties */
530 if (strcmp(prop->name, "type") == 0 ||
531 strcmp(prop->name, "realized") == 0 ||
532 strcmp(prop->name, "hotpluggable") == 0 ||
Stefan Hajnoczi4115dd62014-07-09 14:01:31 +0200533 strcmp(prop->name, "hotplugged") == 0 ||
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200534 strcmp(prop->name, "parent_bus") == 0) {
535 continue;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500536 }
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200537
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
Gonglei07d09c52014-10-07 14:33:23 +0800545 info = make_device_property_info(klass, prop->name, prop->type,
546 prop->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200547 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);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500558
559 return prop_list;
560}
Anthony Liguorie4e31c62012-08-10 11:04:13 -0500561
Alexey Kardashevskiy961c47b2018-03-02 00:09:39 +1100562ObjectPropertyInfoList *qmp_qom_list_properties(const char *typename,
563 Error **errp)
564{
565 ObjectClass *klass;
566 Object *obj = NULL;
567 ObjectProperty *prop;
568 ObjectPropertyIterator iter;
569 ObjectPropertyInfoList *prop_list = NULL;
570
571 klass = object_class_by_name(typename);
572 if (klass == NULL) {
573 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
574 "Class '%s' not found", typename);
575 return NULL;
576 }
577
578 klass = object_class_dynamic_cast(klass, TYPE_OBJECT);
579 if (klass == NULL) {
580 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename", TYPE_OBJECT);
581 return NULL;
582 }
583
584 if (object_class_is_abstract(klass)) {
585 object_class_property_iter_init(&iter, klass);
586 } else {
587 obj = object_new(typename);
588 object_property_iter_init(&iter, obj);
589 }
590 while ((prop = object_property_iter_next(&iter))) {
591 ObjectPropertyInfo *info;
592 ObjectPropertyInfoList *entry;
593
594 info = g_malloc0(sizeof(*info));
595 info->name = g_strdup(prop->name);
596 info->type = g_strdup(prop->type);
597 info->has_description = !!prop->description;
598 info->description = g_strdup(prop->description);
599
600 entry = g_malloc0(sizeof(*entry));
601 entry->value = info;
602 entry->next = prop_list;
603 prop_list = entry;
604 }
605
606 object_unref(obj);
607
608 return prop_list;
609}
610
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300611void qmp_add_client(const char *protocol, const char *fdname,
612 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
613 Error **errp)
614{
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300615 Chardev *s;
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300616 int fd;
617
618 fd = monitor_get_fd(cur_mon, fdname, errp);
619 if (fd < 0) {
620 return;
621 }
622
623 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100624 if (!qemu_using_spice(errp)) {
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300625 close(fd);
626 return;
627 }
628 skipauth = has_skipauth ? skipauth : false;
629 tls = has_tls ? tls : false;
630 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
631 error_setg(errp, "spice failed to add client");
632 close(fd);
633 }
634 return;
635#ifdef CONFIG_VNC
636 } else if (strcmp(protocol, "vnc") == 0) {
637 skipauth = has_skipauth ? skipauth : false;
638 vnc_display_add_client(NULL, fd, skipauth);
639 return;
640#endif
641 } else if ((s = qemu_chr_find(protocol)) != NULL) {
642 if (qemu_chr_add_client(s, fd) < 0) {
643 error_setg(errp, "failed to add client");
644 close(fd);
645 return;
646 }
647 return;
648 }
649
650 error_setg(errp, "protocol '%s' is invalid", protocol);
651 close(fd);
652}
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100653
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100654
Markus Armbruster6eb39372015-09-16 13:06:25 +0200655void qmp_object_add(const char *type, const char *id,
656 bool has_props, QObject *props, Error **errp)
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100657{
Marc-André Lureaue64c75a2016-09-23 00:39:25 +0400658 QDict *pdict;
Eric Blakeb70ce1012016-06-09 10:48:38 -0600659 Visitor *v;
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000660 Object *obj;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100661
662 if (props) {
Max Reitz7dc847e2018-02-24 16:40:29 +0100663 pdict = qobject_to(QDict, props);
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100664 if (!pdict) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100665 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
666 return;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100667 }
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200668 qobject_ref(pdict);
Marc-André Lureaue64c75a2016-09-23 00:39:25 +0400669 } else {
670 pdict = qdict_new();
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100671 }
672
Markus Armbruster048abb72017-03-03 13:32:39 +0100673 v = qobject_input_visitor_new(QOBJECT(pdict));
Eric Blakeb70ce1012016-06-09 10:48:38 -0600674 obj = user_creatable_add_type(type, id, pdict, v, errp);
675 visit_free(v);
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000676 if (obj) {
677 object_unref(obj);
678 }
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200679 qobject_unref(pdict);
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100680}
681
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100682void qmp_object_del(const char *id, Error **errp)
683{
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000684 user_creatable_del(id, errp);
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100685}
Igor Mammedov6f2e2732014-06-16 19:12:25 +0200686
687MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
688{
David Hildenbrand2cc0e2e2018-04-23 18:51:16 +0200689 return qmp_memory_device_list();
Igor Mammedov6f2e2732014-06-16 19:12:25 +0200690}
Igor Mammedov02419bc2014-06-16 19:12:28 +0200691
692ACPIOSTInfoList *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}
Vadim Galitsyn9aa33972017-08-29 17:30:21 +0200710
711MemoryInfo *qmp_query_memory_size_summary(Error **errp)
712{
713 MemoryInfo *mem_info = g_malloc0(sizeof(MemoryInfo));
714
715 mem_info->base_memory = ram_size;
716
717 mem_info->plugged_memory = get_plugged_memory_size();
718 mem_info->has_plugged_memory =
719 mem_info->plugged_memory != (uint64_t)-1;
720
721 return mem_info;
722}