route-lib.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0-or-later
  3. # Copyright (c) 2019-2020 Petr Vorel <pvorel@suse.cz>
  4. TST_NEEDS_ROOT=1
  5. TST_NEEDS_CMDS="ip"
  6. . tst_net.sh
  7. ROUTE_RHOST_PORT=${ROUTE_RHOST_PORT:-65535}
  8. ROUTE_MAX_IP=${ROUTE_MAX_IP:-5}
  9. IP_ADDR_DELIM=','
  10. add_macvlan()
  11. {
  12. local action="add"
  13. local OPTIND
  14. while getopts d opt; do
  15. case "$opt" in
  16. d) action="del";;
  17. esac
  18. done
  19. shift $((OPTIND - 1))
  20. local iface="$1"
  21. local type="${2:-lhost}"
  22. cmd="ip link $action $iface link $(tst_iface $type) type macvlan mode bridge"
  23. if [ $type = "lhost" ]; then
  24. ROD $cmd
  25. [ "$action" = "add" ] || return
  26. LHOST_IFACES="$LHOST_IFACES $iface"
  27. else
  28. tst_rhost_run -s -c "$cmd"
  29. [ "$action" = "add" ] || return
  30. RHOST_IFACES="$RHOST_IFACES $iface"
  31. fi
  32. tst_init_iface $type 1
  33. }
  34. check_max_ip()
  35. {
  36. local max_ip_limit=254
  37. [ "$TST_IPV6" ] && max_ip_limit=65534
  38. tst_is_int "$ROUTE_MAX_IP" || tst_brk TBROK "\$ROUTE_MAX_IP not int ($ROUTE_MAX_IP)"
  39. [ $ROUTE_MAX_IP -gt $max_ip_limit ] && ROUTE_MAX_IP=$max_ip_limit
  40. [ $ROUTE_MAX_IP -gt $ROUTE_CHANGE_NETLINK ] && ROUTE_MAX_IP=$ROUTE_CHANGE_NETLINK
  41. }
  42. cleanup_if()
  43. {
  44. [ "$new_liface" ] && add_macvlan -d $new_liface
  45. [ "$new_riface" ] && add_macvlan -d $new_riface rhost
  46. route_cleanup
  47. }
  48. route_cleanup()
  49. {
  50. tst_restore_ipaddr
  51. tst_restore_ipaddr rhost
  52. }
  53. setup_gw()
  54. {
  55. rt="$(tst_ipaddr_un -p 0 0)"
  56. lhost="$(tst_ipaddr_un 1 1)"
  57. rhost="$(tst_ipaddr_un 0 1)"
  58. tst_add_ipaddr -s -q -a $lhost
  59. tst_add_ipaddr -s -q -a $rhost rhost
  60. }
  61. setup_if()
  62. {
  63. rt="$(tst_ipaddr_un -p 0)"
  64. rhost="$(tst_ipaddr_un 0 1)"
  65. tst_add_ipaddr -s -q -a $rhost rhost
  66. if [ $(tst_get_ifaces_cnt) -lt 2 ]; then
  67. new_liface="ltp_mv2"
  68. tst_res TINFO "2 or more local ifaces required, adding '$new_liface'"
  69. add_macvlan $new_liface
  70. fi
  71. if [ $(tst_get_ifaces_cnt rhost) -lt 2 ]; then
  72. new_riface="ltp_mv1"
  73. tst_res TINFO "2 or more remote ifaces required, adding '$new_riface'"
  74. add_macvlan $new_riface rhost
  75. fi
  76. }
  77. test_netlink()
  78. {
  79. local ret=0
  80. local cmd ip_flag
  81. [ "$TST_IPV6" ] && ip_flag="-6"
  82. cmd="route-change-netlink -c $ROUTE_CHANGE_NETLINK $ip_flag -p $ROUTE_RHOST_PORT $ROUTE_CHANGE_NETLINK_PARAMS"
  83. tst_res TINFO "running $cmd"
  84. $cmd || ret=$?
  85. if [ "$ret" -ne 0 ]; then
  86. [ $((ret & 3)) -ne 0 ] && \
  87. tst_brk TFAIL "route-change-netlink failed"
  88. [ $((ret & 32)) -ne 0 ] && \
  89. tst_brk TCONF "not supported configuration"
  90. [ $((ret & 4)) -ne 0 ] && \
  91. tst_res TWARN "route-change-netlink has warnings"
  92. fi
  93. tst_res TPASS "route-change-netlink passed"
  94. }