rtnetlink_notification.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # This test is for checking rtnetlink notification callpaths, and get as much
  5. # coverage as possible.
  6. #
  7. # set -e
  8. ALL_TESTS="
  9. kci_test_mcast_addr_notification
  10. kci_test_anycast_addr_notification
  11. "
  12. source lib.sh
  13. test_dev="test-dummy1"
  14. kci_test_mcast_addr_notification()
  15. {
  16. RET=0
  17. local tmpfile
  18. local monitor_pid
  19. local match_result
  20. tmpfile=$(mktemp)
  21. defer rm "$tmpfile"
  22. ip monitor maddr > $tmpfile &
  23. monitor_pid=$!
  24. defer kill_process "$monitor_pid"
  25. sleep 1
  26. if [ ! -e "/proc/$monitor_pid" ]; then
  27. RET=$ksft_skip
  28. log_test "mcast addr notification: iproute2 too old"
  29. return $RET
  30. fi
  31. ip link add name "$test_dev" type dummy
  32. check_err $? "failed to add dummy interface"
  33. ip link set "$test_dev" up
  34. check_err $? "failed to set dummy interface up"
  35. ip link del dev "$test_dev"
  36. check_err $? "Failed to delete dummy interface"
  37. sleep 1
  38. # There should be 4 line matches as follows.
  39. # 13: test-dummy1    inet6 mcast ff02::1 scope global 
  40. # 13: test-dummy1    inet mcast 224.0.0.1 scope global 
  41. # Deleted 13: test-dummy1    inet mcast 224.0.0.1 scope global 
  42. # Deleted 13: test-dummy1    inet6 mcast ff02::1 scope global 
  43. match_result=$(grep -cE "$test_dev.*(224.0.0.1|ff02::1)" "$tmpfile")
  44. if [ "$match_result" -ne 4 ]; then
  45. RET=$ksft_fail
  46. fi
  47. log_test "mcast addr notification: Expected 4 matches, got $match_result"
  48. return $RET
  49. }
  50. kci_test_anycast_addr_notification()
  51. {
  52. RET=0
  53. local tmpfile
  54. local monitor_pid
  55. local match_result
  56. tmpfile=$(mktemp)
  57. defer rm "$tmpfile"
  58. ip monitor acaddress > "$tmpfile" &
  59. monitor_pid=$!
  60. defer kill_process "$monitor_pid"
  61. sleep 1
  62. if [ ! -e "/proc/$monitor_pid" ]; then
  63. RET=$ksft_skip
  64. log_test "anycast addr notification: iproute2 too old"
  65. return "$RET"
  66. fi
  67. ip link add name "$test_dev" type dummy
  68. check_err $? "failed to add dummy interface"
  69. ip link set "$test_dev" up
  70. check_err $? "failed to set dummy interface up"
  71. sysctl -qw net.ipv6.conf."$test_dev".forwarding=1
  72. ip link del dev "$test_dev"
  73. check_err $? "Failed to delete dummy interface"
  74. sleep 1
  75. # There should be 2 line matches as follows.
  76. # 9: dummy2 inet6 any fe80:: scope global
  77. # Deleted 9: dummy2 inet6 any fe80:: scope global
  78. match_result=$(grep -cE "$test_dev.*(fe80::)" "$tmpfile")
  79. if [ "$match_result" -ne 2 ]; then
  80. RET=$ksft_fail
  81. fi
  82. log_test "anycast addr notification: Expected 2 matches, got $match_result"
  83. return "$RET"
  84. }
  85. #check for needed privileges
  86. if [ "$(id -u)" -ne 0 ];then
  87. RET=$ksft_skip
  88. log_test "need root privileges"
  89. exit $RET
  90. fi
  91. require_command ip
  92. tests_run
  93. exit $EXIT_STATUS