diff mbox series

[v3,1/3] tst_net.sh: Fix for disabled IPv6

Message ID 20220510065104.1199-2-pvorel@suse.cz
State Accepted
Headers show
Series shell: Fixes for disabled IPv6 | expand

Commit Message

Petr Vorel May 10, 2022, 6:51 a.m. UTC
Tests failed in tst_init_iface even IPv4 only test was run.
Allow to init interfaces at least for IPv4.

We need IPv6 enabled on both endpoints to be usable,
unless test use TST_NET_SKIP_VARIABLE_INIT=1, i.e. use network library
functions but no test links (atm just library test tst_ipaddr_un.sh,
but netns_helper.sh will also use it). In this case only IPv6 on lhost
is tested.

Tests which use TST_IPV6=6 to force IPv6 (atm just broken_ip-nexthdr.sh)
need to be fixed, unless they use just tst_ipaddr_un() (e.g. library
test tst_ipaddr_un.sh).

Store result into $TST_NET_IPV6_ENABLED.

Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
changes v2->v3:
Rebased. tst_net_require_ipv6 is now in tst_net_setup() instead in
tst_net_parse_args().

 testcases/lib/tst_net.sh | 69 ++++++++++++++++++++++++++++++++++------
 1 file changed, 59 insertions(+), 10 deletions(-)

Comments

Cyril Hrubis May 10, 2022, 12:14 p.m. UTC | #1
Hi!
> +tst_net_detect_ipv6()
> +{
> +	local type="${1:-lhost}"
> +	local cmd='[ -f /proc/net/if_inet6 ]'
> +	local ret
> +
> +	if [ "$type" = "lhost" ]; then
> +		$cmd
> +	else
> +		tst_rhost_run -c "$cmd"
> +	fi
> +	ret=$?
> +
> +	if [ $ret -eq 0 ]; then
> +		TST_NET_IPV6_ENABLED=1
> +	else
> +		tst_res TINFO "IPv6 disabled on $type"
> +	fi
> +}
> +
> +tst_net_require_ipv6()
> +{
> +	[ "$TST_NET_IPV6_ENABLED" = 1 ] || tst_brk_ TCONF "IPv6 disabled"
> +}
> +
>  init_ltp_netspace()
>  {
>  	local pid
> @@ -515,7 +542,9 @@ tst_init_iface()
>  		ip link set $iface down || return $?
>  		ip route flush dev $iface || return $?
>  		ip addr flush dev $iface || return $?
> -		sysctl -qw net.ipv6.conf.$iface.accept_dad=0 || return $?
> +		if [ "$TST_NET_IPV6_ENABLED" = 1 ]; then
> +			sysctl -qw net.ipv6.conf.$iface.accept_dad=0 || return $?
> +		fi
>  		ip link set $iface up
>  		return $?
>  	fi
> @@ -527,7 +556,9 @@ tst_init_iface()
>  	tst_rhost_run -c "ip link set $iface down" || return $?
>  	tst_rhost_run -c "ip route flush dev $iface" || return $?
>  	tst_rhost_run -c "ip addr flush dev $iface" || return $?
> -	tst_rhost_run -c "sysctl -qw net.ipv6.conf.$iface.accept_dad=0" || return $?
> +	if [ "$TST_NET_IPV6_ENABLED" = 1 ]; then
> +		tst_rhost_run -c "sysctl -qw net.ipv6.conf.$iface.accept_dad=0" || return $?
> +	fi
>  	tst_rhost_run -c "ip link set $iface up"
>  }
>  
> @@ -604,7 +635,9 @@ tst_restore_ipaddr()
>  	local ret=0
>  	local backup_tst_ipv6=$TST_IPV6
>  	TST_IPV6= tst_add_ipaddr $type $link_num || ret=$?
> -	TST_IPV6=6 tst_add_ipaddr $type $link_num || ret=$?
> +	if [ "$TST_NET_IPV6_ENABLED" = 1 ]; then
> +		TST_IPV6=6 tst_add_ipaddr $type $link_num || ret=$?
> +	fi
>  	TST_IPV6=$backup_tst_ipv6
>  
>  	return $ret
> @@ -936,6 +969,10 @@ tst_default_max_pkt()
>  }
>  
>  [ -n "$TST_USE_LEGACY_API" ] && . test.sh || . tst_test.sh
> +
> +# detect IPv6 support on lhost for tests which don't use test links
> +tst_net_detect_ipv6
> +
>  [ -n "$TST_NET_SKIP_VARIABLE_INIT" ] && return 0
>  
>  # Management Link
> @@ -970,8 +1007,13 @@ IPV6_RHOST="${IPV6_RHOST:-fd00:1:1:1::1/64}"
>  if [ -z "$_tst_net_parse_variables" ]; then
>  	eval $(tst_net_ip_prefix $IPV4_LHOST || echo "exit $?")
>  	eval $(tst_net_ip_prefix -r $IPV4_RHOST || echo "exit $?")
> -	eval $(tst_net_ip_prefix $IPV6_LHOST || echo "exit $?")
> -	eval $(tst_net_ip_prefix -r $IPV6_RHOST || echo "exit $?")
> +
> +	tst_net_detect_ipv6 rhost
> +
> +	if [ "$TST_NET_IPV6_ENABLED" = 1 ]; then
> +		eval $(tst_net_ip_prefix $IPV6_LHOST || echo "exit $?")
> +		eval $(tst_net_ip_prefix -r $IPV6_RHOST || echo "exit $?")
> +	fi

