1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# +--------------------+                     +----------------------+
5# | H1                 |                     |                   H2 |
6# |                    |                     |                      |
7# |          $h1.200 + |                     | + $h2.200            |
8# |     192.0.2.1/28 | |                     | | 192.0.2.18/28      |
9# | 2001:db8:1::1/64 | |                     | | 2001:db8:2::1/64   |
10# |                  | |                     | |                    |
11# |              $h1 + |                     | + $h2                |
12# |                  | |                     | |                    |
13# +------------------|-+                     +-|--------------------+
14#                    |                         |
15# +------------------|-------------------------|--------------------+
16# | SW               |                         |                    |
17# |                  |                         |                    |
18# |             $rp1 +                         + $rp2               |
19# |                  |                         |                    |
20# |         $rp1.200 +                         + $rp2.200           |
21# |     192.0.2.2/28                             192.0.2.17/28      |
22# | 2001:db8:1::2/64                             2001:db8:2::2/64   |
23# |                                                                 |
24# +-----------------------------------------------------------------+
25
26ALL_TESTS="
27	ping_ipv4
28	ping_ipv6
29	test_stats_rx_ipv4
30	test_stats_tx_ipv4
31	test_stats_rx_ipv6
32	test_stats_tx_ipv6
33	respin_enablement
34	test_stats_rx_ipv4
35	test_stats_tx_ipv4
36	test_stats_rx_ipv6
37	test_stats_tx_ipv6
38	reapply_config
39	ping_ipv4
40	ping_ipv6
41	test_stats_rx_ipv4
42	test_stats_tx_ipv4
43	test_stats_rx_ipv6
44	test_stats_tx_ipv6
45	test_stats_report_rx
46	test_stats_report_tx
47	test_destroy_enabled
48	test_double_enable
49"
50NUM_NETIFS=4
51lib_dir=$(dirname "$0")
52source "$lib_dir"/../../../net/forwarding/lib.sh
53source "$lib_dir"/../../../net/forwarding/tc_common.sh
54
55h1_create()
56{
57	simple_if_init $h1
58	vlan_create $h1 200 v$h1 192.0.2.1/28 2001:db8:1::1/64
59	ip route add 192.0.2.16/28 vrf v$h1 nexthop via 192.0.2.2
60	ip -6 route add 2001:db8:2::/64 vrf v$h1 nexthop via 2001:db8:1::2
61}
62
63h1_destroy()
64{
65	ip -6 route del 2001:db8:2::/64 vrf v$h1 nexthop via 2001:db8:1::2
66	ip route del 192.0.2.16/28 vrf v$h1 nexthop via 192.0.2.2
67	vlan_destroy $h1 200
68	simple_if_fini $h1
69}
70
71h2_create()
72{
73	simple_if_init $h2
74	vlan_create $h2 200 v$h2 192.0.2.18/28 2001:db8:2::1/64
75	ip route add 192.0.2.0/28 vrf v$h2 nexthop via 192.0.2.17
76	ip -6 route add 2001:db8:1::/64 vrf v$h2 nexthop via 2001:db8:2::2
77}
78
79h2_destroy()
80{
81	ip -6 route del 2001:db8:1::/64 vrf v$h2 nexthop via 2001:db8:2::2
82	ip route del 192.0.2.0/28 vrf v$h2 nexthop via 192.0.2.17
83	vlan_destroy $h2 200
84	simple_if_fini $h2
85}
86
87router_rp1_200_create()
88{
89	ip link add name $rp1.200 link $rp1 type vlan id 200
90	ip link set dev $rp1.200 addrgenmode eui64
91	ip link set dev $rp1.200 up
92	ip address add dev $rp1.200 192.0.2.2/28
93	ip address add dev $rp1.200 2001:db8:1::2/64
94	ip stats set dev $rp1.200 l3_stats on
95}
96
97router_rp1_200_destroy()
98{
99	ip stats set dev $rp1.200 l3_stats off
100	ip address del dev $rp1.200 2001:db8:1::2/64
101	ip address del dev $rp1.200 192.0.2.2/28
102	ip link del dev $rp1.200
103}
104
105router_create()
106{
107	ip link set dev $rp1 up
108	router_rp1_200_create
109
110	ip link set dev $rp2 up
111	vlan_create $rp2 200 "" 192.0.2.17/28 2001:db8:2::2/64
112}
113
114router_destroy()
115{
116	vlan_destroy $rp2 200
117	ip link set dev $rp2 down
118
119	router_rp1_200_destroy
120	ip link set dev $rp1 down
121}
122
123setup_prepare()
124{
125	h1=${NETIFS[p1]}
126	rp1=${NETIFS[p2]}
127
128	rp2=${NETIFS[p3]}
129	h2=${NETIFS[p4]}
130
131	rp1mac=$(mac_get $rp1)
132	rp2mac=$(mac_get $rp2)
133
134	vrf_prepare
135
136	h1_create
137	h2_create
138
139	router_create
140
141	forwarding_enable
142}
143
144cleanup()
145{
146	pre_cleanup
147
148	forwarding_restore
149
150	router_destroy
151
152	h2_destroy
153	h1_destroy
154
155	vrf_cleanup
156}
157
158ping_ipv4()
159{
160	ping_test $h1.200 192.0.2.18 " IPv4"
161}
162
163ping_ipv6()
164{
165	ping_test $h1.200 2001:db8:2::1 " IPv6"
166}
167
168send_packets_rx_ipv4()
169{
170	# Send 21 packets instead of 20, because the first one might trap and go
171	# through the SW datapath, which might not bump the HW counter.
172	$MZ $h1.200 -c 21 -d 20msec -p 100 \
173	    -a own -b $rp1mac -A 192.0.2.1 -B 192.0.2.18 \
174	    -q -t udp sp=54321,dp=12345
175}
176
177send_packets_rx_ipv6()
178{
179	$MZ $h1.200 -6 -c 21 -d 20msec -p 100 \
180	    -a own -b $rp1mac -A 2001:db8:1::1 -B 2001:db8:2::1 \
181	    -q -t udp sp=54321,dp=12345
182}
183
184send_packets_tx_ipv4()
185{
186	$MZ $h2.200 -c 21 -d 20msec -p 100 \
187	    -a own -b $rp2mac -A 192.0.2.18 -B 192.0.2.1 \
188	    -q -t udp sp=54321,dp=12345
189}
190
191send_packets_tx_ipv6()
192{
193	$MZ $h2.200 -6 -c 21 -d 20msec -p 100 \
194	    -a own -b $rp2mac -A 2001:db8:2::1 -B 2001:db8:1::1 \
195	    -q -t udp sp=54321,dp=12345
196}
197
198___test_stats()
199{
200	local dir=$1; shift
201	local prot=$1; shift
202
203	local a
204	local b
205
206	a=$(hw_stats_get l3_stats $rp1.200 ${dir} packets)
207	send_packets_${dir}_${prot}
208	"$@"
209	b=$(busywait "$TC_HIT_TIMEOUT" until_counter_is ">= $a + 20" \
210		       hw_stats_get l3_stats $rp1.200 ${dir} packets)
211	check_err $? "Traffic not reflected in the counter: $a -> $b"
212}
213
214__test_stats()
215{
216	local dir=$1; shift
217	local prot=$1; shift
218
219	RET=0
220	___test_stats "$dir" "$prot"
221	log_test "Test $dir packets: $prot"
222}
223
224test_stats_rx_ipv4()
225{
226	__test_stats rx ipv4
227}
228
229test_stats_tx_ipv4()
230{
231	__test_stats tx ipv4
232}
233
234test_stats_rx_ipv6()
235{
236	__test_stats rx ipv6
237}
238
239test_stats_tx_ipv6()
240{
241	__test_stats tx ipv6
242}
243
244# Make sure everything works well even after stats have been disabled and
245# reenabled on the same device without touching the L3 configuration.
246respin_enablement()
247{
248	log_info "Turning stats off and on again"
249	ip stats set dev $rp1.200 l3_stats off
250	ip stats set dev $rp1.200 l3_stats on
251}
252
253# For the initial run, l3_stats is enabled on a completely set up netdevice. Now
254# do it the other way around: enabling the L3 stats on an L2 netdevice, and only
255# then apply the L3 configuration.
256reapply_config()
257{
258	log_info "Reapplying configuration"
259
260	router_rp1_200_destroy
261
262	ip link add name $rp1.200 link $rp1 type vlan id 200
263	ip link set dev $rp1.200 addrgenmode none
264	ip stats set dev $rp1.200 l3_stats on
265	ip link set dev $rp1.200 addrgenmode eui64
266	ip link set dev $rp1.200 up
267	ip address add dev $rp1.200 192.0.2.2/28
268	ip address add dev $rp1.200 2001:db8:1::2/64
269}
270
271__test_stats_report()
272{
273	local dir=$1; shift
274	local prot=$1; shift
275
276	local a
277	local b
278
279	RET=0
280
281	a=$(hw_stats_get l3_stats $rp1.200 ${dir} packets)
282	send_packets_${dir}_${prot}
283	ip address flush dev $rp1.200
284	b=$(busywait "$TC_HIT_TIMEOUT" until_counter_is ">= $a + 20" \
285		       hw_stats_get l3_stats $rp1.200 ${dir} packets)
286	check_err $? "Traffic not reflected in the counter: $a -> $b"
287	log_test "Test ${dir} packets: stats pushed on loss of L3"
288
289	ip stats set dev $rp1.200 l3_stats off
290	ip link del dev $rp1.200
291	router_rp1_200_create
292}
293
294test_stats_report_rx()
295{
296	__test_stats_report rx ipv4
297}
298
299test_stats_report_tx()
300{
301	__test_stats_report tx ipv4
302}
303
304test_destroy_enabled()
305{
306	RET=0
307
308	ip link del dev $rp1.200
309	router_rp1_200_create
310
311	log_test "Destroy l3_stats-enabled netdev"
312}
313
314test_double_enable()
315{
316	RET=0
317	___test_stats rx ipv4 \
318		ip stats set dev $rp1.200 l3_stats on
319	log_test "Test stat retention across a spurious enablement"
320}
321
322trap cleanup EXIT
323
324setup_prepare
325setup_wait
326
327used=$(ip -j stats show dev $rp1.200 group offload subgroup hw_stats_info |
328	   jq '.[].info.l3_stats.used')
329[[ $used = true ]]
330check_err $? "hw_stats_info.used=$used"
331log_test "l3_stats offloaded"
332tests_run
333
334exit $EXIT_STATUS
335