xref: /openbmc/qemu/tests/qemu-iotests/083 (revision ea81ca9d)
1dc668dedSStefan Hajnoczi#!/bin/bash
2dc668dedSStefan Hajnoczi#
3dc668dedSStefan Hajnoczi# Test NBD client unexpected disconnect
4dc668dedSStefan Hajnoczi#
5dc668dedSStefan Hajnoczi# Copyright Red Hat, Inc. 2014
6dc668dedSStefan Hajnoczi#
7dc668dedSStefan Hajnoczi# This program is free software; you can redistribute it and/or modify
8dc668dedSStefan Hajnoczi# it under the terms of the GNU General Public License as published by
9dc668dedSStefan Hajnoczi# the Free Software Foundation; either version 2 of the License, or
10dc668dedSStefan Hajnoczi# (at your option) any later version.
11dc668dedSStefan Hajnoczi#
12dc668dedSStefan Hajnoczi# This program is distributed in the hope that it will be useful,
13dc668dedSStefan Hajnoczi# but WITHOUT ANY WARRANTY; without even the implied warranty of
14dc668dedSStefan Hajnoczi# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15dc668dedSStefan Hajnoczi# GNU General Public License for more details.
16dc668dedSStefan Hajnoczi#
17dc668dedSStefan Hajnoczi# You should have received a copy of the GNU General Public License
18dc668dedSStefan Hajnoczi# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19dc668dedSStefan Hajnoczi#
20dc668dedSStefan Hajnoczi
21dc668dedSStefan Hajnoczi# creator
22dc668dedSStefan Hajnocziowner=stefanha@redhat.com
23dc668dedSStefan Hajnoczi
24dc668dedSStefan Hajnocziseq=`basename $0`
25dc668dedSStefan Hajnocziecho "QA output created by $seq"
26dc668dedSStefan Hajnoczi
27dc668dedSStefan Hajnoczihere=`pwd`
28dc668dedSStefan Hajnoczitmp=/tmp/$$
29dc668dedSStefan Hajnoczistatus=1	# failure is the default!
30dc668dedSStefan Hajnoczi
31dc668dedSStefan Hajnoczi# get standard environment, filters and checks
32dc668dedSStefan Hajnoczi. ./common.rc
33dc668dedSStefan Hajnoczi. ./common.filter
34dc668dedSStefan Hajnoczi
35dc668dedSStefan Hajnoczi_supported_fmt generic
36dc668dedSStefan Hajnoczi_supported_proto nbd
37dc668dedSStefan Hajnoczi_supported_os Linux
38dc668dedSStefan Hajnoczi
39dc668dedSStefan Hajnoczi# Pick a TCP port based on our pid.  This way multiple instances of this test
40dc668dedSStefan Hajnoczi# can run in parallel without conflicting.
41dc668dedSStefan Hajnoczichoose_tcp_port() {
42dc668dedSStefan Hajnoczi	echo $((($$ % 31744) + 1024)) # 1024 <= port < 32768
43dc668dedSStefan Hajnoczi}
44dc668dedSStefan Hajnoczi
45dc668dedSStefan Hajnocziwait_for_tcp_port() {
46dc668dedSStefan Hajnoczi	while ! (netstat --tcp --listening --numeric | \
47dc668dedSStefan Hajnoczi		 grep "$1.*0.0.0.0:\*.*LISTEN") 2>&1 >/dev/null; do
48dc668dedSStefan Hajnoczi		sleep 0.1
49dc668dedSStefan Hajnoczi	done
50dc668dedSStefan Hajnoczi}
51dc668dedSStefan Hajnoczi
52dc668dedSStefan Hajnoczifilter_nbd() {
53dc668dedSStefan Hajnoczi	# nbd.c error messages contain function names and line numbers that are prone
54dc668dedSStefan Hajnoczi	# to change.  Message ordering depends on timing between send and receive
55dc668dedSStefan Hajnoczi	# callbacks sometimes, making them unreliable.
56dc668dedSStefan Hajnoczi	#
57dc668dedSStefan Hajnoczi	# Filter out the TCP port number since this changes between runs.
58dc668dedSStefan Hajnoczi	sed -e 's#^nbd.c:.*##g' \
59dc668dedSStefan Hajnoczi	    -e 's#nbd:127.0.0.1:[^:]*:#nbd:127.0.0.1:PORT:#g'
60dc668dedSStefan Hajnoczi}
61dc668dedSStefan Hajnoczi
62dc668dedSStefan Hajnoczicheck_disconnect() {
63dc668dedSStefan Hajnoczi	event=$1
64dc668dedSStefan Hajnoczi	when=$2
65dc668dedSStefan Hajnoczi	negotiation=$3
66dc668dedSStefan Hajnoczi	echo "=== Check disconnect $when $event ==="
67dc668dedSStefan Hajnoczi	echo
68dc668dedSStefan Hajnoczi
69dc668dedSStefan Hajnoczi	port=$(choose_tcp_port)
70dc668dedSStefan Hajnoczi
71dc668dedSStefan Hajnoczi	cat > "$TEST_DIR/nbd-fault-injector.conf" <<EOF
72dc668dedSStefan Hajnoczi[inject-error]
73dc668dedSStefan Hajnoczievent=$event
74dc668dedSStefan Hajnocziwhen=$when
75dc668dedSStefan HajnocziEOF
76dc668dedSStefan Hajnoczi
77dc668dedSStefan Hajnoczi	if [ "$negotiation" = "--classic-negotiation" ]; then
78dc668dedSStefan Hajnoczi		extra_args=--classic-negotiation
79dc668dedSStefan Hajnoczi		nbd_url="nbd:127.0.0.1:$port"
80dc668dedSStefan Hajnoczi	else
81dc668dedSStefan Hajnoczi		nbd_url="nbd:127.0.0.1:$port:exportname=foo"
82dc668dedSStefan Hajnoczi	fi
83dc668dedSStefan Hajnoczi
84*ea81ca9dSMax Reitz	$PYTHON nbd-fault-injector.py $extra_args "127.0.0.1:$port" "$TEST_DIR/nbd-fault-injector.conf" 2>&1 >/dev/null &
85dc668dedSStefan Hajnoczi	wait_for_tcp_port "127.0.0.1:$port"
86dc668dedSStefan Hajnoczi	$QEMU_IO -c "read 0 512" "$nbd_url" 2>&1 | _filter_qemu_io | filter_nbd
87dc668dedSStefan Hajnoczi
88dc668dedSStefan Hajnoczi	echo
89dc668dedSStefan Hajnoczi}
90dc668dedSStefan Hajnoczi
91dc668dedSStefan Hajnoczifor event in neg1 "export" neg2 request reply data; do
92dc668dedSStefan Hajnoczi	for when in before after; do
93dc668dedSStefan Hajnoczi		check_disconnect "$event" "$when"
94dc668dedSStefan Hajnoczi	done
95dc668dedSStefan Hajnoczi
96dc668dedSStefan Hajnoczi	# Also inject short replies from the NBD server
97dc668dedSStefan Hajnoczi	case "$event" in
98dc668dedSStefan Hajnoczi	neg1)
99dc668dedSStefan Hajnoczi		for when in 8 16; do
100dc668dedSStefan Hajnoczi			check_disconnect "$event" "$when"
101dc668dedSStefan Hajnoczi		done
102dc668dedSStefan Hajnoczi		;;
103dc668dedSStefan Hajnoczi	"export")
104dc668dedSStefan Hajnoczi		for when in 4 12 16; do
105dc668dedSStefan Hajnoczi			check_disconnect "$event" "$when"
106dc668dedSStefan Hajnoczi		done
107dc668dedSStefan Hajnoczi		;;
108dc668dedSStefan Hajnoczi	neg2)
109dc668dedSStefan Hajnoczi		for when in 8 10; do
110dc668dedSStefan Hajnoczi			check_disconnect "$event" "$when"
111dc668dedSStefan Hajnoczi		done
112dc668dedSStefan Hajnoczi		;;
113dc668dedSStefan Hajnoczi	reply)
114dc668dedSStefan Hajnoczi		for when in 4 8; do
115dc668dedSStefan Hajnoczi			check_disconnect "$event" "$when"
116dc668dedSStefan Hajnoczi		done
117dc668dedSStefan Hajnoczi		;;
118dc668dedSStefan Hajnoczi	esac
119dc668dedSStefan Hajnoczidone
120dc668dedSStefan Hajnoczi
121dc668dedSStefan Hajnoczi# Also check classic negotiation without export information
122dc668dedSStefan Hajnoczifor when in before 8 16 24 28 after; do
123dc668dedSStefan Hajnoczi	check_disconnect "neg-classic" "$when" --classic-negotiation
124dc668dedSStefan Hajnoczidone
125dc668dedSStefan Hajnoczi
126dc668dedSStefan Hajnoczi# success, all done
127dc668dedSStefan Hajnocziecho "*** done"
128dc668dedSStefan Hajnoczirm -f $seq.full
129dc668dedSStefan Hajnoczistatus=0
130