blob: 8b4bc980f1657e1c0a8643a90ba1d3d729c7d062 [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 Armbrustera0b1a662015-03-17 18:16:21 +010019#include "monitor/monitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010020#include "sysemu/sysemu.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050021#include "qmp-commands.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020022#include "sysemu/char.h"
Luiz Capitulinofbf796f2011-12-07 11:17:51 -020023#include "ui/qemu-spice.h"
24#include "ui/vnc.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010025#include "sysemu/kvm.h"
26#include "sysemu/arch_init.h"
Anthony Liguorib4b12c62011-12-12 14:29:34 -060027#include "hw/qdev.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010028#include "sysemu/blockdev.h"
Max Reitz373340b2015-10-19 17:53:22 +020029#include "sysemu/block-backend.h"
Paolo Bonzini14cccb62012-12-17 18:19:50 +010030#include "qom/qom-qobject.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010031#include "qapi/qmp/qerror.h"
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +010032#include "qapi/qmp/qobject.h"
33#include "qapi/qmp-input-visitor.h"
Igor Mammedov69ca3ea2013-04-30 15:41:25 +020034#include "hw/boards.h"
Igor Mammedov269e09f2014-01-16 17:34:38 +010035#include "qom/object_interfaces.h"
Igor Mammedov6f2e2732014-06-16 19:12:25 +020036#include "hw/mem/pc-dimm.h"
Igor Mammedov02419bc2014-06-16 19:12:28 +020037#include "hw/acpi/acpi_dev_interface.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050038
39NameInfo *qmp_query_name(Error **errp)
40{
41 NameInfo *info = g_malloc0(sizeof(*info));
42
43 if (qemu_name) {
44 info->has_name = true;
45 info->name = g_strdup(qemu_name);
46 }
47
48 return info;
49}
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030050
Markus Armbruster7daecb32014-05-02 13:26:31 +020051VersionInfo *qmp_query_version(Error **errp)
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030052{
Eric Blake4752cdb2015-05-04 09:05:31 -060053 VersionInfo *info = g_new0(VersionInfo, 1);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030054
Eric Blake4752cdb2015-05-04 09:05:31 -060055 info->qemu = g_new0(VersionTriple, 1);
Marc-André Lureau3688d8c2016-09-12 13:18:56 +040056 info->qemu->major = QEMU_VERSION_MAJOR;
57 info->qemu->minor = QEMU_VERSION_MINOR;
58 info->qemu->micro = QEMU_VERSION_MICRO;
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030059 info->package = g_strdup(QEMU_PKGVERSION);
60
61 return info;
62}
Luiz Capitulino292a2602011-09-12 15:10:53 -030063
64KvmInfo *qmp_query_kvm(Error **errp)
65{
66 KvmInfo *info = g_malloc0(sizeof(*info));
67
68 info->enabled = kvm_enabled();
69 info->present = kvm_available();
70
71 return info;
72}
73
Luiz Capitulinoefab7672011-09-13 17:16:25 -030074UuidInfo *qmp_query_uuid(Error **errp)
75{
76 UuidInfo *info = g_malloc0(sizeof(*info));
77 char uuid[64];
78
79 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
80 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
81 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
82 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
83 qemu_uuid[14], qemu_uuid[15]);
84
85 info->UUID = g_strdup(uuid);
86 return info;
87}
88
Markus Armbruster7daecb32014-05-02 13:26:31 +020089void qmp_quit(Error **errp)
Luiz Capitulino7a7f3252011-09-15 14:20:28 -030090{
91 no_shutdown = 0;
92 qemu_system_shutdown_request();
93}
94
Luiz Capitulino5f158f22011-09-15 14:34:39 -030095void qmp_stop(Error **errp)
96{
Peter Xu65d64f32016-02-18 13:16:49 +080097 /* if there is a dump in background, we should wait until the dump
98 * finished */
99 if (dump_in_progress()) {
100 error_setg(errp, "There is a dump in process, please wait.");
101 return;
102 }
103
Paolo Bonzini1e998142012-10-23 14:54:21 +0200104 if (runstate_check(RUN_STATE_INMIGRATE)) {
105 autostart = 0;
106 } else {
107 vm_stop(RUN_STATE_PAUSED);
108 }
Luiz Capitulino5f158f22011-09-15 14:34:39 -0300109}
110
Luiz Capitulino38d22652011-09-15 14:41:46 -0300111void qmp_system_reset(Error **errp)
112{
113 qemu_system_reset_request();
114}
Luiz Capitulino5bc465e2011-09-28 11:06:15 -0300115
116void qmp_system_powerdown(Error **erp)
117{
118 qemu_system_powerdown_request();
119}
Luiz Capitulino755f1962011-10-06 14:31:39 -0300120
121void qmp_cpu(int64_t index, Error **errp)
122{
123 /* Just do nothing */
124}
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200125
Igor Mammedov69ca3ea2013-04-30 15:41:25 +0200126void qmp_cpu_add(int64_t id, Error **errp)
127{
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +0200128 MachineClass *mc;
129
130 mc = MACHINE_GET_CLASS(current_machine);
Marcel Apfelbaum958db902014-04-09 20:34:53 +0300131 if (mc->hot_add_cpu) {
132 mc->hot_add_cpu(id, errp);
Igor Mammedov69ca3ea2013-04-30 15:41:25 +0200133 } else {
134 error_setg(errp, "Not supported");
135 }
136}
137
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200138#ifndef CONFIG_VNC
139/* If VNC support is enabled, the "true" query-vnc command is
140 defined in the VNC subsystem */
141VncInfo *qmp_query_vnc(Error **errp)
142{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100143 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200144 return NULL;
145};
Leon Yu89db21772015-02-02 05:08:51 +0000146
147VncInfo2List *qmp_query_vnc_servers(Error **errp)
148{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100149 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Leon Yu89db21772015-02-02 05:08:51 +0000150 return NULL;
151};
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200152#endif
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200153
154#ifndef CONFIG_SPICE
Markus Armbrusterad0ec142015-01-13 15:56:11 +0100155/*
156 * qmp-commands.hx ensures that QMP command query-spice exists only
157 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
158 * result. However, the QAPI schema is blissfully unaware of that,
159 * and the QAPI code generator happily generates a dead
Markus Armbruster7fad30f2015-09-16 13:06:19 +0200160 * qmp_marshal_query_spice() that calls qmp_query_spice(). Provide it
161 * one, or else linking fails. FIXME Educate the QAPI schema on
162 * CONFIG_SPICE.
Markus Armbrusterad0ec142015-01-13 15:56:11 +0100163 */
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200164SpiceInfo *qmp_query_spice(Error **errp)
165{
Markus Armbrusterad0ec142015-01-13 15:56:11 +0100166 abort();
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200167};
168#endif
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200169
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200170void qmp_cont(Error **errp)
171{
Markus Armbruster4d2855a2015-01-29 10:37:00 +0100172 Error *local_err = NULL;
Max Reitz373340b2015-10-19 17:53:22 +0200173 BlockBackend *blk;
Markus Armbrusterab319792014-05-02 13:26:42 +0200174 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +0200175 BdrvNextIterator it;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200176
Peter Xu65d64f32016-02-18 13:16:49 +0800177 /* if there is a dump in background, we should wait until the dump
178 * finished */
179 if (dump_in_progress()) {
180 error_setg(errp, "There is a dump in process, please wait.");
181 return;
182 }
183
Hu Taoede085b2013-04-26 11:24:40 +0800184 if (runstate_needs_reset()) {
Cole Robinsonf231b882014-03-21 19:42:26 -0400185 error_setg(errp, "Resetting the Virtual Machine is required");
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200186 return;
Luiz Capitulinoad02b962012-04-27 13:33:36 -0300187 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
188 return;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200189 }
190
Max Reitz373340b2015-10-19 17:53:22 +0200191 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
192 blk_iostatus_reset(blk);
Markus Armbrusterab319792014-05-02 13:26:42 +0200193 }
Kevin Wolf7c8eece2016-03-22 18:58:50 +0100194
Kevin Wolf88be7b42016-05-20 18:49:07 +0200195 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Markus Armbruster4d2855a2015-01-29 10:37:00 +0100196 bdrv_add_key(bs, NULL, &local_err);
197 if (local_err) {
198 error_propagate(errp, local_err);
Markus Armbrusterab319792014-05-02 13:26:42 +0200199 return;
200 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200201 }
202
Kevin Wolf76b1c7f2015-12-22 14:07:08 +0100203 /* Continuing after completed migration. Images have been inactivated to
204 * allow the destination to take control. Need to get control back now. */
205 if (runstate_check(RUN_STATE_FINISH_MIGRATE) ||
206 runstate_check(RUN_STATE_POSTMIGRATE))
207 {
208 bdrv_invalidate_cache_all(&local_err);
209 if (local_err) {
210 error_propagate(errp, local_err);
211 return;
212 }
213 }
214
Paolo Bonzini1e998142012-10-23 14:54:21 +0200215 if (runstate_check(RUN_STATE_INMIGRATE)) {
216 autostart = 1;
217 } else {
218 vm_start();
219 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200220}
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600221
Gerd Hoffmann9b9df252012-02-23 13:45:21 +0100222void qmp_system_wakeup(Error **errp)
223{
224 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
225}
226
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600227ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600228{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600229 Object *obj;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600230 bool ambiguous = false;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600231 ObjectPropertyInfoList *props = NULL;
232 ObjectProperty *prop;
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000233 ObjectPropertyIterator iter;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600234
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600235 obj = object_resolve_path(path, &ambiguous);
236 if (obj == NULL) {
Michael Tokarev79772082014-05-04 12:23:59 +0400237 if (ambiguous) {
238 error_setg(errp, "Path '%s' is ambiguous", path);
239 } else {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100240 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
241 "Device '%s' not found", path);
Michael Tokarev79772082014-05-04 12:23:59 +0400242 }
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600243 return NULL;
244 }
245
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000246 object_property_iter_init(&iter, obj);
247 while ((prop = object_property_iter_next(&iter))) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600248 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600249
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600250 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600251 entry->next = props;
252 props = entry;
253
254 entry->value->name = g_strdup(prop->name);
255 entry->value->type = g_strdup(prop->type);
256 }
257
258 return props;
259}
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600260
Markus Armbruster6eb39372015-09-16 13:06:25 +0200261void qmp_qom_set(const char *path, const char *property, QObject *value,
262 Error **errp)
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600263{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600264 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600265
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600266 obj = object_resolve_path(path, NULL);
267 if (!obj) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100268 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Markus Armbruster75158eb2015-03-16 08:57:47 +0100269 "Device '%s' not found", path);
Markus Armbruster485febc2015-03-13 17:25:50 +0100270 return;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600271 }
272
Markus Armbruster485febc2015-03-13 17:25:50 +0100273 object_property_set_qobject(obj, value, property, errp);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600274}
275
Markus Armbruster6eb39372015-09-16 13:06:25 +0200276QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600277{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600278 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600279
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600280 obj = object_resolve_path(path, NULL);
281 if (!obj) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100282 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Markus Armbruster75158eb2015-03-16 08:57:47 +0100283 "Device '%s' not found", path);
Markus Armbruster6eb39372015-09-16 13:06:25 +0200284 return NULL;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600285 }
286
Markus Armbruster6eb39372015-09-16 13:06:25 +0200287 return object_property_get_qobject(obj, property, errp);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600288}
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200289
290void qmp_set_password(const char *protocol, const char *password,
291 bool has_connected, const char *connected, Error **errp)
292{
293 int disconnect_if_connected = 0;
294 int fail_if_connected = 0;
295 int rc;
296
297 if (has_connected) {
298 if (strcmp(connected, "fail") == 0) {
299 fail_if_connected = 1;
300 } else if (strcmp(connected, "disconnect") == 0) {
301 disconnect_if_connected = 1;
302 } else if (strcmp(connected, "keep") == 0) {
303 /* nothing */
304 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100305 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200306 return;
307 }
308 }
309
310 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100311 if (!qemu_using_spice(errp)) {
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200312 return;
313 }
314 rc = qemu_spice_set_passwd(password, fail_if_connected,
315 disconnect_if_connected);
316 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100317 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200318 }
319 return;
320 }
321
322 if (strcmp(protocol, "vnc") == 0) {
323 if (fail_if_connected || disconnect_if_connected) {
324 /* vnc supports "connected=keep" only */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100325 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200326 return;
327 }
328 /* Note that setting an empty password will not disable login through
329 * this interface. */
330 rc = vnc_display_password(NULL, password);
331 if (rc < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100332 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200333 }
334 return;
335 }
336
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100337 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200338}
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200339
340void qmp_expire_password(const char *protocol, const char *whenstr,
341 Error **errp)
342{
343 time_t when;
344 int rc;
345
346 if (strcmp(whenstr, "now") == 0) {
347 when = 0;
348 } else if (strcmp(whenstr, "never") == 0) {
349 when = TIME_MAX;
350 } else if (whenstr[0] == '+') {
351 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
352 } else {
353 when = strtoull(whenstr, NULL, 10);
354 }
355
356 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100357 if (!qemu_using_spice(errp)) {
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200358 return;
359 }
360 rc = qemu_spice_set_pw_expire(when);
361 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100362 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200363 }
364 return;
365 }
366
367 if (strcmp(protocol, "vnc") == 0) {
368 rc = vnc_display_pw_expire(NULL, when);
369 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100370 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200371 }
372 return;
373 }
374
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100375 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200376}
Luiz Capitulino270b2432011-12-08 11:45:55 -0200377
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200378#ifdef CONFIG_VNC
Luiz Capitulino270b2432011-12-08 11:45:55 -0200379void qmp_change_vnc_password(const char *password, Error **errp)
380{
381 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100382 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino270b2432011-12-08 11:45:55 -0200383 }
384}
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200385
Paolo Bonzini007fcd3e2012-10-18 09:01:01 +0200386static void qmp_change_vnc_listen(const char *target, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200387{
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200388 QemuOptsList *olist = qemu_find_opts("vnc");
389 QemuOpts *opts;
390
391 if (strstr(target, "id=")) {
392 error_setg(errp, "id not supported");
393 return;
394 }
395
396 opts = qemu_opts_find(olist, "default");
397 if (opts) {
398 qemu_opts_del(opts);
399 }
Markus Armbruster70b94332015-02-13 12:50:26 +0100400 opts = vnc_parse(target, errp);
Gongleif7801c52015-02-05 17:43:35 +0800401 if (!opts) {
402 return;
403 }
404
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200405 vnc_display_open("default", errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200406}
407
408static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
409 Error **errp)
410{
411 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
412 if (!has_arg) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100413 error_setg(errp, QERR_MISSING_PARAMETER, "password");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200414 } else {
415 qmp_change_vnc_password(arg, errp);
416 }
417 } else {
418 qmp_change_vnc_listen(target, errp);
419 }
420}
421#else
422void qmp_change_vnc_password(const char *password, Error **errp)
423{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100424 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200425}
426static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
427 Error **errp)
428{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100429 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200430}
431#endif /* !CONFIG_VNC */
432
433void qmp_change(const char *device, const char *target,
Markus Armbruster7daecb32014-05-02 13:26:31 +0200434 bool has_arg, const char *arg, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200435{
436 if (strcmp(device, "vnc") == 0) {
Markus Armbruster7daecb32014-05-02 13:26:31 +0200437 qmp_change_vnc(target, has_arg, arg, errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200438 } else {
Kevin Wolf70e2cb32016-09-20 13:38:47 +0200439 qmp_blockdev_change_medium(true, device, false, NULL, target,
440 has_arg, arg, false, 0, errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200441 }
442}
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600443
444static void qom_list_types_tramp(ObjectClass *klass, void *data)
445{
446 ObjectTypeInfoList *e, **pret = data;
447 ObjectTypeInfo *info;
448
449 info = g_malloc0(sizeof(*info));
450 info->name = g_strdup(object_class_get_name(klass));
451
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 */
478static DevicePropertyInfo *make_device_property_info(ObjectClass *klass,
479 const char *name,
Gonglei07d09c52014-10-07 14:33:23 +0800480 const char *default_type,
481 const char *description)
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200482{
483 DevicePropertyInfo *info;
484 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 */
498 if (!prop->info->set) {
499 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);
Gonglei07d09c52014-10-07 14:33:23 +0800504 info->type = g_strdup(prop->info->name);
505 info->has_description = !!prop->info->description;
506 info->description = g_strdup(prop->info->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200507 return info;
508 }
509 klass = object_class_get_parent(klass);
510 } while (klass != object_class_by_name(TYPE_DEVICE));
511
512 /* Not a qdev property, use the default type */
513 info = g_malloc0(sizeof(*info));
514 info->name = g_strdup(name);
515 info->type = g_strdup(default_type);
Gonglei07d09c52014-10-07 14:33:23 +0800516 info->has_description = !!description;
517 info->description = g_strdup(description);
518
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200519 return info;
520}
521
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500522DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
523 Error **errp)
524{
525 ObjectClass *klass;
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200526 Object *obj;
527 ObjectProperty *prop;
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000528 ObjectPropertyIterator iter;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500529 DevicePropertyInfoList *prop_list = NULL;
530
531 klass = object_class_by_name(typename);
532 if (klass == NULL) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100533 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
534 "Device '%s' not found", typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500535 return NULL;
536 }
537
538 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
539 if (klass == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100540 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name", TYPE_DEVICE);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500541 return NULL;
542 }
543
Markus Armbrusteredb15232015-10-01 10:59:57 +0200544 if (object_class_is_abstract(klass)) {
545 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name",
546 "non-abstract device type");
547 return NULL;
548 }
549
Markus Armbruster4c315c22015-10-01 10:59:58 +0200550 if (DEVICE_CLASS(klass)->cannot_destroy_with_object_finalize_yet) {
551 error_setg(errp, "Can't list properties of device '%s'", typename);
552 return NULL;
553 }
554
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200555 obj = object_new(typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500556
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000557 object_property_iter_init(&iter, obj);
558 while ((prop = object_property_iter_next(&iter))) {
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200559 DevicePropertyInfo *info;
560 DevicePropertyInfoList *entry;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500561
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200562 /* Skip Object and DeviceState properties */
563 if (strcmp(prop->name, "type") == 0 ||
564 strcmp(prop->name, "realized") == 0 ||
565 strcmp(prop->name, "hotpluggable") == 0 ||
Stefan Hajnoczi4115dd62014-07-09 14:01:31 +0200566 strcmp(prop->name, "hotplugged") == 0 ||
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200567 strcmp(prop->name, "parent_bus") == 0) {
568 continue;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500569 }
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200570
571 /* Skip legacy properties since they are just string versions of
572 * properties that we already list.
573 */
574 if (strstart(prop->name, "legacy-", NULL)) {
575 continue;
576 }
577
Gonglei07d09c52014-10-07 14:33:23 +0800578 info = make_device_property_info(klass, prop->name, prop->type,
579 prop->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200580 if (!info) {
581 continue;
582 }
583
584 entry = g_malloc0(sizeof(*entry));
585 entry->value = info;
586 entry->next = prop_list;
587 prop_list = entry;
588 }
589
590 object_unref(obj);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500591
592 return prop_list;
593}
Anthony Liguorie4e31c62012-08-10 11:04:13 -0500594
Anthony Liguori76b64a72012-08-14 22:17:36 -0500595CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
596{
597 return arch_query_cpu_definitions(errp);
598}
599
David Hildenbrande09484e2016-09-05 10:52:39 +0200600CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
601 CpuModelInfo *model,
602 Error **errp)
603{
604 return arch_query_cpu_model_expansion(type, model, errp);
605}
606
David Hildenbrand0031e0d2016-09-05 10:52:40 +0200607CpuModelCompareInfo *qmp_query_cpu_model_comparison(CpuModelInfo *modela,
608 CpuModelInfo *modelb,
609 Error **errp)
610{
611 return arch_query_cpu_model_comparison(modela, modelb, errp);
612}
613
David Hildenbrandb18b6042016-09-05 10:52:41 +0200614CpuModelBaselineInfo *qmp_query_cpu_model_baseline(CpuModelInfo *modela,
615 CpuModelInfo *modelb,
616 Error **errp)
617{
618 return arch_query_cpu_model_baseline(modela, modelb, errp);
619}
620
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300621void qmp_add_client(const char *protocol, const char *fdname,
622 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
623 Error **errp)
624{
625 CharDriverState *s;
626 int fd;
627
628 fd = monitor_get_fd(cur_mon, fdname, errp);
629 if (fd < 0) {
630 return;
631 }
632
633 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100634 if (!qemu_using_spice(errp)) {
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300635 close(fd);
636 return;
637 }
638 skipauth = has_skipauth ? skipauth : false;
639 tls = has_tls ? tls : false;
640 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
641 error_setg(errp, "spice failed to add client");
642 close(fd);
643 }
644 return;
645#ifdef CONFIG_VNC
646 } else if (strcmp(protocol, "vnc") == 0) {
647 skipauth = has_skipauth ? skipauth : false;
648 vnc_display_add_client(NULL, fd, skipauth);
649 return;
650#endif
651 } else if ((s = qemu_chr_find(protocol)) != NULL) {
652 if (qemu_chr_add_client(s, fd) < 0) {
653 error_setg(errp, "failed to add client");
654 close(fd);
655 return;
656 }
657 return;
658 }
659
660 error_setg(errp, "protocol '%s' is invalid", protocol);
661 close(fd);
662}
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100663
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100664
Markus Armbruster6eb39372015-09-16 13:06:25 +0200665void qmp_object_add(const char *type, const char *id,
666 bool has_props, QObject *props, Error **errp)
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100667{
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100668 const QDict *pdict = NULL;
Eric Blakeb70ce1012016-06-09 10:48:38 -0600669 Visitor *v;
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000670 Object *obj;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100671
672 if (props) {
673 pdict = qobject_to_qdict(props);
674 if (!pdict) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100675 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
676 return;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100677 }
678 }
679
Eric Blakeb70ce1012016-06-09 10:48:38 -0600680 v = qmp_input_visitor_new(props, true);
681 obj = user_creatable_add_type(type, id, pdict, v, errp);
682 visit_free(v);
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000683 if (obj) {
684 object_unref(obj);
685 }
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100686}
687
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100688void qmp_object_del(const char *id, Error **errp)
689{
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000690 user_creatable_del(id, errp);
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100691}
Igor Mammedov6f2e2732014-06-16 19:12:25 +0200692
693MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
694{
695 MemoryDeviceInfoList *head = NULL;
696 MemoryDeviceInfoList **prev = &head;
697
698 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
699
700 return head;
701}
Igor Mammedov02419bc2014-06-16 19:12:28 +0200702
703ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
704{
705 bool ambig;
706 ACPIOSTInfoList *head = NULL;
707 ACPIOSTInfoList **prev = &head;
708 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
709
710 if (obj) {
711 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
712 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
713
714 adevc->ospm_status(adev, &prev);
715 } else {
716 error_setg(errp, "command is not supported, missing ACPI device");
717 }
718
719 return head;
720}