xref: /linux/tools/testing/selftests/net/mptcp/pm_netlink.sh (revision 0c436dfe5c25d0931b164b944165259f95e5281f)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4. "$(dirname "${0}")/mptcp_lib.sh"
5
6ret=0
7
8usage() {
9	echo "Usage: $0 [ -i ] [ -h ]"
10	echo -e "\t-i: use 'ip mptcp' instead of 'pm_nl_ctl'"
11	echo -e "\t-h: help"
12}
13
14optstring=hi
15while getopts "$optstring" option;do
16	case "$option" in
17	"h")
18		usage "$0"
19		exit ${KSFT_PASS}
20		;;
21	"i")
22		mptcp_lib_set_ip_mptcp
23		;;
24	"?")
25		usage "$0"
26		exit ${KSFT_FAIL}
27		;;
28	esac
29done
30
31ns1=""
32err=$(mktemp)
33
34# This function is used in the cleanup trap
35#shellcheck disable=SC2317
36cleanup()
37{
38	rm -f "${err}"
39	mptcp_lib_ns_exit "${ns1}"
40}
41
42mptcp_lib_check_mptcp
43mptcp_lib_check_tools ip
44
45trap cleanup EXIT
46
47mptcp_lib_ns_init ns1
48
49format_limits() {
50	local accept="${1}"
51	local subflows="${2}"
52
53	if mptcp_lib_is_ip_mptcp; then
54		# with a space at the end
55		printf "add_addr_accepted %d subflows %d \n" "${accept}" "${subflows}"
56	else
57		printf "accept %d\nsubflows %d\n" "${accept}" "${subflows}"
58	fi
59}
60
61get_limits() {
62	if mptcp_lib_is_ip_mptcp; then
63		ip -n "${ns1}" mptcp limits
64	else
65		ip netns exec "${ns1}" ./pm_nl_ctl limits
66	fi
67}
68
69format_endpoints() {
70	mptcp_lib_pm_nl_format_endpoints "${@}"
71}
72
73get_endpoint() {
74	# shellcheck disable=SC2317 # invoked indirectly
75	mptcp_lib_pm_nl_get_endpoint "${ns1}" "${@}"
76}
77
78change_address() {
79	local addr=${1}
80	local flags=${2}
81
82	if mptcp_lib_is_ip_mptcp; then
83		ip -n "${ns1}" mptcp endpoint change "${addr}" "${flags}"
84	else
85		ip netns exec "${ns1}" ./pm_nl_ctl set "${addr}" flags "${flags}"
86	fi
87}
88
89set_limits()
90{
91	mptcp_lib_pm_nl_set_limits "${ns1}" "${@}"
92}
93
94add_endpoint()
95{
96	mptcp_lib_pm_nl_add_endpoint "${ns1}" "${@}"
97}
98
99del_endpoint()
100{
101	mptcp_lib_pm_nl_del_endpoint "${ns1}" "${@}"
102}
103
104flush_endpoint()
105{
106	mptcp_lib_pm_nl_flush_endpoint "${ns1}"
107}
108
109show_endpoints()
110{
111	mptcp_lib_pm_nl_show_endpoints "${ns1}"
112}
113
114change_endpoint()
115{
116	mptcp_lib_pm_nl_change_endpoint "${ns1}" "${@}"
117}
118
119check()
120{
121	local cmd="$1"
122	local expected="$2"
123	local msg="$3"
124	local rc=0
125
126	mptcp_lib_print_title "$msg"
127	mptcp_lib_check_output "${err}" "${cmd}" "${expected}" || rc=${?}
128	if [ ${rc} -eq 2 ]; then
129		mptcp_lib_result_fail "${msg} # error ${rc}"
130		ret=${KSFT_FAIL}
131	elif [ ${rc} -eq 0 ]; then
132		mptcp_lib_print_ok "[ OK ]"
133		mptcp_lib_result_pass "${msg}"
134	elif [ ${rc} -eq 1 ]; then
135		mptcp_lib_result_fail "${msg} # different output"
136		ret=${KSFT_FAIL}
137	fi
138}
139
140mptcp_lib_subtests_last_ts_reset
141
142check "show_endpoints" "" "defaults addr list"
143
144default_limits="$(get_limits)"
145if mptcp_lib_expect_all_features; then
146	check "get_limits" "$(format_limits 0 2)" "defaults limits"
147fi
148
149add_endpoint 10.0.1.1
150add_endpoint 10.0.1.2 flags subflow dev lo
151add_endpoint 10.0.1.3 flags signal,backup
152check "get_endpoint 1" "$(format_endpoints "1,10.0.1.1")" "simple add/get addr"
153
154check "show_endpoints" \
155	"$(format_endpoints "1,10.0.1.1" \
156			    "2,10.0.1.2,subflow,lo" \
157			    "3,10.0.1.3,signal backup")" "dump addrs"
158
159del_endpoint 2
160check "get_endpoint 2" "" "simple del addr"
161check "show_endpoints" \
162	"$(format_endpoints "1,10.0.1.1" \
163			    "3,10.0.1.3,signal backup")" "dump addrs after del"
164
165add_endpoint 10.0.1.3 2>/dev/null
166check "get_endpoint 4" "" "duplicate addr"
167
168add_endpoint 10.0.1.4 flags signal
169check "get_endpoint 4" "$(format_endpoints "4,10.0.1.4,signal")" "id addr increment"
170
171for i in $(seq 5 9); do
172	add_endpoint "10.0.1.${i}" flags signal >/dev/null 2>&1
173done
174check "get_endpoint 9" "$(format_endpoints "9,10.0.1.9,signal")" "hard addr limit"
175check "get_endpoint 10" "" "above hard addr limit"
176
177del_endpoint 9
178for i in $(seq 10 255); do
179	add_endpoint 10.0.0.9 id "${i}"
180	del_endpoint "${i}"
181done
182check "show_endpoints" \
183	"$(format_endpoints "1,10.0.1.1" \
184			    "3,10.0.1.3,signal backup" \
185			    "4,10.0.1.4,signal" \
186			    "5,10.0.1.5,signal" \
187			    "6,10.0.1.6,signal" \
188			    "7,10.0.1.7,signal" \
189			    "8,10.0.1.8,signal")" "id limit"
190
191flush_endpoint
192check "show_endpoints" "" "flush addrs"
193
194set_limits 9 1 2>/dev/null
195check "get_limits" "${default_limits}" "rcv addrs above hard limit"
196
197set_limits 1 9 2>/dev/null
198check "get_limits" "${default_limits}" "subflows above hard limit"
199
200set_limits 8 8
201check "get_limits" "$(format_limits 8 8)" "set limits"
202
203flush_endpoint
204add_endpoint 10.0.1.1
205add_endpoint 10.0.1.2
206add_endpoint 10.0.1.3 id 100
207add_endpoint 10.0.1.4
208add_endpoint 10.0.1.5 id 254
209add_endpoint 10.0.1.6
210add_endpoint 10.0.1.7
211add_endpoint 10.0.1.8
212check "show_endpoints" \
213	"$(format_endpoints "1,10.0.1.1" \
214			    "2,10.0.1.2" \
215			    "3,10.0.1.7" \
216			    "4,10.0.1.8" \
217			    "100,10.0.1.3" \
218			    "101,10.0.1.4" \
219			    "254,10.0.1.5" \
220			    "255,10.0.1.6")" "set ids"
221
222flush_endpoint
223add_endpoint 10.0.0.1
224add_endpoint 10.0.0.2 id 254
225add_endpoint 10.0.0.3
226add_endpoint 10.0.0.4
227add_endpoint 10.0.0.5 id 253
228add_endpoint 10.0.0.6
229add_endpoint 10.0.0.7
230add_endpoint 10.0.0.8
231check "show_endpoints" \
232	"$(format_endpoints "1,10.0.0.1" \
233			    "2,10.0.0.4" \
234			    "3,10.0.0.6" \
235			    "4,10.0.0.7" \
236			    "5,10.0.0.8" \
237			    "253,10.0.0.5" \
238			    "254,10.0.0.2" \
239			    "255,10.0.0.3")" "wrap-around ids"
240
241flush_endpoint
242add_endpoint 10.0.1.1 flags subflow
243change_address 10.0.1.1 backup
244check "show_endpoints" "$(format_endpoints "1,10.0.1.1,subflow backup")" \
245	"set flags (backup)"
246change_address 10.0.1.1 nobackup
247check "show_endpoints" "$(format_endpoints "1,10.0.1.1,subflow")" \
248	"          (nobackup)"
249
250# fullmesh support has been added later
251change_endpoint 1 fullmesh 2>/dev/null
252if show_endpoints | grep -q "fullmesh" ||
253   mptcp_lib_expect_all_features; then
254	check "show_endpoints" "$(format_endpoints "1,10.0.1.1,subflow fullmesh")" \
255		"          (fullmesh)"
256	change_endpoint 1 nofullmesh
257	check "show_endpoints" "$(format_endpoints "1,10.0.1.1,subflow")" \
258		"          (nofullmesh)"
259	change_endpoint 1 backup,fullmesh
260	check "show_endpoints" "$(format_endpoints "1,10.0.1.1,subflow backup fullmesh")" \
261		"          (backup,fullmesh)"
262else
263	for st in fullmesh nofullmesh backup,fullmesh; do
264		st="          (${st})"
265		mptcp_lib_print_title "${st}"
266		mptcp_lib_pr_skip
267		mptcp_lib_result_skip "${st}"
268	done
269fi
270
271mptcp_lib_result_print_all_tap
272exit $ret
273