Skip to contentSkip to conversation

Simplify network setup + fix some network stress tests

RFC PATCH Superseded Petr Vorel 2 participants 36 messages Jul 21, 2017 – Aug 14, 2017

v7 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Petr Vorel

author
#0 lore raw

v7 cover Simplify network setup + fix some network stress tests

Hi,

as it might be uncomfortable to see patches on mailing list, you can also see them on github: https://github.com/pevik/ltp/tree/tst_net_vars.v7

Changes v6->v7:
* I more radically cleanup IPV4 multicast and route scripts. Also changed a bit interfaces tests. I spent a lot of time with cleanup and testing. Cleaned scripts should work under SSH and netns.
* I split tst_net_vars into 3 tools. This was necessary to have reading of
* rtnetlink on rhost.

TODO:
* Add more test links for netns (for route4-change-if).
* Fix also IPv6 scripts.

Alexey Kodanev (1):
network/dhcp: Fix removing veth kernel module

Petr Vorel (10):
network/stress: Add library test_net_stress.sh network/stress: Simplify make_background_tcp_traffic usage network/stress: Reduce the default number of cycles for various tests lib/test_net.sh: tst_rhost_run: Add testcases/bin into PATH for
SSH/RSH
network/stress: Fix and cleanup part of multicast IPv4 tests lib/test_net.sh: Add function reset_ltp_netspace() network/stress: Fix and cleanup route IPv4 tests network/stress: Further enhancements for route4-rmmod
network: Add tools for setup IP related environment variables
network: Use tools to set up IPv4 and IPv6 related variables

 include/tst_net.h                                  | 140 +++++
 testcases/lib/.gitignore                           |   3 +
 testcases/lib/Makefile                             |   2 +-
 testcases/lib/test_net.sh                          | 149 +++--
 testcases/lib/tst_net_iface_prefix.c               | 173 ++++++
 testcases/lib/tst_net_ip_prefix.c                  | 116 ++++
 testcases/lib/tst_net_vars.c                       | 655 +++++++++++++++++++++
 testcases/network/dhcp/dhcp_lib.sh                 |   9 +-
 testcases/network/stress/interface/if-addr-adddel  |  21 +-
 .../network/stress/interface/if-addr-addlarge      |  22 +-
 testcases/network/stress/interface/if-lib.sh       |  73 ---
 testcases/network/stress/interface/if-mtu-change   |  30 +-
 testcases/network/stress/interface/if-route-adddel |  24 +-
 .../network/stress/interface/if-route-addlarge     |  21 +-
 testcases/network/stress/interface/if-updown       |   9 +-
 testcases/network/stress/interface/if4-addr-change |  21 +-
 .../stress/multicast/grp-operation/mcast4-grpope01 | 221 +------
 .../stress/multicast/grp-operation/mcast4-grpope02 | 230 +-------
 .../stress/multicast/grp-operation/mcast4-grpope03 | 217 +------
 .../stress/multicast/grp-operation/mcast4-grpope04 | 236 +-------
 .../stress/multicast/grp-operation/mcast4-lib.sh   | 129 ++++
 testcases/network/stress/ns-tools/Makefile         |   2 +-
 testcases/network/stress/ns-tools/initialize_if    |   3 -
 .../network/stress/ns-tools/test_net_stress.sh     | 172 ++++++
 testcases/network/stress/route/00_Descriptions.txt |  23 +-
 testcases/network/stress/route/route4-change-dst   | 283 +--------
 testcases/network/stress/route/route4-change-gw    | 302 +---------
 testcases/network/stress/route/route4-change-if    | 336 ++---------
 testcases/network/stress/route/route4-ifdown       | 283 ++-------
 testcases/network/stress/route/route4-lib.sh       | 206 +++++++
 testcases/network/stress/route/route4-redirect     | 220 ++-----
 testcases/network/stress/route/route4-rmmod        | 305 ++--------
 testcases/network/stress/route/route6-change-dst   |   2 +-
 33 files changed, 2106 insertions(+), 2532 deletions(-)
 create mode 100644 include/tst_net.h
 create mode 100644 testcases/lib/tst_net_iface_prefix.c
 create mode 100644 testcases/lib/tst_net_ip_prefix.c
 create mode 100644 testcases/lib/tst_net_vars.c
 delete mode 100644 testcases/network/stress/interface/if-lib.sh
 create mode 100644 testcases/network/stress/multicast/grp-operation/mcast4-lib.sh
 create mode 100644 testcases/network/stress/ns-tools/test_net_stress.sh
 create mode 100644 testcases/network/stress/route/route4-lib.sh
Signature · 1 line
2.13.2
author
#1 lore raw

RFC PATCH v7 1/11 network/dhcp: Fix removing veth kernel module

From: Alexey Kodanev <[email protected]>

This patch fixes problems with dhcp based tests run via netns when they call TCONF:

$ /opt/ltp/testscripts/network.sh -6
dhcpd 1 TCONF: 'dhcpd' not found
...
cat: /sys/class/net/ltp_ns_veth2/address: No such file or directory

This also fixes other tests run after dhcp tests which are broken as netns setup got broken.

---
Alexey, this is your proposed patch from mailing list [1]. I've tested it.

[1]: http://lists.linux.it/pipermail/ltp/2017-June/004850.html

---
 testcases/network/dhcp/dhcp_lib.sh | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
testcases/network/dhcp/dhcp_lib.sh +4 -5
index 85f9b88bf..500123284 100755
@@ -40,8 +40,7 @@ init()
 	tst_require_root
 	tst_check_cmds cat $dhcp_name awk ip pgrep pkill dhclient
 
-	veth_loaded=
-	lsmod | grep -q '^veth ' && veth_loaded=1
+	lsmod | grep -q '^veth ' && veth_loaded=yes || veth_loaded=no
 
 	tst_resm TINFO "create veth interfaces"
 	ip li add $iface0 type veth peer name $iface1 || \
@@ -68,6 +67,8 @@ init()
 
 cleanup()
 {
+	[ -z "$veth_loaded" ] && return
+
 	stop_dhcp
 
 	pkill -f "dhclient -$ipv $iface1"
@@ -81,9 +82,7 @@ cleanup()
 
 	[ $veth_added ] && ip li del $iface0
 
-	if [ -z $veth_loaded ]; then
-		lsmod | grep -q '^veth ' && rmmod veth
-	fi
+	[ "$veth_loaded" = "no" ] && lsmod | grep -q '^veth ' && rmmod veth
 
 	tst_rmdir
 }
Signature · 1 line
2.13.2
#2 lore raw

RFC PATCH v7 1/11 network/dhcp: Fix removing veth kernel module

Quoted 10 lines · Petr Vorel

From: Alexey Kodanev <[email protected]>

This patch fixes problems with dhcp based tests run via netns when they call TCONF:

$ /opt/ltp/testscripts/network.sh -6
dhcpd 1 TCONF: 'dhcpd' not found
...
cat: /sys/class/net/ltp_ns_veth2/address: No such file or directory
Quoted 5 lines

This also fixes other tests run after dhcp tests which are broken as netns setup got broken.

Applied.

Thanks,
Alexey

author
#3 lore raw

RFC PATCH v7 2/11 network/stress: Add library test_net_stress.sh

In reply to Petr Vorel

+ use it in interface stress tests

test_net_stress.sh:
* test_net_stress.sh library is intended to be used throughout all stress tests reduce duplicity and use code from test_net.sh where possible instead of legacy scripts in testcases/network/stress/ns-tools/.
* TCID set automatically from basename.
* Add "unused network" related variables. So far used only IPv4 ones.
* Functions from if-lib.sh moved and (and some renamed) to test_net_stress.sh as they are going to be reused in other tests (not just interface stress tests).

Interfaces tests:
* Interfaces tests use test_net_stress.sh.
* Remove restore_ipaddr from cleanup functions as these functions would break netns testing with RTNETLINK errors. Use them just in setup during if-mtu-change where are really needed.

---
I'll port this library to be able to test also IPv6.

---
 testcases/lib/test_net.sh                          |  21 ++-
 testcases/network/stress/interface/if-addr-adddel  |  15 +-
 .../network/stress/interface/if-addr-addlarge      |  16 +-
 testcases/network/stress/interface/if-lib.sh       |  73 ---------
 testcases/network/stress/interface/if-mtu-change   |  23 +--
 testcases/network/stress/interface/if-route-adddel |  17 +-
 .../network/stress/interface/if-route-addlarge     |  15 +-
 testcases/network/stress/interface/if-updown       |   9 +-
 testcases/network/stress/interface/if4-addr-change |   4 +-
 testcases/network/stress/ns-tools/Makefile         |   2 +-
 .../network/stress/ns-tools/test_net_stress.sh     | 171 +++++++++++++++++++++
 11 files changed, 229 insertions(+), 137 deletions(-)
 delete mode 100644 testcases/network/stress/interface/if-lib.sh
 create mode 100644 testcases/network/stress/ns-tools/test_net_stress.sh
testcases/lib/test_net.sh +19 -2
index 10cf6da3a..178df3527 100644
@@ -428,6 +428,7 @@ tst_ping()
 	local src_iface="${1:-$(tst_iface)}"
 	local dst_addr="${2:-$(tst_ipaddr rhost)}"; shift $(( $# >= 2 ? 2 : 0 ))
 	local msg_sizes="$*"
+	local msg="tst_ping IPv${TST_IPV6:-4} iface $src_iface, msg_size"
 	local ret=0
 
 	# ping cmd use 56 as default message size
@@ -436,9 +437,9 @@ tst_ping()
 			-s $size -i 0 > /dev/null 2>&1
 		ret=$?
 		if [ $ret -eq 0 ]; then
-			tst_resm TINFO "tst_ping IPv${TST_IPV6:-4} msg_size $size pass"
+			tst_resm TINFO "$msg $size: pass"
 		else
-			tst_resm TINFO "tst_ping IPv${TST_IPV6:-4} msg_size $size fail"
+			tst_resm TINFO "$msg $size: fail"
 			break
 		fi
 	done
@@ -535,6 +536,22 @@ export RHOST_IPV6_HOST="${RHOST_IPV6_HOST:-:1}"
 export IPV4_NET16_UNUSED="${IPV4_NET16_UNUSED:-10.23}"
 export IPV6_NET32_UNUSED="${IPV6_NET32_UNUSED:-fd00:23}"
 
+export OCTET_3_IPV4_UNUSED=${OCTET_3_IPV4_UNUSED:-23} # x.x.N.x
+export OCTET_4_LHOST_IPV4_HOST_UNUSED=${OCTET_4_LHOST_IPV4_HOST_UNUSED:-2} # x.x.x.N
+export OCTET_4_RHOST_IPV4_HOST_UNUSED=${OCTET_4_RHOST_IPV4_HOST_UNUSED:-1} # x.x.x.N
+export LHOST_IPV4_HOST_UNUSED="${OCTET_3_IPV4_UNUSED}.${OCTET_4_LHOST_IPV4_HOST_UNUSED}"
+export RHOST_IPV4_HOST_UNUSED="${OCTET_3_IPV4_UNUSED}.${OCTET_4_RHOST_IPV4_HOST_UNUSED}"
+export LHOST_IPV4_UNUSED="${IPV4_NET16_UNUSED}.$LHOST_IPV4_HOST_UNUSED"
+export RHOST_IPV4_UNUSED="${IPV4_NET16_UNUSED}.$RHOST_IPV4_HOST_UNUSED"
+
+export OCTET_3_IPV6_UNUSED="${OCTET_3_IPV6_UNUSED:-:0}"
+export OCTET_4_LHOST_IPV6_HOST_UNUSED=${OCTET_4_LHOST_IPV6_HOST_UNUSED:-2}
+export OCTET_4_RHOST_IPV6_HOST_UNUSED=${OCTET_4_RHOST_IPV6_HOST_UNUSED:-1}
+export LHOST_IPV6_HOST_UNUSED="${OCTET_3_IPV6_UNUSED}:${OCTET_4_LHOST_IPV6_HOST_UNUSED}"
+export RHOST_IPV6_HOST_UNUSED="${OCTET_3_IPV6_UNUSED}:${OCTET_4_RHOST_IPV6_HOST_UNUSED}"
+export LHOST_IPV6_UNUSED="${IPV6_NET32_UNUSED}:$LHOST_IPV6_HOST_UNUSED"
+export RHOST_IPV6_UNUSED="${IPV6_NET32_UNUSED}:$RHOST_IPV6_HOST_UNUSED"
+
 export HTTP_DOWNLOAD_DIR="${HTTP_DOWNLOAD_DIR:-/var/www/html}"
 export FTP_DOWNLOAD_DIR="${FTP_DOWNLOAD_DIR:-/var/ftp}"
 export FTP_UPLOAD_DIR="${FTP_UPLOAD_DIR:-/var/ftp/pub}"
testcases/network/stress/interface/if-addr-adddel +4 -11
index dcc018a48..42c2d5afa 100644
@@ -18,20 +18,13 @@
 # Author: Mitsuru Chinen <[email protected]>
 
 TST_TOTAL=2
-TCID=if-addr-adddel
 
-. if-lib.sh
-
-TST_CLEANUP="do_cleanup"
+TST_CLEANUP="netstress_cleanup"
 
 # The interval of the check interface activity
 CHECK_INTERVAL=${CHECK_INTERVAL:-$(($NS_TIMES / 20))}
 
-do_cleanup()
-{
-	cleanup
-	restore_ipaddr
-}
+. test_net_stress.sh
 
 test_body()
 {
@@ -88,7 +81,7 @@ test_body()
 			return
 		fi
 
-		check_connectivity $cnt || return
+		check_connectivity_interval $cnt || return
 
 		cnt=$(($cnt + 1))
 
@@ -122,7 +115,7 @@ test_body()
 	tst_resm TPASS "Test is finished correctly"
 }
 
-setup
+netstress_setup
 
 tst_check_cmds ifconfig
 
testcases/network/stress/interface/if-addr-addlarge +4 -12
index 9e550068b..e667d9138 100644
@@ -18,20 +18,13 @@
 # Author: Mitsuru Chinen <[email protected]>
 
 TST_TOTAL=2
-TCID=if-addr-addlarge
 
-. if-lib.sh
-
-TST_CLEANUP="do_cleanup"
+TST_CLEANUP="netstress_cleanup"
 
 # The interval of the check interface activity
 CHECK_INTERVAL=${CHECK_INTERVAL:-$(($IP_TOTAL / 20))}
 
-do_cleanup()
-{
-	cleanup
-	restore_ipaddr
-}
+. test_net_stress.sh
 
 test_body()
 {
@@ -100,8 +93,7 @@ test_body()
 			return
 		fi
 
-		# Check the connectivity
-		check_connectivity $cnt || return
+		check_connectivity_interval $cnt || return
 
 		# Check the background TCP traffic
 		pgrep -x netstress > /dev/null || make_background_tcp_traffic
@@ -143,7 +135,7 @@ test_body()
 	tst_resm TPASS "Test is finished correctly"
 }
 
-setup
+netstress_setup
 
 tst_check_cmds ifconfig
 
testcases/network/stress/interface/if-lib.sh deleted +0 -73

73 lines not on this page · raw message

testcases/network/stress/interface/if-mtu-change +14 -9
index 637fa6389..5900f546a 100644
@@ -18,9 +18,6 @@
 # Author: Mitsuru Chinen <[email protected]>
 
 TST_TOTAL=2
-TCID=if-mtu-change
-
-. if-lib.sh
 
 TST_CLEANUP="do_cleanup"
 
@@ -33,16 +30,22 @@ CHANGE6_VALUES="1280 1445 1335 1390 1500 1280 1500 1280 1335 1500"
 [ "$TST_IPV6" ] && CHANGE_VALUES=$CHANGE6_VALUES
 saved_mtu=
 
+. test_net_stress.sh
+
+do_setup()
+{
+	tst_restore_ipaddr
+	tst_restore_ipaddr rhost
+	tst_wait_ipv6_dad
+}
+
 do_cleanup()
 {
-	cleanup
+	netstress_cleanup
 	if [ "$saved_mtu" ]; then
 		ip li set $(tst_iface) mtu $saved_mtu
 		tst_rhost_run -c "ip li set $(tst_iface rhost) mtu $saved_mtu"
 	fi
-	tst_restore_ipaddr
-	tst_restore_ipaddr rhost
-	tst_wait_ipv6_dad
 }
 
 test_body()
@@ -59,7 +62,7 @@ test_body()
 	local iface_rmt=$(tst_iface rhost)
 	[ "$TST_IPV6" ] && local netmask=64 || local netmask=16
 
-	tst_resm TINFO "'$cmd_name changes MTU $MTU_CHANGE_TIMES times " \
+	tst_resm TINFO "'$cmd_name changes MTU $MTU_CHANGE_TIMES times" \
 	               "every $CHANGE_INTERVAL seconds"
 
 	make_background_tcp_traffic
@@ -98,12 +101,14 @@ test_body()
 	done
 }
 
-setup
+netstress_setup
 
 tst_check_cmds ifconfig
 
 saved_mtu="$(cat /sys/class/net/$(tst_iface)/mtu)"
 
+do_setup
+
 test_body 'if_cmd'
 test_body 'ip_cmd'
 
testcases/network/stress/interface/if-route-adddel +5 -12
index 451674178..1b6830433 100644
@@ -18,19 +18,12 @@
 # Author: Mitsuru Chinen <[email protected]>
 
 TST_TOTAL=2
-TCID=if-route-adddel
 
-. if-lib.sh
-
-TST_CLEANUP="do_cleanup"
+TST_CLEANUP="netstress_cleanup"
 
 CHECK_INTERVAL=${CHECK_INTERVAL:-$(($NS_TIMES / 20))}
 
-do_cleanup()
-{
-	cleanup
-	restore_ipaddr
-}
+. test_net_stress.sh
 
 test_body()
 {
@@ -50,7 +43,7 @@ test_body()
 		new_rt=${IPV6_NET32_UNUSED}::
 		opt_rt="/64"
 	else
-		new_rt="${IPV4_NET16_UNUSED}.23.0"
+		new_rt="${IPV4_NET16_UNUSED}.${OCTET_3_IPV4_UNUSED}.0"
 		case $cmd_type in
 		rt_cmd) ;;
 		ip_cmd) opt_rt='/24' ;;
@@ -86,7 +79,7 @@ test_body()
 			return
 		fi
 
-		check_connectivity $cnt || return
+		check_connectivity_interval $cnt || return
 
 		# Check the background TCP traffic
 		pgrep -x netstress > /dev/null || make_background_tcp_traffic
@@ -97,7 +90,7 @@ test_body()
 	tst_resm TPASS "Test is finished correctly"
 }
 
-setup
+netstress_setup
 
 tst_check_cmds route
 
testcases/network/stress/interface/if-route-addlarge +4 -11
index e83cf60a7..3337b7225 100644
@@ -18,19 +18,12 @@
 # Author: Mitsuru Chinen <[email protected]>
 
 TST_TOTAL=2
-TCID=if-route-addlarge
 
-. if-lib.sh
-
-TST_CLEANUP="do_cleanup"
+TST_CLEANUP="netstress_cleanup"
 
 CHECK_INTERVAL=${CHECK_INTERVAL:-$(($ROUTE_TOTAL / 20))}
 
-do_cleanup()
-{
-	cleanup
-	restore_ipaddr
-}
+. test_net_stress.sh
 
 test_body()
 {
@@ -93,7 +86,7 @@ test_body()
 			return
 		fi
 
-		check_connectivity $cnt || return
+		check_connectivity_interval $cnt || return
 
 		# Check the background TCP traffic
 		pgrep -x netstress > /dev/null || make_background_tcp_traffic
@@ -112,7 +105,7 @@ test_body()
 	tst_resm TPASS "Test is finished correctly"
 }
 
-setup
+netstress_setup
 
 tst_check_cmds route
 
testcases/network/stress/interface/if-updown +5 -4
index deefef639..5ab54a1f8 100644
@@ -18,12 +18,13 @@
 # Author: Mitsuru Chinen <[email protected]>
 
 TST_TOTAL=2
-TCID=if-updown
 
-. if-lib.sh
+TST_CLEANUP="netstress_cleanup"
 
 CHECK_INTERVAL=${CHECK_INTERVAL:-$(($IF_UPDOWN_TIMES / 20))}
 
+. test_net_stress.sh
+
 test_body()
 {
 	local cmd_type=$1
@@ -59,7 +60,7 @@ test_body()
 			return
 		fi
 
-		check_connectivity $cnt restore_ip || return
+		check_connectivity_interval $cnt restore_ip || return
 
 		cnt=$(($cnt + 1))
 	done
@@ -67,7 +68,7 @@ test_body()
 	tst_resm TPASS "Test is finished correctly"
 }
 
-setup
+netstress_setup
 
 tst_check_cmds ifconfig
 
testcases/network/stress/interface/if4-addr-change +2 -2
index 54b8fd5fb..567f86643 100644
@@ -21,12 +21,12 @@ TST_TOTAL=1
 TCID=if4-addr-change
 TST_CLEANUP="do_cleanup"
 
-. test_net.sh
-
 CHECK_INTERVAL=${CHECK_INTERVAL:-$(($NS_TIMES / 20))}
 # Maximum host portion of the IPv4 address on the local host
 LHOST_IPV4_HOST_MAX="254"
 
