tc_chains.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. ALL_TESTS="unreachable_chain_test gact_goto_chain_test create_destroy_chain \
  4. template_filter_fits"
  5. NUM_NETIFS=2
  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. unreachable_chain_test()
  28. {
  29. RET=0
  30. tc filter add dev $h2 ingress chain 1 protocol ip pref 1 handle 1101 \
  31. flower $tcflags dst_mac $h2mac action drop
  32. $MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac -A 192.0.2.1 -B 192.0.2.2 \
  33. -t ip -q
  34. tc_check_packets "dev $h2 ingress" 1101 1
  35. check_fail $? "matched on filter in unreachable chain"
  36. tc filter del dev $h2 ingress chain 1 protocol ip pref 1 handle 1101 \
  37. flower
  38. log_test "unreachable chain ($tcflags)"
  39. }
  40. gact_goto_chain_test()
  41. {
  42. RET=0
  43. tc filter add dev $h2 ingress chain 1 protocol ip pref 1 handle 1101 \
  44. flower $tcflags dst_mac $h2mac action drop
  45. tc filter add dev $h2 ingress protocol ip pref 2 handle 102 flower \
  46. $tcflags dst_mac $h2mac action drop
  47. tc filter add dev $h2 ingress protocol ip pref 1 handle 101 flower \
  48. $tcflags dst_mac $h2mac action goto chain 1
  49. $MZ $h1 -c 1 -p 64 -a $h1mac -b $h2mac -A 192.0.2.1 -B 192.0.2.2 \
  50. -t ip -q
  51. tc_check_packets "dev $h2 ingress" 102 1
  52. check_fail $? "Matched on a wrong filter"
  53. tc_check_packets "dev $h2 ingress" 101 1
  54. check_err $? "Did not match on correct filter with goto chain action"
  55. tc_check_packets "dev $h2 ingress" 1101 1
  56. check_err $? "Did not match on correct filter in chain 1"
  57. tc filter del dev $h2 ingress protocol ip pref 1 handle 101 flower
  58. tc filter del dev $h2 ingress protocol ip pref 2 handle 102 flower
  59. tc filter del dev $h2 ingress chain 1 protocol ip pref 1 handle 1101 \
  60. flower
  61. log_test "gact goto chain ($tcflags)"
  62. }
  63. create_destroy_chain()
  64. {
  65. RET=0
  66. tc chain add dev $h2 ingress
  67. check_err $? "Failed to create default chain"
  68. output="$(tc -j chain get dev $h2 ingress)"
  69. check_err $? "Failed to get default chain"
  70. echo $output | jq -e ".[] | select(.chain == 0)" &> /dev/null
  71. check_err $? "Unexpected output for default chain"
  72. tc chain add dev $h2 ingress chain 1
  73. check_err $? "Failed to create chain 1"
  74. output="$(tc -j chain get dev $h2 ingress chain 1)"
  75. check_err $? "Failed to get chain 1"
  76. echo $output | jq -e ".[] | select(.chain == 1)" &> /dev/null
  77. check_err $? "Unexpected output for chain 1"
  78. output="$(tc -j chain show dev $h2 ingress)"
  79. check_err $? "Failed to dump chains"
  80. echo $output | jq -e ".[] | select(.chain == 0)" &> /dev/null
  81. check_err $? "Can't find default chain in dump"
  82. echo $output | jq -e ".[] | select(.chain == 1)" &> /dev/null
  83. check_err $? "Can't find chain 1 in dump"
  84. tc chain del dev $h2 ingress
  85. check_err $? "Failed to destroy default chain"
  86. tc chain del dev $h2 ingress chain 1
  87. check_err $? "Failed to destroy chain 1"
  88. log_test "create destroy chain"
  89. }
  90. template_filter_fits()
  91. {
  92. RET=0
  93. tc chain add dev $h2 ingress protocol ip \
  94. flower dst_mac 00:00:00:00:00:00/FF:FF:FF:FF:FF:FF &> /dev/null
  95. tc chain add dev $h2 ingress chain 1 protocol ip \
  96. flower src_mac 00:00:00:00:00:00/FF:FF:FF:FF:FF:FF &> /dev/null
  97. tc filter add dev $h2 ingress protocol ip pref 1 handle 1101 \
  98. flower dst_mac $h2mac action drop
  99. check_err $? "Failed to insert filter which fits template"
  100. tc filter add dev $h2 ingress protocol ip pref 1 handle 1102 \
  101. flower src_mac $h2mac action drop &> /dev/null
  102. check_fail $? "Incorrectly succeded to insert filter which does not template"
  103. tc filter add dev $h2 ingress chain 1 protocol ip pref 1 handle 1101 \
  104. flower src_mac $h2mac action drop
  105. check_err $? "Failed to insert filter which fits template"
  106. tc filter add dev $h2 ingress chain 1 protocol ip pref 1 handle 1102 \
  107. flower dst_mac $h2mac action drop &> /dev/null
  108. check_fail $? "Incorrectly succeded to insert filter which does not template"
  109. tc filter del dev $h2 ingress chain 1 protocol ip pref 1 handle 1102 \
  110. flower &> /dev/null
  111. tc filter del dev $h2 ingress chain 1 protocol ip pref 1 handle 1101 \
  112. flower &> /dev/null
  113. tc filter del dev $h2 ingress protocol ip pref 1 handle 1102 \
  114. flower &> /dev/null
  115. tc filter del dev $h2 ingress protocol ip pref 1 handle 1101 \
  116. flower &> /dev/null
  117. tc chain del dev $h2 ingress chain 1
  118. tc chain del dev $h2 ingress
  119. log_test "template filter fits"
  120. }
  121. setup_prepare()
  122. {
  123. h1=${NETIFS[p1]}
  124. h2=${NETIFS[p2]}
  125. h1mac=$(mac_get $h1)
  126. h2mac=$(mac_get $h2)
  127. vrf_prepare
  128. h1_create
  129. h2_create
  130. }
  131. cleanup()
  132. {
  133. pre_cleanup
  134. h2_destroy
  135. h1_destroy
  136. vrf_cleanup
  137. }
  138. check_tc_chain_support
  139. trap cleanup EXIT
  140. setup_prepare
  141. setup_wait
  142. tests_run
  143. tc_offload_check
  144. if [[ $? -ne 0 ]]; then
  145. log_info "Could not test offloaded functionality"
  146. else
  147. tcflags="skip_sw"
  148. tests_run
  149. fi
  150. exit $EXIT_STATUS