1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4. "$(dirname "${0}")/mptcp_lib.sh"
5
6ksft_skip=4
7ret=0
8
9usage() {
10	echo "Usage: $0 [ -h ]"
11}
12
13
14while getopts "$optstring" option;do
15	case "$option" in
16	"h")
17		usage $0
18		exit 0
19		;;
20	"?")
21		usage $0
22		exit 1
23		;;
24	esac
25done
26
27sec=$(date +%s)
28rndh=$(printf %x $sec)-$(mktemp -u XXXXXX)
29ns1="ns1-$rndh"
30err=$(mktemp)
31ret=0
32
33cleanup()
34{
35	rm -f $err
36	ip netns del $ns1
37}
38
39mptcp_lib_check_mptcp
40
41ip -Version > /dev/null 2>&1
42if [ $? -ne 0 ];then
43	echo "SKIP: Could not run test without ip tool"
44	exit $ksft_skip
45fi
46
47trap cleanup EXIT
48
49ip netns add $ns1 || exit $ksft_skip
50ip -net $ns1 link set lo up
51ip netns exec $ns1 sysctl -q net.mptcp.enabled=1
52
53check()
54{
55	local cmd="$1"
56	local expected="$2"
57	local msg="$3"
58	local out=`$cmd 2>$err`
59	local cmd_ret=$?
60
61	printf "%-50s" "$msg"
62	if [ $cmd_ret -ne 0 ]; then
63		echo "[FAIL] command execution '$cmd' stderr "
64		cat $err
65		mptcp_lib_result_fail "${msg} # error ${cmd_ret}"
66		ret=1
67	elif [ "$out" = "$expected" ]; then
68		echo "[ OK ]"
69		mptcp_lib_result_pass "${msg}"
70	else
71		echo -n "[FAIL] "
72		echo "expected '$expected' got '$out'"
73		mptcp_lib_result_fail "${msg} # different output"
74		ret=1
75	fi
76}
77
78check "ip netns exec $ns1 ./pm_nl_ctl dump" "" "defaults addr list"
79
80default_limits="$(ip netns exec $ns1 ./pm_nl_ctl limits)"
81if mptcp_lib_expect_all_features; then
82	check "ip netns exec $ns1 ./pm_nl_ctl limits" "accept 0
83subflows 2" "defaults limits"
84fi
85
86ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.1
87ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.2 flags subflow dev lo
88ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.3 flags signal,backup
89check "ip netns exec $ns1 ./pm_nl_ctl get 1" "id 1 flags  10.0.1.1" "simple add/get addr"
90
91check "ip netns exec $ns1 ./pm_nl_ctl dump" \
92"id 1 flags  10.0.1.1
93id 2 flags subflow dev lo 10.0.1.2
94id 3 flags signal,backup 10.0.1.3" "dump addrs"
95
96ip netns exec $ns1 ./pm_nl_ctl del 2
97check "ip netns exec $ns1 ./pm_nl_ctl get 2" "" "simple del addr"
98check "ip netns exec $ns1 ./pm_nl_ctl dump" \
99"id 1 flags  10.0.1.1
100id 3 flags signal,backup 10.0.1.3" "dump addrs after del"
101
102ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.3 2>/dev/null
103check "ip netns exec $ns1 ./pm_nl_ctl get 4" "" "duplicate addr"
104
105ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.4 flags signal
106check "ip netns exec $ns1 ./pm_nl_ctl get 4" "id 4 flags signal 10.0.1.4" "id addr increment"
107
108for i in `seq 5 9`; do
109	ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.$i flags signal >/dev/null 2>&1
110done
111check "ip netns exec $ns1 ./pm_nl_ctl get 9" "id 9 flags signal 10.0.1.9" "hard addr limit"
112check "ip netns exec $ns1 ./pm_nl_ctl get 10" "" "above hard addr limit"
113
114ip netns exec $ns1 ./pm_nl_ctl del 9
115for i in `seq 10 255`; do
116	ip netns exec $ns1 ./pm_nl_ctl add 10.0.0.9 id $i
117	ip netns exec $ns1 ./pm_nl_ctl del $i
118done
119check "ip netns exec $ns1 ./pm_nl_ctl dump" "id 1 flags  10.0.1.1
120id 3 flags signal,backup 10.0.1.3
121id 4 flags signal 10.0.1.4
122id 5 flags signal 10.0.1.5
123id 6 flags signal 10.0.1.6
124id 7 flags signal 10.0.1.7
125id 8 flags signal 10.0.1.8" "id limit"
126
127ip netns exec $ns1 ./pm_nl_ctl flush
128check "ip netns exec $ns1 ./pm_nl_ctl dump" "" "flush addrs"
129
130ip netns exec $ns1 ./pm_nl_ctl limits 9 1 2>/dev/null
131check "ip netns exec $ns1 ./pm_nl_ctl limits" "$default_limits" "rcv addrs above hard limit"
132
133ip netns exec $ns1 ./pm_nl_ctl limits 1 9 2>/dev/null
134check "ip netns exec $ns1 ./pm_nl_ctl limits" "$default_limits" "subflows above hard limit"
135
136ip netns exec $ns1 ./pm_nl_ctl limits 8 8
137check "ip netns exec $ns1 ./pm_nl_ctl limits" "accept 8
138subflows 8" "set limits"
139
140ip netns exec $ns1 ./pm_nl_ctl flush
141ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.1
142ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.2
143ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.3 id 100
144ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.4
145ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.5 id 254
146ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.6
147ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.7
148ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.8
149check "ip netns exec $ns1 ./pm_nl_ctl dump" "id 1 flags  10.0.1.1
150id 2 flags  10.0.1.2
151id 3 flags  10.0.1.7
152id 4 flags  10.0.1.8
153id 100 flags  10.0.1.3
154id 101 flags  10.0.1.4
155id 254 flags  10.0.1.5
156id 255 flags  10.0.1.6" "set ids"
157
158ip netns exec $ns1 ./pm_nl_ctl flush
159ip netns exec $ns1 ./pm_nl_ctl add 10.0.0.1
160ip netns exec $ns1 ./pm_nl_ctl add 10.0.0.2 id 254
161ip netns exec $ns1 ./pm_nl_ctl add 10.0.0.3
162ip netns exec $ns1 ./pm_nl_ctl add 10.0.0.4
163ip netns exec $ns1 ./pm_nl_ctl add 10.0.0.5 id 253
164ip netns exec $ns1 ./pm_nl_ctl add 10.0.0.6
165ip netns exec $ns1 ./pm_nl_ctl add 10.0.0.7
166ip netns exec $ns1 ./pm_nl_ctl add 10.0.0.8
167check "ip netns exec $ns1 ./pm_nl_ctl dump" "id 1 flags  10.0.0.1
168id 2 flags  10.0.0.4
169id 3 flags  10.0.0.6
170id 4 flags  10.0.0.7
171id 5 flags  10.0.0.8
172id 253 flags  10.0.0.5
173id 254 flags  10.0.0.2
174id 255 flags  10.0.0.3" "wrap-around ids"
175
176ip netns exec $ns1 ./pm_nl_ctl flush
177ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.1 flags subflow
178ip netns exec $ns1 ./pm_nl_ctl set 10.0.1.1 flags backup
179check "ip netns exec $ns1 ./pm_nl_ctl dump" "id 1 flags \
180subflow,backup 10.0.1.1" "set flags (backup)"
181ip netns exec $ns1 ./pm_nl_ctl set 10.0.1.1 flags nobackup
182check "ip netns exec $ns1 ./pm_nl_ctl dump" "id 1 flags \
183subflow 10.0.1.1" "          (nobackup)"
184
185# fullmesh support has been added later
186ip netns exec $ns1 ./pm_nl_ctl set id 1 flags fullmesh
187if ip netns exec $ns1 ./pm_nl_ctl dump | grep -q "fullmesh" ||
188   mptcp_lib_expect_all_features; then
189	check "ip netns exec $ns1 ./pm_nl_ctl dump" "id 1 flags \
190subflow,fullmesh 10.0.1.1" "          (fullmesh)"
191	ip netns exec $ns1 ./pm_nl_ctl set id 1 flags nofullmesh
192	check "ip netns exec $ns1 ./pm_nl_ctl dump" "id 1 flags \
193subflow 10.0.1.1" "          (nofullmesh)"
194	ip netns exec $ns1 ./pm_nl_ctl set id 1 flags backup,fullmesh
195	check "ip netns exec $ns1 ./pm_nl_ctl dump" "id 1 flags \
196subflow,backup,fullmesh 10.0.1.1" "          (backup,fullmesh)"
197fi
198
199mptcp_lib_result_print_all_tap
200exit $ret
201