Whamcloud - gitweb
LU-17705 ptlrpc: replace synchronize_rcu() with rcu_barrier()
[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 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 argument for 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 argument for peer add"
959         do_lnetctl peer add --nid 1.1.1.1@tcp &&
960                 error "Command should have failed"
961
962         echo "Don't provide mandatory arguments peer add"
963         do_lnetctl peer add &&
964                 error "Command should have failed"
965
966         echo "Invalid secondary nids"
967         do_lnetctl peer add --prim_nid 1.1.1.1@tcp --nid foobar &&
968                 error "Command should have failed"
969
970         echo "Exceed max nids per peer"
971         do_lnetctl peer add --prim_nid 1.1.1.1@tcp --nid 1.1.1.[2-255]@tcp &&
972                 error "Command should have failed"
973
974         echo "Invalid net type"
975         do_lnetctl peer add --prim_nid 1@foo &&
976                 error "Command should have failed"
977
978         echo "Invalid nid format"
979         local invalid_nids="1@tcp 1@o2ib 1.1.1.1@gni"
980
981         local nid
982         for nid in ${invalid_nids}; do
983                 echo "Check invalid primary nid - '$nid'"
984                 do_lnetctl peer add --prim_nid $nid &&
985                         error "Command should have failed"
986         done
987
988         local invalid_strs="[2-1]@gni [a-f/x]@gni 256.256.256.256@tcp"
989         invalid_strs+=" 1.1.1.1.[2-5/f]@tcp 1.]2[.3.4@o2ib"
990         invalid_strs+="1.[2-4,[5-6],7-8].1.1@tcp foobar"
991
992         local nidstr
993         for nidstr in ${invalid_strs}; do
994                 echo "Check invalid nidstring - '$nidstr'"
995                 do_lnetctl peer add --prim_nid 1.1.1.1@tcp --nid $nidstr &&
996                         error "Command should have failed"
997         done
998
999         echo "Add non-local gateway"
1000         do_lnetctl route add --net tcp --gateway 1@gni &&
1001                 error "Command should have failed"
1002
1003         return 0
1004 }
1005 run_test 99a "Check various invalid inputs to lnetctl peer"
1006
1007 test_99b() {
1008         reinit_dlc || return $?
1009
1010         create_base_yaml_file
1011
1012         cat <<EOF > $TMP/sanity-lnet-$testnum-invalid.yaml
1013 peer:
1014     - primary nid: 99.99.99.99@tcp
1015       Multi-Rail: Foobar
1016       peer ni:
1017         - nid: 99.99.99.99@tcp
1018 EOF
1019         do_lnetctl import < $TMP/sanity-lnet-$testnum-invalid.yaml &&
1020                 error "import should have failed"
1021         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
1022         compare_yaml_files
1023 }
1024 run_test 99b "Invalid value for Multi-Rail in yaml import"
1025
1026 have_interface() {
1027         local if="$1"
1028         local ip=$(ip addr show dev $if | awk '/ inet /{print $2}')
1029         [[ -n $ip ]]
1030 }
1031
1032 add_net() {
1033         local net="$1"
1034         local if="$2"
1035
1036         if ! lsmod | grep -q ksocklnd ; then
1037                 load_module ../lnet/klnds/socklnd/ksocklnd ||
1038                         error "Can't load ksocklnd.ko"
1039         fi
1040
1041         do_lnetctl net add --net ${net} --if ${if} ||
1042                 error "Failed to add net ${net} on if ${if}"
1043 }
1044
1045 compare_route_add() {
1046         local rnet="$1"
1047         local gw="$2"
1048
1049         local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
1050
1051         do_lnetctl route add --net ${rnet} --gateway ${gw} ||
1052                 error "route add failed $?"
1053         # CPT configuration is pruned from the exported yaml, since the default
1054         # can vary across test systems (unlike default values for things like
1055         # peer_credits, peer_timeout, etc.)
1056         $LNETCTL export --backup | grep -v CPT > $actual ||
1057                 error "export failed $?"
1058         validate_gateway_nids
1059         return $?
1060 }
1061
1062 test_100() {
1063         have_interface "eth0" || skip "Need eth0 interface with ipv4 configured"
1064         reinit_dlc || return $?
1065         add_net "tcp" "eth0"
1066         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
1067 net:
1068     - net type: tcp
1069       local NI(s):
1070         - interfaces:
1071               0: eth0
1072           tunables:
1073               peer_timeout: 180
1074               peer_credits: 8
1075               peer_buffer_credits: 0
1076               credits: 256
1077 route:
1078     - net: tcp7
1079       gateway: 7.7.7.7@tcp
1080       hop: -1
1081       priority: 0
1082       health_sensitivity: 1
1083 peer:
1084     - primary nid: 7.7.7.7@tcp
1085       Multi-Rail: False
1086       peer ni:
1087         - nid: 7.7.7.7@tcp
1088 EOF
1089         append_global_yaml
1090         compare_route_add "tcp7" "7.7.7.7@tcp" || return $?
1091         compare_yaml_files
1092 }
1093 run_test 100 "Add route with single gw (tcp)"
1094
1095 test_101() {
1096         have_interface "eth0" || skip "Need eth0 interface with ipv4 configured"
1097         reinit_dlc || return $?
1098         add_net "tcp" "eth0"
1099         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
1100 net:
1101     - net type: tcp
1102       local NI(s):
1103         - interfaces:
1104               0: eth0
1105           tunables:
1106               peer_timeout: 180
1107               peer_credits: 8
1108               peer_buffer_credits: 0
1109               credits: 256
1110 route:
1111     - net: tcp8
1112       gateway: 8.8.8.10@tcp
1113       hop: -1
1114       priority: 0
1115       health_sensitivity: 1
1116     - net: tcp8
1117       gateway: 8.8.8.9@tcp
1118       hop: -1
1119       priority: 0
1120       health_sensitivity: 1
1121     - net: tcp8
1122       gateway: 8.8.8.8@tcp
1123       hop: -1
1124       priority: 0
1125       health_sensitivity: 1
1126 peer:
1127     - primary nid: 8.8.8.9@tcp
1128       Multi-Rail: False
1129       peer ni:
1130         - nid: 8.8.8.9@tcp
1131     - primary nid: 8.8.8.10@tcp
1132       Multi-Rail: False
1133       peer ni:
1134         - nid: 8.8.8.10@tcp
1135     - primary nid: 8.8.8.8@tcp
1136       Multi-Rail: False
1137       peer ni:
1138         - nid: 8.8.8.8@tcp
1139 EOF
1140         append_global_yaml
1141         compare_route_add "tcp8" "8.8.8.[8-10]@tcp"
1142 }
1143 run_test 101 "Add route with multiple gw (tcp)"
1144
1145 compare_route_del() {
1146         local rnet="$1"
1147         local gw="$2"
1148
1149         local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
1150
1151         do_lnetctl route del --net ${rnet} --gateway ${gw} ||
1152                 error "route del failed $?"
1153         $LNETCTL export --backup > $actual ||
1154                 error "export failed $?"
1155         validate_gateway_nids
1156 }
1157
1158 test_102() {
1159         have_interface "eth0" || skip "Need eth0 interface with ipv4 configured"
1160         reinit_dlc || return $?
1161         add_net "tcp" "eth0"
1162         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml
1163         do_lnetctl route add --net tcp102 --gateway 102.102.102.102@tcp ||
1164                 error "route add failed $?"
1165         compare_route_del "tcp102" "102.102.102.102@tcp"
1166 }
1167 run_test 102 "Delete route with single gw (tcp)"
1168
1169 test_103() {
1170         have_interface "eth0" || skip "Need eth0 interface with ipv4 configured"
1171         reinit_dlc || return $?
1172         add_net "tcp" "eth0"
1173         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml
1174         do_lnetctl route add --net tcp103 \
1175                 --gateway 103.103.103.[103-120/4]@tcp ||
1176                 error "route add failed $?"
1177         compare_route_del "tcp103" "103.103.103.[103-120/4]@tcp"
1178 }
1179 run_test 103 "Delete route with multiple gw (tcp)"
1180
1181 test_104() {
1182         local tyaml="$TMP/sanity-lnet-$testnum-expected.yaml"
1183
1184         reinit_dlc || return $?
1185
1186         # Default value is '3'
1187         local val=$($LNETCTL global show | awk '/response_tracking/{print $NF}')
1188         [[ $val -ne 3 ]] &&
1189                 error "Expect 3 found $val"
1190
1191         echo "Set < 0;  Should fail"
1192         do_lnetctl set response_tracking -1 &&
1193                  error "should have failed $?"
1194
1195         reinit_dlc || return $?
1196         cat <<EOF > $tyaml
1197 global:
1198     response_tracking: -10
1199 EOF
1200         do_lnetctl import < $tyaml &&
1201                 error "should have failed $?"
1202
1203         echo "Check valid values; Should succeed"
1204         local i
1205         for ((i = 0; i < 4; i++)); do
1206                 reinit_dlc || return $?
1207                 do_lnetctl set response_tracking $i ||
1208                          error "should have succeeded $?"
1209                 $LNETCTL global show | grep -q "response_tracking: $i" ||
1210                         error "Failed to set response_tracking to $i"
1211                 reinit_dlc || return $?
1212                 cat <<EOF > $tyaml
1213 global:
1214     response_tracking: $i
1215 EOF
1216                 do_lnetctl import < $tyaml ||
1217                         error "should have succeeded $?"
1218                 $LNETCTL global show | grep -q "response_tracking: $i" ||
1219                         error "Failed to set response_tracking to $i"
1220         done
1221
1222         reinit_dlc || return $?
1223         echo "Set > 3; Should fail"
1224         do_lnetctl set response_tracking 4 &&
1225                  error "should have failed $?"
1226
1227         reinit_dlc || return $?
1228         cat <<EOF > $tyaml
1229 global:
1230     response_tracking: 10
1231 EOF
1232         do_lnetctl import < $tyaml &&
1233                 error "should have failed $?"
1234         return 0
1235 }
1236 run_test 104 "Set/check response_tracking param"
1237
1238 ### load lnet in default namespace, configure in target namespace
1239
1240 test_200() {
1241         cleanup_lnet || exit 1
1242         load_lnet "networks=\"\""
1243         do_ns $LNETCTL lnet configure --all || exit 1
1244         $LNETCTL net show --net tcp | grep -q "nid: ${FAKE_IP}@tcp$"
1245 }
1246 run_test 200 "load lnet w/o module option, configure in a non-default namespace"
1247
1248 test_201() {
1249         cleanup_lnet || exit 1
1250         load_lnet "networks=tcp($FAKE_IF)"
1251         do_ns $LNETCTL lnet configure --all || exit 1
1252         $LNETCTL net show --net tcp | grep -q "nid: ${FAKE_IP}@tcp$"
1253 }
1254 run_test 201 "load lnet using networks module options in a non-default namespace"
1255
1256 test_202() {
1257         cleanup_lnet || exit 1
1258         load_lnet "networks=\"\" ip2nets=\"tcp0($FAKE_IF) ${FAKE_IP}\""
1259         do_ns $LNETCTL lnet configure --all || exit 1
1260         $LNETCTL net show | grep -q "nid: ${FAKE_IP}@tcp$"
1261 }
1262 run_test 202 "load lnet using ip2nets in a non-default namespace"
1263
1264
1265 ### Add the interfaces in the target namespace
1266
1267 test_203() {
1268         cleanup_lnet || exit 1
1269         load_lnet
1270         do_lnetctl lnet configure || exit 1
1271         do_ns $LNETCTL net add --net tcp0 --if $FAKE_IF
1272 }
1273 run_test 203 "add a network using an interface in the non-default namespace"
1274
1275 LNET_PARAMS_FILE="$TMP/$TESTSUITE.parameters"
1276 function save_lnet_params() {
1277         $LNETCTL global show | egrep -v '^global:$' |
1278                                sed 's/://' > $LNET_PARAMS_FILE
1279 }
1280
1281 function restore_lnet_params() {
1282         local param value
1283         while read param value; do
1284                 [[ $param == max_intf ]] && continue
1285                 [[ $param == lnd_timeout ]] && continue
1286                 $LNETCTL set ${param} ${value} ||
1287                         error "Failed to restore ${param} to ${value}"
1288         done < $LNET_PARAMS_FILE
1289 }
1290
1291 function lnet_health_pre() {
1292         save_lnet_params
1293
1294         # Lower transaction timeout to speed up test execution
1295         $LNETCTL set transaction_timeout 10 ||
1296                 error "Failed to set transaction_timeout $?"
1297
1298         # Increase recovery interval so we have time to capture health values
1299         $LNETCTL set recovery_interval 20 ||
1300                 error "Failed to set recovery_interval $?"
1301
1302         RETRY_PARAM=$($LNETCTL global show | awk '/retry_count/{print $NF}')
1303         RSND_PRE=$($LNETCTL stats show | awk '/resend_count/{print $NF}')
1304         LO_HVAL_PRE=$($LNETCTL net show -v 2 | awk '/health value/{print $NF}' |
1305                       xargs echo | sed 's/ /+/g' | bc -l)
1306
1307         local my_nid=$($LCTL list_nids | head -n 1)
1308
1309         RMT_HVAL_PRE=$($LNETCTL peer show --nid $my_nid -v 2 2>/dev/null |
1310                        awk '/health value/{print $NF}' | xargs echo |
1311                        sed 's/ /+/g' | bc -l)
1312
1313         # Might not have any peers so initialize to zero.
1314         RMT_HVAL_PRE=${RMT_HVAL_PRE:-0}
1315
1316         return 0
1317 }
1318
1319 function lnet_health_post() {
1320         RSND_POST=$($LNETCTL stats show | awk '/resend_count/{print $NF}')
1321         LO_HVAL_POST=$($LNETCTL net show -v 2 |
1322                        awk '/health value/{print $NF}' |
1323                        xargs echo | sed 's/ /+/g' | bc -l)
1324
1325         local my_nid=$($LCTL list_nids | head -n 1)
1326
1327         RMT_HVAL_POST=$($LNETCTL peer show --nid $my_nid -v 2 2>/dev/null |
1328                         awk '/health value/{print $NF}' | xargs echo |
1329                         sed 's/ /+/g' | bc -l)
1330
1331         # Might not have any peers so initialize to zero.
1332         RMT_HVAL_POST=${RMT_HVAL_POST:-0}
1333
1334         ${VERBOSE} &&
1335         echo "Pre resends: $RSND_PRE" &&
1336         echo "Post resends: $RSND_POST" &&
1337         echo "Resends delta: $((RSND_POST - RSND_PRE))" &&
1338         echo "Pre local health: $LO_HVAL_PRE" &&
1339         echo "Post local health: $LO_HVAL_POST" &&
1340         echo "Pre remote health: $RMT_HVAL_PRE" &&
1341         echo "Post remote health: $RMT_HVAL_POST"
1342
1343         restore_lnet_params
1344
1345         return 0
1346 }
1347
1348 function check_no_resends() {
1349         echo "Check that no resends took place"
1350         [[ $RSND_POST -ne $RSND_PRE ]] &&
1351                 error "Found resends: $RSND_POST != $RSND_PRE"
1352
1353         return 0
1354 }
1355
1356 function check_resends() {
1357         local delta=$((RSND_POST - RSND_PRE))
1358
1359         echo "Check that $RETRY_PARAM resends took place"
1360         [[ $delta -ne $RETRY_PARAM ]] &&
1361                 error "Expected $RETRY_PARAM resends found $delta"
1362
1363         return 0
1364 }
1365
1366 function check_no_local_health() {
1367         echo "Check that local NI health is unchanged"
1368         [[ $LO_HVAL_POST -ne $LO_HVAL_PRE ]] &&
1369                 error "Local health changed: $LO_HVAL_POST != $LO_HVAL_PRE"
1370
1371         return 0
1372 }
1373
1374 function check_local_health() {
1375         echo "Check that local NI health has been changed"
1376         [[ $LO_HVAL_POST -eq $LO_HVAL_PRE ]] &&
1377                 error "Local health unchanged: $LO_HVAL_POST == $LO_HVAL_PRE"
1378
1379         return 0
1380 }
1381
1382 function check_no_remote_health() {
1383         echo "Check that remote NI health is unchanged"
1384         [[ $RMT_HVAL_POST -ne $RMT_HVAL_PRE ]] &&
1385                 error "Remote health changed: $RMT_HVAL_POST != $RMT_HVAL_PRE"
1386
1387         return 0
1388 }
1389
1390 function check_remote_health() {
1391         echo "Check that remote NI health has been changed"
1392         [[ $RMT_HVAL_POST -eq $RMT_HVAL_PRE ]] &&
1393                 error "Remote health unchanged: $RMT_HVAL_POST == $RMT_HVAL_PRE"
1394
1395         return 0
1396 }
1397
1398 # See lnet/lnet/lib-msg.c:lnet_health_check()
1399 LNET_LOCAL_RESEND_STATUSES="local_interrupt local_dropped local_aborted"
1400 LNET_LOCAL_RESEND_STATUSES+=" local_no_route local_timeout"
1401 LNET_LOCAL_NO_RESEND_STATUSES="local_error"
1402 test_204() {
1403         have_interface "eth0" || skip "Need eth0 interface with ipv4 configured"
1404         reinit_dlc || return $?
1405         add_net "tcp" "eth0" || return $?
1406
1407         lnet_health_pre || return $?
1408
1409         local hstatus
1410         for hstatus in ${LNET_LOCAL_RESEND_STATUSES} \
1411                        ${LNET_LOCAL_NO_RESEND_STATUSES}; do
1412                 echo "Simulate $hstatus"
1413                 $LCTL net_drop_add -s *@tcp -d *@tcp -m GET -r 1 -e ${hstatus}
1414                 do_lnetctl discover $($LCTL list_nids | head -n 1) &&
1415                         error "Should have failed"
1416                 $LCTL net_drop_del *
1417         done
1418
1419         lnet_health_post
1420
1421         check_no_resends || return $?
1422         check_no_local_health || return $?
1423
1424         return 0
1425 }
1426 run_test 204 "Check no health or resends for single-rail local failures"
1427
1428 test_205() {
1429         have_interface "eth0" || skip "Need eth0 interface with ipv4 configured"
1430
1431         local hstatus
1432         for hstatus in ${LNET_LOCAL_RESEND_STATUSES}; do
1433                 reinit_dlc || return $?
1434                 add_net "tcp" "eth0" || return $?
1435                 add_net "tcp1" "eth0" || return $?
1436
1437                 echo "Simulate $hstatus"
1438                 lnet_health_pre
1439
1440                 $LCTL net_drop_add -s *@tcp -d *@tcp -m GET -r 1 -e ${hstatus}
1441                 $LCTL net_drop_add -s *@tcp1 -d *@tcp1 -m GET -r 1 -e ${hstatus}
1442                 do_lnetctl discover $($LCTL list_nids | head -n 1) &&
1443                         error "Should have failed"
1444                 $LCTL net_drop_del *
1445
1446                 lnet_health_post
1447
1448                 check_resends || return $?
1449                 check_local_health || return $?
1450         done
1451
1452         for hstatus in ${LNET_LOCAL_NO_RESEND_STATUSES}; do
1453                 reinit_dlc || return $?
1454                 add_net "tcp" "eth0" || return $?
1455                 add_net "tcp1" "eth0" || return $?
1456
1457                 echo "Simulate $hstatus"
1458                 lnet_health_pre || return $?
1459
1460                 $LCTL net_drop_add -s *@tcp -d *@tcp -m GET -r 1 -e ${hstatus}
1461                 $LCTL net_drop_add -s *@tcp1 -d *@tcp1 -m GET -r 1 -e ${hstatus}
1462                 do_lnetctl discover $($LCTL list_nids | head -n 1) &&
1463                         error "Should have failed"
1464                 $LCTL net_drop_del *
1465
1466                 lnet_health_post
1467
1468                 check_no_resends || return $?
1469                 check_local_health || return $?
1470         done
1471
1472         return 0
1473 }
1474 run_test 205 "Check health and resends for multi-rail local failures"
1475
1476 # See lnet/lnet/lib-msg.c:lnet_health_check()
1477 LNET_REMOTE_RESEND_STATUSES="remote_dropped"
1478 LNET_REMOTE_NO_RESEND_STATUSES="remote_error remote_timeout network_timeout"
1479 test_206() {
1480         have_interface "eth0" || skip "Need eth0 interface with ipv4 configured"
1481         reinit_dlc || return $?
1482         add_net "tcp" "eth0" || return $?
1483
1484         do_lnetctl discover $($LCTL list_nids | head -n 1) ||
1485                 error "failed to discover myself"
1486
1487         lnet_health_pre || return $?
1488
1489         local hstatus
1490         for hstatus in ${LNET_REMOTE_RESEND_STATUSES} \
1491                        ${LNET_REMOTE_NO_RESEND_STATUSES}; do
1492                 echo "Simulate $hstatus"
1493                 $LCTL net_drop_add -s *@tcp -d *@tcp -m GET -r 1 -e ${hstatus}
1494                 do_lnetctl discover $($LCTL list_nids | head -n 1) &&
1495                         error "Should have failed"
1496                 $LCTL net_drop_del *
1497         done
1498
1499         lnet_health_post
1500
1501         check_no_resends || return $?
1502         check_no_local_health || return $?
1503         check_no_remote_health || return $?
1504
1505         return 0
1506 }
1507 run_test 206 "Check no health or resends for single-rail remote failures"
1508
1509 test_207() {
1510         have_interface "eth0" || skip "Need eth0 interface with ipv4 configured"
1511
1512         local hstatus
1513         for hstatus in ${LNET_REMOTE_RESEND_STATUSES}; do
1514                 reinit_dlc || return $?
1515                 add_net "tcp" "eth0" || return $?
1516                 add_net "tcp1" "eth0" || return $?
1517
1518                 do_lnetctl discover $($LCTL list_nids | head -n 1) ||
1519                         error "failed to discover myself"
1520
1521                 echo "Simulate $hstatus"
1522                 lnet_health_pre || return $?
1523                 $LCTL net_drop_add -s *@tcp -d *@tcp -m GET -r 1 -e ${hstatus}
1524                 $LCTL net_drop_add -s *@tcp1 -d *@tcp1 -m GET -r 1 -e ${hstatus}
1525                 do_lnetctl discover $($LCTL list_nids | head -n 1) &&
1526                         error "Should have failed"
1527                 $LCTL net_drop_del *
1528
1529                 lnet_health_post
1530
1531                 check_resends || return $?
1532                 check_no_local_health || return $?
1533                 check_remote_health || return $?
1534         done
1535         for hstatus in ${LNET_REMOTE_NO_RESEND_STATUSES}; do
1536                 reinit_dlc || return $?
1537                 add_net "tcp" "eth0" || return $?
1538                 add_net "tcp1" "eth0" || return $?
1539
1540                 do_lnetctl discover $($LCTL list_nids | head -n 1) ||
1541                         error "failed to discover myself"
1542
1543                 echo "Simulate $hstatus"
1544                 lnet_health_pre || return $?
1545                 $LCTL net_drop_add -s *@tcp -d *@tcp -m GET -r 1 -e ${hstatus}
1546                 $LCTL net_drop_add -s *@tcp1 -d *@tcp1 -m GET -r 1 -e ${hstatus}
1547                 do_lnetctl discover $($LCTL list_nids | head -n 1) &&
1548                         error "Should have failed"
1549                 $LCTL net_drop_del *
1550
1551                 lnet_health_post
1552
1553                 check_no_resends || return $?
1554                 check_no_local_health || return $?
1555                 check_remote_health || return $?
1556         done
1557
1558         return 0
1559 }
1560 run_test 207 "Check health and resends for multi-rail remote errors"
1561
1562 test_208_load_and_check_lnet() {
1563         local ip2nets="$1"
1564         local p_nid="$2"
1565         local s_nid="$3"
1566         local num_expected=1
1567
1568         load_lnet "networks=\"\" ip2nets=\"${ip2nets_str}\""
1569
1570         $LCTL net up ||
1571                 error "Failed to load LNet with ip2nets \"${ip2nets_str}\""
1572
1573         [[ -n $s_nid ]] &&
1574                 num_expected=2
1575
1576         declare -a nids
1577         nids=( $($LCTL list_nids) )
1578
1579         [[ ${#nids[@]} -ne ${num_expected} ]] &&
1580                 error "Expect ${num_expected} NIDs found ${#nids[@]}"
1581
1582         [[ ${nids[0]} == ${p_nid} ]] ||
1583                 error "Expect NID \"${p_nid}\" found \"${nids[0]}\""
1584
1585         [[ -n $s_nid ]] && [[ ${nids[1]} != ${s_nid} ]] &&
1586                 error "Expect second NID \"${s_nid}\" found \"${nids[1]}\""
1587
1588         $LCTL net down &>/dev/null
1589         cleanup_lnet
1590 }
1591
1592 test_208() {
1593         have_interface "eth0" || skip "Need eth0 interface with ipv4 configured"
1594
1595         cleanup_netns || error "Failed to cleanup netns before test execution"
1596         cleanup_lnet || error "Failed to unload modules before test execution"
1597         setup_fakeif || error "Failed to add fake IF"
1598
1599         have_interface "$FAKE_IF" ||
1600                 error "Expect $FAKE_IF configured but not found"
1601
1602         local eth0_ip=$(ip --oneline addr show dev eth0 |
1603                         awk '/inet /{print $4}' |
1604                         sed 's:/.*::')
1605         local ip2nets_str="tcp(eth0) $eth0_ip"
1606
1607         echo "Configure single NID \"$ip2nets_str\""
1608         test_208_load_and_check_lnet "${ip2nets_str}" "${eth0_ip}@tcp"
1609
1610         ip2nets_str="tcp(eth0) $eth0_ip; tcp1($FAKE_IF) $FAKE_IP"
1611         echo "Configure two NIDs; two NETs \"$ip2nets_str\""
1612         test_208_load_and_check_lnet "${ip2nets_str}" "${eth0_ip}@tcp" \
1613                                      "${FAKE_IP}@tcp1"
1614
1615         ip2nets_str="tcp(eth0) $eth0_ip; tcp($FAKE_IF) $FAKE_IP"
1616         echo "Configure two NIDs; one NET \"$ip2nets_str\""
1617         test_208_load_and_check_lnet "${ip2nets_str}" "${eth0_ip}@tcp" \
1618                                      "${FAKE_IP}@tcp"
1619         local addr1=( ${eth0_ip//./ } )
1620         local addr2=( ${FAKE_IP//./ } )
1621         local range="[${addr1[0]},${addr2[0]}]"
1622
1623         local i
1624         for i in $(seq 1 3); do
1625                 range+=".[${addr1[$i]},${addr2[$i]}]"
1626         done
1627         ip2nets_str="tcp(eth0,${FAKE_IF}) ${range}"
1628
1629         echo "Configured two NIDs; one NET alt syntax \"$ip2nets_str\""
1630         test_208_load_and_check_lnet "${ip2nets_str}" "${eth0_ip}@tcp" \
1631                                      "${FAKE_IP}@tcp"
1632
1633         cleanup_fakeif
1634
1635         echo "alt syntax with missing IF \"$ip2nets_str\""
1636         load_lnet "networks=\"\" ip2nets=\"${ip2nets_str}\""
1637
1638         echo "$LCTL net up should fail"
1639         $LCTL net up &&
1640                 error "LNet bringup should have failed"
1641
1642         cleanup_lnet
1643 }
1644 run_test 208 "Test various kernel ip2nets configurations"
1645
1646 test_300() {
1647         # LU-13274
1648         local header
1649         local out=$TMP/$tfile
1650         local prefix=/usr/include/linux/lnet
1651
1652         # We use a hard coded prefix so that this test will not fail
1653         # when run in tree.
1654         CC=${CC:-cc}
1655         if ! which $CC > /dev/null 2>&1; then
1656                 skip_env "$CC is not installed"
1657         fi
1658
1659         cleanup_lnet || exit 1
1660         load_lnet
1661
1662         if ! [[ -d $prefix ]]; then
1663                 # Assume we're running in tree and fixup the include path.
1664                 prefix=$LUSTRE/../lnet/include/uapi/linux/lnet
1665         fi
1666
1667         for header in $prefix/*.h; do
1668                 if ! [[ -f "$header" ]]; then
1669                         continue
1670                 fi
1671
1672                 $CC -Wall -Werror -std=c99 -include $header -c -x c /dev/null -o $out ||
1673                         error "cannot compile '$header'"
1674         done
1675         rm -f $out
1676 }
1677 run_test 300 "packaged LNet UAPI headers can be compiled"
1678
1679 complete $SECONDS
1680
1681 cleanup_testsuite
1682 exit_status