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 %s" "$msg"
62	if [ $cmd_ret -ne 0 ]; then
63		echo "[FAIL] command execution '$cmd' stderr "
64		cat $err
65		ret=1
66	elif [ "$out" = "$expected" ]; then
67		echo "[ OK ]"
68	else
69		echo -n "[FAIL] "
70		echo "expected '$expected' got '$out'"
71		ret=1
72	fi
73}
74
75check "ip netns exec $ns1 ./pm_nl_ctl dump" "" "defaults addr list"
76
77default_limits="$(ip netns exec $ns1 ./pm_nl_ctl limits)"
78if mptcp_lib_expect_all_features; then
79	check "ip netns exec $ns1 ./pm_nl_ctl limits" "accept 0
80subflows 2" "defaults limits"
81fi
82
83ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.1
84ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.2 flags subflow dev lo
85ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.3 flags signal,backup
86check "ip netns exec $ns1 ./pm_nl_ctl get 1" "id 1 flags  10.0.1.1" "simple add/get addr"
87
88check "ip netns exec $ns1 ./pm_nl_ctl dump" \
89"id 1 flags  10.0.1.1
90id 2 flags subflow dev lo 10.0.1.2
91id 3 flags signal,backup 10.0.1.3" "dump addrs"
92
93ip netns exec $ns1 ./pm_nl_ctl del 2
94check "ip netns exec $ns1 ./pm_nl_ctl get 2" "" "simple del addr"
95check "ip netns exec $ns1 ./pm_nl_ctl dump" \
96"id 1 flags  10.0.1.1
97id 3 flags signal,backup 10.0.1.3" "dump addrs after del"
98
99ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.3
100check "ip netns exec $ns1 ./pm_nl_ctl get 4" "" "duplicate addr"
101
102ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.4 flags signal
103check "ip netns exec $ns1 ./pm_nl_ctl get 4" "id 4 flags signal 10.0.1.4" "id addr increment"
104
105for i in `seq 5 9`; do
106	ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.$i flags signal >/dev/null 2>&1
107done
108check "ip netns exec $ns1 ./pm_nl_ctl get 9" "id 9 flags signal 10.0.1.9" "hard addr limit"
109check "ip netns exec $ns1 ./pm_nl_ctl get 10" "" "above hard addr limit"
110
111ip netns exec $ns1 ./pm_nl_ctl del 9
112for i in `seq 10 255`; do
113	ip netns exec $ns1 ./pm_nl_ctl add 10.0.0.9 id $i
114	ip netns exec $ns1 ./pm_nl_ctl del $i
115done
116check "ip netns exec $ns1 ./pm_nl_ctl dump" "id 1 flags  10.0.1.1
117id 3 flags signal,backup 10.0.1.3
118id 4 flags signal 10.0.1.4
119id 5 flags signal 10.0.1.5
120id 6 flags signal 10.0.1.6
121id 7 flags signal 10.0.1.7
122id 8 flags signal 10.0.1.8" "id limit"
123
124ip netns exec $ns1 ./pm_nl_ctl flush
125check "ip netns exec $ns1 ./pm_nl_ctl dump" "" "flush addrs"
126
127ip netns exec $ns1 ./pm_nl_ctl limits 9 1
128check "ip netns exec $ns1 ./pm_nl_ctl limits" "$default_limits" "rcv addrs above hard limit"
129
130ip netns exec $ns1 ./pm_nl_ctl limits 1 9
131check "ip netns exec $ns1 ./pm_nl_ctl limits" "$default_limits" "subflows above hard limit"
132
133ip netns exec $ns1 ./pm_nl_ctl limits 8 8
134check "ip netns exec $ns1 ./pm_nl_ctl limits" "accept 8
135subflows 8" "set limits"
136
137ip netns exec $ns1 ./pm_nl_ctl flush
138ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.1
139ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.2
140ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.3 id 100
141ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.4
142ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.5 id 254
143ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.6
144ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.7
145ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.8
146check "ip netns exec $ns1 ./pm_nl_ctl dump" "id 1 flags  10.0.1.1
147id 2 flags  10.0.1.2
148id 3 flags  10.0.1.7
149id 4 flags  10.0.1.8
150id 100 flags  10.0.1.3
151id 101 flags  10.0.1.4
152id 254 flags  10.0.1.5
153id 255 flags  10.0.1.6" "set ids"
154
155ip netns exec $ns1 ./pm_nl_ctl flush
156ip netns exec $ns1 ./pm_nl_ctl add 10.0.0.1
157ip netns exec $ns1 ./pm_nl_ctl add 10.0.0.2 id 254
158ip netns exec $ns1 ./pm_nl_ctl add 10.0.0.3
159ip netns exec $ns1 ./pm_nl_ctl add 10.0.0.4
160ip netns exec $ns1 ./pm_nl_ctl add 10.0.0.5 id 253
161ip netns exec $ns1 ./pm_nl_ctl add 10.0.0.6
162ip netns exec $ns1 ./pm_nl_ctl add 10.0.0.7
163ip netns exec $ns1 ./pm_nl_ctl add 10.0.0.8
164check "ip netns exec $ns1 ./pm_nl_ctl dump" "id 1 flags  10.0.0.1
165id 2 flags  10.0.0.4
166id 3 flags  10.0.0.6
167id 4 flags  10.0.0.7
168id 5 flags  10.0.0.8
169id 253 flags  10.0.0.5
170id 254 flags  10.0.0.2
171id 255 flags  10.0.0.3" "wrap-around ids"
172
173ip netns exec $ns1 ./pm_nl_ctl flush
174ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.1 flags subflow
175ip netns exec $ns1 ./pm_nl_ctl set 10.0.1.1 flags backup
176check "ip netns exec $ns1 ./pm_nl_ctl dump" "id 1 flags \
177subflow,backup 10.0.1.1" "set flags (backup)"
178ip netns exec $ns1 ./pm_nl_ctl set 10.0.1.1 flags nobackup
179check "ip netns exec $ns1 ./pm_nl_ctl dump" "id 1 flags \
180subflow 10.0.1.1" "          (nobackup)"
181
182# fullmesh support has been added later
183ip netns exec $ns1 ./pm_nl_ctl set id 1 flags fullmesh
184if ip netns exec $ns1 ./pm_nl_ctl dump | grep -q "fullmesh" ||
185   mptcp_lib_expect_all_features; then
186	check "ip netns exec $ns1 ./pm_nl_ctl dump" "id 1 flags \
187subflow,fullmesh 10.0.1.1" "          (fullmesh)"
188	ip netns exec $ns1 ./pm_nl_ctl set id 1 flags nofullmesh
189	check "ip netns exec $ns1 ./pm_nl_ctl dump" "id 1 flags \
190subflow 10.0.1.1" "          (nofullmesh)"
191	ip netns exec $ns1 ./pm_nl_ctl set id 1 flags backup,fullmesh
192	check "ip netns exec $ns1 ./pm_nl_ctl dump" "id 1 flags \
193subflow,backup,fullmesh 10.0.1.1" "          (backup,fullmesh)"
194fi
195
196exit $ret
197