xref: /aosp_15_r20/external/ltp/testcases/network/stress/route/route-lib.sh (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) 2019-2022 Petr Vorel <[email protected]>
4
5TST_NEEDS_ROOT=1
6TST_NEEDS_CMDS="ip"
7
8ROUTE_RHOST_PORT=${ROUTE_RHOST_PORT:-65535}
9ROUTE_MAX_IP=${ROUTE_MAX_IP:-5}
10
11IP_ADDR_DELIM=','
12
13add_macvlan()
14{
15	local action="add"
16	local OPTIND
17	while getopts d opt; do
18		case "$opt" in
19		d) action="del";;
20		esac
21	done
22	shift $((OPTIND - 1))
23
24	local iface="$1"
25	local type="${2:-lhost}"
26
27	cmd="ip link $action $iface link $(tst_iface $type) type macvlan mode bridge"
28	if [ $type = "lhost" ]; then
29		ROD $cmd
30		[ "$action" = "add" ] || return
31		LHOST_IFACES="$LHOST_IFACES $iface"
32	else
33		tst_rhost_run -s -c "$cmd"
34		[ "$action" = "add" ] || return
35		RHOST_IFACES="$RHOST_IFACES $iface"
36	fi
37	tst_init_iface $type 1
38}
39
40check_max_ip()
41{
42	local max_ip_limit=254
43	[ "$TST_IPV6" ] && max_ip_limit=65534
44
45	tst_is_int "$ROUTE_MAX_IP" || tst_brk TBROK "\$ROUTE_MAX_IP not int ($ROUTE_MAX_IP)"
46	[ $ROUTE_MAX_IP -gt $max_ip_limit ] && ROUTE_MAX_IP=$max_ip_limit
47	[ $ROUTE_MAX_IP -gt $ROUTE_CHANGE_NETLINK ] && ROUTE_MAX_IP=$ROUTE_CHANGE_NETLINK
48}
49
50cleanup_if()
51{
52	[ "$new_liface" ] && add_macvlan -d $new_liface
53	[ "$new_riface" ] && add_macvlan -d $new_riface rhost
54	route_cleanup
55}
56
57route_cleanup()
58{
59	tst_restore_ipaddr
60	tst_restore_ipaddr rhost
61}
62
63setup_gw()
64{
65	rt="$(tst_ipaddr_un -p 0 0)"
66	lhost="$(tst_ipaddr_un 1 1)"
67	rhost="$(tst_ipaddr_un 0 1)"
68	tst_add_ipaddr -s -q -a $lhost
69	tst_add_ipaddr -s -q -a $rhost rhost
70}
71
72setup_if()
73{
74	rt="$(tst_ipaddr_un -p 0)"
75	rhost="$(tst_ipaddr_un 0 1)"
76	tst_add_ipaddr -s -q -a $rhost rhost
77
78	if [ $(tst_get_ifaces_cnt) -lt 2 ]; then
79		new_liface="ltp_mv2"
80		tst_res TINFO "2 or more local ifaces required, adding '$new_liface'"
81		add_macvlan $new_liface
82	fi
83
84	if [ $(tst_get_ifaces_cnt rhost) -lt 2 ]; then
85		new_riface="ltp_mv1"
86		tst_res TINFO "2 or more remote ifaces required, adding '$new_riface'"
87		add_macvlan $new_riface rhost
88	fi
89}
90
91test_netlink()
92{
93	local opt="-c $ROUTE_CHANGE_NETLINK $TST_IPV6_FLAG -p $ROUTE_RHOST_PORT $ROUTE_CHANGE_NETLINK_PARAMS"
94	local cmd="route-change-netlink"
95	local ret=0
96
97	tst_res TINFO "running $cmd $opt"
98	$cmd $opt || ret=$?
99	if [ "$ret" -ne 0 ]; then
100		if [ $((ret & 3)) -ne 0 ]; then
101			tst_res TFAIL "$cmd failed"
102			return
103		fi
104
105		[ $((ret & 32)) -ne 0 ] && \
106			tst_brk TCONF "not supported configuration"
107
108		[ $((ret & 4)) -ne 0 ] && \
109			tst_res TWARN "$cmd has warnings"
110	fi
111	tst_res TPASS "$cmd passed"
112}
113
114. tst_net.sh
115