1#!/bin/bash 2# SPDX-License-Identifier: GPL-2.0 3# 4# ns: me | ns: peer | ns: remote 5# 2001:db8:91::1 | 2001:db8:91::2 | 6# 172.16.1.1 | 172.16.1.2 | 7# veth1 <---|---> veth2 | 8# | veth5 <--|--> veth6 172.16.101.1 9# veth3 <---|---> veth4 | 2001:db8:101::1 10# 172.16.2.1 | 172.16.2.2 | 11# 2001:db8:92::1 | 2001:db8:92::2 | 12# 13# This test is for checking IPv4 and IPv6 FIB behavior with nexthop 14# objects. Device reference counts and network namespace cleanup tested 15# by use of network namespace for peer. 16 17ret=0 18# Kselftest framework requirement - SKIP code is 4. 19ksft_skip=4 20 21# all tests in this script. Can be overridden with -t option 22IPV4_TESTS="ipv4_fcnal ipv4_grp_fcnal ipv4_withv6_fcnal ipv4_fcnal_runtime" 23IPV6_TESTS="ipv6_fcnal ipv6_grp_fcnal ipv6_fcnal_runtime" 24 25ALL_TESTS="basic ${IPV4_TESTS} ${IPV6_TESTS}" 26TESTS="${ALL_TESTS}" 27VERBOSE=0 28PAUSE_ON_FAIL=no 29PAUSE=no 30 31nsid=100 32 33################################################################################ 34# utilities 35 36log_test() 37{ 38 local rc=$1 39 local expected=$2 40 local msg="$3" 41 42 if [ ${rc} -eq ${expected} ]; then 43 printf "TEST: %-60s [ OK ]\n" "${msg}" 44 nsuccess=$((nsuccess+1)) 45 else 46 ret=1 47 nfail=$((nfail+1)) 48 printf "TEST: %-60s [FAIL]\n" "${msg}" 49 if [ "$VERBOSE" = "1" ]; then 50 echo " rc=$rc, expected $expected" 51 fi 52 53 if [ "${PAUSE_ON_FAIL}" = "yes" ]; then 54 echo 55 echo "hit enter to continue, 'q' to quit" 56 read a 57 [ "$a" = "q" ] && exit 1 58 fi 59 fi 60 61 if [ "${PAUSE}" = "yes" ]; then 62 echo 63 echo "hit enter to continue, 'q' to quit" 64 read a 65 [ "$a" = "q" ] && exit 1 66 fi 67 68 [ "$VERBOSE" = "1" ] && echo 69} 70 71run_cmd() 72{ 73 local cmd="$1" 74 local out 75 local stderr="2>/dev/null" 76 77 if [ "$VERBOSE" = "1" ]; then 78 printf "COMMAND: $cmd\n" 79 stderr= 80 fi 81 82 out=$(eval $cmd $stderr) 83 rc=$? 84 if [ "$VERBOSE" = "1" -a -n "$out" ]; then 85 echo " $out" 86 fi 87 88 return $rc 89} 90 91get_linklocal() 92{ 93 local dev=$1 94 local ns 95 local addr 96 97 [ -n "$2" ] && ns="-netns $2" 98 addr=$(ip $ns -6 -br addr show dev ${dev} | \ 99 awk '{ 100 for (i = 3; i <= NF; ++i) { 101 if ($i ~ /^fe80/) 102 print $i 103 } 104 }' 105 ) 106 addr=${addr/\/*} 107 108 [ -z "$addr" ] && return 1 109 110 echo $addr 111 112 return 0 113} 114 115create_ns() 116{ 117 local n=${1} 118 119 ip netns del ${n} 2>/dev/null 120 121 set -e 122 ip netns add ${n} 123 ip netns set ${n} $((nsid++)) 124 ip -netns ${n} addr add 127.0.0.1/8 dev lo 125 ip -netns ${n} link set lo up 126 127 ip netns exec ${n} sysctl -qw net.ipv4.ip_forward=1 128 ip netns exec ${n} sysctl -qw net.ipv4.fib_multipath_use_neigh=1 129 ip netns exec ${n} sysctl -qw net.ipv4.conf.default.ignore_routes_with_linkdown=1 130 ip netns exec ${n} sysctl -qw net.ipv6.conf.all.keep_addr_on_down=1 131 ip netns exec ${n} sysctl -qw net.ipv6.conf.all.forwarding=1 132 ip netns exec ${n} sysctl -qw net.ipv6.conf.default.forwarding=1 133 ip netns exec ${n} sysctl -qw net.ipv6.conf.default.ignore_routes_with_linkdown=1 134 ip netns exec ${n} sysctl -qw net.ipv6.conf.all.accept_dad=0 135 ip netns exec ${n} sysctl -qw net.ipv6.conf.default.accept_dad=0 136 137 set +e 138} 139 140setup() 141{ 142 cleanup 143 144 create_ns me 145 create_ns peer 146 create_ns remote 147 148 IP="ip -netns me" 149 set -e 150 $IP li add veth1 type veth peer name veth2 151 $IP li set veth1 up 152 $IP addr add 172.16.1.1/24 dev veth1 153 $IP -6 addr add 2001:db8:91::1/64 dev veth1 154 155 $IP li add veth3 type veth peer name veth4 156 $IP li set veth3 up 157 $IP addr add 172.16.2.1/24 dev veth3 158 $IP -6 addr add 2001:db8:92::1/64 dev veth3 159 160 $IP li set veth2 netns peer up 161 ip -netns peer addr add 172.16.1.2/24 dev veth2 162 ip -netns peer -6 addr add 2001:db8:91::2/64 dev veth2 163 164 $IP li set veth4 netns peer up 165 ip -netns peer addr add 172.16.2.2/24 dev veth4 166 ip -netns peer -6 addr add 2001:db8:92::2/64 dev veth4 167 168 ip -netns remote li add veth5 type veth peer name veth6 169 ip -netns remote li set veth5 up 170 ip -netns remote addr add dev veth5 172.16.101.1/24 171 ip -netns remote addr add dev veth5 2001:db8:101::1/64 172 ip -netns remote ro add 172.16.0.0/22 via 172.16.101.2 173 ip -netns remote -6 ro add 2001:db8:90::/40 via 2001:db8:101::2 174 175 ip -netns remote li set veth6 netns peer up 176 ip -netns peer addr add dev veth6 172.16.101.2/24 177 ip -netns peer addr add dev veth6 2001:db8:101::2/64 178 set +e 179} 180 181cleanup() 182{ 183 local ns 184 185 for ns in me peer remote; do 186 ip netns del ${ns} 2>/dev/null 187 done 188} 189 190check_output() 191{ 192 local out="$1" 193 local expected="$2" 194 local rc=0 195 196 [ "${out}" = "${expected}" ] && return 0 197 198 if [ -z "${out}" ]; then 199 if [ "$VERBOSE" = "1" ]; then 200 printf "\nNo entry found\n" 201 printf "Expected:\n" 202 printf " ${expected}\n" 203 fi 204 return 1 205 fi 206 207 out=$(echo ${out}) 208 if [ "${out}" != "${expected}" ]; then 209 rc=1 210 if [ "${VERBOSE}" = "1" ]; then 211 printf " Unexpected entry. Have:\n" 212 printf " ${out}\n" 213 printf " Expected:\n" 214 printf " ${expected}\n\n" 215 else 216 echo " WARNING: Unexpected route entry" 217 fi 218 fi 219 220 return $rc 221} 222 223check_nexthop() 224{ 225 local nharg="$1" 226 local expected="$2" 227 local out 228 229 out=$($IP nexthop ls ${nharg} 2>/dev/null) 230 231 check_output "${out}" "${expected}" 232} 233 234check_route() 235{ 236 local pfx="$1" 237 local expected="$2" 238 local out 239 240 out=$($IP route ls match ${pfx} 2>/dev/null) 241 242 check_output "${out}" "${expected}" 243} 244 245check_route6() 246{ 247 local pfx="$1" 248 local expected="$2" 249 local out 250 251 out=$($IP -6 route ls match ${pfx} 2>/dev/null) 252 253 check_output "${out}" "${expected}" 254} 255 256################################################################################ 257# basic operations (add, delete, replace) on nexthops and nexthop groups 258# 259# IPv6 260 261ipv6_fcnal() 262{ 263 local rc 264 265 echo 266 echo "IPv6" 267 echo "----------------------" 268 269 run_cmd "$IP nexthop add id 52 via 2001:db8:91::2 dev veth1" 270 rc=$? 271 log_test $rc 0 "Create nexthop with id, gw, dev" 272 if [ $rc -ne 0 ]; then 273 echo "Basic IPv6 create fails; can not continue" 274 return 1 275 fi 276 277 run_cmd "$IP nexthop get id 52" 278 log_test $? 0 "Get nexthop by id" 279 check_nexthop "id 52" "id 52 via 2001:db8:91::2 dev veth1 scope link" 280 281 run_cmd "$IP nexthop del id 52" 282 log_test $? 0 "Delete nexthop by id" 283 check_nexthop "id 52" "" 284 285 # 286 # gw, device spec 287 # 288 # gw validation, no device - fails since dev required 289 run_cmd "$IP nexthop add id 52 via 2001:db8:92::3" 290 log_test $? 2 "Create nexthop - gw only" 291 292 # gw is not reachable throught given dev 293 run_cmd "$IP nexthop add id 53 via 2001:db8:3::3 dev veth1" 294 log_test $? 2 "Create nexthop - invalid gw+dev combination" 295 296 # onlink arg overrides gw+dev lookup 297 run_cmd "$IP nexthop add id 53 via 2001:db8:3::3 dev veth1 onlink" 298 log_test $? 0 "Create nexthop - gw+dev and onlink" 299 300 # admin down should delete nexthops 301 set -e 302 run_cmd "$IP -6 nexthop add id 55 via 2001:db8:91::3 dev veth1" 303 run_cmd "$IP nexthop add id 56 via 2001:db8:91::4 dev veth1" 304 run_cmd "$IP nexthop add id 57 via 2001:db8:91::5 dev veth1" 305 run_cmd "$IP li set dev veth1 down" 306 set +e 307 check_nexthop "dev veth1" "" 308 log_test $? 0 "Nexthops removed on admin down" 309} 310 311ipv6_grp_fcnal() 312{ 313 local rc 314 315 echo 316 echo "IPv6 groups functional" 317 echo "----------------------" 318 319 # basic functionality: create a nexthop group, default weight 320 run_cmd "$IP nexthop add id 61 via 2001:db8:91::2 dev veth1" 321 run_cmd "$IP nexthop add id 101 group 61" 322 log_test $? 0 "Create nexthop group with single nexthop" 323 324 # get nexthop group 325 run_cmd "$IP nexthop get id 101" 326 log_test $? 0 "Get nexthop group by id" 327 check_nexthop "id 101" "id 101 group 61" 328 329 # delete nexthop group 330 run_cmd "$IP nexthop del id 101" 331 log_test $? 0 "Delete nexthop group by id" 332 check_nexthop "id 101" "" 333 334 $IP nexthop flush >/dev/null 2>&1 335 check_nexthop "id 101" "" 336 337 # 338 # create group with multiple nexthops - mix of gw and dev only 339 # 340 run_cmd "$IP nexthop add id 62 via 2001:db8:91::2 dev veth1" 341 run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1" 342 run_cmd "$IP nexthop add id 64 via 2001:db8:91::4 dev veth1" 343 run_cmd "$IP nexthop add id 65 dev veth1" 344 run_cmd "$IP nexthop add id 102 group 62/63/64/65" 345 log_test $? 0 "Nexthop group with multiple nexthops" 346 check_nexthop "id 102" "id 102 group 62/63/64/65" 347 348 # Delete nexthop in a group and group is updated 349 run_cmd "$IP nexthop del id 63" 350 check_nexthop "id 102" "id 102 group 62/64/65" 351 log_test $? 0 "Nexthop group updated when entry is deleted" 352 353 # create group with multiple weighted nexthops 354 run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1" 355 run_cmd "$IP nexthop add id 103 group 62/63,2/64,3/65,4" 356 log_test $? 0 "Nexthop group with weighted nexthops" 357 check_nexthop "id 103" "id 103 group 62/63,2/64,3/65,4" 358 359 # Delete nexthop in a weighted group and group is updated 360 run_cmd "$IP nexthop del id 63" 361 check_nexthop "id 103" "id 103 group 62/64,3/65,4" 362 log_test $? 0 "Weighted nexthop group updated when entry is deleted" 363 364 # admin down - nexthop is removed from group 365 run_cmd "$IP li set dev veth1 down" 366 check_nexthop "dev veth1" "" 367 log_test $? 0 "Nexthops in groups removed on admin down" 368 369 # expect groups to have been deleted as well 370 check_nexthop "" "" 371 372 run_cmd "$IP li set dev veth1 up" 373 374 $IP nexthop flush >/dev/null 2>&1 375 376 # group with nexthops using different devices 377 set -e 378 run_cmd "$IP nexthop add id 62 via 2001:db8:91::2 dev veth1" 379 run_cmd "$IP nexthop add id 63 via 2001:db8:91::3 dev veth1" 380 run_cmd "$IP nexthop add id 64 via 2001:db8:91::4 dev veth1" 381 run_cmd "$IP nexthop add id 65 via 2001:db8:91::5 dev veth1" 382 383 run_cmd "$IP nexthop add id 72 via 2001:db8:92::2 dev veth3" 384 run_cmd "$IP nexthop add id 73 via 2001:db8:92::3 dev veth3" 385 run_cmd "$IP nexthop add id 74 via 2001:db8:92::4 dev veth3" 386 run_cmd "$IP nexthop add id 75 via 2001:db8:92::5 dev veth3" 387 set +e 388 389 # multiple groups with same nexthop 390 run_cmd "$IP nexthop add id 104 group 62" 391 run_cmd "$IP nexthop add id 105 group 62" 392 check_nexthop "group" "id 104 group 62 id 105 group 62" 393 log_test $? 0 "Multiple groups with same nexthop" 394 395 run_cmd "$IP nexthop flush groups" 396 [ $? -ne 0 ] && return 1 397 398 # on admin down of veth1, it should be removed from the group 399 run_cmd "$IP nexthop add id 105 group 62/63/72/73/64" 400 run_cmd "$IP li set veth1 down" 401 check_nexthop "id 105" "id 105 group 72/73" 402 log_test $? 0 "Nexthops in group removed on admin down - mixed group" 403 404 run_cmd "$IP nexthop add id 106 group 105/74" 405 log_test $? 2 "Nexthop group can not have a group as an entry" 406 407 # a group can have a blackhole entry only if it is the only 408 # nexthop in the group. Needed for atomic replace with an 409 # actual nexthop group 410 run_cmd "$IP -6 nexthop add id 31 blackhole" 411 run_cmd "$IP nexthop add id 107 group 31" 412 log_test $? 0 "Nexthop group with a blackhole entry" 413 414 run_cmd "$IP nexthop add id 108 group 31/24" 415 log_test $? 2 "Nexthop group can not have a blackhole and another nexthop" 416} 417 418ipv6_fcnal_runtime() 419{ 420 local rc 421 422 echo 423 echo "IPv6 functional runtime" 424 echo "-----------------------" 425 426 sleep 5 427 428 # 429 # IPv6 - the basics 430 # 431 run_cmd "$IP nexthop add id 81 via 2001:db8:91::2 dev veth1" 432 run_cmd "$IP ro add 2001:db8:101::1/128 nhid 81" 433 log_test $? 0 "Route add" 434 435 run_cmd "$IP ro delete 2001:db8:101::1/128 nhid 81" 436 log_test $? 0 "Route delete" 437 438 run_cmd "$IP ro add 2001:db8:101::1/128 nhid 81" 439 run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1" 440 log_test $? 0 "Ping with nexthop" 441 442 run_cmd "$IP nexthop add id 82 via 2001:db8:92::2 dev veth3" 443 run_cmd "$IP nexthop add id 122 group 81/82" 444 run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 122" 445 run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1" 446 log_test $? 0 "Ping - multipath" 447 448 # 449 # IPv6 with blackhole nexthops 450 # 451 run_cmd "$IP -6 nexthop add id 83 blackhole" 452 run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 83" 453 run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1" 454 log_test $? 2 "Ping - blackhole" 455 456 run_cmd "$IP nexthop replace id 83 via 2001:db8:91::2 dev veth1" 457 run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1" 458 log_test $? 0 "Ping - blackhole replaced with gateway" 459 460 run_cmd "$IP -6 nexthop replace id 83 blackhole" 461 run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1" 462 log_test $? 2 "Ping - gateway replaced by blackhole" 463 464 run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 122" 465 run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1" 466 if [ $? -eq 0 ]; then 467 run_cmd "$IP nexthop replace id 122 group 83" 468 run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1" 469 log_test $? 2 "Ping - group with blackhole" 470 471 run_cmd "$IP nexthop replace id 122 group 81/82" 472 run_cmd "ip netns exec me ping -c1 -w1 2001:db8:101::1" 473 log_test $? 0 "Ping - group blackhole replaced with gateways" 474 else 475 log_test 2 0 "Ping - multipath failed" 476 fi 477 478 # 479 # device only and gw + dev only mix 480 # 481 run_cmd "$IP -6 nexthop add id 85 dev veth1" 482 run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 85" 483 log_test $? 0 "IPv6 route with device only nexthop" 484 check_route6 "2001:db8:101::1" "2001:db8:101::1 nhid 85 dev veth1 metric 1024 pref medium" 485 486 run_cmd "$IP nexthop add id 123 group 81/85" 487 run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 123" 488 log_test $? 0 "IPv6 multipath route with nexthop mix - dev only + gw" 489 check_route6 "2001:db8:101::1" "2001:db8:101::1 nhid 123 metric 1024 nexthop via 2001:db8:91::2 dev veth1 weight 1 nexthop dev veth1 weight 1 pref medium" 490 491 # 492 # IPv6 route with v4 nexthop - not allowed 493 # 494 run_cmd "$IP ro delete 2001:db8:101::1/128" 495 run_cmd "$IP nexthop add id 84 via 172.16.1.1 dev veth1" 496 run_cmd "$IP ro add 2001:db8:101::1/128 nhid 84" 497 log_test $? 2 "IPv6 route can not have a v4 gateway" 498 499 run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 81" 500 run_cmd "$IP nexthop replace id 81 via 172.16.1.1 dev veth1" 501 log_test $? 2 "Nexthop replace - v6 route, v4 nexthop" 502 503 run_cmd "$IP ro replace 2001:db8:101::1/128 nhid 122" 504 run_cmd "$IP nexthop replace id 81 via 172.16.1.1 dev veth1" 505 log_test $? 2 "Nexthop replace of group entry - v6 route, v4 nexthop" 506 507 $IP nexthop flush >/dev/null 2>&1 508 509 # 510 # weird IPv6 cases 511 # 512 run_cmd "$IP nexthop add id 86 via 2001:db8:91::2 dev veth1" 513 run_cmd "$IP ro add 2001:db8:101::1/128 nhid 81" 514 515 # TO-DO: 516 # existing route with old nexthop; append route with new nexthop 517 # existing route with old nexthop; replace route with new 518 # existing route with new nexthop; replace route with old 519 # route with src address and using nexthop - not allowed 520} 521 522ipv4_fcnal() 523{ 524 local rc 525 526 echo 527 echo "IPv4 functional" 528 echo "----------------------" 529 530 # 531 # basic IPv4 ops - add, get, delete 532 # 533 run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1" 534 rc=$? 535 log_test $rc 0 "Create nexthop with id, gw, dev" 536 if [ $rc -ne 0 ]; then 537 echo "Basic IPv4 create fails; can not continue" 538 return 1 539 fi 540 541 run_cmd "$IP nexthop get id 12" 542 log_test $? 0 "Get nexthop by id" 543 check_nexthop "id 12" "id 12 via 172.16.1.2 dev veth1 scope link" 544 545 run_cmd "$IP nexthop del id 12" 546 log_test $? 0 "Delete nexthop by id" 547 check_nexthop "id 52" "" 548 549 # 550 # gw, device spec 551 # 552 # gw validation, no device - fails since dev is required 553 run_cmd "$IP nexthop add id 12 via 172.16.2.3" 554 log_test $? 2 "Create nexthop - gw only" 555 556 # gw not reachable through given dev 557 run_cmd "$IP nexthop add id 13 via 172.16.3.2 dev veth1" 558 log_test $? 2 "Create nexthop - invalid gw+dev combination" 559 560 # onlink flag overrides gw+dev lookup 561 run_cmd "$IP nexthop add id 13 via 172.16.3.2 dev veth1 onlink" 562 log_test $? 0 "Create nexthop - gw+dev and onlink" 563 564 # admin down should delete nexthops 565 set -e 566 run_cmd "$IP nexthop add id 15 via 172.16.1.3 dev veth1" 567 run_cmd "$IP nexthop add id 16 via 172.16.1.4 dev veth1" 568 run_cmd "$IP nexthop add id 17 via 172.16.1.5 dev veth1" 569 run_cmd "$IP li set dev veth1 down" 570 set +e 571 check_nexthop "dev veth1" "" 572 log_test $? 0 "Nexthops removed on admin down" 573} 574 575ipv4_grp_fcnal() 576{ 577 local rc 578 579 echo 580 echo "IPv4 groups functional" 581 echo "----------------------" 582 583 # basic functionality: create a nexthop group, default weight 584 run_cmd "$IP nexthop add id 11 via 172.16.1.2 dev veth1" 585 run_cmd "$IP nexthop add id 101 group 11" 586 log_test $? 0 "Create nexthop group with single nexthop" 587 588 # get nexthop group 589 run_cmd "$IP nexthop get id 101" 590 log_test $? 0 "Get nexthop group by id" 591 check_nexthop "id 101" "id 101 group 11" 592 593 # delete nexthop group 594 run_cmd "$IP nexthop del id 101" 595 log_test $? 0 "Delete nexthop group by id" 596 check_nexthop "id 101" "" 597 598 $IP nexthop flush >/dev/null 2>&1 599 600 # 601 # create group with multiple nexthops 602 run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1" 603 run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1" 604 run_cmd "$IP nexthop add id 14 via 172.16.1.4 dev veth1" 605 run_cmd "$IP nexthop add id 15 via 172.16.1.5 dev veth1" 606 run_cmd "$IP nexthop add id 102 group 12/13/14/15" 607 log_test $? 0 "Nexthop group with multiple nexthops" 608 check_nexthop "id 102" "id 102 group 12/13/14/15" 609 610 # Delete nexthop in a group and group is updated 611 run_cmd "$IP nexthop del id 13" 612 check_nexthop "id 102" "id 102 group 12/14/15" 613 log_test $? 0 "Nexthop group updated when entry is deleted" 614 615 # create group with multiple weighted nexthops 616 run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1" 617 run_cmd "$IP nexthop add id 103 group 12/13,2/14,3/15,4" 618 log_test $? 0 "Nexthop group with weighted nexthops" 619 check_nexthop "id 103" "id 103 group 12/13,2/14,3/15,4" 620 621 # Delete nexthop in a weighted group and group is updated 622 run_cmd "$IP nexthop del id 13" 623 check_nexthop "id 103" "id 103 group 12/14,3/15,4" 624 log_test $? 0 "Weighted nexthop group updated when entry is deleted" 625 626 # admin down - nexthop is removed from group 627 run_cmd "$IP li set dev veth1 down" 628 check_nexthop "dev veth1" "" 629 log_test $? 0 "Nexthops in groups removed on admin down" 630 631 # expect groups to have been deleted as well 632 check_nexthop "" "" 633 634 run_cmd "$IP li set dev veth1 up" 635 636 $IP nexthop flush >/dev/null 2>&1 637 638 # group with nexthops using different devices 639 set -e 640 run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1" 641 run_cmd "$IP nexthop add id 13 via 172.16.1.3 dev veth1" 642 run_cmd "$IP nexthop add id 14 via 172.16.1.4 dev veth1" 643 run_cmd "$IP nexthop add id 15 via 172.16.1.5 dev veth1" 644 645 run_cmd "$IP nexthop add id 22 via 172.16.2.2 dev veth3" 646 run_cmd "$IP nexthop add id 23 via 172.16.2.3 dev veth3" 647 run_cmd "$IP nexthop add id 24 via 172.16.2.4 dev veth3" 648 run_cmd "$IP nexthop add id 25 via 172.16.2.5 dev veth3" 649 set +e 650 651 # multiple groups with same nexthop 652 run_cmd "$IP nexthop add id 104 group 12" 653 run_cmd "$IP nexthop add id 105 group 12" 654 check_nexthop "group" "id 104 group 12 id 105 group 12" 655 log_test $? 0 "Multiple groups with same nexthop" 656 657 run_cmd "$IP nexthop flush groups" 658 [ $? -ne 0 ] && return 1 659 660 # on admin down of veth1, it should be removed from the group 661 run_cmd "$IP nexthop add id 105 group 12/13/22/23/14" 662 run_cmd "$IP li set veth1 down" 663 check_nexthop "id 105" "id 105 group 22/23" 664 log_test $? 0 "Nexthops in group removed on admin down - mixed group" 665 666 run_cmd "$IP nexthop add id 106 group 105/24" 667 log_test $? 2 "Nexthop group can not have a group as an entry" 668 669 # a group can have a blackhole entry only if it is the only 670 # nexthop in the group. Needed for atomic replace with an 671 # actual nexthop group 672 run_cmd "$IP nexthop add id 31 blackhole" 673 run_cmd "$IP nexthop add id 107 group 31" 674 log_test $? 0 "Nexthop group with a blackhole entry" 675 676 run_cmd "$IP nexthop add id 108 group 31/24" 677 log_test $? 2 "Nexthop group can not have a blackhole and another nexthop" 678} 679 680ipv4_withv6_fcnal() 681{ 682 local lladdr 683 684 set -e 685 lladdr=$(get_linklocal veth2 peer) 686 run_cmd "$IP nexthop add id 11 via ${lladdr} dev veth1" 687 set +e 688 run_cmd "$IP ro add 172.16.101.1/32 nhid 11" 689 log_test $? 0 "IPv6 nexthop with IPv4 route" 690 check_route "172.16.101.1" "172.16.101.1 nhid 11 via inet6 ${lladdr} dev veth1" 691 692 set -e 693 run_cmd "$IP nexthop add id 12 via 172.16.1.2 dev veth1" 694 run_cmd "$IP nexthop add id 101 group 11/12" 695 set +e 696 run_cmd "$IP ro replace 172.16.101.1/32 nhid 101" 697 log_test $? 0 "IPv6 nexthop with IPv4 route" 698 699 check_route "172.16.101.1" "172.16.101.1 nhid 101 nexthop via inet6 ${lladdr} dev veth1 weight 1 nexthop via 172.16.1.2 dev veth1 weight 1" 700 701 run_cmd "$IP ro replace 172.16.101.1/32 via inet6 ${lladdr} dev veth1" 702 log_test $? 0 "IPv4 route with IPv6 gateway" 703 check_route "172.16.101.1" "172.16.101.1 via inet6 ${lladdr} dev veth1" 704 705 run_cmd "$IP ro replace 172.16.101.1/32 via inet6 2001:db8:50::1 dev veth1" 706 log_test $? 2 "IPv4 route with invalid IPv6 gateway" 707} 708 709ipv4_fcnal_runtime() 710{ 711 local lladdr 712 local rc 713 714 echo 715 echo "IPv4 functional runtime" 716 echo "-----------------------" 717 718 run_cmd "$IP nexthop add id 21 via 172.16.1.2 dev veth1" 719 run_cmd "$IP ro add 172.16.101.1/32 nhid 21" 720 log_test $? 0 "Route add" 721 check_route "172.16.101.1" "172.16.101.1 nhid 21 via 172.16.1.2 dev veth1" 722 723 run_cmd "$IP ro delete 172.16.101.1/32 nhid 21" 724 log_test $? 0 "Route delete" 725 726 # 727 # scope mismatch 728 # 729 run_cmd "$IP nexthop add id 22 via 172.16.1.2 dev veth1" 730 run_cmd "$IP ro add 172.16.101.1/32 nhid 22 scope host" 731 log_test $? 2 "Route add - scope conflict with nexthop" 732 733 run_cmd "$IP nexthop replace id 22 dev veth3" 734 run_cmd "$IP ro add 172.16.101.1/32 nhid 22 scope host" 735 run_cmd "$IP nexthop replace id 22 via 172.16.2.2 dev veth3" 736 log_test $? 2 "Nexthop replace with invalid scope for existing route" 737 738 # 739 # add route with nexthop and check traffic 740 # 741 run_cmd "$IP nexthop replace id 21 via 172.16.1.2 dev veth1" 742 run_cmd "$IP ro replace 172.16.101.1/32 nhid 21" 743 run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1" 744 log_test $? 0 "Basic ping" 745 746 run_cmd "$IP nexthop replace id 22 via 172.16.2.2 dev veth3" 747 run_cmd "$IP nexthop add id 122 group 21/22" 748 run_cmd "$IP ro replace 172.16.101.1/32 nhid 122" 749 run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1" 750 log_test $? 0 "Ping - multipath" 751 752 run_cmd "$IP ro delete 172.16.101.1/32 nhid 122" 753 754 # 755 # multiple default routes 756 # - tests fib_select_default 757 run_cmd "$IP nexthop add id 501 via 172.16.1.2 dev veth1" 758 run_cmd "$IP ro add default nhid 501" 759 run_cmd "$IP ro add default via 172.16.1.3 dev veth1 metric 20" 760 run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1" 761 log_test $? 0 "Ping - multiple default routes, nh first" 762 763 # flip the order 764 run_cmd "$IP ro del default nhid 501" 765 run_cmd "$IP ro del default via 172.16.1.3 dev veth1 metric 20" 766 run_cmd "$IP ro add default via 172.16.1.2 dev veth1 metric 20" 767 run_cmd "$IP nexthop replace id 501 via 172.16.1.3 dev veth1" 768 run_cmd "$IP ro add default nhid 501 metric 20" 769 run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1" 770 log_test $? 0 "Ping - multiple default routes, nh second" 771 772 run_cmd "$IP nexthop delete nhid 501" 773 run_cmd "$IP ro del default" 774 775 # 776 # IPv4 with blackhole nexthops 777 # 778 run_cmd "$IP nexthop add id 23 blackhole" 779 run_cmd "$IP ro replace 172.16.101.1/32 nhid 23" 780 run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1" 781 log_test $? 2 "Ping - blackhole" 782 783 run_cmd "$IP nexthop replace id 23 via 172.16.1.2 dev veth1" 784 run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1" 785 log_test $? 0 "Ping - blackhole replaced with gateway" 786 787 run_cmd "$IP nexthop replace id 23 blackhole" 788 run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1" 789 log_test $? 2 "Ping - gateway replaced by blackhole" 790 791 run_cmd "$IP ro replace 172.16.101.1/32 nhid 122" 792 run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1" 793 if [ $? -eq 0 ]; then 794 run_cmd "$IP nexthop replace id 122 group 23" 795 run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1" 796 log_test $? 2 "Ping - group with blackhole" 797 798 run_cmd "$IP nexthop replace id 122 group 21/22" 799 run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1" 800 log_test $? 0 "Ping - group blackhole replaced with gateways" 801 else 802 log_test 2 0 "Ping - multipath failed" 803 fi 804 805 # 806 # device only and gw + dev only mix 807 # 808 run_cmd "$IP nexthop add id 85 dev veth1" 809 run_cmd "$IP ro replace 172.16.101.1/32 nhid 85" 810 log_test $? 0 "IPv4 route with device only nexthop" 811 check_route "172.16.101.1" "172.16.101.1 nhid 85 dev veth1" 812 813 run_cmd "$IP nexthop add id 123 group 21/85" 814 run_cmd "$IP ro replace 172.16.101.1/32 nhid 123" 815 log_test $? 0 "IPv4 multipath route with nexthop mix - dev only + gw" 816 check_route "172.16.101.1" "172.16.101.1 nhid 123 nexthop via 172.16.1.2 dev veth1 weight 1 nexthop dev veth1 weight 1" 817 818 # 819 # IPv4 with IPv6 820 # 821 set -e 822 lladdr=$(get_linklocal veth2 peer) 823 run_cmd "$IP nexthop add id 24 via ${lladdr} dev veth1" 824 set +e 825 run_cmd "$IP ro replace 172.16.101.1/32 nhid 24" 826 run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1" 827 log_test $? 0 "IPv6 nexthop with IPv4 route" 828 829 $IP neigh sh | grep -q "${lladdr} dev veth1" 830 if [ $? -eq 1 ]; then 831 echo " WARNING: Neigh entry missing for ${lladdr}" 832 $IP neigh sh | grep 'dev veth1' 833 fi 834 835 $IP neigh sh | grep -q "172.16.101.1 dev eth1" 836 if [ $? -eq 0 ]; then 837 echo " WARNING: Neigh entry exists for 172.16.101.1" 838 $IP neigh sh | grep 'dev veth1' 839 fi 840 841 set -e 842 run_cmd "$IP nexthop add id 25 via 172.16.1.2 dev veth1" 843 run_cmd "$IP nexthop add id 101 group 24/25" 844 set +e 845 run_cmd "$IP ro replace 172.16.101.1/32 nhid 101" 846 log_test $? 0 "IPv4 route with mixed v4-v6 multipath route" 847 848 check_route "172.16.101.1" "172.16.101.1 nhid 101 nexthop via inet6 ${lladdr} dev veth1 weight 1 nexthop via 172.16.1.2 dev veth1 weight 1" 849 850 run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1" 851 log_test $? 0 "IPv6 nexthop with IPv4 route" 852 853 run_cmd "$IP ro replace 172.16.101.1/32 via inet6 ${lladdr} dev veth1" 854 run_cmd "ip netns exec me ping -c1 -w1 172.16.101.1" 855 log_test $? 0 "IPv4 route with IPv6 gateway" 856 857 $IP neigh sh | grep -q "${lladdr} dev veth1" 858 if [ $? -eq 1 ]; then 859 echo " WARNING: Neigh entry missing for ${lladdr}" 860 $IP neigh sh | grep 'dev veth1' 861 fi 862 863 $IP neigh sh | grep -q "172.16.101.1 dev eth1" 864 if [ $? -eq 0 ]; then 865 echo " WARNING: Neigh entry exists for 172.16.101.1" 866 $IP neigh sh | grep 'dev veth1' 867 fi 868 869 # 870 # MPLS as an example of LWT encap 871 # 872 run_cmd "$IP nexthop add id 51 encap mpls 101 via 172.16.1.2 dev veth1" 873 log_test $? 0 "IPv4 route with MPLS encap" 874 check_nexthop "id 51" "id 51 encap mpls 101 via 172.16.1.2 dev veth1 scope link" 875 log_test $? 0 "IPv4 route with MPLS encap - check" 876 877 run_cmd "$IP nexthop add id 52 encap mpls 102 via inet6 2001:db8:91::2 dev veth1" 878 log_test $? 0 "IPv4 route with MPLS encap and v6 gateway" 879 check_nexthop "id 52" "id 52 encap mpls 102 via 2001:db8:91::2 dev veth1 scope link" 880 log_test $? 0 "IPv4 route with MPLS encap, v6 gw - check" 881} 882 883basic() 884{ 885 echo 886 echo "Basic functional tests" 887 echo "----------------------" 888 run_cmd "$IP nexthop ls" 889 log_test $? 0 "List with nothing defined" 890 891 run_cmd "$IP nexthop get id 1" 892 log_test $? 2 "Nexthop get on non-existent id" 893 894 # attempt to create nh without a device or gw - fails 895 run_cmd "$IP nexthop add id 1" 896 log_test $? 2 "Nexthop with no device or gateway" 897 898 # attempt to create nh with down device - fails 899 $IP li set veth1 down 900 run_cmd "$IP nexthop add id 1 dev veth1" 901 log_test $? 2 "Nexthop with down device" 902 903 # create nh with linkdown device - fails 904 $IP li set veth1 up 905 ip -netns peer li set veth2 down 906 run_cmd "$IP nexthop add id 1 dev veth1" 907 log_test $? 2 "Nexthop with device that is linkdown" 908 ip -netns peer li set veth2 up 909 910 # device only 911 run_cmd "$IP nexthop add id 1 dev veth1" 912 log_test $? 0 "Nexthop with device only" 913 914 # create nh with duplicate id 915 run_cmd "$IP nexthop add id 1 dev veth3" 916 log_test $? 2 "Nexthop with duplicate id" 917 918 # blackhole nexthop 919 run_cmd "$IP nexthop add id 2 blackhole" 920 log_test $? 0 "Blackhole nexthop" 921 922 # blackhole nexthop can not have other specs 923 run_cmd "$IP nexthop replace id 2 blackhole dev veth1" 924 log_test $? 2 "Blackhole nexthop with other attributes" 925 926 # 927 # groups 928 # 929 930 run_cmd "$IP nexthop add id 101 group 1" 931 log_test $? 0 "Create group" 932 933 run_cmd "$IP nexthop add id 102 group 2" 934 log_test $? 0 "Create group with blackhole nexthop" 935 936 # multipath group can not have a blackhole as 1 path 937 run_cmd "$IP nexthop add id 103 group 1/2" 938 log_test $? 2 "Create multipath group where 1 path is a blackhole" 939 940 # multipath group can not have a member replaced by a blackhole 941 run_cmd "$IP nexthop replace id 2 dev veth3" 942 run_cmd "$IP nexthop replace id 102 group 1/2" 943 run_cmd "$IP nexthop replace id 2 blackhole" 944 log_test $? 2 "Multipath group can not have a member replaced by blackhole" 945 946 # attempt to create group with non-existent nexthop 947 run_cmd "$IP nexthop add id 103 group 12" 948 log_test $? 2 "Create group with non-existent nexthop" 949 950 # attempt to create group with same nexthop 951 run_cmd "$IP nexthop add id 103 group 1/1" 952 log_test $? 2 "Create group with same nexthop multiple times" 953 954 # replace nexthop with a group - fails 955 run_cmd "$IP nexthop replace id 2 group 1" 956 log_test $? 2 "Replace nexthop with nexthop group" 957 958 # replace nexthop group with a nexthop - fails 959 run_cmd "$IP nexthop replace id 101 dev veth1" 960 log_test $? 2 "Replace nexthop group with nexthop" 961 962 # nexthop group with other attributes fail 963 run_cmd "$IP nexthop add id 104 group 1 dev veth1" 964 log_test $? 2 "Nexthop group and device" 965 966 # Tests to ensure that flushing works as expected. 967 run_cmd "$IP nexthop add id 105 blackhole proto 99" 968 run_cmd "$IP nexthop add id 106 blackhole proto 100" 969 run_cmd "$IP nexthop add id 107 blackhole proto 99" 970 run_cmd "$IP nexthop flush proto 99" 971 check_nexthop "id 105" "" 972 check_nexthop "id 106" "id 106 blackhole proto 100" 973 check_nexthop "id 107" "" 974 run_cmd "$IP nexthop flush proto 100" 975 check_nexthop "id 106" "" 976 977 run_cmd "$IP nexthop flush proto 100" 978 log_test $? 0 "Test proto flush" 979 980 run_cmd "$IP nexthop add id 104 group 1 blackhole" 981 log_test $? 2 "Nexthop group and blackhole" 982 983 $IP nexthop flush >/dev/null 2>&1 984} 985 986################################################################################ 987# usage 988 989usage() 990{ 991 cat <<EOF 992usage: ${0##*/} OPTS 993 994 -t <test> Test(s) to run (default: all) 995 (options: $ALL_TESTS) 996 -4 IPv4 tests only 997 -6 IPv6 tests only 998 -p Pause on fail 999 -P Pause after each test before cleanup 1000 -v verbose mode (show commands and output) 1001 1002 Runtime test 1003 -n num Number of nexthops to target 1004 -N Use new style to install routes in DUT 1005 1006done 1007EOF 1008} 1009 1010################################################################################ 1011# main 1012 1013while getopts :t:pP46hv o 1014do 1015 case $o in 1016 t) TESTS=$OPTARG;; 1017 4) TESTS=${IPV4_TESTS};; 1018 6) TESTS=${IPV6_TESTS};; 1019 p) PAUSE_ON_FAIL=yes;; 1020 P) PAUSE=yes;; 1021 v) VERBOSE=$(($VERBOSE + 1));; 1022 h) usage; exit 0;; 1023 *) usage; exit 1;; 1024 esac 1025done 1026 1027# make sure we don't pause twice 1028[ "${PAUSE}" = "yes" ] && PAUSE_ON_FAIL=no 1029 1030if [ "$(id -u)" -ne 0 ];then 1031 echo "SKIP: Need root privileges" 1032 exit $ksft_skip; 1033fi 1034 1035if [ ! -x "$(command -v ip)" ]; then 1036 echo "SKIP: Could not run test without ip tool" 1037 exit $ksft_skip 1038fi 1039 1040ip help 2>&1 | grep -q nexthop 1041if [ $? -ne 0 ]; then 1042 echo "SKIP: iproute2 too old, missing nexthop command" 1043 exit $ksft_skip 1044fi 1045 1046out=$(ip nexthop ls 2>&1 | grep -q "Operation not supported") 1047if [ $? -eq 0 ]; then 1048 echo "SKIP: kernel lacks nexthop support" 1049 exit $ksft_skip 1050fi 1051 1052for t in $TESTS 1053do 1054 case $t in 1055 none) IP="ip -netns peer"; setup; exit 0;; 1056 *) setup; $t; cleanup;; 1057 esac 1058done 1059 1060if [ "$TESTS" != "none" ]; then 1061 printf "\nTests passed: %3d\n" ${nsuccess} 1062 printf "Tests failed: %3d\n" ${nfail} 1063fi 1064 1065exit $ret 1066