1# SPDX-License-Identifier: GPL-2.0
2
3nh_stats_do_test()
4{
5	local what=$1; shift
6	local nh1_id=$1; shift
7	local nh2_id=$1; shift
8	local group_id=$1; shift
9	local stats_get=$1; shift
10	local mz="$@"
11
12	local dp
13
14	RET=0
15
16	sleep 2
17	for ((dp=0; dp < 60000; dp += 10000)); do
18		local dd
19		local t0_rp12=$(link_stats_tx_packets_get $rp12)
20		local t0_rp13=$(link_stats_tx_packets_get $rp13)
21		local t0_nh1=$($stats_get $group_id $nh1_id)
22		local t0_nh2=$($stats_get $group_id $nh2_id)
23
24		ip vrf exec vrf-h1 \
25			$mz -q -p 64 -d 0 -t udp \
26				"sp=1024,dp=$((dp))-$((dp + 10000))"
27		sleep 2
28
29		local t1_rp12=$(link_stats_tx_packets_get $rp12)
30		local t1_rp13=$(link_stats_tx_packets_get $rp13)
31		local t1_nh1=$($stats_get $group_id $nh1_id)
32		local t1_nh2=$($stats_get $group_id $nh2_id)
33
34		local d_rp12=$((t1_rp12 - t0_rp12))
35		local d_rp13=$((t1_rp13 - t0_rp13))
36		local d_nh1=$((t1_nh1 - t0_nh1))
37		local d_nh2=$((t1_nh2 - t0_nh2))
38
39		dd=$(absval $((d_rp12 - d_nh1)))
40		((dd < 10))
41		check_err $? "Discrepancy between link and $stats_get: d_rp12=$d_rp12 d_nh1=$d_nh1"
42
43		dd=$(absval $((d_rp13 - d_nh2)))
44		((dd < 10))
45		check_err $? "Discrepancy between link and $stats_get: d_rp13=$d_rp13 d_nh2=$d_nh2"
46	done
47
48	log_test "NH stats test $what"
49}
50
51nh_stats_test_dispatch_swhw()
52{
53	local what=$1; shift
54	local nh1_id=$1; shift
55	local nh2_id=$1; shift
56	local group_id=$1; shift
57	local mz="$@"
58
59	nh_stats_do_test "$what" "$nh1_id" "$nh2_id" "$group_id" \
60			 nh_stats_get "${mz[@]}"
61
62	xfail_on_veth $rp11 \
63		nh_stats_do_test "HW $what" "$nh1_id" "$nh2_id" "$group_id" \
64				 nh_stats_get_hw "${mz[@]}"
65}
66
67nh_stats_test_dispatch()
68{
69	local nhgtype=$1; shift
70	local what=$1; shift
71	local nh1_id=$1; shift
72	local nh2_id=$1; shift
73	local group_id=$1; shift
74	local mz="$@"
75
76	local enabled
77
78	if ! ip nexthop help 2>&1 | grep -q hw_stats; then
79		log_test_skip "NH stats test: ip doesn't support HW stats"
80		return
81	fi
82
83	ip nexthop replace id $group_id group $nh1_id/$nh2_id \
84			   hw_stats on type $nhgtype
85	enabled=$(ip -s -j -d nexthop show id $group_id |
86		      jq '.[].hw_stats.enabled')
87	if [[ $enabled == true ]]; then
88		nh_stats_test_dispatch_swhw "$what" "$nh1_id" "$nh2_id" \
89					    "$group_id" "${mz[@]}"
90	elif [[ $enabled == false ]]; then
91		check_err 1 "HW stats still disabled after enabling"
92		log_test "NH stats test"
93	else
94		log_test_skip "NH stats test: ip doesn't report hw_stats info"
95	fi
96
97	ip nexthop replace id $group_id group $nh1_id/$nh2_id \
98			   hw_stats off type $nhgtype
99}
100
101__nh_stats_test_v4()
102{
103	local nhgtype=$1; shift
104
105	sysctl_set net.ipv4.fib_multipath_hash_policy 1
106	nh_stats_test_dispatch $nhgtype "IPv4" 101 102 103 \
107			       $MZ $h1 -A 192.0.2.2 -B 198.51.100.2
108	sysctl_restore net.ipv4.fib_multipath_hash_policy
109}
110
111__nh_stats_test_v6()
112{
113	local nhgtype=$1; shift
114
115	sysctl_set net.ipv6.fib_multipath_hash_policy 1
116	nh_stats_test_dispatch $nhgtype "IPv6" 104 105 106 \
117			       $MZ -6 $h1 -A 2001:db8:1::2 -B 2001:db8:2::2
118	sysctl_restore net.ipv6.fib_multipath_hash_policy
119}
120
121check_nhgw16()
122{
123	local nhid=$1; shift
124
125	ip nexthop replace id 9999 group "$nhid,65535" &>/dev/null
126	if (( $? )); then
127		log_test_skip "16-bit multipath tests" \
128			      "iproute2 or the kernel do not support 16-bit next hop weights"
129		return 1
130	fi
131	ip nexthop del id 9999 ||:
132}
133