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