xref: /openbmc/linux/tools/testing/selftests/net/netdevice.sh (revision 3eb66e91a25497065c5322b1268cbc3953642227)
1af0e5461SLABBE Corentin#!/bin/sh
2b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0
3af0e5461SLABBE Corentin#
4af0e5461SLABBE Corentin# This test is for checking network interface
5af0e5461SLABBE Corentin# For the moment it tests only ethernet interface (but wifi could be easily added)
6af0e5461SLABBE Corentin#
7af0e5461SLABBE Corentin# We assume that all network driver are loaded
8af0e5461SLABBE Corentin# if not they probably have failed earlier in the boot process and their logged error will be catched by another test
9af0e5461SLABBE Corentin#
10af0e5461SLABBE Corentin
11*57aefc7cSShuah Khan (Samsung OSG)# Kselftest framework requirement - SKIP code is 4.
12*57aefc7cSShuah Khan (Samsung OSG)ksft_skip=4
13*57aefc7cSShuah Khan (Samsung OSG)
14af0e5461SLABBE Corentin# this function will try to up the interface
15af0e5461SLABBE Corentin# if already up, nothing done
16af0e5461SLABBE Corentin# arg1: network interface name
17af0e5461SLABBE Corentinkci_net_start()
18af0e5461SLABBE Corentin{
19af0e5461SLABBE Corentin	netdev=$1
20af0e5461SLABBE Corentin
21af0e5461SLABBE Corentin	ip link show "$netdev" |grep -q UP
22af0e5461SLABBE Corentin	if [ $? -eq 0 ];then
23af0e5461SLABBE Corentin		echo "SKIP: $netdev: interface already up"
24*57aefc7cSShuah Khan (Samsung OSG)		return $ksft_skip
25af0e5461SLABBE Corentin	fi
26af0e5461SLABBE Corentin
27af0e5461SLABBE Corentin	ip link set "$netdev" up
28af0e5461SLABBE Corentin	if [ $? -ne 0 ];then
29af0e5461SLABBE Corentin		echo "FAIL: $netdev: Fail to up interface"
30af0e5461SLABBE Corentin		return 1
31af0e5461SLABBE Corentin	else
32af0e5461SLABBE Corentin		echo "PASS: $netdev: set interface up"
33af0e5461SLABBE Corentin		NETDEV_STARTED=1
34af0e5461SLABBE Corentin	fi
35af0e5461SLABBE Corentin	return 0
36af0e5461SLABBE Corentin}
37af0e5461SLABBE Corentin
38af0e5461SLABBE Corentin# this function will try to setup an IP and MAC address on a network interface
39af0e5461SLABBE Corentin# Doing nothing if the interface was already up
40af0e5461SLABBE Corentin# arg1: network interface name
41af0e5461SLABBE Corentinkci_net_setup()
42af0e5461SLABBE Corentin{
43af0e5461SLABBE Corentin	netdev=$1
44af0e5461SLABBE Corentin
45af0e5461SLABBE Corentin	# do nothing if the interface was already up
46af0e5461SLABBE Corentin	if [ $NETDEV_STARTED -eq 0 ];then
47af0e5461SLABBE Corentin		return 0
48af0e5461SLABBE Corentin	fi
49af0e5461SLABBE Corentin
50af0e5461SLABBE Corentin	MACADDR='02:03:04:05:06:07'
51af0e5461SLABBE Corentin	ip link set dev $netdev address "$MACADDR"
52af0e5461SLABBE Corentin	if [ $? -ne 0 ];then
53af0e5461SLABBE Corentin		echo "FAIL: $netdev: Cannot set MAC address"
54af0e5461SLABBE Corentin	else
55af0e5461SLABBE Corentin		ip link show $netdev |grep -q "$MACADDR"
56af0e5461SLABBE Corentin		if [ $? -eq 0 ];then
57af0e5461SLABBE Corentin			echo "PASS: $netdev: set MAC address"
58af0e5461SLABBE Corentin		else
59af0e5461SLABBE Corentin			echo "FAIL: $netdev: Cannot set MAC address"
60af0e5461SLABBE Corentin		fi
61af0e5461SLABBE Corentin	fi
62af0e5461SLABBE Corentin
63af0e5461SLABBE Corentin	#check that the interface did not already have an IP
64af0e5461SLABBE Corentin	ip address show "$netdev" |grep '^[[:space:]]*inet'
65af0e5461SLABBE Corentin	if [ $? -eq 0 ];then
66af0e5461SLABBE Corentin		echo "SKIP: $netdev: already have an IP"
67*57aefc7cSShuah Khan (Samsung OSG)		return $ksft_skip
68af0e5461SLABBE Corentin	fi
69af0e5461SLABBE Corentin
70af0e5461SLABBE Corentin	# TODO what ipaddr to set ? DHCP ?
71af0e5461SLABBE Corentin	echo "SKIP: $netdev: set IP address"
72*57aefc7cSShuah Khan (Samsung OSG)	return $ksft_skip
73af0e5461SLABBE Corentin}
74af0e5461SLABBE Corentin
75af0e5461SLABBE Corentin# test an ethtool command
76af0e5461SLABBE Corentin# arg1: return code for not supported (see ethtool code source)
77af0e5461SLABBE Corentin# arg2: summary of the command
78af0e5461SLABBE Corentin# arg3: command to execute
79af0e5461SLABBE Corentinkci_netdev_ethtool_test()
80af0e5461SLABBE Corentin{
81af0e5461SLABBE Corentin	if [ $# -le 2 ];then
82af0e5461SLABBE Corentin		echo "SKIP: $netdev: ethtool: invalid number of arguments"
83af0e5461SLABBE Corentin		return 1
84af0e5461SLABBE Corentin	fi
85af0e5461SLABBE Corentin	$3 >/dev/null
86af0e5461SLABBE Corentin	ret=$?
87af0e5461SLABBE Corentin	if [ $ret -ne 0 ];then
88af0e5461SLABBE Corentin		if [ $ret -eq "$1" ];then
89af0e5461SLABBE Corentin			echo "SKIP: $netdev: ethtool $2 not supported"
90*57aefc7cSShuah Khan (Samsung OSG)			return $ksft_skip
91af0e5461SLABBE Corentin		else
92af0e5461SLABBE Corentin			echo "FAIL: $netdev: ethtool $2"
93af0e5461SLABBE Corentin			return 1
94af0e5461SLABBE Corentin		fi
95af0e5461SLABBE Corentin	else
96af0e5461SLABBE Corentin		echo "PASS: $netdev: ethtool $2"
97af0e5461SLABBE Corentin	fi
98af0e5461SLABBE Corentin	return 0
99af0e5461SLABBE Corentin}
100af0e5461SLABBE Corentin
101af0e5461SLABBE Corentin# test ethtool commands
102af0e5461SLABBE Corentin# arg1: network interface name
103af0e5461SLABBE Corentinkci_netdev_ethtool()
104af0e5461SLABBE Corentin{
105af0e5461SLABBE Corentin	netdev=$1
106af0e5461SLABBE Corentin
107af0e5461SLABBE Corentin	#check presence of ethtool
108af0e5461SLABBE Corentin	ethtool --version 2>/dev/null >/dev/null
109af0e5461SLABBE Corentin	if [ $? -ne 0 ];then
110af0e5461SLABBE Corentin		echo "SKIP: ethtool not present"
111*57aefc7cSShuah Khan (Samsung OSG)		return $ksft_skip
112af0e5461SLABBE Corentin	fi
113af0e5461SLABBE Corentin
114af0e5461SLABBE Corentin	TMP_ETHTOOL_FEATURES="$(mktemp)"
115af0e5461SLABBE Corentin	if [ ! -e "$TMP_ETHTOOL_FEATURES" ];then
116af0e5461SLABBE Corentin		echo "SKIP: Cannot create a tmp file"
117af0e5461SLABBE Corentin		return 1
118af0e5461SLABBE Corentin	fi
119af0e5461SLABBE Corentin
120af0e5461SLABBE Corentin	ethtool -k "$netdev" > "$TMP_ETHTOOL_FEATURES"
121af0e5461SLABBE Corentin	if [ $? -ne 0 ];then
122af0e5461SLABBE Corentin		echo "FAIL: $netdev: ethtool list features"
123af0e5461SLABBE Corentin		rm "$TMP_ETHTOOL_FEATURES"
124af0e5461SLABBE Corentin		return 1
125af0e5461SLABBE Corentin	fi
126af0e5461SLABBE Corentin	echo "PASS: $netdev: ethtool list features"
127af0e5461SLABBE Corentin	#TODO for each non fixed features, try to turn them on/off
128af0e5461SLABBE Corentin	rm "$TMP_ETHTOOL_FEATURES"
129af0e5461SLABBE Corentin
130af0e5461SLABBE Corentin	kci_netdev_ethtool_test 74 'dump' "ethtool -d $netdev"
131af0e5461SLABBE Corentin	kci_netdev_ethtool_test 94 'stats' "ethtool -S $netdev"
132af0e5461SLABBE Corentin	return 0
133af0e5461SLABBE Corentin}
134af0e5461SLABBE Corentin
135af0e5461SLABBE Corentin# stop a netdev
136af0e5461SLABBE Corentin# arg1: network interface name
137af0e5461SLABBE Corentinkci_netdev_stop()
138af0e5461SLABBE Corentin{
139af0e5461SLABBE Corentin	netdev=$1
140af0e5461SLABBE Corentin
141af0e5461SLABBE Corentin	if [ $NETDEV_STARTED -eq 0 ];then
142af0e5461SLABBE Corentin		echo "SKIP: $netdev: interface kept up"
143af0e5461SLABBE Corentin		return 0
144af0e5461SLABBE Corentin	fi
145af0e5461SLABBE Corentin
146af0e5461SLABBE Corentin	ip link set "$netdev" down
147af0e5461SLABBE Corentin	if [ $? -ne 0 ];then
148af0e5461SLABBE Corentin		echo "FAIL: $netdev: stop interface"
149af0e5461SLABBE Corentin		return 1
150af0e5461SLABBE Corentin	fi
151af0e5461SLABBE Corentin	echo "PASS: $netdev: stop interface"
152af0e5461SLABBE Corentin	return 0
153af0e5461SLABBE Corentin}
154af0e5461SLABBE Corentin
155af0e5461SLABBE Corentin# run all test on a netdev
156af0e5461SLABBE Corentin# arg1: network interface name
157af0e5461SLABBE Corentinkci_test_netdev()
158af0e5461SLABBE Corentin{
159af0e5461SLABBE Corentin	NETDEV_STARTED=0
160af0e5461SLABBE Corentin	IFACE_TO_UPDOWN="$1"
161af0e5461SLABBE Corentin	IFACE_TO_TEST="$1"
162af0e5461SLABBE Corentin	#check for VLAN interface
163af0e5461SLABBE Corentin	MASTER_IFACE="$(echo $1 | cut -d@ -f2)"
164af0e5461SLABBE Corentin	if [ ! -z "$MASTER_IFACE" ];then
165af0e5461SLABBE Corentin		IFACE_TO_UPDOWN="$MASTER_IFACE"
166af0e5461SLABBE Corentin		IFACE_TO_TEST="$(echo $1 | cut -d@ -f1)"
167af0e5461SLABBE Corentin	fi
168af0e5461SLABBE Corentin
169af0e5461SLABBE Corentin	NETDEV_STARTED=0
170af0e5461SLABBE Corentin	kci_net_start "$IFACE_TO_UPDOWN"
171af0e5461SLABBE Corentin
172af0e5461SLABBE Corentin	kci_net_setup "$IFACE_TO_TEST"
173af0e5461SLABBE Corentin
174af0e5461SLABBE Corentin	kci_netdev_ethtool "$IFACE_TO_TEST"
175af0e5461SLABBE Corentin
176af0e5461SLABBE Corentin	kci_netdev_stop "$IFACE_TO_UPDOWN"
177af0e5461SLABBE Corentin	return 0
178af0e5461SLABBE Corentin}
179af0e5461SLABBE Corentin
180af0e5461SLABBE Corentin#check for needed privileges
181af0e5461SLABBE Corentinif [ "$(id -u)" -ne 0 ];then
182af0e5461SLABBE Corentin	echo "SKIP: Need root privileges"
183*57aefc7cSShuah Khan (Samsung OSG)	exit $ksft_skip
184af0e5461SLABBE Corentinfi
185af0e5461SLABBE Corentin
18664cfcaedSDaniel Díazip link show 2>/dev/null >/dev/null
187af0e5461SLABBE Corentinif [ $? -ne 0 ];then
188af0e5461SLABBE Corentin	echo "SKIP: Could not run test without the ip tool"
189*57aefc7cSShuah Khan (Samsung OSG)	exit $ksft_skip
190af0e5461SLABBE Corentinfi
191af0e5461SLABBE Corentin
192af0e5461SLABBE CorentinTMP_LIST_NETDEV="$(mktemp)"
193af0e5461SLABBE Corentinif [ ! -e "$TMP_LIST_NETDEV" ];then
194af0e5461SLABBE Corentin	echo "FAIL: Cannot create a tmp file"
195af0e5461SLABBE Corentin	exit 1
196af0e5461SLABBE Corentinfi
197af0e5461SLABBE Corentin
198af0e5461SLABBE Corentinip link show |grep '^[0-9]' | grep -oE '[[:space:]].*eth[0-9]*:|[[:space:]].*enp[0-9]s[0-9]:' | cut -d\  -f2 | cut -d: -f1> "$TMP_LIST_NETDEV"
199af0e5461SLABBE Corentinwhile read netdev
200af0e5461SLABBE Corentindo
201af0e5461SLABBE Corentin	kci_test_netdev "$netdev"
202af0e5461SLABBE Corentindone < "$TMP_LIST_NETDEV"
203af0e5461SLABBE Corentin
204af0e5461SLABBE Corentinrm "$TMP_LIST_NETDEV"
205af0e5461SLABBE Corentinexit 0
206