xref: /linux/tools/testing/selftests/net/mptcp/pm_netlink.sh (revision afeea2758b4f1210361ce2a91d8fa3e7df606ad2)
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
140check "show_endpoints" "" "defaults addr list"
141
142default_limits="$(get_limits)"
143if mptcp_lib_expect_all_features; then
144	check "get_limits" "$(format_limits 0 2)" "defaults limits"
145fi
146
147add_endpoint 10.0.1.1
148add_endpoint 10.0.1.2 flags subflow dev lo
149add_endpoint 10.0.1.3 flags signal,backup
150check "get_endpoint 1" "$(format_endpoints "1,10.0.1.1")" "simple add/get addr"
151
152check "show_endpoints" \
153	"$(format_endpoints "1,10.0.1.1" \
154			    "2,10.0.1.2,subflow,lo" \
155			    "3,10.0.1.3,signal backup")" "dump addrs"
156
157del_endpoint 2
158check "get_endpoint 2" "" "simple del addr"
159check "show_endpoints" \
160	"$(format_endpoints "1,10.0.1.1" \
161			    "3,10.0.1.3,signal backup")" "dump addrs after del"
162
163add_endpoint 10.0.1.3 2>/dev/null
164check "get_endpoint 4" "" "duplicate addr"
165
166add_endpoint 10.0.1.4 flags signal
167check "get_endpoint 4" "$(format_endpoints "4,10.0.1.4,signal")" "id addr increment"
168
169for i in $(seq 5 9); do
170	add_endpoint "10.0.1.${i}" flags signal >/dev/null 2>&1
171done
172check "get_endpoint 9" "$(format_endpoints "9,10.0.1.9,signal")" "hard addr limit"
173check "get_endpoint 10" "" "above hard addr limit"
174
175del_endpoint 9
176for i in $(seq 10 255); do
177	add_endpoint 10.0.0.9 id "${i}"
178	del_endpoint "${i}"
179done
180check "show_endpoints" \
181	"$(format_endpoints "1,10.0.1.1" \
182			    "3,10.0.1.3,signal backup" \
183			    "4,10.0.1.4,signal" \
184			    "5,10.0.1.5,signal" \
185			    "6,10.0.1.6,signal" \
186			    "7,10.0.1.7,signal" \
187			    "8,10.0.1.8,signal")" "id limit"
188
189flush_endpoint
190check "show_endpoints" "" "flush addrs"
191
192set_limits 9 1 2>/dev/null
193check "get_limits" "${default_limits}" "rcv addrs above hard limit"
194
195set_limits 1 9 2>/dev/null
196check "get_limits" "${default_limits}" "subflows above hard limit"
197
198set_limits 8 8
199check "get_limits" "$(format_limits 8 8)" "set limits"
200
201flush_endpoint
202add_endpoint 10.0.1.1
203add_endpoint 10.0.1.2
204add_endpoint 10.0.1.3 id 100
205add_endpoint 10.0.1.4
206add_endpoint 10.0.1.5 id 254
207add_endpoint 10.0.1.6
208add_endpoint 10.0.1.7
209add_endpoint 10.0.1.8
210check "show_endpoints" \
211	"$(format_endpoints "1,10.0.1.1" \
212			    "2,10.0.1.2" \
213			    "3,10.0.1.7" \
214			    "4,10.0.1.8" \
215			    "100,10.0.1.3" \
216			    "101,10.0.1.4" \
217			    "254,10.0.1.5" \
218			    "255,10.0.1.6")" "set ids"
219
220flush_endpoint
221add_endpoint 10.0.0.1
222add_endpoint 10.0.0.2 id 254
223add_endpoint 10.0.0.3
224add_endpoint 10.0.0.4
225add_endpoint 10.0.0.5 id 253
226add_endpoint 10.0.0.6
227add_endpoint 10.0.0.7
228add_endpoint 10.0.0.8
229check "show_endpoints" \
230	"$(format_endpoints "1,10.0.0.1" \
231			    "2,10.0.0.4" \
232			    "3,10.0.0.6" \
233			    "4,10.0.0.7" \
234			    "5,10.0.0.8" \
235			    "253,10.0.0.5" \
236			    "254,10.0.0.2" \
237			    "255,10.0.0.3")" "wrap-around ids"
238
239flush_endpoint
240add_endpoint 10.0.1.1 flags subflow
241change_address 10.0.1.1 backup
242check "show_endpoints" "$(format_endpoints "1,10.0.1.1,subflow backup")" \
243	"set flags (backup)"
244change_address 10.0.1.1 nobackup
245check "show_endpoints" "$(format_endpoints "1,10.0.1.1,subflow")" \
246	"          (nobackup)"
247
248# fullmesh support has been added later
249change_endpoint 1 fullmesh 2>/dev/null
250if show_endpoints | grep -q "fullmesh" ||
251   mptcp_lib_expect_all_features; then
252	check "show_endpoints" "$(format_endpoints "1,10.0.1.1,subflow fullmesh")" \
253		"          (fullmesh)"
254	change_endpoint 1 nofullmesh
255	check "show_endpoints" "$(format_endpoints "1,10.0.1.1,subflow")" \
256		"          (nofullmesh)"
257	change_endpoint 1 backup,fullmesh
258	check "show_endpoints" "$(format_endpoints "1,10.0.1.1,subflow backup fullmesh")" \
259		"          (backup,fullmesh)"
260else
261	for st in fullmesh nofullmesh backup,fullmesh; do
262		st="          (${st})"
263		mptcp_lib_print_title "${st}"
264		mptcp_lib_pr_skip
265		mptcp_lib_result_skip "${st}"
266	done
267fi
268
269mptcp_lib_result_print_all_tap
270exit $ret
271