drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / samples / pktgen / functions.sh
blobc08cefb8eb1f5ad8a6eb177440c525e33a9037f9
2 # Common functions used by pktgen scripts
3 # - Depending on bash 3 (or higher) syntax
5 # Author: Jesper Dangaaard Brouer
6 # License: GPL
8 set -o errexit
10 ## -- General shell logging cmds --
11 function err() {
12 local exitcode=$1
13 shift
14 echo "ERROR: $@" >&2
15 exit $exitcode
18 function warn() {
19 echo "WARN : $@" >&2
22 function info() {
23 if [[ -n "$VERBOSE" ]]; then
24 echo "INFO : $@" >&2
28 ## -- Pktgen proc config commands -- ##
29 export PROC_DIR=/proc/net/pktgen
31 # Three different shell functions for configuring the different
32 # components of pktgen:
33 # pg_ctrl(), pg_thread() and pg_set().
35 # These functions correspond to pktgens different components.
36 # * pg_ctrl() control "pgctrl" (/proc/net/pktgen/pgctrl)
37 # * pg_thread() control the kernel threads and binding to devices
38 # * pg_set() control setup of individual devices
39 function pg_ctrl() {
40 local proc_file="pgctrl"
41 proc_cmd ${proc_file} "$@"
44 function pg_thread() {
45 local thread=$1
46 local proc_file="kpktgend_${thread}"
47 shift
48 proc_cmd ${proc_file} "$@"
51 function pg_set() {
52 local dev=$1
53 local proc_file="$dev"
54 shift
55 proc_cmd ${proc_file} "$@"
58 # More generic replacement for pgset(), that does not depend on global
59 # variable for proc file.
60 function proc_cmd() {
61 local result
62 local proc_file=$1
63 local status=0
64 # after shift, the remaining args are contained in $@
65 shift
66 local proc_ctrl=${PROC_DIR}/$proc_file
67 if [[ ! -e "$proc_ctrl" ]]; then
68 err 3 "proc file:$proc_ctrl does not exists (dev added to thread?)"
69 else
70 if [[ ! -w "$proc_ctrl" ]]; then
71 err 4 "proc file:$proc_ctrl not writable, not root?!"
75 if [[ "$DEBUG" == "yes" ]]; then
76 echo "cmd: $@ > $proc_ctrl"
78 # Quoting of "$@" is important for space expansion
79 echo "$@" > "$proc_ctrl" || status=$?
81 if [[ "$proc_file" != "pgctrl" ]]; then
82 result=$(grep "Result: OK:" $proc_ctrl) || true
83 if [[ "$result" == "" ]]; then
84 grep "Result:" $proc_ctrl >&2
87 if (( $status != 0 )); then
88 err 5 "Write error($status) occurred cmd: \"$@ > $proc_ctrl\""
92 # Old obsolete "pgset" function, with slightly improved err handling
93 function pgset() {
94 local result
96 if [[ "$DEBUG" == "yes" ]]; then
97 echo "cmd: $1 > $PGDEV"
99 echo $1 > $PGDEV
100 local status=$?
102 result=`cat $PGDEV | fgrep "Result: OK:"`
103 if [[ "$result" == "" ]]; then
104 cat $PGDEV | fgrep Result:
106 if (( $status != 0 )); then
107 err 5 "Write error($status) occurred cmd: \"$1 > $PGDEV\""
111 function trap_exit()
113 # Cleanup pktgen setup on exit if thats not "append mode"
114 if [[ -z "$APPEND" ]] && [[ $EUID -eq 0 ]]; then
115 trap 'pg_ctrl "reset"' EXIT
119 ## -- General shell tricks --
121 function root_check_run_with_sudo() {
122 # Trick so, program can be run as normal user, will just use "sudo"
123 # call as root_check_run_as_sudo "$@"
124 if [ "$EUID" -ne 0 ]; then
125 if [ -x $0 ]; then # Directly executable use sudo
126 info "Not root, running with sudo"
127 sudo -E "$0" "$@"
128 exit $?
130 err 4 "cannot perform sudo run of $0"
134 # Exact input device's NUMA node info
135 function get_iface_node()
137 local node=$(</sys/class/net/$1/device/numa_node)
138 if [[ $node == -1 ]]; then
139 echo 0
140 else
141 echo $node
145 # Given an Dev/iface, get its queues' irq numbers
146 function get_iface_irqs()
148 local IFACE=$1
149 local queues="${IFACE}-.*TxRx"
151 irqs=$(grep "$queues" /proc/interrupts | cut -f1 -d:)
152 [ -z "$irqs" ] && irqs=$(grep $IFACE /proc/interrupts | cut -f1 -d:)
153 [ -z "$irqs" ] && irqs=$(for i in `ls -Ux /sys/class/net/$IFACE/device/msi_irqs` ;\
154 do grep "$i:.*TxRx" /proc/interrupts | grep -v fdir | cut -f 1 -d : ;\
155 done)
156 [ -z "$irqs" ] && err 3 "Could not find interrupts for $IFACE"
158 echo $irqs
161 # Given a NUMA node, return cpu ids belonging to it.
162 function get_node_cpus()
164 local node=$1
165 local node_cpu_list
166 local node_cpu_range_list=`cut -f1- -d, --output-delimiter=" " \
167 /sys/devices/system/node/node$node/cpulist`
169 for cpu_range in $node_cpu_range_list
171 node_cpu_list="$node_cpu_list "`seq -s " " ${cpu_range//-/ }`
172 done
174 echo $node_cpu_list
177 # Check $1 is in between $2, $3 ($2 <= $1 <= $3)
178 function in_between() { [[ ($1 -ge $2) && ($1 -le $3) ]] ; }
180 # Extend shrunken IPv6 address.
181 # fe80::42:bcff:fe84:e10a => fe80:0:0:0:42:bcff:fe84:e10a
182 function extend_addr6()
184 local addr=$1
185 local sep=: sep2=::
186 local sep_cnt=$(tr -cd $sep <<< $1 | wc -c)
187 local shrink
189 # separator count should be (2 <= $sep_cnt <= 7)
190 if ! (in_between $sep_cnt 2 7); then
191 err 5 "Invalid IP6 address: $1"
194 # if shrink '::' occurs multiple, it's malformed.
195 shrink=( $(grep -E -o "$sep{2,}" <<< $addr) )
196 if [[ ${#shrink[@]} -ne 0 ]]; then
197 if [[ ${#shrink[@]} -gt 1 || ( ${shrink[0]} != $sep2 ) ]]; then
198 err 5 "Invalid IP6 address: $1"
202 # add 0 at begin & end, and extend addr by adding :0
203 [[ ${addr:0:1} == $sep ]] && addr=0${addr}
204 [[ ${addr: -1} == $sep ]] && addr=${addr}0
205 echo "${addr/$sep2/$(printf ':0%.s' $(seq $[8-sep_cnt])):}"
208 # Given a single IP(v4/v6) address, whether it is valid.
209 function validate_addr()
211 # check function is called with (funcname)6
212 [[ ${FUNCNAME[1]: -1} == 6 ]] && local IP6=6
213 local bitlen=$[ IP6 ? 128 : 32 ]
214 local len=$[ IP6 ? 8 : 4 ]
215 local max=$[ 2**(len*2)-1 ]
216 local net prefix
217 local addr sep
219 IFS='/' read net prefix <<< $1
220 [[ $IP6 ]] && net=$(extend_addr6 $net)
222 # if prefix exists, check (0 <= $prefix <= $bitlen)
223 if [[ -n $prefix ]]; then
224 if ! (in_between $prefix 0 $bitlen); then
225 err 5 "Invalid prefix: /$prefix"
229 # set separator for each IP(v4/v6)
230 [[ $IP6 ]] && sep=: || sep=.
231 IFS=$sep read -a addr <<< $net
233 # array length
234 if [[ ${#addr[@]} != $len ]]; then
235 err 5 "Invalid IP$IP6 address: $1"
238 # check each digit (0 <= $digit <= $max)
239 for digit in "${addr[@]}"; do
240 [[ $IP6 ]] && digit=$[ 16#$digit ]
241 if ! (in_between $digit 0 $max); then
242 err 5 "Invalid IP$IP6 address: $1"
244 done
246 return 0
249 function validate_addr6() { validate_addr $@ ; }
251 # Given a single IP(v4/v6) or CIDR, return minimum and maximum IP addr.
252 function parse_addr()
254 # check function is called with (funcname)6
255 [[ ${FUNCNAME[1]: -1} == 6 ]] && local IP6=6
256 local net prefix
257 local min_ip max_ip
259 IFS='/' read net prefix <<< $1
260 [[ $IP6 ]] && net=$(extend_addr6 $net)
262 if [[ -z $prefix ]]; then
263 min_ip=$net
264 max_ip=$net
265 else
266 # defining array for converting Decimal 2 Binary
267 # 00000000 00000001 00000010 00000011 00000100 ...
268 local d2b='{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}'
269 [[ $IP6 ]] && d2b+=$d2b
270 eval local D2B=($d2b)
272 local bitlen=$[ IP6 ? 128 : 32 ]
273 local remain=$[ bitlen-prefix ]
274 local octet=$[ IP6 ? 16 : 8 ]
275 local min_mask max_mask
276 local min max
277 local ip_bit
278 local ip sep
280 # set separator for each IP(v4/v6)
281 [[ $IP6 ]] && sep=: || sep=.
282 IFS=$sep read -ra ip <<< $net
284 min_mask="$(printf '1%.s' $(seq $prefix))$(printf '0%.s' $(seq $remain))"
285 max_mask="$(printf '0%.s' $(seq $prefix))$(printf '1%.s' $(seq $remain))"
287 # calculate min/max ip with &,| operator
288 for i in "${!ip[@]}"; do
289 digit=$[ IP6 ? 16#${ip[$i]} : ${ip[$i]} ]
290 ip_bit=${D2B[$digit]}
292 idx=$[ octet*i ]
293 min[$i]=$[ 2#$ip_bit & 2#${min_mask:$idx:$octet} ]
294 max[$i]=$[ 2#$ip_bit | 2#${max_mask:$idx:$octet} ]
295 [[ $IP6 ]] && { min[$i]=$(printf '%X' ${min[$i]});
296 max[$i]=$(printf '%X' ${max[$i]}); }
297 done
299 min_ip=$(IFS=$sep; echo "${min[*]}")
300 max_ip=$(IFS=$sep; echo "${max[*]}")
303 echo $min_ip $max_ip
306 function parse_addr6() { parse_addr $@ ; }
308 # Given a single or range of port(s), return minimum and maximum port number.
309 function parse_ports()
311 local port_str=$1
312 local port_list
313 local min_port
314 local max_port
316 IFS="-" read -ra port_list <<< $port_str
318 min_port=${port_list[0]}
319 max_port=${port_list[1]:-$min_port}
321 echo $min_port $max_port
324 # Given a minimum and maximum port, verify port number.
325 function validate_ports()
327 local min_port=$1
328 local max_port=$2
330 # 1 <= port <= 65535
331 if (in_between $min_port 1 65535); then
332 if (in_between $max_port 1 65535); then
333 if [[ $min_port -le $max_port ]]; then
334 return 0
339 err 5 "Invalid port(s): $min_port-$max_port"