summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/drivers/net/ocelot/basic_qos.sh
blob: c51c83421c61116f43bd76efcac98bc26d18db0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright 2022 NXP

# The script is mostly generic, with the exception of the
# ethtool per-TC counter names ("rx_green_prio_${tc}")

WAIT_TIME=1
NUM_NETIFS=4
STABLE_MAC_ADDRS=yes
NETIF_CREATE=no
lib_dir=$(dirname $0)/../../../net/forwarding
source $lib_dir/tc_common.sh
source $lib_dir/lib.sh

require_command dcb

h1=${NETIFS[p1]}
swp1=${NETIFS[p2]}
swp2=${NETIFS[p3]}
h2=${NETIFS[p4]}

H1_IPV4="192.0.2.1"
H2_IPV4="192.0.2.2"
H1_IPV6="2001:db8:1::1"
H2_IPV6="2001:db8:1::2"

h1_create()
{
	simple_if_init $h1 $H1_IPV4/24 $H1_IPV6/64
}

h1_destroy()
{
	simple_if_fini $h1 $H1_IPV4/24 $H1_IPV6/64
}

h2_create()
{
	simple_if_init $h2 $H2_IPV4/24 $H2_IPV6/64
}

h2_destroy()
{
	simple_if_fini $h2 $H2_IPV4/24 $H2_IPV6/64
}

h1_vlan_create()
{
	local vid=$1

	vlan_create $h1 $vid
	simple_if_init $h1.$vid $H1_IPV4/24 $H1_IPV6/64
	ip link set $h1.$vid type vlan \
		egress-qos-map 0:0 1:1 2:2 3:3 4:4 5:5 6:6 7:7 \
		ingress-qos-map 0:0 1:1 2:2 3:3 4:4 5:5 6:6 7:7
}

h1_vlan_destroy()
{
	local vid=$1

	simple_if_fini $h1.$vid $H1_IPV4/24 $H1_IPV6/64
	vlan_destroy $h1 $vid
}

h2_vlan_create()
{
	local vid=$1

	vlan_create $h2 $vid
	simple_if_init $h2.$vid $H2_IPV4/24 $H2_IPV6/64
	ip link set $h2.$vid type vlan \
		egress-qos-map 0:0 1:1 2:2 3:3 4:4 5:5 6:6 7:7 \
		ingress-qos-map 0:0 1:1 2:2 3:3 4:4 5:5 6:6 7:7
}

h2_vlan_destroy()
{
	local vid=$1

	simple_if_fini $h2.$vid $H2_IPV4/24 $H2_IPV6/64
	vlan_destroy $h2 $vid
}

vlans_prepare()
{
	h1_vlan_create 100
	h2_vlan_create 100

	tc qdisc add dev ${h1}.100 clsact
	tc filter add dev ${h1}.100 egress protocol ipv4 \
		flower ip_proto icmp action skbedit priority 3
	tc filter add dev ${h1}.100 egress protocol ipv6 \
		flower ip_proto icmpv6 action skbedit priority 3
}

vlans_destroy()
{
	tc qdisc del dev ${h1}.100 clsact

	h1_vlan_destroy 100
	h2_vlan_destroy 100
}

switch_create()
{
	ip link set ${swp1} up
	ip link set ${swp2} up

	# Ports should trust VLAN PCP even with vlan_filtering=0
	ip link add br0 type bridge
	ip link set ${swp1} master br0
	ip link set ${swp2} master br0
	ip link set br0 up
}

switch_destroy()
{
	ip link del br0
}

setup_prepare()
{
	vrf_prepare

	h1_create
	h2_create
	switch_create
}

cleanup()
{
	pre_cleanup

	h2_destroy
	h1_destroy
	switch_destroy

	vrf_cleanup
}

dscp_cs_to_tos()
{
	local dscp_cs=$1

	# https://datatracker.ietf.org/doc/html/rfc2474
	# 4.2.2.1  The Class Selector Codepoints
	echo $((${dscp_cs} << 5))
}

run_test()
{
	local test_name=$1; shift
	local if_name=$1; shift
	local tc=$1; shift
	local tos=$1; shift
	local counter_name="rx_green_prio_${tc}"
	local ipv4_before
	local ipv4_after
	local ipv6_before
	local ipv6_after

	ipv4_before=$(ethtool_stats_get ${swp1} "${counter_name}")
	ping_do ${if_name} $H2_IPV4 "-Q ${tos}"
	ipv4_after=$(ethtool_stats_get ${swp1} "${counter_name}")

	if [ $((${ipv4_after} - ${ipv4_before})) -lt ${PING_COUNT} ]; then
		RET=1
	else
		RET=0
	fi
	log_test "IPv4 ${test_name}"

	ipv6_before=$(ethtool_stats_get ${swp1} "${counter_name}")
	ping_do ${if_name} $H2_IPV6 "-Q ${tos}"
	ipv6_after=$(ethtool_stats_get ${swp1} "${counter_name}")

	if [ $((${ipv6_after} - ${ipv6_before})) -lt ${PING_COUNT} ]; then
		RET=1
	else
		RET=0
	fi
	log_test "IPv6 ${test_name}"
}

port_default_prio_get()
{
	local if_name=$1
	local prio

	prio="$(dcb -j app show dev ${if_name} default-prio | \
		jq '.default_prio[]')"
	if [ -z "${prio}" ]; then
		prio=0
	fi

	echo ${prio}
}

test_port_default()
{
	local orig=$(port_default_prio_get ${swp1})
	local dmac=$(mac_get ${h2})

	dcb app replace dev ${swp1} default-prio 5

	run_test "Port-default QoS classification" ${h1} 5 0

	dcb app replace dev ${swp1} default-prio ${orig}
}

test_vlan_pcp()
{
	vlans_prepare

	run_test "Trusted VLAN PCP QoS classification" ${h1}.100 3 0

	vlans_destroy
}

test_ip_dscp()
{
	local port_default=$(port_default_prio_get ${swp1})
	local tos=$(dscp_cs_to_tos 4)

	dcb app add dev ${swp1} dscp-prio CS4:4
	run_test "Trusted DSCP QoS classification" ${h1} 4 ${tos}
	dcb app del dev ${swp1} dscp-prio CS4:4

	vlans_prepare
	run_test "Untrusted DSCP QoS classification follows VLAN PCP" \
		${h1}.100 3 ${tos}
	vlans_destroy

	run_test "Untrusted DSCP QoS classification follows port default" \
		${h1} ${port_default} ${tos}
}

trap cleanup EXIT

ALL_TESTS="
	test_port_default
	test_vlan_pcp
	test_ip_dscp
"

setup_prepare
setup_wait

tests_run

exit $EXIT_STATUS