1#!/bin/sh 2# SPDX-License-Identifier: GPL-2.0-or-later 3# Copyright (c) 2017-2018 Petr Vorel <pvorel@suse.cz> 4# Copyright (c) 2015-2017 Oracle and/or its affiliates. All Rights Reserved. 5# Copyright (c) International Business Machines Corp., 2006 6# Author: Petr Vorel <pvorel@suse.cz> 7# Author: Alexey Kodanev <alexey.kodanev@oracle.com> 8# 9# Library for all network/stress/ tests. 10# NOTE: More information about network variables can be found 11# in tst_net.sh and testcases/network/stress/README. 12 13. tst_net.sh 14 15# Netmask of for the tested network 16IPV4_NETMASK="255.255.255.0" 17IPV4_NETMASK_NUM=24 18 19# Multicast address and it's prefix 20MCAST_IPV4_ADDR_PREFIX="224.10" 21MCAST_IPV4_ADDR="${MCAST_IPV4_ADDR_PREFIX}.10.1" 22MCAST_IPV6_ADDR_PREFIX="ff0e::1111" 23MCAST_IPV6_ADDR="${MCAST_IPV6_ADDR_PREFIX}:1" 24 25# Setup for tests using netstress. 26netstress_setup() 27{ 28 TST_NEEDS_ROOT=1 29 tst_require_cmds pgrep pkill 30} 31 32# Cleanup for tests using netstress. 33netstress_cleanup() 34{ 35 # Stop the background TCP traffic 36 pkill -13 -x netstress 37 tst_rhost_run -c "pkill -13 -x netstress" 38} 39 40# restore_ipaddr [TYPE] [LINK] [LOCAL_IFACE] [REMOTE_IFACE] 41# TYPE: { lhost | rhost }; Default value is 'lhost'. 42# LINK: link number starting from 0. Default value is '0'. 43# LOCAL_IFACE: local iface name. 44# REMOTE_IFACE: local iface name. 45restore_ipaddr() 46{ 47 local type="${1:-lhost}" 48 local link_num="${2:-0}" 49 local iface_loc=${3:-$(tst_iface lhost $link_num)} 50 local iface_rmt=${4:-$(tst_iface rhost $link_num)} 51 52 tst_restore_ipaddr $type $link_num || return $? 53 [ $type = "lhost" ] && tst_wait_ipv6_dad $iface_loc $iface_rmt 54} 55 56# Check connectivity with tst_ping. 57# check_connectivity SRC_IFACE DST_ADDR [CNT] 58# SRC_IFACE: source interface name. 59# DST_ADDR: destination IPv4 or IPv6 address. 60# CNT: loop step. 61check_connectivity() 62{ 63 local src_iface="${1}" 64 local dst_addr="${2}" 65 local cnt="${3:-}" 66 local cnt_msg 67 68 [ -n "$cnt" ] && cnt_msg=" (step $cnt)" 69 70 tst_res TINFO "ping through $src_iface iface to ${dst_addr}$cnt_msg" 71 72 tst_ping $src_iface $dst_addr 73} 74 75# check_connectivity_interval CNT [RESTORE] [SRC_IFACE] [DST_ADDR] 76# CNT: loop step. 77# RESTORE: whether restore ip addr. 78# SRC_IFACE: source interface name. 79# DST_ADDR: destination IPv4 or IPv6 address. 80check_connectivity_interval() 81{ 82 local cnt="$1" 83 local restore="${2:-false}" 84 local src_iface="${3:-$(tst_iface)}" 85 local dst_addr="${4:-$(tst_ipaddr rhost)}" 86 87 [ $CHECK_INTERVAL -eq 0 ] && return 88 89 [ $(($cnt % $CHECK_INTERVAL)) -ne 0 ] && return 90 91 [ "$restore" != "false" ] && restore_ipaddr 92 93 check_connectivity $src_iface $dst_addr $cnt 94} 95 96# Run netstress process on both lhost and rhost. 97# make_background_tcp_traffic [IP] 98# IP: server IP; Default value is $(tst_ipaddr). 99make_background_tcp_traffic() 100{ 101 pgrep -x netstress > /dev/null && return 102 103 local ip="${1:-$(tst_ipaddr)}" 104 local port=$(tst_get_unused_port ipv${TST_IPVER} stream) 105 106 netstress -R 3 -g $port > /dev/null 2>&1 & 107 tst_rhost_run -b -c "netstress -l -H $ip -g $port" 108} 109 110test_if_ip() 111{ 112 case $1 in 113 1) test_body 'if_cmd';; 114 2) test_body 'ip_cmd';; 115 esac 116} 117 118test_rt_ip() 119{ 120 case $1 in 121 1) test_body 'rt_cmd';; 122 2) test_body 'ip_cmd';; 123 esac 124} 125