1bb24d592SIdo Schimmel#!/bin/bash
2bb24d592SIdo Schimmel# SPDX-License-Identifier: GPL-2.0
3bb24d592SIdo Schimmel#
4bb24d592SIdo Schimmel# Test that packets are sampled when tc-sample is used and that reported
5bb24d592SIdo Schimmel# metadata is correct. Two sets of hosts (with and without LAG) are used, since
6bb24d592SIdo Schimmel# metadata extraction in mlxsw is a bit different when LAG is involved.
7bb24d592SIdo Schimmel#
8bb24d592SIdo Schimmel# +---------------------------------+       +---------------------------------+
9bb24d592SIdo Schimmel# | H1 (vrf)                        |       | H3 (vrf)                        |
10bb24d592SIdo Schimmel# |    + $h1                        |       |    + $h3_lag                    |
11bb24d592SIdo Schimmel# |    | 192.0.2.1/28               |       |    | 192.0.2.17/28              |
12bb24d592SIdo Schimmel# |    |                            |       |    |                            |
13bb24d592SIdo Schimmel# |    |  default via 192.0.2.2     |       |    |  default via 192.0.2.18    |
14bb24d592SIdo Schimmel# +----|----------------------------+       +----|----------------------------+
15bb24d592SIdo Schimmel#      |                                         |
16bb24d592SIdo Schimmel# +----|-----------------------------------------|----------------------------+
17bb24d592SIdo Schimmel# |    | 192.0.2.2/28                            | 192.0.2.18/28              |
18bb24d592SIdo Schimmel# |    + $rp1                                    + $rp3_lag                   |
19bb24d592SIdo Schimmel# |                                                                           |
20bb24d592SIdo Schimmel# |    + $rp2                                    + $rp4_lag                   |
21bb24d592SIdo Schimmel# |    | 198.51.100.2/28                         | 198.51.100.18/28           |
22bb24d592SIdo Schimmel# +----|-----------------------------------------|----------------------------+
23bb24d592SIdo Schimmel#      |                                         |
24bb24d592SIdo Schimmel# +----|----------------------------+       +----|----------------------------+
25bb24d592SIdo Schimmel# |    |  default via 198.51.100.2  |       |    |  default via 198.51.100.18 |
26bb24d592SIdo Schimmel# |    |                            |       |    |                            |
27bb24d592SIdo Schimmel# |    | 198.51.100.1/28            |       |    | 198.51.100.17/28           |
28bb24d592SIdo Schimmel# |    + $h2                        |       |    + $h4_lag                    |
29bb24d592SIdo Schimmel# | H2 (vrf)                        |       | H4 (vrf)                        |
30bb24d592SIdo Schimmel# +---------------------------------+       +---------------------------------+
31bb24d592SIdo Schimmel
32bb24d592SIdo Schimmellib_dir=$(dirname $0)/../../../net/forwarding
33bb24d592SIdo Schimmel
34bb24d592SIdo SchimmelALL_TESTS="
35bb24d592SIdo Schimmel	tc_sample_rate_test
36bb24d592SIdo Schimmel	tc_sample_max_rate_test
377ede22e6SIdo Schimmel	tc_sample_conflict_test
38bb24d592SIdo Schimmel	tc_sample_group_conflict_test
39bb24d592SIdo Schimmel	tc_sample_md_iif_test
40bb24d592SIdo Schimmel	tc_sample_md_lag_iif_test
41bb24d592SIdo Schimmel	tc_sample_md_oif_test
42bb24d592SIdo Schimmel	tc_sample_md_lag_oif_test
43bb24d592SIdo Schimmel	tc_sample_md_out_tc_test
44bb24d592SIdo Schimmel	tc_sample_md_out_tc_occ_test
45f0b692c4SIdo Schimmel	tc_sample_md_latency_test
46f0b692c4SIdo Schimmel	tc_sample_acl_group_conflict_test
47f0b692c4SIdo Schimmel	tc_sample_acl_rate_test
48f0b692c4SIdo Schimmel	tc_sample_acl_max_rate_test
49bb24d592SIdo Schimmel"
50bb24d592SIdo SchimmelNUM_NETIFS=8
51bb24d592SIdo SchimmelCAPTURE_FILE=$(mktemp)
52bb24d592SIdo Schimmelsource $lib_dir/lib.sh
53bb24d592SIdo Schimmelsource $lib_dir/devlink_lib.sh
54*b8bfafe4SPetr Machatasource mlxsw_lib.sh
55bb24d592SIdo Schimmel
56bb24d592SIdo Schimmel# Available at https://github.com/Mellanox/libpsample
57bb24d592SIdo Schimmelrequire_command psample
58bb24d592SIdo Schimmel
59bb24d592SIdo Schimmelh1_create()
60bb24d592SIdo Schimmel{
61bb24d592SIdo Schimmel	simple_if_init $h1 192.0.2.1/28
62bb24d592SIdo Schimmel
63bb24d592SIdo Schimmel	ip -4 route add default vrf v$h1 nexthop via 192.0.2.2
64bb24d592SIdo Schimmel}
65bb24d592SIdo Schimmel
66bb24d592SIdo Schimmelh1_destroy()
67bb24d592SIdo Schimmel{
68bb24d592SIdo Schimmel	ip -4 route del default vrf v$h1 nexthop via 192.0.2.2
69bb24d592SIdo Schimmel
70bb24d592SIdo Schimmel	simple_if_fini $h1 192.0.2.1/28
71bb24d592SIdo Schimmel}
72bb24d592SIdo Schimmel
73bb24d592SIdo Schimmelh2_create()
74bb24d592SIdo Schimmel{
75bb24d592SIdo Schimmel	simple_if_init $h2 198.51.100.1/28
76bb24d592SIdo Schimmel
77bb24d592SIdo Schimmel	ip -4 route add default vrf v$h2 nexthop via 198.51.100.2
78bb24d592SIdo Schimmel}
79bb24d592SIdo Schimmel
80bb24d592SIdo Schimmelh2_destroy()
81bb24d592SIdo Schimmel{
82bb24d592SIdo Schimmel	ip -4 route del default vrf v$h2 nexthop via 198.51.100.2
83bb24d592SIdo Schimmel
84bb24d592SIdo Schimmel	simple_if_fini $h2 198.51.100.1/28
85bb24d592SIdo Schimmel}
86bb24d592SIdo Schimmel
87bb24d592SIdo Schimmelh3_create()
88bb24d592SIdo Schimmel{
89bb24d592SIdo Schimmel	ip link set dev $h3 down
90bb24d592SIdo Schimmel	ip link add name ${h3}_bond type bond mode 802.3ad
91bb24d592SIdo Schimmel	ip link set dev $h3 master ${h3}_bond
92bb24d592SIdo Schimmel
93bb24d592SIdo Schimmel	simple_if_init ${h3}_bond 192.0.2.17/28
94bb24d592SIdo Schimmel
95bb24d592SIdo Schimmel	ip -4 route add default vrf v${h3}_bond nexthop via 192.0.2.18
96bb24d592SIdo Schimmel}
97bb24d592SIdo Schimmel
98bb24d592SIdo Schimmelh3_destroy()
99bb24d592SIdo Schimmel{
100bb24d592SIdo Schimmel	ip -4 route del default vrf v${h3}_bond nexthop via 192.0.2.18
101bb24d592SIdo Schimmel
102bb24d592SIdo Schimmel	simple_if_fini ${h3}_bond 192.0.2.17/28
103bb24d592SIdo Schimmel
104bb24d592SIdo Schimmel	ip link set dev $h3 nomaster
105bb24d592SIdo Schimmel	ip link del dev ${h3}_bond
106bb24d592SIdo Schimmel}
107bb24d592SIdo Schimmel
108bb24d592SIdo Schimmelh4_create()
109bb24d592SIdo Schimmel{
110bb24d592SIdo Schimmel	ip link set dev $h4 down
111bb24d592SIdo Schimmel	ip link add name ${h4}_bond type bond mode 802.3ad
112bb24d592SIdo Schimmel	ip link set dev $h4 master ${h4}_bond
113bb24d592SIdo Schimmel
114bb24d592SIdo Schimmel	simple_if_init ${h4}_bond 198.51.100.17/28
115bb24d592SIdo Schimmel
116bb24d592SIdo Schimmel	ip -4 route add default vrf v${h4}_bond nexthop via 198.51.100.18
117bb24d592SIdo Schimmel}
118bb24d592SIdo Schimmel
119bb24d592SIdo Schimmelh4_destroy()
120bb24d592SIdo Schimmel{
121bb24d592SIdo Schimmel	ip -4 route del default vrf v${h4}_bond nexthop via 198.51.100.18
122bb24d592SIdo Schimmel
123bb24d592SIdo Schimmel	simple_if_fini ${h4}_bond 198.51.100.17/28
124bb24d592SIdo Schimmel
125bb24d592SIdo Schimmel	ip link set dev $h4 nomaster
126bb24d592SIdo Schimmel	ip link del dev ${h4}_bond
127bb24d592SIdo Schimmel}
128bb24d592SIdo Schimmel
129bb24d592SIdo Schimmelrouter_create()
130bb24d592SIdo Schimmel{
131bb24d592SIdo Schimmel	ip link set dev $rp1 up
132bb24d592SIdo Schimmel	__addr_add_del $rp1 add 192.0.2.2/28
133bb24d592SIdo Schimmel	tc qdisc add dev $rp1 clsact
134bb24d592SIdo Schimmel
135bb24d592SIdo Schimmel	ip link set dev $rp2 up
136bb24d592SIdo Schimmel	__addr_add_del $rp2 add 198.51.100.2/28
137bb24d592SIdo Schimmel	tc qdisc add dev $rp2 clsact
138bb24d592SIdo Schimmel
139bb24d592SIdo Schimmel	ip link add name ${rp3}_bond type bond mode 802.3ad
140bb24d592SIdo Schimmel	ip link set dev $rp3 master ${rp3}_bond
141bb24d592SIdo Schimmel	__addr_add_del ${rp3}_bond add 192.0.2.18/28
142bb24d592SIdo Schimmel	tc qdisc add dev $rp3 clsact
143bb24d592SIdo Schimmel	ip link set dev ${rp3}_bond up
144bb24d592SIdo Schimmel
145bb24d592SIdo Schimmel	ip link add name ${rp4}_bond type bond mode 802.3ad
146bb24d592SIdo Schimmel	ip link set dev $rp4 master ${rp4}_bond
147bb24d592SIdo Schimmel	__addr_add_del ${rp4}_bond add 198.51.100.18/28
148bb24d592SIdo Schimmel	tc qdisc add dev $rp4 clsact
149bb24d592SIdo Schimmel	ip link set dev ${rp4}_bond up
150bb24d592SIdo Schimmel}
151bb24d592SIdo Schimmel
152bb24d592SIdo Schimmelrouter_destroy()
153bb24d592SIdo Schimmel{
154bb24d592SIdo Schimmel	ip link set dev ${rp4}_bond down
155bb24d592SIdo Schimmel	tc qdisc del dev $rp4 clsact
156bb24d592SIdo Schimmel	__addr_add_del ${rp4}_bond del 198.51.100.18/28
157bb24d592SIdo Schimmel	ip link set dev $rp4 nomaster
158bb24d592SIdo Schimmel	ip link del dev ${rp4}_bond
159bb24d592SIdo Schimmel
160bb24d592SIdo Schimmel	ip link set dev ${rp3}_bond down
161bb24d592SIdo Schimmel	tc qdisc del dev $rp3 clsact
162bb24d592SIdo Schimmel	__addr_add_del ${rp3}_bond del 192.0.2.18/28
163bb24d592SIdo Schimmel	ip link set dev $rp3 nomaster
164bb24d592SIdo Schimmel	ip link del dev ${rp3}_bond
165bb24d592SIdo Schimmel
166bb24d592SIdo Schimmel	tc qdisc del dev $rp2 clsact
167bb24d592SIdo Schimmel	__addr_add_del $rp2 del 198.51.100.2/28
168bb24d592SIdo Schimmel	ip link set dev $rp2 down
169bb24d592SIdo Schimmel
170bb24d592SIdo Schimmel	tc qdisc del dev $rp1 clsact
171bb24d592SIdo Schimmel	__addr_add_del $rp1 del 192.0.2.2/28
172bb24d592SIdo Schimmel	ip link set dev $rp1 down
173bb24d592SIdo Schimmel}
174bb24d592SIdo Schimmel
175bb24d592SIdo Schimmelsetup_prepare()
176bb24d592SIdo Schimmel{
177bb24d592SIdo Schimmel	h1=${NETIFS[p1]}
178bb24d592SIdo Schimmel	rp1=${NETIFS[p2]}
179bb24d592SIdo Schimmel	rp2=${NETIFS[p3]}
180bb24d592SIdo Schimmel	h2=${NETIFS[p4]}
181bb24d592SIdo Schimmel	h3=${NETIFS[p5]}
182bb24d592SIdo Schimmel	rp3=${NETIFS[p6]}
183bb24d592SIdo Schimmel	h4=${NETIFS[p7]}
184bb24d592SIdo Schimmel	rp4=${NETIFS[p8]}
185bb24d592SIdo Schimmel
186bb24d592SIdo Schimmel	vrf_prepare
187bb24d592SIdo Schimmel
188bb24d592SIdo Schimmel	h1_create
189bb24d592SIdo Schimmel	h2_create
190bb24d592SIdo Schimmel	h3_create
191bb24d592SIdo Schimmel	h4_create
192bb24d592SIdo Schimmel	router_create
193bb24d592SIdo Schimmel}
194bb24d592SIdo Schimmel
195bb24d592SIdo Schimmelcleanup()
196bb24d592SIdo Schimmel{
197bb24d592SIdo Schimmel	pre_cleanup
198bb24d592SIdo Schimmel
199bb24d592SIdo Schimmel	rm -f $CAPTURE_FILE
200bb24d592SIdo Schimmel
201bb24d592SIdo Schimmel	router_destroy
202bb24d592SIdo Schimmel	h4_destroy
203bb24d592SIdo Schimmel	h3_destroy
204bb24d592SIdo Schimmel	h2_destroy
205bb24d592SIdo Schimmel	h1_destroy
206bb24d592SIdo Schimmel
207bb24d592SIdo Schimmel	vrf_cleanup
208bb24d592SIdo Schimmel}
209bb24d592SIdo Schimmel
210bb24d592SIdo Schimmelpsample_capture_start()
211bb24d592SIdo Schimmel{
212bb24d592SIdo Schimmel	rm -f $CAPTURE_FILE
213bb24d592SIdo Schimmel
214bb24d592SIdo Schimmel	psample &> $CAPTURE_FILE &
215bb24d592SIdo Schimmel
216bb24d592SIdo Schimmel	sleep 1
217bb24d592SIdo Schimmel}
218bb24d592SIdo Schimmel
219bb24d592SIdo Schimmelpsample_capture_stop()
220bb24d592SIdo Schimmel{
221bb24d592SIdo Schimmel	{ kill %% && wait %%; } 2>/dev/null
222bb24d592SIdo Schimmel}
223bb24d592SIdo Schimmel
224bb24d592SIdo Schimmel__tc_sample_rate_test()
225bb24d592SIdo Schimmel{
226bb24d592SIdo Schimmel	local desc=$1; shift
227bb24d592SIdo Schimmel	local dip=$1; shift
228bb24d592SIdo Schimmel	local pkts pct
229bb24d592SIdo Schimmel
230bb24d592SIdo Schimmel	RET=0
231bb24d592SIdo Schimmel
232bb24d592SIdo Schimmel	tc filter add dev $rp1 ingress protocol all pref 1 handle 101 matchall \
233bb24d592SIdo Schimmel		skip_sw action sample rate 32 group 1
234bb24d592SIdo Schimmel	check_err $? "Failed to configure sampling rule"
235bb24d592SIdo Schimmel
236bb24d592SIdo Schimmel	psample_capture_start
237bb24d592SIdo Schimmel
23816355c0bSIdo Schimmel	ip vrf exec v$h1 $MZ $h1 -c 320000 -d 100usec -p 64 -A 192.0.2.1 \
239bb24d592SIdo Schimmel		-B $dip -t udp dp=52768,sp=42768 -q
240bb24d592SIdo Schimmel
241bb24d592SIdo Schimmel	psample_capture_stop
242bb24d592SIdo Schimmel
243bb24d592SIdo Schimmel	pkts=$(grep -e "group 1 " $CAPTURE_FILE | wc -l)
24416355c0bSIdo Schimmel	pct=$((100 * (pkts - 10000) / 10000))
245bb24d592SIdo Schimmel	(( -25 <= pct && pct <= 25))
24616355c0bSIdo Schimmel	check_err $? "Expected 10000 packets, got $pkts packets, which is $pct% off. Required accuracy is +-25%"
247bb24d592SIdo Schimmel
248bb24d592SIdo Schimmel	log_test "tc sample rate ($desc)"
249bb24d592SIdo Schimmel
250bb24d592SIdo Schimmel	tc filter del dev $rp1 ingress protocol all pref 1 handle 101 matchall
251bb24d592SIdo Schimmel}
252bb24d592SIdo Schimmel
253bb24d592SIdo Schimmeltc_sample_rate_test()
254bb24d592SIdo Schimmel{
255bb24d592SIdo Schimmel	__tc_sample_rate_test "forward" 198.51.100.1
256bb24d592SIdo Schimmel	__tc_sample_rate_test "local receive" 192.0.2.2
257bb24d592SIdo Schimmel}
258bb24d592SIdo Schimmel
259bb24d592SIdo Schimmeltc_sample_max_rate_test()
260bb24d592SIdo Schimmel{
261bb24d592SIdo Schimmel	RET=0
262bb24d592SIdo Schimmel
263bb24d592SIdo Schimmel	tc filter add dev $rp1 ingress protocol all pref 1 handle 101 matchall \
264bb24d592SIdo Schimmel		skip_sw action sample rate $((35 * 10 ** 8)) group 1
265bb24d592SIdo Schimmel	check_err $? "Failed to configure sampling rule with max rate"
266bb24d592SIdo Schimmel
267bb24d592SIdo Schimmel	tc filter del dev $rp1 ingress protocol all pref 1 handle 101 matchall
268bb24d592SIdo Schimmel
269bb24d592SIdo Schimmel	tc filter add dev $rp1 ingress protocol all pref 1 handle 101 matchall \
270bb24d592SIdo Schimmel		skip_sw action sample rate $((35 * 10 ** 8 + 1)) \
271bb24d592SIdo Schimmel		group 1 &> /dev/null
272bb24d592SIdo Schimmel	check_fail $? "Managed to configure sampling rate above maximum"
273bb24d592SIdo Schimmel
274bb24d592SIdo Schimmel	log_test "tc sample maximum rate"
275bb24d592SIdo Schimmel}
276bb24d592SIdo Schimmel
2777ede22e6SIdo Schimmeltc_sample_conflict_test()
2787ede22e6SIdo Schimmel{
2797ede22e6SIdo Schimmel	RET=0
2807ede22e6SIdo Schimmel
2817ede22e6SIdo Schimmel	# Test that two sampling rules cannot be configured on the same port,
2827ede22e6SIdo Schimmel	# even when they share the same parameters.
2837ede22e6SIdo Schimmel
2847ede22e6SIdo Schimmel	tc filter add dev $rp1 ingress protocol all pref 1 handle 101 matchall \
2857ede22e6SIdo Schimmel		skip_sw action sample rate 1024 group 1
2867ede22e6SIdo Schimmel	check_err $? "Failed to configure sampling rule"
2877ede22e6SIdo Schimmel
2887ede22e6SIdo Schimmel	tc filter add dev $rp1 ingress protocol all pref 2 handle 102 matchall \
2897ede22e6SIdo Schimmel		skip_sw action sample rate 1024 group 1 &> /dev/null
2907ede22e6SIdo Schimmel	check_fail $? "Managed to configure second sampling rule"
2917ede22e6SIdo Schimmel
2927ede22e6SIdo Schimmel	# Delete the first rule and make sure the second rule can now be
2937ede22e6SIdo Schimmel	# configured.
2947ede22e6SIdo Schimmel
2957ede22e6SIdo Schimmel	tc filter del dev $rp1 ingress protocol all pref 1 handle 101 matchall
2967ede22e6SIdo Schimmel
2977ede22e6SIdo Schimmel	tc filter add dev $rp1 ingress protocol all pref 2 handle 102 matchall \
2987ede22e6SIdo Schimmel		skip_sw action sample rate 1024 group 1
2997ede22e6SIdo Schimmel	check_err $? "Failed to configure sampling rule after deletion"
3007ede22e6SIdo Schimmel
3017ede22e6SIdo Schimmel	log_test "tc sample conflict test"
3027ede22e6SIdo Schimmel
3037ede22e6SIdo Schimmel	tc filter del dev $rp1 ingress protocol all pref 2 handle 102 matchall
3047ede22e6SIdo Schimmel}
3057ede22e6SIdo Schimmel
306bb24d592SIdo Schimmeltc_sample_group_conflict_test()
307bb24d592SIdo Schimmel{
308bb24d592SIdo Schimmel	RET=0
309bb24d592SIdo Schimmel
310bb24d592SIdo Schimmel	# Test that two sampling rules cannot be configured on the same port
311bb24d592SIdo Schimmel	# with different groups.
312bb24d592SIdo Schimmel
313bb24d592SIdo Schimmel	tc filter add dev $rp1 ingress protocol all pref 1 handle 101 matchall \
314bb24d592SIdo Schimmel		skip_sw action sample rate 1024 group 1
315bb24d592SIdo Schimmel	check_err $? "Failed to configure sampling rule"
316bb24d592SIdo Schimmel
317bb24d592SIdo Schimmel	tc filter add dev $rp1 ingress protocol all pref 2 handle 102 matchall \
318bb24d592SIdo Schimmel		skip_sw action sample rate 1024 group 2 &> /dev/null
319bb24d592SIdo Schimmel	check_fail $? "Managed to configure sampling rule with conflicting group"
320bb24d592SIdo Schimmel
321bb24d592SIdo Schimmel	log_test "tc sample group conflict test"
322bb24d592SIdo Schimmel
323bb24d592SIdo Schimmel	tc filter del dev $rp1 ingress protocol all pref 1 handle 101 matchall
324bb24d592SIdo Schimmel}
325bb24d592SIdo Schimmel
326bb24d592SIdo Schimmeltc_sample_md_iif_test()
327bb24d592SIdo Schimmel{
328bb24d592SIdo Schimmel	local rp1_ifindex
329bb24d592SIdo Schimmel
330bb24d592SIdo Schimmel	RET=0
331bb24d592SIdo Schimmel
332bb24d592SIdo Schimmel	tc filter add dev $rp1 ingress protocol all pref 1 handle 101 matchall \
333bb24d592SIdo Schimmel		skip_sw action sample rate 5 group 1
334bb24d592SIdo Schimmel	check_err $? "Failed to configure sampling rule"
335bb24d592SIdo Schimmel
336bb24d592SIdo Schimmel	psample_capture_start
337bb24d592SIdo Schimmel
338bb24d592SIdo Schimmel	ip vrf exec v$h1 $MZ $h1 -c 3200 -d 1msec -p 64 -A 192.0.2.1 \
339bb24d592SIdo Schimmel		-B 198.51.100.1 -t udp dp=52768,sp=42768 -q
340bb24d592SIdo Schimmel
341bb24d592SIdo Schimmel	psample_capture_stop
342bb24d592SIdo Schimmel
343bb24d592SIdo Schimmel	rp1_ifindex=$(ip -j -p link show dev $rp1 | jq '.[]["ifindex"]')
344bb24d592SIdo Schimmel	grep -q -e "in-ifindex $rp1_ifindex " $CAPTURE_FILE
345bb24d592SIdo Schimmel	check_err $? "Sampled packets do not have expected in-ifindex"
346bb24d592SIdo Schimmel
347bb24d592SIdo Schimmel	log_test "tc sample iif"
348bb24d592SIdo Schimmel
349bb24d592SIdo Schimmel	tc filter del dev $rp1 ingress protocol all pref 1 handle 101 matchall
350bb24d592SIdo Schimmel}
351bb24d592SIdo Schimmel
352bb24d592SIdo Schimmeltc_sample_md_lag_iif_test()
353bb24d592SIdo Schimmel{
354bb24d592SIdo Schimmel	local rp3_ifindex
355bb24d592SIdo Schimmel
356bb24d592SIdo Schimmel	RET=0
357bb24d592SIdo Schimmel
358bb24d592SIdo Schimmel	tc filter add dev $rp3 ingress protocol all pref 1 handle 101 matchall \
359bb24d592SIdo Schimmel		skip_sw action sample rate 5 group 1
360bb24d592SIdo Schimmel	check_err $? "Failed to configure sampling rule"
361bb24d592SIdo Schimmel
362bb24d592SIdo Schimmel	psample_capture_start
363bb24d592SIdo Schimmel
364bb24d592SIdo Schimmel	ip vrf exec v${h3}_bond $MZ ${h3}_bond -c 3200 -d 1msec -p 64 \
365bb24d592SIdo Schimmel		-A 192.0.2.17 -B 198.51.100.17 -t udp dp=52768,sp=42768 -q
366bb24d592SIdo Schimmel
367bb24d592SIdo Schimmel	psample_capture_stop
368bb24d592SIdo Schimmel
369bb24d592SIdo Schimmel	rp3_ifindex=$(ip -j -p link show dev $rp3 | jq '.[]["ifindex"]')
370bb24d592SIdo Schimmel	grep -q -e "in-ifindex $rp3_ifindex " $CAPTURE_FILE
371bb24d592SIdo Schimmel	check_err $? "Sampled packets do not have expected in-ifindex"
372bb24d592SIdo Schimmel
373bb24d592SIdo Schimmel	log_test "tc sample lag iif"
374bb24d592SIdo Schimmel
375bb24d592SIdo Schimmel	tc filter del dev $rp3 ingress protocol all pref 1 handle 101 matchall
376bb24d592SIdo Schimmel}
377bb24d592SIdo Schimmel
378bb24d592SIdo Schimmeltc_sample_md_oif_test()
379bb24d592SIdo Schimmel{
380bb24d592SIdo Schimmel	local rp2_ifindex
381bb24d592SIdo Schimmel
382bb24d592SIdo Schimmel	RET=0
383bb24d592SIdo Schimmel
384bb24d592SIdo Schimmel	tc filter add dev $rp1 ingress protocol all pref 1 handle 101 matchall \
385bb24d592SIdo Schimmel		skip_sw action sample rate 5 group 1
386bb24d592SIdo Schimmel	check_err $? "Failed to configure sampling rule"
387bb24d592SIdo Schimmel
388bb24d592SIdo Schimmel	psample_capture_start
389bb24d592SIdo Schimmel
390bb24d592SIdo Schimmel	ip vrf exec v$h1 $MZ $h1 -c 3200 -d 1msec -p 64 -A 192.0.2.1 \
391bb24d592SIdo Schimmel		-B 198.51.100.1 -t udp dp=52768,sp=42768 -q
392bb24d592SIdo Schimmel
393bb24d592SIdo Schimmel	psample_capture_stop
394bb24d592SIdo Schimmel
395bb24d592SIdo Schimmel	rp2_ifindex=$(ip -j -p link show dev $rp2 | jq '.[]["ifindex"]')
396bb24d592SIdo Schimmel	grep -q -e "out-ifindex $rp2_ifindex " $CAPTURE_FILE
397bb24d592SIdo Schimmel	check_err $? "Sampled packets do not have expected out-ifindex"
398bb24d592SIdo Schimmel
399bb24d592SIdo Schimmel	log_test "tc sample oif"
400bb24d592SIdo Schimmel
401bb24d592SIdo Schimmel	tc filter del dev $rp1 ingress protocol all pref 1 handle 101 matchall
402bb24d592SIdo Schimmel}
403bb24d592SIdo Schimmel
404bb24d592SIdo Schimmeltc_sample_md_lag_oif_test()
405bb24d592SIdo Schimmel{
406bb24d592SIdo Schimmel	local rp4_ifindex
407bb24d592SIdo Schimmel
408bb24d592SIdo Schimmel	RET=0
409bb24d592SIdo Schimmel
410bb24d592SIdo Schimmel	tc filter add dev $rp3 ingress protocol all pref 1 handle 101 matchall \
411bb24d592SIdo Schimmel		skip_sw action sample rate 5 group 1
412bb24d592SIdo Schimmel	check_err $? "Failed to configure sampling rule"
413bb24d592SIdo Schimmel
414bb24d592SIdo Schimmel	psample_capture_start
415bb24d592SIdo Schimmel
416bb24d592SIdo Schimmel	ip vrf exec v${h3}_bond $MZ ${h3}_bond -c 3200 -d 1msec -p 64 \
417bb24d592SIdo Schimmel		-A 192.0.2.17 -B 198.51.100.17 -t udp dp=52768,sp=42768 -q
418bb24d592SIdo Schimmel
419bb24d592SIdo Schimmel	psample_capture_stop
420bb24d592SIdo Schimmel
421bb24d592SIdo Schimmel	rp4_ifindex=$(ip -j -p link show dev $rp4 | jq '.[]["ifindex"]')
422bb24d592SIdo Schimmel	grep -q -e "out-ifindex $rp4_ifindex " $CAPTURE_FILE
423bb24d592SIdo Schimmel	check_err $? "Sampled packets do not have expected out-ifindex"
424bb24d592SIdo Schimmel
425bb24d592SIdo Schimmel	log_test "tc sample lag oif"
426bb24d592SIdo Schimmel
427bb24d592SIdo Schimmel	tc filter del dev $rp3 ingress protocol all pref 1 handle 101 matchall
428bb24d592SIdo Schimmel}
429bb24d592SIdo Schimmel
430bb24d592SIdo Schimmeltc_sample_md_out_tc_test()
431bb24d592SIdo Schimmel{
432bb24d592SIdo Schimmel	RET=0
433bb24d592SIdo Schimmel
434bb24d592SIdo Schimmel	# Output traffic class is not supported on Spectrum-1.
435*b8bfafe4SPetr Machata	mlxsw_only_on_spectrum 2+ || return
436bb24d592SIdo Schimmel
437bb24d592SIdo Schimmel	tc filter add dev $rp1 ingress protocol all pref 1 handle 101 matchall \
438bb24d592SIdo Schimmel		skip_sw action sample rate 5 group 1
439bb24d592SIdo Schimmel	check_err $? "Failed to configure sampling rule"
440bb24d592SIdo Schimmel
441bb24d592SIdo Schimmel	# By default, all the packets should go to the same traffic class (0).
442bb24d592SIdo Schimmel
443bb24d592SIdo Schimmel	psample_capture_start
444bb24d592SIdo Schimmel
445bb24d592SIdo Schimmel	ip vrf exec v$h1 $MZ $h1 -c 3200 -d 1msec -p 64 -A 192.0.2.1 \
446bb24d592SIdo Schimmel		-B 198.51.100.1 -t udp dp=52768,sp=42768 -q
447bb24d592SIdo Schimmel
448bb24d592SIdo Schimmel	psample_capture_stop
449bb24d592SIdo Schimmel
450bb24d592SIdo Schimmel	grep -q -e "out-tc 0 " $CAPTURE_FILE
451bb24d592SIdo Schimmel	check_err $? "Sampled packets do not have expected out-tc (0)"
452bb24d592SIdo Schimmel
453bb24d592SIdo Schimmel	# Map all priorities to highest traffic class (7) and check reported
454bb24d592SIdo Schimmel	# out-tc.
455bb24d592SIdo Schimmel	tc qdisc replace dev $rp2 root handle 1: \
456bb24d592SIdo Schimmel		prio bands 3 priomap 0 0 0 0 0 0 0 0
457bb24d592SIdo Schimmel
458bb24d592SIdo Schimmel	psample_capture_start
459bb24d592SIdo Schimmel
460bb24d592SIdo Schimmel	ip vrf exec v$h1 $MZ $h1 -c 3200 -d 1msec -p 64 -A 192.0.2.1 \
461bb24d592SIdo Schimmel		-B 198.51.100.1 -t udp dp=52768,sp=42768 -q
462bb24d592SIdo Schimmel
463bb24d592SIdo Schimmel	psample_capture_stop
464bb24d592SIdo Schimmel
465bb24d592SIdo Schimmel	grep -q -e "out-tc 7 " $CAPTURE_FILE
466bb24d592SIdo Schimmel	check_err $? "Sampled packets do not have expected out-tc (7)"
467bb24d592SIdo Schimmel
468bb24d592SIdo Schimmel	log_test "tc sample out-tc"
469bb24d592SIdo Schimmel
470bb24d592SIdo Schimmel	tc qdisc del dev $rp2 root handle 1:
471bb24d592SIdo Schimmel	tc filter del dev $rp1 ingress protocol all pref 1 handle 101 matchall
472bb24d592SIdo Schimmel}
473bb24d592SIdo Schimmel
474bb24d592SIdo Schimmeltc_sample_md_out_tc_occ_test()
475bb24d592SIdo Schimmel{
476bb24d592SIdo Schimmel	local backlog pct occ
477bb24d592SIdo Schimmel
478bb24d592SIdo Schimmel	RET=0
479bb24d592SIdo Schimmel
480bb24d592SIdo Schimmel	# Output traffic class occupancy is not supported on Spectrum-1.
481*b8bfafe4SPetr Machata	mlxsw_only_on_spectrum 2+ || return
482bb24d592SIdo Schimmel
483bb24d592SIdo Schimmel	tc filter add dev $rp1 ingress protocol all pref 1 handle 101 matchall \
484bb24d592SIdo Schimmel		skip_sw action sample rate 1024 group 1
485bb24d592SIdo Schimmel	check_err $? "Failed to configure sampling rule"
486bb24d592SIdo Schimmel
487bb24d592SIdo Schimmel	# Configure a shaper on egress to create congestion.
488bb24d592SIdo Schimmel	tc qdisc replace dev $rp2 root handle 1: \
489bb24d592SIdo Schimmel		tbf rate 1Mbit burst 256k limit 1M
490bb24d592SIdo Schimmel
491bb24d592SIdo Schimmel	psample_capture_start
492bb24d592SIdo Schimmel
493bb24d592SIdo Schimmel	ip vrf exec v$h1 $MZ $h1 -c 0 -d 1usec -p 1400 -A 192.0.2.1 \
494bb24d592SIdo Schimmel		-B 198.51.100.1 -t udp dp=52768,sp=42768 -q &
495bb24d592SIdo Schimmel
496bb24d592SIdo Schimmel	# Allow congestion to reach steady state.
497bb24d592SIdo Schimmel	sleep 10
498bb24d592SIdo Schimmel
499bb24d592SIdo Schimmel	backlog=$(tc -j -p -s qdisc show dev $rp2 | jq '.[0]["backlog"]')
500bb24d592SIdo Schimmel
501bb24d592SIdo Schimmel	# Kill mausezahn.
502bb24d592SIdo Schimmel	{ kill %% && wait %%; } 2>/dev/null
503bb24d592SIdo Schimmel
504bb24d592SIdo Schimmel	psample_capture_stop
505bb24d592SIdo Schimmel
506bb24d592SIdo Schimmel	# Record last congestion sample.
507bb24d592SIdo Schimmel	occ=$(grep -e "out-tc-occ " $CAPTURE_FILE | tail -n 1 | \
508bb24d592SIdo Schimmel		cut -d ' ' -f 16)
509bb24d592SIdo Schimmel
510bb24d592SIdo Schimmel	pct=$((100 * (occ - backlog) / backlog))
511bb24d592SIdo Schimmel	(( -1 <= pct && pct <= 1))
512bb24d592SIdo Schimmel	check_err $? "Recorded a congestion of $backlog bytes, but sampled congestion is $occ bytes, which is $pct% off. Required accuracy is +-5%"
513bb24d592SIdo Schimmel
514bb24d592SIdo Schimmel	log_test "tc sample out-tc-occ"
515bb24d592SIdo Schimmel
516bb24d592SIdo Schimmel	tc qdisc del dev $rp2 root handle 1:
517bb24d592SIdo Schimmel	tc filter del dev $rp1 ingress protocol all pref 1 handle 101 matchall
518bb24d592SIdo Schimmel}
519bb24d592SIdo Schimmel
520f0b692c4SIdo Schimmeltc_sample_md_latency_test()
521f0b692c4SIdo Schimmel{
522f0b692c4SIdo Schimmel	RET=0
523f0b692c4SIdo Schimmel
524f0b692c4SIdo Schimmel	# Egress sampling not supported on Spectrum-1.
525*b8bfafe4SPetr Machata	mlxsw_only_on_spectrum 2+ || return
526f0b692c4SIdo Schimmel
527f0b692c4SIdo Schimmel	tc filter add dev $rp2 egress protocol all pref 1 handle 101 matchall \
528f0b692c4SIdo Schimmel		skip_sw action sample rate 5 group 1
529f0b692c4SIdo Schimmel	check_err $? "Failed to configure sampling rule"
530f0b692c4SIdo Schimmel
531f0b692c4SIdo Schimmel	psample_capture_start
532f0b692c4SIdo Schimmel
533f0b692c4SIdo Schimmel	ip vrf exec v$h1 $MZ $h1 -c 3200 -d 1msec -p 64 -A 192.0.2.1 \
534f0b692c4SIdo Schimmel		-B 198.51.100.1 -t udp dp=52768,sp=42768 -q
535f0b692c4SIdo Schimmel
536f0b692c4SIdo Schimmel	psample_capture_stop
537f0b692c4SIdo Schimmel
538f0b692c4SIdo Schimmel	grep -q -e "latency " $CAPTURE_FILE
539f0b692c4SIdo Schimmel	check_err $? "Sampled packets do not have latency attribute"
540f0b692c4SIdo Schimmel
541f0b692c4SIdo Schimmel	log_test "tc sample latency"
542f0b692c4SIdo Schimmel
543f0b692c4SIdo Schimmel	tc filter del dev $rp2 egress protocol all pref 1 handle 101 matchall
544f0b692c4SIdo Schimmel}
545f0b692c4SIdo Schimmel
546f0b692c4SIdo Schimmeltc_sample_acl_group_conflict_test()
547f0b692c4SIdo Schimmel{
548f0b692c4SIdo Schimmel	RET=0
549f0b692c4SIdo Schimmel
550f0b692c4SIdo Schimmel	# Test that two flower sampling rules cannot be configured on the same
551f0b692c4SIdo Schimmel	# port with different groups.
552f0b692c4SIdo Schimmel
553f0b692c4SIdo Schimmel	# Policy-based sampling is not supported on Spectrum-1.
554*b8bfafe4SPetr Machata	mlxsw_only_on_spectrum 2+ || return
555f0b692c4SIdo Schimmel
556f0b692c4SIdo Schimmel	tc filter add dev $rp1 ingress protocol ip pref 1 handle 101 flower \
557f0b692c4SIdo Schimmel		skip_sw action sample rate 1024 group 1
558f0b692c4SIdo Schimmel	check_err $? "Failed to configure sampling rule"
559f0b692c4SIdo Schimmel
560f0b692c4SIdo Schimmel	tc filter add dev $rp1 ingress protocol ip pref 2 handle 102 flower \
561f0b692c4SIdo Schimmel		skip_sw action sample rate 1024 group 1
562f0b692c4SIdo Schimmel	check_err $? "Failed to configure sampling rule with same group"
563f0b692c4SIdo Schimmel
564f0b692c4SIdo Schimmel	tc filter add dev $rp1 ingress protocol ip pref 3 handle 103 flower \
565f0b692c4SIdo Schimmel		skip_sw action sample rate 1024 group 2 &> /dev/null
566f0b692c4SIdo Schimmel	check_fail $? "Managed to configure sampling rule with conflicting group"
567f0b692c4SIdo Schimmel
568f0b692c4SIdo Schimmel	log_test "tc sample (w/ flower) group conflict test"
569f0b692c4SIdo Schimmel
570f0b692c4SIdo Schimmel	tc filter del dev $rp1 ingress protocol ip pref 2 handle 102 flower
571f0b692c4SIdo Schimmel	tc filter del dev $rp1 ingress protocol ip pref 1 handle 101 flower
572f0b692c4SIdo Schimmel}
573f0b692c4SIdo Schimmel
574f0b692c4SIdo Schimmel__tc_sample_acl_rate_test()
575f0b692c4SIdo Schimmel{
576f0b692c4SIdo Schimmel	local bind=$1; shift
577f0b692c4SIdo Schimmel	local port=$1; shift
578f0b692c4SIdo Schimmel	local pkts pct
579f0b692c4SIdo Schimmel
580f0b692c4SIdo Schimmel	RET=0
581f0b692c4SIdo Schimmel
582f0b692c4SIdo Schimmel	# Policy-based sampling is not supported on Spectrum-1.
583*b8bfafe4SPetr Machata	mlxsw_only_on_spectrum 2+ || return
584f0b692c4SIdo Schimmel
585f0b692c4SIdo Schimmel	tc filter add dev $port $bind protocol ip pref 1 handle 101 flower \
586f0b692c4SIdo Schimmel		skip_sw dst_ip 198.51.100.1 action sample rate 32 group 1
587f0b692c4SIdo Schimmel	check_err $? "Failed to configure sampling rule"
588f0b692c4SIdo Schimmel
589f0b692c4SIdo Schimmel	psample_capture_start
590f0b692c4SIdo Schimmel
59116355c0bSIdo Schimmel	ip vrf exec v$h1 $MZ $h1 -c 320000 -d 100usec -p 64 -A 192.0.2.1 \
592f0b692c4SIdo Schimmel		-B 198.51.100.1 -t udp dp=52768,sp=42768 -q
593f0b692c4SIdo Schimmel
594f0b692c4SIdo Schimmel	psample_capture_stop
595f0b692c4SIdo Schimmel
596f0b692c4SIdo Schimmel	pkts=$(grep -e "group 1 " $CAPTURE_FILE | wc -l)
59716355c0bSIdo Schimmel	pct=$((100 * (pkts - 10000) / 10000))
598f0b692c4SIdo Schimmel	(( -25 <= pct && pct <= 25))
59916355c0bSIdo Schimmel	check_err $? "Expected 10000 packets, got $pkts packets, which is $pct% off. Required accuracy is +-25%"
600f0b692c4SIdo Schimmel
601f0b692c4SIdo Schimmel	# Setup a filter that should not match any packet and make sure packets
602f0b692c4SIdo Schimmel	# are not sampled.
603f0b692c4SIdo Schimmel	tc filter del dev $port $bind protocol ip pref 1 handle 101 flower
604f0b692c4SIdo Schimmel
605f0b692c4SIdo Schimmel	tc filter add dev $port $bind protocol ip pref 1 handle 101 flower \
606f0b692c4SIdo Schimmel		skip_sw dst_ip 198.51.100.10 action sample rate 32 group 1
607f0b692c4SIdo Schimmel	check_err $? "Failed to configure sampling rule"
608f0b692c4SIdo Schimmel
609f0b692c4SIdo Schimmel	psample_capture_start
610f0b692c4SIdo Schimmel
611f0b692c4SIdo Schimmel	ip vrf exec v$h1 $MZ $h1 -c 3200 -d 1msec -p 64 -A 192.0.2.1 \
612f0b692c4SIdo Schimmel		-B 198.51.100.1 -t udp dp=52768,sp=42768 -q
613f0b692c4SIdo Schimmel
614f0b692c4SIdo Schimmel	psample_capture_stop
615f0b692c4SIdo Schimmel
616f0b692c4SIdo Schimmel	grep -q -e "group 1 " $CAPTURE_FILE
617f0b692c4SIdo Schimmel	check_fail $? "Sampled packets when should not"
618f0b692c4SIdo Schimmel
619f0b692c4SIdo Schimmel	log_test "tc sample (w/ flower) rate ($bind)"
620f0b692c4SIdo Schimmel
621f0b692c4SIdo Schimmel	tc filter del dev $port $bind protocol ip pref 1 handle 101 flower
622f0b692c4SIdo Schimmel}
623f0b692c4SIdo Schimmel
624f0b692c4SIdo Schimmeltc_sample_acl_rate_test()
625f0b692c4SIdo Schimmel{
626f0b692c4SIdo Schimmel	__tc_sample_acl_rate_test ingress $rp1
627f0b692c4SIdo Schimmel	__tc_sample_acl_rate_test egress $rp2
628f0b692c4SIdo Schimmel}
629f0b692c4SIdo Schimmel
630f0b692c4SIdo Schimmeltc_sample_acl_max_rate_test()
631f0b692c4SIdo Schimmel{
632f0b692c4SIdo Schimmel	RET=0
633f0b692c4SIdo Schimmel
634f0b692c4SIdo Schimmel	# Policy-based sampling is not supported on Spectrum-1.
635*b8bfafe4SPetr Machata	mlxsw_only_on_spectrum 2+ || return
636f0b692c4SIdo Schimmel
637f0b692c4SIdo Schimmel	tc filter add dev $rp1 ingress protocol ip pref 1 handle 101 flower \
638f0b692c4SIdo Schimmel		skip_sw action sample rate $((2 ** 24 - 1)) group 1
639f0b692c4SIdo Schimmel	check_err $? "Failed to configure sampling rule with max rate"
640f0b692c4SIdo Schimmel
641f0b692c4SIdo Schimmel	tc filter del dev $rp1 ingress protocol ip pref 1 handle 101 flower
642f0b692c4SIdo Schimmel
643f0b692c4SIdo Schimmel	tc filter add dev $rp1 ingress protocol ip pref 1 handle 101 flower \
644f0b692c4SIdo Schimmel		skip_sw action sample rate $((2 ** 24)) \
645f0b692c4SIdo Schimmel		group 1 &> /dev/null
646f0b692c4SIdo Schimmel	check_fail $? "Managed to configure sampling rate above maximum"
647f0b692c4SIdo Schimmel
648f0b692c4SIdo Schimmel	log_test "tc sample (w/ flower) maximum rate"
649f0b692c4SIdo Schimmel}
650f0b692c4SIdo Schimmel
651bb24d592SIdo Schimmeltrap cleanup EXIT
652bb24d592SIdo Schimmel
653bb24d592SIdo Schimmelsetup_prepare
654bb24d592SIdo Schimmelsetup_wait
655bb24d592SIdo Schimmel
656bb24d592SIdo Schimmeltests_run
657bb24d592SIdo Schimmel
658bb24d592SIdo Schimmelexit $EXIT_STATUS
659