[LTP] [PATCH v3 2/3] netns: Rewrite to use tst_net.sh

Petr Vorel pvorel@suse.cz
Tue May 10 08:51:03 CEST 2022


Use TST_NET_SKIP_VARIABLE_INIT=1 thus we get network library
functionality but use it's own test links.

Using tst_net.sh provides:
* detection of disabled IPv6
* -6 support instead of specifying ipv4/ipv6
* ping6 detection
* use tst_ipaddr_un() to get unused addresses instead of basic ranges
  192.168.0.x / fd00::x

Also
* cleanup other parameter handling - instead of passing 7 parameters to
  the setup use as the default the modern variants (ip and netlink), use
  switches for other variants (ns_exec and ifconfig)
* use ROD to shorten code
* cleanup duplicate doc

TODO: Test should be further cleanup to use tst_rhost_run over different
LTP_NETNS and tst_ping instead of $tping (changes already big enough).

Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
changes v2->v3:
* Rebased. $do_cleanup is now required. There could be a variable in
tst_net.sh or even tst_test.sh to signal that setup function passed,
but let's postpone it for now.
* Moved TST_TESTFUNC="do_test" into test files, rest of the variables in
  netns_helper.sh.
* Use conditional expansion for TST_{SETUP,CLEANUP}.

 runtest/containers                            |  32 +--
 .../kernel/containers/netns/netns_breakns.sh  |  31 +--
 .../kernel/containers/netns/netns_comm.sh     |  55 +---
 .../kernel/containers/netns/netns_helper.sh   | 238 +++++++-----------
 4 files changed, 122 insertions(+), 234 deletions(-)

diff --git a/runtest/containers b/runtest/containers
index eea7bfadb..ab0b94f51 100644
--- a/runtest/containers
+++ b/runtest/containers
@@ -25,22 +25,22 @@ mqns_04 mqns_04
 mqns_04_clone mqns_04 -clone
 
 netns_netlink netns_netlink
-netns_breakns_ns_exec_ipv4_netlink netns_breakns.sh ns_exec ipv4 netlink
-netns_breakns_ns_exec_ipv6_netlink netns_breakns.sh ns_exec ipv6 netlink
-netns_breakns_ns_exec_ipv4_ioctl netns_breakns.sh ns_exec ipv4 ioctl
-netns_breakns_ns_exec_ipv6_ioctl netns_breakns.sh ns_exec ipv6 ioctl
-netns_breakns_ip_ipv4_netlink netns_breakns.sh ip ipv4 netlink
-netns_breakns_ip_ipv6_netlink netns_breakns.sh ip ipv6 netlink
-netns_breakns_ip_ipv4_ioctl netns_breakns.sh ip ipv4 ioctl
-netns_breakns_ip_ipv6_ioctl netns_breakns.sh ip ipv6 ioctl
-netns_comm_ns_exec_ipv4_netlink netns_comm.sh ns_exec ipv4 netlink
-netns_comm_ns_exec_ipv6_netlink netns_comm.sh ns_exec ipv6 netlink
-netns_comm_ns_exec_ipv4_ioctl netns_comm.sh ns_exec ipv4 ioctl
-netns_comm_ns_exec_ipv6_ioctl netns_comm.sh ns_exec ipv6 ioctl
-netns_comm_ip_ipv4_netlink netns_comm.sh ip ipv4 netlink
-netns_comm_ip_ipv6_netlink netns_comm.sh ip ipv6 netlink
-netns_comm_ip_ipv4_ioctl netns_comm.sh ip ipv4 ioctl
-netns_comm_ip_ipv6_ioctl netns_comm.sh ip ipv6 ioctl
+netns_breakns_ip_ipv4_netlink netns_breakns.sh
+netns_breakns_ip_ipv6_netlink netns_breakns.sh -6
+netns_breakns_ip_ipv4_ioctl netns_breakns.sh -I
+netns_breakns_ip_ipv6_ioctl netns_breakns.sh -6I
+netns_breakns_ns_exec_ipv4_netlink netns_breakns.sh -e
+netns_breakns_ns_exec_ipv6_netlink netns_breakns.sh -6e
+netns_breakns_ns_exec_ipv4_ioctl netns_breakns.sh -eI
+netns_breakns_ns_exec_ipv6_ioctl netns_breakns.sh -6eI
+netns_comm_ip_ipv4_netlink netns_comm.sh
+netns_comm_ip_ipv6_netlink netns_comm.sh -6
+netns_comm_ip_ipv4_ioctl netns_comm.sh -I
+netns_comm_ip_ipv6_ioctl netns_comm.sh -6I
+netns_comm_ns_exec_ipv4_netlink netns_comm.sh -e
+netns_comm_ns_exec_ipv6_netlink netns_comm.sh -6e
+netns_comm_ns_exec_ipv4_ioctl netns_comm.sh -eI
+netns_comm_ns_exec_ipv6_ioctl netns_comm.sh -6eI
 netns_sysfs netns_sysfs.sh
 
 shmnstest_none shmnstest none
