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 SPDK_BDEV_MODULE_REGISTER(bdev_ut, module_init, module_fini, NULL, NULL, NULL) 180 181 static void 182 register_bdev(struct ut_bdev *ut_bdev, char *name, void *io_target) 183 { 184 memset(ut_bdev, 0, sizeof(*ut_bdev)); 185 186 ut_bdev->io_target = io_target; 187 ut_bdev->bdev.ctxt = ut_bdev; 188 ut_bdev->bdev.name = name; 189 ut_bdev->bdev.fn_table = &fn_table; 190 ut_bdev->bdev.module = SPDK_GET_BDEV_MODULE(bdev_ut); 191 ut_bdev->bdev.blocklen = 4096; 192 ut_bdev->bdev.blockcnt = 1024; 193 194 spdk_bdev_register(&ut_bdev->bdev); 195 } 196 197 static void 198 unregister_bdev(struct ut_bdev *ut_bdev) 199 { 200 /* Handle any deferred messages. */ 201 poll_threads(); 202 spdk_bdev_unregister(&ut_bdev->bdev, NULL, NULL); 203 memset(ut_bdev, 0, sizeof(*ut_bdev)); 204 } 205 206 static void 207 bdev_init_cb(void *done, int rc) 208 { 209 CU_ASSERT(rc == 0); 210 *(bool *)done = true; 211 } 212 213 static void 214 setup_test(void) 215 { 216 bool done = false; 217 218 allocate_threads(BDEV_UT_NUM_THREADS); 219 spdk_bdev_initialize(bdev_init_cb, &done); 220 spdk_io_device_register(&g_io_device, stub_create_ch, stub_destroy_ch, 221 sizeof(struct ut_bdev_channel)); 222 register_bdev(&g_bdev, "ut_bdev", &g_io_device); 223 spdk_bdev_open(&g_bdev.bdev, true, NULL, NULL, &g_desc); 224 } 225 226 static void 227 finish_cb(void *cb_arg) 228 { 229 g_teardown_done = true; 230 } 231 232 static void 233 teardown_test(void) 234 { 235 g_teardown_done = false; 236 spdk_bdev_close(g_desc); 237 g_desc = NULL; 238 unregister_bdev(&g_bdev); 239 spdk_io_device_unregister(&g_io_device, NULL); 240 spdk_bdev_finish(finish_cb, NULL); 241 poll_threads(); 242 CU_ASSERT(g_teardown_done == true); 243 g_teardown_done = false; 244 free_threads(); 245 } 246 247 static void 248 basic(void) 249 { 250 setup_test(); 251 252 set_thread(0); 253 254 g_get_io_channel = false; 255 g_ut_threads[0].ch = spdk_bdev_get_io_channel(g_desc); 256 CU_ASSERT(g_ut_threads[0].ch == NULL); 257 258 g_get_io_channel = true; 259 g_create_ch = false; 260 g_ut_threads[0].ch = spdk_bdev_get_io_channel(g_desc); 261 CU_ASSERT(g_ut_threads[0].ch == NULL); 262 263 g_get_io_channel = true; 264 g_create_ch = true; 265 g_ut_threads[0].ch = spdk_bdev_get_io_channel(g_desc); 266 CU_ASSERT(g_ut_threads[0].ch != NULL); 267 spdk_put_io_channel(g_ut_threads[0].ch); 268 269 teardown_test(); 270 } 271 272 static void 273 poller_run_done(void *ctx) 274 { 275 bool *poller_run = ctx; 276 277 *poller_run = true; 278 } 279 280 static void 281 poller_run_times_done(void *ctx) 282 { 283 int *poller_run_times = ctx; 284 285 (*poller_run_times)++; 286 } 287 288 static void 289 basic_poller(void) 290 { 291 struct spdk_poller *poller = NULL; 292 bool poller_run = false; 293 int poller_run_times = 0; 294 295 setup_test(); 296 297 set_thread(0); 298 reset_time(); 299 /* Register a poller with no-wait time and test execution */ 300 poller = spdk_poller_register(poller_run_done, &poller_run, 0); 301 CU_ASSERT(poller != NULL); 302 303 poll_threads(); 304 CU_ASSERT(poller_run == true); 305 306 spdk_poller_unregister(&poller); 307 CU_ASSERT(poller == NULL); 308 309 /* Register a poller with 1000us wait time and test single execution */ 310 poller_run = false; 311 poller = spdk_poller_register(poller_run_done, &poller_run, 1000); 312 CU_ASSERT(poller != NULL); 313 314 poll_threads(); 315 CU_ASSERT(poller_run == false); 316 317 increment_time(1000); 318 poll_threads(); 319 CU_ASSERT(poller_run == true); 320 321 reset_time(); 322 poller_run = false; 323 poll_threads(); 324 CU_ASSERT(poller_run == false); 325 326 increment_time(1000); 327 poll_threads(); 328 CU_ASSERT(poller_run == true); 329 330 spdk_poller_unregister(&poller); 331 CU_ASSERT(poller == NULL); 332 333 reset_time(); 334 /* Register a poller with 1000us wait time and test multiple execution */ 335 poller = spdk_poller_register(poller_run_times_done, &poller_run_times, 1000); 336 CU_ASSERT(poller != NULL); 337 338 poll_threads(); 339 CU_ASSERT(poller_run_times == 0); 340 341 increment_time(1000); 342 poll_threads(); 343 CU_ASSERT(poller_run_times == 1); 344 345 poller_run_times = 0; 346 increment_time(2000); 347 poll_threads(); 348 CU_ASSERT(poller_run_times == 2); 349 350 spdk_poller_unregister(&poller); 351 CU_ASSERT(poller == NULL); 352 353 teardown_test(); 354 } 355 356 static void 357 reset_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 358 { 359 bool *done = cb_arg; 360 361 CU_ASSERT(success == true); 362 *done = true; 363 spdk_bdev_free_io(bdev_io); 364 } 365 366 static void 367 put_channel_during_reset(void) 368 { 369 struct spdk_io_channel *io_ch; 370 bool done = false; 371 372 setup_test(); 373 374 set_thread(0); 375 io_ch = spdk_bdev_get_io_channel(g_desc); 376 CU_ASSERT(io_ch != NULL); 377 378 /* 379 * Start a reset, but then put the I/O channel before 380 * the deferred messages for the reset get a chance to 381 * execute. 382 */ 383 spdk_bdev_reset(g_desc, io_ch, reset_done, &done); 384 spdk_put_io_channel(io_ch); 385 poll_threads(); 386 stub_complete_io(g_bdev.io_target, 0); 387 388 teardown_test(); 389 } 390 391 static void 392 aborted_reset_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 393 { 394 enum spdk_bdev_io_status *status = cb_arg; 395 396 *status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED; 397 spdk_bdev_free_io(bdev_io); 398 } 399 400 static void 401 aborted_reset(void) 402 { 403 struct spdk_io_channel *io_ch[2]; 404 enum spdk_bdev_io_status status1, status2; 405 406 setup_test(); 407 408 set_thread(0); 409 io_ch[0] = spdk_bdev_get_io_channel(g_desc); 410 CU_ASSERT(io_ch[0] != NULL); 411 spdk_bdev_reset(g_desc, io_ch[0], aborted_reset_done, &status1); 412 poll_threads(); 413 CU_ASSERT(g_bdev.bdev.reset_in_progress != NULL); 414 415 /* 416 * First reset has been submitted on ch0. Now submit a second 417 * reset on ch1 which will get queued since there is already a 418 * reset in progress. 419 */ 420 set_thread(1); 421 io_ch[1] = spdk_bdev_get_io_channel(g_desc); 422 CU_ASSERT(io_ch[1] != NULL); 423 spdk_bdev_reset(g_desc, io_ch[1], aborted_reset_done, &status2); 424 poll_threads(); 425 CU_ASSERT(g_bdev.bdev.reset_in_progress != NULL); 426 427 /* 428 * Now destroy ch1. This will abort the queued reset. Check that 429 * the second reset was completed with failed status. Also check 430 * that bdev->reset_in_progress != NULL, since the original reset 431 * has not been completed yet. This ensures that the bdev code is 432 * correctly noticing that the failed reset is *not* the one that 433 * had been submitted to the bdev module. 434 */ 435 set_thread(1); 436 spdk_put_io_channel(io_ch[1]); 437 poll_threads(); 438 CU_ASSERT(status2 == SPDK_BDEV_IO_STATUS_FAILED); 439 CU_ASSERT(g_bdev.bdev.reset_in_progress != NULL); 440 441 /* 442 * Now complete the first reset, verify that it completed with SUCCESS 443 * status and that bdev->reset_in_progress is also set back to NULL. 444 */ 445 set_thread(0); 446 spdk_put_io_channel(io_ch[0]); 447 stub_complete_io(g_bdev.io_target, 0); 448 poll_threads(); 449 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS); 450 CU_ASSERT(g_bdev.bdev.reset_in_progress == NULL); 451 452 teardown_test(); 453 } 454 455 static void 456 io_during_reset_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 457 { 458 enum spdk_bdev_io_status *status = cb_arg; 459 460 *status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED; 461 spdk_bdev_free_io(bdev_io); 462 } 463 464 static void 465 io_during_reset(void) 466 { 467 struct spdk_io_channel *io_ch[2]; 468 struct spdk_bdev_channel *bdev_ch[2]; 469 enum spdk_bdev_io_status status0, status1, status_reset; 470 int rc; 471 472 setup_test(); 473 474 /* 475 * First test normal case - submit an I/O on each of two channels (with no resets) 476 * and verify they complete successfully. 477 */ 478 set_thread(0); 479 io_ch[0] = spdk_bdev_get_io_channel(g_desc); 480 bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]); 481 CU_ASSERT(bdev_ch[0]->flags == 0); 482 status0 = SPDK_BDEV_IO_STATUS_PENDING; 483 rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_reset_done, &status0); 484 CU_ASSERT(rc == 0); 485 486 set_thread(1); 487 io_ch[1] = spdk_bdev_get_io_channel(g_desc); 488 bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]); 489 CU_ASSERT(bdev_ch[1]->flags == 0); 490 status1 = SPDK_BDEV_IO_STATUS_PENDING; 491 rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_reset_done, &status1); 492 CU_ASSERT(rc == 0); 493 494 poll_threads(); 495 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_PENDING); 496 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING); 497 498 set_thread(0); 499 stub_complete_io(g_bdev.io_target, 0); 500 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_SUCCESS); 501 502 set_thread(1); 503 stub_complete_io(g_bdev.io_target, 0); 504 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS); 505 506 /* 507 * Now submit a reset, and leave it pending while we submit I/O on two different 508 * channels. These I/O should be failed by the bdev layer since the reset is in 509 * progress. 510 */ 511 set_thread(0); 512 status_reset = SPDK_BDEV_IO_STATUS_PENDING; 513 rc = spdk_bdev_reset(g_desc, io_ch[0], io_during_reset_done, &status_reset); 514 CU_ASSERT(rc == 0); 515 516 CU_ASSERT(bdev_ch[0]->flags == 0); 517 CU_ASSERT(bdev_ch[1]->flags == 0); 518 poll_threads(); 519 CU_ASSERT(bdev_ch[0]->flags == BDEV_CH_RESET_IN_PROGRESS); 520 CU_ASSERT(bdev_ch[1]->flags == BDEV_CH_RESET_IN_PROGRESS); 521 522 set_thread(0); 523 status0 = SPDK_BDEV_IO_STATUS_PENDING; 524 rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_reset_done, &status0); 525 CU_ASSERT(rc == 0); 526 527 set_thread(1); 528 status1 = SPDK_BDEV_IO_STATUS_PENDING; 529 rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_reset_done, &status1); 530 CU_ASSERT(rc == 0); 531 532 /* 533 * A reset is in progress so these read I/O should complete with failure. Note that we 534 * need to poll_threads() since I/O completed inline have their completion deferred. 535 */ 536 poll_threads(); 537 CU_ASSERT(status_reset == SPDK_BDEV_IO_STATUS_PENDING); 538 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_FAILED); 539 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_FAILED); 540 541 /* 542 * Complete the reset 543 */ 544 set_thread(0); 545 stub_complete_io(g_bdev.io_target, 0); 546 547 /* 548 * Only poll thread 0. We should not get a completion. 549 */ 550 poll_thread(0); 551 CU_ASSERT(status_reset == SPDK_BDEV_IO_STATUS_PENDING); 552 553 /* 554 * Poll both thread 0 and 1 so the messages can propagate and we 555 * get a completion. 556 */ 557 poll_threads(); 558 CU_ASSERT(status_reset == SPDK_BDEV_IO_STATUS_SUCCESS); 559 560 spdk_put_io_channel(io_ch[0]); 561 set_thread(1); 562 spdk_put_io_channel(io_ch[1]); 563 poll_threads(); 564 565 teardown_test(); 566 } 567 568 static void 569 enomem_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 570 { 571 enum spdk_bdev_io_status *status = cb_arg; 572 573 *status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED; 574 spdk_bdev_free_io(bdev_io); 575 } 576 577 static uint32_t 578 bdev_io_tailq_cnt(bdev_io_tailq_t *tailq) 579 { 580 struct spdk_bdev_io *io; 581 uint32_t cnt = 0; 582 583 TAILQ_FOREACH(io, tailq, link) { 584 cnt++; 585 } 586 587 return cnt; 588 } 589 590 static void 591 enomem(void) 592 { 593 struct spdk_io_channel *io_ch; 594 struct spdk_bdev_channel *bdev_ch; 595 struct spdk_bdev_module_channel *module_ch; 596 struct ut_bdev_channel *ut_ch; 597 const uint32_t IO_ARRAY_SIZE = 64; 598 const uint32_t AVAIL = 20; 599 enum spdk_bdev_io_status status[IO_ARRAY_SIZE], status_reset; 600 uint32_t nomem_cnt, i; 601 struct spdk_bdev_io *first_io; 602 int rc; 603 604 setup_test(); 605 606 set_thread(0); 607 io_ch = spdk_bdev_get_io_channel(g_desc); 608 bdev_ch = spdk_io_channel_get_ctx(io_ch); 609 module_ch = bdev_ch->module_ch; 610 ut_ch = spdk_io_channel_get_ctx(bdev_ch->channel); 611 ut_ch->avail_cnt = AVAIL; 612 613 /* First submit a number of IOs equal to what the channel can support. */ 614 for (i = 0; i < AVAIL; i++) { 615 status[i] = SPDK_BDEV_IO_STATUS_PENDING; 616 rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[i]); 617 CU_ASSERT(rc == 0); 618 } 619 CU_ASSERT(TAILQ_EMPTY(&module_ch->nomem_io)); 620 621 /* 622 * Next, submit one additional I/O. This one should fail with ENOMEM and then go onto 623 * the enomem_io list. 624 */ 625 status[AVAIL] = SPDK_BDEV_IO_STATUS_PENDING; 626 rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[AVAIL]); 627 CU_ASSERT(rc == 0); 628 SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&module_ch->nomem_io)); 629 first_io = TAILQ_FIRST(&module_ch->nomem_io); 630 631 /* 632 * Now submit a bunch more I/O. These should all fail with ENOMEM and get queued behind 633 * the first_io above. 634 */ 635 for (i = AVAIL + 1; i < IO_ARRAY_SIZE; i++) { 636 status[i] = SPDK_BDEV_IO_STATUS_PENDING; 637 rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[i]); 638 CU_ASSERT(rc == 0); 639 } 640 641 /* Assert that first_io is still at the head of the list. */ 642 CU_ASSERT(TAILQ_FIRST(&module_ch->nomem_io) == first_io); 643 CU_ASSERT(bdev_io_tailq_cnt(&module_ch->nomem_io) == (IO_ARRAY_SIZE - AVAIL)); 644 nomem_cnt = bdev_io_tailq_cnt(&module_ch->nomem_io); 645 CU_ASSERT(module_ch->nomem_threshold == (AVAIL - NOMEM_THRESHOLD_COUNT)); 646 647 /* 648 * Complete 1 I/O only. The key check here is bdev_io_tailq_cnt - this should not have 649 * changed since completing just 1 I/O should not trigger retrying the queued nomem_io 650 * list. 651 */ 652 stub_complete_io(g_bdev.io_target, 1); 653 CU_ASSERT(bdev_io_tailq_cnt(&module_ch->nomem_io) == nomem_cnt); 654 655 /* 656 * Complete enough I/O to hit the nomem_theshold. This should trigger retrying nomem_io, 657 * and we should see I/O get resubmitted to the test bdev module. 658 */ 659 stub_complete_io(g_bdev.io_target, NOMEM_THRESHOLD_COUNT - 1); 660 CU_ASSERT(bdev_io_tailq_cnt(&module_ch->nomem_io) < nomem_cnt); 661 nomem_cnt = bdev_io_tailq_cnt(&module_ch->nomem_io); 662 663 /* Complete 1 I/O only. This should not trigger retrying the queued nomem_io. */ 664 stub_complete_io(g_bdev.io_target, 1); 665 CU_ASSERT(bdev_io_tailq_cnt(&module_ch->nomem_io) == nomem_cnt); 666 667 /* 668 * Send a reset and confirm that all I/O are completed, including the ones that 669 * were queued on the nomem_io list. 670 */ 671 status_reset = SPDK_BDEV_IO_STATUS_PENDING; 672 rc = spdk_bdev_reset(g_desc, io_ch, enomem_done, &status_reset); 673 poll_threads(); 674 CU_ASSERT(rc == 0); 675 /* This will complete the reset. */ 676 stub_complete_io(g_bdev.io_target, 0); 677 678 CU_ASSERT(bdev_io_tailq_cnt(&module_ch->nomem_io) == 0); 679 CU_ASSERT(module_ch->io_outstanding == 0); 680 681 spdk_put_io_channel(io_ch); 682 poll_threads(); 683 teardown_test(); 684 } 685 686 static void 687 enomem_multi_bdev(void) 688 { 689 struct spdk_io_channel *io_ch; 690 struct spdk_bdev_channel *bdev_ch; 691 struct spdk_bdev_module_channel *module_ch; 692 struct ut_bdev_channel *ut_ch; 693 const uint32_t IO_ARRAY_SIZE = 64; 694 const uint32_t AVAIL = 20; 695 enum spdk_bdev_io_status status[IO_ARRAY_SIZE]; 696 uint32_t i; 697 struct ut_bdev *second_bdev; 698 struct spdk_bdev_desc *second_desc; 699 struct spdk_bdev_channel *second_bdev_ch; 700 struct spdk_io_channel *second_ch; 701 int rc; 702 703 setup_test(); 704 705 /* Register second bdev with the same io_target */ 706 second_bdev = calloc(1, sizeof(*second_bdev)); 707 SPDK_CU_ASSERT_FATAL(second_bdev != NULL); 708 register_bdev(second_bdev, "ut_bdev2", g_bdev.io_target); 709 spdk_bdev_open(&second_bdev->bdev, true, NULL, NULL, &second_desc); 710 711 set_thread(0); 712 io_ch = spdk_bdev_get_io_channel(g_desc); 713 bdev_ch = spdk_io_channel_get_ctx(io_ch); 714 module_ch = bdev_ch->module_ch; 715 ut_ch = spdk_io_channel_get_ctx(bdev_ch->channel); 716 ut_ch->avail_cnt = AVAIL; 717 718 second_ch = spdk_bdev_get_io_channel(second_desc); 719 second_bdev_ch = spdk_io_channel_get_ctx(second_ch); 720 SPDK_CU_ASSERT_FATAL(module_ch == second_bdev_ch->module_ch); 721 722 /* Saturate io_target through bdev A. */ 723 for (i = 0; i < AVAIL; i++) { 724 status[i] = SPDK_BDEV_IO_STATUS_PENDING; 725 rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[i]); 726 CU_ASSERT(rc == 0); 727 } 728 CU_ASSERT(TAILQ_EMPTY(&module_ch->nomem_io)); 729 730 /* 731 * Now submit I/O through the second bdev. This should fail with ENOMEM 732 * and then go onto the nomem_io list. 733 */ 734 status[AVAIL] = SPDK_BDEV_IO_STATUS_PENDING; 735 rc = spdk_bdev_read_blocks(second_desc, second_ch, NULL, 0, 1, enomem_done, &status[AVAIL]); 736 CU_ASSERT(rc == 0); 737 SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&module_ch->nomem_io)); 738 739 /* Complete first bdev's I/O. This should retry sending second bdev's nomem_io */ 740 stub_complete_io(g_bdev.io_target, AVAIL); 741 742 SPDK_CU_ASSERT_FATAL(TAILQ_EMPTY(&module_ch->nomem_io)); 743 CU_ASSERT(module_ch->io_outstanding == 1); 744 745 /* Now complete our retried I/O */ 746 stub_complete_io(g_bdev.io_target, 1); 747 SPDK_CU_ASSERT_FATAL(module_ch->io_outstanding == 0); 748 749 spdk_put_io_channel(io_ch); 750 spdk_put_io_channel(second_ch); 751 spdk_bdev_close(second_desc); 752 unregister_bdev(second_bdev); 753 free(second_bdev); 754 poll_threads(); 755 teardown_test(); 756 } 757 758 int 759 main(int argc, char **argv) 760 { 761 CU_pSuite suite = NULL; 762 unsigned int num_failures; 763 764 if (CU_initialize_registry() != CUE_SUCCESS) { 765 return CU_get_error(); 766 } 767 768 suite = CU_add_suite("bdev", NULL, NULL); 769 if (suite == NULL) { 770 CU_cleanup_registry(); 771 return CU_get_error(); 772 } 773 774 if ( 775 CU_add_test(suite, "basic", basic) == NULL || 776 CU_add_test(suite, "basic_poller", basic_poller) == NULL || 777 CU_add_test(suite, "put_channel_during_reset", put_channel_during_reset) == NULL || 778 CU_add_test(suite, "aborted_reset", aborted_reset) == NULL || 779 CU_add_test(suite, "io_during_reset", io_during_reset) == NULL || 780 CU_add_test(suite, "enomem", enomem) == NULL || 781 CU_add_test(suite, "enomem_multi_bdev", enomem_multi_bdev) == NULL 782 ) { 783 CU_cleanup_registry(); 784 return CU_get_error(); 785 } 786 787 CU_basic_set_mode(CU_BRM_VERBOSE); 788 CU_basic_run_tests(); 789 num_failures = CU_get_number_of_failures(); 790 CU_cleanup_registry(); 791 return num_failures; 792 } 793