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