]> git.proxmox.com Git - ceph.git/blame - ceph/src/spdk/test/iscsi_tgt/sock/sock.sh
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / spdk / test / iscsi_tgt / sock / sock.sh
CommitLineData
9f95a23c
TL
1#!/usr/bin/env bash
2
3testdir=$(readlink -f $(dirname $0))
4rootdir=$(readlink -f $testdir/../../..)
5source $rootdir/test/common/autotest_common.sh
6source $rootdir/test/iscsi_tgt/common.sh
7
f67539c2
TL
8function waitfortcp() {
9 local addr="$2"
10
11 if hash ip &> /dev/null; then
12 local have_ip_cmd=true
13 else
14 local have_ip_cmd=false
15 fi
16
17 if hash ss &> /dev/null; then
18 local have_ss_cmd=true
19 else
20 local have_ss_cmd=false
21 fi
22
23 echo "Waiting for process to start up and listen on address $addr..."
24 # turn off trace for this loop
25 xtrace_disable
26 local ret=0
27 local i
28 for ((i = 40; i != 0; i--)); do
29 # if the process is no longer running, then exit the script
30 # since it means the application crashed
31 if ! kill -s 0 $1; then
32 echo "ERROR: process (pid: $1) is no longer running"
33 ret=1
34 break
35 fi
36
37 if $have_ip_cmd; then
38 namespace=$(ip netns identify $1)
39 if [ -n "$namespace" ]; then
40 ns_cmd="ip netns exec $namespace"
41 fi
42 fi
43
44 if $have_ss_cmd; then
45 if $ns_cmd ss -ln | grep -E -q "\s+$addr\s+"; then
46 break
47 fi
48 elif [[ "$(uname -s)" == "Linux" ]]; then
49 # For Linux, if system doesn't have ss, just assume it has netstat
50 if $ns_cmd netstat -an | grep -iw LISTENING | grep -E -q "\s+$addr\$"; then
51 break
52 fi
53 fi
54 sleep 0.5
55 done
56
57 xtrace_restore
58 if ((i == 0)); then
59 echo "ERROR: timeout while waiting for process (pid: $1) to start listening on '$addr'"
60 ret=1
61 fi
62 return $ret
63}
64
9f95a23c
TL
65# $1 = "iso" - triggers isolation mode (setting up required environment).
66# $2 = test type posix or vpp. defaults to posix.
67iscsitestinit $1 $2
68
f67539c2
TL
69if [ "$1" == "iso" ]; then
70 TEST_TYPE=$2
71else
72 TEST_TYPE=$1
73fi
74
75if [ -z "$TEST_TYPE" ]; then
76 TEST_TYPE="posix"
77fi
78
79if [ "$TEST_TYPE" != "posix" ] && [ "$TEST_TYPE" != "vpp" ]; then
80 echo "No correct sock implmentation specified"
81 exit 1
82fi
83
84HELLO_SOCK_APP="${TARGET_NS_CMD[*]} $SPDK_EXAMPLE_DIR/hello_sock"
85if [ $SPDK_TEST_VPP -eq 1 ]; then
86 HELLO_SOCK_APP+=" -L sock_vpp"
87fi
9f95a23c
TL
88SOCAT_APP="socat"
89
90# ----------------
91# Test client path
92# ----------------
93timing_enter sock_client
94echo "Testing client path"
95
96# start echo server using socat
f67539c2
TL
97$SOCAT_APP tcp-l:$ISCSI_PORT,fork,bind=$INITIATOR_IP exec:'/bin/cat' &
98server_pid=$!
99trap 'killprocess $server_pid;iscsitestfini $1 $2; exit 1' SIGINT SIGTERM EXIT
9f95a23c 100
f67539c2 101waitfortcp $server_pid $INITIATOR_IP:$ISCSI_PORT
9f95a23c
TL
102
103# send message using hello_sock client
104message="**MESSAGE:This is a test message from the client**"
f67539c2 105response=$(echo $message | $HELLO_SOCK_APP -H $INITIATOR_IP -P $ISCSI_PORT -N $TEST_TYPE)
9f95a23c
TL
106
107if ! echo "$response" | grep -q "$message"; then
108 exit 1
109fi
110
111trap '-' SIGINT SIGTERM EXIT
112# NOTE: socat returns code 143 on SIGINT
113killprocess $server_pid || true
114
9f95a23c
TL
115timing_exit sock_client
116
117# ----------------
118# Test server path
119# ----------------
120
121timing_enter sock_server
122
123# start echo server using hello_sock echo server
f67539c2
TL
124$HELLO_SOCK_APP -H $TARGET_IP -P $ISCSI_PORT -S -N $TEST_TYPE &
125server_pid=$!
126trap 'killprocess $server_pid; iscsitestfini $1 $2; exit 1' SIGINT SIGTERM EXIT
9f95a23c
TL
127waitforlisten $server_pid
128
129# send message to server using socat
130message="**MESSAGE:This is a test message to the server**"
f67539c2 131response=$(echo $message | $SOCAT_APP - tcp:$TARGET_IP:$ISCSI_PORT 2> /dev/null)
9f95a23c
TL
132
133if [ "$message" != "$response" ]; then
134 exit 1
135fi
136
137trap - SIGINT SIGTERM EXIT
138
139killprocess $server_pid
140
141iscsitestfini $1 $2
9f95a23c 142timing_exit sock_server