diff --git a/testcases/kernel/containers/netns/netns_breakns.sh b/testcases/kernel/containers/netns/netns_breakns.sh
index b7f255cb8..0b2eb319c 100755
--- a/testcases/kernel/containers/netns/netns_breakns.sh
+++ b/testcases/kernel/containers/netns/netns_breakns.sh
@@ -1,23 +1,9 @@
 #!/bin/sh
 # SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) 2022 Petr Vorel <pvorel@suse.cz>
 # Copyright (c) Köry Maincent <kory.maincent@bootlin.com> 2020
 # Copyright (c) 2015 Red Hat, Inc.
 #
-# SYNOPSIS:
-# netns_breakns.sh <NS_EXEC_PROGRAM> <IP_VERSION> <COMM_TYPE>
-#
-# OPTIONS:
-#	* NS_EXEC_PROGRAM (ns_exec|ip)
-#		Program which will be used to enter and run other commands
-#		inside a network namespace.
-#	* IP_VERSION (ipv4|ipv6)
-#		Version of IP. (ipv4|ipv6)
-#	* COMM_TYPE (netlink|ioctl)
-#		Communication type between kernel and user space
-#		for basic setup: enabling and assigning IP addresses
-#		to the virtual ethernet devices. (Uses 'ip' command for netlink
-#		and 'ifconfig' for ioctl.)
-#
 # Tests communication with ip (uses netlink) and ifconfig (uses ioctl)
 # over a device which is not visible from the current network namespace.
 #
@@ -26,15 +12,7 @@
 # 1. using netlink (ip command).
 # 2. using ioctl (ifconfig command).
 
-TST_POS_ARGS=3
-TST_SETUP=do_setup
-TST_TESTFUNC=do_test
-
-do_setup()
-{
-	netns_setup $PROG $IP_VER $COM_TYPE "192.168.0.2" "192.168.0.3" "fd00::2" "fd00::3"
-	tst_res TINFO "NS interaction: $PROG | devices setup: $COM_TYPE"
-}
+TST_TESTFUNC="do_test"
 
 do_test()
 {
@@ -45,9 +23,4 @@ do_test()
 }
 
 . netns_helper.sh
-
-PROG=$1
-IP_VER=$2
-COM_TYPE=$3
-
 tst_run
diff --git a/testcases/kernel/containers/netns/netns_comm.sh b/testcases/kernel/containers/netns/netns_comm.sh
index bee944802..6570f386e 100755
--- a/testcases/kernel/containers/netns/netns_comm.sh
+++ b/testcases/kernel/containers/netns/netns_comm.sh
@@ -1,23 +1,9 @@
 #!/bin/sh
 # SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) 2022 Petr Vorel <pvorel@suse.cz>
 # Copyright (c) Köry Maincent <kory.maincent@bootlin.com> 2020
 # Copyright (c) 2015 Red Hat, Inc.
 #
-# SYNOPSIS:
-# netns_comm.sh <NS_EXEC_PROGRAM> <IP_VERSION> <COMM_TYPE>
-#
-# OPTIONS:
-#       * NS_EXEC_PROGRAM (ns_exec|ip)
-#               Program which will be used to enter and run other commands
-#               inside a network namespace.
-#       * IP_VERSION (ipv4|ipv6)
-#               Version of IP. (ipv4|ipv6)
-#	* COMM_TYPE (netlink|ioctl)
-#		Communication type between kernel and user space
-#		for basic setup: enabling and assigning IP addresses
-#		to the virtual ethernet devices. (Uses 'ip' command for netlink
-#		and 'ifconfig' for ioctl.)
-#
 # Tests that a separate network namespace can configure and communicate
 # over the devices it sees. Tests are done using netlink(7) ('ip' command)
 # or ioctl(2) ('ifconfig' command) for controlling devices.
