Whamcloud - gitweb
LU-12410 tests: Additional test cases for lnetctl and DLC
[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 # bug number for skipped test:
11 ALWAYS_EXCEPT="$SANITY_LNET_EXCEPT "
12 [ "$SLOW" = "no" ] && EXCEPT_SLOW=""
13 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
14
15 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
16
17 . $LUSTRE/tests/test-framework.sh
18 CLEANUP=${CLEANUP:-:}
19 SETUP=${SETUP:-:}
20 init_test_env $@
21 . ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
22 init_logging
23
24 build_test_filter
25
26 export LNETCTL=${LNETCTL:-"$LUSTRE/../lnet/utils/lnetctl"}
27 [ ! -f "$LNETCTL" ] &&
28         export LNETCTL=$(which lnetctl 2> /dev/null)
29 [[ -z $LNETCTL ]] && skip "Need lnetctl"
30
31 restore_mounts=false
32
33 if is_mounted $MOUNT || is_mounted $MOUNT2; then
34         cleanupall || error "Failed cleanup prior to test execution"
35         restore_mounts=true
36 fi
37
38 cleanup_lnet() {
39         echo "Cleaning up LNet"
40         $LNETCTL lnet unconfigure 2>/dev/null
41         unload_modules
42 }
43
44 restore_modules=false
45 if module_loaded lnet ; then
46         cleanup_lnet || error "Failed to unload modules before test execution"
47         restore_modules=true
48 fi
49
50 cleanup_testsuite() {
51         rm -f $TMP/sanity-dlc*
52         cleanup_netns
53         cleanup_lnet
54         if $restore_mounts; then
55                 setupall || error "Failed to setup Lustre after test execution"
56         elif $restore_modules; then
57                 load_modules ||
58                         error "Couldn't load modules after test execution"
59         fi
60         return 0
61 }
62
63 load_lnet() {
64         load_module ../libcfs/libcfs/libcfs
65         # Prevent local MODOPTS_LIBCFS being passed as part of environment
66         # variable to remote nodes
67         unset MODOPTS_LIBCFS
68
69         set_default_debug
70         load_module ../lnet/lnet/lnet "$@"
71
72         LNDPATH=${LNDPATH:-"../lnet/klnds"}
73         if [ -z "$LNETLND" ]; then
74                 case $NETTYPE in
75                 o2ib*)  LNETLND="o2iblnd/ko2iblnd" ;;
76                 tcp*)   LNETLND="socklnd/ksocklnd" ;;
77                 *)      local lnd="${NETTYPE%%[0-9]}lnd"
78                         [ -f "$LNDPATH/$lnd/k$lnd.ko" ] &&
79                                 LNETLND="$lnd/k$lnd" ||
80                                 LNETLND="socklnd/ksocklnd"
81                 esac
82         fi
83         load_module ../lnet/klnds/$LNETLND
84 }
85
86 do_lnetctl() {
87         echo "$LNETCTL $@"
88         $LNETCTL "$@"
89 }
90
91 TESTNS='test_ns'
92 FAKE_IF="test1pg"
93 FAKE_IP="10.1.2.3"
94 do_ns() {
95         echo "ip netns exec $TESTNS $@"
96         ip netns exec $TESTNS "$@"
97 }
98
99 setup_netns() {
100         cleanup_netns
101
102         ip netns add $TESTNS
103         ip link add 'test1pl' type veth peer name $FAKE_IF netns $TESTNS
104         ip link set 'test1pl' up
105         do_ns ip addr add "${FAKE_IP}/31" dev $FAKE_IF
106         do_ns ip link set $FAKE_IF up
107 }
108
109 cleanup_netns() {
110         (ip netns list | grep -q $TESTNS) && ip netns del $TESTNS
111         if ip link show test1pl >/dev/null 2>&1; then
112                 ip link del test1pl
113         fi
114 }
115
116 configure_dlc() {
117         echo "Loading LNet and configuring DLC"
118         load_lnet
119         do_lnetctl lnet configure
120 }
121
122 GLOBAL_YAML_FILE=$TMP/sanity-lnet-global.yaml
123 define_global_yaml() {
124         $LNETCTL export --backup >${GLOBAL_YAML_FILE} ||
125                 error "Failed to export global yaml $?"
126 }
127
128 reinit_dlc() {
129         if lsmod | grep -q lnet; then
130                 do_lnetctl lnet unconfigure ||
131                         error "lnetctl lnet unconfigure failed $?"
132                 do_lnetctl lnet configure ||
133                         error "lnetctl lnet configure failed $?"
134         else
135                 configure_dlc || error "configure_dlc failed $?"
136         fi
137         define_global_yaml
138 }
139
140 append_global_yaml() {
141         [[ ! -e ${GLOBAL_YAML_FILE} ]] &&
142                 error "Missing global yaml at ${GLOBAL_YAML_FILE}"
143
144         cat ${GLOBAL_YAML_FILE} >> $TMP/sanity-lnet-$testnum-expected.yaml
145 }
146
147 create_base_yaml_file() {
148         append_global_yaml
149 }
150
151 compare_yaml_files() {
152         local expected="$TMP/sanity-lnet-$testnum-expected.yaml"
153         local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
154         local rc=0
155         ! [[ -e $expected ]] && echo "$expected not found" && return 1
156         ! [[ -e $actual ]] && echo "$actual not found" && return 1
157         diff -upN ${actual} ${expected} || rc=$?
158         echo "Expected:"
159         cat $expected
160         echo "Actual:"
161         cat $actual
162         return $rc
163 }
164
165 validate_nid() {
166         local nid="$1"
167         local net="${nid//*@/}"
168         local addr="${nid//@*/}"
169
170         local num_re='[0-9]\+'
171         local ip_re="[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
172
173         if [[ $net =~ gni[0-9]* ]]; then
174                 [[ $addr =~ ${num_re} ]] && return 0
175         else
176                 [[ $addr =~ ${ip_re} ]] && return 0
177         fi
178 }
179
180 validate_nids() {
181         local yfile=$TMP/sanity-lnet-$testnum-actual.yaml
182         local primary_nids=$(awk '/- primary nid:/{print $NF}' $yfile | xargs echo)
183         local secondary_nids=$(awk '/- nid:/{print $NF}' $yfile | xargs echo)
184         local gateway_nids=$(awk '/gateway:/{print $NF}' $yfile | xargs echo)
185
186         local nid
187         for nid in $primary_nids $secondary_nids; do
188                 validate_nid "$nid" || error "Bad NID \"${nid}\""
189         done
190         return 0
191 }
192
193 validate_peer_nids() {
194         local num_peers="$1"
195         local nids_per_peer="$2"
196
197         local expect_p="$num_peers"
198         # The primary nid also shows up in the list of secondary nids
199         local expect_s="$(($num_peers + $(($nids_per_peer*$num_peers))))"
200
201         local actual_p=$(grep -c -- '- primary nid:' $TMP/sanity-lnet-$testnum-actual.yaml)
202         local actual_s=$(grep -c -- '- nid:' $TMP/sanity-lnet-$testnum-actual.yaml)
203         if [[ $expect_p -ne $actual_p ]]; then
204                 compare_yaml_files
205                 error "Expected $expect_p but found $actual_p primary nids"
206         elif [[ $expect_s -ne $actual_s ]]; then
207                 compare_yaml_files
208                 error "Expected $expect_s but found $actual_s secondary nids"
209         fi
210         validate_nids
211 }
212
213 validate_gateway_nids() {
214         local expect_gw=$(grep -c -- 'gateway:' $TMP/sanity-lnet-$testnum-expected.yaml)
215         local actual_gw=$(grep -c -- 'gateway:' $TMP/sanity-lnet-$testnum-actual.yaml)
216         if [[ $expect_gw -ne $actual_gw ]]; then
217                 compare_yaml_files
218                 error "Expected $expect_gw gateways but found $actual_gw gateways"
219         fi
220         validate_nids
221 }
222
223 cleanupall -f
224 setup_netns || error "setup_netns failed with $?"
225
226 test_0() {
227         load_module ../lnet/lnet/lnet || error "Failed to load module rc = $?"
228         do_lnetctl lnet configure || error "lnet configure failed rc = $?"
229         define_global_yaml
230         reinit_dlc || return $?
231         do_lnetctl import <  ${GLOBAL_YAML_FILE} || error "Import failed $?"
232         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
233         create_base_yaml_file
234         compare_yaml_files || error "Configuration changed after import"
235 }
236 run_test 0 "Export empty config, import the config, compare"
237
238 compare_peer_add() {
239         local prim_nid="${1:+--prim_nid $1}"
240         local nid="${2:+--nid $2}"
241
242         local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
243
244         do_lnetctl peer add ${prim_nid} ${nid} || error "peer add failed $?"
245         $LNETCTL export --backup > $actual || error "export failed $?"
246         compare_yaml_files
247         return $?
248 }
249
250 test_1() {
251         reinit_dlc || return $?
252         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
253 peer:
254     - primary nid: 1.1.1.1@tcp
255       Multi-Rail: True
256       peer ni:
257         - nid: 1.1.1.1@tcp
258 EOF
259         append_global_yaml
260         compare_peer_add "1.1.1.1@tcp"
261 }
262 run_test 1 "Add peer with single nid (tcp)"
263
264 test_2() {
265         reinit_dlc || return $?
266         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
267 peer:
268     - primary nid: 2.2.2.2@o2ib
269       Multi-Rail: True
270       peer ni:
271         - nid: 2.2.2.2@o2ib
272 EOF
273         append_global_yaml
274         compare_peer_add "2.2.2.2@o2ib"
275 }
276 run_test 2 "Add peer with single nid (o2ib)"
277
278 test_3() {
279         reinit_dlc || return $?
280         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
281 peer:
282     - primary nid: 3.3.3.3@tcp
283       Multi-Rail: True
284       peer ni:
285         - nid: 3.3.3.3@tcp
286         - nid: 3.3.3.3@o2ib
287 EOF
288         append_global_yaml
289         compare_peer_add "3.3.3.3@tcp" "3.3.3.3@o2ib"
290 }
291 run_test 3 "Add peer with tcp primary o2ib secondary"
292
293 test_4() {
294         reinit_dlc || return $?
295         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
296 peer:
297     - primary nid: 4.4.4.4@tcp
298       Multi-Rail: True
299       peer ni:
300         - nid: 4.4.4.4@tcp
301         - nid: 4.4.4.1@tcp
302         - nid: 4.4.4.2@tcp
303         - nid: 4.4.4.3@tcp
304 EOF
305         append_global_yaml
306         echo "Add peer with nidrange (tcp)"
307         compare_peer_add "4.4.4.4@tcp" "4.4.4.[1-3]@tcp"
308
309         echo "Add peer with nidrange that overlaps primary nid (tcp)"
310         compare_peer_add "4.4.4.4@tcp" "4.4.4.[1-4]@tcp"
311 }
312 run_test 4 "Add peer with nidrange (tcp)"
313
314 test_5() {
315         reinit_dlc || return $?
316         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
317 peer:
318     - primary nid: 5.5.5.5@o2ib
319       Multi-Rail: True
320       peer ni:
321         - nid: 5.5.5.5@o2ib
322         - nid: 5.5.5.1@o2ib
323         - nid: 5.5.5.2@o2ib
324         - nid: 5.5.5.3@o2ib
325         - nid: 5.5.5.4@o2ib
326 EOF
327         append_global_yaml
328         echo "Add peer with nidrange (o2ib)"
329         compare_peer_add "5.5.5.5@o2ib" "5.5.5.[1-4]@o2ib"
330
331         echo "Add peer with nidranage that overlaps primary nid (o2ib)"
332         compare_peer_add "5.5.5.5@o2ib" "5.5.5.[1-4]@o2ib"
333 }
334 run_test 5 "Add peer with nidrange (o2ib)"
335
336 test_6() {
337         reinit_dlc || return $?
338         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
339 peer:
340     - primary nid: 6.6.6.6@tcp
341       Multi-Rail: True
342       peer ni:
343         - nid: 6.6.6.6@tcp
344         - nid: 6.6.6.0@tcp
345         - nid: 6.6.6.2@tcp
346         - nid: 6.6.6.4@tcp
347         - nid: 6.6.7.0@tcp
348         - nid: 6.6.7.2@tcp
349         - nid: 6.6.7.4@tcp
350         - nid: 6.6.1.0@o2ib
351         - nid: 6.6.1.3@o2ib
352         - nid: 6.6.1.6@o2ib
353         - nid: 6.6.3.0@o2ib
354         - nid: 6.6.3.3@o2ib
355         - nid: 6.6.3.6@o2ib
356 EOF
357         append_global_yaml
358         compare_peer_add "6.6.6.6@tcp" \
359                          "6.6.[6-7].[0-4/2]@tcp,6.6.[1-4/2].[0-6/3]@o2ib"
360 }
361 run_test 6 "Add peer with multiple nidranges"
362
363 compare_peer_del() {
364         local prim_nid="${1:+--prim_nid $1}"
365         local nid="${2:+--nid $2}"
366
367         local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
368
369         do_lnetctl peer del ${prim_nid} ${nid} || error "peer del failed $?"
370         $LNETCTL export --backup > $actual || error "export failed $?"
371         compare_yaml_files
372         return $?
373 }
374
375 test_7() {
376         reinit_dlc || return $?
377         create_base_yaml_file
378
379         echo "Delete peer with single nid (tcp)"
380         do_lnetctl peer add --prim_nid 7.7.7.7@tcp || error "Peer add failed $?"
381         compare_peer_del "7.7.7.7@tcp"
382
383         echo "Delete peer with single nid (o2ib)"
384         do_lnetctl peer add --prim_nid 7.7.7.7@o2ib || error "Peer add failed $?"
385         compare_peer_del "7.7.7.7@o2ib"
386
387         echo "Delete peer that has multiple nids (tcp)"
388         do_lnetctl peer add --prim_nid 7.7.7.7@tcp --nid 7.7.7.[8-12]@tcp ||
389                 error "Peer add failed $?"
390         compare_peer_del "7.7.7.7@tcp"
391
392         echo "Delete peer that has multiple nids (o2ib)"
393         do_lnetctl peer add --prim_nid 7.7.7.7@o2ib --nid 7.7.7.[8-12]@o2ib ||
394                 error "Peer add failed $?"
395         compare_peer_del "7.7.7.7@o2ib"
396
397         echo "Delete peer that has both tcp and o2ib nids"
398         do_lnetctl peer add --prim_nid 7.7.7.7@tcp \
399                 --nid 7.7.7.[9-12]@tcp,7.7.7.[13-15]@o2ib ||
400                 error "Peer add failed $?"
401         compare_peer_del "7.7.7.7@tcp"
402 }
403 run_test 7 "Various peer delete tests"
404
405 test_8() {
406         reinit_dlc || return $?
407
408         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
409 peer:
410     - primary nid: 8.8.8.8@tcp
411       Multi-Rail: True
412       peer ni:
413         - nid: 8.8.8.8@tcp
414         - nid: 8.8.8.10@tcp
415         - nid: 8.8.8.11@tcp
416         - nid: 8.8.8.12@tcp
417         - nid: 8.8.8.14@tcp
418         - nid: 8.8.8.15@tcp
419 EOF
420         append_global_yaml
421
422         do_lnetctl peer add --prim_nid 8.8.8.8@tcp --nid 8.8.8.[10-15]@tcp ||
423                 error "Peer add failed $?"
424         compare_peer_del "8.8.8.8@tcp" "8.8.8.13@tcp"
425 }
426 run_test 8 "Delete single secondary nid from peer (tcp)"
427
428 test_9() {
429         reinit_dlc || return $?
430
431         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
432 peer:
433     - primary nid: 9.9.9.9@tcp
434       Multi-Rail: True
435       peer ni:
436         - nid: 9.9.9.9@tcp
437 EOF
438         append_global_yaml
439
440         do_lnetctl peer add --prim_nid 9.9.9.9@tcp \
441                 --nid 9.9.9.[11-16]@tcp || error "Peer add failed $?"
442         compare_peer_del "9.9.9.9@tcp" "9.9.9.[11-16]@tcp"
443 }
444 run_test 9 "Delete all secondary nids from peer (tcp)"
445
446 test_10() {
447         reinit_dlc || return $?
448
449         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
450 peer:
451     - primary nid: 10.10.10.10@tcp
452       Multi-Rail: True
453       peer ni:
454         - nid: 10.10.10.10@tcp
455         - nid: 10.10.10.12@tcp
456         - nid: 10.10.10.13@tcp
457         - nid: 10.10.10.15@tcp
458         - nid: 10.10.10.16@tcp
459 EOF
460         append_global_yaml
461         do_lnetctl peer add --prim_nid 10.10.10.10@tcp \
462                 --nid 10.10.10.[12-16]@tcp || error "Peer add failed $?"
463         compare_peer_del "10.10.10.10@tcp" "10.10.10.14@tcp"
464 }
465 run_test 10 "Delete single secondary nid from peer (o2ib)"
466
467 test_11() {
468         reinit_dlc || return $?
469
470         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
471 peer:
472     - primary nid: 11.11.11.11@tcp
473       Multi-Rail: True
474       peer ni:
475         - nid: 11.11.11.11@tcp
476 EOF
477         append_global_yaml
478         do_lnetctl peer add --prim_nid 11.11.11.11@tcp \
479                 --nid 11.11.11.[13-17]@tcp || error "Peer add failed $?"
480         compare_peer_del "11.11.11.11@tcp" "11.11.11.[13-17]@tcp"
481 }
482 run_test 11 "Delete all secondary nids from peer (o2ib)"
483
484 test_12() {
485         reinit_dlc || return $?
486
487         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
488 peer:
489     - primary nid: 12.12.12.12@o2ib
490       Multi-Rail: True
491       peer ni:
492         - nid: 12.12.12.12@o2ib
493         - nid: 13.13.13.13@o2ib
494         - nid: 14.13.13.13@o2ib
495         - nid: 14.15.13.13@o2ib
496         - nid: 15.17.1.5@tcp
497         - nid: 15.17.1.10@tcp
498         - nid: 15.17.1.20@tcp
499 EOF
500         append_global_yaml
501         do_lnetctl peer add --prim_nid 12.12.12.12@o2ib \
502                 --nid [13-14/1].[13-15/2].13.13@o2ib,[15-16/3].[17-19/4].[1].[5-20/5]@tcp ||
503                 error "Peer add failed $?"
504         compare_peer_del "12.12.12.12@o2ib" "13.15.13.13@o2ib,15.17.1.15@tcp"
505 }
506 run_test 12 "Delete a secondary nid from peer (tcp and o2ib)"
507
508 test_13() {
509         reinit_dlc || return $?
510
511         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
512 peer:
513     - primary nid: 13.13.13.13@o2ib
514       Multi-Rail: True
515       peer ni:
516         - nid: 13.13.13.13@o2ib
517 EOF
518         append_global_yaml
519         do_lnetctl peer add --prim_nid 13.13.13.13@o2ib \
520                 --nid [14-15].[1-2/1].[1].[100-254/10]@tcp,14.14.[254].14@o2ib ||
521                 error "Peer add failed $?"
522         compare_peer_del "13.13.13.13@o2ib" \
523                 "[14-15].[1-2/1].[1].[100-254/10]@tcp,14.14.[254].14@o2ib"
524 }
525 run_test 13 "Delete all secondary nids from peer (tcp and o2ib)"
526
527 create_nid() {
528         local num="$1"
529         local net="$2"
530
531         if [[ $net =~ gni* ]]; then
532                 echo "${num}@${net}"
533         else
534                 echo "${num}.${num}.${num}.${num}@${net}"
535         fi
536 }
537
538 create_mr_peer_yaml() {
539         local num_peers="$1"
540         local secondary_nids="$2"
541         local net="$3"
542
543         echo "Generating peer yaml for $num_peers peers with $secondary_nids secondary nids"
544         echo "peer:" >> $TMP/sanity-lnet-$testnum-expected.yaml
545         local i
546         local total_nids=$((num_peers + $((num_peers * secondary_nids))))
547         local created=0
548         local nidnum=1
549         while [[ $created -lt $num_peers ]]; do
550                 local primary=$(create_nid ${nidnum} ${net})
551         cat <<EOF >> $TMP/sanity-lnet-$testnum-expected.yaml
552     - primary nid: $primary
553       Multi-Rail: True
554       peer ni:
555         - nid: $primary
556 EOF
557                 local j
558                 local start=$((nidnum + 1))
559                 local end=$((nidnum + $secondary_nids))
560                 for j in $(seq ${start} ${end}); do
561                         local nid=$(create_nid $j ${net})
562                         echo "        - nid: $nid" >> $TMP/sanity-lnet-$testnum-expected.yaml
563                 done
564                 nidnum=$((end + 1))
565                 ((created++))
566         done
567 }
568
569 test_14() {
570         reinit_dlc || return $?
571
572         echo "Create single peer, single nid, using import"
573         create_mr_peer_yaml 1 0 tcp
574         do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
575                 error "Import failed $?"
576         append_global_yaml
577         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
578         compare_yaml_files
579
580         echo "Delete single peer using import --del"
581         do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml ||
582                 error "Import failed $?"
583         rm -f $TMP/sanity-lnet-$testnum-expected.yaml
584         create_base_yaml_file
585         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
586         compare_yaml_files
587 }
588 run_test 14 "import peer create/delete with single nid"
589
590 test_15() {
591         reinit_dlc || return $?
592
593         echo "Create multiple peers, single nid per peer, using import"
594         create_mr_peer_yaml 5 0 o2ib
595         # The ordering of nids for this use-case is non-deterministic, so we
596         # we can't just diff the expected/actual output.
597         do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
598                 error "Import failed $?"
599         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
600         validate_peer_nids 5 0
601
602         echo "Delete multiple peers, single nid per peer, using import --del"
603         do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml ||
604                 error "Import failed $?"
605         rm -f $TMP/sanity-lnet-$testnum-expected.yaml
606         create_base_yaml_file
607         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
608         compare_yaml_files
609 }
610 run_test 15 "import multi peer create/delete with single nid per peer"
611
612 test_16() {
613         reinit_dlc || return $?
614
615         echo "Create single peer, multiple nids, using import"
616         create_mr_peer_yaml 1 5 tcp
617         do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
618                 error "Import failed $?"
619         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
620         validate_peer_nids 1 5
621
622         echo "Delete single peer, multiple nids, using import --del"
623         do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml ||
624                 error "Import failed $?"
625         rm -f $TMP/sanity-lnet-$testnum-expected.yaml
626         create_base_yaml_file
627         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
628         compare_yaml_files
629 }
630 run_test 16 "import peer create/delete with multiple nids"
631
632 test_17() {
633         reinit_dlc || return $?
634
635         echo "Create multiple peers, multiple nids per peer, using import"
636         create_mr_peer_yaml 5 7 o2ib
637         do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
638                 error "Import failed $?"
639         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
640         validate_peer_nids 5 7
641
642         echo "Delete multiple peers, multiple nids per peer, using import --del"
643         do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml ||
644                 error "Import failed $?"
645         rm -f $TMP/sanity-lnet-$testnum-expected.yaml
646         create_base_yaml_file
647         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
648         compare_yaml_files
649 }
650 run_test 17 "import multi peer create/delete with multiple nids"
651
652 test_18a() {
653         reinit_dlc || return $?
654
655         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
656 peer:
657     - primary nid: 1.1.1.1@tcp
658       Multi-Rail: True
659       peer ni:
660         - nid: 1.1.1.1@tcp
661         - nid: 2.2.2.2@tcp
662         - nid: 4.4.4.4@tcp
663         - nid: 3.3.3.3@o2ib
664 EOF
665         echo "Import peer with 4 nids"
666         cat $TMP/sanity-lnet-$testnum-expected.yaml
667         do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
668                 error "Import failed $?"
669         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
670 peer:
671     - primary nid: 1.1.1.1@tcp
672       Multi-Rail: True
673       peer ni:
674         - nid: 2.2.2.2@tcp
675         - nid: 3.3.3.3@o2ib
676 EOF
677         echo "Delete two of the nids"
678         cat $TMP/sanity-lnet-$testnum-expected.yaml
679         do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml
680         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
681 peer:
682     - primary nid: 1.1.1.1@tcp
683       Multi-Rail: True
684       peer ni:
685         - nid: 1.1.1.1@tcp
686         - nid: 4.4.4.4@tcp
687 EOF
688         echo "Check peer has expected nids remaining"
689         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
690         append_global_yaml
691         compare_yaml_files
692 }
693 run_test 18a "Delete a subset of nids from a single peer using import --del"
694
695 test_18b() {
696         reinit_dlc || return $?
697
698         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
699 peer:
700     - primary nid: 1.1.1.1@tcp
701       Multi-Rail: True
702       peer ni:
703         - nid: 1.1.1.1@tcp
704         - nid: 2.2.2.2@tcp
705         - nid: 4.4.4.4@tcp
706         - nid: 3.3.3.3@o2ib
707     - primary nid: 5.5.5.5@o2ib
708       Multi-Rail: True
709       peer ni:
710         - nid: 5.5.5.5@o2ib
711         - nid: 6.6.6.6@tcp
712         - nid: 7.7.7.7@tcp
713         - nid: 8.8.8.8@tcp
714 EOF
715         echo "Import two peers with 4 nids each"
716         cat $TMP/sanity-lnet-$testnum-expected.yaml
717         do_lnetctl import < $TMP/sanity-lnet-$testnum-expected.yaml ||
718                 error "Import failed $?"
719         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
720 peer:
721     - primary nid: 1.1.1.1@tcp
722       Multi-Rail: True
723       peer ni:
724         - nid: 2.2.2.2@tcp
725         - nid: 3.3.3.3@o2ib
726     - primary nid: 5.5.5.5@o2ib
727       Multi-Rail: True
728       peer ni:
729         - nid: 7.7.7.7@tcp
730         - nid: 8.8.8.8@tcp
731 EOF
732         echo "Delete two of the nids from each peer"
733         cat $TMP/sanity-lnet-$testnum-expected.yaml
734         do_lnetctl import --del < $TMP/sanity-lnet-$testnum-expected.yaml
735         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
736 peer:
737     - primary nid: 5.5.5.5@o2ib
738       Multi-Rail: True
739       peer ni:
740         - nid: 5.5.5.5@o2ib
741         - nid: 6.6.6.6@tcp
742     - primary nid: 1.1.1.1@tcp
743       Multi-Rail: True
744       peer ni:
745         - nid: 1.1.1.1@tcp
746         - nid: 4.4.4.4@tcp
747 EOF
748         append_global_yaml
749         echo "Check peers have expected nids remaining"
750         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
751         compare_yaml_files
752         validate_peer_nids 2 1
753 }
754 run_test 18b "Delete multiple nids from multiple peers using import --del"
755
756 test_99a() {
757         reinit_dlc || return $?
758         echo "Invalid prim_nid: should fail"
759         do_lnetctl peer add --prim_nid foobar &&
760                 error "Command should have failed"
761
762         do_lnetctl peer del --prim_nid foobar &&
763                 error "Command should have failed"
764
765         echo "Delete non-existing peer: should fail"
766         do_lnetctl peer del --prim_nid 1.1.1.1@o2ib &&
767                 error "Command should have failed"
768
769         echo "Don't provide mandatory arguments: should fail"
770         do_lnetctl peer del --nid 1.1.1.1@tcp &&
771                 error "Command should have failed"
772         do_lnetctl peer add &&
773                 error "Command should have failed"
774         # This currently causes a segfault. Uncomment when fixed.
775         #echo "Invalid secondary nids: should fail"
776         #do_lnetctl peer add --prim_nid 1.1.1.1@tcp --nid foobar && error "Command should have failed"
777
778         # This shouldn't work, but currently does. Uncomment when fixed.
779         #echo "Exceed max nids per peer"
780         #do_lnetctl peer add --prim_nid 1.1.1.1@tcp --nid 1.1.1.[2-255]@tcp && error "Command should have failed"
781         return 0
782 }
783 run_test 99a "Check various invalid inputs to lnetctl peer add"
784
785 test_99b() {
786         reinit_dlc || return $?
787
788         create_base_yaml_file
789
790         cat <<EOF > $TMP/sanity-lnet-$testnum-invalid.yaml
791 peer:
792     - primary nid: 99.99.99.99@tcp
793       Multi-Rail: Foobar
794       peer ni:
795         - nid: 99.99.99.99@tcp
796 EOF
797         do_lnetctl import < $TMP/sanity-lnet-$testnum-invalid.yaml &&
798                 error "import should have failed"
799         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-actual.yaml
800         compare_yaml_files
801 }
802 run_test 99b "Invalid value for Multi-Rail in yaml import"
803
804 have_interface() {
805         local if="$1"
806         local ip=$(ip addr show dev $if | awk '/ inet /{print $2}')
807         [[ -n $ip ]]
808 }
809
810 add_net() {
811         local net="$1"
812         local if="$2"
813
814         reinit_dlc || return $?
815         load_module ../lnet/klnds/socklnd/ksocklnd ||
816                 error "Can't load ksocklnd.ko"
817         do_lnetctl net add --net ${net} --if ${if} ||
818                 error "Failed to add net ${net} on if ${if}"
819 }
820
821 compare_route_add() {
822         local rnet="$1"
823         local gw="$2"
824
825         local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
826
827         do_lnetctl route add --net ${rnet} --gateway ${gw} ||
828                 error "route add failed $?"
829         # CPT configuration is pruned from the exported yaml, since the default
830         # can vary across test systems (unlike default values for things like
831         # peer_credits, peer_timeout, etc.)
832         $LNETCTL export --backup | grep -v CPT > $actual ||
833                 error "export failed $?"
834         validate_gateway_nids
835         return $?
836 }
837
838 test_100() {
839         have_interface "eth0" || skip "Need eth0 interface with ipv4 configured"
840         add_net "tcp" "eth0"
841         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
842 net:
843     - net type: tcp
844       local NI(s):
845         - interfaces:
846               0: eth0
847           tunables:
848               peer_timeout: 180
849               peer_credits: 8
850               peer_buffer_credits: 0
851               credits: 256
852 route:
853     - net: tcp7
854       gateway: 7.7.7.7@tcp
855       hop: -1
856       priority: 0
857       health_sensitivity: 1
858 peer:
859     - primary nid: 7.7.7.7@tcp
860       Multi-Rail: True
861       peer ni:
862         - nid: 7.7.7.7@tcp
863 EOF
864         append_global_yaml
865         compare_route_add "tcp7" "7.7.7.7@tcp" || return $?
866         compare_yaml_files
867 }
868 run_test 100 "Add route with single gw (tcp)"
869
870 test_101() {
871         have_interface "eth0" || skip "Need eth0 interface with ipv4 configured"
872         add_net "tcp" "eth0"
873         cat <<EOF > $TMP/sanity-lnet-$testnum-expected.yaml
874 net:
875     - net type: tcp
876       local NI(s):
877         - interfaces:
878               0: eth0
879           tunables:
880               peer_timeout: 180
881               peer_credits: 8
882               peer_buffer_credits: 0
883               credits: 256
884 route:
885     - net: tcp8
886       gateway: 8.8.8.10@tcp
887       hop: -1
888       priority: 0
889       health_sensitivity: 1
890     - net: tcp8
891       gateway: 8.8.8.9@tcp
892       hop: -1
893       priority: 0
894       health_sensitivity: 1
895     - net: tcp8
896       gateway: 8.8.8.8@tcp
897       hop: -1
898       priority: 0
899       health_sensitivity: 1
900 peer:
901     - primary nid: 8.8.8.9@tcp
902       Multi-Rail: True
903       peer ni:
904         - nid: 8.8.8.9@tcp
905     - primary nid: 8.8.8.10@tcp
906       Multi-Rail: True
907       peer ni:
908         - nid: 8.8.8.10@tcp
909     - primary nid: 8.8.8.8@tcp
910       Multi-Rail: True
911       peer ni:
912         - nid: 8.8.8.8@tcp
913 EOF
914         append_global_yaml
915         compare_route_add "tcp8" "8.8.8.[8-10]@tcp"
916 }
917 run_test 101 "Add route with multiple gw (tcp)"
918
919 compare_route_del() {
920         local rnet="$1"
921         local gw="$2"
922
923         local actual="$TMP/sanity-lnet-$testnum-actual.yaml"
924
925         do_lnetctl route del --net ${rnet} --gateway ${gw} ||
926                 error "route del failed $?"
927         $LNETCTL export --backup > $actual ||
928                 error "export failed $?"
929         validate_gateway_nids
930 }
931
932 test_102() {
933         have_interface "eth0" || skip "Need eth0 interface with ipv4 configured"
934         add_net "tcp" "eth0"
935         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml
936         do_lnetctl route add --net tcp102 --gateway 102.102.102.102@tcp ||
937                 error "route add failed $?"
938         compare_route_del "tcp102" "102.102.102.102@tcp"
939 }
940 run_test 102 "Delete route with single gw (tcp)"
941
942 test_103() {
943         have_interface "eth0" || skip "Need eth0 interface with ipv4 configured"
944         add_net "tcp" "eth0"
945         $LNETCTL export --backup > $TMP/sanity-lnet-$testnum-expected.yaml
946         do_lnetctl route add --net tcp103 \
947                 --gateway 103.103.103.[103-120/4]@tcp ||
948                 error "route add failed $?"
949         compare_route_del "tcp103" "103.103.103.[103-120/4]@tcp"
950 }
951 run_test 103 "Delete route with multiple gw (tcp)"
952
953 ### load lnet in default namespace, configure in target namespace
954
955 test_200() {
956         cleanup_lnet || exit 1
957         load_lnet "networks=\"\""
958         do_ns $LNETCTL lnet configure --all || exit 1
959         $LNETCTL net show --net tcp | grep -q "nid: ${FAKE_IP}@tcp$"
960 }
961 run_test 200 "load lnet w/o module option, configure in a non-default namespace"
962
963 test_201() {
964         cleanup_lnet || exit 1
965         load_lnet "networks=tcp($FAKE_IF)"
966         do_ns $LNETCTL lnet configure --all || exit 1
967         $LNETCTL net show --net tcp | grep -q "nid: ${FAKE_IP}@tcp$"
968 }
969 run_test 201 "load lnet using networks module options in a non-default namespace"
970
971 test_202() {
972         cleanup_lnet || exit 1
973         load_lnet "networks=\"\" ip2nets=\"tcp0($FAKE_IF) ${FAKE_IP}\""
974         do_ns $LNETCTL lnet configure --all || exit 1
975         $LNETCTL net show | grep -q "nid: ${FAKE_IP}@tcp$"
976 }
977 run_test 202 "load lnet using ip2nets in a non-default namespace"
978
979
980 ### Add the interfaces in the target namespace
981
982 test_203() {
983         cleanup_lnet || exit 1
984         load_lnet
985         do_lnetctl lnet configure || exit 1
986         do_ns $LNETCTL net add --net tcp0 --if $FAKE_IF
987 }
988 run_test 203 "add a network using an interface in the non-default namespace"
989
990 complete $SECONDS
991
992 cleanup_testsuite
993 exit_status