1#!/bin/sh
2# Copyright (c) International Business Machines  Corp., 2006
3# Copyright (c) 2015-2017 Oracle and/or its affiliates. All Rights Reserved.
4# Copyright (c) 2017 Petr Vorel <pvorel@suse.cz>
5#
6# This program is free software; you can redistribute it and/or
7# modify it under the terms of the GNU General Public License as
8# published by the Free Software Foundation; either version 2 of
9# the License, or (at your option) any later version.
10#
11# This program is distributed in the hope that it would be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program. If not, see <http://www.gnu.org/licenses/>.
18#
19# Author: Petr Vorel <pvorel@suse.cz>
20# Author: Alexey Kodanev <alexey.kodanev@oracle.com>
21#
22# Library for all network/stress/ tests.
23# NOTE: More information about network variables can be found
24# in test_net.sh and testcases/network/stress/README.
25
26export TCID="${TCID:-$(basename $0)}"
27
28. test_net.sh
29
30ipver=${TST_IPV6:-4}
31
32# Netmask of for the tested network
33IPV4_NETMASK="255.255.255.0"
34IPV4_NETMASK_NUM=24
35
36# Multicast address and it's prefix
37MCAST_IPV4_ADDR_PREFIX="224.10"
38MCAST_IPV4_ADDR="${MCAST_IPV4_ADDR_PREFIX}.10.1"
39MCAST_IPV6_ADDR_PREFIX="ff0e::1111"
40MCAST_IPV6_ADDR="${MCAST_IPV6_ADDR_PREFIX}:1"
41
42# Setup for tests using netstress.
43netstress_setup()
44{
45	tst_require_root
46	tst_check_cmds ip pgrep pkill
47	trap "tst_brkm TBROK 'test interrupted'" INT
48}
49
50# Cleanup for tests using netstress.
51netstress_cleanup()
52{
53	# Stop the background TCP traffic
54	pkill -13 -x netstress
55	tst_rhost_run -c "pkill -13 -x netstress"
56}
57
58# restore_ipaddr [TYPE] [LINK] [LOCAL_IFACE] [REMOTE_IFACE]
59# TYPE: { lhost | rhost }; Default value is 'lhost'.
60# LINK: link number starting from 0. Default value is '0'.
61# LOCAL_IFACE: local iface name.
62# REMOTE_IFACE: local iface name.
63restore_ipaddr()
64{
65	local type="${1:-lhost}"
66	local link_num="${2:-0}"
67	local iface_loc=${3:-$(tst_iface lhost $link_num)}
68	local iface_rmt=${4:-$(tst_iface rhost $link_num)}
69
70	tst_restore_ipaddr $type $link_num || return $?
71	[ $type = "lhost" ] && tst_wait_ipv6_dad $iface_loc $iface_rmt
72}
73
74# Check connectivity with tst_ping.
75# check_connectivity SRC_IFACE DST_ADDR [CNT]
76# SRC_IFACE: source interface name.
77# DST_ADDR: destination IPv4 or IPv6 address.
78# CNT: loop step.
79check_connectivity()
80{
81	local src_iface="${1}"
82	local dst_addr="${2}"
83	local cnt="${3:-}"
84	local cnt_msg
85
86	[ -n "$cnt" ] && cnt_msg=" (step $cnt)"
87
88	tst_resm TINFO "ping through $src_iface iface to ${dst_addr}$cnt_msg"
89
90	tst_ping $src_iface $dst_addr
91}
92
93# check_connectivity_interval CNT [RESTORE] [SRC_IFACE] [DST_ADDR]
94# CNT: loop step.
95# RESTORE: whether restore ip addr.
96# SRC_IFACE: source interface name.
97# DST_ADDR: destination IPv4 or IPv6 address.
98check_connectivity_interval()
99{
100	local cnt="$1"
101	local restore="${2:-false}"
102	local src_iface="${3:-$(tst_iface)}"
103	local dst_addr="${4:-$(tst_ipaddr rhost)}"
104
105	[ $CHECK_INTERVAL -eq 0 ] && return
106
107	[ $(($cnt % $CHECK_INTERVAL)) -ne 0 ] && return
108
109	[ "$restore" != "false" ] && restore_ipaddr
110
111	check_connectivity $src_iface $dst_addr $cnt
112}
113
114# Run netstress process on both lhost and rhost.
115# make_background_tcp_traffic [IP]
116# IP: server IP; Default value is $(tst_ipaddr).
117make_background_tcp_traffic()
118{
119	pgrep -x netstress > /dev/null && return
120
121	local ip="${1:-$(tst_ipaddr)}"
122	local port=$(tst_get_unused_port ipv${ipver} stream)
123
124	netstress -R 3 -g $port > /dev/null 2>&1 &
125	tst_rhost_run -b -c "netstress -l -H $ip -g $port"
126}
127