Whamcloud - gitweb
LU-15519 quota: fallocate does not increase projectid usage
[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_26() {
936         reinit_dlc || return $?
937
938         do_lnetctl peer add --prim_nid 1.1.1.1@tcp --lock_prim ||
939                 error "Peer add with --lock_prim option failed $?"
940         local peer_state=$($LNETCTL peer show -v 4 --nid 1.1.1.1@tcp |
941                 awk '/peer state/ {print $NF}')
942         # This relies on the following peer state definition:
943         # #define LNET_PEER_LOCK_PRIMARY          BIT(20)
944         if ((!("$peer_state" & (1 << 20)))); then
945                 error "Peer state does not have 'locked' bit set: $peer_state"
946         fi
947         do_lnetctl peer del --prim_nid 1.1.1.1@tcp ||
948                 error "Peer del failed $?"
949         $LNETCTL peer show --nid 1.1.1.1@tcp | grep -q 1.1.1.1@tcp ||
950                 error "1.1.1.1@tcp is not listed"
951         do_lnetctl peer del --prim_nid 1.1.1.1@tcp --force ||
952                 error "Peer del --force failed $?"
953         do_lnetctl peer show --nid 1.1.1.1@tcp &&
954                 error "failed to delete 1.1.1.1@tcp"
955
956         return 0
957 }
958 run_test 26 "Delete peer with primary nid locked"
959
960 test_99a() {
961         reinit_dlc || return $?
962
963         echo "Invalid prim_nid - peer add"
964         do_lnetctl peer add --prim_nid foobar &&
965                 error "Command should have failed"
966
967         echo "Invalid prim_nid - peer del"
968         do_lnetctl peer del --prim_nid foobar &&
969                 error "Command should have failed"
970
971         echo "Delete non-existing peer"
972         do_lnetctl peer del --prim_nid 1.1.1.1@o2ib &&
973                 error "Command should have failed"
974
975         echo "Don't provide mandatory argument for peer del"
976         do_lnetctl peer del --nid 1.1.1.1@tcp &&
977                 error "Command should have failed"
978
979         echo "Don't provide mandatory argument for peer add"
980         do_lnetctl peer add --nid 1.1.1.1@tcp &&
981                 error "Command should have failed"
982
983         echo "Don't provide mandatory arguments peer add"
984         do_lnetctl peer add &&
985                 error "Command should have failed"
986
987         echo "Invalid secondary nids"
988         do_lnetctl peer add --prim_nid 1.1.1.1@tcp --nid foobar &&
989                 error "Command should have failed"
990
991         echo "Exceed max nids per peer"
992         do_lnetctl peer add --prim_nid 1.1.1.1@tcp --nid 1.1.1.[2-255]@tcp &&
993                 error "Command should have failed"
994
995         echo "Invalid net type"
996         do_lnetctl peer add --prim_nid 1@foo &&
997                 error "Command should have failed"
998
999         echo "Invalid nid format"
1000         local invalid_nids="1@tcp 1@o2ib 1.1.1.1@gni"
1001
1002         local nid
1003         for nid in ${invalid_nids}; do
1004                 echo "Check invalid primary nid - '$nid'"
1005                 do_lnetctl peer add --prim_nid $nid &&
1006                         error "Command should have failed"
1007         done
1008
1009         local invalid_strs="[2-1]@gni [a-f/x]@gni 256.256.256.256@tcp"
1010         invalid_strs+=" 1.1.1.1.[2-5/f]@tcp 1.]2[.3.4@o2ib"
1011         invalid_strs+="1.[2-4,[5-6],7-8].1.1@tcp foobar"
1012
1013         local nidstr
1014         for nidstr in ${invalid_strs}; do
1015                 echo "Check invalid nidstring - '$nidstr'"
1016                 do_lnetctl peer add --prim_nid 1.1.1.1@tcp --nid $nidstr &&
1017                         error "Command should have failed"
1018         done
1019
1020         echo "Add non-local gateway"
1021         do_lnetctl route add --net tcp --gateway 1@gni &&
1022                 error "Command should have failed"
1023
1024         return 0
1025 }
1026 run_test 99a "Check various invalid inputs to lnetctl peer"
1027
1028 test_99b() {
1029         reinit_dlc || return $?
1030
1031         create_base_yaml_file
1032
1033         cat <<EOF > $TMP/sanity-lnet-$testnum-invalid.yaml
1034 peer:
1035     - primary nid: 99.99.99.99@tcp
1036       Multi-Rail: Foobar
1037       peer ni:
1038         - nid: 99.99.99.99@tcp
1039 EOF
1040         do_lnetctl import < $TMP/sanity-lnet-$testnum-invalid.yaml &&
1041                 error "import should have failed"
1042         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
1043         compare_yaml_files
1044 }
1045 run_test 99b "Invalid value for Multi-Rail in yaml import"
1046
1047 have_interface() {
1048         local if="$1"
1049         local ip=$(ip addr show dev $if | awk '/ inet /{print $2}')
1050         [[ -n $ip ]]
1051 }
1052
1053 add_net() {
1054         local net="$1"
1055         local if="$2"
1056
1057         if ! lsmod | grep -q ksocklnd ; then
1058                 load_module ../lnet/klnds/socklnd/ksocklnd ||
1059                         error "Can't load ksocklnd.ko"
1060         fi
1061
1062         do_lnetctl net add --net ${net} --if ${if} ||
1063                 error "Failed to add net ${net} on if ${if}"
1064 }
1065
1066 compare_route_add() {
1067         local rnet="$1"
1068         local gw="$2"
1069
1070         local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
1071
1072         do_lnetctl route add --net ${rnet} --gateway ${gw} ||
1073                 error "route add failed $?"
1074         # CPT configuration is pruned from the exported yaml, since the default
1075         # can vary across test systems (unlike default values for things like
1076         # peer_credits, peer_timeout, etc.)
1077         $LNETCTL export --backup | grep -v CPT > $actual ||
1078                 error "export failed $?"
1079         validate_gateway_nids
1080         return $?
1081 }
1082
1083 test_100() {
1084         reinit_dlc || return $?
1085         add_net "tcp" "${INTERFACES[0]}"
1086         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
1087 net:
1088     - net type: tcp
1089       local NI(s):
1090         - interfaces:
1091               0: ${INTERFACES[0]}
1092           tunables:
1093               peer_timeout: 180
1094               peer_credits: 8
1095               peer_buffer_credits: 0
1096               credits: 256
1097           lnd tunables:
1098               conns_per_peer: 1
1099 route:
1100     - net: tcp7
1101       gateway: 7.7.7.7@tcp
1102       hop: -1
1103       priority: 0
1104       health_sensitivity: 1
1105 peer:
1106     - primary nid: 7.7.7.7@tcp
1107       Multi-Rail: False
1108       peer ni:
1109         - nid: 7.7.7.7@tcp
1110 EOF
1111         append_global_yaml
1112         compare_route_add "tcp7" "7.7.7.7@tcp" || return $?
1113         compare_yaml_files
1114 }
1115 run_test 100 "Add route with single gw (tcp)"
1116
1117 test_101() {
1118         reinit_dlc || return $?
1119         add_net "tcp" "${INTERFACES[0]}"
1120         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
1121 net:
1122     - net type: tcp
1123       local NI(s):
1124         - interfaces:
1125               0: ${INTERFACES[0]}
1126           tunables:
1127               peer_timeout: 180
1128               peer_credits: 8
1129               peer_buffer_credits: 0
1130               credits: 256
1131           lnd tunables:
1132               conns_per_peer: 1
1133 route:
1134     - net: tcp8
1135       gateway: 8.8.8.10@tcp
1136       hop: -1
1137       priority: 0
1138       health_sensitivity: 1
1139     - net: tcp8
1140       gateway: 8.8.8.9@tcp
1141       hop: -1
1142       priority: 0
1143       health_sensitivity: 1
1144     - net: tcp8
1145       gateway: 8.8.8.8@tcp
1146       hop: -1
1147       priority: 0
1148       health_sensitivity: 1
1149 peer:
1150     - primary nid: 8.8.8.9@tcp
1151       Multi-Rail: False
1152       peer ni:
1153         - nid: 8.8.8.9@tcp
1154     - primary nid: 8.8.8.10@tcp
1155       Multi-Rail: False
1156       peer ni:
1157         - nid: 8.8.8.10@tcp
1158     - primary nid: 8.8.8.8@tcp
1159       Multi-Rail: False
1160       peer ni:
1161         - nid: 8.8.8.8@tcp
1162 EOF
1163         append_global_yaml
1164         compare_route_add "tcp8" "8.8.8.[8-10]@tcp"
1165 }
1166 run_test 101 "Add route with multiple gw (tcp)"
1167
1168 compare_route_del() {
1169         local rnet="$1"
1170         local gw="$2"
1171
1172         local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
1173
1174         do_lnetctl route del --net ${rnet} --gateway ${gw} ||
1175                 error "route del failed $?"
1176         $LNETCTL export --backup > $actual ||
1177                 error "export failed $?"
1178         validate_gateway_nids
1179 }
1180
1181 test_102() {
1182         reinit_dlc || return $?
1183         add_net "tcp" "${INTERFACES[0]}"
1184         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml
1185         do_lnetctl route add --net tcp102 --gateway 102.102.102.102@tcp ||
1186                 error "route add failed $?"
1187         compare_route_del "tcp102" "102.102.102.102@tcp"
1188 }
1189 run_test 102 "Delete route with single gw (tcp)"
1190
1191 test_103() {
1192         reinit_dlc || return $?
1193         add_net "tcp" "${INTERFACES[0]}"
1194         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml
1195         do_lnetctl route add --net tcp103 \
1196                 --gateway 103.103.103.[103-120/4]@tcp ||
1197                 error "route add failed $?"
1198         compare_route_del "tcp103" "103.103.103.[103-120/4]@tcp"
1199 }
1200 run_test 103 "Delete route with multiple gw (tcp)"
1201
1202 test_104() {
1203         local tyaml="$TMP/sanity-lnet-$testnum-expected.yaml"
1204
1205         reinit_dlc || return $?
1206
1207         # Default value is '3'
1208         local val=$($LNETCTL global show | awk '/response_tracking/{print $NF}')
1209         [[ $val -ne 3 ]] &&
1210                 error "Expect 3 found $val"
1211
1212         echo "Set < 0;  Should fail"
1213         do_lnetctl set response_tracking -1 &&
1214                 error "should have failed $?"
1215
1216         reinit_dlc || return $?
1217         cat <<EOF > $tyaml
1218 global:
1219     response_tracking: -10
1220 EOF
1221         do_lnetctl import < $tyaml &&
1222                 error "should have failed $?"
1223
1224         echo "Check valid values; Should succeed"
1225         local i
1226         for ((i = 0; i < 4; i++)); do
1227                 reinit_dlc || return $?
1228                 do_lnetctl set response_tracking $i ||
1229                         error "should have succeeded $?"
1230                 $LNETCTL global show | grep -q "response_tracking: $i" ||
1231                         error "Failed to set response_tracking to $i"
1232                 reinit_dlc || return $?
1233                 cat <<EOF > $tyaml
1234 global:
1235     response_tracking: $i
1236 EOF
1237                 do_lnetctl import < $tyaml ||
1238                         error "should have succeeded $?"
1239                 $LNETCTL global show | grep -q "response_tracking: $i" ||
1240                         error "Failed to set response_tracking to $i"
1241         done
1242
1243         reinit_dlc || return $?
1244         echo "Set > 3; Should fail"
1245         do_lnetctl set response_tracking 4 &&
1246                 error "should have failed $?"
1247
1248         reinit_dlc || return $?
1249         cat <<EOF > $tyaml
1250 global:
1251     response_tracking: 10
1252 EOF
1253         do_lnetctl import < $tyaml &&
1254                 error "should have failed $?"
1255         return 0
1256 }
1257 run_test 104 "Set/check response_tracking param"
1258
1259 test_105() {
1260         reinit_dlc || return $?
1261         add_net "tcp" "${INTERFACES[0]}"
1262         do_lnetctl route add --net tcp105 --gateway 105.105.105.105@tcp ||
1263                 error "route add failed $?"
1264         do_lnetctl peer add --prim 105.105.105.105@tcp &&
1265                 error "peer add should fail"
1266
1267         return 0
1268 }
1269 run_test 105 "Adding duplicate GW peer should fail"
1270
1271 test_106() {
1272         reinit_dlc || return $?
1273         add_net "tcp" "${INTERFACES[0]}"
1274         do_lnetctl route add --net tcp106 --gateway 106.106.106.106@tcp ||
1275                 error "route add failed $?"
1276         do_lnetctl peer del --prim 106.106.106.106@tcp &&
1277                 error "peer del should fail"
1278
1279         return 0
1280 }
1281 run_test 106 "Deleting GW peer should fail"
1282
1283 test_200() {
1284         cleanup_lnet || exit 1
1285         load_lnet "networks=\"\""
1286         do_ns $LNETCTL lnet configure --all || exit 1
1287         $LNETCTL net show --net tcp | grep -q "nid: ${FAKE_IP}@tcp$"
1288 }
1289 run_test 200 "load lnet w/o module option, configure in a non-default namespace"
1290
1291 test_201() {
1292         cleanup_lnet || exit 1
1293         load_lnet "networks=tcp($FAKE_IF)"
1294         do_ns $LNETCTL lnet configure --all || exit 1
1295         $LNETCTL net show --net tcp | grep -q "nid: ${FAKE_IP}@tcp$"
1296 }
1297 run_test 201 "load lnet using networks module options in a non-default namespace"
1298
1299 test_202() {
1300         cleanup_lnet || exit 1
1301         load_lnet "networks=\"\" ip2nets=\"tcp0($FAKE_IF) ${FAKE_IP}\""
1302         do_ns $LNETCTL lnet configure --all || exit 1
1303         $LNETCTL net show | grep -q "nid: ${FAKE_IP}@tcp$"
1304 }
1305 run_test 202 "load lnet using ip2nets in a non-default namespace"
1306
1307
1308 ### Add the interfaces in the target namespace
1309
1310 test_203() {
1311         cleanup_lnet || exit 1
1312         load_lnet
1313         do_lnetctl lnet configure || exit 1
1314         do_ns $LNETCTL net add --net tcp0 --if $FAKE_IF
1315 }
1316 run_test 203 "add a network using an interface in the non-default namespace"
1317
1318 LNET_PARAMS_FILE="$TMP/$TESTSUITE.parameters"
1319 function save_lnet_params() {
1320         $LNETCTL global show | egrep -v '^global:$' |
1321                                sed 's/://' > $LNET_PARAMS_FILE
1322 }
1323
1324 function restore_lnet_params() {
1325         local param value
1326         while read param value; do
1327                 [[ $param == max_intf ]] && continue
1328                 [[ $param == lnd_timeout ]] && continue
1329                 $LNETCTL set ${param} ${value} ||
1330                         error "Failed to restore ${param} to ${value}"
1331         done < $LNET_PARAMS_FILE
1332 }
1333
1334 function lnet_health_pre() {
1335         save_lnet_params
1336
1337         # Lower transaction timeout to speed up test execution
1338         $LNETCTL set transaction_timeout 10 ||
1339                 error "Failed to set transaction_timeout $?"
1340
1341         RETRY_PARAM=$($LNETCTL global show | awk '/retry_count/{print $NF}')
1342         RSND_PRE=$($LNETCTL stats show | awk '/resend_count/{print $NF}')
1343         LO_HVAL_PRE=$($LNETCTL net show -v 2 | awk '/health value/{print $NF}' |
1344                       xargs echo | sed 's/ /+/g' | bc -l)
1345
1346         RMT_HVAL_PRE=$($LNETCTL peer show --nid ${RNIDS[0]} -v 2 2>/dev/null |
1347                        awk '/health value/{print $NF}' | xargs echo |
1348                        sed 's/ /+/g' | bc -l)
1349
1350         # Might not have any peers so initialize to zero.
1351         RMT_HVAL_PRE=${RMT_HVAL_PRE:-0}
1352
1353         return 0
1354 }
1355
1356 function lnet_health_post() {
1357         RSND_POST=$($LNETCTL stats show | awk '/resend_count/{print $NF}')
1358         LO_HVAL_POST=$($LNETCTL net show -v 2 |
1359                        awk '/health value/{print $NF}' |
1360                        xargs echo | sed 's/ /+/g' | bc -l)
1361
1362         RMT_HVAL_POST=$($LNETCTL peer show --nid ${RNIDS[0]} -v 2 2>/dev/null |
1363                         awk '/health value/{print $NF}' | xargs echo |
1364                         sed 's/ /+/g' | bc -l)
1365
1366         # Might not have any peers so initialize to zero.
1367         RMT_HVAL_POST=${RMT_HVAL_POST:-0}
1368
1369         ${VERBOSE} &&
1370         echo "Pre resends: $RSND_PRE" &&
1371         echo "Post resends: $RSND_POST" &&
1372         echo "Resends delta: $((RSND_POST - RSND_PRE))" &&
1373         echo "Pre local health: $LO_HVAL_PRE" &&
1374         echo "Post local health: $LO_HVAL_POST" &&
1375         echo "Pre remote health: $RMT_HVAL_PRE" &&
1376         echo "Post remote health: $RMT_HVAL_POST"
1377
1378         restore_lnet_params
1379
1380         do_lnetctl peer set --health 1000 --all
1381         do_lnetctl net set --health 1000 --all
1382
1383         return 0
1384 }
1385
1386 function check_no_resends() {
1387         echo "Check that no resends took place"
1388         [[ $RSND_POST -ne $RSND_PRE ]] &&
1389                 error "Found resends: $RSND_POST != $RSND_PRE"
1390
1391         return 0
1392 }
1393
1394 function check_resends() {
1395         local delta=$((RSND_POST - RSND_PRE))
1396
1397         echo "Check that $RETRY_PARAM resends took place"
1398         [[ $delta -ne $RETRY_PARAM ]] &&
1399                 error "Expected $RETRY_PARAM resends found $delta"
1400
1401         return 0
1402 }
1403
1404 function check_no_local_health() {
1405         echo "Check that local NI health is unchanged"
1406         [[ $LO_HVAL_POST -ne $LO_HVAL_PRE ]] &&
1407                 error "Local health changed: $LO_HVAL_POST != $LO_HVAL_PRE"
1408
1409         return 0
1410 }
1411
1412 function check_local_health() {
1413         echo "Check that local NI health has been changed"
1414         [[ $LO_HVAL_POST -eq $LO_HVAL_PRE ]] &&
1415                 error "Local health unchanged: $LO_HVAL_POST == $LO_HVAL_PRE"
1416
1417         return 0
1418 }
1419
1420 function check_no_remote_health() {
1421         echo "Check that remote NI health is unchanged"
1422         [[ $RMT_HVAL_POST -ne $RMT_HVAL_PRE ]] &&
1423                 error "Remote health changed: $RMT_HVAL_POST != $RMT_HVAL_PRE"
1424
1425         return 0
1426 }
1427
1428 function check_remote_health() {
1429         echo "Check that remote NI health has been changed"
1430         [[ $RMT_HVAL_POST -eq $RMT_HVAL_PRE ]] &&
1431                 error "Remote health unchanged: $RMT_HVAL_POST == $RMT_HVAL_PRE"
1432
1433         return 0
1434 }
1435
1436 RNODE=""
1437 RLOADED=false
1438 NET_DEL_ARGS=""
1439 RNIDS=( )
1440 LNIDS=( )
1441 setup_health_test() {
1442         local need_mr=$1
1443         local rc=0
1444
1445         local rnodes=$(remote_nodes_list)
1446         [[ -z $rnodes ]] && skip "Need at least 1 remote node"
1447
1448         cleanup_lnet || error "Failed to cleanup before test execution"
1449
1450         # Loading modules should configure LNet with the appropriate
1451         # test-framework configuration
1452         load_lnet "config_on_load=1" || error "Failed to load modules"
1453
1454         LNIDS=( $($LCTL list_nids | xargs echo) )
1455
1456         RNODE=$(awk '{print $1}' <<<$rnodes)
1457         RNIDS=( $(do_node $RNODE $LCTL list_nids | xargs echo) )
1458
1459         if [[ -z ${RNIDS[@]} ]]; then
1460                 do_rpc_nodes $RNODE load_lnet "config_on_load=1"
1461                 RLOADED=true
1462                 RNIDS=( $(do_node $RNODE $LCTL list_nids | xargs echo) )
1463         fi
1464
1465         [[ ${#LNIDS[@]} -lt 1 ]] &&
1466                 error "No NIDs configured for local host $HOSTNAME"
1467         [[ ${#RNIDS[@]} -lt 1 ]] &&
1468                 error "No NIDs configured for remote host $RNODE"
1469
1470         do_lnetctl discover ${RNIDS[0]} ||
1471                 error "Unable to discover ${RNIDS[0]}"
1472
1473         local mr=$($LNETCTL peer show --nid ${RNIDS[0]} |
1474                    awk '/Multi-Rail/{print $NF}')
1475
1476         if ${need_mr} && [[ $mr == False ]]; then
1477                 cleanup_health_test || return $?
1478                 skip "Need MR peer"
1479         fi
1480
1481         if ( ! ${need_mr} && [[ ${#RNIDS[@]} -gt 1 ]] ) ||
1482            ( ! ${need_mr} && [[ ${#LNIDS[@]} -gt 1 ]] ); then
1483                 cleanup_health_test || return $?
1484                 skip "Need SR peer"
1485         fi
1486
1487         if ${need_mr} && [[ ${#RNIDS[@]} -lt 2 ]]; then
1488                 # Add a second, reachable NID to rnode.
1489                 local net=${RNIDS[0]}
1490
1491                 net="${net//*@/}1"
1492
1493                 local if=$(do_rpc_nodes --quiet $RNODE lnet_if_list)
1494                 [[ -z $if ]] &&
1495                         error "Failed to determine interface for $RNODE"
1496
1497                 do_rpc_nodes $RNODE "$LNETCTL lnet configure"
1498                 do_rpc_nodes $RNODE "$LNETCTL net add --net $net --if $if" ||
1499                         rc=$?
1500                 if [[ $rc -ne 0 ]]; then
1501                         error "Failed to add interface to $RNODE rc=$?"
1502                 else
1503                         RNIDS[1]="${RNIDS[0]}1"
1504                         NET_DEL_ARGS="--net $net --if $if"
1505                 fi
1506         fi
1507
1508         if ${need_mr} && [[ ${#LNIDS[@]} -lt 2 ]]; then
1509                 local net=${LNIDS[0]}
1510                 net="${net//*@/}1"
1511
1512                 do_lnetctl lnet configure &&
1513                         do_lnetctl net add --net $net --if ${INTERFACES[0]} ||
1514                         rc=$?
1515                 if [[ $rc -ne 0 ]]; then
1516                         error "Failed to add interface rc=$?"
1517                 else
1518                         LNIDS[1]="${LNIDS[0]}1"
1519                 fi
1520         fi
1521
1522         $LNETCTL net show
1523
1524         $LNETCTL peer show -v 2 | egrep -e nid -e health
1525
1526         $LCTL set_param debug=+net
1527
1528         return 0
1529
1530 }
1531
1532 cleanup_health_test() {
1533         local rc=0
1534
1535         if [[ -n $NET_DEL_ARGS ]]; then
1536                 do_rpc_nodes $RNODE \
1537                         "$LNETCTL net del $NET_DEL_ARGS" ||
1538                         rc=$((rc + $?))
1539                 NET_DEL_ARGS=""
1540         fi
1541
1542         unload_modules || rc=$?
1543
1544         if $RLOADED; then
1545                 do_rpc_nodes $RNODE unload_modules_local ||
1546                         rc=$((rc + $?))
1547                 RLOADED=false
1548         fi
1549
1550         [[ $rc -ne 0 ]] &&
1551                 error "Failed cleanup"
1552
1553         return $rc
1554 }
1555
1556 add_health_test_drop_rules() {
1557         local hstatus=$1
1558         local lnid rnid
1559
1560         for lnid in "${LNIDS[@]}"; do
1561                 for rnid in "${RNIDS[@]}"; do
1562                         $LCTL net_drop_add -s $lnid -d $rnid -m GET -r 1 -e ${hstatus}
1563                 done
1564         done
1565 }
1566
1567 # See lnet/lnet/lib-msg.c:lnet_health_check()
1568 LNET_LOCAL_RESEND_STATUSES="local_interrupt local_dropped local_aborted"
1569 LNET_LOCAL_RESEND_STATUSES+=" local_no_route local_timeout"
1570 LNET_LOCAL_NO_RESEND_STATUSES="local_error"
1571 test_204() {
1572         setup_health_test false || return $?
1573
1574         local hstatus
1575         for hstatus in ${LNET_LOCAL_RESEND_STATUSES} \
1576                        ${LNET_LOCAL_NO_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_no_resends || return $?
1588                 check_no_local_health || return $?
1589         done
1590
1591         cleanup_health_test || return $?
1592
1593         return 0
1594 }
1595 run_test 204 "Check no health or resends for single-rail local failures"
1596
1597 test_205() {
1598         setup_health_test true || return $?
1599
1600         local hstatus
1601         for hstatus in ${LNET_LOCAL_RESEND_STATUSES}; do
1602                 echo "Simulate $hstatus"
1603                 lnet_health_pre || return $?
1604
1605                 add_health_test_drop_rules ${hstatus}
1606                 do_lnetctl discover ${RNIDS[0]} &&
1607                         error "Should have failed"
1608                 $LCTL net_drop_del -a
1609
1610                 lnet_health_post
1611
1612                 check_resends || return $?
1613                 check_local_health || return $?
1614         done
1615
1616         for hstatus in ${LNET_LOCAL_NO_RESEND_STATUSES}; do
1617                 echo "Simulate $hstatus"
1618                 lnet_health_pre || return $?
1619
1620                 add_health_test_drop_rules ${hstatus}
1621                 do_lnetctl discover ${RNIDS[0]} &&
1622                         error "Should have failed"
1623                 $LCTL net_drop_del -a
1624
1625                 lnet_health_post
1626
1627                 check_no_resends || return $?
1628                 check_local_health || return $?
1629         done
1630
1631         cleanup_health_test || return $?
1632
1633         return 0
1634 }
1635 run_test 205 "Check health and resends for multi-rail local failures"
1636
1637 # See lnet/lnet/lib-msg.c:lnet_health_check()
1638 LNET_REMOTE_RESEND_STATUSES="remote_dropped"
1639 LNET_REMOTE_NO_RESEND_STATUSES="remote_error remote_timeout"
1640 test_206() {
1641         setup_health_test false || return $?
1642
1643         local hstatus
1644         for hstatus in ${LNET_REMOTE_RESEND_STATUSES} \
1645                        ${LNET_REMOTE_NO_RESEND_STATUSES}; do
1646                 echo "Simulate $hstatus"
1647                 lnet_health_pre || return $?
1648
1649                 add_health_test_drop_rules ${hstatus}
1650                 do_lnetctl discover ${RNIDS[0]} &&
1651                         error "Should have failed"
1652                 $LCTL net_drop_del -a
1653
1654                 lnet_health_post
1655
1656                 check_no_resends || return $?
1657                 check_no_local_health || return $?
1658                 check_no_remote_health || return $?
1659         done
1660
1661         cleanup_health_test || return $?
1662
1663         return 0
1664 }
1665 run_test 206 "Check no health or resends for single-rail remote failures"
1666
1667 test_207() {
1668         setup_health_test true || return $?
1669
1670         local hstatus
1671         for hstatus in ${LNET_REMOTE_RESEND_STATUSES}; do
1672                 echo "Simulate $hstatus"
1673                 lnet_health_pre || return $?
1674
1675                 add_health_test_drop_rules ${hstatus}
1676
1677                 do_lnetctl discover ${RNIDS[0]} &&
1678                         error "Should have failed"
1679
1680                 lnet_health_post
1681
1682                 $LCTL net_drop_del -a
1683
1684                 check_resends || return $?
1685                 check_no_local_health || return $?
1686                 check_remote_health || return $?
1687                 do_lnetctl peer set --health 1000 --all ||
1688                         error "Unable to reset health rc=$?"
1689         done
1690         for hstatus in ${LNET_REMOTE_NO_RESEND_STATUSES}; do
1691                 echo "Simulate $hstatus"
1692                 lnet_health_pre || return $?
1693
1694                 add_health_test_drop_rules ${hstatus}
1695
1696                 do_lnetctl discover ${RNIDS[0]} &&
1697                         error "Should have failed"
1698
1699                 lnet_health_post
1700
1701                 $LCTL net_drop_del -a
1702
1703                 check_no_resends || return $?
1704                 check_no_local_health || return $?
1705                 check_remote_health || return $?
1706                 do_lnetctl peer set --health 1000 --all ||
1707                         error "Unable to reset health rc=$?"
1708         done
1709
1710         cleanup_health_test || return $?
1711
1712         return 0
1713 }
1714 run_test 207 "Check health and resends for multi-rail remote errors"
1715
1716 test_208_load_and_check_lnet() {
1717         local ip2nets="$1"
1718         local p_nid="$2"
1719         local s_nid="$3"
1720         local num_expected=1
1721
1722         load_lnet "networks=\"\" ip2nets=\"${ip2nets_str}\""
1723
1724         $LCTL net up ||
1725                 error "Failed to load LNet with ip2nets \"${ip2nets_str}\""
1726
1727         [[ -n $s_nid ]] &&
1728                 num_expected=2
1729
1730         declare -a nids
1731         nids=( $($LCTL list_nids) )
1732
1733         [[ ${#nids[@]} -ne ${num_expected} ]] &&
1734                 error "Expect ${num_expected} NIDs found ${#nids[@]}"
1735
1736         [[ ${nids[0]} == ${p_nid} ]] ||
1737                 error "Expect NID \"${p_nid}\" found \"${nids[0]}\""
1738
1739         [[ -n $s_nid ]] && [[ ${nids[1]} != ${s_nid} ]] &&
1740                 error "Expect second NID \"${s_nid}\" found \"${nids[1]}\""
1741
1742         $LCTL net down &>/dev/null
1743         cleanup_lnet
1744 }
1745
1746 test_208() {
1747         cleanup_netns || error "Failed to cleanup netns before test execution"
1748         cleanup_lnet || error "Failed to unload modules before test execution"
1749         setup_fakeif || error "Failed to add fake IF"
1750
1751         have_interface "$FAKE_IF" ||
1752                 error "Expect $FAKE_IF configured but not found"
1753
1754         local if0_ip=$(ip --oneline addr show dev ${INTERFACES[0]} |
1755                        awk '/inet /{print $4}' |
1756                        sed 's:/.*::')
1757         if0_ip=($(echo "${if0_ip[@]}" | tr ' ' '\n' | uniq | tr '\n' ' '))
1758         local ip2nets_str="tcp(${INTERFACES[0]}) $if0_ip"
1759
1760         echo "Configure single NID \"$ip2nets_str\""
1761         test_208_load_and_check_lnet "${ip2nets_str}" "${if0_ip}@tcp"
1762
1763         ip2nets_str="tcp(${INTERFACES[0]}) $if0_ip; tcp1($FAKE_IF) $FAKE_IP"
1764         echo "Configure two NIDs; two NETs \"$ip2nets_str\""
1765         test_208_load_and_check_lnet "${ip2nets_str}" "${if0_ip}@tcp" \
1766                                      "${FAKE_IP}@tcp1"
1767
1768         ip2nets_str="tcp(${INTERFACES[0]}) $if0_ip; tcp($FAKE_IF) $FAKE_IP"
1769         echo "Configure two NIDs; one NET \"$ip2nets_str\""
1770         test_208_load_and_check_lnet "${ip2nets_str}" "${if0_ip}@tcp" \
1771                                      "${FAKE_IP}@tcp"
1772         local addr1=( ${if0_ip//./ } )
1773         local addr2=( ${FAKE_IP//./ } )
1774         local range="[${addr1[0]},${addr2[0]}]"
1775
1776         local i
1777         for i in $(seq 1 3); do
1778                 range+=".[${addr1[$i]},${addr2[$i]}]"
1779         done
1780         ip2nets_str="tcp(${INTERFACES[0]},${FAKE_IF}) ${range}"
1781
1782         echo "Configured two NIDs; one NET alt syntax \"$ip2nets_str\""
1783         test_208_load_and_check_lnet "${ip2nets_str}" "${if0_ip}@tcp" \
1784                                      "${FAKE_IP}@tcp"
1785
1786         cleanup_fakeif
1787
1788         echo "alt syntax with missing IF \"$ip2nets_str\""
1789         load_lnet "networks=\"\" ip2nets=\"${ip2nets_str}\""
1790
1791         echo "$LCTL net up should fail"
1792         $LCTL net up &&
1793                 error "LNet bringup should have failed"
1794
1795         cleanup_lnet
1796 }
1797 run_test 208 "Test various kernel ip2nets configurations"
1798
1799 test_209() {
1800         setup_health_test false || return $?
1801
1802         echo "Simulate network_timeout w/SR config"
1803         lnet_health_pre
1804
1805         add_health_test_drop_rules network_timeout
1806
1807         do_lnetctl discover ${RNIDS[0]} &&
1808                 error "Should have failed"
1809         $LCTL net_drop_del -a
1810
1811         lnet_health_post
1812
1813         check_no_resends || return $?
1814         check_no_local_health || return $?
1815         check_no_remote_health || return $?
1816
1817         cleanup_health_test || return $?
1818
1819         setup_health_test true || return $?
1820
1821         echo "Simulate network_timeout w/MR config"
1822
1823         lnet_health_pre
1824
1825         add_health_test_drop_rules network_timeout
1826
1827         do_lnetctl discover ${RNIDS[0]} &&
1828                 error "Should have failed"
1829         $LCTL net_drop_del -a
1830
1831         lnet_health_post
1832
1833         check_no_resends || return $?
1834         check_local_health || return $?
1835         check_remote_health || return $?
1836
1837         cleanup_health_test || return $?
1838
1839         return 0
1840 }
1841 run_test 209 "Check health, but not resends, for network timeout"
1842
1843 check_nid_in_recovq() {
1844         local recovq=$($LNETCTL debug recovery $1)
1845         local expect="$2"
1846         local nids=$($LCTL list_nids | xargs echo)
1847         local found=false
1848         local nid=""
1849
1850         echo "Check \"$1\" recovery queue"
1851         echo "$recovq"
1852         if [[ $(grep -c 'nid-'<<<$recovq) -ne $expect ]]; then
1853                 error "Expect $expect NIDs found: \"$recovq\""
1854         fi
1855
1856         [[ $expect -eq 0 ]] && return 0
1857
1858         for nid in ${nids}; do
1859                 grep -q "nid-0: $nid"<<<$recovq &&
1860                         found=true
1861         done
1862
1863         if ! $found; then
1864                 error "Didn't find local NIDs in recovery queue: \"$recovq\""
1865         fi
1866
1867         return 0
1868 }
1869
1870 # First enqueue happens at time 0.
1871 # 2nd at 0 + 2^0 = 1
1872 # 3rd at 1 + 2^1 = 3
1873 # 4th at 3 + 2^2 = 7
1874 # 5th at 7 + 2^3 = 15
1875 # e.g. after 10 seconds we would expect to have seen the 4th enqueue,
1876 # (3 pings sent, 4th about to happen) and the 5th enqueue is yet to
1877 # happen
1878 # If the recovery limit is 10 seconds, then when the 5th enqueue happens
1879 # we expect the peer NI to have aged out, so it will not actually be
1880 # queued.
1881 check_ping_count() {
1882         local queue="$1"
1883         local expect="$2"
1884
1885         echo "Check ping counts:"
1886         local ping_count
1887         if [[ $queue == "ni" ]]; then
1888                 $LNETCTL net show -v 2 | egrep 'nid|health value|ping'
1889                 ping_count=( $($LNETCTL net show -v 2 |
1890                                 awk '/ping_count/{print $NF}') )
1891         elif [[ $queue == "peer_ni" ]]; then
1892                 $LNETCTL peer show -v 2 | egrep 'nid|health value|ping'
1893                 ping_count=( $($LNETCTL peer show -v 2 |
1894                                 awk '/ping_count/{print $NF}') )
1895         else
1896                 error "Unrecognized queue \"$queue\""
1897                 return 1
1898         fi
1899
1900         local count
1901         local found=false
1902         for count in "${ping_count[@]}"; do
1903                 if [[ $count -eq $expect ]]; then
1904                         if [[ $expect -ne 0 ]] && $found ; then
1905                                 error "Found more than one interface matching \"$expect\" ping count"
1906                                 return 1
1907                         else
1908                                 echo "Expect ping count \"$expect\" found \"$count\""
1909                                 found=true;
1910                         fi
1911                 elif [[ $count -ne 0 ]]; then
1912                         error "Found interface with ping count \"$count\" but expect \"$expect\""
1913                         return 1
1914                 fi
1915         done
1916
1917         return 0
1918 }
1919
1920 test_210() {
1921         reinit_dlc || return $?
1922         add_net "tcp" "${INTERFACES[0]}" || return $?
1923         add_net "tcp1" "${INTERFACES[0]}" || return $?
1924
1925         local prim_nid=$($LCTL list_nids | head -n 1)
1926
1927         do_lnetctl discover $prim_nid ||
1928                 error "failed to discover myself"
1929
1930         # Set recovery limit to 10 seconds.
1931         do_lnetctl set recovery_limit 10 ||
1932                 error "failed to set recovery_limit"
1933
1934         $LCTL set_param debug=+net
1935         # Use local_error so LNet doesn't attempt to resend the discovery ping
1936         $LCTL net_drop_add -s *@tcp -d *@tcp -m GET -r 1 -e local_error
1937         $LCTL net_drop_add -s *@tcp1 -d *@tcp1 -m GET -r 1 -e local_error
1938         do_lnetctl discover $($LCTL list_nids | head -n 1) &&
1939                 error "Expected discovery to fail"
1940
1941         sleep 5
1942         check_nid_in_recovq "-l" 1
1943         check_ping_count "ni" "2"
1944
1945         sleep 5
1946
1947         check_nid_in_recovq "-l" 1
1948         check_ping_count "ni" "3"
1949
1950         $LCTL net_drop_del -a
1951
1952         return 0
1953 }
1954 run_test 210 "Local NI recovery checks"
1955
1956 test_211() {
1957         reinit_dlc || return $?
1958         add_net "tcp" "${INTERFACES[0]}" || return $?
1959         add_net "tcp1" "${INTERFACES[0]}" || return $?
1960
1961         local prim_nid=$($LCTL list_nids | head -n 1)
1962
1963         do_lnetctl discover $prim_nid ||
1964                 error "failed to discover myself"
1965
1966         # Set recovery limit to 10 seconds.
1967         do_lnetctl set recovery_limit 10 ||
1968                 error "failed to set recovery_limit"
1969
1970         $LCTL net_drop_add -s *@tcp -d *@tcp -m GET -r 1 -e remote_error
1971         $LCTL net_drop_add -s *@tcp1 -d *@tcp1 -m GET -r 1 -e remote_error
1972
1973         # Set health to 0 on one interface. This forces it onto the recovery
1974         # queue.
1975         $LNETCTL peer set --nid $prim_nid --health 0
1976
1977         # After 5 seconds, we expect the peer NI to still be in recovery
1978         sleep 5
1979         check_nid_in_recovq "-p" 1
1980         check_ping_count "peer_ni" "2"
1981
1982         # After 15 seconds, the peer NI should have been fully processed out of
1983         # the recovery queue. We'll allow a total of 17 seconds to account for
1984         # differences in sleeping for whole seconds vs. the more accurate time
1985         # keeping that is done in the recovery code.
1986         sleep 12
1987         check_nid_in_recovq "-p" 0
1988         check_ping_count "peer_ni" "4"
1989
1990         $LCTL net_drop_del -a
1991
1992         # Set health to force it back onto the recovery queue. Set to 500 means
1993         # in 5 seconds it should be back at maximum value. We'll wait a couple
1994         # more seconds than that to be safe.
1995         # NB: we reset the recovery limit to 0 (indefinite) so the peer NI is
1996         # eligible again
1997         do_lnetctl set recovery_limit 0 ||
1998                 error "failed to set recovery_limit"
1999
2000         $LNETCTL peer set --nid $prim_nid --health 500
2001
2002         check_nid_in_recovq "-p" 1
2003         check_ping_count "peer_ni" "2"
2004
2005         sleep 7
2006
2007         check_nid_in_recovq "-p" 0
2008         check_ping_count "peer_ni" "0"
2009
2010         return 0
2011 }
2012 run_test 211 "Remote NI recovery checks"
2013
2014 test_212() {
2015         local rnodes=$(remote_nodes_list)
2016         [[ -z $rnodes ]] && skip "Need at least 1 remote node"
2017
2018         cleanup_lnet || error "Failed to cleanup before test execution"
2019
2020         # Loading modules should configure LNet with the appropriate
2021         # test-framework configuration
2022         load_lnet "config_on_load=1" || error "Failed to load modules"
2023
2024         local my_nid=$($LCTL list_nids | head -n 1)
2025         [[ -z $my_nid ]] &&
2026                 error "Failed to get primary NID for local host $HOSTNAME"
2027
2028         local rnode=$(awk '{print $1}' <<<$rnodes)
2029         local rnodenids=$(do_node $rnode $LCTL list_nids | xargs echo)
2030         local rloaded=false
2031
2032         if [[ -z $rnodenids ]]; then
2033                 do_rpc_nodes $rnode load_lnet "config_on_load=1"
2034                 rloaded=true
2035                 rnodenids=$(do_node $rnode $LCTL list_nids | xargs echo)
2036         fi
2037
2038         local rnodepnid=$(awk '{print $1}' <<< $rnodenids)
2039
2040         [[ -z $rnodepnid ]] &&
2041                 error "Failed to get primary NID for remote host $rnode"
2042
2043         log "Initial discovery"
2044         do_lnetctl discover --force $rnodepnid ||
2045                 error "Failed to discover $rnodepnid"
2046
2047         do_node $rnode "$LNETCTL discover --force $my_nid" ||
2048                 error "$rnode failed to discover $my_nid"
2049
2050         log "Fail local discover ping to set LNET_PEER_REDISCOVER flag"
2051         $LCTL net_drop_add -s "*@$NETTYPE" -d "*@$NETTYPE" -r 1 -e local_error
2052         do_lnetctl discover --force $rnodepnid &&
2053                 error "Discovery should have failed"
2054         $LCTL net_drop_del -a
2055
2056         local nid
2057         for nid in $rnodenids; do
2058                 # We need GET (PING) delay just long enough so we can trigger
2059                 # discovery on the remote peer
2060                 $LCTL net_delay_add -s "*@$NETTYPE" -d $nid -r 1 -m GET -l 3
2061                 $LCTL net_drop_add -s "*@$NETTYPE" -d $nid -r 1 -m GET -e local_error
2062                 # We need PUT (PUSH) delay just long enough so we can process
2063                 # the PING failure
2064                 $LCTL net_delay_add -s "*@$NETTYPE" -d $nid -r 1 -m PUT -l 6
2065         done
2066
2067         log "Force $HOSTNAME to discover $rnodepnid (in background)"
2068         # We want to get a PING sent that we know will eventually fail.
2069         # The delay rules we added will ensure the ping is not sent until
2070         # the PUSH is also in flight (see below), and the drop rule ensures that
2071         # when the PING is eventually sent it will error out
2072         do_lnetctl discover --force $rnodepnid &
2073         local pid1=$!
2074
2075         # We want a discovery PUSH from rnode to put rnode back on our
2076         # discovery queue. This should cause us to try and send a PUSH to rnode
2077         # while the PING is still outstanding.
2078         log "Force $rnode to discover $my_nid"
2079         do_node $rnode $LNETCTL discover --force $my_nid
2080
2081         # At this point we'll have both PING_SENT and PUSH_SENT set for the
2082         # rnode peer. Wait for the PING to error out which should terminate the
2083         # discovery process that we backgrounded.
2084         log "Wait for $pid1"
2085         wait $pid1
2086         log "Finished wait on $pid1"
2087
2088         # The PING send failure clears the PING_SENT flag and puts the peer back
2089         # on the discovery queue. When discovery thread processes the peer it
2090         # will mistakenly clear the PUSH_SENT flag (and set PUSH_FAILED).
2091         # Discovery will then complete for this peer even though we have an
2092         # outstanding PUSH.
2093         # When PUSH is actually unlinked it will be forced back onto the
2094         # discovery queue, but we no longer have a ref on the peer. When
2095         # discovery completes again, we'll trip the ASSERT in
2096         # lnet_destroy_peer_locked()
2097
2098         # Delete the delay rules to send the PUSH
2099         $LCTL net_delay_del -a
2100         # Delete the drop rules
2101         $LCTL net_drop_del -a
2102
2103         unload_modules ||
2104                 error "Failed to unload modules"
2105         if $rloaded; then
2106                 do_rpc_nodes $rnode unload_modules_local ||
2107                         error "Failed to unload modules on $rnode"
2108         fi
2109
2110         return 0
2111 }
2112 run_test 212 "Check discovery refcount loss bug (LU-14627)"
2113
2114 test_213() {
2115         cleanup_netns || error "Failed to cleanup netns before test execution"
2116         cleanup_lnet || error "Failed to unload modules before test execution"
2117
2118         setup_fakeif || error "Failed to add fake IF"
2119         have_interface "$FAKE_IF" ||
2120                 error "Expect $FAKE_IF configured but not found"
2121
2122         reinit_dlc || return $?
2123
2124         add_net "tcp" "${INTERFACES[0]}" || return $?
2125         add_net "tcp" "$FAKE_IF" || return $?
2126
2127         local nid1=$(lctl list_nids | head -n 1)
2128         local nid2=$(lctl list_nids | tail --lines 1)
2129
2130         [[ $(lctl which_nid $nid1 $nid2) == $nid1 ]] ||
2131                 error "Expect nid1 \"$nid1\" to be preferred"
2132
2133         [[ $(lctl which_nid $nid2 $nid1) == $nid2 ]] ||
2134                 error "Expect nid2 \"$nid2\" to be preferred"
2135
2136         return 0
2137 }
2138 run_test 213 "Check LNetDist calculation for multiple local NIDs"
2139
2140 function check_ni_status() {
2141         local nid="$1"
2142         local expect="$2"
2143
2144         local status=$($LNETCTL net show |
2145                        grep -A 1 ${nid} |
2146                        awk '/status/{print $NF}')
2147
2148         echo "NI ${nid} expect status \"${expect}\" found \"${status}\""
2149         if [[ $status != $expect ]]; then
2150                 error "Error: Expect NI status \"$expect\" for NID \"$nid\" but found \"$status\""
2151         fi
2152
2153         return 0
2154 }
2155
2156 test_214() {
2157         cleanup_netns || error "Failed to cleanup netns before test execution"
2158         cleanup_lnet || error "Failed to unload modules before test execution"
2159
2160         setup_fakeif || error "Failed to add fake IF"
2161         have_interface "$FAKE_IF" ||
2162                 error "Expect $FAKE_IF configured but not found"
2163
2164         reinit_dlc || return $?
2165
2166         add_net "tcp" "${INTERFACES[0]}" || return $?
2167         add_net "tcp" "$FAKE_IF" || return $?
2168
2169         local nid1=$(lctl list_nids | head -n 1)
2170         local nid2=$(lctl list_nids | tail --lines 1)
2171
2172         check_ni_status "0@lo" up
2173         check_ni_status "$nid1" up
2174         check_ni_status "$nid2" up
2175
2176         echo "Set $FAKE_IF down"
2177         echo "ip link set dev $FAKE_IF down"
2178         ip link set dev $FAKE_IF down
2179         check_ni_status "0@lo" up
2180         check_ni_status "$nid1" up
2181         check_ni_status "$nid2" down
2182 }
2183 run_test 214 "Check local NI status when link is downed"
2184
2185 get_ni_stat() {
2186         local nid=$1
2187         local stat=$2
2188
2189         $LNETCTL net show -v 2 |
2190                 egrep -e nid -e $stat |
2191                 grep -wA 1 $nid |
2192                 awk '/'$stat':/{print $NF}'
2193 }
2194
2195 ni_stats_pre() {
2196         local nidvar s
2197         for nidvar in nid1 nid2; do
2198                 for stat in send_count recv_count; do
2199                         s=$(get_ni_stat ${!nidvar} $stat)
2200                         eval ${nidvar}_pre_${stat}=$s
2201                 done
2202         done
2203 }
2204
2205 ni_stats_post() {
2206         local nidvar s
2207         for nidvar in nid1 nid2; do
2208                 for stat in send_count recv_count; do
2209                         s=$(get_ni_stat ${!nidvar} $stat)
2210                         eval ${nidvar}_post_${stat}=$s
2211                 done
2212         done
2213 }
2214
2215 ni_stat_changed() {
2216         local nidvar=$1
2217         local stat=$2
2218
2219         local pre post
2220         eval pre=\${${nidvar}_pre_${stat}}
2221         eval post=\${${nidvar}_post_${stat}}
2222
2223         echo "${!nidvar} pre ${stat} $pre post ${stat} $post"
2224
2225         [[ $pre -ne $post ]]
2226 }
2227
2228 test_215() {
2229         cleanup_netns || error "Failed to cleanup netns before test execution"
2230         cleanup_lnet || error "Failed to unload modules before test execution"
2231
2232         reinit_dlc || return $?
2233
2234         add_net "tcp1" "${INTERFACES[0]}" || return $?
2235         add_net "tcp2" "${INTERFACES[0]}" || return $?
2236
2237         local nid1=$($LCTL list_nids | head -n 1)
2238         local nid2=$($LCTL list_nids | tail --lines 1)
2239
2240         do_lnetctl peer add --prim $nid1 --nid $nid2 ||
2241                 error "Failed to add peer"
2242
2243         local npings=25
2244
2245         for nidvarA in nid1 nid2; do
2246                 src=${!nidvarA}
2247                 dst=${!nidvarA}
2248                 for nidvarB in nid1 nid2; do
2249                         [[ $nidvarA == $nidvarB ]] && continue
2250
2251                         ni_stats_pre
2252
2253                         echo "$LNETCTL ping $dst x $npings"
2254                         for i in $(seq 1 $npings); do
2255                                 $LNETCTL ping $dst &>/dev/null ||
2256                                         error "$LNETCTL ping $dst failed"
2257                         done
2258
2259                         ni_stats_post
2260
2261                         # No source specified, sends to either NID should cause
2262                         # counts to increase across both NIs
2263                         for nidvar in nid1 nid2; do
2264                                 for stat in send_count recv_count; do
2265                                         ni_stat_changed $nidvar $stat ||
2266                                                 error "$stat unchanged for ${!nidvar}"
2267                                 done
2268                         done
2269
2270                         ni_stats_pre
2271
2272                         echo "$LNETCTL ping --source $src $dst x $npings"
2273                         for i in $(seq 1 $npings); do
2274                                 $LNETCTL ping --source $src $dst &>/dev/null ||
2275                                         error "$LNETCTL ping --source $src $dst failed"
2276                         done
2277
2278                         ni_stats_post
2279
2280                         # src nid == dest nid means stats for the _other_ NI
2281                         # should be unchanged
2282                         for nidvar in nid1 nid2; do
2283                                 for stat in send_count recv_count; do
2284                                         if [[ ${!nidvar} == $src ]]; then
2285                                                 ni_stat_changed $nidvar $stat ||
2286                                                         error "$stat unchanged for ${!nidvar}"
2287                                         else
2288                                                 ni_stat_changed $nidvar $stat &&
2289                                                         error "$stat changed for ${!nidvar}"
2290                                         fi
2291                                 done
2292                         done
2293                 done
2294                 # Double number of pings for next iteration because the net
2295                 # sequence numbers will have diverged
2296                 npings=$(($npings * 2))
2297         done
2298
2299         # Ping from nid1 to nid2 should fail
2300         do_lnetctl ping --source $nid1 $nid2 &&
2301                 error "ping from $nid1 to $nid2 should fail"
2302
2303         # Ping from nid2 to nid1 should fail
2304         do_lnetctl ping --source $nid2 $nid1 &&
2305                 error "ping from $nid2 to $nid1 should fail"
2306
2307         return 0
2308 }
2309 run_test 215 "Test lnetctl ping --source option"
2310
2311 test_216() {
2312         local rc=0
2313
2314         reinit_dlc || return $?
2315
2316         add_net "tcp" "${INTERFACES[0]}" || return $?
2317         add_net "tcp1" "${INTERFACES[0]}" || return $?
2318
2319         local nids=( $($LCTL list_nids | xargs echo) )
2320
2321         do_lnetctl discover ${nids[0]} ||
2322                 error "Initial discovery failed"
2323
2324         do_lnetctl ping --source ${nids[0]} ${nids[0]} ||
2325                 error "Initial ping failed $?"
2326
2327         do_lnetctl ping --source ${nids[1]} ${nids[1]} ||
2328                 error "Initial ping failed $?"
2329
2330         local src dst
2331         for src in "${nids[@]}"; do
2332                 for dst in "${nids[@]}"; do
2333                         $LCTL net_drop_add -r 1 -s $src -d $dst -e network_timeout
2334                 done
2335         done
2336
2337         do_lnetctl ping ${nids[0]} || rc=$?
2338
2339         $LCTL net_drop_del -a
2340
2341         [[ $rc -eq 0 ]] &&
2342                 error "expected ping to fail"
2343
2344         check_nid_in_recovq "-p" 0
2345         check_nid_in_recovq "-l" 1
2346
2347         return 0
2348 }
2349 run_test 216 "Failed send to peer NI owned by local host should not trigger peer NI recovery"
2350
2351 test_217() {
2352         reinit_dlc || return $?
2353
2354         [[ $($LNETCTL net show | grep -c nid) -ne 1 ]] &&
2355                 error "Unexpected number of NIs after initalizing DLC"
2356
2357         do_lnetctl discover 0@lo ||
2358                 error "Failed to discover 0@lo"
2359
2360         unload_modules
2361 }
2362 run_test 217 "Don't leak memory when discovering peer with nnis <= 1"
2363
2364 test_218() {
2365         reinit_dlc || return $?
2366
2367         [[ ${#INTERFACES[@]} -lt 2 ]] &&
2368                 skip "Need two LNet interfaces"
2369
2370         add_net "tcp" "${INTERFACES[0]}" || return $?
2371
2372         local nid1=$($LCTL list_nids | head -n 1)
2373
2374         do_lnetctl ping $nid1 ||
2375                 error "ping failed"
2376
2377         add_net "tcp" "${INTERFACES[1]}" || return $?
2378
2379         local nid2=$($LCTL list_nids | tail --lines 1)
2380
2381         do_lnetctl ping $nid2 ||
2382                 error "ping failed"
2383
2384         $LCTL net_drop_add -s $nid1 -d $nid1 -e local_error -r 1
2385
2386         do_lnetctl ping $nid1 &&
2387                 error "ping should have failed"
2388
2389         local health_recovered
2390         local i
2391
2392         for i in $(seq 1 5); do
2393                 health_recovered=$($LNETCTL net show -v 2 |
2394                                    grep -c 'health value: 1000')
2395
2396                 if [[ $health_recovered -ne 2 ]]; then
2397                         echo "Wait 1 second for health to recover"
2398                         sleep 1
2399                 else
2400                         break
2401                 fi
2402         done
2403
2404         health_recovered=$($LNETCTL net show -v 2 |
2405                            grep -c 'health value: 1000')
2406
2407         $LCTL net_drop_del -a
2408
2409         [[ $health_recovered -ne 2 ]] &&
2410                 do_lnetctl net show -v 2 | egrep -e nid -e health &&
2411                 error "Health hasn't recovered"
2412
2413         return 0
2414 }
2415 run_test 218 "Local recovery pings should exercise all available paths"
2416
2417 test_219() {
2418         reinit_dlc || return $?
2419         add_net "tcp" "${INTERFACES[0]}" || return $?
2420         add_net "tcp1" "${INTERFACES[0]}" || return $?
2421
2422         local nid1=$(lctl list_nids | head -n 1)
2423         local nid2=$(lctl list_nids | tail --lines 1)
2424
2425         do_lnetctl ping $nid1 ||
2426                 error "Ping failed $?"
2427         do_lnetctl ping $nid2 ||
2428                 error "Ping failed $?"
2429
2430         do_lnetctl discover $nid2 ||
2431                 error "Discovery failed"
2432
2433         $LNETCTL peer show --nid $nid1 | grep -q $nid2 ||
2434                 error "$nid2 is not listed under $nid1"
2435 }
2436 run_test 219 "Consolidate peer entries"
2437
2438 test_230() {
2439         # LU-12815
2440         echo "Check valid values; Should succeed"
2441         local i
2442         local lnid
2443         local cmd
2444         for ((i = 4; i < 16; i+=1)); do
2445                 reinit_dlc || return $?
2446                 add_net "tcp" "${INTERFACES[0]}" || return $?
2447                 do_lnetctl net set --all --conns-per-peer $i ||
2448                         error "should have succeeded $?"
2449                 $LNETCTL net show -v 1 | grep -q "conns_per_peer: $i" ||
2450                         error "failed to set conns-per-peer to $i"
2451                 lnid="$(lctl list_nids | head -n 1)"
2452                 do_lnetctl ping "$lnid" ||
2453                         error "failed to ping myself"
2454
2455                 # "lctl --net tcp conn_list" prints the list of active
2456                 # connections. Since we're pinging ourselves, there should be
2457                 # 2 Control connections plus 2*conns_per_peer connections
2458                 # created (one Bulk Input, one Bulk Output in each pair).
2459                 # Here's the sample output for conns_per_peer set to 1:
2460                 # 12345-1.1.1.1@tcp I[0]host01->host01:988 2626560/1061296 nonagle
2461                 # 12345-1.1.1.1@tcp O[0]host01->host01:1022 2626560/1061488 nonagle
2462                 # 12345-1.1.1.1@tcp C[0]host01->host01:988 2626560/1061296 nonagle
2463                 # 12345-1.1.1.1@tcp C[0]host01->host01:1023 2626560/1061488 nonagle
2464                 cmd="printf 'network tcp\nconn_list\n' | lctl | grep -c '$lnid'"
2465
2466                 # Expect 2+conns_per_peer*2 connections. Wait no longer
2467                 # than 2 seconds.
2468                 wait_update $HOSTNAME "$cmd" "$((2+i*2))" 2 ||
2469                         error "expected number of tcp connections $((2+i*2))"
2470         done
2471
2472         reinit_dlc || return $?
2473         add_net "tcp" "${INTERFACES[0]}" || return $?
2474         echo "Set > 127; Should fail"
2475         do_lnetctl net set --all --conns-per-peer 128 &&
2476                 error "should have failed $?"
2477
2478         reinit_dlc || return $?
2479         add_net "tcp" "${INTERFACES[0]}" || return $?
2480         echo "Set < 0; Should be ignored"
2481         do_lnetctl net set --all --conns-per-peer -1 ||
2482                 error "should have succeeded $?"
2483         $LNETCTL net show -v 1 | grep -q "conns_per_peer: 1" ||
2484                 error "Did not stay at default"
2485 }
2486 run_test 230 "Test setting conns-per-peer"
2487
2488 ### Test that linux route is added for each ni
2489 test_250() {
2490         reinit_dlc || return $?
2491         add_net "tcp" "${INTERFACES[0]}" || return $?
2492         ip route show table ${INTERFACES[0]} | grep -q "${INTERFACES[0]}"
2493 }
2494 run_test 250 "test that linux routes are added"
2495
2496 test_300() {
2497         # LU-13274
2498         local header
2499         local out=$TMP/$tfile
2500         local prefix=/usr/include/linux/lnet
2501
2502         # We use a hard coded prefix so that this test will not fail
2503         # when run in tree.
2504         CC=${CC:-cc}
2505         if ! which $CC > /dev/null 2>&1; then
2506                 skip_env "$CC is not installed"
2507         fi
2508
2509         cleanup_lnet || exit 1
2510         load_lnet
2511
2512         local cc_args="-Wall -Werror -std=c99 -c -x c /dev/null -o $out"
2513         if ! [[ -d $prefix ]]; then
2514                 # Assume we're running in tree and fixup the include path.
2515                 prefix=$LUSTRE/../lnet/include/uapi/linux/lnet
2516                 cc_args+=" -I $LUSTRE/../lnet/include/uapi"
2517         fi
2518
2519         for header in $prefix/*.h; do
2520                 if ! [[ -f "$header" ]]; then
2521                         continue
2522                 fi
2523
2524                 echo "$CC $cc_args -include $header"
2525                 $CC $cc_args -include $header ||
2526                         error "cannot compile '$header'"
2527         done
2528         rm -f $out
2529 }
2530 run_test 300 "packaged LNet UAPI headers can be compiled"
2531
2532 complete $SECONDS
2533
2534 cleanup_testsuite
2535 exit_status