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 QTestState *qtest; 68 const char *arch = qtest_get_arch(); 69 const char *machine_addition = ""; 70 71 if (!qtest_has_device("virtio-mouse-pci")) { 72 g_test_skip("Device virtio-mouse-pci not available"); 73 return; 74 } 75 76 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { 77 machine_addition = "-machine pc"; 78 } 79 80 qtest = qtest_initf("%s -device virtio-mouse-pci,id=dev0", 81 machine_addition); 82 83 process_device_remove(qtest, "dev0"); 84 85 qtest_quit(qtest); 86 } 87 88 static void test_q35_pci_unplug_request(void) 89 { 90 QTestState *qtest; 91 92 if (!qtest_has_device("virtio-mouse-pci")) { 93 g_test_skip("Device virtio-mouse-pci not available"); 94 return; 95 } 96 97 qtest = qtest_initf("-machine q35 " 98 "-device pcie-root-port,id=p1 " 99 "-device pcie-pci-bridge,bus=p1,id=b1 " 100 "-device virtio-mouse-pci,bus=b1,id=dev0"); 101 102 process_device_remove(qtest, "dev0"); 103 104 qtest_quit(qtest); 105 } 106 107 static void test_pci_unplug_json_request(void) 108 { 109 QTestState *qtest; 110 const char *arch = qtest_get_arch(); 111 const char *machine_addition = ""; 112 113 if (!qtest_has_device("virtio-mouse-pci")) { 114 g_test_skip("Device virtio-mouse-pci not available"); 115 return; 116 } 117 118 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { 119 machine_addition = "-machine pc"; 120 } 121 122 qtest = qtest_initf( 123 "%s -device \"{'driver': 'virtio-mouse-pci', 'id': 'dev0'}\"", 124 machine_addition); 125 126 process_device_remove(qtest, "dev0"); 127 128 qtest_quit(qtest); 129 } 130 131 static void test_q35_pci_unplug_json_request(void) 132 { 133 QTestState *qtest; 134 const char *port = "-device \"{'driver': 'pcie-root-port', " 135 "'id': 'p1'}\""; 136 137 const char *bridge = "-device \"{'driver': 'pcie-pci-bridge', " 138 "'id': 'b1', " 139 "'bus': 'p1'}\""; 140 141 const char *device = "-device \"{'driver': 'virtio-mouse-pci', " 142 "'bus': 'b1', " 143 "'id': 'dev0'}\""; 144 145 if (!qtest_has_device("virtio-mouse-pci")) { 146 g_test_skip("Device virtio-mouse-pci not available"); 147 return; 148 } 149 150 qtest = qtest_initf("-machine q35 %s %s %s", port, bridge, device); 151 152 process_device_remove(qtest, "dev0"); 153 154 qtest_quit(qtest); 155 } 156 157 static void test_ccw_unplug(void) 158 { 159 QTestState *qtest = qtest_initf("-device virtio-balloon-ccw,id=dev0"); 160 161 qtest_qmp_device_del_send(qtest, "dev0"); 162 wait_device_deleted_event(qtest, "dev0"); 163 164 qtest_quit(qtest); 165 } 166 167 static void test_spapr_cpu_unplug_request(void) 168 { 169 QTestState *qtest; 170 171 qtest = qtest_initf("-cpu power9_v2.0 -smp 1,maxcpus=2 " 172 "-device power9_v2.0-spapr-cpu-core,core-id=1,id=dev0"); 173 174 /* similar to test_pci_unplug_request */ 175 process_device_remove(qtest, "dev0"); 176 177 qtest_quit(qtest); 178 } 179 180 static void test_spapr_memory_unplug_request(void) 181 { 182 QTestState *qtest; 183 184 qtest = qtest_initf("-m 256M,slots=1,maxmem=768M " 185 "-object memory-backend-ram,id=mem0,size=512M " 186 "-device pc-dimm,id=dev0,memdev=mem0"); 187 188 /* similar to test_pci_unplug_request */ 189 process_device_remove(qtest, "dev0"); 190 191 qtest_quit(qtest); 192 } 193 194 static void test_spapr_phb_unplug_request(void) 195 { 196 QTestState *qtest; 197 198 qtest = qtest_initf("-device spapr-pci-host-bridge,index=1,id=dev0"); 199 200 /* similar to test_pci_unplug_request */ 201 process_device_remove(qtest, "dev0"); 202 203 qtest_quit(qtest); 204 } 205 206 int main(int argc, char **argv) 207 { 208 const char *arch = qtest_get_arch(); 209 210 g_test_init(&argc, &argv, NULL); 211 212 /* 213 * We need a system that will process unplug requests during system resets 214 * and does not do PCI surprise removal. This holds for x86 ACPI, 215 * s390x and spapr. 216 */ 217 qtest_add_func("/device-plug/pci-unplug-request", 218 test_pci_unplug_request); 219 qtest_add_func("/device-plug/pci-unplug-json-request", 220 test_pci_unplug_json_request); 221 222 if (!strcmp(arch, "s390x")) { 223 qtest_add_func("/device-plug/ccw-unplug", 224 test_ccw_unplug); 225 } 226 227 if (!strcmp(arch, "ppc64")) { 228 qtest_add_func("/device-plug/spapr-cpu-unplug-request", 229 test_spapr_cpu_unplug_request); 230 qtest_add_func("/device-plug/spapr-memory-unplug-request", 231 test_spapr_memory_unplug_request); 232 qtest_add_func("/device-plug/spapr-phb-unplug-request", 233 test_spapr_phb_unplug_request); 234 } 235 236 if (!strcmp(arch, "x86_64") && qtest_has_machine("q35")) { 237 qtest_add_func("/device-plug/q35-pci-unplug-request", 238 test_q35_pci_unplug_request); 239 qtest_add_func("/device-plug/q35-pci-unplug-json-request", 240 test_q35_pci_unplug_json_request); 241 } 242 243 return g_test_run(); 244 } 245