blob: ba82e1df9f462d80db7d9f25cb987036dfe2be05 [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"
Igor Mammedov6f2e2732014-06-16 19:12:25 +020042#include "hw/mem/pc-dimm.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
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200132#ifndef CONFIG_VNC
133/* If VNC support is enabled, the "true" query-vnc command is
134 defined in the VNC subsystem */
135VncInfo *qmp_query_vnc(Error **errp)
136{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100137 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200138 return NULL;
139};
Leon Yu89db21772015-02-02 05:08:51 +0000140
141VncInfo2List *qmp_query_vnc_servers(Error **errp)
142{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100143 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Leon Yu89db21772015-02-02 05:08:51 +0000144 return NULL;
145};
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200146#endif
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200147
148#ifndef CONFIG_SPICE
Markus Armbrusterad0ec142015-01-13 15:56:11 +0100149/*
Markus Armbruster1ef1cee2018-02-11 10:36:06 +0100150 * qmp_unregister_commands_hack() ensures that QMP command query-spice
151 * exists only #ifdef CONFIG_SPICE. Necessary for an accurate
152 * query-commands result. However, the QAPI schema is blissfully
153 * unaware of that, and the QAPI code generator happily generates a
154 * dead qmp_marshal_query_spice() that calls qmp_query_spice().
155 * Provide it one, or else linking fails. FIXME Educate the QAPI
156 * schema on CONFIG_SPICE.
Markus Armbrusterad0ec142015-01-13 15:56:11 +0100157 */
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200158SpiceInfo *qmp_query_spice(Error **errp)
159{
Markus Armbrusterad0ec142015-01-13 15:56:11 +0100160 abort();
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200161};
162#endif
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200163
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200164void qmp_cont(Error **errp)
165{
Max Reitz373340b2015-10-19 17:53:22 +0200166 BlockBackend *blk;
Daniel P. Berrange788cf9f2017-06-23 17:24:15 +0100167 Error *local_err = NULL;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200168
Peter Xu65d64f32016-02-18 13:16:49 +0800169 /* if there is a dump in background, we should wait until the dump
170 * finished */
171 if (dump_in_progress()) {
172 error_setg(errp, "There is a dump in process, please wait.");
173 return;
174 }
175
Hu Taoede085b2013-04-26 11:24:40 +0800176 if (runstate_needs_reset()) {
Cole Robinsonf231b882014-03-21 19:42:26 -0400177 error_setg(errp, "Resetting the Virtual Machine is required");
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200178 return;
Luiz Capitulinoad02b962012-04-27 13:33:36 -0300179 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
180 return;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200181 }
182
Max Reitz373340b2015-10-19 17:53:22 +0200183 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
184 blk_iostatus_reset(blk);
Markus Armbrusterab319792014-05-02 13:26:42 +0200185 }
Kevin Wolf7c8eece2016-03-22 18:58:50 +0100186
Kevin Wolf76b1c7f2015-12-22 14:07:08 +0100187 /* Continuing after completed migration. Images have been inactivated to
Kevin Wolface21a52017-05-04 18:52:36 +0200188 * allow the destination to take control. Need to get control back now.
189 *
190 * If there are no inactive block nodes (e.g. because the VM was just
191 * paused rather than completing a migration), bdrv_inactivate_all() simply
192 * doesn't do anything. */
193 bdrv_invalidate_cache_all(&local_err);
194 if (local_err) {
195 error_propagate(errp, local_err);
196 return;
Kevin Wolf76b1c7f2015-12-22 14:07:08 +0100197 }
198
Paolo Bonzini1e998142012-10-23 14:54:21 +0200199 if (runstate_check(RUN_STATE_INMIGRATE)) {
200 autostart = 1;
201 } else {
202 vm_start();
203 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200204}
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600205
Gerd Hoffmann9b9df252012-02-23 13:45:21 +0100206void qmp_system_wakeup(Error **errp)
207{
208 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
209}
210
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600211ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600212{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600213 Object *obj;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600214 bool ambiguous = false;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600215 ObjectPropertyInfoList *props = NULL;
216 ObjectProperty *prop;
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000217 ObjectPropertyIterator iter;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600218
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600219 obj = object_resolve_path(path, &ambiguous);
220 if (obj == NULL) {
Michael Tokarev79772082014-05-04 12:23:59 +0400221 if (ambiguous) {
222 error_setg(errp, "Path '%s' is ambiguous", path);
223 } else {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100224 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
225 "Device '%s' not found", path);
Michael Tokarev79772082014-05-04 12:23:59 +0400226 }
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600227 return NULL;
228 }
229
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000230 object_property_iter_init(&iter, obj);
231 while ((prop = object_property_iter_next(&iter))) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600232 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600233
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600234 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600235 entry->next = props;
236 props = entry;
237
238 entry->value->name = g_strdup(prop->name);
239 entry->value->type = g_strdup(prop->type);
240 }
241
242 return props;
243}
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600244
Markus Armbruster6eb39372015-09-16 13:06:25 +0200245void qmp_qom_set(const char *path, const char *property, QObject *value,
246 Error **errp)
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600247{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600248 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600249
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600250 obj = object_resolve_path(path, NULL);
251 if (!obj) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100252 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Markus Armbruster75158eb2015-03-16 08:57:47 +0100253 "Device '%s' not found", path);
Markus Armbruster485febc2015-03-13 17:25:50 +0100254 return;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600255 }
256
Markus Armbruster485febc2015-03-13 17:25:50 +0100257 object_property_set_qobject(obj, value, property, errp);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600258}
259
Markus Armbruster6eb39372015-09-16 13:06:25 +0200260QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600261{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600262 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600263
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600264 obj = object_resolve_path(path, NULL);
265 if (!obj) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100266 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Markus Armbruster75158eb2015-03-16 08:57:47 +0100267 "Device '%s' not found", path);
Markus Armbruster6eb39372015-09-16 13:06:25 +0200268 return NULL;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600269 }
270
Markus Armbruster6eb39372015-09-16 13:06:25 +0200271 return object_property_get_qobject(obj, property, errp);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600272}
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200273
274void qmp_set_password(const char *protocol, const char *password,
275 bool has_connected, const char *connected, Error **errp)
276{
277 int disconnect_if_connected = 0;
278 int fail_if_connected = 0;
279 int rc;
280
281 if (has_connected) {
282 if (strcmp(connected, "fail") == 0) {
283 fail_if_connected = 1;
284 } else if (strcmp(connected, "disconnect") == 0) {
285 disconnect_if_connected = 1;
286 } else if (strcmp(connected, "keep") == 0) {
287 /* nothing */
288 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100289 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200290 return;
291 }
292 }
293
294 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100295 if (!qemu_using_spice(errp)) {
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200296 return;
297 }
298 rc = qemu_spice_set_passwd(password, fail_if_connected,
299 disconnect_if_connected);
300 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100301 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200302 }
303 return;
304 }
305
306 if (strcmp(protocol, "vnc") == 0) {
307 if (fail_if_connected || disconnect_if_connected) {
308 /* vnc supports "connected=keep" only */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100309 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200310 return;
311 }
312 /* Note that setting an empty password will not disable login through
313 * this interface. */
314 rc = vnc_display_password(NULL, password);
315 if (rc < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100316 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200317 }
318 return;
319 }
320
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100321 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200322}
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200323
324void qmp_expire_password(const char *protocol, const char *whenstr,
325 Error **errp)
326{
327 time_t when;
328 int rc;
329
330 if (strcmp(whenstr, "now") == 0) {
331 when = 0;
332 } else if (strcmp(whenstr, "never") == 0) {
333 when = TIME_MAX;
334 } else if (whenstr[0] == '+') {
335 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
336 } else {
337 when = strtoull(whenstr, NULL, 10);
338 }
339
340 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100341 if (!qemu_using_spice(errp)) {
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200342 return;
343 }
344 rc = qemu_spice_set_pw_expire(when);
345 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100346 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200347 }
348 return;
349 }
350
351 if (strcmp(protocol, "vnc") == 0) {
352 rc = vnc_display_pw_expire(NULL, when);
353 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100354 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200355 }
356 return;
357 }
358
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100359 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200360}
Luiz Capitulino270b2432011-12-08 11:45:55 -0200361
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200362#ifdef CONFIG_VNC
Luiz Capitulino270b2432011-12-08 11:45:55 -0200363void qmp_change_vnc_password(const char *password, Error **errp)
364{
365 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100366 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino270b2432011-12-08 11:45:55 -0200367 }
368}
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200369
Paolo Bonzini007fcd3e2012-10-18 09:01:01 +0200370static void qmp_change_vnc_listen(const char *target, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200371{
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200372 QemuOptsList *olist = qemu_find_opts("vnc");
373 QemuOpts *opts;
374
375 if (strstr(target, "id=")) {
376 error_setg(errp, "id not supported");
377 return;
378 }
379
380 opts = qemu_opts_find(olist, "default");
381 if (opts) {
382 qemu_opts_del(opts);
383 }
Markus Armbruster70b94332015-02-13 12:50:26 +0100384 opts = vnc_parse(target, errp);
Gongleif7801c52015-02-05 17:43:35 +0800385 if (!opts) {
386 return;
387 }
388
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200389 vnc_display_open("default", errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200390}
391
392static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
393 Error **errp)
394{
395 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
396 if (!has_arg) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100397 error_setg(errp, QERR_MISSING_PARAMETER, "password");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200398 } else {
399 qmp_change_vnc_password(arg, errp);
400 }
401 } else {
402 qmp_change_vnc_listen(target, errp);
403 }
404}
405#else
406void qmp_change_vnc_password(const char *password, Error **errp)
407{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100408 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200409}
410static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
411 Error **errp)
412{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100413 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200414}
415#endif /* !CONFIG_VNC */
416
417void qmp_change(const char *device, const char *target,
Markus Armbruster7daecb32014-05-02 13:26:31 +0200418 bool has_arg, const char *arg, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200419{
420 if (strcmp(device, "vnc") == 0) {
Markus Armbruster7daecb32014-05-02 13:26:31 +0200421 qmp_change_vnc(target, has_arg, arg, errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200422 } else {
Kevin Wolf70e2cb32016-09-20 13:38:47 +0200423 qmp_blockdev_change_medium(true, device, false, NULL, target,
424 has_arg, arg, false, 0, errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200425 }
426}
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600427
428static void qom_list_types_tramp(ObjectClass *klass, void *data)
429{
430 ObjectTypeInfoList *e, **pret = data;
431 ObjectTypeInfo *info;
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300432 ObjectClass *parent = object_class_get_parent(klass);
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600433
434 info = g_malloc0(sizeof(*info));
435 info->name = g_strdup(object_class_get_name(klass));
Eduardo Habkost87467ea2017-07-07 09:22:14 -0300436 info->has_abstract = info->abstract = object_class_is_abstract(klass);
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300437 if (parent) {
438 info->has_parent = true;
439 info->parent = g_strdup(object_class_get_name(parent));
440 }
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600441
442 e = g_malloc0(sizeof(*e));
443 e->value = info;
444 e->next = *pret;
445 *pret = e;
446}
447
448ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
449 const char *implements,
450 bool has_abstract,
451 bool abstract,
452 Error **errp)
453{
454 ObjectTypeInfoList *ret = NULL;
455
456 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
457
458 return ret;
459}
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500460
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200461/* Return a DevicePropertyInfo for a qdev property.
462 *
463 * If a qdev property with the given name does not exist, use the given default
464 * type. If the qdev property info should not be shown, return NULL.
465 *
466 * The caller must free the return value.
467 */
468static DevicePropertyInfo *make_device_property_info(ObjectClass *klass,
469 const char *name,
Gonglei07d09c52014-10-07 14:33:23 +0800470 const char *default_type,
471 const char *description)
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200472{
473 DevicePropertyInfo *info;
474 Property *prop;
475
476 do {
477 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
478 if (strcmp(name, prop->name) != 0) {
479 continue;
480 }
481
482 /*
483 * TODO Properties without a parser are just for dirty hacks.
484 * qdev_prop_ptr is the only such PropertyInfo. It's marked
485 * for removal. This conditional should be removed along with
486 * it.
487 */
Fam Zhengfaabdbb2017-07-14 10:14:51 +0800488 if (!prop->info->set && !prop->info->create) {
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200489 return NULL; /* no way to set it, don't show */
490 }
491
492 info = g_malloc0(sizeof(*info));
493 info->name = g_strdup(prop->name);
Fam Zheng75ab9052017-07-14 10:14:53 +0800494 info->type = default_type ? g_strdup(default_type)
495 : g_strdup(prop->info->name);
Gonglei07d09c52014-10-07 14:33:23 +0800496 info->has_description = !!prop->info->description;
497 info->description = g_strdup(prop->info->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200498 return info;
499 }
500 klass = object_class_get_parent(klass);
501 } while (klass != object_class_by_name(TYPE_DEVICE));
502
503 /* Not a qdev property, use the default type */
504 info = g_malloc0(sizeof(*info));
505 info->name = g_strdup(name);
506 info->type = g_strdup(default_type);
Gonglei07d09c52014-10-07 14:33:23 +0800507 info->has_description = !!description;
508 info->description = g_strdup(description);
509
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200510 return info;
511}
512
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500513DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
514 Error **errp)
515{
516 ObjectClass *klass;
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200517 Object *obj;
518 ObjectProperty *prop;
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000519 ObjectPropertyIterator iter;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500520 DevicePropertyInfoList *prop_list = NULL;
521
522 klass = object_class_by_name(typename);
523 if (klass == NULL) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100524 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
525 "Device '%s' not found", typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500526 return NULL;
527 }
528
529 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
530 if (klass == NULL) {
Lin Ma7a8c1532017-01-25 13:27:03 +0800531 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename", TYPE_DEVICE);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500532 return NULL;
533 }
534
Markus Armbrusteredb15232015-10-01 10:59:57 +0200535 if (object_class_is_abstract(klass)) {
Lin Ma7a8c1532017-01-25 13:27:03 +0800536 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename",
Markus Armbrusteredb15232015-10-01 10:59:57 +0200537 "non-abstract device type");
538 return NULL;
539 }
540
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200541 obj = object_new(typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500542
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000543 object_property_iter_init(&iter, obj);
544 while ((prop = object_property_iter_next(&iter))) {
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200545 DevicePropertyInfo *info;
546 DevicePropertyInfoList *entry;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500547
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200548 /* Skip Object and DeviceState properties */
549 if (strcmp(prop->name, "type") == 0 ||
550 strcmp(prop->name, "realized") == 0 ||
551 strcmp(prop->name, "hotpluggable") == 0 ||
Stefan Hajnoczi4115dd62014-07-09 14:01:31 +0200552 strcmp(prop->name, "hotplugged") == 0 ||
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200553 strcmp(prop->name, "parent_bus") == 0) {
554 continue;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500555 }
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200556
557 /* Skip legacy properties since they are just string versions of
558 * properties that we already list.
559 */
560 if (strstart(prop->name, "legacy-", NULL)) {
561 continue;
562 }
563
Gonglei07d09c52014-10-07 14:33:23 +0800564 info = make_device_property_info(klass, prop->name, prop->type,
565 prop->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200566 if (!info) {
567 continue;
568 }
569
570 entry = g_malloc0(sizeof(*entry));
571 entry->value = info;
572 entry->next = prop_list;
573 prop_list = entry;
574 }
575
576 object_unref(obj);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500577
578 return prop_list;
579}
Anthony Liguorie4e31c62012-08-10 11:04:13 -0500580
Anthony Liguori76b64a72012-08-14 22:17:36 -0500581CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
582{
583 return arch_query_cpu_definitions(errp);
584}
585
David Hildenbrande09484e2016-09-05 10:52:39 +0200586CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
587 CpuModelInfo *model,
588 Error **errp)
589{
590 return arch_query_cpu_model_expansion(type, model, errp);
591}
592
David Hildenbrand0031e0d2016-09-05 10:52:40 +0200593CpuModelCompareInfo *qmp_query_cpu_model_comparison(CpuModelInfo *modela,
594 CpuModelInfo *modelb,
595 Error **errp)
596{
597 return arch_query_cpu_model_comparison(modela, modelb, errp);
598}
599
David Hildenbrandb18b6042016-09-05 10:52:41 +0200600CpuModelBaselineInfo *qmp_query_cpu_model_baseline(CpuModelInfo *modela,
601 CpuModelInfo *modelb,
602 Error **errp)
603{
604 return arch_query_cpu_model_baseline(modela, modelb, errp);
605}
606
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300607void qmp_add_client(const char *protocol, const char *fdname,
608 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
609 Error **errp)
610{
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300611 Chardev *s;
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300612 int fd;
613
614 fd = monitor_get_fd(cur_mon, fdname, errp);
615 if (fd < 0) {
616 return;
617 }
618
619 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100620 if (!qemu_using_spice(errp)) {
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300621 close(fd);
622 return;
623 }
624 skipauth = has_skipauth ? skipauth : false;
625 tls = has_tls ? tls : false;
626 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
627 error_setg(errp, "spice failed to add client");
628 close(fd);
629 }
630 return;
631#ifdef CONFIG_VNC
632 } else if (strcmp(protocol, "vnc") == 0) {
633 skipauth = has_skipauth ? skipauth : false;
634 vnc_display_add_client(NULL, fd, skipauth);
635 return;
636#endif
637 } else if ((s = qemu_chr_find(protocol)) != NULL) {
638 if (qemu_chr_add_client(s, fd) < 0) {
639 error_setg(errp, "failed to add client");
640 close(fd);
641 return;
642 }
643 return;
644 }
645
646 error_setg(errp, "protocol '%s' is invalid", protocol);
647 close(fd);
648}
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100649
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100650
Markus Armbruster6eb39372015-09-16 13:06:25 +0200651void qmp_object_add(const char *type, const char *id,
652 bool has_props, QObject *props, Error **errp)
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100653{
Marc-André Lureaue64c75a2016-09-23 00:39:25 +0400654 QDict *pdict;
Eric Blakeb70ce1012016-06-09 10:48:38 -0600655 Visitor *v;
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000656 Object *obj;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100657
658 if (props) {
659 pdict = qobject_to_qdict(props);
660 if (!pdict) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100661 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
662 return;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100663 }
Marc-André Lureaue64c75a2016-09-23 00:39:25 +0400664 QINCREF(pdict);
665 } else {
666 pdict = qdict_new();
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100667 }
668
Markus Armbruster048abb72017-03-03 13:32:39 +0100669 v = qobject_input_visitor_new(QOBJECT(pdict));
Eric Blakeb70ce1012016-06-09 10:48:38 -0600670 obj = user_creatable_add_type(type, id, pdict, v, errp);
671 visit_free(v);
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000672 if (obj) {
673 object_unref(obj);
674 }
Marc-André Lureaue64c75a2016-09-23 00:39:25 +0400675 QDECREF(pdict);
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100676}
677
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100678void qmp_object_del(const char *id, Error **errp)
679{
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000680 user_creatable_del(id, errp);
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100681}
Igor Mammedov6f2e2732014-06-16 19:12:25 +0200682
683MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
684{
685 MemoryDeviceInfoList *head = NULL;
686 MemoryDeviceInfoList **prev = &head;
687
688 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
689
690 return head;
691}
Igor Mammedov02419bc2014-06-16 19:12:28 +0200692
693ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
694{
695 bool ambig;
696 ACPIOSTInfoList *head = NULL;
697 ACPIOSTInfoList **prev = &head;
698 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
699
700 if (obj) {
701 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
702 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
703
704 adevc->ospm_status(adev, &prev);
705 } else {
706 error_setg(errp, "command is not supported, missing ACPI device");
707 }
708
709 return head;
710}
Vadim Galitsyn9aa33972017-08-29 17:30:21 +0200711
712MemoryInfo *qmp_query_memory_size_summary(Error **errp)
713{
714 MemoryInfo *mem_info = g_malloc0(sizeof(MemoryInfo));
715
716 mem_info->base_memory = ram_size;
717
718 mem_info->plugged_memory = get_plugged_memory_size();
719 mem_info->has_plugged_memory =
720 mem_info->plugged_memory != (uint64_t)-1;
721
722 return mem_info;
723}