xref: /aosp_15_r20/external/ltp/testcases/network/stress/multicast/grp-operation/mcast-lib.sh (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) 2017-2022 Petr Vorel <[email protected]>
4# Copyright (c) International Business Machines Corp., 2006
5# Author: Petr Vorel <[email protected]>
6#
7# Setup script for multicast stress tests.
8
9mcast_setup4()
10{
11	local igmp_max_memberships="$1"
12
13	SYSFS_IGMP_MAX_MEMBERSHIPS=$(sysctl -b net.ipv4.igmp_max_memberships)
14	SYSFS_IGMP_MAX_MSF=$(sysctl -b net.ipv4.igmp_max_msf)
15	SYSFS_FORCE_IGMP_VERSION=$(sysctl -b net.ipv4.conf.$(tst_iface).force_igmp_version)
16	SYSFS_ALL_FORCE_IGMP_VERSION=$(sysctl -b net.ipv4.conf.all.force_igmp_version)
17
18	[ "$igmp_max_memberships" -gt 5459 ] && tst_res TWARN \
19		"\$1 shouldn't be set higher than 5459 as it's used to set /proc/sys/net/ipv4/igmp_max_memberships"
20
21	ROD sysctl -q -w net.ipv4.igmp_max_memberships=$igmp_max_memberships
22	ROD sysctl -q -w net.ipv4.igmp_max_msf=10
23	ROD sysctl -q -w net.ipv4.conf.$(tst_iface).force_igmp_version=0
24	ROD sysctl -q -w net.ipv4.conf.all.force_igmp_version=0
25}
26
27mcast_setup6()
28{
29	local default_mld_max_msf=64
30
31	SYSCTL_ALL_FORCE_MLD_VERSION=$(sysctl -b net.ipv6.conf.all.force_mld_version)
32	SYSCTL_FORCE_MLD_VERSION=$(sysctl -b net.ipv6.conf.$(tst_iface).force_mld_version)
33	SYSCTL_MLD_MAX_MSF=$(sysctl -b net.ipv6.mld_max_msf)
34
35	ROD sysctl -q -w net.ipv6.conf.all.force_mld_version=0
36	ROD sysctl -q -w net.ipv6.conf.$(tst_iface).force_mld_version=0
37	ROD sysctl -q -w net.ipv6.mld_max_msf=$default_mld_max_msf
38}
39
40mcast_setup()
41{
42	local max="$1"
43
44	MCAST_LCMD="ns-mcast_join -f $TST_IPVER -I $(tst_iface)"
45
46	local cmd="ns-igmp_querier"
47	[ "$TST_IPV6" ] && cmd="ns-icmpv6_sender"
48	MCAST_RCMD="$cmd -I $(tst_iface rhost)"
49
50	netstress_setup
51
52	[ "$TST_IPV6" ] && mcast_setup6 || mcast_setup4 $max
53}
54
55mcast_setup_normal()
56{
57	mcast_setup $MCASTNUM_NORMAL
58}
59
60mcast_setup_normal_udp()
61{
62	mcast_setup_normal
63	MCAST_LCMD="ns-mcast_receiver"
64	MCAST_RCMD="ns-udpsender"
65}
66
67mcast_cleanup4()
68{
69	[ -n "$SYSFS_IGMP_MAX_MEMBERSHIPS" ] && sysctl -q -w net.ipv4.igmp_max_memberships=$SYSFS_IGMP_MAX_MEMBERSHIPS
70	[ -n "$SYSFS_IGMP_MAX_MSF" ] && sysctl -q -w net.ipv4.igmp_max_msf=$SYSFS_IGMP_MAX_MSF
71	[ -n "$SYSFS_FORCE_IGMP_VERSION" ] && sysctl -q -w net.ipv4.conf.$(tst_iface).force_igmp_version=$SYSFS_FORCE_IGMP_VERSION
72	[ -n "$SYSFS_ALL_FORCE_IGMP_VERSION" ] && sysctl -q -w net.ipv4.conf.all.force_igmp_version=$SYSFS_ALL_FORCE_IGMP_VERSION
73}
74
75mcast_cleanup6()
76{
77	[ -n "$SYSCTL_ALL_FORCE_MLD_VERSION" ] && sysctl -q -w net.ipv6.conf.all.force_mld_version=$SYSCTL_ALL_FORCE_MLD_VERSION
78	[ -n "$SYSCTL_FORCE_MLD_VERSION" ] && sysctl -q -w net.ipv6.conf.$(tst_iface).force_mld_version=$SYSCTL_FORCE_MLD_VERSION
79	[ -n "$SYSCTL_MLD_MAX_MSF" ] && sysctl -q -w net.ipv6.mld_max_msf=$SYSCTL_MLD_MAX_MSF
80}
81
82mcast_cleanup()
83{
84	[ "$TST_IPV6" ] && mcast_cleanup6 || mcast_cleanup4
85
86	pkill -SIGHUP -f "$MCAST_LCMD"
87	tst_sleep 10ms
88	pkill -9 -f "$MCAST_LCMD"
89
90	tst_rhost_run -c "pkill -SIGHUP -f '$MCAST_RCMD'"
91}
92
93do_multicast_test_multiple_join()
94{
95	local num="$1"
96	local mprefix="$MCAST_IPV4_ADDR_PREFIX"
97	local param_multi_socket ret tmpfile
98
99	[ "${2:-}" = true ] && param_multi_socket="-m"
100	[ "$TST_IPV6" ] && mprefix="$MCAST_IPV6_ADDR_PREFIX"
101
102	# Run a multicast join tool
103	tmpfile=$$
104	EXPECT_PASS $MCAST_LCMD $param_multi_socket -n $num -p $mprefix \> $tmpfile
105	tst_res TINFO "joined $(grep groups $tmpfile)"
106
107	# Send MLD / IGMP General Query from the remote host
108	if [ "$TST_IPV6" ]; then
109		EXPECT_RHOST_PASS $MCAST_RCMD -S $(tst_ipaddr) -m -o
110	else
111		EXPECT_RHOST_PASS $MCAST_RCMD -o -r 1 -m $MCAST_IPV4_ADDR
112	fi
113}
114
115do_multicast_test_join_leave()
116{
117	local cnt define_src_addr filter params pid pids ret
118	local max="$1"
119	local maddr="$MCAST_IPV4_ADDR"
120	[ "$TST_IPV6" ] && maddr="$MCAST_IPV6_ADDR"
121
122	[ "$2" = true ] && define_src_addr=true
123
124	# Send MLD / IGMP General Query from the remote host
125	if [ "$TST_IPV6" ]; then
126		tst_rhost_run -s -c "$MCAST_RCMD -S $(tst_ipaddr) -m -w 1000000000 -r 1000 -b"
127	else
128		tst_rhost_run -s -c "$MCAST_RCMD -i 1000000000 -r 1 -b"
129	fi
130
131	# Run a multicast join tool
132	cnt=0
133	while [ $cnt -lt $max ]; do
134		if [ "$define_src_addr" ]; then
135			[ $((cnt % 5)) -ne 2 ] && filter="include" || filter="exclude"
136			params="-F $filter -s $(tst_ipaddr_un -c$cnt)"
137		fi
138
139		$MCAST_LCMD -l $NS_TIMES -a $maddr $params &
140		pids="$! $pids"
141		cnt=$((cnt + 1))
142	done
143
144	for pid in $pids; do wait $pid; done
145
146	tst_res TPASS "test is finished successfully"
147}
148
149do_multicast_test_join_single_socket()
150{
151	local extra="$1"
152	local prefix="$MCAST_IPV4_ADDR_PREFIX"
153	[ "$TST_IPV6" ] && prefix="$MCAST_IPV6_ADDR_PREFIX"
154
155	# Run a multicast join tool
156	local tmpfile=$$
157	EXPECT_PASS $MCAST_LCMD -n 1 -p $prefix \> $tmpfile
158	tst_res TINFO "joined $(grep groups $tmpfile)"
159
160	local params
161	[ "$TST_IPV6" ] && params="-S $(tst_ipaddr) -m"
162	EXPECT_RHOST_PASS $MCAST_RCMD -t $NS_DURATION -r 0 $params $extra
163}
164
165. tst_net_stress.sh
166