+. test_net.sh
+
 do_cleanup()
 {
 	tst_restore_ipaddr
testcases/network/stress/ns-tools/Makefile +1 -1
index 61f85634a..1c186b232 100644
@@ -29,7 +29,7 @@ INSTALL_TARGETS		:= check_envval get_ifname initialize_if set_ipv4addr \
 			   check_icmpv6_connectivity check_netem check_setkey \
 			   create_file find_portbundle killall_icmp_traffic \
 			   killall_tcp_traffic killall_udp_traffic output_ipsec_conf \
-			   ns-echoclient
+			   ns-echoclient test_net_stress.sh
 
 FILTER_OUT_MAKE_TARGETS	:= ns-common
 
testcases/network/stress/ns-tools/test_net_stress.sh new file +171 -0

171 lines not on this page · raw message

Signature · 1 line
2.13.2
#4 lore raw

RFC PATCH v7 2/11 network/stress: Add library test_net_stress.sh

Hi,

Quoted 71 lines · Petr Vorel

+ use it in interface stress tests

test_net_stress.sh:
* test_net_stress.sh library is intended to be used throughout all stress tests reduce duplicity and use code from test_net.sh where possible instead of legacy scripts in testcases/network/stress/ns-tools/.
* TCID set automatically from basename.
* Add "unused network" related variables. So far used only IPv4 ones.
* Functions from if-lib.sh moved and (and some renamed) to test_net_stress.sh as they are going to be reused in other tests (not just interface stress tests).

Interfaces tests:
* Interfaces tests use test_net_stress.sh.
* Remove restore_ipaddr from cleanup functions as these functions would break netns testing with RTNETLINK errors. Use them just in setup during if-mtu-change where are really needed.

---
I'll port this library to be able to test also IPv6.

---
 testcases/lib/test_net.sh                          |  21 ++-
 testcases/network/stress/interface/if-addr-adddel  |  15 +-
 .../network/stress/interface/if-addr-addlarge      |  16 +-
 testcases/network/stress/interface/if-lib.sh       |  73 ---------
 testcases/network/stress/interface/if-mtu-change   |  23 +--
 testcases/network/stress/interface/if-route-adddel |  17 +-
 .../network/stress/interface/if-route-addlarge     |  15 +-
 testcases/network/stress/interface/if-updown       |   9 +-
 testcases/network/stress/interface/if4-addr-change |   4 +-
 testcases/network/stress/ns-tools/Makefile         |   2 +-
 .../network/stress/ns-tools/test_net_stress.sh     | 171 +++++++++++++++++++++
 11 files changed, 229 insertions(+), 137 deletions(-)
 delete mode 100644 testcases/network/stress/interface/if-lib.sh
 create mode 100644 testcases/network/stress/ns-tools/test_net_stress.sh
testcases/lib/test_net.sh +9 -2
index 10cf6da3a..178df3527 100644
@@ -428,6 +428,7 @@ tst_ping()
 	local src_iface="${1:-$(tst_iface)}"
 	local dst_addr="${2:-$(tst_ipaddr rhost)}"; shift $(( $# >= 2 ? 2 : 0 ))
 	local msg_sizes="$*"
+	local msg="tst_ping IPv${TST_IPV6:-4} iface $src_iface, msg_size"
 	local ret=0
 
 	# ping cmd use 56 as default message size
@@ -436,9 +437,9 @@ tst_ping()
 			-s $size -i 0 > /dev/null 2>&1
 		ret=$?
 		if [ $ret -eq 0 ]; then
-			tst_resm TINFO "tst_ping IPv${TST_IPV6:-4} msg_size $size pass"
+			tst_resm TINFO "$msg $size: pass"
 		else
-			tst_resm TINFO "tst_ping IPv${TST_IPV6:-4} msg_size $size fail"
+			tst_resm TINFO "$msg $size: fail"
 			break
 		fi
 	done
@@ -535,6 +536,22 @@ export RHOST_IPV6_HOST="${RHOST_IPV6_HOST:-:1}"
 export IPV4_NET16_UNUSED="${IPV4_NET16_UNUSED:-10.23}"
 export IPV6_NET32_UNUSED="${IPV6_NET32_UNUSED:-fd00:23}"
 
+export OCTET_3_IPV4_UNUSED=${OCTET_3_IPV4_UNUSED:-23} # x.x.N.x
+export OCTET_4_LHOST_IPV4_HOST_UNUSED=${OCTET_4_LHOST_IPV4_HOST_UNUSED:-2} # x.x.x.N
+export OCTET_4_RHOST_IPV4_HOST_UNUSED=${OCTET_4_RHOST_IPV4_HOST_UNUSED:-1} # x.x.x.N
+export LHOST_IPV4_HOST_UNUSED="${OCTET_3_IPV4_UNUSED}.${OCTET_4_LHOST_IPV4_HOST_UNUSED}"
+export RHOST_IPV4_HOST_UNUSED="${OCTET_3_IPV4_UNUSED}.${OCTET_4_RHOST_IPV4_HOST_UNUSED}"
+export LHOST_IPV4_UNUSED="${IPV4_NET16_UNUSED}.$LHOST_IPV4_HOST_UNUSED"

testcases/lib/test_net.sh export RHOST_IPV6_HOST="${RHOST_IPV6_HOST:-:1}" · line 554

Quoted 10 lines
testcases/lib/test_net.sh +10 -0
+export RHOST_IPV4_UNUSED="${IPV4_NET16_UNUSED}.$RHOST_IPV4_HOST_UNUSED"
+
+export OCTET_3_IPV6_UNUSED="${OCTET_3_IPV6_UNUSED:-:0}"
+export OCTET_4_LHOST_IPV6_HOST_UNUSED=${OCTET_4_LHOST_IPV6_HOST_UNUSED:-2}
+export OCTET_4_RHOST_IPV6_HOST_UNUSED=${OCTET_4_RHOST_IPV6_HOST_UNUSED:-1}
+export LHOST_IPV6_HOST_UNUSED="${OCTET_3_IPV6_UNUSED}:${OCTET_4_LHOST_IPV6_HOST_UNUSED}"
+export RHOST_IPV6_HOST_UNUSED="${OCTET_3_IPV6_UNUSED}:${OCTET_4_RHOST_IPV6_HOST_UNUSED}"
+export LHOST_IPV6_UNUSED="${IPV6_NET32_UNUSED}:$LHOST_IPV6_HOST_UNUSED"
+export RHOST_IPV6_UNUSED="${IPV6_NET32_UNUSED}:$RHOST_IPV6_HOST_UNUSED"
+

Hmm, what is the purpose of adding these variables to the library? Why someone would need to redefine any of them?

Quoted 3 lines
testcases/lib/test_net.sh +0 -0
 export HTTP_DOWNLOAD_DIR="${HTTP_DOWNLOAD_DIR:-/var/www/html}"
 export FTP_DOWNLOAD_DIR="${FTP_DOWNLOAD_DIR:-/var/ftp}"
 export FTP_UPLOAD_DIR="${FTP_UPLOAD_DIR:-/var/ftp/pub}"

...

Quoted 12 lines
testcases/lib/test_net.sh +12 -0
+
+# check_connectivity_interval CNT [RESTORE] [SRC_IFACE] [DST_ADDR]
+# CNT: loop step.
+# RESTORE: whether restore ip addr.
+# SRC_IFACE: source interface name.
+# DST_ADDR: destination IPv4 or IPv6 address.
+check_connectivity_interval()
+{
+	local cnt="$1"
+	local restore="${2:-false}"
+	local src_iface="${3:-$(tst_iface)}"
+	local dst_addr="${4:-$(tst_ipaddr rhost)}"

testcases/lib/test_net.sh

Quoted 10 lines
testcases/lib/test_net.sh +10 -0
+
+	[ $CHECK_INTERVAL -eq 0 ] && return
+
+	[ $(($cnt % $CHECK_INTERVAL)) -ne 0 ] && return
+
+	[ "$restore" != "false" ] && restore_ipaddr
+
+	check_connectivity $src_iface $dst_addr $cnt
+	return $?
+}

No need for 'return $?', the last command determines exit status for a function.

testcases/lib/test_net.sh

Quoted 11 lines
testcases/lib/test_net.sh +11 -0
+
+# Run netstress process on both lhost and rhost.
+# make_background_tcp_traffic [IP]
+# IP: server IP. Default value is $(tst_ipaddr)
+make_background_tcp_traffic()
+{
+	local port=$(tst_get_unused_port ipv${ipver} stream)
+	local ip="${1:-$(tst_ipaddr)}"
+
+	ROD netstress -R 3 -g $port > /dev/null 2>&1 &
+	tst_rhost_run -s -b -c "netstress -l -H $ip -g $port"

There is no point in adding 'ROD' and '-s' option for tst_rhost_run() if it's started in background.

Thanks,
Alexey
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20170724/b6966ad0/attachment.html>

author
#5 lore raw

RFC PATCH v7 2/11 network/stress: Add library test_net_stress.sh

Hi Alexey,

Quoted 10 lines
Quoted 10 lines
testcases/lib/test_net.sh +10 -0
+export OCTET_3_IPV4_UNUSED=${OCTET_3_IPV4_UNUSED:-23} # x.x.N.x
+export OCTET_4_LHOST_IPV4_HOST_UNUSED=${OCTET_4_LHOST_IPV4_HOST_UNUSED:-2} # x.x.x.N
+export OCTET_4_RHOST_IPV4_HOST_UNUSED=${OCTET_4_RHOST_IPV4_HOST_UNUSED:-1} # x.x.x.N
+export LHOST_IPV4_HOST_UNUSED="${OCTET_3_IPV4_UNUSED}.${OCTET_4_LHOST_IPV4_HOST_UNUSED}"
+export RHOST_IPV4_HOST_UNUSED="${OCTET_3_IPV4_UNUSED}.${OCTET_4_RHOST_IPV4_HOST_UNUSED}"
+export LHOST_IPV4_UNUSED="${IPV4_NET16_UNUSED}.$LHOST_IPV4_HOST_UNUSED"
+export RHOST_IPV4_UNUSED="${IPV4_NET16_UNUSED}.$RHOST_IPV4_HOST_UNUSED"
+
+export OCTET_3_IPV6_UNUSED="${OCTET_3_IPV6_UNUSED:-:0}"
+export OCTET_4_LHOST_IPV6_HOST_UNUSED=${OCTET_4_LHOST_IPV6_HOST_UNUSED:-2}
Quoted 8 lines

testcases/lib/test_net.sh export RHOST_IPV6_HOST="${RHOST_IPV6_HOST:-:1}" · line 553

Quoted 5 lines
testcases/lib/test_net.sh +5 -0
+export OCTET_4_RHOST_IPV6_HOST_UNUSED=${OCTET_4_RHOST_IPV6_HOST_UNUSED:-1}
+export LHOST_IPV6_HOST_UNUSED="${OCTET_3_IPV6_UNUSED}:${OCTET_4_LHOST_IPV6_HOST_UNUSED}"
+export RHOST_IPV6_HOST_UNUSED="${OCTET_3_IPV6_UNUSED}:${OCTET_4_RHOST_IPV6_HOST_UNUSED}"
+export LHOST_IPV6_UNUSED="${IPV6_NET32_UNUSED}:$LHOST_IPV6_HOST_UNUSED"
+export RHOST_IPV6_UNUSED="${IPV6_NET32_UNUSED}:$RHOST_IPV6_HOST_UNUSED"

Hmm, what is the purpose of adding these variables to the library? Why someone would need to redefine any of them?

I thought someone might appreciate ability to full control of IP
addresses, but you're right it's probably useless.
If you don't mind, I'll keep {L,R}HOST_IPV{4,6}_UNUSED variables as they are used in many scripts (DRY approach) and delete the rest of variables.

Quoted 11 lines
Quoted 11 lines
testcases/network/stress/ns-tools/test_net_stress.sh new file +11 -0
+check_connectivity_interval()
+{
+	local cnt="$1"
+	local restore="${2:-false}"
+	local src_iface="${3:-$(tst_iface)}"
+	local dst_addr="${4:-$(tst_ipaddr rhost)}"
+
+	[ $CHECK_INTERVAL -eq 0 ] && return
+
+	[ $(($cnt % $CHECK_INTERVAL)) -ne 0 ] && return
+
Quoted 8 lines

testcases/network/stress/ns-tools/test_net_stress.sh line 148

Quoted 5 lines
testcases/network/stress/ns-tools/test_net_stress.sh new file +5 -0
+	[ "$restore" != "false" ] && restore_ipaddr
+
+	check_connectivity $src_iface $dst_addr $cnt
+	return $?
+}

No need for 'return $?', the last command determines exit status for a function.

Right, thanks.

Quoted 10 lines

testcases/network/stress/ns-tools/test_net_stress.sh line 159

Quoted 7 lines
testcases/network/stress/ns-tools/test_net_stress.sh new file +7 -0
+make_background_tcp_traffic()
+{
+	local port=$(tst_get_unused_port ipv${ipver} stream)
+	local ip="${1:-$(tst_ipaddr)}"
+
+	ROD netstress -R 3 -g $port > /dev/null 2>&1 &
+	tst_rhost_run -s -b -c "netstress -l -H $ip -g $port"

There is no point in adding 'ROD' and '-s' option for tst_rhost_run() if it's started in background.

Right, thanks.

Kind regards,
Petr

#6 lore raw

RFC PATCH v7 2/11 network/stress: Add library test_net_stress.sh

Quoted 19 lines · Petr Vorel

Hi Alexey,

Quoted 9 lines
Quoted 9 lines
testcases/lib/test_net.sh +9 -0
+export OCTET_3_IPV4_UNUSED=${OCTET_3_IPV4_UNUSED:-23} # x.x.N.x
+export OCTET_4_LHOST_IPV4_HOST_UNUSED=${OCTET_4_LHOST_IPV4_HOST_UNUSED:-2} # x.x.x.N
+export OCTET_4_RHOST_IPV4_HOST_UNUSED=${OCTET_4_RHOST_IPV4_HOST_UNUSED:-1} # x.x.x.N
+export LHOST_IPV4_HOST_UNUSED="${OCTET_3_IPV4_UNUSED}.${OCTET_4_LHOST_IPV4_HOST_UNUSED}"
+export RHOST_IPV4_HOST_UNUSED="${OCTET_3_IPV4_UNUSED}.${OCTET_4_RHOST_IPV4_HOST_UNUSED}"
+export LHOST_IPV4_UNUSED="${IPV4_NET16_UNUSED}.$LHOST_IPV4_HOST_UNUSED"
+export RHOST_IPV4_UNUSED="${IPV4_NET16_UNUSED}.$RHOST_IPV4_HOST_UNUSED"
+
+export OCTET_3_IPV6_UNUSED="${OCTET_3_IPV6_UNUSED:-:0}"
Quoted 8 lines

testcases/lib/test_net.sh export RHOST_IPV6_HOST="${RHOST_IPV6_HOST:-:1}" · line 553

Quoted 6 lines
testcases/lib/test_net.sh +6 -0
+export OCTET_4_LHOST_IPV6_HOST_UNUSED=${OCTET_4_LHOST_IPV6_HOST_UNUSED:-2}
+export OCTET_4_RHOST_IPV6_HOST_UNUSED=${OCTET_4_RHOST_IPV6_HOST_UNUSED:-1}
+export LHOST_IPV6_HOST_UNUSED="${OCTET_3_IPV6_UNUSED}:${OCTET_4_LHOST_IPV6_HOST_UNUSED}"
+export RHOST_IPV6_HOST_UNUSED="${OCTET_3_IPV6_UNUSED}:${OCTET_4_RHOST_IPV6_HOST_UNUSED}"
+export LHOST_IPV6_UNUSED="${IPV6_NET32_UNUSED}:$LHOST_IPV6_HOST_UNUSED"
+export RHOST_IPV6_UNUSED="${IPV6_NET32_UNUSED}:$RHOST_IPV6_HOST_UNUSED"

Hmm, what is the purpose of adding these variables to the library? Why someone would need to redefine any of them?

Quoted 4 lines

I thought someone might appreciate ability to full control of IP
addresses, but you're right it's probably useless.
If you don't mind, I'll keep {L,R}HOST_IPV{4,6}_UNUSED variables as they are used in many scripts (DRY approach) and delete the rest of variables.

What about adding a new function similar to tst_ipaddr()?

tst_ipaddr_un(), tst_ipaddr_new(), etc.? Probably we would need to add some optional counter parameter along with lhost/rhost...

Best regards,
Alexey

author
#7 lore raw

RFC PATCH v7 2/11 network/stress: Add library test_net_stress.sh

Quoted 26 lines · Petr Vorel
Quoted 19 lines

Hi Alexey,

Quoted 9 lines
Quoted 9 lines
testcases/lib/test_net.sh +9 -0
+export OCTET_3_IPV4_UNUSED=${OCTET_3_IPV4_UNUSED:-23} # x.x.N.x
+export OCTET_4_LHOST_IPV4_HOST_UNUSED=${OCTET_4_LHOST_IPV4_HOST_UNUSED:-2} # x.x.x.N
+export OCTET_4_RHOST_IPV4_HOST_UNUSED=${OCTET_4_RHOST_IPV4_HOST_UNUSED:-1} # x.x.x.N
+export LHOST_IPV4_HOST_UNUSED="${OCTET_3_IPV4_UNUSED}.${OCTET_4_LHOST_IPV4_HOST_UNUSED}"
+export RHOST_IPV4_HOST_UNUSED="${OCTET_3_IPV4_UNUSED}.${OCTET_4_RHOST_IPV4_HOST_UNUSED}"
+export LHOST_IPV4_UNUSED="${IPV4_NET16_UNUSED}.$LHOST_IPV4_HOST_UNUSED"
+export RHOST_IPV4_UNUSED="${IPV4_NET16_UNUSED}.$RHOST_IPV4_HOST_UNUSED"
+
+export OCTET_3_IPV6_UNUSED="${OCTET_3_IPV6_UNUSED:-:0}"
Quoted 8 lines

testcases/lib/test_net.sh export RHOST_IPV6_HOST="${RHOST_IPV6_HOST:-:1}" · line 553

Quoted 6 lines
testcases/lib/test_net.sh +6 -0
+export OCTET_4_LHOST_IPV6_HOST_UNUSED=${OCTET_4_LHOST_IPV6_HOST_UNUSED:-2}
+export OCTET_4_RHOST_IPV6_HOST_UNUSED=${OCTET_4_RHOST_IPV6_HOST_UNUSED:-1}
+export LHOST_IPV6_HOST_UNUSED="${OCTET_3_IPV6_UNUSED}:${OCTET_4_LHOST_IPV6_HOST_UNUSED}"
+export RHOST_IPV6_HOST_UNUSED="${OCTET_3_IPV6_UNUSED}:${OCTET_4_RHOST_IPV6_HOST_UNUSED}"
+export LHOST_IPV6_UNUSED="${IPV6_NET32_UNUSED}:$LHOST_IPV6_HOST_UNUSED"
+export RHOST_IPV6_UNUSED="${IPV6_NET32_UNUSED}:$RHOST_IPV6_HOST_UNUSED"

Hmm, what is the purpose of adding these variables to the library? Why someone would need to redefine any of them?

Quoted 4 lines

I thought someone might appreciate ability to full control of IP
addresses, but you're right it's probably useless.
If you don't mind, I'll keep {L,R}HOST_IPV{4,6}_UNUSED variables as they are used in many scripts (DRY approach) and delete the rest of variables.

Quoted 4 lines

What about adding a new function similar to tst_ipaddr()?

tst_ipaddr_un(), tst_ipaddr_new(), etc.? Probably we would need to add some optional counter parameter along with lhost/rhost...

How about this?

tst_ipaddr_un()

{
	local type="${1:-lhost}"
	local counter="${2:-1}"
	local host net max_counter max_net max_host tmp

	[ "$TST_IPV6" ] && max_net=65536 || max_net=256
	max_host=$((max_net - 2))
	max_counter=$((max_host * max_net / 2))

	if [ $counter -gt $max_counter ]; then
		tst_resm TCONF "'$counter' higher than max counter limit '$max_counter'"
	fi

	tmp=$((counter * 2))
	[ "$type" = "rhost" ] && tmp=$((tmp - 1))

	host=$((tmp % max_host))
	net=$((tmp / max_host))
	[ $host -eq 0 ] && { host=$max_host; net=$((net - 1)); }

	if [ "$TST_IPV6" ]; then
		echo "${IPV6_NET32_UNUSED}::$(printf %x:%x ${net} ${host})"
	else
		echo "${IPV4_NET16_UNUSED}.${net}.${host}"
	fi
}

Given the fact that most of the tests needs just one IP address (the exception are route4-change-{if,gw}, if-{addr,route}-{adddel,addlarge}) it's probably too overhead and error prone. I mean working with last octet/hextet might be enough.

And if we want to work with more than last octet, we might appreciate to be able to specify (optionally) third octet as well ($net variable).
Scripts which could benefit from it are at least:
if-{addr,route}-addlarge, route4-change-if.

Kind regards,
Petr

#8 lore raw

RFC PATCH v7 2/11 network/stress: Add library test_net_stress.sh

Quoted 30 lines · Petr Vorel
Quoted 19 lines · Petr Vorel
Quoted 18 lines

Hi Alexey,

Quoted 17 lines
Quoted 5 lines
testcases/lib/test_net.sh +5 -0
+export OCTET_3_IPV4_UNUSED=${OCTET_3_IPV4_UNUSED:-23} # x.x.N.x
+export OCTET_4_LHOST_IPV4_HOST_UNUSED=${OCTET_4_LHOST_IPV4_HOST_UNUSED:-2} # x.x.x.N
+export OCTET_4_RHOST_IPV4_HOST_UNUSED=${OCTET_4_RHOST_IPV4_HOST_UNUSED:-1} # x.x.x.N
+export LHOST_IPV4_HOST_UNUSED="${OCTET_3_IPV4_UNUSED}.${OCTET_4_LHOST_IPV4_HOST_UNUSED}"
+export RHOST_IPV4_HOST_UNUSED="${OCTET_3_IPV4_UNUSED}.${OCTET_4_RHOST_IPV4_HOST_UNUSED}"

testcases/lib/test_net.sh export RHOST_IPV6_HOST="${RHOST_IPV6_HOST:-:1}" · line 553

Quoted 10 lines
testcases/lib/test_net.sh +10 -0
+export LHOST_IPV4_UNUSED="${IPV4_NET16_UNUSED}.$LHOST_IPV4_HOST_UNUSED"
+export RHOST_IPV4_UNUSED="${IPV4_NET16_UNUSED}.$RHOST_IPV4_HOST_UNUSED"
+
+export OCTET_3_IPV6_UNUSED="${OCTET_3_IPV6_UNUSED:-:0}"
+export OCTET_4_LHOST_IPV6_HOST_UNUSED=${OCTET_4_LHOST_IPV6_HOST_UNUSED:-2}
+export OCTET_4_RHOST_IPV6_HOST_UNUSED=${OCTET_4_RHOST_IPV6_HOST_UNUSED:-1}
+export LHOST_IPV6_HOST_UNUSED="${OCTET_3_IPV6_UNUSED}:${OCTET_4_LHOST_IPV6_HOST_UNUSED}"
+export RHOST_IPV6_HOST_UNUSED="${OCTET_3_IPV6_UNUSED}:${OCTET_4_RHOST_IPV6_HOST_UNUSED}"
+export LHOST_IPV6_UNUSED="${IPV6_NET32_UNUSED}:$LHOST_IPV6_HOST_UNUSED"
+export RHOST_IPV6_UNUSED="${IPV6_NET32_UNUSED}:$RHOST_IPV6_HOST_UNUSED"

Hmm, what is the purpose of adding these variables to the library? Why someone would need to redefine any of them?

Quoted 8 lines
Quoted 4 lines

I thought someone might appreciate ability to full control of IP
addresses, but you're right it's probably useless.
If you don't mind, I'll keep {L,R}HOST_IPV{4,6}_UNUSED variables as they are used in many scripts (DRY approach) and delete the rest of variables.

What about adding a new function similar to tst_ipaddr()? tst_ipaddr_un(), tst_ipaddr_new(), etc.? Probably we would need to add some optional counter parameter along with lhost/rhost...

How about this?

tst_ipaddr_un()

Quoted 12 lines
{
	local type="${1:-lhost}"
	local counter="${2:-1}"
	local host net max_counter max_net max_host tmp

	[ "$TST_IPV6" ] && max_net=65536 || max_net=256
	max_host=$((max_net - 2))
	max_counter=$((max_host * max_net / 2))

	if [ $counter -gt $max_counter ]; then
		tst_resm TCONF "'$counter' higher than max counter limit '$max_counter'"
	fi

I guess we can safely omit this check.

Quoted 14 lines
	tmp=$((counter * 2))
	[ "$type" = "rhost" ] && tmp=$((tmp - 1))

	host=$((tmp % max_host))
	net=$((tmp / max_host))
	[ $host -eq 0 ] && { host=$max_host; net=$((net - 1)); }

	if [ "$TST_IPV6" ]; then
		echo "${IPV6_NET32_UNUSED}::$(printf %x:%x ${net} ${host})"
	else
		echo "${IPV4_NET16_UNUSED}.${net}.${host}"
	fi
}

Looks good to me.

Quoted 13 lines

Given the fact that most of the tests needs just one IP address (the exception are route4-change-{if,gw}, if-{addr,route}-{adddel,addlarge}) it's probably too overhead and error prone. I mean working with last octet/hextet might be enough.

And if we want to work with more than last octet, we might appreciate to be able to specify (optionally) third octet as well ($net variable).
Scripts which could benefit from it are at least:
if-{addr,route}-addlarge, route4-change-if.

Kind regards,
Petr

author
#9 lore raw

RFC PATCH v7 2/11 network/stress: Add library test_net_stress.sh

Quoted 40 lines · Petr Vorel
Quoted 38 lines
Quoted 19 lines · Petr Vorel
Quoted 18 lines

Hi Alexey,

Quoted 17 lines
Quoted 5 lines
testcases/lib/test_net.sh +5 -0
+export OCTET_3_IPV4_UNUSED=${OCTET_3_IPV4_UNUSED:-23} # x.x.N.x
+export OCTET_4_LHOST_IPV4_HOST_UNUSED=${OCTET_4_LHOST_IPV4_HOST_UNUSED:-2} # x.x.x.N
+export OCTET_4_RHOST_IPV4_HOST_UNUSED=${OCTET_4_RHOST_IPV4_HOST_UNUSED:-1} # x.x.x.N
+export LHOST_IPV4_HOST_UNUSED="${OCTET_3_IPV4_UNUSED}.${OCTET_4_LHOST_IPV4_HOST_UNUSED}"
+export RHOST_IPV4_HOST_UNUSED="${OCTET_3_IPV4_UNUSED}.${OCTET_4_RHOST_IPV4_HOST_UNUSED}"

testcases/lib/test_net.sh export RHOST_IPV6_HOST="${RHOST_IPV6_HOST:-:1}" · line 553

Quoted 10 lines
testcases/lib/test_net.sh +10 -0
+export LHOST_IPV4_UNUSED="${IPV4_NET16_UNUSED}.$LHOST_IPV4_HOST_UNUSED"
+export RHOST_IPV4_UNUSED="${IPV4_NET16_UNUSED}.$RHOST_IPV4_HOST_UNUSED"
+
+export OCTET_3_IPV6_UNUSED="${OCTET_3_IPV6_UNUSED:-:0}"
+export OCTET_4_LHOST_IPV6_HOST_UNUSED=${OCTET_4_LHOST_IPV6_HOST_UNUSED:-2}
+export OCTET_4_RHOST_IPV6_HOST_UNUSED=${OCTET_4_RHOST_IPV6_HOST_UNUSED:-1}
+export LHOST_IPV6_HOST_UNUSED="${OCTET_3_IPV6_UNUSED}:${OCTET_4_LHOST_IPV6_HOST_UNUSED}"
+export RHOST_IPV6_HOST_UNUSED="${OCTET_3_IPV6_UNUSED}:${OCTET_4_RHOST_IPV6_HOST_UNUSED}"
+export LHOST_IPV6_UNUSED="${IPV6_NET32_UNUSED}:$LHOST_IPV6_HOST_UNUSED"
+export RHOST_IPV6_UNUSED="${IPV6_NET32_UNUSED}:$RHOST_IPV6_HOST_UNUSED"

Hmm, what is the purpose of adding these variables to the library? Why someone would need to redefine any of them?

Quoted 8 lines
Quoted 4 lines

I thought someone might appreciate ability to full control of IP
addresses, but you're right it's probably useless.
If you don't mind, I'll keep {L,R}HOST_IPV{4,6}_UNUSED variables as they are used in many scripts (DRY approach) and delete the rest of variables.

What about adding a new function similar to tst_ipaddr()? tst_ipaddr_un(), tst_ipaddr_new(), etc.? Probably we would need to add some optional counter parameter along with lhost/rhost...

How about this?

tst_ipaddr_un()

{
	local type="${1:-lhost}"
	local counter="${2:-1}"
	local host net max_counter max_net max_host tmp

	[ "$TST_IPV6" ] && max_net=65536 || max_net=256
	max_host=$((max_net - 2))
	max_counter=$((max_host * max_net / 2))
Quoted 5 lines
Quoted 3 lines

if [ $counter -gt $max_counter ]; then
tst_resm TCONF "'$counter' higher than max counter limit '$max_counter'"
fi

I guess we can safely omit this check.

OK.

Quoted 7 lines
Quoted 6 lines
	tmp=$((counter * 2))
	[ "$type" = "rhost" ] && tmp=$((tmp - 1))

	host=$((tmp % max_host))
	net=$((tmp / max_host))
	[ $host -eq 0 ] && { host=$max_host; net=$((net - 1)); }
Quoted 8 lines
Quoted 6 lines
	if [ "$TST_IPV6" ]; then
		echo "${IPV6_NET32_UNUSED}::$(printf %x:%x ${net} ${host})"
	else
		echo "${IPV4_NET16_UNUSED}.${net}.${host}"
	fi
}

Looks good to me.

Thanks. It will be all in v8. I'd like to send it ASAP as I'm on holiday since next week for two weeks.

Quoted 4 lines
Quoted 4 lines

And if we want to work with more than last octet, we might appreciate to be able to specify (optionally) third octet as well ($net variable).
Scripts which could benefit from it are at least:
if-{addr,route}-addlarge, route4-change-if.

Do we want to get rid of direct usage of IPV4_NET16_UNUSED and IPV6_NET32_UNUSED (at least in most of the scripts) and use tst_ipaddr_un()? I'm not sure if this is useful, but, as I wrote before, tests in if-{addr,route}-addlarge, route4-change-if require change in third octet.

So how about this (I also added security check, even it's close to impossible that will be needed):

# Get IP address of unused network

# tst_ipaddr_un [TYPE] [COUNTER] [OCTET_3]
# TYPE: { lhost | rhost }; Default value is 'lhost'.
# COUNTER: Integer value; Default is 1.
# OCTET_3: Specify third octet; Default is calculated from counter.
tst_ipaddr_un()
{
	local type="${1:-lhost}"
	local counter="${2:-1}"
	local octet_3="${3:-}"
	local host net max_net max_host tmp
	[ "$TST_IPV6" ] && max_net=65536 || max_net=256
	max_host=$((max_net - 2))

	tmp=$((counter * 2))
	[ "$type" = "rhost" ] && tmp=$((tmp - 1))

	host=$((tmp % max_host))
	net=$((tmp / max_host))
	[ $host -eq 0 ] && { host=$max_host; net=$((net - 1)); }

	[ -n "$octet_3" ] && net=$octet_3

	[ $host -gt $max_host ] && host=$max_host
	[ $net -gt $max_net ] && net=$max_net

	if [ "$TST_IPV6" ]; then
		echo "${IPV6_NET32_UNUSED}::$(printf %x:%x ${net} ${host})"
	else
		echo "${IPV4_NET16_UNUSED}.${net}.${host}"
	fi
}

Kind regards,
Petr

author
#10 lore raw

RFC PATCH v7 3/11 network/stress: Simplify make_background_tcp_traffic usage

In reply to Petr Vorel
---
 testcases/network/stress/interface/if-addr-adddel    | 6 +-----
 testcases/network/stress/interface/if-addr-addlarge  | 6 +-----
 testcases/network/stress/interface/if-mtu-change     | 7 ++-----
 testcases/network/stress/interface/if-route-adddel   | 7 ++-----
 testcases/network/stress/interface/if-route-addlarge | 6 +-----
 testcases/network/stress/ns-tools/test_net_stress.sh | 2 ++
 6 files changed, 9 insertions(+), 25 deletions(-)
testcases/network/stress/interface/if-addr-adddel +1 -5
index 42c2d5afa..3b1ab50bb 100644
@@ -53,10 +53,9 @@ test_body()
 		return
 	fi
 
-	make_background_tcp_traffic
-
 	local cnt=1
 	while [ $cnt -le $NS_TIMES ]; do
+		make_background_tcp_traffic
 
 		case $cmd_type in
 		if_cmd)
@@ -85,9 +84,6 @@ test_body()
 
 		cnt=$(($cnt + 1))
 
-		# Check the background TCP traffic
-		pgrep -x netstress > /dev/null || make_background_tcp_traffic
-
 		case $cmd_type in
 		if_cmd)
 			if [ "$TST_IPV6" ]; then
testcases/network/stress/interface/if-addr-addlarge +1 -5
index e667d9138..c3cdb8fa9 100644
@@ -47,8 +47,6 @@ test_body()
 		return
 	fi
 
-	make_background_tcp_traffic
-
 	local x=1
 	local y=1
 	local cnt=1
@@ -61,6 +59,7 @@ test_body()
 	fi
 
 	while [ $cnt -le $IP_TOTAL ]; do
+		make_background_tcp_traffic
 
 		if [ "$TST_IPV6" ]; then
 			local hex_x=$(printf '%x' $x)
@@ -95,9 +94,6 @@ test_body()
 
 		check_connectivity_interval $cnt || return
 
-		# Check the background TCP traffic
-		pgrep -x netstress > /dev/null || make_background_tcp_traffic
-
 		case $cmd_type in
 		if_cmd)
 			if [ "$TST_IPV6" ]; then
testcases/network/stress/interface/if-mtu-change +2 -5
index 5900f546a..62837ae74 100644
@@ -65,8 +65,6 @@ test_body()
 	tst_resm TINFO "'$cmd_name changes MTU $MTU_CHANGE_TIMES times" \
 	               "every $CHANGE_INTERVAL seconds"
 
-	make_background_tcp_traffic
-
 	mtu_array_len=$(echo $CHANGE_VALUES | wc -w)
 	local cnt=0
 	while [ $cnt -lt $MTU_CHANGE_TIMES ]; do
@@ -76,6 +74,8 @@ test_body()
 		mtu=$(echo $CHANGE_VALUES | cut -d ' ' -f $field)
 		[ $cnt -eq $MTU_CHANGE_TIMES ] && mtu="$saved_mtu"
 
+		make_background_tcp_traffic
+
 		tst_resm TINFO "set MTU to $mtu $cnt/$MTU_CHANGE_TIMES"
 		local ret=0
 		case $cmd_type in
@@ -95,9 +95,6 @@ test_body()
 		tst_sleep $CHANGE_INTERVAL
 
 		EXPECT_PASS tst_ping $(tst_ipaddr) $(tst_ipaddr rhost) "1 1000 65507"
-
-		# Check the background TCP traffic
-		pgrep -x netstress > /dev/null || make_background_tcp_traffic
 	done
 }
 
testcases/network/stress/interface/if-route-adddel +2 -5
index 1b6830433..2db74aaa2 100644
@@ -57,10 +57,10 @@ test_body()
 		return
 	fi
 
-	make_background_tcp_traffic
-
 	local cnt=1
 	while [ $cnt -le $NS_TIMES ]; do
+		make_background_tcp_traffic
+
 		case $cmd_type in
 		rt_cmd) route -A $inet add ${new_rt}${opt_rt} dev $iface ;;
 		ip_cmd) ip route add ${new_rt}${opt_rt} dev $iface ;;
@@ -81,9 +81,6 @@ test_body()
 
 		check_connectivity_interval $cnt || return
 
-		# Check the background TCP traffic
-		pgrep -x netstress > /dev/null || make_background_tcp_traffic
-
 		cnt=$(($cnt + 1))
 	done
 
testcases/network/stress/interface/if-route-addlarge +1 -5
index 3337b7225..b7e6952c2 100644
@@ -54,8 +54,6 @@ test_body()
 		return
 	fi
 
-	make_background_tcp_traffic
-
 	local x=1
 	local y=1
 	local cnt=1
@@ -68,6 +66,7 @@ test_body()
 	fi
 
 	while [ $cnt -le $ROUTE_TOTAL ]; do
+		make_background_tcp_traffic
 
 		if [ "$TST_IPV6" ]; then
 			local hex_x=$(printf '%x' $x)
@@ -88,9 +87,6 @@ test_body()
 
 		check_connectivity_interval $cnt || return
 
-		# Check the background TCP traffic
-		pgrep -x netstress > /dev/null || make_background_tcp_traffic
-
 		cnt=$(($cnt + 1))
 		y=$(($y + 1))
 		if [ $y -gt $xymax ]; then
