1# 2# Common functions used by pktgen scripts 3# - Depending on bash 3 (or higher) syntax 4# 5# Author: Jesper Dangaaard Brouer 6# License: GPL 7 8set -o errexit 9 10## -- General shell logging cmds -- 11function err() { 12 local exitcode=$1 13 shift 14 echo "ERROR: $@" >&2 15 exit $exitcode 16} 17 18function warn() { 19 echo "WARN : $@" >&2 20} 21 22function info() { 23 if [[ -n "$VERBOSE" ]]; then 24 echo "INFO : $@" >&2 25 fi 26} 27 28## -- Pktgen proc config commands -- ## 29export PROC_DIR=/proc/net/pktgen 30# 31# Three different shell functions for configuring the different 32# components of pktgen: 33# pg_ctrl(), pg_thread() and pg_set(). 34# 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 39function pg_ctrl() { 40 local proc_file="pgctrl" 41 proc_cmd ${proc_file} "$@" 42} 43 44function pg_thread() { 45 local thread=$1 46 local proc_file="kpktgend_${thread}" 47 shift 48 proc_cmd ${proc_file} "$@" 49} 50 51function pg_set() { 52 local dev=$1 53 local proc_file="$dev" 54 shift 55 proc_cmd ${proc_file} "$@" 56} 57 58# More generic replacement for pgset(), that does not depend on global 59# variable for proc file. 60function 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?!" 72 fi 73 fi 74 75 if [[ "$DEBUG" == "yes" ]]; then 76 echo "cmd: $@ > $proc_ctrl" 77 fi 78 # Quoting of "$@" is important for space expansion 79 echo "$@" > "$proc_ctrl" || status=$? 80 81 if [[ "$proc_file" != "pgctrl" ]]; then 82 result=$(grep "Result: OK:" $proc_ctrl) || true 83 if [[ "$result" == "" ]]; then 84 grep "Result:" $proc_ctrl >&2 85 fi 86 fi 87 if (( $status != 0 )); then 88 err 5 "Write error($status) occurred cmd: \"$@ > $proc_ctrl\"" 89 fi 90} 91 92# Old obsolete "pgset" function, with slightly improved err handling 93function pgset() { 94 local result 95 96 if [[ "$DEBUG" == "yes" ]]; then 97 echo "cmd: $1 > $PGDEV" 98 fi 99 echo $1 > $PGDEV 100 local status=$? 101 102 result=`cat $PGDEV | fgrep "Result: OK:"` 103 if [[ "$result" == "" ]]; then 104 cat $PGDEV | fgrep Result: 105 fi 106 if (( $status != 0 )); then 107 err 5 "Write error($status) occurred cmd: \"$1 > $PGDEV\"" 108 fi 109} 110 111[[ $EUID -eq 0 ]] && trap 'pg_ctrl "reset"' EXIT 112 113## -- General shell tricks -- 114 115function root_check_run_with_sudo() { 116 # Trick so, program can be run as normal user, will just use "sudo" 117 # call as root_check_run_as_sudo "$@" 118 if [ "$EUID" -ne 0 ]; then 119 if [ -x $0 ]; then # Directly executable use sudo 120 info "Not root, running with sudo" 121 sudo "$0" "$@" 122 exit $? 123 fi 124 err 4 "cannot perform sudo run of $0" 125 fi 126} 127 128# Exact input device's NUMA node info 129function get_iface_node() 130{ 131 local node=$(</sys/class/net/$1/device/numa_node) 132 if [[ $node == -1 ]]; then 133 echo 0 134 else 135 echo $node 136 fi 137} 138 139# Given an Dev/iface, get its queues' irq numbers 140function get_iface_irqs() 141{ 142 local IFACE=$1 143 local queues="${IFACE}-.*TxRx" 144 145 irqs=$(grep "$queues" /proc/interrupts | cut -f1 -d:) 146 [ -z "$irqs" ] && irqs=$(grep $IFACE /proc/interrupts | cut -f1 -d:) 147 [ -z "$irqs" ] && irqs=$(for i in `ls -Ux /sys/class/net/$IFACE/device/msi_irqs` ;\ 148 do grep "$i:.*TxRx" /proc/interrupts | grep -v fdir | cut -f 1 -d : ;\ 149 done) 150 [ -z "$irqs" ] && err 3 "Could not find interrupts for $IFACE" 151 152 echo $irqs 153} 154 155# Given a NUMA node, return cpu ids belonging to it. 156function get_node_cpus() 157{ 158 local node=$1 159 local node_cpu_list 160 local node_cpu_range_list=`cut -f1- -d, --output-delimiter=" " \ 161 /sys/devices/system/node/node$node/cpulist` 162 163 for cpu_range in $node_cpu_range_list 164 do 165 node_cpu_list="$node_cpu_list "`seq -s " " ${cpu_range//-/ }` 166 done 167 168 echo $node_cpu_list 169} 170 171# Given a single or range of port(s), return minimum and maximum port number. 172function parse_ports() 173{ 174 local port_str=$1 175 local port_list 176 local min_port 177 local max_port 178 179 IFS="-" read -ra port_list <<< $port_str 180 181 min_port=${port_list[0]} 182 max_port=${port_list[1]:-$min_port} 183 184 echo $min_port $max_port 185} 186 187# Given a minimum and maximum port, verify port number. 188function validate_ports() 189{ 190 local min_port=$1 191 local max_port=$2 192 193 # 0 < port < 65536 194 if [[ $min_port -gt 0 && $min_port -lt 65536 ]]; then 195 if [[ $max_port -gt 0 && $max_port -lt 65536 ]]; then 196 if [[ $min_port -le $max_port ]]; then 197 return 0 198 fi 199 fi 200 fi 201 202 err 5 "Invalid port(s): $min_port-$max_port" 203} 204