Whamcloud - gitweb
LU-16859 lnet: incorrect check for duplicate NI
[fs/lustre-release.git] / lustre / tests / sanity-lnet.sh
1 #!/bin/bash
2 #
3 # Run select tests by setting ONLY, or as arguments to the script.
4 # Skip specific tests by setting EXCEPT.
5 #
6
7 set -e
8
9 ONLY=${ONLY:-"$*"}
10
11 # bug number for skipped test:
12 ALWAYS_EXCEPT="$SANITY_LNET_EXCEPT "
13 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
14
15 [ "$SLOW" = "no" ] && EXCEPT_SLOW=""
16
17 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
18
19 . $LUSTRE/tests/test-framework.sh
20 CLEANUP=${CLEANUP:-:}
21 SETUP=${SETUP:-:}
22 init_test_env "$@"
23 . ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
24 init_logging
25
26 build_test_filter
27
28 [[ -z $LNETCTL ]] && skip "Need lnetctl"
29
30 restore_mounts=false
31
32 if is_mounted $MOUNT || is_mounted $MOUNT2; then
33         cleanupall || error "Failed cleanup prior to test execution"
34         restore_mounts=true
35 fi
36
37 cleanup_lnet() {
38         echo "Cleaning up LNet"
39         lsmod | grep -q lnet &&
40                 $LNETCTL lnet unconfigure 2>/dev/null
41         unload_modules
42 }
43
44 restore_modules=false
45 if module_loaded lnet ; then
46         cleanup_lnet || error "Failed to unload modules before test execution"
47         restore_modules=true
48 fi
49
50 cleanup_testsuite() {
51         trap "" EXIT
52         # Cleanup any tmp files created by the sub tests
53         rm -f $TMP/sanity-lnet-*.yaml $LNET_PARAMS_FILE
54         cleanup_netns
55         cleanup_lnet
56         if $restore_mounts; then
57                 setupall || error "Failed to setup Lustre after test execution"
58         elif $restore_modules; then
59                 load_modules ||
60                         error "Couldn't load modules after test execution"
61         fi
62         return 0
63 }
64
65 TESTNS='test_ns'
66 FAKE_IF="test1pg"
67 FAKE_IP="10.1.2.3"
68 FAKE_IP_ALIAS="10.1.2.31"
69 do_ns() {
70         echo "ip netns exec $TESTNS $*"
71         ip netns exec $TESTNS "$@"
72 }
73
74 setup_fakeif() {
75         local netns="$1"
76
77         local netns_arg=""
78         [[ -n $netns ]] &&
79                 netns_arg="netns $netns"
80
81         ip link add 'test1pl' type veth peer name $FAKE_IF $netns_arg
82         ip link set 'test1pl' up
83         if [[ -n $netns ]]; then
84                 do_ns ip addr add "${FAKE_IP}/31" dev $FAKE_IF
85                 do_ns ip link set $FAKE_IF up
86         else
87                 ip addr add "${FAKE_IP}/31" dev $FAKE_IF
88                 ip link set $FAKE_IF up
89         fi
90 }
91
92 cleanup_fakeif() {
93         ip link show test1pl >& /dev/null && ip link del test1pl || return 0
94 }
95
96 setup_netns() {
97         cleanup_netns
98
99         ip netns add $TESTNS
100         setup_fakeif $TESTNS
101 }
102
103 cleanup_netns() {
104         (ip netns list | grep -q $TESTNS) && ip netns del $TESTNS
105         cleanup_fakeif
106 }
107
108 configure_dlc() {
109         echo "Loading LNet and configuring DLC"
110         load_lnet || return $?
111         do_lnetctl lnet configure
112 }
113
114 GLOBAL_YAML_FILE=$TMP/sanity-lnet-global.yaml
115 define_global_yaml() {
116         $LNETCTL export --backup >${GLOBAL_YAML_FILE} ||
117                 error "Failed to export global yaml $?"
118 }
119
120 reinit_dlc() {
121         if lsmod | grep -q lnet; then
122                 do_lnetctl lnet unconfigure ||
123                         error "lnetctl lnet unconfigure failed $?"
124                 do_lnetctl lnet configure ||
125                         error "lnetctl lnet configure failed $?"
126         else
127                 configure_dlc || error "configure_dlc failed $?"
128         fi
129         define_global_yaml
130 }
131
132 append_global_yaml() {
133         [[ ! -e ${GLOBAL_YAML_FILE} ]] &&
134                 error "Missing global yaml at ${GLOBAL_YAML_FILE}"
135
136         cat ${GLOBAL_YAML_FILE} >> $TMP/sanity-lnet-$testnum-expected.yaml
137 }
138
139 create_base_yaml_file() {
140         append_global_yaml
141 }
142
143 compare_yaml_files() {
144         local expected="$TMP/sanity-lnet-$testnum-expected.yaml"
145         local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
146         local rc=0
147         ! [[ -e $expected ]] && echo "$expected not found" && return 1
148         ! [[ -e $actual ]] && echo "$actual not found" && return 1
149         if [ verify_yaml_available ]; then
150                 verify_compare_yaml $actual $expected || rc=$?
151         else
152                 diff -upN ${actual} ${expected} || rc=$?
153         fi
154         echo "Expected:"
155         cat $expected
156         echo "Actual:"
157         cat $actual
158         return $rc
159 }
160
161 validate_nid() {
162         local nid="$1"
163         local net="${nid//*@/}"
164         local addr="${nid//@*/}"
165
166         local num_re='[0-9]+'
167         local ip_re="[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
168
169         if [[ $net =~ (gni|kfi)[0-9]* ]]; then
170                 [[ $addr =~ ${num_re} ]] && return 0
171         else
172                 [[ $addr =~ ${ip_re} ]] && return 0
173         fi
174 }
175
176 validate_nids() {
177         local yfile=$TMP/sanity-lnet-$testnum-actual.yaml
178         local primary_nids=$(awk '/- primary nid:/{print $NF}' $yfile | xargs echo)
179         local secondary_nids=$(awk '/- nid:/{print $NF}' $yfile | xargs echo)
180         local gateway_nids=$(awk '/gateway:/{print $NF}' $yfile | xargs echo)
181
182         local nid
183         for nid in $primary_nids $secondary_nids; do
184                 validate_nid "$nid" || error "Bad NID \"${nid}\""
185         done
186         return 0
187 }
188
189 validate_peer_nids() {
190         local num_peers="$1"
191         local nids_per_peer="$2"
192
193         local expect_p="$num_peers"
194         # The primary nid also shows up in the list of secondary nids
195         local expect_s="$(($num_peers + $(($nids_per_peer*$num_peers))))"
196
197         local actual_p=$(grep -c -- '- primary nid:' $TMP/sanity-lnet-$testnum-actual.yaml)
198         local actual_s=$(grep -c -- '- nid:' $TMP/sanity-lnet-$testnum-actual.yaml)
199         if [[ $expect_p -ne $actual_p ]]; then
200                 compare_yaml_files
201                 error "Expected $expect_p but found $actual_p primary nids"
202         elif [[ $expect_s -ne $actual_s ]]; then
203                 compare_yaml_files
204                 error "Expected $expect_s but found $actual_s secondary nids"
205         fi
206         validate_nids
207 }
208
209 validate_gateway_nids() {
210         local expect_gw=$(grep -c -- 'gateway:' $TMP/sanity-lnet-$testnum-expected.yaml)
211         local actual_gw=$(grep -c -- 'gateway:' $TMP/sanity-lnet-$testnum-actual.yaml)
212         if [[ $expect_gw -ne $actual_gw ]]; then
213                 compare_yaml_files
214                 error "Expected $expect_gw gateways but found $actual_gw gateways"
215         fi
216
217         local expect_gwnids=$(awk '/gateway:/{print $NF}' $TMP/sanity-lnet-$testnum-expected.yaml |
218                               xargs echo)
219         local nid
220         for nid in ${expect_gwnids}; do
221                 if ! grep -q "gateway: ${nid}" $TMP/sanity-lnet-$testnum-actual.yaml; then
222                         error "${nid} not configured as gateway"
223                 fi
224         done
225
226         validate_nids
227 }
228
229 cleanupall -f
230 setup_netns || error "setup_netns failed with $?"
231
232 # Determine the local interface(s) used for LNet
233 load_lnet "config_on_load=1" || error "Failed to load modules"
234
235 do_lnetctl net show
236 ip a
237
238 INTERFACES=( $(lnet_if_list) )
239
240 cleanup_lnet || error "Failed to cleanup LNet"
241
242 stack_trap 'cleanup_testsuite' EXIT
243
244 test_0() {
245         configure_dlc || error "Failed to configure DLC rc = $?"
246         define_global_yaml
247         reinit_dlc || return $?
248         do_lnetctl import <  ${GLOBAL_YAML_FILE} || error "Import failed $?"
249         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
250         create_base_yaml_file
251         compare_yaml_files || error "Configuration changed after import"
252 }
253 run_test 0 "Export empty config, import the config, compare"
254
255 compare_peer_add() {
256         local prim_nid="${1:+--prim_nid $1}"
257         local nid="${2:+--nid $2}"
258
259         local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
260
261         do_lnetctl peer add ${prim_nid} ${nid} || error "peer add failed $?"
262         $LNETCTL export --backup > $actual || error "export failed $?"
263         compare_yaml_files
264         return $?
265 }
266
267 test_1() {
268         reinit_dlc || return $?
269         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
270 peer:
271     - primary nid: 1.1.1.1@tcp
272       Multi-Rail: True
273       peer ni:
274         - nid: 1.1.1.1@tcp
275 EOF
276         append_global_yaml
277         compare_peer_add "1.1.1.1@tcp"
278 }
279 run_test 1 "Add peer with single nid (tcp)"
280
281 test_2() {
282         reinit_dlc || return $?
283         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
284 peer:
285     - primary nid: 2.2.2.2@o2ib
286       Multi-Rail: True
287       peer ni:
288         - nid: 2.2.2.2@o2ib
289 EOF
290         append_global_yaml
291         compare_peer_add "2.2.2.2@o2ib"
292 }
293 run_test 2 "Add peer with single nid (o2ib)"
294
295 test_3() {
296         reinit_dlc || return $?
297         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
298 peer:
299     - primary nid: 3.3.3.3@tcp
300       Multi-Rail: True
301       peer ni:
302         - nid: 3.3.3.3@tcp
303         - nid: 3.3.3.3@o2ib
304 EOF
305         append_global_yaml
306         compare_peer_add "3.3.3.3@tcp" "3.3.3.3@o2ib"
307 }
308 run_test 3 "Add peer with tcp primary o2ib secondary"
309
310 test_4() {
311         reinit_dlc || return $?
312         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
313 peer:
314     - primary nid: 4.4.4.4@tcp
315       Multi-Rail: True
316       peer ni:
317         - nid: 4.4.4.4@tcp
318         - nid: 4.4.4.1@tcp
319         - nid: 4.4.4.2@tcp
320         - nid: 4.4.4.3@tcp
321 EOF
322         append_global_yaml
323         echo "Add peer with nidrange (tcp)"
324         compare_peer_add "4.4.4.4@tcp" "4.4.4.[1-3]@tcp"
325
326         echo "Add peer with nidrange that overlaps primary nid (tcp)"
327         compare_peer_add "4.4.4.4@tcp" "4.4.4.[1-4]@tcp"
328 }
329 run_test 4 "Add peer with nidrange (tcp)"
330
331 test_5() {
332         reinit_dlc || return $?
333         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
334 peer:
335     - primary nid: 5.5.5.5@o2ib
336       Multi-Rail: True
337       peer ni:
338         - nid: 5.5.5.5@o2ib
339         - nid: 5.5.5.1@o2ib
340         - nid: 5.5.5.2@o2ib
341         - nid: 5.5.5.3@o2ib
342         - nid: 5.5.5.4@o2ib
343 EOF
344         append_global_yaml
345         echo "Add peer with nidrange (o2ib)"
346         compare_peer_add "5.5.5.5@o2ib" "5.5.5.[1-4]@o2ib"
347
348         echo "Add peer with nidranage that overlaps primary nid (o2ib)"
349         compare_peer_add "5.5.5.5@o2ib" "5.5.5.[1-4]@o2ib"
350
351         echo "Add peer with nidranage that contain , plus primary nid (o2ib)"
352         compare_peer_add "5.5.5.5@o2ib" "5.5.5.[1,2,3-4]@o2ib"
353 }
354 run_test 5 "Add peer with nidrange (o2ib)"
355
356 test_6() {
357         reinit_dlc || return $?
358         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
359 peer:
360     - primary nid: 6.6.6.6@tcp
361       Multi-Rail: True
362       peer ni:
363         - nid: 6.6.6.6@tcp
364         - nid: 6.6.6.0@tcp
365         - nid: 6.6.6.2@tcp
366         - nid: 6.6.6.4@tcp
367         - nid: 6.6.7.0@tcp
368         - nid: 6.6.7.2@tcp
369         - nid: 6.6.7.4@tcp
370         - nid: 6.6.1.0@o2ib
371         - nid: 6.6.1.3@o2ib
372         - nid: 6.6.1.6@o2ib
373         - nid: 6.6.3.0@o2ib
374         - nid: 6.6.3.3@o2ib
375         - nid: 6.6.3.6@o2ib
376         - nid: 6@gni
377         - nid: 10@gni
378         - nid: 6@kfi
379         - nid: 10@kfi
380 EOF
381         append_global_yaml
382
383         local nid_expr="6.6.[6-7].[0-4/2]@tcp"
384         nid_expr+=",6.6.[1-4/2].[0-6/3]@o2ib"
385         nid_expr+=",[6-12/4]@gni"
386         nid_expr+=",[6-12/4]@kfi"
387
388         compare_peer_add "6.6.6.6@tcp" "${nid_expr}"
389 }
390 run_test 6 "Add peer with multiple nidranges"
391
392 compare_peer_del() {
393         local prim_nid="${1:+--prim_nid $1}"
394         local nid="${2:+--nid $2}"
395
396         local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
397
398         do_lnetctl peer del ${prim_nid} ${nid} || error "peer del failed $?"
399         $LNETCTL export --backup > $actual || error "export failed $?"
400         compare_yaml_files
401         return $?
402 }
403
404 test_7() {
405         reinit_dlc || return $?
406         create_base_yaml_file
407
408         echo "Delete peer with single nid (tcp)"
409         do_lnetctl peer add --prim_nid 7.7.7.7@tcp || error "Peer add failed $?"
410         compare_peer_del "7.7.7.7@tcp"
411
412         echo "Delete peer with single nid (o2ib)"
413         do_lnetctl peer add --prim_nid 7.7.7.7@o2ib || error "Peer add failed $?"
414         compare_peer_del "7.7.7.7@o2ib"
415
416         echo "Delete peer that has multiple nids (tcp)"
417         do_lnetctl peer add --prim_nid 7.7.7.7@tcp --nid 7.7.7.[8-12]@tcp ||
418                 error "Peer add failed $?"
419         compare_peer_del "7.7.7.7@tcp"
420
421         echo "Delete peer that has multiple nids (o2ib)"
422         do_lnetctl peer add --prim_nid 7.7.7.7@o2ib --nid 7.7.7.[8-12]@o2ib ||
423                 error "Peer add failed $?"
424         compare_peer_del "7.7.7.7@o2ib"
425
426         echo "Delete peer that has both tcp and o2ib nids"
427         do_lnetctl peer add --prim_nid 7.7.7.7@tcp \
428                 --nid 7.7.7.[9-12]@tcp,7.7.7.[13-15]@o2ib ||
429                 error "Peer add failed $?"
430         compare_peer_del "7.7.7.7@tcp"
431
432         echo "Delete peer with single nid (gni)"
433         do_lnetctl peer add --prim_nid 7@gni || error "Peer add failed $?"
434         compare_peer_del "7@gni"
435
436         echo "Delete peer that has multiple nids (gni)"
437         do_lnetctl peer add --prim_nid 7@gni --nid [8-12]@gni ||
438                 error "Peer add failed $?"
439         compare_peer_del "7@gni"
440
441         echo "Delete peer with single nid (kfi)"
442         do_lnetctl peer add --prim_nid 7@kfi || error "Peer add failed $?"
443         compare_peer_del "7@kfi"
444
445         echo "Delete peer that has multiple nids (kfi)"
446         do_lnetctl peer add --prim_nid 7@kfi --nid [8-12]@kfi ||
447                 error "Peer add failed $?"
448         compare_peer_del "7@kfi"
449
450         echo "Delete peer that has tcp, o2ib, gni and kfi nids"
451         do_lnetctl peer add --prim_nid 7@gni \
452                 --nid [8-12]@gni,7.7.7.[1-4]@tcp,7.7.7.[5-9]@o2ib,[1-5]@kfi ||
453                 error "Peer add failed $?"
454         compare_peer_del "7@gni"
455
456         echo "Delete peer that contains nid equal to primary nid"
457         do_lnetctl peer add --prim 1.1.1.1@tcp \
458                 --nid 1.1.1.1@tcp,2.2.2.2@tcp,3.3.3.3@tcp,4.4.4.4@tcp ||
459                 error "Peer add failed $?"
460         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"
461 }
462 run_test 7 "Various peer delete tests"
463
464 test_8() {
465         reinit_dlc || return $?
466
467         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
468 peer:
469     - primary nid: 8.8.8.8@tcp
470       Multi-Rail: True
471       peer ni:
472         - nid: 8.8.8.8@tcp
473         - nid: 8.8.8.10@tcp
474         - nid: 8.8.8.11@tcp
475         - nid: 8.8.8.12@tcp
476         - nid: 8.8.8.14@tcp
477         - nid: 8.8.8.15@tcp
478 EOF
479         append_global_yaml
480
481         do_lnetctl peer add --prim_nid 8.8.8.8@tcp --nid 8.8.8.[10-15]@tcp ||
482                 error "Peer add failed $?"
483         compare_peer_del "8.8.8.8@tcp" "8.8.8.13@tcp"
484 }
485 run_test 8 "Delete single secondary nid from peer (tcp)"
486
487 test_9() {
488         reinit_dlc || return $?
489
490         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
491 peer:
492     - primary nid: 9.9.9.9@tcp
493       Multi-Rail: True
494       peer ni:
495         - nid: 9.9.9.9@tcp
496 EOF
497         append_global_yaml
498
499         do_lnetctl peer add --prim_nid 9.9.9.9@tcp \
500                 --nid 9.9.9.[11-16]@tcp || error "Peer add failed $?"
501         compare_peer_del "9.9.9.9@tcp" "9.9.9.[11-16]@tcp"
502 }
503 run_test 9 "Delete all secondary nids from peer (tcp)"
504
505 test_10() {
506         reinit_dlc || return $?
507
508         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
509 peer:
510     - primary nid: 10.10.10.10@tcp
511       Multi-Rail: True
512       peer ni:
513         - nid: 10.10.10.10@tcp
514         - nid: 10.10.10.12@tcp
515         - nid: 10.10.10.13@tcp
516         - nid: 10.10.10.15@tcp
517         - nid: 10.10.10.16@tcp
518 EOF
519         append_global_yaml
520         do_lnetctl peer add --prim_nid 10.10.10.10@tcp \
521                 --nid 10.10.10.[12-16]@tcp || error "Peer add failed $?"
522         compare_peer_del "10.10.10.10@tcp" "10.10.10.14@tcp"
523 }
524 run_test 10 "Delete single secondary nid from peer (o2ib)"
525
526 test_11() {
527         reinit_dlc || return $?
528
529         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
530 peer:
531     - primary nid: 11.11.11.11@tcp
532       Multi-Rail: True
533       peer ni:
534         - nid: 11.11.11.11@tcp
535 EOF
536         append_global_yaml
537         do_lnetctl peer add --prim_nid 11.11.11.11@tcp \
538                 --nid 11.11.11.[13-17]@tcp || error "Peer add failed $?"
539         compare_peer_del "11.11.11.11@tcp" "11.11.11.[13-17]@tcp"
540 }
541 run_test 11 "Delete all secondary nids from peer (o2ib)"
542
543 test_12() {
544         reinit_dlc || return $?
545
546         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
547 peer:
548     - primary nid: 12.12.12.12@o2ib
549       Multi-Rail: True
550       peer ni:
551         - nid: 12.12.12.12@o2ib
552         - nid: 13.13.13.13@o2ib
553         - nid: 14.13.13.13@o2ib
554         - nid: 14.15.13.13@o2ib
555         - nid: 15.17.1.5@tcp
556         - nid: 15.17.1.10@tcp
557         - nid: 15.17.1.20@tcp
558 EOF
559         append_global_yaml
560         do_lnetctl peer add --prim_nid 12.12.12.12@o2ib \
561                 --nid [13-14/1].[13-15/2].13.13@o2ib,[15-16/3].[17-19/4].[1].[5-20/5]@tcp ||
562                 error "Peer add failed $?"
563         compare_peer_del "12.12.12.12@o2ib" "13.15.13.13@o2ib,15.17.1.15@tcp"
564 }
565 run_test 12 "Delete a secondary nid from peer (tcp and o2ib)"
566
567 test_13() {
568         reinit_dlc || return $?
569
570         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
571 peer:
572     - primary nid: 13.13.13.13@o2ib
573       Multi-Rail: True
574       peer ni:
575         - nid: 13.13.13.13@o2ib
576 EOF
577         append_global_yaml
578         do_lnetctl peer add --prim_nid 13.13.13.13@o2ib \
579                 --nid [14-15].[1-2/1].[1].[100-254/10]@tcp,14.14.[254].14@o2ib ||
580                 error "Peer add failed $?"
581         compare_peer_del "13.13.13.13@o2ib" \
582                 "[14-15].[1-2/1].[1].[100-254/10]@tcp,14.14.[254].14@o2ib"
583 }
584 run_test 13 "Delete all secondary nids from peer (tcp and o2ib)"
585
586 create_nid() {
587         local num="$1"
588         local net="$2"
589
590         if [[ $net =~ gni* ]] || [[ $net =~ kfi* ]]; then
591                 echo "${num}@${net}"
592         else
593                 echo "${num}.${num}.${num}.${num}@${net}"
594         fi
595 }
596
597 create_mr_peer_yaml() {
598         local num_peers="$1"
599         local secondary_nids="$2"
600         local net="$3"
601
602         echo "Generating peer yaml for $num_peers peers with $secondary_nids secondary nids"
603         echo "peer:" >> $TMP/sanity-lnet-$testnum-expected.yaml
604         local i
605         local total_nids=$((num_peers + $((num_peers * secondary_nids))))
606         local created=0
607         local nidnum=1
608         while [[ $created -lt $num_peers ]]; do
609                 local primary=$(create_nid ${nidnum} ${net})
610         cat <<EOF >> $TMP/sanity-lnet-$testnum-expected.yaml
611     - primary nid: $primary
612       Multi-Rail: True
613       peer ni:
614         - nid: $primary
615 EOF
616                 local j
617                 local start=$((nidnum + 1))
618                 local end=$((nidnum + $secondary_nids))
619                 for j in $(seq ${start} ${end}); do
620                         local nid=$(create_nid $j ${net})
621                         echo "        - nid: $nid" >> $TMP/sanity-lnet-$testnum-expected.yaml
622                 done
623                 nidnum=$((end + 1))
624                 ((created++))
625         done
626 }
627
628 test_14() {
629         reinit_dlc || return $?
630
631         echo "Create single peer, single nid, using import"
632         create_mr_peer_yaml 1 0 tcp
633         do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
634                 error "Import failed $?"
635         append_global_yaml
636         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
637         compare_yaml_files
638
639         echo "Delete single peer using import --del"
640         do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml ||
641                 error "Import failed $?"
642         rm -f $TMP/sanity-lnet-$testnum-expected.yaml
643         create_base_yaml_file
644         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
645         compare_yaml_files
646 }
647 run_test 14 "import peer create/delete with single nid"
648
649 test_15() {
650         reinit_dlc || return $?
651
652         echo "Create multiple peers, single nid per peer, using import"
653         create_mr_peer_yaml 5 0 o2ib
654         # The ordering of nids for this use-case is non-deterministic, so we
655         # we can't just diff the expected/actual output.
656         do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
657                 error "Import failed $?"
658         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
659         validate_peer_nids 5 0
660
661         echo "Delete multiple peers, single nid per peer, using import --del"
662         do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml ||
663                 error "Import failed $?"
664         rm -f $TMP/sanity-lnet-$testnum-expected.yaml
665         create_base_yaml_file
666         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
667         compare_yaml_files
668 }
669 run_test 15 "import multi peer create/delete with single nid per peer"
670
671 test_16() {
672         reinit_dlc || return $?
673
674         echo "Create single peer, multiple nids, using import"
675         create_mr_peer_yaml 1 5 tcp
676         do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
677                 error "Import failed $?"
678         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
679         validate_peer_nids 1 5
680
681         echo "Delete single peer, multiple nids, using import --del"
682         do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml ||
683                 error "Import failed $?"
684         rm -f $TMP/sanity-lnet-$testnum-expected.yaml
685         create_base_yaml_file
686         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
687         compare_yaml_files
688 }
689 run_test 16 "import peer create/delete with multiple nids"
690
691 test_17() {
692         reinit_dlc || return $?
693
694         echo "Create multiple peers, multiple nids per peer, using import"
695         create_mr_peer_yaml 5 7 o2ib
696         do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
697                 error "Import failed $?"
698         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
699         validate_peer_nids 5 7
700
701         echo "Delete multiple peers, multiple nids per peer, using import --del"
702         do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml ||
703                 error "Import failed $?"
704         rm -f $TMP/sanity-lnet-$testnum-expected.yaml
705         create_base_yaml_file
706         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
707         compare_yaml_files
708 }
709 run_test 17 "import multi peer create/delete with multiple nids"
710
711 test_18a() {
712         reinit_dlc || return $?
713
714         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
715 peer:
716     - primary nid: 1.1.1.1@tcp
717       Multi-Rail: True
718       peer ni:
719         - nid: 1.1.1.1@tcp
720         - nid: 2.2.2.2@tcp
721         - nid: 4.4.4.4@tcp
722         - nid: 3.3.3.3@o2ib
723         - nid: 5@gni
724 EOF
725         echo "Import peer with 5 nids"
726         cat $TMP/sanity-lnet-$testnum-expected.yaml
727         do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
728                 error "Import failed $?"
729         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
730 peer:
731     - primary nid: 1.1.1.1@tcp
732       Multi-Rail: True
733       peer ni:
734         - nid: 2.2.2.2@tcp
735         - nid: 3.3.3.3@o2ib
736         - nid: 5@gni
737 EOF
738         echo "Delete three of the nids"
739         cat $TMP/sanity-lnet-$testnum-expected.yaml
740         do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml
741         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
742 peer:
743     - primary nid: 1.1.1.1@tcp
744       Multi-Rail: True
745       peer ni:
746         - nid: 1.1.1.1@tcp
747         - nid: 4.4.4.4@tcp
748 EOF
749         echo "Check peer has expected nids remaining"
750         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
751         append_global_yaml
752         compare_yaml_files
753 }
754 run_test 18a "Delete a subset of nids from a single peer using import --del"
755
756 test_18b() {
757         reinit_dlc || return $?
758
759         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
760 peer:
761     - primary nid: 1.1.1.1@tcp
762       Multi-Rail: True
763       peer ni:
764         - nid: 1.1.1.1@tcp
765         - nid: 2.2.2.2@tcp
766         - nid: 4.4.4.4@tcp
767         - nid: 3.3.3.3@o2ib
768         - nid: 5@gni
769     - primary nid: 6.6.6.6@o2ib
770       Multi-Rail: True
771       peer ni:
772         - nid: 6.6.6.6@o2ib
773         - nid: 7.7.7.7@tcp
774         - nid: 8.8.8.8@tcp
775         - nid: 9.9.9.9@tcp
776         - nid: 10@gni
777 EOF
778         echo "Import two peers with 5 nids each"
779         cat $TMP/sanity-lnet-$testnum-expected.yaml
780         do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
781                 error "Import failed $?"
782         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
783 peer:
784     - primary nid: 1.1.1.1@tcp
785       Multi-Rail: True
786       peer ni:
787         - nid: 2.2.2.2@tcp
788         - nid: 3.3.3.3@o2ib
789         - nid: 5@gni
790     - primary nid: 6.6.6.6@o2ib
791       Multi-Rail: True
792       peer ni:
793         - nid: 7.7.7.7@tcp
794         - nid: 8.8.8.8@tcp
795         - nid: 10@gni
796 EOF
797         echo "Delete three of the nids from each peer"
798         cat $TMP/sanity-lnet-$testnum-expected.yaml
799         do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml
800         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
801 peer:
802     - primary nid: 6.6.6.6@o2ib
803       Multi-Rail: True
804       peer ni:
805         - nid: 6.6.6.6@o2ib
806         - nid: 7.7.7.7@tcp
807     - primary nid: 1.1.1.1@tcp
808       Multi-Rail: True
809       peer ni:
810         - nid: 1.1.1.1@tcp
811         - nid: 4.4.4.4@tcp
812 EOF
813         append_global_yaml
814         echo "Check peers have expected nids remaining"
815         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
816         compare_yaml_files
817         validate_peer_nids 2 1
818 }
819 run_test 18b "Delete multiple nids from multiple peers using import --del"
820
821 test_19() {
822         reinit_dlc || return $?
823         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
824 peer:
825     - primary nid: 19@gni
826       Multi-Rail: True
827       peer ni:
828         - nid: 19@gni
829 EOF
830         append_global_yaml
831         compare_peer_add "19@gni"
832 }
833 run_test 19 "Add peer with single nid (gni)"
834
835 test_20() {
836         reinit_dlc || return $?
837         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
838 peer:
839     - primary nid: 20@gni
840       Multi-Rail: True
841       peer ni:
842         - nid: 20@gni
843         - nid: 20.20.20.20@tcp
844         - nid: 20.20.20.20@o2ib
845 EOF
846         append_global_yaml
847         compare_peer_add "20@gni" "20.20.20.20@tcp,20.20.20.20@o2ib"
848 }
849 run_test 20 "Add peer with gni primary and tcp, o2ib secondary"
850
851 test_21() {
852         reinit_dlc || return $?
853         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
854 peer:
855     - primary nid: 21@gni
856       Multi-Rail: True
857       peer ni:
858         - nid: 21@gni
859         - nid: 22@gni
860         - nid: 23@gni
861         - nid: 24@gni
862         - nid: 25@gni
863 EOF
864         append_global_yaml
865         echo "Add peer with nidrange (gni)"
866         compare_peer_add "21@gni" "[22-25]@gni" || error
867         echo "Add peer with nidrange that overlaps primary nid (gni)"
868         compare_peer_add "21@gni" "[21-25]@gni"
869 }
870 run_test 21 "Add peer with nidrange (gni)"
871
872 test_22() {
873         reinit_dlc || return $?
874         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
875 peer:
876     - primary nid: 22@gni
877       Multi-Rail: True
878       peer ni:
879         - nid: 22@gni
880         - nid: 24@gni
881         - nid: 25@gni
882         - nid: 27@gni
883         - nid: 28@gni
884         - nid: 29@gni
885 EOF
886         append_global_yaml
887         do_lnetctl peer add --prim_nid 22@gni --nid [24-29]@gni ||
888                 error "Peer add failed $?"
889         compare_peer_del "22@gni" "26@gni"
890 }
891 run_test 22 "Delete single secondary nid from peer (gni)"
892
893 test_23() {
894         reinit_dlc || return $?
895         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
896 peer:
897     - primary nid: 23@gni
898       Multi-Rail: True
899       peer ni:
900         - nid: 23@gni
901 EOF
902         append_global_yaml
903
904         do_lnetctl peer add --prim_nid 23@gni --nid [25-29]@gni ||
905                 error "Peer add failed $?"
906         compare_peer_del "23@gni" "[25-29]@gni"
907 }
908 run_test 23 "Delete all secondary nids from peer (gni)"
909
910 test_24() {
911         reinit_dlc || return $?
912         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
913 peer:
914     - primary nid: 24@gni
915       Multi-Rail: True
916       peer ni:
917         - nid: 24@gni
918         - nid: 11@gni
919         - nid: 13.13.13.13@o2ib
920         - nid: 14.13.13.13@o2ib
921         - nid: 14.15.13.13@o2ib
922         - nid: 15.17.1.5@tcp
923         - nid: 15.17.1.10@tcp
924         - nid: 15.17.1.20@tcp
925 EOF
926         append_global_yaml
927         do_lnetctl peer add --prim_nid 24@gni \
928                 --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 ||
929                 error "Peer add failed $?"
930         compare_peer_del "24@gni" "5@gni,13.15.13.13@o2ib,15.17.1.15@tcp"
931 }
932 run_test 24 "Delete a secondary nid from peer (tcp, o2ib and gni)"
933
934 test_25() {
935         reinit_dlc || return $?
936         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
937 peer:
938     - primary nid: 25@gni
939       Multi-Rail: True
940       peer ni:
941         - nid: 25@gni
942 EOF
943         append_global_yaml
944         do_lnetctl peer add --prim_nid 25@gni \
945                 --nid [26-27].[4-10/3].26.26@tcp,26.26.26.26@o2ib,[30-35]@gni ||
946                 error "Peer add failed $?"
947         compare_peer_del "25@gni" \
948                 "[26-27].[4-10/3].26.26@tcp,26.26.26.26@o2ib,[30-35]@gni"
949 }
950 run_test 25 "Delete all secondary nids from peer (tcp, gni and o2ib)"
951
952 test_26() {
953         reinit_dlc || return $?
954
955         do_lnetctl peer add --prim_nid 1.1.1.1@tcp --lock_prim ||
956                 error "Peer add with --lock_prim option failed $?"
957         local peer_state=$($LNETCTL peer show -v 4 --nid 1.1.1.1@tcp |
958                 awk '/peer state/ {print $NF}')
959         # This relies on the following peer state definition:
960         # #define LNET_PEER_LOCK_PRIMARY          BIT(20)
961         if ((!("$peer_state" & (1 << 20)))); then
962                 error "Peer state does not have 'locked' bit set: $peer_state"
963         fi
964         do_lnetctl peer del --prim_nid 1.1.1.1@tcp ||
965                 error "Peer del failed $?"
966         $LNETCTL peer show --nid 1.1.1.1@tcp | grep -q 1.1.1.1@tcp ||
967                 error "1.1.1.1@tcp is not listed"
968         do_lnetctl peer del --prim_nid 1.1.1.1@tcp --force ||
969                 error "Peer del --force failed $?"
970         do_lnetctl peer show --nid 1.1.1.1@tcp &&
971                 error "failed to delete 1.1.1.1@tcp"
972
973         return 0
974 }
975 run_test 26 "Delete peer with primary nid locked"
976
977 test_99a() {
978         reinit_dlc || return $?
979
980         echo "Invalid prim_nid - peer add"
981         do_lnetctl peer add --prim_nid foobar &&
982                 error "Command should have failed"
983
984         echo "Invalid prim_nid - peer del"
985         do_lnetctl peer del --prim_nid foobar &&
986                 error "Command should have failed"
987
988         echo "Delete non-existing peer"
989         do_lnetctl peer del --prim_nid 1.1.1.1@o2ib &&
990                 error "Command should have failed"
991
992         echo "Don't provide mandatory argument for peer del"
993         do_lnetctl peer del --nid 1.1.1.1@tcp &&
994                 error "Command should have failed"
995
996         echo "Don't provide mandatory argument for peer add"
997         do_lnetctl peer add --nid 1.1.1.1@tcp &&
998                 error "Command should have failed"
999
1000         echo "Don't provide mandatory arguments peer add"
1001         do_lnetctl peer add &&
1002                 error "Command should have failed"
1003
1004         echo "Invalid secondary nids"
1005         do_lnetctl peer add --prim_nid 1.1.1.1@tcp --nid foobar &&
1006                 error "Command should have failed"
1007
1008         echo "Exceed max nids per peer"
1009         do_lnetctl peer add --prim_nid 1.1.1.1@tcp --nid 1.1.1.[2-255]@tcp &&
1010                 error "Command should have failed"
1011
1012         echo "Invalid net type"
1013         do_lnetctl peer add --prim_nid 1@foo &&
1014                 error "Command should have failed"
1015
1016         echo "Invalid nid format"
1017         local invalid_nids="1@tcp 1@o2ib 1.1.1.1@gni"
1018
1019         local nid
1020         for nid in ${invalid_nids}; do
1021                 echo "Check invalid primary nid - '$nid'"
1022                 do_lnetctl peer add --prim_nid $nid &&
1023                         error "Command should have failed"
1024         done
1025
1026         local invalid_strs="[2-1]@gni [a-f/x]@gni 256.256.256.256@tcp"
1027         invalid_strs+=" 1.1.1.1.[2-5/f]@tcp 1.]2[.3.4@o2ib"
1028         invalid_strs+="1.[2-4,[5-6],7-8].1.1@tcp foobar"
1029
1030         local nidstr
1031         for nidstr in ${invalid_strs}; do
1032                 echo "Check invalid nidstring - '$nidstr'"
1033                 do_lnetctl peer add --prim_nid 1.1.1.1@tcp --nid $nidstr &&
1034                         error "Command should have failed"
1035         done
1036
1037         echo "Add non-local gateway"
1038         do_lnetctl route add --net tcp --gateway 1@gni &&
1039                 error "Command should have failed"
1040
1041         return 0
1042 }
1043 run_test 99a "Check various invalid inputs to lnetctl peer"
1044
1045 test_99b() {
1046         reinit_dlc || return $?
1047
1048         create_base_yaml_file
1049
1050         cat <<EOF > $TMP/sanity-lnet-$testnum-invalid.yaml
1051 peer:
1052     - primary nid: 99.99.99.99@tcp
1053       Multi-Rail: Foobar
1054       peer ni:
1055         - nid: 99.99.99.99@tcp
1056 EOF
1057         do_lnetctl import < $TMP/sanity-lnet-$testnum-invalid.yaml &&
1058                 error "import should have failed"
1059         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
1060         compare_yaml_files
1061 }
1062 run_test 99b "Invalid value for Multi-Rail in yaml import"
1063
1064 have_interface() {
1065         local if="$1"
1066         local ip=$(ip addr show dev $if | awk '/ inet /{print $2}')
1067         [[ -n $ip ]]
1068 }
1069
1070 add_net() {
1071         local net="$1"
1072         local if="$2"
1073
1074         do_lnetctl net add --net ${net} --if ${if} ||
1075                 error "Failed to add net ${net} on if ${if}"
1076 }
1077
1078 del_net() {
1079         local net="$1"
1080         local if="$2"
1081
1082         do_lnetctl net del --net ${net} --if ${if} ||
1083                 error "Failed to del net ${net} on if ${if}"
1084 }
1085
1086 compare_route_add() {
1087         local rnet="$1"
1088         local gw="$2"
1089
1090         local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
1091
1092         do_lnetctl route add --net ${rnet} --gateway ${gw} ||
1093                 error "route add failed $?"
1094         $LNETCTL export --backup > $actual ||
1095                 error "export failed $?"
1096         validate_gateway_nids
1097         return $?
1098 }
1099
1100 append_net_tunables() {
1101         local net=${1:-tcp}
1102
1103         $LNETCTL net show -v --net ${net} | grep -v 'dev cpt' |
1104                 awk '/^\s+tunables:$/,/^\s+CPT:/' >> $TMP/sanity-lnet-$testnum-expected.yaml
1105 }
1106
1107 IF0_IP=$(ip -o -4 a s ${INTERFACES[0]} |
1108          awk '{print $4}' | sed 's/\/.*//')
1109 IF0_NET=$(awk -F. '{print $1"."$2"."$3}'<<<"${IF0_IP}")
1110 IF0_HOSTNUM=$(awk -F. '{print $4}'<<<"${IF0_IP}")
1111 if (((IF0_HOSTNUM + 5) > 254)); then
1112         GW_HOSTNUM=1
1113 else
1114         GW_HOSTNUM=$((IF0_HOSTNUM + 1))
1115 fi
1116 GW_NID="${IF0_NET}.${GW_HOSTNUM}@${NETTYPE}"
1117 test_100() {
1118         [[ ${NETTYPE} == tcp* ]] ||
1119                 skip "Need tcp NETTYPE"
1120         reinit_dlc || return $?
1121         add_net "${NETTYPE}" "${INTERFACES[0]}"
1122         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
1123 net:
1124     - net type: ${NETTYPE}
1125       local NI(s):
1126         - interfaces:
1127               0: ${INTERFACES[0]}
1128 EOF
1129         append_net_tunables tcp
1130         cat <<EOF >> $TMP/sanity-lnet-$testnum-expected.yaml
1131 route:
1132     - net: tcp7
1133       gateway: ${GW_NID}
1134       hop: -1
1135       priority: 0
1136       health_sensitivity: 1
1137 peer:
1138     - primary nid: ${GW_NID}
1139       Multi-Rail: False
1140       peer ni:
1141         - nid: ${GW_NID}
1142 EOF
1143         append_global_yaml
1144         compare_route_add "tcp7" "${GW_NID}"
1145 }
1146 run_test 100 "Add route with single gw (tcp)"
1147
1148 test_101() {
1149         [[ ${NETTYPE} == tcp* ]] ||
1150                 skip "Need tcp NETTYPE"
1151         reinit_dlc || return $?
1152         add_net "${NETTYPE}" "${INTERFACES[0]}"
1153         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
1154 net:
1155     - net type: ${NETTYPE}
1156       local NI(s):
1157         - interfaces:
1158               0: ${INTERFACES[0]}
1159 EOF
1160         append_net_tunables tcp
1161
1162         echo "route:" >> $TMP/sanity-lnet-$testnum-expected.yaml
1163         for i in $(seq $GW_HOSTNUM $((GW_HOSTNUM + 4))); do
1164                 cat <<EOF >> $TMP/sanity-lnet-$testnum-expected.yaml
1165     - net: tcp8
1166       gateway: ${IF0_NET}.${i}@tcp
1167       hop: -1
1168       priority: 0
1169       health_sensitivity: 1
1170 EOF
1171         done
1172
1173         echo "peer:" >> $TMP/sanity-lnet-$testnum-expected.yaml
1174         for i in $(seq $GW_HOSTNUM $((GW_HOSTNUM + 4))); do
1175                 cat <<EOF >> $TMP/sanity-lnet-$testnum-expected.yaml
1176     - primary nid: ${IF0_NET}.${i}@tcp
1177       Multi-Rail: False
1178       peer ni:
1179         - nid: ${IF0_NET}.${i}@tcp
1180 EOF
1181         done
1182         append_global_yaml
1183
1184         local gw="${IF0_NET}.[$GW_HOSTNUM-$((GW_HOSTNUM + 4))]@tcp"
1185
1186         compare_route_add "tcp8" "${gw}"
1187 }
1188 run_test 101 "Add route with multiple gw (tcp)"
1189
1190 compare_route_del() {
1191         local rnet="$1"
1192         local gw="$2"
1193
1194         local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
1195
1196         do_lnetctl route del --net ${rnet} --gateway ${gw} ||
1197                 error "route del failed $?"
1198         $LNETCTL export --backup > $actual ||
1199                 error "export failed $?"
1200         validate_gateway_nids
1201 }
1202
1203 generate_gw_nid() {
1204         local net=${1}
1205
1206         if [[ ${net} =~ (tcp|o2ib)[0-9]* ]]; then
1207                 echo "${GW_NID}"
1208         else
1209                 echo "$((${testnum} % 255))@${net}"
1210         fi
1211 }
1212
1213 test_102() {
1214         reinit_dlc || return $?
1215         add_net "${NETTYPE}" "${INTERFACES[0]}"
1216         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml
1217
1218         local gwnid=$(generate_gw_nid ${NETTYPE})
1219
1220         do_lnetctl route add --net ${NETTYPE}2 --gateway ${gwnid} ||
1221                 error "route add failed $?"
1222         compare_route_del "${NETTYPE}2" "${gwnid}"
1223 }
1224 run_test 102 "Delete route with single gw"
1225
1226 IP_NID_EXPR='103.103.103.[103-120/4]'
1227 NUM_NID_EXPR='[103-120/4]'
1228 test_103() {
1229         reinit_dlc || return $?
1230         add_net "${NETTYPE}" "${INTERFACES[0]}"
1231         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml
1232
1233         local nid_expr
1234
1235         if [[ $NETTYPE =~ (tcp|o2ib)[0-9]* ]]; then
1236                 nid_expr="${IF0_NET}.[$GW_HOSTNUM-$((GW_HOSTNUM+5))/2]"
1237         else
1238                 nid_expr="${NUM_NID_EXPR}"
1239         fi
1240
1241         do_lnetctl route add --net ${NETTYPE}103 \
1242                 --gateway ${nid_expr}@${NETTYPE} ||
1243                 error "route add failed $?"
1244         compare_route_del "${NETTYPE}103" "${nid_expr}@${NETTYPE}"
1245 }
1246 run_test 103 "Delete route with multiple gw"
1247
1248 test_104() {
1249         local tyaml="$TMP/sanity-lnet-$testnum-expected.yaml"
1250
1251         reinit_dlc || return $?
1252
1253         # Default value is '3'
1254         local val=$($LNETCTL global show | awk '/response_tracking/{print $NF}')
1255         [[ $val -ne 3 ]] &&
1256                 error "Expect 3 found $val"
1257
1258         echo "Set < 0;  Should fail"
1259         do_lnetctl set response_tracking -1 &&
1260                 error "should have failed $?"
1261
1262         reinit_dlc || return $?
1263         cat <<EOF > $tyaml
1264 global:
1265     response_tracking: -10
1266 EOF
1267         do_lnetctl import < $tyaml &&
1268                 error "should have failed $?"
1269
1270         echo "Check valid values; Should succeed"
1271         local i
1272         for ((i = 0; i < 4; i++)); do
1273                 reinit_dlc || return $?
1274                 do_lnetctl set response_tracking $i ||
1275                         error "should have succeeded $?"
1276                 $LNETCTL global show | grep -q "response_tracking: $i" ||
1277                         error "Failed to set response_tracking to $i"
1278                 reinit_dlc || return $?
1279                 cat <<EOF > $tyaml
1280 global:
1281     response_tracking: $i
1282 EOF
1283                 do_lnetctl import < $tyaml ||
1284                         error "should have succeeded $?"
1285                 $LNETCTL global show | grep -q "response_tracking: $i" ||
1286                         error "Failed to set response_tracking to $i"
1287         done
1288
1289         reinit_dlc || return $?
1290         echo "Set > 3; Should fail"
1291         do_lnetctl set response_tracking 4 &&
1292                 error "should have failed $?"
1293
1294         reinit_dlc || return $?
1295         cat <<EOF > $tyaml
1296 global:
1297     response_tracking: 10
1298 EOF
1299         do_lnetctl import < $tyaml &&
1300                 error "should have failed $?"
1301         return 0
1302 }
1303 run_test 104 "Set/check response_tracking param"
1304
1305 test_105() {
1306         reinit_dlc || return $?
1307         add_net "${NETTYPE}" "${INTERFACES[0]}"
1308
1309         local gwnid=$(generate_gw_nid ${NETTYPE})
1310
1311         do_lnetctl route add --net ${NETTYPE}105 --gateway ${gwnid} ||
1312                 error "route add failed $?"
1313         do_lnetctl peer add --prim ${gwnid} &&
1314                 error "peer add should fail"
1315
1316         return 0
1317 }
1318 run_test 105 "Adding duplicate GW peer should fail"
1319
1320 test_106() {
1321         reinit_dlc || return $?
1322         add_net "${NETTYPE}" "${INTERFACES[0]}"
1323
1324         local gwnid=$(generate_gw_nid ${NETTYPE})
1325
1326         do_lnetctl route add --net ${NETTYPE}106 --gateway ${gwnid} ||
1327                 error "route add failed $?"
1328         do_lnetctl peer del --prim ${gwnid} &&
1329                 error "peer del should fail"
1330
1331         return 0
1332 }
1333 run_test 106 "Deleting GW peer should fail"
1334
1335 test_107() {
1336         [[ ${NETTYPE} == tcp* ]] || skip "Need tcp NETTYPE"
1337
1338         cleanup_netns || error "Failed to cleanup netns before test execution"
1339         cleanup_lnet || error "Failed to unload modules before test execution"
1340
1341         setup_fakeif || error "Failed to add fake IF"
1342         have_interface "$FAKE_IF" ||
1343                 error "Expect $FAKE_IF configured but not found"
1344
1345         reinit_dlc || return $?
1346
1347         add_net "tcp" "${INTERFACES[0]}" || return $?
1348         add_net "tcp" "$FAKE_IF" || return $?
1349
1350         del_net "tcp" "$FAKE_IF" || return $?
1351
1352         cleanup_fakeif
1353         cleanup_lnet
1354         setup_netns
1355 }
1356 run_test 107 "Deleting extra interface doesn't crash node"
1357
1358 test_108() {
1359         [[ ${NETTYPE} == tcp* ]] || skip "Need tcp NETTYPE"
1360
1361         cleanup_netns || error "Failed to cleanup netns before test execution"
1362         cleanup_lnet || error "Failed to unload modules before test execution"
1363
1364         setup_fakeif || error "Failed to add fake IF"
1365         have_interface "$FAKE_IF" ||
1366                 error "Expect $FAKE_IF configured but not found"
1367
1368         reinit_dlc || return $?
1369
1370         add_net "tcp" "${INTERFACES[0]}" || return $?
1371         $LNETCTL net show > $TMP/sanity-lnet-$testnum-expected.yaml
1372         add_net "tcp" "$FAKE_IF" || return $?
1373
1374         cat <<EOF >> $TMP/sanity-lnet-$testnum-expected.yaml
1375       -     nid: ${FAKE_IP}@tcp
1376             status: up
1377             interfaces:
1378                   0: ${FAKE_IF}
1379 EOF
1380         $LNETCTL net show > $TMP/sanity-lnet-$testnum-actual.yaml
1381         compare_yaml_files || error "not all interfaces were setup"
1382
1383         cleanup_fakeif
1384         cleanup_lnet
1385         setup_netns
1386
1387         return 0
1388 }
1389 run_test 108 "Check Multi-Rail setup"
1390
1391 test_109() {
1392         [[ ${NETTYPE} == tcp* ]] || skip "Need tcp NETTYPE"
1393
1394         cleanup_netns || error "Failed to cleanup netns before test execution"
1395         cleanup_lnet || error "Failed to unload modules before test execution"
1396
1397         setup_fakeif || error "Failed to add fake IF"
1398         have_interface "$FAKE_IF" ||
1399                 error "Expect $FAKE_IF configured but not found"
1400
1401         FAKE_IF_ALIAS="${FAKE_IF}"
1402         FAKE_IF_ALIAS+=":0"
1403
1404         ifconfig "$FAKE_IF_ALIAS" "$FAKE_IP_ALIAS" up ||
1405                 error "Failed to add fake IF alias"
1406
1407         reinit_dlc || return $?
1408
1409         # add interface with longer name first
1410         add_net "tcp" "$FAKE_IF_ALIAS" || return $?
1411         add_net "tcp" "$FAKE_IF" || return $?
1412
1413         del_net "tcp" "$FAKE_IF" || return $?
1414         del_net "tcp" "$FAKE_IF_ALIAS" || return $?
1415
1416         # add interface with shorter name first
1417         add_net "tcp" "$FAKE_IF" || return $?
1418         add_net "tcp" "$FAKE_IF_ALIAS" || return $?
1419
1420         ifconfig "$FAKE_IF_ALIAS" "$FAKE_IP_ALIAS" down ||
1421                 error "Failed to clean up fake IF alias"
1422
1423         cleanup_fakeif
1424         cleanup_lnet
1425         setup_netns
1426 }
1427 run_test 109 "Add NI using a network interface alias (LU-16859)"
1428
1429 test_200() {
1430         [[ ${NETTYPE} == tcp* ]] ||
1431                 skip "Need tcp NETTYPE"
1432         cleanup_lnet || exit 1
1433         load_lnet "networks=\"\""
1434         do_ns $LNETCTL lnet configure --all || exit 1
1435         $LNETCTL net show --net tcp | grep -q "nid: ${FAKE_IP}@tcp$"
1436 }
1437 run_test 200 "load lnet w/o module option, configure in a non-default namespace"
1438
1439 test_201() {
1440         [[ ${NETTYPE} == tcp* ]] ||
1441                 skip "Need tcp NETTYPE"
1442         cleanup_lnet || exit 1
1443         load_lnet "networks=tcp($FAKE_IF)"
1444         do_ns $LNETCTL lnet configure --all || exit 1
1445         $LNETCTL net show --net tcp | grep -q "nid: ${FAKE_IP}@tcp$"
1446 }
1447 run_test 201 "load lnet using networks module options in a non-default namespace"
1448
1449 test_202() {
1450         [[ ${NETTYPE} == tcp* ]] ||
1451                 skip "Need tcp NETTYPE"
1452         cleanup_lnet || exit 1
1453         load_lnet "networks=\"\" ip2nets=\"tcp0($FAKE_IF) ${FAKE_IP}\""
1454         do_ns $LNETCTL lnet configure --all || exit 1
1455         $LNETCTL net show | grep -q "nid: ${FAKE_IP}@tcp$"
1456 }
1457 run_test 202 "load lnet using ip2nets in a non-default namespace"
1458
1459
1460 ### Add the interfaces in the target namespace
1461
1462 test_203() {
1463         [[ ${NETTYPE} == tcp* ]] ||
1464                 skip "Need tcp NETTYPE"
1465         cleanup_lnet || exit 1
1466         load_lnet
1467         do_lnetctl lnet configure || exit 1
1468         do_ns $LNETCTL net add --net tcp0 --if $FAKE_IF
1469 }
1470 run_test 203 "add a network using an interface in the non-default namespace"
1471
1472 LNET_PARAMS_FILE="$TMP/$TESTSUITE.parameters"
1473 function save_lnet_params() {
1474         $LNETCTL global show | egrep -v '^global:$' |
1475                                sed 's/://' > $LNET_PARAMS_FILE
1476 }
1477
1478 function restore_lnet_params() {
1479         local param value
1480         while read param value; do
1481                 [[ $param == max_intf ]] && continue
1482                 [[ $param == lnd_timeout ]] && continue
1483                 $LNETCTL set ${param} ${value} ||
1484                         error "Failed to restore ${param} to ${value}"
1485         done < $LNET_PARAMS_FILE
1486 }
1487
1488 function lnet_health_pre() {
1489         save_lnet_params
1490
1491         # Lower transaction timeout to speed up test execution
1492         $LNETCTL set transaction_timeout 10 ||
1493                 error "Failed to set transaction_timeout $?"
1494
1495         RETRY_PARAM=$($LNETCTL global show | awk '/retry_count/{print $NF}')
1496         RSND_PRE=$($LNETCTL stats show | awk '/resend_count/{print $NF}')
1497         LO_HVAL_PRE=$($LNETCTL net show -v 2 | awk '/health value/{print $NF}' |
1498                       xargs echo | sed 's/ /+/g' | bc -l)
1499
1500         RMT_HVAL_PRE=$($LNETCTL peer show --nid ${RNIDS[0]} -v 2 2>/dev/null |
1501                        awk '/health value/{print $NF}' | xargs echo |
1502                        sed 's/ /+/g' | bc -l)
1503
1504         # Might not have any peers so initialize to zero.
1505         RMT_HVAL_PRE=${RMT_HVAL_PRE:-0}
1506
1507         return 0
1508 }
1509
1510 function lnet_health_post() {
1511         RSND_POST=$($LNETCTL stats show | awk '/resend_count/{print $NF}')
1512         LO_HVAL_POST=$($LNETCTL net show -v 2 |
1513                        awk '/health value/{print $NF}' |
1514                        xargs echo | sed 's/ /+/g' | bc -l)
1515
1516         RMT_HVAL_POST=$($LNETCTL peer show --nid ${RNIDS[0]} -v 2 2>/dev/null |
1517                         awk '/health value/{print $NF}' | xargs echo |
1518                         sed 's/ /+/g' | bc -l)
1519
1520         # Might not have any peers so initialize to zero.
1521         RMT_HVAL_POST=${RMT_HVAL_POST:-0}
1522
1523         ${VERBOSE} &&
1524         echo "Pre resends: $RSND_PRE" &&
1525         echo "Post resends: $RSND_POST" &&
1526         echo "Resends delta: $((RSND_POST - RSND_PRE))" &&
1527         echo "Pre local health: $LO_HVAL_PRE" &&
1528         echo "Post local health: $LO_HVAL_POST" &&
1529         echo "Pre remote health: $RMT_HVAL_PRE" &&
1530         echo "Post remote health: $RMT_HVAL_POST"
1531
1532         restore_lnet_params
1533
1534         do_lnetctl peer set --health 1000 --all
1535         do_lnetctl net set --health 1000 --all
1536
1537         return 0
1538 }
1539
1540 function check_no_resends() {
1541         echo "Check that no resends took place"
1542         [[ $RSND_POST -ne $RSND_PRE ]] &&
1543                 error "Found resends: $RSND_POST != $RSND_PRE"
1544
1545         return 0
1546 }
1547
1548 function check_resends() {
1549         local delta=$((RSND_POST - RSND_PRE))
1550
1551         echo "Check that $RETRY_PARAM resends took place"
1552         [[ $delta -ne $RETRY_PARAM ]] &&
1553                 error "Expected $RETRY_PARAM resends found $delta"
1554
1555         return 0
1556 }
1557
1558 function check_no_local_health() {
1559         echo "Check that local NI health is unchanged"
1560         [[ $LO_HVAL_POST -ne $LO_HVAL_PRE ]] &&
1561                 error "Local health changed: $LO_HVAL_POST != $LO_HVAL_PRE"
1562
1563         return 0
1564 }
1565
1566 function check_local_health() {
1567         echo "Check that local NI health has been changed"
1568         [[ $LO_HVAL_POST -eq $LO_HVAL_PRE ]] &&
1569                 error "Local health unchanged: $LO_HVAL_POST == $LO_HVAL_PRE"
1570
1571         return 0
1572 }
1573
1574 function check_no_remote_health() {
1575         echo "Check that remote NI health is unchanged"
1576         [[ $RMT_HVAL_POST -ne $RMT_HVAL_PRE ]] &&
1577                 error "Remote health changed: $RMT_HVAL_POST != $RMT_HVAL_PRE"
1578
1579         return 0
1580 }
1581
1582 function check_remote_health() {
1583         echo "Check that remote NI health has been changed"
1584         [[ $RMT_HVAL_POST -eq $RMT_HVAL_PRE ]] &&
1585                 error "Remote health unchanged: $RMT_HVAL_POST == $RMT_HVAL_PRE"
1586
1587         return 0
1588 }
1589
1590 RNODE=""
1591 RLOADED=false
1592 NET_DEL_ARGS=""
1593 RNIDS=( )
1594 LNIDS=( )
1595 setup_health_test() {
1596         local need_mr=$1
1597         local rc=0
1598
1599         [[ ${NETTYPE} == kfi* ]] && skip "kfi doesn't support drop rules"
1600
1601         local rnodes=$(remote_nodes_list)
1602         [[ -z $rnodes ]] && skip "Need at least 1 remote node"
1603
1604         cleanup_lnet || error "Failed to cleanup before test execution"
1605
1606         # Loading modules should configure LNet with the appropriate
1607         # test-framework configuration
1608         load_lnet "config_on_load=1" || error "Failed to load modules"
1609
1610         LNIDS=( $($LCTL list_nids | xargs echo) )
1611
1612         RNODE=$(awk '{print $1}' <<<$rnodes)
1613         RNIDS=( $(do_node $RNODE $LCTL list_nids | xargs echo) )
1614
1615         if [[ -z ${RNIDS[@]} ]]; then
1616                 do_rpc_nodes $RNODE load_lnet "config_on_load=1"
1617                 RLOADED=true
1618                 RNIDS=( $(do_node $RNODE $LCTL list_nids | xargs echo) )
1619         fi
1620
1621         [[ ${#LNIDS[@]} -lt 1 ]] &&
1622                 error "No NIDs configured for local host $HOSTNAME"
1623         [[ ${#RNIDS[@]} -lt 1 ]] &&
1624                 error "No NIDs configured for remote host $RNODE"
1625
1626         # Ensure all peer NIs are local (i.e. non-routed config)
1627         local rnid rnet lnid lnet
1628
1629         for rnid in ${RNIDS[@]}; do
1630                 rnet=${rnid##*@}
1631                 for lnid in ${LNIDS[@]}; do
1632                         lnet=${lnid##*@}
1633                         [[ ${lnet} == ${rnet} ]] &&
1634                                 break
1635                 done
1636                 [[ ${lnet} != ${rnet} ]] &&
1637                         skip "Need non-routed configuration"
1638         done
1639
1640         do_lnetctl discover ${RNIDS[0]} ||
1641                 error "Unable to discover ${RNIDS[0]}"
1642
1643         local mr=$($LNETCTL peer show --nid ${RNIDS[0]} |
1644                    awk '/Multi-Rail/{print $NF}')
1645
1646         if ${need_mr} && [[ $mr == False ]]; then
1647                 cleanup_health_test || return $?
1648                 skip "Need MR peer"
1649         fi
1650
1651         if ( ! ${need_mr} && [[ ${#RNIDS[@]} -gt 1 ]] ) ||
1652            ( ! ${need_mr} && [[ ${#LNIDS[@]} -gt 1 ]] ); then
1653                 cleanup_health_test || return $?
1654                 skip "Need SR peer"
1655         fi
1656
1657         if ${need_mr} && [[ ${#RNIDS[@]} -lt 2 ]]; then
1658                 # Add a second, reachable NID to rnode.
1659                 local net=${RNIDS[0]}
1660
1661                 net="${net//*@/}1"
1662
1663                 local if=$(do_rpc_nodes --quiet $RNODE lnet_if_list)
1664                 [[ -z $if ]] &&
1665                         error "Failed to determine interface for $RNODE"
1666
1667                 do_rpc_nodes $RNODE "$LNETCTL lnet configure"
1668                 do_rpc_nodes $RNODE "$LNETCTL net add --net $net --if $if" ||
1669                         rc=$?
1670                 if [[ $rc -ne 0 ]]; then
1671                         error "Failed to add interface to $RNODE rc=$?"
1672                 else
1673                         RNIDS[1]="${RNIDS[0]}1"
1674                         NET_DEL_ARGS="--net $net --if $if"
1675                 fi
1676         fi
1677
1678         if ${need_mr} && [[ ${#LNIDS[@]} -lt 2 ]]; then
1679                 local net=${LNIDS[0]}
1680                 net="${net//*@/}1"
1681
1682                 do_lnetctl lnet configure &&
1683                         do_lnetctl net add --net $net --if ${INTERFACES[0]} ||
1684                         rc=$?
1685                 if [[ $rc -ne 0 ]]; then
1686                         error "Failed to add interface rc=$?"
1687                 else
1688                         LNIDS[1]="${LNIDS[0]}1"
1689                 fi
1690         fi
1691
1692         $LNETCTL net show
1693
1694         $LNETCTL peer show -v 2 | egrep -e nid -e health
1695
1696         $LCTL set_param debug=+net
1697
1698         return 0
1699
1700 }
1701
1702 cleanup_health_test() {
1703         local rc=0
1704
1705         if [[ -n $NET_DEL_ARGS ]]; then
1706                 do_rpc_nodes $RNODE \
1707                         "$LNETCTL net del $NET_DEL_ARGS" ||
1708                         rc=$((rc + $?))
1709                 NET_DEL_ARGS=""
1710         fi
1711
1712         unload_modules || rc=$?
1713
1714         if $RLOADED; then
1715                 do_rpc_nodes $RNODE unload_modules_local ||
1716                         rc=$((rc + $?))
1717                 RLOADED=false
1718         fi
1719
1720         [[ $rc -ne 0 ]] &&
1721                 error "Failed cleanup"
1722
1723         return $rc
1724 }
1725
1726 add_health_test_drop_rules() {
1727         local args="-m GET -r 1 -e ${1}"
1728         local src dst
1729
1730         for src in "${LNIDS[@]}"; do
1731                 for dst in "${RNIDS[@]}" "${LNIDS[@]}"; do
1732                         $LCTL net_drop_add -s $src -d $dst ${args} ||
1733                                 error "Failed to add drop rule $src $dst $args"
1734                 done
1735         done
1736 }
1737
1738 do_lnet_health_ping_test() {
1739         local hstatus="$1"
1740
1741         echo "Simulate $hstatus"
1742
1743         lnet_health_pre || return $?
1744
1745         add_health_test_drop_rules ${hstatus}
1746         do_lnetctl ping ${RNIDS[0]} &&
1747                 error "Should have failed"
1748
1749         lnet_health_post
1750
1751         $LCTL net_drop_del -a
1752
1753         return 0
1754 }
1755
1756 # See lnet/lnet/lib-msg.c:lnet_health_check()
1757 LNET_LOCAL_RESEND_STATUSES="local_interrupt local_dropped local_aborted"
1758 LNET_LOCAL_RESEND_STATUSES+=" local_no_route local_timeout"
1759 LNET_LOCAL_NO_RESEND_STATUSES="local_error"
1760 test_204() {
1761         setup_health_test false || return $?
1762
1763         local hstatus
1764         for hstatus in ${LNET_LOCAL_RESEND_STATUSES} \
1765                        ${LNET_LOCAL_NO_RESEND_STATUSES}; do
1766                 do_lnet_health_ping_test "${hstatus}" || return $?
1767                 check_no_resends || return $?
1768                 check_no_local_health || return $?
1769         done
1770
1771         cleanup_health_test || return $?
1772
1773         return 0
1774 }
1775 run_test 204 "Check no health or resends for single-rail local failures"
1776
1777 test_205() {
1778         (( $MDS1_VERSION >= $(version_code 2.14.58) )) ||
1779                 skip "need at least 2.14.58"
1780
1781         setup_health_test true || return $?
1782
1783         local hstatus
1784         for hstatus in ${LNET_LOCAL_RESEND_STATUSES}; do
1785                 do_lnet_health_ping_test "${hstatus}" || return $?
1786                 check_resends || return $?
1787                 check_local_health || return $?
1788         done
1789
1790         for hstatus in ${LNET_LOCAL_NO_RESEND_STATUSES}; do
1791                 do_lnet_health_ping_test "${hstatus}" || return $?
1792                 check_no_resends || return $?
1793                 check_local_health || return $?
1794         done
1795
1796         cleanup_health_test || return $?
1797
1798         return 0
1799 }
1800 run_test 205 "Check health and resends for multi-rail local failures"
1801
1802 # See lnet/lnet/lib-msg.c:lnet_health_check()
1803 LNET_REMOTE_RESEND_STATUSES="remote_dropped"
1804 LNET_REMOTE_NO_RESEND_STATUSES="remote_error remote_timeout"
1805 test_206() {
1806         setup_health_test false || return $?
1807
1808         local hstatus
1809         for hstatus in ${LNET_REMOTE_RESEND_STATUSES} \
1810                        ${LNET_REMOTE_NO_RESEND_STATUSES}; do
1811                 do_lnet_health_ping_test "${hstatus}" || return $?
1812                 check_no_resends || return $?
1813                 check_no_local_health || return $?
1814                 check_no_remote_health || return $?
1815         done
1816
1817         cleanup_health_test || return $?
1818
1819         return 0
1820 }
1821 run_test 206 "Check no health or resends for single-rail remote failures"
1822
1823 test_207() {
1824         (( $MDS1_VERSION >= $(version_code 2.14.58) )) ||
1825                 skip "need at least 2.14.58"
1826
1827         setup_health_test true || return $?
1828
1829         local hstatus
1830         for hstatus in ${LNET_REMOTE_RESEND_STATUSES}; do
1831                 do_lnet_health_ping_test "${hstatus}" || return $?
1832                 check_resends || return $?
1833                 check_no_local_health || return $?
1834                 check_remote_health || return $?
1835                 do_lnetctl peer set --health 1000 --all ||
1836                         error "Unable to reset health rc=$?"
1837         done
1838         for hstatus in ${LNET_REMOTE_NO_RESEND_STATUSES}; do
1839                 do_lnet_health_ping_test "${hstatus}" || return $?
1840                 check_no_resends || return $?
1841                 check_no_local_health || return $?
1842                 check_remote_health || return $?
1843                 do_lnetctl peer set --health 1000 --all ||
1844                         error "Unable to reset health rc=$?"
1845         done
1846
1847         cleanup_health_test || return $?
1848
1849         return 0
1850 }
1851 run_test 207 "Check health and resends for multi-rail remote errors"
1852
1853 test_208_load_and_check_lnet() {
1854         local ip2nets="$1"
1855         local p_nid="$2"
1856         local s_nid="$3"
1857         local num_expected=1
1858
1859         load_lnet "networks=\"\" ip2nets=\"${ip2nets_str}\""
1860
1861         $LCTL net up ||
1862                 error "Failed to load LNet with ip2nets \"${ip2nets_str}\""
1863
1864         [[ -n $s_nid ]] &&
1865                 num_expected=2
1866
1867         declare -a nids
1868         nids=( $($LCTL list_nids) )
1869
1870         [[ ${#nids[@]} -ne ${num_expected} ]] &&
1871                 error "Expect ${num_expected} NIDs found ${#nids[@]}"
1872
1873         [[ ${nids[0]} == ${p_nid} ]] ||
1874                 error "Expect NID \"${p_nid}\" found \"${nids[0]}\""
1875
1876         [[ -n $s_nid ]] && [[ ${nids[1]} != ${s_nid} ]] &&
1877                 error "Expect second NID \"${s_nid}\" found \"${nids[1]}\""
1878
1879         $LCTL net down &>/dev/null
1880         cleanup_lnet
1881 }
1882
1883 test_208() {
1884         [[ ${NETTYPE} == tcp* ]] ||
1885                 skip "Need tcp NETTYPE"
1886
1887         cleanup_netns || error "Failed to cleanup netns before test execution"
1888         cleanup_lnet || error "Failed to unload modules before test execution"
1889         setup_fakeif || error "Failed to add fake IF"
1890
1891         have_interface "$FAKE_IF" ||
1892                 error "Expect $FAKE_IF configured but not found"
1893
1894         local if0_ip=$(ip --oneline addr show dev ${INTERFACES[0]} |
1895                        awk '/inet /{print $4}' |
1896                        sed 's:/.*::')
1897         if0_ip=($(echo "${if0_ip[@]}" | tr ' ' '\n' | uniq | tr '\n' ' '))
1898         local ip2nets_str="tcp(${INTERFACES[0]}) $if0_ip"
1899
1900         echo "Configure single NID \"$ip2nets_str\""
1901         test_208_load_and_check_lnet "${ip2nets_str}" "${if0_ip}@tcp"
1902
1903         ip2nets_str="tcp(${INTERFACES[0]}) $if0_ip; tcp1($FAKE_IF) $FAKE_IP"
1904         echo "Configure two NIDs; two NETs \"$ip2nets_str\""
1905         test_208_load_and_check_lnet "${ip2nets_str}" "${if0_ip}@tcp" \
1906                                      "${FAKE_IP}@tcp1"
1907
1908         ip2nets_str="tcp(${INTERFACES[0]}) $if0_ip; tcp($FAKE_IF) $FAKE_IP"
1909         echo "Configure two NIDs; one NET \"$ip2nets_str\""
1910         test_208_load_and_check_lnet "${ip2nets_str}" "${if0_ip}@tcp" \
1911                                      "${FAKE_IP}@tcp"
1912         local addr1=( ${if0_ip//./ } )
1913         local addr2=( ${FAKE_IP//./ } )
1914         local range="[${addr1[0]},${addr2[0]}]"
1915
1916         local i
1917         for i in $(seq 1 3); do
1918                 range+=".[${addr1[$i]},${addr2[$i]}]"
1919         done
1920         ip2nets_str="tcp(${INTERFACES[0]},${FAKE_IF}) ${range}"
1921
1922         echo "Configured two NIDs; one NET alt syntax \"$ip2nets_str\""
1923         test_208_load_and_check_lnet "${ip2nets_str}" "${if0_ip}@tcp" \
1924                                      "${FAKE_IP}@tcp"
1925
1926         cleanup_fakeif
1927
1928         echo "alt syntax with missing IF \"$ip2nets_str\""
1929         load_lnet "networks=\"\" ip2nets=\"${ip2nets_str}\""
1930
1931         echo "$LCTL net up should fail"
1932         $LCTL net up &&
1933                 error "LNet bring up should have failed"
1934
1935         cleanup_lnet
1936 }
1937 run_test 208 "Test various kernel ip2nets configurations"
1938
1939 test_209() {
1940         (( $MDS1_VERSION >= $(version_code 2.14.58) )) ||
1941                 skip "need at least 2.14.58"
1942
1943         setup_health_test false || return $?
1944
1945         echo "Simulate network_timeout w/SR config"
1946         lnet_health_pre
1947
1948         add_health_test_drop_rules network_timeout
1949
1950         do_lnetctl discover ${RNIDS[0]} &&
1951                 error "Should have failed"
1952
1953         lnet_health_post
1954
1955         check_no_resends || return $?
1956         check_no_local_health || return $?
1957         check_no_remote_health || return $?
1958
1959         cleanup_health_test || return $?
1960
1961         setup_health_test true || return $?
1962
1963         echo "Simulate network_timeout w/MR config"
1964
1965         lnet_health_pre
1966
1967         add_health_test_drop_rules network_timeout
1968
1969         do_lnetctl discover ${RNIDS[0]} &&
1970                 error "Should have failed"
1971
1972         lnet_health_post
1973
1974         check_no_resends || return $?
1975         check_local_health || return $?
1976         check_remote_health || return $?
1977
1978         cleanup_health_test || return $?
1979
1980         return 0
1981 }
1982 run_test 209 "Check health, but not resends, for network timeout"
1983
1984 check_nid_in_recovq() {
1985         local queue="$1"
1986         local nid="$2"
1987         local expect="$3"
1988         local max_wait="${4:-10}"
1989         local rc=0
1990
1991         (($expect == 0)) &&
1992                 echo "$queue recovery queue should be empty" ||
1993                 echo "$queue recovery queue should have $nid"
1994
1995         wait_update $HOSTNAME \
1996                 "$LNETCTL debug recovery $queue | \
1997                  grep -wc \"nid-0: $nid\"" \
1998                 "$expect" "$max_wait"
1999         rc=$?
2000         do_lnetctl debug recovery $queue
2001         (($rc == 0)) ||
2002                 error "Expect $expect NIDs in recovery."
2003
2004         return 0
2005 }
2006
2007 # First ping is sent at time 0.
2008 # 2nd at 0 + 2^1 = 2
2009 # 3rd at 2 + 2^2 = 6
2010 # 4th at 6 + 2^3 = 14
2011 # 5th at 14 + 2^4 = 30
2012 # e.g. after 10 seconds we would expect 3 pings to have been sent, and the
2013 # NI will have been enqueued for the 4th ping.
2014
2015 # If the recovery limit is 10 seconds, then after the 4th ping is sent
2016 # we expect the peer NI to have aged out, so it will not actually be
2017 # queued for a 5th ping.
2018 # If max_recovery_ping_interval is set to 4 then:
2019 #  First ping is sent at time 0
2020 #  2nd at  0 + min(2^1, 4) = 2
2021 #  3rd at  2 + min(2^2, 4) = 6
2022 #  4th at  6 + min(2^3, 4) = 10
2023 #  5th at 10 + min(2^4, 4) = 14
2024 #  6th at 14 + min(2^5, 4) = 18
2025 #  7th at 18 + min(2^6, 4) = 22
2026 # e.g. after 4 seconds we would expect 2 pings to have been sent, and
2027 # after 13 seconds we would expect 4 pings to have been sent
2028 check_ping_count() {
2029         local queue="$1"
2030         local nid="$2"
2031         local expect="$3"
2032         local max_wait="$4"
2033
2034         echo "Check ping counts:"
2035
2036         local rc=0
2037         if [[ $queue == "ni" ]]; then
2038                 wait_update $HOSTNAME \
2039                         "$LNETCTL net show -v 2 | \
2040                          grep -e $nid -e ping_count | grep -wA1 $nid | \
2041                          awk '/ping_count/{print \\\$NF}'" "$expect" "$max_wait"
2042                 rc=$?
2043                 $LNETCTL net show -v 2 | grep -e $nid -e ping_count
2044         elif [[ $queue == peer_ni ]]; then
2045                 wait_update $HOSTNAME \
2046                         "$LNETCTL peer show -v 2 --nid $nid | \
2047                          grep -v primary | \
2048                          grep -e $nid -e ping_count | grep -wA1 $nid | \
2049                          awk '/ping_count/{print \\\$NF}'" "$expect" "$max_wait"
2050                 rc=$?
2051                 $LNETCTL peer show -v 2 --nid $nid | grep -v primary |
2052                         grep -e $nid -e ping_count
2053         else
2054                 error "Unrecognized queue \"$queue\""
2055                 return 1
2056         fi
2057
2058         ((rc == 0)) || error "Unexpected ping count"
2059
2060         return 0
2061 }
2062
2063 test_210() {
2064         [[ ${NETTYPE} == kfi* ]] && skip "kfi doesn't support drop rules"
2065
2066         reinit_dlc || return $?
2067         add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?
2068         add_net "${NETTYPE}1" "${INTERFACES[0]}" || return $?
2069
2070         local prim_nid=$($LCTL list_nids | head -n 1)
2071
2072         do_lnetctl discover $prim_nid ||
2073                 error "failed to discover myself"
2074
2075         local default=$($LNETCTL global show |
2076                         awk '/recovery_limit/{print $NF}')
2077         # Set recovery limit to 10 seconds.
2078         do_lnetctl set recovery_limit 10 ||
2079                 error "failed to set recovery_limit"
2080
2081         $LCTL set_param debug=+net
2082         $LCTL net_drop_add -s *@${NETTYPE} -d *@${NETTYPE} -m GET -r 1 -e local_error
2083         $LCTL net_drop_add -s *@${NETTYPE}1 -d *@${NETTYPE}1 -m GET -r 1 -e local_error
2084         $LCTL net_drop_add -s *@${NETTYPE} -d *@${NETTYPE} -r 1
2085         $LCTL net_drop_add -s *@${NETTYPE}1 -d *@${NETTYPE}1 -r 1
2086         do_lnetctl net set --health 0 --nid $prim_nid
2087
2088         check_ping_count "ni" "$prim_nid" "2" "10"
2089         check_nid_in_recovq "-l" "$prim_nid" "1"
2090
2091         check_ping_count "ni" "$prim_nid" "3" "10"
2092         check_nid_in_recovq "-l" "$prim_nid" "1"
2093
2094         $LCTL net_drop_del -a
2095
2096         reinit_dlc || return $?
2097         add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?
2098         add_net "${NETTYPE}1" "${INTERFACES[0]}" || return $?
2099
2100         local prim_nid=$($LCTL list_nids | head -n 1)
2101
2102         do_lnetctl discover $prim_nid ||
2103                 error "failed to discover myself"
2104
2105         do_lnetctl set recovery_limit $default ||
2106                 error "failed to set recovery_limit"
2107
2108         default=$($LNETCTL global show |
2109                   awk '/max_recovery_ping_interval/{print $NF}')
2110         do_lnetctl set max_recovery_ping_interval 4 ||
2111                 error "failed to set max_recovery_ping_interval"
2112
2113         $LCTL set_param debug=+net
2114         $LCTL net_drop_add -s *@${NETTYPE} -d *@${NETTYPE} -m GET -r 1 -e local_error
2115         $LCTL net_drop_add -s *@${NETTYPE}1 -d *@${NETTYPE}1 -m GET -r 1 -e local_error
2116         $LCTL net_drop_add -s *@${NETTYPE} -d *@${NETTYPE} -r 1
2117         $LCTL net_drop_add -s *@${NETTYPE}1 -d *@${NETTYPE}1 -r 1
2118         do_lnetctl net set --health 0 --nid $prim_nid
2119
2120         check_ping_count "ni" "$prim_nid" "2" "10"
2121         check_nid_in_recovq "-l" "$prim_nid" "1"
2122
2123         check_ping_count "ni" "$prim_nid" "4" "10"
2124         check_nid_in_recovq "-l" "$prim_nid" "1"
2125
2126         $LCTL net_drop_del -a
2127
2128         do_lnetctl set max_recovery_ping_interval $default ||
2129                 error "failed to set max_recovery_ping_interval"
2130
2131         return 0
2132 }
2133 run_test 210 "Local NI recovery checks"
2134
2135 test_211() {
2136         [[ ${NETTYPE} == kfi* ]] && skip "kfi doesn't support drop rules"
2137
2138         reinit_dlc || return $?
2139         add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?
2140         add_net "${NETTYPE}1" "${INTERFACES[0]}" || return $?
2141
2142         local prim_nid=$($LCTL list_nids | head -n 1)
2143
2144         do_lnetctl discover $prim_nid ||
2145                 error "failed to discover myself"
2146
2147         local default=$($LNETCTL global show |
2148                         awk '/recovery_limit/{print $NF}')
2149         # Set recovery limit to 10 seconds.
2150         do_lnetctl set recovery_limit 10 ||
2151                 error "failed to set recovery_limit"
2152
2153         $LCTL net_drop_add -s *@${NETTYPE} -d *@${NETTYPE} -m GET -r 1 -e remote_error
2154         $LCTL net_drop_add -s *@${NETTYPE}1 -d *@${NETTYPE}1 -m GET -r 1 -e remote_error
2155         $LCTL net_drop_add -s *@${NETTYPE} -d *@${NETTYPE} -r 1
2156         $LCTL net_drop_add -s *@${NETTYPE}1 -d *@${NETTYPE}1 -r 1
2157
2158         # Set health to 0 on one interface. This forces it onto the recovery
2159         # queue.
2160         do_lnetctl peer set --nid $prim_nid --health 0
2161
2162         check_nid_in_recovq "-p" "$prim_nid" "1"
2163
2164         # The peer should age out in 10-20 seconds
2165         check_nid_in_recovq "-p" "$prim_nid" "0" "20"
2166         # Ping count should reset to 0 when peer ages out
2167         check_ping_count "peer_ni" "$prim_nid" "0"
2168
2169         $LCTL net_drop_del -a
2170
2171         # Set health to force it back onto the recovery queue. Set to 500 means
2172         # in ~5 seconds it should be back at maximum value.
2173         # NB: we reset the recovery limit to 0 (indefinite) so the peer NI is
2174         # eligible again
2175         do_lnetctl set recovery_limit 0 ||
2176                 error "failed to set recovery_limit"
2177
2178         do_lnetctl peer set --nid $prim_nid --health 500
2179
2180         check_nid_in_recovq "-p" "$prim_nid" "1"
2181         check_nid_in_recovq "-p" "$prim_nid" "0" "20"
2182
2183         reinit_dlc || return $?
2184         add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?
2185         add_net "${NETTYPE}1" "${INTERFACES[0]}" || return $?
2186
2187         local prim_nid=$($LCTL list_nids | head -n 1)
2188
2189         do_lnetctl discover $prim_nid ||
2190                 error "failed to discover myself"
2191
2192         do_lnetctl set recovery_limit $default ||
2193                 error "failed to set recovery_limit"
2194
2195         default=$($LNETCTL global show |
2196                   awk '/max_recovery_ping_interval/{print $NF}')
2197         do_lnetctl set max_recovery_ping_interval 4 ||
2198                 error "failed to set max_recovery_ping_interval"
2199
2200         $LCTL net_drop_add -s *@${NETTYPE} -d *@${NETTYPE} -m GET -r 1 -e remote_error
2201         $LCTL net_drop_add -s *@${NETTYPE}1 -d *@${NETTYPE}1 -m GET -r 1 -e remote_error
2202         $LCTL net_drop_add -s *@${NETTYPE} -d *@${NETTYPE} -r 1
2203         $LCTL net_drop_add -s *@${NETTYPE}1 -d *@${NETTYPE}1 -r 1
2204
2205         # Set health to 0 on one interface. This forces it onto the recovery
2206         # queue.
2207         do_lnetctl peer set --nid $prim_nid --health 0
2208
2209         check_ping_count "peer_ni" "$prim_nid" "1" "4"
2210         check_nid_in_recovq "-p" "$prim_nid" "1"
2211
2212         # After we detect the 1st ping above, the 4th ping should be sent after
2213         # ~13 seconds
2214         check_ping_count "peer_ni" "$prim_nid" "4" "14"
2215         check_nid_in_recovq "-p" "$prim_nid" "1"
2216
2217         $LCTL net_drop_del -a
2218
2219         do_lnetctl set max_recovery_ping_interval $default ||
2220                 error "failed to set max_recovery_ping_interval"
2221
2222         return 0
2223 }
2224 run_test 211 "Remote NI recovery checks"
2225
2226 test_212() {
2227         [[ ${NETTYPE} == kfi* ]] && skip "kfi doesn't support drop rules"
2228
2229         local rnodes=$(remote_nodes_list)
2230         [[ -z $rnodes ]] && skip "Need at least 1 remote node"
2231
2232         cleanup_lnet || error "Failed to cleanup before test execution"
2233
2234         # Loading modules should configure LNet with the appropriate
2235         # test-framework configuration
2236         load_lnet "config_on_load=1" || error "Failed to load modules"
2237
2238         local my_nid=$($LCTL list_nids | head -n 1)
2239         [[ -z $my_nid ]] &&
2240                 error "Failed to get primary NID for local host $HOSTNAME"
2241
2242         local rnode=$(awk '{print $1}' <<<$rnodes)
2243         local rnodenids=$(do_node $rnode $LCTL list_nids | xargs echo)
2244         local rloaded=false
2245
2246         if [[ -z $rnodenids ]]; then
2247                 do_rpc_nodes $rnode load_lnet "config_on_load=1"
2248                 rloaded=true
2249                 rnodenids=$(do_node $rnode $LCTL list_nids | xargs echo)
2250         fi
2251
2252         local rnodepnid=$(awk '{print $1}' <<< $rnodenids)
2253
2254         [[ -z $rnodepnid ]] &&
2255                 error "Failed to get primary NID for remote host $rnode"
2256
2257         log "Initial discovery"
2258         do_lnetctl discover --force $rnodepnid ||
2259                 error "Failed to discover $rnodepnid"
2260
2261         do_node $rnode "$LNETCTL discover --force $my_nid" ||
2262                 error "$rnode failed to discover $my_nid"
2263
2264         log "Fail local discover ping to set LNET_PEER_REDISCOVER flag"
2265         $LCTL net_drop_add -s "*@$NETTYPE" -d "*@$NETTYPE" -r 1 -e local_error
2266         do_lnetctl discover --force $rnodepnid &&
2267                 error "Discovery should have failed"
2268         $LCTL net_drop_del -a
2269
2270         local nid
2271         for nid in $rnodenids; do
2272                 # We need GET (PING) delay just long enough so we can trigger
2273                 # discovery on the remote peer
2274                 $LCTL net_delay_add -s "*@$NETTYPE" -d $nid -r 1 -m GET -l 3
2275                 $LCTL net_drop_add -s "*@$NETTYPE" -d $nid -r 1 -m GET -e local_error
2276                 # We need PUT (PUSH) delay just long enough so we can process
2277                 # the PING failure
2278                 $LCTL net_delay_add -s "*@$NETTYPE" -d $nid -r 1 -m PUT -l 6
2279         done
2280
2281         log "Force $HOSTNAME to discover $rnodepnid (in background)"
2282         # We want to get a PING sent that we know will eventually fail.
2283         # The delay rules we added will ensure the ping is not sent until
2284         # the PUSH is also in flight (see below), and the drop rule ensures that
2285         # when the PING is eventually sent it will error out
2286         do_lnetctl discover --force $rnodepnid &
2287         local pid1=$!
2288
2289         # We want a discovery PUSH from rnode to put rnode back on our
2290         # discovery queue. This should cause us to try and send a PUSH to rnode
2291         # while the PING is still outstanding.
2292         log "Force $rnode to discover $my_nid"
2293         do_node $rnode $LNETCTL discover --force $my_nid
2294
2295         # At this point we'll have both PING_SENT and PUSH_SENT set for the
2296         # rnode peer. Wait for the PING to error out which should terminate the
2297         # discovery process that we backgrounded.
2298         log "Wait for $pid1"
2299         wait $pid1
2300         log "Finished wait on $pid1"
2301
2302         # The PING send failure clears the PING_SENT flag and puts the peer back
2303         # on the discovery queue. When discovery thread processes the peer it
2304         # will mistakenly clear the PUSH_SENT flag (and set PUSH_FAILED).
2305         # Discovery will then complete for this peer even though we have an
2306         # outstanding PUSH.
2307         # When PUSH is actually unlinked it will be forced back onto the
2308         # discovery queue, but we no longer have a ref on the peer. When
2309         # discovery completes again, we'll trip the ASSERT in
2310         # lnet_destroy_peer_locked()
2311
2312         # Delete the delay rules to send the PUSH
2313         $LCTL net_delay_del -a
2314         # Delete the drop rules
2315         $LCTL net_drop_del -a
2316
2317         unload_modules ||
2318                 error "Failed to unload modules"
2319         if $rloaded; then
2320                 do_rpc_nodes $rnode unload_modules_local ||
2321                         error "Failed to unload modules on $rnode"
2322         fi
2323
2324         return 0
2325 }
2326 run_test 212 "Check discovery refcount loss bug (LU-14627)"
2327
2328 test_213() {
2329         [[ ${NETTYPE} == tcp* ]] || skip "Need tcp NETTYPE"
2330
2331         cleanup_netns || error "Failed to cleanup netns before test execution"
2332         cleanup_lnet || error "Failed to unload modules before test execution"
2333
2334         setup_fakeif || error "Failed to add fake IF"
2335         have_interface "$FAKE_IF" ||
2336                 error "Expect $FAKE_IF configured but not found"
2337
2338         reinit_dlc || return $?
2339
2340         add_net "tcp" "${INTERFACES[0]}" || return $?
2341         add_net "tcp" "$FAKE_IF" || return $?
2342
2343         local nid1=$(lctl list_nids | head -n 1)
2344         local nid2=$(lctl list_nids | tail --lines 1)
2345
2346         [[ $(lctl which_nid $nid1 $nid2) == $nid1 ]] ||
2347                 error "Expect nid1 \"$nid1\" to be preferred"
2348
2349         [[ $(lctl which_nid $nid2 $nid1) == $nid2 ]] ||
2350                 error "Expect nid2 \"$nid2\" to be preferred"
2351
2352         return 0
2353 }
2354 run_test 213 "Check LNetDist calculation for multiple local NIDs"
2355
2356 function check_ni_status() {
2357         local nid="$1"
2358         local expect="$2"
2359
2360         local status=$($LNETCTL net show |
2361                        grep -A 1 ${nid} |
2362                        awk '/status/{print $NF}')
2363
2364         echo "NI ${nid} expect status \"${expect}\" found \"${status}\""
2365         if [[ $status != $expect ]]; then
2366                 error "Error: Expect NI status \"$expect\" for NID \"$nid\" but found \"$status\""
2367         fi
2368
2369         return 0
2370 }
2371
2372 test_214() {
2373         [[ ${NETTYPE} == tcp* ]] || skip "Need tcp NETTYPE"
2374
2375         cleanup_netns || error "Failed to cleanup netns before test execution"
2376         cleanup_lnet || error "Failed to unload modules before test execution"
2377
2378         setup_fakeif || error "Failed to add fake IF"
2379         have_interface "$FAKE_IF" ||
2380                 error "Expect $FAKE_IF configured but not found"
2381
2382         reinit_dlc || return $?
2383
2384         add_net "tcp" "${INTERFACES[0]}" || return $?
2385         add_net "tcp" "$FAKE_IF" || return $?
2386
2387         local nid1=$(lctl list_nids | head -n 1)
2388         local nid2=$(lctl list_nids | tail --lines 1)
2389
2390         check_ni_status "0@lo" up
2391         check_ni_status "$nid1" up
2392         check_ni_status "$nid2" up
2393
2394         do_lnetctl ping --source $nid2 $nid1 ||
2395                 error "$LNETCTL ping --source $nid2 $nid1 failed"
2396
2397         echo "Set $FAKE_IF down"
2398         echo "ip link set dev $FAKE_IF down"
2399         ip link set dev $FAKE_IF down
2400         check_ni_status "0@lo" up
2401         check_ni_status "$nid1" up
2402         check_ni_status "$nid2" down
2403 }
2404 run_test 214 "Check local NI status when link is downed"
2405
2406 get_ni_stat() {
2407         local nid=$1
2408         local stat=$2
2409
2410         $LNETCTL net show -v 2 |
2411                 egrep -e nid -e $stat |
2412                 grep -wA 1 $nid |
2413                 awk '/'$stat':/{print $NF}'
2414 }
2415
2416 ni_stats_pre() {
2417         local nidvar s
2418         for nidvar in nid1 nid2; do
2419                 for stat in send_count recv_count; do
2420                         s=$(get_ni_stat ${!nidvar} $stat)
2421                         eval ${nidvar}_pre_${stat}=$s
2422                 done
2423         done
2424 }
2425
2426 ni_stats_post() {
2427         local nidvar s
2428         for nidvar in nid1 nid2; do
2429                 for stat in send_count recv_count; do
2430                         s=$(get_ni_stat ${!nidvar} $stat)
2431                         eval ${nidvar}_post_${stat}=$s
2432                 done
2433         done
2434 }
2435
2436 ni_stat_changed() {
2437         local nidvar=$1
2438         local stat=$2
2439
2440         local pre post
2441         eval pre=\${${nidvar}_pre_${stat}}
2442         eval post=\${${nidvar}_post_${stat}}
2443
2444         echo "${!nidvar} pre ${stat} $pre post ${stat} $post"
2445
2446         [[ $pre -ne $post ]]
2447 }
2448
2449 test_215() {
2450         cleanup_netns || error "Failed to cleanup netns before test execution"
2451         cleanup_lnet || error "Failed to unload modules before test execution"
2452
2453         reinit_dlc || return $?
2454
2455         add_net "${NETTYPE}1" "${INTERFACES[0]}" || return $?
2456         add_net "${NETTYPE}2" "${INTERFACES[0]}" || return $?
2457
2458         local nid1=$($LCTL list_nids | head -n 1)
2459         local nid2=$($LCTL list_nids | tail --lines 1)
2460
2461         do_lnetctl peer add --prim $nid1 --nid $nid2 ||
2462                 error "Failed to add peer"
2463
2464         local npings=25
2465
2466         for nidvarA in nid1 nid2; do
2467                 src=${!nidvarA}
2468                 dst=${!nidvarA}
2469                 for nidvarB in nid1 nid2; do
2470                         [[ $nidvarA == $nidvarB ]] && continue
2471
2472                         ni_stats_pre
2473
2474                         echo "$LNETCTL ping $dst x $npings"
2475                         for i in $(seq 1 $npings); do
2476                                 $LNETCTL ping $dst &>/dev/null ||
2477                                         error "$LNETCTL ping $dst failed"
2478                         done
2479
2480                         ni_stats_post
2481
2482                         # No source specified, sends to either NID should cause
2483                         # counts to increase across both NIs
2484                         for nidvar in nid1 nid2; do
2485                                 for stat in send_count recv_count; do
2486                                         ni_stat_changed $nidvar $stat ||
2487                                                 error "$stat unchanged for ${!nidvar}"
2488                                 done
2489                         done
2490
2491                         ni_stats_pre
2492
2493                         echo "$LNETCTL ping --source $src $dst x $npings"
2494                         for i in $(seq 1 $npings); do
2495                                 $LNETCTL ping --source $src $dst &>/dev/null ||
2496                                         error "$LNETCTL ping --source $src $dst failed"
2497                         done
2498
2499                         ni_stats_post
2500
2501                         # src nid == dest nid means stats for the _other_ NI
2502                         # should be unchanged
2503                         for nidvar in nid1 nid2; do
2504                                 for stat in send_count recv_count; do
2505                                         if [[ ${!nidvar} == $src ]]; then
2506                                                 ni_stat_changed $nidvar $stat ||
2507                                                         error "$stat unchanged for ${!nidvar}"
2508                                         else
2509                                                 ni_stat_changed $nidvar $stat &&
2510                                                         error "$stat changed for ${!nidvar}"
2511                                         fi
2512                                 done
2513                         done
2514                 done
2515                 # Double number of pings for next iteration because the net
2516                 # sequence numbers will have diverged
2517                 npings=$(($npings * 2))
2518         done
2519
2520         # Ping from nid1 to nid2 should fail
2521         do_lnetctl ping --source $nid1 $nid2 &&
2522                 error "ping from $nid1 to $nid2 should fail"
2523
2524         # Ping from nid2 to nid1 should fail
2525         do_lnetctl ping --source $nid2 $nid1 &&
2526                 error "ping from $nid2 to $nid1 should fail"
2527
2528         return 0
2529 }
2530 run_test 215 "Test lnetctl ping --source option"
2531
2532 test_216() {
2533         [[ ${NETTYPE} == kfi* ]] && skip "kfi doesn't support drop rules"
2534
2535         local rc=0
2536
2537         reinit_dlc || return $?
2538
2539         add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?
2540         add_net "${NETTYPE}1" "${INTERFACES[0]}" || return $?
2541
2542         local nids=( $($LCTL list_nids | xargs echo) )
2543
2544         do_lnetctl discover ${nids[0]} ||
2545                 error "Initial discovery failed"
2546
2547         do_lnetctl ping --source ${nids[0]} ${nids[0]} ||
2548                 error "Initial ping failed $?"
2549
2550         do_lnetctl ping --source ${nids[1]} ${nids[1]} ||
2551                 error "Initial ping failed $?"
2552
2553         local src dst
2554         for src in "${nids[@]}"; do
2555                 for dst in "${nids[@]}"; do
2556                         $LCTL net_drop_add -r 1 -s $src -d $dst -e network_timeout
2557                 done
2558         done
2559
2560         do_lnetctl ping ${nids[0]} || rc=$?
2561
2562         $LCTL net_drop_del -a
2563
2564         [[ $rc -eq 0 ]] &&
2565                 error "expected ping to fail"
2566
2567         check_nid_in_recovq "-p" "${nids[0]}" "0"
2568         check_nid_in_recovq "-l" "${nids[0]}" "1"
2569
2570         return 0
2571 }
2572 run_test 216 "Failed send to peer NI owned by local host should not trigger peer NI recovery"
2573
2574 test_217() {
2575         reinit_dlc || return $?
2576
2577         [[ $($LNETCTL net show | grep -c nid) -ne 1 ]] &&
2578                 error "Unexpected number of NIs after initalizing DLC"
2579
2580         do_lnetctl discover 0@lo ||
2581                 error "Failed to discover 0@lo"
2582
2583         unload_modules
2584 }
2585 run_test 217 "Don't leak memory when discovering peer with nnis <= 1"
2586
2587 test_218() {
2588         [[ ${NETTYPE} == kfi* ]] && skip "kfi doesn't support drop rules"
2589
2590         reinit_dlc || return $?
2591
2592         [[ ${#INTERFACES[@]} -lt 2 ]] &&
2593                 skip "Need two LNet interfaces"
2594
2595         add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?
2596
2597         local nid1=$($LCTL list_nids | head -n 1)
2598
2599         do_lnetctl ping $nid1 ||
2600                 error "ping failed"
2601
2602         add_net "${NETTYPE}" "${INTERFACES[1]}" || return $?
2603
2604         local nid2=$($LCTL list_nids | tail --lines 1)
2605
2606         do_lnetctl ping $nid2 ||
2607                 error "ping failed"
2608
2609         $LCTL net_drop_add -s $nid1 -d $nid1 -e local_error -r 1
2610
2611         do_lnetctl ping --source $nid1 $nid1 &&
2612                 error "ping should have failed"
2613
2614         local health_recovered
2615         local i
2616
2617         for i in $(seq 1 5); do
2618                 health_recovered=$($LNETCTL net show -v 2 |
2619                                    grep -c 'health value: 1000')
2620
2621                 if [[ $health_recovered -ne 2 ]]; then
2622                         echo "Wait 1 second for health to recover"
2623                         sleep 1
2624                 else
2625                         break
2626                 fi
2627         done
2628
2629         health_recovered=$($LNETCTL net show -v 2 |
2630                            grep -c 'health value: 1000')
2631
2632         $LCTL net_drop_del -a
2633
2634         [[ $health_recovered -ne 2 ]] &&
2635                 do_lnetctl net show -v 2 | egrep -e nid -e health &&
2636                 error "Health hasn't recovered"
2637
2638         return 0
2639 }
2640 run_test 218 "Local recovery pings should exercise all available paths"
2641
2642 test_219() {
2643         reinit_dlc || return $?
2644         add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?
2645         add_net "${NETTYPE}1" "${INTERFACES[0]}" || return $?
2646
2647         local nid1=$(lctl list_nids | head -n 1)
2648         local nid2=$(lctl list_nids | tail --lines 1)
2649
2650         do_lnetctl ping $nid1 ||
2651                 error "Ping failed $?"
2652         do_lnetctl ping $nid2 ||
2653                 error "Ping failed $?"
2654
2655         do_lnetctl discover $nid2 ||
2656                 error "Discovery failed"
2657
2658         $LNETCTL peer show --nid $nid1 | grep -q $nid2 ||
2659                 error "$nid2 is not listed under $nid1"
2660 }
2661 run_test 219 "Consolidate peer entries"
2662
2663 do_net_add() {
2664         local node=$1
2665         local net=$2
2666         local if=$3
2667
2668         do_rpc_nodes $node "$LNETCTL net add --net $net --if $if" ||
2669                 error "add $net on interface $if on node $node failed rc=$?"
2670 }
2671
2672 do_route_add() {
2673         local node=$1
2674         local net=$2
2675         local gw=$3
2676
2677         do_node $node "$LNETCTL route add --net $net --gateway $gw" ||
2678                 error "route add to $net via $gw failed rc=$?"
2679 }
2680
2681 ROUTER=""
2682 ROUTER_INTERFACES=()
2683 RPEER=""
2684 RPEER_INTERFACES=()
2685 init_router_test_vars() {
2686         local rnodes=$(remote_nodes_list)
2687         [[ -z $rnodes || $(wc -w <<<$rnodes) -lt 2 ]] &&
2688                 skip "Need at least 2 remote nodes found \"$rnodes\""
2689
2690         ROUTER=$(awk '{print $1}' <<<$rnodes)
2691         RPEER=$(awk '{print $2}' <<<$rnodes)
2692
2693         rnodes=$(comma_list $ROUTER $RPEER)
2694         local all_nodes=$(comma_list $rnodes $HOSTNAME)
2695
2696         do_nodes $rnodes $LUSTRE_RMMOD ||
2697                 error "failed to unload modules"
2698
2699         do_rpc_nodes $rnodes "load_lnet config_on_load=1" ||
2700                 error "Failed to load and configure LNet"
2701
2702         ROUTER_INTERFACES=( $(do_rpc_nodes --quiet $ROUTER lnet_if_list) )
2703
2704         RPEER_INTERFACES=( $(do_rpc_nodes --quiet $RPEER lnet_if_list) )
2705
2706         do_nodes $all_nodes $LUSTRE_RMMOD ||
2707                 error "Failed to unload modules"
2708
2709         [[ ${#INTERFACES[@]} -eq 0 ]] &&
2710                 error "No interfaces configured for local host $HOSTNAME"
2711         [[ ${#ROUTER_INTERFACES[@]} -eq 0 ]] &&
2712                 error "No interfaces configured for router $ROUTER"
2713         [[ ${#RPEER_INTERFACES[@]} -eq 0 ]] &&
2714                 error "No interfaces configured for remote peer $RPEER"
2715
2716         return 0
2717 }
2718
2719 ROUTER_NIDS=()
2720 RPEER_NIDS=()
2721 LNIDS=()
2722 LOCAL_NET=${NETTYPE}1
2723 REMOTE_NET=${NETTYPE}2
2724 setup_router_test() {
2725         local mod_opts="$@"
2726
2727         (( $MDS1_VERSION >= $(version_code 2.15.0) )) ||
2728                 skip "need at least 2.15.0 for load_lnet"
2729
2730         if [[ ${#RPEER_INTERFACES[@]} -eq 0 ]]; then
2731                 init_router_test_vars ||
2732                         return $?
2733         fi
2734
2735         local all_nodes=$(comma_list $ROUTER $RPEER $HOSTNAME)
2736
2737         do_nodes $all_nodes $LUSTRE_RMMOD ||
2738                 error "failed to unload modules"
2739
2740         mod_opts+=" alive_router_check_interval=5"
2741         mod_opts+=" router_ping_timeout=5"
2742         mod_opts+=" large_router_buffers=4"
2743         mod_opts+=" small_router_buffers=8"
2744         mod_opts+=" tiny_router_buffers=16"
2745         do_rpc_nodes $all_nodes load_lnet "${mod_opts}" ||
2746                 error "Failed to load lnet"
2747
2748         do_nodes $all_nodes "$LNETCTL lnet configure" ||
2749                 error "Failed to initialize DLC"
2750
2751         do_net_add $ROUTER $LOCAL_NET ${ROUTER_INTERFACES[0]} ||
2752                 return $?
2753
2754         do_net_add $ROUTER $REMOTE_NET ${ROUTER_INTERFACES[0]} ||
2755                 return $?
2756
2757         do_net_add $RPEER $REMOTE_NET ${RPEER_INTERFACES[0]} ||
2758                 return $?
2759
2760         add_net $LOCAL_NET ${INTERFACES[0]} ||
2761                 return $?
2762
2763         ROUTER_NIDS=( $(do_node $ROUTER $LCTL list_nids 2>/dev/null |
2764                         xargs echo) )
2765         RPEER_NIDS=( $(do_node $RPEER $LCTL list_nids 2>/dev/null |
2766                        xargs echo) )
2767         LNIDS=( $($LCTL list_nids 2>/dev/null | xargs echo) )
2768 }
2769
2770 do_route_del() {
2771         local node=$1
2772         local net=$2
2773         local gw=$3
2774
2775         do_nodesv $node "if $LNETCTL route show --net $net --gateway $gw; then \
2776                                 $LNETCTL route del --net $net --gateway $gw;   \
2777                          else                                                  \
2778                                 exit 0;                                        \
2779                          fi"
2780 }
2781
2782 cleanup_router_test() {
2783         local all_nodes=$(comma_list $HOSTNAME $ROUTER $RPEER)
2784
2785         do_route_del $HOSTNAME $REMOTE_NET ${ROUTER_NIDS[0]} ||
2786                 error "Failed to delete $REMOTE_NET route"
2787
2788         do_route_del $RPEER $LOCAL_NET ${ROUTER_NIDS[1]} ||
2789                 error "Failed to delete $LOCAL_NET route"
2790
2791         do_nodes $all_nodes $LUSTRE_RMMOD ||
2792                 error "failed to unload modules"
2793
2794         return 0
2795 }
2796
2797 check_route_aliveness() {
2798         local node="$1"
2799         local expected="$2"
2800
2801         local lctl_actual
2802         local lnetctl_actual
2803         local chk_intvl
2804         local i
2805
2806         chk_intvl=$(cat /sys/module/lnet/parameters/alive_router_check_interval)
2807
2808         lctl_actual=$(do_node $node $LCTL show_route | awk '{print $7}')
2809         lnetctl_actual=$(do_node $node $LNETCTL route show -v |
2810                          awk '/state/{print $NF}')
2811
2812         for ((i = 0; i < $chk_intvl; i++)); do
2813                 if [[ $lctl_actual == $expected ]] &&
2814                    [[ $lnetctl_actual == $expected ]]; then
2815                         break
2816                 fi
2817
2818                 echo "wait 1s for route state change"
2819                 sleep 1
2820
2821                 lctl_actual=$(do_node $node $LCTL show_route | awk '{print $7}')
2822                 lnetctl_actual=$(do_node $node $LNETCTL route show -v |
2823                                  awk '/state/{print $NF}')
2824         done
2825
2826         [[ $lctl_actual != $expected ]] &&
2827                 error "Wanted \"$expected\" lctl found \"$lctl_actual\""
2828
2829         [[ $lnetctl_actual != $expected ]] &&
2830                 error "Wanted \"$expected\" lnetctl found \"$lnetctl_actual\""
2831
2832         return 0
2833 }
2834
2835 check_router_ni_status() {
2836         local expected_local="$1"
2837         local expected_remote="$2"
2838
2839         local actual_local
2840         local actual_remote
2841         local chk_intvl
2842         local timeout
2843         local i
2844
2845         chk_intvl=$(cat /sys/module/lnet/parameters/alive_router_check_interval)
2846         timeout=$(cat /sys/module/lnet/parameters/router_ping_timeout)
2847
2848         actual_local=$(do_node $ROUTER "$LNETCTL net show --net $LOCAL_NET" |
2849                        awk '/status/{print $NF}')
2850         actual_remote=$(do_node $ROUTER "$LNETCTL net show --net $REMOTE_NET" |
2851                         awk '/status/{print $NF}')
2852
2853         for ((i = 0; i < $((chk_intvl + timeout)); i++)); do
2854                 if [[ $actual_local == $expected_local ]] &&
2855                    [[ $actual_remote == $expected_remote ]]; then
2856                         break
2857                 fi
2858
2859                 echo "wait 1s for NI state change"
2860                 sleep 1
2861
2862                 actual_local=$(do_node $ROUTER \
2863                                "$LNETCTL net show --net $LOCAL_NET" |
2864                                 awk '/status/{print $NF}')
2865                 actual_remote=$(do_node $ROUTER \
2866                                 "$LNETCTL net show --net $REMOTE_NET" |
2867                                 awk '/status/{print $NF}')
2868         done
2869
2870         [[ $actual_local == $expected_local ]] ||
2871                 error "$LOCAL_NET should be $expected_local"
2872
2873         [[ $actual_remote == $expected_remote ]] ||
2874                 error "$REMOTE_NET should be $expected_remote"
2875
2876         return 0
2877 }
2878
2879 do_basic_rtr_test() {
2880         do_node $ROUTER "$LNETCTL set routing 1" ||
2881                 error "Unable to enable routing on $ROUTER"
2882
2883         do_route_add $HOSTNAME $REMOTE_NET ${ROUTER_NIDS[0]} ||
2884                 return $?
2885
2886         do_route_add $RPEER $LOCAL_NET ${ROUTER_NIDS[1]} ||
2887                 return $?
2888
2889         check_route_aliveness "$HOSTNAME" "up" ||
2890                 return $?
2891
2892         check_route_aliveness "$RPEER" "up" ||
2893                 return $?
2894
2895         do_lnetctl ping ${RPEER_NIDS[0]} ||
2896                 error "Failed to ping ${RPEER_NIDS[0]}"
2897
2898         do_node $RPEER "$LNETCTL ping ${LNIDS[0]}" ||
2899                 error "$RPEER failed to ping ${LNIDS[0]}"
2900
2901         return 0
2902 }
2903
2904 test_220() {
2905         setup_router_test || return $?
2906
2907         do_basic_rtr_test || return $?
2908
2909         do_rpc_nodes $HOSTNAME,$RPEER load_module ../lnet/selftest/lnet_selftest ||
2910                 error "Failed to load lnet-selftest module"
2911
2912         $LSTSH -H -t $HOSTNAME -f $RPEER -m rw -s 4k ||
2913                 error "lst failed"
2914
2915         $LSTSH -H -t $HOSTNAME -f $RPEER -m rw ||
2916                 error "lst failed"
2917
2918         cleanup_router_test || return $?
2919 }
2920 run_test 220 "Add routes w/default options - check aliveness"
2921
2922 test_221() {
2923         setup_router_test lnet_peer_discovery_disabled=1 || return $?
2924
2925         do_basic_rtr_test || return $?
2926
2927         cleanup_router_test || return $?
2928 }
2929 run_test 221 "Add routes w/DD disabled - check aliveness"
2930
2931 do_aarf_enabled_test() {
2932         do_node $ROUTER "$LNETCTL set routing 1" ||
2933                 error "Unable to enable routing on $ROUTER"
2934
2935         check_router_ni_status "down" "down"
2936
2937         do_lnetctl ping ${RPEER_NIDS[0]} &&
2938                 error "Ping should fail"
2939
2940         do_node $RPEER "$LNETCTL ping ${LNIDS[0]}" &&
2941                 error "$RPEER ping should fail"
2942
2943         # Adding a route should cause the router's NI on LOCAL_NET to get up
2944         do_route_add $HOSTNAME $REMOTE_NET ${ROUTER_NIDS[0]} ||
2945                 return $?
2946
2947         check_router_ni_status "up" "down" ||
2948                 return $?
2949
2950         # But route should still be down because of avoid_asym_router_failure
2951         check_route_aliveness "$HOSTNAME" "down" ||
2952                 return $?
2953
2954         do_lnetctl ping ${RPEER_NIDS[0]} &&
2955                 error "Ping should fail"
2956
2957         do_node $RPEER "$LNETCTL ping ${LNIDS[0]}" &&
2958                 error "$RPEER ping should fail"
2959
2960         # Adding the symmetric route should cause the remote NI to go up and
2961         # routes to go up
2962         do_route_add $RPEER $LOCAL_NET ${ROUTER_NIDS[1]} ||
2963                 return $?
2964
2965         check_router_ni_status "up" "up" ||
2966                 return $?
2967
2968         check_route_aliveness "$HOSTNAME" "up" ||
2969                 return $?
2970
2971         check_route_aliveness "$RPEER" "up" ||
2972                 return $?
2973
2974         do_lnetctl ping ${RPEER_NIDS[0]} ||
2975                 error "Failed to ping ${RPEER_NIDS[0]}"
2976
2977         do_node $RPEER "$LNETCTL ping ${LNIDS[0]}" ||
2978                 error "$RPEER failed to ping ${LNIDS[0]}"
2979
2980         # Stop LNet on local host
2981         do_lnetctl lnet unconfigure ||
2982                 error "Failed to stop LNet rc=$?"
2983
2984         check_router_ni_status "down" "up" ||
2985                 return $?
2986
2987         check_route_aliveness "$RPEER" "down" ||
2988                 return $?
2989
2990         do_lnetctl ping ${RPEER_NIDS[0]} &&
2991                 error "Ping should fail"
2992
2993         do_node $RPEER "$LNETCTL ping ${LNIDS[0]}" &&
2994                 error "$RPEER ping should fail"
2995
2996         return 0
2997 }
2998
2999 test_222() {
3000         setup_router_test avoid_asym_router_failure=1 || return $?
3001
3002         do_aarf_enabled_test || return $?
3003
3004         cleanup_router_test || return $?
3005 }
3006 run_test 222 "Check avoid_asym_router_failure=1"
3007
3008 test_223() {
3009         local opts="avoid_asym_router_failure=1 lnet_peer_discovery_disabled=1"
3010
3011         setup_router_test $opts || return $?
3012
3013         do_aarf_enabled_test || return $?
3014
3015         cleanup_router_test || return $?
3016 }
3017 run_test 223 "Check avoid_asym_router_failure=1 w/DD disabled"
3018
3019 do_aarf_disabled_test() {
3020         do_node $ROUTER "$LNETCTL set routing 1" ||
3021                 error "Unable to enable routing on $ROUTER"
3022
3023         check_router_ni_status "down" "down"
3024
3025         do_route_add $HOSTNAME $REMOTE_NET ${ROUTER_NIDS[0]} ||
3026                 return $?
3027
3028         check_router_ni_status "up" "down" ||
3029                 return $?
3030
3031         check_route_aliveness "$HOSTNAME" "up" ||
3032                 return $?
3033
3034         do_route_add $RPEER $LOCAL_NET ${ROUTER_NIDS[1]} ||
3035                 return $?
3036
3037         check_router_ni_status "up" "up" ||
3038                 return $?
3039
3040         check_route_aliveness "$HOSTNAME" "up" ||
3041                 return $?
3042
3043         check_route_aliveness "$RPEER" "up" ||
3044                 return $?
3045
3046         do_lnetctl ping ${RPEER_NIDS[0]} ||
3047                 error "Failed to ping ${RPEER_NIDS[0]}"
3048
3049         do_node $RPEER "$LNETCTL ping ${LNIDS[0]}" ||
3050                 error "$RPEER failed to ping ${LNIDS[0]}"
3051
3052         # Stop LNet on local host
3053         do_lnetctl lnet unconfigure ||
3054                 error "Failed to stop LNet rc=$?"
3055
3056         check_router_ni_status "down" "up" ||
3057                 return $?
3058
3059         check_route_aliveness "$RPEER" "up" ||
3060                 return $?
3061
3062         return 0
3063 }
3064
3065 test_224() {
3066         setup_router_test avoid_asym_router_failure=0 ||
3067                 return $?
3068
3069         do_aarf_disabled_test ||
3070                 return $?
3071
3072         cleanup_router_test ||
3073                 return $?
3074 }
3075 run_test 224 "Check avoid_asym_router_failure=0"
3076
3077 test_225() {
3078         local opts="avoid_asym_router_failure=0 lnet_peer_discovery_disabled=1"
3079
3080         setup_router_test $opts || return $?
3081
3082         do_aarf_disabled_test || return $?
3083
3084         cleanup_router_test ||
3085                 return $?
3086 }
3087 run_test 225 "Check avoid_asym_router_failure=0 w/DD disabled"
3088
3089 test_230() {
3090         [[ ${NETTYPE} == tcp* ]] ||
3091                 skip "Need tcp NETTYPE"
3092         # LU-12815
3093         echo "Check valid values; Should succeed"
3094         local i
3095         local lnid
3096         local cmd
3097         for ((i = 4; i < 16; i+=1)); do
3098                 reinit_dlc || return $?
3099                 add_net "tcp" "${INTERFACES[0]}" || return $?
3100                 do_lnetctl net set --all --conns-per-peer $i ||
3101                         error "should have succeeded $?"
3102                 $LNETCTL net show -v 1 | grep -q "conns_per_peer: $i" ||
3103                         error "failed to set conns-per-peer to $i"
3104                 lnid="$(lctl list_nids | head -n 1)"
3105                 do_lnetctl ping "$lnid" ||
3106                         error "failed to ping myself"
3107
3108                 # "lctl --net tcp conn_list" prints the list of active
3109                 # connections. Since we're pinging ourselves, there should be
3110                 # 2 Control connections plus 2*conns_per_peer connections
3111                 # created (one Bulk Input, one Bulk Output in each pair).
3112                 # Here's the sample output for conns_per_peer set to 1:
3113                 # 12345-1.1.1.1@tcp I[0]host01->host01:988 2626560/1061296 nonagle
3114                 # 12345-1.1.1.1@tcp O[0]host01->host01:1022 2626560/1061488 nonagle
3115                 # 12345-1.1.1.1@tcp C[0]host01->host01:988 2626560/1061296 nonagle
3116                 # 12345-1.1.1.1@tcp C[0]host01->host01:1023 2626560/1061488 nonagle
3117                 cmd="printf 'network tcp\nconn_list\n' | lctl | grep -c '$lnid'"
3118
3119                 # Expect 2+conns_per_peer*2 connections. Wait no longer
3120                 # than 2 seconds.
3121                 wait_update $HOSTNAME "$cmd" "$((2+i*2))" 2 ||
3122                         error "expected number of tcp connections $((2+i*2))"
3123         done
3124
3125         reinit_dlc || return $?
3126         add_net "tcp" "${INTERFACES[0]}" || return $?
3127         echo "Set > 127; Should fail"
3128         do_lnetctl net set --all --conns-per-peer 128 &&
3129                 error "should have failed $?"
3130
3131         reinit_dlc || return $?
3132         add_net "tcp" "${INTERFACES[0]}" || return $?
3133
3134         local default=$($LNETCTL net show -v 1 |
3135                         awk '/conns_per_peer/{print $NF}')
3136
3137         echo "Set < 0; Should be ignored"
3138         do_lnetctl net set --all --conns-per-peer -1 ||
3139                 error "should have succeeded $?"
3140         $LNETCTL net show -v 1 | grep -q "conns_per_peer: ${default}" ||
3141                 error "Did not stay at default"
3142 }
3143 run_test 230 "Test setting conns-per-peer"
3144
3145 test_231() {
3146         reinit_dlc || return $?
3147
3148         local net=${NETTYPE}231
3149
3150         do_lnetctl net add --net $net --if ${INTERFACES[0]} ||
3151                 error "Failed to add net"
3152
3153         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml
3154         sed -i 's/peer_timeout: .*$/peer_timeout: 0/' \
3155                 $TMP/sanity-lnet-$testnum-expected.yaml
3156
3157         reinit_dlc || return $?
3158
3159         do_lnetctl import $TMP/sanity-lnet-$testnum-expected.yaml ||
3160                 error "Failed to import configuration"
3161
3162         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
3163
3164         compare_yaml_files || error "Wrong config after import"
3165
3166         do_lnetctl net del --net $net --if ${INTERFACES[0]} ||
3167                 error "Failed to delete net $net"
3168
3169         do_lnetctl net add --net $net --if ${INTERFACES[0]} --peer-timeout=0 ||
3170                 error "Failed to add net with peer-timeout=0"
3171
3172         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
3173
3174         compare_yaml_files || error "Wrong config after lnetctl net add"
3175
3176         reinit_dlc || return $?
3177
3178         # lnet/include/lnet/lib-lnet.h defines DEFAULT_PEER_TIMEOUT 180
3179         sed -i 's/peer_timeout: .*$/peer_timeout: 180/' \
3180                 $TMP/sanity-lnet-$testnum-expected.yaml
3181
3182         sed -i '/^.*peer_timeout:.*$/d' $TMP/sanity-lnet-$testnum-actual.yaml
3183
3184         do_lnetctl import $TMP/sanity-lnet-$testnum-actual.yaml ||
3185                 error "Failed to import config without peer_timeout"
3186
3187         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
3188
3189         compare_yaml_files
3190 }
3191 run_test 231 "Check DLC handling of peer_timeout parameter"
3192
3193 ### Test that linux route is added for each ni
3194 test_250() {
3195         local skip_param
3196
3197         [[ ${NETTYPE} == tcp* ]] ||
3198                 skip "Need tcp NETTYPE"
3199         reinit_dlc || return $?
3200         add_net "tcp" "${INTERFACES[0]}" || return $?
3201
3202         skip_param=$(cat /sys/module/ksocklnd/parameters/skip_mr_route_setup)
3203         [[ ${skip_param:-0} -ne 0 ]] &&
3204                 skip "Need skip_mr_route_setup=0 found $skip_param"
3205
3206         ip route show table ${INTERFACES[0]} | grep -q "${INTERFACES[0]}"
3207 }
3208 run_test 250 "test that linux routes are added"
3209
3210 test_251() {
3211         [[ ${NETTYPE} =~ kfi* ]] ||
3212                 skip "Need kfi NETTYPE"
3213
3214         reinit_dlc || return $?
3215         add_net "kfi" "${INTERFACES[0]}" || return $?
3216         add_net "kfi1" "${INTERFACES[0]}" || return $?
3217         add_net "kfi10" "${INTERFACES[0]}" || return $?
3218         return 0
3219 }
3220 run_test 251 "Define multiple kfi networks on single interface"
3221
3222 test_252() {
3223         setup_health_test false || return $?
3224
3225         local rc=0
3226
3227         do_rpc_nodes $RNODE unload_modules_local || rc=$?
3228
3229         if [[ $rc -ne 0 ]]; then
3230                 cleanup_health_test || return $?
3231
3232                 error "Failed to unload modules on $RNODE rc=$rc"
3233         else
3234                 RLOADED=false
3235         fi
3236
3237         local ts1=$(date +%s)
3238
3239         do_lnetctl ping --timeout 15 ${RNIDS[0]} &&
3240                 error "Expected ping ${RNIDS[0]} to fail"
3241
3242         local ts2=$(date +%s)
3243
3244         local delta=$(echo "$ts2 - $ts1" | bc)
3245
3246         [[ $delta -lt 15 ]] ||
3247                 error "Ping took longer than expected to fail: $delta"
3248
3249         cleanup_health_test
3250 }
3251 run_test 252 "Ping to down peer should unlink quickly"
3252
3253 do_expired_message_drop_test() {
3254         local rnid lnid old_tto
3255
3256         old_tto=$($LNETCTL global show |
3257                   awk '/transaction_timeout:/{print $NF}')
3258
3259         [[ -z $old_tto ]] &&
3260                 error "Cannot determine LNet transaction timeout"
3261
3262         local tto=10
3263
3264         do_lnetctl set transaction_timeout "${tto}" ||
3265                 error "Failed to set transaction_timeout"
3266
3267         # We want to consume all peer credits for at least transaction_timeout
3268         # seconds
3269         local delay
3270
3271         delay=$((tto + 1))
3272
3273         for lnid in "${LNIDS[@]}"; do
3274                 for rnid in "${RNIDS[@]}"; do
3275                         $LCTL net_delay_add -s "${lnid}" -d "${rnid}" \
3276                                 -l "${delay}" -r 1 -m GET
3277                 done
3278         done
3279
3280         declare -a pcs
3281
3282         pcs=( $($LNETCTL peer show -v --nid "${RNIDS[0]}" |
3283                 awk '/max_ni_tx_credits:/{print $NF}' |
3284                 xargs echo) )
3285
3286         [[ ${#RNIDS[@]} -ne ${#pcs[@]} ]] &&
3287                 error "Expect ${#RNIDS[@]} peer credit values found ${#pcs[@]}"
3288
3289         local rnet lnid lnet i j
3290
3291         # Need to use --source for multi-rail configs to ensure we consume
3292         # all available peer credits
3293         for ((i = 0; i < ${#RNIDS[@]}; i++)); do
3294                 local ping_args="--timeout $((delay+2))"
3295
3296                 rnet=${RNIDS[i]##*@}
3297                 for lnid in ${LNIDS[@]}; do
3298                         lnet=${lnid##*@}
3299                         [[ $rnet == $lnet ]] && break
3300                 done
3301
3302                 ping_args+=" --source ${lnid} ${RNIDS[i]}"
3303                 for j in $(seq 1 "${pcs[i]}"); do
3304                         $LNETCTL ping ${ping_args} 1>/dev/null &
3305                 done
3306
3307                 echo "Issued ${pcs[i]} pings to ${RNIDS[i]} from $lnid"
3308         done
3309
3310         # This ping should be queued on peer NI tx credit
3311         $LNETCTL ping --timeout $((delay+2)) "${RNIDS[0]}" &
3312
3313         sleep ${delay}
3314
3315         $LCTL net_delay_del -a
3316
3317         wait
3318
3319         # Messages sent from the delay list do not go through
3320         # lnet_post_send_locked(), thus we should only have a single drop
3321         local dropped
3322
3323         dropped=$($LNETCTL peer show -v 2 --nid "${RNIDS[0]}" |
3324                         grep -A 2 dropped_stats |
3325                         awk '/get:/{print $2}' |
3326                         xargs echo |
3327                         sed 's/ /\+/g' | bc)
3328
3329         [[ $dropped -ne 1 ]] &&
3330                 error "Expect 1 dropped GET but found $dropped"
3331
3332         do_lnetctl set transaction_timeout "${old_tto}"
3333
3334         return 0
3335 }
3336
3337 test_253() {
3338         setup_health_test false || return $?
3339
3340         do_expired_message_drop_test || return $?
3341
3342         cleanup_health_test
3343 }
3344 run_test 253 "Message delayed beyond deadline should be dropped (single-rail)"
3345
3346 test_254() {
3347         setup_health_test true || return $?
3348
3349         do_expired_message_drop_test || return $?
3350
3351         cleanup_health_test
3352 }
3353 run_test 254 "Message delayed beyond deadline should be dropped (multi-rail)"
3354
3355 test_255() {
3356         [[ ${NETTYPE} == tcp* ]] || skip "Need tcp NETTYPE"
3357
3358         reinit_dlc || return $?
3359
3360         cleanup_lnet || return $?
3361
3362         local routes_str="o2ib ${IF0_NET}.[$GW_HOSTNUM-$((GW_HOSTNUM+4))]"
3363         local network_str="${NETTYPE}(${INTERFACES[0]})"
3364
3365         load_lnet "networks=\"${network_str}\" routes=\"${routes_str}\"" ||
3366                 error "Failed to load LNet"
3367
3368         $LCTL net up ||
3369                 error "Failed to load LNet with networks=\"${network_str}\" routes=\"${routes_str}\""
3370
3371         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
3372 net:
3373     - net type: ${NETTYPE}
3374       local NI(s):
3375         - interfaces:
3376               0: ${INTERFACES[0]}
3377 EOF
3378         append_net_tunables tcp
3379
3380         echo "route:" >> $TMP/sanity-lnet-$testnum-expected.yaml
3381         for i in $(seq $GW_HOSTNUM $((GW_HOSTNUM + 4))); do
3382                 cat <<EOF >> $TMP/sanity-lnet-$testnum-expected.yaml
3383     - net: o2ib
3384       gateway: ${IF0_NET}.${i}@${NETTYPE}
3385       hop: -1
3386       priority: 0
3387       health_sensitivity: 1
3388 EOF
3389         done
3390
3391         echo "peer:" >> $TMP/sanity-lnet-$testnum-expected.yaml
3392         for i in $(seq $GW_HOSTNUM $((GW_HOSTNUM + 4))); do
3393                 cat <<EOF >> $TMP/sanity-lnet-$testnum-expected.yaml
3394     - primary nid: ${IF0_NET}.${i}@${NETTYPE}
3395       Multi-Rail: False
3396       peer ni:
3397         - nid: ${IF0_NET}.${i}@${NETTYPE}
3398 EOF
3399         done
3400
3401         append_global_yaml
3402
3403         $LNETCTL export --backup >  $TMP/sanity-lnet-$testnum-actual.yaml ||
3404                 error "export failed $?"
3405
3406         validate_gateway_nids
3407 }
3408 run_test 255 "Use lnet routes param with pdsh syntax"
3409
3410 test_300() {
3411         # LU-13274
3412         local header
3413         local out=$TMP/$tfile
3414         local prefix=/usr/include/linux/lnet
3415
3416         # We use a hard coded prefix so that this test will not fail
3417         # when run in tree.
3418         CC=${CC:-cc}
3419         if ! which $CC > /dev/null 2>&1; then
3420                 skip_env "$CC is not installed"
3421         fi
3422
3423         cleanup_lnet || exit 1
3424         load_lnet
3425
3426         local cc_args="-Wall -Werror -std=c99 -c -x c /dev/null -o $out"
3427         if ! [[ -d $prefix ]]; then
3428                 # Assume we're running in tree and fixup the include path.
3429                 prefix=$LUSTRE/../lnet/include/uapi/linux/lnet
3430                 cc_args+=" -I $LUSTRE/../lnet/include/uapi"
3431         fi
3432
3433         for header in $prefix/*.h; do
3434                 if ! [[ -f "$header" ]]; then
3435                         continue
3436                 fi
3437
3438                 echo "$CC $cc_args -include $header"
3439                 $CC $cc_args -include $header ||
3440                         error "cannot compile '$header'"
3441         done
3442         rm -f $out
3443 }
3444 run_test 300 "packaged LNet UAPI headers can be compiled"
3445
3446 # LU-16081 lnet: Memory leak on adding existing interface
3447
3448 test_301() {
3449         reinit_dlc || return $?
3450         do_lnetctl net add --net ${NETTYPE} --if ${INTERFACES[0]} ||
3451                 error "Failed to add net"
3452         do_lnetctl net add --net ${NETTYPE} --if ${INTERFACES[0]} &&
3453                 error "add net should have failed"
3454         do_lnetctl net del --net ${NETTYPE} --if ${INTERFACES[0]} ||
3455                 error "Failed to del net"
3456         unload_modules
3457 }
3458 run_test 301 "Check for dynamic adds of same/wrong interface (memory leak)"
3459
3460 test_302() {
3461         ! [[ $NETTYPE =~ (tcp|o2ib) ]] && skip "Need tcp or o2ib NETTYPE"
3462         reinit_dlc || return $?
3463
3464         add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?
3465
3466         local nid=$($LCTL list_nids)
3467
3468         do_lnetctl ping ${nid} ||
3469                 error "pinging self failed $?"
3470         do_lnetctl debug peer --nid ${nid} ||
3471                 error "failed to dump peer debug info $?"
3472 }
3473 run_test 302 "Check that peer debug info can be dumped"
3474
3475 test_303() {
3476         [[ ${NETTYPE} == tcp* ]] || skip "Need tcp NETTYPE"
3477
3478         setup_health_test true || return $?
3479
3480         cleanup_netns || error "Failed to cleanup netns before test execution"
3481         setup_fakeif || error "Failed to add fake IF"
3482         have_interface "$FAKE_IF" ||
3483                 error "Expect $FAKE_IF configured but not found"
3484
3485         add_net "${NETTYPE}99" "$FAKE_IF" || return $?
3486
3487         local nid=$($LCTL list_nids | tail --lines 1)
3488
3489         # Our updated config should be pushed to RNODE
3490         local found=$(do_node $RNODE "$LNETCTL peer show --nid $nid")
3491
3492         [[ -z $found ]] && error "Peer not updated on $RNODE"
3493
3494         local prim=$($LCTL list_nids | head -n 1)
3495
3496         if ! grep -q -- " primary nid: $prim"<<<"${found}"; then
3497                 echo "$found"
3498                 error "Wrong primary nid"
3499         fi
3500
3501         echo "Set $FAKE_IF down"
3502         echo "ip link set dev $FAKE_IF down"
3503         ip link set dev $FAKE_IF down
3504         check_ni_status "$nid" down
3505
3506         local hval=$(do_node $RNODE "$LNETCTL peer show --nid $nid -v 2 | \
3507                                      grep -e '- nid:' -e 'health value:'")
3508
3509         hval=$(grep -A 1 $nid<<<"$hval" | tail -n 1 | awk '{print $NF}')
3510         (( hval < 1000 )) ||
3511                 error "Expect $hval < 1000"
3512
3513         return 0
3514 }
3515 run_test 303 "Check peer NI health after link down"
3516
3517 test_304() {
3518         [[ ${NETTYPE} == tcp* ]] || skip "Need tcp NETTYPE"
3519
3520         cleanup_netns || error "Failed to cleanup netns before test execution"
3521         cleanup_lnet || error "Failed to unload modules before test execution"
3522
3523         setup_fakeif || error "Failed to add fake IF"
3524         have_interface "$FAKE_IF" ||
3525                 error "Expect $FAKE_IF configured but not found"
3526
3527         reinit_dlc || return $?
3528
3529         add_net "tcp" "${INTERFACES[0]}" || return $?
3530         add_net "tcp" "$FAKE_IF" || return $?
3531
3532         local nid1=$(lctl list_nids | head -n 1)
3533         local nid2=$(lctl list_nids | tail --lines 1)
3534
3535         check_ni_status "$nid1" up
3536         check_ni_status "$nid2" up
3537
3538         do_lnetctl peer add --prim_nid ${nid2} --lock_prim ||
3539                 error "peer add failed $?"
3540         local locked_peer_state=($(do_lnetctl peer show -v 4 --nid ${nid2} |
3541                 awk '/peer state/{print $NF}'))
3542
3543         # Expect peer state bits:
3544         #   LNET_PEER_MULTI_RAIL(0) | LNET_PEER_CONFIGURED(3) |
3545         #   LNET_PEER_LOCK_PRIMARY(20)
3546         (( $locked_peer_state != "1048585")) &&
3547                 error "Wrong peer state \"$locked_peer_state\" expected 1048585"
3548
3549         # Clear LNET_PEER_CONFIGURED bit and verify
3550         do_lnetctl peer set --nid ${nid2} --state 1048577 ||
3551                 error "peer add failed $?"
3552         locked_peer_state=($(do_lnetctl peer show -v 4 --nid ${nid2} |
3553                 awk '/peer state/{print $NF}'))
3554         (( $locked_peer_state != "1048577")) &&
3555                 error "Wrong peer state \"$locked_peer_state\" expected 1048577"
3556         do_lnetctl discover ${nid1} ||
3557                 error "Failed to discover peer"
3558
3559         # Expect nid2 and nid1 peer entries to be consolidated,
3560         # nid2 to stay primary
3561         cat <<EOF >> $TMP/sanity-lnet-$testnum-expected.yaml
3562 peer:
3563     - primary nid: ${nid2}
3564       Multi-Rail: True
3565       peer ni:
3566         - nid: ${nid1}
3567           state: NA
3568         - nid: ${nid2}
3569           state: NA
3570 EOF
3571         $LNETCTL peer show > $TMP/sanity-lnet-$testnum-actual.yaml
3572         compare_yaml_files ||
3573                 error "Unexpected peer configuration"
3574
3575         locked_peer_state=($(do_lnetctl peer show -v 4 --nid ${nid2} |
3576                 awk '/peer state/{print $NF}'))
3577         # Expect peer state bits to be added:
3578         #   LNET_PEER_DISCOVERED(4) | LNET_PEER_NIDS_UPTODATE(8)
3579         (( $locked_peer_state != "1048849")) &&
3580                 error "Wrong peer state \"$locked_peer_state\" expected 1048849"
3581         return 0
3582 }
3583 run_test 304 "Check locked primary peer nid consolidation"
3584
3585 check_parameter() {
3586         local para=$1
3587         local value=$2
3588
3589         echo "check parameter ${para} value ${value}"
3590
3591         return $(( $(do_lnetctl net show -v | \
3592                      tee /dev/stderr | \
3593                      grep -c "^ \+${para}: ${value}$") != ${#INTERFACES[@]} ))
3594 }
3595
3596 static_config() {
3597         local module=$1
3598         local setting=$2
3599
3600         cleanup_lnet || error "Failed to cleanup LNet"
3601
3602         load_module ../libcfs/libcfs/libcfs ||
3603                 error "Failed to load module libcfs rc = $?"
3604
3605         load_module ../lnet/lnet/lnet ||
3606                 error "Failed to load module lnet rc = $?"
3607
3608         echo "loading ${module} ${setting} type ${NETTYPE}"
3609         load_module "${module}" "${setting}" ||
3610                 error "Failed to load module ${module} rc = $?"
3611
3612         do_lnetctl lnet configure --all || error "lnet configure failed rc = $?"
3613
3614         return 0
3615 }
3616
3617 test_310() {
3618         local value=65
3619
3620         if [[ ${NETTYPE} == tcp* ]];then
3621                 static_config "../lnet/klnds/socklnd/ksocklnd" \
3622                               "sock_timeout=${value}"
3623         elif [[ ${NETTYPE} == o2ib* ]]; then
3624                 static_config "../lnet/klnds/o2iblnd/ko2iblnd" \
3625                               "timeout=${value}"
3626         elif [[ ${NETTYPE} == gni* ]]; then
3627                 static_config "../lnet/klnds/gnilnd/kgnilnd" \
3628                               "timeout=${value}"
3629         else
3630                 skip "NETTYPE ${NETTYPE} not supported"
3631         fi
3632
3633         check_parameter "timeout" $value
3634
3635         return $?
3636 }
3637 run_test 310 "Set timeout and verify"
3638
3639 test_311() {
3640         [[ $NETTYPE == kfi* ]] ||
3641                 skip "Need kfi network type"
3642
3643         setupall || error "setupall failed"
3644
3645         mkdir -p $DIR/$tdir || error "mkdir failed"
3646         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 oflag=direct ||
3647                 error "dd write failed"
3648
3649         local list=$(comma_list $(osts_nodes))
3650
3651 #define CFS_KFI_FAIL_WAIT_SEND_COMP1 0xF115
3652         do_nodes $list $LCTL set_param fail_loc=0x8000F115
3653         dd if=$DIR/$tdir/$tfile of=/dev/null bs=1M count=1 ||
3654                 error "dd read failed"
3655
3656         rm -f $DIR/$tdir/$tfile
3657         rmdir $DIR/$tdir
3658
3659         cleanupall || error "Failed cleanup"
3660 }
3661 run_test 311 "Fail bulk put in send wait completion"
3662
3663 test_312() {
3664         [[ $NETTYPE == kfi* ]] ||
3665                 skip "Need kfi network type"
3666
3667         setupall || error "setupall failed"
3668
3669         mkdir -p $DIR/$tdir || error "mkdir failed"
3670
3671         local list=$(comma_list $(osts_nodes))
3672
3673 #define CFS_KFI_FAIL_WAIT_SEND_COMP3 0xF117
3674         do_nodes $list $LCTL set_param fail_loc=0x8000F117
3675         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 oflag=direct ||
3676                 error "dd write failed"
3677
3678         local tfile2="$DIR/$tdir/testfile2"
3679
3680         do_nodes $list $LCTL set_param fail_loc=0x8000F117
3681         dd if=$DIR/$tdir/$tfile of=$tfile2 bs=1M count=1 oflag=direct ||
3682                 error "dd read failed"
3683
3684         rm -f $DIR/$tdir/$tfile
3685         rm -f $tfile2
3686         rmdir $DIR/$tdir
3687
3688         cleanupall || error "Failed cleanup"
3689 }
3690 run_test 312 "TAG_RX_OK is possible after TX_FAIL"
3691
3692 check_udsp_prio() {
3693         local target_net="${1}"
3694         local target_nid="${2}"
3695         local expect_net="${3}"
3696         local expect_nid="${4}"
3697         local type="${5}"
3698
3699         declare -a nids
3700         declare -a net_prios
3701         declare -a nid_prios
3702
3703         nids=( $($LNETCTL ${type} show -v 5 | awk '/-\s+nid:/{print $NF}' |
3704                  xargs echo) )
3705
3706         net_prios=( $($LNETCTL ${type} show -v 5 |
3707                       awk '/net priority:/{print $NF}' | xargs echo) )
3708
3709         nid_prios=( $($LNETCTL ${type} show -v 5 |
3710                       awk '/nid priority:/{print $NF}' | xargs echo) )
3711
3712         (( ${#nids[@]} != ${#net_prios[@]} )) &&
3713                 error "Wrong # net prios ${#nids[@]} != ${#net_prios[@]}"
3714
3715         (( ${#nids[@]} != ${#nid_prios[@]} )) &&
3716                 error "Wrong # nid prios ${#nids[@]} != ${#nid_prios[@]}"
3717
3718         local i
3719
3720         for ((i = 0; i < ${#nids[@]}; i++)); do
3721                 [[ -n ${target_net} ]] &&
3722                         [[ ${nids[i]##*@} != "${target_net}" ]] &&
3723                         continue
3724                 [[ -n ${target_nid} ]] &&
3725                         [[ ${nids[i]} != "${target_nid}" ]] &&
3726                         continue
3727
3728                 echo "${nids[i]}: net_prio ${net_prios[i]} expect ${expect_net}"
3729                 (( net_prios[i] != expect_net )) &&
3730                         error "Wrong net priority \"${net_prios[i]}\" expect ${expect_net}"
3731
3732                 echo "${nids[i]}: nid_prio ${nid_prios[i]} expect ${expect_nid}"
3733                 (( nid_prios[i] != expect_nid )) &&
3734                         error "Wrong nid priority \"${nid_prios[i]}\" expect ${expect_nid}"
3735         done
3736
3737         return 0
3738 }
3739
3740 check_peer_udsp_prio() {
3741         check_udsp_prio "${1}" "${2}" "${3}" "${4}" "peer"
3742 }
3743
3744 check_net_udsp_prio() {
3745         check_udsp_prio "${1}" "${2}" "${3}" "${4}" "net"
3746 }
3747
3748 test_400() {
3749         reinit_dlc || return $?
3750
3751         do_lnetctl udsp add --src tcp --priority 0 ||
3752                 error "Failed to add udsp rule"
3753         do_lnetctl udsp del --idx 0 ||
3754                 error "Failed to del udsp rule"
3755         unload_modules
3756 }
3757 run_test 400 "Check for udsp add/delete net rule without net num"
3758
3759 test_401() {
3760         reinit_dlc || return $?
3761
3762         do_lnetctl net add --net ${NETTYPE} --if ${INTERFACES[0]} ||
3763                 error "Failed to add net"
3764
3765         do_lnetctl udsp add --dst ${NETTYPE} --prio 1 ||
3766                 error "Failed to add peer net priority rule"
3767
3768         do_lnetctl discover $($LCTL list_nids | head -n 1) ||
3769                 error "Failed to discover peer"
3770
3771         check_peer_udsp_prio "${NETTYPE}" "" "1" "-1"
3772
3773         return 0
3774 }
3775 run_test 401 "Discover peer after adding peer net UDSP rule"
3776
3777 test_402() {
3778         reinit_dlc || return $?
3779
3780         do_lnetctl udsp add --dst kfi --priority 0 ||
3781                 error "Failed to add UDSP rule"
3782
3783         do_lnetctl peer add --prim 402@kfi ||
3784                 error "Failed to add peer"
3785
3786         return 0
3787 }
3788 run_test 402 "Destination net rule should not panic"
3789
3790 test_500() {
3791         reinit_dlc || return $?
3792
3793         cleanup_netns || error "Failed to cleanup netns before test execution"
3794         setup_fakeif || error "Failed to add fake IF"
3795         have_interface "$FAKE_IF" ||
3796                 error "Expect $FAKE_IF configured but not found"
3797
3798         add_net "tcp" "${INTERFACES[0]}"
3799         add_net "tcp" "${FAKE_IF}"
3800
3801         do_lnetctl discover $($LCTL list_nids | head -n 1) ||
3802                 error "Failed to discover self"
3803
3804         $LCTL net_delay_add -s *@tcp -d *@tcp -r 1 -l 1 -m PUT ||
3805                 error "Failed to add delay rule"
3806
3807         $LCTL net_drop_add -s *@tcp -d $($LCTL list_nids | head -n 1) -m PUT -e local_timeout -r 1
3808         $LCTL net_drop_add -s *@tcp -d $($LCTL list_nids | tail -n 1) -m PUT -e local_timeout -r 1
3809
3810         ip link set $FAKE_IF down ||
3811                 error "Failed to set link down"
3812         ip link set $FAKE_IF up ||
3813                 error "Failed to set link up"
3814
3815         unload_modules
3816 }
3817 run_test 500 "Check deadlock on ping target update"
3818
3819 complete_test $SECONDS
3820 cleanup_testsuite
3821 exit_status