testcases/network/stress/ns-tools/test_net_stress.sh +2 -0
index eef9f12f9..5ab3304db 100644
@@ -152,6 +152,8 @@ check_connectivity_interval()
 # IP: server IP. Default value is $(tst_ipaddr)
 make_background_tcp_traffic()
 {
+	pgrep -x netstress > /dev/null && return
+
 	local port=$(tst_get_unused_port ipv${ipver} stream)
 	local ip="${1:-$(tst_ipaddr)}"
 
Signature · 1 line
2.13.2
#11 lore raw

RFC PATCH v7 3/11 network/stress: Simplify make_background_tcp_traffic usage

Quoted 27 lines · Petr Vorel
---
 testcases/network/stress/interface/if-addr-adddel    | 6 +-----
 testcases/network/stress/interface/if-addr-addlarge  | 6 +-----
 testcases/network/stress/interface/if-mtu-change     | 7 ++-----
 testcases/network/stress/interface/if-route-adddel   | 7 ++-----
 testcases/network/stress/interface/if-route-addlarge | 6 +-----
 testcases/network/stress/ns-tools/test_net_stress.sh | 2 ++
 6 files changed, 9 insertions(+), 25 deletions(-)
testcases/network/stress/interface/if-addr-adddel +1 -2
index 42c2d5afa..3b1ab50bb 100644
@@ -53,10 +53,9 @@ test_body()
 		return
 	fi
 
-	make_background_tcp_traffic
-
 	local cnt=1
 	while [ $cnt -le $NS_TIMES ]; do
+		make_background_tcp_traffic
 
 		case $cmd_type in
 		if_cmd)

testcases/network/stress/interface/if-addr-adddel test_body() · line 89

Quoted 5 lines
testcases/network/stress/interface/if-addr-adddel +0 -2
@@ -85,9 +84,6 @@ test_body()
 
 		cnt=$(($cnt + 1))
 
-		# Check the background TCP traffic
-		pgrep -x netstress > /dev/null || make_background_tcp_traffic

Does 'make_background_tcp_traffic()' function check that background traffic is not running already?

Thanks,
Alexey
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20170724/84692954/attachment.html>

author
#12 lore raw

RFC PATCH v7 3/11 network/stress: Simplify make_background_tcp_traffic usage

Hi Alexey,

Quoted 10 lines
Quoted 9 lines
testcases/network/stress/interface/if-addr-adddel +1 -2
-	make_background_tcp_traffic
-
 	local cnt=1
 	while [ $cnt -le $NS_TIMES ]; do
+		make_background_tcp_traffic
 
 		case $cmd_type in
 		if_cmd)
@@ -85,9 +84,6 @@ test_body()
Quoted 8 lines

testcases/network/stress/interface/if-addr-adddel test_body() · line 89

Quoted 4 lines
testcases/network/stress/interface/if-addr-adddel +0 -2
 		cnt=$(($cnt + 1))
 
-		# Check the background TCP traffic
-		pgrep -x netstress > /dev/null || make_background_tcp_traffic

Does 'make_background_tcp_traffic()' function check that background traffic is not running already?

