2 # Common functions used by pktgen scripts
3 # - Depending on bash 3 (or higher) syntax
5 # Author: Jesper Dangaaard Brouer
8 ## -- General shell logging cmds --
21 if [[ -n "$VERBOSE" ]]; then
26 ## -- Pktgen proc config commands -- ##
27 export PROC_DIR
=/proc
/net
/pktgen
29 # Three different shell functions for configuring the different
30 # components of pktgen:
31 # pg_ctrl(), pg_thread() and pg_set().
33 # These functions correspond to pktgens different components.
34 # * pg_ctrl() control "pgctrl" (/proc/net/pktgen/pgctrl)
35 # * pg_thread() control the kernel threads and binding to devices
36 # * pg_set() control setup of individual devices
38 local proc_file
="pgctrl"
39 proc_cmd
${proc_file} "$@"
42 function pg_thread
() {
44 local proc_file
="kpktgend_${thread}"
46 proc_cmd
${proc_file} "$@"
51 local proc_file
="$dev"
53 proc_cmd
${proc_file} "$@"
56 # More generic replacement for pgset(), that does not depend on global
57 # variable for proc file.
61 # after shift, the remaining args are contained in $@
63 local proc_ctrl
=${PROC_DIR}/$proc_file
64 if [[ ! -e "$proc_ctrl" ]]; then
65 err
3 "proc file:$proc_ctrl does not exists (dev added to thread?)"
67 if [[ ! -w "$proc_ctrl" ]]; then
68 err
4 "proc file:$proc_ctrl not writable, not root?!"
72 if [[ "$DEBUG" == "yes" ]]; then
73 echo "cmd: $@ > $proc_ctrl"
75 # Quoting of "$@" is important for space expansion
76 echo "$@" > "$proc_ctrl"
79 result
=$
(grep "Result: OK:" $proc_ctrl)
80 # Due to pgctrl, cannot use exit code $? from grep
81 if [[ "$result" == "" ]]; then
82 grep "Result:" $proc_ctrl >&2
84 if (( $status != 0 )); then
85 err
5 "Write error($status) occurred cmd: \"$@ > $proc_ctrl\""
89 # Old obsolete "pgset" function, with slightly improved err handling
93 if [[ "$DEBUG" == "yes" ]]; then
94 echo "cmd: $1 > $PGDEV"
99 result
=`cat $PGDEV | fgrep "Result: OK:"`
100 if [[ "$result" == "" ]]; then
101 cat $PGDEV | fgrep Result
:
103 if (( $status != 0 )); then
104 err
5 "Write error($status) occurred cmd: \"$1 > $PGDEV\""
108 ## -- General shell tricks --
110 function root_check_run_with_sudo
() {
111 # Trick so, program can be run as normal user, will just use "sudo"
112 # call as root_check_run_as_sudo "$@"
113 if [ "$EUID" -ne 0 ]; then
114 if [ -x $0 ]; then # Directly executable use sudo
115 info
"Not root, running with sudo"
119 err
4 "cannot perform sudo run of $0"
123 # Exact input device's NUMA node info
124 function get_iface_node
()
126 local node
=$
(</sys
/class
/net
/$1/device
/numa_node
)
127 if [[ $node == -1 ]]; then
134 # Given an Dev/iface, get its queues' irq numbers
135 function get_iface_irqs
()
138 local queues
="${IFACE}-.*TxRx"
140 irqs
=$
(grep "$queues" /proc
/interrupts | cut
-f1 -d:)
141 [ -z "$irqs" ] && irqs
=$
(grep $IFACE /proc
/interrupts | cut
-f1 -d:)
142 [ -z "$irqs" ] && irqs
=$
(for i
in `ls -Ux /sys/class/net/$IFACE/device/msi_irqs` ;\
143 do grep "$i:.*TxRx" /proc
/interrupts |
grep -v fdir | cut
-f 1 -d : ;\
145 [ -z "$irqs" ] && err
3 "Could not find interrupts for $IFACE"
150 # Given a NUMA node, return cpu ids belonging to it.
151 function get_node_cpus
()
155 local node_cpu_range_list
=`cut -f1- -d, --output-delimiter=" " \
156 /sys/devices/system/node/node$node/cpulist`
158 for cpu_range
in $node_cpu_range_list
160 node_cpu_list
="$node_cpu_list "`seq -s " " ${cpu_range//-/ }`
166 # Given a single or range of port(s), return minimum and maximum port number.
167 function parse_ports
()
174 IFS
="-" read -ra port_list
<<< $port_str
176 min_port
=${port_list[0]}
177 max_port
=${port_list[1]:-$min_port}
179 echo $min_port $max_port
182 # Given a minimum and maximum port, verify port number.
183 function validate_ports
()
189 if [[ $min_port -gt 0 && $min_port -lt 65536 ]]; then
190 if [[ $max_port -gt 0 && $max_port -lt 65536 ]]; then
191 if [[ $min_port -le $max_port ]]; then
197 err
5 "Invalid port(s): $min_port-$max_port"