If I'm looking right at this piece of code we do run the
tst_net_detect_ipv6 twice once for lhost and once for rhost when the
script is sourced and we only set the TST_NET_IPV6_ENABLED when the
check succeeds, right?

Shouldn't we also unset it when the check fails because otherwise the
check for rhost is basically no-op as long as the lhost supports ipv6?
Petr Vorel May 10, 2022, 1:06 p.m. UTC | #2
Hi Cyril,

...
> > +# detect IPv6 support on lhost for tests which don't use test links
> > +tst_net_detect_ipv6
> > +
> >  [ -n "$TST_NET_SKIP_VARIABLE_INIT" ] && return 0

> >  # Management Link
> > @@ -970,8 +1007,13 @@ IPV6_RHOST="${IPV6_RHOST:-fd00:1:1:1::1/64}"
> >  if [ -z "$_tst_net_parse_variables" ]; then
> >  	eval $(tst_net_ip_prefix $IPV4_LHOST || echo "exit $?")
> >  	eval $(tst_net_ip_prefix -r $IPV4_RHOST || echo "exit $?")
> > -	eval $(tst_net_ip_prefix $IPV6_LHOST || echo "exit $?")
> > -	eval $(tst_net_ip_prefix -r $IPV6_RHOST || echo "exit $?")
> > +
> > +	tst_net_detect_ipv6 rhost
> > +
> > +	if [ "$TST_NET_IPV6_ENABLED" = 1 ]; then
> > +		eval $(tst_net_ip_prefix $IPV6_LHOST || echo "exit $?")
> > +		eval $(tst_net_ip_prefix -r $IPV6_RHOST || echo "exit $?")
> > +	fi

> If I'm looking right at this piece of code we do run the
> tst_net_detect_ipv6 twice once for lhost and once for rhost when the
> script is sourced and we only set the TST_NET_IPV6_ENABLED when the
> check succeeds, right?

Yes.

FYI 2 tests run only first run for lhost - these which use
TST_NET_SKIP_VARIABLE_INIT=1:
* lib/newlib_tests/shell/net/tst_ipaddr_un.sh (which does not need this feature
  as test runs for both IPv4 and IPv6 and does not need IPv6 functionality)
* and with this patchset also for testcases/kernel/containers/netns/netns_lib.sh
  netns tests require IPv6 only for lhost, thus that's enough for
  TST_NET_SKIP_VARIABLE_INIT=1

> Shouldn't we also unset it when the check fails because otherwise the
> check for rhost is basically no-op as long as the lhost supports ipv6?

Hm, that's right. Patch below would will fix it.
Anyway I'm not much happy with tst_net_detect_ipv6 setting global variable
$TST_NET_IPV6_ENABLED. But I wanted to stay compatible with the rest of
tst_net.sh, which becomes more an more messy (rewriting/deleting old tests
will allow cleanup code for TST_USE_LEGACY_API=1). Anyway, feel free to suggest
something better.

Kind regards,
Petr

diff --git testcases/lib/tst_net.sh testcases/lib/tst_net.sh
index 29d80df89..48dd6e8eb 100644
--- testcases/lib/tst_net.sh
+++ testcases/lib/tst_net.sh
@@ -1008,7 +1008,7 @@ if [ -z "$_tst_net_parse_variables" ]; then
        eval $(tst_net_ip_prefix $IPV4_LHOST || echo "exit $?")
        eval $(tst_net_ip_prefix -r $IPV4_RHOST || echo "exit $?")

-       tst_net_detect_ipv6 rhost
+       [ "$TST_NET_IPV6_ENABLED" = 1 ] && tst_net_detect_ipv6 rhost

        if [ "$TST_NET_IPV6_ENABLED" = 1 ]; then
                eval $(tst_net_ip_prefix $IPV6_LHOST || echo "exit $?")