Sure, I just moved your pgrep check into function (it's in the same commit):
make_background_tcp_traffic()

{
	pgrep -x netstress > /dev/null && return

	local port=$(tst_get_unused_port ipv${ipver} stream)
	local ip="${1:-$(tst_ipaddr)}"

	netstress -R 3 -g $port > /dev/null 2>&1 &
	tst_rhost_run -s -c "netstress -l -H $ip -g $port"
}

Kind regards,
Petr

author
#13 lore raw

RFC PATCH v7 4/11 network/stress: Reduce the default number of cycles for various tests

In reply to Petr Vorel

Reducing value to one fifth resp. one tenth should be enough even for stress testing.

---
 testcases/lib/test_net.sh | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
testcases/lib/test_net.sh +5 -5
index 178df3527..76e6b0a41 100644
@@ -559,19 +559,19 @@ export FTP_UPLOAD_URLDIR="${FTP_UPLOAD_URLDIR:-pub}"
 
 # network/stress tests require additional parameters
 export NS_DURATION="${NS_DURATION:-3600}"
-export NS_TIMES="${NS_TIMES:-10000}"
+export NS_TIMES="${NS_TIMES:-2000}"
 export CONNECTION_TOTAL="${CONNECTION_TOTAL:-4000}"
-export IP_TOTAL="${IP_TOTAL:-10000}"
+export IP_TOTAL="${IP_TOTAL:-2000}"
 export IP_TOTAL_FOR_TCPIP="${IP_TOTAL_FOR_TCPIP:-100}"
-export ROUTE_TOTAL="${ROUTE_TOTAL:-10000}"
+export ROUTE_TOTAL="${ROUTE_TOTAL:-2000}"
 export MTU_CHANGE_TIMES="${MTU_CHANGE_TIMES:-1000}"
-export IF_UPDOWN_TIMES="${IF_UPDOWN_TIMES:-10000}"
+export IF_UPDOWN_TIMES="${IF_UPDOWN_TIMES:-2000}"
 export DOWNLOAD_BIGFILESIZE="${DOWNLOAD_BIGFILESIZE:-2147483647}"
 export DOWNLOAD_REGFILESIZE="${DOWNLOAD_REGFILESIZE:-1048576}"
 export UPLOAD_BIGFILESIZE="${UPLOAD_BIGFILESIZE:-2147483647}"
 export UPLOAD_REGFILESIZE="${UPLOAD_REGFILESIZE:-1024}"
 export MCASTNUM_NORMAL="${MCASTNUM_NORMAL:-20}"
-export MCASTNUM_HEAVY="${MCASTNUM_HEAVY:-40000}"
+export MCASTNUM_HEAVY="${MCASTNUM_HEAVY:-4000}"
 
 [ -n "$TST_USE_NETNS" -a "$TST_INIT_NETNS" != "no" ] && init_ltp_netspace
 
Signature · 1 line
2.13.2
author
#14 lore raw

RFC PATCH v7 5/11 lib/test_net.sh: tst_rhost_run: Add testcases/bin into PATH for SSH/RSH

In reply to Petr Vorel
---
 testcases/lib/test_net.sh | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
testcases/lib/test_net.sh +5 -5
index 76e6b0a41..0cafe1514 100644
@@ -55,7 +55,6 @@ init_ltp_netspace()
 # -b run in background
 # -s safe option, if something goes wrong, will exit with TBROK
 # -c specify command to run
-
 tst_rhost_run()
 {
 	local pre_cmd=
@@ -91,15 +90,16 @@ tst_rhost_run()
 
 	local output=
 	local ret=0
+	local newpath="$LTPROOT/testcases/bin"
 	if [ -n "${TST_USE_SSH:-}" ]; then
-		output=`ssh -n -q $user@$RHOST "sh -c \
-			'$pre_cmd $cmd $post_cmd'" $out 2>&1 || echo 'RTERR'`
+		output=`ssh -n -q $user@$RHOST "PATH=$newpath:\$PATH \
+			sh -c '$pre_cmd $cmd $post_cmd'" $out 2>&1 || echo 'RTERR'`
 	elif [ -n "$TST_USE_NETNS" ]; then
 		output=`$LTP_NETNS sh -c \
 			"$pre_cmd $cmd $post_cmd" $out 2>&1 || echo 'RTERR'`
 	else
-		output=`rsh -n -l $user $RHOST "sh -c \
-			'$pre_cmd $cmd $post_cmd'" $out 2>&1 || echo 'RTERR'`
+		output=`rsh -n -l $user $RHOST "PATH=$newpath:\$PATH \
+			sh -c '$pre_cmd $cmd $post_cmd'" $out 2>&1 || echo 'RTERR'`
 	fi
 	echo "$output" | grep -q 'RTERR$' && ret=1
 	if [ $ret -eq 1 ]; then
Signature · 1 line
2.13.2
#15 lore raw

RFC PATCH v7 5/11 lib/test_net.sh: tst_rhost_run: Add testcases/bin into PATH for SSH/RSH

Hi,

Quoted 18 lines · Petr Vorel
---
 testcases/lib/test_net.sh | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
testcases/lib/test_net.sh +0 -1
index 76e6b0a41..0cafe1514 100644
@@ -55,7 +55,6 @@ init_ltp_netspace()
 # -b run in background
 # -s safe option, if something goes wrong, will exit with TBROK
 # -c specify command to run
-
 tst_rhost_run()
 {
 	local pre_cmd=

testcases/lib/test_net.sh tst_rhost_run() · line 93

Quoted 4 lines
testcases/lib/test_net.sh +1 -0
@@ -91,15 +90,16 @@ tst_rhost_run()
 
 	local output=
 	local ret=0
+	local newpath="$LTPROOT/testcases/bin"

Why setting the PATH, LTPROOT can't be done before test run on remote host? what if LTPROOT differs b/w machines?

testcases/lib/test_net.sh tst_rhost_run() · line 96

Quoted 5 lines
testcases/lib/test_net.sh +2 -2
 	if [ -n "${TST_USE_SSH:-}" ]; then
-		output=`ssh -n -q $user@$RHOST "sh -c \
-			'$pre_cmd $cmd $post_cmd'" $out 2>&1 || echo 'RTERR'`
+		output=`ssh -n -q $user@$RHOST "PATH=$newpath:\$PATH \
+			sh -c '$pre_cmd $cmd $post_cmd'" $out 2>&1 || echo 'RTERR'`

$PATH still expands on local machine with backticks

echo echo "\$PATH" vs echo echo "\\$PATH"

or better use POSIX $(...)

echo $(echo "\$PATH")

Thanks,
Alexey

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20170725/0ce9f048/attachment-0001.html>

author
#16 lore raw

RFC PATCH v7 5/11 lib/test_net.sh: tst_rhost_run: Add testcases/bin into PATH for SSH/RSH

Hi Alexey,

Quoted 4 lines

testcases/lib/test_net.sh tst_rhost_run() · line 93

Quoted 1 line
testcases/lib/test_net.sh +1 -0
+	local newpath="$LTPROOT/testcases/bin"

Why setting the PATH, LTPROOT can't be done before test run on remote host? what if LTPROOT differs b/w machines?

You're right, that setting correct PATH is required by doc [1], so I'll drop this patch from v8.

But PATH is set in several local scripts, the same is for LTPROOT (see git grep LTPROOT=' and git grep PATH=.*LTPROOT.*testcases/bin'), including our testscripts/network.sh, so for localhost is not really required. I'd like to have it for remote host as well.

But of course the whole problem is how to detect correct LTPROOT for remote. I used the same as I had an idea, that ltp should be installed into same path for lhost and rhost, but I'm wrong.

BTW this is IMHO better way how to set LTPROOT:

# /opt/ltp/testscripts/network.sh
export LTPROOT="${LTPROOT:-$(cd $(dirname $0)/../testcases/bin; pwd)}"
# /opt/ltp/testcases/bin/test_net.sh
export LTPROOT="${LTPROOT:-$(cd $(dirname $0); pwd)}"
Quoted 8 lines

testcases/lib/test_net.sh tst_rhost_run() · line 96

Quoted 5 lines
testcases/lib/test_net.sh +2 -2
 	if [ -n "${TST_USE_SSH:-}" ]; then
-		output=`ssh -n -q $user@$RHOST "sh -c \
-			'$pre_cmd $cmd $post_cmd'" $out 2>&1 || echo 'RTERR'`
+		output=`ssh -n -q $user@$RHOST "PATH=$newpath:\$PATH \
+			sh -c '$pre_cmd $cmd $post_cmd'" $out 2>&1 || echo 'RTERR'`

$PATH still expands on local machine with backticks

Quoted 5 lines

echo echo "\$PATH" vs echo echo "\\$PATH"

or better use POSIX $(...)

echo $(echo "\$PATH")

Oh, you're right.

Kind regards,
Petr

[1]: https://github.com/linux-test-project/ltp/tree/master/testcases/network#ltp-setup

author
#17 lore raw

RFC PATCH v7 6/11 network/stress: Fix and cleanup part of multicast IPv4 tests

In reply to Petr Vorel

* Fix test for SSH and netns based testing.
* Remove hardcoded rsh dependency.
* Create shell library mcast4-lib.sh (multicast IPv4 specific) to reduce duplicity in tests. Library uses test_stress_net.sh (and therefore test_net.sh).
* Cleanup code + fixed typos.

---
 .../stress/multicast/grp-operation/mcast4-grpope01 | 221 ++-----------------
 .../stress/multicast/grp-operation/mcast4-grpope02 | 230 ++------------------
 .../stress/multicast/grp-operation/mcast4-grpope03 | 217 ++-----------------
 .../stress/multicast/grp-operation/mcast4-grpope04 | 236 ++-------------------
 .../stress/multicast/grp-operation/mcast4-lib.sh   | 129 +++++++++++
 5 files changed, 207 insertions(+), 826 deletions(-)
 create mode 100644 testcases/network/stress/multicast/grp-operation/mcast4-lib.sh
testcases/network/stress/multicast/grp-operation/mcast4-grpope01 +21 -200
index b03ff626b..086ecb1f9 100644
@@ -1,211 +1,32 @@
 #!/bin/sh
-
-################################################################################
-##                                                                            ##
-## Copyright (c) International Business Machines  Corp., 2006                 ##
-##                                                                            ##
-## This program is free software;  you can redistribute it and#or modify      ##
-## it under the terms of the GNU General Public License as published by       ##
-## the Free Software Foundation; either version 2 of the License, or          ##
-## (at your option) any later version.                                        ##
-##                                                                            ##
-## This program is distributed in the hope that it will be useful, but        ##
-## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
-## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
-## for more details.                                                          ##
-##                                                                            ##
-## You should have received a copy of the GNU General Public License          ##
-## along with this program;  if not, write to the Free Software               ##
-## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    ##
-##                                                                            ##
-##                                                                            ##
-################################################################################
-#
-# File:
-#   mcast4-grpope01
-#
-# Description:
-#   Verify that the kernel is not crashed when joining lots of IPv4 multicast
-#   groups on a single socket
-#
-# Setup:
-#   See ltp-yyyymmdd/testcases/network/stress/README
-#
-# Author:
-#   Mitsuru Chinen <[email protected]>
-#
-# History:
-#	May 1 2006 - Created (Mitsuru Chinen)
-#
-#-----------------------------------------------------------------------
-# Uncomment line below for debug output.
-#trace_logic=${trace_logic:-"set -x"}
-$trace_logic
-
-# The test case ID, the test case count and the total number of test case
-TCID=mcast4-grpope01
-TST_TOTAL=1
-TST_COUNT=1
-export TCID
-export TST_COUNT
-export TST_TOTAL
-
-# Make sure the value of LTPROOT
-LTPROOT=${LTPROOT:-`(cd ../../../../.. ; pwd)`}
-export LTPROOT
-
-# Check the environmanet variable
-. check_envval || exit $TST_TOTAL
-
-# Number of the group
-MCASTNUM_HEAVY=${MCASTNUM_HEAVY:-40000}
-
-# The number of the test link where tests run
-LINK_NUM=${LINK_NUM:-0}
-
-# Network portion of the IPv4 address
-NETWORK_PART=${IPV4_NETWORK:-"10.0.0"}
-
-# Netmask of the IPv4 network
-NETWORK_MASK=24
-
-# Host portion of the IPv4 address
-LHOST_HOST_PART=${LHOST_IPV4_HOST:-"2"}     # local host
-RHOST_HOST_PART=${RHOST_IPV4_HOST:-"1"}     # remote host
-
-# Prefix of the Multicast Address
-MCAST_ADDR_PREFIX=224.10
-
-
-#-----------------------------------------------------------------------
+# Copyright (c) International Business Machines  Corp., 2006
+# Copyright (c) 2017 Petr Vorel <[email protected]>
 #
-# Function: do_cleanup
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 2 of
+# the License, or (at your option) any later version.
 #
-# Description:
-#   Recover the system configuration
-#
-#-----------------------------------------------------------------------
-do_cleanup()
-{
-    # Make sure to kill the multicast receiver and sender
-    killall -SIGHUP ns-mcast_join >/dev/null 2>&1
-    $LTP_RSH $RHOST killall -SIGHUP ns-igmp_querier >/dev/null 2>&1
-
-    # Clean up each interface
-    initialize_if lhost ${LINK_NUM}
-    initialize_if rhost ${LINK_NUM}
-}
-
-
-#-----------------------------------------------------------------------
+# This program is distributed in the hope that it would be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
 #
-# Function: do_setup
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
 #
 # Description:
-#   Configure the ssystem for the test
-#
-#-----------------------------------------------------------------------
-do_setup()
-{
-    # Initialize the system configuration
-    do_cleanup
-
-    # Call do_cleanup function before exit
-    trap do_cleanup 0
-
-    # name of interface of the local/remote host
-    lhost_ifname=`get_ifname lhost $LINK_NUM`
-    if [ $? -ne 0 ]; then
-	tst_resm TBROK "Failed to get the interface name at the local host"
-	exit $TST_TOTAL
-    fi
-
-    rhost_ifname=`get_ifname rhost $LINK_NUM`
-    if [ $? -ne 0 ]; then
-	tst_resm TBROK "Failed to get the interface name at the remote host"
-	exit $TST_TOTAL
-    fi
-
-    # Set IPv4 addresses to the interfaces
-    set_ipv4addr lhost $LINK_NUM $NETWORK_PART $LHOST_HOST_PART
-    if [ $? -ne 0 ]; then
-	tst_resm TBROK "Failed to add any IP address at the local host"
-	exit 1
-    fi
-
-    set_ipv4addr rhost $LINK_NUM $NETWORK_PART $RHOST_HOST_PART
-    if [ $? -ne 0 ]; then
-	tst_resm TBROK "Failed to add any IP address at the remote host"
-	exit 1
-    fi
-
-    # IPv4 address of the local/remote host
-    lhost_addr="${NETWORK_PART}.${LHOST_HOST_PART}"
-    rhost_addr="${NETWORK_PART}.${RHOST_HOST_PART}"
-
-    # Make sure the connectvity
-    check_icmpv4_connectivity $lhost_ifname $rhost_addr
-    if [ $? -ne 0 ]; then
-	tst_resm TBROK "There is no IPv4 connectivity."
-	exit 1
-    fi
-
-    # Make sure the sysctl values
-    sysctl -w net.ipv4.igmp_max_memberships=$MCASTNUM_HEAVY >/dev/null
-    if [ $? -ne 0 ]; then
-	tst_resm TBROK "Failed to set the sysctl value regarding multicast"
-	exit $TST_TOTAL
-    fi
-
-    sysctl -w net.ipv4.igmp_max_msf=10 >/dev/null
-    sysctl -w net.ipv4.conf.${lhost_ifname}.force_igmp_version=0 >/dev/null
-    sysctl -w net.ipv4.conf.all.force_igmp_version=0 >/dev/null
-}
-
-
-#-----------------------------------------------------------------------
-#
-# Main
-#
-#
-
-# Test description
-tst_resm TINFO "Verify that the kernel is not crashed when joining $MCASTNUM_HEAVY IPv4 multicast groups on a single socket"
-
-do_setup
-
-# Run a multicast join tool
-tmpfile=$TMPDIR/ns-mcast_join.$$
-ns-mcast_join -f 4 -I $lhost_ifname -n $MCASTNUM_HEAVY -p $MCAST_ADDR_PREFIX > $tmpfile
-if [ $? -ne 0 ]; then
-    tst_resm TBROK "Failed to start multicast joining tool Please check the environment"
-    rm -f $tmpfile
-    exit 1
-fi
-msg=`cat $tmpfile | grep groups`
-tst_resm TINFO "Joined $msg"
-rm -f $tmpfile
-
-# Send IGMP General Query from the remote host
-ret=`$LTP_RSH $RHOST ${LTPROOT}/testcases/bin/ns-igmp_querier -I $rhost_ifname -o -r 1' ; echo $?'`
-if [ $ret -ne 0 ]; then
-    tst_resm TBROK "Failed to start IGMP querier"
-    exit 1
-fi
-
-#-----------------------------------------------------------------------
+# Verify that the kernel is not crashed when joining lots of IPv4 multicast
+# groups on a single socket.
 #
-# Clean up
+# Setup: testcases/network/stress/README
 #
+# Author: Mitsuru Chinen <[email protected]>
 
-killall -SIGHUP ns-mcast_join >/dev/null 2>&1
-while true ; do
-    ps auxw | grep -v grep | grep ns-mcast_join > /dev/null
-    if [ $? -ne 0 ] ; then
-  	break
-    fi
-done
+. mcast4-lib.sh
 
-tst_resm TPASS "Test is finished successfully."
+tst_resm TINFO "joining $MCASTNUM_HEAVY IPv4 multicast groups on a single socket"
 
-exit 0
+mcast_setup $MCASTNUM_HEAVY
+do_multicast_test_multiple_join $MCASTNUM_HEAVY
+tst_exit
testcases/network/stress/multicast/grp-operation/mcast4-grpope02 +19 -211

239 lines not on this page · raw message

testcases/network/stress/multicast/grp-operation/mcast4-grpope03 +19 -198

226 lines not on this page · raw message

testcases/network/stress/multicast/grp-operation/mcast4-grpope04 +19 -217

245 lines not on this page · raw message

testcases/network/stress/multicast/grp-operation/mcast4-lib.sh new file +129 -0

129 lines not on this page · raw message

Signature · 1 line
2.13.2
#18 lore raw

RFC PATCH v7 6/11 network/stress: Fix and cleanup part of multicast IPv4 tests

Quoted 17 lines · Petr Vorel

* Fix test for SSH and netns based testing.
* Remove hardcoded rsh dependency.
* Create shell library mcast4-lib.sh (multicast IPv4 specific) to reduce duplicity in tests. Library uses test_stress_net.sh (and therefore test_net.sh).
* Cleanup code + fixed typos.

---
 .../stress/multicast/grp-operation/mcast4-grpope01 | 221 ++-----------------
 .../stress/multicast/grp-operation/mcast4-grpope02 | 230 ++------------------
 .../stress/multicast/grp-operation/mcast4-grpope03 | 217 ++-----------------
 .../stress/multicast/grp-operation/mcast4-grpope04 | 236 ++-------------------
 .../stress/multicast/grp-operation/mcast4-lib.sh   | 129 +++++++++++
 5 files changed, 207 insertions(+), 826 deletions(-)
 create mode 100644 testcases/network/stress/multicast/grp-operation/mcast4-lib.sh

...

Quoted 24 lines
testcases/network/stress/multicast/grp-operation/mcast4-lib.sh new file +24 -0
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+# Author: Petr Vorel <[email protected]>
+#
+# Setup script for mcast4-grpope* tests.
+
+TST_TOTAL=1
+TST_CLEANUP="mcast_cleanup"
+
+. test_net_stress.sh
+
+mcast_setup()
+{
+	local igmp_max_memberships="$1"
+	local increase_max="${2:-}"
+	local iface=$(tst_iface)
+
+	netstress_setup
+	tst_tmpdir
+
+	SYSFS_IGMP_MAX_MEMBERSHIPS=$(sysctl -b net.ipv4.igmp_max_memberships)
+	SYSFS_IGMP_MAX_MSF=$(sysctl -b net.ipv4.igmp_max_msf)

testcases/network/stress/multicast/grp-operation/mcast4-lib.sh line 47

Quoted 10 lines
testcases/network/stress/multicast/grp-operation/mcast4-lib.sh new file +10 -0
+	SYSFS_FORCE_IGMP_VERSION=$(sysctl -b net.ipv4.conf.${iface}.force_igmp_version)
+	SYSFS_ALL_FORCE_IGMP_VERSION=$(sysctl -b net.ipv4.conf.all.force_igmp_version)
+
+	if [ "$increase_max" ]; then
+		# Increase the maximum number of open file descriptors
+		[ `ulimit -n` -lt $igmp_max_memberships ] && ROD ulimit -n $igmp_max_memberships
+	fi
+
+	tst_add_ipaddr_stress
+	tst_add_ipaddr_stress rhost

This is very strange, the test adds a new pair of addresses and then runs ping in setup. After that they not used...

By the way, we already have IP addresses defined by IPV4_HOST variables, better use them if needed.

testcases/network/stress/multicast/grp-operation/mcast4-lib.sh line 48

Quoted 1 line
testcases/network/stress/multicast/grp-operation/mcast4-lib.sh new file +1 -0
+	check_connectivity $iface $RHOST_IPV4_UNUSED

No need for this test.

Quoted 21 lines
testcases/network/stress/multicast/grp-operation/mcast4-lib.sh new file +21 -0
+
+	ROD sysctl -qw net.ipv4.igmp_max_memberships=$igmp_max_memberships
+	ROD sysctl -qw net.ipv4.igmp_max_msf=10
+	ROD sysctl -qw net.ipv4.conf.${iface}.force_igmp_version=0
+	ROD sysctl -qw net.ipv4.conf.all.force_igmp_version=0
+}
+
+mcast_cleanup()
+{
+	local iface=$(tst_iface)
+
+	[ -n "$SYSFS_IGMP_MAX_MEMBERSHIPS" ] && sysctl -qw net.ipv4.igmp_max_memberships=$SYSFS_IGMP_MAX_MEMBERSHIPS
+	[ -n "$SYSFS_IGMP_MAX_MSF" ] && sysctl -qw net.ipv4.igmp_max_msf=$SYSFS_IGMP_MAX_MSF
+	[ -n "$SYSFS_FORCE_IGMP_VERSION" ] && sysctl -qw net.ipv4.conf.${iface}.force_igmp_version=$SYSFS_FORCE_IGMP_VERSION
+	[ -n "$SYSFS_ALL_FORCE_IGMP_VERSION" ] && sysctl -qw net.ipv4.conf.all.force_igmp_version=$SYSFS_ALL_FORCE_IGMP_VERSION
+
+	pkill -SIGHUP -x ns-mcast_join
+	tst_sleep 10ms
+	pkill -9 -x ns-mcast_join
+
+	tst_rhost_run -c "pkill -SIGHUP -x ns-igmp_querier"

testcases/network/stress/multicast/grp-operation/mcast4-lib.sh line 79

Quoted 10 lines
testcases/network/stress/multicast/grp-operation/mcast4-lib.sh new file +10 -0
+
+	restore_ipaddr
+	restore_ipaddr rhost
+}
+
+do_multicast_test_multiple_join()
+{
+	local igmp_max_memberships="$1"
+	local iface=$(tst_iface)
+	local riface=$(tst_iface rhost)

I would rather use those functions inline.

testcases/network/stress/multicast/grp-operation/mcast4-lib.sh line 87

Quoted 8 lines
testcases/network/stress/multicast/grp-operation/mcast4-lib.sh new file +8 -0
+	local param_multi_socket ret tmpfile
+
+	[ "${2:-}" = true ] && param_multi_socket="-m"
+
+	# Run a multicast join tool
+	tmpfile=ns-mcast_join.$$
+
+	EXPECT_PASS ns-mcast_join $param_multi_socket -f 4 -I $iface -n $igmp_max_memberships -p $MCAST_IPV4_ADDR_PREFIX > $tmpfile

EXPECT_PASS ... \> $tmpfile

testcases/network/stress/multicast/grp-operation/mcast4-lib.sh line 92

Quoted 5 lines
testcases/network/stress/multicast/grp-operation/mcast4-lib.sh new file +5 -0
+
+	tst_resm TINFO "joined $(grep groups $tmpfile)"
+
+	# Send IGMP General Query from the remote host
+	EXPECT_RHOST_PASS ns-igmp_querier -I $riface -o -r 1

We need to use the address defined with the prefix $MCAST_IPV4_ADDR_PREFIX above otherwise we won't get a reply, i.e. "-m $MCAST_IPV4_ADDR"

Quoted 20 lines
testcases/network/stress/multicast/grp-operation/mcast4-lib.sh new file +20 -0
+}
+
+do_multicast_test_join_leave()
+{
+	local iface=$(tst_iface)
+	local riface=$(tst_iface rhost)
+	local define_source_address filter num ret source_addr x y
+
+	[ "$1" = true ] && define_source_address=true
+
+	# Send IGMP General Query from the remote host
+	tst_rhost_run -s -c "ns-igmp_querier -I $riface -i 1000000000 -r 1 -b"
+
+	# Run a multicast join tool
+	num=0
+	while [ $num -lt $MCASTNUM_NORMAL ]; do
+		if [ "$define_source_address" ]; then
+			x=$(($num / 254))
+			y=$(($num % 254 + 1))
+			if [ $x -gt 254 ]; then

testcases/network/stress/multicast/grp-operation/mcast4-lib.sh line 122

Quoted 10 lines
testcases/network/stress/multicast/grp-operation/mcast4-lib.sh new file +10 -0
+				tst_resm TINFO "the number of the connection is less than $num"
+				break
+			fi
+			source_addr="${IPV4_NET16_UNUSED}.${x}.${y}"
+
+			[ $(($num % 5)) -ne 2 ] && filter="include" || filter="exclude"
+			params="-F $filter -s $source_addr"
+		fi
+
+		ROD ns-mcast_join -f 4 -I $iface -l $NS_TIMES -a $MCAST_IPV4_ADDR $params &

just ns-mcast_join ... &

testcases/network/stress/multicast/grp-operation/mcast4-lib.sh line 129

Quoted 7 lines
testcases/network/stress/multicast/grp-operation/mcast4-lib.sh new file +7 -0
+		num=$(($num + 1))
+	done
+
+	wait
+
+	tst_resm TPASS "test is finished successfully"
+}

Thanks,
Alexey

author
#19 lore raw

RFC PATCH v7 6/11 network/stress: Fix and cleanup part of multicast IPv4 tests

Hi Alexey,

thank you for your review.

Quoted 7 lines

testcases/network/stress/multicast/grp-operation/mcast4-lib.sh line 47

Quoted 7 lines
testcases/network/stress/multicast/grp-operation/mcast4-lib.sh new file +7 -0
+	if [ "$increase_max" ]; then
+		# Increase the maximum number of open file descriptors
+		[ `ulimit -n` -lt $igmp_max_memberships ] && ROD ulimit -n $igmp_max_memberships
+	fi
+
+	tst_add_ipaddr_stress
+	tst_add_ipaddr_stress rhost

This is very strange, the test adds a new pair of addresses and then

Quoted 4 lines

runs ping in setup. After that they not used...

By the way, we already have IP addresses defined by IPV4_HOST variables, better use them if needed.

I thought that it'd be more secure to use "unused network" also for multicast. OK, I'll use regular IP addresses.
BTW would you use "unused network"? with tests in testcases/network/stress/ than route and interface?

Quoted 3 lines

testcases/network/stress/multicast/grp-operation/mcast4-lib.sh line 48

Quoted 1 line
testcases/network/stress/multicast/grp-operation/mcast4-lib.sh new file +1 -0
+	check_connectivity $iface $RHOST_IPV4_UNUSED

No need for this test.

OK (copied from your interface tests).

...

Quoted 3 lines
Quoted 3 lines
testcases/network/stress/multicast/grp-operation/mcast4-lib.sh new file +3 -0
+mcast_cleanup()
+{
+	local iface=$(tst_iface)

...

Quoted 7 lines

testcases/network/stress/multicast/grp-operation/mcast4-lib.sh line 79

Quoted 5 lines
testcases/network/stress/multicast/grp-operation/mcast4-lib.sh new file +5 -0
+do_multicast_test_multiple_join()
+{
+	local igmp_max_memberships="$1"
+	local iface=$(tst_iface)
+	local riface=$(tst_iface rhost)

I would rather use those functions inline.

OK.

Quoted 10 lines

testcases/network/stress/multicast/grp-operation/mcast4-lib.sh line 87

Quoted 8 lines
testcases/network/stress/multicast/grp-operation/mcast4-lib.sh new file +8 -0
+	local param_multi_socket ret tmpfile
+
+	[ "${2:-}" = true ] && param_multi_socket="-m"
+
+	# Run a multicast join tool
+	tmpfile=ns-mcast_join.$$
+
+	EXPECT_PASS ns-mcast_join $param_multi_socket -f 4 -I $iface -n $igmp_max_memberships -p $MCAST_IPV4_ADDR_PREFIX > $tmpfile

EXPECT_PASS ... \> $tmpfile

Eh, silly mistake.

Quoted 9 lines

testcases/network/stress/multicast/grp-operation/mcast4-lib.sh line 92

Quoted 5 lines
testcases/network/stress/multicast/grp-operation/mcast4-lib.sh new file +5 -0
+
+	tst_resm TINFO "joined $(grep groups $tmpfile)"
+
+	# Send IGMP General Query from the remote host
+	EXPECT_RHOST_PASS ns-igmp_querier -I $riface -o -r 1

We need to use the address defined with the prefix $MCAST_IPV4_ADDR_PREFIX above otherwise we won't get a reply, i.e. "-m $MCAST_IPV4_ADDR"

Right, thanks. It was broken in original code. Trying to read ns-mcast_join.c and ns-igmp_querier.c, but still not sure I fully understand tham. Hope the other flags are correct (e.g. not missing MCAST_IPV4_ADDR_PREFIX resp. MCAST_IPV4_ADDR).

Quoted 25 lines
Quoted 25 lines
testcases/network/stress/multicast/grp-operation/mcast4-lib.sh new file +25 -0
+}
+
+do_multicast_test_join_leave()
+{
+	local iface=$(tst_iface)
+	local riface=$(tst_iface rhost)
+	local define_source_address filter num ret source_addr x y
+
+	[ "$1" = true ] && define_source_address=true
+
+	# Send IGMP General Query from the remote host
+	tst_rhost_run -s -c "ns-igmp_querier -I $riface -i 1000000000 -r 1 -b"
+
+	# Run a multicast join tool
+	num=0
+	while [ $num -lt $MCASTNUM_NORMAL ]; do
+		if [ "$define_source_address" ]; then
+			x=$(($num / 254))
+			y=$(($num % 254 + 1))
+			if [ $x -gt 254 ]; then
+				tst_resm TINFO "the number of the connection is less than $num"
+				break
+			fi
+			source_addr="${IPV4_NET16_UNUSED}.${x}.${y}"
+
Quoted 8 lines

testcases/network/stress/multicast/grp-operation/mcast4-lib.sh line 122

Quoted 5 lines
testcases/network/stress/multicast/grp-operation/mcast4-lib.sh new file +5 -0
+			[ $(($num % 5)) -ne 2 ] && filter="include" || filter="exclude"
+			params="-F $filter -s $source_addr"
+		fi
+
+		ROD ns-mcast_join -f 4 -I $iface -l $NS_TIMES -a $MCAST_IPV4_ADDR $params &

just ns-mcast_join ... &

OK

Kind regards,
Petr

author
#20 lore raw

RFC PATCH v7 7/11 lib/test_net.sh: Add function reset_ltp_netspace()

In reply to Petr Vorel

This function is useful when we know we broke netns (e.g. by unloading network driver).

---
 testcases/lib/test_net.sh | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
testcases/lib/test_net.sh +13 -1
index 0cafe1514..cecb0bab2 100644
@@ -42,7 +42,7 @@ init_ltp_netspace()
 	export TST_INIT_NETNS="no"
 
 	pid="$(echo $(readlink /var/run/netns/ltp_ns) | cut -f3 -d'/')"
-	export LTP_NETNS="${LTP_NETNS:-ns_exec $pid net,mnt}"
+	export LTP_NETNS="ns_exec $pid net,mnt"
 
 	tst_restore_ipaddr
 	tst_restore_ipaddr rhost
@@ -50,6 +50,18 @@ init_ltp_netspace()
 	tst_wait_ipv6_dad
 }
 
+# Force to reset netns.
+reset_ltp_netspace()
+{
+	[ -n "$TST_USE_NETNS" ] || return
+
+	tst_resm TINFO "reset netns"
+	ip link delete ltp_ns_veth2 2> /dev/null
+	rm -f /var/run/netns/ltp_ns
+	pkill ns_create
+	init_ltp_netspace
+}
+
 # Run command on remote host.
 # Options:
 # -b run in background
Signature · 1 line
2.13.2
#21 lore raw

RFC PATCH v7 7/11 lib/test_net.sh: Add function reset_ltp_netspace()

Hi,

Quoted 13 lines · Petr Vorel

This function is useful when we know we broke netns (e.g. by unloading network driver).

---
 testcases/lib/test_net.sh | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
testcases/lib/test_net.sh +0 -0
index 0cafe1514..cecb0bab2 100644

testcases/lib/test_net.sh init_ltp_netspace() · line 45

Quoted 5 lines
testcases/lib/test_net.sh +1 -1
@@ -42,7 +42,7 @@ init_ltp_netspace()
 	export TST_INIT_NETNS="no"
 
 	pid="$(echo $(readlink /var/run/netns/ltp_ns) | cut -f3 -d'/')"
-	export LTP_NETNS="${LTP_NETNS:-ns_exec $pid net,mnt}"
+	export LTP_NETNS="ns_exec $pid net,mnt"

It is better to reset LTP_NETNS variable before calling init_ltp_netspace in reset_ltp_netspace().

Thanks,
Alexey

Quoted 12 lines
testcases/lib/test_net.sh +5 -0
 	tst_restore_ipaddr
 	tst_restore_ipaddr rhost
@@ -50,6 +50,18 @@ init_ltp_netspace()
 	tst_wait_ipv6_dad
 }
 
+# Force to reset netns.
+reset_ltp_netspace()
+{
+	[ -n "$TST_USE_NETNS" ] || return
+

testcases/lib/test_net.sh init_ltp_netspace() · line 67

Quoted 10 lines
testcases/lib/test_net.sh +7 -0
+	tst_resm TINFO "reset netns"
+	ip link delete ltp_ns_veth2 2> /dev/null
+	rm -f /var/run/netns/ltp_ns
+	pkill ns_create
+	init_ltp_netspace
+}
+
 # Run command on remote host.
 # Options:
 # -b run in background

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20170726/7e04e5c7/attachment.html>

author
#22 lore raw

RFC PATCH v7 7/11 lib/test_net.sh: Add function reset_ltp_netspace()

Hi Alexey,

Quoted 6 lines

testcases/lib/test_net.sh init_ltp_netspace() · line 45

Quoted 3 lines
testcases/lib/test_net.sh +1 -1
 	pid="$(echo $(readlink /var/run/netns/ltp_ns) | cut -f3 -d'/')"
-	export LTP_NETNS="${LTP_NETNS:-ns_exec $pid net,mnt}"
+	export LTP_NETNS="ns_exec $pid net,mnt"

It is better to reset LTP_NETNS variable before calling init_ltp_netspace in reset_ltp_netspace().

OK, I'll do it :-). I wonder what is a real usage for LTP_NETNS configured by user...

Thanks for your review!

Kind regards,
Petr

#23 lore raw

RFC PATCH v7 7/11 lib/test_net.sh: Add function reset_ltp_netspace()

Quoted 8 lines · Petr Vorel

Hi Alexey,

Quoted 5 lines

testcases/lib/test_net.sh init_ltp_netspace() · line 45

Quoted 3 lines
testcases/lib/test_net.sh +1 -1
 	pid="$(echo $(readlink /var/run/netns/ltp_ns) | cut -f3 -d'/')"
-	export LTP_NETNS="${LTP_NETNS:-ns_exec $pid net,mnt}"
+	export LTP_NETNS="ns_exec $pid net,mnt"

It is better to reset LTP_NETNS variable before calling init_ltp_netspace in reset_ltp_netspace().

OK, I'll do it :-). I wonder what is a real usage for LTP_NETNS configured by user...

e.g. to use 'ip' command instead.

Thanks,
Alexey

Quoted 5 lines

Thanks for your review!

Kind regards,
Petr

author
#24 lore raw

RFC PATCH v7 8/11 network/stress: Fix and cleanup route IPv4 tests

In reply to Petr Vorel

* Fix test for netns based testing (route4-change-if requires to have manually set another pair of interfaces).
* Remove rsh dependency.
* Create shell library route4-lib.sh (route IPv4 specific) to reduce duplicity in tests. Library uses test_stress_net.sh (and therefore test_net.sh).
* Stop using TST_COUNT, simplify TCID
* Cleanup code, fix typos, update doc.

---
 testcases/network/stress/ns-tools/initialize_if    |   3 -
 .../network/stress/ns-tools/test_net_stress.sh     |   9 +-
 testcases/network/stress/route/00_Descriptions.txt |  23 +-
 testcases/network/stress/route/route4-change-dst   | 283 ++---------------
 testcases/network/stress/route/route4-change-gw    | 302 ++----------------
 testcases/network/stress/route/route4-change-if    | 336 +++------------------
 testcases/network/stress/route/route4-ifdown       | 283 ++++-------------
 testcases/network/stress/route/route4-lib.sh       | 206 +++++++++++++
 testcases/network/stress/route/route4-redirect     | 220 +++-----------
 testcases/network/stress/route/route4-rmmod        | 314 +++++--------------
 testcases/network/stress/route/route6-change-dst   |   2 +-
 11 files changed, 480 insertions(+), 1501 deletions(-)
 create mode 100644 testcases/network/stress/route/route4-lib.sh
testcases/network/stress/ns-tools/initialize_if +0 -3
index d64203e4c..23bf68bc0 100644
@@ -69,9 +69,6 @@ fi
 # Define the interface name
 ifname=`get_ifname $host_type $link_num` || exit 1
 
-# Initialize the specified interface
-command="ifconfig $ifname down mtu 1500 ; ip route flush dev $ifname ; ip addr flush dev $ifname ; ifconfig $ifname up"
-
 if [ $host_type = lhost ]; then
     ( ifconfig $ifname down && \
     ip link set mtu 1500 dev $ifname && \
testcases/network/stress/ns-tools/test_net_stress.sh +4 -5
index 5ab3304db..d112286bd 100644
@@ -31,7 +31,6 @@ export TCID="${TCID:-$(basename $0)}"
 # Netmask of for the tested network
 IPV4_NETMASK="255.255.255.0"
 IPV4_NETMASK_NUM=24
-IPV4_BROADCAST=${IPV4_NET16_UNUSED}.0.255
 
 # Prefix of the Multicast Address
 MCAST_IPV4_ADDR_PREFIX="224.10"
@@ -64,16 +63,16 @@ netstress_cleanup()
 }
 
 # Set an IPv4 address on local/remote interface.
-# tst_add_ipaddr_stress [TYPE] [LINK] [IP]
+# tst_add_ipaddr_stress [TYPE] [IP] [LINK]
 # TYPE: { lhost | rhost }; Default value is 'lhost'.
-# LINK: link number starting from 0. Default value is '0'.
 # IP: IP address to be set; Default value is $LHOST_IPV4_UNUSED (for lhost)
 # or $RHOST_IPV4_UNUSED (for rhost).
+# LINK: link number starting from 0. Default value is '0'.
 tst_add_ipaddr_stress()
 {
 	local type="${1:-lhost}"
-	local link_num="${2:-0}"
-	local ip="${3:-}"
+	local ip="${2:-}"
+	local link_num="${3:-0}"
 	local mask=$IPV4_NETMASK_NUM
 	local iface=$(tst_iface $type $link_num)
 
testcases/network/stress/route/00_Descriptions.txt +11 -12
index 9bcbc5f05..3d3d26999 100644
@@ -1,48 +1,47 @@
-route4-change-dst01
+route4-change-dst 1
 	Verify the kernel is not crashed when the destination of an IPv4 route
 	is changed frequently by route command
 
-route4-change-dst02
+route4-change-dst 2
 	Verify the kernel is not crashed when the destination of an IPv4 route
 	is changed frequently by ip command
 
-route4-change-gw01
+route4-change-gw 1
 	Verify the kernel is not crashed when the gateway of an IPv4 route is
 	changed frequently by route command
 
-route4-change-gw02
+route4-change-gw 2
 	Verify the kernel is not crashed when the gateway of an IPv4 route is
 	changed frequently by ip command
 
-route4-change-if01
+route4-change-if 1
 	Verify the kernel is not crashed when the interface of an IPv4 route is
 	changed frequently by route command
 
-route4-change-if02
+route4-change-if 2
 	Verify the kernel is not crashed when the interface of an IPv4 route is
 	changed frequently by ip command
 
-route4-redirect01
+route4-redirect 1
 	Verify the kernel is not crashed when the IPv4 route is modified by
 	ICMP Redirects frequently
 
-route4-ifdown01
+route4-ifdown 1
 	Verify the kernel is not crashed when IPv4 route is add by route command
 	then it is deleted by the interface down with ifconfig command
 
-route4-ifdown02
+route4-ifdown 2
 	Verify the kernel is not crashed when IPv4 route is add by ip command
 	then it is deleted by the interface down with ip command
 
-route4-rmmod01
+route4-rmmod 1
 	Verify the kernel is not crashed when IPv4 route is add by route command
 	then it is deleted by the removing network driver
 
-route4-rmmod02
+route4-rmmod 2
 	Verify the kernel is not crashed when IPv4 route is add by ip command
 	then it is deleted by the removing network driver
 
-
 route6-change-dst01
 	Verify the kernel is not crashed when the destination of an IPv6 route
 	is changed frequently by route command
testcases/network/stress/route/route4-change-dst +25 -258

301 lines not on this page · raw message

testcases/network/stress/route/route4-change-gw +31 -271

323 lines not on this page · raw message

testcases/network/stress/route/route4-change-if +42 -294

366 lines not on this page · raw message

testcases/network/stress/route/route4-ifdown +51 -232

312 lines not on this page · raw message

testcases/network/stress/route/route4-lib.sh new file +206 -0

206 lines not on this page · raw message

testcases/network/stress/route/route4-redirect +40 -180

252 lines not on this page · raw message

testcases/network/stress/route/route4-rmmod +69 -245

349 lines not on this page · raw message

testcases/network/stress/route/route6-change-dst +1 -1
index 33e89e985..649f0cc00 100644
@@ -242,7 +242,7 @@ test_body()
 	    ;;
 	esac
 	if [ $? -ne 0 ]; then
-	    tst_resm TFAIL "Cannot delte the route to ${ADDDEL_ROUTE}"
+	    tst_resm TFAIL "Cannot delete the route to ${ADDDEL_ROUTE}"
 	    return 1
 	fi
 
Signature · 1 line
2.13.2
#25 lore raw

RFC PATCH v7 8/11 network/stress: Fix and cleanup route IPv4 tests

Quoted 29 lines · Petr Vorel

* Fix test for netns based testing (route4-change-if requires to have manually set another pair of interfaces).
* Remove rsh dependency.
* Create shell library route4-lib.sh (route IPv4 specific) to reduce duplicity in tests. Library uses test_stress_net.sh (and therefore test_net.sh).
* Stop using TST_COUNT, simplify TCID
* Cleanup code, fix typos, update doc.

---
 testcases/network/stress/ns-tools/initialize_if    |   3 -
 .../network/stress/ns-tools/test_net_stress.sh     |   9 +-
 testcases/network/stress/route/00_Descriptions.txt |  23 +-
 testcases/network/stress/route/route4-change-dst   | 283 ++---------------
 testcases/network/stress/route/route4-change-gw    | 302 ++----------------
 testcases/network/stress/route/route4-change-if    | 336 +++------------------
 testcases/network/stress/route/route4-ifdown       | 283 ++++-------------
 testcases/network/stress/route/route4-lib.sh       | 206 +++++++++++++
 testcases/network/stress/route/route4-redirect     | 220 +++-----------
 testcases/network/stress/route/route4-rmmod        | 314 +++++--------------
 testcases/network/stress/route/route6-change-dst   |   2 +-
 11 files changed, 480 insertions(+), 1501 deletions(-)
 create mode 100644 testcases/network/stress/route/route4-lib.sh
testcases/network/stress/ns-tools/initialize_if +0 -0
index d64203e4c..23bf68bc0 100644

testcases/network/stress/ns-tools/initialize_if fi · line 74

Quoted 6 lines
testcases/network/stress/ns-tools/initialize_if +0 -3
@@ -69,9 +69,6 @@ fi
 # Define the interface name
 ifname=`get_ifname $host_type $link_num` || exit 1
 
-# Initialize the specified interface
-command="ifconfig $ifname down mtu 1500 ; ip route flush dev $ifname ; ip addr flush dev $ifname ; ifconfig $ifname up"
-

Again, why?

Quoted 18 lines
testcases/network/stress/ns-tools/initialize_if +0 -0
 if [ $host_type = lhost ]; then
     ( ifconfig $ifname down && \
     ip link set mtu 1500 dev $ifname && \
testcases/network/stress/ns-tools/test_net_stress.sh +0 -1
index 5ab3304db..d112286bd 100644
@@ -31,7 +31,6 @@ export TCID="${TCID:-$(basename $0)}"
 # Netmask of for the tested network
 IPV4_NETMASK="255.255.255.0"
 IPV4_NETMASK_NUM=24
-IPV4_BROADCAST=${IPV4_NET16_UNUSED}.0.255
 
 # Prefix of the Multicast Address
 MCAST_IPV4_ADDR_PREFIX="224.10"
@@ -64,16 +63,16 @@ netstress_cleanup()
 }
 

...

Quoted 5 lines
testcases/network/stress/ns-tools/test_net_stress.sh +1 -4
-	if [ $? -ne 0 ]; then
-	    tst_resm TFAIL "Failed to down/up the interface"
-	    return 1
-	fi
+		manipulate_route $cmd_name_route 'add' $dst_network $IPV4_NETMASK_NUM $IPV4_NETMASK $RHOST_IPV4_UNUSED $lhost_ifname

testcases/network/stress/ns-tools/test_net_stress.sh

Quoted 10 lines
testcases/network/stress/ns-tools/test_net_stress.sh +3 -5
 
-	cnt=`expr $cnt + 1`
-    done
+		check_connectivity_interval $cnt false $lhost_ifname $RHOST_IPV4_UNUSED
 
-    tst_resm TPASS "Test is finished correctly."
-    return 0
-}
+		# Down then up the interface
+		sh -c "$cmd_iface_down"

Can be just $cmd_iface_down, or better ROD/EXPECT_PASS.

Quoted 187 lines
testcases/network/stress/ns-tools/test_net_stress.sh +11 -13
+		if [ $? -ne 0 ]; then
+			tst_resm TFAIL "failed to down/up the interface"
+			return
+		fi
 
+		cnt=$(($cnt + 1))
+	done
 
-#-----------------------------------------------------------------------
-#
-# Main
-#
-# Exit Value:
-#   The number of the failure
-#
-#-----------------------------------------------------------------------
+	tst_resm TPASS "test is finished correctly"
+}
 
-RC=0
 do_setup
-test_body 1 || RC=`expr $RC + 1`      # Case of route command
-test_body 2 || RC=`expr $RC + 1`      # Case of ip command
-do_cleanup
+test_body 'rt_cmd'
+test_body 'ip_cmd'
 
-exit $RC
+tst_exit
testcases/network/stress/route/route4-lib.sh new file +152 -0
new file mode 100644
index 000000000..1f0047f68
@@ -0,0 +1,206 @@
+#!/bin/sh
+# Copyright (c) International Business Machines  Corp., 2006
+# Copyright (c) 2017 Petr Vorel <[email protected]>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 2 of
+# the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it would be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+# Setup script for route4-* tests.
+#
+# More information about network parameters can be found
+# in the following document: testcases/network/stress/README
+#
+# Author: Petr Vorel <[email protected]>
+
+. test_net_stress.sh
+
+tst_check_cmds ip pgrep route
+
+CHECK_INTERVAL=${CHECK_INTERVAL:-$(($NS_TIMES / 20))}
+
+# The destination network
+# NOTE: DST_NETWORK expects NETMASK IPV4_NETMASK_NUM=24
+DST_NETWORK="${IPV4_NET16_UNUSED}.${OCTET_3_IPV4_UNUSED}"
+DST_HOST=5
+DST_PORT=7
+
+route_cleanup()
+{
+	netstress_cleanup
+	restore_ipaddr
+	restore_ipaddr rhost
+}
+
+route_ns_udpsender_cleanup()
+{
+	stop_ns_udpsender
+	restore_ipaddr
+	restore_ipaddr rhost
+}
+
+route_setup()
+{
+	netstress_setup
+	tst_check_cmds route
+	route_cleanup
+}
+
+manipulate_route()
+{
+	local cmd_name=$1
+	local task=$2
+	local network=$3
+	local prefix=$4
+	local netmask=$5
+	local gateway=$6
+	local ifname=$7
+
+	[ "$task" = "add" ] || [ "$task" = "del" ] || \
+		[ "$cmd_name" = "ip" -a "$task" = "append" ] || tst_brkm TBROK "wrong task: '$task'"
+
+	[ "$cmd_name" = "ip" -a "$task" = "add" ] && task="append"
+	if [ "$cmd_name" = "ip" ]; then
+		ROD "$cmd_name route $task $network/$prefix via $gateway dev $ifname"
+	else
+		ROD "$cmd_name $task -net $network netmask $netmask gw $gateway dev $ifname"
+	fi
+}
+
+# Load the route with UDP traffic.
+run_ns_udpsender()
+{
+	local dst_addr=$1
+	local run_in_background=$2
+	local udp_size="${3:-1472}"
+	local params="-o"
+
+	if [ "$run_in_background" = true ]; then
+		# daemon is already running
+		pgrep -x ns-udpsender > /dev/null && return 0
+		params="-b"
+	fi
+
+	ROD ns-udpsender -f 4 -D $dst_addr -p $DST_PORT -s $udp_size $params
+}
+
+stop_ns_udpsender()
+{
+	local type="${1:-lhost}"
+	kill_daemon ns-udpsender $type
+}
+
+get_cmd()
+{
+	local cmd_type=$1
+
+	case $cmd_type in
+	rt_cmd) echo 'route' ;;
+	ip_cmd) echo 'ip' ;;
+	*) tst_brkm TBROK "Unknown test parameter '$cmd_type'"
+	esac
+}
+
+route_test_change()
+{
+	local test_field=$1
+	local cmd_type=$2
+
+	local rhost_ip="$RHOST_IPV4_UNUSED"
+	local lhost_ifname=$(tst_iface)
+	local rhost_part_first=$RHOST_IPV4_HOST_TOP
+	local rhost_part_last=$RHOST_IPV4_HOST_LAST
+	local cnt=0
+	local link_num=0
+	local first_route_net=${DST_NETWORK}.0
+	local cmd_name="$(get_cmd $cmd_type)"
+	local gateway="$rhost_ip"
+	local dst_addr dst_network dst_network_postfix gateway2 lhost_ifname2 rhost_part test_field_name
+
+	case $test_field in
+	dst) test_field_name='destination' ;;
+	gw) test_field_name='gateway' ;;
+	if) test_field_name='interface' ;;
+	*) tst_brkm TBROK "Unknown test parameter '$test_field'"
+	esac
+
+	tst_resm TINFO "the $test_field_name of an IPv4 route is changed by '$cmd_name' command $NS_TIMES times"
+
+	if [ "$test_field" = "if" ]; then
+		while [ $link_num -lt $LINK_TOTAL ]; do
+			rhost_ip="${IPV4_NET16_UNUSED}.${link_num}.${OCTET_4_RHOST_IPV4_HOST_UNUSED}"
+			check_connectivity $(tst_iface lhost $link_num) $rhost_ip
+			link_num=$(($link_num + 1))
+		done
+		link_num=0
+		lhost_ifname="$(tst_iface lhost $link_num)"
+		gateway="${IPV4_NET16_UNUSED}.${link_num}.${RHOST_IPV4_HOST}"
+	else
+		if [ "$test_field" = "gw" ]; then
+			rhost_ip="${IPV4_NET16_UNUSED}.${OCTET_3_IPV4_UNUSED}.$RHOST_IPV4_HOST_TOP"
+			rhost_part=$rhost_part_first
+			gateway="${IPV4_NET16_UNUSED}.${OCTET_3_IPV4_UNUSED}.${rhost_part}"
+		fi

testcases/network/stress/route/route4-lib.sh line 162

Quoted 10 lines
testcases/network/stress/route/route4-lib.sh new file +10 -0
+		tst_add_ipaddr_stress
+		check_connectivity $(tst_iface) $rhost_ip
+	fi
+
+	if [ "$test_field" != "dst" ]; then
+		dst_addr=${DST_NETWORK}.${DST_HOST}
+		dst_network=$first_route_net
+
+		tst_resm TINFO "adding first route"
+		ROD manipulate_route $cmd_name 'add' $first_route_net $IPV4_NETMASK_NUM $IPV4_NETMASK $gateway $lhost_ifname

Without 'ROD', in the new library it won't work with script functions, besides you have ROD on every command in that function already.

Quoted 17 lines
testcases/network/stress/route/route4-lib.sh new file +17 -0
+	fi
+
+	while [ $cnt -lt $NS_TIMES ]; do
+		lhost_ifname2="$lhost_ifname"
+		gateway2="$gateway"
+
+		make_background_tcp_traffic $LHOST_IPV4_UNUSED
+
+		if [ "$test_field" = "dst" ]; then
+			dst_network_postfix="$(($cnt % 255))"
+			dst_addr="${IPV4_NET16_UNUSED}.${dst_network_postfix}.${DST_HOST}"
+			dst_network="${IPV4_NET16_UNUSED}.${dst_network_postfix}.0"
+		elif [ "$test_field" = "gw" ]; then
+			pre_rhost_part=$rhost_part
+			rhost_part=$(($rhost_part + 1))
+			[ $rhost_part -gt $rhost_part_last ] && rhost_part=$rhost_part_first
+			gateway="${IPV4_NET16_UNUSED}.${OCTET_3_IPV4_UNUSED}.${rhost_part}"

testcases/network/stress/route/route4-lib.sh line 189

Quoted 10 lines
testcases/network/stress/route/route4-lib.sh new file +10 -0
+			gateway2="${IPV4_NET16_UNUSED}.${OCTET_3_IPV4_UNUSED}.${pre_rhost_part}"
+		elif [ "$test_field" = "if" ]; then
+			link_num=$(($link_num + 1))
+			[ $link_num -ge $LINK_TOTAL ] && link_num=0
+			lhost_ifname="$(tst_iface lhost $link_num)"
+			gateway="${IPV4_NET16_UNUSED}.${link_num}.${RHOST_IPV4_HOST}"
+			rhost_ip="${IPV4_NET16_UNUSED}.${link_num}.${OCTET_4_RHOST_IPV4_HOST_UNUSED}"
+		fi
+
+		manipulate_route $cmd_name 'add' $dst_network $IPV4_NETMASK_NUM $IPV4_NETMASK $gateway $lhost_ifname

