blob: dea8f81345b5623175c0f954b0ba15ddd270e333 [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 const char *version = QEMU_VERSION;
Carlos L. Torresd4ba8cb2015-07-19 18:02:21 -050055 const char *tmp;
56 int err;
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030057
Eric Blake4752cdb2015-05-04 09:05:31 -060058 info->qemu = g_new0(VersionTriple, 1);
Carlos L. Torresd4ba8cb2015-07-19 18:02:21 -050059 err = qemu_strtoll(version, &tmp, 10, &info->qemu->major);
60 assert(err == 0);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030061 tmp++;
Carlos L. Torresd4ba8cb2015-07-19 18:02:21 -050062
63 err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->minor);
64 assert(err == 0);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030065 tmp++;
Carlos L. Torresd4ba8cb2015-07-19 18:02:21 -050066
67 err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->micro);
68 assert(err == 0);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030069 info->package = g_strdup(QEMU_PKGVERSION);
70
71 return info;
72}
Luiz Capitulino292a2602011-09-12 15:10:53 -030073
74KvmInfo *qmp_query_kvm(Error **errp)
75{
76 KvmInfo *info = g_malloc0(sizeof(*info));
77
78 info->enabled = kvm_enabled();
79 info->present = kvm_available();
80
81 return info;
82}
83
Luiz Capitulinoefab7672011-09-13 17:16:25 -030084UuidInfo *qmp_query_uuid(Error **errp)
85{
86 UuidInfo *info = g_malloc0(sizeof(*info));
87 char uuid[64];
88
89 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
90 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
91 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
92 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
93 qemu_uuid[14], qemu_uuid[15]);
94
95 info->UUID = g_strdup(uuid);
96 return info;
97}
98
Markus Armbruster7daecb32014-05-02 13:26:31 +020099void qmp_quit(Error **errp)
Luiz Capitulino7a7f3252011-09-15 14:20:28 -0300100{
101 no_shutdown = 0;
102 qemu_system_shutdown_request();
103}
104
Luiz Capitulino5f158f22011-09-15 14:34:39 -0300105void qmp_stop(Error **errp)
106{
Peter Xu65d64f32016-02-18 13:16:49 +0800107 /* if there is a dump in background, we should wait until the dump
108 * finished */
109 if (dump_in_progress()) {
110 error_setg(errp, "There is a dump in process, please wait.");
111 return;
112 }
113
Paolo Bonzini1e998142012-10-23 14:54:21 +0200114 if (runstate_check(RUN_STATE_INMIGRATE)) {
115 autostart = 0;
116 } else {
117 vm_stop(RUN_STATE_PAUSED);
118 }
Luiz Capitulino5f158f22011-09-15 14:34:39 -0300119}
120
Luiz Capitulino38d22652011-09-15 14:41:46 -0300121void qmp_system_reset(Error **errp)
122{
123 qemu_system_reset_request();
124}
Luiz Capitulino5bc465e2011-09-28 11:06:15 -0300125
126void qmp_system_powerdown(Error **erp)
127{
128 qemu_system_powerdown_request();
129}
Luiz Capitulino755f1962011-10-06 14:31:39 -0300130
131void qmp_cpu(int64_t index, Error **errp)
132{
133 /* Just do nothing */
134}
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200135
Igor Mammedov69ca3ea2013-04-30 15:41:25 +0200136void qmp_cpu_add(int64_t id, Error **errp)
137{
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +0200138 MachineClass *mc;
139
140 mc = MACHINE_GET_CLASS(current_machine);
Marcel Apfelbaum958db902014-04-09 20:34:53 +0300141 if (mc->hot_add_cpu) {
142 mc->hot_add_cpu(id, errp);
Igor Mammedov69ca3ea2013-04-30 15:41:25 +0200143 } else {
144 error_setg(errp, "Not supported");
145 }
146}
147
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200148#ifndef CONFIG_VNC
149/* If VNC support is enabled, the "true" query-vnc command is
150 defined in the VNC subsystem */
151VncInfo *qmp_query_vnc(Error **errp)
152{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100153 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200154 return NULL;
155};
Leon Yu89db21772015-02-02 05:08:51 +0000156
157VncInfo2List *qmp_query_vnc_servers(Error **errp)
158{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100159 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Leon Yu89db21772015-02-02 05:08:51 +0000160 return NULL;
161};
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200162#endif
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200163
164#ifndef CONFIG_SPICE
Markus Armbrusterad0ec142015-01-13 15:56:11 +0100165/*
166 * qmp-commands.hx ensures that QMP command query-spice exists only
167 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
168 * result. However, the QAPI schema is blissfully unaware of that,
169 * and the QAPI code generator happily generates a dead
Markus Armbruster7fad30f2015-09-16 13:06:19 +0200170 * qmp_marshal_query_spice() that calls qmp_query_spice(). Provide it
171 * one, or else linking fails. FIXME Educate the QAPI schema on
172 * CONFIG_SPICE.
Markus Armbrusterad0ec142015-01-13 15:56:11 +0100173 */
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200174SpiceInfo *qmp_query_spice(Error **errp)
175{
Markus Armbrusterad0ec142015-01-13 15:56:11 +0100176 abort();
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200177};
178#endif
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200179
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200180void qmp_cont(Error **errp)
181{
Markus Armbruster4d2855a2015-01-29 10:37:00 +0100182 Error *local_err = NULL;
Max Reitz373340b2015-10-19 17:53:22 +0200183 BlockBackend *blk;
Markus Armbrusterab319792014-05-02 13:26:42 +0200184 BlockDriverState *bs;
Kevin Wolf88be7b42016-05-20 18:49:07 +0200185 BdrvNextIterator it;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200186
Peter Xu65d64f32016-02-18 13:16:49 +0800187 /* if there is a dump in background, we should wait until the dump
188 * finished */
189 if (dump_in_progress()) {
190 error_setg(errp, "There is a dump in process, please wait.");
191 return;
192 }
193
Hu Taoede085b2013-04-26 11:24:40 +0800194 if (runstate_needs_reset()) {
Cole Robinsonf231b882014-03-21 19:42:26 -0400195 error_setg(errp, "Resetting the Virtual Machine is required");
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200196 return;
Luiz Capitulinoad02b962012-04-27 13:33:36 -0300197 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
198 return;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200199 }
200
Max Reitz373340b2015-10-19 17:53:22 +0200201 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
202 blk_iostatus_reset(blk);
Markus Armbrusterab319792014-05-02 13:26:42 +0200203 }
Kevin Wolf7c8eece2016-03-22 18:58:50 +0100204
Kevin Wolf88be7b42016-05-20 18:49:07 +0200205 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
Markus Armbruster4d2855a2015-01-29 10:37:00 +0100206 bdrv_add_key(bs, NULL, &local_err);
207 if (local_err) {
208 error_propagate(errp, local_err);
Markus Armbrusterab319792014-05-02 13:26:42 +0200209 return;
210 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200211 }
212
Kevin Wolf76b1c7f2015-12-22 14:07:08 +0100213 /* Continuing after completed migration. Images have been inactivated to
214 * allow the destination to take control. Need to get control back now. */
215 if (runstate_check(RUN_STATE_FINISH_MIGRATE) ||
216 runstate_check(RUN_STATE_POSTMIGRATE))
217 {
218 bdrv_invalidate_cache_all(&local_err);
219 if (local_err) {
220 error_propagate(errp, local_err);
221 return;
222 }
223 }
224
Paolo Bonzini1e998142012-10-23 14:54:21 +0200225 if (runstate_check(RUN_STATE_INMIGRATE)) {
226 autostart = 1;
227 } else {
228 vm_start();
229 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200230}
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600231
Gerd Hoffmann9b9df252012-02-23 13:45:21 +0100232void qmp_system_wakeup(Error **errp)
233{
234 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
235}
236
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600237ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600238{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600239 Object *obj;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600240 bool ambiguous = false;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600241 ObjectPropertyInfoList *props = NULL;
242 ObjectProperty *prop;
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000243 ObjectPropertyIterator iter;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600244
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600245 obj = object_resolve_path(path, &ambiguous);
246 if (obj == NULL) {
Michael Tokarev79772082014-05-04 12:23:59 +0400247 if (ambiguous) {
248 error_setg(errp, "Path '%s' is ambiguous", path);
249 } else {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100250 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
251 "Device '%s' not found", path);
Michael Tokarev79772082014-05-04 12:23:59 +0400252 }
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600253 return NULL;
254 }
255
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000256 object_property_iter_init(&iter, obj);
257 while ((prop = object_property_iter_next(&iter))) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600258 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600259
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600260 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600261 entry->next = props;
262 props = entry;
263
264 entry->value->name = g_strdup(prop->name);
265 entry->value->type = g_strdup(prop->type);
266 }
267
268 return props;
269}
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600270
Markus Armbruster6eb39372015-09-16 13:06:25 +0200271void qmp_qom_set(const char *path, const char *property, QObject *value,
272 Error **errp)
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600273{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600274 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600275
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600276 obj = object_resolve_path(path, NULL);
277 if (!obj) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100278 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Markus Armbruster75158eb2015-03-16 08:57:47 +0100279 "Device '%s' not found", path);
Markus Armbruster485febc2015-03-13 17:25:50 +0100280 return;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600281 }
282
Markus Armbruster485febc2015-03-13 17:25:50 +0100283 object_property_set_qobject(obj, value, property, errp);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600284}
285
Markus Armbruster6eb39372015-09-16 13:06:25 +0200286QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600287{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600288 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600289
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600290 obj = object_resolve_path(path, NULL);
291 if (!obj) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100292 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Markus Armbruster75158eb2015-03-16 08:57:47 +0100293 "Device '%s' not found", path);
Markus Armbruster6eb39372015-09-16 13:06:25 +0200294 return NULL;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600295 }
296
Markus Armbruster6eb39372015-09-16 13:06:25 +0200297 return object_property_get_qobject(obj, property, errp);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600298}
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200299
300void qmp_set_password(const char *protocol, const char *password,
301 bool has_connected, const char *connected, Error **errp)
302{
303 int disconnect_if_connected = 0;
304 int fail_if_connected = 0;
305 int rc;
306
307 if (has_connected) {
308 if (strcmp(connected, "fail") == 0) {
309 fail_if_connected = 1;
310 } else if (strcmp(connected, "disconnect") == 0) {
311 disconnect_if_connected = 1;
312 } else if (strcmp(connected, "keep") == 0) {
313 /* nothing */
314 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100315 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200316 return;
317 }
318 }
319
320 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100321 if (!qemu_using_spice(errp)) {
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200322 return;
323 }
324 rc = qemu_spice_set_passwd(password, fail_if_connected,
325 disconnect_if_connected);
326 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100327 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200328 }
329 return;
330 }
331
332 if (strcmp(protocol, "vnc") == 0) {
333 if (fail_if_connected || disconnect_if_connected) {
334 /* vnc supports "connected=keep" only */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100335 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200336 return;
337 }
338 /* Note that setting an empty password will not disable login through
339 * this interface. */
340 rc = vnc_display_password(NULL, password);
341 if (rc < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100342 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200343 }
344 return;
345 }
346
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100347 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200348}
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200349
350void qmp_expire_password(const char *protocol, const char *whenstr,
351 Error **errp)
352{
353 time_t when;
354 int rc;
355
356 if (strcmp(whenstr, "now") == 0) {
357 when = 0;
358 } else if (strcmp(whenstr, "never") == 0) {
359 when = TIME_MAX;
360 } else if (whenstr[0] == '+') {
361 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
362 } else {
363 when = strtoull(whenstr, NULL, 10);
364 }
365
366 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100367 if (!qemu_using_spice(errp)) {
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200368 return;
369 }
370 rc = qemu_spice_set_pw_expire(when);
371 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100372 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200373 }
374 return;
375 }
376
377 if (strcmp(protocol, "vnc") == 0) {
378 rc = vnc_display_pw_expire(NULL, when);
379 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100380 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200381 }
382 return;
383 }
384
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100385 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200386}
Luiz Capitulino270b2432011-12-08 11:45:55 -0200387
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200388#ifdef CONFIG_VNC
Luiz Capitulino270b2432011-12-08 11:45:55 -0200389void qmp_change_vnc_password(const char *password, Error **errp)
390{
391 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100392 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino270b2432011-12-08 11:45:55 -0200393 }
394}
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200395
Paolo Bonzini007fcd3e2012-10-18 09:01:01 +0200396static void qmp_change_vnc_listen(const char *target, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200397{
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200398 QemuOptsList *olist = qemu_find_opts("vnc");
399 QemuOpts *opts;
400
401 if (strstr(target, "id=")) {
402 error_setg(errp, "id not supported");
403 return;
404 }
405
406 opts = qemu_opts_find(olist, "default");
407 if (opts) {
408 qemu_opts_del(opts);
409 }
Markus Armbruster70b94332015-02-13 12:50:26 +0100410 opts = vnc_parse(target, errp);
Gongleif7801c52015-02-05 17:43:35 +0800411 if (!opts) {
412 return;
413 }
414
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200415 vnc_display_open("default", errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200416}
417
418static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
419 Error **errp)
420{
421 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
422 if (!has_arg) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100423 error_setg(errp, QERR_MISSING_PARAMETER, "password");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200424 } else {
425 qmp_change_vnc_password(arg, errp);
426 }
427 } else {
428 qmp_change_vnc_listen(target, errp);
429 }
430}
431#else
432void qmp_change_vnc_password(const char *password, Error **errp)
433{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100434 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200435}
436static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
437 Error **errp)
438{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100439 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200440}
441#endif /* !CONFIG_VNC */
442
443void qmp_change(const char *device, const char *target,
Markus Armbruster7daecb32014-05-02 13:26:31 +0200444 bool has_arg, const char *arg, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200445{
446 if (strcmp(device, "vnc") == 0) {
Markus Armbruster7daecb32014-05-02 13:26:31 +0200447 qmp_change_vnc(target, has_arg, arg, errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200448 } else {
Max Reitz39ff43d2015-11-11 04:49:44 +0100449 qmp_blockdev_change_medium(device, target, has_arg, arg, false, 0,
450 errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200451 }
452}
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600453
454static void qom_list_types_tramp(ObjectClass *klass, void *data)
455{
456 ObjectTypeInfoList *e, **pret = data;
457 ObjectTypeInfo *info;
458
459 info = g_malloc0(sizeof(*info));
460 info->name = g_strdup(object_class_get_name(klass));
461
462 e = g_malloc0(sizeof(*e));
463 e->value = info;
464 e->next = *pret;
465 *pret = e;
466}
467
468ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
469 const char *implements,
470 bool has_abstract,
471 bool abstract,
472 Error **errp)
473{
474 ObjectTypeInfoList *ret = NULL;
475
476 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
477
478 return ret;
479}
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500480
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200481/* Return a DevicePropertyInfo for a qdev property.
482 *
483 * If a qdev property with the given name does not exist, use the given default
484 * type. If the qdev property info should not be shown, return NULL.
485 *
486 * The caller must free the return value.
487 */
488static DevicePropertyInfo *make_device_property_info(ObjectClass *klass,
489 const char *name,
Gonglei07d09c52014-10-07 14:33:23 +0800490 const char *default_type,
491 const char *description)
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200492{
493 DevicePropertyInfo *info;
494 Property *prop;
495
496 do {
497 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
498 if (strcmp(name, prop->name) != 0) {
499 continue;
500 }
501
502 /*
503 * TODO Properties without a parser are just for dirty hacks.
504 * qdev_prop_ptr is the only such PropertyInfo. It's marked
505 * for removal. This conditional should be removed along with
506 * it.
507 */
508 if (!prop->info->set) {
509 return NULL; /* no way to set it, don't show */
510 }
511
512 info = g_malloc0(sizeof(*info));
513 info->name = g_strdup(prop->name);
Gonglei07d09c52014-10-07 14:33:23 +0800514 info->type = g_strdup(prop->info->name);
515 info->has_description = !!prop->info->description;
516 info->description = g_strdup(prop->info->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200517 return info;
518 }
519 klass = object_class_get_parent(klass);
520 } while (klass != object_class_by_name(TYPE_DEVICE));
521
522 /* Not a qdev property, use the default type */
523 info = g_malloc0(sizeof(*info));
524 info->name = g_strdup(name);
525 info->type = g_strdup(default_type);
Gonglei07d09c52014-10-07 14:33:23 +0800526 info->has_description = !!description;
527 info->description = g_strdup(description);
528
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200529 return info;
530}
531
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500532DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
533 Error **errp)
534{
535 ObjectClass *klass;
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200536 Object *obj;
537 ObjectProperty *prop;
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000538 ObjectPropertyIterator iter;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500539 DevicePropertyInfoList *prop_list = NULL;
540
541 klass = object_class_by_name(typename);
542 if (klass == NULL) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100543 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
544 "Device '%s' not found", typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500545 return NULL;
546 }
547
548 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
549 if (klass == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100550 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name", TYPE_DEVICE);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500551 return NULL;
552 }
553
Markus Armbrusteredb15232015-10-01 10:59:57 +0200554 if (object_class_is_abstract(klass)) {
555 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name",
556 "non-abstract device type");
557 return NULL;
558 }
559
Markus Armbruster4c315c22015-10-01 10:59:58 +0200560 if (DEVICE_CLASS(klass)->cannot_destroy_with_object_finalize_yet) {
561 error_setg(errp, "Can't list properties of device '%s'", typename);
562 return NULL;
563 }
564
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200565 obj = object_new(typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500566
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000567 object_property_iter_init(&iter, obj);
568 while ((prop = object_property_iter_next(&iter))) {
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200569 DevicePropertyInfo *info;
570 DevicePropertyInfoList *entry;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500571
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200572 /* Skip Object and DeviceState properties */
573 if (strcmp(prop->name, "type") == 0 ||
574 strcmp(prop->name, "realized") == 0 ||
575 strcmp(prop->name, "hotpluggable") == 0 ||
Stefan Hajnoczi4115dd62014-07-09 14:01:31 +0200576 strcmp(prop->name, "hotplugged") == 0 ||
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200577 strcmp(prop->name, "parent_bus") == 0) {
578 continue;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500579 }
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200580
581 /* Skip legacy properties since they are just string versions of
582 * properties that we already list.
583 */
584 if (strstart(prop->name, "legacy-", NULL)) {
585 continue;
586 }
587
Gonglei07d09c52014-10-07 14:33:23 +0800588 info = make_device_property_info(klass, prop->name, prop->type,
589 prop->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200590 if (!info) {
591 continue;
592 }
593
594 entry = g_malloc0(sizeof(*entry));
595 entry->value = info;
596 entry->next = prop_list;
597 prop_list = entry;
598 }
599
600 object_unref(obj);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500601
602 return prop_list;
603}
Anthony Liguorie4e31c62012-08-10 11:04:13 -0500604
Anthony Liguori76b64a72012-08-14 22:17:36 -0500605CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
606{
607 return arch_query_cpu_definitions(errp);
608}
609
David Hildenbrande09484e2016-09-05 10:52:39 +0200610CpuModelExpansionInfo *qmp_query_cpu_model_expansion(CpuModelExpansionType type,
611 CpuModelInfo *model,
612 Error **errp)
613{
614 return arch_query_cpu_model_expansion(type, model, errp);
615}
616
David Hildenbrand0031e0d2016-09-05 10:52:40 +0200617CpuModelCompareInfo *qmp_query_cpu_model_comparison(CpuModelInfo *modela,
618 CpuModelInfo *modelb,
619 Error **errp)
620{
621 return arch_query_cpu_model_comparison(modela, modelb, errp);
622}
623
David Hildenbrandb18b6042016-09-05 10:52:41 +0200624CpuModelBaselineInfo *qmp_query_cpu_model_baseline(CpuModelInfo *modela,
625 CpuModelInfo *modelb,
626 Error **errp)
627{
628 return arch_query_cpu_model_baseline(modela, modelb, errp);
629}
630
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300631void qmp_add_client(const char *protocol, const char *fdname,
632 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
633 Error **errp)
634{
635 CharDriverState *s;
636 int fd;
637
638 fd = monitor_get_fd(cur_mon, fdname, errp);
639 if (fd < 0) {
640 return;
641 }
642
643 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100644 if (!qemu_using_spice(errp)) {
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300645 close(fd);
646 return;
647 }
648 skipauth = has_skipauth ? skipauth : false;
649 tls = has_tls ? tls : false;
650 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
651 error_setg(errp, "spice failed to add client");
652 close(fd);
653 }
654 return;
655#ifdef CONFIG_VNC
656 } else if (strcmp(protocol, "vnc") == 0) {
657 skipauth = has_skipauth ? skipauth : false;
658 vnc_display_add_client(NULL, fd, skipauth);
659 return;
660#endif
661 } else if ((s = qemu_chr_find(protocol)) != NULL) {
662 if (qemu_chr_add_client(s, fd) < 0) {
663 error_setg(errp, "failed to add client");
664 close(fd);
665 return;
666 }
667 return;
668 }
669
670 error_setg(errp, "protocol '%s' is invalid", protocol);
671 close(fd);
672}
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100673
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100674
Markus Armbruster6eb39372015-09-16 13:06:25 +0200675void qmp_object_add(const char *type, const char *id,
676 bool has_props, QObject *props, Error **errp)
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100677{
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100678 const QDict *pdict = NULL;
Eric Blakeb70ce1012016-06-09 10:48:38 -0600679 Visitor *v;
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000680 Object *obj;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100681
682 if (props) {
683 pdict = qobject_to_qdict(props);
684 if (!pdict) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100685 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
686 return;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100687 }
688 }
689
Eric Blakeb70ce1012016-06-09 10:48:38 -0600690 v = qmp_input_visitor_new(props, true);
691 obj = user_creatable_add_type(type, id, pdict, v, errp);
692 visit_free(v);
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000693 if (obj) {
694 object_unref(obj);
695 }
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100696}
697
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100698void qmp_object_del(const char *id, Error **errp)
699{
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000700 user_creatable_del(id, errp);
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100701}
Igor Mammedov6f2e2732014-06-16 19:12:25 +0200702
703MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
704{
705 MemoryDeviceInfoList *head = NULL;
706 MemoryDeviceInfoList **prev = &head;
707
708 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
709
710 return head;
711}
Igor Mammedov02419bc2014-06-16 19:12:28 +0200712
713ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
714{
715 bool ambig;
716 ACPIOSTInfoList *head = NULL;
717 ACPIOSTInfoList **prev = &head;
718 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
719
720 if (obj) {
721 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
722 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
723
724 adevc->ospm_status(adev, &prev);
725 } else {
726 error_setg(errp, "command is not supported, missing ACPI device");
727 }
728
729 return head;
730}