xref: /linux/tools/testing/selftests/drivers/net/mlxsw/qos_dscp_router.sh (revision 58f6259b7a08f8d47d4629609703d358b042f0fd)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# Test for DSCP prioritization in the router.
5#
6# With ip_forward_update_priority disabled, the packets are expected to keep
7# their DSCP (which in this test uses only values 0..7) intact as they are
8# forwarded by the switch. That is verified at $h2. ICMP responses are formed
9# with the same DSCP as the requests, and likewise pass through the switch
10# intact, which is verified at $h1.
11#
12# With ip_forward_update_priority enabled, router reprioritizes the packets
13# according to the table in reprioritize(). Thus, say, DSCP 7 maps to priority
14# 4, which on egress maps back to DSCP 4. The response packet then gets
15# reprioritized to 6, getting DSCP 6 on egress.
16#
17# +----------------------+                             +----------------------+
18# | H1                   |                             |                   H2 |
19# |    + $h1             |                             |            $h2 +     |
20# |    | 192.0.2.1/28    |                             |  192.0.2.18/28 |     |
21# +----|-----------------+                             +----------------|-----+
22#      |                                                                |
23# +----|----------------------------------------------------------------|-----+
24# | SW |                                                                |     |
25# |    + $swp1                                                    $swp2 +     |
26# |      192.0.2.2/28                                     192.0.2.17/28       |
27# |      APP=0,5,0 .. 7,5,7                          APP=0,5,0 .. 7,5,7       |
28# +---------------------------------------------------------------------------+
29
30ALL_TESTS="
31	ping_ipv4
32	test_update
33	test_no_update
34	test_pedit_norewrite
35	test_dscp_leftover
36"
37
38lib_dir=$(dirname $0)/../../../net/forwarding
39
40NUM_NETIFS=4
41source $lib_dir/lib.sh
42
43reprioritize()
44{
45	local in=$1; shift
46
47	# This is based on rt_tos2priority in include/net/route.h. Assuming 1:1
48	# mapping between priorities and TOS, it yields a new priority for a
49	# packet with ingress priority of $in.
50	local -a reprio=(0 0 2 2 6 6 4 4)
51
52	echo ${reprio[$in]}
53}
54
55zero()
56{
57    echo 0
58}
59
60three()
61{
62    echo 3
63}
64
65h1_create()
66{
67	simple_if_init $h1 192.0.2.1/28
68	tc qdisc add dev $h1 clsact
69	dscp_capture_install $h1 0
70	ip route add vrf v$h1 192.0.2.16/28 via 192.0.2.2
71}
72
73h1_destroy()
74{
75	ip route del vrf v$h1 192.0.2.16/28 via 192.0.2.2
76	dscp_capture_uninstall $h1 0
77	tc qdisc del dev $h1 clsact
78	simple_if_fini $h1 192.0.2.1/28
79}
80
81h2_create()
82{
83	simple_if_init $h2 192.0.2.18/28
84	tc qdisc add dev $h2 clsact
85	dscp_capture_install $h2 0
86	ip route add vrf v$h2 192.0.2.0/28 via 192.0.2.17
87}
88
89h2_destroy()
90{
91	ip route del vrf v$h2 192.0.2.0/28 via 192.0.2.17
92	dscp_capture_uninstall $h2 0
93	tc qdisc del dev $h2 clsact
94	simple_if_fini $h2 192.0.2.18/28
95}
96
97switch_create()
98{
99	simple_if_init $swp1 192.0.2.2/28
100	__simple_if_init $swp2 v$swp1 192.0.2.17/28
101
102	tc qdisc add dev $swp1 clsact
103	tc qdisc add dev $swp2 clsact
104
105	dcb app add dev $swp1 dscp-prio 0:0 1:1 2:2 3:3 4:4 5:5 6:6 7:7
106	dcb app add dev $swp2 dscp-prio 0:0 1:1 2:2 3:3 4:4 5:5 6:6 7:7
107}
108
109switch_destroy()
110{
111	dcb app del dev $swp2 dscp-prio 0:0 1:1 2:2 3:3 4:4 5:5 6:6 7:7
112	dcb app del dev $swp1 dscp-prio 0:0 1:1 2:2 3:3 4:4 5:5 6:6 7:7
113
114	tc qdisc del dev $swp2 clsact
115	tc qdisc del dev $swp1 clsact
116
117	__simple_if_fini $swp2 192.0.2.17/28
118	simple_if_fini $swp1 192.0.2.2/28
119}
120
121setup_prepare()
122{
123	h1=${NETIFS[p1]}
124	swp1=${NETIFS[p2]}
125
126	swp2=${NETIFS[p3]}
127	h2=${NETIFS[p4]}
128
129	vrf_prepare
130
131	sysctl_set net.ipv4.ip_forward_update_priority 1
132	h1_create
133	h2_create
134	switch_create
135}
136
137cleanup()
138{
139	pre_cleanup
140
141	switch_destroy
142	h2_destroy
143	h1_destroy
144	sysctl_restore net.ipv4.ip_forward_update_priority
145
146	vrf_cleanup
147}
148
149ping_ipv4()
150{
151	ping_test $h1 192.0.2.18
152}
153
154dscp_ping_test()
155{
156	local vrf_name=$1; shift
157	local sip=$1; shift
158	local dip=$1; shift
159	local prio=$1; shift
160	local reprio=$1; shift
161	local dev1=$1; shift
162	local dev2=$1; shift
163	local i
164
165	local prio2=$($reprio $prio)   # ICMP Request egress prio
166	local prio3=$($reprio $prio2)  # ICMP Response egress prio
167
168	local dscp=$((prio << 2))     # ICMP Request ingress DSCP
169	local dscp2=$((prio2 << 2))   # ICMP Request egress DSCP
170	local dscp3=$((prio3 << 2))   # ICMP Response egress DSCP
171
172	RET=0
173
174	eval "local -A dev1_t0s=($(dscp_fetch_stats $dev1 0))"
175	eval "local -A dev2_t0s=($(dscp_fetch_stats $dev2 0))"
176
177	local ping_timeout=$((PING_TIMEOUT * 5))
178	ip vrf exec $vrf_name \
179	   ${PING} -Q $dscp ${sip:+-I $sip} $dip \
180		   -c 10 -i 0.5 -w $ping_timeout &> /dev/null
181
182	eval "local -A dev1_t1s=($(dscp_fetch_stats $dev1 0))"
183	eval "local -A dev2_t1s=($(dscp_fetch_stats $dev2 0))"
184
185	for i in {0..7}; do
186		local dscpi=$((i << 2))
187		local expect2=0
188		local expect3=0
189
190		if ((i == prio2)); then
191			expect2=10
192		fi
193		if ((i == prio3)); then
194			expect3=10
195		fi
196
197		local delta=$((dev2_t1s[$i] - dev2_t0s[$i]))
198		((expect2 == delta))
199		check_err $? "DSCP $dscpi@$dev2: Expected to capture $expect2 packets, got $delta."
200
201		delta=$((dev1_t1s[$i] - dev1_t0s[$i]))
202		((expect3 == delta))
203		check_err $? "DSCP $dscpi@$dev1: Expected to capture $expect3 packets, got $delta."
204	done
205
206	log_test "DSCP rewrite: $dscp-(prio $prio2)-$dscp2-(prio $prio3)-$dscp3"
207}
208
209__test_update()
210{
211	local update=$1; shift
212	local reprio=$1; shift
213	local prio
214
215	sysctl_restore net.ipv4.ip_forward_update_priority
216	sysctl_set net.ipv4.ip_forward_update_priority $update
217
218	for prio in {0..7}; do
219		dscp_ping_test v$h1 192.0.2.1 192.0.2.18 $prio $reprio $h1 $h2
220	done
221}
222
223test_update()
224{
225	echo "Test net.ipv4.ip_forward_update_priority=1"
226	__test_update 1 reprioritize
227}
228
229test_no_update()
230{
231	echo "Test net.ipv4.ip_forward_update_priority=0"
232	__test_update 0 echo
233}
234
235# Test that when DSCP is updated in pedit, the DSCP rewrite is turned off.
236test_pedit_norewrite()
237{
238	echo "Test no DSCP rewrite after DSCP is updated by pedit"
239
240	tc filter add dev $swp1 ingress handle 101 pref 1 prot ip flower \
241	    action pedit ex munge ip dsfield set $((3 << 2)) retain 0xfc \
242	    action skbedit priority 3
243
244	__test_update 0 three
245
246	tc filter del dev $swp1 ingress pref 1
247}
248
249# Test that when the last APP rule is removed, the prio->DSCP map is properly
250# set to zeroes, and that the last APP rule does not stay active in the ASIC.
251test_dscp_leftover()
252{
253	echo "Test that last removed DSCP rule is deconfigured correctly"
254
255	dcb app del dev $swp2 dscp-prio 0:0 1:1 2:2 3:3 4:4 5:5 6:6 7:7
256
257	__test_update 0 zero
258
259	dcb app add dev $swp2 dscp-prio 0:0 1:1 2:2 3:3 4:4 5:5 6:6 7:7
260}
261
262trap cleanup EXIT
263
264setup_prepare
265setup_wait
266
267tests_run
268
269exit $EXIT_STATUS
270