[LTP] [PATCH v2 2/2] tst_net.sh: Detect IPv6 disabled on interface via sysctl

Petr Vorel pvorel@suse.cz
Fri Feb 17 16:10:36 CET 2023


IPv6 on interface can be also disabled by sysctl
net.ipv6.conf.$iface.disable_ipv6=1. This is better to be checked:
* for all interfaces before run (can be disabled even for netns
  interface previously created).
* before ip addr command tries to work with IPv6 (in tst_init_iface()
  and tst_add_ipaddr(), other functions should run these before)

Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
 testcases/lib/tst_net.sh | 46 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)

diff --git a/testcases/lib/tst_net.sh b/testcases/lib/tst_net.sh
index 96eed50793..f414dd9359 100644
--- a/testcases/lib/tst_net.sh
+++ b/testcases/lib/tst_net.sh
@@ -121,6 +121,47 @@ tst_net_detect_ipv6()
 	TST_NET_IPV6_ENABLED=1
 }
 
+# Detect IPv6 disabled on interface via sysctl
+# net.ipv6.conf.$iface.disable_ipv6=1.
+# $TST_NET_IPV6_ENABLED: 1 on IPv6 enabled, 0 on IPv6 disabled.
+# return: 0 on IPv6 enabled, 1 on IPv6 disabled.
+tst_net_detect_ipv6_iface()
+{
+	[ "$TST_NET_IPV6_ENABLED" = 1 ] || return 1
+
+	local iface="$1"
+	local type="${2:-lhost}"
+	local check="sysctl -n net.ipv6.conf.$iface.disable_ipv6"
+	local disabled
+
+	if [ "$type" = "lhost" ]; then
+		disabled=$($check)
+	else
+		disabled=$(tst_rhost_run -c "$check")
+	fi
+	if [ $disabled = 1 ]; then
+		tst_res_ TINFO "IPv6 disabled on $type on $iface"
+		TST_NET_IPV6_ENABLED=0
+		return 1
+	fi
+
+	return 0
+}
+
+# Detect IPv6 disabled on used interfaces.
+tst_net_check_ifaces_ipv6()
+{
+	local iface
+
+	for iface in $(tst_get_ifaces); do
+		tst_net_detect_ipv6_iface || return
+	done
+
+	for iface in $(tst_get_ifaces rhost); do
+		tst_net_detect_ipv6_iface $iface rhost || return
+	done
+}
+
 tst_net_require_ipv6()
 {
 	[ "$TST_NET_IPV6_ENABLED" = 1 ] || tst_brk_ TCONF "IPv6 disabled"
@@ -531,7 +572,9 @@ tst_init_iface()
 	local type="${1:-lhost}"
 	local link_num="${2:-0}"
 	local iface="$(tst_iface $type $link_num)"
+
 	tst_res_ TINFO "initialize '$type' '$iface' interface"
+	tst_net_detect_ipv6_iface $iface $type
 
 	if [ "$type" = "lhost" ]; then
 		if ip xfrm state 1>/dev/null 2>&1; then
@@ -591,6 +634,8 @@ tst_add_ipaddr()
 	local link_num="${2:-0}"
 	local iface=$(tst_iface $type $link_num)
 
+	tst_net_detect_ipv6_iface $iface $type
+
 	if [ "$TST_IPV6" ]; then
 		dad="nodad"
 		[ "$type" = "lhost" ] && mask=$IPV6_LPREFIX || mask=$IPV6_RPREFIX
@@ -1005,6 +1050,7 @@ tst_net_setup_network()
 		$IPV4_RHOST/$IPV4_RPREFIX || echo "exit $?")
 
 	if [ "$TST_NET_IPV6_ENABLED" = 1 ]; then
+		tst_net_check_ifaces_ipv6
 		eval $(tst_net_iface_prefix $IPV6_LHOST || echo "exit $?")
 		eval $(tst_rhost_run -c 'tst_net_iface_prefix -r '$IPV6_RHOST \
 			|| echo "exit $?")
-- 
2.39.1



More information about the ltp mailing list