11e8a1faeSThomas Huth /* 21e8a1faeSThomas Huth * QEMU device plug/unplug handling 31e8a1faeSThomas Huth * 41e8a1faeSThomas Huth * Copyright (C) 2019 Red Hat Inc. 51e8a1faeSThomas Huth * 61e8a1faeSThomas Huth * Authors: 71e8a1faeSThomas Huth * David Hildenbrand <david@redhat.com> 81e8a1faeSThomas Huth * 91e8a1faeSThomas Huth * This work is licensed under the terms of the GNU GPL, version 2 or later. 101e8a1faeSThomas Huth * See the COPYING file in the top-level directory. 111e8a1faeSThomas Huth */ 121e8a1faeSThomas Huth 131e8a1faeSThomas Huth #include "qemu/osdep.h" 14*907b5105SMarc-André Lureau #include "libqtest.h" 151e8a1faeSThomas Huth #include "qapi/qmp/qdict.h" 161e8a1faeSThomas Huth #include "qapi/qmp/qstring.h" 171e8a1faeSThomas Huth 18c45a70d8SPaolo Bonzini static void device_del(QTestState *qtest, const char *id) 191e8a1faeSThomas Huth { 20c45a70d8SPaolo Bonzini QDict *resp; 211e8a1faeSThomas Huth 22c45a70d8SPaolo Bonzini resp = qtest_qmp(qtest, 23c45a70d8SPaolo Bonzini "{'execute': 'device_del', 'arguments': { 'id': %s } }", id); 241e8a1faeSThomas Huth 251e8a1faeSThomas Huth g_assert(qdict_haskey(resp, "return")); 261e8a1faeSThomas Huth qobject_unref(resp); 271e8a1faeSThomas Huth } 281e8a1faeSThomas Huth 291e8a1faeSThomas Huth static void system_reset(QTestState *qtest) 301e8a1faeSThomas Huth { 311e8a1faeSThomas Huth QDict *resp; 321e8a1faeSThomas Huth 331e8a1faeSThomas Huth resp = qtest_qmp(qtest, "{'execute': 'system_reset'}"); 341e8a1faeSThomas Huth g_assert(qdict_haskey(resp, "return")); 351e8a1faeSThomas Huth qobject_unref(resp); 361e8a1faeSThomas Huth } 371e8a1faeSThomas Huth 381e8a1faeSThomas Huth static void wait_device_deleted_event(QTestState *qtest, const char *id) 391e8a1faeSThomas Huth { 401e8a1faeSThomas Huth QDict *resp, *data; 411e8a1faeSThomas Huth QString *qstr; 421e8a1faeSThomas Huth 431e8a1faeSThomas Huth /* 441e8a1faeSThomas Huth * Other devices might get removed along with the removed device. Skip 451e8a1faeSThomas Huth * these. The device of interest will be the last one. 461e8a1faeSThomas Huth */ 471e8a1faeSThomas Huth for (;;) { 481e8a1faeSThomas Huth resp = qtest_qmp_eventwait_ref(qtest, "DEVICE_DELETED"); 491e8a1faeSThomas Huth data = qdict_get_qdict(resp, "data"); 501e8a1faeSThomas Huth if (!data || !qdict_get(data, "device")) { 511e8a1faeSThomas Huth qobject_unref(resp); 521e8a1faeSThomas Huth continue; 531e8a1faeSThomas Huth } 541e8a1faeSThomas Huth qstr = qobject_to(QString, qdict_get(data, "device")); 551e8a1faeSThomas Huth g_assert(qstr); 561e8a1faeSThomas Huth if (!strcmp(qstring_get_str(qstr), id)) { 571e8a1faeSThomas Huth qobject_unref(resp); 581e8a1faeSThomas Huth break; 591e8a1faeSThomas Huth } 601e8a1faeSThomas Huth qobject_unref(resp); 611e8a1faeSThomas Huth } 621e8a1faeSThomas Huth } 631e8a1faeSThomas Huth 641e8a1faeSThomas Huth static void test_pci_unplug_request(void) 651e8a1faeSThomas Huth { 667b172333SDr. David Alan Gilbert const char *arch = qtest_get_arch(); 677b172333SDr. David Alan Gilbert const char *machine_addition = ""; 687b172333SDr. David Alan Gilbert 697b172333SDr. David Alan Gilbert if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { 707b172333SDr. David Alan Gilbert machine_addition = "-machine pc"; 717b172333SDr. David Alan Gilbert } 727b172333SDr. David Alan Gilbert 737b172333SDr. David Alan Gilbert QTestState *qtest = qtest_initf("%s -device virtio-mouse-pci,id=dev0", 747b172333SDr. David Alan Gilbert machine_addition); 751e8a1faeSThomas Huth 761e8a1faeSThomas Huth /* 771e8a1faeSThomas Huth * Request device removal. As the guest is not running, the request won't 781e8a1faeSThomas Huth * be processed. However during system reset, the removal will be 791e8a1faeSThomas Huth * handled, removing the device. 801e8a1faeSThomas Huth */ 81c45a70d8SPaolo Bonzini device_del(qtest, "dev0"); 821e8a1faeSThomas Huth system_reset(qtest); 831e8a1faeSThomas Huth wait_device_deleted_event(qtest, "dev0"); 841e8a1faeSThomas Huth 851e8a1faeSThomas Huth qtest_quit(qtest); 861e8a1faeSThomas Huth } 871e8a1faeSThomas Huth 8864b4529aSDaniel P. Berrangé static void test_pci_unplug_json_request(void) 8964b4529aSDaniel P. Berrangé { 907b172333SDr. David Alan Gilbert const char *arch = qtest_get_arch(); 917b172333SDr. David Alan Gilbert const char *machine_addition = ""; 927b172333SDr. David Alan Gilbert 937b172333SDr. David Alan Gilbert if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { 947b172333SDr. David Alan Gilbert machine_addition = "-machine pc"; 957b172333SDr. David Alan Gilbert } 967b172333SDr. David Alan Gilbert 9764b4529aSDaniel P. Berrangé QTestState *qtest = qtest_initf( 987b172333SDr. David Alan Gilbert "%s -device '{\"driver\": \"virtio-mouse-pci\", \"id\": \"dev0\"}'", 997b172333SDr. David Alan Gilbert machine_addition); 10064b4529aSDaniel P. Berrangé 10164b4529aSDaniel P. Berrangé /* 10264b4529aSDaniel P. Berrangé * Request device removal. As the guest is not running, the request won't 10364b4529aSDaniel P. Berrangé * be processed. However during system reset, the removal will be 10464b4529aSDaniel P. Berrangé * handled, removing the device. 10564b4529aSDaniel P. Berrangé */ 10664b4529aSDaniel P. Berrangé device_del(qtest, "dev0"); 10764b4529aSDaniel P. Berrangé system_reset(qtest); 10864b4529aSDaniel P. Berrangé wait_device_deleted_event(qtest, "dev0"); 10964b4529aSDaniel P. Berrangé 11064b4529aSDaniel P. Berrangé qtest_quit(qtest); 11164b4529aSDaniel P. Berrangé } 11264b4529aSDaniel P. Berrangé 1131e8a1faeSThomas Huth static void test_ccw_unplug(void) 1141e8a1faeSThomas Huth { 1151e8a1faeSThomas Huth QTestState *qtest = qtest_initf("-device virtio-balloon-ccw,id=dev0"); 1161e8a1faeSThomas Huth 117c45a70d8SPaolo Bonzini device_del(qtest, "dev0"); 1181e8a1faeSThomas Huth wait_device_deleted_event(qtest, "dev0"); 1191e8a1faeSThomas Huth 1201e8a1faeSThomas Huth qtest_quit(qtest); 1211e8a1faeSThomas Huth } 1221e8a1faeSThomas Huth 1231e8a1faeSThomas Huth static void test_spapr_cpu_unplug_request(void) 1241e8a1faeSThomas Huth { 1251e8a1faeSThomas Huth QTestState *qtest; 1261e8a1faeSThomas Huth 1271e8a1faeSThomas Huth qtest = qtest_initf("-cpu power9_v2.0 -smp 1,maxcpus=2 " 1281e8a1faeSThomas Huth "-device power9_v2.0-spapr-cpu-core,core-id=1,id=dev0"); 1291e8a1faeSThomas Huth 1301e8a1faeSThomas Huth /* similar to test_pci_unplug_request */ 131c45a70d8SPaolo Bonzini device_del(qtest, "dev0"); 1321e8a1faeSThomas Huth system_reset(qtest); 1331e8a1faeSThomas Huth wait_device_deleted_event(qtest, "dev0"); 1341e8a1faeSThomas Huth 1351e8a1faeSThomas Huth qtest_quit(qtest); 1361e8a1faeSThomas Huth } 1371e8a1faeSThomas Huth 1381e8a1faeSThomas Huth static void test_spapr_memory_unplug_request(void) 1391e8a1faeSThomas Huth { 1401e8a1faeSThomas Huth QTestState *qtest; 1411e8a1faeSThomas Huth 1421e8a1faeSThomas Huth qtest = qtest_initf("-m 256M,slots=1,maxmem=768M " 1431e8a1faeSThomas Huth "-object memory-backend-ram,id=mem0,size=512M " 1441e8a1faeSThomas Huth "-device pc-dimm,id=dev0,memdev=mem0"); 1451e8a1faeSThomas Huth 1461e8a1faeSThomas Huth /* similar to test_pci_unplug_request */ 147c45a70d8SPaolo Bonzini device_del(qtest, "dev0"); 1481e8a1faeSThomas Huth system_reset(qtest); 1491e8a1faeSThomas Huth wait_device_deleted_event(qtest, "dev0"); 1501e8a1faeSThomas Huth 1511e8a1faeSThomas Huth qtest_quit(qtest); 1521e8a1faeSThomas Huth } 1531e8a1faeSThomas Huth 1541e8a1faeSThomas Huth static void test_spapr_phb_unplug_request(void) 1551e8a1faeSThomas Huth { 1561e8a1faeSThomas Huth QTestState *qtest; 1571e8a1faeSThomas Huth 1581e8a1faeSThomas Huth qtest = qtest_initf("-device spapr-pci-host-bridge,index=1,id=dev0"); 1591e8a1faeSThomas Huth 1601e8a1faeSThomas Huth /* similar to test_pci_unplug_request */ 161c45a70d8SPaolo Bonzini device_del(qtest, "dev0"); 1621e8a1faeSThomas Huth system_reset(qtest); 1631e8a1faeSThomas Huth wait_device_deleted_event(qtest, "dev0"); 1641e8a1faeSThomas Huth 1651e8a1faeSThomas Huth qtest_quit(qtest); 1661e8a1faeSThomas Huth } 1671e8a1faeSThomas Huth 1681e8a1faeSThomas Huth int main(int argc, char **argv) 1691e8a1faeSThomas Huth { 1701e8a1faeSThomas Huth const char *arch = qtest_get_arch(); 1711e8a1faeSThomas Huth 1721e8a1faeSThomas Huth g_test_init(&argc, &argv, NULL); 1731e8a1faeSThomas Huth 1741e8a1faeSThomas Huth /* 1751e8a1faeSThomas Huth * We need a system that will process unplug requests during system resets 1761e8a1faeSThomas Huth * and does not do PCI surprise removal. This holds for x86 ACPI, 1771e8a1faeSThomas Huth * s390x and spapr. 1781e8a1faeSThomas Huth */ 1791e8a1faeSThomas Huth qtest_add_func("/device-plug/pci-unplug-request", 1801e8a1faeSThomas Huth test_pci_unplug_request); 18164b4529aSDaniel P. Berrangé qtest_add_func("/device-plug/pci-unplug-json-request", 18264b4529aSDaniel P. Berrangé test_pci_unplug_json_request); 1831e8a1faeSThomas Huth 1841e8a1faeSThomas Huth if (!strcmp(arch, "s390x")) { 1851e8a1faeSThomas Huth qtest_add_func("/device-plug/ccw-unplug", 1861e8a1faeSThomas Huth test_ccw_unplug); 1871e8a1faeSThomas Huth } 1881e8a1faeSThomas Huth 1891e8a1faeSThomas Huth if (!strcmp(arch, "ppc64")) { 1901e8a1faeSThomas Huth qtest_add_func("/device-plug/spapr-cpu-unplug-request", 1911e8a1faeSThomas Huth test_spapr_cpu_unplug_request); 1921e8a1faeSThomas Huth qtest_add_func("/device-plug/spapr-memory-unplug-request", 1931e8a1faeSThomas Huth test_spapr_memory_unplug_request); 1941e8a1faeSThomas Huth qtest_add_func("/device-plug/spapr-phb-unplug-request", 1951e8a1faeSThomas Huth test_spapr_phb_unplug_request); 1961e8a1faeSThomas Huth } 1971e8a1faeSThomas Huth 1981e8a1faeSThomas Huth return g_test_run(); 1991e8a1faeSThomas Huth } 200