Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / testing / selftests / net / forwarding / tsn_lib.sh
blobb91bcd8008a9930529e6235c3a5d45e04740cf6b
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright 2021-2022 NXP
5 REQUIRE_ISOCHRON=${REQUIRE_ISOCHRON:=yes}
6 REQUIRE_LINUXPTP=${REQUIRE_LINUXPTP:=yes}
8 # Tunables
9 UTC_TAI_OFFSET=37
10 ISOCHRON_CPU=1
12 if [[ "$REQUIRE_ISOCHRON" = "yes" ]]; then
13 # https://github.com/vladimiroltean/tsn-scripts
14 # WARNING: isochron versions pre-1.0 are unstable,
15 # always use the latest version
16 require_command isochron
18 if [[ "$REQUIRE_LINUXPTP" = "yes" ]]; then
19 require_command phc2sys
20 require_command ptp4l
23 phc2sys_start()
25 local uds_address=$1
26 local extra_args=""
28 if ! [ -z "${uds_address}" ]; then
29 extra_args="${extra_args} -z ${uds_address}"
32 phc2sys_log="$(mktemp)"
34 chrt -f 10 phc2sys -m \
35 -a -rr \
36 --step_threshold 0.00002 \
37 --first_step_threshold 0.00002 \
38 ${extra_args} \
39 > "${phc2sys_log}" 2>&1 &
40 phc2sys_pid=$!
42 echo "phc2sys logs to ${phc2sys_log} and has pid ${phc2sys_pid}"
44 sleep 1
47 phc2sys_stop()
49 { kill ${phc2sys_pid} && wait ${phc2sys_pid}; } 2> /dev/null
50 rm "${phc2sys_log}" 2> /dev/null
53 # Replace space separators from interface list with underscores
54 if_names_to_label()
56 local if_name_list="$1"
58 echo "${if_name_list/ /_}"
61 ptp4l_start()
63 local if_names="$1"
64 local slave_only=$2
65 local uds_address=$3
66 local log="ptp4l_log_$(if_names_to_label ${if_names})"
67 local pid="ptp4l_pid_$(if_names_to_label ${if_names})"
68 local extra_args=""
70 for if_name in ${if_names}; do
71 extra_args="${extra_args} -i ${if_name}"
72 done
74 if [ "${slave_only}" = true ]; then
75 extra_args="${extra_args} -s"
78 # declare dynamic variables ptp4l_log_${if_name} and ptp4l_pid_${if_name}
79 # as global, so that they can be referenced later
80 declare -g "${log}=$(mktemp)"
82 chrt -f 10 ptp4l -m -2 -P \
83 --step_threshold 0.00002 \
84 --first_step_threshold 0.00002 \
85 --tx_timestamp_timeout 100 \
86 --uds_address="${uds_address}" \
87 ${extra_args} \
88 > "${!log}" 2>&1 &
89 declare -g "${pid}=$!"
91 echo "ptp4l for interfaces ${if_names} logs to ${!log} and has pid ${!pid}"
93 sleep 1
96 ptp4l_stop()
98 local if_names="$1"
99 local log="ptp4l_log_$(if_names_to_label ${if_names})"
100 local pid="ptp4l_pid_$(if_names_to_label ${if_names})"
102 { kill ${!pid} && wait ${!pid}; } 2> /dev/null
103 rm "${!log}" 2> /dev/null
106 cpufreq_max()
108 local cpu=$1
109 local freq="cpu${cpu}_freq"
110 local governor="cpu${cpu}_governor"
112 # Kernel may be compiled with CONFIG_CPU_FREQ disabled
113 if ! [ -d /sys/bus/cpu/devices/cpu${cpu}/cpufreq ]; then
114 return
117 # declare dynamic variables cpu${cpu}_freq and cpu${cpu}_governor as
118 # global, so they can be referenced later
119 declare -g "${freq}=$(cat /sys/bus/cpu/devices/cpu${cpu}/cpufreq/scaling_min_freq)"
120 declare -g "${governor}=$(cat /sys/bus/cpu/devices/cpu${cpu}/cpufreq/scaling_governor)"
122 cat /sys/bus/cpu/devices/cpu${cpu}/cpufreq/scaling_max_freq > \
123 /sys/bus/cpu/devices/cpu${cpu}/cpufreq/scaling_min_freq
124 echo -n "performance" > \
125 /sys/bus/cpu/devices/cpu${cpu}/cpufreq/scaling_governor
128 cpufreq_restore()
130 local cpu=$1
131 local freq="cpu${cpu}_freq"
132 local governor="cpu${cpu}_governor"
134 if ! [ -d /sys/bus/cpu/devices/cpu${cpu}/cpufreq ]; then
135 return
138 echo "${!freq}" > /sys/bus/cpu/devices/cpu${cpu}/cpufreq/scaling_min_freq
139 echo -n "${!governor}" > \
140 /sys/bus/cpu/devices/cpu${cpu}/cpufreq/scaling_governor
143 isochron_recv_start()
145 local if_name=$1
146 local uds=$2
147 local stats_port=$3
148 local extra_args=$4
149 local pid="isochron_pid_${stats_port}"
151 if ! [ -z "${uds}" ]; then
152 extra_args="${extra_args} --unix-domain-socket ${uds}"
155 isochron rcv \
156 --interface ${if_name} \
157 --sched-priority 98 \
158 --sched-fifo \
159 --utc-tai-offset ${UTC_TAI_OFFSET} \
160 --stats-port ${stats_port} \
161 --quiet \
162 ${extra_args} & \
163 declare -g "${pid}=$!"
165 sleep 1
168 isochron_recv_stop()
170 local stats_port=$1
171 local pid="isochron_pid_${stats_port}"
173 { kill ${!pid} && wait ${!pid}; } 2> /dev/null
176 isochron_do()
178 local sender_if_name=$1; shift
179 local receiver_if_name=$1; shift
180 local sender_uds=$1; shift
181 local receiver_uds=$1; shift
182 local base_time=$1; shift
183 local cycle_time=$1; shift
184 local shift_time=$1; shift
185 local num_pkts=$1; shift
186 local vid=$1; shift
187 local priority=$1; shift
188 local dst_ip=$1; shift
189 local isochron_dat=$1; shift
190 local extra_args=""
191 local receiver_extra_args=""
192 local vrf="$(master_name_get ${sender_if_name})"
193 local use_l2="true"
195 if ! [ -z "${dst_ip}" ]; then
196 use_l2="false"
199 if ! [ -z "${vrf}" ]; then
200 dst_ip="${dst_ip}%${vrf}"
203 if ! [ -z "${vid}" ]; then
204 vid="--vid=${vid}"
207 if [ -z "${receiver_uds}" ]; then
208 extra_args="${extra_args} --omit-remote-sync"
211 if ! [ -z "${shift_time}" ]; then
212 extra_args="${extra_args} --shift-time=${shift_time}"
215 if [ "${use_l2}" = "true" ]; then
216 extra_args="${extra_args} --l2 --etype=0xdead ${vid}"
217 receiver_extra_args="--l2 --etype=0xdead"
218 else
219 extra_args="${extra_args} --l4 --ip-destination=${dst_ip}"
220 receiver_extra_args="--l4"
223 cpufreq_max ${ISOCHRON_CPU}
225 isochron_recv_start "${h2}" "${receiver_uds}" 5000 "${receiver_extra_args}"
227 isochron send \
228 --interface ${sender_if_name} \
229 --unix-domain-socket ${sender_uds} \
230 --priority ${priority} \
231 --base-time ${base_time} \
232 --cycle-time ${cycle_time} \
233 --num-frames ${num_pkts} \
234 --frame-size 64 \
235 --txtime \
236 --utc-tai-offset ${UTC_TAI_OFFSET} \
237 --cpu-mask $((1 << ${ISOCHRON_CPU})) \
238 --sched-fifo \
239 --sched-priority 98 \
240 --client 127.0.0.1 \
241 --sync-threshold 5000 \
242 --output-file ${isochron_dat} \
243 ${extra_args} \
244 --quiet
246 isochron_recv_stop 5000
248 cpufreq_restore ${ISOCHRON_CPU}