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	local dscp;
40
41	simple_if_init $h1 192.0.2.1/28
42	tc qdisc add dev $h1 clsact
43	dscp_capture_install $h1 10
44}
45
46h1_destroy()
47{
48	dscp_capture_uninstall $h1 10
49	tc qdisc del dev $h1 clsact
50	simple_if_fini $h1 192.0.2.1/28
51}
52
53h2_create()
54{
55	simple_if_init $h2 192.0.2.2/28
56	tc qdisc add dev $h2 clsact
57	dscp_capture_install $h2 20
58}
59
60h2_destroy()
61{
62	dscp_capture_uninstall $h2 20
63	tc qdisc del dev $h2 clsact
64	simple_if_fini $h2 192.0.2.2/28
65}
66
67dscp_map()
68{
69	local base=$1; shift
70
71	for prio in {0..7}; do
72		echo app=$prio,5,$((base + prio))
73	done
74}
75
76switch_create()
77{
78	ip link add name br1 type bridge vlan_filtering 1
79	ip link set dev br1 up
80	ip link set dev $swp1 master br1
81	ip link set dev $swp1 up
82	ip link set dev $swp2 master br1
83	ip link set dev $swp2 up
84
85	lldptool -T -i $swp1 -V APP $(dscp_map 10) >/dev/null
86	lldptool -T -i $swp2 -V APP $(dscp_map 20) >/dev/null
87	lldpad_app_wait_set $swp1
88	lldpad_app_wait_set $swp2
89}
90
91switch_destroy()
92{
93	lldptool -T -i $swp2 -V APP -d $(dscp_map 20) >/dev/null
94	lldptool -T -i $swp1 -V APP -d $(dscp_map 10) >/dev/null
95	lldpad_app_wait_del
96
97	ip link set dev $swp2 nomaster
98	ip link set dev $swp1 nomaster
99	ip link del dev br1
100}
101
102setup_prepare()
103{
104	h1=${NETIFS[p1]}
105	swp1=${NETIFS[p2]}
106
107	swp2=${NETIFS[p3]}
108	h2=${NETIFS[p4]}
109
110	vrf_prepare
111
112	h1_create
113	h2_create
114	switch_create
115}
116
117cleanup()
118{
119	pre_cleanup
120
121	switch_destroy
122	h2_destroy
123	h1_destroy
124
125	vrf_cleanup
126}
127
128ping_ipv4()
129{
130	ping_test $h1 192.0.2.2
131}
132
133dscp_ping_test()
134{
135	local vrf_name=$1; shift
136	local sip=$1; shift
137	local dip=$1; shift
138	local prio=$1; shift
139	local dev_10=$1; shift
140	local dev_20=$1; shift
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	for prio in {0..7}; do
179		dscp_ping_test v$h1 192.0.2.1 192.0.2.2 $prio $h1 $h2
180	done
181}
182
183trap cleanup EXIT
184
185setup_prepare
186setup_wait
187
188tests_run
189
190exit $EXIT_STATUS
191