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