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
117__test_defprio()
118{
119	local prio_install=$1; shift
120	local prio_observe=$1; shift
121	local key
122	local t1
123	local i
124
125	RET=0
126
127	defprio_install $swp1 $prio_install
128
129	local t0=$(ethtool_stats_get $swp1 rx_frames_prio_$prio_observe)
130	mausezahn -q $h1 -d 100m -c 10 -t arp reply
131	t1=$(busywait "$HIT_TIMEOUT" until_counter_is ">= $((t0 + 10))" \
132		ethtool_stats_get $swp1 rx_frames_prio_$prio_observe)
133
134	check_err $? "Default priority $prio_install/$prio_observe: Expected to capture 10 packets, got $((t1 - t0))."
135	log_test "Default priority $prio_install/$prio_observe"
136
137	defprio_uninstall $swp1 $prio_install
138}
139
140test_defprio()
141{
142	local prio
143
144	for prio in {0..7}; do
145		__test_defprio $prio $prio
146	done
147
148	defprio_install $swp1 3
149	__test_defprio 0 3
150	__test_defprio 1 3
151	__test_defprio 2 3
152	__test_defprio 4 4
153	__test_defprio 5 5
154	__test_defprio 6 6
155	__test_defprio 7 7
156	defprio_uninstall $swp1 3
157}
158
159trap cleanup EXIT
160
161setup_prepare
162setup_wait
163
164tests_run
165
166exit $EXIT_STATUS
167