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