Whamcloud - gitweb
LU-4423 lnet: Better cookie gen
[fs/lustre-release.git] / lustre / scripts / lustre_routes_config
1 # !/bin/bash
2 #
3 # lustre_route_config
4 # This script configures lnet with the routes in the passed in file.
5 # The routes should be in the following format:
6 # <network>: { gateway: <gateway>@<exit network> [hop: <hop>] [priority: <prioirty>] }
7 #
8 # Examples:
9 # tcp1: { gateway: 10.1.1.2@tcp0, priority: 3 }
10 # tcp4: { gateway: 10.3.3.4@tcp }
11 # tcp6: { gateway: 10.3.3.6@tcp, hop: 2, priority: 5 }
12 # tcp7: { gateway: 10.3.3.[6-12]@tcp, priority: 20, hop: 8 }
13 #
14 # The purpose of this script is to circumvent the limitation on the number of
15 # routes which could be configured through the lustre.conf module parameters.
16 #
17 ###########################################################################
18
19
20 progname=$(basename $0)
21
22 # print usage
23 usage() {
24         cat <<- USAGE
25         Setup or cleanup LNET routes from specified config file"
26         usage: $progname [--setup|--cleanup|--dry-run|--verbose]
27         <config_file>
28                 --setup: configure routes listed in config_file
29                 --cleanup: unconfigure routes listed in config_file
30                 --dry-run: echo commands to be run, but do not execute them
31                 --verbose: echo commands before they are executed
32         USAGE
33 }
34
35 # Set default paramters
36 CMD=add_route
37 VERBOSE=false
38 EXEC=true
39
40 # sanity check
41 [ -z "$1" ] && usage && exit 1
42
43 # check parameters
44 while [ ! -f "$1" ]; do
45         case "$1" in
46         -c|--cleanup) CMD=del_route; shift ;;
47         -h|--help)    usage; exit 0 ;;
48         -n|--dry-run) EXEC=false; VERBOSE=true; shift ;;
49         -s|--setup)   CMD=add_route; shift ;;
50         -v|--verbose) VERBOSE=true; shift ;;
51         *)            usage; exit 1 ;;
52         esac
53 done
54
55 # Usage: do_lctl <params>
56 # execut the command and/or print if verbose is set
57 do_lctl() {
58         local RC=0
59
60         $VERBOSE && echo "lctl $@"
61         if $EXEC; then
62                 lctl "$@"
63                 RC=$?
64         fi
65
66         return $RC
67 }
68
69 # Usage: find_arg_value <array> <arg>
70 find_arg_value() {
71         local i=0
72         local value=""
73         local arg="$2"
74
75         declare -a array=("${!1}")
76         for ((i = 0; i < ${#array[@]}; i++)); do
77                 if [ "${array[$i]}" == "$arg" ]; then
78                         value="${array[$((i + 1))]}"
79                         break
80                 fi
81         done
82         echo -n $value
83 }
84
85 while read line; do
86         # Parse line using ':' and ',' as delimiters and ignoring all
87         # white space, tabs and linefeed
88         IFS="$IFS:,"
89         params=($line)
90
91         # get the mandatory parameters: network and gateway
92         # If either is not present skip that line
93         network=${params[0]}
94         OBR=${params[1]}
95         GATE=${params[2]}
96         gateway=${params[3]}
97
98         if [ -z $network ] || [ -z $gateway ] ||
99            [ $GATE != "gateway" ]; then
100                 continue
101         fi
102
103         case "$CMD" in
104                 add_route)
105                         baselctl="--net $network add_route $gateway"
106
107                         # walk through the optional params until you hit
108                         # the closing brace.  Build an associative db:
109                         # option=value
110                         i=4
111                         while [ $i -lt ${#params[@]} ]; do
112                                 option=${params[$i]}
113                                 if [ "$option" == "}" ]; then
114                                         break
115                                 fi
116                                 outoptions[$i]=$option
117                                 ((i++))
118                                 value=${params[$i]}
119                                 outoptions[$i]=$value
120                                 ((i++))
121                         done
122
123                         # find the hop and priority
124                         # This can be expanded later on if we add extra
125                         # parameters
126                         # NOTE: the order between hop and priority is not
127                         # enforced.  It's also possible to add hop without
128                         # prio or prio without hop
129                         priority=$(find_arg_value outoptions[@] "priority")
130                         hop=$(find_arg_value outoptions[@] "hop")
131                         if [ -n "$priority" ] && [ -z "$hop" ]; then
132                                 baselctl+=" 1 $priority"
133                         else
134                                 baselctl+=" $hop $priority"
135                         fi
136                         ;;
137                 del_route)
138                         baselctl="del_route $gateway"
139         esac
140
141         do_lctl $baselctl
142 done < "$1"