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