xref: /spdk/test/unit/lib/bdev/mt/bdev.c/bdev_ut.c (revision be2d2c76cf7208202bfb2c4f109451f6b00c4ec2)
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/test_env.c"
37 #include "common/lib/ut_multithread.c"
38 #include "unit/lib/json_mock.c"
39 
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_V(spdk_scsi_nvme_translate, (const struct spdk_bdev_io *bdev_io,
48 		int *sc, int *sk, int *asc, int *ascq));
49 
50 DEFINE_STUB(spdk_conf_find_section, struct spdk_conf_section *, (struct spdk_conf *cp,
51 		const char *name), NULL);
52 DEFINE_STUB(spdk_conf_section_get_nmval, char *,
53 	    (struct spdk_conf_section *sp, const char *key, int idx1, int idx2), NULL);
54 DEFINE_STUB(spdk_conf_section_get_intval, int, (struct spdk_conf_section *sp, const char *key), -1);
55 
56 struct spdk_trace_histories *g_trace_histories;
57 DEFINE_STUB_V(spdk_trace_add_register_fn, (struct spdk_trace_register_fn *reg_fn));
58 DEFINE_STUB_V(spdk_trace_register_owner, (uint8_t type, char id_prefix));
59 DEFINE_STUB_V(spdk_trace_register_object, (uint8_t type, char id_prefix));
60 DEFINE_STUB_V(spdk_trace_register_description, (const char *name, const char *short_name,
61 		uint16_t tpoint_id, uint8_t owner_type,
62 		uint8_t object_type, uint8_t new_object,
63 		uint8_t arg1_is_ptr, const char *arg1_name));
64 DEFINE_STUB_V(_spdk_trace_record, (uint64_t tsc, uint16_t tpoint_id, uint16_t poller_id,
65 				   uint32_t size, uint64_t object_id, uint64_t arg1));
66 
67 struct ut_bdev {
68 	struct spdk_bdev	bdev;
69 	void			*io_target;
70 };
71 
72 struct ut_bdev_channel {
73 	TAILQ_HEAD(, spdk_bdev_io)	outstanding_io;
74 	uint32_t			outstanding_cnt;
75 	uint32_t			avail_cnt;
76 };
77 
78 int g_io_device;
79 struct ut_bdev g_bdev;
80 struct spdk_bdev_desc *g_desc;
81 bool g_teardown_done = false;
82 bool g_get_io_channel = true;
83 bool g_create_ch = true;
84 bool g_init_complete_called = false;
85 bool g_fini_start_called = true;
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 both IOPS and bandwidth rate limits.
621 	 * In this case, both rate limits will take equal effect.
622 	 */
623 	bdev->internal.qos->iops_rate_limit = 2000; /* 2 I/O per millisecond */
624 	bdev->internal.qos->byte_rate_limit = 8192000; /* 8K byte per millisecond with 4K block size */
625 
626 	g_get_io_channel = true;
627 
628 	set_thread(0);
629 	io_ch[0] = spdk_bdev_get_io_channel(g_desc);
630 	bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]);
631 	CU_ASSERT(bdev_ch[0]->flags == BDEV_CH_QOS_ENABLED);
632 
633 	set_thread(1);
634 	io_ch[1] = spdk_bdev_get_io_channel(g_desc);
635 	bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]);
636 	CU_ASSERT(bdev_ch[1]->flags == BDEV_CH_QOS_ENABLED);
637 
638 	/*
639 	 * Send an I/O on thread 0, which is where the QoS thread is running.
640 	 */
641 	set_thread(0);
642 	status = SPDK_BDEV_IO_STATUS_PENDING;
643 	rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status);
644 	CU_ASSERT(rc == 0);
645 	CU_ASSERT(status == SPDK_BDEV_IO_STATUS_PENDING);
646 	poll_threads();
647 	stub_complete_io(g_bdev.io_target, 0);
648 	poll_threads();
649 	CU_ASSERT(status == SPDK_BDEV_IO_STATUS_SUCCESS);
650 
651 	/* Send an I/O on thread 1. The QoS thread is not running here. */
652 	status = SPDK_BDEV_IO_STATUS_PENDING;
653 	set_thread(1);
654 	rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status);
655 	CU_ASSERT(rc == 0);
656 	CU_ASSERT(status == SPDK_BDEV_IO_STATUS_PENDING);
657 	poll_threads();
658 	/* Complete I/O on thread 1. This should not complete the I/O we submitted */
659 	stub_complete_io(g_bdev.io_target, 0);
660 	poll_threads();
661 	CU_ASSERT(status == SPDK_BDEV_IO_STATUS_PENDING);
662 	/* Now complete I/O on thread 0 */
663 	set_thread(0);
664 	poll_threads();
665 	stub_complete_io(g_bdev.io_target, 0);
666 	poll_threads();
667 	CU_ASSERT(status == SPDK_BDEV_IO_STATUS_SUCCESS);
668 
669 	/* Tear down the channels */
670 	set_thread(0);
671 	spdk_put_io_channel(io_ch[0]);
672 	set_thread(1);
673 	spdk_put_io_channel(io_ch[1]);
674 	poll_threads();
675 	set_thread(0);
676 
677 	/* Close the descriptor, which should stop the qos channel */
678 	spdk_bdev_close(g_desc);
679 	poll_threads();
680 	CU_ASSERT(bdev->internal.qos->ch == NULL);
681 
682 	spdk_bdev_open(bdev, true, NULL, NULL, &g_desc);
683 
684 	/* Create the channels in reverse order. */
685 	set_thread(1);
686 	io_ch[1] = spdk_bdev_get_io_channel(g_desc);
687 	bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]);
688 	CU_ASSERT(bdev_ch[1]->flags == BDEV_CH_QOS_ENABLED);
689 
690 	set_thread(0);
691 	io_ch[0] = spdk_bdev_get_io_channel(g_desc);
692 	bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]);
693 	CU_ASSERT(bdev_ch[0]->flags == BDEV_CH_QOS_ENABLED);
694 
695 	/* Confirm that the qos thread is now thread 1 */
696 	CU_ASSERT(bdev->internal.qos->ch == bdev_ch[1]);
697 
698 	/* Tear down the channels */
699 	set_thread(0);
700 	spdk_put_io_channel(io_ch[0]);
701 	set_thread(1);
702 	spdk_put_io_channel(io_ch[1]);
703 	poll_threads();
704 
705 	set_thread(0);
706 
707 	teardown_test();
708 }
709 
710 static void
711 io_during_qos_queue(void)
712 {
713 	struct spdk_io_channel *io_ch[2];
714 	struct spdk_bdev_channel *bdev_ch[2];
715 	struct spdk_bdev *bdev;
716 	enum spdk_bdev_io_status status0, status1;
717 	int rc;
718 
719 	setup_test();
720 	reset_time();
721 
722 	/* Enable QoS */
723 	bdev = &g_bdev.bdev;
724 	bdev->internal.qos = calloc(1, sizeof(*bdev->internal.qos));
725 	SPDK_CU_ASSERT_FATAL(bdev->internal.qos != NULL);
726 	TAILQ_INIT(&bdev->internal.qos->queued);
727 	/*
728 	 * Enable both IOPS and bandwidth rate limits.
729 	 * In this case, IOPS rate limit will take effect first.
730 	 */
731 	bdev->internal.qos->iops_rate_limit = 1000; /* 1000 I/O per second, or 1 per millisecond */
732 	bdev->internal.qos->byte_rate_limit = 8192000; /* 8K byte per millisecond with 4K block size */
733 
734 	g_get_io_channel = true;
735 
736 	/* Create channels */
737 	set_thread(0);
738 	io_ch[0] = spdk_bdev_get_io_channel(g_desc);
739 	bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]);
740 	CU_ASSERT(bdev_ch[0]->flags == BDEV_CH_QOS_ENABLED);
741 
742 	set_thread(1);
743 	io_ch[1] = spdk_bdev_get_io_channel(g_desc);
744 	bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]);
745 	CU_ASSERT(bdev_ch[1]->flags == BDEV_CH_QOS_ENABLED);
746 
747 	/* Send two I/O */
748 	status1 = SPDK_BDEV_IO_STATUS_PENDING;
749 	rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1);
750 	CU_ASSERT(rc == 0);
751 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING);
752 	set_thread(0);
753 	status0 = SPDK_BDEV_IO_STATUS_PENDING;
754 	rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0);
755 	CU_ASSERT(rc == 0);
756 	CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_PENDING);
757 
758 	/* Complete any I/O that arrived at the disk */
759 	poll_threads();
760 	set_thread(1);
761 	stub_complete_io(g_bdev.io_target, 0);
762 	set_thread(0);
763 	stub_complete_io(g_bdev.io_target, 0);
764 	poll_threads();
765 
766 	/* Only one of the I/O should complete. (logical XOR) */
767 	if (status0 == SPDK_BDEV_IO_STATUS_SUCCESS) {
768 		CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING);
769 	} else {
770 		CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS);
771 	}
772 
773 	/* Advance in time by a millisecond */
774 	increment_time(1000);
775 
776 	/* Complete more I/O */
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 	/* Now the second I/O should be done */
785 	CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_SUCCESS);
786 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS);
787 
788 	/* Tear down the channels */
789 	set_thread(1);
790 	spdk_put_io_channel(io_ch[1]);
791 	set_thread(0);
792 	spdk_put_io_channel(io_ch[0]);
793 	poll_threads();
794 
795 	teardown_test();
796 }
797 
798 static void
799 io_during_qos_reset(void)
800 {
801 	struct spdk_io_channel *io_ch[2];
802 	struct spdk_bdev_channel *bdev_ch[2];
803 	struct spdk_bdev *bdev;
804 	enum spdk_bdev_io_status status0, status1, reset_status;
805 	int rc;
806 
807 	setup_test();
808 	reset_time();
809 
810 	/* Enable QoS */
811 	bdev = &g_bdev.bdev;
812 	bdev->internal.qos = calloc(1, sizeof(*bdev->internal.qos));
813 	SPDK_CU_ASSERT_FATAL(bdev->internal.qos != NULL);
814 	TAILQ_INIT(&bdev->internal.qos->queued);
815 	/*
816 	 * Enable both IOPS and bandwidth rate limits.
817 	 * In this case, bandwidth rate limit will take effect first.
818 	 */
819 	bdev->internal.qos->iops_rate_limit = 2000; /* 2000 I/O per second, or 2 per millisecond */
820 	bdev->internal.qos->byte_rate_limit = 4096000; /* 4K byte per millisecond with 4K block size */
821 
822 	g_get_io_channel = true;
823 
824 	/* Create channels */
825 	set_thread(0);
826 	io_ch[0] = spdk_bdev_get_io_channel(g_desc);
827 	bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]);
828 	CU_ASSERT(bdev_ch[0]->flags == BDEV_CH_QOS_ENABLED);
829 
830 	set_thread(1);
831 	io_ch[1] = spdk_bdev_get_io_channel(g_desc);
832 	bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]);
833 	CU_ASSERT(bdev_ch[1]->flags == BDEV_CH_QOS_ENABLED);
834 
835 	/* Send two I/O. One of these gets queued by QoS. The other is sitting at the disk. */
836 	status1 = SPDK_BDEV_IO_STATUS_PENDING;
837 	rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1);
838 	CU_ASSERT(rc == 0);
839 	set_thread(0);
840 	status0 = SPDK_BDEV_IO_STATUS_PENDING;
841 	rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0);
842 	CU_ASSERT(rc == 0);
843 
844 	poll_threads();
845 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING);
846 	CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_PENDING);
847 
848 	/* Reset the bdev. */
849 	reset_status = SPDK_BDEV_IO_STATUS_PENDING;
850 	rc = spdk_bdev_reset(g_desc, io_ch[0], io_during_io_done, &reset_status);
851 	CU_ASSERT(rc == 0);
852 
853 	/* Complete any I/O that arrived at the disk */
854 	poll_threads();
855 	set_thread(1);
856 	stub_complete_io(g_bdev.io_target, 0);
857 	set_thread(0);
858 	stub_complete_io(g_bdev.io_target, 0);
859 	poll_threads();
860 
861 	CU_ASSERT(reset_status == SPDK_BDEV_IO_STATUS_SUCCESS);
862 	CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_FAILED);
863 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_FAILED);
864 
865 	/* Tear down the channels */
866 	set_thread(1);
867 	spdk_put_io_channel(io_ch[1]);
868 	set_thread(0);
869 	spdk_put_io_channel(io_ch[0]);
870 	poll_threads();
871 
872 	teardown_test();
873 }
874 
875 static void
876 enomem_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
877 {
878 	enum spdk_bdev_io_status *status = cb_arg;
879 
880 	*status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED;
881 	spdk_bdev_free_io(bdev_io);
882 }
883 
884 static void
885 enomem(void)
886 {
887 	struct spdk_io_channel *io_ch;
888 	struct spdk_bdev_channel *bdev_ch;
889 	struct spdk_bdev_shared_resource *shared_resource;
890 	struct ut_bdev_channel *ut_ch;
891 	const uint32_t IO_ARRAY_SIZE = 64;
892 	const uint32_t AVAIL = 20;
893 	enum spdk_bdev_io_status status[IO_ARRAY_SIZE], status_reset;
894 	uint32_t nomem_cnt, i;
895 	struct spdk_bdev_io *first_io;
896 	int rc;
897 
898 	setup_test();
899 
900 	set_thread(0);
901 	io_ch = spdk_bdev_get_io_channel(g_desc);
902 	bdev_ch = spdk_io_channel_get_ctx(io_ch);
903 	shared_resource = bdev_ch->shared_resource;
904 	ut_ch = spdk_io_channel_get_ctx(bdev_ch->channel);
905 	ut_ch->avail_cnt = AVAIL;
906 
907 	/* First submit a number of IOs equal to what the channel can support. */
908 	for (i = 0; i < AVAIL; i++) {
909 		status[i] = SPDK_BDEV_IO_STATUS_PENDING;
910 		rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[i]);
911 		CU_ASSERT(rc == 0);
912 	}
913 	CU_ASSERT(TAILQ_EMPTY(&shared_resource->nomem_io));
914 
915 	/*
916 	 * Next, submit one additional I/O.  This one should fail with ENOMEM and then go onto
917 	 *  the enomem_io list.
918 	 */
919 	status[AVAIL] = SPDK_BDEV_IO_STATUS_PENDING;
920 	rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[AVAIL]);
921 	CU_ASSERT(rc == 0);
922 	SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&shared_resource->nomem_io));
923 	first_io = TAILQ_FIRST(&shared_resource->nomem_io);
924 
925 	/*
926 	 * Now submit a bunch more I/O.  These should all fail with ENOMEM and get queued behind
927 	 *  the first_io above.
928 	 */
929 	for (i = AVAIL + 1; i < IO_ARRAY_SIZE; i++) {
930 		status[i] = SPDK_BDEV_IO_STATUS_PENDING;
931 		rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[i]);
932 		CU_ASSERT(rc == 0);
933 	}
934 
935 	/* Assert that first_io is still at the head of the list. */
936 	CU_ASSERT(TAILQ_FIRST(&shared_resource->nomem_io) == first_io);
937 	CU_ASSERT(bdev_io_tailq_cnt(&shared_resource->nomem_io) == (IO_ARRAY_SIZE - AVAIL));
938 	nomem_cnt = bdev_io_tailq_cnt(&shared_resource->nomem_io);
939 	CU_ASSERT(shared_resource->nomem_threshold == (AVAIL - NOMEM_THRESHOLD_COUNT));
940 
941 	/*
942 	 * Complete 1 I/O only.  The key check here is bdev_io_tailq_cnt - this should not have
943 	 *  changed since completing just 1 I/O should not trigger retrying the queued nomem_io
944 	 *  list.
945 	 */
946 	stub_complete_io(g_bdev.io_target, 1);
947 	CU_ASSERT(bdev_io_tailq_cnt(&shared_resource->nomem_io) == nomem_cnt);
948 
949 	/*
950 	 * Complete enough I/O to hit the nomem_theshold.  This should trigger retrying nomem_io,
951 	 *  and we should see I/O get resubmitted to the test bdev module.
952 	 */
953 	stub_complete_io(g_bdev.io_target, NOMEM_THRESHOLD_COUNT - 1);
954 	CU_ASSERT(bdev_io_tailq_cnt(&shared_resource->nomem_io) < nomem_cnt);
955 	nomem_cnt = bdev_io_tailq_cnt(&shared_resource->nomem_io);
956 
957 	/* Complete 1 I/O only.  This should not trigger retrying the queued nomem_io. */
958 	stub_complete_io(g_bdev.io_target, 1);
959 	CU_ASSERT(bdev_io_tailq_cnt(&shared_resource->nomem_io) == nomem_cnt);
960 
961 	/*
962 	 * Send a reset and confirm that all I/O are completed, including the ones that
963 	 *  were queued on the nomem_io list.
964 	 */
965 	status_reset = SPDK_BDEV_IO_STATUS_PENDING;
966 	rc = spdk_bdev_reset(g_desc, io_ch, enomem_done, &status_reset);
967 	poll_threads();
968 	CU_ASSERT(rc == 0);
969 	/* This will complete the reset. */
970 	stub_complete_io(g_bdev.io_target, 0);
971 
972 	CU_ASSERT(bdev_io_tailq_cnt(&shared_resource->nomem_io) == 0);
973 	CU_ASSERT(shared_resource->io_outstanding == 0);
974 
975 	spdk_put_io_channel(io_ch);
976 	poll_threads();
977 	teardown_test();
978 }
979 
980 static void
981 enomem_multi_bdev(void)
982 {
983 	struct spdk_io_channel *io_ch;
984 	struct spdk_bdev_channel *bdev_ch;
985 	struct spdk_bdev_shared_resource *shared_resource;
986 	struct ut_bdev_channel *ut_ch;
987 	const uint32_t IO_ARRAY_SIZE = 64;
988 	const uint32_t AVAIL = 20;
989 	enum spdk_bdev_io_status status[IO_ARRAY_SIZE];
990 	uint32_t i;
991 	struct ut_bdev *second_bdev;
992 	struct spdk_bdev_desc *second_desc = NULL;
993 	struct spdk_bdev_channel *second_bdev_ch;
994 	struct spdk_io_channel *second_ch;
995 	int rc;
996 
997 	setup_test();
998 
999 	/* Register second bdev with the same io_target  */
1000 	second_bdev = calloc(1, sizeof(*second_bdev));
1001 	SPDK_CU_ASSERT_FATAL(second_bdev != NULL);
1002 	register_bdev(second_bdev, "ut_bdev2", g_bdev.io_target);
1003 	spdk_bdev_open(&second_bdev->bdev, true, NULL, NULL, &second_desc);
1004 	SPDK_CU_ASSERT_FATAL(second_desc != NULL);
1005 
1006 	set_thread(0);
1007 	io_ch = spdk_bdev_get_io_channel(g_desc);
1008 	bdev_ch = spdk_io_channel_get_ctx(io_ch);
1009 	shared_resource = bdev_ch->shared_resource;
1010 	ut_ch = spdk_io_channel_get_ctx(bdev_ch->channel);
1011 	ut_ch->avail_cnt = AVAIL;
1012 
1013 	second_ch = spdk_bdev_get_io_channel(second_desc);
1014 	second_bdev_ch = spdk_io_channel_get_ctx(second_ch);
1015 	SPDK_CU_ASSERT_FATAL(shared_resource == second_bdev_ch->shared_resource);
1016 
1017 	/* Saturate io_target through bdev A. */
1018 	for (i = 0; i < AVAIL; i++) {
1019 		status[i] = SPDK_BDEV_IO_STATUS_PENDING;
1020 		rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[i]);
1021 		CU_ASSERT(rc == 0);
1022 	}
1023 	CU_ASSERT(TAILQ_EMPTY(&shared_resource->nomem_io));
1024 
1025 	/*
1026 	 * Now submit I/O through the second bdev. This should fail with ENOMEM
1027 	 * and then go onto the nomem_io list.
1028 	 */
1029 	status[AVAIL] = SPDK_BDEV_IO_STATUS_PENDING;
1030 	rc = spdk_bdev_read_blocks(second_desc, second_ch, NULL, 0, 1, enomem_done, &status[AVAIL]);
1031 	CU_ASSERT(rc == 0);
1032 	SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&shared_resource->nomem_io));
1033 
1034 	/* Complete first bdev's I/O. This should retry sending second bdev's nomem_io */
1035 	stub_complete_io(g_bdev.io_target, AVAIL);
1036 
1037 	SPDK_CU_ASSERT_FATAL(TAILQ_EMPTY(&shared_resource->nomem_io));
1038 	CU_ASSERT(shared_resource->io_outstanding == 1);
1039 
1040 	/* Now complete our retried I/O  */
1041 	stub_complete_io(g_bdev.io_target, 1);
1042 	SPDK_CU_ASSERT_FATAL(shared_resource->io_outstanding == 0);
1043 
1044 	spdk_put_io_channel(io_ch);
1045 	spdk_put_io_channel(second_ch);
1046 	spdk_bdev_close(second_desc);
1047 	unregister_bdev(second_bdev);
1048 	poll_threads();
1049 	free(second_bdev);
1050 	teardown_test();
1051 }
1052 
1053 
1054 static void
1055 enomem_multi_io_target(void)
1056 {
1057 	struct spdk_io_channel *io_ch;
1058 	struct spdk_bdev_channel *bdev_ch;
1059 	struct ut_bdev_channel *ut_ch;
1060 	const uint32_t IO_ARRAY_SIZE = 64;
1061 	const uint32_t AVAIL = 20;
1062 	enum spdk_bdev_io_status status[IO_ARRAY_SIZE];
1063 	uint32_t i;
1064 	int new_io_device;
1065 	struct ut_bdev *second_bdev;
1066 	struct spdk_bdev_desc *second_desc = NULL;
1067 	struct spdk_bdev_channel *second_bdev_ch;
1068 	struct spdk_io_channel *second_ch;
1069 	int rc;
1070 
1071 	setup_test();
1072 
1073 	/* Create new io_target and a second bdev using it */
1074 	spdk_io_device_register(&new_io_device, stub_create_ch, stub_destroy_ch,
1075 				sizeof(struct ut_bdev_channel), NULL);
1076 	second_bdev = calloc(1, sizeof(*second_bdev));
1077 	SPDK_CU_ASSERT_FATAL(second_bdev != NULL);
1078 	register_bdev(second_bdev, "ut_bdev2", &new_io_device);
1079 	spdk_bdev_open(&second_bdev->bdev, true, NULL, NULL, &second_desc);
1080 	SPDK_CU_ASSERT_FATAL(second_desc != NULL);
1081 
1082 	set_thread(0);
1083 	io_ch = spdk_bdev_get_io_channel(g_desc);
1084 	bdev_ch = spdk_io_channel_get_ctx(io_ch);
1085 	ut_ch = spdk_io_channel_get_ctx(bdev_ch->channel);
1086 	ut_ch->avail_cnt = AVAIL;
1087 
1088 	/* Different io_target should imply a different shared_resource */
1089 	second_ch = spdk_bdev_get_io_channel(second_desc);
1090 	second_bdev_ch = spdk_io_channel_get_ctx(second_ch);
1091 	SPDK_CU_ASSERT_FATAL(bdev_ch->shared_resource != second_bdev_ch->shared_resource);
1092 
1093 	/* Saturate io_target through bdev A. */
1094 	for (i = 0; i < AVAIL; i++) {
1095 		status[i] = SPDK_BDEV_IO_STATUS_PENDING;
1096 		rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[i]);
1097 		CU_ASSERT(rc == 0);
1098 	}
1099 	CU_ASSERT(TAILQ_EMPTY(&bdev_ch->shared_resource->nomem_io));
1100 
1101 	/* Issue one more I/O to fill ENOMEM list. */
1102 	status[AVAIL] = SPDK_BDEV_IO_STATUS_PENDING;
1103 	rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[AVAIL]);
1104 	CU_ASSERT(rc == 0);
1105 	SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&bdev_ch->shared_resource->nomem_io));
1106 
1107 	/*
1108 	 * Now submit I/O through the second bdev. This should go through and complete
1109 	 * successfully because we're using a different io_device underneath.
1110 	 */
1111 	status[AVAIL] = SPDK_BDEV_IO_STATUS_PENDING;
1112 	rc = spdk_bdev_read_blocks(second_desc, second_ch, NULL, 0, 1, enomem_done, &status[AVAIL]);
1113 	CU_ASSERT(rc == 0);
1114 	SPDK_CU_ASSERT_FATAL(TAILQ_EMPTY(&second_bdev_ch->shared_resource->nomem_io));
1115 	stub_complete_io(second_bdev->io_target, 1);
1116 
1117 	/* Cleanup; Complete outstanding I/O. */
1118 	stub_complete_io(g_bdev.io_target, AVAIL);
1119 	SPDK_CU_ASSERT_FATAL(TAILQ_EMPTY(&bdev_ch->shared_resource->nomem_io));
1120 	/* Complete the ENOMEM I/O */
1121 	stub_complete_io(g_bdev.io_target, 1);
1122 	CU_ASSERT(bdev_ch->shared_resource->io_outstanding == 0);
1123 
1124 	SPDK_CU_ASSERT_FATAL(TAILQ_EMPTY(&bdev_ch->shared_resource->nomem_io));
1125 	CU_ASSERT(bdev_ch->shared_resource->io_outstanding == 0);
1126 	spdk_put_io_channel(io_ch);
1127 	spdk_put_io_channel(second_ch);
1128 	spdk_bdev_close(second_desc);
1129 	unregister_bdev(second_bdev);
1130 	spdk_io_device_unregister(&new_io_device, NULL);
1131 	poll_threads();
1132 	free(second_bdev);
1133 	teardown_test();
1134 }
1135 
1136 static void
1137 qos_dynamic_enable_done(void *cb_arg, int status)
1138 {
1139 	int *rc = cb_arg;
1140 	*rc = status;
1141 }
1142 
1143 static void
1144 qos_dynamic_enable(void)
1145 {
1146 	struct spdk_io_channel *io_ch[2];
1147 	struct spdk_bdev_channel *bdev_ch[2];
1148 	struct spdk_bdev *bdev;
1149 	enum spdk_bdev_io_status bdev_io_status[2];
1150 	int status, second_status, rc, i;
1151 
1152 	setup_test();
1153 	reset_time();
1154 
1155 	bdev = &g_bdev.bdev;
1156 
1157 	g_get_io_channel = true;
1158 
1159 	/* Create channels */
1160 	set_thread(0);
1161 	io_ch[0] = spdk_bdev_get_io_channel(g_desc);
1162 	bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]);
1163 	CU_ASSERT(bdev_ch[0]->flags == 0);
1164 
1165 	set_thread(1);
1166 	io_ch[1] = spdk_bdev_get_io_channel(g_desc);
1167 	bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]);
1168 	CU_ASSERT(bdev_ch[1]->flags == 0);
1169 
1170 	set_thread(0);
1171 
1172 	/* Enable QoS */
1173 	status = -1;
1174 	spdk_bdev_set_qos_limit_iops(bdev, 10000, qos_dynamic_enable_done, &status);
1175 	poll_threads();
1176 	CU_ASSERT(status == 0);
1177 	CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) != 0);
1178 	CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) != 0);
1179 
1180 	/*
1181 	 * Submit and complete 10 I/O to fill the QoS allotment for this timeslice.
1182 	 * Additional I/O will then be queued.
1183 	 */
1184 	set_thread(0);
1185 	for (i = 0; i < 10; i++) {
1186 		bdev_io_status[0] = SPDK_BDEV_IO_STATUS_PENDING;
1187 		rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &bdev_io_status[0]);
1188 		CU_ASSERT(rc == 0);
1189 		CU_ASSERT(bdev_io_status[0] == SPDK_BDEV_IO_STATUS_PENDING);
1190 		poll_thread(0);
1191 		stub_complete_io(g_bdev.io_target, 0);
1192 		CU_ASSERT(bdev_io_status[0] == SPDK_BDEV_IO_STATUS_SUCCESS);
1193 	}
1194 
1195 	/*
1196 	 * Send two more I/O.  These I/O will be queued since the current timeslice allotment has been
1197 	 * filled already.  We want to test that when QoS is disabled that these two I/O:
1198 	 *  1) are not aborted
1199 	 *  2) are sent back to their original thread for resubmission
1200 	 */
1201 	bdev_io_status[0] = SPDK_BDEV_IO_STATUS_PENDING;
1202 	rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &bdev_io_status[0]);
1203 	CU_ASSERT(rc == 0);
1204 	CU_ASSERT(bdev_io_status[0] == SPDK_BDEV_IO_STATUS_PENDING);
1205 	set_thread(1);
1206 	bdev_io_status[1] = SPDK_BDEV_IO_STATUS_PENDING;
1207 	rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &bdev_io_status[1]);
1208 	CU_ASSERT(rc == 0);
1209 	CU_ASSERT(bdev_io_status[1] == SPDK_BDEV_IO_STATUS_PENDING);
1210 	poll_threads();
1211 
1212 	/* Disable QoS */
1213 	status = -1;
1214 	spdk_bdev_set_qos_limit_iops(bdev, 0, 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 	 * All I/O should have been resubmitted back on their original thread.  Complete
1222 	 *  all I/O on thread 0, and ensure that only the thread 0 I/O was completed.
1223 	 */
1224 	set_thread(0);
1225 	stub_complete_io(g_bdev.io_target, 0);
1226 	poll_threads();
1227 	CU_ASSERT(bdev_io_status[0] == SPDK_BDEV_IO_STATUS_SUCCESS);
1228 	CU_ASSERT(bdev_io_status[1] == SPDK_BDEV_IO_STATUS_PENDING);
1229 
1230 	/* Now complete all I/O on thread 1 and ensure the thread 1 I/O was completed. */
1231 	set_thread(1);
1232 	stub_complete_io(g_bdev.io_target, 0);
1233 	poll_threads();
1234 	CU_ASSERT(bdev_io_status[1] == SPDK_BDEV_IO_STATUS_SUCCESS);
1235 
1236 	/* Disable QoS again */
1237 	status = -1;
1238 	spdk_bdev_set_qos_limit_iops(bdev, 0, qos_dynamic_enable_done, &status);
1239 	poll_threads();
1240 	CU_ASSERT(status == 0); /* This should succeed */
1241 	CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) == 0);
1242 	CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) == 0);
1243 
1244 	/* Enable QoS on thread 0 */
1245 	status = -1;
1246 	spdk_bdev_set_qos_limit_iops(bdev, 10000, qos_dynamic_enable_done, &status);
1247 	poll_threads();
1248 	CU_ASSERT(status == 0);
1249 	CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) != 0);
1250 	CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) != 0);
1251 
1252 	/* Disable QoS on thread 1 */
1253 	set_thread(1);
1254 	status = -1;
1255 	spdk_bdev_set_qos_limit_iops(bdev, 0, qos_dynamic_enable_done, &status);
1256 	/* Don't poll yet. This should leave the channels with QoS enabled */
1257 	CU_ASSERT(status == -1);
1258 	CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) != 0);
1259 	CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) != 0);
1260 
1261 	/* Enable QoS. This should immediately fail because the previous disable QoS hasn't completed. */
1262 	second_status = 0;
1263 	spdk_bdev_set_qos_limit_iops(bdev, 10000, qos_dynamic_enable_done, &second_status);
1264 	poll_threads();
1265 	CU_ASSERT(status == 0); /* The disable should succeed */
1266 	CU_ASSERT(second_status < 0); /* The enable should fail */
1267 	CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) == 0);
1268 	CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) == 0);
1269 
1270 	/* Enable QoS on thread 1. This should succeed now that the disable has completed. */
1271 	status = -1;
1272 	spdk_bdev_set_qos_limit_iops(bdev, 10000, qos_dynamic_enable_done, &status);
1273 	poll_threads();
1274 	CU_ASSERT(status == 0);
1275 	CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) != 0);
1276 	CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) != 0);
1277 
1278 	/* Tear down the channels */
1279 	set_thread(0);
1280 	spdk_put_io_channel(io_ch[0]);
1281 	set_thread(1);
1282 	spdk_put_io_channel(io_ch[1]);
1283 	poll_threads();
1284 
1285 	set_thread(0);
1286 	teardown_test();
1287 }
1288 
1289 int
1290 main(int argc, char **argv)
1291 {
1292 	CU_pSuite	suite = NULL;
1293 	unsigned int	num_failures;
1294 
1295 	if (CU_initialize_registry() != CUE_SUCCESS) {
1296 		return CU_get_error();
1297 	}
1298 
1299 	suite = CU_add_suite("bdev", NULL, NULL);
1300 	if (suite == NULL) {
1301 		CU_cleanup_registry();
1302 		return CU_get_error();
1303 	}
1304 
1305 	if (
1306 		CU_add_test(suite, "basic", basic) == NULL ||
1307 		CU_add_test(suite, "unregister_and_close", unregister_and_close) == NULL ||
1308 		CU_add_test(suite, "basic_qos", basic_qos) == NULL ||
1309 		CU_add_test(suite, "put_channel_during_reset", put_channel_during_reset) == NULL ||
1310 		CU_add_test(suite, "aborted_reset", aborted_reset) == NULL ||
1311 		CU_add_test(suite, "io_during_reset", io_during_reset) == NULL ||
1312 		CU_add_test(suite, "io_during_qos_queue", io_during_qos_queue) == NULL ||
1313 		CU_add_test(suite, "io_during_qos_reset", io_during_qos_reset) == NULL ||
1314 		CU_add_test(suite, "enomem", enomem) == NULL ||
1315 		CU_add_test(suite, "enomem_multi_bdev", enomem_multi_bdev) == NULL ||
1316 		CU_add_test(suite, "enomem_multi_io_target", enomem_multi_io_target) == NULL ||
1317 		CU_add_test(suite, "qos_dynamic_enable", qos_dynamic_enable) == NULL
1318 	) {
1319 		CU_cleanup_registry();
1320 		return CU_get_error();
1321 	}
1322 
1323 	CU_basic_set_mode(CU_BRM_VERBOSE);
1324 	CU_basic_run_tests();
1325 	num_failures = CU_get_number_of_failures();
1326 	CU_cleanup_registry();
1327 	return num_failures;
1328 }
1329