1b64b0d1eSJesper Dangaard Brouer# 2b64b0d1eSJesper Dangaard Brouer# Common functions used by pktgen scripts 3b64b0d1eSJesper Dangaard Brouer# - Depending on bash 3 (or higher) syntax 4b64b0d1eSJesper Dangaard Brouer# 5b64b0d1eSJesper Dangaard Brouer# Author: Jesper Dangaaard Brouer 6b64b0d1eSJesper Dangaard Brouer# License: GPL 7b64b0d1eSJesper Dangaard Brouer 8*3cad8f91SDaniel T. Leeset -o errexit 9*3cad8f91SDaniel T. Lee 10b64b0d1eSJesper Dangaard Brouer## -- General shell logging cmds -- 11b64b0d1eSJesper Dangaard Brouerfunction err() { 12b64b0d1eSJesper Dangaard Brouer local exitcode=$1 13b64b0d1eSJesper Dangaard Brouer shift 14b64b0d1eSJesper Dangaard Brouer echo "ERROR: $@" >&2 15b64b0d1eSJesper Dangaard Brouer exit $exitcode 16b64b0d1eSJesper Dangaard Brouer} 17b64b0d1eSJesper Dangaard Brouer 18b64b0d1eSJesper Dangaard Brouerfunction warn() { 19b64b0d1eSJesper Dangaard Brouer echo "WARN : $@" >&2 20b64b0d1eSJesper Dangaard Brouer} 21b64b0d1eSJesper Dangaard Brouer 22b64b0d1eSJesper Dangaard Brouerfunction info() { 23b64b0d1eSJesper Dangaard Brouer if [[ -n "$VERBOSE" ]]; then 24b64b0d1eSJesper Dangaard Brouer echo "INFO : $@" >&2 25b64b0d1eSJesper Dangaard Brouer fi 26b64b0d1eSJesper Dangaard Brouer} 27b64b0d1eSJesper Dangaard Brouer 28b64b0d1eSJesper Dangaard Brouer## -- Pktgen proc config commands -- ## 29b64b0d1eSJesper Dangaard Brouerexport PROC_DIR=/proc/net/pktgen 30b64b0d1eSJesper Dangaard Brouer# 31b64b0d1eSJesper Dangaard Brouer# Three different shell functions for configuring the different 32b64b0d1eSJesper Dangaard Brouer# components of pktgen: 33b64b0d1eSJesper Dangaard Brouer# pg_ctrl(), pg_thread() and pg_set(). 34b64b0d1eSJesper Dangaard Brouer# 35b64b0d1eSJesper Dangaard Brouer# These functions correspond to pktgens different components. 36b64b0d1eSJesper Dangaard Brouer# * pg_ctrl() control "pgctrl" (/proc/net/pktgen/pgctrl) 37b64b0d1eSJesper Dangaard Brouer# * pg_thread() control the kernel threads and binding to devices 38b64b0d1eSJesper Dangaard Brouer# * pg_set() control setup of individual devices 39b64b0d1eSJesper Dangaard Brouerfunction pg_ctrl() { 40b64b0d1eSJesper Dangaard Brouer local proc_file="pgctrl" 41b64b0d1eSJesper Dangaard Brouer proc_cmd ${proc_file} "$@" 42b64b0d1eSJesper Dangaard Brouer} 43b64b0d1eSJesper Dangaard Brouer 44b64b0d1eSJesper Dangaard Brouerfunction pg_thread() { 45b64b0d1eSJesper Dangaard Brouer local thread=$1 46b64b0d1eSJesper Dangaard Brouer local proc_file="kpktgend_${thread}" 47b64b0d1eSJesper Dangaard Brouer shift 48b64b0d1eSJesper Dangaard Brouer proc_cmd ${proc_file} "$@" 49b64b0d1eSJesper Dangaard Brouer} 50b64b0d1eSJesper Dangaard Brouer 51b64b0d1eSJesper Dangaard Brouerfunction pg_set() { 52b64b0d1eSJesper Dangaard Brouer local dev=$1 53b64b0d1eSJesper Dangaard Brouer local proc_file="$dev" 54b64b0d1eSJesper Dangaard Brouer shift 55b64b0d1eSJesper Dangaard Brouer proc_cmd ${proc_file} "$@" 56b64b0d1eSJesper Dangaard Brouer} 57b64b0d1eSJesper Dangaard Brouer 58b64b0d1eSJesper Dangaard Brouer# More generic replacement for pgset(), that does not depend on global 59b64b0d1eSJesper Dangaard Brouer# variable for proc file. 60b64b0d1eSJesper Dangaard Brouerfunction proc_cmd() { 61b64b0d1eSJesper Dangaard Brouer local result 62b64b0d1eSJesper Dangaard Brouer local proc_file=$1 63*3cad8f91SDaniel T. Lee local status=0 64b64b0d1eSJesper Dangaard Brouer # after shift, the remaining args are contained in $@ 65b64b0d1eSJesper Dangaard Brouer shift 66b64b0d1eSJesper Dangaard Brouer local proc_ctrl=${PROC_DIR}/$proc_file 67b64b0d1eSJesper Dangaard Brouer if [[ ! -e "$proc_ctrl" ]]; then 68b64b0d1eSJesper Dangaard Brouer err 3 "proc file:$proc_ctrl does not exists (dev added to thread?)" 69b64b0d1eSJesper Dangaard Brouer else 70b64b0d1eSJesper Dangaard Brouer if [[ ! -w "$proc_ctrl" ]]; then 71b64b0d1eSJesper Dangaard Brouer err 4 "proc file:$proc_ctrl not writable, not root?!" 72b64b0d1eSJesper Dangaard Brouer fi 73b64b0d1eSJesper Dangaard Brouer fi 74b64b0d1eSJesper Dangaard Brouer 75b64b0d1eSJesper Dangaard Brouer if [[ "$DEBUG" == "yes" ]]; then 76b64b0d1eSJesper Dangaard Brouer echo "cmd: $@ > $proc_ctrl" 77b64b0d1eSJesper Dangaard Brouer fi 78b64b0d1eSJesper Dangaard Brouer # Quoting of "$@" is important for space expansion 79*3cad8f91SDaniel T. Lee echo "$@" > "$proc_ctrl" || status=$? 80b64b0d1eSJesper Dangaard Brouer 81*3cad8f91SDaniel T. Lee if [[ "$proc_file" != "pgctrl" ]]; then 82*3cad8f91SDaniel T. Lee result=$(grep "Result: OK:" $proc_ctrl) || true 83b64b0d1eSJesper Dangaard Brouer if [[ "$result" == "" ]]; then 84b64b0d1eSJesper Dangaard Brouer grep "Result:" $proc_ctrl >&2 85b64b0d1eSJesper Dangaard Brouer fi 86*3cad8f91SDaniel T. Lee fi 87b64b0d1eSJesper Dangaard Brouer if (( $status != 0 )); then 88b64b0d1eSJesper Dangaard Brouer err 5 "Write error($status) occurred cmd: \"$@ > $proc_ctrl\"" 89b64b0d1eSJesper Dangaard Brouer fi 90b64b0d1eSJesper Dangaard Brouer} 91b64b0d1eSJesper Dangaard Brouer 92b64b0d1eSJesper Dangaard Brouer# Old obsolete "pgset" function, with slightly improved err handling 93b64b0d1eSJesper Dangaard Brouerfunction pgset() { 94b64b0d1eSJesper Dangaard Brouer local result 95b64b0d1eSJesper Dangaard Brouer 96b64b0d1eSJesper Dangaard Brouer if [[ "$DEBUG" == "yes" ]]; then 97b64b0d1eSJesper Dangaard Brouer echo "cmd: $1 > $PGDEV" 98b64b0d1eSJesper Dangaard Brouer fi 99b64b0d1eSJesper Dangaard Brouer echo $1 > $PGDEV 100b64b0d1eSJesper Dangaard Brouer local status=$? 101b64b0d1eSJesper Dangaard Brouer 102b64b0d1eSJesper Dangaard Brouer result=`cat $PGDEV | fgrep "Result: OK:"` 103b64b0d1eSJesper Dangaard Brouer if [[ "$result" == "" ]]; then 104b64b0d1eSJesper Dangaard Brouer cat $PGDEV | fgrep Result: 105b64b0d1eSJesper Dangaard Brouer fi 106b64b0d1eSJesper Dangaard Brouer if (( $status != 0 )); then 107b64b0d1eSJesper Dangaard Brouer err 5 "Write error($status) occurred cmd: \"$1 > $PGDEV\"" 108b64b0d1eSJesper Dangaard Brouer fi 109b64b0d1eSJesper Dangaard Brouer} 110b64b0d1eSJesper Dangaard Brouer 111*3cad8f91SDaniel T. Lee[[ $EUID -eq 0 ]] && trap 'pg_ctrl "reset"' EXIT 112*3cad8f91SDaniel T. Lee 113b64b0d1eSJesper Dangaard Brouer## -- General shell tricks -- 114b64b0d1eSJesper Dangaard Brouer 115b64b0d1eSJesper Dangaard Brouerfunction root_check_run_with_sudo() { 116b64b0d1eSJesper Dangaard Brouer # Trick so, program can be run as normal user, will just use "sudo" 117b64b0d1eSJesper Dangaard Brouer # call as root_check_run_as_sudo "$@" 118b64b0d1eSJesper Dangaard Brouer if [ "$EUID" -ne 0 ]; then 119b64b0d1eSJesper Dangaard Brouer if [ -x $0 ]; then # Directly executable use sudo 120b64b0d1eSJesper Dangaard Brouer info "Not root, running with sudo" 121b64b0d1eSJesper Dangaard Brouer sudo "$0" "$@" 122b64b0d1eSJesper Dangaard Brouer exit $? 123b64b0d1eSJesper Dangaard Brouer fi 124b64b0d1eSJesper Dangaard Brouer err 4 "cannot perform sudo run of $0" 125b64b0d1eSJesper Dangaard Brouer fi 126b64b0d1eSJesper Dangaard Brouer} 12722ac5ad4SRobert Hoo 12822ac5ad4SRobert Hoo# Exact input device's NUMA node info 12922ac5ad4SRobert Hoofunction get_iface_node() 13022ac5ad4SRobert Hoo{ 13122ac5ad4SRobert Hoo local node=$(</sys/class/net/$1/device/numa_node) 13222ac5ad4SRobert Hoo if [[ $node == -1 ]]; then 13322ac5ad4SRobert Hoo echo 0 13422ac5ad4SRobert Hoo else 13522ac5ad4SRobert Hoo echo $node 13622ac5ad4SRobert Hoo fi 13722ac5ad4SRobert Hoo} 13822ac5ad4SRobert Hoo 13922ac5ad4SRobert Hoo# Given an Dev/iface, get its queues' irq numbers 14022ac5ad4SRobert Hoofunction get_iface_irqs() 14122ac5ad4SRobert Hoo{ 14222ac5ad4SRobert Hoo local IFACE=$1 14322ac5ad4SRobert Hoo local queues="${IFACE}-.*TxRx" 14422ac5ad4SRobert Hoo 14522ac5ad4SRobert Hoo irqs=$(grep "$queues" /proc/interrupts | cut -f1 -d:) 14622ac5ad4SRobert Hoo [ -z "$irqs" ] && irqs=$(grep $IFACE /proc/interrupts | cut -f1 -d:) 14722ac5ad4SRobert Hoo [ -z "$irqs" ] && irqs=$(for i in `ls -Ux /sys/class/net/$IFACE/device/msi_irqs` ;\ 14822ac5ad4SRobert Hoo do grep "$i:.*TxRx" /proc/interrupts | grep -v fdir | cut -f 1 -d : ;\ 14922ac5ad4SRobert Hoo done) 15022ac5ad4SRobert Hoo [ -z "$irqs" ] && err 3 "Could not find interrupts for $IFACE" 15122ac5ad4SRobert Hoo 15222ac5ad4SRobert Hoo echo $irqs 15322ac5ad4SRobert Hoo} 15422ac5ad4SRobert Hoo 15522ac5ad4SRobert Hoo# Given a NUMA node, return cpu ids belonging to it. 15622ac5ad4SRobert Hoofunction get_node_cpus() 15722ac5ad4SRobert Hoo{ 15822ac5ad4SRobert Hoo local node=$1 15922ac5ad4SRobert Hoo local node_cpu_list 16022ac5ad4SRobert Hoo local node_cpu_range_list=`cut -f1- -d, --output-delimiter=" " \ 16122ac5ad4SRobert Hoo /sys/devices/system/node/node$node/cpulist` 16222ac5ad4SRobert Hoo 16322ac5ad4SRobert Hoo for cpu_range in $node_cpu_range_list 16422ac5ad4SRobert Hoo do 16522ac5ad4SRobert Hoo node_cpu_list="$node_cpu_list "`seq -s " " ${cpu_range//-/ }` 16622ac5ad4SRobert Hoo done 16722ac5ad4SRobert Hoo 16822ac5ad4SRobert Hoo echo $node_cpu_list 16922ac5ad4SRobert Hoo} 170226b96c2SDaniel T. Lee 171226b96c2SDaniel T. Lee# Given a single or range of port(s), return minimum and maximum port number. 172226b96c2SDaniel T. Leefunction parse_ports() 173226b96c2SDaniel T. Lee{ 174226b96c2SDaniel T. Lee local port_str=$1 175226b96c2SDaniel T. Lee local port_list 176226b96c2SDaniel T. Lee local min_port 177226b96c2SDaniel T. Lee local max_port 178226b96c2SDaniel T. Lee 179226b96c2SDaniel T. Lee IFS="-" read -ra port_list <<< $port_str 180226b96c2SDaniel T. Lee 181226b96c2SDaniel T. Lee min_port=${port_list[0]} 182226b96c2SDaniel T. Lee max_port=${port_list[1]:-$min_port} 183226b96c2SDaniel T. Lee 184226b96c2SDaniel T. Lee echo $min_port $max_port 185226b96c2SDaniel T. Lee} 186226b96c2SDaniel T. Lee 187226b96c2SDaniel T. Lee# Given a minimum and maximum port, verify port number. 188226b96c2SDaniel T. Leefunction validate_ports() 189226b96c2SDaniel T. Lee{ 190226b96c2SDaniel T. Lee local min_port=$1 191226b96c2SDaniel T. Lee local max_port=$2 192226b96c2SDaniel T. Lee 193226b96c2SDaniel T. Lee # 0 < port < 65536 194226b96c2SDaniel T. Lee if [[ $min_port -gt 0 && $min_port -lt 65536 ]]; then 195226b96c2SDaniel T. Lee if [[ $max_port -gt 0 && $max_port -lt 65536 ]]; then 196226b96c2SDaniel T. Lee if [[ $min_port -le $max_port ]]; then 197226b96c2SDaniel T. Lee return 0 198226b96c2SDaniel T. Lee fi 199226b96c2SDaniel T. Lee fi 200226b96c2SDaniel T. Lee fi 201226b96c2SDaniel T. Lee 202226b96c2SDaniel T. Lee err 5 "Invalid port(s): $min_port-$max_port" 203226b96c2SDaniel T. Lee} 204