1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4ALL_TESTS="
5	port_pool_test
6	port_tc_ip_test
7	port_tc_arp_test
8"
9
10NUM_NETIFS=2
11source ../../../net/forwarding/lib.sh
12source ../../../net/forwarding/devlink_lib.sh
13source mlxsw_lib.sh
14
15SB_POOL_ING=0
16SB_POOL_EGR_CPU=10
17
18SB_ITC_CPU_IP=2
19SB_ITC_CPU_ARP=2
20SB_ITC=0
21
22h1_create()
23{
24	simple_if_init $h1 192.0.1.1/24
25	tc qdisc add dev $h1 clsact
26
27	# Add egress filter on $h1 that will guarantee that the packet sent,
28	# will be the only packet being passed to the device.
29	tc filter add dev $h1 egress pref 2 handle 102 matchall action drop
30}
31
32h1_destroy()
33{
34	tc filter del dev $h1 egress pref 2 handle 102 matchall action drop
35	tc qdisc del dev $h1 clsact
36	simple_if_fini $h1 192.0.1.1/24
37}
38
39h2_create()
40{
41	simple_if_init $h2 192.0.1.2/24
42	tc qdisc add dev $h2 clsact
43
44	# Add egress filter on $h2 that will guarantee that the packet sent,
45	# will be the only packet being passed to the device.
46	tc filter add dev $h2 egress pref 1 handle 101 matchall action drop
47}
48
49h2_destroy()
50{
51	tc filter del dev $h2 egress pref 1 handle 101 matchall action drop
52	tc qdisc del dev $h2 clsact
53	simple_if_fini $h2 192.0.1.2/24
54}
55
56sb_occ_pool_check()
57{
58	local dl_port=$1; shift
59	local pool=$1; shift
60	local exp_max_occ=$1
61	local max_occ
62	local err=0
63
64	max_occ=$(devlink sb -j occupancy show $dl_port \
65		  | jq -e ".[][][\"pool\"][\"$pool\"][\"max\"]")
66
67	if [[ "$max_occ" -ne "$exp_max_occ" ]]; then
68		err=1
69	fi
70
71	echo $max_occ
72	return $err
73}
74
75sb_occ_itc_check()
76{
77	local dl_port=$1; shift
78	local itc=$1; shift
79	local exp_max_occ=$1
80	local max_occ
81	local err=0
82
83	max_occ=$(devlink sb -j occupancy show $dl_port \
84		  | jq -e ".[][][\"itc\"][\"$itc\"][\"max\"]")
85
86	if [[ "$max_occ" -ne "$exp_max_occ" ]]; then
87		err=1
88	fi
89
90	echo $max_occ
91	return $err
92}
93
94sb_occ_etc_check()
95{
96	local dl_port=$1; shift
97	local etc=$1; shift
98	local exp_max_occ=$1; shift
99	local max_occ
100	local err=0
101
102	max_occ=$(devlink sb -j occupancy show $dl_port \
103		  | jq -e ".[][][\"etc\"][\"$etc\"][\"max\"]")
104
105	if [[ "$max_occ" -ne "$exp_max_occ" ]]; then
106		err=1
107	fi
108
109	echo $max_occ
110	return $err
111}
112
113port_pool_test()
114{
115	local exp_max_occ=$(devlink_cell_size_get)
116	local max_occ
117
118	tc filter add dev $h1 egress protocol ip pref 1 handle 101 flower \
119		src_mac $h1mac dst_mac $h2mac \
120		src_ip 192.0.1.1 dst_ip 192.0.1.2 \
121		action pass
122
123	devlink sb occupancy clearmax $DEVLINK_DEV
124
125	$MZ $h1 -c 1 -p 10 -a $h1mac -b $h2mac -A 192.0.1.1 -B 192.0.1.2 \
126		-t ip -q
127
128	devlink sb occupancy snapshot $DEVLINK_DEV
129
130	RET=0
131	max_occ=$(sb_occ_pool_check $dl_port2 $SB_POOL_ING $exp_max_occ)
132	check_err $? "Expected iPool($SB_POOL_ING) max occupancy to be $exp_max_occ, but got $max_occ"
133	log_test "physical port's($h2) ingress pool"
134
135	RET=0
136	max_occ=$(sb_occ_pool_check $cpu_dl_port $SB_POOL_EGR_CPU $exp_max_occ)
137	check_err $? "Expected ePool($SB_POOL_EGR_CPU) max occupancy to be $exp_max_occ, but got $max_occ"
138	log_test "CPU port's egress pool"
139
140	tc filter del dev $h1 egress protocol ip pref 1 handle 101 flower \
141		src_mac $h1mac dst_mac $h2mac \
142		src_ip 192.0.1.1 dst_ip 192.0.1.2 \
143		action pass
144}
145
146port_tc_ip_test()
147{
148	local exp_max_occ=$(devlink_cell_size_get)
149	local max_occ
150
151	tc filter add dev $h1 egress protocol ip pref 1 handle 101 flower \
152		src_mac $h1mac dst_mac $h2mac \
153		src_ip 192.0.1.1 dst_ip 192.0.1.2 \
154		action pass
155
156	devlink sb occupancy clearmax $DEVLINK_DEV
157
158	$MZ $h1 -c 1 -p 10 -a $h1mac -b $h2mac -A 192.0.1.1 -B 192.0.1.2 \
159		-t ip -q
160
161	devlink sb occupancy snapshot $DEVLINK_DEV
162
163	RET=0
164	max_occ=$(sb_occ_itc_check $dl_port2 $SB_ITC $exp_max_occ)
165	check_err $? "Expected ingress TC($SB_ITC) max occupancy to be $exp_max_occ, but got $max_occ"
166	log_test "physical port's($h2) ingress TC - IP packet"
167
168	RET=0
169	max_occ=$(sb_occ_etc_check $cpu_dl_port $SB_ITC_CPU_IP $exp_max_occ)
170	check_err $? "Expected egress TC($SB_ITC_CPU_IP) max occupancy to be $exp_max_occ, but got $max_occ"
171	log_test "CPU port's egress TC - IP packet"
172
173	tc filter del dev $h1 egress protocol ip pref 1 handle 101 flower \
174		src_mac $h1mac dst_mac $h2mac \
175		src_ip 192.0.1.1 dst_ip 192.0.1.2 \
176		action pass
177}
178
179port_tc_arp_test()
180{
181	local exp_max_occ=$(devlink_cell_size_get)
182	local max_occ
183
184	tc filter add dev $h1 egress protocol arp pref 1 handle 101 flower \
185		src_mac $h1mac action pass
186
187	devlink sb occupancy clearmax $DEVLINK_DEV
188
189	$MZ $h1 -c 1 -p 10 -a $h1mac -A 192.0.1.1 -t arp -q
190
191	devlink sb occupancy snapshot $DEVLINK_DEV
192
193	RET=0
194	max_occ=$(sb_occ_itc_check $dl_port2 $SB_ITC $exp_max_occ)
195	check_err $? "Expected ingress TC($SB_ITC) max occupancy to be $exp_max_occ, but got $max_occ"
196	log_test "physical port's($h2) ingress TC - ARP packet"
197
198	RET=0
199	max_occ=$(sb_occ_etc_check $cpu_dl_port $SB_ITC_CPU_ARP $exp_max_occ)
200	check_err $? "Expected egress TC($SB_ITC_IP2ME) max occupancy to be $exp_max_occ, but got $max_occ"
201	log_test "CPU port's egress TC - ARP packet"
202
203	tc filter del dev $h1 egress protocol arp pref 1 handle 101 flower \
204		src_mac $h1mac action pass
205}
206
207setup_prepare()
208{
209	h1=${NETIFS[p1]}
210	h2=${NETIFS[p2]}
211
212	h1mac=$(mac_get $h1)
213	h2mac=$(mac_get $h2)
214
215	dl_port1=$(devlink_port_by_netdev $h1)
216	dl_port2=$(devlink_port_by_netdev $h2)
217
218	cpu_dl_port=$(devlink_cpu_port_get)
219
220	vrf_prepare
221
222	h1_create
223	h2_create
224}
225
226cleanup()
227{
228	pre_cleanup
229
230	h2_destroy
231	h1_destroy
232
233	vrf_cleanup
234}
235
236trap cleanup EXIT
237
238setup_prepare
239setup_wait
240
241tests_run
242
243exit $EXIT_STATUS
244