xref: /spdk/test/json_config/json_config.sh (revision f30fdcf100267bd386c5a5c4c38a9f20959fbff8)
1#!/usr/bin/env bash
2#  SPDX-License-Identifier: BSD-3-Clause
3#  Copyright (C) 2018 Intel Corporation
4#  All rights reserved.
5#
6rootdir=$(readlink -f $(dirname $0)/../..)
7source "$rootdir/test/common/autotest_common.sh"
8source "$rootdir/test/nvmf/common.sh"
9source "$rootdir/test/json_config/common.sh"
10
11if [[ $SPDK_TEST_ISCSI -eq 1 ]]; then
12	source "$rootdir/test/iscsi_tgt/common.sh"
13fi
14
15if [[ $SPDK_TEST_VHOST -ne 1 && $SPDK_TEST_VHOST_INIT -eq 1 ]]; then
16	SPDK_TEST_VHOST=1
17	echo "WARNING: Virtio initiator JSON_config test requires vhost target."
18	echo "         Setting SPDK_TEST_VHOST=1 for duration of current script."
19fi
20
21if ((SPDK_TEST_BLOCKDEV + \
22	SPDK_TEST_ISCSI + \
23	SPDK_TEST_NVMF + \
24	SPDK_TEST_VHOST + \
25	SPDK_TEST_VHOST_INIT + \
26	SPDK_TEST_RBD == 0)); then
27	echo "WARNING: No tests are enabled so not running JSON configuration tests"
28	exit 0
29fi
30
31declare -A app_pid=([target]="" [initiator]="")
32declare -A app_socket=([target]='/var/tmp/spdk_tgt.sock' [initiator]='/var/tmp/spdk_initiator.sock')
33declare -A app_params=([target]='-m 0x1 -s 1024' [initiator]='-m 0x2 -g -u -s 1024')
34declare -A configs_path=([target]="$rootdir/spdk_tgt_config.json" [initiator]="$rootdir/spdk_initiator_config.json")
35
36function initiator_rpc() {
37	$rootdir/scripts/rpc.py -s "${app_socket[initiator]}" "$@"
38}
39
40last_event_id=0
41
42function tgt_check_notification_types() {
43	timing_enter "${FUNCNAME[0]}"
44
45	local ret=0
46	local enabled_types=("bdev_register" "bdev_unregister")
47
48	local get_types=($(tgt_rpc notify_get_types | jq -r '.[]'))
49	if [[ ${enabled_types[*]} != "${get_types[*]}" ]]; then
50		echo "ERROR: expected types: ${enabled_types[*]}, but got: ${get_types[*]}"
51		ret=1
52	fi
53
54	timing_exit "${FUNCNAME[0]}"
55	return $ret
56}
57
58get_notifications() {
59	local ev_type ev_ctx event_id
60
61	while IFS=":" read -r ev_type ev_ctx event_id; do
62		echo "$ev_type:$ev_ctx"
63	done < <(tgt_rpc notify_get_notifications -i "$last_event_id" | jq -r '.[] | "\(.type):\(.ctx):\(.id)"')
64}
65
66function tgt_check_notifications() {
67	local events_to_check
68	local recorded_events
69
70	# Seems like notifications don't necessarily have to come in order, so make sure they are sorted
71	events_to_check=($(printf '%s\n' "$@" | sort))
72	recorded_events=($(get_notifications | sort))
73
74	if [[ ${events_to_check[*]} != "${recorded_events[*]}" ]]; then
75		cat <<- ERROR
76			Expected events did not match.
77
78			Expected:
79			$(printf ' %s\n' "${events_to_check[@]}")
80			Recorded:
81			$(printf ' %s\n' "${recorded_events[@]}")
82		ERROR
83		return 1
84	fi
85
86	cat <<- INFO
87		Expected events matched:
88		$(printf ' %s\n' "${recorded_events[@]}")
89	INFO
90}
91
92function create_accel_config() {
93	timing_enter "${FUNCNAME[0]}"
94
95	if [[ $SPDK_TEST_CRYPTO -eq 1 ]]; then
96		tgt_rpc dpdk_cryptodev_scan_accel_module
97		tgt_rpc accel_assign_opc -o encrypt -m dpdk_cryptodev
98		tgt_rpc accel_assign_opc -o decrypt -m dpdk_cryptodev
99	fi
100
101	timing_exit "${FUNCNAME[0]}"
102}
103
104function create_bdev_subsystem_config() {
105	timing_enter "${FUNCNAME[0]}"
106
107	local expected_notifications=()
108
109	# Consider multiple nvme devices loaded into the subsystem prior running
110	# the tests.
111	expected_notifications+=($(get_notifications))
112
113	if [[ $SPDK_TEST_BLOCKDEV -eq 1 ]]; then
114		local lvol_store_base_bdev=Nvme0n1
115
116		tgt_rpc bdev_split_create $lvol_store_base_bdev 2
117		tgt_rpc bdev_split_create Malloc0 3
118		tgt_rpc bdev_malloc_create 8 4096 --name Malloc3
119		tgt_rpc bdev_passthru_create -b Malloc3 -p PTBdevFromMalloc3
120
121		tgt_rpc bdev_null_create Null0 32 512
122
123		tgt_rpc bdev_malloc_create 32 512 --name Malloc0
124		tgt_rpc bdev_malloc_create 16 4096 --name Malloc1
125
126		expected_notifications+=(
127			bdev_register:${lvol_store_base_bdev}p1
128			bdev_register:${lvol_store_base_bdev}p0
129			bdev_register:Malloc3
130			bdev_register:PTBdevFromMalloc3
131			bdev_register:Null0
132			bdev_register:Malloc0
133			bdev_register:Malloc0p2
134			bdev_register:Malloc0p1
135			bdev_register:Malloc0p0
136			bdev_register:Malloc1
137		)
138
139		# This AIO bdev must be large enough to be used as LVOL store
140		dd if=/dev/zero of="$SPDK_TEST_STORAGE/sample_aio" bs=1024 count=102400
141		tgt_rpc bdev_aio_create "$SPDK_TEST_STORAGE/sample_aio" aio_disk 1024
142		expected_notifications+=(bdev_register:aio_disk)
143
144		# For LVOLs use split to check for proper order of initialization.
145		# If LVOLs configuration will be reordered (eg moved before splits or AIO/NVMe)
146		# it should fail loading JSON config from file.
147		tgt_rpc bdev_lvol_create_lvstore -c 1048576 ${lvol_store_base_bdev}p0 lvs_test
148
149		expected_notifications+=(
150			"bdev_register:$(tgt_rpc bdev_lvol_create -l lvs_test lvol0 32)"
151			"bdev_register:$(tgt_rpc bdev_lvol_create -l lvs_test -t lvol1 32)"
152			"bdev_register:$(tgt_rpc bdev_lvol_snapshot lvs_test/lvol0 snapshot0)"
153			"bdev_register:$(tgt_rpc bdev_lvol_clone lvs_test/snapshot0 clone0)"
154		)
155	fi
156
157	if [[ $SPDK_TEST_CRYPTO -eq 1 ]]; then
158		tgt_rpc bdev_malloc_create 8 1024 --name MallocForCryptoBdev
159		if [[ $(lspci -d:37c8 | wc -l) -eq 0 ]]; then
160			local crypto_driver=crypto_aesni_mb
161		else
162			local crypto_driver=crypto_qat
163		fi
164
165		tgt_rpc bdev_crypto_create MallocForCryptoBdev CryptoMallocBdev -p $crypto_driver -k 01234567891234560123456789123456
166		expected_notifications+=(
167			bdev_register:MallocForCryptoBdev
168			bdev_register:CryptoMallocBdev
169		)
170	fi
171
172	if [[ $SPDK_TEST_RBD -eq 1 ]]; then
173		rbd_setup 127.0.0.1
174		tgt_rpc bdev_rbd_create $RBD_POOL $RBD_NAME 4096
175		expected_notifications+=(bdev_register:Ceph0)
176	fi
177
178	tgt_check_notifications "${expected_notifications[@]}"
179
180	timing_exit "${FUNCNAME[0]}"
181}
182
183function cleanup_bdev_subsystem_config() {
184	timing_enter "${FUNCNAME[0]}"
185
186	if [[ $SPDK_TEST_BLOCKDEV -eq 1 ]]; then
187		tgt_rpc bdev_lvol_delete lvs_test/clone0
188		tgt_rpc bdev_lvol_delete lvs_test/lvol0
189		tgt_rpc bdev_lvol_delete lvs_test/snapshot0
190		tgt_rpc bdev_lvol_delete_lvstore -l lvs_test
191	fi
192
193	if [[ $(uname -s) = Linux ]]; then
194		rm -f "$SPDK_TEST_STORAGE/sample_aio"
195	fi
196
197	if [[ $SPDK_TEST_RBD -eq 1 ]]; then
198		rbd_cleanup
199	fi
200
201	timing_exit "${FUNCNAME[0]}"
202}
203
204function create_vhost_subsystem_config() {
205	timing_enter "${FUNCNAME[0]}"
206
207	tgt_rpc bdev_malloc_create 64 1024 --name MallocForVhost0
208	tgt_rpc bdev_split_create MallocForVhost0 8
209
210	tgt_rpc vhost_create_scsi_controller VhostScsiCtrlr0
211	tgt_rpc vhost_scsi_controller_add_target VhostScsiCtrlr0 0 MallocForVhost0p3
212	tgt_rpc vhost_scsi_controller_add_target VhostScsiCtrlr0 -1 MallocForVhost0p4
213	tgt_rpc vhost_controller_set_coalescing VhostScsiCtrlr0 1 100
214
215	tgt_rpc vhost_create_blk_controller VhostBlkCtrlr0 MallocForVhost0p5
216
217	timing_exit "${FUNCNAME[0]}"
218}
219
220function create_iscsi_subsystem_config() {
221	timing_enter "${FUNCNAME[0]}"
222	tgt_rpc bdev_malloc_create 64 1024 --name MallocForIscsi0
223	tgt_rpc iscsi_create_portal_group $PORTAL_TAG 127.0.0.1:$ISCSI_PORT
224	tgt_rpc iscsi_create_initiator_group $INITIATOR_TAG $INITIATOR_NAME $NETMASK
225	tgt_rpc iscsi_create_target_node Target3 Target3_alias 'MallocForIscsi0:0' $PORTAL_TAG:$INITIATOR_TAG 64 -d
226	timing_exit "${FUNCNAME[0]}"
227}
228
229function create_nvmf_subsystem_config() {
230	timing_enter "${FUNCNAME[0]}"
231
232	NVMF_FIRST_TARGET_IP="127.0.0.1"
233	if [[ $SPDK_TEST_NVMF_TRANSPORT == "rdma" ]]; then
234		TEST_TRANSPORT=$SPDK_TEST_NVMF_TRANSPORT nvmftestinit
235	fi
236
237	if [[ -z $NVMF_FIRST_TARGET_IP ]]; then
238		echo "Error: no NIC for nvmf test"
239		return 1
240	fi
241
242	tgt_rpc bdev_malloc_create 8 512 --name MallocForNvmf0
243	tgt_rpc bdev_malloc_create 4 1024 --name MallocForNvmf1
244
245	tgt_rpc nvmf_create_transport -t $SPDK_TEST_NVMF_TRANSPORT -u 8192 -c 0
246	tgt_rpc nvmf_create_subsystem nqn.2016-06.io.spdk:cnode1 -a -s SPDK00000000000001
247	tgt_rpc nvmf_subsystem_add_ns nqn.2016-06.io.spdk:cnode1 MallocForNvmf0
248	tgt_rpc nvmf_subsystem_add_ns nqn.2016-06.io.spdk:cnode1 MallocForNvmf1
249	tgt_rpc nvmf_subsystem_add_listener nqn.2016-06.io.spdk:cnode1 -t $SPDK_TEST_NVMF_TRANSPORT -a $NVMF_FIRST_TARGET_IP -s "$NVMF_PORT"
250
251	timing_exit "${FUNCNAME[0]}"
252}
253
254function create_virtio_initiator_config() {
255	timing_enter "${FUNCNAME[0]}"
256	initiator_rpc bdev_virtio_attach_controller -t user -a /var/tmp/VhostScsiCtrlr0 -d scsi VirtioScsiCtrlr0
257	initiator_rpc bdev_virtio_attach_controller -t user -a /var/tmp/VhostBlkCtrlr0 -d blk VirtioBlk0
258	timing_exit "${FUNCNAME[0]}"
259}
260
261function json_config_test_init() {
262	timing_enter "${FUNCNAME[0]}"
263	timing_enter json_config_setup_target
264
265	json_config_test_start_app target --wait-for-rpc
266
267	#TODO: global subsystem params
268
269	create_accel_config
270
271	# Load nvme configuration. The load_config will issue framework_start_init automatically
272	(
273		$rootdir/scripts/gen_nvme.sh --json-with-subsystems
274	) | tgt_rpc load_config
275
276	tgt_check_notification_types
277
278	if [[ $SPDK_TEST_BLOCKDEV -eq 1 ]]; then
279		create_bdev_subsystem_config
280	fi
281
282	if [[ $SPDK_TEST_VHOST -eq 1 ]]; then
283		create_vhost_subsystem_config
284	fi
285
286	if [[ $SPDK_TEST_ISCSI -eq 1 ]]; then
287		create_iscsi_subsystem_config
288	fi
289
290	if [[ $SPDK_TEST_NVMF -eq 1 ]]; then
291		create_nvmf_subsystem_config
292	fi
293	timing_exit json_config_setup_target
294
295	if [[ $SPDK_TEST_VHOST_INIT -eq 1 ]]; then
296		json_config_test_start_app initiator
297		create_virtio_initiator_config
298	fi
299
300	tgt_rpc bdev_malloc_create 8 512 --name MallocBdevForConfigChangeCheck
301
302	timing_exit "${FUNCNAME[0]}"
303}
304
305function json_config_test_fini() {
306	timing_enter "${FUNCNAME[0]}"
307	local ret=0
308
309	if [[ -n "${app_pid[initiator]}" ]]; then
310		if [[ $SPDK_TEST_VHOST_INIT -eq 1 ]]; then
311			initiator_rpc bdev_virtio_detach_controller VirtioScsiCtrlr0 || :
312			initiator_rpc bdev_virtio_detach_controller VirtioBlk0 || :
313		fi
314		killprocess ${app_pid[initiator]}
315	fi
316
317	if [[ -n "${app_pid[target]}" ]]; then
318
319		# Remove any artifacts we created (files, lvol etc)
320		cleanup_bdev_subsystem_config
321
322		# SPDK_TEST_NVMF: Should we clear something?
323		killprocess ${app_pid[target]}
324	fi
325
326	rm -f "${configs_path[@]}"
327	timing_exit "${FUNCNAME[0]}"
328	return $ret
329}
330
331function json_config_clear() {
332	[[ -n "${#app_socket[$1]}" ]] # Check app type
333	$rootdir/test/json_config/clear_config.py -s ${app_socket[$1]} clear_config
334
335	# Check if config is clean.
336	# Global params can't be cleared so need to filter them out.
337	local config_filter="$rootdir/test/json_config/config_filter.py"
338
339	# RPC's used to cleanup configuration (e.g. to delete split and nvme bdevs)
340	# complete immediately and they don't wait for the unregister callback.
341	# It causes that configuration may not be fully cleaned at this moment and
342	# we should to wait a while. (See github issue #789)
343	count=100
344	while [ $count -gt 0 ]; do
345		$rootdir/scripts/rpc.py -s "${app_socket[$1]}" save_config | $config_filter -method delete_global_parameters | $config_filter -method check_empty && break
346		count=$((count - 1))
347		sleep 0.1
348	done
349
350	if [ $count -eq 0 ]; then
351		return 1
352	fi
353}
354
355trap 'on_error_exit "${FUNCNAME}" "${LINENO}"' ERR
356echo "INFO: JSON configuration test init"
357json_config_test_init
358
359tgt_rpc save_config > ${configs_path[target]}
360
361echo "INFO: shutting down applications..."
362if [[ $SPDK_TEST_VHOST_INIT -eq 1 ]]; then
363	initiator_rpc save_config > ${configs_path[initiator]}
364	json_config_clear initiator
365	json_config_test_shutdown_app initiator
366fi
367
368json_config_clear target
369json_config_test_shutdown_app target
370
371echo "INFO: relaunching applications..."
372json_config_test_start_app target --json ${configs_path[target]}
373if [[ $SPDK_TEST_VHOST_INIT -eq 1 ]]; then
374	json_config_test_start_app initiator --json ${configs_path[initiator]}
375fi
376
377echo "INFO: Checking if target configuration is the same..."
378$rootdir/test/json_config/json_diff.sh <(tgt_rpc save_config) "${configs_path[target]}"
379if [[ $SPDK_TEST_VHOST_INIT -eq 1 ]]; then
380	echo "INFO: Checking if virtio initiator configuration is the same..."
381	$rootdir/test/json_config/json_diff.sh <(initiator_rpc save_config) "${configs_path[initiator]}"
382fi
383
384echo "INFO: changing configuration and checking if this can be detected..."
385# Self test to check if configuration diff can be detected.
386tgt_rpc bdev_malloc_delete MallocBdevForConfigChangeCheck
387if $rootdir/test/json_config/json_diff.sh <(tgt_rpc save_config) "${configs_path[target]}" > /dev/null; then
388	echo "ERROR: intentional configuration difference not detected!"
389	false
390else
391	echo "INFO: configuration change detected."
392fi
393
394json_config_test_fini
395
396echo "INFO: Success"
397