Looking at the original test-case, after new route added, the test triesto send a single UDP datagram in order to load it. But now all the tests have a background traffic on that interface and route not used, correct?

Quoted 473 lines
testcases/network/stress/route/route4-lib.sh new file +17 -0
+		manipulate_route $cmd_name 'del' $dst_network $IPV4_NETMASK_NUM $IPV4_NETMASK $gateway2 $lhost_ifname2
+
+		check_connectivity_interval $cnt false $lhost_ifname $rhost_ip || return
+
+		cnt=$(($cnt + 1))
+	done
+
+	if [ "$test_field" != "dst" ]; then
+		ROD manipulate_route $cmd_name del $first_route_net $IPV4_NETMASK_NUM $IPV4_NETMASK $gateway $lhost_ifname
+	fi
+
+	if [ "$test_field" != "if" ]; then
+		restore_ipaddr
+	fi
+
+	tst_resm TPASS "test is finished correctly"
+}
testcases/network/stress/route/route4-redirect +40 -180
index 9eb8a4624..714e0af25 100644
@@ -1,212 +1,72 @@
 #!/bin/sh
-
-################################################################################
-##                                                                            ##
-## Copyright (c) International Business Machines  Corp., 2006                 ##
-##                                                                            ##
-## This program is free software;  you can redistribute it and#or modify      ##
-## it under the terms of the GNU General Public License as published by       ##
-## the Free Software Foundation; either version 2 of the License, or          ##
-## (at your option) any later version.                                        ##
-##                                                                            ##
-## This program is distributed in the hope that it will be useful, but        ##
-## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
-## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
-## for more details.                                                          ##
-##                                                                            ##
-## You should have received a copy of the GNU General Public License          ##
-## along with this program;  if not, write to the Free Software               ##
-## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    ##
-##                                                                            ##
-##                                                                            ##
-################################################################################
+# Copyright (c) International Business Machines  Corp., 2006
+# Copyright (c) 2017 Petr Vorel <[email protected]>
 #
-# File:
-#   route4-redirect
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 2 of
+# the License, or (at your option) any later version.
 #
-# Description:
-#   Verify the kernel is not crashed when the IPv4 route is modified by
-#   ICMP Redirects frequently
+# This program is distributed in the hope that it would be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
 #
-# Setup:
-#   See ltp-yyyymmdd/testcases/network/stress/README
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
 #
-# Author:
-#   Mitsuru Chinen <[email protected]>
+# Verify the kernel is not crashed when the IPv4 route is modified by
+# ICMP Redirects frequently.
 #
-# History:
-#	Apr 07 2006 - Created (Mitsuru Chinen)
+# Setup: testcases/network/stress/README
 #
-#-----------------------------------------------------------------------
-# Uncomment line below for debug output.
-#trace_logic=${trace_logic:-"set -x"}
-$trace_logic
+# Author: Mitsuru Chinen <[email protected]>
 
-# The test case ID, the test case count and the total number of test case
-TCID=route4-redirect01
 TST_TOTAL=1
-TST_COUNT=1
-export TCID
-export TST_COUNT
-export TST_TOTAL
-
-# Test description
-tst_resm TINFO "Verify the kernel is not crashed when the IPv4 route is modified by  ICMP Redirects frequently"
-
-# Make sure the value of LTPROOT
-LTPROOT=${LTPROOT:-`(cd ../../../.. ; pwd)`}
-export LTPROOT
-
-# Check the environmanet variable
-. check_envval || exit $TST_TOTAL
-
-# The number of times where route is changed
-NS_TIMES=${NS_TIMES:-10000}
-
-# The number of the test link where tests run
-LINK_NUM=${LINK_NUM:-0}
-
-# Network portion of the IPv4 address
-IPV4_NETWORK=${IPV4_NETWORK:-"10.0.0"}
-
-# Netmask of for the tested network
-IPV4_NETMASK_NUM=24
 
-# Broadcast address of the tested network
-IPV4_BROADCAST=${IPV4_NETWORK}.255
+. route4-lib.sh
 
-# Host portion of the IPv4 address
-LHOST_IPV4_HOST=${LHOST_IPV4_HOST:-"1"}	# src
-RHOST_IPV4_HOST="2"	# gateway
+TST_CLEANUP="do_cleanup"
 
-# The destination network
-DST_NETWORK="10.10.0"   # destination network would be 10.10.0.0/24
-DST_HOST="5"
-DST_PORT="7"
+SYSFS_ACCEPT_REDIRECTS=
+SYSFS_SECURE_REDIRECTS=
 
-
-#-----------------------------------------------------------------------
-#
-# NAME:
-#   do_cleanup
-#
-# DESCRIPTION:
-#   Recover the tested interfaces
-#
-#-----------------------------------------------------------------------
 do_cleanup()
 {
-    # Kill the redirector utility
-    $LTP_RSH $RHOST killall -SIGHUP ns-icmp_redirector >/dev/null 2>&1
-
-    # Initialize the interfaces
-    initialize_if lhost ${LINK_NUM}
-    initialize_if rhost ${LINK_NUM}
+	route_ns_udpsender_cleanup
+	kill_daemon ns-icmp_redirector remote
+	[ -n "$SYSFS_ACCEPT_REDIRECTS" ] && sysctl -qw net.ipv4.conf.$(tst_iface).accept_redirects=$SYSFS_ACCEPT_REDIRECTS
+	[ -n "$SYSFS_SECURE_REDIRECTS" ] && sysctl -qw net.ipv4.conf.$(tst_iface).secure_redirects=$SYSFS_SECURE_REDIRECTS
 }
 
-
-#-----------------------------------------------------------------------
-#
-# NAME:
-#   do_setup
-#
-# DESCRIPTION:
-#   Set the initial route and start icmp redirect on the remote host
-#
-# SET VALUES:
-#   rhost_ipv4addr      - IPv4 Address of the remote host
-#   lhost_ifname        - Interface name of the local host
-#   rhost_ifname        - Interface name of the remote host
-#
-#-----------------------------------------------------------------------
 do_setup()
 {
-    # Make sure to clean up
-    do_cleanup
-
-    # Get the Interface name of local host
-    lhost_ifname=`get_ifname lhost ${LINK_NUM}`
-    if [ $? -ne 0 ]; then
-	tst_resm TBROK "Failed to get the interface name at the local host"
-	exit $TST_TOTAL
-    fi
-
-    # Get the Interface name of remote host
-    rhost_ifname=`get_ifname rhost ${LINK_NUM}`
-    if [ $? -ne 0 ]; then
-	tst_resm TBROK "Failed to get the interface name at the remote host"
-	exit $TST_TOTAL
-    fi
+	route_setup
 
-    # Remove the link-local address of the remote host
-    sleep 3
-    $LTP_RSH $RHOST "ip addr flush dev $rhost_ifname" > /dev/null
+	SYSFS_ACCEPT_REDIRECTS=$(sysctl -b net.ipv4.conf.$(tst_iface).accept_redirects)
+	SYSFS_SECURE_REDIRECTS=$(sysctl -b net.ipv4.conf.$(tst_iface).secure_redirects)
 
-    # Assign IPv4 address to the interface of the local host
-    set_ipv4addr lhost ${LINK_NUM} ${IPV4_NETWORK} ${LHOST_IPV4_HOST}
-    if [ $? -ne 0 ]; then
-	tst_resm TBROK "Failed to assign an IPv4 address at the local host"
-	return 1
-    fi
+	tst_add_ipaddr_stress
 
-    # Add route to the initial gateway
-    route add -net ${DST_NETWORK}.0 netmask 255.255.255.0 gw ${IPV4_NETWORK}.${RHOST_IPV4_HOST} dev $lhost_ifname
+	tst_resm TINFO "add route to the initial gateway"
+	ROD manipulate_route "route" "add" ${DST_NETWORK}.0 $IPV4_NETMASK_NUM $IPV4_NETMASK $RHOST_IPV4_UNUSED $(tst_iface)
 
-    # Make sure the sysctl value is set for accepting the redirect
-    sysctl -w net.ipv4.conf.${lhost_ifname}.accept_redirects=1 >/dev/null
-    sysctl -w net.ipv4.conf.${lhost_ifname}.secure_redirects=0 >/dev/null
-
-    # Run the redirector utility at the remote host
-    ret=`$LTP_RSH $RHOST "${LTPROOT}/testcases/bin/ns-icmp_redirector -I $rhost_ifname -b ; "'echo $?'`
-    if [ $ret -ne 0 ]; then
-	tst_resm TBROK "Failed to run icmp redirector at the remote host"
-	exit $TST_TOTAL
-    fi
+	# Run the redirector utility at the remote host
+	tst_rhost_run -s -c "ns-icmp_redirector -I $(tst_iface rhost) -b"
 }
 
-
-
-#-----------------------------------------------------------------------
-#
-# FUNCTION:
-#   test_body
-#
-# DESCRIPTION:
-#   main code of the test
-#
-# Arguments:
-#   None
-#
-#-----------------------------------------------------------------------
 test_body()
 {
-    # Loop for changing the route
-    cnt=0
-    while [ $cnt -lt $NS_TIMES ]; do
-	ns-udpsender -f 4 -D ${DST_NETWORK}.${DST_HOST} -p $DST_PORT -o -s 8
-	if [ $? -ne 0 ]; then
-	    tst_resm TBROK "Failed to run udp packet sender"
-	    return 1
-	fi
-	cnt=`expr $cnt + 1`
-    done
+	local cnt=0
+	while [ $cnt -lt $NS_TIMES ]; do
+		run_ns_udpsender ${DST_NETWORK}.${DST_HOST} no 8
+		cnt=$(($cnt + 1))
+	done
 
-    tst_resm TPASS "Test is finished correctly."
-    return 0
+	tst_resm TPASS "test is finished correctly"
 }
 
-
-#-----------------------------------------------------------------------
-#
-# Main
-#
-# Exit Value:
-#   The number of the failure
-#
-#-----------------------------------------------------------------------
-RC=0
 do_setup
-test_body || RC=`expr $RC + 1`
-do_cleanup
+test_body
 
-exit $RC
+tst_exit
testcases/network/stress/route/route4-rmmod +22 -154
index 36c8c970d..70a6e3745 100644
@@ -1,280 +1,104 @@
 #!/bin/sh
-
-################################################################################
-##                                                                            ##
-## Copyright (c) International Business Machines  Corp., 2006                 ##
-##                                                                            ##
-## This program is free software;  you can redistribute it and#or modify      ##
-## it under the terms of the GNU General Public License as published by       ##
-## the Free Software Foundation; either version 2 of the License, or          ##
-## (at your option) any later version.                                        ##
-##                                                                            ##
-## This program is distributed in the hope that it will be useful, but        ##
-## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
-## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
-## for more details.                                                          ##
-##                                                                            ##
-## You should have received a copy of the GNU General Public License          ##
-## along with this program;  if not, write to the Free Software               ##
-## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    ##
-##                                                                            ##
-##                                                                            ##
-################################################################################
-#
-# File:
-#   route4-rmmod
+# Copyright (c) International Business Machines  Corp., 2006
+# Copyright (c) 2017 Petr Vorel <[email protected]>
 #
-# Description:
-#   Verify the kernel is not crashed when IPv4 route is add then it is deleted
-#   by the removing network driver
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 2 of
+# the License, or (at your option) any later version.
 #
-# Setup:
-#   See ltp-yyyymmdd/testcases/network/stress/README
+# This program is distributed in the hope that it would be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
 #
-# Author:
-#   Mitsuru Chinen <[email protected]>
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
 #
-# History:
-#	Apr 8 2006 - Created (Mitsuru Chinen)
+# Setup: testcases/network/stress/README
 #
-#-----------------------------------------------------------------------
-# Uncomment line below for debug output.
-#trace_logic=${trace_logic:-"set -x"}
-$trace_logic
-
-# Make sure the value of LTPROOT
-LTPROOT=${LTPROOT:-`(cd ../../../../ ; pwd)`}
-export LTPROOT
+# Author: Mitsuru Chinen <[email protected]>
 
-# Total number of the test case
 TST_TOTAL=2
-export TST_TOTAL
-
-# Default of the test case ID and the test case count
-TCID=route4-rmmod
-TST_COUNT=0
-export TCID
-export TST_COUNT
-
-# Check the environmanet variable
-. check_envval || exit $TST_TOTAL
-
-# The number of times where IPv4 route is add/delete
-NS_TIMES=${NS_TIMES:-10000}
-
-# The number of the test link where tests run
-LINK_NUM=${LINK_NUM:-0}
-
-# Network portion of the IPv4 address
-IPV4_NETWORK=${IPV4_NETWORK:-"10.0.0"}
-
-# Netmask of for the tested network
-IPV4_NETMASK="255.255.255.0"
-IPV4_NETMASK_NUM=24
-
-# Broadcast address of the tested network
-IPV4_BROADCAST=${IPV4_NETWORK}.255
-
-# Host portion of the IPv4 address
-LHOST_IPV4_HOST=${LHOST_IPV4_HOST:-"2"}	# src
-RHOST_IPV4_HOST=${RHOST_IPV4_HOST:-"1"}	# gateway
 
-# The destination network
-DST_NETWORK="10.10.10"	# destination network would be 10.10.10.0/24
-DST_HOST="5"
-DST_PORT="7"
+. route4-lib.sh
 
+TST_CLEANUP="do_cleanup"
 
-#-----------------------------------------------------------------------
-#
-# NAME:
-#   do_cleanup
-#
-# DESCRIPTION:
-#   Recover the tested interfaces
-#
-#-----------------------------------------------------------------------
 do_cleanup()
 {
-    # Make sure to load the network driver
-    if [ x${lhost_module} != x ]; then
-	modprobe $lhost_module
-    fi
-
-    # Initialize the interfaces
-    initialize_if lhost ${LINK_NUM}
-    initialize_if rhost ${LINK_NUM}
+	if [ -n "${lhost_module}" ]; then
+		modprobe $lhost_module
+		tst_sleep 100ms
+	fi
+	netstress_cleanup
+	restore_ipaddr
 }
 
 
-#-----------------------------------------------------------------------
-#
-# NAME:
-#   do_setup
-#
-# DESCRIPTION:
-#   Make a IPv4 connectivity
-#
-#-----------------------------------------------------------------------
 do_setup()
 {
-    # Check the local host has ethtool utility
-    which ethtool >/dev/null
-    if [ $? -ne 0 ]; then
-	tst_resm TBROK "This test case requires ethtool utility"
-	exit $TST_TOTAL
-    fi
-
-    # Make sure to clean up
-    do_cleanup
-
-    # Set IPv4 address to the interfaces of the remote host
-    set_ipv4addr rhost ${LINK_NUM} ${IPV4_NETWORK} ${RHOST_IPV4_HOST}
-    if [ $? -ne 0 ]; then
-	tst_resm TBROK "Failed to add an IPv4 address the remote host"
-	exit $TST_TOTAL
-    fi
-    rhost_ipv4addr="${IPV4_NETWORK}.${RHOST_IPV4_HOST}"
-
-    # Assign IPv4 address to the interface of the local host
-    set_ipv4addr lhost ${LINK_NUM} ${IPV4_NETWORK} ${LHOST_IPV4_HOST}
-    if [ $? -ne 0 ]; then
-	tst_resm TBROK "Failed to assign an IPv4 address at the local host"
-	return 1
-    fi
-    lhost_ipv4addr="${IPV4_NETWORK}.${LHOST_IPV4_HOST}"
-
-    # Get the Interface names
-    lhost_ifname=`get_ifname lhost ${LINK_NUM}`
-    if [ $? -ne 0 ]; then
-	tst_resm TBROK "Failed to get the interface name at the local host"
-	exit $TST_TOTAL
-    fi
-
-    rhost_ifname=`get_ifname rhost ${LINK_NUM}`
-    if [ $? -ne 0 ]; then
-	tst_resm TBROK "Failed to get the interface name at the remote host"
-	exit $TST_TOTAL
-    fi
-
-    # Get the module name of the interface at the local host
-    lhost_module=`ethtool -i $lhost_ifname | grep driver | sed "s/driver:[[:blank:]]*//"`
-
-    # Chack the other active interface uses the same driver
-    for ifname in `ifconfig | grep ^eth | awk '{ print $1}'`; do
-	if [ $lhost_ifname = $ifname ]; then
-	    continue
-	fi
-
-	module=`ethtool -i $ifname | grep driver | sed "s/driver:[[:blank:]]*//"`
-	if [ $lhost_module = $module ]; then
-	    tst_resm TBROK "An active interface $ifname uses the same network deriver $module with the test intreface."
-	    exit $TST_TOTAL

testcases/network/stress/route/route4-rmmod line 44

Quoted 10 lines
testcases/network/stress/route/route4-rmmod +4 -6
-	fi
-    done
-
-    # Set the variables for destination network
-    dst_addr=${DST_NETWORK}.${DST_HOST}
-    dst_network=${DST_NETWORK}.0
+	route_setup
+	tst_check_cmds ethtool
+
+	tst_add_ipaddr_stress

tst_add_ipaddr_stress() should be after all the checks. Also I doubt that we need to add a new address in every test. Could you check again if we really need it?

Quoted 86 lines
testcases/network/stress/route/route4-rmmod +26 -52
+
+	# Get the module name of the interface at the local host
+	lhost_module=$(ethtool -i $(tst_iface) | grep driver | sed "s/driver:[[:blank:]]*//")
+
+	# Check the other active interface uses the same driver
+	for ifname in $(ifconfig | grep ^eth | awk '{ print $1}'); do
+		if [ "$(tst_iface)" = "$ifname" ]; then
+			continue
+		fi
+
+		module=$(ethtool -i $ifname | grep driver | sed "s/driver:[[:blank:]]*//")
+		if [ $lhost_module = $module ]; then
+			tst_resm TBROK "an active interface '$ifname' uses the same network driver '$module' with the test intreface"
+			return
+		fi
+	done
 }
 
-
-#-----------------------------------------------------------------------
-#
-# FUNCTION:
-#   test_body
-#
-# DESCRIPTION:
-#   main code of the test
-#
-# Arguments:
-#   $1: define the test type
-#	1 - route command case
-#	2 - ip command case
-#
-#-----------------------------------------------------------------------
 test_body()
 {
-    test_type=$1
+	local cmd_type=$1
+	local cmd_name="$(get_cmd $cmd_type)"
+	local dst_addr=${DST_NETWORK}.${DST_HOST}
+	local dst_network=${DST_NETWORK}.0
+	local cnt=0
 
-    TCID=route4-rmmod0${test_type}
-    TST_COUNT=$test_type
+	tst_resm TINFO "IPv4 route is added by '$cmd_name' command and then deleted by removing network driver $NS_TIMES times"
 
-    case $test_type in
-	1)
-	tst_resm TINFO "Verify the kernel is not crashed when IPv4 route is add by route command then it is deleted by removing network driver in $NS_TIMES times"
-	;;
-	2)
-	tst_resm TINFO "Verify the kernel is not crashed when IPv4 route is add by ip command then it is deleted by removing network driver in $NS_TIMES times"
-	;;
-	*)
-	tst_resm TBROK "unspecified case"
-	return 1
-	;;
-    esac
+	while [ $cnt -lt $NS_TIMES ]; do
+		make_background_tcp_traffic $LHOST_IPV4_UNUSED
 
-    # Start the loop
-    cnt=0
-    while [ $cnt -lt $NS_TIMES ]; do
-	# Check the connectivity to the gateway
-	check_icmpv4_connectivity $lhost_ifname $rhost_ipv4addr
-	if [ $? -ne 0 ]; then
-	    tst_resm TBROK "Test Link $LINK_NUM is somthing wrong."
-	    return 1
-	fi
+		check_connectivity_interval $cnt false $(tst_iface) $RHOST_IPV4_UNUSED || return
 
-	# Add the route
-	case $test_type in
-	    1)
-	    route add -net $dst_network netmask 255.255.255.0 gw $rhost_ipv4addr dev $lhost_ifname
-	    ;;
-	    2)
-	    ip route add ${dst_network}/24 via $rhost_ipv4addr dev $lhost_ifname
-	    ;;
-	esac
-	if [ $? -ne 0 ]; then
-	    tst_resm TFAIL "Failed to add the route to ${dst_network}/24"
-	    return 1
-	fi
+		# Add the route

testcases/network/stress/route/route4-rmmod line 82

Quoted 10 lines
testcases/network/stress/route/route4-rmmod +3 -6
+		ROD manipulate_route $cmd_name 'add' $dst_network $IPV4_NETMASK_NUM $IPV4_NETMASK $RHOST_IPV4_UNUSED $(tst_iface)
 
-	# Load the route with UDP datagram
-	ns-udpsender -f 4 -D $dst_addr -p $DST_PORT -o -s 1472
-	if [ $? -ne 0 ]; then
-	    tst_resm TFAIL "Failed to run a UDP datagram sender"
-	    return 1
-	fi
+		# Remove and reload the network driver
+		rmmod $lhost_module && modprobe $lhost_module

ROD modprobe -r $module
ROD modprobe $module

testcases/network/stress/route/route4-rmmod line 87

Quoted 5 lines
testcases/network/stress/route/route4-rmmod +5 -0
+		if [ $? -ne 0 ]; then
+			tst_resm TFAIL "failed to unload/reload the network driver '$lhost_module'"
+			return
+		fi
+		tst_sleep 100ms

Are you sure we need sleep here?

Quoted 49 lines
testcases/network/stress/route/route4-rmmod +9 -27
-	# Remove and reload the network driver
-	rmmod $lhost_module && modprobe $lhost_module
-	if [ $? -ne 0 ]; then
-	    tst_resm TFAIL "Failed to unload/reload the network driver"
-	    return 1
-	fi
+		# If we use netns we need to reset it as unloading kernel module breaks it
+		reset_ltp_netspace
 
-	# Make sure to assing the IPv4 address
-	set_ipv4addr lhost ${LINK_NUM} ${IPV4_NETWORK} ${LHOST_IPV4_HOST} >/dev/null 2>&1
+		tst_add_ipaddr_stress
 
-	cnt=`expr $cnt + 1`
-    done
+		cnt=$(($cnt + 1))
+	done
 
-    tst_resm TPASS "Test is finished correctly."
-    return 0
+	tst_resm TPASS "test is finished correctly"
 }
 
-
-#-----------------------------------------------------------------------
-#
-# Main
-#
-# Exit Value:
-#   The number of the failure
-#
-#-----------------------------------------------------------------------
-
-RC=0
 do_setup
-test_body 1 || RC=`expr $RC + 1`      # Case of route command
-test_body 2 || RC=`expr $RC + 1`      # Case of ip command
-do_cleanup
+test_body 'rt_cmd'
+test_body 'ip_cmd'
 
-exit $RC
+tst_exit
testcases/network/stress/route/route6-change-dst +0 -0
index 33e89e985..649f0cc00 100644

testcases/network/stress/route/route6-change-dst test_body() · line 248

Quoted 8 lines
testcases/network/stress/route/route6-change-dst +1 -1
@@ -242,7 +242,7 @@ test_body()
 	    ;;
 	esac
 	if [ $? -ne 0 ]; then
-	    tst_resm TFAIL "Cannot delte the route to ${ADDDEL_ROUTE}"
+	    tst_resm TFAIL "Cannot delete the route to ${ADDDEL_ROUTE}"
 	    return 1
 	fi
 

Thanks,
Alexey

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20170726/ea4df174/attachment-0001.html>

author
#26 lore raw

RFC PATCH v7 8/11 network/stress: Fix and cleanup route IPv4 tests

Hi Alexey,

Quoted 1 line
Quoted 1 line
testcases/network/stress/ns-tools/initialize_if +0 -0

...

Quoted 5 lines

testcases/network/stress/ns-tools/initialize_if fi · line 74

Quoted 3 lines
testcases/network/stress/ns-tools/initialize_if +0 -3
-# Initialize the specified interface
-command="ifconfig $ifname down mtu 1500 ; ip route flush dev $ifname ; ip addr flush dev $ifname ; ifconfig $ifname up"
-

Again, why?

OK, I'll drop this part.
Why: I admit that this cleanup is unnecessary as initialize_if is deprecated I just didn't consider cleaning initialize_if harmful (the script is still used).

Quoted 4 lines

testcases/network/stress/ns-tools/initialize_if

Quoted 2 lines
testcases/network/stress/ns-tools/initialize_if +2 -0
+		# Down then up the interface
+		sh -c "$cmd_iface_down"

Can be just $cmd_iface_down, or better ROD/EXPECT_PASS.

I'll have to split command into two, as only 'sh -c' can handle '&&' (and ROD_BASE doesn't use 'sh -c')

Quoted 6 lines

testcases/network/stress/ns-tools/initialize_if

Quoted 3 lines
testcases/network/stress/ns-tools/initialize_if +3 -0
+
+		tst_resm TINFO "adding first route"
+		ROD manipulate_route $cmd_name 'add' $first_route_net $IPV4_NETMASK_NUM $IPV4_NETMASK $gateway $lhost_ifname

Without 'ROD', in the new library it won't work with script functions, besides you have ROD on every command in that function already.

OK. I'm not sure, if it's best to have ROD inside manipulate_route, but I'll keep it for simplicity.

Quoted 22 lines
Quoted 22 lines
testcases/network/stress/ns-tools/initialize_if +22 -0
+	while [ $cnt -lt $NS_TIMES ]; do
+		lhost_ifname2="$lhost_ifname"
+		gateway2="$gateway"
+
+		make_background_tcp_traffic $LHOST_IPV4_UNUSED
+
+		if [ "$test_field" = "dst" ]; then
+			dst_network_postfix="$(($cnt % 255))"
+			dst_addr="${IPV4_NET16_UNUSED}.${dst_network_postfix}.${DST_HOST}"
+			dst_network="${IPV4_NET16_UNUSED}.${dst_network_postfix}.0"
+		elif [ "$test_field" = "gw" ]; then
+			pre_rhost_part=$rhost_part
+			rhost_part=$(($rhost_part + 1))
+			[ $rhost_part -gt $rhost_part_last ] && rhost_part=$rhost_part_first
+			gateway="${IPV4_NET16_UNUSED}.${OCTET_3_IPV4_UNUSED}.${rhost_part}"
+			gateway2="${IPV4_NET16_UNUSED}.${OCTET_3_IPV4_UNUSED}.${pre_rhost_part}"
+		elif [ "$test_field" = "if" ]; then
+			link_num=$(($link_num + 1))
+			[ $link_num -ge $LINK_TOTAL ] && link_num=0
+			lhost_ifname="$(tst_iface lhost $link_num)"
+			gateway="${IPV4_NET16_UNUSED}.${link_num}.${RHOST_IPV4_HOST}"
+			rhost_ip="${IPV4_NET16_UNUSED}.${link_num}.${OCTET_4_RHOST_IPV4_HOST_UNUSED}"
Quoted 8 lines

testcases/network/stress/ns-tools/initialize_if

Quoted 3 lines
testcases/network/stress/ns-tools/initialize_if +3 -0
+		fi
+
+		manipulate_route $cmd_name 'add' $dst_network $IPV4_NETMASK_NUM $IPV4_NETMASK $gateway $lhost_ifname

Looking at the original test-case, after new route added, the test tries to send a single UDP datagram in order to load it. But now all the tests have a background traffic on that interface and route not used, correct?

I intended to replace ns-udpsender with make_background_tcp_traffic call. But that probably not covering the test scenario. But, even the code in ns-udpsender.c is very simple I'm not sure if it's possible to do this replacement.
Generally, I'd like to get rid of these old C programs and use netstress as much as possible.

Quoted 7 lines
Quoted 7 lines
testcases/network/stress/ns-tools/initialize_if +7 -0
+		manipulate_route $cmd_name 'del' $dst_network $IPV4_NETMASK_NUM $IPV4_NETMASK $gateway2 $lhost_ifname2
+
+		check_connectivity_interval $cnt false $lhost_ifname $rhost_ip || return
+
+		cnt=$(($cnt + 1))
+	done
+

...

Quoted 11 lines

testcases/network/stress/ns-tools/initialize_if

Quoted 7 lines
testcases/network/stress/ns-tools/initialize_if +4 -3
-    # Set the variables for destination network
-    dst_addr=${DST_NETWORK}.${DST_HOST}
-    dst_network=${DST_NETWORK}.0
+	route_setup
+	tst_check_cmds ethtool
+
+	tst_add_ipaddr_stress

tst_add_ipaddr_stress() should be after all the checks. Also I doubt that we need to add a new address in every test. Could you check again if we really need it?

I'll do.

Quoted 5 lines

testcases/network/stress/ns-tools/initialize_if

Quoted 2 lines
testcases/network/stress/ns-tools/initialize_if +2 -0
+		# Remove and reload the network driver
+		rmmod $lhost_module && modprobe $lhost_module

ROD modprobe -r $module
ROD modprobe $module

OK.

Quoted 7 lines

testcases/network/stress/ns-tools/initialize_if

Quoted 5 lines
testcases/network/stress/ns-tools/initialize_if +5 -0
+		if [ $? -ne 0 ]; then
+			tst_resm TFAIL "failed to unload/reload the network driver '$lhost_module'"
+			return
+		fi
+		tst_sleep 100ms

