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