selftests: net: Create veth pair for testing in networkless kernel
authorAbhinav Jain <jain.abhinav177@gmail.com>
Wed, 21 Aug 2024 17:19:01 +0000 (22:49 +0530)
committerJakub Kicinski <kuba@kernel.org>
Thu, 22 Aug 2024 23:56:06 +0000 (16:56 -0700)
Check if the netdev list is empty and create veth pair to be used for
feature on/off testing.
Remove the veth pair after testing is complete.

Signed-off-by: Abhinav Jain <jain.abhinav177@gmail.com>
Reviewed-by: Simon Horman <horms@kernel.org>
Link: https://patch.msgid.link/20240821171903.118324-2-jain.abhinav177@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
tools/testing/selftests/net/netdevice.sh

index e3afcb424710326f02d5078fcf0f4dc750ce68f3..999d72b6670c2c511956c85294a42fe953c1f000 100755 (executable)
@@ -129,6 +129,7 @@ kci_netdev_ethtool()
 
        kci_netdev_ethtool_test 74 'dump' "ethtool -d $netdev"
        kci_netdev_ethtool_test 94 'stats' "ethtool -S $netdev"
+
        return 0
 }
 
@@ -196,10 +197,24 @@ if [ ! -e "$TMP_LIST_NETDEV" ];then
 fi
 
 ip 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"
+
+if [ ! -s "$TMP_LIST_NETDEV" ]; then
+       echo "No valid network device found, creating veth pair"
+       ip link add veth0 type veth peer name veth1
+       echo "veth0" > "$TMP_LIST_NETDEV"
+       veth_created=1
+fi
+
 while read netdev
 do
        kci_test_netdev "$netdev"
 done < "$TMP_LIST_NETDEV"
 
+#clean up veth interface pair if it was created
+if [ "$veth_created" ]; then
+       ip link delete veth0
+       echo "Removed veth pair"
+fi
+
 rm "$TMP_LIST_NETDEV"
 exit 0