| 1 | /* |
| 2 | * blockdev.c test cases |
| 3 | * |
| 4 | * Copyright (C) 2013-2014 Red Hat Inc. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Stefan Hajnoczi <stefanha@redhat.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. |
| 10 | * See the COPYING.LIB file in the top-level directory. |
| 11 | */ |
| 12 | |
| 13 | #include "qemu/osdep.h" |
| 14 | #include "libqtest.h" |
| 15 | #include "libqos/virtio.h" |
| 16 | #include "qobject/qdict.h" |
| 17 | #include "qobject/qlist.h" |
| 18 | |
| 19 | static const char *qvirtio_get_dev_type(void); |
| 20 | |
| 21 | static bool look_for_drive0(QTestState *qts, const char *command, const char *key) |
| 22 | { |
| 23 | QDict *response; |
| 24 | QList *ret; |
| 25 | QListEntry *entry; |
| 26 | bool found; |
| 27 | |
| 28 | response = qtest_qmp(qts, "{'execute': %s}", command); |
| 29 | g_assert(response && qdict_haskey(response, "return")); |
| 30 | ret = qdict_get_qlist(response, "return"); |
| 31 | |
| 32 | found = false; |
| 33 | QLIST_FOREACH_ENTRY(ret, entry) { |
| 34 | QDict *entry_dict = qobject_to(QDict, entry->value); |
| 35 | if (!strcmp(qdict_get_str(entry_dict, key), "drive0")) { |
| 36 | found = true; |
| 37 | break; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | qobject_unref(response); |
| 42 | return found; |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * This covers the possible absence of a device due to QEMU build |
| 47 | * options. |
| 48 | */ |
| 49 | static bool has_device_builtin(const char *dev) |
| 50 | { |
| 51 | gchar *device = g_strdup_printf("%s-%s", dev, qvirtio_get_dev_type()); |
| 52 | bool rc = qtest_has_device(device); |
| 53 | |
| 54 | g_free(device); |
| 55 | return rc; |
| 56 | } |
| 57 | |
| 58 | static bool has_drive(QTestState *qts) |
| 59 | { |
| 60 | return look_for_drive0(qts, "query-block", "device"); |
| 61 | } |
| 62 | |
| 63 | static bool has_blockdev(QTestState *qts) |
| 64 | { |
| 65 | return look_for_drive0(qts, "query-named-block-nodes", "node-name"); |
| 66 | } |
| 67 | |
| 68 | static void blockdev_add_with_media(QTestState *qts) |
| 69 | { |
| 70 | QDict *response; |
| 71 | |
| 72 | response = qtest_qmp(qts, |
| 73 | "{ 'execute': 'blockdev-add'," |
| 74 | " 'arguments': {" |
| 75 | " 'driver': 'raw'," |
| 76 | " 'node-name': 'drive0'," |
| 77 | " 'file': {" |
| 78 | " 'driver': 'null-co'," |
| 79 | " 'read-zeroes': true" |
| 80 | " }" |
| 81 | " }" |
| 82 | "}"); |
| 83 | |
| 84 | g_assert(response); |
| 85 | g_assert(qdict_haskey(response, "return")); |
| 86 | qobject_unref(response); |
| 87 | g_assert(has_blockdev(qts)); |
| 88 | } |
| 89 | |
| 90 | static void drive_add(QTestState *qts) |
| 91 | { |
| 92 | char *resp = qtest_hmp(qts, "drive_add 0 if=none,id=drive0"); |
| 93 | |
| 94 | g_assert_cmpstr(resp, ==, "OK\r\n"); |
| 95 | g_assert(has_drive(qts)); |
| 96 | g_free(resp); |
| 97 | } |
| 98 | |
| 99 | static void drive_add_with_media(QTestState *qts) |
| 100 | { |
| 101 | char *resp = qtest_hmp(qts, |
| 102 | "drive_add 0 if=none,id=drive0,file=null-co://," |
| 103 | "file.read-zeroes=on,format=raw"); |
| 104 | |
| 105 | g_assert_cmpstr(resp, ==, "OK\r\n"); |
| 106 | g_assert(has_drive(qts)); |
| 107 | g_free(resp); |
| 108 | } |
| 109 | |
| 110 | static void drive_del(QTestState *qts) |
| 111 | { |
| 112 | char *resp; |
| 113 | |
| 114 | g_assert(has_drive(qts)); |
| 115 | resp = qtest_hmp(qts, "drive_del drive0"); |
| 116 | g_assert_cmpstr(resp, ==, ""); |
| 117 | g_assert(!has_drive(qts)); |
| 118 | g_free(resp); |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * qvirtio_get_dev_type: |
| 123 | * Returns: the preferred virtio bus/device type for the current architecture. |
| 124 | * TODO: delete this |
| 125 | */ |
| 126 | static const char *qvirtio_get_dev_type(void) |
| 127 | { |
| 128 | const char *arch = qtest_get_arch(); |
| 129 | |
| 130 | if (g_str_equal(arch, "arm") || g_str_equal(arch, "aarch64")) { |
| 131 | return "device"; /* for virtio-mmio */ |
| 132 | } else if (g_str_equal(arch, "s390x")) { |
| 133 | return "ccw"; |
| 134 | } else { |
| 135 | return "pci"; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | static void device_add(QTestState *qts) |
| 140 | { |
| 141 | g_autofree char *driver = g_strdup_printf("virtio-blk-%s", |
| 142 | qvirtio_get_dev_type()); |
| 143 | QDict *response = |
| 144 | qtest_qmp(qts, "{'execute': 'device_add'," |
| 145 | " 'arguments': {" |
| 146 | " 'driver': %s," |
| 147 | " 'drive': 'drive0'," |
| 148 | " 'id': 'dev0'" |
| 149 | "}}", driver); |
| 150 | g_assert(response); |
| 151 | g_assert(qdict_haskey(response, "return")); |
| 152 | qobject_unref(response); |
| 153 | } |
| 154 | |
| 155 | static void device_del(QTestState *qts, bool and_reset) |
| 156 | { |
| 157 | qtest_qmp_device_del_send(qts, "dev0"); |
| 158 | |
| 159 | if (and_reset) { |
| 160 | qtest_system_reset_nowait(qts); |
| 161 | } |
| 162 | |
| 163 | qtest_qmp_eventwait(qts, "DEVICE_DELETED"); |
| 164 | } |
| 165 | |
| 166 | static void test_drive_without_dev(void) |
| 167 | { |
| 168 | QTestState *qts; |
| 169 | |
| 170 | #ifndef CONFIG_HMP |
| 171 | g_test_skip("HMP not enabled"); |
| 172 | return; |
| 173 | #endif |
| 174 | |
| 175 | /* Start with an empty drive */ |
| 176 | qts = qtest_init("-drive if=none,id=drive0 -M none"); |
| 177 | |
| 178 | /* Delete the drive */ |
| 179 | drive_del(qts); |
| 180 | |
| 181 | /* Ensure re-adding the drive works - there should be no duplicate ID error |
| 182 | * because the old drive must be gone. |
| 183 | */ |
| 184 | drive_add(qts); |
| 185 | |
| 186 | qtest_quit(qts); |
| 187 | } |
| 188 | |
| 189 | static void test_after_failed_device_add(void) |
| 190 | { |
| 191 | char driver[32]; |
| 192 | QDict *response; |
| 193 | QTestState *qts; |
| 194 | |
| 195 | #ifndef CONFIG_HMP |
| 196 | g_test_skip("HMP not enabled"); |
| 197 | return; |
| 198 | #endif |
| 199 | |
| 200 | if (!has_device_builtin("virtio-blk")) { |
| 201 | g_test_skip("Device virtio-blk is not available"); |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | snprintf(driver, sizeof(driver), "virtio-blk-%s", |
| 206 | qvirtio_get_dev_type()); |
| 207 | |
| 208 | qts = qtest_init("-drive if=none,id=drive0"); |
| 209 | |
| 210 | /* Make device_add fail. If this leaks the virtio-blk device then a |
| 211 | * reference to drive0 will also be held (via qdev properties). |
| 212 | */ |
| 213 | response = qtest_qmp(qts, "{'execute': 'device_add'," |
| 214 | " 'arguments': {" |
| 215 | " 'driver': %s," |
| 216 | " 'drive': 'drive0'" |
| 217 | "}}", driver); |
| 218 | g_assert(response); |
| 219 | qmp_expect_error_and_unref(response, "GenericError"); |
| 220 | |
| 221 | /* Delete the drive */ |
| 222 | drive_del(qts); |
| 223 | |
| 224 | /* Try to re-add the drive. This fails with duplicate IDs if a leaked |
| 225 | * virtio-blk device exists that holds a reference to the old drive0. |
| 226 | */ |
| 227 | drive_add(qts); |
| 228 | |
| 229 | qtest_quit(qts); |
| 230 | } |
| 231 | |
| 232 | static void test_drive_del_device_del(void) |
| 233 | { |
| 234 | QTestState *qts; |
| 235 | |
| 236 | #ifndef CONFIG_HMP |
| 237 | g_test_skip("HMP not enabled"); |
| 238 | return; |
| 239 | #endif |
| 240 | |
| 241 | if (!has_device_builtin("virtio-scsi")) { |
| 242 | g_test_skip("Device virtio-scsi is not available"); |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | /* Start with a drive used by a device that unplugs instantaneously */ |
| 247 | qts = qtest_initf("-drive if=none,id=drive0,file=null-co://," |
| 248 | "file.read-zeroes=on,format=raw" |
| 249 | " -device virtio-scsi-%s" |
| 250 | " -device scsi-hd,drive=drive0,id=dev0", |
| 251 | qvirtio_get_dev_type()); |
| 252 | |
| 253 | /* |
| 254 | * Delete the drive, and then the device |
| 255 | * Doing it in this order takes notoriously tricky special paths |
| 256 | */ |
| 257 | drive_del(qts); |
| 258 | device_del(qts, false); |
| 259 | g_assert(!has_drive(qts)); |
| 260 | |
| 261 | qtest_quit(qts); |
| 262 | } |
| 263 | |
| 264 | static void test_cli_device_del(void) |
| 265 | { |
| 266 | QTestState *qts; |
| 267 | const char *arch = qtest_get_arch(); |
| 268 | const char *machine_addition = ""; |
| 269 | |
| 270 | if (!has_device_builtin("virtio-blk")) { |
| 271 | g_test_skip("Device virtio-blk is not available"); |
| 272 | return; |
| 273 | } |
| 274 | |
| 275 | if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { |
| 276 | if (!qtest_has_machine("pc")) { |
| 277 | g_test_skip("Machine 'pc' is not available"); |
| 278 | return; |
| 279 | } |
| 280 | machine_addition = "-machine pc"; |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * -drive/-device and device_del. Start with a drive used by a |
| 285 | * device that unplugs after reset. |
| 286 | */ |
| 287 | qts = qtest_initf("%s -drive if=none,id=drive0,file=null-co://," |
| 288 | "file.read-zeroes=on,format=raw" |
| 289 | " -device virtio-blk-%s,drive=drive0,id=dev0", |
| 290 | machine_addition, |
| 291 | qvirtio_get_dev_type()); |
| 292 | |
| 293 | device_del(qts, true); |
| 294 | g_assert(!has_drive(qts)); |
| 295 | |
| 296 | qtest_quit(qts); |
| 297 | } |
| 298 | |
| 299 | static void test_cli_device_del_q35(void) |
| 300 | { |
| 301 | QTestState *qts; |
| 302 | |
| 303 | if (!has_device_builtin("virtio-blk")) { |
| 304 | g_test_skip("Device virtio-blk is not available"); |
| 305 | return; |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * -drive/-device and device_del. Start with a drive used by a |
| 310 | * device that unplugs after reset. |
| 311 | */ |
| 312 | qts = qtest_initf("-drive if=none,id=drive0,file=null-co://," |
| 313 | "file.read-zeroes=on,format=raw " |
| 314 | "-machine q35 -device pcie-root-port,id=p1 " |
| 315 | "-device pcie-pci-bridge,bus=p1,id=b1 " |
| 316 | "-device virtio-blk-%s,drive=drive0,bus=b1,id=dev0", |
| 317 | qvirtio_get_dev_type()); |
| 318 | |
| 319 | device_del(qts, true); |
| 320 | g_assert(!has_drive(qts)); |
| 321 | |
| 322 | qtest_quit(qts); |
| 323 | } |
| 324 | |
| 325 | static void test_empty_device_del(void) |
| 326 | { |
| 327 | QTestState *qts; |
| 328 | |
| 329 | if (!has_device_builtin("virtio-scsi")) { |
| 330 | g_test_skip("Device virtio-scsi is not available"); |
| 331 | return; |
| 332 | } |
| 333 | |
| 334 | /* device_del with no drive plugged. */ |
| 335 | qts = qtest_initf("-device virtio-scsi-%s -device scsi-cd,id=dev0", |
| 336 | qvirtio_get_dev_type()); |
| 337 | |
| 338 | device_del(qts, false); |
| 339 | qtest_quit(qts); |
| 340 | } |
| 341 | |
| 342 | static void test_device_add_and_del(void) |
| 343 | { |
| 344 | QTestState *qts; |
| 345 | const char *arch = qtest_get_arch(); |
| 346 | const char *machine_addition = ""; |
| 347 | |
| 348 | if (!has_device_builtin("virtio-blk")) { |
| 349 | g_test_skip("Device virtio-blk is not available"); |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { |
| 354 | if (!qtest_has_machine("pc")) { |
| 355 | g_test_skip("Machine 'pc' is not available"); |
| 356 | return; |
| 357 | } |
| 358 | machine_addition = "-machine pc"; |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * -drive/device_add and device_del. Start with a drive used by a |
| 363 | * device that unplugs after reset. |
| 364 | */ |
| 365 | qts = qtest_initf("%s -drive if=none,id=drive0,file=null-co://," |
| 366 | "file.read-zeroes=on,format=raw", machine_addition); |
| 367 | |
| 368 | device_add(qts); |
| 369 | device_del(qts, true); |
| 370 | g_assert(!has_drive(qts)); |
| 371 | |
| 372 | qtest_quit(qts); |
| 373 | } |
| 374 | |
| 375 | static void device_add_q35(QTestState *qts) |
| 376 | { |
| 377 | g_autofree char *driver = g_strdup_printf("virtio-blk-%s", |
| 378 | qvirtio_get_dev_type()); |
| 379 | QDict *response = |
| 380 | qtest_qmp(qts, "{'execute': 'device_add'," |
| 381 | " 'arguments': {" |
| 382 | " 'driver': %s," |
| 383 | " 'drive': 'drive0'," |
| 384 | " 'id': 'dev0'," |
| 385 | " 'bus': 'b1'" |
| 386 | "}}", driver); |
| 387 | g_assert(response); |
| 388 | g_assert(qdict_haskey(response, "return")); |
| 389 | qobject_unref(response); |
| 390 | } |
| 391 | |
| 392 | static void test_device_add_and_del_q35(void) |
| 393 | { |
| 394 | QTestState *qts; |
| 395 | |
| 396 | if (!has_device_builtin("virtio-blk")) { |
| 397 | g_test_skip("Device virtio-blk is not available"); |
| 398 | return; |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * -drive/device_add and device_del. Start with a drive used by a |
| 403 | * device that unplugs after reset. |
| 404 | */ |
| 405 | qts = qtest_initf("-machine q35 -device pcie-root-port,id=p1 " |
| 406 | "-device pcie-pci-bridge,bus=p1,id=b1 " |
| 407 | "-drive if=none,id=drive0,file=null-co://," |
| 408 | "file.read-zeroes=on,format=raw"); |
| 409 | |
| 410 | device_add_q35(qts); |
| 411 | device_del(qts, true); |
| 412 | g_assert(!has_drive(qts)); |
| 413 | |
| 414 | qtest_quit(qts); |
| 415 | } |
| 416 | |
| 417 | static void test_drive_add_device_add_and_del(void) |
| 418 | { |
| 419 | QTestState *qts; |
| 420 | const char *arch = qtest_get_arch(); |
| 421 | const char *machine_addition = ""; |
| 422 | |
| 423 | #ifndef CONFIG_HMP |
| 424 | g_test_skip("HMP not enabled"); |
| 425 | return; |
| 426 | #endif |
| 427 | |
| 428 | if (!has_device_builtin("virtio-blk")) { |
| 429 | g_test_skip("Device virtio-blk is not available"); |
| 430 | return; |
| 431 | } |
| 432 | |
| 433 | if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { |
| 434 | if (!qtest_has_machine("pc")) { |
| 435 | g_test_skip("Machine 'pc' is not available"); |
| 436 | return; |
| 437 | } |
| 438 | machine_addition = "-machine pc"; |
| 439 | } |
| 440 | |
| 441 | qts = qtest_init(machine_addition); |
| 442 | |
| 443 | /* |
| 444 | * drive_add/device_add and device_del. The drive is used by a |
| 445 | * device that unplugs after reset. |
| 446 | */ |
| 447 | drive_add_with_media(qts); |
| 448 | device_add(qts); |
| 449 | device_del(qts, true); |
| 450 | g_assert(!has_drive(qts)); |
| 451 | |
| 452 | qtest_quit(qts); |
| 453 | } |
| 454 | |
| 455 | static void test_drive_add_device_add_and_del_q35(void) |
| 456 | { |
| 457 | QTestState *qts; |
| 458 | |
| 459 | #ifndef CONFIG_HMP |
| 460 | g_test_skip("HMP not enabled"); |
| 461 | return; |
| 462 | #endif |
| 463 | |
| 464 | if (!has_device_builtin("virtio-blk")) { |
| 465 | g_test_skip("Device virtio-blk is not available"); |
| 466 | return; |
| 467 | } |
| 468 | |
| 469 | qts = qtest_init("-machine q35 -device pcie-root-port,id=p1 " |
| 470 | "-device pcie-pci-bridge,bus=p1,id=b1"); |
| 471 | |
| 472 | /* |
| 473 | * drive_add/device_add and device_del. The drive is used by a |
| 474 | * device that unplugs after reset. |
| 475 | */ |
| 476 | drive_add_with_media(qts); |
| 477 | device_add_q35(qts); |
| 478 | device_del(qts, true); |
| 479 | g_assert(!has_drive(qts)); |
| 480 | |
| 481 | qtest_quit(qts); |
| 482 | } |
| 483 | |
| 484 | static void test_blockdev_add_device_add_and_del(void) |
| 485 | { |
| 486 | QTestState *qts; |
| 487 | const char *arch = qtest_get_arch(); |
| 488 | const char *machine_addition = ""; |
| 489 | |
| 490 | if (!has_device_builtin("virtio-blk")) { |
| 491 | g_test_skip("Device virtio-blk is not available"); |
| 492 | return; |
| 493 | } |
| 494 | |
| 495 | if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { |
| 496 | if (!qtest_has_machine("pc")) { |
| 497 | g_test_skip("Machine 'pc' is not available"); |
| 498 | return; |
| 499 | } |
| 500 | machine_addition = "-machine pc"; |
| 501 | } |
| 502 | |
| 503 | qts = qtest_init(machine_addition); |
| 504 | |
| 505 | /* |
| 506 | * blockdev_add/device_add and device_del. The drive is used by a |
| 507 | * device that unplugs after reset, but it doesn't go away. |
| 508 | */ |
| 509 | blockdev_add_with_media(qts); |
| 510 | device_add(qts); |
| 511 | device_del(qts, true); |
| 512 | g_assert(has_blockdev(qts)); |
| 513 | |
| 514 | qtest_quit(qts); |
| 515 | } |
| 516 | |
| 517 | static void test_blockdev_add_device_add_and_del_q35(void) |
| 518 | { |
| 519 | QTestState *qts; |
| 520 | |
| 521 | if (!has_device_builtin("virtio-blk")) { |
| 522 | g_test_skip("Device virtio-blk is not available"); |
| 523 | return; |
| 524 | } |
| 525 | |
| 526 | qts = qtest_init("-machine q35 -device pcie-root-port,id=p1 " |
| 527 | "-device pcie-pci-bridge,bus=p1,id=b1"); |
| 528 | |
| 529 | /* |
| 530 | * blockdev_add/device_add and device_del. The drive is used by a |
| 531 | * device that unplugs after reset, but it doesn't go away. |
| 532 | */ |
| 533 | blockdev_add_with_media(qts); |
| 534 | device_add_q35(qts); |
| 535 | device_del(qts, true); |
| 536 | g_assert(has_blockdev(qts)); |
| 537 | |
| 538 | qtest_quit(qts); |
| 539 | } |
| 540 | |
| 541 | int main(int argc, char **argv) |
| 542 | { |
| 543 | g_test_init(&argc, &argv, NULL); |
| 544 | |
| 545 | qtest_add_func("/drive_del/without-dev", test_drive_without_dev); |
| 546 | |
| 547 | if (qvirtio_get_dev_type() != NULL) { |
| 548 | qtest_add_func("/drive_del/after_failed_device_add", |
| 549 | test_after_failed_device_add); |
| 550 | qtest_add_func("/drive_del/drive_del_device_del", |
| 551 | test_drive_del_device_del); |
| 552 | qtest_add_func("/device_del/drive/cli_device", |
| 553 | test_cli_device_del); |
| 554 | qtest_add_func("/device_del/drive/device_add", |
| 555 | test_device_add_and_del); |
| 556 | qtest_add_func("/device_del/drive/drive_add_device_add", |
| 557 | test_drive_add_device_add_and_del); |
| 558 | qtest_add_func("/device_del/empty", |
| 559 | test_empty_device_del); |
| 560 | qtest_add_func("/device_del/blockdev", |
| 561 | test_blockdev_add_device_add_and_del); |
| 562 | |
| 563 | if (qtest_has_machine("q35")) { |
| 564 | qtest_add_func("/device_del/drive/cli_device_q35", |
| 565 | test_cli_device_del_q35); |
| 566 | qtest_add_func("/device_del/drive/device_add_q35", |
| 567 | test_device_add_and_del_q35); |
| 568 | qtest_add_func("/device_del/drive/drive_add_device_add_q35", |
| 569 | test_drive_add_device_add_and_del_q35); |
| 570 | qtest_add_func("/device_del/blockdev_q35", |
| 571 | test_blockdev_add_device_add_and_del_q35); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | return g_test_run(); |
| 576 | } |