mirror_lib.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # SPDX-License-Identifier: GPL-2.0
  2. mirror_install()
  3. {
  4. local from_dev=$1; shift
  5. local direction=$1; shift
  6. local to_dev=$1; shift
  7. local filter=$1; shift
  8. tc filter add dev $from_dev $direction \
  9. pref 1000 $filter \
  10. action mirred egress mirror dev $to_dev
  11. }
  12. mirror_uninstall()
  13. {
  14. local from_dev=$1; shift
  15. local direction=$1; shift
  16. tc filter del dev $swp1 $direction pref 1000
  17. }
  18. mirror_test()
  19. {
  20. local vrf_name=$1; shift
  21. local sip=$1; shift
  22. local dip=$1; shift
  23. local dev=$1; shift
  24. local pref=$1; shift
  25. local expect=$1; shift
  26. local t0=$(tc_rule_stats_get $dev $pref)
  27. ip vrf exec $vrf_name \
  28. ${PING} ${sip:+-I $sip} $dip -c 10 -i 0.1 -w 2 &> /dev/null
  29. local t1=$(tc_rule_stats_get $dev $pref)
  30. local delta=$((t1 - t0))
  31. # Tolerate a couple stray extra packets.
  32. ((expect <= delta && delta <= expect + 2))
  33. check_err $? "Expected to capture $expect packets, got $delta."
  34. }
  35. do_test_span_dir_ips()
  36. {
  37. local expect=$1; shift
  38. local dev=$1; shift
  39. local direction=$1; shift
  40. local ip1=$1; shift
  41. local ip2=$1; shift
  42. icmp_capture_install $dev
  43. mirror_test v$h1 $ip1 $ip2 $dev 100 $expect
  44. mirror_test v$h2 $ip2 $ip1 $dev 100 $expect
  45. icmp_capture_uninstall $dev
  46. }
  47. quick_test_span_dir_ips()
  48. {
  49. do_test_span_dir_ips 10 "$@"
  50. }
  51. fail_test_span_dir_ips()
  52. {
  53. do_test_span_dir_ips 0 "$@"
  54. }
  55. test_span_dir_ips()
  56. {
  57. local dev=$1; shift
  58. local direction=$1; shift
  59. local forward_type=$1; shift
  60. local backward_type=$1; shift
  61. local ip1=$1; shift
  62. local ip2=$1; shift
  63. quick_test_span_dir_ips "$dev" "$direction" "$ip1" "$ip2"
  64. icmp_capture_install $dev "type $forward_type"
  65. mirror_test v$h1 $ip1 $ip2 $dev 100 10
  66. icmp_capture_uninstall $dev
  67. icmp_capture_install $dev "type $backward_type"
  68. mirror_test v$h2 $ip2 $ip1 $dev 100 10
  69. icmp_capture_uninstall $dev
  70. }
  71. fail_test_span_dir()
  72. {
  73. fail_test_span_dir_ips "$@" 192.0.2.1 192.0.2.2
  74. }
  75. test_span_dir()
  76. {
  77. test_span_dir_ips "$@" 192.0.2.1 192.0.2.2
  78. }
  79. do_test_span_vlan_dir_ips()
  80. {
  81. local expect=$1; shift
  82. local dev=$1; shift
  83. local vid=$1; shift
  84. local direction=$1; shift
  85. local ip1=$1; shift
  86. local ip2=$1; shift
  87. # Install the capture as skip_hw to avoid double-counting of packets.
  88. # The traffic is meant for local box anyway, so will be trapped to
  89. # kernel.
  90. vlan_capture_install $dev "skip_hw vlan_id $vid vlan_ethtype ip"
  91. mirror_test v$h1 $ip1 $ip2 $dev 100 $expect
  92. mirror_test v$h2 $ip2 $ip1 $dev 100 $expect
  93. vlan_capture_uninstall $dev
  94. }
  95. quick_test_span_vlan_dir_ips()
  96. {
  97. do_test_span_vlan_dir_ips 10 "$@"
  98. }
  99. fail_test_span_vlan_dir_ips()
  100. {
  101. do_test_span_vlan_dir_ips 0 "$@"
  102. }
  103. quick_test_span_vlan_dir()
  104. {
  105. quick_test_span_vlan_dir_ips "$@" 192.0.2.1 192.0.2.2
  106. }
  107. fail_test_span_vlan_dir()
  108. {
  109. fail_test_span_vlan_dir_ips "$@" 192.0.2.1 192.0.2.2
  110. }