xref: /spdk/test/bdev/blockdev.sh (revision 1078198e78653b2f39414c1566740018d76ee73d)
1#!/usr/bin/env bash
2#  SPDX-License-Identifier: BSD-3-Clause
3#  Copyright (C) 2016 Intel Corporation
4#  Copyright (c) 2022, 2023 NVIDIA CORPORATION & AFFILIATES.
5#  All rights reserved.
6#
7testdir=$(readlink -f $(dirname $0))
8rootdir=$(readlink -f $testdir/../..)
9source $rootdir/test/common/autotest_common.sh
10source $testdir/nbd_common.sh
11
12rpc_py=rpc_cmd
13conf_file="$testdir/bdev.json"
14nonenclosed_conf_file="$testdir/nonenclosed.json"
15nonarray_conf_file="$testdir/nonarray.json"
16
17# Make sure the configuration is clean
18: > "$conf_file"
19
20function cleanup() {
21	rm -f "$SPDK_TEST_STORAGE/aiofile"
22	rm -f "$conf_file"
23
24	if [[ $test_type == rbd ]]; then
25		rbd_cleanup
26	fi
27
28	if [[ $test_type == daos ]]; then
29		daos_cleanup
30	fi
31
32	if [[ "$test_type" = "gpt" ]]; then
33		"$rootdir/scripts/setup.sh" reset
34		if [[ -b $gpt_nvme ]]; then
35			wipefs --all "$gpt_nvme"
36		fi
37	fi
38	if [[ $test_type == xnvme ]]; then
39		"$rootdir/scripts/setup.sh"
40	fi
41}
42
43function start_spdk_tgt() {
44	"$SPDK_BIN_DIR/spdk_tgt" "$env_ctx" "$wait_for_rpc" &
45	spdk_tgt_pid=$!
46	trap 'killprocess "$spdk_tgt_pid"; exit 1' SIGINT SIGTERM EXIT
47	waitforlisten "$spdk_tgt_pid"
48}
49
50function setup_bdev_conf() {
51	"$rpc_py" <<- RPC
52		iobuf_set_options --small-pool-count 10000 --large-pool-count 1100
53		framework_start_init
54		bdev_split_create Malloc1 2
55		bdev_split_create -s 4 Malloc2 8
56		bdev_malloc_create -b Malloc0 32 512
57		bdev_malloc_create -b Malloc1 32 512
58		bdev_malloc_create -b Malloc2 32 512
59		bdev_malloc_create -b Malloc3 32 512
60		bdev_malloc_create -b Malloc4 32 512
61		bdev_malloc_create -b Malloc5 32 512
62		bdev_malloc_create -b Malloc6 32 512
63		bdev_malloc_create -b Malloc7 32 512
64		bdev_malloc_create -b Malloc8 32 512
65		bdev_malloc_create -b Malloc9 32 512
66		bdev_passthru_create -p TestPT -b Malloc3
67		bdev_raid_create -n raid0 -z 64 -r 0 -b "Malloc4 Malloc5"
68		bdev_raid_create -n concat0 -z 64 -r concat -b "Malloc6 Malloc7"
69		bdev_raid_create -n raid1 -r 1 -b "Malloc8 Malloc9"
70		bdev_set_qos_limit --rw_mbytes_per_sec 100 Malloc3
71		bdev_set_qos_limit --rw_ios_per_sec 20000 Malloc0
72	RPC
73
74	dd if=/dev/zero of="$SPDK_TEST_STORAGE/aiofile" bs=2048 count=5000
75	"$rpc_py" bdev_aio_create "$SPDK_TEST_STORAGE/aiofile" AIO0 2048
76}
77
78function setup_nvme_conf() {
79	local json
80	mapfile -t json < <("$rootdir/scripts/gen_nvme.sh")
81	"$rpc_py" load_subsystem_config -j "'${json[*]}'"
82}
83
84function setup_xnvme_conf() {
85	# TODO: Switch to io_uring_cmd when proper CI support is in place
86	local io_mechanism=io_uring
87	local nvme nvmes
88
89	"$rootdir/scripts/setup.sh" reset
90	get_zoned_devs
91
92	for nvme in /dev/nvme*n*; do
93		[[ -b $nvme && -z ${zoned_devs["${nvme##*/}"]} ]] || continue
94		nvmes+=("bdev_xnvme_create $nvme ${nvme##*/} $io_mechanism")
95	done
96
97	((${#nvmes[@]} > 0))
98	"$rpc_py" < <(printf '%s\n' "${nvmes[@]}")
99}
100
101function setup_gpt_conf() {
102	$rootdir/scripts/setup.sh reset
103	get_zoned_devs
104	# Get nvme devices by following drivers' links towards nvme class
105	local nvme_devs=(/sys/bus/pci/drivers/nvme/*/nvme/nvme*/nvme*n*) nvme_dev
106	gpt_nvme=""
107	# Pick first device which doesn't have any valid partition table
108	for nvme_dev in "${nvme_devs[@]}"; do
109		[[ -z ${zoned_devs["${nvme_dev##*/}"]} ]] || continue
110		dev=/dev/${nvme_dev##*/}
111		if ! pt=$(parted "$dev" -ms print 2>&1); then
112			[[ $pt == *"$dev: unrecognised disk label"* ]] || continue
113			gpt_nvme=$dev
114			break
115		fi
116	done
117	if [[ -n $gpt_nvme ]]; then
118		# These Unique Partition GUIDs were randomly generated for testing and are distinct
119		# from the Partition Type GUIDs (SPDK_GPT_OLD_GUID and SPDK_GPT_GUID) which have
120		# special meaning to SPDK. See section 5.3.3 of UEFI Spec 2.3 for the distinction
121		# between Unique Partition GUID and Partition Type GUID.
122		typeset -g g_unique_partguid=6f89f330-603b-4116-ac73-2ca8eae53030
123		typeset -g g_unique_partguid_old=abf1734f-66e5-4c0f-aa29-4021d4d307df
124
125		# Create gpt partition table
126		parted -s "$gpt_nvme" mklabel gpt mkpart SPDK_TEST_first '0%' '50%' mkpart SPDK_TEST_second '50%' '100%'
127		# Change the partition type GUIDs to SPDK partition type values
128		SPDK_GPT_OLD_GUID=$(get_spdk_gpt_old)
129		SPDK_GPT_GUID=$(get_spdk_gpt)
130		sgdisk -t "1:$SPDK_GPT_GUID" -u "1:$g_unique_partguid" "$gpt_nvme"
131		sgdisk -t "2:$SPDK_GPT_OLD_GUID" -u "2:$g_unique_partguid_old" "$gpt_nvme"
132		"$rootdir/scripts/setup.sh"
133		"$rpc_py" bdev_get_bdevs
134		setup_nvme_conf
135	else
136		printf 'Did not find any nvme block devices to work with, aborting the test\n' >&2
137		"$rootdir/scripts/setup.sh"
138		return 1
139	fi
140}
141
142function setup_crypto_aesni_conf() {
143	# Malloc0 and Malloc1 use AESNI
144	"$rpc_py" <<- RPC
145		dpdk_cryptodev_scan_accel_module
146		dpdk_cryptodev_set_driver -d crypto_aesni_mb
147		accel_assign_opc -o encrypt -m dpdk_cryptodev
148		accel_assign_opc -o decrypt -m dpdk_cryptodev
149		framework_start_init
150		accel_crypto_key_create -c AES_CBC -k 01234567891234560123456789123456 -n test_dek_aesni_cbc_1
151		accel_crypto_key_create -c AES_CBC -k 12345678912345601234567891234560 -n test_dek_aesni_cbc_2
152		accel_crypto_key_create -c AES_CBC -k 23456789123456012345678912345601 -n test_dek_aesni_cbc_3
153		accel_crypto_key_create -c AES_CBC -k 34567891234560123456789123456012 -n test_dek_aesni_cbc_4
154		bdev_malloc_create -b Malloc0 32 512
155		bdev_malloc_create -b Malloc1 32 512
156		bdev_malloc_create -b Malloc2 32 4096
157		bdev_malloc_create -b Malloc3 32 4096
158		bdev_crypto_create Malloc0 crypto_ram -n test_dek_aesni_cbc_1
159		bdev_crypto_create Malloc1 crypto_ram2 -n test_dek_aesni_cbc_2
160		bdev_crypto_create Malloc2 crypto_ram3 -n test_dek_aesni_cbc_3
161		bdev_crypto_create Malloc3 crypto_ram4 -n test_dek_aesni_cbc_4
162	RPC
163}
164
165function setup_crypto_qat_conf() {
166	# Malloc0 will use QAT AES_CBC
167	# Malloc1 will use QAT AES_XTS
168	"$rpc_py" <<- RPC
169		dpdk_cryptodev_scan_accel_module
170		dpdk_cryptodev_set_driver -d crypto_qat
171		accel_assign_opc -o encrypt -m dpdk_cryptodev
172		accel_assign_opc -o decrypt -m dpdk_cryptodev
173		framework_start_init
174		accel_crypto_key_create -c AES_CBC -k 01234567891234560123456789123456 -n test_dek_qat_cbc
175		accel_crypto_key_create -c AES_XTS -k 00112233445566778899001122334455 -e 12345678912345601234567891234560 -n test_dek_qat_xts
176		accel_crypto_key_create -c AES_CBC -k 23456789123456012345678912345601 -n test_dek_qat_cbc2
177		accel_crypto_key_create -c AES_XTS -k 22334455667788990011223344550011 -e 34567891234560123456789123456012 -n test_dek_qat_xts2
178		bdev_malloc_create -b Malloc0 32 512
179		bdev_malloc_create -b Malloc1 32 512
180		bdev_malloc_create -b Malloc2 32 4096
181		bdev_malloc_create -b Malloc3 32 4096
182		bdev_crypto_create Malloc0 crypto_ram -n test_dek_qat_cbc
183		bdev_crypto_create Malloc1 crypto_ram1 -n test_dek_qat_xts
184		bdev_crypto_create Malloc2 crypto_ram2 -n test_dek_qat_cbc2
185		bdev_crypto_create Malloc3 crypto_ram3 -n test_dek_qat_xts2
186		bdev_get_bdevs -b Malloc1
187	RPC
188}
189
190function setup_crypto_sw_conf() {
191	"$rpc_py" <<- RPC
192		framework_start_init
193		bdev_malloc_create -b Malloc0 16 512
194		bdev_malloc_create -b Malloc1 16 4096
195		accel_crypto_key_create -c AES_XTS -k 00112233445566778899001122334455 -e 11223344556677889900112233445500 -n test_dek_sw
196		accel_crypto_key_create -c AES_XTS -k 22334455667788990011223344550011 -e 33445566778899001122334455001122 -n test_dek_sw2
197		accel_crypto_key_create -c AES_XTS -k 33445566778899001122334455001122 -e 44556677889900112233445500112233 -n test_dek_sw3
198		bdev_crypto_create Malloc0 crypto_ram -n test_dek_sw
199		bdev_crypto_create Malloc1 crypto_ram2 -n test_dek_sw2
200		bdev_crypto_create crypto_ram2 crypto_ram3 -n test_dek_sw3
201		bdev_get_bdevs -b Malloc1
202	RPC
203}
204
205function setup_crypto_accel_mlx5_conf() {
206	"$rpc_py" <<- RPC
207		mlx5_scan_accel_module
208		accel_assign_opc -o encrypt -m mlx5
209		accel_assign_opc -o decrypt -m mlx5
210		framework_start_init
211		bdev_malloc_create -b Malloc0 32 512
212		bdev_malloc_create -b Malloc1 32 512
213		bdev_malloc_create -b Malloc2 32 4096
214		bdev_malloc_create -b Malloc3 32 4096
215		accel_crypto_key_create -c AES_XTS -k 00112233445566778899001122334455 -e 11223344556677889900112233445500 -n test_dek_accel_mlx5_1
216		accel_crypto_key_create -c AES_XTS -k 11223344556677889900112233445500 -e 22334455667788990011223344550011 -n test_dek_accel_mlx5_2
217		accel_crypto_key_create -c AES_XTS -k 22334455667788990011223344550011 -e 33445566778899001122334455002233 -n test_dek_accel_mlx5_3
218		accel_crypto_key_create -c AES_XTS -k 33445566778899001122334455001122 -e 44556677889900112233445500112233 -n test_dek_accel_mlx5_4
219		bdev_crypto_create Malloc0 crypto_ram_1 -n test_dek_accel_mlx5_1
220		bdev_crypto_create Malloc1 crypto_ram_2 -n test_dek_accel_mlx5_2
221		bdev_crypto_create Malloc2 crypto_ram_3 -n test_dek_accel_mlx5_3
222		bdev_crypto_create Malloc3 crypto_ram_4 -n test_dek_accel_mlx5_4
223		bdev_get_bdevs -b Malloc1
224	RPC
225}
226
227function setup_crypto_mlx5_conf() {
228	local key=$1
229	local block_key
230	local tweak_key
231	if [ ${#key} == 64 ]; then
232		# 64 bytes is 32 + 32 - AES_XTS_128 in hexlified format
233		block_key=${key:0:32}
234		tweak_key=${key:32:32}
235	elif [ ${#key} == 128 ]; then
236		# 128 bytes is 64 + 64 - AES_XTS_256 in hexlified format
237		block_key=${key:0:64}
238		tweak_key=${key:64:64}
239	else
240		echo "ERROR: Invalid DEK size for MLX5 crypto setup: ${#key}"
241		echo "ERROR: Supported key sizes for MLX5: 64 bytes (AES_XTS_128) and 128 bytes (AES_XTS_256)."
242		return 1
243	fi
244
245	# Malloc0 will use MLX5 AES_XTS
246	"$rootdir/scripts/rpc.py" <<- RPC
247		dpdk_cryptodev_scan_accel_module
248		dpdk_cryptodev_set_driver -d mlx5_pci
249		accel_assign_opc -o encrypt -m dpdk_cryptodev
250		accel_assign_opc -o decrypt -m dpdk_cryptodev
251		framework_start_init
252		bdev_malloc_create -b Malloc0 16 512
253		bdev_crypto_create Malloc0 crypto_ram4 -k $block_key -c AES_XTS -k2 $tweak_key
254		bdev_get_bdevs -b Malloc0
255	RPC
256}
257
258function setup_rbd_conf() {
259	timing_enter rbd_setup
260	rbd_setup 127.0.0.1
261	timing_exit rbd_setup
262
263	"$rpc_py" bdev_rbd_create -b Ceph0 rbd foo 512
264}
265
266function setup_daos_conf() {
267	local pool=testpool
268	local cont=testcont
269
270	timing_enter daos_setup
271	daos_setup $pool $cont
272	timing_exit daos_setup
273
274	"$rpc_py" bdev_daos_create Daos0 $pool $cont 16 4096
275}
276
277function setup_raid5f_conf() {
278	"$rpc_py" <<- RPC
279		bdev_malloc_create -b Malloc0 32 512
280		bdev_malloc_create -b Malloc1 32 512
281		bdev_malloc_create -b Malloc2 32 512
282		bdev_raid_create -n raid5f -z 2 -r 5f -b "Malloc0 Malloc1 Malloc2"
283	RPC
284}
285
286function bdev_bounds() {
287	$testdir/bdevio/bdevio -w -s $PRE_RESERVED_MEM --json "$conf_file" "$env_ctx" &
288	bdevio_pid=$!
289	trap 'cleanup; killprocess $bdevio_pid; exit 1' SIGINT SIGTERM EXIT
290	echo "Process bdevio pid: $bdevio_pid"
291	waitforlisten $bdevio_pid
292	$testdir/bdevio/tests.py perform_tests
293	killprocess $bdevio_pid
294	trap - SIGINT SIGTERM EXIT
295}
296
297function nbd_function_test() {
298	[[ $(uname -s) == Linux ]] || return 0
299
300	local rpc_server=/var/tmp/spdk-nbd.sock
301	local conf=$1
302	local bdev_all=($2)
303	local bdev_num=${#bdev_all[@]}
304
305	# FIXME: Centos7 in the CI is not shipped with a kernel supporting BLK_DEV_NBD
306	# so don't fail here for now.
307	[[ -e /sys/module/nbd ]] || modprobe -q nbd nbds_max=$bdev_num || return 0
308
309	local nbd_all=(/dev/nbd+([0-9]))
310	bdev_num=$((${#nbd_all[@]} < bdev_num ? ${#nbd_all[@]} : bdev_num))
311
312	local nbd_list=(${nbd_all[@]::bdev_num})
313	local bdev_list=(${bdev_all[@]::bdev_num})
314
315	$rootdir/test/app/bdev_svc/bdev_svc -r $rpc_server -i 0 --json "$conf" "$env_ctx" &
316	nbd_pid=$!
317	trap 'cleanup; killprocess $nbd_pid' SIGINT SIGTERM EXIT
318	waitforlisten $nbd_pid $rpc_server
319
320	nbd_rpc_start_stop_verify $rpc_server "${bdev_list[*]}"
321	nbd_rpc_data_verify $rpc_server "${bdev_list[*]}" "${nbd_list[*]}"
322	nbd_with_lvol_verify $rpc_server "${nbd_list[*]}"
323
324	killprocess $nbd_pid
325	trap - SIGINT SIGTERM EXIT
326}
327
328function fio_test_suite() {
329	local env_context
330
331	# Make sure that state files and anything else produced by fio test will
332	# stay at the testdir.
333	pushd $testdir
334	trap 'rm -f ./*.state; popd; exit 1' SIGINT SIGTERM EXIT
335
336	# Generate the fio config file given the list of all unclaimed bdevs
337	env_context=$(echo "$env_ctx" | sed 's/--env-context=//')
338	fio_config_gen $testdir/bdev.fio verify AIO "$env_context"
339	for b in "${bdevs_name[@]}"; do
340		echo "[job_$b]" >> $testdir/bdev.fio
341		echo "filename=$b" >> $testdir/bdev.fio
342	done
343
344	local fio_params="--ioengine=spdk_bdev --iodepth=8 --bs=4k --runtime=10 $testdir/bdev.fio \
345			--verify_state_save=0 --spdk_json_conf=$conf_file"
346
347	run_test "bdev_fio_rw_verify" fio_bdev $fio_params --spdk_mem=$PRE_RESERVED_MEM --aux-path=$output_dir
348	rm -f ./*.state
349	rm -f $testdir/bdev.fio
350
351	# Generate the fio config file given the list of all unclaimed bdevs that support unmap
352	fio_config_gen $testdir/bdev.fio trim "" "$env_context"
353	if [[ -n $(printf '%s\n' "${bdevs[@]}" | jq -r 'select(.supported_io_types.unmap == true) | .name') ]]; then
354		for b in $(printf '%s\n' "${bdevs[@]}" | jq -r 'select(.supported_io_types.unmap == true) | .name'); do
355			echo "[job_$b]" >> $testdir/bdev.fio
356			echo "filename=$b" >> $testdir/bdev.fio
357		done
358	else
359		rm -f $testdir/bdev.fio
360		popd
361		trap - SIGINT SIGTERM EXIT
362		return 0
363	fi
364
365	run_test "bdev_fio_trim" fio_bdev $fio_params --verify_state_save=0 --aux-path=$output_dir
366	rm -f ./*.state
367	rm -f $testdir/bdev.fio
368	popd
369	trap - SIGINT SIGTERM EXIT
370}
371
372function get_io_result() {
373	local limit_type=$1
374	local qos_dev=$2
375	local iostat_result
376	iostat_result=$($rootdir/scripts/iostat.py -d -i 1 -t $QOS_RUN_TIME | grep $qos_dev | tail -1)
377	if [ $limit_type = IOPS ]; then
378		iostat_result=$(awk '{print $2}' <<< $iostat_result)
379	elif [ $limit_type = BANDWIDTH ]; then
380		iostat_result=$(awk '{print $6}' <<< $iostat_result)
381	fi
382
383	echo ${iostat_result/.*/}
384}
385
386function run_qos_test() {
387	local qos_limit=$1
388	local qos_result=0
389
390	qos_result=$(get_io_result $2 $3)
391	if [ $2 = BANDWIDTH ]; then
392		qos_limit=$((qos_limit * 1024))
393	fi
394	lower_limit=$((qos_limit * 9 / 10))
395	upper_limit=$((qos_limit * 11 / 10))
396
397	# QoS realization is related with bytes transferred. It currently has some variation.
398	if [ $qos_result -lt $lower_limit ] || [ $qos_result -gt $upper_limit ]; then
399		echo "Failed to limit the io read rate of NULL bdev by qos"
400		$rpc_py bdev_malloc_delete $QOS_DEV_1
401		$rpc_py bdev_null_delete $QOS_DEV_2
402		killprocess $QOS_PID
403		exit 1
404	fi
405}
406
407function qos_function_test() {
408	local qos_lower_iops_limit=1000
409	local qos_lower_bw_limit=2
410	local io_result=0
411	local iops_limit=0
412	local bw_limit=0
413
414	io_result=$(get_io_result IOPS $QOS_DEV_1)
415	# Set the IOPS limit as one quarter of the measured performance without QoS
416	iops_limit=$(((io_result / 4) / qos_lower_iops_limit * qos_lower_iops_limit))
417	if [ $iops_limit -gt $qos_lower_iops_limit ]; then
418
419		# Run bdevperf with IOPS rate limit on bdev 1
420		$rpc_py bdev_set_qos_limit --rw_ios_per_sec $iops_limit $QOS_DEV_1
421		run_test "bdev_qos_iops" run_qos_test $iops_limit IOPS $QOS_DEV_1
422
423		# Run bdevperf with bandwidth rate limit on bdev 2
424		# Set the bandwidth limit as 1/10 of the measure performance without QoS
425		bw_limit=$(get_io_result BANDWIDTH $QOS_DEV_2)
426		bw_limit=$((bw_limit / 1024 / 10))
427		if [ $bw_limit -lt $qos_lower_bw_limit ]; then
428			bw_limit=$qos_lower_bw_limit
429		fi
430		$rpc_py bdev_set_qos_limit --rw_mbytes_per_sec $bw_limit $QOS_DEV_2
431		run_test "bdev_qos_bw" run_qos_test $bw_limit BANDWIDTH $QOS_DEV_2
432
433		# Run bdevperf with additional read only bandwidth rate limit on bdev 1
434		$rpc_py bdev_set_qos_limit --r_mbytes_per_sec $qos_lower_bw_limit $QOS_DEV_1
435		run_test "bdev_qos_ro_bw" run_qos_test $qos_lower_bw_limit BANDWIDTH $QOS_DEV_1
436	else
437		echo "Actual IOPS without limiting is too low - exit testing"
438	fi
439}
440
441function qos_test_suite() {
442	# Run bdevperf with QoS disabled first
443	"$rootdir/build/examples/bdevperf" -z -m 0x2 -q 256 -o 4096 -w randread -t 60 "$env_ctx" &
444	QOS_PID=$!
445	echo "Process qos testing pid: $QOS_PID"
446	trap 'cleanup; killprocess $QOS_PID; exit 1' SIGINT SIGTERM EXIT
447	waitforlisten $QOS_PID
448
449	$rpc_py bdev_malloc_create -b $QOS_DEV_1 128 512
450	waitforbdev $QOS_DEV_1
451	$rpc_py bdev_null_create $QOS_DEV_2 128 512
452	waitforbdev $QOS_DEV_2
453
454	$rootdir/examples/bdev/bdevperf/bdevperf.py perform_tests &
455	qos_function_test
456
457	$rpc_py bdev_malloc_delete $QOS_DEV_1
458	$rpc_py bdev_null_delete $QOS_DEV_2
459	killprocess $QOS_PID
460	trap - SIGINT SIGTERM EXIT
461}
462
463function error_test_suite() {
464	DEV_1="Dev_1"
465	DEV_2="Dev_2"
466	ERR_DEV="EE_Dev_1"
467
468	# Run bdevperf with 1 normal bdev and 1 error bdev, also continue on error
469	"$rootdir/build/examples/bdevperf" -z -m 0x2 -q 16 -o 4096 -w randread -t 5 -f "$env_ctx" &
470	ERR_PID=$!
471	echo "Process error testing pid: $ERR_PID"
472	waitforlisten $ERR_PID
473
474	$rpc_py bdev_malloc_create -b $DEV_1 128 512
475	waitforbdev $DEV_1
476	$rpc_py bdev_error_create $DEV_1
477	$rpc_py bdev_malloc_create -b $DEV_2 128 512
478	waitforbdev $DEV_2
479	$rpc_py bdev_error_inject_error $ERR_DEV 'all' 'failure' -n 5
480
481	$rootdir/examples/bdev/bdevperf/bdevperf.py -t 1 perform_tests &
482	sleep 1
483
484	# Bdevperf is expected to be there as the continue on error is set
485	if kill -0 $ERR_PID; then
486		echo "Process is existed as continue on error is set. Pid: $ERR_PID"
487	else
488		echo "Process exited unexpectedly. Pid: $ERR_PID"
489		exit 1
490	fi
491
492	# Delete the error devices
493	$rpc_py bdev_error_delete $ERR_DEV
494	$rpc_py bdev_malloc_delete $DEV_1
495	sleep 5
496	# Expected to exit normally
497	killprocess $ERR_PID
498
499	# Run bdevperf with 1 normal bdev and 1 error bdev, and exit on error
500	"$rootdir/build/examples/bdevperf" -z -m 0x2 -q 16 -o 4096 -w randread -t 5 "$env_ctx" &
501	ERR_PID=$!
502	echo "Process error testing pid: $ERR_PID"
503	waitforlisten $ERR_PID
504
505	$rpc_py bdev_malloc_create -b $DEV_1 128 512
506	waitforbdev $DEV_1
507	$rpc_py bdev_error_create $DEV_1
508	$rpc_py bdev_malloc_create -b $DEV_2 128 512
509	waitforbdev $DEV_2
510	$rpc_py bdev_error_inject_error $ERR_DEV 'all' 'failure' -n 5
511
512	$rootdir/examples/bdev/bdevperf/bdevperf.py -t 1 perform_tests &
513	NOT wait $ERR_PID
514}
515
516function qd_sampling_function_test() {
517	local bdev_name=$1
518	local sampling_period=10
519	local iostats
520
521	$rpc_py bdev_set_qd_sampling_period $bdev_name $sampling_period
522
523	iostats=$($rpc_py bdev_get_iostat -b $bdev_name)
524
525	qd_sampling_period=$(jq -r '.bdevs[0].queue_depth_polling_period' <<< "$iostats")
526
527	if [ $qd_sampling_period == null ] || [ $qd_sampling_period -ne $sampling_period ]; then
528		echo "Qeueue depth polling period is not right"
529		$rpc_py bdev_malloc_delete $QD_DEV
530		killprocess $QD_PID
531		exit 1
532	fi
533}
534
535function qd_sampling_test_suite() {
536	QD_DEV="Malloc_QD"
537
538	"$rootdir/build/examples/bdevperf" -z -m 0x3 -q 256 -o 4096 -w randread -t 5 -C "$env_ctx" &
539	QD_PID=$!
540	echo "Process bdev QD sampling period testing pid: $QD_PID"
541	trap 'cleanup; killprocess $QD_PID; exit 1' SIGINT SIGTERM EXIT
542	waitforlisten $QD_PID
543
544	$rpc_py bdev_malloc_create -b $QD_DEV 128 512
545	waitforbdev $QD_DEV
546
547	$rootdir/examples/bdev/bdevperf/bdevperf.py perform_tests &
548	sleep 2
549	qd_sampling_function_test $QD_DEV
550
551	$rpc_py bdev_malloc_delete $QD_DEV
552	killprocess $QD_PID
553	trap - SIGINT SIGTERM EXIT
554}
555
556function stat_function_test() {
557	local bdev_name=$1
558	local iostats
559	local io_count1
560	local io_count2
561	local iostats_per_channel
562	local io_count_per_channel1
563	local io_count_per_channel2
564	local io_count_per_channel_all=0
565
566	iostats=$($rpc_py bdev_get_iostat -b $bdev_name)
567	io_count1=$(jq -r '.bdevs[0].num_read_ops' <<< "$iostats")
568
569	iostats_per_channel=$($rpc_py bdev_get_iostat -b $bdev_name -c)
570	io_count_per_channel1=$(jq -r '.channels[0].num_read_ops' <<< "$iostats_per_channel")
571	io_count_per_channel_all=$((io_count_per_channel_all + io_count_per_channel1))
572	io_count_per_channel2=$(jq -r '.channels[1].num_read_ops' <<< "$iostats_per_channel")
573	io_count_per_channel_all=$((io_count_per_channel_all + io_count_per_channel2))
574
575	iostats=$($rpc_py bdev_get_iostat -b $bdev_name)
576	io_count2=$(jq -r '.bdevs[0].num_read_ops' <<< "$iostats")
577
578	# There is little time passed between the three iostats collected. So that
579	# the accumulated statistics from per channel data shall be bigger than the
580	# the first run and smaller than the third run in this short time of period.
581	if [ $io_count_per_channel_all -lt $io_count1 ] || [ $io_count_per_channel_all -gt $io_count2 ]; then
582		echo "Failed to collect the per Core IO statistics"
583		$rpc_py bdev_malloc_delete $STAT_DEV
584		killprocess $STAT_PID
585		exit 1
586	fi
587}
588
589function stat_test_suite() {
590	STAT_DEV="Malloc_STAT"
591
592	# Run bdevperf with 2 cores so as to collect per Core IO statistics
593	"$rootdir/build/examples/bdevperf" -z -m 0x3 -q 256 -o 4096 -w randread -t 10 -C "$env_ctx" &
594	STAT_PID=$!
595	echo "Process Bdev IO statistics testing pid: $STAT_PID"
596	trap 'cleanup; killprocess $STAT_PID; exit 1' SIGINT SIGTERM EXIT
597	waitforlisten $STAT_PID
598
599	$rpc_py bdev_malloc_create -b $STAT_DEV 128 512
600	waitforbdev $STAT_DEV
601
602	$rootdir/examples/bdev/bdevperf/bdevperf.py perform_tests &
603	sleep 2
604	stat_function_test $STAT_DEV
605
606	$rpc_py bdev_malloc_delete $STAT_DEV
607	killprocess $STAT_PID
608	trap - SIGINT SIGTERM EXIT
609}
610
611function bdev_gpt_uuid() {
612	local bdev
613
614	start_spdk_tgt
615
616	"$rpc_py" load_config -j "$conf_file"
617	"$rpc_py" bdev_wait_for_examine
618
619	bdev=$("$rpc_py" bdev_get_bdevs -b "$g_unique_partguid")
620	[[ "$(jq -r 'length' <<< "$bdev")" == "1" ]]
621	[[ "$(jq -r '.[0].aliases[0]' <<< "$bdev")" == "$g_unique_partguid" ]]
622	[[ "$(jq -r '.[0].driver_specific.gpt.unique_partition_guid' <<< "$bdev")" == "$g_unique_partguid" ]]
623
624	bdev=$("$rpc_py" bdev_get_bdevs -b "$g_unique_partguid_old")
625	[[ "$(jq -r 'length' <<< "$bdev")" == "1" ]]
626	[[ "$(jq -r '.[0].aliases[0]' <<< "$bdev")" == "$g_unique_partguid_old" ]]
627	[[ "$(jq -r '.[0].driver_specific.gpt.unique_partition_guid' <<< "$bdev")" == "$g_unique_partguid_old" ]]
628
629	killprocess "$spdk_tgt_pid"
630}
631
632function bdev_crypto_enomem() {
633	local base_dev="base0"
634	local test_dev="crypt0"
635	local err_dev="EE_$base_dev"
636	local qd=32
637
638	"$rootdir/build/examples/bdevperf" -z -m 0x2 -q $qd -o 4096 -w randwrite -t 5 -f "$env_ctx" &
639	ERR_PID=$!
640	trap 'cleanup; killprocess $ERR_PID; exit 1' SIGINT SIGTERM EXIT
641	waitforlisten $ERR_PID
642
643	$rpc_py <<- RPC
644		accel_crypto_key_create -c AES_XTS -k 00112233445566778899001122334455 -e 11223344556677889900112233445500 -n test_dek_sw
645		bdev_null_create $base_dev 1024 512
646		bdev_error_create $base_dev
647		bdev_crypto_create $err_dev $test_dev -n test_dek_sw
648	RPC
649
650	waitforbdev $test_dev
651
652	$rootdir/examples/bdev/bdevperf/bdevperf.py perform_tests &
653	rpcpid=$!
654
655	sleep 1
656	$rpc_py bdev_error_inject_error $err_dev -n 5 -q $((qd - 1)) write nomem
657
658	wait $rpcpid
659
660	$rpc_py bdev_crypto_delete $test_dev
661
662	killprocess $ERR_PID
663	trap - SIGINT SIGTERM EXIT
664}
665
666# Initial bdev creation and configuration
667#-----------------------------------------------------
668QOS_DEV_1="Malloc_0"
669QOS_DEV_2="Null_1"
670QOS_RUN_TIME=5
671
672if [ $(uname -s) = Linux ]; then
673	# Test dynamic memory management. All hugepages will be reserved at runtime
674	PRE_RESERVED_MEM=0
675else
676	# Dynamic memory management is not supported on BSD
677	PRE_RESERVED_MEM=2048
678fi
679
680test_type=${1:-bdev}
681crypto_device=$2
682dek=$3
683env_ctx=""
684wait_for_rpc=""
685if [ -n "$crypto_device" ]; then
686	env_ctx="--env-context=--allow=$crypto_device,class=crypto"
687fi
688if [[ $test_type == bdev || $test_type == crypto_* ]]; then
689	wait_for_rpc="--wait-for-rpc"
690fi
691start_spdk_tgt
692case "$test_type" in
693	bdev)
694		setup_bdev_conf
695		;;
696	nvme)
697		setup_nvme_conf
698		;;
699	gpt)
700		setup_gpt_conf
701		;;
702	crypto_aesni)
703		setup_crypto_aesni_conf
704		;;
705	crypto_qat)
706		setup_crypto_qat_conf
707		;;
708	crypto_sw)
709		setup_crypto_sw_conf
710		;;
711	crypto_mlx5)
712		setup_crypto_mlx5_conf $dek
713		;;
714	crypto_accel_mlx5)
715		setup_crypto_accel_mlx5_conf
716		;;
717	rbd)
718		setup_rbd_conf
719		;;
720	daos)
721		setup_daos_conf
722		;;
723	raid5f)
724		setup_raid5f_conf
725		;;
726	xnvme)
727		setup_xnvme_conf
728		;;
729	*)
730		echo "invalid test name"
731		exit 1
732		;;
733esac
734
735"$rpc_py" bdev_wait_for_examine
736
737# Generate json config and use it throughout all the tests
738cat <<- CONF > "$conf_file"
739	        {"subsystems":[
740	        $("$rpc_py" save_subsystem_config -n accel),
741	        $("$rpc_py" save_subsystem_config -n bdev),
742	        $("$rpc_py" save_subsystem_config -n iobuf)
743	        ]}
744CONF
745
746mapfile -t bdevs < <("$rpc_py" bdev_get_bdevs | jq -r '.[] | select(.claimed == false)')
747mapfile -t bdevs_name < <(printf '%s\n' "${bdevs[@]}" | jq -r '.name')
748bdev_list=("${bdevs_name[@]}")
749
750hello_world_bdev=${bdev_list[0]}
751trap - SIGINT SIGTERM EXIT
752killprocess "$spdk_tgt_pid"
753# End bdev configuration
754#-----------------------------------------------------
755
756trap "cleanup" SIGINT SIGTERM EXIT
757
758run_test "bdev_hello_world" $SPDK_EXAMPLE_DIR/hello_bdev --json "$conf_file" -b "$hello_world_bdev" "$env_ctx"
759run_test "bdev_bounds" bdev_bounds "$env_ctx"
760run_test "bdev_nbd" nbd_function_test "$conf_file" "${bdevs_name[*]}" "$env_ctx"
761if [[ $CONFIG_FIO_PLUGIN == y ]]; then
762	if [ "$test_type" = "nvme" ] || [ "$test_type" = "gpt" ]; then
763		# TODO: once we get real multi-ns drives, re-enable this test for NVMe.
764		echo "skipping fio tests on NVMe due to multi-ns failures."
765	else
766		run_test "bdev_fio" fio_test_suite "$env_ctx"
767	fi
768else
769	echo "FIO not available"
770	exit 1
771fi
772
773trap "cleanup" SIGINT SIGTERM EXIT
774
775run_test "bdev_verify" $rootdir/build/examples/bdevperf --json "$conf_file" -q 128 -o 4096 -w verify -t 5 -C -m 0x3 "$env_ctx"
776run_test "bdev_verify_big_io" $rootdir/build/examples/bdevperf --json "$conf_file" -q 128 -o 65536 -w verify -t 5 -C -m 0x3 "$env_ctx"
777run_test "bdev_write_zeroes" $rootdir/build/examples/bdevperf --json "$conf_file" -q 128 -o 4096 -w write_zeroes -t 1 "$env_ctx"
778
779# test json config not enclosed with {}
780run_test "bdev_json_nonenclosed" $rootdir/build/examples/bdevperf --json "$nonenclosed_conf_file" -q 128 -o 4096 -w write_zeroes -t 1 "$env_ctx" || true
781
782# test json config "subsystems" not with array
783run_test "bdev_json_nonarray" $rootdir/build/examples/bdevperf --json "$nonarray_conf_file" -q 128 -o 4096 -w write_zeroes -t 1 "$env_ctx" || true
784
785if [[ $test_type == bdev ]]; then
786	run_test "bdev_qos" qos_test_suite "$env_ctx"
787	run_test "bdev_qd_sampling" qd_sampling_test_suite "$env_ctx"
788	run_test "bdev_error" error_test_suite "$env_ctx"
789	run_test "bdev_stat" stat_test_suite "$env_ctx"
790fi
791
792if [[ $test_type == gpt ]]; then
793	run_test "bdev_gpt_uuid" bdev_gpt_uuid
794fi
795
796if [[ $test_type == crypto_sw ]]; then
797	run_test "bdev_crypto_enomem" bdev_crypto_enomem
798fi
799
800# Temporarily disabled - infinite loop
801# if [ $RUN_NIGHTLY -eq 1 ]; then
802# run_test "bdev_reset" $rootdir/build/examples/bdevperf --json "$conf_file" -q 16 -w reset -o 4096 -t 60 "$env_ctx"
803# fi
804
805# Bdev and configuration cleanup below this line
806#-----------------------------------------------------
807
808trap - SIGINT SIGTERM EXIT
809cleanup
810