blob: 3f5dfe3f51a29be4cd53e3d3522d843f3a9c07be [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
16#include "qemu-common.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010017#include "sysemu/sysemu.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050018#include "qmp-commands.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020019#include "sysemu/char.h"
Luiz Capitulinofbf796f2011-12-07 11:17:51 -020020#include "ui/qemu-spice.h"
21#include "ui/vnc.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010022#include "sysemu/kvm.h"
23#include "sysemu/arch_init.h"
Anthony Liguorib4b12c62011-12-12 14:29:34 -060024#include "hw/qdev.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010025#include "sysemu/blockdev.h"
Paolo Bonzini14cccb62012-12-17 18:19:50 +010026#include "qom/qom-qobject.h"
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +010027#include "qapi/qmp/qobject.h"
28#include "qapi/qmp-input-visitor.h"
Igor Mammedov69ca3ea2013-04-30 15:41:25 +020029#include "hw/boards.h"
Igor Mammedov269e09f2014-01-16 17:34:38 +010030#include "qom/object_interfaces.h"
Igor Mammedov6f2e2732014-06-16 19:12:25 +020031#include "hw/mem/pc-dimm.h"
Igor Mammedov02419bc2014-06-16 19:12:28 +020032#include "hw/acpi/acpi_dev_interface.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050033
34NameInfo *qmp_query_name(Error **errp)
35{
36 NameInfo *info = g_malloc0(sizeof(*info));
37
38 if (qemu_name) {
39 info->has_name = true;
40 info->name = g_strdup(qemu_name);
41 }
42
43 return info;
44}
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030045
Markus Armbruster7daecb32014-05-02 13:26:31 +020046VersionInfo *qmp_query_version(Error **errp)
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030047{
Eric Blake4752cdb2015-05-04 09:05:31 -060048 VersionInfo *info = g_new0(VersionInfo, 1);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030049 const char *version = QEMU_VERSION;
50 char *tmp;
51
Eric Blake4752cdb2015-05-04 09:05:31 -060052 info->qemu = g_new0(VersionTriple, 1);
53 info->qemu->major = strtol(version, &tmp, 10);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030054 tmp++;
Eric Blake4752cdb2015-05-04 09:05:31 -060055 info->qemu->minor = strtol(tmp, &tmp, 10);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030056 tmp++;
Eric Blake4752cdb2015-05-04 09:05:31 -060057 info->qemu->micro = strtol(tmp, &tmp, 10);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030058 info->package = g_strdup(QEMU_PKGVERSION);
59
60 return info;
61}
Luiz Capitulino292a2602011-09-12 15:10:53 -030062
63KvmInfo *qmp_query_kvm(Error **errp)
64{
65 KvmInfo *info = g_malloc0(sizeof(*info));
66
67 info->enabled = kvm_enabled();
68 info->present = kvm_available();
69
70 return info;
71}
72
Luiz Capitulinoefab7672011-09-13 17:16:25 -030073UuidInfo *qmp_query_uuid(Error **errp)
74{
75 UuidInfo *info = g_malloc0(sizeof(*info));
76 char uuid[64];
77
78 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
79 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
80 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
81 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
82 qemu_uuid[14], qemu_uuid[15]);
83
84 info->UUID = g_strdup(uuid);
85 return info;
86}
87
Markus Armbruster7daecb32014-05-02 13:26:31 +020088void qmp_quit(Error **errp)
Luiz Capitulino7a7f3252011-09-15 14:20:28 -030089{
90 no_shutdown = 0;
91 qemu_system_shutdown_request();
92}
93
Luiz Capitulino5f158f22011-09-15 14:34:39 -030094void qmp_stop(Error **errp)
95{
Paolo Bonzini1e998142012-10-23 14:54:21 +020096 if (runstate_check(RUN_STATE_INMIGRATE)) {
97 autostart = 0;
98 } else {
99 vm_stop(RUN_STATE_PAUSED);
100 }
Luiz Capitulino5f158f22011-09-15 14:34:39 -0300101}
102
Luiz Capitulino38d22652011-09-15 14:41:46 -0300103void qmp_system_reset(Error **errp)
104{
105 qemu_system_reset_request();
106}
Luiz Capitulino5bc465e2011-09-28 11:06:15 -0300107
108void qmp_system_powerdown(Error **erp)
109{
110 qemu_system_powerdown_request();
111}
Luiz Capitulino755f1962011-10-06 14:31:39 -0300112
113void qmp_cpu(int64_t index, Error **errp)
114{
115 /* Just do nothing */
116}
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200117
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{
135 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
136 return NULL;
137};
Leon Yu89db21772015-02-02 05:08:51 +0000138
139VncInfo2List *qmp_query_vnc_servers(Error **errp)
140{
141 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
142 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
152 * qmp_marshal_input_query_spice() that calls qmp_query_spice().
153 * Provide it one, or else linking fails.
154 * FIXME Educate the QAPI schema on CONFIG_SPICE.
155 */
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{
Markus Armbruster4d2855a2015-01-29 10:37:00 +0100164 Error *local_err = NULL;
Markus Armbrusterab319792014-05-02 13:26:42 +0200165 BlockDriverState *bs;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200166
Hu Taoede085b2013-04-26 11:24:40 +0800167 if (runstate_needs_reset()) {
Cole Robinsonf231b882014-03-21 19:42:26 -0400168 error_setg(errp, "Resetting the Virtual Machine is required");
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200169 return;
Luiz Capitulinoad02b962012-04-27 13:33:36 -0300170 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
171 return;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200172 }
173
Markus Armbrusterab319792014-05-02 13:26:42 +0200174 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
175 bdrv_iostatus_reset(bs);
176 }
177 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
Markus Armbruster4d2855a2015-01-29 10:37:00 +0100178 bdrv_add_key(bs, NULL, &local_err);
179 if (local_err) {
180 error_propagate(errp, local_err);
Markus Armbrusterab319792014-05-02 13:26:42 +0200181 return;
182 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200183 }
184
Paolo Bonzini1e998142012-10-23 14:54:21 +0200185 if (runstate_check(RUN_STATE_INMIGRATE)) {
186 autostart = 1;
187 } else {
188 vm_start();
189 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200190}
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600191
Gerd Hoffmann9b9df252012-02-23 13:45:21 +0100192void qmp_system_wakeup(Error **errp)
193{
194 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
195}
196
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600197ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600198{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600199 Object *obj;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600200 bool ambiguous = false;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600201 ObjectPropertyInfoList *props = NULL;
202 ObjectProperty *prop;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600203
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600204 obj = object_resolve_path(path, &ambiguous);
205 if (obj == NULL) {
Michael Tokarev79772082014-05-04 12:23:59 +0400206 if (ambiguous) {
207 error_setg(errp, "Path '%s' is ambiguous", path);
208 } else {
209 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
210 }
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600211 return NULL;
212 }
213
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600214 QTAILQ_FOREACH(prop, &obj->properties, node) {
215 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600216
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600217 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600218 entry->next = props;
219 props = entry;
220
221 entry->value->name = g_strdup(prop->name);
222 entry->value->type = g_strdup(prop->type);
223 }
224
225 return props;
226}
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600227
228/* FIXME: teach qapi about how to pass through Visitors */
229int qmp_qom_set(Monitor *mon, const QDict *qdict, QObject **ret)
230{
231 const char *path = qdict_get_str(qdict, "path");
232 const char *property = qdict_get_str(qdict, "property");
233 QObject *value = qdict_get(qdict, "value");
234 Error *local_err = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600235 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600236
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600237 obj = object_resolve_path(path, NULL);
238 if (!obj) {
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600239 error_set(&local_err, QERR_DEVICE_NOT_FOUND, path);
240 goto out;
241 }
242
Paolo Bonzini9f5f1352012-02-01 16:58:47 +0100243 object_property_set_qobject(obj, value, property, &local_err);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600244
245out:
246 if (local_err) {
247 qerror_report_err(local_err);
248 error_free(local_err);
249 return -1;
250 }
251
252 return 0;
253}
254
255int qmp_qom_get(Monitor *mon, const QDict *qdict, QObject **ret)
256{
257 const char *path = qdict_get_str(qdict, "path");
258 const char *property = qdict_get_str(qdict, "property");
259 Error *local_err = NULL;
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) {
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600264 error_set(&local_err, QERR_DEVICE_NOT_FOUND, path);
265 goto out;
266 }
267
Paolo Bonzini9f5f1352012-02-01 16:58:47 +0100268 *ret = object_property_get_qobject(obj, property, &local_err);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600269
270out:
271 if (local_err) {
272 qerror_report_err(local_err);
273 error_free(local_err);
274 return -1;
275 }
276
277 return 0;
278}
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200279
280void qmp_set_password(const char *protocol, const char *password,
281 bool has_connected, const char *connected, Error **errp)
282{
283 int disconnect_if_connected = 0;
284 int fail_if_connected = 0;
285 int rc;
286
287 if (has_connected) {
288 if (strcmp(connected, "fail") == 0) {
289 fail_if_connected = 1;
290 } else if (strcmp(connected, "disconnect") == 0) {
291 disconnect_if_connected = 1;
292 } else if (strcmp(connected, "keep") == 0) {
293 /* nothing */
294 } else {
295 error_set(errp, QERR_INVALID_PARAMETER, "connected");
296 return;
297 }
298 }
299
300 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100301 if (!qemu_using_spice(errp)) {
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200302 return;
303 }
304 rc = qemu_spice_set_passwd(password, fail_if_connected,
305 disconnect_if_connected);
306 if (rc != 0) {
307 error_set(errp, QERR_SET_PASSWD_FAILED);
308 }
309 return;
310 }
311
312 if (strcmp(protocol, "vnc") == 0) {
313 if (fail_if_connected || disconnect_if_connected) {
314 /* vnc supports "connected=keep" only */
315 error_set(errp, QERR_INVALID_PARAMETER, "connected");
316 return;
317 }
318 /* Note that setting an empty password will not disable login through
319 * this interface. */
320 rc = vnc_display_password(NULL, password);
321 if (rc < 0) {
322 error_set(errp, QERR_SET_PASSWD_FAILED);
323 }
324 return;
325 }
326
327 error_set(errp, QERR_INVALID_PARAMETER, "protocol");
328}
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200329
330void qmp_expire_password(const char *protocol, const char *whenstr,
331 Error **errp)
332{
333 time_t when;
334 int rc;
335
336 if (strcmp(whenstr, "now") == 0) {
337 when = 0;
338 } else if (strcmp(whenstr, "never") == 0) {
339 when = TIME_MAX;
340 } else if (whenstr[0] == '+') {
341 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
342 } else {
343 when = strtoull(whenstr, NULL, 10);
344 }
345
346 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100347 if (!qemu_using_spice(errp)) {
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200348 return;
349 }
350 rc = qemu_spice_set_pw_expire(when);
351 if (rc != 0) {
352 error_set(errp, QERR_SET_PASSWD_FAILED);
353 }
354 return;
355 }
356
357 if (strcmp(protocol, "vnc") == 0) {
358 rc = vnc_display_pw_expire(NULL, when);
359 if (rc != 0) {
360 error_set(errp, QERR_SET_PASSWD_FAILED);
361 }
362 return;
363 }
364
365 error_set(errp, QERR_INVALID_PARAMETER, "protocol");
366}
Luiz Capitulino270b2432011-12-08 11:45:55 -0200367
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200368#ifdef CONFIG_VNC
Luiz Capitulino270b2432011-12-08 11:45:55 -0200369void qmp_change_vnc_password(const char *password, Error **errp)
370{
371 if (vnc_display_password(NULL, password) < 0) {
372 error_set(errp, QERR_SET_PASSWD_FAILED);
373 }
374}
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200375
Paolo Bonzini007fcd3e2012-10-18 09:01:01 +0200376static void qmp_change_vnc_listen(const char *target, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200377{
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200378 QemuOptsList *olist = qemu_find_opts("vnc");
379 QemuOpts *opts;
380
381 if (strstr(target, "id=")) {
382 error_setg(errp, "id not supported");
383 return;
384 }
385
386 opts = qemu_opts_find(olist, "default");
387 if (opts) {
388 qemu_opts_del(opts);
389 }
390 opts = vnc_parse_func(target);
Gongleif7801c52015-02-05 17:43:35 +0800391 if (!opts) {
392 return;
393 }
394
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200395 vnc_display_open("default", errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200396}
397
398static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
399 Error **errp)
400{
401 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
402 if (!has_arg) {
403 error_set(errp, QERR_MISSING_PARAMETER, "password");
404 } else {
405 qmp_change_vnc_password(arg, errp);
406 }
407 } else {
408 qmp_change_vnc_listen(target, errp);
409 }
410}
411#else
412void qmp_change_vnc_password(const char *password, Error **errp)
413{
414 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
415}
416static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
417 Error **errp)
418{
419 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
420}
421#endif /* !CONFIG_VNC */
422
423void qmp_change(const char *device, const char *target,
Markus Armbruster7daecb32014-05-02 13:26:31 +0200424 bool has_arg, const char *arg, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200425{
426 if (strcmp(device, "vnc") == 0) {
Markus Armbruster7daecb32014-05-02 13:26:31 +0200427 qmp_change_vnc(target, has_arg, arg, errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200428 } else {
Markus Armbruster7daecb32014-05-02 13:26:31 +0200429 qmp_change_blockdev(device, target, arg, errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200430 }
431}
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600432
433static void qom_list_types_tramp(ObjectClass *klass, void *data)
434{
435 ObjectTypeInfoList *e, **pret = data;
436 ObjectTypeInfo *info;
437
438 info = g_malloc0(sizeof(*info));
439 info->name = g_strdup(object_class_get_name(klass));
440
441 e = g_malloc0(sizeof(*e));
442 e->value = info;
443 e->next = *pret;
444 *pret = e;
445}
446
447ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
448 const char *implements,
449 bool has_abstract,
450 bool abstract,
451 Error **errp)
452{
453 ObjectTypeInfoList *ret = NULL;
454
455 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
456
457 return ret;
458}
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500459
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200460/* Return a DevicePropertyInfo for a qdev property.
461 *
462 * If a qdev property with the given name does not exist, use the given default
463 * type. If the qdev property info should not be shown, return NULL.
464 *
465 * The caller must free the return value.
466 */
467static DevicePropertyInfo *make_device_property_info(ObjectClass *klass,
468 const char *name,
Gonglei07d09c52014-10-07 14:33:23 +0800469 const char *default_type,
470 const char *description)
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200471{
472 DevicePropertyInfo *info;
473 Property *prop;
474
475 do {
476 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
477 if (strcmp(name, prop->name) != 0) {
478 continue;
479 }
480
481 /*
482 * TODO Properties without a parser are just for dirty hacks.
483 * qdev_prop_ptr is the only such PropertyInfo. It's marked
484 * for removal. This conditional should be removed along with
485 * it.
486 */
487 if (!prop->info->set) {
488 return NULL; /* no way to set it, don't show */
489 }
490
491 info = g_malloc0(sizeof(*info));
492 info->name = g_strdup(prop->name);
Gonglei07d09c52014-10-07 14:33:23 +0800493 info->type = g_strdup(prop->info->name);
494 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;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500517 DevicePropertyInfoList *prop_list = NULL;
518
519 klass = object_class_by_name(typename);
520 if (klass == NULL) {
521 error_set(errp, QERR_DEVICE_NOT_FOUND, typename);
522 return NULL;
523 }
524
525 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
526 if (klass == NULL) {
527 error_set(errp, QERR_INVALID_PARAMETER_VALUE,
528 "name", TYPE_DEVICE);
529 return NULL;
530 }
531
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200532 obj = object_new(typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500533
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200534 QTAILQ_FOREACH(prop, &obj->properties, node) {
535 DevicePropertyInfo *info;
536 DevicePropertyInfoList *entry;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500537
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200538 /* Skip Object and DeviceState properties */
539 if (strcmp(prop->name, "type") == 0 ||
540 strcmp(prop->name, "realized") == 0 ||
541 strcmp(prop->name, "hotpluggable") == 0 ||
Stefan Hajnoczi4115dd62014-07-09 14:01:31 +0200542 strcmp(prop->name, "hotplugged") == 0 ||
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200543 strcmp(prop->name, "parent_bus") == 0) {
544 continue;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500545 }
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200546
547 /* Skip legacy properties since they are just string versions of
548 * properties that we already list.
549 */
550 if (strstart(prop->name, "legacy-", NULL)) {
551 continue;
552 }
553
Gonglei07d09c52014-10-07 14:33:23 +0800554 info = make_device_property_info(klass, prop->name, prop->type,
555 prop->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200556 if (!info) {
557 continue;
558 }
559
560 entry = g_malloc0(sizeof(*entry));
561 entry->value = info;
562 entry->next = prop_list;
563 prop_list = entry;
564 }
565
566 object_unref(obj);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500567
568 return prop_list;
569}
Anthony Liguorie4e31c62012-08-10 11:04:13 -0500570
Anthony Liguori76b64a72012-08-14 22:17:36 -0500571CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
572{
573 return arch_query_cpu_definitions(errp);
574}
575
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300576void qmp_add_client(const char *protocol, const char *fdname,
577 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
578 Error **errp)
579{
580 CharDriverState *s;
581 int fd;
582
583 fd = monitor_get_fd(cur_mon, fdname, errp);
584 if (fd < 0) {
585 return;
586 }
587
588 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100589 if (!qemu_using_spice(errp)) {
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300590 close(fd);
591 return;
592 }
593 skipauth = has_skipauth ? skipauth : false;
594 tls = has_tls ? tls : false;
595 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
596 error_setg(errp, "spice failed to add client");
597 close(fd);
598 }
599 return;
600#ifdef CONFIG_VNC
601 } else if (strcmp(protocol, "vnc") == 0) {
602 skipauth = has_skipauth ? skipauth : false;
603 vnc_display_add_client(NULL, fd, skipauth);
604 return;
605#endif
606 } else if ((s = qemu_chr_find(protocol)) != NULL) {
607 if (qemu_chr_add_client(s, fd) < 0) {
608 error_setg(errp, "failed to add client");
609 close(fd);
610 return;
611 }
612 return;
613 }
614
615 error_setg(errp, "protocol '%s' is invalid", protocol);
616 close(fd);
617}
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100618
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100619void object_add(const char *type, const char *id, const QDict *qdict,
620 Visitor *v, Error **errp)
621{
622 Object *obj;
Eduardo Habkostc3481242014-04-16 14:39:38 -0300623 ObjectClass *klass;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100624 const QDictEntry *e;
625 Error *local_err = NULL;
626
Eduardo Habkostc3481242014-04-16 14:39:38 -0300627 klass = object_class_by_name(type);
628 if (!klass) {
Paolo Bonzinid1169462014-05-14 17:43:13 +0800629 error_setg(errp, "invalid object type: %s", type);
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100630 return;
631 }
632
Eduardo Habkostc3481242014-04-16 14:39:38 -0300633 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
634 error_setg(errp, "object type '%s' isn't supported by object-add",
635 type);
636 return;
637 }
638
639 if (object_class_is_abstract(klass)) {
640 error_setg(errp, "object type '%s' is abstract", type);
641 return;
642 }
643
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100644 obj = object_new(type);
645 if (qdict) {
646 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
647 object_property_set(obj, v, e->key, &local_err);
648 if (local_err) {
Igor Mammedov69252c02014-01-16 17:34:36 +0100649 goto out;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100650 }
651 }
652 }
653
Igor Mammedova790f4e2014-06-02 15:24:59 +0200654 object_property_add_child(container_get(object_get_root(), "/objects"),
655 id, obj, &local_err);
Igor Mammedov269e09f2014-01-16 17:34:38 +0100656 if (local_err) {
657 goto out;
658 }
659
Igor Mammedova790f4e2014-06-02 15:24:59 +0200660 user_creatable_complete(obj, &local_err);
661 if (local_err) {
662 object_property_del(container_get(object_get_root(), "/objects"),
663 id, &error_abort);
664 goto out;
665 }
Igor Mammedov69252c02014-01-16 17:34:36 +0100666out:
667 if (local_err) {
668 error_propagate(errp, local_err);
669 }
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100670 object_unref(obj);
671}
672
673int qmp_object_add(Monitor *mon, const QDict *qdict, QObject **ret)
674{
675 const char *type = qdict_get_str(qdict, "qom-type");
676 const char *id = qdict_get_str(qdict, "id");
677 QObject *props = qdict_get(qdict, "props");
678 const QDict *pdict = NULL;
679 Error *local_err = NULL;
680 QmpInputVisitor *qiv;
681
682 if (props) {
683 pdict = qobject_to_qdict(props);
684 if (!pdict) {
685 error_set(&local_err, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
686 goto out;
687 }
688 }
689
690 qiv = qmp_input_visitor_new(props);
691 object_add(type, id, pdict, qmp_input_get_visitor(qiv), &local_err);
692 qmp_input_visitor_cleanup(qiv);
693
694out:
695 if (local_err) {
696 qerror_report_err(local_err);
697 error_free(local_err);
698 return -1;
699 }
700
701 return 0;
702}
703
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100704void qmp_object_del(const char *id, Error **errp)
705{
706 Object *container;
707 Object *obj;
708
709 container = container_get(object_get_root(), "/objects");
710 obj = object_resolve_path_component(container, id);
711 if (!obj) {
712 error_setg(errp, "object id not found");
713 return;
714 }
Lin Mad6edb152015-03-30 16:36:28 +0800715
716 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj), errp)) {
717 error_setg(errp, "%s is in use, can not be deleted", id);
718 return;
719 }
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100720 object_unparent(obj);
721}
Igor Mammedov6f2e2732014-06-16 19:12:25 +0200722
723MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
724{
725 MemoryDeviceInfoList *head = NULL;
726 MemoryDeviceInfoList **prev = &head;
727
728 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
729
730 return head;
731}
Igor Mammedov02419bc2014-06-16 19:12:28 +0200732
733ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
734{
735 bool ambig;
736 ACPIOSTInfoList *head = NULL;
737 ACPIOSTInfoList **prev = &head;
738 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
739
740 if (obj) {
741 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
742 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
743
744 adevc->ospm_status(adev, &prev);
745 } else {
746 error_setg(errp, "command is not supported, missing ACPI device");
747 }
748
749 return head;
750}