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 "common/lib/ut_multithread.c" 37 #include "unit/lib/json_mock.c" 38 39 #include "spdk/config.h" 40 /* HACK: disable VTune integration so the unit test doesn't need VTune headers and libs to build */ 41 #undef SPDK_CONFIG_VTUNE 42 43 #include "bdev/bdev.c" 44 45 #define BDEV_UT_NUM_THREADS 3 46 47 struct spdk_trace_histories *g_trace_histories; 48 DEFINE_STUB_V(spdk_trace_add_register_fn, (struct spdk_trace_register_fn *reg_fn)); 49 DEFINE_STUB_V(spdk_trace_register_owner, (uint8_t type, char id_prefix)); 50 DEFINE_STUB_V(spdk_trace_register_object, (uint8_t type, char id_prefix)); 51 DEFINE_STUB_V(spdk_trace_register_description, (const char *name, 52 uint16_t tpoint_id, uint8_t owner_type, 53 uint8_t object_type, uint8_t new_object, 54 uint8_t arg1_type, const char *arg1_name)); 55 DEFINE_STUB_V(_spdk_trace_record, (uint64_t tsc, uint16_t tpoint_id, uint16_t poller_id, 56 uint32_t size, uint64_t object_id, uint64_t arg1)); 57 DEFINE_STUB(spdk_notify_send, uint64_t, (const char *type, const char *ctx), 0); 58 DEFINE_STUB(spdk_notify_type_register, struct spdk_notify_type *, (const char *type), NULL); 59 60 struct ut_bdev { 61 struct spdk_bdev bdev; 62 void *io_target; 63 }; 64 65 struct ut_bdev_channel { 66 TAILQ_HEAD(, spdk_bdev_io) outstanding_io; 67 uint32_t outstanding_cnt; 68 uint32_t avail_cnt; 69 }; 70 71 int g_io_device; 72 struct ut_bdev g_bdev; 73 struct spdk_bdev_desc *g_desc; 74 bool g_teardown_done = false; 75 bool g_get_io_channel = true; 76 bool g_create_ch = true; 77 bool g_init_complete_called = false; 78 bool g_fini_start_called = true; 79 int g_status = 0; 80 int g_count = 0; 81 struct spdk_histogram_data *g_histogram = NULL; 82 83 static int 84 stub_create_ch(void *io_device, void *ctx_buf) 85 { 86 struct ut_bdev_channel *ch = ctx_buf; 87 88 if (g_create_ch == false) { 89 return -1; 90 } 91 92 TAILQ_INIT(&ch->outstanding_io); 93 ch->outstanding_cnt = 0; 94 /* 95 * When avail gets to 0, the submit_request function will return ENOMEM. 96 * Most tests to not want ENOMEM to occur, so by default set this to a 97 * big value that won't get hit. The ENOMEM tests can then override this 98 * value to something much smaller to induce ENOMEM conditions. 99 */ 100 ch->avail_cnt = 2048; 101 return 0; 102 } 103 104 static void 105 stub_destroy_ch(void *io_device, void *ctx_buf) 106 { 107 } 108 109 static struct spdk_io_channel * 110 stub_get_io_channel(void *ctx) 111 { 112 struct ut_bdev *ut_bdev = ctx; 113 114 if (g_get_io_channel == true) { 115 return spdk_get_io_channel(ut_bdev->io_target); 116 } else { 117 return NULL; 118 } 119 } 120 121 static int 122 stub_destruct(void *ctx) 123 { 124 return 0; 125 } 126 127 static void 128 stub_submit_request(struct spdk_io_channel *_ch, struct spdk_bdev_io *bdev_io) 129 { 130 struct ut_bdev_channel *ch = spdk_io_channel_get_ctx(_ch); 131 struct spdk_bdev_io *io; 132 133 if (bdev_io->type == SPDK_BDEV_IO_TYPE_RESET) { 134 while (!TAILQ_EMPTY(&ch->outstanding_io)) { 135 io = TAILQ_FIRST(&ch->outstanding_io); 136 TAILQ_REMOVE(&ch->outstanding_io, io, module_link); 137 ch->outstanding_cnt--; 138 spdk_bdev_io_complete(io, SPDK_BDEV_IO_STATUS_ABORTED); 139 ch->avail_cnt++; 140 } 141 } else if (bdev_io->type == SPDK_BDEV_IO_TYPE_ABORT) { 142 TAILQ_FOREACH(io, &ch->outstanding_io, module_link) { 143 if (io == bdev_io->u.abort.bio_to_abort) { 144 TAILQ_REMOVE(&ch->outstanding_io, io, module_link); 145 ch->outstanding_cnt--; 146 spdk_bdev_io_complete(io, SPDK_BDEV_IO_STATUS_ABORTED); 147 ch->avail_cnt++; 148 149 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_SUCCESS); 150 return; 151 } 152 } 153 154 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 155 return; 156 } 157 158 if (ch->avail_cnt > 0) { 159 TAILQ_INSERT_TAIL(&ch->outstanding_io, bdev_io, module_link); 160 ch->outstanding_cnt++; 161 ch->avail_cnt--; 162 } else { 163 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_NOMEM); 164 } 165 } 166 167 static uint32_t 168 stub_complete_io(void *io_target, uint32_t num_to_complete) 169 { 170 struct spdk_io_channel *_ch = spdk_get_io_channel(io_target); 171 struct ut_bdev_channel *ch = spdk_io_channel_get_ctx(_ch); 172 struct spdk_bdev_io *io; 173 bool complete_all = (num_to_complete == 0); 174 uint32_t num_completed = 0; 175 176 while (complete_all || num_completed < num_to_complete) { 177 if (TAILQ_EMPTY(&ch->outstanding_io)) { 178 break; 179 } 180 io = TAILQ_FIRST(&ch->outstanding_io); 181 TAILQ_REMOVE(&ch->outstanding_io, io, module_link); 182 ch->outstanding_cnt--; 183 spdk_bdev_io_complete(io, SPDK_BDEV_IO_STATUS_SUCCESS); 184 ch->avail_cnt++; 185 num_completed++; 186 } 187 188 spdk_put_io_channel(_ch); 189 return num_completed; 190 } 191 192 static bool 193 stub_io_type_supported(void *ctx, enum spdk_bdev_io_type type) 194 { 195 return true; 196 } 197 198 static struct spdk_bdev_fn_table fn_table = { 199 .get_io_channel = stub_get_io_channel, 200 .destruct = stub_destruct, 201 .submit_request = stub_submit_request, 202 .io_type_supported = stub_io_type_supported, 203 }; 204 205 struct spdk_bdev_module bdev_ut_if; 206 207 static int 208 module_init(void) 209 { 210 spdk_bdev_module_init_done(&bdev_ut_if); 211 return 0; 212 } 213 214 static void 215 module_fini(void) 216 { 217 } 218 219 static void 220 init_complete(void) 221 { 222 g_init_complete_called = true; 223 } 224 225 static void 226 fini_start(void) 227 { 228 g_fini_start_called = true; 229 } 230 231 struct spdk_bdev_module bdev_ut_if = { 232 .name = "bdev_ut", 233 .module_init = module_init, 234 .module_fini = module_fini, 235 .async_init = true, 236 .init_complete = init_complete, 237 .fini_start = fini_start, 238 }; 239 240 SPDK_BDEV_MODULE_REGISTER(bdev_ut, &bdev_ut_if) 241 242 static void 243 register_bdev(struct ut_bdev *ut_bdev, char *name, void *io_target) 244 { 245 memset(ut_bdev, 0, sizeof(*ut_bdev)); 246 247 ut_bdev->io_target = io_target; 248 ut_bdev->bdev.ctxt = ut_bdev; 249 ut_bdev->bdev.name = name; 250 ut_bdev->bdev.fn_table = &fn_table; 251 ut_bdev->bdev.module = &bdev_ut_if; 252 ut_bdev->bdev.blocklen = 4096; 253 ut_bdev->bdev.blockcnt = 1024; 254 255 spdk_bdev_register(&ut_bdev->bdev); 256 } 257 258 static void 259 unregister_bdev(struct ut_bdev *ut_bdev) 260 { 261 /* Handle any deferred messages. */ 262 poll_threads(); 263 spdk_bdev_unregister(&ut_bdev->bdev, NULL, NULL); 264 } 265 266 static void 267 bdev_init_cb(void *done, int rc) 268 { 269 CU_ASSERT(rc == 0); 270 *(bool *)done = true; 271 } 272 273 static void 274 _bdev_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, 275 void *event_ctx) 276 { 277 switch (type) { 278 case SPDK_BDEV_EVENT_REMOVE: 279 if (event_ctx != NULL) { 280 *(bool *)event_ctx = true; 281 } 282 break; 283 default: 284 CU_ASSERT(false); 285 break; 286 } 287 } 288 289 static void 290 setup_test(void) 291 { 292 bool done = false; 293 294 allocate_cores(BDEV_UT_NUM_THREADS); 295 allocate_threads(BDEV_UT_NUM_THREADS); 296 set_thread(0); 297 spdk_bdev_initialize(bdev_init_cb, &done); 298 spdk_io_device_register(&g_io_device, stub_create_ch, stub_destroy_ch, 299 sizeof(struct ut_bdev_channel), NULL); 300 register_bdev(&g_bdev, "ut_bdev", &g_io_device); 301 spdk_bdev_open_ext("ut_bdev", true, _bdev_event_cb, NULL, &g_desc); 302 } 303 304 static void 305 finish_cb(void *cb_arg) 306 { 307 g_teardown_done = true; 308 } 309 310 static void 311 teardown_test(void) 312 { 313 set_thread(0); 314 g_teardown_done = false; 315 spdk_bdev_close(g_desc); 316 g_desc = NULL; 317 unregister_bdev(&g_bdev); 318 spdk_io_device_unregister(&g_io_device, NULL); 319 spdk_bdev_finish(finish_cb, NULL); 320 poll_threads(); 321 memset(&g_bdev, 0, sizeof(g_bdev)); 322 CU_ASSERT(g_teardown_done == true); 323 g_teardown_done = false; 324 free_threads(); 325 free_cores(); 326 } 327 328 static uint32_t 329 bdev_io_tailq_cnt(bdev_io_tailq_t *tailq) 330 { 331 struct spdk_bdev_io *io; 332 uint32_t cnt = 0; 333 334 TAILQ_FOREACH(io, tailq, internal.link) { 335 cnt++; 336 } 337 338 return cnt; 339 } 340 341 static void 342 basic(void) 343 { 344 g_init_complete_called = false; 345 setup_test(); 346 CU_ASSERT(g_init_complete_called == true); 347 348 set_thread(0); 349 350 g_get_io_channel = false; 351 g_ut_threads[0].ch = spdk_bdev_get_io_channel(g_desc); 352 CU_ASSERT(g_ut_threads[0].ch == NULL); 353 354 g_get_io_channel = true; 355 g_create_ch = false; 356 g_ut_threads[0].ch = spdk_bdev_get_io_channel(g_desc); 357 CU_ASSERT(g_ut_threads[0].ch == NULL); 358 359 g_get_io_channel = true; 360 g_create_ch = true; 361 g_ut_threads[0].ch = spdk_bdev_get_io_channel(g_desc); 362 CU_ASSERT(g_ut_threads[0].ch != NULL); 363 spdk_put_io_channel(g_ut_threads[0].ch); 364 365 g_fini_start_called = false; 366 teardown_test(); 367 CU_ASSERT(g_fini_start_called == true); 368 } 369 370 static void 371 _bdev_unregistered(void *done, int rc) 372 { 373 CU_ASSERT(rc == 0); 374 *(bool *)done = true; 375 } 376 377 static void 378 unregister_and_close(void) 379 { 380 bool done, remove_notify; 381 struct spdk_bdev_desc *desc = NULL; 382 383 setup_test(); 384 set_thread(0); 385 386 /* setup_test() automatically opens the bdev, 387 * but this test needs to do that in a different 388 * way. */ 389 spdk_bdev_close(g_desc); 390 poll_threads(); 391 392 /* Try hotremoving a bdev with descriptors which don't provide 393 * any context to the notification callback */ 394 spdk_bdev_open_ext("ut_bdev", true, _bdev_event_cb, NULL, &desc); 395 SPDK_CU_ASSERT_FATAL(desc != NULL); 396 397 /* There is an open descriptor on the device. Unregister it, 398 * which can't proceed until the descriptor is closed. */ 399 done = false; 400 spdk_bdev_unregister(&g_bdev.bdev, _bdev_unregistered, &done); 401 402 /* Poll the threads to allow all events to be processed */ 403 poll_threads(); 404 405 /* Make sure the bdev was not unregistered. We still have a 406 * descriptor open */ 407 CU_ASSERT(done == false); 408 409 spdk_bdev_close(desc); 410 poll_threads(); 411 desc = NULL; 412 413 /* The unregister should have completed */ 414 CU_ASSERT(done == true); 415 416 417 /* Register the bdev again */ 418 register_bdev(&g_bdev, "ut_bdev", &g_io_device); 419 420 remove_notify = false; 421 spdk_bdev_open_ext("ut_bdev", true, _bdev_event_cb, &remove_notify, &desc); 422 SPDK_CU_ASSERT_FATAL(desc != NULL); 423 CU_ASSERT(remove_notify == false); 424 425 /* There is an open descriptor on the device. Unregister it, 426 * which can't proceed until the descriptor is closed. */ 427 done = false; 428 spdk_bdev_unregister(&g_bdev.bdev, _bdev_unregistered, &done); 429 /* No polling has occurred, so neither of these should execute */ 430 CU_ASSERT(remove_notify == false); 431 CU_ASSERT(done == false); 432 433 /* Prior to the unregister completing, close the descriptor */ 434 spdk_bdev_close(desc); 435 436 /* Poll the threads to allow all events to be processed */ 437 poll_threads(); 438 439 /* Remove notify should not have been called because the 440 * descriptor is already closed. */ 441 CU_ASSERT(remove_notify == false); 442 443 /* The unregister should have completed */ 444 CU_ASSERT(done == true); 445 446 /* Restore the original g_bdev so that we can use teardown_test(). */ 447 register_bdev(&g_bdev, "ut_bdev", &g_io_device); 448 spdk_bdev_open_ext("ut_bdev", true, _bdev_event_cb, NULL, &g_desc); 449 teardown_test(); 450 } 451 452 static void 453 reset_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 454 { 455 bool *done = cb_arg; 456 457 CU_ASSERT(success == true); 458 *done = true; 459 spdk_bdev_free_io(bdev_io); 460 } 461 462 static void 463 put_channel_during_reset(void) 464 { 465 struct spdk_io_channel *io_ch; 466 bool done = false; 467 468 setup_test(); 469 470 set_thread(0); 471 io_ch = spdk_bdev_get_io_channel(g_desc); 472 CU_ASSERT(io_ch != NULL); 473 474 /* 475 * Start a reset, but then put the I/O channel before 476 * the deferred messages for the reset get a chance to 477 * execute. 478 */ 479 spdk_bdev_reset(g_desc, io_ch, reset_done, &done); 480 spdk_put_io_channel(io_ch); 481 poll_threads(); 482 stub_complete_io(g_bdev.io_target, 0); 483 484 teardown_test(); 485 } 486 487 static void 488 aborted_reset_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 489 { 490 enum spdk_bdev_io_status *status = cb_arg; 491 492 *status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED; 493 spdk_bdev_free_io(bdev_io); 494 } 495 496 static void 497 aborted_reset(void) 498 { 499 struct spdk_io_channel *io_ch[2]; 500 enum spdk_bdev_io_status status1 = SPDK_BDEV_IO_STATUS_PENDING, 501 status2 = SPDK_BDEV_IO_STATUS_PENDING; 502 503 setup_test(); 504 505 set_thread(0); 506 io_ch[0] = spdk_bdev_get_io_channel(g_desc); 507 CU_ASSERT(io_ch[0] != NULL); 508 spdk_bdev_reset(g_desc, io_ch[0], aborted_reset_done, &status1); 509 poll_threads(); 510 CU_ASSERT(g_bdev.bdev.internal.reset_in_progress != NULL); 511 512 /* 513 * First reset has been submitted on ch0. Now submit a second 514 * reset on ch1 which will get queued since there is already a 515 * reset in progress. 516 */ 517 set_thread(1); 518 io_ch[1] = spdk_bdev_get_io_channel(g_desc); 519 CU_ASSERT(io_ch[1] != NULL); 520 spdk_bdev_reset(g_desc, io_ch[1], aborted_reset_done, &status2); 521 poll_threads(); 522 CU_ASSERT(g_bdev.bdev.internal.reset_in_progress != NULL); 523 524 /* 525 * Now destroy ch1. This will abort the queued reset. Check that 526 * the second reset was completed with failed status. Also check 527 * that bdev->internal.reset_in_progress != NULL, since the 528 * original reset has not been completed yet. This ensures that 529 * the bdev code is correctly noticing that the failed reset is 530 * *not* the one that had been submitted to the bdev module. 531 */ 532 set_thread(1); 533 spdk_put_io_channel(io_ch[1]); 534 poll_threads(); 535 CU_ASSERT(status2 == SPDK_BDEV_IO_STATUS_FAILED); 536 CU_ASSERT(g_bdev.bdev.internal.reset_in_progress != NULL); 537 538 /* 539 * Now complete the first reset, verify that it completed with SUCCESS 540 * status and that bdev->internal.reset_in_progress is also set back to NULL. 541 */ 542 set_thread(0); 543 spdk_put_io_channel(io_ch[0]); 544 stub_complete_io(g_bdev.io_target, 0); 545 poll_threads(); 546 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS); 547 CU_ASSERT(g_bdev.bdev.internal.reset_in_progress == NULL); 548 549 teardown_test(); 550 } 551 552 static void 553 io_during_io_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 554 { 555 enum spdk_bdev_io_status *status = cb_arg; 556 557 *status = bdev_io->internal.status; 558 spdk_bdev_free_io(bdev_io); 559 } 560 561 static void 562 io_during_reset(void) 563 { 564 struct spdk_io_channel *io_ch[2]; 565 struct spdk_bdev_channel *bdev_ch[2]; 566 enum spdk_bdev_io_status status0, status1, status_reset; 567 int rc; 568 569 setup_test(); 570 571 /* 572 * First test normal case - submit an I/O on each of two channels (with no resets) 573 * and verify they complete successfully. 574 */ 575 set_thread(0); 576 io_ch[0] = spdk_bdev_get_io_channel(g_desc); 577 bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]); 578 CU_ASSERT(bdev_ch[0]->flags == 0); 579 status0 = SPDK_BDEV_IO_STATUS_PENDING; 580 rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0); 581 CU_ASSERT(rc == 0); 582 583 set_thread(1); 584 io_ch[1] = spdk_bdev_get_io_channel(g_desc); 585 bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]); 586 CU_ASSERT(bdev_ch[1]->flags == 0); 587 status1 = SPDK_BDEV_IO_STATUS_PENDING; 588 rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1); 589 CU_ASSERT(rc == 0); 590 591 poll_threads(); 592 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_PENDING); 593 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING); 594 595 set_thread(0); 596 stub_complete_io(g_bdev.io_target, 0); 597 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_SUCCESS); 598 599 set_thread(1); 600 stub_complete_io(g_bdev.io_target, 0); 601 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS); 602 603 /* 604 * Now submit a reset, and leave it pending while we submit I/O on two different 605 * channels. These I/O should be failed by the bdev layer since the reset is in 606 * progress. 607 */ 608 set_thread(0); 609 status_reset = SPDK_BDEV_IO_STATUS_PENDING; 610 rc = spdk_bdev_reset(g_desc, io_ch[0], io_during_io_done, &status_reset); 611 CU_ASSERT(rc == 0); 612 613 CU_ASSERT(bdev_ch[0]->flags == 0); 614 CU_ASSERT(bdev_ch[1]->flags == 0); 615 poll_threads(); 616 CU_ASSERT(bdev_ch[0]->flags == BDEV_CH_RESET_IN_PROGRESS); 617 CU_ASSERT(bdev_ch[1]->flags == BDEV_CH_RESET_IN_PROGRESS); 618 619 set_thread(0); 620 status0 = SPDK_BDEV_IO_STATUS_PENDING; 621 rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0); 622 CU_ASSERT(rc == 0); 623 624 set_thread(1); 625 status1 = SPDK_BDEV_IO_STATUS_PENDING; 626 rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1); 627 CU_ASSERT(rc == 0); 628 629 /* 630 * A reset is in progress so these read I/O should complete with aborted. Note that we 631 * need to poll_threads() since I/O completed inline have their completion deferred. 632 */ 633 poll_threads(); 634 CU_ASSERT(status_reset == SPDK_BDEV_IO_STATUS_PENDING); 635 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_ABORTED); 636 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_ABORTED); 637 638 /* 639 * Complete the reset 640 */ 641 set_thread(0); 642 stub_complete_io(g_bdev.io_target, 0); 643 644 /* 645 * Only poll thread 0. We should not get a completion. 646 */ 647 poll_thread(0); 648 CU_ASSERT(status_reset == SPDK_BDEV_IO_STATUS_PENDING); 649 650 /* 651 * Poll both thread 0 and 1 so the messages can propagate and we 652 * get a completion. 653 */ 654 poll_threads(); 655 CU_ASSERT(status_reset == SPDK_BDEV_IO_STATUS_SUCCESS); 656 657 spdk_put_io_channel(io_ch[0]); 658 set_thread(1); 659 spdk_put_io_channel(io_ch[1]); 660 poll_threads(); 661 662 teardown_test(); 663 } 664 665 static void 666 basic_qos(void) 667 { 668 struct spdk_io_channel *io_ch[2]; 669 struct spdk_bdev_channel *bdev_ch[2]; 670 struct spdk_bdev *bdev; 671 enum spdk_bdev_io_status status, abort_status; 672 int rc; 673 674 setup_test(); 675 676 /* Enable QoS */ 677 bdev = &g_bdev.bdev; 678 bdev->internal.qos = calloc(1, sizeof(*bdev->internal.qos)); 679 SPDK_CU_ASSERT_FATAL(bdev->internal.qos != NULL); 680 TAILQ_INIT(&bdev->internal.qos->queued); 681 /* 682 * Enable read/write IOPS, read only byte per second and 683 * read/write byte per second rate limits. 684 * In this case, all rate limits will take equal effect. 685 */ 686 /* 2000 read/write I/O per second, or 2 per millisecond */ 687 bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT].limit = 2000; 688 /* 8K read/write byte per millisecond with 4K block size */ 689 bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT].limit = 8192000; 690 /* 8K read only byte per millisecond with 4K block size */ 691 bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_R_BPS_RATE_LIMIT].limit = 8192000; 692 693 g_get_io_channel = true; 694 695 set_thread(0); 696 io_ch[0] = spdk_bdev_get_io_channel(g_desc); 697 bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]); 698 CU_ASSERT(bdev_ch[0]->flags == BDEV_CH_QOS_ENABLED); 699 700 set_thread(1); 701 io_ch[1] = spdk_bdev_get_io_channel(g_desc); 702 bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]); 703 CU_ASSERT(bdev_ch[1]->flags == BDEV_CH_QOS_ENABLED); 704 705 /* 706 * Send an I/O on thread 0, which is where the QoS thread is running. 707 */ 708 set_thread(0); 709 status = SPDK_BDEV_IO_STATUS_PENDING; 710 rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status); 711 CU_ASSERT(rc == 0); 712 CU_ASSERT(status == SPDK_BDEV_IO_STATUS_PENDING); 713 poll_threads(); 714 stub_complete_io(g_bdev.io_target, 0); 715 poll_threads(); 716 CU_ASSERT(status == SPDK_BDEV_IO_STATUS_SUCCESS); 717 718 /* Send an I/O on thread 1. The QoS thread is not running here. */ 719 status = SPDK_BDEV_IO_STATUS_PENDING; 720 set_thread(1); 721 rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status); 722 CU_ASSERT(rc == 0); 723 CU_ASSERT(status == SPDK_BDEV_IO_STATUS_PENDING); 724 poll_threads(); 725 /* Complete I/O on thread 1. This should not complete the I/O we submitted */ 726 stub_complete_io(g_bdev.io_target, 0); 727 poll_threads(); 728 CU_ASSERT(status == SPDK_BDEV_IO_STATUS_PENDING); 729 /* Now complete I/O on thread 0 */ 730 set_thread(0); 731 poll_threads(); 732 stub_complete_io(g_bdev.io_target, 0); 733 poll_threads(); 734 CU_ASSERT(status == SPDK_BDEV_IO_STATUS_SUCCESS); 735 736 /* Reset rate limit for the next test cases. */ 737 spdk_delay_us(SPDK_BDEV_QOS_TIMESLICE_IN_USEC); 738 poll_threads(); 739 740 /* 741 * Test abort request when QoS is enabled. 742 */ 743 744 /* Send an I/O on thread 0, which is where the QoS thread is running. */ 745 set_thread(0); 746 status = SPDK_BDEV_IO_STATUS_PENDING; 747 rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status); 748 CU_ASSERT(rc == 0); 749 CU_ASSERT(status == SPDK_BDEV_IO_STATUS_PENDING); 750 /* Send an abort to the I/O on the same thread. */ 751 abort_status = SPDK_BDEV_IO_STATUS_PENDING; 752 rc = spdk_bdev_abort(g_desc, io_ch[0], &status, io_during_io_done, &abort_status); 753 CU_ASSERT(rc == 0); 754 CU_ASSERT(abort_status == SPDK_BDEV_IO_STATUS_PENDING); 755 poll_threads(); 756 CU_ASSERT(abort_status == SPDK_BDEV_IO_STATUS_SUCCESS); 757 CU_ASSERT(status == SPDK_BDEV_IO_STATUS_ABORTED); 758 759 /* Send an I/O on thread 1. The QoS thread is not running here. */ 760 status = SPDK_BDEV_IO_STATUS_PENDING; 761 set_thread(1); 762 rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status); 763 CU_ASSERT(rc == 0); 764 CU_ASSERT(status == SPDK_BDEV_IO_STATUS_PENDING); 765 poll_threads(); 766 /* Send an abort to the I/O on the same thread. */ 767 abort_status = SPDK_BDEV_IO_STATUS_PENDING; 768 rc = spdk_bdev_abort(g_desc, io_ch[1], &status, io_during_io_done, &abort_status); 769 CU_ASSERT(rc == 0); 770 CU_ASSERT(abort_status == SPDK_BDEV_IO_STATUS_PENDING); 771 poll_threads(); 772 /* Complete the I/O with failure and the abort with success on thread 1. */ 773 CU_ASSERT(abort_status == SPDK_BDEV_IO_STATUS_SUCCESS); 774 CU_ASSERT(status == SPDK_BDEV_IO_STATUS_ABORTED); 775 776 set_thread(0); 777 778 /* 779 * Close the descriptor only, which should stop the qos channel as 780 * the last descriptor removed. 781 */ 782 spdk_bdev_close(g_desc); 783 poll_threads(); 784 CU_ASSERT(bdev->internal.qos->ch == NULL); 785 786 /* 787 * Open the bdev again which shall setup the qos channel as the 788 * channels are valid. 789 */ 790 spdk_bdev_open_ext("ut_bdev", true, _bdev_event_cb, NULL, &g_desc); 791 poll_threads(); 792 CU_ASSERT(bdev->internal.qos->ch != NULL); 793 794 /* Tear down the channels */ 795 set_thread(0); 796 spdk_put_io_channel(io_ch[0]); 797 set_thread(1); 798 spdk_put_io_channel(io_ch[1]); 799 poll_threads(); 800 set_thread(0); 801 802 /* Close the descriptor, which should stop the qos channel */ 803 spdk_bdev_close(g_desc); 804 poll_threads(); 805 CU_ASSERT(bdev->internal.qos->ch == NULL); 806 807 /* Open the bdev again, no qos channel setup without valid channels. */ 808 spdk_bdev_open_ext("ut_bdev", true, _bdev_event_cb, NULL, &g_desc); 809 poll_threads(); 810 CU_ASSERT(bdev->internal.qos->ch == NULL); 811 812 /* Create the channels in reverse order. */ 813 set_thread(1); 814 io_ch[1] = spdk_bdev_get_io_channel(g_desc); 815 bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]); 816 CU_ASSERT(bdev_ch[1]->flags == BDEV_CH_QOS_ENABLED); 817 818 set_thread(0); 819 io_ch[0] = spdk_bdev_get_io_channel(g_desc); 820 bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]); 821 CU_ASSERT(bdev_ch[0]->flags == BDEV_CH_QOS_ENABLED); 822 823 /* Confirm that the qos thread is now thread 1 */ 824 CU_ASSERT(bdev->internal.qos->ch == bdev_ch[1]); 825 826 /* Tear down the channels */ 827 set_thread(0); 828 spdk_put_io_channel(io_ch[0]); 829 set_thread(1); 830 spdk_put_io_channel(io_ch[1]); 831 poll_threads(); 832 833 set_thread(0); 834 835 teardown_test(); 836 } 837 838 static void 839 io_during_qos_queue(void) 840 { 841 struct spdk_io_channel *io_ch[2]; 842 struct spdk_bdev_channel *bdev_ch[2]; 843 struct spdk_bdev *bdev; 844 enum spdk_bdev_io_status status0, status1, status2; 845 int rc; 846 847 setup_test(); 848 MOCK_SET(spdk_get_ticks, 0); 849 850 /* Enable QoS */ 851 bdev = &g_bdev.bdev; 852 bdev->internal.qos = calloc(1, sizeof(*bdev->internal.qos)); 853 SPDK_CU_ASSERT_FATAL(bdev->internal.qos != NULL); 854 TAILQ_INIT(&bdev->internal.qos->queued); 855 /* 856 * Enable read/write IOPS, read only byte per sec, write only 857 * byte per sec and read/write byte per sec rate limits. 858 * In this case, both read only and write only byte per sec 859 * rate limit will take effect. 860 */ 861 /* 4000 read/write I/O per second, or 4 per millisecond */ 862 bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT].limit = 4000; 863 /* 8K byte per millisecond with 4K block size */ 864 bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT].limit = 8192000; 865 /* 4K byte per millisecond with 4K block size */ 866 bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_R_BPS_RATE_LIMIT].limit = 4096000; 867 /* 4K byte per millisecond with 4K block size */ 868 bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_W_BPS_RATE_LIMIT].limit = 4096000; 869 870 g_get_io_channel = true; 871 872 /* Create channels */ 873 set_thread(0); 874 io_ch[0] = spdk_bdev_get_io_channel(g_desc); 875 bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]); 876 CU_ASSERT(bdev_ch[0]->flags == BDEV_CH_QOS_ENABLED); 877 878 set_thread(1); 879 io_ch[1] = spdk_bdev_get_io_channel(g_desc); 880 bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]); 881 CU_ASSERT(bdev_ch[1]->flags == BDEV_CH_QOS_ENABLED); 882 883 /* Send two read I/Os */ 884 status1 = SPDK_BDEV_IO_STATUS_PENDING; 885 rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1); 886 CU_ASSERT(rc == 0); 887 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING); 888 set_thread(0); 889 status0 = SPDK_BDEV_IO_STATUS_PENDING; 890 rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0); 891 CU_ASSERT(rc == 0); 892 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_PENDING); 893 /* Send one write I/O */ 894 status2 = SPDK_BDEV_IO_STATUS_PENDING; 895 rc = spdk_bdev_write_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status2); 896 CU_ASSERT(rc == 0); 897 CU_ASSERT(status2 == SPDK_BDEV_IO_STATUS_PENDING); 898 899 /* Complete any I/O that arrived at the disk */ 900 poll_threads(); 901 set_thread(1); 902 stub_complete_io(g_bdev.io_target, 0); 903 set_thread(0); 904 stub_complete_io(g_bdev.io_target, 0); 905 poll_threads(); 906 907 /* Only one of the two read I/Os should complete. (logical XOR) */ 908 if (status0 == SPDK_BDEV_IO_STATUS_SUCCESS) { 909 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING); 910 } else { 911 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS); 912 } 913 /* The write I/O should complete. */ 914 CU_ASSERT(status2 == SPDK_BDEV_IO_STATUS_SUCCESS); 915 916 /* Advance in time by a millisecond */ 917 spdk_delay_us(1000); 918 919 /* Complete more I/O */ 920 poll_threads(); 921 set_thread(1); 922 stub_complete_io(g_bdev.io_target, 0); 923 set_thread(0); 924 stub_complete_io(g_bdev.io_target, 0); 925 poll_threads(); 926 927 /* Now the second read I/O should be done */ 928 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_SUCCESS); 929 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS); 930 931 /* Tear down the channels */ 932 set_thread(1); 933 spdk_put_io_channel(io_ch[1]); 934 set_thread(0); 935 spdk_put_io_channel(io_ch[0]); 936 poll_threads(); 937 938 teardown_test(); 939 } 940 941 static void 942 io_during_qos_reset(void) 943 { 944 struct spdk_io_channel *io_ch[2]; 945 struct spdk_bdev_channel *bdev_ch[2]; 946 struct spdk_bdev *bdev; 947 enum spdk_bdev_io_status status0, status1, reset_status; 948 int rc; 949 950 setup_test(); 951 MOCK_SET(spdk_get_ticks, 0); 952 953 /* Enable QoS */ 954 bdev = &g_bdev.bdev; 955 bdev->internal.qos = calloc(1, sizeof(*bdev->internal.qos)); 956 SPDK_CU_ASSERT_FATAL(bdev->internal.qos != NULL); 957 TAILQ_INIT(&bdev->internal.qos->queued); 958 /* 959 * Enable read/write IOPS, write only byte per sec and 960 * read/write byte per second rate limits. 961 * In this case, read/write byte per second rate limit will 962 * take effect first. 963 */ 964 /* 2000 read/write I/O per second, or 2 per millisecond */ 965 bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT].limit = 2000; 966 /* 4K byte per millisecond with 4K block size */ 967 bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT].limit = 4096000; 968 /* 8K byte per millisecond with 4K block size */ 969 bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_W_BPS_RATE_LIMIT].limit = 8192000; 970 971 g_get_io_channel = true; 972 973 /* Create channels */ 974 set_thread(0); 975 io_ch[0] = spdk_bdev_get_io_channel(g_desc); 976 bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]); 977 CU_ASSERT(bdev_ch[0]->flags == BDEV_CH_QOS_ENABLED); 978 979 set_thread(1); 980 io_ch[1] = spdk_bdev_get_io_channel(g_desc); 981 bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]); 982 CU_ASSERT(bdev_ch[1]->flags == BDEV_CH_QOS_ENABLED); 983 984 /* Send two I/O. One of these gets queued by QoS. The other is sitting at the disk. */ 985 status1 = SPDK_BDEV_IO_STATUS_PENDING; 986 rc = spdk_bdev_write_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1); 987 CU_ASSERT(rc == 0); 988 set_thread(0); 989 status0 = SPDK_BDEV_IO_STATUS_PENDING; 990 rc = spdk_bdev_write_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0); 991 CU_ASSERT(rc == 0); 992 993 poll_threads(); 994 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING); 995 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_PENDING); 996 997 /* Reset the bdev. */ 998 reset_status = SPDK_BDEV_IO_STATUS_PENDING; 999 rc = spdk_bdev_reset(g_desc, io_ch[0], io_during_io_done, &reset_status); 1000 CU_ASSERT(rc == 0); 1001 1002 /* Complete any I/O that arrived at the disk */ 1003 poll_threads(); 1004 set_thread(1); 1005 stub_complete_io(g_bdev.io_target, 0); 1006 set_thread(0); 1007 stub_complete_io(g_bdev.io_target, 0); 1008 poll_threads(); 1009 1010 CU_ASSERT(reset_status == SPDK_BDEV_IO_STATUS_SUCCESS); 1011 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_ABORTED); 1012 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_ABORTED); 1013 1014 /* Tear down the channels */ 1015 set_thread(1); 1016 spdk_put_io_channel(io_ch[1]); 1017 set_thread(0); 1018 spdk_put_io_channel(io_ch[0]); 1019 poll_threads(); 1020 1021 teardown_test(); 1022 } 1023 1024 static void 1025 enomem_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 1026 { 1027 enum spdk_bdev_io_status *status = cb_arg; 1028 1029 *status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED; 1030 spdk_bdev_free_io(bdev_io); 1031 } 1032 1033 static void 1034 enomem(void) 1035 { 1036 struct spdk_io_channel *io_ch; 1037 struct spdk_bdev_channel *bdev_ch; 1038 struct spdk_bdev_shared_resource *shared_resource; 1039 struct ut_bdev_channel *ut_ch; 1040 const uint32_t IO_ARRAY_SIZE = 64; 1041 const uint32_t AVAIL = 20; 1042 enum spdk_bdev_io_status status[IO_ARRAY_SIZE], status_reset; 1043 uint32_t nomem_cnt, i; 1044 struct spdk_bdev_io *first_io; 1045 int rc; 1046 1047 setup_test(); 1048 1049 set_thread(0); 1050 io_ch = spdk_bdev_get_io_channel(g_desc); 1051 bdev_ch = spdk_io_channel_get_ctx(io_ch); 1052 shared_resource = bdev_ch->shared_resource; 1053 ut_ch = spdk_io_channel_get_ctx(bdev_ch->channel); 1054 ut_ch->avail_cnt = AVAIL; 1055 1056 /* First submit a number of IOs equal to what the channel can support. */ 1057 for (i = 0; i < AVAIL; i++) { 1058 status[i] = SPDK_BDEV_IO_STATUS_PENDING; 1059 rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[i]); 1060 CU_ASSERT(rc == 0); 1061 } 1062 CU_ASSERT(TAILQ_EMPTY(&shared_resource->nomem_io)); 1063 1064 /* 1065 * Next, submit one additional I/O. This one should fail with ENOMEM and then go onto 1066 * the enomem_io list. 1067 */ 1068 status[AVAIL] = SPDK_BDEV_IO_STATUS_PENDING; 1069 rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[AVAIL]); 1070 CU_ASSERT(rc == 0); 1071 SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&shared_resource->nomem_io)); 1072 first_io = TAILQ_FIRST(&shared_resource->nomem_io); 1073 1074 /* 1075 * Now submit a bunch more I/O. These should all fail with ENOMEM and get queued behind 1076 * the first_io above. 1077 */ 1078 for (i = AVAIL + 1; i < IO_ARRAY_SIZE; i++) { 1079 status[i] = SPDK_BDEV_IO_STATUS_PENDING; 1080 rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[i]); 1081 CU_ASSERT(rc == 0); 1082 } 1083 1084 /* Assert that first_io is still at the head of the list. */ 1085 CU_ASSERT(TAILQ_FIRST(&shared_resource->nomem_io) == first_io); 1086 CU_ASSERT(bdev_io_tailq_cnt(&shared_resource->nomem_io) == (IO_ARRAY_SIZE - AVAIL)); 1087 nomem_cnt = bdev_io_tailq_cnt(&shared_resource->nomem_io); 1088 CU_ASSERT(shared_resource->nomem_threshold == (AVAIL - NOMEM_THRESHOLD_COUNT)); 1089 1090 /* 1091 * Complete 1 I/O only. The key check here is bdev_io_tailq_cnt - this should not have 1092 * changed since completing just 1 I/O should not trigger retrying the queued nomem_io 1093 * list. 1094 */ 1095 stub_complete_io(g_bdev.io_target, 1); 1096 CU_ASSERT(bdev_io_tailq_cnt(&shared_resource->nomem_io) == nomem_cnt); 1097 1098 /* 1099 * Complete enough I/O to hit the nomem_theshold. This should trigger retrying nomem_io, 1100 * and we should see I/O get resubmitted to the test bdev module. 1101 */ 1102 stub_complete_io(g_bdev.io_target, NOMEM_THRESHOLD_COUNT - 1); 1103 CU_ASSERT(bdev_io_tailq_cnt(&shared_resource->nomem_io) < nomem_cnt); 1104 nomem_cnt = bdev_io_tailq_cnt(&shared_resource->nomem_io); 1105 1106 /* Complete 1 I/O only. This should not trigger retrying the queued nomem_io. */ 1107 stub_complete_io(g_bdev.io_target, 1); 1108 CU_ASSERT(bdev_io_tailq_cnt(&shared_resource->nomem_io) == nomem_cnt); 1109 1110 /* 1111 * Send a reset and confirm that all I/O are completed, including the ones that 1112 * were queued on the nomem_io list. 1113 */ 1114 status_reset = SPDK_BDEV_IO_STATUS_PENDING; 1115 rc = spdk_bdev_reset(g_desc, io_ch, enomem_done, &status_reset); 1116 poll_threads(); 1117 CU_ASSERT(rc == 0); 1118 /* This will complete the reset. */ 1119 stub_complete_io(g_bdev.io_target, 0); 1120 1121 CU_ASSERT(bdev_io_tailq_cnt(&shared_resource->nomem_io) == 0); 1122 CU_ASSERT(shared_resource->io_outstanding == 0); 1123 1124 spdk_put_io_channel(io_ch); 1125 poll_threads(); 1126 teardown_test(); 1127 } 1128 1129 static void 1130 enomem_multi_bdev(void) 1131 { 1132 struct spdk_io_channel *io_ch; 1133 struct spdk_bdev_channel *bdev_ch; 1134 struct spdk_bdev_shared_resource *shared_resource; 1135 struct ut_bdev_channel *ut_ch; 1136 const uint32_t IO_ARRAY_SIZE = 64; 1137 const uint32_t AVAIL = 20; 1138 enum spdk_bdev_io_status status[IO_ARRAY_SIZE]; 1139 uint32_t i; 1140 struct ut_bdev *second_bdev; 1141 struct spdk_bdev_desc *second_desc = NULL; 1142 struct spdk_bdev_channel *second_bdev_ch; 1143 struct spdk_io_channel *second_ch; 1144 int rc; 1145 1146 setup_test(); 1147 1148 /* Register second bdev with the same io_target */ 1149 second_bdev = calloc(1, sizeof(*second_bdev)); 1150 SPDK_CU_ASSERT_FATAL(second_bdev != NULL); 1151 register_bdev(second_bdev, "ut_bdev2", g_bdev.io_target); 1152 spdk_bdev_open_ext("ut_bdev2", true, _bdev_event_cb, NULL, &second_desc); 1153 SPDK_CU_ASSERT_FATAL(second_desc != NULL); 1154 1155 set_thread(0); 1156 io_ch = spdk_bdev_get_io_channel(g_desc); 1157 bdev_ch = spdk_io_channel_get_ctx(io_ch); 1158 shared_resource = bdev_ch->shared_resource; 1159 ut_ch = spdk_io_channel_get_ctx(bdev_ch->channel); 1160 ut_ch->avail_cnt = AVAIL; 1161 1162 second_ch = spdk_bdev_get_io_channel(second_desc); 1163 second_bdev_ch = spdk_io_channel_get_ctx(second_ch); 1164 SPDK_CU_ASSERT_FATAL(shared_resource == second_bdev_ch->shared_resource); 1165 1166 /* Saturate io_target through bdev A. */ 1167 for (i = 0; i < AVAIL; i++) { 1168 status[i] = SPDK_BDEV_IO_STATUS_PENDING; 1169 rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[i]); 1170 CU_ASSERT(rc == 0); 1171 } 1172 CU_ASSERT(TAILQ_EMPTY(&shared_resource->nomem_io)); 1173 1174 /* 1175 * Now submit I/O through the second bdev. This should fail with ENOMEM 1176 * and then go onto the nomem_io list. 1177 */ 1178 status[AVAIL] = SPDK_BDEV_IO_STATUS_PENDING; 1179 rc = spdk_bdev_read_blocks(second_desc, second_ch, NULL, 0, 1, enomem_done, &status[AVAIL]); 1180 CU_ASSERT(rc == 0); 1181 SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&shared_resource->nomem_io)); 1182 1183 /* Complete first bdev's I/O. This should retry sending second bdev's nomem_io */ 1184 stub_complete_io(g_bdev.io_target, AVAIL); 1185 1186 SPDK_CU_ASSERT_FATAL(TAILQ_EMPTY(&shared_resource->nomem_io)); 1187 CU_ASSERT(shared_resource->io_outstanding == 1); 1188 1189 /* Now complete our retried I/O */ 1190 stub_complete_io(g_bdev.io_target, 1); 1191 SPDK_CU_ASSERT_FATAL(shared_resource->io_outstanding == 0); 1192 1193 spdk_put_io_channel(io_ch); 1194 spdk_put_io_channel(second_ch); 1195 spdk_bdev_close(second_desc); 1196 unregister_bdev(second_bdev); 1197 poll_threads(); 1198 free(second_bdev); 1199 teardown_test(); 1200 } 1201 1202 1203 static void 1204 enomem_multi_io_target(void) 1205 { 1206 struct spdk_io_channel *io_ch; 1207 struct spdk_bdev_channel *bdev_ch; 1208 struct ut_bdev_channel *ut_ch; 1209 const uint32_t IO_ARRAY_SIZE = 64; 1210 const uint32_t AVAIL = 20; 1211 enum spdk_bdev_io_status status[IO_ARRAY_SIZE]; 1212 uint32_t i; 1213 int new_io_device; 1214 struct ut_bdev *second_bdev; 1215 struct spdk_bdev_desc *second_desc = NULL; 1216 struct spdk_bdev_channel *second_bdev_ch; 1217 struct spdk_io_channel *second_ch; 1218 int rc; 1219 1220 setup_test(); 1221 1222 /* Create new io_target and a second bdev using it */ 1223 spdk_io_device_register(&new_io_device, stub_create_ch, stub_destroy_ch, 1224 sizeof(struct ut_bdev_channel), NULL); 1225 second_bdev = calloc(1, sizeof(*second_bdev)); 1226 SPDK_CU_ASSERT_FATAL(second_bdev != NULL); 1227 register_bdev(second_bdev, "ut_bdev2", &new_io_device); 1228 spdk_bdev_open_ext("ut_bdev2", true, _bdev_event_cb, NULL, &second_desc); 1229 SPDK_CU_ASSERT_FATAL(second_desc != NULL); 1230 1231 set_thread(0); 1232 io_ch = spdk_bdev_get_io_channel(g_desc); 1233 bdev_ch = spdk_io_channel_get_ctx(io_ch); 1234 ut_ch = spdk_io_channel_get_ctx(bdev_ch->channel); 1235 ut_ch->avail_cnt = AVAIL; 1236 1237 /* Different io_target should imply a different shared_resource */ 1238 second_ch = spdk_bdev_get_io_channel(second_desc); 1239 second_bdev_ch = spdk_io_channel_get_ctx(second_ch); 1240 SPDK_CU_ASSERT_FATAL(bdev_ch->shared_resource != second_bdev_ch->shared_resource); 1241 1242 /* Saturate io_target through bdev A. */ 1243 for (i = 0; i < AVAIL; i++) { 1244 status[i] = SPDK_BDEV_IO_STATUS_PENDING; 1245 rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[i]); 1246 CU_ASSERT(rc == 0); 1247 } 1248 CU_ASSERT(TAILQ_EMPTY(&bdev_ch->shared_resource->nomem_io)); 1249 1250 /* Issue one more I/O to fill ENOMEM list. */ 1251 status[AVAIL] = SPDK_BDEV_IO_STATUS_PENDING; 1252 rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[AVAIL]); 1253 CU_ASSERT(rc == 0); 1254 SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&bdev_ch->shared_resource->nomem_io)); 1255 1256 /* 1257 * Now submit I/O through the second bdev. This should go through and complete 1258 * successfully because we're using a different io_device underneath. 1259 */ 1260 status[AVAIL] = SPDK_BDEV_IO_STATUS_PENDING; 1261 rc = spdk_bdev_read_blocks(second_desc, second_ch, NULL, 0, 1, enomem_done, &status[AVAIL]); 1262 CU_ASSERT(rc == 0); 1263 SPDK_CU_ASSERT_FATAL(TAILQ_EMPTY(&second_bdev_ch->shared_resource->nomem_io)); 1264 stub_complete_io(second_bdev->io_target, 1); 1265 1266 /* Cleanup; Complete outstanding I/O. */ 1267 stub_complete_io(g_bdev.io_target, AVAIL); 1268 SPDK_CU_ASSERT_FATAL(TAILQ_EMPTY(&bdev_ch->shared_resource->nomem_io)); 1269 /* Complete the ENOMEM I/O */ 1270 stub_complete_io(g_bdev.io_target, 1); 1271 CU_ASSERT(bdev_ch->shared_resource->io_outstanding == 0); 1272 1273 SPDK_CU_ASSERT_FATAL(TAILQ_EMPTY(&bdev_ch->shared_resource->nomem_io)); 1274 CU_ASSERT(bdev_ch->shared_resource->io_outstanding == 0); 1275 spdk_put_io_channel(io_ch); 1276 spdk_put_io_channel(second_ch); 1277 spdk_bdev_close(second_desc); 1278 unregister_bdev(second_bdev); 1279 spdk_io_device_unregister(&new_io_device, NULL); 1280 poll_threads(); 1281 free(second_bdev); 1282 teardown_test(); 1283 } 1284 1285 static void 1286 qos_dynamic_enable_done(void *cb_arg, int status) 1287 { 1288 int *rc = cb_arg; 1289 *rc = status; 1290 } 1291 1292 static void 1293 qos_dynamic_enable(void) 1294 { 1295 struct spdk_io_channel *io_ch[2]; 1296 struct spdk_bdev_channel *bdev_ch[2]; 1297 struct spdk_bdev *bdev; 1298 enum spdk_bdev_io_status bdev_io_status[2]; 1299 uint64_t limits[SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES] = {}; 1300 int status, second_status, rc, i; 1301 1302 setup_test(); 1303 MOCK_SET(spdk_get_ticks, 0); 1304 1305 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 1306 limits[i] = UINT64_MAX; 1307 } 1308 1309 bdev = &g_bdev.bdev; 1310 1311 g_get_io_channel = true; 1312 1313 /* Create channels */ 1314 set_thread(0); 1315 io_ch[0] = spdk_bdev_get_io_channel(g_desc); 1316 bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]); 1317 CU_ASSERT(bdev_ch[0]->flags == 0); 1318 1319 set_thread(1); 1320 io_ch[1] = spdk_bdev_get_io_channel(g_desc); 1321 bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]); 1322 CU_ASSERT(bdev_ch[1]->flags == 0); 1323 1324 set_thread(0); 1325 1326 /* 1327 * Enable QoS: Read/Write IOPS, Read/Write byte, 1328 * Read only byte and Write only byte per second 1329 * rate limits. 1330 * More than 10 I/Os allowed per timeslice. 1331 */ 1332 status = -1; 1333 limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT] = 10000; 1334 limits[SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT] = 100; 1335 limits[SPDK_BDEV_QOS_R_BPS_RATE_LIMIT] = 100; 1336 limits[SPDK_BDEV_QOS_W_BPS_RATE_LIMIT] = 10; 1337 spdk_bdev_set_qos_rate_limits(bdev, limits, qos_dynamic_enable_done, &status); 1338 poll_threads(); 1339 CU_ASSERT(status == 0); 1340 CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) != 0); 1341 CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) != 0); 1342 1343 /* 1344 * Submit and complete 10 I/O to fill the QoS allotment for this timeslice. 1345 * Additional I/O will then be queued. 1346 */ 1347 set_thread(0); 1348 for (i = 0; i < 10; i++) { 1349 bdev_io_status[0] = SPDK_BDEV_IO_STATUS_PENDING; 1350 rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &bdev_io_status[0]); 1351 CU_ASSERT(rc == 0); 1352 CU_ASSERT(bdev_io_status[0] == SPDK_BDEV_IO_STATUS_PENDING); 1353 poll_thread(0); 1354 stub_complete_io(g_bdev.io_target, 0); 1355 CU_ASSERT(bdev_io_status[0] == SPDK_BDEV_IO_STATUS_SUCCESS); 1356 } 1357 1358 /* 1359 * Send two more I/O. These I/O will be queued since the current timeslice allotment has been 1360 * filled already. We want to test that when QoS is disabled that these two I/O: 1361 * 1) are not aborted 1362 * 2) are sent back to their original thread for resubmission 1363 */ 1364 bdev_io_status[0] = SPDK_BDEV_IO_STATUS_PENDING; 1365 rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &bdev_io_status[0]); 1366 CU_ASSERT(rc == 0); 1367 CU_ASSERT(bdev_io_status[0] == SPDK_BDEV_IO_STATUS_PENDING); 1368 set_thread(1); 1369 bdev_io_status[1] = SPDK_BDEV_IO_STATUS_PENDING; 1370 rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &bdev_io_status[1]); 1371 CU_ASSERT(rc == 0); 1372 CU_ASSERT(bdev_io_status[1] == SPDK_BDEV_IO_STATUS_PENDING); 1373 poll_threads(); 1374 1375 /* 1376 * Disable QoS: Read/Write IOPS, Read/Write byte, 1377 * Read only byte rate limits 1378 */ 1379 status = -1; 1380 limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT] = 0; 1381 limits[SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT] = 0; 1382 limits[SPDK_BDEV_QOS_R_BPS_RATE_LIMIT] = 0; 1383 spdk_bdev_set_qos_rate_limits(bdev, limits, qos_dynamic_enable_done, &status); 1384 poll_threads(); 1385 CU_ASSERT(status == 0); 1386 CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) != 0); 1387 CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) != 0); 1388 1389 /* Disable QoS: Write only Byte per second rate limit */ 1390 status = -1; 1391 limits[SPDK_BDEV_QOS_W_BPS_RATE_LIMIT] = 0; 1392 spdk_bdev_set_qos_rate_limits(bdev, limits, qos_dynamic_enable_done, &status); 1393 poll_threads(); 1394 CU_ASSERT(status == 0); 1395 CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) == 0); 1396 CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) == 0); 1397 1398 /* 1399 * All I/O should have been resubmitted back on their original thread. Complete 1400 * all I/O on thread 0, and ensure that only the thread 0 I/O was completed. 1401 */ 1402 set_thread(0); 1403 stub_complete_io(g_bdev.io_target, 0); 1404 poll_threads(); 1405 CU_ASSERT(bdev_io_status[0] == SPDK_BDEV_IO_STATUS_SUCCESS); 1406 CU_ASSERT(bdev_io_status[1] == SPDK_BDEV_IO_STATUS_PENDING); 1407 1408 /* Now complete all I/O on thread 1 and ensure the thread 1 I/O was completed. */ 1409 set_thread(1); 1410 stub_complete_io(g_bdev.io_target, 0); 1411 poll_threads(); 1412 CU_ASSERT(bdev_io_status[1] == SPDK_BDEV_IO_STATUS_SUCCESS); 1413 1414 /* Disable QoS again */ 1415 status = -1; 1416 limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT] = 0; 1417 spdk_bdev_set_qos_rate_limits(bdev, limits, qos_dynamic_enable_done, &status); 1418 poll_threads(); 1419 CU_ASSERT(status == 0); /* This should succeed */ 1420 CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) == 0); 1421 CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) == 0); 1422 1423 /* Enable QoS on thread 0 */ 1424 status = -1; 1425 limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT] = 10000; 1426 spdk_bdev_set_qos_rate_limits(bdev, limits, qos_dynamic_enable_done, &status); 1427 poll_threads(); 1428 CU_ASSERT(status == 0); 1429 CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) != 0); 1430 CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) != 0); 1431 1432 /* Disable QoS on thread 1 */ 1433 set_thread(1); 1434 status = -1; 1435 limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT] = 0; 1436 spdk_bdev_set_qos_rate_limits(bdev, limits, qos_dynamic_enable_done, &status); 1437 /* Don't poll yet. This should leave the channels with QoS enabled */ 1438 CU_ASSERT(status == -1); 1439 CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) != 0); 1440 CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) != 0); 1441 1442 /* Enable QoS. This should immediately fail because the previous disable QoS hasn't completed. */ 1443 second_status = 0; 1444 limits[SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT] = 10; 1445 spdk_bdev_set_qos_rate_limits(bdev, limits, qos_dynamic_enable_done, &second_status); 1446 poll_threads(); 1447 CU_ASSERT(status == 0); /* The disable should succeed */ 1448 CU_ASSERT(second_status < 0); /* The enable should fail */ 1449 CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) == 0); 1450 CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) == 0); 1451 1452 /* Enable QoS on thread 1. This should succeed now that the disable has completed. */ 1453 status = -1; 1454 limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT] = 10000; 1455 spdk_bdev_set_qos_rate_limits(bdev, limits, qos_dynamic_enable_done, &status); 1456 poll_threads(); 1457 CU_ASSERT(status == 0); 1458 CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) != 0); 1459 CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) != 0); 1460 1461 /* Tear down the channels */ 1462 set_thread(0); 1463 spdk_put_io_channel(io_ch[0]); 1464 set_thread(1); 1465 spdk_put_io_channel(io_ch[1]); 1466 poll_threads(); 1467 1468 set_thread(0); 1469 teardown_test(); 1470 } 1471 1472 static void 1473 histogram_status_cb(void *cb_arg, int status) 1474 { 1475 g_status = status; 1476 } 1477 1478 static void 1479 histogram_data_cb(void *cb_arg, int status, struct spdk_histogram_data *histogram) 1480 { 1481 g_status = status; 1482 g_histogram = histogram; 1483 } 1484 1485 static void 1486 histogram_io_count(void *ctx, uint64_t start, uint64_t end, uint64_t count, 1487 uint64_t total, uint64_t so_far) 1488 { 1489 g_count += count; 1490 } 1491 1492 static void 1493 bdev_histograms_mt(void) 1494 { 1495 struct spdk_io_channel *ch[2]; 1496 struct spdk_histogram_data *histogram; 1497 uint8_t buf[4096]; 1498 int status = false; 1499 int rc; 1500 1501 1502 setup_test(); 1503 1504 set_thread(0); 1505 ch[0] = spdk_bdev_get_io_channel(g_desc); 1506 CU_ASSERT(ch[0] != NULL); 1507 1508 set_thread(1); 1509 ch[1] = spdk_bdev_get_io_channel(g_desc); 1510 CU_ASSERT(ch[1] != NULL); 1511 1512 1513 /* Enable histogram */ 1514 spdk_bdev_histogram_enable(&g_bdev.bdev, histogram_status_cb, NULL, true); 1515 poll_threads(); 1516 CU_ASSERT(g_status == 0); 1517 CU_ASSERT(g_bdev.bdev.internal.histogram_enabled == true); 1518 1519 /* Allocate histogram */ 1520 histogram = spdk_histogram_data_alloc(); 1521 1522 /* Check if histogram is zeroed */ 1523 spdk_bdev_histogram_get(&g_bdev.bdev, histogram, histogram_data_cb, NULL); 1524 poll_threads(); 1525 CU_ASSERT(g_status == 0); 1526 SPDK_CU_ASSERT_FATAL(g_histogram != NULL); 1527 1528 g_count = 0; 1529 spdk_histogram_data_iterate(g_histogram, histogram_io_count, NULL); 1530 1531 CU_ASSERT(g_count == 0); 1532 1533 set_thread(0); 1534 rc = spdk_bdev_write_blocks(g_desc, ch[0], &buf, 0, 1, io_during_io_done, &status); 1535 CU_ASSERT(rc == 0); 1536 1537 spdk_delay_us(10); 1538 stub_complete_io(g_bdev.io_target, 1); 1539 poll_threads(); 1540 CU_ASSERT(status == true); 1541 1542 1543 set_thread(1); 1544 rc = spdk_bdev_read_blocks(g_desc, ch[1], &buf, 0, 1, io_during_io_done, &status); 1545 CU_ASSERT(rc == 0); 1546 1547 spdk_delay_us(10); 1548 stub_complete_io(g_bdev.io_target, 1); 1549 poll_threads(); 1550 CU_ASSERT(status == true); 1551 1552 set_thread(0); 1553 1554 /* Check if histogram gathered data from all I/O channels */ 1555 spdk_bdev_histogram_get(&g_bdev.bdev, histogram, histogram_data_cb, NULL); 1556 poll_threads(); 1557 CU_ASSERT(g_status == 0); 1558 CU_ASSERT(g_bdev.bdev.internal.histogram_enabled == true); 1559 SPDK_CU_ASSERT_FATAL(g_histogram != NULL); 1560 1561 g_count = 0; 1562 spdk_histogram_data_iterate(g_histogram, histogram_io_count, NULL); 1563 CU_ASSERT(g_count == 2); 1564 1565 /* Disable histogram */ 1566 spdk_bdev_histogram_enable(&g_bdev.bdev, histogram_status_cb, NULL, false); 1567 poll_threads(); 1568 CU_ASSERT(g_status == 0); 1569 CU_ASSERT(g_bdev.bdev.internal.histogram_enabled == false); 1570 1571 spdk_histogram_data_free(histogram); 1572 1573 /* Tear down the channels */ 1574 set_thread(0); 1575 spdk_put_io_channel(ch[0]); 1576 set_thread(1); 1577 spdk_put_io_channel(ch[1]); 1578 poll_threads(); 1579 set_thread(0); 1580 teardown_test(); 1581 1582 } 1583 1584 struct timeout_io_cb_arg { 1585 struct iovec iov; 1586 uint8_t type; 1587 }; 1588 1589 static int 1590 bdev_channel_count_submitted_io(struct spdk_bdev_channel *ch) 1591 { 1592 struct spdk_bdev_io *bdev_io; 1593 int n = 0; 1594 1595 if (!ch) { 1596 return -1; 1597 } 1598 1599 TAILQ_FOREACH(bdev_io, &ch->io_submitted, internal.ch_link) { 1600 n++; 1601 } 1602 1603 return n; 1604 } 1605 1606 static void 1607 bdev_channel_io_timeout_cb(void *cb_arg, struct spdk_bdev_io *bdev_io) 1608 { 1609 struct timeout_io_cb_arg *ctx = cb_arg; 1610 1611 ctx->type = bdev_io->type; 1612 ctx->iov.iov_base = bdev_io->iov.iov_base; 1613 ctx->iov.iov_len = bdev_io->iov.iov_len; 1614 } 1615 1616 static bool g_io_done; 1617 1618 static void 1619 io_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 1620 { 1621 g_io_done = true; 1622 spdk_bdev_free_io(bdev_io); 1623 } 1624 1625 static void 1626 bdev_set_io_timeout_mt(void) 1627 { 1628 struct spdk_io_channel *ch[3]; 1629 struct spdk_bdev_channel *bdev_ch[3]; 1630 struct timeout_io_cb_arg cb_arg; 1631 1632 setup_test(); 1633 1634 g_bdev.bdev.optimal_io_boundary = 16; 1635 g_bdev.bdev.split_on_optimal_io_boundary = true; 1636 1637 set_thread(0); 1638 ch[0] = spdk_bdev_get_io_channel(g_desc); 1639 CU_ASSERT(ch[0] != NULL); 1640 1641 set_thread(1); 1642 ch[1] = spdk_bdev_get_io_channel(g_desc); 1643 CU_ASSERT(ch[1] != NULL); 1644 1645 set_thread(2); 1646 ch[2] = spdk_bdev_get_io_channel(g_desc); 1647 CU_ASSERT(ch[2] != NULL); 1648 1649 /* Multi-thread mode 1650 * 1, Check the poller was registered successfully 1651 * 2, Check the timeout IO and ensure the IO was the submitted by user 1652 * 3, Check the link int the bdev_ch works right. 1653 * 4, Close desc and put io channel during the timeout poller is polling 1654 */ 1655 1656 /* In desc thread set the timeout */ 1657 set_thread(0); 1658 CU_ASSERT(spdk_bdev_set_timeout(g_desc, 5, bdev_channel_io_timeout_cb, &cb_arg) == 0); 1659 CU_ASSERT(g_desc->io_timeout_poller != NULL); 1660 CU_ASSERT(g_desc->cb_fn == bdev_channel_io_timeout_cb); 1661 CU_ASSERT(g_desc->cb_arg == &cb_arg); 1662 1663 /* check the IO submitted list and timeout handler */ 1664 CU_ASSERT(spdk_bdev_read_blocks(g_desc, ch[0], (void *)0x2000, 0, 1, io_done, NULL) == 0); 1665 bdev_ch[0] = spdk_io_channel_get_ctx(ch[0]); 1666 CU_ASSERT(bdev_channel_count_submitted_io(bdev_ch[0]) == 1); 1667 1668 set_thread(1); 1669 CU_ASSERT(spdk_bdev_write_blocks(g_desc, ch[1], (void *)0x1000, 0, 1, io_done, NULL) == 0); 1670 bdev_ch[1] = spdk_io_channel_get_ctx(ch[1]); 1671 CU_ASSERT(bdev_channel_count_submitted_io(bdev_ch[1]) == 1); 1672 1673 /* Now test that a single-vector command is split correctly. 1674 * Offset 14, length 8, payload 0xF000 1675 * Child - Offset 14, length 2, payload 0xF000 1676 * Child - Offset 16, length 6, payload 0xF000 + 2 * 512 1677 * 1678 * Set up the expected values before calling spdk_bdev_read_blocks 1679 */ 1680 set_thread(2); 1681 CU_ASSERT(spdk_bdev_read_blocks(g_desc, ch[2], (void *)0xF000, 14, 8, io_done, NULL) == 0); 1682 bdev_ch[2] = spdk_io_channel_get_ctx(ch[2]); 1683 CU_ASSERT(bdev_channel_count_submitted_io(bdev_ch[2]) == 3); 1684 1685 set_thread(0); 1686 memset(&cb_arg, 0, sizeof(cb_arg)); 1687 spdk_delay_us(3 * spdk_get_ticks_hz()); 1688 poll_threads(); 1689 CU_ASSERT(cb_arg.type == 0); 1690 CU_ASSERT(cb_arg.iov.iov_base == (void *)0x0); 1691 CU_ASSERT(cb_arg.iov.iov_len == 0); 1692 1693 /* Now the time reach the limit */ 1694 spdk_delay_us(3 * spdk_get_ticks_hz()); 1695 poll_thread(0); 1696 CU_ASSERT(cb_arg.type == SPDK_BDEV_IO_TYPE_READ); 1697 CU_ASSERT(cb_arg.iov.iov_base == (void *)0x2000); 1698 CU_ASSERT(cb_arg.iov.iov_len == 1 * g_bdev.bdev.blocklen); 1699 stub_complete_io(g_bdev.io_target, 1); 1700 CU_ASSERT(bdev_channel_count_submitted_io(bdev_ch[0]) == 0); 1701 1702 memset(&cb_arg, 0, sizeof(cb_arg)); 1703 set_thread(1); 1704 poll_thread(1); 1705 CU_ASSERT(cb_arg.type == SPDK_BDEV_IO_TYPE_WRITE); 1706 CU_ASSERT(cb_arg.iov.iov_base == (void *)0x1000); 1707 CU_ASSERT(cb_arg.iov.iov_len == 1 * g_bdev.bdev.blocklen); 1708 stub_complete_io(g_bdev.io_target, 1); 1709 CU_ASSERT(bdev_channel_count_submitted_io(bdev_ch[1]) == 0); 1710 1711 memset(&cb_arg, 0, sizeof(cb_arg)); 1712 set_thread(2); 1713 poll_thread(2); 1714 CU_ASSERT(cb_arg.type == SPDK_BDEV_IO_TYPE_READ); 1715 CU_ASSERT(cb_arg.iov.iov_base == (void *)0xF000); 1716 CU_ASSERT(cb_arg.iov.iov_len == 8 * g_bdev.bdev.blocklen); 1717 stub_complete_io(g_bdev.io_target, 1); 1718 CU_ASSERT(bdev_channel_count_submitted_io(bdev_ch[2]) == 2); 1719 stub_complete_io(g_bdev.io_target, 1); 1720 CU_ASSERT(bdev_channel_count_submitted_io(bdev_ch[2]) == 0); 1721 1722 /* Run poll_timeout_done() it means complete the timeout poller */ 1723 set_thread(0); 1724 poll_thread(0); 1725 CU_ASSERT(g_desc->refs == 0); 1726 CU_ASSERT(spdk_bdev_read_blocks(g_desc, ch[0], (void *)0x1000, 0, 1, io_done, NULL) == 0); 1727 set_thread(1); 1728 CU_ASSERT(spdk_bdev_write_blocks(g_desc, ch[1], (void *)0x2000, 0, 2, io_done, NULL) == 0); 1729 set_thread(2); 1730 CU_ASSERT(spdk_bdev_read_blocks(g_desc, ch[2], (void *)0x3000, 0, 3, io_done, NULL) == 0); 1731 1732 /* Trigger timeout poller to run again, desc->refs is incremented. 1733 * In thread 0 we destroy the io channel before timeout poller runs. 1734 * Timeout callback is not called on thread 0. 1735 */ 1736 spdk_delay_us(6 * spdk_get_ticks_hz()); 1737 memset(&cb_arg, 0, sizeof(cb_arg)); 1738 set_thread(0); 1739 stub_complete_io(g_bdev.io_target, 1); 1740 spdk_put_io_channel(ch[0]); 1741 poll_thread(0); 1742 CU_ASSERT(g_desc->refs == 1) 1743 CU_ASSERT(cb_arg.type == 0); 1744 CU_ASSERT(cb_arg.iov.iov_base == (void *)0x0); 1745 CU_ASSERT(cb_arg.iov.iov_len == 0); 1746 1747 /* In thread 1 timeout poller runs then we destroy the io channel 1748 * Timeout callback is called on thread 1. 1749 */ 1750 memset(&cb_arg, 0, sizeof(cb_arg)); 1751 set_thread(1); 1752 poll_thread(1); 1753 stub_complete_io(g_bdev.io_target, 1); 1754 spdk_put_io_channel(ch[1]); 1755 poll_thread(1); 1756 CU_ASSERT(cb_arg.type == SPDK_BDEV_IO_TYPE_WRITE); 1757 CU_ASSERT(cb_arg.iov.iov_base == (void *)0x2000); 1758 CU_ASSERT(cb_arg.iov.iov_len == 2 * g_bdev.bdev.blocklen); 1759 1760 /* Close the desc. 1761 * Unregister the timeout poller first. 1762 * Then decrement desc->refs but it's not zero yet so desc is not freed. 1763 */ 1764 set_thread(0); 1765 spdk_bdev_close(g_desc); 1766 CU_ASSERT(g_desc->refs == 1); 1767 CU_ASSERT(g_desc->io_timeout_poller == NULL); 1768 1769 /* Timeout poller runs on thread 2 then we destroy the io channel. 1770 * Desc is closed so we would exit the timeout poller directly. 1771 * timeout callback is not called on thread 2. 1772 */ 1773 memset(&cb_arg, 0, sizeof(cb_arg)); 1774 set_thread(2); 1775 poll_thread(2); 1776 stub_complete_io(g_bdev.io_target, 1); 1777 spdk_put_io_channel(ch[2]); 1778 poll_thread(2); 1779 CU_ASSERT(cb_arg.type == 0); 1780 CU_ASSERT(cb_arg.iov.iov_base == (void *)0x0); 1781 CU_ASSERT(cb_arg.iov.iov_len == 0); 1782 1783 set_thread(0); 1784 poll_thread(0); 1785 g_teardown_done = false; 1786 unregister_bdev(&g_bdev); 1787 spdk_io_device_unregister(&g_io_device, NULL); 1788 spdk_bdev_finish(finish_cb, NULL); 1789 poll_threads(); 1790 memset(&g_bdev, 0, sizeof(g_bdev)); 1791 CU_ASSERT(g_teardown_done == true); 1792 g_teardown_done = false; 1793 free_threads(); 1794 free_cores(); 1795 } 1796 1797 static bool g_io_done2; 1798 static bool g_lock_lba_range_done; 1799 static bool g_unlock_lba_range_done; 1800 1801 static void 1802 io_done2(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 1803 { 1804 g_io_done2 = true; 1805 spdk_bdev_free_io(bdev_io); 1806 } 1807 1808 static void 1809 lock_lba_range_done(void *ctx, int status) 1810 { 1811 g_lock_lba_range_done = true; 1812 } 1813 1814 static void 1815 unlock_lba_range_done(void *ctx, int status) 1816 { 1817 g_unlock_lba_range_done = true; 1818 } 1819 1820 static uint32_t 1821 stub_channel_outstanding_cnt(void *io_target) 1822 { 1823 struct spdk_io_channel *_ch = spdk_get_io_channel(io_target); 1824 struct ut_bdev_channel *ch = spdk_io_channel_get_ctx(_ch); 1825 uint32_t outstanding_cnt; 1826 1827 outstanding_cnt = ch->outstanding_cnt; 1828 1829 spdk_put_io_channel(_ch); 1830 return outstanding_cnt; 1831 } 1832 1833 static void 1834 lock_lba_range_then_submit_io(void) 1835 { 1836 struct spdk_bdev_desc *desc = NULL; 1837 void *io_target; 1838 struct spdk_io_channel *io_ch[3]; 1839 struct spdk_bdev_channel *bdev_ch[3]; 1840 struct lba_range *range; 1841 char buf[4096]; 1842 int ctx0, ctx1, ctx2; 1843 int rc; 1844 1845 setup_test(); 1846 1847 io_target = g_bdev.io_target; 1848 desc = g_desc; 1849 1850 set_thread(0); 1851 io_ch[0] = spdk_bdev_get_io_channel(desc); 1852 bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]); 1853 CU_ASSERT(io_ch[0] != NULL); 1854 1855 set_thread(1); 1856 io_ch[1] = spdk_bdev_get_io_channel(desc); 1857 bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]); 1858 CU_ASSERT(io_ch[1] != NULL); 1859 1860 set_thread(0); 1861 g_lock_lba_range_done = false; 1862 rc = bdev_lock_lba_range(desc, io_ch[0], 20, 10, lock_lba_range_done, &ctx0); 1863 CU_ASSERT(rc == 0); 1864 poll_threads(); 1865 1866 /* The lock should immediately become valid, since there are no outstanding 1867 * write I/O. 1868 */ 1869 CU_ASSERT(g_lock_lba_range_done == true); 1870 range = TAILQ_FIRST(&bdev_ch[0]->locked_ranges); 1871 SPDK_CU_ASSERT_FATAL(range != NULL); 1872 CU_ASSERT(range->offset == 20); 1873 CU_ASSERT(range->length == 10); 1874 CU_ASSERT(range->owner_ch == bdev_ch[0]); 1875 1876 g_io_done = false; 1877 CU_ASSERT(TAILQ_EMPTY(&bdev_ch[0]->io_locked)); 1878 rc = spdk_bdev_read_blocks(desc, io_ch[0], buf, 20, 1, io_done, &ctx0); 1879 CU_ASSERT(rc == 0); 1880 CU_ASSERT(stub_channel_outstanding_cnt(io_target) == 1); 1881 1882 stub_complete_io(io_target, 1); 1883 poll_threads(); 1884 CU_ASSERT(g_io_done == true); 1885 CU_ASSERT(TAILQ_EMPTY(&bdev_ch[0]->io_locked)); 1886 1887 /* Try a write I/O. This should actually be allowed to execute, since the channel 1888 * holding the lock is submitting the write I/O. 1889 */ 1890 g_io_done = false; 1891 CU_ASSERT(TAILQ_EMPTY(&bdev_ch[0]->io_locked)); 1892 rc = spdk_bdev_write_blocks(desc, io_ch[0], buf, 20, 1, io_done, &ctx0); 1893 CU_ASSERT(rc == 0); 1894 CU_ASSERT(stub_channel_outstanding_cnt(io_target) == 1); 1895 1896 stub_complete_io(io_target, 1); 1897 poll_threads(); 1898 CU_ASSERT(g_io_done == true); 1899 CU_ASSERT(TAILQ_EMPTY(&bdev_ch[0]->io_locked)); 1900 1901 /* Try a write I/O. This should get queued in the io_locked tailq. */ 1902 set_thread(1); 1903 g_io_done = false; 1904 CU_ASSERT(TAILQ_EMPTY(&bdev_ch[1]->io_locked)); 1905 rc = spdk_bdev_write_blocks(desc, io_ch[1], buf, 20, 1, io_done, &ctx1); 1906 CU_ASSERT(rc == 0); 1907 poll_threads(); 1908 CU_ASSERT(stub_channel_outstanding_cnt(io_target) == 0); 1909 CU_ASSERT(!TAILQ_EMPTY(&bdev_ch[1]->io_locked)); 1910 CU_ASSERT(g_io_done == false); 1911 1912 /* Try to unlock the lba range using thread 1's io_ch. This should fail. */ 1913 rc = bdev_unlock_lba_range(desc, io_ch[1], 20, 10, unlock_lba_range_done, &ctx1); 1914 CU_ASSERT(rc == -EINVAL); 1915 1916 /* Now create a new channel and submit a write I/O with it. This should also be queued. 1917 * The new channel should inherit the active locks from the bdev's internal list. 1918 */ 1919 set_thread(2); 1920 io_ch[2] = spdk_bdev_get_io_channel(desc); 1921 bdev_ch[2] = spdk_io_channel_get_ctx(io_ch[2]); 1922 CU_ASSERT(io_ch[2] != NULL); 1923 1924 g_io_done2 = false; 1925 CU_ASSERT(TAILQ_EMPTY(&bdev_ch[2]->io_locked)); 1926 rc = spdk_bdev_write_blocks(desc, io_ch[2], buf, 22, 2, io_done2, &ctx2); 1927 CU_ASSERT(rc == 0); 1928 poll_threads(); 1929 CU_ASSERT(stub_channel_outstanding_cnt(io_target) == 0); 1930 CU_ASSERT(!TAILQ_EMPTY(&bdev_ch[2]->io_locked)); 1931 CU_ASSERT(g_io_done2 == false); 1932 1933 set_thread(0); 1934 rc = bdev_unlock_lba_range(desc, io_ch[0], 20, 10, unlock_lba_range_done, &ctx0); 1935 CU_ASSERT(rc == 0); 1936 poll_threads(); 1937 CU_ASSERT(TAILQ_EMPTY(&bdev_ch[0]->locked_ranges)); 1938 1939 /* The LBA range is unlocked, so the write IOs should now have started execution. */ 1940 CU_ASSERT(TAILQ_EMPTY(&bdev_ch[1]->io_locked)); 1941 CU_ASSERT(TAILQ_EMPTY(&bdev_ch[2]->io_locked)); 1942 1943 set_thread(1); 1944 CU_ASSERT(stub_channel_outstanding_cnt(io_target) == 1); 1945 stub_complete_io(io_target, 1); 1946 set_thread(2); 1947 CU_ASSERT(stub_channel_outstanding_cnt(io_target) == 1); 1948 stub_complete_io(io_target, 1); 1949 1950 poll_threads(); 1951 CU_ASSERT(g_io_done == true); 1952 CU_ASSERT(g_io_done2 == true); 1953 1954 /* Tear down the channels */ 1955 set_thread(0); 1956 spdk_put_io_channel(io_ch[0]); 1957 set_thread(1); 1958 spdk_put_io_channel(io_ch[1]); 1959 set_thread(2); 1960 spdk_put_io_channel(io_ch[2]); 1961 poll_threads(); 1962 set_thread(0); 1963 teardown_test(); 1964 } 1965 1966 int 1967 main(int argc, char **argv) 1968 { 1969 CU_pSuite suite = NULL; 1970 unsigned int num_failures; 1971 1972 CU_set_error_action(CUEA_ABORT); 1973 CU_initialize_registry(); 1974 1975 suite = CU_add_suite("bdev", NULL, NULL); 1976 1977 CU_ADD_TEST(suite, basic); 1978 CU_ADD_TEST(suite, unregister_and_close); 1979 CU_ADD_TEST(suite, basic_qos); 1980 CU_ADD_TEST(suite, put_channel_during_reset); 1981 CU_ADD_TEST(suite, aborted_reset); 1982 CU_ADD_TEST(suite, io_during_reset); 1983 CU_ADD_TEST(suite, io_during_qos_queue); 1984 CU_ADD_TEST(suite, io_during_qos_reset); 1985 CU_ADD_TEST(suite, enomem); 1986 CU_ADD_TEST(suite, enomem_multi_bdev); 1987 CU_ADD_TEST(suite, enomem_multi_io_target); 1988 CU_ADD_TEST(suite, qos_dynamic_enable); 1989 CU_ADD_TEST(suite, bdev_histograms_mt); 1990 CU_ADD_TEST(suite, bdev_set_io_timeout_mt); 1991 CU_ADD_TEST(suite, lock_lba_range_then_submit_io); 1992 1993 CU_basic_set_mode(CU_BRM_VERBOSE); 1994 CU_basic_run_tests(); 1995 num_failures = CU_get_number_of_failures(); 1996 CU_cleanup_registry(); 1997 return num_failures; 1998 } 1999