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 DEFINE_STUB(spdk_conf_find_section, struct spdk_conf_section *, (struct spdk_conf *cp, 48 const char *name), NULL); 49 DEFINE_STUB(spdk_conf_section_get_nmval, char *, 50 (struct spdk_conf_section *sp, const char *key, int idx1, int idx2), NULL); 51 DEFINE_STUB(spdk_conf_section_get_intval, int, (struct spdk_conf_section *sp, const char *key), -1); 52 53 struct spdk_trace_histories *g_trace_histories; 54 DEFINE_STUB_V(spdk_trace_add_register_fn, (struct spdk_trace_register_fn *reg_fn)); 55 DEFINE_STUB_V(spdk_trace_register_owner, (uint8_t type, char id_prefix)); 56 DEFINE_STUB_V(spdk_trace_register_object, (uint8_t type, char id_prefix)); 57 DEFINE_STUB_V(spdk_trace_register_description, (const char *name, const char *short_name, 58 uint16_t tpoint_id, uint8_t owner_type, 59 uint8_t object_type, uint8_t new_object, 60 uint8_t arg1_is_ptr, const char *arg1_name)); 61 DEFINE_STUB_V(_spdk_trace_record, (uint64_t tsc, uint16_t tpoint_id, uint16_t poller_id, 62 uint32_t size, uint64_t object_id, uint64_t arg1)); 63 64 struct ut_bdev { 65 struct spdk_bdev bdev; 66 void *io_target; 67 }; 68 69 struct ut_bdev_channel { 70 TAILQ_HEAD(, spdk_bdev_io) outstanding_io; 71 uint32_t outstanding_cnt; 72 uint32_t avail_cnt; 73 }; 74 75 int g_io_device; 76 struct ut_bdev g_bdev; 77 struct spdk_bdev_desc *g_desc; 78 bool g_teardown_done = false; 79 bool g_get_io_channel = true; 80 bool g_create_ch = true; 81 bool g_init_complete_called = false; 82 bool g_fini_start_called = true; 83 int g_status = 0; 84 int g_count = 0; 85 struct spdk_histogram_data *g_histogram = NULL; 86 87 static int 88 stub_create_ch(void *io_device, void *ctx_buf) 89 { 90 struct ut_bdev_channel *ch = ctx_buf; 91 92 if (g_create_ch == false) { 93 return -1; 94 } 95 96 TAILQ_INIT(&ch->outstanding_io); 97 ch->outstanding_cnt = 0; 98 /* 99 * When avail gets to 0, the submit_request function will return ENOMEM. 100 * Most tests to not want ENOMEM to occur, so by default set this to a 101 * big value that won't get hit. The ENOMEM tests can then override this 102 * value to something much smaller to induce ENOMEM conditions. 103 */ 104 ch->avail_cnt = 2048; 105 return 0; 106 } 107 108 static void 109 stub_destroy_ch(void *io_device, void *ctx_buf) 110 { 111 } 112 113 static struct spdk_io_channel * 114 stub_get_io_channel(void *ctx) 115 { 116 struct ut_bdev *ut_bdev = ctx; 117 118 if (g_get_io_channel == true) { 119 return spdk_get_io_channel(ut_bdev->io_target); 120 } else { 121 return NULL; 122 } 123 } 124 125 static int 126 stub_destruct(void *ctx) 127 { 128 return 0; 129 } 130 131 static void 132 stub_submit_request(struct spdk_io_channel *_ch, struct spdk_bdev_io *bdev_io) 133 { 134 struct ut_bdev_channel *ch = spdk_io_channel_get_ctx(_ch); 135 136 if (bdev_io->type == SPDK_BDEV_IO_TYPE_RESET) { 137 struct spdk_bdev_io *io; 138 139 while (!TAILQ_EMPTY(&ch->outstanding_io)) { 140 io = TAILQ_FIRST(&ch->outstanding_io); 141 TAILQ_REMOVE(&ch->outstanding_io, io, module_link); 142 ch->outstanding_cnt--; 143 spdk_bdev_io_complete(io, SPDK_BDEV_IO_STATUS_FAILED); 144 ch->avail_cnt++; 145 } 146 } 147 148 if (ch->avail_cnt > 0) { 149 TAILQ_INSERT_TAIL(&ch->outstanding_io, bdev_io, module_link); 150 ch->outstanding_cnt++; 151 ch->avail_cnt--; 152 } else { 153 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_NOMEM); 154 } 155 } 156 157 static uint32_t 158 stub_complete_io(void *io_target, uint32_t num_to_complete) 159 { 160 struct spdk_io_channel *_ch = spdk_get_io_channel(io_target); 161 struct ut_bdev_channel *ch = spdk_io_channel_get_ctx(_ch); 162 struct spdk_bdev_io *io; 163 bool complete_all = (num_to_complete == 0); 164 uint32_t num_completed = 0; 165 166 while (complete_all || num_completed < num_to_complete) { 167 if (TAILQ_EMPTY(&ch->outstanding_io)) { 168 break; 169 } 170 io = TAILQ_FIRST(&ch->outstanding_io); 171 TAILQ_REMOVE(&ch->outstanding_io, io, module_link); 172 ch->outstanding_cnt--; 173 spdk_bdev_io_complete(io, SPDK_BDEV_IO_STATUS_SUCCESS); 174 ch->avail_cnt++; 175 num_completed++; 176 } 177 178 spdk_put_io_channel(_ch); 179 return num_completed; 180 } 181 182 static struct spdk_bdev_fn_table fn_table = { 183 .get_io_channel = stub_get_io_channel, 184 .destruct = stub_destruct, 185 .submit_request = stub_submit_request, 186 }; 187 188 static int 189 module_init(void) 190 { 191 return 0; 192 } 193 194 static void 195 module_fini(void) 196 { 197 } 198 199 static void 200 init_complete(void) 201 { 202 g_init_complete_called = true; 203 } 204 205 static void 206 fini_start(void) 207 { 208 g_fini_start_called = true; 209 } 210 211 struct spdk_bdev_module bdev_ut_if = { 212 .name = "bdev_ut", 213 .module_init = module_init, 214 .module_fini = module_fini, 215 .init_complete = init_complete, 216 .fini_start = fini_start, 217 }; 218 219 SPDK_BDEV_MODULE_REGISTER(&bdev_ut_if) 220 221 static void 222 register_bdev(struct ut_bdev *ut_bdev, char *name, void *io_target) 223 { 224 memset(ut_bdev, 0, sizeof(*ut_bdev)); 225 226 ut_bdev->io_target = io_target; 227 ut_bdev->bdev.ctxt = ut_bdev; 228 ut_bdev->bdev.name = name; 229 ut_bdev->bdev.fn_table = &fn_table; 230 ut_bdev->bdev.module = &bdev_ut_if; 231 ut_bdev->bdev.blocklen = 4096; 232 ut_bdev->bdev.blockcnt = 1024; 233 234 spdk_bdev_register(&ut_bdev->bdev); 235 } 236 237 static void 238 unregister_bdev(struct ut_bdev *ut_bdev) 239 { 240 /* Handle any deferred messages. */ 241 poll_threads(); 242 spdk_bdev_unregister(&ut_bdev->bdev, NULL, NULL); 243 } 244 245 static void 246 bdev_init_cb(void *done, int rc) 247 { 248 CU_ASSERT(rc == 0); 249 *(bool *)done = true; 250 } 251 252 static void 253 setup_test(void) 254 { 255 bool done = false; 256 257 allocate_threads(BDEV_UT_NUM_THREADS); 258 set_thread(0); 259 spdk_bdev_initialize(bdev_init_cb, &done); 260 spdk_io_device_register(&g_io_device, stub_create_ch, stub_destroy_ch, 261 sizeof(struct ut_bdev_channel), NULL); 262 register_bdev(&g_bdev, "ut_bdev", &g_io_device); 263 spdk_bdev_open(&g_bdev.bdev, true, NULL, NULL, &g_desc); 264 } 265 266 static void 267 finish_cb(void *cb_arg) 268 { 269 g_teardown_done = true; 270 } 271 272 static void 273 teardown_test(void) 274 { 275 set_thread(0); 276 g_teardown_done = false; 277 spdk_bdev_close(g_desc); 278 g_desc = NULL; 279 unregister_bdev(&g_bdev); 280 spdk_io_device_unregister(&g_io_device, NULL); 281 spdk_bdev_finish(finish_cb, NULL); 282 poll_threads(); 283 memset(&g_bdev, 0, sizeof(g_bdev)); 284 CU_ASSERT(g_teardown_done == true); 285 g_teardown_done = false; 286 free_threads(); 287 } 288 289 static uint32_t 290 bdev_io_tailq_cnt(bdev_io_tailq_t *tailq) 291 { 292 struct spdk_bdev_io *io; 293 uint32_t cnt = 0; 294 295 TAILQ_FOREACH(io, tailq, internal.link) { 296 cnt++; 297 } 298 299 return cnt; 300 } 301 302 static void 303 basic(void) 304 { 305 g_init_complete_called = false; 306 setup_test(); 307 CU_ASSERT(g_init_complete_called == true); 308 309 set_thread(0); 310 311 g_get_io_channel = false; 312 g_ut_threads[0].ch = spdk_bdev_get_io_channel(g_desc); 313 CU_ASSERT(g_ut_threads[0].ch == NULL); 314 315 g_get_io_channel = true; 316 g_create_ch = false; 317 g_ut_threads[0].ch = spdk_bdev_get_io_channel(g_desc); 318 CU_ASSERT(g_ut_threads[0].ch == NULL); 319 320 g_get_io_channel = true; 321 g_create_ch = true; 322 g_ut_threads[0].ch = spdk_bdev_get_io_channel(g_desc); 323 CU_ASSERT(g_ut_threads[0].ch != NULL); 324 spdk_put_io_channel(g_ut_threads[0].ch); 325 326 g_fini_start_called = false; 327 teardown_test(); 328 CU_ASSERT(g_fini_start_called == true); 329 } 330 331 static void 332 _bdev_removed(void *done) 333 { 334 *(bool *)done = true; 335 } 336 337 static void 338 _bdev_unregistered(void *done, int rc) 339 { 340 CU_ASSERT(rc == 0); 341 *(bool *)done = true; 342 } 343 344 static void 345 unregister_and_close(void) 346 { 347 bool done, remove_notify; 348 struct spdk_bdev_desc *desc; 349 350 setup_test(); 351 set_thread(0); 352 353 /* setup_test() automatically opens the bdev, 354 * but this test needs to do that in a different 355 * way. */ 356 spdk_bdev_close(g_desc); 357 poll_threads(); 358 359 remove_notify = false; 360 spdk_bdev_open(&g_bdev.bdev, true, _bdev_removed, &remove_notify, &desc); 361 CU_ASSERT(remove_notify == false); 362 CU_ASSERT(desc != NULL); 363 364 /* There is an open descriptor on the device. Unregister it, 365 * which can't proceed until the descriptor is closed. */ 366 done = false; 367 spdk_bdev_unregister(&g_bdev.bdev, _bdev_unregistered, &done); 368 /* No polling has occurred, so neither of these should execute */ 369 CU_ASSERT(remove_notify == false); 370 CU_ASSERT(done == false); 371 372 /* Prior to the unregister completing, close the descriptor */ 373 spdk_bdev_close(desc); 374 375 /* Poll the threads to allow all events to be processed */ 376 poll_threads(); 377 378 /* Remove notify should not have been called because the 379 * descriptor is already closed. */ 380 CU_ASSERT(remove_notify == false); 381 382 /* The unregister should have completed */ 383 CU_ASSERT(done == true); 384 385 spdk_bdev_finish(finish_cb, NULL); 386 poll_threads(); 387 free_threads(); 388 } 389 390 static void 391 reset_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 392 { 393 bool *done = cb_arg; 394 395 CU_ASSERT(success == true); 396 *done = true; 397 spdk_bdev_free_io(bdev_io); 398 } 399 400 static void 401 put_channel_during_reset(void) 402 { 403 struct spdk_io_channel *io_ch; 404 bool done = false; 405 406 setup_test(); 407 408 set_thread(0); 409 io_ch = spdk_bdev_get_io_channel(g_desc); 410 CU_ASSERT(io_ch != NULL); 411 412 /* 413 * Start a reset, but then put the I/O channel before 414 * the deferred messages for the reset get a chance to 415 * execute. 416 */ 417 spdk_bdev_reset(g_desc, io_ch, reset_done, &done); 418 spdk_put_io_channel(io_ch); 419 poll_threads(); 420 stub_complete_io(g_bdev.io_target, 0); 421 422 teardown_test(); 423 } 424 425 static void 426 aborted_reset_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 427 { 428 enum spdk_bdev_io_status *status = cb_arg; 429 430 *status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED; 431 spdk_bdev_free_io(bdev_io); 432 } 433 434 static void 435 aborted_reset(void) 436 { 437 struct spdk_io_channel *io_ch[2]; 438 enum spdk_bdev_io_status status1 = SPDK_BDEV_IO_STATUS_PENDING, 439 status2 = SPDK_BDEV_IO_STATUS_PENDING; 440 441 setup_test(); 442 443 set_thread(0); 444 io_ch[0] = spdk_bdev_get_io_channel(g_desc); 445 CU_ASSERT(io_ch[0] != NULL); 446 spdk_bdev_reset(g_desc, io_ch[0], aborted_reset_done, &status1); 447 poll_threads(); 448 CU_ASSERT(g_bdev.bdev.internal.reset_in_progress != NULL); 449 450 /* 451 * First reset has been submitted on ch0. Now submit a second 452 * reset on ch1 which will get queued since there is already a 453 * reset in progress. 454 */ 455 set_thread(1); 456 io_ch[1] = spdk_bdev_get_io_channel(g_desc); 457 CU_ASSERT(io_ch[1] != NULL); 458 spdk_bdev_reset(g_desc, io_ch[1], aborted_reset_done, &status2); 459 poll_threads(); 460 CU_ASSERT(g_bdev.bdev.internal.reset_in_progress != NULL); 461 462 /* 463 * Now destroy ch1. This will abort the queued reset. Check that 464 * the second reset was completed with failed status. Also check 465 * that bdev->internal.reset_in_progress != NULL, since the 466 * original reset has not been completed yet. This ensures that 467 * the bdev code is correctly noticing that the failed reset is 468 * *not* the one that had been submitted to the bdev module. 469 */ 470 set_thread(1); 471 spdk_put_io_channel(io_ch[1]); 472 poll_threads(); 473 CU_ASSERT(status2 == SPDK_BDEV_IO_STATUS_FAILED); 474 CU_ASSERT(g_bdev.bdev.internal.reset_in_progress != NULL); 475 476 /* 477 * Now complete the first reset, verify that it completed with SUCCESS 478 * status and that bdev->internal.reset_in_progress is also set back to NULL. 479 */ 480 set_thread(0); 481 spdk_put_io_channel(io_ch[0]); 482 stub_complete_io(g_bdev.io_target, 0); 483 poll_threads(); 484 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS); 485 CU_ASSERT(g_bdev.bdev.internal.reset_in_progress == NULL); 486 487 teardown_test(); 488 } 489 490 static void 491 io_during_io_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 492 { 493 enum spdk_bdev_io_status *status = cb_arg; 494 495 *status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED; 496 spdk_bdev_free_io(bdev_io); 497 } 498 499 static void 500 io_during_reset(void) 501 { 502 struct spdk_io_channel *io_ch[2]; 503 struct spdk_bdev_channel *bdev_ch[2]; 504 enum spdk_bdev_io_status status0, status1, status_reset; 505 int rc; 506 507 setup_test(); 508 509 /* 510 * First test normal case - submit an I/O on each of two channels (with no resets) 511 * and verify they complete successfully. 512 */ 513 set_thread(0); 514 io_ch[0] = spdk_bdev_get_io_channel(g_desc); 515 bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]); 516 CU_ASSERT(bdev_ch[0]->flags == 0); 517 status0 = SPDK_BDEV_IO_STATUS_PENDING; 518 rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0); 519 CU_ASSERT(rc == 0); 520 521 set_thread(1); 522 io_ch[1] = spdk_bdev_get_io_channel(g_desc); 523 bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]); 524 CU_ASSERT(bdev_ch[1]->flags == 0); 525 status1 = SPDK_BDEV_IO_STATUS_PENDING; 526 rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1); 527 CU_ASSERT(rc == 0); 528 529 poll_threads(); 530 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_PENDING); 531 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING); 532 533 set_thread(0); 534 stub_complete_io(g_bdev.io_target, 0); 535 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_SUCCESS); 536 537 set_thread(1); 538 stub_complete_io(g_bdev.io_target, 0); 539 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS); 540 541 /* 542 * Now submit a reset, and leave it pending while we submit I/O on two different 543 * channels. These I/O should be failed by the bdev layer since the reset is in 544 * progress. 545 */ 546 set_thread(0); 547 status_reset = SPDK_BDEV_IO_STATUS_PENDING; 548 rc = spdk_bdev_reset(g_desc, io_ch[0], io_during_io_done, &status_reset); 549 CU_ASSERT(rc == 0); 550 551 CU_ASSERT(bdev_ch[0]->flags == 0); 552 CU_ASSERT(bdev_ch[1]->flags == 0); 553 poll_threads(); 554 CU_ASSERT(bdev_ch[0]->flags == BDEV_CH_RESET_IN_PROGRESS); 555 CU_ASSERT(bdev_ch[1]->flags == BDEV_CH_RESET_IN_PROGRESS); 556 557 set_thread(0); 558 status0 = SPDK_BDEV_IO_STATUS_PENDING; 559 rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0); 560 CU_ASSERT(rc == 0); 561 562 set_thread(1); 563 status1 = SPDK_BDEV_IO_STATUS_PENDING; 564 rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1); 565 CU_ASSERT(rc == 0); 566 567 /* 568 * A reset is in progress so these read I/O should complete with failure. Note that we 569 * need to poll_threads() since I/O completed inline have their completion deferred. 570 */ 571 poll_threads(); 572 CU_ASSERT(status_reset == SPDK_BDEV_IO_STATUS_PENDING); 573 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_FAILED); 574 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_FAILED); 575 576 /* 577 * Complete the reset 578 */ 579 set_thread(0); 580 stub_complete_io(g_bdev.io_target, 0); 581 582 /* 583 * Only poll thread 0. We should not get a completion. 584 */ 585 poll_thread(0); 586 CU_ASSERT(status_reset == SPDK_BDEV_IO_STATUS_PENDING); 587 588 /* 589 * Poll both thread 0 and 1 so the messages can propagate and we 590 * get a completion. 591 */ 592 poll_threads(); 593 CU_ASSERT(status_reset == SPDK_BDEV_IO_STATUS_SUCCESS); 594 595 spdk_put_io_channel(io_ch[0]); 596 set_thread(1); 597 spdk_put_io_channel(io_ch[1]); 598 poll_threads(); 599 600 teardown_test(); 601 } 602 603 static void 604 basic_qos(void) 605 { 606 struct spdk_io_channel *io_ch[2]; 607 struct spdk_bdev_channel *bdev_ch[2]; 608 struct spdk_bdev *bdev; 609 enum spdk_bdev_io_status status; 610 int rc; 611 612 setup_test(); 613 614 /* Enable QoS */ 615 bdev = &g_bdev.bdev; 616 bdev->internal.qos = calloc(1, sizeof(*bdev->internal.qos)); 617 SPDK_CU_ASSERT_FATAL(bdev->internal.qos != NULL); 618 TAILQ_INIT(&bdev->internal.qos->queued); 619 /* 620 * Enable read/write IOPS, read only byte per second and 621 * read/write byte per second rate limits. 622 * In this case, all rate limits will take equal effect. 623 */ 624 /* 2000 read/write I/O per second, or 2 per millisecond */ 625 bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT].limit = 2000; 626 /* 8K read/write byte per millisecond with 4K block size */ 627 bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT].limit = 8192000; 628 /* 8K read only byte per millisecond with 4K block size */ 629 bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_R_BPS_RATE_LIMIT].limit = 8192000; 630 631 g_get_io_channel = true; 632 633 set_thread(0); 634 io_ch[0] = spdk_bdev_get_io_channel(g_desc); 635 bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]); 636 CU_ASSERT(bdev_ch[0]->flags == BDEV_CH_QOS_ENABLED); 637 638 set_thread(1); 639 io_ch[1] = spdk_bdev_get_io_channel(g_desc); 640 bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]); 641 CU_ASSERT(bdev_ch[1]->flags == BDEV_CH_QOS_ENABLED); 642 643 /* 644 * Send an I/O on thread 0, which is where the QoS thread is running. 645 */ 646 set_thread(0); 647 status = SPDK_BDEV_IO_STATUS_PENDING; 648 rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status); 649 CU_ASSERT(rc == 0); 650 CU_ASSERT(status == SPDK_BDEV_IO_STATUS_PENDING); 651 poll_threads(); 652 stub_complete_io(g_bdev.io_target, 0); 653 poll_threads(); 654 CU_ASSERT(status == SPDK_BDEV_IO_STATUS_SUCCESS); 655 656 /* Send an I/O on thread 1. The QoS thread is not running here. */ 657 status = SPDK_BDEV_IO_STATUS_PENDING; 658 set_thread(1); 659 rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status); 660 CU_ASSERT(rc == 0); 661 CU_ASSERT(status == SPDK_BDEV_IO_STATUS_PENDING); 662 poll_threads(); 663 /* Complete I/O on thread 1. This should not complete the I/O we submitted */ 664 stub_complete_io(g_bdev.io_target, 0); 665 poll_threads(); 666 CU_ASSERT(status == SPDK_BDEV_IO_STATUS_PENDING); 667 /* Now complete I/O on thread 0 */ 668 set_thread(0); 669 poll_threads(); 670 stub_complete_io(g_bdev.io_target, 0); 671 poll_threads(); 672 CU_ASSERT(status == SPDK_BDEV_IO_STATUS_SUCCESS); 673 674 /* Tear down the channels */ 675 set_thread(0); 676 spdk_put_io_channel(io_ch[0]); 677 set_thread(1); 678 spdk_put_io_channel(io_ch[1]); 679 poll_threads(); 680 set_thread(0); 681 682 /* Close the descriptor, which should stop the qos channel */ 683 spdk_bdev_close(g_desc); 684 poll_threads(); 685 CU_ASSERT(bdev->internal.qos->ch == NULL); 686 687 spdk_bdev_open(bdev, true, NULL, NULL, &g_desc); 688 689 /* Create the channels in reverse order. */ 690 set_thread(1); 691 io_ch[1] = spdk_bdev_get_io_channel(g_desc); 692 bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]); 693 CU_ASSERT(bdev_ch[1]->flags == BDEV_CH_QOS_ENABLED); 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 /* Confirm that the qos thread is now thread 1 */ 701 CU_ASSERT(bdev->internal.qos->ch == bdev_ch[1]); 702 703 /* Tear down the channels */ 704 set_thread(0); 705 spdk_put_io_channel(io_ch[0]); 706 set_thread(1); 707 spdk_put_io_channel(io_ch[1]); 708 poll_threads(); 709 710 set_thread(0); 711 712 teardown_test(); 713 } 714 715 static void 716 io_during_qos_queue(void) 717 { 718 struct spdk_io_channel *io_ch[2]; 719 struct spdk_bdev_channel *bdev_ch[2]; 720 struct spdk_bdev *bdev; 721 enum spdk_bdev_io_status status0, status1, status2; 722 int rc; 723 724 setup_test(); 725 MOCK_SET(spdk_get_ticks, 0); 726 727 /* Enable QoS */ 728 bdev = &g_bdev.bdev; 729 bdev->internal.qos = calloc(1, sizeof(*bdev->internal.qos)); 730 SPDK_CU_ASSERT_FATAL(bdev->internal.qos != NULL); 731 TAILQ_INIT(&bdev->internal.qos->queued); 732 /* 733 * Enable read/write IOPS, read only byte per sec, write only 734 * byte per sec and read/write byte per sec rate limits. 735 * In this case, both read only and write only byte per sec 736 * rate limit will take effect. 737 */ 738 /* 4000 read/write I/O per second, or 4 per millisecond */ 739 bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT].limit = 4000; 740 /* 8K byte per millisecond with 4K block size */ 741 bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT].limit = 8192000; 742 /* 4K byte per millisecond with 4K block size */ 743 bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_R_BPS_RATE_LIMIT].limit = 4096000; 744 /* 4K byte per millisecond with 4K block size */ 745 bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_W_BPS_RATE_LIMIT].limit = 4096000; 746 747 g_get_io_channel = true; 748 749 /* Create channels */ 750 set_thread(0); 751 io_ch[0] = spdk_bdev_get_io_channel(g_desc); 752 bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]); 753 CU_ASSERT(bdev_ch[0]->flags == BDEV_CH_QOS_ENABLED); 754 755 set_thread(1); 756 io_ch[1] = spdk_bdev_get_io_channel(g_desc); 757 bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]); 758 CU_ASSERT(bdev_ch[1]->flags == BDEV_CH_QOS_ENABLED); 759 760 /* Send two read I/Os */ 761 status1 = SPDK_BDEV_IO_STATUS_PENDING; 762 rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1); 763 CU_ASSERT(rc == 0); 764 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING); 765 set_thread(0); 766 status0 = SPDK_BDEV_IO_STATUS_PENDING; 767 rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0); 768 CU_ASSERT(rc == 0); 769 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_PENDING); 770 /* Send one write I/O */ 771 status2 = SPDK_BDEV_IO_STATUS_PENDING; 772 rc = spdk_bdev_write_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status2); 773 CU_ASSERT(rc == 0); 774 CU_ASSERT(status2 == SPDK_BDEV_IO_STATUS_PENDING); 775 776 /* Complete any I/O that arrived at the disk */ 777 poll_threads(); 778 set_thread(1); 779 stub_complete_io(g_bdev.io_target, 0); 780 set_thread(0); 781 stub_complete_io(g_bdev.io_target, 0); 782 poll_threads(); 783 784 /* Only one of the two read I/Os should complete. (logical XOR) */ 785 if (status0 == SPDK_BDEV_IO_STATUS_SUCCESS) { 786 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING); 787 } else { 788 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS); 789 } 790 /* The write I/O should complete. */ 791 CU_ASSERT(status2 == SPDK_BDEV_IO_STATUS_SUCCESS); 792 793 /* Advance in time by a millisecond */ 794 spdk_delay_us(1000); 795 796 /* Complete more I/O */ 797 poll_threads(); 798 set_thread(1); 799 stub_complete_io(g_bdev.io_target, 0); 800 set_thread(0); 801 stub_complete_io(g_bdev.io_target, 0); 802 poll_threads(); 803 804 /* Now the second read I/O should be done */ 805 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_SUCCESS); 806 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS); 807 808 /* Tear down the channels */ 809 set_thread(1); 810 spdk_put_io_channel(io_ch[1]); 811 set_thread(0); 812 spdk_put_io_channel(io_ch[0]); 813 poll_threads(); 814 815 teardown_test(); 816 } 817 818 static void 819 io_during_qos_reset(void) 820 { 821 struct spdk_io_channel *io_ch[2]; 822 struct spdk_bdev_channel *bdev_ch[2]; 823 struct spdk_bdev *bdev; 824 enum spdk_bdev_io_status status0, status1, reset_status; 825 int rc; 826 827 setup_test(); 828 MOCK_SET(spdk_get_ticks, 0); 829 830 /* Enable QoS */ 831 bdev = &g_bdev.bdev; 832 bdev->internal.qos = calloc(1, sizeof(*bdev->internal.qos)); 833 SPDK_CU_ASSERT_FATAL(bdev->internal.qos != NULL); 834 TAILQ_INIT(&bdev->internal.qos->queued); 835 /* 836 * Enable read/write IOPS, write only byte per sec and 837 * read/write byte per second rate limits. 838 * In this case, read/write byte per second rate limit will 839 * take effect first. 840 */ 841 /* 2000 read/write I/O per second, or 2 per millisecond */ 842 bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT].limit = 2000; 843 /* 4K byte per millisecond with 4K block size */ 844 bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT].limit = 4096000; 845 /* 8K byte per millisecond with 4K block size */ 846 bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_W_BPS_RATE_LIMIT].limit = 8192000; 847 848 g_get_io_channel = true; 849 850 /* Create channels */ 851 set_thread(0); 852 io_ch[0] = spdk_bdev_get_io_channel(g_desc); 853 bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]); 854 CU_ASSERT(bdev_ch[0]->flags == BDEV_CH_QOS_ENABLED); 855 856 set_thread(1); 857 io_ch[1] = spdk_bdev_get_io_channel(g_desc); 858 bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]); 859 CU_ASSERT(bdev_ch[1]->flags == BDEV_CH_QOS_ENABLED); 860 861 /* Send two I/O. One of these gets queued by QoS. The other is sitting at the disk. */ 862 status1 = SPDK_BDEV_IO_STATUS_PENDING; 863 rc = spdk_bdev_write_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1); 864 CU_ASSERT(rc == 0); 865 set_thread(0); 866 status0 = SPDK_BDEV_IO_STATUS_PENDING; 867 rc = spdk_bdev_write_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0); 868 CU_ASSERT(rc == 0); 869 870 poll_threads(); 871 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING); 872 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_PENDING); 873 874 /* Reset the bdev. */ 875 reset_status = SPDK_BDEV_IO_STATUS_PENDING; 876 rc = spdk_bdev_reset(g_desc, io_ch[0], io_during_io_done, &reset_status); 877 CU_ASSERT(rc == 0); 878 879 /* Complete any I/O that arrived at the disk */ 880 poll_threads(); 881 set_thread(1); 882 stub_complete_io(g_bdev.io_target, 0); 883 set_thread(0); 884 stub_complete_io(g_bdev.io_target, 0); 885 poll_threads(); 886 887 CU_ASSERT(reset_status == SPDK_BDEV_IO_STATUS_SUCCESS); 888 CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_FAILED); 889 CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_FAILED); 890 891 /* Tear down the channels */ 892 set_thread(1); 893 spdk_put_io_channel(io_ch[1]); 894 set_thread(0); 895 spdk_put_io_channel(io_ch[0]); 896 poll_threads(); 897 898 teardown_test(); 899 } 900 901 static void 902 enomem_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 903 { 904 enum spdk_bdev_io_status *status = cb_arg; 905 906 *status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED; 907 spdk_bdev_free_io(bdev_io); 908 } 909 910 static void 911 enomem(void) 912 { 913 struct spdk_io_channel *io_ch; 914 struct spdk_bdev_channel *bdev_ch; 915 struct spdk_bdev_shared_resource *shared_resource; 916 struct ut_bdev_channel *ut_ch; 917 const uint32_t IO_ARRAY_SIZE = 64; 918 const uint32_t AVAIL = 20; 919 enum spdk_bdev_io_status status[IO_ARRAY_SIZE], status_reset; 920 uint32_t nomem_cnt, i; 921 struct spdk_bdev_io *first_io; 922 int rc; 923 924 setup_test(); 925 926 set_thread(0); 927 io_ch = spdk_bdev_get_io_channel(g_desc); 928 bdev_ch = spdk_io_channel_get_ctx(io_ch); 929 shared_resource = bdev_ch->shared_resource; 930 ut_ch = spdk_io_channel_get_ctx(bdev_ch->channel); 931 ut_ch->avail_cnt = AVAIL; 932 933 /* First submit a number of IOs equal to what the channel can support. */ 934 for (i = 0; i < AVAIL; i++) { 935 status[i] = SPDK_BDEV_IO_STATUS_PENDING; 936 rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[i]); 937 CU_ASSERT(rc == 0); 938 } 939 CU_ASSERT(TAILQ_EMPTY(&shared_resource->nomem_io)); 940 941 /* 942 * Next, submit one additional I/O. This one should fail with ENOMEM and then go onto 943 * the enomem_io list. 944 */ 945 status[AVAIL] = SPDK_BDEV_IO_STATUS_PENDING; 946 rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[AVAIL]); 947 CU_ASSERT(rc == 0); 948 SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&shared_resource->nomem_io)); 949 first_io = TAILQ_FIRST(&shared_resource->nomem_io); 950 951 /* 952 * Now submit a bunch more I/O. These should all fail with ENOMEM and get queued behind 953 * the first_io above. 954 */ 955 for (i = AVAIL + 1; i < IO_ARRAY_SIZE; i++) { 956 status[i] = SPDK_BDEV_IO_STATUS_PENDING; 957 rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[i]); 958 CU_ASSERT(rc == 0); 959 } 960 961 /* Assert that first_io is still at the head of the list. */ 962 CU_ASSERT(TAILQ_FIRST(&shared_resource->nomem_io) == first_io); 963 CU_ASSERT(bdev_io_tailq_cnt(&shared_resource->nomem_io) == (IO_ARRAY_SIZE - AVAIL)); 964 nomem_cnt = bdev_io_tailq_cnt(&shared_resource->nomem_io); 965 CU_ASSERT(shared_resource->nomem_threshold == (AVAIL - NOMEM_THRESHOLD_COUNT)); 966 967 /* 968 * Complete 1 I/O only. The key check here is bdev_io_tailq_cnt - this should not have 969 * changed since completing just 1 I/O should not trigger retrying the queued nomem_io 970 * list. 971 */ 972 stub_complete_io(g_bdev.io_target, 1); 973 CU_ASSERT(bdev_io_tailq_cnt(&shared_resource->nomem_io) == nomem_cnt); 974 975 /* 976 * Complete enough I/O to hit the nomem_theshold. This should trigger retrying nomem_io, 977 * and we should see I/O get resubmitted to the test bdev module. 978 */ 979 stub_complete_io(g_bdev.io_target, NOMEM_THRESHOLD_COUNT - 1); 980 CU_ASSERT(bdev_io_tailq_cnt(&shared_resource->nomem_io) < nomem_cnt); 981 nomem_cnt = bdev_io_tailq_cnt(&shared_resource->nomem_io); 982 983 /* Complete 1 I/O only. This should not trigger retrying the queued nomem_io. */ 984 stub_complete_io(g_bdev.io_target, 1); 985 CU_ASSERT(bdev_io_tailq_cnt(&shared_resource->nomem_io) == nomem_cnt); 986 987 /* 988 * Send a reset and confirm that all I/O are completed, including the ones that 989 * were queued on the nomem_io list. 990 */ 991 status_reset = SPDK_BDEV_IO_STATUS_PENDING; 992 rc = spdk_bdev_reset(g_desc, io_ch, enomem_done, &status_reset); 993 poll_threads(); 994 CU_ASSERT(rc == 0); 995 /* This will complete the reset. */ 996 stub_complete_io(g_bdev.io_target, 0); 997 998 CU_ASSERT(bdev_io_tailq_cnt(&shared_resource->nomem_io) == 0); 999 CU_ASSERT(shared_resource->io_outstanding == 0); 1000 1001 spdk_put_io_channel(io_ch); 1002 poll_threads(); 1003 teardown_test(); 1004 } 1005 1006 static void 1007 enomem_multi_bdev(void) 1008 { 1009 struct spdk_io_channel *io_ch; 1010 struct spdk_bdev_channel *bdev_ch; 1011 struct spdk_bdev_shared_resource *shared_resource; 1012 struct ut_bdev_channel *ut_ch; 1013 const uint32_t IO_ARRAY_SIZE = 64; 1014 const uint32_t AVAIL = 20; 1015 enum spdk_bdev_io_status status[IO_ARRAY_SIZE]; 1016 uint32_t i; 1017 struct ut_bdev *second_bdev; 1018 struct spdk_bdev_desc *second_desc = NULL; 1019 struct spdk_bdev_channel *second_bdev_ch; 1020 struct spdk_io_channel *second_ch; 1021 int rc; 1022 1023 setup_test(); 1024 1025 /* Register second bdev with the same io_target */ 1026 second_bdev = calloc(1, sizeof(*second_bdev)); 1027 SPDK_CU_ASSERT_FATAL(second_bdev != NULL); 1028 register_bdev(second_bdev, "ut_bdev2", g_bdev.io_target); 1029 spdk_bdev_open(&second_bdev->bdev, true, NULL, NULL, &second_desc); 1030 SPDK_CU_ASSERT_FATAL(second_desc != NULL); 1031 1032 set_thread(0); 1033 io_ch = spdk_bdev_get_io_channel(g_desc); 1034 bdev_ch = spdk_io_channel_get_ctx(io_ch); 1035 shared_resource = bdev_ch->shared_resource; 1036 ut_ch = spdk_io_channel_get_ctx(bdev_ch->channel); 1037 ut_ch->avail_cnt = AVAIL; 1038 1039 second_ch = spdk_bdev_get_io_channel(second_desc); 1040 second_bdev_ch = spdk_io_channel_get_ctx(second_ch); 1041 SPDK_CU_ASSERT_FATAL(shared_resource == second_bdev_ch->shared_resource); 1042 1043 /* Saturate io_target through bdev A. */ 1044 for (i = 0; i < AVAIL; i++) { 1045 status[i] = SPDK_BDEV_IO_STATUS_PENDING; 1046 rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[i]); 1047 CU_ASSERT(rc == 0); 1048 } 1049 CU_ASSERT(TAILQ_EMPTY(&shared_resource->nomem_io)); 1050 1051 /* 1052 * Now submit I/O through the second bdev. This should fail with ENOMEM 1053 * and then go onto the nomem_io list. 1054 */ 1055 status[AVAIL] = SPDK_BDEV_IO_STATUS_PENDING; 1056 rc = spdk_bdev_read_blocks(second_desc, second_ch, NULL, 0, 1, enomem_done, &status[AVAIL]); 1057 CU_ASSERT(rc == 0); 1058 SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&shared_resource->nomem_io)); 1059 1060 /* Complete first bdev's I/O. This should retry sending second bdev's nomem_io */ 1061 stub_complete_io(g_bdev.io_target, AVAIL); 1062 1063 SPDK_CU_ASSERT_FATAL(TAILQ_EMPTY(&shared_resource->nomem_io)); 1064 CU_ASSERT(shared_resource->io_outstanding == 1); 1065 1066 /* Now complete our retried I/O */ 1067 stub_complete_io(g_bdev.io_target, 1); 1068 SPDK_CU_ASSERT_FATAL(shared_resource->io_outstanding == 0); 1069 1070 spdk_put_io_channel(io_ch); 1071 spdk_put_io_channel(second_ch); 1072 spdk_bdev_close(second_desc); 1073 unregister_bdev(second_bdev); 1074 poll_threads(); 1075 free(second_bdev); 1076 teardown_test(); 1077 } 1078 1079 1080 static void 1081 enomem_multi_io_target(void) 1082 { 1083 struct spdk_io_channel *io_ch; 1084 struct spdk_bdev_channel *bdev_ch; 1085 struct ut_bdev_channel *ut_ch; 1086 const uint32_t IO_ARRAY_SIZE = 64; 1087 const uint32_t AVAIL = 20; 1088 enum spdk_bdev_io_status status[IO_ARRAY_SIZE]; 1089 uint32_t i; 1090 int new_io_device; 1091 struct ut_bdev *second_bdev; 1092 struct spdk_bdev_desc *second_desc = NULL; 1093 struct spdk_bdev_channel *second_bdev_ch; 1094 struct spdk_io_channel *second_ch; 1095 int rc; 1096 1097 setup_test(); 1098 1099 /* Create new io_target and a second bdev using it */ 1100 spdk_io_device_register(&new_io_device, stub_create_ch, stub_destroy_ch, 1101 sizeof(struct ut_bdev_channel), NULL); 1102 second_bdev = calloc(1, sizeof(*second_bdev)); 1103 SPDK_CU_ASSERT_FATAL(second_bdev != NULL); 1104 register_bdev(second_bdev, "ut_bdev2", &new_io_device); 1105 spdk_bdev_open(&second_bdev->bdev, true, NULL, NULL, &second_desc); 1106 SPDK_CU_ASSERT_FATAL(second_desc != NULL); 1107 1108 set_thread(0); 1109 io_ch = spdk_bdev_get_io_channel(g_desc); 1110 bdev_ch = spdk_io_channel_get_ctx(io_ch); 1111 ut_ch = spdk_io_channel_get_ctx(bdev_ch->channel); 1112 ut_ch->avail_cnt = AVAIL; 1113 1114 /* Different io_target should imply a different shared_resource */ 1115 second_ch = spdk_bdev_get_io_channel(second_desc); 1116 second_bdev_ch = spdk_io_channel_get_ctx(second_ch); 1117 SPDK_CU_ASSERT_FATAL(bdev_ch->shared_resource != second_bdev_ch->shared_resource); 1118 1119 /* Saturate io_target through bdev A. */ 1120 for (i = 0; i < AVAIL; i++) { 1121 status[i] = SPDK_BDEV_IO_STATUS_PENDING; 1122 rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[i]); 1123 CU_ASSERT(rc == 0); 1124 } 1125 CU_ASSERT(TAILQ_EMPTY(&bdev_ch->shared_resource->nomem_io)); 1126 1127 /* Issue one more I/O to fill ENOMEM list. */ 1128 status[AVAIL] = SPDK_BDEV_IO_STATUS_PENDING; 1129 rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[AVAIL]); 1130 CU_ASSERT(rc == 0); 1131 SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&bdev_ch->shared_resource->nomem_io)); 1132 1133 /* 1134 * Now submit I/O through the second bdev. This should go through and complete 1135 * successfully because we're using a different io_device underneath. 1136 */ 1137 status[AVAIL] = SPDK_BDEV_IO_STATUS_PENDING; 1138 rc = spdk_bdev_read_blocks(second_desc, second_ch, NULL, 0, 1, enomem_done, &status[AVAIL]); 1139 CU_ASSERT(rc == 0); 1140 SPDK_CU_ASSERT_FATAL(TAILQ_EMPTY(&second_bdev_ch->shared_resource->nomem_io)); 1141 stub_complete_io(second_bdev->io_target, 1); 1142 1143 /* Cleanup; Complete outstanding I/O. */ 1144 stub_complete_io(g_bdev.io_target, AVAIL); 1145 SPDK_CU_ASSERT_FATAL(TAILQ_EMPTY(&bdev_ch->shared_resource->nomem_io)); 1146 /* Complete the ENOMEM I/O */ 1147 stub_complete_io(g_bdev.io_target, 1); 1148 CU_ASSERT(bdev_ch->shared_resource->io_outstanding == 0); 1149 1150 SPDK_CU_ASSERT_FATAL(TAILQ_EMPTY(&bdev_ch->shared_resource->nomem_io)); 1151 CU_ASSERT(bdev_ch->shared_resource->io_outstanding == 0); 1152 spdk_put_io_channel(io_ch); 1153 spdk_put_io_channel(second_ch); 1154 spdk_bdev_close(second_desc); 1155 unregister_bdev(second_bdev); 1156 spdk_io_device_unregister(&new_io_device, NULL); 1157 poll_threads(); 1158 free(second_bdev); 1159 teardown_test(); 1160 } 1161 1162 static void 1163 qos_dynamic_enable_done(void *cb_arg, int status) 1164 { 1165 int *rc = cb_arg; 1166 *rc = status; 1167 } 1168 1169 static void 1170 qos_dynamic_enable(void) 1171 { 1172 struct spdk_io_channel *io_ch[2]; 1173 struct spdk_bdev_channel *bdev_ch[2]; 1174 struct spdk_bdev *bdev; 1175 enum spdk_bdev_io_status bdev_io_status[2]; 1176 uint64_t limits[SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES] = {}; 1177 int status, second_status, rc, i; 1178 1179 setup_test(); 1180 MOCK_SET(spdk_get_ticks, 0); 1181 1182 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 1183 limits[i] = UINT64_MAX; 1184 } 1185 1186 bdev = &g_bdev.bdev; 1187 1188 g_get_io_channel = true; 1189 1190 /* Create channels */ 1191 set_thread(0); 1192 io_ch[0] = spdk_bdev_get_io_channel(g_desc); 1193 bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]); 1194 CU_ASSERT(bdev_ch[0]->flags == 0); 1195 1196 set_thread(1); 1197 io_ch[1] = spdk_bdev_get_io_channel(g_desc); 1198 bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]); 1199 CU_ASSERT(bdev_ch[1]->flags == 0); 1200 1201 set_thread(0); 1202 1203 /* 1204 * Enable QoS: Read/Write IOPS, Read/Write byte, 1205 * Read only byte and Write only byte per second 1206 * rate limits. 1207 * More than 10 I/Os allowed per timeslice. 1208 */ 1209 status = -1; 1210 limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT] = 10000; 1211 limits[SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT] = 100; 1212 limits[SPDK_BDEV_QOS_R_BPS_RATE_LIMIT] = 100; 1213 limits[SPDK_BDEV_QOS_W_BPS_RATE_LIMIT] = 10; 1214 spdk_bdev_set_qos_rate_limits(bdev, limits, qos_dynamic_enable_done, &status); 1215 poll_threads(); 1216 CU_ASSERT(status == 0); 1217 CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) != 0); 1218 CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) != 0); 1219 1220 /* 1221 * Submit and complete 10 I/O to fill the QoS allotment for this timeslice. 1222 * Additional I/O will then be queued. 1223 */ 1224 set_thread(0); 1225 for (i = 0; i < 10; i++) { 1226 bdev_io_status[0] = SPDK_BDEV_IO_STATUS_PENDING; 1227 rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &bdev_io_status[0]); 1228 CU_ASSERT(rc == 0); 1229 CU_ASSERT(bdev_io_status[0] == SPDK_BDEV_IO_STATUS_PENDING); 1230 poll_thread(0); 1231 stub_complete_io(g_bdev.io_target, 0); 1232 CU_ASSERT(bdev_io_status[0] == SPDK_BDEV_IO_STATUS_SUCCESS); 1233 } 1234 1235 /* 1236 * Send two more I/O. These I/O will be queued since the current timeslice allotment has been 1237 * filled already. We want to test that when QoS is disabled that these two I/O: 1238 * 1) are not aborted 1239 * 2) are sent back to their original thread for resubmission 1240 */ 1241 bdev_io_status[0] = SPDK_BDEV_IO_STATUS_PENDING; 1242 rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &bdev_io_status[0]); 1243 CU_ASSERT(rc == 0); 1244 CU_ASSERT(bdev_io_status[0] == SPDK_BDEV_IO_STATUS_PENDING); 1245 set_thread(1); 1246 bdev_io_status[1] = SPDK_BDEV_IO_STATUS_PENDING; 1247 rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &bdev_io_status[1]); 1248 CU_ASSERT(rc == 0); 1249 CU_ASSERT(bdev_io_status[1] == SPDK_BDEV_IO_STATUS_PENDING); 1250 poll_threads(); 1251 1252 /* 1253 * Disable QoS: Read/Write IOPS, Read/Write byte, 1254 * Read only byte rate limits 1255 */ 1256 status = -1; 1257 limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT] = 0; 1258 limits[SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT] = 0; 1259 limits[SPDK_BDEV_QOS_R_BPS_RATE_LIMIT] = 0; 1260 spdk_bdev_set_qos_rate_limits(bdev, limits, qos_dynamic_enable_done, &status); 1261 poll_threads(); 1262 CU_ASSERT(status == 0); 1263 CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) != 0); 1264 CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) != 0); 1265 1266 /* Disable QoS: Write only Byte per second rate limit */ 1267 status = -1; 1268 limits[SPDK_BDEV_QOS_W_BPS_RATE_LIMIT] = 0; 1269 spdk_bdev_set_qos_rate_limits(bdev, limits, qos_dynamic_enable_done, &status); 1270 poll_threads(); 1271 CU_ASSERT(status == 0); 1272 CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) == 0); 1273 CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) == 0); 1274 1275 /* 1276 * All I/O should have been resubmitted back on their original thread. Complete 1277 * all I/O on thread 0, and ensure that only the thread 0 I/O was completed. 1278 */ 1279 set_thread(0); 1280 stub_complete_io(g_bdev.io_target, 0); 1281 poll_threads(); 1282 CU_ASSERT(bdev_io_status[0] == SPDK_BDEV_IO_STATUS_SUCCESS); 1283 CU_ASSERT(bdev_io_status[1] == SPDK_BDEV_IO_STATUS_PENDING); 1284 1285 /* Now complete all I/O on thread 1 and ensure the thread 1 I/O was completed. */ 1286 set_thread(1); 1287 stub_complete_io(g_bdev.io_target, 0); 1288 poll_threads(); 1289 CU_ASSERT(bdev_io_status[1] == SPDK_BDEV_IO_STATUS_SUCCESS); 1290 1291 /* Disable QoS again */ 1292 status = -1; 1293 limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT] = 0; 1294 spdk_bdev_set_qos_rate_limits(bdev, limits, qos_dynamic_enable_done, &status); 1295 poll_threads(); 1296 CU_ASSERT(status == 0); /* This should succeed */ 1297 CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) == 0); 1298 CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) == 0); 1299 1300 /* Enable QoS on thread 0 */ 1301 status = -1; 1302 limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT] = 10000; 1303 spdk_bdev_set_qos_rate_limits(bdev, limits, qos_dynamic_enable_done, &status); 1304 poll_threads(); 1305 CU_ASSERT(status == 0); 1306 CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) != 0); 1307 CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) != 0); 1308 1309 /* Disable QoS on thread 1 */ 1310 set_thread(1); 1311 status = -1; 1312 limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT] = 0; 1313 spdk_bdev_set_qos_rate_limits(bdev, limits, qos_dynamic_enable_done, &status); 1314 /* Don't poll yet. This should leave the channels with QoS enabled */ 1315 CU_ASSERT(status == -1); 1316 CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) != 0); 1317 CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) != 0); 1318 1319 /* Enable QoS. This should immediately fail because the previous disable QoS hasn't completed. */ 1320 second_status = 0; 1321 limits[SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT] = 10; 1322 spdk_bdev_set_qos_rate_limits(bdev, limits, qos_dynamic_enable_done, &second_status); 1323 poll_threads(); 1324 CU_ASSERT(status == 0); /* The disable should succeed */ 1325 CU_ASSERT(second_status < 0); /* The enable should fail */ 1326 CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) == 0); 1327 CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) == 0); 1328 1329 /* Enable QoS on thread 1. This should succeed now that the disable has completed. */ 1330 status = -1; 1331 limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT] = 10000; 1332 spdk_bdev_set_qos_rate_limits(bdev, limits, qos_dynamic_enable_done, &status); 1333 poll_threads(); 1334 CU_ASSERT(status == 0); 1335 CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) != 0); 1336 CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) != 0); 1337 1338 /* Tear down the channels */ 1339 set_thread(0); 1340 spdk_put_io_channel(io_ch[0]); 1341 set_thread(1); 1342 spdk_put_io_channel(io_ch[1]); 1343 poll_threads(); 1344 1345 set_thread(0); 1346 teardown_test(); 1347 } 1348 1349 static void 1350 histogram_status_cb(void *cb_arg, int status) 1351 { 1352 g_status = status; 1353 } 1354 1355 static void 1356 histogram_data_cb(void *cb_arg, int status, struct spdk_histogram_data *histogram) 1357 { 1358 g_status = status; 1359 g_histogram = histogram; 1360 } 1361 1362 static void 1363 histogram_io_count(void *ctx, uint64_t start, uint64_t end, uint64_t count, 1364 uint64_t total, uint64_t so_far) 1365 { 1366 g_count += count; 1367 } 1368 1369 static void 1370 bdev_histograms_mt(void) 1371 { 1372 struct spdk_io_channel *ch[2]; 1373 struct spdk_histogram_data *histogram; 1374 uint8_t buf[4096]; 1375 int status = false; 1376 int rc; 1377 1378 1379 setup_test(); 1380 1381 set_thread(0); 1382 ch[0] = spdk_bdev_get_io_channel(g_desc); 1383 CU_ASSERT(ch[0] != NULL); 1384 1385 set_thread(1); 1386 ch[1] = spdk_bdev_get_io_channel(g_desc); 1387 CU_ASSERT(ch[1] != NULL); 1388 1389 1390 /* Enable histogram */ 1391 spdk_bdev_histogram_enable(&g_bdev.bdev, histogram_status_cb, NULL, true); 1392 poll_threads(); 1393 CU_ASSERT(g_status == 0); 1394 CU_ASSERT(g_bdev.bdev.internal.histogram_enabled == true); 1395 1396 /* Allocate histogram */ 1397 histogram = spdk_histogram_data_alloc(); 1398 1399 /* Check if histogram is zeroed */ 1400 spdk_bdev_histogram_get(&g_bdev.bdev, histogram, histogram_data_cb, NULL); 1401 poll_threads(); 1402 CU_ASSERT(g_status == 0); 1403 SPDK_CU_ASSERT_FATAL(g_histogram != NULL); 1404 1405 g_count = 0; 1406 spdk_histogram_data_iterate(g_histogram, histogram_io_count, NULL); 1407 1408 CU_ASSERT(g_count == 0); 1409 1410 set_thread(0); 1411 rc = spdk_bdev_write_blocks(g_desc, ch[0], &buf, 0, 1, io_during_io_done, &status); 1412 CU_ASSERT(rc == 0); 1413 1414 spdk_delay_us(10); 1415 stub_complete_io(g_bdev.io_target, 1); 1416 poll_threads(); 1417 CU_ASSERT(status == true); 1418 1419 1420 set_thread(1); 1421 rc = spdk_bdev_read_blocks(g_desc, ch[1], &buf, 0, 1, io_during_io_done, &status); 1422 CU_ASSERT(rc == 0); 1423 1424 spdk_delay_us(10); 1425 stub_complete_io(g_bdev.io_target, 1); 1426 poll_threads(); 1427 CU_ASSERT(status == true); 1428 1429 set_thread(0); 1430 1431 /* Check if histogram gathered data from all I/O channels */ 1432 spdk_bdev_histogram_get(&g_bdev.bdev, histogram, histogram_data_cb, NULL); 1433 poll_threads(); 1434 CU_ASSERT(g_status == 0); 1435 CU_ASSERT(g_bdev.bdev.internal.histogram_enabled == true); 1436 SPDK_CU_ASSERT_FATAL(g_histogram != NULL); 1437 1438 g_count = 0; 1439 spdk_histogram_data_iterate(g_histogram, histogram_io_count, NULL); 1440 CU_ASSERT(g_count == 2); 1441 1442 /* Disable histogram */ 1443 spdk_bdev_histogram_enable(&g_bdev.bdev, histogram_status_cb, NULL, false); 1444 poll_threads(); 1445 CU_ASSERT(g_status == 0); 1446 CU_ASSERT(g_bdev.bdev.internal.histogram_enabled == false); 1447 1448 spdk_histogram_data_free(g_histogram); 1449 } 1450 1451 int 1452 main(int argc, char **argv) 1453 { 1454 CU_pSuite suite = NULL; 1455 unsigned int num_failures; 1456 1457 if (CU_initialize_registry() != CUE_SUCCESS) { 1458 return CU_get_error(); 1459 } 1460 1461 suite = CU_add_suite("bdev", NULL, NULL); 1462 if (suite == NULL) { 1463 CU_cleanup_registry(); 1464 return CU_get_error(); 1465 } 1466 1467 if ( 1468 CU_add_test(suite, "basic", basic) == NULL || 1469 CU_add_test(suite, "unregister_and_close", unregister_and_close) == NULL || 1470 CU_add_test(suite, "basic_qos", basic_qos) == NULL || 1471 CU_add_test(suite, "put_channel_during_reset", put_channel_during_reset) == NULL || 1472 CU_add_test(suite, "aborted_reset", aborted_reset) == NULL || 1473 CU_add_test(suite, "io_during_reset", io_during_reset) == NULL || 1474 CU_add_test(suite, "io_during_qos_queue", io_during_qos_queue) == NULL || 1475 CU_add_test(suite, "io_during_qos_reset", io_during_qos_reset) == NULL || 1476 CU_add_test(suite, "enomem", enomem) == NULL || 1477 CU_add_test(suite, "enomem_multi_bdev", enomem_multi_bdev) == NULL || 1478 CU_add_test(suite, "enomem_multi_io_target", enomem_multi_io_target) == NULL || 1479 CU_add_test(suite, "qos_dynamic_enable", qos_dynamic_enable) == NULL || 1480 CU_add_test(suite, "bdev_histograms_mt", bdev_histograms_mt) == NULL 1481 ) { 1482 CU_cleanup_registry(); 1483 return CU_get_error(); 1484 } 1485 1486 CU_basic_set_mode(CU_BRM_VERBOSE); 1487 CU_basic_run_tests(); 1488 num_failures = CU_get_number_of_failures(); 1489 CU_cleanup_registry(); 1490 return num_failures; 1491 } 1492