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