@@ -29,43 +15,24 @@
 #   3. communication over the lo (localhost) device in a separate
 #      network namespace
 
-TST_POS_ARGS=3
-TST_SETUP=do_setup
-TST_TESTFUNC=do_test
-
-do_setup()
-{
-	netns_setup $PROG $IP_VER $COM_TYPE "192.168.0.2" "192.168.0.3" "fd00::2" "fd00::3"
-	tst_res TINFO "NS interaction: $PROG | devices setup: $COM_TYPE"
-}
+TST_TESTFUNC="do_test"
 
 do_test()
 {
-	EXPECT_PASS $NS_EXEC $NS_HANDLE0 $NS_TYPE $tping -q -c2 -I veth0 $IP1 1>/dev/null
+	local ip_lo="127.0.0.1"
+	[ "$TST_IPV6" ] && ip_lo="::1"
 
+	EXPECT_PASS $NS_EXEC $NS_HANDLE0 $NS_TYPE $tping -q -c2 -I veth0 $IP1 1>/dev/null
 	EXPECT_PASS $NS_EXEC $NS_HANDLE1 $NS_TYPE $tping -q -c2 -I veth1 $IP0 1>/dev/null
 
-	case "$IP_VER" in
-	ipv4) ip_lo="127.0.0.1" ;;
-	ipv6) ip_lo="::1" ;;
-	esac
-	case "$COM_TYPE" in
-	netlink)
-		$NS_EXEC $NS_HANDLE0 $NS_TYPE ip link set dev lo up || \
-			tst_brk TBROK "enabling lo device failed"
-		;;
-	ioctl)
-		$NS_EXEC $NS_HANDLE0 $NS_TYPE ifconfig lo up || \
-			tst_brk TBROK "enabling lo device failed"
-		;;
-	esac
+	if [ "$COMM_TYPE" = "netlink" ]; then
+		ROD $NS_EXEC $NS_HANDLE0 $NS_TYPE ip link set dev lo up
+	else
+		ROD $NS_EXEC $NS_HANDLE0 $NS_TYPE ifconfig lo up
+	fi
+
 	EXPECT_PASS $NS_EXEC $NS_HANDLE0 $NS_TYPE $tping -q -c2 -I lo $ip_lo 1>/dev/null
 }
 
 . netns_helper.sh
-
-PROG=$1
-IP_VER=$2
-COM_TYPE=$3
-
 tst_run
diff --git a/testcases/kernel/containers/netns/netns_helper.sh b/testcases/kernel/containers/netns/netns_helper.sh
index a74d923d1..e8a09347a 100755
--- a/testcases/kernel/containers/netns/netns_helper.sh
+++ b/testcases/kernel/containers/netns/netns_helper.sh
@@ -1,36 +1,30 @@
 #!/bin/sh
 # SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) 2022 Petr Vorel <pvorel@suse.cz>
 # Copyright (c) Linux Test Project, 2014-2021
 # Copyright (c) 2015 Red Hat, Inc.
 
-TST_CLEANUP=netns_ns_exec_cleanup
 TST_NEEDS_ROOT=1
 TST_NEEDS_CMDS="ip ping"
 TST_NEEDS_DRIVERS="veth"
 
-# Set to 1 only for test cases using ifconfig (ioctl).
-USE_IFCONFIG=0
+TST_OPTS="eI"
+TST_PARSE_ARGS="netns_parse_args"
+TST_USAGE="netns_usage"
+TST_SETUP="${TST_SETUP:-netns_setup}"
+TST_CLEANUP="${TST_CLEANUP:-netns_cleanup}"
 
-# Variables which can be used in test cases (set by netns_setup() function):
+TST_NET_SKIP_VARIABLE_INIT=1
 
