Whamcloud - gitweb
LU-13734 lnet: Allow duplicate nets in ip2nets syntax
[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 test ARM testing until they are fixed
16 if [[ $(uname -m) = aarch64 ]]; then
17         # bug number for skipped test: LU-13704 LU-13701 LU-13701 LU-13701
18         ALWAYS_EXCEPT+="               204      205      206      207 "
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 export LNETCTL=${LNETCTL:-"$LUSTRE/../lnet/utils/lnetctl"}
35 [ ! -f "$LNETCTL" ] &&
36         export LNETCTL=$(which lnetctl 2> /dev/null)
37 [[ -z $LNETCTL ]] && skip "Need lnetctl"
38
39 restore_mounts=false
40
41 if is_mounted $MOUNT || is_mounted $MOUNT2; then
42         cleanupall || error "Failed cleanup prior to test execution"
43         restore_mounts=true
44 fi
45
46 cleanup_lnet() {
47         echo "Cleaning up LNet"
48         lsmod | grep -q lnet &&
49                 $LNETCTL lnet unconfigure 2>/dev/null
50         unload_modules
51 }
52
53 restore_modules=false
54 if module_loaded lnet ; then
55         cleanup_lnet || error "Failed to unload modules before test execution"
56         restore_modules=true
57 fi
58
59 cleanup_testsuite() {
60         trap "" EXIT
61         rm -f $TMP/sanity-dlc*
62         cleanup_netns
63         cleanup_lnet
64         if $restore_mounts; then
65                 setupall || error "Failed to setup Lustre after test execution"
66         elif $restore_modules; then
67                 load_modules ||
68                         error "Couldn't load modules after test execution"
69         fi
70         return 0
71 }
72
73 load_lnet() {
74         load_module ../libcfs/libcfs/libcfs
75         # Prevent local MODOPTS_LIBCFS being passed as part of environment
76         # variable to remote nodes
77         unset MODOPTS_LIBCFS
78
79         set_default_debug "neterror net nettrace malloc"
80         load_module ../lnet/lnet/lnet "$@"
81
82         LNDPATH=${LNDPATH:-"../lnet/klnds"}
83         if [ -z "$LNETLND" ]; then
84                 case $NETTYPE in
85                 o2ib*)  LNETLND="o2iblnd/ko2iblnd" ;;
86                 tcp*)   LNETLND="socklnd/ksocklnd" ;;
87                 *)      local lnd="${NETTYPE%%[0-9]}lnd"
88                         [ -f "$LNDPATH/$lnd/k$lnd.ko" ] &&
89                                 LNETLND="$lnd/k$lnd" ||
90                                 LNETLND="socklnd/ksocklnd"
91                 esac
92         fi
93         load_module ../lnet/klnds/$LNETLND
94 }
95
96 do_lnetctl() {
97         echo "$LNETCTL $@"
98         $LNETCTL "$@"
99 }
100
101 TESTNS='test_ns'
102 FAKE_IF="test1pg"
103 FAKE_IP="10.1.2.3"
104 do_ns() {
105         echo "ip netns exec $TESTNS $@"
106         ip netns exec $TESTNS "$@"
107 }
108
109 setup_fakeif() {
110         local netns="$1"
111
112         local netns_arg=""
113         [[ -n $netns ]] &&
114                 netns_arg="netns $netns"
115
116         ip link add 'test1pl' type veth peer name $FAKE_IF $netns_arg
117         ip link set 'test1pl' up
118         if [[ -n $netns ]]; then
119                 do_ns ip addr add "${FAKE_IP}/31" dev $FAKE_IF
120                 do_ns ip link set $FAKE_IF up
121         else
122                 ip addr add "${FAKE_IP}/31" dev $FAKE_IF
123                 ip link set $FAKE_IF up
124         fi
125 }
126
127 cleanup_fakeif() {
128         ip link show test1pl >& /dev/null && ip link del test1pl || return 0
129 }
130
131 setup_netns() {
132         cleanup_netns
133
134         ip netns add $TESTNS
135         setup_fakeif $TESTNS
136 }
137
138 cleanup_netns() {
139         (ip netns list | grep -q $TESTNS) && ip netns del $TESTNS
140         cleanup_fakeif
141 }
142
143 configure_dlc() {
144         echo "Loading LNet and configuring DLC"
145         load_lnet
146         do_lnetctl lnet configure
147 }
148
149 GLOBAL_YAML_FILE=$TMP/sanity-lnet-global.yaml
150 define_global_yaml() {
151         $LNETCTL export --backup >${GLOBAL_YAML_FILE} ||
152                 error "Failed to export global yaml $?"
153 }
154
155 reinit_dlc() {
156         if lsmod | grep -q lnet; then
157                 do_lnetctl lnet unconfigure ||
158                         error "lnetctl lnet unconfigure failed $?"
159                 do_lnetctl lnet configure ||
160                         error "lnetctl lnet configure failed $?"
161         else
162                 configure_dlc || error "configure_dlc failed $?"
163         fi
164         define_global_yaml
165 }
166
167 append_global_yaml() {
168         [[ ! -e ${GLOBAL_YAML_FILE} ]] &&
169                 error "Missing global yaml at ${GLOBAL_YAML_FILE}"
170
171         cat ${GLOBAL_YAML_FILE} >> $TMP/sanity-lnet-$testnum-expected.yaml
172 }
173
174 create_base_yaml_file() {
175         append_global_yaml
176 }
177
178 compare_yaml_files() {
179         local expected="$TMP/sanity-lnet-$testnum-expected.yaml"
180         local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
181         local rc=0
182         ! [[ -e $expected ]] && echo "$expected not found" && return 1
183         ! [[ -e $actual ]] && echo "$actual not found" && return 1
184         diff -upN ${actual} ${expected} || rc=$?
185         echo "Expected:"
186         cat $expected
187         echo "Actual:"
188         cat $actual
189         return $rc
190 }
191
192 validate_nid() {
193         local nid="$1"
194         local net="${nid//*@/}"
195         local addr="${nid//@*/}"
196
197         local num_re='[0-9]\+'
198         local ip_re="[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
199
200         if [[ $net =~ gni[0-9]* ]]; then
201                 [[ $addr =~ ${num_re} ]] && return 0
202         else
203                 [[ $addr =~ ${ip_re} ]] && return 0
204         fi
205 }
206
207 validate_nids() {
208         local yfile=$TMP/sanity-lnet-$testnum-actual.yaml
209         local primary_nids=$(awk '/- primary nid:/{print $NF}' $yfile | xargs echo)
210         local secondary_nids=$(awk '/- nid:/{print $NF}' $yfile | xargs echo)
211         local gateway_nids=$(awk '/gateway:/{print $NF}' $yfile | xargs echo)
212
213         local nid
214         for nid in $primary_nids $secondary_nids; do
215                 validate_nid "$nid" || error "Bad NID \"${nid}\""
216         done
217         return 0
218 }
219
220 validate_peer_nids() {
221         local num_peers="$1"
222         local nids_per_peer="$2"
223
224         local expect_p="$num_peers"
225         # The primary nid also shows up in the list of secondary nids
226         local expect_s="$(($num_peers + $(($nids_per_peer*$num_peers))))"
227
228         local actual_p=$(grep -c -- '- primary nid:' $TMP/sanity-lnet-$testnum-actual.yaml)
229         local actual_s=$(grep -c -- '- nid:' $TMP/sanity-lnet-$testnum-actual.yaml)
230         if [[ $expect_p -ne $actual_p ]]; then
231                 compare_yaml_files
232                 error "Expected $expect_p but found $actual_p primary nids"
233         elif [[ $expect_s -ne $actual_s ]]; then
234                 compare_yaml_files
235                 error "Expected $expect_s but found $actual_s secondary nids"
236         fi
237         validate_nids
238 }
239
240 validate_gateway_nids() {
241         local expect_gw=$(grep -c -- 'gateway:' $TMP/sanity-lnet-$testnum-expected.yaml)
242         local actual_gw=$(grep -c -- 'gateway:' $TMP/sanity-lnet-$testnum-actual.yaml)
243         if [[ $expect_gw -ne $actual_gw ]]; then
244                 compare_yaml_files
245                 error "Expected $expect_gw gateways but found $actual_gw gateways"
246         fi
247         validate_nids
248 }
249
250 cleanupall -f
251 setup_netns || error "setup_netns failed with $?"
252
253 stack_trap 'cleanup_testsuite' EXIT
254
255 test_0() {
256         load_module ../lnet/lnet/lnet || error "Failed to load module rc = $?"
257         do_lnetctl lnet configure || error "lnet configure failed rc = $?"
258         define_global_yaml
259         reinit_dlc || return $?
260         do_lnetctl import <  ${GLOBAL_YAML_FILE} || error "Import failed $?"
261         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
262         create_base_yaml_file
263         compare_yaml_files || error "Configuration changed after import"
264 }
265 run_test 0 "Export empty config, import the config, compare"
266
267 compare_peer_add() {
268         local prim_nid="${1:+--prim_nid $1}"
269         local nid="${2:+--nid $2}"
270
271         local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
272
273         do_lnetctl peer add ${prim_nid} ${nid} || error "peer add failed $?"
274         $LNETCTL export --backup > $actual || error "export failed $?"
275         compare_yaml_files
276         return $?
277 }
278
279 test_1() {
280         reinit_dlc || return $?
281         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
282 peer:
283     - primary nid: 1.1.1.1@tcp
284       Multi-Rail: True
285       peer ni:
286         - nid: 1.1.1.1@tcp
287 EOF
288         append_global_yaml
289         compare_peer_add "1.1.1.1@tcp"
290 }
291 run_test 1 "Add peer with single nid (tcp)"
292
293 test_2() {
294         reinit_dlc || return $?
295         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
296 peer:
297     - primary nid: 2.2.2.2@o2ib
298       Multi-Rail: True
299       peer ni:
300         - nid: 2.2.2.2@o2ib
301 EOF
302         append_global_yaml
303         compare_peer_add "2.2.2.2@o2ib"
304 }
305 run_test 2 "Add peer with single nid (o2ib)"
306
307 test_3() {
308         reinit_dlc || return $?
309         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
310 peer:
311     - primary nid: 3.3.3.3@tcp
312       Multi-Rail: True
313       peer ni:
314         - nid: 3.3.3.3@tcp
315         - nid: 3.3.3.3@o2ib
316 EOF
317         append_global_yaml
318         compare_peer_add "3.3.3.3@tcp" "3.3.3.3@o2ib"
319 }
320 run_test 3 "Add peer with tcp primary o2ib secondary"
321
322 test_4() {
323         reinit_dlc || return $?
324         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
325 peer:
326     - primary nid: 4.4.4.4@tcp
327       Multi-Rail: True
328       peer ni:
329         - nid: 4.4.4.4@tcp
330         - nid: 4.4.4.1@tcp
331         - nid: 4.4.4.2@tcp
332         - nid: 4.4.4.3@tcp
333 EOF
334         append_global_yaml
335         echo "Add peer with nidrange (tcp)"
336         compare_peer_add "4.4.4.4@tcp" "4.4.4.[1-3]@tcp"
337
338         echo "Add peer with nidrange that overlaps primary nid (tcp)"
339         compare_peer_add "4.4.4.4@tcp" "4.4.4.[1-4]@tcp"
340 }
341 run_test 4 "Add peer with nidrange (tcp)"
342
343 test_5() {
344         reinit_dlc || return $?
345         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
346 peer:
347     - primary nid: 5.5.5.5@o2ib
348       Multi-Rail: True
349       peer ni:
350         - nid: 5.5.5.5@o2ib
351         - nid: 5.5.5.1@o2ib
352         - nid: 5.5.5.2@o2ib
353         - nid: 5.5.5.3@o2ib
354         - nid: 5.5.5.4@o2ib
355 EOF
356         append_global_yaml
357         echo "Add peer with nidrange (o2ib)"
358         compare_peer_add "5.5.5.5@o2ib" "5.5.5.[1-4]@o2ib"
359
360         echo "Add peer with nidranage that overlaps primary nid (o2ib)"
361         compare_peer_add "5.5.5.5@o2ib" "5.5.5.[1-4]@o2ib"
362 }
363 run_test 5 "Add peer with nidrange (o2ib)"
364
365 test_6() {
366         reinit_dlc || return $?
367         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
368 peer:
369     - primary nid: 6.6.6.6@tcp
370       Multi-Rail: True
371       peer ni:
372         - nid: 6.6.6.6@tcp
373         - nid: 6.6.6.0@tcp
374         - nid: 6.6.6.2@tcp
375         - nid: 6.6.6.4@tcp
376         - nid: 6.6.7.0@tcp
377         - nid: 6.6.7.2@tcp
378         - nid: 6.6.7.4@tcp
379         - nid: 6.6.1.0@o2ib
380         - nid: 6.6.1.3@o2ib
381         - nid: 6.6.1.6@o2ib
382         - nid: 6.6.3.0@o2ib
383         - nid: 6.6.3.3@o2ib
384         - nid: 6.6.3.6@o2ib
385         - nid: 6@gni
386         - nid: 10@gni
387 EOF
388         append_global_yaml
389         compare_peer_add "6.6.6.6@tcp" \
390                 "6.6.[6-7].[0-4/2]@tcp,6.6.[1-4/2].[0-6/3]@o2ib,[6-12/4]@gni"
391 }
392 run_test 6 "Add peer with multiple nidranges"
393
394 compare_peer_del() {
395         local prim_nid="${1:+--prim_nid $1}"
396         local nid="${2:+--nid $2}"
397
398         local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
399
400         do_lnetctl peer del ${prim_nid} ${nid} || error "peer del failed $?"
401         $LNETCTL export --backup > $actual || error "export failed $?"
402         compare_yaml_files
403         return $?
404 }
405
406 test_7() {
407         reinit_dlc || return $?
408         create_base_yaml_file
409
410         echo "Delete peer with single nid (tcp)"
411         do_lnetctl peer add --prim_nid 7.7.7.7@tcp || error "Peer add failed $?"
412         compare_peer_del "7.7.7.7@tcp"
413
414         echo "Delete peer with single nid (o2ib)"
415         do_lnetctl peer add --prim_nid 7.7.7.7@o2ib || error "Peer add failed $?"
416         compare_peer_del "7.7.7.7@o2ib"
417
418         echo "Delete peer that has multiple nids (tcp)"
419         do_lnetctl peer add --prim_nid 7.7.7.7@tcp --nid 7.7.7.[8-12]@tcp ||
420                 error "Peer add failed $?"
421         compare_peer_del "7.7.7.7@tcp"
422
423         echo "Delete peer that has multiple nids (o2ib)"
424         do_lnetctl peer add --prim_nid 7.7.7.7@o2ib --nid 7.7.7.[8-12]@o2ib ||
425                 error "Peer add failed $?"
426         compare_peer_del "7.7.7.7@o2ib"
427
428         echo "Delete peer that has both tcp and o2ib nids"
429         do_lnetctl peer add --prim_nid 7.7.7.7@tcp \
430                 --nid 7.7.7.[9-12]@tcp,7.7.7.[13-15]@o2ib ||
431                 error "Peer add failed $?"
432         compare_peer_del "7.7.7.7@tcp"
433
434         echo "Delete peer with single nid (gni)"
435         do_lnetctl peer add --prim_nid 7@gni || error "Peer add failed $?"
436         compare_peer_del "7@gni"
437
438         echo "Delete peer that has multiple nids (gni)"
439         do_lnetctl peer add --prim_nid 7@gni --nid [8-12]@gni ||
440                 error "Peer add failed $?"
441         compare_peer_del "7@gni"
442
443         echo "Delete peer that has tcp, o2ib and gni nids"
444         do_lnetctl peer add --prim_nid 7@gni \
445                 --nid [8-12]@gni,7.7.7.[9-12]@tcp,7.7.7.[13-15]@o2ib ||
446                 error "Peer add failed $?"
447         compare_peer_del "7@gni"
448 }
449 run_test 7 "Various peer delete tests"
450
451 test_8() {
452         reinit_dlc || return $?
453
454         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
455 peer:
456     - primary nid: 8.8.8.8@tcp
457       Multi-Rail: True
458       peer ni:
459         - nid: 8.8.8.8@tcp
460         - nid: 8.8.8.10@tcp
461         - nid: 8.8.8.11@tcp
462         - nid: 8.8.8.12@tcp
463         - nid: 8.8.8.14@tcp
464         - nid: 8.8.8.15@tcp
465 EOF
466         append_global_yaml
467
468         do_lnetctl peer add --prim_nid 8.8.8.8@tcp --nid 8.8.8.[10-15]@tcp ||
469                 error "Peer add failed $?"
470         compare_peer_del "8.8.8.8@tcp" "8.8.8.13@tcp"
471 }
472 run_test 8 "Delete single secondary nid from peer (tcp)"
473
474 test_9() {
475         reinit_dlc || return $?
476
477         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
478 peer:
479     - primary nid: 9.9.9.9@tcp
480       Multi-Rail: True
481       peer ni:
482         - nid: 9.9.9.9@tcp
483 EOF
484         append_global_yaml
485
486         do_lnetctl peer add --prim_nid 9.9.9.9@tcp \
487                 --nid 9.9.9.[11-16]@tcp || error "Peer add failed $?"
488         compare_peer_del "9.9.9.9@tcp" "9.9.9.[11-16]@tcp"
489 }
490 run_test 9 "Delete all secondary nids from peer (tcp)"
491
492 test_10() {
493         reinit_dlc || return $?
494
495         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
496 peer:
497     - primary nid: 10.10.10.10@tcp
498       Multi-Rail: True
499       peer ni:
500         - nid: 10.10.10.10@tcp
501         - nid: 10.10.10.12@tcp
502         - nid: 10.10.10.13@tcp
503         - nid: 10.10.10.15@tcp
504         - nid: 10.10.10.16@tcp
505 EOF
506         append_global_yaml
507         do_lnetctl peer add --prim_nid 10.10.10.10@tcp \
508                 --nid 10.10.10.[12-16]@tcp || error "Peer add failed $?"
509         compare_peer_del "10.10.10.10@tcp" "10.10.10.14@tcp"
510 }
511 run_test 10 "Delete single secondary nid from peer (o2ib)"
512
513 test_11() {
514         reinit_dlc || return $?
515
516         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
517 peer:
518     - primary nid: 11.11.11.11@tcp
519       Multi-Rail: True
520       peer ni:
521         - nid: 11.11.11.11@tcp
522 EOF
523         append_global_yaml
524         do_lnetctl peer add --prim_nid 11.11.11.11@tcp \
525                 --nid 11.11.11.[13-17]@tcp || error "Peer add failed $?"
526         compare_peer_del "11.11.11.11@tcp" "11.11.11.[13-17]@tcp"
527 }
528 run_test 11 "Delete all secondary nids from peer (o2ib)"
529
530 test_12() {
531         reinit_dlc || return $?
532
533         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
534 peer:
535     - primary nid: 12.12.12.12@o2ib
536       Multi-Rail: True
537       peer ni:
538         - nid: 12.12.12.12@o2ib
539         - nid: 13.13.13.13@o2ib
540         - nid: 14.13.13.13@o2ib
541         - nid: 14.15.13.13@o2ib
542         - nid: 15.17.1.5@tcp
543         - nid: 15.17.1.10@tcp
544         - nid: 15.17.1.20@tcp
545 EOF
546         append_global_yaml
547         do_lnetctl peer add --prim_nid 12.12.12.12@o2ib \
548                 --nid [13-14/1].[13-15/2].13.13@o2ib,[15-16/3].[17-19/4].[1].[5-20/5]@tcp ||
549                 error "Peer add failed $?"
550         compare_peer_del "12.12.12.12@o2ib" "13.15.13.13@o2ib,15.17.1.15@tcp"
551 }
552 run_test 12 "Delete a secondary nid from peer (tcp and o2ib)"
553
554 test_13() {
555         reinit_dlc || return $?
556
557         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
558 peer:
559     - primary nid: 13.13.13.13@o2ib
560       Multi-Rail: True
561       peer ni:
562         - nid: 13.13.13.13@o2ib
563 EOF
564         append_global_yaml
565         do_lnetctl peer add --prim_nid 13.13.13.13@o2ib \
566                 --nid [14-15].[1-2/1].[1].[100-254/10]@tcp,14.14.[254].14@o2ib ||
567                 error "Peer add failed $?"
568         compare_peer_del "13.13.13.13@o2ib" \
569                 "[14-15].[1-2/1].[1].[100-254/10]@tcp,14.14.[254].14@o2ib"
570 }
571 run_test 13 "Delete all secondary nids from peer (tcp and o2ib)"
572
573 create_nid() {
574         local num="$1"
575         local net="$2"
576
577         if [[ $net =~ gni* ]]; then
578                 echo "${num}@${net}"
579         else
580                 echo "${num}.${num}.${num}.${num}@${net}"
581         fi
582 }
583
584 create_mr_peer_yaml() {
585         local num_peers="$1"
586         local secondary_nids="$2"
587         local net="$3"
588
589         echo "Generating peer yaml for $num_peers peers with $secondary_nids secondary nids"
590         echo "peer:" >> $TMP/sanity-lnet-$testnum-expected.yaml
591         local i
592         local total_nids=$((num_peers + $((num_peers * secondary_nids))))
593         local created=0
594         local nidnum=1
595         while [[ $created -lt $num_peers ]]; do
596                 local primary=$(create_nid ${nidnum} ${net})
597         cat <<EOF >> $TMP/sanity-lnet-$testnum-expected.yaml
598     - primary nid: $primary
599       Multi-Rail: True
600       peer ni:
601         - nid: $primary
602 EOF
603                 local j
604                 local start=$((nidnum + 1))
605                 local end=$((nidnum + $secondary_nids))
606                 for j in $(seq ${start} ${end}); do
607                         local nid=$(create_nid $j ${net})
608                         echo "        - nid: $nid" >> $TMP/sanity-lnet-$testnum-expected.yaml
609                 done
610                 nidnum=$((end + 1))
611                 ((created++))
612         done
613 }
614
615 test_14() {
616         reinit_dlc || return $?
617
618         echo "Create single peer, single nid, using import"
619         create_mr_peer_yaml 1 0 tcp
620         do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
621                 error "Import failed $?"
622         append_global_yaml
623         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
624         compare_yaml_files
625
626         echo "Delete single peer using import --del"
627         do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml ||
628                 error "Import failed $?"
629         rm -f $TMP/sanity-lnet-$testnum-expected.yaml
630         create_base_yaml_file
631         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
632         compare_yaml_files
633 }
634 run_test 14 "import peer create/delete with single nid"
635
636 test_15() {
637         reinit_dlc || return $?
638
639         echo "Create multiple peers, single nid per peer, using import"
640         create_mr_peer_yaml 5 0 o2ib
641         # The ordering of nids for this use-case is non-deterministic, so we
642         # we can't just diff the expected/actual output.
643         do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
644                 error "Import failed $?"
645         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
646         validate_peer_nids 5 0
647
648         echo "Delete multiple peers, single nid per peer, using import --del"
649         do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml ||
650                 error "Import failed $?"
651         rm -f $TMP/sanity-lnet-$testnum-expected.yaml
652         create_base_yaml_file
653         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
654         compare_yaml_files
655 }
656 run_test 15 "import multi peer create/delete with single nid per peer"
657
658 test_16() {
659         reinit_dlc || return $?
660
661         echo "Create single peer, multiple nids, using import"
662         create_mr_peer_yaml 1 5 tcp
663         do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
664                 error "Import failed $?"
665         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
666         validate_peer_nids 1 5
667
668         echo "Delete single peer, multiple nids, using import --del"
669         do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml ||
670                 error "Import failed $?"
671         rm -f $TMP/sanity-lnet-$testnum-expected.yaml
672         create_base_yaml_file
673         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
674         compare_yaml_files
675 }
676 run_test 16 "import peer create/delete with multiple nids"
677
678 test_17() {
679         reinit_dlc || return $?
680
681         echo "Create multiple peers, multiple nids per peer, using import"
682         create_mr_peer_yaml 5 7 o2ib
683         do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
684                 error "Import failed $?"
685         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
686         validate_peer_nids 5 7
687
688         echo "Delete multiple peers, multiple nids per peer, using import --del"
689         do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml ||
690                 error "Import failed $?"
691         rm -f $TMP/sanity-lnet-$testnum-expected.yaml
692         create_base_yaml_file
693         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
694         compare_yaml_files
695 }
696 run_test 17 "import multi peer create/delete with multiple nids"
697
698 test_18a() {
699         reinit_dlc || return $?
700
701         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
702 peer:
703     - primary nid: 1.1.1.1@tcp
704       Multi-Rail: True
705       peer ni:
706         - nid: 1.1.1.1@tcp
707         - nid: 2.2.2.2@tcp
708         - nid: 4.4.4.4@tcp
709         - nid: 3.3.3.3@o2ib
710         - nid: 5@gni
711 EOF
712         echo "Import peer with 5 nids"
713         cat $TMP/sanity-lnet-$testnum-expected.yaml
714         do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
715                 error "Import failed $?"
716         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
717 peer:
718     - primary nid: 1.1.1.1@tcp
719       Multi-Rail: True
720       peer ni:
721         - nid: 2.2.2.2@tcp
722         - nid: 3.3.3.3@o2ib
723         - nid: 5@gni
724 EOF
725         echo "Delete three of the nids"
726         cat $TMP/sanity-lnet-$testnum-expected.yaml
727         do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml
728         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
729 peer:
730     - primary nid: 1.1.1.1@tcp
731       Multi-Rail: True
732       peer ni:
733         - nid: 1.1.1.1@tcp
734         - nid: 4.4.4.4@tcp
735 EOF
736         echo "Check peer has expected nids remaining"
737         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
738         append_global_yaml
739         compare_yaml_files
740 }
741 run_test 18a "Delete a subset of nids from a single peer using import --del"
742
743 test_18b() {
744         reinit_dlc || return $?
745
746         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
747 peer:
748     - primary nid: 1.1.1.1@tcp
749       Multi-Rail: True
750       peer ni:
751         - nid: 1.1.1.1@tcp
752         - nid: 2.2.2.2@tcp
753         - nid: 4.4.4.4@tcp
754         - nid: 3.3.3.3@o2ib
755         - nid: 5@gni
756     - primary nid: 6.6.6.6@o2ib
757       Multi-Rail: True
758       peer ni:
759         - nid: 6.6.6.6@o2ib
760         - nid: 7.7.7.7@tcp
761         - nid: 8.8.8.8@tcp
762         - nid: 9.9.9.9@tcp
763         - nid: 10@gni
764 EOF
765         echo "Import two peers with 5 nids each"
766         cat $TMP/sanity-lnet-$testnum-expected.yaml
767         do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
768                 error "Import failed $?"
769         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
770 peer:
771     - primary nid: 1.1.1.1@tcp
772       Multi-Rail: True
773       peer ni:
774         - nid: 2.2.2.2@tcp
775         - nid: 3.3.3.3@o2ib
776         - nid: 5@gni
777     - primary nid: 6.6.6.6@o2ib
778       Multi-Rail: True
779       peer ni:
780         - nid: 7.7.7.7@tcp
781         - nid: 8.8.8.8@tcp
782         - nid: 10@gni
783 EOF
784         echo "Delete three of the nids from each peer"
785         cat $TMP/sanity-lnet-$testnum-expected.yaml
786         do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml
787         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
788 peer:
789     - primary nid: 6.6.6.6@o2ib
790       Multi-Rail: True
791       peer ni:
792         - nid: 6.6.6.6@o2ib
793         - nid: 7.7.7.7@tcp
794     - primary nid: 1.1.1.1@tcp
795       Multi-Rail: True
796       peer ni:
797         - nid: 1.1.1.1@tcp
798         - nid: 4.4.4.4@tcp
799 EOF
800         append_global_yaml
801         echo "Check peers have expected nids remaining"
802         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
803         compare_yaml_files
804         validate_peer_nids 2 1
805 }
806 run_test 18b "Delete multiple nids from multiple peers using import --del"
807
808 test_19() {
809         reinit_dlc || return $?
810         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
811 peer:
812     - primary nid: 19@gni
813       Multi-Rail: True
814       peer ni:
815         - nid: 19@gni
816 EOF
817         append_global_yaml
818         compare_peer_add "19@gni"
819 }
820 run_test 19 "Add peer with single nid (gni)"
821
822 test_20() {
823         reinit_dlc || return $?
824         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
825 peer:
826     - primary nid: 20@gni
827       Multi-Rail: True
828       peer ni:
829         - nid: 20@gni
830         - nid: 20.20.20.20@tcp
831         - nid: 20.20.20.20@o2ib
832 EOF
833         append_global_yaml
834         compare_peer_add "20@gni" "20.20.20.20@tcp,20.20.20.20@o2ib"
835 }
836 run_test 20 "Add peer with gni primary and tcp, o2ib secondary"
837
838 test_21() {
839         reinit_dlc || return $?
840         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
841 peer:
842     - primary nid: 21@gni
843       Multi-Rail: True
844       peer ni:
845         - nid: 21@gni
846         - nid: 22@gni
847         - nid: 23@gni
848         - nid: 24@gni
849         - nid: 25@gni
850 EOF
851         append_global_yaml
852         echo"Add peer with nidrange (gni)"
853         compare_peer_add "21@gni" "[22-25]@gni" || error
854         echo "Add peer with nidrange that overlaps primary nid (gni)"
855         compare_peer_add "21@gni" "[21-25]@gni"
856 }
857 run_test 21 "Add peer with nidrange (gni)"
858
859 test_22() {
860         reinit_dlc || return $?
861         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
862 peer:
863     - primary nid: 22@gni
864       Multi-Rail: True
865       peer ni:
866         - nid: 22@gni
867         - nid: 24@gni
868         - nid: 25@gni
869         - nid: 27@gni
870         - nid: 28@gni
871         - nid: 29@gni
872 EOF
873         append_global_yaml
874         do_lnetctl peer add --prim_nid 22@gni --nid [24-29]@gni ||
875                 error "Peer add failed $?"
876         compare_peer_del "22@gni" "26@gni"
877 }
878 run_test 22 "Delete single secondary nid from peer (gni)"
879
880 test_23() {
881         reinit_dlc || return $?
882         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
883 peer:
884     - primary nid: 23@gni
885       Multi-Rail: True
886       peer ni:
887         - nid: 23@gni
888 EOF
889         append_global_yaml
890
891         do_lnetctl peer add --prim_nid 23@gni --nid [25-29]@gni ||
892                 error "Peer add failed $?"
893         compare_peer_del "23@gni" "[25-29]@gni"
894 }
895 run_test 23 "Delete all secondary nids from peer (gni)"
896
897 test_24() {
898         reinit_dlc || return $?
899         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
900 peer:
901     - primary nid: 24@gni
902       Multi-Rail: True
903       peer ni:
904         - nid: 24@gni
905         - nid: 11@gni
906         - nid: 13.13.13.13@o2ib
907         - nid: 14.13.13.13@o2ib
908         - nid: 14.15.13.13@o2ib
909         - nid: 15.17.1.5@tcp
910         - nid: 15.17.1.10@tcp
911         - nid: 15.17.1.20@tcp
912 EOF
913         append_global_yaml
914         do_lnetctl peer add --prim_nid 24@gni \
915                 --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 ||
916                 error "Peer add failed $?"
917         compare_peer_del "24@gni" "5@gni,13.15.13.13@o2ib,15.17.1.15@tcp"
918 }
919 run_test 24 "Delete a secondary nid from peer (tcp, o2ib and gni)"
920
921 test_25() {
922         reinit_dlc || return $?
923         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
924 peer:
925     - primary nid: 25@gni
926       Multi-Rail: True
927       peer ni:
928         - nid: 25@gni
929 EOF
930         append_global_yaml
931         do_lnetctl peer add --prim_nid 25@gni \
932                 --nid [26-27].[4-10/3].26.26@tcp,26.26.26.26@o2ib,[30-35]@gni ||
933                 error "Peer add failed $?"
934         compare_peer_del "25@gni" \
935                 "[26-27].[4-10/3].26.26@tcp,26.26.26.26@o2ib,[30-35]@gni"
936 }
937 run_test 25 "Delete all secondary nids from peer (tcp, gni and o2ib)"
938
939 test_99a() {
940         reinit_dlc || return $?
941
942         echo "Invalid prim_nid - peer add"
943         do_lnetctl peer add --prim_nid foobar &&
944                 error "Command should have failed"
945
946         echo "Invalid prim_nid - peer del"
947         do_lnetctl peer del --prim_nid foobar &&
948                 error "Command should have failed"
949
950         echo "Delete non-existing peer"
951         do_lnetctl peer del --prim_nid 1.1.1.1@o2ib &&
952                 error "Command should have failed"
953
954         echo "Don't provide mandatory arguments peer del"
955         do_lnetctl peer del --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 --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         have_interface "eth0" || skip "Need eth0 interface with ipv4 configured"
1060         reinit_dlc || return $?
1061         add_net "tcp" "eth0"
1062         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
1063 net:
1064     - net type: tcp
1065       local NI(s):
1066         - interfaces:
1067               0: eth0
1068           tunables:
1069               peer_timeout: 180
1070               peer_credits: 8
1071               peer_buffer_credits: 0
1072               credits: 256
1073 route:
1074     - net: tcp7
1075       gateway: 7.7.7.7@tcp
1076       hop: -1
1077       priority: 0
1078       health_sensitivity: 1
1079 peer:
1080     - primary nid: 7.7.7.7@tcp
1081       Multi-Rail: False
1082       peer ni:
1083         - nid: 7.7.7.7@tcp
1084 EOF
1085         append_global_yaml
1086         compare_route_add "tcp7" "7.7.7.7@tcp" || return $?
1087         compare_yaml_files
1088 }
1089 run_test 100 "Add route with single gw (tcp)"
1090
1091 test_101() {
1092         have_interface "eth0" || skip "Need eth0 interface with ipv4 configured"
1093         reinit_dlc || return $?
1094         add_net "tcp" "eth0"
1095         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
1096 net:
1097     - net type: tcp
1098       local NI(s):
1099         - interfaces:
1100               0: eth0
1101           tunables:
1102               peer_timeout: 180
1103               peer_credits: 8
1104               peer_buffer_credits: 0
1105               credits: 256
1106 route:
1107     - net: tcp8
1108       gateway: 8.8.8.10@tcp
1109       hop: -1
1110       priority: 0
1111       health_sensitivity: 1
1112     - net: tcp8
1113       gateway: 8.8.8.9@tcp
1114       hop: -1
1115       priority: 0
1116       health_sensitivity: 1
1117     - net: tcp8
1118       gateway: 8.8.8.8@tcp
1119       hop: -1
1120       priority: 0
1121       health_sensitivity: 1
1122 peer:
1123     - primary nid: 8.8.8.9@tcp
1124       Multi-Rail: False
1125       peer ni:
1126         - nid: 8.8.8.9@tcp
1127     - primary nid: 8.8.8.10@tcp
1128       Multi-Rail: False
1129       peer ni:
1130         - nid: 8.8.8.10@tcp
1131     - primary nid: 8.8.8.8@tcp
1132       Multi-Rail: False
1133       peer ni:
1134         - nid: 8.8.8.8@tcp
1135 EOF
1136         append_global_yaml
1137         compare_route_add "tcp8" "8.8.8.[8-10]@tcp"
1138 }
1139 run_test 101 "Add route with multiple gw (tcp)"
1140
1141 compare_route_del() {
1142         local rnet="$1"
1143         local gw="$2"
1144
1145         local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
1146
1147         do_lnetctl route del --net ${rnet} --gateway ${gw} ||
1148                 error "route del failed $?"
1149         $LNETCTL export --backup > $actual ||
1150                 error "export failed $?"
1151         validate_gateway_nids
1152 }
1153
1154 test_102() {
1155         have_interface "eth0" || skip "Need eth0 interface with ipv4 configured"
1156         reinit_dlc || return $?
1157         add_net "tcp" "eth0"
1158         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml
1159         do_lnetctl route add --net tcp102 --gateway 102.102.102.102@tcp ||
1160                 error "route add failed $?"
1161         compare_route_del "tcp102" "102.102.102.102@tcp"
1162 }
1163 run_test 102 "Delete route with single gw (tcp)"
1164
1165 test_103() {
1166         have_interface "eth0" || skip "Need eth0 interface with ipv4 configured"
1167         reinit_dlc || return $?
1168         add_net "tcp" "eth0"
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 ### load lnet in default namespace, configure in target namespace
1178
1179 test_200() {
1180         cleanup_lnet || exit 1
1181         load_lnet "networks=\"\""
1182         do_ns $LNETCTL lnet configure --all || exit 1
1183         $LNETCTL net show --net tcp | grep -q "nid: ${FAKE_IP}@tcp$"
1184 }
1185 run_test 200 "load lnet w/o module option, configure in a non-default namespace"
1186
1187 test_201() {
1188         cleanup_lnet || exit 1
1189         load_lnet "networks=tcp($FAKE_IF)"
1190         do_ns $LNETCTL lnet configure --all || exit 1
1191         $LNETCTL net show --net tcp | grep -q "nid: ${FAKE_IP}@tcp$"
1192 }
1193 run_test 201 "load lnet using networks module options in a non-default namespace"
1194
1195 test_202() {
1196         cleanup_lnet || exit 1
1197         load_lnet "networks=\"\" ip2nets=\"tcp0($FAKE_IF) ${FAKE_IP}\""
1198         do_ns $LNETCTL lnet configure --all || exit 1
1199         $LNETCTL net show | grep -q "nid: ${FAKE_IP}@tcp$"
1200 }
1201 run_test 202 "load lnet using ip2nets in a non-default namespace"
1202
1203
1204 ### Add the interfaces in the target namespace
1205
1206 test_203() {
1207         cleanup_lnet || exit 1
1208         load_lnet
1209         do_lnetctl lnet configure || exit 1
1210         do_ns $LNETCTL net add --net tcp0 --if $FAKE_IF
1211 }
1212 run_test 203 "add a network using an interface in the non-default namespace"
1213
1214 LNET_PARAMS_FILE="$TMP/$TESTSUITE.parameters"
1215 function save_lnet_params() {
1216         $LNETCTL global show | egrep -v '^global:$' |
1217                                sed 's/://' > $LNET_PARAMS_FILE
1218 }
1219
1220 function restore_lnet_params() {
1221         local param value
1222         while read param value; do
1223                 [[ $param == max_intf ]] && continue
1224                 [[ $param == lnd_timeout ]] && continue
1225                 $LNETCTL set ${param} ${value} ||
1226                         error "Failed to restore ${param} to ${value}"
1227         done < $LNET_PARAMS_FILE
1228 }
1229
1230 function lnet_health_pre() {
1231         save_lnet_params
1232
1233         # Lower transaction timeout to speed up test execution
1234         $LNETCTL set transaction_timeout 10 ||
1235                 error "Failed to set transaction_timeout $?"
1236
1237         # Increase recovery interval so we have time to capture health values
1238         $LNETCTL set recovery_interval 20 ||
1239                 error "Failed to set recovery_interval $?"
1240
1241         RETRY_PARAM=$($LNETCTL global show | awk '/retry_count/{print $NF}')
1242         RSND_PRE=$($LNETCTL stats show | awk '/resend_count/{print $NF}')
1243         LO_HVAL_PRE=$($LNETCTL net show -v 2 | awk '/health value/{print $NF}' |
1244                       xargs echo | sed 's/ /+/g' | bc -l)
1245
1246         local my_nid=$($LCTL list_nids | head -n 1)
1247
1248         RMT_HVAL_PRE=$($LNETCTL peer show --nid $my_nid -v 2 2>/dev/null |
1249                        awk '/health value/{print $NF}' | xargs echo |
1250                        sed 's/ /+/g' | bc -l)
1251
1252         # Might not have any peers so initialize to zero.
1253         RMT_HVAL_PRE=${RMT_HVAL_PRE:-0}
1254
1255         return 0
1256 }
1257
1258 function lnet_health_post() {
1259         RSND_POST=$($LNETCTL stats show | awk '/resend_count/{print $NF}')
1260         LO_HVAL_POST=$($LNETCTL net show -v 2 |
1261                        awk '/health value/{print $NF}' |
1262                        xargs echo | sed 's/ /+/g' | bc -l)
1263
1264         local my_nid=$($LCTL list_nids | head -n 1)
1265
1266         RMT_HVAL_POST=$($LNETCTL peer show --nid $my_nid -v 2 2>/dev/null |
1267                         awk '/health value/{print $NF}' | xargs echo |
1268                         sed 's/ /+/g' | bc -l)
1269
1270         # Might not have any peers so initialize to zero.
1271         RMT_HVAL_POST=${RMT_HVAL_POST:-0}
1272
1273         ${VERBOSE} &&
1274         echo "Pre resends: $RSND_PRE" &&
1275         echo "Post resends: $RSND_POST" &&
1276         echo "Resends delta: $((RSND_POST - RSND_PRE))" &&
1277         echo "Pre local health: $LO_HVAL_PRE" &&
1278         echo "Post local health: $LO_HVAL_POST" &&
1279         echo "Pre remote health: $RMT_HVAL_PRE" &&
1280         echo "Post remote health: $RMT_HVAL_POST"
1281
1282         restore_lnet_params
1283
1284         return 0
1285 }
1286
1287 function check_no_resends() {
1288         echo "Check that no resends took place"
1289         [[ $RSND_POST -ne $RSND_PRE ]] &&
1290                 error "Found resends: $RSND_POST != $RSND_PRE"
1291
1292         return 0
1293 }
1294
1295 function check_resends() {
1296         local delta=$((RSND_POST - RSND_PRE))
1297
1298         echo "Check that $RETRY_PARAM resends took place"
1299         [[ $delta -ne $RETRY_PARAM ]] &&
1300                 error "Expected $RETRY_PARAM resends found $delta"
1301
1302         return 0
1303 }
1304
1305 function check_no_local_health() {
1306         echo "Check that local NI health is unchanged"
1307         [[ $LO_HVAL_POST -ne $LO_HVAL_PRE ]] &&
1308                 error "Local health changed: $LO_HVAL_POST != $LO_HVAL_PRE"
1309
1310         return 0
1311 }
1312
1313 function check_local_health() {
1314         echo "Check that local NI health has been changed"
1315         [[ $LO_HVAL_POST -eq $LO_HVAL_PRE ]] &&
1316                 error "Local health unchanged: $LO_HVAL_POST == $LO_HVAL_PRE"
1317
1318         return 0
1319 }
1320
1321 function check_no_remote_health() {
1322         echo "Check that remote NI health is unchanged"
1323         [[ $RMT_HVAL_POST -ne $RMT_HVAL_PRE ]] &&
1324                 error "Remote health changed: $RMT_HVAL_POST != $RMT_HVAL_PRE"
1325
1326         return 0
1327 }
1328
1329 function check_remote_health() {
1330         echo "Check that remote NI health has been changed"
1331         [[ $RMT_HVAL_POST -eq $RMT_HVAL_PRE ]] &&
1332                 error "Remote health unchanged: $RMT_HVAL_POST == $RMT_HVAL_PRE"
1333
1334         return 0
1335 }
1336
1337 # See lnet/lnet/lib-msg.c:lnet_health_check()
1338 LNET_LOCAL_RESEND_STATUSES="local_interrupt local_dropped local_aborted"
1339 LNET_LOCAL_RESEND_STATUSES+=" local_no_route local_timeout"
1340 LNET_LOCAL_NO_RESEND_STATUSES="local_error"
1341 test_204() {
1342         have_interface "eth0" || skip "Need eth0 interface with ipv4 configured"
1343         reinit_dlc || return $?
1344         add_net "tcp" "eth0" || return $?
1345
1346         lnet_health_pre || return $?
1347
1348         local hstatus
1349         for hstatus in ${LNET_LOCAL_RESEND_STATUSES} \
1350                        ${LNET_LOCAL_NO_RESEND_STATUSES}; do
1351                 echo "Simulate $hstatus"
1352                 $LCTL net_drop_add -s *@tcp -d *@tcp -m GET -r 1 -e ${hstatus}
1353                 do_lnetctl discover $($LCTL list_nids | head -n 1) &&
1354                         error "Should have failed"
1355                 $LCTL net_drop_del *
1356         done
1357
1358         lnet_health_post
1359
1360         check_no_resends || return $?
1361         check_no_local_health || return $?
1362
1363         return 0
1364 }
1365 run_test 204 "Check no health or resends for single-rail local failures"
1366
1367 test_205() {
1368         have_interface "eth0" || skip "Need eth0 interface with ipv4 configured"
1369
1370         local hstatus
1371         for hstatus in ${LNET_LOCAL_RESEND_STATUSES}; do
1372                 reinit_dlc || return $?
1373                 add_net "tcp" "eth0" || return $?
1374                 add_net "tcp1" "eth0" || return $?
1375
1376                 echo "Simulate $hstatus"
1377                 lnet_health_pre
1378
1379                 $LCTL net_drop_add -s *@tcp -d *@tcp -m GET -r 1 -e ${hstatus}
1380                 $LCTL net_drop_add -s *@tcp1 -d *@tcp1 -m GET -r 1 -e ${hstatus}
1381                 do_lnetctl discover $($LCTL list_nids | head -n 1) &&
1382                         error "Should have failed"
1383                 $LCTL net_drop_del *
1384
1385                 lnet_health_post
1386
1387                 check_resends || return $?
1388                 check_local_health || return $?
1389         done
1390
1391         for hstatus in ${LNET_LOCAL_NO_RESEND_STATUSES}; do
1392                 reinit_dlc || return $?
1393                 add_net "tcp" "eth0" || return $?
1394                 add_net "tcp1" "eth0" || return $?
1395
1396                 echo "Simulate $hstatus"
1397                 lnet_health_pre || return $?
1398
1399                 $LCTL net_drop_add -s *@tcp -d *@tcp -m GET -r 1 -e ${hstatus}
1400                 $LCTL net_drop_add -s *@tcp1 -d *@tcp1 -m GET -r 1 -e ${hstatus}
1401                 do_lnetctl discover $($LCTL list_nids | head -n 1) &&
1402                         error "Should have failed"
1403                 $LCTL net_drop_del *
1404
1405                 lnet_health_post
1406
1407                 check_no_resends || return $?
1408                 check_local_health || return $?
1409         done
1410
1411         return 0
1412 }
1413 run_test 205 "Check health and resends for multi-rail local failures"
1414
1415 # See lnet/lnet/lib-msg.c:lnet_health_check()
1416 LNET_REMOTE_RESEND_STATUSES="remote_dropped"
1417 LNET_REMOTE_NO_RESEND_STATUSES="remote_error remote_timeout network_timeout"
1418 test_206() {
1419         have_interface "eth0" || skip "Need eth0 interface with ipv4 configured"
1420         reinit_dlc || return $?
1421         add_net "tcp" "eth0" || return $?
1422
1423         do_lnetctl discover $($LCTL list_nids | head -n 1) ||
1424                 error "failed to discover myself"
1425
1426         lnet_health_pre || return $?
1427
1428         local hstatus
1429         for hstatus in ${LNET_REMOTE_RESEND_STATUSES} \
1430                        ${LNET_REMOTE_NO_RESEND_STATUSES}; do
1431                 echo "Simulate $hstatus"
1432                 $LCTL net_drop_add -s *@tcp -d *@tcp -m GET -r 1 -e ${hstatus}
1433                 do_lnetctl discover $($LCTL list_nids | head -n 1) &&
1434                         error "Should have failed"
1435                 $LCTL net_drop_del *
1436         done
1437
1438         lnet_health_post
1439
1440         check_no_resends || return $?
1441         check_no_local_health || return $?
1442         check_no_remote_health || return $?
1443
1444         return 0
1445 }
1446 run_test 206 "Check no health or resends for single-rail remote failures"
1447
1448 test_207() {
1449         have_interface "eth0" || skip "Need eth0 interface with ipv4 configured"
1450
1451         local hstatus
1452         for hstatus in ${LNET_REMOTE_RESEND_STATUSES}; do
1453                 reinit_dlc || return $?
1454                 add_net "tcp" "eth0" || return $?
1455                 add_net "tcp1" "eth0" || return $?
1456
1457                 do_lnetctl discover $($LCTL list_nids | head -n 1) ||
1458                         error "failed to discover myself"
1459
1460                 echo "Simulate $hstatus"
1461                 lnet_health_pre || return $?
1462                 $LCTL net_drop_add -s *@tcp -d *@tcp -m GET -r 1 -e ${hstatus}
1463                 $LCTL net_drop_add -s *@tcp1 -d *@tcp1 -m GET -r 1 -e ${hstatus}
1464                 do_lnetctl discover $($LCTL list_nids | head -n 1) &&
1465                         error "Should have failed"
1466                 $LCTL net_drop_del *
1467
1468                 lnet_health_post
1469
1470                 check_resends || return $?
1471                 check_no_local_health || return $?
1472                 check_remote_health || return $?
1473         done
1474         for hstatus in ${LNET_REMOTE_NO_RESEND_STATUSES}; do
1475                 reinit_dlc || return $?
1476                 add_net "tcp" "eth0" || return $?
1477                 add_net "tcp1" "eth0" || return $?
1478
1479                 do_lnetctl discover $($LCTL list_nids | head -n 1) ||
1480                         error "failed to discover myself"
1481
1482                 echo "Simulate $hstatus"
1483                 lnet_health_pre || return $?
1484                 $LCTL net_drop_add -s *@tcp -d *@tcp -m GET -r 1 -e ${hstatus}
1485                 $LCTL net_drop_add -s *@tcp1 -d *@tcp1 -m GET -r 1 -e ${hstatus}
1486                 do_lnetctl discover $($LCTL list_nids | head -n 1) &&
1487                         error "Should have failed"
1488                 $LCTL net_drop_del *
1489
1490                 lnet_health_post
1491
1492                 check_no_resends || return $?
1493                 check_no_local_health || return $?
1494                 check_remote_health || return $?
1495         done
1496
1497         return 0
1498 }
1499 run_test 207 "Check health and resends for multi-rail remote errors"
1500
1501 test_208_load_and_check_lnet() {
1502         local ip2nets="$1"
1503         local p_nid="$2"
1504         local s_nid="$3"
1505         local num_expected=1
1506
1507         load_lnet "networks=\"\" ip2nets=\"${ip2nets_str}\""
1508
1509         $LCTL net up ||
1510                 error "Failed to load LNet with ip2nets \"${ip2nets_str}\""
1511
1512         [[ -n $s_nid ]] &&
1513                 num_expected=2
1514
1515         declare -a nids
1516         nids=( $($LCTL list_nids) )
1517
1518         [[ ${#nids[@]} -ne ${num_expected} ]] &&
1519                 error "Expect ${num_expected} NIDs found ${#nids[@]}"
1520
1521         [[ ${nids[0]} == ${p_nid} ]] ||
1522                 error "Expect NID \"${p_nid}\" found \"${nids[0]}\""
1523
1524         [[ -n $s_nid ]] && [[ ${nids[1]} != ${s_nid} ]] &&
1525                 error "Expect second NID \"${s_nid}\" found \"${nids[1]}\""
1526
1527         $LCTL net down &>/dev/null
1528         cleanup_lnet
1529 }
1530
1531 test_208() {
1532         have_interface "eth0" || skip "Need eth0 interface with ipv4 configured"
1533
1534         cleanup_netns || error "Failed to cleanup netns before test execution"
1535         cleanup_lnet || error "Failed to unload modules before test execution"
1536         setup_fakeif || error "Failed to add fake IF"
1537
1538         have_interface "$FAKE_IF" ||
1539                 error "Expect $FAKE_IF configured but not found"
1540
1541         local eth0_ip=$(ip --oneline addr show dev eth0 |
1542                         awk '/inet /{print $4}' |
1543                         sed 's:/.*::')
1544         local ip2nets_str="tcp(eth0) $eth0_ip"
1545
1546         echo "Configure single NID \"$ip2nets_str\""
1547         test_208_load_and_check_lnet "${ip2nets_str}" "${eth0_ip}@tcp"
1548
1549         ip2nets_str="tcp(eth0) $eth0_ip; tcp1($FAKE_IF) $FAKE_IP"
1550         echo "Configure two NIDs; two NETs \"$ip2nets_str\""
1551         test_208_load_and_check_lnet "${ip2nets_str}" "${eth0_ip}@tcp" \
1552                                      "${FAKE_IP}@tcp1"
1553
1554         ip2nets_str="tcp(eth0) $eth0_ip; tcp($FAKE_IF) $FAKE_IP"
1555         echo "Configure two NIDs; one NET \"$ip2nets_str\""
1556         test_208_load_and_check_lnet "${ip2nets_str}" "${eth0_ip}@tcp" \
1557                                      "${FAKE_IP}@tcp"
1558         local addr1=( ${eth0_ip//./ } )
1559         local addr2=( ${FAKE_IP//./ } )
1560         local range="[${addr1[0]},${addr2[0]}]"
1561
1562         local i
1563         for i in $(seq 1 3); do
1564                 range+=".[${addr1[$i]},${addr2[$i]}]"
1565         done
1566         ip2nets_str="tcp(eth0,${FAKE_IF}) ${range}"
1567
1568         echo "Configured two NIDs; one NET alt syntax \"$ip2nets_str\""
1569         test_208_load_and_check_lnet "${ip2nets_str}" "${eth0_ip}@tcp" \
1570                                      "${FAKE_IP}@tcp"
1571
1572         cleanup_fakeif
1573
1574         echo "alt syntax with missing IF \"$ip2nets_str\""
1575         load_lnet "networks=\"\" ip2nets=\"${ip2nets_str}\""
1576
1577         echo "$LCTL net up should fail"
1578         $LCTL net up &&
1579                 error "LNet bringup should have failed"
1580
1581         cleanup_lnet
1582 }
1583 run_test 208 "Test various kernel ip2nets configurations"
1584
1585 test_300() {
1586         # LU-13274
1587         local header
1588         local out=$TMP/$tfile
1589         local prefix=/usr/include/linux/lnet
1590
1591         # We use a hard coded prefix so that this test will not fail
1592         # when run in tree.
1593         CC=${CC:-cc}
1594         if ! which $CC > /dev/null 2>&1; then
1595                 skip_env "$CC is not installed"
1596         fi
1597
1598         cleanup_lnet || exit 1
1599         load_lnet
1600
1601         if ! [[ -d $prefix ]]; then
1602                 # Assume we're running in tree and fixup the include path.
1603                 prefix=$LUSTRE/../lnet/include/uapi/linux/lnet
1604         fi
1605
1606         for header in $prefix/*.h; do
1607                 if ! [[ -f "$header" ]]; then
1608                         continue
1609                 fi
1610
1611                 $CC -Wall -Werror -std=c99 -include $header -c -x c /dev/null -o $out ||
1612                         error "cannot compile '$header'"
1613         done
1614         rm -f $out
1615 }
1616 run_test 300 "packaged LNet UAPI headers can be compiled"
1617
1618 complete $SECONDS
1619
1620 cleanup_testsuite
1621 exit_status