basic_qos.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. # Copyright 2022 NXP
  4. # The script is mostly generic, with the exception of the
  5. # ethtool per-TC counter names ("rx_green_prio_${tc}")
  6. WAIT_TIME=1
  7. NUM_NETIFS=4
  8. STABLE_MAC_ADDRS=yes
  9. NETIF_CREATE=no
  10. lib_dir=$(dirname $0)/../../../net/forwarding
  11. source $lib_dir/tc_common.sh
  12. source $lib_dir/lib.sh
  13. require_command dcb
  14. h1=${NETIFS[p1]}
  15. swp1=${NETIFS[p2]}
  16. swp2=${NETIFS[p3]}
  17. h2=${NETIFS[p4]}
  18. H1_IPV4="192.0.2.1"
  19. H2_IPV4="192.0.2.2"
  20. H1_IPV6="2001:db8:1::1"
  21. H2_IPV6="2001:db8:1::2"
  22. h1_create()
  23. {
  24. simple_if_init $h1 $H1_IPV4/24 $H1_IPV6/64
  25. }
  26. h1_destroy()
  27. {
  28. simple_if_fini $h1 $H1_IPV4/24 $H1_IPV6/64
  29. }
  30. h2_create()
  31. {
  32. simple_if_init $h2 $H2_IPV4/24 $H2_IPV6/64
  33. }
  34. h2_destroy()
  35. {
  36. simple_if_fini $h2 $H2_IPV4/24 $H2_IPV6/64
  37. }
  38. h1_vlan_create()
  39. {
  40. local vid=$1
  41. vlan_create $h1 $vid
  42. simple_if_init $h1.$vid $H1_IPV4/24 $H1_IPV6/64
  43. ip link set $h1.$vid type vlan \
  44. egress-qos-map 0:0 1:1 2:2 3:3 4:4 5:5 6:6 7:7 \
  45. ingress-qos-map 0:0 1:1 2:2 3:3 4:4 5:5 6:6 7:7
  46. }
  47. h1_vlan_destroy()
  48. {
  49. local vid=$1
  50. simple_if_fini $h1.$vid $H1_IPV4/24 $H1_IPV6/64
  51. vlan_destroy $h1 $vid
  52. }
  53. h2_vlan_create()
  54. {
  55. local vid=$1
  56. vlan_create $h2 $vid
  57. simple_if_init $h2.$vid $H2_IPV4/24 $H2_IPV6/64
  58. ip link set $h2.$vid type vlan \
  59. egress-qos-map 0:0 1:1 2:2 3:3 4:4 5:5 6:6 7:7 \
  60. ingress-qos-map 0:0 1:1 2:2 3:3 4:4 5:5 6:6 7:7
  61. }
  62. h2_vlan_destroy()
  63. {
  64. local vid=$1
  65. simple_if_fini $h2.$vid $H2_IPV4/24 $H2_IPV6/64
  66. vlan_destroy $h2 $vid
  67. }
  68. vlans_prepare()
  69. {
  70. h1_vlan_create 100
  71. h2_vlan_create 100
  72. tc qdisc add dev ${h1}.100 clsact
  73. tc filter add dev ${h1}.100 egress protocol ipv4 \
  74. flower ip_proto icmp action skbedit priority 3
  75. tc filter add dev ${h1}.100 egress protocol ipv6 \
  76. flower ip_proto icmpv6 action skbedit priority 3
  77. }
  78. vlans_destroy()
  79. {
  80. tc qdisc del dev ${h1}.100 clsact
  81. h1_vlan_destroy 100
  82. h2_vlan_destroy 100
  83. }
  84. switch_create()
  85. {
  86. ip link set ${swp1} up
  87. ip link set ${swp2} up
  88. # Ports should trust VLAN PCP even with vlan_filtering=0
  89. ip link add br0 type bridge
  90. ip link set ${swp1} master br0
  91. ip link set ${swp2} master br0
  92. ip link set br0 up
  93. }
  94. switch_destroy()
  95. {
  96. ip link del br0
  97. }
  98. setup_prepare()
  99. {
  100. vrf_prepare
  101. h1_create
  102. h2_create
  103. switch_create
  104. }
  105. cleanup()
  106. {
  107. pre_cleanup
  108. h2_destroy
  109. h1_destroy
  110. switch_destroy
  111. vrf_cleanup
  112. }
  113. dscp_cs_to_tos()
  114. {
  115. local dscp_cs=$1
  116. # https://datatracker.ietf.org/doc/html/rfc2474
  117. # 4.2.2.1 The Class Selector Codepoints
  118. echo $((${dscp_cs} << 5))
  119. }
  120. run_test()
  121. {
  122. local test_name=$1; shift
  123. local if_name=$1; shift
  124. local tc=$1; shift
  125. local tos=$1; shift
  126. local counter_name="rx_green_prio_${tc}"
  127. local ipv4_before
  128. local ipv4_after
  129. local ipv6_before
  130. local ipv6_after
  131. ipv4_before=$(ethtool_stats_get ${swp1} "${counter_name}")
  132. ping_do ${if_name} $H2_IPV4 "-Q ${tos}"
  133. ipv4_after=$(ethtool_stats_get ${swp1} "${counter_name}")
  134. if [ $((${ipv4_after} - ${ipv4_before})) -lt ${PING_COUNT} ]; then
  135. RET=1
  136. else
  137. RET=0
  138. fi
  139. log_test "IPv4 ${test_name}"
  140. ipv6_before=$(ethtool_stats_get ${swp1} "${counter_name}")
  141. ping_do ${if_name} $H2_IPV6 "-Q ${tos}"
  142. ipv6_after=$(ethtool_stats_get ${swp1} "${counter_name}")
  143. if [ $((${ipv6_after} - ${ipv6_before})) -lt ${PING_COUNT} ]; then
  144. RET=1
  145. else
  146. RET=0
  147. fi
  148. log_test "IPv6 ${test_name}"
  149. }
  150. port_default_prio_get()
  151. {
  152. local if_name=$1
  153. local prio
  154. prio="$(dcb -j app show dev ${if_name} default-prio | \
  155. jq '.default_prio[]')"
  156. if [ -z "${prio}" ]; then
  157. prio=0
  158. fi
  159. echo ${prio}
  160. }
  161. test_port_default()
  162. {
  163. local orig=$(port_default_prio_get ${swp1})
  164. local dmac=$(mac_get ${h2})
  165. dcb app replace dev ${swp1} default-prio 5
  166. run_test "Port-default QoS classification" ${h1} 5 0
  167. dcb app replace dev ${swp1} default-prio ${orig}
  168. }
  169. test_vlan_pcp()
  170. {
  171. vlans_prepare
  172. run_test "Trusted VLAN PCP QoS classification" ${h1}.100 3 0
  173. vlans_destroy
  174. }
  175. test_ip_dscp()
  176. {
  177. local port_default=$(port_default_prio_get ${swp1})
  178. local tos=$(dscp_cs_to_tos 4)
  179. dcb app add dev ${swp1} dscp-prio CS4:4
  180. run_test "Trusted DSCP QoS classification" ${h1} 4 ${tos}
  181. dcb app del dev ${swp1} dscp-prio CS4:4
  182. vlans_prepare
  183. run_test "Untrusted DSCP QoS classification follows VLAN PCP" \
  184. ${h1}.100 3 ${tos}
  185. vlans_destroy
  186. run_test "Untrusted DSCP QoS classification follows port default" \
  187. ${h1} ${port_default} ${tos}
  188. }
  189. trap cleanup EXIT
  190. ALL_TESTS="
  191. test_port_default
  192. test_vlan_pcp
  193. test_ip_dscp
  194. "
  195. setup_prepare
  196. setup_wait
  197. tests_run
  198. exit $EXIT_STATUS