-# Use in test cases to execute commands inside a namespace. Set to 'ns_exec' or
-# 'ip netns exec' command according to NS_EXEC_PROGRAM argument specified in
-# netns_setup() function call.
-NS_EXEC=
+# from tst_net_vars.c
+IPV4_NET16_UNUSED="10.23"
+IPV6_NET32_UNUSED="fd00:23"
 
 # Set to "net" for ns_create/ns_exec as their options requires
 # to specify a namespace type. Empty for ip command.
 NS_TYPE=
 
-# IP addresses of veth0 (IP0) and veth1 (IP1) devices (ipv4/ipv6 variant
-# is determined according to the IP_VERSION argument specified in netns_setup()
-# function call.
-IP0=
-IP1=
-NETMASK=
-
-# 'ping' or 'ping6' according to the IP_VERSION argument specified
-# in netns_setup() function call.
+# 'ping' or 'ping6'
 tping=
 
 # Network namespaces handles for manipulating and executing commands inside
@@ -45,107 +39,73 @@ NS_HANDLE1=
 # ifconfig <device> $IFCONF_IN6_ARG IP/NETMASK
 IFCONF_IN6_ARG=
 
-# Sets up global variables which can be used in test cases (documented above),
-# creates two network namespaces and a pair of virtual ethernet devices, each
-# device in one namespace. Each device is then enabled and assigned an IP
-# address according to the function parameters. IFCONF_IN6_ARG variable is set
-# only if ipv6 variant of test case is used (determined by IP_VERSION argument).
-#
-# SYNOPSIS:
-# netns_setup <NS_EXEC_PROGRAM> <IP_VERSION> <COMM_TYPE> <IP4_VETH0>
-#             <IP4_VETH1> <IP6_VETH0> <IP6_VETH1>
-#
-# OPTIONS:
-#	* NS_EXEC_PROGRAM (ns_exec|ip)
-#		Program which will be used to enter and run other commands
-#		inside a network namespace.
-#	* IP_VERSION (ipv4|ipv6)
-#		Version of IP. (ipv4|ipv6)
-#	* COMM_TYPE (netlink|ioctl)
-#		Communication type between kernel and user space
-#		for enabling and assigning IP addresses to the virtual
-#		ethernet devices. Uses 'ip' command for netlink and 'ifconfig'
-#		for ioctl. (If set to ioctl, function also checks the existance
-#		of the 'ifconfig' command.)
-#	* IP4_VETH0, IP4_VETH1
-#		IPv4 addresses for veth0 and veth1 devices.
-#	* IP6_VETH0, IP6_VETH1
-#		IPv6 addresses for veth0 and veth1 devices.
-#
-# On success function returns, on error tst_brk is called and TC is terminated.
+# Program which will be used to enter and run other commands inside a network namespace.
+# (ns_exec|ip)
+NS_EXEC="ip"
+
+# Communication type between kernel and user space for basic setup: enabling and
+# assigning IP addresses to the virtual ethernet devices. (Uses 'ip' command for
+# netlink and 'ifconfig' for ioctl.)
+# (netlink|ioctl)
+COMM_TYPE="netlink"
+
+do_cleanup=
+
+netns_parse_args()
+{
+	case $1 in
+	e) NS_EXEC="ns_exec" ;;
+	I) COMM_TYPE="ioctl"; tst_require_cmds ifconfig ;;
+	esac
+}
+
+netns_usage()
+{
+	echo "usage: $0 [ -e ] [ -t ]"
+	echo "OPTIONS"
+	echo "-e use ns_exec instead of ip"
+	echo "-I test ioctl (with ifconfig) instead of netlink (with ip)"
+}
+
 netns_setup()
 {
-	case "$1" in
-	ns_exec)
+	if [ "$NS_EXEC" = "ip" ]; then
+		netns_ip_setup
+	else
 		setns_check
 		[ $? -eq 32 ] && tst_brk TCONF "setns not supported"
 
 		NS_TYPE="net"
 		netns_ns_exec_setup
