Whamcloud - gitweb
LU-15595 tests: Add various router tests
[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 do_ns() {
69         echo "ip netns exec $TESTNS $*"
70         ip netns exec $TESTNS "$@"
71 }
72
73 setup_fakeif() {
74         local netns="$1"
75
76         local netns_arg=""
77         [[ -n $netns ]] &&
78                 netns_arg="netns $netns"
79
80         ip link add 'test1pl' type veth peer name $FAKE_IF $netns_arg
81         ip link set 'test1pl' up
82         if [[ -n $netns ]]; then
83                 do_ns ip addr add "${FAKE_IP}/31" dev $FAKE_IF
84                 do_ns ip link set $FAKE_IF up
85         else
86                 ip addr add "${FAKE_IP}/31" dev $FAKE_IF
87                 ip link set $FAKE_IF up
88         fi
89 }
90
91 cleanup_fakeif() {
92         ip link show test1pl >& /dev/null && ip link del test1pl || return 0
93 }
94
95 setup_netns() {
96         cleanup_netns
97
98         ip netns add $TESTNS
99         setup_fakeif $TESTNS
100 }
101
102 cleanup_netns() {
103         (ip netns list | grep -q $TESTNS) && ip netns del $TESTNS
104         cleanup_fakeif
105 }
106
107 configure_dlc() {
108         echo "Loading LNet and configuring DLC"
109         load_lnet
110         do_lnetctl lnet configure
111 }
112
113 GLOBAL_YAML_FILE=$TMP/sanity-lnet-global.yaml
114 define_global_yaml() {
115         $LNETCTL export --backup >${GLOBAL_YAML_FILE} ||
116                 error "Failed to export global yaml $?"
117 }
118
119 reinit_dlc() {
120         if lsmod | grep -q lnet; then
121                 do_lnetctl lnet unconfigure ||
122                         error "lnetctl lnet unconfigure failed $?"
123                 do_lnetctl lnet configure ||
124                         error "lnetctl lnet configure failed $?"
125         else
126                 configure_dlc || error "configure_dlc failed $?"
127         fi
128         define_global_yaml
129 }
130
131 append_global_yaml() {
132         [[ ! -e ${GLOBAL_YAML_FILE} ]] &&
133                 error "Missing global yaml at ${GLOBAL_YAML_FILE}"
134
135         cat ${GLOBAL_YAML_FILE} >> $TMP/sanity-lnet-$testnum-expected.yaml
136 }
137
138 create_base_yaml_file() {
139         append_global_yaml
140 }
141
142 compare_yaml_files() {
143         local expected="$TMP/sanity-lnet-$testnum-expected.yaml"
144         local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
145         local rc=0
146         ! [[ -e $expected ]] && echo "$expected not found" && return 1
147         ! [[ -e $actual ]] && echo "$actual not found" && return 1
148         diff -upN ${actual} ${expected} || rc=$?
149         echo "Expected:"
150         cat $expected
151         echo "Actual:"
152         cat $actual
153         return $rc
154 }
155
156 validate_nid() {
157         local nid="$1"
158         local net="${nid//*@/}"
159         local addr="${nid//@*/}"
160
161         local num_re='[0-9]\+'
162         local ip_re="[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
163
164         if [[ $net =~ gni[0-9]* ]] || [[ $net =~ kfi[0-9]* ]]; then
165                 [[ $addr =~ ${num_re} ]] && return 0
166         else
167                 [[ $addr =~ ${ip_re} ]] && return 0
168         fi
169 }
170
171 validate_nids() {
172         local yfile=$TMP/sanity-lnet-$testnum-actual.yaml
173         local primary_nids=$(awk '/- primary nid:/{print $NF}' $yfile | xargs echo)
174         local secondary_nids=$(awk '/- nid:/{print $NF}' $yfile | xargs echo)
175         local gateway_nids=$(awk '/gateway:/{print $NF}' $yfile | xargs echo)
176
177         local nid
178         for nid in $primary_nids $secondary_nids; do
179                 validate_nid "$nid" || error "Bad NID \"${nid}\""
180         done
181         return 0
182 }
183
184 validate_peer_nids() {
185         local num_peers="$1"
186         local nids_per_peer="$2"
187
188         local expect_p="$num_peers"
189         # The primary nid also shows up in the list of secondary nids
190         local expect_s="$(($num_peers + $(($nids_per_peer*$num_peers))))"
191
192         local actual_p=$(grep -c -- '- primary nid:' $TMP/sanity-lnet-$testnum-actual.yaml)
193         local actual_s=$(grep -c -- '- nid:' $TMP/sanity-lnet-$testnum-actual.yaml)
194         if [[ $expect_p -ne $actual_p ]]; then
195                 compare_yaml_files
196                 error "Expected $expect_p but found $actual_p primary nids"
197         elif [[ $expect_s -ne $actual_s ]]; then
198                 compare_yaml_files
199                 error "Expected $expect_s but found $actual_s secondary nids"
200         fi
201         validate_nids
202 }
203
204 validate_gateway_nids() {
205         local expect_gw=$(grep -c -- 'gateway:' $TMP/sanity-lnet-$testnum-expected.yaml)
206         local actual_gw=$(grep -c -- 'gateway:' $TMP/sanity-lnet-$testnum-actual.yaml)
207         if [[ $expect_gw -ne $actual_gw ]]; then
208                 compare_yaml_files
209                 error "Expected $expect_gw gateways but found $actual_gw gateways"
210         fi
211         validate_nids
212 }
213
214 cleanupall -f
215 setup_netns || error "setup_netns failed with $?"
216
217 # Determine the local interface(s) used for LNet
218 load_lnet "config_on_load=1" || error "Failed to load modules"
219
220 do_lnetctl net show
221 ip a
222
223 INTERFACES=( $(lnet_if_list) )
224
225 cleanup_lnet || error "Failed to cleanup LNet"
226
227 stack_trap 'cleanup_testsuite' EXIT
228
229 test_0() {
230         load_module ../lnet/lnet/lnet || error "Failed to load module rc = $?"
231         do_lnetctl lnet configure || error "lnet configure failed rc = $?"
232         define_global_yaml
233         reinit_dlc || return $?
234         do_lnetctl import <  ${GLOBAL_YAML_FILE} || error "Import failed $?"
235         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
236         create_base_yaml_file
237         compare_yaml_files || error "Configuration changed after import"
238 }
239 run_test 0 "Export empty config, import the config, compare"
240
241 compare_peer_add() {
242         local prim_nid="${1:+--prim_nid $1}"
243         local nid="${2:+--nid $2}"
244
245         local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
246
247         do_lnetctl peer add ${prim_nid} ${nid} || error "peer add failed $?"
248         $LNETCTL export --backup > $actual || error "export failed $?"
249         compare_yaml_files
250         return $?
251 }
252
253 test_1() {
254         reinit_dlc || return $?
255         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
256 peer:
257     - primary nid: 1.1.1.1@tcp
258       Multi-Rail: True
259       peer ni:
260         - nid: 1.1.1.1@tcp
261 EOF
262         append_global_yaml
263         compare_peer_add "1.1.1.1@tcp"
264 }
265 run_test 1 "Add peer with single nid (tcp)"
266
267 test_2() {
268         reinit_dlc || return $?
269         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
270 peer:
271     - primary nid: 2.2.2.2@o2ib
272       Multi-Rail: True
273       peer ni:
274         - nid: 2.2.2.2@o2ib
275 EOF
276         append_global_yaml
277         compare_peer_add "2.2.2.2@o2ib"
278 }
279 run_test 2 "Add peer with single nid (o2ib)"
280
281 test_3() {
282         reinit_dlc || return $?
283         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
284 peer:
285     - primary nid: 3.3.3.3@tcp
286       Multi-Rail: True
287       peer ni:
288         - nid: 3.3.3.3@tcp
289         - nid: 3.3.3.3@o2ib
290 EOF
291         append_global_yaml
292         compare_peer_add "3.3.3.3@tcp" "3.3.3.3@o2ib"
293 }
294 run_test 3 "Add peer with tcp primary o2ib secondary"
295
296 test_4() {
297         reinit_dlc || return $?
298         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
299 peer:
300     - primary nid: 4.4.4.4@tcp
301       Multi-Rail: True
302       peer ni:
303         - nid: 4.4.4.4@tcp
304         - nid: 4.4.4.1@tcp
305         - nid: 4.4.4.2@tcp
306         - nid: 4.4.4.3@tcp
307 EOF
308         append_global_yaml
309         echo "Add peer with nidrange (tcp)"
310         compare_peer_add "4.4.4.4@tcp" "4.4.4.[1-3]@tcp"
311
312         echo "Add peer with nidrange that overlaps primary nid (tcp)"
313         compare_peer_add "4.4.4.4@tcp" "4.4.4.[1-4]@tcp"
314 }
315 run_test 4 "Add peer with nidrange (tcp)"
316
317 test_5() {
318         reinit_dlc || return $?
319         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
320 peer:
321     - primary nid: 5.5.5.5@o2ib
322       Multi-Rail: True
323       peer ni:
324         - nid: 5.5.5.5@o2ib
325         - nid: 5.5.5.1@o2ib
326         - nid: 5.5.5.2@o2ib
327         - nid: 5.5.5.3@o2ib
328         - nid: 5.5.5.4@o2ib
329 EOF
330         append_global_yaml
331         echo "Add peer with nidrange (o2ib)"
332         compare_peer_add "5.5.5.5@o2ib" "5.5.5.[1-4]@o2ib"
333
334         echo "Add peer with nidranage that overlaps primary nid (o2ib)"
335         compare_peer_add "5.5.5.5@o2ib" "5.5.5.[1-4]@o2ib"
336 }
337 run_test 5 "Add peer with nidrange (o2ib)"
338
339 test_6() {
340         reinit_dlc || return $?
341         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
342 peer:
343     - primary nid: 6.6.6.6@tcp
344       Multi-Rail: True
345       peer ni:
346         - nid: 6.6.6.6@tcp
347         - nid: 6.6.6.0@tcp
348         - nid: 6.6.6.2@tcp
349         - nid: 6.6.6.4@tcp
350         - nid: 6.6.7.0@tcp
351         - nid: 6.6.7.2@tcp
352         - nid: 6.6.7.4@tcp
353         - nid: 6.6.1.0@o2ib
354         - nid: 6.6.1.3@o2ib
355         - nid: 6.6.1.6@o2ib
356         - nid: 6.6.3.0@o2ib
357         - nid: 6.6.3.3@o2ib
358         - nid: 6.6.3.6@o2ib
359         - nid: 6@gni
360         - nid: 10@gni
361         - nid: 6@kfi
362         - nid: 10@kfi
363 EOF
364         append_global_yaml
365
366         local nid_expr="6.6.[6-7].[0-4/2]@tcp"
367         nid_expr+=",6.6.[1-4/2].[0-6/3]@o2ib"
368         nid_expr+=",[6-12/4]@gni"
369         nid_expr+=",[6-12/4]@kfi"
370
371         compare_peer_add "6.6.6.6@tcp" "${nid_expr}"
372 }
373 run_test 6 "Add peer with multiple nidranges"
374
375 compare_peer_del() {
376         local prim_nid="${1:+--prim_nid $1}"
377         local nid="${2:+--nid $2}"
378
379         local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
380
381         do_lnetctl peer del ${prim_nid} ${nid} || error "peer del failed $?"
382         $LNETCTL export --backup > $actual || error "export failed $?"
383         compare_yaml_files
384         return $?
385 }
386
387 test_7() {
388         reinit_dlc || return $?
389         create_base_yaml_file
390
391         echo "Delete peer with single nid (tcp)"
392         do_lnetctl peer add --prim_nid 7.7.7.7@tcp || error "Peer add failed $?"
393         compare_peer_del "7.7.7.7@tcp"
394
395         echo "Delete peer with single nid (o2ib)"
396         do_lnetctl peer add --prim_nid 7.7.7.7@o2ib || error "Peer add failed $?"
397         compare_peer_del "7.7.7.7@o2ib"
398
399         echo "Delete peer that has multiple nids (tcp)"
400         do_lnetctl peer add --prim_nid 7.7.7.7@tcp --nid 7.7.7.[8-12]@tcp ||
401                 error "Peer add failed $?"
402         compare_peer_del "7.7.7.7@tcp"
403
404         echo "Delete peer that has multiple nids (o2ib)"
405         do_lnetctl peer add --prim_nid 7.7.7.7@o2ib --nid 7.7.7.[8-12]@o2ib ||
406                 error "Peer add failed $?"
407         compare_peer_del "7.7.7.7@o2ib"
408
409         echo "Delete peer that has both tcp and o2ib nids"
410         do_lnetctl peer add --prim_nid 7.7.7.7@tcp \
411                 --nid 7.7.7.[9-12]@tcp,7.7.7.[13-15]@o2ib ||
412                 error "Peer add failed $?"
413         compare_peer_del "7.7.7.7@tcp"
414
415         echo "Delete peer with single nid (gni)"
416         do_lnetctl peer add --prim_nid 7@gni || error "Peer add failed $?"
417         compare_peer_del "7@gni"
418
419         echo "Delete peer that has multiple nids (gni)"
420         do_lnetctl peer add --prim_nid 7@gni --nid [8-12]@gni ||
421                 error "Peer add failed $?"
422         compare_peer_del "7@gni"
423
424         echo "Delete peer with single nid (kfi)"
425         do_lnetctl peer add --prim_nid 7@kfi || error "Peer add failed $?"
426         compare_peer_del "7@kfi"
427
428         echo "Delete peer that has multiple nids (kfi)"
429         do_lnetctl peer add --prim_nid 7@kfi --nid [8-12]@kfi ||
430                 error "Peer add failed $?"
431         compare_peer_del "7@kfi"
432
433         echo "Delete peer that has tcp, o2ib, gni and kfi nids"
434         do_lnetctl peer add --prim_nid 7@gni \
435                 --nid [8-12]@gni,7.7.7.[1-4]@tcp,7.7.7.[5-9]@o2ib,[1-5]@kfi ||
436                 error "Peer add failed $?"
437         compare_peer_del "7@gni"
438 }
439 run_test 7 "Various peer delete tests"
440
441 test_8() {
442         reinit_dlc || return $?
443
444         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
445 peer:
446     - primary nid: 8.8.8.8@tcp
447       Multi-Rail: True
448       peer ni:
449         - nid: 8.8.8.8@tcp
450         - nid: 8.8.8.10@tcp
451         - nid: 8.8.8.11@tcp
452         - nid: 8.8.8.12@tcp
453         - nid: 8.8.8.14@tcp
454         - nid: 8.8.8.15@tcp
455 EOF
456         append_global_yaml
457
458         do_lnetctl peer add --prim_nid 8.8.8.8@tcp --nid 8.8.8.[10-15]@tcp ||
459                 error "Peer add failed $?"
460         compare_peer_del "8.8.8.8@tcp" "8.8.8.13@tcp"
461 }
462 run_test 8 "Delete single secondary nid from peer (tcp)"
463
464 test_9() {
465         reinit_dlc || return $?
466
467         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
468 peer:
469     - primary nid: 9.9.9.9@tcp
470       Multi-Rail: True
471       peer ni:
472         - nid: 9.9.9.9@tcp
473 EOF
474         append_global_yaml
475
476         do_lnetctl peer add --prim_nid 9.9.9.9@tcp \
477                 --nid 9.9.9.[11-16]@tcp || error "Peer add failed $?"
478         compare_peer_del "9.9.9.9@tcp" "9.9.9.[11-16]@tcp"
479 }
480 run_test 9 "Delete all secondary nids from peer (tcp)"
481
482 test_10() {
483         reinit_dlc || return $?
484
485         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
486 peer:
487     - primary nid: 10.10.10.10@tcp
488       Multi-Rail: True
489       peer ni:
490         - nid: 10.10.10.10@tcp
491         - nid: 10.10.10.12@tcp
492         - nid: 10.10.10.13@tcp
493         - nid: 10.10.10.15@tcp
494         - nid: 10.10.10.16@tcp
495 EOF
496         append_global_yaml
497         do_lnetctl peer add --prim_nid 10.10.10.10@tcp \
498                 --nid 10.10.10.[12-16]@tcp || error "Peer add failed $?"
499         compare_peer_del "10.10.10.10@tcp" "10.10.10.14@tcp"
500 }
501 run_test 10 "Delete single secondary nid from peer (o2ib)"
502
503 test_11() {
504         reinit_dlc || return $?
505
506         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
507 peer:
508     - primary nid: 11.11.11.11@tcp
509       Multi-Rail: True
510       peer ni:
511         - nid: 11.11.11.11@tcp
512 EOF
513         append_global_yaml
514         do_lnetctl peer add --prim_nid 11.11.11.11@tcp \
515                 --nid 11.11.11.[13-17]@tcp || error "Peer add failed $?"
516         compare_peer_del "11.11.11.11@tcp" "11.11.11.[13-17]@tcp"
517 }
518 run_test 11 "Delete all secondary nids from peer (o2ib)"
519
520 test_12() {
521         reinit_dlc || return $?
522
523         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
524 peer:
525     - primary nid: 12.12.12.12@o2ib
526       Multi-Rail: True
527       peer ni:
528         - nid: 12.12.12.12@o2ib
529         - nid: 13.13.13.13@o2ib
530         - nid: 14.13.13.13@o2ib
531         - nid: 14.15.13.13@o2ib
532         - nid: 15.17.1.5@tcp
533         - nid: 15.17.1.10@tcp
534         - nid: 15.17.1.20@tcp
535 EOF
536         append_global_yaml
537         do_lnetctl peer add --prim_nid 12.12.12.12@o2ib \
538                 --nid [13-14/1].[13-15/2].13.13@o2ib,[15-16/3].[17-19/4].[1].[5-20/5]@tcp ||
539                 error "Peer add failed $?"
540         compare_peer_del "12.12.12.12@o2ib" "13.15.13.13@o2ib,15.17.1.15@tcp"
541 }
542 run_test 12 "Delete a secondary nid from peer (tcp and o2ib)"
543
544 test_13() {
545         reinit_dlc || return $?
546
547         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
548 peer:
549     - primary nid: 13.13.13.13@o2ib
550       Multi-Rail: True
551       peer ni:
552         - nid: 13.13.13.13@o2ib
553 EOF
554         append_global_yaml
555         do_lnetctl peer add --prim_nid 13.13.13.13@o2ib \
556                 --nid [14-15].[1-2/1].[1].[100-254/10]@tcp,14.14.[254].14@o2ib ||
557                 error "Peer add failed $?"
558         compare_peer_del "13.13.13.13@o2ib" \
559                 "[14-15].[1-2/1].[1].[100-254/10]@tcp,14.14.[254].14@o2ib"
560 }
561 run_test 13 "Delete all secondary nids from peer (tcp and o2ib)"
562
563 create_nid() {
564         local num="$1"
565         local net="$2"
566
567         if [[ $net =~ gni* ]] || [[ $net =~ kfi* ]]; then
568                 echo "${num}@${net}"
569         else
570                 echo "${num}.${num}.${num}.${num}@${net}"
571         fi
572 }
573
574 create_mr_peer_yaml() {
575         local num_peers="$1"
576         local secondary_nids="$2"
577         local net="$3"
578
579         echo "Generating peer yaml for $num_peers peers with $secondary_nids secondary nids"
580         echo "peer:" >> $TMP/sanity-lnet-$testnum-expected.yaml
581         local i
582         local total_nids=$((num_peers + $((num_peers * secondary_nids))))
583         local created=0
584         local nidnum=1
585         while [[ $created -lt $num_peers ]]; do
586                 local primary=$(create_nid ${nidnum} ${net})
587         cat <<EOF >> $TMP/sanity-lnet-$testnum-expected.yaml
588     - primary nid: $primary
589       Multi-Rail: True
590       peer ni:
591         - nid: $primary
592 EOF
593                 local j
594                 local start=$((nidnum + 1))
595                 local end=$((nidnum + $secondary_nids))
596                 for j in $(seq ${start} ${end}); do
597                         local nid=$(create_nid $j ${net})
598                         echo "        - nid: $nid" >> $TMP/sanity-lnet-$testnum-expected.yaml
599                 done
600                 nidnum=$((end + 1))
601                 ((created++))
602         done
603 }
604
605 test_14() {
606         reinit_dlc || return $?
607
608         echo "Create single peer, single nid, using import"
609         create_mr_peer_yaml 1 0 tcp
610         do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
611                 error "Import failed $?"
612         append_global_yaml
613         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
614         compare_yaml_files
615
616         echo "Delete single peer using import --del"
617         do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml ||
618                 error "Import failed $?"
619         rm -f $TMP/sanity-lnet-$testnum-expected.yaml
620         create_base_yaml_file
621         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
622         compare_yaml_files
623 }
624 run_test 14 "import peer create/delete with single nid"
625
626 test_15() {
627         reinit_dlc || return $?
628
629         echo "Create multiple peers, single nid per peer, using import"
630         create_mr_peer_yaml 5 0 o2ib
631         # The ordering of nids for this use-case is non-deterministic, so we
632         # we can't just diff the expected/actual output.
633         do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
634                 error "Import failed $?"
635         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
636         validate_peer_nids 5 0
637
638         echo "Delete multiple peers, single nid per peer, using import --del"
639         do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml ||
640                 error "Import failed $?"
641         rm -f $TMP/sanity-lnet-$testnum-expected.yaml
642         create_base_yaml_file
643         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
644         compare_yaml_files
645 }
646 run_test 15 "import multi peer create/delete with single nid per peer"
647
648 test_16() {
649         reinit_dlc || return $?
650
651         echo "Create single peer, multiple nids, using import"
652         create_mr_peer_yaml 1 5 tcp
653         do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
654                 error "Import failed $?"
655         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
656         validate_peer_nids 1 5
657
658         echo "Delete single peer, multiple nids, using import --del"
659         do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml ||
660                 error "Import failed $?"
661         rm -f $TMP/sanity-lnet-$testnum-expected.yaml
662         create_base_yaml_file
663         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
664         compare_yaml_files
665 }
666 run_test 16 "import peer create/delete with multiple nids"
667
668 test_17() {
669         reinit_dlc || return $?
670
671         echo "Create multiple peers, multiple nids per peer, using import"
672         create_mr_peer_yaml 5 7 o2ib
673         do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
674                 error "Import failed $?"
675         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
676         validate_peer_nids 5 7
677
678         echo "Delete multiple peers, multiple nids per peer, using import --del"
679         do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml ||
680                 error "Import failed $?"
681         rm -f $TMP/sanity-lnet-$testnum-expected.yaml
682         create_base_yaml_file
683         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
684         compare_yaml_files
685 }
686 run_test 17 "import multi peer create/delete with multiple nids"
687
688 test_18a() {
689         reinit_dlc || return $?
690
691         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
692 peer:
693     - primary nid: 1.1.1.1@tcp
694       Multi-Rail: True
695       peer ni:
696         - nid: 1.1.1.1@tcp
697         - nid: 2.2.2.2@tcp
698         - nid: 4.4.4.4@tcp
699         - nid: 3.3.3.3@o2ib
700         - nid: 5@gni
701 EOF
702         echo "Import peer with 5 nids"
703         cat $TMP/sanity-lnet-$testnum-expected.yaml
704         do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
705                 error "Import failed $?"
706         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
707 peer:
708     - primary nid: 1.1.1.1@tcp
709       Multi-Rail: True
710       peer ni:
711         - nid: 2.2.2.2@tcp
712         - nid: 3.3.3.3@o2ib
713         - nid: 5@gni
714 EOF
715         echo "Delete three of the nids"
716         cat $TMP/sanity-lnet-$testnum-expected.yaml
717         do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml
718         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
719 peer:
720     - primary nid: 1.1.1.1@tcp
721       Multi-Rail: True
722       peer ni:
723         - nid: 1.1.1.1@tcp
724         - nid: 4.4.4.4@tcp
725 EOF
726         echo "Check peer has expected nids remaining"
727         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
728         append_global_yaml
729         compare_yaml_files
730 }
731 run_test 18a "Delete a subset of nids from a single peer using import --del"
732
733 test_18b() {
734         reinit_dlc || return $?
735
736         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
737 peer:
738     - primary nid: 1.1.1.1@tcp
739       Multi-Rail: True
740       peer ni:
741         - nid: 1.1.1.1@tcp
742         - nid: 2.2.2.2@tcp
743         - nid: 4.4.4.4@tcp
744         - nid: 3.3.3.3@o2ib
745         - nid: 5@gni
746     - primary nid: 6.6.6.6@o2ib
747       Multi-Rail: True
748       peer ni:
749         - nid: 6.6.6.6@o2ib
750         - nid: 7.7.7.7@tcp
751         - nid: 8.8.8.8@tcp
752         - nid: 9.9.9.9@tcp
753         - nid: 10@gni
754 EOF
755         echo "Import two peers with 5 nids each"
756         cat $TMP/sanity-lnet-$testnum-expected.yaml
757         do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
758                 error "Import failed $?"
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: 2.2.2.2@tcp
765         - nid: 3.3.3.3@o2ib
766         - nid: 5@gni
767     - primary nid: 6.6.6.6@o2ib
768       Multi-Rail: True
769       peer ni:
770         - nid: 7.7.7.7@tcp
771         - nid: 8.8.8.8@tcp
772         - nid: 10@gni
773 EOF
774         echo "Delete three of the nids from each peer"
775         cat $TMP/sanity-lnet-$testnum-expected.yaml
776         do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml
777         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
778 peer:
779     - primary nid: 6.6.6.6@o2ib
780       Multi-Rail: True
781       peer ni:
782         - nid: 6.6.6.6@o2ib
783         - nid: 7.7.7.7@tcp
784     - primary nid: 1.1.1.1@tcp
785       Multi-Rail: True
786       peer ni:
787         - nid: 1.1.1.1@tcp
788         - nid: 4.4.4.4@tcp
789 EOF
790         append_global_yaml
791         echo "Check peers have expected nids remaining"
792         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
793         compare_yaml_files
794         validate_peer_nids 2 1
795 }
796 run_test 18b "Delete multiple nids from multiple peers using import --del"
797
798 test_19() {
799         reinit_dlc || return $?
800         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
801 peer:
802     - primary nid: 19@gni
803       Multi-Rail: True
804       peer ni:
805         - nid: 19@gni
806 EOF
807         append_global_yaml
808         compare_peer_add "19@gni"
809 }
810 run_test 19 "Add peer with single nid (gni)"
811
812 test_20() {
813         reinit_dlc || return $?
814         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
815 peer:
816     - primary nid: 20@gni
817       Multi-Rail: True
818       peer ni:
819         - nid: 20@gni
820         - nid: 20.20.20.20@tcp
821         - nid: 20.20.20.20@o2ib
822 EOF
823         append_global_yaml
824         compare_peer_add "20@gni" "20.20.20.20@tcp,20.20.20.20@o2ib"
825 }
826 run_test 20 "Add peer with gni primary and tcp, o2ib secondary"
827
828 test_21() {
829         reinit_dlc || return $?
830         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
831 peer:
832     - primary nid: 21@gni
833       Multi-Rail: True
834       peer ni:
835         - nid: 21@gni
836         - nid: 22@gni
837         - nid: 23@gni
838         - nid: 24@gni
839         - nid: 25@gni
840 EOF
841         append_global_yaml
842         echo "Add peer with nidrange (gni)"
843         compare_peer_add "21@gni" "[22-25]@gni" || error
844         echo "Add peer with nidrange that overlaps primary nid (gni)"
845         compare_peer_add "21@gni" "[21-25]@gni"
846 }
847 run_test 21 "Add peer with nidrange (gni)"
848
849 test_22() {
850         reinit_dlc || return $?
851         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
852 peer:
853     - primary nid: 22@gni
854       Multi-Rail: True
855       peer ni:
856         - nid: 22@gni
857         - nid: 24@gni
858         - nid: 25@gni
859         - nid: 27@gni
860         - nid: 28@gni
861         - nid: 29@gni
862 EOF
863         append_global_yaml
864         do_lnetctl peer add --prim_nid 22@gni --nid [24-29]@gni ||
865                 error "Peer add failed $?"
866         compare_peer_del "22@gni" "26@gni"
867 }
868 run_test 22 "Delete single secondary nid from peer (gni)"
869
870 test_23() {
871         reinit_dlc || return $?
872         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
873 peer:
874     - primary nid: 23@gni
875       Multi-Rail: True
876       peer ni:
877         - nid: 23@gni
878 EOF
879         append_global_yaml
880
881         do_lnetctl peer add --prim_nid 23@gni --nid [25-29]@gni ||
882                 error "Peer add failed $?"
883         compare_peer_del "23@gni" "[25-29]@gni"
884 }
885 run_test 23 "Delete all secondary nids from peer (gni)"
886
887 test_24() {
888         reinit_dlc || return $?
889         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
890 peer:
891     - primary nid: 24@gni
892       Multi-Rail: True
893       peer ni:
894         - nid: 24@gni
895         - nid: 11@gni
896         - nid: 13.13.13.13@o2ib
897         - nid: 14.13.13.13@o2ib
898         - nid: 14.15.13.13@o2ib
899         - nid: 15.17.1.5@tcp
900         - nid: 15.17.1.10@tcp
901         - nid: 15.17.1.20@tcp
902 EOF
903         append_global_yaml
904         do_lnetctl peer add --prim_nid 24@gni \
905                 --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 ||
906                 error "Peer add failed $?"
907         compare_peer_del "24@gni" "5@gni,13.15.13.13@o2ib,15.17.1.15@tcp"
908 }
909 run_test 24 "Delete a secondary nid from peer (tcp, o2ib and gni)"
910
911 test_25() {
912         reinit_dlc || return $?
913         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
914 peer:
915     - primary nid: 25@gni
916       Multi-Rail: True
917       peer ni:
918         - nid: 25@gni
919 EOF
920         append_global_yaml
921         do_lnetctl peer add --prim_nid 25@gni \
922                 --nid [26-27].[4-10/3].26.26@tcp,26.26.26.26@o2ib,[30-35]@gni ||
923                 error "Peer add failed $?"
924         compare_peer_del "25@gni" \
925                 "[26-27].[4-10/3].26.26@tcp,26.26.26.26@o2ib,[30-35]@gni"
926 }
927 run_test 25 "Delete all secondary nids from peer (tcp, gni and o2ib)"
928
929 test_99a() {
930         reinit_dlc || return $?
931
932         echo "Invalid prim_nid - peer add"
933         do_lnetctl peer add --prim_nid foobar &&
934                 error "Command should have failed"
935
936         echo "Invalid prim_nid - peer del"
937         do_lnetctl peer del --prim_nid foobar &&
938                 error "Command should have failed"
939
940         echo "Delete non-existing peer"
941         do_lnetctl peer del --prim_nid 1.1.1.1@o2ib &&
942                 error "Command should have failed"
943
944         echo "Don't provide mandatory argument for peer del"
945         do_lnetctl peer del --nid 1.1.1.1@tcp &&
946                 error "Command should have failed"
947
948         echo "Don't provide mandatory argument for peer add"
949         do_lnetctl peer add --nid 1.1.1.1@tcp &&
950                 error "Command should have failed"
951
952         echo "Don't provide mandatory arguments peer add"
953         do_lnetctl peer add &&
954                 error "Command should have failed"
955
956         echo "Invalid secondary nids"
957         do_lnetctl peer add --prim_nid 1.1.1.1@tcp --nid foobar &&
958                 error "Command should have failed"
959
960         echo "Exceed max nids per peer"
961         do_lnetctl peer add --prim_nid 1.1.1.1@tcp --nid 1.1.1.[2-255]@tcp &&
962                 error "Command should have failed"
963
964         echo "Invalid net type"
965         do_lnetctl peer add --prim_nid 1@foo &&
966                 error "Command should have failed"
967
968         echo "Invalid nid format"
969         local invalid_nids="1@tcp 1@o2ib 1.1.1.1@gni"
970
971         local nid
972         for nid in ${invalid_nids}; do
973                 echo "Check invalid primary nid - '$nid'"
974                 do_lnetctl peer add --prim_nid $nid &&
975                         error "Command should have failed"
976         done
977
978         local invalid_strs="[2-1]@gni [a-f/x]@gni 256.256.256.256@tcp"
979         invalid_strs+=" 1.1.1.1.[2-5/f]@tcp 1.]2[.3.4@o2ib"
980         invalid_strs+="1.[2-4,[5-6],7-8].1.1@tcp foobar"
981
982         local nidstr
983         for nidstr in ${invalid_strs}; do
984                 echo "Check invalid nidstring - '$nidstr'"
985                 do_lnetctl peer add --prim_nid 1.1.1.1@tcp --nid $nidstr &&
986                         error "Command should have failed"
987         done
988
989         echo "Add non-local gateway"
990         do_lnetctl route add --net tcp --gateway 1@gni &&
991                 error "Command should have failed"
992
993         return 0
994 }
995 run_test 99a "Check various invalid inputs to lnetctl peer"
996
997 test_99b() {
998         reinit_dlc || return $?
999
1000         create_base_yaml_file
1001
1002         cat <<EOF > $TMP/sanity-lnet-$testnum-invalid.yaml
1003 peer:
1004     - primary nid: 99.99.99.99@tcp
1005       Multi-Rail: Foobar
1006       peer ni:
1007         - nid: 99.99.99.99@tcp
1008 EOF
1009         do_lnetctl import < $TMP/sanity-lnet-$testnum-invalid.yaml &&
1010                 error "import should have failed"
1011         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
1012         compare_yaml_files
1013 }
1014 run_test 99b "Invalid value for Multi-Rail in yaml import"
1015
1016 have_interface() {
1017         local if="$1"
1018         local ip=$(ip addr show dev $if | awk '/ inet /{print $2}')
1019         [[ -n $ip ]]
1020 }
1021
1022 add_net() {
1023         local net="$1"
1024         local if="$2"
1025
1026         if ! lsmod | grep -q ksocklnd ; then
1027                 load_module ../lnet/klnds/socklnd/ksocklnd ||
1028                         error "Can't load ksocklnd.ko"
1029         fi
1030
1031         do_lnetctl net add --net ${net} --if ${if} ||
1032                 error "Failed to add net ${net} on if ${if}"
1033 }
1034
1035 compare_route_add() {
1036         local rnet="$1"
1037         local gw="$2"
1038
1039         local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
1040
1041         do_lnetctl route add --net ${rnet} --gateway ${gw} ||
1042                 error "route add failed $?"
1043         # CPT configuration is pruned from the exported yaml, since the default
1044         # can vary across test systems (unlike default values for things like
1045         # peer_credits, peer_timeout, etc.)
1046         $LNETCTL export --backup | grep -v CPT > $actual ||
1047                 error "export failed $?"
1048         validate_gateway_nids
1049         return $?
1050 }
1051
1052 test_100() {
1053         reinit_dlc || return $?
1054         add_net "tcp" "${INTERFACES[0]}"
1055         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
1056 net:
1057     - net type: tcp
1058       local NI(s):
1059         - interfaces:
1060               0: ${INTERFACES[0]}
1061           tunables:
1062               peer_timeout: 180
1063               peer_credits: 8
1064               peer_buffer_credits: 0
1065               credits: 256
1066           lnd tunables:
1067               conns_per_peer: 1
1068 route:
1069     - net: tcp7
1070       gateway: 7.7.7.7@tcp
1071       hop: -1
1072       priority: 0
1073       health_sensitivity: 1
1074 peer:
1075     - primary nid: 7.7.7.7@tcp
1076       Multi-Rail: False
1077       peer ni:
1078         - nid: 7.7.7.7@tcp
1079 EOF
1080         append_global_yaml
1081         compare_route_add "tcp7" "7.7.7.7@tcp" || return $?
1082         compare_yaml_files
1083 }
1084 run_test 100 "Add route with single gw (tcp)"
1085
1086 test_101() {
1087         reinit_dlc || return $?
1088         add_net "tcp" "${INTERFACES[0]}"
1089         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
1090 net:
1091     - net type: tcp
1092       local NI(s):
1093         - interfaces:
1094               0: ${INTERFACES[0]}
1095           tunables:
1096               peer_timeout: 180
1097               peer_credits: 8
1098               peer_buffer_credits: 0
1099               credits: 256
1100           lnd tunables:
1101               conns_per_peer: 1
1102 route:
1103     - net: tcp8
1104       gateway: 8.8.8.10@tcp
1105       hop: -1
1106       priority: 0
1107       health_sensitivity: 1
1108     - net: tcp8
1109       gateway: 8.8.8.9@tcp
1110       hop: -1
1111       priority: 0
1112       health_sensitivity: 1
1113     - net: tcp8
1114       gateway: 8.8.8.8@tcp
1115       hop: -1
1116       priority: 0
1117       health_sensitivity: 1
1118 peer:
1119     - primary nid: 8.8.8.9@tcp
1120       Multi-Rail: False
1121       peer ni:
1122         - nid: 8.8.8.9@tcp
1123     - primary nid: 8.8.8.10@tcp
1124       Multi-Rail: False
1125       peer ni:
1126         - nid: 8.8.8.10@tcp
1127     - primary nid: 8.8.8.8@tcp
1128       Multi-Rail: False
1129       peer ni:
1130         - nid: 8.8.8.8@tcp
1131 EOF
1132         append_global_yaml
1133         compare_route_add "tcp8" "8.8.8.[8-10]@tcp"
1134 }
1135 run_test 101 "Add route with multiple gw (tcp)"
1136
1137 compare_route_del() {
1138         local rnet="$1"
1139         local gw="$2"
1140
1141         local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
1142
1143         do_lnetctl route del --net ${rnet} --gateway ${gw} ||
1144                 error "route del failed $?"
1145         $LNETCTL export --backup > $actual ||
1146                 error "export failed $?"
1147         validate_gateway_nids
1148 }
1149
1150 test_102() {
1151         reinit_dlc || return $?
1152         add_net "tcp" "${INTERFACES[0]}"
1153         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml
1154         do_lnetctl route add --net tcp102 --gateway 102.102.102.102@tcp ||
1155                 error "route add failed $?"
1156         compare_route_del "tcp102" "102.102.102.102@tcp"
1157 }
1158 run_test 102 "Delete route with single gw (tcp)"
1159
1160 test_103() {
1161         reinit_dlc || return $?
1162         add_net "tcp" "${INTERFACES[0]}"
1163         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml
1164         do_lnetctl route add --net tcp103 \
1165                 --gateway 103.103.103.[103-120/4]@tcp ||
1166                 error "route add failed $?"
1167         compare_route_del "tcp103" "103.103.103.[103-120/4]@tcp"
1168 }
1169 run_test 103 "Delete route with multiple gw (tcp)"
1170
1171 test_104() {
1172         local tyaml="$TMP/sanity-lnet-$testnum-expected.yaml"
1173
1174         reinit_dlc || return $?
1175
1176         # Default value is '3'
1177         local val=$($LNETCTL global show | awk '/response_tracking/{print $NF}')
1178         [[ $val -ne 3 ]] &&
1179                 error "Expect 3 found $val"
1180
1181         echo "Set < 0;  Should fail"
1182         do_lnetctl set response_tracking -1 &&
1183                 error "should have failed $?"
1184
1185         reinit_dlc || return $?
1186         cat <<EOF > $tyaml
1187 global:
1188     response_tracking: -10
1189 EOF
1190         do_lnetctl import < $tyaml &&
1191                 error "should have failed $?"
1192
1193         echo "Check valid values; Should succeed"
1194         local i
1195         for ((i = 0; i < 4; i++)); do
1196                 reinit_dlc || return $?
1197                 do_lnetctl set response_tracking $i ||
1198                         error "should have succeeded $?"
1199                 $LNETCTL global show | grep -q "response_tracking: $i" ||
1200                         error "Failed to set response_tracking to $i"
1201                 reinit_dlc || return $?
1202                 cat <<EOF > $tyaml
1203 global:
1204     response_tracking: $i
1205 EOF
1206                 do_lnetctl import < $tyaml ||
1207                         error "should have succeeded $?"
1208                 $LNETCTL global show | grep -q "response_tracking: $i" ||
1209                         error "Failed to set response_tracking to $i"
1210         done
1211
1212         reinit_dlc || return $?
1213         echo "Set > 3; Should fail"
1214         do_lnetctl set response_tracking 4 &&
1215                 error "should have failed $?"
1216
1217         reinit_dlc || return $?
1218         cat <<EOF > $tyaml
1219 global:
1220     response_tracking: 10
1221 EOF
1222         do_lnetctl import < $tyaml &&
1223                 error "should have failed $?"
1224         return 0
1225 }
1226 run_test 104 "Set/check response_tracking param"
1227
1228 test_105() {
1229         reinit_dlc || return $?
1230         add_net "tcp" "${INTERFACES[0]}"
1231         do_lnetctl route add --net tcp105 --gateway 105.105.105.105@tcp ||
1232                 error "route add failed $?"
1233         do_lnetctl peer add --prim 105.105.105.105@tcp &&
1234                 error "peer add should fail"
1235
1236         return 0
1237 }
1238 run_test 105 "Adding duplicate GW peer should fail"
1239
1240 test_106() {
1241         reinit_dlc || return $?
1242         add_net "tcp" "${INTERFACES[0]}"
1243         do_lnetctl route add --net tcp106 --gateway 106.106.106.106@tcp ||
1244                 error "route add failed $?"
1245         do_lnetctl peer del --prim 106.106.106.106@tcp &&
1246                 error "peer del should fail"
1247
1248         return 0
1249 }
1250 run_test 106 "Deleting GW peer should fail"
1251
1252 test_200() {
1253         cleanup_lnet || exit 1
1254         load_lnet "networks=\"\""
1255         do_ns $LNETCTL lnet configure --all || exit 1
1256         $LNETCTL net show --net tcp | grep -q "nid: ${FAKE_IP}@tcp$"
1257 }
1258 run_test 200 "load lnet w/o module option, configure in a non-default namespace"
1259
1260 test_201() {
1261         cleanup_lnet || exit 1
1262         load_lnet "networks=tcp($FAKE_IF)"
1263         do_ns $LNETCTL lnet configure --all || exit 1
1264         $LNETCTL net show --net tcp | grep -q "nid: ${FAKE_IP}@tcp$"
1265 }
1266 run_test 201 "load lnet using networks module options in a non-default namespace"
1267
1268 test_202() {
1269         cleanup_lnet || exit 1
1270         load_lnet "networks=\"\" ip2nets=\"tcp0($FAKE_IF) ${FAKE_IP}\""
1271         do_ns $LNETCTL lnet configure --all || exit 1
1272         $LNETCTL net show | grep -q "nid: ${FAKE_IP}@tcp$"
1273 }
1274 run_test 202 "load lnet using ip2nets in a non-default namespace"
1275
1276
1277 ### Add the interfaces in the target namespace
1278
1279 test_203() {
1280         cleanup_lnet || exit 1
1281         load_lnet
1282         do_lnetctl lnet configure || exit 1
1283         do_ns $LNETCTL net add --net tcp0 --if $FAKE_IF
1284 }
1285 run_test 203 "add a network using an interface in the non-default namespace"
1286
1287 LNET_PARAMS_FILE="$TMP/$TESTSUITE.parameters"
1288 function save_lnet_params() {
1289         $LNETCTL global show | egrep -v '^global:$' |
1290                                sed 's/://' > $LNET_PARAMS_FILE
1291 }
1292
1293 function restore_lnet_params() {
1294         local param value
1295         while read param value; do
1296                 [[ $param == max_intf ]] && continue
1297                 [[ $param == lnd_timeout ]] && continue
1298                 $LNETCTL set ${param} ${value} ||
1299                         error "Failed to restore ${param} to ${value}"
1300         done < $LNET_PARAMS_FILE
1301 }
1302
1303 function lnet_health_pre() {
1304         save_lnet_params
1305
1306         # Lower transaction timeout to speed up test execution
1307         $LNETCTL set transaction_timeout 10 ||
1308                 error "Failed to set transaction_timeout $?"
1309
1310         RETRY_PARAM=$($LNETCTL global show | awk '/retry_count/{print $NF}')
1311         RSND_PRE=$($LNETCTL stats show | awk '/resend_count/{print $NF}')
1312         LO_HVAL_PRE=$($LNETCTL net show -v 2 | awk '/health value/{print $NF}' |
1313                       xargs echo | sed 's/ /+/g' | bc -l)
1314
1315         RMT_HVAL_PRE=$($LNETCTL peer show --nid ${RNIDS[0]} -v 2 2>/dev/null |
1316                        awk '/health value/{print $NF}' | xargs echo |
1317                        sed 's/ /+/g' | bc -l)
1318
1319         # Might not have any peers so initialize to zero.
1320         RMT_HVAL_PRE=${RMT_HVAL_PRE:-0}
1321
1322         return 0
1323 }
1324
1325 function lnet_health_post() {
1326         RSND_POST=$($LNETCTL stats show | awk '/resend_count/{print $NF}')
1327         LO_HVAL_POST=$($LNETCTL net show -v 2 |
1328                        awk '/health value/{print $NF}' |
1329                        xargs echo | sed 's/ /+/g' | bc -l)
1330
1331         RMT_HVAL_POST=$($LNETCTL peer show --nid ${RNIDS[0]} -v 2 2>/dev/null |
1332                         awk '/health value/{print $NF}' | xargs echo |
1333                         sed 's/ /+/g' | bc -l)
1334
1335         # Might not have any peers so initialize to zero.
1336         RMT_HVAL_POST=${RMT_HVAL_POST:-0}
1337
1338         ${VERBOSE} &&
1339         echo "Pre resends: $RSND_PRE" &&
1340         echo "Post resends: $RSND_POST" &&
1341         echo "Resends delta: $((RSND_POST - RSND_PRE))" &&
1342         echo "Pre local health: $LO_HVAL_PRE" &&
1343         echo "Post local health: $LO_HVAL_POST" &&
1344         echo "Pre remote health: $RMT_HVAL_PRE" &&
1345         echo "Post remote health: $RMT_HVAL_POST"
1346
1347         restore_lnet_params
1348
1349         do_lnetctl peer set --health 1000 --all
1350         do_lnetctl net set --health 1000 --all
1351
1352         return 0
1353 }
1354
1355 function check_no_resends() {
1356         echo "Check that no resends took place"
1357         [[ $RSND_POST -ne $RSND_PRE ]] &&
1358                 error "Found resends: $RSND_POST != $RSND_PRE"
1359
1360         return 0
1361 }
1362
1363 function check_resends() {
1364         local delta=$((RSND_POST - RSND_PRE))
1365
1366         echo "Check that $RETRY_PARAM resends took place"
1367         [[ $delta -ne $RETRY_PARAM ]] &&
1368                 error "Expected $RETRY_PARAM resends found $delta"
1369
1370         return 0
1371 }
1372
1373 function check_no_local_health() {
1374         echo "Check that local NI health is unchanged"
1375         [[ $LO_HVAL_POST -ne $LO_HVAL_PRE ]] &&
1376                 error "Local health changed: $LO_HVAL_POST != $LO_HVAL_PRE"
1377
1378         return 0
1379 }
1380
1381 function check_local_health() {
1382         echo "Check that local NI health has been changed"
1383         [[ $LO_HVAL_POST -eq $LO_HVAL_PRE ]] &&
1384                 error "Local health unchanged: $LO_HVAL_POST == $LO_HVAL_PRE"
1385
1386         return 0
1387 }
1388
1389 function check_no_remote_health() {
1390         echo "Check that remote NI health is unchanged"
1391         [[ $RMT_HVAL_POST -ne $RMT_HVAL_PRE ]] &&
1392                 error "Remote health changed: $RMT_HVAL_POST != $RMT_HVAL_PRE"
1393
1394         return 0
1395 }
1396
1397 function check_remote_health() {
1398         echo "Check that remote NI health has been changed"
1399         [[ $RMT_HVAL_POST -eq $RMT_HVAL_PRE ]] &&
1400                 error "Remote health unchanged: $RMT_HVAL_POST == $RMT_HVAL_PRE"
1401
1402         return 0
1403 }
1404
1405 RNODE=""
1406 RLOADED=false
1407 NET_DEL_ARGS=""
1408 RNIDS=( )
1409 LNIDS=( )
1410 setup_health_test() {
1411         local need_mr=$1
1412         local rc=0
1413
1414         local rnodes=$(remote_nodes_list)
1415         [[ -z $rnodes ]] && skip "Need at least 1 remote node"
1416
1417         cleanup_lnet || error "Failed to cleanup before test execution"
1418
1419         # Loading modules should configure LNet with the appropriate
1420         # test-framework configuration
1421         load_lnet "config_on_load=1" || error "Failed to load modules"
1422
1423         LNIDS=( $($LCTL list_nids | xargs echo) )
1424
1425         RNODE=$(awk '{print $1}' <<<$rnodes)
1426         RNIDS=( $(do_node $RNODE $LCTL list_nids | xargs echo) )
1427
1428         if [[ -z ${RNIDS[@]} ]]; then
1429                 do_rpc_nodes $RNODE load_lnet "config_on_load=1"
1430                 RLOADED=true
1431                 RNIDS=( $(do_node $RNODE $LCTL list_nids | xargs echo) )
1432         fi
1433
1434         [[ ${#LNIDS[@]} -lt 1 ]] &&
1435                 error "No NIDs configured for local host $HOSTNAME"
1436         [[ ${#RNIDS[@]} -lt 1 ]] &&
1437                 error "No NIDs configured for remote host $RNODE"
1438
1439         do_lnetctl discover ${RNIDS[0]} ||
1440                 error "Unable to discover ${RNIDS[0]}"
1441
1442         local mr=$($LNETCTL peer show --nid ${RNIDS[0]} |
1443                    awk '/Multi-Rail/{print $NF}')
1444
1445         if ${need_mr} && [[ $mr == False ]]; then
1446                 cleanup_health_test || return $?
1447                 skip "Need MR peer"
1448         fi
1449
1450         if ( ! ${need_mr} && [[ ${#RNIDS[@]} -gt 1 ]] ) ||
1451            ( ! ${need_mr} && [[ ${#LNIDS[@]} -gt 1 ]] ); then
1452                 cleanup_health_test || return $?
1453                 skip "Need SR peer"
1454         fi
1455
1456         if ${need_mr} && [[ ${#RNIDS[@]} -lt 2 ]]; then
1457                 # Add a second, reachable NID to rnode.
1458                 local net=${RNIDS[0]}
1459
1460                 net="${net//*@/}1"
1461
1462                 local if=$(do_rpc_nodes --quiet $RNODE lnet_if_list)
1463                 [[ -z $if ]] &&
1464                         error "Failed to determine interface for $RNODE"
1465
1466                 do_rpc_nodes $RNODE "$LNETCTL lnet configure"
1467                 do_rpc_nodes $RNODE "$LNETCTL net add --net $net --if $if" ||
1468                         rc=$?
1469                 if [[ $rc -ne 0 ]]; then
1470                         error "Failed to add interface to $RNODE rc=$?"
1471                 else
1472                         RNIDS[1]="${RNIDS[0]}1"
1473                         NET_DEL_ARGS="--net $net --if $if"
1474                 fi
1475         fi
1476
1477         if ${need_mr} && [[ ${#LNIDS[@]} -lt 2 ]]; then
1478                 local net=${LNIDS[0]}
1479                 net="${net//*@/}1"
1480
1481                 do_lnetctl lnet configure &&
1482                         do_lnetctl net add --net $net --if ${INTERFACES[0]} ||
1483                         rc=$?
1484                 if [[ $rc -ne 0 ]]; then
1485                         error "Failed to add interface rc=$?"
1486                 else
1487                         LNIDS[1]="${LNIDS[0]}1"
1488                 fi
1489         fi
1490
1491         $LNETCTL net show
1492
1493         $LNETCTL peer show -v 2 | egrep -e nid -e health
1494
1495         $LCTL set_param debug=+net
1496
1497         return 0
1498
1499 }
1500
1501 cleanup_health_test() {
1502         local rc=0
1503
1504         if [[ -n $NET_DEL_ARGS ]]; then
1505                 do_rpc_nodes $RNODE \
1506                         "$LNETCTL net del $NET_DEL_ARGS" ||
1507                         rc=$((rc + $?))
1508                 NET_DEL_ARGS=""
1509         fi
1510
1511         unload_modules || rc=$?
1512
1513         if $RLOADED; then
1514                 do_rpc_nodes $RNODE unload_modules_local ||
1515                         rc=$((rc + $?))
1516                 RLOADED=false
1517         fi
1518
1519         [[ $rc -ne 0 ]] &&
1520                 error "Failed cleanup"
1521
1522         return $rc
1523 }
1524
1525 add_health_test_drop_rules() {
1526         local hstatus=$1
1527         local lnid rnid
1528
1529         for lnid in "${LNIDS[@]}"; do
1530                 for rnid in "${RNIDS[@]}"; do
1531                         $LCTL net_drop_add -s $lnid -d $rnid -m GET -r 1 -e ${hstatus}
1532                 done
1533         done
1534 }
1535
1536 # See lnet/lnet/lib-msg.c:lnet_health_check()
1537 LNET_LOCAL_RESEND_STATUSES="local_interrupt local_dropped local_aborted"
1538 LNET_LOCAL_RESEND_STATUSES+=" local_no_route local_timeout"
1539 LNET_LOCAL_NO_RESEND_STATUSES="local_error"
1540 test_204() {
1541         setup_health_test false || return $?
1542
1543         local hstatus
1544         for hstatus in ${LNET_LOCAL_RESEND_STATUSES} \
1545                        ${LNET_LOCAL_NO_RESEND_STATUSES}; do
1546                 echo "Simulate $hstatus"
1547                 lnet_health_pre || return $?
1548
1549                 add_health_test_drop_rules ${hstatus}
1550                 do_lnetctl discover ${RNIDS[0]} &&
1551                         error "Should have failed"
1552                 $LCTL net_drop_del -a
1553
1554                 lnet_health_post
1555
1556                 check_no_resends || return $?
1557                 check_no_local_health || return $?
1558         done
1559
1560         cleanup_health_test || return $?
1561
1562         return 0
1563 }
1564 run_test 204 "Check no health or resends for single-rail local failures"
1565
1566 test_205() {
1567         setup_health_test true || return $?
1568
1569         local hstatus
1570         for hstatus in ${LNET_LOCAL_RESEND_STATUSES}; do
1571                 echo "Simulate $hstatus"
1572                 lnet_health_pre || return $?
1573
1574                 add_health_test_drop_rules ${hstatus}
1575                 do_lnetctl discover ${RNIDS[0]} &&
1576                         error "Should have failed"
1577                 $LCTL net_drop_del -a
1578
1579                 lnet_health_post
1580
1581                 check_resends || return $?
1582                 check_local_health || return $?
1583         done
1584
1585         for hstatus in ${LNET_LOCAL_NO_RESEND_STATUSES}; do
1586                 echo "Simulate $hstatus"
1587                 lnet_health_pre || return $?
1588
1589                 add_health_test_drop_rules ${hstatus}
1590                 do_lnetctl discover ${RNIDS[0]} &&
1591                         error "Should have failed"
1592                 $LCTL net_drop_del -a
1593
1594                 lnet_health_post
1595
1596                 check_no_resends || return $?
1597                 check_local_health || return $?
1598         done
1599
1600         cleanup_health_test || return $?
1601
1602         return 0
1603 }
1604 run_test 205 "Check health and resends for multi-rail local failures"
1605
1606 # See lnet/lnet/lib-msg.c:lnet_health_check()
1607 LNET_REMOTE_RESEND_STATUSES="remote_dropped"
1608 LNET_REMOTE_NO_RESEND_STATUSES="remote_error remote_timeout"
1609 test_206() {
1610         setup_health_test false || return $?
1611
1612         local hstatus
1613         for hstatus in ${LNET_REMOTE_RESEND_STATUSES} \
1614                        ${LNET_REMOTE_NO_RESEND_STATUSES}; do
1615                 echo "Simulate $hstatus"
1616                 lnet_health_pre || return $?
1617
1618                 add_health_test_drop_rules ${hstatus}
1619                 do_lnetctl discover ${RNIDS[0]} &&
1620                         error "Should have failed"
1621                 $LCTL net_drop_del -a
1622
1623                 lnet_health_post
1624
1625                 check_no_resends || return $?
1626                 check_no_local_health || return $?
1627                 check_no_remote_health || return $?
1628         done
1629
1630         cleanup_health_test || return $?
1631
1632         return 0
1633 }
1634 run_test 206 "Check no health or resends for single-rail remote failures"
1635
1636 test_207() {
1637         setup_health_test true || return $?
1638
1639         local hstatus
1640         for hstatus in ${LNET_REMOTE_RESEND_STATUSES}; do
1641                 echo "Simulate $hstatus"
1642                 lnet_health_pre || return $?
1643
1644                 add_health_test_drop_rules ${hstatus}
1645
1646                 do_lnetctl discover ${RNIDS[0]} &&
1647                         error "Should have failed"
1648
1649                 lnet_health_post
1650
1651                 $LCTL net_drop_del -a
1652
1653                 check_resends || return $?
1654                 check_no_local_health || return $?
1655                 check_remote_health || return $?
1656                 do_lnetctl peer set --health 1000 --all ||
1657                         error "Unable to reset health rc=$?"
1658         done
1659         for hstatus in ${LNET_REMOTE_NO_RESEND_STATUSES}; do
1660                 echo "Simulate $hstatus"
1661                 lnet_health_pre || return $?
1662
1663                 add_health_test_drop_rules ${hstatus}
1664
1665                 do_lnetctl discover ${RNIDS[0]} &&
1666                         error "Should have failed"
1667
1668                 lnet_health_post
1669
1670                 $LCTL net_drop_del -a
1671
1672                 check_no_resends || return $?
1673                 check_no_local_health || return $?
1674                 check_remote_health || return $?
1675                 do_lnetctl peer set --health 1000 --all ||
1676                         error "Unable to reset health rc=$?"
1677         done
1678
1679         cleanup_health_test || return $?
1680
1681         return 0
1682 }
1683 run_test 207 "Check health and resends for multi-rail remote errors"
1684
1685 test_208_load_and_check_lnet() {
1686         local ip2nets="$1"
1687         local p_nid="$2"
1688         local s_nid="$3"
1689         local num_expected=1
1690
1691         load_lnet "networks=\"\" ip2nets=\"${ip2nets_str}\""
1692
1693         $LCTL net up ||
1694                 error "Failed to load LNet with ip2nets \"${ip2nets_str}\""
1695
1696         [[ -n $s_nid ]] &&
1697                 num_expected=2
1698
1699         declare -a nids
1700         nids=( $($LCTL list_nids) )
1701
1702         [[ ${#nids[@]} -ne ${num_expected} ]] &&
1703                 error "Expect ${num_expected} NIDs found ${#nids[@]}"
1704
1705         [[ ${nids[0]} == ${p_nid} ]] ||
1706                 error "Expect NID \"${p_nid}\" found \"${nids[0]}\""
1707
1708         [[ -n $s_nid ]] && [[ ${nids[1]} != ${s_nid} ]] &&
1709                 error "Expect second NID \"${s_nid}\" found \"${nids[1]}\""
1710
1711         $LCTL net down &>/dev/null
1712         cleanup_lnet
1713 }
1714
1715 test_208() {
1716         cleanup_netns || error "Failed to cleanup netns before test execution"
1717         cleanup_lnet || error "Failed to unload modules before test execution"
1718         setup_fakeif || error "Failed to add fake IF"
1719
1720         have_interface "$FAKE_IF" ||
1721                 error "Expect $FAKE_IF configured but not found"
1722
1723         local if0_ip=$(ip --oneline addr show dev ${INTERFACES[0]} |
1724                        awk '/inet /{print $4}' |
1725                        sed 's:/.*::')
1726         if0_ip=($(echo "${if0_ip[@]}" | tr ' ' '\n' | uniq | tr '\n' ' '))
1727         local ip2nets_str="tcp(${INTERFACES[0]}) $if0_ip"
1728
1729         echo "Configure single NID \"$ip2nets_str\""
1730         test_208_load_and_check_lnet "${ip2nets_str}" "${if0_ip}@tcp"
1731
1732         ip2nets_str="tcp(${INTERFACES[0]}) $if0_ip; tcp1($FAKE_IF) $FAKE_IP"
1733         echo "Configure two NIDs; two NETs \"$ip2nets_str\""
1734         test_208_load_and_check_lnet "${ip2nets_str}" "${if0_ip}@tcp" \
1735                                      "${FAKE_IP}@tcp1"
1736
1737         ip2nets_str="tcp(${INTERFACES[0]}) $if0_ip; tcp($FAKE_IF) $FAKE_IP"
1738         echo "Configure two NIDs; one NET \"$ip2nets_str\""
1739         test_208_load_and_check_lnet "${ip2nets_str}" "${if0_ip}@tcp" \
1740                                      "${FAKE_IP}@tcp"
1741         local addr1=( ${if0_ip//./ } )
1742         local addr2=( ${FAKE_IP//./ } )
1743         local range="[${addr1[0]},${addr2[0]}]"
1744
1745         local i
1746         for i in $(seq 1 3); do
1747                 range+=".[${addr1[$i]},${addr2[$i]}]"
1748         done
1749         ip2nets_str="tcp(${INTERFACES[0]},${FAKE_IF}) ${range}"
1750
1751         echo "Configured two NIDs; one NET alt syntax \"$ip2nets_str\""
1752         test_208_load_and_check_lnet "${ip2nets_str}" "${if0_ip}@tcp" \
1753                                      "${FAKE_IP}@tcp"
1754
1755         cleanup_fakeif
1756
1757         echo "alt syntax with missing IF \"$ip2nets_str\""
1758         load_lnet "networks=\"\" ip2nets=\"${ip2nets_str}\""
1759
1760         echo "$LCTL net up should fail"
1761         $LCTL net up &&
1762                 error "LNet bringup should have failed"
1763
1764         cleanup_lnet
1765 }
1766 run_test 208 "Test various kernel ip2nets configurations"
1767
1768 test_209() {
1769         setup_health_test false || return $?
1770
1771         echo "Simulate network_timeout w/SR config"
1772         lnet_health_pre
1773
1774         add_health_test_drop_rules network_timeout
1775
1776         do_lnetctl discover ${RNIDS[0]} &&
1777                 error "Should have failed"
1778         $LCTL net_drop_del -a
1779
1780         lnet_health_post
1781
1782         check_no_resends || return $?
1783         check_no_local_health || return $?
1784         check_no_remote_health || return $?
1785
1786         cleanup_health_test || return $?
1787
1788         setup_health_test true || return $?
1789
1790         echo "Simulate network_timeout w/MR config"
1791
1792         lnet_health_pre
1793
1794         add_health_test_drop_rules network_timeout
1795
1796         do_lnetctl discover ${RNIDS[0]} &&
1797                 error "Should have failed"
1798         $LCTL net_drop_del -a
1799
1800         lnet_health_post
1801
1802         check_no_resends || return $?
1803         check_local_health || return $?
1804         check_remote_health || return $?
1805
1806         cleanup_health_test || return $?
1807
1808         return 0
1809 }
1810 run_test 209 "Check health, but not resends, for network timeout"
1811
1812 check_nid_in_recovq() {
1813         local recovq=$($LNETCTL debug recovery $1)
1814         local expect="$2"
1815         local nids=$($LCTL list_nids | xargs echo)
1816         local found=false
1817         local nid=""
1818
1819         echo "Check \"$1\" recovery queue"
1820         echo "$recovq"
1821         if [[ $(grep -c 'nid-'<<<$recovq) -ne $expect ]]; then
1822                 error "Expect $expect NIDs found: \"$recovq\""
1823         fi
1824
1825         [[ $expect -eq 0 ]] && return 0
1826
1827         for nid in ${nids}; do
1828                 grep -q "nid-0: $nid"<<<$recovq &&
1829                         found=true
1830         done
1831
1832         if ! $found; then
1833                 error "Didn't find local NIDs in recovery queue: \"$recovq\""
1834         fi
1835
1836         return 0
1837 }
1838
1839 # First enqueue happens at time 0.
1840 # 2nd at 0 + 2^0 = 1
1841 # 3rd at 1 + 2^1 = 3
1842 # 4th at 3 + 2^2 = 7
1843 # 5th at 7 + 2^3 = 15
1844 # e.g. after 10 seconds we would expect to have seen the 4th enqueue,
1845 # (3 pings sent, 4th about to happen) and the 5th enqueue is yet to
1846 # happen
1847 # If the recovery limit is 10 seconds, then when the 5th enqueue happens
1848 # we expect the peer NI to have aged out, so it will not actually be
1849 # queued.
1850 # If max_recovery_ping_interval is set to 2 then:
1851 #  First enqueue happens at time 0.
1852 #  2nd at 0 + 2^0 = 1
1853 #  3rd at 1 + 2^1 = 3
1854 #  4th at 3 + 2^1 = 5
1855 #  5th at 5 + 2^1 = 7
1856 #  6th at 7 + 2^1 = 9
1857 #  7th at 9 + 2^1 = 11
1858 # e.g. after 4 seconds we would expect to have seen the 3th enqueue,
1859 # (2 pings sent, 3rd about to happen), and the 4th enqueue is yet to happen
1860 # e.g. after 10 seconds we would expect to have seen the 6th enqueue,
1861 # (5 pings sent, 6th about to happen), and the 8th enqueue is yet to happen
1862 check_ping_count() {
1863         local queue="$1"
1864         local expect="$2"
1865
1866         echo "Check ping counts:"
1867         local ping_count
1868         if [[ $queue == "ni" ]]; then
1869                 $LNETCTL net show -v 2 | egrep 'nid|health value|ping'
1870                 ping_count=( $($LNETCTL net show -v 2 |
1871                                 awk '/ping_count/{print $NF}') )
1872         elif [[ $queue == "peer_ni" ]]; then
1873                 $LNETCTL peer show -v 2 | egrep 'nid|health value|ping'
1874                 ping_count=( $($LNETCTL peer show -v 2 |
1875                                 awk '/ping_count/{print $NF}') )
1876         else
1877                 error "Unrecognized queue \"$queue\""
1878                 return 1
1879         fi
1880
1881         local count
1882         local found=false
1883         for count in "${ping_count[@]}"; do
1884                 if [[ $count -eq $expect ]]; then
1885                         if [[ $expect -ne 0 ]] && $found ; then
1886                                 error "Found more than one interface matching \"$expect\" ping count"
1887                                 return 1
1888                         else
1889                                 echo "Expect ping count \"$expect\" found \"$count\""
1890                                 found=true;
1891                         fi
1892                 elif [[ $count -ne 0 ]]; then
1893                         error "Found interface with ping count \"$count\" but expect \"$expect\""
1894                         return 1
1895                 fi
1896         done
1897
1898         return 0
1899 }
1900
1901 test_210() {
1902         reinit_dlc || return $?
1903         add_net "tcp" "${INTERFACES[0]}" || return $?
1904         add_net "tcp1" "${INTERFACES[0]}" || return $?
1905
1906         local prim_nid=$($LCTL list_nids | head -n 1)
1907
1908         do_lnetctl discover $prim_nid ||
1909                 error "failed to discover myself"
1910
1911         local default=$($LNETCTL global show |
1912                         awk '/recovery_limit/{print $NF}')
1913         # Set recovery limit to 10 seconds.
1914         do_lnetctl set recovery_limit 10 ||
1915                 error "failed to set recovery_limit"
1916
1917         $LCTL set_param debug=+net
1918         # Use local_error so LNet doesn't attempt to resend the discovery ping
1919         $LCTL net_drop_add -s *@tcp -d *@tcp -m GET -r 1 -e local_error
1920         $LCTL net_drop_add -s *@tcp1 -d *@tcp1 -m GET -r 1 -e local_error
1921         do_lnetctl discover $prim_nid &&
1922                 error "Expected discovery to fail"
1923
1924         # See comment for check_ping_count()
1925         sleep 5
1926         check_nid_in_recovq "-l" "1"
1927         check_ping_count "ni" "2"
1928
1929         sleep 5
1930
1931         check_nid_in_recovq "-l" "1"
1932         check_ping_count "ni" "3"
1933
1934         $LCTL net_drop_del -a
1935
1936         reinit_dlc || return $?
1937         add_net "tcp" "${INTERFACES[0]}" || return $?
1938         add_net "tcp1" "${INTERFACES[0]}" || return $?
1939
1940         local prim_nid=$($LCTL list_nids | head -n 1)
1941
1942         do_lnetctl discover $prim_nid ||
1943                 error "failed to discover myself"
1944
1945         do_lnetctl set recovery_limit $default ||
1946                 error "failed to set recovery_limit"
1947
1948         default=$($LNETCTL global show |
1949                   awk '/max_recovery_ping_interval/{print $NF}')
1950         do_lnetctl set max_recovery_ping_interval 2 ||
1951                 error "failed to set max_recovery_ping_interval"
1952
1953         $LCTL set_param debug=+net
1954         # Use local_error so LNet doesn't attempt to resend the discovery ping
1955         $LCTL net_drop_add -s *@tcp -d *@tcp -m GET -r 1 -e local_error
1956         $LCTL net_drop_add -s *@tcp1 -d *@tcp1 -m GET -r 1 -e local_error
1957         do_lnetctl discover $prim_nid &&
1958                 error "Expected discovery to fail"
1959
1960         # See comment for check_ping_count()
1961         sleep 4
1962         check_nid_in_recovq "-l" "1"
1963         check_ping_count "ni" "2"
1964
1965         sleep 6
1966         check_nid_in_recovq "-l" "1"
1967         check_ping_count "ni" "5"
1968
1969         $LCTL net_drop_del -a
1970
1971         do_lnetctl set max_recovery_ping_interval $default ||
1972                 error "failed to set max_recovery_ping_interval"
1973
1974         return 0
1975 }
1976 run_test 210 "Local NI recovery checks"
1977
1978 test_211() {
1979         reinit_dlc || return $?
1980         add_net "tcp" "${INTERFACES[0]}" || return $?
1981         add_net "tcp1" "${INTERFACES[0]}" || return $?
1982
1983         local prim_nid=$($LCTL list_nids | head -n 1)
1984
1985         do_lnetctl discover $prim_nid ||
1986                 error "failed to discover myself"
1987
1988         local default=$($LNETCTL global show |
1989                         awk '/recovery_limit/{print $NF}')
1990         # Set recovery limit to 10 seconds.
1991         do_lnetctl set recovery_limit 10 ||
1992                 error "failed to set recovery_limit"
1993
1994         $LCTL net_drop_add -s *@tcp -d *@tcp -m GET -r 1 -e remote_error
1995         $LCTL net_drop_add -s *@tcp1 -d *@tcp1 -m GET -r 1 -e remote_error
1996
1997         # Set health to 0 on one interface. This forces it onto the recovery
1998         # queue.
1999         $LNETCTL peer set --nid $prim_nid --health 0
2000
2001         # After 5 seconds, we expect the peer NI to still be in recovery
2002         sleep 5
2003         check_nid_in_recovq "-p" 1
2004         check_ping_count "peer_ni" "2"
2005
2006         # After 15 seconds, the peer NI should have been fully processed out of
2007         # the recovery queue. We'll allow a total of 17 seconds to account for
2008         # differences in sleeping for whole seconds vs. the more accurate time
2009         # keeping that is done in the recovery code.
2010         sleep 12
2011         check_nid_in_recovq "-p" 0
2012         check_ping_count "peer_ni" "4"
2013
2014         $LCTL net_drop_del -a
2015
2016         # Set health to force it back onto the recovery queue. Set to 500 means
2017         # in 5 seconds it should be back at maximum value. We'll wait a couple
2018         # more seconds than that to be safe.
2019         # NB: we reset the recovery limit to 0 (indefinite) so the peer NI is
2020         # eligible again
2021         do_lnetctl set recovery_limit 0 ||
2022                 error "failed to set recovery_limit"
2023
2024         $LNETCTL peer set --nid $prim_nid --health 500
2025
2026         check_nid_in_recovq "-p" 1
2027         check_ping_count "peer_ni" "2"
2028
2029         sleep 7
2030
2031         check_nid_in_recovq "-p" 0
2032         check_ping_count "peer_ni" "0"
2033
2034         reinit_dlc || return $?
2035         add_net "tcp" "${INTERFACES[0]}" || return $?
2036         add_net "tcp1" "${INTERFACES[0]}" || return $?
2037
2038         local prim_nid=$($LCTL list_nids | head -n 1)
2039
2040         do_lnetctl discover $prim_nid ||
2041                 error "failed to discover myself"
2042
2043         do_lnetctl set recovery_limit $default ||
2044                 error "failed to set recovery_limit"
2045
2046         default=$($LNETCTL global show |
2047                   awk '/max_recovery_ping_interval/{print $NF}')
2048         do_lnetctl set max_recovery_ping_interval 2 ||
2049                 error "failed to set max_recovery_ping_interval"
2050
2051         $LCTL net_drop_add -s *@tcp -d *@tcp -m GET -r 1 -e remote_error
2052         $LCTL net_drop_add -s *@tcp1 -d *@tcp1 -m GET -r 1 -e remote_error
2053
2054         # Set health to 0 on one interface. This forces it onto the recovery
2055         # queue.
2056         $LNETCTL peer set --nid $prim_nid --health 0
2057
2058         # See comment for check_ping_count()
2059         sleep 4
2060         check_nid_in_recovq "-p" "1"
2061         check_ping_count "peer_ni" "2"
2062
2063         sleep 6
2064         check_nid_in_recovq "-p" "1"
2065         check_ping_count "peer_ni" "5"
2066
2067         $LCTL net_drop_del -a
2068
2069         do_lnetctl set max_recovery_ping_interval $default ||
2070                 error "failed to set max_recovery_ping_interval"
2071
2072         return 0
2073 }
2074 run_test 211 "Remote NI recovery checks"
2075
2076 test_212() {
2077         local rnodes=$(remote_nodes_list)
2078         [[ -z $rnodes ]] && skip "Need at least 1 remote node"
2079
2080         cleanup_lnet || error "Failed to cleanup before test execution"
2081
2082         # Loading modules should configure LNet with the appropriate
2083         # test-framework configuration
2084         load_lnet "config_on_load=1" || error "Failed to load modules"
2085
2086         local my_nid=$($LCTL list_nids | head -n 1)
2087         [[ -z $my_nid ]] &&
2088                 error "Failed to get primary NID for local host $HOSTNAME"
2089
2090         local rnode=$(awk '{print $1}' <<<$rnodes)
2091         local rnodenids=$(do_node $rnode $LCTL list_nids | xargs echo)
2092         local rloaded=false
2093
2094         if [[ -z $rnodenids ]]; then
2095                 do_rpc_nodes $rnode load_lnet "config_on_load=1"
2096                 rloaded=true
2097                 rnodenids=$(do_node $rnode $LCTL list_nids | xargs echo)
2098         fi
2099
2100         local rnodepnid=$(awk '{print $1}' <<< $rnodenids)
2101
2102         [[ -z $rnodepnid ]] &&
2103                 error "Failed to get primary NID for remote host $rnode"
2104
2105         log "Initial discovery"
2106         do_lnetctl discover --force $rnodepnid ||
2107                 error "Failed to discover $rnodepnid"
2108
2109         do_node $rnode "$LNETCTL discover --force $my_nid" ||
2110                 error "$rnode failed to discover $my_nid"
2111
2112         log "Fail local discover ping to set LNET_PEER_REDISCOVER flag"
2113         $LCTL net_drop_add -s "*@$NETTYPE" -d "*@$NETTYPE" -r 1 -e local_error
2114         do_lnetctl discover --force $rnodepnid &&
2115                 error "Discovery should have failed"
2116         $LCTL net_drop_del -a
2117
2118         local nid
2119         for nid in $rnodenids; do
2120                 # We need GET (PING) delay just long enough so we can trigger
2121                 # discovery on the remote peer
2122                 $LCTL net_delay_add -s "*@$NETTYPE" -d $nid -r 1 -m GET -l 3
2123                 $LCTL net_drop_add -s "*@$NETTYPE" -d $nid -r 1 -m GET -e local_error
2124                 # We need PUT (PUSH) delay just long enough so we can process
2125                 # the PING failure
2126                 $LCTL net_delay_add -s "*@$NETTYPE" -d $nid -r 1 -m PUT -l 6
2127         done
2128
2129         log "Force $HOSTNAME to discover $rnodepnid (in background)"
2130         # We want to get a PING sent that we know will eventually fail.
2131         # The delay rules we added will ensure the ping is not sent until
2132         # the PUSH is also in flight (see below), and the drop rule ensures that
2133         # when the PING is eventually sent it will error out
2134         do_lnetctl discover --force $rnodepnid &
2135         local pid1=$!
2136
2137         # We want a discovery PUSH from rnode to put rnode back on our
2138         # discovery queue. This should cause us to try and send a PUSH to rnode
2139         # while the PING is still outstanding.
2140         log "Force $rnode to discover $my_nid"
2141         do_node $rnode $LNETCTL discover --force $my_nid
2142
2143         # At this point we'll have both PING_SENT and PUSH_SENT set for the
2144         # rnode peer. Wait for the PING to error out which should terminate the
2145         # discovery process that we backgrounded.
2146         log "Wait for $pid1"
2147         wait $pid1
2148         log "Finished wait on $pid1"
2149
2150         # The PING send failure clears the PING_SENT flag and puts the peer back
2151         # on the discovery queue. When discovery thread processes the peer it
2152         # will mistakenly clear the PUSH_SENT flag (and set PUSH_FAILED).
2153         # Discovery will then complete for this peer even though we have an
2154         # outstanding PUSH.
2155         # When PUSH is actually unlinked it will be forced back onto the
2156         # discovery queue, but we no longer have a ref on the peer. When
2157         # discovery completes again, we'll trip the ASSERT in
2158         # lnet_destroy_peer_locked()
2159
2160         # Delete the delay rules to send the PUSH
2161         $LCTL net_delay_del -a
2162         # Delete the drop rules
2163         $LCTL net_drop_del -a
2164
2165         unload_modules ||
2166                 error "Failed to unload modules"
2167         if $rloaded; then
2168                 do_rpc_nodes $rnode unload_modules_local ||
2169                         error "Failed to unload modules on $rnode"
2170         fi
2171
2172         return 0
2173 }
2174 run_test 212 "Check discovery refcount loss bug (LU-14627)"
2175
2176 test_213() {
2177         cleanup_netns || error "Failed to cleanup netns before test execution"
2178         cleanup_lnet || error "Failed to unload modules before test execution"
2179
2180         setup_fakeif || error "Failed to add fake IF"
2181         have_interface "$FAKE_IF" ||
2182                 error "Expect $FAKE_IF configured but not found"
2183
2184         reinit_dlc || return $?
2185
2186         add_net "tcp" "${INTERFACES[0]}" || return $?
2187         add_net "tcp" "$FAKE_IF" || return $?
2188
2189         local nid1=$(lctl list_nids | head -n 1)
2190         local nid2=$(lctl list_nids | tail --lines 1)
2191
2192         [[ $(lctl which_nid $nid1 $nid2) == $nid1 ]] ||
2193                 error "Expect nid1 \"$nid1\" to be preferred"
2194
2195         [[ $(lctl which_nid $nid2 $nid1) == $nid2 ]] ||
2196                 error "Expect nid2 \"$nid2\" to be preferred"
2197
2198         return 0
2199 }
2200 run_test 213 "Check LNetDist calculation for multiple local NIDs"
2201
2202 function check_ni_status() {
2203         local nid="$1"
2204         local expect="$2"
2205
2206         local status=$($LNETCTL net show |
2207                        grep -A 1 ${nid} |
2208                        awk '/status/{print $NF}')
2209
2210         echo "NI ${nid} expect status \"${expect}\" found \"${status}\""
2211         if [[ $status != $expect ]]; then
2212                 error "Error: Expect NI status \"$expect\" for NID \"$nid\" but found \"$status\""
2213         fi
2214
2215         return 0
2216 }
2217
2218 test_214() {
2219         cleanup_netns || error "Failed to cleanup netns before test execution"
2220         cleanup_lnet || error "Failed to unload modules before test execution"
2221
2222         setup_fakeif || error "Failed to add fake IF"
2223         have_interface "$FAKE_IF" ||
2224                 error "Expect $FAKE_IF configured but not found"
2225
2226         reinit_dlc || return $?
2227
2228         add_net "tcp" "${INTERFACES[0]}" || return $?
2229         add_net "tcp" "$FAKE_IF" || return $?
2230
2231         local nid1=$(lctl list_nids | head -n 1)
2232         local nid2=$(lctl list_nids | tail --lines 1)
2233
2234         check_ni_status "0@lo" up
2235         check_ni_status "$nid1" up
2236         check_ni_status "$nid2" up
2237
2238         echo "Set $FAKE_IF down"
2239         echo "ip link set dev $FAKE_IF down"
2240         ip link set dev $FAKE_IF down
2241         check_ni_status "0@lo" up
2242         check_ni_status "$nid1" up
2243         check_ni_status "$nid2" down
2244 }
2245 run_test 214 "Check local NI status when link is downed"
2246
2247 get_ni_stat() {
2248         local nid=$1
2249         local stat=$2
2250
2251         $LNETCTL net show -v 2 |
2252                 egrep -e nid -e $stat |
2253                 grep -wA 1 $nid |
2254                 awk '/'$stat':/{print $NF}'
2255 }
2256
2257 ni_stats_pre() {
2258         local nidvar s
2259         for nidvar in nid1 nid2; do
2260                 for stat in send_count recv_count; do
2261                         s=$(get_ni_stat ${!nidvar} $stat)
2262                         eval ${nidvar}_pre_${stat}=$s
2263                 done
2264         done
2265 }
2266
2267 ni_stats_post() {
2268         local nidvar s
2269         for nidvar in nid1 nid2; do
2270                 for stat in send_count recv_count; do
2271                         s=$(get_ni_stat ${!nidvar} $stat)
2272                         eval ${nidvar}_post_${stat}=$s
2273                 done
2274         done
2275 }
2276
2277 ni_stat_changed() {
2278         local nidvar=$1
2279         local stat=$2
2280
2281         local pre post
2282         eval pre=\${${nidvar}_pre_${stat}}
2283         eval post=\${${nidvar}_post_${stat}}
2284
2285         echo "${!nidvar} pre ${stat} $pre post ${stat} $post"
2286
2287         [[ $pre -ne $post ]]
2288 }
2289
2290 test_215() {
2291         cleanup_netns || error "Failed to cleanup netns before test execution"
2292         cleanup_lnet || error "Failed to unload modules before test execution"
2293
2294         reinit_dlc || return $?
2295
2296         add_net "tcp1" "${INTERFACES[0]}" || return $?
2297         add_net "tcp2" "${INTERFACES[0]}" || return $?
2298
2299         local nid1=$($LCTL list_nids | head -n 1)
2300         local nid2=$($LCTL list_nids | tail --lines 1)
2301
2302         do_lnetctl peer add --prim $nid1 --nid $nid2 ||
2303                 error "Failed to add peer"
2304
2305         local npings=25
2306
2307         for nidvarA in nid1 nid2; do
2308                 src=${!nidvarA}
2309                 dst=${!nidvarA}
2310                 for nidvarB in nid1 nid2; do
2311                         [[ $nidvarA == $nidvarB ]] && continue
2312
2313                         ni_stats_pre
2314
2315                         echo "$LNETCTL ping $dst x $npings"
2316                         for i in $(seq 1 $npings); do
2317                                 $LNETCTL ping $dst &>/dev/null ||
2318                                         error "$LNETCTL ping $dst failed"
2319                         done
2320
2321                         ni_stats_post
2322
2323                         # No source specified, sends to either NID should cause
2324                         # counts to increase across both NIs
2325                         for nidvar in nid1 nid2; do
2326                                 for stat in send_count recv_count; do
2327                                         ni_stat_changed $nidvar $stat ||
2328                                                 error "$stat unchanged for ${!nidvar}"
2329                                 done
2330                         done
2331
2332                         ni_stats_pre
2333
2334                         echo "$LNETCTL ping --source $src $dst x $npings"
2335                         for i in $(seq 1 $npings); do
2336                                 $LNETCTL ping --source $src $dst &>/dev/null ||
2337                                         error "$LNETCTL ping --source $src $dst failed"
2338                         done
2339
2340                         ni_stats_post
2341
2342                         # src nid == dest nid means stats for the _other_ NI
2343                         # should be unchanged
2344                         for nidvar in nid1 nid2; do
2345                                 for stat in send_count recv_count; do
2346                                         if [[ ${!nidvar} == $src ]]; then
2347                                                 ni_stat_changed $nidvar $stat ||
2348                                                         error "$stat unchanged for ${!nidvar}"
2349                                         else
2350                                                 ni_stat_changed $nidvar $stat &&
2351                                                         error "$stat changed for ${!nidvar}"
2352                                         fi
2353                                 done
2354                         done
2355                 done
2356                 # Double number of pings for next iteration because the net
2357                 # sequence numbers will have diverged
2358                 npings=$(($npings * 2))
2359         done
2360
2361         # Ping from nid1 to nid2 should fail
2362         do_lnetctl ping --source $nid1 $nid2 &&
2363                 error "ping from $nid1 to $nid2 should fail"
2364
2365         # Ping from nid2 to nid1 should fail
2366         do_lnetctl ping --source $nid2 $nid1 &&
2367                 error "ping from $nid2 to $nid1 should fail"
2368
2369         return 0
2370 }
2371 run_test 215 "Test lnetctl ping --source option"
2372
2373 test_216() {
2374         local rc=0
2375
2376         reinit_dlc || return $?
2377
2378         add_net "tcp" "${INTERFACES[0]}" || return $?
2379         add_net "tcp1" "${INTERFACES[0]}" || return $?
2380
2381         local nids=( $($LCTL list_nids | xargs echo) )
2382
2383         do_lnetctl discover ${nids[0]} ||
2384                 error "Initial discovery failed"
2385
2386         do_lnetctl ping --source ${nids[0]} ${nids[0]} ||
2387                 error "Initial ping failed $?"
2388
2389         do_lnetctl ping --source ${nids[1]} ${nids[1]} ||
2390                 error "Initial ping failed $?"
2391
2392         local src dst
2393         for src in "${nids[@]}"; do
2394                 for dst in "${nids[@]}"; do
2395                         $LCTL net_drop_add -r 1 -s $src -d $dst -e network_timeout
2396                 done
2397         done
2398
2399         do_lnetctl ping ${nids[0]} || rc=$?
2400
2401         $LCTL net_drop_del -a
2402
2403         [[ $rc -eq 0 ]] &&
2404                 error "expected ping to fail"
2405
2406         check_nid_in_recovq "-p" 0
2407         check_nid_in_recovq "-l" 1
2408
2409         return 0
2410 }
2411 run_test 216 "Failed send to peer NI owned by local host should not trigger peer NI recovery"
2412
2413 test_217() {
2414         reinit_dlc || return $?
2415
2416         [[ $($LNETCTL net show | grep -c nid) -ne 1 ]] &&
2417                 error "Unexpected number of NIs after initalizing DLC"
2418
2419         do_lnetctl discover 0@lo ||
2420                 error "Failed to discover 0@lo"
2421
2422         unload_modules
2423 }
2424 run_test 217 "Don't leak memory when discovering peer with nnis <= 1"
2425
2426 test_218() {
2427         reinit_dlc || return $?
2428
2429         [[ ${#INTERFACES[@]} -lt 2 ]] &&
2430                 skip "Need two LNet interfaces"
2431
2432         add_net "tcp" "${INTERFACES[0]}" || return $?
2433
2434         local nid1=$($LCTL list_nids | head -n 1)
2435
2436         do_lnetctl ping $nid1 ||
2437                 error "ping failed"
2438
2439         add_net "tcp" "${INTERFACES[1]}" || return $?
2440
2441         local nid2=$($LCTL list_nids | tail --lines 1)
2442
2443         do_lnetctl ping $nid2 ||
2444                 error "ping failed"
2445
2446         $LCTL net_drop_add -s $nid1 -d $nid1 -e local_error -r 1
2447
2448         do_lnetctl ping $nid1 &&
2449                 error "ping should have failed"
2450
2451         local health_recovered
2452         local i
2453
2454         for i in $(seq 1 5); do
2455                 health_recovered=$($LNETCTL net show -v 2 |
2456                                    grep -c 'health value: 1000')
2457
2458                 if [[ $health_recovered -ne 2 ]]; then
2459                         echo "Wait 1 second for health to recover"
2460                         sleep 1
2461                 else
2462                         break
2463                 fi
2464         done
2465
2466         health_recovered=$($LNETCTL net show -v 2 |
2467                            grep -c 'health value: 1000')
2468
2469         $LCTL net_drop_del -a
2470
2471         [[ $health_recovered -ne 2 ]] &&
2472                 do_lnetctl net show -v 2 | egrep -e nid -e health &&
2473                 error "Health hasn't recovered"
2474
2475         return 0
2476 }
2477 run_test 218 "Local recovery pings should exercise all available paths"
2478
2479 test_219() {
2480         reinit_dlc || return $?
2481         add_net "tcp" "${INTERFACES[0]}" || return $?
2482         add_net "tcp1" "${INTERFACES[0]}" || return $?
2483
2484         local nid1=$(lctl list_nids | head -n 1)
2485         local nid2=$(lctl list_nids | tail --lines 1)
2486
2487         do_lnetctl ping $nid1 ||
2488                 error "Ping failed $?"
2489         do_lnetctl ping $nid2 ||
2490                 error "Ping failed $?"
2491
2492         do_lnetctl discover $nid2 ||
2493                 error "Discovery failed"
2494
2495         $LNETCTL peer show --nid $nid1 | grep -q $nid2 ||
2496                 error "$nid2 is not listed under $nid1"
2497 }
2498 run_test 219 "Consolidate peer entries"
2499
2500 do_net_add() {
2501         local node=$1
2502         local net=$2
2503         local if=$3
2504
2505         do_rpc_nodes $node "$LNETCTL net add --net $net --if $if" ||
2506                 error "add $net on interface $if on node $node failed rc=$?"
2507 }
2508
2509 do_route_add() {
2510         local node=$1
2511         local net=$2
2512         local gw=$3
2513
2514         do_node $node "$LNETCTL route add --net $net --gateway $gw" ||
2515                 error "route add to $net via $gw failed rc=$?"
2516 }
2517
2518 ROUTER=""
2519 ROUTER_INTERFACES=()
2520 RPEER=""
2521 RPEER_INTERFACES=()
2522 init_router_test_vars() {
2523         local rnodes=$(remote_nodes_list)
2524         [[ -z $rnodes || $(wc -w <<<$rnodes) -lt 2 ]] &&
2525                 skip "Need at least 2 remote nodes found \"$rnodes\""
2526
2527         ROUTER=$(awk '{print $1}' <<<$rnodes)
2528         RPEER=$(awk '{print $2}' <<<$rnodes)
2529
2530         rnodes=$(comma_list $ROUTER $RPEER)
2531         local all_nodes=$(comma_list $rnodes $HOSTNAME)
2532
2533         do_nodes $rnodes $LUSTRE_RMMOD ||
2534                 error "failed to unload modules"
2535
2536         do_rpc_nodes $rnodes "load_lnet config_on_load=1" ||
2537                 error "Failed to load and configure LNet"
2538
2539         ROUTER_INTERFACES=( $(do_rpc_nodes --quiet $ROUTER lnet_if_list) )
2540
2541         RPEER_INTERFACES=( $(do_rpc_nodes --quiet $RPEER lnet_if_list) )
2542
2543         do_nodes $all_nodes $LUSTRE_RMMOD ||
2544                 error "Failed to unload modules"
2545
2546         [[ ${#INTERFACES[@]} -eq 0 ]] &&
2547                 error "No interfaces configured for local host $HOSTNAME"
2548         [[ ${#ROUTER_INTERFACES[@]} -eq 0 ]] &&
2549                 error "No interfaces configured for router $ROUTER"
2550         [[ ${#RPEER_INTERFACES[@]} -eq 0 ]] &&
2551                 error "No interfaces configured for remote peer $RPEER"
2552
2553         return 0
2554 }
2555
2556 ROUTER_NIDS=()
2557 RPEER_NIDS=()
2558 LNIDS=()
2559 LOCAL_NET=${NETTYPE}1
2560 REMOTE_NET=${NETTYPE}2
2561 setup_router_test() {
2562         local mod_opts="$@"
2563
2564         if [[ ${#RPEER_INTERFACES[@]} -eq 0 ]]; then
2565                 init_router_test_vars ||
2566                         return $?
2567         fi
2568
2569         local all_nodes=$(comma_list $ROUTER $RPEER $HOSTNAME)
2570
2571         do_nodes $all_nodes $LUSTRE_RMMOD ||
2572                 error "failed to unload modules"
2573
2574         mod_opts+=" alive_router_check_interval=5"
2575         mod_opts+=" router_ping_timeout=5"
2576         do_rpc_nodes $all_nodes load_lnet "${mod_opts}" ||
2577                 error "Failed to load lnet"
2578
2579         do_nodes $all_nodes "$LNETCTL lnet configure" ||
2580                 error "Failed to initialize DLC"
2581
2582         do_net_add $ROUTER $LOCAL_NET ${ROUTER_INTERFACES[0]} ||
2583                 return $?
2584
2585         do_net_add $ROUTER $REMOTE_NET ${ROUTER_INTERFACES[0]} ||
2586                 return $?
2587
2588         do_net_add $RPEER $REMOTE_NET ${RPEER_INTERFACES[0]} ||
2589                 return $?
2590
2591         add_net $LOCAL_NET ${INTERFACES[0]} ||
2592                 return $?
2593
2594         ROUTER_NIDS=( $(do_node $ROUTER $LCTL list_nids 2>/dev/null |
2595                         xargs echo) )
2596         RPEER_NIDS=( $(do_node $RPEER $LCTL list_nids 2>/dev/null |
2597                        xargs echo) )
2598         LNIDS=( $($LCTL list_nids 2>/dev/null | xargs echo) )
2599 }
2600
2601 do_route_del() {
2602         local node=$1
2603         local net=$2
2604         local gw=$3
2605
2606         do_nodesv $node "if $LNETCTL route show --net $net --gateway $gw; then \
2607                                 $LNETCTL route del --net $net --gateway $gw;   \
2608                          else                                                  \
2609                                 exit 0;                                        \
2610                          fi"
2611 }
2612
2613 cleanup_router_test() {
2614         local all_nodes=$(comma_list $HOSTNAME $ROUTER $RPEER)
2615
2616         do_route_del $HOSTNAME $REMOTE_NET ${ROUTER_NIDS[0]} ||
2617                 error "Failed to delete $REMOTE_NET route"
2618
2619         do_route_del $RPEER $LOCAL_NET ${ROUTER_NIDS[1]} ||
2620                 error "Failed to delete $LOCAL_NET route"
2621
2622         do_nodes $all_nodes $LUSTRE_RMMOD ||
2623                 error "failed to unload modules"
2624
2625         return 0
2626 }
2627
2628 check_route_aliveness() {
2629         local node="$1"
2630         local expected="$2"
2631
2632         local lctl_actual
2633         local lnetctl_actual
2634         local chk_intvl
2635         local i
2636
2637         chk_intvl=$(cat /sys/module/lnet/parameters/alive_router_check_interval)
2638
2639         lctl_actual=$(do_node $node $LCTL show_route | awk '{print $7}')
2640         lnetctl_actual=$(do_node $node $LNETCTL route show -v |
2641                          awk '/state/{print $NF}')
2642
2643         for ((i = 0; i < $chk_intvl; i++)); do
2644                 if [[ $lctl_actual == $expected ]] &&
2645                    [[ $lnetctl_actual == $expected ]]; then
2646                         break
2647                 fi
2648
2649                 echo "wait 1s for route state change"
2650                 sleep 1
2651
2652                 lctl_actual=$(do_node $node $LCTL show_route | awk '{print $7}')
2653                 lnetctl_actual=$(do_node $node $LNETCTL route show -v |
2654                                  awk '/state/{print $NF}')
2655         done
2656
2657         [[ $lctl_actual != $expected ]] &&
2658                 error "Wanted \"$expected\" lctl found \"$lctl_actual\""
2659
2660         [[ $lnetctl_actual != $expected ]] &&
2661                 error "Wanted \"$expected\" lnetctl found \"$lnetctl_actual\""
2662
2663         return 0
2664 }
2665
2666 check_router_ni_status() {
2667         local expected_local="$1"
2668         local expected_remote="$2"
2669
2670         local actual_local
2671         local actual_remote
2672         local chk_intvl
2673         local timeout
2674         local i
2675
2676         chk_intvl=$(cat /sys/module/lnet/parameters/alive_router_check_interval)
2677         timeout=$(cat /sys/module/lnet/parameters/router_ping_timeout)
2678
2679         actual_local=$(do_node $ROUTER "$LNETCTL net show --net $LOCAL_NET" |
2680                        awk '/status/{print $NF}')
2681         actual_remote=$(do_node $ROUTER "$LNETCTL net show --net $REMOTE_NET" |
2682                         awk '/status/{print $NF}')
2683
2684         for ((i = 0; i < $((chk_intvl + timeout)); i++)); do
2685                 if [[ $actual_local == $expected_local ]] &&
2686                    [[ $actual_remote == $expected_remote ]]; then
2687                         break
2688                 fi
2689
2690                 echo "wait 1s for NI state change"
2691                 sleep 1
2692
2693                 actual_local=$(do_node $ROUTER \
2694                                "$LNETCTL net show --net $LOCAL_NET" |
2695                                 awk '/status/{print $NF}')
2696                 actual_remote=$(do_node $ROUTER \
2697                                 "$LNETCTL net show --net $REMOTE_NET" |
2698                                 awk '/status/{print $NF}')
2699         done
2700
2701         [[ $actual_local == $expected_local ]] ||
2702                 error "$LOCAL_NET should be $expected_local"
2703
2704         [[ $actual_remote == $expected_remote ]] ||
2705                 error "$REMOTE_NET should be $expected_remote"
2706
2707         return 0
2708 }
2709
2710 do_basic_rtr_test() {
2711         do_node $ROUTER "$LNETCTL set routing 1" ||
2712                 error "Unable to enable routing on $ROUTER"
2713
2714         do_route_add $HOSTNAME $REMOTE_NET ${ROUTER_NIDS[0]} ||
2715                 return $?
2716
2717         do_route_add $RPEER $LOCAL_NET ${ROUTER_NIDS[1]} ||
2718                 return $?
2719
2720         check_route_aliveness "$HOSTNAME" "up" ||
2721                 return $?
2722
2723         check_route_aliveness "$RPEER" "up" ||
2724                 return $?
2725
2726         do_lnetctl ping ${RPEER_NIDS[0]} ||
2727                 error "Failed to ping ${RPEER_NIDS[0]}"
2728
2729         do_node $RPEER "$LNETCTL ping ${LNIDS[0]}" ||
2730                 error "$RPEER failed to ping ${LNIDS[0]}"
2731
2732         return 0
2733 }
2734
2735 test_220() {
2736         setup_router_test || return $?
2737
2738         do_basic_rtr_test || return $?
2739
2740         cleanup_router_test || return $?
2741 }
2742 run_test 220 "Add routes w/default options - check aliveness"
2743
2744 test_221() {
2745         setup_router_test lnet_peer_discovery_disabled=1 || return $?
2746
2747         do_basic_rtr_test || return $?
2748
2749         cleanup_router_test || return $?
2750 }
2751 run_test 221 "Add routes w/DD disabled - check aliveness"
2752
2753 do_aarf_enabled_test() {
2754         do_node $ROUTER "$LNETCTL set routing 1" ||
2755                 error "Unable to enable routing on $ROUTER"
2756
2757         check_router_ni_status "down" "down"
2758
2759         do_lnetctl ping ${RPEER_NIDS[0]} &&
2760                 error "Ping should fail"
2761
2762         do_node $RPEER "$LNETCTL ping ${LNIDS[0]}" &&
2763                 error "$RPEER ping should fail"
2764
2765         # Adding a route should cause the router's NI on LOCAL_NET to get up
2766         do_route_add $HOSTNAME $REMOTE_NET ${ROUTER_NIDS[0]} ||
2767                 return $?
2768
2769         check_router_ni_status "up" "down" ||
2770                 return $?
2771
2772         # But route should still be down because of avoid_asym_router_failure
2773         check_route_aliveness "$HOSTNAME" "down" ||
2774                 return $?
2775
2776         do_lnetctl ping ${RPEER_NIDS[0]} &&
2777                 error "Ping should fail"
2778
2779         do_node $RPEER "$LNETCTL ping ${LNIDS[0]}" &&
2780                 error "$RPEER ping should fail"
2781
2782         # Adding the symmetric route should cause the remote NI to go up and
2783         # routes to go up
2784         do_route_add $RPEER $LOCAL_NET ${ROUTER_NIDS[1]} ||
2785                 return $?
2786
2787         check_router_ni_status "up" "up" ||
2788                 return $?
2789
2790         check_route_aliveness "$HOSTNAME" "up" ||
2791                 return $?
2792
2793         check_route_aliveness "$RPEER" "up" ||
2794                 return $?
2795
2796         do_lnetctl ping ${RPEER_NIDS[0]} ||
2797                 error "Failed to ping ${RPEER_NIDS[0]}"
2798
2799         do_node $RPEER "$LNETCTL ping ${LNIDS[0]}" ||
2800                 error "$RPEER failed to ping ${LNIDS[0]}"
2801
2802         # Stop LNet on local host
2803         do_lnetctl lnet unconfigure ||
2804                 error "Failed to stop LNet rc=$?"
2805
2806         check_router_ni_status "down" "up" ||
2807                 return $?
2808
2809         check_route_aliveness "$RPEER" "down" ||
2810                 return $?
2811
2812         do_lnetctl ping ${RPEER_NIDS[0]} &&
2813                 error "Ping should fail"
2814
2815         do_node $RPEER "$LNETCTL ping ${LNIDS[0]}" &&
2816                 error "$RPEER ping should fail"
2817
2818         return 0
2819 }
2820
2821 test_222() {
2822         setup_router_test avoid_asym_router_failure=1 || return $?
2823
2824         do_aarf_enabled_test || return $?
2825
2826         cleanup_router_test || return $?
2827 }
2828 run_test 222 "Check avoid_asym_router_failure=1"
2829
2830 test_223() {
2831         local opts="avoid_asym_router_failure=1 lnet_peer_discovery_disabled=1"
2832
2833         setup_router_test $opts || return $?
2834
2835         do_aarf_enabled_test || return $?
2836
2837         cleanup_router_test || return $?
2838 }
2839 run_test 223 "Check avoid_asym_router_failure=1 w/DD disabled"
2840
2841 do_aarf_disabled_test() {
2842         do_node $ROUTER "$LNETCTL set routing 1" ||
2843                 error "Unable to enable routing on $ROUTER"
2844
2845         check_router_ni_status "down" "down"
2846
2847         do_route_add $HOSTNAME $REMOTE_NET ${ROUTER_NIDS[0]} ||
2848                 return $?
2849
2850         check_router_ni_status "up" "down" ||
2851                 return $?
2852
2853         check_route_aliveness "$HOSTNAME" "up" ||
2854                 return $?
2855
2856         do_route_add $RPEER $LOCAL_NET ${ROUTER_NIDS[1]} ||
2857                 return $?
2858
2859         check_router_ni_status "up" "up" ||
2860                 return $?
2861
2862         check_route_aliveness "$HOSTNAME" "up" ||
2863                 return $?
2864
2865         check_route_aliveness "$RPEER" "up" ||
2866                 return $?
2867
2868         do_lnetctl ping ${RPEER_NIDS[0]} ||
2869                 error "Failed to ping ${RPEER_NIDS[0]}"
2870
2871         do_node $RPEER "$LNETCTL ping ${LNIDS[0]}" ||
2872                 error "$RPEER failed to ping ${LNIDS[0]}"
2873
2874         # Stop LNet on local host
2875         do_lnetctl lnet unconfigure ||
2876                 error "Failed to stop LNet rc=$?"
2877
2878         check_router_ni_status "down" "up" ||
2879                 return $?
2880
2881         check_route_aliveness "$RPEER" "up" ||
2882                 return $?
2883
2884         return 0
2885 }
2886
2887 test_224() {
2888         setup_router_test avoid_asym_router_failure=0 ||
2889                 return $?
2890
2891         do_aarf_disabled_test ||
2892                 return $?
2893
2894         cleanup_router_test ||
2895                 return $?
2896 }
2897 run_test 224 "Check avoid_asym_router_failure=0"
2898
2899 test_225() {
2900         local opts="avoid_asym_router_failure=0 lnet_peer_discovery_disabled=1"
2901
2902         setup_router_test $opts || return $?
2903
2904         do_aarf_disabled_test || return $?
2905
2906         cleanup_router_test ||
2907                 return $?
2908 }
2909 run_test 225 "Check avoid_asym_router_failure=0 w/DD disabled"
2910
2911 test_230() {
2912         # LU-12815
2913         echo "Check valid values; Should succeed"
2914         local i
2915         local lnid
2916         local cmd
2917         for ((i = 4; i < 16; i+=1)); do
2918                 reinit_dlc || return $?
2919                 add_net "tcp" "${INTERFACES[0]}" || return $?
2920                 do_lnetctl net set --all --conns-per-peer $i ||
2921                         error "should have succeeded $?"
2922                 $LNETCTL net show -v 1 | grep -q "conns_per_peer: $i" ||
2923                         error "failed to set conns-per-peer to $i"
2924                 lnid="$(lctl list_nids | head -n 1)"
2925                 do_lnetctl ping "$lnid" ||
2926                         error "failed to ping myself"
2927
2928                 # "lctl --net tcp conn_list" prints the list of active
2929                 # connections. Since we're pinging ourselves, there should be
2930                 # 2 Control connections plus 2*conns_per_peer connections
2931                 # created (one Bulk Input, one Bulk Output in each pair).
2932                 # Here's the sample output for conns_per_peer set to 1:
2933                 # 12345-1.1.1.1@tcp I[0]host01->host01:988 2626560/1061296 nonagle
2934                 # 12345-1.1.1.1@tcp O[0]host01->host01:1022 2626560/1061488 nonagle
2935                 # 12345-1.1.1.1@tcp C[0]host01->host01:988 2626560/1061296 nonagle
2936                 # 12345-1.1.1.1@tcp C[0]host01->host01:1023 2626560/1061488 nonagle
2937                 cmd="printf 'network tcp\nconn_list\n' | lctl | grep -c '$lnid'"
2938
2939                 # Expect 2+conns_per_peer*2 connections. Wait no longer
2940                 # than 2 seconds.
2941                 wait_update $HOSTNAME "$cmd" "$((2+i*2))" 2 ||
2942                         error "expected number of tcp connections $((2+i*2))"
2943         done
2944
2945         reinit_dlc || return $?
2946         add_net "tcp" "${INTERFACES[0]}" || return $?
2947         echo "Set > 127; Should fail"
2948         do_lnetctl net set --all --conns-per-peer 128 &&
2949                 error "should have failed $?"
2950
2951         reinit_dlc || return $?
2952         add_net "tcp" "${INTERFACES[0]}" || return $?
2953         echo "Set < 0; Should be ignored"
2954         do_lnetctl net set --all --conns-per-peer -1 ||
2955                 error "should have succeeded $?"
2956         $LNETCTL net show -v 1 | grep -q "conns_per_peer: 1" ||
2957                 error "Did not stay at default"
2958 }
2959 run_test 230 "Test setting conns-per-peer"
2960
2961 ### Test that linux route is added for each ni
2962 test_250() {
2963         reinit_dlc || return $?
2964         add_net "tcp" "${INTERFACES[0]}" || return $?
2965         ip route show table ${INTERFACES[0]} | grep -q "${INTERFACES[0]}"
2966 }
2967 run_test 250 "test that linux routes are added"
2968
2969 test_300() {
2970         # LU-13274
2971         local header
2972         local out=$TMP/$tfile
2973         local prefix=/usr/include/linux/lnet
2974
2975         # We use a hard coded prefix so that this test will not fail
2976         # when run in tree.
2977         CC=${CC:-cc}
2978         if ! which $CC > /dev/null 2>&1; then
2979                 skip_env "$CC is not installed"
2980         fi
2981
2982         cleanup_lnet || exit 1
2983         load_lnet
2984
2985         local cc_args="-Wall -Werror -std=c99 -c -x c /dev/null -o $out"
2986         if ! [[ -d $prefix ]]; then
2987                 # Assume we're running in tree and fixup the include path.
2988                 prefix=$LUSTRE/../lnet/include/uapi/linux/lnet
2989                 cc_args+=" -I $LUSTRE/../lnet/include/uapi"
2990         fi
2991
2992         for header in $prefix/*.h; do
2993                 if ! [[ -f "$header" ]]; then
2994                         continue
2995                 fi
2996
2997                 echo "$CC $cc_args -include $header"
2998                 $CC $cc_args -include $header ||
2999                         error "cannot compile '$header'"
3000         done
3001         rm -f $out
3002 }
3003 run_test 300 "packaged LNet UAPI headers can be compiled"
3004
3005 complete $SECONDS
3006
3007 cleanup_testsuite
3008 exit_status