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