xref: /linux/tools/testing/selftests/drivers/net/hw/ethtool_rmon.sh (revision 48ba00da2eb4b54a7e6ed2ca3a9f2e575dff48c9)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4ALL_TESTS="
5	rmon_rx_histogram
6	rmon_tx_histogram
7"
8
9NUM_NETIFS=2
10lib_dir=$(dirname "$0")
11source "$lib_dir"/../../../net/forwarding/lib.sh
12
13ETH_FCS_LEN=4
14ETH_HLEN=$((6+6+2))
15
16declare -A netif_mtu
17
18ensure_mtu()
19{
20	local iface=$1; shift
21	local len=$1; shift
22	local current=$(ip -j link show dev $iface | jq -r '.[0].mtu')
23	local required=$((len - ETH_HLEN - ETH_FCS_LEN))
24
25	if [ $current -lt $required ]; then
26		ip link set dev $iface mtu $required || return 1
27	fi
28}
29
30bucket_test()
31{
32	local iface=$1; shift
33	local neigh=$1; shift
34	local set=$1; shift
35	local bucket=$1; shift
36	local len=$1; shift
37	local num_rx=10000
38	local num_tx=20000
39	local expected=
40	local before=
41	local after=
42	local delta=
43
44	# Mausezahn does not include FCS bytes in its length - but the
45	# histogram counters do
46	len=$((len - ETH_FCS_LEN))
47
48	before=$(ethtool --json -S $iface --groups rmon | \
49		jq -r ".[0].rmon[\"${set}-pktsNtoM\"][$bucket].val")
50
51	# Send 10k one way and 20k in the other, to detect counters
52	# mapped to the wrong direction
53	$MZ $neigh -q -c $num_rx -p $len -a own -b bcast -d 10us
54	$MZ $iface -q -c $num_tx -p $len -a own -b bcast -d 10us
55
56	after=$(ethtool --json -S $iface --groups rmon | \
57		jq -r ".[0].rmon[\"${set}-pktsNtoM\"][$bucket].val")
58
59	delta=$((after - before))
60
61	expected=$([ $set = rx ] && echo $num_rx || echo $num_tx)
62
63	# Allow some extra tolerance for other packets sent by the stack
64	[ $delta -ge $expected ] && [ $delta -le $((expected + 100)) ]
65}
66
67rmon_histogram()
68{
69	local iface=$1; shift
70	local neigh=$1; shift
71	local set=$1; shift
72	local nbuckets=0
73	local step=
74
75	RET=0
76
77	while read -r -a bucket; do
78		step="$set-pkts${bucket[0]}to${bucket[1]} on $iface"
79
80		for if in $iface $neigh; do
81			if ! ensure_mtu $if ${bucket[0]}; then
82				log_test_xfail "$if does not support the required MTU for $step"
83				return
84			fi
85		done
86
87		if ! bucket_test $iface $neigh $set $nbuckets ${bucket[0]}; then
88			check_err 1 "$step failed"
89			return 1
90		fi
91		log_test "$step"
92		nbuckets=$((nbuckets + 1))
93	done < <(ethtool --json -S $iface --groups rmon | \
94		jq -r ".[0].rmon[\"${set}-pktsNtoM\"][]|[.low, .high]|@tsv" 2>/dev/null)
95
96	if [ $nbuckets -eq 0 ]; then
97		log_test_xfail "$iface does not support $set histogram counters"
98		return
99	fi
100}
101
102rmon_rx_histogram()
103{
104	rmon_histogram $h1 $h2 rx
105	rmon_histogram $h2 $h1 rx
106}
107
108rmon_tx_histogram()
109{
110	rmon_histogram $h1 $h2 tx
111	rmon_histogram $h2 $h1 tx
112}
113
114setup_prepare()
115{
116	h1=${NETIFS[p1]}
117	h2=${NETIFS[p2]}
118
119	for iface in $h1 $h2; do
120		netif_mtu[$iface]=$(ip -j link show dev $iface | jq -r '.[0].mtu')
121		ip link set dev $iface up
122	done
123}
124
125cleanup()
126{
127	pre_cleanup
128
129	for iface in $h2 $h1; do
130		ip link set dev $iface \
131			mtu ${netif_mtu[$iface]} \
132			down
133	done
134}
135
136check_ethtool_counter_group_support
137trap cleanup EXIT
138
139setup_prepare
140setup_wait
141
142tests_run
143
144exit $EXIT_STATUS
145