Cyril Hrubis May 10, 2022, 1:21 p.m. UTC | #3
Hi!
> diff --git testcases/lib/tst_net.sh testcases/lib/tst_net.sh
> index 29d80df89..48dd6e8eb 100644
> --- testcases/lib/tst_net.sh
> +++ testcases/lib/tst_net.sh
> @@ -1008,7 +1008,7 @@ if [ -z "$_tst_net_parse_variables" ]; then
>         eval $(tst_net_ip_prefix $IPV4_LHOST || echo "exit $?")
>         eval $(tst_net_ip_prefix -r $IPV4_RHOST || echo "exit $?")
> 
> -       tst_net_detect_ipv6 rhost
> +       [ "$TST_NET_IPV6_ENABLED" = 1 ] && tst_net_detect_ipv6 rhost

That solves the case where lhost does not support ipv6 but rhost does,
but there is still case where lhost does support it but rhost does not,
to fix that we have to clear TST_NET_IPV6_ENABLED in the
tst_net_detect_ipv6() as well.

	if [ $ret -eq 0 ]; then
		TST_NET_IPV6_ENABLED=1
	else
		TST_NET_IPV6_ENABLED=0
		tst_res TINFO "IPv6 disabled on $type"
	fi

>         if [ "$TST_NET_IPV6_ENABLED" = 1 ]; then
>                 eval $(tst_net_ip_prefix $IPV6_LHOST || echo "exit $?")
> 
>
Petr Vorel May 10, 2022, 2:04 p.m. UTC | #4
Hi Cyril,

> > -       tst_net_detect_ipv6 rhost
> > +       [ "$TST_NET_IPV6_ENABLED" = 1 ] && tst_net_detect_ipv6 rhost

> That solves the case where lhost does not support ipv6 but rhost does,
> but there is still case where lhost does support it but rhost does not,
> to fix that we have to clear TST_NET_IPV6_ENABLED in the
> tst_net_detect_ipv6() as well.

Ah, correct, thanks!

> 	if [ $ret -eq 0 ]; then
> 		TST_NET_IPV6_ENABLED=1
> 	else
> 		TST_NET_IPV6_ENABLED=0
nit: I'd slightly prefer to use TST_NET_IPV6_ENABLED=
> 		tst_res TINFO "IPv6 disabled on $type"
> 	fi

Kind regards,
Petr
diff mbox series

Patch

diff --git a/testcases/lib/tst_net.sh b/testcases/lib/tst_net.sh
index b27b7e51c..29d80df89 100644
--- a/testcases/lib/tst_net.sh
+++ b/testcases/lib/tst_net.sh
@@ -59,6 +59,8 @@  tst_net_remote_tmpdir()
 
 tst_net_setup()
 {
+	[ "$TST_IPVER" = 6 ] && tst_net_require_ipv6
+
 	tst_net_remote_tmpdir
 	[ -n "$TST_SETUP_CALLER" ] && $TST_SETUP_CALLER
 
@@ -98,6 +100,31 @@  tst_brk_()
 	[ -z "$TST_USE_LEGACY_API" ] && tst_brk $@ || tst_brkm $@
 }
 
+tst_net_detect_ipv6()
+{
+	local type="${1:-lhost}"
+	local cmd='[ -f /proc/net/if_inet6 ]'
+	local ret
+
+	if [ "$type" = "lhost" ]; then
+		$cmd
+	else
+		tst_rhost_run -c "$cmd"
+	fi
+	ret=$?
+
+	if [ $ret -eq 0 ]; then
+		TST_NET_IPV6_ENABLED=1
+	else
+		tst_res TINFO "IPv6 disabled on $type"
+	fi
+}
+
+tst_net_require_ipv6()
+{
+	[ "$TST_NET_IPV6_ENABLED" = 1 ] || tst_brk_ TCONF "IPv6 disabled"
+}
+
 init_ltp_netspace()
 {
 	local pid
@@ -515,7 +542,9 @@  tst_init_iface()
 		ip link set $iface down || return $?
 		ip route flush dev $iface || return $?
 		ip addr flush dev $iface || return $?
-		sysctl -qw net.ipv6.conf.$iface.accept_dad=0 || return $?
+		if [ "$TST_NET_IPV6_ENABLED" = 1 ]; then
+			sysctl -qw net.ipv6.conf.$iface.accept_dad=0 || return $?
+		fi
 		ip link set $iface up
 		return $?
 	fi
@@ -527,7 +556,9 @@  tst_init_iface()
 	tst_rhost_run -c "ip link set $iface down" || return $?
 	tst_rhost_run -c "ip route flush dev $iface" || return $?
 	tst_rhost_run -c "ip addr flush dev $iface" || return $?
-	tst_rhost_run -c "sysctl -qw net.ipv6.conf.$iface.accept_dad=0" || return $?
+	if [ "$TST_NET_IPV6_ENABLED" = 1 ]; then
+		tst_rhost_run -c "sysctl -qw net.ipv6.conf.$iface.accept_dad=0" || return $?
+	fi
 	tst_rhost_run -c "ip link set $iface up"
 }
 
