From: Jiri Pirko <j...@nvidia.com>

Allow driver tests to work without specifying the netdevice names.
Introduce a possibility to search for available netdevices according to
set driver name. Allow test to specify the name by setting
NETIF_FIND_DRIVER variable.

Note that user overrides this either by passing netdevice names on the
command line or by declaring NETIFS array in custom forwarding.config
configuration file.

Signed-off-by: Jiri Pirko <j...@nvidia.com>
---
v3->v4:
- rebased on top of changes in patch #2
- reworded NETIF_FIND_DRIVER comment to explicitly refer to "importer"
- simplified driver_name_get() avoiding else branch
- s/find_netif/netif_find_driver/
v1->v2:
- removed unnecessary "-p" and "-e" options
- removed unnecessary "! -z" from the check
- moved NETIF_FIND_DRIVER declaration from the config options
---
 tools/testing/selftests/net/forwarding/lib.sh | 37 +++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/tools/testing/selftests/net/forwarding/lib.sh 
b/tools/testing/selftests/net/forwarding/lib.sh
index b63a5866ce97..d49b97edb886 100644
--- a/tools/testing/selftests/net/forwarding/lib.sh
+++ b/tools/testing/selftests/net/forwarding/lib.sh
@@ -89,6 +89,43 @@ if [[ "$(id -u)" -ne 0 ]]; then
        exit $ksft_skip
 fi
 
+##############################################################################
+# Find netifs by test-specified driver name
+
+driver_name_get()
+{
+       local dev=$1; shift
+       local driver_path="/sys/class/net/$dev/device/driver"
+
+       if [[ -L $driver_path ]]; then
+               basename `realpath $driver_path`
+       fi
+}
+
+netif_find_driver()
+{
+       local ifnames=`ip -j link show | jq -r ".[].ifname"`
+       local count=0
+
+       for ifname in $ifnames
+       do
+               local driver_name=`driver_name_get $ifname`
+               if [[ ! -z $driver_name && $driver_name == $NETIF_FIND_DRIVER 
]]; then
+                       count=$((count + 1))
+                       NETIFS[p$count]="$ifname"
+               fi
+       done
+}
+
+# Whether to find netdevice according to the driver speficied by the importer
+: "${NETIF_FIND_DRIVER:=}"
+
+if [[ $NETIF_FIND_DRIVER ]]; then
+       unset NETIFS
+       declare -A NETIFS
+       netif_find_driver
+fi
+
 net_forwarding_dir=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")
 
 if [[ -f $net_forwarding_dir/forwarding.config ]]; then
-- 
2.44.0


Reply via email to