xref: /qemu/tests/qtest/device-plug-test.c (revision 336d354b)
1 /*
2  * QEMU device plug/unplug handling
3  *
4  * Copyright (C) 2019 Red Hat Inc.
5  *
6  * Authors:
7  *  David Hildenbrand <david@redhat.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or later.
10  * See the COPYING file in the top-level directory.
11  */
12 
13 #include "qemu/osdep.h"
14 #include "libqos/libqtest.h"
15 #include "qapi/qmp/qdict.h"
16 #include "qapi/qmp/qstring.h"
17 
18 static void device_del(QTestState *qtest, const char *id)
19 {
20     QDict *resp;
21 
22     resp = qtest_qmp(qtest,
23                      "{'execute': 'device_del', 'arguments': { 'id': %s } }", id);
24 
25     g_assert(qdict_haskey(resp, "return"));
26     qobject_unref(resp);
27 }
28 
29 static void system_reset(QTestState *qtest)
30 {
31     QDict *resp;
32 
33     resp = qtest_qmp(qtest, "{'execute': 'system_reset'}");
34     g_assert(qdict_haskey(resp, "return"));
35     qobject_unref(resp);
36 }
37 
38 static void wait_device_deleted_event(QTestState *qtest, const char *id)
39 {
40     QDict *resp, *data;
41     QString *qstr;
42 
43     /*
44      * Other devices might get removed along with the removed device. Skip
45      * these. The device of interest will be the last one.
46      */
47     for (;;) {
48         resp = qtest_qmp_eventwait_ref(qtest, "DEVICE_DELETED");
49         data = qdict_get_qdict(resp, "data");
50         if (!data || !qdict_get(data, "device")) {
51             qobject_unref(resp);
52             continue;
53         }
54         qstr = qobject_to(QString, qdict_get(data, "device"));
55         g_assert(qstr);
56         if (!strcmp(qstring_get_str(qstr), id)) {
57             qobject_unref(resp);
58             break;
59         }
60         qobject_unref(resp);
61     }
62 }
63 
64 static void test_pci_unplug_request(void)
65 {
66     const char *arch = qtest_get_arch();
67     const char *machine_addition = "";
68 
69     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
70         machine_addition = "-machine pc";
71     }
72 
73     QTestState *qtest = qtest_initf("%s -device virtio-mouse-pci,id=dev0",
74                                     machine_addition);
75 
76     /*
77      * Request device removal. As the guest is not running, the request won't
78      * be processed. However during system reset, the removal will be
79      * handled, removing the device.
80      */
81     device_del(qtest, "dev0");
82     system_reset(qtest);
83     wait_device_deleted_event(qtest, "dev0");
84 
85     qtest_quit(qtest);
86 }
87 
88 static void test_pci_unplug_json_request(void)
89 {
90     const char *arch = qtest_get_arch();
91     const char *machine_addition = "";
92 
93     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
94         machine_addition = "-machine pc";
95     }
96 
97     QTestState *qtest = qtest_initf(
98         "%s -device '{\"driver\": \"virtio-mouse-pci\", \"id\": \"dev0\"}'",
99         machine_addition);
100 
101     /*
102      * Request device removal. As the guest is not running, the request won't
103      * be processed. However during system reset, the removal will be
104      * handled, removing the device.
105      */
106     device_del(qtest, "dev0");
107     system_reset(qtest);
108     wait_device_deleted_event(qtest, "dev0");
109 
110     qtest_quit(qtest);
111 }
112 
113 static void test_ccw_unplug(void)
114 {
115     QTestState *qtest = qtest_initf("-device virtio-balloon-ccw,id=dev0");
116 
117     device_del(qtest, "dev0");
118     wait_device_deleted_event(qtest, "dev0");
119 
120     qtest_quit(qtest);
121 }
122 
123 static void test_spapr_cpu_unplug_request(void)
124 {
125     QTestState *qtest;
126 
127     qtest = qtest_initf("-cpu power9_v2.0 -smp 1,maxcpus=2 "
128                         "-device power9_v2.0-spapr-cpu-core,core-id=1,id=dev0");
129 
130     /* similar to test_pci_unplug_request */
131     device_del(qtest, "dev0");
132     system_reset(qtest);
133     wait_device_deleted_event(qtest, "dev0");
134 
135     qtest_quit(qtest);
136 }
137 
138 static void test_spapr_memory_unplug_request(void)
139 {
140     QTestState *qtest;
141 
142     qtest = qtest_initf("-m 256M,slots=1,maxmem=768M "
143                         "-object memory-backend-ram,id=mem0,size=512M "
144                         "-device pc-dimm,id=dev0,memdev=mem0");
145 
146     /* similar to test_pci_unplug_request */
147     device_del(qtest, "dev0");
148     system_reset(qtest);
149     wait_device_deleted_event(qtest, "dev0");
150 
151     qtest_quit(qtest);
152 }
153 
154 static void test_spapr_phb_unplug_request(void)
155 {
156     QTestState *qtest;
157 
158     qtest = qtest_initf("-device spapr-pci-host-bridge,index=1,id=dev0");
159 
160     /* similar to test_pci_unplug_request */
161     device_del(qtest, "dev0");
162     system_reset(qtest);
163     wait_device_deleted_event(qtest, "dev0");
164 
165     qtest_quit(qtest);
166 }
167 
168 int main(int argc, char **argv)
169 {
170     const char *arch = qtest_get_arch();
171 
172     g_test_init(&argc, &argv, NULL);
173 
174     /*
175      * We need a system that will process unplug requests during system resets
176      * and does not do PCI surprise removal. This holds for x86 ACPI,
177      * s390x and spapr.
178      */
179     qtest_add_func("/device-plug/pci-unplug-request",
180                    test_pci_unplug_request);
181     qtest_add_func("/device-plug/pci-unplug-json-request",
182                    test_pci_unplug_json_request);
183 
184     if (!strcmp(arch, "s390x")) {
185         qtest_add_func("/device-plug/ccw-unplug",
186                        test_ccw_unplug);
187     }
188 
189     if (!strcmp(arch, "ppc64")) {
190         qtest_add_func("/device-plug/spapr-cpu-unplug-request",
191                        test_spapr_cpu_unplug_request);
192         qtest_add_func("/device-plug/spapr-memory-unplug-request",
193                        test_spapr_memory_unplug_request);
194         qtest_add_func("/device-plug/spapr-phb-unplug-request",
195                        test_spapr_phb_unplug_request);
196     }
197 
198     return g_test_run();
199 }
200