Viewing: sanity-lnet.sh

#!/usr/bin/bash
#
# Run select tests by setting ONLY, or as arguments to the script.
# Skip specific tests by setting EXCEPT.
#

set -e

ONLY=${ONLY:-"$*"}

LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}

. $LUSTRE/tests/test-framework.sh
CLEANUP=${CLEANUP:-:}
SETUP=${SETUP:-:}
PTLDEBUG="$PTLDEBUG neterror net nettrace malloc"
init_test_env "$@"
. ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
init_logging

ALWAYS_EXCEPT="$SANITY_LNET_EXCEPT "
always_except LU-10391 253 254
always_except LU-18751 410

[[ "$SLOW" = "no" ]] && EXCEPT_SLOW=""

[[ -z $LNETCTL ]] && skip "Need lnetctl"

restore_mounts=false

if is_mounted $MOUNT || is_mounted $MOUNT2; then
	cleanupall || error "Failed cleanup prior to test execution"
	restore_mounts=true
fi

cleanup_lnet() {
	echo "Cleaning up LNet"
	lsmod | grep -q lnet &&
		$LNETCTL lnet unconfigure 2>/dev/null
	unload_modules
}

if (( $MDS1_VERSION >= $(version_code 2.16.56) )); then
	net_delay_add="net_delay add"
	net_delay_del="net_delay del"
else
	net_delay_add="net_delay_add"
	net_delay_del="net_delay_del"
fi

restore_modules=false
if module_loaded lnet ; then
	cleanup_lnet || error "Failed to unload modules before test execution"
	restore_modules=true
fi

cleanup_testsuite() {
	trap "" EXIT
	# Cleanup any tmp files created by the sub tests
	rm -f $TMP/sanity-lnet-*.yaml $LNET_PARAMS_FILE
	cleanup_netns
	cleanup_lnet
	if $restore_mounts; then
		setupall || error "Failed to setup Lustre after test execution"
	elif $restore_modules; then
		load_modules ||
			error "Couldn't load modules after test execution"
	fi
	return 0
}

TESTNS='test_ns'
FAKE_IF="test1pg"
FAKE_IP="10.1.2.3"
FAKE_IP_ALIAS="10.1.2.31"
FAKE_IPV6="2001:db8:0:f101::1"
do_ns() {
	echo "ip netns exec $TESTNS $*"
	ip netns exec $TESTNS "$@"
}

setup_fakeif() {
	local netns="$1"

	local netns_arg netns_exec
	if [[ -n $netns ]]; then
		netns_arg="netns $netns"
		netns_exec="ip netns exec $netns"
	fi

	echo "ip link add 'test1pl' type veth peer name $FAKE_IF $netns_arg"
	ip link add 'test1pl' type veth peer name $FAKE_IF $netns_arg
	echo "ip link set 'test1pl' up"
	ip link set 'test1pl' up
	echo "$netns_exec ip addr add \"${FAKE_IP}/31\" dev $FAKE_IF"
	$netns_exec ip addr add "${FAKE_IP}/31" dev $FAKE_IF
	echo "$netns_exec ip -6 addr add \"${FAKE_IPV6}/64\" dev $FAKE_IF"
	$netns_exec ip -6 addr add "${FAKE_IPV6}/64" dev $FAKE_IF
	echo "$netns_exec ip link set $FAKE_IF up"
	$netns_exec ip link set $FAKE_IF up

	local ip4=$($netns_exec ip -o -4 a s $FAKE_IF 2>/dev/null |
		    grep $FAKE_IP)
	local ip6=$($netns_exec ip -o -6 a s $FAKE_IF 2>/dev/null |
		    grep $FAKE_IPV6)

	[[ -n $ip4 && -n $ip6 ]] || error "Failed setup $FAKE_IF"
	wait_update $HOSTNAME \
		"$netns_exec ip -o a s $FAKE_IF | grep -c tentative" \
		"0" "10"
}

cleanup_fakeif() {
	ip link show test1pl >& /dev/null && ip link del test1pl || return 0
}

setup_netns() {
	cleanup_netns

	ip netns add $TESTNS
	setup_fakeif $TESTNS
}

cleanup_netns() {
	(ip netns list | grep -q $TESTNS) && ip netns del $TESTNS
	cleanup_fakeif
}

configure_dlc() {
	echo "Loading LNet and configuring DLC"
	load_lnet || return $?
	do_lnetctl lnet configure $LNET_CONFIG_OPT
}

GLOBAL_YAML_FILE=$TMP/sanity-lnet-global.yaml
define_global_yaml() {
	$LNETCTL export --backup &>${GLOBAL_YAML_FILE} ||
		error "Failed to export global yaml $?"
}

reinit_dlc() {
	if lsmod | grep -q lnet; then
		do_lnetctl lnet unconfigure ||
			error "lnetctl lnet unconfigure failed $?"
		do_lnetctl lnet configure $LNET_CONFIG_OPT ||
			error "lnetctl lnet configure failed $?"
	else
		configure_dlc || error "configure_dlc failed $?"
	fi
	define_global_yaml
}

append_global_yaml() {
	[[ ! -e ${GLOBAL_YAML_FILE} ]] &&
		error "Missing global yaml at ${GLOBAL_YAML_FILE}"

	cat ${GLOBAL_YAML_FILE} >> $TMP/sanity-lnet-$testnum-expected.yaml
}

create_base_yaml_file() {
	append_global_yaml
}

compare_yaml_files() {
	local expected="$TMP/sanity-lnet-$testnum-expected.yaml"
	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
	local rc=0
	! [[ -e $expected ]] && echo "$expected not found" && return 1
	! [[ -e $actual ]] && echo "$actual not found" && return 1
	if [ verify_yaml_available ]; then
		verify_compare_yaml $actual $expected || rc=$?
	else
		diff -upN ${actual} ${expected} || rc=$?
	fi
	echo "Expected:"
	cat $expected
	echo "Actual:"
	cat $actual
	return $rc
}

validate_nid() {
	local nid="$1"
	local net="${nid//*@/}"
	local addr="${nid//@*/}"

	local rc=1

	local num_re='[0-9]+'

	if [[ $net =~ (gni|kfi|bxi3f)[0-9]* ]]; then
		[[ $addr =~ ${num_re} ]] && rc=0
	elif [[ $net =~ tcp[0-9]* ]]; then
		if ip_is_v4 "$addr" || ip_is_v6 "$addr"; then
			rc=0
		fi
	elif [[ $net =~ o2ib[0-9]* ]]; then
		ip_is_v4 "$addr" && rc=0
	else
		echo "Unrecognized net: \"$net\""
	fi

	if ((rc != 0)); then
		echo "Invalid nid: \"$nid\""
	fi

	return $rc
}

validate_nids() {
	local yfile=$TMP/sanity-lnet-$testnum-actual.yaml
	local primary_nids=$(awk '/-\s+primary nid:/{print $NF}' $yfile |
			     xargs echo)
	local secondary_nids=$(awk '/-\s+nid:/{print $NF}' $yfile | xargs echo)
	local gateway_nids=$(awk '/gateway:/{print $NF}' $yfile | xargs echo)

	local nid
	for nid in $primary_nids $secondary_nids $gateway_nids; do
		validate_nid "$nid" || error "Bad NID \"${nid}\""
	done
	return 0
}

validate_peer_nids() {
	local num_peers="$1"
	local nids_per_peer="$2"

	local expect_p="$num_peers"
	# The primary nid also shows up in the list of secondary nids
	local expect_s="$(($num_peers + $(($nids_per_peer*$num_peers))))"

	local actual_p=$(awk '/-\s+primary nid:/{print $NF}' $TMP/sanity-lnet-$testnum-actual.yaml | wc -l)
	local actual_s=$(awk '/-\s+nid:/{print $NF}' $TMP/sanity-lnet-$testnum-actual.yaml | wc -l)
	if [[ $expect_p -ne $actual_p ]]; then
		compare_yaml_files
		error "Expected $expect_p but found $actual_p primary nids"
	elif [[ $expect_s -ne $actual_s ]]; then
		compare_yaml_files
		error "Expected $expect_s but found $actual_s secondary nids"
	fi
	validate_nids
}

validate_gateway_nids() {
	local expect_gw=$(grep -c -- 'gateway:' $TMP/sanity-lnet-$testnum-expected.yaml)
	local actual_gw=$(grep -c -- 'gateway:' $TMP/sanity-lnet-$testnum-actual.yaml)

	echo "expect_gw: $expect_gw actual_gw: $actual_gw"
	if [[ $expect_gw -ne $actual_gw ]]; then
		compare_yaml_files
		error "Expected $expect_gw gateways but found $actual_gw gateways"
	fi

	local expect_gwnids=$(awk '/gateway:/{print $NF}' $TMP/sanity-lnet-$testnum-expected.yaml |
			      xargs echo)
	local nid

	echo "expect_gwnids: $expect_gwnids"
	for nid in ${expect_gwnids}; do
		if ! grep -q "gateway: ${nid}" $TMP/sanity-lnet-$testnum-actual.yaml; then
			compare_yaml_files
			error "${nid} not configured as gateway"
		fi
	done

	validate_nids
}

intf_has_ipv6() {
	local addr=$(ip -o -6 a s "$1" | awk '{print $4}' | head -n 1 |
		     grep -v '^fe80::' | sed 's,/[0-9]\+$,,')

	ip_is_v6 "${addr}"
}

intf_has_ipv4() {
	local addr=$(ip -o -4 a s "$1" | awk '{print $4}' | head -n 1 |
		     sed 's,/[0-9]\+$,,')

	ip_is_v4 "${addr}"
}

cleanupall -f

# Determine the local interface(s) used for LNet
load_lnet "config_on_load=1" || error "Failed to load modules"

do_lnetctl net show
ip a

INTERFACES=( $(lnet_if_list) )

if [[ -z ${INTERFACES[@]} ]]; then
	error "Did not identify any LNet interfaces"
fi

# If we don't have IPv6 addresses then make sure the test suite runs in
# "IPv4 mode". If we have IPv6, but not IPv4, then make sure we run in
# "IPv6 mode". In a mixed environment we take whatever has been specified
# by the test environment configuration.
if ! intf_has_ipv6 ${INTERFACES[0]}; then
	FORCE_LARGE_NID=false
	LNET_CONFIG_INIT_OPT="--all"
	LNET_CONFIG_OPT=""
elif ! intf_has_ipv4 ${INTERFACES[0]}; then
	FORCE_LARGE_NID=true
	LNET_CONFIG_INIT_OPT="--all --large"
	LNET_CONFIG_OPT="-l"
fi

if $FORCE_LARGE_NID; then
	always_except LU-14288 101
	always_except LU-14288 103
	always_except LU-17457 199
	always_except LU-17457 208
	always_except LU-9680 213
	always_except LU-17458 220
	always_except LU-19314 228
	always_except LU-5960 230
	always_except LU-9680 231
	always_except LU-17457 255
	always_except LU-19334 257
	always_except LU-19363 270
	always_except LU-9680 302

	FAKE_NID="${FAKE_IPV6}@tcp"
else
	FAKE_NID="${FAKE_IP}@tcp"
fi

build_test_filter

cleanup_lnet || error "Failed to cleanup LNet"

stack_trap 'cleanup_testsuite' EXIT

test_0() {
	configure_dlc || error "Failed to configure DLC rc = $?"
	define_global_yaml
	reinit_dlc || return $?
	do_lnetctl import <  ${GLOBAL_YAML_FILE} || error "Import failed $?"
	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
	create_base_yaml_file
	compare_yaml_files || error "Configuration changed after import"
}
run_test 0 "Export empty config, import the config, compare"

compare_peer_add() {
	local prim_nid="${1:+--prim_nid $1}"
	local nid="${2:+--nid $2}"

	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"

	do_lnetctl peer add ${prim_nid} ${nid} || error "peer add failed $?"
	$LNETCTL export --backup > $actual || error "export failed $?"
	compare_yaml_files
	return $?
}

test_1() {
	reinit_dlc || return $?
	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
peer:
    - primary nid: 1.1.1.1@tcp
      Multi-Rail: True
      peer ni:
        - nid: 1.1.1.1@tcp
EOF
	append_global_yaml
	compare_peer_add "1.1.1.1@tcp"
}
run_test 1 "Add peer with single nid (tcp)"

test_2() {
	reinit_dlc || return $?
	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
peer:
    - primary nid: 2.2.2.2@o2ib
      Multi-Rail: True
      peer ni:
        - nid: 2.2.2.2@o2ib
EOF
	append_global_yaml
	compare_peer_add "2.2.2.2@o2ib"
}
run_test 2 "Add peer with single nid (o2ib)"

test_3() {
	reinit_dlc || return $?
	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
peer:
    - primary nid: 3.3.3.3@tcp
      Multi-Rail: True
      peer ni:
        - nid: 3.3.3.3@tcp
        - nid: 3.3.3.3@o2ib
EOF
	append_global_yaml
	compare_peer_add "3.3.3.3@tcp" "3.3.3.3@o2ib"
}
run_test 3 "Add peer with tcp primary o2ib secondary"

test_4() {
	reinit_dlc || return $?
	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
peer:
    - primary nid: 4.4.4.4@tcp
      Multi-Rail: True
      peer ni:
        - nid: 4.4.4.4@tcp
        - nid: 4.4.4.1@tcp
        - nid: 4.4.4.2@tcp
        - nid: 4.4.4.3@tcp
EOF
	append_global_yaml
	echo "Add peer with nidrange (tcp)"
	compare_peer_add "4.4.4.4@tcp" "4.4.4.[1-3]@tcp"

	echo "Add peer with nidrange that overlaps primary nid (tcp)"
	compare_peer_add "4.4.4.4@tcp" "4.4.4.[1-4]@tcp"
}
run_test 4 "Add peer with nidrange (tcp)"

test_5() {
	reinit_dlc || return $?
	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
peer:
    - primary nid: 5.5.5.5@o2ib
      Multi-Rail: True
      peer ni:
        - nid: 5.5.5.5@o2ib
        - nid: 5.5.5.1@o2ib
        - nid: 5.5.5.2@o2ib
        - nid: 5.5.5.3@o2ib
        - nid: 5.5.5.4@o2ib
EOF
	append_global_yaml
	echo "Add peer with nidrange (o2ib)"
	compare_peer_add "5.5.5.5@o2ib" "5.5.5.[1-4]@o2ib"

	echo "Add peer with nidranage that overlaps primary nid (o2ib)"
	compare_peer_add "5.5.5.5@o2ib" "5.5.5.[1-4]@o2ib"

	echo "Add peer with nidranage that contain , plus primary nid (o2ib)"
	compare_peer_add "5.5.5.5@o2ib" "5.5.5.[1,2,3-4]@o2ib"
}
run_test 5 "Add peer with nidrange (o2ib)"

test_6() {
	reinit_dlc || return $?
	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
peer:
    - primary nid: 6.6.6.6@tcp
      Multi-Rail: True
      peer ni:
        - nid: 6.6.6.6@tcp
        - nid: 6.6.6.0@tcp
        - nid: 6.6.6.2@tcp
        - nid: 6.6.6.4@tcp
        - nid: 6.6.7.0@tcp
        - nid: 6.6.7.2@tcp
        - nid: 6.6.7.4@tcp
        - nid: 6.6.1.0@o2ib
        - nid: 6.6.1.3@o2ib
        - nid: 6.6.1.6@o2ib
        - nid: 6.6.3.0@o2ib
        - nid: 6.6.3.3@o2ib
        - nid: 6.6.3.6@o2ib
        - nid: 6@gni
        - nid: 10@gni
        - nid: 6@kfi
        - nid: 10@kfi
        - nid: 6@bxi3f
        - nid: 10@bxi3f
EOF
	append_global_yaml

	local nid_expr="6.6.[6-7].[0-4/2]@tcp"
	nid_expr+=",6.6.[1-4/2].[0-6/3]@o2ib"
	nid_expr+=",[6-12/4]@gni"
	nid_expr+=",[6-12/4]@kfi"
	nid_expr+=",[6-12/4]@bxi3f"

	compare_peer_add "6.6.6.6@tcp" "${nid_expr}"
}
run_test 6 "Add peer with multiple nidranges"

compare_peer_del() {
	local prim_nid="${1:+--prim_nid $1}"
	local nid="${2:+--nid $2}"

	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"

	do_lnetctl peer del ${prim_nid} ${nid} || error "peer del failed $?"
	$LNETCTL export --backup > $actual || error "export failed $?"
	compare_yaml_files
	return $?
}

test_7() {
	reinit_dlc || return $?
	create_base_yaml_file

	echo "Delete peer with single nid (tcp)"
	do_lnetctl peer add --prim_nid 7.7.7.7@tcp || error "Peer add failed $?"
	compare_peer_del "7.7.7.7@tcp"

	echo "Delete peer with single nid (o2ib)"
	do_lnetctl peer add --prim_nid 7.7.7.7@o2ib || error "Peer add failed $?"
	compare_peer_del "7.7.7.7@o2ib"

	echo "Delete peer that has multiple nids (tcp)"
	do_lnetctl peer add --prim_nid 7.7.7.7@tcp --nid 7.7.7.[8-12]@tcp ||
		error "Peer add failed $?"
	compare_peer_del "7.7.7.7@tcp"

	echo "Delete peer that has multiple nids (o2ib)"
	do_lnetctl peer add --prim_nid 7.7.7.7@o2ib --nid 7.7.7.[8-12]@o2ib ||
		error "Peer add failed $?"
	compare_peer_del "7.7.7.7@o2ib"

	echo "Delete peer that has both tcp and o2ib nids"
	do_lnetctl peer add --prim_nid 7.7.7.7@tcp \
		--nid 7.7.7.[9-12]@tcp,7.7.7.[13-15]@o2ib ||
		error "Peer add failed $?"
	compare_peer_del "7.7.7.7@tcp"

	echo "Delete peer with single nid (gni)"
	do_lnetctl peer add --prim_nid 7@gni || error "Peer add failed $?"
	compare_peer_del "7@gni"

	echo "Delete peer that has multiple nids (gni)"
	do_lnetctl peer add --prim_nid 7@gni --nid [8-12]@gni ||
		error "Peer add failed $?"
	compare_peer_del "7@gni"

	echo "Delete peer with single nid (kfi)"
	do_lnetctl peer add --prim_nid 7@kfi || error "Peer add failed $?"
	compare_peer_del "7@kfi"

	echo "Delete peer that has multiple nids (kfi)"
	do_lnetctl peer add --prim_nid 7@kfi --nid [8-12]@kfi ||
		error "Peer add failed $?"
	compare_peer_del "7@kfi"

	echo "Delete peer with single nid (bxi3f)"
	do_lnetctl peer add --prim_nid 7@bxi3f || error "Peer add failed $?"
	compare_peer_del "7@bxi3f"

	echo "Delete peer that has multiple nids (bxi3f)"
	do_lnetctl peer add --prim_nid 7@bxi3f --nid [8-12]@bxi3f ||
		error "Peer add failed $?"
	compare_peer_del "7@bxi3f"

	echo "Delete peer that has tcp, o2ib, gni, kfi and bxi3f nids"
	local nid_expr="[8-12]@gni"
	nid_expr+=",7.7.7.[1-4]@tcp"
	nid_expr+=",7.7.7.[5-9]@o2ib"
	nid_expr+=",[1-5]@kfi"
	nid_expr+=",[3-7]@bxi3f"
	do_lnetctl peer add --prim_nid 7@gni --nid "${nid_expr}" ||
		error "Peer add failed $?"
	compare_peer_del "7@gni"

	echo "Delete peer that contains nid equal to primary nid"
	do_lnetctl peer add --prim 1.1.1.1@tcp \
		--nid 1.1.1.1@tcp,2.2.2.2@tcp,3.3.3.3@tcp,4.4.4.4@tcp ||
		error "Peer add failed $?"
	compare_peer_del "1.1.1.1@tcp" "1.1.1.1@tcp,2.2.2.2@tcp,3.3.3.3@tcp,4.4.4.4@tcp"
}
run_test 7 "Various peer delete tests"

test_8() {
	reinit_dlc || return $?

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
peer:
    - primary nid: 8.8.8.8@tcp
      Multi-Rail: True
      peer ni:
        - nid: 8.8.8.8@tcp
        - nid: 8.8.8.10@tcp
        - nid: 8.8.8.11@tcp
        - nid: 8.8.8.12@tcp
        - nid: 8.8.8.14@tcp
        - nid: 8.8.8.15@tcp
EOF
	append_global_yaml

	do_lnetctl peer add --prim_nid 8.8.8.8@tcp --nid 8.8.8.[10-15]@tcp ||
		error "Peer add failed $?"
	compare_peer_del "8.8.8.8@tcp" "8.8.8.13@tcp"
}
run_test 8 "Delete single secondary nid from peer (tcp)"

test_9() {
	reinit_dlc || return $?

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
peer:
    - primary nid: 9.9.9.9@tcp
      Multi-Rail: True
      peer ni:
        - nid: 9.9.9.9@tcp
EOF
	append_global_yaml

	do_lnetctl peer add --prim_nid 9.9.9.9@tcp \
		--nid 9.9.9.[11-16]@tcp || error "Peer add failed $?"
	compare_peer_del "9.9.9.9@tcp" "9.9.9.[11-16]@tcp"
}
run_test 9 "Delete all secondary nids from peer (tcp)"

test_10() {
	reinit_dlc || return $?

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
peer:
    - primary nid: 10.10.10.10@tcp
      Multi-Rail: True
      peer ni:
        - nid: 10.10.10.10@tcp
        - nid: 10.10.10.12@tcp
        - nid: 10.10.10.13@tcp
        - nid: 10.10.10.15@tcp
        - nid: 10.10.10.16@tcp
EOF
	append_global_yaml
	do_lnetctl peer add --prim_nid 10.10.10.10@tcp \
		--nid 10.10.10.[12-16]@tcp || error "Peer add failed $?"
	compare_peer_del "10.10.10.10@tcp" "10.10.10.14@tcp"
}
run_test 10 "Delete single secondary nid from peer (o2ib)"

test_11() {
	reinit_dlc || return $?

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
peer:
    - primary nid: 11.11.11.11@tcp
      Multi-Rail: True
      peer ni:
        - nid: 11.11.11.11@tcp
EOF
	append_global_yaml
	do_lnetctl peer add --prim_nid 11.11.11.11@tcp \
		--nid 11.11.11.[13-17]@tcp || error "Peer add failed $?"
	compare_peer_del "11.11.11.11@tcp" "11.11.11.[13-17]@tcp"
}
run_test 11 "Delete all secondary nids from peer (o2ib)"

test_12() {
	reinit_dlc || return $?

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
peer:
    - primary nid: 12.12.12.12@o2ib
      Multi-Rail: True
      peer ni:
        - nid: 12.12.12.12@o2ib
        - nid: 13.13.13.13@o2ib
        - nid: 14.13.13.13@o2ib
        - nid: 14.15.13.13@o2ib
        - nid: 15.17.1.5@tcp
        - nid: 15.17.1.10@tcp
        - nid: 15.17.1.20@tcp
EOF
	append_global_yaml
	do_lnetctl peer add --prim_nid 12.12.12.12@o2ib \
		--nid [13-14/1].[13-15/2].13.13@o2ib,[15-16/3].[17-19/4].[1].[5-20/5]@tcp ||
		error "Peer add failed $?"
	compare_peer_del "12.12.12.12@o2ib" "13.15.13.13@o2ib,15.17.1.15@tcp"
}
run_test 12 "Delete a secondary nid from peer (tcp and o2ib)"

test_13() {
	reinit_dlc || return $?

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
peer:
    - primary nid: 13.13.13.13@o2ib
      Multi-Rail: True
      peer ni:
        - nid: 13.13.13.13@o2ib
EOF
	append_global_yaml
	do_lnetctl peer add --prim_nid 13.13.13.13@o2ib \
		--nid [14-15].[1-2/1].[1].[100-254/10]@tcp,14.14.[254].14@o2ib ||
		error "Peer add failed $?"
	compare_peer_del "13.13.13.13@o2ib" \
		"[14-15].[1-2/1].[1].[100-254/10]@tcp,14.14.[254].14@o2ib"
}
run_test 13 "Delete all secondary nids from peer (tcp and o2ib)"

create_nid() {
	local num="$1"
	local net="$2"

	if [[ $net =~ (gni|kfi|bxi3f) ]]; then
		echo "${num}@${net}"
	else
		echo "${num}.${num}.${num}.${num}@${net}"
	fi
}

create_mr_peer_yaml() {
	local num_peers="$1"
	local secondary_nids="$2"
	local net="$3"

	echo "Generating peer yaml for $num_peers peers with $secondary_nids secondary nids"
	echo "peer:" >> $TMP/sanity-lnet-$testnum-expected.yaml
	local i
	local total_nids=$((num_peers + $((num_peers * secondary_nids))))
	local created=0
	local nidnum=1
	while [[ $created -lt $num_peers ]]; do
		local primary=$(create_nid ${nidnum} ${net})
	cat <<EOF >> $TMP/sanity-lnet-$testnum-expected.yaml
    - primary nid: $primary
      Multi-Rail: True
      peer ni:
        - nid: $primary
EOF
		local j
		local start=$((nidnum + 1))
		local end=$((nidnum + $secondary_nids))
		for j in $(seq ${start} ${end}); do
			local nid=$(create_nid $j ${net})
			echo "        - nid: $nid" >> $TMP/sanity-lnet-$testnum-expected.yaml
		done
		nidnum=$((end + 1))
		((created++))
	done
}

test_14() {
	reinit_dlc || return $?

	echo "Create single peer, single nid, using import"
	create_mr_peer_yaml 1 0 tcp
	do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
		error "Import failed $?"
	append_global_yaml
	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
	compare_yaml_files

	echo "Delete single peer using import --del"
	do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml ||
		error "Import failed $?"
	rm -f $TMP/sanity-lnet-$testnum-expected.yaml
	create_base_yaml_file
	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
	compare_yaml_files
}
run_test 14 "import peer create/delete with single nid"

test_15() {
	reinit_dlc || return $?

	echo "Create multiple peers, single nid per peer, using import"
	create_mr_peer_yaml 5 0 o2ib
	# The ordering of nids for this use-case is non-deterministic, so we
	# we can't just diff the expected/actual output.
	do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
		error "Import failed $?"
	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
	validate_peer_nids 5 0

	echo "Delete multiple peers, single nid per peer, using import --del"
	do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml ||
		error "Import failed $?"
	rm -f $TMP/sanity-lnet-$testnum-expected.yaml
	create_base_yaml_file
	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
	compare_yaml_files
}
run_test 15 "import multi peer create/delete with single nid per peer"

test_16() {
	reinit_dlc || return $?

	echo "Create single peer, multiple nids, using import"
	create_mr_peer_yaml 1 5 tcp
	do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
		error "Import failed $?"
	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
	validate_peer_nids 1 5

	echo "Delete single peer, multiple nids, using import --del"
	do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml ||
		error "Import failed $?"
	rm -f $TMP/sanity-lnet-$testnum-expected.yaml
	create_base_yaml_file
	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
	compare_yaml_files
}
run_test 16 "import peer create/delete with multiple nids"

test_17() {
	reinit_dlc || return $?

	echo "Create multiple peers, multiple nids per peer, using import"
	create_mr_peer_yaml 5 7 o2ib
	do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
		error "Import failed $?"
	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
	validate_peer_nids 5 7

	echo "Delete multiple peers, multiple nids per peer, using import --del"
	do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml ||
		error "Import failed $?"
	rm -f $TMP/sanity-lnet-$testnum-expected.yaml
	create_base_yaml_file
	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
	compare_yaml_files
}
run_test 17 "import multi peer create/delete with multiple nids"

test_18a() {
	reinit_dlc || return $?

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
peer:
    - primary nid: 1.1.1.1@tcp
      Multi-Rail: True
      peer ni:
        - nid: 1.1.1.1@tcp
        - nid: 2.2.2.2@tcp
        - nid: 4.4.4.4@tcp
        - nid: 3.3.3.3@o2ib
        - nid: 5@gni
EOF
	echo "Import peer with 5 nids"
	cat $TMP/sanity-lnet-$testnum-expected.yaml
	do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
		error "Import failed $?"
	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
peer:
    - primary nid: 1.1.1.1@tcp
      Multi-Rail: True
      peer ni:
        - nid: 2.2.2.2@tcp
        - nid: 3.3.3.3@o2ib
        - nid: 5@gni
EOF
	echo "Delete three of the nids"
	cat $TMP/sanity-lnet-$testnum-expected.yaml
	do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml
	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
peer:
    - primary nid: 1.1.1.1@tcp
      Multi-Rail: True
      peer ni:
        - nid: 1.1.1.1@tcp
        - nid: 4.4.4.4@tcp
EOF
	echo "Check peer has expected nids remaining"
	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
	append_global_yaml
	compare_yaml_files
}
run_test 18a "Delete a subset of nids from a single peer using import --del"

test_18b() {
	reinit_dlc || return $?

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
peer:
    - primary nid: 1.1.1.1@tcp
      Multi-Rail: True
      peer ni:
        - nid: 1.1.1.1@tcp
        - nid: 2.2.2.2@tcp
        - nid: 4.4.4.4@tcp
        - nid: 3.3.3.3@o2ib
        - nid: 5@gni
    - primary nid: 6.6.6.6@o2ib
      Multi-Rail: True
      peer ni:
        - nid: 6.6.6.6@o2ib
        - nid: 7.7.7.7@tcp
        - nid: 8.8.8.8@tcp
        - nid: 9.9.9.9@tcp
        - nid: 10@gni
EOF
	echo "Import two peers with 5 nids each"
	cat $TMP/sanity-lnet-$testnum-expected.yaml
	do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
		error "Import failed $?"
	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
peer:
    - primary nid: 1.1.1.1@tcp
      Multi-Rail: True
      peer ni:
        - nid: 2.2.2.2@tcp
        - nid: 3.3.3.3@o2ib
        - nid: 5@gni
    - primary nid: 6.6.6.6@o2ib
      Multi-Rail: True
      peer ni:
        - nid: 7.7.7.7@tcp
        - nid: 8.8.8.8@tcp
        - nid: 10@gni
EOF
	echo "Delete three of the nids from each peer"
	cat $TMP/sanity-lnet-$testnum-expected.yaml
	do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml
	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
peer:
    - primary nid: 6.6.6.6@o2ib
      Multi-Rail: True
      peer ni:
        - nid: 6.6.6.6@o2ib
        - nid: 7.7.7.7@tcp
    - primary nid: 1.1.1.1@tcp
      Multi-Rail: True
      peer ni:
        - nid: 1.1.1.1@tcp
        - nid: 4.4.4.4@tcp
EOF
	append_global_yaml
	echo "Check peers have expected nids remaining"
	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
	compare_yaml_files
	validate_peer_nids 2 1
}
run_test 18b "Delete multiple nids from multiple peers using import --del"

