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