259382ad552c3019c279d4aeeaedb751eb2af9e2
[platform/kernel/linux-rpi.git] / tools / testing / selftests / net / mptcp / userspace_pm.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3
4 ip -Version > /dev/null 2>&1
5 if [ $? -ne 0 ];then
6         echo "SKIP: Cannot not run test without ip tool"
7         exit 1
8 fi
9
10 ANNOUNCED=6        # MPTCP_EVENT_ANNOUNCED
11 REMOVED=7          # MPTCP_EVENT_REMOVED
12 SUB_ESTABLISHED=10 # MPTCP_EVENT_SUB_ESTABLISHED
13 SUB_CLOSED=11      # MPTCP_EVENT_SUB_CLOSED
14 LISTENER_CREATED=15 #MPTCP_EVENT_LISTENER_CREATED
15 LISTENER_CLOSED=16  #MPTCP_EVENT_LISTENER_CLOSED
16
17 AF_INET=2
18 AF_INET6=10
19
20 file=""
21 server_evts=""
22 client_evts=""
23 server_evts_pid=0
24 client_evts_pid=0
25 client4_pid=0
26 server4_pid=0
27 client6_pid=0
28 server6_pid=0
29 client4_token=""
30 server4_token=""
31 client6_token=""
32 server6_token=""
33 client4_port=0;
34 client6_port=0;
35 app4_port=50002
36 new4_port=50003
37 app6_port=50004
38 client_addr_id=${RANDOM:0:2}
39 server_addr_id=${RANDOM:0:2}
40
41 sec=$(date +%s)
42 rndh=$(printf %x "$sec")-$(mktemp -u XXXXXX)
43 ns1="ns1-$rndh"
44 ns2="ns2-$rndh"
45
46 print_title()
47 {
48         stdbuf -o0 -e0 printf "INFO: %s\n" "${1}"
49 }
50
51 kill_wait()
52 {
53         kill $1 > /dev/null 2>&1
54         wait $1 2>/dev/null
55 }
56
57 cleanup()
58 {
59         print_title "Cleanup"
60
61         rm -rf $file $client_evts $server_evts
62
63         # Terminate the MPTCP connection and related processes
64         if [ $client4_pid -ne 0 ]; then
65                 kill -SIGUSR1 $client4_pid > /dev/null 2>&1
66         fi
67         if [ $server4_pid -ne 0 ]; then
68                 kill_wait $server4_pid
69         fi
70         if [ $client6_pid -ne 0 ]; then
71                 kill -SIGUSR1 $client6_pid > /dev/null 2>&1
72         fi
73         if [ $server6_pid -ne 0 ]; then
74                 kill_wait $server6_pid
75         fi
76         if [ $server_evts_pid -ne 0 ]; then
77                 kill_wait $server_evts_pid
78         fi
79         if [ $client_evts_pid -ne 0 ]; then
80                 kill_wait $client_evts_pid
81         fi
82         local netns
83         for netns in "$ns1" "$ns2" ;do
84                 ip netns del "$netns"
85         done
86
87         stdbuf -o0 -e0 printf "Done\n"
88 }
89
90 trap cleanup EXIT
91
92 # Create and configure network namespaces for testing
93 for i in "$ns1" "$ns2" ;do
94         ip netns add "$i" || exit 1
95         ip -net "$i" link set lo up
96         ip netns exec "$i" sysctl -q net.mptcp.enabled=1
97         ip netns exec "$i" sysctl -q net.mptcp.pm_type=1
98 done
99
100 #  "$ns1"              ns2
101 #     ns1eth2    ns2eth1
102
103 ip link add ns1eth2 netns "$ns1" type veth peer name ns2eth1 netns "$ns2"
104
105 # Add IPv4/v6 addresses to the namespaces
106 ip -net "$ns1" addr add 10.0.1.1/24 dev ns1eth2
107 ip -net "$ns1" addr add 10.0.2.1/24 dev ns1eth2
108 ip -net "$ns1" addr add dead:beef:1::1/64 dev ns1eth2 nodad
109 ip -net "$ns1" addr add dead:beef:2::1/64 dev ns1eth2 nodad
110 ip -net "$ns1" link set ns1eth2 up
111
112 ip -net "$ns2" addr add 10.0.1.2/24 dev ns2eth1
113 ip -net "$ns2" addr add 10.0.2.2/24 dev ns2eth1
114 ip -net "$ns2" addr add dead:beef:1::2/64 dev ns2eth1 nodad
115 ip -net "$ns2" addr add dead:beef:2::2/64 dev ns2eth1 nodad
116 ip -net "$ns2" link set ns2eth1 up
117
118 print_title "Init"
119 stdbuf -o0 -e0 printf "Created network namespaces ns1, ns2         \t\t\t[OK]\n"
120
121 make_file()
122 {
123         # Store a chunk of data in a file to transmit over an MPTCP connection
124         local name=$1
125         local ksize=1
126
127         dd if=/dev/urandom of="$name" bs=2 count=$ksize 2> /dev/null
128         echo -e "\nMPTCP_TEST_FILE_END_MARKER" >> "$name"
129 }
130
131 make_connection()
132 {
133         if [ -z "$file" ]; then
134                 file=$(mktemp)
135         fi
136         make_file "$file" "client"
137
138         local is_v6=$1
139         local app_port=$app4_port
140         local connect_addr="10.0.1.1"
141         local listen_addr="0.0.0.0"
142         if [ "$is_v6" = "v6" ]
143         then
144                 connect_addr="dead:beef:1::1"
145                 listen_addr="::"
146                 app_port=$app6_port
147         else
148                 is_v6="v4"
149         fi
150
151         # Capture netlink events over the two network namespaces running
152         # the MPTCP client and server
153         if [ -z "$client_evts" ]; then
154                 client_evts=$(mktemp)
155         fi
156         :>"$client_evts"
157         if [ $client_evts_pid -ne 0 ]; then
158                 kill_wait $client_evts_pid
159         fi
160         ip netns exec "$ns2" ./pm_nl_ctl events >> "$client_evts" 2>&1 &
161         client_evts_pid=$!
162         if [ -z "$server_evts" ]; then
163                 server_evts=$(mktemp)
164         fi
165         :>"$server_evts"
166         if [ $server_evts_pid -ne 0 ]; then
167                 kill_wait $server_evts_pid
168         fi
169         ip netns exec "$ns1" ./pm_nl_ctl events >> "$server_evts" 2>&1 &
170         server_evts_pid=$!
171         sleep 0.5
172
173         # Run the server
174         ip netns exec "$ns1" \
175            ./mptcp_connect -s MPTCP -w 300 -p $app_port -l $listen_addr > /dev/null 2>&1 &
176         local server_pid=$!
177         sleep 0.5
178
179         # Run the client, transfer $file and stay connected to the server
180         # to conduct tests
181         ip netns exec "$ns2" \
182            ./mptcp_connect -s MPTCP -w 300 -m sendfile -p $app_port $connect_addr\
183            2>&1 > /dev/null < "$file" &
184         local client_pid=$!
185         sleep 1
186
187         # Capture client/server attributes from MPTCP connection netlink events
188
189         local client_token
190         local client_port
191         local client_serverside
192         local server_token
193         local server_serverside
194
195         client_token=$(sed --unbuffered -n 's/.*\(token:\)\([[:digit:]]*\).*$/\2/p;q' "$client_evts")
196         client_port=$(sed --unbuffered -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q' "$client_evts")
197         client_serverside=$(sed --unbuffered -n 's/.*\(server_side:\)\([[:digit:]]*\).*$/\2/p;q'\
198                                       "$client_evts")
199         server_token=$(grep "type:1," "$server_evts" |
200                        sed --unbuffered -n 's/.*\(token:\)\([[:digit:]]*\).*$/\2/p;q')
201         server_serverside=$(grep "type:1," "$server_evts" |
202                             sed --unbuffered -n 's/.*\(server_side:\)\([[:digit:]]*\).*$/\2/p;q')
203
204         stdbuf -o0 -e0 printf "Established IP%s MPTCP Connection ns2 => ns1    \t\t" $is_v6
205         if [ "$client_token" != "" ] && [ "$server_token" != "" ] && [ "$client_serverside" = 0 ] &&
206                    [ "$server_serverside" = 1 ]
207         then
208                 stdbuf -o0 -e0 printf "[OK]\n"
209         else
210                 stdbuf -o0 -e0 printf "[FAIL]\n"
211                 stdbuf -o0 -e0 printf "\tExpected tokens (c:%s - s:%s) and server (c:%d - s:%d)\n" \
212                         "${client_token}" "${server_token}" \
213                         "${client_serverside}" "${server_serverside}"
214                 exit 1
215         fi
216
217         if [ "$is_v6" = "v6" ]
218         then
219                 client6_token=$client_token
220                 server6_token=$server_token
221                 client6_port=$client_port
222                 client6_pid=$client_pid
223                 server6_pid=$server_pid
224         else
225                 client4_token=$client_token
226                 server4_token=$server_token
227                 client4_port=$client_port
228                 client4_pid=$client_pid
229                 server4_pid=$server_pid
230         fi
231 }
232
233 # $1: var name ; $2: prev ret
234 check_expected_one()
235 {
236         local var="${1}"
237         local exp="e_${var}"
238         local prev_ret="${2}"
239
240         if [ "${!var}" = "${!exp}" ]
241         then
242                 return 0
243         fi
244
245         if [ "${prev_ret}" = "0" ]
246         then
247                 stdbuf -o0 -e0 printf "[FAIL]\n"
248         fi
249
250         stdbuf -o0 -e0 printf "\tExpected value for '%s': '%s', got '%s'.\n" \
251                 "${var}" "${!var}" "${!exp}"
252         return 1
253 }
254
255 # $@: all var names to check
256 check_expected()
257 {
258         local ret=0
259         local var
260
261         for var in "${@}"
262         do
263                 check_expected_one "${var}" "${ret}" || ret=1
264         done
265
266         if [ ${ret} -eq 0 ]
267         then
268                 stdbuf -o0 -e0 printf "[OK]\n"
269                 return 0
270         fi
271
272         exit 1
273 }
274
275 verify_announce_event()
276 {
277         local evt=$1
278         local e_type=$2
279         local e_token=$3
280         local e_addr=$4
281         local e_id=$5
282         local e_dport=$6
283         local e_af=$7
284         local type
285         local token
286         local addr
287         local dport
288         local id
289
290         type=$(sed --unbuffered -n 's/.*\(type:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
291         token=$(sed --unbuffered -n 's/.*\(token:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
292         if [ "$e_af" = "v6" ]
293         then
294                 addr=$(sed --unbuffered -n 's/.*\(daddr6:\)\([0-9a-f:.]*\).*$/\2/p;q' "$evt")
295         else
296                 addr=$(sed --unbuffered -n 's/.*\(daddr4:\)\([0-9.]*\).*$/\2/p;q' "$evt")
297         fi
298         dport=$(sed --unbuffered -n 's/.*\(dport:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
299         id=$(sed --unbuffered -n 's/.*\(rem_id:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
300
301         check_expected "type" "token" "addr" "dport" "id"
302 }
303
304 test_announce()
305 {
306         print_title "Announce tests"
307
308         # Capture events on the network namespace running the server
309         :>"$server_evts"
310
311         # ADD_ADDR using an invalid token should result in no action
312         local invalid_token=$(( client4_token - 1))
313         ip netns exec "$ns2" ./pm_nl_ctl ann 10.0.2.2 token $invalid_token id\
314            $client_addr_id dev ns2eth1 > /dev/null 2>&1
315
316         local type
317         type=$(sed --unbuffered -n 's/.*\(type:\)\([[:digit:]]*\).*$/\2/p;q' "$server_evts")
318         stdbuf -o0 -e0 printf "ADD_ADDR 10.0.2.2 (ns2) => ns1, invalid token    \t\t"
319         if [ "$type" = "" ]
320         then
321                 stdbuf -o0 -e0 printf "[OK]\n"
322         else
323                 stdbuf -o0 -e0 printf "[FAIL]\n\ttype defined: %s\n" "${type}"
324                 exit 1
325         fi
326
327         # ADD_ADDR from the client to server machine reusing the subflow port
328         :>"$server_evts"
329         ip netns exec "$ns2"\
330            ./pm_nl_ctl ann 10.0.2.2 token "$client4_token" id $client_addr_id dev\
331            ns2eth1 > /dev/null 2>&1
332         stdbuf -o0 -e0 printf "ADD_ADDR id:%d 10.0.2.2 (ns2) => ns1, reuse port \t\t" $client_addr_id
333         sleep 0.5
334         verify_announce_event $server_evts $ANNOUNCED $server4_token "10.0.2.2" $client_addr_id \
335                               "$client4_port"
336
337         # ADD_ADDR6 from the client to server machine reusing the subflow port
338         :>"$server_evts"
339         ip netns exec "$ns2" ./pm_nl_ctl ann\
340            dead:beef:2::2 token "$client6_token" id $client_addr_id dev ns2eth1 > /dev/null 2>&1
341         stdbuf -o0 -e0 printf "ADD_ADDR6 id:%d dead:beef:2::2 (ns2) => ns1, reuse port\t\t" $client_addr_id
342         sleep 0.5
343         verify_announce_event "$server_evts" "$ANNOUNCED" "$server6_token" "dead:beef:2::2"\
344                               "$client_addr_id" "$client6_port" "v6"
345
346         # ADD_ADDR from the client to server machine using a new port
347         :>"$server_evts"
348         client_addr_id=$((client_addr_id+1))
349         ip netns exec "$ns2" ./pm_nl_ctl ann 10.0.2.2 token "$client4_token" id\
350            $client_addr_id dev ns2eth1 port $new4_port > /dev/null 2>&1
351         stdbuf -o0 -e0 printf "ADD_ADDR id:%d 10.0.2.2 (ns2) => ns1, new port \t\t\t" $client_addr_id
352         sleep 0.5
353         verify_announce_event "$server_evts" "$ANNOUNCED" "$server4_token" "10.0.2.2"\
354                               "$client_addr_id" "$new4_port"
355
356         # Capture events on the network namespace running the client
357         :>"$client_evts"
358
359         # ADD_ADDR from the server to client machine reusing the subflow port
360         ip netns exec "$ns1" ./pm_nl_ctl ann 10.0.2.1 token "$server4_token" id\
361            $server_addr_id dev ns1eth2 > /dev/null 2>&1
362         stdbuf -o0 -e0 printf "ADD_ADDR id:%d 10.0.2.1 (ns1) => ns2, reuse port \t\t" $server_addr_id
363         sleep 0.5
364         verify_announce_event "$client_evts" "$ANNOUNCED" "$client4_token" "10.0.2.1"\
365                               "$server_addr_id" "$app4_port"
366
367         # ADD_ADDR6 from the server to client machine reusing the subflow port
368         :>"$client_evts"
369         ip netns exec "$ns1" ./pm_nl_ctl ann dead:beef:2::1 token "$server6_token" id\
370            $server_addr_id dev ns1eth2 > /dev/null 2>&1
371         stdbuf -o0 -e0 printf "ADD_ADDR6 id:%d dead:beef:2::1 (ns1) => ns2, reuse port\t\t" $server_addr_id
372         sleep 0.5
373         verify_announce_event "$client_evts" "$ANNOUNCED" "$client6_token" "dead:beef:2::1"\
374                               "$server_addr_id" "$app6_port" "v6"
375
376         # ADD_ADDR from the server to client machine using a new port
377         :>"$client_evts"
378         server_addr_id=$((server_addr_id+1))
379         ip netns exec "$ns1" ./pm_nl_ctl ann 10.0.2.1 token "$server4_token" id\
380            $server_addr_id dev ns1eth2 port $new4_port > /dev/null 2>&1
381         stdbuf -o0 -e0 printf "ADD_ADDR id:%d 10.0.2.1 (ns1) => ns2, new port \t\t\t" $server_addr_id
382         sleep 0.5
383         verify_announce_event "$client_evts" "$ANNOUNCED" "$client4_token" "10.0.2.1"\
384                               "$server_addr_id" "$new4_port"
385 }
386
387 verify_remove_event()
388 {
389         local evt=$1
390         local e_type=$2
391         local e_token=$3
392         local e_id=$4
393         local type
394         local token
395         local id
396
397         type=$(sed --unbuffered -n 's/.*\(type:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
398         token=$(sed --unbuffered -n 's/.*\(token:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
399         id=$(sed --unbuffered -n 's/.*\(rem_id:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
400
401         check_expected "type" "token" "id"
402 }
403
404 test_remove()
405 {
406         print_title "Remove tests"
407
408         # Capture events on the network namespace running the server
409         :>"$server_evts"
410
411         # RM_ADDR using an invalid token should result in no action
412         local invalid_token=$(( client4_token - 1 ))
413         ip netns exec "$ns2" ./pm_nl_ctl rem token $invalid_token id\
414            $client_addr_id > /dev/null 2>&1
415         stdbuf -o0 -e0 printf "RM_ADDR id:%d ns2 => ns1, invalid token                    \t"\
416                $client_addr_id
417         local type
418         type=$(sed --unbuffered -n 's/.*\(type:\)\([[:digit:]]*\).*$/\2/p;q' "$server_evts")
419         if [ "$type" = "" ]
420         then
421                 stdbuf -o0 -e0 printf "[OK]\n"
422         else
423                 stdbuf -o0 -e0 printf "[FAIL]\n"
424         fi
425
426         # RM_ADDR using an invalid addr id should result in no action
427         local invalid_id=$(( client_addr_id + 1 ))
428         ip netns exec "$ns2" ./pm_nl_ctl rem token "$client4_token" id\
429            $invalid_id > /dev/null 2>&1
430         stdbuf -o0 -e0 printf "RM_ADDR id:%d ns2 => ns1, invalid id                    \t"\
431                $invalid_id
432         type=$(sed --unbuffered -n 's/.*\(type:\)\([[:digit:]]*\).*$/\2/p;q' "$server_evts")
433         if [ "$type" = "" ]
434         then
435                 stdbuf -o0 -e0 printf "[OK]\n"
436         else
437                 stdbuf -o0 -e0 printf "[FAIL]\n"
438         fi
439
440         # RM_ADDR from the client to server machine
441         :>"$server_evts"
442         ip netns exec "$ns2" ./pm_nl_ctl rem token "$client4_token" id\
443            $client_addr_id > /dev/null 2>&1
444         stdbuf -o0 -e0 printf "RM_ADDR id:%d ns2 => ns1                                \t"\
445                $client_addr_id
446         sleep 0.5
447         verify_remove_event "$server_evts" "$REMOVED" "$server4_token" "$client_addr_id"
448
449         # RM_ADDR from the client to server machine
450         :>"$server_evts"
451         client_addr_id=$(( client_addr_id - 1 ))
452         ip netns exec "$ns2" ./pm_nl_ctl rem token "$client4_token" id\
453            $client_addr_id > /dev/null 2>&1
454         stdbuf -o0 -e0 printf "RM_ADDR id:%d ns2 => ns1                                \t"\
455                $client_addr_id
456         sleep 0.5
457         verify_remove_event "$server_evts" "$REMOVED" "$server4_token" "$client_addr_id"
458
459         # RM_ADDR6 from the client to server machine
460         :>"$server_evts"
461         ip netns exec "$ns2" ./pm_nl_ctl rem token "$client6_token" id\
462            $client_addr_id > /dev/null 2>&1
463         stdbuf -o0 -e0 printf "RM_ADDR6 id:%d ns2 => ns1                               \t"\
464                $client_addr_id
465         sleep 0.5
466         verify_remove_event "$server_evts" "$REMOVED" "$server6_token" "$client_addr_id"
467
468         # Capture events on the network namespace running the client
469         :>"$client_evts"
470
471         # RM_ADDR from the server to client machine
472         ip netns exec "$ns1" ./pm_nl_ctl rem token "$server4_token" id\
473            $server_addr_id > /dev/null 2>&1
474         stdbuf -o0 -e0 printf "RM_ADDR id:%d ns1 => ns2                                \t"\
475                $server_addr_id
476         sleep 0.5
477         verify_remove_event "$client_evts" "$REMOVED" "$client4_token" "$server_addr_id"
478
479         # RM_ADDR from the server to client machine
480         :>"$client_evts"
481         server_addr_id=$(( server_addr_id - 1 ))
482         ip netns exec "$ns1" ./pm_nl_ctl rem token "$server4_token" id\
483            $server_addr_id > /dev/null 2>&1
484         stdbuf -o0 -e0 printf "RM_ADDR id:%d ns1 => ns2                                \t" $server_addr_id
485         sleep 0.5
486         verify_remove_event "$client_evts" "$REMOVED" "$client4_token" "$server_addr_id"
487
488         # RM_ADDR6 from the server to client machine
489         :>"$client_evts"
490         ip netns exec "$ns1" ./pm_nl_ctl rem token "$server6_token" id\
491            $server_addr_id > /dev/null 2>&1
492         stdbuf -o0 -e0 printf "RM_ADDR6 id:%d ns1 => ns2                               \t" $server_addr_id
493         sleep 0.5
494         verify_remove_event "$client_evts" "$REMOVED" "$client6_token" "$server_addr_id"
495 }
496
497 verify_subflow_events()
498 {
499         local evt=$1
500         local e_type=$2
501         local e_token=$3
502         local e_family=$4
503         local e_saddr=$5
504         local e_daddr=$6
505         local e_dport=$7
506         local e_locid=$8
507         local e_remid=$9
508         shift 2
509         local e_from=$8
510         local e_to=$9
511         local type
512         local token
513         local family
514         local saddr
515         local daddr
516         local dport
517         local locid
518         local remid
519
520         if [ "$e_type" = "$SUB_ESTABLISHED" ]
521         then
522                 if [ "$e_family" = "$AF_INET6" ]
523                 then
524                         stdbuf -o0 -e0 printf "CREATE_SUBFLOW6 %s (%s) => %s (%s)    "\
525                                "$e_saddr" "$e_from" "$e_daddr" "$e_to"
526                 else
527                         stdbuf -o0 -e0 printf "CREATE_SUBFLOW %s (%s) => %s (%s)         \t"\
528                                "$e_saddr" "$e_from" "$e_daddr" "$e_to"
529                 fi
530         else
531                 if [ "$e_family" = "$AF_INET6" ]
532                 then
533                         stdbuf -o0 -e0 printf "DESTROY_SUBFLOW6 %s (%s) => %s (%s)   "\
534                                "$e_saddr" "$e_from" "$e_daddr" "$e_to"
535                 else
536                         stdbuf -o0 -e0 printf "DESTROY_SUBFLOW %s (%s) => %s (%s)         \t"\
537                                "$e_saddr" "$e_from" "$e_daddr" "$e_to"
538                 fi
539         fi
540
541         type=$(sed --unbuffered -n 's/.*\(type:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
542         token=$(sed --unbuffered -n 's/.*\(token:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
543         family=$(sed --unbuffered -n 's/.*\(family:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
544         dport=$(sed --unbuffered -n 's/.*\(dport:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
545         locid=$(sed --unbuffered -n 's/.*\(loc_id:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
546         remid=$(sed --unbuffered -n 's/.*\(rem_id:\)\([[:digit:]]*\).*$/\2/p;q' "$evt")
547         if [ "$family" = "$AF_INET6" ]
548         then
549                 saddr=$(sed --unbuffered -n 's/.*\(saddr6:\)\([0-9a-f:.]*\).*$/\2/p;q' "$evt")
550                 daddr=$(sed --unbuffered -n 's/.*\(daddr6:\)\([0-9a-f:.]*\).*$/\2/p;q' "$evt")
551         else
552                 saddr=$(sed --unbuffered -n 's/.*\(saddr4:\)\([0-9.]*\).*$/\2/p;q' "$evt")
553                 daddr=$(sed --unbuffered -n 's/.*\(daddr4:\)\([0-9.]*\).*$/\2/p;q' "$evt")
554         fi
555
556         check_expected "type" "token" "daddr" "dport" "family" "saddr" "locid" "remid"
557 }
558
559 test_subflows()
560 {
561         print_title "Subflows v4 or v6 only tests"
562
563         # Capture events on the network namespace running the server
564         :>"$server_evts"
565
566         # Attempt to add a listener at 10.0.2.2:<subflow-port>
567         ip netns exec "$ns2" ./pm_nl_ctl listen 10.0.2.2\
568            "$client4_port" > /dev/null 2>&1 &
569         local listener_pid=$!
570
571         # ADD_ADDR from client to server machine reusing the subflow port
572         ip netns exec "$ns2" ./pm_nl_ctl ann 10.0.2.2 token "$client4_token" id\
573            $client_addr_id > /dev/null 2>&1
574         sleep 0.5
575
576         # CREATE_SUBFLOW from server to client machine
577         :>"$server_evts"
578         ip netns exec "$ns1" ./pm_nl_ctl csf lip 10.0.2.1 lid 23 rip 10.0.2.2\
579            rport "$client4_port" token "$server4_token" > /dev/null 2>&1
580         sleep 0.5
581         verify_subflow_events $server_evts $SUB_ESTABLISHED $server4_token $AF_INET "10.0.2.1" \
582                               "10.0.2.2" "$client4_port" "23" "$client_addr_id" "ns1" "ns2"
583
584         # Delete the listener from the client ns, if one was created
585         kill_wait $listener_pid
586
587         local sport
588         sport=$(sed --unbuffered -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q' "$server_evts")
589
590         # DESTROY_SUBFLOW from server to client machine
591         :>"$server_evts"
592         ip netns exec "$ns1" ./pm_nl_ctl dsf lip 10.0.2.1 lport "$sport" rip 10.0.2.2 rport\
593            "$client4_port" token "$server4_token" > /dev/null 2>&1
594         sleep 0.5
595         verify_subflow_events "$server_evts" "$SUB_CLOSED" "$server4_token" "$AF_INET" "10.0.2.1"\
596                               "10.0.2.2" "$client4_port" "23" "$client_addr_id" "ns1" "ns2"
597
598         # RM_ADDR from client to server machine
599         ip netns exec "$ns2" ./pm_nl_ctl rem id $client_addr_id token\
600            "$client4_token" > /dev/null 2>&1
601         sleep 0.5
602
603         # Attempt to add a listener at dead:beef:2::2:<subflow-port>
604         ip netns exec "$ns2" ./pm_nl_ctl listen dead:beef:2::2\
605            "$client6_port" > /dev/null 2>&1 &
606         listener_pid=$!
607
608         # ADD_ADDR6 from client to server machine reusing the subflow port
609         :>"$server_evts"
610         ip netns exec "$ns2" ./pm_nl_ctl ann dead:beef:2::2 token "$client6_token" id\
611            $client_addr_id > /dev/null 2>&1
612         sleep 0.5
613
614         # CREATE_SUBFLOW6 from server to client machine
615         :>"$server_evts"
616         ip netns exec "$ns1" ./pm_nl_ctl csf lip dead:beef:2::1 lid 23 rip\
617            dead:beef:2::2 rport "$client6_port" token "$server6_token" > /dev/null 2>&1
618         sleep 0.5
619         verify_subflow_events "$server_evts" "$SUB_ESTABLISHED" "$server6_token" "$AF_INET6"\
620                               "dead:beef:2::1" "dead:beef:2::2" "$client6_port" "23"\
621                               "$client_addr_id" "ns1" "ns2"
622
623         # Delete the listener from the client ns, if one was created
624         kill_wait $listener_pid
625
626         sport=$(sed --unbuffered -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q' "$server_evts")
627
628         # DESTROY_SUBFLOW6 from server to client machine
629         :>"$server_evts"
630         ip netns exec "$ns1" ./pm_nl_ctl dsf lip dead:beef:2::1 lport "$sport" rip\
631            dead:beef:2::2 rport "$client6_port" token "$server6_token" > /dev/null 2>&1
632         sleep 0.5
633         verify_subflow_events "$server_evts" "$SUB_CLOSED" "$server6_token" "$AF_INET6"\
634                               "dead:beef:2::1" "dead:beef:2::2" "$client6_port" "23"\
635                               "$client_addr_id" "ns1" "ns2"
636
637         # RM_ADDR from client to server machine
638         ip netns exec "$ns2" ./pm_nl_ctl rem id $client_addr_id token\
639            "$client6_token" > /dev/null 2>&1
640         sleep 0.5
641
642         # Attempt to add a listener at 10.0.2.2:<new-port>
643         ip netns exec "$ns2" ./pm_nl_ctl listen 10.0.2.2\
644            $new4_port > /dev/null 2>&1 &
645         listener_pid=$!
646
647         # ADD_ADDR from client to server machine using a new port
648         :>"$server_evts"
649         ip netns exec "$ns2" ./pm_nl_ctl ann 10.0.2.2 token "$client4_token" id\
650            $client_addr_id port $new4_port > /dev/null 2>&1
651         sleep 0.5
652
653         # CREATE_SUBFLOW from server to client machine
654         :>"$server_evts"
655         ip netns exec "$ns1" ./pm_nl_ctl csf lip 10.0.2.1 lid 23 rip 10.0.2.2 rport\
656            $new4_port token "$server4_token" > /dev/null 2>&1
657         sleep 0.5
658         verify_subflow_events "$server_evts" "$SUB_ESTABLISHED" "$server4_token" "$AF_INET"\
659                               "10.0.2.1" "10.0.2.2" "$new4_port" "23"\
660                               "$client_addr_id" "ns1" "ns2"
661
662         # Delete the listener from the client ns, if one was created
663         kill_wait $listener_pid
664
665         sport=$(sed --unbuffered -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q' "$server_evts")
666
667         # DESTROY_SUBFLOW from server to client machine
668         :>"$server_evts"
669         ip netns exec "$ns1" ./pm_nl_ctl dsf lip 10.0.2.1 lport "$sport" rip 10.0.2.2 rport\
670            $new4_port token "$server4_token" > /dev/null 2>&1
671         sleep 0.5
672         verify_subflow_events "$server_evts" "$SUB_CLOSED" "$server4_token" "$AF_INET" "10.0.2.1"\
673                               "10.0.2.2" "$new4_port" "23" "$client_addr_id" "ns1" "ns2"
674
675         # RM_ADDR from client to server machine
676         ip netns exec "$ns2" ./pm_nl_ctl rem id $client_addr_id token\
677            "$client4_token" > /dev/null 2>&1
678
679         # Capture events on the network namespace running the client
680         :>"$client_evts"
681
682         # Attempt to add a listener at 10.0.2.1:<subflow-port>
683         ip netns exec "$ns1" ./pm_nl_ctl listen 10.0.2.1\
684            $app4_port > /dev/null 2>&1 &
685         listener_pid=$!
686
687         # ADD_ADDR from server to client machine reusing the subflow port
688         ip netns exec "$ns1" ./pm_nl_ctl ann 10.0.2.1 token "$server4_token" id\
689            $server_addr_id > /dev/null 2>&1
690         sleep 0.5
691
692         # CREATE_SUBFLOW from client to server machine
693         :>"$client_evts"
694         ip netns exec "$ns2" ./pm_nl_ctl csf lip 10.0.2.2 lid 23 rip 10.0.2.1 rport\
695            $app4_port token "$client4_token" > /dev/null 2>&1
696         sleep 0.5
697         verify_subflow_events $client_evts $SUB_ESTABLISHED $client4_token $AF_INET "10.0.2.2"\
698                               "10.0.2.1" "$app4_port" "23" "$server_addr_id" "ns2" "ns1"
699
700         # Delete the listener from the server ns, if one was created
701         kill_wait $listener_pid
702
703         sport=$(sed --unbuffered -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q' "$client_evts")
704
705         # DESTROY_SUBFLOW from client to server machine
706         :>"$client_evts"
707         ip netns exec "$ns2" ./pm_nl_ctl dsf lip 10.0.2.2 lport "$sport" rip 10.0.2.1 rport\
708            $app4_port token "$client4_token" > /dev/null 2>&1
709         sleep 0.5
710         verify_subflow_events "$client_evts" "$SUB_CLOSED" "$client4_token" "$AF_INET" "10.0.2.2"\
711                               "10.0.2.1" "$app4_port" "23" "$server_addr_id" "ns2" "ns1"
712
713         # RM_ADDR from server to client machine
714         ip netns exec "$ns1" ./pm_nl_ctl rem id $server_addr_id token\
715            "$server4_token" > /dev/null 2>&1
716         sleep 0.5
717
718         # Attempt to add a listener at dead:beef:2::1:<subflow-port>
719         ip netns exec "$ns1" ./pm_nl_ctl listen dead:beef:2::1\
720            $app6_port > /dev/null 2>&1 &
721         listener_pid=$!
722
723         # ADD_ADDR6 from server to client machine reusing the subflow port
724         :>"$client_evts"
725         ip netns exec "$ns1" ./pm_nl_ctl ann dead:beef:2::1 token "$server6_token" id\
726            $server_addr_id > /dev/null 2>&1
727         sleep 0.5
728
729         # CREATE_SUBFLOW6 from client to server machine
730         :>"$client_evts"
731         ip netns exec "$ns2" ./pm_nl_ctl csf lip dead:beef:2::2 lid 23 rip\
732            dead:beef:2::1 rport $app6_port token "$client6_token" > /dev/null 2>&1
733         sleep 0.5
734         verify_subflow_events "$client_evts" "$SUB_ESTABLISHED" "$client6_token"\
735                               "$AF_INET6" "dead:beef:2::2"\
736                               "dead:beef:2::1" "$app6_port" "23"\
737                               "$server_addr_id" "ns2" "ns1"
738
739         # Delete the listener from the server ns, if one was created
740         kill_wait $listener_pid
741
742         sport=$(sed --unbuffered -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q' "$client_evts")
743
744         # DESTROY_SUBFLOW6 from client to server machine
745         :>"$client_evts"
746         ip netns exec "$ns2" ./pm_nl_ctl dsf lip dead:beef:2::2 lport "$sport" rip\
747            dead:beef:2::1 rport $app6_port token "$client6_token" > /dev/null 2>&1
748         sleep 0.5
749         verify_subflow_events $client_evts $SUB_CLOSED $client6_token $AF_INET6 "dead:beef:2::2"\
750                               "dead:beef:2::1" "$app6_port" "23" "$server_addr_id" "ns2" "ns1"
751
752         # RM_ADDR6 from server to client machine
753         ip netns exec "$ns1" ./pm_nl_ctl rem id $server_addr_id token\
754            "$server6_token" > /dev/null 2>&1
755         sleep 0.5
756
757         # Attempt to add a listener at 10.0.2.1:<new-port>
758         ip netns exec "$ns1" ./pm_nl_ctl listen 10.0.2.1\
759            $new4_port > /dev/null 2>&1 &
760         listener_pid=$!
761
762         # ADD_ADDR from server to client machine using a new port
763         :>"$client_evts"
764         ip netns exec "$ns1" ./pm_nl_ctl ann 10.0.2.1 token "$server4_token" id\
765            $server_addr_id port $new4_port > /dev/null 2>&1
766         sleep 0.5
767
768         # CREATE_SUBFLOW from client to server machine
769         :>"$client_evts"
770         ip netns exec "$ns2" ./pm_nl_ctl csf lip 10.0.2.2 lid 23 rip 10.0.2.1 rport\
771            $new4_port token "$client4_token" > /dev/null 2>&1
772         sleep 0.5
773         verify_subflow_events "$client_evts" "$SUB_ESTABLISHED" "$client4_token" "$AF_INET"\
774                               "10.0.2.2" "10.0.2.1" "$new4_port" "23" "$server_addr_id" "ns2" "ns1"
775
776         # Delete the listener from the server ns, if one was created
777         kill_wait $listener_pid
778
779         sport=$(sed --unbuffered -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q' "$client_evts")
780
781         # DESTROY_SUBFLOW from client to server machine
782         :>"$client_evts"
783         ip netns exec "$ns2" ./pm_nl_ctl dsf lip 10.0.2.2 lport "$sport" rip 10.0.2.1 rport\
784            $new4_port token "$client4_token" > /dev/null 2>&1
785         sleep 0.5
786         verify_subflow_events "$client_evts" "$SUB_CLOSED" "$client4_token" "$AF_INET" "10.0.2.2"\
787                               "10.0.2.1" "$new4_port" "23" "$server_addr_id" "ns2" "ns1"
788
789         # RM_ADDR from server to client machine
790         ip netns exec "$ns1" ./pm_nl_ctl rem id $server_addr_id token\
791            "$server4_token" > /dev/null 2>&1
792 }
793
794 test_subflows_v4_v6_mix()
795 {
796         print_title "Subflows v4 and v6 mix tests"
797
798         # Attempt to add a listener at 10.0.2.1:<subflow-port>
799         ip netns exec "$ns1" ./pm_nl_ctl listen 10.0.2.1\
800            $app6_port > /dev/null 2>&1 &
801         local listener_pid=$!
802
803         # ADD_ADDR4 from server to client machine reusing the subflow port on
804         # the established v6 connection
805         :>"$client_evts"
806         ip netns exec "$ns1" ./pm_nl_ctl ann 10.0.2.1 token "$server6_token" id\
807            $server_addr_id dev ns1eth2 > /dev/null 2>&1
808         stdbuf -o0 -e0 printf "ADD_ADDR4 id:%d 10.0.2.1 (ns1) => ns2, reuse port\t\t" $server_addr_id
809         sleep 0.5
810         verify_announce_event "$client_evts" "$ANNOUNCED" "$client6_token" "10.0.2.1"\
811                               "$server_addr_id" "$app6_port"
812
813         # CREATE_SUBFLOW from client to server machine
814         :>"$client_evts"
815         ip netns exec "$ns2" ./pm_nl_ctl csf lip 10.0.2.2 lid 23 rip 10.0.2.1 rport\
816            $app6_port token "$client6_token" > /dev/null 2>&1
817         sleep 0.5
818         verify_subflow_events "$client_evts" "$SUB_ESTABLISHED" "$client6_token"\
819                               "$AF_INET" "10.0.2.2" "10.0.2.1" "$app6_port" "23"\
820                               "$server_addr_id" "ns2" "ns1"
821
822         # Delete the listener from the server ns, if one was created
823         kill_wait $listener_pid
824
825         sport=$(sed --unbuffered -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q' "$client_evts")
826
827         # DESTROY_SUBFLOW from client to server machine
828         :>"$client_evts"
829         ip netns exec "$ns2" ./pm_nl_ctl dsf lip 10.0.2.2 lport "$sport" rip 10.0.2.1 rport\
830            $app6_port token "$client6_token" > /dev/null 2>&1
831         sleep 0.5
832         verify_subflow_events "$client_evts" "$SUB_CLOSED" "$client6_token" \
833                               "$AF_INET" "10.0.2.2" "10.0.2.1" "$app6_port" "23"\
834                               "$server_addr_id" "ns2" "ns1"
835
836         # RM_ADDR from server to client machine
837         ip netns exec "$ns1" ./pm_nl_ctl rem id $server_addr_id token\
838            "$server6_token" > /dev/null 2>&1
839         sleep 0.5
840 }
841
842 test_prio()
843 {
844         print_title "Prio tests"
845
846         local count
847
848         # Send MP_PRIO signal from client to server machine
849         ip netns exec "$ns2" ./pm_nl_ctl set 10.0.1.2 port "$client4_port" flags backup token "$client4_token" rip 10.0.1.1 rport "$server4_port"
850         sleep 0.5
851
852         # Check TX
853         stdbuf -o0 -e0 printf "MP_PRIO TX                                                 \t"
854         count=$(ip netns exec "$ns2" nstat -as | grep MPTcpExtMPPrioTx | awk '{print $2}')
855         [ -z "$count" ] && count=0
856         if [ $count != 1 ]; then
857                 stdbuf -o0 -e0 printf "[FAIL]\n\tCount != 1: %d\n" "${count}"
858                 exit 1
859         else
860                 stdbuf -o0 -e0 printf "[OK]\n"
861         fi
862
863         # Check RX
864         stdbuf -o0 -e0 printf "MP_PRIO RX                                                 \t"
865         count=$(ip netns exec "$ns1" nstat -as | grep MPTcpExtMPPrioRx | awk '{print $2}')
866         [ -z "$count" ] && count=0
867         if [ $count != 1 ]; then
868                 stdbuf -o0 -e0 printf "[FAIL]\n\tCount != 1: %d\n" "${count}"
869                 exit 1
870         else
871                 stdbuf -o0 -e0 printf "[OK]\n"
872         fi
873 }
874
875 verify_listener_events()
876 {
877         local evt=$1
878         local e_type=$2
879         local e_family=$3
880         local e_saddr=$4
881         local e_sport=$5
882         local type
883         local family
884         local saddr
885         local sport
886
887         if [ $e_type = $LISTENER_CREATED ]; then
888                 stdbuf -o0 -e0 printf "CREATE_LISTENER %s:%s\t\t\t\t\t"\
889                         $e_saddr $e_sport
890         elif [ $e_type = $LISTENER_CLOSED ]; then
891                 stdbuf -o0 -e0 printf "CLOSE_LISTENER %s:%s\t\t\t\t\t"\
892                         $e_saddr $e_sport
893         fi
894
895         type=$(grep "type:$e_type," $evt |
896                sed --unbuffered -n 's/.*\(type:\)\([[:digit:]]*\).*$/\2/p;q')
897         family=$(grep "type:$e_type," $evt |
898                  sed --unbuffered -n 's/.*\(family:\)\([[:digit:]]*\).*$/\2/p;q')
899         sport=$(grep "type:$e_type," $evt |
900                 sed --unbuffered -n 's/.*\(sport:\)\([[:digit:]]*\).*$/\2/p;q')
901         if [ $family ] && [ $family = $AF_INET6 ]; then
902                 saddr=$(grep "type:$e_type," $evt |
903                         sed --unbuffered -n 's/.*\(saddr6:\)\([0-9a-f:.]*\).*$/\2/p;q')
904         else
905                 saddr=$(grep "type:$e_type," $evt |
906                         sed --unbuffered -n 's/.*\(saddr4:\)\([0-9.]*\).*$/\2/p;q')
907         fi
908
909         check_expected "type" "family" "saddr" "sport"
910 }
911
912 test_listener()
913 {
914         print_title "Listener tests"
915
916         # Capture events on the network namespace running the client
917         :>$client_evts
918
919         # Attempt to add a listener at 10.0.2.2:<subflow-port>
920         ip netns exec $ns2 ./pm_nl_ctl listen 10.0.2.2\
921                 $client4_port > /dev/null 2>&1 &
922         local listener_pid=$!
923
924         verify_listener_events $client_evts $LISTENER_CREATED $AF_INET 10.0.2.2 $client4_port
925
926         # ADD_ADDR from client to server machine reusing the subflow port
927         ip netns exec $ns2 ./pm_nl_ctl ann 10.0.2.2 token $client4_token id\
928                 $client_addr_id > /dev/null 2>&1
929         sleep 0.5
930
931         # CREATE_SUBFLOW from server to client machine
932         ip netns exec $ns1 ./pm_nl_ctl csf lip 10.0.2.1 lid 23 rip 10.0.2.2\
933                 rport $client4_port token $server4_token > /dev/null 2>&1
934         sleep 0.5
935
936         # Delete the listener from the client ns, if one was created
937         kill_wait $listener_pid
938
939         verify_listener_events $client_evts $LISTENER_CLOSED $AF_INET 10.0.2.2 $client4_port
940 }
941
942 print_title "Make connections"
943 make_connection
944 make_connection "v6"
945
946 test_announce
947 test_remove
948 test_subflows
949 test_subflows_v4_v6_mix
950 test_prio
951 test_listener
952
953 exit 0