2 * Dynamic device configuration and creation.
4 * Copyright (c) 2009 CodeSourcery
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "hw/sysbus.h"
22 #include "monitor/hmp.h"
23 #include "monitor/monitor.h"
24 #include "monitor/qdev.h"
25 #include "sysemu/arch_init.h"
26 #include "qapi/error.h"
27 #include "qapi/qapi-commands-qdev.h"
28 #include "qapi/qmp/qdict.h"
29 #include "qapi/qmp/qerror.h"
30 #include "qemu/config-file.h"
31 #include "qemu/error-report.h"
32 #include "qemu/help_option.h"
33 #include "qemu/option.h"
34 #include "qemu/qemu-print.h"
35 #include "qemu/option_int.h"
36 #include "sysemu/block-backend.h"
37 #include "sysemu/sysemu.h"
38 #include "migration/misc.h"
39 #include "migration/migration.h"
40 #include "qemu/cutils.h"
41 #include "hw/qdev-properties.h"
45 * Aliases were a bad idea from the start. Let's keep them
46 * from spreading further.
48 typedef struct QDevAlias
55 /* Please keep this table sorted by typename. */
56 static const QDevAlias qdev_alias_table
[] = {
57 { "AC97", "ac97" }, /* -soundhw name */
58 { "e1000", "e1000-82540em" },
59 { "ES1370", "es1370" }, /* -soundhw name */
60 { "ich9-ahci", "ahci" },
61 { "lsi53c895a", "lsi" },
62 { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_S390X
},
63 { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
64 { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_S390X
},
65 { "virtio-balloon-pci", "virtio-balloon",
66 QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
67 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_S390X
},
68 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
69 { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_S390X
},
70 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
71 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_S390X
},
72 { "virtio-input-host-pci", "virtio-input-host",
73 QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
74 { "virtio-iommu-pci", "virtio-iommu", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
75 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_S390X
},
76 { "virtio-keyboard-pci", "virtio-keyboard",
77 QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
78 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_S390X
},
79 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
80 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_S390X
},
81 { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
82 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_S390X
},
83 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
84 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_S390X
},
85 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
86 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_S390X
},
87 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
88 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_S390X
},
89 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
93 static const char *qdev_class_get_alias(DeviceClass
*dc
)
95 const char *typename
= object_class_get_name(OBJECT_CLASS(dc
));
98 for (i
= 0; qdev_alias_table
[i
].typename
; i
++) {
99 if (qdev_alias_table
[i
].arch_mask
&&
100 !(qdev_alias_table
[i
].arch_mask
& arch_type
)) {
104 if (strcmp(qdev_alias_table
[i
].typename
, typename
) == 0) {
105 return qdev_alias_table
[i
].alias
;
112 static bool qdev_class_has_alias(DeviceClass
*dc
)
114 return (qdev_class_get_alias(dc
) != NULL
);
117 static void qdev_print_devinfo(DeviceClass
*dc
)
119 qemu_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc
)));
121 qemu_printf(", bus %s", dc
->bus_type
);
123 if (qdev_class_has_alias(dc
)) {
124 qemu_printf(", alias \"%s\"", qdev_class_get_alias(dc
));
127 qemu_printf(", desc \"%s\"", dc
->desc
);
129 if (!dc
->user_creatable
) {
130 qemu_printf(", no-user");
135 static void qdev_print_devinfos(bool show_no_user
)
137 static const char *cat_name
[DEVICE_CATEGORY_MAX
+ 1] = {
138 [DEVICE_CATEGORY_BRIDGE
] = "Controller/Bridge/Hub",
139 [DEVICE_CATEGORY_USB
] = "USB",
140 [DEVICE_CATEGORY_STORAGE
] = "Storage",
141 [DEVICE_CATEGORY_NETWORK
] = "Network",
142 [DEVICE_CATEGORY_INPUT
] = "Input",
143 [DEVICE_CATEGORY_DISPLAY
] = "Display",
144 [DEVICE_CATEGORY_SOUND
] = "Sound",
145 [DEVICE_CATEGORY_MISC
] = "Misc",
146 [DEVICE_CATEGORY_CPU
] = "CPU",
147 [DEVICE_CATEGORY_MAX
] = "Uncategorized",
153 module_load_qom_all();
154 list
= object_class_get_list_sorted(TYPE_DEVICE
, false);
156 for (i
= 0; i
<= DEVICE_CATEGORY_MAX
; i
++) {
158 for (elt
= list
; elt
; elt
= elt
->next
) {
159 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
161 if ((i
< DEVICE_CATEGORY_MAX
162 ? !test_bit(i
, dc
->categories
)
163 : !bitmap_empty(dc
->categories
, DEVICE_CATEGORY_MAX
))
165 && !dc
->user_creatable
)) {
169 qemu_printf("%s%s devices:\n", i
? "\n" : "", cat_name
[i
]);
172 qdev_print_devinfo(dc
);
179 static int set_property(void *opaque
, const char *name
, const char *value
,
182 Object
*obj
= opaque
;
184 if (strcmp(name
, "driver") == 0)
186 if (strcmp(name
, "bus") == 0)
189 if (!object_property_parse(obj
, name
, value
, errp
)) {
195 static const char *find_typename_by_alias(const char *alias
)
199 for (i
= 0; qdev_alias_table
[i
].alias
; i
++) {
200 if (qdev_alias_table
[i
].arch_mask
&&
201 !(qdev_alias_table
[i
].arch_mask
& arch_type
)) {
205 if (strcmp(qdev_alias_table
[i
].alias
, alias
) == 0) {
206 return qdev_alias_table
[i
].typename
;
213 static DeviceClass
*qdev_get_device_class(const char **driver
, Error
**errp
)
217 const char *original_name
= *driver
;
219 oc
= module_object_class_by_name(*driver
);
221 const char *typename
= find_typename_by_alias(*driver
);
225 oc
= module_object_class_by_name(*driver
);
229 if (!object_class_dynamic_cast(oc
, TYPE_DEVICE
)) {
230 if (*driver
!= original_name
) {
231 error_setg(errp
, "'%s' (alias '%s') is not a valid device model"
232 " name", original_name
, *driver
);
234 error_setg(errp
, "'%s' is not a valid device model name", *driver
);
239 if (object_class_is_abstract(oc
)) {
240 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
241 "a non-abstract device type");
245 dc
= DEVICE_CLASS(oc
);
246 if (!dc
->user_creatable
||
247 (qdev_hotplug
&& !dc
->hotpluggable
)) {
248 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
249 "a pluggable device type");
257 int qdev_device_help(QemuOpts
*opts
)
259 Error
*local_err
= NULL
;
261 ObjectPropertyInfoList
*prop_list
;
262 ObjectPropertyInfoList
*prop
;
266 driver
= qemu_opt_get(opts
, "driver");
267 if (driver
&& is_help_option(driver
)) {
268 qdev_print_devinfos(false);
272 if (!driver
|| !qemu_opt_has_help_opt(opts
)) {
276 if (!object_class_by_name(driver
)) {
277 const char *typename
= find_typename_by_alias(driver
);
284 prop_list
= qmp_device_list_properties(driver
, &local_err
);
290 qemu_printf("%s options:\n", driver
);
292 qemu_printf("There are no options for %s.\n", driver
);
294 array
= g_ptr_array_new();
295 for (prop
= prop_list
; prop
; prop
= prop
->next
) {
296 g_ptr_array_add(array
,
297 object_property_help(prop
->value
->name
,
299 prop
->value
->default_value
,
300 prop
->value
->description
));
302 g_ptr_array_sort(array
, (GCompareFunc
)qemu_pstrcmp0
);
303 for (i
= 0; i
< array
->len
; i
++) {
304 qemu_printf("%s\n", (char *)array
->pdata
[i
]);
306 g_ptr_array_set_free_func(array
, g_free
);
307 g_ptr_array_free(array
, true);
308 qapi_free_ObjectPropertyInfoList(prop_list
);
312 error_report_err(local_err
);
316 static Object
*qdev_get_peripheral(void)
321 dev
= container_get(qdev_get_machine(), "/peripheral");
327 static Object
*qdev_get_peripheral_anon(void)
332 dev
= container_get(qdev_get_machine(), "/peripheral-anon");
338 static void qbus_error_append_bus_list_hint(DeviceState
*dev
,
342 const char *sep
= " ";
344 error_append_hint(errp
, "child buses at \"%s\":",
345 dev
->id
? dev
->id
: object_get_typename(OBJECT(dev
)));
346 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
347 error_append_hint(errp
, "%s\"%s\"", sep
, child
->name
);
350 error_append_hint(errp
, "\n");
353 static void qbus_error_append_dev_list_hint(BusState
*bus
,
357 const char *sep
= " ";
359 error_append_hint(errp
, "devices at \"%s\":", bus
->name
);
360 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
361 DeviceState
*dev
= kid
->child
;
362 error_append_hint(errp
, "%s\"%s\"", sep
,
363 object_get_typename(OBJECT(dev
)));
365 error_append_hint(errp
, "/\"%s\"", dev
->id
);
369 error_append_hint(errp
, "\n");
372 static BusState
*qbus_find_bus(DeviceState
*dev
, char *elem
)
376 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
377 if (strcmp(child
->name
, elem
) == 0) {
384 static DeviceState
*qbus_find_dev(BusState
*bus
, char *elem
)
389 * try to match in order:
390 * (1) instance id, if present
392 * (3) driver alias, if present
394 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
395 DeviceState
*dev
= kid
->child
;
396 if (dev
->id
&& strcmp(dev
->id
, elem
) == 0) {
400 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
401 DeviceState
*dev
= kid
->child
;
402 if (strcmp(object_get_typename(OBJECT(dev
)), elem
) == 0) {
406 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
407 DeviceState
*dev
= kid
->child
;
408 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
410 if (qdev_class_has_alias(dc
) &&
411 strcmp(qdev_class_get_alias(dc
), elem
) == 0) {
418 static inline bool qbus_is_full(BusState
*bus
)
420 BusClass
*bus_class
= BUS_GET_CLASS(bus
);
421 return bus_class
->max_dev
&& bus
->num_children
>= bus_class
->max_dev
;
425 * Search the tree rooted at @bus for a bus.
426 * If @name, search for a bus with that name. Note that bus names
427 * need not be unique. Yes, that's screwed up.
428 * Else search for a bus that is a subtype of @bus_typename.
429 * If more than one exists, prefer one that can take another device.
430 * Return the bus if found, else %NULL.
432 static BusState
*qbus_find_recursive(BusState
*bus
, const char *name
,
433 const char *bus_typename
)
436 BusState
*pick
, *child
, *ret
;
439 assert(name
|| bus_typename
);
441 match
= !strcmp(bus
->name
, name
);
443 match
= !!object_dynamic_cast(OBJECT(bus
), bus_typename
);
446 if (match
&& !qbus_is_full(bus
)) {
447 return bus
; /* root matches and isn't full */
450 pick
= match
? bus
: NULL
;
452 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
453 DeviceState
*dev
= kid
->child
;
454 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
455 ret
= qbus_find_recursive(child
, name
, bus_typename
);
456 if (ret
&& !qbus_is_full(ret
)) {
457 return ret
; /* a descendant matches and isn't full */
465 /* root or a descendant matches, but is full */
469 static BusState
*qbus_find(const char *path
, Error
**errp
)
476 /* find start element */
477 if (path
[0] == '/') {
478 bus
= sysbus_get_default();
481 if (sscanf(path
, "%127[^/]%n", elem
, &len
) != 1) {
485 bus
= qbus_find_recursive(sysbus_get_default(), elem
, NULL
);
487 error_setg(errp
, "Bus '%s' not found", elem
);
494 assert(path
[pos
] == '/' || !path
[pos
]);
495 while (path
[pos
] == '/') {
498 if (path
[pos
] == '\0') {
503 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
504 g_assert_not_reached();
508 dev
= qbus_find_dev(bus
, elem
);
510 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
511 "Device '%s' not found", elem
);
512 qbus_error_append_dev_list_hint(bus
, errp
);
516 assert(path
[pos
] == '/' || !path
[pos
]);
517 while (path
[pos
] == '/') {
520 if (path
[pos
] == '\0') {
521 /* last specified element is a device. If it has exactly
522 * one child bus accept it nevertheless */
523 if (dev
->num_child_bus
== 1) {
524 bus
= QLIST_FIRST(&dev
->child_bus
);
527 if (dev
->num_child_bus
) {
528 error_setg(errp
, "Device '%s' has multiple child buses",
530 qbus_error_append_bus_list_hint(dev
, errp
);
532 error_setg(errp
, "Device '%s' has no child bus", elem
);
538 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
539 g_assert_not_reached();
543 bus
= qbus_find_bus(dev
, elem
);
545 error_setg(errp
, "Bus '%s' not found", elem
);
546 qbus_error_append_bus_list_hint(dev
, errp
);
551 if (qbus_is_full(bus
)) {
552 error_setg(errp
, "Bus '%s' is full", path
);
558 void qdev_set_id(DeviceState
*dev
, const char *id
)
565 object_property_add_child(qdev_get_peripheral(), dev
->id
,
568 static int anon_count
;
569 gchar
*name
= g_strdup_printf("device[%d]", anon_count
++);
570 object_property_add_child(qdev_get_peripheral_anon(), name
,
576 DeviceState
*qdev_device_add(QemuOpts
*opts
, Error
**errp
)
579 const char *driver
, *path
;
580 DeviceState
*dev
= NULL
;
581 BusState
*bus
= NULL
;
583 driver
= qemu_opt_get(opts
, "driver");
585 error_setg(errp
, QERR_MISSING_PARAMETER
, "driver");
590 dc
= qdev_get_device_class(&driver
, errp
);
596 path
= qemu_opt_get(opts
, "bus");
598 bus
= qbus_find(path
, errp
);
602 if (!object_dynamic_cast(OBJECT(bus
), dc
->bus_type
)) {
603 error_setg(errp
, "Device '%s' can't go on %s bus",
604 driver
, object_get_typename(OBJECT(bus
)));
607 } else if (dc
->bus_type
!= NULL
) {
608 bus
= qbus_find_recursive(sysbus_get_default(), NULL
, dc
->bus_type
);
609 if (!bus
|| qbus_is_full(bus
)) {
610 error_setg(errp
, "No '%s' bus found for device '%s'",
611 dc
->bus_type
, driver
);
616 if (qemu_opt_get(opts
, "failover_pair_id")) {
618 error_setg(errp
, "Device with failover_pair_id don't have id");
621 if (qdev_should_hide_device(opts
)) {
622 if (bus
&& !qbus_is_hotpluggable(bus
)) {
623 error_setg(errp
, QERR_BUS_NO_HOTPLUG
, bus
->name
);
629 if (qdev_hotplug
&& bus
&& !qbus_is_hotpluggable(bus
)) {
630 error_setg(errp
, QERR_BUS_NO_HOTPLUG
, bus
->name
);
634 if (!migration_is_idle()) {
635 error_setg(errp
, "device_add not allowed while migrating");
640 dev
= qdev_new(driver
);
642 /* Check whether the hotplug is allowed by the machine */
643 if (qdev_hotplug
&& !qdev_hotplug_allowed(dev
, errp
)) {
647 if (!bus
&& qdev_hotplug
&& !qdev_get_machine_hotplug_handler(dev
)) {
648 /* No bus, no machine hotplug handler --> device is not hotpluggable */
649 error_setg(errp
, "Device '%s' can not be hotplugged on this machine",
654 qdev_set_id(dev
, qemu_opts_id(opts
));
657 if (qemu_opt_foreach(opts
, set_property
, dev
, errp
)) {
662 if (!qdev_realize(DEVICE(dev
), bus
, errp
)) {
670 object_unparent(OBJECT(dev
));
671 object_unref(OBJECT(dev
));
677 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
678 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
);
680 static void qdev_print_props(Monitor
*mon
, DeviceState
*dev
, Property
*props
,
685 for (; props
->name
; props
++) {
687 char *legacy_name
= g_strdup_printf("legacy-%s", props
->name
);
689 if (object_property_get_type(OBJECT(dev
), legacy_name
, NULL
)) {
690 value
= object_property_get_str(OBJECT(dev
), legacy_name
, NULL
);
692 value
= object_property_print(OBJECT(dev
), props
->name
, true,
700 qdev_printf("%s = %s\n", props
->name
,
701 *value
? value
: "<null>");
706 static void bus_print_dev(BusState
*bus
, Monitor
*mon
, DeviceState
*dev
, int indent
)
708 BusClass
*bc
= BUS_GET_CLASS(bus
);
711 bc
->print_dev(mon
, dev
, indent
);
715 static void qdev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
722 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev
)),
723 dev
->id
? dev
->id
: "");
725 QLIST_FOREACH(ngl
, &dev
->gpios
, node
) {
727 qdev_printf("gpio-in \"%s\" %d\n", ngl
->name
? ngl
->name
: "",
731 qdev_printf("gpio-out \"%s\" %d\n", ngl
->name
? ngl
->name
: "",
735 QLIST_FOREACH(ncl
, &dev
->clocks
, node
) {
736 qdev_printf("clock-%s%s \"%s\" freq_hz=%e\n",
737 ncl
->output
? "out" : "in",
738 ncl
->alias
? " (alias)" : "",
740 CLOCK_PERIOD_TO_HZ(1.0 * clock_get(ncl
->clock
)));
742 class = object_get_class(OBJECT(dev
));
744 qdev_print_props(mon
, dev
, DEVICE_CLASS(class)->props_
, indent
);
745 class = object_class_get_parent(class);
746 } while (class != object_class_by_name(TYPE_DEVICE
));
747 bus_print_dev(dev
->parent_bus
, mon
, dev
, indent
);
748 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
749 qbus_print(mon
, child
, indent
);
753 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
)
757 qdev_printf("bus: %s\n", bus
->name
);
759 qdev_printf("type %s\n", object_get_typename(OBJECT(bus
)));
760 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
761 DeviceState
*dev
= kid
->child
;
762 qdev_print(mon
, dev
, indent
);
767 void hmp_info_qtree(Monitor
*mon
, const QDict
*qdict
)
769 if (sysbus_get_default())
770 qbus_print(mon
, sysbus_get_default(), 0);
773 void hmp_info_qdm(Monitor
*mon
, const QDict
*qdict
)
775 qdev_print_devinfos(true);
778 void qmp_device_add(QDict
*qdict
, QObject
**ret_data
, Error
**errp
)
783 opts
= qemu_opts_from_qdict(qemu_find_opts("device"), qdict
, errp
);
787 if (!monitor_cur_is_qmp() && qdev_device_help(opts
)) {
791 dev
= qdev_device_add(opts
, errp
);
794 * Drain all pending RCU callbacks. This is done because
795 * some bus related operations can delay a device removal
796 * (in this case this can happen if device is added and then
797 * removed due to a configuration error)
798 * to a RCU callback, but user might expect that this interface
799 * will finish its job completely once qmp command returns result
808 object_unref(OBJECT(dev
));
811 static DeviceState
*find_device_state(const char *id
, Error
**errp
)
816 obj
= object_resolve_path(id
, NULL
);
818 char *root_path
= object_get_canonical_path(qdev_get_peripheral());
819 char *path
= g_strdup_printf("%s/%s", root_path
, id
);
822 obj
= object_resolve_path_type(path
, TYPE_DEVICE
, NULL
);
827 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
828 "Device '%s' not found", id
);
832 if (!object_dynamic_cast(obj
, TYPE_DEVICE
)) {
833 error_setg(errp
, "%s is not a hotpluggable device", id
);
840 void qdev_unplug(DeviceState
*dev
, Error
**errp
)
842 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
843 HotplugHandler
*hotplug_ctrl
;
844 HotplugHandlerClass
*hdc
;
845 Error
*local_err
= NULL
;
847 if (dev
->parent_bus
&& !qbus_is_hotpluggable(dev
->parent_bus
)) {
848 error_setg(errp
, QERR_BUS_NO_HOTPLUG
, dev
->parent_bus
->name
);
852 if (!dc
->hotpluggable
) {
853 error_setg(errp
, QERR_DEVICE_NO_HOTPLUG
,
854 object_get_typename(OBJECT(dev
)));
858 if (!migration_is_idle() && !dev
->allow_unplug_during_migration
) {
859 error_setg(errp
, "device_del not allowed while migrating");
863 qdev_hot_removed
= true;
865 hotplug_ctrl
= qdev_get_hotplug_handler(dev
);
866 /* hotpluggable device MUST have HotplugHandler, if it doesn't
867 * then something is very wrong with it */
868 g_assert(hotplug_ctrl
);
870 /* If device supports async unplug just request it to be done,
871 * otherwise just remove it synchronously */
872 hdc
= HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl
);
873 if (hdc
->unplug_request
) {
874 hotplug_handler_unplug_request(hotplug_ctrl
, dev
, &local_err
);
876 hotplug_handler_unplug(hotplug_ctrl
, dev
, &local_err
);
878 object_unparent(OBJECT(dev
));
881 error_propagate(errp
, local_err
);
884 void qmp_device_del(const char *id
, Error
**errp
)
886 DeviceState
*dev
= find_device_state(id
, errp
);
888 if (dev
->pending_deleted_event
) {
889 error_setg(errp
, "Device %s is already in the "
890 "process of unplug", id
);
894 qdev_unplug(dev
, errp
);
898 void hmp_device_add(Monitor
*mon
, const QDict
*qdict
)
902 qmp_device_add((QDict
*)qdict
, NULL
, &err
);
903 hmp_handle_error(mon
, err
);
906 void hmp_device_del(Monitor
*mon
, const QDict
*qdict
)
908 const char *id
= qdict_get_str(qdict
, "id");
911 qmp_device_del(id
, &err
);
912 hmp_handle_error(mon
, err
);
915 BlockBackend
*blk_by_qdev_id(const char *id
, Error
**errp
)
920 dev
= find_device_state(id
, errp
);
925 blk
= blk_by_dev(dev
);
927 error_setg(errp
, "Device does not have a block device backend");
932 QemuOptsList qemu_device_opts
= {
934 .implied_opt_name
= "driver",
935 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
938 * no elements => accept any
939 * sanity checking will happen later
940 * when setting device properties
942 { /* end of list */ }
946 QemuOptsList qemu_global_opts
= {
948 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
952 .type
= QEMU_OPT_STRING
,
955 .type
= QEMU_OPT_STRING
,
958 .type
= QEMU_OPT_STRING
,
960 { /* end of list */ }
964 int qemu_global_option(const char *str
)
966 char driver
[64], property
[64];
970 rc
= sscanf(str
, "%63[^.=].%63[^=]%n", driver
, property
, &offset
);
971 if (rc
== 2 && str
[offset
] == '=') {
972 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0, &error_abort
);
973 qemu_opt_set(opts
, "driver", driver
, &error_abort
);
974 qemu_opt_set(opts
, "property", property
, &error_abort
);
975 qemu_opt_set(opts
, "value", str
+ offset
+ 1, &error_abort
);
979 opts
= qemu_opts_parse_noisily(&qemu_global_opts
, str
, false);