blob: 793f6f332302ee8aad0d45cc9ece5f9c4a2016cd [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"
Anthony Liguori48a32be2011-09-02 12:34:48 -050024#include "qmp-commands.h"
Marc-André Lureau8228e352017-01-26 17:19:46 +040025#include "chardev/char.h"
Luiz Capitulinofbf796f2011-12-07 11:17:51 -020026#include "ui/qemu-spice.h"
27#include "ui/vnc.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010028#include "sysemu/kvm.h"
29#include "sysemu/arch_init.h"
Anthony Liguorib4b12c62011-12-12 14:29:34 -060030#include "hw/qdev.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010031#include "sysemu/blockdev.h"
Max Reitz373340b2015-10-19 17:53:22 +020032#include "sysemu/block-backend.h"
Paolo Bonzini14cccb62012-12-17 18:19:50 +010033#include "qom/qom-qobject.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010034#include "qapi/error.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010035#include "qapi/qmp/qdict.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010036#include "qapi/qmp/qerror.h"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010037#include "qapi/qobject-input-visitor.h"
Igor Mammedov69ca3ea2013-04-30 15:41:25 +020038#include "hw/boards.h"
Igor Mammedov269e09f2014-01-16 17:34:38 +010039#include "qom/object_interfaces.h"
Igor Mammedov6f2e2732014-06-16 19:12:25 +020040#include "hw/mem/pc-dimm.h"
Igor Mammedov02419bc2014-06-16 19:12:28 +020041#include "hw/acpi/acpi_dev_interface.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050042
43NameInfo *qmp_query_name(Error **errp)
44{
45 NameInfo *info = g_malloc0(sizeof(*info));
46
47 if (qemu_name) {
48 info->has_name = true;
49 info->name = g_strdup(qemu_name);
50 }
51
52 return info;
53}
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030054
Markus Armbruster7daecb32014-05-02 13:26:31 +020055VersionInfo *qmp_query_version(Error **errp)
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030056{
Eric Blake4752cdb2015-05-04 09:05:31 -060057 VersionInfo *info = g_new0(VersionInfo, 1);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030058
Eric Blake4752cdb2015-05-04 09:05:31 -060059 info->qemu = g_new0(VersionTriple, 1);
Marc-André Lureau3688d8c2016-09-12 13:18:56 +040060 info->qemu->major = QEMU_VERSION_MAJOR;
61 info->qemu->minor = QEMU_VERSION_MINOR;
62 info->qemu->micro = QEMU_VERSION_MICRO;
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030063 info->package = g_strdup(QEMU_PKGVERSION);
64
65 return info;
66}
Luiz Capitulino292a2602011-09-12 15:10:53 -030067
68KvmInfo *qmp_query_kvm(Error **errp)
69{
70 KvmInfo *info = g_malloc0(sizeof(*info));
71
72 info->enabled = kvm_enabled();
73 info->present = kvm_available();
74
75 return info;
76}
77
Luiz Capitulinoefab7672011-09-13 17:16:25 -030078UuidInfo *qmp_query_uuid(Error **errp)
79{
80 UuidInfo *info = g_malloc0(sizeof(*info));
Luiz Capitulinoefab7672011-09-13 17:16:25 -030081
Fam Zheng9c5ce8d2016-09-21 12:27:22 +080082 info->UUID = qemu_uuid_unparse_strdup(&qemu_uuid);
Luiz Capitulinoefab7672011-09-13 17:16:25 -030083 return info;
84}
85
Markus Armbruster7daecb32014-05-02 13:26:31 +020086void qmp_quit(Error **errp)
Luiz Capitulino7a7f3252011-09-15 14:20:28 -030087{
88 no_shutdown = 0;
Eric Blakecf83f142017-05-15 16:41:13 -050089 qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP);
Luiz Capitulino7a7f3252011-09-15 14:20:28 -030090}
91
Luiz Capitulino5f158f22011-09-15 14:34:39 -030092void qmp_stop(Error **errp)
93{
Peter Xu65d64f32016-02-18 13:16:49 +080094 /* if there is a dump in background, we should wait until the dump
95 * finished */
96 if (dump_in_progress()) {
97 error_setg(errp, "There is a dump in process, please wait.");
98 return;
99 }
100
Paolo Bonzini1e998142012-10-23 14:54:21 +0200101 if (runstate_check(RUN_STATE_INMIGRATE)) {
102 autostart = 0;
103 } else {
104 vm_stop(RUN_STATE_PAUSED);
105 }
Luiz Capitulino5f158f22011-09-15 14:34:39 -0300106}
107
Luiz Capitulino38d22652011-09-15 14:41:46 -0300108void qmp_system_reset(Error **errp)
109{
Eric Blakecf83f142017-05-15 16:41:13 -0500110 qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP);
Luiz Capitulino38d22652011-09-15 14:41:46 -0300111}
Luiz Capitulino5bc465e2011-09-28 11:06:15 -0300112
113void qmp_system_powerdown(Error **erp)
114{
115 qemu_system_powerdown_request();
116}
Luiz Capitulino755f1962011-10-06 14:31:39 -0300117
Igor Mammedov69ca3ea2013-04-30 15:41:25 +0200118void qmp_cpu_add(int64_t id, Error **errp)
119{
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +0200120 MachineClass *mc;
121
122 mc = MACHINE_GET_CLASS(current_machine);
Marcel Apfelbaum958db902014-04-09 20:34:53 +0300123 if (mc->hot_add_cpu) {
124 mc->hot_add_cpu(id, errp);
Igor Mammedov69ca3ea2013-04-30 15:41:25 +0200125 } else {
126 error_setg(errp, "Not supported");
127 }
128}
129
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200130#ifndef CONFIG_VNC
131/* If VNC support is enabled, the "true" query-vnc command is
132 defined in the VNC subsystem */
133VncInfo *qmp_query_vnc(Error **errp)
134{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100135 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200136 return NULL;
137};
Leon Yu89db21772015-02-02 05:08:51 +0000138
139VncInfo2List *qmp_query_vnc_servers(Error **errp)
140{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100141 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Leon Yu89db21772015-02-02 05:08:51 +0000142 return NULL;
143};
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200144#endif
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200145
146#ifndef CONFIG_SPICE
Markus Armbrusterad0ec142015-01-13 15:56:11 +0100147/*
148 * qmp-commands.hx ensures that QMP command query-spice exists only
149 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
150 * result. However, the QAPI schema is blissfully unaware of that,
151 * and the QAPI code generator happily generates a dead
Markus Armbruster7fad30f2015-09-16 13:06:19 +0200152 * qmp_marshal_query_spice() that calls qmp_query_spice(). Provide it
153 * one, or else linking fails. FIXME Educate the QAPI schema on
154 * CONFIG_SPICE.
Markus Armbrusterad0ec142015-01-13 15:56:11 +0100155 */
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200156SpiceInfo *qmp_query_spice(Error **errp)
157{
Markus Armbrusterad0ec142015-01-13 15:56:11 +0100158 abort();
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200159};
160#endif
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200161
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200162void qmp_cont(Error **errp)
163{
Max Reitz373340b2015-10-19 17:53:22 +0200164 BlockBackend *blk;
Daniel P. Berrange788cf9f2017-06-23 17:24:15 +0100165 Error *local_err = NULL;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200166
Peter Xu65d64f32016-02-18 13:16:49 +0800167 /* if there is a dump in background, we should wait until the dump
168 * finished */
169 if (dump_in_progress()) {
170 error_setg(errp, "There is a dump in process, please wait.");
171 return;
172 }
173
Hu Taoede085b2013-04-26 11:24:40 +0800174 if (runstate_needs_reset()) {
Cole Robinsonf231b882014-03-21 19:42:26 -0400175 error_setg(errp, "Resetting the Virtual Machine is required");
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200176 return;
Luiz Capitulinoad02b962012-04-27 13:33:36 -0300177 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
178 return;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200179 }
180
Max Reitz373340b2015-10-19 17:53:22 +0200181 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
182 blk_iostatus_reset(blk);
Markus Armbrusterab319792014-05-02 13:26:42 +0200183 }
Kevin Wolf7c8eece2016-03-22 18:58:50 +0100184
Kevin Wolf76b1c7f2015-12-22 14:07:08 +0100185 /* Continuing after completed migration. Images have been inactivated to
Kevin Wolface21a52017-05-04 18:52:36 +0200186 * allow the destination to take control. Need to get control back now.
187 *
188 * If there are no inactive block nodes (e.g. because the VM was just
189 * paused rather than completing a migration), bdrv_inactivate_all() simply
190 * doesn't do anything. */
191 bdrv_invalidate_cache_all(&local_err);
192 if (local_err) {
193 error_propagate(errp, local_err);
194 return;
Kevin Wolf76b1c7f2015-12-22 14:07:08 +0100195 }
196
Paolo Bonzini1e998142012-10-23 14:54:21 +0200197 if (runstate_check(RUN_STATE_INMIGRATE)) {
198 autostart = 1;
199 } else {
200 vm_start();
201 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200202}
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600203
Gerd Hoffmann9b9df252012-02-23 13:45:21 +0100204void qmp_system_wakeup(Error **errp)
205{
206 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
207}
208
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600209ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600210{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600211 Object *obj;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600212 bool ambiguous = false;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600213 ObjectPropertyInfoList *props = NULL;
214 ObjectProperty *prop;
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000215 ObjectPropertyIterator iter;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600216
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600217 obj = object_resolve_path(path, &ambiguous);
218 if (obj == NULL) {
Michael Tokarev79772082014-05-04 12:23:59 +0400219 if (ambiguous) {
220 error_setg(errp, "Path '%s' is ambiguous", path);
221 } else {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100222 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
223 "Device '%s' not found", path);
Michael Tokarev79772082014-05-04 12:23:59 +0400224 }
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600225 return NULL;
226 }
227
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000228 object_property_iter_init(&iter, obj);
229 while ((prop = object_property_iter_next(&iter))) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600230 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600231
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600232 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600233 entry->next = props;
234 props = entry;
235
236 entry->value->name = g_strdup(prop->name);
237 entry->value->type = g_strdup(prop->type);
238 }
239
240 return props;
241}
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600242
Markus Armbruster6eb39372015-09-16 13:06:25 +0200243void qmp_qom_set(const char *path, const char *property, QObject *value,
244 Error **errp)
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600245{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600246 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600247
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600248 obj = object_resolve_path(path, NULL);
249 if (!obj) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100250 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Markus Armbruster75158eb2015-03-16 08:57:47 +0100251 "Device '%s' not found", path);
Markus Armbruster485febc2015-03-13 17:25:50 +0100252 return;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600253 }
254
Markus Armbruster485febc2015-03-13 17:25:50 +0100255 object_property_set_qobject(obj, value, property, errp);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600256}
257
Markus Armbruster6eb39372015-09-16 13:06:25 +0200258QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600259{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600260 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600261
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600262 obj = object_resolve_path(path, NULL);
263 if (!obj) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100264 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Markus Armbruster75158eb2015-03-16 08:57:47 +0100265 "Device '%s' not found", path);
Markus Armbruster6eb39372015-09-16 13:06:25 +0200266 return NULL;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600267 }
268
Markus Armbruster6eb39372015-09-16 13:06:25 +0200269 return object_property_get_qobject(obj, property, errp);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600270}
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200271
272void qmp_set_password(const char *protocol, const char *password,
273 bool has_connected, const char *connected, Error **errp)
274{
275 int disconnect_if_connected = 0;
276 int fail_if_connected = 0;
277 int rc;
278
279 if (has_connected) {
280 if (strcmp(connected, "fail") == 0) {
281 fail_if_connected = 1;
282 } else if (strcmp(connected, "disconnect") == 0) {
283 disconnect_if_connected = 1;
284 } else if (strcmp(connected, "keep") == 0) {
285 /* nothing */
286 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100287 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200288 return;
289 }
290 }
291
292 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100293 if (!qemu_using_spice(errp)) {
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200294 return;
295 }
296 rc = qemu_spice_set_passwd(password, fail_if_connected,
297 disconnect_if_connected);
298 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100299 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200300 }
301 return;
302 }
303
304 if (strcmp(protocol, "vnc") == 0) {
305 if (fail_if_connected || disconnect_if_connected) {
306 /* vnc supports "connected=keep" only */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100307 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200308 return;
309 }
310 /* Note that setting an empty password will not disable login through
311 * this interface. */
312 rc = vnc_display_password(NULL, password);
313 if (rc < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100314 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200315 }
316 return;
317 }
318
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100319 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200320}
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200321
322void qmp_expire_password(const char *protocol, const char *whenstr,
323 Error **errp)
324{
325 time_t when;
326 int rc;
327
328 if (strcmp(whenstr, "now") == 0) {
329 when = 0;
330 } else if (strcmp(whenstr, "never") == 0) {
331 when = TIME_MAX;
332 } else if (whenstr[0] == '+') {
333 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
334 } else {
335 when = strtoull(whenstr, NULL, 10);
336 }
337
338 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100339 if (!qemu_using_spice(errp)) {
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200340 return;
341 }
342 rc = qemu_spice_set_pw_expire(when);
343 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100344 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200345 }
346 return;
347 }
348
349 if (strcmp(protocol, "vnc") == 0) {
350 rc = vnc_display_pw_expire(NULL, when);
351 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100352 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200353 }
354 return;
355 }
356
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100357 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200358}
Luiz Capitulino270b2432011-12-08 11:45:55 -0200359
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200360#ifdef CONFIG_VNC
Luiz Capitulino270b2432011-12-08 11:45:55 -0200361void qmp_change_vnc_password(const char *password, Error **errp)
362{
363 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100364 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino270b2432011-12-08 11:45:55 -0200365 }
366}
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200367
Paolo Bonzini007fcd3e2012-10-18 09:01:01 +0200368static void qmp_change_vnc_listen(const char *target, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200369{
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200370 QemuOptsList *olist = qemu_find_opts("vnc");
371 QemuOpts *opts;
372
373 if (strstr(target, "id=")) {
374 error_setg(errp, "id not supported");
375 return;
376 }
377
378 opts = qemu_opts_find(olist, "default");
379 if (opts) {
380 qemu_opts_del(opts);
381 }
Markus Armbruster70b94332015-02-13 12:50:26 +0100382 opts = vnc_parse(target, errp);
Gongleif7801c52015-02-05 17:43:35 +0800383 if (!opts) {
384 return;
385 }
386
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200387 vnc_display_open("default", errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200388}
389
390static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
391 Error **errp)
392{
393 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
394 if (!has_arg) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100395 error_setg(errp, QERR_MISSING_PARAMETER, "password");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200396 } else {
397 qmp_change_vnc_password(arg, errp);
398 }
399 } else {
400 qmp_change_vnc_listen(target, errp);
401 }
402}
403#else
404void qmp_change_vnc_password(const char *password, Error **errp)
405{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100406 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200407}
408static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
409 Error **errp)
410{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100411 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200412}
413#endif /* !CONFIG_VNC */
414
415void qmp_change(const char *device, const char *target,
Markus Armbruster7daecb32014-05-02 13:26:31 +0200416 bool has_arg, const char *arg, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200417{
418 if (strcmp(device, "vnc") == 0) {
Markus Armbruster7daecb32014-05-02 13:26:31 +0200419 qmp_change_vnc(target, has_arg, arg, errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200420 } else {
Kevin Wolf70e2cb32016-09-20 13:38:47 +0200421 qmp_blockdev_change_medium(true, device, false, NULL, target,
422 has_arg, arg, false, 0, errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200423 }
424}
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600425
426static void qom_list_types_tramp(ObjectClass *klass, void *data)
427{
428 ObjectTypeInfoList *e, **pret = data;
429 ObjectTypeInfo *info;
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300430 ObjectClass *parent = object_class_get_parent(klass);
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600431
432 info = g_malloc0(sizeof(*info));
433 info->name = g_strdup(object_class_get_name(klass));
Eduardo Habkost87467ea2017-07-07 09:22:14 -0300434 info->has_abstract = info->abstract = object_class_is_abstract(klass);
Eduardo Habkostf86285c2017-07-07 09:22:15 -0300435 if (parent) {
436 info->has_parent = true;
437 info->parent = g_strdup(object_class_get_name(parent));
438 }
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600439
440 e = g_malloc0(sizeof(*e));
441 e->value = info;
442 e->next = *pret;
443 *pret = e;
444}
445
446ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
447 const char *implements,
448 bool has_abstract,
449 bool abstract,
450 Error **errp)
451{
452 ObjectTypeInfoList *ret = NULL;
453
454 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
455
456 return ret;
457}
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500458
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200459/* Return a DevicePropertyInfo for a qdev property.
460 *
461 * If a qdev property with the given name does not exist, use the given default
462 * type. If the qdev property info should not be shown, return NULL.
463 *
464 * The caller must free the return value.
465 */
466static DevicePropertyInfo *make_device_property_info(ObjectClass *klass,
467 const char *name,
Gonglei07d09c52014-10-07 14:33:23 +0800468 const char *default_type,
469 const char *description)
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200470{
471 DevicePropertyInfo *info;
472 Property *prop;
473
474 do {
475 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
476 if (strcmp(name, prop->name) != 0) {
477 continue;
478 }
479
480 /*
481 * TODO Properties without a parser are just for dirty hacks.
482 * qdev_prop_ptr is the only such PropertyInfo. It's marked
483 * for removal. This conditional should be removed along with
484 * it.
485 */
Fam Zhengfaabdbb2017-07-14 10:14:51 +0800486 if (!prop->info->set && !prop->info->create) {
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200487 return NULL; /* no way to set it, don't show */
488 }
489
490 info = g_malloc0(sizeof(*info));
491 info->name = g_strdup(prop->name);
Fam Zheng75ab9052017-07-14 10:14:53 +0800492 info->type = default_type ? g_strdup(default_type)
493 : g_strdup(prop->info->name);
Gonglei07d09c52014-10-07 14:33:23 +0800494 info->has_description = !!prop->info->description;
495 info->description = g_strdup(prop->info->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200496 return info;
497 }
498 klass = object_class_get_parent(klass);
499 } while (klass != object_class_by_name(TYPE_DEVICE));
500
501 /* Not a qdev property, use the default type */
502 info = g_malloc0(sizeof(*info));
503 info->name = g_strdup(name);
504 info->type = g_strdup(default_type);
Gonglei07d09c52014-10-07 14:33:23 +0800505 info->has_description = !!description;
506 info->description = g_strdup(description);
507
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200508 return info;
509}
510
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500511DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
512 Error **errp)
513{
514 ObjectClass *klass;
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200515 Object *obj;
516 ObjectProperty *prop;
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000517 ObjectPropertyIterator iter;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500518 DevicePropertyInfoList *prop_list = NULL;
519
520 klass = object_class_by_name(typename);
521 if (klass == NULL) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100522 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
523 "Device '%s' not found", typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500524 return NULL;
525 }
526
527 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
528 if (klass == NULL) {
Lin Ma7a8c1532017-01-25 13:27:03 +0800529 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename", TYPE_DEVICE);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500530 return NULL;
531 }
532
Markus Armbrusteredb15232015-10-01 10:59:57 +0200533 if (object_class_is_abstract(klass)) {
Lin Ma7a8c1532017-01-25 13:27:03 +0800534 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "typename",
Markus Armbrusteredb15232015-10-01 10:59:57 +0200535 "non-abstract device type");
536 return NULL;
537 }
538
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200539 obj = object_new(typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500540
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000541 object_property_iter_init(&iter, obj);
542 while ((prop = object_property_iter_next(&iter))) {
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200543 DevicePropertyInfo *info;
544 DevicePropertyInfoList *entry;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500545
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200546 /* Skip Object and DeviceState properties */
547 if (strcmp(prop->name, "type") == 0 ||
548 strcmp(prop->name, "realized") == 0 ||
549 strcmp(prop->name, "hotpluggable") == 0 ||
Stefan Hajnoczi4115dd62014-07-09 14:01:31 +0200550 strcmp(prop->name, "hotplugged") == 0 ||
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200551 strcmp(prop->name, "parent_bus") == 0) {
552 continue;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500553 }
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200554
555 /* Skip legacy properties since they are just string versions of
556 * properties that we already list.
557 */
558 if (strstart(prop->name, "legacy-", NULL)) {
559 continue;
560 }
561
Gonglei07d09c52014-10-07 14:33:23 +0800562 info = make_device_property_info(klass, prop->name, prop->type,
563 prop->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200564 if (!info) {
565 continue;
566 }
567
568 entry = g_malloc0(sizeof(*entry));
569 entry->value = info;
570 entry->next = prop_list;
571 prop_list = entry;
572 }
573
574 object_unref(obj);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500575
576 return prop_list;
577}
Anthony Liguorie4e31c62012-08-10 11:04:13 -0500578
Anthony Liguori76b64a72012-08-14 22:17:36 -0500579CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
580{
581 return arch_query_cpu_definitions(errp);
582}
583
David Hildenbrande09484e2016-09-05 10:52:39 +0200584CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
585 CpuModelInfo *model,
586 Error **errp)
587{
588 return arch_query_cpu_model_expansion(type, model, errp);
589}
590
David Hildenbrand0031e0d2016-09-05 10:52:40 +0200591CpuModelCompareInfo *qmp_query_cpu_model_comparison(CpuModelInfo *modela,
592 CpuModelInfo *modelb,
593 Error **errp)
594{
595 return arch_query_cpu_model_comparison(modela, modelb, errp);
596}
597
David Hildenbrandb18b6042016-09-05 10:52:41 +0200598CpuModelBaselineInfo *qmp_query_cpu_model_baseline(CpuModelInfo *modela,
599 CpuModelInfo *modelb,
600 Error **errp)
601{
602 return arch_query_cpu_model_baseline(modela, modelb, errp);
603}
604
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300605void qmp_add_client(const char *protocol, const char *fdname,
606 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
607 Error **errp)
608{
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300609 Chardev *s;
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300610 int fd;
611
612 fd = monitor_get_fd(cur_mon, fdname, errp);
613 if (fd < 0) {
614 return;
615 }
616
617 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100618 if (!qemu_using_spice(errp)) {
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300619 close(fd);
620 return;
621 }
622 skipauth = has_skipauth ? skipauth : false;
623 tls = has_tls ? tls : false;
624 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
625 error_setg(errp, "spice failed to add client");
626 close(fd);
627 }
628 return;
629#ifdef CONFIG_VNC
630 } else if (strcmp(protocol, "vnc") == 0) {
631 skipauth = has_skipauth ? skipauth : false;
632 vnc_display_add_client(NULL, fd, skipauth);
633 return;
634#endif
635 } else if ((s = qemu_chr_find(protocol)) != NULL) {
636 if (qemu_chr_add_client(s, fd) < 0) {
637 error_setg(errp, "failed to add client");
638 close(fd);
639 return;
640 }
641 return;
642 }
643
644 error_setg(errp, "protocol '%s' is invalid", protocol);
645 close(fd);
646}
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100647
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100648
Markus Armbruster6eb39372015-09-16 13:06:25 +0200649void qmp_object_add(const char *type, const char *id,
650 bool has_props, QObject *props, Error **errp)
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100651{
Marc-André Lureaue64c75a2016-09-23 00:39:25 +0400652 QDict *pdict;
Eric Blakeb70ce102016-06-09 10:48:38 -0600653 Visitor *v;
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000654 Object *obj;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100655
656 if (props) {
657 pdict = qobject_to_qdict(props);
658 if (!pdict) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100659 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
660 return;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100661 }
Marc-André Lureaue64c75a2016-09-23 00:39:25 +0400662 QINCREF(pdict);
663 } else {
664 pdict = qdict_new();
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100665 }
666
Markus Armbruster048abb72017-03-03 13:32:39 +0100667 v = qobject_input_visitor_new(QOBJECT(pdict));
Eric Blakeb70ce102016-06-09 10:48:38 -0600668 obj = user_creatable_add_type(type, id, pdict, v, errp);
669 visit_free(v);
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000670 if (obj) {
671 object_unref(obj);
672 }
Marc-André Lureaue64c75a2016-09-23 00:39:25 +0400673 QDECREF(pdict);
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100674}
675
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100676void qmp_object_del(const char *id, Error **errp)
677{
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000678 user_creatable_del(id, errp);
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100679}
Igor Mammedov6f2e2732014-06-16 19:12:25 +0200680
681MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
682{
683 MemoryDeviceInfoList *head = NULL;
684 MemoryDeviceInfoList **prev = &head;
685
686 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
687
688 return head;
689}
Igor Mammedov02419bc2014-06-16 19:12:28 +0200690
691ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
692{
693 bool ambig;
694 ACPIOSTInfoList *head = NULL;
695 ACPIOSTInfoList **prev = &head;
696 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
697
698 if (obj) {
699 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
700 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
701
702 adevc->ospm_status(adev, &prev);
703 } else {
704 error_setg(errp, "command is not supported, missing ACPI device");
705 }
706
707 return head;
708}
Vadim Galitsyn9aa33972017-08-29 17:30:21 +0200709
710MemoryInfo *qmp_query_memory_size_summary(Error **errp)
711{
712 MemoryInfo *mem_info = g_malloc0(sizeof(MemoryInfo));
713
714 mem_info->base_memory = ram_size;
715
716 mem_info->plugged_memory = get_plugged_memory_size();
717 mem_info->has_plugged_memory =
718 mem_info->plugged_memory != (uint64_t)-1;
719
720 return mem_info;
721}