-		TST_CLEANUP=netns_ns_exec_cleanup
-		;;
-	ip)
-		netns_ip_setup
-		TST_CLEANUP=netns_ip_cleanup
-		;;
-	*)
-		tst_brk TBROK \
-		"first argument must be a program used to enter a network namespace (ns_exec|ip)"
-		;;
-	esac
-
-	case "$3" in
-	netlink)
-		;;
-	ioctl)
-		USE_IFCONFIG=1
-		tst_require_cmds ifconfig
-		;;
-	*)
-		tst_brk TBROK \
-		"third argument must be a comm. type between kernel and user space (netlink|ioctl)"
-		;;
-	esac
-
-	if [ -z "$4" ]; then
-		tst_brk TBROK "fourth argument must be the IPv4 address for veth0"
-	fi
-	if [ -z "$5" ]; then
-		tst_brk TBROK "fifth argument must be the IPv4 address for veth1"
-	fi
-	if [ -z "$6" ]; then
-		tst_brk TBROK "sixth argument must be the IPv6 address for veth0"
-	fi
-	if [ -z "$7" ]; then
-		tst_brk TBROK "seventh argument must be the IPv6 address for veth1"
 	fi
 
-	case "$2" in
-	ipv4)
-		IP0=$4
-		IP1=$5
-		tping="ping"
-		NETMASK=24
-		;;
-	ipv6)
+	IP0=$(tst_ipaddr_un -c 1)
+	IP1=$(tst_ipaddr_un -c 2)
+
+	if [ "$TST_IPV6" ]; then
 		IFCONF_IN6_ARG="inet6 add"
-		IP0=$6
-		IP1=$7
-
-		if tst_cmd_available ping6; then
-			tping="ping6"
-		else
-			tping="ping -6"
-			tst_res_ TINFO "ping6 binary/symlink is missing, using workaround. Please, report missing ping6 to your distribution."
-		fi
 		NETMASK=64
-		;;
-	*)
-		tst_brk TBROK "second argument must be an ip version (ipv4|ipv6)"
-		;;
-	esac
+	else
+		NETMASK=24
+	fi
+
+	tping=ping$TST_IPV6
 
 	netns_set_ip
+
+	tst_res TINFO "testing netns over $COMM_TYPE with $NS_EXEC $PROG"
+	do_cleanup=1
+}
+
+netns_cleanup()
+{
+	[ "$do_cleanup" ] || return
+
+	if [ "$NS_EXEC" = "ip" ]; then
+		netns_ip_cleanup
+	else
+		netns_ns_exec_cleanup
+	fi
 }
 
 # Sets up NS_EXEC to use 'ns_exec', creates two network namespaces and stores
@@ -197,23 +157,18 @@ netns_ip_setup()
 	ip netns del $NS_HANDLE0 2>/dev/null
 	ip netns del $NS_HANDLE1 2>/dev/null
 
-	ip netns add $NS_HANDLE0 || \
-		tst_brk TBROK "unable to create a new network namespace"
-	ip netns add $NS_HANDLE1 || \
-		tst_brk TBROK "unable to create a new network namespace"
-
-	$NS_EXEC $NS_HANDLE0 ip link add veth0 type veth peer name veth1 || \
-		tst_brk TBROK "unable to create veth pair devices"
+	ROD ip netns add $NS_HANDLE0
+	ROD ip netns add $NS_HANDLE1
 