Are you sure we need sleep here?

Yes. It's because loading driver takes some time, test was failing without that.

Kind regards,
Petr

author
#27 lore raw

RFC PATCH v7 9/11 network/stress: Further enhancements for route4-rmmod

In reply to Petr Vorel

* Remove dependency to ifconfig
* Use TCONF when test would break other interface
* Don't raise TCONF only if the other device is in 'down' mode

---
 testcases/network/stress/route/route4-rmmod | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
testcases/network/stress/route/route4-rmmod +10 -9
index 70a6e3745..a089d6712 100644
@@ -35,28 +35,29 @@ do_cleanup()
 	restore_ipaddr
 }
 
-
 do_setup()
 {
 	route_setup
+
+	# NOTE: we have to use ethtool as
+	# /sys/class/net/<iface>/device/driver/module doesn't exist
+	# for virtual drivers (e.g. veth)
 	tst_check_cmds ethtool
 
 	tst_add_ipaddr_stress
 
 	# Get the module name of the interface at the local host
 	lhost_module=$(ethtool -i $(tst_iface) | grep driver | sed "s/driver:[[:blank:]]*//")
+	[ -z "$lhost_module" ] && tst_resm TBROK "module for interface '$(tst_iface)' not found"
 
 	# Check the other active interface uses the same driver
-	for ifname in $(ifconfig | grep ^eth | awk '{ print $1}'); do
-		if [ "$(tst_iface)" = "$ifname" ]; then
-			continue
-		fi
+	for ifname in $(basename -a /sys/class/net/*); do
+		[ "$(tst_iface)" = "$ifname" ] && continue
 
 		module=$(ethtool -i $ifname | grep driver | sed "s/driver:[[:blank:]]*//")
-		if [ $lhost_module = $module ]; then
-			tst_resm TBROK "an active interface '$ifname' uses the same network driver '$module' with the test intreface"
-			return
-		fi
+
+		[ "$lhost_module" = "$module" -a "$(cat /sys/class/net/$ifname/operstate)" != "down" ] && \
+			tst_brkm TCONF "an active interface '$ifname' uses the same network driver '$module' with the test interface '$(tst_iface)'"
 	done
 }
 
Signature · 1 line
2.13.2
author
#28 lore raw

RFC PATCH v7 10/11 network: Add tools for setup IP related environment variables

In reply to Petr Vorel

New tools:
* tst_net_ip_prefix
Strip prefix from IP address and save both
If no prefix found sets default prefix.

* tst_net_iface_prefix
tst_net_iface_prefix reads prefix and interface from rtnetlink.

* tst_net_vars takes input of local and remote address and setup most of test link (IP related) environment variables.

NOTABLE CHANGES IN ENVIRONMENT VARIABLES:
Network variables are defined for both local and remote:
IPV4_NETWORK was replaced by IPV4_LNETWORK and IPV4_RNETWORK IPV6_NETWORK was replaced by IPV6_LNETWORK and IPV6_RNETWORK

NEW VARIABLES:
IPV4_LHOST, IPV4_RHOST, IPV4_LNETMASK, IPV4_RNETMASK, IPV4_LPREFIX, IPV4_RPREFIX, IPV6_LHOST, IPV6_RHOST, IPV6_LNETMASK, IPV6_RNETMASK, IPV6_LPREFIX, IPV6_RPREFIX,

For full list of environment variables which tools setup run:
tst_net_ip_prefix -h
tst_net_iface_prefix -h
tst_net_vars -h

Thanks a lot to Alexey Kodanev who reviewed many versions of this patch and give feedback and pointed out errors, as well as to Cyril Hrubis who reviewed orignal version of tst_net_vars.c.

---
 include/tst_net.h                    | 140 ++++++++
 testcases/lib/.gitignore             |   3 +
 testcases/lib/Makefile               |   2 +-
 testcases/lib/tst_net_iface_prefix.c | 173 +++++++++
 testcases/lib/tst_net_ip_prefix.c    | 116 +++++++
 testcases/lib/tst_net_vars.c         | 655 +++++++++++++++++++++++++++++++++++
 6 files changed, 1088 insertions(+), 1 deletion(-)
 create mode 100644 include/tst_net.h
 create mode 100644 testcases/lib/tst_net_iface_prefix.c
 create mode 100644 testcases/lib/tst_net_ip_prefix.c
 create mode 100644 testcases/lib/tst_net_vars.c
include/tst_net.h new file +140 -0
new file mode 100644
index 000000000..07ca79825
@@ -0,0 +1,140 @@
+/*
+ * Copyright (c) 2017 Petr Vorel <[email protected]>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <arpa/inet.h>
+#include <errno.h>
+
+#define MAX_IPV4_PREFIX 32
+#define MAX_IPV6_PREFIX 128
+
+#define tst_res_comment(...) { \
+	fprintf(stderr, "# "); \
+	tst_res(__VA_ARGS__); } \
+
+
+#define tst_brk_comment(...) { \
+	fprintf(stderr, "# "); \
+	tst_brk(__VA_ARGS__); } \
+
+static inline void print_svar(const char *name, const char *val)
+{
+	if (name && val)
+		printf("export %s=\"%s\"\n", name, val);
+}
+
+static inline void print_svar_change(const char *name, const char *val)
+{
+	if (name && val)
+		printf("export %s=\"${%s:-%s}\"\n", name, name, val);
+}
+
+/*
+ * Function bit_count is from ipcalc project, ipcalc.c.
+ */
+static int bit_count(uint32_t i)
+{
+	int c = 0;
+	unsigned int seen_one = 0;
+
+	while (i > 0) {
+		if (i & 1) {
+			seen_one = 1;
+			c++;
+		} else {
+			if (seen_one)
+				return -1;
+		}
+		i >>= 1;
+	}
+
+	return c;
+}
+
+/*
+ * Function mask2prefix is from ipcalc project, ipcalc.c.
+ */
+static int mask2prefix(struct in_addr mask)
+{
+	return bit_count(ntohl(mask.s_addr));
+}
+
+/*
+ * Function ipv4_mask_to_int is from ipcalc project, ipcalc.c.
+ */
+static int ipv4_mask_to_int(const char *prefix)
+{
+	int ret;
+	struct in_addr in;
+
+	ret = inet_pton(AF_INET, prefix, &in);
+	if (ret == 0)
+		return -1;
+
+	return mask2prefix(in);
+}
+
+/*
+ * Function safe_atoi is from ipcalc project, ipcalc.c.
+ */
+static int safe_atoi(const char *s, int *ret_i)
+{
+	char *x = NULL;
+	long l;
+
+	errno = 0;
+	l = strtol(s, &x, 0);
+
+	if (!x || x == s || *x || errno)
+		return errno > 0 ? -errno : -EINVAL;
+
+	if ((long)(int)l != l)
+		return -ERANGE;
+
+	*ret_i = (int)l;
+
+	return 0;
+}
+
+/*
+ * Function get_prefix use code from ipcalc project, str_to_prefix/ipcalc.c.
+ */
+static int get_prefix(const char *ip_str, int is_ipv6)
+{
+	char *prefix_str = NULL;
+	int prefix = -1, r;
+
+	prefix_str = strchr(ip_str, '/');
+	if (!prefix_str)
+		return -1;
+
+	*(prefix_str++) = '\0';
+
+	if (!is_ipv6 && strchr(prefix_str, '.'))
+		prefix = ipv4_mask_to_int(prefix_str);
+	else {
+		r = safe_atoi(prefix_str, &prefix);
+		if (r != 0)
+			tst_brk(TBROK, "conversion error");
+	}
+
+	if (prefix < 0 || ((is_ipv6 && prefix > MAX_IPV6_PREFIX) ||
+		(!is_ipv6 && prefix > MAX_IPV4_PREFIX)))
+		tst_brk(TBROK, "bad %s prefix: %s", is_ipv6 ? "IPv6" : "IPv4",
+			prefix_str);
+
+	return prefix;
+}
testcases/lib/.gitignore +3 -0
index 522889bed..f36ed99fa 100644
@@ -4,3 +4,6 @@
 /tst_rod
 /tst_kvcmp
 /tst_device
+/tst_net_iface_prefix
+/tst_net_ip_prefix
+/tst_net_vars
testcases/lib/Makefile +1 -1
index 1127a59fe..398150ae0 100644
@@ -27,6 +27,6 @@ include $(top_srcdir)/include/mk/testcases.mk
 INSTALL_TARGETS		:= *.sh
 
 MAKE_TARGETS		:= tst_sleep tst_random tst_checkpoint tst_rod tst_kvcmp\
-			   tst_device
+			   tst_device tst_net_iface_prefix tst_net_ip_prefix tst_net_vars
 
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/lib/tst_net_iface_prefix.c new file +173 -0

173 lines not on this page · raw message

testcases/lib/tst_net_ip_prefix.c new file +116 -0

116 lines not on this page · raw message

testcases/lib/tst_net_vars.c new file +655 -0

655 lines not on this page · raw message

Signature · 1 line
2.13.2
#29 lore raw

RFC PATCH v7 10/11 network: Add tools for setup IP related environment variables

Quoted 10 lines · Petr Vorel

New tools:
* tst_net_ip_prefix
Strip prefix from IP address and save both
If no prefix found sets default prefix.

* tst_net_iface_prefix
tst_net_iface_prefix reads prefix and interface from rtnetlink.

* tst_net_vars takes input of local and remote address and setup most of test link (IP related) environment variables.

Have you checked my comments for the previous version?

Quoted 478 lines

NOTABLE CHANGES IN ENVIRONMENT VARIABLES:
Network variables are defined for both local and remote:
IPV4_NETWORK was replaced by IPV4_LNETWORK and IPV4_RNETWORK IPV6_NETWORK was replaced by IPV6_LNETWORK and IPV6_RNETWORK

NEW VARIABLES:
IPV4_LHOST, IPV4_RHOST, IPV4_LNETMASK, IPV4_RNETMASK, IPV4_LPREFIX, IPV4_RPREFIX, IPV6_LHOST, IPV6_RHOST, IPV6_LNETMASK, IPV6_RNETMASK, IPV6_LPREFIX, IPV6_RPREFIX,

For full list of environment variables which tools setup run:
tst_net_ip_prefix -h
tst_net_iface_prefix -h
tst_net_vars -h

Thanks a lot to Alexey Kodanev who reviewed many versions of this patch and give feedback and pointed out errors, as well as to Cyril Hrubis who reviewed orignal version of tst_net_vars.c.

---
 include/tst_net.h                    | 140 ++++++++
 testcases/lib/.gitignore             |   3 +
 testcases/lib/Makefile               |   2 +-
 testcases/lib/tst_net_iface_prefix.c | 173 +++++++++
 testcases/lib/tst_net_ip_prefix.c    | 116 +++++++
 testcases/lib/tst_net_vars.c         | 655 +++++++++++++++++++++++++++++++++++
 6 files changed, 1088 insertions(+), 1 deletion(-)
 create mode 100644 include/tst_net.h
 create mode 100644 testcases/lib/tst_net_iface_prefix.c
 create mode 100644 testcases/lib/tst_net_ip_prefix.c
 create mode 100644 testcases/lib/tst_net_vars.c
include/tst_net.h new file +140 -0
new file mode 100644
index 000000000..07ca79825
@@ -0,0 +1,140 @@
+/*
+ * Copyright (c) 2017 Petr Vorel <[email protected]>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <arpa/inet.h>
+#include <errno.h>
+
+#define MAX_IPV4_PREFIX 32
+#define MAX_IPV6_PREFIX 128
+
+#define tst_res_comment(...) { \
+	fprintf(stderr, "# "); \
+	tst_res(__VA_ARGS__); } \
+
+
+#define tst_brk_comment(...) { \
+	fprintf(stderr, "# "); \
+	tst_brk(__VA_ARGS__); } \
+
+static inline void print_svar(const char *name, const char *val)
+{
+	if (name && val)
+		printf("export %s=\"%s\"\n", name, val);
+}
+
+static inline void print_svar_change(const char *name, const char *val)
+{
+	if (name && val)
+		printf("export %s=\"${%s:-%s}\"\n", name, name, val);
+}
+
+/*
+ * Function bit_count is from ipcalc project, ipcalc.c.
+ */
+static int bit_count(uint32_t i)
+{
+	int c = 0;
+	unsigned int seen_one = 0;
+
+	while (i > 0) {
+		if (i & 1) {
+			seen_one = 1;
+			c++;
+		} else {
+			if (seen_one)
+				return -1;
+		}
+		i >>= 1;
+	}
+
+	return c;
+}
+
+/*
+ * Function mask2prefix is from ipcalc project, ipcalc.c.
+ */
+static int mask2prefix(struct in_addr mask)
+{
+	return bit_count(ntohl(mask.s_addr));
+}
+
+/*
+ * Function ipv4_mask_to_int is from ipcalc project, ipcalc.c.
+ */
+static int ipv4_mask_to_int(const char *prefix)
+{
+	int ret;
+	struct in_addr in;
+
+	ret = inet_pton(AF_INET, prefix, &in);
+	if (ret == 0)
+		return -1;
+
+	return mask2prefix(in);
+}
+
+/*
+ * Function safe_atoi is from ipcalc project, ipcalc.c.
+ */
+static int safe_atoi(const char *s, int *ret_i)
+{
+	char *x = NULL;
+	long l;
+
+	errno = 0;
+	l = strtol(s, &x, 0);
+
+	if (!x || x == s || *x || errno)
+		return errno > 0 ? -errno : -EINVAL;
+
+	if ((long)(int)l != l)
+		return -ERANGE;
+
+	*ret_i = (int)l;
+
+	return 0;
+}
+
+/*
+ * Function get_prefix use code from ipcalc project, str_to_prefix/ipcalc.c.
+ */
+static int get_prefix(const char *ip_str, int is_ipv6)
+{
+	char *prefix_str = NULL;
+	int prefix = -1, r;
+
+	prefix_str = strchr(ip_str, '/');
+	if (!prefix_str)
+		return -1;
+
+	*(prefix_str++) = '\0';
+
+	if (!is_ipv6 && strchr(prefix_str, '.'))
+		prefix = ipv4_mask_to_int(prefix_str);
+	else {
+		r = safe_atoi(prefix_str, &prefix);
+		if (r != 0)
+			tst_brk(TBROK, "conversion error");
+	}
+
+	if (prefix < 0 || ((is_ipv6 && prefix > MAX_IPV6_PREFIX) ||
+		(!is_ipv6 && prefix > MAX_IPV4_PREFIX)))
+		tst_brk(TBROK, "bad %s prefix: %s", is_ipv6 ? "IPv6" : "IPv4",
+			prefix_str);
+
+	return prefix;
+}
testcases/lib/.gitignore +3 -0
index 522889bed..f36ed99fa 100644
@@ -4,3 +4,6 @@
 /tst_rod
 /tst_kvcmp
 /tst_device
+/tst_net_iface_prefix
+/tst_net_ip_prefix
+/tst_net_vars
testcases/lib/Makefile +1 -1
index 1127a59fe..398150ae0 100644
@@ -27,6 +27,6 @@ include $(top_srcdir)/include/mk/testcases.mk
 INSTALL_TARGETS		:= *.sh
 
 MAKE_TARGETS		:= tst_sleep tst_random tst_checkpoint tst_rod tst_kvcmp\
-			   tst_device
+			   tst_device tst_net_iface_prefix tst_net_ip_prefix tst_net_vars
 
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/lib/tst_net_iface_prefix.c new file +173 -0
new file mode 100644
index 000000000..7a90b1d9e
@@ -0,0 +1,173 @@
+/*
+ * Copyright (c) 2017 Petr Vorel <[email protected]>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <linux/rtnetlink.h>
+#include <net/if.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#define TST_NO_DEFAULT_MAIN
+#include "tst_test.h"
+
+#include "tst_net.h"
+
+typedef struct data {
+	char *ifname;
+	int prefix;
+} data;
+
+static data vars;
+
+static void usage(const char *cmd)
+{
+	fprintf(stderr, "USAGE:\n"
+		"%s IPV4_LHOST/IPV4_LPREFIX\n"
+		"%s -r IPV4_RHOST/IPV4_RPREFIX\n"
+		"%s IPV6_LHOST/IPV6_LPREFIX\n"
+		"%s -r IPV6_RHOST/IPV6_RPREFIX\n\n"
+		"%s IPV4_LHOST\n"
+		"%s -r IPV4_RHOST\n\n"
+		"%s IPV6_LHOST\n"
+		"%s -r IPV6_RHOST\n\n"
+		"%s -h\n\n"
+		"Set prefix and interface name for given IP.\n"
+		"Tries to find prefix and interface from kernel exported info (rtnetlink).\n\n"
+		"If prefix is not provided, is tried to be detected from rtnetlink data for local IP address, which is used for remote prefix as well. If is not found use default values.\n"
+		"EXPORTED VARIABLES:\n"
+		"Export one of following variables:\n"
+		"Export one of following variables:\n"
+		"IPV4_LPREFIX: IPv4 prefix for IPV4_LNETWORK\n"
+		"IPV4_RPREFIX: IPv4 prefix for IPV4_RNETWORK\n"
+		"IPV6_LPREFIX: IPv6 prefix for IPV6_LNETWORK\n"
+		"IPV6_RPREFIX: IPv6 prefix for IPV6_RNETWORK\n"
+		"Export one of following variables (if found):\n"
+		"LHOST_IFACES: iface name of the local host (*)\n"
+		"RHOST_IFACES: iface name of the remote host (*)\n\n"
+		"(*): respect user settings\n"
+		"Iface (LHOST_IFACES, RHOST_IFACES) as user might want to use more ifaces.\n\n"
+		"PARAMS:\n"
+		"-h this help\n"
+		"-r export remote environment variables\n",
+		cmd, cmd, cmd, cmd, cmd, cmd, cmd, cmd, cmd);
+}
+
+static int read_iface_prefix(const char *ip_str, int is_ipv6)
+{
+	uint8_t family = is_ipv6 ? AF_INET6 : AF_INET;
+
+	char buf[16384];
+	int len;
+
+	struct {
+		struct nlmsghdr nlhdr;
+		struct ifaddrmsg addrmsg;
+	} msg;
+
+	struct nlmsghdr *retmsg;
+
+	int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
+
+	memset(&msg, 0, sizeof(msg));
+	msg.nlhdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
+	msg.nlhdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT;
+	msg.nlhdr.nlmsg_type = RTM_GETADDR;
+	msg.addrmsg.ifa_family = family;
+
+	send(sock, &msg, msg.nlhdr.nlmsg_len, 0);
+	len = recv(sock, buf, sizeof(buf), 0);
+	retmsg = (struct nlmsghdr *)buf;
+
+	while NLMSG_OK(retmsg, len) {
+		char ifname[IFNAMSIZ];
+		struct ifaddrmsg *retaddr;
+		struct rtattr *retrta;
+		char pradd[128];
+		int attlen;
+
+		retaddr = (struct ifaddrmsg *)NLMSG_DATA(retmsg);
+		retrta = (struct rtattr *)IFA_RTA(retaddr);
+		attlen = IFA_PAYLOAD(retmsg);
+
+		while RTA_OK(retrta, attlen) {
+			if (retrta->rta_type == IFA_ADDRESS) {
+				inet_ntop(family, RTA_DATA(retrta), pradd,
+					  sizeof(pradd));
+
+				if_indextoname(retaddr->ifa_index, ifname);
+
+				if (!strcmp(pradd, ip_str)) {
+					vars.prefix = retaddr->ifa_prefixlen;
+					vars.ifname = strdup(ifname);
+					return 0;
+				}
+			}
+			retrta = RTA_NEXT(retrta, attlen);
+
+		}
+		retmsg = NLMSG_NEXT(retmsg, len);
+	}
+
+	return -1;
+}
+
+static void print_ivar(const char *name, unsigned int val)
+{
+	if (name)
+		printf("export %s=%d\n", name, val);
+}
+
+int main(int argc, char *argv[])
+{
+	char *ip_str = NULL, *prefix_str = NULL;
+	int is_ipv6, is_rhost = 0;
+
+	int is_usage = argc > 1 && (!strcmp(argv[1], "-h") ||
+		!strcmp(argv[1], "--help"));
+	if (argc < 2 || is_usage) {
+		usage(argv[0]);
+		exit(is_usage ? EXIT_SUCCESS : EXIT_FAILURE);
+	}
+	if (!strcmp(argv[1], "-r"))
+		is_rhost = 1;
+
+	ip_str = argv[is_rhost ? 2 : 1];
+	is_ipv6 = !!strchr(ip_str, ':');
+
+	prefix_str = strchr(ip_str, '/');
+	if (prefix_str) {
+		vars.prefix = get_prefix(ip_str, is_ipv6);
+		tst_res_comment(TINFO,
+			"IP address '%s' contains prefix %d, using it and don't search for iface.\n",
+			ip_str, vars.prefix);
+	} else if (read_iface_prefix(ip_str, is_ipv6)) {
+		tst_res_comment(TINFO,
+			"prefix and interface not found for '%s'.\n", ip_str);
+		exit(EXIT_SUCCESS);
+	}
+
+	if (is_ipv6) {
+		print_svar_change("LHOST_IFACES", vars.ifname);
+		print_ivar(is_rhost ? "IPV6_RPREFIX" : "IPV6_LPREFIX",
+			vars.prefix);
+	} else {
+		print_svar_change("RHOST_IFACES", vars.ifname);
+		print_ivar(is_rhost ? "IPV4_RPREFIX" : "IPV4_LPREFIX",
+			vars.prefix);
+	}
+
+	exit(EXIT_SUCCESS);
+}
testcases/lib/tst_net_ip_prefix.c new file +92 -0
new file mode 100644
index 000000000..19a3929bb
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2017 Petr Vorel <[email protected]>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <linux/rtnetlink.h>
+#include <net/if.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#define TST_NO_DEFAULT_MAIN
+#include "tst_test.h"
+
+#include "tst_net.h"
+
+#define DEFAULT_IPV4_PREFIX 24
+#define DEFAULT_IPV6_PREFIX 64
+
+typedef struct data {
+	int prefix;
+} data;
+
+static data vars;
+
+static void usage(const char *cmd)
+{
+	fprintf(stderr, "USAGE:\n"
+		"%s IPV4_LHOST/IPV4_LPREFIX\n"
+		"%s -r IPV4_RHOST/IPV4_RPREFIX\n"
+		"%s IPV6_LHOST/IPV6_LPREFIX\n"
+		"%s -r IPV6_RHOST/IPV6_RPREFIX\n\n"
+		"%s IPV4_LHOST\n"
+		"%s -r IPV4_RHOST\n\n"
+		"%s IPV6_LHOST\n"
+		"%s -r IPV6_RHOST\n\n"
+		"%s -h\n\n"
+		"Set IP without prefix and prefix for given IP.\n"
+		"If prefix is not provided, is tried to be detected from rtnetlink data for local IP address, which is used for remote prefix as well. If is not found use default values.\n"
+		"EXPORTED VARIABLES:\n"
+		"Export one of following variables:\n"
+		"IPV4_LHOST: IPv4 address of the local host (without /prefix)\n"
+		"IPV4_RHOST: IPv4 address of the remote host (without /prefix)\n"
+		"IPV6_LHOST: IPv6 address of the local host (without /prefix)\n"
+		"IPV6_RHOST: IPv6 address of the remote host (without /prefix)\n"
+		"Export one of following variables:\n"
+		"IPV4_LPREFIX: IPv4 prefix for IPV4_LNETWORK\n"
+		"IPV4_RPREFIX: IPv4 prefix for IPV4_RNETWORK\n"
+		"IPV6_LPREFIX: IPv6 prefix for IPV6_LNETWORK\n"
+		"IPV6_RPREFIX: IPv6 prefix for IPV6_RNETWORK\n"
+		"Export one of following variables (if found):\n"
+		"Default IPv4 prefix: %d.\n"
+		"Default IPv6 prefix: %d.\n\n"
+		"PARAMS:\n"
+		"-h this help\n"
+		"-r export remote environment variables\n",
+		cmd, cmd, cmd, cmd, cmd, cmd, cmd, cmd, cmd,
+		DEFAULT_IPV4_PREFIX, DEFAULT_IPV6_PREFIX);
+}
+
+static void print_ivar(const char *name, unsigned int val)
+{
+	if (name)
+		printf("export %s=%d\n", name, val);
+}
+
+int main(int argc, char *argv[])
+{
+	char *ip_str = NULL, *prefix_str = NULL;
+	int is_ipv6, is_rhost = 0;
+
+	int is_usage = argc > 1 && (!strcmp(argv[1], "-h") ||
+		!strcmp(argv[1], "--help"));
+	if (argc < 2 || is_usage) {
+		usage(argv[0]);
+		exit(is_usage ? EXIT_SUCCESS : EXIT_FAILURE);
+	}
+	if (!strcmp(argv[1], "-r"))
+		is_rhost = 1;
+
+	ip_str = argv[is_rhost ? 2 : 1];

testcases/lib/tst_net_ip_prefix.c line 102

Quoted 10 lines
testcases/lib/tst_net_ip_prefix.c new file +10 -0
+	is_ipv6 = !!strchr(ip_str, ':');
+
+	prefix_str = strchr(ip_str, '/');
+	if (prefix_str)
+		vars.prefix = get_prefix(ip_str, is_ipv6);
+	else {
+		vars.prefix = is_ipv6 ? DEFAULT_IPV6_PREFIX : DEFAULT_IPV4_PREFIX;
+		tst_res_comment(TBROK,
+				"prefix and interface not found for '%s'. Using default value %d.\n",
+				ip_str, vars.prefix);

The message is misleading, after tst_net_ip_prefix the script callstst_net_iface_prefix.So better remove it, looks like it will be printed every time if you relies on auto configuration.

Quoted 600 lines
testcases/lib/tst_net_ip_prefix.c new file +14 -0
+	}
+
+	if (is_ipv6) {
+		print_ivar(is_rhost ? "IPV6_RPREFIX" : "IPV6_LPREFIX",
+			vars.prefix);
+		print_svar(is_rhost ? "IPV6_RHOST" : "IPV6_LHOST", ip_str);
+	} else {
+		print_ivar(is_rhost ? "IPV4_RPREFIX" : "IPV4_LPREFIX",
+			vars.prefix);
+		print_svar(is_rhost ? "IPV4_RHOST" : "IPV4_LHOST", ip_str);
+	}
+
+	exit(EXIT_SUCCESS);
+}
testcases/lib/tst_net_vars.c new file +580 -0
new file mode 100644
index 000000000..a4b58951c
@@ -0,0 +1,655 @@
+/*
+ * Copyright (c) 2017 Petr Vorel <[email protected]>
+ * Copyright (c) 1997-2015 Red Hat, Inc. All rights reserved.
+ * Copyright (c) 2011-2013 Rich Felker, et al.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <arpa/inet.h>
+#include <arpa/nameser.h>
+#include <assert.h>
+#include <errno.h>
+#include <linux/rtnetlink.h>
+#include <net/if.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <stdarg.h>
+
+
+#define TST_NO_DEFAULT_MAIN
+#include "tst_test.h"
+
+#include "tst_net.h"
+
+#define BASE_IPV4_PREFIX 8
+#define BASE_IPV6_PREFIX 16
+
+#define MAX_IPV4_PREFIX 32
+#define MAX_IPV6_PREFIX 128
+
+#define DEFAULT_IPV4_UNUSED_PART1 10
+#define DEFAULT_IPV6_UNUSED_PART1 0xfd
+
+#define DEFAULT_IPV4_UNUSED_PART2 23
+#define DEFAULT_IPV6_UNUSED_PART2 0x23
+
+typedef struct ltp_net_variables {
+	char *ipv4_lhost;
+	char *ipv4_rhost;
+	char *ipv4_lbroadcast;
+	char *ipv4_rbroadcast;
+	char *ipv4_lnetmask;
+	char *ipv4_rnetmask;
+	char *ipv4_lnetwork;
+	char *ipv4_rnetwork;
+	char *lhost_ipv4_host;
+	char *rhost_ipv4_host;
+	char *ipv6_lhost;
+	char *ipv6_rhost;
+	char *ipv6_lnetmask;
+	char *ipv6_rnetmask;
+	char *ipv6_lnetwork;
+	char *ipv6_rnetwork;
+	char *lhost_ipv6_host;
+	char *rhost_ipv6_host;
+	char *ipv4_net16_unused;
+	char *ipv6_net32_unused;
+	char *lhost_ifaces;
+} ltp_net_variables;
+
+
+static ltp_net_variables vars;
+
+static void usage(const char *cmd)
+{
+	fprintf(stderr, "USAGE:\n"
+		"%s IPV4_LHOST/IPV4_LPREFIX IPV4_RHOST/IPV4_RPREFIX\n"
+		"%s IPV6_LHOST/IPV6_LPREFIX IPV6_RHOST/IPV6_RPREFIX\n\n"
+		"%s IPV4_LHOST IPV4_RHOST\n"
+		"%s IPV6_LHOST IPV6_RHOST\n"
+		"%s -h\n\n"
+		"IP addresses must be different\n"
+		"Exported variables:\n"
+		"IPV4_LHOST: IPv4 address of the local host (1st parameter)\n"
+		"IPV4_RHOST: IPv4 address of the remote host (2nd parameter)\n"
+		"IPV4_LBROADCAST: IPv4 broadcast of the local host\n"
+		"IPV4_RBROADCAST: IPv4 broadcast of the remote host\n"
+		"IPV4_LNETMASK: IPv4 netmask of the local host\n"
+		"IPV4_RNETMASK: IPv4 netmask of the remote host\n"
+		"IPV4_LNETWORK: IPv4 network part of IPV4_LHOST\n"
+		"IPV4_RNETWORK: IPv4 network part of IPV4_RHOST\n"
+		"LHOST_IPV4_HOST IPv4 host part of IPV4_LHOST\n"
+		"RHOST_IPV4_HOST IPv4 host part of IPV4_RHOST\n"
+		"IPV4_NET16_UNUSED: IPv4 16 bit unused subnet\n"
+		"IPV6_LHOST: IPv6 address of the local host (1st parameter)\n"
+		"IPV6_RHOST: IPv6 address of the remote host (2nd parameter)\n"
+		"IPV6_LNETMASK: IPv6 netmask of the local host\n"
+		"IPV6_RNETMASK: IPv6 netmask of the remote host (*)\n"
+		"IPV6_LNETWORK: IPv6 network part of IPV6_LHOST\n"
+		"IPV6_RNETWORK: IPv6 network part of IPV6_LHOST\n"
+		"LHOST_IPV6_HOST: IPv6 unique part of IPV6_LHOST\n"
+		"RHOST_IPV6_HOST: IPv6 unique part of IPV6_RHOST\n"
+		"IPV6_NET32_UNUSED: IPv6 32 bit unused subnet\n"
+		"(*): respect user settings:\n"
+		"NOTE: Prefixes, ifaces and lhosts are expected to be set by tst_net_iface_prefix.\n"
+		"OPTIONS:\n"
+		"-h this help\n",
+		cmd, cmd, cmd, cmd, cmd);
+}
+
+static void get_in_addr(const char *ip_str, struct in_addr *ip)
+{
+	if (inet_pton(AF_INET, ip_str, ip) <= 0)
+		tst_brk_comment(TCONF, "bad IPv4 address: '%s'", ip_str);
+}
+
+static void get_in6_addr(const char *ip_str, struct in6_addr *ip6)
+{
+	if (inet_pton(AF_INET6, ip_str, ip6) <= 0)
+		tst_brk_comment(TCONF, "bad IPv6 address: '%s'", ip_str);
+}
+
+/*
+ * Function prefix2mask is from ipcalc project, ipcalc.c.
+ */
+static struct in_addr prefix2mask(unsigned int prefix)
+{
+	struct in_addr mask = {0};
+
+	if (prefix)
+		mask.s_addr = htonl(~((1 << (32 - prefix)) - 1));
+	else
+		mask.s_addr = htonl(0);
+
+	return mask;
+}
+
+/*
+ * Function calc_network is based on ipcalc project,
+ * calc_network/ipcalc.c.
+ */
+static struct in_addr calc_network(const struct in_addr *ip, struct in_addr *mask)
+{
+	struct in_addr network = {0};
+
+	network.s_addr = ip->s_addr & mask->s_addr;
+	return network;
+}
+
+static int is_in_subnet_ipv4(const struct in_addr *network,
+	const struct in_addr *mask, const struct in_addr *ip)
+{
+	return (ip->s_addr & mask->s_addr) ==
+		(network->s_addr & mask->s_addr);
+}
+
+static int is_in_subnet_ipv6(const struct in6_addr *network,
+	const struct in6_addr *mask, const struct in6_addr *ip6)
+{
+	unsigned int i;
+
+	for (i = 0; i < sizeof(struct in6_addr) / sizeof(int); i++) {
+		if (((((int *) ip6)[i] & ((int *) mask)[i])) !=
+			(((int *) network)[i] & ((int *) mask)[i]))
+			return 0;
+	}
+	return 1;
+}
+
+/*
+ * Function get_ipv4_netmask uses code from calc_network, which is from
+ * ipcalc project, ipcalc.c.
+ */
+static char *get_ipv4_netmask(unsigned int prefix)
+{
+	char buf[INET_ADDRSTRLEN + 1];
+	struct in_addr mask = prefix2mask(prefix);
+
+	if (prefix > MAX_IPV4_PREFIX)
+		return NULL;
+
+	if (!inet_ntop(AF_INET, &mask, buf, sizeof(buf)))
+		tst_brk_comment(TCONF, "error calculating IPv4 address");
+
+	return strdup(buf);
+}
+
+/*
+ * Function get_ipv4_netmask uses code from ipv6_prefix_to_mask and
+ * ipv6_mask_to_str, which are from ipcalc project, ipcalc.c.
+ */
+static char *get_ipv6_netmask(unsigned int prefix)
+{
+	struct in6_addr in6;
+	char buf[128];
+	int i, j;
+
+	if (prefix > MAX_IPV6_PREFIX)
+		return NULL;
+
+	memset(&in6, 0x0, sizeof(in6));
+	for (i = prefix, j = 0; i > 0; i -= 8, j++) {
+		if (i >= 8)
+			in6.s6_addr[j] = 0xff;
+		else
+			in6.s6_addr[j] = (unsigned long)(0xffU << (8 - i));
+	}
+
+	if (!inet_ntop(AF_INET6, &in6, buf, sizeof(buf)))
+		tst_brk_comment(TCONF, "error calculating IPv6 address");
+
+	return strdup(buf);
+}
+
+/*
+ * Function get_ipv4_broadcast uses code from calc_broadcast, which is from
+ * ipcalc project, ipcalc.c.
+ */
+static char *get_ipv4_broadcast(struct in_addr ip, unsigned int prefix)
+{
+	struct in_addr mask = prefix2mask(prefix);
+	struct in_addr broadcast;
+	char buf[INET_ADDRSTRLEN + 1];
+
+	memset(&broadcast, 0, sizeof(broadcast));
+	broadcast.s_addr = (ip.s_addr & mask.s_addr) | ~mask.s_addr;
+
+	if (!inet_ntop(AF_INET, &broadcast, buf, sizeof(buf)))
+		tst_brk_comment(TCONF, "error calculating IPv4 address");
+
+	return strdup(buf);
+}
+
+/*
+ * For unused network we use
+ * DEFAULT_IPV4_UNUSED_PART1:DEFAULT_IPV4_UNUSED_PART2 or
+ * {DEFAULT_IPV4_UNUSED_PART1}.XY, when there is a collision with IP.
+ */
+static char *get_ipv4_net16_unused(const struct in_addr *ip,
+	unsigned int prefix)
+{
+	struct in_addr mask, network;
+	char buf[128], net_unused[128];
+
+	mask = prefix2mask(prefix);
+	network = calc_network(ip, &mask);
+
+	sprintf(net_unused, "%d.%d", DEFAULT_IPV4_UNUSED_PART1,
+			DEFAULT_IPV4_UNUSED_PART2);
+	sprintf(buf, "%s.0.0", net_unused);
+
+	get_in_addr(buf, &network);
+
+	if (!is_in_subnet_ipv4(ip, &mask, &network))
+		return strdup(net_unused);
+
+	srand(time(NULL));
+
+	/* try to randomize second group */
+	sprintf(net_unused, "%d.%d", DEFAULT_IPV4_UNUSED_PART1,
+		(rand() % 128) + (((ip->s_addr >> 8) & 0xff) < 128 ? 128 : 0));
+	sprintf(buf, "%s.0.0", net_unused);
+
+	get_in_addr(buf, &network);
+
+	if (!is_in_subnet_ipv4(ip, &mask, &network))
+		return strdup(net_unused);
+
+	/* try to randomize first group */
+	sprintf(net_unused, "%d.%d", (rand() % 128) + (((ip->s_addr) & 0xff)
+			< 128 ? 128 : 0), DEFAULT_IPV4_UNUSED_PART2);
+	sprintf(buf, "%s.0.0", net_unused);
+
+	get_in_addr(buf, &network);
+
+	if (!is_in_subnet_ipv4(ip, &mask, &network))
+		return strdup(net_unused);
+
+	return NULL;
+}
+
+/*
+ * Function get_ipv6_net32_unused is inspired by ipcalc project,
+ * get_ipv6_info/ipcalc.c.
+ *
+ * For unused network we use DEFAULT_IPV6_UNUSED_PART1:DEFAULT_IPV6_UNUSED_PART2
+ * if no collision with existing IP range.
+ * Otherwise we try to use
+ * {DEFAULT_IPV6_UNUSED_PART1}XY:DEFAULT_IPV6_UNUSED_PART2 or
+ * XY:DEFAULT_IPV6_UNUSED_PART2.
+ */
+static char *get_ipv6_net32_unused(const struct in6_addr *ip6,
+	unsigned int prefix)
+{
+	int i, j;
+	struct in6_addr mask = {0}, network;
+	char buf[128], net_unused[128];
+
+	if (prefix > 128)
+		return NULL;
+
+
+	for (i = prefix, j = 0; i > 0; i -= 8, j++) {
+		if (i >= 8)
+			mask.s6_addr[j] = 0xff;
+		else
+			mask.s6_addr[j] = (unsigned long)(0xffU << (8 - i));
+	}
+
+	sprintf(net_unused, "%x:%x", 256 * DEFAULT_IPV6_UNUSED_PART1,
+			DEFAULT_IPV6_UNUSED_PART2);
+	sprintf(buf, "%s::", net_unused);
+
+	get_in6_addr(buf, &network);
+
+	if (!is_in_subnet_ipv6(ip6, &mask, &network))
+		return strdup(net_unused);
+
+	srand(time(NULL));
+
+	/* try to randomize second group */
+	sprintf(net_unused, "%x:%x", 256 * DEFAULT_IPV6_UNUSED_PART1 +
+			(rand() % 128) + (ip6->s6_addr[1] < 128 ? 128 : 0),
+			DEFAULT_IPV6_UNUSED_PART2);
+	sprintf(buf, "%s::", net_unused);
+
+	get_in6_addr(buf, &network);
+
+	if (!is_in_subnet_ipv6(ip6, &mask, &network))
+		return strdup(net_unused);
+
+	/* try to randomize first group */
+	sprintf(net_unused, "%x:%x",
+			256 * (rand() % 128) + (256 * ip6->s6_addr[0] < 128 ?
+			128 : 0), DEFAULT_IPV6_UNUSED_PART2);
+	sprintf(buf, "%s::", net_unused);
+
+	get_in6_addr(buf, &network);
+
+	if (!is_in_subnet_ipv6(ip6, &mask, &network))
+		return strdup(net_unused);
+
+	return NULL;
+}
+
+/*
+ * Function get_ipv6_network is based on musl libc project,
+ * inet_ntop/inet_ntop.c.
+ */
+static char *get_ipv6_network(const unsigned char *a0, unsigned int prefix)
+{
+	const unsigned char *a = a0;
+	unsigned int i, j, max, best, border = 0;
+	char buf[100];
+	char ret[100];
+	char tmp[100];
+	char *p_ret = ret;
+	char *p_tmp = tmp;
+	size_t offset;
+
+	if (prefix > MAX_IPV6_PREFIX)
+		return NULL;
+
+	if (prefix == MAX_IPV6_PREFIX)
+		return strdup("\0");
+
+	snprintf(buf, sizeof(buf),
+		"%x:%x:%x:%x:%x:%x:%x:%x",
+		256 * a[0] + a[1], 256 * a[2] + a[3],
+		256 * a[4] + a[5], 256 * a[6] + a[7],
+		256 * a[8] + a[9], 256 * a[10] + a[11],
+		256 * a[12] + a[13], 256 * a[14] + a[15]);
+
+	for (i = 0; i < 8; i++) {
+		if (i < prefix >> 4) {
+			border += sprintf(p_tmp, "%x", 256 * a[2 * i] +
+				a[2 * i + 1]);
+			if (i > 0)
+				border++;
+		}
+
+		if (i >= prefix >> 4)
+			break;
+
+		/* ':' only if no leading in host or ending in net */
+		if (i > 0)
+			*p_ret++ = ':';
+
+		offset = sprintf(p_ret, "%x", 256 * a[2 * i] + a[2 * i + 1]);
+		p_ret += offset;
+	}
+
+	*p_ret = '\0';
+
+	/* Find longest /(^0|:)[:0]{2,}/ */
+	for (i = best = 0, max = 2; buf[i]; i++) {
+		if (i && buf[i] != ':')
+			continue;
+		j = strspn(buf + i, ":0");
+
+		if (j > max)
+			best = i, max = j;
+	}
+
+	size_t length = strlen(ret);
+	size_t best_end = best + max - 1;
+
+	if (max > 2 && best < border) {
+		p_ret = ret;
+		/* Replace longest /(^0|:)[:0]{2,}/ with "::" */
+		if (best == 0 && best_end >= border) {
+			/* zeros in whole net part or continue to host */
+			ret[0] = ':';
+			ret[1] = '\0';
+		} else if (best == 0 && best_end < border) {
+			/* zeros on beginning, not whole part */
+			ret[0] = ':';
+			memmove(p_ret + 1, p_ret + best_end, border - best_end
+				+ 1);
+		} else if (best > 0 && best_end >= border) {
+			/* zeros not from beginning to border or continue to host */
+			ret[best] = ':';
+			ret[best + 1] = '\0';
+		} else {
+			/* zeros somewhere in the middle */
+			ret[best] = ':';
+			memmove(p_ret + best + 1, p_ret + best_end,
+					border - best + 1);
+		}
+	}
+
+	if (length < INET6_ADDRSTRLEN)
+		return strdup(ret);
+
+	return NULL;
+}
+
+/*
+ * Strip host part from ip address.
+ */
+static char *get_host_from_ip(const char *ip, const char *net)
+{
+	if (ip == NULL || net == NULL)
+		return NULL;
+
+	char *result = strstr(ip, net);
+
+	if (!result || result != ip)
+		return NULL;
+
+	char *buf = strdup(ip);
+	unsigned int index = strlen(net);
+
+	/* prefix < 8 (IPv4) or 128 (IPv6) */
+	if (index == strlen(ip))
+		return strdup("\0");
+
+	/* prefix > 0 && prefix < 32 (IPv4) or 128 (IPv6) */
+	if (index > 0 && index < strlen(ip)) {
+		int len = strlen(ip) - index - 1;
+		assert(ip[index] == ':' || ip[index] == '.');
+		memmove(buf, buf + index + 1, len);
+		buf[len] = '\0';
+	}
+
+	return buf;
+}
+
+static void check_prefix_range(unsigned int prefix, int is_ipv6, int is_lhost)
+{
+	unsigned int base_prefix = is_ipv6 ? BASE_IPV6_PREFIX :
+		BASE_IPV4_PREFIX;
+	unsigned int max_prefix = is_ipv6 ? MAX_IPV6_PREFIX : MAX_IPV4_PREFIX;
+
+	if (prefix < base_prefix || (is_ipv6 && prefix == 128) ||
+		(!is_ipv6 && prefix == 32))
+		tst_res_comment(TWARN,
+			"prefix %d for %s will be unsuitable for some stress tests which needs %s variable."
+			" To avoid this use prefix >= %d and prefix < %d.",
+			prefix, is_ipv6 ?  "IPv6" : "IPv4",
+			is_ipv6 ? (is_lhost ? "IPV6_LNETWORK" : "IPV6_RNETWORK")
+				: (is_lhost ? "IPV4_LNETWORK" : "IPV4_RNETWORK"),
+			base_prefix, max_prefix);
+}
+
+static char *get_ipv4_network(int ip, unsigned int prefix)
+{
+	char buf[INET_ADDRSTRLEN + 1];
+	char *p_buf = buf;
+	unsigned char byte;
+	unsigned int i;
+
+	if (prefix > MAX_IPV4_PREFIX)
+		return NULL;
+
+	if (prefix == MAX_IPV4_PREFIX)
+		return strdup("\0");
+
+	prefix &= 0x18;
+
+	for (i = 0; i < MAX_IPV4_PREFIX && (prefix == 0 || i < prefix); i += 8) {
+		if (i == 0) {
+			byte = ip & 0xff;
+			sprintf(p_buf, "%d", byte);
+		} else {
+			byte = (ip >> i) & 0xff;
+			sprintf(p_buf, ".%d", byte);
+		}
+		p_buf += strlen(p_buf);
+	}
+
+	return strdup(buf);
+}
+
+/*
+ * Round down prefix.
+ */
+static int round_down_prefix(unsigned int prefix, int is_ipv6)
+{
+	unsigned int base_prefix = is_ipv6 ? BASE_IPV6_PREFIX :
+		BASE_IPV4_PREFIX;
+
+	return prefix / base_prefix * base_prefix;
+}
+
+static void get_ipv4_info(const char *lip_str, const char *rip_str, int lprefix,
+	int rprefix)
+{
+	struct in_addr lip, rip;
+	int lprefix_round, rprefix_round;
+
+	lprefix_round = round_down_prefix(lprefix, 0);
+	rprefix_round = round_down_prefix(rprefix, 0);
+
+	get_in_addr(lip_str, &lip);
+	get_in_addr(rip_str, &rip);
+
+	vars.ipv4_lhost = strdup(lip_str);
+	vars.ipv4_rhost = strdup(rip_str);
+
+	vars.ipv4_lbroadcast = get_ipv4_broadcast(lip, lprefix);
+	vars.ipv4_rbroadcast = get_ipv4_broadcast(rip, rprefix);
+
+	vars.ipv4_lnetmask = get_ipv4_netmask(lprefix);
+	vars.ipv4_rnetmask = get_ipv4_netmask(rprefix);
+
+	vars.ipv4_lnetwork = get_ipv4_network(lip.s_addr, lprefix_round);
+	vars.ipv4_rnetwork = get_ipv4_network(rip.s_addr, rprefix_round);
+
+	vars.lhost_ipv4_host = get_host_from_ip(lip_str, vars.ipv4_lnetwork);
+	vars.rhost_ipv4_host = get_host_from_ip(rip_str, vars.ipv4_rnetwork);
+
+	vars.ipv4_net16_unused = get_ipv4_net16_unused(&lip, lprefix_round);
+}
+
+static void get_ipv6_info(const char *lip_str, const char *rip_str,
+	int lprefix, int rprefix)
+{
+	struct in6_addr lip, rip;
+	int lprefix_round, rprefix_round;
+
+	lprefix_round = round_down_prefix(lprefix, 1);
+	rprefix_round = round_down_prefix(rprefix, 1);
+
+	get_in6_addr(lip_str, &lip);
+	get_in6_addr(rip_str, &rip);
+
+	vars.ipv6_lhost = strdup(lip_str);
+	vars.ipv6_rhost = strdup(rip_str);
+
+	vars.ipv6_lnetmask = get_ipv6_netmask(lprefix);
+	vars.ipv6_rnetmask = get_ipv6_netmask(rprefix);
+
+	vars.ipv6_lnetwork = get_ipv6_network(lip.s6_addr, lprefix_round);
+	vars.ipv6_rnetwork = get_ipv6_network(rip.s6_addr, rprefix_round);
+
+	vars.lhost_ipv6_host = get_host_from_ip(lip_str, vars.ipv6_lnetwork);

testcases/lib/tst_net_vars.c line 590

Quoted 10 lines
testcases/lib/tst_net_vars.c new file +10 -0
+	vars.rhost_ipv6_host = get_host_from_ip(rip_str, vars.ipv6_rnetwork);
+
+	vars.ipv6_net32_unused = get_ipv6_net32_unused(&lip, lprefix_round);
+}
+
+static void print_vars(int is_ipv6)
+{
+	if (is_ipv6) {
+		print_svar("IPV6_LHOST", vars.ipv6_lhost);
+		print_svar("IPV6_RHOST", vars.ipv6_rhost);

It is already done by tst_net_ip_prefix

testcases/lib/tst_net_vars.c line 600

Quoted 10 lines
testcases/lib/tst_net_vars.c new file +10 -0
+		print_svar("IPV6_LNETMASK", vars.ipv6_lnetmask);
+		print_svar_change("IPV6_RNETMASK", vars.ipv6_rnetmask);
+		print_svar("IPV6_LNETWORK", vars.ipv6_lnetwork);
+		print_svar("IPV6_RNETWORK", vars.ipv6_rnetwork);
+		print_svar("LHOST_IPV6_HOST", vars.lhost_ipv6_host);
+		print_svar("RHOST_IPV6_HOST", vars.rhost_ipv6_host);
+		print_svar("IPV6_NET32_UNUSED", vars.ipv6_net32_unused);
+	} else {
+		print_svar("IPV4_LHOST", vars.ipv4_lhost);
+		print_svar("IPV4_RHOST", vars.ipv4_rhost);

Here the same.

Quoted 45 lines
testcases/lib/tst_net_vars.c new file +45 -0
+		print_svar("IPV4_LBROADCAST", vars.ipv4_lbroadcast);
+		print_svar_change("IPV4_RBROADCAST", vars.ipv4_rbroadcast);
+		print_svar("IPV4_LNETMASK", vars.ipv4_lnetmask);
+		print_svar_change("IPV4_RNETMASK", vars.ipv4_rnetmask);
+		print_svar("IPV4_LNETWORK", vars.ipv4_lnetwork);
+		print_svar("IPV4_RNETWORK", vars.ipv4_rnetwork);
+		print_svar("LHOST_IPV4_HOST", vars.lhost_ipv4_host);
+		print_svar("RHOST_IPV4_HOST", vars.rhost_ipv4_host);
+		print_svar("IPV4_NET16_UNUSED", vars.ipv4_net16_unused);
+	}
+}
+
+int main(int argc, char *argv[])
+{
+	char *lip_str = NULL, *rip_str = NULL;
+	int is_ipv6, lprefix, rprefix;
+
+	int is_usage = argc > 1 && (!strcmp(argv[1], "-h") ||
+		!strcmp(argv[1], "--help"));
+	if (argc < 3 || is_usage) {
+		usage(argv[0]);
+		exit(is_usage ? EXIT_SUCCESS : EXIT_FAILURE);
+	}
+
+	lip_str = argv[1];
+	rip_str = argv[2];
+
+	is_ipv6 = !!strchr(lip_str, ':');
+	if (is_ipv6 != !(strchr(rip_str, ':') == NULL))
+		tst_brk_comment(TBROK, "mixed IPv4 and IPv6 addresses");
+
+	lprefix = get_prefix(lip_str, is_ipv6);
+	rprefix = get_prefix(rip_str, is_ipv6);
+
+	if (lprefix < 0)
+		tst_brk_comment(TBROK, "wrong lprefix");
+
+	if (rprefix < 0)
+		tst_brk_comment(TBROK, "wrong rprefix");
+
+	check_prefix_range(lprefix, is_ipv6, 1);
+	check_prefix_range(rprefix, is_ipv6, 0);
+
+	if (!strcmp(lip_str, rip_str))
+		tst_brk_comment(TBROK, "IP addresses cannot be the same");

testcases/lib/tst_net_vars.c line 655

Quoted 10 lines
testcases/lib/tst_net_vars.c new file +10 -0
+
+	if (is_ipv6)
+		get_ipv6_info(lip_str, rip_str, lprefix, rprefix);
+	else
+		get_ipv4_info(lip_str, rip_str, lprefix, rprefix);
+
+	print_vars(is_ipv6);
+
+	exit(EXIT_SUCCESS);
+}

Thanks,
Alexey

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20170726/e5527e68/attachment-0001.html>

author
#30 lore raw

RFC PATCH v7 10/11 network: Add tools for setup IP related environment variables

Hi Alexey,

Quoted 6 lines · Petr Vorel
Quoted 4 lines

New tools:
* tst_net_ip_prefix
Strip prefix from IP address and save both
If no prefix found sets default prefix.

Quoted 7 lines
Quoted 5 lines

* tst_net_iface_prefix
tst_net_iface_prefix reads prefix and interface from rtnetlink.

* tst_net_vars takes input of local and remote address and setup most of test link (IP related) environment variables.

Have you checked my comments for the previous version?

Sure :-). I've spent quite some time to think about that and test it, but of course I might overlook something.
Anything particular you don't agree with?

