xref: /spdk/test/ftl/common.sh (revision f538c202ffc4a20f6cd3abcbe1a131278e7e0ea6)
1#  SPDX-License-Identifier: BSD-3-Clause
2#  Copyright (C) 2019 Intel Corporation
3#  All rights reserved.
4#
5
6# Common utility functions to be sourced by the libftl test scripts
7
8testdir=$(readlink -f "$(dirname "$0")")
9rootdir=$(readlink -f "$testdir/../..")
10rpc_py=$rootdir/scripts/rpc.py
11
12export ftl_tgt_core_mask="[0]"
13
14export spdk_tgt_bin="$SPDK_BIN_DIR/spdk_tgt"
15export spdk_tgt_cpumask="[0]"
16export spdk_tgt_cnfg="${testdir}/config/tgt.json"
17export spdk_tgt_pid=""
18
19export spdk_ini_bin="$SPDK_BIN_DIR/spdk_tgt"
20export spdk_ini_cpumask="[1]"
21export spdk_ini_rpc="/var/tmp/spdk.tgt.sock"
22export spdk_ini_cnfg="${testdir}/config/ini.json"
23export spdk_ini_pid=""
24
25export spdk_dd_bin="$SPDK_BIN_DIR/spdk_dd"
26
27function clear_lvols() {
28	stores=$($rpc_py bdev_lvol_get_lvstores | jq -r ".[] | .uuid")
29	for lvs in $stores; do
30		$rpc_py bdev_lvol_delete_lvstore -u $lvs
31	done
32}
33
34function create_nv_cache_bdev() {
35	local name=$1
36	local cache_bdf=$2
37	local base_bdev=$3
38	local cache_size=$4
39
40	# use 5% space of base bdev
41	local base_size=$(($(get_bdev_size "$base_bdev") * 5 / 100))
42
43	# Create NVMe bdev on specified device and split it so that it has the desired size
44	local nvc_bdev
45	nvc_bdev=$($rpc_py bdev_nvme_attach_controller -b "$name" -t PCIe -a "$cache_bdf")
46
47	if [[ -z "$cache_size" ]]; then
48		cache_size=$(($(get_bdev_size "$base_bdev") * 5 / 100))
49	fi
50	$rpc_py bdev_split_create "$nvc_bdev" -s "$cache_size" 1
51}
52
53function create_base_bdev() {
54	local name=$1
55	local base_bdf=$2
56	local size=$3
57
58	# Create NVMe bdev on specified device and split it so that it has the desired size
59	local base_bdev
60	base_bdev=$($rpc_py bdev_nvme_attach_controller -b $name -t PCIe -a $base_bdf)
61
62	local base_size
63	base_size=$(get_bdev_size $base_bdev)
64	if [[ $size -le $base_size ]]; then
65		$rpc_py bdev_split_create $base_bdev -s $size 1
66	else
67		clear_lvols
68		lvs=$($rpc_py bdev_lvol_create_lvstore $base_bdev lvs)
69		$rpc_py bdev_lvol_create ${base_bdev}p0 $size -t -u $lvs
70	fi
71}
72
73# required input variables:
74# FTL_BDEV - FTL bdev name
75# FTL_BASE - FTL base device PCIe path
76# FTL_BASE_SIZE - FTL base device size
77# FTL_CACHE - FTL NV cache device PCIe path
78# FTL_CACHE_SIZE - FTL NV cache device size
79# FTL_L2P_DRAM_LIMIT - FTL L2P DRAM limit
80function tcp_target_setup() {
81	local base_bdev=""
82	local cache_bdev=""
83
84	if [[ -f "$spdk_tgt_cnfg" ]]; then
85		$spdk_tgt_bin "--cpumask=$spdk_tgt_cpumask" --config="$spdk_tgt_cnfg" &
86	else
87		$spdk_tgt_bin "--cpumask=$spdk_tgt_cpumask" &
88	fi
89	spdk_tgt_pid=$!
90	export spdk_tgt_pid
91	waitforlisten "$spdk_tgt_pid"
92
93	if [[ -f "$spdk_tgt_cnfg" ]]; then
94		# Configuration loaded from the JSON file
95		return 0
96	fi
97
98	# Check if input parameters are available
99	local params=(FTL_BDEV FTL_BASE FTL_BASE_SIZE FTL_CACHE FTL_CACHE_SIZE FTL_L2P_DRAM_LIMIT)
100	for param in "${params[@]}"; do
101		if [[ -z "${!param}" ]]; then
102			echo "Missing $param"
103			exit 1
104		fi
105	done
106
107	base_bdev=$(create_base_bdev base "$FTL_BASE" "$FTL_BASE_SIZE")
108	if [[ -z "$base_bdev" ]]; then
109		echo "Cannot create base device"
110		exit 1
111	fi
112
113	cache_bdev=$(create_nv_cache_bdev cache "$FTL_CACHE" "$base_bdev" "$FTL_CACHE_SIZE")
114	if [[ -z "$cache_bdev" ]]; then
115		echo "Cannot create nv cache device"
116		exit 1
117	fi
118
119	$rpc_py -t 60 bdev_ftl_create -b "$FTL_BDEV" -d "$base_bdev" -c "$cache_bdev" --l2p_dram_limit "$FTL_L2P_DRAM_LIMIT"
120
121	$rpc_py nvmf_create_transport --trtype TCP
122	$rpc_py nvmf_create_subsystem nqn.2018-09.io.spdk:cnode0 -a -m 1
123	$rpc_py nvmf_subsystem_add_ns nqn.2018-09.io.spdk:cnode0 "$FTL_BDEV"
124	$rpc_py nvmf_subsystem_add_listener nqn.2018-09.io.spdk:cnode0 -t TCP -f ipv4 -s 4420 -a 127.0.0.1
125
126	$rpc_py save_config > "$spdk_tgt_cnfg"
127}
128
129function tcp_target_shutdown() {
130	if [[ -n "$spdk_tgt_pid" ]]; then
131		killprocess "$spdk_tgt_pid"
132		unset spdk_tgt_pid
133	fi
134}
135
136function tcp_target_shutdown_dirty() {
137	if [[ -n "$spdk_tgt_pid" ]]; then
138		kill -9 "$spdk_tgt_pid"
139		unset spdk_tgt_pid
140	fi
141}
142
143function tcp_target_cleanup() {
144	tcp_target_shutdown
145	rm -f "$spdk_tgt_cnfg"
146}
147
148# required input variables:
149# FTL_BDEV - FTL bdev name
150function tcp_initiator_setup() {
151	local rpc="$rpc_py -s ${spdk_ini_rpc}"
152
153	if [[ -f "$spdk_ini_cnfg" ]]; then
154		return 0
155	fi
156
157	if [[ -z "$FTL_BDEV" ]]; then
158		echo "Missing FTL_BDEV"
159		exit 1
160	fi
161
162	$spdk_ini_bin --cpumask="$spdk_ini_cpumask" --rpc-socket="$spdk_ini_rpc" &
163	spdk_ini_pid=$!
164	export spdk_ini_pid
165	waitforlisten $spdk_ini_pid $spdk_ini_rpc
166
167	$rpc bdev_nvme_attach_controller -b "$FTL_BDEV" \
168		-t tcp -a 127.0.0.1 -s 4420 -f ipv4 -n nqn.2018-09.io.spdk:cnode0
169
170	(
171		echo '{"subsystems": ['
172		$rpc save_subsystem_config -n bdev
173		echo ']}'
174	) > "$spdk_ini_cnfg"
175
176	killprocess $spdk_ini_pid
177	unset spdk_ini_pid
178}
179
180function tcp_initiator_shutdown() {
181	if [[ -n "$spdk_ini_pid" ]]; then
182		killprocess "$spdk_ini_pid"
183		unset spdk_ini_pid
184	fi
185}
186
187function tcp_initiator_cleanup() {
188	tcp_initiator_shutdown
189	rm -f "$spdk_ini_cnfg"
190}
191
192function tcp_cleanup() {
193	tcp_target_cleanup
194	tcp_initiator_cleanup
195}
196
197function tcp_dd() {
198	tcp_initiator_setup
199	$spdk_dd_bin --cpumask="$spdk_ini_cpumask" --rpc-socket="$spdk_ini_rpc" --json="$spdk_ini_cnfg" "$@"
200}
201
202# Remove not needed files from shared memory
203function remove_shm() {
204	echo Remove shared memory files
205	rm -f rm -f /dev/shm/ftl*
206	rm -f rm -f /dev/hugepages/ftl*
207	rm -f rm -f /dev/shm/spdk*
208	rm -f rm -f /dev/shm/iscsi
209	rm -f rm -f /dev/hugepages/spdk*
210}
211