@@ -604,7 +635,9 @@  tst_restore_ipaddr()
 	local ret=0
 	local backup_tst_ipv6=$TST_IPV6
 	TST_IPV6= tst_add_ipaddr $type $link_num || ret=$?
-	TST_IPV6=6 tst_add_ipaddr $type $link_num || ret=$?
+	if [ "$TST_NET_IPV6_ENABLED" = 1 ]; then
+		TST_IPV6=6 tst_add_ipaddr $type $link_num || ret=$?
+	fi
 	TST_IPV6=$backup_tst_ipv6
 
 	return $ret
@@ -936,6 +969,10 @@  tst_default_max_pkt()
 }
 
 [ -n "$TST_USE_LEGACY_API" ] && . test.sh || . tst_test.sh
+
+# detect IPv6 support on lhost for tests which don't use test links
+tst_net_detect_ipv6
+
 [ -n "$TST_NET_SKIP_VARIABLE_INIT" ] && return 0
 
 # Management Link
@@ -970,8 +1007,13 @@  IPV6_RHOST="${IPV6_RHOST:-fd00:1:1:1::1/64}"
 if [ -z "$_tst_net_parse_variables" ]; then
 	eval $(tst_net_ip_prefix $IPV4_LHOST || echo "exit $?")
 	eval $(tst_net_ip_prefix -r $IPV4_RHOST || echo "exit $?")
-	eval $(tst_net_ip_prefix $IPV6_LHOST || echo "exit $?")
-	eval $(tst_net_ip_prefix -r $IPV6_RHOST || echo "exit $?")
+
+	tst_net_detect_ipv6 rhost
+
+	if [ "$TST_NET_IPV6_ENABLED" = 1 ]; then
+		eval $(tst_net_ip_prefix $IPV6_LHOST || echo "exit $?")
+		eval $(tst_net_ip_prefix -r $IPV6_RHOST || echo "exit $?")
+	fi
 fi
 
 [ -n "$TST_USE_NETNS" -a "$TST_INIT_NETNS" != "no" ] && init_ltp_netspace
@@ -980,19 +1022,26 @@  if [ -z "$_tst_net_parse_variables" ]; then
 	eval $(tst_net_iface_prefix $IPV4_LHOST || echo "exit $?")
 	eval $(tst_rhost_run -c 'tst_net_iface_prefix -r '$IPV4_RHOST \
 		|| echo "exit $?")
-	eval $(tst_net_iface_prefix $IPV6_LHOST || echo "exit $?")
-	eval $(tst_rhost_run -c 'tst_net_iface_prefix -r '$IPV6_RHOST \
-		|| echo "exit $?")
+
+	if [ "$TST_NET_IPV6_ENABLED" = 1 ]; then
+		eval $(tst_net_iface_prefix $IPV6_LHOST || echo "exit $?")
+		eval $(tst_rhost_run -c 'tst_net_iface_prefix -r '$IPV6_RHOST \
+			|| echo "exit $?")
+	fi
 
 	eval $(tst_net_vars $IPV4_LHOST/$IPV4_LPREFIX \
 		$IPV4_RHOST/$IPV4_RPREFIX || echo "exit $?")
-	eval $(tst_net_vars $IPV6_LHOST/$IPV6_LPREFIX \
-		$IPV6_RHOST/$IPV6_RPREFIX || echo "exit $?")
+
+	if [ "$TST_NET_IPV6_ENABLED" = 1 ]; then
+		eval $(tst_net_vars $IPV6_LHOST/$IPV6_LPREFIX \
+			$IPV6_RHOST/$IPV6_RPREFIX || echo "exit $?")
+	fi
 
 	tst_res_ TINFO "Network config (local -- remote):"
 	tst_res_ TINFO "$LHOST_IFACES -- $RHOST_IFACES"
 	tst_res_ TINFO "$IPV4_LHOST/$IPV4_LPREFIX -- $IPV4_RHOST/$IPV4_RPREFIX"
 	tst_res_ TINFO "$IPV6_LHOST/$IPV6_LPREFIX -- $IPV6_RHOST/$IPV6_RPREFIX"
+
 	export _tst_net_parse_variables="yes"
 fi