xref: /spdk/test/nvmf/target/referrals.sh (revision 020c4e1192a1bb93c07652c5927fb7af615f0cf9)
1#!/usr/bin/env bash
2#  SPDX-License-Identifier: BSD-3-Clause
3#  Copyright (C) 2023 Intel Corporation
4#  All rights reserved.
5#
6testdir=$(readlink -f $(dirname $0))
7rootdir=$(readlink -f $testdir/../../..)
8source $rootdir/test/common/autotest_common.sh
9source $rootdir/test/nvmf/common.sh
10
11NVMF_REFERRAL_IP_1=127.0.0.2
12NVMF_REFERRAL_IP_2=127.0.0.3
13NVMF_REFERRAL_IP_3=127.0.0.4
14NVMF_PORT_REFERRAL=4430
15DISCOVERY_NQN=nqn.2014-08.org.nvmexpress.discovery
16NQN=nqn.2016-06.io.spdk:cnode1
17
18get_referral_ips() {
19	if [[ "$1" == "rpc" ]]; then
20		# shellcheck disable=SC2005
21		echo $(rpc_cmd nvmf_discovery_get_referrals | jq -r '.[].address.traddr' | sort)
22	elif [[ "$1" == "nvme" ]]; then
23		# shellcheck disable=SC2005
24		echo $(nvme discover "${NVME_HOST[@]}" -t $TEST_TRANSPORT -a $NVMF_FIRST_TARGET_IP -s 8009 -o json \
25			| jq -r '.records[] | select(.subtype != "current discovery subsystem").traddr' \
26			| sort)
27	fi
28}
29
30get_discovery_entries() {
31	local subtype="$1"
32
33	nvme discover "${NVME_HOST[@]}" -t $TEST_TRANSPORT -a $NVMF_FIRST_TARGET_IP -s 8009 -o json \
34		| jq ".records[] | select(.subtype == \"$subtype\")"
35}
36
37nvmftestinit
38nvmfappstart -m 0xF
39
40rpc_cmd nvmf_create_transport $NVMF_TRANSPORT_OPTS -u 8192
41rpc_cmd nvmf_subsystem_add_listener -t $TEST_TRANSPORT -a $NVMF_FIRST_TARGET_IP -s 8009 discovery
42
43# Add a referral to another discovery service
44rpc_cmd nvmf_discovery_add_referral -t $TEST_TRANSPORT -a $NVMF_REFERRAL_IP_1 -s $NVMF_PORT_REFERRAL
45rpc_cmd nvmf_discovery_add_referral -t $TEST_TRANSPORT -a $NVMF_REFERRAL_IP_2 -s $NVMF_PORT_REFERRAL
46rpc_cmd nvmf_discovery_add_referral -t $TEST_TRANSPORT -a $NVMF_REFERRAL_IP_3 -s $NVMF_PORT_REFERRAL
47
48(($(rpc_cmd nvmf_discovery_get_referrals | jq 'length') == 3))
49[[ $(get_referral_ips "rpc") == "$NVMF_REFERRAL_IP_1 $NVMF_REFERRAL_IP_2 $NVMF_REFERRAL_IP_3" ]]
50[[ $(get_referral_ips "nvme") == "$NVMF_REFERRAL_IP_1 $NVMF_REFERRAL_IP_2 $NVMF_REFERRAL_IP_3" ]]
51
52rpc_cmd nvmf_discovery_remove_referral -t $TEST_TRANSPORT -a $NVMF_REFERRAL_IP_1 -s $NVMF_PORT_REFERRAL
53rpc_cmd nvmf_discovery_remove_referral -t $TEST_TRANSPORT -a $NVMF_REFERRAL_IP_2 -s $NVMF_PORT_REFERRAL
54rpc_cmd nvmf_discovery_remove_referral -t $TEST_TRANSPORT -a $NVMF_REFERRAL_IP_3 -s $NVMF_PORT_REFERRAL
55
56(($(rpc_cmd nvmf_discovery_get_referrals | jq 'length') == 0))
57[[ $(get_referral_ips "nvme") == "" ]]
58
59# Add a referral to a discovery and NVMe subsystems on the same IP/port
60rpc_cmd nvmf_discovery_add_referral -t $TEST_TRANSPORT -a $NVMF_REFERRAL_IP_1 \
61	-s $NVMF_PORT_REFERRAL -n discovery
62rpc_cmd nvmf_discovery_add_referral -t $TEST_TRANSPORT -a $NVMF_REFERRAL_IP_1 \
63	-s $NVMF_PORT_REFERRAL -n "$NQN"
64
65[[ $(get_referral_ips "rpc") == "$NVMF_REFERRAL_IP_1 $NVMF_REFERRAL_IP_1" ]]
66[[ $(get_referral_ips "nvme") == "$NVMF_REFERRAL_IP_1 $NVMF_REFERRAL_IP_1" ]]
67[[ $(get_discovery_entries "nvme subsystem" | jq -r '.subnqn') == "$NQN" ]]
68[[ $(get_discovery_entries "discovery subsystem referral" | jq -r '.subnqn') == "$DISCOVERY_NQN" ]]
69
70# Remove one of the referrals and check that it's gone
71rpc_cmd nvmf_discovery_remove_referral -t $TEST_TRANSPORT -a $NVMF_REFERRAL_IP_1 \
72	-s $NVMF_PORT_REFERRAL -n $NQN
73[[ $(get_referral_ips "rpc") == "$NVMF_REFERRAL_IP_1" ]]
74[[ $(get_referral_ips "nvme") == "$NVMF_REFERRAL_IP_1" ]]
75[[ $(get_discovery_entries "nvme subsystem" | jq -r '.subnqn') == "" ]]
76[[ $(get_discovery_entries "discovery subsystem referral" | jq -r '.subnqn') == "$DISCOVERY_NQN" ]]
77
78# Remove the second one
79rpc_cmd nvmf_discovery_remove_referral -t $TEST_TRANSPORT -a $NVMF_REFERRAL_IP_1 \
80	-s $NVMF_PORT_REFERRAL -n $DISCOVERY_NQN
81
82(($(rpc_cmd nvmf_discovery_get_referrals | jq 'length') == 0))
83[[ $(get_referral_ips "nvme") == "" ]]
84
85trap - SIGINT SIGTERM EXIT
86nvmftestfini
87