blob: 73e46d795feaf1f5e98a251fecba0b4e0e209ca9 [file] [log] [blame]
Anthony Liguori48a32be2011-09-02 12:34:48 -05001/*
2 * QEMU Management Protocol
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010012 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
Anthony Liguori48a32be2011-09-02 12:34:48 -050014 */
15
Peter Maydelld38ea872016-01-29 17:50:05 +000016#include "qemu/osdep.h"
Fam Zheng67a1de02016-06-01 17:44:21 +080017#include "qemu-version.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020018#include "qemu/cutils.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010019#include "qemu/option.h"
Markus Armbrustera0b1a662015-03-17 18:16:21 +010020#include "monitor/monitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010021#include "sysemu/sysemu.h"
Marc-André Lureau213dcb02016-12-12 20:22:24 +030022#include "qemu/config-file.h"
Fam Zhengcea25272016-09-21 12:27:14 +080023#include "qemu/uuid.h"
Marc-André Lureau8228e352017-01-26 17:19:46 +040024#include "chardev/char.h"
Luiz Capitulinofbf796f2011-12-07 11:17:51 -020025#include "ui/qemu-spice.h"
26#include "ui/vnc.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010027#include "sysemu/kvm.h"
28#include "sysemu/arch_init.h"
Anthony Liguorib4b12c62011-12-12 14:29:34 -060029#include "hw/qdev.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010030#include "sysemu/blockdev.h"
Max Reitz373340b2015-10-19 17:53:22 +020031#include "sysemu/block-backend.h"
Paolo Bonzini14cccb62012-12-17 18:19:50 +010032#include "qom/qom-qobject.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010033#include "qapi/error.h"
Markus Armbruster112ed242018-02-26 17:13:27 -060034#include "qapi/qapi-commands-block-core.h"
35#include "qapi/qapi-commands-misc.h"
36#include "qapi/qapi-commands-ui.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010037#include "qapi/qmp/qdict.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010038#include "qapi/qmp/qerror.h"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010039#include "qapi/qobject-input-visitor.h"
Igor Mammedov69ca3ea2013-04-30 15:41:25 +020040#include "hw/boards.h"
Igor Mammedov269e09f2014-01-16 17:34:38 +010041#include "qom/object_interfaces.h"
David Hildenbrand2cc0e2e2018-04-23 18:51:16 +020042#include "hw/mem/memory-device.h"
Igor Mammedov02419bc2014-06-16 19:12:28 +020043#include "hw/acpi/acpi_dev_interface.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050044
45NameInfo *qmp_query_name(Error **errp)
46{
47 NameInfo *info = g_malloc0(sizeof(*info));
48
49 if (qemu_name) {
50 info->has_name = true;
51 info->name = g_strdup(qemu_name);
52 }
53
54 return info;
55}
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030056
Markus Armbruster7daecb32014-05-02 13:26:31 +020057VersionInfo *qmp_query_version(Error **errp)
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030058{
Eric Blake4752cdb2015-05-04 09:05:31 -060059 VersionInfo *info = g_new0(VersionInfo, 1);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030060
Eric Blake4752cdb2015-05-04 09:05:31 -060061 info->qemu = g_new0(VersionTriple, 1);
Marc-André Lureau3688d8c2016-09-12 13:18:56 +040062 info->qemu->major = QEMU_VERSION_MAJOR;
63 info->qemu->minor = QEMU_VERSION_MINOR;
64 info->qemu->micro = QEMU_VERSION_MICRO;
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030065 info->package = g_strdup(QEMU_PKGVERSION);
66
67 return info;
68}
Luiz Capitulino292a2602011-09-12 15:10:53 -030069
70KvmInfo *qmp_query_kvm(Error **errp)
71{
72 KvmInfo *info = g_malloc0(sizeof(*info));
73
74 info->enabled = kvm_enabled();
75 info->present = kvm_available();
76
77 return info;
78}
79
Luiz Capitulinoefab7672011-09-13 17:16:25 -030080UuidInfo *qmp_query_uuid(Error **errp)
81{
82 UuidInfo *info = g_malloc0(sizeof(*info));
Luiz Capitulinoefab7672011-09-13 17:16:25 -030083
Fam Zheng9c5ce8d2016-09-21 12:27:22 +080084 info->UUID = qemu_uuid_unparse_strdup(&qemu_uuid);
Luiz Capitulinoefab7672011-09-13 17:16:25 -030085 return info;
86}
87
Markus Armbruster7daecb32014-05-02 13:26:31 +020088void qmp_quit(Error **errp)
Luiz Capitulino7a7f3252011-09-15 14:20:28 -030089{
90 no_shutdown = 0;
Eric Blakecf83f142017-05-15 16:41:13 -050091 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP);
Luiz Capitulino7a7f3252011-09-15 14:20:28 -030092}
93
Luiz Capitulino5f158f22011-09-15 14:34:39 -030094void qmp_stop(Error **errp)
95{
Peter Xu65d64f32016-02-18 13:16:49 +080096 /* if there is a dump in background, we should wait until the dump
97 * finished */
98 if (dump_in_progress()) {
99 error_setg(errp, "There is a dump in process, please wait.");
100 return;
101 }
102
Paolo Bonzini1e998142012-10-23 14:54:21 +0200103 if (runstate_check(RUN_STATE_INMIGRATE)) {
104 autostart = 0;
105 } else {
106 vm_stop(RUN_STATE_PAUSED);
107 }
Luiz Capitulino5f158f22011-09-15 14:34:39 -0300108}
109
Luiz Capitulino38d22652011-09-15 14:41:46 -0300110void qmp_system_reset(Error **errp)
111{
Eric Blakecf83f142017-05-15 16:41:13 -0500112 qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP);
Luiz Capitulino38d22652011-09-15 14:41:46 -0300113}
Luiz Capitulino5bc465e2011-09-28 11:06:15 -0300114
115void qmp_system_powerdown(Error **erp)
116{
117 qemu_system_powerdown_request();
118}
Luiz Capitulino755f1962011-10-06 14:31:39 -0300119
Igor Mammedov69ca3ea2013-04-30 15:41:25 +0200120void qmp_cpu_add(int64_t id, Error **errp)
121{
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +0200122 MachineClass *mc;
123
124 mc = MACHINE_GET_CLASS(current_machine);
Marcel Apfelbaum958db902014-04-09 20:34:53 +0300125 if (mc->hot_add_cpu) {
126 mc->hot_add_cpu(id, errp);
Igor Mammedov69ca3ea2013-04-30 15:41:25 +0200127 } else {
128 error_setg(errp, "Not supported");
129 }
130}
131
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
Igor Mammedov047f7032018-05-11 19:24:43 +0200164void qmp_exit_preconfig(Error **errp)
165{
166 if (!runstate_check(RUN_STATE_PRECONFIG)) {
167 error_setg(errp, "The command is permitted only in '%s' state",
168 RunState_str(RUN_STATE_PRECONFIG));
169 return;
170 }
171 qemu_exit_preconfig_request();
172}
173
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200174void qmp_cont(Error **errp)
175{
Max Reitz373340b2015-10-19 17:53:22 +0200176 BlockBackend *blk;
Daniel P. Berrange788cf9f2017-06-23 17:24:15 +0100177 Error *local_err = NULL;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200178
Peter Xu65d64f32016-02-18 13:16:49 +0800179 /* if there is a dump in background, we should wait until the dump
180 * finished */
181 if (dump_in_progress()) {
182 error_setg(errp, "There is a dump in process, please wait.");
183 return;
184 }
185
Hu Taoede085b2013-04-26 11:24:40 +0800186 if (runstate_needs_reset()) {
Cole Robinsonf231b882014-03-21 19:42:26 -0400187 error_setg(errp, "Resetting the Virtual Machine is required");
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200188 return;
Luiz Capitulinoad02b962012-04-27 13:33:36 -0300189 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
190 return;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200191 }
192
Max Reitz373340b2015-10-19 17:53:22 +0200193 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
194 blk_iostatus_reset(blk);
Markus Armbrusterab319792014-05-02 13:26:42 +0200195 }
Kevin Wolf7c8eece2016-03-22 18:58:50 +0100196
Kevin Wolf76b1c7f2015-12-22 14:07:08 +0100197 /* Continuing after completed migration. Images have been inactivated to
Kevin Wolface21a52017-05-04 18:52:36 +0200198 * allow the destination to take control. Need to get control back now.
199 *
200 * If there are no inactive block nodes (e.g. because the VM was just
201 * paused rather than completing a migration), bdrv_inactivate_all() simply
202 * doesn't do anything. */
203 bdrv_invalidate_cache_all(&local_err);
204 if (local_err) {
205 error_propagate(errp, local_err);
206 return;
Kevin Wolf76b1c7f2015-12-22 14:07:08 +0100207 }
208
Paolo Bonzini1e998142012-10-23 14:54:21 +0200209 if (runstate_check(RUN_STATE_INMIGRATE)) {
210 autostart = 1;
211 } else {
212 vm_start();
213 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200214}
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600215
Gerd Hoffmann9b9df252012-02-23 13:45:21 +0100216void qmp_system_wakeup(Error **errp)
217{
218 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
219}
220
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600221ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600222{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600223 Object *obj;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600224 bool ambiguous = false;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600225 ObjectPropertyInfoList *props = NULL;
226 ObjectProperty *prop;
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000227 ObjectPropertyIterator iter;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600228
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600229 obj = object_resolve_path(path, &ambiguous);
230 if (obj == NULL) {
Michael Tokarev79772082014-05-04 12:23:59 +0400231 if (ambiguous) {
232 error_setg(errp, "Path '%s' is ambiguous", path);
233 } else {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100234 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
235 "Device '%s' not found", path);
Michael Tokarev79772082014-05-04 12:23:59 +0400236 }
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600237 return NULL;
238 }
239
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000240 object_property_iter_init(&iter, obj);
241 while ((prop = object_property_iter_next(&iter))) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600242 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600243
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600244 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600245 entry->next = props;
246 props = entry;
247
248 entry->value->name = g_strdup(prop->name);
249 entry->value->type = g_strdup(prop->type);
250 }
251
252 return props;
253}
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600254
Markus Armbruster6eb39372015-09-16 13:06:25 +0200255void qmp_qom_set(const char *path, const char *property, QObject *value,
256 Error **errp)
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600257{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600258 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600259
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600260 obj = object_resolve_path(path, NULL);
261 if (!obj) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100262 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Markus Armbruster75158eb2015-03-16 08:57:47 +0100263 "Device '%s' not found", path);
Markus Armbruster485febc2015-03-13 17:25:50 +0100264 return;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600265 }
266
Markus Armbruster485febc2015-03-13 17:25:50 +0100267 object_property_set_qobject(obj, value, property, errp);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600268}
269
Markus Armbruster6eb39372015-09-16 13:06:25 +0200270QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600271{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600272 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600273
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600274 obj = object_resolve_path(path, NULL);
275 if (!obj) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100276 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Markus Armbruster75158eb2015-03-16 08:57:47 +0100277 "Device '%s' not found", path);
Markus Armbruster6eb39372015-09-16 13:06:25 +0200278 return NULL;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600279 }
280
Markus Armbruster6eb39372015-09-16 13:06:25 +0200281 return object_property_get_qobject(obj, property, errp);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600282}
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200283
284void qmp_set_password(const char *protocol, const char *password,
285 bool has_connected, const char *connected, Error **errp)
286{
287 int disconnect_if_connected = 0;
288 int fail_if_connected = 0;
289 int rc;
290
291 if (has_connected) {
292 if (strcmp(connected, "fail") == 0) {
293 fail_if_connected = 1;
294 } else if (strcmp(connected, "disconnect") == 0) {
295 disconnect_if_connected = 1;
296 } else if (strcmp(connected, "keep") == 0) {
297 /* nothing */
298 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100299 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200300 return;
301 }
302 }
303
304 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100305 if (!qemu_using_spice(errp)) {
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200306 return;
307 }
308 rc = qemu_spice_set_passwd(password, fail_if_connected,
309 disconnect_if_connected);
310 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100311 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200312 }
313 return;
314 }
315
316 if (strcmp(protocol, "vnc") == 0) {
317 if (fail_if_connected || disconnect_if_connected) {
318 /* vnc supports "connected=keep" only */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100319 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200320 return;
321 }
322 /* Note that setting an empty password will not disable login through
323 * this interface. */
324 rc = vnc_display_password(NULL, password);
325 if (rc < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100326 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200327 }
328 return;
329 }
330
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100331 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200332}
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200333
334void qmp_expire_password(const char *protocol, const char *whenstr,
335 Error **errp)
336{
337 time_t when;
338 int rc;
339
340 if (strcmp(whenstr, "now") == 0) {
341 when = 0;
342 } else if (strcmp(whenstr, "never") == 0) {
343 when = TIME_MAX;
344 } else if (whenstr[0] == '+') {
345 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
346 } else {
347 when = strtoull(whenstr, NULL, 10);
348 }
349
350 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100351 if (!qemu_using_spice(errp)) {
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200352 return;
353 }
354 rc = qemu_spice_set_pw_expire(when);
355 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100356 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200357 }
358 return;
359 }
360
361 if (strcmp(protocol, "vnc") == 0) {
362 rc = vnc_display_pw_expire(NULL, when);
363 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100364 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200365 }
366 return;
367 }
368
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100369 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200370}
Luiz Capitulino270b2432011-12-08 11:45:55 -0200371
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200372#ifdef CONFIG_VNC
Luiz Capitulino270b2432011-12-08 11:45:55 -0200373void qmp_change_vnc_password(const char *password, Error **errp)
374{
375 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100376 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino270b2432011-12-08 11:45:55 -0200377 }
378}
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200379
Paolo Bonzini007fcd3e2012-10-18 09:01:01 +0200380static void qmp_change_vnc_listen(const char *target, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200381{
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200382 QemuOptsList *olist = qemu_find_opts("vnc");
383 QemuOpts *opts;
384
385 if (strstr(target, "id=")) {
386 error_setg(errp, "id not supported");
387 return;
388 }
389
390 opts = qemu_opts_find(olist, "default");
391 if (opts) {
392 qemu_opts_del(opts);
393 }
Markus Armbruster70b94332015-02-13 12:50:26 +0100394 opts = vnc_parse(target, errp);
Gongleif7801c52015-02-05 17:43:35 +0800395 if (!opts) {
396 return;
397 }
398
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200399 vnc_display_open("default", errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200400}
401
402static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
403 Error **errp)
404{
405 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
406 if (!has_arg) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100407 error_setg(errp, QERR_MISSING_PARAMETER, "password");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200408 } else {
409 qmp_change_vnc_password(arg, errp);
410 }
411 } else {
412 qmp_change_vnc_listen(target, errp);
413 }
414}
415#else
416void qmp_change_vnc_password(const char *password, Error **errp)
417{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100418 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200419}
420static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
421 Error **errp)
422{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100423 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200424}
425#endif /* !CONFIG_VNC */
426
427void qmp_change(const char *device, const char *target,
Markus Armbruster7daecb32014-05-02 13:26:31 +0200428 bool has_arg, const char *arg, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200429{
430 if (strcmp(device, "vnc") == 0) {
Markus Armbruster7daecb32014-05-02 13:26:31 +0200431 qmp_change_vnc(target, has_arg, arg, errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200432 } else {
Kevin Wolf70e2cb32016-09-20 13:38:47 +0200433 qmp_blockdev_change_medium(true, device, false, NULL, target,
434 has_arg, arg, false, 0, errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200435 }
436}
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600437
438static void qom_list_types_tramp(ObjectClass *klass, void *data)
439{
440 ObjectTypeInfoList *e, **pret = data;
441 ObjectTypeInfo *info;
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300442 ObjectClass *parent = object_class_get_parent(klass);
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600443
444 info = g_malloc0(sizeof(*info));
445 info->name = g_strdup(object_class_get_name(klass));
Eduardo Habkost87467ea2017-07-07 09:22:14 -0300446 info->has_abstract = info->abstract = object_class_is_abstract(klass);
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300447 if (parent) {
448 info->has_parent = true;
449 info->parent = g_strdup(object_class_get_name(parent));
450 }
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600451
452 e = g_malloc0(sizeof(*e));
453 e->value = info;
454 e->next = *pret;
455 *pret = e;
456}
457
458ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
459 const char *implements,
460 bool has_abstract,
461 bool abstract,
462 Error **errp)
463{
464 ObjectTypeInfoList *ret = NULL;
465
466 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
467
468 return ret;
469}
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500470
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200471/* Return a DevicePropertyInfo for a qdev property.
472 *
473 * If a qdev property with the given name does not exist, use the given default
474 * type. If the qdev property info should not be shown, return NULL.
475 *
476 * The caller must free the return value.
477 */
Alexey Kardashevskiy35f63762018-03-02 00:09:38 +1100478static ObjectPropertyInfo *make_device_property_info(ObjectClass *klass,
479 const char *name,
480 const char *default_type,
481 const char *description)
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200482{
Alexey Kardashevskiy35f63762018-03-02 00:09:38 +1100483 ObjectPropertyInfo *info;
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200484 Property *prop;
485
486 do {
487 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
488 if (strcmp(name, prop->name) != 0) {
489 continue;
490 }
491
492 /*
493 * TODO Properties without a parser are just for dirty hacks.
494 * qdev_prop_ptr is the only such PropertyInfo. It's marked
495 * for removal. This conditional should be removed along with
496 * it.
497 */
Fam Zhengfaabdbb2017-07-14 10:14:51 +0800498 if (!prop->info->set && !prop->info->create) {
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200499 return NULL; /* no way to set it, don't show */
500 }
501
502 info = g_malloc0(sizeof(*info));
503 info->name = g_strdup(prop->name);
Fam Zheng75ab9052017-07-14 10:14:53 +0800504 info->type = default_type ? g_strdup(default_type)
505 : g_strdup(prop->info->name);
Gonglei07d09c52014-10-07 14:33:23 +0800506 info->has_description = !!prop->info->description;
507 info->description = g_strdup(prop->info->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200508 return info;
509 }
510 klass = object_class_get_parent(klass);
511 } while (klass != object_class_by_name(TYPE_DEVICE));
512
513 /* Not a qdev property, use the default type */
514 info = g_malloc0(sizeof(*info));
515 info->name = g_strdup(name);
516 info->type = g_strdup(default_type);
Gonglei07d09c52014-10-07 14:33:23 +0800517 info->has_description = !!description;
518 info->description = g_strdup(description);
519
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200520 return info;
521}
522
Alexey Kardashevskiy35f63762018-03-02 00:09:38 +1100523ObjectPropertyInfoList *qmp_device_list_properties(const char *typename,
524 Error **errp)
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500525{
526 ObjectClass *klass;
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200527 Object *obj;
528 ObjectProperty *prop;
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000529 ObjectPropertyIterator iter;
Alexey Kardashevskiy35f63762018-03-02 00:09:38 +1100530 ObjectPropertyInfoList *prop_list = NULL;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500531
532 klass = object_class_by_name(typename);
533 if (klass == NULL) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100534 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
535 "Device '%s' not found", typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500536 return NULL;
537 }
538
539 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
540 if (klass == NULL) {
Lin Ma7a8c1532017-01-25 13:27:03 +0800541 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename", TYPE_DEVICE);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500542 return NULL;
543 }
544
Markus Armbrusteredb15232015-10-01 10:59:57 +0200545 if (object_class_is_abstract(klass)) {
Lin Ma7a8c1532017-01-25 13:27:03 +0800546 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename",
Markus Armbrusteredb15232015-10-01 10:59:57 +0200547 "non-abstract device type");
548 return NULL;
549 }
550
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200551 obj = object_new(typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500552
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000553 object_property_iter_init(&iter, obj);
554 while ((prop = object_property_iter_next(&iter))) {
Alexey Kardashevskiy35f63762018-03-02 00:09:38 +1100555 ObjectPropertyInfo *info;
556 ObjectPropertyInfoList *entry;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500557
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200558 /* Skip Object and DeviceState properties */
559 if (strcmp(prop->name, "type") == 0 ||
560 strcmp(prop->name, "realized") == 0 ||
561 strcmp(prop->name, "hotpluggable") == 0 ||
Stefan Hajnoczi4115dd62014-07-09 14:01:31 +0200562 strcmp(prop->name, "hotplugged") == 0 ||
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200563 strcmp(prop->name, "parent_bus") == 0) {
564 continue;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500565 }
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200566
567 /* Skip legacy properties since they are just string versions of
568 * properties that we already list.
569 */
570 if (strstart(prop->name, "legacy-", NULL)) {
571 continue;
572 }
573
Gonglei07d09c52014-10-07 14:33:23 +0800574 info = make_device_property_info(klass, prop->name, prop->type,
575 prop->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200576 if (!info) {
577 continue;
578 }
579
580 entry = g_malloc0(sizeof(*entry));
581 entry->value = info;
582 entry->next = prop_list;
583 prop_list = entry;
584 }
585
586 object_unref(obj);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500587
588 return prop_list;
589}
Anthony Liguorie4e31c62012-08-10 11:04:13 -0500590
Alexey Kardashevskiy961c47b2018-03-02 00:09:39 +1100591ObjectPropertyInfoList *qmp_qom_list_properties(const char *typename,
592 Error **errp)
593{
594 ObjectClass *klass;
595 Object *obj = NULL;
596 ObjectProperty *prop;
597 ObjectPropertyIterator iter;
598 ObjectPropertyInfoList *prop_list = NULL;
599
600 klass = object_class_by_name(typename);
601 if (klass == NULL) {
602 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
603 "Class '%s' not found", typename);
604 return NULL;
605 }
606
607 klass = object_class_dynamic_cast(klass, TYPE_OBJECT);
608 if (klass == NULL) {
609 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename", TYPE_OBJECT);
610 return NULL;
611 }
612
613 if (object_class_is_abstract(klass)) {
614 object_class_property_iter_init(&iter, klass);
615 } else {
616 obj = object_new(typename);
617 object_property_iter_init(&iter, obj);
618 }
619 while ((prop = object_property_iter_next(&iter))) {
620 ObjectPropertyInfo *info;
621 ObjectPropertyInfoList *entry;
622
623 info = g_malloc0(sizeof(*info));
624 info->name = g_strdup(prop->name);
625 info->type = g_strdup(prop->type);
626 info->has_description = !!prop->description;
627 info->description = g_strdup(prop->description);
628
629 entry = g_malloc0(sizeof(*entry));
630 entry->value = info;
631 entry->next = prop_list;
632 prop_list = entry;
633 }
634
635 object_unref(obj);
636
637 return prop_list;
638}
639
Anthony Liguori76b64a72012-08-14 22:17:36 -0500640CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
641{
642 return arch_query_cpu_definitions(errp);
643}
644
David Hildenbrande09484e2016-09-05 10:52:39 +0200645CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
646 CpuModelInfo *model,
647 Error **errp)
648{
649 return arch_query_cpu_model_expansion(type, model, errp);
650}
651
David Hildenbrand0031e0d2016-09-05 10:52:40 +0200652CpuModelCompareInfo *qmp_query_cpu_model_comparison(CpuModelInfo *modela,
653 CpuModelInfo *modelb,
654 Error **errp)
655{
656 return arch_query_cpu_model_comparison(modela, modelb, errp);
657}
658
David Hildenbrandb18b6042016-09-05 10:52:41 +0200659CpuModelBaselineInfo *qmp_query_cpu_model_baseline(CpuModelInfo *modela,
660 CpuModelInfo *modelb,
661 Error **errp)
662{
663 return arch_query_cpu_model_baseline(modela, modelb, errp);
664}
665
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300666void qmp_add_client(const char *protocol, const char *fdname,
667 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
668 Error **errp)
669{
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300670 Chardev *s;
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300671 int fd;
672
673 fd = monitor_get_fd(cur_mon, fdname, errp);
674 if (fd < 0) {
675 return;
676 }
677
678 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100679 if (!qemu_using_spice(errp)) {
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300680 close(fd);
681 return;
682 }
683 skipauth = has_skipauth ? skipauth : false;
684 tls = has_tls ? tls : false;
685 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
686 error_setg(errp, "spice failed to add client");
687 close(fd);
688 }
689 return;
690#ifdef CONFIG_VNC
691 } else if (strcmp(protocol, "vnc") == 0) {
692 skipauth = has_skipauth ? skipauth : false;
693 vnc_display_add_client(NULL, fd, skipauth);
694 return;
695#endif
696 } else if ((s = qemu_chr_find(protocol)) != NULL) {
697 if (qemu_chr_add_client(s, fd) < 0) {
698 error_setg(errp, "failed to add client");
699 close(fd);
700 return;
701 }
702 return;
703 }
704
705 error_setg(errp, "protocol '%s' is invalid", protocol);
706 close(fd);
707}
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100708
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100709
Markus Armbruster6eb39372015-09-16 13:06:25 +0200710void qmp_object_add(const char *type, const char *id,
711 bool has_props, QObject *props, Error **errp)
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100712{
Marc-André Lureaue64c75a2016-09-23 00:39:25 +0400713 QDict *pdict;
Eric Blakeb70ce1012016-06-09 10:48:38 -0600714 Visitor *v;
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000715 Object *obj;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100716
717 if (props) {
Max Reitz7dc847e2018-02-24 16:40:29 +0100718 pdict = qobject_to(QDict, props);
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100719 if (!pdict) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100720 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
721 return;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100722 }
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200723 qobject_ref(pdict);
Marc-André Lureaue64c75a2016-09-23 00:39:25 +0400724 } else {
725 pdict = qdict_new();
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100726 }
727
Markus Armbruster048abb72017-03-03 13:32:39 +0100728 v = qobject_input_visitor_new(QOBJECT(pdict));
Eric Blakeb70ce1012016-06-09 10:48:38 -0600729 obj = user_creatable_add_type(type, id, pdict, v, errp);
730 visit_free(v);
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000731 if (obj) {
732 object_unref(obj);
733 }
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200734 qobject_unref(pdict);
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100735}
736
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100737void qmp_object_del(const char *id, Error **errp)
738{
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000739 user_creatable_del(id, errp);
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100740}
Igor Mammedov6f2e2732014-06-16 19:12:25 +0200741
742MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
743{
David Hildenbrand2cc0e2e2018-04-23 18:51:16 +0200744 return qmp_memory_device_list();
Igor Mammedov6f2e2732014-06-16 19:12:25 +0200745}
Igor Mammedov02419bc2014-06-16 19:12:28 +0200746
747ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
748{
749 bool ambig;
750 ACPIOSTInfoList *head = NULL;
751 ACPIOSTInfoList **prev = &head;
752 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
753
754 if (obj) {
755 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
756 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
757
758 adevc->ospm_status(adev, &prev);
759 } else {
760 error_setg(errp, "command is not supported, missing ACPI device");
761 }
762
763 return head;
764}
Vadim Galitsyn9aa33972017-08-29 17:30:21 +0200765
766MemoryInfo *qmp_query_memory_size_summary(Error **errp)
767{
768 MemoryInfo *mem_info = g_malloc0(sizeof(MemoryInfo));
769
770 mem_info->base_memory = ram_size;
771
772 mem_info->plugged_memory = get_plugged_memory_size();
773 mem_info->has_plugged_memory =
774 mem_info->plugged_memory != (uint64_t)-1;
775
776 return mem_info;
777}
Peter Xu469638f2018-03-09 17:00:04 +0800778
779static QemuSemaphore x_oob_test_sem;
780
781static void __attribute__((constructor)) x_oob_test_init(void)
782{
783 qemu_sem_init(&x_oob_test_sem, 0);
784}
785
786void qmp_x_oob_test(bool lock, Error **errp)
787{
788 if (lock) {
789 qemu_sem_wait(&x_oob_test_sem);
790 } else {
791 qemu_sem_post(&x_oob_test_sem);
792 }
793}