-	$NS_EXEC $NS_HANDLE0 ip link set veth1 netns $NS_HANDLE1 || \
-		tst_brk TBROK "unable to add device veth1 to the separate network namespace"
+	ROD $NS_EXEC $NS_HANDLE0 ip link add veth0 type veth peer name veth1
+	ROD $NS_EXEC $NS_HANDLE0 ip link set veth1 netns $NS_HANDLE1
 }
 
 # Enables virtual ethernet devices and assigns IP addresses for both
 # of them (IPv4/IPv6 variant is decided by netns_setup() function).
 netns_set_ip()
 {
-	[ "$NS_EXEC" ] || tst_brk TBROK "netns_setup() function must be called first"
+	local cmd="ip"
 
 	# This applies only for ipv6 variant:
 	# Do not accept Router Advertisements (accept_ra) and do not use
@@ -222,35 +177,28 @@ netns_set_ip()
 	# there is no other host with the same address, the address is
 	# considered to be "tentative" (attempts to bind() to the address fail
 	# with EADDRNOTAVAIL) which may cause problems for tests using ipv6.
-	echo 0 | $NS_EXEC $NS_HANDLE0 $NS_TYPE \
-		tee /proc/sys/net/ipv6/conf/veth0/accept_dad \
-		/proc/sys/net/ipv6/conf/veth0/accept_ra >/dev/null
-	echo 0 | $NS_EXEC $NS_HANDLE1 $NS_TYPE \
-		tee /proc/sys/net/ipv6/conf/veth1/accept_dad \
-		/proc/sys/net/ipv6/conf/veth1/accept_ra >/dev/null
-
-	case $USE_IFCONFIG in
-	1)
-		$NS_EXEC $NS_HANDLE0 $NS_TYPE ifconfig veth0 $IFCONF_IN6_ARG $IP0/$NETMASK ||
-			tst_brk TBROK "adding address to veth0 failed"
-		$NS_EXEC $NS_HANDLE1 $NS_TYPE ifconfig veth1 $IFCONF_IN6_ARG $IP1/$NETMASK ||
-			tst_brk TBROK "adding address to veth1 failed"
-		$NS_EXEC $NS_HANDLE0 $NS_TYPE ifconfig veth0 up ||
-			tst_brk TBROK "enabling veth0 device failed"
-		$NS_EXEC $NS_HANDLE1 $NS_TYPE ifconfig veth1 up ||
-			tst_brk TBROK "enabling veth1 device failed"
-		;;
-	*)
-		$NS_EXEC $NS_HANDLE0 $NS_TYPE ip address add $IP0/$NETMASK dev veth0 ||
-			tst_brk TBROK "adding address to veth0 failed"
-		$NS_EXEC $NS_HANDLE1 $NS_TYPE ip address add $IP1/$NETMASK dev veth1 ||
-			tst_brk TBROK "adding address to veth1 failed"
-		$NS_EXEC $NS_HANDLE0 $NS_TYPE ip link set veth0 up ||
-			tst_brk TBROK "enabling veth0 device failed"
-		$NS_EXEC $NS_HANDLE1 $NS_TYPE ip link set veth1 up ||
-			tst_brk TBROK "enabling veth1 device failed"
-		;;
-	esac
+	if [ "$TST_IPV6" ]; then
+		echo 0 | $NS_EXEC $NS_HANDLE0 $NS_TYPE \
+			tee /proc/sys/net/ipv6/conf/veth0/accept_dad \
+			/proc/sys/net/ipv6/conf/veth0/accept_ra >/dev/null
+		echo 0 | $NS_EXEC $NS_HANDLE1 $NS_TYPE \
+			tee /proc/sys/net/ipv6/conf/veth1/accept_dad \
+			/proc/sys/net/ipv6/conf/veth1/accept_ra >/dev/null
+	fi
+
+	[ "$COMM_TYPE" = "ioctl" ] && cmd="ifconfig"
+
+	if [ "$COMM_TYPE" = "netlink" ]; then
+		ROD $NS_EXEC $NS_HANDLE0 $NS_TYPE ip address add $IP0/$NETMASK dev veth0
+		ROD $NS_EXEC $NS_HANDLE1 $NS_TYPE ip address add $IP1/$NETMASK dev veth1
+		ROD $NS_EXEC $NS_HANDLE0 $NS_TYPE ip link set veth0 up
+		ROD $NS_EXEC $NS_HANDLE1 $NS_TYPE ip link set veth1 up
+	else
+		ROD $NS_EXEC $NS_HANDLE0 $NS_TYPE ifconfig veth0 $IFCONF_IN6_ARG $IP0/$NETMASK
+		ROD $NS_EXEC $NS_HANDLE1 $NS_TYPE ifconfig veth1 $IFCONF_IN6_ARG $IP1/$NETMASK
+		ROD $NS_EXEC $NS_HANDLE0 $NS_TYPE ifconfig veth0 up
+		ROD $NS_EXEC $NS_HANDLE1 $NS_TYPE ifconfig veth1 up
+	fi
 }
 
 netns_ns_exec_cleanup()
@@ -276,4 +224,4 @@ netns_ip_cleanup()
 	ip netns del $NS_HANDLE1 2>/dev/null
 }
 
-. tst_test.sh
+. tst_net.sh
-- 
2.36.0



More information about the ltp mailing list