Ido Schimmel | 73bae67 | 2018-02-28 12:25:06 +0200 | [diff] [blame^] | 1 | #!/bin/bash |
| 2 | # SPDX-License-Identifier: GPL-2.0 |
| 3 | |
| 4 | ############################################################################## |
| 5 | # Defines |
| 6 | |
| 7 | # Can be overridden by the configuration file. |
| 8 | PING=${PING:=ping} |
| 9 | PING6=${PING6:=ping6} |
| 10 | WAIT_TIME=${WAIT_TIME:=5} |
| 11 | PAUSE_ON_FAIL=${PAUSE_ON_FAIL:=no} |
| 12 | PAUSE_ON_CLEANUP=${PAUSE_ON_CLEANUP:=no} |
| 13 | |
| 14 | if [[ -f forwarding.config ]]; then |
| 15 | source forwarding.config |
| 16 | fi |
| 17 | |
| 18 | ############################################################################## |
| 19 | # Sanity checks |
| 20 | |
| 21 | if [[ "$(id -u)" -ne 0 ]]; then |
| 22 | echo "SKIP: need root privileges" |
| 23 | exit 0 |
| 24 | fi |
| 25 | |
| 26 | tc -j &> /dev/null |
| 27 | if [[ $? -ne 0 ]]; then |
| 28 | echo "SKIP: iproute2 too old, missing JSON support" |
| 29 | exit 0 |
| 30 | fi |
| 31 | |
| 32 | if [[ ! -x "$(command -v jq)" ]]; then |
| 33 | echo "SKIP: jq not installed" |
| 34 | exit 0 |
| 35 | fi |
| 36 | |
| 37 | if [[ ! -v NUM_NETIFS ]]; then |
| 38 | echo "SKIP: importer does not define \"NUM_NETIFS\"" |
| 39 | exit 0 |
| 40 | fi |
| 41 | |
| 42 | ############################################################################## |
| 43 | # Network interfaces configuration |
| 44 | |
| 45 | for i in $(eval echo {1..$NUM_NETIFS}); do |
| 46 | ip link show dev ${NETIFS[p$i]} &> /dev/null |
| 47 | if [[ $? -ne 0 ]]; then |
| 48 | echo "SKIP: could not find all required interfaces" |
| 49 | exit 0 |
| 50 | fi |
| 51 | done |
| 52 | |
| 53 | ############################################################################## |
| 54 | # Helpers |
| 55 | |
| 56 | # Exit status to return at the end. Set in case one of the tests fails. |
| 57 | EXIT_STATUS=0 |
| 58 | # Per-test return value. Clear at the beginning of each test. |
| 59 | RET=0 |
| 60 | |
| 61 | check_err() |
| 62 | { |
| 63 | local err=$1 |
| 64 | local msg=$2 |
| 65 | |
| 66 | if [[ $RET -eq 0 && $err -ne 0 ]]; then |
| 67 | RET=$err |
| 68 | retmsg=$msg |
| 69 | fi |
| 70 | } |
| 71 | |
| 72 | check_fail() |
| 73 | { |
| 74 | local err=$1 |
| 75 | local msg=$2 |
| 76 | |
| 77 | if [[ $RET -eq 0 && $err -eq 0 ]]; then |
| 78 | RET=1 |
| 79 | retmsg=$msg |
| 80 | fi |
| 81 | } |
| 82 | |
| 83 | log_test() |
| 84 | { |
| 85 | local test_name=$1 |
| 86 | local opt_str=$2 |
| 87 | |
| 88 | if [[ $# -eq 2 ]]; then |
| 89 | opt_str="($opt_str)" |
| 90 | fi |
| 91 | |
| 92 | if [[ $RET -ne 0 ]]; then |
| 93 | EXIT_STATUS=1 |
| 94 | printf "TEST: %-60s [FAIL]\n" "$test_name $opt_str" |
| 95 | if [[ ! -z "$retmsg" ]]; then |
| 96 | printf "\t%s\n" "$retmsg" |
| 97 | fi |
| 98 | if [ "${PAUSE_ON_FAIL}" = "yes" ]; then |
| 99 | echo "Hit enter to continue, 'q' to quit" |
| 100 | read a |
| 101 | [ "$a" = "q" ] && exit 1 |
| 102 | fi |
| 103 | return 1 |
| 104 | fi |
| 105 | |
| 106 | printf "TEST: %-60s [PASS]\n" "$test_name $opt_str" |
| 107 | return 0 |
| 108 | } |
| 109 | |
| 110 | setup_wait() |
| 111 | { |
| 112 | for i in $(eval echo {1..$NUM_NETIFS}); do |
| 113 | while true; do |
| 114 | ip link show dev ${NETIFS[p$i]} up \ |
| 115 | | grep 'state UP' &> /dev/null |
| 116 | if [[ $? -ne 0 ]]; then |
| 117 | sleep 1 |
| 118 | else |
| 119 | break |
| 120 | fi |
| 121 | done |
| 122 | done |
| 123 | |
| 124 | # Make sure links are ready. |
| 125 | sleep $WAIT_TIME |
| 126 | } |
| 127 | |
| 128 | pre_cleanup() |
| 129 | { |
| 130 | if [ "${PAUSE_ON_CLEANUP}" = "yes" ]; then |
| 131 | echo "Pausing before cleanup, hit any key to continue" |
| 132 | read |
| 133 | fi |
| 134 | } |
| 135 | |
| 136 | vrf_prepare() |
| 137 | { |
| 138 | ip -4 rule add pref 32765 table local |
| 139 | ip -4 rule del pref 0 |
| 140 | ip -6 rule add pref 32765 table local |
| 141 | ip -6 rule del pref 0 |
| 142 | } |
| 143 | |
| 144 | vrf_cleanup() |
| 145 | { |
| 146 | ip -6 rule add pref 0 table local |
| 147 | ip -6 rule del pref 32765 |
| 148 | ip -4 rule add pref 0 table local |
| 149 | ip -4 rule del pref 32765 |
| 150 | } |
| 151 | |
| 152 | __last_tb_id=0 |
| 153 | declare -A __TB_IDS |
| 154 | |
| 155 | __vrf_td_id_assign() |
| 156 | { |
| 157 | local vrf_name=$1 |
| 158 | |
| 159 | __last_tb_id=$((__last_tb_id + 1)) |
| 160 | __TB_IDS[$vrf_name]=$__last_tb_id |
| 161 | return $__last_tb_id |
| 162 | } |
| 163 | |
| 164 | __vrf_td_id_lookup() |
| 165 | { |
| 166 | local vrf_name=$1 |
| 167 | |
| 168 | return ${__TB_IDS[$vrf_name]} |
| 169 | } |
| 170 | |
| 171 | vrf_create() |
| 172 | { |
| 173 | local vrf_name=$1 |
| 174 | local tb_id |
| 175 | |
| 176 | __vrf_td_id_assign $vrf_name |
| 177 | tb_id=$? |
| 178 | |
| 179 | ip link add dev $vrf_name type vrf table $tb_id |
| 180 | ip -4 route add table $tb_id unreachable default metric 4278198272 |
| 181 | ip -6 route add table $tb_id unreachable default metric 4278198272 |
| 182 | } |
| 183 | |
| 184 | vrf_destroy() |
| 185 | { |
| 186 | local vrf_name=$1 |
| 187 | local tb_id |
| 188 | |
| 189 | __vrf_td_id_lookup $vrf_name |
| 190 | tb_id=$? |
| 191 | |
| 192 | ip -6 route del table $tb_id unreachable default metric 4278198272 |
| 193 | ip -4 route del table $tb_id unreachable default metric 4278198272 |
| 194 | ip link del dev $vrf_name |
| 195 | } |
| 196 | |
| 197 | __addr_add_del() |
| 198 | { |
| 199 | local if_name=$1 |
| 200 | local add_del=$2 |
| 201 | local array |
| 202 | |
| 203 | shift |
| 204 | shift |
| 205 | array=("${@}") |
| 206 | |
| 207 | for addrstr in "${array[@]}"; do |
| 208 | ip address $add_del $addrstr dev $if_name |
| 209 | done |
| 210 | } |
| 211 | |
| 212 | simple_if_init() |
| 213 | { |
| 214 | local if_name=$1 |
| 215 | local vrf_name |
| 216 | local array |
| 217 | |
| 218 | shift |
| 219 | vrf_name=v$if_name |
| 220 | array=("${@}") |
| 221 | |
| 222 | vrf_create $vrf_name |
| 223 | ip link set dev $if_name master $vrf_name |
| 224 | ip link set dev $vrf_name up |
| 225 | ip link set dev $if_name up |
| 226 | |
| 227 | __addr_add_del $if_name add "${array[@]}" |
| 228 | } |
| 229 | |
| 230 | simple_if_fini() |
| 231 | { |
| 232 | local if_name=$1 |
| 233 | local vrf_name |
| 234 | local array |
| 235 | |
| 236 | shift |
| 237 | vrf_name=v$if_name |
| 238 | array=("${@}") |
| 239 | |
| 240 | __addr_add_del $if_name del "${array[@]}" |
| 241 | |
| 242 | ip link set dev $if_name down |
| 243 | vrf_destroy $vrf_name |
| 244 | } |
| 245 | |
| 246 | master_name_get() |
| 247 | { |
| 248 | local if_name=$1 |
| 249 | |
| 250 | ip -j link show dev $if_name | jq -r '.[]["master"]' |
| 251 | } |
| 252 | |
| 253 | ############################################################################## |
| 254 | # Tests |
| 255 | |
| 256 | ping_test() |
| 257 | { |
| 258 | local if_name=$1 |
| 259 | local dip=$2 |
| 260 | local vrf_name |
| 261 | |
| 262 | RET=0 |
| 263 | |
| 264 | vrf_name=$(master_name_get $if_name) |
| 265 | ip vrf exec $vrf_name $PING $dip -c 10 -i 0.1 -w 2 &> /dev/null |
| 266 | check_err $? |
| 267 | log_test "ping" |
| 268 | } |
| 269 | |
| 270 | ping6_test() |
| 271 | { |
| 272 | local if_name=$1 |
| 273 | local dip=$2 |
| 274 | local vrf_name |
| 275 | |
| 276 | RET=0 |
| 277 | |
| 278 | vrf_name=$(master_name_get $if_name) |
| 279 | ip vrf exec $vrf_name $PING6 $dip -c 10 -i 0.1 -w 2 &> /dev/null |
| 280 | check_err $? |
| 281 | log_test "ping6" |
| 282 | } |