xref: /spdk/test/nvmf/target/tls.sh (revision a5f87f39127c7e0da8d9c4fcd042a27e350be84e)
1#!/usr/bin/env bash
2#  SPDX-License-Identifier: BSD-3-Clause
3#  All rights reserved.
4#
5
6testdir=$(readlink -f $(dirname $0))
7rootdir=$(readlink -f $testdir/../../..)
8
9source $rootdir/test/common/autotest_common.sh
10source $rootdir/test/nvmf/common.sh
11
12rpc_py="$rootdir/scripts/rpc.py"
13
14cleanup() {
15	process_shm --id $NVMF_APP_SHM_ID || true
16	killprocess $bdevperf_pid
17	nvmftestfini || true
18	rm -f $key_path $key_2_path $key_long_path
19}
20
21function run_bdevperf() {
22	local subnqn hostnqn psk
23	subnqn=$1 hostnqn=$2 psk=${3:+--psk $3}
24
25	bdevperf_rpc_sock=/var/tmp/bdevperf.sock
26	# use bdevperf to test "bdev_nvme_attach_controller"
27	$rootdir/build/examples/bdevperf -m 0x4 -z -r $bdevperf_rpc_sock -q 128 -o 4096 -w verify -t 10 "${NO_HUGE[@]}" &
28	bdevperf_pid=$!
29
30	trap 'cleanup; exit 1' SIGINT SIGTERM EXIT
31	waitforlisten $bdevperf_pid $bdevperf_rpc_sock
32
33	# send RPC
34	if ! $rpc_py -s $bdevperf_rpc_sock bdev_nvme_attach_controller -b TLSTEST -t $TEST_TRANSPORT \
35		-a $NVMF_FIRST_TARGET_IP -s $NVMF_PORT -f ipv4 -n "$subnqn" -q "$hostnqn" $psk; then
36		killprocess $bdevperf_pid
37		return 1
38	fi
39
40	# run I/O and wait
41	$rootdir/examples/bdev/bdevperf/bdevperf.py -t 20 -s $bdevperf_rpc_sock perform_tests
42
43	# finish
44	trap 'nvmftestfini; exit 1' SIGINT SIGTERM EXIT
45	killprocess $bdevperf_pid
46}
47
48format_interchange_psk() {
49	local key hash crc
50
51	key=$1 hash=${2:-01}
52	crc=$(echo -n $key | gzip -1 -c | tail -c8 | head -c 4)
53
54	echo "NVMeTLSkey-1:$hash:$(base64 <(echo -n ${key}${crc})):"
55}
56
57setup_nvmf_tgt() {
58	local key=$1
59
60	$rpc_py nvmf_create_transport $NVMF_TRANSPORT_OPTS
61	$rpc_py nvmf_create_subsystem nqn.2016-06.io.spdk:cnode1 -s SPDK00000000000001 -m 10
62	$rpc_py nvmf_subsystem_add_listener nqn.2016-06.io.spdk:cnode1 -t $TEST_TRANSPORT \
63		-a $NVMF_FIRST_TARGET_IP -s $NVMF_PORT -k
64	$rpc_py bdev_malloc_create 32 4096 -b malloc0
65	$rpc_py nvmf_subsystem_add_ns nqn.2016-06.io.spdk:cnode1 malloc0 -n 1
66
67	$rpc_py nvmf_subsystem_add_host nqn.2016-06.io.spdk:cnode1 nqn.2016-06.io.spdk:host1 \
68		--psk $key
69}
70
71nvmftestinit
72nvmfappstart -m 0x2 --wait-for-rpc
73
74if [ "$TEST_TRANSPORT" != tcp ]; then
75	echo "Unsupported transport: $TEST_TRANSPORT"
76	exit 0
77fi
78
79$rpc_py sock_set_default_impl -i ssl
80
81# Check default TLS version
82version=$($rpc_py sock_impl_get_options -i ssl | jq -r .tls_version)
83if [[ "$version" != "0" ]]; then
84	echo "TLS version was not set correctly $version != 0"
85	exit 1
86fi
87
88# Check TLS version set to 13
89$rpc_py sock_impl_set_options -i ssl --tls-version 13
90version=$($rpc_py sock_impl_get_options -i ssl | jq -r .tls_version)
91if [[ "$version" != "13" ]]; then
92	echo "TLS version was not set correctly $version != 13"
93	exit 1
94fi
95
96# Check incorrect TLS version set to 7
97$rpc_py sock_impl_set_options -i ssl --tls-version 7
98version=$($rpc_py sock_impl_get_options -i ssl | jq -r .tls_version)
99if [[ "$version" != "7" ]]; then
100	echo "TLS version was not set correctly $version != 7"
101	exit 1
102fi
103
104# Check default KTLS is disabled
105ktls=$($rpc_py sock_impl_get_options -i ssl | jq -r .enable_ktls)
106if [[ "$ktls" != "false" ]]; then
107	echo "KTLS was not set correctly $ktls != false"
108	exit 1
109fi
110
111# Check KTLS enable
112$rpc_py sock_impl_set_options -i ssl --enable-ktls
113ktls=$($rpc_py sock_impl_get_options -i ssl | jq -r .enable_ktls)
114if [[ "$ktls" != "true" ]]; then
115	echo "KTLS was not set correctly $ktls != true"
116	exit 1
117fi
118
119# Check KTLS disable
120$rpc_py sock_impl_set_options -i ssl --disable-ktls
121ktls=$($rpc_py sock_impl_get_options -i ssl | jq -r .enable_ktls)
122if [[ "$ktls" != "false" ]]; then
123	echo "KTLS was not set correctly $ktls != false"
124	exit 1
125fi
126
127key=$(format_interchange_psk 00112233445566778899aabbccddeeff)
128key_2=$(format_interchange_psk ffeeddccbbaa99887766554433221100)
129
130key_path="$testdir/key1.txt"
131key_2_path="$testdir/key2.txt"
132
133echo -n "$key" > $key_path
134echo -n "$key_2" > $key_2_path
135
136chmod 0600 $key_path
137chmod 0600 $key_2_path
138
139$rpc_py sock_impl_set_options -i ssl --tls-version 13
140$rpc_py framework_start_init
141
142setup_nvmf_tgt $key_path
143
144# Test #1 - test connectivity with perf and bdevperf application
145# Check connectivity with nvmeperf"
146"${NVMF_TARGET_NS_CMD[@]}" $SPDK_BIN_DIR/spdk_nvme_perf -S ssl -q 64 -o 4096 -w randrw -M 30 -t 10 \
147	-r "trtype:${TEST_TRANSPORT} adrfam:IPv4 traddr:${NVMF_FIRST_TARGET_IP} trsvcid:${NVMF_PORT} \
148subnqn:nqn.2016-06.io.spdk:cnode1 hostnqn:nqn.2016-06.io.spdk:host1" \
149	--psk-path $key_path "${NO_HUGE[@]}"
150
151# Check connectivity with bdevperf with 32 bytes long key
152run_bdevperf nqn.2016-06.io.spdk:cnode1 nqn.2016-06.io.spdk:host1 "$key_path"
153
154# Test #2 - test if it is possible to connect with different PSK
155NOT run_bdevperf nqn.2016-06.io.spdk:cnode1 nqn.2016-06.io.spdk:host1 "$key_2_path"
156
157# Test #3 - test if it is possible to connect with different hostnqn
158NOT run_bdevperf nqn.2016-06.io.spdk:cnode1 nqn.2016-06.io.spdk:host2 "$key_path"
159
160# Test #4 - test if it is possible to connect with different subnqn
161NOT run_bdevperf nqn.2016-06.io.spdk:cnode2 nqn.2016-06.io.spdk:host1 "$key_path"
162
163# Test #5 - test if it is possible to connect with POSIX socket to SSL socket (no credentials provided)
164NOT run_bdevperf nqn.2016-06.io.spdk:cnode1 nqn.2016-06.io.spdk:host1 ""
165
166# Test #6 - check connectivity with bdevperf, but with 48 bytes long key
167killprocess $nvmfpid
168key_long=$(format_interchange_psk 00112233445566778899aabbccddeeff0011223344556677 02)
169key_long_path="$testdir/key_long.txt"
170echo -n "$key_long" > $key_long_path
171chmod 0600 $key_long_path
172nvmfappstart -m 0x2
173
174setup_nvmf_tgt $key_long_path
175
176run_bdevperf nqn.2016-06.io.spdk:cnode1 nqn.2016-06.io.spdk:host1 "$key_long_path"
177
178# Test #7 - check if it is possible to connect with incorrect permissions
179chmod 0666 $key_long_path
180NOT run_bdevperf nqn.2016-06.io.spdk:cnode1 nqn.2016-06.io.spdk:host1 "$key_long_path"
181
182# Test #8 - check if it is possible to setup nvmf_tgt with PSK with incorrect permissions
183killprocess $nvmfpid
184nvmfappstart -m 0x2
185
186NOT setup_nvmf_tgt $key_long_path
187
188# Test #9 - test saving/loading JSON configuration by connecting to bdevperf
189killprocess $nvmfpid
190chmod 0600 $key_long_path
191
192# Run both applications just to get their JSON configs
193nvmfappstart -m 0x2
194setup_nvmf_tgt $key_long_path
195
196$rootdir/build/examples/bdevperf -m 0x4 -z -r $bdevperf_rpc_sock -q 128 -o 4096 -w verify -t 10 "${NO_HUGE[@]}" &
197bdevperf_pid=$!
198
199trap 'cleanup; exit 1' SIGINT SIGTERM EXIT
200waitforlisten $bdevperf_pid $bdevperf_rpc_sock
201$rpc_py -s $bdevperf_rpc_sock bdev_nvme_attach_controller -b TLSTEST -t $TEST_TRANSPORT \
202	-a $NVMF_FIRST_TARGET_IP -s $NVMF_PORT -f ipv4 -n nqn.2016-06.io.spdk:cnode1 \
203	-q nqn.2016-06.io.spdk:host1 --psk $key_long_path
204
205tgtconf=$($rpc_py save_config)
206bdevperfconf=$($rpc_py -s $bdevperf_rpc_sock save_config)
207
208killprocess $bdevperf_pid
209killprocess $nvmfpid
210
211# Launch apps with configs
212nvmfappstart -m 0x2 -c <(echo "$tgtconf")
213$rootdir/build/examples/bdevperf -m 0x4 -z -r $bdevperf_rpc_sock -q 128 -o 4096 -w verify -t 10 \
214	-c <(echo "$bdevperfconf") "${NO_HUGE[@]}" &
215
216bdevperf_pid=$!
217waitforlisten $bdevperf_pid $bdevperf_rpc_sock
218
219# Run I/O
220$rootdir/examples/bdev/bdevperf/bdevperf.py -t 20 -s $bdevperf_rpc_sock perform_tests
221
222trap 'nvmftestfini; exit 1' SIGINT SIGTERM EXIT
223killprocess $bdevperf_pid
224killprocess $nvmfpid
225
226trap - SIGINT SIGTERM EXIT
227cleanup
228