1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# Send packets with transmit timestamps over loopback with netem
5# Verify that timestamps correspond to netem delay
6
7set -e
8
9setup() {
10	# set 1ms delay on lo egress
11	tc qdisc add dev lo root netem delay 1ms
12
13	# set 2ms delay on ifb0 egress
14	modprobe ifb
15	ip link add ifb_netem0 type ifb
16	ip link set dev ifb_netem0 up
17	tc qdisc add dev ifb_netem0 root netem delay 2ms
18
19	# redirect lo ingress through ifb0 egress
20	tc qdisc add dev lo handle ffff: ingress
21	tc filter add dev lo parent ffff: \
22		u32 match mark 0 0xffff \
23		action mirred egress redirect dev ifb_netem0
24}
25
26run_test_v4v6() {
27	# SND will be delayed 1000us
28	# ACK will be delayed 6000us: 1 + 2 ms round-trip
29	local -r args="$@ -v 1000 -V 6000"
30
31	./txtimestamp ${args} -4 -L 127.0.0.1
32	./txtimestamp ${args} -6 -L ::1
33}
34
35run_test_tcpudpraw() {
36	local -r args=$@
37
38	run_test_v4v6 ${args}		# tcp
39	run_test_v4v6 ${args} -u	# udp
40	run_test_v4v6 ${args} -r	# raw
41	run_test_v4v6 ${args} -R	# raw (IPPROTO_RAW)
42	run_test_v4v6 ${args} -P	# pf_packet
43}
44
45run_test_all() {
46	setup
47	run_test_tcpudpraw		# setsockopt
48	run_test_tcpudpraw -C		# cmsg
49	run_test_tcpudpraw -n		# timestamp w/o data
50	echo "OK. All tests passed"
51}
52
53run_test_one() {
54	setup
55	./txtimestamp $@
56}
57
58usage() {
59	echo "Usage: $0 [ -r | --run ] <txtimestamp args> | [ -h | --help ]"
60	echo "  (no args)  Run all tests"
61	echo "  -r|--run  Run an individual test with arguments"
62	echo "  -h|--help Help"
63}
64
65main() {
66	if [[ $# -eq 0 ]]; then
67		run_test_all
68	else
69		if [[ "$1" = "-r" || "$1" == "--run" ]]; then
70			shift
71			run_test_one $@
72		else
73			usage
74		fi
75	fi
76}
77
78if [[ -z "$(ip netns identify)" ]]; then
79	./in_netns.sh $0 $@
80else
81	main $@
82fi
83