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
97dscp_map()
98{
99	local base=$1; shift
100	local prio
101
102	for prio in {0..7}; do
103		echo app=$prio,5,$((base + prio))
104	done
105}
106
107switch_create()
108{
109	simple_if_init $swp1 192.0.2.2/28
110	__simple_if_init $swp2 v$swp1 192.0.2.17/28
111
112	tc qdisc add dev $swp1 clsact
113	tc qdisc add dev $swp2 clsact
114
115	lldptool -T -i $swp1 -V APP $(dscp_map 0) >/dev/null
116	lldptool -T -i $swp2 -V APP $(dscp_map 0) >/dev/null
117	lldpad_app_wait_set $swp1
118	lldpad_app_wait_set $swp2
119}
120
121switch_destroy()
122{
123	lldptool -T -i $swp2 -V APP -d $(dscp_map 0) >/dev/null
124	lldptool -T -i $swp1 -V APP -d $(dscp_map 0) >/dev/null
125	lldpad_app_wait_del
126
127	tc qdisc del dev $swp2 clsact
128	tc qdisc del dev $swp1 clsact
129
130	__simple_if_fini $swp2 192.0.2.17/28
131	simple_if_fini $swp1 192.0.2.2/28
132}
133
134setup_prepare()
135{
136	h1=${NETIFS[p1]}
137	swp1=${NETIFS[p2]}
138
139	swp2=${NETIFS[p3]}
140	h2=${NETIFS[p4]}
141
142	vrf_prepare
143
144	sysctl_set net.ipv4.ip_forward_update_priority 1
145	h1_create
146	h2_create
147	switch_create
148}
149
150cleanup()
151{
152	pre_cleanup
153
154	switch_destroy
155	h2_destroy
156	h1_destroy
157	sysctl_restore net.ipv4.ip_forward_update_priority
158
159	vrf_cleanup
160}
161
162ping_ipv4()
163{
164	ping_test $h1 192.0.2.18
165}
166
167dscp_ping_test()
168{
169	local vrf_name=$1; shift
170	local sip=$1; shift
171	local dip=$1; shift
172	local prio=$1; shift
173	local reprio=$1; shift
174	local dev1=$1; shift
175	local dev2=$1; shift
176	local i
177
178	local prio2=$($reprio $prio)   # ICMP Request egress prio
179	local prio3=$($reprio $prio2)  # ICMP Response egress prio
180
181	local dscp=$((prio << 2))     # ICMP Request ingress DSCP
182	local dscp2=$((prio2 << 2))   # ICMP Request egress DSCP
183	local dscp3=$((prio3 << 2))   # ICMP Response egress DSCP
184
185	RET=0
186
187	eval "local -A dev1_t0s=($(dscp_fetch_stats $dev1 0))"
188	eval "local -A dev2_t0s=($(dscp_fetch_stats $dev2 0))"
189
190	local ping_timeout=$((PING_TIMEOUT * 5))
191	ip vrf exec $vrf_name \
192	   ${PING} -Q $dscp ${sip:+-I $sip} $dip \
193		   -c 10 -i 0.5 -w $ping_timeout &> /dev/null
194
195	eval "local -A dev1_t1s=($(dscp_fetch_stats $dev1 0))"
196	eval "local -A dev2_t1s=($(dscp_fetch_stats $dev2 0))"
197
198	for i in {0..7}; do
199		local dscpi=$((i << 2))
200		local expect2=0
201		local expect3=0
202
203		if ((i == prio2)); then
204			expect2=10
205		fi
206		if ((i == prio3)); then
207			expect3=10
208		fi
209
210		local delta=$((dev2_t1s[$i] - dev2_t0s[$i]))
211		((expect2 == delta))
212		check_err $? "DSCP $dscpi@$dev2: Expected to capture $expect2 packets, got $delta."
213
214		delta=$((dev1_t1s[$i] - dev1_t0s[$i]))
215		((expect3 == delta))
216		check_err $? "DSCP $dscpi@$dev1: Expected to capture $expect3 packets, got $delta."
217	done
218
219	log_test "DSCP rewrite: $dscp-(prio $prio2)-$dscp2-(prio $prio3)-$dscp3"
220}
221
222__test_update()
223{
224	local update=$1; shift
225	local reprio=$1; shift
226	local prio
227
228	sysctl_restore net.ipv4.ip_forward_update_priority
229	sysctl_set net.ipv4.ip_forward_update_priority $update
230
231	for prio in {0..7}; do
232		dscp_ping_test v$h1 192.0.2.1 192.0.2.18 $prio $reprio $h1 $h2
233	done
234}
235
236test_update()
237{
238	echo "Test net.ipv4.ip_forward_update_priority=1"
239	__test_update 1 reprioritize
240}
241
242test_no_update()
243{
244	echo "Test net.ipv4.ip_forward_update_priority=0"
245	__test_update 0 echo
246}
247
248# Test that when DSCP is updated in pedit, the DSCP rewrite is turned off.
249test_pedit_norewrite()
250{
251	echo "Test no DSCP rewrite after DSCP is updated by pedit"
252
253	tc filter add dev $swp1 ingress handle 101 pref 1 prot ip flower \
254	    action pedit ex munge ip dsfield set $((3 << 2)) retain 0xfc \
255	    action skbedit priority 3
256
257	__test_update 0 three
258
259	tc filter del dev $swp1 ingress pref 1
260}
261
262# Test that when the last APP rule is removed, the prio->DSCP map is properly
263# set to zeroes, and that the last APP rule does not stay active in the ASIC.
264test_dscp_leftover()
265{
266	echo "Test that last removed DSCP rule is deconfigured correctly"
267
268	lldptool -T -i $swp2 -V APP -d $(dscp_map 0) >/dev/null
269	lldpad_app_wait_del
270
271	__test_update 0 zero
272
273	lldptool -T -i $swp2 -V APP $(dscp_map 0) >/dev/null
274	lldpad_app_wait_set $swp2
275}
276
277trap cleanup EXIT
278
279setup_prepare
280setup_wait
281
282tests_run
283
284exit $EXIT_STATUS
285