...

Quoted 21 lines
Quoted 21 lines
testcases/lib/tst_net_ip_prefix.c new file +21 -0
+int main(int argc, char *argv[])
+{
+	char *ip_str = NULL, *prefix_str = NULL;
+	int is_ipv6, is_rhost = 0;
+
+	int is_usage = argc > 1 && (!strcmp(argv[1], "-h") ||
+		!strcmp(argv[1], "--help"));
+	if (argc < 2 || is_usage) {
+		usage(argv[0]);
+		exit(is_usage ? EXIT_SUCCESS : EXIT_FAILURE);
+	}
+	if (!strcmp(argv[1], "-r"))
+		is_rhost = 1;
+
+	ip_str = argv[is_rhost ? 2 : 1];
+	is_ipv6 = !!strchr(ip_str, ':');
+
+	prefix_str = strchr(ip_str, '/');
+	if (prefix_str)
+		vars.prefix = get_prefix(ip_str, is_ipv6);
+	else {
Quoted 8 lines

testcases/lib/tst_net_ip_prefix.c line 102

Quoted 4 lines
testcases/lib/tst_net_ip_prefix.c new file +4 -0
+		vars.prefix = is_ipv6 ? DEFAULT_IPV6_PREFIX : DEFAULT_IPV4_PREFIX;
+		tst_res_comment(TBROK,
+				"prefix and interface not found for '%s'. Using default value %d.\n",
+				ip_str, vars.prefix);

The message is misleading, after tst_net_ip_prefix the script calls tst_net_iface_prefix.So better remove it, looks like it will be printed every time if you relies on auto configuration.

OK.

...

Quoted 7 lines

testcases/lib/tst_net_vars.c line 590

Quoted 5 lines
testcases/lib/tst_net_vars.c new file +5 -0
+static void print_vars(int is_ipv6)
+{
+	if (is_ipv6) {
+		print_svar("IPV6_LHOST", vars.ipv6_lhost);
+		print_svar("IPV6_RHOST", vars.ipv6_rhost);

It is already done by tst_net_ip_prefix

Oh, good catch, sorry.

Quoted 4 lines
Quoted 4 lines
testcases/lib/tst_net_vars.c new file +4 -0
+		print_svar("IPV6_LNETMASK", vars.ipv6_lnetmask);
+		print_svar_change("IPV6_RNETMASK", vars.ipv6_rnetmask);
+		print_svar("IPV6_LNETWORK", vars.ipv6_lnetwork);
+		print_svar("IPV6_RNETWORK", vars.ipv6_rnetwork);
Quoted 8 lines

testcases/lib/tst_net_vars.c line 600

Quoted 6 lines
testcases/lib/tst_net_vars.c new file +6 -0
+		print_svar("LHOST_IPV6_HOST", vars.lhost_ipv6_host);
+		print_svar("RHOST_IPV6_HOST", vars.rhost_ipv6_host);
+		print_svar("IPV6_NET32_UNUSED", vars.ipv6_net32_unused);
+	} else {
+		print_svar("IPV4_LHOST", vars.ipv4_lhost);
+		print_svar("IPV4_RHOST", vars.ipv4_rhost);

Here the same.

Thanks for your review!

Kind regards,
Petr

author
#31 lore raw

RFC PATCH v7 11/11 network: Use tools to set up IPv4 and IPv6 related variables

In reply to Petr Vorel

Tools (tst_net_ip_prefix, tst_net_iface_prefix, tst_net_vars) simplify setup as it only needs to have passed 4 IP addresses (IPv4 lhost + rhost and IPv6 lhost + rhost).

It's possible to define different network part for local and remote hosts.

---
 testcases/lib/test_net.sh                          | 94 +++++++++++++++-------
 testcases/network/stress/interface/if4-addr-change | 17 ++--
 2 files changed, 75 insertions(+), 36 deletions(-)
testcases/lib/test_net.sh +63 -31
index cecb0bab2..95510f7cf 100644
@@ -230,19 +230,10 @@ tst_read_opts $*
 tst_ipaddr()
 {
 	local type="${1:-lhost}"
-	local ipv="${TST_IPV6:-4}"
-	local tst_host=
-
-	if [ "$type" = "lhost" ]; then
-		eval "tst_host=\$LHOST_IPV${ipv}_HOST"
-	else
-		eval "tst_host=\$RHOST_IPV${ipv}_HOST"
-	fi
-
 	if [ "$TST_IPV6" ]; then
-		echo "${IPV6_NETWORK}:${tst_host}"
+		[ "$type" = "lhost" ] && echo "$IPV6_LHOST" || echo "$IPV6_RHOST"
 	else
-		echo "${IPV4_NETWORK}.${tst_host}"
+		[ "$type" = "lhost" ] && echo "$IPV4_LHOST" || echo "$IPV4_RHOST"
 	fi
 }
 
@@ -281,9 +272,13 @@ tst_add_ipaddr()
 {
 	local type="${1:-lhost}"
 	local link_num="${2:-0}"
+	local mask
 
-	local mask=24
-	[ "$TST_IPV6" ] && mask=64
+	if [ "$TST_IPV6" ]; then
+		[ "$type" = "lhost" ] && mask=$IPV6_LPREFIX || mask=$IPV6_RPREFIX
+	else
+		[ "$type" = "lhost" ] && mask=$IPV4_LPREFIX || mask=$IPV4_RPREFIX
+	fi
 
 	local iface=$(tst_iface $type $link_num)
 
@@ -529,25 +524,58 @@ export PASSWD="${PASSWD:-}"
 export LTP_RSH="${LTP_RSH:-rsh -n}"
 
 # Test Links
-# Set first three octets of the network address, default is '10.0.0'
-export IPV4_NETWORK="${IPV4_NETWORK:-10.0.0}"
-# Set local host last octet, default is '2'
-export LHOST_IPV4_HOST="${LHOST_IPV4_HOST:-2}"
-# Set remote host last octet, default is '1'
-export RHOST_IPV4_HOST="${RHOST_IPV4_HOST:-1}"
-# Set the reverse of IPV4_NETWORK
-export IPV4_NET_REV="${IPV4_NET_REV:-0.0.10}"
-# Set first three octets of the network address, default is 'fd00:1:1:1'
-export IPV6_NETWORK="${IPV6_NETWORK:-fd00:1:1:1}"
-# Set local host last octet, default is '2'
-export LHOST_IPV6_HOST="${LHOST_IPV6_HOST:-:2}"
-# Set remote host last octet, default is '1'
-export RHOST_IPV6_HOST="${RHOST_IPV6_HOST:-:1}"
-
-# Networks that aren't reachable through the test links
-export IPV4_NET16_UNUSED="${IPV4_NET16_UNUSED:-10.23}"
-export IPV6_NET32_UNUSED="${IPV6_NET32_UNUSED:-fd00:23}"
+# IPV{4,6}_{L,R}HOST can be set with or without prefix (e.g. IP or IP/prefix),
+# but if you use IP/prefix form, /prefix will be removed by tst_net_vars.
+IPV4_LHOST="${IPV4_LHOST:-10.0.0.2/24}"
+IPV4_RHOST="${IPV4_RHOST:-10.0.0.1/24}"
+IPV6_LHOST="${IPV6_LHOST:-fd00:1:1:1::2/64}"
+IPV6_RHOST="${IPV6_RHOST:-fd00:1:1:1::1/64}"
+
+# tst_net_ip_prefix
+# Strip prefix from IP address and save both If no prefix found sets
+# default prefix.
+#
+# tst_net_iface_prefix reads prefix and interface from rtnetlink.
+# If nothing found sets default prefix value.
+#
+# tst_net_vars exports environment variables related to test links and
+# networks that aren't reachable through the test links.
+# Not run for netns (init_ltp_netspace is not called, no links setup yet).
+#
+# For full list of exported environment variables see:
+# tst_net_ip_prefix -h
+# tst_net_iface_prefix -h
+# tst_net_vars -h
+if [ -z "$TST_PARSE_VARIABLES" ]; then
+	tst_resm TINFO "Parsing IP and prefixes"
+	set -x
+	eval "$(tst_net_ip_prefix $IPV4_LHOST)" || exit $?
+	eval "$(tst_net_ip_prefix -r $IPV4_RHOST)" || exit $?
+	eval "$(tst_net_ip_prefix $IPV6_LHOST)" || exit $?
+	eval "$(tst_net_ip_prefix -r $IPV6_RHOST)" || exit $?
+	set +x
+
+	if [ "$TST_USE_NETNS" != "yes" ]; then
+		tst_resm TINFO "Parsing prefixes and ifaces from rtnetlink"
+		set -x
+		eval "$(tst_net_iface_prefix $IPV4_LHOST)" || exit $?
+		eval "$(tst_rhost_run -c 'tst_net_iface_prefix -r '$IPV4_RHOST)" || exit $?
+		eval "$(tst_net_iface_prefix $IPV6_LHOST)" || exit $?
+		eval "$(tst_rhost_run -c 'tst_net_iface_prefix -r '$IPV6_RHOST)" || exit $?
+		set +x
+	fi
 
+	tst_resm TINFO "Parsing all remaining variables"
+	set -x
+	eval "$(tst_net_vars $IPV4_LHOST/$IPV4_LPREFIX $IPV4_RHOST/$IPV4_RPREFIX)" || exit $?
+	eval "$(tst_net_vars $IPV6_LHOST/$IPV6_LPREFIX $IPV6_RHOST/$IPV6_RPREFIX)" || exit $?
+	set +x
+
+	export TST_PARSE_VARIABLES="yes"
+fi
+
+# The rest of variables for networks that aren't reachable through the test links.
+# IPV4_NET16_UNUSED and IPV6_NET32_UNUSED are set by tst_net_vars.
 export OCTET_3_IPV4_UNUSED=${OCTET_3_IPV4_UNUSED:-23} # x.x.N.x
 export OCTET_4_LHOST_IPV4_HOST_UNUSED=${OCTET_4_LHOST_IPV4_HOST_UNUSED:-2} # x.x.x.N
 export OCTET_4_RHOST_IPV4_HOST_UNUSED=${OCTET_4_RHOST_IPV4_HOST_UNUSED:-1} # x.x.x.N
@@ -564,6 +592,7 @@ export RHOST_IPV6_HOST_UNUSED="${OCTET_3_IPV6_UNUSED}:${OCTET_4_RHOST_IPV6_HOST_
 export LHOST_IPV6_UNUSED="${IPV6_NET32_UNUSED}:$LHOST_IPV6_HOST_UNUSED"
 export RHOST_IPV6_UNUSED="${IPV6_NET32_UNUSED}:$RHOST_IPV6_HOST_UNUSED"
 
+# Directories for http and ftp stress tests
 export HTTP_DOWNLOAD_DIR="${HTTP_DOWNLOAD_DIR:-/var/www/html}"
 export FTP_DOWNLOAD_DIR="${FTP_DOWNLOAD_DIR:-/var/ftp}"
 export FTP_UPLOAD_DIR="${FTP_UPLOAD_DIR:-/var/ftp/pub}"
@@ -589,8 +618,11 @@ export MCASTNUM_HEAVY="${MCASTNUM_HEAVY:-4000}"
 
 # Warning: make sure to set valid interface names and IP addresses below.
 # Set names for test interfaces, e.g. "eth0 eth1"
+# This is fallback for LHOST_IFACES in case tst_net_vars finds nothing or we
+# want to use more ifaces.
 export LHOST_IFACES="${LHOST_IFACES:-eth0}"
 export RHOST_IFACES="${RHOST_IFACES:-eth0}"
+
 # Set corresponding HW addresses, e.g. "00:00:00:00:00:01 00:00:00:00:00:02"
 export LHOST_HWADDRS="${LHOST_HWADDRS:-$(tst_get_hwaddrs lhost)}"
 export RHOST_HWADDRS="${RHOST_HWADDRS:-$(tst_get_hwaddrs rhost)}"
testcases/network/stress/interface/if4-addr-change +12 -5
index 567f86643..a7469c252 100644
@@ -48,12 +48,19 @@ while [ $cnt -lt $NS_TIMES ]; do
 
 	[ $num -eq $RHOST_IPV4_HOST ] && continue
 
-	# Change IPv4 address
-	lhost_ipv4addr="${IPV4_NETWORK}.${num}"
+	# check prefix and fix values for prefix != 24
+	add_to_net=
+	if [ $IPV4_LPREFIX -lt 8 -o $IPV4_LPREFIX -ge 32 ] ; then
+		tst_brkm TCONF "test must be with prefix >= 8 and prefix < 32 ($IPV4_LPREFIX)"
+	elif [ $IPV4_LPREFIX -lt 16 ]; then # N.x.x.num
+		add_to_net=".0.1"
+	elif [ $IPV4_LPREFIX -lt 24 ]; then # N.N.x.num
+		add_to_net=".1"
+	fi
 
-	ifconfig $(tst_iface) $lhost_ipv4addr netmask 255.255.255.0 \
-		broadcast 255.255.255.255 || \
-		tst_brkm TFAIL "Failed to change into ${lhost_ipv4addr}"
+	# Change IPv4 address
+	ROD ifconfig $(tst_iface) ${IPV4_LNETWORK}${add_to_net}.${num} netmask \
+		$IPV4_LNETMASK broadcast $IPV4_LBROADCAST
 
 	cnt=$(($cnt + 1))
 
Signature · 1 line
2.13.2
#32 lore raw

RFC PATCH v7 11/11 network: Use tools to set up IPv4 and IPv6 related variables

Quoted 92 lines · Petr Vorel

Tools (tst_net_ip_prefix, tst_net_iface_prefix, tst_net_vars) simplify setup as it only needs to have passed 4 IP addresses (IPv4 lhost + rhost and IPv6 lhost + rhost).

It's possible to define different network part for local and remote hosts.

---
 testcases/lib/test_net.sh                          | 94 +++++++++++++++-------
 testcases/network/stress/interface/if4-addr-change | 17 ++--
 2 files changed, 75 insertions(+), 36 deletions(-)
testcases/lib/test_net.sh +23 -31
index cecb0bab2..95510f7cf 100644
@@ -230,19 +230,10 @@ tst_read_opts $*
 tst_ipaddr()
 {
 	local type="${1:-lhost}"
-	local ipv="${TST_IPV6:-4}"
-	local tst_host=
-
-	if [ "$type" = "lhost" ]; then
-		eval "tst_host=\$LHOST_IPV${ipv}_HOST"
-	else
-		eval "tst_host=\$RHOST_IPV${ipv}_HOST"
-	fi
-
 	if [ "$TST_IPV6" ]; then
-		echo "${IPV6_NETWORK}:${tst_host}"
+		[ "$type" = "lhost" ] && echo "$IPV6_LHOST" || echo "$IPV6_RHOST"
 	else
-		echo "${IPV4_NETWORK}.${tst_host}"
+		[ "$type" = "lhost" ] && echo "$IPV4_LHOST" || echo "$IPV4_RHOST"
 	fi
 }
 
@@ -281,9 +272,13 @@ tst_add_ipaddr()
 {
 	local type="${1:-lhost}"
 	local link_num="${2:-0}"
+	local mask
 
-	local mask=24
-	[ "$TST_IPV6" ] && mask=64
+	if [ "$TST_IPV6" ]; then
+		[ "$type" = "lhost" ] && mask=$IPV6_LPREFIX || mask=$IPV6_RPREFIX
+	else
+		[ "$type" = "lhost" ] && mask=$IPV4_LPREFIX || mask=$IPV4_RPREFIX
+	fi
 
 	local iface=$(tst_iface $type $link_num)
 
@@ -529,25 +524,58 @@ export PASSWD="${PASSWD:-}"
 export LTP_RSH="${LTP_RSH:-rsh -n}"
 
 # Test Links
-# Set first three octets of the network address, default is '10.0.0'
-export IPV4_NETWORK="${IPV4_NETWORK:-10.0.0}"
-# Set local host last octet, default is '2'
-export LHOST_IPV4_HOST="${LHOST_IPV4_HOST:-2}"
-# Set remote host last octet, default is '1'
-export RHOST_IPV4_HOST="${RHOST_IPV4_HOST:-1}"
-# Set the reverse of IPV4_NETWORK
-export IPV4_NET_REV="${IPV4_NET_REV:-0.0.10}"
-# Set first three octets of the network address, default is 'fd00:1:1:1'
-export IPV6_NETWORK="${IPV6_NETWORK:-fd00:1:1:1}"
-# Set local host last octet, default is '2'
-export LHOST_IPV6_HOST="${LHOST_IPV6_HOST:-:2}"
-# Set remote host last octet, default is '1'
-export RHOST_IPV6_HOST="${RHOST_IPV6_HOST:-:1}"
-
-# Networks that aren't reachable through the test links
-export IPV4_NET16_UNUSED="${IPV4_NET16_UNUSED:-10.23}"
-export IPV6_NET32_UNUSED="${IPV6_NET32_UNUSED:-fd00:23}"
+# IPV{4,6}_{L,R}HOST can be set with or without prefix (e.g. IP or IP/prefix),
+# but if you use IP/prefix form, /prefix will be removed by tst_net_vars.
+IPV4_LHOST="${IPV4_LHOST:-10.0.0.2/24}"
+IPV4_RHOST="${IPV4_RHOST:-10.0.0.1/24}"
+IPV6_LHOST="${IPV6_LHOST:-fd00:1:1:1::2/64}"
+IPV6_RHOST="${IPV6_RHOST:-fd00:1:1:1::1/64}"
+
+# tst_net_ip_prefix
+# Strip prefix from IP address and save both If no prefix found sets
+# default prefix.
+#
+# tst_net_iface_prefix reads prefix and interface from rtnetlink.
+# If nothing found sets default prefix value.
+#
+# tst_net_vars exports environment variables related to test links and

testcases/lib/test_net.sh export PASSWD="${PASSWD:-}" · line 551

Quoted 10 lines
testcases/lib/test_net.sh +10 -0
+# networks that aren't reachable through the test links.
+# Not run for netns (init_ltp_netspace is not called, no links setup yet).
+#
+# For full list of exported environment variables see:
+# tst_net_ip_prefix -h
+# tst_net_iface_prefix -h
+# tst_net_vars -h
+if [ -z "$TST_PARSE_VARIABLES" ]; then
+	tst_resm TINFO "Parsing IP and prefixes"
+	set -x

It might be useful just for debugging, please remove it.

testcases/lib/test_net.sh export PASSWD="${PASSWD:-}" · line 552

Quoted 1 line
testcases/lib/test_net.sh +1 -0
+	eval "$(tst_net_ip_prefix $IPV4_LHOST)" || exit $?

Not quite right, if you want to deal with the status of tst_net_ip_prefix, you have to set 'exit' on error inside $(...)

eval "$(tst_net_ip_prefix $IPV4_LHOST || echo 'exit $?')"

Quoted 5 lines
testcases/lib/test_net.sh +5 -0
+	eval "$(tst_net_ip_prefix -r $IPV4_RHOST)" || exit $?
+	eval "$(tst_net_ip_prefix $IPV6_LHOST)" || exit $?
+	eval "$(tst_net_ip_prefix -r $IPV6_RHOST)" || exit $?
+	set +x
+

testcases/lib/test_net.sh export PASSWD="${PASSWD:-}" · line 567

Quoted 10 lines
testcases/lib/test_net.sh +9 -0
+	if [ "$TST_USE_NETNS" != "yes" ]; then
+		tst_resm TINFO "Parsing prefixes and ifaces from rtnetlink"
+		set -x
+		eval "$(tst_net_iface_prefix $IPV4_LHOST)" || exit $?
+		eval "$(tst_rhost_run -c 'tst_net_iface_prefix -r '$IPV4_RHOST)" || exit $?
+		eval "$(tst_net_iface_prefix $IPV6_LHOST)" || exit $?
+		eval "$(tst_rhost_run -c 'tst_net_iface_prefix -r '$IPV6_RHOST)" || exit $?
+		set +x
+	fi
 

Why netns is not working in the above case or it's an optimization?

Quoted 51 lines
testcases/lib/test_net.sh +15 -0
+	tst_resm TINFO "Parsing all remaining variables"
+	set -x
+	eval "$(tst_net_vars $IPV4_LHOST/$IPV4_LPREFIX $IPV4_RHOST/$IPV4_RPREFIX)" || exit $?
+	eval "$(tst_net_vars $IPV6_LHOST/$IPV6_LPREFIX $IPV6_RHOST/$IPV6_RPREFIX)" || exit $?
+	set +x
+
+	export TST_PARSE_VARIABLES="yes"
+fi
+
+# The rest of variables for networks that aren't reachable through the test links.
+# IPV4_NET16_UNUSED and IPV6_NET32_UNUSED are set by tst_net_vars.
 export OCTET_3_IPV4_UNUSED=${OCTET_3_IPV4_UNUSED:-23} # x.x.N.x
 export OCTET_4_LHOST_IPV4_HOST_UNUSED=${OCTET_4_LHOST_IPV4_HOST_UNUSED:-2} # x.x.x.N
 export OCTET_4_RHOST_IPV4_HOST_UNUSED=${OCTET_4_RHOST_IPV4_HOST_UNUSED:-1} # x.x.x.N
@@ -564,6 +592,7 @@ export RHOST_IPV6_HOST_UNUSED="${OCTET_3_IPV6_UNUSED}:${OCTET_4_RHOST_IPV6_HOST_
 export LHOST_IPV6_UNUSED="${IPV6_NET32_UNUSED}:$LHOST_IPV6_HOST_UNUSED"
 export RHOST_IPV6_UNUSED="${IPV6_NET32_UNUSED}:$RHOST_IPV6_HOST_UNUSED"
 
+# Directories for http and ftp stress tests
 export HTTP_DOWNLOAD_DIR="${HTTP_DOWNLOAD_DIR:-/var/www/html}"
 export FTP_DOWNLOAD_DIR="${FTP_DOWNLOAD_DIR:-/var/ftp}"
 export FTP_UPLOAD_DIR="${FTP_UPLOAD_DIR:-/var/ftp/pub}"
@@ -589,8 +618,11 @@ export MCASTNUM_HEAVY="${MCASTNUM_HEAVY:-4000}"
 
 # Warning: make sure to set valid interface names and IP addresses below.
 # Set names for test interfaces, e.g. "eth0 eth1"
+# This is fallback for LHOST_IFACES in case tst_net_vars finds nothing or we
+# want to use more ifaces.
 export LHOST_IFACES="${LHOST_IFACES:-eth0}"
 export RHOST_IFACES="${RHOST_IFACES:-eth0}"
+
 # Set corresponding HW addresses, e.g. "00:00:00:00:00:01 00:00:00:00:00:02"
 export LHOST_HWADDRS="${LHOST_HWADDRS:-$(tst_get_hwaddrs lhost)}"
 export RHOST_HWADDRS="${RHOST_HWADDRS:-$(tst_get_hwaddrs rhost)}"
testcases/network/stress/interface/if4-addr-change +7 -2
index 567f86643..a7469c252 100644
@@ -48,12 +48,19 @@ while [ $cnt -lt $NS_TIMES ]; do
 
 	[ $num -eq $RHOST_IPV4_HOST ] && continue
 
-	# Change IPv4 address
-	lhost_ipv4addr="${IPV4_NETWORK}.${num}"
+	# check prefix and fix values for prefix != 24
+	add_to_net=
+	if [ $IPV4_LPREFIX -lt 8 -o $IPV4_LPREFIX -ge 32 ] ; then
+		tst_brkm TCONF "test must be with prefix >= 8 and prefix < 32 ($IPV4_LPREFIX)"
+	elif [ $IPV4_LPREFIX -lt 16 ]; then # N.x.x.num
+		add_to_net=".0.1"
+	elif [ $IPV4_LPREFIX -lt 24 ]; then # N.N.x.num

testcases/network/stress/interface/if4-addr-change while [ $cnt -lt $NS_TIMES ]; do · line 64

Quoted 10 lines
testcases/network/stress/interface/if4-addr-change +5 -3
+		add_to_net=".1"
+	fi
 
-	ifconfig $(tst_iface) $lhost_ipv4addr netmask 255.255.255.0 \
-		broadcast 255.255.255.255 || \
-		tst_brkm TFAIL "Failed to change into ${lhost_ipv4addr}"
+	# Change IPv4 address
+	ROD ifconfig $(tst_iface) ${IPV4_LNETWORK}${add_to_net}.${num} netmask \
+		$IPV4_LNETMASK broadcast $IPV4_LBROADCAST
 

Could you make a separate patch for this? It looks completely unrelated to the rest in this patch.

Thanks,
Alexey

testcases/network/stress/interface/if4-addr-change while [ $cnt -lt $NS_TIMES ]; do · line 66

Quoted 2 lines
testcases/network/stress/interface/if4-addr-change +0 -0
 	cnt=$(($cnt + 1))
 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20170726/2279d07f/attachment.html>

author
#33 lore raw

RFC PATCH v7 11/11 network: Use tools to set up IPv4 and IPv6 related variables

Hi Alexey,

Quoted 9 lines

testcases/lib/test_net.sh export PASSWD="${PASSWD:-}" · line 551

Quoted 7 lines
testcases/lib/test_net.sh +7 -0
+# For full list of exported environment variables see:
+# tst_net_ip_prefix -h
+# tst_net_iface_prefix -h
+# tst_net_vars -h
+if [ -z "$TST_PARSE_VARIABLES" ]; then
+	tst_resm TINFO "Parsing IP and prefixes"
+	set -x

It might be useful just for debugging, please remove it.

OK, I'll remove it, because you don't like it. But I have to admit, I think it's important to see values of automatic setup.
If user reports a bug/problem we have ver_linux, which is suitable for most of the scripts, but not for networking. And printing values of automatic configuration doesn't have to be by 'set -x', that was just a quick way how to achieve it.

Quoted 6 lines

testcases/lib/test_net.sh export PASSWD="${PASSWD:-}" · line 552

Quoted 1 line
testcases/lib/test_net.sh +1 -0
+	eval "$(tst_net_ip_prefix $IPV4_LHOST)" || exit $?

Not quite right, if you want to deal with the status of tst_net_ip_prefix, you have to set 'exit' on error inside $(...)

eval "$(tst_net_ip_prefix $IPV4_LHOST || echo 'exit $?')"

Am I missing something? Are you sure, you're right? POSIX defines [1]:
EXIT STATUS
If there are no arguments, or only null arguments, eval shall return a zero exit status; otherwise, it shall return the exit status of the command defined by the string of concatenated arguments separated by <space> characters, or a non-zero exit status if the concatenation could not be parsed as a command and the shell is interactive (and therefore did not abort).

[1]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_19

And bash, dash and busybox shell all implement this behaviour correctly. So both ways deals correctly with tst_net_ip_prefix exit status and I consider mine form a bit clearer.

Quoted 11 lines

testcases/lib/test_net.sh export PASSWD="${PASSWD:-}" · line 566

Quoted 9 lines
testcases/lib/test_net.sh +9 -0
+	if [ "$TST_USE_NETNS" != "yes" ]; then
+		tst_resm TINFO "Parsing prefixes and ifaces from rtnetlink"
+		set -x
+		eval "$(tst_net_iface_prefix $IPV4_LHOST)" || exit $?
+		eval "$(tst_rhost_run -c 'tst_net_iface_prefix -r '$IPV4_RHOST)" || exit $?
+		eval "$(tst_net_iface_prefix $IPV6_LHOST)" || exit $?
+		eval "$(tst_rhost_run -c 'tst_net_iface_prefix -r '$IPV6_RHOST)" || exit $?
+		set +x
+	fi

Why netns is not working in the above case or it's an optimization?

Chicken or the egg problem. We need tst_rhost_run, but it requires LTP_NETNS being set => that requires init_ltp_netspace() call. But in init_ltp_netspace() we set remote IP. Does it make sense to move tst_net_iface_prefix behind netns initialization and call 'tst_restore_ipaddr' and 'tst_restore_ipaddr rhost' again?

...

Quoted 20 lines
Quoted 19 lines
testcases/network/stress/interface/if4-addr-change +9 -2
index 567f86643..a7469c252 100644
@@ -48,12 +48,19 @@ while [ $cnt -lt $NS_TIMES ]; do
 
 	[ $num -eq $RHOST_IPV4_HOST ] && continue
 
-	# Change IPv4 address
-	lhost_ipv4addr="${IPV4_NETWORK}.${num}"
+	# check prefix and fix values for prefix != 24
+	add_to_net=
+	if [ $IPV4_LPREFIX -lt 8 -o $IPV4_LPREFIX -ge 32 ] ; then
+		tst_brkm TCONF "test must be with prefix >= 8 and prefix < 32 ($IPV4_LPREFIX)"
+	elif [ $IPV4_LPREFIX -lt 16 ]; then # N.x.x.num
+		add_to_net=".0.1"
+	elif [ $IPV4_LPREFIX -lt 24 ]; then # N.N.x.num
+		add_to_net=".1"
+	fi
Quoted 10 lines

testcases/network/stress/interface/if4-addr-change while [ $cnt -lt $NS_TIMES ]; do · line 63

Quoted 6 lines
testcases/network/stress/interface/if4-addr-change +3 -3
-	ifconfig $(tst_iface) $lhost_ipv4addr netmask 255.255.255.0 \
-		broadcast 255.255.255.255 || \
-		tst_brkm TFAIL "Failed to change into ${lhost_ipv4addr}"
+	# Change IPv4 address
+	ROD ifconfig $(tst_iface) ${IPV4_LNETWORK}${add_to_net}.${num} netmask \
+		$IPV4_LNETMASK broadcast $IPV4_LBROADCAST

Could you make a separate patch for this? It looks completely unrelated to the rest in this patch.

No, it is related: IPV4_NETWORK disappeared and was replaced by IPV4_LNETWORK and IPV4_RNETWORK. If we split the commit into two, if4-addr-change will not work in the first commit (and we break principle of atomic commits).
Of course, we can temporarily (for one commit) define IPV4_NETWORK locally in if4-addr-change in the first commit in order not to break the script, but does it make much sense?
I didn't mention in commit message that I'm updating all scripts which use old variables (the ones which disappeared) with new ones. If you agree to keep it all in one commit, I put the info in the message.

Thanks for your review, I really appreciate it!

Kind regards,
Petr

#34 lore raw

RFC PATCH v7 11/11 network: Use tools to set up IPv4 and IPv6 related variables

Hi,

Quoted 20 lines · Petr Vorel

Hi Alexey,

Quoted 8 lines

testcases/lib/test_net.sh export PASSWD="${PASSWD:-}" · line 551

Quoted 7 lines
testcases/lib/test_net.sh +7 -0
+# For full list of exported environment variables see:
+# tst_net_ip_prefix -h
+# tst_net_iface_prefix -h
+# tst_net_vars -h
+if [ -z "$TST_PARSE_VARIABLES" ]; then
+	tst_resm TINFO "Parsing IP and prefixes"
+	set -x

It might be useful just for debugging, please remove it.

OK, I'll remove it, because you don't like it. But I have to admit, I think it's important to see values of automatic setup.
If user reports a bug/problem we have ver_linux, which is suitable for most of the scripts, but not for networking. And printing values of automatic configuration doesn't have to be by 'set -x', that was just a quick way how to achieve it.

Quoted 4 lines

testcases/lib/test_net.sh export PASSWD="${PASSWD:-}" · line 552

Quoted 1 line
testcases/lib/test_net.sh +1 -0
+	eval "$(tst_net_ip_prefix $IPV4_LHOST)" || exit $?

Not quite right, if you want to deal with the status of tst_net_ip_prefix, you have to set 'exit' on error inside $(...) eval "$(tst_net_ip_prefix $IPV4_LHOST || echo 'exit $?')"

Quoted 7 lines

Am I missing something? Are you sure, you're right? POSIX defines [1]:
EXIT STATUS
If there are no arguments, or only null arguments, eval shall return a zero exit status; otherwise, it shall return the exit status of the command defined by the string of concatenated arguments separated by <space> characters, or a non-zero exit status if the concatenation could not be parsed as a command and the shell is interactive (and therefore did not abort).

eval executes the output of tst_net_ip_prefix command there, not $(...), so basically it's the same as if we write:

cmd="$(tst_net_ip_prefix)"

On error $cmd is empty because you write to stderr, so eval returns 0.

eval "$cmd"

Or you could use '$(...)' instead of "$(...)", but in that case eval is kind of redundant.

Quoted 17 lines

[1]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_19

And bash, dash and busybox shell all implement this behaviour correctly. So both ways deals correctly with tst_net_ip_prefix exit status and I consider mine form a bit clearer.

Quoted 10 lines

testcases/lib/test_net.sh export PASSWD="${PASSWD:-}" · line 566

Quoted 9 lines
testcases/lib/test_net.sh +9 -0
+	if [ "$TST_USE_NETNS" != "yes" ]; then
+		tst_resm TINFO "Parsing prefixes and ifaces from rtnetlink"
+		set -x
+		eval "$(tst_net_iface_prefix $IPV4_LHOST)" || exit $?
+		eval "$(tst_rhost_run -c 'tst_net_iface_prefix -r '$IPV4_RHOST)" || exit $?
+		eval "$(tst_net_iface_prefix $IPV6_LHOST)" || exit $?
+		eval "$(tst_rhost_run -c 'tst_net_iface_prefix -r '$IPV6_RHOST)" || exit $?
+		set +x
+	fi

Why netns is not working in the above case or it's an optimization?

Quoted 4 lines

Chicken or the egg problem. We need tst_rhost_run, but it requires LTP_NETNS being set => that requires init_ltp_netspace() call. But in init_ltp_netspace() we set remote IP. Does it make sense to move tst_net_iface_prefix behind netns initialization and call 'tst_restore_ipaddr' and 'tst_restore_ipaddr rhost' again?

I see, it could be solved by moving init_ltp_netspace() between tst_net_ip_prefix() calls and "parsing prefixes and ifaces from rtnetlink".

Thanks,
Alexey

author
#35 lore raw

RFC PATCH v7 11/11 network: Use tools to set up IPv4 and IPv6 related variables

Hi Alexey,

Quoted 11 lines

eval executes the output of tst_net_ip_prefix command there, not $(...), so basically it's the same as if we write:

cmd="$(tst_net_ip_prefix)"

On error $cmd is empty because you write to stderr, so eval returns 0.

eval "$cmd"

Or you could use '$(...)' instead of "$(...)", but in that case eval is kind of redundant.

Thanks for detailed explanation. We need eval (for export variables), so I'll use your original version.

Kind regards,
Petr