Whamcloud - gitweb
LU-15234 lnet: add mechanism for dumping lnd peer debug info
[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 args="-m GET -r 1 -e ${1}"
1527         local src dst
1528
1529         for src in "${LNIDS[@]}"; do
1530                 for dst in "${RNIDS[@]}" "${LNIDS[@]}"; do
1531                         $LCTL net_drop_add -s $src -d $dst ${args} ||
1532                                 error "Failed to add drop rule $src $dst $args"
1533                 done
1534         done
1535 }
1536
1537 do_lnet_health_ping_test() {
1538         local hstatus="$1"
1539
1540         echo "Simulate $hstatus"
1541
1542         lnet_health_pre || return $?
1543
1544         add_health_test_drop_rules ${hstatus}
1545         do_lnetctl ping ${RNIDS[0]} &&
1546                 error "Should have failed"
1547
1548         lnet_health_post
1549
1550         $LCTL net_drop_del -a
1551
1552         return 0
1553 }
1554
1555 # See lnet/lnet/lib-msg.c:lnet_health_check()
1556 LNET_LOCAL_RESEND_STATUSES="local_interrupt local_dropped local_aborted"
1557 LNET_LOCAL_RESEND_STATUSES+=" local_no_route local_timeout"
1558 LNET_LOCAL_NO_RESEND_STATUSES="local_error"
1559 test_204() {
1560         setup_health_test false || return $?
1561
1562         local hstatus
1563         for hstatus in ${LNET_LOCAL_RESEND_STATUSES} \
1564                        ${LNET_LOCAL_NO_RESEND_STATUSES}; do
1565                 do_lnet_health_ping_test "${hstatus}" || return $?
1566                 check_no_resends || return $?
1567                 check_no_local_health || return $?
1568         done
1569
1570         cleanup_health_test || return $?
1571
1572         return 0
1573 }
1574 run_test 204 "Check no health or resends for single-rail local failures"
1575
1576 test_205() {
1577         setup_health_test true || return $?
1578
1579         local hstatus
1580         for hstatus in ${LNET_LOCAL_RESEND_STATUSES}; do
1581                 do_lnet_health_ping_test "${hstatus}" || return $?
1582                 check_resends || return $?
1583                 check_local_health || return $?
1584         done
1585
1586         for hstatus in ${LNET_LOCAL_NO_RESEND_STATUSES}; do
1587                 do_lnet_health_ping_test "${hstatus}" || return $?
1588                 check_no_resends || return $?
1589                 check_local_health || return $?
1590         done
1591
1592         cleanup_health_test || return $?
1593
1594         return 0
1595 }
1596 run_test 205 "Check health and resends for multi-rail local failures"
1597
1598 # See lnet/lnet/lib-msg.c:lnet_health_check()
1599 LNET_REMOTE_RESEND_STATUSES="remote_dropped"
1600 LNET_REMOTE_NO_RESEND_STATUSES="remote_error remote_timeout"
1601 test_206() {
1602         setup_health_test false || return $?
1603
1604         local hstatus
1605         for hstatus in ${LNET_REMOTE_RESEND_STATUSES} \
1606                        ${LNET_REMOTE_NO_RESEND_STATUSES}; do
1607                 do_lnet_health_ping_test "${hstatus}" || return $?
1608                 check_no_resends || return $?
1609                 check_no_local_health || return $?
1610                 check_no_remote_health || return $?
1611         done
1612
1613         cleanup_health_test || return $?
1614
1615         return 0
1616 }
1617 run_test 206 "Check no health or resends for single-rail remote failures"
1618
1619 test_207() {
1620         setup_health_test true || return $?
1621
1622         local hstatus
1623         for hstatus in ${LNET_REMOTE_RESEND_STATUSES}; do
1624                 do_lnet_health_ping_test "${hstatus}" || return $?
1625                 check_resends || return $?
1626                 check_no_local_health || return $?
1627                 check_remote_health || return $?
1628                 do_lnetctl peer set --health 1000 --all ||
1629                         error "Unable to reset health rc=$?"
1630         done
1631         for hstatus in ${LNET_REMOTE_NO_RESEND_STATUSES}; do
1632                 do_lnet_health_ping_test "${hstatus}" || return $?
1633                 check_no_resends || return $?
1634                 check_no_local_health || return $?
1635                 check_remote_health || return $?
1636                 do_lnetctl peer set --health 1000 --all ||
1637                         error "Unable to reset health rc=$?"
1638         done
1639
1640         cleanup_health_test || return $?
1641
1642         return 0
1643 }
1644 run_test 207 "Check health and resends for multi-rail remote errors"
1645
1646 test_208_load_and_check_lnet() {
1647         local ip2nets="$1"
1648         local p_nid="$2"
1649         local s_nid="$3"
1650         local num_expected=1
1651
1652         load_lnet "networks=\"\" ip2nets=\"${ip2nets_str}\""
1653
1654         $LCTL net up ||
1655                 error "Failed to load LNet with ip2nets \"${ip2nets_str}\""
1656
1657         [[ -n $s_nid ]] &&
1658                 num_expected=2
1659
1660         declare -a nids
1661         nids=( $($LCTL list_nids) )
1662
1663         [[ ${#nids[@]} -ne ${num_expected} ]] &&
1664                 error "Expect ${num_expected} NIDs found ${#nids[@]}"
1665
1666         [[ ${nids[0]} == ${p_nid} ]] ||
1667                 error "Expect NID \"${p_nid}\" found \"${nids[0]}\""
1668
1669         [[ -n $s_nid ]] && [[ ${nids[1]} != ${s_nid} ]] &&
1670                 error "Expect second NID \"${s_nid}\" found \"${nids[1]}\""
1671
1672         $LCTL net down &>/dev/null
1673         cleanup_lnet
1674 }
1675
1676 test_208() {
1677         cleanup_netns || error "Failed to cleanup netns before test execution"
1678         cleanup_lnet || error "Failed to unload modules before test execution"
1679         setup_fakeif || error "Failed to add fake IF"
1680
1681         have_interface "$FAKE_IF" ||
1682                 error "Expect $FAKE_IF configured but not found"
1683
1684         local if0_ip=$(ip --oneline addr show dev ${INTERFACES[0]} |
1685                        awk '/inet /{print $4}' |
1686                        sed 's:/.*::')
1687         if0_ip=($(echo "${if0_ip[@]}" | tr ' ' '\n' | uniq | tr '\n' ' '))
1688         local ip2nets_str="tcp(${INTERFACES[0]}) $if0_ip"
1689
1690         echo "Configure single NID \"$ip2nets_str\""
1691         test_208_load_and_check_lnet "${ip2nets_str}" "${if0_ip}@tcp"
1692
1693         ip2nets_str="tcp(${INTERFACES[0]}) $if0_ip; tcp1($FAKE_IF) $FAKE_IP"
1694         echo "Configure two NIDs; two NETs \"$ip2nets_str\""
1695         test_208_load_and_check_lnet "${ip2nets_str}" "${if0_ip}@tcp" \
1696                                      "${FAKE_IP}@tcp1"
1697
1698         ip2nets_str="tcp(${INTERFACES[0]}) $if0_ip; tcp($FAKE_IF) $FAKE_IP"
1699         echo "Configure two NIDs; one NET \"$ip2nets_str\""
1700         test_208_load_and_check_lnet "${ip2nets_str}" "${if0_ip}@tcp" \
1701                                      "${FAKE_IP}@tcp"
1702         local addr1=( ${if0_ip//./ } )
1703         local addr2=( ${FAKE_IP//./ } )
1704         local range="[${addr1[0]},${addr2[0]}]"
1705
1706         local i
1707         for i in $(seq 1 3); do
1708                 range+=".[${addr1[$i]},${addr2[$i]}]"
1709         done
1710         ip2nets_str="tcp(${INTERFACES[0]},${FAKE_IF}) ${range}"
1711
1712         echo "Configured two NIDs; one NET alt syntax \"$ip2nets_str\""
1713         test_208_load_and_check_lnet "${ip2nets_str}" "${if0_ip}@tcp" \
1714                                      "${FAKE_IP}@tcp"
1715
1716         cleanup_fakeif
1717
1718         echo "alt syntax with missing IF \"$ip2nets_str\""
1719         load_lnet "networks=\"\" ip2nets=\"${ip2nets_str}\""
1720
1721         echo "$LCTL net up should fail"
1722         $LCTL net up &&
1723                 error "LNet bringup should have failed"
1724
1725         cleanup_lnet
1726 }
1727 run_test 208 "Test various kernel ip2nets configurations"
1728
1729 test_209() {
1730         setup_health_test false || return $?
1731
1732         echo "Simulate network_timeout w/SR config"
1733         lnet_health_pre
1734
1735         add_health_test_drop_rules network_timeout
1736
1737         do_lnetctl discover ${RNIDS[0]} &&
1738                 error "Should have failed"
1739
1740         lnet_health_post
1741
1742         check_no_resends || return $?
1743         check_no_local_health || return $?
1744         check_no_remote_health || return $?
1745
1746         cleanup_health_test || return $?
1747
1748         setup_health_test true || return $?
1749
1750         echo "Simulate network_timeout w/MR config"
1751
1752         lnet_health_pre
1753
1754         add_health_test_drop_rules network_timeout
1755
1756         do_lnetctl discover ${RNIDS[0]} &&
1757                 error "Should have failed"
1758
1759         lnet_health_post
1760
1761         check_no_resends || return $?
1762         check_local_health || return $?
1763         check_remote_health || return $?
1764
1765         cleanup_health_test || return $?
1766
1767         return 0
1768 }
1769 run_test 209 "Check health, but not resends, for network timeout"
1770
1771 check_nid_in_recovq() {
1772         local recovq=$($LNETCTL debug recovery $1)
1773         local expect="$2"
1774         local nids=$($LCTL list_nids | xargs echo)
1775         local found=false
1776         local nid=""
1777
1778         echo "Check \"$1\" recovery queue"
1779         echo "$recovq"
1780         if [[ $(grep -c 'nid-'<<<$recovq) -ne $expect ]]; then
1781                 error "Expect $expect NIDs found: \"$recovq\""
1782         fi
1783
1784         [[ $expect -eq 0 ]] && return 0
1785
1786         for nid in ${nids}; do
1787                 grep -q "nid-0: $nid"<<<$recovq &&
1788                         found=true
1789         done
1790
1791         if ! $found; then
1792                 error "Didn't find local NIDs in recovery queue: \"$recovq\""
1793         fi
1794
1795         return 0
1796 }
1797
1798 # First enqueue happens at time 0.
1799 # 2nd at 0 + 2^0 = 1
1800 # 3rd at 1 + 2^1 = 3
1801 # 4th at 3 + 2^2 = 7
1802 # 5th at 7 + 2^3 = 15
1803 # e.g. after 10 seconds we would expect to have seen the 4th enqueue,
1804 # (3 pings sent, 4th about to happen) and the 5th enqueue is yet to
1805 # happen
1806 # If the recovery limit is 10 seconds, then when the 5th enqueue happens
1807 # we expect the peer NI to have aged out, so it will not actually be
1808 # queued.
1809 # If max_recovery_ping_interval is set to 4 then:
1810 #  First enqueue happens at time 0.
1811 #  2nd at 0 + min(2^0, 4) = 1
1812 #  3rd at 1 + min(2^1, 4) = 3
1813 #  4th at 3 + min(2^2, 4) = 7
1814 #  5th at 7 + min(2^3, 4) = 11
1815 #  6th at 11 + min(2^4, 4) = 15
1816 #  7th at 15 + min(2^5, 4) = 19
1817 # e.g. after 4 seconds we would expect to have seen the 3rd enqueue,
1818 # (2 pings sent, 3rd about to happen), and the 4th enqueue is yet to happen
1819 # e.g. after 13 seconds we would expect to have seen the 5th enqueue,
1820 # (4 pings sent, 5th about to happen), and the 6th enqueue is yet to happen
1821 check_ping_count() {
1822         local queue="$1"
1823         local expect="$2"
1824
1825         echo "Check ping counts:"
1826         local ping_count
1827         if [[ $queue == "ni" ]]; then
1828                 $LNETCTL net show -v 2 | egrep 'nid|health value|ping'
1829                 ping_count=( $($LNETCTL net show -v 2 |
1830                                 awk '/ping_count/{print $NF}') )
1831         elif [[ $queue == "peer_ni" ]]; then
1832                 $LNETCTL peer show -v 2 | egrep 'nid|health value|ping'
1833                 ping_count=( $($LNETCTL peer show -v 2 |
1834                                 awk '/ping_count/{print $NF}') )
1835         else
1836                 error "Unrecognized queue \"$queue\""
1837                 return 1
1838         fi
1839
1840         local count
1841         local found=false
1842         for count in "${ping_count[@]}"; do
1843                 if [[ $count -eq $expect ]]; then
1844                         if [[ $expect -ne 0 ]] && $found ; then
1845                                 error "Found more than one interface matching \"$expect\" ping count"
1846                                 return 1
1847                         else
1848                                 echo "Expect ping count \"$expect\" found \"$count\""
1849                                 found=true;
1850                         fi
1851                 elif [[ $count -ne 0 ]]; then
1852                         error "Found interface with ping count \"$count\" but expect \"$expect\""
1853                         return 1
1854                 fi
1855         done
1856
1857         return 0
1858 }
1859
1860 test_210() {
1861         reinit_dlc || return $?
1862         add_net "tcp" "${INTERFACES[0]}" || return $?
1863         add_net "tcp1" "${INTERFACES[0]}" || return $?
1864
1865         local prim_nid=$($LCTL list_nids | head -n 1)
1866
1867         do_lnetctl discover $prim_nid ||
1868                 error "failed to discover myself"
1869
1870         local default=$($LNETCTL global show |
1871                         awk '/recovery_limit/{print $NF}')
1872         # Set recovery limit to 10 seconds.
1873         do_lnetctl set recovery_limit 10 ||
1874                 error "failed to set recovery_limit"
1875
1876         $LCTL set_param debug=+net
1877         # Use local_error so LNet doesn't attempt to resend the discovery ping
1878         $LCTL net_drop_add -s *@tcp -d *@tcp -m GET -r 1 -e local_error
1879         $LCTL net_drop_add -s *@tcp1 -d *@tcp1 -m GET -r 1 -e local_error
1880         do_lnetctl discover $prim_nid &&
1881                 error "Expected discovery to fail"
1882
1883         # See comment for check_ping_count()
1884         sleep 5
1885         check_nid_in_recovq "-l" "1"
1886         check_ping_count "ni" "2"
1887
1888         sleep 5
1889
1890         check_nid_in_recovq "-l" "1"
1891         check_ping_count "ni" "3"
1892
1893         $LCTL net_drop_del -a
1894
1895         reinit_dlc || return $?
1896         add_net "tcp" "${INTERFACES[0]}" || return $?
1897         add_net "tcp1" "${INTERFACES[0]}" || return $?
1898
1899         local prim_nid=$($LCTL list_nids | head -n 1)
1900
1901         do_lnetctl discover $prim_nid ||
1902                 error "failed to discover myself"
1903
1904         do_lnetctl set recovery_limit $default ||
1905                 error "failed to set recovery_limit"
1906
1907         default=$($LNETCTL global show |
1908                   awk '/max_recovery_ping_interval/{print $NF}')
1909         do_lnetctl set max_recovery_ping_interval 4 ||
1910                 error "failed to set max_recovery_ping_interval"
1911
1912         $LCTL set_param debug=+net
1913         # Use local_error so LNet doesn't attempt to resend the discovery ping
1914         $LCTL net_drop_add -s *@tcp -d *@tcp -m GET -r 1 -e local_error
1915         $LCTL net_drop_add -s *@tcp1 -d *@tcp1 -m GET -r 1 -e local_error
1916         do_lnetctl discover $prim_nid &&
1917                 error "Expected discovery to fail"
1918
1919         # See comment for check_ping_count()
1920         sleep 4
1921         check_nid_in_recovq "-l" "1"
1922         check_ping_count "ni" "2"
1923
1924         sleep 9
1925         check_nid_in_recovq "-l" "1"
1926         check_ping_count "ni" "4"
1927
1928         $LCTL net_drop_del -a
1929
1930         do_lnetctl set max_recovery_ping_interval $default ||
1931                 error "failed to set max_recovery_ping_interval"
1932
1933         return 0
1934 }
1935 run_test 210 "Local NI recovery checks"
1936
1937 test_211() {
1938         reinit_dlc || return $?
1939         add_net "tcp" "${INTERFACES[0]}" || return $?
1940         add_net "tcp1" "${INTERFACES[0]}" || return $?
1941
1942         local prim_nid=$($LCTL list_nids | head -n 1)
1943
1944         do_lnetctl discover $prim_nid ||
1945                 error "failed to discover myself"
1946
1947         local default=$($LNETCTL global show |
1948                         awk '/recovery_limit/{print $NF}')
1949         # Set recovery limit to 10 seconds.
1950         do_lnetctl set recovery_limit 10 ||
1951                 error "failed to set recovery_limit"
1952
1953         $LCTL net_drop_add -s *@tcp -d *@tcp -m GET -r 1 -e remote_error
1954         $LCTL net_drop_add -s *@tcp1 -d *@tcp1 -m GET -r 1 -e remote_error
1955
1956         # Set health to 0 on one interface. This forces it onto the recovery
1957         # queue.
1958         $LNETCTL peer set --nid $prim_nid --health 0
1959
1960         # After 5 seconds, we expect the peer NI to still be in recovery
1961         sleep 5
1962         check_nid_in_recovq "-p" 1
1963         check_ping_count "peer_ni" "2"
1964
1965         # After 15 seconds, the peer NI should have been fully processed out of
1966         # the recovery queue. We'll allow a total of 17 seconds to account for
1967         # differences in sleeping for whole seconds vs. the more accurate time
1968         # keeping that is done in the recovery code.
1969         sleep 12
1970         check_nid_in_recovq "-p" 0
1971         check_ping_count "peer_ni" "4"
1972
1973         $LCTL net_drop_del -a
1974
1975         # Set health to force it back onto the recovery queue. Set to 500 means
1976         # in 5 seconds it should be back at maximum value. We'll wait a couple
1977         # more seconds than that to be safe.
1978         # NB: we reset the recovery limit to 0 (indefinite) so the peer NI is
1979         # eligible again
1980         do_lnetctl set recovery_limit 0 ||
1981                 error "failed to set recovery_limit"
1982
1983         $LNETCTL peer set --nid $prim_nid --health 500
1984
1985         check_nid_in_recovq "-p" 1
1986         check_ping_count "peer_ni" "2"
1987
1988         sleep 7
1989
1990         check_nid_in_recovq "-p" 0
1991         check_ping_count "peer_ni" "0"
1992
1993         reinit_dlc || return $?
1994         add_net "tcp" "${INTERFACES[0]}" || return $?
1995         add_net "tcp1" "${INTERFACES[0]}" || return $?
1996
1997         local prim_nid=$($LCTL list_nids | head -n 1)
1998
1999         do_lnetctl discover $prim_nid ||
2000                 error "failed to discover myself"
2001
2002         do_lnetctl set recovery_limit $default ||
2003                 error "failed to set recovery_limit"
2004
2005         default=$($LNETCTL global show |
2006                   awk '/max_recovery_ping_interval/{print $NF}')
2007         do_lnetctl set max_recovery_ping_interval 4 ||
2008                 error "failed to set max_recovery_ping_interval"
2009
2010         $LCTL net_drop_add -s *@tcp -d *@tcp -m GET -r 1 -e remote_error
2011         $LCTL net_drop_add -s *@tcp1 -d *@tcp1 -m GET -r 1 -e remote_error
2012
2013         # Set health to 0 on one interface. This forces it onto the recovery
2014         # queue.
2015         $LNETCTL peer set --nid $prim_nid --health 0
2016
2017         # See comment for check_ping_count()
2018         sleep 4
2019         check_nid_in_recovq "-p" "1"
2020         check_ping_count "peer_ni" "2"
2021
2022         sleep 9
2023         check_nid_in_recovq "-p" "1"
2024         check_ping_count "peer_ni" "4"
2025
2026         $LCTL net_drop_del -a
2027
2028         do_lnetctl set max_recovery_ping_interval $default ||
2029                 error "failed to set max_recovery_ping_interval"
2030
2031         return 0
2032 }
2033 run_test 211 "Remote NI recovery checks"
2034
2035 test_212() {
2036         local rnodes=$(remote_nodes_list)
2037         [[ -z $rnodes ]] && skip "Need at least 1 remote node"
2038
2039         cleanup_lnet || error "Failed to cleanup before test execution"
2040
2041         # Loading modules should configure LNet with the appropriate
2042         # test-framework configuration
2043         load_lnet "config_on_load=1" || error "Failed to load modules"
2044
2045         local my_nid=$($LCTL list_nids | head -n 1)
2046         [[ -z $my_nid ]] &&
2047                 error "Failed to get primary NID for local host $HOSTNAME"
2048
2049         local rnode=$(awk '{print $1}' <<<$rnodes)
2050         local rnodenids=$(do_node $rnode $LCTL list_nids | xargs echo)
2051         local rloaded=false
2052
2053         if [[ -z $rnodenids ]]; then
2054                 do_rpc_nodes $rnode load_lnet "config_on_load=1"
2055                 rloaded=true
2056                 rnodenids=$(do_node $rnode $LCTL list_nids | xargs echo)
2057         fi
2058
2059         local rnodepnid=$(awk '{print $1}' <<< $rnodenids)
2060
2061         [[ -z $rnodepnid ]] &&
2062                 error "Failed to get primary NID for remote host $rnode"
2063
2064         log "Initial discovery"
2065         do_lnetctl discover --force $rnodepnid ||
2066                 error "Failed to discover $rnodepnid"
2067
2068         do_node $rnode "$LNETCTL discover --force $my_nid" ||
2069                 error "$rnode failed to discover $my_nid"
2070
2071         log "Fail local discover ping to set LNET_PEER_REDISCOVER flag"
2072         $LCTL net_drop_add -s "*@$NETTYPE" -d "*@$NETTYPE" -r 1 -e local_error
2073         do_lnetctl discover --force $rnodepnid &&
2074                 error "Discovery should have failed"
2075         $LCTL net_drop_del -a
2076
2077         local nid
2078         for nid in $rnodenids; do
2079                 # We need GET (PING) delay just long enough so we can trigger
2080                 # discovery on the remote peer
2081                 $LCTL net_delay_add -s "*@$NETTYPE" -d $nid -r 1 -m GET -l 3
2082                 $LCTL net_drop_add -s "*@$NETTYPE" -d $nid -r 1 -m GET -e local_error
2083                 # We need PUT (PUSH) delay just long enough so we can process
2084                 # the PING failure
2085                 $LCTL net_delay_add -s "*@$NETTYPE" -d $nid -r 1 -m PUT -l 6
2086         done
2087
2088         log "Force $HOSTNAME to discover $rnodepnid (in background)"
2089         # We want to get a PING sent that we know will eventually fail.
2090         # The delay rules we added will ensure the ping is not sent until
2091         # the PUSH is also in flight (see below), and the drop rule ensures that
2092         # when the PING is eventually sent it will error out
2093         do_lnetctl discover --force $rnodepnid &
2094         local pid1=$!
2095
2096         # We want a discovery PUSH from rnode to put rnode back on our
2097         # discovery queue. This should cause us to try and send a PUSH to rnode
2098         # while the PING is still outstanding.
2099         log "Force $rnode to discover $my_nid"
2100         do_node $rnode $LNETCTL discover --force $my_nid
2101
2102         # At this point we'll have both PING_SENT and PUSH_SENT set for the
2103         # rnode peer. Wait for the PING to error out which should terminate the
2104         # discovery process that we backgrounded.
2105         log "Wait for $pid1"
2106         wait $pid1
2107         log "Finished wait on $pid1"
2108
2109         # The PING send failure clears the PING_SENT flag and puts the peer back
2110         # on the discovery queue. When discovery thread processes the peer it
2111         # will mistakenly clear the PUSH_SENT flag (and set PUSH_FAILED).
2112         # Discovery will then complete for this peer even though we have an
2113         # outstanding PUSH.
2114         # When PUSH is actually unlinked it will be forced back onto the
2115         # discovery queue, but we no longer have a ref on the peer. When
2116         # discovery completes again, we'll trip the ASSERT in
2117         # lnet_destroy_peer_locked()
2118
2119         # Delete the delay rules to send the PUSH
2120         $LCTL net_delay_del -a
2121         # Delete the drop rules
2122         $LCTL net_drop_del -a
2123
2124         unload_modules ||
2125                 error "Failed to unload modules"
2126         if $rloaded; then
2127                 do_rpc_nodes $rnode unload_modules_local ||
2128                         error "Failed to unload modules on $rnode"
2129         fi
2130
2131         return 0
2132 }
2133 run_test 212 "Check discovery refcount loss bug (LU-14627)"
2134
2135 test_213() {
2136         cleanup_netns || error "Failed to cleanup netns before test execution"
2137         cleanup_lnet || error "Failed to unload modules before test execution"
2138
2139         setup_fakeif || error "Failed to add fake IF"
2140         have_interface "$FAKE_IF" ||
2141                 error "Expect $FAKE_IF configured but not found"
2142
2143         reinit_dlc || return $?
2144
2145         add_net "tcp" "${INTERFACES[0]}" || return $?
2146         add_net "tcp" "$FAKE_IF" || return $?
2147
2148         local nid1=$(lctl list_nids | head -n 1)
2149         local nid2=$(lctl list_nids | tail --lines 1)
2150
2151         [[ $(lctl which_nid $nid1 $nid2) == $nid1 ]] ||
2152                 error "Expect nid1 \"$nid1\" to be preferred"
2153
2154         [[ $(lctl which_nid $nid2 $nid1) == $nid2 ]] ||
2155                 error "Expect nid2 \"$nid2\" to be preferred"
2156
2157         return 0
2158 }
2159 run_test 213 "Check LNetDist calculation for multiple local NIDs"
2160
2161 function check_ni_status() {
2162         local nid="$1"
2163         local expect="$2"
2164
2165         local status=$($LNETCTL net show |
2166                        grep -A 1 ${nid} |
2167                        awk '/status/{print $NF}')
2168
2169         echo "NI ${nid} expect status \"${expect}\" found \"${status}\""
2170         if [[ $status != $expect ]]; then
2171                 error "Error: Expect NI status \"$expect\" for NID \"$nid\" but found \"$status\""
2172         fi
2173
2174         return 0
2175 }
2176
2177 test_214() {
2178         cleanup_netns || error "Failed to cleanup netns before test execution"
2179         cleanup_lnet || error "Failed to unload modules before test execution"
2180
2181         setup_fakeif || error "Failed to add fake IF"
2182         have_interface "$FAKE_IF" ||
2183                 error "Expect $FAKE_IF configured but not found"
2184
2185         reinit_dlc || return $?
2186
2187         add_net "tcp" "${INTERFACES[0]}" || return $?
2188         add_net "tcp" "$FAKE_IF" || return $?
2189
2190         local nid1=$(lctl list_nids | head -n 1)
2191         local nid2=$(lctl list_nids | tail --lines 1)
2192
2193         check_ni_status "0@lo" up
2194         check_ni_status "$nid1" up
2195         check_ni_status "$nid2" up
2196
2197         echo "Set $FAKE_IF down"
2198         echo "ip link set dev $FAKE_IF down"
2199         ip link set dev $FAKE_IF down
2200         check_ni_status "0@lo" up
2201         check_ni_status "$nid1" up
2202         check_ni_status "$nid2" down
2203 }
2204 run_test 214 "Check local NI status when link is downed"
2205
2206 get_ni_stat() {
2207         local nid=$1
2208         local stat=$2
2209
2210         $LNETCTL net show -v 2 |
2211                 egrep -e nid -e $stat |
2212                 grep -wA 1 $nid |
2213                 awk '/'$stat':/{print $NF}'
2214 }
2215
2216 ni_stats_pre() {
2217         local nidvar s
2218         for nidvar in nid1 nid2; do
2219                 for stat in send_count recv_count; do
2220                         s=$(get_ni_stat ${!nidvar} $stat)
2221                         eval ${nidvar}_pre_${stat}=$s
2222                 done
2223         done
2224 }
2225
2226 ni_stats_post() {
2227         local nidvar s
2228         for nidvar in nid1 nid2; do
2229                 for stat in send_count recv_count; do
2230                         s=$(get_ni_stat ${!nidvar} $stat)
2231                         eval ${nidvar}_post_${stat}=$s
2232                 done
2233         done
2234 }
2235
2236 ni_stat_changed() {
2237         local nidvar=$1
2238         local stat=$2
2239
2240         local pre post
2241         eval pre=\${${nidvar}_pre_${stat}}
2242         eval post=\${${nidvar}_post_${stat}}
2243
2244         echo "${!nidvar} pre ${stat} $pre post ${stat} $post"
2245
2246         [[ $pre -ne $post ]]
2247 }
2248
2249 test_215() {
2250         cleanup_netns || error "Failed to cleanup netns before test execution"
2251         cleanup_lnet || error "Failed to unload modules before test execution"
2252
2253         reinit_dlc || return $?
2254
2255         add_net "tcp1" "${INTERFACES[0]}" || return $?
2256         add_net "tcp2" "${INTERFACES[0]}" || return $?
2257
2258         local nid1=$($LCTL list_nids | head -n 1)
2259         local nid2=$($LCTL list_nids | tail --lines 1)
2260
2261         do_lnetctl peer add --prim $nid1 --nid $nid2 ||
2262                 error "Failed to add peer"
2263
2264         local npings=25
2265
2266         for nidvarA in nid1 nid2; do
2267                 src=${!nidvarA}
2268                 dst=${!nidvarA}
2269                 for nidvarB in nid1 nid2; do
2270                         [[ $nidvarA == $nidvarB ]] && continue
2271
2272                         ni_stats_pre
2273
2274                         echo "$LNETCTL ping $dst x $npings"
2275                         for i in $(seq 1 $npings); do
2276                                 $LNETCTL ping $dst &>/dev/null ||
2277                                         error "$LNETCTL ping $dst failed"
2278                         done
2279
2280                         ni_stats_post
2281
2282                         # No source specified, sends to either NID should cause
2283                         # counts to increase across both NIs
2284                         for nidvar in nid1 nid2; do
2285                                 for stat in send_count recv_count; do
2286                                         ni_stat_changed $nidvar $stat ||
2287                                                 error "$stat unchanged for ${!nidvar}"
2288                                 done
2289                         done
2290
2291                         ni_stats_pre
2292
2293                         echo "$LNETCTL ping --source $src $dst x $npings"
2294                         for i in $(seq 1 $npings); do
2295                                 $LNETCTL ping --source $src $dst &>/dev/null ||
2296                                         error "$LNETCTL ping --source $src $dst failed"
2297                         done
2298
2299                         ni_stats_post
2300
2301                         # src nid == dest nid means stats for the _other_ NI
2302                         # should be unchanged
2303                         for nidvar in nid1 nid2; do
2304                                 for stat in send_count recv_count; do
2305                                         if [[ ${!nidvar} == $src ]]; then
2306                                                 ni_stat_changed $nidvar $stat ||
2307                                                         error "$stat unchanged for ${!nidvar}"
2308                                         else
2309                                                 ni_stat_changed $nidvar $stat &&
2310                                                         error "$stat changed for ${!nidvar}"
2311                                         fi
2312                                 done
2313                         done
2314                 done
2315                 # Double number of pings for next iteration because the net
2316                 # sequence numbers will have diverged
2317                 npings=$(($npings * 2))
2318         done
2319
2320         # Ping from nid1 to nid2 should fail
2321         do_lnetctl ping --source $nid1 $nid2 &&
2322                 error "ping from $nid1 to $nid2 should fail"
2323
2324         # Ping from nid2 to nid1 should fail
2325         do_lnetctl ping --source $nid2 $nid1 &&
2326                 error "ping from $nid2 to $nid1 should fail"
2327
2328         return 0
2329 }
2330 run_test 215 "Test lnetctl ping --source option"
2331
2332 test_216() {
2333         local rc=0
2334
2335         reinit_dlc || return $?
2336
2337         add_net "tcp" "${INTERFACES[0]}" || return $?
2338         add_net "tcp1" "${INTERFACES[0]}" || return $?
2339
2340         local nids=( $($LCTL list_nids | xargs echo) )
2341
2342         do_lnetctl discover ${nids[0]} ||
2343                 error "Initial discovery failed"
2344
2345         do_lnetctl ping --source ${nids[0]} ${nids[0]} ||
2346                 error "Initial ping failed $?"
2347
2348         do_lnetctl ping --source ${nids[1]} ${nids[1]} ||
2349                 error "Initial ping failed $?"
2350
2351         local src dst
2352         for src in "${nids[@]}"; do
2353                 for dst in "${nids[@]}"; do
2354                         $LCTL net_drop_add -r 1 -s $src -d $dst -e network_timeout
2355                 done
2356         done
2357
2358         do_lnetctl ping ${nids[0]} || rc=$?
2359
2360         $LCTL net_drop_del -a
2361
2362         [[ $rc -eq 0 ]] &&
2363                 error "expected ping to fail"
2364
2365         check_nid_in_recovq "-p" 0
2366         check_nid_in_recovq "-l" 1
2367
2368         return 0
2369 }
2370 run_test 216 "Failed send to peer NI owned by local host should not trigger peer NI recovery"
2371
2372 test_217() {
2373         reinit_dlc || return $?
2374
2375         [[ $($LNETCTL net show | grep -c nid) -ne 1 ]] &&
2376                 error "Unexpected number of NIs after initalizing DLC"
2377
2378         do_lnetctl discover 0@lo ||
2379                 error "Failed to discover 0@lo"
2380
2381         unload_modules
2382 }
2383 run_test 217 "Don't leak memory when discovering peer with nnis <= 1"
2384
2385 test_218() {
2386         reinit_dlc || return $?
2387
2388         [[ ${#INTERFACES[@]} -lt 2 ]] &&
2389                 skip "Need two LNet interfaces"
2390
2391         add_net "tcp" "${INTERFACES[0]}" || return $?
2392
2393         local nid1=$($LCTL list_nids | head -n 1)
2394
2395         do_lnetctl ping $nid1 ||
2396                 error "ping failed"
2397
2398         add_net "tcp" "${INTERFACES[1]}" || return $?
2399
2400         local nid2=$($LCTL list_nids | tail --lines 1)
2401
2402         do_lnetctl ping $nid2 ||
2403                 error "ping failed"
2404
2405         $LCTL net_drop_add -s $nid1 -d $nid1 -e local_error -r 1
2406
2407         do_lnetctl ping $nid1 &&
2408                 error "ping should have failed"
2409
2410         local health_recovered
2411         local i
2412
2413         for i in $(seq 1 5); do
2414                 health_recovered=$($LNETCTL net show -v 2 |
2415                                    grep -c 'health value: 1000')
2416
2417                 if [[ $health_recovered -ne 2 ]]; then
2418                         echo "Wait 1 second for health to recover"
2419                         sleep 1
2420                 else
2421                         break
2422                 fi
2423         done
2424
2425         health_recovered=$($LNETCTL net show -v 2 |
2426                            grep -c 'health value: 1000')
2427
2428         $LCTL net_drop_del -a
2429
2430         [[ $health_recovered -ne 2 ]] &&
2431                 do_lnetctl net show -v 2 | egrep -e nid -e health &&
2432                 error "Health hasn't recovered"
2433
2434         return 0
2435 }
2436 run_test 218 "Local recovery pings should exercise all available paths"
2437
2438 test_219() {
2439         reinit_dlc || return $?
2440         add_net "tcp" "${INTERFACES[0]}" || return $?
2441         add_net "tcp1" "${INTERFACES[0]}" || return $?
2442
2443         local nid1=$(lctl list_nids | head -n 1)
2444         local nid2=$(lctl list_nids | tail --lines 1)
2445
2446         do_lnetctl ping $nid1 ||
2447                 error "Ping failed $?"
2448         do_lnetctl ping $nid2 ||
2449                 error "Ping failed $?"
2450
2451         do_lnetctl discover $nid2 ||
2452                 error "Discovery failed"
2453
2454         $LNETCTL peer show --nid $nid1 | grep -q $nid2 ||
2455                 error "$nid2 is not listed under $nid1"
2456 }
2457 run_test 219 "Consolidate peer entries"
2458
2459 do_net_add() {
2460         local node=$1
2461         local net=$2
2462         local if=$3
2463         local opts=$4
2464
2465         do_rpc_nodes $node "$LNETCTL net add --net $net --if $if $opts" ||
2466                 error "add $net on interface $if on node $node failed rc=$?"
2467 }
2468
2469 do_route_add() {
2470         local node=$1
2471         local net=$2
2472         local gw=$3
2473
2474         do_node $node "$LNETCTL route add --net $net --gateway $gw" ||
2475                 error "route add to $net via $gw failed rc=$?"
2476 }
2477
2478 ROUTER=""
2479 ROUTER_INTERFACES=()
2480 RPEER=""
2481 RPEER_INTERFACES=()
2482 init_router_test_vars() {
2483         local rnodes=$(remote_nodes_list)
2484         [[ -z $rnodes || $(wc -w <<<$rnodes) -lt 2 ]] &&
2485                 skip "Need at least 2 remote nodes found \"$rnodes\""
2486
2487         ROUTER=$(awk '{print $1}' <<<$rnodes)
2488         RPEER=$(awk '{print $2}' <<<$rnodes)
2489
2490         rnodes=$(comma_list $ROUTER $RPEER)
2491         local all_nodes=$(comma_list $rnodes $HOSTNAME)
2492
2493         do_nodes $rnodes $LUSTRE_RMMOD ||
2494                 error "failed to unload modules"
2495
2496         do_rpc_nodes $rnodes "load_lnet config_on_load=1" ||
2497                 error "Failed to load and configure LNet"
2498
2499         ROUTER_INTERFACES=( $(do_rpc_nodes --quiet $ROUTER lnet_if_list) )
2500
2501         RPEER_INTERFACES=( $(do_rpc_nodes --quiet $RPEER lnet_if_list) )
2502
2503         do_nodes $all_nodes $LUSTRE_RMMOD ||
2504                 error "Failed to unload modules"
2505
2506         [[ ${#INTERFACES[@]} -eq 0 ]] &&
2507                 error "No interfaces configured for local host $HOSTNAME"
2508         [[ ${#ROUTER_INTERFACES[@]} -eq 0 ]] &&
2509                 error "No interfaces configured for router $ROUTER"
2510         [[ ${#RPEER_INTERFACES[@]} -eq 0 ]] &&
2511                 error "No interfaces configured for remote peer $RPEER"
2512
2513         return 0
2514 }
2515
2516 ROUTER_NIDS=()
2517 RPEER_NIDS=()
2518 LNIDS=()
2519 LOCAL_NET=${NETTYPE}1
2520 REMOTE_NET=${NETTYPE}2
2521 setup_router_test() {
2522         local mod_opts="$1"
2523         local rtr_net_opts="$2"
2524
2525         (( $MDS1_VERSION >= $(version_code 2.15.0) )) ||
2526                 skip "need at least 2.15.0 for load_lnet"
2527
2528         if [[ ${#RPEER_INTERFACES[@]} -eq 0 ]]; then
2529                 init_router_test_vars ||
2530                         return $?
2531         fi
2532
2533         local all_nodes=$(comma_list $ROUTER $RPEER $HOSTNAME)
2534
2535         do_nodes $all_nodes $LUSTRE_RMMOD ||
2536                 error "failed to unload modules"
2537
2538         mod_opts+=" alive_router_check_interval=5"
2539         mod_opts+=" router_ping_timeout=5"
2540         mod_opts+=" large_router_buffers=4"
2541         mod_opts+=" small_router_buffers=8"
2542         mod_opts+=" tiny_router_buffers=16"
2543         do_rpc_nodes $all_nodes load_lnet "${mod_opts}" ||
2544                 error "Failed to load lnet"
2545
2546         do_nodes $all_nodes "$LNETCTL lnet configure" ||
2547                 error "Failed to initialize DLC"
2548
2549         do_net_add $ROUTER $LOCAL_NET ${ROUTER_INTERFACES[0]} $rtr_net_opts ||
2550                 return $?
2551
2552         do_net_add $ROUTER $REMOTE_NET ${ROUTER_INTERFACES[0]} ||
2553                 return $?
2554
2555         do_net_add $RPEER $REMOTE_NET ${RPEER_INTERFACES[0]} ||
2556                 return $?
2557
2558         add_net $LOCAL_NET ${INTERFACES[0]} ||
2559                 return $?
2560
2561         ROUTER_NIDS=( $(do_node $ROUTER $LCTL list_nids 2>/dev/null |
2562                         xargs echo) )
2563         RPEER_NIDS=( $(do_node $RPEER $LCTL list_nids 2>/dev/null |
2564                        xargs echo) )
2565         LNIDS=( $($LCTL list_nids 2>/dev/null | xargs echo) )
2566 }
2567
2568 do_route_del() {
2569         local node=$1
2570         local net=$2
2571         local gw=$3
2572
2573         do_nodesv $node "if $LNETCTL route show --net $net --gateway $gw; then \
2574                                 $LNETCTL route del --net $net --gateway $gw;   \
2575                          else                                                  \
2576                                 exit 0;                                        \
2577                          fi"
2578 }
2579
2580 cleanup_router_test() {
2581         local all_nodes=$(comma_list $HOSTNAME $ROUTER $RPEER)
2582
2583         do_route_del $HOSTNAME $REMOTE_NET ${ROUTER_NIDS[0]} ||
2584                 error "Failed to delete $REMOTE_NET route"
2585
2586         do_route_del $RPEER $LOCAL_NET ${ROUTER_NIDS[1]} ||
2587                 error "Failed to delete $LOCAL_NET route"
2588
2589         do_nodes $all_nodes $LUSTRE_RMMOD ||
2590                 error "failed to unload modules"
2591
2592         return 0
2593 }
2594
2595 check_route_aliveness() {
2596         local node="$1"
2597         local expected="$2"
2598
2599         local lctl_actual
2600         local lnetctl_actual
2601         local chk_intvl
2602         local i
2603
2604         chk_intvl=$(cat /sys/module/lnet/parameters/alive_router_check_interval)
2605
2606         lctl_actual=$(do_node $node $LCTL show_route | awk '{print $7}')
2607         lnetctl_actual=$(do_node $node $LNETCTL route show -v |
2608                          awk '/state/{print $NF}')
2609
2610         for ((i = 0; i < $chk_intvl; i++)); do
2611                 if [[ $lctl_actual == $expected ]] &&
2612                    [[ $lnetctl_actual == $expected ]]; then
2613                         break
2614                 fi
2615
2616                 echo "wait 1s for route state change"
2617                 sleep 1
2618
2619                 lctl_actual=$(do_node $node $LCTL show_route | awk '{print $7}')
2620                 lnetctl_actual=$(do_node $node $LNETCTL route show -v |
2621                                  awk '/state/{print $NF}')
2622         done
2623
2624         [[ $lctl_actual != $expected ]] &&
2625                 error "Wanted \"$expected\" lctl found \"$lctl_actual\""
2626
2627         [[ $lnetctl_actual != $expected ]] &&
2628                 error "Wanted \"$expected\" lnetctl found \"$lnetctl_actual\""
2629
2630         return 0
2631 }
2632
2633 check_router_ni_status() {
2634         local expected_local="$1"
2635         local expected_remote="$2"
2636
2637         local actual_local
2638         local actual_remote
2639         local chk_intvl
2640         local timeout
2641         local i
2642
2643         chk_intvl=$(cat /sys/module/lnet/parameters/alive_router_check_interval)
2644         timeout=$(cat /sys/module/lnet/parameters/router_ping_timeout)
2645
2646         actual_local=$(do_node $ROUTER "$LNETCTL net show --net $LOCAL_NET" |
2647                        awk '/status/{print $NF}')
2648         actual_remote=$(do_node $ROUTER "$LNETCTL net show --net $REMOTE_NET" |
2649                         awk '/status/{print $NF}')
2650
2651         for ((i = 0; i < $((chk_intvl + timeout)); i++)); do
2652                 if [[ $actual_local == $expected_local ]] &&
2653                    [[ $actual_remote == $expected_remote ]]; then
2654                         break
2655                 fi
2656
2657                 echo "wait 1s for NI state change"
2658                 sleep 1
2659
2660                 actual_local=$(do_node $ROUTER \
2661                                "$LNETCTL net show --net $LOCAL_NET" |
2662                                 awk '/status/{print $NF}')
2663                 actual_remote=$(do_node $ROUTER \
2664                                 "$LNETCTL net show --net $REMOTE_NET" |
2665                                 awk '/status/{print $NF}')
2666         done
2667
2668         [[ $actual_local == $expected_local ]] ||
2669                 error "$LOCAL_NET should be $expected_local"
2670
2671         [[ $actual_remote == $expected_remote ]] ||
2672                 error "$REMOTE_NET should be $expected_remote"
2673
2674         return 0
2675 }
2676
2677 do_basic_rtr_test() {
2678         do_node $ROUTER "$LNETCTL set routing 1" ||
2679                 error "Unable to enable routing on $ROUTER"
2680
2681         do_route_add $HOSTNAME $REMOTE_NET ${ROUTER_NIDS[0]} ||
2682                 return $?
2683
2684         do_route_add $RPEER $LOCAL_NET ${ROUTER_NIDS[1]} ||
2685                 return $?
2686
2687         check_route_aliveness "$HOSTNAME" "up" ||
2688                 return $?
2689
2690         check_route_aliveness "$RPEER" "up" ||
2691                 return $?
2692
2693         do_lnetctl ping ${RPEER_NIDS[0]} ||
2694                 error "Failed to ping ${RPEER_NIDS[0]}"
2695
2696         do_node $RPEER "$LNETCTL ping ${LNIDS[0]}" ||
2697                 error "$RPEER failed to ping ${LNIDS[0]}"
2698
2699         return 0
2700 }
2701
2702 test_220() {
2703         setup_router_test || return $?
2704
2705         do_basic_rtr_test || return $?
2706
2707         cleanup_router_test || return $?
2708 }
2709 run_test 220 "Add routes w/default options - check aliveness"
2710
2711 test_221() {
2712         setup_router_test lnet_peer_discovery_disabled=1 || return $?
2713
2714         do_basic_rtr_test || return $?
2715
2716         cleanup_router_test || return $?
2717 }
2718 run_test 221 "Add routes w/DD disabled - check aliveness"
2719
2720 do_aarf_enabled_test() {
2721         do_node $ROUTER "$LNETCTL set routing 1" ||
2722                 error "Unable to enable routing on $ROUTER"
2723
2724         check_router_ni_status "down" "down"
2725
2726         do_lnetctl ping ${RPEER_NIDS[0]} &&
2727                 error "Ping should fail"
2728
2729         do_node $RPEER "$LNETCTL ping ${LNIDS[0]}" &&
2730                 error "$RPEER ping should fail"
2731
2732         # Adding a route should cause the router's NI on LOCAL_NET to get up
2733         do_route_add $HOSTNAME $REMOTE_NET ${ROUTER_NIDS[0]} ||
2734                 return $?
2735
2736         check_router_ni_status "up" "down" ||
2737                 return $?
2738
2739         # But route should still be down because of avoid_asym_router_failure
2740         check_route_aliveness "$HOSTNAME" "down" ||
2741                 return $?
2742
2743         do_lnetctl ping ${RPEER_NIDS[0]} &&
2744                 error "Ping should fail"
2745
2746         do_node $RPEER "$LNETCTL ping ${LNIDS[0]}" &&
2747                 error "$RPEER ping should fail"
2748
2749         # Adding the symmetric route should cause the remote NI to go up and
2750         # routes to go up
2751         do_route_add $RPEER $LOCAL_NET ${ROUTER_NIDS[1]} ||
2752                 return $?
2753
2754         check_router_ni_status "up" "up" ||
2755                 return $?
2756
2757         check_route_aliveness "$HOSTNAME" "up" ||
2758                 return $?
2759
2760         check_route_aliveness "$RPEER" "up" ||
2761                 return $?
2762
2763         do_lnetctl ping ${RPEER_NIDS[0]} ||
2764                 error "Failed to ping ${RPEER_NIDS[0]}"
2765
2766         do_node $RPEER "$LNETCTL ping ${LNIDS[0]}" ||
2767                 error "$RPEER failed to ping ${LNIDS[0]}"
2768
2769         # Stop LNet on local host
2770         do_lnetctl lnet unconfigure ||
2771                 error "Failed to stop LNet rc=$?"
2772
2773         check_router_ni_status "down" "up" ||
2774                 return $?
2775
2776         check_route_aliveness "$RPEER" "down" ||
2777                 return $?
2778
2779         do_lnetctl ping ${RPEER_NIDS[0]} &&
2780                 error "Ping should fail"
2781
2782         do_node $RPEER "$LNETCTL ping ${LNIDS[0]}" &&
2783                 error "$RPEER ping should fail"
2784
2785         return 0
2786 }
2787
2788 test_222() {
2789         setup_router_test avoid_asym_router_failure=1 || return $?
2790
2791         do_aarf_enabled_test || return $?
2792
2793         cleanup_router_test || return $?
2794 }
2795 run_test 222 "Check avoid_asym_router_failure=1"
2796
2797 test_223() {
2798         local opts="avoid_asym_router_failure=1 lnet_peer_discovery_disabled=1"
2799
2800         setup_router_test "$opts" || return $?
2801
2802         do_aarf_enabled_test || return $?
2803
2804         cleanup_router_test || return $?
2805 }
2806 run_test 223 "Check avoid_asym_router_failure=1 w/DD disabled"
2807
2808 do_aarf_disabled_test() {
2809         do_node $ROUTER "$LNETCTL set routing 1" ||
2810                 error "Unable to enable routing on $ROUTER"
2811
2812         check_router_ni_status "down" "down"
2813
2814         do_route_add $HOSTNAME $REMOTE_NET ${ROUTER_NIDS[0]} ||
2815                 return $?
2816
2817         check_router_ni_status "up" "down" ||
2818                 return $?
2819
2820         check_route_aliveness "$HOSTNAME" "up" ||
2821                 return $?
2822
2823         do_route_add $RPEER $LOCAL_NET ${ROUTER_NIDS[1]} ||
2824                 return $?
2825
2826         check_router_ni_status "up" "up" ||
2827                 return $?
2828
2829         check_route_aliveness "$HOSTNAME" "up" ||
2830                 return $?
2831
2832         check_route_aliveness "$RPEER" "up" ||
2833                 return $?
2834
2835         do_lnetctl ping ${RPEER_NIDS[0]} ||
2836                 error "Failed to ping ${RPEER_NIDS[0]}"
2837
2838         do_node $RPEER "$LNETCTL ping ${LNIDS[0]}" ||
2839                 error "$RPEER failed to ping ${LNIDS[0]}"
2840
2841         # Stop LNet on local host
2842         do_lnetctl lnet unconfigure ||
2843                 error "Failed to stop LNet rc=$?"
2844
2845         check_router_ni_status "down" "up" ||
2846                 return $?
2847
2848         check_route_aliveness "$RPEER" "up" ||
2849                 return $?
2850
2851         return 0
2852 }
2853
2854 test_224() {
2855         setup_router_test avoid_asym_router_failure=0 ||
2856                 return $?
2857
2858         do_aarf_disabled_test ||
2859                 return $?
2860
2861         cleanup_router_test ||
2862                 return $?
2863 }
2864 run_test 224 "Check avoid_asym_router_failure=0"
2865
2866 test_225() {
2867         local opts="avoid_asym_router_failure=0 lnet_peer_discovery_disabled=1"
2868
2869         setup_router_test "$opts" || return $?
2870
2871         do_aarf_disabled_test || return $?
2872
2873         cleanup_router_test ||
2874                 return $?
2875 }
2876 run_test 225 "Check avoid_asym_router_failure=0 w/DD disabled"
2877
2878 do_rtr_peer_health_test() {
2879         local expected="$1"
2880
2881         do_node $ROUTER "$LNETCTL set routing 1" ||
2882                 error "Unable to enable routing on $ROUTER"
2883
2884         do_route_add $HOSTNAME $REMOTE_NET ${ROUTER_NIDS[0]} ||
2885                 return $?
2886
2887         do_route_add $RPEER $LOCAL_NET ${ROUTER_NIDS[1]} ||
2888                 return $?
2889
2890         check_router_ni_status "up" "up" ||
2891                 return $?
2892
2893         check_route_aliveness "$HOSTNAME" "up" ||
2894                 return $?
2895
2896         check_route_aliveness "$RPEER" "up" ||
2897                 return $?
2898
2899         do_lnetctl ping ${RPEER_NIDS[0]} ||
2900                 error "Failed to ping ${RPEER_NIDS[0]}"
2901
2902         do_node $RPEER "$LNETCTL ping ${LNIDS[0]}" ||
2903                 error "$RPEER failed to ping ${LNIDS[0]}"
2904
2905         # Stop LNet on local host
2906         do_lnetctl lnet unconfigure ||
2907                 error "Failed to stop LNet rc=$?"
2908
2909         check_router_ni_status "down" "up" ||
2910                 return $?
2911
2912         check_route_aliveness "$RPEER" "up" ||
2913                 return $?
2914
2915         # The NI used to send the message to the destination will be the
2916         # router's NI on LOCAL_NET, so that's the drop count that will be
2917         # incremented
2918         local d1=$(do_node $ROUTER $LNETCTL net show -v --net $LOCAL_NET | \
2919                                    awk '/drop_count:/{print $NF}')
2920
2921         # Ping from RPEER to local host should be dropped by the router
2922         do_node $RPEER "$LCTL ping ${LNIDS[0]}" &&
2923                 error "$RPEER expected ping to fail"
2924
2925         local d2=$(do_node $ROUTER $LNETCTL net show -v --net $LOCAL_NET | \
2926                                    awk '/drop_count:/{print $NF}')
2927
2928         [[ $((d2 - d1)) -ne $expected ]] &&
2929                 error "Expected drop count change by $expected: $d1 -> $d2"
2930
2931         return 0
2932 }
2933
2934 test_226() {
2935         setup_router_test avoid_asym_router_failure=0 --peer-timeout=10 ||
2936                 return $?
2937
2938         do_rtr_peer_health_test 1 ||
2939                 return $?
2940
2941         cleanup_router_test ||
2942                 return $?
2943 }
2944 run_test 226 "Check router peer health enabled"
2945
2946 test_227() {
2947         setup_router_test avoid_asym_router_failure=0 --peer-timeout=0 ||
2948                 return $?
2949
2950         do_rtr_peer_health_test 0 ||
2951                 return $?
2952
2953         cleanup_router_test ||
2954                 return $?
2955 }
2956 run_test 227 "Check router peer health disabled"
2957
2958 test_230() {
2959         # LU-12815
2960         echo "Check valid values; Should succeed"
2961         local i
2962         local lnid
2963         local cmd
2964         for ((i = 4; i < 16; i+=1)); do
2965                 reinit_dlc || return $?
2966                 add_net "tcp" "${INTERFACES[0]}" || return $?
2967                 do_lnetctl net set --all --conns-per-peer $i ||
2968                         error "should have succeeded $?"
2969                 $LNETCTL net show -v 1 | grep -q "conns_per_peer: $i" ||
2970                         error "failed to set conns-per-peer to $i"
2971                 lnid="$(lctl list_nids | head -n 1)"
2972                 do_lnetctl ping "$lnid" ||
2973                         error "failed to ping myself"
2974
2975                 # "lctl --net tcp conn_list" prints the list of active
2976                 # connections. Since we're pinging ourselves, there should be
2977                 # 2 Control connections plus 2*conns_per_peer connections
2978                 # created (one Bulk Input, one Bulk Output in each pair).
2979                 # Here's the sample output for conns_per_peer set to 1:
2980                 # 12345-1.1.1.1@tcp I[0]host01->host01:988 2626560/1061296 nonagle
2981                 # 12345-1.1.1.1@tcp O[0]host01->host01:1022 2626560/1061488 nonagle
2982                 # 12345-1.1.1.1@tcp C[0]host01->host01:988 2626560/1061296 nonagle
2983                 # 12345-1.1.1.1@tcp C[0]host01->host01:1023 2626560/1061488 nonagle
2984                 cmd="printf 'network tcp\nconn_list\n' | lctl | grep -c '$lnid'"
2985
2986                 # Expect 2+conns_per_peer*2 connections. Wait no longer
2987                 # than 2 seconds.
2988                 wait_update $HOSTNAME "$cmd" "$((2+i*2))" 2 ||
2989                         error "expected number of tcp connections $((2+i*2))"
2990         done
2991
2992         reinit_dlc || return $?
2993         add_net "tcp" "${INTERFACES[0]}" || return $?
2994         echo "Set > 127; Should fail"
2995         do_lnetctl net set --all --conns-per-peer 128 &&
2996                 error "should have failed $?"
2997
2998         reinit_dlc || return $?
2999         add_net "tcp" "${INTERFACES[0]}" || return $?
3000         echo "Set < 0; Should be ignored"
3001         do_lnetctl net set --all --conns-per-peer -1 ||
3002                 error "should have succeeded $?"
3003         $LNETCTL net show -v 1 | grep -q "conns_per_peer: 1" ||
3004                 error "Did not stay at default"
3005 }
3006 run_test 230 "Test setting conns-per-peer"
3007
3008 test_231() {
3009         reinit_dlc || return $?
3010
3011         do_lnetctl net add --net tcp --if ${INTERFACES[0]} ||
3012                 error "Failed to add net"
3013
3014         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml
3015         sed -i 's/peer_timeout: .*$/peer_timeout: 0/' \
3016                 $TMP/sanity-lnet-$testnum-expected.yaml
3017
3018         reinit_dlc || return $?
3019
3020         do_lnetctl import $TMP/sanity-lnet-$testnum-expected.yaml ||
3021                 error "Failed to import configuration"
3022
3023         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
3024
3025         compare_yaml_files || error "Wrong config after import"
3026
3027         do_lnetctl net del --net tcp --if ${INTERFACES[0]} ||
3028                 error "Failed to delete net tcp"
3029
3030         do_lnetctl net add --net tcp --if ${INTERFACES[0]} --peer-timeout=0 ||
3031                 error "Failed to add net with peer-timeout=0"
3032
3033         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
3034
3035         compare_yaml_files || error "Wrong config after lnetctl net add"
3036
3037         reinit_dlc || return $?
3038
3039         # lnet/include/lnet/lib-lnet.h defines DEFAULT_PEER_TIMEOUT 180
3040         sed -i 's/peer_timeout: .*$/peer_timeout: 180/' \
3041                 $TMP/sanity-lnet-$testnum-expected.yaml
3042
3043         sed -i '/^.*peer_timeout:.*$/d' $TMP/sanity-lnet-$testnum-actual.yaml
3044
3045         do_lnetctl import $TMP/sanity-lnet-$testnum-actual.yaml ||
3046                 error "Failed to import config without peer_timeout"
3047
3048         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
3049
3050         compare_yaml_files
3051 }
3052 run_test 231 "Check DLC handling of peer_timeout parameter"
3053
3054 ### Test that linux route is added for each ni
3055 test_250() {
3056         reinit_dlc || return $?
3057         add_net "tcp" "${INTERFACES[0]}" || return $?
3058         ip route show table ${INTERFACES[0]} | grep -q "${INTERFACES[0]}"
3059 }
3060 run_test 250 "test that linux routes are added"
3061
3062 test_251() {
3063         [[ ${NETTYPE} =~ kfi* ]] ||
3064                 skip "Need kfi NETTYPE"
3065
3066         reinit_dlc || return $?
3067         add_net "kfi" "${INTERFACES[0]}" || return $?
3068         add_net "kfi1" "${INTERFACES[0]}" || return $?
3069         add_net "kfi10" "${INTERFACES[0]}" || return $?
3070         return 0
3071 }
3072 run_test 251 "Define multiple kfi networks on single interface"
3073
3074 test_300() {
3075         # LU-13274
3076         local header
3077         local out=$TMP/$tfile
3078         local prefix=/usr/include/linux/lnet
3079
3080         # We use a hard coded prefix so that this test will not fail
3081         # when run in tree.
3082         CC=${CC:-cc}
3083         if ! which $CC > /dev/null 2>&1; then
3084                 skip_env "$CC is not installed"
3085         fi
3086
3087         cleanup_lnet || exit 1
3088         load_lnet
3089
3090         local cc_args="-Wall -Werror -std=c99 -c -x c /dev/null -o $out"
3091         if ! [[ -d $prefix ]]; then
3092                 # Assume we're running in tree and fixup the include path.
3093                 prefix=$LUSTRE/../lnet/include/uapi/linux/lnet
3094                 cc_args+=" -I $LUSTRE/../lnet/include/uapi"
3095         fi
3096
3097         for header in $prefix/*.h; do
3098                 if ! [[ -f "$header" ]]; then
3099                         continue
3100                 fi
3101
3102                 echo "$CC $cc_args -include $header"
3103                 $CC $cc_args -include $header ||
3104                         error "cannot compile '$header'"
3105         done
3106         rm -f $out
3107 }
3108 run_test 300 "packaged LNet UAPI headers can be compiled"
3109
3110 # LU-16081 lnet: Memory leak on adding existing interface
3111
3112 test_301() {
3113         reinit_dlc || return $?
3114         do_lnetctl net add --net tcp --if ${INTERFACES[0]} ||
3115                 error "Failed to add net"
3116         do_lnetctl net add --net tcp --if ${INTERFACES[0]} &&
3117                 error "add net should have failed"
3118         do_lnetctl net del --net tcp --if ${INTERFACES[0]} ||
3119                 error "Failed to del net"
3120         unload_modules
3121 }
3122 run_test 301 "Check for dynamic adds of same/wrong interface (memory leak)"
3123
3124 test_302() {
3125         ! [[ $NETTYPE =~ (tcp|o2ib) ]] && skip "Need tcp or o2ib NETTYPE"
3126         reinit_dlc || return $?
3127
3128         add_net "${NETTYPE}" "${INTERFACES[0]}" || return $?
3129
3130         local nid=$($LCTL list_nids)
3131
3132         do_lnetctl ping ${nid} ||
3133                 error "pinging self failed $?"
3134         do_lnetctl debug peer --nid ${nid} ||
3135                 error "failed to dump peer debug info $?"
3136 }
3137 run_test 302 "Check that peer debug info can be dumped"
3138
3139 complete $SECONDS
3140
3141 cleanup_testsuite
3142 exit_status