diff mbox series

[net-next,v3,3/6] selftests: forwarding: add ability to assemble NETIFS array by driver name

Message ID 20240417164554.3651321-4-jiri@resnulli.us (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series selftests: virtio_net: introduce initial testing infrastructure | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 8 this patch: 8
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 1 maintainers not CCed: linux-kselftest@vger.kernel.org
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success net selftest script(s) already in Makefile
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 8 this patch: 8
netdev/checkpatch warning WARNING: line length of 86 exceeds 80 columns
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Jiri Pirko April 17, 2024, 4:45 p.m. UTC
From: Jiri Pirko <jiri@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 <jiri@nvidia.com>
---
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 | 39 +++++++++++++++++++
 1 file changed, 39 insertions(+)

Comments

Petr Machata April 18, 2024, 8:11 a.m. UTC | #1
Jiri Pirko <jiri@resnulli.us> writes:

> From: Jiri Pirko <jiri@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 <jiri@nvidia.com>
> ---
> 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 | 39 +++++++++++++++++++
>  1 file changed, 39 insertions(+)
>
> diff --git a/tools/testing/selftests/net/forwarding/lib.sh b/tools/testing/selftests/net/forwarding/lib.sh
> index 2e7695b94b6b..b3fd0f052d71 100644
> --- a/tools/testing/selftests/net/forwarding/lib.sh
> +++ b/tools/testing/selftests/net/forwarding/lib.sh
> @@ -94,6 +94,45 @@ if [[ ! -v NUM_NETIFS ]]; 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
> +		echo ""
> +	else
> +		basename `realpath $driver_path`
> +	fi

This is just:

	if [[ -L $driver_path ]]; then
		basename `realpath $driver_path`
	fi

> +}
> +
> +find_netif()

Maybe name it find_driver_netif? find_netif sounds super generic.

Also consider having it take an argument instead of accessing
environment NETIF_FIND_DRIVER directly.

> +{
> +	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 specified driver.
> +: "${NETIF_FIND_DRIVER:=}"

This would be better placed up there in the Topology description
section. Together with NETIFS and NETIF_NO_CABLE, as it concerns
specification of which interfaces to use.

> +
> +if [[ $NETIF_FIND_DRIVER ]]; then
> +	unset NETIFS
> +	declare -A NETIFS
> +	find_netif
> +fi
> +
>  net_forwarding_dir=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")
>  
>  if [[ -f $net_forwarding_dir/forwarding.config ]]; then
Petr Machata April 18, 2024, 8:43 a.m. UTC | #2
Petr Machata <petrm@nvidia.com> writes:

> Jiri Pirko <jiri@resnulli.us> writes:
>
>> +# Whether to find netdevice according to the specified driver.
>> +: "${NETIF_FIND_DRIVER:=}"
>
> This would be better placed up there in the Topology description
> section. Together with NETIFS and NETIF_NO_CABLE, as it concerns
> specification of which interfaces to use.

Oh never mind, it's not something a user should configure, but rather a
test API.
Jiri Pirko April 18, 2024, 12:01 p.m. UTC | #3
Thu, Apr 18, 2024 at 10:43:44AM CEST, petrm@nvidia.com wrote:
>
>Petr Machata <petrm@nvidia.com> writes:
>
>> Jiri Pirko <jiri@resnulli.us> writes:
>>
>>> +# Whether to find netdevice according to the specified driver.
>>> +: "${NETIF_FIND_DRIVER:=}"
>>
>> This would be better placed up there in the Topology description
>> section. Together with NETIFS and NETIF_NO_CABLE, as it concerns
>> specification of which interfaces to use.
>
>Oh never mind, it's not something a user should configure, but rather a
>test API.

Yep.
Jiri Pirko April 18, 2024, 12:14 p.m. UTC | #4
Thu, Apr 18, 2024 at 10:11:12AM CEST, petrm@nvidia.com wrote:
>
>Jiri Pirko <jiri@resnulli.us> writes:
>
>> From: Jiri Pirko <jiri@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 <jiri@nvidia.com>
>> ---
>> 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 | 39 +++++++++++++++++++
>>  1 file changed, 39 insertions(+)
>>
>> diff --git a/tools/testing/selftests/net/forwarding/lib.sh b/tools/testing/selftests/net/forwarding/lib.sh
>> index 2e7695b94b6b..b3fd0f052d71 100644
>> --- a/tools/testing/selftests/net/forwarding/lib.sh
>> +++ b/tools/testing/selftests/net/forwarding/lib.sh
>> @@ -94,6 +94,45 @@ if [[ ! -v NUM_NETIFS ]]; 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
>> +		echo ""
>> +	else
>> +		basename `realpath $driver_path`
>> +	fi
>
>This is just:
>
>	if [[ -L $driver_path ]]; then
>		basename `realpath $driver_path`
>	fi

Ok.


>
>> +}
>> +
>> +find_netif()
>
>Maybe name it find_driver_netif? find_netif sounds super generic.

netif_find_driver() to be aligned with the variable name.


>
>Also consider having it take an argument instead of accessing
>environment NETIF_FIND_DRIVER directly.

Considered, don't see any benefit. Touching outside variable NETIFS
anyway.


>
>> +{
>> +	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 specified driver.
>> +: "${NETIF_FIND_DRIVER:=}"
>
>This would be better placed up there in the Topology description
>section. Together with NETIFS and NETIF_NO_CABLE, as it concerns
>specification of which interfaces to use.


You replied yourself.



>
>> +
>> +if [[ $NETIF_FIND_DRIVER ]]; then
>> +	unset NETIFS
>> +	declare -A NETIFS
>> +	find_netif
>> +fi
>> +
>>  net_forwarding_dir=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")
>>  
>>  if [[ -f $net_forwarding_dir/forwarding.config ]]; then
>

Thanks!
diff mbox series

Patch

diff --git a/tools/testing/selftests/net/forwarding/lib.sh b/tools/testing/selftests/net/forwarding/lib.sh
index 2e7695b94b6b..b3fd0f052d71 100644
--- a/tools/testing/selftests/net/forwarding/lib.sh
+++ b/tools/testing/selftests/net/forwarding/lib.sh
@@ -94,6 +94,45 @@  if [[ ! -v NUM_NETIFS ]]; 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
+		echo ""
+	else
+		basename `realpath $driver_path`
+	fi
+}
+
+find_netif()
+{
+	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 specified driver.
+: "${NETIF_FIND_DRIVER:=}"
+
+if [[ $NETIF_FIND_DRIVER ]]; then
+	unset NETIFS
+	declare -A NETIFS
+	find_netif
+fi
+
 net_forwarding_dir=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")
 
 if [[ -f $net_forwarding_dir/forwarding.config ]]; then