xref: /spdk/test/vhost/hotplug/common.sh (revision c7a40fcb25a7a49405c7469199c6a5cf592a26e0)
1#  SPDX-License-Identifier: BSD-3-Clause
2#  Copyright (C) 2017 Intel Corporation
3#  All rights reserved.
4#
5
6testdir=$(readlink -f $(dirname $0))
7rootdir=$(readlink -f $testdir/../../..)
8source $rootdir/test/common/autotest_common.sh
9source $rootdir/test/vhost/common.sh
10
11dry_run=false
12no_shutdown=false
13fio_bin="fio"
14fio_jobs="$testdir/fio_jobs/"
15test_type=spdk_vhost_scsi
16reuse_vms=false
17vms=()
18used_vms=""
19disk_split=""
20x=""
21scsi_hot_remove_test=0
22blk_hot_remove_test=0
23readonly=""
24
25function usage() {
26	[[ -n $2 ]] && (
27		echo "$2"
28		echo ""
29	)
30	echo "Shortcut script for doing automated hotattach/hotdetach test"
31	echo "Usage: $(basename $1) [OPTIONS]"
32	echo
33	echo "-h, --help                print help and exit"
34	echo "    --test-type=TYPE      Perform specified test:"
35	echo "                          virtio - test host virtio-scsi-pci using file as disk image"
36	echo "                          kernel_vhost - use kernel driver vhost-scsi"
37	echo "                          spdk_vhost_scsi - use spdk vhost scsi"
38	echo "                          spdk_vhost_blk - use spdk vhost block"
39	echo "-x                        set -x for script debug"
40	echo "    --fio-bin=FIO         Use specific fio binary (will be uploaded to VM)"
41	echo "    --fio-jobs=           Fio configs to use for tests. Can point to a directory or"
42	echo "    --vm=NUM[,OS][,DISKS] VM configuration. This parameter might be used more than once:"
43	echo "                          NUM - VM number (mandatory)"
44	echo "                          OS - VM os disk path (optional)"
45	echo "                          DISKS - VM os test disks/devices path (virtio - optional, kernel_vhost - mandatory)"
46	echo "    --scsi-hotremove-test Run scsi hotremove tests"
47	echo "    --readonly            Use readonly for fio"
48	exit 0
49}
50
51while getopts 'xh-:' optchar; do
52	case "$optchar" in
53		-)
54			case "$OPTARG" in
55				help) usage $0 ;;
56				fio-bin=*) fio_bin="${OPTARG#*=}" ;;
57				fio-jobs=*) fio_jobs="${OPTARG#*=}" ;;
58				test-type=*) test_type="${OPTARG#*=}" ;;
59				vm=*) vms+=("${OPTARG#*=}") ;;
60				scsi-hotremove-test) scsi_hot_remove_test=1 ;;
61				blk-hotremove-test) blk_hot_remove_test=1 ;;
62				readonly) readonly="--readonly" ;;
63				*) usage $0 "Invalid argument '$OPTARG'" ;;
64			esac
65			;;
66		h) usage $0 ;;
67		x)
68			set -x
69			x="-x"
70			;;
71		*) usage $0 "Invalid argument '$OPTARG'" ;;
72	esac
73done
74shift $((OPTIND - 1))
75
76fio_job=$testdir/fio_jobs/default_integrity.job
77tmp_attach_job=$testdir/fio_jobs/fio_attach.job.tmp
78tmp_detach_job=$testdir/fio_jobs/fio_detach.job.tmp
79
80rpc_py="$rootdir/scripts/rpc.py -s $(get_vhost_dir 0)/rpc.sock"
81
82function print_test_fio_header() {
83	notice "==============="
84	notice ""
85	notice "Testing..."
86
87	notice "Running fio jobs ..."
88	if [ $# -gt 0 ]; then
89		echo $1
90	fi
91}
92
93function vms_setup() {
94	for vm_conf in "${vms[@]}"; do
95		IFS=',' read -ra conf <<< "$vm_conf"
96		if [[ -z ${conf[0]} ]] || ! assert_number ${conf[0]}; then
97			fail "invalid VM configuration syntax $vm_conf"
98		fi
99
100		# Sanity check if VM is not defined twice
101		for vm_num in $used_vms; do
102			if [[ $vm_num -eq ${conf[0]} ]]; then
103				fail "VM$vm_num defined more than twice ( $(printf "'%s' " "${vms[@]}"))!"
104			fi
105		done
106
107		used_vms+=" ${conf[0]}"
108
109		setup_cmd="vm_setup --disk-type=$test_type --force=${conf[0]}"
110		[[ x"${conf[1]}" != x"" ]] && setup_cmd+=" --os=${conf[1]}"
111		[[ x"${conf[2]}" != x"" ]] && setup_cmd+=" --disks=${conf[2]}"
112		$setup_cmd
113	done
114}
115
116function vm_run_with_arg() {
117	local vms_to_run="$*"
118	vm_run $vms_to_run
119	vm_wait_for_boot 300 $vms_to_run
120}
121
122function vms_setup_and_run() {
123	local vms_to_run="$*"
124	vms_setup
125	vm_run_with_arg $vms_to_run
126}
127
128function vms_prepare() {
129	for vm_num in $1; do
130		qemu_mask_param="VM_${vm_num}_qemu_mask"
131
132		host_name="VM-${vm_num}-${!qemu_mask_param}"
133		notice "Setting up hostname: $host_name"
134		vm_exec $vm_num "hostname $host_name"
135		vm_start_fio_server --fio-bin=$fio_bin $readonly $vm_num
136	done
137}
138
139function vms_reboot_all() {
140	notice "Rebooting all vms "
141	for vm_num in $1; do
142		VM_SSH_OPTIONS="-oServerAliveInterval=1" vm_exec $vm_num "reboot -f" || true
143	done
144
145	vm_wait_for_boot 300 $1
146}
147
148function check_fio_retcode() {
149	local fio_retcode=$3
150	echo $1
151	local retcode_expected=$2
152	if [ $retcode_expected == 0 ]; then
153		if [ $fio_retcode != 0 ]; then
154			error "    Fio test ended with error."
155		else
156			notice "    Fio test ended with success."
157		fi
158	else
159		if [ $fio_retcode != 0 ]; then
160			notice "    Fio test ended with expected error."
161		else
162			error "    Fio test ended with unexpected success."
163		fi
164	fi
165}
166
167function wait_for_finish() {
168	local wait_for_pid=$1
169	local sequence=${2:-30}
170	for i in $(seq 1 $sequence); do
171		if kill -0 $wait_for_pid; then
172			sleep 0.5
173			continue
174		else
175			break
176		fi
177	done
178	if kill -0 $wait_for_pid; then
179		error "Timeout for fio command"
180	fi
181
182	wait $wait_for_pid
183}
184
185function reboot_all_and_prepare() {
186	vms_reboot_all "$1"
187	vms_prepare "$1"
188}
189
190function post_test_case() {
191	vm_shutdown_all
192	vhost_kill 0
193}
194
195function on_error_exit() {
196	set +e
197	echo "Error on $1 - $2"
198	post_test_case
199	print_backtrace
200	exit 1
201}
202
203function check_disks() {
204	if [ "$1" == "$2" ]; then
205		echo "Disk has not been deleted"
206		exit 1
207	fi
208}
209
210function get_traddr() {
211	local nvme_name=$1
212	local nvme
213
214	nvme="$($rootdir/scripts/gen_nvme.sh)"
215	traddr=$(jq -r ".config[] | select(.params.name == \"$nvme_name\") | .params.traddr" <<< "$nvme")
216	[[ -n $traddr ]] || return 1
217}
218
219function delete_nvme() {
220	$rpc_py bdev_nvme_detach_controller $1
221}
222
223function add_nvme() {
224	$rpc_py bdev_nvme_attach_controller -b $1 -t PCIe -a $2
225}
226