1# SPDX-License-Identifier: GPL-2.0
2
3RIF_COUNTER_NUM_NETIFS=2
4
5rif_counter_addr4()
6{
7	local i=$1; shift
8	local p=$1; shift
9
10	printf 192.0.%d.%d $((i / 64)) $(((4 * i % 256) + p))
11}
12
13rif_counter_addr4pfx()
14{
15	rif_counter_addr4 $@
16	printf /30
17}
18
19rif_counter_h1_create()
20{
21	simple_if_init $h1
22}
23
24rif_counter_h1_destroy()
25{
26	simple_if_fini $h1
27}
28
29rif_counter_h2_create()
30{
31	simple_if_init $h2
32}
33
34rif_counter_h2_destroy()
35{
36	simple_if_fini $h2
37}
38
39rif_counter_setup_prepare()
40{
41	h1=${NETIFS[p1]}
42	h2=${NETIFS[p2]}
43
44	vrf_prepare
45
46	rif_counter_h1_create
47	rif_counter_h2_create
48}
49
50rif_counter_cleanup()
51{
52	local count=$1; shift
53
54	pre_cleanup
55
56	for ((i = 1; i <= count; i++)); do
57		vlan_destroy $h2 $i
58	done
59
60	rif_counter_h2_destroy
61	rif_counter_h1_destroy
62
63	vrf_cleanup
64
65	if [[ -v RIF_COUNTER_BATCH_FILE ]]; then
66		rm -f $RIF_COUNTER_BATCH_FILE
67	fi
68}
69
70
71rif_counter_test()
72{
73	local count=$1; shift
74	local should_fail=$1; shift
75
76	RIF_COUNTER_BATCH_FILE="$(mktemp)"
77
78	for ((i = 1; i <= count; i++)); do
79		vlan_create $h2 $i v$h2 $(rif_counter_addr4pfx $i 2)
80	done
81	for ((i = 1; i <= count; i++)); do
82		cat >> $RIF_COUNTER_BATCH_FILE <<-EOF
83			stats set dev $h2.$i l3_stats on
84		EOF
85	done
86
87	ip -b $RIF_COUNTER_BATCH_FILE
88	check_err_fail $should_fail $? "RIF counter enablement"
89}
90
91rif_counter_traffic_test()
92{
93	local count=$1; shift
94	local i;
95
96	for ((i = count; i > 0; i /= 2)); do
97		$MZ $h1 -Q $i -c 1 -d 20msec -p 100 -a own -b $(mac_get $h2) \
98		    -A $(rif_counter_addr4 $i 1) \
99		    -B $(rif_counter_addr4 $i 2) \
100		    -q -t udp sp=54321,dp=12345
101	done
102	for ((i = count; i > 0; i /= 2)); do
103		busywait "$TC_HIT_TIMEOUT" until_counter_is "== 1" \
104			 hw_stats_get l3_stats $h2.$i rx packets > /dev/null
105		check_err $? "Traffic not seen at RIF $h2.$i"
106	done
107}
108