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