1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# Test for DSCP prioritization and rewrite. Packets ingress $swp1 with a DSCP
5# tag and are prioritized according to the map at $swp1. They egress $swp2 and
6# the DSCP value is updated to match the map at that interface. The updated DSCP
7# tag is verified at $h2.
8#
9# ICMP responses are produced with the same DSCP tag that arrived at $h2. They
10# go through prioritization at $swp2 and DSCP retagging at $swp1. The tag is
11# verified at $h1--it should match the original tag.
12#
13# +----------------------+                             +----------------------+
14# | H1                   |                             |                   H2 |
15# |    + $h1             |                             |            $h2 +     |
16# |    | 192.0.2.1/28    |                             |   192.0.2.2/28 |     |
17# +----|-----------------+                             +----------------|-----+
18#      |                                                                |
19# +----|----------------------------------------------------------------|-----+
20# | SW |                                                                |     |
21# |  +-|----------------------------------------------------------------|-+   |
22# |  | + $swp1                       BR                           $swp2 + |   |
23# |  |   APP=0,5,10 .. 7,5,17                      APP=0,5,20 .. 7,5,27   |   |
24# |  +--------------------------------------------------------------------+   |
25# +---------------------------------------------------------------------------+
26
27ALL_TESTS="
28	ping_ipv4
29	test_dscp
30"
31
32lib_dir=$(dirname $0)/../../../net/forwarding
33
34NUM_NETIFS=4
35source $lib_dir/lib.sh
36
37h1_create()
38{
39	simple_if_init $h1 192.0.2.1/28
40	tc qdisc add dev $h1 clsact
41	dscp_capture_install $h1 10
42}
43
44h1_destroy()
45{
46	dscp_capture_uninstall $h1 10
47	tc qdisc del dev $h1 clsact
48	simple_if_fini $h1 192.0.2.1/28
49}
50
51h2_create()
52{
53	simple_if_init $h2 192.0.2.2/28
54	tc qdisc add dev $h2 clsact
55	dscp_capture_install $h2 20
56}
57
58h2_destroy()
59{
60	dscp_capture_uninstall $h2 20
61	tc qdisc del dev $h2 clsact
62	simple_if_fini $h2 192.0.2.2/28
63}
64
65dscp_map()
66{
67	local base=$1; shift
68	local prio
69
70	for prio in {0..7}; do
71		echo app=$prio,5,$((base + prio))
72	done
73}
74
75switch_create()
76{
77	ip link add name br1 type bridge vlan_filtering 1
78	ip link set dev br1 up
79	ip link set dev $swp1 master br1
80	ip link set dev $swp1 up
81	ip link set dev $swp2 master br1
82	ip link set dev $swp2 up
83
84	lldptool -T -i $swp1 -V APP $(dscp_map 10) >/dev/null
85	lldptool -T -i $swp2 -V APP $(dscp_map 20) >/dev/null
86	lldpad_app_wait_set $swp1
87	lldpad_app_wait_set $swp2
88}
89
90switch_destroy()
91{
92	lldptool -T -i $swp2 -V APP -d $(dscp_map 20) >/dev/null
93	lldptool -T -i $swp1 -V APP -d $(dscp_map 10) >/dev/null
94	lldpad_app_wait_del
95
96	ip link set dev $swp2 nomaster
97	ip link set dev $swp1 nomaster
98	ip link del dev br1
99}
100
101setup_prepare()
102{
103	h1=${NETIFS[p1]}
104	swp1=${NETIFS[p2]}
105
106	swp2=${NETIFS[p3]}
107	h2=${NETIFS[p4]}
108
109	vrf_prepare
110
111	h1_create
112	h2_create
113	switch_create
114}
115
116cleanup()
117{
118	pre_cleanup
119
120	switch_destroy
121	h2_destroy
122	h1_destroy
123
124	vrf_cleanup
125}
126
127ping_ipv4()
128{
129	ping_test $h1 192.0.2.2
130}
131
132dscp_ping_test()
133{
134	local vrf_name=$1; shift
135	local sip=$1; shift
136	local dip=$1; shift
137	local prio=$1; shift
138	local dev_10=$1; shift
139	local dev_20=$1; shift
140	local key
141
142	local dscp_10=$(((prio + 10) << 2))
143	local dscp_20=$(((prio + 20) << 2))
144
145	RET=0
146
147	local -A t0s
148	eval "t0s=($(dscp_fetch_stats $dev_10 10)
149		   $(dscp_fetch_stats $dev_20 20))"
150
151	local ping_timeout=$((PING_TIMEOUT * 5))
152	ip vrf exec $vrf_name \
153	   ${PING} -Q $dscp_10 ${sip:+-I $sip} $dip \
154		   -c 10 -i 0.5 -w $ping_timeout &> /dev/null
155
156	local -A t1s
157	eval "t1s=($(dscp_fetch_stats $dev_10 10)
158		   $(dscp_fetch_stats $dev_20 20))"
159
160	for key in ${!t0s[@]}; do
161		local expect
162		if ((key == prio+10 || key == prio+20)); then
163			expect=10
164		else
165			expect=0
166		fi
167
168		local delta=$((t1s[$key] - t0s[$key]))
169		((expect == delta))
170		check_err $? "DSCP $key: Expected to capture $expect packets, got $delta."
171	done
172
173	log_test "DSCP rewrite: $dscp_10-(prio $prio)-$dscp_20"
174}
175
176test_dscp()
177{
178	local prio
179
180	for prio in {0..7}; do
181		dscp_ping_test v$h1 192.0.2.1 192.0.2.2 $prio $h1 $h2
182	done
183}
184
185trap cleanup EXIT
186
187setup_prepare
188setup_wait
189
190tests_run
191
192exit $EXIT_STATUS
193