xref: /spdk/test/dd/common.sh (revision 838e61c3772fdefb17e1a0b8f9880e2bcb9c4c0d)
1#  SPDX-License-Identifier: BSD-3-Clause
2#  Copyright (C) 2020 Intel Corporation
3#  All rights reserved.
4#
5
6source "$rootdir/test/common/autotest_common.sh"
7source "$rootdir/scripts/common.sh"
8
9clear_nvme() {
10	local bdev=$1
11	local nvme_ref=$2
12	local size=${3:-0xffff}
13
14	local bs=$((1024 << 10)) # 1M
15	local count=$(((size / bs) + (size % bs ? 1 : 0)))
16
17	"${DD_APP[@]}" \
18		--if="/dev/zero" \
19		--bs="$bs" \
20		--ob="$bdev" \
21		--count="$count" \
22		--json <(gen_conf $nvme_ref)
23}
24
25trunc_files() {
26	local f
27	for f; do : > "$f"; done
28}
29
30gen_conf() {
31	xtrace_disable
32
33	local ref_name
34	local method methods
35	local param params
36	local config
37
38	# Pick references to all assoc arrays and build subsystem's config
39	# around them. The assoc array should be the name of the rpc method
40	# suffixed with unique _ID (ID may be any string). Default arrays
41	# should be prefixed with _method string. The keys of the array
42	# should store names of the method's parameters - proper quoting
43	# of the values is done here. extra_subsystems[] can store extra
44	# json configuration for different subsystems, other than bdev.
45
46	if (($# == 0)) && [[ -z ${!method_@} ]]; then
47		return 1
48	fi
49
50	methods=("${@:-${!method_@}}")
51	local IFS=","
52
53	for ref_name in "${methods[@]}"; do
54		method=${ref_name#*method_} method=${method%_*} params=()
55
56		# FIXME: centos7's Bash got trapped in 2011:
57		# local -n ref=$ref_name -> local: -n: invalid option
58		# HACK: it with eval and partial refs instead.
59		eval "local refs=(\${!${ref_name}[@]})"
60		local param_ref
61
62		for param in "${refs[@]}"; do
63			param_ref="${ref_name}[$param]"
64			if [[ ${!param_ref} =~ ^([0-9]+|true|false|\{.*\})$ ]]; then
65				params+=("\"$param\": ${!param_ref}")
66			else
67				params+=("\"$param\": \"${!param_ref}\"")
68			fi
69		done
70
71		config+=("$(
72			cat <<- JSON
73				{
74				  "params": {
75				    ${params[*]}
76				  },
77				  "method": "$method"
78				}
79			JSON
80		)")
81	done
82
83	jq . <<- JSON | tee /dev/stderr
84		{
85		  "subsystems": [
86		    {
87		      "subsystem": "bdev",
88		      "config": [
89		        ${config[*]},
90			{
91			  "method": "bdev_wait_for_examine"
92			}
93		      ]
94		    }
95		    ${extra_subsystems[*]:+,${extra_subsystems[*]}}
96		  ]
97		}
98	JSON
99
100	xtrace_restore
101}
102
103gen_bytes() {
104	xtrace_disable
105
106	local max=$1
107	local bytes
108	local byte
109	local string
110	shift
111
112	bytes=({a..z} {0..9})
113	if (($#)); then
114		bytes=("$@")
115	fi
116
117	for ((byte = 0; byte < max; byte++)); do
118		string+=${bytes[RANDOM % ${#bytes[@]}]}
119	done
120	printf '%b' "$string"
121
122	xtrace_restore
123}
124
125get_native_nvme_bs() {
126	# This is now needed since spdk_dd will reject all bs smaller than the
127	# native bs of given nvme. We need to make sure all tests are using
128	# bs >= native_bs. Use identify here so we don't have to switch nvmes
129	# between user space and the kernel back and forth.
130	local pci=$1 lbaf id
131
132	mapfile -t id < <("$rootdir/build/examples/identify" -r trtype:pcie "traddr:$pci")
133
134	# Get size of the current LBAF
135	[[ ${id[*]} =~ "Current LBA Format:"\ *"LBA Format #"([0-9]+) ]]
136	lbaf=${BASH_REMATCH[1]}
137	[[ ${id[*]} =~ "LBA Format #$lbaf: Data Size:"\ *([0-9]+) ]]
138	lbaf=${BASH_REMATCH[1]}
139
140	echo "$lbaf"
141}
142
143check_liburing() {
144	# Simply check if spdk_dd links to liburing. If yes, log that information.
145	local lib so
146	local -g liburing_in_use=0
147
148	while read -r lib _ so _; do
149		if [[ $lib == liburing.so.* ]]; then
150			printf '* spdk_dd linked to liburing\n'
151			# For sanity, check build config to see if liburing was requested.
152			if [[ -e $rootdir/test/common/build_config.sh ]]; then
153				source "$rootdir/test/common/build_config.sh"
154			fi
155			if [[ $CONFIG_URING != y ]]; then
156				printf '* spdk_dd built with liburing, but no liburing support requested?\n'
157			fi
158			if [[ ! -e $so ]]; then
159				printf '* %s is missing, aborting\n' "$lib"
160				return 1
161			fi
162			export liburing_in_use=1
163			return 0
164		fi
165	done < <(LD_TRACE_LOADED_OBJECTS=1 "${DD_APP[@]}") >&2
166}
167
168init_zram() {
169	[[ -e /sys/class/zram-control ]] || modprobe zram num_devices=0
170	return
171}
172
173create_zram_dev() {
174	cat /sys/class/zram-control/hot_add
175}
176
177remove_zram_dev() {
178	local id=$1
179
180	[[ -e /sys/block/zram$id ]]
181
182	echo 1 > "/sys/block/zram$id/reset"
183	echo "$id" > "/sys/class/zram-control/hot_remove"
184}
185
186set_zram_dev() {
187	local id=$1
188	local size=${2:-64M}
189
190	[[ -e /sys/block/zram$id ]]
191
192	echo "$size" > "/sys/block/zram$id/disksize"
193}
194
195init_null_blk() {
196	[[ -e /sys/module/null_blk ]] || modprobe null_blk "$@"
197	return
198}
199
200remove_null_blk() {
201	modprobe -r null_blk
202}
203