Linux Audio

Check our new training course

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