tc_police.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # Test tc-police action.
  5. #
  6. # +---------------------------------+
  7. # | H1 (vrf) |
  8. # | + $h1 |
  9. # | | 192.0.2.1/24 |
  10. # | | |
  11. # | | default via 192.0.2.2 |
  12. # +----|----------------------------+
  13. # |
  14. # +----|----------------------------------------------------------------------+
  15. # | SW | |
  16. # | + $rp1 |
  17. # | 192.0.2.2/24 |
  18. # | |
  19. # | 198.51.100.2/24 203.0.113.2/24 |
  20. # | + $rp2 + $rp3 |
  21. # | | | |
  22. # +----|-----------------------------------------|----------------------------+
  23. # | |
  24. # +----|----------------------------+ +----|----------------------------+
  25. # | | default via 198.51.100.2 | | | default via 203.0.113.2 |
  26. # | | | | | |
  27. # | | 198.51.100.1/24 | | | 203.0.113.1/24 |
  28. # | + $h2 | | + $h3 |
  29. # | H2 (vrf) | | H3 (vrf) |
  30. # +---------------------------------+ +---------------------------------+
  31. ALL_TESTS="
  32. police_rx_test
  33. police_tx_test
  34. police_shared_test
  35. police_rx_mirror_test
  36. police_tx_mirror_test
  37. police_pps_rx_test
  38. police_pps_tx_test
  39. police_mtu_rx_test
  40. police_mtu_tx_test
  41. "
  42. NUM_NETIFS=6
  43. source tc_common.sh
  44. source lib.sh
  45. h1_create()
  46. {
  47. simple_if_init $h1 192.0.2.1/24
  48. ip -4 route add default vrf v$h1 nexthop via 192.0.2.2
  49. }
  50. h1_destroy()
  51. {
  52. ip -4 route del default vrf v$h1 nexthop via 192.0.2.2
  53. simple_if_fini $h1 192.0.2.1/24
  54. }
  55. h2_create()
  56. {
  57. simple_if_init $h2 198.51.100.1/24
  58. ip -4 route add default vrf v$h2 nexthop via 198.51.100.2
  59. tc qdisc add dev $h2 clsact
  60. }
  61. h2_destroy()
  62. {
  63. tc qdisc del dev $h2 clsact
  64. ip -4 route del default vrf v$h2 nexthop via 198.51.100.2
  65. simple_if_fini $h2 198.51.100.1/24
  66. }
  67. h3_create()
  68. {
  69. simple_if_init $h3 203.0.113.1/24
  70. ip -4 route add default vrf v$h3 nexthop via 203.0.113.2
  71. tc qdisc add dev $h3 clsact
  72. }
  73. h3_destroy()
  74. {
  75. tc qdisc del dev $h3 clsact
  76. ip -4 route del default vrf v$h3 nexthop via 203.0.113.2
  77. simple_if_fini $h3 203.0.113.1/24
  78. }
  79. router_create()
  80. {
  81. ip link set dev $rp1 up
  82. ip link set dev $rp2 up
  83. ip link set dev $rp3 up
  84. __addr_add_del $rp1 add 192.0.2.2/24
  85. __addr_add_del $rp2 add 198.51.100.2/24
  86. __addr_add_del $rp3 add 203.0.113.2/24
  87. tc qdisc add dev $rp1 clsact
  88. tc qdisc add dev $rp2 clsact
  89. }
  90. router_destroy()
  91. {
  92. tc qdisc del dev $rp2 clsact
  93. tc qdisc del dev $rp1 clsact
  94. __addr_add_del $rp3 del 203.0.113.2/24
  95. __addr_add_del $rp2 del 198.51.100.2/24
  96. __addr_add_del $rp1 del 192.0.2.2/24
  97. ip link set dev $rp3 down
  98. ip link set dev $rp2 down
  99. ip link set dev $rp1 down
  100. }
  101. police_common_test()
  102. {
  103. local test_name=$1; shift
  104. RET=0
  105. # Rule to measure bandwidth on ingress of $h2
  106. tc filter add dev $h2 ingress protocol ip pref 1 handle 101 flower \
  107. dst_ip 198.51.100.1 ip_proto udp dst_port 54321 \
  108. action drop
  109. mausezahn $h1 -a own -b $(mac_get $rp1) -A 192.0.2.1 -B 198.51.100.1 \
  110. -t udp sp=12345,dp=54321 -p 1000 -c 0 -q &
  111. local t0=$(tc_rule_stats_get $h2 1 ingress .bytes)
  112. sleep 10
  113. local t1=$(tc_rule_stats_get $h2 1 ingress .bytes)
  114. local er=$((10 * 1000 * 1000))
  115. local nr=$(rate $t0 $t1 10)
  116. local nr_pct=$((100 * (nr - er) / er))
  117. ((-10 <= nr_pct && nr_pct <= 10))
  118. check_err $? "Expected rate $(humanize $er), got $(humanize $nr), which is $nr_pct% off. Required accuracy is +-10%."
  119. log_test "$test_name"
  120. { kill %% && wait %%; } 2>/dev/null
  121. tc filter del dev $h2 ingress protocol ip pref 1 handle 101 flower
  122. }
  123. police_rx_test()
  124. {
  125. # Rule to police traffic destined to $h2 on ingress of $rp1
  126. tc filter add dev $rp1 ingress protocol ip pref 1 handle 101 flower \
  127. dst_ip 198.51.100.1 ip_proto udp dst_port 54321 \
  128. action police rate 10mbit burst 16k conform-exceed drop/ok
  129. police_common_test "police on rx"
  130. tc filter del dev $rp1 ingress protocol ip pref 1 handle 101 flower
  131. }
  132. police_tx_test()
  133. {
  134. # Rule to police traffic destined to $h2 on egress of $rp2
  135. tc filter add dev $rp2 egress protocol ip pref 1 handle 101 flower \
  136. dst_ip 198.51.100.1 ip_proto udp dst_port 54321 \
  137. action police rate 10mbit burst 16k conform-exceed drop/ok
  138. police_common_test "police on tx"
  139. tc filter del dev $rp2 egress protocol ip pref 1 handle 101 flower
  140. }
  141. police_shared_common_test()
  142. {
  143. local dport=$1; shift
  144. local test_name=$1; shift
  145. RET=0
  146. mausezahn $h1 -a own -b $(mac_get $rp1) -A 192.0.2.1 -B 198.51.100.1 \
  147. -t udp sp=12345,dp=$dport -p 1000 -c 0 -q &
  148. local t0=$(tc_rule_stats_get $h2 1 ingress .bytes)
  149. sleep 10
  150. local t1=$(tc_rule_stats_get $h2 1 ingress .bytes)
  151. local er=$((10 * 1000 * 1000))
  152. local nr=$(rate $t0 $t1 10)
  153. local nr_pct=$((100 * (nr - er) / er))
  154. ((-10 <= nr_pct && nr_pct <= 10))
  155. check_err $? "Expected rate $(humanize $er), got $(humanize $nr), which is $nr_pct% off. Required accuracy is +-10%."
  156. log_test "$test_name"
  157. { kill %% && wait %%; } 2>/dev/null
  158. }
  159. police_shared_test()
  160. {
  161. # Rule to measure bandwidth on ingress of $h2
  162. tc filter add dev $h2 ingress protocol ip pref 1 handle 101 flower \
  163. dst_ip 198.51.100.1 ip_proto udp src_port 12345 \
  164. action drop
  165. # Rule to police traffic destined to $h2 on ingress of $rp1
  166. tc filter add dev $rp1 ingress protocol ip pref 1 handle 101 flower \
  167. dst_ip 198.51.100.1 ip_proto udp dst_port 54321 \
  168. action police rate 10mbit burst 16k conform-exceed drop/ok \
  169. index 10
  170. # Rule to police a different flow destined to $h2 on egress of $rp2
  171. # using same policer
  172. tc filter add dev $rp2 egress protocol ip pref 1 handle 101 flower \
  173. dst_ip 198.51.100.1 ip_proto udp dst_port 22222 \
  174. action police index 10
  175. police_shared_common_test 54321 "police with shared policer - rx"
  176. police_shared_common_test 22222 "police with shared policer - tx"
  177. tc filter del dev $rp2 egress protocol ip pref 1 handle 101 flower
  178. tc filter del dev $rp1 ingress protocol ip pref 1 handle 101 flower
  179. tc filter del dev $h2 ingress protocol ip pref 1 handle 101 flower
  180. }
  181. police_mirror_common_test()
  182. {
  183. local pol_if=$1; shift
  184. local dir=$1; shift
  185. local test_name=$1; shift
  186. RET=0
  187. # Rule to measure bandwidth on ingress of $h2
  188. tc filter add dev $h2 ingress protocol ip pref 1 handle 101 flower \
  189. dst_ip 198.51.100.1 ip_proto udp dst_port 54321 \
  190. action drop
  191. # Rule to measure bandwidth of mirrored traffic on ingress of $h3
  192. tc filter add dev $h3 ingress protocol ip pref 1 handle 101 flower \
  193. dst_ip 198.51.100.1 ip_proto udp dst_port 54321 \
  194. action drop
  195. # Rule to police traffic destined to $h2 and mirror to $h3
  196. tc filter add dev $pol_if $dir protocol ip pref 1 handle 101 flower \
  197. dst_ip 198.51.100.1 ip_proto udp dst_port 54321 \
  198. action police rate 10mbit burst 16k conform-exceed drop/pipe \
  199. action mirred egress mirror dev $rp3
  200. mausezahn $h1 -a own -b $(mac_get $rp1) -A 192.0.2.1 -B 198.51.100.1 \
  201. -t udp sp=12345,dp=54321 -p 1000 -c 0 -q &
  202. local t0=$(tc_rule_stats_get $h2 1 ingress .bytes)
  203. sleep 10
  204. local t1=$(tc_rule_stats_get $h2 1 ingress .bytes)
  205. local er=$((10 * 1000 * 1000))
  206. local nr=$(rate $t0 $t1 10)
  207. local nr_pct=$((100 * (nr - er) / er))
  208. ((-10 <= nr_pct && nr_pct <= 10))
  209. check_err $? "Expected rate $(humanize $er), got $(humanize $nr), which is $nr_pct% off. Required accuracy is +-10%."
  210. local t0=$(tc_rule_stats_get $h3 1 ingress .bytes)
  211. sleep 10
  212. local t1=$(tc_rule_stats_get $h3 1 ingress .bytes)
  213. local er=$((10 * 1000 * 1000))
  214. local nr=$(rate $t0 $t1 10)
  215. local nr_pct=$((100 * (nr - er) / er))
  216. ((-10 <= nr_pct && nr_pct <= 10))
  217. check_err $? "Expected rate $(humanize $er), got $(humanize $nr), which is $nr_pct% off. Required accuracy is +-10%."
  218. log_test "$test_name"
  219. { kill %% && wait %%; } 2>/dev/null
  220. tc filter del dev $pol_if $dir protocol ip pref 1 handle 101 flower
  221. tc filter del dev $h3 ingress protocol ip pref 1 handle 101 flower
  222. tc filter del dev $h2 ingress protocol ip pref 1 handle 101 flower
  223. }
  224. police_rx_mirror_test()
  225. {
  226. police_mirror_common_test $rp1 ingress "police rx and mirror"
  227. }
  228. police_tx_mirror_test()
  229. {
  230. police_mirror_common_test $rp2 egress "police tx and mirror"
  231. }
  232. police_pps_common_test()
  233. {
  234. local test_name=$1; shift
  235. RET=0
  236. # Rule to measure bandwidth on ingress of $h2
  237. tc filter add dev $h2 ingress protocol ip pref 1 handle 101 flower \
  238. dst_ip 198.51.100.1 ip_proto udp dst_port 54321 \
  239. action drop
  240. mausezahn $h1 -a own -b $(mac_get $rp1) -A 192.0.2.1 -B 198.51.100.1 \
  241. -t udp sp=12345,dp=54321 -p 1000 -c 0 -q &
  242. local t0=$(tc_rule_stats_get $h2 1 ingress .packets)
  243. sleep 10
  244. local t1=$(tc_rule_stats_get $h2 1 ingress .packets)
  245. local er=$((2000))
  246. local nr=$(packets_rate $t0 $t1 10)
  247. local nr_pct=$((100 * (nr - er) / er))
  248. ((-10 <= nr_pct && nr_pct <= 10))
  249. check_err $? "Expected rate $(humanize $er), got $(humanize $nr), which is $nr_pct% off. Required accuracy is +-10%."
  250. log_test "$test_name"
  251. { kill %% && wait %%; } 2>/dev/null
  252. tc filter del dev $h2 ingress protocol ip pref 1 handle 101 flower
  253. }
  254. police_pps_rx_test()
  255. {
  256. # Rule to police traffic destined to $h2 on ingress of $rp1
  257. tc filter add dev $rp1 ingress protocol ip pref 1 handle 101 flower \
  258. dst_ip 198.51.100.1 ip_proto udp dst_port 54321 \
  259. action police pkts_rate 2000 pkts_burst 400 conform-exceed drop/ok
  260. police_pps_common_test "police pps on rx"
  261. tc filter del dev $rp1 ingress protocol ip pref 1 handle 101 flower
  262. }
  263. police_pps_tx_test()
  264. {
  265. # Rule to police traffic destined to $h2 on egress of $rp2
  266. tc filter add dev $rp2 egress protocol ip pref 1 handle 101 flower \
  267. dst_ip 198.51.100.1 ip_proto udp dst_port 54321 \
  268. action police pkts_rate 2000 pkts_burst 400 conform-exceed drop/ok
  269. police_pps_common_test "police pps on tx"
  270. tc filter del dev $rp2 egress protocol ip pref 1 handle 101 flower
  271. }
  272. police_mtu_common_test() {
  273. RET=0
  274. local test_name=$1; shift
  275. local dev=$1; shift
  276. local direction=$1; shift
  277. tc filter add dev $dev $direction protocol ip pref 1 handle 101 flower \
  278. dst_ip 198.51.100.1 ip_proto udp dst_port 54321 \
  279. action police mtu 1042 conform-exceed drop/ok
  280. # to count "conform" packets
  281. tc filter add dev $h2 ingress protocol ip pref 1 handle 101 flower \
  282. dst_ip 198.51.100.1 ip_proto udp dst_port 54321 \
  283. action drop
  284. mausezahn $h1 -a own -b $(mac_get $rp1) -A 192.0.2.1 -B 198.51.100.1 \
  285. -t udp sp=12345,dp=54321 -p 1001 -c 10 -q
  286. mausezahn $h1 -a own -b $(mac_get $rp1) -A 192.0.2.1 -B 198.51.100.1 \
  287. -t udp sp=12345,dp=54321 -p 1000 -c 3 -q
  288. tc_check_packets "dev $dev $direction" 101 13
  289. check_err $? "wrong packet counter"
  290. # "exceed" packets
  291. local overlimits_t0=$(tc_rule_stats_get ${dev} 1 ${direction} .overlimits)
  292. test ${overlimits_t0} = 10
  293. check_err $? "wrong overlimits, expected 10 got ${overlimits_t0}"
  294. # "conform" packets
  295. tc_check_packets "dev $h2 ingress" 101 3
  296. check_err $? "forwarding error"
  297. tc filter del dev $h2 ingress protocol ip pref 1 handle 101 flower
  298. tc filter del dev $dev $direction protocol ip pref 1 handle 101 flower
  299. log_test "$test_name"
  300. }
  301. police_mtu_rx_test()
  302. {
  303. police_mtu_common_test "police mtu (rx)" $rp1 ingress
  304. }
  305. police_mtu_tx_test()
  306. {
  307. police_mtu_common_test "police mtu (tx)" $rp2 egress
  308. }
  309. setup_prepare()
  310. {
  311. h1=${NETIFS[p1]}
  312. rp1=${NETIFS[p2]}
  313. rp2=${NETIFS[p3]}
  314. h2=${NETIFS[p4]}
  315. rp3=${NETIFS[p5]}
  316. h3=${NETIFS[p6]}
  317. vrf_prepare
  318. forwarding_enable
  319. h1_create
  320. h2_create
  321. h3_create
  322. router_create
  323. }
  324. cleanup()
  325. {
  326. pre_cleanup
  327. router_destroy
  328. h3_destroy
  329. h2_destroy
  330. h1_destroy
  331. forwarding_restore
  332. vrf_cleanup
  333. }
  334. trap cleanup EXIT
  335. setup_prepare
  336. setup_wait
  337. tests_run
  338. exit $EXIT_STATUS