xref: /spdk/test/unit/lib/bdev/raid/bdev_raid.c/bdev_raid_ut.c (revision 69038a944dd521e7f620676a916faef0f35e4ed1)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2018 Intel Corporation.
3  *   All rights reserved.
4  *   Copyright (c) 2022-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5  */
6 
7 #include "spdk/stdinc.h"
8 #include "spdk_cunit.h"
9 #include "spdk/env.h"
10 #include "spdk_internal/mock.h"
11 #include "thread/thread_internal.h"
12 #include "bdev/raid/bdev_raid.c"
13 #include "bdev/raid/bdev_raid_rpc.c"
14 #include "bdev/raid/raid0.c"
15 #include "common/lib/ut_multithread.c"
16 
17 #define MAX_BASE_DRIVES 32
18 #define MAX_RAIDS 2
19 #define INVALID_IO_SUBMIT 0xFFFF
20 #define MAX_TEST_IO_RANGE (3 * 3 * 3 * (MAX_BASE_DRIVES + 5))
21 #define BLOCK_CNT (1024ul * 1024ul * 1024ul * 1024ul)
22 
23 struct spdk_bdev_channel {
24 	struct spdk_io_channel *channel;
25 };
26 
27 struct spdk_bdev_desc {
28 	struct spdk_bdev *bdev;
29 };
30 
31 /* Data structure to capture the output of IO for verification */
32 struct io_output {
33 	struct spdk_bdev_desc       *desc;
34 	struct spdk_io_channel      *ch;
35 	uint64_t                    offset_blocks;
36 	uint64_t                    num_blocks;
37 	spdk_bdev_io_completion_cb  cb;
38 	void                        *cb_arg;
39 	enum spdk_bdev_io_type      iotype;
40 };
41 
42 struct raid_io_ranges {
43 	uint64_t lba;
44 	uint64_t nblocks;
45 };
46 
47 /* Globals */
48 int g_bdev_io_submit_status;
49 struct io_output *g_io_output = NULL;
50 uint32_t g_io_output_index;
51 uint32_t g_io_comp_status;
52 bool g_child_io_status_flag;
53 void *g_rpc_req;
54 uint32_t g_rpc_req_size;
55 TAILQ_HEAD(bdev, spdk_bdev);
56 struct bdev g_bdev_list;
57 TAILQ_HEAD(waitq, spdk_bdev_io_wait_entry);
58 struct waitq g_io_waitq;
59 uint32_t g_block_len;
60 uint32_t g_strip_size;
61 uint32_t g_max_io_size;
62 uint8_t g_max_base_drives;
63 uint8_t g_max_raids;
64 uint8_t g_ignore_io_output;
65 uint8_t g_rpc_err;
66 char *g_get_raids_output[MAX_RAIDS];
67 uint32_t g_get_raids_count;
68 uint8_t g_json_decode_obj_err;
69 uint8_t g_json_decode_obj_create;
70 uint8_t g_config_level_create = 0;
71 uint8_t g_test_multi_raids;
72 struct raid_io_ranges g_io_ranges[MAX_TEST_IO_RANGE];
73 uint32_t g_io_range_idx;
74 uint64_t g_lba_offset;
75 struct spdk_io_channel g_io_channel;
76 
77 DEFINE_STUB_V(spdk_bdev_module_examine_done, (struct spdk_bdev_module *module));
78 DEFINE_STUB_V(spdk_bdev_module_list_add, (struct spdk_bdev_module *bdev_module));
79 DEFINE_STUB(spdk_bdev_register, int, (struct spdk_bdev *bdev), 0);
80 DEFINE_STUB(spdk_bdev_io_type_supported, bool, (struct spdk_bdev *bdev,
81 		enum spdk_bdev_io_type io_type), true);
82 DEFINE_STUB_V(spdk_bdev_close, (struct spdk_bdev_desc *desc));
83 DEFINE_STUB(spdk_bdev_flush_blocks, int, (struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
84 		uint64_t offset_blocks, uint64_t num_blocks, spdk_bdev_io_completion_cb cb,
85 		void *cb_arg), 0);
86 DEFINE_STUB(spdk_conf_next_section, struct spdk_conf_section *, (struct spdk_conf_section *sp),
87 	    NULL);
88 DEFINE_STUB_V(spdk_rpc_register_method, (const char *method, spdk_rpc_method_handler func,
89 		uint32_t state_mask));
90 DEFINE_STUB_V(spdk_rpc_register_alias_deprecated, (const char *method, const char *alias));
91 DEFINE_STUB_V(spdk_jsonrpc_end_result, (struct spdk_jsonrpc_request *request,
92 					struct spdk_json_write_ctx *w));
93 DEFINE_STUB_V(spdk_jsonrpc_send_bool_response, (struct spdk_jsonrpc_request *request,
94 		bool value));
95 DEFINE_STUB(spdk_json_decode_string, int, (const struct spdk_json_val *val, void *out), 0);
96 DEFINE_STUB(spdk_json_decode_uint32, int, (const struct spdk_json_val *val, void *out), 0);
97 DEFINE_STUB(spdk_json_decode_array, int, (const struct spdk_json_val *values,
98 		spdk_json_decode_fn decode_func,
99 		void *out, size_t max_size, size_t *out_size, size_t stride), 0);
100 DEFINE_STUB(spdk_json_write_name, int, (struct spdk_json_write_ctx *w, const char *name), 0);
101 DEFINE_STUB(spdk_json_write_object_begin, int, (struct spdk_json_write_ctx *w), 0);
102 DEFINE_STUB(spdk_json_write_named_object_begin, int, (struct spdk_json_write_ctx *w,
103 		const char *name), 0);
104 DEFINE_STUB(spdk_json_write_string, int, (struct spdk_json_write_ctx *w, const char *val), 0);
105 DEFINE_STUB(spdk_json_write_object_end, int, (struct spdk_json_write_ctx *w), 0);
106 DEFINE_STUB(spdk_json_write_array_begin, int, (struct spdk_json_write_ctx *w), 0);
107 DEFINE_STUB(spdk_json_write_array_end, int, (struct spdk_json_write_ctx *w), 0);
108 DEFINE_STUB(spdk_json_write_named_array_begin, int, (struct spdk_json_write_ctx *w,
109 		const char *name), 0);
110 DEFINE_STUB(spdk_json_write_bool, int, (struct spdk_json_write_ctx *w, bool val), 0);
111 DEFINE_STUB(spdk_json_write_null, int, (struct spdk_json_write_ctx *w), 0);
112 DEFINE_STUB(spdk_strerror, const char *, (int errnum), NULL);
113 DEFINE_STUB(spdk_bdev_queue_io_wait, int, (struct spdk_bdev *bdev, struct spdk_io_channel *ch,
114 		struct spdk_bdev_io_wait_entry *entry), 0);
115 DEFINE_STUB(spdk_bdev_get_memory_domains, int, (struct spdk_bdev *bdev,
116 		struct spdk_memory_domain **domains,	int array_size), 0);
117 DEFINE_STUB(spdk_bdev_get_name, const char *, (const struct spdk_bdev *bdev), "test_bdev");
118 
119 struct spdk_io_channel *
120 spdk_bdev_get_io_channel(struct spdk_bdev_desc *desc)
121 {
122 	g_io_channel.thread = spdk_get_thread();
123 
124 	return &g_io_channel;
125 }
126 
127 static void
128 set_test_opts(void)
129 {
130 
131 	g_max_base_drives = MAX_BASE_DRIVES;
132 	g_max_raids = MAX_RAIDS;
133 	g_block_len = 4096;
134 	g_strip_size = 64;
135 	g_max_io_size = 1024;
136 
137 	printf("Test Options\n");
138 	printf("blocklen = %u, strip_size = %u, max_io_size = %u, g_max_base_drives = %u, "
139 	       "g_max_raids = %u\n",
140 	       g_block_len, g_strip_size, g_max_io_size, g_max_base_drives, g_max_raids);
141 }
142 
143 /* Set globals before every test run */
144 static void
145 set_globals(void)
146 {
147 	uint32_t max_splits;
148 
149 	g_bdev_io_submit_status = 0;
150 	if (g_max_io_size < g_strip_size) {
151 		max_splits = 2;
152 	} else {
153 		max_splits = (g_max_io_size / g_strip_size) + 1;
154 	}
155 	if (max_splits < g_max_base_drives) {
156 		max_splits = g_max_base_drives;
157 	}
158 
159 	g_io_output = calloc(max_splits, sizeof(struct io_output));
160 	SPDK_CU_ASSERT_FATAL(g_io_output != NULL);
161 	g_io_output_index = 0;
162 	memset(g_get_raids_output, 0, sizeof(g_get_raids_output));
163 	g_get_raids_count = 0;
164 	g_io_comp_status = 0;
165 	g_ignore_io_output = 0;
166 	g_config_level_create = 0;
167 	g_rpc_err = 0;
168 	g_test_multi_raids = 0;
169 	g_child_io_status_flag = true;
170 	TAILQ_INIT(&g_bdev_list);
171 	TAILQ_INIT(&g_io_waitq);
172 	g_rpc_req = NULL;
173 	g_rpc_req_size = 0;
174 	g_json_decode_obj_err = 0;
175 	g_json_decode_obj_create = 0;
176 	g_lba_offset = 0;
177 }
178 
179 static void
180 base_bdevs_cleanup(void)
181 {
182 	struct spdk_bdev *bdev;
183 	struct spdk_bdev *bdev_next;
184 
185 	if (!TAILQ_EMPTY(&g_bdev_list)) {
186 		TAILQ_FOREACH_SAFE(bdev, &g_bdev_list, internal.link, bdev_next) {
187 			free(bdev->name);
188 			TAILQ_REMOVE(&g_bdev_list, bdev, internal.link);
189 			free(bdev);
190 		}
191 	}
192 }
193 
194 static void
195 check_and_remove_raid_bdev(struct raid_bdev *raid_bdev)
196 {
197 	struct raid_base_bdev_info *base_info;
198 
199 	assert(raid_bdev != NULL);
200 	assert(raid_bdev->base_bdev_info != NULL);
201 
202 	RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) {
203 		if (base_info->bdev) {
204 			raid_bdev_free_base_bdev_resource(raid_bdev, base_info);
205 		}
206 	}
207 	assert(raid_bdev->num_base_bdevs_discovered == 0);
208 	raid_bdev_cleanup_and_free(raid_bdev);
209 }
210 
211 /* Reset globals */
212 static void
213 reset_globals(void)
214 {
215 	if (g_io_output) {
216 		free(g_io_output);
217 		g_io_output = NULL;
218 	}
219 	g_rpc_req = NULL;
220 	g_rpc_req_size = 0;
221 }
222 
223 void
224 spdk_bdev_io_get_buf(struct spdk_bdev_io *bdev_io, spdk_bdev_io_get_buf_cb cb,
225 		     uint64_t len)
226 {
227 	cb(bdev_io->internal.ch->channel, bdev_io, true);
228 }
229 
230 /* Store the IO completion status in global variable to verify by various tests */
231 void
232 spdk_bdev_io_complete(struct spdk_bdev_io *bdev_io, enum spdk_bdev_io_status status)
233 {
234 	g_io_comp_status = ((status == SPDK_BDEV_IO_STATUS_SUCCESS) ? true : false);
235 }
236 
237 static void
238 set_io_output(struct io_output *output,
239 	      struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
240 	      uint64_t offset_blocks, uint64_t num_blocks,
241 	      spdk_bdev_io_completion_cb cb, void *cb_arg,
242 	      enum spdk_bdev_io_type iotype)
243 {
244 	output->desc = desc;
245 	output->ch = ch;
246 	output->offset_blocks = offset_blocks;
247 	output->num_blocks = num_blocks;
248 	output->cb = cb;
249 	output->cb_arg = cb_arg;
250 	output->iotype = iotype;
251 }
252 
253 /* It will cache the split IOs for verification */
254 int
255 spdk_bdev_writev_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
256 			struct iovec *iov, int iovcnt,
257 			uint64_t offset_blocks, uint64_t num_blocks,
258 			spdk_bdev_io_completion_cb cb, void *cb_arg)
259 {
260 	struct io_output *output = &g_io_output[g_io_output_index];
261 	struct spdk_bdev_io *child_io;
262 
263 	if (g_ignore_io_output) {
264 		return 0;
265 	}
266 
267 	if (g_max_io_size < g_strip_size) {
268 		SPDK_CU_ASSERT_FATAL(g_io_output_index < 2);
269 	} else {
270 		SPDK_CU_ASSERT_FATAL(g_io_output_index < (g_max_io_size / g_strip_size) + 1);
271 	}
272 	if (g_bdev_io_submit_status == 0) {
273 		set_io_output(output, desc, ch, offset_blocks, num_blocks, cb, cb_arg,
274 			      SPDK_BDEV_IO_TYPE_WRITE);
275 		g_io_output_index++;
276 
277 		child_io = calloc(1, sizeof(struct spdk_bdev_io));
278 		SPDK_CU_ASSERT_FATAL(child_io != NULL);
279 		cb(child_io, g_child_io_status_flag, cb_arg);
280 	}
281 
282 	return g_bdev_io_submit_status;
283 }
284 
285 int
286 spdk_bdev_writev_blocks_ext(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
287 			    struct iovec *iov, int iovcnt,
288 			    uint64_t offset_blocks, uint64_t num_blocks,
289 			    spdk_bdev_io_completion_cb cb, void *cb_arg,
290 			    struct spdk_bdev_ext_io_opts *opts)
291 {
292 	return spdk_bdev_writev_blocks(desc, ch, iov, iovcnt, offset_blocks, num_blocks, cb, cb_arg);
293 }
294 
295 int
296 spdk_bdev_reset(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
297 		spdk_bdev_io_completion_cb cb, void *cb_arg)
298 {
299 	struct io_output *output = &g_io_output[g_io_output_index];
300 	struct spdk_bdev_io *child_io;
301 
302 	if (g_ignore_io_output) {
303 		return 0;
304 	}
305 
306 	if (g_bdev_io_submit_status == 0) {
307 		set_io_output(output, desc, ch, 0, 0, cb, cb_arg, SPDK_BDEV_IO_TYPE_RESET);
308 		g_io_output_index++;
309 
310 		child_io = calloc(1, sizeof(struct spdk_bdev_io));
311 		SPDK_CU_ASSERT_FATAL(child_io != NULL);
312 		cb(child_io, g_child_io_status_flag, cb_arg);
313 	}
314 
315 	return g_bdev_io_submit_status;
316 }
317 
318 int
319 spdk_bdev_unmap_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
320 		       uint64_t offset_blocks, uint64_t num_blocks,
321 		       spdk_bdev_io_completion_cb cb, void *cb_arg)
322 {
323 	struct io_output *output = &g_io_output[g_io_output_index];
324 	struct spdk_bdev_io *child_io;
325 
326 	if (g_ignore_io_output) {
327 		return 0;
328 	}
329 
330 	if (g_bdev_io_submit_status == 0) {
331 		set_io_output(output, desc, ch, offset_blocks, num_blocks, cb, cb_arg,
332 			      SPDK_BDEV_IO_TYPE_UNMAP);
333 		g_io_output_index++;
334 
335 		child_io = calloc(1, sizeof(struct spdk_bdev_io));
336 		SPDK_CU_ASSERT_FATAL(child_io != NULL);
337 		cb(child_io, g_child_io_status_flag, cb_arg);
338 	}
339 
340 	return g_bdev_io_submit_status;
341 }
342 
343 void
344 spdk_bdev_destruct_done(struct spdk_bdev *bdev, int bdeverrno)
345 {
346 	CU_ASSERT(bdeverrno == 0);
347 	SPDK_CU_ASSERT_FATAL(bdev->internal.unregister_cb != NULL);
348 	bdev->internal.unregister_cb(bdev->internal.unregister_ctx, bdeverrno);
349 }
350 
351 void
352 spdk_bdev_unregister(struct spdk_bdev *bdev, spdk_bdev_unregister_cb cb_fn, void *cb_arg)
353 {
354 	int ret;
355 
356 	bdev->internal.unregister_cb = cb_fn;
357 	bdev->internal.unregister_ctx = cb_arg;
358 
359 	ret = bdev->fn_table->destruct(bdev->ctxt);
360 	CU_ASSERT(ret == 1);
361 
362 	poll_threads();
363 }
364 
365 int
366 spdk_bdev_open_ext(const char *bdev_name, bool write, spdk_bdev_event_cb_t event_cb,
367 		   void *event_ctx, struct spdk_bdev_desc **_desc)
368 {
369 	struct spdk_bdev *bdev;
370 
371 	bdev = spdk_bdev_get_by_name(bdev_name);
372 	if (bdev == NULL) {
373 		return -ENODEV;
374 	}
375 
376 	*_desc = (void *)bdev;
377 	return 0;
378 }
379 
380 struct spdk_bdev *
381 spdk_bdev_desc_get_bdev(struct spdk_bdev_desc *desc)
382 {
383 	return (void *)desc;
384 }
385 
386 char *
387 spdk_sprintf_alloc(const char *format, ...)
388 {
389 	return strdup(format);
390 }
391 
392 int
393 spdk_json_write_named_uint32(struct spdk_json_write_ctx *w, const char *name, uint32_t val)
394 {
395 	if (!g_test_multi_raids) {
396 		struct rpc_bdev_raid_create *req = g_rpc_req;
397 		if (strcmp(name, "strip_size_kb") == 0) {
398 			CU_ASSERT(req->strip_size_kb == val);
399 		} else if (strcmp(name, "blocklen_shift") == 0) {
400 			CU_ASSERT(spdk_u32log2(g_block_len) == val);
401 		} else if (strcmp(name, "num_base_bdevs") == 0) {
402 			CU_ASSERT(req->base_bdevs.num_base_bdevs == val);
403 		} else if (strcmp(name, "state") == 0) {
404 			CU_ASSERT(val == RAID_BDEV_STATE_ONLINE);
405 		} else if (strcmp(name, "destruct_called") == 0) {
406 			CU_ASSERT(val == 0);
407 		} else if (strcmp(name, "num_base_bdevs_discovered") == 0) {
408 			CU_ASSERT(req->base_bdevs.num_base_bdevs == val);
409 		}
410 	}
411 	return 0;
412 }
413 
414 int
415 spdk_json_write_named_string(struct spdk_json_write_ctx *w, const char *name, const char *val)
416 {
417 	if (g_test_multi_raids) {
418 		if (strcmp(name, "name") == 0) {
419 			g_get_raids_output[g_get_raids_count] = strdup(val);
420 			SPDK_CU_ASSERT_FATAL(g_get_raids_output[g_get_raids_count] != NULL);
421 			g_get_raids_count++;
422 		}
423 	} else {
424 		struct rpc_bdev_raid_create *req = g_rpc_req;
425 		if (strcmp(name, "raid_level") == 0) {
426 			CU_ASSERT(strcmp(val, raid_bdev_level_to_str(req->level)) == 0);
427 		}
428 	}
429 	return 0;
430 }
431 
432 void
433 spdk_bdev_free_io(struct spdk_bdev_io *bdev_io)
434 {
435 	if (bdev_io) {
436 		free(bdev_io);
437 	}
438 }
439 
440 /* It will cache split IOs for verification */
441 int
442 spdk_bdev_readv_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
443 		       struct iovec *iov, int iovcnt,
444 		       uint64_t offset_blocks, uint64_t num_blocks,
445 		       spdk_bdev_io_completion_cb cb, void *cb_arg)
446 {
447 	struct io_output *output = &g_io_output[g_io_output_index];
448 	struct spdk_bdev_io *child_io;
449 
450 	if (g_ignore_io_output) {
451 		return 0;
452 	}
453 
454 	SPDK_CU_ASSERT_FATAL(g_io_output_index <= (g_max_io_size / g_strip_size) + 1);
455 	if (g_bdev_io_submit_status == 0) {
456 		set_io_output(output, desc, ch, offset_blocks, num_blocks, cb, cb_arg,
457 			      SPDK_BDEV_IO_TYPE_READ);
458 		g_io_output_index++;
459 
460 		child_io = calloc(1, sizeof(struct spdk_bdev_io));
461 		SPDK_CU_ASSERT_FATAL(child_io != NULL);
462 		cb(child_io, g_child_io_status_flag, cb_arg);
463 	}
464 
465 	return g_bdev_io_submit_status;
466 }
467 
468 int
469 spdk_bdev_readv_blocks_ext(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
470 			   struct iovec *iov, int iovcnt,
471 			   uint64_t offset_blocks, uint64_t num_blocks,
472 			   spdk_bdev_io_completion_cb cb, void *cb_arg,
473 			   struct spdk_bdev_ext_io_opts *opts)
474 {
475 	return spdk_bdev_readv_blocks(desc, ch, iov, iovcnt, offset_blocks, num_blocks, cb, cb_arg);
476 }
477 
478 void
479 spdk_bdev_module_release_bdev(struct spdk_bdev *bdev)
480 {
481 	CU_ASSERT(bdev->internal.claim_type == SPDK_BDEV_CLAIM_EXCL_WRITE);
482 	CU_ASSERT(bdev->internal.claim.v1.module != NULL);
483 	bdev->internal.claim_type = SPDK_BDEV_CLAIM_NONE;
484 	bdev->internal.claim.v1.module = NULL;
485 }
486 
487 int
488 spdk_bdev_module_claim_bdev(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
489 			    struct spdk_bdev_module *module)
490 {
491 	if (bdev->internal.claim_type != SPDK_BDEV_CLAIM_NONE) {
492 		CU_ASSERT(bdev->internal.claim.v1.module != NULL);
493 		return -1;
494 	}
495 	CU_ASSERT(bdev->internal.claim.v1.module == NULL);
496 	bdev->internal.claim_type = SPDK_BDEV_CLAIM_EXCL_WRITE;
497 	bdev->internal.claim.v1.module = module;
498 	return 0;
499 }
500 
501 int
502 spdk_json_decode_object(const struct spdk_json_val *values,
503 			const struct spdk_json_object_decoder *decoders, size_t num_decoders,
504 			void *out)
505 {
506 	struct rpc_bdev_raid_create *req, *_out;
507 	size_t i;
508 
509 	if (g_json_decode_obj_err) {
510 		return -1;
511 	} else if (g_json_decode_obj_create) {
512 		req = g_rpc_req;
513 		_out = out;
514 
515 		_out->name = strdup(req->name);
516 		SPDK_CU_ASSERT_FATAL(_out->name != NULL);
517 		_out->strip_size_kb = req->strip_size_kb;
518 		_out->level = req->level;
519 		_out->base_bdevs.num_base_bdevs = req->base_bdevs.num_base_bdevs;
520 		for (i = 0; i < req->base_bdevs.num_base_bdevs; i++) {
521 			_out->base_bdevs.base_bdevs[i] = strdup(req->base_bdevs.base_bdevs[i]);
522 			SPDK_CU_ASSERT_FATAL(_out->base_bdevs.base_bdevs[i]);
523 		}
524 	} else {
525 		memcpy(out, g_rpc_req, g_rpc_req_size);
526 	}
527 
528 	return 0;
529 }
530 
531 struct spdk_json_write_ctx *
532 spdk_jsonrpc_begin_result(struct spdk_jsonrpc_request *request)
533 {
534 	return (void *)1;
535 }
536 
537 void
538 spdk_jsonrpc_send_error_response(struct spdk_jsonrpc_request *request,
539 				 int error_code, const char *msg)
540 {
541 	g_rpc_err = 1;
542 }
543 
544 void
545 spdk_jsonrpc_send_error_response_fmt(struct spdk_jsonrpc_request *request,
546 				     int error_code, const char *fmt, ...)
547 {
548 	g_rpc_err = 1;
549 }
550 
551 struct spdk_bdev *
552 spdk_bdev_get_by_name(const char *bdev_name)
553 {
554 	struct spdk_bdev *bdev;
555 
556 	if (!TAILQ_EMPTY(&g_bdev_list)) {
557 		TAILQ_FOREACH(bdev, &g_bdev_list, internal.link) {
558 			if (strcmp(bdev_name, bdev->name) == 0) {
559 				return bdev;
560 			}
561 		}
562 	}
563 
564 	return NULL;
565 }
566 
567 static void
568 bdev_io_cleanup(struct spdk_bdev_io *bdev_io)
569 {
570 	if (bdev_io->u.bdev.iovs) {
571 		if (bdev_io->u.bdev.iovs->iov_base) {
572 			free(bdev_io->u.bdev.iovs->iov_base);
573 		}
574 		free(bdev_io->u.bdev.iovs);
575 	}
576 	free(bdev_io);
577 }
578 
579 static void
580 bdev_io_initialize(struct spdk_bdev_io *bdev_io, struct spdk_io_channel *ch, struct spdk_bdev *bdev,
581 		   uint64_t lba, uint64_t blocks, int16_t iotype)
582 {
583 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
584 
585 	bdev_io->bdev = bdev;
586 	bdev_io->u.bdev.offset_blocks = lba;
587 	bdev_io->u.bdev.num_blocks = blocks;
588 	bdev_io->type = iotype;
589 
590 	if (bdev_io->type == SPDK_BDEV_IO_TYPE_UNMAP || bdev_io->type == SPDK_BDEV_IO_TYPE_FLUSH) {
591 		return;
592 	}
593 
594 	bdev_io->u.bdev.iovcnt = 1;
595 	bdev_io->u.bdev.iovs = calloc(1, sizeof(struct iovec));
596 	SPDK_CU_ASSERT_FATAL(bdev_io->u.bdev.iovs != NULL);
597 	bdev_io->u.bdev.iovs->iov_base = calloc(1, bdev_io->u.bdev.num_blocks * g_block_len);
598 	SPDK_CU_ASSERT_FATAL(bdev_io->u.bdev.iovs->iov_base != NULL);
599 	bdev_io->u.bdev.iovs->iov_len = bdev_io->u.bdev.num_blocks * g_block_len;
600 	bdev_io->internal.ch = channel;
601 }
602 
603 static void
604 verify_reset_io(struct spdk_bdev_io *bdev_io, uint8_t num_base_drives,
605 		struct raid_bdev_io_channel *ch_ctx, struct raid_bdev *raid_bdev, uint32_t io_status)
606 {
607 	uint8_t index = 0;
608 	struct io_output *output;
609 
610 	SPDK_CU_ASSERT_FATAL(raid_bdev != NULL);
611 	SPDK_CU_ASSERT_FATAL(num_base_drives != 0);
612 	SPDK_CU_ASSERT_FATAL(io_status != INVALID_IO_SUBMIT);
613 	SPDK_CU_ASSERT_FATAL(ch_ctx->base_channel != NULL);
614 
615 	CU_ASSERT(g_io_output_index == num_base_drives);
616 	for (index = 0; index < g_io_output_index; index++) {
617 		output = &g_io_output[index];
618 		CU_ASSERT(ch_ctx->base_channel[index] == output->ch);
619 		CU_ASSERT(raid_bdev->base_bdev_info[index].desc == output->desc);
620 		CU_ASSERT(bdev_io->type == output->iotype);
621 	}
622 	CU_ASSERT(g_io_comp_status == io_status);
623 }
624 
625 static void
626 verify_io(struct spdk_bdev_io *bdev_io, uint8_t num_base_drives,
627 	  struct raid_bdev_io_channel *ch_ctx, struct raid_bdev *raid_bdev, uint32_t io_status)
628 {
629 	uint32_t strip_shift = spdk_u32log2(g_strip_size);
630 	uint64_t start_strip = bdev_io->u.bdev.offset_blocks >> strip_shift;
631 	uint64_t end_strip = (bdev_io->u.bdev.offset_blocks + bdev_io->u.bdev.num_blocks - 1) >>
632 			     strip_shift;
633 	uint32_t splits_reqd = (end_strip - start_strip + 1);
634 	uint32_t strip;
635 	uint64_t pd_strip;
636 	uint8_t pd_idx;
637 	uint32_t offset_in_strip;
638 	uint64_t pd_lba;
639 	uint64_t pd_blocks;
640 	uint32_t index = 0;
641 	struct io_output *output;
642 
643 	if (io_status == INVALID_IO_SUBMIT) {
644 		CU_ASSERT(g_io_comp_status == false);
645 		return;
646 	}
647 	SPDK_CU_ASSERT_FATAL(raid_bdev != NULL);
648 	SPDK_CU_ASSERT_FATAL(num_base_drives != 0);
649 
650 	CU_ASSERT(splits_reqd == g_io_output_index);
651 	for (strip = start_strip; strip <= end_strip; strip++, index++) {
652 		pd_strip = strip / num_base_drives;
653 		pd_idx = strip % num_base_drives;
654 		if (strip == start_strip) {
655 			offset_in_strip = bdev_io->u.bdev.offset_blocks & (g_strip_size - 1);
656 			pd_lba = (pd_strip << strip_shift) + offset_in_strip;
657 			if (strip == end_strip) {
658 				pd_blocks = bdev_io->u.bdev.num_blocks;
659 			} else {
660 				pd_blocks = g_strip_size - offset_in_strip;
661 			}
662 		} else if (strip == end_strip) {
663 			pd_lba = pd_strip << strip_shift;
664 			pd_blocks = ((bdev_io->u.bdev.offset_blocks + bdev_io->u.bdev.num_blocks - 1) &
665 				     (g_strip_size - 1)) + 1;
666 		} else {
667 			pd_lba = pd_strip << raid_bdev->strip_size_shift;
668 			pd_blocks = raid_bdev->strip_size;
669 		}
670 		output = &g_io_output[index];
671 		CU_ASSERT(pd_lba == output->offset_blocks);
672 		CU_ASSERT(pd_blocks == output->num_blocks);
673 		CU_ASSERT(ch_ctx->base_channel[pd_idx] == output->ch);
674 		CU_ASSERT(raid_bdev->base_bdev_info[pd_idx].desc == output->desc);
675 		CU_ASSERT(bdev_io->type == output->iotype);
676 	}
677 	CU_ASSERT(g_io_comp_status == io_status);
678 }
679 
680 static void
681 verify_io_without_payload(struct spdk_bdev_io *bdev_io, uint8_t num_base_drives,
682 			  struct raid_bdev_io_channel *ch_ctx, struct raid_bdev *raid_bdev,
683 			  uint32_t io_status)
684 {
685 	uint32_t strip_shift = spdk_u32log2(g_strip_size);
686 	uint64_t start_offset_in_strip = bdev_io->u.bdev.offset_blocks % g_strip_size;
687 	uint64_t end_offset_in_strip = (bdev_io->u.bdev.offset_blocks + bdev_io->u.bdev.num_blocks - 1) %
688 				       g_strip_size;
689 	uint64_t start_strip = bdev_io->u.bdev.offset_blocks >> strip_shift;
690 	uint64_t end_strip = (bdev_io->u.bdev.offset_blocks + bdev_io->u.bdev.num_blocks - 1) >>
691 			     strip_shift;
692 	uint8_t n_disks_involved;
693 	uint64_t start_strip_disk_idx;
694 	uint64_t end_strip_disk_idx;
695 	uint64_t nblocks_in_start_disk;
696 	uint64_t offset_in_start_disk;
697 	uint8_t disk_idx;
698 	uint64_t base_io_idx;
699 	uint64_t sum_nblocks = 0;
700 	struct io_output *output;
701 
702 	if (io_status == INVALID_IO_SUBMIT) {
703 		CU_ASSERT(g_io_comp_status == false);
704 		return;
705 	}
706 	SPDK_CU_ASSERT_FATAL(raid_bdev != NULL);
707 	SPDK_CU_ASSERT_FATAL(num_base_drives != 0);
708 	SPDK_CU_ASSERT_FATAL(bdev_io->type != SPDK_BDEV_IO_TYPE_READ);
709 	SPDK_CU_ASSERT_FATAL(bdev_io->type != SPDK_BDEV_IO_TYPE_WRITE);
710 
711 	n_disks_involved = spdk_min(end_strip - start_strip + 1, num_base_drives);
712 	CU_ASSERT(n_disks_involved == g_io_output_index);
713 
714 	start_strip_disk_idx = start_strip % num_base_drives;
715 	end_strip_disk_idx = end_strip % num_base_drives;
716 
717 	offset_in_start_disk = g_io_output[0].offset_blocks;
718 	nblocks_in_start_disk = g_io_output[0].num_blocks;
719 
720 	for (base_io_idx = 0, disk_idx = start_strip_disk_idx; base_io_idx < n_disks_involved;
721 	     base_io_idx++, disk_idx++) {
722 		uint64_t start_offset_in_disk;
723 		uint64_t end_offset_in_disk;
724 
725 		output = &g_io_output[base_io_idx];
726 
727 		/* round disk_idx */
728 		if (disk_idx >= num_base_drives) {
729 			disk_idx %= num_base_drives;
730 		}
731 
732 		/* start_offset_in_disk aligned in strip check:
733 		 * The first base io has a same start_offset_in_strip with the whole raid io.
734 		 * Other base io should have aligned start_offset_in_strip which is 0.
735 		 */
736 		start_offset_in_disk = output->offset_blocks;
737 		if (base_io_idx == 0) {
738 			CU_ASSERT(start_offset_in_disk % g_strip_size == start_offset_in_strip);
739 		} else {
740 			CU_ASSERT(start_offset_in_disk % g_strip_size == 0);
741 		}
742 
743 		/* end_offset_in_disk aligned in strip check:
744 		 * Base io on disk at which end_strip is located, has a same end_offset_in_strip
745 		 * with the whole raid io.
746 		 * Other base io should have aligned end_offset_in_strip.
747 		 */
748 		end_offset_in_disk = output->offset_blocks + output->num_blocks - 1;
749 		if (disk_idx == end_strip_disk_idx) {
750 			CU_ASSERT(end_offset_in_disk % g_strip_size == end_offset_in_strip);
751 		} else {
752 			CU_ASSERT(end_offset_in_disk % g_strip_size == g_strip_size - 1);
753 		}
754 
755 		/* start_offset_in_disk compared with start_disk.
756 		 * 1. For disk_idx which is larger than start_strip_disk_idx: Its start_offset_in_disk
757 		 *    mustn't be larger than the start offset of start_offset_in_disk; And the gap
758 		 *    must be less than strip size.
759 		 * 2. For disk_idx which is less than start_strip_disk_idx, Its start_offset_in_disk
760 		 *    must be larger than the start offset of start_offset_in_disk; And the gap mustn't
761 		 *    be less than strip size.
762 		 */
763 		if (disk_idx > start_strip_disk_idx) {
764 			CU_ASSERT(start_offset_in_disk <= offset_in_start_disk);
765 			CU_ASSERT(offset_in_start_disk - start_offset_in_disk < g_strip_size);
766 		} else if (disk_idx < start_strip_disk_idx) {
767 			CU_ASSERT(start_offset_in_disk > offset_in_start_disk);
768 			CU_ASSERT(output->offset_blocks - offset_in_start_disk <= g_strip_size);
769 		}
770 
771 		/* nblocks compared with start_disk:
772 		 * The gap between them must be within a strip size.
773 		 */
774 		if (output->num_blocks <= nblocks_in_start_disk) {
775 			CU_ASSERT(nblocks_in_start_disk - output->num_blocks <= g_strip_size);
776 		} else {
777 			CU_ASSERT(output->num_blocks - nblocks_in_start_disk < g_strip_size);
778 		}
779 
780 		sum_nblocks += output->num_blocks;
781 
782 		CU_ASSERT(ch_ctx->base_channel[disk_idx] == output->ch);
783 		CU_ASSERT(raid_bdev->base_bdev_info[disk_idx].desc == output->desc);
784 		CU_ASSERT(bdev_io->type == output->iotype);
785 	}
786 
787 	/* Sum of each nblocks should be same with raid bdev_io */
788 	CU_ASSERT(bdev_io->u.bdev.num_blocks == sum_nblocks);
789 
790 	CU_ASSERT(g_io_comp_status == io_status);
791 }
792 
793 static void
794 verify_raid_bdev_present(const char *name, bool presence)
795 {
796 	struct raid_bdev *pbdev;
797 	bool   pbdev_found;
798 
799 	pbdev_found = false;
800 	TAILQ_FOREACH(pbdev, &g_raid_bdev_list, global_link) {
801 		if (strcmp(pbdev->bdev.name, name) == 0) {
802 			pbdev_found = true;
803 			break;
804 		}
805 	}
806 	if (presence == true) {
807 		CU_ASSERT(pbdev_found == true);
808 	} else {
809 		CU_ASSERT(pbdev_found == false);
810 	}
811 }
812 
813 static void
814 verify_raid_bdev(struct rpc_bdev_raid_create *r, bool presence, uint32_t raid_state)
815 {
816 	struct raid_bdev *pbdev;
817 	struct raid_base_bdev_info *base_info;
818 	struct spdk_bdev *bdev = NULL;
819 	bool   pbdev_found;
820 	uint64_t min_blockcnt = 0xFFFFFFFFFFFFFFFF;
821 
822 	pbdev_found = false;
823 	TAILQ_FOREACH(pbdev, &g_raid_bdev_list, global_link) {
824 		if (strcmp(pbdev->bdev.name, r->name) == 0) {
825 			pbdev_found = true;
826 			if (presence == false) {
827 				break;
828 			}
829 			CU_ASSERT(pbdev->base_bdev_info != NULL);
830 			CU_ASSERT(pbdev->strip_size == ((r->strip_size_kb * 1024) / g_block_len));
831 			CU_ASSERT(pbdev->strip_size_shift == spdk_u32log2(((r->strip_size_kb * 1024) /
832 					g_block_len)));
833 			CU_ASSERT(pbdev->blocklen_shift == spdk_u32log2(g_block_len));
834 			CU_ASSERT((uint32_t)pbdev->state == raid_state);
835 			CU_ASSERT(pbdev->num_base_bdevs == r->base_bdevs.num_base_bdevs);
836 			CU_ASSERT(pbdev->num_base_bdevs_discovered == r->base_bdevs.num_base_bdevs);
837 			CU_ASSERT(pbdev->level == r->level);
838 			CU_ASSERT(pbdev->base_bdev_info != NULL);
839 			RAID_FOR_EACH_BASE_BDEV(pbdev, base_info) {
840 				CU_ASSERT(base_info->bdev != NULL);
841 				bdev = spdk_bdev_get_by_name(base_info->bdev->name);
842 				CU_ASSERT(bdev != NULL);
843 				CU_ASSERT(base_info->remove_scheduled == false);
844 
845 				if (bdev && bdev->blockcnt < min_blockcnt) {
846 					min_blockcnt = bdev->blockcnt;
847 				}
848 			}
849 			CU_ASSERT((((min_blockcnt / (r->strip_size_kb * 1024 / g_block_len)) *
850 				    (r->strip_size_kb * 1024 / g_block_len)) *
851 				   r->base_bdevs.num_base_bdevs) == pbdev->bdev.blockcnt);
852 			CU_ASSERT(strcmp(pbdev->bdev.product_name, "Raid Volume") == 0);
853 			CU_ASSERT(pbdev->bdev.write_cache == 0);
854 			CU_ASSERT(pbdev->bdev.blocklen == g_block_len);
855 			if (pbdev->num_base_bdevs > 1) {
856 				CU_ASSERT(pbdev->bdev.optimal_io_boundary == pbdev->strip_size);
857 				CU_ASSERT(pbdev->bdev.split_on_optimal_io_boundary == true);
858 			} else {
859 				CU_ASSERT(pbdev->bdev.optimal_io_boundary == 0);
860 				CU_ASSERT(pbdev->bdev.split_on_optimal_io_boundary == false);
861 			}
862 			CU_ASSERT(pbdev->bdev.ctxt == pbdev);
863 			CU_ASSERT(pbdev->bdev.fn_table == &g_raid_bdev_fn_table);
864 			CU_ASSERT(pbdev->bdev.module == &g_raid_if);
865 			break;
866 		}
867 	}
868 	if (presence == true) {
869 		CU_ASSERT(pbdev_found == true);
870 	} else {
871 		CU_ASSERT(pbdev_found == false);
872 	}
873 }
874 
875 static void
876 verify_get_raids(struct rpc_bdev_raid_create *construct_req,
877 		 uint8_t g_max_raids,
878 		 char **g_get_raids_output, uint32_t g_get_raids_count)
879 {
880 	uint8_t i, j;
881 	bool found;
882 
883 	CU_ASSERT(g_max_raids == g_get_raids_count);
884 	if (g_max_raids == g_get_raids_count) {
885 		for (i = 0; i < g_max_raids; i++) {
886 			found = false;
887 			for (j = 0; j < g_max_raids; j++) {
888 				if (construct_req[i].name &&
889 				    strcmp(construct_req[i].name, g_get_raids_output[i]) == 0) {
890 					found = true;
891 					break;
892 				}
893 			}
894 			CU_ASSERT(found == true);
895 		}
896 	}
897 }
898 
899 static void
900 create_base_bdevs(uint32_t bbdev_start_idx)
901 {
902 	uint8_t i;
903 	struct spdk_bdev *base_bdev;
904 	char name[16];
905 
906 	for (i = 0; i < g_max_base_drives; i++, bbdev_start_idx++) {
907 		snprintf(name, 16, "%s%u%s", "Nvme", bbdev_start_idx, "n1");
908 		base_bdev = calloc(1, sizeof(struct spdk_bdev));
909 		SPDK_CU_ASSERT_FATAL(base_bdev != NULL);
910 		base_bdev->name = strdup(name);
911 		SPDK_CU_ASSERT_FATAL(base_bdev->name != NULL);
912 		base_bdev->blocklen = g_block_len;
913 		base_bdev->blockcnt = BLOCK_CNT;
914 		TAILQ_INSERT_TAIL(&g_bdev_list, base_bdev, internal.link);
915 	}
916 }
917 
918 static void
919 create_test_req(struct rpc_bdev_raid_create *r, const char *raid_name,
920 		uint8_t bbdev_start_idx, bool create_base_bdev)
921 {
922 	uint8_t i;
923 	char name[16];
924 	uint8_t bbdev_idx = bbdev_start_idx;
925 
926 	r->name = strdup(raid_name);
927 	SPDK_CU_ASSERT_FATAL(r->name != NULL);
928 	r->strip_size_kb = (g_strip_size * g_block_len) / 1024;
929 	r->level = RAID0;
930 	r->base_bdevs.num_base_bdevs = g_max_base_drives;
931 	for (i = 0; i < g_max_base_drives; i++, bbdev_idx++) {
932 		snprintf(name, 16, "%s%u%s", "Nvme", bbdev_idx, "n1");
933 		r->base_bdevs.base_bdevs[i] = strdup(name);
934 		SPDK_CU_ASSERT_FATAL(r->base_bdevs.base_bdevs[i] != NULL);
935 	}
936 	if (create_base_bdev == true) {
937 		create_base_bdevs(bbdev_start_idx);
938 	}
939 	g_rpc_req = r;
940 	g_rpc_req_size = sizeof(*r);
941 }
942 
943 static void
944 create_raid_bdev_create_req(struct rpc_bdev_raid_create *r, const char *raid_name,
945 			    uint8_t bbdev_start_idx, bool create_base_bdev,
946 			    uint8_t json_decode_obj_err)
947 {
948 	create_test_req(r, raid_name, bbdev_start_idx, create_base_bdev);
949 
950 	g_rpc_err = 0;
951 	g_json_decode_obj_create = 1;
952 	g_json_decode_obj_err = json_decode_obj_err;
953 	g_config_level_create = 0;
954 	g_test_multi_raids = 0;
955 }
956 
957 static void
958 free_test_req(struct rpc_bdev_raid_create *r)
959 {
960 	uint8_t i;
961 
962 	free(r->name);
963 	for (i = 0; i < r->base_bdevs.num_base_bdevs; i++) {
964 		free(r->base_bdevs.base_bdevs[i]);
965 	}
966 }
967 
968 static void
969 create_raid_bdev_delete_req(struct rpc_bdev_raid_delete *r, const char *raid_name,
970 			    uint8_t json_decode_obj_err)
971 {
972 	r->name = strdup(raid_name);
973 	SPDK_CU_ASSERT_FATAL(r->name != NULL);
974 
975 	g_rpc_req = r;
976 	g_rpc_req_size = sizeof(*r);
977 	g_rpc_err = 0;
978 	g_json_decode_obj_create = 0;
979 	g_json_decode_obj_err = json_decode_obj_err;
980 	g_config_level_create = 0;
981 	g_test_multi_raids = 0;
982 }
983 
984 static void
985 create_get_raids_req(struct rpc_bdev_raid_get_bdevs *r, const char *category,
986 		     uint8_t json_decode_obj_err)
987 {
988 	r->category = strdup(category);
989 	SPDK_CU_ASSERT_FATAL(r->category != NULL);
990 
991 	g_rpc_req = r;
992 	g_rpc_req_size = sizeof(*r);
993 	g_rpc_err = 0;
994 	g_json_decode_obj_create = 0;
995 	g_json_decode_obj_err = json_decode_obj_err;
996 	g_config_level_create = 0;
997 	g_test_multi_raids = 1;
998 	g_get_raids_count = 0;
999 }
1000 
1001 static void
1002 test_create_raid(void)
1003 {
1004 	struct rpc_bdev_raid_create req;
1005 	struct rpc_bdev_raid_delete delete_req;
1006 
1007 	set_globals();
1008 	CU_ASSERT(raid_bdev_init() == 0);
1009 
1010 	verify_raid_bdev_present("raid1", false);
1011 	create_raid_bdev_create_req(&req, "raid1", 0, true, 0);
1012 	rpc_bdev_raid_create(NULL, NULL);
1013 	CU_ASSERT(g_rpc_err == 0);
1014 	verify_raid_bdev(&req, true, RAID_BDEV_STATE_ONLINE);
1015 	free_test_req(&req);
1016 
1017 	create_raid_bdev_delete_req(&delete_req, "raid1", 0);
1018 	rpc_bdev_raid_delete(NULL, NULL);
1019 	CU_ASSERT(g_rpc_err == 0);
1020 	raid_bdev_exit();
1021 	base_bdevs_cleanup();
1022 	reset_globals();
1023 }
1024 
1025 static void
1026 test_delete_raid(void)
1027 {
1028 	struct rpc_bdev_raid_create construct_req;
1029 	struct rpc_bdev_raid_delete delete_req;
1030 
1031 	set_globals();
1032 	CU_ASSERT(raid_bdev_init() == 0);
1033 
1034 	verify_raid_bdev_present("raid1", false);
1035 	create_raid_bdev_create_req(&construct_req, "raid1", 0, true, 0);
1036 	rpc_bdev_raid_create(NULL, NULL);
1037 	CU_ASSERT(g_rpc_err == 0);
1038 	verify_raid_bdev(&construct_req, true, RAID_BDEV_STATE_ONLINE);
1039 	free_test_req(&construct_req);
1040 
1041 	create_raid_bdev_delete_req(&delete_req, "raid1", 0);
1042 	rpc_bdev_raid_delete(NULL, NULL);
1043 	CU_ASSERT(g_rpc_err == 0);
1044 	verify_raid_bdev_present("raid1", false);
1045 
1046 	raid_bdev_exit();
1047 	base_bdevs_cleanup();
1048 	reset_globals();
1049 }
1050 
1051 static void
1052 test_create_raid_invalid_args(void)
1053 {
1054 	struct rpc_bdev_raid_create req;
1055 	struct rpc_bdev_raid_delete destroy_req;
1056 	struct raid_bdev *raid_bdev;
1057 
1058 	set_globals();
1059 	CU_ASSERT(raid_bdev_init() == 0);
1060 
1061 	verify_raid_bdev_present("raid1", false);
1062 	create_raid_bdev_create_req(&req, "raid1", 0, true, 0);
1063 	req.level = INVALID_RAID_LEVEL;
1064 	rpc_bdev_raid_create(NULL, NULL);
1065 	CU_ASSERT(g_rpc_err == 1);
1066 	free_test_req(&req);
1067 	verify_raid_bdev_present("raid1", false);
1068 
1069 	create_raid_bdev_create_req(&req, "raid1", 0, false, 1);
1070 	rpc_bdev_raid_create(NULL, NULL);
1071 	CU_ASSERT(g_rpc_err == 1);
1072 	free_test_req(&req);
1073 	verify_raid_bdev_present("raid1", false);
1074 
1075 	create_raid_bdev_create_req(&req, "raid1", 0, false, 0);
1076 	req.strip_size_kb = 1231;
1077 	rpc_bdev_raid_create(NULL, NULL);
1078 	CU_ASSERT(g_rpc_err == 1);
1079 	free_test_req(&req);
1080 	verify_raid_bdev_present("raid1", false);
1081 
1082 	create_raid_bdev_create_req(&req, "raid1", 0, false, 0);
1083 	rpc_bdev_raid_create(NULL, NULL);
1084 	CU_ASSERT(g_rpc_err == 0);
1085 	verify_raid_bdev(&req, true, RAID_BDEV_STATE_ONLINE);
1086 	free_test_req(&req);
1087 
1088 	create_raid_bdev_create_req(&req, "raid1", 0, false, 0);
1089 	rpc_bdev_raid_create(NULL, NULL);
1090 	CU_ASSERT(g_rpc_err == 1);
1091 	free_test_req(&req);
1092 
1093 	create_raid_bdev_create_req(&req, "raid2", 0, false, 0);
1094 	rpc_bdev_raid_create(NULL, NULL);
1095 	CU_ASSERT(g_rpc_err == 1);
1096 	free_test_req(&req);
1097 	verify_raid_bdev_present("raid2", false);
1098 
1099 	create_raid_bdev_create_req(&req, "raid2", g_max_base_drives, true, 0);
1100 	free(req.base_bdevs.base_bdevs[g_max_base_drives - 1]);
1101 	req.base_bdevs.base_bdevs[g_max_base_drives - 1] = strdup("Nvme0n1");
1102 	SPDK_CU_ASSERT_FATAL(req.base_bdevs.base_bdevs[g_max_base_drives - 1] != NULL);
1103 	rpc_bdev_raid_create(NULL, NULL);
1104 	CU_ASSERT(g_rpc_err == 1);
1105 	free_test_req(&req);
1106 	verify_raid_bdev_present("raid2", false);
1107 
1108 	create_raid_bdev_create_req(&req, "raid2", g_max_base_drives, true, 0);
1109 	free(req.base_bdevs.base_bdevs[g_max_base_drives - 1]);
1110 	req.base_bdevs.base_bdevs[g_max_base_drives - 1] = strdup("Nvme100000n1");
1111 	SPDK_CU_ASSERT_FATAL(req.base_bdevs.base_bdevs[g_max_base_drives - 1] != NULL);
1112 	rpc_bdev_raid_create(NULL, NULL);
1113 	CU_ASSERT(g_rpc_err == 0);
1114 	free_test_req(&req);
1115 	verify_raid_bdev_present("raid2", true);
1116 	raid_bdev = raid_bdev_find_by_name("raid2");
1117 	SPDK_CU_ASSERT_FATAL(raid_bdev != NULL);
1118 	check_and_remove_raid_bdev(raid_bdev);
1119 
1120 	create_raid_bdev_create_req(&req, "raid2", g_max_base_drives, false, 0);
1121 	rpc_bdev_raid_create(NULL, NULL);
1122 	CU_ASSERT(g_rpc_err == 0);
1123 	free_test_req(&req);
1124 	verify_raid_bdev_present("raid2", true);
1125 	verify_raid_bdev_present("raid1", true);
1126 
1127 	create_raid_bdev_delete_req(&destroy_req, "raid1", 0);
1128 	rpc_bdev_raid_delete(NULL, NULL);
1129 	create_raid_bdev_delete_req(&destroy_req, "raid2", 0);
1130 	rpc_bdev_raid_delete(NULL, NULL);
1131 	raid_bdev_exit();
1132 	base_bdevs_cleanup();
1133 	reset_globals();
1134 }
1135 
1136 static void
1137 test_delete_raid_invalid_args(void)
1138 {
1139 	struct rpc_bdev_raid_create construct_req;
1140 	struct rpc_bdev_raid_delete destroy_req;
1141 
1142 	set_globals();
1143 	CU_ASSERT(raid_bdev_init() == 0);
1144 
1145 	verify_raid_bdev_present("raid1", false);
1146 	create_raid_bdev_create_req(&construct_req, "raid1", 0, true, 0);
1147 	rpc_bdev_raid_create(NULL, NULL);
1148 	CU_ASSERT(g_rpc_err == 0);
1149 	verify_raid_bdev(&construct_req, true, RAID_BDEV_STATE_ONLINE);
1150 	free_test_req(&construct_req);
1151 
1152 	create_raid_bdev_delete_req(&destroy_req, "raid2", 0);
1153 	rpc_bdev_raid_delete(NULL, NULL);
1154 	CU_ASSERT(g_rpc_err == 1);
1155 
1156 	create_raid_bdev_delete_req(&destroy_req, "raid1", 1);
1157 	rpc_bdev_raid_delete(NULL, NULL);
1158 	CU_ASSERT(g_rpc_err == 1);
1159 	free(destroy_req.name);
1160 	verify_raid_bdev_present("raid1", true);
1161 
1162 	create_raid_bdev_delete_req(&destroy_req, "raid1", 0);
1163 	rpc_bdev_raid_delete(NULL, NULL);
1164 	CU_ASSERT(g_rpc_err == 0);
1165 	verify_raid_bdev_present("raid1", false);
1166 
1167 	raid_bdev_exit();
1168 	base_bdevs_cleanup();
1169 	reset_globals();
1170 }
1171 
1172 static void
1173 test_io_channel(void)
1174 {
1175 	struct rpc_bdev_raid_create req;
1176 	struct rpc_bdev_raid_delete destroy_req;
1177 	struct raid_bdev *pbdev;
1178 	struct raid_bdev_io_channel *ch_ctx;
1179 	uint8_t i;
1180 
1181 	set_globals();
1182 	CU_ASSERT(raid_bdev_init() == 0);
1183 
1184 	create_raid_bdev_create_req(&req, "raid1", 0, true, 0);
1185 	verify_raid_bdev_present("raid1", false);
1186 	rpc_bdev_raid_create(NULL, NULL);
1187 	CU_ASSERT(g_rpc_err == 0);
1188 	verify_raid_bdev(&req, true, RAID_BDEV_STATE_ONLINE);
1189 
1190 	TAILQ_FOREACH(pbdev, &g_raid_bdev_list, global_link) {
1191 		if (strcmp(pbdev->bdev.name, "raid1") == 0) {
1192 			break;
1193 		}
1194 	}
1195 	CU_ASSERT(pbdev != NULL);
1196 	ch_ctx = calloc(1, sizeof(struct raid_bdev_io_channel));
1197 	SPDK_CU_ASSERT_FATAL(ch_ctx != NULL);
1198 
1199 	CU_ASSERT(raid_bdev_create_cb(pbdev, ch_ctx) == 0);
1200 	for (i = 0; i < req.base_bdevs.num_base_bdevs; i++) {
1201 		CU_ASSERT(ch_ctx->base_channel && ch_ctx->base_channel[i] == &g_io_channel);
1202 	}
1203 	raid_bdev_destroy_cb(pbdev, ch_ctx);
1204 	CU_ASSERT(ch_ctx->base_channel == NULL);
1205 	free_test_req(&req);
1206 
1207 	create_raid_bdev_delete_req(&destroy_req, "raid1", 0);
1208 	rpc_bdev_raid_delete(NULL, NULL);
1209 	CU_ASSERT(g_rpc_err == 0);
1210 	verify_raid_bdev_present("raid1", false);
1211 
1212 	free(ch_ctx);
1213 	raid_bdev_exit();
1214 	base_bdevs_cleanup();
1215 	reset_globals();
1216 }
1217 
1218 static void
1219 test_write_io(void)
1220 {
1221 	struct rpc_bdev_raid_create req;
1222 	struct rpc_bdev_raid_delete destroy_req;
1223 	struct raid_bdev *pbdev;
1224 	struct spdk_io_channel *ch;
1225 	struct raid_bdev_io_channel *ch_ctx;
1226 	uint8_t i;
1227 	struct spdk_bdev_io *bdev_io;
1228 	uint64_t io_len;
1229 	uint64_t lba = 0;
1230 	struct spdk_io_channel *ch_b;
1231 	struct spdk_bdev_channel *ch_b_ctx;
1232 
1233 	set_globals();
1234 	CU_ASSERT(raid_bdev_init() == 0);
1235 
1236 	create_raid_bdev_create_req(&req, "raid1", 0, true, 0);
1237 	verify_raid_bdev_present("raid1", false);
1238 	rpc_bdev_raid_create(NULL, NULL);
1239 	CU_ASSERT(g_rpc_err == 0);
1240 	verify_raid_bdev(&req, true, RAID_BDEV_STATE_ONLINE);
1241 	TAILQ_FOREACH(pbdev, &g_raid_bdev_list, global_link) {
1242 		if (strcmp(pbdev->bdev.name, "raid1") == 0) {
1243 			break;
1244 		}
1245 	}
1246 	CU_ASSERT(pbdev != NULL);
1247 	ch = calloc(1, sizeof(struct spdk_io_channel) + sizeof(struct raid_bdev_io_channel));
1248 	SPDK_CU_ASSERT_FATAL(ch != NULL);
1249 
1250 	ch_b = calloc(1, sizeof(struct spdk_io_channel) + sizeof(struct spdk_bdev_channel));
1251 	SPDK_CU_ASSERT_FATAL(ch_b != NULL);
1252 	ch_b_ctx = spdk_io_channel_get_ctx(ch_b);
1253 	ch_b_ctx->channel = ch;
1254 
1255 	ch_ctx = spdk_io_channel_get_ctx(ch);
1256 	SPDK_CU_ASSERT_FATAL(ch_ctx != NULL);
1257 
1258 	CU_ASSERT(raid_bdev_create_cb(pbdev, ch_ctx) == 0);
1259 	for (i = 0; i < req.base_bdevs.num_base_bdevs; i++) {
1260 		CU_ASSERT(ch_ctx->base_channel && ch_ctx->base_channel[i] == &g_io_channel);
1261 	}
1262 
1263 	/* test 2 IO sizes based on global strip size set earlier */
1264 	for (i = 0; i < 2; i++) {
1265 		bdev_io = calloc(1, sizeof(struct spdk_bdev_io) + sizeof(struct raid_bdev_io));
1266 		SPDK_CU_ASSERT_FATAL(bdev_io != NULL);
1267 		io_len = (g_strip_size / 2) << i;
1268 		bdev_io_initialize(bdev_io, ch_b, &pbdev->bdev, lba, io_len, SPDK_BDEV_IO_TYPE_WRITE);
1269 		lba += g_strip_size;
1270 		memset(g_io_output, 0, ((g_max_io_size / g_strip_size) + 1) * sizeof(struct io_output));
1271 		g_io_output_index = 0;
1272 		raid_bdev_submit_request(ch, bdev_io);
1273 		verify_io(bdev_io, req.base_bdevs.num_base_bdevs, ch_ctx, pbdev,
1274 			  g_child_io_status_flag);
1275 		bdev_io_cleanup(bdev_io);
1276 	}
1277 
1278 	free_test_req(&req);
1279 	raid_bdev_destroy_cb(pbdev, ch_ctx);
1280 	CU_ASSERT(ch_ctx->base_channel == NULL);
1281 	free(ch);
1282 	free(ch_b);
1283 	create_raid_bdev_delete_req(&destroy_req, "raid1", 0);
1284 	rpc_bdev_raid_delete(NULL, NULL);
1285 	CU_ASSERT(g_rpc_err == 0);
1286 	verify_raid_bdev_present("raid1", false);
1287 
1288 	raid_bdev_exit();
1289 	base_bdevs_cleanup();
1290 	reset_globals();
1291 }
1292 
1293 static void
1294 test_read_io(void)
1295 {
1296 	struct rpc_bdev_raid_create req;
1297 	struct rpc_bdev_raid_delete destroy_req;
1298 	struct raid_bdev *pbdev;
1299 	struct spdk_io_channel *ch;
1300 	struct raid_bdev_io_channel *ch_ctx;
1301 	uint8_t i;
1302 	struct spdk_bdev_io *bdev_io;
1303 	uint64_t io_len;
1304 	uint64_t lba;
1305 	struct spdk_io_channel *ch_b;
1306 	struct spdk_bdev_channel *ch_b_ctx;
1307 
1308 	set_globals();
1309 	CU_ASSERT(raid_bdev_init() == 0);
1310 
1311 	verify_raid_bdev_present("raid1", false);
1312 	create_raid_bdev_create_req(&req, "raid1", 0, true, 0);
1313 	rpc_bdev_raid_create(NULL, NULL);
1314 	CU_ASSERT(g_rpc_err == 0);
1315 	verify_raid_bdev(&req, true, RAID_BDEV_STATE_ONLINE);
1316 	TAILQ_FOREACH(pbdev, &g_raid_bdev_list, global_link) {
1317 		if (strcmp(pbdev->bdev.name, "raid1") == 0) {
1318 			break;
1319 		}
1320 	}
1321 	CU_ASSERT(pbdev != NULL);
1322 	ch = calloc(1, sizeof(struct spdk_io_channel) + sizeof(struct raid_bdev_io_channel));
1323 	SPDK_CU_ASSERT_FATAL(ch != NULL);
1324 
1325 	ch_b = calloc(1, sizeof(struct spdk_io_channel) + sizeof(struct spdk_bdev_channel));
1326 	SPDK_CU_ASSERT_FATAL(ch_b != NULL);
1327 	ch_b_ctx = spdk_io_channel_get_ctx(ch_b);
1328 	ch_b_ctx->channel = ch;
1329 
1330 	ch_ctx = spdk_io_channel_get_ctx(ch);
1331 	SPDK_CU_ASSERT_FATAL(ch_ctx != NULL);
1332 
1333 	CU_ASSERT(raid_bdev_create_cb(pbdev, ch_ctx) == 0);
1334 	for (i = 0; i < req.base_bdevs.num_base_bdevs; i++) {
1335 		CU_ASSERT(ch_ctx->base_channel && ch_ctx->base_channel[i] == &g_io_channel);
1336 	}
1337 	free_test_req(&req);
1338 
1339 	/* test 2 IO sizes based on global strip size set earlier */
1340 	lba = 0;
1341 	for (i = 0; i < 2; i++) {
1342 		bdev_io = calloc(1, sizeof(struct spdk_bdev_io) + sizeof(struct raid_bdev_io));
1343 		SPDK_CU_ASSERT_FATAL(bdev_io != NULL);
1344 		io_len = (g_strip_size / 2) << i;
1345 		bdev_io_initialize(bdev_io, ch_b, &pbdev->bdev, lba, io_len, SPDK_BDEV_IO_TYPE_READ);
1346 		lba += g_strip_size;
1347 		memset(g_io_output, 0, ((g_max_io_size / g_strip_size) + 1) * sizeof(struct io_output));
1348 		g_io_output_index = 0;
1349 		raid_bdev_submit_request(ch, bdev_io);
1350 		verify_io(bdev_io, req.base_bdevs.num_base_bdevs, ch_ctx, pbdev,
1351 			  g_child_io_status_flag);
1352 		bdev_io_cleanup(bdev_io);
1353 	}
1354 
1355 	raid_bdev_destroy_cb(pbdev, ch_ctx);
1356 	CU_ASSERT(ch_ctx->base_channel == NULL);
1357 	free(ch);
1358 	free(ch_b);
1359 	create_raid_bdev_delete_req(&destroy_req, "raid1", 0);
1360 	rpc_bdev_raid_delete(NULL, NULL);
1361 	CU_ASSERT(g_rpc_err == 0);
1362 	verify_raid_bdev_present("raid1", false);
1363 
1364 	raid_bdev_exit();
1365 	base_bdevs_cleanup();
1366 	reset_globals();
1367 }
1368 
1369 static void
1370 raid_bdev_io_generate_by_strips(uint64_t n_strips)
1371 {
1372 	uint64_t lba;
1373 	uint64_t nblocks;
1374 	uint64_t start_offset;
1375 	uint64_t end_offset;
1376 	uint64_t offsets_in_strip[3];
1377 	uint64_t start_bdev_idx;
1378 	uint64_t start_bdev_offset;
1379 	uint64_t start_bdev_idxs[3];
1380 	int i, j, l;
1381 
1382 	/* 3 different situations of offset in strip */
1383 	offsets_in_strip[0] = 0;
1384 	offsets_in_strip[1] = g_strip_size >> 1;
1385 	offsets_in_strip[2] = g_strip_size - 1;
1386 
1387 	/* 3 different situations of start_bdev_idx */
1388 	start_bdev_idxs[0] = 0;
1389 	start_bdev_idxs[1] = g_max_base_drives >> 1;
1390 	start_bdev_idxs[2] = g_max_base_drives - 1;
1391 
1392 	/* consider different offset in strip */
1393 	for (i = 0; i < 3; i++) {
1394 		start_offset = offsets_in_strip[i];
1395 		for (j = 0; j < 3; j++) {
1396 			end_offset = offsets_in_strip[j];
1397 			if (n_strips == 1 && start_offset > end_offset) {
1398 				continue;
1399 			}
1400 
1401 			/* consider at which base_bdev lba is started. */
1402 			for (l = 0; l < 3; l++) {
1403 				start_bdev_idx = start_bdev_idxs[l];
1404 				start_bdev_offset = start_bdev_idx * g_strip_size;
1405 				lba = g_lba_offset + start_bdev_offset + start_offset;
1406 				nblocks = (n_strips - 1) * g_strip_size + end_offset - start_offset + 1;
1407 
1408 				g_io_ranges[g_io_range_idx].lba = lba;
1409 				g_io_ranges[g_io_range_idx].nblocks = nblocks;
1410 
1411 				SPDK_CU_ASSERT_FATAL(g_io_range_idx < MAX_TEST_IO_RANGE);
1412 				g_io_range_idx++;
1413 			}
1414 		}
1415 	}
1416 }
1417 
1418 static void
1419 raid_bdev_io_generate(void)
1420 {
1421 	uint64_t n_strips;
1422 	uint64_t n_strips_span = g_max_base_drives;
1423 	uint64_t n_strips_times[5] = {g_max_base_drives + 1, g_max_base_drives * 2 - 1,
1424 				      g_max_base_drives * 2, g_max_base_drives * 3,
1425 				      g_max_base_drives * 4
1426 				     };
1427 	uint32_t i;
1428 
1429 	g_io_range_idx = 0;
1430 
1431 	/* consider different number of strips from 1 to strips spanned base bdevs,
1432 	 * and even to times of strips spanned base bdevs
1433 	 */
1434 	for (n_strips = 1; n_strips < n_strips_span; n_strips++) {
1435 		raid_bdev_io_generate_by_strips(n_strips);
1436 	}
1437 
1438 	for (i = 0; i < SPDK_COUNTOF(n_strips_times); i++) {
1439 		n_strips = n_strips_times[i];
1440 		raid_bdev_io_generate_by_strips(n_strips);
1441 	}
1442 }
1443 
1444 static void
1445 test_unmap_io(void)
1446 {
1447 	struct rpc_bdev_raid_create req;
1448 	struct rpc_bdev_raid_delete destroy_req;
1449 	struct raid_bdev *pbdev;
1450 	struct spdk_io_channel *ch;
1451 	struct raid_bdev_io_channel *ch_ctx;
1452 	uint8_t i;
1453 	struct spdk_bdev_io *bdev_io;
1454 	uint32_t count;
1455 	uint64_t io_len;
1456 	uint64_t lba;
1457 
1458 	set_globals();
1459 	CU_ASSERT(raid_bdev_init() == 0);
1460 
1461 	verify_raid_bdev_present("raid1", false);
1462 	create_raid_bdev_create_req(&req, "raid1", 0, true, 0);
1463 	rpc_bdev_raid_create(NULL, NULL);
1464 	CU_ASSERT(g_rpc_err == 0);
1465 	verify_raid_bdev(&req, true, RAID_BDEV_STATE_ONLINE);
1466 	TAILQ_FOREACH(pbdev, &g_raid_bdev_list, global_link) {
1467 		if (strcmp(pbdev->bdev.name, "raid1") == 0) {
1468 			break;
1469 		}
1470 	}
1471 	CU_ASSERT(pbdev != NULL);
1472 	ch = calloc(1, sizeof(struct spdk_io_channel) + sizeof(struct raid_bdev_io_channel));
1473 	SPDK_CU_ASSERT_FATAL(ch != NULL);
1474 	ch_ctx = spdk_io_channel_get_ctx(ch);
1475 	SPDK_CU_ASSERT_FATAL(ch_ctx != NULL);
1476 
1477 	CU_ASSERT(raid_bdev_create_cb(pbdev, ch_ctx) == 0);
1478 	for (i = 0; i < req.base_bdevs.num_base_bdevs; i++) {
1479 		SPDK_CU_ASSERT_FATAL(ch_ctx->base_channel && ch_ctx->base_channel[i] == &g_io_channel);
1480 	}
1481 
1482 	CU_ASSERT(raid_bdev_io_type_supported(pbdev, SPDK_BDEV_IO_TYPE_UNMAP) == true);
1483 	CU_ASSERT(raid_bdev_io_type_supported(pbdev, SPDK_BDEV_IO_TYPE_FLUSH) == true);
1484 
1485 	raid_bdev_io_generate();
1486 	for (count = 0; count < g_io_range_idx; count++) {
1487 		bdev_io = calloc(1, sizeof(struct spdk_bdev_io) + sizeof(struct raid_bdev_io));
1488 		SPDK_CU_ASSERT_FATAL(bdev_io != NULL);
1489 		io_len = g_io_ranges[count].nblocks;
1490 		lba = g_io_ranges[count].lba;
1491 		bdev_io_initialize(bdev_io, ch, &pbdev->bdev, lba, io_len, SPDK_BDEV_IO_TYPE_UNMAP);
1492 		memset(g_io_output, 0, g_max_base_drives * sizeof(struct io_output));
1493 		g_io_output_index = 0;
1494 		raid_bdev_submit_request(ch, bdev_io);
1495 		verify_io_without_payload(bdev_io, req.base_bdevs.num_base_bdevs, ch_ctx, pbdev,
1496 					  g_child_io_status_flag);
1497 		bdev_io_cleanup(bdev_io);
1498 	}
1499 	free_test_req(&req);
1500 
1501 	raid_bdev_destroy_cb(pbdev, ch_ctx);
1502 	CU_ASSERT(ch_ctx->base_channel == NULL);
1503 	free(ch);
1504 	create_raid_bdev_delete_req(&destroy_req, "raid1", 0);
1505 	rpc_bdev_raid_delete(NULL, NULL);
1506 	CU_ASSERT(g_rpc_err == 0);
1507 	verify_raid_bdev_present("raid1", false);
1508 
1509 	raid_bdev_exit();
1510 	base_bdevs_cleanup();
1511 	reset_globals();
1512 }
1513 
1514 /* Test IO failures */
1515 static void
1516 test_io_failure(void)
1517 {
1518 	struct rpc_bdev_raid_create req;
1519 	struct rpc_bdev_raid_delete destroy_req;
1520 	struct raid_bdev *pbdev;
1521 	struct spdk_io_channel *ch;
1522 	struct raid_bdev_io_channel *ch_ctx;
1523 	uint8_t i;
1524 	struct spdk_bdev_io *bdev_io;
1525 	uint32_t count;
1526 	uint64_t io_len;
1527 	uint64_t lba;
1528 
1529 	set_globals();
1530 	CU_ASSERT(raid_bdev_init() == 0);
1531 
1532 	verify_raid_bdev_present("raid1", false);
1533 	create_raid_bdev_create_req(&req, "raid1", 0, true, 0);
1534 	rpc_bdev_raid_create(NULL, NULL);
1535 	CU_ASSERT(g_rpc_err == 0);
1536 	verify_raid_bdev(&req, true, RAID_BDEV_STATE_ONLINE);
1537 	TAILQ_FOREACH(pbdev, &g_raid_bdev_list, global_link) {
1538 		if (strcmp(pbdev->bdev.name, req.name) == 0) {
1539 			break;
1540 		}
1541 	}
1542 	CU_ASSERT(pbdev != NULL);
1543 	ch = calloc(1, sizeof(struct spdk_io_channel) + sizeof(struct raid_bdev_io_channel));
1544 	SPDK_CU_ASSERT_FATAL(ch != NULL);
1545 	ch_ctx = spdk_io_channel_get_ctx(ch);
1546 	SPDK_CU_ASSERT_FATAL(ch_ctx != NULL);
1547 
1548 	CU_ASSERT(raid_bdev_create_cb(pbdev, ch_ctx) == 0);
1549 	for (i = 0; i < req.base_bdevs.num_base_bdevs; i++) {
1550 		CU_ASSERT(ch_ctx->base_channel && ch_ctx->base_channel[i] == &g_io_channel);
1551 	}
1552 	free_test_req(&req);
1553 
1554 	lba = 0;
1555 	for (count = 0; count < 1; count++) {
1556 		bdev_io = calloc(1, sizeof(struct spdk_bdev_io) + sizeof(struct raid_bdev_io));
1557 		SPDK_CU_ASSERT_FATAL(bdev_io != NULL);
1558 		io_len = (g_strip_size / 2) << count;
1559 		bdev_io_initialize(bdev_io, ch, &pbdev->bdev, lba, io_len, SPDK_BDEV_IO_TYPE_INVALID);
1560 		lba += g_strip_size;
1561 		memset(g_io_output, 0, ((g_max_io_size / g_strip_size) + 1) * sizeof(struct io_output));
1562 		g_io_output_index = 0;
1563 		raid_bdev_submit_request(ch, bdev_io);
1564 		verify_io(bdev_io, req.base_bdevs.num_base_bdevs, ch_ctx, pbdev,
1565 			  INVALID_IO_SUBMIT);
1566 		bdev_io_cleanup(bdev_io);
1567 	}
1568 
1569 
1570 	lba = 0;
1571 	g_child_io_status_flag = false;
1572 	for (count = 0; count < 1; count++) {
1573 		bdev_io = calloc(1, sizeof(struct spdk_bdev_io) + sizeof(struct raid_bdev_io));
1574 		SPDK_CU_ASSERT_FATAL(bdev_io != NULL);
1575 		io_len = (g_strip_size / 2) << count;
1576 		bdev_io_initialize(bdev_io, ch, &pbdev->bdev, lba, io_len, SPDK_BDEV_IO_TYPE_WRITE);
1577 		lba += g_strip_size;
1578 		memset(g_io_output, 0, ((g_max_io_size / g_strip_size) + 1) * sizeof(struct io_output));
1579 		g_io_output_index = 0;
1580 		raid_bdev_submit_request(ch, bdev_io);
1581 		verify_io(bdev_io, req.base_bdevs.num_base_bdevs, ch_ctx, pbdev,
1582 			  g_child_io_status_flag);
1583 		bdev_io_cleanup(bdev_io);
1584 	}
1585 
1586 	raid_bdev_destroy_cb(pbdev, ch_ctx);
1587 	CU_ASSERT(ch_ctx->base_channel == NULL);
1588 	free(ch);
1589 	create_raid_bdev_delete_req(&destroy_req, "raid1", 0);
1590 	rpc_bdev_raid_delete(NULL, NULL);
1591 	CU_ASSERT(g_rpc_err == 0);
1592 	verify_raid_bdev_present("raid1", false);
1593 
1594 	raid_bdev_exit();
1595 	base_bdevs_cleanup();
1596 	reset_globals();
1597 }
1598 
1599 /* Test reset IO */
1600 static void
1601 test_reset_io(void)
1602 {
1603 	struct rpc_bdev_raid_create req;
1604 	struct rpc_bdev_raid_delete destroy_req;
1605 	struct raid_bdev *pbdev;
1606 	struct spdk_io_channel *ch;
1607 	struct raid_bdev_io_channel *ch_ctx;
1608 	uint8_t i;
1609 	struct spdk_bdev_io *bdev_io;
1610 
1611 	set_globals();
1612 	CU_ASSERT(raid_bdev_init() == 0);
1613 
1614 	verify_raid_bdev_present("raid1", false);
1615 	create_raid_bdev_create_req(&req, "raid1", 0, true, 0);
1616 	rpc_bdev_raid_create(NULL, NULL);
1617 	CU_ASSERT(g_rpc_err == 0);
1618 	verify_raid_bdev(&req, true, RAID_BDEV_STATE_ONLINE);
1619 	TAILQ_FOREACH(pbdev, &g_raid_bdev_list, global_link) {
1620 		if (strcmp(pbdev->bdev.name, "raid1") == 0) {
1621 			break;
1622 		}
1623 	}
1624 	CU_ASSERT(pbdev != NULL);
1625 	ch = calloc(1, sizeof(struct spdk_io_channel) + sizeof(struct raid_bdev_io_channel));
1626 	SPDK_CU_ASSERT_FATAL(ch != NULL);
1627 	ch_ctx = spdk_io_channel_get_ctx(ch);
1628 	SPDK_CU_ASSERT_FATAL(ch_ctx != NULL);
1629 
1630 	SPDK_CU_ASSERT_FATAL(raid_bdev_create_cb(pbdev, ch_ctx) == 0);
1631 	for (i = 0; i < req.base_bdevs.num_base_bdevs; i++) {
1632 		CU_ASSERT(ch_ctx->base_channel && ch_ctx->base_channel[i] == &g_io_channel);
1633 	}
1634 	free_test_req(&req);
1635 
1636 	g_bdev_io_submit_status = 0;
1637 	g_child_io_status_flag = true;
1638 
1639 	CU_ASSERT(raid_bdev_io_type_supported(pbdev, SPDK_BDEV_IO_TYPE_RESET) == true);
1640 
1641 	bdev_io = calloc(1, sizeof(struct spdk_bdev_io) + sizeof(struct raid_bdev_io));
1642 	SPDK_CU_ASSERT_FATAL(bdev_io != NULL);
1643 	bdev_io_initialize(bdev_io, ch, &pbdev->bdev, 0, 1, SPDK_BDEV_IO_TYPE_RESET);
1644 	memset(g_io_output, 0, g_max_base_drives * sizeof(struct io_output));
1645 	g_io_output_index = 0;
1646 	raid_bdev_submit_request(ch, bdev_io);
1647 	verify_reset_io(bdev_io, req.base_bdevs.num_base_bdevs, ch_ctx, pbdev,
1648 			true);
1649 	bdev_io_cleanup(bdev_io);
1650 
1651 	raid_bdev_destroy_cb(pbdev, ch_ctx);
1652 	CU_ASSERT(ch_ctx->base_channel == NULL);
1653 	free(ch);
1654 	create_raid_bdev_delete_req(&destroy_req, "raid1", 0);
1655 	rpc_bdev_raid_delete(NULL, NULL);
1656 	CU_ASSERT(g_rpc_err == 0);
1657 	verify_raid_bdev_present("raid1", false);
1658 
1659 	raid_bdev_exit();
1660 	base_bdevs_cleanup();
1661 	reset_globals();
1662 }
1663 
1664 /* Create multiple raids, destroy raids without IO, get_raids related tests */
1665 static void
1666 test_multi_raid_no_io(void)
1667 {
1668 	struct rpc_bdev_raid_create *construct_req;
1669 	struct rpc_bdev_raid_delete destroy_req;
1670 	struct rpc_bdev_raid_get_bdevs get_raids_req;
1671 	uint8_t i;
1672 	char name[16];
1673 	uint8_t bbdev_idx = 0;
1674 
1675 	set_globals();
1676 	construct_req = calloc(MAX_RAIDS, sizeof(struct rpc_bdev_raid_create));
1677 	SPDK_CU_ASSERT_FATAL(construct_req != NULL);
1678 	CU_ASSERT(raid_bdev_init() == 0);
1679 	for (i = 0; i < g_max_raids; i++) {
1680 		snprintf(name, 16, "%s%u", "raid", i);
1681 		verify_raid_bdev_present(name, false);
1682 		create_raid_bdev_create_req(&construct_req[i], name, bbdev_idx, true, 0);
1683 		bbdev_idx += g_max_base_drives;
1684 		rpc_bdev_raid_create(NULL, NULL);
1685 		CU_ASSERT(g_rpc_err == 0);
1686 		verify_raid_bdev(&construct_req[i], true, RAID_BDEV_STATE_ONLINE);
1687 	}
1688 
1689 	create_get_raids_req(&get_raids_req, "all", 0);
1690 	rpc_bdev_raid_get_bdevs(NULL, NULL);
1691 	CU_ASSERT(g_rpc_err == 0);
1692 	verify_get_raids(construct_req, g_max_raids, g_get_raids_output, g_get_raids_count);
1693 	for (i = 0; i < g_get_raids_count; i++) {
1694 		free(g_get_raids_output[i]);
1695 	}
1696 
1697 	create_get_raids_req(&get_raids_req, "online", 0);
1698 	rpc_bdev_raid_get_bdevs(NULL, NULL);
1699 	CU_ASSERT(g_rpc_err == 0);
1700 	verify_get_raids(construct_req, g_max_raids, g_get_raids_output, g_get_raids_count);
1701 	for (i = 0; i < g_get_raids_count; i++) {
1702 		free(g_get_raids_output[i]);
1703 	}
1704 
1705 	create_get_raids_req(&get_raids_req, "configuring", 0);
1706 	rpc_bdev_raid_get_bdevs(NULL, NULL);
1707 	CU_ASSERT(g_rpc_err == 0);
1708 	CU_ASSERT(g_get_raids_count == 0);
1709 
1710 	create_get_raids_req(&get_raids_req, "offline", 0);
1711 	rpc_bdev_raid_get_bdevs(NULL, NULL);
1712 	CU_ASSERT(g_rpc_err == 0);
1713 	CU_ASSERT(g_get_raids_count == 0);
1714 
1715 	create_get_raids_req(&get_raids_req, "invalid_category", 0);
1716 	rpc_bdev_raid_get_bdevs(NULL, NULL);
1717 	CU_ASSERT(g_rpc_err == 1);
1718 	CU_ASSERT(g_get_raids_count == 0);
1719 
1720 	create_get_raids_req(&get_raids_req, "all", 1);
1721 	rpc_bdev_raid_get_bdevs(NULL, NULL);
1722 	CU_ASSERT(g_rpc_err == 1);
1723 	free(get_raids_req.category);
1724 	CU_ASSERT(g_get_raids_count == 0);
1725 
1726 	create_get_raids_req(&get_raids_req, "all", 0);
1727 	rpc_bdev_raid_get_bdevs(NULL, NULL);
1728 	CU_ASSERT(g_rpc_err == 0);
1729 	CU_ASSERT(g_get_raids_count == g_max_raids);
1730 	for (i = 0; i < g_get_raids_count; i++) {
1731 		free(g_get_raids_output[i]);
1732 	}
1733 
1734 	for (i = 0; i < g_max_raids; i++) {
1735 		SPDK_CU_ASSERT_FATAL(construct_req[i].name != NULL);
1736 		snprintf(name, 16, "%s", construct_req[i].name);
1737 		create_raid_bdev_delete_req(&destroy_req, name, 0);
1738 		rpc_bdev_raid_delete(NULL, NULL);
1739 		CU_ASSERT(g_rpc_err == 0);
1740 		verify_raid_bdev_present(name, false);
1741 	}
1742 	raid_bdev_exit();
1743 	for (i = 0; i < g_max_raids; i++) {
1744 		free_test_req(&construct_req[i]);
1745 	}
1746 	free(construct_req);
1747 	base_bdevs_cleanup();
1748 	reset_globals();
1749 }
1750 
1751 /* Create multiple raids, fire IOs on raids */
1752 static void
1753 test_multi_raid_with_io(void)
1754 {
1755 	struct rpc_bdev_raid_create *construct_req;
1756 	struct rpc_bdev_raid_delete destroy_req;
1757 	uint8_t i, j;
1758 	char name[16];
1759 	uint8_t bbdev_idx = 0;
1760 	struct raid_bdev *pbdev;
1761 	struct spdk_io_channel *ch;
1762 	struct raid_bdev_io_channel *ch_ctx = NULL;
1763 	struct spdk_bdev_io *bdev_io;
1764 	uint64_t io_len;
1765 	uint64_t lba = 0;
1766 	int16_t iotype;
1767 	struct spdk_io_channel *ch_b;
1768 	struct spdk_bdev_channel *ch_b_ctx;
1769 
1770 	set_globals();
1771 	construct_req = calloc(g_max_raids, sizeof(struct rpc_bdev_raid_create));
1772 	SPDK_CU_ASSERT_FATAL(construct_req != NULL);
1773 	CU_ASSERT(raid_bdev_init() == 0);
1774 	ch = calloc(g_max_raids, sizeof(struct spdk_io_channel) + sizeof(struct raid_bdev_io_channel));
1775 	SPDK_CU_ASSERT_FATAL(ch != NULL);
1776 
1777 	ch_b = calloc(1, sizeof(struct spdk_io_channel) + sizeof(struct spdk_bdev_channel));
1778 	SPDK_CU_ASSERT_FATAL(ch_b != NULL);
1779 	ch_b_ctx = spdk_io_channel_get_ctx(ch_b);
1780 	ch_b_ctx->channel = ch;
1781 
1782 	for (i = 0; i < g_max_raids; i++) {
1783 		snprintf(name, 16, "%s%u", "raid", i);
1784 		verify_raid_bdev_present(name, false);
1785 		create_raid_bdev_create_req(&construct_req[i], name, bbdev_idx, true, 0);
1786 		bbdev_idx += g_max_base_drives;
1787 		rpc_bdev_raid_create(NULL, NULL);
1788 		CU_ASSERT(g_rpc_err == 0);
1789 		verify_raid_bdev(&construct_req[i], true, RAID_BDEV_STATE_ONLINE);
1790 		TAILQ_FOREACH(pbdev, &g_raid_bdev_list, global_link) {
1791 			if (strcmp(pbdev->bdev.name, construct_req[i].name) == 0) {
1792 				break;
1793 			}
1794 		}
1795 		CU_ASSERT(pbdev != NULL);
1796 		ch_ctx = spdk_io_channel_get_ctx(&ch[i]);
1797 		SPDK_CU_ASSERT_FATAL(ch_ctx != NULL);
1798 		CU_ASSERT(raid_bdev_create_cb(pbdev, ch_ctx) == 0);
1799 		SPDK_CU_ASSERT_FATAL(ch_ctx->base_channel != NULL);
1800 		for (j = 0; j < construct_req[i].base_bdevs.num_base_bdevs; j++) {
1801 			CU_ASSERT(ch_ctx->base_channel[j] == &g_io_channel);
1802 		}
1803 	}
1804 
1805 	/* This will perform a write on the first raid and a read on the second. It can be
1806 	 * expanded in the future to perform r/w on each raid device in the event that
1807 	 * multiple raid levels are supported.
1808 	 */
1809 	for (i = 0; i < g_max_raids; i++) {
1810 		bdev_io = calloc(1, sizeof(struct spdk_bdev_io) + sizeof(struct raid_bdev_io));
1811 		SPDK_CU_ASSERT_FATAL(bdev_io != NULL);
1812 		io_len = g_strip_size;
1813 		iotype = (i) ? SPDK_BDEV_IO_TYPE_WRITE : SPDK_BDEV_IO_TYPE_READ;
1814 		memset(g_io_output, 0, ((g_max_io_size / g_strip_size) + 1) * sizeof(struct io_output));
1815 		g_io_output_index = 0;
1816 		TAILQ_FOREACH(pbdev, &g_raid_bdev_list, global_link) {
1817 			if (strcmp(pbdev->bdev.name, construct_req[i].name) == 0) {
1818 				break;
1819 			}
1820 		}
1821 		bdev_io_initialize(bdev_io, ch_b, &pbdev->bdev, lba, io_len, iotype);
1822 		CU_ASSERT(pbdev != NULL);
1823 		raid_bdev_submit_request(ch, bdev_io);
1824 		verify_io(bdev_io, g_max_base_drives, ch_ctx, pbdev,
1825 			  g_child_io_status_flag);
1826 		bdev_io_cleanup(bdev_io);
1827 	}
1828 
1829 	for (i = 0; i < g_max_raids; i++) {
1830 		TAILQ_FOREACH(pbdev, &g_raid_bdev_list, global_link) {
1831 			if (strcmp(pbdev->bdev.name, construct_req[i].name) == 0) {
1832 				break;
1833 			}
1834 		}
1835 		CU_ASSERT(pbdev != NULL);
1836 		ch_ctx = spdk_io_channel_get_ctx(&ch[i]);
1837 		SPDK_CU_ASSERT_FATAL(ch_ctx != NULL);
1838 		raid_bdev_destroy_cb(pbdev, ch_ctx);
1839 		CU_ASSERT(ch_ctx->base_channel == NULL);
1840 		snprintf(name, 16, "%s", construct_req[i].name);
1841 		create_raid_bdev_delete_req(&destroy_req, name, 0);
1842 		rpc_bdev_raid_delete(NULL, NULL);
1843 		CU_ASSERT(g_rpc_err == 0);
1844 		verify_raid_bdev_present(name, false);
1845 	}
1846 	raid_bdev_exit();
1847 	for (i = 0; i < g_max_raids; i++) {
1848 		free_test_req(&construct_req[i]);
1849 	}
1850 	free(construct_req);
1851 	free(ch);
1852 	free(ch_b);
1853 	base_bdevs_cleanup();
1854 	reset_globals();
1855 }
1856 
1857 static void
1858 test_io_type_supported(void)
1859 {
1860 	CU_ASSERT(raid_bdev_io_type_supported(NULL, SPDK_BDEV_IO_TYPE_READ) == true);
1861 	CU_ASSERT(raid_bdev_io_type_supported(NULL, SPDK_BDEV_IO_TYPE_WRITE) == true);
1862 	CU_ASSERT(raid_bdev_io_type_supported(NULL, SPDK_BDEV_IO_TYPE_INVALID) == false);
1863 }
1864 
1865 static void
1866 test_raid_json_dump_info(void)
1867 {
1868 	struct rpc_bdev_raid_create req;
1869 	struct rpc_bdev_raid_delete destroy_req;
1870 	struct raid_bdev *pbdev;
1871 
1872 	set_globals();
1873 	CU_ASSERT(raid_bdev_init() == 0);
1874 
1875 	verify_raid_bdev_present("raid1", false);
1876 	create_raid_bdev_create_req(&req, "raid1", 0, true, 0);
1877 	rpc_bdev_raid_create(NULL, NULL);
1878 	CU_ASSERT(g_rpc_err == 0);
1879 	verify_raid_bdev(&req, true, RAID_BDEV_STATE_ONLINE);
1880 
1881 	TAILQ_FOREACH(pbdev, &g_raid_bdev_list, global_link) {
1882 		if (strcmp(pbdev->bdev.name, "raid1") == 0) {
1883 			break;
1884 		}
1885 	}
1886 	CU_ASSERT(pbdev != NULL);
1887 
1888 	CU_ASSERT(raid_bdev_dump_info_json(pbdev, NULL) == 0);
1889 
1890 	free_test_req(&req);
1891 
1892 	create_raid_bdev_delete_req(&destroy_req, "raid1", 0);
1893 	rpc_bdev_raid_delete(NULL, NULL);
1894 	CU_ASSERT(g_rpc_err == 0);
1895 	verify_raid_bdev_present("raid1", false);
1896 
1897 	raid_bdev_exit();
1898 	base_bdevs_cleanup();
1899 	reset_globals();
1900 }
1901 
1902 static void
1903 test_context_size(void)
1904 {
1905 	CU_ASSERT(raid_bdev_get_ctx_size() == sizeof(struct raid_bdev_io));
1906 }
1907 
1908 static void
1909 test_raid_level_conversions(void)
1910 {
1911 	const char *raid_str;
1912 
1913 	CU_ASSERT(raid_bdev_str_to_level("abcd123") == INVALID_RAID_LEVEL);
1914 	CU_ASSERT(raid_bdev_str_to_level("0") == RAID0);
1915 	CU_ASSERT(raid_bdev_str_to_level("raid0") == RAID0);
1916 	CU_ASSERT(raid_bdev_str_to_level("RAID0") == RAID0);
1917 
1918 	raid_str = raid_bdev_level_to_str(INVALID_RAID_LEVEL);
1919 	CU_ASSERT(raid_str != NULL && strlen(raid_str) == 0);
1920 	raid_str = raid_bdev_level_to_str(1234);
1921 	CU_ASSERT(raid_str != NULL && strlen(raid_str) == 0);
1922 	raid_str = raid_bdev_level_to_str(RAID0);
1923 	CU_ASSERT(raid_str != NULL && strcmp(raid_str, "raid0") == 0);
1924 }
1925 
1926 int
1927 main(int argc, char **argv)
1928 {
1929 	CU_pSuite       suite = NULL;
1930 	unsigned int    num_failures;
1931 
1932 	CU_set_error_action(CUEA_ABORT);
1933 	CU_initialize_registry();
1934 
1935 	suite = CU_add_suite("raid", NULL, NULL);
1936 
1937 	CU_ADD_TEST(suite, test_create_raid);
1938 	CU_ADD_TEST(suite, test_delete_raid);
1939 	CU_ADD_TEST(suite, test_create_raid_invalid_args);
1940 	CU_ADD_TEST(suite, test_delete_raid_invalid_args);
1941 	CU_ADD_TEST(suite, test_io_channel);
1942 	CU_ADD_TEST(suite, test_reset_io);
1943 	CU_ADD_TEST(suite, test_write_io);
1944 	CU_ADD_TEST(suite, test_read_io);
1945 	CU_ADD_TEST(suite, test_unmap_io);
1946 	CU_ADD_TEST(suite, test_io_failure);
1947 	CU_ADD_TEST(suite, test_multi_raid_no_io);
1948 	CU_ADD_TEST(suite, test_multi_raid_with_io);
1949 	CU_ADD_TEST(suite, test_io_type_supported);
1950 	CU_ADD_TEST(suite, test_raid_json_dump_info);
1951 	CU_ADD_TEST(suite, test_context_size);
1952 	CU_ADD_TEST(suite, test_raid_level_conversions);
1953 
1954 	allocate_threads(1);
1955 	set_thread(0);
1956 
1957 	CU_basic_set_mode(CU_BRM_VERBOSE);
1958 	set_test_opts();
1959 	CU_basic_run_tests();
1960 	num_failures = CU_get_number_of_failures();
1961 	CU_cleanup_registry();
1962 
1963 	free_threads();
1964 
1965 	return num_failures;
1966 }
1967