xref: /spdk/test/vhost/migration/migration-tc2.sh (revision 32999ab917f67af61872f868585fd3d78ad6fb8a)
1source $rootdir/test/nvmf/common.sh
2
3function migration_tc2_cleanup_nvmf_tgt() {
4	process_shm --id $NVMF_APP_SHM_ID
5	nvmftestfini
6}
7
8function migration_tc2_cleanup_vhost_config() {
9	timing_enter migration_tc2_cleanup_vhost_config
10
11	notice "Shutting down all VMs"
12	vm_shutdown_all
13
14	notice "Removing vhost devices & controllers via RPC ..."
15	# Delete bdev first to remove all LUNs and SCSI targets
16	$rpc_0 bdev_nvme_detach_controller Nvme0
17	$rpc_0 vhost_delete_controller $incoming_vm_ctrlr
18
19	$rpc_1 delete_nvme_controller Nvme0
20	$rpc_1 vhost_delete_controller $target_vm_ctrlr
21
22	notice "killing vhost app"
23	vhost_kill 0
24	vhost_kill 1
25
26	unset -v incoming_vm target_vm incoming_vm_ctrlr target_vm_ctrlr
27	unset -v rpc_0 rpc_1
28
29	migration_tc2_cleanup_nvmf_tgt
30
31	timing_exit migration_tc2_cleanup_vhost_config
32}
33
34function migration_tc2_configure_vhost() {
35	timing_enter migration_tc2_configure_vhost
36
37	TEST_TRANSPORT=rdma TEST_MODE=iso nvmftestinit
38
39	# Those are global intentionally - they will be unset in cleanup handler
40
41	incoming_vm=1
42	target_vm=2
43	incoming_vm_ctrlr=naa.VhostScsi0.$incoming_vm
44	target_vm_ctrlr=naa.VhostScsi0.$target_vm
45
46	rpc_0="$rootdir/scripts/rpc.py -s $(get_vhost_dir 0)/rpc.sock"
47	rpc_1="$rootdir/scripts/rpc.py -s $(get_vhost_dir 1)/rpc.sock"
48
49	# Run nvmf_tgt and two vhost instances:
50	# nvmf_tgt uses core id 2 (-m 0x4)
51	# First uses core id 0
52	# Second uses core id 1
53	# This force to use VM 1 and 2.
54	timing_enter start_nvmf_tgt
55	notice "Running nvmf_tgt..."
56	nvmfappstart -s 512 -m 0x4 --wait-for-rpc
57	# Override the trap set in place via nvmfappstart()
58	trap 'migration_tc2_error_cleanup; error_exit "${FUNCNAME}" "${LINENO}"' INT ERR EXIT
59	rpc_cmd framework_start_init
60	rpc_cmd nvmf_create_transport $NVMF_TRANSPORT_OPTS -u 8192
61	mapfile -t json < <("$rootdir/scripts/gen_nvme.sh")
62	rpc_cmd load_subsystem_config -j "'${json[*]}'"
63	timing_exit start_nvmf_tgt
64
65	vhost_run -n 0 -a "-m 0x1 -s 512 -u"
66	vhost_run -n 1 -a "-m 0x2 -s 512 -u"
67
68	local rdma_ip_list
69	local nvmf_target_ip
70	rdma_ip_list=$(get_available_rdma_ips)
71	nvmf_target_ip=$(echo "$rdma_ip_list" | head -n 1)
72
73	if [[ -z "$nvmf_target_ip" ]]; then
74		fail "no NIC for nvmf target"
75	fi
76
77	notice "Configuring nvmf_tgt, vhost devices & controllers via RPC ..."
78
79	# Construct shared bdevs and controllers
80	rpc_cmd nvmf_create_subsystem nqn.2016-06.io.spdk:cnode1 -a -s SPDK00000000000001
81	rpc_cmd nvmf_subsystem_add_ns nqn.2016-06.io.spdk:cnode1 Nvme0n1
82	rpc_cmd nvmf_subsystem_add_listener nqn.2016-06.io.spdk:cnode1 -t rdma -a $nvmf_target_ip -s 4420
83
84	$rpc_0 bdev_nvme_attach_controller -b Nvme0 -t rdma -f ipv4 -a $nvmf_target_ip -s 4420 -n "nqn.2016-06.io.spdk:cnode1"
85	$rpc_0 vhost_create_scsi_controller $incoming_vm_ctrlr
86	$rpc_0 vhost_scsi_controller_add_target $incoming_vm_ctrlr 0 Nvme0n1
87
88	$rpc_1 bdev_nvme_attach_controller -b Nvme0 -t rdma -f ipv4 -a $nvmf_target_ip -s 4420 -n "nqn.2016-06.io.spdk:cnode1"
89	$rpc_1 vhost_create_scsi_controller $target_vm_ctrlr
90	$rpc_1 vhost_scsi_controller_add_target $target_vm_ctrlr 0 Nvme0n1
91
92	notice "Setting up VMs"
93	vm_setup --os="$os_image" --force=$incoming_vm --disk-type=spdk_vhost_scsi --disks=VhostScsi0 \
94		--migrate-to=$target_vm --memory=1024 --vhost-name=0
95	vm_setup --force=$target_vm --disk-type=spdk_vhost_scsi --disks=VhostScsi0 --incoming=$incoming_vm --memory=1024 \
96		--vhost-name=1
97
98	# Run everything
99	vm_run $incoming_vm $target_vm
100
101	# Wait only for incoming VM, as target is waiting for migration
102	vm_wait_for_boot 300 $incoming_vm
103
104	notice "Configuration done"
105
106	timing_exit migration_tc2_configure_vhost
107}
108
109function migration_tc2_error_cleanup() {
110	vm_kill_all
111	migration_tc2_cleanup_nvmf_tgt
112	migration_tc2_cleanup_vhost_config
113	notice "Migration TC2 FAILED"
114}
115
116function migration_tc2() {
117	# Use 2 VMs:
118	# incoming VM - the one we want to migrate
119	# targe VM - the one which will accept migration
120	local job_file="$testdir/migration-tc2.job"
121	local log_file
122	log_file="/root/$(basename ${job_file%%.*}).log"
123
124	migration_tc2_configure_vhost
125
126	# Run fio before migration
127	notice "Starting FIO"
128	vm_check_scsi_location $incoming_vm
129	run_fio $fio_bin --job-file="$job_file" --no-wait-for-fio --local --vm="${incoming_vm}$(printf ':/dev/%s' $SCSI_DISK)"
130
131	# Wait a while to let the FIO time to issue some IO
132	sleep 5
133
134	# Check if fio is still running before migration
135	if ! is_fio_running $incoming_vm; then
136		vm_exec $incoming_vm "cat $log_file"
137		error "FIO is not running before migration: process crashed or finished too early"
138	fi
139
140	vm_migrate $incoming_vm
141	sleep 3
142
143	# Check if fio is still running after migration
144	if ! is_fio_running $target_vm; then
145		vm_exec $target_vm "cat $log_file"
146		error "FIO is not running after migration: process crashed or finished too early"
147	fi
148
149	notice "Waiting for fio to finish"
150	local timeout=20
151	while is_fio_running $target_vm; do
152		sleep 1
153		echo -n "."
154		if ((timeout-- == 0)); then
155			error "timeout while waiting for FIO!"
156		fi
157	done
158
159	notice "Fio result is:"
160	vm_exec $target_vm "cat $log_file"
161
162	migration_tc2_cleanup_vhost_config
163	notice "Migration TC2 SUCCESS"
164}
165