1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# Test for port-default priority. Non-IP packets ingress $swp1 and are
5# prioritized according to the default priority specified at the port.
6# rx_octets_prio_* counters are used to verify the prioritization.
7#
8# +-----------------------+
9# | H1                    |
10# |    + $h1              |
11# |    | 192.0.2.1/28     |
12# +----|------------------+
13#      |
14# +----|------------------+
15# | SW |                  |
16# |    + $swp1            |
17# |      192.0.2.2/28     |
18# |      APP=<prio>,1,0   |
19# +-----------------------+
20
21ALL_TESTS="
22	ping_ipv4
23	test_defprio
24"
25
26lib_dir=$(dirname $0)/../../../net/forwarding
27
28NUM_NETIFS=2
29: ${HIT_TIMEOUT:=1000} # ms
30source $lib_dir/lib.sh
31
32declare -a APP
33
34defprio_install()
35{
36	local dev=$1; shift
37	local prio=$1; shift
38	local app="app=$prio,1,0"
39
40	lldptool -T -i $dev -V APP $app >/dev/null
41	lldpad_app_wait_set $dev
42	APP[$prio]=$app
43}
44
45defprio_uninstall()
46{
47	local dev=$1; shift
48	local prio=$1; shift
49	local app=${APP[$prio]}
50
51	lldptool -T -i $dev -V APP -d $app >/dev/null
52	lldpad_app_wait_del
53	unset APP[$prio]
54}
55
56defprio_flush()
57{
58	local dev=$1; shift
59	local prio
60
61	if ((${#APP[@]})); then
62		lldptool -T -i $dev -V APP -d ${APP[@]} >/dev/null
63	fi
64	lldpad_app_wait_del
65	APP=()
66}
67
68h1_create()
69{
70	simple_if_init $h1 192.0.2.1/28
71}
72
73h1_destroy()
74{
75	simple_if_fini $h1 192.0.2.1/28
76}
77
78switch_create()
79{
80	ip link set dev $swp1 up
81	ip addr add dev $swp1 192.0.2.2/28
82}
83
84switch_destroy()
85{
86	defprio_flush $swp1
87	ip addr del dev $swp1 192.0.2.2/28
88	ip link set dev $swp1 down
89}
90
91setup_prepare()
92{
93	h1=${NETIFS[p1]}
94	swp1=${NETIFS[p2]}
95
96	vrf_prepare
97
98	h1_create
99	switch_create
100}
101
102cleanup()
103{
104	pre_cleanup
105
106	switch_destroy
107	h1_destroy
108
109	vrf_cleanup
110}
111
112ping_ipv4()
113{
114	ping_test $h1 192.0.2.2
115}
116
117wait_for_packets()
118{
119	local t0=$1; shift
120	local prio_observe=$1; shift
121
122	local t1=$(ethtool_stats_get $swp1 rx_frames_prio_$prio_observe)
123	local delta=$((t1 - t0))
124	echo $delta
125	((delta >= 10))
126}
127
128__test_defprio()
129{
130	local prio_install=$1; shift
131	local prio_observe=$1; shift
132	local delta
133	local key
134	local i
135
136	RET=0
137
138	defprio_install $swp1 $prio_install
139
140	local t0=$(ethtool_stats_get $swp1 rx_frames_prio_$prio_observe)
141	mausezahn -q $h1 -d 100m -c 10 -t arp reply
142	delta=$(busywait "$HIT_TIMEOUT" wait_for_packets $t0 $prio_observe)
143
144	check_err $? "Default priority $prio_install/$prio_observe: Expected to capture 10 packets, got $delta."
145	log_test "Default priority $prio_install/$prio_observe"
146
147	defprio_uninstall $swp1 $prio_install
148}
149
150test_defprio()
151{
152	local prio
153
154	for prio in {0..7}; do
155		__test_defprio $prio $prio
156	done
157
158	defprio_install $swp1 3
159	__test_defprio 0 3
160	__test_defprio 1 3
161	__test_defprio 2 3
162	__test_defprio 4 4
163	__test_defprio 5 5
164	__test_defprio 6 6
165	__test_defprio 7 7
166	defprio_uninstall $swp1 3
167}
168
169trap cleanup EXIT
170
171setup_prepare
172setup_wait
173
174tests_run
175
176exit $EXIT_STATUS
177