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_dscp_leftover
35"
36
37lib_dir=$(dirname $0)/../../../net/forwarding
38
39NUM_NETIFS=4
40source $lib_dir/lib.sh
41
42reprioritize()
43{
44	local in=$1; shift
45
46	# This is based on rt_tos2priority in include/net/route.h. Assuming 1:1
47	# mapping between priorities and TOS, it yields a new priority for a
48	# packet with ingress priority of $in.
49	local -a reprio=(0 0 2 2 6 6 4 4)
50
51	echo ${reprio[$in]}
52}
53
54zero()
55{
56    echo 0
57}
58
59h1_create()
60{
61	simple_if_init $h1 192.0.2.1/28
62	tc qdisc add dev $h1 clsact
63	dscp_capture_install $h1 0
64	ip route add vrf v$h1 192.0.2.16/28 via 192.0.2.2
65}
66
67h1_destroy()
68{
69	ip route del vrf v$h1 192.0.2.16/28 via 192.0.2.2
70	dscp_capture_uninstall $h1 0
71	tc qdisc del dev $h1 clsact
72	simple_if_fini $h1 192.0.2.1/28
73}
74
75h2_create()
76{
77	simple_if_init $h2 192.0.2.18/28
78	tc qdisc add dev $h2 clsact
79	dscp_capture_install $h2 0
80	ip route add vrf v$h2 192.0.2.0/28 via 192.0.2.17
81}
82
83h2_destroy()
84{
85	ip route del vrf v$h2 192.0.2.0/28 via 192.0.2.17
86	dscp_capture_uninstall $h2 0
87	tc qdisc del dev $h2 clsact
88	simple_if_fini $h2 192.0.2.18/28
89}
90
91dscp_map()
92{
93	local base=$1; shift
94	local prio
95
96	for prio in {0..7}; do
97		echo app=$prio,5,$((base + prio))
98	done
99}
100
101switch_create()
102{
103	simple_if_init $swp1 192.0.2.2/28
104	__simple_if_init $swp2 v$swp1 192.0.2.17/28
105
106	lldptool -T -i $swp1 -V APP $(dscp_map 0) >/dev/null
107	lldptool -T -i $swp2 -V APP $(dscp_map 0) >/dev/null
108	lldpad_app_wait_set $swp1
109	lldpad_app_wait_set $swp2
110}
111
112switch_destroy()
113{
114	lldptool -T -i $swp2 -V APP -d $(dscp_map 0) >/dev/null
115	lldptool -T -i $swp1 -V APP -d $(dscp_map 0) >/dev/null
116	lldpad_app_wait_del
117
118	__simple_if_fini $swp2 192.0.2.17/28
119	simple_if_fini $swp1 192.0.2.2/28
120}
121
122setup_prepare()
123{
124	h1=${NETIFS[p1]}
125	swp1=${NETIFS[p2]}
126
127	swp2=${NETIFS[p3]}
128	h2=${NETIFS[p4]}
129
130	vrf_prepare
131
132	sysctl_set net.ipv4.ip_forward_update_priority 1
133	h1_create
134	h2_create
135	switch_create
136}
137
138cleanup()
139{
140	pre_cleanup
141
142	switch_destroy
143	h2_destroy
144	h1_destroy
145	sysctl_restore net.ipv4.ip_forward_update_priority
146
147	vrf_cleanup
148}
149
150ping_ipv4()
151{
152	ping_test $h1 192.0.2.18
153}
154
155dscp_ping_test()
156{
157	local vrf_name=$1; shift
158	local sip=$1; shift
159	local dip=$1; shift
160	local prio=$1; shift
161	local reprio=$1; shift
162	local dev1=$1; shift
163	local dev2=$1; shift
164	local i
165
166	local prio2=$($reprio $prio)   # ICMP Request egress prio
167	local prio3=$($reprio $prio2)  # ICMP Response egress prio
168
169	local dscp=$((prio << 2))     # ICMP Request ingress DSCP
170	local dscp2=$((prio2 << 2))   # ICMP Request egress DSCP
171	local dscp3=$((prio3 << 2))   # ICMP Response egress DSCP
172
173	RET=0
174
175	eval "local -A dev1_t0s=($(dscp_fetch_stats $dev1 0))"
176	eval "local -A dev2_t0s=($(dscp_fetch_stats $dev2 0))"
177
178	local ping_timeout=$((PING_TIMEOUT * 5))
179	ip vrf exec $vrf_name \
180	   ${PING} -Q $dscp ${sip:+-I $sip} $dip \
181		   -c 10 -i 0.5 -w $ping_timeout &> /dev/null
182
183	eval "local -A dev1_t1s=($(dscp_fetch_stats $dev1 0))"
184	eval "local -A dev2_t1s=($(dscp_fetch_stats $dev2 0))"
185
186	for i in {0..7}; do
187		local dscpi=$((i << 2))
188		local expect2=0
189		local expect3=0
190
191		if ((i == prio2)); then
192			expect2=10
193		fi
194		if ((i == prio3)); then
195			expect3=10
196		fi
197
198		local delta=$((dev2_t1s[$i] - dev2_t0s[$i]))
199		((expect2 == delta))
200		check_err $? "DSCP $dscpi@$dev2: Expected to capture $expect2 packets, got $delta."
201
202		delta=$((dev1_t1s[$i] - dev1_t0s[$i]))
203		((expect3 == delta))
204		check_err $? "DSCP $dscpi@$dev1: Expected to capture $expect3 packets, got $delta."
205	done
206
207	log_test "DSCP rewrite: $dscp-(prio $prio2)-$dscp2-(prio $prio3)-$dscp3"
208}
209
210__test_update()
211{
212	local update=$1; shift
213	local reprio=$1; shift
214	local prio
215
216	sysctl_restore net.ipv4.ip_forward_update_priority
217	sysctl_set net.ipv4.ip_forward_update_priority $update
218
219	for prio in {0..7}; do
220		dscp_ping_test v$h1 192.0.2.1 192.0.2.18 $prio $reprio $h1 $h2
221	done
222}
223
224test_update()
225{
226	__test_update 1 reprioritize
227}
228
229test_no_update()
230{
231	__test_update 0 echo
232}
233
234# Test that when the last APP rule is removed, the prio->DSCP map is properly
235# set to zeroes, and that the last APP rule does not stay active in the ASIC.
236test_dscp_leftover()
237{
238	lldptool -T -i $swp2 -V APP -d $(dscp_map 0) >/dev/null
239	lldpad_app_wait_del
240
241	__test_update 0 zero
242
243	lldptool -T -i $swp2 -V APP $(dscp_map 0) >/dev/null
244	lldpad_app_wait_set $swp2
245}
246
247trap cleanup EXIT
248
249setup_prepare
250setup_wait
251
252tests_run
253
254exit $EXIT_STATUS
255