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