1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4speeds_arr_get()
5{
6	cmd='/ETHTOOL_LINK_MODE_[^[:space:]]*_BIT[[:space:]]+=[[:space:]]+/ \
7		{sub(/,$/, "") \
8		sub(/ETHTOOL_LINK_MODE_/,"") \
9		sub(/_BIT/,"") \
10		sub(/_Full/,"/Full") \
11		sub(/_Half/,"/Half");\
12		print "["$1"]="$3}'
13
14	awk "${cmd}" /usr/include/linux/ethtool.h
15}
16
17ethtool_set()
18{
19	local cmd="$@"
20	local out=$(ethtool -s $cmd 2>&1 | wc -l)
21
22	check_err $out "error in configuration. $cmd"
23}
24
25dev_linkmodes_params_get()
26{
27	local dev=$1; shift
28	local adver=$1; shift
29	local -a linkmodes_params
30	local param_count
31	local arr
32
33	if (($adver)); then
34		mode="Advertised link modes"
35	else
36		mode="Supported link modes"
37	fi
38
39	local -a dev_linkmodes=($(dev_speeds_get $dev 1 $adver))
40	for ((i=0; i<${#dev_linkmodes[@]}; i++)); do
41		linkmodes_params[$i]=$(echo -e "${dev_linkmodes[$i]}" | \
42			# Replaces all non numbers with spaces
43			sed -e 's/[^0-9]/ /g' | \
44			# Squeeze spaces in sequence to 1 space
45			tr -s ' ')
46		# Count how many numbers were found in the linkmode
47		param_count=$(echo "${linkmodes_params[$i]}" | wc -w)
48		if [[ $param_count -eq 1 ]]; then
49			linkmodes_params[$i]="${linkmodes_params[$i]} 1"
50		elif [[ $param_count -ge 3 ]]; then
51			arr=(${linkmodes_params[$i]})
52			# Take only first two params
53			linkmodes_params[$i]=$(echo "${arr[@]:0:2}")
54		fi
55	done
56	echo ${linkmodes_params[@]}
57}
58
59dev_speeds_get()
60{
61	local dev=$1; shift
62	local with_mode=$1; shift
63	local adver=$1; shift
64	local speeds_str
65
66	if (($adver)); then
67		mode="Advertised link modes"
68	else
69		mode="Supported link modes"
70	fi
71
72	speeds_str=$(ethtool "$dev" | \
73		# Snip everything before the link modes section.
74		sed -n '/'"$mode"':/,$p' | \
75		# Quit processing the rest at the start of the next section.
76		# When checking, skip the header of this section (hence the 2,).
77		sed -n '2,${/^[\t][^ \t]/q};p' | \
78		# Drop the section header of the current section.
79		cut -d':' -f2)
80
81	local -a speeds_arr=($speeds_str)
82	if [[ $with_mode -eq 0 ]]; then
83		for ((i=0; i<${#speeds_arr[@]}; i++)); do
84			speeds_arr[$i]=${speeds_arr[$i]%base*}
85		done
86	fi
87	echo ${speeds_arr[@]}
88}
89
90common_speeds_get()
91{
92	dev1=$1; shift
93	dev2=$1; shift
94	with_mode=$1; shift
95	adver=$1; shift
96
97	local -a dev1_speeds=($(dev_speeds_get $dev1 $with_mode $adver))
98	local -a dev2_speeds=($(dev_speeds_get $dev2 $with_mode $adver))
99
100	comm -12 \
101		<(printf '%s\n' "${dev1_speeds[@]}" | sort -u) \
102		<(printf '%s\n' "${dev2_speeds[@]}" | sort -u)
103}
104
105different_speeds_get()
106{
107	local dev1=$1; shift
108	local dev2=$1; shift
109	local with_mode=$1; shift
110	local adver=$1; shift
111
112	local -a speeds_arr
113
114	speeds_arr=($(common_speeds_get $dev1 $dev2 $with_mode $adver))
115	if [[ ${#speeds_arr[@]} < 2 ]]; then
116		check_err 1 "cannot check different speeds. There are not enough speeds"
117	fi
118
119	echo ${speeds_arr[0]} ${speeds_arr[1]}
120}
121