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