test_19() {
	reinit_dlc || return $?
	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
peer:
    - primary nid: 19@gni
      Multi-Rail: True
      peer ni:
        - nid: 19@gni
EOF
	append_global_yaml
	compare_peer_add "19@gni"
}
run_test 19 "Add peer with single nid (gni)"

test_20() {
	reinit_dlc || return $?
	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
peer:
    - primary nid: 20@gni
      Multi-Rail: True
      peer ni:
        - nid: 20@gni
        - nid: 20.20.20.20@tcp
        - nid: 20.20.20.20@o2ib
EOF
	append_global_yaml
	compare_peer_add "20@gni" "20.20.20.20@tcp,20.20.20.20@o2ib"
}
run_test 20 "Add peer with gni primary and tcp, o2ib secondary"

test_21() {
	reinit_dlc || return $?
	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
peer:
    - primary nid: 21@gni
      Multi-Rail: True
      peer ni:
        - nid: 21@gni
        - nid: 22@gni
        - nid: 23@gni
        - nid: 24@gni
        - nid: 25@gni
EOF
	append_global_yaml
	echo "Add peer with nidrange (gni)"
	compare_peer_add "21@gni" "[22-25]@gni" || error
	echo "Add peer with nidrange that overlaps primary nid (gni)"
	compare_peer_add "21@gni" "[21-25]@gni"
}
run_test 21 "Add peer with nidrange (gni)"

test_22() {
	reinit_dlc || return $?
	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
peer:
    - primary nid: 22@gni
      Multi-Rail: True
      peer ni:
        - nid: 22@gni
        - nid: 24@gni
        - nid: 25@gni
        - nid: 27@gni
        - nid: 28@gni
        - nid: 29@gni
EOF
	append_global_yaml
	do_lnetctl peer add --prim_nid 22@gni --nid [24-29]@gni ||
		error "Peer add failed $?"
	compare_peer_del "22@gni" "26@gni"
}
run_test 22 "Delete single secondary nid from peer (gni)"

test_23() {
	reinit_dlc || return $?
	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
peer:
    - primary nid: 23@gni
      Multi-Rail: True
      peer ni:
        - nid: 23@gni
EOF
	append_global_yaml

	do_lnetctl peer add --prim_nid 23@gni --nid [25-29]@gni ||
		error "Peer add failed $?"
	compare_peer_del "23@gni" "[25-29]@gni"
}
run_test 23 "Delete all secondary nids from peer (gni)"

test_24() {
	reinit_dlc || return $?
	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
peer:
    - primary nid: 24@gni
      Multi-Rail: True
      peer ni:
        - nid: 24@gni
        - nid: 11@gni
        - nid: 13.13.13.13@o2ib
        - nid: 14.13.13.13@o2ib
        - nid: 14.15.13.13@o2ib
        - nid: 15.17.1.5@tcp
        - nid: 15.17.1.10@tcp
        - nid: 15.17.1.20@tcp
EOF
	append_global_yaml
	do_lnetctl peer add --prim_nid 24@gni \
		--nid [13-14/1].[13-15/2].13.13@o2ib,[15-16/3].[17-19/4].[1].[5-20/5]@tcp,[5-12/6]@gni ||
		error "Peer add failed $?"
	compare_peer_del "24@gni" "5@gni,13.15.13.13@o2ib,15.17.1.15@tcp"
}
run_test 24 "Delete a secondary nid from peer (tcp, o2ib and gni)"

test_25() {
	reinit_dlc || return $?
	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
peer:
    - primary nid: 25@gni
      Multi-Rail: True
      peer ni:
        - nid: 25@gni
EOF
	append_global_yaml
	do_lnetctl peer add --prim_nid 25@gni \
		--nid [26-27].[4-10/3].26.26@tcp,26.26.26.26@o2ib,[30-35]@gni ||
		error "Peer add failed $?"
	compare_peer_del "25@gni" \
		"[26-27].[4-10/3].26.26@tcp,26.26.26.26@o2ib,[30-35]@gni"
}
run_test 25 "Delete all secondary nids from peer (tcp, gni and o2ib)"

test_26() {
	reinit_dlc || return $?

	do_lnetctl peer add --prim_nid 1.1.1.1@tcp --lock_prim ||
		error "Peer add with --lock_prim option failed $?"
	local peer_state=$($LNETCTL peer show -v 4 --nid 1.1.1.1@tcp |
		awk '/peer state/ {print $NF}')
	# This relies on the following peer state definition:
	# #define LNET_PEER_LOCK_PRIMARY          BIT(20)
	if ((!("$peer_state" & (1 << 20)))); then
		error "Peer state does not have 'locked' bit set: $peer_state"
	fi
	do_lnetctl peer del --prim_nid 1.1.1.1@tcp ||
		error "Peer del failed $?"
	$LNETCTL peer show --nid 1.1.1.1@tcp | grep -q 1.1.1.1@tcp ||
		error "1.1.1.1@tcp is not listed"
	do_lnetctl peer del --prim_nid 1.1.1.1@tcp --force ||
		error "Peer del --force failed $?"
	do_lnetctl peer show --nid 1.1.1.1@tcp &&
		error "failed to delete 1.1.1.1@tcp"

	return 0
}
run_test 26 "Delete peer with primary nid locked"

test_27() {
	reinit_dlc || return $?

	local tmp=$(mktemp)

	echo "foobar:" > $tmp
	do_lnetctl import $tmp 2>&1 | grep -q "invalid key" ||
		error "Expected import to detect invalid key"

	cat ${GLOBAL_YAML_FILE} > $tmp

	grep -q discovery $tmp ||
		error "Expect discovery param in global params"

	sed -i 's/discovery:.*/discovery:/' $tmp ||
		error "Failed to edit $tmp rc $?"

	do_lnetctl import $tmp 2>&1 | grep -q "no value" ||
		error "Expected import to detect missing value"

	sed -i 's/discovery:/discovery: foo/' $tmp ||
		error "Failed to edit $tmp rc $?"

	do_lnetctl import $tmp 2>&1 | grep -q "invalid value" ||
		error "Expected import to detect invalid value"

	rm -f $tmp
}
run_test 27 "Import bad config should fail gracefully"

test_28() {
	reinit_dlc || return $?

	do_lnetctl peer add --prim_nid 1.1.1.1@tcp --ni 7.7.7.7@tcp ||
		error "First peer add failed $?"
	do_lnetctl peer add --prim_nid 1.1.1.2@tcp --nid 7.7.7.8@tcp ||
		error "Second peer add failed $?"

	count=$(do_lnetctl peer list | awk '/-\s+nid:/{print $NF}' | wc -l)
	[[ $count -eq 2 ]] || error "wrong number of peers reported"
}
run_test 28 "Test peer_list"

add_net() {
	local net="$1"
	local if="$2"

	do_lnetctl net add --net ${net} --if ${if} ||
		error "Failed to add net ${net} on if ${if}"
}

test_40() {
	reinit_dlc || return $?

	local lock_prim
	lock_prim=$(cat /sys/module/lnet/parameters/lock_prim_nid 2>/dev/null)
	[[ "$lock_prim" == "1" ]] || skip "Need lock_prim_nid=1"

	add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?

	# A reachable NID. A health monitor pings it before the filesystem
	# is mounted, so a peer record for it already exists, and is up to
	# date, by the time the config llog calls LNetAddPeer().
	local secondary=$($LCTL list_nids | head -n 1)

	# The primary NID from the config. It lives on a network with no
	# local interface, so it never appears in the secondary's ping
	# reply, the same as a primary NID whose interface is down.
	local primary="${secondary%@*}@${NETTYPE}99"
	local uuid="lnet_add_peer_uuid"

	# Health monitor discovers the reachable NID first.
	do_lnetctl discover ${secondary} ||
		error "Failed to discover $secondary"

	(($($LNETCTL peer show --nid ${secondary} |
	    grep -cFw ${primary}) == 0)) ||
		error "$primary present before LNetAddPeer"

	# Need Lustre modules for add_uuid
	load_modules_local ||
		error "Failed to load modules rc=$?"

	# The config llog associates both NIDs with one peer via
	# LNetAddPeer(). The primary NID is added first and locked.
	$LCTL add_uuid ${uuid} ${primary} ||
		error "add_uuid $primary failed $?"
	$LCTL add_uuid ${uuid} ${secondary} ||
		error "add_uuid $secondary failed $?"

	# The pre-existing, up to date peer for the secondary NID must
	# still be merged into the primary peer.
	local show="$LNETCTL peer show --nid ${primary} 2>/dev/null"
	wait_update $HOSTNAME "$show | grep -cFw ${secondary}" "1" "20"
	local rc=$?

	$LCTL del_uuid ${uuid} || error "del_uuid failed $?"

	((rc == 0)) || error "$secondary was not merged into peer $primary"

	unload_modules_local
}
run_test 40 "LNetAddPeer merges a pre-existing non-primary peer NID"

