]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/test/iscsi_tgt/common.sh
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / spdk / test / iscsi_tgt / common.sh
1 # Network configuration
2 TARGET_INTERFACE="spdk_tgt_int"
3 INITIATOR_INTERFACE="spdk_init_int"
4 TARGET_NAMESPACE="spdk_iscsi_ns"
5 TARGET_NS_CMD=(ip netns exec "$TARGET_NAMESPACE")
6
7 # iSCSI target configuration
8 TARGET_IP=10.0.0.1
9 INITIATOR_IP=10.0.0.2
10 ISCSI_PORT=3260
11 NETMASK=$INITIATOR_IP/32
12 INITIATOR_TAG=2
13 INITIATOR_NAME=ANY
14 PORTAL_TAG=1
15 ISCSI_APP=("${TARGET_NS_CMD[@]}" "${ISCSI_APP[@]}")
16 if [ $SPDK_TEST_VPP -eq 1 ]; then
17 ISCSI_APP+=(-L sock_vpp)
18 fi
19 ISCSI_TEST_CORE_MASK=0xFF
20
21 function create_veth_interfaces() {
22 # $1 = test type (posix/vpp)
23 ip netns del $TARGET_NAMESPACE || true
24 ip link delete $INITIATOR_INTERFACE || true
25
26 trap 'cleanup_veth_interfaces $1; exit 1' SIGINT SIGTERM EXIT
27
28 # Create veth (Virtual ethernet) interface pair
29 ip link add $INITIATOR_INTERFACE type veth peer name $TARGET_INTERFACE
30 ip addr add $INITIATOR_IP/24 dev $INITIATOR_INTERFACE
31 ip link set $INITIATOR_INTERFACE up
32
33 # Create and add interface for target to network namespace
34 ip netns add $TARGET_NAMESPACE
35 ip link set $TARGET_INTERFACE netns $TARGET_NAMESPACE
36
37 # Accept connections from veth interface
38 iptables -I INPUT 1 -i $INITIATOR_INTERFACE -p tcp --dport $ISCSI_PORT -j ACCEPT
39
40 "${TARGET_NS_CMD[@]}" ip link set $TARGET_INTERFACE up
41
42 if [ "$1" == "posix" ]; then
43 "${TARGET_NS_CMD[@]}" ip link set lo up
44 "${TARGET_NS_CMD[@]}" ip addr add $TARGET_IP/24 dev $TARGET_INTERFACE
45
46 # Verify connectivity
47 ping -c 1 $TARGET_IP
48 ip netns exec $TARGET_NAMESPACE ping -c 1 $INITIATOR_IP
49 else
50 start_vpp
51 fi
52 }
53
54 function cleanup_veth_interfaces() {
55 # $1 = test type (posix/vpp)
56 if [ "$1" == "vpp" ]; then
57 kill_vpp
58 fi
59
60 # Cleanup veth interfaces and network namespace
61 # Note: removing one veth, removes the pair
62 ip link delete $INITIATOR_INTERFACE
63 ip netns del $TARGET_NAMESPACE
64 }
65
66 function iscsitestinit() {
67 if [ "$1" == "iso" ]; then
68 $rootdir/scripts/setup.sh
69 if [ -n "$2" ]; then
70 create_veth_interfaces $2
71 else
72 # default to posix
73 create_veth_interfaces "posix"
74 fi
75 fi
76 }
77
78 function waitforiscsidevices() {
79 local num=$1
80
81 for ((i = 1; i <= 20; i++)); do
82 n=$(iscsiadm -m session -P 3 | grep -c "Attached scsi disk sd[a-z]*" || true)
83 if [ $n -ne $num ]; then
84 sleep 0.1
85 else
86 return 0
87 fi
88 done
89
90 return 1
91 }
92
93 function iscsitestfini() {
94 if [ "$1" == "iso" ]; then
95 if [ -n "$2" ]; then
96 cleanup_veth_interfaces $2
97 else
98 # default to posix
99 cleanup_veth_interfaces "posix"
100 fi
101 $rootdir/scripts/setup.sh reset
102 fi
103 }
104
105 function start_vpp() {
106 # We need to make sure that posix side doesn't send jumbo packets while
107 # for VPP side maximal size of MTU for TCP is 1460 and tests doesn't work
108 # stable with larger packets
109 MTU=1460
110 MTU_W_HEADER=$((MTU + 20))
111 ip link set dev $INITIATOR_INTERFACE mtu $MTU
112 ethtool -K $INITIATOR_INTERFACE tso off
113 ethtool -k $INITIATOR_INTERFACE
114
115 # Start VPP process in SPDK target network namespace
116 "${TARGET_NS_CMD[@]}" vpp \
117 unix { nodaemon cli-listen /run/vpp/cli.sock } \
118 dpdk { no-pci } \
119 session { evt_qs_memfd_seg } \
120 socksvr { socket-name /run/vpp-api.sock } \
121 plugins { \
122 plugin default { disable } \
123 plugin dpdk_plugin.so { enable } \
124 } &
125
126 vpp_pid=$!
127 echo "VPP Process pid: $vpp_pid"
128
129 gdb_attach $vpp_pid &
130
131 # Wait until VPP starts responding
132 xtrace_disable
133 counter=40
134 while [ $counter -gt 0 ]; do
135 vppctl show version | grep -E "vpp v[0-9]+\.[0-9]+" && break
136 counter=$((counter - 1))
137 sleep 0.5
138 done
139 xtrace_restore
140 if [ $counter -eq 0 ]; then
141 return 1
142 fi
143
144 # Below VPP commands are masked with "|| true" for the sake of
145 # running the test in the CI system. For reasons unknown when
146 # run via CI these commands result in 141 return code (pipefail)
147 # even despite producing valid output.
148 # Using "|| true" does not impact the "-e" flag used in test scripts
149 # because vppctl cli commands always return with 0, even if
150 # there was an error.
151 # As a result - grep checks on command outputs must be used to
152 # verify vpp configuration and connectivity.
153
154 # Setup host interface
155 vppctl create host-interface name $TARGET_INTERFACE || true
156 VPP_TGT_INT="host-$TARGET_INTERFACE"
157 vppctl set interface state $VPP_TGT_INT up || true
158 vppctl set interface ip address $VPP_TGT_INT $TARGET_IP/24 || true
159 vppctl set interface mtu $MTU $VPP_TGT_INT || true
160
161 vppctl show interface | tr -s " " | grep -E "host-$TARGET_INTERFACE [0-9]+ up $MTU/0/0/0"
162
163 # Disable session layer
164 # NOTE: VPP net framework should enable it itself.
165 vppctl session disable || true
166
167 # Verify connectivity
168 vppctl show int addr | grep -E "$TARGET_IP/24"
169 ip addr show $INITIATOR_INTERFACE
170 ip netns exec $TARGET_NAMESPACE ip addr show $TARGET_INTERFACE
171 sleep 3
172 # SC1010: ping -M do - in this case do is an option not bash special word
173 # shellcheck disable=SC1010
174 ping -c 1 $TARGET_IP -s $((MTU - 28)) -M do
175 vppctl ping $INITIATOR_IP repeat 1 size $((MTU - (28 + 8))) verbose | grep -E "$MTU_W_HEADER bytes from $INITIATOR_IP"
176 }
177
178 function kill_vpp() {
179 vppctl delete host-interface name $TARGET_INTERFACE || true
180
181 # Dump VPP configuration before kill
182 vppctl show api clients || true
183 vppctl show session || true
184 vppctl show errors || true
185
186 killprocess $vpp_pid
187 }
188 function initiator_json_config() {
189 # Prepare config file for iSCSI initiator
190 jq . <<- JSON
191 {
192 "subsystems": [
193 {
194 "subsystem": "bdev",
195 "config": [
196 {
197 "method": "bdev_iscsi_create",
198 "params": {
199 "name": "iSCSI0",
200 "url": "iscsi://$TARGET_IP/iqn.2016-06.io.spdk:disk1/0",
201 "initiator_iqn": "iqn.2016-06.io.spdk:disk1/0"
202 }
203 }${*:+,$*}
204 ]
205 }
206 ]
207 }
208 JSON
209 }