1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "spdk_cunit.h" 35 36 #include "lib/test_env.c" 37 #include "lib/ut_multithread.c" 38 39 /* HACK: disable VTune integration so the unit test doesn't need VTune headers and libs to build */ 40 #undef SPDK_CONFIG_VTUNE 41 42 #include "bdev/bdev.c" 43 44 #define BDEV_UT_NUM_THREADS 3 45 46 DEFINE_STUB_V(spdk_scsi_nvme_translate, (const struct spdk_bdev_io *bdev_io, 47 int *sc, int *sk, int *asc, int *ascq)); 48 49 struct ut_bdev { 50 struct spdk_bdev bdev; 51 void *io_target; 52 }; 53 54 struct ut_bdev_channel { 55 TAILQ_HEAD(, spdk_bdev_io) outstanding_io; 56 uint32_t outstanding_cnt; 57 uint32_t avail_cnt; 58 }; 59 60 int g_io_device; 61 struct ut_bdev g_bdev; 62 struct spdk_bdev_desc *g_desc; 63 bool g_teardown_done = false; 64 bool g_get_io_channel = true; 65 bool g_create_ch = true; 66 67 static int 68 stub_create_ch(void *io_device, void *ctx_buf) 69 { 70 struct ut_bdev_channel *ch = ctx_buf; 71 72 if (g_create_ch == false) { 73 return -1; 74 } 75 76 TAILQ_INIT(&ch->outstanding_io); 77 ch->outstanding_cnt = 0; 78 /* 79 * When avail gets to 0, the submit_request function will return ENOMEM. 80 * Most tests to not want ENOMEM to occur, so by default set this to a 81 * big value that won't get hit. The ENOMEM tests can then override this 82 * value to something much smaller to induce ENOMEM conditions. 83 */ 84 ch->avail_cnt = 2048; 85 return 0; 86 } 87 88 static void 89 stub_destroy_ch(void *io_device, void *ctx_buf) 90 { 91 } 92 93 static struct spdk_io_channel * 94 stub_get_io_channel(void *ctx) 95 { 96 struct ut_bdev *ut_bdev = ctx; 97 98 if (g_get_io_channel == true) { 99 return spdk_get_io_channel(ut_bdev->io_target); 100 } else { 101 return NULL; 102 } 103 } 104 105 static int 106 stub_destruct(void *ctx) 107 { 108 return 0; 109 } 110 111 static void 112 stub_submit_request(struct spdk_io_channel *_ch, struct spdk_bdev_io *bdev_io) 113 { 114 struct ut_bdev_channel *ch = spdk_io_channel_get_ctx(_ch); 115 116 if (bdev_io->type == SPDK_BDEV_IO_TYPE_RESET) { 117 struct spdk_bdev_io *io; 118 119 while (!TAILQ_EMPTY(&ch->outstanding_io)) { 120 io = TAILQ_FIRST(&ch->outstanding_io); 121 TAILQ_REMOVE(&ch->outstanding_io, io, module_link); 122 ch->outstanding_cnt--; 123 spdk_bdev_io_complete(io, SPDK_BDEV_IO_STATUS_FAILED); 124 ch->avail_cnt++; 125 } 126 } 127 128 if (ch->avail_cnt > 0) { 129 TAILQ_INSERT_TAIL(&ch->outstanding_io, bdev_io, module_link); 130 ch->outstanding_cnt++; 131 ch->avail_cnt--; 132 } else { 133 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_NOMEM); 134 } 135 } 136 137 static uint32_t 138 stub_complete_io(void *io_target, uint32_t num_to_complete) 139 { 140 struct spdk_io_channel *_ch = spdk_get_io_channel(io_target); 141 struct ut_bdev_channel *ch = spdk_io_channel_get_ctx(_ch); 142 struct spdk_bdev_io *io; 143 bool complete_all = (num_to_complete == 0); 144 uint32_t num_completed = 0; 145 146 while (complete_all || num_completed < num_to_complete) { 147 if (TAILQ_EMPTY(&ch->outstanding_io)) { 148 break; 149 } 150 io = TAILQ_FIRST(&ch->outstanding_io); 151 TAILQ_REMOVE(&ch->outstanding_io, io, module_link); 152 ch->outstanding_cnt--; 153 spdk_bdev_io_complete(io, SPDK_BDEV_IO_STATUS_SUCCESS); 154 ch->avail_cnt++; 155 num_completed++; 156 } 157 158 spdk_put_io_channel(_ch); 159 return num_completed; 160 } 161 162 static struct spdk_bdev_fn_table fn_table = { 163 .get_io_channel = stub_get_io_channel, 164 .destruct = stub_destruct, 165 .submit_request = stub_submit_request, 166 }; 167 168 static int 169 module_init(void) 170 { 171 return 0; 172 } 173 174 static void 175 module_fini(void) 176 { 177 } 178 179 struct spdk_bdev_module bdev_ut_if = { 180 .name = "bdev_ut", 181 .module_init = module_init, 182 .module_fini = module_fini, 183 }; 184 185 SPDK_BDEV_MODULE_REGISTER(&bdev_ut_if) 186 187 static void 188 register_bdev(struct ut_bdev *ut_bdev, char *name, void *io_target) 189 { 190 memset(ut_bdev, 0, sizeof(*ut_bdev)); 191 192 ut_bdev->io_target = io_target; 193 ut_bdev->bdev.ctxt = ut_bdev; 194 ut_bdev->bdev.name = name; 195 ut_bdev->bdev.fn_table = &fn_table; 196 ut_bdev->bdev.module = &bdev_ut_if; 197 ut_bdev->bdev.blocklen = 4096; 198 ut_bdev->bdev.blockcnt = 1024; 199 200 spdk_bdev_register(&ut_bdev->bdev); 201 } 202 203 static void 204 unregister_bdev(struct ut_bdev *ut_bdev) 205 { 206 /* Handle any deferred messages. */ 207 poll_threads(); 208 spdk_bdev_unregister(&ut_bdev->bdev, NULL, NULL); 209 memset(ut_bdev, 0, sizeof(*ut_bdev)); 210 } 211 212 static void 213 bdev_init_cb(void *done, int rc) 214 { 215 CU_ASSERT(rc == 0); 216 *(bool *)done = true; 217 } 218 219 static void 220 setup_test(void) 221 { 222 bool done = false; 223 224 allocate_threads(BDEV_UT_NUM_THREADS); 225 spdk_bdev_initialize(bdev_init_cb, &done); 226 spdk_io_device_register(&g_io_device, stub_create_ch, stub_destroy_ch, 227 sizeof(struct ut_bdev_channel)); 228 register_bdev(&g_bdev, "ut_bdev", &g_io_device); 229 spdk_bdev_open(&g_bdev.bdev, true, NULL, NULL, &g_desc); 230 } 231 232 static void 233 finish_cb(void *cb_arg) 234 { 235 g_teardown_done = true; 236 } 237 238 static void 239 teardown_test(void) 240 { 241 g_teardown_done = false; 242 spdk_bdev_close(g_desc); 243 g_desc = NULL; 244 unregister_bdev(&g_bdev); 245 spdk_io_device_unregister(&g_io_device, NULL); 246 spdk_bdev_finish(finish_cb, NULL); 247 poll_threads(); 248 CU_ASSERT(g_teardown_done == true); 249 g_teardown_done = false; 250 free_threads(); 251 } 252 253 static uint32_t 254 bdev_io_tailq_cnt(bdev_io_tailq_t *tailq) 255 { 256 struct spdk_bdev_io *io; 257 uint32_t cnt = 0; 258 259 TAILQ_FOREACH(io, tailq, link) { 260 cnt++; 261 } 262 263 return cnt; 264 } 265 266 static void 267 basic(void) 268 { 269 setup_test(); 270 271 set_thread(0); 272 273 g_get_io_channel = false; 274 g_ut_threads[0].ch = spdk_bdev_get_io_channel(g_desc); 275 CU_ASSERT(g_ut_threads[0].ch == NULL); 276 277 g_get_io_channel = true; 278 g_create_ch = false; 279 g_ut_threads[0].ch = spdk_bdev_get_io_channel(g_desc); 280 CU_ASSERT(g_ut_threads[0].ch == NULL); 281 282 g_get_io_channel = true; 283 g_create_ch = true; 284 g_ut_threads[0].ch = spdk_bdev_get_io_channel(g_desc); 285 CU_ASSERT(g_ut_threads[0].ch != NULL); 286 spdk_put_io_channel(g_ut_threads[0].ch); 287 288 teardown_test(); 289 } 290 291 static void 292 poller_run_done(void *ctx) 293 { 294 bool *poller_run = ctx; 295 296 *poller_run = true; 297 } 298 299 static void 300 poller_run_times_done(void *ctx) 301 { 302 int *poller_run_times = ctx; 303 304 (*poller_run_times)++; 305 } 306 307 static void 308 basic_poller(void) 309 { 310 struct spdk_poller *poller = NULL; 311 bool poller_run = false; 312 int poller_run_times = 0; 313 314 setup_test(); 315 316 set_thread(0); 317 reset_time(); 318 /* Register a poller with no-wait time and test execution */ 319 poller = spdk_poller_register(poller_run_done, &poller_run, 0); 320 CU_ASSERT(poller != NULL); 321 322 poll_threads(); 323 CU_ASSERT(poller_run == true); 324 325 spdk_poller_unregister(&poller); 326 CU_ASSERT(poller == NULL); 327 328 /* Register a poller with 1000us wait time and test single execution */ 329 poller_run = false; 330 poller = spdk_poller_register(poller_run_done, &poller_run, 1000); 331 CU_ASSERT(poller != NULL); 332 333 poll_threads(); 334 CU_ASSERT(poller_run == false); 335 336 increment_time(1000); 337 poll_threads(); 338 CU_ASSERT(poller_run == true); 339 340 reset_time(); 341 poller_run = false; 342 poll_threads(); 343 CU_ASSERT(poller_run == false); 344 345 increment_time(1000); 346 poll_threads(); 347 CU_ASSERT(poller_run == true); 348 349 spdk_poller_unregister(&poller); 350 CU_ASSERT(poller == NULL); 351 352 reset_time(); 353 /* Register a poller with 1000us wait time and test multiple execution */ 354 poller = spdk_poller_register(poller_run_times_done, &poller_run_times, 1000); 355 CU_ASSERT(poller != NULL); 356 357 poll_threads(); 358 CU_ASSERT(poller_run_times == 0); 359 360 increment_time(1000); 361 poll_threads(); 362 CU_ASSERT(poller_run_times == 1); 363 364 poller_run_times = 0; 365 increment_time(2000); 366 poll_threads(); 367 CU_ASSERT(poller_run_times == 2); 368 369 spdk_poller_unregister(&poller); 370 CU_ASSERT(poller == NULL); 371 372 teardown_test(); 373 } 374 375 static void 376 reset_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 377 { 378 bool *done = cb_arg; 379 380 CU_ASSERT(success == true); 381 *done = true; 382 spdk_bdev_free_io(bdev_io); 383 } 384 385 static void 386 put_channel_during_reset(void) 387 { 388 struct spdk_io_channel *io_ch; 389 bool done = false; 390 391 setup_test(); 392 393 set_thread(0); 394 io_ch = spdk_bdev_get_io_channel(g_desc); 395 CU_ASSERT(io_ch != NULL); 396 397 /* 398 * Start a reset, but then put the I/O channel before 399 * the deferred messages for the reset get a chance to 400 * execute. 401 */ 402 spdk_bdev_reset(g_desc, io_ch, reset_done, &done); 403 spdk_put_io_channel(io_ch); 404 poll_threads(); 405 stub_complete_io(g_bdev.io_target, 0); 406 407 teardown_test(); 408 } 409 410 static void 411 aborted_reset_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 412 { 413 enum spdk_bdev_io_status *status = cb_arg; 414 415 *status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED; 416 spdk_bdev_free_io(bdev_io); 417 } 418 419 static void 420 aborted_reset(void) 421 { 422 struct spdk_io_channel *io_ch[2]; 423 enum spdk_bdev_io_status status1, status2; 424 425 setup_test(); 426 427 set_thread(0); 428 io_ch[0] = spdk_bdev_get_io_channel(g_desc); 429 CU_ASSERT(io_ch[0] != NULL); 430 spdk_bdev_reset(g_desc, io_ch[0], aborted_reset_done, &status1); 431 poll_threads(); 432 CU_ASSERT(g_bdev.bdev.reset_in_progress != NULL); 433 434 /* 435 * First reset has been submitted on ch0. Now submit a second 436 * reset on ch1 which will get queued since there is already a 437 * reset in progress. 438 */ 439 set_thread(1); 440 io_ch[1] = spdk_bdev_get_io_channel(g_desc); 441 CU_ASSERT(io_ch[1] != NULL); 442 spdk_bdev_reset(g_desc, io_ch[1], aborted_reset_done, &status2); 443 poll_threads(); 444 CU_ASSERT(g_bdev.bdev.reset_in_progress != NULL); 445 446 /* 447 * Now destroy ch1. This will abort the queued reset. Check that 448 * the second reset was completed with failed status. Also check 449 * that bdev->reset_in_progress != NULL, since the original reset 450 * has not been completed yet. This ensures that the bdev code is 451 * correctly noticing that the failed reset is *not* the one that 452 * had been submitted to the bdev module. 453 */ 454 set_thread(1); 455 spdk_put_io_channel(io_ch[1]); 456 poll_threads(); 457 CU_ASSERT(status2 == SPDK_BDEV_IO_STATUS_FAILED); 458 CU_ASSERT(g_bdev.bdev.reset_in_progress != NULL); 459 460 /* 461 * Now complete the first reset, verify that it completed with SUCCESS 462 * status and that bdev->reset_in_progress is also set back to NULL. 463 */ 464 set_thread(0); 465 spdk_put_io_channel(io_ch[0]); 466 stub_complete_io(g_bdev.io_target, 0); 467 poll_threads(); 468 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS); 469 CU_ASSERT(g_bdev.bdev.reset_in_progress == NULL); 470 471 teardown_test(); 472 } 473 474 static void 475 io_during_io_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 476 { 477 enum spdk_bdev_io_status *status = cb_arg; 478 479 *status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED; 480 spdk_bdev_free_io(bdev_io); 481 } 482 483 static void 484 io_during_reset(void) 485 { 486 struct spdk_io_channel *io_ch[2]; 487 struct spdk_bdev_channel *bdev_ch[2]; 488 enum spdk_bdev_io_status status0, status1, status_reset; 489 int rc; 490 491 setup_test(); 492 493 /* 494 * First test normal case - submit an I/O on each of two channels (with no resets) 495 * and verify they complete successfully. 496 */ 497 set_thread(0); 498 io_ch[0] = spdk_bdev_get_io_channel(g_desc); 499 bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]); 500 CU_ASSERT(bdev_ch[0]->flags == 0); 501 status0 = SPDK_BDEV_IO_STATUS_PENDING; 502 rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0); 503 CU_ASSERT(rc == 0); 504 505 set_thread(1); 506 io_ch[1] = spdk_bdev_get_io_channel(g_desc); 507 bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]); 508 CU_ASSERT(bdev_ch[1]->flags == 0); 509 status1 = SPDK_BDEV_IO_STATUS_PENDING; 510 rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1); 511 CU_ASSERT(rc == 0); 512 513 poll_threads(); 514 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_PENDING); 515 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING); 516 517 set_thread(0); 518 stub_complete_io(g_bdev.io_target, 0); 519 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_SUCCESS); 520 521 set_thread(1); 522 stub_complete_io(g_bdev.io_target, 0); 523 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS); 524 525 /* 526 * Now submit a reset, and leave it pending while we submit I/O on two different 527 * channels. These I/O should be failed by the bdev layer since the reset is in 528 * progress. 529 */ 530 set_thread(0); 531 status_reset = SPDK_BDEV_IO_STATUS_PENDING; 532 rc = spdk_bdev_reset(g_desc, io_ch[0], io_during_io_done, &status_reset); 533 CU_ASSERT(rc == 0); 534 535 CU_ASSERT(bdev_ch[0]->flags == 0); 536 CU_ASSERT(bdev_ch[1]->flags == 0); 537 poll_threads(); 538 CU_ASSERT(bdev_ch[0]->flags == BDEV_CH_RESET_IN_PROGRESS); 539 CU_ASSERT(bdev_ch[1]->flags == BDEV_CH_RESET_IN_PROGRESS); 540 541 set_thread(0); 542 status0 = SPDK_BDEV_IO_STATUS_PENDING; 543 rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0); 544 CU_ASSERT(rc == 0); 545 546 set_thread(1); 547 status1 = SPDK_BDEV_IO_STATUS_PENDING; 548 rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1); 549 CU_ASSERT(rc == 0); 550 551 /* 552 * A reset is in progress so these read I/O should complete with failure. Note that we 553 * need to poll_threads() since I/O completed inline have their completion deferred. 554 */ 555 poll_threads(); 556 CU_ASSERT(status_reset == SPDK_BDEV_IO_STATUS_PENDING); 557 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_FAILED); 558 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_FAILED); 559 560 /* 561 * Complete the reset 562 */ 563 set_thread(0); 564 stub_complete_io(g_bdev.io_target, 0); 565 566 /* 567 * Only poll thread 0. We should not get a completion. 568 */ 569 poll_thread(0); 570 CU_ASSERT(status_reset == SPDK_BDEV_IO_STATUS_PENDING); 571 572 /* 573 * Poll both thread 0 and 1 so the messages can propagate and we 574 * get a completion. 575 */ 576 poll_threads(); 577 CU_ASSERT(status_reset == SPDK_BDEV_IO_STATUS_SUCCESS); 578 579 spdk_put_io_channel(io_ch[0]); 580 set_thread(1); 581 spdk_put_io_channel(io_ch[1]); 582 poll_threads(); 583 584 teardown_test(); 585 } 586 587 static void 588 basic_qos(void) 589 { 590 struct spdk_io_channel *io_ch[3]; 591 struct spdk_bdev_channel *bdev_ch[3], *qos_bdev_ch; 592 struct spdk_bdev *bdev; 593 enum spdk_bdev_io_status status; 594 struct spdk_bdev_module_channel *module_ch; 595 int rc; 596 597 setup_test(); 598 599 /* 600 * First test normal case - submit an I/O on the channel (QoS not enabled) 601 * and verify it completes successfully. 602 */ 603 set_thread(0); 604 g_get_io_channel = false; 605 io_ch[0] = spdk_bdev_get_io_channel(g_desc); 606 CU_ASSERT(io_ch[0] == NULL); 607 g_get_io_channel = true; 608 io_ch[0] = spdk_bdev_get_io_channel(g_desc); 609 bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]); 610 status = SPDK_BDEV_IO_STATUS_PENDING; 611 rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status); 612 CU_ASSERT(rc == 0); 613 CU_ASSERT(bdev_ch[0]->flags == 0); 614 615 CU_ASSERT(status == SPDK_BDEV_IO_STATUS_PENDING); 616 617 set_thread(0); 618 stub_complete_io(g_bdev.io_target, 0); 619 CU_ASSERT(status == SPDK_BDEV_IO_STATUS_SUCCESS); 620 621 poll_threads(); 622 623 set_thread(1); 624 bdev = &g_bdev.bdev; 625 bdev->ios_per_sec = 2000; 626 g_get_io_channel = false; 627 io_ch[1] = spdk_bdev_get_io_channel(g_desc); 628 CU_ASSERT(io_ch[1] == NULL); 629 bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]); 630 qos_bdev_ch = bdev->qos_channel; 631 CU_ASSERT(qos_bdev_ch == NULL); 632 g_get_io_channel = true; 633 io_ch[1] = spdk_bdev_get_io_channel(g_desc); 634 bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]); 635 qos_bdev_ch = bdev->qos_channel; 636 CU_ASSERT(bdev->qos_channel->flags == BDEV_CH_QOS_ENABLED); 637 CU_ASSERT(qos_bdev_ch != NULL); 638 module_ch = qos_bdev_ch->module_ch; 639 CU_ASSERT(module_ch->io_outstanding == 0); 640 CU_ASSERT(g_ut_threads[1].thread == bdev->qos_thread); 641 642 /* 643 * Now sending one I/O on first channel 644 */ 645 set_thread(0); 646 status = SPDK_BDEV_IO_STATUS_PENDING; 647 rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status); 648 CU_ASSERT(rc == 0); 649 650 poll_threads(); 651 CU_ASSERT(module_ch->io_outstanding == 1); 652 CU_ASSERT(status == SPDK_BDEV_IO_STATUS_PENDING); 653 654 /* 655 * IO is operated on thread_id(1) via the QoS thread 656 */ 657 set_thread(1); 658 stub_complete_io(g_bdev.io_target, 1); 659 660 poll_threads(); 661 CU_ASSERT(status == SPDK_BDEV_IO_STATUS_SUCCESS); 662 663 /* 664 * QoS thread is on thread 1. Put I/O channel on thread 1 first 665 * to trigger an async destruction of QoS bdev channel. 666 */ 667 set_thread(1); 668 spdk_put_io_channel(io_ch[0]); 669 set_thread(0); 670 spdk_put_io_channel(io_ch[1]); 671 672 /* 673 * Handle the messages on thread 1 first so that the QoS bdev 674 * channel destroy message from thread 0 handling will be active 675 * there. 676 */ 677 poll_thread(1); 678 poll_thread(0); 679 680 /* 681 * Create a new I/O channel when the async destruction of QoS 682 * bdev channel is on going. The expected result is the QoS bdev 683 * channel will be properly setup again. 684 */ 685 set_thread(2); 686 io_ch[2] = spdk_bdev_get_io_channel(g_desc); 687 bdev_ch[2] = spdk_io_channel_get_ctx(io_ch[2]); 688 689 poll_threads(); 690 691 qos_bdev_ch = bdev->qos_channel; 692 CU_ASSERT(qos_bdev_ch->flags == BDEV_CH_QOS_ENABLED); 693 CU_ASSERT(qos_bdev_ch != NULL); 694 module_ch = qos_bdev_ch->module_ch; 695 CU_ASSERT(module_ch->io_outstanding == 0); 696 CU_ASSERT(g_ut_threads[1].thread == bdev->qos_thread); 697 698 /* 699 * Destroy the last I/O channel so that the QoS bdev channel 700 * will be destroyed. 701 */ 702 set_thread(2); 703 spdk_put_io_channel(io_ch[2]); 704 705 poll_threads(); 706 707 teardown_test(); 708 } 709 710 static void 711 io_during_qos(void) 712 { 713 struct spdk_io_channel *io_ch[3]; 714 struct spdk_bdev_channel *bdev_ch[3], *qos_bdev_ch; 715 struct spdk_bdev *bdev; 716 enum spdk_bdev_io_status status0, status1; 717 struct spdk_bdev_module_channel *module_ch; 718 int rc; 719 720 setup_test(); 721 722 /* 723 * First test normal case - submit an I/O on each of two channels (QoS not enabled) 724 * and verify they complete successfully. 725 */ 726 set_thread(0); 727 io_ch[0] = spdk_bdev_get_io_channel(g_desc); 728 bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]); 729 status0 = SPDK_BDEV_IO_STATUS_PENDING; 730 rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0); 731 CU_ASSERT(rc == 0); 732 CU_ASSERT(bdev_ch[0]->flags == 0); 733 734 set_thread(1); 735 io_ch[1] = spdk_bdev_get_io_channel(g_desc); 736 bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]); 737 status1 = SPDK_BDEV_IO_STATUS_PENDING; 738 rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1); 739 CU_ASSERT(rc == 0); 740 CU_ASSERT(bdev_ch[1]->flags == 0); 741 742 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_PENDING); 743 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING); 744 745 set_thread(0); 746 stub_complete_io(g_bdev.io_target, 0); 747 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_SUCCESS); 748 749 set_thread(1); 750 stub_complete_io(g_bdev.io_target, 0); 751 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS); 752 753 poll_threads(); 754 755 set_thread(2); 756 bdev = &g_bdev.bdev; 757 /* 758 * 10 IOs allowed per millisecond 759 */ 760 bdev->ios_per_sec = 10000; 761 io_ch[2] = spdk_bdev_get_io_channel(g_desc); 762 bdev_ch[2] = spdk_io_channel_get_ctx(io_ch[2]); 763 qos_bdev_ch = bdev->qos_channel; 764 CU_ASSERT(bdev->qos_channel->flags == BDEV_CH_QOS_ENABLED); 765 CU_ASSERT(qos_bdev_ch != NULL); 766 module_ch = qos_bdev_ch->module_ch; 767 CU_ASSERT(module_ch->io_outstanding == 0); 768 769 /* 770 * Now sending some I/Os on different channels when QoS has been enabled 771 */ 772 set_thread(0); 773 status0 = SPDK_BDEV_IO_STATUS_PENDING; 774 rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0); 775 CU_ASSERT(rc == 0); 776 777 set_thread(1); 778 status1 = SPDK_BDEV_IO_STATUS_PENDING; 779 rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1); 780 CU_ASSERT(rc == 0); 781 782 poll_threads(); 783 CU_ASSERT(module_ch->io_outstanding == 2); 784 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_PENDING); 785 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING); 786 787 /* 788 * IOs are operated on thread_id(2) via the QoS thread 789 */ 790 set_thread(2); 791 stub_complete_io(g_bdev.io_target, 2); 792 793 poll_threads(); 794 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_SUCCESS); 795 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS); 796 797 set_thread(0); 798 spdk_put_io_channel(io_ch[0]); 799 set_thread(1); 800 spdk_put_io_channel(io_ch[1]); 801 set_thread(2); 802 spdk_put_io_channel(io_ch[2]); 803 804 poll_threads(); 805 806 teardown_test(); 807 } 808 809 static void 810 io_during_qos_queue(void) 811 { 812 struct spdk_io_channel *io_ch[3]; 813 struct spdk_bdev_channel *bdev_ch[3], *qos_bdev_ch; 814 struct spdk_bdev *bdev; 815 enum spdk_bdev_io_status status0, status1; 816 struct spdk_bdev_module_channel *module_ch; 817 int rc; 818 819 setup_test(); 820 reset_time(); 821 822 /* 823 * First test normal case - submit an I/O on each of two channels (QoS not enabled) 824 * and verify they complete successfully. 825 */ 826 set_thread(0); 827 io_ch[0] = spdk_bdev_get_io_channel(g_desc); 828 bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]); 829 status0 = SPDK_BDEV_IO_STATUS_PENDING; 830 rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0); 831 CU_ASSERT(rc == 0); 832 CU_ASSERT(bdev_ch[0]->flags == 0); 833 834 set_thread(1); 835 io_ch[1] = spdk_bdev_get_io_channel(g_desc); 836 bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]); 837 status1 = SPDK_BDEV_IO_STATUS_PENDING; 838 rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1); 839 CU_ASSERT(rc == 0); 840 CU_ASSERT(bdev_ch[1]->flags == 0); 841 842 poll_threads(); 843 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_PENDING); 844 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING); 845 846 set_thread(0); 847 stub_complete_io(g_bdev.io_target, 0); 848 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_SUCCESS); 849 850 set_thread(1); 851 stub_complete_io(g_bdev.io_target, 0); 852 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS); 853 854 poll_threads(); 855 856 set_thread(2); 857 bdev = bdev_ch[0]->bdev; 858 /* 859 * Only 1 IO allowed per millisecond. More IOs will be queued. 860 */ 861 bdev->ios_per_sec = 1000; 862 io_ch[2] = spdk_bdev_get_io_channel(g_desc); 863 bdev_ch[2] = spdk_io_channel_get_ctx(io_ch[2]); 864 qos_bdev_ch = bdev->qos_channel; 865 CU_ASSERT(bdev->qos_channel->flags == BDEV_CH_QOS_ENABLED); 866 CU_ASSERT(qos_bdev_ch != NULL); 867 module_ch = qos_bdev_ch->module_ch; 868 CU_ASSERT(module_ch->io_outstanding == 0); 869 870 /* 871 * Now sending some I/Os on different channels when QoS has been enabled 872 */ 873 set_thread(0); 874 status0 = SPDK_BDEV_IO_STATUS_PENDING; 875 rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0); 876 CU_ASSERT(rc == 0); 877 878 set_thread(1); 879 status1 = SPDK_BDEV_IO_STATUS_PENDING; 880 rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1); 881 CU_ASSERT(rc == 0); 882 883 /* 884 * Poll the QoS thread to send the allowed I/O down 885 */ 886 poll_threads(); 887 CU_ASSERT(module_ch->io_outstanding == 1); 888 CU_ASSERT(bdev_io_tailq_cnt(&qos_bdev_ch->qos_io) == 1); 889 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_PENDING); 890 891 /* 892 * Increase the time and poll the QoS thread to run the periodical poller 893 */ 894 increment_time(1000); 895 poll_threads(); 896 CU_ASSERT(module_ch->io_outstanding == 2); 897 CU_ASSERT(bdev_io_tailq_cnt(&qos_bdev_ch->qos_io) == 0); 898 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING); 899 900 /* 901 * IOs are handled on the thread(2) as the master thread 902 */ 903 set_thread(2); 904 stub_complete_io(g_bdev.io_target, 0); 905 spdk_put_io_channel(io_ch[0]); 906 spdk_put_io_channel(io_ch[1]); 907 spdk_put_io_channel(io_ch[2]); 908 909 poll_threads(); 910 911 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_SUCCESS); 912 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS); 913 914 teardown_test(); 915 } 916 917 static void 918 enomem_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 919 { 920 enum spdk_bdev_io_status *status = cb_arg; 921 922 *status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED; 923 spdk_bdev_free_io(bdev_io); 924 } 925 926 static void 927 enomem(void) 928 { 929 struct spdk_io_channel *io_ch; 930 struct spdk_bdev_channel *bdev_ch; 931 struct spdk_bdev_module_channel *module_ch; 932 struct ut_bdev_channel *ut_ch; 933 const uint32_t IO_ARRAY_SIZE = 64; 934 const uint32_t AVAIL = 20; 935 enum spdk_bdev_io_status status[IO_ARRAY_SIZE], status_reset; 936 uint32_t nomem_cnt, i; 937 struct spdk_bdev_io *first_io; 938 int rc; 939 940 setup_test(); 941 942 set_thread(0); 943 io_ch = spdk_bdev_get_io_channel(g_desc); 944 bdev_ch = spdk_io_channel_get_ctx(io_ch); 945 module_ch = bdev_ch->module_ch; 946 ut_ch = spdk_io_channel_get_ctx(bdev_ch->channel); 947 ut_ch->avail_cnt = AVAIL; 948 949 /* First submit a number of IOs equal to what the channel can support. */ 950 for (i = 0; i < AVAIL; i++) { 951 status[i] = SPDK_BDEV_IO_STATUS_PENDING; 952 rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[i]); 953 CU_ASSERT(rc == 0); 954 } 955 CU_ASSERT(TAILQ_EMPTY(&module_ch->nomem_io)); 956 957 /* 958 * Next, submit one additional I/O. This one should fail with ENOMEM and then go onto 959 * the enomem_io list. 960 */ 961 status[AVAIL] = SPDK_BDEV_IO_STATUS_PENDING; 962 rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[AVAIL]); 963 CU_ASSERT(rc == 0); 964 SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&module_ch->nomem_io)); 965 first_io = TAILQ_FIRST(&module_ch->nomem_io); 966 967 /* 968 * Now submit a bunch more I/O. These should all fail with ENOMEM and get queued behind 969 * the first_io above. 970 */ 971 for (i = AVAIL + 1; i < IO_ARRAY_SIZE; i++) { 972 status[i] = SPDK_BDEV_IO_STATUS_PENDING; 973 rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[i]); 974 CU_ASSERT(rc == 0); 975 } 976 977 /* Assert that first_io is still at the head of the list. */ 978 CU_ASSERT(TAILQ_FIRST(&module_ch->nomem_io) == first_io); 979 CU_ASSERT(bdev_io_tailq_cnt(&module_ch->nomem_io) == (IO_ARRAY_SIZE - AVAIL)); 980 nomem_cnt = bdev_io_tailq_cnt(&module_ch->nomem_io); 981 CU_ASSERT(module_ch->nomem_threshold == (AVAIL - NOMEM_THRESHOLD_COUNT)); 982 983 /* 984 * Complete 1 I/O only. The key check here is bdev_io_tailq_cnt - this should not have 985 * changed since completing just 1 I/O should not trigger retrying the queued nomem_io 986 * list. 987 */ 988 stub_complete_io(g_bdev.io_target, 1); 989 CU_ASSERT(bdev_io_tailq_cnt(&module_ch->nomem_io) == nomem_cnt); 990 991 /* 992 * Complete enough I/O to hit the nomem_theshold. This should trigger retrying nomem_io, 993 * and we should see I/O get resubmitted to the test bdev module. 994 */ 995 stub_complete_io(g_bdev.io_target, NOMEM_THRESHOLD_COUNT - 1); 996 CU_ASSERT(bdev_io_tailq_cnt(&module_ch->nomem_io) < nomem_cnt); 997 nomem_cnt = bdev_io_tailq_cnt(&module_ch->nomem_io); 998 999 /* Complete 1 I/O only. This should not trigger retrying the queued nomem_io. */ 1000 stub_complete_io(g_bdev.io_target, 1); 1001 CU_ASSERT(bdev_io_tailq_cnt(&module_ch->nomem_io) == nomem_cnt); 1002 1003 /* 1004 * Send a reset and confirm that all I/O are completed, including the ones that 1005 * were queued on the nomem_io list. 1006 */ 1007 status_reset = SPDK_BDEV_IO_STATUS_PENDING; 1008 rc = spdk_bdev_reset(g_desc, io_ch, enomem_done, &status_reset); 1009 poll_threads(); 1010 CU_ASSERT(rc == 0); 1011 /* This will complete the reset. */ 1012 stub_complete_io(g_bdev.io_target, 0); 1013 1014 CU_ASSERT(bdev_io_tailq_cnt(&module_ch->nomem_io) == 0); 1015 CU_ASSERT(module_ch->io_outstanding == 0); 1016 1017 spdk_put_io_channel(io_ch); 1018 poll_threads(); 1019 teardown_test(); 1020 } 1021 1022 static void 1023 enomem_multi_bdev(void) 1024 { 1025 struct spdk_io_channel *io_ch; 1026 struct spdk_bdev_channel *bdev_ch; 1027 struct spdk_bdev_module_channel *module_ch; 1028 struct ut_bdev_channel *ut_ch; 1029 const uint32_t IO_ARRAY_SIZE = 64; 1030 const uint32_t AVAIL = 20; 1031 enum spdk_bdev_io_status status[IO_ARRAY_SIZE]; 1032 uint32_t i; 1033 struct ut_bdev *second_bdev; 1034 struct spdk_bdev_desc *second_desc; 1035 struct spdk_bdev_channel *second_bdev_ch; 1036 struct spdk_io_channel *second_ch; 1037 int rc; 1038 1039 setup_test(); 1040 1041 /* Register second bdev with the same io_target */ 1042 second_bdev = calloc(1, sizeof(*second_bdev)); 1043 SPDK_CU_ASSERT_FATAL(second_bdev != NULL); 1044 register_bdev(second_bdev, "ut_bdev2", g_bdev.io_target); 1045 spdk_bdev_open(&second_bdev->bdev, true, NULL, NULL, &second_desc); 1046 1047 set_thread(0); 1048 io_ch = spdk_bdev_get_io_channel(g_desc); 1049 bdev_ch = spdk_io_channel_get_ctx(io_ch); 1050 module_ch = bdev_ch->module_ch; 1051 ut_ch = spdk_io_channel_get_ctx(bdev_ch->channel); 1052 ut_ch->avail_cnt = AVAIL; 1053 1054 second_ch = spdk_bdev_get_io_channel(second_desc); 1055 second_bdev_ch = spdk_io_channel_get_ctx(second_ch); 1056 SPDK_CU_ASSERT_FATAL(module_ch == second_bdev_ch->module_ch); 1057 1058 /* Saturate io_target through bdev A. */ 1059 for (i = 0; i < AVAIL; i++) { 1060 status[i] = SPDK_BDEV_IO_STATUS_PENDING; 1061 rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[i]); 1062 CU_ASSERT(rc == 0); 1063 } 1064 CU_ASSERT(TAILQ_EMPTY(&module_ch->nomem_io)); 1065 1066 /* 1067 * Now submit I/O through the second bdev. This should fail with ENOMEM 1068 * and then go onto the nomem_io list. 1069 */ 1070 status[AVAIL] = SPDK_BDEV_IO_STATUS_PENDING; 1071 rc = spdk_bdev_read_blocks(second_desc, second_ch, NULL, 0, 1, enomem_done, &status[AVAIL]); 1072 CU_ASSERT(rc == 0); 1073 SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&module_ch->nomem_io)); 1074 1075 /* Complete first bdev's I/O. This should retry sending second bdev's nomem_io */ 1076 stub_complete_io(g_bdev.io_target, AVAIL); 1077 1078 SPDK_CU_ASSERT_FATAL(TAILQ_EMPTY(&module_ch->nomem_io)); 1079 CU_ASSERT(module_ch->io_outstanding == 1); 1080 1081 /* Now complete our retried I/O */ 1082 stub_complete_io(g_bdev.io_target, 1); 1083 SPDK_CU_ASSERT_FATAL(module_ch->io_outstanding == 0); 1084 1085 spdk_put_io_channel(io_ch); 1086 spdk_put_io_channel(second_ch); 1087 spdk_bdev_close(second_desc); 1088 unregister_bdev(second_bdev); 1089 free(second_bdev); 1090 poll_threads(); 1091 teardown_test(); 1092 } 1093 1094 int 1095 main(int argc, char **argv) 1096 { 1097 CU_pSuite suite = NULL; 1098 unsigned int num_failures; 1099 1100 if (CU_initialize_registry() != CUE_SUCCESS) { 1101 return CU_get_error(); 1102 } 1103 1104 suite = CU_add_suite("bdev", NULL, NULL); 1105 if (suite == NULL) { 1106 CU_cleanup_registry(); 1107 return CU_get_error(); 1108 } 1109 1110 if ( 1111 CU_add_test(suite, "basic", basic) == NULL || 1112 CU_add_test(suite, "basic_poller", basic_poller) == NULL || 1113 CU_add_test(suite, "basic_qos", basic_qos) == NULL || 1114 CU_add_test(suite, "put_channel_during_reset", put_channel_during_reset) == NULL || 1115 CU_add_test(suite, "aborted_reset", aborted_reset) == NULL || 1116 CU_add_test(suite, "io_during_reset", io_during_reset) == NULL || 1117 CU_add_test(suite, "io_during_qos", io_during_qos) == NULL || 1118 CU_add_test(suite, "io_during_qos_queue", io_during_qos_queue) == NULL || 1119 CU_add_test(suite, "enomem", enomem) == NULL || 1120 CU_add_test(suite, "enomem_multi_bdev", enomem_multi_bdev) == NULL 1121 ) { 1122 CU_cleanup_registry(); 1123 return CU_get_error(); 1124 } 1125 1126 CU_basic_set_mode(CU_BRM_VERBOSE); 1127 CU_basic_run_tests(); 1128 num_failures = CU_get_number_of_failures(); 1129 CU_cleanup_registry(); 1130 return num_failures; 1131 } 1132