1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4NS=ns
5IP4=172.16.0.1/24
6TGT4=172.16.0.2
7IP6=2001:db8:1::1/64
8TGT6=2001:db8:1::2
9
10cleanup()
11{
12    ip netns del $NS
13}
14
15trap cleanup EXIT
16
17# Namespaces
18ip netns add $NS
19
20ip netns exec $NS sysctl -w net.ipv4.ping_group_range='0 2147483647' > /dev/null
21
22# Connectivity
23ip -netns $NS link add type dummy
24ip -netns $NS link set dev dummy0 up
25ip -netns $NS addr add $IP4 dev dummy0
26ip -netns $NS addr add $IP6 dev dummy0
27
28# Need FQ for TXTIME
29ip netns exec $NS tc qdisc replace dev dummy0 root fq
30
31# Test
32BAD=0
33TOTAL=0
34
35check_result() {
36    ((TOTAL++))
37    if [ $1 -ne 0 ]; then
38	echo "  Case $4 returned $1, expected 0"
39	((BAD++))
40    elif [ "$2" != "$3" ]; then
41	echo "  Case $4 returned '$2', expected '$3'"
42	((BAD++))
43    fi
44}
45
46for i in "-4 $TGT4" "-6 $TGT6"; do
47    for p in u i r; do
48	[ $p == "u" ] && prot=UDPv${i:1:2}
49	[ $p == "i" ] && prot=ICMPv${i:1:2}
50	[ $p == "r" ] && prot=RAWv${i:1:2}
51
52	ts=$(ip netns exec $NS ./cmsg_sender -p $p $i 1234)
53	check_result $? "$ts" "" "$prot - no options"
54
55	ts=$(ip netns exec $NS ./cmsg_sender -p $p $i 1234 -t | wc -l)
56	check_result $? "$ts" "2" "$prot - ts cnt"
57	ts=$(ip netns exec $NS ./cmsg_sender -p $p $i 1234 -t |
58		 sed -n "s/.*SCHED ts0 [0-9].*/OK/p")
59	check_result $? "$ts" "OK" "$prot - ts0 SCHED"
60	ts=$(ip netns exec $NS ./cmsg_sender -p $p $i 1234 -t |
61		 sed -n "s/.*SND ts0 [0-9].*/OK/p")
62	check_result $? "$ts" "OK" "$prot - ts0 SND"
63
64	ts=$(ip netns exec $NS ./cmsg_sender -p $p $i 1234 -t -d 1000 |
65		 awk '/SND/ { if ($3 > 1000) print "OK"; }')
66	check_result $? "$ts" "OK" "$prot - TXTIME abs"
67
68	ts=$(ip netns exec $NS ./cmsg_sender -p $p $i 1234 -t -d 1000 |
69		 awk '/SND/ {snd=$3}
70		      /SCHED/ {sch=$3}
71		      END { if (snd - sch > 500) print "OK"; }')
72	check_result $? "$ts" "OK" "$prot - TXTIME rel"
73    done
74done
75
76# Summary
77if [ $BAD -ne 0 ]; then
78    echo "FAIL - $BAD/$TOTAL cases failed"
79    exit 1
80else
81    echo "OK"
82    exit 0
83fi
84