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# |  |   dcb dscp-prio 10:0...17:7            dcb dscp-prio 20:0...27:7   |   |
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
65switch_create()
66{
67	ip link add name br1 type bridge vlan_filtering 1
68	ip link set dev br1 addrgenmode none
69	ip link set dev br1 up
70	ip link set dev $swp1 master br1
71	ip link set dev $swp1 up
72	ip link set dev $swp2 master br1
73	ip link set dev $swp2 up
74
75	dcb app add dev $swp1 dscp-prio 10:0 11:1 12:2 13:3 14:4 15:5 16:6 17:7
76	dcb app add dev $swp2 dscp-prio 20:0 21:1 22:2 23:3 24:4 25:5 26:6 27:7
77}
78
79switch_destroy()
80{
81	dcb app del dev $swp2 dscp-prio 20:0 21:1 22:2 23:3 24:4 25:5 26:6 27:7
82	dcb app del dev $swp1 dscp-prio 10:0 11:1 12:2 13:3 14:4 15:5 16:6 17:7
83
84	ip link set dev $swp2 down
85	ip link set dev $swp2 nomaster
86	ip link set dev $swp1 down
87	ip link set dev $swp1 nomaster
88	ip link del dev br1
89}
90
91setup_prepare()
92{
93	h1=${NETIFS[p1]}
94	swp1=${NETIFS[p2]}
95
96	swp2=${NETIFS[p3]}
97	h2=${NETIFS[p4]}
98
99	vrf_prepare
100
101	h1_create
102	h2_create
103	switch_create
104}
105
106cleanup()
107{
108	pre_cleanup
109
110	switch_destroy
111	h2_destroy
112	h1_destroy
113
114	vrf_cleanup
115}
116
117ping_ipv4()
118{
119	ping_test $h1 192.0.2.2
120}
121
122dscp_ping_test()
123{
124	local vrf_name=$1; shift
125	local sip=$1; shift
126	local dip=$1; shift
127	local prio=$1; shift
128	local dev_10=$1; shift
129	local dev_20=$1; shift
130	local key
131
132	local dscp_10=$(((prio + 10) << 2))
133	local dscp_20=$(((prio + 20) << 2))
134
135	RET=0
136
137	local -A t0s
138	eval "t0s=($(dscp_fetch_stats $dev_10 10)
139		   $(dscp_fetch_stats $dev_20 20))"
140
141	local ping_timeout=$((PING_TIMEOUT * 5))
142	ip vrf exec $vrf_name \
143	   ${PING} -Q $dscp_10 ${sip:+-I $sip} $dip \
144		   -c 10 -i 0.5 -w $ping_timeout &> /dev/null
145
146	local -A t1s
147	eval "t1s=($(dscp_fetch_stats $dev_10 10)
148		   $(dscp_fetch_stats $dev_20 20))"
149
150	for key in ${!t0s[@]}; do
151		local expect
152		if ((key == prio+10 || key == prio+20)); then
153			expect=10
154		else
155			expect=0
156		fi
157
158		local delta=$((t1s[$key] - t0s[$key]))
159		((expect == delta))
160		check_err $? "DSCP $key: Expected to capture $expect packets, got $delta."
161	done
162
163	log_test "DSCP rewrite: $dscp_10-(prio $prio)-$dscp_20"
164}
165
166test_dscp()
167{
168	local prio
169
170	for prio in {0..7}; do
171		dscp_ping_test v$h1 192.0.2.1 192.0.2.2 $prio $h1 $h2
172	done
173}
174
175trap cleanup EXIT
176
177setup_prepare
178setup_wait
179
180tests_run
181
182exit $EXIT_STATUS
183