test_50() {
	reinit_dlc || return $?

	local param

	for param in alive_router_check_interval router_ping_timeout; do
		echo 1 > /sys/module/lnet/parameters/$param
	done

	local tyaml=$TMP/sanity-lnet-$testnum.yaml
	cat <<EOF > $tyaml
routing:
    enable: 1
EOF

	echo "Enable routing"
	do_lnetctl import $tyaml ||
		error "Import failed rc = $?"

	# Get default buffer sizes for later
	local tiny=$($LNETCTL export --backup |
		     awk '/\s+tiny:/{print $NF}')
	local small=$($LNETCTL export --backup |
		      awk '/\s+small:/{print $NF}')
	local large=$($LNETCTL export --backup |
		      awk '/\s+large:/{print $NF}')

	wait_update $HOSTNAME \
		"$LNETCTL routing show | grep enable | grep -c 1" \
		"1" "10"

	(($? == 0)) ||
		error "Routing not enabled"

	cat <<EOF > $tyaml
routing:
    enable: 0
EOF

	echo "Disable routing"
	do_lnetctl import $tyaml ||
		error "Import failed rc = $?"

	wait_update $HOSTNAME \
		"$LNETCTL routing show | grep enable | grep -c 0" \
		"1" "10"
	(($? == 0)) ||
		error "Routing not disabled"

	local ncpt=$($LCTL get_param -n cpu_partition_table | wc -l)

	tiny=$((tiny + ncpt))
	small=$((small + ncpt))
	large=$((large + ncpt))
	cat <<EOF > $tyaml
routing:
    enable: 1
buffers:
    tiny: $tiny
    small: $small
    large: $large
EOF

	echo "Enable routing w/custom buffer sizses"
	do_lnetctl import $tyaml ||
		error "Import failed rc = $?"

	cat /sys/module/lnet/parameters/*buffers
	local tiny2=$($LNETCTL export --backup |
		      awk '/\s+tiny:/{print $NF}')
	local small2=$($LNETCTL export --backup |
		       awk '/\s+small:/{print $NF}')
	local large2=$($LNETCTL export --backup |
		       awk '/\s+large:/{print $NF}')

	((tiny2 == tiny)) ||
		error "Expect tiny buffers $tiny found $tiny2"
	((small2 == small)) ||
		error "Expect small buffers $small found $small2"
	((large2 == large)) ||
		error "Expect large buffers $large found $large2"
}
run_test 50 "Enable/disable routing via yaml import"

test_51() {
	reinit_dlc || return $?

	local range=$(cat /sys/module/lnet/parameters/lnet_numa_range)
	local tyaml=$TMP/sanity-lnet-$testnum.yaml

	((range++))
	cat <<EOF > $tyaml
numa:
    range: $range
EOF

	do_lnetctl import $tyaml || error "Import failed rc = $?"

	local range2=$(cat /sys/module/lnet/parameters/lnet_numa_range)

	((range2 == range)) ||
		error "Expect lnet_numa_range $range found $range2"
}
run_test 51 "Set numa range via yaml import"

test_99a() {
	reinit_dlc || return $?

	echo "Invalid prim_nid - peer add"
	do_lnetctl peer add --prim_nid foobar &&
		error "Command should have failed"

	echo "Invalid prim_nid - peer del"
	do_lnetctl peer del --prim_nid foobar &&
		error "Command should have failed"

	echo "Delete non-existing peer"
	do_lnetctl peer del --prim_nid 1.1.1.1@o2ib &&
		error "Command should have failed"

	echo "Don't provide mandatory argument for peer del"
	do_lnetctl peer del --nid 1.1.1.1@tcp &&
		error "Command should have failed"

	echo "Don't provide mandatory argument for peer add"
	do_lnetctl peer add --nid 1.1.1.1@tcp &&
		error "Command should have failed"

	echo "Don't provide mandatory arguments peer add"
	do_lnetctl peer add &&
		error "Command should have failed"

	echo "Invalid secondary nids"
	do_lnetctl peer add --prim_nid 1.1.1.1@tcp --nid foobar &&
		error "Command should have failed"

	echo "Exceed max nids per peer"
	do_lnetctl peer add --prim_nid 1.1.1.1@tcp --nid 1.1.1.[2-255]@tcp &&
		error "Command should have failed"

	echo "Invalid net type"
	do_lnetctl peer add --prim_nid 1@foo &&
		error "Command should have failed"

	echo "Invalid nid format"
	local invalid_nids="1@tcp 1@o2ib 1.1.1.1@gni"

	local nid
	for nid in ${invalid_nids}; do
		echo "Check invalid primary nid - '$nid'"
		do_lnetctl peer add --prim_nid $nid &&
			error "Command should have failed"
	done

	local invalid_strs="[2-1]@gni [a-f/x]@gni 256.256.256.256@tcp"
	invalid_strs+=" 1.1.1.1.[2-5/f]@tcp 1.]2[.3.4@o2ib"
	invalid_strs+="1.[2-4,[5-6],7-8].1.1@tcp foobar"

	local nidstr
	for nidstr in ${invalid_strs}; do
		echo "Check invalid nidstring - '$nidstr'"
		do_lnetctl peer add --prim_nid 1.1.1.1@tcp --nid $nidstr &&
			error "Command should have failed"
	done

	echo "Add non-local gateway"
	do_lnetctl route add --net tcp --gateway 1@gni &&
		error "Command should have failed"

	return 0
}
run_test 99a "Check various invalid inputs to lnetctl peer"

test_99b() {
	reinit_dlc || return $?

	create_base_yaml_file

	cat <<EOF > $TMP/sanity-lnet-$testnum-invalid.yaml
peer:
    - primary nid: 99.99.99.99@tcp
      Multi-Rail: Foobar
      peer ni:
        - nid: 99.99.99.99@tcp
EOF
	do_lnetctl import < $TMP/sanity-lnet-$testnum-invalid.yaml &&
		error "import should have failed"
	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
	compare_yaml_files
}
run_test 99b "Invalid value for Multi-Rail in yaml import"

del_net() {
	local net="$1"
	local if="$2"

	do_lnetctl net del --net ${net} --if ${if} ||
		error "Failed to del net ${net} on if ${if}"
}

compare_route_add() {
	local rnet="$1"
	local gw="$2"

	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"

	do_lnetctl route add --net ${rnet} --gateway ${gw} ||
		error "route add failed $?"

	echo "$LNETCTL export --backup > $actual"

	$LNETCTL export --backup > $actual ||
		error "export failed $?"

	validate_gateway_nids
	return $?
}

append_net_tunables() {
	local net=${1:-tcp}

	$LNETCTL net show -v --net ${net} | grep -v 'dev cpt' |
		awk '/^\s+tunables:$/,/^\s+CPT:/' >> $TMP/sanity-lnet-$testnum-expected.yaml
}

ROUTERS=()
declare -A ROUTER_INTERFACES
RPEERS=()
declare -A RPEER_INTERFACES
NTRB=16
init_router_test_vars() {
	local routers_required="$1"
	local rpeers_required="$2"

	local rnodes_required
	((rnodes_required=routers_required+rpeers_required))
	# all remote nodes, including some that may not be used
	local rnodes_all=( $(remote_nodes_list | tr ',' ' ') )
	[[ -z $rnodes_all || "${#rnodes_all[@]}" -lt $rnodes_required ]] &&
		skip "Need at least $rnodes_required remote nodes" \
			"found \"${rnodes_all[@]}\""

	ROUTERS=( "${rnodes_all[@]:0:${routers_required}}" )
	RPEERS=( "${rnodes_all[@]:${routers_required}:${rpeers_required}}" )

	local rnodes=$(comma_list ${ROUTERS[@]} ${RPEERS[@]})
	local all_nodes=$(comma_list ${ROUTERS[@]} ${RPEERS[@]} $HOSTNAME)

	do_nodes $rnodes $LUSTRE_RMMOD ||
		error "failed to unload modules"

	do_rpc_nodes $rnodes "load_lnet" ||
		error "Failed to load LNet"

	do_nodes $rnodes "$LNETCTL lnet configure $LNET_CONFIG_INIT_OPT" ||
		error "Failed to configure LNet on $rnodes rc = $?"

	for router in ${ROUTERS[@]}; do
		ROUTER_INTERFACES[$router]=$(do_rpc_nodes --quiet \
						$router lnet_if_list)
	done

	for rpeer in ${RPEERS[@]}; do
		RPEER_INTERFACES[$rpeer]=$(do_rpc_nodes --quiet \
						$rpeer lnet_if_list)
	done

	# test_256 needs tiny_router_buffers > (# CPTs * peer credits)
	local rtr_list=$(comma_list ${ROUTERS[@]})
	local max_ncpt=$(do_nodes $rtr_list "$LCTL get_param -n cpu_partition_table | wc -l" |
			 sort -ug | tail -n 1)
	local max_pcs=$(do_nodes $rtr_list "$LNETCTL net show -v" |
			awk '/peer_credits:/{print $NF}' |
			sort -ug | tail -n 1)
	((NTRB=max_ncpt*(max_pcs+1)))

	do_nodes $all_nodes $LUSTRE_RMMOD ||
		error "Failed to unload modules"

	[[ ${#INTERFACES[@]} -eq 0 ]] &&
		error "No interfaces configured for local host $HOSTNAME"
	for router in ${!ROUTER_INTERFACES[@]}; do
		[[ -z "${ROUTER_INTERFACES[$router]}" ]] &&
			error "No interfaces configured for router $router"
	done
	for rpeer in ${!RPEER_INTERFACES[@]}; do
		[[ -z "${RPEER_INTERFACES[$rpeer]}" ]] &&
			error "No interfaces configured for remote peer $rpeer"
	done

	return 0
}

do_net_add() {
	local node=$1
	local net=$2
	local if=$3

	do_rpc_nodes $node "$LNETCTL net add --net $net --if $if" ||
		error "add $net on interface $if on node $node failed rc=$?"
}

do_route_add() {
	local node=$1
	local net=$2
	local gw=$3

	do_node $node "$LNETCTL route add --net $net --gateway $gw" ||
		error "route add to $net via $gw failed rc=$?"
}

declare -A ROUTER_NIDS
declare -A RPEER_NIDS
LNIDS=()
LOCAL_NET=${NETTYPE}
LOCAL_NET2=${NETTYPE}2
REMOTE_NET=${NETTYPE}1
REMOTE_NET2=${NETTYPE}3
setup_router_test() {
	(( $MDS1_VERSION >= $(version_code 2.15.0) )) ||
		skip "need at least 2.15.0 for load_lnet"

	local routers_required=1
	local rpeers_required=1
	local mr_peers=false
	local flag

	while getopts "mp:r:" flag; do
		case $flag in
			m) mr_peers=true;;
			p) rpeers_required="$OPTARG";;
			r) routers_required="$OPTARG";;
			*) ;;
		esac
	done
	shift $((OPTIND - 1))

	local mod_opts="$@"

	if [[ ${#RPEER_INTERFACES[@]} -eq 0 ]]; then
		init_router_test_vars $routers_required $rpeers_required ||
			return $?
	fi

	local all_nodes=$(comma_list ${ROUTERS[@]} ${RPEERS[@]} $HOSTNAME)

	do_nodes $all_nodes $LUSTRE_RMMOD ||
		error "failed to unload modules"

	mod_opts+=" alive_router_check_interval=5"
	mod_opts+=" router_ping_timeout=5"
	mod_opts+=" large_router_buffers=4"
	mod_opts+=" small_router_buffers=8"
	mod_opts+=" tiny_router_buffers=$NTRB"
	do_rpc_nodes $all_nodes load_lnet "${mod_opts}" ||
		error "Failed to load lnet"

	do_nodes $all_nodes "$LNETCTL lnet configure $LNET_CONFIG_OPT" ||
		error "Failed to initialize DLC"

	for router in ${!ROUTER_INTERFACES[@]}; do
		local router_interfaces=( ${ROUTER_INTERFACES[$router]} )

		do_net_add $router $LOCAL_NET ${router_interfaces[0]} ||
			return $?
		do_net_add $router $REMOTE_NET ${router_interfaces[0]} ||
			return $?
		if $mr_peers; then
			do_net_add $router $LOCAL_NET2 ${router_interfaces[0]} ||
				return $?
			do_net_add $router $REMOTE_NET2 ${router_interfaces[0]} ||
				return $?
		fi
	done

	for rpeer in ${!RPEER_INTERFACES[@]}; do
		local rpeer_interfaces=( ${RPEER_INTERFACES[$rpeer]} )

		do_net_add $rpeer $REMOTE_NET ${rpeer_interfaces[0]} ||
			return $?
		if $mr_peers; then
			do_net_add $rpeer $REMOTE_NET2 ${rpeer_interfaces[0]} ||
				return $?
		fi
	done

	add_net $LOCAL_NET ${INTERFACES[0]} ||
		return $?
	if $mr_peers; then
		add_net $LOCAL_NET2 ${INTERFACES[0]} ||
			return $?
	fi

	for router in ${!ROUTER_INTERFACES[@]}; do
		ROUTER_NIDS[$router]=$(do_node $router $LCTL list_nids \
				       2>/dev/null | xargs echo)
		echo "router: $router nids: ${ROUTER_NIDS[$router]}"
	done

	for rpeer in ${!RPEER_INTERFACES[@]}; do
		RPEER_NIDS[$rpeer]=$(do_node $rpeer $LCTL list_nids \
				     2>/dev/null | xargs echo)
		echo "rpeer: $rpeer nids: ${RPEER_NIDS[$rpeer]}"
	done

	LNIDS=( $($LCTL list_nids 2>/dev/null | xargs echo) )
	echo "local: $HOSTNAME nids: ${LNIDS[@]}"
}

do_route_del() {
	local node=$1
	local net=$2
	local gw=$3

	do_nodesv $node \
	"output=\\\"\\\$($LNETCTL route show --net $net --gateway $gw 2>/dev/null)\\\"; \
		if [[ -n \\\"\\\${output}\\\" ]]; then			\
			echo \\\"Delete route to $net via $gw\\\";	\
			$LNETCTL route del --net $net --gateway $gw;	\
		else							\
			exit 0;						\
		fi"
}

cleanup_router_test() {
	local all_nodes=$(comma_list $HOSTNAME ${ROUTERS[@]} ${RPEERS[@]})

	for router in ${!ROUTER_NIDS[@]}; do
		local router_nids=( ${ROUTER_NIDS[$router]} )

		do_route_del $HOSTNAME $REMOTE_NET ${router_nids[0]} ||
			error "Failed to delete $HOSTNAME -> "\
				"$REMOTE_NET via ${router_nids[0]} route"
	done

	for router in ${!ROUTER_INTERFACES[@]}; do
		local router_nids=( ${ROUTER_NIDS[$router]} )

		do_route_del $rpeer $LOCAL_NET ${router_nids[1]} ||
			error "Failed to delete $rpeer -> "\
				"$LOCAL_NET via ${router_nids[1]} route"
	done

	do_nodes $all_nodes $LUSTRE_RMMOD ||
		error "failed to unload modules"

	return 0
}

test_100() {
	define_global_yaml
	setup_router_test || return $?
	local router_nids=( ${ROUTER_NIDS[${ROUTERS[0]}]} )

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
net:
    - net type: $LOCAL_NET
      local NI(s):
        - interfaces:
              0: ${INTERFACES[0]}
EOF
	append_net_tunables $LOCAL_NET
	cat <<EOF >> $TMP/sanity-lnet-$testnum-expected.yaml
route:
    - net: $REMOTE_NET
      gateway: ${router_nids[0]}
      hop: -1
      priority: 0
peer:
    - primary nid: ${router_nids[0]}
      Multi-Rail: False
      peer ni:
        - nid: ${router_nids[0]}
EOF
	append_global_yaml
	compare_route_add "$REMOTE_NET" "${router_nids[0]}"
	cleanup_router_test
}
run_test 100 "Add route with single gw"

ARR_IF0_IP=($(ip -o -4 a s ${INTERFACES[0]} |
	    awk '{print $4}' | sed 's/\/.*//'))
echo "Total IP for ${INTERFACES[0]} found [${#ARR_IF0_IP[@]}]"
echo "Interface:IP are"
for i in ${ARR_IF0_IP[@]}; do
	echo "${INTERFACES[0]}:$i"
done
IF0_IP=${ARR_IF0_IP[0]}
IF0_NET=$(awk -F. '{print $1"."$2"."$3}'<<<"${IF0_IP}")
IF0_HOSTNUM=$(awk -F. '{print $4}'<<<"${IF0_IP}")
if (((IF0_HOSTNUM + 5) > 254)); then
	GW_HOSTNUM=1
else
	GW_HOSTNUM=$((IF0_HOSTNUM + 1))
fi
GW_NID="${IF0_NET}.${GW_HOSTNUM}@${NETTYPE}"
echo "Using GW_NID:$GW_NID"

test_101() {
	[[ -n $ARR_IF0_IP ]] || skip "Need IPv4 NIDs"

	reinit_dlc || return $?

	add_net "${NETTYPE}" "${INTERFACES[0]}"

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
net:
    - net type: ${NETTYPE}
      local NI(s):
        - interfaces:
              0: ${INTERFACES[0]}
EOF
	append_net_tunables ${NETTYPE}

	echo "route:" >> $TMP/sanity-lnet-$testnum-expected.yaml
	for i in $(seq $GW_HOSTNUM $((GW_HOSTNUM + 4))); do
		cat <<EOF >> $TMP/sanity-lnet-$testnum-expected.yaml
    - net: ${REMOTE_NET}
      gateway: ${IF0_NET}.${i}@${NETTYPE}
      hop: -1
      priority: 0
EOF
	done

	echo "peer:" >> $TMP/sanity-lnet-$testnum-expected.yaml
	for i in $(seq $GW_HOSTNUM $((GW_HOSTNUM + 4))); do
		cat <<EOF >> $TMP/sanity-lnet-$testnum-expected.yaml
    - primary nid: ${IF0_NET}.${i}@${NETTYPE}
      Multi-Rail: False
      peer ni:
        - nid: ${IF0_NET}.${i}@${NETTYPE}
EOF
	done
	append_global_yaml

	local gw="${IF0_NET}.[$GW_HOSTNUM-$((GW_HOSTNUM + 4))]@${NETTYPE}"

	compare_route_add "${REMOTE_NET}" "${gw}"
}
run_test 101 "Add route with multiple gw"

compare_route_del() {
	local rnet="$1"
	local gw="$2"

	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"

	do_lnetctl route del --net ${rnet} --gateway ${gw} ||
		error "route del failed $?"
	$LNETCTL export --backup > $actual ||
		error "export failed $?"
	validate_gateway_nids
}

test_102() {
	define_global_yaml
	setup_router_test || return $?
	local router_nids=( ${ROUTER_NIDS[${ROUTERS[0]}]} )

	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml

	do_lnetctl route add --net $REMOTE_NET --gateway ${router_nids[0]} ||
		error "route add failed $?"

	compare_route_del "$REMOTE_NET" "${router_nids[0]}"
	cleanup_router_test
}
run_test 102 "Delete route with single gw"

NUM_NID_EXPR='[103-120/4]'
test_103() {
	[[ -n $ARR_IF0_IP ]] || skip "Need IPv4 NIDs"

	reinit_dlc || return $?
	add_net "${NETTYPE}" "${INTERFACES[0]}"
	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml

	local nid_expr

	if [[ $NETTYPE =~ (tcp|o2ib)[0-9]* ]]; then
		nid_expr="${IF0_NET}.[$GW_HOSTNUM-$((GW_HOSTNUM+5))/2]"
	else
		nid_expr="${NUM_NID_EXPR}"
	fi

	do_lnetctl route add --net ${NETTYPE}103 \
		--gateway ${nid_expr}@${NETTYPE} ||
		error "route add failed $?"
	compare_route_del "${NETTYPE}103" "${nid_expr}@${NETTYPE}"
}
run_test 103 "Delete route with multiple gw"

test_104() {
	local tyaml="$TMP/sanity-lnet-$testnum-expected.yaml"

	reinit_dlc || return $?

	# Default value is '3'
	local val=$($LNETCTL global show | awk '/response_tracking/{print $NF}')
	[[ $val -ne 3 ]] &&
		error "Expect 3 found $val"

	echo "Set < 0;  Should fail"
	do_lnetctl set response_tracking -1 &&
		error "should have failed $?"

	reinit_dlc || return $?
	cat <<EOF > $tyaml
global:
    response_tracking: -10
EOF
	do_lnetctl import < $tyaml &&
		error "should have failed $?"

	echo "Check valid values; Should succeed"
	local i
	for ((i = 0; i < 4; i++)); do
		reinit_dlc || return $?
		do_lnetctl set response_tracking $i ||
			error "should have succeeded $?"
		$LNETCTL global show | grep -q "response_tracking: $i" ||
			error "Failed to set response_tracking to $i"
		reinit_dlc || return $?
		cat <<EOF > $tyaml
global:
    response_tracking: $i
EOF
		do_lnetctl import < $tyaml ||
			error "should have succeeded $?"
		$LNETCTL global show | grep -q "response_tracking: $i" ||
			error "Failed to set response_tracking to $i"
	done

	reinit_dlc || return $?
	echo "Set > 3; Should fail"
	do_lnetctl set response_tracking 4 &&
		error "should have failed $?"

	reinit_dlc || return $?
	cat <<EOF > $tyaml
global:
    response_tracking: 10
EOF
	do_lnetctl import < $tyaml &&
		error "should have failed $?"
	return 0
}
run_test 104 "Set/check response_tracking param"

test_105() {
	define_global_yaml
	setup_router_test || return $?
	local router_nids=( ${ROUTER_NIDS[${ROUTERS[0]}]} )

	do_lnetctl route add --net $REMOTE_NET --gateway ${router_nids[0]} ||
		error "route add failed $?"
	do_lnetctl peer add --prim ${router_nids[0]} &&
		error "peer add should fail"

	cleanup_router_test
}
run_test 105 "Adding duplicate GW peer should fail"

test_106() {
	define_global_yaml
	setup_router_test || return $?
	local router_nids=( ${ROUTER_NIDS[${ROUTERS[0]}]} )

	do_lnetctl route add --net $REMOTE_NET --gateway ${router_nids[0]} ||
		error "route add failed $?"
	do_lnetctl peer del --prim ${router_nids[0]} &&
		error "peer del should fail"

	cleanup_router_test
}
run_test 106 "Deleting GW peer should fail"

test_107() {
	[[ ${NETTYPE} == tcp* ]] || skip "Need tcp NETTYPE"

	cleanup_lnet || error "Failed to unload modules before test execution"

	setup_fakeif || error "Failed to add fake IF"

	reinit_dlc || return $?

	add_net "tcp" "${INTERFACES[0]}" || return $?
	add_net "tcp" "$FAKE_IF" || return $?

	del_net "tcp" "$FAKE_IF" || return $?

	cleanup_fakeif
	cleanup_lnet
}
run_test 107 "Deleting extra interface doesn't crash node"

test_108() {
	[[ ${NETTYPE} == tcp* ]] || skip "Need tcp NETTYPE"

	cleanup_lnet || error "Failed to unload modules before test execution"

	setup_fakeif || error "Failed to add fake IF"

	reinit_dlc || return $?

	add_net "tcp" "${INTERFACES[0]}" || return $?
	$LNETCTL net show > $TMP/sanity-lnet-$testnum-expected.yaml
	add_net "tcp" "$FAKE_IF" || return $?

	cat <<EOF >> $TMP/sanity-lnet-$testnum-expected.yaml
      -     nid: $FAKE_NID
            status: up
            interfaces:
                  0: ${FAKE_IF}
EOF
	$LNETCTL net show > $TMP/sanity-lnet-$testnum-actual.yaml
	compare_yaml_files || error "not all interfaces were setup"

	cleanup_fakeif
	cleanup_lnet

	return 0
}
run_test 108 "Check Multi-Rail setup"

test_109() {
	[[ ${NETTYPE} == tcp* ]] || skip "Need tcp NETTYPE"

	cleanup_lnet || error "Failed to unload modules before test execution"

	setup_fakeif || error "Failed to add fake IF"

	FAKE_IF_ALIAS="${FAKE_IF}"
	FAKE_IF_ALIAS+=":0"

	ifconfig "$FAKE_IF_ALIAS" "$FAKE_IP_ALIAS" up ||
		error "Failed to add fake IF alias"

	reinit_dlc || return $?

	# add interface with longer name first
	add_net "tcp" "$FAKE_IF_ALIAS" || return $?
	add_net "tcp" "$FAKE_IF" || return $?

	del_net "tcp" "$FAKE_IF" || return $?
	del_net "tcp" "$FAKE_IF_ALIAS" || return $?

	# add interface with shorter name first
	add_net "tcp" "$FAKE_IF" || return $?
	add_net "tcp" "$FAKE_IF_ALIAS" || return $?

	ifconfig "$FAKE_IF_ALIAS" "$FAKE_IP_ALIAS" down ||
		error "Failed to clean up fake IF alias"

	cleanup_fakeif
	cleanup_lnet
}
run_test 109 "Add NI using a network interface alias (LU-16859)"

test_110() {
	[[ $NETTYPE =~ (tcp|o2ib) ]] || skip "Need tcp or o2ib NETTYPE"

	reinit_dlc || return $?

	add_net ${NETTYPE} ${INTERFACES[0]} || return $?

	local nid=$($LCTL list_nids)

	reinit_dlc || return $?

	do_lnetctl net add --nid $nid ||
		error "Failed to add net via nid \"$nid\""

	$LNETCTL net show --net $NETTYPE | grep -q "nid: $nid" ||
		error "Failed to configure $nid"

	do_lnetctl net del --nid $nid ||
		error "Failed to del net via nid \"$nid\""

	$LNETCTL net show | grep -q "nid: $nid" &&
		error "$nid should have been deleted"

	cleanup_lnet
}
run_test 110 "Configure NI using lnetctl net add --nid"

test_111() {
	[[ $(uname -r | grep "3.10") ]] &&
		skip "Unsupported on RHEL7"

	setup_router_test || return $?

	local i
	local gw=( ${ROUTER_NIDS[${ROUTERS[0]}]} )

	for i in {2..500}
	do
		do_lnetctl route add -n ${REMOTE_NET}${i} -g ${gw[0]} ||
			error "Failed to add route rc=$?"
	done

	local num_routes=$($LNETCTL route show 2>/dev/null | grep -c gateway)

	(( num_routes == 499 )) ||
		error "Expect 499 routes but lnetctl found $num_routes"

	num_routes=$($LCTL show_route 2>/dev/null | grep -c net)
	(( num_routes == 499 )) ||
		error "Epect 499 routes but lctl found $num_routes"

	cleanup_router_test
}
run_test 111 "Test many routes"

test_112() {
	cleanup_lnet || error "Failed to unload modules before test execution"

	setup_fakeif || error "Failed to add fake IF"

	reinit_dlc || return $?

	$LNETCTL net add --net tcp1 --if ${INTERFACES[0]}
	$LNETCTL net add --nid ${FAKE_IP}@tcp2

	local count=$($LNETCTL net show -v 3 2>/dev/null | grep -c "lnd tunables:")
	(( count == 2 )) || error "missing lnd tunables"

	cleanup_lnet
	cleanup_fakeif
}
run_test 112 "multiple net configurations"

do_import_test() {
	reinit_dlc || return $?

	cat ${1:-"$TMP/sanity-lnet-$testnum-expected.yaml"}

	do_lnetctl import ${1:-"$TMP/sanity-lnet-$testnum-expected.yaml"} ||
		error "Import failed with rc = $?"

	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml

	compare_yaml_files || return $?
}

test_150() {
	reinit_dlc || return $?

	add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?

	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml

	cat <<EOF > $TMP/sanity-lnet.yaml
ip2nets:
  - net-spec: ${NETTYPE}
    interfaces:
        0: ${INTERFACES[0]}
    ip-range:
        0: $(ip -o -4 a s ${INTERFACES[0]} | awk '{print $4}' | sed 's/\/.*//')
EOF

	do_import_test "$TMP/sanity-lnet.yaml" || return $?
}
run_test 150 "Check import of ip2nets yaml sequence"

test_151() {
	[[ $NETTYPE == tcp* ]] || skip "Need tcp nettype"

	cleanup_lnet || error "Failed to unload modules before test execution"

	setup_fakeif || error "Failed to add fake IF"

	reinit_dlc || return $?

	add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?
	add_net "${NETTYPE}" "${FAKE_IF}" || return $?

	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml

	local if0_ip=$(ip -o -4 a s ${INTERFACES[0]} | awk '{print $4}' |
		       sed 's/\/.*//')

	echo "Check import of ip2nets with two matching interfaces"

	cat <<EOF > $TMP/sanity-lnet-$testnum.yaml
ip2nets:
  - net-spec: ${NETTYPE}
    interfaces:
        0: ${INTERFACES[0]}
        1: ${FAKE_IF}
    ip-range:
        0: ${if0_ip}
        1: ${FAKE_IP}
EOF

	do_import_test "$TMP/sanity-lnet-$testnum.yaml" || return $?

	echo "Additional rules for same net should not change config"

	cat <<EOF >> $TMP/sanity-lnet-$testnum.yaml
  - net-spec: ${NETTYPE}
    interfaces:
        0: ${INTERFACES[0]}
  - net-spec: ${NETTYPE}
    interfaces:
        0: ${FAKE_IF}
    ip-range:
        0: ${FAKE_IP}
EOF

	do_import_test "$TMP/sanity-lnet-$testnum.yaml" || return $?

	add_net "${NETTYPE}2" "${INTERFACES[0]}"

	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml

	echo "Additional rule for second net should apply"

	cat <<EOF >> $TMP/sanity-lnet-$testnum.yaml
  - net-spec: ${NETTYPE}2
    interfaces:
        0: ${INTERFACES[0]}
EOF

	do_import_test "$TMP/sanity-lnet-$testnum.yaml" || return $?

	cleanup_fakeif
}
run_test 151 "Check correct application of multiple ip2nets rules"

test_152() {
	[[ $NETTYPE == tcp* ]] || skip "Need tcp nettype"

	cleanup_lnet || error "Failed to unload modules before test execution"

	reinit_dlc || return $?

	add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?

	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml

	local if0_ip=$(ip -o -4 a s ${INTERFACES[0]} | awk '{print $4}' |
		       sed 's/\/.*//')
	local i range

	echo "Check import of ip2nets with wildcard IP patterns"

	for i in {3..1}; do
		range=$(echo "$if0_ip" | cut -d. -f1-$i)
		for ((j = 3; j >= $i; j--)); do
			range="${range:+$range.}*"
		done

		cat <<EOF > $TMP/sanity-lnet-$testnum.yaml
ip2nets:
  - net-spec: ${NETTYPE}
    interfaces:
        0: ${INTERFACES[0]}
    ip-range:
        0: ${range}
EOF

		do_import_test "$TMP/sanity-lnet-$testnum.yaml" || return $?
	done

	setup_fakeif || error "Failed to add fake IF"

	echo "Check match-all expression mask later rules"

	cat <<EOF > $TMP/sanity-lnet-$testnum.yaml
ip2nets:
  - net-spec: ${NETTYPE}
    interfaces:
        0: ${INTERFACES[0]}
    ip-range:
        0: "*.*.*.*"
  - net-spec: ${NETTYPE}
    interfaces:
        0: ${INTERFACES[0]}
        1: ${FAKE_IF}
  - net-spec: ${NETTYPE}
    interfaces:
        0: ${INTERFACES[0]}
        1: ${FAKE_IF}
    ip-range:
        0: $if0_ip
        1: $FAKE_IP
EOF

	do_import_test "$TMP/sanity-lnet-$testnum.yaml" || return $?

	cleanup_fakeif
}
run_test 152 "Check import of ip2nets with wildcard IP patterns"

create_ipv4_range() {
	local stepped=${1:-false}

	local if0_ip=$(ip -o -4 a s ${INTERFACES[0]} | awk '{print $4}' |
		       sed 's/\/.*//')
	local if0_net=$(echo "$if0_ip" | cut -d. -f1-3)
	local if0_host=$(echo "$if0_ip" | cut -d. -f4)
	local range_start=$((if0_host - 4 < 0 ? 0 : if0_host - 4))
	local range_end=$((if0_host + 4 > 255 ? 255 : if0_host + 4))

	if ${stepped}; then
		(((if0_host - range_start) % 2 == 0)) || range_start=${if0_host}

		echo "${if0_net}.[${range_start}-${range_end}/2]"
	else
		echo "${if0_net}.[${range_start}-${range_end}]"
	fi
}

test_153() {
	[[ $NETTYPE == tcp* ]] || skip "Need tcp nettype"

	cleanup_lnet || error "Failed to unload modules before test execution"

	reinit_dlc || return $?

	add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?

	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml

	local ip_range=$(create_ipv4_range)

	echo "Check import of ip2nets with IP address ranges"

	cat <<EOF > $TMP/sanity-lnet-$testnum.yaml
ip2nets:
  - net-spec: ${NETTYPE}
    interfaces:
        0: ${INTERFACES[0]}
    ip-range:
        0: ${ip_range}
EOF

	do_import_test "$TMP/sanity-lnet-$testnum.yaml" || return $?
}
run_test 153 "Check import of ip2nets with IP address ranges"

test_154() {
	[[ $NETTYPE == tcp* ]] || skip "Need tcp nettype"

	cleanup_lnet || error "Failed to unload modules before test execution"

	setup_fakeif || error "Failed to add fake IF"

	reinit_dlc || return $?

	add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?

	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml

	local ip_range=$(create_ipv4_range true)

	echo "Check import of ip2nets with stepped IP address ranges"

	cat <<EOF > $TMP/sanity-lnet-$testnum.yaml
ip2nets:
  - net-spec: ${NETTYPE}
    interfaces:
        0: ${INTERFACES[0]}
    ip-range:
        0: ${ip_range}
EOF

	do_import_test "$TMP/sanity-lnet-$testnum.yaml" || return $?

	cleanup_fakeif
}
run_test 154 "Check import of ip2nets with stepped IP address ranges"

test_155() {
	[[ $NETTYPE == tcp* ]] || skip "Need tcp nettype"

	reinit_dlc || return $?

	local if0_ip=$(ip -o -4 a s ${INTERFACES[0]} | awk '{print $4}' |
		       sed 's/\/.*//')

	echo "Check import with no matching rules (should fail)"

	cat <<EOF > $TMP/sanity-lnet-$testnum.yaml
ip2nets:
  - net-spec: ${NETTYPE}
    interfaces:
        0: ${INTERFACES[0]}
    ip-range:
        0: 10.20.30.40
EOF

	! do_lnetctl import "$TMP/sanity-lnet-$testnum.yaml" ||
		error "Import should have failed"
}
run_test 155 "Check ip2nets import failure with no matching rules"

test_156() {
	[[ $NETTYPE == tcp* ]] || skip "Need tcp nettype"

	cleanup_lnet || error "Failed to unload modules before test execution"

	reinit_dlc || return $?

	add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?

	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml

	local if0_ip=$(ip -o -4 a s ${INTERFACES[0]} | awk '{print $4}' |
		       sed 's/\/.*//')
	local if0_net3=$(echo "$if0_ip" | cut -d. -f1-3)
	local if0_host=$(echo "$if0_ip" | cut -d. -f4)
	local host2=$if0_host
	local i

	for i in {1..4}; do
		((host2 + 2 < 255)) &&
			host2=$((host2 + 2)) ||
			host2=1
		range=${range:+$range,}$host2
		((i == 2)) && range=${range},$if0_host
	done

	echo "Check import of ip2nets with comma-separated IP list"

	cat <<EOF > $TMP/sanity-lnet-$testnum.yaml
ip2nets:
  - net-spec: ${NETTYPE}
    interfaces:
        0: ${INTERFACES[0]}
    ip-range:
        0: ${if0_net3}.[${range}]
EOF

	do_import_test "$TMP/sanity-lnet-$testnum.yaml"
}
run_test 156 "Check import of ip2nets with comma-separated IP ranges"

test_157() {
	reinit_dlc || return $?

	echo "Check import failure with missing net-spec"

	cat <<EOF > $TMP/sanity-lnet-$testnum.yaml
ip2nets:
  - interfaces:
        0: ${INTERFACES[0]}
    ip-range:
        0: 192.168.1.1
EOF

	! do_lnetctl import "$TMP/sanity-lnet-$testnum.yaml" ||
		error "Import should have failed with missing net-spec"
}
run_test 157 "Check import failure with malformed ip2nets YAML"

test_158() {
	[[ $NETTYPE == tcp* ]] || skip "Need tcp nettype"

	intf_has_ipv6 ${INTERFACES[0]} || skip "Interface has no IPv6"

	reinit_dlc || return $?

	add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?

	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml

	local if0_ipv6=$(ip -o -6 a s ${INTERFACES[0]} | awk '{print $4}' |
			 grep -v '^fe80::' | head -n 1 | cut -d/ -f1)

	echo "Check import of ip2nets with single IPv6 address"

	cat <<EOF > $TMP/sanity-lnet-$testnum.yaml
ip2nets:
  - net-spec: ${NETTYPE}
    interfaces:
        0: ${INTERFACES[0]}
    ip-range:
        0: ${if0_ipv6}
EOF

	do_import_test "$TMP/sanity-lnet-$testnum.yaml" || return $?
}
run_test 158 "Check import of ip2nets with single IPv6 address"

test_159() {
	[[ $NETTYPE == tcp* ]] || skip "Need tcp nettype"

	intf_has_ipv6 ${INTERFACES[0]} || skip "Interface has no IPv6"

	reinit_dlc || return $?

	add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?

	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml

	local if0_ipv6=$(ip -o -6 a s ${INTERFACES[0]} | awk '{print $4}' |
			 grep -v '^fe80::' | head -n 1 | cut -d/ -f1)

	echo "Check import of ip2nets with IPv6 /128 CIDR"

	cat <<EOF > $TMP/sanity-lnet-$testnum.yaml
ip2nets:
  - net-spec: ${NETTYPE}
    interfaces:
        0: ${INTERFACES[0]}
    ip-range:
        0: ${if0_ipv6}/128
EOF

	do_import_test "$TMP/sanity-lnet-$testnum.yaml" || return $?
}
run_test 159 "Check import of ip2nets with IPv6 /128 CIDR notation"

test_160() {
	[[ $NETTYPE == tcp* ]] || skip "Need tcp nettype"

	intf_has_ipv6 ${INTERFACES[0]} || skip "Interface has no IPv6"

	cleanup_lnet || error "Failed to unload modules before test execution"

	setup_fakeif || error "Failed to add fake IF"

	reinit_dlc || return $?

	add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?
	add_net "${NETTYPE}" "${FAKE_IF}" || return $?

	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml

	local if0_ipv6=$(ip -o -6 a s ${INTERFACES[0]} | awk '{print $4}' |
			 grep -v '^fe80::' | head -n 1 | cut -d/ -f1)

	echo "Check import of ip2nets with two IPv6 interfaces"

	cat <<EOF > $TMP/sanity-lnet-$testnum.yaml
ip2nets:
  - net-spec: ${NETTYPE}
    interfaces:
        0: ${INTERFACES[0]}
        1: ${FAKE_IF}
    ip-range:
        0: ${if0_ipv6}
        1: ${FAKE_IPV6}
EOF

	do_import_test "$TMP/sanity-lnet-$testnum.yaml" || return $?

	echo "Additional rules for same net should not change config"

	cat <<EOF >> $TMP/sanity-lnet-$testnum.yaml
  - net-spec: ${NETTYPE}
    interfaces:
        0: ${INTERFACES[0]}
  - net-spec: ${NETTYPE}
    interfaces:
        0: ${FAKE_IF}
    ip-range:
        0: ${FAKE_IPV6}
EOF

	do_import_test "$TMP/sanity-lnet-$testnum.yaml" || return $?

	add_net "${NETTYPE}2" "${INTERFACES[0]}"

	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml

	echo "Additional rule for second net should apply"

	cat <<EOF >> $TMP/sanity-lnet-$testnum.yaml
  - net-spec: ${NETTYPE}2
    interfaces:
        0: ${INTERFACES[0]}
    ip-range:
        0: ${if0_ipv6}
EOF

	do_import_test "$TMP/sanity-lnet-$testnum.yaml" || return $?

	cleanup_fakeif
}
run_test 160 "Check correct application of multiple IPv6 ip2nets rules"

test_161() {
	[[ $NETTYPE == tcp* ]] || skip "Need tcp nettype"

	intf_has_ipv6 ${INTERFACES[0]} || skip "Interface has no IPv6"

	reinit_dlc || return $?

	add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?

	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml

	local if0_ipv6=$(ip -o -6 a s ${INTERFACES[0]} | awk '{print $4}' |
			 grep -v '^fe80::' | head -n 1)
	local network_addr=$(echo "$if0_ipv6" | cut -d/ -f1)
	local prefix_len=$(cut -d/ -f2<<<"$if0_ipv6")

	[[ -n $network_addr && -n $prefix_len ]] ||
		error "Failed to get net addr or prefix len"

	echo "Check import of ip2nets with various IPv6 prefix lengths"

	local i

	for i in {1..4}; do
		cat <<EOF > $TMP/sanity-lnet-$testnum.yaml
ip2nets:
  - net-spec: ${NETTYPE}
    interfaces:
        0: ${INTERFACES[0]}
    ip-range:
        0: ${network_addr}/${prefix_len}
EOF

		echo "Testing with /${prefix_len} prefix"
		do_import_test "$TMP/sanity-lnet-$testnum.yaml" || return $?

		prefix_len=$((prefix_len/2))
	done
}
run_test 161 "Check import of ip2nets with various IPv6 prefix lengths"

test_162() {
	[[ $NETTYPE == tcp* ]] || skip "Need tcp nettype"

	intf_has_ipv6 ${INTERFACES[0]} || skip "Interface has no IPv6"

	reinit_dlc || return $?

	local if0_ipv6=$(ip -o -6 a s ${INTERFACES[0]} | awk '{print $4}' |
			 grep -v '^fe80::' | head -n 1 | sed 's,/[0-9]\+$,,')

	echo "Check import with non-matching IPv6 address (should fail)"

	cat <<EOF > $TMP/sanity-lnet-$testnum.yaml
ip2nets:
  - net-spec: ${NETTYPE}
    interfaces:
        0: ${INTERFACES[0]}
    ip-range:
        0: 2001:db8:dead:beef::1
EOF

	! do_lnetctl import "$TMP/sanity-lnet-$testnum.yaml" ||
		error "Import should have failed with non-matching IPv6"
}
run_test 162 "Check ip2nets import failure with non-matching IPv6"

check_tunable_warning() {
	dmesg | tac | sed '/LNet: Added LNI/q' | \
		grep -q "All tunables of NIs of a net should be the same"
}

test_170() {
	[[ ${NETTYPE} == tcp* ]] || skip "Test written for tcp"

	cleanup_lnet || error "Failed to unload modules before test execution"
	cleanup_netns || error "Failed to cleanup netns before test execution"
	setup_fakeif || error "Failed to add fake IF"
	reinit_dlc || return $?

	# Disable console ratelimit
	rlpath="/sys/module/libcfs/parameters/libcfs_console_ratelimit"
	local rl=$(cat $rlpath)
	echo 0 > $rlpath

	local IMPORT="do_lnetctl import --old-api"
	local expected="$TMP/sanity-lnet-$testnum-expected.yaml"
	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"

	echo "Different CPTs"
	cat <<EOF > $expected
net:
-     net type: ${NETTYPE}
      local NI(s):
        - interfaces:
                0: ${INTERFACES[0]}
          CPT: "[0]"
        - interfaces:
                0: ${FAKE_IF}
          CPT: "[1]"
EOF
	$IMPORT <  ${expected} || error "Import failed $?"
	cat <<EOF > $expected
CPT:"[0]"
CPT:"[1]"
EOF
	$LNETCTL export --backup | grep CPT: | tr -d ' ' > $actual
	diff $expected $actual || error "Bad parsing"
	reinit_dlc

	echo "lnd_tunables not read if no tunables"
	cat <<EOF > $expected
net:
-     net type: ${NETTYPE}
      local NI(s):
        - interfaces:
                0: ${INTERFACES[0]}
          lnd tunables:
                conns_per_peer: 5
                tos: -1
EOF
	$IMPORT <  ${expected} || error "Import failed $?"
	$LNETCTL export --backup | grep -q "conns_per_peer: 5" || error "Bad parsing"
	reinit_dlc

	echo "lnd_tunables cannot be different"
	cat <<EOF > $expected
net:
-     net type: ${NETTYPE}
      local NI(s):
        - interfaces:
                0: ${INTERFACES[0]}
          lnd tunables:
                conns_per_peer: 5
                tos: -1
        - interfaces:
                0: ${FAKE_IF}
          lnd tunables:
                conns_per_peer: 3
                tos: -1
EOF
	$IMPORT <  ${expected} || error "Import failed $?"
	$LNETCTL export --backup | grep -q "conns_per_peer: 5" || error "Bad parsing"
	$LNETCTL export --backup | grep -q "conns_per_peer: 3" || error "Bad parsing"
	reinit_dlc

	echo "Wrong tunables (lnd_tunables) read from NI if first is empty"
	cat <<EOF > $expected
net:
-     net type: ${NETTYPE}
      local NI(s):
        - interfaces:
                0: ${INTERFACES[0]}
        - interfaces:
                0: ${FAKE_IF}
          tunables:
                peer_timeout: 181
                peer_credits: 8
                peer_buffer_credits: 0
                credits: 256
          lnd tunables:
                conns_per_peer: 3
                tos: -1
EOF
	$IMPORT <  ${expected} || error "Import failed $?"
	# conns_per_peer has default value for first ni and 3 for second
	$LNETCTL export --backup | grep "conns_per_peer:" | grep -qv 3 || error "Bad parsing"
	$LNETCTL export --backup | grep -q "conns_per_peer: 3" || error "Bad parsing"
	# Previous ni got set tunables to default value
	(( $($LNETCTL export --backup | grep -c "peer_timeout: 181") == 0 )) || \
		error "Bad parsing"
	check_tunable_warning || error "Missing warning"
	reinit_dlc

	echo "Tunables can be set directly in the net"
	cat <<EOF > $expected
net:
-     net type: ${NETTYPE}
      local NI(s):
        - interfaces:
                0: ${INTERFACES[0]}
        - interfaces:
                0: ${FAKE_IF}
      tunables:
            peer_timeout: 182
            peer_credits: 8
            peer_buffer_credits: 0
            credits: 256
      lnd tunables:
            conns_per_peer: 3
            tos: -1
EOF
	$IMPORT <  ${expected} || error "Import failed $?"
	(( $($LNETCTL export --backup | grep -c "peer_timeout: 182") == 2 )) ||
		error "Bad parsing"
	(( $($LNETCTL export --backup | grep -c "conns_per_peer: 3") == 2 )) ||
		error "Bad parsing"
	reinit_dlc

	echo "Show warning if we try to set different tunables (not lnd_tunables) for NIs of a same net"
	cat <<EOF > $expected
net:
-     net type: ${NETTYPE}
      local NI(s):
        - interfaces:
                0: ${INTERFACES[0]}
          tunables:
                peer_timeout: 182
                peer_credits: 8
                peer_buffer_credits: 0
                credits: 256
        - interfaces:
                0: ${FAKE_IF}
          tunables:
                peer_timeout: 183
                peer_credits: 8
                peer_buffer_credits: 0
                credits: 256
EOF
	$IMPORT <  ${expected} || error "Import failed $?"
	# Tunables for the two nis will be the same, a warning will be emitted
	(( $($LNETCTL export --backup | grep -c "peer_timeout: 182") == 2 )) ||
	check_tunable_warning || error "Missing warning"

	# Restore console_ratelimit
	echo $rl > $rlpath

	cleanup_lnet
}
run_test 170 "Check CPTs and tunables parsing when importing"

test_199() {
	[[ ${NETTYPE} == tcp* || ${NETTYPE} == o2ib* ]] ||
		skip "Need tcp or o2ib NETTYPE"

	reinit_dlc || return $?

	echo "Add interface ${INTERFACES[0]}"
	do_lnetctl net add --net ${NETTYPE} --if ${INTERFACES[0]} ||
		error "Failed to add net ${NETTYPE} with ${INTERFACES[0]}"

	local ipaddress=$(ip --oneline addr show dev ${INTERFACES[0]} |
			  awk '/inet /{print $4}' |
			  sed 's:/.*::')

	echo "Add IP address ${ipaddress} for interface ${INTERFACES[0]}"
	do_lnetctl net add --ip2net "${NETTYPE} ${ipaddress}" || return 0

	error "Failed to add net ${NETTYPE} with ${INTERFACES[0]}"
}
run_test 199 "load lnet w/o module option, configure interface twice"

test_200() {
	[[ ${NETTYPE} == tcp* ]] ||
		skip "Need tcp NETTYPE"
	cleanup_lnet || return $?
	setup_netns || error "setup_netns failed with $?"
	load_lnet "networks=\"\""
	ip a
	ss -ltunp
	do_ns ip a
	do_ns ss -ltunp
	lsmod | grep lnet
	do_ns $LNETCTL lnet configure $LNET_CONFIG_INIT_OPT ||
		error "Failed to configure LNet in non-default namespace rc = $?"
	$LNETCTL net show --net tcp | grep -q "nid: $FAKE_NID$" ||
		error "$FAKE_NID is not configured as expected"
	cleanup_netns
}
run_test 200 "load lnet w/o module option, configure in a non-default namespace"

test_201() {
	[[ ${NETTYPE} == tcp* ]] ||
		skip "Need tcp NETTYPE"
	cleanup_lnet || return $?
	setup_netns || error "setup_netns failed with $?"
	load_lnet "networks=tcp($FAKE_IF)"
	do_ns $LNETCTL lnet configure $LNET_CONFIG_INIT_OPT ||
		error "Failed to configure LNet in non-default namespace rc = $?"
	$LNETCTL net show --net tcp | grep -q "nid: $FAKE_NID$" ||
		error "$FAKE_NID is not configured as expected"
	cleanup_netns
}
run_test 201 "load lnet using networks module options in a non-default namespace"

test_202() {
	[[ ${NETTYPE} == tcp* ]] ||
		skip "Need tcp NETTYPE"
	(( $CLIENT_VERSION < $(version_code 2.17.53) )) ||
		skip "kernel ip2nets deprecated in 2.17.53"
	cleanup_lnet || return $?
	setup_netns || error "setup_netns failed with $?"
	load_lnet "networks=\"\" ip2nets=\"tcp0($FAKE_IF) ${FAKE_IP}\""
	do_ns $LNETCTL lnet configure --all ||
		error "Failed to configure LNet in non-default namespace rc = $?"
	$LNETCTL net show | grep -q "nid: ${FAKE_IP}@tcp$" ||
		error "$FAKE_IP@tcp is not configured as expected"
	cleanup_netns
}
run_test 202 "load lnet using ip2nets in a non-default namespace"

### Add the interfaces in the target namespace

test_203() {
	[[ ${NETTYPE} == tcp* ]] ||
		skip "Need tcp NETTYPE"
	cleanup_lnet || return $?
	setup_netns || error "setup_netns failed with $?"
	load_lnet
	do_lnetctl lnet configure $LNET_CONFIG_OPT ||
		error "Failed to configure LNet in non-default namespace rc = $?"
	do_ns $LNETCTL net add --net tcp0 --if $FAKE_IF ||
		error "Failed to add net in non-default namespace"
	do_ns $LNETCTL net show | grep -q "nid: $FAKE_NID$" ||
		error "$FAKE_NID is not configured as expected"
	cleanup_netns
}
run_test 203 "add a network using an interface in the non-default namespace"

LNET_PARAMS_FILE="$TMP/$TESTSUITE.parameters"
function save_lnet_params() {
	$LNETCTL global show | grep -E -v '^global:$' |
			       sed 's/://' > $LNET_PARAMS_FILE
}

function restore_lnet_params() {
	local param value
	while read param value; do
		[[ $param == max_intf ]] && continue
		[[ $param == lnd_timeout ]] && continue
		do_lnetctl set ${param} ${value} ||
			error "Failed to restore ${param} to ${value}"
	done < $LNET_PARAMS_FILE
}

function set_ltt_node() {
	# Achieves a desired LNet Transaction Timeout (LTT) value for a node by
	# setting the LND timeout (LNDT) value for the network being used for
	# tests.
	local node=$1
	local ltt=$2
	local nettype=$3

	if do_node $node $LNETCTL net set -h | grep -q -- "--lnd-timeout:"; then
		# lnetctl supports setting the LNDT parameter.
		local retry_count=$(do_node $node $LNETCTL global show |
				    awk '/retry_count/{print $NF}')

		# Determine LNDT value to achieve LTT. This is taken from the
		# the formula, using LNet retry count (LRC):
		# LTT = LNDT(LRC + 1) + 1
		# LNDT = (LTT - 1)/(LRC + 1)
		local lnd_timeout=$(( (ltt - 1) / (retry_count + 1) ))

		do_node $node $LNETCTL net set \
			--net ${nettype} --lnd-timeout $lnd_timeout ||
			error "Failed to set LND timeout on ${nettype} net"
	fi
	# Also set the default global LTT
	do_node $node $LNETCTL set transaction_timeout $ltt ||
		error "Failed to set transaction_timeout on $node"
}

function lnet_health_pre() {
	save_lnet_params

	# Lower transaction timeout to speed up test execution
	set_ltt_node $HOSTNAME 10 $NETTYPE ||
		error "Failed to set transaction timeout $?"

	RETRY_PARAM=$($LNETCTL global show | awk '/retry_count/{print $NF}')
	RSND_PRE=$($LNETCTL stats show | awk '/resend_count/{print $NF}')
	FAILED_RSND_PRE=$($LNETCTL stats show |
			  awk '/failed_resends/{print $NF}')
	SUCCESS_RSND_PRE=$($LNETCTL stats show |
			   awk '/successful_resends/{print $NF}')
	LO_HVAL_PRE=$($LNETCTL net show -v 2 | awk '/health value/{print $NF}' |
		      xargs echo | sed 's/ /+/g' | bc -l)

	RMT_HVAL_PRE=$($LNETCTL peer show --nid ${RNIDS[0]} -v 2 2>/dev/null |
		       awk '/health value/{print $NF}' | xargs echo |
		       sed 's/ /+/g' | bc -l)

	# Might not have any peers so initialize to zero.
	RMT_HVAL_PRE=${RMT_HVAL_PRE:-0}

	return 0
}

function lnet_health_post() {
	RSND_POST=$($LNETCTL stats show | awk '/resend_count/{print $NF}')
	FAILED_RSND_POST=$($LNETCTL stats show |
			   awk '/failed_resends/{print $NF}')
	SUCCESS_RSND_POST=$($LNETCTL stats show |
			    awk '/successful_resends/{print $NF}')
	LO_HVAL_POST=$($LNETCTL net show -v 2 |
		       awk '/health value/{print $NF}' |
		       xargs echo | sed 's/ /+/g' | bc -l)

	RMT_HVAL_POST=$($LNETCTL peer show --nid ${RNIDS[0]} -v 2 2>/dev/null |
			awk '/health value/{print $NF}' | xargs echo |
			sed 's/ /+/g' | bc -l)

	# Might not have any peers so initialize to zero.
	RMT_HVAL_POST=${RMT_HVAL_POST:-0}

	${VERBOSE} &&
	echo "Pre resends: $RSND_PRE" &&
	echo "Post resends: $RSND_POST" &&
	echo "Resends delta: $((RSND_POST - RSND_PRE))" &&
	echo "Pre failed resends: $FAILED_RSND_PRE" &&
	echo "Post failed resends: $FAILED_RSND_POST" &&
	echo "Pre successful resends: $SUCCESS_RSND_PRE" &&
	echo "Post successful resends: $SUCCESS_RSND_POST" &&
	echo "Pre local health: $LO_HVAL_PRE" &&
	echo "Post local health: $LO_HVAL_POST" &&
	echo "Pre remote health: $RMT_HVAL_PRE" &&
	echo "Post remote health: $RMT_HVAL_POST"

	restore_lnet_params

	do_lnetctl peer set --health 1000 --all ||
		error "Failed to set peer health rc $?"
	do_lnetctl net set --health 1000 --all ||
		error "Failed to set local NI health rc $?"

	return 0
}

function check_no_resends() {
	echo "Check that no resends took place"
	(( RSND_POST == RSND_PRE )) ||
		error "Found resends: $RSND_POST != $RSND_PRE"
	(( FAILED_RSND_POST == FAILED_RSND_PRE )) ||
		error "Found resends: $FAILED_RSND_POST != $FAILED_RSND_PRE"
	(( SUCCESS_RSND_POST == SUCCESS_RSND_PRE )) ||
		error "Found resends: $SUCCESS_RSND_POST != $SUCCESS_RSND_PRE"

	return 0
}

function check_resends() {
	local delta=$((RSND_POST - RSND_PRE))

	echo "Check that $RETRY_PARAM resends took place"
	(( delta == RETRY_PARAM )) ||
		error "Expected $RETRY_PARAM resends found $delta"

	echo "Check for 1 failed resend"
	delta=$((FAILED_RSND_POST - FAILED_RSND_PRE))
	(( delta == 1 )) || error "Found $delta failed resends"

	echo "Check for 0 successful resends"
	delta=$((SUCCESS_RSND_POST - SUCCESS_RSND_PRE))
	(( delta == 0 )) || error "Found $delta successful resends"

	return 0
}

function check_successful_resends() {
	local delta=$((SUCCESS_RSND_POST - SUCCESS_RSND_PRE))

	echo "Check for 1 successful resend"
	(( delta == 1 )) || error "Found $delta successful resends"
}

function check_no_local_health() {
	echo "Check that local NI health is unchanged"
	[[ $LO_HVAL_POST -ne $LO_HVAL_PRE ]] &&
		error "Local health changed: $LO_HVAL_POST != $LO_HVAL_PRE"

	return 0
}

function check_local_health() {
	echo "Check that local NI health has been changed"
	[[ $LO_HVAL_POST -eq $LO_HVAL_PRE ]] &&
		error "Local health unchanged: $LO_HVAL_POST == $LO_HVAL_PRE"

	return 0
}

function check_no_remote_health() {
	echo "Check that remote NI health is unchanged"
	[[ $RMT_HVAL_POST -ne $RMT_HVAL_PRE ]] &&
		error "Remote health changed: $RMT_HVAL_POST != $RMT_HVAL_PRE"

	return 0
}

function check_remote_health() {
	echo "Check that remote NI health has been changed"
	[[ $RMT_HVAL_POST -eq $RMT_HVAL_PRE ]] &&
		error "Remote health unchanged: $RMT_HVAL_POST == $RMT_HVAL_PRE"

	return 0
}

RNODE=""
RLOADED=false
NET_DEL_ARGS=""
RNIDS=( )
LNIDS=( )
setup_health_test() {
	local need_mr=$1

	[[ ${NETTYPE} == kfi* ]] && skip "kfi doesn't support drop rules"

	local rnodes=$(remote_nodes_list)
	[[ -z $rnodes ]] && skip "Need at least 1 remote node"

	cleanup_lnet || error "Failed to cleanup before test execution"

	# Loading modules should configure LNet with the appropriate
	# test-framework configuration
	load_lnet "config_on_load=1" || error "Failed to load modules"

	LNIDS=( $($LCTL list_nids | xargs echo) )

	RNODE=(${rnodes//,/ })
	RNIDS=( $(do_node $RNODE $LCTL list_nids | xargs echo) )

	if [[ -z ${RNIDS[@]} ]]; then
		do_rpc_nodes $RNODE load_lnet ||
			error "load_lnet failed on $RNODE rc = $?"
		do_node $RNODE $LNETCTL lnet configure $LNET_CONFIG_INIT_OPT ||
			error "Failed to configure lnet on $RNODE rc = $?"
		RLOADED=true
		RNIDS=( $(do_node $RNODE $LCTL list_nids | xargs echo) )
	fi

	[[ ${#LNIDS[@]} -lt 1 ]] &&
		error "No NIDs configured for local host $HOSTNAME"
	[[ ${#RNIDS[@]} -lt 1 ]] &&
		error "No NIDs configured for remote host $RNODE"

	# Ensure all peer NIs are local (i.e. non-routed config)
	local rnid rnet lnid lnet

	for rnid in ${RNIDS[@]}; do
		rnet=${rnid##*@}
		for lnid in ${LNIDS[@]}; do
			lnet=${lnid##*@}
			[[ ${lnet} == ${rnet} ]] &&
				break
		done
		[[ ${lnet} != ${rnet} ]] &&
			skip "Need non-routed configuration"
	done

	do_lnetctl discover ${RNIDS[0]} ||
		error "Unable to discover ${RNIDS[0]}"

	local mr=$($LNETCTL peer show --nid ${RNIDS[0]} |
		   awk '/Multi-Rail/{print $NF}')

	if ${need_mr} && [[ $mr == False ]]; then
		cleanup_health_test || return $?
		skip "Need MR peer"
	fi

	if ( ! ${need_mr} && [[ ${#RNIDS[@]} -gt 1 ]] ) ||
	   ( ! ${need_mr} && [[ ${#LNIDS[@]} -gt 1 ]] ); then
		cleanup_health_test || return $?
		skip "Need SR peer"
	fi

	if ${need_mr} && [[ ${#RNIDS[@]} -lt 2 ]]; then
		# Add a second, reachable NID to rnode.
		local net=${RNIDS[0]}

		net="${net//*@/}1"

		local if=$(do_rpc_nodes --quiet $RNODE lnet_if_list)
		[[ -z $if ]] &&
			error "Failed to determine interface for $RNODE"

		do_node $RNODE "$LNETCTL lnet configure $LNET_CONFIG_OPT" ||
			error "Failed to configure LNet on $RNODE rc = $?"
		do_rpc_nodes $RNODE "$LNETCTL net add --net $net --if $if" ||
			error "Failed to add interface to $RNODE rc = $?"
		RNIDS[1]="${RNIDS[0]}1"
		NET_DEL_ARGS="--net $net --if $if"
	fi

	if ${need_mr} && [[ ${#LNIDS[@]} -lt 2 ]]; then
		local net=${LNIDS[0]}
		net="${net//*@/}1"

		do_lnetctl lnet configure $LNET_CONFIG_OPT ||
			error "Failed to configure LNet rc = $?"
		do_lnetctl net add --net $net --if ${INTERFACES[0]} ||
			error "Failed to add interface rc = $?"
		LNIDS[1]="${LNIDS[0]}1"
	fi

	$LNETCTL net show

	$LNETCTL peer show -v 2 | grep -E -e nid -e health

	$LCTL set_param debug=+net

	return 0

}

cleanup_health_test() {
	local rc=0

	if [[ -n $NET_DEL_ARGS ]]; then
		do_rpc_nodes $RNODE \
			"$LNETCTL net del $NET_DEL_ARGS" ||
			rc=$((rc + $?))
		NET_DEL_ARGS=""
	fi

	unload_modules || rc=$((rc + $?))

	if $RLOADED; then
		do_rpc_nodes $RNODE unload_modules_local ||
			rc=$((rc + $?))
		RLOADED=false
	fi

	((rc == 0)) || error "Failed cleanup"

	return $rc
}

add_drop_rule() {
	if ((${RANDOM} % 2 == 0)); then
		do_lctl net_drop add $@ ||
			error "$LCTL net_drop add $@ failed rc = $?"
	else
		do_lnetctl fault drop add $@ ||
			error "$LNETCTL fault drop add $@ failed rc = $?"
	fi
}

del_drop_rule() {
	if ((${RANDOM} % 2 == 0)); then
		do_lctl net_drop del $@ ||
			error "$LCTL net_drop del $@ failed rc = $?"
	else
		do_lnetctl fault drop del $@ ||
			error "$LNETCTL fault drop del $@ failed rc = $?"
	fi
}

add_delay_rule() {
	if ((${RANDOM} % 2 == 0)); then
		do_lctl $net_delay_add $@ ||
			error "$LCTL net_delay add $@ failed rc = $?"
	else
		do_lnetctl fault delay add $@ ||
			error "$LNETCTL fault delay add $@ failed rc = $?"
	fi
}

del_delay_rule() {
	if ((${RANDOM} % 2 == 0)); then
		do_lctl $net_delay_del $@ ||
			error "$LCTL net_delay del $@ failed rc = $?"
	else
		do_lnetctl fault delay del $@ ||
			error "$LNETCTL fault delay del $@ failed rc = $?"
	fi
}

add_health_test_drop_rules() {
	local hstatus="-e $1"
	local rate="-r ${2:-1}"
	local args="-m GET $hstatus $rate"
	local src dst

	for src in "${LNIDS[@]}"; do
		for dst in "${RNIDS[@]}" "${LNIDS[@]}"; do
			add_drop_rule -s $src -d $dst ${args}
		done
	done
}

do_lnet_health_ping_test() {
	local hstatus="$1"

	echo "Simulate $hstatus"

	lnet_health_pre || return $?

	add_health_test_drop_rules ${hstatus}
	do_lnetctl ping ${RNIDS[0]} &&
		error "Should have failed"

	lnet_health_post

	del_drop_rule -a

	return 0
}

# See lnet/lnet/lib-msg.c:lnet_health_check()
LNET_LOCAL_RESEND_STATUSES="local_interrupt local_dropped local_aborted"
LNET_LOCAL_RESEND_STATUSES+=" local_no_route local_timeout"
LNET_LOCAL_NO_RESEND_STATUSES="local_error"
test_204() {
	setup_health_test false || return $?

	local hstatus
	for hstatus in ${LNET_LOCAL_RESEND_STATUSES} \
		       ${LNET_LOCAL_NO_RESEND_STATUSES}; do
		do_lnet_health_ping_test "${hstatus}" || return $?
		check_no_resends || return $?
		check_local_health || return $?
	done

	cleanup_health_test || return $?

	return 0
}
run_test 204 "Check no health or resends for single-rail local failures"

test_205() {
	(( $MDS1_VERSION >= $(version_code 2.14.58) )) ||
		skip "need at least 2.14.58"

	setup_health_test true || return $?

	local hstatus
	for hstatus in ${LNET_LOCAL_RESEND_STATUSES}; do
		do_lnet_health_ping_test "${hstatus}" || return $?
		check_resends || return $?
		check_local_health || return $?
	done

	for hstatus in ${LNET_LOCAL_NO_RESEND_STATUSES}; do
		do_lnet_health_ping_test "${hstatus}" || return $?
		check_no_resends || return $?
		check_local_health || return $?
	done

	cleanup_health_test || return $?

	return 0
}
run_test 205 "Check health and resends for multi-rail local failures"

# See lnet/lnet/lib-msg.c:lnet_health_check()
LNET_REMOTE_RESEND_STATUSES="remote_dropped"
LNET_REMOTE_NO_RESEND_STATUSES="remote_error remote_timeout"
test_206() {
	setup_health_test false || return $?

	local hstatus
	for hstatus in ${LNET_REMOTE_RESEND_STATUSES} \
		       ${LNET_REMOTE_NO_RESEND_STATUSES}; do
		do_lnet_health_ping_test "${hstatus}" || return $?
		check_no_resends || return $?
		check_no_local_health || return $?
		check_no_remote_health || return $?
	done

	cleanup_health_test || return $?

	return 0
}
run_test 206 "Check no health or resends for single-rail remote failures"

test_207() {
	(( $MDS1_VERSION >= $(version_code 2.14.58) )) ||
		skip "need at least 2.14.58"

	setup_health_test true || return $?

	local hstatus
	for hstatus in ${LNET_REMOTE_RESEND_STATUSES}; do
		do_lnet_health_ping_test "${hstatus}" || return $?
		check_resends || return $?
		check_no_local_health || return $?
		check_remote_health || return $?
		do_lnetctl peer set --health 1000 --all ||
			error "Unable to reset health rc=$?"
	done
	for hstatus in ${LNET_REMOTE_NO_RESEND_STATUSES}; do
		do_lnet_health_ping_test "${hstatus}" || return $?
		check_no_resends || return $?
		check_no_local_health || return $?
		check_remote_health || return $?
		do_lnetctl peer set --health 1000 --all ||
			error "Unable to reset health rc=$?"
	done

	cleanup_health_test || return $?

	return 0
}
run_test 207 "Check health and resends for multi-rail remote errors"

test_208_load_and_check_lnet() {
	local ip2nets="$1"
	local p_nid="$2"
	local s_nid="$3"
	local num_expected=1

	load_lnet "networks=\"\" ip2nets=\"${ip2nets_str}\""

	$LCTL net up $LNET_CONFIG_OPT ||
		error "Failed to load LNet with ip2nets \"${ip2nets_str}\""

	[[ -n $s_nid ]] &&
		num_expected=2

	declare -a nids
	nids=( $($LCTL list_nids) )

	[[ ${#nids[@]} -ne ${num_expected} ]] &&
		error "Expect ${num_expected} NIDs found ${#nids[@]}"

	[[ ${nids[0]} == ${p_nid} ]] ||
		error "Expect NID \"${p_nid}\" found \"${nids[0]}\""

	[[ -n $s_nid ]] && [[ ${nids[1]} != ${s_nid} ]] &&
		error "Expect second NID \"${s_nid}\" found \"${nids[1]}\""

	$LCTL net down &>/dev/null
	cleanup_lnet
}

test_208() {
	[[ ${NETTYPE} == tcp* ]] || skip "Need tcp NETTYPE"
	(( $CLIENT_VERSION < $(version_code 2.17.53) )) ||
		skip "kernel ip2nets deprecated in 2.17.53"

	cleanup_lnet || error "Failed to unload modules before test execution"
	setup_fakeif || error "Failed to add fake IF"

	local if0_ip=$(ip --oneline addr show dev ${INTERFACES[0]} |
		       awk '/inet /{print $4}' |
		       sed 's:/.*::')
	if0_ip=($(echo "${if0_ip[@]}" | tr ' ' '\n' | uniq | tr '\n' ' '))
	local ip2nets_str="tcp(${INTERFACES[0]}) $if0_ip"

	echo "Configure single NID \"$ip2nets_str\""
	test_208_load_and_check_lnet "${ip2nets_str}" "${if0_ip}@tcp"

	ip2nets_str="tcp(${INTERFACES[0]}) $if0_ip; tcp1($FAKE_IF) $FAKE_IP"
	echo "Configure two NIDs; two NETs \"$ip2nets_str\""
	test_208_load_and_check_lnet "${ip2nets_str}" "${if0_ip}@tcp" \
				     "${FAKE_IP}@tcp1"

	ip2nets_str="tcp(${INTERFACES[0]}) $if0_ip; tcp($FAKE_IF) $FAKE_IP"
	echo "Configure two NIDs; one NET \"$ip2nets_str\""
	test_208_load_and_check_lnet "${ip2nets_str}" "${if0_ip}@tcp" \
				     "${FAKE_IP}@tcp"
	local addr1=( ${if0_ip//./ } )
	local addr2=( ${FAKE_IP//./ } )
	local range="[${addr1[0]},${addr2[0]}]"

	local i
	for i in $(seq 1 3); do
		range+=".[${addr1[$i]},${addr2[$i]}]"
	done
	ip2nets_str="tcp(${INTERFACES[0]},${FAKE_IF}) ${range}"

	echo "Configured two NIDs; one NET alt syntax \"$ip2nets_str\""
	test_208_load_and_check_lnet "${ip2nets_str}" "${if0_ip}@tcp" \
				     "${FAKE_IP}@tcp"

	cleanup_fakeif

	echo "alt syntax with missing IF \"$ip2nets_str\""
	load_lnet "networks=\"\" ip2nets=\"${ip2nets_str}\""

	echo "$LCTL net up $LNET_CONFIG_OPT should fail"
	$LCTL net up $LNET_CONFIG_OPT &&
		error "LNet bring up should have failed"

	cleanup_lnet
}
run_test 208 "Test various kernel ip2nets configurations"

test_209() {
	(( $MDS1_VERSION >= $(version_code 2.14.58) )) ||
		skip "need at least 2.14.58"

	setup_health_test false || return $?

	echo "Simulate network_timeout w/SR config"
	lnet_health_pre

	add_health_test_drop_rules network_timeout

	do_lnetctl discover ${RNIDS[0]} &&
		error "Should have failed"

	lnet_health_post

	check_no_resends || return $?
	check_local_health || return $?
	check_no_remote_health || return $?

	cleanup_health_test || return $?

	setup_health_test true || return $?

	echo "Simulate network_timeout w/MR config"

	lnet_health_pre

	add_health_test_drop_rules network_timeout

	do_lnetctl discover ${RNIDS[0]} &&
		error "Should have failed"

	lnet_health_post

	check_no_resends || return $?
	check_local_health || return $?
	check_remote_health || return $?

	cleanup_health_test || return $?

	return 0
}
run_test 209 "Check health, but not resends, for network timeout"

check_nid_in_recovq() {
	local queue="$1"
	local nid="$2"
	local expect="$3"
	local max_wait="${4:-10}"
	local rc=0

	(($expect == 0)) &&
		echo "$queue recovery queue should be empty" ||
		echo "$queue recovery queue should have $nid"

	wait_update $HOSTNAME \
		"$LNETCTL debug recovery $queue | \
		 grep -wc \"nid-0: $nid\"" \
		"$expect" "$max_wait"
	rc=$?
	do_lnetctl debug recovery $queue
	(($rc == 0)) ||
		error "Expect $expect NIDs in recovery."

	return 0
}

# First ping is sent at time 0.
# 2nd at 0 + 2^1 = 2
# 3rd at 2 + 2^2 = 6
# 4th at 6 + 2^3 = 14
# 5th at 14 + 2^4 = 30
# e.g. after 10 seconds we would expect 3 pings to have been sent, and the
# NI will have been enqueued for the 4th ping.
# 
# If the recovery limit is 10 seconds, then after the 4th ping is sent
# we expect the peer NI to have aged out, so it will not actually be
# queued for a 5th ping.
# If max_recovery_ping_interval is set to 4 then:
#  First ping is sent at time 0
#  2nd at  0 + min(2^1, 4) = 2
#  3rd at  2 + min(2^2, 4) = 6
#  4th at  6 + min(2^3, 4) = 10
#  5th at 10 + min(2^4, 4) = 14
#  6th at 14 + min(2^5, 4) = 18
#  7th at 18 + min(2^6, 4) = 22
# e.g. after 4 seconds we would expect 2 pings to have been sent, and
# after 13 seconds we would expect 4 pings to have been sent
check_ping_count() {
	local queue="$1"
	local nid="$2"
	local expect="$3"
	local max_wait="$4"

	echo "Check ping counts:"

	local rc=0
	if [[ $queue == "ni" ]]; then
		wait_update $HOSTNAME \
			"$LNETCTL net show -v 2 | \
			 grep -e $nid -e ping_count | grep -wA1 $nid | \
			 awk '/ping_count/{print \\\$NF}'" "$expect" "$max_wait"
		rc=$?
		$LNETCTL net show -v 2 | grep -e $nid -e ping_count
	elif [[ $queue == peer_ni ]]; then
		wait_update $HOSTNAME \
			"$LNETCTL peer show -v 2 --nid $nid | \
			 grep -v primary | \
			 grep -e $nid -e ping_count | grep -wA1 $nid | \
			 awk '/ping_count/{print \\\$NF}'" "$expect" "$max_wait"
		rc=$?
		$LNETCTL peer show -v 2 --nid $nid | grep -v primary |
			grep -e $nid -e ping_count
	else
		error "Unrecognized queue \"$queue\""
		return 1
	fi

	((rc == 0)) || error "Unexpected ping count"

	return 0
}

test_210() {
	[[ ${NETTYPE} == kfi* ]] && skip "kfi doesn't support drop rules"

	reinit_dlc || return $?
	add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?
	add_net "${NETTYPE}1" "${INTERFACES[0]}" || return $?

	local prim_nid=$($LCTL list_nids | head -n 1)

	do_lnetctl discover $prim_nid ||
		error "failed to discover myself"

	local default=$($LNETCTL global show |
			awk '/recovery_limit/{print $NF}')
	# Set recovery limit to 10 seconds.
	do_lnetctl set recovery_limit 10 ||
		error "failed to set recovery_limit"

	$LCTL set_param debug=+net
	add_drop_rule -s *@${NETTYPE} -d *@${NETTYPE} -m GET -r 1 -e local_error
	add_drop_rule -s *@${NETTYPE}1 -d *@${NETTYPE}1 -m GET -r 1 \
		-e local_error
	add_drop_rule -s *@${NETTYPE} -d *@${NETTYPE} -r 1
	add_drop_rule -s *@${NETTYPE}1 -d *@${NETTYPE}1 -r 1
	do_lnetctl net set --health 0 --nid $prim_nid ||
		error "Failed to set NI health to 0 rc $?"

	check_ping_count "ni" "$prim_nid" "2" "10"
	check_nid_in_recovq "-l" "$prim_nid" "1"

	check_ping_count "ni" "$prim_nid" "3" "10"
	check_nid_in_recovq "-l" "$prim_nid" "1"

	del_drop_rule -a

	reinit_dlc || return $?
	add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?
	add_net "${NETTYPE}1" "${INTERFACES[0]}" || return $?

	local prim_nid=$($LCTL list_nids | head -n 1)

	do_lnetctl discover $prim_nid ||
		error "failed to discover myself"

	do_lnetctl set recovery_limit $default ||
		error "failed to set recovery_limit"

	default=$($LNETCTL global show |
		  awk '/max_recovery_ping_interval/{print $NF}')
	do_lnetctl set max_recovery_ping_interval 4 ||
		error "failed to set max_recovery_ping_interval"

	$LCTL set_param debug=+net
	add_drop_rule -s *@${NETTYPE} -d *@${NETTYPE} -m GET -r 1 -e local_error
	add_drop_rule -s *@${NETTYPE}1 -d *@${NETTYPE}1 -m GET -r 1 \
		-e local_error
	add_drop_rule -s *@${NETTYPE} -d *@${NETTYPE} -r 1
	add_drop_rule -s *@${NETTYPE}1 -d *@${NETTYPE}1 -r 1
	do_lnetctl net set --health 0 --nid $prim_nid ||
		error "Failed to set NI health to 0 rc $?"

	check_ping_count "ni" "$prim_nid" "2" "10"
	check_nid_in_recovq "-l" "$prim_nid" "1"

	check_ping_count "ni" "$prim_nid" "4" "10"
	check_nid_in_recovq "-l" "$prim_nid" "1"

	del_drop_rule -a

	do_lnetctl set max_recovery_ping_interval $default ||
		error "failed to set max_recovery_ping_interval"

	return 0
}
run_test 210 "Local NI recovery checks"

test_211() {
	[[ ${NETTYPE} == kfi* ]] && skip "kfi doesn't support drop rules"

	reinit_dlc || return $?
	add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?
	add_net "${NETTYPE}1" "${INTERFACES[0]}" || return $?

	local prim_nid=$($LCTL list_nids | head -n 1)

	do_lnetctl discover $prim_nid ||
		error "failed to discover myself"

	local default=$($LNETCTL global show |
			awk '/recovery_limit/{print $NF}')
	# Set recovery limit to 10 seconds.
	do_lnetctl set recovery_limit 10 ||
		error "failed to set recovery_limit"

	add_drop_rule -s *@${NETTYPE} -d *@${NETTYPE} -m GET -r 1 \
		-e remote_error
	add_drop_rule -s *@${NETTYPE}1 -d *@${NETTYPE}1 -m GET -r 1 \
		-e remote_error
	add_drop_rule -s *@${NETTYPE} -d *@${NETTYPE} -r 1
	add_drop_rule -s *@${NETTYPE}1 -d *@${NETTYPE}1 -r 1

	# Set health to 0 on one interface. This forces it onto the recovery
	# queue.
	do_lnetctl peer set --nid $prim_nid --health 0

	check_nid_in_recovq "-p" "$prim_nid" "1"

	# The peer should age out in 10-20 seconds
	check_nid_in_recovq "-p" "$prim_nid" "0" "20"
	# Ping count should reset to 0 when peer ages out
	check_ping_count "peer_ni" "$prim_nid" "0"

	del_drop_rule -a

	# Set health to force it back onto the recovery queue. Set to 500 means
	# in ~5 seconds it should be back at maximum value.
	# NB: we reset the recovery limit to 0 (indefinite) so the peer NI is
	# eligible again
	do_lnetctl set recovery_limit 0 ||
		error "failed to set recovery_limit"

	do_lnetctl peer set --nid $prim_nid --health 500

	check_nid_in_recovq "-p" "$prim_nid" "1"
	check_nid_in_recovq "-p" "$prim_nid" "0" "20"

	reinit_dlc || return $?
	add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?
	add_net "${NETTYPE}1" "${INTERFACES[0]}" || return $?

	local prim_nid=$($LCTL list_nids | head -n 1)

	do_lnetctl discover $prim_nid ||
		error "failed to discover myself"

	do_lnetctl set recovery_limit $default ||
		error "failed to set recovery_limit"

	default=$($LNETCTL global show |
		  awk '/max_recovery_ping_interval/{print $NF}')
	do_lnetctl set max_recovery_ping_interval 4 ||
		error "failed to set max_recovery_ping_interval"

	add_drop_rule -s *@${NETTYPE} -d *@${NETTYPE} -m GET -r 1 \
		-e remote_error
	add_drop_rule -s *@${NETTYPE}1 -d *@${NETTYPE}1 -m GET -r 1 \
		-e remote_error
	add_drop_rule -s *@${NETTYPE} -d *@${NETTYPE} -r 1
	add_drop_rule -s *@${NETTYPE}1 -d *@${NETTYPE}1 -r 1

	# Set health to 0 on one interface. This forces it onto the recovery
	# queue.
	do_lnetctl peer set --nid $prim_nid --health 0

	check_ping_count "peer_ni" "$prim_nid" "1" "4"
	check_nid_in_recovq "-p" "$prim_nid" "1"

	# After we detect the 1st ping above, the 4th ping should be sent after
	# ~13 seconds
	check_ping_count "peer_ni" "$prim_nid" "4" "14"
	check_nid_in_recovq "-p" "$prim_nid" "1"

	del_drop_rule -a

	do_lnetctl set max_recovery_ping_interval $default ||
		error "failed to set max_recovery_ping_interval"

	return 0
}
run_test 211 "Remote NI recovery checks"

test_212() {
	[[ ${NETTYPE} == kfi* ]] && skip "kfi doesn't support drop rules"

	local rnodes=$(remote_nodes_list)
	[[ -z $rnodes ]] && skip "Need at least 1 remote node"

	cleanup_lnet || error "Failed to cleanup before test execution"

	# Loading modules should configure LNet with the appropriate
	# test-framework configuration
	load_lnet "config_on_load=1" || error "Failed to load modules"

	local my_nid=$($LCTL list_nids | head -n 1)
	[[ -z $my_nid ]] &&
		error "Failed to get primary NID for local host $HOSTNAME"

	local rnode=(${rnodes//,/ })
	local rnodenids=$(do_node $rnode $LCTL list_nids | xargs echo)
	local rloaded=false

	if [[ -z $rnodenids ]]; then
		do_rpc_nodes $rnode load_lnet ||
			error "load_lnet failed on $rnode rc = $?"
		do_node $rnode $LNETCTL lnet configure $LNET_CONFIG_INIT_OPT ||
			error "Failed to configure LNet on $rnode rc = $?"
		rloaded=true
		rnodenids=$(do_node $rnode $LCTL list_nids | xargs echo)
	fi

	local rnodepnid=$(awk '{print $1}' <<< $rnodenids)

	[[ -z $rnodepnid ]] &&
		error "Failed to get primary NID for remote host $rnode"

	log "Initial discovery"
	do_lnetctl discover --force $rnodepnid ||
		error "Failed to discover $rnodepnid"

	do_node $rnode "$LNETCTL discover --force $my_nid" ||
		error "$rnode failed to discover $my_nid"

	log "Fail local discover ping to set LNET_PEER_REDISCOVER flag"
	add_drop_rule -s "*@$NETTYPE" -d "*@$NETTYPE" -r 1 -e local_error
	do_lnetctl discover --force $rnodepnid &&
		error "Discovery should have failed"
	del_drop_rule -a

	local nid
	for nid in $rnodenids; do
		# We need GET (PING) delay just long enough so we can trigger
		# discovery on the remote peer
		add_delay_rule -s "*@$NETTYPE" -d $nid -r 1 -m GET -l 3
		add_drop_rule -s "*@$NETTYPE" -d $nid -r 1 -m GET -e local_error
		# We need PUT (PUSH) delay just long enough so we can process
		# the PING failure
		add_delay_rule -s "*@$NETTYPE" -d $nid -r 1 -m PUT -l 6
	done

	log "Force $HOSTNAME to discover $rnodepnid (in background)"
	# We want to get a PING sent that we know will eventually fail.
	# The delay rules we added will ensure the ping is not sent until
	# the PUSH is also in flight (see below), and the drop rule ensures that
	# when the PING is eventually sent it will error out
	do_lnetctl discover --force $rnodepnid &
	local pid1=$!

	# We want a discovery PUSH from rnode to put rnode back on our
	# discovery queue. This should cause us to try and send a PUSH to rnode
	# while the PING is still outstanding.
	log "Force $rnode to discover $my_nid"
	do_node $rnode $LNETCTL discover --force $my_nid

	# At this point we'll have both PING_SENT and PUSH_SENT set for the
	# rnode peer. Wait for the PING to error out which should terminate the
	# discovery process that we backgrounded.
	log "Wait for $pid1"
	wait $pid1
	log "Finished wait on $pid1"

	# The PING send failure clears the PING_SENT flag and puts the peer back
	# on the discovery queue. When discovery thread processes the peer it
	# will mistakenly clear the PUSH_SENT flag (and set PUSH_FAILED).
	# Discovery will then complete for this peer even though we have an
	# outstanding PUSH.
	# When PUSH is actually unlinked it will be forced back onto the
	# discovery queue, but we no longer have a ref on the peer. When
	# discovery completes again, we'll trip the ASSERT in
	# lnet_destroy_peer_locked()

	# Delete the delay rules to send the PUSH
	del_delay_rule -a
	# Delete the drop rules
	del_drop_rule -a

	unload_modules ||
		error "Failed to unload modules"
	if $rloaded; then
		do_rpc_nodes $rnode unload_modules_local ||
			error "Failed to unload modules on $rnode"
	fi

	return 0
}
run_test 212 "Check discovery refcount loss bug (LU-14627)"

test_213() {
	[[ ${NETTYPE} == tcp* ]] || skip "Need tcp NETTYPE"

	cleanup_lnet || error "Failed to unload modules before test execution"

	setup_fakeif || error "Failed to add fake IF"

	reinit_dlc || return $?

	add_net "tcp" "${INTERFACES[0]}" || return $?
	add_net "tcp" "$FAKE_IF" || return $?

	local nid1=$(lctl list_nids | head -n 1)
	local nid2=$(lctl list_nids | tail --lines 1)

	[[ $(lctl which_nid $nid1 $nid2) == $nid1 ]] ||
		error "Expect nid1 \"$nid1\" to be preferred"

	[[ $(lctl which_nid $nid2 $nid1) == $nid2 ]] ||
		error "Expect nid2 \"$nid2\" to be preferred"

	return 0
}
run_test 213 "Check LNetDist calculation for multiple local NIDs"

function check_ni_status() {
	local nid="$1"
	local expect="$2"

	local status=$($LNETCTL net show |
		       grep -wA 1 ${nid} |
		       awk '/status/{print $NF}')

	echo "NI ${nid} expect status \"${expect}\" found \"${status}\""
	if [[ $status != $expect ]]; then
		error "Error: Expect NI status \"$expect\" for NID \"$nid\" but found \"$status\""
	fi

	return 0
}

test_214() {
	[[ ${NETTYPE} == tcp* ]] || skip "Need tcp NETTYPE"

	cleanup_lnet || error "Failed to unload modules before test execution"

	setup_fakeif || error "Failed to add fake IF"

	reinit_dlc || return $?

	add_net "tcp" "${INTERFACES[0]}" || return $?
	add_net "tcp" "$FAKE_IF" || return $?

	local nid1=$(lctl list_nids | head -n 1)
	local nid2=$(lctl list_nids | tail --lines 1)

	check_ni_status "0@lo" up
	check_ni_status "$nid1" up
	check_ni_status "$nid2" up

	do_lnetctl ping --source $nid2 $nid1 ||
		error "$LNETCTL ping --source $nid2 $nid1 failed"

	echo "Set $FAKE_IF down"
	echo "ip link set dev $FAKE_IF down"
	ip link set dev $FAKE_IF down
	check_ni_status "0@lo" up
	check_ni_status "$nid1" up
	check_ni_status "$nid2" down
}
run_test 214 "Check local NI status when link is downed"

get_ni_stat() {
	local nid=$1
	local stat=$2

	$LNETCTL net show -v 2 |
		grep -E -e nid -e $stat |
		grep -wA 1 $nid |
		awk '/'$stat':/{print $NF}'
}

ni_stats_pre() {
	local nidvar s
	for nidvar in nid1 nid2; do
		for stat in send_count recv_count; do
			s=$(get_ni_stat ${!nidvar} $stat)
			eval ${nidvar}_pre_${stat}=$s
		done
	done
}

ni_stats_post() {
	local nidvar s
	for nidvar in nid1 nid2; do
		for stat in send_count recv_count; do
			s=$(get_ni_stat ${!nidvar} $stat)
			eval ${nidvar}_post_${stat}=$s
		done
	done
}

ni_stat_changed() {
	local nidvar=$1
	local stat=$2

	local pre post
	eval pre=\${${nidvar}_pre_${stat}}
	eval post=\${${nidvar}_post_${stat}}

	echo "${!nidvar} pre ${stat} $pre post ${stat} $post"

	[[ $pre -ne $post ]]
}

test_215() {
	cleanup_lnet || error "Failed to unload modules before test execution"

	reinit_dlc || return $?

	add_net "${NETTYPE}1" "${INTERFACES[0]}" || return $?
	add_net "${NETTYPE}2" "${INTERFACES[0]}" || return $?

	local nid1=$($LCTL list_nids | head -n 1)
	local nid2=$($LCTL list_nids | tail --lines 1)

	do_lnetctl peer add --prim $nid1 --nid $nid2 ||
		error "Failed to add peer"

	local npings=25

	for nidvarA in nid1 nid2; do
		src=${!nidvarA}
		dst=${!nidvarA}
		for nidvarB in nid1 nid2; do
			[[ $nidvarA == $nidvarB ]] && continue

			ni_stats_pre

			echo "$LNETCTL ping $dst x $npings"
			for i in $(seq 1 $npings); do
				$LNETCTL ping $dst &>/dev/null ||
					error "$LNETCTL ping $dst failed"
			done

			ni_stats_post

			# No source specified, sends to either NID should cause
			# counts to increase across both NIs
			for nidvar in nid1 nid2; do
				for stat in send_count recv_count; do
					ni_stat_changed $nidvar $stat ||
						error "$stat unchanged for ${!nidvar}"
				done
			done

			ni_stats_pre

			echo "$LNETCTL ping --source $src $dst x $npings"
			for i in $(seq 1 $npings); do
				$LNETCTL ping --source $src $dst &>/dev/null ||
					error "$LNETCTL ping --source $src $dst failed"
			done

			ni_stats_post

			# src nid == dest nid means stats for the _other_ NI
			# should be unchanged
			for nidvar in nid1 nid2; do
				for stat in send_count recv_count; do
					if [[ ${!nidvar} == $src ]]; then
						ni_stat_changed $nidvar $stat ||
							error "$stat unchanged for ${!nidvar}"
					else
						ni_stat_changed $nidvar $stat &&
							error "$stat changed for ${!nidvar}"
					fi
				done
			done
		done
		# Double number of pings for next iteration because the net
		# sequence numbers will have diverged
		npings=$(($npings * 2))
	done

	# Ping from nid1 to nid2 should fail
	do_lnetctl ping --source $nid1 $nid2 &&
		error "ping from $nid1 to $nid2 should fail"

	# Ping from nid2 to nid1 should fail
	do_lnetctl ping --source $nid2 $nid1 &&
		error "ping from $nid2 to $nid1 should fail"

	return 0
}
run_test 215 "Test lnetctl ping --source option"

test_216() {
	[[ ${NETTYPE} == kfi* ]] && skip "kfi doesn't support drop rules"

	local rc=0

	reinit_dlc || return $?

	add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?
	add_net "${NETTYPE}1" "${INTERFACES[0]}" || return $?

	local nids=( $($LCTL list_nids | xargs echo) )

	do_lnetctl discover ${nids[0]} ||
		error "Initial discovery failed"

	do_lnetctl ping --source ${nids[0]} ${nids[0]} ||
		error "Initial ping failed $?"

	do_lnetctl ping --source ${nids[1]} ${nids[1]} ||
		error "Initial ping failed $?"

	local src dst
	for src in "${nids[@]}"; do
		for dst in "${nids[@]}"; do
			add_drop_rule -r 1 -s $src -d $dst -e network_timeout
		done
	done

	do_lnetctl ping ${nids[0]} || rc=$?

	del_drop_rule -a

	[[ $rc -eq 0 ]] &&
		error "expected ping to fail"

	check_nid_in_recovq "-p" "${nids[0]}" "0"
	check_nid_in_recovq "-l" "${nids[0]}" "1"

	return 0
}
run_test 216 "Failed send to peer NI owned by local host should not trigger peer NI recovery"

test_217() {
	reinit_dlc || return $?

	[[ $($LNETCTL net show | grep -c nid) -ne 1 ]] &&
		error "Unexpected number of NIs after initalizing DLC"

	do_lnetctl discover 0@lo ||
		error "Failed to discover 0@lo"

	unload_modules
}
run_test 217 "Don't leak memory when discovering peer with nnis <= 1"

test_218() {
	[[ ${NETTYPE} == kfi* ]] && skip "kfi doesn't support drop rules"

	reinit_dlc || return $?

	[[ ${#INTERFACES[@]} -lt 2 ]] &&
		skip "Need two LNet interfaces"

	add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?

	local nid1=$($LCTL list_nids | head -n 1)

	do_lnetctl ping $nid1 ||
		error "ping failed"

	add_net "${NETTYPE}" "${INTERFACES[1]}" || return $?

	local nid2=$($LCTL list_nids | tail --lines 1)

	do_lnetctl ping $nid2 ||
		error "ping failed"

	add_drop_rule -s $nid1 -d $nid1 -e local_error -r 1

	do_lnetctl ping --source $nid1 $nid1 &&
		error "ping should have failed"

	local health_recovered
	local i

	for i in $(seq 1 5); do
		health_recovered=$($LNETCTL net show -v 2 |
				   grep -c 'health value: 1000')

		if [[ $health_recovered -ne 2 ]]; then
			echo "Wait 1 second for health to recover"
			sleep 1
		else
			break
		fi
	done

	health_recovered=$($LNETCTL net show -v 2 |
			   grep -c 'health value: 1000')

	del_drop_rule -a

	[[ $health_recovered -ne 2 ]] &&
		do_lnetctl net show -v 2 | grep -E -e nid -e health &&
		error "Health hasn't recovered"

	return 0
}
run_test 218 "Local recovery pings should exercise all available paths"

test_219() {
	reinit_dlc || return $?
	add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?
	add_net "${NETTYPE}1" "${INTERFACES[0]}" || return $?

	local nid1=$(lctl list_nids | head -n 1)
	local nid2=$(lctl list_nids | tail --lines 1)

	do_lnetctl ping $nid1 ||
		error "Ping failed $?"
	do_lnetctl ping $nid2 ||
		error "Ping failed $?"

	do_lnetctl discover $nid2 ||
		error "Discovery failed"

	$LNETCTL peer show --nid $nid1 | grep -q $nid2 ||
		error "$nid2 is not listed under $nid1"
}
run_test 219 "Consolidate peer entries"

# check that all routes have the 'expect' status
# lctl, lnetctl and debugfs can all report route status, so we check all three
# to ensure they are in agreement.
check_route_aliveness() {
	local node="$1"
	local expect="$2"
	local noerror="${3:-false}"

	local lctl_status
	local lnetctl_status
	local debugfs_status
	local chk_intvl
	local timeout

	chk_intvl=$(cat /sys/module/lnet/parameters/alive_router_check_interval)
	timeout=$(cat /sys/module/lnet/parameters/router_ping_timeout)

	# Router may delay start for chk_intvl + timeout, so wait for 2x this
	local lctl_cmd="do_node $node $LCTL show_route"
	local lnetctl_cmd="do_node $node $LNETCTL route show -v"
	local debugfs_cmd="do_node $node $LCTL get_param -n routes"
	local max_wait=$((2 * (chk_intvl + timeout)))
	local waited=0
	local begin=$SECONDS
	local got_expected=false

	echo "waiting up to $max_wait seconds for route status agreement"
	while ((waited <= max_wait)); do
		lctl_status=$($lctl_cmd | awk '{print $7}' | sort -u | xargs)
		lnetctl_status=$($lnetctl_cmd | awk '/state/{print $NF}' |
				 sort -u | xargs)
		debugfs_status=$($debugfs_cmd | awk '/'${NETTYPE}'/{print $4}' |
				 sort -u | xargs)


		if [[ $lctl_status == $expect ]] &&
		   [[ $lnetctl_status == $expect ]] &&
		   [[ $debugfs_status == $expect ]]; then
		   	got_expected=true
			break
		fi
		if ${VERBOSE}; then
			echo "Wait $((max_wait - waited))s for route '$expect'"
			echo "    lctl:'$lctl_status' lnetctl:'$lnetctl_status'"
			echo "    debugfs:'$debugfs_status'"
		fi

		sleep 1
		waited=$((SECONDS - begin))
	done

	if ! $got_expected; then
		echo "lctl shows:"
		$lctl_cmd
		echo "lnetctl shows:"
		$lnetctl_cmd
		echo "debugfs shows:"
		$debugfs_cmd

		if ${noerror}; then
			return 1
		fi

		[[ $lctl_status == $expect ]] ||
			error "Wanted '$expect' lctl found '$lctl_status'"

		[[ $lnetctl_status == $expect ]] ||
			error "Wanted '$expect' lnetctl found '$lnetctl_status'"

		[[ $debugfs_status == $expect ]] ||
			error "Wanted '$expect' debugfs found '$debugfs_status'"
	fi

	echo "Got '$expect' after ${waited}s"

	return 0
}

check_router_ni_status() {
	local router="$1"
	local expected_local="$2"
	local expected_remote="$3"

	local actual_local
	local actual_remote
	local chk_intvl
	local timeout
	local i

	chk_intvl=$(cat /sys/module/lnet/parameters/alive_router_check_interval)
	timeout=$(cat /sys/module/lnet/parameters/router_ping_timeout)

	actual_local=$(do_node $router "$LNETCTL net show --net $LOCAL_NET" |
		       awk '/status/{print $NF}')
	actual_remote=$(do_node $router "$LNETCTL net show --net $REMOTE_NET" |
			awk '/status/{print $NF}')

	for ((i = 0; i < $((chk_intvl + timeout)); i++)); do
		if [[ $actual_local == $expected_local ]] &&
		   [[ $actual_remote == $expected_remote ]]; then
			break
		fi

		echo "wait 1s for NI state change"
		sleep 1

		actual_local=$(do_node $router \
			       "$LNETCTL net show --net $LOCAL_NET" |
				awk '/status/{print $NF}')
		actual_remote=$(do_node $router \
				"$LNETCTL net show --net $REMOTE_NET" |
				awk '/status/{print $NF}')
	done

	[[ $actual_local == $expected_local ]] ||
		error "$LOCAL_NET should be $expected_local"

	[[ $actual_remote == $expected_remote ]] ||
		error "$REMOTE_NET should be $expected_remote"

	return 0
}

config_routes() {
	for router in ${!ROUTER_INTERFACES[@]}; do
		do_node $router "$LNETCTL set routing 1" ||
			error "Unable to enable routing on $router"
	done

	for router in ${!ROUTER_NIDS[@]}; do
		local router_nids=( ${ROUTER_NIDS[$router]} )

		do_route_add $HOSTNAME $REMOTE_NET ${router_nids[0]} ||
			return $?
	done

	for router in ${!ROUTER_INTERFACES[@]}; do
		local router_nids=( ${ROUTER_NIDS[$router]} )

		for rpeer in ${!RPEER_INTERFACES[@]}; do
			do_route_add $rpeer $LOCAL_NET ${router_nids[1]} ||
				return $?
		done
	done

	check_route_aliveness "$HOSTNAME" "up" ||
		return $?

	for rpeer in ${RPEERS[@]}; do
		check_route_aliveness "$rpeer" "up" ||
			return $?
	done
}

do_basic_rtr_test() {
	config_routes || return $?

	for rpeer in ${!RPEER_NIDS[@]}; do
		local rpeer_nids=( ${RPEER_NIDS[$rpeer]} )

		do_lnetctl ping ${rpeer_nids[0]} ||
			error "Failed to ping ${rpeer_nids[0]}"
	done

	for rpeer in ${RPEERS[@]}; do
		do_node $rpeer "$LNETCTL ping ${LNIDS[0]}" ||
			error "$rpeer failed to ping ${LNIDS[0]}"
	done

	return 0
}

test_220() {
	setup_router_test || return $?

	do_basic_rtr_test || return $?

	do_rpc_nodes $HOSTNAME,${RPEERS[0]} load_module \
		../lnet/selftest/lnet_selftest ||
			error "Failed to load lnet-selftest module"

	$LSTSH -H -t $HOSTNAME -f ${RPEERS[0]} -m rw -s 4k ||
		error "lst failed"

	$LSTSH -H -t $HOSTNAME -f ${RPEERS[0]} -m rw ||
		error "lst failed"

	cleanup_router_test || return $?
}
run_test 220 "Add routes w/default options - check aliveness"

test_221() {
	setup_router_test lnet_peer_discovery_disabled=1 || return $?

	do_basic_rtr_test || return $?

	cleanup_router_test || return $?
}
run_test 221 "Add routes w/DD disabled - check aliveness"

# assumes 1 router, 1 peer
do_aarf_enabled_test() {

	local router=${ROUTERS[0]}
	local router_nids=( ${ROUTER_NIDS[$router]} )
	local rpeer=${RPEERS[0]}
	local rpeer_nids=( ${RPEER_NIDS[$rpeer]} )

	do_node $router "$LNETCTL set routing 1" ||
		error "Unable to enable routing on $ROUTER"

	check_router_ni_status $router "down" "down"

	do_lnetctl ping ${rpeer_nids[0]} &&
		error "Ping should fail"

	do_node $rpeer "$LNETCTL ping ${LNIDS[0]}" &&
		error "$rpeer ping should fail"

	# Adding a route should cause the router's NI on LOCAL_NET to get up
	do_route_add $HOSTNAME $REMOTE_NET ${router_nids[0]} ||
		return $?

	check_router_ni_status $router "up" "down" ||
		return $?

	# But route should still be down because of avoid_asym_router_failure
	check_route_aliveness "$HOSTNAME" "down" ||
		return $?

	do_lnetctl ping ${rpeer_nids[0]} &&
		error "Ping should fail"

	do_node $rpeer "$LNETCTL ping ${LNIDS[0]}" &&
		error "$rpeer ping should fail"

	# Adding the symmetric route should cause the remote NI to go up and
	# routes to go up
	do_route_add $rpeer $LOCAL_NET ${router_nids[1]} ||
		return $?

	check_router_ni_status $router "up" "up" ||
		return $?

	check_route_aliveness "$HOSTNAME" "up" ||
		return $?

	check_route_aliveness "$rpeer" "up" ||
		return $?

	do_lnetctl ping ${rpeer_nids[0]} ||
		error "Failed to ping ${rpeer_nids[0]}"

	do_node $rpeer "$LNETCTL ping ${LNIDS[0]}" ||
		error "$rpeer failed to ping ${LNIDS[0]}"

	# Stop LNet on local host
	do_lnetctl lnet unconfigure ||
		error "Failed to stop LNet rc=$?"

	check_router_ni_status $router "down" "up" ||
		return $?

	check_route_aliveness "$rpeer" "down" ||
		return $?

	do_lnetctl ping ${rpeer_nids[0]} &&
		error "Ping should fail"

	do_node $rpeer "$LNETCTL ping ${LNIDS[0]}" &&
		error "$rpeer ping should fail"

	return 0
}

test_222() {
	setup_router_test avoid_asym_router_failure=1 || return $?

	do_aarf_enabled_test || return $?

	cleanup_router_test || return $?
}
run_test 222 "Check avoid_asym_router_failure=1"

test_223() {
	local opts="avoid_asym_router_failure=1 lnet_peer_discovery_disabled=1"

	setup_router_test $opts || return $?

	do_aarf_enabled_test || return $?

	cleanup_router_test || return $?
}
run_test 223 "Check avoid_asym_router_failure=1 w/DD disabled"

do_aarf_disabled_test() {
	local router=${ROUTERS[0]}
	local router_nids=( ${ROUTER_NIDS[$router]} )
	local rpeer=${RPEERS[0]}
	local rpeer_nids=( ${RPEER_NIDS[$rpeer]} )

	do_node $router "$LNETCTL set routing 1" ||
		error "Unable to enable routing on $router"

	check_router_ni_status $router "down" "down"

	do_route_add $HOSTNAME $REMOTE_NET ${router_nids[0]} ||
		return $?

	check_router_ni_status $router "up" "down" ||
		return $?

	check_route_aliveness "$HOSTNAME" "up" ||
		return $?

	do_route_add $rpeer $LOCAL_NET ${router_nids[1]} ||
		return $?

	check_router_ni_status $router "up" "up" ||
		return $?


	check_route_aliveness "$HOSTNAME" "up" ||
		return $?

	check_route_aliveness "$rpeer" "up" ||
		return $?

	do_lnetctl ping ${rpeer_nids[0]} ||
		error "Failed to ping ${rpeer_nids[0]}"

	do_node $rpeer "$LNETCTL ping ${LNIDS[0]}" ||
		error "$rpeer failed to ping ${LNIDS[0]}"

	# Stop LNet on local host
	do_lnetctl lnet unconfigure ||
		error "Failed to stop LNet rc=$?"

	check_router_ni_status $router "down" "up" ||
		return $?

	check_route_aliveness "$rpeer" "up" ||
		return $?

	return 0
}

test_224() {
	setup_router_test avoid_asym_router_failure=0 ||
		return $?

	do_aarf_disabled_test ||
		return $?

	cleanup_router_test ||
		return $?
}
run_test 224 "Check avoid_asym_router_failure=0"

test_225() {
	local opts="avoid_asym_router_failure=0 lnet_peer_discovery_disabled=1"

	setup_router_test $opts || return $?

	do_aarf_disabled_test || return $?

	cleanup_router_test ||
		return $?
}
run_test 225 "Check avoid_asym_router_failure=0 w/DD disabled"

test_226() {
	(( $MDS1_VERSION >= $(version_code v2_16_57-110-ged70ccb471) )) ||
		skip "need MDS >= 2.16.57.110 for refcnt fix LU-17440"

	setup_router_test -r 2 || return $?

	do_basic_rtr_test || return $?

	# ping the peer from host to make sure it works
	local rpeer=${RPEERS[0]}
	local rpeer_nids=( ${RPEER_NIDS[$rpeer]} )

	for i in {1..4}; do
		do_lnetctl ping ${rpeer_nids[0]} ||
			error "Failed to ping ${rpeer_nids[0]} on try $i"
	done

	# remove a route from the peer
	local router_nids=( ${ROUTER_NIDS[${ROUTERS[0]}]} )

	do_route_del $rpeer $LOCAL_NET ${router_nids[1]}

	# should attempt to use both routes due to round-robin
	# failure case here is an LBUG on $rpeer
	for i in {1..4}; do
		do_lnetctl ping ${rpeer_nids[0]}
	done

	cleanup_router_test || return $?
}
run_test 226 "test missing route for 1 of 2 routers"

test_227() {
	local opts="lnet_peer_discovery_disabled=1 lnet_health_sensitivity=0"
	opts+=" lnet_transaction_timeout=10"

	[[ $NETTYPE != kfi* ]] || skip "kfi doesn't support drop rules"

	setup_router_test -p 2 $opts || return $?

	do_basic_rtr_test || return $?

	do_node ${RPEERS[0]} $LNETCTL lnet unconfigure ||
		error "Failed to unconfigure lnet on ${RPEERS[0]}"

	local rpeer_nids=( ${RPEER_NIDS[${RPEERS[0]}]} )

	do_lnetctl ping ${rpeer_nids[0]} &&
		error "Expected ping to fail"

	do_lnetctl ping ${rpeer_nids[0]} &&
		error "Expected ping to fail"

	local dropped=$(do_node ${ROUTERS[0]} \
			$LNETCTL peer show -v 2 --nid ${rpeer_nids[0]} |
			grep -A 2 dropped_stats |
			awk '/get:/{print $2}' |
			xargs echo |
			sed 's/ /\+/g' | bc)

	((dropped > 0)) ||
		error "Expected dropped > 0 found $dropped"

	cleanup_router_test
}
run_test 227 "Check router peer health w/DD disabled"

test_228() {
	setup_router_test -r 2 || return $?

	do_basic_rtr_test || return $?

	do_rpc_nodes $HOSTNAME,${RPEERS[0]} load_module \
		../lnet/selftest/lnet_selftest ||
			error "Failed to load lnet-selftest module"

	local lstpid rc

	$LSTSH -H -t $HOSTNAME -f ${RPEERS[0]} -m rw &
	lstpid=$!

	do_lnetctl peer set --health 500 --all ||
		error "Failed to set peer NI health rc = $?"

	wait $lstpid
	rc=$?

	((rc == 0)) ||
		error "LST returned non-zero rc = $rc"

	do_lnetctl peer set --health 1000 --all ||
		error "Failed to set peer NI health rc = $?"

	local no_route=$(dmesg | tail | grep lnet_handle_find_routed_path |
			 grep -c "no route")

	((no_route == 0)) ||
		error "Detected no route send failures"

	cleanup_router_test || return $?
}
run_test 228 "Routes should stay up when health is decremented"

test_230() {
	[[ ${NETTYPE} == tcp* ]] || skip "Need tcp NETTYPE"

	# LU-12815
	echo "Check valid values; Should succeed"
	local i
	local lnid
	local cmd
	for ((i = 4; i < 16; i+=1)); do
		reinit_dlc || return $?
		if ((i % 2 == 0)); then
			add_net "tcp" "${INTERFACES[0]}" || return $?
			do_lnetctl net set --all --conns-per-peer $i ||
				error "should have succeeded $?"
		else
			do_lnetctl net add --net "tcp" --if ${INTERFACES[0]} --conns-per-peer $i ||
				error "should have succeeded $?"
		fi
		$LNETCTL net show -v 1 | grep -q "conns_per_peer: $i" ||
			error "failed to set conns-per-peer to $i"
		lnid="$($LCTL list_nids | head -n 1)"
		do_lnetctl ping "$lnid" ||
			error "failed to ping myself"

		# "lctl --net tcp conn_list" prints the list of active
		# connections. Since we're pinging ourselves, there should be
		# 2 Control connections plus 2*conns_per_peer connections
		# created (one Bulk Input, one Bulk Output in each pair).
		# Here's the sample output for conns_per_peer set to 1:
		# 12345-1.1.1.1@tcp I[0]host01->host01:988 2626560/1061296 nonagle
		# 12345-1.1.1.1@tcp O[0]host01->host01:1022 2626560/1061488 nonagle
		# 12345-1.1.1.1@tcp C[0]host01->host01:988 2626560/1061296 nonagle
		# 12345-1.1.1.1@tcp C[0]host01->host01:1023 2626560/1061488 nonagle
		cmd="printf 'network tcp\nconn_list\n' | lctl | grep -c '$lnid'"

		# Expect 2+conns_per_peer*2 connections. Wait no longer
		# than 10 seconds.
		wait_update $HOSTNAME "$cmd" "$((2+i*2))" 10 ||
			error "expected number of tcp connections $((2+i*2))"
	done

	reinit_dlc || return $?
	add_net "tcp" "${INTERFACES[0]}" || return $?
	echo "Set > 127; Should fail"
	do_lnetctl net set --all --conns-per-peer 128 &&
		error "should have failed $?"

	reinit_dlc || return $?
	add_net "tcp" "${INTERFACES[0]}" || return $?

	local default=$($LNETCTL net show -v 1 |
			awk '/conns_per_peer/{print $NF}')

	echo "Set < 0; Should be ignored"
	do_lnetctl net set --all --conns-per-peer -1 ||
		error "should have succeeded $?"
	$LNETCTL net show -v 1 | grep -q "conns_per_peer: ${default}" ||
		error "Did not stay at default"

	reinit_dlc || return $?
	echo "Add > 127; Should fail and not configure NI"
	! do_lnetctl net add --net "tcp" --if ${INTERFACES[0]} \
		--conns-per-peer 128 ||
		error "should have failed $?"
	! $LNETCTL net show --net tcp 2>/dev/null | grep -qE "nid: .*@tcp$" ||
		error "tcp NI should not be configured"

	reinit_dlc || return $?
	echo "Add overflow value; Should fail and not configure NI"
	! do_lnetctl net add --net "tcp" --if ${INTERFACES[0]} \
		--conns-per-peer 1000000000000000000000000 ||
		error "should have failed $?"
	! $LNETCTL net show --net tcp 2>/dev/null | grep -qE "nid: .*@tcp$" ||
		error "tcp NI should not be configured"

	reinit_dlc || return $?
	echo "Add non-numeric value; Should fail and not configure NI"
	! do_lnetctl net add --net "tcp" --if ${INTERFACES[0]} \
		--conns-per-peer foo ||
		error "should have failed $?"
	! $LNETCTL net show --net tcp 2>/dev/null | grep -qE "nid: .*@tcp$" ||
		error "tcp NI should not be configured"

	reinit_dlc || return $?
	echo "Import > 127 conns-per-peer; Should fail and not configure NI"
	local yfile=$TMP/sanity-lnet-$testnum-import.yaml
	do_lnetctl net add --net "tcp" --if ${INTERFACES[0]} \
		--conns-per-peer 8 ||
		error "should have succeeded $?"
	$LNETCTL export --backup > $yfile || error "export failed $?"
	reinit_dlc || return $?
	# Corrupt the exported value to one outside the valid 0-127 range
	sed -i 's/conns_per_peer: 8/conns_per_peer: 200/' $yfile
	! do_lnetctl import < $yfile ||
		error "import of out-of-range conns-per-peer should have failed"
	! $LNETCTL net show --net tcp 2>/dev/null | grep -qE "nid: .*@tcp$" ||
		error "tcp NI should not be configured"
}
run_test 230 "Test setting conns-per-peer"

test_231() {
	reinit_dlc || return $?

	local net=${NETTYPE}231
	local opts="--net $net --if ${INTERFACES[0]}"

	do_lnetctl net add $opts || error "Failed to add net"

	local lnd=$(basename $LNETLND)
	local param_path="/sys/module/$lnd/parameters"

	[[ -d $param_path ]] ||
		error "Cannot find kernel params for $lnd at $param_path"

	local lnd_pto=$(cat $param_path/peer_timeout)

	# A value of -1 means we use the default peer timeout
	#lnet/include/lnet/lib-lnet.h:#define DEFAULT_PEER_TIMEOUT    180
	((lnd_pto != -1)) || lnd_pto=180

	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml

	local pto=$(awk '/^\s+peer_timeout:/{print $NF}' \
		    $TMP/sanity-lnet-$testnum-expected.yaml)

	((pto == lnd_pto)) ||
		error "Expect peer_timeout $lnd_pto but found $pto"

	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
	compare_yaml_files ||
		error "Unexpected config after net add without options"

	for pto in -1 0 60 180; do
		reinit_dlc || return $?

		sed -i 's/peer_timeout: .*/peer_timeout: '$pto'/' \
			$TMP/sanity-lnet-$testnum-expected.yaml

		do_lnetctl import $TMP/sanity-lnet-$testnum-expected.yaml ||
			error "Failed to import configuration"

		$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml

		# Swap lnd_to for -1 in the expected output
		((pto != -1)) ||
			sed -i 's/peer_timeout: '$pto'/peer_timeout: '$lnd_pto'/' \
				$TMP/sanity-lnet-$testnum-expected.yaml

		compare_yaml_files || error "Wrong config after import"

		do_lnetctl net del $opts || error "Failed to delete net $net"

		do_lnetctl net add $opts --peer-timeout=$pto ||
			error "Failed to add net with peer-timeout=$pto"

		$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml

		compare_yaml_files || error "Wrong config after lnetctl net add"
	done

	return 0
}
run_test 231 "Check DLC handling of peer_timeout parameter"

test_232() {
	[[ ${NETTYPE} =~ ^(tcp|o2ib) ]] ||
		skip "Need tcp or o2ib NETTYPE"

	local i
	local modparam=-1
	local net=${NETTYPE}232

	reinit_dlc || return $?
	if [[ ${NETTYPE} == tcp* ]];then
		modparam=$(cat "/sys/module/ksocklnd/parameters/tos")
	elif [[ ${NETTYPE} == o2ib* ]]; then
		modparam=$(cat "/sys/module/ko2iblnd/parameters/tos")
	fi

	do_lnetctl net add --net $net --if ${INTERFACES[0]} ||
		error "Failed to add net (no ToS)"
	do_lnetctl net show --net $net -v 1 | grep -q "tos: $modparam" ||
		error "Failed ToS value should inherit from module parameter $modparam"
	do_lnetctl net del --net $net --if ${INTERFACES[0]} ||
		error "Failed to delete net $net"

	for i in -1 104 106; do
		do_lnetctl net add --net $net --if ${INTERFACES[0]} --tos $i ||
			error "Failed to add net (ToS: $i)"
		do_lnetctl net show --net $net -v 1 | grep -q "tos: $i" ||
			error "Failed to set ToS value to 104"
		do_lnetctl net del --net $net --if ${INTERFACES[0]} ||
			error "Failed to delete net $net"
	done

	! do_lnetctl net add --net $net --if ${INTERFACES[0]} --tos 300 ||
		error "lnetctl should reject invalid ToS value (>255)"
}
run_test 232 "Test setting ToS value"

test_233() {
	setup_health_test true || return $?

	local retries=$($LNETCTL global show | awk '/retry_count:/{print $NF}')
	(( retries > 0 )) || skip "Need retry_count > 0, found $retries"

	local hstatus
	for hstatus in ${LNET_LOCAL_RESEND_STATUSES}; do
		echo "Simulate intermittent $hstatus"

		lnet_health_pre || return $?
		add_health_test_drop_rules ${hstatus} 2
		do_lnetctl ping --source ${LNIDS[0]} ${RNIDS[0]} ||
			error "ping failed with rc = $?"
		$LCTL net_drop_del -a
		lnet_health_post

		check_successful_resends || return $?
	done

	for hstatus in ${LNET_REMOTE_RESEND_STATUSES}; do
		echo "Simulate intermittent $hstatus"

		lnet_health_pre || return $?
		add_health_test_drop_rules ${hstatus} 2
		do_lnetctl ping --source ${LNIDS[0]} ${RNIDS[0]} ||
			error "ping failed with rc = $?"
		lnet_health_post
		$LCTL net_drop_del -a

		check_successful_resends || return $?
	done

	cleanup_health_test
}
run_test 233 "Check for successful resends"

check_parameter() {
	local para=$1
	local value=$2

	echo "check parameter ${para} value ${value}"

	return $(( $(do_lnetctl net show -v | \
		     tee /dev/stderr | \
		     grep -c "^ \+${para}: ${value}$") != ${#INTERFACES[@]} ))
}

test_234() {
	reinit_dlc || return $?

	add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?

	local nid=$($LCTL list_nids | head -n 1)

	# Verify NI starts with UP status and maximum health
	check_ni_status "$nid" up

	local health=$($LNETCTL net show -v 2 --net ${NETTYPE} |
		       awk '/health value/{print $NF}')
	echo "Initial health value: $health"
	((health == 1000)) ||
		error "NI $nid should start with health value 1000, found $health"

	# Prevent recovery pings from incrementing health during this test
	local hs=$($LNETCTL global show |
		   awk '/^\s+health_sensitivity:/{print $NF}')
	do_lnetctl set health_sensitivity 0 ||
		error "Failed to set health_sensitivity 0"

	# Manually set health to 0 - should transition status to DOWN
	echo "Setting health value to 0"
	do_lnetctl net set --health 0 --nid $nid ||
		error "Failed to set health to 0"

	# Verify status changed to DOWN
	check_ni_status "$nid" down

	# Verify health is actually 0
	health=$($LNETCTL net show -v 2 --net ${NETTYPE} |
		 awk '/health value/{print $NF}')
	echo "Health value after setting to 0: $health"
	((health == 0)) ||
		error "NI $nid health should be 0, found $health"

	# Set health back to maximum - should transition status to UP
	echo "Setting health value to 1000"
	do_lnetctl net set --health 1000 --nid $nid ||
		error "Failed to set health to 1000"

	# Verify status changed back to UP
	check_ni_status "$nid" up

	# Verify health is at maximum
	health=$($LNETCTL net show -v 2 --net ${NETTYPE} |
		 awk '/health value/{print $NF}')
	echo "Health value after setting to 1000: $health"
	((health == 1000)) ||
		error "NI $nid health should be 1000, found $health"

	do_lnetctl set health_sensitivity $hs ||
		error "Failed to set health_sensitivity $hs"

	return 0
}
run_test 234 "Verify NI status changes with manual health value transitions"

test_235() {
	[[ ${NETTYPE} == kfi* ]] && skip "kfi doesn't support drop rules"

	reinit_dlc || return $?
	add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?
	add_net "${NETTYPE}1" "${INTERFACES[0]}" || return $?

	local nid1=$($LCTL list_nids | head -n 1)
	local nid2=$($LCTL list_nids | tail -n 1)

	# Verify both NIDs start with UP status
	check_ni_status "$nid1" up
	check_ni_status "$nid2" up

	# Add drop rule to cause local failures on nid1
	echo "Adding drop rule for $nid1"
	$LCTL net_drop_add -s $nid1 -d $nid1 -m GET -r 1 -e local_timeout ||
		error "Failed to add drop rule"

	# Manually set health to 100 to force NI into recovery
	echo "Setting health to 100 for $nid1"
	do_lnetctl net set --health 100 --nid $nid1 ||
		error "Failed to set health to 100"

	# Recovery ping should cause health to decrement to 0, and status set
	# to DOWN
	wait_update $HOSTNAME \
		"$LNETCTL net show --net ${NETTYPE} | \
		 awk '/^\\\s+status:/{print \\\$NF}'" \
		"down" "60"

	(($? == 0)) || error "NI status did not go down"

	# Verify nid2 is still UP
	check_ni_status "$nid2" up

	# Clear drop rules
	$LCTL net_drop_del -a

	# Set health 900, recovery ping should increment to max and status set
	# to UP
	echo "Restoring health to 900 for $nid1"
	do_lnetctl net set --health 900 --nid $nid1 ||
		error "Failed to set health to 900"

	# Verify status is back to UP
	wait_update $HOSTNAME \
		"$LNETCTL net show --net ${NETTYPE} | \
		 awk '/^\\\s+status:/{print \\\$NF}'" \
		"up" "60"

	(($? == 0)) || error "NI status did not go up"

	return 0
}
run_test 235 "Verify NI status DOWN when health reaches 0 after failures"

check_remote_peer_ni_status() {
	local node="$1"
	local nid="$2"
	local expect="$3"
	local rc

	wait_update $node \
		"$LNETCTL peer show --nid $nid | \
		 grep -E -A 1 '\s+nid: ${nid}$' | \
		 awk '/\s+state:/{print \\\$NF}'" \
		 "$expect" "60"

	rc=$?
	(($rc == 0)) ||
		error "Expect peer NI state \"$expect\" for $nid on $node"

	return $rc
}

test_236() {
	setup_router_test || return $?

	do_basic_rtr_test || return $?

	# Add another net on local host and router
	add_net "${NETTYPE}3" "${INTERFACES[0]}" || return $?

	local router=${ROUTERS[0]}
	local rtr_interfaces=( ${ROUTER_INTERFACES[$router]} )

	do_net_add $router ${NETTYPE}3 ${rtr_interfaces[0]} ||
		return $?

	local nid1=$($LCTL list_nids | head -1)
	local nid2=$($LCTL list_nids | tail -1)

	check_ni_status "$nid1" up
	check_ni_status "$nid2" up

	# Router shows both interfaces as up
	check_remote_peer_ni_status "$router" "$nid1" "up" || return $?
	check_remote_peer_ni_status "$router" "$nid2" "up" || return $?

	# Drop traffic on nid1
	$LCTL net_drop_add -s $nid1 -d $nid1 -r 1 -e local_timeout ||
		error "Failed to add drop rule"

	# Set health to 0
	do_lnetctl net set --health 0 --nid $nid1 ||
		error "Failed to set health to 0"

	# Verify nid1 status is down, nid2 up
	check_ni_status "$nid1" down
	check_ni_status "$nid2" up

	# Verify new NI status is reflected on router
	check_remote_peer_ni_status "$router" "$nid1" "down" || return $?
	check_remote_peer_ni_status "$router" "$nid2" "up" || return $?

	# Clear drop rules, verify status transitions back to UP
	$LCTL net_drop_del -a || return $?
	do_lnetctl net set --health 900 --nid $nid1 ||
		error "Failed to set health to 900"
	wait_update $HOSTNAME \
		"$LNETCTL net show --net ${NETTYPE} | \
		 awk '/^\\\s+status:/{print \\\$NF}'" \
		"up" "60"

	(($? == 0)) || error "NI status did not go to up"

	# Verify new NI status is reflected on router
	check_remote_peer_ni_status "$router" "$nid1" "up" || return $?
	check_remote_peer_ni_status "$router" "$nid2" "up" || return $?

	cleanup_router_test
}
run_test 236 "Local NI state propagates to routers"

do_mr_forwarding_test() {
	local expect_mr_forwarding="$1"

	local rpeer=${RPEERS[0]}
	local rpeer_nids=( ${RPEER_NIDS[$rpeer]} )

	do_lnetctl discover ${rpeer_nids[0]} ||
		error "Discovery failed with rc = $?"

	local router=${ROUTERS[0]}
	local i

#define CFS_FAIL_RTR_HEALTH_INC                0xe004
	do_node $router "$LCTL set_param fail_loc=0xe004" ||
		error "Failed to set fail_loc rc = $?"
	do_node $router "$LNETCTL set health_sensitivity 0" ||
		error "Failed to set health_sensitivity 0 rc = $?"
	do_node $router "$LNETCTL peer set --health 0 --nid ${rpeer_nids[0]}" ||
		error "Failed to set health 0 for ${rpeer_nids[0]} rc = $?"

	local pre_sends=( $(do_node $router \
			    "$LNETCTL peer show -v --nid ${rpeer_nids[0]}" |
			    awk '/send_count/{print $NF}' | xargs echo) )

	do_node $router "$LNETCTL peer show -v 2 | grep -e nid -e health"

	local my_nid=$($LCTL list_nids | head -n 1)

	for i in {1..50}; do
		do_lnetctl ping --source ${my_nid} ${rpeer_nids[0]} ||
			error "Ping failed with rc=$?"
	done

	local post_sends=( $(do_node $router \
			     "$LNETCTL peer show -v --nid ${rpeer_nids[0]}" |
			     awk '/send_count/{print $NF}' | xargs echo ) )

	do_node $router "$LNETCTL peer show -v 2 | grep -e nid -e health"

	echo "pre_sends: ${pre_sends[@]}"
	echo "post_sends: ${post_sends[@]}"

	do_node $router "$LCTL set_param fail_loc=0" ||
		error "Failed to set fail_loc rc = $?"

	if [[ ${#pre_sends[@]} != 2 ]] || [[ ${#post_sends[@]} != 2 ]]; then
		error "Unexpected send counts"
	fi

	if $expect_mr_forwarding ; then
		((post_sends[1] - pre_sends[1] >= 50)) ||
			error "Expected ${post_sends[1]}-${pre_sends[1]} >= 50"
	else
		((post_sends[1] - pre_sends[1] < 50)) ||
			error "Expected ${post_sends[1]}-${pre_sends[1]} < 50"
	fi

	return 0
}

test_237() {
	setup_router_test -m || return $?

	do_basic_rtr_test || return $?

	do_mr_forwarding_test true || return $?

	cleanup_router_test
}
run_test 237 "Check MR forwarding feature"

test_238() {
	setup_router_test -m

	do_lnetctl set discovery 0 ||
		error "Failed to disable discovery rc = $?"

	do_basic_rtr_test || return $?

	# Router should show non-MR for me
	local my_nid=$($LCTL list_nids | head -n 1)
	local mr=$(do_node ${ROUTERS[0]} "$LNETCTL peer show --nid $my_nid" |
		   awk '/Multi-Rail:/{print $NF}')

	[[ $mr == false ]] ||
		error "Expect 'Multi-Rail: false', found $mr"

	do_mr_forwarding_test false || return $?

	cleanup_router_test
}
run_test 238 "Check MR forwarding feature skipped for non-MR source"

test_241() {
	reinit_dlc || return $?

	do_lnetctl net add --net ${NETTYPE} --if ${INTERFACES[0]} ||
		error "Failed to add net"

	do_lnetctl net set -h | grep -q -- "--lnd-timeout:" ||
		skip "lnetctl net set does not support --lnd-timeout option"

	# Capture existing timeout value, we'll restore to this later
	local old_lnd_to=$($LNETCTL net show --net ${NETTYPE} -v |
			   awk '/^\s+timeout:/{print $NF}')
	local expected_lnd_to=$(( old_lnd_to + 1 ))

	# Set new timeout and check it shows up in tunables
	do_lnetctl net set --net ${NETTYPE} --lnd-timeout ${expected_lnd_to} ||
		error "Failed to set LND timeout on ${NETTYPE} net"

	check_parameter "timeout" ${expected_lnd_to} ||
		error "Expected LND timeout $expected_lnd_to"

	# Check if setting LND timeout to zero ends up defaulting to global
	# lnd_timeout value
	local global_lnd_to=$($LNETCTL global show -v |
			      awk '/lnd_timeout:/{print $NF}')

	do_lnetctl net set --net ${NETTYPE} --lnd-timeout 0 ||
		"Failed to set LND timeout on ${NETTYPE} net to zero"

	check_parameter "timeout" ${global_lnd_to} ||
		error "Expected LND timeout $global_lnd_to"

	# Restore tunable timeout to old value
	do_lnetctl net set --net ${NETTYPE} --lnd-timeout ${old_lnd_to}

}
run_test 241 "Check setting LND timeout value via lnetctl updates tunables"

test_245() {
	reinit_dlc || return $?

	add_net ${NETTYPE} ${INTERFACES[0]} || return $?

	local param count paramlist="timeout"

	[[ $NETTYPE != kfi* ]] || paramlist+=" traffic_class_num"

	for param in $paramlist; do
		echo "Check $param present in net show -v"
		count=$($LNETCTL net show -v | grep -c " \+$param:")

		((count == 1)) ||
			error "$param parameter is missing"

		echo "Check $param absent from export -b"
		count=$($LNETCTL export -b | grep -c " \+$param:")

		((param == 0)) ||
			error "$param parameter is present"
	done

	return 0
}
run_test 245 "Check read-only params do not appear in export --backup"

### Test that linux route is added for each ni
test_250() {
	local skip_param

	[[ ${NETTYPE} == tcp* ]] || skip "Need tcp NETTYPE"

	reinit_dlc || return $?
	add_net "tcp" "${INTERFACES[0]}" || return $?

	skip_param=$(cat /sys/module/ksocklnd/parameters/skip_mr_route_setup)
	[[ ${skip_param:-0} -ne 0 ]] &&
		skip "Need skip_mr_route_setup=0 found $skip_param"

	ip route show table ${INTERFACES[0]} | grep -q "${INTERFACES[0]}"
}
run_test 250 "test that linux routes are added"

test_251() {
	[[ ${NETTYPE} =~ kfi* ]] ||
		skip "Need kfi NETTYPE"

	reinit_dlc || return $?
	add_net "kfi" "${INTERFACES[0]}" || return $?
	add_net "kfi1" "${INTERFACES[0]}" || return $?
	add_net "kfi10" "${INTERFACES[0]}" || return $?
	return 0
}
run_test 251 "Define multiple kfi networks on single interface"

test_252() {

	(( $MDS1_VERSION >= $(version_code v2_14_55-10-gc807e3f33b) )) ||
		skip "need MDS >= 2.14.55.10 for memory leak fix"

	setup_health_test false || return $?

	local rc=0

	do_rpc_nodes $RNODE unload_modules_local || rc=$?

	if [[ $rc -ne 0 ]]; then
		cleanup_health_test || return $?

		error "Failed to unload modules on $RNODE rc=$rc"
	else
		RLOADED=false
	fi

	local ts1=$(date +%s)

	do_lnetctl ping --timeout 15 ${RNIDS[0]} &&
		error "Expected ping ${RNIDS[0]} to fail"

	local ts2=$(date +%s)

	local delta=$(echo "$ts2 - $ts1" | bc)

	[[ $delta -lt 15 ]] ||
		error "Ping took longer than expected to fail: $delta"

	cleanup_health_test
}
run_test 252 "Ping to down peer should unlink quickly"

do_expired_message_drop_test() {
	local rnid lnid old_tto

	local old_retry=$($LNETCTL global show |
			  awk '/^\s+retry_count:/{print $NF}')

	# Capture default, global LNet transaction timeout (LTT). If there's an
	# LND timeout (LNDT) set for $NETTYPE, the true LTT = LNDT(LRC + 1) + 1.
	local old_ltt=$($LNETCTL global show |
			awk '/^\s+transaction_timeout:/{print $NF}')
	local old_lnd_to=$($LNETCTL net show --net $NETTYPE --verbose |
			   awk '/^\s+timeout:/{print $NF}')
	[[ -z "$old_lnd_to" ]] ||
		old_ltt=$(( old_lnd_to * (old_retry + 1) + 1 ))

	do_lnetctl set retry_count 0 || error "Failed to set retry count to 0"
	$LNETCTL global show

	[[ -z $old_ltt ]] &&
		error "Cannot determine LNet transaction timeout"

	# Set new LNet transaction timeout (LTT)
	local ltt=10
	set_ltt_node $HOSTNAME $ltt $NETTYPE ||
		error "Failed to set transaction timeout"

	# We want to consume all peer credits for at least LTT seconds
	local delay=$((ltt + 1))

	for lnid in "${LNIDS[@]}"; do
		for rnid in "${RNIDS[@]}"; do
			add_delay_rule -s "${lnid}" -d "${rnid}" \
				-l "${delay}" -r 1 -m GET
		done
	done

	declare -a pcs

	pcs=( $($LNETCTL peer show -v --nid "${RNIDS[0]}" |
		awk '/max_ni_tx_credits:/{print $NF}' |
		xargs echo) )

	[[ ${#RNIDS[@]} -ne ${#pcs[@]} ]] &&
		error "Expect ${#RNIDS[@]} peer credit values found ${#pcs[@]}"

	local rnet lnid lnet i j

	# Need to use --source for multi-rail configs to ensure we consume
	# all available peer credits
	for ((i = 0; i < ${#RNIDS[@]}; i++)); do
		local ping_args="--timeout $((delay+2))"

		rnet=${RNIDS[i]##*@}
		for lnid in ${LNIDS[@]}; do
			lnet=${lnid##*@}
			[[ $rnet == $lnet ]] && break
		done

		ping_args+=" --source ${lnid} ${RNIDS[i]}"
		for j in $(seq 1 "${pcs[i]}"); do
			$LNETCTL ping ${ping_args} 1>/dev/null &
		done

		echo "Issued ${pcs[i]} pings to ${RNIDS[i]} from $lnid"
	done

	# This ping should be queued on peer NI tx credit
	$LNETCTL ping --timeout $((delay+2)) "${RNIDS[0]}" &

	sleep ${delay}

	del_delay_rule -a

	wait

	# Messages sent from the delay list do not go through
	# lnet_post_send_locked(), thus we should only have a single drop
	local dropped

	dropped=$($LNETCTL peer show -v 2 --nid "${RNIDS[0]}" |
			grep -A 2 dropped_stats |
			awk '/get:/{print $2}' |
			xargs echo |
			sed 's/ /\+/g' | bc)

	[[ $dropped -ne 1 ]] &&
		error "Expect 1 dropped GET but found $dropped"

	# Restore retry_count and transaction timeout values in the order they
	# were changed.
	do_lnetctl set retry_count $old_retry
	set_ltt_node $HOSTNAME ${old_ltt} $NETTYPE

	return 0
}

test_253() {
	setup_health_test false || return $?

	do_expired_message_drop_test || return $?

	cleanup_health_test
}
run_test 253 "Message delayed beyond deadline should be dropped (single-rail)"

test_254() {
	setup_health_test true || return $?

	do_expired_message_drop_test || return $?

	cleanup_health_test
}
run_test 254 "Message delayed beyond deadline should be dropped (multi-rail)"

test_255() {
	[[ ${NETTYPE} == tcp* ]] || skip "Need tcp NETTYPE"
	(( $CLIENT_VERSION < $(version_code 2.17.53) )) ||
		skip "kernel ip2nets deprecated in 2.17.53"

	reinit_dlc || return $?

	cleanup_lnet || return $?

	local routes_str="o2ib ${IF0_NET}.[$GW_HOSTNUM-$((GW_HOSTNUM+4))]"
	local network_str="${NETTYPE}(${INTERFACES[0]})"

	load_lnet "networks=\"${network_str}\" routes=\"${routes_str}\"" ||
		error "Failed to load LNet"

	$LCTL net up $LNET_CONFIG_OPT ||
		error "Failed to load LNet with networks=\"${network_str}\" routes=\"${routes_str}\""

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
net:
    - net type: ${NETTYPE}
      local NI(s):
        - interfaces:
              0: ${INTERFACES[0]}
EOF
	append_net_tunables tcp

	echo "route:" >> $TMP/sanity-lnet-$testnum-expected.yaml
	for i in $(seq $GW_HOSTNUM $((GW_HOSTNUM + 4))); do
		cat <<EOF >> $TMP/sanity-lnet-$testnum-expected.yaml
    - net: o2ib
      gateway: ${IF0_NET}.${i}@${NETTYPE}
      hop: -1
      priority: 0
EOF
	done

	echo "peer:" >> $TMP/sanity-lnet-$testnum-expected.yaml
	for i in $(seq $GW_HOSTNUM $((GW_HOSTNUM + 4))); do
		cat <<EOF >> $TMP/sanity-lnet-$testnum-expected.yaml
    - primary nid: ${IF0_NET}.${i}@${NETTYPE}
      Multi-Rail: False
      peer ni:
        - nid: ${IF0_NET}.${i}@${NETTYPE}
EOF
	done

	append_global_yaml

	$LNETCTL export --backup >  $TMP/sanity-lnet-$testnum-actual.yaml ||
		error "export failed $?"

	validate_gateway_nids

	# Since we have a complex YAML config file we can test import
	reinit_dlc || return $?

	do_lnetctl import < $TMP/sanity-lnet-$testnum-actual.yaml ||
		error "Import failed $?"

	validate_gateway_nids
}
run_test 255 "Use lnet routes param with pdsh syntax"

test_256() {
	[[ $NETTYPE != kfi* ]] || skip "kfi doesn't support delay rules"

	setup_router_test peer_buffer_credits=1024 || return $?

	do_basic_rtr_test || return $?

	local rpeer=${RPEERS[0]}
	local rpeer_nids=( ${RPEER_NIDS[$rpeer]} )
	local rpnid=${rpeer_nids[0]}
	local router=${ROUTERS[0]}
	local router_nids=( ${ROUTER_NIDS[$router]} )
	local rtrpnid=${router_nids[0]}

	local rtr_pc=$(do_node $router $LNETCTL peer show -v --nid $rpnid |
		       awk '/max_ni_tx_credits:/{print $NF}' |
		       xargs echo | sed 's/ /\+/g' | bc)

	((rtr_pc > 0)) ||
		error "$router couldn't determine peer credits for $rpnid"

	local my_pc=$($LNETCTL peer show -v --nid $rtrpnid |
		      awk '/max_ni_tx_credits:/{print $NF}' |
		      xargs echo | sed 's/ /\+/g' | bc)

	((my_pc > 0)) || error "couldn't determine peer credits for $rtrpnid"

	if ((my_pc < rtr_pc )); then
		cleanup_router_test || return $?
		skip "Need local peer credits >= router's peer credits"
	fi

	local old_retry=$(do_node $router $LNETCTL global show |
			  awk '/retry_count:/{print $NF}')

	[[ -n $old_retry ]] ||
		error "Cannot determine LNet retry count"

	# Capture default, global LNet transaction timeout (LTT). If there's an
	# LND timeout (LNDT) set for the router's REMOTE_NET, the true
	# LTT = LNDT(LRC + 1) + 1.
	local old_ltt=$(do_node $router $LNETCTL global show |
			awk '/transaction_timeout:/{print $NF}')
	local old_lnd_to=$(do_node $router $LNETCTL net show --net $REMOTE_NET \
			   --verbose | awk '/^\s+timeout:/{print $NF}')
	[[ -n $old_lnd_to ]] && old_ltt=$(( old_lnd_to * (old_retry + 1) + 1 ))

	# Set router's retry_count to zero to shorten/simplify message timeout.

	do_node $router $LNETCTL set retry_count 0 ||
		error "Failed to set retry_count"

	local ltt=10

	set_ltt_node $router $ltt $REMOTE_NET ||
		error "Failed to set transaction_timeout"

#define CFS_FAIL_DELAY_MSG_FORWARD      0xe002
	do_node $router $LCTL set_param fail_loc=0xe002

	# We want to consume all peer credits for at least transaction_timeout
	# seconds
	local delay=$((ltt + 1))

	local rnid lnid cmd
	local args="-l $delay -r 1 -m GET"

	for lnid in ${LNIDS[@]}; do
		for rnid in ${rpeer_nids[@]}; do
			cmd="$LCTL $net_delay_add -s ${lnid} -d ${rnid} $args"
			echo "$router $cmd"
			do_node $router $cmd || error "Failed to add delay rule"
		done
	done

	local idx
	declare -a pids

	for idx in $(seq 1 ${rtr_pc}); do
		echo "$LNETCTL ping --timeout $((delay+2)) $rpnid"
		$LNETCTL ping --timeout $((delay+2)) $rpnid 1>/dev/null &
		pids[$idx]=$!
	done

	((idx++))

	echo "Issued ${rtr_pc} pings to $rpnid"

	# This ping should be queued on the router's peer NI tx credit queue
	$LNETCTL ping --timeout $((delay+2)) $rpnid &
	pids[$idx]=$!

	echo "Issued last ping - sleep $delay"
	sleep ${delay}

	do_node $router $LCTL $net_delay_del -a

	local rc=0 rcsum=0
	for idx in $(seq 1 $((rtr_pc + 1))); do
		wait ${pids[$idx]} || rc=$?
		((rc != 0)) && echo "ping pid ${pids[$idx]} returned rc=$rc"
		((rcsum += rc))
	done

	((rcsum == 0)) || error "Detected ping failures"

	# Restore old retry_count and REMOTE_NET LTT values
	do_node $router $LNETCTL set retry_count ${old_retry}
	set_ltt_node $router ${old_ltt} $REMOTE_NET

	# Router should not drop any of the messages that have exceeded their
	# deadline
	local dropped=$(do_node $router $LNETCTL peer show -v 2 --nid $rpnid |
			grep -A 2 dropped_stats |
			awk '/get:/{print $2}' |
			xargs echo |
			sed 's/ /\+/g' | bc)

	((dropped == 0)) ||
		error "Expect 0 dropped GET but found $dropped"

	cleanup_router_test
}
run_test 256 "Router should not drop messages that are past the deadline"

test_257() {
	setup_router_test -r 2 -p 1 || return $?

	do_basic_rtr_test || return $?

	do_rpc_nodes $HOSTNAME,${RPEERS[0]} load_module \
		../lnet/selftest/lnet_selftest ||
			error "Failed to load lnet-selftest module"

	local param
	local all_nodes=$(comma_list ${ROUTERS[@]} ${RPEERS[@]} $HOSTNAME)

	for param in alive_router_check_interval router_ping_timeout; do
		do_nodes $all_nodes "echo 5 > /sys/module/lnet/parameters/$param"
	done

	$LSTSH -H -t $HOSTNAME -f ${RPEERS[0]} -m rw &

	log "Wait 5s for LST to start"
	sleep 5

	log "Disable routing on ${ROUTERS[0]}"
	do_node ${ROUTERS[0]} "$LNETCTL set routing 0" ||
		error "Failed to disable routing rc = $?"

	log "Wait for lst to finish"
	wait

	local drops=$(do_node ${ROUTERS[0]} \
		      "$LNETCTL net show -v; $LNETCTL peer show -v" |
		      awk '/drop_count:/{print $NF}' | xargs echo |
		      sed 's/ /\+/g' | bc)

	((drops == 0)) ||
		error "Detected $drops dropped messages - expect 0"

	$LSTSH -H -t $HOSTNAME -f ${RPEERS[0]} -m rw &

	log "Wait 5s for LST to start"
	sleep 5

	log "Enable routing on ${ROUTERS[0]}"
	do_node ${ROUTERS[0]} "$LNETCTL set routing 1" ||
		error "Failed to enable routing rc = $?"

	log "Wait for lst to finish"
	wait

	drops=$(do_node ${ROUTERS[0]} \
		"$LNETCTL net show -v; $LNETCTL peer show -v" |
		awk '/drop_count:/{print $NF}' | xargs echo |
		sed 's/ /\+/g' | bc)

	((drops == 0)) ||
		error "Detected $drops dropped messages - expect 0"

	cleanup_router_test
}
run_test 257 "Test graceful router shutdown/startup"

check_sysctl() {
	while IFS= read -r line; do
		# Couldn't find a way to break this line
		if [[ "$line" =~ \
		      ^[[:space:]]*([a-zA-Z0-9_.-]+)[[:space:]]*=[[:space:]]*([a-zA-Z0-9_.-]+)[[:space:]]*$ \
		   ]]; then
			value=$(sysctl -n "${BASH_REMATCH[1]}" 2>/dev/null)
			if [ -z "${value}" ]; then
				echo "Parameter ${BASH_REMATCH[1]} not set"
				return 1
			fi
			echo "found: ${BASH_REMATCH[1]} ${value}"
			if [ "${value}" != "${BASH_REMATCH[2]}" ]; then
				echo "Parameter ${BASH_REMATCH[1]}" \
					" wrong value: ${value}" \
					" expected: ${BASH_REMATCH[2]}"
				return 2
			fi
		fi
	done < "$1"
	return 0
}

### Test that linux route is added for each ni

test_260() {
	local sysctl_file="/etc/lnet-sysctl.conf"
	local sysctl_conf_bak="/etc/lnet-sysctl.bak"
	local sysctl_bak=$TMP/lnet-sysctl.bak
	local -i max_wait=60
	local -i retries=0

	echo "Setting default values and create backup for check"

	sysctl -w net.ipv4.neigh.default.gc_thresh1=128 > "$sysctl_bak"
	sysctl -w net.ipv4.neigh.default.gc_thresh2=512 >> "$sysctl_bak"
	sysctl -w net.ipv4.neigh.default.gc_thresh3=1024 >> "$sysctl_bak"
	sysctl -w net.ipv6.neigh.default.gc_thresh1=128 >> "$sysctl_bak"
	sysctl -w net.ipv6.neigh.default.gc_thresh2=512 >> "$sysctl_bak"
	sysctl -w net.ipv6.neigh.default.gc_thresh3=1024 >> "$sysctl_bak"

	echo "Check default configuration"
	retries=0
	until check_sysctl "${sysctl_bak}"
	do
		if (( retries >= max_wait )); then
			error "Default sysconfig values not set"
		fi
		sleep 1
		retries+=1
	done


	load_modules || error "Failed to load Modules"

	sysctlstat=$(cat /sys/module/lnet/parameters/enable_sysctl_setup 2>&-)
	echo "enable_sysctl_setup set to ${sysctlstat}"

	echo "New configuration"

	echo 1 > /sys/module/lnet/parameters/enable_sysctl_setup 2>&1

	sysctlstat=$(cat /sys/module/lnet/parameters/enable_sysctl_setup 2>&-)
	echo "enable_sysctl_setup set to ${sysctlstat}"

	$LNETCTL setup-sysctl ||
		error "setup-sysctl failed"

	echo "Check new configuration"
	retries=0
	until check_sysctl "${sysctl_file}"; do
		if (( retries >= max_wait )); then
			error "New sysctl values not set"
		fi
		sleep 1
		retries+=1
	done

	echo "Reset to original values"
	echo 0 > /sys/module/lnet/parameters/enable_sysctl_setup 2>&1

	sysctlstat=$(cat /sys/module/lnet/parameters/enable_sysctl_setup 2>&-)
	echo "enable_sysctl_setup set to ${sysctlstat}"

	$LNETCTL setup-sysctl ||
		error "setup-sysctl failed"

	echo "Check original configuration"
	retries=0
	until check_sysctl "${sysctl_bak}"; do
		if (( retries >= max_wait )); then
			echo "Original sysconfig values not set"
			break
		fi
		sleep 1
		retries+=1
	done

	rm -f "${sysctl_bak}"

	unload_modules || error "Failed to cleanup Modules"
}
run_test 260 "test that linux sysctl parameter are set correctly"

test_265() {
	reinit_dlc || return $?

	local cpt

	for cpt in "[1-0]" "[]"; do
		cat <<EOF > $TMP/sanity-lnet-$testnum.yaml
net:
-     net type: ${NETTYPE}
      local NI(s):
      -     interfaces:
                  0: ${INTERFACES[0]}
            CPT: "${cpt}"
EOF

		! do_lnetctl import $TMP/sanity-lnet-$testnum.yaml ||
			error "Import should have failed"

		cat <<EOF > $TMP/sanity-lnet-$testnum.yaml
net:
-     net type: ${NETTYPE}
      local NI(s):
      -     interfaces:
                  0: ${INTERFACES[0]}
            CPT: ${cpt}
EOF

		! do_lnetctl import $TMP/sanity-lnet-$testnum.yaml ||
			error "Import should have failed"
	done
}
run_test 265 "Import of invalid CPT should fail"

test_266() {
	reinit_dlc || return $?

	add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?

	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml

	local cpt=$($LNETCTL net show -v --net ${NETTYPE} |
		    awk '/CPT/{print $NF}')
	local low=$(awk -F, '{print $1}'<<<"$cpt" | tr -d '[]"')
	local high=$(awk -F, '{print $NF}'<<<"$cpt" | tr -d '[]"')

	local tyaml=$TMP/sanity-lnet-$testnum.yaml

	cat <<EOF > $tyaml
net:
-     net type: ${NETTYPE}
      local NI(s):
      -     interfaces:
                  0: ${INTERFACES[0]}
EOF
	$LNETCTL net show -v -n ${NETTYPE} | awk '/^\s+tunables:$/,/^\s+CPT:/' |
		grep -v -e 'dev cpt' -e 'CPT' >> $tyaml

	echo "            CPT: \"[$low-$high]\"" >> $tyaml

	reinit_dlc || return $?

	do_lnetctl import $tyaml || error "Import failed rc = $?"

	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml

	compare_yaml_files || return $?

	cat <<EOF > $tyaml
net:
-     net type: ${NETTYPE}
      local NI(s):
      -     interfaces:
                  0: ${INTERFACES[0]}
EOF
	$LNETCTL net show -v -n ${NETTYPE} | awk '/^\s+tunables:$/,/^\s+CPT:/' |
		grep -v -e 'dev cpt' -e 'CPT' >> $tyaml

	echo "            CPT: [$low-$high]" >> $tyaml

	reinit_dlc || return $?

	do_lnetctl import $tyaml || error "Import failed rc = $?"

	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml

	compare_yaml_files || return $?
}
run_test 266 "Validate CPT parsing in network sequence"

test_270() {
	[[ "$NETTYPE" =~ tcp ]] || skip "Need tcp NETTYPE"

	cleanup_lnet || error "Failed to unload modules before test execution"
	reinit_dlc || return $?

	#define CFS_FAIL_TEST_PING_MD 0xe003  CFS_FAIL_SOME 0x10000000
	"$LCTL" set_param fail_loc=0x1000e003 fail_val=100

	"$LNETCTL" net add --net "tcp" --if "${INTERFACES[0]}"

	local nid="$($LCTL list_nids | head -n 1)"

	do_lnetctl ping "$nid" ||
		error "$LNETCTL ping $nid with TEST_PING_MD failed"
	dmesg | grep -q "found metadata reply" ||
		error "Didn't find metadata reply message in log"

	cleanup_lnet || error "Failed to unload modules after test execution"
}
run_test 270 "Test NID metadata"

test_280() {
	cleanup_lnet || error "Failed to unload modules before test execution"

	local rc=0

	modinfo ksocklnd 2>/dev/null || rc=$?

	((rc == 1)) || skip "Need request_module to fail"

	local lnd=""

	case $NETTYPE in
		o2ib*) lnd=ko2iblnd;;
		tcp*) lnd=ksocklnd;;
		kfi*) lnd=kkfilnd;;
		gni*) lnd=kgnilnd;;
	esac

	[[ -n $lnd ]] || skip "Unsupported NETTYPE $NETTYPE"

	load_lnet || error "Failed to load lnet"

	$LUSTRE_RMMOD $lnd || error "Failed to unload $lnd"

	$LNETCTL lnet configure -a

	$LUSTRE_RMMOD
}
run_test 280 "Don't panic when request_module fails"

test_290() {
	[[ ${NETTYPE} == tcp* ]] || skip "Need tcp NETTYPE"
	if (( $LINUX_VERSION_CODE < $(version_code 4.2.0) )); then
		skip "Need kernel >= 4.2.0 for local net namespace"
	fi

	cleanup_lnet || error "Failed to unload modules before test execution"
	setup_fakeif || error "Failed to add fake IF"
	reinit_dlc || return $?

	$LNETCTL net add --nid ${FAKE_IP}@tcp
	sleep 1
	accept_port=$(cat /sys/module/lnet/parameters/accept_port)
	binding_found=$(ss -ltnup | awk -v ip="${FAKE_IP}" -v port="${accept_port}" '
		$5 == ip ":" port { print }
	')
	if [[ -z "$binding_found" ]]; then
		error "Explicit binding not found for ${FAKE_IP}:${accept_port}"
	fi

	cleanup_fakeif
}
run_test 290 "Check that lnet acceptor is using explicit address binding"

test_291() {
	[[ ${NETTYPE} == tcp* ]] ||
		skip "Need tcp NETTYPE"

	reinit_dlc || return $?
	cleanup_lnet || return $?

	local lnid cmd

	load_lnet "accept_port_bulk=989" || error "Failed to load LNet"

	$LCTL net up $LNET_CONFIG_OPT ||
		error "Failed to load LNet with accept_port_bulk=989"

	$LNETCTL net show -v 1 | grep -q "conns_per_peer: 1" ||
		error "failed to set conns-per-peer to 1"
	lnid="$(lctl list_nids | head -n 1)"
	do_lnetctl ping "$lnid" ||
		error "failed to ping myself"

	# "lctl --net tcp conn_list" prints the list of active
	# connections. Since we're pinging ourselves, there should be
	# 2 Control connections plus 2*conns_per_peer connections
	# created (one Bulk Input, one Bulk Output in each pair).
	# Here's the sample output for conns_per_peer set to 1:
	# 12345-1.1.1.1@tcp I[0]host01->host01:988 2626560/1061296 nonagle
	# 12345-1.1.1.1@tcp O[0]host01->host01:1022 2626560/1061488 nonagle
	# 12345-1.1.1.1@tcp C[0]host01->host01:988 2626560/1061296 nonagle
	# 12345-1.1.1.1@tcp C[0]host01->host01:1023 2626560/1061488 nonagle
	cmd="printf 'network tcp\nconn_list\n' | lctl | grep -c ':989'"

	# Expect one bulk connection on port 989
	wait_update $HOSTNAME "$cmd" "1" 10 ||
		error "expected 1 tcp connection on port 989"

	cleanup_lnet || return $?
}
run_test 291 "load lnet using custom accept port for bulk tcp connections"

test_300() {
	# LU-13274
	local header
	local out=$TMP/$tfile
	local prefix=/usr/include/linux/lnet

	# We use a hard coded prefix so that this test will not fail
	# when run in tree.
	CC=${CC:-cc}
	if ! which $CC > /dev/null 2>&1; then
		skip_env "$CC is not installed"
	fi

	cleanup_lnet || return $?
	load_lnet

	local cc_args="-Wall -Werror -std=c99 -c -x c /dev/null -o $out"
	if ! [[ -d $prefix ]]; then
		# Assume we're running in tree and fixup the include path.
		prefix=$LUSTRE/../lnet/include/uapi/linux/lnet
		cc_args+=" -I $LUSTRE/../lnet/include/uapi"
	fi

	for header in $prefix/*.h; do
		if ! [[ -f "$header" ]]; then
			continue
		fi

		echo "$CC $cc_args -include $header"
		$CC $cc_args -include $header ||
			error "cannot compile '$header'"
	done
	rm -f $out
}
run_test 300 "packaged LNet UAPI headers can be compiled"

# LU-16081 lnet: Memory leak on adding existing interface

test_301() {
	reinit_dlc || return $?
	do_lnetctl net add --net ${NETTYPE} --if ${INTERFACES[0]} ||
		error "Failed to add net"
	do_lnetctl net add --net ${NETTYPE} --if ${INTERFACES[0]} &&
		error "add net should have failed"
	do_lnetctl net del --net ${NETTYPE} --if ${INTERFACES[0]} ||
		error "Failed to del net"
	unload_modules
}
run_test 301 "Check for dynamic adds of same/wrong interface (memory leak)"

test_302() {
	! [[ $NETTYPE =~ (tcp|o2ib) ]] && skip "Need tcp or o2ib NETTYPE"

	reinit_dlc || return $?

	add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?

	local nid=$($LCTL list_nids)

	do_lnetctl ping ${nid} ||
		error "pinging self failed $?"
	do_lnetctl debug peer --nid ${nid} ||
		error "failed to dump peer debug info $?"
}
run_test 302 "Check that peer debug info can be dumped"

test_303() {
	[[ ${NETTYPE} == tcp* ]] || skip "Need tcp NETTYPE"

	setup_health_test true || return $?

	setup_fakeif || error "Failed to add fake IF"

	add_net "${NETTYPE}99" "$FAKE_IF" || return $?

	local nid=$($LCTL list_nids | tail --lines 1)

	# Our updated config should be pushed to RNODE
	local found=$(do_node $RNODE "$LNETCTL peer show --nid $nid")

	[[ -z $found ]] && error "Peer not updated on $RNODE"

	local prim=$($LCTL list_nids | head -n 1)

	if ! grep -q -- " primary nid: $prim"<<<"${found}"; then
		echo "$found"
		error "Wrong primary nid"
	fi

	echo "Set $FAKE_IF down"
	echo "ip link set dev $FAKE_IF down"
	ip link set dev $FAKE_IF down
	check_ni_status "$nid" down

	local hval=$(do_node $RNODE "$LNETCTL peer show --nid $nid -v 2 | \
				     grep -e '- nid:' -e 'health value:'")

	hval=$(grep -A 1 $nid<<<"$hval" | tail -n 1 | awk '{print $NF}')
	(( hval < 1000 )) ||
		error "Expect $hval < 1000"

	return 0
}
run_test 303 "Check peer NI health after link down"

test_304() {
	[[ ${NETTYPE} == tcp* ]] || skip "Need tcp NETTYPE"

	cleanup_lnet || error "Failed to unload modules before test execution"

	setup_fakeif || error "Failed to add fake IF"

	reinit_dlc || return $?

	add_net "tcp" "${INTERFACES[0]}" || return $?
	add_net "tcp" "$FAKE_IF" || return $?

	local nid1=$(lctl list_nids | head -n 1)
	local nid2=$(lctl list_nids | tail --lines 1)

	check_ni_status "$nid1" up
	check_ni_status "$nid2" up

	do_lnetctl peer add --prim_nid ${nid2} --lock_prim ||
		error "peer add failed $?"
	local locked_peer_state=($(do_lnetctl peer show -v 4 --nid ${nid2} |
		awk '/peer state/{print $NF}'))

	# Expect peer state bits:
	#   LNET_PEER_MULTI_RAIL(0) | LNET_PEER_CONFIGURED(3) |
	#   LNET_PEER_LOCK_PRIMARY(20)
	(( $locked_peer_state != "1048585")) &&
		error "Wrong peer state \"$locked_peer_state\" expected 1048585"

	# Clear LNET_PEER_CONFIGURED bit and verify
	do_lnetctl peer set --nid ${nid2} --state 1048577 ||
		error "peer add failed $?"
	locked_peer_state=($(do_lnetctl peer show -v 4 --nid ${nid2} |
		awk '/peer state/{print $NF}'))
	(( $locked_peer_state != "1048577")) &&
		error "Wrong peer state \"$locked_peer_state\" expected 1048577"
	do_lnetctl discover ${nid1} ||
		error "Failed to discover peer"

	# Expect nid2 and nid1 peer entries to be consolidated,
	# nid2 to stay primary
	cat <<EOF >> $TMP/sanity-lnet-$testnum-expected.yaml
peer:
    - primary nid: ${nid2}
      Multi-Rail: True
      peer ni:
        - nid: ${nid1}
          state: NA
        - nid: ${nid2}
          state: NA
EOF
	$LNETCTL peer show > $TMP/sanity-lnet-$testnum-actual.yaml
	compare_yaml_files ||
		error "Unexpected peer configuration"

	locked_peer_state=($(do_lnetctl peer show -v 4 --nid ${nid2} |
		awk '/peer state/{print $NF}'))
	# Expect peer state bits to be added:
	#   LNET_PEER_DISCOVERED(4) | LNET_PEER_NIDS_UPTODATE(8)
	(( $locked_peer_state != "1048849")) &&
		error "Wrong peer state \"$locked_peer_state\" expected 1048849"

	cleanup_lnet
}
run_test 304 "Check locked primary peer nid consolidation"

test_305() {
	[[ ${NETTYPE} == tcp* ]] || skip "Need tcp NETTYPE"

	reinit_dlc || return $?

	add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?

	local nid=$($LCTL list_nids)

	do_lnetctl ping ${nid} ||
		error "pinging self failed $?"

	[[ "${nid%@*}" == "$(hostname -i | awk '{print $1}')" ]] ||
		skip "IP $(hostname -i) isn't NID $nid"

	nid="$(hostname -s)@${NETTYPE}"
	do_lnetctl ping $nid ||
		error "pinging own hostname $nid failed $?"
}
run_test 305 "Resolve hostname before lnetctl ping"

static_config() {
	local module=$1
	local setting=$2

	cleanup_lnet || error "Failed to cleanup LNet"

	load_module ../libcfs/libcfs/libcfs ||
		error "Failed to load module libcfs rc = $?"

	load_module ../lnet/lnet/lnet ||
		error "Failed to load module lnet rc = $?"

	echo "loading ${module} ${setting} type ${NETTYPE}"
	load_module "${module}" "${setting}" ||
		error "Failed to load module ${module} rc = $?"

	do_lnetctl lnet configure $LNET_CONFIG_INIT_OPT ||
		error "lnet configure $LNET_CONFIG_INIT_OPT failed rc = $?"

	return 0
}

test_310() {
	local value=65

	if [[ ${NETTYPE} == tcp* ]];then
		static_config "../lnet/klnds/socklnd/ksocklnd" \
			      "sock_timeout=${value}"
	elif [[ ${NETTYPE} == o2ib* ]]; then
		static_config "../lnet/klnds/o2iblnd/ko2iblnd" \
			      "timeout=${value}"
	elif [[ ${NETTYPE} == gni* ]]; then
		static_config "../lnet/klnds/gnilnd/kgnilnd" \
			      "timeout=${value}"
	else
		skip "NETTYPE ${NETTYPE} not supported"
	fi

	check_parameter "timeout" $value

	return $?
}
run_test 310 "Set timeout and verify"

test_311() {
	[[ $NETTYPE == kfi* ]] ||
		skip "Need kfi network type"

	setupall || error "setupall failed"

	mkdir -p $DIR/$tdir || error "mkdir failed"
	dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 oflag=direct ||
		error "dd write failed"

	local osts=$(osts_nodes)

#define CFS_KFI_FAIL_WAIT_SEND_COMP1 0xF115
	do_nodes $osts $LCTL set_param fail_loc=0x8000F115
	dd if=$DIR/$tdir/$tfile of=/dev/null bs=1M count=1 ||
		error "dd read failed"

	rm -f $DIR/$tdir/$tfile
	rmdir $DIR/$tdir

	cleanupall || error "Failed cleanup"
}
run_test 311 "Fail bulk put in send wait completion"

test_312() {
	[[ $NETTYPE == kfi* ]] ||
		skip "Need kfi network type"

	setupall || error "setupall failed"

	mkdir -p $DIR/$tdir || error "mkdir failed"

	local osts=$(osts_nodes)

#define CFS_KFI_FAIL_WAIT_SEND_COMP3 0xF117
	do_nodes $osts $LCTL set_param fail_loc=0x8000F117
	dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 oflag=direct ||
		error "dd write failed"

	local tfile2="$DIR/$tdir/testfile2"

	do_nodes $osts $LCTL set_param fail_loc=0x8000F117
	dd if=$DIR/$tdir/$tfile of=$tfile2 bs=1M count=1 oflag=direct ||
		error "dd read failed"

	rm -f $DIR/$tdir/$tfile
	rm -f $tfile2
	rmdir $DIR/$tdir

	cleanupall || error "Failed cleanup"
}
run_test 312 "TAG_RX_OK is possible after TX_FAIL"

test_350() {
	reinit_dlc || return $?

	add_net ${NETTYPE} ${INTERFACES[0]} || return $?

	local my_nid=$($LCTL list_nids)

	cat <<EOF > $TMP/sanity-lnet-$testnum.yaml
ping:
  nids:
    0: $my_nid
EOF

	do_lnetctl import -e $TMP/sanity-lnet-$testnum.yaml ||
		error "Import failed with rc = $?"

	cat <<EOF > $TMP/sanity-lnet-$testnum.yaml
discover:
  nids:
    0: $my_nid
EOF

	do_lnetctl import -e $TMP/sanity-lnet-$testnum.yaml ||
		error "Import failed with rc = $?"

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
peer:
-     primary nid: $my_nid
      Multi-Rail: true
      peer ni:
      -     nid: $my_nid
            state: NA
EOF

	$LNETCTL peer show > $TMP/sanity-lnet-$testnum-actual.yaml ||
		error "export failed with rc = $?"

	compare_yaml_files
}
run_test 350 "Check import --exec of ping/discover"

check_udsp_prio() {
	local target_net="${1}"
	local target_nid="${2}"
	local expect_net="${3}"
	local expect_nid="${4}"
	local type="${5}"

	declare -a nids
	declare -a net_prios
	declare -a nid_prios

	nids=( $($LNETCTL ${type} show -v 5 | awk '/-\s+nid:/{print $NF}' |
		 xargs echo) )

	net_prios=( $($LNETCTL ${type} show -v 5 |
		      awk '/net priority:/{print $NF}' | xargs echo) )

	nid_prios=( $($LNETCTL ${type} show -v 5 |
		      awk '/nid priority:/{print $NF}' | xargs echo) )

	(( ${#nids[@]} != ${#net_prios[@]} )) &&
		error "Wrong # net prios ${#nids[@]} != ${#net_prios[@]}"

	(( ${#nids[@]} != ${#nid_prios[@]} )) &&
		error "Wrong # nid prios ${#nids[@]} != ${#nid_prios[@]}"

	local i

	for ((i = 0; i < ${#nids[@]}; i++)); do
		[[ -n ${target_net} ]] &&
			[[ ${nids[i]##*@} != "${target_net}" ]] &&
			continue
		[[ -n ${target_nid} ]] &&
			[[ ${nids[i]} != "${target_nid}" ]] &&
			continue

		echo "${nids[i]}: net_prio ${net_prios[i]} expect ${expect_net}"
		(( net_prios[i] != expect_net )) &&
			error "Wrong net priority \"${net_prios[i]}\" expect ${expect_net}"

		echo "${nids[i]}: nid_prio ${nid_prios[i]} expect ${expect_nid}"
		(( nid_prios[i] != expect_nid )) &&
			error "Wrong nid priority \"${nid_prios[i]}\" expect ${expect_nid}"
	done

	return 0
}

check_peer_udsp_prio() {
	check_udsp_prio "${1}" "${2}" "${3}" "${4}" "peer"
}

check_net_udsp_prio() {
	check_udsp_prio "${1}" "${2}" "${3}" "${4}" "net"
}

test_400() {
	reinit_dlc || return $?

	do_lnetctl udsp add --src tcp --priority 0 ||
		error "Failed to add udsp rule"
	do_lnetctl udsp del --idx 0 ||
		error "Failed to del udsp rule"
	unload_modules
}
run_test 400 "Check for udsp add/delete net rule without net num"

test_401() {
	reinit_dlc || return $?

	do_lnetctl net add --net ${NETTYPE} --if ${INTERFACES[0]} ||
		error "Failed to add net"

	do_lnetctl udsp add --dst ${NETTYPE} --prio 1 ||
		error "Failed to add peer net priority rule"

	do_lnetctl discover $($LCTL list_nids | head -n 1) ||
		error "Failed to discover peer"

	check_peer_udsp_prio "${NETTYPE}" "" "1" "-1"

	return 0
}
run_test 401 "Discover peer after adding peer net UDSP rule"

test_402() {
	reinit_dlc || return $?

	do_lnetctl udsp add --dst kfi --priority 0 ||
		error "Failed to add UDSP rule"

	do_lnetctl peer add --prim 402@kfi ||
		error "Failed to add peer"

	return 0
}
run_test 402 "Destination net rule should not panic"

test_403() {
	local rule_type

	for rule_type in src dst; do
		reinit_dlc || return $?

		local nid

		if [[ ${NETTYPE} == kfi* ]] || [[ ${NETTYPE} == gni* ]]; then
			nid=401@kfi
		else
			nid=1.1.1.1@${NETTYPE}
		fi

		for arg in ${NETTYPE} ${nid}; do
			cat <<EOF > $TMP/$TESTSUITE-$testnum-expected.yaml
udsp:
- idx: 0
  ${rule_type}: ${arg}
  action:
    priority: 1
EOF

			do_lnetctl udsp add --${rule_type} ${arg} --prio 1 ||
				error "udsp add failed rc=$?"

			$LNETCTL udsp show > $TMP/$TESTSUITE-$testnum-actual.yaml

			compare_yaml_files || return $?

			do_lnetctl udsp del --idx 0 ||
				error "udsp del failed rc=$?"

			do_lnetctl udsp show

			[[ -z $($LNETCTL udsp show) ]] ||
				error "Unexpected udsp show output"
		done
	done

	return 0
}
run_test 403 "Add and delete udsp net and nid rules"

check_udsp_priorities() {
	local rule_type="$1"

	if [[ $rule_type == both ]]; then
		check_net_udsp_prio "$net" "$nid" "1" "-1"
		check_peer_udsp_prio "$net" "$nid" "1" "-1"
	elif [[ ${rule_type} == src ]]; then
		check_net_udsp_prio "$net" "$nid" "1" "-1"
	else
		check_peer_udsp_prio "$net" "$nid" "1" "-1"
	fi
}

do_udsp_test() {
	local rule_type="$1"
	local net="$2"
	local nid="$3"

	local arg arg_type

	if [[ -n $net ]]; then
		arg=$net
		arg_type="net"
	else
		arg=$nid
		arg_type="nid"
	fi

	[[ -n $net ]] && arg=$net || arg=$nid

	if [[ $rule_type == both ]]; then
		do_lnetctl udsp add --src $arg --prio 1 ||
			error "Failed to add $rule_type $arg_type priority rule"
		do_lnetctl udsp add --dst $arg --prio 1 ||
			error "Failed to add $rule_type $arg_type priority rule"
	else
		do_lnetctl udsp add --$rule_type $arg --prio 1 ||
			error "Failed to add $rule_type $arg_type priority rule"
	fi

	do_lnetctl discover "$($LCTL list_nids | head -n 1)" ||
		error "failed to discover myself"

	check_udsp_priorities "$rule_type"

	$LNETCTL export --backup > $TMP/$TESTSUITE-$testnum-expected.yaml ||
		error "export failed"

	reinit_dlc || return $?

	$LNETCTL import $TMP/$TESTSUITE-$testnum-expected.yaml ||
		error "Failed to import from backup"

	$LNETCTL export --backup > $TMP/$TESTSUITE-$testnum-actual.yaml ||
		error "export failed"

	compare_yaml_files || error "Unexpected config after import"

	do_lnetctl discover "$($LCTL list_nids | head -n 1)" ||
		error "failed to discover myself"

	check_udsp_priorities "$rule_type"
}

test_404() {
	local rule_type nid

	for rule_type in src dst both ; do
		reinit_dlc || return $?

		add_net "${NETTYPE}" "${INTERFACES[0]}"
		add_net "${NETTYPE}2" "${INTERFACES[0]}"

		nid=$($LCTL list_nids | head -n 1)

		do_udsp_test ${rule_type} "${NETTYPE}" "${nid}" || return $?

		reinit_dlc || return $?

		add_net "${NETTYPE}" "${INTERFACES[0]}"
		add_net "${NETTYPE}2" "${INTERFACES[0]}"

		nid=$($LCTL list_nids | tail -n 1)

		do_lnetctl discover "$($LCTL list_nids | head -n 1)" ||
			error "Failed to discover myself"

		do_udsp_test ${rule_type} "${NETTYPE}2" "${nid}" || return $?
	done

	return 0
}
run_test 404 "Check udsp src/dst net rule"

test_410() {
	reinit_dlc || return $?

	$LCTL mark "$LNETCTL fault reset"
	echo "$LNETCTL fault reset"
	$LNETCTL fault reset '' &&
		error "Command should fail"

	return 0
}
run_test 410 "No segfault in lnetctl fault command"

test_450() {
	[[ $NETTYPE == tcp* ]] || skip "Need tcp NETTYPE"

	reinit_dlc || return $?

	setup_fakeif || return $?

	add_net "$NETTYPE" "${INTERFACES[0]}" || return $?
	add_net "$NETTYPE" "$FAKE_IF" || return $?

	$LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml

	cat <<EOF > $TMP/sanity-lnet-$testnum.yaml
net:
    - net type: $NETTYPE
      local NI(s):
        - interfaces:
              0: ${INTERFACES[0]}
              1: ${FAKE_IF}
EOF

	do_import_test "$TMP/sanity-lnet-$testnum.yaml" || return $?

	do_lnetctl import --del $TMP/sanity-lnet-$testnum.yaml ||
		error "Import --del failed with rc = $?"

	[[ -z $($LCTL list_nids) ]] ||
		error "NIDs still configured \"$($LCTL list_nids)\""

	cleanup_fakeif
}
run_test 450 "Check import of multiple interfaces"

test_500() {
	reinit_dlc || return $?

	setup_fakeif || error "Failed to add fake IF"

	add_net "tcp" "${INTERFACES[0]}"
	add_net "tcp" "${FAKE_IF}"

	do_lnetctl discover $($LCTL list_nids | head -n 1) ||
		error "Failed to discover self"

	add_delay_rule -s *@tcp -d *@tcp -r 1 -l 1 -m PUT

	add_drop_rule -s *@tcp -d $($LCTL list_nids | head -n 1) -m PUT \
		-e local_timeout -r 1
	add_drop_rule -s *@tcp -d $($LCTL list_nids | tail -n 1) -m PUT \
		-e local_timeout -r 1

	ip link set $FAKE_IF down ||
		error "Failed to set link down"
	ip link set $FAKE_IF up ||
		error "Failed to set link up"

	unload_modules
}
run_test 500 "Check deadlock on ping target update"

do_peer_set_health_test() {
	local test_val="$1"
	shift
	local nid_arg="$@"

	local max_hval

	if [[ ${nid_arg} == --all ]]; then
		max_hval=$($LNETCTL peer show -v 2 2>/dev/null |
			   awk '/health value/{print $NF}' | xargs echo |
			   sed 's/ /+/g' | bc -l)
	else
		max_hval=$($LNETCTL peer show ${nid_arg} -v 2 2>/dev/null |
			   awk '/health value/{print $NF}' | xargs echo |
			   sed 's/ /+/g' | bc -l)
	fi

	(( max_hval >= 1000 )) && (( max_hval % 1000 == 0)) ||
		error "Unexpected max health value $max_hval"

	lnet_health_pre || return $?

	do_lnetctl peer set --health $test_val ${nid_arg} ||
		error "failed to set health value"

	lnet_health_post || return $?

	local hval

	if [[ ${nid_arg} == --all ]]; then
		hval=$($LNETCTL peer show -v 2 2>/dev/null |
		       awk '/health value/{print $NF}' | xargs echo |
		       sed 's/ /+/g' | bc -l)
	else
		hval=$($LNETCTL peer show ${nid_arg} -v 2 2>/dev/null |
		       awk '/health value/{print $NF}' | xargs echo |
		       sed 's/ /+/g' | bc -l)
	fi

	check_remote_health || return $?

	echo "hval: $hval max_hval: $max_hval"
	(( hval == max_hval )) || error "Failed to reset health to max"

	return 0
}

test_501() {
	reinit_dlc || return $?

	setup_health_test false || return $?

	$LCTL set_param debug=-1

	add_drop_rule -s *@tcp -d *@tcp -r 1

	local test_val

	for test_val in 0 500; do
		do_peer_set_health_test $test_val --nid ${RNIDS[0]} || return $?
		do_peer_set_health_test $test_val --all || return $?
	done

	cleanup_health_test
}
run_test 501 "Verify lnetctl peer set --health (SR)"

test_502() {
	reinit_dlc || return $?

	setup_health_test true || return $?

	$LCTL set_param debug=-1

	add_drop_rule -s *@tcp -d *@tcp -r 1

	local test_val

	for test_val in 0 500; do
		do_peer_set_health_test $test_val --nid ${RNIDS[0]} || return $?
		do_peer_set_health_test $test_val --all || return $?
	done

	cleanup_health_test
}
run_test 502 "Verify lnetctl peer set --health (MR)"

test_550() {
	[[ ${NETTYPE} == tcp* ]] ||
		skip "Need tcp NETTYPE to fail a single local rail"

	(( ${#INTERFACES[@]} >= 2 )) ||
		skip "Need at least 2 local interfaces for multi-rail"

	setup_router_test lnet_peer_discovery_disabled=1 || return $?

	# Add a second local NI on the local net so the gateway is reachable
	# via two local rails (multi-rail).
	add_net $LOCAL_NET ${INTERFACES[1]} || return $?
	LNIDS[1]=$($LCTL list_nids | tail -1)

	# Configure this peer as MR, all others stay non-MR
	local all_nodes=$(comma_list ${ROUTERS[@]} ${RPEERS[@]} $HOSTNAME)
	do_nodes $all_nodes "$LNETCTL peer add --prim ${LNIDS[0]} --nid ${LNIDS[1]}" ||
		error "Failed to add peer rc=$?"

	config_routes || error "Failed to configure routes rc=$?"

	local rpeer=${RPEERS[0]}
	local rpeer_nids=( ${RPEER_NIDS[$rpeer]} )
	local i

	# Ping via the secondary NI so path selection favors it for rpeer
	local snid=$($LCTL list_nids | tail -1)
	do_lnetctl ping --source $snid ${rpeer_nids[0]} ||
		error "Failed to ping from $snid to ${rpeer_nids[0]}"

	check_route_aliveness "$HOSTNAME" "up" || return $?

	# Drop a marker on the console so we can scope the search for a route
	# up->down transition to events that happen after we fail the rail.
	# lnet_set_route_aliveness() logs such transitions via CERROR, which
	# reaches the console
	local marker="DDN-6845-${testnum}-${RANDOM}"
	log "$marker"

	echo "Fail a send via ${INTERFACES[1]}"
	$LCTL net_drop_add -s ${LNIDS[1]} -d "*@$REMOTE_NET" -e local_error -r 1 ||
		error "Failed to add $REMOTE_NET drop rule rc=$?"
	$LCTL net_drop_add -s ${LNIDS[1]} -d "*@$LOCAL_NET" -e local_error -r 1 ||
		error "Failed to add $LOCAL_NET drop rule rc=$?"

	echo "Set fail_loc to allow disconnect on simulated error"

	#define CFS_FAIL_SOCK_CONN              0xe020
	$LCTL set_param fail_loc=0x8000e020
	! do_lnetctl ping ${rpeer_nids[0]} ||
		error "Ping should have failed"

	check_route_aliveness "$HOSTNAME" "down" true &&
		error "Route went down after single local-rail failure"

	local downtrans
	downtrans=$( dmesg | sed -n "/$marker/,\$p" |
		     grep -c "has gone from up to down")
	((downtrans == 0)) ||
		error "route marked down on single local-rail failure $downtrans up->down transition(s)"

	check_route_aliveness "$HOSTNAME" "up" ||
		error "route down after single local-rail failure"

	# Regression guard: confirm the route goes down when the gateway is
	# unreachable from all local NIs, not just the failed one.
	do_node ${ROUTERS[0]} "$LNETCTL lnet unconfigure" ||
		error "Failed to unconfigure lnet on ${ROUTERS[0]}"

	check_route_aliveness "$HOSTNAME" "down" ||
		error "route didn't go down"

	cleanup_router_test
}
run_test 550 "DD-off: keep route up when gw reachable via another local NI"

test_600() {
	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
ip2nets:
    - net-spec: tcp0
      interfaces:
          0: eth0
          1: eth1
      ip-range:
          0: 10.0.0.*
    - net-spec: o2ib0
      ip-range:
          0: 192.168.[1-2].*
EOF

	$LEGACY2YAML 'tcp0(eth0,eth1) 10.0.0.*; o2ib0 192.168.[1-2].*' \
		> $actual || error "lnet_legacy2yaml failed with rc=$?"

	compare_yaml_files || error "YAML comparison failed"
}
run_test 600 "lnet_legacy2yaml: basic ip2nets conversion"

test_601() {
	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
route:
    - net: tcp0
      gateway: 10.0.0.1@o2ib0
      hop: 1
EOF

	$LEGACY2YAML "routes='tcp0 1 10.0.0.1@o2ib0'" > $actual ||
		error "lnet_legacy2yaml failed with rc=$?"

	compare_yaml_files || error "YAML comparison failed"
}
run_test 601 "lnet_legacy2yaml: basic routes conversion"

test_602() {
	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
route:
    - net: tcp0
      gateway: 10.0.0.1@o2ib0
      hop: 2
      priority: 7
EOF

	$LEGACY2YAML "routes='tcp0 2 10.0.0.1@o2ib0:7'" > $actual ||
		error "lnet_legacy2yaml failed with rc=$?"

	compare_yaml_files || error "YAML comparison failed"
}
run_test 602 "lnet_legacy2yaml: routes with priority"

test_603() {
	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
route:
    - net: tcp0
      gateway: 10.0.0.1@o2ib0
      hop: 1
EOF

	$LEGACY2YAML "routes='tcp0 10.0.0.1@o2ib0'" --default-hop \
		> $actual || error "lnet_legacy2yaml failed with rc=$?"

	compare_yaml_files || error "YAML comparison failed"
}
run_test 603 "lnet_legacy2yaml: default hop flag"

test_604() {
	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
route:
    - net: tcp0
      gateway: 10.0.0.1@o2ib0
      hop: 3
EOF

	$LEGACY2YAML --default-hop 3 "routes='tcp0 10.0.0.1@o2ib0'" \
		> $actual || error "lnet_legacy2yaml failed with rc=$?"

	compare_yaml_files || error "YAML comparison failed"
}
run_test 604 "lnet_legacy2yaml: default hop with explicit value"

test_605() {
	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
ip2nets:
    - net-spec: tcp
      ip-range:
          0: "*.*.*.*"
EOF

	$LEGACY2YAML 'tcp *.*.*.*' > $actual ||
		error "lnet_legacy2yaml failed with rc=$?"

	compare_yaml_files || error "YAML comparison failed"
}
run_test 605 "lnet_legacy2yaml: quote leading star in IP pattern"

test_606() {
	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
	local ip_file="$TMP/sanity-lnet-$testnum-ip.rules"
	local routes_file="$TMP/sanity-lnet-$testnum-routes.rules"

	echo 'tcp0 10.1.0.*' > "$ip_file"
	echo 'tcp0 1 10.1.0.1@o2ib0:9' > "$routes_file"

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
ip2nets:
    - net-spec: tcp0
      ip-range:
          0: 10.1.0.*
route:
    - net: tcp0
      gateway: 10.1.0.1@o2ib0
      hop: 1
      priority: 9
EOF

	$LEGACY2YAML -i "$ip_file" -r "$routes_file" > $actual ||
		error "lnet_legacy2yaml failed with rc=$?"

	compare_yaml_files || error "YAML comparison failed"

	rm -f "$ip_file" "$routes_file"
}
run_test 606 "lnet_legacy2yaml: file inputs"

test_607() {
	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
	local opts

	opts="options lnet 'ip2nets=\"tcp0 10.0.0.*\""
	opts+=" routes=\"tcp0 1 10.0.0.2@o2ib0\"'"

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
ip2nets:
    - net-spec: tcp0
      ip-range:
          0: 10.0.0.*
route:
    - net: tcp0
      gateway: 10.0.0.2@o2ib0
      hop: 1
EOF

	$LEGACY2YAML "$opts" > $actual ||
		error "lnet_legacy2yaml failed with rc=$?"

	compare_yaml_files || error "YAML comparison failed"
}
run_test 607 \
	"lnet_legacy2yaml: options line with combined ip2nets and routes"

test_608() {
	local output

	output=$($LEGACY2YAML 'tcp0' 2>&1)
	local rc=$?

	((rc == 2)) || error "Expected exit code 2, got $rc"
	echo "$output" | grep -iq 'error' ||
		error "Expected error message in output"
}
run_test 608 "lnet_legacy2yaml: error on invalid rule (missing IP pattern)"

test_609() {
	local output

	# Run with no stdin and no args - expect error
	output=$(echo -n "" | $LEGACY2YAML 2>&1)
	local rc=$?

	((rc == 1)) || error "Expected exit code 1, got $rc"
	echo "$output" | grep -iq "no ip2nets, routes, or networks data" ||
		error "Expected 'no data' message in output"
}
run_test 609 "lnet_legacy2yaml: error on no input"

test_610() {
	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
	local ip1="$TMP/sanity-lnet-$testnum-ip1.rules"
	local ip2="$TMP/sanity-lnet-$testnum-ip2.rules"
	local r1="$TMP/sanity-lnet-$testnum-r1.rules"

	echo 'tcp0 10.0.0.*' > "$ip1"
	echo 'o2ib0 192.168.0.*' > "$ip2"
	echo 'tcp0 1 10.0.0.1@o2ib0' > "$r1"

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
ip2nets:
    - net-spec: tcp0
      ip-range:
          0: 10.0.0.*
    - net-spec: o2ib0
      ip-range:
          0: 192.168.0.*
route:
    - net: tcp0
      gateway: 10.0.0.1@o2ib0
      hop: 1
    - net: o2ib0
      gateway: 192.168.0.1@tcp0
      hop: 1
EOF

	$LEGACY2YAML -i "$ip1" -i "$ip2" -r "$r1" \
		"routes='o2ib0 1 192.168.0.1@tcp0'" > $actual ||
		error "lnet_legacy2yaml failed with rc=$?"

	compare_yaml_files || error "YAML comparison failed"

	rm -f "$ip1" "$ip2" "$r1"
}
run_test 610 \
	"lnet_legacy2yaml: multiple files and inline, check order"

test_611() {
	local output

	output=$($LEGACY2YAML "routes='tcp0 0 10.0.0.1@o2ib0'" 2>&1)
	local rc=$?

	((rc == 2)) || error "Expected exit code 2, got $rc"
	echo "$output" | grep -iq "hopcount" ||
		error "Expected hopcount error in output"
}
run_test 611 "lnet_legacy2yaml: invalid hopcount"

test_612() {
	local output

	output=$($LEGACY2YAML 'tcp0 10.0.0' 2>&1)
	local rc=$?

	((rc == 2)) || error "Expected exit code 2, got $rc"
	echo "$output" | grep -iq "invalid.*ip pattern" ||
		error "Expected invalid IP pattern error in output"
}
run_test 612 "lnet_legacy2yaml: malformed IP pattern"

test_613() {
	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
ip2nets:
    - net-spec: tcp0
      ip-range:
          0: 10.0.0.*
          1: 10.0.1.*
EOF

	$LEGACY2YAML 'tcp0 10.0.0.* 10.0.1.*' > $actual ||
		error "lnet_legacy2yaml failed with rc=$?"

	compare_yaml_files || error "YAML comparison failed"
}
run_test 613 \
	"lnet_legacy2yaml: multiple IP patterns in single rule"

test_614() {
	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
ip2nets:
    - net-spec: tcp0
      interfaces:
          0: eth0
          1: eth1
      ip-range:
          0: 10.0.0.*
          1: 10.0.1.*
EOF

	$LEGACY2YAML 'tcp0(eth0,eth1) 10.0.0.* 10.0.1.*' > $actual ||
		error "lnet_legacy2yaml failed with rc=$?"

	compare_yaml_files || error "YAML comparison failed"
}
run_test 614 "lnet_legacy2yaml: interfaces enumeration with multiple IP ranges"

test_615() {
	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
	local modprobe_file="$TMP/sanity-lnet-$testnum-modprobe.conf"

	cat <<EOF > "$modprobe_file"
# Modprobe configuration file for LNet
options lnet ip2nets="tcp0(eth0) 10.1.0.*"

# Routes configuration
options lnet routes="tcp0 1 10.1.0.1@o2ib0:9"

# Line continuation test
options lnet \\
  ip2nets="o2ib0 192.168.1.*"
EOF

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
ip2nets:
    - net-spec: tcp0
      interfaces:
          0: eth0
      ip-range:
          0: 10.1.0.*
    - net-spec: o2ib0
      ip-range:
          0: 192.168.1.*
route:
    - net: tcp0
      gateway: 10.1.0.1@o2ib0
      hop: 1
      priority: 9
EOF

	$LEGACY2YAML -m "$modprobe_file" > $actual ||
		error "lnet_legacy2yaml failed with rc=$?"

	compare_yaml_files || error "YAML comparison failed"

	rm -f "$modprobe_file"
}
run_test 615 "lnet_legacy2yaml: modprobe configuration file input"

test_616() {
	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
	local modprobe_file="$TMP/sanity-lnet-$testnum-modprobe.conf"
	local ip_file="$TMP/sanity-lnet-$testnum-ip.rules"

	cat <<EOF > "$modprobe_file"
options lnet ip2nets="tcp0 10.0.0.*"
options lnet routes="tcp0 1 10.0.0.1@o2ib0"
EOF

	echo 'o2ib0 192.168.0.*' > "$ip_file"

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
ip2nets:
    - net-spec: tcp0
      ip-range:
          0: 10.0.0.*
    - net-spec: o2ib0
      ip-range:
          0: 192.168.0.*
route:
    - net: tcp0
      gateway: 10.0.0.1@o2ib0
      hop: 1
EOF

	$LEGACY2YAML -m "$modprobe_file" -i "$ip_file" > $actual ||
		error "lnet_legacy2yaml failed with rc=$?"

	compare_yaml_files || error "YAML comparison failed"

	rm -f "$modprobe_file" "$ip_file"
}
run_test 616 \
	"lnet_legacy2yaml: combining modprobe file with explicit ip2nets file"

test_617() {
	reinit_dlc || return $?

	local modprobe_file="$TMP/sanity-lnet-$testnum-modprobe.conf"
	local yaml_file="$TMP/sanity-lnet-$testnum.yaml"
	local export_file="$TMP/sanity-lnet-$testnum-expected.yaml"
	local actual_file="$TMP/sanity-lnet-$testnum-actual.yaml"
	local if0_ip
	local ip_pattern

	# Get IP address of the first interface and create pattern
	if0_ip=$(ip -o -4 a s ${INTERFACES[0]} | awk '{print $4}' | \
		 sed 's/\/.*//' | head -n 1)
	[[ -n $if0_ip ]] || error "Cannot determine IP for ${INTERFACES[0]}"

	# Create IP pattern matching the network (e.g., 192.168.1.*)
	ip_pattern=$(awk -F. '{print $1"."$2"."$3".*"}' <<< "${if0_ip}")

	echo "Configuring network ${NETTYPE} on ${INTERFACES[0]}"
	add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?

	$LNETCTL export --backup > "$export_file" ||
		error "lnetctl export failed"

	echo "Generating modprobe configuration"
	cat <<EOF > "$modprobe_file"
options lnet ip2nets="${NETTYPE}(${INTERFACES[0]}) ${ip_pattern}"
EOF

	echo "Generated modprobe configuration:"
	cat "$modprobe_file"

	echo "Converting modprobe config to YAML"
	$LEGACY2YAML -m "$modprobe_file" > "$yaml_file" ||
		error "lnet_legacy2yaml failed with rc=$?"

	echo "Generated YAML configuration:"
	cat "$yaml_file"

	reinit_dlc || return $?

	do_lnetctl import < "$yaml_file" ||
		error "lnetctl import failed"

	$LNETCTL export --backup > "$actual_file" ||
		error "lnetctl export after import failed"

	compare_yaml_files ||
		error "Round-trip YAML comparison failed"

	rm -f "$modprobe_file" "$yaml_file" "$export_file" "$actual_file"
}
run_test 617 \
	"lnet_legacy2yaml: round-trip tunables from LNet to modprobe to YAML"

test_618() {
	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
route:
    - net: tcp7
      gateway: 10.3.3.[6-12]@tcp
      hop: 8
      priority: 20
EOF

	local cfg="tcp7: { gateway: 10.3.3.[6-12]@tcp, priority: 20, hop: 8 }"
	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"

	$LEGACY2YAML -r <(echo "$cfg") > $actual ||
		error "lnet_legacy2yaml failed with rc=$?"

	compare_yaml_files || error "YAML comparison failed"
}
run_test 618 "lnet_legacy2yaml: alternative route format"

test_619() {
	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
	local routes_file="$TMP/sanity-lnet-$testnum-routes.conf"

	cat <<EOF > "$routes_file"
# Alternative route format examples
tcp1: { gateway: 10.1.1.2@tcp0, priority: 3 }  # High priority route
# This is a comment line
tcp4: { gateway: 10.3.3.4@tcp }
tcp6: { gateway: 10.3.3.6@tcp, hop: 2, priority: 5 }
tcp7: { gateway: 10.3.3.[6-12]@tcp, priority: 20, hop: 8 }
EOF

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
route:
    - net: tcp1
      gateway: 10.1.1.2@tcp0
      priority: 3
    - net: tcp4
      gateway: 10.3.3.4@tcp
    - net: tcp6
      gateway: 10.3.3.6@tcp
      hop: 2
      priority: 5
    - net: tcp7
      gateway: 10.3.3.[6-12]@tcp
      hop: 8
      priority: 20
EOF

	$LEGACY2YAML -r "$routes_file" > $actual ||
		error "lnet_legacy2yaml failed with rc=$?"

	compare_yaml_files || error "YAML comparison failed"

	rm -f "$routes_file"
}
run_test 619 "lnet_legacy2yaml: multiple alternative format routes from file"

test_620() {
	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
	local routes_file="$TMP/sanity-lnet-$testnum-routes.conf"

	cat <<EOF > "$routes_file"
# Mixed route formats
tcp1: { gateway: 10.1.1.2@tcp0, priority: 3 }
tcp0 1 10.2.2.3@tcp1
tcp6: { gateway: 10.3.3.6@tcp, hop: 2, priority: 5 }
tcp2 2 10.4.4.5@tcp0:7
EOF

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
route:
    - net: tcp1
      gateway: 10.1.1.2@tcp0
      priority: 3
    - net: tcp0
      gateway: 10.2.2.3@tcp1
      hop: 1
    - net: tcp6
      gateway: 10.3.3.6@tcp
      hop: 2
      priority: 5
    - net: tcp2
      gateway: 10.4.4.5@tcp0
      hop: 2
      priority: 7
EOF

	$LEGACY2YAML -r "$routes_file" > $actual ||
		error "lnet_legacy2yaml failed with rc=$?"

	compare_yaml_files || error "YAML comparison failed"

	rm -f "$routes_file"
}
run_test 620 "lnet_legacy2yaml: mixed legacy and alternative route formats"

test_621() {
	local cfg="tcp0: { gateway: 10.0.0.1@o2ib0, hop: 0 }"
	local output

	output=$($LEGACY2YAML -r <(echo "$cfg") 2>&1)
	local rc=$?

	((rc == 2)) || error "Expected exit code 2, got $rc"
	echo "$output" | grep -iq "hopcount.*out of range" ||
		error "Expected hopcount out of range error in output"
}
run_test 621 "lnet_legacy2yaml: alternative format with invalid hopcount"

test_622() {
	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
	local cfg="tcp5: { gateway: 10.5.5.5@tcp0 }"

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
route:
    - net: tcp5
      gateway: 10.5.5.5@tcp0
      hop: 3
EOF

	$LEGACY2YAML -r <(echo "$cfg") --default-hop 3 > $actual ||
		error "lnet_legacy2yaml failed with rc=$?"

	compare_yaml_files || error "YAML comparison failed"
}
run_test 622 "lnet_legacy2yaml: alternative format with default-hop flag"

test_623() {
	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
net:
    - net type: tcp
EOF

	$LEGACY2YAML 'options lnet networks=tcp' > $actual ||
		error "lnet_legacy2yaml failed with rc=$?"

	compare_yaml_files || error "YAML comparison failed"
}
run_test 623 "lnet_legacy2yaml: basic networks conversion"

test_624() {
	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
net:
    - net type: tcp0
      local NI(s):
        - interfaces:
              0: eth0
EOF

	$LEGACY2YAML 'options lnet networks=tcp0(eth0)' > $actual ||
		error "lnet_legacy2yaml failed with rc=$?"

	compare_yaml_files || error "YAML comparison failed"
}
run_test 624 "lnet_legacy2yaml: networks with interfaces"

test_625() {
	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
net:
    - net type: tcp0
      local NI(s):
        - interfaces:
              0: eth0
    - net type: o2ib
      local NI(s):
        - interfaces:
              0: ib0
EOF

	$LEGACY2YAML 'options lnet networks=tcp0(eth0),o2ib(ib0)' > $actual ||
		error "lnet_legacy2yaml failed with rc=$?"

	compare_yaml_files || error "YAML comparison failed"
}
run_test 625 "lnet_legacy2yaml: multiple networks"

test_626() {
	local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
	local modprobe_file="$TMP/sanity-lnet-$testnum-modprobe.conf"

	cat <<EOF > "$modprobe_file"
options lnet networks=tcp0(eth0),o2ib(ib0)
options lnet ip2nets="tcp1(eth1) 192.168.1.*"
options lnet routes="tcp0 1 10.0.0.1@o2ib0"
EOF

	cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
net:
    - net type: tcp0
      local NI(s):
        - interfaces:
              0: eth0
    - net type: o2ib
      local NI(s):
        - interfaces:
              0: ib0
ip2nets:
    - net-spec: tcp1
      interfaces:
          0: eth1
      ip-range:
          0: 192.168.1.*
route:
    - net: tcp0
      gateway: 10.0.0.1@o2ib0
      hop: 1
EOF

	$LEGACY2YAML -m "$modprobe_file" > $actual ||
		error "lnet_legacy2yaml failed with rc=$?"

	compare_yaml_files || error "YAML comparison failed"

	rm -f "$modprobe_file"
}
run_test 626 "lnet_legacy2yaml: mixed networks, ip2nets, and routes"

test_627() {
	local output

	output=$($LEGACY2YAML 'options lnet networks=tcp0(eth0' 2>&1)
	local rc=$?

	((rc == 2)) || error "Expected exit code 2, got $rc"
	echo "$output" | grep -iq "Invalid network specification" ||
		error "Expected invalid network specification error in output"
}
run_test 627 "lnet_legacy2yaml: invalid networks format"

test_628() {
	local output

	output=$($LEGACY2YAML 'tcp0(eth0) 10.0.0.* tcp1(eth1) 192.168.1.*' 2>&1)
	local rc=$?

	((rc == 2)) || error "Expected exit code 2, got $rc"
	echo "$output" | grep -iq "Multiple network rules must be separated" ||
		error "Expected missing delimiter error in output"
}
run_test 628 "lnet_legacy2yaml: missing delimiter error"

test_629() {
	local output

	output=$($LEGACY2YAML 'options lnet routes="tcp0 1 10.0.0.1@o2ib0 10.0.0.2@o2ib0"' 2>&1)
	local rc=$?

	((rc == 2)) || error "Expected exit code 2, got $rc"
	echo "$output" | grep -iq "multiple gateway specifications found" ||
		error "Expected multiple gateway error in output"
}
run_test 629 "lnet_legacy2yaml: multiple gateways error"

complete_test $SECONDS
cleanup_testsuite
exit_status