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