xref: /spdk/test/iscsi_tgt/common.sh (revision 90755749b9c7111ac73548f0373ebb9c42081a3c)
1#  SPDX-License-Identifier: BSD-3-Clause
2#  Copyright (C) 2017 Intel Corporation
3#  All rights reserved.
4#
5
6# Network configuration
7# There is one initiator interface and it is accessed directly.
8# There are two target interfaces and they are accessed through an namespace.
9ISCSI_BRIDGE="iscsi_br"
10INITIATOR_INTERFACE="spdk_init_int"
11INITIATOR_BRIDGE="init_br"
12TARGET_NAMESPACE="spdk_iscsi_ns"
13TARGET_NS_CMD=(ip netns exec "$TARGET_NAMESPACE")
14TARGET_INTERFACE="spdk_tgt_int"
15TARGET_INTERFACE2="spdk_tgt_int2"
16TARGET_BRIDGE="tgt_br"
17TARGET_BRIDGE2="tgt_br2"
18
19# iSCSI target configuration
20TARGET_IP=10.0.0.1
21TARGET_IP2=10.0.0.3
22INITIATOR_IP=10.0.0.2
23ISCSI_PORT=3260
24NETMASK=$INITIATOR_IP/32
25INITIATOR_TAG=2
26INITIATOR_NAME=ANY
27PORTAL_TAG=1
28ISCSI_APP=("${TARGET_NS_CMD[@]}" "${ISCSI_APP[@]}")
29ISCSI_TEST_CORE_MASK=0xF
30
31function create_veth_interfaces() {
32	ip link set $INITIATOR_BRIDGE nomaster || true
33	ip link set $TARGET_BRIDGE nomaster || true
34	ip link set $TARGET_BRIDGE2 nomaster || true
35	ip link set $INITIATOR_BRIDGE down || true
36	ip link set $TARGET_BRIDGE down || true
37	ip link set $TARGET_BRIDGE2 down || true
38	ip link delete $ISCSI_BRIDGE type bridge || true
39	ip link delete $INITIATOR_INTERFACE || true
40	"${TARGET_NS_CMD[@]}" ip link delete $TARGET_INTERFACE || true
41	"${TARGET_NS_CMD[@]}" ip link delete $TARGET_INTERFACE2 || true
42	ip netns del $TARGET_NAMESPACE || true
43
44	trap 'cleanup_veth_interfaces; exit 1' SIGINT SIGTERM EXIT
45
46	# Create network namespace
47	ip netns add $TARGET_NAMESPACE
48
49	# Create veth (Virtual ethernet) interface pairs
50	ip link add $INITIATOR_INTERFACE type veth peer name $INITIATOR_BRIDGE
51	ip link add $TARGET_INTERFACE type veth peer name $TARGET_BRIDGE
52	ip link add $TARGET_INTERFACE2 type veth peer name $TARGET_BRIDGE2
53
54	# Associate veth interface pairs with network namespace
55	ip link set $TARGET_INTERFACE netns $TARGET_NAMESPACE
56	ip link set $TARGET_INTERFACE2 netns $TARGET_NAMESPACE
57
58	# Allocate IP addresses
59	ip addr add $INITIATOR_IP/24 dev $INITIATOR_INTERFACE
60	"${TARGET_NS_CMD[@]}" ip addr add $TARGET_IP/24 dev $TARGET_INTERFACE
61	"${TARGET_NS_CMD[@]}" ip addr add $TARGET_IP2/24 dev $TARGET_INTERFACE2
62
63	# Link up veth interfaces
64	ip link set $INITIATOR_INTERFACE up
65	ip link set $INITIATOR_BRIDGE up
66	ip link set $TARGET_BRIDGE up
67	ip link set $TARGET_BRIDGE2 up
68	"${TARGET_NS_CMD[@]}" ip link set $TARGET_INTERFACE up
69	"${TARGET_NS_CMD[@]}" ip link set $TARGET_INTERFACE2 up
70	"${TARGET_NS_CMD[@]}" ip link set lo up
71
72	# Create a bridge
73	ip link add $ISCSI_BRIDGE type bridge
74	ip link set $ISCSI_BRIDGE up
75
76	# Add veth interfaces to the bridge
77	ip link set $INITIATOR_BRIDGE master $ISCSI_BRIDGE
78	ip link set $TARGET_BRIDGE master $ISCSI_BRIDGE
79	ip link set $TARGET_BRIDGE2 master $ISCSI_BRIDGE
80
81	# Accept connections from veth interface
82	iptables -I INPUT 1 -i $INITIATOR_INTERFACE -p tcp --dport $ISCSI_PORT -j ACCEPT
83	iptables -A FORWARD -i $ISCSI_BRIDGE -o $ISCSI_BRIDGE -j ACCEPT
84
85	# Verify connectivity
86	ping -c 1 $TARGET_IP
87	ping -c 1 $TARGET_IP2
88	"${TARGET_NS_CMD[@]}" ping -c 1 $INITIATOR_IP
89	"${TARGET_NS_CMD[@]}" ping -c 1 $INITIATOR_IP
90}
91
92function cleanup_veth_interfaces() {
93	# Cleanup bridge, veth interfaces, and network namespace
94	# Note: removing one veth, removes the pair
95	ip link set $INITIATOR_BRIDGE nomaster
96	ip link set $TARGET_BRIDGE nomaster
97	ip link set $TARGET_BRIDGE2 nomaster
98	ip link set $INITIATOR_BRIDGE down
99	ip link set $TARGET_BRIDGE down
100	ip link set $TARGET_BRIDGE2 down
101	ip link delete $ISCSI_BRIDGE type bridge
102	ip link delete $INITIATOR_INTERFACE
103	"${TARGET_NS_CMD[@]}" ip link delete $TARGET_INTERFACE
104	"${TARGET_NS_CMD[@]}" ip link delete $TARGET_INTERFACE2
105	ip netns del $TARGET_NAMESPACE
106}
107
108function iscsitestinit() {
109	if [ "$TEST_MODE" == "iso" ]; then
110		$rootdir/scripts/setup.sh
111		create_veth_interfaces
112	fi
113}
114
115function waitforiscsidevices() {
116	local num=$1
117
118	for ((i = 1; i <= 20; i++)); do
119		n=$(iscsiadm -m session -P 3 | grep -c "Attached scsi disk sd[a-z]*" || true)
120		if [ $n -ne $num ]; then
121			sleep 0.1
122		else
123			return 0
124		fi
125	done
126
127	return 1
128}
129
130function iscsitestfini() {
131	if [ "$TEST_MODE" == "iso" ]; then
132		cleanup_veth_interfaces
133		$rootdir/scripts/setup.sh reset
134	fi
135}
136
137function initiator_json_config() {
138	# Prepare config file for iSCSI initiator
139	jq . <<- JSON
140		{
141		  "subsystems": [
142		    {
143		      "subsystem": "bdev",
144		      "config": [
145		        {
146		          "method": "bdev_iscsi_create",
147		          "params": {
148		            "name": "iSCSI0",
149		            "url": "iscsi://$TARGET_IP/iqn.2016-06.io.spdk:disk1/0",
150		            "initiator_iqn": "iqn.2016-06.io.spdk:disk1/0"
151		          }
152		        },
153		        {
154		          "method": "bdev_wait_for_examine"
155		        }
156		      ]
157		    }
158		  ]
159		}
160	JSON
161}
162