aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tools/testing/selftests/net/forwarding/router_mpath_nh_lib.sh
blob: 7e7d62161c345a45fde2c05131ab0d64d9f18075 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# SPDX-License-Identifier: GPL-2.0

nh_stats_do_test()
{
	local what=$1; shift
	local nh1_id=$1; shift
	local nh2_id=$1; shift
	local group_id=$1; shift
	local stats_get=$1; shift
	local mz="$@"

	local dp

	RET=0

	sleep 2
	for ((dp=0; dp < 60000; dp += 10000)); do
		local dd
		local t0_rp12=$(link_stats_tx_packets_get $rp12)
		local t0_rp13=$(link_stats_tx_packets_get $rp13)
		local t0_nh1=$($stats_get $group_id $nh1_id)
		local t0_nh2=$($stats_get $group_id $nh2_id)

		ip vrf exec vrf-h1 \
			$mz -q -p 64 -d 0 -t udp \
				"sp=1024,dp=$((dp))-$((dp + 10000))"
		sleep 2

		local t1_rp12=$(link_stats_tx_packets_get $rp12)
		local t1_rp13=$(link_stats_tx_packets_get $rp13)
		local t1_nh1=$($stats_get $group_id $nh1_id)
		local t1_nh2=$($stats_get $group_id $nh2_id)

		local d_rp12=$((t1_rp12 - t0_rp12))
		local d_rp13=$((t1_rp13 - t0_rp13))
		local d_nh1=$((t1_nh1 - t0_nh1))
		local d_nh2=$((t1_nh2 - t0_nh2))

		dd=$(absval $((d_rp12 - d_nh1)))
		((dd < 10))
		check_err $? "Discrepancy between link and $stats_get: d_rp12=$d_rp12 d_nh1=$d_nh1"

		dd=$(absval $((d_rp13 - d_nh2)))
		((dd < 10))
		check_err $? "Discrepancy between link and $stats_get: d_rp13=$d_rp13 d_nh2=$d_nh2"
	done

	log_test "NH stats test $what"
}

nh_stats_test_dispatch_swhw()
{
	local what=$1; shift
	local nh1_id=$1; shift
	local nh2_id=$1; shift
	local group_id=$1; shift
	local mz="$@"

	local used

	nh_stats_do_test "$what" "$nh1_id" "$nh2_id" "$group_id" \
			 nh_stats_get "${mz[@]}"

	used=$(ip -s -j -d nexthop show id $group_id |
		   jq '.[].hw_stats.used')
	kind=$(ip -j -d link show dev $rp11 |
		   jq -r '.[].linkinfo.info_kind')
	if [[ $used == true ]]; then
		nh_stats_do_test "HW $what" "$nh1_id" "$nh2_id" "$group_id" \
				 nh_stats_get_hw "${mz[@]}"
	elif [[ $kind == veth ]]; then
		log_test_skip "HW stats not offloaded on veth topology"
	fi
}

nh_stats_test_dispatch()
{
	local nhgtype=$1; shift
	local what=$1; shift
	local nh1_id=$1; shift
	local nh2_id=$1; shift
	local group_id=$1; shift
	local mz="$@"

	local enabled
	local kind

	if ! ip nexthop help 2>&1 | grep -q hw_stats; then
		log_test_skip "NH stats test: ip doesn't support HW stats"
		return
	fi

	ip nexthop replace id $group_id group $nh1_id/$nh2_id \
			   hw_stats on type $nhgtype
	enabled=$(ip -s -j -d nexthop show id $group_id |
		      jq '.[].hw_stats.enabled')
	if [[ $enabled == true ]]; then
		nh_stats_test_dispatch_swhw "$what" "$nh1_id" "$nh2_id" \
					    "$group_id" "${mz[@]}"
	elif [[ $enabled == false ]]; then
		check_err 1 "HW stats still disabled after enabling"
		log_test "NH stats test"
	else
		log_test_skip "NH stats test: ip doesn't report hw_stats info"
	fi

	ip nexthop replace id $group_id group $nh1_id/$nh2_id \
			   hw_stats off type $nhgtype
}

__nh_stats_test_v4()
{
	local nhgtype=$1; shift

	sysctl_set net.ipv4.fib_multipath_hash_policy 1
	nh_stats_test_dispatch $nhgtype "IPv4" 101 102 103 \
			       $MZ $h1 -A 192.0.2.2 -B 198.51.100.2
	sysctl_restore net.ipv4.fib_multipath_hash_policy
}

__nh_stats_test_v6()
{
	local nhgtype=$1; shift

	sysctl_set net.ipv6.fib_multipath_hash_policy 1
	nh_stats_test_dispatch $nhgtype "IPv6" 104 105 106 \
			       $MZ -6 $h1 -A 2001:db8:1::2 -B 2001:db8:2::2
	sysctl_restore net.ipv6.fib_multipath_hash_policy
}