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