blob: 6ae723022eb6c0cf24b46c1f837dd49e3fa1b599 [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"
Anthony Liguori48a32be2011-09-02 12:34:48 -050017#include "qemu-common.h"
Markus Armbrustera0b1a662015-03-17 18:16:21 +010018#include "monitor/monitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010019#include "sysemu/sysemu.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050020#include "qmp-commands.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020021#include "sysemu/char.h"
Luiz Capitulinofbf796f2011-12-07 11:17:51 -020022#include "ui/qemu-spice.h"
23#include "ui/vnc.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010024#include "sysemu/kvm.h"
25#include "sysemu/arch_init.h"
Anthony Liguorib4b12c62011-12-12 14:29:34 -060026#include "hw/qdev.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010027#include "sysemu/blockdev.h"
Max Reitz373340b2015-10-19 17:53:22 +020028#include "sysemu/block-backend.h"
Paolo Bonzini14cccb62012-12-17 18:19:50 +010029#include "qom/qom-qobject.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010030#include "qapi/qmp/qerror.h"
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +010031#include "qapi/qmp/qobject.h"
32#include "qapi/qmp-input-visitor.h"
Igor Mammedov69ca3ea2013-04-30 15:41:25 +020033#include "hw/boards.h"
Igor Mammedov269e09f2014-01-16 17:34:38 +010034#include "qom/object_interfaces.h"
Igor Mammedov6f2e2732014-06-16 19:12:25 +020035#include "hw/mem/pc-dimm.h"
Igor Mammedov02419bc2014-06-16 19:12:28 +020036#include "hw/acpi/acpi_dev_interface.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050037
38NameInfo *qmp_query_name(Error **errp)
39{
40 NameInfo *info = g_malloc0(sizeof(*info));
41
42 if (qemu_name) {
43 info->has_name = true;
44 info->name = g_strdup(qemu_name);
45 }
46
47 return info;
48}
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030049
Markus Armbruster7daecb32014-05-02 13:26:31 +020050VersionInfo *qmp_query_version(Error **errp)
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030051{
Eric Blake4752cdb2015-05-04 09:05:31 -060052 VersionInfo *info = g_new0(VersionInfo, 1);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030053 const char *version = QEMU_VERSION;
Carlos L. Torresd4ba8cb2015-07-19 18:02:21 -050054 const char *tmp;
55 int err;
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030056
Eric Blake4752cdb2015-05-04 09:05:31 -060057 info->qemu = g_new0(VersionTriple, 1);
Carlos L. Torresd4ba8cb2015-07-19 18:02:21 -050058 err = qemu_strtoll(version, &tmp, 10, &info->qemu->major);
59 assert(err == 0);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030060 tmp++;
Carlos L. Torresd4ba8cb2015-07-19 18:02:21 -050061
62 err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->minor);
63 assert(err == 0);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030064 tmp++;
Carlos L. Torresd4ba8cb2015-07-19 18:02:21 -050065
66 err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->micro);
67 assert(err == 0);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030068 info->package = g_strdup(QEMU_PKGVERSION);
69
70 return info;
71}
Luiz Capitulino292a2602011-09-12 15:10:53 -030072
73KvmInfo *qmp_query_kvm(Error **errp)
74{
75 KvmInfo *info = g_malloc0(sizeof(*info));
76
77 info->enabled = kvm_enabled();
78 info->present = kvm_available();
79
80 return info;
81}
82
Luiz Capitulinoefab7672011-09-13 17:16:25 -030083UuidInfo *qmp_query_uuid(Error **errp)
84{
85 UuidInfo *info = g_malloc0(sizeof(*info));
86 char uuid[64];
87
88 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
89 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
90 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
91 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
92 qemu_uuid[14], qemu_uuid[15]);
93
94 info->UUID = g_strdup(uuid);
95 return info;
96}
97
Markus Armbruster7daecb32014-05-02 13:26:31 +020098void qmp_quit(Error **errp)
Luiz Capitulino7a7f3252011-09-15 14:20:28 -030099{
100 no_shutdown = 0;
101 qemu_system_shutdown_request();
102}
103
Luiz Capitulino5f158f22011-09-15 14:34:39 -0300104void qmp_stop(Error **errp)
105{
Paolo Bonzini1e998142012-10-23 14:54:21 +0200106 if (runstate_check(RUN_STATE_INMIGRATE)) {
107 autostart = 0;
108 } else {
109 vm_stop(RUN_STATE_PAUSED);
110 }
Luiz Capitulino5f158f22011-09-15 14:34:39 -0300111}
112
Luiz Capitulino38d22652011-09-15 14:41:46 -0300113void qmp_system_reset(Error **errp)
114{
115 qemu_system_reset_request();
116}
Luiz Capitulino5bc465e2011-09-28 11:06:15 -0300117
118void qmp_system_powerdown(Error **erp)
119{
120 qemu_system_powerdown_request();
121}
Luiz Capitulino755f1962011-10-06 14:31:39 -0300122
123void qmp_cpu(int64_t index, Error **errp)
124{
125 /* Just do nothing */
126}
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200127
Igor Mammedov69ca3ea2013-04-30 15:41:25 +0200128void qmp_cpu_add(int64_t id, Error **errp)
129{
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +0200130 MachineClass *mc;
131
132 mc = MACHINE_GET_CLASS(current_machine);
Marcel Apfelbaum958db902014-04-09 20:34:53 +0300133 if (mc->hot_add_cpu) {
134 mc->hot_add_cpu(id, errp);
Igor Mammedov69ca3ea2013-04-30 15:41:25 +0200135 } else {
136 error_setg(errp, "Not supported");
137 }
138}
139
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200140#ifndef CONFIG_VNC
141/* If VNC support is enabled, the "true" query-vnc command is
142 defined in the VNC subsystem */
143VncInfo *qmp_query_vnc(Error **errp)
144{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100145 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200146 return NULL;
147};
Leon Yu89db21772015-02-02 05:08:51 +0000148
149VncInfo2List *qmp_query_vnc_servers(Error **errp)
150{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100151 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Leon Yu89db21772015-02-02 05:08:51 +0000152 return NULL;
153};
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200154#endif
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200155
156#ifndef CONFIG_SPICE
Markus Armbrusterad0ec142015-01-13 15:56:11 +0100157/*
158 * qmp-commands.hx ensures that QMP command query-spice exists only
159 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
160 * result. However, the QAPI schema is blissfully unaware of that,
161 * and the QAPI code generator happily generates a dead
Markus Armbruster7fad30f2015-09-16 13:06:19 +0200162 * qmp_marshal_query_spice() that calls qmp_query_spice(). Provide it
163 * one, or else linking fails. FIXME Educate the QAPI schema on
164 * CONFIG_SPICE.
Markus Armbrusterad0ec142015-01-13 15:56:11 +0100165 */
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200166SpiceInfo *qmp_query_spice(Error **errp)
167{
Markus Armbrusterad0ec142015-01-13 15:56:11 +0100168 abort();
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200169};
170#endif
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200171
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200172void qmp_cont(Error **errp)
173{
Markus Armbruster4d2855a2015-01-29 10:37:00 +0100174 Error *local_err = NULL;
Max Reitz373340b2015-10-19 17:53:22 +0200175 BlockBackend *blk;
Markus Armbrusterab319792014-05-02 13:26:42 +0200176 BlockDriverState *bs;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200177
Hu Taoede085b2013-04-26 11:24:40 +0800178 if (runstate_needs_reset()) {
Cole Robinsonf231b882014-03-21 19:42:26 -0400179 error_setg(errp, "Resetting the Virtual Machine is required");
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200180 return;
Luiz Capitulinoad02b962012-04-27 13:33:36 -0300181 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
182 return;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200183 }
184
Max Reitz373340b2015-10-19 17:53:22 +0200185 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
186 blk_iostatus_reset(blk);
Markus Armbrusterab319792014-05-02 13:26:42 +0200187 }
188 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
Markus Armbruster4d2855a2015-01-29 10:37:00 +0100189 bdrv_add_key(bs, NULL, &local_err);
190 if (local_err) {
191 error_propagate(errp, local_err);
Markus Armbrusterab319792014-05-02 13:26:42 +0200192 return;
193 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200194 }
195
Kevin Wolf76b1c7f2015-12-22 14:07:08 +0100196 /* Continuing after completed migration. Images have been inactivated to
197 * allow the destination to take control. Need to get control back now. */
198 if (runstate_check(RUN_STATE_FINISH_MIGRATE) ||
199 runstate_check(RUN_STATE_POSTMIGRATE))
200 {
201 bdrv_invalidate_cache_all(&local_err);
202 if (local_err) {
203 error_propagate(errp, local_err);
204 return;
205 }
206 }
207
Paolo Bonzini1e998142012-10-23 14:54:21 +0200208 if (runstate_check(RUN_STATE_INMIGRATE)) {
209 autostart = 1;
210 } else {
211 vm_start();
212 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200213}
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600214
Gerd Hoffmann9b9df252012-02-23 13:45:21 +0100215void qmp_system_wakeup(Error **errp)
216{
217 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
218}
219
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600220ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600221{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600222 Object *obj;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600223 bool ambiguous = false;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600224 ObjectPropertyInfoList *props = NULL;
225 ObjectProperty *prop;
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000226 ObjectPropertyIterator iter;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600227
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600228 obj = object_resolve_path(path, &ambiguous);
229 if (obj == NULL) {
Michael Tokarev79772082014-05-04 12:23:59 +0400230 if (ambiguous) {
231 error_setg(errp, "Path '%s' is ambiguous", path);
232 } else {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100233 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
234 "Device '%s' not found", path);
Michael Tokarev79772082014-05-04 12:23:59 +0400235 }
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600236 return NULL;
237 }
238
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000239 object_property_iter_init(&iter, obj);
240 while ((prop = object_property_iter_next(&iter))) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600241 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600242
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600243 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600244 entry->next = props;
245 props = entry;
246
247 entry->value->name = g_strdup(prop->name);
248 entry->value->type = g_strdup(prop->type);
249 }
250
251 return props;
252}
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600253
Markus Armbruster6eb39372015-09-16 13:06:25 +0200254void qmp_qom_set(const char *path, const char *property, QObject *value,
255 Error **errp)
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600256{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600257 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600258
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600259 obj = object_resolve_path(path, NULL);
260 if (!obj) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100261 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Markus Armbruster75158eb2015-03-16 08:57:47 +0100262 "Device '%s' not found", path);
Markus Armbruster485febc2015-03-13 17:25:50 +0100263 return;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600264 }
265
Markus Armbruster485febc2015-03-13 17:25:50 +0100266 object_property_set_qobject(obj, value, property, errp);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600267}
268
Markus Armbruster6eb39372015-09-16 13:06:25 +0200269QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600270{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600271 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600272
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600273 obj = object_resolve_path(path, NULL);
274 if (!obj) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100275 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Markus Armbruster75158eb2015-03-16 08:57:47 +0100276 "Device '%s' not found", path);
Markus Armbruster6eb39372015-09-16 13:06:25 +0200277 return NULL;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600278 }
279
Markus Armbruster6eb39372015-09-16 13:06:25 +0200280 return object_property_get_qobject(obj, property, errp);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600281}
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200282
283void qmp_set_password(const char *protocol, const char *password,
284 bool has_connected, const char *connected, Error **errp)
285{
286 int disconnect_if_connected = 0;
287 int fail_if_connected = 0;
288 int rc;
289
290 if (has_connected) {
291 if (strcmp(connected, "fail") == 0) {
292 fail_if_connected = 1;
293 } else if (strcmp(connected, "disconnect") == 0) {
294 disconnect_if_connected = 1;
295 } else if (strcmp(connected, "keep") == 0) {
296 /* nothing */
297 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100298 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200299 return;
300 }
301 }
302
303 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100304 if (!qemu_using_spice(errp)) {
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200305 return;
306 }
307 rc = qemu_spice_set_passwd(password, fail_if_connected,
308 disconnect_if_connected);
309 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100310 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200311 }
312 return;
313 }
314
315 if (strcmp(protocol, "vnc") == 0) {
316 if (fail_if_connected || disconnect_if_connected) {
317 /* vnc supports "connected=keep" only */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100318 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200319 return;
320 }
321 /* Note that setting an empty password will not disable login through
322 * this interface. */
323 rc = vnc_display_password(NULL, password);
324 if (rc < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100325 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200326 }
327 return;
328 }
329
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100330 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200331}
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200332
333void qmp_expire_password(const char *protocol, const char *whenstr,
334 Error **errp)
335{
336 time_t when;
337 int rc;
338
339 if (strcmp(whenstr, "now") == 0) {
340 when = 0;
341 } else if (strcmp(whenstr, "never") == 0) {
342 when = TIME_MAX;
343 } else if (whenstr[0] == '+') {
344 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
345 } else {
346 when = strtoull(whenstr, NULL, 10);
347 }
348
349 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100350 if (!qemu_using_spice(errp)) {
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200351 return;
352 }
353 rc = qemu_spice_set_pw_expire(when);
354 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100355 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200356 }
357 return;
358 }
359
360 if (strcmp(protocol, "vnc") == 0) {
361 rc = vnc_display_pw_expire(NULL, when);
362 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100363 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200364 }
365 return;
366 }
367
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100368 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200369}
Luiz Capitulino270b2432011-12-08 11:45:55 -0200370
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200371#ifdef CONFIG_VNC
Luiz Capitulino270b2432011-12-08 11:45:55 -0200372void qmp_change_vnc_password(const char *password, Error **errp)
373{
374 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100375 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino270b2432011-12-08 11:45:55 -0200376 }
377}
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200378
Paolo Bonzini007fcd3e2012-10-18 09:01:01 +0200379static void qmp_change_vnc_listen(const char *target, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200380{
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200381 QemuOptsList *olist = qemu_find_opts("vnc");
382 QemuOpts *opts;
383
384 if (strstr(target, "id=")) {
385 error_setg(errp, "id not supported");
386 return;
387 }
388
389 opts = qemu_opts_find(olist, "default");
390 if (opts) {
391 qemu_opts_del(opts);
392 }
Markus Armbruster70b94332015-02-13 12:50:26 +0100393 opts = vnc_parse(target, errp);
Gongleif7801c52015-02-05 17:43:35 +0800394 if (!opts) {
395 return;
396 }
397
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200398 vnc_display_open("default", errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200399}
400
401static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
402 Error **errp)
403{
404 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
405 if (!has_arg) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100406 error_setg(errp, QERR_MISSING_PARAMETER, "password");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200407 } else {
408 qmp_change_vnc_password(arg, errp);
409 }
410 } else {
411 qmp_change_vnc_listen(target, errp);
412 }
413}
414#else
415void qmp_change_vnc_password(const char *password, Error **errp)
416{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100417 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200418}
419static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
420 Error **errp)
421{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100422 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200423}
424#endif /* !CONFIG_VNC */
425
426void qmp_change(const char *device, const char *target,
Markus Armbruster7daecb32014-05-02 13:26:31 +0200427 bool has_arg, const char *arg, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200428{
429 if (strcmp(device, "vnc") == 0) {
Markus Armbruster7daecb32014-05-02 13:26:31 +0200430 qmp_change_vnc(target, has_arg, arg, errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200431 } else {
Max Reitz39ff43d2015-11-11 04:49:44 +0100432 qmp_blockdev_change_medium(device, target, has_arg, arg, false, 0,
433 errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200434 }
435}
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600436
437static void qom_list_types_tramp(ObjectClass *klass, void *data)
438{
439 ObjectTypeInfoList *e, **pret = data;
440 ObjectTypeInfo *info;
441
442 info = g_malloc0(sizeof(*info));
443 info->name = g_strdup(object_class_get_name(klass));
444
445 e = g_malloc0(sizeof(*e));
446 e->value = info;
447 e->next = *pret;
448 *pret = e;
449}
450
451ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
452 const char *implements,
453 bool has_abstract,
454 bool abstract,
455 Error **errp)
456{
457 ObjectTypeInfoList *ret = NULL;
458
459 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
460
461 return ret;
462}
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500463
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200464/* Return a DevicePropertyInfo for a qdev property.
465 *
466 * If a qdev property with the given name does not exist, use the given default
467 * type. If the qdev property info should not be shown, return NULL.
468 *
469 * The caller must free the return value.
470 */
471static DevicePropertyInfo *make_device_property_info(ObjectClass *klass,
472 const char *name,
Gonglei07d09c52014-10-07 14:33:23 +0800473 const char *default_type,
474 const char *description)
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200475{
476 DevicePropertyInfo *info;
477 Property *prop;
478
479 do {
480 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
481 if (strcmp(name, prop->name) != 0) {
482 continue;
483 }
484
485 /*
486 * TODO Properties without a parser are just for dirty hacks.
487 * qdev_prop_ptr is the only such PropertyInfo. It's marked
488 * for removal. This conditional should be removed along with
489 * it.
490 */
491 if (!prop->info->set) {
492 return NULL; /* no way to set it, don't show */
493 }
494
495 info = g_malloc0(sizeof(*info));
496 info->name = g_strdup(prop->name);
Gonglei07d09c52014-10-07 14:33:23 +0800497 info->type = g_strdup(prop->info->name);
498 info->has_description = !!prop->info->description;
499 info->description = g_strdup(prop->info->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200500 return info;
501 }
502 klass = object_class_get_parent(klass);
503 } while (klass != object_class_by_name(TYPE_DEVICE));
504
505 /* Not a qdev property, use the default type */
506 info = g_malloc0(sizeof(*info));
507 info->name = g_strdup(name);
508 info->type = g_strdup(default_type);
Gonglei07d09c52014-10-07 14:33:23 +0800509 info->has_description = !!description;
510 info->description = g_strdup(description);
511
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200512 return info;
513}
514
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500515DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
516 Error **errp)
517{
518 ObjectClass *klass;
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200519 Object *obj;
520 ObjectProperty *prop;
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000521 ObjectPropertyIterator iter;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500522 DevicePropertyInfoList *prop_list = NULL;
523
524 klass = object_class_by_name(typename);
525 if (klass == NULL) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100526 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
527 "Device '%s' not found", typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500528 return NULL;
529 }
530
531 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
532 if (klass == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100533 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name", TYPE_DEVICE);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500534 return NULL;
535 }
536
Markus Armbrusteredb15232015-10-01 10:59:57 +0200537 if (object_class_is_abstract(klass)) {
538 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name",
539 "non-abstract device type");
540 return NULL;
541 }
542
Markus Armbruster4c315c22015-10-01 10:59:58 +0200543 if (DEVICE_CLASS(klass)->cannot_destroy_with_object_finalize_yet) {
544 error_setg(errp, "Can't list properties of device '%s'", typename);
545 return NULL;
546 }
547
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200548 obj = object_new(typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500549
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000550 object_property_iter_init(&iter, obj);
551 while ((prop = object_property_iter_next(&iter))) {
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200552 DevicePropertyInfo *info;
553 DevicePropertyInfoList *entry;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500554
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200555 /* Skip Object and DeviceState properties */
556 if (strcmp(prop->name, "type") == 0 ||
557 strcmp(prop->name, "realized") == 0 ||
558 strcmp(prop->name, "hotpluggable") == 0 ||
Stefan Hajnoczi4115dd62014-07-09 14:01:31 +0200559 strcmp(prop->name, "hotplugged") == 0 ||
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200560 strcmp(prop->name, "parent_bus") == 0) {
561 continue;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500562 }
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200563
564 /* Skip legacy properties since they are just string versions of
565 * properties that we already list.
566 */
567 if (strstart(prop->name, "legacy-", NULL)) {
568 continue;
569 }
570
Gonglei07d09c52014-10-07 14:33:23 +0800571 info = make_device_property_info(klass, prop->name, prop->type,
572 prop->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200573 if (!info) {
574 continue;
575 }
576
577 entry = g_malloc0(sizeof(*entry));
578 entry->value = info;
579 entry->next = prop_list;
580 prop_list = entry;
581 }
582
583 object_unref(obj);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500584
585 return prop_list;
586}
Anthony Liguorie4e31c62012-08-10 11:04:13 -0500587
Anthony Liguori76b64a72012-08-14 22:17:36 -0500588CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
589{
590 return arch_query_cpu_definitions(errp);
591}
592
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300593void qmp_add_client(const char *protocol, const char *fdname,
594 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
595 Error **errp)
596{
597 CharDriverState *s;
598 int fd;
599
600 fd = monitor_get_fd(cur_mon, fdname, errp);
601 if (fd < 0) {
602 return;
603 }
604
605 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100606 if (!qemu_using_spice(errp)) {
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300607 close(fd);
608 return;
609 }
610 skipauth = has_skipauth ? skipauth : false;
611 tls = has_tls ? tls : false;
612 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
613 error_setg(errp, "spice failed to add client");
614 close(fd);
615 }
616 return;
617#ifdef CONFIG_VNC
618 } else if (strcmp(protocol, "vnc") == 0) {
619 skipauth = has_skipauth ? skipauth : false;
620 vnc_display_add_client(NULL, fd, skipauth);
621 return;
622#endif
623 } else if ((s = qemu_chr_find(protocol)) != NULL) {
624 if (qemu_chr_add_client(s, fd) < 0) {
625 error_setg(errp, "failed to add client");
626 close(fd);
627 return;
628 }
629 return;
630 }
631
632 error_setg(errp, "protocol '%s' is invalid", protocol);
633 close(fd);
634}
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100635
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100636void object_add(const char *type, const char *id, const QDict *qdict,
637 Visitor *v, Error **errp)
638{
639 Object *obj;
Eduardo Habkostc3481242014-04-16 14:39:38 -0300640 ObjectClass *klass;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100641 const QDictEntry *e;
642 Error *local_err = NULL;
643
Eduardo Habkostc3481242014-04-16 14:39:38 -0300644 klass = object_class_by_name(type);
645 if (!klass) {
Paolo Bonzinid1169462014-05-14 17:43:13 +0800646 error_setg(errp, "invalid object type: %s", type);
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100647 return;
648 }
649
Eduardo Habkostc3481242014-04-16 14:39:38 -0300650 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
651 error_setg(errp, "object type '%s' isn't supported by object-add",
652 type);
653 return;
654 }
655
656 if (object_class_is_abstract(klass)) {
657 error_setg(errp, "object type '%s' is abstract", type);
658 return;
659 }
660
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100661 obj = object_new(type);
662 if (qdict) {
663 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
664 object_property_set(obj, v, e->key, &local_err);
665 if (local_err) {
Igor Mammedov69252c02014-01-16 17:34:36 +0100666 goto out;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100667 }
668 }
669 }
670
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +0100671 object_property_add_child(object_get_objects_root(),
Igor Mammedova790f4e2014-06-02 15:24:59 +0200672 id, obj, &local_err);
Igor Mammedov269e09f2014-01-16 17:34:38 +0100673 if (local_err) {
674 goto out;
675 }
676
Igor Mammedova790f4e2014-06-02 15:24:59 +0200677 user_creatable_complete(obj, &local_err);
678 if (local_err) {
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +0100679 object_property_del(object_get_objects_root(),
Igor Mammedova790f4e2014-06-02 15:24:59 +0200680 id, &error_abort);
681 goto out;
682 }
Igor Mammedov69252c02014-01-16 17:34:36 +0100683out:
684 if (local_err) {
685 error_propagate(errp, local_err);
686 }
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100687 object_unref(obj);
688}
689
Markus Armbruster6eb39372015-09-16 13:06:25 +0200690void qmp_object_add(const char *type, const char *id,
691 bool has_props, QObject *props, Error **errp)
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100692{
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100693 const QDict *pdict = NULL;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100694 QmpInputVisitor *qiv;
695
696 if (props) {
697 pdict = qobject_to_qdict(props);
698 if (!pdict) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100699 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
700 return;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100701 }
702 }
703
704 qiv = qmp_input_visitor_new(props);
Markus Armbruster485febc2015-03-13 17:25:50 +0100705 object_add(type, id, pdict, qmp_input_get_visitor(qiv), errp);
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100706 qmp_input_visitor_cleanup(qiv);
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100707}
708
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100709void qmp_object_del(const char *id, Error **errp)
710{
711 Object *container;
712 Object *obj;
713
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +0100714 container = object_get_objects_root();
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100715 obj = object_resolve_path_component(container, id);
716 if (!obj) {
717 error_setg(errp, "object id not found");
718 return;
719 }
Lin Mad6edb152015-03-30 16:36:28 +0800720
721 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj), errp)) {
722 error_setg(errp, "%s is in use, can not be deleted", id);
723 return;
724 }
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100725 object_unparent(obj);
726}
Igor Mammedov6f2e2732014-06-16 19:12:25 +0200727
728MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
729{
730 MemoryDeviceInfoList *head = NULL;
731 MemoryDeviceInfoList **prev = &head;
732
733 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
734
735 return head;
736}
Igor Mammedov02419bc2014-06-16 19:12:28 +0200737
738ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
739{
740 bool ambig;
741 ACPIOSTInfoList *head = NULL;
742 ACPIOSTInfoList **prev = &head;
743 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
744
745 if (obj) {
746 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
747 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
748
749 adevc->ospm_status(adev, &prev);
750 } else {
751 error_setg(errp, "command is not supported, missing ACPI device");
752 }
753
754 return head;
755}