tc_actions.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. ALL_TESTS="gact_drop_and_ok_test mirred_egress_redirect_test \
  4. mirred_egress_mirror_test gact_trap_test"
  5. NUM_NETIFS=4
  6. source tc_common.sh
  7. source lib.sh
  8. tcflags="skip_hw"
  9. h1_create()
  10. {
  11. simple_if_init $h1 192.0.2.1/24
  12. }
  13. h1_destroy()
  14. {
  15. simple_if_fini $h1 192.0.2.1/24
  16. }
  17. h2_create()
  18. {
  19. simple_if_init $h2 192.0.2.2/24
  20. tc qdisc add dev $h2 clsact
  21. }
  22. h2_destroy()
  23. {
  24. tc qdisc del dev $h2 clsact
  25. simple_if_fini $h2 192.0.2.2/24
  26. }
  27. switch_create()
  28. {
  29. simple_if_init $swp1 192.0.2.2/24
  30. tc qdisc add dev $swp1 clsact
  31. simple_if_init $swp2 192.0.2.1/24
  32. }
  33. switch_destroy()
  34. {
  35. simple_if_fini $swp2 192.0.2.1/24
  36. tc qdisc del dev $swp1 clsact
  37. simple_if_fini $swp1 192.0.2.2/24
  38. }
  39. mirred_egress_test()
  40. {
  41. local action=$1
  42. RET=0
  43. tc filter add dev $h2 ingress protocol ip pref 1 handle 101 flower \
  44. $tcflags dst_ip 192.0.2.2 action drop
  45. $MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac -A 192.0.2.1 -B 192.0.2.2 \
  46. -t ip -q
  47. tc_check_packets "dev $h2 ingress" 101 1
  48. check_fail $? "Matched without redirect rule inserted"
  49. tc filter add dev $swp1 ingress protocol ip pref 1 handle 101 flower \
  50. $tcflags dst_ip 192.0.2.2 action mirred egress $action \
  51. dev $swp2
  52. $MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac -A 192.0.2.1 -B 192.0.2.2 \
  53. -t ip -q
  54. tc_check_packets "dev $h2 ingress" 101 1
  55. check_err $? "Did not match incoming $action packet"
  56. tc filter del dev $swp1 ingress protocol ip pref 1 handle 101 flower
  57. tc filter del dev $h2 ingress protocol ip pref 1 handle 101 flower
  58. log_test "mirred egress $action ($tcflags)"
  59. }
  60. gact_drop_and_ok_test()
  61. {
  62. RET=0
  63. tc filter add dev $swp1 ingress protocol ip pref 2 handle 102 flower \
  64. $tcflags dst_ip 192.0.2.2 action drop
  65. $MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac -A 192.0.2.1 -B 192.0.2.2 \
  66. -t ip -q
  67. tc_check_packets "dev $swp1 ingress" 102 1
  68. check_err $? "Packet was not dropped"
  69. tc filter add dev $swp1 ingress protocol ip pref 1 handle 101 flower \
  70. $tcflags dst_ip 192.0.2.2 action ok
  71. $MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac -A 192.0.2.1 -B 192.0.2.2 \
  72. -t ip -q
  73. tc_check_packets "dev $swp1 ingress" 101 1
  74. check_err $? "Did not see passed packet"
  75. tc_check_packets "dev $swp1 ingress" 102 2
  76. check_fail $? "Packet was dropped and it should not reach here"
  77. tc filter del dev $swp1 ingress protocol ip pref 2 handle 102 flower
  78. tc filter del dev $swp1 ingress protocol ip pref 1 handle 101 flower
  79. log_test "gact drop and ok ($tcflags)"
  80. }
  81. gact_trap_test()
  82. {
  83. RET=0
  84. if [[ "$tcflags" != "skip_sw" ]]; then
  85. return 0;
  86. fi
  87. tc filter add dev $swp1 ingress protocol ip pref 1 handle 101 flower \
  88. skip_hw dst_ip 192.0.2.2 action drop
  89. tc filter add dev $swp1 ingress protocol ip pref 3 handle 103 flower \
  90. $tcflags dst_ip 192.0.2.2 action mirred egress redirect \
  91. dev $swp2
  92. $MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac -A 192.0.2.1 -B 192.0.2.2 \
  93. -t ip -q
  94. tc_check_packets "dev $swp1 ingress" 101 1
  95. check_fail $? "Saw packet without trap rule inserted"
  96. tc filter add dev $swp1 ingress protocol ip pref 2 handle 102 flower \
  97. $tcflags dst_ip 192.0.2.2 action trap
  98. $MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac -A 192.0.2.1 -B 192.0.2.2 \
  99. -t ip -q
  100. tc_check_packets "dev $swp1 ingress" 102 1
  101. check_err $? "Packet was not trapped"
  102. tc_check_packets "dev $swp1 ingress" 101 1
  103. check_err $? "Did not see trapped packet"
  104. tc filter del dev $swp1 ingress protocol ip pref 3 handle 103 flower
  105. tc filter del dev $swp1 ingress protocol ip pref 2 handle 102 flower
  106. tc filter del dev $swp1 ingress protocol ip pref 1 handle 101 flower
  107. log_test "trap ($tcflags)"
  108. }
  109. setup_prepare()
  110. {
  111. h1=${NETIFS[p1]}
  112. swp1=${NETIFS[p2]}
  113. swp2=${NETIFS[p3]}
  114. h2=${NETIFS[p4]}
  115. h1mac=$(mac_get $h1)
  116. h2mac=$(mac_get $h2)
  117. swp1origmac=$(mac_get $swp1)
  118. swp2origmac=$(mac_get $swp2)
  119. ip link set $swp1 address $h2mac
  120. ip link set $swp2 address $h1mac
  121. vrf_prepare
  122. h1_create
  123. h2_create
  124. switch_create
  125. }
  126. cleanup()
  127. {
  128. pre_cleanup
  129. switch_destroy
  130. h2_destroy
  131. h1_destroy
  132. vrf_cleanup
  133. ip link set $swp2 address $swp2origmac
  134. ip link set $swp1 address $swp1origmac
  135. }
  136. mirred_egress_redirect_test()
  137. {
  138. mirred_egress_test "redirect"
  139. }
  140. mirred_egress_mirror_test()
  141. {
  142. mirred_egress_test "mirror"
  143. }
  144. trap cleanup EXIT
  145. setup_prepare
  146. setup_wait
  147. tests_run
  148. tc_offload_check
  149. if [[ $? -ne 0 ]]; then
  150. log_info "Could not test offloaded functionality"
  151. else
  152. tcflags="skip_sw"
  153. tests_run
  154. fi
  155. exit $EXIT_STATUS